api-Hypernova / ac2-game-src

Temporary source base for AC2 game code, soon to be ported to Rival
1 stars 0 forks source link

Add ammo system #45

Open api-Hypernova opened 7 years ago

api-Hypernova commented 7 years ago

Add a basic ammo system where the player spawns with a certain amount of ammo for each weapon, and firing reduces the ammo count by the amount specified in the gun metadata struct. Adding ammo via pickups will come later (after Bullet Physics integration).

api-Hypernova commented 7 years ago

Changes:

api-Hypernova commented 7 years ago

On second thoughts: We should be setting up ammo by ammo types! Not ammo-per-attack. Each attack should have a pointer to which kind of ammo it uses. gameent should have an array of size NUMAMMOTYPES. Shooting simply does: d->ammo[atk]-=atk->sub; This way the ammo is kept synchronized over different ammo types and weapons that use the same ammo. This data could also drive the HUD when it's looking for what to render (multiple ammo types per weapon, or just one). To find which firing mode is being used in the current attack: int gun = d->gunselect, act = d->attacking, atk = guns[gun].attacks[act]; This is yet another reason to remove the server verification code for now.

api-Hypernova commented 7 years ago

Blocked by #48 (remove server correctness checks).

api-Hypernova commented 7 years ago

Gameent changes are done. Added a new enum/struct combo in game.h for storing ammo types: enum { AMMO_NONE=0, AMMO_SHELLS, AMMO_PULSE, AMMO_556, //M4 ammo AMMO_357, AMMO_9MM, AMMO_IMPACTNADE, //smg grenade AMMO_ORB, AMMO_ELECTROBOLT, AMMO_CELLS, //electrobolt lightning AMMO_SHOCK, //shockrifle AMMO_MISSILE, NUMAMMOTYPES }; static struct _ammodata { int add, max; const char *icon; } ammodata[NUMAMMOTYPES] = { //index with enum above { 1, 1, "infinite" }, { 6, 30, "shells" }, { 30, 90, "pulse" }, { 20, 40, "556" }, { 6, 24, "357" }, { 12, 48, "9mm" }, { 1, 10, "smgnade" }, { 1, 3, "orb" }, { 5, 30, "electrobolt" }, { 40, 120, "lightning" }, { 5, 15, "shock" }, { 1, 10, "rpg_missile" } };

The gamestate->ammo[] is now defined as: int ammo[NUMAMMOTYPES];

Also added a new macro to shared/tools.h for grabbing the size of a raw array: #define ARRSIZE(x) (sizeof(x) / sizeof((x)[0]))

Gamestate->hasammo() is now: bool hasammo(int gun, int exclude = -1) { loopi(ARRSIZE(guns[gun].attacks)) { if(ammo[guns[gun].attacks[i]]) return validgun(gun) && gun != exclude; } return false; }

and etc. All that's left is to update the usages everywhere.

api-Hypernova commented 7 years ago

Checking if player has ammo (either fire mode) for a gun: player->hasammo(gun) Checking amount of ammo left for particular fire mode: player->ammo[ATKAMMOTYPE(attack)]

api-Hypernova commented 7 years ago

The system is functioning, however there seems to be a bug with player->hasammo(gun) not retuning always correctly. Disabling this check in game::gunselect() allows switching to weapons which have ammo, while hasammo() was denying it.

api-Hypernova commented 7 years ago

Standing bugs:

  1. gameent->hasammo() not working properly.
  2. "pulse_rifle" and "railgun" not being provisioned with ammo from the spawn.
  3. HUD ammo string contains gibberish.