hedgewars/uLandGenTemplateBased.pas
author unc0rr
Mon, 17 Mar 2014 00:41:45 +0400
changeset 10200 edc2fe0ca03f
parent 10199 fdb689b57b1b
child 10201 9bee9541edf1
permissions -rw-r--r--
More math, implementation is nearly complete, just still have an issue to resolve
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
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
     4
uses uLandTemplates;
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);
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
     7
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
     8
implementation
10200
edc2fe0ca03f More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents: 10199
diff changeset
     9
uses uVariables, uConsts, uFloat, uLandOutline, uLandUtils, uRandom, SDLh, math;
10198
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    10
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    11
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    12
procedure SetPoints(var Template: TEdgeTemplate; var pa: TPixAr; fps: PPointArray);
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    13
var i: LongInt;
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    14
begin
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    15
    with Template do
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    16
        begin
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    17
        pa.Count:= BasePointsCount;
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    18
        for i:= 0 to pred(pa.Count) 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.ar[i].x:= BasePoints^[i].x + LongInt(GetRandom(BasePoints^[i].w));
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    21
            if pa.ar[i].x <> NTPX then
10199
fdb689b57b1b Some progress on new generator
unc0rr
parents: 10198
diff changeset
    22
                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
    23
            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
    24
            end;
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    25
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    26
        if canMirror then
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    27
            if getrandom(2) = 0 then
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    28
                begin
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    29
                for i:= 0 to pred(BasePointsCount) do
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    30
                if pa.ar[i].x <> NTPX then
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    31
                    pa.ar[i].x:= LAND_WIDTH - 1 - pa.ar[i].x;
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    32
                for i:= 0 to pred(FillPointsCount) do
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    33
                    fps^[i].x:= LAND_WIDTH - 1 - fps^[i].x;
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    34
                end;
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    35
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    36
(*  Experiment in making this option more useful
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    37
     if ((not isNegative) and (cTemplateFilter = 4)) or
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    38
        (canFlip and (getrandom(2) = 0)) then
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    39
           begin
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    40
           for i:= 0 to pred(BasePointsCount) do
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    41
               begin
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    42
               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
    43
               if pa.ar[i].y > LAND_HEIGHT - 1 then
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    44
                   pa.ar[i].y:= LAND_HEIGHT - 1;
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    45
               end;
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    46
           for i:= 0 to pred(FillPointsCount) do
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    47
               begin
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    48
               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
    49
               if FillPoints^[i].y > LAND_HEIGHT - 1 then
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    50
                   FillPoints^[i].y:= LAND_HEIGHT - 1;
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    51
               end;
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    52
           end;
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    53
     end
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    54
*)
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    55
// template recycling.  Pull these off the floor a bit
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    56
    if (not isNegative) and (cTemplateFilter = 4) then
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    57
        begin
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    58
        for i:= 0 to pred(BasePointsCount) do
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    59
            begin
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    60
            dec(pa.ar[i].y, 100);
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    61
            if pa.ar[i].y < 0 then
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    62
                pa.ar[i].y:= 0;
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    63
            end;
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    64
        for i:= 0 to pred(FillPointsCount) do
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    65
            begin
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    66
            dec(fps^[i].y, 100);
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    67
            if fps^[i].y < 0 then
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    68
                fps^[i].y:= 0;
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    69
            end;
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    70
        end;
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    71
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    72
    if (canFlip and (getrandom(2) = 0)) then
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    73
        begin
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    74
        for i:= 0 to pred(BasePointsCount) do
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    75
            pa.ar[i].y:= LAND_HEIGHT - 1 - pa.ar[i].y;
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    76
        for i:= 0 to pred(FillPointsCount) do
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    77
            fps^[i].y:= LAND_HEIGHT - 1 - fps^[i].y;
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    78
        end;
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    79
    end
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    80
end;
10199
fdb689b57b1b Some progress on new generator
unc0rr
parents: 10198
diff changeset
    81
