File: Rivers.pas

package info (click to toggle)
cevomapgen 39-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 476 kB
  • sloc: pascal: 2,765; xml: 169; makefile: 50
file content (338 lines) | stat: -rw-r--r-- 8,401 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
unit Rivers;

{***********************************************************

Project:    C-evo External Map Generator
Copyright:  2024 P Blackman
License:    GPLv3+

Unit to support the generation of rivers (diffusion model).

***********************************************************}

interface

uses MapTiles, CevoMap, Classes;

type
    tRivers = class
    private

    const Fudge  = 7; // Fudge factor to help avoid local maximums when running rivers.

    type
        tNeighbours = array [1..4] of tCell;

        tRData =
            record
            AltReal: integer;
            River: boolean;
        end;

        tMyTiles = array [1..LxMax, 1..LyMax] of tRData;

    private
        fMap: tMap;
        fRMap: tMyTiles;

        fPercentage: integer;

        fWidthSlots  : array of integer;
        fHeightSlots : array of integer;

    protected
        function RiverNeighbours (W, H: integer): integer;
        function NextRiverTile(W, H: integer; out NW, NH: integer): boolean;
        function GetNeighbours(W, H: integer): tNeighbours;
        function OutOfRange(N: tcell): boolean;

        function GetCoastalTiles(W, H: integer): integer;
        function RunRiver(SW, SH: integer): integer;
        function NearToRiver(W, H: integer): boolean;
        function Estuary(W, H: integer): boolean;
        function SeaTiles(W, H: integer): integer;
        function BestStart(out SW, SH: integer): boolean;
        function Getland : integer;

        procedure RandomSequence(var MyArray : array of integer);
    public
        procedure Generate;
        procedure Loaddata(Map: tMap);

        procedure Free;
        constructor Create(Percentage: integer);
    end;


implementation

uses SysUtils;

constructor tRivers.Create(Percentage: integer);
begin
    inherited Create;
    fPercentage := Percentage;

    fRMap := default(tMyTiles);
end;

procedure tRivers.Free;
begin
    SetLength (fWidthSlots,  0);
    SetLength (fHeightSlots, 0);

    inherited Free;
end;


{ Note, some neighbours will be out of height range }
{ Need to wrap width in round world }
function tRivers.GetNeighbours(W, H: integer): tNeighbours;
var MyBors: tNeighbours;
begin
    if Odd(H) then
    begin
        MyBors[1].wid := fmap.WrapCheck(Pred(W));
        MyBors[2].wid := W;
        MyBors[3].wid := W;
        MyBors[4].wid := fmap.WrapCheck(Pred(W));
    end
    else
    begin
        MyBors[1].wid := W;
        MyBors[2].wid := fmap.WrapCheck(Succ(W));
        MyBors[3].wid := fmap.WrapCheck(Succ(W));
        MyBors[4].wid := W;
    end;

    MyBors[1].hgt := H - 1;
    MyBors[2].hgt := H - 1;
    MyBors[3].hgt := H + 1;
    MyBors[4].hgt := H + 1;

    result := MyBors;
end;

function tRivers.OutOfRange(N: tcell): boolean;
begin
    result := (N.wid < 1) or (N.wid > fMap.Width) or (N.hgt < 1) or
        (N.hgt > fMap.Height);
end;

function tRivers.RiverNeighbours (W, H: integer): integer;
var MyBors: tNeighbours;
    N  : Integer;
begin
    MyBors := Getneighbours(W, H);
    Result := 0;
    for N := 1 to 4 do
        if OutOfRange(MyBors[N]) then
        {Skip it}
        else
        if fRMap[MyBors[N].Wid, MyBors[N].Hgt].River then
            inc (Result);
end;

function tRivers.NextRiverTile(W, H: integer; out NW, NH: integer): boolean;
var MyBors: tNeighbours;
    N,Myhgt,nbhgt,lwhgt: integer;
begin
    result := False;
    lwhgt := 1000; // Arbitrary large number;
    NW := 0;
    NH := 0;
    Myhgt := fRMap[W, H].AltReal;
    MyBors := Getneighbours(W, H);

    for N := 1 to 4 do
        if OutOfRange(MyBors[N]) then
        {Skip it}
        else
        if fRMap[MyBors[N].Wid, MyBors[N].Hgt].River then
        // Already a river tile
        else
        begin
            nbhgt := fRMap[MyBors[N].Wid, MyBors[N].Hgt].Altreal;
            if (MyHgt <= nbhgt + Fudge) and (nbhgt < lwhgt)
            and (SeaTiles(MyBors[N].Wid, MyBors[N].Hgt) = 0)
            and (RiverNeighbours (MyBors[N].Wid, MyBors[N].Hgt) < 3) then
            begin
                { Found a lowest higher or equal neighbour, that is not on the coast }
                result := True;
                NW := MyBors[N].Wid;
                NH := MyBors[N].Hgt;
                lwhgt := nbhgt;
            end;
        end;
