Merge branch 'master' into terencehill/bot_waypoints
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / bot / default / waypoints.qc
1 #include "waypoints.qh"
2
3 #include <server/defs.qh>
4 #include <server/miscfunctions.qh>
5 #include "cvars.qh"
6
7 #include "bot.qh"
8 #include "navigation.qh"
9
10 #include <common/state.qh>
11
12 #include "../../antilag.qh"
13
14 #include <common/constants.qh>
15 #include <common/net_linked.qh>
16 #include <common/physics/player.qh>
17
18 #include <lib/warpzone/common.qh>
19 #include <lib/warpzone/util_server.qh>
20
21 .entity spawnpointmodel;
22 void waypoint_unreachable(entity pl)
23 {
24         IL_EACH(g_waypoints, true,
25         {
26                 it.colormod = '0.5 0.5 0.5';
27                 it.effects &= ~(EF_NODEPTHTEST | EF_RED | EF_BLUE);
28         });
29
30         entity e2 = navigation_findnearestwaypoint(pl, false);
31         if(!e2)
32         {
33                 LOG_INFOF("Can't find any waypoint nearby\n");
34                 return;
35         }
36
37         navigation_markroutes(pl, e2);
38
39         int j = 0;
40         int m = 0;
41         IL_EACH(g_waypoints, it.wpcost >= 10000000,
42         {
43                 LOG_INFO("unreachable: ", etos(it), " ", vtos(it.origin), "\n");
44                 it.colormod_z = 8;
45                 it.effects |= EF_NODEPTHTEST | EF_BLUE;
46                 j++;
47                 m++;
48         });
49         if (j) LOG_INFOF("%d waypoints cannot be reached from here in any way (marked with blue light)\n", j);
50         navigation_markroutes_inverted(e2);
51
52         j = 0;
53         IL_EACH(g_waypoints, it.wpcost >= 10000000,
54         {
55                 LOG_INFO("cannot reach me: ", etos(it), " ", vtos(it.origin), "\n");
56                 it.colormod_x = 8;
57                 if (!(it.effects & EF_NODEPTHTEST))  // not already reported before
58                         m++;
59                 it.effects |= EF_NODEPTHTEST | EF_RED;
60                 j++;
61         });
62         if (j) LOG_INFOF("%d waypoints cannot walk to here in any way (marked with red light)\n", j);
63         if (m) LOG_INFOF("%d waypoints have been marked total\n", m);
64
65         j = 0;
66         IL_EACH(g_spawnpoints, true,
67         {
68                 if (navigation_findnearestwaypoint(it, false))
69                 {
70                         if(it.spawnpointmodel)
71                         {
72                                 delete(it.spawnpointmodel);
73                                 it.spawnpointmodel = NULL;
74                         }
75                 }
76                 else
77                 {
78                         if(!it.spawnpointmodel)
79                         {
80                                 tracebox(it.origin, PL_MIN_CONST, PL_MAX_CONST, it.origin - '0 0 512', MOVE_NOMONSTERS, NULL);
81                                 entity e = new(spawnpointmodel);
82                                 vector org = trace_endpos + eZ;
83                                 setorigin(e, org);
84                                 e.solid = SOLID_TRIGGER;
85                                 it.spawnpointmodel = e;
86                         }
87                         LOG_INFO("spawn without waypoint: ", etos(it), " ", vtos(it.origin), "\n");
88                         it.spawnpointmodel.effects |= EF_NODEPTHTEST;
89                         _setmodel(it.spawnpointmodel, pl.model);
90                         it.spawnpointmodel.frame = pl.frame;
91                         it.spawnpointmodel.skin = pl.skin;
92                         it.spawnpointmodel.colormap = pl.colormap;
93                         it.spawnpointmodel.colormod = pl.colormod;
94                         it.spawnpointmodel.glowmod = pl.glowmod;
95                         setsize(it.spawnpointmodel, PL_MIN_CONST, PL_MAX_CONST);
96                         j++;
97                 }
98         });
99         if (j) LOG_INFOF("%d spawnpoints have no nearest waypoint (marked by player model)\n", j);
100
101         j = 0;
102         IL_EACH(g_items, true,
103         {
104                 it.effects &= ~(EF_NODEPTHTEST | EF_RED | EF_BLUE);
105                 it.colormod = '0.5 0.5 0.5';
106         });
107         IL_EACH(g_items, true,
108         {
109                 if (navigation_findnearestwaypoint(it, false))
110                         continue;
111                 LOG_INFO("item without waypoint: ", etos(it), " ", vtos(it.origin), "\n");
112                 it.effects |= EF_NODEPTHTEST | EF_RED;
113                 it.colormod_x = 8;
114                 j++;
115         });
116         if (j) LOG_INFOF("%d items have no nearest waypoint and cannot be walked away from (marked with red light)\n", j);
117
118         j = 0;
119         IL_EACH(g_items, true,
120         {
121                 if (navigation_findnearestwaypoint(it, true))
122                         continue;
123                 LOG_INFO("item without waypoint: ", etos(it), " ", vtos(it.origin), "\n");
124                 it.effects |= EF_NODEPTHTEST | EF_BLUE;
125                 it.colormod_z = 8;
126                 j++;
127         });
128         if (j) LOG_INFOF("%d items have no nearest waypoint and cannot be walked to (marked with blue light)\n", j);
129 }
130
131 vector waypoint_getSymmetricalOrigin(vector org, int ctf_flags)
132 {
133         vector new_org = org;
134         if (fabs(autocvar_g_waypointeditor_symmetrical) == 1)
135         {
136                 vector map_center = havocbot_middlepoint;
137                 if (autocvar_g_waypointeditor_symmetrical == -1)
138                         map_center = autocvar_g_waypointeditor_symmetrical_origin;
139
140                 new_org = Rotate(org - map_center, 360 * DEG2RAD / ctf_flags) + map_center;
141         }
142         else if (fabs(autocvar_g_waypointeditor_symmetrical) == 2)
143         {
144                 float m = havocbot_symmetryaxis_equation.x;
145                 float q = havocbot_symmetryaxis_equation.y;
146                 if (autocvar_g_waypointeditor_symmetrical == -2)
147                 {
148                         m = autocvar_g_waypointeditor_symmetrical_axis.x;
149                         q = autocvar_g_waypointeditor_symmetrical_axis.y;
150                 }
151
152                 new_org.x = (1 / (1 + m*m)) * ((1 - m*m) * org.x + 2 * m * org.y - 2 * m * q);
153                 new_org.y = (1 / (1 + m*m)) * (2 * m * org.x + (m*m - 1) * org.y + 2 * q);
154         }
155         new_org.z = org.z;
156         return new_org;
157 }
158
159 void waypoint_setupmodel(entity wp)
160 {
161         if (autocvar_g_waypointeditor)
162         {
163                 // TODO: add some sort of visible box in edit mode for box waypoints
164                 vector m1 = wp.mins;
165                 vector m2 = wp.maxs;
166                 setmodel(wp, MDL_WAYPOINT);
167                 setsize(wp, m1, m2);
168                 wp.effects = EF_LOWPRECISION;
169                 if (wp.wpflags & WAYPOINTFLAG_ITEM)
170                         wp.colormod = '1 0 0';
171                 else if (wp.wpflags & WAYPOINTFLAG_GENERATED)
172                         wp.colormod = '1 1 0';
173                 else if (wp.wphardwired)
174                         wp.colormod = '0.5 0 1';
175                 else
176                         wp.colormod = '1 1 1';
177         }
178         else
179                 wp.model = "";
180 }
181
182 // create a new spawnfunc_waypoint and automatically link it to other waypoints, and link
183 // them back to it as well
184 // (suitable for spawnfunc_waypoint editor)
185 entity waypoint_spawn(vector m1, vector m2, float f)
186 {
187         if(!(f & WAYPOINTFLAG_PERSONAL))
188         {
189                 vector em1 = m1, em2 = m2;
190                 if (!(f & WAYPOINTFLAG_GENERATED) && m1 == m2)
191                 {
192                         em1 = m1 - '8 8 8';
193                         em2 = m2 + '8 8 8';
194                 }
195                 IL_EACH(g_waypoints, boxesoverlap(em1, em2, it.absmin, it.absmax),
196                 {
197                         return it;
198                 });
199         }
200
201         entity w = new(waypoint);
202         IL_PUSH(g_waypoints, w);
203         w.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_PLAYERCLIP | DPCONTENTS_BOTCLIP;
204         w.wpflags = f;
205         w.solid = SOLID_TRIGGER;
206         setorigin(w, (m1 + m2) * 0.5);
207         setsize(w, m1 - w.origin, m2 - w.origin);
208         if (w.size)
209                 w.wpisbox = true;
210
211         if(!w.wpisbox)
212         {
213                 setsize(w, PL_MIN_CONST - '1 1 0', PL_MAX_CONST + '1 1 0');
214                 if(!move_out_of_solid(w))
215                 {
216                         if(!(f & WAYPOINTFLAG_GENERATED))
217                         {
218                                 LOG_TRACE("Killed a waypoint that was stuck in solid at ", vtos(w.origin));
219                                 delete(w);
220                                 return NULL;
221                         }
222                         else
223                         {
224                                 if(autocvar_developer)
225                                 {
226                                         LOG_INFO("A generated waypoint is stuck in solid at ", vtos(w.origin));
227                                         backtrace("Waypoint stuck");
228                                 }
229                         }
230                 }
231                 setsize(w, '0 0 0', '0 0 0');
232         }
233
234         waypoint_clearlinks(w);
235         //waypoint_schedulerelink(w);
236
237         waypoint_setupmodel(w);
238
239         return w;
240 }
241
242 void waypoint_spawn_fromeditor(entity pl)
243 {
244         entity e;
245         vector org = pl.origin;
246         int ctf_flags = havocbot_symmetryaxis_equation.z;
247         bool sym = ((autocvar_g_waypointeditor_symmetrical > 0 && ctf_flags >= 2)
248                    || (autocvar_g_waypointeditor_symmetrical < 0));
249         int order = ctf_flags;
250         if(autocvar_g_waypointeditor_symmetrical_order >= 2)
251         {
252                 order = autocvar_g_waypointeditor_symmetrical_order;
253                 ctf_flags = order;
254         }
255
256         if(!PHYS_INPUT_BUTTON_CROUCH(pl))
257         {
258                 // snap waypoint to item's origin if close enough
259                 IL_EACH(g_items, true,
260                 {
261                         vector item_org = (it.absmin + it.absmax) * 0.5;
262                         item_org.z = it.absmin.z - PL_MIN_CONST.z;
263                         if(vlen(item_org - org) < 30)
264                         {
265                                 org = item_org;
266                                 break;
267                         }
268                 });
269         }
270
271         LABEL(add_wp);
272         e = waypoint_spawn(org, org, 0);
273         if(!e)
274         {
275                 LOG_INFOF("Couldn't spawn waypoint at %v\n", org);
276                 return;
277         }
278         waypoint_schedulerelink(e);
279         bprint(strcat("Waypoint spawned at ", vtos(e.origin), "\n"));
280         if(sym)
281         {
282                 org = waypoint_getSymmetricalOrigin(e.origin, ctf_flags);
283                 if (vdist(org - pl.origin, >, 32))
284                 {
285                         if(order > 2)
286                                 order--;
287                         else
288                                 sym = false;
289                         goto add_wp;
290                 }
291         }
292 }
293
294 void waypoint_remove(entity wp)
295 {
296         // tell all waypoints linked to wp that they need to relink
297         IL_EACH(g_waypoints, it != wp,
298         {
299                 if (waypoint_islinked(it, wp))
300                         waypoint_removelink(it, wp);
301         });
302         delete(wp);
303 }
304
305 void waypoint_remove_fromeditor(entity pl)
306 {
307         entity e = navigation_findnearestwaypoint(pl, false);
308
309         int ctf_flags = havocbot_symmetryaxis_equation.z;
310         bool sym = ((autocvar_g_waypointeditor_symmetrical > 0 && ctf_flags >= 2)
311                    || (autocvar_g_waypointeditor_symmetrical < 0));
312         int order = ctf_flags;
313         if(autocvar_g_waypointeditor_symmetrical_order >= 2)
314         {
315                 order = autocvar_g_waypointeditor_symmetrical_order;
316                 ctf_flags = order;
317         }
318
319         LABEL(remove_wp);
320         if (!e) return;
321         if (e.wpflags & WAYPOINTFLAG_GENERATED) return;
322
323         if (e.wphardwired)
324         {
325                 LOG_INFO("^1Warning: ^7Removal of hardwired waypoints is not allowed in the editor. Please remove links from/to this waypoint (", vtos(e.origin), ") by hand from maps/", mapname, ".waypoints.hardwired\n");
326                 return;
327         }
328
329         entity wp_sym = NULL;
330         if (sym)
331         {
332                 vector org = waypoint_getSymmetricalOrigin(e.origin, ctf_flags);
333                 FOREACH_ENTITY_CLASS("waypoint", !(it.wpflags & WAYPOINTFLAG_GENERATED), {
334                         if(vdist(org - it.origin, <, 3))
335                         {
336                                 wp_sym = it;
337                                 break;
338                         }
339                 });
340         }
341
342         bprint(strcat("Waypoint removed at ", vtos(e.origin), "\n"));
343         waypoint_remove(e);
344
345         if (sym && wp_sym)
346         {
347                 e = wp_sym;
348                 if(order > 2)
349                         order--;
350                 else
351                         sym = false;
352                 goto remove_wp;
353         }
354 }
355
356 void waypoint_removelink(entity from, entity to)
357 {
358         if (from == to || (from.wpflags & WAYPOINTFLAG_NORELINK))
359                 return;
360
361         bool found = false;
362         if (!found && from.wp00 == to) found = true; if (found) {from.wp00 = from.wp01; from.wp00mincost = from.wp01mincost;}
363         if (!found && from.wp01 == to) found = true; if (found) {from.wp01 = from.wp02; from.wp01mincost = from.wp02mincost;}
364         if (!found && from.wp02 == to) found = true; if (found) {from.wp02 = from.wp03; from.wp02mincost = from.wp03mincost;}
365         if (!found && from.wp03 == to) found = true; if (found) {from.wp03 = from.wp04; from.wp03mincost = from.wp04mincost;}
366         if (!found && from.wp04 == to) found = true; if (found) {from.wp04 = from.wp05; from.wp04mincost = from.wp05mincost;}
367         if (!found && from.wp05 == to) found = true; if (found) {from.wp05 = from.wp06; from.wp05mincost = from.wp06mincost;}
368         if (!found && from.wp06 == to) found = true; if (found) {from.wp06 = from.wp07; from.wp06mincost = from.wp07mincost;}
369         if (!found && from.wp07 == to) found = true; if (found) {from.wp07 = from.wp08; from.wp07mincost = from.wp08mincost;}
370         if (!found && from.wp08 == to) found = true; if (found) {from.wp08 = from.wp09; from.wp08mincost = from.wp09mincost;}
371         if (!found && from.wp09 == to) found = true; if (found) {from.wp09 = from.wp10; from.wp09mincost = from.wp10mincost;}
372         if (!found && from.wp10 == to) found = true; if (found) {from.wp10 = from.wp11; from.wp10mincost = from.wp11mincost;}
373         if (!found && from.wp11 == to) found = true; if (found) {from.wp11 = from.wp12; from.wp11mincost = from.wp12mincost;}
374         if (!found && from.wp12 == to) found = true; if (found) {from.wp12 = from.wp13; from.wp12mincost = from.wp13mincost;}
375         if (!found && from.wp13 == to) found = true; if (found) {from.wp13 = from.wp14; from.wp13mincost = from.wp14mincost;}
376         if (!found && from.wp14 == to) found = true; if (found) {from.wp14 = from.wp15; from.wp14mincost = from.wp15mincost;}
377         if (!found && from.wp15 == to) found = true; if (found) {from.wp15 = from.wp16; from.wp15mincost = from.wp16mincost;}
378         if (!found && from.wp16 == to) found = true; if (found) {from.wp16 = from.wp17; from.wp16mincost = from.wp17mincost;}
379         if (!found && from.wp17 == to) found = true; if (found) {from.wp17 = from.wp18; from.wp17mincost = from.wp18mincost;}
380         if (!found && from.wp18 == to) found = true; if (found) {from.wp18 = from.wp19; from.wp18mincost = from.wp19mincost;}
381         if (!found && from.wp19 == to) found = true; if (found) {from.wp19 = from.wp20; from.wp19mincost = from.wp20mincost;}
382         if (!found && from.wp20 == to) found = true; if (found) {from.wp20 = from.wp21; from.wp20mincost = from.wp21mincost;}
383         if (!found && from.wp21 == to) found = true; if (found) {from.wp21 = from.wp22; from.wp21mincost = from.wp22mincost;}
384         if (!found && from.wp22 == to) found = true; if (found) {from.wp22 = from.wp23; from.wp22mincost = from.wp23mincost;}
385         if (!found && from.wp23 == to) found = true; if (found) {from.wp23 = from.wp24; from.wp23mincost = from.wp24mincost;}
386         if (!found && from.wp24 == to) found = true; if (found) {from.wp24 = from.wp25; from.wp24mincost = from.wp25mincost;}
387         if (!found && from.wp25 == to) found = true; if (found) {from.wp25 = from.wp26; from.wp25mincost = from.wp26mincost;}
388         if (!found && from.wp26 == to) found = true; if (found) {from.wp26 = from.wp27; from.wp26mincost = from.wp27mincost;}
389         if (!found && from.wp27 == to) found = true; if (found) {from.wp27 = from.wp28; from.wp27mincost = from.wp28mincost;}
390         if (!found && from.wp28 == to) found = true; if (found) {from.wp28 = from.wp29; from.wp28mincost = from.wp29mincost;}
391         if (!found && from.wp29 == to) found = true; if (found) {from.wp29 = from.wp30; from.wp29mincost = from.wp30mincost;}
392         if (!found && from.wp30 == to) found = true; if (found) {from.wp30 = from.wp31; from.wp30mincost = from.wp31mincost;}
393         if (found) {from.wp31 = NULL; from.wp31mincost = 10000000;}
394 }
395
396 bool waypoint_islinked(entity from, entity to)
397 {
398         if (from.wp00 == to) return true;if (from.wp01 == to) return true;if (from.wp02 == to) return true;if (from.wp03 == to) return true;
399         if (from.wp04 == to) return true;if (from.wp05 == to) return true;if (from.wp06 == to) return true;if (from.wp07 == to) return true;
400         if (from.wp08 == to) return true;if (from.wp09 == to) return true;if (from.wp10 == to) return true;if (from.wp11 == to) return true;
401         if (from.wp12 == to) return true;if (from.wp13 == to) return true;if (from.wp14 == to) return true;if (from.wp15 == to) return true;
402         if (from.wp16 == to) return true;if (from.wp17 == to) return true;if (from.wp18 == to) return true;if (from.wp19 == to) return true;
403         if (from.wp20 == to) return true;if (from.wp21 == to) return true;if (from.wp22 == to) return true;if (from.wp23 == to) return true;
404         if (from.wp24 == to) return true;if (from.wp25 == to) return true;if (from.wp26 == to) return true;if (from.wp27 == to) return true;
405         if (from.wp28 == to) return true;if (from.wp29 == to) return true;if (from.wp30 == to) return true;if (from.wp31 == to) return true;
406         return false;
407 }
408
409 void waypoint_updatecost_foralllinks()
410 {
411         IL_EACH(g_waypoints, !(it.wpflags & WAYPOINTFLAG_TELEPORT),
412         {
413                 if(it.wp00) it.wp00mincost = waypoint_getlinkcost(it, it.wp00);
414                 if(it.wp01) it.wp01mincost = waypoint_getlinkcost(it, it.wp01);
415                 if(it.wp02) it.wp02mincost = waypoint_getlinkcost(it, it.wp02);
416                 if(it.wp03) it.wp03mincost = waypoint_getlinkcost(it, it.wp03);
417                 if(it.wp04) it.wp04mincost = waypoint_getlinkcost(it, it.wp04);
418                 if(it.wp05) it.wp05mincost = waypoint_getlinkcost(it, it.wp05);
419                 if(it.wp06) it.wp06mincost = waypoint_getlinkcost(it, it.wp06);
420                 if(it.wp07) it.wp07mincost = waypoint_getlinkcost(it, it.wp07);
421                 if(it.wp08) it.wp08mincost = waypoint_getlinkcost(it, it.wp08);
422                 if(it.wp09) it.wp09mincost = waypoint_getlinkcost(it, it.wp09);
423                 if(it.wp10) it.wp10mincost = waypoint_getlinkcost(it, it.wp10);
424                 if(it.wp11) it.wp11mincost = waypoint_getlinkcost(it, it.wp11);
425                 if(it.wp12) it.wp12mincost = waypoint_getlinkcost(it, it.wp12);
426                 if(it.wp13) it.wp13mincost = waypoint_getlinkcost(it, it.wp13);
427                 if(it.wp14) it.wp14mincost = waypoint_getlinkcost(it, it.wp14);
428                 if(it.wp15) it.wp15mincost = waypoint_getlinkcost(it, it.wp15);
429                 if(it.wp16) it.wp16mincost = waypoint_getlinkcost(it, it.wp16);
430                 if(it.wp17) it.wp17mincost = waypoint_getlinkcost(it, it.wp17);
431                 if(it.wp18) it.wp18mincost = waypoint_getlinkcost(it, it.wp18);
432                 if(it.wp19) it.wp19mincost = waypoint_getlinkcost(it, it.wp19);
433                 if(it.wp20) it.wp20mincost = waypoint_getlinkcost(it, it.wp20);
434                 if(it.wp21) it.wp21mincost = waypoint_getlinkcost(it, it.wp21);
435                 if(it.wp22) it.wp22mincost = waypoint_getlinkcost(it, it.wp22);
436                 if(it.wp23) it.wp23mincost = waypoint_getlinkcost(it, it.wp23);
437                 if(it.wp24) it.wp24mincost = waypoint_getlinkcost(it, it.wp24);
438                 if(it.wp25) it.wp25mincost = waypoint_getlinkcost(it, it.wp25);
439                 if(it.wp26) it.wp26mincost = waypoint_getlinkcost(it, it.wp26);
440                 if(it.wp27) it.wp27mincost = waypoint_getlinkcost(it, it.wp27);
441                 if(it.wp28) it.wp28mincost = waypoint_getlinkcost(it, it.wp28);
442                 if(it.wp29) it.wp29mincost = waypoint_getlinkcost(it, it.wp29);
443                 if(it.wp30) it.wp30mincost = waypoint_getlinkcost(it, it.wp30);
444                 if(it.wp31) it.wp31mincost = waypoint_getlinkcost(it, it.wp31);
445         });
446 }
447
448 float waypoint_getlinearcost(float dist)
449 {
450         if(skill >= autocvar_bot_ai_bunnyhop_skilloffset)
451                 return dist / (autocvar_sv_maxspeed * 1.25);
452         return dist / autocvar_sv_maxspeed;
453 }
454 float waypoint_getlinearcost_underwater(float dist)
455 {
456         // NOTE: this value is hardcoded on the engine too, see SV_WaterMove
457         return dist / (autocvar_sv_maxspeed * 0.7);
458 }
459
460 float waypoint_gettravelcost(vector from, vector to, entity from_ent, entity to_ent)
461 {
462         bool submerged_from = navigation_check_submerged_state(from_ent, from);
463         bool submerged_to = navigation_check_submerged_state(to_ent, to);
464
465         if (submerged_from && submerged_to)
466                 return waypoint_getlinearcost_underwater(vlen(to - from));
467
468         float c = waypoint_getlinearcost(vlen(to - from));
469
470         float height = from.z - to.z;
471         if(height > jumpheight_vec.z && autocvar_sv_gravity > 0)
472         {
473                 float height_cost = sqrt(height / (autocvar_sv_gravity / 2));
474                 c = waypoint_getlinearcost(vlen(vec2(to - from))); // xy distance cost
475                 if(height_cost > c)
476                         c = height_cost;
477         }
478
479         if (submerged_from || submerged_to)
480                 return (c + waypoint_getlinearcost_underwater(vlen(to - from))) / 2;
481         return c;
482 }
483
484 float waypoint_getlinkcost(entity from, entity to)
485 {
486         vector v1 = from.origin;
487         vector v2 = to.origin;
488         if (from.wpisbox)
489         {
490                 vector m1 = from.absmin, m2 = from.absmax;
491                 v1.x = bound(m1.x, v2.x, m2.x);
492                 v1.y = bound(m1.y, v2.y, m2.y);
493                 v1.z = bound(m1.z, v2.z, m2.z);
494         }
495         if (to.wpisbox)
496         {
497                 vector m1 = to.absmin, m2 = to.absmax;
498                 v2.x = bound(m1.x, v1.x, m2.x);
499                 v2.y = bound(m1.y, v1.y, m2.y);
500                 v2.z = bound(m1.z, v1.z, m2.z);
501         }
502         return waypoint_gettravelcost(v1, v2, from, to);
503 }
504
505 // add a new link to the spawnfunc_waypoint, replacing the furthest link it already has
506 // if c == -1 automatically determine cost of the link
507 void waypoint_addlink_customcost(entity from, entity to, float c)
508 {
509         if (from == to || waypoint_islinked(from, to))
510                 return;
511         if (c == -1 && (from.wpflags & WAYPOINTFLAG_NORELINK))
512                 return;
513
514         if(c == -1)
515                 c = waypoint_getlinkcost(from, to);
516
517         if (from.wp31mincost < c) return;
518         if (from.wp30mincost < c) {from.wp31 = to;from.wp31mincost = c;return;} from.wp31 = from.wp30;from.wp31mincost = from.wp30mincost;
519         if (from.wp29mincost < c) {from.wp30 = to;from.wp30mincost = c;return;} from.wp30 = from.wp29;from.wp30mincost = from.wp29mincost;
520         if (from.wp28mincost < c) {from.wp29 = to;from.wp29mincost = c;return;} from.wp29 = from.wp28;from.wp29mincost = from.wp28mincost;
521         if (from.wp27mincost < c) {from.wp28 = to;from.wp28mincost = c;return;} from.wp28 = from.wp27;from.wp28mincost = from.wp27mincost;
522         if (from.wp26mincost < c) {from.wp27 = to;from.wp27mincost = c;return;} from.wp27 = from.wp26;from.wp27mincost = from.wp26mincost;
523         if (from.wp25mincost < c) {from.wp26 = to;from.wp26mincost = c;return;} from.wp26 = from.wp25;from.wp26mincost = from.wp25mincost;
524         if (from.wp24mincost < c) {from.wp25 = to;from.wp25mincost = c;return;} from.wp25 = from.wp24;from.wp25mincost = from.wp24mincost;
525         if (from.wp23mincost < c) {from.wp24 = to;from.wp24mincost = c;return;} from.wp24 = from.wp23;from.wp24mincost = from.wp23mincost;
526         if (from.wp22mincost < c) {from.wp23 = to;from.wp23mincost = c;return;} from.wp23 = from.wp22;from.wp23mincost = from.wp22mincost;
527         if (from.wp21mincost < c) {from.wp22 = to;from.wp22mincost = c;return;} from.wp22 = from.wp21;from.wp22mincost = from.wp21mincost;
528         if (from.wp20mincost < c) {from.wp21 = to;from.wp21mincost = c;return;} from.wp21 = from.wp20;from.wp21mincost = from.wp20mincost;
529         if (from.wp19mincost < c) {from.wp20 = to;from.wp20mincost = c;return;} from.wp20 = from.wp19;from.wp20mincost = from.wp19mincost;
530         if (from.wp18mincost < c) {from.wp19 = to;from.wp19mincost = c;return;} from.wp19 = from.wp18;from.wp19mincost = from.wp18mincost;
531         if (from.wp17mincost < c) {from.wp18 = to;from.wp18mincost = c;return;} from.wp18 = from.wp17;from.wp18mincost = from.wp17mincost;
532         if (from.wp16mincost < c) {from.wp17 = to;from.wp17mincost = c;return;} from.wp17 = from.wp16;from.wp17mincost = from.wp16mincost;
533         if (from.wp15mincost < c) {from.wp16 = to;from.wp16mincost = c;return;} from.wp16 = from.wp15;from.wp16mincost = from.wp15mincost;
534         if (from.wp14mincost < c) {from.wp15 = to;from.wp15mincost = c;return;} from.wp15 = from.wp14;from.wp15mincost = from.wp14mincost;
535         if (from.wp13mincost < c) {from.wp14 = to;from.wp14mincost = c;return;} from.wp14 = from.wp13;from.wp14mincost = from.wp13mincost;
536         if (from.wp12mincost < c) {from.wp13 = to;from.wp13mincost = c;return;} from.wp13 = from.wp12;from.wp13mincost = from.wp12mincost;
537         if (from.wp11mincost < c) {from.wp12 = to;from.wp12mincost = c;return;} from.wp12 = from.wp11;from.wp12mincost = from.wp11mincost;
538         if (from.wp10mincost < c) {from.wp11 = to;from.wp11mincost = c;return;} from.wp11 = from.wp10;from.wp11mincost = from.wp10mincost;
539         if (from.wp09mincost < c) {from.wp10 = to;from.wp10mincost = c;return;} from.wp10 = from.wp09;from.wp10mincost = from.wp09mincost;
540         if (from.wp08mincost < c) {from.wp09 = to;from.wp09mincost = c;return;} from.wp09 = from.wp08;from.wp09mincost = from.wp08mincost;
541         if (from.wp07mincost < c) {from.wp08 = to;from.wp08mincost = c;return;} from.wp08 = from.wp07;from.wp08mincost = from.wp07mincost;
542         if (from.wp06mincost < c) {from.wp07 = to;from.wp07mincost = c;return;} from.wp07 = from.wp06;from.wp07mincost = from.wp06mincost;
543         if (from.wp05mincost < c) {from.wp06 = to;from.wp06mincost = c;return;} from.wp06 = from.wp05;from.wp06mincost = from.wp05mincost;
544         if (from.wp04mincost < c) {from.wp05 = to;from.wp05mincost = c;return;} from.wp05 = from.wp04;from.wp05mincost = from.wp04mincost;
545         if (from.wp03mincost < c) {from.wp04 = to;from.wp04mincost = c;return;} from.wp04 = from.wp03;from.wp04mincost = from.wp03mincost;
546         if (from.wp02mincost < c) {from.wp03 = to;from.wp03mincost = c;return;} from.wp03 = from.wp02;from.wp03mincost = from.wp02mincost;
547         if (from.wp01mincost < c) {from.wp02 = to;from.wp02mincost = c;return;} from.wp02 = from.wp01;from.wp02mincost = from.wp01mincost;
548         if (from.wp00mincost < c) {from.wp01 = to;from.wp01mincost = c;return;} from.wp01 = from.wp00;from.wp01mincost = from.wp00mincost;
549         from.wp00 = to;from.wp00mincost = c;return;
550 }
551
552 void waypoint_addlink(entity from, entity to)
553 {
554         waypoint_addlink_customcost(from, to, -1);
555 }
556
557 // relink this spawnfunc_waypoint
558 // (precompile a list of all reachable waypoints from this spawnfunc_waypoint)
559 // (SLOW!)
560 void waypoint_think(entity this)
561 {
562         vector sv = '0 0 0', sv2 = '0 0 0', ev = '0 0 0', ev2 = '0 0 0', dv;
563         float sv2_height = 0, ev2_height = 0;
564
565         bot_calculate_stepheightvec();
566
567         bot_navigation_movemode = ((autocvar_bot_navigation_ignoreplayers) ? MOVE_NOMONSTERS : MOVE_NORMAL);
568
569         //dprint("waypoint_think wpisbox = ", ftos(this.wpisbox), "\n");
570         IL_EACH(g_waypoints, this != it,
571         {
572                 if (boxesoverlap(this.absmin, this.absmax, it.absmin, it.absmax))
573                 {
574                         waypoint_addlink(this, it);
575                         waypoint_addlink(it, this);
576                 }
577                 else
578                 {
579                         ++relink_total;
580                         if(!checkpvs(this.origin, it))
581                         {
582                                 ++relink_pvsculled;
583                                 continue;
584                         }
585
586                         SET_TRACEWALK_DESTCOORDS_2(this, it.origin, sv, sv2, sv2_height);
587                         SET_TRACEWALK_DESTCOORDS_2(it, this.origin, ev, ev2, ev2_height);
588
589                         dv = ev - sv;
590                         dv.z = 0;
591                         if(vdist(dv, >=, 1050)) // max search distance in XY
592                         {
593                                 ++relink_lengthculled;
594                                 continue;
595                         }
596
597                         navigation_testtracewalk = 0;
598
599                         //traceline(this.origin, it.origin, false, NULL);
600                         //if (trace_fraction == 1)
601                         if (this.wpisbox)
602                                 relink_walkculled += 0.5;
603                         else
604                         {
605                                 if (tracewalk(this, sv, PL_MIN_CONST, PL_MAX_CONST, ev2, ev2_height, MOVE_NOMONSTERS))
606                                         waypoint_addlink(this, it);
607                                 else
608                                         relink_walkculled += 0.5;
609                         }
610
611                         if (it.wpisbox)
612                                 relink_walkculled += 0.5;
613                         else
614                         {
615                                 if (tracewalk(it, ev, PL_MIN_CONST, PL_MAX_CONST, sv2, sv2_height, MOVE_NOMONSTERS))
616                                         waypoint_addlink(it, this);
617                                 else
618                                         relink_walkculled += 0.5;
619                         }
620                 }
621         });
622         navigation_testtracewalk = 0;
623         this.wplinked = true;
624 }
625
626 void waypoint_clearlinks(entity wp)
627 {
628         // clear links to other waypoints
629         float f = 10000000;
630         wp.wp00 = wp.wp01 = wp.wp02 = wp.wp03 = wp.wp04 = wp.wp05 = wp.wp06 = wp.wp07 = NULL;
631         wp.wp08 = wp.wp09 = wp.wp10 = wp.wp11 = wp.wp12 = wp.wp13 = wp.wp14 = wp.wp15 = NULL;
632         wp.wp16 = wp.wp17 = wp.wp18 = wp.wp19 = wp.wp20 = wp.wp21 = wp.wp22 = wp.wp23 = NULL;
633         wp.wp24 = wp.wp25 = wp.wp26 = wp.wp27 = wp.wp28 = wp.wp29 = wp.wp30 = wp.wp31 = NULL;
634
635         wp.wp00mincost = wp.wp01mincost = wp.wp02mincost = wp.wp03mincost = wp.wp04mincost = wp.wp05mincost = wp.wp06mincost = wp.wp07mincost = f;
636         wp.wp08mincost = wp.wp09mincost = wp.wp10mincost = wp.wp11mincost = wp.wp12mincost = wp.wp13mincost = wp.wp14mincost = wp.wp15mincost = f;
637         wp.wp16mincost = wp.wp17mincost = wp.wp18mincost = wp.wp19mincost = wp.wp20mincost = wp.wp21mincost = wp.wp22mincost = wp.wp23mincost = f;
638         wp.wp24mincost = wp.wp25mincost = wp.wp26mincost = wp.wp27mincost = wp.wp28mincost = wp.wp29mincost = wp.wp30mincost = wp.wp31mincost = f;
639
640         wp.wplinked = false;
641 }
642
643 // tell a spawnfunc_waypoint to relink
644 void waypoint_schedulerelink(entity wp)
645 {
646         if (wp == NULL)
647                 return;
648
649         waypoint_setupmodel(wp);
650         wp.wpisbox = vdist(wp.size, >, 0);
651         wp.enemy = NULL;
652         if (!(wp.wpflags & WAYPOINTFLAG_PERSONAL))
653                 wp.owner = NULL;
654         if (!(wp.wpflags & WAYPOINTFLAG_NORELINK))
655                 waypoint_clearlinks(wp);
656         // schedule an actual relink on next frame
657         setthink(wp, waypoint_think);
658         wp.nextthink = time;
659         wp.effects = EF_LOWPRECISION;
660 }
661
662 // spawnfunc_waypoint map entity
663 spawnfunc(waypoint)
664 {
665         IL_PUSH(g_waypoints, this);
666
667         setorigin(this, this.origin);
668         // schedule a relink after other waypoints have had a chance to spawn
669         waypoint_clearlinks(this);
670         //waypoint_schedulerelink(this);
671 }
672
673 // tell all waypoints to relink
674 // actually this is useful only to update relink_* stats
675 void waypoint_schedulerelinkall()
676 {
677         relink_total = relink_walkculled = relink_pvsculled = relink_lengthculled = 0;
678         IL_EACH(g_waypoints, true,
679         {
680                 waypoint_schedulerelink(it);
681         });
682         waypoint_load_links_hardwired();
683 }
684
685 // Load waypoint links from file
686 bool waypoint_load_links()
687 {
688         string filename, s;
689         float file, tokens, c = 0, found;
690         entity wp_from = NULL, wp_to;
691         vector wp_to_pos, wp_from_pos;
692         filename = strcat("maps/", mapname);
693         filename = strcat(filename, ".waypoints.cache");
694         file = fopen(filename, FILE_READ);
695
696         if (file < 0)
697         {
698                 LOG_TRACE("waypoint links load from ");
699                 LOG_TRACE(filename);
700                 LOG_TRACE(" failed");
701                 return false;
702         }
703
704         bool parse_comments = true;
705         float ver = 0;
706
707         while ((s = fgets(file)))
708         {
709                 if(parse_comments)
710                 {
711                         if(substring(s, 0, 2) == "//")
712                         {
713                                 if(substring(s, 2, 17) == "WAYPOINT_VERSION ")
714                                         ver = stof(substring(s, 19, -1));
715                                 continue;
716                         }
717                         else
718                         {
719                                 if(ver < WAYPOINT_VERSION)
720                                         return false;
721                                 parse_comments = false;
722                         }
723                 }
724
725                 tokens = tokenizebyseparator(s, "*");
726
727                 if (tokens!=2)
728                 {
729                         // bad file format
730                         fclose(file);
731                         return false;
732                 }
733
734                 wp_from_pos     = stov(argv(0));
735                 wp_to_pos       = stov(argv(1));
736
737                 // Search "from" waypoint
738                 if(!wp_from || wp_from.origin!=wp_from_pos)
739                 {
740                         wp_from = findradius(wp_from_pos, 1);
741                         found = false;
742                         while(wp_from)
743                         {
744                                 if(vdist(wp_from.origin - wp_from_pos, <, 1))
745                                 if(wp_from.classname == "waypoint")
746                                 {
747                                         found = true;
748                                         break;
749                                 }
750                                 wp_from = wp_from.chain;
751                         }
752
753                         if(!found)
754                         {
755                                 LOG_TRACE("waypoint_load_links: couldn't find 'from' waypoint at ", vtos(wp_from_pos));
756                                 continue;
757                         }
758
759                 }
760
761                 // Search "to" waypoint
762                 wp_to = findradius(wp_to_pos, 1);
763                 found = false;
764                 while(wp_to)
765                 {
766                         if(vdist(wp_to.origin - wp_to_pos, <, 1))
767                         if(wp_to.classname == "waypoint")
768                         {
769                                 found = true;
770                                 break;
771                         }
772                         wp_to = wp_to.chain;
773                 }
774
775                 if(!found)
776                 {
777                         LOG_TRACE("waypoint_load_links: couldn't find 'to' waypoint at ", vtos(wp_to_pos));
778                         continue;
779                 }
780
781                 ++c;
782                 waypoint_addlink(wp_from, wp_to);
783         }
784
785         fclose(file);
786
787         LOG_TRACE("loaded ", ftos(c), " waypoint links from maps/", mapname, ".waypoints.cache");
788
789         botframe_cachedwaypointlinks = true;
790         return true;
791 }
792
793 void waypoint_load_or_remove_links_hardwired(bool removal_mode)
794 {
795         string filename, s;
796         float file, tokens, c = 0, found;
797         entity wp_from = NULL, wp_to;
798         vector wp_to_pos, wp_from_pos;
799         filename = strcat("maps/", mapname);
800         filename = strcat(filename, ".waypoints.hardwired");
801         file = fopen(filename, FILE_READ);
802
803         botframe_loadedforcedlinks = true;
804
805         if (file < 0)
806         {
807                 if(!removal_mode)
808                         LOG_TRACE("waypoint links load from ", filename, " failed");
809                 return;
810         }
811
812         while ((s = fgets(file)))
813         {
814                 if(substring(s, 0, 2)=="//")
815                         continue;
816
817                 if(substring(s, 0, 1)=="#")
818                         continue;
819
820                 tokens = tokenizebyseparator(s, "*");
821
822                 if (tokens!=2)
823                         continue;
824
825                 wp_from_pos     = stov(argv(0));
826                 wp_to_pos       = stov(argv(1));
827
828                 // Search "from" waypoint
829                 if(!wp_from || wp_from.origin!=wp_from_pos)
830                 {
831                         wp_from = findradius(wp_from_pos, 5);
832                         found = false;
833                         while(wp_from)
834                         {
835                                 if(vdist(wp_from.origin - wp_from_pos, <, 5))
836                                 if(wp_from.classname == "waypoint")
837                                 {
838                                         found = true;
839                                         break;
840                                 }
841                                 wp_from = wp_from.chain;
842                         }
843
844                         if(!found)
845                         {
846                                 if(!removal_mode)
847                                         LOG_INFO("NOTICE: Can not find waypoint at ", vtos(wp_from_pos), ". Path skipped");
848                                 continue;
849                         }
850                 }
851
852                 // Search "to" waypoint
853                 wp_to = findradius(wp_to_pos, 5);
854                 found = false;
855                 while(wp_to)
856                 {
857                         if(vdist(wp_to.origin - wp_to_pos, <, 5))
858                         if(wp_to.classname == "waypoint")
859                         {
860                                 found = true;
861                                 break;
862                         }
863                         wp_to = wp_to.chain;
864                 }
865
866                 if(!found)
867                 {
868                         if(!removal_mode)
869                                 LOG_INFO("NOTICE: Can not find waypoint at ", vtos(wp_to_pos), ". Path skipped");
870                         continue;
871                 }
872
873                 ++c;
874                 if(removal_mode)
875                 {
876                         waypoint_removelink(wp_from, wp_to);
877                         continue;
878                 }
879
880                 waypoint_addlink(wp_from, wp_to);
881                 wp_from.wphardwired = true;
882                 wp_to.wphardwired = true;
883                 waypoint_setupmodel(wp_from);
884                 waypoint_setupmodel(wp_to);
885         }
886
887         fclose(file);
888
889         if(!removal_mode)
890                 LOG_TRACE("loaded ", ftos(c), " waypoint links from maps/", mapname, ".waypoints.hardwired");
891 }
892
893 entity waypoint_get_link(entity w, float i)
894 {
895         switch(i)
896         {
897                 case  0:return w.wp00;
898                 case  1:return w.wp01;
899                 case  2:return w.wp02;
900                 case  3:return w.wp03;
901                 case  4:return w.wp04;
902                 case  5:return w.wp05;
903                 case  6:return w.wp06;
904                 case  7:return w.wp07;
905                 case  8:return w.wp08;
906                 case  9:return w.wp09;
907                 case 10:return w.wp10;
908                 case 11:return w.wp11;
909                 case 12:return w.wp12;
910                 case 13:return w.wp13;
911                 case 14:return w.wp14;
912                 case 15:return w.wp15;
913                 case 16:return w.wp16;
914                 case 17:return w.wp17;
915                 case 18:return w.wp18;
916                 case 19:return w.wp19;
917                 case 20:return w.wp20;
918                 case 21:return w.wp21;
919                 case 22:return w.wp22;
920                 case 23:return w.wp23;
921                 case 24:return w.wp24;
922                 case 25:return w.wp25;
923                 case 26:return w.wp26;
924                 case 27:return w.wp27;
925                 case 28:return w.wp28;
926                 case 29:return w.wp29;
927                 case 30:return w.wp30;
928                 case 31:return w.wp31;
929                 default:return NULL;
930         }
931 }
932
933 // Save all waypoint links to a file
934 void waypoint_save_links()
935 {
936         // temporarily remove hardwired links so they don't get saved among normal links
937         waypoint_remove_links_hardwired();
938
939         string filename = sprintf("maps/%s.waypoints.cache", mapname);
940         int file = fopen(filename, FILE_WRITE);
941         if (file < 0)
942         {
943                 LOG_INFOF("waypoint link save to %s failed", filename);
944                 return;
945         }
946
947         fputs(file, strcat("//", "WAYPOINT_VERSION ", ftos_decimals(WAYPOINT_VERSION, 2), "\n"));
948
949         int c = 0;
950         IL_EACH(g_waypoints, true,
951         {
952                 for(int j = 0; j < 32; ++j)
953                 {
954                         entity link = waypoint_get_link(it, j);
955                         if(link)
956                         {
957                                 string s = strcat(vtos(it.origin), "*", vtos(link.origin), "\n");
958                                 fputs(file, s);
959                                 ++c;
960                         }
961                 }
962         });
963         fclose(file);
964         botframe_cachedwaypointlinks = true;
965
966         LOG_INFOF("saved %d waypoint links to maps/%s.waypoints.cache", c, mapname);
967
968         waypoint_load_links_hardwired();
969 }
970
971 // save waypoints to gamedir/data/maps/mapname.waypoints
972 void waypoint_saveall()
973 {
974         string filename = sprintf("maps/%s.waypoints", mapname);
975         int file = fopen(filename, FILE_WRITE);
976         if (file < 0)
977         {
978                 waypoint_save_links(); // save anyway?
979                 botframe_loadedforcedlinks = false;
980
981                 LOG_INFOF("waypoint links: save to %s failed", filename);
982                 return;
983         }
984
985         // add 3 comments to not break compatibility with older Xonotic versions
986         // (they are read as a waypoint with origin '0 0 0' and flag 0 though)
987         fputs(file, strcat("//", "WAYPOINT_VERSION ", ftos_decimals(WAYPOINT_VERSION, 2), "\n"));
988         fputs(file, strcat("//", "\n"));
989         fputs(file, strcat("//", "\n"));
990
991         int c = 0;
992         IL_EACH(g_waypoints, true,
993         {
994                 if(it.wpflags & WAYPOINTFLAG_GENERATED)
995                         continue;
996
997                 string s;
998                 s = strcat(vtos(it.origin + it.mins), "\n");
999                 s = strcat(s, vtos(it.origin + it.maxs));
1000                 s = strcat(s, "\n");
1001                 s = strcat(s, ftos(it.wpflags));
1002                 s = strcat(s, "\n");
1003                 fputs(file, s);
1004                 c++;
1005         });
1006         fclose(file);
1007         waypoint_save_links();
1008         botframe_loadedforcedlinks = false;
1009
1010         LOG_INFOF("saved %d waypoints to maps/%s.waypoints", c, mapname);
1011 }
1012
1013 // load waypoints from file
1014 float waypoint_loadall()
1015 {
1016         string filename, s;
1017         float file, cwp, cwb, fl;
1018         vector m1, m2;
1019         cwp = 0;
1020         cwb = 0;
1021         filename = strcat("maps/", mapname);
1022         filename = strcat(filename, ".waypoints");
1023         file = fopen(filename, FILE_READ);
1024
1025         bool parse_comments = true;
1026         float ver = 0;
1027
1028         if (file < 0)
1029         {
1030                 LOG_TRACE("waypoint load from ", filename, " failed");
1031                 return 0;
1032         }
1033
1034         while ((s = fgets(file)))
1035         {
1036                 if(parse_comments)
1037                 {
1038                         if(substring(s, 0, 2) == "//")
1039                         {
1040                                 if(substring(s, 2, 17) == "WAYPOINT_VERSION ")
1041                                         ver = stof(substring(s, 19, -1));
1042                                 continue;
1043                         }
1044                         else
1045                         {
1046                                 if(floor(ver) < floor(WAYPOINT_VERSION))
1047                                         LOG_TRACE("waypoints for this map are outdated");
1048                                 parse_comments = false;
1049                         }
1050                 }
1051                 m1 = stov(s);
1052                 s = fgets(file);
1053                 if (!s)
1054                         break;
1055                 m2 = stov(s);
1056                 s = fgets(file);
1057                 if (!s)
1058                         break;
1059                 fl = stof(s);
1060                 waypoint_spawn(m1, m2, fl);
1061                 if (m1 == m2)
1062                         cwp = cwp + 1;
1063                 else
1064                         cwb = cwb + 1;
1065         }
1066         fclose(file);
1067         LOG_TRACE("loaded ", ftos(cwp), " waypoints and ", ftos(cwb), " wayboxes from maps/", mapname, ".waypoints");
1068
1069         return cwp + cwb;
1070 }
1071
1072 #define waypoint_fixorigin(position, tracetest_ent) \
1073         waypoint_fixorigin_down_dir(position, tracetest_ent, '0 0 -1')
1074
1075 vector waypoint_fixorigin_down_dir(vector position, entity tracetest_ent, vector down_dir)
1076 {
1077         tracebox(position + '0 0 1', PL_MIN_CONST, PL_MAX_CONST, position + down_dir * 3000, MOVE_NOMONSTERS, tracetest_ent);
1078         if(trace_startsolid)
1079                 tracebox(position + '0 0 1' * (1 - PL_MIN_CONST.z / 2), PL_MIN_CONST, PL_MAX_CONST, position + down_dir * 3000, MOVE_NOMONSTERS, tracetest_ent);
1080         if(trace_startsolid)
1081                 tracebox(position + '0 0 1' * (1 - PL_MIN_CONST.z), PL_MIN_CONST, PL_MAX_CONST, position + down_dir * 3000, MOVE_NOMONSTERS, tracetest_ent);
1082         if(trace_fraction < 1)
1083                 position = trace_endpos;
1084         return position;
1085 }
1086
1087 void waypoint_spawnforitem_force(entity e, vector org)
1088 {
1089         // Fix the waypoint altitude if necessary
1090         org = waypoint_fixorigin(org, NULL);
1091
1092         // don't spawn an item spawnfunc_waypoint if it already exists
1093         IL_EACH(g_waypoints, true,
1094         {
1095                 if(it.wpisbox)
1096                 {
1097                         if(boxesoverlap(org, org, it.absmin, it.absmax))
1098                         {
1099                                 e.nearestwaypoint = it;
1100                                 return;
1101                         }
1102                 }
1103                 else
1104                 {
1105                         if(vdist(it.origin - org, <, 16))
1106                         {
1107                                 e.nearestwaypoint = it;
1108                                 return;
1109                         }
1110                 }
1111         });
1112
1113         e.nearestwaypoint = waypoint_spawn(org, org, WAYPOINTFLAG_GENERATED | WAYPOINTFLAG_ITEM);
1114 }
1115
1116 void waypoint_spawnforitem(entity e)
1117 {
1118         if(!bot_waypoints_for_items)
1119                 return;
1120
1121         waypoint_spawnforitem_force(e, e.origin);
1122 }
1123
1124 void waypoint_spawnforteleporter_boxes(entity e, int teleport_flag, vector org1, vector org2, vector destination1, vector destination2, float timetaken)
1125 {
1126         entity w;
1127         entity dw;
1128         w = waypoint_spawn(org1, org2, WAYPOINTFLAG_GENERATED | teleport_flag | WAYPOINTFLAG_NORELINK);
1129         dw = waypoint_spawn(destination1, destination2, WAYPOINTFLAG_GENERATED);
1130         // one way link to the destination
1131         w.wp00 = dw;
1132         w.wp00mincost = timetaken; // this is just for jump pads
1133         // the teleporter's nearest spawnfunc_waypoint is this one
1134         // (teleporters are not goals, so this is probably useless)
1135         e.nearestwaypoint = w;
1136         e.nearestwaypointtimeout = -1;
1137 }
1138
1139 void waypoint_spawnforteleporter_wz(entity e, vector org, vector destination, float timetaken, vector down_dir, entity tracetest_ent)
1140 {
1141         // warpzones with oblique warp plane rely on down_dir to snap waypoints
1142         // to the ground without leaving the warp plane
1143         // warpzones with horizontal warp plane (down_dir.x == -1) generate
1144         // destination waypoint snapped to the ground (leaving warpzone), source
1145         // waypoint in the center of the warp plane
1146         if(down_dir.x != -1)
1147                 org = waypoint_fixorigin_down_dir(org, tracetest_ent, down_dir);
1148         if(down_dir.x == -1)
1149                 down_dir = '0 0 -1';
1150         destination = waypoint_fixorigin_down_dir(destination, tracetest_ent, down_dir);
1151         waypoint_spawnforteleporter_boxes(e, WAYPOINTFLAG_TELEPORT, org, org, destination, destination, timetaken);
1152 }
1153
1154 void waypoint_spawnforteleporter(entity e, vector destination, float timetaken, entity tracetest_ent)
1155 {
1156         destination = waypoint_fixorigin(destination, tracetest_ent);
1157         waypoint_spawnforteleporter_boxes(e, WAYPOINTFLAG_TELEPORT, e.absmin - PL_MAX_CONST + '1 1 1', e.absmax - PL_MIN_CONST + '-1 -1 -1', destination, destination, timetaken);
1158 }
1159
1160 entity waypoint_spawnpersonal(entity this, vector position)
1161 {
1162         entity w;
1163
1164         // drop the waypoint to a proper location:
1165         //   first move it up by a player height
1166         //   then move it down to hit the floor with player bbox size
1167         position = waypoint_fixorigin(position, this);
1168
1169         w = waypoint_spawn(position, position, WAYPOINTFLAG_GENERATED | WAYPOINTFLAG_PERSONAL);
1170         w.nearestwaypoint = NULL;
1171         w.nearestwaypointtimeout = 0;
1172         w.owner = this;
1173
1174         waypoint_schedulerelink(w);
1175
1176         return w;
1177 }
1178
1179 void waypoint_showlink(entity wp1, entity wp2, int display_type)
1180 {
1181         if (!(wp1 && wp2))
1182                 return;
1183
1184         if (wp1.wphardwired && wp2.wphardwired)
1185                 te_beam(NULL, wp1.origin, wp2.origin);
1186         else if (display_type == 1)
1187                 te_lightning2(NULL, wp1.origin, wp2.origin);
1188 }
1189
1190 void waypoint_showlinks_to(entity wp, int display_type)
1191 {
1192         IL_EACH(g_waypoints, it != wp,
1193         {
1194                 if (waypoint_islinked(it, wp))
1195                         waypoint_showlink(it, wp, display_type);
1196         });
1197 }
1198
1199 void waypoint_showlinks_from(entity wp, int display_type)
1200 {
1201         waypoint_showlink(wp.wp00, wp, display_type); waypoint_showlink(wp.wp16, wp, display_type);
1202         waypoint_showlink(wp.wp01, wp, display_type); waypoint_showlink(wp.wp17, wp, display_type);
1203         waypoint_showlink(wp.wp02, wp, display_type); waypoint_showlink(wp.wp18, wp, display_type);
1204         waypoint_showlink(wp.wp03, wp, display_type); waypoint_showlink(wp.wp19, wp, display_type);
1205         waypoint_showlink(wp.wp04, wp, display_type); waypoint_showlink(wp.wp20, wp, display_type);
1206         waypoint_showlink(wp.wp05, wp, display_type); waypoint_showlink(wp.wp21, wp, display_type);
1207         waypoint_showlink(wp.wp06, wp, display_type); waypoint_showlink(wp.wp22, wp, display_type);
1208         waypoint_showlink(wp.wp07, wp, display_type); waypoint_showlink(wp.wp23, wp, display_type);
1209         waypoint_showlink(wp.wp08, wp, display_type); waypoint_showlink(wp.wp24, wp, display_type);
1210         waypoint_showlink(wp.wp09, wp, display_type); waypoint_showlink(wp.wp25, wp, display_type);
1211         waypoint_showlink(wp.wp10, wp, display_type); waypoint_showlink(wp.wp26, wp, display_type);
1212         waypoint_showlink(wp.wp11, wp, display_type); waypoint_showlink(wp.wp27, wp, display_type);
1213         waypoint_showlink(wp.wp12, wp, display_type); waypoint_showlink(wp.wp28, wp, display_type);
1214         waypoint_showlink(wp.wp13, wp, display_type); waypoint_showlink(wp.wp29, wp, display_type);
1215         waypoint_showlink(wp.wp14, wp, display_type); waypoint_showlink(wp.wp30, wp, display_type);
1216         waypoint_showlink(wp.wp15, wp, display_type); waypoint_showlink(wp.wp31, wp, display_type);
1217 }
1218
1219 void botframe_showwaypointlinks()
1220 {
1221         if (time < botframe_waypointeditorlightningtime)
1222                 return;
1223         botframe_waypointeditorlightningtime = time + 0.5;
1224         FOREACH_CLIENT(IS_PLAYER(it) && !it.isbot,
1225         {
1226                 int display_type = 0;
1227                 entity head = navigation_findnearestwaypoint(it, false);
1228                 if (IS_ONGROUND(it) || it.waterlevel > WATERLEVEL_NONE)
1229                         display_type = 1; // default
1230                 else if(head && (head.wphardwired))
1231                         display_type = 2; // only hardwired
1232
1233                 if (display_type)
1234                 {
1235                         //navigation_testtracewalk = true;
1236                         //print("currently selected WP is ", etos(head), "\n");
1237                         //navigation_testtracewalk = false;
1238                         if (head)
1239                         {
1240                                 te_lightning2(NULL, head.origin, it.origin);
1241                                 if(PHYS_INPUT_BUTTON_CROUCH(it))
1242                                         waypoint_showlinks_to(head, display_type);
1243                                 else
1244                                         waypoint_showlinks_from(head, display_type);
1245                         }
1246                 }
1247         });
1248 }
1249
1250 float botframe_autowaypoints_fixdown(vector v)
1251 {
1252         tracebox(v, PL_MIN_CONST, PL_MAX_CONST, v + '0 0 -64', MOVE_NOMONSTERS, NULL);
1253         if(trace_fraction >= 1)
1254                 return 0;
1255         return 1;
1256 }
1257
1258 float botframe_autowaypoints_createwp(vector v, entity p, .entity fld, float f)
1259 {
1260         IL_EACH(g_waypoints, boxesoverlap(v - '32 32 32', v + '32 32 32', it.absmin, it.absmax),
1261         {
1262                 // if a matching spawnfunc_waypoint already exists, don't add a duplicate
1263                 return 0;
1264         });
1265
1266         waypoint_schedulerelink(p.(fld) = waypoint_spawn(v, v, f));
1267         return 1;
1268 }
1269
1270 // return value:
1271 //    1 = WP created
1272 //    0 = no action needed
1273 //   -1 = temp fail, try from world too
1274 //   -2 = permanent fail, do not retry
1275 float botframe_autowaypoints_fix_from(entity p, float walkfromwp, entity wp, .entity fld)
1276 {
1277         // make it possible to go from p to wp, if we can
1278         // if wp is NULL, nearest is chosen
1279
1280         entity w;
1281         vector porg;
1282         float t, tmin, tmax;
1283         vector o;
1284         vector save;
1285
1286         if(!botframe_autowaypoints_fixdown(p.origin))
1287                 return -2;
1288         porg = trace_endpos;
1289
1290         if(wp)
1291         {
1292                 // if any WP w fulfills wp -> w -> porg and w is closer than wp, then switch from wp to w
1293
1294                 // if wp -> porg, then OK
1295                 float maxdist;
1296                 if(navigation_waypoint_will_link(wp.origin, porg, p, porg, 0, wp.origin, 0, walkfromwp, 1050))
1297                 {
1298                         // we may find a better one
1299                         maxdist = vlen(wp.origin - porg);
1300                 }
1301                 else
1302                 {
1303                         // accept any "good"
1304                         maxdist = 2100;
1305                 }
1306
1307                 float bestdist = maxdist;
1308                 IL_EACH(g_waypoints, it != wp && !(it.wpflags & WAYPOINTFLAG_NORELINK),
1309                 {
1310                         float d = vlen(wp.origin - it.origin) + vlen(it.origin - porg);
1311                         if(d < bestdist)
1312                         if(navigation_waypoint_will_link(wp.origin, it.origin, p, it.origin, 0, wp.origin, 0, walkfromwp, 1050))
1313                         if(navigation_waypoint_will_link(it.origin, porg, p, porg, 0, it.origin, 0, walkfromwp, 1050))
1314                         {
1315                                 bestdist = d;
1316                                 p.(fld) = it;
1317                         }
1318                 });
1319                 if(bestdist < maxdist)
1320                 {
1321                         LOG_INFO("update chain to new nearest WP ", etos(p.(fld)));
1322                         return 0;
1323                 }
1324
1325                 if(bestdist < 2100)
1326                 {
1327                         // we know maxdist < 2100
1328                         // so wp -> porg is still valid
1329                         // all is good
1330                         p.(fld) = wp;
1331                         return 0;
1332                 }
1333
1334                 // otherwise, no existing WP can fix our issues
1335         }
1336         else
1337         {
1338                 save = p.origin;
1339                 setorigin(p, porg);
1340                 w = navigation_findnearestwaypoint(p, walkfromwp);
1341                 setorigin(p, save);
1342                 if(w)
1343                 {
1344                         p.(fld) = w;
1345                         return 0;
1346                 }
1347         }
1348
1349         tmin = 0;
1350         tmax = 1;
1351         for (;;)
1352         {
1353                 if(tmax - tmin < 0.001)
1354                 {
1355                         // did not get a good candidate
1356                         return -1;
1357                 }
1358
1359                 t = (tmin + tmax) * 0.5;
1360                 o = antilag_takebackorigin(p, CS(p), time - t);
1361                 if(!botframe_autowaypoints_fixdown(o))
1362                         return -2;
1363                 o = trace_endpos;
1364
1365                 if(wp)
1366                 {
1367                         if(!navigation_waypoint_will_link(wp.origin, o, p, o, 0, wp.origin, 0, walkfromwp, 1050))
1368                         {
1369                                 // we cannot walk from wp.origin to o
1370                                 // get closer to tmax
1371                                 tmin = t;
1372                                 continue;
1373                         }
1374                 }
1375                 else
1376                 {
1377                         save = p.origin;
1378                         setorigin(p, o);
1379                         w = navigation_findnearestwaypoint(p, walkfromwp);
1380                         setorigin(p, save);
1381                         if(!w)
1382                         {
1383                                 // we cannot walk from any WP to o
1384                                 // get closer to tmax
1385                                 tmin = t;
1386                                 continue;
1387                         }
1388                 }
1389
1390                 // if we get here, o is valid regarding waypoints
1391                 // check if o is connected right to the player
1392                 // we break if it succeeds, as that means o is a good waypoint location
1393                 if(navigation_waypoint_will_link(o, porg, p, porg, 0, o, 0, walkfromwp, 1050))
1394                         break;
1395
1396                 // o is no good, we need to get closer to the player
1397                 tmax = t;
1398         }
1399
1400         LOG_INFO("spawning a waypoint for connecting to ", etos(wp));
1401         botframe_autowaypoints_createwp(o, p, fld, 0);
1402         return 1;
1403 }
1404
1405 // automatically create missing waypoints
1406 .entity botframe_autowaypoints_lastwp0, botframe_autowaypoints_lastwp1;
1407 void botframe_autowaypoints_fix(entity p, float walkfromwp, .entity fld)
1408 {
1409         float r = botframe_autowaypoints_fix_from(p, walkfromwp, p.(fld), fld);
1410         if(r != -1)
1411                 return;
1412         r = botframe_autowaypoints_fix_from(p, walkfromwp, NULL, fld);
1413         if(r != -1)
1414                 return;
1415
1416         LOG_INFO("emergency: got no good nearby WP to build a link from, starting a new chain");
1417         if(!botframe_autowaypoints_fixdown(p.origin))
1418                 return; // shouldn't happen, caught above
1419         botframe_autowaypoints_createwp(trace_endpos, p, fld, WAYPOINTFLAG_PROTECTED);
1420 }
1421
1422 void botframe_deleteuselesswaypoints()
1423 {
1424         IL_EACH(g_items, it.bot_pickup,
1425         {
1426                 // NOTE: this protects waypoints if they're the ONLY nearest
1427                 // waypoint. That's the intention.
1428                 navigation_findnearestwaypoint(it, false);  // Walk TO item.
1429                 navigation_findnearestwaypoint(it, true);  // Walk FROM item.
1430         });
1431         IL_EACH(g_waypoints, true,
1432         {
1433                 it.wpflags |= WAYPOINTFLAG_DEAD_END;
1434                 it.wpflags &= ~WAYPOINTFLAG_USEFUL;
1435                 // WP is useful if:
1436                 if (it.wpflags & WAYPOINTFLAG_ITEM)
1437                         it.wpflags |= WAYPOINTFLAG_USEFUL;
1438                 if (it.wpflags & WAYPOINTFLAG_TELEPORT)
1439                         it.wpflags |= WAYPOINTFLAG_USEFUL;
1440                 if (it.wpflags & WAYPOINTFLAG_LADDER)
1441                         it.wpflags |= WAYPOINTFLAG_USEFUL;
1442                 if (it.wpflags & WAYPOINTFLAG_PROTECTED)
1443                         it.wpflags |= WAYPOINTFLAG_USEFUL;
1444                 // b) WP is closest WP for an item/spawnpoint/other entity
1445                 //    This has been done above by protecting these WPs.
1446         });
1447         // c) There are w1, w, w2 so that w1 -> w, w -> w2 and not w1 -> w2.
1448         IL_EACH(g_waypoints, !(it.wpflags & WAYPOINTFLAG_PERSONAL),
1449         {
1450                 for (int m = 0; m < 32; ++m)
1451                 {
1452                         entity w = waypoint_get_link(it, m);
1453                         if (!w)
1454                                 break;
1455                         if (w.wpflags & WAYPOINTFLAG_PERSONAL)
1456                                 continue;
1457                         if (w.wpflags & WAYPOINTFLAG_USEFUL)
1458                                 continue;
1459                         for (int j = 0; j < 32; ++j)
1460                         {
1461                                 entity w2 = waypoint_get_link(w, j);
1462                                 if (!w2)
1463                                         break;
1464                                 if (it == w2)
1465                                         continue;
1466                                 if (w2.wpflags & WAYPOINTFLAG_PERSONAL)
1467                                         continue;
1468                                 // If we got here, it != w2 exist with it -> w
1469                                 // and w -> w2. That means the waypoint is not
1470                                 // a dead end.
1471                                 w.wpflags &= ~WAYPOINTFLAG_DEAD_END;
1472                                 for (int k = 0; k < 32; ++k)
1473                                 {
1474                                         if (waypoint_get_link(it, k) == w2)
1475                                                 continue;
1476                                         // IF WE GET HERE, w is proven useful
1477                                         // to get from it to w2!
1478                                         w.wpflags |= WAYPOINTFLAG_USEFUL;
1479                                         goto next;
1480                                 }
1481                         }
1482 LABEL(next)
1483                 }
1484         });
1485         // d) The waypoint is a dead end. Dead end waypoints must be kept as
1486         //     they are needed to complete routes while autowaypointing.
1487
1488         IL_EACH(g_waypoints, !(it.wpflags & (WAYPOINTFLAG_USEFUL | WAYPOINTFLAG_DEAD_END)),
1489         {
1490                 LOG_INFOF("Removed a waypoint at %v. Try again for more!", it.origin);
1491                 te_explosion(it.origin);
1492                 waypoint_remove(it);
1493                 break;
1494         });
1495
1496         IL_EACH(g_waypoints, true,
1497         {
1498                 it.wpflags &= ~(WAYPOINTFLAG_USEFUL | WAYPOINTFLAG_DEAD_END); // temp flag
1499         });
1500 }
1501
1502 void botframe_autowaypoints()
1503 {
1504         FOREACH_CLIENT(IS_PLAYER(it) && IS_REAL_CLIENT(it) && !IS_DEAD(it), {
1505                 // going back is broken, so only fix waypoints to walk TO the player
1506                 //botframe_autowaypoints_fix(p, false, botframe_autowaypoints_lastwp0);
1507                 botframe_autowaypoints_fix(it, true, botframe_autowaypoints_lastwp1);
1508                 //te_explosion(p.botframe_autowaypoints_lastwp0.origin);
1509         });
1510
1511         if (autocvar_g_waypointeditor_auto >= 2) {
1512                 botframe_deleteuselesswaypoints();
1513         }
1514 }
1515