]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/resources.qc
URS: Added GiveResourceWithLimit.
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / resources.qc
1 /// \file
2 /// \brief Source file that contains implementation of the resource system.
3 /// \author Lyberta
4 /// \copyright GNU GPLv3 or any later version.
5
6 #include "resources.qh"
7
8 #include "autocvars.qh"
9 #include "miscfunctions.qh"
10
11 float GetResourceLimit(entity e, int resource_type)
12 {
13         float limit;
14         switch (resource_type)
15         {
16                 case RESOURCE_HEALTH:
17                 {
18                         limit = autocvar_g_balance_health_limit;
19                         break;
20                 }
21                 case RESOURCE_ARMOR:
22                 {
23                         limit = autocvar_g_balance_armor_limit;
24                         break;
25                 }
26                 case RESOURCE_SHELLS:
27                 {
28                         limit = g_pickup_shells_max;
29                         break;
30                 }
31                 case RESOURCE_BULLETS:
32                 {
33                         limit = g_pickup_nails_max;
34                         break;
35                 }
36                 case RESOURCE_ROCKETS:
37                 {
38                         limit = g_pickup_rockets_max;
39                         break;
40                 }
41                 case RESOURCE_CELLS:
42                 {
43                         limit = g_pickup_cells_max;
44                         break;
45                 }
46                 case RESOURCE_PLASMA:
47                 {
48                         limit = g_pickup_plasma_max;
49                         break;
50                 }
51                 case RESOURCE_FUEL:
52                 {
53                         limit = autocvar_g_balance_fuel_limit;
54                         break;
55                 }
56                 default:
57                 {
58                         error("GetResourceLimit: Invalid resource type.");
59                         return 0;
60                 }
61         }
62         MUTATOR_CALLHOOK(GetResourceLimit, e, resource_type, limit);
63         limit = M_ARGV(2, float);
64         if (limit > RESOURCE_AMOUNT_HARD_LIMIT)
65         {
66                 limit = RESOURCE_AMOUNT_HARD_LIMIT;
67         }
68         return limit;
69 }
70
71 float GetResourceAmount(entity e, int resource_type)
72 {
73         .float resource_field = GetResourceField(resource_type);
74         return e.(resource_field);
75 }
76
77 void SetResourceAmount(entity e, int resource_type, float amount)
78 {
79         bool forbid = MUTATOR_CALLHOOK(SetResourceAmount, e, resource_type, amount);
80         if (forbid)
81         {
82                 return;
83         }
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)
88         {
89                 return;
90         }
91         float max_amount = GetResourceLimit(e, resource_type);
92         if (amount > max_amount)
93         {
94                 amount = max_amount;
95         }
96         e.(resource_field) = amount;
97 }
98
99 void GiveResource(entity receiver, int resource_type, float amount)
100 {
101         if (amount == 0)
102         {
103                 return;
104         }
105         bool forbid = MUTATOR_CALLHOOK(GiveResource, receiver, resource_type,
106                 amount);
107         if (forbid)
108         {
109                 return;
110         }
111         resource_type = M_ARGV(1, int);
112         amount = M_ARGV(2, float);
113         if (amount <= 0)
114         {
115                 return;
116         }
117         SetResourceAmount(receiver, resource_type,
118                 GetResourceAmount(receiver, resource_type) + amount);
119         switch (resource_type)
120         {
121                 case RESOURCE_HEALTH:
122                 {
123                         receiver.pauserothealth_finished =
124                                 max(receiver.pauserothealth_finished, time +
125                                 autocvar_g_balance_pause_health_rot);
126                         return;
127                 }
128                 case RESOURCE_ARMOR:
129                 {
130                         receiver.pauserotarmor_finished =
131                                 max(receiver.pauserotarmor_finished, time +
132                                 autocvar_g_balance_pause_armor_rot);
133                         return;
134                 }
135                 case RESOURCE_FUEL:
136                 {
137                         receiver.pauserotfuel_finished = max(receiver.pauserotfuel_finished,
138                                 time + autocvar_g_balance_pause_fuel_rot);
139                         return;
140                 }
141         }
142 }
143
144 void GiveResourceWithLimit(entity receiver, int resource_type, float amount,
145         float limit)
146 {
147         if (amount == 0)
148         {
149                 return;
150         }
151         float current_amount = GetResourceAmount(receiver, resource_type);
152         if (current_amount + amount > limit)
153         {
154                 amount = limit - current_amount;
155         }
156         GiveResource(receiver, resource_type, amount);
157 }
158
159 int GetResourceType(.float resource_field)
160 {
161         switch (resource_field)
162         {
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; }
171         }
172         error("GetResourceType: Invalid field.");
173         return 0;
174 }
175
176 .float GetResourceField(int resource_type)
177 {
178         switch (resource_type)
179         {
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; }
188         }
189         error("GetResourceField: Invalid resource type.");
190         return health;
191 }