hedgewars/uLandGenTemplateBased.pas
author Wuzzy <Wuzzy2@mail.ru>
Mon, 26 Mar 2018 15:12:33 +0200
changeset 13287 b13071610c07
parent 11542 de40095f3327
child 13908 e26573441808
permissions -rw-r--r--
Flying portal balls now spawn a few spark effects This has been adapted from PortalEffects from HedgeEditor. HedgeEditor already included such effects for some reason, so they have been removed as redundant.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10198
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
     1
unit uLandGenTemplateBased;
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
     2
interface
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
     3
10387
cb17b79844b5 Apply new distortion on maze gen
unc0rr
parents: 10226
diff changeset
     4
uses uLandTemplates, uLandOutline;
10198
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
     5
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
     6
procedure GenTemplated(var Template: TEdgeTemplate);
10387
cb17b79844b5 Apply new distortion on maze gen
unc0rr
parents: 10226
diff changeset
     7
procedure DivideEdges(fillPointsCount: LongWord; var pa: TPixAr);
10198
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
     8
10499
0d8016085108 modestly randomise dab, bump points again.
nemo
parents: 10495
diff changeset
     9
var minDistance, dabDiv: LongInt; // different details size
10477
b219c5a2317f Fiddling with slider, unbreak maze. Next to mess around w/ perlin params.
nemo
parents: 10472
diff changeset
    10
10198
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    11
implementation
11542
de40095f3327 - ifdef to avoid compiler warning
antonc27 <antonc27@mail.ru>
parents: 11541
diff changeset
    12
uses {$IFDEF IPHONEOS}uTypes, {$ENDIF} uVariables, uConsts, uFloat, uLandUtils, uRandom, SDLh, math;
10198
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    13
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    14
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    15
procedure SetPoints(var Template: TEdgeTemplate; var pa: TPixAr; fps: PPointArray);
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    16
var i: LongInt;
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    17
begin
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    18
    with Template do
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    19
        begin
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    20
        pa.Count:= BasePointsCount;
10564
0cb20aa8877a more fixing and allow pas2c to run tests. they will still fail though - engine does not exit with the specified exit codes, also data types are messed up
sheepluva
parents: 10562
diff changeset
    21
        for i:= 0 to pred(LongInt(pa.Count)) do
10198
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    22
            begin
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    23
            pa.ar[i].x:= BasePoints^[i].x + LongInt(GetRandom(BasePoints^[i].w));
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    24
            if pa.ar[i].x <> NTPX then
10199
fdb689b57b1b Some progress on new generator
unc0rr
parents: 10198
diff changeset
    25
                pa.ar[i].x:= pa.ar[i].x + ((LAND_WIDTH - Template.TemplateWidth) div 2);
10198
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    26
            pa.ar[i].y:= BasePoints^[i].y + LongInt(GetRandom(BasePoints^[i].h)) + LAND_HEIGHT - LongInt(Template.TemplateHeight)
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    27
            end;
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    28
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    29
        if canMirror then
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    30
            if getrandom(2) = 0 then
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    31
                begin
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    32
                for i:= 0 to pred(BasePointsCount) do
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    33
                if pa.ar[i].x <> NTPX then
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    34
                    pa.ar[i].x:= LAND_WIDTH - 1 - pa.ar[i].x;
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    35
                for i:= 0 to pred(FillPointsCount) do
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    36
                    fps^[i].x:= LAND_WIDTH - 1 - fps^[i].x;
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    37
                end;
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    38
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    39
(*  Experiment in making this option more useful
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    40
     if ((not isNegative) and (cTemplateFilter = 4)) or
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    41
        (canFlip and (getrandom(2) = 0)) then
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    42
           begin
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    43
           for i:= 0 to pred(BasePointsCount) do
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    44
               begin
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    45
               pa.ar[i].y:= LAND_HEIGHT - 1 - pa.ar[i].y + (LAND_HEIGHT - TemplateHeight) * 2;
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    46
               if pa.ar[i].y > LAND_HEIGHT - 1 then
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    47
                   pa.ar[i].y:= LAND_HEIGHT - 1;
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    48
               end;
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    49
           for i:= 0 to pred(FillPointsCount) do
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    50
               begin
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    51
               FillPoints^[i].y:= LAND_HEIGHT - 1 - FillPoints^[i].y + (LAND_HEIGHT - TemplateHeight) * 2;
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    52
               if FillPoints^[i].y > LAND_HEIGHT - 1 then
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    53
                   FillPoints^[i].y:= LAND_HEIGHT - 1;
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    54
               end;
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    55
           end;
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    56
     end
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    57
*)
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    58
// template recycling.  Pull these off the floor a bit
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    59
    if (not isNegative) and (cTemplateFilter = 4) then
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    60
        begin
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    61
        for i:= 0 to pred(BasePointsCount) do
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    62
            begin
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    63
            dec(pa.ar[i].y, 100);
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    64
            if pa.ar[i].y < 0 then
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    65
                pa.ar[i].y:= 0;
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    66
            end;
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    67
        for i:= 0 to pred(FillPointsCount) do
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    68
            begin
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    69
            dec(fps^[i].y, 100);
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    70
            if fps^[i].y < 0 then
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    71
                fps^[i].y:= 0;
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    72
            end;
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    73
        end;
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    74
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    75
    if (canFlip and (getrandom(2) = 0)) then
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    76
        begin
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    77
        for i:= 0 to pred(BasePointsCount) do
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    78
            pa.ar[i].y:= LAND_HEIGHT - 1 - pa.ar[i].y;
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    79
        for i:= 0 to pred(FillPointsCount) do
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    80
            fps^[i].y:= LAND_HEIGHT - 1 - fps^[i].y;
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    81
        end;
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    82
    end
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    83
end;
10199
fdb689b57b1b Some progress on new generator
unc0rr
parents: 10198
diff changeset
    84
fdb689b57b1b Some progress on new generator
unc0rr
parents: 10198
diff changeset
    85
fdb689b57b1b Some progress on new generator
unc0rr
parents: 10198
diff changeset
    86
procedure Distort1(var Template: TEdgeTemplate; var pa: TPixAr);
fdb689b57b1b Some progress on new generator
unc0rr
parents: 10198
diff changeset
    87
