]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/commitdiff
URS: Added GiveResourceWithLimit.
authorLyberta <lyberta@lyberta.net>
Wed, 30 Aug 2017 04:34:31 +0000 (07:34 +0300)
committerLyberta <lyberta@lyberta.net>
Wed, 30 Aug 2017 04:34:31 +0000 (07:34 +0300)
qcsrc/common/mutators/mutator/nades/nades.qc
qcsrc/common/t_items.qc
qcsrc/server/resources.qc
qcsrc/server/resources.qh

index d667c0cbf42a80a85b36498812bc2d048618914b..0c446952d033a60d14a52f83c4c2784cb1eaaf00 100644 (file)
@@ -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 )
                {
index 9ea5a6543bdc2ab99432dd587f67806024671d2a..4fc2e38219c144e2a5801129419847e2444c4808 100644 (file)
@@ -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;
 }
 
index 6526e2f11d262f80dd888a66d6bfbad60a904d60..c5bbe75e1e4382d259da687c53946b2e445eb530 100644 (file)
@@ -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)
index 97f49485d90d99bb01cbb1fc6d05b772f0caeed1..522ff5b68d5ad4dd7ed52825689f328c9825da72 100644 (file)
@@ -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.