Improve land generator
authorunc0rr
Sun, 15 Jul 2007 20:52:03 +0000
changeset 561 19d2d422ff84
parent 560 7bf2b554de0c
child 562 d1a9035175ed
Improve land generator
hedgewars/uLand.pas
hedgewars/uLandTemplates.pas
--- a/hedgewars/uLand.pas	Sun Jul 15 14:02:26 2007 +0000
+++ b/hedgewars/uLand.pas	Sun Jul 15 20:52:03 2007 +0000
@@ -395,11 +395,49 @@
      end
 end;
 
+function CheckIntersect(V1, V2, V3, V4: TPoint): boolean;
+var c1, c2, dm: LongInt;
+begin
+dm:= (V4.y - V3.y) * (V2.x - V1.x) - (V4.x - V3.x) * (V2.y - V1.y);
+c1:= (V4.x - V3.x) * (V1.y - V3.y) - (V4.y - V3.y) * (V1.x - V3.x);
+if dm = 0 then exit(false);
+
+c2:= (V2.x - V3.x) * (V1.y - V3.y) - (V2.y - V3.y) * (V1.x - V3.x);
+if dm > 0 then
+   begin
+   if (c1 < 0) or (c1 > dm) then exit(false);
+   if (c2 < 0) or (c2 > dm) then exit(false)
+   end else
+   begin
+   if (c1 > 0) or (c1 < dm) then exit(false);
+   if (c2 > 0) or (c2 < dm) then exit(false)
+   end;
+
+//AddFileLog('1  (' + inttostr(V1.x) + ',' + inttostr(V1.y) + ')x(' + inttostr(V2.x) + ',' + inttostr(V2.y) + ')');
+//AddFileLog('2  (' + inttostr(V3.x) + ',' + inttostr(V3.y) + ')x(' + inttostr(V4.x) + ',' + inttostr(V4.y) + ')');
+CheckIntersect:= true
+end;
+
+function CheckSelfIntersect(var pa: TPixAr; ind: Longword): boolean;
+var i: Longword;
+begin
+if (ind <= 0) or (ind >= Pred(pa.Count)) then exit(false);
+for i:= 1 to pa.Count - 3 do
+    if (i <= ind - 1) or (i >= ind + 2) then
+      begin
+      if (i <> ind - 1) and
+         CheckIntersect(pa.ar[ind], pa.ar[ind - 1], pa.ar[i], pa.ar[i - 1]) then exit(true);
+      if (i <> ind + 2) and
+         CheckIntersect(pa.ar[ind], pa.ar[ind + 1], pa.ar[i], pa.ar[i - 1]) then exit(true);
+      end;
+CheckSelfIntersect:= false
+end;
+
 procedure RandomizePoints(var pa: TPixAr);
 const cEdge = 55;
-      cMinDist = 0;
+      cMinDist = 8;
 var radz: array[0..Pred(cMaxEdgePoints)] of LongInt;
-    i, k, dist: LongInt;
+    i, k, dist, px, py: LongInt;
 begin
 radz[0]:= 0;
 for i:= 0 to Pred(pa.Count) do
@@ -421,8 +459,15 @@
   with pa.ar[i] do
     if ((x and $FFFFF800) = 0) and ((y and $FFFFFC00) = 0) then
       begin
+      px:= x;
+      py:= y;
       x:= x + LongInt(GetRandom(7) - 3) * (radz[i] * 5 div 7) div 3;
-      y:= y + LongInt(GetRandom(7) - 3) * (radz[i] * 5 div 7) div 3
+      y:= y + LongInt(GetRandom(7) - 3) * (radz[i] * 5 div 7) div 3;
+      if CheckSelfIntersect(pa, i) then
+         begin
+         x:= px;
+         y:= py
+         end;
       end
 end;
 
@@ -440,6 +485,7 @@
 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);
--- a/hedgewars/uLandTemplates.pas	Sun Jul 15 14:02:26 2007 +0000
+++ b/hedgewars/uLandTemplates.pas	Sun Jul 15 20:52:03 2007 +0000
@@ -349,7 +349,7 @@
         FillPoints: @Template0FPoints;
         FillPointsCount: Succ(High(Template0FPoints));
         BezierizeCount: 3;
