X-Git-Url: http://de.git.xonotic.org/?p=xonotic%2Fxonotic-data.pk3dir.git;a=blobdiff_plain;f=qcsrc%2Fserver%2Fresources.qc;h=b3b19095ae8b14e7832b386d1f5614c891a3d93f;hp=edf4ff16238ee591537546037eba9eff8fe14750;hb=50d6cb6a02a959a7303b5b687631b664a807b26f;hpb=3cd932a2030bbb989c54d3def6c19f6204c2dad0 diff --git a/qcsrc/server/resources.qc b/qcsrc/server/resources.qc index edf4ff162..b3b19095a 100644 --- a/qcsrc/server/resources.qc +++ b/qcsrc/server/resources.qc @@ -1,4 +1,5 @@ #include "resources.qh" + /// \file /// \brief Source file that contains implementation of the resource system. /// \author Lyberta @@ -9,6 +10,9 @@ float GetResourceLimit(entity e, int resource_type) { + if(!IS_PLAYER(e)) + return RESOURCE_LIMIT_NONE; // no limits on non-players + float limit; switch (resource_type) { @@ -73,6 +77,17 @@ float GetResourceAmount(entity e, int resource_type) return e.(resource_field); } +bool SetResourceAmountExplicit(entity e, int resource_type, float amount) +{ + .float resource_field = GetResourceField(resource_type); + if (e.(resource_field) != amount) + { + e.(resource_field) = amount; + return true; + } + return false; +} + void SetResourceAmount(entity e, int resource_type, float amount) { bool forbid = MUTATOR_CALLHOOK(SetResourceAmount, e, resource_type, amount); @@ -82,22 +97,28 @@ void SetResourceAmount(entity e, int resource_type, float amount) } resource_type = M_ARGV(1, int); amount = M_ARGV(2, float); - .float resource_field = GetResourceField(resource_type); - if (e.(resource_field) == amount) + float max_amount = GetResourceLimit(e, resource_type); // TODO: should allow overriding these limits if cheats are enabled! + float amount_wasted = 0; + if (amount > max_amount && max_amount != RESOURCE_LIMIT_NONE) { - return; + amount_wasted = amount - max_amount; + amount = max_amount; } - float max_amount = GetResourceLimit(e, resource_type); - if (amount > max_amount) + bool changed = SetResourceAmountExplicit(e, resource_type, amount); + if (changed) { - amount = max_amount; + MUTATOR_CALLHOOK(ResourceAmountChanged, e, resource_type, amount); + } + if (amount_wasted == 0) + { + return; } - e.(resource_field) = amount; + MUTATOR_CALLHOOK(ResourceWasted, e, resource_type, amount_wasted); } void GiveResource(entity receiver, int resource_type, float amount) { - if (amount == 0) + if (amount <= 0) { return; } @@ -143,18 +164,81 @@ void GiveResource(entity receiver, int resource_type, float amount) void GiveResourceWithLimit(entity receiver, int resource_type, float amount, float limit) { - if (amount == 0) + if (amount <= 0) + { + return; + } + bool forbid = MUTATOR_CALLHOOK(GiveResourceWithLimit, receiver, + resource_type, amount, limit); + if (forbid) + { + return; + } + resource_type = M_ARGV(1, int); + amount = M_ARGV(2, float); + limit = M_ARGV(3, float); + if (amount <= 0) { return; } float current_amount = GetResourceAmount(receiver, resource_type); - if (current_amount + amount > limit) + if (current_amount + amount > limit && limit != RESOURCE_LIMIT_NONE) { amount = limit - current_amount; } GiveResource(receiver, resource_type, amount); } +void TakeResource(entity receiver, int resource_type, float amount) +{ + if (amount <= 0) + { + return; + } + bool forbid = MUTATOR_CALLHOOK(TakeResource, receiver, resource_type, + amount); + if (forbid) + { + return; + } + resource_type = M_ARGV(1, int); + amount = M_ARGV(2, float); + if (amount <= 0) + { + return; + } + SetResourceAmount(receiver, resource_type, + GetResourceAmount(receiver, resource_type) - amount); +} + +void TakeResourceWithLimit(entity receiver, int resource_type, float amount, + float limit) +{ + if (amount <= 0) + { + return; + } + bool forbid = MUTATOR_CALLHOOK(TakeResourceWithLimit, receiver, + resource_type, amount, limit); + if (forbid) + { + return; + } + resource_type = M_ARGV(1, int); + amount = M_ARGV(2, float); + limit = M_ARGV(3, float); + if (amount <= 0) + { + return; + } + float current_amount = GetResourceAmount(receiver, resource_type); + if (current_amount - amount < limit) + { + amount = limit + current_amount; + } + TakeResource(receiver, resource_type, amount); +} + int GetResourceType(.float resource_field) { switch (resource_field)