hedgewars/uAmmos.pas
author unc0rr
Thu, 11 Jan 2007 20:45:59 +0000
changeset 316 57d50189ad86
parent 295 8834f3cb620e
child 351 29bc9c36ad5f
permissions -rw-r--r--
Calculate land digest
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
288
929c44745fd9 Ammo schemes and ammo stores support in engine
unc0rr
parents:
diff changeset
     1
unit uAmmos;
929c44745fd9 Ammo schemes and ammo stores support in engine
unc0rr
parents:
diff changeset
     2
interface
929c44745fd9 Ammo schemes and ammo stores support in engine
unc0rr
parents:
diff changeset
     3
uses uConsts;
929c44745fd9 Ammo schemes and ammo stores support in engine
unc0rr
parents:
diff changeset
     4
{$INCLUDE options.inc}
929c44745fd9 Ammo schemes and ammo stores support in engine
unc0rr
parents:
diff changeset
     5
type PHHAmmo = ^THHAmmo;
929c44745fd9 Ammo schemes and ammo stores support in engine
unc0rr
parents:
diff changeset
     6
     THHAmmo = array[0..cMaxSlotIndex, 0..cMaxSlotAmmoIndex] of TAmmo;
929c44745fd9 Ammo schemes and ammo stores support in engine
unc0rr
parents:
diff changeset
     7
929c44745fd9 Ammo schemes and ammo stores support in engine
unc0rr
parents:
diff changeset
     8
procedure AddAmmoStore(s: shortstring);
929c44745fd9 Ammo schemes and ammo stores support in engine
unc0rr
parents:
diff changeset
     9
procedure AssignStores;
295
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
    10
procedure AddAmmo(Hedgehog: pointer; ammo: TAmmoType);
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
    11
function  HHHasAmmo(Hedgehog: pointer; Ammo: TAmmoType): boolean;
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
    12
procedure PackAmmo(Ammo: PHHAmmo; Slot: integer);
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
    13
procedure OnUsedAmmo(Ammo: PHHAmmo);
288
929c44745fd9 Ammo schemes and ammo stores support in engine
unc0rr
parents:
diff changeset
    14
929c44745fd9 Ammo schemes and ammo stores support in engine
unc0rr
parents:
diff changeset
    15
implementation
295
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
    16
uses uMisc, uTeams, uGears;
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
    17
type TAmmoCounts = array[TAmmoType] of Longword;
288
929c44745fd9 Ammo schemes and ammo stores support in engine
unc0rr
parents:
diff changeset
    18
var StoresList: array[0..Pred(cMaxHHs)] of PHHAmmo;
929c44745fd9 Ammo schemes and ammo stores support in engine
unc0rr
parents:
diff changeset
    19
    StoreCnt: Longword = 0;
929c44745fd9 Ammo schemes and ammo stores support in engine
unc0rr
parents:
diff changeset
    20
295
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
    21
procedure FillAmmoStore(Ammo: PHHAmmo; var cnts: TAmmoCounts);
288
929c44745fd9 Ammo schemes and ammo stores support in engine
unc0rr
parents:
diff changeset
    22
var mi: array[0..cMaxSlotIndex] of byte;
929c44745fd9 Ammo schemes and ammo stores support in engine
unc0rr
parents:
diff changeset
    23
    a: TAmmoType;
295
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
    24
begin
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
    25
FillChar(mi, sizeof(mi), 0);
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
    26
FillChar(Ammo^, sizeof(Ammo^), 0);
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
    27
for a:= Low(TAmmoType) to High(TAmmoType) do
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
    28
    if cnts[a] > 0 then
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
    29
       begin
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
    30
       TryDo(mi[Ammoz[a].Slot] <= cMaxSlotAmmoIndex, 'Ammo slot overflow', true);
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
    31
       Ammo[Ammoz[a].Slot, mi[Ammoz[a].Slot]]:= Ammoz[a].Ammo;
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
    32
       Ammo[Ammoz[a].Slot, mi[Ammoz[a].Slot]].Count:= cnts[a];
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
    33
       inc(mi[Ammoz[a].Slot])
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
    34
       end
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
    35
end;
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
    36
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
    37
