From 468aaf9eca66c143de9f47af866c899bf93068d1 Mon Sep 17 00:00:00 2001 From: Lyberta Date: Mon, 28 Aug 2017 22:58:34 +0300 Subject: [PATCH] URS: Added SetResourceAmount. --- qcsrc/server/resources.qc | 25 +++++++++++++++++++++---- qcsrc/server/resources.qh | 7 +++++++ 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/qcsrc/server/resources.qc b/qcsrc/server/resources.qc index eede42b2e..49871e9b5 100644 --- a/qcsrc/server/resources.qc +++ b/qcsrc/server/resources.qc @@ -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: diff --git a/qcsrc/server/resources.qh b/qcsrc/server/resources.qh index d81b6ac6a..8bf3d8ee0 100644 --- a/qcsrc/server/resources.qh +++ b/qcsrc/server/resources.qh @@ -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). -- 2.39.2