hedgewars/uAmmos.pas
author unc0rr
Mon, 04 Jun 2007 21:29:05 +0000
changeset 537 4d2e60623e44
parent 534 92fb2b0d5117
child 545 f527450337c1
permissions -rw-r--r--
Fix typo
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
393
db01cc79f278 Update copyright information
unc0rr
parents: 371
diff changeset
     1
(*
db01cc79f278 Update copyright information
unc0rr
parents: 371
diff changeset
     2
 * Hedgewars, a worms-like game
db01cc79f278 Update copyright information
unc0rr
parents: 371
diff changeset
     3
 * Copyright (c) 2006, 2007 Andrey Korotaev <unC0Rr@gmail.com>
db01cc79f278 Update copyright information
unc0rr
parents: 371
diff changeset
     4
 *
db01cc79f278 Update copyright information
unc0rr
parents: 371
diff changeset
     5
 * This program is free software; you can redistribute it and/or modify
db01cc79f278 Update copyright information
unc0rr
parents: 371
diff changeset
     6
 * it under the terms of the GNU General Public License as published by
db01cc79f278 Update copyright information
unc0rr
parents: 371
diff changeset
     7
 * the Free Software Foundation; version 2 of the License
db01cc79f278 Update copyright information
unc0rr
parents: 371
diff changeset
     8
 *
db01cc79f278 Update copyright information
unc0rr
parents: 371
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
db01cc79f278 Update copyright information
unc0rr
parents: 371
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
db01cc79f278 Update copyright information
unc0rr
parents: 371
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
db01cc79f278 Update copyright information
unc0rr
parents: 371
diff changeset
    12
 * GNU General Public License for more details.
db01cc79f278 Update copyright information
unc0rr
parents: 371
diff changeset
    13
 *
db01cc79f278 Update copyright information
unc0rr
parents: 371
diff changeset
    14
 * You should have received a copy of the GNU General Public License
db01cc79f278 Update copyright information
unc0rr
parents: 371
diff changeset
    15
 * along with this program; if not, write to the Free Software
db01cc79f278 Update copyright information
unc0rr
parents: 371
diff changeset
    16
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
db01cc79f278 Update copyright information
unc0rr
parents: 371
diff changeset
    17
 *)
db01cc79f278 Update copyright information
unc0rr
parents: 371
diff changeset
    18
288
929c44745fd9 Ammo schemes and ammo stores support in engine
unc0rr
parents:
diff changeset
    19
unit uAmmos;
929c44745fd9 Ammo schemes and ammo stores support in engine
unc0rr
parents:
diff changeset
    20
interface
534
92fb2b0d5117 - Fix some bugs
unc0rr
parents: 394
diff changeset
    21
uses uConsts, uTeams;
288
929c44745fd9 Ammo schemes and ammo stores support in engine
unc0rr
parents:
diff changeset
    22
{$INCLUDE options.inc}
929c44745fd9 Ammo schemes and ammo stores support in engine
unc0rr
parents:
diff changeset
    23
929c44745fd9 Ammo schemes and ammo stores support in engine
unc0rr
parents:
diff changeset
    24
procedure AddAmmoStore(s: shortstring);
929c44745fd9 Ammo schemes and ammo stores support in engine
unc0rr
parents:
diff changeset
    25
procedure AssignStores;
295
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
    26
procedure AddAmmo(Hedgehog: pointer; ammo: TAmmoType);
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
    27
function  HHHasAmmo(Hedgehog: pointer; Ammo: TAmmoType): boolean;
371
731ad6d27bd1 integer -> LongInt
unc0rr
parents: 351
diff changeset
    28
procedure PackAmmo(Ammo: PHHAmmo; Slot: LongInt);
534
92fb2b0d5117 - Fix some bugs
unc0rr
parents: 394
diff changeset
    29
procedure OnUsedAmmo(var Hedgehog: THedgehog);
288
929c44745fd9 Ammo schemes and ammo stores support in engine
unc0rr
parents:
diff changeset
    30
929c44745fd9 Ammo schemes and ammo stores support in engine
unc0rr
parents:
diff changeset
    31
implementation
534
92fb2b0d5117 - Fix some bugs
unc0rr
parents: 394
diff changeset
    32
uses uMisc, uGears;
295
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
    33
type TAmmoCounts = array[TAmmoType] of Longword;
288
929c44745fd9 Ammo schemes and ammo stores support in engine
unc0rr
parents:
diff changeset
    34
