]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blobdiff - qcsrc/server/bot/navigation.qc
Merge remote-tracking branch 'origin/terencehill/better_dom_hud_stats'
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / bot / navigation.qc
index 95f5d5f0070be99471c02af1cdf4eb7d9de6bff1..ebaf210b3c493855e071ee409de9efdf34d7c233 100644 (file)
@@ -641,16 +641,18 @@ void navigation_markroutes_inverted(entity fixed_source_waypoint)
 void navigation_routerating(entity e, float f, float rangebias)
 {
        entity nwp;
+       vector o;
        if (!e)
                return;
 
+       o = (e.absmin + e.absmax) * 0.5;
        //print("routerating ", etos(e), " = ", ftos(f), " - ", ftos(rangebias), "\n");
 
        // Evaluate path using jetpack
        if(g_jetpack)
        if(self.items & IT_JETPACK)
        if(autocvar_bot_ai_navigation_jetpack)
-       if(vlen(self.origin - e.origin) > autocvar_bot_ai_navigation_jetpack_mindistance)
+       if(vlen(self.origin - o) > autocvar_bot_ai_navigation_jetpack_mindistance)
        {
                vector pointa, pointb;
 
@@ -661,7 +663,7 @@ void navigation_routerating(entity e, float f, float rangebias)
                pointa = trace_endpos - '0 0 1';
 
                // Point B
-               traceline(e.origin, e.origin + '0 0 65535', MOVE_NORMAL, e);
+               traceline(o, o + '0 0 65535', MOVE_NORMAL, e);
                pointb = trace_endpos - '0 0 1';
 
                // Can I see these two points from the sky?
@@ -768,7 +770,7 @@ void navigation_routerating(entity e, float f, float rangebias)
        {
                //te_wizspike(nwp.wpnearestpoint);
        //      dprint(e.classname, " ", ftos(f), "/(1+", ftos((nwp.wpcost + vlen(e.origin - nwp.wpnearestpoint))), "/", ftos(rangebias), ") = ");
-               f = f * rangebias / (rangebias + (nwp.wpcost + vlen(e.origin - nwp.wpnearestpoint)));
+               f = f * rangebias / (rangebias + (nwp.wpcost + vlen(o - nwp.wpnearestpoint)));
                //dprint("considering ", e.classname, " (with rating ", ftos(f), ")\n");
                //dprint(ftos(f));
                if (navigation_bestrating < f)
@@ -801,7 +803,7 @@ float navigation_routetogoal(entity e, vector startposition)
                return TRUE;
 
        // if it can reach the goal there is nothing more to do
-       if (tracewalk(self, startposition, PL_MIN, PL_MAX, e.origin, bot_navigation_movemode))
+       if (tracewalk(self, startposition, PL_MIN, PL_MAX, (e.absmin + e.absmax) * 0.5, bot_navigation_movemode))
                return TRUE;
 
        // see if there are waypoints describing a path to the item
@@ -890,6 +892,9 @@ void navigation_poptouchedgoals()
 // begin a goal selection session (queries spawnfunc_waypoint network)
 void navigation_goalrating_start()
 {
+       if(self.aistatus & AI_STATUS_STUCK)
+               return;
+
        self.navigation_jetpack_goal = world;
        navigation_bestrating = -1;
        self.navigation_hasgoals = FALSE;
@@ -901,29 +906,21 @@ void navigation_goalrating_start()
 // ends a goal selection session (updates goal stack to the best goal)
 void navigation_goalrating_end()
 {
+       if(self.aistatus & AI_STATUS_STUCK)
+               return;
+
        navigation_routetogoal(navigation_bestgoal, self.origin);
 //     dprint("best goal ", self.goalcurrent.classname , "\n");
 
-       // Hack: if it can't walk to any goal just move blindly to the first visible waypoint
+       // If the bot got stuck then try to reach the farthest waypoint
        if not (self.navigation_hasgoals)
+       if (autocvar_bot_wander_enable)
        {
-               dprint(self.netname, " can't walk to any goal, going to a near waypoint\n");
-
-               entity head;
-
-               RandomSelection_Init();
-               head = findradius(self.origin,1000);
-               while(head)
+               if not(self.aistatus & AI_STATUS_STUCK)
                {
-                       if(head.classname=="waypoint")
-                       if(!(head.wpflags & WAYPOINTFLAG_GENERATED))
-                       if(vlen(self.origin-head.origin)>100)
-                       if(checkpvs(self.view_ofs,head))
-                               RandomSelection_Add(head, 0, string_null, 1 + (vlen(self.origin-head.origin)<500), 0);
-                       head = head.chain;
+                       dprint(self.netname, " cannot walk to any goal\n");
+                       self.aistatus |= AI_STATUS_STUCK;
                }
-               if(RandomSelection_chosen_ent)
-                       navigation_routetogoal(RandomSelection_chosen_ent, self.origin);
 
                self.navigation_hasgoals = FALSE; // Reset this value
        }
@@ -932,7 +929,7 @@ void navigation_goalrating_end()
 void botframe_updatedangerousobjects(float maxupdate)
 {
        local entity head, bot_dodgelist;
-       local vector m1, m2, v;
+       local vector m1, m2, v, o;
        local float c, d, danger;
        c = 0;
        bot_dodgelist = findchainfloat(bot_dodge, TRUE);
@@ -949,10 +946,11 @@ void botframe_updatedangerousobjects(float maxupdate)
                        v_x = bound(m1_x, v_x, m2_x);
                        v_y = bound(m1_y, v_y, m2_y);
                        v_z = bound(m1_z, v_z, m2_z);
-                       d = head.bot_dodgerating - vlen(head.origin - v);
+                       o = (head.absmin + head.absmax) * 0.5;
+                       d = head.bot_dodgerating - vlen(o - v);
                        if (d > 0)
                        {
-                               traceline(head.origin, v, TRUE, world);
+                               traceline(o, v, TRUE, world);
                                if (trace_fraction == 1)
                                        danger = danger + d;
                        }
@@ -966,6 +964,96 @@ void botframe_updatedangerousobjects(float maxupdate)
        }
 };
 
+void navigation_unstuck()
+{
+       float search_radius = 1000;
+
+       if not(autocvar_bot_wander_enable)
+               return;
+
+       if not(bot_waypoint_queue_owner)
+       {
+       //      dprint(self.netname, " sutck, taking over the waypoints queue\n");
+               bot_waypoint_queue_owner = self;
+               bot_waypoint_queue_bestgoal = world;
+               bot_waypoint_queue_bestgoalrating = 0;
+       }
+
+       if(bot_waypoint_queue_owner!=self)
+               return;
+
+       if (bot_waypoint_queue_goal)
+       {
+               // evaluate the next goal on the queue
+               float d = vlen(self.origin - bot_waypoint_queue_goal.origin);
+               // dprint(self.netname, " evaluating ", bot_waypoint_queue_goal.classname, " with distance ", ftos(d), "\n");
+               if(tracewalk(bot_waypoint_queue_goal, self.origin, PL_MIN, PL_MAX, bot_waypoint_queue_goal.origin, bot_navigation_movemode))
+               {
+                       if( d > bot_waypoint_queue_bestgoalrating)
+                       {
+                               bot_waypoint_queue_bestgoalrating = d;
+                               bot_waypoint_queue_bestgoal = bot_waypoint_queue_goal;
+                       }
+               }
+               bot_waypoint_queue_goal = bot_waypoint_queue_goal.bot_waypoint_queue_nextgoal;
+
+               if not(bot_waypoint_queue_goal)
+               {
+                       if (bot_waypoint_queue_bestgoal)
+                       {
+                               dprint(self.netname, " stuck, reachable waypoint found, heading to it\n");
+                               navigation_routetogoal(bot_waypoint_queue_bestgoal, self.origin);
+                               self.bot_strategytime = time + autocvar_bot_ai_strategyinterval;
+                               self.aistatus &~= AI_STATUS_STUCK;
+                       }
+                       else
+                       {
+                               dprint(self.netname, " stuck, cannot walk to any waypoint at all\n");
+                       }
+
+                       bot_waypoint_queue_owner = world;
+               }
+       }
+       else
+       {
+               if(bot_strategytoken!=self)
+                       return;
+
+               // build a new queue
+               dprint(self.netname, " stuck, scanning reachable waypoints within ", ftos(search_radius)," qu\n");
+
+               entity head, first;
+
+               first = world;
+               head = findradius(self.origin, search_radius);
+
+               while(head)
+               {
+                       if(head.classname=="waypoint")
+               //      if(!(head.wpflags & WAYPOINTFLAG_GENERATED))
+                       {
+                               if(bot_waypoint_queue_goal)
+                                       bot_waypoint_queue_goal.bot_waypoint_queue_nextgoal = head;
+                               else
+                                       first = head;
+
+                               bot_waypoint_queue_goal = head;
+                               bot_waypoint_queue_goal.bot_waypoint_queue_nextgoal = world;
+                       }
+
+                       head = head.chain;
+               }
+
+               if (first)
+                       bot_waypoint_queue_goal = first;
+               else
+               {
+                       dprint(self.netname, " stuck, cannot walk to any waypoint at all\n");
+                       bot_waypoint_queue_owner = world;
+               }
+       }
+}
+
 // Support for debugging tracewalk visually
 
 void debugresetnodes()
@@ -1019,7 +1107,7 @@ void debugnodestatus(vector position, float status)
 void debuggoalstack()
 {
        local entity goal;
-       local vector org;
+       local vector org, go;
 
        if(self.goalcounter==0)goal=self.goalcurrent;
        else if(self.goalcounter==1)goal=self.goalstack01;
@@ -1067,8 +1155,9 @@ void debuggoalstack()
                org = self.lastposition;
 
 
-       te_lightning2(world, org, goal.origin);
-       self.lastposition = goal.origin;
+       go = ( goal.absmin + goal.absmax ) * 0.5;
+       te_lightning2(world, org, go);
+       self.lastposition = go;
 
        self.goalcounter++;
 }