var i: Longword;
fdb689b57b1b Some progress on new generator
unc0rr
parents: 10198
diff changeset
    88
begin
fdb689b57b1b Some progress on new generator
unc0rr
parents: 10198
diff changeset
    89
    for i:= 1 to Template.BezierizeCount do
fdb689b57b1b Some progress on new generator
unc0rr
parents: 10198
diff changeset
    90
        begin
fdb689b57b1b Some progress on new generator
unc0rr
parents: 10198
diff changeset
    91
        BezierizeEdge(pa, _0_5);
fdb689b57b1b Some progress on new generator
unc0rr
parents: 10198
diff changeset
    92
        RandomizePoints(pa);
fdb689b57b1b Some progress on new generator
unc0rr
parents: 10198
diff changeset
    93
        RandomizePoints(pa)
fdb689b57b1b Some progress on new generator
unc0rr
parents: 10198
diff changeset
    94
        end;
fdb689b57b1b Some progress on new generator
unc0rr
parents: 10198
diff changeset
    95
    for i:= 1 to Template.RandPassesCount do
fdb689b57b1b Some progress on new generator
unc0rr
parents: 10198
diff changeset
    96
        RandomizePoints(pa);
fdb689b57b1b Some progress on new generator
unc0rr
parents: 10198
diff changeset
    97
    BezierizeEdge(pa, _0_1);
fdb689b57b1b Some progress on new generator
unc0rr
parents: 10198
diff changeset
    98
end;
fdb689b57b1b Some progress on new generator
unc0rr
parents: 10198
diff changeset
    99
10209
76316652ef26 Fix warnings
unc0rr
parents: 10208
diff changeset
   100
procedure FindPoint(si: LongInt; fillPointsCount: LongWord; var newPoint: TPoint; var pa: TPixAr);
10208
f04fdb35fc33 - Limit outline to leftX/rightX/topY instead of LAND_WIDTH/LAND_HEIGHT
unc0rr
parents: 10207
diff changeset
   101
const mapBorderMargin = 40;
10205
fc99e124ba4d Prevent intersections with segment from last to first point, adjust size of details
unc0rr
parents: 10204
diff changeset
   102
var p1, p2, p4, fp, mp: TPoint;
10226
cb63617a0c2f Fix new generator on 32 bit arch
unc0rr
parents: 10225
diff changeset
   103
    i, t1, t2, iy, ix, aqpb: LongInt;
cb63617a0c2f Fix new generator on 32 bit arch
unc0rr
parents: 10225
diff changeset
   104
    a, b, p, q: LongInt;
10200
edc2fe0ca03f More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents: 10199
diff changeset
   105
    dab, d, distL, distR: LongInt;
10199
fdb689b57b1b Some progress on new generator
unc0rr
parents: 10198
diff changeset
   106
begin
10207
9dd3a44805a1 - Make sure distortion doesn't move fill point to other side of segment, this prevents corrupted maps
unc0rr
parents: 10206
diff changeset
   107
    // [p1, p2] is the segment we're trying to divide
10199
fdb689b57b1b Some progress on new generator
unc0rr
parents: 10198
diff changeset
   108
    p1:= pa.ar[si];
fdb689b57b1b Some progress on new generator
unc0rr
parents: 10198
diff changeset
   109
    p2:= pa.ar[si + 1];
fdb689b57b1b Some progress on new generator
unc0rr
parents: 10198
diff changeset
   110
10206
979a663d7351 Process segments from last to first point too
unc0rr
parents: 10205
diff changeset
   111
    if p2.x = NTPX then
979a663d7351 Process segments from last to first point too
unc0rr
parents: 10205
diff changeset
   112
    // it is segment from last to first point, so need to find first point
979a663d7351 Process segments from last to first point too
unc0rr
parents: 10205
diff changeset
   113
    begin
979a663d7351 Process segments from last to first point too
unc0rr
parents: 10205
diff changeset
   114
        i:= si - 2;
979a663d7351 Process segments from last to first point too
unc0rr
parents: 10205
diff changeset
   115
        while (i >= 0) and (pa.ar[i].x <> NTPX) do
979a663d7351 Process segments from last to first point too
unc0rr
parents: 10205
diff changeset
   116
            dec(i);
979a663d7351 Process segments from last to first point too
unc0rr
parents: 10205
diff changeset
   117
        p2:= pa.ar[i + 1]
979a663d7351 Process segments from last to first point too
unc0rr
parents: 10205
diff changeset
   118
    end;
979a663d7351 Process segments from last to first point too
unc0rr
parents: 10205
diff changeset
   119
10201
9bee9541edf1 Fix detection of intersections, still need to check if passing any point in move, but result is already okayish
unc0rr
parents: 10200
diff changeset
   120
    // perpendicular vector
9bee9541edf1 Fix detection of intersections, still need to check if passing any point in move, but result is already okayish
unc0rr
parents: 10200
diff changeset
   121
    a:= p2.y - p1.y;
9bee9541edf1 Fix detection of intersections, still need to check if passing any point in move, but result is already okayish
unc0rr
parents: 10200
diff changeset
   122
    b:= p1.x - p2.x;
9bee9541edf1 Fix detection of intersections, still need to check if passing any point in move, but result is already okayish
unc0rr
parents: 10200
diff changeset
   123
    dab:= DistanceI(a, b).Round;
9bee9541edf1 Fix detection of intersections, still need to check if passing any point in move, but result is already okayish
unc0rr
parents: 10200
diff changeset
   124
10206
979a663d7351 Process segments from last to first point too
unc0rr
parents: 10205
diff changeset
   125
    // its middle point
979a663d7351 Process segments from last to first point too
unc0rr
parents: 10205
diff changeset
   126
    mp.x:= (p1.x + p2.x) div 2;
979a663d7351 Process segments from last to first point too
unc0rr
parents: 10205
diff changeset
   127
    mp.y:= (p1.y + p2.y) div 2;
979a663d7351 Process segments from last to first point too
unc0rr
parents: 10205
diff changeset
   128
979a663d7351 Process segments from last to first point too
unc0rr
parents: 10205
diff changeset
   129
    // don't process too short segments or those which are too close to map borders
979a663d7351 Process segments from last to first point too
unc0rr
parents: 10205
diff changeset
   130
    if (p1.x = NTPX)
