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 )
{
{
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;
}
}
}
+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)
/// \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.