hedgewars/uLandOutline.pas
author unc0rr
Sun, 04 Dec 2011 00:52:47 +0300
changeset 6490 531bf083e8db
child 6491 736479f3d348
permissions -rw-r--r--
- Give uLand more modularity - Get rid of some more nested functions
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6490
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
     1
unit uLandOutline;
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
     2
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
     3
interface
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
     4
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
     5
uses uConsts, SDLh, uFloat;
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
     6
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
     7
type TPixAr = record
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
     8
              Count: Longword;
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
     9
              ar: array[0..Pred(cMaxEdgePoints)] of TPoint;
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
    10
              end;
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
    11
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
    12
procedure DrawEdge(var pa: TPixAr; Color: Longword);
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
    13
procedure FillLand(x, y: LongInt);
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
    14
procedure BezierizeEdge(var pa: TPixAr; Delta: hwFloat);
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
    15
procedure RandomizePoints(var pa: TPixAr);
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
    16
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
    17
implementation
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
    18
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
    19
uses uLandGraphics, uDebug, uVariables, uLandTemplates, uMisc, uRandom, uUtils;
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
    20
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
    21
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
    22
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
    23
var Stack: record
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
    24
           Count: Longword;
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
    25
           points: array[0..8192] of record
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
    26
                                     xl, xr, y, dir: LongInt;
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
    27
                                     end
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
    28
           end;
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
    29
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
    30
procedure Push(_xl, _xr, _y, _dir: LongInt);
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
    31
begin
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
    32
    TryDo(Stack.Count <= 8192, 'FillLand: stack overflow', true);
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
    33
    _y:= _y + _dir;
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
    34
    if (_y < 0) or (_y >= LAND_HEIGHT) then exit;
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
    35
    with Stack.points[Stack.Count] do
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
    36
            begin
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
    37
            xl:= _xl;
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
    38
            xr:= _xr;
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
    39
            y:= _y;
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
    40
            dir:= _dir
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
    41
            end;
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
    42
    inc(Stack.Count)
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
    43
end;
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
    44
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
    45
procedure Pop(var _xl, _xr, _y, _dir: LongInt);
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
    46
begin
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
    47
    dec(Stack.Count);
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
    48
    with Stack.points[Stack.Count] do
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
    49
        begin
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
    50
        _xl:= xl;
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
    51
        _xr:= xr;
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
    52
        _y:= y;
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
    53
        _dir:= dir
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
    54
        end
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
    55
end;
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
    56
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
    57
procedure FillLand(x, y: LongInt);
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
    58
var xl, xr, dir: LongInt;
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
    59
begin
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
    60
    Stack.Count:= 0;
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
    61
    xl:= x - 1;
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
    62
    xr:= x;
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
    63
    Push(xl, xr, y, -1);
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
    64
    Push(xl, xr, y,  1);
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
    65
    dir:= 0;
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
    66
    while Stack.Count > 0 do
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
    67
        begin
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
    68
        Pop(xl, xr, y, dir);
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
    69
        while (xl > 0) and (Land[y, xl] <> 0) do dec(xl);
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
    70
        while (xr < LAND_WIDTH - 1) and (Land[y, xr] <> 0) do inc(xr);
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
    71
        while (xl < xr) do
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
    72
            begin
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
    73
            while (xl <= xr) and (Land[y, xl] = 0) do inc(xl);
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
    74
            x:= xl;
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
    75
            while (xl <= xr) and (Land[y, xl] <> 0) do
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
    76
                begin
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
    77
                Land[y, xl]:= 0;
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
    78
                inc(xl)
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
    79
                end;
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
    80
            if x < xl then
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
    81
                begin
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
    82
                Push(x, Pred(xl), y, dir);
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
    83
                Push(x, Pred(xl), y,-dir);
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
    84
                end;
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
    85
            end;
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
    86
        end;
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
    87
end;
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
    88
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
    89
procedure DrawEdge(var pa: TPixAr; Color: Longword);
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
    90
var i: LongInt;
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
    91
