From 5ead9de9d49ca70c28dcf9cd931ba0922559c23b Mon Sep 17 00:00:00 2001 From: Lyberta Date: Wed, 30 Aug 2017 07:34:31 +0300 Subject: [PATCH] URS: Added GiveResourceWithLimit. --- qcsrc/common/mutators/mutator/nades/nades.qc | 7 ++++--- qcsrc/common/t_items.qc | 12 ++---------- qcsrc/server/resources.qc | 15 +++++++++++++++ qcsrc/server/resources.qh | 9 +++++++++ 4 files changed, 30 insertions(+), 13 deletions(-) diff --git a/qcsrc/common/mutators/mutator/nades/nades.qc b/qcsrc/common/mutators/mutator/nades/nades.qc index d667c0cbf..0c446952d 100644 --- a/qcsrc/common/mutators/mutator/nades/nades.qc +++ b/qcsrc/common/mutators/mutator/nades/nades.qc @@ -626,11 +626,12 @@ void nade_heal_touch(entity this, entity toucher) float hp = GetResourceAmount(toucher, RESOURCE_HEALTH); if (hp < maxhealth) { - if ( this.nade_show_particles ) + if (this.nade_show_particles) + { Send_Effect(EFFECT_HEALING, toucher.origin, '0 0 0', 1); - SetResourceAmount(toucher, RESOURCE_HEALTH, min(hp + health_factor, maxhealth)); + } + GiveResourceWithLimit(toucher, RESOURCE_HEALTH, health_factor, maxhealth); } - toucher.pauserothealth_finished = max(toucher.pauserothealth_finished, time + autocvar_g_balance_pause_health_rot); } else if ( health_factor < 0 ) { diff --git a/qcsrc/common/t_items.qc b/qcsrc/common/t_items.qc index 9ea5a6543..4fc2e3821 100644 --- a/qcsrc/common/t_items.qc +++ b/qcsrc/common/t_items.qc @@ -696,22 +696,14 @@ float Item_GiveAmmoTo(entity item, entity player, int resource_type, float ammom { return false; } - if ((player_amount + amount) > ammomax) - { - amount = ammomax - player_amount; - } - GiveResource(player, resource_type, amount); + GiveResourceWithLimit(player, resource_type, amount, ammomax); return true; } if (g_weapon_stay != 2) { return false; } - float mi = min(amount, ammomax); - if (player_amount < mi) - { - GiveResource(player, resource_type, mi - player_amount); - } + GiveResourceWithLimit(player, resource_type, amount, min(amount, ammomax)); return true; } diff --git a/qcsrc/server/resources.qc b/qcsrc/server/resources.qc index 6526e2f11..c5bbe75e1 100644 --- a/qcsrc/server/resources.qc +++ b/qcsrc/server/resources.qc @@ -141,6 +141,21 @@ void GiveResource(entity receiver, int resource_type, float amount) } } +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_field) diff --git a/qcsrc/server/resources.qh b/qcsrc/server/resources.qh index 97f49485d..522ff5b68 100644 --- a/qcsrc/server/resources.qh +++ b/qcsrc/server/resources.qh @@ -49,6 +49,15 @@ void SetResourceAmount(entity e, int resource_type, float amount); /// \return No return. void GiveResource(entity receiver, int resource_type, float amount); +/// \brief Gives an entity some resource but not more than a limit. +/// \param[in,out] receiver Entity to give resource to. +/// \param[in] resource_type Type of the resource (a RESOURCE_* constant). +/// \param[in] amount Amount of resource to give. +/// \param[in] limit Limit of resources to give. +/// \return No return. +void GiveResourceWithLimit(entity receiver, int resource_type, float amount, + float limit); + // ===================== Legacy and/or internal API =========================== /// \brief Converts an entity field to resource type. -- 2.39.2