]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blobdiff - qcsrc/server/bot/waypoints.qc
Merge branch 'master' into Mario/turrets
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / bot / waypoints.qc
index c48545e5aa2c922e60a7503b6c4103a4f8842221..627c622d2554de6f9b3a6015f5d8ffaebb2e7066 100644 (file)
@@ -1,12 +1,24 @@
+#include "waypoints.qh"
+#include "../_all.qh"
+
+#include "bot.qh"
+#include "navigation.qh"
+
+#include "../antilag.qh"
+
+#include "../../common/constants.qh"
+
+#include "../../warpzonelib/util_server.qh"
+
 // create a new spawnfunc_waypoint and automatically link it to other waypoints, and link
 // them back to it as well
 // (suitable for spawnfunc_waypoint editor)
 entity waypoint_spawn(vector m1, vector m2, float f)
 {
-       local entity w;
+       entity w;
        w = find(world, classname, "waypoint");
 
-       if not(f & WAYPOINTFLAG_PERSONAL)
+       if (!(f & WAYPOINTFLAG_PERSONAL))
        while (w)
        {
                // if a matching spawnfunc_waypoint already exists, don't add a duplicate
@@ -22,7 +34,7 @@ entity waypoint_spawn(vector m1, vector m2, float f)
        setorigin(w, (m1 + m2) * 0.5);
        setsize(w, m1 - w.origin, m2 - w.origin);
        if (vlen(w.size) > 0)
-               w.wpisbox = TRUE;
+               w.wpisbox = true;
 
        if(!w.wpisbox)
        {
@@ -67,12 +79,12 @@ entity waypoint_spawn(vector m1, vector m2, float f)
                w.model = "";
 
        return w;
-};
+}
 
 // add a new link to the spawnfunc_waypoint, replacing the furthest link it already has
 void waypoint_addlink(entity from, entity to)
 {
-       local float c;
+       float c;
 
        if (from == to)
                return;
@@ -92,7 +104,7 @@ void waypoint_addlink(entity from, entity to)
        {
                // if either is a box we have to find the nearest points on them to
                // calculate the distance properly
-               local vector v1, v2, m1, m2;
+               vector v1, v2, m1, m2;
                v1 = from.origin;
                m1 = to.absmin;
                m2 = to.absmax;
@@ -144,15 +156,15 @@ void waypoint_addlink(entity from, entity to)
        if (from.wp01mincost < c) {from.wp02 = to;from.wp02mincost = c;return;} from.wp02 = from.wp01;from.wp02mincost = from.wp01mincost;
        if (from.wp00mincost < c) {from.wp01 = to;from.wp01mincost = c;return;} from.wp01 = from.wp00;from.wp01mincost = from.wp00mincost;
        from.wp00 = to;from.wp00mincost = c;return;
-};
+}
 
 // relink this spawnfunc_waypoint
 // (precompile a list of all reachable waypoints from this spawnfunc_waypoint)
 // (SLOW!)
 void waypoint_think()
 {
-       local entity e;
-       local vector sv, sm1, sm2, ev, em1, em2, dv;
+       entity e;
+       vector sv, sm1, sm2, ev, em1, em2, dv;
 
        bot_calculate_stepheightvec();
 
@@ -177,17 +189,17 @@ void waypoint_think()
                                continue;
                        }
                        sv = e.origin;
-                       sv_x = bound(sm1_x, sv_x, sm2_x);
-                       sv_y = bound(sm1_y, sv_y, sm2_y);
-                       sv_z = bound(sm1_z, sv_z, sm2_z);
+                       sv.x = bound(sm1_x, sv.x, sm2_x);
+                       sv.y = bound(sm1_y, sv.y, sm2_y);
+                       sv.z = bound(sm1_z, sv.z, sm2_z);
                        ev = self.origin;
                        em1 = e.origin + e.mins;
                        em2 = e.origin + e.maxs;
-                       ev_x = bound(em1_x, ev_x, em2_x);
-                       ev_y = bound(em1_y, ev_y, em2_y);
-                       ev_z = bound(em1_z, ev_z, em2_z);
+                       ev.x = bound(em1_x, ev.x, em2_x);
+                       ev.y = bound(em1_y, ev.y, em2_y);
+                       ev.z = bound(em1_z, ev.z, em2_z);
                        dv = ev - sv;
-                       dv_z = 0;
+                       dv.z = 0;
                        if (vlen(dv) >= 1050) // max search distance in XY
                        {
                                ++relink_lengthculled;
@@ -196,7 +208,7 @@ void waypoint_think()
                        navigation_testtracewalk = 0;
                        if (!self.wpisbox)
                        {
-                               tracebox(sv - PL_MIN_z * '0 0 1', PL_MIN, PL_MAX, sv, FALSE, self);
+                               tracebox(sv - PL_MIN.z * '0 0 1', PL_MIN, PL_MAX, sv, false, self);
                                if (!trace_startsolid)
                                {
                                        //dprint("sv deviation", vtos(trace_endpos - sv), "\n");
@@ -205,14 +217,14 @@ void waypoint_think()
                        }
                        if (!e.wpisbox)
                        {
-                               tracebox(ev - PL_MIN_z * '0 0 1', PL_MIN, PL_MAX, ev, FALSE, e);
+                               tracebox(ev - PL_MIN.z * '0 0 1', PL_MIN, PL_MAX, ev, false, e);
                                if (!trace_startsolid)
                                {
                                        //dprint("ev deviation", vtos(trace_endpos - ev), "\n");
                                        ev = trace_endpos + '0 0 1';
                                }
                        }
-                       //traceline(self.origin, e.origin, FALSE, world);
+                       //traceline(self.origin, e.origin, false, world);
                        //if (trace_fraction == 1)
                        if (!self.wpisbox && tracewalk(self, sv, PL_MIN, PL_MAX, ev, MOVE_NOMONSTERS))
                                waypoint_addlink(self, e);
@@ -225,13 +237,13 @@ void waypoint_think()
                }
        }
        navigation_testtracewalk = 0;
-       self.wplinked = TRUE;
-};
+       self.wplinked = true;
+}
 
 void waypoint_clearlinks(entity wp)
 {
        // clear links to other waypoints
-       local float f;
+       float f;
        f = 10000000;
        wp.wp00 = wp.wp01 = wp.wp02 = wp.wp03 = wp.wp04 = wp.wp05 = wp.wp06 = wp.wp07 = world;
        wp.wp08 = wp.wp09 = wp.wp10 = wp.wp11 = wp.wp12 = wp.wp13 = wp.wp14 = wp.wp15 = world;
@@ -243,8 +255,8 @@ void waypoint_clearlinks(entity wp)
        wp.wp16mincost = wp.wp17mincost = wp.wp18mincost = wp.wp19mincost = wp.wp20mincost = wp.wp21mincost = wp.wp22mincost = wp.wp23mincost = f;
        wp.wp24mincost = wp.wp25mincost = wp.wp26mincost = wp.wp27mincost = wp.wp28mincost = wp.wp29mincost = wp.wp30mincost = wp.wp31mincost = f;
 
-       wp.wplinked = FALSE;
-};
+       wp.wplinked = false;
+}
 
 // tell a spawnfunc_waypoint to relink
 void waypoint_schedulerelink(entity wp)
