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