]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blobdiff - qcsrc/server/bot/default/navigation.qc
More work was needed to complete transition to travel cost expressed in time rather...
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / bot / default / navigation.qc
index fd8a521d1de2c8dd7a084e2f98351b863e5029f8..e1debd404f23fd6573b5fcbe947260726f361e0e 100644 (file)
 
 .float speed;
 
+void navigation_dynamicgoal_init(entity this, bool initially_static)
+{
+       this.navigation_dynamicgoal = true;
+       this.bot_basewaypoint = this.nearestwaypoint;
+       if(initially_static)
+               this.nearestwaypointtimeout = -1;
+       else
+               this.nearestwaypointtimeout = time;
+}
+
+void navigation_dynamicgoal_set(entity this)
+{
+       this.nearestwaypointtimeout = time;
+}
+
+void navigation_dynamicgoal_unset(entity this)
+{
+       if(this.bot_basewaypoint)
+               this.nearestwaypoint = this.bot_basewaypoint;
+       this.nearestwaypointtimeout = -1;
+}
+
 // rough simulation of walking from one point to another to test if a path
 // can be traveled, used for waypoint linking and havocbot
 
@@ -26,7 +48,6 @@ bool tracewalk(entity e, vector start, vector m1, vector m2, vector end, float m
        float dist;
        float totaldist;
        float stepdist;
-       float yaw;
        float ignorehazards;
        float swimming;
 
@@ -70,7 +91,6 @@ bool tracewalk(entity e, vector start, vector m1, vector m2, vector end, float m
        }
 
        // Movement loop
-       yaw = vectoyaw(move);
        move = end - org;
        for (;;)
        {
@@ -115,14 +135,14 @@ bool tracewalk(entity e, vector start, vector m1, vector m2, vector end, float m
                        if (trace_fraction < 1)
                        {
                                swimming = true;
-                               org = trace_endpos - normalize(org - trace_endpos) * stepdist;
+                               org = trace_endpos + normalize(org - trace_endpos) * stepdist;
                                for (; org.z < end.z + e.maxs.z; org.z += stepdist)
                                {
-                                               if(autocvar_bot_debug_tracewalk)
-                                                       debugnode(e, org);
+                                       if(autocvar_bot_debug_tracewalk)
+                                               debugnode(e, org);
 
-                                       if(pointcontents(org) == CONTENT_EMPTY)
-                                                       break;
+                                       if(pointcontents(org) == CONTENT_EMPTY)
+                                               break;
                                }
 
                                if(pointcontents(org + '0 0 1') != CONTENT_EMPTY)
@@ -157,11 +177,26 @@ bool tracewalk(entity e, vector start, vector m1, vector m2, vector end, float m
                                        tracebox(org + jumpstepheightvec, m1, m2, move + jumpstepheightvec, movemode, e);
                                        if (trace_fraction < 1 || trace_startsolid)
                                        {
+                                               bool ladder_found = false;
+                                               FOREACH_ENTITY_CLASS("func_ladder", boxesoverlap(trace_endpos + m1 + '-1 -1 -1', trace_endpos + m2 + '1 1 1', it.absmin, it.absmax),
+                                               {
+                                                       if(boxesoverlap(end, end, it.absmin + (m1 - eZ * m1.z - '1 1 0'), it.absmax + (m2 - eZ * m2.z + '1 1 0')))
+                                                               ladder_found = true; // can't return here ("Loop mutex held by tracewalk" error)
+                                               });
+                                               if(ladder_found)
+                                               {
+                                                       if(autocvar_bot_debug_tracewalk)
+                                                               debugnodestatus(org, DEBUG_NODE_SUCCESS);
+
+                                                       //print("tracewalk: ", vtos(start), " can reach ", vtos(end), "\n");
+                                                       return true;
+                                               }
+
                                                if(autocvar_bot_debug_tracewalk)
                                                        debugnodestatus(trace_endpos, DEBUG_NODE_WARNING);
 
-                                               // check for doors
                                                traceline( org, move, movemode, e);
+
                                                if ( trace_ent.classname == "door_rotating" || trace_ent.classname == "door")
                                                {
                                                        vector nextmove;
@@ -231,7 +266,7 @@ bool tracewalk(entity e, vector start, vector m1, vector m2, vector end, float m
 void navigation_clearroute(entity this)
 {
        //print("bot ", etos(this), " clear\n");
-       this.navigation_hasgoals = false;
+       this.goalentity = NULL;
        this.goalcurrent = NULL;
        this.goalstack01 = NULL;
        this.goalstack02 = NULL;
@@ -275,6 +310,8 @@ void navigation_clearroute(entity this)
 void navigation_pushroute(entity this, entity e)
 {
        //print("bot ", etos(this), " push ", etos(e), "\n");
+       if(this.goalstack31 == this.goalentity)
+               this.goalentity = NULL;
        this.goalstack31 = this.goalstack30;
        this.goalstack30 = this.goalstack29;
        this.goalstack29 = this.goalstack28;
@@ -315,6 +352,8 @@ void navigation_pushroute(entity this, entity e)
 void navigation_poproute(entity this)
 {
        //print("bot ", etos(this), " pop\n");
+       if(this.goalcurrent == this.goalentity)
+               this.goalentity = NULL;
        this.goalcurrent = this.goalstack01;
        this.goalstack01 = this.goalstack02;
        this.goalstack02 = this.goalstack03;
@@ -457,7 +496,7 @@ float navigation_markroutes_nearestwaypoints(entity this, float maxdist)
                        if (tracewalk(this, this.origin, this.mins, this.maxs, v, bot_navigation_movemode))
                        {
                                it.wpnearestpoint = v;
-                               it.wpcost = vlen(v - this.origin) + it.dmg;
+                               it.wpcost = waypoint_getdistancecost(this.origin, v) + it.dmg;
                                it.wpfire = 1;
                                it.enemy = NULL;
                                c = c + 1;
@@ -484,7 +523,7 @@ void navigation_markroutes_checkwaypoint(entity w, entity wp, float cost2, vecto
        }
        else
                v = wp.origin;
-       cost2 = cost2 + vlen(v - p);
+       cost2 += waypoint_getdistancecost(p, v);
        if (wp.wpcost > cost2)
        {
                wp.wpcost = cost2;
@@ -649,6 +688,9 @@ void navigation_routerating(entity this, entity e, float f, float rangebias)
        if(e.blacklisted)
                return;
 
+       rangebias = waypoint_getdistancecost_simple(rangebias);
+       f = waypoint_getdistancecost_simple(f);
+
        if (IS_PLAYER(e))
        {
                bool rate_wps = false;
@@ -673,7 +715,9 @@ void navigation_routerating(entity this, entity e, float f, float rangebias)
                        entity theEnemy = e;
                        entity best_wp = NULL;
                        float best_dist = 10000;
-                       IL_EACH(g_waypoints, vdist(it.origin - theEnemy.origin, <, 500) && vdist(it.origin - this.origin, >, 100),
+                       IL_EACH(g_waypoints, vdist(it.origin - theEnemy.origin, <, 500)
+                               && vdist(it.origin - this.origin, >, 100)
+                               && !(it.wpflags & WAYPOINTFLAG_TELEPORT),
                        {
                                float dist = vlen(it.origin - theEnemy.origin);
                                if (dist < best_dist)
@@ -792,25 +836,11 @@ void navigation_routerating(entity this, entity e, float f, float rangebias)
        }
        else
        {
-               float search;
-
-               search = true;
-
-               if(e.flags & FL_ITEM)
-               {
-                       if (!(e.flags & FL_WEAPON))
-                       if(e.nearestwaypoint)
-                               search = false;
-               }
-               else if (e.flags & FL_WEAPON)
-               {
-                       if(e.classname != "droppedweapon")
-                       if(e.nearestwaypoint)
-                               search = false;
-               }
+               if(autocvar_g_waypointeditor && e.nearestwaypointtimeout >= 0 && time > e.nearestwaypointtimeout)
+                       e.nearestwaypoint = NULL;
 
-               if(search)
-               if (time > e.nearestwaypointtimeout)
+               if ((!e.nearestwaypoint || e.navigation_dynamicgoal)
+                       && e.nearestwaypointtimeout >= 0 && time > e.nearestwaypointtimeout)
                {
                        nwp = navigation_findnearestwaypoint(e, true);
                        if(nwp)
@@ -819,13 +849,8 @@ void navigation_routerating(entity this, entity e, float f, float rangebias)
                        {
                                LOG_DEBUG("FAILED to find a nearest waypoint to '", e.classname, "' #", etos(e));
 
-                               if(e.flags & FL_ITEM)
+                               if(!e.navigation_dynamicgoal)
                                        e.blacklisted = true;
-                               else if (e.flags & FL_WEAPON)
-                               {
-                                       if(e.classname != "droppedweapon")
-                                               e.blacklisted = true;
-                               }
 
                                if(e.blacklisted)
                                {
@@ -834,11 +859,10 @@ void navigation_routerating(entity this, entity e, float f, float rangebias)
                                }
                        }
 
-                       // TODO: Cleaner solution, probably handling this timeout from ctf.qc
-                       if(e.classname=="item_flag_team")
+                       if(e.navigation_dynamicgoal)
                                e.nearestwaypointtimeout = time + 2;
-                       else
-                               e.nearestwaypointtimeout = time + random() * 3 + 5;
+                       else if(autocvar_g_waypointeditor)
+                               e.nearestwaypointtimeout = time + 3 + random() * 2;
                }
                nwp = e.nearestwaypoint;
        }
@@ -863,18 +887,22 @@ void navigation_routerating(entity this, entity e, float f, float rangebias)
 // adds an item to the the goal stack with the path to a given item
 bool navigation_routetogoal(entity this, entity e, vector startposition)
 {
-       this.goalentity = e;
-
        // if there is no goal, just exit
        if (!e)
                return false;
 
-       this.navigation_hasgoals = true;
+       this.goalentity = e;
 
        // put the entity on the goal stack
        //print("routetogoal ", etos(e), "\n");
        navigation_pushroute(this, e);
 
+       if(e.classname == "waypoint" && !(e.wpflags & WAYPOINTFLAG_PERSONAL))
+       {
+               this.wp_goal_prev1 = this.wp_goal_prev0;
+               this.wp_goal_prev0 = e;
+       }
+
        if(g_jetpack)
        if(e==this.navigation_jetpack_goal)
                return true;
@@ -883,15 +911,26 @@ bool navigation_routetogoal(entity this, entity e, vector startposition)
        if (tracewalk(this, startposition, STAT(PL_MIN, this), STAT(PL_MAX, this), (e.absmin + e.absmax) * 0.5, bot_navigation_movemode))
                return true;
 
+       entity nearest_wp = NULL;
        // see if there are waypoints describing a path to the item
        if(e.classname != "waypoint" || (e.wpflags & WAYPOINTFLAG_PERSONAL))
+       {
                e = e.nearestwaypoint;
+               nearest_wp = e;
+       }
        else
                e = e.enemy; // we already have added it, so...
 
        if(e == NULL)
                return false;
 
+       if(nearest_wp && nearest_wp.enemy)
+       {
+               // often path can be optimized by not adding the nearest waypoint
+               if(tracewalk(this, nearest_wp.enemy.origin, STAT(PL_MIN, this), STAT(PL_MAX, this), (this.goalentity.absmin + this.goalentity.absmax) * 0.5, bot_navigation_movemode))
+                       e = nearest_wp.enemy;
+       }
+
        for (;;)
        {
                // add the spawnfunc_waypoint to the path
@@ -914,9 +953,6 @@ void navigation_poptouchedgoals(entity this)
        m1 = org + this.mins;
        m2 = org + this.maxs;
 
-       while(this.goalcurrent && wasfreed(this.goalcurrent))
-               navigation_poproute(this);
-
        if(this.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT)
        {
                // make sure jumppad is really hit, don't rely on distance based checks
@@ -935,13 +971,6 @@ void navigation_poptouchedgoals(entity this)
                }
        }
 
-       if(this.goalcurrent.bot_pickup_respawning)
-       {
-               if(!this.goalcurrent.solid)
-                       return;
-               this.goalcurrent.bot_pickup_respawning = false;
-       }
-
        // If for some reason the bot is closer to the next goal, pop the current one
        if(this.goalstack01 && !wasfreed(this.goalstack01))
        if(vlen2(this.goalcurrent.origin - this.origin) > vlen2(this.goalstack01.origin - this.origin))
@@ -960,9 +989,9 @@ void navigation_poptouchedgoals(entity this)
 
        // Loose goal touching check when running
        if(this.aistatus & AI_STATUS_RUNNING)
-       if(this.speed >= autocvar_sv_maxspeed) // if -really- running
        if(this.goalcurrent.classname=="waypoint")
        if(!(this.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT))
+       if(vlen(this.velocity - eZ * this.velocity.z) >= autocvar_sv_maxspeed) // if -really- running
        {
                if(vdist(this.origin - this.goalcurrent.origin, <, 150))
                {
@@ -982,8 +1011,18 @@ void navigation_poptouchedgoals(entity this)
                }
        }
 
-       while (this.goalcurrent && !IS_PLAYER(this.goalcurrent) && boxesoverlap(m1, m2, this.goalcurrent.absmin, this.goalcurrent.absmax))
+       while (this.goalcurrent && !IS_PLAYER(this.goalcurrent))
        {
+               vector gc_min = this.goalcurrent.absmin;
+               vector gc_max = this.goalcurrent.absmax;
+               if(this.goalcurrent.classname == "waypoint" && !this.goalcurrent.wpisbox)
+               {
+                       gc_min = this.goalcurrent.origin - '1 1 1' * 12;
+                       gc_max = this.goalcurrent.origin + '1 1 1' * 12;
+               }
+               if(!boxesoverlap(m1, m2, gc_min, gc_max))
+                       break;
+
                if((this.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT))
                        break;
 
@@ -1007,7 +1046,6 @@ void navigation_goalrating_start(entity this)
 
        this.navigation_jetpack_goal = NULL;
        navigation_bestrating = -1;
-       this.navigation_hasgoals = false;
        navigation_clearroute(this);
        navigation_bestgoal = NULL;
        navigation_markroutes(this, NULL);
@@ -1023,16 +1061,13 @@ void navigation_goalrating_end(entity this)
        LOG_DEBUG("best goal ", this.goalcurrent.classname);
 
        // If the bot got stuck then try to reach the farthest waypoint
-       if (!this.navigation_hasgoals)
-       if (autocvar_bot_wander_enable)
+       if (!this.goalentity && autocvar_bot_wander_enable)
        {
                if (!(this.aistatus & AI_STATUS_STUCK))
                {
                        LOG_DEBUG(this.netname, " cannot walk to any goal");
                        this.aistatus |= AI_STATUS_STUCK;
                }
-
-               this.navigation_hasgoals = false; // Reset this value
        }
 }
 
@@ -1053,7 +1088,7 @@ void botframe_updatedangerousobjects(float maxupdate)
                        v.y = bound(m1_y, v.y, m2_y);
                        v.z = bound(m1_z, v.z, m2_z);
                        o = (it.absmin + it.absmax) * 0.5;
-                       d = it.bot_dodgerating - vlen(o - v);
+                       d = waypoint_getdistancecost_simple(it.bot_dodgerating) - waypoint_getdistancecost(o, v);
                        if (d > 0)
                        {
                                traceline(o, v, true, NULL);