hedgewars/uLandObjects.pas
author unc0rr
Wed, 25 Jan 2006 19:45:39 +0000
changeset 56 a29135563e94
parent 54 839fd258ae6f
child 70 82d93eeecebe
permissions -rw-r--r--
- Replaced water sprite - Many small ammo fixes - Increased speed of placing land objects - New land template
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
51
b6e3ae05857f - Get rid of hwserv and runhelper
unc0rr
parents: 35
diff changeset
     1
(*
b6e3ae05857f - Get rid of hwserv and runhelper
unc0rr
parents: 35
diff changeset
     2
 * Hedgewars, a worms-like game
b6e3ae05857f - Get rid of hwserv and runhelper
unc0rr
parents: 35
diff changeset
     3
 * Copyright (c) 2005, 2006 Andrey Korotaev <unC0Rr@gmail.com>
b6e3ae05857f - Get rid of hwserv and runhelper
unc0rr
parents: 35
diff changeset
     4
 *
b6e3ae05857f - Get rid of hwserv and runhelper
unc0rr
parents: 35
diff changeset
     5
 * Distributed under the terms of the BSD-modified licence:
b6e3ae05857f - Get rid of hwserv and runhelper
unc0rr
parents: 35
diff changeset
     6
 *
b6e3ae05857f - Get rid of hwserv and runhelper
unc0rr
parents: 35
diff changeset
     7
 * Permission is hereby granted, free of charge, to any person obtaining a copy
b6e3ae05857f - Get rid of hwserv and runhelper
unc0rr
parents: 35
diff changeset
     8
 * of this software and associated documentation files (the "Software"), to deal
b6e3ae05857f - Get rid of hwserv and runhelper
unc0rr
parents: 35
diff changeset
     9
 * with the Software without restriction, including without limitation the
b6e3ae05857f - Get rid of hwserv and runhelper
unc0rr
parents: 35
diff changeset
    10
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
b6e3ae05857f - Get rid of hwserv and runhelper
unc0rr
parents: 35
diff changeset
    11
 * sell copies of the Software, and to permit persons to whom the Software is
b6e3ae05857f - Get rid of hwserv and runhelper
unc0rr
parents: 35
diff changeset
    12
 * furnished to do so, subject to the following conditions:
b6e3ae05857f - Get rid of hwserv and runhelper
unc0rr
parents: 35
diff changeset
    13
 *
b6e3ae05857f - Get rid of hwserv and runhelper
unc0rr
parents: 35
diff changeset
    14
 * 1. Redistributions of source code must retain the above copyright notice,
b6e3ae05857f - Get rid of hwserv and runhelper
unc0rr
parents: 35
diff changeset
    15
 *    this list of conditions and the following disclaimer.
b6e3ae05857f - Get rid of hwserv and runhelper
unc0rr
parents: 35
diff changeset
    16
 * 2. Redistributions in binary form must reproduce the above copyright notice,
b6e3ae05857f - Get rid of hwserv and runhelper
unc0rr
parents: 35
diff changeset
    17
 *    this list of conditions and the following disclaimer in the documentation
b6e3ae05857f - Get rid of hwserv and runhelper
unc0rr
parents: 35
diff changeset
    18
 *    and/or other materials provided with the distribution.
b6e3ae05857f - Get rid of hwserv and runhelper
unc0rr
parents: 35
diff changeset
    19
 * 3. The name of the author may not be used to endorse or promote products
b6e3ae05857f - Get rid of hwserv and runhelper
unc0rr
parents: 35
diff changeset
    20
 *    derived from this software without specific prior written permission.
b6e3ae05857f - Get rid of hwserv and runhelper
unc0rr
parents: 35
diff changeset
    21
 *
b6e3ae05857f - Get rid of hwserv and runhelper
unc0rr
parents: 35
diff changeset
    22
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
b6e3ae05857f - Get rid of hwserv and runhelper
unc0rr
parents: 35
diff changeset
    23
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
b6e3ae05857f - Get rid of hwserv and runhelper
unc0rr
parents: 35
diff changeset
    24
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
b6e3ae05857f - Get rid of hwserv and runhelper
unc0rr
parents: 35
diff changeset
    25
 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
b6e3ae05857f - Get rid of hwserv and runhelper
unc0rr
parents: 35
diff changeset
    26
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
b6e3ae05857f - Get rid of hwserv and runhelper
unc0rr
parents: 35
diff changeset
    27
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
b6e3ae05857f - Get rid of hwserv and runhelper
unc0rr
parents: 35
diff changeset
    28
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
b6e3ae05857f - Get rid of hwserv and runhelper
unc0rr
parents: 35
diff changeset
    29
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
b6e3ae05857f - Get rid of hwserv and runhelper
unc0rr
parents: 35
diff changeset
    30
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
b6e3ae05857f - Get rid of hwserv and runhelper
unc0rr
parents: 35
diff changeset
    31
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
b6e3ae05857f - Get rid of hwserv and runhelper
unc0rr
parents: 35
diff changeset
    32
 *)
