]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blobdiff - qcsrc/server/bot/default/havocbot/roles.qc
Clear out most references to .health and .armorvalue on the server side
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / bot / default / havocbot / roles.qc
index 80a1d237aec8ca403712e052cfe74c9d5b7d0895..9ca2b1208e91a2e828b13ba4ce1646111ac1fc22 100644 (file)
@@ -2,6 +2,8 @@
 
 #include <server/defs.qh>
 #include <server/miscfunctions.qh>
+#include <server/items.qh>
+#include <server/resources.qh>
 #include "havocbot.qh"
 
 #include "../cvars.qh"
@@ -9,6 +11,8 @@
 #include "../bot.qh"
 #include "../navigation.qh"
 
+.float bot_ratingscale;
+.float bot_ratingscale_time;
 .float max_armorvalue;
 .float havocbot_role_timeout;
 
@@ -44,53 +48,63 @@ void havocbot_goalrating_waypoints(entity this, float ratingscale, vector org, f
        }
 };
 
+bool havocbot_goalrating_item_can_be_left_to_teammate(entity this, entity player, entity item)
+{
+       if (GetResourceAmount(item, RESOURCE_HEALTH) && GetResourceAmount(player, RESOURCE_HEALTH) <= GetResourceAmount(this, RESOURCE_HEALTH)) {return true;}
+       if (GetResourceAmount(item, RESOURCE_ARMOR) && GetResourceAmount(player, RESOURCE_ARMOR) <= GetResourceAmount(this, RESOURCE_ARMOR)) {return true;}
+       if (STAT(WEAPONS, item) && !(STAT(WEAPONS, player) & STAT(WEAPONS, item))) {return true;}
+       if (GetResourceAmount(item, RESOURCE_SHELLS) && GetResourceAmount(player, RESOURCE_SHELLS) <= GetResourceAmount(this, RESOURCE_SHELLS)) {return true;}
+       if (GetResourceAmount(item, RESOURCE_BULLETS) && GetResourceAmount(player, RESOURCE_BULLETS) <= GetResourceAmount(this, RESOURCE_BULLETS)) {return true;}
+       if (GetResourceAmount(item, RESOURCE_ROCKETS) && GetResourceAmount(player, RESOURCE_ROCKETS) <= GetResourceAmount(this, RESOURCE_ROCKETS)) {return true;}
+       if (GetResourceAmount(item, RESOURCE_CELLS) && GetResourceAmount(player, RESOURCE_CELLS) <= GetResourceAmount(this, RESOURCE_CELLS)) {return true;}
+       if (GetResourceAmount(item, RESOURCE_PLASMA) && GetResourceAmount(player, RESOURCE_PLASMA) <= GetResourceAmount(this, RESOURCE_PLASMA)) {return true;}
+       if (item.itemdef.instanceOfPowerup) {return true;}
+
+       return false;
+};
+
 bool havocbot_goalrating_item_pickable_check_players(entity this, vector org, entity item, vector item_org)
 {
        if(!teamplay)
                return true;
 
+       // actually these variables hold the squared distances in order to optimize code
        float friend_distance = FLOAT_MAX;
        float enemy_distance = FLOAT_MAX;
-       bool discard = false;
+       float dist;
 
        FOREACH_CLIENT(IS_PLAYER(it) && it != this && !IS_DEAD(it),
        {
                if (it.team == this.team)
                {
-                       if (!IS_REAL_CLIENT(it) || discard)
+                       if (!IS_REAL_CLIENT(it))
                                continue;
 
-                       if(vdist(it.origin - item_org, >, friend_distance))
+                       dist = vlen2(it.origin - item_org);
+                       if(dist > friend_distance)
                                continue;
 
-                       friend_distance = vlen(it.origin - item_org);
-                       discard = true;
-
-                       if (item.health && it.health > this.health) continue;
-                       if (item.armorvalue && it.armorvalue > this.armorvalue) continue;
-
-                       if (item.weapons && (item.weapons & ~it.weapons)) continue;
-
-                       if (item.ammo_shells && it.ammo_shells > this.ammo_shells) continue;
-                       if (item.ammo_nails && it.ammo_nails > this.ammo_nails) continue;
-                       if (item.ammo_rockets && it.ammo_rockets > this.ammo_rockets) continue;
-                       if (item.ammo_cells && it.ammo_cells > this.ammo_cells) continue;
-                       if (item.ammo_plasma && it.ammo_plasma > this.ammo_plasma) continue;
-
-                       discard = false;
+                       if(havocbot_goalrating_item_can_be_left_to_teammate(this, it, item))
+                       {
+                               friend_distance = dist;
+                               continue;
+                       }
                }
                else
                {
                        // If enemy only track distances
                        // TODO: track only if visible ?
-                       if(vdist(it.origin - item_org, <, enemy_distance))
-                               enemy_distance = vlen(it.origin - item_org);
+                       dist = vlen2(it.origin - item_org);
+                       if(dist < enemy_distance)
+                               enemy_distance = dist;
                }
        });
 
        // Rate the item only if no one needs it, or if an enemy is closer to it
-       if ((enemy_distance < friend_distance && vdist(item_org - org, <, enemy_distance)) ||
-               (friend_distance > autocvar_bot_ai_friends_aware_pickup_radius) || !discard)
+       dist = vlen2(item_org - org);
+       if ((enemy_distance < friend_distance && dist < enemy_distance) ||
+               (friend_distance > autocvar_bot_ai_friends_aware_pickup_radius ** 2) ||
+               (dist < friend_distance && dist < 200 ** 2))
                return true;
        return false;
 };