10510
9329dab04490 some whitespace fixes
sheepluva
parents: 10502
diff changeset
   131
            or (dab < minDistance * 3)
10209
76316652ef26 Fix warnings
unc0rr
parents: 10208
diff changeset
   132
            or (mp.x < LongInt(leftX) + mapBorderMargin)
76316652ef26 Fix warnings
unc0rr
parents: 10208
diff changeset
   133
            or (mp.x > LongInt(rightX) - mapBorderMargin)
76316652ef26 Fix warnings
unc0rr
parents: 10208
diff changeset
   134
            or (mp.y < LongInt(topY) + mapBorderMargin)
76316652ef26 Fix warnings
unc0rr
parents: 10208
diff changeset
   135
            or (mp.y > LongInt(LAND_HEIGHT) - mapBorderMargin)
10206
979a663d7351 Process segments from last to first point too
unc0rr
parents: 10205
diff changeset
   136
    then
10200
edc2fe0ca03f More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents: 10199
diff changeset
   137
    begin
edc2fe0ca03f More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents: 10199
diff changeset
   138
        newPoint:= p1;
edc2fe0ca03f More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents: 10199
diff changeset
   139
        exit;
edc2fe0ca03f More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents: 10199
diff changeset
   140
    end;
edc2fe0ca03f More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents: 10199
diff changeset
   141
edc2fe0ca03f More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents: 10199
diff changeset
   142
    // find distances to map borders
edc2fe0ca03f More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents: 10199
diff changeset
   143
    if a <> 0 then
edc2fe0ca03f More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents: 10199
diff changeset
   144
    begin
edc2fe0ca03f More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents: 10199
diff changeset
   145
        // left border
10209
76316652ef26 Fix warnings
unc0rr
parents: 10208
diff changeset
   146
        iy:= (LongInt(leftX) + mapBorderMargin - mp.x) * b div a + mp.y;
10208
f04fdb35fc33 - Limit outline to leftX/rightX/topY instead of LAND_WIDTH/LAND_HEIGHT
unc0rr
parents: 10207
diff changeset
   147
        d:= DistanceI(mp.x - leftX - mapBorderMargin, mp.y - iy).Round;
10200
edc2fe0ca03f More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents: 10199
diff changeset
   148
        t1:= a * (mp.x - mapBorderMargin) + b * (mp.y - iy);
edc2fe0ca03f More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents: 10199
diff changeset
   149
        if t1 > 0 then distL:= d else distR:= d;
edc2fe0ca03f More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents: 10199
diff changeset
   150
edc2fe0ca03f More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents: 10199
diff changeset
   151
        // right border
10502
a888e649bea2 Fix difference in map generation between fpc and pas2c engine
unc0rr
parents: 10499
diff changeset
   152
        iy:= (LongInt(rightX) - mapBorderMargin - mp.x) * b div a + mp.y;
10208
f04fdb35fc33 - Limit outline to leftX/rightX/topY instead of LAND_WIDTH/LAND_HEIGHT
unc0rr
parents: 10207
diff changeset
   153
        d:= DistanceI(mp.x - rightX + mapBorderMargin, mp.y - iy).Round;
10200
edc2fe0ca03f More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents: 10199
diff changeset
   154
        if t1 > 0 then distR:= d else distL:= d;
10478
12662c21e0e9 Fix use of uninitialized variables (fixes straight lines in maze gen aswell)
unc0rr
parents: 10477
diff changeset
   155
    end else
12662c21e0e9 Fix use of uninitialized variables (fixes straight lines in maze gen aswell)
unc0rr
parents: 10477
diff changeset
   156
    begin
12662c21e0e9 Fix use of uninitialized variables (fixes straight lines in maze gen aswell)
unc0rr
parents: 10477
diff changeset
   157
        distL:= LAND_WIDTH + LAND_HEIGHT;
12662c21e0e9 Fix use of uninitialized variables (fixes straight lines in maze gen aswell)
unc0rr
parents: 10477
diff changeset
   158
        distR:= distL;
10200
edc2fe0ca03f More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents: 10199
diff changeset
   159
    end;
edc2fe0ca03f More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents: 10199
diff changeset
   160
edc2fe0ca03f More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents: 10199
diff changeset
   161
    if b <> 0 then
edc2fe0ca03f More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents: 10199
diff changeset
   162
    begin
edc2fe0ca03f More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents: 10199
diff changeset
   163
        // top border
10209
76316652ef26 Fix warnings
unc0rr
parents: 10208
diff changeset
   164
        ix:= (LongInt(topY) + mapBorderMargin - mp.y) * a div b + mp.x;
10208
f04fdb35fc33 - Limit outline to leftX/rightX/topY instead of LAND_WIDTH/LAND_HEIGHT
unc0rr
parents: 10207
diff changeset
   165
        d:= DistanceI(mp.y - topY - mapBorderMargin, mp.x - ix).Round;
10200
edc2fe0ca03f More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents: 10199
diff changeset
   166
        t2:= b * (mp.y - mapBorderMargin) + a * (mp.x - ix);
edc2fe0ca03f More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents: 10199
diff changeset
   167
        if t2 > 0 then distL:= min(d, distL) else distR:= min(d, distR);
edc2fe0ca03f More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents: 10199
diff changeset
   168
edc2fe0ca03f More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents: 10199
diff changeset
   169
        // bottom border
edc2fe0ca03f More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents: 10199
diff changeset
   170
        ix:= (LAND_HEIGHT - mapBorderMargin - mp.y) * a div b + mp.x;
edc2fe0ca03f More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents: 10199
diff changeset
   171
        d:= DistanceI(mp.y - LAND_HEIGHT + mapBorderMargin, mp.x - ix).Round;
edc2fe0ca03f More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents: 10199
diff changeset
   172
        if t2 > 0 then distR:= min(d, distR) else distL:= min(d, distL);
edc2fe0ca03f More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents: 10199
diff changeset
   173
    end;
edc2fe0ca03f More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents: 10199
diff changeset
   174
edc2fe0ca03f More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents: 10199
diff changeset
   175
    // now go through all other segments
10205
fc99e124ba4d Prevent intersections with segment from last to first point, adjust size of details
unc0rr
parents: 10204
diff changeset
   176
    fp:= pa.ar[0];
