home *** CD-ROM | disk | FTP | other *** search
/ Chip 2011 November / CHIP_2011_11.iso / Programy / Inne / Gry / Enigma / Enigma-1.01-w7.exe / data / levels / lib / natmaze.xml < prev    next >
Extensible Markup Language  |  2009-12-13  |  8KB  |  277 lines

  1. <?xml version="1.0" encoding="UTF-8" standalone="no" ?>
  2. <el:level xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://enigma-game.org/schema/level/1 level.xsd" xmlns:el="http://enigma-game.org/schema/level/1">
  3.   <el:protected>
  4.     <el:info el:type="library">
  5.       <el:identity el:title="" el:id="lib/natmaze"/>
  6.       <el:version el:score="1" el:release="1" el:revision="0" el:status="released"/>
  7.       <el:author  el:name="Nat Pryce" el:email="" el:homepage=""/>
  8.       <el:copyright>Copyright ┬⌐ 2002 Nat Pryce</el:copyright>
  9.       <el:license el:type="GPL v2.0 or above" el:open="true"/>
  10.       <el:compatibility el:enigma="0.92">
  11.       </el:compatibility>
  12.       <el:modes el:easy="false" el:single="false" el:network="false"/>
  13.       <el:comments>
  14.         <el:code>Lua 5.1 and XML converted by Leveladministrators</el:code>
  15.       </el:comments>
  16.       <el:score el:easy="-" el:difficult="-"/>
  17.     </el:info>
  18.     <el:luamain><![CDATA[
  19. -- Maze module: maze generator and renderer for Enigma
  20. -- 
  21.  
  22. function remove_random_element( table )
  23.     return tremove( table, random( 1, getn(table) ) )
  24. end
  25.  
  26. function random_element( table )
  27.     return table[random( 1, getn(table) )]
  28. end
  29.  
  30.  
  31. -------------------------------------------------------------------------------
  32. -- Maze objects
  33.  
  34.  
  35. -- These constants conflict with those defined in init.lua; commented
  36. -- them out for now (dh).
  37.  
  38. -- NORTH = 1
  39. -- SOUTH = 2
  40. -- WEST = 3
  41. -- EAST = 4
  42.  
  43. function maze_coords_to_cell( maze, x, y )
  44.     return y*maze.width + x
  45. end
  46.  
  47. function maze_can_go_south( maze, x, y )
  48.     return y < (maze.height-1) and maze.linky[maze_coords_to_cell(maze,x,y)]
  49. end
  50.  
  51. function maze_can_go_north( maze, x, y )
  52.     return y > 0 and maze_can_go_south(maze,x,y-1)
  53. end
  54.  
  55. function maze_can_go_east( maze, x, y )
  56.     return x < (maze.width-1) and maze.linkx[maze_coords_to_cell(maze,x,y)]
  57. end
  58.  
  59. function maze_can_go_west( maze, x, y )
  60.     return x > 0 and maze_can_go_east( maze, x-1, y )
  61. end
  62.  
  63. function maze_link_south( maze, x, y )
  64.     assert( maze_contains_cell( maze, x, y+1 ) )
  65.     maze.linky[maze_coords_to_cell(maze,x,y)] = 1
  66. end
  67.  
  68. function maze_link_north( maze, x, y )
  69.     assert( maze_contains_cell( maze, x, y-1 ) )
  70.     maze_link_south( maze, x, y-1 )
  71. end
  72.  
  73. function maze_link_east( maze, x, y )
  74.     assert( maze_contains_cell( maze, x+1, y ) )
  75.     maze.linkx[maze_coords_to_cell(maze,x,y)] = 1
  76. end
  77.  
  78. function maze_link_west( maze, x, y )
  79.     assert( maze_contains_cell( maze, x-1, y ) )
  80.     maze_link_east( maze, x-1, y )
  81. end
  82.  
  83. function maze_contains_cell( maze, x, y )
  84.     return x >= 0 and x < maze.width and y >= 0 and y < maze.height
  85. end
  86.  
  87. function new_maze( width, height )
  88.     local maze = {}
  89.     
  90.     maze.width = width
  91.     maze.height = height
  92.     maze.linkx = {}
  93.     maze.linky = {}
  94.     
  95.     maze.contains_cell = maze_contains_cell
  96.     maze.coords_to_cell = maze_coords_to_cell
  97.     maze.can_go_north = maze_can_go_north
  98.     maze.can_go_south = maze_can_go_south
  99.     maze.can_go_west = maze_can_go_west
  100.     maze.can_go_east = maze_can_go_east
  101.     maze.link_north = maze_link_north
  102.     maze.link_south = maze_link_south
  103.     maze.link_west = maze_link_west
  104.     maze.link_east = maze_link_east
  105.     
  106.     return maze
  107. end
  108.  
  109.  
  110. -------------------------------------------------------------------------------
  111. -- Maze generator based on Kruskal's minimum spanning tree algorithm
  112.  
  113. function new_kruskal_maze( width, height )
  114.     local maze = new_maze(width,height)
  115.     local walls = maze_walls( maze )
  116.     local zones = {}
  117.     local zone_count = width*height
  118.     
  119.     while zone_count > 1 do
  120.         wall = remove_random_element( walls )
  121.         local x1 = wall.cellx
  122.         local y1 = wall.celly
  123.         local x2, y2
  124.         
  125.         if wall.side == SOUTH then
  126.             x2 = wall.cellx
  127.             y2 = wall.celly+1
  128.         else -- wall.side == EAST
  129.             x2 = wall.cellx+1
  130.             y2 = wall.celly
  131.         end
  132.         
  133.         cell1 = maze:coords_to_cell(x1,y1)
  134.         cell2 = maze:coords_to_cell(x2,y2)
  135.         zone1 = find_zone( zones, cell1 )
  136.         zone2 = find_zone( zones, cell2 )
  137.         
  138.         if zone1 ~= zone2 then
  139.             if wall.side == SOUTH then
  140.                 maze:link_south( x1, y1 )
  141.             else -- wall.side == EAST
  142.                 maze:link_east( x1, y1 )
  143.             end
  144.             
  145.             union_zones( zones, cell1, cell2 )
  146.             zone_count = zone_count - 1
  147.         end
  148.     end
  149.     
  150.     return maze
  151. end
  152.  
  153.  
  154. function maze_walls( maze )
  155.     walls = {}
  156.     for y = 0, maze.height-1 do
  157.         for x = 0, maze.width-1 do
  158.             if y < maze.height-1 then
  159.                 tinsert( walls, {cellx=x,celly=y,side=SOUTH} )
  160.             end
  161.             if x < maze.width-1 then
  162.                 tinsert( walls, {cellx=x,celly=y,side=EAST} )
  163.             end
  164.         end
  165.     end
  166.     
  167.     return walls
  168. end
  169.  
  170.  
  171.  
  172. -- Union-find algorithm for merging "zones" of the maze.  Each zone is
  173. -- a tree of cells identified by the cell at the root of the tree.  A root
  174. -- cell is represented by a nil reference in the zones table indexed by the
  175. -- cell number.
  176.  
  177. function find_zone( zones, cell )
  178.     if zones[cell] == nil then
  179.         return cell
  180.     else
  181.         zones[cell] = find_zone( zones, zones[cell] )
  182.         return zones[cell]
  183.     end
  184. end
  185.  
  186. function union_zones( zones, cell1, cell2 )
  187.     zones[find_zone(zones,cell2)] = find_zone(zones,cell1)
  188. end
  189.  
  190.  
  191.  
  192. -------------------------------------------------------------------------------
  193. -- Maze renderers
  194.  
  195.  
  196. function render_maze( maze, cell_renderer )
  197.     for cellx = 0, maze.width-1 do
  198.         for celly = 0, maze.height-1 do
  199.             cell_renderer( maze, cellx, celly )
  200.         end
  201.     end
  202. end
  203.  
  204. function tight_maze( maze, maze_floor, wall_floor, wall_stone )
  205.     local originx = 1
  206.     local originy = 1
  207.     
  208.     function cell_to_level( cellx, celly )
  209.         return originx + cellx * 2, originy + celly * 2
  210.     end
  211.     
  212.     function draw_maze_border( maze, stone )
  213.         width = maze.width*2
  214.         height = maze.height*2
  215.         
  216.         draw_stones( stone, {0,0}, {1,0}, width )
  217.         draw_stones( stone, {0,height-1},{1,0}, width )
  218.         draw_stones( stone, {0,0}, {0,1}, height )
  219.         draw_stones( stone, {width-1,0},{0,1}, height )
  220.     end
  221.     
  222.     function render_cell( maze, cellx, celly )
  223.         local x, y = cell_to_level( cellx, celly )
  224.         
  225.         if wall_stone then 
  226.             set_stone( wall_stone, x+1, y+1 )
  227.         end
  228.         if maze_floor then
  229.             set_floor( maze_floor, x, y )
  230.         end
  231.         if maze:can_go_south(cellx,celly) then
  232.             if maze_floor then
  233.                 set_floor( maze_floor, x, y+1 )
  234.             end
  235.         else
  236.             if wall_stone then
  237.                 set_stone( wall_stone, x, y+1 )
  238.             end
  239.         end
  240.         if maze:can_go_east(cellx,celly) then
  241.             if maze_floor then
  242.                 set_floor( maze_floor, x+1, y )
  243.             end
  244.         else
  245.             if wall_stone then
  246.                 set_stone( wall_stone, x+1, y )
  247.             end
  248.         end
  249.     end
  250.     
  251.     create_world( maze.width*2 + 1, maze.height*2 + 1 )
  252.     if wall_stone then
  253.         draw_border( wall_stone )
  254.     end
  255.     if wall_floor then
  256.         fill_floor( wall_floor)
  257.     else
  258.         fill_floor( maze_floor)
  259.     end
  260.     render_maze( maze, render_cell )
  261.     
  262.     oxyd(1,0)
  263.     oxyd(2*maze.width-1,2*maze.height)
  264.     oxyd(1,2*maze.height)
  265.     oxyd(2*maze.width-1,0)
  266.  
  267.     local actorx, actory = cell_to_level( random(maze.width)-1, 
  268.                                           random(maze.height)-1 )
  269.     set_actor( "ac-blackball", actorx + 0.5, actory + 0.5,
  270.                { player=0 } )
  271. end
  272.     ]]></el:luamain>
  273.     <el:i18n>
  274.     </el:i18n>
  275.   </el:protected>
  276. </el:level>
  277.