b6e3ae05857f - Get rid of hwserv and runhelper
unc0rr
parents: 35
diff changeset
    33
24
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    34
unit uLandObjects;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    35
interface
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    36
uses SDLh;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    37
{$include options.inc}
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    38
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    39
procedure AddObjects(Surface: PSDL_Surface);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    40
procedure BlitImageAndGenerateCollisionInfo(cpX, cpY: Longword; Image, Surface: PSDL_Surface);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    41
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    42
implementation
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    43
uses uLand, uStore, uConsts, uMisc, uConsole, uRandom;
27
c374fe590272 - improve land generation
unc0rr
parents: 24
diff changeset
    44
const MaxRects = 256;
c374fe590272 - improve land generation
unc0rr
parents: 24
diff changeset
    45
      MAXOBJECTRECTS = 16;
24
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    46
type  PRectArray = ^TRectsArray;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    47
      TRectsArray = array[0..MaxRects] of TSDL_rect;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    48
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    49
type TThemeObject = record
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    50
                    Surf: PSDL_Surface;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    51
                    inland: TSDL_Rect;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    52
                    outland: array[1..MAXOBJECTRECTS] of TSDL_Rect;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    53
                    rectcnt: Longword;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    54
                    Width, Height: Longword;
56
a29135563e94 - Replaced water sprite
unc0rr
parents: 54
diff changeset
    55
                    Maxcnt: Longword;
24
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    56
                    end;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    57
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    58
var Rects: PRectArray;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    59
    RectCount: Longword;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    60
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    61
procedure BlitImageAndGenerateCollisionInfo(cpX, cpY: Longword; Image, Surface: PSDL_Surface);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    62
var i, p: LongWord;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    63
    x, y: Longword;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    64
    bpp: integer;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    65
    r: TSDL_Rect;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    66
begin
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    67
r.x:= cpX;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    68
r.y:= cpY;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    69
SDL_UpperBlit(Image, nil, Surface, @r);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    70
WriteToConsole('Generating collision info... ');
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    71
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    72
if SDL_MustLock(Image) then
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    73
   SDLTry(SDL_LockSurface(Image) >= 0, true);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    74
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    75
bpp:= Image.format.BytesPerPixel;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    76
WriteToConsole('('+inttostr(bpp)+') ');
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    77
p:= LongWord(Image.pixels);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    78
case bpp of
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    79
     1: OutError('We don''t work with 8 bit surfaces', true);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    80
     2: for y:= 0 to Pred(Image.h) do
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    81
            begin
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    82
            i:= Longword(@Land[cpY + y, cpX]);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    83
            for x:= 0 to Pred(Image.w) do
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    84
                if PWord(p + x * 2)^ <> 0 then PLongWord(i + x * 4)^:= $FFFFFF;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    85
            inc(p, Image.pitch);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    86
            end;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    87
     3: for y:= 0 to Pred(Image.h) do
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    88
            begin
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    89
            i:= Longword(@Land[cpY + y, cpX]);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    90
            for x:= 0 to Pred(Image.w) do
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    91
                if  (PByte(p + x * 3 + 0)^ <> 0)
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    92
                 or (PByte(p + x * 3 + 1)^ <> 0)
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    93
                 or (PByte(p + x * 3 + 2)^ <> 0) then PLongWord(i + x * 4)^:= $FFFFFF;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    94
            inc(p, Image.pitch);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    95
            end;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    96
     4: for y:= 0 to Pred(Image.h) do
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    97
            begin
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    98
            i:= Longword(@Land[cpY + y, cpX]);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    99
            for x:= 0 to Pred(Image.w) do
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   100
                if PLongword(p + x * 4)^ <> 0 then PLongWord(i + x * 4)^:= $FFFFFF;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   101
            inc(p, Image.pitch);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   102
            end;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   103
     end;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   104