10564
0cb20aa8877a more fixing and allow pas2c to run tests. they will still fail though - engine does not exit with the specified exit codes, also data types are messed up
sheepluva
parents: 10562
diff changeset
   177
    for i:= 0 to LongInt(pa.Count) - 2 do
10205
fc99e124ba4d Prevent intersections with segment from last to first point, adjust size of details
unc0rr
parents: 10204
diff changeset
   178
        if pa.ar[i].x = NTPX then
fc99e124ba4d Prevent intersections with segment from last to first point, adjust size of details
unc0rr
parents: 10204
diff changeset
   179
            fp:= pa.ar[i + 1]
fc99e124ba4d Prevent intersections with segment from last to first point, adjust size of details
unc0rr
parents: 10204
diff changeset
   180
        else if (i <> si) then
10199
fdb689b57b1b Some progress on new generator
unc0rr
parents: 10198
diff changeset
   181
        begin
10205
fc99e124ba4d Prevent intersections with segment from last to first point, adjust size of details
unc0rr
parents: 10204
diff changeset
   182
        p4:= pa.ar[i + 1];
fc99e124ba4d Prevent intersections with segment from last to first point, adjust size of details
unc0rr
parents: 10204
diff changeset
   183
        if p4.x = NTPX then
fc99e124ba4d Prevent intersections with segment from last to first point, adjust size of details
unc0rr
parents: 10204
diff changeset
   184
            p4:= fp;
10206
979a663d7351 Process segments from last to first point too
unc0rr
parents: 10205
diff changeset
   185
10199
fdb689b57b1b Some progress on new generator
unc0rr
parents: 10198
diff changeset
   186
            // check if it intersects
10200
edc2fe0ca03f More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents: 10199
diff changeset
   187
            t1:= (mp.x - pa.ar[i].x) * b - a * (mp.y - pa.ar[i].y);
10205
fc99e124ba4d Prevent intersections with segment from last to first point, adjust size of details
unc0rr
parents: 10204
diff changeset
   188
            t2:= (mp.x - p4.x) * b - a * (mp.y - p4.y);
10199
fdb689b57b1b Some progress on new generator
unc0rr
parents: 10198
diff changeset
   189
fdb689b57b1b Some progress on new generator
unc0rr
parents: 10198
diff changeset
   190
            if (t1 > 0) <> (t2 > 0) then // yes it does, hard arith follows
fdb689b57b1b Some progress on new generator
unc0rr
parents: 10198
diff changeset
   191
            begin
10205
fc99e124ba4d Prevent intersections with segment from last to first point, adjust size of details
unc0rr
parents: 10204
diff changeset
   192
                p:= p4.x - pa.ar[i].x;
fc99e124ba4d Prevent intersections with segment from last to first point, adjust size of details
unc0rr
parents: 10204
diff changeset
   193
                q:= p4.y - pa.ar[i].y;
10199
fdb689b57b1b Some progress on new generator
unc0rr
parents: 10198
diff changeset
   194
                aqpb:= a * q - p * b;
fdb689b57b1b Some progress on new generator
unc0rr
parents: 10198
diff changeset
   195
fdb689b57b1b Some progress on new generator
unc0rr
parents: 10198
diff changeset
   196
                if (aqpb <> 0) then
fdb689b57b1b Some progress on new generator
unc0rr
parents: 10198
diff changeset
   197
                begin
fdb689b57b1b Some progress on new generator
unc0rr
parents: 10198
diff changeset
   198
                    // (ix; iy) is intersection point
10494
0eb97cf4c78e Fix warnings given by 32-bit fpc
unC0Rr
parents: 10478
diff changeset
   199
                    iy:= (((Int64(pa.ar[i].x) - mp.x) * b + Int64(mp.y) * a) * q - Int64(pa.ar[i].y) * p * b) div aqpb;
10201
9bee9541edf1 Fix detection of intersections, still need to check if passing any point in move, but result is already okayish
unc0rr
parents: 10200
diff changeset
   200
                    if abs(b) > abs(q) then
9bee9541edf1 Fix detection of intersections, still need to check if passing any point in move, but result is already okayish
unc0rr
parents: 10200
diff changeset
   201
                        ix:= (iy - mp.y) * a div b + mp.x
10199
fdb689b57b1b Some progress on new generator
unc0rr
parents: 10198
diff changeset
   202
                    else
10201
9bee9541edf1 Fix detection of intersections, still need to check if passing any point in move, but result is already okayish
unc0rr
parents: 10200
diff changeset
   203
                        ix:= (iy - pa.ar[i].y) * p div q + pa.ar[i].x;
10199
fdb689b57b1b Some progress on new generator
unc0rr
parents: 10198
diff changeset
   204
10200
edc2fe0ca03f More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents: 10199
diff changeset
   205
                    d:= DistanceI(mp.y - iy, mp.x - ix).Round;
edc2fe0ca03f More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents: 10199
diff changeset
   206
                    t1:= b * (mp.y - iy) + a * (mp.x - ix);
edc2fe0ca03f More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents: 10199
diff changeset
   207
                    if t1 > 0 then distL:= min(d, distL) else distR:= min(d, distR);
10199
fdb689b57b1b Some progress on new generator
unc0rr
parents: 10198
diff changeset
   208
                end;
fdb689b57b1b Some progress on new generator
unc0rr
parents: 10198
diff changeset
   209
            end;
fdb689b57b1b Some progress on new generator
unc0rr
parents: 10198
diff changeset
   210
        end;
10207
9dd3a44805a1 - Make sure distortion doesn't move fill point to other side of segment, this prevents corrupted maps
unc0rr
parents: 10206
diff changeset
   211
9dd3a44805a1 - Make sure distortion doesn't move fill point to other side of segment, this prevents corrupted maps
unc0rr
parents: 10206
diff changeset
   212
    // go through all points, including fill points
10564
0cb20aa8877a more fixing and allow pas2c to run tests. they will still fail though - engine does not exit with the specified exit codes, also data types are messed up
sheepluva
parents: 10562
diff changeset
   213
    for i:= 0 to Pred(LongInt(pa.Count + fillPointsCount)) do
10202
f7c8cb11a70e No self intersections, except for weirdness between first and last point
unc0rr
parents: 10201
diff changeset
   214
        // if this point isn't on current segment
