hedgewars/uLandUtils.pas
author unC0Rr
Mon, 02 Jan 2023 15:59:26 +0100
branchtransitional_engine
changeset 15929 128ace913837
parent 15164 794dc7237ca1
child 15930 f39f0f614dbf
permissions -rw-r--r--
Introduce hwengine-future library, use Land allocated in it
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 uLandUtils;
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
procedure ResizeLand(width, height: LongWord);
15929
128ace913837 Introduce hwengine-future library, use Land allocated in it
unC0Rr
parents: 15164
diff changeset
     5
procedure DisposeLand();
10626
2562797ab3cf adjust position of world edges (at 150 px away from outmost land collision, or less if land array ends earlier)
sheepluva
parents: 10198
diff changeset
     6
procedure InitWorldEdges();
10198
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
     7
15929
128ace913837 Introduce hwengine-future library, use Land allocated in it
unC0Rr
parents: 15164
diff changeset
     8
function  LandGet(y, x: LongInt): Word;
128ace913837 Introduce hwengine-future library, use Land allocated in it
unC0Rr
parents: 15164
diff changeset
     9
procedure LandSet(y, x: LongInt; value: Word);
128ace913837 Introduce hwengine-future library, use Land allocated in it
unC0Rr
parents: 15164
diff changeset
    10
128ace913837 Introduce hwengine-future library, use Land allocated in it
unC0Rr
parents: 15164
diff changeset
    11
procedure FillLand(x, y: LongInt; border, value: Word);
128ace913837 Introduce hwengine-future library, use Land allocated in it
unC0Rr
parents: 15164
diff changeset
    12
10198
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    13
implementation
10626
2562797ab3cf adjust position of world edges (at 150 px away from outmost land collision, or less if land array ends earlier)
sheepluva
parents: 10198
diff changeset
    14
uses uUtils, uConsts, uVariables, uTypes;
10198
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    15
15929
128ace913837 Introduce hwengine-future library, use Land allocated in it
unC0Rr
parents: 15164
diff changeset
    16
const LibFutureName = 'hwengine_future';
128ace913837 Introduce hwengine-future library, use Land allocated in it
unC0Rr
parents: 15164
diff changeset
    17
function  create_game_field(width, height: Longword): pointer; cdecl; external LibFutureName;
128ace913837 Introduce hwengine-future library, use Land allocated in it
unC0Rr
parents: 15164
diff changeset
    18
procedure dispose_game_field(game_field: pointer); cdecl; external LibFutureName;
128ace913837 Introduce hwengine-future library, use Land allocated in it
unC0Rr
parents: 15164
diff changeset
    19
function  land_get(game_field: pointer; x, y: LongInt): Word; cdecl; external LibFutureName;
128ace913837 Introduce hwengine-future library, use Land allocated in it
unC0Rr
parents: 15164
diff changeset
    20
procedure land_set(game_field: pointer; x, y: LongInt; value: Word); cdecl; external LibFutureName;
128ace913837 Introduce hwengine-future library, use Land allocated in it
unC0Rr
parents: 15164
diff changeset
    21
procedure land_fill(game_field: pointer; x, y: LongInt; border, fill: Word); cdecl; external LibFutureName;
128ace913837 Introduce hwengine-future library, use Land allocated in it
unC0Rr
parents: 15164
diff changeset
    22
128ace913837 Introduce hwengine-future library, use Land allocated in it
unC0Rr
parents: 15164
diff changeset
    23
var gameField: pointer;
128ace913837 Introduce hwengine-future library, use Land allocated in it
unC0Rr
parents: 15164
diff changeset
    24
128ace913837 Introduce hwengine-future library, use Land allocated in it
unC0Rr
parents: 15164
diff changeset
    25
function  LandGet(y, x: LongInt): Word;
128ace913837 Introduce hwengine-future library, use Land allocated in it
unC0Rr
parents: 15164
diff changeset
    26
begin
128ace913837 Introduce hwengine-future library, use Land allocated in it
unC0Rr
parents: 15164
diff changeset
    27
    LandGet:= land_get(gameField, x, y)
128ace913837 Introduce hwengine-future library, use Land allocated in it
unC0Rr
parents: 15164
diff changeset
    28
