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 bool SetResourceAmountExplicit(entity e, int resource_type, float amount)
16 .float resource_field = GetResourceField(resource_type);
17 if (e.(resource_field) != amount)
19 e.(resource_field) = amount;
25 void SetResourceAmount(entity e, int resource_type, float amount)
27 SetResourceAmountExplicit(e, resource_type, amount);
30 void TakeResource(entity receiver, int resource_type, float amount)
36 SetResourceAmount(receiver, resource_type,
37 GetResourceAmount(receiver, resource_type) - amount);
40 void TakeResourceWithLimit(entity receiver, int resource_type, float amount,
47 float current_amount = GetResourceAmount(receiver, resource_type);
48 if (current_amount - amount < limit)
50 amount = limit + current_amount;
52 TakeResource(receiver, resource_type, amount);
55 int GetResourceType(.float resource_field)
57 switch (resource_field)
59 case health: { return RESOURCE_HEALTH; }
60 case armorvalue: { return RESOURCE_ARMOR; }
61 case ammo_shells: { return RESOURCE_SHELLS; }
62 case ammo_nails: { return RESOURCE_BULLETS; }
63 case ammo_rockets: { return RESOURCE_ROCKETS; }
64 case ammo_cells: { return RESOURCE_CELLS; }
65 case ammo_plasma: { return RESOURCE_PLASMA; }
66 case ammo_fuel: { return RESOURCE_FUEL; }
68 error("GetResourceType: Invalid field.");
72 .float GetResourceField(int resource_type)
74 switch (resource_type)
76 case RESOURCE_HEALTH: { return health; }
77 case RESOURCE_ARMOR: { return armorvalue; }
78 case RESOURCE_SHELLS: { return ammo_shells; }
79 case RESOURCE_BULLETS: { return ammo_nails; }
80 case RESOURCE_ROCKETS: { return ammo_rockets; }
81 case RESOURCE_CELLS: { return ammo_cells; }
82 case RESOURCE_PLASMA: { return ammo_plasma; }
83 case RESOURCE_FUEL: { return ammo_fuel; }
85 error("GetResourceField: Invalid resource type.");