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 void GiveResourceWithLimit(entity receiver, int resource_type, float amount,
151 float current_amount = GetResourceAmount(receiver, resource_type);
152 if (current_amount + amount > limit)
154 amount = limit - current_amount;
156 GiveResource(receiver, resource_type, amount);
159 int GetResourceType(.float resource_field)
161 switch (resource_field)
163 case health: { return RESOURCE_HEALTH; }
164 case armorvalue: { return RESOURCE_ARMOR; }
165 case ammo_shells: { return RESOURCE_SHELLS; }
166 case ammo_nails: { return RESOURCE_BULLETS; }
167 case ammo_rockets: { return RESOURCE_ROCKETS; }
168 case ammo_cells: { return RESOURCE_CELLS; }
169 case ammo_plasma: { return RESOURCE_PLASMA; }
170 case ammo_fuel: { return RESOURCE_FUEL; }
172 error("GetResourceType: Invalid field.");
176 .float GetResourceField(int resource_type)
178 switch (resource_type)
180 case RESOURCE_HEALTH: { return health; }
181 case RESOURCE_ARMOR: { return armorvalue; }
182 case RESOURCE_SHELLS: { return ammo_shells; }
183 case RESOURCE_BULLETS: { return ammo_nails; }
184 case RESOURCE_ROCKETS: { return ammo_rockets; }
185 case RESOURCE_CELLS: { return ammo_cells; }
186 case RESOURCE_PLASMA: { return ammo_plasma; }
187 case RESOURCE_FUEL: { return ammo_fuel; }
189 error("GetResourceField: Invalid resource type.");