if SDL_MustLock(Image) then
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   105
   SDL_UnlockSurface(Image);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   106
WriteLnToConsole(msgOK)
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   107
end;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   108
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   109
procedure AddRect(x1, y1, w1, h1: integer);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   110
begin
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   111
with Rects[RectCount] do
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   112
     begin
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   113
     x:= x1;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   114
     y:= y1;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   115
     w:= w1;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   116
     h:= h1
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   117
     end;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   118
inc(RectCount);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   119
TryDo(RectCount < MaxRects, 'AddRect: overflow', true)
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   120
end;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   121
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   122
procedure InitRects;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   123
begin
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   124
RectCount:= 0;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   125
New(Rects)
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   126
end;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   127
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   128
procedure FreeRects;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   129
begin
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   130
Dispose(rects)
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   131
end;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   132
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   133
function CheckIntersect(x1, y1, w1, h1: integer): boolean;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   134
var i: Longword;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   135
begin
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   136
Result:= false;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   137
i:= 0;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   138
if RectCount > 0 then
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   139
   repeat
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   140
   with Rects[i] do
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   141
        Result:= (x < x1 + w1) and (x1 < x + w) and
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   142
                 (y < y1 + h1) and (y1 < y + h);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   143
   inc(i)
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   144
   until (i = RectCount) or (Result)
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   145
end;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   146
27
c374fe590272 - improve land generation
unc0rr
parents: 24
diff changeset
   147
function AddGirder(gX: integer; Surface: PSDL_Surface): boolean;
24
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   148
var tmpsurf: PSDL_Surface;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   149
    x1, x2, y, k, i: integer;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   150
    r, rr: TSDL_Rect;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   151
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   152
    function CountNonZeroz(x, y: integer): Longword;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   153
    var i: integer;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   154
    begin
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   155
    Result:= 0;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   156
    for i:= y to y + 15 do
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   157
        if Land[i, x] <> 0 then inc(Result)
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   158
    end;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   159
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   160
begin
27
c374fe590272 - improve land generation
unc0rr
parents: 24
diff changeset
   161
y:= 150;
24
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   162
repeat
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   163
  inc(y, 24);
27
c374fe590272 - improve land generation
unc0rr
parents: 24
diff changeset
   164
  x1:= gX;
c374fe590272 - improve land generation
unc0rr
parents: 24
diff changeset
   165
  x2:= gX;
24
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   166
  while (x1 > 100) and (CountNonZeroz(x1, y) = 0) do dec(x1, 2);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   167
  i:= x1 - 12;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   168
  repeat
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   169
    k:= CountNonZeroz(x1, y);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   170
    dec(x1, 2)
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   171
  until (x1 < 100) or (k = 0) or (k = 16) or (x1 < i);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   172
  inc(x1, 2);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   173
  if k = 16 then
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   174
     begin
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   175
     while (x2 < 1900) and (CountNonZeroz(x2, y) = 0) do inc(x2, 2);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   176
     i:= x2 + 12;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   177
     repeat
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   178
       k:= CountNonZeroz(x2, y);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   179
       inc(x2, 2)
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   180
     until (x2 > 1900) or (k = 0) or (k = 16) or (x2 > i);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   181
     if (x2 < 1900) and (k = 16) and (x2 - x1 > 250)
30
794e98e11b66 - Fixed slow sprite blt
unc0rr
parents: 27
diff changeset
   182
        and not CheckIntersect(x1 - 32, y - 64, x2 - x1 + 64, 144) then break;
24
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   183
     end;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   184
x1:= 0;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   185
until y > 900;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   186
if x1 > 0 then
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   187
   begin
27
c374fe590272 - improve land generation
unc0rr
parents: 24
diff changeset
   188
   Result:= true;
