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_GOING = 64; // Going to a personal waypoint
9 float AI_STATUS_WAYPOINT_PERSONAL_REACHED = 128; // Personal waypoint reached
11 // utilities for path debugging
12 #ifdef DEBUG_TRACEWALK
14 float DEBUG_NODE_SUCCESS = 1;
15 float DEBUG_NODE_WARNING = 2;
16 float DEBUG_NODE_FAIL = 3;
20 void debugresetnodes()
22 debuglastnode = '0 0 0';
25 void debugnode(vector node)
27 if not(self.classname=="player")
30 if(debuglastnode=='0 0 0')
36 te_lightning2(world, node, debuglastnode);
40 void debugnodestatus(vector position, float status)
46 case DEBUG_NODE_SUCCESS:
49 case DEBUG_NODE_WARNING:
59 te_customflash(position, 40, 2, color);
64 // rough simulation of walking from one point to another to test if a path
65 // can be traveled, used for waypoint linking and havocbot
68 float navigation_testtracewalk;
69 float tracewalk(entity e, vector start, vector m1, vector m2, vector end, float movemode)
75 local float totaldist;
78 local float ignorehazards;
81 #ifdef DEBUG_TRACEWALK
89 dist = totaldist = vlen(move);
90 dir = normalize(move);
92 ignorehazards = FALSE;
94 // Analyze starting point
95 traceline(start, start, MOVE_NORMAL, e);
96 if (trace_dpstartcontents & (DPCONTENTS_SLIME | DPCONTENTS_LAVA))
100 traceline( start, start + '0 0 -65536', MOVE_NORMAL, e);
101 if (trace_dpstartcontents & (DPCONTENTS_SLIME | DPCONTENTS_LAVA))
103 ignorehazards = TRUE;
107 tracebox(start, m1, m2, start, MOVE_NOMONSTERS, e);
108 if (trace_startsolid)
111 #ifdef DEBUG_TRACEWALK
112 debugnodestatus(start, DEBUG_NODE_FAIL);
118 yaw = vectoyaw(move);
122 if (boxesoverlap(end, end, org + m1 + '-1 -1 -1', org + m2 + '1 1 1'))
125 #ifdef DEBUG_TRACEWALK
126 debugnodestatus(org, DEBUG_NODE_SUCCESS);
130 #ifdef DEBUG_TRACEWALK
138 dist = dist - stepdist;
139 traceline(org, org, MOVE_NORMAL, e);
142 if (trace_dpstartcontents & (DPCONTENTS_SLIME | DPCONTENTS_LAVA))
144 // hazards blocking path
145 #ifdef DEBUG_TRACEWALK
146 debugnodestatus(org, DEBUG_NODE_FAIL);
151 if (trace_dpstartcontents & DPCONTENTS_LIQUIDSMASK)
153 move = normalize(end - org);
154 tracebox(org, m1, m2, org + move * stepdist, movemode, e);
156 #ifdef DEBUG_TRACEWALK
157 debugnode(trace_endpos);
160 if (trace_fraction < 1)
163 org = trace_endpos - normalize(org - trace_endpos) * stepdist;
164 for(; org_z < end_z + self.maxs_z; org_z += stepdist)
166 #ifdef DEBUG_TRACEWALK
169 if(pointcontents(org) == CONTENT_EMPTY)
173 if not (pointcontents(org + '0 0 1') == CONTENT_EMPTY)
175 #ifdef DEBUG_TRACEWALK
176 debugnodestatus(org, DEBUG_NODE_FAIL);
188 move = dir * stepdist + org;
189 tracebox(org, m1, m2, move, movemode, e);
191 #ifdef DEBUG_TRACEWALK
192 debugnode(trace_endpos);
196 if (trace_fraction < 1)
198 // check if we can walk over this obstacle
199 tracebox(org + stepheightvec, m1, m2, move + stepheightvec, movemode, e);
200 if (trace_fraction < 1 || trace_startsolid)
202 #ifdef DEBUG_TRACEWALK
203 debugnodestatus(trace_endpos, DEBUG_NODE_WARNING);
207 traceline( org, move, movemode, e);
208 if ( trace_ent.classname == "door_rotating" || trace_ent.classname == "door")
210 local vector nextmove;
212 while(trace_ent.classname == "door_rotating" || trace_ent.classname == "door")
214 nextmove = move + (dir * stepdist);
215 traceline( move, nextmove, movemode, e);
221 #ifdef DEBUG_TRACEWALK
222 debugnodestatus(trace_endpos, DEBUG_NODE_FAIL);
224 return FALSE; // failed
233 // trace down from stepheight as far as possible and move there,
234 // if this starts in solid we try again without the stepup, and
235 // if that also fails we assume it is a wall
236 // (this is the same logic as the Quake walkmove function used)
237 tracebox(move, m1, m2, move + '0 0 -65536', movemode, e);
239 // moved successfully
243 c = pointcontents(org + '0 0 1');
244 if not(c == CONTENT_WATER || c == CONTENT_LAVA || c == CONTENT_SLIME)
254 // moved but didn't arrive at the intended destination
255 #ifdef DEBUG_TRACEWALK
256 debugnodestatus(org, DEBUG_NODE_FAIL);
263 // grenade tracing to decide the best pitch to fire at
266 entity tracetossfaketarget;
268 // traces multiple trajectories to find one that will impact the target
269 // 'end' vector is the place it aims for,
270 // returns TRUE only if it hit targ (don't target non-solid entities)
271 vector findtrajectory_velocity;
272 float findtrajectorywithleading(vector org, vector m1, vector m2, entity targ, float shotspeed, float shotspeedupward, float maxtime, float shotdelay, entity ignore)
274 local float c, savesolid, shottime;
275 local vector dir, end, v;
277 return FALSE; // could cause division by zero if calculated
278 if (targ.solid < SOLID_BBOX) // SOLID_NOT and SOLID_TRIGGER
279 return FALSE; // could never hit it
281 tracetossent = spawn();
282 tracetossent.owner = ignore;
283 setsize(tracetossent, m1, m2);
284 savesolid = targ.solid;
285 targ.solid = SOLID_NOT;
286 shottime = ((vlen(targ.origin - org) / shotspeed) + shotdelay);
287 v = targ.velocity * shottime + targ.origin;
288 tracebox(targ.origin, targ.mins, targ.maxs, v, FALSE, targ);
290 end = v + (targ.mins + targ.maxs) * 0.5;
291 if ((vlen(end - org) / shotspeed + 0.2) > maxtime)
294 targ.solid = savesolid;
298 if (!tracetossfaketarget)
299 tracetossfaketarget = spawn();
300 tracetossfaketarget.solid = savesolid;
301 tracetossfaketarget.movetype = targ.movetype;
302 setmodel(tracetossfaketarget, targ.model); // no low precision
303 tracetossfaketarget.model = targ.model;
304 tracetossfaketarget.modelindex = targ.modelindex;
305 setsize(tracetossfaketarget, targ.mins, targ.maxs);
306 setorigin(tracetossfaketarget, v);
309 dir = normalize(end - org);
310 while (c < 10) // 10 traces
312 setorigin(tracetossent, org); // reset
313 tracetossent.velocity = findtrajectory_velocity = normalize(dir) * shotspeed + shotspeedupward * '0 0 1';
314 tracetoss(tracetossent, ignore); // love builtin functions...
315 if (trace_ent == tracetossfaketarget) // done
317 targ.solid = savesolid;
320 tracetossfaketarget.solid = SOLID_NOT;
321 tracetossfaketarget.movetype = MOVETYPE_NONE;
322 tracetossfaketarget.model = "";
323 tracetossfaketarget.modelindex = 0;
324 // relink to remove it from physics considerations
325 setorigin(tracetossfaketarget, v);
329 dir_z = dir_z + 0.1; // aim up a little more
332 targ.solid = savesolid;
335 tracetossfaketarget.solid = SOLID_NOT;
336 tracetossfaketarget.movetype = MOVETYPE_NONE;
337 tracetossfaketarget.model = "";
338 tracetossfaketarget.modelindex = 0;
339 // relink to remove it from physics considerations
340 setorigin(tracetossfaketarget, v);
342 // leave a valid one even if it won't reach
343 findtrajectory_velocity = normalize(end - org) * shotspeed + shotspeedupward * '0 0 1';
351 .void(float t, float f1, float f2, entity e1, vector v1, vector v2, vector v3, vector v4) lag_func;
353 // upto 5 queued messages
357 .entity lag1_entity1;
366 .entity lag2_entity1;
375 .entity lag3_entity1;
384 .entity lag4_entity1;
393 .entity lag5_entity1;
401 if (self.lag1_time) if (time > self.lag1_time) {self.lag_func(self.lag1_time, self.lag1_float1, self.lag1_float2, self.lag1_entity1, self.lag1_vec1, self.lag1_vec2, self.lag1_vec3, self.lag1_vec4);self.lag1_time = 0;}
402 if (self.lag2_time) if (time > self.lag2_time) {self.lag_func(self.lag2_time, self.lag2_float1, self.lag2_float2, self.lag2_entity1, self.lag2_vec1, self.lag2_vec2, self.lag2_vec3, self.lag2_vec4);self.lag2_time = 0;}
403 if (self.lag3_time) if (time > self.lag3_time) {self.lag_func(self.lag3_time, self.lag3_float1, self.lag3_float2, self.lag3_entity1, self.lag3_vec1, self.lag3_vec2, self.lag3_vec3, self.lag3_vec4);self.lag3_time = 0;}
404 if (self.lag4_time) if (time > self.lag4_time) {self.lag_func(self.lag4_time, self.lag4_float1, self.lag4_float2, self.lag4_entity1, self.lag4_vec1, self.lag4_vec2, self.lag4_vec3, self.lag4_vec4);self.lag4_time = 0;}
405 if (self.lag5_time) if (time > self.lag5_time) {self.lag_func(self.lag5_time, self.lag5_float1, self.lag5_float2, self.lag5_entity1, self.lag5_vec1, self.lag5_vec2, self.lag5_vec3, self.lag5_vec4);self.lag5_time = 0;}
408 float lag_additem(float t, float f1, float f2, entity e1, vector v1, vector v2, vector v3, vector v4)
410 if (self.lag1_time == 0) {self.lag1_time = t;self.lag1_float1 = f1;self.lag1_float2 = f2;self.lag1_entity1 = e1;self.lag1_vec1 = v1;self.lag1_vec2 = v2;self.lag1_vec3 = v3;self.lag1_vec4 = v4;return TRUE;}
411 if (self.lag2_time == 0) {self.lag2_time = t;self.lag2_float1 = f1;self.lag2_float2 = f2;self.lag2_entity1 = e1;self.lag2_vec1 = v1;self.lag2_vec2 = v2;self.lag2_vec3 = v3;self.lag2_vec4 = v4;return TRUE;}
412 if (self.lag3_time == 0) {self.lag3_time = t;self.lag3_float1 = f1;self.lag3_float2 = f2;self.lag3_entity1 = e1;self.lag3_vec1 = v1;self.lag3_vec2 = v2;self.lag3_vec3 = v3;self.lag3_vec4 = v4;return TRUE;}
413 if (self.lag4_time == 0) {self.lag4_time = t;self.lag4_float1 = f1;self.lag4_float2 = f2;self.lag4_entity1 = e1;self.lag4_vec1 = v1;self.lag4_vec2 = v2;self.lag4_vec3 = v3;self.lag4_vec4 = v4;return TRUE;}
414 if (self.lag5_time == 0) {self.lag5_time = t;self.lag5_float1 = f1;self.lag5_float2 = f2;self.lag5_entity1 = e1;self.lag5_vec1 = v1;self.lag5_vec2 = v2;self.lag5_vec3 = v3;self.lag5_vec4 = v4;return TRUE;}
415 // no room for it (what is the best thing to do here??)
420 // Random skill system
421 .float bot_thinkskill;
422 .float bot_mouseskill;
423 .float bot_predictionskill;
424 .float bot_offsetskill;
427 // spawnfunc_waypoint navigation system
429 // itemscore = (howmuchmoreIwant / howmuchIcanwant) / itemdistance
430 // waypointscore = 0.7 / waypointdistance
432 .entity wp00, wp01, wp02, wp03, wp04, wp05, wp06, wp07;
433 .entity wp08, wp09, wp10, wp11, wp12, wp13, wp14, wp15;
434 .entity wp16, wp17, wp18, wp19, wp20, wp21, wp22, wp23, wp24, wp25, wp26, wp27, wp28, wp29, wp30, wp31;
435 .float wp00mincost, wp01mincost, wp02mincost, wp03mincost, wp04mincost, wp05mincost, wp06mincost, wp07mincost;
436 .float wp08mincost, wp09mincost, wp10mincost, wp11mincost, wp12mincost, wp13mincost, wp14mincost, wp15mincost;
437 .float wp16mincost, wp17mincost, wp18mincost, wp19mincost, wp20mincost, wp21mincost, wp22mincost, wp23mincost, wp24mincost, wp25mincost, wp26mincost, wp27mincost, wp28mincost, wp29mincost, wp30mincost, wp31mincost;
438 .float wpfire, wpcost, wpconsidered;
441 .vector wpnearestpoint;
443 // stack of current goals (the last one of which may be an item or other
444 // desirable object, the rest are typically waypoints to reach it)
445 .entity goalcurrent, goalstack01, goalstack02, goalstack03;
446 .entity goalstack04, goalstack05, goalstack06, goalstack07;
447 .entity goalstack08, goalstack09, goalstack10, goalstack11;
448 .entity goalstack12, goalstack13, goalstack14, goalstack15;
449 .entity goalstack16, goalstack17, goalstack18, goalstack19;
450 .entity goalstack20, goalstack21, goalstack22, goalstack23;
451 .entity goalstack24, goalstack25, goalstack26, goalstack27;
452 .entity goalstack28, goalstack29, goalstack30, goalstack31;
454 .entity nearestwaypoint;
455 .float nearestwaypointtimeout;
457 // used during navigation_goalrating_begin/end sessions
458 #define MAX_BESTGOALS 3
459 float bestgoalswindex;
460 float bestgoalsrindex;
461 float navigation_bestrating;
462 entity navigation_bestgoals[MAX_BESTGOALS];
463 .float navigation_hasgoals;
465 /////////////////////////////////////////////////////////////////////////////
466 // spawnfunc_waypoint management
467 /////////////////////////////////////////////////////////////////////////////
469 // waypoints with this flag are not saved, they are automatically generated
470 // waypoints like jump pads, teleporters, and items
471 float WAYPOINTFLAG_GENERATED = 8388608;
472 float WAYPOINTFLAG_ITEM = 4194304;
473 float WAYPOINTFLAG_TELEPORT = 2097152;
474 float WAYPOINTFLAG_NORELINK = 1048576;
475 float WAYPOINTFLAG_PERSONAL = 524288;
477 // add a new link to the spawnfunc_waypoint, replacing the furthest link it already has
478 void waypoint_addlink(entity from, entity to)
484 if (from.wpflags & WAYPOINTFLAG_NORELINK)
487 if (from.wp00 == to) return;if (from.wp01 == to) return;if (from.wp02 == to) return;if (from.wp03 == to) return;
488 if (from.wp04 == to) return;if (from.wp05 == to) return;if (from.wp06 == to) return;if (from.wp07 == to) return;
489 if (from.wp08 == to) return;if (from.wp09 == to) return;if (from.wp10 == to) return;if (from.wp11 == to) return;
490 if (from.wp12 == to) return;if (from.wp13 == to) return;if (from.wp14 == to) return;if (from.wp15 == to) return;
491 if (from.wp16 == to) return;if (from.wp17 == to) return;if (from.wp18 == to) return;if (from.wp19 == to) return;
492 if (from.wp20 == to) return;if (from.wp21 == to) return;if (from.wp22 == to) return;if (from.wp23 == to) return;
493 if (from.wp24 == to) return;if (from.wp25 == to) return;if (from.wp26 == to) return;if (from.wp27 == to) return;
494 if (from.wp28 == to) return;if (from.wp29 == to) return;if (from.wp30 == to) return;if (from.wp31 == to) return;
496 if (to.wpisbox || from.wpisbox)
498 // if either is a box we have to find the nearest points on them to
499 // calculate the distance properly
500 local vector v1, v2, m1, m2;
504 v1_x = bound(m1_x, v1_x, m2_x);
505 v1_y = bound(m1_y, v1_y, m2_y);
506 v1_z = bound(m1_z, v1_z, m2_z);
510 v2_x = bound(m1_x, v2_x, m2_x);
511 v2_y = bound(m1_y, v2_y, m2_y);
512 v2_z = bound(m1_z, v2_z, m2_z);
517 c = vlen(to.origin - from.origin);
519 if (from.wp31mincost < c) return;
520 if (from.wp30mincost < c) {from.wp31 = to;from.wp31mincost = c;return;} from.wp31 = from.wp30;from.wp31mincost = from.wp30mincost;
521 if (from.wp29mincost < c) {from.wp30 = to;from.wp30mincost = c;return;} from.wp30 = from.wp29;from.wp30mincost = from.wp29mincost;
522 if (from.wp28mincost < c) {from.wp29 = to;from.wp29mincost = c;return;} from.wp29 = from.wp28;from.wp29mincost = from.wp28mincost;
523 if (from.wp27mincost < c) {from.wp28 = to;from.wp28mincost = c;return;} from.wp28 = from.wp27;from.wp28mincost = from.wp27mincost;
524 if (from.wp26mincost < c) {from.wp27 = to;from.wp27mincost = c;return;} from.wp27 = from.wp26;from.wp27mincost = from.wp26mincost;
525 if (from.wp25mincost < c) {from.wp26 = to;from.wp26mincost = c;return;} from.wp26 = from.wp25;from.wp26mincost = from.wp25mincost;
526 if (from.wp24mincost < c) {from.wp25 = to;from.wp25mincost = c;return;} from.wp25 = from.wp24;from.wp25mincost = from.wp24mincost;
527 if (from.wp23mincost < c) {from.wp24 = to;from.wp24mincost = c;return;} from.wp24 = from.wp23;from.wp24mincost = from.wp23mincost;
528 if (from.wp22mincost < c) {from.wp23 = to;from.wp23mincost = c;return;} from.wp23 = from.wp22;from.wp23mincost = from.wp22mincost;
529 if (from.wp21mincost < c) {from.wp22 = to;from.wp22mincost = c;return;} from.wp22 = from.wp21;from.wp22mincost = from.wp21mincost;
530 if (from.wp20mincost < c) {from.wp21 = to;from.wp21mincost = c;return;} from.wp21 = from.wp20;from.wp21mincost = from.wp20mincost;
531 if (from.wp19mincost < c) {from.wp20 = to;from.wp20mincost = c;return;} from.wp20 = from.wp19;from.wp20mincost = from.wp19mincost;
532 if (from.wp18mincost < c) {from.wp19 = to;from.wp19mincost = c;return;} from.wp19 = from.wp18;from.wp19mincost = from.wp18mincost;
533 if (from.wp17mincost < c) {from.wp18 = to;from.wp18mincost = c;return;} from.wp18 = from.wp17;from.wp18mincost = from.wp17mincost;
534 if (from.wp16mincost < c) {from.wp17 = to;from.wp17mincost = c;return;} from.wp17 = from.wp16;from.wp17mincost = from.wp16mincost;
535 if (from.wp15mincost < c) {from.wp16 = to;from.wp16mincost = c;return;} from.wp16 = from.wp15;from.wp16mincost = from.wp15mincost;
536 if (from.wp14mincost < c) {from.wp15 = to;from.wp15mincost = c;return;} from.wp15 = from.wp14;from.wp15mincost = from.wp14mincost;
537 if (from.wp13mincost < c) {from.wp14 = to;from.wp14mincost = c;return;} from.wp14 = from.wp13;from.wp14mincost = from.wp13mincost;
538 if (from.wp12mincost < c) {from.wp13 = to;from.wp13mincost = c;return;} from.wp13 = from.wp12;from.wp13mincost = from.wp12mincost;
539 if (from.wp11mincost < c) {from.wp12 = to;from.wp12mincost = c;return;} from.wp12 = from.wp11;from.wp12mincost = from.wp11mincost;
540 if (from.wp10mincost < c) {from.wp11 = to;from.wp11mincost = c;return;} from.wp11 = from.wp10;from.wp11mincost = from.wp10mincost;
541 if (from.wp09mincost < c) {from.wp10 = to;from.wp10mincost = c;return;} from.wp10 = from.wp09;from.wp10mincost = from.wp09mincost;
542 if (from.wp08mincost < c) {from.wp09 = to;from.wp09mincost = c;return;} from.wp09 = from.wp08;from.wp09mincost = from.wp08mincost;
543 if (from.wp07mincost < c) {from.wp08 = to;from.wp08mincost = c;return;} from.wp08 = from.wp07;from.wp08mincost = from.wp07mincost;
544 if (from.wp06mincost < c) {from.wp07 = to;from.wp07mincost = c;return;} from.wp07 = from.wp06;from.wp07mincost = from.wp06mincost;
545 if (from.wp05mincost < c) {from.wp06 = to;from.wp06mincost = c;return;} from.wp06 = from.wp05;from.wp06mincost = from.wp05mincost;
546 if (from.wp04mincost < c) {from.wp05 = to;from.wp05mincost = c;return;} from.wp05 = from.wp04;from.wp05mincost = from.wp04mincost;
547 if (from.wp03mincost < c) {from.wp04 = to;from.wp04mincost = c;return;} from.wp04 = from.wp03;from.wp04mincost = from.wp03mincost;
548 if (from.wp02mincost < c) {from.wp03 = to;from.wp03mincost = c;return;} from.wp03 = from.wp02;from.wp03mincost = from.wp02mincost;
549 if (from.wp01mincost < c) {from.wp02 = to;from.wp02mincost = c;return;} from.wp02 = from.wp01;from.wp02mincost = from.wp01mincost;
550 if (from.wp00mincost < c) {from.wp01 = to;from.wp01mincost = c;return;} from.wp01 = from.wp00;from.wp01mincost = from.wp00mincost;
551 from.wp00 = to;from.wp00mincost = c;return;
554 // fields you can query using prvm_global server to get some statistics about waypoint linking culling
555 float relink_total, relink_walkculled, relink_pvsculled, relink_lengthculled;
557 // relink this spawnfunc_waypoint
558 // (precompile a list of all reachable waypoints from this spawnfunc_waypoint)
560 void waypoint_think()
563 local vector sv, sm1, sm2, ev, em1, em2, dv;
564 //dprint("waypoint_think wpisbox = ", ftos(self.wpisbox), "\n");
565 sm1 = self.origin + self.mins;
566 sm2 = self.origin + self.maxs;
567 stepheightvec = cvar("sv_stepheight") * '0 0 1';
568 for(e = world; (e = find(e, classname, "waypoint")); )
570 if (boxesoverlap(self.absmin, self.absmax, e.absmin, e.absmax))
572 waypoint_addlink(self, e);
573 waypoint_addlink(e, self);
578 if(!checkpvs(self.origin, e))
584 sv_x = bound(sm1_x, sv_x, sm2_x);
585 sv_y = bound(sm1_y, sv_y, sm2_y);
586 sv_z = bound(sm1_z, sv_z, sm2_z);
588 em1 = e.origin + e.mins;
589 em2 = e.origin + e.maxs;
590 ev_x = bound(em1_x, ev_x, em2_x);
591 ev_y = bound(em1_y, ev_y, em2_y);
592 ev_z = bound(em1_z, ev_z, em2_z);
595 if (vlen(dv) >= 1050) // max search distance in XY
597 ++relink_lengthculled;
600 navigation_testtracewalk = 0;
603 tracebox(sv - PL_MIN_z * '0 0 1', PL_MIN, PL_MAX, sv, FALSE, self);
604 if (!trace_startsolid)
606 //dprint("sv deviation", vtos(trace_endpos - sv), "\n");
607 sv = trace_endpos + '0 0 1';
612 tracebox(ev - PL_MIN_z * '0 0 1', PL_MIN, PL_MAX, ev, FALSE, e);
613 if (!trace_startsolid)
615 //dprint("ev deviation", vtos(trace_endpos - ev), "\n");
616 ev = trace_endpos + '0 0 1';
619 //traceline(self.origin, e.origin, FALSE, world);
620 //if (trace_fraction == 1)
621 if (!self.wpisbox && tracewalk(self, sv, PL_MIN, PL_MAX, ev, MOVE_NOMONSTERS))
622 waypoint_addlink(self, e);
624 relink_walkculled += 0.5;
625 if (!e.wpisbox && tracewalk(e, ev, PL_MIN, PL_MAX, sv, MOVE_NOMONSTERS))
626 waypoint_addlink(e, self);
628 relink_walkculled += 0.5;
631 navigation_testtracewalk = 0;
634 void waypoint_clearlinks(entity wp)
636 // clear links to other waypoints
639 wp.wp00 = wp.wp01 = wp.wp02 = wp.wp03 = wp.wp04 = wp.wp05 = wp.wp06 = wp.wp07 = world;wp.wp00mincost = wp.wp01mincost = wp.wp02mincost = wp.wp03mincost = wp.wp04mincost = wp.wp05mincost = wp.wp06mincost = wp.wp07mincost = f;
640 wp.wp08 = wp.wp09 = wp.wp10 = wp.wp11 = wp.wp12 = wp.wp13 = wp.wp14 = wp.wp15 = world;wp.wp08mincost = wp.wp09mincost = wp.wp10mincost = wp.wp11mincost = wp.wp12mincost = wp.wp13mincost = wp.wp14mincost = wp.wp15mincost = f;
641 wp.wp16 = wp.wp17 = wp.wp18 = wp.wp19 = wp.wp20 = wp.wp21 = wp.wp22 = wp.wp23 = world;wp.wp16mincost = wp.wp17mincost = wp.wp18mincost = wp.wp19mincost = wp.wp20mincost = wp.wp21mincost = wp.wp22mincost = wp.wp23mincost = f;
642 wp.wp24 = wp.wp25 = wp.wp26 = wp.wp27 = wp.wp28 = wp.wp29 = wp.wp30 = wp.wp31 = world;wp.wp24mincost = wp.wp25mincost = wp.wp26mincost = wp.wp27mincost = wp.wp28mincost = wp.wp29mincost = wp.wp30mincost = wp.wp31mincost = f;
645 // tell a spawnfunc_waypoint to relink
646 void waypoint_schedulerelink(entity wp)
650 // TODO: add some sort of visible box in edit mode for box waypoints
651 if (cvar("g_waypointeditor"))
656 setmodel(wp, "models/runematch/rune.mdl"); wp.effects = EF_LOWPRECISION;
658 if (wp.wpflags & WAYPOINTFLAG_ITEM)
659 wp.colormod = '1 0 0';
660 else if (wp.wpflags & WAYPOINTFLAG_GENERATED)
661 wp.colormod = '1 1 0';
663 wp.colormod = '1 1 1';
667 wp.wpisbox = vlen(wp.size) > 0;
670 if (!(wp.wpflags & WAYPOINTFLAG_NORELINK))
671 waypoint_clearlinks(wp);
672 // schedule an actual relink on next frame
673 wp.think = waypoint_think;
675 wp.effects = EF_LOWPRECISION;
678 // create a new spawnfunc_waypoint and automatically link it to other waypoints, and link
679 // them back to it as well
680 // (suitable for spawnfunc_waypoint editor)
681 entity waypoint_spawn(vector m1, vector m2, float f)
685 w = find(world, classname, "waypoint");
687 if not(f & WAYPOINTFLAG_PERSONAL)
690 // if a matching spawnfunc_waypoint already exists, don't add a duplicate
691 if (boxesoverlap(m1, m2, w.absmin, w.absmax))
693 w = find(w, classname, "waypoint");
697 w.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_PLAYERCLIP;
698 w.classname = "waypoint";
700 setorigin(w, (m1 + m2) * 0.5);
701 setsize(w, m1 - w.origin, m2 - w.origin);
702 if (vlen(w.size) > 0)
705 if(!(f & WAYPOINTFLAG_GENERATED))
708 traceline(w.origin + '0 0 1', w.origin + PL_MIN_z * '0 0 1' - '0 0 1', MOVE_NOMONSTERS, w);
709 if (trace_fraction < 1)
710 setorigin(w, trace_endpos - PL_MIN_z * '0 0 1');
712 // check if the start position is stuck
713 tracebox(w.origin, PL_MIN + '-1 -1 -1', PL_MAX + '1 1 1', w.origin, MOVE_NOMONSTERS, w);
714 if (trace_startsolid)
716 org = w.origin + '0 0 26';
717 tracebox(org, PL_MIN, PL_MAX, w.origin, MOVE_WORLDONLY, w);
720 org = w.origin + '2 2 2';
721 tracebox(org, PL_MIN, PL_MAX, w.origin, MOVE_WORLDONLY, w);
724 org = w.origin + '-2 -2 2';
725 tracebox(org, PL_MIN, PL_MAX, w.origin, MOVE_WORLDONLY, w);
728 org = w.origin + '-2 2 2';
729 tracebox(org, PL_MIN, PL_MAX, w.origin, MOVE_WORLDONLY, w);
732 org = w.origin + '2 -2 2';
733 tracebox(org, PL_MIN, PL_MAX, w.origin, MOVE_WORLDONLY, w);
736 // this WP is in solid, refuse it
737 dprint("Killed a waypoint that was stuck in solid at ", vtos(org), "\n");
745 setorigin(w, org * 0.05 + trace_endpos * 0.95); // don't trust the trace fully
748 tracebox(w.origin, PL_MIN, PL_MAX, w.origin - '0 0 128', MOVE_WORLDONLY, w);
751 dprint("Killed a waypoint that was stuck in solid ", vtos(w.origin), "\n");
757 if(trace_fraction == 1)
759 dprint("Killed a waypoint that was stuck in air at ", vtos(w.origin), "\n");
763 trace_endpos_z += 0.1; // don't trust the trace fully
764 // dprint("Moved waypoint at ", vtos(w.origin), " by ", ftos(vlen(w.origin - trace_endpos)));
765 // dprint(" direction: ", vtos((trace_endpos - w.origin)), "\n");
766 setorigin(w, trace_endpos);
770 waypoint_clearlinks(w);
771 //waypoint_schedulerelink(w);
775 // spawnfunc_waypoint map entity
776 void spawnfunc_waypoint()
778 setorigin(self, self.origin);
779 // schedule a relink after other waypoints have had a chance to spawn
780 waypoint_clearlinks(self);
781 //waypoint_schedulerelink(self);
784 // remove a spawnfunc_waypoint, and schedule all neighbors to relink
785 void waypoint_remove(entity e)
787 // tell all linked waypoints that they need to relink
788 waypoint_schedulerelink(e.wp00);
789 waypoint_schedulerelink(e.wp01);
790 waypoint_schedulerelink(e.wp02);
791 waypoint_schedulerelink(e.wp03);
792 waypoint_schedulerelink(e.wp04);
793 waypoint_schedulerelink(e.wp05);
794 waypoint_schedulerelink(e.wp06);
795 waypoint_schedulerelink(e.wp07);
796 waypoint_schedulerelink(e.wp08);
797 waypoint_schedulerelink(e.wp09);
798 waypoint_schedulerelink(e.wp10);
799 waypoint_schedulerelink(e.wp11);
800 waypoint_schedulerelink(e.wp12);
801 waypoint_schedulerelink(e.wp13);
802 waypoint_schedulerelink(e.wp14);
803 waypoint_schedulerelink(e.wp15);
804 waypoint_schedulerelink(e.wp16);
805 waypoint_schedulerelink(e.wp17);
806 waypoint_schedulerelink(e.wp18);
807 waypoint_schedulerelink(e.wp19);
808 waypoint_schedulerelink(e.wp20);
809 waypoint_schedulerelink(e.wp21);
810 waypoint_schedulerelink(e.wp22);
811 waypoint_schedulerelink(e.wp23);
812 waypoint_schedulerelink(e.wp24);
813 waypoint_schedulerelink(e.wp25);
814 waypoint_schedulerelink(e.wp26);
815 waypoint_schedulerelink(e.wp27);
816 waypoint_schedulerelink(e.wp28);
817 waypoint_schedulerelink(e.wp29);
818 waypoint_schedulerelink(e.wp30);
819 waypoint_schedulerelink(e.wp31);
820 // and now remove the spawnfunc_waypoint
824 // empties the map of waypoints
825 void waypoint_removeall()
827 local entity head, next;
828 head = findchain(classname, "waypoint");
837 // tell all waypoints to relink
838 // (is this useful at all?)
839 void waypoint_schedulerelinkall()
842 relink_total = relink_walkculled = relink_pvsculled = relink_lengthculled = 0;
843 head = findchain(classname, "waypoint");
846 waypoint_schedulerelink(head);
851 // Load waypoint links from file
852 float botframe_cachedwaypointlinks;
853 float waypoint_load_links()
855 local string filename, s;
856 local float file, tokens, c, found;
857 local entity wp_from, wp_to;
858 local vector wp_to_pos, wp_from_pos;
859 filename = strcat("maps/", mapname);
860 filename = strcat(filename, ".waypoints.cache");
861 file = fopen(filename, FILE_READ);
865 dprint("waypoint links load from ");
877 tokens = tokenizebyseparator(s, "*");
886 wp_from_pos = stov(argv(0));
887 wp_to_pos = stov(argv(1));
889 // Search "from" waypoint
890 if(wp_from.origin!=wp_from_pos)
892 wp_from = findradius(wp_from_pos, 1);
896 if(vlen(wp_from.origin-wp_from_pos)<1)
897 if(wp_from.classname == "waypoint")
902 wp_from = wp_from.chain;
907 // can't find that waypoint
913 // Search "to" waypoint
914 wp_to = findradius(wp_to_pos, 1);
918 if(vlen(wp_to.origin-wp_to_pos)<1)
919 if(wp_to.classname == "waypoint")
929 // can't find that waypoint
935 waypoint_addlink(wp_from, wp_to);
942 dprint(" waypoint links from maps/");
944 dprint(".waypoints.cache\n");
946 botframe_cachedwaypointlinks = TRUE;
950 float botframe_loadedforcedlinks;
951 void waypoint_load_links_hardwired()
953 local string filename, s;
954 local float file, tokens, c, found;
955 local entity wp_from, wp_to;
956 local vector wp_to_pos, wp_from_pos;
957 filename = strcat("maps/", mapname);
958 filename = strcat(filename, ".waypoints.hardwired");
959 file = fopen(filename, FILE_READ);
961 botframe_loadedforcedlinks = TRUE;
965 dprint("waypoint links load from ");
977 if(substring(s, 0, 2)=="//")
980 if(substring(s, 0, 1)=="#")
983 tokens = tokenizebyseparator(s, "*");
988 wp_from_pos = stov(argv(0));
989 wp_to_pos = stov(argv(1));
991 // Search "from" waypoint
992 if(wp_from.origin!=wp_from_pos)
994 wp_from = findradius(wp_from_pos, 1);
998 if(vlen(wp_from.origin-wp_from_pos)<1)
999 if(wp_from.classname == "waypoint")
1004 wp_from = wp_from.chain;
1009 print(strcat("NOTICE: Can not find waypoint at ", vtos(wp_from_pos), ". Path skipped\n"));
1014 // Search "to" waypoint
1015 wp_to = findradius(wp_to_pos, 1);
1019 if(vlen(wp_to.origin-wp_to_pos)<1)
1020 if(wp_to.classname == "waypoint")
1025 wp_to = wp_to.chain;
1030 print(strcat("NOTICE: Can not find waypoint at ", vtos(wp_to_pos), ". Path skipped\n"));
1035 waypoint_addlink(wp_from, wp_to);
1042 dprint(" waypoint links from maps/");
1044 dprint(".waypoints.hardwired\n");
1048 // Save all waypoint links to a file
1049 void waypoint_save_links()
1051 local string filename, s;
1052 local float file, c, i;
1053 local entity w, link;
1054 filename = strcat("maps/", mapname);
1055 filename = strcat(filename, ".waypoints.cache");
1056 file = fopen(filename, FILE_WRITE);
1059 print("waypoint links save to ");
1064 w = findchain(classname, "waypoint");
1072 // for i in $(seq -w 0 31); do echo "case $i:link = w.wp$i; break;"; done;
1073 case 00:link = w.wp00; break;
1074 case 01:link = w.wp01; break;
1075 case 02:link = w.wp02; break;
1076 case 03:link = w.wp03; break;
1077 case 04:link = w.wp04; break;
1078 case 05:link = w.wp05; break;
1079 case 06:link = w.wp06; break;
1080 case 07:link = w.wp07; break;
1081 case 08:link = w.wp08; break;
1082 case 09:link = w.wp09; break;
1083 case 10:link = w.wp10; break;
1084 case 11:link = w.wp11; break;
1085 case 12:link = w.wp12; break;
1086 case 13:link = w.wp13; break;
1087 case 14:link = w.wp14; break;
1088 case 15:link = w.wp15; break;
1089 case 16:link = w.wp16; break;
1090 case 17:link = w.wp17; break;
1091 case 18:link = w.wp18; break;
1092 case 19:link = w.wp19; break;
1093 case 20:link = w.wp20; break;
1094 case 21:link = w.wp21; break;
1095 case 22:link = w.wp22; break;
1096 case 23:link = w.wp23; break;
1097 case 24:link = w.wp24; break;
1098 case 25:link = w.wp25; break;
1099 case 26:link = w.wp26; break;
1100 case 27:link = w.wp27; break;
1101 case 28:link = w.wp28; break;
1102 case 29:link = w.wp29; break;
1103 case 30:link = w.wp30; break;
1104 case 31:link = w.wp31; break;
1110 s = strcat(vtos(w.origin), "*", vtos(link.origin), "\n");
1117 botframe_cachedwaypointlinks = TRUE;
1121 print(" waypoints links to maps/");
1123 print(".waypoints.cache\n");
1126 // save waypoints to gamedir/data/maps/mapname.waypoints
1127 void waypoint_saveall()
1129 local string filename, s;
1130 local float file, c;
1132 filename = strcat("maps/", mapname);
1133 filename = strcat(filename, ".waypoints");
1134 file = fopen(filename, FILE_WRITE);
1138 w = findchain(classname, "waypoint");
1141 if (!(w.wpflags & WAYPOINTFLAG_GENERATED))
1143 s = strcat(vtos(w.origin + w.mins), "\n");
1144 s = strcat(s, vtos(w.origin + w.maxs));
1145 s = strcat(s, "\n");
1146 s = strcat(s, ftos(w.wpflags));
1147 s = strcat(s, "\n");
1156 bprint(" waypoints to maps/");
1158 bprint(".waypoints\n");
1162 bprint("waypoint save to ");
1164 bprint(" failed\n");
1166 waypoint_save_links();
1167 botframe_loadedforcedlinks = FALSE;
1170 // load waypoints from file
1171 float waypoint_loadall()
1173 local string filename, s;
1174 local float file, cwp, cwb, fl;
1175 local vector m1, m2;
1178 filename = strcat("maps/", mapname);
1179 filename = strcat(filename, ".waypoints");
1180 file = fopen(filename, FILE_READ);
1197 waypoint_spawn(m1, m2, fl);
1206 dprint(" waypoints and ");
1208 dprint(" wayboxes from maps/");
1210 dprint(".waypoints\n");
1214 dprint("waypoint load from ");
1216 dprint(" failed\n");
1221 void waypoint_spawnforitem(entity e)
1226 if(!bot_waypoints_for_items)
1230 org = (e.absmax + e.absmin) * 0.5;
1232 // Fix the waypoint altitude if necessary
1233 traceline(org, org + '0 0 -65535', TRUE, e);
1235 org_z - trace_endpos_z > PL_MAX_z - PL_MIN_z + 10 // If middle of entiy is above player heigth
1236 || org_z - trace_endpos_z < (PL_MAX_z - PL_MIN_z) * 0.5 // or below half player height
1238 org_z = trace_endpos_z + PL_MAX_z - PL_MIN_z;
1240 // TODO: Cleaner solution
1241 if(e.classname!="item_flag_team")
1242 e.nearestwaypointtimeout = time + 1000000000;
1244 // don't spawn an item spawnfunc_waypoint if it already exists
1245 w = findchain(classname, "waypoint");
1250 if (boxesoverlap(org, org, w.absmin, w.absmax))
1252 e.nearestwaypoint = w;
1258 if (vlen(w.origin - org) < 16)
1260 e.nearestwaypoint = w;
1266 e.nearestwaypoint = waypoint_spawn(org, org, WAYPOINTFLAG_GENERATED | WAYPOINTFLAG_ITEM);
1269 void waypoint_spawnforteleporter(entity e, vector destination, float timetaken)
1273 w = waypoint_spawn(e.absmin, e.absmax, WAYPOINTFLAG_GENERATED | WAYPOINTFLAG_TELEPORT | WAYPOINTFLAG_NORELINK);
1274 dw = waypoint_spawn(destination, destination, WAYPOINTFLAG_GENERATED);
1275 // one way link to the destination
1277 w.wp00mincost = timetaken; // this is just for jump pads
1278 // the teleporter's nearest spawnfunc_waypoint is this one
1279 // (teleporters are not goals, so this is probably useless)
1280 e.nearestwaypoint = w;
1281 e.nearestwaypointtimeout = time + 1000000000;
1284 entity waypoint_spawnpersonal(vector position)
1288 w = waypoint_spawn(position, position, WAYPOINTFLAG_GENERATED | WAYPOINTFLAG_PERSONAL);
1289 w.nearestwaypoint = world;
1290 w.nearestwaypointtimeout = 0;
1296 /////////////////////////////////////////////////////////////////////////////
1298 /////////////////////////////////////////////////////////////////////////////
1300 // completely empty the goal stack, used when deciding where to go
1301 void navigation_clearroute()
1303 self.navigation_hasgoals = FALSE;
1304 self.goalcurrent = world;
1305 self.goalstack01 = world;
1306 self.goalstack02 = world;
1307 self.goalstack03 = world;
1308 self.goalstack04 = world;
1309 self.goalstack05 = world;
1310 self.goalstack06 = world;
1311 self.goalstack07 = world;
1312 self.goalstack08 = world;
1313 self.goalstack09 = world;
1314 self.goalstack10 = world;
1315 self.goalstack11 = world;
1316 self.goalstack12 = world;
1317 self.goalstack13 = world;
1318 self.goalstack14 = world;
1319 self.goalstack15 = world;
1320 self.goalstack16 = world;
1321 self.goalstack17 = world;
1322 self.goalstack18 = world;
1323 self.goalstack19 = world;
1324 self.goalstack20 = world;
1325 self.goalstack21 = world;
1326 self.goalstack22 = world;
1327 self.goalstack23 = world;
1328 self.goalstack24 = world;
1329 self.goalstack25 = world;
1330 self.goalstack26 = world;
1331 self.goalstack27 = world;
1332 self.goalstack28 = world;
1333 self.goalstack29 = world;
1334 self.goalstack30 = world;
1335 self.goalstack31 = world;
1338 // add a new goal at the beginning of the stack
1339 // (in other words: add a new prerequisite before going to the later goals)
1340 void navigation_pushroute(entity e)
1342 self.goalstack31 = self.goalstack30;
1343 self.goalstack30 = self.goalstack29;
1344 self.goalstack29 = self.goalstack28;
1345 self.goalstack28 = self.goalstack27;
1346 self.goalstack27 = self.goalstack26;
1347 self.goalstack26 = self.goalstack25;
1348 self.goalstack25 = self.goalstack24;
1349 self.goalstack24 = self.goalstack23;
1350 self.goalstack23 = self.goalstack22;
1351 self.goalstack22 = self.goalstack21;
1352 self.goalstack21 = self.goalstack20;
1353 self.goalstack20 = self.goalstack19;
1354 self.goalstack19 = self.goalstack18;
1355 self.goalstack18 = self.goalstack17;
1356 self.goalstack17 = self.goalstack16;
1357 self.goalstack16 = self.goalstack15;
1358 self.goalstack15 = self.goalstack14;
1359 self.goalstack14 = self.goalstack13;
1360 self.goalstack13 = self.goalstack12;
1361 self.goalstack12 = self.goalstack11;
1362 self.goalstack11 = self.goalstack10;
1363 self.goalstack10 = self.goalstack09;
1364 self.goalstack09 = self.goalstack08;
1365 self.goalstack08 = self.goalstack07;
1366 self.goalstack07 = self.goalstack06;
1367 self.goalstack06 = self.goalstack05;
1368 self.goalstack05 = self.goalstack04;
1369 self.goalstack04 = self.goalstack03;
1370 self.goalstack03 = self.goalstack02;
1371 self.goalstack02 = self.goalstack01;
1372 self.goalstack01 = self.goalcurrent;
1373 self.goalcurrent = e;
1376 // remove first goal from stack
1377 // (in other words: remove a prerequisite for reaching the later goals)
1378 // (used when a spawnfunc_waypoint is reached)
1379 void navigation_poproute()
1381 if(self.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL && self.goalcurrent.owner==self)
1382 remove(self.goalcurrent);
1384 self.goalcurrent = self.goalstack01;
1385 self.goalstack01 = self.goalstack02;
1386 self.goalstack02 = self.goalstack03;
1387 self.goalstack03 = self.goalstack04;
1388 self.goalstack04 = self.goalstack05;
1389 self.goalstack05 = self.goalstack06;
1390 self.goalstack06 = self.goalstack07;
1391 self.goalstack07 = self.goalstack08;
1392 self.goalstack08 = self.goalstack09;
1393 self.goalstack09 = self.goalstack10;
1394 self.goalstack10 = self.goalstack11;
1395 self.goalstack11 = self.goalstack12;
1396 self.goalstack12 = self.goalstack13;
1397 self.goalstack13 = self.goalstack14;
1398 self.goalstack14 = self.goalstack15;
1399 self.goalstack15 = self.goalstack16;
1400 self.goalstack16 = self.goalstack17;
1401 self.goalstack17 = self.goalstack18;
1402 self.goalstack18 = self.goalstack19;
1403 self.goalstack19 = self.goalstack20;
1404 self.goalstack20 = self.goalstack21;
1405 self.goalstack21 = self.goalstack22;
1406 self.goalstack22 = self.goalstack23;
1407 self.goalstack23 = self.goalstack24;
1408 self.goalstack24 = self.goalstack25;
1409 self.goalstack25 = self.goalstack26;
1410 self.goalstack26 = self.goalstack27;
1411 self.goalstack27 = self.goalstack28;
1412 self.goalstack28 = self.goalstack29;
1413 self.goalstack29 = self.goalstack30;
1414 self.goalstack30 = self.goalstack31;
1415 self.goalstack31 = world;
1418 // find the spawnfunc_waypoint near a dynamic goal such as a dropped weapon
1419 entity navigation_findnearestwaypoint(entity ent, float walkfromwp)
1421 local entity waylist, w, best;
1422 local float dist, bestdist;
1423 local vector v, org, pm1, pm2;
1424 pm1 = ent.origin + PL_MIN;
1425 pm2 = ent.origin + PL_MAX;
1426 waylist = findchain(classname, "waypoint");
1428 // do two scans, because box test is cheaper
1432 // if object is touching spawnfunc_waypoint
1433 if (boxesoverlap(pm1, pm2, w.absmin, w.absmax))
1438 org = ent.origin + (ent.mins_z - PL_MIN_z) * '0 0 1';
1440 org = org + ent.tag_entity.origin;
1441 if (navigation_testtracewalk)
1446 // box check failed, try walk
1450 // if object can walk from spawnfunc_waypoint
1453 local vector wm1, wm2;
1454 wm1 = w.origin + w.mins;
1455 wm2 = w.origin + w.maxs;
1456 v_x = bound(wm1_x, org_x, wm2_x);
1457 v_y = bound(wm1_y, org_y, wm2_y);
1458 v_z = bound(wm1_z, org_z, wm2_z);
1462 dist = vlen(v - org);
1463 if (bestdist > dist)
1467 traceline(v, org, TRUE, ent);
1468 if (trace_fraction == 1)
1469 if (tracewalk(ent, v, PL_MIN, PL_MAX, org, MOVE_NORMAL))
1477 traceline(v, org, TRUE, ent);
1478 if (trace_fraction == 1)
1479 if (tracewalk(ent, org, PL_MIN, PL_MAX, v, MOVE_NORMAL))
1491 // finds the waypoints near the bot initiating a navigation query
1492 float navigation_markroutes_nearestwaypoints(entity waylist, float maxdist)
1495 local vector v, m1, m2, diff;
1497 // navigation_testtracewalk = TRUE;
1502 if (!head.wpconsidered)
1506 m1 = head.origin + head.mins;
1507 m2 = head.origin + head.maxs;
1509 v_x = bound(m1_x, v_x, m2_x);
1510 v_y = bound(m1_y, v_y, m2_y);
1511 v_z = bound(m1_z, v_z, m2_z);
1515 diff = v - self.origin;
1516 diff_z = max(0, diff_z);
1517 if (vlen(diff) < maxdist)
1519 head.wpconsidered = TRUE;
1520 if (tracewalk(self, self.origin, self.mins, self.maxs, v, MOVE_NORMAL))
1522 head.wpnearestpoint = v;
1523 head.wpcost = vlen(v - self.origin) + head.dmg;
1532 //navigation_testtracewalk = FALSE;
1536 // updates a path link if a spawnfunc_waypoint link is better than the current one
1537 void navigation_markroutes_checkwaypoint(entity w, entity wp, float cost2, vector p)
1546 v_x = bound(m1_x, p_x, m2_x);
1547 v_y = bound(m1_y, p_y, m2_y);
1548 v_z = bound(m1_z, p_z, m2_z);
1552 cost2 = cost2 + vlen(v);
1553 if (wp.wpcost > cost2)
1558 wp.wpnearestpoint = v;
1562 // queries the entire spawnfunc_waypoint network for pathes leading away from the bot
1563 void navigation_markroutes()
1565 local entity w, wp, waylist;
1566 local float searching, cost, cost2;
1568 w = waylist = findchain(classname, "waypoint");
1571 w.wpconsidered = FALSE;
1572 w.wpnearestpoint = '0 0 0';
1573 w.wpcost = 10000000;
1579 // try a short range search for the nearest waypoints, and expand the search repeatedly if none are found
1580 // as this search is expensive we will use lower values if the bot is on the air
1581 local float i, increment, maxdistance;
1582 if(self.flags & FL_ONGROUND)
1585 maxdistance = 50000;
1593 for(i=increment;!navigation_markroutes_nearestwaypoints(waylist, i)&&i<maxdistance;i+=increment);
1607 p = w.wpnearestpoint;
1608 wp = w.wp00;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + wp.wp00mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
1609 wp = w.wp01;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + wp.wp01mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
1610 wp = w.wp02;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + wp.wp02mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
1611 wp = w.wp03;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + wp.wp03mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
1612 wp = w.wp04;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + wp.wp04mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
1613 wp = w.wp05;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + wp.wp05mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
1614 wp = w.wp06;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + wp.wp06mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
1615 wp = w.wp07;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + wp.wp07mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
1616 wp = w.wp08;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + wp.wp08mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
1617 wp = w.wp09;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + wp.wp09mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
1618 wp = w.wp10;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + wp.wp10mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
1619 wp = w.wp11;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + wp.wp11mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
1620 wp = w.wp12;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + wp.wp12mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
1621 wp = w.wp13;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + wp.wp13mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
1622 wp = w.wp14;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + wp.wp14mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
1623 wp = w.wp15;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + wp.wp15mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
1624 wp = w.wp16;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + wp.wp16mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
1625 wp = w.wp17;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + wp.wp17mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
1626 wp = w.wp18;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + wp.wp18mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
1627 wp = w.wp19;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + wp.wp19mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
1628 wp = w.wp20;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + wp.wp20mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
1629 wp = w.wp21;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + wp.wp21mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
1630 wp = w.wp22;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + wp.wp22mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
1631 wp = w.wp23;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + wp.wp23mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
1632 wp = w.wp24;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + wp.wp24mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
1633 wp = w.wp25;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + wp.wp25mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
1634 wp = w.wp26;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + wp.wp26mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
1635 wp = w.wp27;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + wp.wp27mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
1636 wp = w.wp28;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + wp.wp28mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
1637 wp = w.wp29;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + wp.wp29mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
1638 wp = w.wp30;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + wp.wp30mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
1639 wp = w.wp31;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + wp.wp31mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
1640 }}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
1647 void navigation_bestgoals_reset()
1651 bestgoalswindex = 0;
1652 bestgoalsrindex = 0;
1654 for(i=0;i>MAX_BESTGOALS-1;++i)
1656 navigation_bestgoals[i] = world;
1660 void navigation_add_bestgoal(entity goal)
1662 if(bestgoalsrindex>0)
1666 if(bestgoalsrindex==MAX_BESTGOALS)
1667 bestgoalsrindex = 0;
1670 if(bestgoalswindex==MAX_BESTGOALS)
1673 if(bestgoalsrindex==0)
1677 navigation_bestgoals[bestgoalswindex] = goal;
1682 entity navigation_get_bestgoal()
1686 ent = navigation_bestgoals[bestgoalsrindex];
1687 navigation_bestgoals[bestgoalsrindex] = world;
1691 if(bestgoalsrindex==MAX_BESTGOALS)
1692 bestgoalsrindex = 0;
1697 // updates the best goal according to a weighted calculation of travel cost and item value of a new proposed item
1698 .void() havocbot_role;
1699 void() havocbot_role_ctf_offense;
1700 void navigation_routerating(entity e, float f, float rangebias)
1704 //te_wizspike(e.origin);
1707 // update the cached spawnfunc_waypoint link on a dynamic item entity
1708 if (time > e.nearestwaypointtimeout)
1710 e.nearestwaypoint = navigation_findnearestwaypoint(e, TRUE);
1712 // TODO: Cleaner solution, probably handling this timeout from ctf.qc
1713 if(e.classname=="item_flag_team")
1714 e.nearestwaypointtimeout = time + 2;
1716 e.nearestwaypointtimeout = time + random() * 3 + 5;
1719 //dprint("-- checking ", e.classname, " (with cost ", ftos(e.nearestwaypoint.wpcost), ")\n");
1720 if (e.nearestwaypoint)
1721 if (e.nearestwaypoint.wpcost < 10000000)
1723 //te_wizspike(e.nearestwaypoint.wpnearestpoint);
1724 //dprint(e.classname, " ", ftos(f), "/(1+", ftos((e.nearestwaypoint.wpcost + vlen(e.origin - e.nearestwaypoint.wpnearestpoint))), "/", ftos(rangebias), ") = ");
1725 f = f * rangebias / (rangebias + (e.nearestwaypoint.wpcost + vlen(e.origin - e.nearestwaypoint.wpnearestpoint)));
1726 //if (self.havocbot_role == havocbot_role_ctf_offense)
1727 //dprint("considering ", e.classname, " (with rating ", ftos(f), ")\n");
1729 if (navigation_bestrating < f)
1731 navigation_bestrating = f;
1732 navigation_add_bestgoal(e);
1738 // adds an item to the the goal stack with the path to a given item
1739 float navigation_routetogoal(entity e, vector startposition)
1741 self.goalentity = e;
1743 // if there is no goal, just exit
1747 self.navigation_hasgoals = TRUE;
1749 // put the entity on the goal stack
1750 navigation_pushroute(e);
1752 // if it can reach the goal there is nothing more to do
1753 if (tracewalk(self, startposition, PL_MIN, PL_MAX, e.origin, MOVE_NORMAL))
1756 // see if there are waypoints describing a path to the item
1757 e = e.nearestwaypoint;
1763 // add the spawnfunc_waypoint to the path
1764 navigation_pushroute(e);
1774 void navigation_routetogoals()
1778 navigation_clearroute();
1780 g1 = navigation_get_bestgoal();
1784 g2 = navigation_get_bestgoal();
1788 navigation_routetogoal(g1, self.origin);
1792 if(navigation_routetogoal(g1, g2.origin))
1799 navigation_clearroute();
1805 // removes any currently touching waypoints from the goal stack
1806 // (this is how bots detect if they have reached a goal)
1807 .float lastteleporttime;
1809 void navigation_poptouchedgoals()
1811 local vector org, m1, m2;
1813 m1 = org + self.mins;
1814 m2 = org + self.maxs;
1816 if(self.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT)
1818 if(self.lastteleporttime>0)
1819 if(time-self.lastteleporttime<0.15)
1821 navigation_poproute();
1826 // Loose goal touching check when running
1827 if(self.aistatus & AI_STATUS_RUNNING)
1828 if(self.goalcurrent.classname=="waypoint")
1830 if(vlen(self.origin - self.goalcurrent.origin)<150)
1832 traceline(self.origin + self.view_ofs , self.goalcurrent.origin, TRUE, world);
1833 if(trace_fraction==1)
1835 // Detect personal waypoints
1836 if(self.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
1837 if(self.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL && self.goalcurrent.owner==self)
1839 self.aistatus &~= AI_STATUS_WAYPOINT_PERSONAL_GOING;
1840 self.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_REACHED;
1843 navigation_poproute();
1848 while (self.goalcurrent && boxesoverlap(m1, m2, self.goalcurrent.absmin, self.goalcurrent.absmax))
1850 // Detect personal waypoints
1851 if(self.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
1852 if(self.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL && self.goalcurrent.owner==self)
1854 self.aistatus &~= AI_STATUS_WAYPOINT_PERSONAL_GOING;
1855 self.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_REACHED;
1858 navigation_poproute();
1862 // begin a goal selection session (queries spawnfunc_waypoint network)
1863 void navigation_goalrating_start()
1865 navigation_bestrating = -1;
1866 self.navigation_hasgoals = FALSE;
1867 navigation_bestgoals_reset();
1868 navigation_markroutes();
1871 // ends a goal selection session (updates goal stack to the best goal)
1872 void navigation_goalrating_end()
1874 navigation_routetogoals();
1876 // Hack: if it can't walk to any goal just move blindly to the first visible waypoint
1877 if not (self.navigation_hasgoals)
1879 dprint(self.netname, " can't walk to any goal, going to a near waypoint\n");
1881 head = findradius(self.origin,500);
1884 if(head.classname=="waypoint")
1885 if(checkpvs(self.origin,head))
1886 navigation_routetogoal(head,self.origin);
1890 self.navigation_hasgoals = FALSE; // Reset this value
1895 //////////////////////////////////////////////////////////////////////////////
1896 // general bot functions
1897 //////////////////////////////////////////////////////////////////////////////
1899 .float isbot; // true if this client is actually a bot
1904 .string netname_freeme;
1905 .string playermodel_freeme;
1906 .string playerskin_freeme;
1911 .float bot_preferredcolors;
1915 .float bot_dodgerating;
1917 //.float bot_painintensity;
1918 .float bot_firetimer;
1919 //.float bot_oldhealth;
1922 .entity bot_aimtarg;
1923 .float bot_aimlatency;
1924 .vector bot_aimselforigin;
1925 .vector bot_aimselfvelocity;
1926 .vector bot_aimtargorigin;
1927 .vector bot_aimtargvelocity;
1930 .float(entity player, entity item) bot_pickupevalfunc;
1931 .float bot_pickupbasevalue;
1933 .float bot_strategytime;
1935 // used for aiming currently
1936 // FIXME: make the weapon code use these and then replace the calculations here with a call to the weapon code
1940 .float bot_forced_team;
1941 .float bot_config_loaded;
1943 void bot_setnameandstuff()
1945 local string readfile, s;
1946 local float file, tokens;
1948 local string bot_name, bot_model, bot_skin, bot_shirt, bot_pants;
1949 local string name, prefix, suffix;
1951 file = fopen(cvar_string("bot_config_file"), FILE_READ);
1954 print(strcat("Error: Can not open the bot configuration file '",cvar_string("bot_config_file"),"'\n"));
1957 RandomSelection_Init();
1963 if(substring(s, 0, 2) == "//")
1965 if(substring(s, 0, 1) == "#")
1967 RandomSelection_Add(world, 0, s, 1, 0);
1968 readfile = RandomSelection_chosen_string;
1973 tokens = tokenizebyseparator(readfile, "\t");
1974 if(argv(0) != "") bot_name = argv(0);
1975 else bot_name = "Bot";
1977 if(argv(1) != "") bot_model = argv(1);
1978 else bot_model = "marine";
1980 if(argv(2) != "") bot_skin = argv(2);
1981 else bot_skin = "0";
1983 if(argv(3) != "" && stof(argv(3)) >= 0) bot_shirt = argv(3);
1984 else bot_shirt = ftos(floor(random() * 15));
1986 if(argv(4) != "" && stof(argv(4)) >= 0) bot_pants = argv(4);
1987 else bot_pants = ftos(floor(random() * 15));
1989 self.bot_forced_team = stof(argv(5));
1990 self.bot_config_loaded = TRUE;
1991 prefix = cvar_string("bot_prefix");
1992 suffix = cvar_string("bot_suffix");
1994 // this is really only a default, JoinBestTeam is called later
1995 setcolor(self, stof(bot_shirt) * 16 + stof(bot_pants));
1996 self.bot_preferredcolors = self.clientcolors;
1999 if (cvar("bot_usemodelnames"))
2004 // pick the model and skin
2005 self.playermodel = self.playermodel_freeme = strzone(strcat("models/player/", bot_model, ".zym"));
2006 self.playerskin = self.playerskin_freeme = strzone(bot_skin);
2008 if(!cvar("g_campaign"))
2009 self.netname = self.netname_freeme = strzone(strcat(prefix, name, suffix));
2011 self.netname = self.netname_freeme = strzone(name);
2014 float bot_custom_weapon;
2015 float bot_distance_far;
2016 float bot_distance_close;
2018 float bot_weapons_far[WEP_LAST];
2019 float bot_weapons_mid[WEP_LAST];
2020 float bot_weapons_close[WEP_LAST];
2022 void bot_custom_weapon_priority_setup()
2024 local float tokens, i, c, w;
2026 bot_custom_weapon = FALSE;
2028 if( cvar_string("bot_ai_custom_weapon_priority_far") == "" ||
2029 cvar_string("bot_ai_custom_weapon_priority_mid") == "" ||
2030 cvar_string("bot_ai_custom_weapon_priority_close") == "" ||
2031 cvar_string("bot_ai_custom_weapon_priority_distances") == ""
2036 tokens = tokenizebyseparator(cvar_string("bot_ai_custom_weapon_priority_distances")," ");
2041 bot_distance_far = stof(argv(0));
2042 bot_distance_close = stof(argv(1));
2044 if(bot_distance_far < bot_distance_close){
2045 bot_distance_far = stof(argv(1));
2046 bot_distance_close = stof(argv(0));
2049 // Initialize list of weapons
2050 bot_weapons_far[0] = -1;
2051 bot_weapons_mid[0] = -1;
2052 bot_weapons_close[0] = -1;
2054 // Parse far distance weapon priorities
2055 tokens = tokenizebyseparator(cvar_string("bot_ai_custom_weapon_priority_far")," ");
2058 for(i=0; i < tokens && c < WEP_COUNT; ++i){
2060 if ( w >= WEP_FIRST && w <= WEP_LAST) {
2061 bot_weapons_far[c] = w;
2066 bot_weapons_far[c] = -1;
2068 // Parse mid distance weapon priorities
2069 tokens = tokenizebyseparator(cvar_string("bot_ai_custom_weapon_priority_mid")," ");
2072 for(i=0; i < tokens && c < WEP_COUNT; ++i){
2074 if ( w >= WEP_FIRST && w <= WEP_LAST) {
2075 bot_weapons_mid[c] = w;
2080 bot_weapons_mid[c] = -1;
2082 // Parse close distance weapon priorities
2083 tokens = tokenizebyseparator(cvar_string("bot_ai_custom_weapon_priority_close")," ");
2086 for(i=0; i < tokens && i < WEP_COUNT; ++i){
2088 if ( w >= WEP_FIRST && w <= WEP_LAST) {
2089 bot_weapons_close[c] = w;
2094 bot_weapons_close[c] = -1;
2096 bot_custom_weapon = TRUE;
2103 //dprint("bot_endgame\n");
2107 setcolor(e, e.bot_preferredcolors);
2110 // if dynamic waypoints are ever implemented, save them here
2113 float bot_ignore_bots; // let bots not attack other bots (only works in non-teamplay)
2114 float bot_shouldattack(entity e)
2116 if (e.team == self.team)
2126 if(clienttype(e) == CLIENTTYPE_BOT)
2135 if(e.items & IT_STRENGTH)
2137 if(e.flags & FL_NOTARGET)
2142 void bot_lagfunc(float t, float f1, float f2, entity e1, vector v1, vector v2, vector v3, vector v4)
2144 if(self.flags & FL_INWATER)
2146 self.bot_aimtarg = world;
2149 self.bot_aimtarg = e1;
2150 self.bot_aimlatency = self.ping; // FIXME? Shouldn't this be in the lag item?
2151 self.bot_aimselforigin = v1;
2152 self.bot_aimselfvelocity = v2;
2153 self.bot_aimtargorigin = v3;
2154 self.bot_aimtargvelocity = v4;
2156 self.bot_canfire = (random() < 0.8);
2158 self.bot_canfire = (random() < 0.9);
2160 self.bot_canfire = (random() < 0.95);
2162 self.bot_canfire = 1;
2165 .float bot_nextthink;
2166 .float bot_badaimtime;
2167 .float bot_aimthinktime;
2168 .float bot_prevaimtime;
2169 .vector bot_mouseaim;
2170 .vector bot_badaimoffset;
2171 .vector bot_1st_order_aimfilter;
2172 .vector bot_2nd_order_aimfilter;
2173 .vector bot_3th_order_aimfilter;
2174 .vector bot_4th_order_aimfilter;
2175 .vector bot_5th_order_aimfilter;
2176 .vector bot_olddesiredang;
2177 float bot_aimdir(vector v, float maxfiredeviation)
2179 local float dist, delta_t, blend;
2180 local vector desiredang, diffang;
2182 //dprint("aim ", self.netname, ": old:", vtos(self.v_angle));
2183 // make sure v_angle is sane first
2184 self.v_angle_y = self.v_angle_y - floor(self.v_angle_y / 360) * 360;
2187 // get the desired angles to aim at
2188 //dprint(" at:", vtos(v));
2190 //te_lightning2(world, self.origin + self.view_ofs, self.origin + self.view_ofs + v * 200);
2191 if (time >= self.bot_badaimtime)
2193 self.bot_badaimtime = max(self.bot_badaimtime + 0.3, time);
2194 self.bot_badaimoffset = randomvec() * bound(0, 5 - 0.5 * (skill+self.bot_offsetskill), 5) * cvar("bot_ai_aimskill_offset");
2196 desiredang = vectoangles(v) + self.bot_badaimoffset;
2197 //dprint(" desired:", vtos(desiredang));
2198 if (desiredang_x >= 180)
2199 desiredang_x = desiredang_x - 360;
2200 desiredang_x = bound(-90, 0 - desiredang_x, 90);
2201 desiredang_z = self.v_angle_z;
2202 //dprint(" / ", vtos(desiredang));
2204 //// pain throws off aim
2205 //if (self.bot_painintensity)
2207 // // shake from pain
2208 // desiredang = desiredang + randomvec() * self.bot_painintensity * 0.2;
2211 // calculate turn angles
2212 diffang = (desiredang - self.bot_olddesiredang);
2214 diffang_y = diffang_y - floor(diffang_y / 360) * 360;
2215 if (diffang_y >= 180)
2216 diffang_y = diffang_y - 360;
2217 self.bot_olddesiredang = desiredang;
2218 //dprint(" diff:", vtos(diffang));
2220 delta_t = time-self.bot_prevaimtime;
2221 self.bot_prevaimtime = time;
2222 // Here we will try to anticipate the comming aiming direction
2223 self.bot_1st_order_aimfilter= self.bot_1st_order_aimfilter
2224 + (diffang * (1 / delta_t) - self.bot_1st_order_aimfilter) * bound(0, cvar("bot_ai_aimskill_order_filter_1st"),1);
2225 self.bot_2nd_order_aimfilter= self.bot_2nd_order_aimfilter
2226 + (self.bot_1st_order_aimfilter - self.bot_2nd_order_aimfilter) * bound(0, cvar("bot_ai_aimskill_order_filter_2nd"),1);
2227 self.bot_3th_order_aimfilter= self.bot_3th_order_aimfilter
2228 + (self.bot_2nd_order_aimfilter - self.bot_3th_order_aimfilter) * bound(0, cvar("bot_ai_aimskill_order_filter_3th"),1);
2229 self.bot_4th_order_aimfilter= self.bot_4th_order_aimfilter
2230 + (self.bot_3th_order_aimfilter - self.bot_4th_order_aimfilter) * bound(0, cvar("bot_ai_aimskill_order_filter_4th"),1);
2231 self.bot_5th_order_aimfilter= self.bot_5th_order_aimfilter
2232 + (self.bot_4th_order_aimfilter - self.bot_5th_order_aimfilter) * bound(0, cvar("bot_ai_aimskill_order_filter_5th"),1);
2234 //blend = (bound(0,skill,10)*0.1)*pow(1-bound(0,skill,10)*0.05,2.5)*5.656854249; //Plot formule before changing !
2235 blend = bound(0,skill,10)*0.1;
2236 desiredang = desiredang + blend *
2238 self.bot_1st_order_aimfilter * cvar("bot_ai_aimskill_order_mix_1st")
2239 + self.bot_2nd_order_aimfilter * cvar("bot_ai_aimskill_order_mix_2nd")
2240 + self.bot_3th_order_aimfilter * cvar("bot_ai_aimskill_order_mix_3th")
2241 + self.bot_4th_order_aimfilter * cvar("bot_ai_aimskill_order_mix_4th")
2242 + self.bot_5th_order_aimfilter * cvar("bot_ai_aimskill_order_mix_5th")
2245 // calculate turn angles
2246 diffang = desiredang - self.bot_mouseaim;
2248 diffang_y = diffang_y - floor(diffang_y / 360) * 360;
2249 if (diffang_y >= 180)
2250 diffang_y = diffang_y - 360;
2251 //dprint(" diff:", vtos(diffang));
2253 if (time >= self.bot_aimthinktime)
2255 self.bot_aimthinktime = max(self.bot_aimthinktime + 0.5 - 0.05*(skill+self.bot_thinkskill), time);
2256 self.bot_mouseaim = self.bot_mouseaim + diffang * (1-random()*0.1*bound(1,10-skill,10));
2259 //self.v_angle = self.v_angle + diffang * bound(0, r * frametime * (skill * 0.5 + 2), 1);
2261 diffang = self.bot_mouseaim - desiredang;
2263 diffang_y = diffang_y - floor(diffang_y / 360) * 360;
2264 if (diffang_y >= 180)
2265 diffang_y = diffang_y - 360;
2266 desiredang = desiredang + diffang * bound(0,cvar("bot_ai_aimskill_think"),1);
2268 // calculate turn angles
2269 diffang = desiredang - self.v_angle;
2271 diffang_y = diffang_y - floor(diffang_y / 360) * 360;
2272 if (diffang_y >= 180)
2273 diffang_y = diffang_y - 360;
2274 //dprint(" diff:", vtos(diffang));
2277 dist = vlen(diffang);
2278 //diffang = diffang + randomvec() * (dist * 0.05 * (3.5 - bound(0, skill, 3)));
2281 local float r, fixedrate, blendrate;
2282 fixedrate = cvar("bot_ai_aimskill_fixedrate") / bound(1,dist,1000);
2283 blendrate = cvar("bot_ai_aimskill_blendrate");
2284 r = max(fixedrate, blendrate);
2285 //self.v_angle = self.v_angle + diffang * bound(frametime, r * frametime * (2+skill*skill*0.05-random()*0.05*(10-skill)), 1);
2286 self.v_angle = self.v_angle + diffang * bound(delta_t, r * delta_t * (2+pow(skill+self.bot_mouseskill,3)*0.005-random()), 1);
2287 self.v_angle = self.v_angle * bound(0,cvar("bot_ai_aimskill_mouse"),1) + desiredang * bound(0,(1-cvar("bot_ai_aimskill_mouse")),1);
2288 //self.v_angle = self.v_angle + diffang * bound(0, r * frametime * (skill * 0.5 + 2), 1);
2289 //self.v_angle = self.v_angle + diffang * (1/ blendrate);
2291 self.v_angle_y = self.v_angle_y - floor(self.v_angle_y / 360) * 360;
2292 //dprint(" turn:", vtos(self.v_angle));
2294 makevectors(self.v_angle);
2295 shotorg = self.origin + self.view_ofs;
2296 shotdir = v_forward;
2298 //dprint(" dir:", vtos(v_forward));
2299 //te_lightning2(world, shotorg, shotorg + shotdir * 100);
2301 // calculate turn angles again
2302 //diffang = desiredang - self.v_angle;
2303 //diffang_y = diffang_y - floor(diffang_y / 360) * 360;
2304 //if (diffang_y >= 180)
2305 // diffang_y = diffang_y - 360;
2307 //dprint("e ", vtos(diffang), " < ", ftos(maxfiredeviation), "\n");
2309 // decide whether to fire this time
2310 // note the maxfiredeviation is in degrees so this has to convert to radians first
2311 //if ((normalize(v) * shotdir) >= cos(maxfiredeviation * (3.14159265358979323846 / 180)))
2312 if ((normalize(v) * shotdir) >= cos(maxfiredeviation * (3.14159265358979323846 / 180)))
2313 if (vlen(trace_endpos-shotorg) < 500+500*bound(0, skill, 10) || random()*random()>bound(0,skill*0.05,1))
2314 self.bot_firetimer = time + bound(0.1, 0.5-skill*0.05, 0.5);
2315 //traceline(shotorg,shotorg+shotdir*1000,FALSE,world);
2316 //dprint(ftos(maxfiredeviation),"\n");
2317 //dprint(" diff:", vtos(diffang), "\n");
2319 return self.bot_canfire && (time < self.bot_firetimer);
2322 vector bot_shotlead(vector targorigin, vector targvelocity, float shotspeed, float shotdelay)
2324 // Try to add code here that predicts gravity effect here, no clue HOW to though ... well not yet atleast...
2325 return targorigin + targvelocity * (shotdelay + vlen(targorigin - shotorg) / shotspeed);
2328 float bot_aim(float shotspeed, float shotspeedupward, float maxshottime, float applygravity)
2334 dprint("bot_aim(", ftos(shotspeed));
2335 dprint(", ", ftos(shotspeedupward));
2336 dprint(", ", ftos(maxshottime));
2337 dprint(", ", ftos(applygravity));
2342 dprint("bot_aim: WARNING: weapon ", W_Name(self.weapon), " shotspeed is zero!\n");
2343 shotspeed = 1000000;
2347 dprint("bot_aim: WARNING: weapon ", W_Name(self.weapon), " maxshottime is zero!\n");
2350 makevectors(self.v_angle);
2351 shotorg = self.origin + self.view_ofs;
2352 shotdir = v_forward;
2353 v = bot_shotlead(self.bot_aimtargorigin, self.bot_aimtargvelocity, shotspeed, self.bot_aimlatency);
2354 local float distanceratio;
2355 distanceratio =sqrt(bound(0,skill,10000))*0.3*(vlen(v-shotorg)-100)/cvar("bot_ai_aimskill_firetolerance_distdegrees");
2356 distanceratio = bound(0,distanceratio,1);
2357 r = (cvar("bot_ai_aimskill_firetolerance_maxdegrees")-cvar("bot_ai_aimskill_firetolerance_mindegrees"))
2358 * (1-distanceratio) + cvar("bot_ai_aimskill_firetolerance_mindegrees");
2359 if (applygravity && self.bot_aimtarg)
2361 if (!findtrajectorywithleading(shotorg, '0 0 0', '0 0 0', self.bot_aimtarg, shotspeed, shotspeedupward, maxshottime, 0, self))
2363 f = bot_aimdir(findtrajectory_velocity - shotspeedupward * '0 0 1', r);
2367 f = bot_aimdir(v - shotorg, r);
2368 //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");
2369 traceline(shotorg, shotorg + shotdir * 10000, FALSE, self);
2370 if (trace_ent.takedamage)
2371 if (trace_fraction < 1)
2372 if (!bot_shouldattack(trace_ent))
2374 traceline(shotorg, self.bot_aimtargorigin, FALSE, self);
2375 if (trace_fraction < 1)
2376 if (trace_ent != self.enemy)
2377 if (!bot_shouldattack(trace_ent))
2380 if (r > maxshottime * shotspeed)
2385 // TODO: move this painintensity code to the player damage code
2388 if (self.bot_nextthink > time)
2391 self.flags &~= FL_GODMODE;
2393 self.flags |= FL_GODMODE;
2395 self.bot_nextthink = self.bot_nextthink + cvar("bot_ai_thinkinterval");
2396 //if (self.bot_painintensity > 0)
2397 // self.bot_painintensity = self.bot_painintensity - (skill + 1) * 40 * frametime;
2399 //self.bot_painintensity = self.bot_painintensity + self.bot_oldhealth - self.health;
2400 //self.bot_painintensity = bound(0, self.bot_painintensity, 100);
2402 if(time < game_starttime || ((cvar("g_campaign") && !campaign_bots_may_start)))
2404 self.nextthink = time + 0.5;
2410 self.v_angle = self.angles;
2412 self.fixangle = FALSE;
2417 self.dmg_inflictor = world;
2419 // calculate an aiming latency based on the skill setting
2420 // (simulated network latency + naturally delayed reflexes)
2421 //self.ping = 0.7 - bound(0, 0.05 * skill, 0.5); // moved the reflexes to bot_aimdir (under the name 'think')
2422 // minimum ping 20+10 random
2423 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
2424 // skill 10 = ping 0.2 (adrenaline)
2425 // skill 0 = ping 0.7 (slightly drunk)
2428 self.BUTTON_ATCK = 0;
2430 self.BUTTON_JUMP = 0;
2431 self.BUTTON_ATCK2 = 0;
2432 self.BUTTON_ZOOM = 0;
2433 self.BUTTON_CROUCH = 0;
2434 self.BUTTON_HOOK = 0;
2435 self.BUTTON_INFO = 0;
2437 self.BUTTON_CHAT = 0;
2438 self.BUTTON_USE = 0;
2440 // if dead, just wait until we can respawn
2443 if (self.deadflag == DEAD_DEAD)
2445 self.BUTTON_JUMP = 1; // press jump to respawn
2446 self.bot_strategytime = 0;
2450 // now call the current bot AI (havocbot for example)
2454 entity bot_strategytoken;
2455 float bot_strategytoken_taken;
2458 void bot_relinkplayerlist()
2461 local entity prevbot;
2464 player_list = e = findchainflags(flags, FL_CLIENT);
2469 player_count = player_count + 1;
2470 e.nextplayer = e.chain;
2471 if (clienttype(e) == CLIENTTYPE_BOT)
2474 prevbot.nextbot = e;
2478 bot_list.nextbot = world;
2481 currentbots = currentbots + 1;
2485 dprint(strcat("relink: ", ftos(currentbots), " bots seen.\n"));
2486 bot_strategytoken = bot_list;
2487 bot_strategytoken_taken = TRUE;
2490 void() havocbot_setupbot;
2491 float JoinBestTeam(entity pl, float only_return_best, float forcebestteam);
2493 void bot_clientdisconnect()
2495 if (clienttype(self) != CLIENTTYPE_BOT)
2497 if(self.netname_freeme)
2498 strunzone(self.netname_freeme);
2499 if(self.playermodel_freeme)
2500 strunzone(self.playermodel_freeme);
2501 if(self.playerskin_freeme)
2502 strunzone(self.playerskin_freeme);
2503 self.netname_freeme = string_null;
2504 self.playermodel_freeme = string_null;
2505 self.playerskin_freeme = string_null;
2508 void bot_clientconnect()
2510 if (clienttype(self) != CLIENTTYPE_BOT)
2512 self.bot_preferredcolors = self.clientcolors;
2513 self.bot_nextthink = time - random();
2514 self.lag_func = bot_lagfunc;
2516 self.createdtime = self.nextthink;
2518 if(!self.bot_config_loaded) // This is needed so team overrider doesn't break between matches
2519 bot_setnameandstuff();
2521 if(self.bot_forced_team==1)
2522 self.team = COLOR_TEAM1;
2523 else if(self.bot_forced_team==2)
2524 self.team = COLOR_TEAM2;
2525 else if(self.bot_forced_team==3)
2526 self.team = COLOR_TEAM3;
2527 else if(self.bot_forced_team==4)
2528 self.team = COLOR_TEAM4;
2530 JoinBestTeam(self, FALSE, TRUE);
2532 havocbot_setupbot();
2533 self.bot_mouseskill=random()-0.5;
2534 self.bot_thinkskill=random()-0.5;
2535 self.bot_predictionskill=random()-0.5;
2536 self.bot_offsetskill=random()-0.5;
2541 local entity oldself, bot;
2542 bot = spawnclient();
2545 currentbots = currentbots + 1;
2548 bot_setnameandstuff();
2550 PutClientInServer();
2556 void CheckAllowedTeams(entity for_whom); void GetTeamCounts(entity other); float c1, c2, c3, c4;
2557 void bot_removefromlargestteam()
2559 local float besttime, bestcount, thiscount;
2560 local entity best, head;
2561 CheckAllowedTeams(world);
2562 GetTeamCounts(world);
2563 head = findchainfloat(isbot, TRUE);
2567 besttime = head.createdtime;
2571 if(head.team == COLOR_TEAM1)
2573 else if(head.team == COLOR_TEAM2)
2575 else if(head.team == COLOR_TEAM3)
2577 else if(head.team == COLOR_TEAM4)
2581 if (thiscount > bestcount)
2583 bestcount = thiscount;
2584 besttime = head.createdtime;
2587 else if (thiscount == bestcount && besttime < head.createdtime)
2589 besttime = head.createdtime;
2594 currentbots = currentbots - 1;
2598 void bot_removenewest()
2600 local float besttime;
2601 local entity best, head;
2605 bot_removefromlargestteam();
2609 head = findchainfloat(isbot, TRUE);
2613 besttime = head.createdtime;
2616 if (besttime < head.createdtime)
2618 besttime = head.createdtime;
2623 currentbots = currentbots - 1;
2627 float botframe_waypointeditorlightningtime;
2628 void botframe_showwaypointlinks()
2630 local entity player, head, w;
2631 if (time < botframe_waypointeditorlightningtime)
2633 botframe_waypointeditorlightningtime = time + 0.5;
2634 player = find(world, classname, "player");
2638 if (player.flags & FL_ONGROUND || player.waterlevel > WATERLEVEL_NONE)
2640 //navigation_testtracewalk = TRUE;
2641 head = navigation_findnearestwaypoint(player, FALSE);
2642 //navigation_testtracewalk = FALSE;
2645 w = head ;if (w) te_lightning2(world, w.origin, player.origin);
2646 w = head.wp00;if (w) te_lightning2(world, w.origin, head.origin);
2647 w = head.wp01;if (w) te_lightning2(world, w.origin, head.origin);
2648 w = head.wp02;if (w) te_lightning2(world, w.origin, head.origin);
2649 w = head.wp03;if (w) te_lightning2(world, w.origin, head.origin);
2650 w = head.wp04;if (w) te_lightning2(world, w.origin, head.origin);
2651 w = head.wp05;if (w) te_lightning2(world, w.origin, head.origin);
2652 w = head.wp06;if (w) te_lightning2(world, w.origin, head.origin);
2653 w = head.wp07;if (w) te_lightning2(world, w.origin, head.origin);
2654 w = head.wp08;if (w) te_lightning2(world, w.origin, head.origin);
2655 w = head.wp09;if (w) te_lightning2(world, w.origin, head.origin);
2656 w = head.wp10;if (w) te_lightning2(world, w.origin, head.origin);
2657 w = head.wp11;if (w) te_lightning2(world, w.origin, head.origin);
2658 w = head.wp12;if (w) te_lightning2(world, w.origin, head.origin);
2659 w = head.wp13;if (w) te_lightning2(world, w.origin, head.origin);
2660 w = head.wp14;if (w) te_lightning2(world, w.origin, head.origin);
2661 w = head.wp15;if (w) te_lightning2(world, w.origin, head.origin);
2662 w = head.wp16;if (w) te_lightning2(world, w.origin, head.origin);
2663 w = head.wp17;if (w) te_lightning2(world, w.origin, head.origin);
2664 w = head.wp18;if (w) te_lightning2(world, w.origin, head.origin);
2665 w = head.wp19;if (w) te_lightning2(world, w.origin, head.origin);
2666 w = head.wp20;if (w) te_lightning2(world, w.origin, head.origin);
2667 w = head.wp21;if (w) te_lightning2(world, w.origin, head.origin);
2668 w = head.wp22;if (w) te_lightning2(world, w.origin, head.origin);
2669 w = head.wp23;if (w) te_lightning2(world, w.origin, head.origin);
2670 w = head.wp24;if (w) te_lightning2(world, w.origin, head.origin);
2671 w = head.wp25;if (w) te_lightning2(world, w.origin, head.origin);
2672 w = head.wp26;if (w) te_lightning2(world, w.origin, head.origin);
2673 w = head.wp27;if (w) te_lightning2(world, w.origin, head.origin);
2674 w = head.wp28;if (w) te_lightning2(world, w.origin, head.origin);
2675 w = head.wp29;if (w) te_lightning2(world, w.origin, head.origin);
2676 w = head.wp30;if (w) te_lightning2(world, w.origin, head.origin);
2677 w = head.wp31;if (w) te_lightning2(world, w.origin, head.origin);
2680 player = find(player, classname, "player");
2684 entity botframe_dangerwaypoint;
2685 void botframe_updatedangerousobjects(float maxupdate)
2687 local entity head, bot_dodgelist;
2688 local vector m1, m2, v;
2689 local float c, d, danger;
2691 bot_dodgelist = findchainfloat(bot_dodge, TRUE);
2692 botframe_dangerwaypoint = find(botframe_dangerwaypoint, classname, "waypoint");
2693 while (botframe_dangerwaypoint != world)
2696 m1 = botframe_dangerwaypoint.mins;
2697 m2 = botframe_dangerwaypoint.maxs;
2698 head = bot_dodgelist;
2702 v_x = bound(m1_x, v_x, m2_x);
2703 v_y = bound(m1_y, v_y, m2_y);
2704 v_z = bound(m1_z, v_z, m2_z);
2705 d = head.bot_dodgerating - vlen(head.origin - v);
2708 traceline(head.origin, v, TRUE, world);
2709 if (trace_fraction == 1)
2710 danger = danger + d;
2714 botframe_dangerwaypoint.dmg = danger;
2718 botframe_dangerwaypoint = find(botframe_dangerwaypoint, classname, "waypoint");
2723 float botframe_spawnedwaypoints;
2724 float botframe_nextthink;
2725 float botframe_nextdangertime;
2727 float autoskill_nextthink;
2728 .float totalfrags_lastcheck;
2729 void autoskill(float factor)
2737 FOR_EACH_PLAYER(head)
2739 if(clienttype(head) == CLIENTTYPE_REAL)
2740 bestplayer = max(bestplayer, head.totalfrags - head.totalfrags_lastcheck);
2742 bestbot = max(bestbot, head.totalfrags - head.totalfrags_lastcheck);
2745 dprint("autoskill: best player got ", ftos(bestplayer), ", ");
2746 dprint("best bot got ", ftos(bestbot), "; ");
2747 if(bestbot < 0 || bestplayer < 0)
2749 dprint("not doing anything\n");
2750 // don't return, let it reset all counters below
2752 else if(bestbot <= bestplayer * factor - 2)
2754 if(cvar("skill") < 17)
2756 dprint("2 frags difference, increasing skill\n");
2757 cvar_set("skill", ftos(cvar("skill") + 1));
2758 bprint("^2SKILL UP!^7 Now at level ", ftos(cvar("skill")), "\n");
2761 else if(bestbot >= bestplayer * factor + 2)
2763 if(cvar("skill") > 0)
2765 dprint("2 frags difference, decreasing skill\n");
2766 cvar_set("skill", ftos(cvar("skill") - 1));
2767 bprint("^1SKILL DOWN!^7 Now at level ", ftos(cvar("skill")), "\n");
2772 dprint("not doing anything\n");
2774 // don't reset counters, wait for them to accumulate
2777 FOR_EACH_PLAYER(head)
2778 head.totalfrags_lastcheck = head.totalfrags;
2781 float bot_cvar_nextthink;
2782 void bot_serverframe()
2784 float realplayers, bots, activerealplayers;
2787 if (intermission_running)
2793 if(time > autoskill_nextthink)
2796 a = cvar("skill_auto");
2799 autoskill_nextthink = time + 5;
2802 activerealplayers = 0;
2805 FOR_EACH_REALCLIENT(head)
2807 if(head.classname == "player" || g_lms || g_arena)
2808 ++activerealplayers;
2812 // add/remove bots if needed to make sure there are at least
2813 // minplayers+bot_number, or remove all bots if no one is playing
2814 // But don't remove bots immediately on level change, as the real players
2815 // usually haven't rejoined yet
2816 bots_would_leave = FALSE;
2817 if (realplayers || cvar("bot_join_empty") || (currentbots > 0 && time < 5))
2819 float realminplayers, minplayers;
2820 realminplayers = cvar("minplayers");
2821 minplayers = max(0, floor(realminplayers));
2823 float realminbots, minbots;
2824 if(cvar("bot_vs_human"))
2825 realminbots = ceil(fabs(cvar("bot_vs_human")) * activerealplayers);
2827 realminbots = cvar("bot_number");
2828 minbots = max(0, floor(realminbots));
2830 bots = min(max(minbots, minplayers - activerealplayers), maxclients - realplayers);
2832 bots_would_leave = TRUE;
2836 // if there are no players, remove bots
2840 bot_ignore_bots = cvar("bot_ignore_bots");
2842 // only add one bot per frame to avoid utter chaos
2843 if(time > botframe_nextthink)
2845 //dprint(ftos(bots), " ? ", ftos(currentbots), "\n");
2846 while (currentbots < bots)
2848 if (bot_spawn() == world)
2850 bprint("Can not add bot, server full.\n");
2851 botframe_nextthink = time + 10;
2855 while (currentbots > bots)
2859 if(botframe_spawnedwaypoints)
2861 if(cvar("waypoint_benchmark"))
2865 if (currentbots > 0 || cvar("g_waypointeditor"))
2866 if (botframe_spawnedwaypoints)
2868 if(botframe_cachedwaypointlinks)
2870 if(!botframe_loadedforcedlinks)
2871 waypoint_load_links_hardwired();
2875 // TODO: Make this check cleaner
2876 local entity wp = findchain(classname, "waypoint");
2877 if(time - wp.nextthink > 10)
2878 waypoint_save_links();
2883 botframe_spawnedwaypoints = TRUE;
2885 if(!waypoint_load_links())
2886 waypoint_schedulerelinkall();
2891 // cycle the goal token from one bot to the next each frame
2892 // (this prevents them from all doing spawnfunc_waypoint searches on the same
2893 // frame, which causes choppy framerates)
2894 if (bot_strategytoken_taken)
2896 bot_strategytoken_taken = FALSE;
2897 if (bot_strategytoken)
2898 bot_strategytoken = bot_strategytoken.nextbot;
2899 if (!bot_strategytoken)
2900 bot_strategytoken = bot_list;
2903 if (botframe_nextdangertime < time)
2905 local float interval;
2906 interval = cvar("bot_ai_dangerdetectioninterval");
2907 if (botframe_nextdangertime < time - interval * 1.5)
2908 botframe_nextdangertime = time;
2909 botframe_nextdangertime = botframe_nextdangertime + interval;
2910 botframe_updatedangerousobjects(cvar("bot_ai_dangerdetectionupdates"));
2914 if (cvar("g_waypointeditor"))
2915 botframe_showwaypointlinks();
2917 if(time > bot_cvar_nextthink)
2920 bot_custom_weapon_priority_setup();
2921 bot_cvar_nextthink = time + 5;