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