54
839fd258ae6f - Fixed game loading
unc0rr
parents: 51
diff changeset
   189
   tmpsurf:= LoadImage(Pathz[ptGraphics] + '/Girder.png', false);
24
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   190
   rr.x:= x1;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   191
   rr.y:= y;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   192
   while rr.x + 100 < x2 do
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   193
         begin
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   194
         SDL_UpperBlit(tmpsurf, nil, Surface, @rr);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   195
         inc(rr.x, 100);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   196
         end;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   197
   r.x:= 0;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   198
   r.y:= 0;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   199
   r.w:= x2 - rr.x;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   200
   r.h:= 16;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   201
   SDL_UpperBlit(tmpsurf, @r, Surface, @rr);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   202
   SDL_FreeSurface(tmpsurf);
30
794e98e11b66 - Fixed slow sprite blt
unc0rr
parents: 27
diff changeset
   203
   AddRect(x1 - 8, y - 32, x2 - x1 + 16, 80);
24
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   204
   for k:= y to y + 15 do
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   205
       for i:= x1 to x2 do Land[k, i]:= $FFFFFF
27
c374fe590272 - improve land generation
unc0rr
parents: 24
diff changeset
   206
   end else Result:= false
24
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   207
end;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   208
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   209
function CheckLand(rect: TSDL_Rect; dX, dY, Color: Longword): boolean;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   210
var i: Longword;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   211
begin
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   212
Result:= true;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   213
inc(rect.x, dX);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   214
inc(rect.y, dY);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   215
i:= 0;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   216
{$WARNINGS OFF}
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   217
while (i <= rect.w) and Result do
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   218
      begin
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   219
      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
   220
      inc(i)
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   221
      end;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   222
i:= 0;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   223
while (i <= rect.h) and Result do
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   224
      begin
27
c374fe590272 - improve land generation
unc0rr
parents: 24
diff changeset
   225
      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
   226
      inc(i)
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   227
      end;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   228
{$WARNINGS ON}
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   229
end;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   230
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   231
function CheckCanPlace(x, y: Longword; var Obj: TThemeObject): boolean;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   232
var i: Longword;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   233
begin
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   234
with Obj do
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   235
     if CheckLand(inland, x, y, $FFFFFF) then
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   236
        begin
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   237
        Result:= true;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   238
        i:= 1;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   239
        while Result and (i <= rectcnt) do
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   240
              begin
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   241
              Result:= CheckLand(outland[i], x, y, 0);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   242
              inc(i)
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   243
              end;
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
           Result:= not CheckIntersect(x, y, Width, Height)
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   246
        end else
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   247
        Result:= false
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   248
end;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   249
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   250
function TryPut(var Obj: TThemeObject; Surface: PSDL_Surface): boolean;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   251
const MaxPointsIndex = 2047;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   252
var x, y: Longword;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   253
    ar: array[0..MaxPointsIndex] of TPoint;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   254
    cnt, i: Longword;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   255
begin
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   256
cnt:= 0;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   257
with Obj do
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   258
     begin
56
a29135563e94 - Replaced water sprite
unc0rr
parents: 54
diff changeset
   259
     if Maxcnt = 0 then
a29135563e94 - Replaced water sprite
unc0rr
parents: 54
diff changeset
   260
        begin
a29135563e94 - Replaced water sprite
unc0rr
parents: 54
diff changeset
   261
        Result:= false;
a29135563e94 - Replaced water sprite
unc0rr
parents: 54
diff changeset
   262
        exit
a29135563e94 - Replaced water sprite
unc0rr
parents: 54
diff changeset
   263
        end;
24
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   264
     x:= 0;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   265
     repeat
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   266
         y:= 0;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   267
         repeat
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   268
             if CheckCanPlace(x, y, Obj) then
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   269
                begin
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   270
                ar[cnt].x:= x;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   271
                ar[cnt].y:= y;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   272
                inc(cnt);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   273
                if cnt > MaxPointsIndex then // buffer is full, do not check the rest land
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   274
                   begin
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   275
                   y:= 5000;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   276
                   x:= 5000;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   277
                   end
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   278
                end;
27
c374fe590272 - improve land generation
unc0rr
parents: 24
diff changeset
   279
             inc(y, 3);