procedure AddAmmoStore(s: shortstring);
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
    38
var cnt: Longword;
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
    39
    a: TAmmoType;
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
    40
    ammos: TAmmoCounts;
288
929c44745fd9 Ammo schemes and ammo stores support in engine
unc0rr
parents:
diff changeset
    41
begin
929c44745fd9 Ammo schemes and ammo stores support in engine
unc0rr
parents:
diff changeset
    42
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
    43
929c44745fd9 Ammo schemes and ammo stores support in engine
unc0rr
parents:
diff changeset
    44
inc(StoreCnt);
929c44745fd9 Ammo schemes and ammo stores support in engine
unc0rr
parents:
diff changeset
    45
TryDo(StoreCnt <= cMaxHHs, 'Ammo stores overflow', true);
929c44745fd9 Ammo schemes and ammo stores support in engine
unc0rr
parents:
diff changeset
    46
929c44745fd9 Ammo schemes and ammo stores support in engine
unc0rr
parents:
diff changeset
    47
new(StoresList[Pred(StoreCnt)]);
929c44745fd9 Ammo schemes and ammo stores support in engine
unc0rr
parents:
diff changeset
    48
929c44745fd9 Ammo schemes and ammo stores support in engine
unc0rr
parents:
diff changeset
    49
for a:= Low(TAmmoType) to High(TAmmoType) do
929c44745fd9 Ammo schemes and ammo stores support in engine
unc0rr
parents:
diff changeset
    50
    begin
929c44745fd9 Ammo schemes and ammo stores support in engine
unc0rr
parents:
diff changeset
    51
    cnt:= byte(s[ord(a) + 1]) - byte('0');
295
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
    52
    if cnt = 9 then cnt:= AMMO_INFINITE;
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
    53
    ammos[a]:= cnt
288
929c44745fd9 Ammo schemes and ammo stores support in engine
unc0rr
parents:
diff changeset
    54
    end;
295
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
    55
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
    56
FillAmmoStore(StoresList[Pred(StoreCnt)], ammos)
288
929c44745fd9 Ammo schemes and ammo stores support in engine
unc0rr
parents:
diff changeset
    57
end;
929c44745fd9 Ammo schemes and ammo stores support in engine
unc0rr
parents:
diff changeset
    58
929c44745fd9 Ammo schemes and ammo stores support in engine
unc0rr
parents:
diff changeset
    59
function GetAmmoByNum(num: Longword): PHHAmmo;
929c44745fd9 Ammo schemes and ammo stores support in engine
unc0rr
parents:
diff changeset
    60
begin
929c44745fd9 Ammo schemes and ammo stores support in engine
unc0rr
parents:
diff changeset
    61
TryDo(num < StoreCnt, 'Invalid store number', true);
929c44745fd9 Ammo schemes and ammo stores support in engine
unc0rr
parents:
diff changeset
    62
Result:= StoresList[num]
929c44745fd9 Ammo schemes and ammo stores support in engine
unc0rr
parents:
diff changeset
    63
end;
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
procedure AssignStores;
929c44745fd9 Ammo schemes and ammo stores support in engine
unc0rr
parents:
diff changeset
    66
var tteam: PTeam;
929c44745fd9 Ammo schemes and ammo stores support in engine
unc0rr
parents:
diff changeset
    67
    i: Longword;
929c44745fd9 Ammo schemes and ammo stores support in engine
unc0rr
parents:
diff changeset
    68
begin
929c44745fd9 Ammo schemes and ammo stores support in engine
unc0rr
parents:
diff changeset
    69
tteam:= TeamsList;
929c44745fd9 Ammo schemes and ammo stores support in engine
unc0rr
parents:
diff changeset
    70
while tteam <> nil do
929c44745fd9 Ammo schemes and ammo stores support in engine
unc0rr
parents:
diff changeset
    71
      begin
929c44745fd9 Ammo schemes and ammo stores support in engine
unc0rr
parents:
diff changeset
    72
      for i:= 0 to cMaxHHIndex do
929c44745fd9 Ammo schemes and ammo stores support in engine
unc0rr
parents:
diff changeset
    73
          if tteam.Hedgehogs[i].Gear <> nil then
