From: Mario Date: Wed, 20 Jun 2018 09:51:02 +0000 (+1000) Subject: Fix not being able to set a health item to take health X-Git-Tag: xonotic-v0.8.5~1996 X-Git-Url: https://git.rm.cloudns.org/?a=commitdiff_plain;h=8a22260395360c683c34abecd91562267375203e;p=xonotic%2Fxonotic-data.pk3dir.git Fix not being able to set a health item to take health --- diff --git a/qcsrc/common/t_items.qc b/qcsrc/common/t_items.qc index 68fa7ef57..140b619c5 100644 --- a/qcsrc/common/t_items.qc +++ b/qcsrc/common/t_items.qc @@ -742,14 +742,14 @@ float Item_GiveAmmoTo(entity item, entity player, int resource_type, float ammom { return false; } - GiveResourceWithLimit(player, resource_type, amount, ammomax); + GiveOrTakeResourceWithLimit(player, resource_type, amount, ammomax); return true; } if (g_weapon_stay != 2) { return false; } - GiveResourceWithLimit(player, resource_type, amount, min(amount, ammomax)); + GiveOrTakeResourceWithLimit(player, resource_type, amount, min(amount, ammomax)); return true; } diff --git a/qcsrc/server/resources.qc b/qcsrc/server/resources.qc index b3b19095a..74a0c5578 100644 --- a/qcsrc/server/resources.qc +++ b/qcsrc/server/resources.qc @@ -232,13 +232,38 @@ void TakeResourceWithLimit(entity receiver, int resource_type, float amount, return; } float current_amount = GetResourceAmount(receiver, resource_type); - if (current_amount - amount < limit) + if (current_amount - amount < -limit) { - amount = limit + current_amount; + amount = -limit + current_amount; } TakeResource(receiver, resource_type, amount); } +void GiveOrTakeResource(entity receiver, int resource_type, float amount) +{ + if(amount < 0) + { + TakeResource(receiver, resource_type, amount * -1); + } + else + { + GiveResource(receiver, resource_type, amount); + } +} + +void GiveOrTakeResourceWithLimit(entity receiver, int resource_type, float amount, + float limit) +{ + if(amount < 0) + { + TakeResourceWithLimit(receiver, resource_type, amount * -1, limit); + } + else + { + GiveResourceWithLimit(receiver, resource_type, amount, limit); + } +} + int GetResourceType(.float resource_field) { switch (resource_field) diff --git a/qcsrc/server/resources.qh b/qcsrc/server/resources.qh index e4631ab56..15433b264 100644 --- a/qcsrc/server/resources.qh +++ b/qcsrc/server/resources.qh @@ -67,6 +67,22 @@ void TakeResource(entity receiver, int resource_type, float amount); void TakeResourceWithLimit(entity receiver, int resource_type, float amount, float limit); +/// \brief Gives to or takes from an entity resource. +/// \param[in,out] receiver Entity to give or take resource. +/// \param[in] resource_type Type of the resource (a RESOURCE_* constant). +/// \param[in] amount Amount of resource to give or take. +/// \return No return. +void GiveOrTakeResource(entity receiver, int resource_type, float amount); + +/// \brief Gives to or takes from an entity resource but not more/less than a limit. +/// \param[in,out] receiver Entity to give or take resource. +/// \param[in] resource_type Type of the resource (a RESOURCE_* constant). +/// \param[in] amount Amount of resource to give or take. +/// \param[in] limit Limit of resources to give or take. +/// \return No return. +void GiveOrTakeResourceWithLimit(entity receiver, int resource_type, float amount, + float limit); + // ===================== Legacy and/or internal API =========================== /// \brief Converts an entity field to resource type.