24
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   280
         until y > 1023 - Height;
27
c374fe590272 - improve land generation
unc0rr
parents: 24
diff changeset
   281
         inc(x, getrandom(6) + 3)
24
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   282
     until x > 2047 - Width;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   283
     Result:= cnt <> 0;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   284
     if Result then
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   285
        begin
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   286
        i:= getrandom(cnt);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   287
        BlitImageAndGenerateCollisionInfo(ar[i].x, ar[i].y, Obj.Surf, Surface);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   288
        AddRect(ar[i].x, ar[i].y, Width, Height);
56
a29135563e94 - Replaced water sprite
unc0rr
parents: 54
diff changeset
   289
        dec(Maxcnt)
a29135563e94 - Replaced water sprite
unc0rr
parents: 54
diff changeset
   290
        end else Maxcnt:= 0
24
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   291
     end
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   292
end;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   293
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   294
procedure AddThemeObjects(Surface: PSDL_Surface; MaxCount: Longword);
27
c374fe590272 - improve land generation
unc0rr
parents: 24
diff changeset
   295
const MAXTHEMEOBJECTS = 32;
24
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   296
var f: textfile;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   297
    s: string;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   298
    ThemeObjects: array[1..MAXTHEMEOBJECTS] of TThemeObject;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   299
    i, ii, t, n: Longword;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   300
    b: boolean;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   301
begin
54
839fd258ae6f - Fixed game loading
unc0rr
parents: 51
diff changeset
   302
s:= Pathz[ptThemeCurrent] + '/' + cThemeCFGFilename;
24
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   303
WriteLnToConsole('Adding objects...');
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   304
AssignFile(f, s);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   305
{$I-}
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   306
Reset(f);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   307
Readln(f, s); // skip color
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   308
Readln(f, n);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   309
for i:= 1 to n do
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   310
    begin
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   311
    Readln(f, s); // filename
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   312
    with ThemeObjects[i] do
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   313
         begin
54
839fd258ae6f - Fixed game loading
unc0rr
parents: 51
diff changeset
   314
         Surf:= LoadImage(Pathz[ptThemeCurrent] + '/' + s + '.png', false);
24
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   315
         Read(f, Width, Height);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   316
         with inland do Read(f, x, y, w, h);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   317
         Read(f, rectcnt);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   318
         for ii:= 1 to rectcnt do
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   319
             with outland[ii] do Read(f, x, y, w, h);
56
a29135563e94 - Replaced water sprite
unc0rr
parents: 54
diff changeset
   320
         Maxcnt:= 2;
24
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   321
         ReadLn(f)
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   322
         end;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   323
    end;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   324
Closefile(f);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   325
{$I+}
54
839fd258ae6f - Fixed game loading
unc0rr
parents: 51
diff changeset
   326
TryDo(IOResult = 0, 'Bad data or cannot access file ' + cThemeCFGFilename, true);
24
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   327
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   328
// loaded objects, try to put on land
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   329
if n = 0 then exit;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   330
i:= 1;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   331
repeat
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   332
    t:= getrandom(n) + 1;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   333
    ii:= t;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   334
    repeat
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   335
      inc(ii);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   336
      if ii > n then ii:= 1;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   337
      b:= TryPut(ThemeObjects[ii], Surface)
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   338
    until b or (ii = t);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   339
inc(i)
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   340
until (i > MaxCount) or not b
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   341
end;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   342
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   343
procedure AddObjects(Surface: PSDL_Surface);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   344
begin
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   345
InitRects;
27
c374fe590272 - improve land generation
unc0rr
parents: 24
diff changeset
   346
AddGirder(512, Surface);
c374fe590272 - improve land generation
unc0rr
parents: 24
diff changeset
   347
AddGirder(1024, Surface);
c374fe590272 - improve land generation
unc0rr
parents: 24
diff changeset
   348
AddGirder(1300, Surface);
c374fe590272 - improve land generation
unc0rr
parents: 24
diff changeset
   349
AddGirder(1536, Surface);
c374fe590272 - improve land generation
unc0rr
parents: 24
diff changeset
   350
AddThemeObjects(Surface, 8);
24
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   351
FreeRects
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   352
end;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   353
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   354
end.