929c44745fd9 Ammo schemes and ammo stores support in engine
unc0rr
parents:
diff changeset
    74
             tteam.Hedgehogs[i].Ammo:= GetAmmoByNum(tteam.Hedgehogs[i].AmmoStore);
929c44745fd9 Ammo schemes and ammo stores support in engine
unc0rr
parents:
diff changeset
    75
      tteam:= tteam.Next
929c44745fd9 Ammo schemes and ammo stores support in engine
unc0rr
parents:
diff changeset
    76
      end
929c44745fd9 Ammo schemes and ammo stores support in engine
unc0rr
parents:
diff changeset
    77
end;
929c44745fd9 Ammo schemes and ammo stores support in engine
unc0rr
parents:
diff changeset
    78
295
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
    79
procedure AddAmmo(Hedgehog: pointer; ammo: TAmmoType);
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
    80
var ammos: TAmmoCounts;
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
    81
    slot, ami: integer;
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
    82
    hhammo: PHHAmmo;
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
    83
begin
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
    84
FillChar(ammos, sizeof(ammos), 0);
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
    85
hhammo:= PHedgehog(Hedgehog).Ammo;
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
    86
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
    87
for slot:= 0 to cMaxSlotIndex do
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
    88
    for ami:= 0 to cMaxSlotAmmoIndex do
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
    89
        if hhammo[slot, ami].Count > 0 then
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
    90
           ammos[hhammo[slot, ami].AmmoType]:= hhammo[slot, ami].Count;
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
    91
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
    92
if ammos[ammo] <> AMMO_INFINITE then inc(ammos[ammo]);
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
    93
FillAmmoStore(hhammo, ammos)
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
    94
end;
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
    95
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
    96
procedure PackAmmo(Ammo: PHHAmmo; Slot: integer);
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
    97
var ami: integer;
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
    98
    b: boolean;
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
    99
begin
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
   100
    repeat
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
   101
      b:= false;
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
   102
      ami:= 0;
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
   103
      while (not b) and (ami < cMaxSlotAmmoIndex) do
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
   104
          if (Ammo[Slot, ami].Count = 0)
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
   105
             and (Ammo[Slot, ami + 1].Count > 0) then b:= true
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
   106
                                                 else inc(ami);
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
   107
      if b then // there's a free item in ammo stack
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
   108
         begin
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
   109
         Ammo[Slot, ami]:= Ammo[Slot, ami + 1];
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
   110
         Ammo[Slot, ami + 1].Count:= 0
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
   111
         end;
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
   112
    until not b;
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
   113
end;
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
   114
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
   115
procedure OnUsedAmmo(Ammo: PHHAmmo);
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
   116
var s, a: Longword;
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
   117
begin
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
   118
with CurrentTeam.Hedgehogs[CurrentTeam.CurrHedgehog] do
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
   119
     begin
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
   120
     if CurAmmoGear = nil then begin s:= CurSlot; a:= CurAmmo end
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
   121
                          else begin s:= AltSlot; a:= AltAmmo end;
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
   122
     with Ammo[s, a] do
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
   123
          if Count <> AMMO_INFINITE then
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
   124
             begin
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
   125
             dec(Count);
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
   126
             if Count = 0 then PackAmmo(Ammo, CurSlot)
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
   127
             end
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
   128
     end
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
   129
end;
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
   130
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
   131
function  HHHasAmmo(Hedgehog: pointer; Ammo: TAmmoType): boolean;
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
   132
var slot, ami: integer;
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
   133
begin
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
   134
Slot:= Ammoz[Ammo].Slot;
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
   135
ami:= 0;
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
   136
Result:= false;
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
   137
while (not Result) and (ami <= cMaxSlotAmmoIndex) do
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
   138
      begin
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
   139
      with PHedgehog(Hedgehog).Ammo[Slot, ami] do
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
   140
            if (AmmoType = Ammo) and (Count > 0) then Result:= true;
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
   141
      inc(ami)
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
   142
      end
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
   143
end;
8834f3cb620e - Implement ammo cases
unc0rr
parents: 288
diff changeset
   144
288
929c44745fd9 Ammo schemes and ammo stores support in engine
unc0rr
parents:
diff changeset
   145
end.