Advertisement
Guest User

Untitled

a guest
Jun 1st, 2013
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 6.34 KB | None | 0 0
  1. unit WorldGen;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, allegro, PerlinNoiseUnit, Tilesets, Sprites,boolUtils;
  9.  
  10. type
  11. TChunk = array of array of byte;
  12.  
  13. { TWorldGenerator }
  14.  
  15. TWorldGenerator = class
  16.   public
  17.     constructor Create(seed: Integer);
  18.     destructor Destroy();
  19.     function GenerateChunk(ChunkX,ChunkY, chunksize:Integer):TChunk;
  20.     function Draw(bitmap:AL_BITMAPptr; x,y:Integer; Tileset:TTileset; Chunk:TChunk):TSprite;
  21.     function GetChunkWithBlocks(Chunk: TChunk):TChunk; //blocks are destroyable by the player, so we need a way to get them
  22.                                                        //so we can destroy them in world (by displaying as TSprite).
  23.  
  24.   private
  25.     PerlinGenerator: TPerlinNoise;
  26.     cx,cy:Integer;
  27.     function _genStepTerrain(Chunk:TChunk):TChunk;
  28.     function _genStepGround(Chunk:TChunk):TChunk;
  29.     function _genStepCarve(Chunk:TChunk):TChunk;
  30.     function _genStepTraps(Chunk:TChunk):TChunk;
  31.     function _genStepBlocks(Chunk:TChunk):TChunk;
  32.     function GetTrapSprite(Chunk:TChunk; Tileset:TTileset):TSprite;  //util functions so we can get collidable terrain
  33.     function GetChunkSprite(Chunk:TChunk; Tileset:TTileset):TSprite; //and spikes as sprites for easy collision checking.
  34.  
  35. end;
  36.  
  37.  
  38. implementation
  39. uses ChunkUtils;
  40. const
  41.   Octaves=4;
  42.   Persistance=6.5;
  43.   Frequency=2;
  44.   TrapChance=14; //there is 14 percent of chance that spike will spawn on ground or block.
  45. { TWorldGenerator }
  46.  
  47. constructor TWorldGenerator.Create(seed: Integer);
  48. begin
  49.   inherited Create;
  50.   PerlinGenerator:= TPerlinNoise.Create(seed);
  51.  
  52. end;
  53.  
  54. destructor TWorldGenerator.Destroy;
  55. begin
  56.   inherited;
  57.   PerlinGenerator.Destroy;
  58. end;
  59.  
  60. function TWorldGenerator.GenerateChunk(ChunkX, ChunkY, chunksize: Integer
  61.   ): TChunk;
  62. var x:Integer;
  63.  
  64.   Chunk:TChunk;
  65. begin
  66.     cx:=Chunkx;
  67.     cy:=Chunky;
  68.     //setting dimensions of chunk
  69.     SetLength(Chunk,chunksize);
  70.     for x:=0 to chunksize-1 do SetLength(Chunk[x],chunksize);
  71.     Chunk:=_genStepTerrain(Chunk);
  72.     Chunk:=_genStepCarve(Chunk);
  73.     Chunk:=_genStepGround(Chunk);
  74.     Chunk:=_genStepBlocks(Chunk);
  75.     Chunk:=_genStepTraps(Chunk);
  76.     result:=Chunk;
  77. end;
  78.  
  79.  
  80. function TWorldGenerator.Draw(bitmap: AL_BITMAPptr; x, y: Integer;
  81.   Tileset: TTileset; Chunk:TChunk):TSprite;
  82. begin
  83.  
  84. end;
  85.  
  86. function TWorldGenerator._genStepTerrain(Chunk: TChunk): TChunk;
  87. var  i,perlinresult:Byte;
  88.      x,y,temp,chunksize:Integer;
  89.      neighbors:array[0..7]of byte;
  90. begin
  91.   //getting length of chunk
  92.   chunksize:=Length(Chunk);
  93.   //generation
  94.     for x:=0 to chunksize-1 do
  95.     begin
  96.       for y:=0 to chunksize-1 do
  97.       begin
  98.         //getting perlin for specified point
  99.         perlinresult:=Trunc(PerlinGenerator.PerlinNoise2d((cx*chunksize)+x,(cy*chunksize)+y,Persistance,Frequency,Octaves)*255);
  100.         //smoothing it so it won't look awful
  101.         neighbors[0]:=Trunc(PerlinGenerator.PerlinNoise2d(((cx*chunksize)+x)-1,(cy*chunksize)+y,Persistance,Frequency,Octaves)*255);
  102.         neighbors[1]:=Trunc(PerlinGenerator.PerlinNoise2d(((cx*chunksize)+x)+1,(cy*chunksize)+y,Persistance,Frequency,Octaves)*255);
  103.         neighbors[2]:=Trunc(PerlinGenerator.PerlinNoise2d((cx*chunksize)+x,((cy*chunksize)+y)-1,Persistance,Frequency,Octaves)*255);
  104.         neighbors[3]:=Trunc(PerlinGenerator.PerlinNoise2d((cx*chunksize)+x,((cy*chunksize)+y)+1,Persistance,Frequency,Octaves)*255);
  105.         neighbors[4]:=Trunc(PerlinGenerator.PerlinNoise2d(((cx*chunksize)+x)-1,((cy*chunksize)+y)-1,Persistance,Frequency,Octaves)*255);
  106.         neighbors[5]:=Trunc(PerlinGenerator.PerlinNoise2d(((cx*chunksize)+x)+1,((cy*chunksize)+y)-1,Persistance,Frequency,Octaves)*255);
  107.         neighbors[6]:=Trunc(PerlinGenerator.PerlinNoise2d(((cx*chunksize)+x)+1,((cy*chunksize)+y)+1,Persistance,Frequency,Octaves)*255);
  108.         neighbors[7]:=Trunc(PerlinGenerator.PerlinNoise2d(((cx*chunksize)+x)-1,((cy*chunksize)+y)+1,Persistance,Frequency,Octaves)*255);
  109.         temp:= perlinresult div 2;
  110.         for i:=0 to 7 do begin
  111.           temp:= temp + (neighbors[i] div 12);
  112.         end;
  113.         if temp>255 then temp:=255;
  114.         perlinresult:=temp;
  115.         //standard ground
  116.         if Between(perlinresult,90,256) then chunk[x][y]:=GROUNDID
  117.         else chunk[x][y]:= AIRID;
  118.       end;
  119.     end;
  120.     result:=Chunk;
  121. end;
  122.  
  123. function TWorldGenerator._genStepGround(Chunk: TChunk): TChunk;
  124. var chunksize,x,y:Integer;
  125.  
  126. begin
  127.   chunksize:=Length(Chunk);
  128.   for x:=0 to chunksize-1 do begin
  129.     for y:=1 to chunksize-2 do begin
  130.       //we start at Y=1 and end one index before end of chunk as we need to test block before that for being
  131.       //air and if there is something under.
  132.       if ((Chunk[x][y]=AIRID) and (Chunk[x][y-1]=AIRID) and (Chunk[x][y+1]=GROUNDID))
  133.          then Chunk[x][y]:=INVINCIBLEBLOCKID;
  134.     end;
  135.   end;
  136.   result:=Chunk;
  137. end;
  138.  
  139. function TWorldGenerator._genStepCarve(Chunk: TChunk): TChunk;
  140. var x,chunksize:Integer;
  141. begin
  142.   chunksize:=Length(Chunk);
  143.   for x:=0 to Random(chunksize+(chunksize div 5)) do begin
  144.           //al_circlefill(perlin,random(100),random(25)+25,Random(12),al_makecol(255,0,255)); //putting some random magenta circlesin middle co carve way a bit.
  145.           ChunkCircle(Chunk,random(chunksize),random(chunksize div 2)+chunksize div 5,random(chunksize div 5),-1,true);
  146.       end;
  147.   result:=chunk;
  148. end;
  149.  
  150. function TWorldGenerator._genStepTraps(Chunk: TChunk): TChunk;
  151. var chunksize,x,y:Integer;
  152.  
  153. begin
  154.   chunksize:=Length(Chunk);
  155.   for x:=0 to chunksize-1 do begin
  156.     for y:=1 to chunksize-2 do begin
  157.       //we start at Y=1 and end one index before end of chunk as we need to test block before that for being
  158.       //air and if there is something under.
  159.       if ((Chunk[x][y]=AIRID) and (Chunk[x][y-1]=AIRID) and ((Chunk[x][y+1]=GROUNDID) or
  160.          (Chunk[x][y+1]=INVINCIBLEBLOCKID)) and (Random(101)<TrapChance)) then Chunk[x][y]:=SPIKEID;
  161.          //randomizing above is so whole ground won't be in traps.
  162.     end;
  163.   end;
  164.   result:=Chunk;
  165.  
  166. end;
  167.  
  168. function TWorldGenerator._genStepBlocks(Chunk: TChunk): TChunk;
  169. begin
  170.   result:=Chunk;
  171. end;
  172.  
  173. function TWorldGenerator.GetTrapSprite(Chunk: TChunk; Tileset: TTileset
  174.   ): TSprite;
  175. begin
  176.  
  177. end;
  178.  
  179. function TWorldGenerator.GetChunkSprite(Chunk: TChunk; Tileset: TTileset
  180.   ): TSprite;
  181. begin
  182.  
  183. end;
  184.  
  185. function TWorldGenerator.GetChunkWithBlocks(Chunk: TChunk): TChunk;
  186. begin
  187.  
  188. end;
  189.  
  190. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement