Move template-based generator into its own file
authorunc0rr
Fri, 14 Mar 2014 19:59:34 +0400
changeset 10198 e9cbe111c0df
parent 10197 c57798251b55
child 10199 fdb689b57b1b
Move template-based generator into its own file
hedgewars/CMakeLists.txt
hedgewars/uLand.pas
hedgewars/uLandGenTemplateBased.pas
hedgewars/uLandUtils.pas
--- a/hedgewars/CMakeLists.txt	Fri Mar 14 16:00:36 2014 +0400
+++ b/hedgewars/CMakeLists.txt	Fri Mar 14 19:59:34 2014 +0400
@@ -70,6 +70,8 @@
     uLandOutline.pas
     uLandGenMaze.pas
     uLandGenPerlin.pas
+    uLandGenTemplateBased.pas
+    uLandUtils.pas
 
     #this is where dependency tracking becomes hard
     uStore.pas
--- a/hedgewars/uLand.pas	Fri Mar 14 16:00:36 2014 +0400
+++ b/hedgewars/uLand.pas	Fri Mar 14 19:59:34 2014 +0400
@@ -32,34 +32,11 @@
 implementation
 uses uConsole, uStore, uRandom, uLandObjects, uIO, uLandTexture, SysUtils,
      uVariables, uUtils, uCommands, adler32, uDebug, uLandPainted, uTextures,
-     uLandGenMaze, uLandOutline, uPhysFSLayer, uScript, uLandGenPerlin;
+     uLandGenMaze, uLandOutline, uPhysFSLayer, uScript, uLandGenPerlin,
+     uLandGenTemplateBased, uLandUtils;
 
 var digest: shortstring;
 
-procedure ResizeLand(width, height: LongWord);
-var potW, potH: LongInt;
-begin
-potW:= toPowerOf2(width);
-potH:= toPowerOf2(height);
-if (potW <> LAND_WIDTH) or (potH <> LAND_HEIGHT) then
-    begin
-    LAND_WIDTH:= potW;
-    LAND_HEIGHT:= potH;
-    LAND_WIDTH_MASK:= not(LAND_WIDTH-1);
-    LAND_HEIGHT_MASK:= not(LAND_HEIGHT-1);
-    cWaterLine:= LAND_HEIGHT;
-    if (cReducedQuality and rqBlurryLand) = 0 then
-        SetLength(LandPixels, LAND_HEIGHT, LAND_WIDTH)
-    else
-        SetLength(LandPixels, LAND_HEIGHT div 2, LAND_WIDTH div 2);
-
-    SetLength(Land, LAND_HEIGHT, LAND_WIDTH);
-    SetLength(LandDirty, (LAND_HEIGHT div 32), (LAND_WIDTH div 32));
-    // 0.5 is already approaching on unplayable
-    if (width div 4096 >= 2) or (height div 2048 >= 2) then cMaxZoomLevel:= 0.5;
-    cMinMaxZoomLevelDelta:= cMaxZoomLevel - cMinZoomLevel
-    end;
-end;
 
 procedure PrettifyLandAlpha();
 begin
@@ -221,139 +198,6 @@
     if (LandBackSurface <> nil) and GrayScale then Surface2GrayScale(LandBackSurface);
 end;
 
-procedure SetPoints(var Template: TEdgeTemplate; var pa: TPixAr; fps: PPointArray);
-var i: LongInt;
-begin
-with Template do
-    begin
-    pa.Count:= BasePointsCount;
-    for i:= 0 to pred(pa.Count) do
-        begin
-        pa.ar[i].x:= BasePoints^[i].x + LongInt(GetRandom(BasePoints^[i].w));
-        if pa.ar[i].x <> NTPX then
-           pa.ar[i].x:= pa.ar[i].x + ((LAND_WIDTH - Template.TemplateWidth) div 2);
-        pa.ar[i].y:= BasePoints^[i].y + LongInt(GetRandom(BasePoints^[i].h)) + LAND_HEIGHT - LongInt(Template.TemplateHeight)
-        end;
-
-    if canMirror then
-        if getrandom(2) = 0 then
-            begin
-            for i:= 0 to pred(BasePointsCount) do
-               if pa.ar[i].x <> NTPX then
-                   pa.ar[i].x:= LAND_WIDTH - 1 - pa.ar[i].x;
-            for i:= 0 to pred(FillPointsCount) do
-                fps^[i].x:= LAND_WIDTH - 1 - fps^[i].x;
-            end;
-
-(*  Experiment in making this option more useful
-     if ((not isNegative) and (cTemplateFilter = 4)) or
-        (canFlip and (getrandom(2) = 0)) then
-           begin
-           for i:= 0 to pred(BasePointsCount) do
-               begin
-               pa.ar[i].y:= LAND_HEIGHT - 1 - pa.ar[i].y + (LAND_HEIGHT - TemplateHeight) * 2;
-               if pa.ar[i].y > LAND_HEIGHT - 1 then
-                   pa.ar[i].y:= LAND_HEIGHT - 1;
-               end;
-           for i:= 0 to pred(FillPointsCount) do
-               begin
-               FillPoints^[i].y:= LAND_HEIGHT - 1 - FillPoints^[i].y + (LAND_HEIGHT - TemplateHeight) * 2;
-               if FillPoints^[i].y > LAND_HEIGHT - 1 then
-                   FillPoints^[i].y:= LAND_HEIGHT - 1;
-               end;
-           end;
-     end
-*)
-// template recycling.  Pull these off the floor a bit
-    if (not isNegative) and (cTemplateFilter = 4) then
-        begin
-        for i:= 0 to pred(BasePointsCount) do
-            begin
-            dec(pa.ar[i].y, 100);
-            if pa.ar[i].y < 0 then
-                pa.ar[i].y:= 0;
-            end;
-        for i:= 0 to pred(FillPointsCount) do
-            begin
-            dec(fps^[i].y, 100);
-            if fps^[i].y < 0 then
-                fps^[i].y:= 0;
-            end;
-        end;
-
-    if (canFlip and (getrandom(2) = 0)) then
-        begin
-        for i:= 0 to pred(BasePointsCount) do
-            pa.ar[i].y:= LAND_HEIGHT - 1 - pa.ar[i].y;
-        for i:= 0 to pred(FillPointsCount) do
-            fps^[i].y:= LAND_HEIGHT - 1 - fps^[i].y;
-        end;
-    end
-end;
-
-
-procedure GenBlank(var Template: TEdgeTemplate);
-var pa: TPixAr;
-    i: Longword;
-    y, x: Longword;
-    fps: TPointArray;
-begin
-    fps:=Template.FillPoints^;
-    ResizeLand(Template.TemplateWidth, Template.TemplateHeight);
-    for y:= 0 to LAND_HEIGHT - 1 do
-        for x:= 0 to LAND_WIDTH - 1 do
-            Land[y, x]:= lfBasic;
-    {$HINTS OFF}
-    SetPoints(Template, pa, @fps);
-    {$HINTS ON}
-    
-    for i:= 1 to Template.BezierizeCount do
-        begin
-        BezierizeEdge(pa, _0_5);
-        RandomizePoints(pa);
-        RandomizePoints(pa)
-        end;
-    for i:= 1 to Template.RandPassesCount do
-        RandomizePoints(pa);
-    BezierizeEdge(pa, _0_1);
-
-
-    DrawEdge(pa, 0);
-
-    with Template do
-        for i:= 0 to pred(FillPointsCount) do
-            with fps[i] do
-                FillLand(x, y, 0, 0);
-
-    DrawEdge(pa, lfBasic);
-
-    MaxHedgehogs:= Template.MaxHedgehogs;
-    hasGirders:= Template.hasGirders;
-    playHeight:= Template.TemplateHeight;
-    playWidth:= Template.TemplateWidth;
-    leftX:= ((LAND_WIDTH - playWidth) div 2);
-    rightX:= (playWidth + ((LAND_WIDTH - playWidth) div 2)) - 1;
-    topY:= LAND_HEIGHT - playHeight;
-
-    // HACK: force to only cavern even if a cavern map is invertable if cTemplateFilter = 4 ?
-    if (cTemplateFilter = 4)
-    or (Template.canInvert and (getrandom(2) = 0))
-    or (not Template.canInvert and Template.isNegative) then
-        begin
-        hasBorder:= true;
-        for y:= 0 to LAND_HEIGHT - 1 do
-            for x:= 0 to LAND_WIDTH - 1 do
-                if (y < topY) or (x < leftX) or (x > rightX) then
-                    Land[y, x]:= 0
-                else
-                    begin
-                    if Land[y, x] = 0 then
-                        Land[y, x]:= lfBasic
-                    else if Land[y, x] = lfBasic then
-                        Land[y, x]:= 0;
-                    end;
-        end;
-end;
 
 procedure GenDrawnMap;
 begin
@@ -701,7 +545,7 @@
             begin
             WriteLnToConsole('Generating land...');
             case cMapGen of
-                0: GenBlank(EdgeTemplates[SelectTemplate]);
+                0: GenTemplated(EdgeTemplates[SelectTemplate]);
                 //1: begin ResizeLand(4096,2048); GenMaze; end;
                 1: begin ResizeLand(4096,2048); GenPerlin; end;
                 2: GenDrawnMap;
@@ -830,7 +674,7 @@
 begin
     WriteLnToConsole('Generating preview...');
     case cMapGen of
-        0: GenBlank(EdgeTemplates[SelectTemplate]);
+        0: GenTemplated(EdgeTemplates[SelectTemplate]);
         //1: begin ResizeLand(4096,2048); GenMaze; end;
         1: begin ResizeLand(4096,2048); GenPerlin; end;
         2: GenDrawnMap;
@@ -878,7 +722,7 @@
 begin
     WriteLnToConsole('Generating preview...');
     case cMapGen of
-        0: GenBlank(EdgeTemplates[SelectTemplate]);
+        0: GenTemplated(EdgeTemplates[SelectTemplate]);
         //1: begin ResizeLand(4096,2048); GenMaze; end;
         1: begin ResizeLand(4096,2048); GenPerlin; end;
         2: GenDrawnMap;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/hedgewars/uLandGenTemplateBased.pas	Fri Mar 14 19:59:34 2014 +0400
