]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blobdiff - qcsrc/server/bot/default/havocbot/havocbot.qc
Merge remote-tracking branch 'origin/terencehill/bot_waypoints'
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / bot / default / havocbot / havocbot.qc
index 429254a5ef60d39631b7cebae03a91b4b301c7ee..b82398675c175290a77594bba4b420fc11362647 100644 (file)
@@ -18,8 +18,9 @@
 #include <common/items/_mod.qh>
 #include <common/wepent.qh>
 
-#include <common/triggers/teleporters.qh>
-#include <common/triggers/trigger/jumppads.qh>
+#include <common/mapobjects/func/ladder.qh>
+#include <common/mapobjects/teleporters.qh>
+#include <common/mapobjects/trigger/jumppads.qh>
 
 #include <lib/warpzone/common.qh>
 
@@ -30,6 +31,11 @@ void havocbot_ai(entity this)
        if(this.draggedby)
                return;
 
+       this.bot_aimdir_executed = false;
+       // lock aim if teleported or passing through a warpzone
+       if (this.lastteleporttime && !this.jumppadcount)
+               this.bot_aimdir_executed = true;
+
        if(bot_execute_commands(this))
                return;
 
@@ -105,14 +111,12 @@ void havocbot_ai(entity this)
        havocbot_aim(this);
        lag_update(this);
 
