1 #include "resources.qh"
3 /// \brief Source file that contains implementation of the resource system.
5 /// \copyright GNU GPLv2 or any later version.
7 #include "autocvars.qh"
8 #include "miscfunctions.qh"
10 float GetResourceLimit(entity e, int resource_type)
13 switch (resource_type)
17 limit = autocvar_g_balance_health_limit;
22 limit = autocvar_g_balance_armor_limit;
27 limit = g_pickup_shells_max;
30 case RESOURCE_BULLETS:
32 limit = g_pickup_nails_max;
35 case RESOURCE_ROCKETS:
37 limit = g_pickup_rockets_max;
42 limit = g_pickup_cells_max;
47 limit = g_pickup_plasma_max;
52 limit = autocvar_g_balance_fuel_limit;
57 error("GetResourceLimit: Invalid resource type.");
61 MUTATOR_CALLHOOK(GetResourceLimit, e, resource_type, limit);
62 limit = M_ARGV(2, float);
63 if (limit > RESOURCE_AMOUNT_HARD_LIMIT)
65 limit = RESOURCE_AMOUNT_HARD_LIMIT;
70 float GetResourceAmount(entity e, int resource_type)
72 .float resource_field = GetResourceField(resource_type);
73 return e.(resource_field);
76 void SetResourceAmount(entity e, int resource_type, float amount)
78 bool forbid = MUTATOR_CALLHOOK(SetResourceAmount, e, resource_type, amount);
83 resource_type = M_ARGV(1, int);
84 amount = M_ARGV(2, float);
85 .float resource_field = GetResourceField(resource_type);
86 if (e.(resource_field) == amount)
90 float max_amount = GetResourceLimit(e, resource_type);
91 if (amount > max_amount)
95 e.(resource_field) = amount;
98 void GiveResource(entity receiver, int resource_type, float amount)
104 bool forbid = MUTATOR_CALLHOOK(GiveResource, receiver, resource_type,
110 resource_type = M_ARGV(1, int);
111 amount = M_ARGV(2, float);
116 SetResourceAmount(receiver, resource_type,
117 GetResourceAmount(receiver, resource_type) + amount);
118 switch (resource_type)
120 case RESOURCE_HEALTH:
122 receiver.pauserothealth_finished =
123 max(receiver.pauserothealth_finished, time +
124 autocvar_g_balance_pause_health_rot);
129 receiver.pauserotarmor_finished =
130 max(receiver.pauserotarmor_finished, time +
131 autocvar_g_balance_pause_armor_rot);
136 receiver.pauserotfuel_finished = max(receiver.pauserotfuel_finished,
137 time + autocvar_g_balance_pause_fuel_rot);
143 void GiveResourceWithLimit(entity receiver, int resource_type, float amount,
150 float current_amount = GetResourceAmount(receiver, resource_type);
151 if (current_amount + amount > limit)
153 amount = limit - current_amount;
155 GiveResource(receiver, resource_type, amount);
158 int GetResourceType(.float resource_field)
160 switch (resource_field)
162 case health: { return RESOURCE_HEALTH; }
163 case armorvalue: { return RESOURCE_ARMOR; }
164 case ammo_shells: { return RESOURCE_SHELLS; }
165 case ammo_nails: { return RESOURCE_BULLETS; }
166 case ammo_rockets: { return RESOURCE_ROCKETS; }
167 case ammo_cells: { return RESOURCE_CELLS; }
168 case ammo_plasma: { return RESOURCE_PLASMA; }
169 case ammo_fuel: { return RESOURCE_FUEL; }
171 error("GetResourceType: Invalid field.");
175 .float GetResourceField(int resource_type)
177 switch (resource_type)
179 case RESOURCE_HEALTH: { return health; }
180 case RESOURCE_ARMOR: { return armorvalue; }
181 case RESOURCE_SHELLS: { return ammo_shells; }
182 case RESOURCE_BULLETS: { return ammo_nails; }
183 case RESOURCE_ROCKETS: { return ammo_rockets; }
184 case RESOURCE_CELLS: { return ammo_cells; }
185 case RESOURCE_PLASMA: { return ammo_plasma; }
186 case RESOURCE_FUEL: { return ammo_fuel; }
188 error("GetResourceField: Invalid resource type.");