]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blobdiff - qcsrc/server/resources.qc
Set consistent rating scales in havocbot_goalrating_enemyplayers calls (no actual...
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / resources.qc
index eede42b2ebe4e2b9c0e0a032062fb9d0a0063efb..4ad66bb98ed2169380e7074ea23147a503e652cf 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)
 {
@@ -68,6 +74,35 @@ float GetResourceAmount(entity e, int 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 max_amount = GetResourceLimit(e, resource_type);
+       float amount_wasted = 0;
+       if (amount > max_amount)
+       {
+               amount_wasted = amount - max_amount;
+               amount = max_amount;
+       }
+       .float resource_field = GetResourceField(resource_type);
+       if (e.(resource_field) != amount)
+       {
+               e.(resource_field) = amount;
+               MUTATOR_CALLHOOK(ResourceAmountChanged, e, resource_type, amount);
+       }
+       if (amount_wasted == 0)
+       {
+               return;
+       }
+       MUTATOR_CALLHOOK(ResourceWasted, e, resource_type, amount_wasted);
+}
+
 void GiveResource(entity receiver, int resource_type, float amount)
 {
        if (amount == 0)
@@ -82,10 +117,12 @@ void GiveResource(entity receiver, int resource_type, float amount)
        }
        resource_type = M_ARGV(1, int);
        amount = M_ARGV(2, float);
-       .float resource_field = GetResourceField(resource_type);
-       float max_amount = GetResourceLimit(receiver, resource_type);
-       receiver.(resource_field) = bound(receiver.(resource_field),
-               receiver.(resource_field) + amount, max_amount);
+       if (amount <= 0)
+       {
+               return;
+       }
+       SetResourceAmount(receiver, resource_type,
+               GetResourceAmount(receiver, resource_type) + amount);
        switch (resource_type)
        {
                case RESOURCE_HEALTH:
@@ -111,6 +148,34 @@ void GiveResource(entity receiver, int resource_type, float amount)
        }
 }
 
+void GiveResourceWithLimit(entity receiver, int resource_type, float amount,
+       float limit)
+{
+       if (amount == 0)
+       {
+               return;
+       }
+       bool forbid = MUTATOR_CALLHOOK(GiveResourceWithLimit, receiver,
+               resource_type, amount, limit);
+       if (forbid)
+       {
+               return;
+       }
+       resource_type = M_ARGV(1, int);
+       amount = M_ARGV(2, float);
+       limit = M_ARGV(3, float);
+       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_field)