fdb689b57b1b Some progress on new generator
unc0rr
parents: 10198
diff changeset
    82
fdb689b57b1b Some progress on new generator
unc0rr
parents: 10198
diff changeset
    83
procedure Distort1(var Template: TEdgeTemplate; var pa: TPixAr);
fdb689b57b1b Some progress on new generator
unc0rr
parents: 10198
diff changeset
    84
var i: Longword;
fdb689b57b1b Some progress on new generator
unc0rr
parents: 10198
diff changeset
    85
begin
fdb689b57b1b Some progress on new generator
unc0rr
parents: 10198
diff changeset
    86
    for i:= 1 to Template.BezierizeCount do
fdb689b57b1b Some progress on new generator
unc0rr
parents: 10198
diff changeset
    87
        begin
fdb689b57b1b Some progress on new generator
unc0rr
parents: 10198
diff changeset
    88
        BezierizeEdge(pa, _0_5);
fdb689b57b1b Some progress on new generator
unc0rr
parents: 10198
diff changeset
    89
        RandomizePoints(pa);
fdb689b57b1b Some progress on new generator
unc0rr
parents: 10198
diff changeset
    90
        RandomizePoints(pa)
fdb689b57b1b Some progress on new generator
unc0rr
parents: 10198
diff changeset
    91
        end;
fdb689b57b1b Some progress on new generator
unc0rr
parents: 10198
diff changeset
    92
    for i:= 1 to Template.RandPassesCount do
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
    BezierizeEdge(pa, _0_1);
fdb689b57b1b Some progress on new generator
unc0rr
parents: 10198
diff changeset
    95
end;
fdb689b57b1b Some progress on new generator
unc0rr
parents: 10198
diff changeset
    96
fdb689b57b1b Some progress on new generator
unc0rr
parents: 10198
diff changeset
    97
10200
edc2fe0ca03f More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents: 10199
diff changeset
    98
procedure FindPoint(si: LongInt; var newPoint: TPoint; var pa: TPixAr);
edc2fe0ca03f More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents: 10199
diff changeset
    99
const mapBorderMargin = 0;
10199
fdb689b57b1b Some progress on new generator
unc0rr
parents: 10198
diff changeset
   100
var p1, p2, mp, ap: TPoint;
fdb689b57b1b Some progress on new generator
unc0rr
parents: 10198
diff changeset
   101
    i, t1, t2, a, b, p, q, iy, ix, aqpb: LongInt;
10200
edc2fe0ca03f More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents: 10199
diff changeset
   102
    dab, d, distL, distR: LongInt;
10199
fdb689b57b1b Some progress on new generator
unc0rr
parents: 10198
diff changeset
   103
begin
fdb689b57b1b Some progress on new generator
unc0rr
parents: 10198
diff changeset
   104
    // [p1, p2] is segment we're trying to divide
fdb689b57b1b Some progress on new generator
unc0rr
parents: 10198
diff changeset
   105
    p1:= pa.ar[si];
fdb689b57b1b Some progress on new generator
unc0rr
parents: 10198
diff changeset
   106
    p2:= pa.ar[si + 1];
fdb689b57b1b Some progress on new generator
unc0rr
parents: 10198
diff changeset
   107
10200
edc2fe0ca03f More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents: 10199
diff changeset
   108
    if (p1.x = NTPX) or (p2.x = NTPX) then
edc2fe0ca03f More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents: 10199
diff changeset
   109
    begin
edc2fe0ca03f More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents: 10199
diff changeset
   110
        newPoint:= p1;
edc2fe0ca03f More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents: 10199
diff changeset
   111
        exit;
edc2fe0ca03f More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents: 10199
diff changeset
   112
    end;
edc2fe0ca03f More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents: 10199
diff changeset
   113
10199
fdb689b57b1b Some progress on new generator
unc0rr
parents: 10198
diff changeset
   114
    // its middle point