end;
128ace913837 Introduce hwengine-future library, use Land allocated in it
unC0Rr
parents: 15164
diff changeset
    29
128ace913837 Introduce hwengine-future library, use Land allocated in it
unC0Rr
parents: 15164
diff changeset
    30
procedure LandSet(y, x: LongInt; value: Word);
128ace913837 Introduce hwengine-future library, use Land allocated in it
unC0Rr
parents: 15164
diff changeset
    31
begin
128ace913837 Introduce hwengine-future library, use Land allocated in it
unC0Rr
parents: 15164
diff changeset
    32
    land_set(gameField, x, y, value)
128ace913837 Introduce hwengine-future library, use Land allocated in it
unC0Rr
parents: 15164
diff changeset
    33
end;
128ace913837 Introduce hwengine-future library, use Land allocated in it
unC0Rr
parents: 15164
diff changeset
    34
128ace913837 Introduce hwengine-future library, use Land allocated in it
unC0Rr
parents: 15164
diff changeset
    35
procedure FillLand(x, y: LongInt; border, value: Word);
128ace913837 Introduce hwengine-future library, use Land allocated in it
unC0Rr
parents: 15164
diff changeset
    36
begin
128ace913837 Introduce hwengine-future library, use Land allocated in it
unC0Rr
parents: 15164
diff changeset
    37
    land_fill(gameField, x, y, border, value)
128ace913837 Introduce hwengine-future library, use Land allocated in it
unC0Rr
parents: 15164
diff changeset
    38
end;
128ace913837 Introduce hwengine-future library, use Land allocated in it
unC0Rr
parents: 15164
diff changeset
    39
10198
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    40
procedure ResizeLand(width, height: LongWord);
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    41
var potW, potH: LongInt;
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
potW:= toPowerOf2(width);
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    44
potH:= toPowerOf2(height);
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    45
if (potW <> LAND_WIDTH) or (potH <> LAND_HEIGHT) then
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    46
    begin
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    47
    LAND_WIDTH:= potW;
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    48
    LAND_HEIGHT:= potH;
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    49
    LAND_WIDTH_MASK:= not(LAND_WIDTH-1);
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    50
    LAND_HEIGHT_MASK:= not(LAND_HEIGHT-1);
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    51
    cWaterLine:= LAND_HEIGHT;
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    52
    if (cReducedQuality and rqBlurryLand) = 0 then
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    53
        SetLength(LandPixels, LAND_HEIGHT, LAND_WIDTH)
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    54
    else
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    55
        SetLength(LandPixels, LAND_HEIGHT div 2, LAND_WIDTH div 2);
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    56
15929
128ace913837 Introduce hwengine-future library, use Land allocated in it
unC0Rr
parents: 15164
diff changeset
    57
    gameField:= create_game_field(LAND_WIDTH, LAND_HEIGHT);
10198
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    58
    SetLength(LandDirty, (LAND_HEIGHT div 32), (LAND_WIDTH div 32));
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    59
    // 0.5 is already approaching on unplayable
10994
cd7f918eed30 bit friendlier if the uVariables values get changed
nemo
parents: 10626
diff changeset
    60
    if (width div 4096 >= 2) or (height div 2048 >= 2) then cMaxZoomLevel:= cMaxZoomLevel/2;
10198
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    61
    cMinMaxZoomLevelDelta:= cMaxZoomLevel - cMinZoomLevel
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    62
    end;
11704
1694b379c83f fix clouds/flakes area not being adjusted after ResizeLand()
sheepluva
parents: 10994
diff changeset
    63
initScreenSpaceVars();
10198
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    64
end;
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    65
15929
128ace913837 Introduce hwengine-future library, use Land allocated in it
unC0Rr
parents: 15164
diff changeset
    66
procedure DisposeLand();
128ace913837 Introduce hwengine-future library, use Land allocated in it
unC0Rr
parents: 15164
diff changeset
    67
begin
128ace913837 Introduce hwengine-future library, use Land allocated in it
unC0Rr
parents: 15164
diff changeset
    68
    dispose_game_field(gameField)