begin
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
    92
    i:= 0;
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
    93
    with pa do
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
    94
        while i < LongInt(Count) - 1 do
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
    95
            if (ar[i + 1].X = NTPX) then 
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
    96
                inc(i, 2)
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
    97
            else 
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
    98
                begin
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
    99
                DrawLine(ar[i].x, ar[i].y, ar[i + 1].x, ar[i + 1].y, Color);
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   100
                inc(i)
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   101
                end
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   102
end;
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   103
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   104
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   105
procedure Vector(p1, p2, p3: TPoint; var Vx, Vy: hwFloat);
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   106
var d1, d2, d: hwFloat;
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   107
begin
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   108
    Vx:= int2hwFloat(p1.X - p3.X);
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   109
    Vy:= int2hwFloat(p1.Y - p3.Y);
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   110
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   111
    d:= DistanceI(p2.X - p1.X, p2.Y - p1.Y);
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   112
    d1:= DistanceI(p2.X - p3.X, p2.Y - p3.Y);
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   113
    d2:= Distance(Vx, Vy);
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   114
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   115
    if d1 < d then d:= d1;
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   116
    if d2 < d then d:= d2;
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   117
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   118
    d:= d * _1div3;
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   119
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   120
    if d2.QWordValue = 0 then
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   121
        begin
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   122
        Vx:= _0;
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   123
        Vy:= _0
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   124
        end 
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   125
    else
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   126
        begin
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   127
        d2:= _1 / d2;
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   128
        Vx:= Vx * d2;
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   129
        Vy:= Vy * d2;
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   130
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   131
        Vx:= Vx * d;
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   132
        Vy:= Vy * d
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   133
        end
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   134
end;
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   135
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   136
procedure AddLoopPoints(var pa, opa: TPixAr; StartI, EndI: LongInt; Delta: hwFloat);
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   137
var i, pi, ni: LongInt;
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   138
    NVx, NVy, PVx, PVy: hwFloat;
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   139
    x1, x2, y1, y2: LongInt;
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   140
    tsq, tcb, t, r1, r2, r3, cx1, cx2, cy1, cy2: hwFloat;
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   141
    X, Y: LongInt;
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   142
begin
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   143
pi:= EndI;
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   144
i:= StartI;
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   145
ni:= Succ(StartI);
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   146
{$HINTS OFF}
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   147
Vector(opa.ar[pi], opa.ar[i], opa.ar[ni], NVx, NVy);
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   148
{$HINTS ON}
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   149
repeat
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   150
    inc(pi);
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   151
    if pi > EndI then pi:= StartI;
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   152
    inc(i);
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   153
    if i > EndI then i:= StartI;
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   154
    inc(ni);
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   155
    if ni > EndI then ni:= StartI;
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   156
    PVx:= NVx;
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   157
    PVy:= NVy;
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   158
    Vector(opa.ar[pi], opa.ar[i], opa.ar[ni], NVx, NVy);
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   159
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   160
    x1:= opa.ar[pi].x;
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   161
    y1:= opa.ar[pi].y;
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   162
    x2:= opa.ar[i].x;
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   163
    y2:= opa.ar[i].y;
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   164
    cx1:= int2hwFloat(x1) - PVx;
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   165
    cy1:= int2hwFloat(y1) - PVy;
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   166
    cx2:= int2hwFloat(x2) + NVx;
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   167
    cy2:= int2hwFloat(y2) + NVy;
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   168
    t:= _0;
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   169
    while t.Round = 0 do
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   170
          begin
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   171
          tsq:= t * t;
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   172
          tcb:= tsq * t;
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   173
          r1:= (_1 - t*3 + tsq*3 - tcb);
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   174
          r2:= (     t*3 - tsq*6 + tcb*3);
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   175
          r3:= (           tsq*3 - tcb*3);
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   176
          X:= hwRound(r1 * x1 + r2 * cx1 + r3 * cx2 + tcb * x2);
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   177
          Y:= hwRound(r1 * y1 + r2 * cy1 + r3 * cy2 + tcb * y2);
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   178
          t:= t + Delta;
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   179
          pa.ar[pa.Count].x:= X;
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   180
          pa.ar[pa.Count].y:= Y;
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   181
          inc(pa.Count);
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   182
          TryDo(pa.Count <= cMaxEdgePoints, 'Edge points overflow', true)
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   183
          end;
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   184
until i = StartI;
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   185
pa.ar[pa.Count].x:= opa.ar[StartI].X;
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   186
pa.ar[pa.Count].y:= opa.ar[StartI].Y;
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   187
inc(pa.Count)
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   188
end;
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   189
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   190
procedure BezierizeEdge(var pa: TPixAr; Delta: hwFloat);
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   191
var i, StartLoop: LongInt;
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   192
    opa: TPixAr;
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   193
begin
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   194
opa:= pa;
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   195
pa.Count:= 0;
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   196
i:= 0;
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   197
StartLoop:= 0;
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   198
while i < LongInt(opa.Count) do
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   199
    if (opa.ar[i + 1].X = NTPX) then
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   200
       begin
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   201
       AddLoopPoints(pa, opa, StartLoop, i, Delta);
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   202
       inc(i, 2);
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   203
       StartLoop:= i;
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   204
       pa.ar[pa.Count].X:= NTPX;
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   205
       pa.ar[pa.Count].Y:= 0;
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   206
       inc(pa.Count);
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   207
       end else inc(i)
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   208
end;
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   209
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   210
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   211
function CheckIntersect(V1, V2, V3, V4: TPoint): boolean;
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   212
var c1, c2, dm: LongInt;
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   213
begin
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   214
    dm:= (V4.y - V3.y) * (V2.x - V1.x) - (V4.x - V3.x) * (V2.y - V1.y);
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   215
    c1:= (V4.x - V3.x) * (V1.y - V3.y) - (V4.y - V3.y) * (V1.x - V3.x);
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   216
    if dm = 0 then exit(false);
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   217
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   218
    c2:= (V2.x - V3.x) * (V1.y - V3.y) - (V2.y - V3.y) * (V1.x - V3.x);
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   219
    if dm > 0 then
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   220
        begin
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   221
        if (c1 < 0) or (c1 > dm) then exit(false);
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   222
        if (c2 < 0) or (c2 > dm) then exit(false)
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   223
        end 
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   224
    else
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   225
        begin
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   226
        if (c1 > 0) or (c1 < dm) then exit(false);
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   227
        if (c2 > 0) or (c2 < dm) then exit(false)
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   228
        end;
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   229
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   230
    //AddFileLog('1  (' + inttostr(V1.x) + ',' + inttostr(V1.y) + ')x(' + inttostr(V2.x) + ',' + inttostr(V2.y) + ')');
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   231
    //AddFileLog('2  (' + inttostr(V3.x) + ',' + inttostr(V3.y) + ')x(' + inttostr(V4.x) + ',' + inttostr(V4.y) + ')');
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   232
    CheckIntersect:= true
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   233
end;
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   234
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   235
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   236
function CheckSelfIntersect(var pa: TPixAr; ind: Longword): boolean;
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   237
var i: Longword;
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   238
begin
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   239
    if (ind <= 0) or (ind >= Pred(pa.Count)) then exit(false);
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   240
    for i:= 1 to pa.Count - 3 do
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   241
        if (i <= ind - 1) or (i >= ind + 2) then
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   242
        begin
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   243
        if (i <> ind - 1) and
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   244
            CheckIntersect(pa.ar[ind], pa.ar[ind - 1], pa.ar[i], pa.ar[i - 1]) then exit(true);
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   245
        if (i <> ind + 2) and
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   246
            CheckIntersect(pa.ar[ind], pa.ar[ind + 1], pa.ar[i], pa.ar[i - 1]) then exit(true);
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   247
        end;
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   248
    CheckSelfIntersect:= false
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   249
end;
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   250
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   251
procedure RandomizePoints(var pa: TPixAr);
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   252
const cEdge = 55;
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   253
      cMinDist = 8;
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   254
var radz: array[0..Pred(cMaxEdgePoints)] of LongInt;
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   255
    i, k, dist, px, py: LongInt;
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   256
begin
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   257
    for i:= 0 to Pred(pa.Count) do
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   258
    begin
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   259
    radz[i]:= 0;
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   260
        with pa.ar[i] do
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   261
            if x <> NTPX then
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   262
            begin
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   263
            radz[i]:= Min(Max(x - cEdge, 0), Max(LAND_WIDTH - cEdge - x, 0));
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   264
            radz[i]:= Min(radz[i], Min(Max(y - cEdge, 0), Max(LAND_HEIGHT - cEdge - y, 0)));
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   265
            if radz[i] > 0 then
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   266
                for k:= 0 to Pred(i) do
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   267
                begin
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   268
                dist:= Max(abs(x - pa.ar[k].x), abs(y - pa.ar[k].y));
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   269
                radz[k]:= Max(0, Min((dist - cMinDist) div 2, radz[k]));
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   270
                radz[i]:= Max(0, Min(dist - radz[k] - cMinDist, radz[i]))
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   271
                end
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   272
            end;
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   273
    end;
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   274
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   275
    for i:= 0 to Pred(pa.Count) do
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   276
        with pa.ar[i] do
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   277
            if ((x and LAND_WIDTH_MASK) = 0) and ((y and LAND_HEIGHT_MASK) = 0) then
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   278
            begin
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   279
            px:= x;
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   280
            py:= y;
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   281
            x:= x + LongInt(GetRandom(7) - 3) * (radz[i] * 5 div 7) div 3;
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   282
            y:= y + LongInt(GetRandom(7) - 3) * (radz[i] * 5 div 7) div 3;
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   283
            if CheckSelfIntersect(pa, i) then
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   284
                begin
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   285
                x:= px;
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   286
                y:= py
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   287
                end;
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   288
            end
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   289
end;
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   290
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   291
531bf083e8db - Give uLand more modularity
unc0rr
parents:
diff changeset
   292
end.