hedgewars/uAmmos.pas
changeset 3836 833c0f32e326
parent 3697 d5b30d6373fc
child 3931 fd04228971b3
child 3935 5ca27a0e9a63
equal deleted inserted replaced
3835:cbee69165c6a 3836:833c0f32e326
    40 procedure SwitchNotHeldAmmo(var Hedgehog: THedgehog);
    40 procedure SwitchNotHeldAmmo(var Hedgehog: THedgehog);
    41 procedure SetWeapon(weap: TAmmoType);
    41 procedure SetWeapon(weap: TAmmoType);
    42 procedure DisableSomeWeapons;
    42 procedure DisableSomeWeapons;
    43 procedure ResetWeapons;
    43 procedure ResetWeapons;
    44 function  GetAmmoByNum(num: Longword): PHHAmmo;
    44 function  GetAmmoByNum(num: Longword): PHHAmmo;
       
    45 function  GetAmmoEntry(var Hedgehog: THedgehog): PAmmo;
    45 
    46 
    46 var shoppa: boolean;
    47 var shoppa: boolean;
    47 
    48 
    48 implementation
    49 implementation
    49 uses uMisc, uGears, uWorld, uLocale, uConsole;
    50 uses uMisc, uGears, uWorld, uLocale, uConsole;
   154 begin
   155 begin
   155 TryDo(num < StoreCnt, 'Invalid store number', true);
   156 TryDo(num < StoreCnt, 'Invalid store number', true);
   156 exit(StoresList[num])
   157 exit(StoresList[num])
   157 end;
   158 end;
   158 
   159 
       
   160 function GetAmmoEntry(var Hedgehog: THedgehog): PAmmo;
       
   161 var ammoidx, slot: LongWord;
       
   162 begin
       
   163 with Hedgehog do
       
   164     begin
       
   165     slot:= Ammoz[CurAmmoType].Slot;
       
   166     ammoidx:= 0;
       
   167     while (ammoidx < cMaxSlotAmmoIndex) and (Ammo^[slot, ammoidx].AmmoType <> CurAmmoType) do inc(ammoidx);
       
   168     GetAmmoEntry:= @Ammo^[slot, ammoidx];
       
   169     end
       
   170 end;
       
   171 
   159 procedure AssignStores;
   172 procedure AssignStores;
   160 var t: LongInt;
   173 var t: LongInt;
   161     i: Longword;
   174     i: Longword;
   162 begin
   175 begin
   163 for t:= 0 to Pred(TeamsCount) do
   176 for t:= 0 to Pred(TeamsCount) do
   164    with TeamsArray[t]^ do
   177    with TeamsArray[t]^ do
   165       begin
   178       begin
   166       for i:= 0 to cMaxHHIndex do
   179       for i:= 0 to cMaxHHIndex do
   167           if Hedgehogs[i].Gear <> nil then
   180           if Hedgehogs[i].Gear <> nil then
       
   181              begin
   168              Hedgehogs[i].Ammo:= GetAmmoByNum(Hedgehogs[i].AmmoStore);
   182              Hedgehogs[i].Ammo:= GetAmmoByNum(Hedgehogs[i].AmmoStore);
       
   183              Hedgehogs[i].CurAmmoType:= amNothing;
       
   184              end
   169       end
   185       end
   170 end;
   186 end;
   171 
   187 
   172 procedure AddAmmo(var Hedgehog: THedgehog; ammo: TAmmoType);
   188 procedure AddAmmo(var Hedgehog: THedgehog; ammo: TAmmoType);
   173 var ammos: TAmmoCounts;
   189 var ammos: TAmmoCounts;
   211          end;
   227          end;
   212     until not b;
   228     until not b;
   213 end;
   229 end;
   214 
   230 
   215 procedure OnUsedAmmo(var Hedgehog: THedgehog);
   231 procedure OnUsedAmmo(var Hedgehog: THedgehog);
   216 begin
   232 var CurWeapon: PAmmo;
   217 with Hedgehog do
   233 begin
   218     begin
   234 CurWeapon:= GetAmmoEntry(Hedgehog);
       
   235 with Hedgehog do
       
   236     begin
       
   237 
   219     MultiShootAttacks:= 0;
   238     MultiShootAttacks:= 0;
   220     with Ammo^[CurSlot, CurAmmo] do
   239     with CurWeapon^ do
   221         if Count <> AMMO_INFINITE then
   240         if Count <> AMMO_INFINITE then
   222             begin
   241             begin
   223             dec(Count);
   242             dec(Count);
   224             if Count = 0 then
   243             if Count = 0 then
   225                 begin
   244                 begin
   226                 PackAmmo(Ammo, CurSlot);
   245                 PackAmmo(Ammo, Ammoz[AmmoType].Slot);
   227                 SwitchNotHeldAmmo(Hedgehog)
   246                 SwitchNotHeldAmmo(Hedgehog)
   228                 end
   247                 end
   229             end
   248             end
   230     end
   249     end
   231 end;
   250 end;
   262         end
   281         end
   263     end
   282     end
   264 end;
   283 end;
   265 
   284 
   266 procedure SwitchToFirstLegalAmmo(var Hedgehog: THedgehog);
   285 procedure SwitchToFirstLegalAmmo(var Hedgehog: THedgehog);
   267 begin
   286 var slot, ammoidx: LongWord;
   268 with Hedgehog do
   287 begin
   269     begin
   288 with Hedgehog do
   270     CurAmmo:= 0;
   289     begin
   271     CurSlot:= 0;
   290     CurAmmoType:= amNothing;
   272     while (CurSlot <= cMaxSlotIndex) and
   291     slot:= 0;
   273         ((Ammo^[CurSlot, CurAmmo].Count = 0) or
   292     ammoidx:= 0;
   274         (Ammoz[Ammo^[CurSlot, CurAmmo].AmmoType].SkipTurns - CurrentTeam^.Clan^.TurnNumber >= 0))
   293     while (slot <= cMaxSlotIndex) and
       
   294         ((Ammo^[slot, ammoidx].Count = 0) or
       
   295         (Ammoz[Ammo^[slot, ammoidx].AmmoType].SkipTurns - CurrentTeam^.Clan^.TurnNumber >= 0))
   275         do
   296         do
   276         begin
   297         begin
   277         while (CurAmmo <= cMaxSlotAmmoIndex) and
   298         while (ammoidx <= cMaxSlotAmmoIndex) and
   278             ((Ammo^[CurSlot, CurAmmo].Count = 0) or
   299             ((Ammo^[slot, ammoidx].Count = 0) or
   279             (Ammoz[Ammo^[CurSlot, CurAmmo].AmmoType].SkipTurns - CurrentTeam^.Clan^.TurnNumber >= 0))
   300             (Ammoz[Ammo^[slot, ammoidx].AmmoType].SkipTurns - CurrentTeam^.Clan^.TurnNumber >= 0))
   280             do inc(CurAmmo);
   301             do inc(ammoidx);
   281 
   302 
   282         if (CurAmmo > cMaxSlotAmmoIndex) then
   303         if (ammoidx > cMaxSlotAmmoIndex) then
   283             begin
   304             begin
   284             CurAmmo:= 0;
   305             ammoidx:= 0;
   285             inc(CurSlot)
   306             inc(slot)
   286             end
   307             end
   287         end;
   308         end;
   288     TryDo(CurSlot <= cMaxSlotIndex, 'Ammo slot index overflow', true)
   309     TryDo(slot <= cMaxSlotIndex, 'Ammo slot index overflow', true);
       
   310     CurAmmoType:= Ammo^[slot, ammoidx].AmmoType;
   289     end
   311     end
   290 end;
   312 end;
   291 
   313 
   292 procedure ApplyAmmoChanges(var Hedgehog: THedgehog);
   314 procedure ApplyAmmoChanges(var Hedgehog: THedgehog);
   293 var s: shortstring;
   315 var s: shortstring;
       
   316     CurWeapon: PAmmo;
   294 begin
   317 begin
   295 TargetPoint.X:= NoPointX;
   318 TargetPoint.X:= NoPointX;
   296 
   319 
   297 with Hedgehog do
   320 with Hedgehog do
   298     begin
   321     begin
   299     Timer:= 10;
   322     Timer:= 10;
   300 
   323 
   301     if (Ammo^[CurSlot, CurAmmo].Count = 0) then
   324     CurWeapon:= GetAmmoEntry(Hedgehog);
       
   325 
       
   326     if (CurWeapon^.Count = 0) then
   302         SwitchToFirstLegalAmmo(Hedgehog);
   327         SwitchToFirstLegalAmmo(Hedgehog);
   303 
   328 
   304         //bad things could happen here in case CurSlot is overflowing
   329     CurWeapon:= GetAmmoEntry(Hedgehog);
   305     ApplyAngleBounds(Hedgehog, Ammo^[CurSlot, CurAmmo].AmmoType);
   330 
   306 
   331     ApplyAngleBounds(Hedgehog, CurWeapon^.AmmoType);
   307     with Ammo^[CurSlot, CurAmmo] do
   332 
       
   333     with CurWeapon^ do
   308         begin
   334         begin
   309         if AmmoType <> amNothing then
   335         if AmmoType <> amNothing then
   310             begin
   336             begin
   311             s:= trammo[Ammoz[AmmoType].NameId];
   337             s:= trammo[Ammoz[AmmoType].NameId];
   312             if (Count <> AMMO_INFINITE) and not (Hedgehog.Team^.ExtDriven or (Hedgehog.BotLevel > 0)) then
   338             if (Count <> AMMO_INFINITE) and not (Hedgehog.Team^.ExtDriven or (Hedgehog.BotLevel > 0)) then
   321             isCursorVisible:= true
   347             isCursorVisible:= true
   322             end else begin
   348             end else begin
   323             Gear^.State:= Gear^.State and not gstHHChooseTarget;
   349             Gear^.State:= Gear^.State and not gstHHChooseTarget;
   324             isCursorVisible:= false
   350             isCursorVisible:= false
   325             end;
   351             end;
   326         if (CurAmmoGear <> nil) and ((CurAmmoGear^.Ammo^.Propz and ammoprop_AltAttack) <> 0) then
   352         if (CurAmmoGear <> nil) and ((Ammoz[CurAmmoGear^.AmmoType].Ammo.Propz and ammoprop_AltAttack) <> 0) then
   327             ShowCrosshair:= (CurAmmoGear^.Ammo^.Propz and ammoprop_NoCrossHair) = 0
   353             ShowCrosshair:= (Ammoz[CurAmmoGear^.AmmoType].Ammo.Propz and ammoprop_NoCrossHair) = 0
   328         else
   354         else
   329             ShowCrosshair:= (Propz and ammoprop_NoCrosshair) = 0;
   355             ShowCrosshair:= (Propz and ammoprop_NoCrosshair) = 0;
   330         end
   356         end
   331     end
   357     end
   332 end;
   358 end;
   333 
   359 
   334 procedure SwitchNotHeldAmmo(var Hedgehog: THedgehog);
   360 procedure SwitchNotHeldAmmo(var Hedgehog: THedgehog);
   335 begin
   361 begin
   336 with Hedgehog do
   362 with Hedgehog do
   337     if ((Ammo^[CurSlot, CurAmmo].Propz and ammoprop_DontHold) <> 0) or
   363     if ((Ammoz[CurAmmoType].Ammo.Propz and ammoprop_DontHold) <> 0) or
   338         (Ammoz[Ammo^[CurSlot, CurAmmo].AmmoType].SkipTurns - CurrentTeam^.Clan^.TurnNumber >= 0) then
   364         (Ammoz[CurAmmoType].SkipTurns - CurrentTeam^.Clan^.TurnNumber >= 0) then
   339         SwitchToFirstLegalAmmo(Hedgehog);
   365         SwitchToFirstLegalAmmo(Hedgehog);
   340 end;
   366 end;
   341 
   367 
   342 procedure SetWeapon(weap: TAmmoType);
   368 procedure SetWeapon(weap: TAmmoType);
   343 begin
   369 begin