128ace913837 Introduce hwengine-future library, use Land allocated in it
unC0Rr
parents: 15164
diff changeset
    69
end;
128ace913837 Introduce hwengine-future library, use Land allocated in it
unC0Rr
parents: 15164
diff changeset
    70
10626
2562797ab3cf adjust position of world edges (at 150 px away from outmost land collision, or less if land array ends earlier)
sheepluva
parents: 10198
diff changeset
    71
procedure InitWorldEdges();
2562797ab3cf adjust position of world edges (at 150 px away from outmost land collision, or less if land array ends earlier)
sheepluva
parents: 10198
diff changeset
    72
var cy, cx, lx, ly: LongInt;
2562797ab3cf adjust position of world edges (at 150 px away from outmost land collision, or less if land array ends earlier)
sheepluva
parents: 10198
diff changeset
    73
    found: boolean;
2562797ab3cf adjust position of world edges (at 150 px away from outmost land collision, or less if land array ends earlier)
sheepluva
parents: 10198
diff changeset
    74
begin
2562797ab3cf adjust position of world edges (at 150 px away from outmost land collision, or less if land array ends earlier)
sheepluva
parents: 10198
diff changeset
    75
playHeight:= LAND_HEIGHT;
2562797ab3cf adjust position of world edges (at 150 px away from outmost land collision, or less if land array ends earlier)
sheepluva
parents: 10198
diff changeset
    76
topY:= 0;
2562797ab3cf adjust position of world edges (at 150 px away from outmost land collision, or less if land array ends earlier)
sheepluva
parents: 10198
diff changeset
    77
2562797ab3cf adjust position of world edges (at 150 px away from outmost land collision, or less if land array ends earlier)
sheepluva
parents: 10198
diff changeset
    78
lx:= LongInt(LAND_WIDTH) - 1;
2562797ab3cf adjust position of world edges (at 150 px away from outmost land collision, or less if land array ends earlier)
sheepluva
parents: 10198
diff changeset
    79
15164
794dc7237ca1 Fix world edge positions for drawn maps
Wuzzy <Wuzzy2@mail.ru>
parents: 12311
diff changeset
    80
// don't change world edges for drawn maps
794dc7237ca1 Fix world edge positions for drawn maps
Wuzzy <Wuzzy2@mail.ru>
parents: 12311
diff changeset
    81
if (cMapGen = mgDrawn) then
794dc7237ca1 Fix world edge positions for drawn maps
Wuzzy <Wuzzy2@mail.ru>
parents: 12311
diff changeset
    82
    // edges were adjusted already in GenDrawnMap() in uLand
794dc7237ca1 Fix world edge positions for drawn maps
Wuzzy <Wuzzy2@mail.ru>
parents: 12311
diff changeset
    83
    EXIT;
794dc7237ca1 Fix world edge positions for drawn maps
Wuzzy <Wuzzy2@mail.ru>
parents: 12311
diff changeset
    84
12311
1b5a4807f8f4 fix fort mode's edge adjustments getting lost with weWrap. issue #181
sheepluva
parents: 11704
diff changeset
    85
// use maximum available map width if there is no special world edge
10626
2562797ab3cf adjust position of world edges (at 150 px away from outmost land collision, or less if land array ends earlier)
sheepluva
parents: 10198
diff changeset
    86
if WorldEdge = weNone then
2562797ab3cf adjust position of world edges (at 150 px away from outmost land collision, or less if land array ends earlier)
sheepluva
parents: 10198
diff changeset
    87
    begin
2562797ab3cf adjust position of world edges (at 150 px away from outmost land collision, or less if land array ends earlier)
sheepluva
parents: 10198
diff changeset
    88
    playWidth:= LAND_WIDTH;
2562797ab3cf adjust position of world edges (at 150 px away from outmost land collision, or less if land array ends earlier)
sheepluva
parents: 10198
diff changeset
    89
    leftX := 0;
2562797ab3cf adjust position of world edges (at 150 px away from outmost land collision, or less if land array ends earlier)
sheepluva
parents: 10198
diff changeset
    90
    rightX:= lx;
