1 #include "resources.qh"
3 #include <common/items/item/ammo.qh>
6 /// \brief Source file that contains implementation of the resource system.
7 /// \copyright GNU GPLv2 or any later version.
9 float GetResource(entity e, int res_type)
11 return e.(GetResourceField(res_type));
14 bool SetResourceExplicit(entity e, int res_type, float amount)
16 .float res_field = GetResourceField(res_type);
17 if (e.(res_field) != amount)
19 e.(res_field) = amount;
25 void SetResource(entity e, int res_type, float amount)
27 SetResourceExplicit(e, res_type, amount);
30 void TakeResource(entity receiver, int res_type, float amount)
36 SetResource(receiver, res_type, GetResource(receiver, res_type) - amount);
39 void TakeResourceWithLimit(entity receiver, int res_type, float amount, float limit)
45 float current_amount = GetResource(receiver, res_type);
46 if (current_amount - amount < limit)
48 amount = limit + current_amount;
50 TakeResource(receiver, res_type, amount);
53 int GetResourceType(.float res_field)
57 case health: { return RES_HEALTH; }
58 case armorvalue: { return RES_ARMOR; }
59 case ammo_shells: { return RES_SHELLS; }
60 case ammo_nails: { return RES_BULLETS; }
61 case ammo_rockets: { return RES_ROCKETS; }
62 case ammo_cells: { return RES_CELLS; }
63 case ammo_plasma: { return RES_PLASMA; }
64 case ammo_fuel: { return RES_FUEL; }
66 error("GetResourceType: Invalid field.");
70 .float GetResourceField(int res_type)
74 case RES_HEALTH: { return health; }
75 case RES_ARMOR: { return armorvalue; }
76 case RES_SHELLS: { return ammo_shells; }
77 case RES_BULLETS: { return ammo_nails; }
78 case RES_ROCKETS: { return ammo_rockets; }
79 case RES_CELLS: { return ammo_cells; }
80 case RES_PLASMA: { return ammo_plasma; }
81 case RES_FUEL: { return ammo_fuel; }
83 error("GetResourceField: Invalid resource type.");