2 float AI_STATUS_ROAMING = 1; // Bot is just crawling the map. No enemies at sight
3 float AI_STATUS_ATTACKING = 2; // There are enemies at sight
4 float AI_STATUS_RUNNING = 4; // Bot is bunny hopping
5 float AI_STATUS_DANGER_AHEAD = 8; // There is lava/slime/trigger_hurt ahead
6 float AI_STATUS_OUT_JUMPPAD = 16; // Trying to get out of a "vertical" jump pad
7 float AI_STATUS_OUT_WATER = 32; // Trying to get out of water
8 float AI_STATUS_WAYPOINT_PERSONAL_LINKING = 64; // Waiting for the personal waypoint to be linked
9 float AI_STATUS_WAYPOINT_PERSONAL_GOING = 128; // Going to a personal waypoint
10 float AI_STATUS_WAYPOINT_PERSONAL_REACHED = 256; // Personal waypoint reached
11 float AI_STATUS_JETPACK_FLYING = 512;
12 float AI_STATUS_JETPACK_LANDING = 1024;
14 // utilities for path debugging
15 #ifdef DEBUG_TRACEWALK
17 float DEBUG_NODE_SUCCESS = 1;
18 float DEBUG_NODE_WARNING = 2;
19 float DEBUG_NODE_FAIL = 3;
23 void debugresetnodes()
25 debuglastnode = '0 0 0';
28 void debugnode(vector node)
30 if not(self.classname=="player")
33 if(debuglastnode=='0 0 0')
39 te_lightning2(world, node, debuglastnode);
43 void debugnodestatus(vector position, float status)
49 case DEBUG_NODE_SUCCESS:
52 case DEBUG_NODE_WARNING:
62 te_customflash(position, 40, 2, color);
67 // rough simulation of walking from one point to another to test if a path
68 // can be traveled, used for waypoint linking and havocbot
71 float bot_navigation_movemode;
72 float navigation_testtracewalk;
73 float tracewalk(entity e, vector start, vector m1, vector m2, vector end, float movemode)
79 local float totaldist;
82 local float ignorehazards;
85 #ifdef DEBUG_TRACEWALK
93 dist = totaldist = vlen(move);
94 dir = normalize(move);
96 ignorehazards = FALSE;
98 // Analyze starting point
99 traceline(start, start, MOVE_NORMAL, e);
100 if (trace_dpstartcontents & (DPCONTENTS_SLIME | DPCONTENTS_LAVA))
101 ignorehazards = TRUE;
104 traceline( start, start + '0 0 -65536', MOVE_NORMAL, e);
105 if (trace_dpstartcontents & (DPCONTENTS_SLIME | DPCONTENTS_LAVA))
107 ignorehazards = TRUE;
111 tracebox(start, m1, m2, start, MOVE_NOMONSTERS, e);
112 if (trace_startsolid)
115 #ifdef DEBUG_TRACEWALK
116 debugnodestatus(start, DEBUG_NODE_FAIL);
118 //print("tracewalk: ", vtos(start), " is a bad start\n");
123 yaw = vectoyaw(move);
127 if (boxesoverlap(end, end, org + m1 + '-1 -1 -1', org + m2 + '1 1 1'))
130 #ifdef DEBUG_TRACEWALK
131 debugnodestatus(org, DEBUG_NODE_SUCCESS);
133 //print("tracewalk: ", vtos(start), " can reach ", vtos(end), "\n");
136 #ifdef DEBUG_TRACEWALK
144 dist = dist - stepdist;
145 traceline(org, org, MOVE_NORMAL, e);
148 if (trace_dpstartcontents & (DPCONTENTS_SLIME | DPCONTENTS_LAVA))
150 // hazards blocking path
151 #ifdef DEBUG_TRACEWALK
152 debugnodestatus(org, DEBUG_NODE_FAIL);
154 //print("tracewalk: ", vtos(start), " hits a hazard when trying to reach ", vtos(end), "\n");
158 if (trace_dpstartcontents & DPCONTENTS_LIQUIDSMASK)
160 move = normalize(end - org);
161 tracebox(org, m1, m2, org + move * stepdist, movemode, e);
163 #ifdef DEBUG_TRACEWALK
164 debugnode(trace_endpos);
167 if (trace_fraction < 1)
170 org = trace_endpos - normalize(org - trace_endpos) * stepdist;
171 for(; org_z < end_z + self.maxs_z; org_z += stepdist)
173 #ifdef DEBUG_TRACEWALK
176 if(pointcontents(org) == CONTENT_EMPTY)
180 if not (pointcontents(org + '0 0 1') == CONTENT_EMPTY)
182 #ifdef DEBUG_TRACEWALK
183 debugnodestatus(org, DEBUG_NODE_FAIL);
186 //print("tracewalk: ", vtos(start), " failed under water\n");
196 move = dir * stepdist + org;
197 tracebox(org, m1, m2, move, movemode, e);
199 #ifdef DEBUG_TRACEWALK
200 debugnode(trace_endpos);
204 if (trace_fraction < 1)
206 // check if we can walk over this obstacle
207 tracebox(org + stepheightvec, m1, m2, move + stepheightvec, movemode, e);
208 if (trace_fraction < 1 || trace_startsolid)
210 #ifdef DEBUG_TRACEWALK
211 debugnodestatus(trace_endpos, DEBUG_NODE_WARNING);
215 traceline( org, move, movemode, e);
216 if ( trace_ent.classname == "door_rotating" || trace_ent.classname == "door")
218 local vector nextmove;
220 while(trace_ent.classname == "door_rotating" || trace_ent.classname == "door")
222 nextmove = move + (dir * stepdist);
223 traceline( move, nextmove, movemode, e);
229 #ifdef DEBUG_TRACEWALK
230 debugnodestatus(trace_endpos, DEBUG_NODE_FAIL);
232 //print("tracewalk: ", vtos(start), " hit something when trying to reach ", vtos(end), "\n");
233 //te_explosion(trace_endpos);
234 //print(ftos(e.dphitcontentsmask), "\n");
235 return FALSE; // failed
244 // trace down from stepheight as far as possible and move there,
245 // if this starts in solid we try again without the stepup, and
246 // if that also fails we assume it is a wall
247 // (this is the same logic as the Quake walkmove function used)
248 tracebox(move, m1, m2, move + '0 0 -65536', movemode, e);
250 // moved successfully
254 c = pointcontents(org + '0 0 1');
255 if not(c == CONTENT_WATER || c == CONTENT_LAVA || c == CONTENT_SLIME)
265 //print("tracewalk: ", vtos(start), " did not arrive at ", vtos(end), " but at ", vtos(org), "\n");
267 // moved but didn't arrive at the intended destination
268 #ifdef DEBUG_TRACEWALK
269 debugnodestatus(org, DEBUG_NODE_FAIL);
276 // grenade tracing to decide the best pitch to fire at
279 entity tracetossfaketarget;
281 // traces multiple trajectories to find one that will impact the target
282 // 'end' vector is the place it aims for,
283 // returns TRUE only if it hit targ (don't target non-solid entities)
284 vector findtrajectory_velocity;
285 float findtrajectorywithleading(vector org, vector m1, vector m2, entity targ, float shotspeed, float shotspeedupward, float maxtime, float shotdelay, entity ignore)
287 local float c, savesolid, shottime;
288 local vector dir, end, v;
290 return FALSE; // could cause division by zero if calculated
291 if (targ.solid < SOLID_BBOX) // SOLID_NOT and SOLID_TRIGGER
292 return FALSE; // could never hit it
294 tracetossent = spawn();
295 tracetossent.owner = ignore;
296 setsize(tracetossent, m1, m2);
297 savesolid = targ.solid;
298 targ.solid = SOLID_NOT;
299 shottime = ((vlen(targ.origin - org) / shotspeed) + shotdelay);
300 v = targ.velocity * shottime + targ.origin;
301 tracebox(targ.origin, targ.mins, targ.maxs, v, FALSE, targ);
303 end = v + (targ.mins + targ.maxs) * 0.5;
304 if ((vlen(end - org) / shotspeed + 0.2) > maxtime)
307 targ.solid = savesolid;
311 if (!tracetossfaketarget)
312 tracetossfaketarget = spawn();
313 tracetossfaketarget.solid = savesolid;
314 tracetossfaketarget.movetype = targ.movetype;
315 setmodel(tracetossfaketarget, targ.model); // no low precision
316 tracetossfaketarget.model = targ.model;
317 tracetossfaketarget.modelindex = targ.modelindex;
318 setsize(tracetossfaketarget, targ.mins, targ.maxs);
319 setorigin(tracetossfaketarget, v);
322 dir = normalize(end - org);
323 while (c < 10) // 10 traces
325 setorigin(tracetossent, org); // reset
326 tracetossent.velocity = findtrajectory_velocity = normalize(dir) * shotspeed + shotspeedupward * '0 0 1';
327 tracetoss(tracetossent, ignore); // love builtin functions...
328 if (trace_ent == tracetossfaketarget) // done
330 targ.solid = savesolid;
333 tracetossfaketarget.solid = SOLID_NOT;
334 tracetossfaketarget.movetype = MOVETYPE_NONE;
335 tracetossfaketarget.model = "";
336 tracetossfaketarget.modelindex = 0;
337 // relink to remove it from physics considerations
338 setorigin(tracetossfaketarget, v);
342 dir_z = dir_z + 0.1; // aim up a little more
345 targ.solid = savesolid;
348 tracetossfaketarget.solid = SOLID_NOT;
349 tracetossfaketarget.movetype = MOVETYPE_NONE;
350 tracetossfaketarget.model = "";
351 tracetossfaketarget.modelindex = 0;
352 // relink to remove it from physics considerations
353 setorigin(tracetossfaketarget, v);
355 // leave a valid one even if it won't reach
356 findtrajectory_velocity = normalize(end - org) * shotspeed + shotspeedupward * '0 0 1';
363 #define LAG_QUEUE_LENGTH 4
365 .void(float t, float f1, float f2, entity e1, vector v1, vector v2, vector v3, vector v4) lag_func;
367 .float lag_time[LAG_QUEUE_LENGTH];
368 .float lag_float1[LAG_QUEUE_LENGTH];
369 .float lag_float2[LAG_QUEUE_LENGTH];
370 .vector lag_vec1[LAG_QUEUE_LENGTH];
371 .vector lag_vec2[LAG_QUEUE_LENGTH];
372 .vector lag_vec3[LAG_QUEUE_LENGTH];
373 .vector lag_vec4[LAG_QUEUE_LENGTH];
374 .entity lag_entity1[LAG_QUEUE_LENGTH];
379 for(i=0;i<LAG_QUEUE_LENGTH;++i)
381 if (self.lag_time[i])
382 if (time > self.lag_time[i])
385 self.lag_time[i], self.lag_float1[i], self.lag_float2[i],
386 self.lag_entity1[i], self.lag_vec1[i], self.lag_vec2[i], self.lag_vec3[i],
389 // Clear this position on the queue
390 self.(lag_time[i]) = 0;
395 float lag_additem(float t, float f1, float f2, entity e1, vector v1, vector v2, vector v3, vector v4)
398 for(i=0;i<LAG_QUEUE_LENGTH;++i)
400 // Find a free position on the queue
401 if (self.lag_time[i] == 0)
403 self.(lag_time[i]) = t;
404 self.(lag_vec1[i]) = v1;
405 self.(lag_vec2[i]) = v2;
406 self.(lag_vec3[i]) = v3;
407 self.(lag_vec4[i]) = v4;
408 self.(lag_float1[i]) = f1;
409 self.(lag_float2[i]) = f2;
410 self.(lag_entity1[i]) = e1;
418 // Random skill system
419 .float bot_thinkskill;
420 .float bot_mouseskill;
421 .float bot_predictionskill;
422 .float bot_offsetskill;
425 // spawnfunc_waypoint navigation system
427 // itemscore = (howmuchmoreIwant / howmuchIcanwant) / itemdistance
428 // waypointscore = 0.7 / waypointdistance
430 #define MAX_WAYPOINTS_LINKS 32
431 .vector wpnearestpoint;
432 .entity wlink[MAX_WAYPOINTS_LINKS];
433 .float wpmincost[MAX_WAYPOINTS_LINKS];
434 .float wpfire, wpcost, wpconsidered, wpisbox, wpflags, wplinked;
436 // stack of current goals (the last one of which may be an item or other
437 // desirable object, the rest are typically waypoints to reach it)
438 .entity goalcurrent, goalstack01, goalstack02, goalstack03;
439 .entity goalstack04, goalstack05, goalstack06, goalstack07;
440 .entity goalstack08, goalstack09, goalstack10, goalstack11;
441 .entity goalstack12, goalstack13, goalstack14, goalstack15;
442 .entity goalstack16, goalstack17, goalstack18, goalstack19;
443 .entity goalstack20, goalstack21, goalstack22, goalstack23;
444 .entity goalstack24, goalstack25, goalstack26, goalstack27;
445 .entity goalstack28, goalstack29, goalstack30, goalstack31;
447 .entity nearestwaypoint;
448 .float nearestwaypointtimeout;
450 // used during navigation_goalrating_begin/end sessions
451 #define MAX_BESTGOALS 3
452 float bestgoalswindex;
453 float bestgoalsrindex;
454 float navigation_bestrating;
455 entity navigation_bestgoals[MAX_BESTGOALS];
456 .float navigation_hasgoals;
458 /////////////////////////////////////////////////////////////////////////////
459 // spawnfunc_waypoint management
460 /////////////////////////////////////////////////////////////////////////////
462 // waypoints with this flag are not saved, they are automatically generated
463 // waypoints like jump pads, teleporters, and items
464 float WAYPOINTFLAG_GENERATED = 8388608;
465 float WAYPOINTFLAG_ITEM = 4194304;
466 float WAYPOINTFLAG_TELEPORT = 2097152;
467 float WAYPOINTFLAG_NORELINK = 1048576;
468 float WAYPOINTFLAG_PERSONAL = 524288;
470 // add a new link to the spawnfunc_waypoint, replacing the furthest link it already has
471 void waypoint_addlink(entity from, entity to)
478 if (from.wpflags & WAYPOINTFLAG_NORELINK)
481 // return if already linked
482 for(i=0;i<MAX_WAYPOINTS_LINKS;++i)
484 if(from.wlink[i]==to)
488 // if either is a box we have to find the nearest points on them to
489 // calculate the distance properly
490 if (to.wpisbox || from.wpisbox)
492 local vector v1, v2, m1, m2;
496 v1_x = bound(m1_x, v1_x, m2_x);
497 v1_y = bound(m1_y, v1_y, m2_y);
498 v1_z = bound(m1_z, v1_z, m2_z);
502 v2_x = bound(m1_x, v2_x, m2_x);
503 v2_y = bound(m1_y, v2_y, m2_y);
504 v2_z = bound(m1_z, v2_z, m2_z);
509 c = vlen(to.origin - from.origin);
511 if (from.wpmincost[MAX_WAYPOINTS_LINKS - 1] < c)
514 for(i=MAX_WAYPOINTS_LINKS - 2;i>0;--i)
516 if (from.wpmincost[i] < c)
518 from.(wlink[i]) = to;
519 from.(wpmincost[i]) = c;
522 from.(wlink[i]) = from.wlink[i-1];
523 from.(wpmincost[i]) = from.wpmincost[i-1];
525 from.(wlink[0]) = to;
526 from.(wpmincost[0]) = c;
530 // fields you can query using prvm_global server to get some statistics about waypoint linking culling
531 float relink_total, relink_walkculled, relink_pvsculled, relink_lengthculled;
533 // relink this spawnfunc_waypoint
534 // (precompile a list of all reachable waypoints from this spawnfunc_waypoint)
536 void waypoint_think()
539 local vector sv, sm1, sm2, ev, em1, em2, dv;
541 stepheightvec = cvar("sv_stepheight") * '0 0 1';
542 bot_navigation_movemode = ((cvar("bot_navigation_ignoreplayers")) ? MOVE_NOMONSTERS : MOVE_NORMAL);
544 //dprint("waypoint_think wpisbox = ", ftos(self.wpisbox), "\n");
545 sm1 = self.origin + self.mins;
546 sm2 = self.origin + self.maxs;
547 for(e = world; (e = find(e, classname, "waypoint")); )
549 if (boxesoverlap(self.absmin, self.absmax, e.absmin, e.absmax))
551 waypoint_addlink(self, e);
552 waypoint_addlink(e, self);
557 if(!checkpvs(self.origin, e))
563 sv_x = bound(sm1_x, sv_x, sm2_x);
564 sv_y = bound(sm1_y, sv_y, sm2_y);
565 sv_z = bound(sm1_z, sv_z, sm2_z);
567 em1 = e.origin + e.mins;
568 em2 = e.origin + e.maxs;
569 ev_x = bound(em1_x, ev_x, em2_x);
570 ev_y = bound(em1_y, ev_y, em2_y);
571 ev_z = bound(em1_z, ev_z, em2_z);
574 if (vlen(dv) >= 1050) // max search distance in XY
576 ++relink_lengthculled;
579 navigation_testtracewalk = 0;
582 tracebox(sv - PL_MIN_z * '0 0 1', PL_MIN, PL_MAX, sv, FALSE, self);
583 if (!trace_startsolid)
585 //dprint("sv deviation", vtos(trace_endpos - sv), "\n");
586 sv = trace_endpos + '0 0 1';
591 tracebox(ev - PL_MIN_z * '0 0 1', PL_MIN, PL_MAX, ev, FALSE, e);
592 if (!trace_startsolid)
594 //dprint("ev deviation", vtos(trace_endpos - ev), "\n");
595 ev = trace_endpos + '0 0 1';
598 //traceline(self.origin, e.origin, FALSE, world);
599 //if (trace_fraction == 1)
600 if (!self.wpisbox && tracewalk(self, sv, PL_MIN, PL_MAX, ev, MOVE_NOMONSTERS))
601 waypoint_addlink(self, e);
603 relink_walkculled += 0.5;
604 if (!e.wpisbox && tracewalk(e, ev, PL_MIN, PL_MAX, sv, MOVE_NOMONSTERS))
605 waypoint_addlink(e, self);
607 relink_walkculled += 0.5;
610 navigation_testtracewalk = 0;
611 self.wplinked = TRUE;
614 void waypoint_clearlinks(entity w)
616 // clear links to other waypoints
620 for(i=0;i<MAX_WAYPOINTS_LINKS;++i)
622 w.(wlink[i]) = world;
623 w.(wpmincost[i]) = f;
628 // tell a spawnfunc_waypoint to relink
629 void waypoint_schedulerelink(entity w)
634 // TODO: add some sort of visible box in edit mode for box waypoints
635 if (cvar("g_waypointeditor"))
640 setmodel(w, "models/runematch/rune.mdl");
641 w.effects = EF_LOWPRECISION;
643 if (w.wpflags & WAYPOINTFLAG_ITEM)
644 w.colormod = '1 0 0';
645 else if (w.wpflags & WAYPOINTFLAG_GENERATED)
646 w.colormod = '1 1 0';
648 w.colormod = '1 1 1';
652 w.wpisbox = vlen(w.size) > 0;
654 if (!(w.wpflags & WAYPOINTFLAG_PERSONAL))
656 if (!(w.wpflags & WAYPOINTFLAG_NORELINK))
657 waypoint_clearlinks(w);
658 // schedule an actual relink on next frame
659 w.think = waypoint_think;
661 w.effects = EF_LOWPRECISION;
664 // create a new spawnfunc_waypoint and automatically link it to other waypoints, and link
665 // them back to it as well
666 // (suitable for spawnfunc_waypoint editor)
667 entity waypoint_spawn(vector m1, vector m2, float f)
671 w = find(world, classname, "waypoint");
673 if not(f & WAYPOINTFLAG_PERSONAL)
676 // if a matching spawnfunc_waypoint already exists, don't add a duplicate
677 if (boxesoverlap(m1, m2, w.absmin, w.absmax))
679 w = find(w, classname, "waypoint");
683 w.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_PLAYERCLIP;
684 w.classname = "waypoint";
686 setorigin(w, (m1 + m2) * 0.5);
687 setsize(w, m1 - w.origin, m2 - w.origin);
688 if (vlen(w.size) > 0)
691 if(!(f & WAYPOINTFLAG_GENERATED))
694 traceline(w.origin + '0 0 1', w.origin + PL_MIN_z * '0 0 1' - '0 0 1', MOVE_NOMONSTERS, w);
695 if (trace_fraction < 1)
696 setorigin(w, trace_endpos - PL_MIN_z * '0 0 1');
698 // check if the start position is stuck
699 tracebox(w.origin, PL_MIN + '-1 -1 -1', PL_MAX + '1 1 1', w.origin, MOVE_NOMONSTERS, w);
700 if (trace_startsolid)
702 org = w.origin + '0 0 26';
703 tracebox(org, PL_MIN, PL_MAX, w.origin, MOVE_WORLDONLY, w);
706 org = w.origin + '2 2 2';
707 tracebox(org, PL_MIN, PL_MAX, w.origin, MOVE_WORLDONLY, w);
710 org = w.origin + '-2 -2 2';
711 tracebox(org, PL_MIN, PL_MAX, w.origin, MOVE_WORLDONLY, w);
714 org = w.origin + '-2 2 2';
715 tracebox(org, PL_MIN, PL_MAX, w.origin, MOVE_WORLDONLY, w);
718 org = w.origin + '2 -2 2';
719 tracebox(org, PL_MIN, PL_MAX, w.origin, MOVE_WORLDONLY, w);
722 // this WP is in solid, refuse it
723 dprint("Killed a waypoint that was stuck in solid at ", vtos(org), "\n");
731 setorigin(w, org * 0.05 + trace_endpos * 0.95); // don't trust the trace fully
734 tracebox(w.origin, PL_MIN, PL_MAX, w.origin - '0 0 128', MOVE_WORLDONLY, w);
737 dprint("Killed a waypoint that was stuck in solid ", vtos(w.origin), "\n");
743 if(trace_fraction == 1)
745 dprint("Killed a waypoint that was stuck in air at ", vtos(w.origin), "\n");
749 trace_endpos_z += 0.1; // don't trust the trace fully
750 // dprint("Moved waypoint at ", vtos(w.origin), " by ", ftos(vlen(w.origin - trace_endpos)));
751 // dprint(" direction: ", vtos((trace_endpos - w.origin)), "\n");
752 setorigin(w, trace_endpos);
756 waypoint_clearlinks(w);
757 //waypoint_schedulerelink(w);
759 if (cvar("g_waypointeditor"))
763 setmodel(w, "models/runematch/rune.mdl"); w.effects = EF_LOWPRECISION;
765 if (w.wpflags & WAYPOINTFLAG_ITEM)
766 w.colormod = '1 0 0';
767 else if (w.wpflags & WAYPOINTFLAG_GENERATED)
768 w.colormod = '1 1 0';
770 w.colormod = '1 1 1';
778 // spawnfunc_waypoint map entity
779 void spawnfunc_waypoint()
781 setorigin(self, self.origin);
782 // schedule a relink after other waypoints have had a chance to spawn
783 waypoint_clearlinks(self);
784 //waypoint_schedulerelink(self);
787 // remove a spawnfunc_waypoint, and schedule all neighbors to relink
788 void waypoint_remove(entity e)
792 // tell all linked waypoints that they need to relink
793 for(i=0;i<MAX_WAYPOINTS_LINKS;++i)
796 waypoint_schedulerelink(e.wlink[i]);
799 // and now remove the spawnfunc_waypoint
803 // empties the map of waypoints
804 void waypoint_removeall()
806 local entity head, next;
807 head = findchain(classname, "waypoint");
816 // tell all waypoints to relink
817 // (is this useful at all?)
818 void waypoint_schedulerelinkall()
821 relink_total = relink_walkculled = relink_pvsculled = relink_lengthculled = 0;
822 head = findchain(classname, "waypoint");
825 waypoint_schedulerelink(head);
830 // Load waypoint links from file
831 float botframe_cachedwaypointlinks;
832 float waypoint_load_links()
834 local string filename, s;
835 local float file, tokens, c, found;
836 local entity wp_from, wp_to;
837 local vector wp_to_pos, wp_from_pos;
838 filename = strcat("maps/", mapname);
839 filename = strcat(filename, ".waypoints.cache");
840 file = fopen(filename, FILE_READ);
844 dprint("waypoint links load from ");
856 tokens = tokenizebyseparator(s, "*");
865 wp_from_pos = stov(argv(0));
866 wp_to_pos = stov(argv(1));
868 // Search "from" waypoint
869 if(wp_from.origin!=wp_from_pos)
871 wp_from = findradius(wp_from_pos, 1);
875 if(vlen(wp_from.origin-wp_from_pos)<1)
876 if(wp_from.classname == "waypoint")
881 wp_from = wp_from.chain;
886 // can't find that waypoint
892 // Search "to" waypoint
893 wp_to = findradius(wp_to_pos, 1);
897 if(vlen(wp_to.origin-wp_to_pos)<1)
898 if(wp_to.classname == "waypoint")
908 // can't find that waypoint
914 waypoint_addlink(wp_from, wp_to);
921 dprint(" waypoint links from maps/");
923 dprint(".waypoints.cache\n");
925 botframe_cachedwaypointlinks = TRUE;
929 float botframe_loadedforcedlinks;
930 void waypoint_load_links_hardwired()
932 local string filename, s;
933 local float file, tokens, c, found;
934 local entity wp_from, wp_to;
935 local vector wp_to_pos, wp_from_pos;
936 filename = strcat("maps/", mapname);
937 filename = strcat(filename, ".waypoints.hardwired");
938 file = fopen(filename, FILE_READ);
940 botframe_loadedforcedlinks = TRUE;
944 dprint("waypoint links load from ");
956 if(substring(s, 0, 2)=="//")
959 if(substring(s, 0, 1)=="#")
962 tokens = tokenizebyseparator(s, "*");
967 wp_from_pos = stov(argv(0));
968 wp_to_pos = stov(argv(1));
970 // Search "from" waypoint
971 if(wp_from.origin!=wp_from_pos)
973 wp_from = findradius(wp_from_pos, 1);
977 if(vlen(wp_from.origin-wp_from_pos)<1)
978 if(wp_from.classname == "waypoint")
983 wp_from = wp_from.chain;
988 print(strcat("NOTICE: Can not find waypoint at ", vtos(wp_from_pos), ". Path skipped\n"));
993 // Search "to" waypoint
994 wp_to = findradius(wp_to_pos, 1);
998 if(vlen(wp_to.origin-wp_to_pos)<1)
999 if(wp_to.classname == "waypoint")
1004 wp_to = wp_to.chain;
1009 print(strcat("NOTICE: Can not find waypoint at ", vtos(wp_to_pos), ". Path skipped\n"));
1014 waypoint_addlink(wp_from, wp_to);
1021 dprint(" waypoint links from maps/");
1023 dprint(".waypoints.hardwired\n");
1027 // Save all waypoint links to a file
1028 void waypoint_save_links()
1030 local string filename, s;
1031 local float file, c, i;
1032 local entity w, link;
1033 filename = strcat("maps/", mapname);
1034 filename = strcat(filename, ".waypoints.cache");
1035 file = fopen(filename, FILE_WRITE);
1038 print("waypoint links save to ");
1043 w = findchain(classname, "waypoint");
1046 for(i=0;i<MAX_WAYPOINTS_LINKS;++i)
1053 s = strcat(vtos(w.origin), "*", vtos(link.origin), "\n");
1060 botframe_cachedwaypointlinks = TRUE;
1064 print(" waypoints links to maps/");
1066 print(".waypoints.cache\n");
1069 // save waypoints to gamedir/data/maps/mapname.waypoints
1070 void waypoint_saveall()
1072 local string filename, s;
1073 local float file, c;
1075 filename = strcat("maps/", mapname);
1076 filename = strcat(filename, ".waypoints");
1077 file = fopen(filename, FILE_WRITE);
1081 w = findchain(classname, "waypoint");
1084 if (!(w.wpflags & WAYPOINTFLAG_GENERATED))
1086 s = strcat(vtos(w.origin + w.mins), "\n");
1087 s = strcat(s, vtos(w.origin + w.maxs));
1088 s = strcat(s, "\n");
1089 s = strcat(s, ftos(w.wpflags));
1090 s = strcat(s, "\n");
1099 bprint(" waypoints to maps/");
1101 bprint(".waypoints\n");
1105 bprint("waypoint save to ");
1107 bprint(" failed\n");
1109 waypoint_save_links();
1110 botframe_loadedforcedlinks = FALSE;
1113 // load waypoints from file
1114 float waypoint_loadall()
1116 local string filename, s;
1117 local float file, cwp, cwb, fl;
1118 local vector m1, m2;
1121 filename = strcat("maps/", mapname);
1122 filename = strcat(filename, ".waypoints");
1123 file = fopen(filename, FILE_READ);
1140 waypoint_spawn(m1, m2, fl);
1149 dprint(" waypoints and ");
1151 dprint(" wayboxes from maps/");
1153 dprint(".waypoints\n");
1157 dprint("waypoint load from ");
1159 dprint(" failed\n");
1164 void waypoint_spawnforitem(entity e)
1169 if(!bot_waypoints_for_items)
1173 org = (e.absmax + e.absmin) * 0.5;
1175 // Fix the waypoint altitude if necessary
1176 traceline(org, org + '0 0 -65535', TRUE, e);
1178 org_z - trace_endpos_z > PL_MAX_z - PL_MIN_z + 10 // If middle of entiy is above player heigth
1179 || org_z - trace_endpos_z < (PL_MAX_z - PL_MIN_z) * 0.5 // or below half player height
1181 org_z = trace_endpos_z + PL_MAX_z - PL_MIN_z;
1183 // TODO: Cleaner solution
1184 if(e.classname!="item_flag_team")
1185 e.nearestwaypointtimeout = time + 1000000000;
1187 // don't spawn an item spawnfunc_waypoint if it already exists
1188 w = findchain(classname, "waypoint");
1193 if (boxesoverlap(org, org, w.absmin, w.absmax))
1195 e.nearestwaypoint = w;
1201 if (vlen(w.origin - org) < 16)
1203 e.nearestwaypoint = w;
1209 e.nearestwaypoint = waypoint_spawn(org, org, WAYPOINTFLAG_GENERATED | WAYPOINTFLAG_ITEM);
1212 void waypoint_spawnforteleporter(entity e, vector destination, float timetaken)
1216 w = waypoint_spawn(e.absmin, e.absmax, WAYPOINTFLAG_GENERATED | WAYPOINTFLAG_TELEPORT | WAYPOINTFLAG_NORELINK);
1217 dw = waypoint_spawn(destination, destination, WAYPOINTFLAG_GENERATED);
1218 // one way link to the destination
1220 w.(wpmincost[0]) = timetaken; // this is just for jump pads
1221 // the teleporter's nearest spawnfunc_waypoint is this one
1222 // (teleporters are not goals, so this is probably useless)
1223 e.nearestwaypoint = w;
1224 e.nearestwaypointtimeout = time + 1000000000;
1227 entity waypoint_spawnpersonal(vector position)
1231 // drop the waypoint to a proper location:
1232 // first move it up by a player height
1233 // then move it down to hit the floor with player bbox size
1234 traceline(position, position + '0 0 1' * (PL_MAX_z - PL_MIN_z), MOVE_NOMONSTERS, world);
1235 tracebox(trace_endpos, PL_MIN, PL_MAX, trace_endpos + '0 0 -1024', MOVE_NOMONSTERS, world);
1236 if(trace_fraction < 1)
1237 position = trace_endpos;
1239 w = waypoint_spawn(position, position, WAYPOINTFLAG_GENERATED | WAYPOINTFLAG_PERSONAL);
1240 w.nearestwaypoint = world;
1241 w.nearestwaypointtimeout = 0;
1244 waypoint_schedulerelink(w);
1249 /////////////////////////////////////////////////////////////////////////////
1251 /////////////////////////////////////////////////////////////////////////////
1253 // completely empty the goal stack, used when deciding where to go
1254 void navigation_clearroute()
1256 //print("bot ", etos(self), " clear\n");
1257 self.navigation_hasgoals = FALSE;
1258 self.goalcurrent = world;
1259 self.goalstack01 = world;
1260 self.goalstack02 = world;
1261 self.goalstack03 = world;
1262 self.goalstack04 = world;
1263 self.goalstack05 = world;
1264 self.goalstack06 = world;
1265 self.goalstack07 = world;
1266 self.goalstack08 = world;
1267 self.goalstack09 = world;
1268 self.goalstack10 = world;
1269 self.goalstack11 = world;
1270 self.goalstack12 = world;
1271 self.goalstack13 = world;
1272 self.goalstack14 = world;
1273 self.goalstack15 = world;
1274 self.goalstack16 = world;
1275 self.goalstack17 = world;
1276 self.goalstack18 = world;
1277 self.goalstack19 = world;
1278 self.goalstack20 = world;
1279 self.goalstack21 = world;
1280 self.goalstack22 = world;
1281 self.goalstack23 = world;
1282 self.goalstack24 = world;
1283 self.goalstack25 = world;
1284 self.goalstack26 = world;
1285 self.goalstack27 = world;
1286 self.goalstack28 = world;
1287 self.goalstack29 = world;
1288 self.goalstack30 = world;
1289 self.goalstack31 = world;
1292 // add a new goal at the beginning of the stack
1293 // (in other words: add a new prerequisite before going to the later goals)
1294 void navigation_pushroute(entity e)
1296 //print("bot ", etos(self), " push ", etos(e), "\n");
1297 self.goalstack31 = self.goalstack30;
1298 self.goalstack30 = self.goalstack29;
1299 self.goalstack29 = self.goalstack28;
1300 self.goalstack28 = self.goalstack27;
1301 self.goalstack27 = self.goalstack26;
1302 self.goalstack26 = self.goalstack25;
1303 self.goalstack25 = self.goalstack24;
1304 self.goalstack24 = self.goalstack23;
1305 self.goalstack23 = self.goalstack22;
1306 self.goalstack22 = self.goalstack21;
1307 self.goalstack21 = self.goalstack20;
1308 self.goalstack20 = self.goalstack19;
1309 self.goalstack19 = self.goalstack18;
1310 self.goalstack18 = self.goalstack17;
1311 self.goalstack17 = self.goalstack16;
1312 self.goalstack16 = self.goalstack15;
1313 self.goalstack15 = self.goalstack14;
1314 self.goalstack14 = self.goalstack13;
1315 self.goalstack13 = self.goalstack12;
1316 self.goalstack12 = self.goalstack11;
1317 self.goalstack11 = self.goalstack10;
1318 self.goalstack10 = self.goalstack09;
1319 self.goalstack09 = self.goalstack08;
1320 self.goalstack08 = self.goalstack07;
1321 self.goalstack07 = self.goalstack06;
1322 self.goalstack06 = self.goalstack05;
1323 self.goalstack05 = self.goalstack04;
1324 self.goalstack04 = self.goalstack03;
1325 self.goalstack03 = self.goalstack02;
1326 self.goalstack02 = self.goalstack01;
1327 self.goalstack01 = self.goalcurrent;
1328 self.goalcurrent = e;
1331 // remove first goal from stack
1332 // (in other words: remove a prerequisite for reaching the later goals)
1333 // (used when a spawnfunc_waypoint is reached)
1334 void navigation_poproute()
1336 //print("bot ", etos(self), " pop\n");
1337 self.goalcurrent = self.goalstack01;
1338 self.goalstack01 = self.goalstack02;
1339 self.goalstack02 = self.goalstack03;
1340 self.goalstack03 = self.goalstack04;
1341 self.goalstack04 = self.goalstack05;
1342 self.goalstack05 = self.goalstack06;
1343 self.goalstack06 = self.goalstack07;
1344 self.goalstack07 = self.goalstack08;
1345 self.goalstack08 = self.goalstack09;
1346 self.goalstack09 = self.goalstack10;
1347 self.goalstack10 = self.goalstack11;
1348 self.goalstack11 = self.goalstack12;
1349 self.goalstack12 = self.goalstack13;
1350 self.goalstack13 = self.goalstack14;
1351 self.goalstack14 = self.goalstack15;
1352 self.goalstack15 = self.goalstack16;
1353 self.goalstack16 = self.goalstack17;
1354 self.goalstack17 = self.goalstack18;
1355 self.goalstack18 = self.goalstack19;
1356 self.goalstack19 = self.goalstack20;
1357 self.goalstack20 = self.goalstack21;
1358 self.goalstack21 = self.goalstack22;
1359 self.goalstack22 = self.goalstack23;
1360 self.goalstack23 = self.goalstack24;
1361 self.goalstack24 = self.goalstack25;
1362 self.goalstack25 = self.goalstack26;
1363 self.goalstack26 = self.goalstack27;
1364 self.goalstack27 = self.goalstack28;
1365 self.goalstack28 = self.goalstack29;
1366 self.goalstack29 = self.goalstack30;
1367 self.goalstack30 = self.goalstack31;
1368 self.goalstack31 = world;
1371 // find the spawnfunc_waypoint near a dynamic goal such as a dropped weapon
1372 entity navigation_findnearestwaypoint(entity ent, float walkfromwp)
1374 local entity waylist, w, best;
1375 local float dist, bestdist;
1376 local vector v, org, pm1, pm2;
1377 pm1 = ent.origin + PL_MIN;
1378 pm2 = ent.origin + PL_MAX;
1379 waylist = findchain(classname, "waypoint");
1381 // do two scans, because box test is cheaper
1385 // if object is touching spawnfunc_waypoint
1387 if (boxesoverlap(pm1, pm2, w.absmin, w.absmax))
1392 org = ent.origin + (ent.mins_z - PL_MIN_z) * '0 0 1';
1394 org = org + ent.tag_entity.origin;
1395 if (navigation_testtracewalk)
1401 // box check failed, try walk
1405 // if object can walk from spawnfunc_waypoint
1410 local vector wm1, wm2;
1411 wm1 = w.origin + w.mins;
1412 wm2 = w.origin + w.maxs;
1413 v_x = bound(wm1_x, org_x, wm2_x);
1414 v_y = bound(wm1_y, org_y, wm2_y);
1415 v_z = bound(wm1_z, org_z, wm2_z);
1419 dist = vlen(v - org);
1420 if (bestdist > dist)
1422 traceline(v, org, TRUE, ent);
1423 if (trace_fraction == 1)
1427 //print("^1can I reach ", vtos(org), " from ", vtos(v), "?\n");
1428 if (tracewalk(ent, v, PL_MIN, PL_MAX, org, bot_navigation_movemode))
1436 if (tracewalk(ent, org, PL_MIN, PL_MAX, v, bot_navigation_movemode))
1450 // finds the waypoints near the bot initiating a navigation query
1451 float navigation_markroutes_nearestwaypoints(entity waylist, float maxdist)
1454 local vector v, m1, m2, diff;
1456 // navigation_testtracewalk = TRUE;
1461 if (!head.wpconsidered)
1465 m1 = head.origin + head.mins;
1466 m2 = head.origin + head.maxs;
1468 v_x = bound(m1_x, v_x, m2_x);
1469 v_y = bound(m1_y, v_y, m2_y);
1470 v_z = bound(m1_z, v_z, m2_z);
1474 diff = v - self.origin;
1475 diff_z = max(0, diff_z);
1476 if (vlen(diff) < maxdist)
1478 head.wpconsidered = TRUE;
1479 if (tracewalk(self, self.origin, self.mins, self.maxs, v, bot_navigation_movemode))
1481 head.wpnearestpoint = v;
1482 head.wpcost = vlen(v - self.origin) + head.dmg;
1491 //navigation_testtracewalk = FALSE;
1495 // updates a path link if a spawnfunc_waypoint link is better than the current one
1496 void navigation_markroutes_checkwaypoint(entity w, entity w2, float cost2, vector p)
1505 v_x = bound(m1_x, p_x, m2_x);
1506 v_y = bound(m1_y, p_y, m2_y);
1507 v_z = bound(m1_z, p_z, m2_z);
1511 cost2 = cost2 + vlen(v);
1512 if (w2.wpcost > cost2)
1517 w2.wpnearestpoint = v;
1521 // queries the entire spawnfunc_waypoint network for pathes leading away from the bot
1522 void navigation_markroutes()
1524 local entity w, wp, waylist;
1525 local float searching, cost, cost2;
1527 w = waylist = findchain(classname, "waypoint");
1530 w.wpconsidered = FALSE;
1531 w.wpnearestpoint = '0 0 0';
1532 w.wpcost = 10000000;
1538 // try a short range search for the nearest waypoints, and expand the search repeatedly if none are found
1539 // as this search is expensive we will use lower values if the bot is on the air
1540 local float i, increment, maxdistance;
1541 if(self.flags & FL_ONGROUND)
1544 maxdistance = 50000;
1552 for(i=increment;!navigation_markroutes_nearestwaypoints(waylist, i)&&i<maxdistance;i+=increment);
1566 p = w.wpnearestpoint;
1568 for(i=0;i<MAX_WAYPOINTS_LINKS;++i)
1574 cost2 = cost + wp.dmg;
1575 if (wp.wpcost > cost2 + wp.wpmincost[i])
1576 navigation_markroutes_checkwaypoint(w, wp, cost2, p);
1586 void navigation_bestgoals_reset()
1590 bestgoalswindex = 0;
1591 bestgoalsrindex = 0;
1593 for(i=0;i>MAX_BESTGOALS-1;++i)
1595 navigation_bestgoals[i] = world;
1599 void navigation_add_bestgoal(entity goal)
1601 if(bestgoalsrindex>0)
1605 if(bestgoalsrindex==MAX_BESTGOALS)
1606 bestgoalsrindex = 0;
1609 if(bestgoalswindex==MAX_BESTGOALS)
1612 if(bestgoalsrindex==0)
1616 navigation_bestgoals[bestgoalswindex] = goal;
1621 entity navigation_get_bestgoal()
1625 ent = navigation_bestgoals[bestgoalsrindex];
1626 navigation_bestgoals[bestgoalsrindex] = world;
1630 if(bestgoalsrindex==MAX_BESTGOALS)
1631 bestgoalsrindex = 0;
1636 // fields required for jetpack navigation
1637 .entity navigation_jetpack_goal;
1638 .vector navigation_jetpack_point;
1640 // updates the best goal according to a weighted calculation of travel cost and item value of a new proposed item
1641 .void() havocbot_role;
1642 void() havocbot_role_ctf_offense;
1643 void navigation_routerating(entity e, float f, float rangebias)
1648 // Evaluate path using jetpack
1650 if(self.items & IT_JETPACK)
1651 if(cvar("bot_ai_navigation_jetpack"))
1652 if(vlen(self.origin - e.origin) > cvar("bot_ai_navigation_jetpack_mindistance"))
1654 vector pointa, pointb;
1656 // dprint("jetpack ai: evaluating path for ", e.classname,"\n");
1659 traceline(self.origin, self.origin + '0 0 65535', MOVE_NORMAL, self);
1660 pointa = trace_endpos - '0 0 1';
1663 traceline(e.origin, e.origin + '0 0 65535', MOVE_NORMAL, e);
1664 pointb = trace_endpos - '0 0 1';
1666 // Can I see these two points from the sky?
1667 traceline(pointa, pointb, MOVE_NORMAL, self);
1669 if(trace_fraction==1)
1671 // dprint("jetpack ai: can bridge these two points\n");
1673 // Lower the altitude of these points as much as possible
1674 local float zdistance, xydistance, cost, t, fuel;
1675 local vector down, npa, npb;
1677 down = '0 0 -1' * (PL_MAX_z - PL_MIN_z) * 10;
1680 npa = pointa + down;
1681 npb = pointb + down;
1683 if(npa_z<=self.absmax_z)
1686 if(npb_z<=e.absmax_z)
1689 traceline(npa, npb, MOVE_NORMAL, self);
1690 if(trace_fraction==1)
1696 while(trace_fraction == 1);
1699 // Rough estimation of fuel consumption
1700 // (ignores acceleration and current xyz velocity)
1701 xydistance = vlen(pointa - pointb);
1702 zdistance = fabs(pointa_z - self.origin_z);
1704 t = zdistance / cvar("g_jetpack_maxspeed_up");
1705 t += xydistance / cvar("g_jetpack_maxspeed_side");
1706 fuel = t * cvar("g_jetpack_fuel") * 0.8;
1708 // dprint("jetpack ai: required fuel ", ftos(fuel), " self.ammo_fuel ", ftos(self.ammo_fuel),"\n");
1711 if(self.ammo_fuel>fuel)
1714 // (as onground costs calculation is mostly based on distances, here we do the same establishing some relationship
1715 // - between air and ground speeds)
1717 cost = xydistance / (cvar("g_jetpack_maxspeed_side")/cvar("sv_maxspeed"));
1718 cost += zdistance / (cvar("g_jetpack_maxspeed_up")/cvar("sv_maxspeed"));
1721 // Compare against other goals
1722 f = f * rangebias / (rangebias + cost);
1724 if (navigation_bestrating < f)
1726 // dprint("jetpack path: added goal", e.classname, " (with rating ", ftos(f), ")\n");
1727 navigation_bestrating = f;
1728 navigation_add_bestgoal(e);
1729 self.navigation_jetpack_goal = e;
1730 self.navigation_jetpack_point = pointb;
1737 //te_wizspike(e.origin);
1740 // update the cached spawnfunc_waypoint link on a dynamic item entity
1741 if (time > e.nearestwaypointtimeout)
1743 e.nearestwaypoint = navigation_findnearestwaypoint(e, TRUE);
1745 // TODO: Cleaner solution, probably handling this timeout from ctf.qc
1746 if(e.classname=="item_flag_team")
1747 e.nearestwaypointtimeout = time + 2;
1749 e.nearestwaypointtimeout = time + random() * 3 + 5;
1752 //dprint("-- checking ", e.classname, " (with cost ", ftos(e.nearestwaypoint.wpcost), ")\n");
1753 if (e.nearestwaypoint)
1754 if (e.nearestwaypoint.wpcost < 10000000)
1756 //te_wizspike(e.nearestwaypoint.wpnearestpoint);
1757 // dprint(e.classname, " ", ftos(f), "/(1+", ftos((e.nearestwaypoint.wpcost + vlen(e.origin - e.nearestwaypoint.wpnearestpoint))), "/", ftos(rangebias), ") = ");
1758 f = f * rangebias / (rangebias + (e.nearestwaypoint.wpcost + vlen(e.origin - e.nearestwaypoint.wpnearestpoint)));
1759 //if (self.havocbot_role == havocbot_role_ctf_offense)
1760 //dprint("considering ", e.classname, " (with rating ", ftos(f), ")\n");
1762 if (navigation_bestrating < f)
1764 // dprint("ground path: added goal ", e.classname, " (with rating ", ftos(f), ")\n");
1765 navigation_bestrating = f;
1766 navigation_add_bestgoal(e);
1772 // adds an item to the the goal stack with the path to a given item
1773 float navigation_routetogoal(entity e, vector startposition)
1775 self.goalentity = e;
1777 // if there is no goal, just exit
1781 self.navigation_hasgoals = TRUE;
1783 // put the entity on the goal stack
1784 navigation_pushroute(e);
1787 if(e==self.navigation_jetpack_goal)
1790 // if it can reach the goal there is nothing more to do
1791 if (tracewalk(self, startposition, PL_MIN, PL_MAX, e.origin, bot_navigation_movemode))
1794 // see if there are waypoints describing a path to the item
1795 e = e.nearestwaypoint;
1801 // add the spawnfunc_waypoint to the path
1802 navigation_pushroute(e);
1812 void navigation_routetogoals()
1816 navigation_clearroute();
1818 g1 = navigation_get_bestgoal();
1822 g2 = navigation_get_bestgoal();
1826 navigation_routetogoal(g1, self.origin);
1830 if(navigation_routetogoal(g1, g2.origin))
1837 navigation_clearroute();
1843 // removes any currently touching waypoints from the goal stack
1844 // (this is how bots detect if they have reached a goal)
1845 .float lastteleporttime;
1847 void navigation_poptouchedgoals()
1849 local vector org, m1, m2;
1851 m1 = org + self.mins;
1852 m2 = org + self.maxs;
1854 if(self.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT)
1856 if(self.lastteleporttime>0)
1857 if(time-self.lastteleporttime<0.15)
1859 navigation_poproute();
1864 // Loose goal touching check when running
1865 if(self.aistatus & AI_STATUS_RUNNING)
1866 if(self.goalcurrent.classname=="waypoint")
1868 if(vlen(self.origin - self.goalcurrent.origin)<150)
1870 traceline(self.origin + self.view_ofs , self.goalcurrent.origin, TRUE, world);
1871 if(trace_fraction==1)
1873 // Detect personal waypoints
1874 if(self.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
1875 if(self.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL && self.goalcurrent.owner==self)
1877 self.aistatus &~= AI_STATUS_WAYPOINT_PERSONAL_GOING;
1878 self.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_REACHED;
1881 navigation_poproute();
1886 while (self.goalcurrent && boxesoverlap(m1, m2, self.goalcurrent.absmin, self.goalcurrent.absmax))
1888 // Detect personal waypoints
1889 if(self.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
1890 if(self.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL && self.goalcurrent.owner==self)
1892 self.aistatus &~= AI_STATUS_WAYPOINT_PERSONAL_GOING;
1893 self.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_REACHED;
1896 navigation_poproute();
1900 // begin a goal selection session (queries spawnfunc_waypoint network)
1901 void navigation_goalrating_start()
1903 self.navigation_jetpack_goal = world;
1904 navigation_bestrating = -1;
1905 self.navigation_hasgoals = FALSE;
1906 navigation_bestgoals_reset();
1907 navigation_markroutes();
1910 // ends a goal selection session (updates goal stack to the best goal)
1911 void navigation_goalrating_end()
1913 navigation_routetogoals();
1914 // dprint("best goal ", self.goalcurrent.classname , "\n");
1916 // Hack: if it can't walk to any goal just move blindly to the first visible waypoint
1917 if not (self.navigation_hasgoals)
1919 dprint(self.netname, " can't walk to any goal, going to a near waypoint\n");
1923 RandomSelection_Init();
1924 head = findradius(self.origin,1000);
1927 if(head.classname=="waypoint")
1928 if(!(head.wpflags & WAYPOINTFLAG_GENERATED))
1929 if(vlen(self.origin-head.origin)>100)
1930 if(checkpvs(self.view_ofs,head))
1931 RandomSelection_Add(head, 0, string_null, 1 + (vlen(self.origin-head.origin)<500), 0);
1934 if(RandomSelection_chosen_ent)
1935 navigation_routetogoal(RandomSelection_chosen_ent, self.origin);
1937 self.navigation_hasgoals = FALSE; // Reset this value
1942 //////////////////////////////////////////////////////////////////////////////
1943 // general bot functions
1944 //////////////////////////////////////////////////////////////////////////////
1946 .float isbot; // true if this client is actually a bot
1951 .string netname_freeme;
1952 .string playermodel_freeme;
1953 .string playerskin_freeme;
1958 .float bot_preferredcolors;
1962 .float bot_dodgerating;
1964 //.float bot_painintensity;
1965 .float bot_firetimer;
1966 //.float bot_oldhealth;
1969 .entity bot_aimtarg;
1970 .float bot_aimlatency;
1971 .vector bot_aimselforigin;
1972 .vector bot_aimselfvelocity;
1973 .vector bot_aimtargorigin;
1974 .vector bot_aimtargvelocity;
1977 .float(entity player, entity item) bot_pickupevalfunc;
1978 .float bot_pickupbasevalue;
1980 .float bot_strategytime;
1982 // used for aiming currently
1983 // FIXME: make the weapon code use these and then replace the calculations here with a call to the weapon code
1987 .float bot_forced_team;
1988 .float bot_config_loaded;
1990 void bot_setnameandstuff()
1993 float file, tokens, prio;
1996 string bot_name, bot_model, bot_skin, bot_shirt, bot_pants;
1997 string name, prefix, suffix;
1999 if(cvar("g_campaign"))
2006 prefix = cvar_string("bot_prefix");
2007 suffix = cvar_string("bot_suffix");
2010 file = fopen(cvar_string("bot_config_file"), FILE_READ);
2013 print(strcat("Error: Can not open the bot configuration file '",cvar_string("bot_config_file"),"'\n"));
2016 RandomSelection_Init();
2019 readfile = fgets(file);
2022 if(substring(readfile, 0, 2) == "//")
2024 if(substring(readfile, 0, 1) == "#")
2026 tokens = tokenizebyseparator(readfile, "\t");
2031 if(strcat(prefix, s, suffix) == p.netname)
2037 RandomSelection_Add(world, 0, readfile, 1, prio);
2039 readfile = RandomSelection_chosen_string;
2043 tokens = tokenizebyseparator(readfile, "\t");
2044 if(argv(0) != "") bot_name = argv(0);
2045 else bot_name = "Bot";
2047 if(argv(1) != "") bot_model = argv(1);
2048 else bot_model = "marine";
2050 if(argv(2) != "") bot_skin = argv(2);
2051 else bot_skin = "0";
2053 if(argv(3) != "" && stof(argv(3)) >= 0) bot_shirt = argv(3);
2054 else bot_shirt = ftos(floor(random() * 15));
2056 if(argv(4) != "" && stof(argv(4)) >= 0) bot_pants = argv(4);
2057 else bot_pants = ftos(floor(random() * 15));
2059 self.bot_forced_team = stof(argv(5));
2060 self.bot_config_loaded = TRUE;
2062 // this is really only a default, JoinBestTeam is called later
2063 setcolor(self, stof(bot_shirt) * 16 + stof(bot_pants));
2064 self.bot_preferredcolors = self.clientcolors;
2067 if (cvar("bot_usemodelnames"))
2072 // pick the model and skin
2073 if(substring(bot_model, -4, 1) != ".")
2074 bot_model = strcat(bot_model, ".zym");
2075 self.playermodel = self.playermodel_freeme = strzone(strcat("models/player/", bot_model));
2076 self.playerskin = self.playerskin_freeme = strzone(bot_skin);
2078 self.netname = self.netname_freeme = strzone(strcat(prefix, name, suffix));
2081 float bot_custom_weapon;
2082 float bot_distance_far;
2083 float bot_distance_close;
2085 float bot_weapons_far[WEP_LAST];
2086 float bot_weapons_mid[WEP_LAST];
2087 float bot_weapons_close[WEP_LAST];
2089 void bot_custom_weapon_priority_setup()
2091 local float tokens, i, c, w;
2093 bot_custom_weapon = FALSE;
2095 if( cvar_string("bot_ai_custom_weapon_priority_far") == "" ||
2096 cvar_string("bot_ai_custom_weapon_priority_mid") == "" ||
2097 cvar_string("bot_ai_custom_weapon_priority_close") == "" ||
2098 cvar_string("bot_ai_custom_weapon_priority_distances") == ""
2103 tokens = tokenizebyseparator(cvar_string("bot_ai_custom_weapon_priority_distances")," ");
2108 bot_distance_far = stof(argv(0));
2109 bot_distance_close = stof(argv(1));
2111 if(bot_distance_far < bot_distance_close){
2112 bot_distance_far = stof(argv(1));
2113 bot_distance_close = stof(argv(0));
2116 // Initialize list of weapons
2117 bot_weapons_far[0] = -1;
2118 bot_weapons_mid[0] = -1;
2119 bot_weapons_close[0] = -1;
2121 // Parse far distance weapon priorities
2122 tokens = tokenizebyseparator(cvar_string("bot_ai_custom_weapon_priority_far")," ");
2125 for(i=0; i < tokens && c < WEP_COUNT; ++i){
2127 if ( w >= WEP_FIRST && w <= WEP_LAST) {
2128 bot_weapons_far[c] = w;
2133 bot_weapons_far[c] = -1;
2135 // Parse mid distance weapon priorities
2136 tokens = tokenizebyseparator(cvar_string("bot_ai_custom_weapon_priority_mid")," ");
2139 for(i=0; i < tokens && c < WEP_COUNT; ++i){
2141 if ( w >= WEP_FIRST && w <= WEP_LAST) {
2142 bot_weapons_mid[c] = w;
2147 bot_weapons_mid[c] = -1;
2149 // Parse close distance weapon priorities
2150 tokens = tokenizebyseparator(cvar_string("bot_ai_custom_weapon_priority_close")," ");
2153 for(i=0; i < tokens && i < WEP_COUNT; ++i){
2155 if ( w >= WEP_FIRST && w <= WEP_LAST) {
2156 bot_weapons_close[c] = w;
2161 bot_weapons_close[c] = -1;
2163 bot_custom_weapon = TRUE;
2170 //dprint("bot_endgame\n");
2174 setcolor(e, e.bot_preferredcolors);
2177 // if dynamic waypoints are ever implemented, save them here
2180 float bot_ignore_bots; // let bots not attack other bots (only works in non-teamplay)
2181 float bot_shouldattack(entity e)
2183 if (e.team == self.team)
2197 else if(bot_ignore_bots)
2198 if(clienttype(e) == CLIENTTYPE_BOT)
2208 if(e.items & IT_STRENGTH)
2210 if(e.flags & FL_NOTARGET)
2215 void bot_lagfunc(float t, float f1, float f2, entity e1, vector v1, vector v2, vector v3, vector v4)
2217 if(self.flags & FL_INWATER)
2219 self.bot_aimtarg = world;
2222 self.bot_aimtarg = e1;
2223 self.bot_aimlatency = self.ping; // FIXME? Shouldn't this be in the lag item?
2224 self.bot_aimselforigin = v1;
2225 self.bot_aimselfvelocity = v2;
2226 self.bot_aimtargorigin = v3;
2227 self.bot_aimtargvelocity = v4;
2229 self.bot_canfire = (random() < 0.8);
2231 self.bot_canfire = (random() < 0.9);
2233 self.bot_canfire = (random() < 0.95);
2235 self.bot_canfire = 1;
2238 .float bot_nextthink;
2239 .float bot_badaimtime;
2240 .float bot_aimthinktime;
2241 .float bot_prevaimtime;
2242 .vector bot_mouseaim;
2243 .vector bot_badaimoffset;
2244 .vector bot_1st_order_aimfilter;
2245 .vector bot_2nd_order_aimfilter;
2246 .vector bot_3th_order_aimfilter;
2247 .vector bot_4th_order_aimfilter;
2248 .vector bot_5th_order_aimfilter;
2249 .vector bot_olddesiredang;
2250 float bot_aimdir(vector v, float maxfiredeviation)
2252 local float dist, delta_t, blend;
2253 local vector desiredang, diffang;
2255 //dprint("aim ", self.netname, ": old:", vtos(self.v_angle));
2256 // make sure v_angle is sane first
2257 self.v_angle_y = self.v_angle_y - floor(self.v_angle_y / 360) * 360;
2260 // get the desired angles to aim at
2261 //dprint(" at:", vtos(v));
2263 //te_lightning2(world, self.origin + self.view_ofs, self.origin + self.view_ofs + v * 200);
2264 if (time >= self.bot_badaimtime)
2266 self.bot_badaimtime = max(self.bot_badaimtime + 0.3, time);
2267 self.bot_badaimoffset = randomvec() * bound(0, 5 - 0.5 * (skill+self.bot_offsetskill), 5) * cvar("bot_ai_aimskill_offset");
2269 desiredang = vectoangles(v) + self.bot_badaimoffset;
2270 //dprint(" desired:", vtos(desiredang));
2271 if (desiredang_x >= 180)
2272 desiredang_x = desiredang_x - 360;
2273 desiredang_x = bound(-90, 0 - desiredang_x, 90);
2274 desiredang_z = self.v_angle_z;
2275 //dprint(" / ", vtos(desiredang));
2277 //// pain throws off aim
2278 //if (self.bot_painintensity)
2280 // // shake from pain
2281 // desiredang = desiredang + randomvec() * self.bot_painintensity * 0.2;
2284 // calculate turn angles
2285 diffang = (desiredang - self.bot_olddesiredang);
2287 diffang_y = diffang_y - floor(diffang_y / 360) * 360;
2288 if (diffang_y >= 180)
2289 diffang_y = diffang_y - 360;
2290 self.bot_olddesiredang = desiredang;
2291 //dprint(" diff:", vtos(diffang));
2293 delta_t = time-self.bot_prevaimtime;
2294 self.bot_prevaimtime = time;
2295 // Here we will try to anticipate the comming aiming direction
2296 self.bot_1st_order_aimfilter= self.bot_1st_order_aimfilter
2297 + (diffang * (1 / delta_t) - self.bot_1st_order_aimfilter) * bound(0, cvar("bot_ai_aimskill_order_filter_1st"),1);
2298 self.bot_2nd_order_aimfilter= self.bot_2nd_order_aimfilter
2299 + (self.bot_1st_order_aimfilter - self.bot_2nd_order_aimfilter) * bound(0, cvar("bot_ai_aimskill_order_filter_2nd"),1);
2300 self.bot_3th_order_aimfilter= self.bot_3th_order_aimfilter
2301 + (self.bot_2nd_order_aimfilter - self.bot_3th_order_aimfilter) * bound(0, cvar("bot_ai_aimskill_order_filter_3th"),1);
2302 self.bot_4th_order_aimfilter= self.bot_4th_order_aimfilter
2303 + (self.bot_3th_order_aimfilter - self.bot_4th_order_aimfilter) * bound(0, cvar("bot_ai_aimskill_order_filter_4th"),1);
2304 self.bot_5th_order_aimfilter= self.bot_5th_order_aimfilter
2305 + (self.bot_4th_order_aimfilter - self.bot_5th_order_aimfilter) * bound(0, cvar("bot_ai_aimskill_order_filter_5th"),1);
2307 //blend = (bound(0,skill,10)*0.1)*pow(1-bound(0,skill,10)*0.05,2.5)*5.656854249; //Plot formule before changing !
2308 blend = bound(0,skill,10)*0.1;
2309 desiredang = desiredang + blend *
2311 self.bot_1st_order_aimfilter * cvar("bot_ai_aimskill_order_mix_1st")
2312 + self.bot_2nd_order_aimfilter * cvar("bot_ai_aimskill_order_mix_2nd")
2313 + self.bot_3th_order_aimfilter * cvar("bot_ai_aimskill_order_mix_3th")
2314 + self.bot_4th_order_aimfilter * cvar("bot_ai_aimskill_order_mix_4th")
2315 + self.bot_5th_order_aimfilter * cvar("bot_ai_aimskill_order_mix_5th")
2318 // calculate turn angles
2319 diffang = desiredang - self.bot_mouseaim;
2321 diffang_y = diffang_y - floor(diffang_y / 360) * 360;
2322 if (diffang_y >= 180)
2323 diffang_y = diffang_y - 360;
2324 //dprint(" diff:", vtos(diffang));
2326 if (time >= self.bot_aimthinktime)
2328 self.bot_aimthinktime = max(self.bot_aimthinktime + 0.5 - 0.05*(skill+self.bot_thinkskill), time);
2329 self.bot_mouseaim = self.bot_mouseaim + diffang * (1-random()*0.1*bound(1,10-skill,10));
2332 //self.v_angle = self.v_angle + diffang * bound(0, r * frametime * (skill * 0.5 + 2), 1);
2334 diffang = self.bot_mouseaim - desiredang;
2336 diffang_y = diffang_y - floor(diffang_y / 360) * 360;
2337 if (diffang_y >= 180)
2338 diffang_y = diffang_y - 360;
2339 desiredang = desiredang + diffang * bound(0,cvar("bot_ai_aimskill_think"),1);
2341 // calculate turn angles
2342 diffang = desiredang - self.v_angle;
2344 diffang_y = diffang_y - floor(diffang_y / 360) * 360;
2345 if (diffang_y >= 180)
2346 diffang_y = diffang_y - 360;
2347 //dprint(" diff:", vtos(diffang));
2350 dist = vlen(diffang);
2351 //diffang = diffang + randomvec() * (dist * 0.05 * (3.5 - bound(0, skill, 3)));
2354 local float r, fixedrate, blendrate;
2355 fixedrate = cvar("bot_ai_aimskill_fixedrate") / bound(1,dist,1000);
2356 blendrate = cvar("bot_ai_aimskill_blendrate");
2357 r = max(fixedrate, blendrate);
2358 //self.v_angle = self.v_angle + diffang * bound(frametime, r * frametime * (2+skill*skill*0.05-random()*0.05*(10-skill)), 1);
2359 self.v_angle = self.v_angle + diffang * bound(delta_t, r * delta_t * (2+pow(skill+self.bot_mouseskill,3)*0.005-random()), 1);
2360 self.v_angle = self.v_angle * bound(0,cvar("bot_ai_aimskill_mouse"),1) + desiredang * bound(0,(1-cvar("bot_ai_aimskill_mouse")),1);
2361 //self.v_angle = self.v_angle + diffang * bound(0, r * frametime * (skill * 0.5 + 2), 1);
2362 //self.v_angle = self.v_angle + diffang * (1/ blendrate);
2364 self.v_angle_y = self.v_angle_y - floor(self.v_angle_y / 360) * 360;
2365 //dprint(" turn:", vtos(self.v_angle));
2367 makevectors(self.v_angle);
2368 shotorg = self.origin + self.view_ofs;
2369 shotdir = v_forward;
2371 //dprint(" dir:", vtos(v_forward));
2372 //te_lightning2(world, shotorg, shotorg + shotdir * 100);
2374 // calculate turn angles again
2375 //diffang = desiredang - self.v_angle;
2376 //diffang_y = diffang_y - floor(diffang_y / 360) * 360;
2377 //if (diffang_y >= 180)
2378 // diffang_y = diffang_y - 360;
2380 //dprint("e ", vtos(diffang), " < ", ftos(maxfiredeviation), "\n");
2382 // decide whether to fire this time
2383 // note the maxfiredeviation is in degrees so this has to convert to radians first
2384 //if ((normalize(v) * shotdir) >= cos(maxfiredeviation * (3.14159265358979323846 / 180)))
2385 if ((normalize(v) * shotdir) >= cos(maxfiredeviation * (3.14159265358979323846 / 180)))
2386 if (vlen(trace_endpos-shotorg) < 500+500*bound(0, skill, 10) || random()*random()>bound(0,skill*0.05,1))
2387 self.bot_firetimer = time + bound(0.1, 0.5-skill*0.05, 0.5);
2388 //traceline(shotorg,shotorg+shotdir*1000,FALSE,world);
2389 //dprint(ftos(maxfiredeviation),"\n");
2390 //dprint(" diff:", vtos(diffang), "\n");
2392 return self.bot_canfire && (time < self.bot_firetimer);
2395 vector bot_shotlead(vector targorigin, vector targvelocity, float shotspeed, float shotdelay)
2397 // Try to add code here that predicts gravity effect here, no clue HOW to though ... well not yet atleast...
2398 return targorigin + targvelocity * (shotdelay + vlen(targorigin - shotorg) / shotspeed);
2401 float bot_aim(float shotspeed, float shotspeedupward, float maxshottime, float applygravity)
2407 dprint("bot_aim(", ftos(shotspeed));
2408 dprint(", ", ftos(shotspeedupward));
2409 dprint(", ", ftos(maxshottime));
2410 dprint(", ", ftos(applygravity));
2413 shotspeed *= g_weaponspeedfactor;
2414 shotspeedupward *= g_weaponspeedfactor;
2417 dprint("bot_aim: WARNING: weapon ", W_Name(self.weapon), " shotspeed is zero!\n");
2418 shotspeed = 1000000;
2422 dprint("bot_aim: WARNING: weapon ", W_Name(self.weapon), " maxshottime is zero!\n");
2425 makevectors(self.v_angle);
2426 shotorg = self.origin + self.view_ofs;
2427 shotdir = v_forward;
2428 v = bot_shotlead(self.bot_aimtargorigin, self.bot_aimtargvelocity, shotspeed, self.bot_aimlatency);
2429 local float distanceratio;
2430 distanceratio =sqrt(bound(0,skill,10000))*0.3*(vlen(v-shotorg)-100)/cvar("bot_ai_aimskill_firetolerance_distdegrees");
2431 distanceratio = bound(0,distanceratio,1);
2432 r = (cvar("bot_ai_aimskill_firetolerance_maxdegrees")-cvar("bot_ai_aimskill_firetolerance_mindegrees"))
2433 * (1-distanceratio) + cvar("bot_ai_aimskill_firetolerance_mindegrees");
2434 if (applygravity && self.bot_aimtarg)
2436 if (!findtrajectorywithleading(shotorg, '0 0 0', '0 0 0', self.bot_aimtarg, shotspeed, shotspeedupward, maxshottime, 0, self))
2438 f = bot_aimdir(findtrajectory_velocity - shotspeedupward * '0 0 1', r);
2442 f = bot_aimdir(v - shotorg, r);
2443 //dprint("AIM: ");dprint(vtos(self.bot_aimtargorigin));dprint(" + ");dprint(vtos(self.bot_aimtargvelocity));dprint(" * ");dprint(ftos(self.bot_aimlatency + vlen(self.bot_aimtargorigin - shotorg) / shotspeed));dprint(" = ");dprint(vtos(v));dprint(" : aimdir = ");dprint(vtos(normalize(v - shotorg)));dprint(" : ");dprint(vtos(shotdir));dprint("\n");
2444 traceline(shotorg, shotorg + shotdir * 10000, FALSE, self);
2445 if (trace_ent.takedamage)
2446 if (trace_fraction < 1)
2447 if (!bot_shouldattack(trace_ent))
2449 traceline(shotorg, self.bot_aimtargorigin, FALSE, self);
2450 if (trace_fraction < 1)
2451 if (trace_ent != self.enemy)
2452 if (!bot_shouldattack(trace_ent))
2455 if (r > maxshottime * shotspeed)
2460 // TODO: move this painintensity code to the player damage code
2463 if (self.bot_nextthink > time)
2466 self.flags &~= FL_GODMODE;
2468 self.flags |= FL_GODMODE;
2470 self.bot_nextthink = self.bot_nextthink + cvar("bot_ai_thinkinterval");
2471 //if (self.bot_painintensity > 0)
2472 // self.bot_painintensity = self.bot_painintensity - (skill + 1) * 40 * frametime;
2474 //self.bot_painintensity = self.bot_painintensity + self.bot_oldhealth - self.health;
2475 //self.bot_painintensity = bound(0, self.bot_painintensity, 100);
2477 if(time < game_starttime || ((cvar("g_campaign") && !campaign_bots_may_start)))
2479 self.nextthink = time + 0.5;
2485 self.v_angle = self.angles;
2487 self.fixangle = FALSE;
2492 self.dmg_inflictor = world;
2494 // calculate an aiming latency based on the skill setting
2495 // (simulated network latency + naturally delayed reflexes)
2496 //self.ping = 0.7 - bound(0, 0.05 * skill, 0.5); // moved the reflexes to bot_aimdir (under the name 'think')
2497 // minimum ping 20+10 random
2498 self.ping = bound(0,0.07 - bound(0, skill * 0.005,0.05)+random()*0.01,0.65); // Now holds real lag to server, and higer skill players take a less laggy server
2499 // skill 10 = ping 0.2 (adrenaline)
2500 // skill 0 = ping 0.7 (slightly drunk)
2503 self.BUTTON_ATCK = 0;
2505 self.BUTTON_JUMP = 0;
2506 self.BUTTON_ATCK2 = 0;
2507 self.BUTTON_ZOOM = 0;
2508 self.BUTTON_CROUCH = 0;
2509 self.BUTTON_HOOK = 0;
2510 self.BUTTON_INFO = 0;
2512 self.BUTTON_CHAT = 0;
2513 self.BUTTON_USE = 0;
2515 // if dead, just wait until we can respawn
2518 if (self.deadflag == DEAD_DEAD)
2520 self.BUTTON_JUMP = 1; // press jump to respawn
2521 self.bot_strategytime = 0;
2525 // now call the current bot AI (havocbot for example)
2529 entity bot_strategytoken;
2530 float bot_strategytoken_taken;
2533 void bot_relinkplayerlist()
2536 local entity prevbot;
2539 player_list = e = findchainflags(flags, FL_CLIENT);
2544 player_count = player_count + 1;
2545 e.nextplayer = e.chain;
2546 if (clienttype(e) == CLIENTTYPE_BOT)
2549 prevbot.nextbot = e;
2553 bot_list.nextbot = world;
2556 currentbots = currentbots + 1;
2560 dprint(strcat("relink: ", ftos(currentbots), " bots seen.\n"));
2561 bot_strategytoken = bot_list;
2562 bot_strategytoken_taken = TRUE;
2565 void() havocbot_setupbot;
2566 float JoinBestTeam(entity pl, float only_return_best, float forcebestteam);
2568 void bot_clientdisconnect()
2570 if (clienttype(self) != CLIENTTYPE_BOT)
2572 if(self.netname_freeme)
2573 strunzone(self.netname_freeme);
2574 if(self.playermodel_freeme)
2575 strunzone(self.playermodel_freeme);
2576 if(self.playerskin_freeme)
2577 strunzone(self.playerskin_freeme);
2578 self.netname_freeme = string_null;
2579 self.playermodel_freeme = string_null;
2580 self.playerskin_freeme = string_null;
2583 void bot_clientconnect()
2585 if (clienttype(self) != CLIENTTYPE_BOT)
2587 self.bot_preferredcolors = self.clientcolors;
2588 self.bot_nextthink = time - random();
2589 self.lag_func = bot_lagfunc;
2591 self.createdtime = self.nextthink;
2593 if(!self.bot_config_loaded) // This is needed so team overrider doesn't break between matches
2594 bot_setnameandstuff();
2596 if(self.bot_forced_team==1)
2597 self.team = COLOR_TEAM1;
2598 else if(self.bot_forced_team==2)
2599 self.team = COLOR_TEAM2;
2600 else if(self.bot_forced_team==3)
2601 self.team = COLOR_TEAM3;
2602 else if(self.bot_forced_team==4)
2603 self.team = COLOR_TEAM4;
2605 JoinBestTeam(self, FALSE, TRUE);
2607 havocbot_setupbot();
2608 self.bot_mouseskill=random()-0.5;
2609 self.bot_thinkskill=random()-0.5;
2610 self.bot_predictionskill=random()-0.5;
2611 self.bot_offsetskill=random()-0.5;
2616 local entity oldself, bot;
2617 bot = spawnclient();
2620 currentbots = currentbots + 1;
2623 bot_setnameandstuff();
2625 PutClientInServer();
2631 void CheckAllowedTeams(entity for_whom); void GetTeamCounts(entity other); float c1, c2, c3, c4;
2632 void bot_removefromlargestteam()
2634 local float besttime, bestcount, thiscount;
2635 local entity best, head;
2636 CheckAllowedTeams(world);
2637 GetTeamCounts(world);
2638 head = findchainfloat(isbot, TRUE);
2642 besttime = head.createdtime;
2646 if(head.team == COLOR_TEAM1)
2648 else if(head.team == COLOR_TEAM2)
2650 else if(head.team == COLOR_TEAM3)
2652 else if(head.team == COLOR_TEAM4)
2656 if (thiscount > bestcount)
2658 bestcount = thiscount;
2659 besttime = head.createdtime;
2662 else if (thiscount == bestcount && besttime < head.createdtime)
2664 besttime = head.createdtime;
2669 currentbots = currentbots - 1;
2673 void bot_removenewest()
2675 local float besttime;
2676 local entity best, head;
2680 bot_removefromlargestteam();
2684 head = findchainfloat(isbot, TRUE);
2688 besttime = head.createdtime;
2691 if (besttime < head.createdtime)
2693 besttime = head.createdtime;
2698 currentbots = currentbots - 1;
2702 float botframe_waypointeditorlightningtime;
2703 void botframe_showwaypointlinks()
2705 local entity player, head, w;
2708 if (time < botframe_waypointeditorlightningtime)
2710 botframe_waypointeditorlightningtime = time + 0.5;
2711 player = find(world, classname, "player");
2715 if (player.flags & FL_ONGROUND || player.waterlevel > WATERLEVEL_NONE)
2717 //navigation_testtracewalk = TRUE;
2718 head = navigation_findnearestwaypoint(player, FALSE);
2719 // print("currently selected WP is ", etos(head), "\n");
2720 //navigation_testtracewalk = FALSE;
2723 te_lightning2(world, head.origin, player.origin);
2725 for(i=0;i<MAX_WAYPOINTS_LINKS;++i)
2729 te_lightning2(world, w.origin, head.origin);
2733 player = find(player, classname, "player");
2737 entity botframe_dangerwaypoint;
2738 void botframe_updatedangerousobjects(float maxupdate)
2740 local entity head, bot_dodgelist;
2741 local vector m1, m2, v;
2742 local float c, d, danger;
2744 bot_dodgelist = findchainfloat(bot_dodge, TRUE);
2745 botframe_dangerwaypoint = find(botframe_dangerwaypoint, classname, "waypoint");
2746 while (botframe_dangerwaypoint != world)
2749 m1 = botframe_dangerwaypoint.mins;
2750 m2 = botframe_dangerwaypoint.maxs;
2751 head = bot_dodgelist;
2755 v_x = bound(m1_x, v_x, m2_x);
2756 v_y = bound(m1_y, v_y, m2_y);
2757 v_z = bound(m1_z, v_z, m2_z);
2758 d = head.bot_dodgerating - vlen(head.origin - v);
2761 traceline(head.origin, v, TRUE, world);
2762 if (trace_fraction == 1)
2763 danger = danger + d;
2767 botframe_dangerwaypoint.dmg = danger;
2771 botframe_dangerwaypoint = find(botframe_dangerwaypoint, classname, "waypoint");
2776 float botframe_spawnedwaypoints;
2777 float botframe_nextthink;
2778 float botframe_nextdangertime;
2780 float autoskill_nextthink;
2781 .float totalfrags_lastcheck;
2782 void autoskill(float factor)
2790 FOR_EACH_PLAYER(head)
2792 if(clienttype(head) == CLIENTTYPE_REAL)
2793 bestplayer = max(bestplayer, head.totalfrags - head.totalfrags_lastcheck);
2795 bestbot = max(bestbot, head.totalfrags - head.totalfrags_lastcheck);
2798 dprint("autoskill: best player got ", ftos(bestplayer), ", ");
2799 dprint("best bot got ", ftos(bestbot), "; ");
2800 if(bestbot < 0 || bestplayer < 0)
2802 dprint("not doing anything\n");
2803 // don't return, let it reset all counters below
2805 else if(bestbot <= bestplayer * factor - 2)
2807 if(cvar("skill") < 17)
2809 dprint("2 frags difference, increasing skill\n");
2810 cvar_set("skill", ftos(cvar("skill") + 1));
2811 bprint("^2SKILL UP!^7 Now at level ", ftos(cvar("skill")), "\n");
2814 else if(bestbot >= bestplayer * factor + 2)
2816 if(cvar("skill") > 0)
2818 dprint("2 frags difference, decreasing skill\n");
2819 cvar_set("skill", ftos(cvar("skill") - 1));
2820 bprint("^1SKILL DOWN!^7 Now at level ", ftos(cvar("skill")), "\n");
2825 dprint("not doing anything\n");
2827 // don't reset counters, wait for them to accumulate
2830 FOR_EACH_PLAYER(head)
2831 head.totalfrags_lastcheck = head.totalfrags;
2834 float bot_cvar_nextthink;
2835 void bot_serverframe()
2837 float realplayers, bots, activerealplayers;
2840 if (intermission_running)
2846 stepheightvec = cvar("sv_stepheight") * '0 0 1';
2847 bot_navigation_movemode = ((cvar("bot_navigation_ignoreplayers")) ? MOVE_NOMONSTERS : MOVE_NORMAL);
2849 if(time > autoskill_nextthink)
2852 a = cvar("skill_auto");
2855 autoskill_nextthink = time + 5;
2858 activerealplayers = 0;
2861 FOR_EACH_REALCLIENT(head)
2863 if(head.classname == "player" || g_lms || g_arena)
2864 ++activerealplayers;
2868 // add/remove bots if needed to make sure there are at least
2869 // minplayers+bot_number, or remove all bots if no one is playing
2870 // But don't remove bots immediately on level change, as the real players
2871 // usually haven't rejoined yet
2872 bots_would_leave = FALSE;
2873 if ((realplayers || cvar("bot_join_empty") || (currentbots > 0 && time < 5)))
2875 float realminplayers, minplayers;
2876 realminplayers = cvar("minplayers");
2877 minplayers = max(0, floor(realminplayers));
2879 float realminbots, minbots;
2880 if(teamplay && cvar("bot_vs_human"))
2881 realminbots = ceil(fabs(cvar("bot_vs_human")) * activerealplayers);
2883 realminbots = cvar("bot_number");
2884 minbots = max(0, floor(realminbots));
2886 bots = min(max(minbots, minplayers - activerealplayers), maxclients - realplayers);
2888 bots_would_leave = TRUE;
2892 // if there are no players, remove bots
2896 bot_ignore_bots = cvar("bot_ignore_bots");
2898 // only add one bot per frame to avoid utter chaos
2899 if(time > botframe_nextthink)
2901 //dprint(ftos(bots), " ? ", ftos(currentbots), "\n");
2902 while (currentbots < bots)
2904 if (bot_spawn() == world)
2906 bprint("Can not add bot, server full.\n");
2907 botframe_nextthink = time + 10;
2911 while (currentbots > bots)
2915 if(botframe_spawnedwaypoints)
2917 if(cvar("waypoint_benchmark"))
2921 if (currentbots > 0 || cvar("g_waypointeditor"))
2922 if (botframe_spawnedwaypoints)
2924 if(botframe_cachedwaypointlinks)
2926 if(!botframe_loadedforcedlinks)
2927 waypoint_load_links_hardwired();
2931 // TODO: Make this check cleaner
2932 local entity w = findchain(classname, "waypoint");
2933 if(time - w.nextthink > 10)
2934 waypoint_save_links();
2939 botframe_spawnedwaypoints = TRUE;
2941 if(!waypoint_load_links())
2942 waypoint_schedulerelinkall();
2947 // cycle the goal token from one bot to the next each frame
2948 // (this prevents them from all doing spawnfunc_waypoint searches on the same
2949 // frame, which causes choppy framerates)
2950 if (bot_strategytoken_taken)
2952 bot_strategytoken_taken = FALSE;
2953 if (bot_strategytoken)
2954 bot_strategytoken = bot_strategytoken.nextbot;
2955 if (!bot_strategytoken)
2956 bot_strategytoken = bot_list;
2959 if (botframe_nextdangertime < time)
2961 local float interval;
2962 interval = cvar("bot_ai_dangerdetectioninterval");
2963 if (botframe_nextdangertime < time - interval * 1.5)
2964 botframe_nextdangertime = time;
2965 botframe_nextdangertime = botframe_nextdangertime + interval;
2966 botframe_updatedangerousobjects(cvar("bot_ai_dangerdetectionupdates"));
2970 if (cvar("g_waypointeditor"))
2971 botframe_showwaypointlinks();
2973 if(time > bot_cvar_nextthink)
2976 bot_custom_weapon_priority_setup();
2977 bot_cvar_nextthink = time + 5;
2981 FTEQCC_YOU_SUCK_THIS_IS_NOT_UNREFERENCED(lag_time);
2982 FTEQCC_YOU_SUCK_THIS_IS_NOT_UNREFERENCED(lag_float1);
2983 FTEQCC_YOU_SUCK_THIS_IS_NOT_UNREFERENCED(lag_float2);
2984 FTEQCC_YOU_SUCK_THIS_IS_NOT_UNREFERENCED(lag_vec1);
2985 FTEQCC_YOU_SUCK_THIS_IS_NOT_UNREFERENCED(lag_vec2);
2986 FTEQCC_YOU_SUCK_THIS_IS_NOT_UNREFERENCED(lag_vec3);
2987 FTEQCC_YOU_SUCK_THIS_IS_NOT_UNREFERENCED(lag_vec4);
2988 FTEQCC_YOU_SUCK_THIS_IS_NOT_UNREFERENCED(lag_entity1);
2989 FTEQCC_YOU_SUCK_THIS_IS_NOT_UNREFERENCED(wlink);
2990 FTEQCC_YOU_SUCK_THIS_IS_NOT_UNREFERENCED(wpmincost);