2562797ab3cf adjust position of world edges (at 150 px away from outmost land collision, or less if land array ends earlier)
sheepluva
parents: 10198
diff changeset
    91
    EXIT;
2562797ab3cf adjust position of world edges (at 150 px away from outmost land collision, or less if land array ends earlier)
sheepluva
parents: 10198
diff changeset
    92
    end;
2562797ab3cf adjust position of world edges (at 150 px away from outmost land collision, or less if land array ends earlier)
sheepluva
parents: 10198
diff changeset
    93
12311
1b5a4807f8f4 fix fort mode's edge adjustments getting lost with weWrap. issue #181
sheepluva
parents: 11704
diff changeset
    94
// keep fort distance consistent if we're in wrap mode on fort map
1b5a4807f8f4 fix fort mode's edge adjustments getting lost with weWrap. issue #181
sheepluva
parents: 11704
diff changeset
    95
if (cMapGen = mgForts) and (WorldEdge = weWrap) then
1b5a4807f8f4 fix fort mode's edge adjustments getting lost with weWrap. issue #181
sheepluva
parents: 11704
diff changeset
    96
    begin
1b5a4807f8f4 fix fort mode's edge adjustments getting lost with weWrap. issue #181
sheepluva
parents: 11704
diff changeset
    97
    // edges were adjusted already in MakeFortsMap() in uLand
1b5a4807f8f4 fix fort mode's edge adjustments getting lost with weWrap. issue #181
sheepluva
parents: 11704
diff changeset
    98
    EXIT;
1b5a4807f8f4 fix fort mode's edge adjustments getting lost with weWrap. issue #181
sheepluva
parents: 11704
diff changeset
    99
    end;
1b5a4807f8f4 fix fort mode's edge adjustments getting lost with weWrap. issue #181
sheepluva
parents: 11704
diff changeset
   100
10626
2562797ab3cf adjust position of world edges (at 150 px away from outmost land collision, or less if land array ends earlier)
sheepluva
parents: 10198
diff changeset
   101
ly:= LongInt(LAND_HEIGHT) - 1;
2562797ab3cf adjust position of world edges (at 150 px away from outmost land collision, or less if land array ends earlier)
sheepluva
parents: 10198
diff changeset
   102
2562797ab3cf adjust position of world edges (at 150 px away from outmost land collision, or less if land array ends earlier)
sheepluva
parents: 10198
diff changeset
   103
// find most left land pixels and set leftX accordingly
2562797ab3cf adjust position of world edges (at 150 px away from outmost land collision, or less if land array ends earlier)
sheepluva
parents: 10198
diff changeset
   104
found:= false;
2562797ab3cf adjust position of world edges (at 150 px away from outmost land collision, or less if land array ends earlier)
sheepluva
parents: 10198
diff changeset
   105
for cx:= 0 to lx do
2562797ab3cf adjust position of world edges (at 150 px away from outmost land collision, or less if land array ends earlier)
sheepluva
parents: 10198
diff changeset
   106
    begin
2562797ab3cf adjust position of world edges (at 150 px away from outmost land collision, or less if land array ends earlier)
sheepluva
parents: 10198
diff changeset
   107
    for cy:= ly downto 0 do
15929
128ace913837 Introduce hwengine-future library, use Land allocated in it
unC0Rr
parents: 15164
diff changeset
   108
        if LandGet(cy, cx) <> 0 then
10626
2562797ab3cf adjust position of world edges (at 150 px away from outmost land collision, or less if land array ends earlier)
sheepluva
parents: 10198
diff changeset
   109
            begin
2562797ab3cf adjust position of world edges (at 150 px away from outmost land collision, or less if land array ends earlier)
sheepluva
parents: 10198
diff changeset
   110
            leftX:= max(0, cx - cWorldEdgeDist);
2562797ab3cf adjust position of world edges (at 150 px away from outmost land collision, or less if land array ends earlier)
sheepluva
parents: 10198
diff changeset
   111
            // break out of both loops
2562797ab3cf adjust position of world edges (at 150 px away from outmost land collision, or less if land array ends earlier)
sheepluva
parents: 10198
diff changeset
   112
            found:= true;
