]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/client/resources.qc
Merge branch 'master' into martin-t/dmgtext
[xonotic/xonotic-data.pk3dir.git] / qcsrc / client / resources.qc
1 #include "resources.qh"
2 #include <common/items/item/ammo.qh>
3
4 /// \file
5 /// \brief Source file that contains implementation of the resource system.
6 /// \copyright GNU GPLv2 or any later version.
7
8 float GetResourceAmount(entity e, int resource_type)
9 {
10         .float resource_field = GetResourceField(resource_type);
11         return e.(resource_field);
12 }
13
14 bool SetResourceAmountExplicit(entity e, int resource_type, float amount)
15 {
16         .float resource_field = GetResourceField(resource_type);
17         if (e.(resource_field) != amount)
18         {
19                 e.(resource_field) = amount;
20                 return true;
21         }
22         return false;
23 }
24
25 void SetResourceAmount(entity e, int resource_type, float amount)
26 {
27         SetResourceAmountExplicit(e, resource_type, amount);
28 }
29
30 void TakeResource(entity receiver, int resource_type, float amount)
31 {
32         if (amount == 0)
33         {
34                 return;
35         }
36         SetResourceAmount(receiver, resource_type,
37                 GetResourceAmount(receiver, resource_type) - amount);
38 }
39
40 void TakeResourceWithLimit(entity receiver, int resource_type, float amount,
41         float limit)
42 {
43         if (amount == 0)
44         {
45                 return;
46         }
47         float current_amount = GetResourceAmount(receiver, resource_type);
48         if (current_amount - amount < limit)
49         {
50                 amount = limit + current_amount;
51         }
52         TakeResource(receiver, resource_type, amount);
53 }
54
55 int GetResourceType(.float resource_field)
56 {
57         switch (resource_field)
58         {
59                 case health: { return RESOURCE_HEALTH; }
60                 case armorvalue: { return RESOURCE_ARMOR; }
61                 case ammo_shells: { return RESOURCE_SHELLS; }
62                 case ammo_nails: { return RESOURCE_BULLETS; }
63                 case ammo_rockets: { return RESOURCE_ROCKETS; }
64                 case ammo_cells: { return RESOURCE_CELLS; }
65                 case ammo_plasma: { return RESOURCE_PLASMA; }
66                 case ammo_fuel: { return RESOURCE_FUEL; }
67         }
68         error("GetResourceType: Invalid field.");
69         return 0;
70 }
71
72 .float GetResourceField(int resource_type)
73 {
74         switch (resource_type)
75         {
76                 case RESOURCE_HEALTH: { return health; }
77                 case RESOURCE_ARMOR: { return armorvalue; }
78                 case RESOURCE_SHELLS: { return ammo_shells; }
79                 case RESOURCE_BULLETS: { return ammo_nails; }
80                 case RESOURCE_ROCKETS: { return ammo_rockets; }
81                 case RESOURCE_CELLS: { return ammo_cells; }
82                 case RESOURCE_PLASMA: { return ammo_plasma; }
83                 case RESOURCE_FUEL: { return ammo_fuel; }
84         }
85         error("GetResourceField: Invalid resource type.");
86         return health;
87 }