1 #include "resources.qh"
2 #include <common/items/item/ammo.qh>
5 /// \brief Source file that contains implementation of the resource system.
6 /// \copyright GNU GPLv2 or any later version.
8 float GetResourceAmount(entity e, int resource_type)
10 .float resource_field = GetResourceField(resource_type);
11 return e.(resource_field);
14 void SetResourceAmount(entity e, int resource_type, float amount)
16 .float resource_field = GetResourceField(resource_type);
17 e.(resource_field) = amount;
20 void TakeResource(entity receiver, int resource_type, float amount)
26 SetResourceAmount(receiver, resource_type,
27 GetResourceAmount(receiver, resource_type) - amount);
30 void TakeResourceWithLimit(entity receiver, int resource_type, float amount,
37 float current_amount = GetResourceAmount(receiver, resource_type);
38 if (current_amount - amount < limit)
40 amount = limit + current_amount;
42 TakeResource(receiver, resource_type, amount);
45 int GetResourceType(.float resource_field)
47 switch (resource_field)
49 case health: { return RESOURCE_HEALTH; }
50 case armorvalue: { return RESOURCE_ARMOR; }
51 case ammo_shells: { return RESOURCE_SHELLS; }
52 case ammo_nails: { return RESOURCE_BULLETS; }
53 case ammo_rockets: { return RESOURCE_ROCKETS; }
54 case ammo_cells: { return RESOURCE_CELLS; }
55 case ammo_plasma: { return RESOURCE_PLASMA; }
56 case ammo_fuel: { return RESOURCE_FUEL; }
58 error("GetResourceType: Invalid field.");
62 .float GetResourceField(int resource_type)
64 switch (resource_type)
66 case RESOURCE_HEALTH: { return health; }
67 case RESOURCE_ARMOR: { return armorvalue; }
68 case RESOURCE_SHELLS: { return ammo_shells; }
69 case RESOURCE_BULLETS: { return ammo_nails; }
70 case RESOURCE_ROCKETS: { return ammo_rockets; }
71 case RESOURCE_CELLS: { return ammo_cells; }
72 case RESOURCE_PLASMA: { return ammo_plasma; }
73 case RESOURCE_FUEL: { return ammo_fuel; }
75 error("GetResourceField: Invalid resource type.");