]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/resources.qc
URS: Renamed GetResourceProperty to GetResourceField, added GetResourceAmount.
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / resources.qc
1 #include "resources.qh"
2
3 //#include <server/autocvars.qh>
4
5 float GetResourceLimit(entity e, int resource_type)
6 {
7         float limit;
8         switch (resource_type)
9         {
10                 case RESOURCE_HEALTH:
11                 {
12                         limit = autocvar_g_balance_health_limit;
13                         break;
14                 }
15                 case RESOURCE_ARMOR:
16                 {
17                         limit = autocvar_g_balance_armor_limit;
18                         break;
19                 }
20                 case RESOURCE_SHELLS:
21                 {
22                         limit = g_pickup_shells_max;
23                         break;
24                 }
25                 case RESOURCE_BULLETS:
26                 {
27                         limit = g_pickup_nails_max;
28                         break;
29                 }
30                 case RESOURCE_ROCKETS:
31                 {
32                         limit = g_pickup_rockets_max;
33                         break;
34                 }
35                 case RESOURCE_CELLS:
36                 {
37                         limit = g_pickup_cells_max;
38                         break;
39                 }
40                 case RESOURCE_PLASMA:
41                 {
42                         limit = g_pickup_plasma_max;
43                         break;
44                 }
45                 case RESOURCE_FUEL:
46                 {
47                         limit = autocvar_g_balance_fuel_limit;
48                         break;
49                 }
50                 default:
51                 {
52                         error("GetResourceLimit: Invalid resource type.");
53                         return 0;
54                 }
55         }
56         MUTATOR_CALLHOOK(GetResourceLimit, e, resource_type, limit);
57         limit = M_ARGV(2, float);
58         if (limit > RESOURCE_AMOUNT_HARD_LIMIT)
59         {
60                 limit = RESOURCE_AMOUNT_HARD_LIMIT;
61         }
62         return limit;
63 }
64
65 float GetResourceAmount(entity e, int resource_type)
66 {
67         .float resource_field = GetResourceField(resource_type);
68         return e.(resource_field);
69 }
70
71 void GiveResource(entity receiver, int resource_type, float amount)
72 {
73         if (amount == 0)
74         {
75                 return;
76         }
77         bool forbid = MUTATOR_CALLHOOK(GiveResource, receiver, resource_type,
78                 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         float max_amount = GetResourceLimit(receiver, resource_type);
87         receiver.(resource_field) = bound(receiver.(resource_field),
88                 receiver.(resource_field) + amount, max_amount);
89         switch (resource_type)
90         {
91                 case RESOURCE_HEALTH:
92                 {
93                         receiver.pauserothealth_finished =
94                                 max(receiver.pauserothealth_finished, time +
95                                 autocvar_g_balance_pause_health_rot);
96                         return;
97                 }
98                 case RESOURCE_ARMOR:
99                 {
100                         receiver.pauserotarmor_finished =
101                                 max(receiver.pauserotarmor_finished, time +
102                                 autocvar_g_balance_pause_armor_rot);
103                         return;
104                 }
105                 case RESOURCE_FUEL:
106                 {
107                         receiver.pauserotfuel_finished = max(receiver.pauserotfuel_finished,
108                                 time + autocvar_g_balance_pause_fuel_rot);
109                         return;
110                 }
111         }
112 }
113
114 int GetResourceType(.float resource_field)
115 {
116         switch (resource_field)
117         {
118                 case health: { return RESOURCE_HEALTH; }
119                 case armorvalue: { return RESOURCE_ARMOR; }
120                 case ammo_shells: { return RESOURCE_SHELLS; }
121                 case ammo_nails: { return RESOURCE_BULLETS; }
122                 case ammo_rockets: { return RESOURCE_ROCKETS; }
123                 case ammo_cells: { return RESOURCE_CELLS; }
124                 case ammo_plasma: { return RESOURCE_PLASMA; }
125                 case ammo_fuel: { return RESOURCE_FUEL; }
126         }
127         error("GetResourceType: Invalid field.");
128         return 0;
129 }
130
131 .float GetResourceField(int resource_type)
132 {
133         switch (resource_type)
134         {
135                 case RESOURCE_HEALTH: { return health; }
136                 case RESOURCE_ARMOR: { return armorvalue; }
137                 case RESOURCE_SHELLS: { return ammo_shells; }
138                 case RESOURCE_BULLETS: { return ammo_nails; }
139                 case RESOURCE_ROCKETS: { return ammo_rockets; }
140                 case RESOURCE_CELLS: { return ammo_cells; }
141                 case RESOURCE_PLASMA: { return ammo_plasma; }
142                 case RESOURCE_FUEL: { return ammo_fuel; }
143         }
144         error("GetResourceField: Invalid resource type.");
145         return health;
146 }