5 |
-- modify it under the terms of the GNU General Public License |
-- modify it under the terms of the GNU General Public License |
6 |
-- as published by the Free Software Foundation; either version 2 |
-- as published by the Free Software Foundation; either version 2 |
7 |
-- of the License, or (at your option) any later version. |
-- of the License, or (at your option) any later version. |
8 |
-- |
-- |
9 |
-- This program is distributed in the hope that it will be useful, |
-- This program is distributed in the hope that it will be useful, |
10 |
-- but WITHOUT ANY WARRANTY; without even the implied warranty of |
-- but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 |
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 |
-- GNU General Public License for more details. |
-- GNU General Public License for more details. |
13 |
-- |
-- |
14 |
-- You should have received a copy of the GNU General Public License along |
-- You should have received a copy of the GNU General Public License along |
15 |
-- with this program; if not, write to the Free Software Foundation, Inc., |
-- with this program; if not, write to the Free Software Foundation, Inc., |
16 |
-- 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. |
-- 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. |
20 |
-- |
-- |
21 |
-- This module contains a support functions for designing enigma maps. |
-- This module contains a support functions for designing enigma maps. |
22 |
-- There is a documentation available, please read it if you want |
-- There is a documentation available, please read it if you want |
23 |
-- to find out how to use this (see <enigma-dir>/doc/ant_lua.html), |
-- to find out how to use this (see <enigma-dir>/doc/ant_lua.txt), |
24 |
-- or look at how miscelaneous ant??.lua maps are done. |
-- or look at how miscelaneous ant??.lua maps are done. |
25 |
-- Any questions, bug reports or anything related to this module please send to: |
-- Any questions, bug reports or anything related to this module please send to: |
26 |
-- my e-mail: ant_39 at centrum.cz |
-- my e-mail: ant_39 at centrum.cz |
49 |
-- changes in multiples (there are more kinds, and interfacing changed). |
-- changes in multiples (there are more kinds, and interfacing changed). |
50 |
-- A bug in multichar cell key fixed |
-- A bug in multichar cell key fixed |
51 |
-- 2003-02-25 -- |
-- 2003-02-25 -- |
52 |
-- add_multi* now uses tinsert/tremove, functions that work with multiples adapted where neccessary |
-- add_multi* now uses tinsert/tremove, functions that work with multiples adapted where necessary |
53 |
-- some comments fixed, few minor fixes across the source. warning(). |
-- some comments fixed, few minor fixes across the source. warning(). |
54 |
-- 2003-03-08 -- |
-- 2003-03-08 -- |
55 |
-- support for parents that do not accept coordinates, default cell key meanings, updates in default |
-- support for parents that do not accept coordinates, default cell key meanings, updates in default |
56 |
-- parent handling, debug mode |
-- parent handling, debug mode |
57 |
-- 2003-03-13 -- several cosmetic changes plus add_multitag, spread_tag, wormholes generator and slope generator |
-- 2003-03-13 -- several cosmetic changes plus add_multitag, spread_tag, wormholes generator and slope generator |
58 |
-- 2003-03-21 -- warning may raise error if warning_raises_error is set to 1 (ralf) |
-- 2003-03-21 -- warning may raise error if warning_raises_error is set to 1 (ralf) |
59 |
|
-- 2003-03-30 -- |
60 |
|
-- some comments fixed, be_pedantic() added, add_multitag fix, draw_map and get_map_size |
61 |
|
-- were broken up to several parts so that particular tasks may be solved as designer wishes. |
62 |
|
-- cell0.tag removed - I'm not sure if it was ever used at all... |
63 |
|
-- some improvements in handling default cell key meanings at multichar maps |
64 |
|
|
65 |
|
|
66 |
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- |
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- |
67 |
-- MISCELANEOUS - -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- |
-- MISCELANEOUS - -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- |
68 |
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- |
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- |
69 |
-- for warning messages |
-- for warning messages |
70 |
function warning(text) |
function warning(text) |
71 |
if (warning_raises_error and (warning_raises_error == 1)) then |
if (warning_raises_error) then |
72 |
error(" [ant.lua]: "..text) |
error(" [ant.lua]: "..text) |
73 |
else |
else |
74 |
print("warning: [ant.lua]: "..text) |
print("warning: [ant.lua]: "..text) |
75 |
end |
end |
76 |
end |
end |
77 |
|
|
78 |
|
-- turn warning messages to errors |
79 |
|
-- usage: be_pedantic() -> turn on raising errors instead of warnings |
80 |
|
function be_pedantic(mode) |
81 |
|
warning_raises_error = ((mode==nil)or(mode~=0)) |
82 |
|
end |
83 |
|
|
84 |
-- for debug messages |
-- for debug messages |
85 |
local ldm={str="", cnt=0} |
local ldm={str="", cnt=0} |
86 |
function debug(text) |
function debug(text) |
96 |
end |
end |
97 |
end |
end |
98 |
|
|
99 |
|
-- debug mode |
100 |
|
-- in debug mode, ant.lua produces messages that inform you about the execution |
101 |
|
DEBUG_MODE = nil |
102 |
|
function debug_mode() |
103 |
|
DEBUG_MODE = 1 |
104 |
|
debug("debug_mode: debug mode turned on") |
105 |
|
end |
106 |
|
|
107 |
|
-- turn off debug mode |
108 |
|
function debug_mode_off() |
109 |
|
debug("debug_mode_off: turning debug mode off") |
110 |
|
DEBUG_MODE = nil |
111 |
|
end |
112 |
|
|
113 |
-- x,y should be numbers, although chars and strings are generally also the right choice |
-- x,y should be numbers, although chars and strings are generally also the right choice |
114 |
function getkey(...) |
function getkey(...) |
115 |
local key="" |
local key="" |
120 |
return key |
return key |
121 |
end |
end |
122 |
|
|
123 |
|
-- todo: deep clone - clone also nested tables |
124 |
|
function clone_table(tab) |
125 |
|
local ntab = {} |
126 |
|
|
127 |
|
for key,val in tab do |
128 |
|
ntab[key] = val |
129 |
|
end |
130 |
|
|
131 |
|
return ntab; |
132 |
|
end |
133 |
|
|
134 |
-- each ant.lua function, that accepts coordinates, may be |
-- each ant.lua function, that accepts coordinates, may be |
135 |
-- called with negative coordinates as well. That in fact |
-- called with negative coordinates as well. That in fact |
136 |
-- means to place the object relatively to lower right |
-- means to place the object relatively to lower right |
181 |
-- VISUAL MAP CREATION -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- |
-- VISUAL MAP CREATION -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- |
182 |
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- |
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- |
183 |
|
|
|
-- debug mode |
|
|
-- in debug mode, ant.lua produces messages that inform you about the execution |
|
|
DEBUG_MODE = nil |
|
|
function debug_mode() |
|
|
DEBUG_MODE = 1 |
|
|
debug("debug mode turned on") |
|
|
end |
|
|
function debug_mode_off() |
|
|
debug("") |
|
|
DEBUG_MODE = nil |
|
|
warning("debug mode turned off") |
|
|
end |
|
|
|
|
184 |
-- width of key in map definition |
-- width of key in map definition |
185 |
-- CELL_KEY_WIDTH means how many characters occupy single map cell. In several maps I strongly |
-- CELL_KEY_WIDTH means how many characters occupy single map cell. In several maps I strongly |
186 |
-- wanted this functions (cannonball for example), and after all here it is. Usable in maps with |
-- wanted this functions (cannonball for example), and after all here it is. Usable in maps with |
189 |
function set_cell_key_width(w) |
function set_cell_key_width(w) |
190 |
if ( (type(w) == "number") and (w>0) and (floor(w)==w) ) then |
if ( (type(w) == "number") and (w>0) and (floor(w)==w) ) then |
191 |
CELL_KEY_WIDTH = w |
CELL_KEY_WIDTH = w |
192 |
debug("CELL_KEY_WIDTH is: "..w) |
debug("set_cell_key_width: CELL_KEY_WIDTH is: "..w) |
193 |
return w |
return w |
194 |
else |
else |
195 |
warning("CELL_KEY_WIDTH has to be positive whole number") |
warning("set_cell_key_width: CELL_KEY_WIDTH has to be positive whole number") |
196 |
return -1 |
return -1 |
197 |
end |
end |
198 |
end |
end |
199 |
|
|
200 |
-- this function tries to guess the width of key based on the most common width of keys in cell[key] |
-- get a table with cell functions |
201 |
-- also there is possibility to get level as argument and try to divide its width by common level widths, |
-- with no argument given, global cells is looked for. |
202 |
-- such as 20, 39, ... this way it would be possible to get the value also... |
-- if none is present, warning occurs. |
203 |
-- Anyway, I think first test is better. |
function get_cellfuncs(cellfuncs) |
204 |
|
local funcs = cellfuncs or cells; -- hope for a global cells |
205 |
|
|
206 |
|
if (not(funcs)) then |
207 |
|
warning("get_cellfuncs: no cellfuncs declared and global variable 'cells' is absent.") |
208 |
|
funcs = {}; |
209 |
|
else |
210 |
|
debug("get_cellfuncs: ok, found cellfuncs table.") |
211 |
|
end |
212 |
|
|
213 |
|
return funcs; |
214 |
|
end |
215 |
|
|
216 |
|
-- this function tries to guess the width of key. |
217 |
|
-- It does so by looking for the most common width of keys in cells[key] |
218 |
function guess_cell_key_width(cellfuncs) |
function guess_cell_key_width(cellfuncs) |
219 |
local funcs = cellfuncs or cells or {} -- hope for a global cells |
local funcs = get_cellfuncs(cellfuncs) |
220 |
local freq_table = {} |
local freq_table = {} |
221 |
|
|
222 |
for key,_ in funcs do |
for key,_ in funcs do |
235 |
end |
end |
236 |
end |
end |
237 |
|
|
238 |
debug("CELL_KEY_WIDTH guessed "..maxw) |
debug("guess_cell_key_width: CELL_KEY_WIDTH guessed "..maxw) |
239 |
return set_cell_key_width(maxw) |
return set_cell_key_width(maxw) |
240 |
end |
end |
241 |
|
|
247 |
-- or string argument. The table will be built from this. |
-- or string argument. The table will be built from this. |
248 |
DEFAULT_CELL_PARENT = nil |
DEFAULT_CELL_PARENT = nil |
249 |
function set_default_parent(func) |
function set_default_parent(func) |
250 |
|
if (not(func)) then |
251 |
|
debug("set_default_parent: default parent turned off") |
252 |
|
DEFAULT_CELL_PARENT = nil |
253 |
|
return |
254 |
|
end |
255 |
|
|
256 |
if ((type(func)=="function")or(type(func)=="string")) then |
if ((type(func)=="function")or(type(func)=="string")) then |
257 |
func = {func} |
func = {func} |
258 |
end |
end |
261 |
for key,f in func do |
for key,f in func do |
262 |
local ftype = type(f) |
local ftype = type(f) |
263 |
if ((ftype~="function")and(ftype~="string")) then |
if ((ftype~="function")and(ftype~="string")) then |
264 |
warning("element "..key.." of DEFAULT_CELL_PARENT has to be function or string, not "..ftype.."!") |
warning("set_default_parent: element "..key.." of DEFAULT_CELL_PARENT\n".. |
265 |
|
"has to be function or string, not "..ftype.."!") |
266 |
return |
return |
267 |
end |
end |
268 |
end |
end |
269 |
DEFAULT_CELL_PARENT = func |
DEFAULT_CELL_PARENT = func |
270 |
else |
else |
271 |
warning("default parent has to be function, table of functions or string key") |
warning("set_default_parent: default parent has to be\n".. |
272 |
|
"a function, table of functions or string key") |
273 |
end |
end |
274 |
end |
end |
275 |
|
|
298 |
-- + pmode - parent arglist handling mode. Defaults to 0 and it means where to put (x,y) couple in arglist |
-- + pmode - parent arglist handling mode. Defaults to 0 and it means where to put (x,y) couple in arglist |
299 |
-- ++ pmode = -1 => do not place the (x,y) couple to arglist |
-- ++ pmode = -1 => do not place the (x,y) couple to arglist |
300 |
-- ++ pmode = +0 => place it before parent arguments |
-- ++ pmode = +0 => place it before parent arguments |
301 |
|
|
302 |
|
-- ATTENTION: there *has* to be a coordlist even in case of pmode=-1, or NO arguments may be passed: |
303 |
|
-- -> func() |
304 |
|
-- -> func(x,y) |
305 |
|
-- -> func(x,y, arg1, arg2, ...) |
306 |
|
-- -> func(arg1, arg2) --> INCORRECT!! |
307 |
|
|
308 |
function cell(structure) |
function cell(structure) |
309 |
local cell0 = {} |
local cell0 = {} |
310 |
|
|
|
cell0.tag = (structure.tag or "?") |
|
311 |
cell0.floor = cell_item(structure.floor or {}) |
cell0.floor = cell_item(structure.floor or {}) |
312 |
cell0.stone = cell_item(structure.stone or {}) |
cell0.stone = cell_item(structure.stone or {}) |
313 |
cell0.item = cell_item(structure.item or {}) |
cell0.item = cell_item(structure.item or {}) |
323 |
elseif (type(parent)=="table") then |
elseif (type(parent)=="table") then |
324 |
-- this is O.K. |
-- this is O.K. |
325 |
else |
else |
326 |
warning("illegal: parent has to be a function, a table of functions") |
warning("cell{}: parent has to be a function, a table of functions\n".. |
327 |
warning("or a table of curry-functions, not "..type(parent)) |
"or a table of curry-functions, not "..type(parent)) |
328 |
parent={} |
parent={} |
329 |
end |
end |
330 |
|
|
332 |
if (type(parent[a])=="function") then |
if (type(parent[a])=="function") then |
333 |
parent[a] = {parent[a]} |
parent[a] = {parent[a]} |
334 |
elseif (type(parent[a])=="table") then |
elseif (type(parent[a])=="table") then |
335 |
-- this is O.K. => a curry function call {func, arg1, arg2, ...} |
if (type(parent[a][1])=="function") then |
336 |
|
-- this is O.K. => a curry function call {func, arg1, arg2, ...} |
337 |
|
else |
338 |
|
warning("cell{}: parent element number "..a.." is a curry-function\n".. |
339 |
|
"construction, but it's first element has to be a function, not "..type(parent[a])..".\n".. |
340 |
|
"Using empty function instead.") |
341 |
|
parent[a]={function() end} |
342 |
|
end |
343 |
else |
else |
344 |
warning("illegal: parent element id "..a.." should be a function or") |
warning("cell{}: parent element number "..a.." should be a function\n".. |
345 |
warning("a curry-function construction, not "..type(parent[a])) |
"or a curry-function construction, not "..type(parent[a])..".\n".. |
346 |
|
"Using empty function instead.") |
347 |
parent[a]={function() end} |
parent[a]={function() end} |
348 |
end |
end |
349 |
end |
end |
405 |
end |
end |
406 |
end |
end |
407 |
|
|
408 |
|
-- height is just number of lines |
409 |
|
function get_map_height(map) |
410 |
|
return getn(map) |
411 |
|
end |
412 |
|
|
413 |
|
-- width of particular map row |
414 |
|
function map_row_length(y, map) |
415 |
|
return floor(strlen(map[y])/CELL_KEY_WIDTH); |
416 |
|
end |
417 |
|
|
418 |
-- width - this is width of the widest row of map |
-- width - this is width of the widest row of map |
419 |
function get_map_width(level) |
function get_map_width(map) |
420 |
local levelw = 0 |
local mapw = 0 |
421 |
|
|
422 |
for _,str in level do |
for y=1,get_map_height(map) do |
423 |
if strlen(str)>levelw then |
local w = map_row_length(y, map) |
424 |
levelw = strlen(str) |
if w>mapw then mapw = w end |
|
end |
|
425 |
end |
end |
426 |
|
|
427 |
return floor(levelw/CELL_KEY_WIDTH) |
return mapw |
|
end |
|
|
|
|
|
-- height is just number of lines |
|
|
function get_map_height(level) |
|
|
return getn(level) |
|
428 |
end |
end |
429 |
|
|
430 |
-- and size combined |
-- and size combined |
431 |
function get_map_size(level) |
function get_map_size(map) |
432 |
return get_map_width(level), get_map_height(level) |
return get_map_width(map), get_map_height(map) |
433 |
end |
end |
434 |
|
|
435 |
-- common map |
-- common map |
436 |
-- if a key has no meaning declared in given 'cellfuncs', we'll look here |
-- if a key has no meaning declared in given 'cellfuncs', we'll look here |
437 |
-- whether there is no 'default' meaning |
-- whether there is a 'default' meaning |
438 |
-- look at the bottom of ant.lua to see the map |
-- look at the bottom of ant.lua to see the map |
439 |
DEFAULT_KEY_MEANING={} |
DEFAULT_KEY_MEANING={} |
440 |
function map_cell_meaning(key, func) |
function map_cell_meaning(key, func) |
450 |
end |
end |
451 |
end |
end |
452 |
|
|
453 |
|
-- function picks a function from table of default cell keys |
454 |
|
-- and results it |
455 |
|
function default_cell(key) |
456 |
|
return DEFAULT_KEY_MEANING[key] |
457 |
|
end |
458 |
|
|
459 |
-- this function accepts a cell key and cellfuncs table |
-- this function accepts a cell key and cellfuncs table |
460 |
-- it looks into that table and if there is no func with given key, it returns default value instead |
-- it looks into that table and if there is no func with given key, it returns default |
461 |
function get_cell_func(key, funcs) |
-- value instead and inserts the value to given cellfuncs table |
462 |
if (funcs[key]) then |
function get_cell_func(key, cellfuncs) |
463 |
return funcs[key] |
if (cellfuncs[key]) then |
464 |
|
return cellfuncs[key] |
465 |
elseif (DEFAULT_KEY_MEANING[key]) then |
elseif (DEFAULT_KEY_MEANING[key]) then |
466 |
debug("using DEFAULT_KEY_MEANING of '"..key.."'") |
debug("get_cell_func: using DEFAULT_KEY_MEANING of '"..key.."'") |
467 |
|
cellfuncs[key] = DEFAULT_KEY_MEANING[key] |
468 |
return DEFAULT_KEY_MEANING[key] |
return DEFAULT_KEY_MEANING[key] |
469 |
else |
else |
470 |
warning("no default meaning found for map key '"..key.."'") |
-- for multichar maps, try by the first char |
471 |
|
if (strlen(key)>1) then |
472 |
|
local ret = get_cell_func(strsub(key, 1, 1), cellfuncs) |
473 |
|
if (ret) then |
474 |
|
debug("get_cell_func: as a meaning of the key '"..key.."'") |
475 |
|
cellfuncs[key] = ret; |
476 |
|
return ret |
477 |
|
end |
478 |
|
end |
479 |
|
|
480 |
|
warning("get_cell_func: no function declared for map key '"..key.."'") |
481 |
return nil |
return nil |
482 |
end |
end |
483 |
end |
end |
484 |
|
|
485 |
-- this draws the map to the position [x0,y0] |
-- render a map square by key |
486 |
-- map is array of strings. Each string is one line of result map, each char is one map square. |
-- rx,ry are x,y coordinates of given cell on map |
487 |
function draw_map(x0, y0, map, cellfuncs) |
-- key is a cell descriptor |
488 |
local funcs = cellfuncs or cells or {}; --we hope that if nil is passed, global cells is defined. |
-- func is a table of cellfuncs |
489 |
local map = map or {}; --but we have no default variable for a map itself |
function render_key(rx, ry, key, cellfuncs) |
490 |
|
local cellfuncs = cellfuncs or get_cellfuncs(cellfuncs); |
491 |
for y,str in map do |
|
492 |
local width = floor(strlen(str)/CELL_KEY_WIDTH) |
-- call default parents |
493 |
|
if (DEFAULT_CELL_PARENT) then |
494 |
for x = 1,width do |
for _,val in DEFAULT_CELL_PARENT do |
495 |
local key = strsub(str, (x-1)*CELL_KEY_WIDTH+1, x*CELL_KEY_WIDTH) |
local dpfunc=0 |
496 |
local rx, ry = x+x0-1, y+y0-1 |
if (type(val)=="string") then |
497 |
|
dpfunc=get_cell_func(val, cellfuncs) |
498 |
-- call default parents |
else |
499 |
if (DEFAULT_CELL_PARENT) then |
dpfunc=val |
|
for _,val in DEFAULT_CELL_PARENT do |
|
|
local dpfunc=0 |
|
|
if (type(val)=="string") then |
|
|
dpfunc=get_cell_func(val, funcs) |
|
|
else |
|
|
dpfunc=val |
|
|
end |
|
|
|
|
|
if (type(dpfunc)=="function") then |
|
|
dpfunc(rx, ry) |
|
|
else |
|
|
warning("unknown default parent for this key:") |
|
|
warning(val) |
|
|
end |
|
|
end |
|
500 |
end |
end |
501 |
|
|
502 |
-- call common cell function |
if (type(dpfunc)=="function") then |
503 |
local func = get_cell_func(key, funcs) |
dpfunc(rx, ry) |
|
local ftype = type(func) |
|
|
if (ftype == "function") then |
|
|
func(rx, ry) |
|
|
elseif (ftype == "nil") then |
|
|
warning("function doesn't exist for map element '"..key.."'.") |
|
504 |
else |
else |
505 |
warning("cell element '"..key.."' is not a function, it's "..ftype..".") |
warning("render_key: unknown default parent type for the key: '"..val.."'") |
506 |
end |
end |
507 |
end |
end |
508 |
end |
end |
509 |
|
|
510 |
|
-- call common cell function |
511 |
|
local func = get_cell_func(key, cellfuncs) |
512 |
|
local ftype = type(func) |
513 |
|
if (ftype == "function") then |
514 |
|
func(rx, ry) |
515 |
|
elseif (ftype == "nil") then |
516 |
|
warning("render_key: function doesn't exist for map element '"..key.."'.") |
517 |
|
else |
518 |
|
warning("render_key: cell element '"..key.."' is not a function, it's "..ftype..".") |
519 |
|
end |
520 |
end |
end |
521 |
|
|
522 |
-- this prepares enigma world and draws given map |
-- this function returns a cell key by its x,y coordinates |
523 |
function create_world_by_map(level, cellfuncs) |
function get_cell_by_xy(mx, my, map) |
524 |
|
return strsub(map[my], (mx-1)*CELL_KEY_WIDTH+1, mx*CELL_KEY_WIDTH) |
525 |
|
end |
526 |
|
|
527 |
|
-- function renders a given cell of map |
528 |
|
-- rx0, ry0 are coordinates of left top square of map |
529 |
|
-- mx, my are coordinates of map square to be rendered |
530 |
|
function render_map_cell(rx0, ry0, mx, my, map, cellfuncs) |
531 |
|
if (not(map)) then |
532 |
|
warning("render_map_cell: no map given!") |
533 |
|
return |
534 |
|
end |
535 |
|
local cellfuncs = cellfuncs or get_cellfuncs(cellfuncs) |
536 |
|
|
537 |
|
local rx, ry = mx+rx0-1, my+ry0-1 |
538 |
|
local key = get_cell_by_xy(mx, my, map) |
539 |
|
render_key(rx, ry, key, cellfuncs) |
540 |
|
end |
541 |
|
|
542 |
|
-- this draws the map to the position [rx0,ry0] |
543 |
|
-- map is array of strings. Each string is one line of result map, each char is one map square. |
544 |
|
-- you may omit cellfuncs, default global 'cells' is used instead |
545 |
|
function draw_map_portion(rx0, ry0, mxy0, mxy1, map, cellfuncs) |
546 |
|
if (not(map)) then |
547 |
|
warning("draw_map: no map given!") |
548 |
|
return |
549 |
|
end |
550 |
|
local cellfuncs = cellfuncs or get_cellfuncs(cellfuncs); |
551 |
|
|
552 |
|
for my = mxy0[2],mxy1[2] do |
553 |
|
for mx = mxy0[1],mxy1[1] do |
554 |
|
render_map_cell(rx0, ry0, mx, my, map, cellfuncs); |
555 |
|
end -- for x |
556 |
|
end -- for y |
557 |
|
end |
558 |
|
|
559 |
|
-- this will draw whole map |
560 |
|
-- rx0, ry0 are coordinates of left top map corner |
561 |
|
function draw_map(rx0, ry0, map, cellfuncs) |
562 |
|
local mapw, maph = get_map_size(map) |
563 |
|
draw_map_portion(rx0, ry0, {1,1}, {mapw, maph}, map, cellfuncs) |
564 |
|
end |
565 |
|
|
566 |
|
-- this just prepares the world, but no map is created |
567 |
|
function prepare_world_by_map(map) |
568 |
|
local mapw, maph = get_map_size(map) |
569 |
local flavor = oxyd_default_flavor |
local flavor = oxyd_default_flavor |
|
local levelw, levelh = get_map_size(level) |
|
570 |
|
|
571 |
debug("creating world ["..levelw.."x"..levelh.."]") |
debug("creating world ["..mapw.."x"..maph.."]") |
572 |
create_world(levelw, levelh) |
create_world(mapw, maph) |
573 |
oxyd_default_flavor = flavor or oxyd_default_flavor or "b" |
oxyd_default_flavor = flavor or oxyd_default_flavor or "b" |
574 |
|
end |
575 |
|
|
576 |
draw_map(0, 0, level, cellfuncs) |
-- this prepares enigma world and draws given map |
577 |
|
-- you may omit cellfuncs, default global 'cells' is used instead |
578 |
|
function create_world_by_map(map, cellfuncs) |
579 |
|
prepare_world_by_map(map) |
580 |
|
draw_map(0, 0, map, cellfuncs) |
581 |
end |
end |
582 |
|
|
583 |
|
|
940 |
|
|
941 |
if (type(func)=="function") then |
if (type(func)=="function") then |
942 |
tab.tag = func(x, y) |
tab.tag = func(x, y) |
943 |
|
else |
944 |
|
tab.tag = tag |
945 |
end |
end |
946 |
|
|
947 |
group[tag] = group[tag] or {} |
group[tag] = group[tag] or {} |
967 |
-- at the end of levelfile: |
-- at the end of levelfile: |
968 |
-- spread_tag(pivots) |
-- spread_tag(pivots) |
969 |
-- render_slopes(slopes) |
-- render_slopes(slopes) |
970 |
|
-- BUGS: |
971 |
|
-- stack overflows, if: |
972 |
|
-- + the shape is not closed |
973 |
|
-- + area is too large (for example very long corridor...) |
974 |
function spread_tag_depth(x0, y0, tab, tag) |
function spread_tag_depth(x0, y0, tab, tag) |
975 |
if tab[getkey(x0, y0)] then return end |
if tab[getkey(x0, y0)] then return end |
976 |
|
|
990 |
end |
end |
991 |
end |
end |
992 |
|
|
|
|
|
|
|
|
993 |
-- add the rubber band between all objects in group 1 and all objects in group 2 |
-- add the rubber band between all objects in group 1 and all objects in group 2 |
994 |
-- that is, each object from gr1 will be connected with each object from gr2 |
-- that is, each object from gr1 will be connected with each object from gr2 |
995 |
function add_rubber_bands(gr1, gr2, strength, length) |
function add_rubber_bands(gr1, gr2, strength, length) |
1204 |
|
|
1205 |
|
|
1206 |
|
|
1207 |
|
|
1208 |
|
|
1209 |
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- |
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- |
1210 |
-- SLOPE GENERATOR - -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- |
-- SLOPE GENERATOR - -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- |
1211 |
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- |
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- |
1293 |
longval = val; |
longval = val; |
1294 |
warns = {} |
warns = {} |
1295 |
elseif (count==longest) then |
elseif (count==longest) then |
1296 |
tinsert(warns, "ambiguous match: '"..longkey.."' vs. '"..key.."' for re '"..re.."'") |
tinsert(warns, "map_slope: ambiguous match: '"..longkey.."' vs. '"..key.."' for re '"..re.."'") |
1297 |
end |
end |
1298 |
end |
end |
1299 |
end |
end |
1303 |
end |
end |
1304 |
|
|
1305 |
if (longest==0) then |
if (longest==0) then |
1306 |
warning("render_slopes: no match for mask '"..re.."' at ["..x..","..y.."].") |
warning("map_slope: no match for mask '"..re.."' at ["..x..","..y.."].") |
1307 |
end |
end |
1308 |
|
|
1309 |
return longval; |
return longval; |
1324 |
tinsert(node, (tab[getkey(x+1,y+1)] ~= nil) or 0) |
tinsert(node, (tab[getkey(x+1,y+1)] ~= nil) or 0) |
1325 |
|
|
1326 |
if (val.tag~=2) then |
if (val.tag~=2) then |
1327 |
if (not (invert)) then |
if ((invert~=nil)~=(val.tag==-1)) then -- this is ((invert) xnor (val.tag==-1)) |
|
gradient(x, y, map_slope(x, y, node)) |
|
|
else |
|
1328 |
gradient(x, y, __grad_inverse[map_slope(x, y, node)]) |
gradient(x, y, __grad_inverse[map_slope(x, y, node)]) |
1329 |
|
else |
1330 |
|
gradient(x, y, map_slope(x, y, node)) |
1331 |
end |
end |
1332 |
end |
end |
1333 |
end |
end |
1402 |
map_cell_meaning("D", cell{stone="st-death"}) |
map_cell_meaning("D", cell{stone="st-death"}) |
1403 |
map_cell_meaning("=", cell{stone="st-glass"}) |
map_cell_meaning("=", cell{stone="st-glass"}) |
1404 |
map_cell_meaning("X", cell{stone="st-grate1"}) |
map_cell_meaning("X", cell{stone="st-grate1"}) |
|
map_cell_meaning("~", cell{stone="st-timer"}) |
|
1405 |
|
|
1406 |
-- actors |
-- actors |
1407 |
map_cell_meaning("o", cell{actor={"ac-whiteball-small", {player=0, mouseforce=1}}}) |
map_cell_meaning("o", cell{actor={"ac-whiteball-small", {player=0, mouseforce=1}}}) |
1417 |
map_cell_meaning("2", cell{item="it-yinyang", actor={"ac-whiteball", {player=1, mouseforce=1}}}) |
map_cell_meaning("2", cell{item="it-yinyang", actor={"ac-whiteball", {player=1, mouseforce=1}}}) |
1418 |
end |
end |
1419 |
|
|
1420 |
|
-- level mood |
1421 |
function grass_mode() |
function grass_mode() |
1422 |
map_cell_meaning(" ", cell{floor="fl-leaves"}) |
map_cell_meaning(" ", cell{floor="fl-leaves"}) |
1423 |
map_cell_meaning("#", cell{stone="st-rock1"}) |
map_cell_meaning("#", cell{stone="st-rock1"}) |
1424 |
end |
end |
1425 |
|
|
1426 |
|
function metal_mode() |
1427 |
|
map_cell_meaning(" ", cell{floor="fl-metal"}) |
1428 |
|
map_cell_meaning("#", cell{stone="st-rock2"}) |
1429 |
|
end |
1430 |
|
|
1431 |
-- maybe in a future, there will be a possibility to integrate Nat's mazes into maps |
-- maybe in a future, there will be a possibility to integrate Nat's mazes into maps |
1432 |
function maze_mode() |
function maze_mode() |
1433 |
Require("levels/natmaze.lua") |
Require("levels/natmaze.lua") |