fdb689b57b1b Some progress on new generator
unc0rr
parents: 10198
diff changeset
   115
    mp.x:= (p1.x + p2.x) div 2;
fdb689b57b1b Some progress on new generator
unc0rr
parents: 10198
diff changeset
   116
    mp.y:= (p1.y + p2.y) div 2;
fdb689b57b1b Some progress on new generator
unc0rr
parents: 10198
diff changeset
   117
    // another point on the perpendicular bisector
fdb689b57b1b Some progress on new generator
unc0rr
parents: 10198
diff changeset
   118
    ap.x:= mp.x + p2.y - p1.y;
fdb689b57b1b Some progress on new generator
unc0rr
parents: 10198
diff changeset
   119
    ap.y:= mp.y + p1.x - p2.x;
10200
edc2fe0ca03f More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents: 10199
diff changeset
   120
    // vector between these points
edc2fe0ca03f More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents: 10199
diff changeset
   121
    a:= p2.y - p1.y;
edc2fe0ca03f More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents: 10199
diff changeset
   122
    b:= p1.x - p2.x;
10199
fdb689b57b1b Some progress on new generator
unc0rr
parents: 10198
diff changeset
   123
10200
edc2fe0ca03f More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents: 10199
diff changeset
   124
    // find distances to map borders
edc2fe0ca03f More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents: 10199
diff changeset
   125
    if a <> 0 then
edc2fe0ca03f More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents: 10199
diff changeset
   126
    begin
edc2fe0ca03f More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents: 10199
diff changeset
   127
        // left border
edc2fe0ca03f More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents: 10199
diff changeset
   128
        iy:= (mapBorderMargin - mp.x) * b div a + mp.y;
edc2fe0ca03f More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents: 10199
diff changeset
   129
        d:= DistanceI(mp.x - mapBorderMargin, mp.y - iy).Round;
edc2fe0ca03f More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents: 10199
diff changeset
   130
        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
   131
        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
   132
                    writeln('====== Left border: ', mapBorderMargin, '; ', mp.y - iy, ', distance = ', d);
edc2fe0ca03f More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents: 10199
diff changeset
   133
                    writeln(a, ' ', -b);
edc2fe0ca03f More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents: 10199
diff changeset
   134
                    writeln(t1);
edc2fe0ca03f More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents: 10199
diff changeset
   135
                    writeln(mp.x - mapBorderMargin, ' ', mp.y - iy);
edc2fe0ca03f More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents: 10199
diff changeset
   136
                    writeln('MP: ', mp.x, ' ', mp.y);
edc2fe0ca03f More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents: 10199
diff changeset
   137
                    writeln('L: ', distL, '; R: ', distR);
edc2fe0ca03f More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents: 10199
diff changeset
   138
edc2fe0ca03f More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents: 10199
diff changeset
   139
        // right border
edc2fe0ca03f More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents: 10199
diff changeset
   140
        iy:= (LAND_WIDTH - mapBorderMargin - mp.x) * b div a + mp.y;
edc2fe0ca03f More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents: 10199
diff changeset
   141
        d:= DistanceI(mp.x - LAND_WIDTH + mapBorderMargin, mp.y - iy).Round;
edc2fe0ca03f More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents: 10199
diff changeset
   142
        if t1 > 0 then distR:= d else distL:= d;
edc2fe0ca03f More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents: 10199
diff changeset
   143
    end;
edc2fe0ca03f More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents: 10199
diff changeset
   144
edc2fe0ca03f More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents: 10199
diff changeset
   145
    if b <> 0 then
edc2fe0ca03f More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents: 10199
diff changeset
   146
    begin
edc2fe0ca03f More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents: 10199
diff changeset
   147
        // top border