@@ -103,6 +117,13 @@ void havocbot_goalrating_items(entity this, float ratingscale, vector org, float
 
        IL_EACH(g_items, it.bot_pickup,
        {
+               // ignore if bot already rated this item with a higher ratingscale
+               // NOTE: this code assumes each bot rates items in a different frame
+               if(it.bot_ratingscale_time == time && ratingscale < it.bot_ratingscale)
+                       continue;
+               it.bot_ratingscale_time = time;
+               it.bot_ratingscale = ratingscale;
+
                if(!it.solid)
                {
                        if(!autocvar_bot_ai_timeitems)
@@ -130,7 +151,7 @@ void havocbot_goalrating_items(entity this, float ratingscale, vector org, float
                        continue;
 
                // Check if the item can be picked up safely
-               if(it.classname == "droppedweapon")
+               if(Item_IsLoot(it))
                {
                        if(!IS_ONGROUND(it))
                                continue;
@@ -153,9 +174,8 @@ void havocbot_goalrating_items(entity this, float ratingscale, vector org, float
 
                if(!havocbot_goalrating_item_pickable_check_players(this, org, it, o))
                        continue;
-               
-               rating = it.bot_pickupevalfunc(this, it);
 
+               rating = it.bot_pickupevalfunc(this, it);
                if(rating > 0)
                        navigation_routerating(this, it, rating * ratingscale, 2000);
        });
@@ -178,6 +198,8 @@ void havocbot_goalrating_enemyplayers(entity this, float ratingscale, vector org
                // TODO: Merge this logic with the bot_shouldattack function
                if(vdist(it.origin - org, <, 100) || vdist(it.origin - org, >, sradius))
                        continue;
+               if(vdist(vec2(it.velocity), >, autocvar_sv_maxspeed * 2))
+                       continue;
 
                // rate only visible enemies
                /*
@@ -186,7 +208,7 @@ void havocbot_goalrating_enemyplayers(entity this, float ratingscale, vector org
                        continue;
                */
 
-               t = ((this.health + this.armorvalue) - (it.health + it.armorvalue)) / 150;
+               t = ((GetResourceAmount(this, RESOURCE_HEALTH) + GetResourceAmount(this, RESOURCE_ARMOR)) - (GetResourceAmount(it, RESOURCE_HEALTH) + GetResourceAmount(it, RESOURCE_ARMOR))) / 150;
                t = bound(0, 1 + t, 3);
                if (skill > 3)
                {