@@ -254,7 +266,7 @@ void waypoint_schedulerelink(entity wp)
        // TODO: add some sort of visible box in edit mode for box waypoints
        if (autocvar_g_waypointeditor)
        {
-               local vector m1, m2;
+               vector m1, m2;
                m1 = wp.mins;
                m2 = wp.maxs;
                setmodel(wp, "models/runematch/rune.mdl"); wp.effects = EF_LOWPRECISION;
@@ -287,7 +299,7 @@ void spawnfunc_waypoint()
        // schedule a relink after other waypoints have had a chance to spawn
        waypoint_clearlinks(self);
        //waypoint_schedulerelink(self);
-};
+}
 
 // remove a spawnfunc_waypoint, and schedule all neighbors to relink
 void waypoint_remove(entity e)
@@ -327,12 +339,12 @@ void waypoint_remove(entity e)
        waypoint_schedulerelink(e.wp31);
        // and now remove the spawnfunc_waypoint
        remove(e);
-};
+}
 
 // empties the map of waypoints
 void waypoint_removeall()
 {
-       local entity head, next;
+       entity head, next;
        head = findchain(classname, "waypoint");
        while (head)
        {
@@ -340,13 +352,13 @@ void waypoint_removeall()
                remove(head);
                head = next;
        }
-};
+}
 
 // tell all waypoints to relink
 // (is this useful at all?)
 void waypoint_schedulerelinkall()
 {
-       local entity head;
+       entity head;
        relink_total = relink_walkculled = relink_pvsculled = relink_lengthculled = 0;
        head = findchain(classname, "waypoint");
        while (head)
@@ -354,15 +366,15 @@ void waypoint_schedulerelinkall()
                waypoint_schedulerelink(head);
                head = head.chain;
        }