edc2fe0ca03f More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents: 10199
diff changeset
   148
        ix:= (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
   149
        d:= DistanceI(mp.y - mapBorderMargin, mp.x - ix).Round;
edc2fe0ca03f More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents: 10199
diff changeset
   150
        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
   151
        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
   152
edc2fe0ca03f More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents: 10199
diff changeset
   153
        // bottom border
edc2fe0ca03f More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents: 10199
diff changeset
   154
        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
   155
        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
   156
        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
   157
                    writeln('====== Bottom border: ', ix, '; ', LAND_HEIGHT - mapBorderMargin, ', distance = ', d);
edc2fe0ca03f More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents: 10199
diff changeset
   158
                    writeln(a, ' ', -b);
edc2fe0ca03f More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents: 10199
diff changeset
   159
                    writeln(t2);
edc2fe0ca03f More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents: 10199
diff changeset
   160
                    writeln(mp.x - ix, ' ', mp.y - LAND_HEIGHT + mapBorderMargin);
edc2fe0ca03f More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents: 10199
diff changeset
   161
                    writeln('L: ', distL, '; R: ', distR);
edc2fe0ca03f More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents: 10199
diff changeset
   162
    end;
edc2fe0ca03f More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents: 10199
diff changeset
   163
edc2fe0ca03f More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents: 10199
diff changeset
   164
    // now go through all other segments
edc2fe0ca03f More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents: 10199
diff changeset
   165
    for i:= 0 to pa.Count - 2 do
edc2fe0ca03f More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents: 10199
diff changeset
   166
        if (i <> si) and (pa.ar[i].x <> NTPX) and (pa.ar[i + 1].x <> NTPX) then
10199
fdb689b57b1b Some progress on new generator
unc0rr
parents: 10198
diff changeset
   167
        begin
fdb689b57b1b Some progress on new generator
unc0rr
parents: 10198
diff changeset
   168
            // check if it intersects
10200
edc2fe0ca03f More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents: 10199
diff changeset
   169
            t1:= (mp.x - pa.ar[i].x) * b - a * (mp.y - pa.ar[i].y);
edc2fe0ca03f More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents: 10199
diff changeset
   170
            t2:= (mp.x - pa.ar[i + 1].x) * b - a * (mp.y - pa.ar[i + 1].y);
10199
fdb689b57b1b Some progress on new generator
unc0rr
parents: 10198
diff changeset
   171
fdb689b57b1b Some progress on new generator
unc0rr
parents: 10198
diff changeset
   172
            if (t1 > 0) <> (t2 > 0) then // yes it does, hard arith follows
fdb689b57b1b Some progress on new generator
unc0rr
parents: 10198
diff changeset
   173
            begin
fdb689b57b1b Some progress on new generator
unc0rr
parents: 10198
diff changeset
   174
                p:= pa.ar[i + 1].x - pa.ar[i].x;
fdb689b57b1b Some progress on new generator
unc0rr
parents: 10198
diff changeset
   175
                q:= pa.ar[i + 1].y - pa.ar[i].y;
fdb689b57b1b Some progress on new generator
unc0rr
parents: 10198
diff changeset
   176
                aqpb:= a * q - p * b;
fdb689b57b1b Some progress on new generator
unc0rr
parents: 10198
diff changeset
   177
fdb689b57b1b Some progress on new generator
unc0rr
parents: 10198
diff changeset
   178
                if (aqpb <> 0) then
fdb689b57b1b Some progress on new generator
unc0rr
parents: 10198
diff changeset
   179
                begin
fdb689b57b1b Some progress on new generator
unc0rr
parents: 10198
diff changeset
   180
                    // (ix; iy) is intersection point
fdb689b57b1b Some progress on new generator
unc0rr
parents: 10198
diff changeset
   181
                    iy:= (((pa.ar[i].x - mp.x) * b + mp.y * a) * q - pa.ar[i].y * p * b);
fdb689b57b1b Some progress on new generator
unc0rr
parents: 10198
diff changeset
   182
                    if b <> 0 then
10200
edc2fe0ca03f More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents: 10199
diff changeset
   183
                        ix:= (iy - mp.y * aqpb) * a div b div aqpb + mp.x
10199
fdb689b57b1b Some progress on new generator
unc0rr
parents: 10198
diff changeset
   184
                    else
fdb689b57b1b Some progress on new generator
unc0rr
parents: 10198
diff changeset
   185
                        ix:= (iy - pa.ar[i].y * aqpb) * p div q div aqpb + pa.ar[i].x;
fdb689b57b1b Some progress on new generator
unc0rr
parents: 10198
diff changeset
   186
                    iy:= iy div aqpb;
fdb689b57b1b Some progress on new generator
unc0rr
parents: 10198
diff changeset
   187
10200
edc2fe0ca03f More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents: 10199
diff changeset
   188
                    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
   189
                    writeln('====== Intersection: ', ix, '; ', iy, ', distance = ', d);
edc2fe0ca03f More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents: 10199
diff changeset
   190
                    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
   191
                    writeln(a, ' ', -b);
edc2fe0ca03f More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents: 10199
diff changeset
   192
                    writeln(t1);
edc2fe0ca03f More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents: 10199
diff changeset
   193
                    writeln(mp.y - iy, ' ', mp.x - ix);
edc2fe0ca03f More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents: 10199
diff changeset
   194
                    if t1 > 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
   195
                    writeln('L: ', distL, '; R: ', distR);
10199
fdb689b57b1b Some progress on new generator
unc0rr
parents: 10198
diff changeset
   196
                end;
fdb689b57b1b Some progress on new generator
unc0rr
parents: 10198
diff changeset
   197
            end;
fdb689b57b1b Some progress on new generator
unc0rr
parents: 10198
diff changeset
   198
        end;
10200
edc2fe0ca03f More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents: 10199
diff changeset
   199
edc2fe0ca03f More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents: 10199
diff changeset
   200
    if distR + distL < 40 then
edc2fe0ca03f More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents: 10199
diff changeset
   201
    begin
edc2fe0ca03f More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents: 10199
diff changeset
   202
        // limits are too narrow, leave point alone
edc2fe0ca03f More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents: 10199
diff changeset
   203
        newPoint:= p1
edc2fe0ca03f More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents: 10199
diff changeset
   204
    end
edc2fe0ca03f More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents: 10199
diff changeset
   205
    else
edc2fe0ca03f More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents: 10199
diff changeset
   206
    begin
edc2fe0ca03f More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents: 10199
diff changeset
   207
        // select distance within [-distL; distR]
edc2fe0ca03f More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents: 10199
diff changeset
   208
        d:= -distL;
edc2fe0ca03f More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents: 10199
diff changeset
   209
        //d:= distR;
edc2fe0ca03f More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents: 10199
diff changeset
   210
edc2fe0ca03f More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents: 10199
diff changeset
   211
        // calculate new point
edc2fe0ca03f More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents: 10199
diff changeset
   212
        dab:= DistanceI(a, b).Round;
edc2fe0ca03f More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents: 10199
diff changeset
   213
edc2fe0ca03f More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents: 10199
diff changeset
   214
        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
   215
        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
   216
edc2fe0ca03f More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents: 10199
diff changeset
   217
        writeln('Middle Point ', mp.x, '; ', mp.y);
edc2fe0ca03f More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents: 10199
diff changeset
   218
        writeln('New Point ', newPoint.x, '; ', newPoint.y);
edc2fe0ca03f More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents: 10199
diff changeset
   219
    end;
10199
fdb689b57b1b Some progress on new generator
unc0rr
parents: 10198
diff changeset
   220
end;
fdb689b57b1b Some progress on new generator
unc0rr
parents: 10198
diff changeset
   221
fdb689b57b1b Some progress on new generator
unc0rr
parents: 10198
diff changeset
   222
procedure DivideEdges(var pa: TPixAr);
fdb689b57b1b Some progress on new generator
unc0rr
parents: 10198
diff changeset
   223
var npa: TPixAr;
fdb689b57b1b Some progress on new generator
unc0rr
parents: 10198
diff changeset
   224
    i: LongInt;
10200
edc2fe0ca03f More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents: 10199
diff changeset
   225
    newPoint: TPoint;
10199
fdb689b57b1b Some progress on new generator
unc0rr
parents: 10198
diff changeset
   226
begin
fdb689b57b1b Some progress on new generator
unc0rr
parents: 10198
diff changeset
   227
    i:= 0;
fdb689b57b1b Some progress on new generator
unc0rr
parents: 10198
diff changeset
   228
    npa.Count:= 0;
fdb689b57b1b Some progress on new generator
unc0rr
parents: 10198
diff changeset
   229
    while i < pa.Count do
fdb689b57b1b Some progress on new generator
unc0rr
parents: 10198
diff changeset
   230
    begin
10200
edc2fe0ca03f More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents: 10199
diff changeset
   231
        npa.ar[npa.Count]:= pa.ar[i];
edc2fe0ca03f More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents: 10199
diff changeset
   232
        inc(npa.Count);
edc2fe0ca03f More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents: 10199
diff changeset
   233
edc2fe0ca03f More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents: 10199
diff changeset
   234
        if i < 1 then
10199
fdb689b57b1b Some progress on new generator
unc0rr
parents: 10198
diff changeset
   235
        begin
10200
edc2fe0ca03f More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents: 10199
diff changeset
   236
            FindPoint(i, newPoint, pa);
edc2fe0ca03f More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents: 10199
diff changeset
   237
            if (newPoint.x <> pa.ar[i].x) or (newPoint.y <> pa.ar[i].y) then
edc2fe0ca03f More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents: 10199
diff changeset
   238
            begin
edc2fe0ca03f More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents: 10199
diff changeset
   239
            npa.ar[npa.Count]:= newPoint;
10199
fdb689b57b1b Some progress on new generator
unc0rr
parents: 10198
diff changeset
   240
            inc(npa.Count)
10200
edc2fe0ca03f More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents: 10199
diff changeset
   241
            end;
10199
fdb689b57b1b Some progress on new generator
unc0rr
parents: 10198
diff changeset
   242
        end;
fdb689b57b1b Some progress on new generator
unc0rr
parents: 10198
diff changeset
   243
fdb689b57b1b Some progress on new generator
unc0rr
parents: 10198
diff changeset
   244
        inc(i)
fdb689b57b1b Some progress on new generator
unc0rr
parents: 10198
diff changeset
   245
    end;
fdb689b57b1b Some progress on new generator
unc0rr
parents: 10198
diff changeset
   246
fdb689b57b1b Some progress on new generator
unc0rr
parents: 10198
diff changeset
   247
    pa:= npa;
fdb689b57b1b Some progress on new generator
unc0rr
parents: 10198
diff changeset
   248
end;
fdb689b57b1b Some progress on new generator
unc0rr
parents: 10198
diff changeset
   249
fdb689b57b1b Some progress on new generator
unc0rr
parents: 10198
diff changeset
   250
procedure Distort2(var Template: TEdgeTemplate; var pa: TPixAr);
fdb689b57b1b Some progress on new generator
unc0rr
parents: 10198
diff changeset
   251
//var i: Longword;
fdb689b57b1b Some progress on new generator
unc0rr
parents: 10198
diff changeset
   252
begin
fdb689b57b1b Some progress on new generator
unc0rr
parents: 10198
diff changeset
   253
    DivideEdges(pa);
fdb689b57b1b Some progress on new generator
unc0rr
parents: 10198
diff changeset
   254
    {for i:= 1 to Template.BezierizeCount do
fdb689b57b1b Some progress on new generator
unc0rr
parents: 10198
diff changeset
   255
        begin
fdb689b57b1b Some progress on new generator
unc0rr
parents: 10198
diff changeset
   256
        BezierizeEdge(pa, _0_5);
fdb689b57b1b Some progress on new generator
unc0rr
parents: 10198
diff changeset
   257
        RandomizePoints(pa);
fdb689b57b1b Some progress on new generator
unc0rr
parents: 10198
diff changeset
   258
        RandomizePoints(pa)
fdb689b57b1b Some progress on new generator
unc0rr
parents: 10198
diff changeset
   259
        end;
fdb689b57b1b Some progress on new generator
unc0rr
parents: 10198
diff changeset
   260
    for i:= 1 to Template.RandPassesCount do
fdb689b57b1b Some progress on new generator
unc0rr
parents: 10198
diff changeset
   261
        RandomizePoints(pa);}
fdb689b57b1b Some progress on new generator
unc0rr
parents: 10198
diff changeset
   262
    BezierizeEdge(pa, _0_9);
fdb689b57b1b Some progress on new generator
unc0rr
parents: 10198
diff changeset
   263
end;
fdb689b57b1b Some progress on new generator
unc0rr
parents: 10198
diff changeset
   264
fdb689b57b1b Some progress on new generator
unc0rr
parents: 10198
diff changeset
   265
10198
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
   266
procedure GenTemplated(var Template: TEdgeTemplate);
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
   267
var pa: TPixAr;
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
   268
    i: Longword;
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
   269
    y, x: Longword;
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
   270
    fps: TPointArray;
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
   271
begin
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
   272
    fps:=Template.FillPoints^;
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
   273
    ResizeLand(Template.TemplateWidth, Template.TemplateHeight);
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
   274
    for y:= 0 to LAND_HEIGHT - 1 do
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
   275
        for x:= 0 to LAND_WIDTH - 1 do
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
   276
            Land[y, x]:= lfBasic;
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
   277
    {$HINTS OFF}
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
   278
    SetPoints(Template, pa, @fps);
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
   279
    {$HINTS ON}
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
   280
10199
fdb689b57b1b Some progress on new generator
unc0rr
parents: 10198
diff changeset
   281
    Distort1(Template, pa);
10198
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
   282
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
   283
    DrawEdge(pa, 0);
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
   284
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
   285
    with Template do
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
   286
        for i:= 0 to pred(FillPointsCount) do
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
   287
            with fps[i] do
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
   288
                FillLand(x, y, 0, 0);
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
   289
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
   290
    DrawEdge(pa, lfBasic);
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
   291
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
   292
    MaxHedgehogs:= Template.MaxHedgehogs;
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
   293
    hasGirders:= Template.hasGirders;
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
   294
    playHeight:= Template.TemplateHeight;
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
   295
    playWidth:= Template.TemplateWidth;
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
   296
    leftX:= ((LAND_WIDTH - playWidth) div 2);
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
   297
    rightX:= (playWidth + ((LAND_WIDTH - playWidth) div 2)) - 1;
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
   298
    topY:= LAND_HEIGHT - playHeight;
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
   299
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
   300
    // 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
   301
    if (cTemplateFilter = 4)
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
   302
    or (Template.canInvert and (getrandom(2) = 0))
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
   303
    or (not Template.canInvert and Template.isNegative) then
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
   304
        begin
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
   305
        hasBorder:= true;
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
   306
        for y:= 0 to LAND_HEIGHT - 1 do
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
   307
            for x:= 0 to LAND_WIDTH - 1 do
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
   308
                if (y < topY) or (x < leftX) or (x > rightX) then
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
   309
                    Land[y, x]:= 0
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
   310
                else
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
   311
                    begin
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
   312
                    if Land[y, x] = 0 then
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
   313
                        Land[y, x]:= lfBasic
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
   314
                    else if Land[y, x] = lfBasic then
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
   315
                        Land[y, x]:= 0;
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
   316
                    end;
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
   317
        end;
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
   318
end;
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
   319
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
   320
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
   321
end.