end;

function tRivers.GetCoastalTiles(W, H: integer): integer;
var N,NW,NH: integer;
begin
    result := 0;
    for N := 1 to 8 do
    begin
        fMap.SetNeighbour(W, H, N, NW, NH);

        if fRMap[NW, NH].AltReal = -1 then
            Inc(result);
    end;
end;

function tRivers.NearToRiver(W, H: integer): boolean;
var N,NW,NH: integer;
begin
    result := False;
    for N := 0 to 20 do
    begin
        // Nothing to do with  cities, but convenient to use this function
        fMap.GetCityNeighbour(W, H, N, NW, NH);

        if (NH > 0) and (NH <= fMap.Height) then
            if fRMap[NW, NH].River then
                result := True;
    end;
end;

function tRivers.Estuary(W, H: integer): boolean;
var C : Integer;
begin
    if SeaTiles (W,H) = 1 then
        C := GetCoastalTiles(W, H)
    else
        C := 0;

    Estuary := C in [1,2,3];
end;

function tRivers.SeaTiles(W, H: integer): integer;
var N: integer;
    MyBors: tNeighbours;
begin
    result := 0;
    MyBors := GetNeighbours(W, H);

    for N := 1 to 4 do
        if OutofRange(MyBors[N]) then
        // skip
        else
        if fRMap[MyBors[N].Wid, MyBors[N].hgt].ALtReal = -1 then
            Inc(result);
end;

function tRivers.BestStart(out SW, SH: integer): boolean;
var GCT,CS,W,H,WS,HS: integer;
begin
    result := False;
    SW := 0;
    SH := 0;
    CS := 9; // Impossible result

    // slots indexed from 0, Map indexed from 1
    for W := 0 to fMap.Width-1 do
    begin
        WS := fWidthSlots[W] +1;
        for H := 0 to fMap.Height-1 do
        begin
            HS := fHeightSlots[H] +1;

            if NearToRiver(WS, HS) then
                // Space out River starts
            else
            if Estuary(WS, HS) then
            begin
                GCT := GetCoastalTiles(WS, HS);
                if (GCT > 0) and (GCT < CS) then
                    if (GCT = 1) and (CS < 9) then
                        // Try avoid starting river from a puddle
                    else
                    begin
                        // Best so far
                        SW := WS;
                        SH := HS;
                        CS := GCT;
                        result := True;
                    end;
            end;
        end;
    end;
end;

function tRivers.RunRiver(SW, SH: integer): integer;
var NW,NH: integer;
begin
    fRMap[SW, SH].River := True;
    fMap.Tiles[SW, SH].River := True;
    result := 1;
    while NextRiverTile(SW, SH, NW, NH) do
    begin
        SW := NW;
        SH := NH;
        fRMap[SW, SH].River := True;
        fMap.Tiles[SW, SH].River := True;
        Inc(result);
    end;

    //Writeln('Run River ', result);
end;

function tRivers.Getland : integer;
var W,H: integer;
begin
    result := 0;
    for W := 1 to fMap.Width do
      for H := 1 to fMap.Height do
        if fRMap[W, H].AltReal <> -1 then
            Inc(result);
end;

procedure tRivers.Generate;
var SW,SH,Target: integer;
begin
    Target := (GetLand * fPercentage) div 100;

    while Target > 0 do
        if BestStart(SW, SH) then
            Target := Target - RunRiver(SW, SH)
        else
            Target := 0 // Terminate loop
end;


procedure tRivers.RandomSequence(var MyArray : array of integer);
var I,J,tmp : integer;
begin
    for I := low(MyArray) to high(MyArray) do
        MyArray[I] := I;

    // Shuffle by swaping each element with a random one
    for I := low(MyArray) to high(MyArray) do
    begin
        tmp         := MyArray[I];
        J           := Random(high(MyArray)+1);
        MyArray[I]  := MyArray[J];
        MyArray[J]  := tmp;
    end;
end;

procedure tRivers.LoadData(Map: tMap);
var W,H: integer;
begin
    fMap := Map;

    for W := 1 to fMap.Width do
      for H := 1 to fMap.Height do
        if fMap.GetVal(Altitude, W, H) < fMap.SeaLevel then
            { Ocean tile }
            fRMap[W, H].AltReal := -1
        else
            fRMap[W, H].AltReal := fMap.GetVal(Altitude, W, H) - fMap.SeaLevel;

    SetLength (fWidthSlots,  fMap.Width);
    SetLength (fHeightSlots, fMap.Height);

    RandomSequence (fWidthSlots);
    RandomSequence (fHeightSlots);
end;

end.