var StoresList: array[0..Pred(cMaxHHs)] of PHHAmmo;
929c44745fd9 Ammo schemes and ammo stores support in engine
unc0rr
parents:
diff changeset
    35
    StoreCnt: Longword = 0;
929c44745fd9 Ammo schemes and ammo stores support in engine
unc0rr
parents:
diff changeset
    36
295
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
    37
procedure FillAmmoStore(Ammo: PHHAmmo; var cnts: TAmmoCounts);
288
929c44745fd9 Ammo schemes and ammo stores support in engine
unc0rr
parents:
diff changeset
    38
var mi: array[0..cMaxSlotIndex] of byte;
929c44745fd9 Ammo schemes and ammo stores support in engine
unc0rr
parents:
diff changeset
    39
    a: TAmmoType;
295
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
    40
begin
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
    41
FillChar(mi, sizeof(mi), 0);
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
    42
FillChar(Ammo^, sizeof(Ammo^), 0);
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
    43
for a:= Low(TAmmoType) to High(TAmmoType) do
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
    44
    if cnts[a] > 0 then
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
    45
       begin
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
    46
       TryDo(mi[Ammoz[a].Slot] <= cMaxSlotAmmoIndex, 'Ammo slot overflow', true);
351
29bc9c36ad5f Fixed-point arithmetics in engine.
unc0rr
parents: 295
diff changeset
    47
       Ammo^[Ammoz[a].Slot, mi[Ammoz[a].Slot]]:= Ammoz[a].Ammo;
29bc9c36ad5f Fixed-point arithmetics in engine.
unc0rr
parents: 295
diff changeset
    48
       Ammo^[Ammoz[a].Slot, mi[Ammoz[a].Slot]].Count:= cnts[a];
295
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
    49
       inc(mi[Ammoz[a].Slot])
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
    50
       end
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
    51
end;
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
    52
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
    53
procedure AddAmmoStore(s: shortstring);
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
    54
var cnt: Longword;
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
    55
    a: TAmmoType;
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
    56
    ammos: TAmmoCounts;
288
929c44745fd9 Ammo schemes and ammo stores support in engine
unc0rr
parents:
diff changeset
    57
begin
929c44745fd9 Ammo schemes and ammo stores support in engine
unc0rr
parents:
diff changeset
    58
TryDo(byte(s[0]) = byte(ord(High(TAmmoType)) + 1), 'Invalid ammo scheme (incompatible frontend)', true);
929c44745fd9 Ammo schemes and ammo stores support in engine
unc0rr
parents:
diff changeset
    59
929c44745fd9 Ammo schemes and ammo stores support in engine
unc0rr
parents:
diff changeset
    60
inc(StoreCnt);
929c44745fd9 Ammo schemes and ammo stores support in engine
unc0rr
parents:
diff changeset
    61
TryDo(StoreCnt <= cMaxHHs, 'Ammo stores overflow', true);
929c44745fd9 Ammo schemes and ammo stores support in engine
unc0rr
parents:
diff changeset
    62
929c44745fd9 Ammo schemes and ammo stores support in engine
unc0rr
parents:
diff changeset
    63
new(StoresList[Pred(StoreCnt)]);
929c44745fd9 Ammo schemes and ammo stores support in engine
unc0rr
parents:
diff changeset
    64
929c44745fd9 Ammo schemes and ammo stores support in engine
unc0rr
parents:
diff changeset
    65
for a:= Low(TAmmoType) to High(TAmmoType) do
929c44745fd9 Ammo schemes and ammo stores support in engine
unc0rr
parents:
diff changeset
    66
    begin
929c44745fd9 Ammo schemes and ammo stores support in engine
unc0rr
parents:
diff changeset
    67
    cnt:= byte(s[ord(a) + 1]) - byte('0');
295
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
    68
    if cnt = 9 then cnt:= AMMO_INFINITE;
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
    69
    ammos[a]:= cnt
288
929c44745fd9 Ammo schemes and ammo stores support in engine
unc0rr
parents:
diff changeset
    70
    end;
295
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
    71
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
    72
FillAmmoStore(StoresList[Pred(StoreCnt)], ammos)
288
929c44745fd9 Ammo schemes and ammo stores support in engine
unc0rr
parents:
diff changeset
    73
end;
929c44745fd9 Ammo schemes and ammo stores support in engine
unc0rr
parents:
diff changeset
    74
