hedgewars/uLandObjects.pas
author unc0rr
Fri, 16 Dec 2005 20:57:14 +0000
changeset 27 c374fe590272
parent 24 79c411363184
child 30 794e98e11b66
permissions -rw-r--r--
- improve land generation - don't use themes with no objects
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
24
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
     1
unit uLandObjects;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
     2
interface
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
     3
uses SDLh;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
     4
{$include options.inc}
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
     5
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
     6
procedure AddObjects(Surface: PSDL_Surface);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
     7
procedure BlitImageAndGenerateCollisionInfo(cpX, cpY: Longword; Image, Surface: PSDL_Surface);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
     8
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
     9
implementation
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    10
uses uLand, uStore, uConsts, uMisc, uConsole, uRandom;
27
c374fe590272 - improve land generation
unc0rr
parents: 24
diff changeset
    11
const MaxRects = 256;
c374fe590272 - improve land generation
unc0rr
parents: 24
diff changeset
    12
      MAXOBJECTRECTS = 16;
24
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    13
type  PRectArray = ^TRectsArray;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    14
      TRectsArray = array[0..MaxRects] of TSDL_rect;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    15
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    16
type TThemeObject = record
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    17
                    Surf: PSDL_Surface;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    18
                    inland: TSDL_Rect;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    19
                    outland: array[1..MAXOBJECTRECTS] of TSDL_Rect;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    20
                    rectcnt: Longword;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    21
                    Width, Height: Longword;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    22
                    end;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    23
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    24
var Rects: PRectArray;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    25
    RectCount: Longword;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    26
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    27
procedure BlitImageAndGenerateCollisionInfo(cpX, cpY: Longword; Image, Surface: PSDL_Surface);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    28
var i, p: LongWord;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    29
    x, y: Longword;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    30
    bpp: integer;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    31
    r: TSDL_Rect;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    32
begin
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    33
r.x:= cpX;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    34
r.y:= cpY;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    35
SDL_UpperBlit(Image, nil, Surface, @r);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    36
WriteToConsole('Generating collision info... ');
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    37
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    38
if SDL_MustLock(Image) then
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    39
   SDLTry(SDL_LockSurface(Image) >= 0, true);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    40
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    41
bpp:= Image.format.BytesPerPixel;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    42
WriteToConsole('('+inttostr(bpp)+') ');
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    43
p:= LongWord(Image.pixels);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    44
case bpp of
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    45
     1: OutError('We don''t work with 8 bit surfaces', true);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    46
     2: for y:= 0 to Pred(Image.h) do
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    47
            begin
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    48
            i:= Longword(@Land[cpY + y, cpX]);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    49
            for x:= 0 to Pred(Image.w) do
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    50
                if PWord(p + x * 2)^ <> 0 then PLongWord(i + x * 4)^:= $FFFFFF;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    51
            inc(p, Image.pitch);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    52
            end;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    53
     3: for y:= 0 to Pred(Image.h) do
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    54
            begin
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    55
            i:= Longword(@Land[cpY + y, cpX]);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    56
            for x:= 0 to Pred(Image.w) do
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    57
                if  (PByte(p + x * 3 + 0)^ <> 0)
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    58
                 or (PByte(p + x * 3 + 1)^ <> 0)
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    59
                 or (PByte(p + x * 3 + 2)^ <> 0) then PLongWord(i + x * 4)^:= $FFFFFF;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    60
            inc(p, Image.pitch);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    61
            end;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    62
     4: for y:= 0 to Pred(Image.h) do
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    63
            begin
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    64
            i:= Longword(@Land[cpY + y, cpX]);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    65
            for x:= 0 to Pred(Image.w) do
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    66
                if PLongword(p + x * 4)^ <> 0 then PLongWord(i + x * 4)^:= $FFFFFF;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    67
            inc(p, Image.pitch);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    68
            end;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    69
     end;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    70
if SDL_MustLock(Image) then
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    71
   SDL_UnlockSurface(Image);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    72
WriteLnToConsole(msgOK)
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    73
end;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    74
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    75
procedure AddRect(x1, y1, w1, h1: integer);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    76
begin
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    77
with Rects[RectCount] do
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    78
     begin
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    79
     x:= x1;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    80
     y:= y1;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    81
     w:= w1;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    82
     h:= h1
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    83
     end;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    84
inc(RectCount);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    85
TryDo(RectCount < MaxRects, 'AddRect: overflow', true)
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    86
end;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    87
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    88
procedure InitRects;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    89
begin
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    90
RectCount:= 0;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    91
New(Rects)
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    92
end;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    93
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    94
procedure FreeRects;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    95
begin
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    96
Dispose(rects)
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    97
end;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    98
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    99
function CheckIntersect(x1, y1, w1, h1: integer): boolean;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   100
var i: Longword;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   101
begin
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   102
Result:= false;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   103
i:= 0;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   104
if RectCount > 0 then
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   105
   repeat
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   106
   with Rects[i] do
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   107
        Result:= (x < x1 + w1) and (x1 < x + w) and
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   108
                 (y < y1 + h1) and (y1 < y + h);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   109
   inc(i)
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   110
   until (i = RectCount) or (Result)
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   111
end;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   112
27
c374fe590272 - improve land generation
unc0rr
parents: 24
diff changeset
   113
