From: Lyberta Date: Wed, 30 Aug 2017 04:34:31 +0000 (+0300) Subject: URS: Added GiveResourceWithLimit. X-Git-Tag: xonotic-v0.8.5~2488^2~2 X-Git-Url: https://git.rm.cloudns.org/?a=commitdiff_plain;h=5ead9de9d49ca70c28dcf9cd931ba0922559c23b;p=xonotic%2Fxonotic-data.pk3dir.git URS: Added GiveResourceWithLimit. --- 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.