929c44745fd9 Ammo schemes and ammo stores support in engine
unc0rr
parents:
diff changeset
    75
function GetAmmoByNum(num: Longword): PHHAmmo;
929c44745fd9 Ammo schemes and ammo stores support in engine
unc0rr
parents:
diff changeset
    76
begin
929c44745fd9 Ammo schemes and ammo stores support in engine
unc0rr
parents:
diff changeset
    77
TryDo(num < StoreCnt, 'Invalid store number', true);
351
29bc9c36ad5f Fixed-point arithmetics in engine.
unc0rr
parents: 295
diff changeset
    78
exit(StoresList[num])
288
929c44745fd9 Ammo schemes and ammo stores support in engine
unc0rr
parents:
diff changeset
    79
end;
929c44745fd9 Ammo schemes and ammo stores support in engine
unc0rr
parents:
diff changeset
    80
929c44745fd9 Ammo schemes and ammo stores support in engine
unc0rr
parents:
diff changeset
    81
procedure AssignStores;
929c44745fd9 Ammo schemes and ammo stores support in engine
unc0rr
parents:
diff changeset
    82
var tteam: PTeam;
929c44745fd9 Ammo schemes and ammo stores support in engine
unc0rr
parents:
diff changeset
    83
    i: Longword;
929c44745fd9 Ammo schemes and ammo stores support in engine
unc0rr
parents:
diff changeset
    84
begin
929c44745fd9 Ammo schemes and ammo stores support in engine
unc0rr
parents:
diff changeset
    85
tteam:= TeamsList;
929c44745fd9 Ammo schemes and ammo stores support in engine
unc0rr
parents:
diff changeset
    86
while tteam <> nil do
929c44745fd9 Ammo schemes and ammo stores support in engine
unc0rr
parents:
diff changeset
    87
      begin
929c44745fd9 Ammo schemes and ammo stores support in engine
unc0rr
parents:
diff changeset
    88
      for i:= 0 to cMaxHHIndex do
351
29bc9c36ad5f Fixed-point arithmetics in engine.
unc0rr
parents: 295
diff changeset
    89
          if tteam^.Hedgehogs[i].Gear <> nil then
29bc9c36ad5f Fixed-point arithmetics in engine.
unc0rr
parents: 295
diff changeset
    90
             tteam^.Hedgehogs[i].Ammo:= GetAmmoByNum(tteam^.Hedgehogs[i].AmmoStore);
29bc9c36ad5f Fixed-point arithmetics in engine.
unc0rr
parents: 295
diff changeset
    91
      tteam:= tteam^.Next
288
929c44745fd9 Ammo schemes and ammo stores support in engine
unc0rr
parents:
diff changeset
    92
      end
929c44745fd9 Ammo schemes and ammo stores support in engine
unc0rr
parents:
diff changeset
    93
end;
929c44745fd9 Ammo schemes and ammo stores support in engine
unc0rr
parents:
diff changeset
    94
295
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
    95
procedure AddAmmo(Hedgehog: pointer; ammo: TAmmoType);
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
    96
var ammos: TAmmoCounts;
371
731ad6d27bd1 integer -> LongInt
unc0rr
parents: 351
diff changeset
    97
    slot, ami: LongInt;
295
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
    98
    hhammo: PHHAmmo;
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
    99
begin
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
   100
FillChar(ammos, sizeof(ammos), 0);
351
29bc9c36ad5f Fixed-point arithmetics in engine.
unc0rr
parents: 295
diff changeset
   101
hhammo:= PHedgehog(Hedgehog)^.Ammo;
295
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
   102
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
   103
for slot:= 0 to cMaxSlotIndex do
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
   104
    for ami:= 0 to cMaxSlotAmmoIndex do
351
29bc9c36ad5f Fixed-point arithmetics in engine.
unc0rr
parents: 295
diff changeset
   105
        if hhammo^[slot, ami].Count > 0 then
29bc9c36ad5f Fixed-point arithmetics in engine.
unc0rr
parents: 295
diff changeset
   106
           ammos[hhammo^[slot, ami].AmmoType]:= hhammo^[slot, ami].Count;
295
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
   107
394
4c017ae1226a - Implement hack to let ammo stores work without needed assistance of frontend
unc0rr
parents: 393
diff changeset
   108
if ammos[ammo] <> AMMO_INFINITE then inc(ammos[ammo], Ammoz[ammo].NumberInCase);
295
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
   109
