2 /// \brief Source file that contains implementation of the resource system.
4 /// \copyright GNU GPLv3 or any later version.
6 #include "resources.qh"
8 #include "autocvars.qh"
9 #include "miscfunctions.qh"
11 float GetResourceLimit(entity e, int resource_type)
14 switch (resource_type)
18 limit = autocvar_g_balance_health_limit;
23 limit = autocvar_g_balance_armor_limit;
28 limit = g_pickup_shells_max;
31 case RESOURCE_BULLETS:
33 limit = g_pickup_nails_max;
36 case RESOURCE_ROCKETS:
38 limit = g_pickup_rockets_max;
43 limit = g_pickup_cells_max;
48 limit = g_pickup_plasma_max;
53 limit = autocvar_g_balance_fuel_limit;
58 error("GetResourceLimit: Invalid resource type.");
62 MUTATOR_CALLHOOK(GetResourceLimit, e, resource_type, limit);
63 limit = M_ARGV(2, float);
64 if (limit > RESOURCE_AMOUNT_HARD_LIMIT)
66 limit = RESOURCE_AMOUNT_HARD_LIMIT;
71 float GetResourceAmount(entity e, int resource_type)
73 .float resource_field = GetResourceField(resource_type);
74 return e.(resource_field);
77 void SetResourceAmount(entity e, int resource_type, float amount)
79 bool forbid = MUTATOR_CALLHOOK(SetResourceAmount, e, resource_type, amount);
84 resource_type = M_ARGV(1, int);
85 amount = M_ARGV(2, float);
86 .float resource_field = GetResourceField(resource_type);
87 if (e.(resource_field) == amount)
91 float max_amount = GetResourceLimit(e, resource_type);
92 if (amount > max_amount)
96 e.(resource_field) = amount;
99 void GiveResource(entity receiver, int resource_type, float amount)
105 bool forbid = MUTATOR_CALLHOOK(GiveResource, receiver, resource_type,
111 resource_type = M_ARGV(1, int);
112 amount = M_ARGV(2, float);
117 SetResourceAmount(receiver, resource_type,
118 GetResourceAmount(receiver, resource_type) + amount);
119 switch (resource_type)
121 case RESOURCE_HEALTH:
123 receiver.pauserothealth_finished =
124 max(receiver.pauserothealth_finished, time +
125 autocvar_g_balance_pause_health_rot);
130 receiver.pauserotarmor_finished =
131 max(receiver.pauserotarmor_finished, time +
132 autocvar_g_balance_pause_armor_rot);
137 receiver.pauserotfuel_finished = max(receiver.pauserotfuel_finished,
138 time + autocvar_g_balance_pause_fuel_rot);
144 int GetResourceType(.float resource_field)
146 switch (resource_field)
148 case health: { return RESOURCE_HEALTH; }
149 case armorvalue: { return RESOURCE_ARMOR; }
150 case ammo_shells: { return RESOURCE_SHELLS; }
151 case ammo_nails: { return RESOURCE_BULLETS; }
152 case ammo_rockets: { return RESOURCE_ROCKETS; }
153 case ammo_cells: { return RESOURCE_CELLS; }
154 case ammo_plasma: { return RESOURCE_PLASMA; }
155 case ammo_fuel: { return RESOURCE_FUEL; }
157 error("GetResourceType: Invalid field.");
161 .float GetResourceField(int resource_type)
163 switch (resource_type)
165 case RESOURCE_HEALTH: { return health; }
166 case RESOURCE_ARMOR: { return armorvalue; }
167 case RESOURCE_SHELLS: { return ammo_shells; }
168 case RESOURCE_BULLETS: { return ammo_nails; }
169 case RESOURCE_ROCKETS: { return ammo_rockets; }
170 case RESOURCE_CELLS: { return ammo_cells; }
171 case RESOURCE_PLASMA: { return ammo_plasma; }
172 case RESOURCE_FUEL: { return ammo_fuel; }
174 error("GetResourceField: Invalid resource type.");