hedgewars/uAmmos.pas
branchhedgeroid
changeset 7855 ddcdedd3330b
parent 7754 e81dc9bef8b8
child 8026 4a4f21070479
child 8992 5b0be812dcdb
equal deleted inserted replaced
6350:41b0a9955c47 7855:ddcdedd3330b
     1 (*
     1 (*
     2  * Hedgewars, a free turn based strategy game
     2  * Hedgewars, a free turn based strategy game
     3  * Copyright (c) 2004-2011 Andrey Korotaev <unC0Rr@gmail.com>
     3  * Copyright (c) 2004-2012 Andrey Korotaev <unC0Rr@gmail.com>
     4  *
     4  *
     5  * This program is free software; you can redistribute it and/or modify
     5  * This program is free software; you can redistribute it and/or modify
     6  * it under the terms of the GNU General Public License as published by
     6  * it under the terms of the GNU General Public License as published by
     7  * the Free Software Foundation; version 2 of the License
     7  * the Free Software Foundation; version 2 of the License
     8  *
     8  *
    18 
    18 
    19 {$INCLUDE "options.inc"}
    19 {$INCLUDE "options.inc"}
    20 
    20 
    21 unit uAmmos;
    21 unit uAmmos;
    22 interface
    22 interface
    23 uses uConsts, uTypes;
    23 uses uConsts, uTypes, uStore;
    24 
    24 
    25 procedure initModule;
    25 procedure initModule;
    26 procedure freeModule;
    26 procedure freeModule;
    27 
    27 
    28 procedure AddAmmoStore;
    28 procedure AddAmmoStore;
    29 procedure SetAmmoLoadout(s: shortstring);
    29 procedure SetAmmoLoadout(var s: shortstring);
    30 procedure SetAmmoProbability(s: shortstring);
    30 procedure SetAmmoProbability(var s: shortstring);
    31 procedure SetAmmoDelay(s: shortstring);
    31 procedure SetAmmoDelay(var s: shortstring);
    32 procedure SetAmmoReinforcement(s: shortstring);
    32 procedure SetAmmoReinforcement(var s: shortstring);
    33 procedure AssignStores;
    33 procedure AssignStores;
    34 procedure AddAmmo(var Hedgehog: THedgehog; ammo: TAmmoType);
    34 procedure AddAmmo(var Hedgehog: THedgehog; ammo: TAmmoType);
    35 procedure AddAmmo(var Hedgehog: THedgehog; ammo: TAmmoType; cnt: LongWord);
    35 procedure AddAmmo(var Hedgehog: THedgehog; ammo: TAmmoType; amt: LongWord);
    36 function  HHHasAmmo(var Hedgehog: THedgehog; Ammo: TAmmoType): boolean;
    36 procedure SetAmmo(var Hedgehog: THedgehog; ammo: TAmmoType; cnt: LongWord);
       
    37 function  HHHasAmmo(var Hedgehog: THedgehog; Ammo: TAmmoType): LongWord;
    37 procedure PackAmmo(Ammo: PHHAmmo; Slot: LongInt);
    38 procedure PackAmmo(Ammo: PHHAmmo; Slot: LongInt);
    38 procedure OnUsedAmmo(var Hedgehog: THedgehog);
    39 procedure OnUsedAmmo(var Hedgehog: THedgehog);
    39 procedure ApplyAngleBounds(var Hedgehog: THedgehog; AmmoType: TAmmoType);
    40 procedure ApplyAngleBounds(var Hedgehog: THedgehog; AmmoType: TAmmoType);
    40 procedure ApplyAmmoChanges(var Hedgehog: THedgehog);
    41 procedure ApplyAmmoChanges(var Hedgehog: THedgehog);
    41 procedure SwitchNotHeldAmmo(var Hedgehog: THedgehog);
    42 procedure SwitchNotHeldAmmo(var Hedgehog: THedgehog);
    42 procedure SetWeapon(weap: TAmmoType);
    43 procedure SetWeapon(weap: TAmmoType);
    43 procedure DisableSomeWeapons;
    44 procedure DisableSomeWeapons;
    44 procedure ResetWeapons;
    45 procedure ResetWeapons;
    45 function  GetAmmoByNum(num: Longword): PHHAmmo;
    46 function  GetAmmoByNum(num: Longword): PHHAmmo;
    46 function  GetAmmoEntry(var Hedgehog: THedgehog): PAmmo;
    47 function  GetCurAmmoEntry(var Hedgehog: THedgehog): PAmmo;
    47 function  GetAmmoEntry(var Hedgehog: THedgehog; am: TAmmoType): PAmmo;
    48 function  GetAmmoEntry(var Hedgehog: THedgehog; am: TAmmoType): PAmmo;
    48 
    49 
    49 var StoreCnt: Longword;
    50 var StoreCnt: Longword;
    50 
    51 
    51 implementation
    52 implementation
    52 uses uLocale, uMobile, uVariables, uCommands, uUtils, uCaptions, uDebug;
    53 uses uLocale, uVariables, uCommands, uUtils, uCaptions, uDebug;
    53 
    54 
    54 type TAmmoCounts = array[TAmmoType] of Longword;
    55 type TAmmoCounts = array[TAmmoType] of Longword;
    55 var StoresList: array[0..Pred(cMaxHHs)] of PHHAmmo;
    56 var StoresList: array[0..Pred(cMaxHHs)] of PHHAmmo;
    56     ammoLoadout, ammoProbability, ammoDelay, ammoReinforcement: shortstring;
    57     ammoLoadout, ammoProbability, ammoDelay, ammoReinforcement: shortstring;
    57     InitialCounts: array[0..Pred(cMaxHHs)] of TAmmoCounts;
    58     InitialCounts: array[0..Pred(cMaxHHs)] of TAmmoCounts;
    65 {$HINTS ON}
    66 {$HINTS ON}
    66 FillChar(Ammo^, sizeof(Ammo^), 0);
    67 FillChar(Ammo^, sizeof(Ammo^), 0);
    67 for a:= Low(TAmmoType) to High(TAmmoType) do
    68 for a:= Low(TAmmoType) to High(TAmmoType) do
    68     begin
    69     begin
    69     if cnts[a] > 0 then
    70     if cnts[a] > 0 then
    70        begin
    71         begin
    71        TryDo(mi[Ammoz[a].Slot] <= cMaxSlotAmmoIndex, 'Ammo slot overflow', true);
    72         TryDo(mi[Ammoz[a].Slot] <= cMaxSlotAmmoIndex, 'Ammo slot overflow', true);
    72        Ammo^[Ammoz[a].Slot, mi[Ammoz[a].Slot]]:= Ammoz[a].Ammo;
    73         Ammo^[Ammoz[a].Slot, mi[Ammoz[a].Slot]]:= Ammoz[a].Ammo;
    73        with Ammo^[Ammoz[a].Slot, mi[Ammoz[a].Slot]] do Count:= cnts[a];
    74         with Ammo^[Ammoz[a].Slot, mi[Ammoz[a].Slot]] do
    74        inc(mi[Ammoz[a].Slot])
    75             Count:= cnts[a];
    75        end
    76         inc(mi[Ammoz[a].Slot])
    76     end
    77         end
       
    78     end;
       
    79 AmmoMenuInvalidated:= true;
    77 end;
    80 end;
    78 
    81 
    79 procedure AddAmmoStore;
    82 procedure AddAmmoStore;
    80 const probability: array [0..8] of LongWord = (0,20,30,60,100,200,400,600,800);
    83 const probability: array [0..8] of LongWord = (0,20,30,60,100,200,400,600,800);
    81 var cnt: Longword;
    84 var cnt: Longword;
   101         if cnt = 9 then
   104         if cnt = 9 then
   102             begin
   105             begin
   103             cnt:= AMMO_INFINITE;
   106             cnt:= AMMO_INFINITE;
   104             Ammoz[a].Probability:= 0
   107             Ammoz[a].Probability:= 0
   105             end;
   108             end;
   106         if Ammoz[a].NumberInCase = 0 then Ammoz[a].Probability:= 0;
   109         if Ammoz[a].NumberInCase = 0 then
       
   110             Ammoz[a].Probability:= 0;
   107 
   111 
   108         // avoid things we already have by scheme
   112         // avoid things we already have by scheme
   109         // merge this into DisableSomeWeapons ?
   113         // merge this into DisableSomeWeapons ?
   110         if ((a = amLowGravity) and ((GameFlags and gfLowGravity) <> 0)) or
   114         if ((a = amLowGravity) and ((GameFlags and gfLowGravity) <> 0))
   111            ((a = amInvulnerable) and ((GameFlags and gfInvulnerable) <> 0)) or
   115         or ((a = amInvulnerable) and ((GameFlags and gfInvulnerable) <> 0))
   112            ((a = amLaserSight) and ((GameFlags and gfLaserSight) <> 0)) or
   116         or ((a = amLaserSight) and ((GameFlags and gfLaserSight) <> 0))
   113            ((a = amVampiric) and ((GameFlags and gfVampiric) <> 0)) or
   117         or ((a = amVampiric) and ((GameFlags and gfVampiric) <> 0))
   114            ((a = amExtraTime) and (cHedgehogTurnTime >= 1000000)) or
   118         or ((a = amExtraTime) and (cHedgehogTurnTime >= 1000000)) then
   115             (a = amStructure) then
       
   116             begin
   119             begin
   117             cnt:= 0;
   120             cnt:= 0;
   118             Ammoz[a].Probability:= 0
   121             Ammoz[a].Probability:= 0
   119             end;
   122             end;
   120         ammos[a]:= cnt;
   123         ammos[a]:= cnt;
   121 
   124 
   122         if ((GameFlags and gfKing) <> 0) and ((GameFlags and gfPlaceHog) = 0) and (Ammoz[a].SkipTurns = 0) and (a <> amTeleport) and (a <> amSkip) then
   125         if ((GameFlags and gfKing) <> 0) and ((GameFlags and gfPlaceHog) = 0)
       
   126         and (Ammoz[a].SkipTurns = 0) and (a <> amTeleport) and (a <> amSkip) then
   123             Ammoz[a].SkipTurns:= 1;
   127             Ammoz[a].SkipTurns:= 1;
   124 
   128 
   125         if ((GameFlags and gfPlaceHog) <> 0) and
   129         if ((GameFlags and gfPlaceHog) <> 0)
   126             (a <> amTeleport) and (a <> amSkip) and
   130         and (a <> amTeleport) and (a <> amSkip)
   127             (Ammoz[a].SkipTurns < 10000) then inc(Ammoz[a].SkipTurns,10000);
   131         and (Ammoz[a].SkipTurns < 10000) then
   128     if ((GameFlags and gfPlaceHog) <> 0) and (a = amTeleport) then ammos[a]:= AMMO_INFINITE
   132             inc(Ammoz[a].SkipTurns,10000);
       
   133     if ((GameFlags and gfPlaceHog) <> 0) and (a = amTeleport) then
       
   134         ammos[a]:= AMMO_INFINITE
   129         end 
   135         end 
   130     else ammos[a]:= AMMO_INFINITE;
   136         
       
   137     else
       
   138         ammos[a]:= AMMO_INFINITE;
   131     if ((GameFlags and gfPlaceHog) <> 0) and (a = amTeleport) then 
   139     if ((GameFlags and gfPlaceHog) <> 0) and (a = amTeleport) then 
   132         InitialCounts[Pred(StoreCnt)][a]:= cnt
   140         InitialCounts[Pred(StoreCnt)][a]:= cnt
   133     else
   141     else
   134         InitialCounts[Pred(StoreCnt)][a]:= ammos[a];
   142         InitialCounts[Pred(StoreCnt)][a]:= ammos[a];
   135     end;
   143     end;
   136 FillAmmoStore(StoresList[Pred(StoreCnt)], ammos)
   144 FillAmmoStore(StoresList[Pred(StoreCnt)], ammos)
   137 end;
   145 end;
   138 
   146 
   139 function GetAmmoByNum(num: Longword): PHHAmmo;
   147 function GetAmmoByNum(num: Longword): PHHAmmo;
   140 begin
   148 begin
   141 TryDo(num < StoreCnt, 'Invalid store number', true);
   149     TryDo(num < StoreCnt, 'Invalid store number', true);
   142 exit(StoresList[num])
   150     GetAmmoByNum:= StoresList[num]
   143 end;
   151 end;
   144 
   152 
   145 function GetAmmoEntry(var Hedgehog: THedgehog): PAmmo;
   153 function GetCurAmmoEntry(var Hedgehog: THedgehog): PAmmo;
   146 begin
   154 begin
   147 GetAmmoEntry:= GetAmmoEntry(Hedgehog, Hedgehog.CurAmmoType)
   155     GetCurAmmoEntry:= GetAmmoEntry(Hedgehog, Hedgehog.CurAmmoType)
   148 end;
   156 end;
   149 
   157 
   150 function GetAmmoEntry(var Hedgehog: THedgehog; am: TAmmoType): PAmmo;
   158 function GetAmmoEntry(var Hedgehog: THedgehog; am: TAmmoType): PAmmo;
   151 var ammoidx, slot: LongWord;
   159 var ammoidx, slot: LongWord;
   152 begin
   160 begin
   153 with Hedgehog do
   161 with Hedgehog do
   154     begin
   162     begin
   155     slot:= Ammoz[am].Slot;
   163     slot:= Ammoz[am].Slot;
   156     ammoidx:= 0;
   164     ammoidx:= 0;
   157     while (ammoidx < cMaxSlotAmmoIndex) and (Ammo^[slot, ammoidx].AmmoType <> am) do inc(ammoidx);
   165     while (ammoidx < cMaxSlotAmmoIndex) and (Ammo^[slot, ammoidx].AmmoType <> am) do
       
   166         inc(ammoidx);
   158     GetAmmoEntry:= @Ammo^[slot, ammoidx];
   167     GetAmmoEntry:= @Ammo^[slot, ammoidx];
   159     if (Ammo^[slot, ammoidx].AmmoType <> am) then GetAmmoEntry:= GetAmmoEntry(Hedgehog, amNothing)
   168     if (Ammo^[slot, ammoidx].AmmoType <> am) then
       
   169         GetAmmoEntry:= GetAmmoEntry(Hedgehog, amNothing)
   160     end;
   170     end;
   161 end;
   171 end;
   162 
   172 
   163 procedure AssignStores;
   173 procedure AssignStores;
   164 var t: LongInt;
   174 var t: LongInt;
   165     i: Longword;
   175     i: Longword;
   166 begin
   176 begin
   167 for t:= 0 to Pred(TeamsCount) do
   177 for t:= 0 to Pred(TeamsCount) do
   168    with TeamsArray[t]^ do
   178     with TeamsArray[t]^ do
   169       begin
   179         begin
   170       for i:= 0 to cMaxHHIndex do
   180         for i:= 0 to cMaxHHIndex do
   171           if Hedgehogs[i].Gear <> nil then
   181             if Hedgehogs[i].Gear <> nil then
   172              begin
   182                 begin
   173              Hedgehogs[i].Ammo:= GetAmmoByNum(Hedgehogs[i].AmmoStore);
   183                 Hedgehogs[i].Ammo:= GetAmmoByNum(Hedgehogs[i].AmmoStore);
   174              if (GameFlags and gfPlaceHog) <> 0 then
   184                 if (GameFlags and gfPlaceHog) <> 0 then
   175                 Hedgehogs[i].CurAmmoType:= amTeleport
   185                     Hedgehogs[i].CurAmmoType:= amTeleport
   176              else
   186                 else
   177                 Hedgehogs[i].CurAmmoType:= amNothing
   187                     Hedgehogs[i].CurAmmoType:= amNothing
   178              end
   188                 end
   179       end
   189         end
   180 end;
   190 end;
   181 
   191 
   182 procedure AddAmmo(var Hedgehog: THedgehog; ammo: TAmmoType);
   192 procedure AddAmmo(var Hedgehog: THedgehog; ammo: TAmmoType; amt: LongWord);
   183 var cnt: LongWord;
   193 var cnt: LongWord;
   184     a: PAmmo;
   194     a: PAmmo;
   185 begin
   195 begin
   186 a:= GetAmmoEntry(Hedgehog, ammo);
   196 a:= GetAmmoEntry(Hedgehog, ammo);
   187 if (a^.AmmoType <> amNothing) then cnt:= a^.Count
   197 if (a^.AmmoType <> amNothing) then
   188 else cnt:= 0;
   198     cnt:= a^.Count
       
   199 else
       
   200     cnt:= 0;
   189 if (cnt <> AMMO_INFINITE) then
   201 if (cnt <> AMMO_INFINITE) then
   190     begin
   202     begin
   191     inc(cnt, Ammoz[ammo].NumberInCase);
   203     inc(cnt, amt);
   192     AddAmmo(Hedgehog, ammo, cnt)
   204     SetAmmo(Hedgehog, ammo, cnt)
   193     end
   205     end
   194 end;
   206 end;
   195 
   207 
   196 procedure AddAmmo(var Hedgehog: THedgehog; ammo: TAmmoType; cnt: LongWord);
   208 procedure AddAmmo(var Hedgehog: THedgehog; ammo: TAmmoType);
       
   209 begin
       
   210     AddAmmo(Hedgehog, ammo, Ammoz[ammo].NumberInCase);
       
   211 end;
       
   212 
       
   213 procedure SetAmmo(var Hedgehog: THedgehog; ammo: TAmmoType; cnt: LongWord);
   197 var ammos: TAmmoCounts;
   214 var ammos: TAmmoCounts;
   198     slot, ami: LongInt;
   215     slot, ami: LongInt;
   199     hhammo: PHHAmmo;
   216     hhammo: PHHAmmo;
   200     CurWeapon: PAmmo;
   217     CurWeapon: PAmmo;
   201 begin
   218 begin
   205 hhammo:= Hedgehog.Ammo;
   222 hhammo:= Hedgehog.Ammo;
   206 
   223 
   207 for slot:= 0 to cMaxSlotIndex do
   224 for slot:= 0 to cMaxSlotIndex do
   208     for ami:= 0 to cMaxSlotAmmoIndex do
   225     for ami:= 0 to cMaxSlotAmmoIndex do
   209         if hhammo^[slot, ami].Count > 0 then
   226         if hhammo^[slot, ami].Count > 0 then
   210            ammos[hhammo^[slot, ami].AmmoType]:= hhammo^[slot, ami].Count;
   227             ammos[hhammo^[slot, ami].AmmoType]:= hhammo^[slot, ami].Count;
   211 
   228 
   212 ammos[ammo]:= cnt;
   229 ammos[ammo]:= cnt;
   213 if ammos[ammo] > AMMO_INFINITE then ammos[ammo]:= AMMO_INFINITE;
   230 if ammos[ammo] > AMMO_INFINITE then ammos[ammo]:= AMMO_INFINITE;
   214 
   231 
   215 FillAmmoStore(hhammo, ammos);
   232 FillAmmoStore(hhammo, ammos);
   216 CurWeapon:= GetAmmoEntry(Hedgehog);
   233 CurWeapon:= GetCurAmmoEntry(Hedgehog);
   217 with Hedgehog, CurWeapon^ do
   234 with Hedgehog, CurWeapon^ do
   218         if (Count = 0) or (AmmoType = amNothing) then
   235     if (Count = 0) or (AmmoType = amNothing) then
   219             begin
   236         begin
   220             PackAmmo(Ammo, Ammoz[AmmoType].Slot);
   237         PackAmmo(Ammo, Ammoz[AmmoType].Slot);
   221             CurAmmoType:= amNothing
   238         CurAmmoType:= amNothing
   222             end
   239         end
   223 end;
   240 end;
   224 
   241 
   225 procedure PackAmmo(Ammo: PHHAmmo; Slot: LongInt);
   242 procedure PackAmmo(Ammo: PHHAmmo; Slot: LongInt);
   226 var ami: LongInt;
   243 var ami: LongInt;
   227     b: boolean;
   244     b: boolean;
   228 begin
   245 begin
   229     repeat
   246     repeat
   230       b:= false;
   247         b:= false;
   231       ami:= 0;
   248         ami:= 0;
   232       while (not b) and (ami < cMaxSlotAmmoIndex) do
   249         while (not b) and (ami < cMaxSlotAmmoIndex) do
   233           if (Ammo^[Slot, ami].Count = 0)
   250             if (Ammo^[Slot, ami].Count = 0)
   234              and (Ammo^[Slot, ami + 1].Count > 0) then b:= true
   251             and (Ammo^[Slot, ami + 1].Count > 0) then
   235                                                   else inc(ami);
   252                 b:= true
   236       if b then // there is a free item in ammo stack
   253             else
   237          begin
   254                 inc(ami);
   238          Ammo^[Slot, ami]:= Ammo^[Slot, ami + 1];
   255         if b then // there is a free item in ammo stack
   239          Ammo^[Slot, ami + 1].Count:= 0
   256             begin
   240          end;
   257             Ammo^[Slot, ami]:= Ammo^[Slot, ami + 1];
       
   258             Ammo^[Slot, ami + 1].Count:= 0
       
   259             end;
   241     until not b;
   260     until not b;
       
   261 AmmoMenuInvalidated:= true;
   242 end;
   262 end;
   243 
   263 
   244 procedure OnUsedAmmo(var Hedgehog: THedgehog);
   264 procedure OnUsedAmmo(var Hedgehog: THedgehog);
   245 var CurWeapon: PAmmo;
   265 var CurWeapon: PAmmo;
   246 begin
   266 begin
   247 CurWeapon:= GetAmmoEntry(Hedgehog);
   267 CurWeapon:= GetCurAmmoEntry(Hedgehog);
   248 with Hedgehog do
   268 with Hedgehog do
   249     begin
   269     begin
   250 
   270 
   251     MultiShootAttacks:= 0;
   271     MultiShootAttacks:= 0;
   252     with CurWeapon^ do
   272     with CurWeapon^ do
   255             dec(Count);
   275             dec(Count);
   256             if Count = 0 then
   276             if Count = 0 then
   257                 begin
   277                 begin
   258                 PackAmmo(Ammo, Ammoz[AmmoType].Slot);
   278                 PackAmmo(Ammo, Ammoz[AmmoType].Slot);
   259                 //SwitchNotHeldAmmo(Hedgehog);
   279                 //SwitchNotHeldAmmo(Hedgehog);
       
   280                 if CurAmmoType = amKnife then LoadHedgehogHat(Hedgehog, Hedgehog.Hat);
   260                 CurAmmoType:= amNothing
   281                 CurAmmoType:= amNothing
   261                 end
   282                 end
   262             end
   283             end
   263     end;
   284     end;
   264 uMobile.AmmoUpdate;
   285 end;
   265 end;
   286 
   266 
   287 function  HHHasAmmo(var Hedgehog: THedgehog; Ammo: TAmmoType): LongWord;
   267 function  HHHasAmmo(var Hedgehog: THedgehog; Ammo: TAmmoType): boolean;
       
   268 var slot, ami: LongInt;
   288 var slot, ami: LongInt;
   269 begin
   289 begin
   270 Slot:= Ammoz[Ammo].Slot;
   290     HHHasAmmo:= 0;
   271 ami:= 0;
   291     Slot:= Ammoz[Ammo].Slot;
   272 while (ami <= cMaxSlotAmmoIndex) do
   292     ami:= 0;
   273       begin
   293     while (ami <= cMaxSlotAmmoIndex) do
   274       with Hedgehog.Ammo^[Slot, ami] do
   294     begin
       
   295         with Hedgehog.Ammo^[Slot, ami] do
   275             if (AmmoType = Ammo) then
   296             if (AmmoType = Ammo) then
   276                exit((Count > 0) and (Hedgehog.Team^.Clan^.TurnNumber > Ammoz[AmmoType].SkipTurns));
   297                 if Hedgehog.Team^.Clan^.TurnNumber > Ammoz[AmmoType].SkipTurns then
   277       inc(ami)
   298                     exit(Count)
   278       end;
   299                 else 
   279 HHHasAmmo:= false
   300                     exit(0);
       
   301         inc(ami)
       
   302     end;
   280 end;
   303 end;
   281 
   304 
   282 procedure ApplyAngleBounds(var Hedgehog: THedgehog; AmmoType: TAmmoType);
   305 procedure ApplyAngleBounds(var Hedgehog: THedgehog; AmmoType: TAmmoType);
   283 begin
   306 begin
   284 with Hedgehog do
   307 if Hedgehog.Gear <> nil then
   285     begin
   308     with Hedgehog do
   286     CurMinAngle:= Ammoz[AmmoType].minAngle;
   309         begin
   287     if Ammoz[AmmoType].maxAngle <> 0 then
   310         CurMinAngle:= Ammoz[AmmoType].minAngle;
   288         CurMaxAngle:= Ammoz[AmmoType].maxAngle
   311         if Ammoz[AmmoType].maxAngle <> 0 then
   289     else
   312             CurMaxAngle:= Ammoz[AmmoType].maxAngle
   290         CurMaxAngle:= cMaxAngle;
   313         else
   291 
   314             CurMaxAngle:= cMaxAngle;
   292     with Hedgehog.Gear^ do
   315 
   293         begin
   316         with Hedgehog.Gear^ do
   294         if Angle < CurMinAngle then Angle:= CurMinAngle;
   317             begin
   295         if Angle > CurMaxAngle then Angle:= CurMaxAngle;
   318             if Angle < CurMinAngle then
       
   319                 Angle:= CurMinAngle;
       
   320             if Angle > CurMaxAngle then
       
   321                 Angle:= CurMaxAngle;
       
   322             end
   296         end
   323         end
   297     end
       
   298 end;
   324 end;
   299 
   325 
   300 procedure SwitchToFirstLegalAmmo(var Hedgehog: THedgehog);
   326 procedure SwitchToFirstLegalAmmo(var Hedgehog: THedgehog);
   301 var slot, ammoidx: LongWord;
   327 var slot, ammoidx: LongWord;
   302 begin
   328 begin
   307     ammoidx:= 0;
   333     ammoidx:= 0;
   308     while (slot <= cMaxSlotIndex) and
   334     while (slot <= cMaxSlotIndex) and
   309         ((Ammo^[slot, ammoidx].Count = 0) or
   335         ((Ammo^[slot, ammoidx].Count = 0) or
   310         (Ammoz[Ammo^[slot, ammoidx].AmmoType].SkipTurns - CurrentTeam^.Clan^.TurnNumber >= 0))
   336         (Ammoz[Ammo^[slot, ammoidx].AmmoType].SkipTurns - CurrentTeam^.Clan^.TurnNumber >= 0))
   311         do
   337         do
   312         begin
   338             begin
   313         while (ammoidx <= cMaxSlotAmmoIndex) and
   339             while (ammoidx <= cMaxSlotAmmoIndex)
   314             ((Ammo^[slot, ammoidx].Count = 0) or
   340             and ((Ammo^[slot, ammoidx].Count = 0) or (Ammoz[Ammo^[slot, ammoidx].AmmoType].SkipTurns - CurrentTeam^.Clan^.TurnNumber >= 0))
   315             (Ammoz[Ammo^[slot, ammoidx].AmmoType].SkipTurns - CurrentTeam^.Clan^.TurnNumber >= 0))
   341                 do inc(ammoidx);
   316             do inc(ammoidx);
       
   317 
   342 
   318         if (ammoidx > cMaxSlotAmmoIndex) then
   343         if (ammoidx > cMaxSlotAmmoIndex) then
   319             begin
   344             begin
   320             ammoidx:= 0;
   345             ammoidx:= 0;
   321             inc(slot)
   346             inc(slot)
   322             end
   347             end
   323         end;
   348         end;
   324     TryDo(slot <= cMaxSlotIndex, 'Ammo slot index overflow', true);
   349     TryDo(slot <= cMaxSlotIndex, 'Ammo slot index overflow', true);
   325     CurAmmoType:= Ammo^[slot, ammoidx].AmmoType;
   350     CurAmmoType:= Ammo^[slot, ammoidx].AmmoType;
       
   351     if CurAmmoType = amKnife then LoadHedgehogHat(Hedgehog, 'Reserved/chef')
   326     end
   352     end
   327 end;
   353 end;
   328 
   354 
   329 procedure ApplyAmmoChanges(var Hedgehog: THedgehog);
   355 procedure ApplyAmmoChanges(var Hedgehog: THedgehog);
   330 var s: shortstring;
   356 var s: shortstring;
   334 
   360 
   335 with Hedgehog do
   361 with Hedgehog do
   336     begin
   362     begin
   337     Timer:= 10;
   363     Timer:= 10;
   338 
   364 
   339     CurWeapon:= GetAmmoEntry(Hedgehog);
   365     CurWeapon:= GetCurAmmoEntry(Hedgehog);
   340 
   366 
   341     if (CurWeapon^.Count = 0) then
   367     if (CurWeapon^.Count = 0) then
   342         SwitchToFirstLegalAmmo(Hedgehog)
   368         SwitchToFirstLegalAmmo(Hedgehog)
   343     else if CurWeapon^.AmmoType = amNothing then Hedgehog.CurAmmoType:= amNothing;
   369     else if CurWeapon^.AmmoType = amNothing then
   344 
   370         Hedgehog.CurAmmoType:= amNothing;
   345     CurWeapon:= GetAmmoEntry(Hedgehog);
   371 
       
   372     CurWeapon:= GetCurAmmoEntry(Hedgehog);
   346 
   373 
   347     ApplyAngleBounds(Hedgehog, CurWeapon^.AmmoType);
   374     ApplyAngleBounds(Hedgehog, CurWeapon^.AmmoType);
   348 
   375 
   349     with CurWeapon^ do
   376     with CurWeapon^ do
   350         begin
   377         begin
   351         s:= trammo[Ammoz[AmmoType].NameId];
   378         s:= trammo[Ammoz[AmmoType].NameId];
   352         if (Count <> AMMO_INFINITE) and not (Hedgehog.Team^.ExtDriven or (Hedgehog.BotLevel > 0)) then
   379         if (Count <> AMMO_INFINITE) and (not (Hedgehog.Team^.ExtDriven or (Hedgehog.BotLevel > 0))) then
   353             s:= s + ' (' + IntToStr(Count) + ')';
   380             s:= s + ' (' + IntToStr(Count) + ')';
   354         if (Propz and ammoprop_Timerable) <> 0 then
   381         if (Propz and ammoprop_Timerable) <> 0 then
   355             s:= s + ', ' + IntToStr(Timer div 1000) + ' ' + trammo[sidSeconds];
   382             s:= s + ', ' + IntToStr(Timer div 1000) + ' ' + trammo[sidSeconds];
   356         AddCaption(s, Team^.Clan^.Color, capgrpAmmoinfo);
   383         AddCaption(s, Team^.Clan^.Color, capgrpAmmoinfo);
   357         if (Propz and ammoprop_NeedTarget) <> 0
   384         if (Propz and ammoprop_NeedTarget) <> 0 then
   358             then begin
   385             begin
   359             Gear^.State:= Gear^.State or      gstHHChooseTarget;
   386             if Gear <> nil then Gear^.State:= Gear^.State or      gstHHChooseTarget;
   360             isCursorVisible:= true
   387             isCursorVisible:= true
   361             end else begin
   388             end
   362             Gear^.State:= Gear^.State and not gstHHChooseTarget;
   389         else
       
   390             begin
       
   391             if Gear <> nil then Gear^.State:= Gear^.State and (not gstHHChooseTarget);
   363             isCursorVisible:= false
   392             isCursorVisible:= false
   364             end;
   393             end;
   365         end
   394         end
   366     end;
   395     end;
   367 end;
   396 end;
   368 
   397 
   369 procedure SwitchNotHeldAmmo(var Hedgehog: THedgehog);
   398 procedure SwitchNotHeldAmmo(var Hedgehog: THedgehog);
   370 begin
   399 begin
   371 with Hedgehog do
   400 with Hedgehog do
   372     if ((Ammoz[CurAmmoType].Ammo.Propz and ammoprop_DontHold) <> 0) or
   401     if ((Ammoz[CurAmmoType].Ammo.Propz and ammoprop_DontHold) <> 0)
   373         (Ammoz[CurAmmoType].SkipTurns - CurrentTeam^.Clan^.TurnNumber >= 0) then
   402     or (Ammoz[CurAmmoType].SkipTurns - CurrentTeam^.Clan^.TurnNumber >= 0) then
   374         SwitchToFirstLegalAmmo(Hedgehog);
   403         SwitchToFirstLegalAmmo(Hedgehog);
   375 end;
   404 end;
   376 
   405 
   377 procedure SetWeapon(weap: TAmmoType);
   406 procedure SetWeapon(weap: TAmmoType);
   378 begin
   407 begin
   396 
   425 
   397         PackAmmo(StoresList[i], slot)
   426         PackAmmo(StoresList[i], slot)
   398         end;
   427         end;
   399 
   428 
   400 for t:= Low(TAmmoType) to High(TAmmoType) do
   429 for t:= Low(TAmmoType) to High(TAmmoType) do
   401     if (Ammoz[t].Ammo.Propz and ammoprop_NotBorder) <> 0 then Ammoz[t].Probability:= 0
   430     if (Ammoz[t].Ammo.Propz and ammoprop_NotBorder) <> 0 then
   402 end;
   431         Ammoz[t].Probability:= 0
   403 
   432 end;
   404 procedure SetAmmoLoadout(s: shortstring);
   433 
       
   434 procedure SetAmmoLoadout(var s: shortstring);
   405 begin
   435 begin
   406     ammoLoadout:= s;
   436     ammoLoadout:= s;
   407 end;
   437 end;
   408 
   438 
   409 procedure SetAmmoProbability(s: shortstring);
   439 procedure SetAmmoProbability(var s: shortstring);
   410 begin
   440 begin
   411     ammoProbability:= s;
   441     ammoProbability:= s;
   412 end;
   442 end;
   413 
   443 
   414 procedure SetAmmoDelay(s: shortstring);
   444 procedure SetAmmoDelay(var s: shortstring);
   415 begin
   445 begin
   416     ammoDelay:= s;
   446     ammoDelay:= s;
   417 end;
   447 end;
   418 
   448 
   419 procedure SetAmmoReinforcement(s: shortstring);
   449 procedure SetAmmoReinforcement(var s: shortstring);
   420 begin
   450 begin
   421     ammoReinforcement:= s;
   451     ammoReinforcement:= s;
   422 end;
   452 end;
   423 
   453 
   424 // Restore indefinitely disabled weapons and initial weapon counts.  Only used for hog placement right now
   454 // Restore indefinitely disabled weapons and initial weapon counts.  Only used for hog placement right now
   425 procedure ResetWeapons;
   455 procedure ResetWeapons;
   426 var i, t: Longword;
   456 var i, t: Longword;
   427     a: TAmmoType;
   457     a: TAmmoType;
   428 begin
   458 begin
   429 for t:= 0 to Pred(TeamsCount) do
   459 for t:= 0 to Pred(TeamsCount) do
   430    with TeamsArray[t]^ do
   460     with TeamsArray[t]^ do
   431       for i:= 0 to cMaxHHIndex do
   461         for i:= 0 to cMaxHHIndex do
   432           if Hedgehogs[i].Gear <> nil then
   462             Hedgehogs[i].CurAmmoType:= amNothing;
   433              begin
   463 
   434              FillAmmoStore(Hedgehogs[i].Ammo, InitialCounts[Hedgehogs[i].AmmoStore]);
   464 for i:= 0 to Pred(StoreCnt) do
   435              Hedgehogs[i].CurAmmoType:= amNothing
   465     FillAmmoStore(StoresList[i], InitialCounts[i]);
   436              end;
       
   437 
   466 
   438 for a:= Low(TAmmoType) to High(TAmmoType) do
   467 for a:= Low(TAmmoType) to High(TAmmoType) do
   439     if Ammoz[a].SkipTurns >= 10000 then dec(Ammoz[a].SkipTurns,10000)
   468     if Ammoz[a].SkipTurns >= 10000 then
       
   469         dec(Ammoz[a].SkipTurns,10000)
   440 end;
   470 end;
   441 
   471 
   442 
   472 
   443 
   473 
   444 procedure chAddAmmoStore(var descr: shortstring);
   474 procedure chAddAmmoStore(var descr: shortstring);
   446 descr:= ''; // avoid compiler hint
   476 descr:= ''; // avoid compiler hint
   447 AddAmmoStore
   477 AddAmmoStore
   448 end;
   478 end;
   449 
   479 
   450 procedure initModule;
   480 procedure initModule;
   451 begin
   481 var i: Longword;
   452     RegisterVariable('ammloadt', vtCommand, @SetAmmoLoadout, false);
   482 begin
   453     RegisterVariable('ammdelay', vtCommand, @SetAmmoDelay, false);
   483     RegisterVariable('ammloadt', @SetAmmoLoadout, false);
   454     RegisterVariable('ammprob',  vtCommand, @SetAmmoProbability, false);
   484     RegisterVariable('ammdelay', @SetAmmoDelay, false);
   455     RegisterVariable('ammreinf', vtCommand, @SetAmmoReinforcement, false);
   485     RegisterVariable('ammprob',  @SetAmmoProbability, false);
   456     RegisterVariable('ammstore', vtCommand, @chAddAmmoStore , false);
   486     RegisterVariable('ammreinf', @SetAmmoReinforcement, false);
       
   487     RegisterVariable('ammstore', @chAddAmmoStore , false);
   457 
   488 
   458     StoreCnt:= 0;
   489     StoreCnt:= 0;
   459     ammoLoadout:= '';
   490     ammoLoadout:= '';
   460     ammoProbability:= '';
   491     ammoProbability:= '';
   461     ammoDelay:= '';
   492     ammoDelay:= '';
   462     ammoReinforcement:= '';
   493     ammoReinforcement:= '';
       
   494     for i:=1 to ord(High(TAmmoType)) do
       
   495         begin
       
   496         ammoLoadout:= ammoLoadout + '0';
       
   497         ammoProbability:= ammoProbability + '0';
       
   498         ammoDelay:= ammoDelay + '0';
       
   499         ammoReinforcement:= ammoReinforcement + '0'
       
   500         end;
   463     FillChar(InitialCounts, sizeof(InitialCounts), 0)
   501     FillChar(InitialCounts, sizeof(InitialCounts), 0)
   464 end;
   502 end;
   465 
   503 
   466 procedure freeModule;
   504 procedure freeModule;
   467 var i: LongWord;
   505 var i: LongWord;
   468 begin
   506 begin
   469     if StoreCnt > 0 then
   507     if StoreCnt > 0 then
   470         for i:= 0 to Pred(StoreCnt) do Dispose(StoresList[i])
   508         for i:= 0 to Pred(StoreCnt) do
       
   509             Dispose(StoresList[i])
   471 end;
   510 end;
   472 
   511 
   473 end.
   512 end.