-};
+}
 
 // Load waypoint links from file
 float waypoint_load_links()
 {
-       local string filename, s;
-       local float file, tokens, c, found;
-       local entity wp_from, wp_to;
-       local vector wp_to_pos, wp_from_pos;
+       string filename, s;
+       float file, tokens, c = 0, found;
+       entity wp_from = world, wp_to;
+       vector wp_to_pos, wp_from_pos;
        filename = strcat("maps/", mapname);
        filename = strcat(filename, ".waypoints.cache");
        file = fopen(filename, FILE_READ);
@@ -372,38 +384,34 @@ float waypoint_load_links()
                dprint("waypoint links load from ");
                dprint(filename);
                dprint(" failed\n");
-               return FALSE;
+               return false;
        }
 
-       while (1)
+       while ((s = fgets(file)))
        {
-               s = fgets(file);
-               if (!s)
-                       break;
-
                tokens = tokenizebyseparator(s, "*");
 
                if (tokens!=2)
                {
                        // bad file format
                        fclose(file);
-                       return FALSE;
+                       return false;
                }
 
                wp_from_pos     = stov(argv(0));
                wp_to_pos       = stov(argv(1));
 
                // Search "from" waypoint
-               if(wp_from.origin!=wp_from_pos)
+               if(!wp_from || wp_from.origin!=wp_from_pos)
                {
                        wp_from = findradius(wp_from_pos, 1);
-                       found = FALSE;
+                       found = false;
                        while(wp_from)
                        {
                                if(vlen(wp_from.origin-wp_from_pos)<1)
                                if(wp_from.classname == "waypoint")
                                {
-                                       found = TRUE;
+                                       found = true;
                                        break;
                                }
                                wp_from = wp_from.chain;
@@ -419,13 +427,13 @@ float waypoint_load_links()
 
                // Search "to" waypoint
                wp_to = findradius(wp_to_pos, 1);
-               found = FALSE;
+               found = false;
                while(wp_to)
                {
                        if(vlen(wp_to.origin-wp_to_pos)<1)
                        if(wp_to.classname == "waypoint")
                        {
-                               found = TRUE;
+                               found = true;
                                break;
                        }
                        wp_to = wp_to.chain;
@@ -449,21 +457,21 @@ float waypoint_load_links()
        dprint(mapname);
        dprint(".waypoints.cache\n");
 
-       botframe_cachedwaypointlinks = TRUE;
-       return TRUE;
-};
+       botframe_cachedwaypointlinks = true;
+       return true;
+}
 
 void waypoint_load_links_hardwired()
 {
-       local string filename, s;
-       local float file, tokens, c, found;
-       local entity wp_from, wp_to;
-       local vector wp_to_pos, wp_from_pos;
+       string filename, s;
+       float file, tokens, c = 0, found;
+       entity wp_from = world, wp_to;
+       vector wp_to_pos, wp_from_pos;
        filename = strcat("maps/", mapname);
        filename = strcat(filename, ".waypoints.hardwired");
        file = fopen(filename, FILE_READ);
 
-       botframe_loadedforcedlinks = TRUE;
+       botframe_loadedforcedlinks = true;
 
        if (file < 0)
        {
@@ -473,12 +481,8 @@ void waypoint_load_links_hardwired()
                return;
        }
 
-       for (;;)
+       while ((s = fgets(file)))
        {
-               s = fgets(file);
-               if (!s)
-                       break;
-
                if(substring(s, 0, 2)=="//")
                        continue;
 
@@ -494,16 +498,16 @@ void waypoint_load_links_hardwired()
                wp_to_pos       = stov(argv(1));
 
                // Search "from" waypoint
-               if(wp_from.origin!=wp_from_pos)
+               if(!wp_from || wp_from.origin!=wp_from_pos)
                {
                        wp_from = findradius(wp_from_pos, 5);
-                       found = FALSE;
+                       found = false;
                        while(wp_from)
                        {
                                if(vlen(wp_from.origin-wp_from_pos)<5)
                                if(wp_from.classname == "waypoint")
                                {
-                                       found = TRUE;
+                                       found = true;
                                        break;
                                }
                                wp_from = wp_from.chain;
@@ -518,13 +522,13 @@ void waypoint_load_links_hardwired()
 
                // Search "to" waypoint
                wp_to = findradius(wp_to_pos, 5);
-               found = FALSE;
+               found = false;
                while(wp_to)
                {
                        if(vlen(wp_to.origin-wp_to_pos)<5)
                        if(wp_to.classname == "waypoint")
                        {
-                               found = TRUE;
+                               found = true;
                                break;
                        }
                        wp_to = wp_to.chain;
@@ -538,8 +542,8 @@ void waypoint_load_links_hardwired()
 
                ++c;
                waypoint_addlink(wp_from, wp_to);
-               wp_from.wphardwired = TRUE;
-               wp_to.wphardwired = TRUE;
+               wp_from.wphardwired = true;
+               wp_to.wphardwired = true;
        }
 
        fclose(file);
@@ -549,14 +553,54 @@ void waypoint_load_links_hardwired()
        dprint(" waypoint links from maps/");
        dprint(mapname);
        dprint(".waypoints.hardwired\n");
-};
+}
+
+entity waypoint_get_link(entity w, float i)
+{
+       switch(i)
+       {
+               case  0:return w.wp00;
+               case  1:return w.wp01;
+               case  2:return w.wp02;
+               case  3:return w.wp03;
+               case  4:return w.wp04;
+               case  5:return w.wp05;
+               case  6:return w.wp06;
+               case  7:return w.wp07;
+               case  8:return w.wp08;
+               case  9:return w.wp09;
+               case 10:return w.wp10;
+               case 11:return w.wp11;
+               case 12:return w.wp12;
+               case 13:return w.wp13;
+               case 14:return w.wp14;
+               case 15:return w.wp15;
+               case 16:return w.wp16;
+               case 17:return w.wp17;
+               case 18:return w.wp18;
+               case 19:return w.wp19;
+               case 20:return w.wp20;
+               case 21:return w.wp21;
+               case 22:return w.wp22;
+               case 23:return w.wp23;
+               case 24:return w.wp24;
+               case 25:return w.wp25;
+               case 26:return w.wp26;
+               case 27:return w.wp27;
+               case 28:return w.wp28;
+               case 29:return w.wp29;
+               case 30:return w.wp30;
+               case 31:return w.wp31;
+               default:return world;
+       }
+}
 
 // Save all waypoint links to a file
 void waypoint_save_links()
 {
-       local string filename, s;
-       local float file, c, i;
-       local entity w, link;
+       string filename, s;
+       float file, c, i;
+       entity w, link;
        filename = strcat("maps/", mapname);
        filename = strcat(filename, ".waypoints.cache");
        file = fopen(filename, FILE_WRITE);
@@ -573,43 +617,7 @@ void waypoint_save_links()
                for(i=0;i<32;++i)
                {
                        // :S
-                       switch(i)
-                       {
-                               //      for i in $(seq -w 0 31); do echo "case $i:link = w.wp$i; break;"; done;
-                               case 00:link = w.wp00; break;
-                               case 01:link = w.wp01; break;
-                               case 02:link = w.wp02; break;
-                               case 03:link = w.wp03; break;
-                               case 04:link = w.wp04; break;
-                               case 05:link = w.wp05; break;
-                               case 06:link = w.wp06; break;
-                               case 07:link = w.wp07; break;
-                               case 08:link = w.wp08; break;
-                               case 09:link = w.wp09; break;
-                               case 10:link = w.wp10; break;
-                               case 11:link = w.wp11; break;
-                               case 12:link = w.wp12; break;
-                               case 13:link = w.wp13; break;
-                               case 14:link = w.wp14; break;
-                               case 15:link = w.wp15; break;
-                               case 16:link = w.wp16; break;
-                               case 17:link = w.wp17; break;
-                               case 18:link = w.wp18; break;
-                               case 19:link = w.wp19; break;
-                               case 20:link = w.wp20; break;
-                               case 21:link = w.wp21; break;
-                               case 22:link = w.wp22; break;
-                               case 23:link = w.wp23; break;
-                               case 24:link = w.wp24; break;
-                               case 25:link = w.wp25; break;
-                               case 26:link = w.wp26; break;
-                               case 27:link = w.wp27; break;
-                               case 28:link = w.wp28; break;
-                               case 29:link = w.wp29; break;
-                               case 30:link = w.wp30; break;
-                               case 31:link = w.wp31; break;
-                       }
-
+                       link = waypoint_get_link(w, i);
                        if(link==world)
                                continue;
 
@@ -620,21 +628,21 @@ void waypoint_save_links()
                w = w.chain;
        }
        fclose(file);
-       botframe_cachedwaypointlinks = TRUE;
+       botframe_cachedwaypointlinks = true;
 
        print("saved ");
        print(ftos(c));
        print(" waypoints links to maps/");
        print(mapname);
        print(".waypoints.cache\n");
-};
+}
 
 // save waypoints to gamedir/data/maps/mapname.waypoints
 void waypoint_saveall()
 {
-       local string filename, s;
-       local float file, c;
-       local entity w;
+       string filename, s;
+       float file, c;
+       entity w;
        filename = strcat("maps/", mapname);
        filename = strcat(filename, ".waypoints");
        file = fopen(filename, FILE_WRITE);
@@ -670,15 +678,15 @@ void waypoint_saveall()
                bprint(" failed\n");
        }
        waypoint_save_links();
-       botframe_loadedforcedlinks = FALSE;
-};
+       botframe_loadedforcedlinks = false;
+}
 
 // load waypoints from file
 float waypoint_loadall()
 {
-       local string filename, s;
-       local float file, cwp, cwb, fl;
-       local vector m1, m2;
+       string filename, s;
+       float file, cwp, cwb, fl;
+       vector m1, m2;
        cwp = 0;
        cwb = 0;
        filename = strcat("maps/", mapname);
@@ -686,11 +694,8 @@ float waypoint_loadall()
        file = fopen(filename, FILE_READ);
        if (file >= 0)
        {
-               while (1)
+               while ((s = fgets(file)))
                {
-                       s = fgets(file);
-                       if (!s)
-                               break;
                        m1 = stov(s);
                        s = fgets(file);
                        if (!s)
@@ -722,11 +727,11 @@ float waypoint_loadall()
                dprint(" failed\n");
        }
        return cwp + cwb;
-};
+}
 
 vector waypoint_fixorigin(vector position)
 {
-       tracebox(position + '0 0 1' * (1 - PL_MIN_z), PL_MIN, PL_MAX, position + '0 0 -512', MOVE_NOMONSTERS, world);
+       tracebox(position + '0 0 1' * (1 - PL_MIN.z), PL_MIN, PL_MAX, position + '0 0 -512', MOVE_NOMONSTERS, world);
        if(trace_fraction < 1)
                position = trace_endpos;
        //traceline(position, position + '0 0 -512', MOVE_NOMONSTERS, world);
@@ -736,7 +741,7 @@ vector waypoint_fixorigin(vector position)
 
 void waypoint_spawnforitem_force(entity e, vector org)
 {
-       local entity w;
+       entity w;
 
        // Fix the waypoint altitude if necessary
        org = waypoint_fixorigin(org);
@@ -772,12 +777,12 @@ void waypoint_spawnforitem(entity e)
                return;
 
        waypoint_spawnforitem_force(e, e.origin);
-};
+}
 
 void waypoint_spawnforteleporter_boxes(entity e, vector org1, vector org2, vector destination1, vector destination2, float timetaken)
 {
-       local entity w;
-       local entity dw;
+       entity w;
+       entity dw;
        w = waypoint_spawn(org1, org2, WAYPOINTFLAG_GENERATED | WAYPOINTFLAG_TELEPORT | WAYPOINTFLAG_NORELINK);
        dw = waypoint_spawn(destination1, destination2, WAYPOINTFLAG_GENERATED);
        // one way link to the destination
@@ -787,20 +792,20 @@ void waypoint_spawnforteleporter_boxes(entity e, vector org1, vector org2, vecto
        // (teleporters are not goals, so this is probably useless)
        e.nearestwaypoint = w;
        e.nearestwaypointtimeout = time + 1000000000;
-};
+}
 
 void waypoint_spawnforteleporter_v(entity e, vector org, vector destination, float timetaken)
 {
        org = waypoint_fixorigin(org);
        destination = waypoint_fixorigin(destination);
        waypoint_spawnforteleporter_boxes(e, org, org, destination, destination, timetaken);
-};
+}
 
 void waypoint_spawnforteleporter(entity e, vector destination, float timetaken)
 {
        destination = waypoint_fixorigin(destination);
        waypoint_spawnforteleporter_boxes(e, e.absmin, e.absmax, destination, destination, timetaken);
-};
+}
 
 entity waypoint_spawnpersonal(vector position)
 {
@@ -819,11 +824,11 @@ entity waypoint_spawnpersonal(vector position)
        waypoint_schedulerelink(w);
 
        return w;
-};
+}
 
 void botframe_showwaypointlinks()
 {
-       local entity player, head, w;
+       entity player, head, w;
        if (time < botframe_waypointeditorlightningtime)
                return;
        botframe_waypointeditorlightningtime = time + 0.5;
@@ -833,10 +838,10 @@ void botframe_showwaypointlinks()
                if (!player.isbot)
                if (player.flags & FL_ONGROUND || player.waterlevel > WATERLEVEL_NONE)
                {
-                       //navigation_testtracewalk = TRUE;
-                       head = navigation_findnearestwaypoint(player, FALSE);
+                       //navigation_testtracewalk = true;
+                       head = navigation_findnearestwaypoint(player, false);
                //      print("currently selected WP is ", etos(head), "\n");
-                       //navigation_testtracewalk = FALSE;
+                       //navigation_testtracewalk = false;
                        if (head)
                        {
                                w = head     ;if (w) te_lightning2(world, w.origin, player.origin);
@@ -876,4 +881,290 @@ void botframe_showwaypointlinks()
                }
                player = find(player, classname, "player");
        }
-};
+}
+
+float botframe_autowaypoints_fixdown(vector v)
+{
+       tracebox(v, PL_MIN, PL_MAX, v + '0 0 -64', MOVE_NOMONSTERS, world);
+       if(trace_fraction >= 1)
+               return 0;
+       return 1;
+}
+
+float botframe_autowaypoints_createwp(vector v, entity p, .entity fld, float f)
+{
+       entity w;
+
+       w = find(world, classname, "waypoint");
+       while (w)
+       {
+               // if a matching spawnfunc_waypoint already exists, don't add a duplicate
+               if (boxesoverlap(v - '32 32 32', v + '32 32 32', w.absmin, w.absmax))
+               //if (boxesoverlap(v - '4 4 4', v + '4 4 4', w.absmin, w.absmax))
+                       return 0;
+               w = find(w, classname, "waypoint");
+       }
+
+       waypoint_schedulerelink(p.(fld) = waypoint_spawn(v, v, f));
+       return 1;
+}
+
+// return value:
+//    1 = WP created
+//    0 = no action needed
+//   -1 = temp fail, try from world too
+//   -2 = permanent fail, do not retry
+float botframe_autowaypoints_fix_from(entity p, float walkfromwp, entity wp, .entity fld)
+{
+       // make it possible to go from p to wp, if we can
+       // if wp is world, nearest is chosen
+
+       entity w;
+       vector porg;
+       float t, tmin, tmax;
+       vector o;
+       vector save;
+
+       if(!botframe_autowaypoints_fixdown(p.origin))
+               return -2;
+       porg = trace_endpos;
+
+       if(wp)
+       {
+               // if any WP w fulfills wp -> w -> porg and w is closer than wp, then switch from wp to w
+
+               // if wp -> porg, then OK
+               float maxdist;
+               if(navigation_waypoint_will_link(wp.origin, porg, p, walkfromwp, 1050))
+               {
+                       // we may find a better one
+                       maxdist = vlen(wp.origin - porg);
+               }
+               else
+               {
+                       // accept any "good"
+                       maxdist = 2100;
+               }
+
+               float bestdist;
+               bestdist = maxdist;
+               w = find(world, classname, "waypoint");
+               while (w)
+               {
+                       if(w != wp && !(w.wpflags & WAYPOINTFLAG_NORELINK))
+                       {
+                               float d;
+                               d = vlen(wp.origin - w.origin) + vlen(w.origin - porg);
+                               if(d < bestdist)
+                                       if(navigation_waypoint_will_link(wp.origin, w.origin, p, walkfromwp, 1050))
+                                               if(navigation_waypoint_will_link(w.origin, porg, p, walkfromwp, 1050))
+                                               {
+                                                       bestdist = d;
+                                                       p.(fld) = w;
+                                               }
+                       }
+                       w = find(w, classname, "waypoint");
+               }
+               if(bestdist < maxdist)
+               {
+                       print("update chain to new nearest WP ", etos(p.(fld)), "\n");
+                       return 0;
+               }
+
+               if(bestdist < 2100)
+               {
+                       // we know maxdist < 2100
+                       // so wp -> porg is still valid
+                       // all is good
+                       p.(fld) = wp;
+                       return 0;
+               }
+
+               // otherwise, no existing WP can fix our issues
+       }
+       else
+       {
+               save = p.origin;
+               setorigin(p, porg);
+               w = navigation_findnearestwaypoint(p, walkfromwp);
+               setorigin(p, save);
+               if(w)
+               {
+                       p.(fld) = w;
+                       return 0;
+               }
+       }
+
+       tmin = 0;
+       tmax = 1;
+       for (;;)
+       {
+               if(tmax - tmin < 0.001)
+               {
+                       // did not get a good candidate
+                       return -1;
+               }
+
+               t = (tmin + tmax) * 0.5;
+               o = antilag_takebackorigin(p, time - t);
+               if(!botframe_autowaypoints_fixdown(o))
+                       return -2;
+               o = trace_endpos;
+
+               if(wp)
+               {
+                       if(!navigation_waypoint_will_link(wp.origin, o, p, walkfromwp, 1050))
+                       {
+                               // we cannot walk from wp.origin to o
+                               // get closer to tmax
+                               tmin = t;
+                               continue;
+                       }
+               }
+               else
+               {
+                       save = p.origin;
+                       setorigin(p, o);
+                       w = navigation_findnearestwaypoint(p, walkfromwp);
+                       setorigin(p, save);
+                       if(!w)
+                       {
+                               // we cannot walk from any WP to o
+                               // get closer to tmax
+                               tmin = t;
+                               continue;
+                       }
+               }
+
+               // if we get here, o is valid regarding waypoints
+               // check if o is connected right to the player
+               // we break if it succeeds, as that means o is a good waypoint location
+               if(navigation_waypoint_will_link(o, porg, p, walkfromwp, 1050))
+                       break;
+
+               // o is no good, we need to get closer to the player
+               tmax = t;
+       }
+
+       print("spawning a waypoint for connecting to ", etos(wp), "\n");
+       botframe_autowaypoints_createwp(o, p, fld, 0);
+       return 1;
+}
+
+// automatically create missing waypoints
+.entity botframe_autowaypoints_lastwp0, botframe_autowaypoints_lastwp1;
+void botframe_autowaypoints_fix(entity p, float walkfromwp, .entity fld)
+{
+       float r = botframe_autowaypoints_fix_from(p, walkfromwp, p.(fld), fld);
+       if(r != -1)
+               return;
+       r = botframe_autowaypoints_fix_from(p, walkfromwp, world, fld);
+       if(r != -1)
+               return;
+
+       print("emergency: got no good nearby WP to build a link from, starting a new chain\n");
+       if(!botframe_autowaypoints_fixdown(p.origin))
+               return; // shouldn't happen, caught above
+       botframe_autowaypoints_createwp(trace_endpos, p, fld, WAYPOINTFLAG_PROTECTED);
+}
+
+void botframe_deleteuselesswaypoints()
+{
+       entity w, w1, w2;
+       float i, j, k;
+       for (w = world; (w = findfloat(w, bot_pickup, true)); )
+       {
+               // NOTE: this protects waypoints if they're the ONLY nearest
+               // waypoint. That's the intention.
+               navigation_findnearestwaypoint(w, false);  // Walk TO item.
+               navigation_findnearestwaypoint(w, true);  // Walk FROM item.
+       }
+       for (w = world; (w = find(w, classname, "waypoint")); )
+       {
+               w.wpflags |= WAYPOINTFLAG_DEAD_END;
+               w.wpflags &= ~WAYPOINTFLAG_USEFUL;
+               // WP is useful if:
+               if (w.wpflags & WAYPOINTFLAG_ITEM)
+                       w.wpflags |= WAYPOINTFLAG_USEFUL;
+               if (w.wpflags & WAYPOINTFLAG_TELEPORT)
+                       w.wpflags |= WAYPOINTFLAG_USEFUL;
+               if (w.wpflags & WAYPOINTFLAG_PROTECTED)
+                       w.wpflags |= WAYPOINTFLAG_USEFUL;
+               // b) WP is closest WP for an item/spawnpoint/other entity
+               //    This has been done above by protecting these WPs.
+       }
+       // c) There are w1, w, w2 so that w1 -> w, w -> w2 and not w1 -> w2.
+       for (w1 = world; (w1 = find(w1, classname, "waypoint")); )
+       {
+               if (w1.wpflags & WAYPOINTFLAG_PERSONAL)
+                       continue;
+               for (i = 0; i < 32; ++i)
+               {
+                       w = waypoint_get_link(w1, i);
+                       if (!w)
+                               break;
+                       if (w.wpflags & WAYPOINTFLAG_PERSONAL)
+                               continue;
+                       if (w.wpflags & WAYPOINTFLAG_USEFUL)
+                               continue;
+                       for (j = 0; j < 32; ++j)
+                       {
+                               w2 = waypoint_get_link(w, j);
+                               if (!w2)
+                                       break;
+                               if (w1 == w2)
+                                       continue;
+                               if (w2.wpflags & WAYPOINTFLAG_PERSONAL)
+                                       continue;
+                               // If we got here, w1 != w2 exist with w1 -> w
+                               // and w -> w2. That means the waypoint is not
+                               // a dead end.
+                               w.wpflags &= ~WAYPOINTFLAG_DEAD_END;
+                               for (k = 0; k < 32; ++k)
+                               {
+                                       if (waypoint_get_link(w1, k) == w2)
+                                               continue;
+                                       // IF WE GET HERE, w is proven useful
+                                       // to get from w1 to w2!
+                                       w.wpflags |= WAYPOINTFLAG_USEFUL;
+                                       goto next;
+                               }
+                       }
+:next
+               }
+       }
+       // d) The waypoint is a dead end. Dead end waypoints must be kept as
+       //     they are needed to complete routes while autowaypointing.
+
+       for (w = world; (w = find(w, classname, "waypoint")); )
+       {
+               if (!(w.wpflags & (WAYPOINTFLAG_USEFUL | WAYPOINTFLAG_DEAD_END)))
+               {
+                       printf("Removed a waypoint at %v. Try again for more!\n", w.origin);
+                       te_explosion(w.origin);
+                       waypoint_remove(w);
+                       break;
+               }
+       }
+       for (w = world; (w = find(w, classname, "waypoint")); )
+               w.wpflags &= ~(WAYPOINTFLAG_USEFUL | WAYPOINTFLAG_DEAD_END); // temp flag
+}
+
+void botframe_autowaypoints()
+{
+       entity p;
+       FOR_EACH_REALPLAYER(p)
+       {
+               if(p.deadflag)
+                       continue;
+               // going back is broken, so only fix waypoints to walk TO the player
+               //botframe_autowaypoints_fix(p, false, botframe_autowaypoints_lastwp0);
+               botframe_autowaypoints_fix(p, true, botframe_autowaypoints_lastwp1);
+               //te_explosion(p.botframe_autowaypoints_lastwp0.origin);
+       }
+
+       if (autocvar_g_waypointeditor_auto >= 2) {
+               botframe_deleteuselesswaypoints();
+       }
+}
+