function AddGirder(gX: integer; Surface: PSDL_Surface): boolean;
24
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   114
var tmpsurf: PSDL_Surface;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   115
    x1, x2, y, k, i: integer;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   116
    r, rr: TSDL_Rect;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   117
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   118
    function CountNonZeroz(x, y: integer): Longword;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   119
    var i: integer;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   120
    begin
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   121
    Result:= 0;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   122
    for i:= y to y + 15 do
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   123
        if Land[i, x] <> 0 then inc(Result)
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   124
    end;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   125
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   126
begin
27
c374fe590272 - improve land generation
unc0rr
parents: 24
diff changeset
   127
y:= 150;
24
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   128
repeat
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   129
  inc(y, 24);
27
c374fe590272 - improve land generation
unc0rr
parents: 24
diff changeset
   130
  x1:= gX;
c374fe590272 - improve land generation
unc0rr
parents: 24
diff changeset
   131
  x2:= gX;
24
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   132
  while (x1 > 100) and (CountNonZeroz(x1, y) = 0) do dec(x1, 2);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   133
  i:= x1 - 12;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   134
  repeat
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   135
    k:= CountNonZeroz(x1, y);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   136
    dec(x1, 2)
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   137
  until (x1 < 100) or (k = 0) or (k = 16) or (x1 < i);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   138
  inc(x1, 2);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   139
  if k = 16 then
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   140
     begin
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   141
     while (x2 < 1900) and (CountNonZeroz(x2, y) = 0) do inc(x2, 2);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   142
     i:= x2 + 12;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   143
     repeat
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   144
       k:= CountNonZeroz(x2, y);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   145
       inc(x2, 2)
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   146
     until (x2 > 1900) or (k = 0) or (k = 16) or (x2 > i);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   147
     if (x2 < 1900) and (k = 16) and (x2 - x1 > 250)
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   148
        and not CheckIntersect(x1, y, x2 - x1, 16) then break;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   149
     end;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   150
x1:= 0;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   151
until y > 900;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   152
if x1 > 0 then
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   153
   begin
27
c374fe590272 - improve land generation
unc0rr
parents: 24
diff changeset
   154
   Result:= true;
24
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   155
   tmpsurf:= LoadImage(Pathz[ptGraphics] + 'Girder.png');
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   156
   rr.x:= x1;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   157
   rr.y:= y;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   158
   while rr.x + 100 < x2 do
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   159
         begin
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   160
         SDL_UpperBlit(tmpsurf, nil, Surface, @rr);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   161
         inc(rr.x, 100);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   162
         end;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   163
   r.x:= 0;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   164
   r.y:= 0;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   165
   r.w:= x2 - rr.x;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   166
   r.h:= 16;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   167
   SDL_UpperBlit(tmpsurf, @r, Surface, @rr);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   168
   SDL_FreeSurface(tmpsurf);
27
c374fe590272 - improve land generation
unc0rr
parents: 24
diff changeset
   169
   AddRect(x1 - 8, y - 8, x2 - x1 + 16, 72);
24
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   170
   for k:= y to y + 15 do
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   171
       for i:= x1 to x2 do Land[k, i]:= $FFFFFF
27
c374fe590272 - improve land generation
unc0rr
parents: 24
diff changeset
   172
   end else Result:= false
24
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   173
end;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   174
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   175
function CheckLand(rect: TSDL_Rect; dX, dY, Color: Longword): boolean;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   176
var i: Longword;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   177
begin
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   178
Result:= true;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   179
inc(rect.x, dX);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   180
inc(rect.y, dY);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   181
i:= 0;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   182
{$WARNINGS OFF}
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   183
while (i <= rect.w) and Result do
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   184
      begin
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   185
      Result:= (Land[rect.y, rect.x + i] = Color) and (Land[rect.y + rect.h, rect.x + i] = Color);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   186
      inc(i)
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   187
      end;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   188
i:= 0;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   189
while (i <= rect.h) and Result do
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   190
      begin
27
c374fe590272 - improve land generation
unc0rr
parents: 24
diff changeset
   191
      Result:= (Land[rect.y + i, rect.x] = Color) and (Land[rect.y + i, rect.x + rect.w] = Color);
