hedgewars/uAmmos.pas
branchwebgl
changeset 9521 8054d9d775fd
parent 9127 e350500c4edb
parent 9391 2af25494f3bb
child 9950 2759212a27de
equal deleted inserted replaced
9282:92af50454cf2 9521:8054d9d775fd
    51 
    51 
    52 implementation
    52 implementation
    53 uses uLocale, uVariables, uCommands, uUtils, uCaptions, uDebug;
    53 uses uLocale, uVariables, uCommands, uUtils, uCaptions, uDebug;
    54 
    54 
    55 type TAmmoCounts = array[TAmmoType] of Longword;
    55 type TAmmoCounts = array[TAmmoType] of Longword;
       
    56      TAmmoArray = array[TAmmoType] of TAmmo;
    56 var StoresList: array[0..Pred(cMaxHHs)] of PHHAmmo;
    57 var StoresList: array[0..Pred(cMaxHHs)] of PHHAmmo;
    57     ammoLoadout, ammoProbability, ammoDelay, ammoReinforcement: shortstring;
    58     ammoLoadout, ammoProbability, ammoDelay, ammoReinforcement: shortstring;
    58     InitialCounts: array[0..Pred(cMaxHHs)] of TAmmoCounts;
    59     InitialCounts: array[0..Pred(cMaxHHs)] of TAmmoCounts;
    59 
    60 
    60 procedure FillAmmoStore(Ammo: PHHAmmo; var cnts: TAmmoCounts);
    61 procedure FillAmmoStore(Ammo: PHHAmmo; var newAmmo: TAmmoArray);
    61 var mi: array[0..cMaxSlotIndex] of byte;
    62 var mi: array[0..cMaxSlotIndex] of byte;
    62     a: TAmmoType;
    63     a: TAmmoType;
    63 begin
    64 begin
    64 {$HINTS OFF}
    65 {$HINTS OFF}
    65 FillChar(mi, sizeof(mi), 0);
    66 FillChar(mi, sizeof(mi), 0);
    66 {$HINTS ON}
    67 {$HINTS ON}
    67 FillChar(Ammo^, sizeof(Ammo^), 0);
    68 FillChar(Ammo^, sizeof(Ammo^), 0);
    68 for a:= Low(TAmmoType) to High(TAmmoType) do
    69 for a:= Low(TAmmoType) to High(TAmmoType) do
    69     begin
    70     begin
    70     if cnts[a] > 0 then
    71     if newAmmo[a].Count > 0 then
    71         begin
    72         begin
    72         TryDo(mi[Ammoz[a].Slot] <= cMaxSlotAmmoIndex, 'Ammo slot overflow', true);
    73         TryDo(mi[Ammoz[a].Slot] <= cMaxSlotAmmoIndex, 'Ammo slot overflow', true);
    73         Ammo^[Ammoz[a].Slot, mi[Ammoz[a].Slot]]:= Ammoz[a].Ammo;
    74         Ammo^[Ammoz[a].Slot, mi[Ammoz[a].Slot]]:= newAmmo[a];
    74         with Ammo^[Ammoz[a].Slot, mi[Ammoz[a].Slot]] do
       
    75             Count:= cnts[a];
       
    76         inc(mi[Ammoz[a].Slot])
    75         inc(mi[Ammoz[a].Slot])
    77         end
    76         end
    78     end;
    77     end;
    79 AmmoMenuInvalidated:= true;
    78 AmmoMenuInvalidated:= true;
    80 end;
    79 end;
    82 procedure AddAmmoStore;
    81 procedure AddAmmoStore;
    83 const probability: array [0..8] of LongWord = (0,20,30,60,100,200,400,600,800);
    82 const probability: array [0..8] of LongWord = (0,20,30,60,100,200,400,600,800);
    84 var cnt: Longword;
    83 var cnt: Longword;
    85     a: TAmmoType;
    84     a: TAmmoType;
    86     ammos: TAmmoCounts;
    85     ammos: TAmmoCounts;
       
    86     newAmmos: TAmmoArray;
    87 begin
    87 begin
    88 TryDo((byte(ammoLoadout[0]) = byte(ord(High(TAmmoType)))) and (byte(ammoProbability[0]) = byte(ord(High(TAmmoType)))) and (byte(ammoDelay[0]) = byte(ord(High(TAmmoType)))) and (byte(ammoReinforcement[0]) = byte(ord(High(TAmmoType)))), 'Incomplete or missing ammo scheme set (incompatible frontend or demo/save?)', true);
    88 TryDo((byte(ammoLoadout[0]) = byte(ord(High(TAmmoType)))) and (byte(ammoProbability[0]) = byte(ord(High(TAmmoType)))) and (byte(ammoDelay[0]) = byte(ord(High(TAmmoType)))) and (byte(ammoReinforcement[0]) = byte(ord(High(TAmmoType)))), 'Incomplete or missing ammo scheme set (incompatible frontend or demo/save?)', true);
    89 
    89 
    90 inc(StoreCnt);
    90 inc(StoreCnt);
    91 TryDo(StoreCnt <= cMaxHHs, 'Ammo stores overflow', true);
    91 TryDo(StoreCnt <= cMaxHHs, 'Ammo stores overflow', true);
   139     if ((GameFlags and gfPlaceHog) <> 0) and (a = amTeleport) then 
   139     if ((GameFlags and gfPlaceHog) <> 0) and (a = amTeleport) then 
   140         InitialCounts[Pred(StoreCnt)][a]:= cnt
   140         InitialCounts[Pred(StoreCnt)][a]:= cnt
   141     else
   141     else
   142         InitialCounts[Pred(StoreCnt)][a]:= ammos[a];
   142         InitialCounts[Pred(StoreCnt)][a]:= ammos[a];
   143     end;
   143     end;
   144 FillAmmoStore(StoresList[Pred(StoreCnt)], ammos)
   144     
       
   145     for a:= Low(TAmmoType) to High(TAmmoType) do
       
   146         begin
       
   147         newAmmos[a]:= Ammoz[a].Ammo;
       
   148         newAmmos[a].Count:= ammos[a]
       
   149         end;
       
   150         
       
   151 FillAmmoStore(StoresList[Pred(StoreCnt)], newAmmos)
   145 end;
   152 end;
   146 
   153 
   147 function GetAmmoByNum(num: Longword): PHHAmmo;
   154 function GetAmmoByNum(num: Longword): PHHAmmo;
   148 begin
   155 begin
   149     TryDo(num < StoreCnt, 'Invalid store number', true);
   156     TryDo(num < StoreCnt, 'Invalid store number', true);
   209 begin
   216 begin
   210     AddAmmo(Hedgehog, ammo, Ammoz[ammo].NumberInCase);
   217     AddAmmo(Hedgehog, ammo, Ammoz[ammo].NumberInCase);
   211 end;
   218 end;
   212 
   219 
   213 procedure SetAmmo(var Hedgehog: THedgehog; ammo: TAmmoType; cnt: LongWord);
   220 procedure SetAmmo(var Hedgehog: THedgehog; ammo: TAmmoType; cnt: LongWord);
   214 var ammos: TAmmoCounts;
   221 var ammos: TAmmoArray;
   215     slot, ami: LongInt;
   222     slot, ami: LongInt;
   216     hhammo: PHHAmmo;
   223     hhammo: PHHAmmo;
   217     CurWeapon: PAmmo;
   224     CurWeapon: PAmmo;
       
   225     a: TAmmoType;
   218 begin
   226 begin
   219 {$HINTS OFF}
   227 {$HINTS OFF}
   220 FillChar(ammos, sizeof(ammos), 0);
   228 FillChar(ammos, sizeof(ammos), 0);
   221 {$HINTS ON}
   229 {$HINTS ON}
   222 hhammo:= Hedgehog.Ammo;
   230 hhammo:= Hedgehog.Ammo;
   223 
   231 
       
   232 for a:= Low(TAmmoType) to High(TAmmoType) do
       
   233     begin
       
   234     ammos[a]:= Ammoz[a].Ammo;
       
   235     ammos[a].Count:= 0
       
   236     end;
       
   237 
   224 for slot:= 0 to cMaxSlotIndex do
   238 for slot:= 0 to cMaxSlotIndex do
   225     for ami:= 0 to cMaxSlotAmmoIndex do
   239     for ami:= 0 to cMaxSlotAmmoIndex do
   226         if hhammo^[slot, ami].Count > 0 then
   240         if hhammo^[slot, ami].Count > 0 then
   227             ammos[hhammo^[slot, ami].AmmoType]:= hhammo^[slot, ami].Count;
   241             ammos[hhammo^[slot, ami].AmmoType]:= hhammo^[slot, ami];
   228 
   242 
   229 ammos[ammo]:= cnt;
   243 ammos[ammo].Count:= cnt;
   230 if ammos[ammo] > AMMO_INFINITE then ammos[ammo]:= AMMO_INFINITE;
   244 if ammos[ammo].Count > AMMO_INFINITE then ammos[ammo].Count:= AMMO_INFINITE;
   231 
   245 
   232 FillAmmoStore(hhammo, ammos);
   246 FillAmmoStore(hhammo, ammos);
   233 CurWeapon:= GetCurAmmoEntry(Hedgehog);
   247 CurWeapon:= GetCurAmmoEntry(Hedgehog);
   234 with Hedgehog, CurWeapon^ do
   248 with Hedgehog, CurWeapon^ do
   235     if (Count = 0) or (AmmoType = amNothing) then
   249     if (Count = 0) or (AmmoType = amNothing) then
   453 
   467 
   454 // Restore indefinitely disabled weapons and initial weapon counts.  Only used for hog placement right now
   468 // Restore indefinitely disabled weapons and initial weapon counts.  Only used for hog placement right now
   455 procedure ResetWeapons;
   469 procedure ResetWeapons;
   456 var i, t: Longword;
   470 var i, t: Longword;
   457     a: TAmmoType;
   471     a: TAmmoType;
       
   472     newAmmos: TAmmoArray;
   458 begin
   473 begin
   459 for t:= 0 to Pred(TeamsCount) do
   474 for t:= 0 to Pred(TeamsCount) do
   460     with TeamsArray[t]^ do
   475     with TeamsArray[t]^ do
   461         for i:= 0 to cMaxHHIndex do
   476         for i:= 0 to cMaxHHIndex do
   462             Hedgehogs[i].CurAmmoType:= amNothing;
   477             Hedgehogs[i].CurAmmoType:= amNothing;
   463 
   478 
       
   479 for a:= Low(TAmmoType) to High(TAmmoType) do
       
   480     newAmmos[a]:= Ammoz[a].Ammo;
       
   481     
   464 for i:= 0 to Pred(StoreCnt) do
   482 for i:= 0 to Pred(StoreCnt) do
   465     FillAmmoStore(StoresList[i], InitialCounts[i]);
   483     begin
       
   484     for a:= Low(TAmmoType) to High(TAmmoType) do
       
   485         newAmmos[a].Count:= InitialCounts[i][a];
       
   486     FillAmmoStore(StoresList[i], newAmmos);
       
   487     end;
   466 
   488 
   467 for a:= Low(TAmmoType) to High(TAmmoType) do
   489 for a:= Low(TAmmoType) to High(TAmmoType) do
   468     if Ammoz[a].SkipTurns >= 10000 then
   490     if Ammoz[a].SkipTurns >= 10000 then
   469         dec(Ammoz[a].SkipTurns,10000)
   491         dec(Ammoz[a].SkipTurns,10000)
   470 end;
   492 end;