]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blobdiff - qcsrc/server/resources.qc
Merge branch 'Mario/target_teleporter_v2' into 'master'
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / resources.qc
index a6e0e07245895fa9df80b8386dce80f395762985..a2a1358b97856196366b7dc95c909c620f2152ec 100644 (file)
@@ -1,6 +1,12 @@
 #include "resources.qh"
 
-//#include <server/autocvars.qh>
+/// \file
+/// \brief Source file that contains implementation of the resource system.
+/// \author Lyberta
+/// \copyright GNU GPLv2 or any later version.
+
+#include "autocvars.qh"
+#include "miscfunctions.qh"
 
 float GetResourceLimit(entity e, int resource_type)
 {
@@ -62,6 +68,34 @@ float GetResourceLimit(entity e, int resource_type)
        return limit;
 }
 
+float GetResourceAmount(entity e, int resource_type)
+{
+       .float resource_field = GetResourceField(resource_type);
+       return e.(resource_field);
+}
+
+void SetResourceAmount(entity e, int resource_type, float amount)
+{
+       bool forbid = MUTATOR_CALLHOOK(SetResourceAmount, e, resource_type, amount);
+       if (forbid)
+       {
+               return;
+       }
+       resource_type = M_ARGV(1, int);
+       amount = M_ARGV(2, float);
+       .float resource_field = GetResourceField(resource_type);
+       if (e.(resource_field) == amount)
+       {
+               return;
+       }
+       float max_amount = GetResourceLimit(e, resource_type);
+       if (amount > max_amount)
+       {
+               amount = max_amount;
+       }
+       e.(resource_field) = amount;
+}
+
 void GiveResource(entity receiver, int resource_type, float amount)
 {
        if (amount == 0)
@@ -76,10 +110,12 @@ void GiveResource(entity receiver, int resource_type, float amount)
        }
        resource_type = M_ARGV(1, int);
        amount = M_ARGV(2, float);
-       .float resource_property = GetResourceProperty(resource_type);
-       float max_amount = GetResourceLimit(receiver, resource_type);
-       receiver.(resource_property) = bound(receiver.(resource_property),
-               receiver.(resource_property) + amount, max_amount);
+       if (amount <= 0)
+       {
+               return;
+       }
+       SetResourceAmount(receiver, resource_type,
+               GetResourceAmount(receiver, resource_type) + amount);
        switch (resource_type)
        {
                case RESOURCE_HEALTH:
@@ -105,9 +141,24 @@ void GiveResource(entity receiver, int resource_type, float amount)
        }
 }
 
-int GetResourceType(.float resource_property)
+void GiveResourceWithLimit(entity receiver, int resource_type, float amount,
+       float limit)
+{
+       if (amount == 0)
+       {
+               return;
+       }
+       float current_amount = GetResourceAmount(receiver, resource_type);
+       if (current_amount + amount > limit)
+       {
+               amount = limit - current_amount;
+       }
+       GiveResource(receiver, resource_type, amount);
+}
+
+int GetResourceType(.float resource_field)
 {
-       switch (resource_property)
+       switch (resource_field)
        {
                case health: { return RESOURCE_HEALTH; }
                case armorvalue: { return RESOURCE_ARMOR; }
@@ -118,11 +169,11 @@ int GetResourceType(.float resource_property)
                case ammo_plasma: { return RESOURCE_PLASMA; }
                case ammo_fuel: { return RESOURCE_FUEL; }
        }
-       error("GetResourceType: Invalid property.");
+       error("GetResourceType: Invalid field.");
        return 0;
 }
 
-.float GetResourceProperty(int resource_type)
+.float GetResourceField(int resource_type)
 {
        switch (resource_type)
        {
@@ -135,6 +186,6 @@ int GetResourceType(.float resource_property)
                case RESOURCE_PLASMA: { return ammo_plasma; }
                case RESOURCE_FUEL: { return ammo_fuel; }
        }
-       error("GetResourceProperty: Invalid resource type.");
+       error("GetResourceField: Invalid resource type.");
        return health;
 }