10205
fc99e124ba4d Prevent intersections with segment from last to first point, adjust size of details
unc0rr
parents: 10204
diff changeset
   215
        if (si <> i) and (i <> si + 1) and (pa.ar[i].x <> NTPX) then
10202
f7c8cb11a70e No self intersections, except for weirdness between first and last point
unc0rr
parents: 10201
diff changeset
   216
        begin
f7c8cb11a70e No self intersections, except for weirdness between first and last point
unc0rr
parents: 10201
diff changeset
   217
            // also check intersection with rays through pa.ar[i] if this point is good
f7c8cb11a70e No self intersections, except for weirdness between first and last point
unc0rr
parents: 10201
diff changeset
   218
            t1:= (p1.x - pa.ar[i].x) * b - a * (p1.y - pa.ar[i].y);
f7c8cb11a70e No self intersections, except for weirdness between first and last point
unc0rr
parents: 10201
diff changeset
   219
            t2:= (p2.x - pa.ar[i].x) * b - a * (p2.y - pa.ar[i].y);
f7c8cb11a70e No self intersections, except for weirdness between first and last point
unc0rr
parents: 10201
diff changeset
   220
            if (t1 > 0) <> (t2 > 0) then
f7c8cb11a70e No self intersections, except for weirdness between first and last point
unc0rr
parents: 10201
diff changeset
   221
            begin
f7c8cb11a70e No self intersections, except for weirdness between first and last point
unc0rr
parents: 10201
diff changeset
   222
                // ray from p1
f7c8cb11a70e No self intersections, except for weirdness between first and last point
unc0rr
parents: 10201
diff changeset
   223
                p:= pa.ar[i].x - p1.x;
f7c8cb11a70e No self intersections, except for weirdness between first and last point
unc0rr
parents: 10201
diff changeset
   224
                q:= pa.ar[i].y - p1.y;
f7c8cb11a70e No self intersections, except for weirdness between first and last point
unc0rr
parents: 10201
diff changeset
   225
                aqpb:= a * q - p * b;
10201
9bee9541edf1 Fix detection of intersections, still need to check if passing any point in move, but result is already okayish
unc0rr
parents: 10200
diff changeset
   226
10202
f7c8cb11a70e No self intersections, except for weirdness between first and last point
unc0rr
parents: 10201
diff changeset
   227
                if (aqpb <> 0) then
f7c8cb11a70e No self intersections, except for weirdness between first and last point
unc0rr
parents: 10201
diff changeset
   228
                begin
f7c8cb11a70e No self intersections, except for weirdness between first and last point
unc0rr
parents: 10201
diff changeset
   229
                    // (ix; iy) is intersection point
10494
0eb97cf4c78e Fix warnings given by 32-bit fpc
unC0Rr
parents: 10478
diff changeset
   230
                    iy:= (((Int64(p1.x) - mp.x) * b + Int64(mp.y) * a) * q - Int64(p1.y) * p * b) div aqpb;
10202
f7c8cb11a70e No self intersections, except for weirdness between first and last point
unc0rr
parents: 10201
diff changeset
   231
                    if abs(b) > abs(q) then
f7c8cb11a70e No self intersections, except for weirdness between first and last point
unc0rr
parents: 10201
diff changeset
   232
                        ix:= (iy - mp.y) * a div b + mp.x
f7c8cb11a70e No self intersections, except for weirdness between first and last point
unc0rr
parents: 10201
diff changeset
   233
                    else
f7c8cb11a70e No self intersections, except for weirdness between first and last point
unc0rr
parents: 10201
diff changeset
   234
                        ix:= (iy - p1.y) * p div q + p1.x;
f7c8cb11a70e No self intersections, except for weirdness between first and last point
unc0rr
parents: 10201
diff changeset
   235
f7c8cb11a70e No self intersections, except for weirdness between first and last point
unc0rr
parents: 10201
diff changeset
   236
                    d:= DistanceI(mp.y - iy, mp.x - ix).Round;
f7c8cb11a70e No self intersections, except for weirdness between first and last point
unc0rr
parents: 10201
diff changeset
   237
                    t1:= b * (mp.y - iy) + a * (mp.x - ix);
f7c8cb11a70e No self intersections, except for weirdness between first and last point
unc0rr
parents: 10201
diff changeset
   238
                    if t1 > 0 then distL:= min(d, distL) else distR:= min(d, distR);
f7c8cb11a70e No self intersections, except for weirdness between first and last point
unc0rr
parents: 10201
diff changeset
   239
                end;
f7c8cb11a70e No self intersections, except for weirdness between first and last point
unc0rr
parents: 10201
diff changeset
   240
f7c8cb11a70e No self intersections, except for weirdness between first and last point
unc0rr
parents: 10201
diff changeset
   241
                // and ray from p2
f7c8cb11a70e No self intersections, except for weirdness between first and last point
unc0rr
parents: 10201
diff changeset
   242
                p:= pa.ar[i].x - p2.x;
f7c8cb11a70e No self intersections, except for weirdness between first and last point
unc0rr
parents: 10201
diff changeset
   243
                q:= pa.ar[i].y - p2.y;
f7c8cb11a70e No self intersections, except for weirdness between first and last point
unc0rr
parents: 10201
diff changeset
   244
                aqpb:= a * q - p * b;
f7c8cb11a70e No self intersections, except for weirdness between first and last point
unc0rr
parents: 10201
diff changeset
   245
f7c8cb11a70e No self intersections, except for weirdness between first and last point
unc0rr
parents: 10201
diff changeset
   246
                if (aqpb <> 0) then
f7c8cb11a70e No self intersections, except for weirdness between first and last point
unc0rr
parents: 10201
diff changeset
   247
                begin
f7c8cb11a70e No self intersections, except for weirdness between first and last point
unc0rr
parents: 10201
diff changeset
   248
                    // (ix; iy) is intersection point
10494
0eb97cf4c78e Fix warnings given by 32-bit fpc
unC0Rr
parents: 10478
diff changeset
   249
                    iy:= (((Int64(p2.x) - mp.x) * b + Int64(mp.y) * a) * q - Int64(p2.y) * p * b) div aqpb;