@@ -0,0 +1,146 @@
+unit uLandGenTemplateBased;
+interface
+
+uses uLandTemplates;
+
+procedure GenTemplated(var Template: TEdgeTemplate);
+
+implementation
+uses uTypes, uVariables, uConsts, uFloat, uLandOutline, uLandUtils, uRandom;
+
+
+procedure SetPoints(var Template: TEdgeTemplate; var pa: TPixAr; fps: PPointArray);
+var i: LongInt;
+begin
+    with Template do
+        begin
+        pa.Count:= BasePointsCount;
+        for i:= 0 to pred(pa.Count) do
+            begin
+            pa.ar[i].x:= BasePoints^[i].x + LongInt(GetRandom(BasePoints^[i].w));
+            if pa.ar[i].x <> NTPX then
+            pa.ar[i].x:= pa.ar[i].x + ((LAND_WIDTH - Template.TemplateWidth) div 2);
+            pa.ar[i].y:= BasePoints^[i].y + LongInt(GetRandom(BasePoints^[i].h)) + LAND_HEIGHT - LongInt(Template.TemplateHeight)
+            end;
+
+        if canMirror then
+            if getrandom(2) = 0 then
+                begin
+                for i:= 0 to pred(BasePointsCount) do
+                if pa.ar[i].x <> NTPX then
+                    pa.ar[i].x:= LAND_WIDTH - 1 - pa.ar[i].x;
+                for i:= 0 to pred(FillPointsCount) do
+                    fps^[i].x:= LAND_WIDTH - 1 - fps^[i].x;
+                end;
+
+(*  Experiment in making this option more useful
+     if ((not isNegative) and (cTemplateFilter = 4)) or
+        (canFlip and (getrandom(2) = 0)) then
+           begin
+           for i:= 0 to pred(BasePointsCount) do
+               begin
+               pa.ar[i].y:= LAND_HEIGHT - 1 - pa.ar[i].y + (LAND_HEIGHT - TemplateHeight) * 2;
+               if pa.ar[i].y > LAND_HEIGHT - 1 then
+                   pa.ar[i].y:= LAND_HEIGHT - 1;
+               end;
+           for i:= 0 to pred(FillPointsCount) do
+               begin
+               FillPoints^[i].y:= LAND_HEIGHT - 1 - FillPoints^[i].y + (LAND_HEIGHT - TemplateHeight) * 2;
+               if FillPoints^[i].y > LAND_HEIGHT - 1 then
+                   FillPoints^[i].y:= LAND_HEIGHT - 1;
+               end;
+           end;
+     end
+*)
+// template recycling.  Pull these off the floor a bit
+    if (not isNegative) and (cTemplateFilter = 4) then
+        begin
+        for i:= 0 to pred(BasePointsCount) do
+            begin
+            dec(pa.ar[i].y, 100);
+            if pa.ar[i].y < 0 then
+                pa.ar[i].y:= 0;
+            end;
+        for i:= 0 to pred(FillPointsCount) do
+            begin
+            dec(fps^[i].y, 100);
+            if fps^[i].y < 0 then
+                fps^[i].y:= 0;
+            end;
+        end;
+
+    if (canFlip and (getrandom(2) = 0)) then
+        begin
+        for i:= 0 to pred(BasePointsCount) do
+            pa.ar[i].y:= LAND_HEIGHT - 1 - pa.ar[i].y;
+        for i:= 0 to pred(FillPointsCount) do
+            fps^[i].y:= LAND_HEIGHT - 1 - fps^[i].y;
+        end;
+    end
+end;
+    
+procedure GenTemplated(var Template: TEdgeTemplate);
+var pa: TPixAr;
+    i: Longword;
+    y, x: Longword;
+    fps: TPointArray;
+begin
+    fps:=Template.FillPoints^;
+    ResizeLand(Template.TemplateWidth, Template.TemplateHeight);
+    for y:= 0 to LAND_HEIGHT - 1 do
+        for x:= 0 to LAND_WIDTH - 1 do
+            Land[y, x]:= lfBasic;
+    {$HINTS OFF}
+    SetPoints(Template, pa, @fps);
+    {$HINTS ON}
+    
+    for i:= 1 to Template.BezierizeCount do
+        begin
+        BezierizeEdge(pa, _0_5);
+        RandomizePoints(pa);
+        RandomizePoints(pa)
+        end;
+    for i:= 1 to Template.RandPassesCount do
+        RandomizePoints(pa);
+    BezierizeEdge(pa, _0_1);
+
+
+    DrawEdge(pa, 0);
+
+    with Template do
+        for i:= 0 to pred(FillPointsCount) do
+            with fps[i] do
+                FillLand(x, y, 0, 0);
+
+    DrawEdge(pa, lfBasic);
+
+    MaxHedgehogs:= Template.MaxHedgehogs;
+    hasGirders:= Template.hasGirders;
+    playHeight:= Template.TemplateHeight;
+    playWidth:= Template.TemplateWidth;
+    leftX:= ((LAND_WIDTH - playWidth) div 2);
+    rightX:= (playWidth + ((LAND_WIDTH - playWidth) div 2)) - 1;
+    topY:= LAND_HEIGHT - playHeight;
+
+    // HACK: force to only cavern even if a cavern map is invertable if cTemplateFilter = 4 ?
+    if (cTemplateFilter = 4)
+    or (Template.canInvert and (getrandom(2) = 0))
+    or (not Template.canInvert and Template.isNegative) then
+        begin
+        hasBorder:= true;
+        for y:= 0 to LAND_HEIGHT - 1 do
+            for x:= 0 to LAND_WIDTH - 1 do
+                if (y < topY) or (x < leftX) or (x > rightX) then
+                    Land[y, x]:= 0
+                else
+                    begin
+                    if Land[y, x] = 0 then
+                        Land[y, x]:= lfBasic
+                    else if Land[y, x] = lfBasic then
+                        Land[y, x]:= 0;
+                    end;
+        end;
+end;
+
+
+end.
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/hedgewars/uLandUtils.pas	Fri Mar 14 19:59:34 2014 +0400
@@ -0,0 +1,34 @@
+unit uLandUtils;
+interface
+
+procedure ResizeLand(width, height: LongWord);
+
+implementation
+uses uUtils, uConsts, uVariables;
+
+procedure ResizeLand(width, height: LongWord);
+var potW, potH: LongInt;
+begin
+potW:= toPowerOf2(width);
+potH:= toPowerOf2(height);
+if (potW <> LAND_WIDTH) or (potH <> LAND_HEIGHT) then
+    begin
+    LAND_WIDTH:= potW;
+    LAND_HEIGHT:= potH;
+    LAND_WIDTH_MASK:= not(LAND_WIDTH-1);
+    LAND_HEIGHT_MASK:= not(LAND_HEIGHT-1);
+    cWaterLine:= LAND_HEIGHT;
+    if (cReducedQuality and rqBlurryLand) = 0 then
+        SetLength(LandPixels, LAND_HEIGHT, LAND_WIDTH)
+    else
+        SetLength(LandPixels, LAND_HEIGHT div 2, LAND_WIDTH div 2);
+
+    SetLength(Land, LAND_HEIGHT, LAND_WIDTH);
+    SetLength(LandDirty, (LAND_HEIGHT div 32), (LAND_WIDTH div 32));
+    // 0.5 is already approaching on unplayable
+    if (width div 4096 >= 2) or (height div 2048 >= 2) then cMaxZoomLevel:= 0.5;
+    cMinMaxZoomLevelDelta:= cMaxZoomLevel - cMinZoomLevel
+    end;
+end;
+
+end.