hedgewars/uAmmos.pas
changeset 288 929c44745fd9
child 295 8834f3cb620e
equal deleted inserted replaced
287:b0eef98928f8 288:929c44745fd9
       
     1 unit uAmmos;
       
     2 interface
       
     3 uses uConsts;
       
     4 {$INCLUDE options.inc}
       
     5 type PHHAmmo = ^THHAmmo;
       
     6      THHAmmo = array[0..cMaxSlotIndex, 0..cMaxSlotAmmoIndex] of TAmmo;
       
     7 
       
     8 procedure AddAmmoStore(s: shortstring);
       
     9 procedure AssignStores;
       
    10 
       
    11 implementation
       
    12 uses uMisc, uTeams;
       
    13 var StoresList: array[0..Pred(cMaxHHs)] of PHHAmmo;
       
    14     StoreCnt: Longword = 0;
       
    15 
       
    16 procedure AddAmmoStore(s: shortstring);
       
    17 var mi: array[0..cMaxSlotIndex] of byte;
       
    18     a: TAmmoType;
       
    19     cnt: Longword;
       
    20     tmp: PHHAmmo;
       
    21 begin
       
    22 TryDo(byte(s[0]) = byte(ord(High(TAmmoType)) + 1), 'Invalid ammo scheme (incompatible frontend)', true);
       
    23 
       
    24 inc(StoreCnt);
       
    25 TryDo(StoreCnt <= cMaxHHs, 'Ammo stores overflow', true);
       
    26 
       
    27 new(StoresList[Pred(StoreCnt)]);
       
    28 tmp:= StoresList[Pred(StoreCnt)];
       
    29 
       
    30 FillChar(mi, sizeof(mi), 0);
       
    31 for a:= Low(TAmmoType) to High(TAmmoType) do
       
    32     begin
       
    33     cnt:= byte(s[ord(a) + 1]) - byte('0');
       
    34     if cnt > 0 then
       
    35        begin
       
    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;
       
    43 end;
       
    44 
       
    45 function GetAmmoByNum(num: Longword): PHHAmmo;
       
    46 begin
       
    47 TryDo(num < StoreCnt, 'Invalid store number', true);
       
    48 Result:= StoresList[num]
       
    49 end;
       
    50 
       
    51 procedure AssignStores;
       
    52 var tteam: PTeam;
       
    53     i: Longword;
       
    54 begin
       
    55 tteam:= TeamsList;
       
    56 while tteam <> nil do
       
    57       begin
       
    58       for i:= 0 to cMaxHHIndex do
       
    59           if tteam.Hedgehogs[i].Gear <> nil then
       
    60              tteam.Hedgehogs[i].Ammo:= GetAmmoByNum(tteam.Hedgehogs[i].AmmoStore);
       
    61       tteam:= tteam.Next
       
    62       end
       
    63 end;
       
    64 
       
    65 end.