10202
f7c8cb11a70e No self intersections, except for weirdness between first and last point
unc0rr
parents: 10201
diff changeset
   250
                    if abs(b) > abs(q) then
f7c8cb11a70e No self intersections, except for weirdness between first and last point
unc0rr
parents: 10201
diff changeset
   251
                        ix:= (iy - mp.y) * a div b + mp.x
f7c8cb11a70e No self intersections, except for weirdness between first and last point
unc0rr
parents: 10201
diff changeset
   252
                    else
f7c8cb11a70e No self intersections, except for weirdness between first and last point
unc0rr
parents: 10201
diff changeset
   253
                        ix:= (iy - p2.y) * p div q + p2.x;
f7c8cb11a70e No self intersections, except for weirdness between first and last point
unc0rr
parents: 10201
diff changeset
   254
f7c8cb11a70e No self intersections, except for weirdness between first and last point
unc0rr
parents: 10201
diff changeset
   255
                    d:= DistanceI(mp.y - iy, mp.x - ix).Round;
f7c8cb11a70e No self intersections, except for weirdness between first and last point
unc0rr
parents: 10201
diff changeset
   256
                    t2:= b * (mp.y - iy) + a * (mp.x - ix);
f7c8cb11a70e No self intersections, except for weirdness between first and last point
unc0rr
parents: 10201
diff changeset
   257
                    if t2 > 0 then distL:= min(d, distL) else distR:= min(d, distR);
f7c8cb11a70e No self intersections, except for weirdness between first and last point
unc0rr
parents: 10201
diff changeset
   258
                end;
f7c8cb11a70e No self intersections, except for weirdness between first and last point
unc0rr
parents: 10201
diff changeset
   259
            end;
f7c8cb11a70e No self intersections, except for weirdness between first and last point
unc0rr
parents: 10201
diff changeset
   260
        end;
f7c8cb11a70e No self intersections, except for weirdness between first and last point
unc0rr
parents: 10201
diff changeset
   261
f7c8cb11a70e No self intersections, except for weirdness between first and last point
unc0rr
parents: 10201
diff changeset
   262
    // don't move new point for more than length of initial segment
10207
9dd3a44805a1 - Make sure distortion doesn't move fill point to other side of segment, this prevents corrupted maps
unc0rr
parents: 10206
diff changeset
   263
    // adjust/parametrize for more flat surfaces (try values 3/4, 1/2 of dab, or even 1/4)
10499
0d8016085108 modestly randomise dab, bump points again.
nemo
parents: 10495
diff changeset
   264
    d:= dab * 100 div dabDiv;
10495
6d61b44a5652 - Comment out getrandom in addgear for hedgehog, causes preview-game desync
unc0rr
parents: 10494
diff changeset
   265
    //d:= dab * (1 + abs(cFeatureSize - 8)) div 6;
6d61b44a5652 - Comment out getrandom in addgear for hedgehog, causes preview-game desync
unc0rr
parents: 10494
diff changeset
   266
    //d:= dab * (14 + cFeatureSize) div 20;
10201
9bee9541edf1 Fix detection of intersections, still need to check if passing any point in move, but result is already okayish
unc0rr
parents: 10200
diff changeset
   267
    if distL > d then distL:= d;
9bee9541edf1 Fix detection of intersections, still need to check if passing any point in move, but result is already okayish
unc0rr
parents: 10200
diff changeset
   268
    if distR > d then distR:= d;
9bee9541edf1 Fix detection of intersections, still need to check if passing any point in move, but result is already okayish
unc0rr
parents: 10200
diff changeset
   269
10204
50e52e511300 Fix div by zero error in new generator
unc0rr
parents: 10203
diff changeset
   270
    if distR + distL < minDistance * 2 + 10 then
10200
edc2fe0ca03f More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents: 10199
diff changeset
   271
    begin
10225
0278759252b6 No more occasional long straight edges
unc0rr
parents: 10209
diff changeset
   272
        // limits are too narrow, just divide
0278759252b6 No more occasional long straight edges
unc0rr
parents: 10209
diff changeset
   273
        newPoint.x:= mp.x;
0278759252b6 No more occasional long straight edges
unc0rr
parents: 10209
diff changeset
   274
        newPoint.y:= mp.y;
10200
edc2fe0ca03f More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents: 10199
diff changeset
   275
    end
edc2fe0ca03f More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents: 10199
diff changeset
   276
    else
edc2fe0ca03f More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents: 10199
diff changeset
   277
    begin
edc2fe0ca03f More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents: 10199
diff changeset
   278
        // select distance within [-distL; distR]
10209
76316652ef26 Fix warnings
unc0rr
parents: 10208
diff changeset
   279
        d:= -distL + minDistance + LongInt(GetRandom(distR + distL - minDistance * 2));
10201
9bee9541edf1 Fix detection of intersections, still need to check if passing any point in move, but result is already okayish
unc0rr
parents: 10200
diff changeset
   280
        //d:= distR - minDistance;
10202
f7c8cb11a70e No self intersections, except for weirdness between first and last point
unc0rr
parents: 10201
diff changeset
   281
        //d:= - distL + minDistance;
10200
edc2fe0ca03f More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents: 10199
diff changeset
   282
edc2fe0ca03f More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents: 10199
diff changeset
   283
        // calculate new point
edc2fe0ca03f More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents: 10199
diff changeset
   284
        newPoint.x:= mp.x + a * d div dab;
edc2fe0ca03f More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents: 10199
diff changeset
   285
        newPoint.y:= mp.y + b * d div dab;
edc2fe0ca03f More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents: 10199
diff changeset
   286
    end;
10199
fdb689b57b1b Some progress on new generator
unc0rr
parents: 10198
diff changeset
   287
end;
fdb689b57b1b Some progress on new generator
unc0rr
parents: 10198
diff changeset
   288
10209
76316652ef26 Fix warnings
unc0rr
parents: 10208
diff changeset
   289
procedure DivideEdges(fillPointsCount: LongWord; var pa: TPixAr);
10202
f7c8cb11a70e No self intersections, except for weirdness between first and last point
unc0rr
parents: 10201
diff changeset
   290
var i, t: LongInt;
10200
edc2fe0ca03f More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents: 10199
diff changeset
   291
    newPoint: TPoint;
10199
fdb689b57b1b Some progress on new generator
unc0rr
parents: 10198
diff changeset
   292