FillAmmoStore(hhammo, ammos)
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
   110
end;
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
   111
371
731ad6d27bd1 integer -> LongInt
unc0rr
parents: 351
diff changeset
   112
procedure PackAmmo(Ammo: PHHAmmo; Slot: LongInt);
731ad6d27bd1 integer -> LongInt
unc0rr
parents: 351
diff changeset
   113
var ami: LongInt;
295
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
   114
    b: boolean;
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
   115
begin
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
   116
    repeat
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
   117
      b:= false;
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
   118
      ami:= 0;
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
   119
      while (not b) and (ami < cMaxSlotAmmoIndex) do
351
29bc9c36ad5f Fixed-point arithmetics in engine.
unc0rr
parents: 295
diff changeset
   120
          if (Ammo^[Slot, ami].Count = 0)
29bc9c36ad5f Fixed-point arithmetics in engine.
unc0rr
parents: 295
diff changeset
   121
             and (Ammo^[Slot, ami + 1].Count > 0) then b:= true
295
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
   122
                                                 else inc(ami);
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
   123
      if b then // there's a free item in ammo stack
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
   124
         begin
351
29bc9c36ad5f Fixed-point arithmetics in engine.
unc0rr
parents: 295
diff changeset
   125
         Ammo^[Slot, ami]:= Ammo^[Slot, ami + 1];
29bc9c36ad5f Fixed-point arithmetics in engine.
unc0rr
parents: 295
diff changeset
   126
         Ammo^[Slot, ami + 1].Count:= 0
295
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
   127
         end;
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
   128
    until not b;
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
   129
end;
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
   130
534
92fb2b0d5117 - Fix some bugs
unc0rr
parents: 394
diff changeset
   131
procedure OnUsedAmmo(var Hedgehog: THedgehog);
295
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
   132
var s, a: Longword;
534
92fb2b0d5117 - Fix some bugs
unc0rr
parents: 394
diff changeset
   133
    Ammo: PHHAmmo;
295
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
   134
begin
534
92fb2b0d5117 - Fix some bugs
unc0rr
parents: 394
diff changeset
   135
Ammo:= Hedgehog.Ammo;
92fb2b0d5117 - Fix some bugs
unc0rr
parents: 394
diff changeset
   136
with Hedgehog do
295
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
   137
     begin
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
   138
     if CurAmmoGear = nil then begin s:= CurSlot; a:= CurAmmo end
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
   139
                          else begin s:= AltSlot; a:= AltAmmo end;
351
29bc9c36ad5f Fixed-point arithmetics in engine.
unc0rr
parents: 295
diff changeset
   140
     with Ammo^[s, a] do
295
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
   141
          if Count <> AMMO_INFINITE then
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
   142
             begin
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
   143
             dec(Count);
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
   144
             if Count = 0 then PackAmmo(Ammo, CurSlot)
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
   145
             end
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
   146
     end
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
   147
end;
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
   148
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
   149
function  HHHasAmmo(Hedgehog: pointer; Ammo: TAmmoType): boolean;
371
731ad6d27bd1 integer -> LongInt
unc0rr
parents: 351
diff changeset
   150
var slot, ami: LongInt;
295
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
   151
begin
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
   152
Slot:= Ammoz[Ammo].Slot;
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
   153
ami:= 0;
351
29bc9c36ad5f Fixed-point arithmetics in engine.
unc0rr
parents: 295
diff changeset
   154
while (ami <= cMaxSlotAmmoIndex) do
295
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
   155
      begin
351
29bc9c36ad5f Fixed-point arithmetics in engine.
unc0rr
parents: 295
diff changeset
   156
      with PHedgehog(Hedgehog)^.Ammo^[Slot, ami] do
29bc9c36ad5f Fixed-point arithmetics in engine.
unc0rr
parents: 295
diff changeset
   157
            if (AmmoType = Ammo) and (Count > 0) then exit(true);
295
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
   158
      inc(ami)
351
29bc9c36ad5f Fixed-point arithmetics in engine.
unc0rr
parents: 295
diff changeset
   159
      end;
29bc9c36ad5f Fixed-point arithmetics in engine.
unc0rr
parents: 295
diff changeset
   160
HHHasAmmo:= false
295
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
   161
end;
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
   162
288
929c44745fd9 Ammo schemes and ammo stores support in engine
unc0rr
parents:
diff changeset
   163
end.