]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blobdiff - qcsrc/server/resources.qc
Fix half part of #2191: 'Player is now spectating' isn't shown in teamless games...
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / resources.qc
index 2632af10ff5e6d3cd9d8a971b2ea323e25c1704a..74a0c55788a71a2f19de6accaeaacba47f1c9c3b 100644 (file)
@@ -10,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)
        {
@@ -96,7 +99,7 @@ void SetResourceAmount(entity e, int resource_type, float amount)
        amount = M_ARGV(2, float);
        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)
+       if (amount > max_amount && max_amount != RESOURCE_LIMIT_NONE)
        {
                amount_wasted = amount - max_amount;
                amount = max_amount;
@@ -115,7 +118,7 @@ void SetResourceAmount(entity e, int resource_type, float amount)
 
 void GiveResource(entity receiver, int resource_type, float amount)
 {
-       if (amount == 0)
+       if (amount <= 0)
        {
                return;
        }
@@ -161,7 +164,7 @@ 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;
        }
@@ -174,12 +177,12 @@ void GiveResourceWithLimit(entity receiver, int resource_type, float amount,
        resource_type = M_ARGV(1, int);
        amount = M_ARGV(2, float);
        limit = M_ARGV(3, float);
-       if (amount == 0)
+       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;
        }
@@ -188,7 +191,7 @@ void GiveResourceWithLimit(entity receiver, int resource_type, float amount,
 
 void TakeResource(entity receiver, int resource_type, float amount)
 {
-       if (amount == 0)
+       if (amount <= 0)
        {
                return;
        }
@@ -200,7 +203,7 @@ void TakeResource(entity receiver, int resource_type, float amount)
        }
        resource_type = M_ARGV(1, int);
        amount = M_ARGV(2, float);
-       if (amount == 0)
+       if (amount <= 0)
        {
                return;
        }
@@ -211,7 +214,7 @@ void TakeResource(entity receiver, int resource_type, float amount)
 void TakeResourceWithLimit(entity receiver, int resource_type, float amount,
        float limit)
 {
-       if (amount == 0)
+       if (amount <= 0)
        {
                return;
        }
@@ -224,18 +227,43 @@ void TakeResourceWithLimit(entity receiver, int resource_type, float amount,
        resource_type = M_ARGV(1, int);
        amount = M_ARGV(2, float);
        limit = M_ARGV(3, float);
-       if (amount == 0)
+       if (amount <= 0)
        {
                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)