begin
10209
76316652ef26 Fix warnings
unc0rr
parents: 10208
diff changeset
   293
    newPoint.x:= 0;
76316652ef26 Fix warnings
unc0rr
parents: 10208
diff changeset
   294
    newPoint.y:= 0;
10199
fdb689b57b1b Some progress on new generator
unc0rr
parents: 10198
diff changeset
   295
    i:= 0;
10202
f7c8cb11a70e No self intersections, except for weirdness between first and last point
unc0rr
parents: 10201
diff changeset
   296
10564
0cb20aa8877a more fixing and allow pas2c to run tests. they will still fail though - engine does not exit with the specified exit codes, also data types are messed up
sheepluva
parents: 10562
diff changeset
   297
    while i < LongInt(pa.Count) - 1 do
10199
fdb689b57b1b Some progress on new generator
unc0rr
parents: 10198
diff changeset
   298
    begin
10207
9dd3a44805a1 - Make sure distortion doesn't move fill point to other side of segment, this prevents corrupted maps
unc0rr
parents: 10206
diff changeset
   299
        FindPoint(i, fillPointsCount, newPoint, pa);
9dd3a44805a1 - Make sure distortion doesn't move fill point to other side of segment, this prevents corrupted maps
unc0rr
parents: 10206
diff changeset
   300
10202
f7c8cb11a70e No self intersections, except for weirdness between first and last point
unc0rr
parents: 10201
diff changeset
   301
        if (newPoint.x <> pa.ar[i].x) or (newPoint.y <> pa.ar[i].y) then
10199
fdb689b57b1b Some progress on new generator
unc0rr
parents: 10198
diff changeset
   302
        begin
10207
9dd3a44805a1 - Make sure distortion doesn't move fill point to other side of segment, this prevents corrupted maps
unc0rr
parents: 10206
diff changeset
   303
            // point found, free a slot for it in array, don't forget to move appended fill points
9dd3a44805a1 - Make sure distortion doesn't move fill point to other side of segment, this prevents corrupted maps
unc0rr
parents: 10206
diff changeset
   304
            for t:= pa.Count + fillPointsCount downto i + 2 do
10202
f7c8cb11a70e No self intersections, except for weirdness between first and last point
unc0rr
parents: 10201
diff changeset
   305
                pa.ar[t]:= pa.ar[t - 1];
f7c8cb11a70e No self intersections, except for weirdness between first and last point
unc0rr
parents: 10201
diff changeset
   306
            inc(pa.Count);
f7c8cb11a70e No self intersections, except for weirdness between first and last point
unc0rr
parents: 10201
diff changeset
   307
            pa.ar[i + 1]:= newPoint;
f7c8cb11a70e No self intersections, except for weirdness between first and last point
unc0rr
parents: 10201
diff changeset
   308
            inc(i)
10199
fdb689b57b1b Some progress on new generator
unc0rr
parents: 10198
diff changeset
   309
        end;
fdb689b57b1b Some progress on new generator
unc0rr
parents: 10198
diff changeset
   310
        inc(i)
fdb689b57b1b Some progress on new generator
unc0rr
parents: 10198
diff changeset
   311
    end;
fdb689b57b1b Some progress on new generator
unc0rr
parents: 10198
diff changeset
   312
end;
fdb689b57b1b Some progress on new generator
unc0rr
parents: 10198
diff changeset
   313
10207
9dd3a44805a1 - Make sure distortion doesn't move fill point to other side of segment, this prevents corrupted maps
unc0rr
parents: 10206
diff changeset
   314
procedure Distort2(var Template: TEdgeTemplate; fps: PPointArray; var pa: TPixAr);
10201
9bee9541edf1 Fix detection of intersections, still need to check if passing any point in move, but result is already okayish
unc0rr
parents: 10200
diff changeset
   315
var i: Longword;
10199
fdb689b57b1b Some progress on new generator
unc0rr
parents: 10198
diff changeset
   316
begin
10207
9dd3a44805a1 - Make sure distortion doesn't move fill point to other side of segment, this prevents corrupted maps
unc0rr
parents: 10206
diff changeset
   317
    // append fill points to ensure distortion won't move them to other side of segment
9dd3a44805a1 - Make sure distortion doesn't move fill point to other side of segment, this prevents corrupted maps
unc0rr
parents: 10206
diff changeset
   318
    for i:= 0 to pred(Template.FillPointsCount) do
9dd3a44805a1 - Make sure distortion doesn't move fill point to other side of segment, this prevents corrupted maps
unc0rr
parents: 10206
diff changeset
   319
        begin
9dd3a44805a1 - Make sure distortion doesn't move fill point to other side of segment, this prevents corrupted maps
unc0rr
parents: 10206
diff changeset
   320
            pa.ar[pa.Count + i].x:= fps^[i].x;
9dd3a44805a1 - Make sure distortion doesn't move fill point to other side of segment, this prevents corrupted maps
unc0rr
parents: 10206
diff changeset
   321
            pa.ar[pa.Count + i].y:= fps^[i].y;
9dd3a44805a1 - Make sure distortion doesn't move fill point to other side of segment, this prevents corrupted maps
unc0rr
parents: 10206
diff changeset
   322
        end;
9dd3a44805a1 - Make sure distortion doesn't move fill point to other side of segment, this prevents corrupted maps
unc0rr
parents: 10206
diff changeset
   323
9dd3a44805a1 - Make sure distortion doesn't move fill point to other side of segment, this prevents corrupted maps
unc0rr
parents: 10206
diff changeset
   324
    // divide while it divides
10203
adeab6c21fe5 Greedy approach: divide while it divides
unc0rr
parents: 10202
diff changeset
   325
    repeat
adeab6c21fe5 Greedy approach: divide while it divides
unc0rr
parents: 10202
diff changeset
   326
        i:= pa.Count;
10207
9dd3a44805a1 - Make sure distortion doesn't move fill point to other side of segment, this prevents corrupted maps
unc0rr
parents: 10206
diff changeset
   327
        DivideEdges(Template.FillPointsCount, pa)
10203
adeab6c21fe5 Greedy approach: divide while it divides
unc0rr
parents: 10202
diff changeset
   328
    until i = pa.Count;