24
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   192
      inc(i)
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   193
      end;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   194
{$WARNINGS ON}
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   195
end;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   196
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   197
function CheckCanPlace(x, y: Longword; var Obj: TThemeObject): boolean;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   198
var i: Longword;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   199
begin
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   200
with Obj do
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   201
     if CheckLand(inland, x, y, $FFFFFF) then
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   202
        begin
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   203
        Result:= true;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   204
        i:= 1;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   205
        while Result and (i <= rectcnt) do
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   206
              begin
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   207
              Result:= CheckLand(outland[i], x, y, 0);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   208
              inc(i)
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   209
              end;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   210
        if Result then
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   211
           Result:= not CheckIntersect(x, y, Width, Height)
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   212
        end else
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   213
        Result:= false
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   214
end;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   215
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   216
function TryPut(var Obj: TThemeObject; Surface: PSDL_Surface): boolean;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   217
const MaxPointsIndex = 2047;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   218
var x, y: Longword;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   219
    ar: array[0..MaxPointsIndex] of TPoint;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   220
    cnt, i: Longword;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   221
begin
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   222
cnt:= 0;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   223
with Obj do
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   224
     begin
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   225
     x:= 0;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   226
     repeat
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   227
         y:= 0;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   228
         repeat
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   229
             if CheckCanPlace(x, y, Obj) then
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   230
                begin
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   231
                ar[cnt].x:= x;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   232
                ar[cnt].y:= y;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   233
                inc(cnt);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   234
                if cnt > MaxPointsIndex then // buffer is full, do not check the rest land
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   235
                   begin
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   236
                   y:= 5000;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   237
                   x:= 5000;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   238
                   end
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   239
                end;
27
c374fe590272 - improve land generation
unc0rr
parents: 24
diff changeset
   240
             inc(y, 3);
24
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   241
         until y > 1023 - Height;
27
c374fe590272 - improve land generation
unc0rr
parents: 24
diff changeset
   242
         inc(x, getrandom(6) + 3)
24
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   243
     until x > 2047 - Width;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   244
     Result:= cnt <> 0;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   245
     if Result then
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   246
        begin
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   247
        i:= getrandom(cnt);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   248
        BlitImageAndGenerateCollisionInfo(ar[i].x, ar[i].y, Obj.Surf, Surface);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   249
        AddRect(ar[i].x, ar[i].y, Width, Height);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   250
        end
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   251
     end
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   252
end;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   253
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   254
procedure AddThemeObjects(Surface: PSDL_Surface; MaxCount: Longword);
27
c374fe590272 - improve land generation
unc0rr
parents: 24
diff changeset
   255
const MAXTHEMEOBJECTS = 32;
24
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   256
var f: textfile;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   257
    s: string;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   258
    ThemeObjects: array[1..MAXTHEMEOBJECTS] of TThemeObject;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   259
    i, ii, t, n: Longword;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   260
    b: boolean;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   261
begin
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   262
s:= Pathz[ptThemeCurrent] + cThemeCFGFilename;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   263
WriteLnToConsole('Adding objects...');
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   264
AssignFile(f, s);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   265
{$I-}
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   266
Reset(f);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   267
Readln(f, s); // skip color
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   268
Readln(f, n);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   269
for i:= 1 to n do
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   270
    begin
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   271
    Readln(f, s); // filename
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   272
    with ThemeObjects[i] do
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   273
         begin
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   274
         Surf:= LoadImage(Pathz[ptThemeCurrent] + s + '.png');
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   275
         Read(f, Width, Height);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   276
         with inland do Read(f, x, y, w, h);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   277
         Read(f, rectcnt);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   278
         for ii:= 1 to rectcnt do
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   279
             with outland[ii] do Read(f, x, y, w, h);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   280
         ReadLn(f)
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   281
         end;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   282
    end;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   283
Closefile(f);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   284
{$I+}
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   285
TryDo(IOResult = 0, 'Bad data or cannot access file', true);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   286
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   287
// loaded objects, try to put on land
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   288
if n = 0 then exit;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   289
i:= 1;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   290
repeat
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   291
    t:= getrandom(n) + 1;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   292
    ii:= t;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   293
    repeat
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   294
      inc(ii);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   295
      if ii > n then ii:= 1;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   296
      b:= TryPut(ThemeObjects[ii], Surface)
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   297
    until b or (ii = t);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   298
inc(i)
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   299
until (i > MaxCount) or not b
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   300
end;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   301
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   302
procedure AddObjects(Surface: PSDL_Surface);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   303
begin
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   304
InitRects;
27
c374fe590272 - improve land generation
unc0rr
parents: 24
diff changeset
   305
AddGirder(512, Surface);
c374fe590272 - improve land generation
unc0rr
parents: 24
diff changeset
   306
AddGirder(1024, Surface);
c374fe590272 - improve land generation
unc0rr
parents: 24
diff changeset
   307
AddGirder(1300, Surface);
c374fe590272 - improve land generation
unc0rr
parents: 24
diff changeset
   308
AddGirder(1536, Surface);
c374fe590272 - improve land generation
unc0rr
parents: 24
diff changeset
   309
AddThemeObjects(Surface, 8);
24
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   310
FreeRects
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   311
end;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   312
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   313
end.