/// \author Lyberta
/// \copyright GNU GPLv2 or any later version.
-#include "autocvars.qh"
-#include "miscfunctions.qh"
+#include <server/mutators/_mod.qh>
+#include <server/world.qh>
float GetResourceLimit(entity e, int res_type)
{
return limit;
}
-float GetResourceAmount(entity e, int res_type)
+float GetResource(entity e, int res_type)
{
return e.(GetResourceField(res_type));
}
-void SetResourceAmount(entity e, int res_type, float amount)
+bool SetResourceExplicit(entity e, int res_type, float amount)
{
- bool forbid = MUTATOR_CALLHOOK(SetResourceAmount, e, res_type, amount);
+ .float res_field = GetResourceField(res_type);
+ if (e.(res_field) != amount)
+ {
+ e.(res_field) = amount;
+ return true;
+ }
+ return false;
+}
+
+void SetResource(entity e, int res_type, float amount)
+{
+ bool forbid = MUTATOR_CALLHOOK(SetResource, e, res_type, amount);
if (forbid)
{
return;
amount_wasted = amount - max_amount;
amount = max_amount;
}
- .float res_field = GetResourceField(res_type);
- if (e.(res_field) != amount)
+ bool changed = SetResourceExplicit(e, res_type, amount);
+ if (changed)
{
- e.(res_field) = amount;
MUTATOR_CALLHOOK(ResourceAmountChanged, e, res_type, amount);
}
if (amount_wasted == 0)
{
return;
}
- SetResourceAmount(receiver, res_type, GetResourceAmount(receiver, res_type) + amount);
+ SetResource(receiver, res_type, GetResource(receiver, res_type) + amount);
switch (res_type)
{
case RES_HEALTH:
{
return;
}
- float current_amount = GetResourceAmount(receiver, res_type);
+ float current_amount = GetResource(receiver, res_type);
if (current_amount + amount > limit && limit != RES_LIMIT_NONE)
{
amount = limit - current_amount;
{
return;
}
- SetResourceAmount(receiver, res_type, GetResourceAmount(receiver, res_type) - amount);
+ SetResource(receiver, res_type, GetResource(receiver, res_type) - amount);
}
void TakeResourceWithLimit(entity receiver, int res_type, float amount, float limit)
{
return;
}
- float current_amount = GetResourceAmount(receiver, res_type);
+ float current_amount = GetResource(receiver, res_type);
if (current_amount - amount < -limit)
{
amount = -limit + current_amount;
TakeResource(receiver, res_type, amount);
}
-void GiveOrTakeResource(entity receiver, int res_type, float amount)
-{
- if(amount < 0)
- {
- TakeResource(receiver, res_type, amount * -1);
- }
- else
- {
- GiveResource(receiver, res_type, amount);
- }
-}
-
-void GiveOrTakeResourceWithLimit(entity receiver, int res_type, float amount, float limit)
-{
- if(amount < 0)
- {
- TakeResourceWithLimit(receiver, res_type, amount * -1, limit);
- }
- else
- {
- GiveResourceWithLimit(receiver, res_type, amount, limit);
- }
-}
-
int GetResourceType(.float res_field)
{
switch (res_field)