hedgewars/uLandObjects.pas
author unc0rr
Mon, 05 Dec 2005 21:46:15 +0000
changeset 24 79c411363184
child 27 c374fe590272
permissions -rw-r--r--
Add theme objects to land
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;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    11
const MaxRects = 1024;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    12
      MAXOBJECTRECTS = 32;
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
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   113
procedure AddGirders(Surface: PSDL_Surface);
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
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   127
y:= 256;
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);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   130
  x1:= 1024;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   131
  x2:= 1024;
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
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   154
   tmpsurf:= LoadImage(Pathz[ptGraphics] + 'Girder.png');
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   155
   rr.x:= x1;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   156
   rr.y:= y;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   157
   while rr.x + 100 < x2 do
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   158
         begin
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   159
         SDL_UpperBlit(tmpsurf, nil, Surface, @rr);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   160
         inc(rr.x, 100);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   161
         end;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   162
   r.x:= 0;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   163
   r.y:= 0;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   164
   r.w:= x2 - rr.x;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   165
   r.h:= 16;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   166
   SDL_UpperBlit(tmpsurf, @r, Surface, @rr);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   167
   SDL_FreeSurface(tmpsurf);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   168
   AddRect(x1 - 8, y - 8, x2 - x1 + 8, 32);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   169
   for k:= y to y + 15 do
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   170
       for i:= x1 to x2 do Land[k, i]:= $FFFFFF
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   171
   end
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   172
end;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   173
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   174
function CheckLand(rect: TSDL_Rect; dX, dY, Color: Longword): boolean;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   175
var i: Longword;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   176
begin
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   177
Result:= true;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   178
inc(rect.x, dX);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   179
inc(rect.y, dY);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   180
i:= 0;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   181
{$WARNINGS OFF}
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   182
while (i <= rect.w) and Result do
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   183
      begin
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   184
      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
   185
      inc(i)
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   186
      end;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   187
i:= 0;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   188
while (i <= rect.h) and Result do
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   189
      begin
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   190
      Result:= (Land[rect.y + i, rect.x] = Color) or (Land[rect.y + i, rect.x + rect.w] = Color);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   191
      inc(i)
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   192
      end;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   193
{$WARNINGS ON}
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   194
end;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   195
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   196
function CheckCanPlace(x, y: Longword; var Obj: TThemeObject): boolean;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   197
var i: Longword;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   198
begin
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   199
with Obj do
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   200
     if CheckLand(inland, x, y, $FFFFFF) then
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   201
        begin
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   202
        Result:= true;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   203
        i:= 1;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   204
        while Result and (i <= rectcnt) do
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   205
              begin
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   206
              Result:= CheckLand(outland[i], x, y, 0);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   207
              inc(i)
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   208
              end;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   209
        if Result then
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   210
           Result:= not CheckIntersect(x, y, Width, Height)
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   211
        end else
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   212
        Result:= false
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   213
end;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   214
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   215
function TryPut(var Obj: TThemeObject; Surface: PSDL_Surface): boolean;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   216
const MaxPointsIndex = 2047;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   217
var x, y: Longword;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   218
    ar: array[0..MaxPointsIndex] of TPoint;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   219
    cnt, i: Longword;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   220
begin
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   221
cnt:= 0;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   222
with Obj do
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   223
     begin
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   224
     x:= 0;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   225
     repeat
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   226
         y:= 0;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   227
         repeat
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   228
             if CheckCanPlace(x, y, Obj) then
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   229
                begin
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   230
                ar[cnt].x:= x;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   231
                ar[cnt].y:= y;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   232
                inc(cnt);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   233
                if cnt > MaxPointsIndex then // buffer is full, do not check the rest land
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   234
                   begin
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   235
                   y:= 5000;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   236
                   x:= 5000;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   237
                   end
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   238
                end;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   239
             inc(y, 2);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   240
         until y > 1023 - Height;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   241
         inc(x, getrandom(8) + 2)
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   242
     until x > 2047 - Width;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   243
     Result:= cnt <> 0;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   244
     if Result then
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   245
        begin
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   246
        i:= getrandom(cnt);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   247
        BlitImageAndGenerateCollisionInfo(ar[i].x, ar[i].y, Obj.Surf, Surface);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   248
        AddRect(ar[i].x, ar[i].y, Width, Height);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   249
        end
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
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   253
procedure AddThemeObjects(Surface: PSDL_Surface; MaxCount: Longword);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   254
const MAXTHEMEOBJECTS = 16;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   255
var f: textfile;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   256
    s: string;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   257
    ThemeObjects: array[1..MAXTHEMEOBJECTS] of TThemeObject;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   258
    i, ii, t, n: Longword;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   259
    b: boolean;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   260
begin
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   261
s:= Pathz[ptThemeCurrent] + cThemeCFGFilename;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   262
WriteLnToConsole('Adding objects...');
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   263
AssignFile(f, s);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   264
{$I-}
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   265
Reset(f);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   266
Readln(f, s); // skip color
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   267
Readln(f, n);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   268
for i:= 1 to n do
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   269
    begin
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   270
    Readln(f, s); // filename
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   271
    with ThemeObjects[i] do
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   272
         begin
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   273
         Surf:= LoadImage(Pathz[ptThemeCurrent] + s + '.png');
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   274
         Read(f, Width, Height);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   275
         with inland do Read(f, x, y, w, h);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   276
         Read(f, rectcnt);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   277
         for ii:= 1 to rectcnt do
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   278
             with outland[ii] do Read(f, x, y, w, h);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   279
         ReadLn(f)
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   280
         end;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   281
    end;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   282
Closefile(f);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   283
{$I+}
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   284
TryDo(IOResult = 0, 'Bad data or cannot access file', true);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   285
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   286
// loaded objects, try to put on land
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   287
if n = 0 then exit;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   288
i:= 1;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   289
repeat
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   290
    t:= getrandom(n) + 1;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   291
    ii:= t;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   292
    repeat
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   293
      inc(ii);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   294
      if ii > n then ii:= 1;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   295
      b:= TryPut(ThemeObjects[ii], Surface)
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   296
    until b or (ii = t);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   297
inc(i)
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   298
until (i > MaxCount) or not b
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   299
end;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   300
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   301
procedure AddObjects(Surface: PSDL_Surface);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   302
begin
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   303
InitRects;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   304
AddGirders(Surface);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   305
AddThemeObjects(Surface, 5);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   306
FreeRects
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   307
end;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   308
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   309
end.