Xonotic QuakeC
The free, fast arena FPS with crisp movement and a wide array of weapons
sv_resources.qc
Go to the documentation of this file.
1#include "sv_resources.qh"
2
7
10#include <server/world.qh>
11
13{
14 if(!IS_PLAYER(e))
15 return RES_LIMIT_NONE; // no limits on non-players
16
17 float limit;
18 // TODO: registry handles
19 switch (res_type)
20 {
21 case RES_HEALTH:
22 {
24 break;
25 }
26 case RES_ARMOR:
27 {
29 break;
30 }
31 case RES_SHELLS:
32 {
34 break;
35 }
36 case RES_BULLETS:
37 {
39 break;
40 }
41 case RES_ROCKETS:
42 {
44 break;
45 }
46 case RES_CELLS:
47 {
49 break;
50 }
51 case RES_FUEL:
52 {
54 break;
55 }
56 default:
57 {
58 error("GetResourceLimit: Invalid resource type.");
59 return 0;
60 }
61 }
62 MUTATOR_CALLHOOK(GetResourceLimit, e, res_type, limit);
63 limit = M_ARGV(9, float);
64 if (limit > RES_AMOUNT_HARD_LIMIT)
65 {
67 }
68 return limit;
69}
70
71float GetResource(entity e, Resource res_type)
72{
73 return e.(GetResourceField(res_type));
74}
75
76bool SetResourceExplicit(entity e, Resource res_type, float amount)
77{
78 .float res_field = GetResourceField(res_type);
79 if (e.(res_field) != amount)
80 {
81 e.(res_field) = amount;
82 return true;
83 }
84 return false;
85}
86
87void SetResource(entity e, Resource res_type, float amount)
88{
89 bool forbid = MUTATOR_CALLHOOK(SetResource, e, res_type, amount);
90 if (forbid)
91 {
92 return;
93 }
94 res_type = M_ARGV(8, entity);
95 amount = M_ARGV(9, float);
96 float max_amount = GetResourceLimit(e, res_type); // TODO: should allow overriding these limits if cheats are enabled!
97 float amount_wasted = 0;
98 if (amount > max_amount && max_amount != RES_LIMIT_NONE)
99 {
100 amount_wasted = amount - max_amount;
101 amount = max_amount;
102 }
103 bool changed = SetResourceExplicit(e, res_type, amount);
104 if (changed)
105 {
106 MUTATOR_CALLHOOK(ResourceAmountChanged, e, res_type, amount);
107 }
108 if (amount_wasted == 0)
109 {
110 return;
111 }
112 MUTATOR_CALLHOOK(ResourceWasted, e, res_type, amount_wasted);
113}
114
115void GiveResource(entity receiver, Resource res_type, float amount)
116{
117 if (amount <= 0)
118 {
119 return;
120 }
121 bool forbid = MUTATOR_CALLHOOK(GiveResource, receiver, res_type, amount);
122 if (forbid)
123 {
124 return;
125 }
126 res_type = M_ARGV(8, entity);
127 amount = M_ARGV(9, float);
128 if (amount <= 0)
129 {
130 return;
131 }
132 SetResource(receiver, res_type, GetResource(receiver, res_type) + amount);
133 // TODO: registry handles
134 switch (res_type)
135 {
136 case RES_HEALTH:
137 {
138 receiver.pauserothealth_finished =
139 max(receiver.pauserothealth_finished, time +
141 return;
142 }
143 case RES_ARMOR:
144 {
145 receiver.pauserotarmor_finished =
146 max(receiver.pauserotarmor_finished, time +
148 return;
149 }
150 case RES_FUEL:
151 {
152 receiver.pauserotfuel_finished = max(receiver.pauserotfuel_finished,
154 return;
155 }
156 }
157}
158
159void GiveResourceWithLimit(entity receiver, Resource res_type, float amount, float limit)
160{
161 if (amount <= 0)
162 {
163 return;
164 }
165 bool forbid = MUTATOR_CALLHOOK(GiveResourceWithLimit, receiver, res_type, amount, limit);
166 if (forbid)
167 {
168 return;
169 }
170 res_type = M_ARGV(8, entity);
171 amount = M_ARGV(9, float);
172 limit = M_ARGV(10, float);
173 if (amount <= 0)
174 {
175 return;
176 }
177 float current_amount = GetResource(receiver, res_type);
178 if (current_amount + amount > limit && limit != RES_LIMIT_NONE)
179 {
180 amount = limit - current_amount;
181 }
182 GiveResource(receiver, res_type, amount);
183}
184
185void TakeResource(entity receiver, Resource res_type, float amount)
186{
187 if (amount <= 0)
188 {
189 return;
190 }
191 bool forbid = MUTATOR_CALLHOOK(TakeResource, receiver, res_type, amount);
192 if (forbid)
193 {
194 return;
195 }
196 res_type = M_ARGV(8, entity);
197 amount = M_ARGV(9, float);
198 if (amount <= 0)
199 {
200 return;
201 }
202 SetResource(receiver, res_type, GetResource(receiver, res_type) - amount);
203}
204
205void TakeResourceWithLimit(entity receiver, Resource res_type, float amount, float limit)
206{
207 if (amount <= 0)
208 {
209 return;
210 }
211 bool forbid = MUTATOR_CALLHOOK(TakeResourceWithLimit, receiver, res_type, amount, limit);
212 if (forbid)
213 {
214 return;
215 }
216 res_type = M_ARGV(8, entity);
217 amount = M_ARGV(9, float);
218 limit = M_ARGV(10, float);
219 if (amount <= 0)
220 {
221 return;
222 }
223 float current_amount = GetResource(receiver, res_type);
224 if (current_amount - amount < -limit)
225 {
226 amount = -limit + current_amount;
227 }
228 TakeResource(receiver, res_type, amount);
229}
230
232{
233 if (wpn.ammo_type == RES_NONE) return 0;
234 switch (wpn)
235 {
236 case WEP_ARC: return WEP_CVAR(WEP_ARC, beam_ammo);
237 case WEP_DEVASTATOR: return WEP_CVAR(WEP_DEVASTATOR, ammo);
238 case WEP_MACHINEGUN: return WEP_CVAR(WEP_MACHINEGUN, sustained_ammo);
239 case WEP_MINE_LAYER: return WEP_CVAR(WEP_MINE_LAYER, ammo);
240 case WEP_SEEKER: return WEP_CVAR(WEP_SEEKER, tag_ammo);
241 default: return WEP_CVAR_PRI(wpn, ammo);
242 }
243}
#define MUTATOR_CALLHOOK(id,...)
Definition base.qh:143
var entity(vector mins, vector maxs,.entity tofield) findbox_tofield_OrFallback
void TakeResourceWithLimit(entity receiver, Resource res_type, float amount, float limit)
Takes an entity some resource but not less than a limit.
void SetResource(entity e, Resource res_type, float amount)
Sets the current amount of resource the given entity will have.
void TakeResource(entity receiver, Resource res_type, float amount)
Takes an entity some resource.
int autocvar_g_pickup_rockets_max
Definition ammo.qh:116
int autocvar_g_pickup_shells_max
Definition ammo.qh:44
int autocvar_g_pickup_cells_max
Definition ammo.qh:152
int autocvar_g_pickup_nails_max
Definition ammo.qh:80
#define M_ARGV(x, type)
Definition events.qh:17
#define IS_PLAYER(s)
Definition player.qh:243
float time
RES_ARMOR
Definition ent_cs.qc:130
float max(float f,...)
#define error
Definition pre.qh:6
float GetResourceField(Resource res_type)
Converts resource type (a RES_* constant) to entity field.
Definition resources.qc:11
const int RES_LIMIT_NONE
Definition resources.qh:60
const int RES_AMOUNT_HARD_LIMIT
Unconditional maximum amount of resources the entity can have.
Definition resources.qh:59
void TakeResourceWithLimit(entity receiver, Resource res_type, float amount, float limit)
Takes an entity some resource but not less than a limit.
void SetResource(entity e, Resource res_type, float amount)
Sets the current amount of resource the given entity will have.
void GiveResource(entity receiver, Resource res_type, float amount)
Gives an entity some resource.
float GetResource(entity e, Resource res_type)
Returns the current amount of resource the given entity has.
void TakeResource(entity receiver, Resource res_type, float amount)
Takes an entity some resource.
float GetResourceLimit(entity e, Resource res_type)
Returns the maximum amount of the given resource.
int GetAmmoConsumption(entity wpn)
Returns ammo consumed per shot by the primary/default fire mode.
bool SetResourceExplicit(entity e, Resource res_type, float amount)
Sets the resource amount of an entity without calling any hooks.
void GiveResourceWithLimit(entity receiver, Resource res_type, float amount, float limit)
Gives an entity some resource but not more than a limit.
Header file that describes the resource system.
float autocvar_g_balance_fuel_limit
int autocvar_g_balance_armor_limit
float autocvar_g_balance_pause_armor_rot
float autocvar_g_balance_pause_health_rot
float autocvar_g_balance_health_limit
float autocvar_g_balance_pause_fuel_rot
float ammo
Definition sv_turrets.qh:43
#define WEP_CVAR_PRI(wep, name)
Definition all.qh:322
#define WEP_CVAR(wep, name)
Definition all.qh:321