-       this.bot_aimdir_executed = false;
-
        if (this.bot_aimtarg)
        {
                this.aistatus |= AI_STATUS_ATTACKING;
                this.aistatus &= ~AI_STATUS_ROAMING;
 
-               if(this.weapons)
+               if(STAT(WEAPONS, this))
                {
                        if (autocvar_bot_nofire || IS_INDEPENDENT_PLAYER(this))
                        {
@@ -136,7 +140,7 @@ void havocbot_ai(entity this)
                else
                {
                        if(IS_PLAYER(this.bot_aimtarg))
-                               bot_aimdir(this, this.bot_aimtarg.origin + this.bot_aimtarg.view_ofs - this.origin - this.view_ofs , -1);
+                               bot_aimdir(this, this.bot_aimtarg.origin + this.bot_aimtarg.view_ofs - this.origin - this.view_ofs, 0);
                }
        }
        else if (this.goalcurrent)
@@ -152,7 +156,7 @@ void havocbot_ai(entity this)
                vector dir = get_closer_dest(this.goalcurrent, this.origin);
                dir -= this.origin + this.view_ofs;
                dir.z = 0;
-               bot_aimdir(this, dir, -1);
+               bot_aimdir(this, dir, 0);
        }
 
        // if the bot is not attacking, consider reloading weapons
@@ -176,7 +180,7 @@ void havocbot_ai(entity this)
                        if(this.(weaponentity).clip_load >= 0) // only if we're not reloading a weapon already
                        {
                                FOREACH(Weapons, it != WEP_Null, {
-                                       if((this.weapons & (it.m_wepset)) && (it.spawnflags & WEP_FLAG_RELOADABLE) && (this.(weaponentity).weapon_load[it.m_id] < it.reloading_ammo))
+                                       if((STAT(WEAPONS, this) & (it.m_wepset)) && (it.spawnflags & WEP_FLAG_RELOADABLE) && (this.(weaponentity).weapon_load[it.m_id] < it.reloading_ammo))
                                        {
                                                this.(weaponentity).m_switchweapon = it;
                                                break;
@@ -473,7 +477,7 @@ void havocbot_movetogoal(entity this)
        // Jetpack navigation
        if(this.navigation_jetpack_goal)
        if(this.goalcurrent==this.navigation_jetpack_goal)
-       if(this.ammo_fuel)
+       if(GetResourceAmount(this, RESOURCE_FUEL))
        {
                if(autocvar_bot_debug_goalstack)
                {
@@ -498,13 +502,13 @@ void havocbot_movetogoal(entity this)
                {
                        // Calculate brake distance in xy
                        float d = vlen(vec2(this.origin - (this.goalcurrent.absmin + this.goalcurrent.absmax) * 0.5));
-                       float v = vlen(vec2(this.velocity));
-                       float db = ((v ** 2) / (autocvar_g_jetpack_acceleration_side * 2)) + 100;
+                       float vel2 = vlen2(vec2(this.velocity));
+                       float db = (vel2 / (autocvar_g_jetpack_acceleration_side * 2)) + 100;
                        //LOG_INFOF("distance %d, velocity %d, brake at %d ", ceil(d), ceil(v), ceil(db));
                        if(d < db || d < 500)
                        {
                                // Brake
-                               if(v > maxspeed * 0.3)
+                               if (vel2 > (maxspeed * 0.3) ** 2)
                                {
                                        CS(this).movement_x = dir * v_forward * -maxspeed;
                                        return;
@@ -576,7 +580,7 @@ void havocbot_movetogoal(entity this)
                                        this.aistatus &= ~AI_STATUS_OUT_JUMPPAD;
                                }
                        }
-                       else
+                       else //if (this.goalcurrent)
                        {
                                if (this.goalcurrent.bot_pickup)
                                {
@@ -600,9 +604,9 @@ void havocbot_movetogoal(entity this)
                                        return;
                        }
                }
-               else
+               else //if (!(this.aistatus & AI_STATUS_OUT_JUMPPAD))
                {
-                       if(time - this.lastteleporttime > 0.2 && this.velocity.z > 0)
+                       if(this.origin.z - this.lastteleport_origin.z > (this.maxs.z - this.mins.z) * 0.5)
                        {
                                vector velxy = this.velocity; velxy_z = 0;
                                if(vdist(velxy, <, autocvar_sv_maxspeed * 0.2))
@@ -668,7 +672,8 @@ void havocbot_movetogoal(entity this)
 
                        return;
                }
-               else if(this.health + this.armorvalue > ROCKETJUMP_DAMAGE())
+               else if(!this.jumppadcount && !this.goalcurrent.wphardwired
+                       && GetResourceAmount(this, RESOURCE_HEALTH) + GetResourceAmount(this, RESOURCE_ARMOR) > ROCKETJUMP_DAMAGE())
                {
                        if(this.velocity.z < 0)
                        {
@@ -721,9 +726,10 @@ void havocbot_movetogoal(entity this)
                else
                        PHYS_INPUT_BUTTON_JUMP(this) = false;
                makevectors(this.v_angle.y * '0 1 0');
-               CS(this).movement_x = dir * v_forward * maxspeed;
-               CS(this).movement_y = dir * v_right * maxspeed;
-               CS(this).movement_z = dir * v_up * maxspeed;
+               vector v = dir * maxspeed;
+               CS(this).movement.x = v * v_forward;
+               CS(this).movement.y = v * v_right;
+               CS(this).movement.z = v * v_up;
        }
 
        // if there is nowhere to go, exit
@@ -772,23 +778,48 @@ void havocbot_movetogoal(entity this)
        if (this.goalcurrent == this.goalentity && this.goalentity_lock_timeout > time)
                locked_goal = true;
 
-       navigation_shortenpath(this);
+       if (navigation_shortenpath(this))
+       {
+               if (vdist(this.origin - this.goalcurrent_prev.origin, <, 50)
+                       && navigation_goalrating_timeout_can_be_anticipated(this))
+               {
+                       navigation_goalrating_timeout_force(this);
+               }
+       }
 
        bool goalcurrent_can_be_removed = false;
-       if (IS_MOVABLE(this.goalcurrent))
+       if (IS_PLAYER(this.goalcurrent) || IS_MONSTER(this.goalcurrent))
        {
-               // if is movable  =>  not frozen
-               if (IS_DEAD(this.goalcurrent) || (this.goalentity_shouldbefrozen && this.goalentity == this.goalcurrent))
+               bool freeze_state_changed = (boolean(STAT(FROZEN, this.goalentity)) != this.goalentity_shouldbefrozen);
+               if (IS_DEAD(this.goalcurrent) || (this.goalentity == this.goalcurrent && freeze_state_changed))
                {
                        goalcurrent_can_be_removed = true;
                        // don't remove if not visible
                        if (checkpvs(this.origin + this.view_ofs, this.goalcurrent))
                        {
-                               navigation_goalrating_timeout_force(this);
-                               return;
+                               if (IS_DEAD(this.goalcurrent))
+                               {
+                                       IL_EACH(g_items, it.enemy == this.goalcurrent && Item_IsLoot(it),
+                                       {
+                                               if (vdist(it.origin - this.goalcurrent.death_origin, <, 50))
+                                               {
+                                                       navigation_clearroute(this);
+                                                       navigation_pushroute(this, it);
+                                                       // loot can't be immediately rated since it isn't on ground yet
+                                                       // it will be rated after a second when on ground, meanwhile head to it
+                                                       navigation_goalrating_timeout_expire(this, 1);
+                                                       return;
+                                               }
+                                       });
+                               }
+                               if (!Item_IsLoot(this.goalcurrent))
+                               {
+                                       navigation_goalrating_timeout_force(this);
+                                       return;
+                               }
                        }
                }
-               else if (this.bot_tracewalk_time < time)
+               else if (!(STAT(FROZEN, this.goalentity)) && this.bot_tracewalk_time < time)
                {
                        set_tracewalk_dest(this.goalcurrent, this.origin, true);
                        if (!(trace_ent == this || tracewalk(this, this.origin, this.mins, this.maxs,
@@ -800,6 +831,7 @@ void havocbot_movetogoal(entity this)
                        this.bot_tracewalk_time = max(time, this.bot_tracewalk_time) + 0.25;
                }
        }
+
        if(!locked_goal)
        {
                // optimize path finding by anticipating goalrating when bot is near a waypoint;
@@ -854,27 +886,48 @@ void havocbot_movetogoal(entity this)
                // if bot used the jumppad, push towards jumppad origin until jumppad waypoint gets removed
                destorg = this.goalcurrent.origin;
        }
-       else if (this.goalcurrent.wpisbox && boxesoverlap(this.goalcurrent.absmin, this.goalcurrent.absmax, this.origin + eZ * this.mins.z, this.origin + eZ * this.maxs.z))
+       else if (this.goalcurrent.wpisbox)
        {
                // if bot is inside the teleport waypoint, head to teleport origin until teleport gets used
-               bunnyhop_forbidden = true;
-               destorg = this.goalcurrent.origin;
-               if(destorg.z > this.origin.z)
-                       PHYS_INPUT_BUTTON_JUMP(this) = true;
+               // do it even if bot is on a ledge above a teleport/jumppad so it doesn't get stuck
+               if (boxesoverlap(this.goalcurrent.absmin, this.goalcurrent.absmax, this.origin + eZ * this.mins.z, this.origin + eZ * this.maxs.z)
+                       || (this.absmin.z > destorg.z && destorg.x == this.origin.x && destorg.y == this.origin.y))
+               {
+                       bunnyhop_forbidden = true;
+                       destorg = this.goalcurrent.origin;
+                       if(destorg.z > this.origin.z)
+                               PHYS_INPUT_BUTTON_JUMP(this) = true;
+               }
        }
 
        diff = destorg - this.origin;
 
-       // 1. stop if too close to target player (even if frozen)
-       // 2. stop if the locked goal has been reached
-       if ((IS_PLAYER(this.goalcurrent) && vdist(diff, <, 80))
+       if (time < this.bot_stop_moving_timeout
                || (this.goalcurrent == this.goalentity && time < this.goalentity_lock_timeout && vdist(diff, <, 10)))
        {
+               // stop if the locked goal has been reached
                destorg = this.origin;
-               diff = '0 0 0';
+               diff = dir = '0 0 0';
        }
-
-       dir = normalize(diff);
+       else if (IS_PLAYER(this.goalcurrent) || IS_MONSTER(this.goalcurrent))
+       {
+               if (vdist(diff, <, 80))
+               {
+                       // stop if too close to target player (even if frozen)
+                       destorg = this.origin;
+                       diff = dir = '0 0 0';
+               }
+               else
+               {
+                       // move destorg out of target players, otherwise bot will consider them
+                       // an obstacle that needs to be jumped (especially if frozen)
+                       dir = normalize(diff);
+                       destorg -= dir * PL_MAX_CONST.x * M_SQRT2;
+                       diff = destorg - this.origin;
+               }
+       }
+       else
+               dir = normalize(diff);
        flatdir = (diff.z == 0) ? dir : normalize(vec2(diff));
 
        //if (this.bot_dodgevector_time < time)
@@ -886,9 +939,9 @@ void havocbot_movetogoal(entity this)
 
                this.aistatus &= ~AI_STATUS_DANGER_AHEAD;
                makevectors(this.v_angle.y * '0 1 0');
-               if (this.waterlevel)
+               if (this.waterlevel > WATERLEVEL_WETFEET)
                {
-                       if(this.waterlevel>WATERLEVEL_SWIMMING)
+                       if (this.waterlevel > WATERLEVEL_SWIMMING)
                        {
                                if(!this.goalcurrent)
                                        this.aistatus |= AI_STATUS_OUT_WATER;
@@ -897,12 +950,18 @@ void havocbot_movetogoal(entity this)
                        }
                        else
                        {
-                               dir = flatdir;
                                if(this.velocity.z >= 0 && !(this.watertype == CONTENT_WATER && destorg.z < this.origin.z) &&
-                                       ( !(this.waterlevel == WATERLEVEL_WETFEET && this.watertype == CONTENT_WATER) || this.aistatus & AI_STATUS_OUT_WATER))
+                                       (this.aistatus & AI_STATUS_OUT_WATER))
+                               {
                                        PHYS_INPUT_BUTTON_JUMP(this) = true;
+                                       dir = flatdir;
+                               }
                                else
+                               {
                                        PHYS_INPUT_BUTTON_JUMP(this) = false;
+                                       if (destorg.z > this.origin.z)
+                                               dir = flatdir;
+                               }
                        }
                }
                else
@@ -932,8 +991,16 @@ void havocbot_movetogoal(entity this)
                        {
                                if (vlen2(flat_diff) < vlen2(offset))
                                {
-                                       actual_destorg.x = destorg.x;
-                                       actual_destorg.y = destorg.y;
+                                       if (this.goalcurrent.wpflags & WAYPOINTFLAG_JUMP && this.goalstack01)
+                                       {
+                                               // oblique warpzones need a jump otherwise bots gets stuck
+                                               PHYS_INPUT_BUTTON_JUMP(this) = true;
+                                       }
+                                       else
+                                       {
+                                               actual_destorg.x = destorg.x;
+                                               actual_destorg.y = destorg.y;
+                                       }
                                }
                        }
                        else if (vdist(flat_diff, <, 32) && diff.z < -16) // destination is under the bot
@@ -1026,6 +1093,12 @@ void havocbot_movetogoal(entity this)
                        // Check for water/slime/lava and dangerous edges
                        // (only when the bot is on the ground or jumping intentionally)
 
+                       if (skill + this.bot_moveskill <= 3 && time > this.bot_stop_moving_timeout
+                               && current_speed > maxspeed * 0.9 && fabs(deviation.y) > 70)
+                       {
+                               this.bot_stop_moving_timeout = time + 0.4 + random() * 0.2;
+                       }
+
                        offset = (vdist(this.velocity, >, 32) ? this.velocity * 0.2 : v_forward * 32);
                        vector dst_ahead = this.origin + this.view_ofs + offset;
                        vector dst_down = dst_ahead - '0 0 3000';
@@ -1129,7 +1202,10 @@ void havocbot_movetogoal(entity this)
                }
                if (ladder_zdir)
                {
-                       dir.z = ladder_zdir * 1.3;
+                       if (vdist(vec2(diff), <, 40))
+                               dir.z = ladder_zdir * 4;
+                       else
+                               dir.z = ladder_zdir * 2;
                        dir = normalize(dir);
                }
        }
@@ -1144,8 +1220,14 @@ void havocbot_movetogoal(entity this)
                dir = normalize(dir);
        }
 
+       // already executed when bot targets an enemy
        if (!this.bot_aimdir_executed)
-               bot_aimdir(this, dir, -1);
+       {
+               if (time < this.bot_stop_moving_timeout)
+                       bot_aimdir(this, normalize(this.goalcurrent.origin - this.origin), 0);
+               else
+                       bot_aimdir(this, dir, 0);
+       }
 
        if (!ladder_zdir)
        {
@@ -1153,6 +1235,7 @@ void havocbot_movetogoal(entity this)
                dir = normalize(dir + dodge);
        }
 
+       makevectors(this.v_angle);
        //dir = this.bot_dodgevector;
        //if (this.bot_dodgevector_jumpbutton)
        //      PHYS_INPUT_BUTTON_JUMP(this) = true;
@@ -1220,7 +1303,7 @@ void havocbot_chooseenemy(entity this)
                        traceline(this.origin+this.view_ofs, ( this.enemy.absmin + this.enemy.absmax ) * 0.5,false,NULL);
                        if (trace_ent == this.enemy || trace_fraction == 1)
                        if (vdist(((this.enemy.absmin + this.enemy.absmax) * 0.5) - this.origin, <, 1000))
-                       if (this.health > 30)
+                       if (GetResourceAmount(this, RESOURCE_HEALTH) > 30)
                        {
                                // remain tracking him for a shot while (case he went after a small corner or pilar
                                this.havocbot_chooseenemy_finished = time + 0.5;
@@ -1291,7 +1374,7 @@ LABEL(scan_targets)
 
                // I want to do a second scan if no enemy was found or I don't have weapons
                // TODO: Perform the scan when using the rifle (requires changes on the rifle code)
-               if(best || this.weapons) // || this.weapon == WEP_RIFLE.m_id
+               if(best || STAT(WEAPONS, this)) // || this.weapon == WEP_RIFLE.m_id
                        break;
                if(scan_transparent)
                        break;
@@ -1368,7 +1451,7 @@ void havocbot_chooseweapon(entity this, .entity weaponentity)
        // Should it do a weapon combo?
        float af, ct, combo_time, combo;
 
-       af = ATTACK_FINISHED(this, 0);
+       af = ATTACK_FINISHED(this, weaponentity);
        ct = autocvar_bot_ai_weapon_combo_threshold;
 
        // Bots with no skill will be 4 times more slower than "godlike" bots when doing weapon combos
@@ -1516,14 +1599,14 @@ float havocbot_moveto(entity this, vector pos)
                        vector dir = get_closer_dest(this.goalcurrent, this.origin);
                        dir -= this.origin + this.view_ofs;
                        dir.z = 0;
-                       bot_aimdir(this, dir, -1);
+                       bot_aimdir(this, dir, 0);
                }
 
                if(this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_REACHED)
                {
                        // Step 5: Waypoint reached
                        LOG_TRACE(this.netname, "'s personal waypoint reached");
-                       delete(this.havocbot_personal_waypoint);
+                       waypoint_remove(this.havocbot_personal_waypoint);
                        this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_REACHED;
                        return CMD_STATUS_FINISHED;
                }
@@ -1592,6 +1675,7 @@ void havocbot_setupbot(entity this)
        this.cmd_moveto = havocbot_moveto;
        this.cmd_resetgoal = havocbot_resetgoal;
 
+       // NOTE: bot is not player yet
        havocbot_chooserole(this);
 }