Skip to content

Commit

Permalink
Fix crash when bg too small, when exiting game with custom ammo
Browse files Browse the repository at this point in the history
  • Loading branch information
cxong committed Jun 22, 2024
1 parent c858fd0 commit 936adcc
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 128 deletions.
29 changes: 14 additions & 15 deletions src/cdogs/actors.c
Original file line number Diff line number Diff line change
Expand Up @@ -1001,9 +1001,8 @@ static bool ActorTryChangeDirection(
{
const bool willChangeDirecton =
!actor->petrified && CMD_HAS_DIRECTION(cmd) &&
(!Button2(cmd) ||
ConfigGetEnum(&gConfig, "Game.SwitchMoveStyle") !=
SWITCHMOVE_STRAFE) &&
(!Button2(cmd) || ConfigGetEnum(&gConfig, "Game.SwitchMoveStyle") !=
SWITCHMOVE_STRAFE) &&
(!Button1(prevCmd) ||
ConfigGetEnum(&gConfig, "Game.FireMoveStyle") != FIREMOVE_STRAFE);
const direction_e dir = CmdToDirection(cmd);
Expand Down Expand Up @@ -1128,13 +1127,13 @@ void CommandActor(TActor *actor, int cmd, int ticks)
static bool ActorTryMove(TActor *actor, int cmd, int ticks)
{
const bool canMoveWhenShooting =
actor->PlayerUID < 0 ? (actor->flags & FLAGS_MOVE_AND_SHOOT) :
(
ConfigGetEnum(&gConfig, "Game.FireMoveStyle") != FIREMOVE_STOP ||
(ConfigGetEnum(&gConfig, "Game.SwitchMoveStyle") ==
SWITCHMOVE_STRAFE &&
Button2(cmd))
);
actor->PlayerUID < 0
? (actor->flags & FLAGS_MOVE_AND_SHOOT)
: (ConfigGetEnum(&gConfig, "Game.FireMoveStyle") !=
FIREMOVE_STOP ||
(ConfigGetEnum(&gConfig, "Game.SwitchMoveStyle") ==
SWITCHMOVE_STRAFE &&
Button2(cmd)));
const bool canMove = !actor->hasShot || canMoveWhenShooting;
const bool willMove =
!actor->petrified && CMD_HAS_DIRECTION(cmd) && canMove;
Expand Down Expand Up @@ -1479,7 +1478,7 @@ static void ActorDie(TActor *actor)
GameEvent e = GameEventNew(GAME_EVENT_ACTOR_DIE);
e.u.ActorDie.UID = actor->uid;
GameEventsEnqueue(&gGameEvents, e);

// Persist player ammo for when they respawn
ActorPersistPlayerWeaponsAndAmmo(actor);
}
Expand Down Expand Up @@ -1701,10 +1700,10 @@ TActor *ActorAdd(NActorAdd aa)
{
// Use persisted ammo amount if it is greater,
// or if buy/sell is enabled
if (gCampaign.Setting.BuyAndSell ||
(int)aa.Ammo[i].Amount >
AmmoGetById(&gAmmo, aa.Ammo[i].Id)->Amount *
AMMO_STARTING_MULTIPLE)
const Ammo *ammo = AmmoGetById(&gAmmo, aa.Ammo[i].Id);
if (ammo && (gCampaign.Setting.BuyAndSell ||
(int)aa.Ammo[i].Amount >
ammo->Amount * AMMO_STARTING_MULTIPLE))
{
CArraySet(&actor->ammo, aa.Ammo[i].Id, &aa.Ammo[i].Amount);
}
Expand Down
85 changes: 42 additions & 43 deletions src/cdogs/ammo.c
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
/*
C-Dogs SDL
A port of the legendary (and fun) action/arcade cdogs.
Copyright (c) 2014, 2016-2019, 2023 Cong Xu
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
C-Dogs SDL
A port of the legendary (and fun) action/arcade cdogs.
Copyright (c) 2014, 2016-2019, 2023-2024 Cong Xu
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
#include "ammo.h"

Expand All @@ -34,10 +34,8 @@
#include "pic_manager.h"
#include "utils.h"


AmmoClasses gAmmo;


// TODO: use map structure?
Ammo *StrAmmo(const char *s)
{
Expand All @@ -50,16 +48,16 @@ int StrAmmoId(const char *s)
return 0;
}
CA_FOREACH(Ammo, a, gAmmo.CustomAmmo)
if (strcmp(s, a->Name) == 0)
{
return _ca_index + (int)gAmmo.Ammo.size;
}
if (strcmp(s, a->Name) == 0)
{
return _ca_index + (int)gAmmo.Ammo.size;
}
CA_FOREACH_END()
CA_FOREACH(Ammo, a, gAmmo.Ammo)
if (strcmp(s, a->Name) == 0)
{
return _ca_index;
}
if (strcmp(s, a->Name) == 0)
{
return _ca_index;
}
CA_FOREACH_END()
CASSERT(false, "cannot parse ammo name");
return 0;
Expand Down Expand Up @@ -87,8 +85,8 @@ void AmmoInitialize(AmmoClasses *ammo, const char *path)
e = json_stream_parse(f, &root);
if (e != JSON_OK)
{
LOG(LM_MAIN, LL_ERROR, "Error parsing ammo file %s [error %d]",
buf, (int)e);
LOG(LM_MAIN, LL_ERROR, "Error parsing ammo file %s [error %d]", buf,
(int)e);
goto bail;
}
AmmoLoadJSON(&ammo->Ammo, root);
Expand Down Expand Up @@ -139,9 +137,9 @@ static void LoadAmmo(Ammo *a, json_t *node, const int version)
void AmmoClassesClear(CArray *ammo)
{
CA_FOREACH(Ammo, a, *ammo)
CFREE(a->Name);
CFREE(a->Sound);
CFREE(a->DefaultGun);
CFREE(a->Name);
CFREE(a->Sound);
CFREE(a->DefaultGun);
CA_FOREACH_END()
CArrayClear(ammo);
}
Expand All @@ -159,18 +157,19 @@ Ammo *AmmoGetById(AmmoClasses *ammo, const int id)
{
return CArrayGet(&ammo->Ammo, id);
}
else
const int id2 = id - (int)ammo->Ammo.size;
if (id < (int)ammo->CustomAmmo.size)
{
return CArrayGet(&ammo->CustomAmmo, id - (int)ammo->Ammo.size);
return CArrayGet(&ammo->CustomAmmo, id2);
}
return NULL;
}

int AmmoGetNumClasses(const AmmoClasses *ammo)
{
return (int)ammo->Ammo.size + (int)ammo->CustomAmmo.size;
}


bool AmmoIsLow(const Ammo *a, const int amount)
{
// Low ammo if less than or equal to half the default amount
Expand Down
Loading

0 comments on commit 936adcc

Please sign in to comment.