]> git.rm.cloudns.org Git - xonotic/xonotic-data.pk3dir.git/commitdiff
URS: Added SetResourceAmount.
authorLyberta <lyberta@lyberta.net>
Mon, 28 Aug 2017 19:58:34 +0000 (22:58 +0300)
committerLyberta <lyberta@lyberta.net>
Mon, 28 Aug 2017 19:58:34 +0000 (22:58 +0300)
qcsrc/server/resources.qc
qcsrc/server/resources.qh

index eede42b2ebe4e2b9c0e0a032062fb9d0a0063efb..49871e9b565ee45f920ae5f601edc6be6bddeae2 100644 (file)
@@ -68,6 +68,21 @@ float GetResourceAmount(entity e, int resource_type)
        return e.(resource_field);
 }
 
+void SetResourceAmount(entity e, int resource_type, float amount)
+{
+       .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)
@@ -82,10 +97,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:
index d81b6ac6adc9ee5d6a5663c869b1d1458548aa45..8bf3d8ee0228ecf5cbd8b46e3e58ef6d8b45d5cd 100644 (file)
@@ -30,6 +30,13 @@ float GetResourceLimit(entity e, int resource_type);
 /// \return Current amount of resource the given entity has.
 float GetResourceAmount(entity e, int resource_type);
 
+/// \brief Sets the current amount of resource the given entity will have.
+/// \param[in,out] e Entity to adjust.
+/// \param[in] resource_type Type of the resource (a RESOURCE_* constant).
+/// \param[in] amount Amount of resource to set.
+/// \return No return.
+void SetResourceAmount(entity e, int resource_type, float amount);
+
 /// \brief Gives an entity some resource.
 /// \param[in,out] receiver Entity to give resource to.
 /// \param[in] resource_type Type of the resource (a RESOURCE_* constant).