2562797ab3cf adjust position of world edges (at 150 px away from outmost land collision, or less if land array ends earlier)
sheepluva
parents: 10198
diff changeset
   113
            break;
2562797ab3cf adjust position of world edges (at 150 px away from outmost land collision, or less if land array ends earlier)
sheepluva
parents: 10198
diff changeset
   114
            end;
2562797ab3cf adjust position of world edges (at 150 px away from outmost land collision, or less if land array ends earlier)
sheepluva
parents: 10198
diff changeset
   115
    if found then break;
2562797ab3cf adjust position of world edges (at 150 px away from outmost land collision, or less if land array ends earlier)
sheepluva
parents: 10198
diff changeset
   116
    end;
2562797ab3cf adjust position of world edges (at 150 px away from outmost land collision, or less if land array ends earlier)
sheepluva
parents: 10198
diff changeset
   117
2562797ab3cf adjust position of world edges (at 150 px away from outmost land collision, or less if land array ends earlier)
sheepluva
parents: 10198
diff changeset
   118
// find most right land pixels and set rightX accordingly
2562797ab3cf adjust position of world edges (at 150 px away from outmost land collision, or less if land array ends earlier)
sheepluva
parents: 10198
diff changeset
   119
found:= false;
2562797ab3cf adjust position of world edges (at 150 px away from outmost land collision, or less if land array ends earlier)
sheepluva
parents: 10198
diff changeset
   120
for cx:= lx downto 0 do
2562797ab3cf adjust position of world edges (at 150 px away from outmost land collision, or less if land array ends earlier)
sheepluva
parents: 10198
diff changeset
   121
    begin
2562797ab3cf adjust position of world edges (at 150 px away from outmost land collision, or less if land array ends earlier)
sheepluva
parents: 10198
diff changeset
   122
    for cy:= ly downto 0 do
15929
128ace913837 Introduce hwengine-future library, use Land allocated in it
unC0Rr
parents: 15164
diff changeset
   123
        if LandGet(cy, cx) <> 0 then
10626
2562797ab3cf adjust position of world edges (at 150 px away from outmost land collision, or less if land array ends earlier)
sheepluva
parents: 10198
diff changeset
   124
            begin
2562797ab3cf adjust position of world edges (at 150 px away from outmost land collision, or less if land array ends earlier)
sheepluva
parents: 10198
diff changeset
   125
            rightX:= min(lx, cx + cWorldEdgeDist);
2562797ab3cf adjust position of world edges (at 150 px away from outmost land collision, or less if land array ends earlier)
sheepluva
parents: 10198
diff changeset
   126
            // break out of both loops
2562797ab3cf adjust position of world edges (at 150 px away from outmost land collision, or less if land array ends earlier)
sheepluva
parents: 10198
diff changeset
   127
            found:= true;
2562797ab3cf adjust position of world edges (at 150 px away from outmost land collision, or less if land array ends earlier)
sheepluva
parents: 10198
diff changeset
   128
            break;
2562797ab3cf adjust position of world edges (at 150 px away from outmost land collision, or less if land array ends earlier)
sheepluva
parents: 10198
diff changeset
   129
            end;
2562797ab3cf adjust position of world edges (at 150 px away from outmost land collision, or less if land array ends earlier)
sheepluva
parents: 10198
diff changeset
   130
    if found then break;
2562797ab3cf adjust position of world edges (at 150 px away from outmost land collision, or less if land array ends earlier)
sheepluva
parents: 10198
diff changeset
   131
    end;
2562797ab3cf adjust position of world edges (at 150 px away from outmost land collision, or less if land array ends earlier)
sheepluva
parents: 10198
diff changeset
   132
2562797ab3cf adjust position of world edges (at 150 px away from outmost land collision, or less if land array ends earlier)
sheepluva
parents: 10198
diff changeset
   133
playWidth := rightX + 1 - leftX;
2562797ab3cf adjust position of world edges (at 150 px away from outmost land collision, or less if land array ends earlier)
sheepluva
parents: 10198
diff changeset
   134
end;
2562797ab3cf adjust position of world edges (at 150 px away from outmost land collision, or less if land array ends earlier)
sheepluva
parents: 10198
diff changeset
   135
10198
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
   136
end.