10201
9bee9541edf1 Fix detection of intersections, still need to check if passing any point in move, but result is already okayish
unc0rr
parents: 10200
diff changeset
   329
11179
e1a098f950a9 - Attempt to fix a crash while selecting 'Random' for map generation
antonc27 <antonc27@mail.ru>
parents: 10564
diff changeset
   330
{$IFDEF IPHONEOS}
e1a098f950a9 - Attempt to fix a crash while selecting 'Random' for map generation
antonc27 <antonc27@mail.ru>
parents: 10564
diff changeset
   331
    if GameType <> gmtLandPreview then
e1a098f950a9 - Attempt to fix a crash while selecting 'Random' for map generation
antonc27 <antonc27@mail.ru>
parents: 10564
diff changeset
   332
{$ENDIF}
10207
9dd3a44805a1 - Make sure distortion doesn't move fill point to other side of segment, this prevents corrupted maps
unc0rr
parents: 10206
diff changeset
   333
    // make it smooth
9dd3a44805a1 - Make sure distortion doesn't move fill point to other side of segment, this prevents corrupted maps
unc0rr
parents: 10206
diff changeset
   334
    BezierizeEdge(pa, _0_2);
10199
fdb689b57b1b Some progress on new generator
unc0rr
parents: 10198
diff changeset
   335
end;
fdb689b57b1b Some progress on new generator
unc0rr
parents: 10198
diff changeset
   336
fdb689b57b1b Some progress on new generator
unc0rr
parents: 10198
diff changeset
   337
10198
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
   338
procedure GenTemplated(var Template: TEdgeTemplate);
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
   339
var pa: TPixAr;
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
   340
    i: Longword;
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
   341
    y, x: Longword;
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
   342
    fps: TPointArray;
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
   343
begin
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
   344
    fps:=Template.FillPoints^;
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
   345
    ResizeLand(Template.TemplateWidth, Template.TemplateHeight);
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
   346
    for y:= 0 to LAND_HEIGHT - 1 do
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
   347
        for x:= 0 to LAND_WIDTH - 1 do
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
   348
            Land[y, x]:= lfBasic;
10510
9329dab04490 some whitespace fixes
sheepluva
parents: 10502
diff changeset
   349
10495
6d61b44a5652 - Comment out getrandom in addgear for hedgehog, causes preview-game desync
unc0rr
parents: 10494
diff changeset
   350
    minDistance:= sqr(cFeatureSize) div 8 + 10;
10499
0d8016085108 modestly randomise dab, bump points again.
nemo
parents: 10495
diff changeset
   351
    //dabDiv:= getRandom(41)+60;
0d8016085108 modestly randomise dab, bump points again.
nemo
parents: 10495
diff changeset
   352
    //dabDiv:= getRandom(31)+70;
0d8016085108 modestly randomise dab, bump points again.
nemo
parents: 10495
diff changeset
   353
    dabDiv:= getRandom(21)+100;
10208
f04fdb35fc33 - Limit outline to leftX/rightX/topY instead of LAND_WIDTH/LAND_HEIGHT
unc0rr
parents: 10207
diff changeset
   354
    MaxHedgehogs:= Template.MaxHedgehogs;
f04fdb35fc33 - Limit outline to leftX/rightX/topY instead of LAND_WIDTH/LAND_HEIGHT
unc0rr
parents: 10207
diff changeset
   355
    hasGirders:= Template.hasGirders;
f04fdb35fc33 - Limit outline to leftX/rightX/topY instead of LAND_WIDTH/LAND_HEIGHT
unc0rr
parents: 10207
diff changeset
   356
    playHeight:= Template.TemplateHeight;
f04fdb35fc33 - Limit outline to leftX/rightX/topY instead of LAND_WIDTH/LAND_HEIGHT
unc0rr
parents: 10207
diff changeset
   357
    playWidth:= Template.TemplateWidth;
f04fdb35fc33 - Limit outline to leftX/rightX/topY instead of LAND_WIDTH/LAND_HEIGHT
unc0rr
parents: 10207
diff changeset
   358
    leftX:= (LAND_WIDTH - playWidth) div 2;
10209
76316652ef26 Fix warnings
unc0rr
parents: 10208
diff changeset
   359
    rightX:= Pred(leftX + playWidth);
10208
f04fdb35fc33 - Limit outline to leftX/rightX/topY instead of LAND_WIDTH/LAND_HEIGHT
unc0rr
parents: 10207
diff changeset
   360
    topY:= LAND_HEIGHT - playHeight;
10510
9329dab04490 some whitespace fixes
sheepluva
parents: 10502
diff changeset
   361
10198
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
   362
    {$HINTS OFF}
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
   363
    SetPoints(Template, pa, @fps);
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
   364
    {$HINTS ON}
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
   365
10207
9dd3a44805a1 - Make sure distortion doesn't move fill point to other side of segment, this prevents corrupted maps
unc0rr
parents: 10206
diff changeset
   366
    Distort2(Template, @fps, pa);
10198
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
   367
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
   368
    DrawEdge(pa, 0);
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
   369
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
   370
    with Template do
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
   371
        for i:= 0 to pred(FillPointsCount) do
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
   372
            with fps[i] do
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
   373
                FillLand(x, y, 0, 0);
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
   374
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
   375
    DrawEdge(pa, lfBasic);
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
   376
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
   377
    // HACK: force to only cavern even if a cavern map is invertable if cTemplateFilter = 4 ?
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
   378
    if (cTemplateFilter = 4)
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
   379
    or (Template.canInvert and (getrandom(2) = 0))
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
   380
    or (not Template.canInvert and Template.isNegative) then
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
   381
        begin
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
   382
        hasBorder:= true;
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
   383
        for y:= 0 to LAND_HEIGHT - 1 do
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
   384
            for x:= 0 to LAND_WIDTH - 1 do
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
   385
                if (y < topY) or (x < leftX) or (x > rightX) then
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
   386
                    Land[y, x]:= 0
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
   387
                else
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
   388
                    begin
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
   389
                    if Land[y, x] = 0 then
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
   390
                        Land[y, x]:= lfBasic
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
   391
                    else if Land[y, x] = lfBasic then
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
   392
                        Land[y, x]:= 0;
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
   393
                    end;
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
   394
        end;
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
   395
end;
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
   396
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
   397
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
   398
end.