hedgewars/uLandGenMaze.pas
branchwebgl
changeset 8026 4a4f21070479
parent 6580 6155187bf599
child 9127 e350500c4edb
equal deleted inserted replaced
8023:7de85783b823 8026:4a4f21070479
    12 const DIR_N: direction = (x: 0; y: -1);
    12 const DIR_N: direction = (x: 0; y: -1);
    13     DIR_E: direction = (x: 1; y: 0);
    13     DIR_E: direction = (x: 1; y: 0);
    14     DIR_S: direction = (x: 0; y: 1);
    14     DIR_S: direction = (x: 0; y: 1);
    15     DIR_W: direction = (x: -1; y: 0);
    15     DIR_W: direction = (x: -1; y: 0);
    16 
    16 
       
    17 {xymeng : make all dynamic arrays static }
       
    18 const max_num_cells_x = 4096 div 128;
       
    19    max_num_cells_y    = 4096 div 128;
       
    20    max_num_steps      = 3;
    17 
    21 
    18 operator = (const a, b: direction) c: Boolean;
    22 operator = (const a, b: direction) c: Boolean;
    19 begin
    23 begin
    20     c := (a.x = b.x) and (a.y = b.y);
    24     c := (a.x = b.x) and (a.y = b.y);
    21 end;
    25 end;
    23 const small_cell_size = 128;
    27 const small_cell_size = 128;
    24     medium_cell_size = 192;
    28     medium_cell_size = 192;
    25     large_cell_size = 256;
    29     large_cell_size = 256;
    26     braidness = 10;
    30     braidness = 10;
    27 
    31 
    28 var x, y: LongInt;
    32 type
    29     cellsize: LongInt; //selected by the user in the gui
    33    cell_t = record x,y	       : LongInt
    30     seen_cells_x, seen_cells_y: LongInt; //number of cells that can be visited by the generator, that is every second cell in x and y direction. the cells between there are walls that will be removed when we move from one cell to another
    34 	    end;
    31     num_edges_x, num_edges_y: LongInt; //number of resulting edges that need to be vertexificated
    35    
    32     num_cells_x, num_cells_y: LongInt; //actual number of cells, depending on cell size
    36 var x, y		       : LongInt;
    33     seen_list: array of array of LongInt;
    37     cellsize		       : LongInt; //selected by the user in the gui
    34     xwalls: array of array of Boolean;
    38     seen_cells_x, seen_cells_y : LongInt; //number of cells that can be visited by the generator, that is every second cell in x and y direction. the cells between there are walls that will be removed when we move from one cell to another
    35     ywalls: array of array of Boolean;
    39     num_edges_x, num_edges_y   : LongInt; //number of resulting edges that need to be vertexificated
    36     x_edge_list: array of array of Boolean;
    40     num_cells_x, num_cells_y   : LongInt; //actual number of cells, depending on cell size
    37     y_edge_list: array of array of Boolean;
    41 
    38     maze: array of array of Boolean;
    42 
    39     pa: TPixAr;
    43     seen_list		       : array of array of LongInt;
    40     num_vertices: LongInt;
    44     xwalls		       : array of array of Boolean;
    41     off_y: LongInt;
    45     ywalls		       : array of array of Boolean;
    42     num_steps: LongInt;
    46     x_edge_list		       : array of array of Boolean;
    43     current_step: LongInt;
    47     y_edge_list		       : array of array of Boolean;
    44     step_done: array of Boolean;
    48     maze		       : array of array of Boolean;
    45     done: Boolean;
    49 
    46     last_cell: array of record x, y: LongInt; end;
    50     pa			       : TPixAr;
    47     came_from: array of array of record x, y: LongInt; end;
    51     num_vertices	       : LongInt;
       
    52     off_y		       : LongInt;
       
    53     num_steps		       : LongInt;
       
    54     current_step	       : LongInt;
       
    55    
       
    56     step_done		       : array of Boolean;
       
    57    
       
    58     done		       : Boolean;
       
    59 
       
    60 {   last_cell		       : array 0..3 of record x, y :LongInt ; end;
       
    61     came_from		       : array of array of record x, y: LongInt; end;
       
    62     came_from_pos	       : array of LongInt;
       
    63 }
       
    64     last_cell : array of cell_t;
       
    65     came_from : array of array of cell_t;
    48     came_from_pos: array of LongInt;
    66     came_from_pos: array of LongInt;
    49     maze_inverted: Boolean;
    67    
       
    68     maze_inverted					   : Boolean;
    50 
    69 
    51 function when_seen(x: LongInt; y: LongInt): LongInt;
    70 function when_seen(x: LongInt; y: LongInt): LongInt;
    52 begin
    71 begin
    53 if (x < 0) or (x >= seen_cells_x) or (y < 0) or (y >= seen_cells_y) then
    72 if (x < 0) or (x >= seen_cells_x) or (y < 0) or (y >= seen_cells_y) then
    54     when_seen := current_step
    73     when_seen := current_step
   100 begin
   119 begin
   101     if when_seen(x + dir.x, y + dir.y) = current_step then //we are seeing ourselves, try another direction
   120     if when_seen(x + dir.x, y + dir.y) = current_step then //we are seeing ourselves, try another direction
   102     begin
   121     begin
   103         //we have already seen the target cell, decide if we should remove the wall anyway
   122         //we have already seen the target cell, decide if we should remove the wall anyway
   104         //(or put a wall there if maze_inverted, but we are not doing that right now)
   123         //(or put a wall there if maze_inverted, but we are not doing that right now)
   105         if not maze_inverted and (GetRandom(braidness) = 0) then
   124         if (not maze_inverted) and (GetRandom(braidness) = 0) then
   106         //or just warn that inverted+braid+indestructible terrain != good idea
   125         //or just warn that inverted+braid+indestructible terrain != good idea
   107         begin
   126         begin
   108             case dir.x of
   127             case dir.x of
   109             
   128             
   110                 -1:
   129                 -1:
   176     last_cell[current_step].x := came_from[current_step, came_from_pos[current_step]].x;
   195     last_cell[current_step].x := came_from[current_step, came_from_pos[current_step]].x;
   177     last_cell[current_step].y := came_from[current_step, came_from_pos[current_step]].y;
   196     last_cell[current_step].y := came_from[current_step, came_from_pos[current_step]].y;
   178     came_from_pos[current_step] := came_from_pos[current_step] - 1;
   197     came_from_pos[current_step] := came_from_pos[current_step] - 1;
   179     
   198     
   180     if came_from_pos[current_step] >= 0 then
   199     if came_from_pos[current_step] >= 0 then
   181         see_cell
   200         see_cell()
   182         
   201         
   183     else
   202     else
   184         step_done[current_step] := true;
   203         step_done[current_step] := true;
   185     end;
   204     end;
   186 end;
   205 end;
   334     
   353     
   335 SetLength(step_done, num_steps);
   354 SetLength(step_done, num_steps);
   336 SetLength(last_cell, num_steps);
   355 SetLength(last_cell, num_steps);
   337 SetLength(came_from_pos, num_steps);
   356 SetLength(came_from_pos, num_steps);
   338 SetLength(came_from, num_steps, num_cells_x*num_cells_y);
   357 SetLength(came_from, num_steps, num_cells_x*num_cells_y);
       
   358 
   339 done := false;
   359 done := false;
   340 
   360 
   341 for current_step := 0 to num_steps - 1 do
   361 for current_step := 0 to num_steps - 1 do
       
   362 begin
   342     step_done[current_step] := false;
   363     step_done[current_step] := false;
   343     came_from_pos[current_step] := 0;
   364     came_from_pos[current_step] := 0;
   344     
   365 end;
       
   366 
   345 current_step := 0;
   367 current_step := 0;
       
   368 
   346 
   369 
   347 SetLength(seen_list, seen_cells_x, seen_cells_y);
   370 SetLength(seen_list, seen_cells_x, seen_cells_y);
   348 SetLength(xwalls, seen_cells_x, seen_cells_y - 1);
   371 SetLength(xwalls, seen_cells_x, seen_cells_y - 1);
   349 SetLength(ywalls, seen_cells_x - 1, seen_cells_y);
   372 SetLength(ywalls, seen_cells_x - 1, seen_cells_y);
   350 SetLength(x_edge_list, num_edges_x, num_cells_y);
   373 SetLength(x_edge_list, num_edges_x, num_cells_y);
   351 SetLength(y_edge_list, num_cells_x, num_edges_y);
   374 SetLength(y_edge_list, num_cells_x, num_edges_y);
   352 SetLength(maze, num_cells_x, num_cells_y);
   375 SetLength(maze, num_cells_x, num_cells_y);
   353 
   376 
       
   377    
   354 num_vertices := 0;
   378 num_vertices := 0;
   355 
   379 
   356 playHeight := num_cells_y * cellsize;
   380 playHeight := num_cells_y * cellsize;
   357 playWidth := num_cells_x * cellsize;
   381 playWidth := num_cells_x * cellsize;
   358 off_y := LAND_HEIGHT - playHeight;
   382 off_y := LAND_HEIGHT - playHeight;