-        RandPassesCount: 1;
+        RandPassesCount: 8;
         canMirror: true; canFlip: false;
        ),
        (BasePoints: @Template1Points;
@@ -357,7 +357,7 @@
         FillPoints: @Template1FPoints;
         FillPointsCount: Succ(High(Template1FPoints));
         BezierizeCount: 3;
-        RandPassesCount: 2;
+        RandPassesCount: 7;
         canMirror: true; canFlip: false;
        ),
        (BasePoints: @Template2Points;
@@ -365,7 +365,7 @@
         FillPoints: @Template2FPoints;
         FillPointsCount: Succ(High(Template2FPoints));
         BezierizeCount: 2;
-        RandPassesCount: 2;
+        RandPassesCount: 6;
         canMirror: true; canFlip: false;
        ),
        (BasePoints: @Template3Points;
@@ -373,7 +373,7 @@
         FillPoints: @Template3FPoints;
         FillPointsCount: Succ(High(Template3FPoints));
         BezierizeCount: 3;
-        RandPassesCount: 2;
+        RandPassesCount: 4;
         canMirror: false; canFlip: false;
        ),
        (BasePoints: @Template4Points;
@@ -381,7 +381,7 @@
         FillPoints: @Template4FPoints;
         FillPointsCount: Succ(High(Template4FPoints));
         BezierizeCount: 3;
-        RandPassesCount: 2;
+        RandPassesCount: 4;
         canMirror: true; canFlip: false;
        ),
        (BasePoints: @Template5Points;
@@ -389,15 +389,15 @@
         FillPoints: @Template5FPoints;
         FillPointsCount: Succ(High(Template5FPoints));
         BezierizeCount: 2;
-        RandPassesCount: 3;
+        RandPassesCount: 8;
         canMirror: true; canFlip: false;
        ),
        (BasePoints: @Template6Points;
         BasePointsCount: Succ(High(Template6Points));
         FillPoints: @Template6FPoints;
         FillPointsCount: Succ(High(Template6FPoints));
-        BezierizeCount: 3;
-        RandPassesCount: 1;
+        BezierizeCount: 2;
+        RandPassesCount: 5;
         canMirror: true; canFlip: false;
        ),
        (BasePoints: @Template7Points;
@@ -405,7 +405,7 @@
         FillPoints: @Template7FPoints;
         FillPointsCount: Succ(High(Template7FPoints));
         BezierizeCount: 4;
-        RandPassesCount: 1;
+        RandPassesCount: 4;
         canMirror: true; canFlip: false;
        ),
        (BasePoints: @Template8Points;
@@ -413,31 +413,31 @@
         FillPoints: @Template8FPoints;
         FillPointsCount: Succ(High(Template8FPoints));
         BezierizeCount: 2;
-        RandPassesCount: 2;
+        RandPassesCount: 7;
         canMirror: true; canFlip: false;
        ),
        (BasePoints: @Template9Points;
         BasePointsCount: Succ(High(Template9Points));
         FillPoints: @Template9FPoints;
         FillPointsCount: Succ(High(Template9FPoints));
-        BezierizeCount: 2;
-        RandPassesCount: 3;
+        BezierizeCount: 1;
+        RandPassesCount: 5;
         canMirror: true; canFlip: false;
        ),
        (BasePoints: @Template10Points;
         BasePointsCount: Succ(High(Template10Points));
         FillPoints: @Template10FPoints;
         FillPointsCount: Succ(High(Template10FPoints));
-        BezierizeCount: 3;
-        RandPassesCount: 2;
+        BezierizeCount: 2;
+        RandPassesCount: 6;
         canMirror: true; canFlip: false;
        ),
        (BasePoints: @Template11Points;
         BasePointsCount: Succ(High(Template11Points));
         FillPoints: @Template11FPoints;
         FillPointsCount: Succ(High(Template11FPoints));
-        BezierizeCount: 2;
-        RandPassesCount: 5;
+        BezierizeCount: 1;
+        RandPassesCount: 8;
         canMirror: true; canFlip: false;
        )
       );