]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/bot/default/havocbot/havocbot.qc
Merge branch 'master' into Mario/entcs
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / bot / default / havocbot / havocbot.qc
1 #include "havocbot.qh"
2
3 #include "../cvars.qh"
4
5 #include "../aim.qh"
6 #include "../bot.qh"
7 #include "../navigation.qh"
8 #include "../scripting.qh"
9 #include "../waypoints.qh"
10
11 #include <common/constants.qh>
12 #include <common/physics/player.qh>
13 #include <common/state.qh>
14 #include <common/items/_mod.qh>
15
16 #include <common/triggers/trigger/jumppads.qh>
17
18 #include <lib/warpzone/common.qh>
19
20 .float speed;
21
22 void havocbot_ai(entity this)
23 {
24         if(this.draggedby)
25                 return;
26
27         if(bot_execute_commands(this))
28                 return;
29
30         if (bot_strategytoken == this)
31         if (!bot_strategytoken_taken)
32         {
33                 if(this.havocbot_blockhead)
34                 {
35                         this.havocbot_blockhead = false;
36                 }
37                 else
38                 {
39                         if (!this.jumppadcount)
40                                 this.havocbot_role(this); // little too far down the rabbit hole
41                 }
42
43                 // TODO: tracewalk() should take care of this job (better path finding under water)
44                 // if we don't have a goal and we're under water look for a waypoint near the "shore" and push it
45                 if(IS_DEAD(this))
46                 if(!this.goalcurrent)
47                 if(this.waterlevel == WATERLEVEL_SWIMMING || (this.aistatus & AI_STATUS_OUT_WATER))
48                 {
49                         // Look for the closest waypoint out of water
50                         entity newgoal = NULL;
51                         IL_EACH(g_waypoints, vdist(it.origin - this.origin, <=, 10000),
52                         {
53                                 if(it.origin.z < this.origin.z)
54                                         continue;
55
56                                 if(it.origin.z - this.origin.z - this.view_ofs.z > 100)
57                                         continue;
58
59                                 if (pointcontents(it.origin + it.maxs + '0 0 1') != CONTENT_EMPTY)
60                                         continue;
61
62                                 traceline(this.origin + this.view_ofs, ((it.absmin + it.absmax) * 0.5), true, this);
63
64                                 if(trace_fraction < 1)
65                                         continue;
66
67                                 if(!newgoal || vlen2(it.origin - this.origin) < vlen2(newgoal.origin - this.origin))
68                                         newgoal = it;
69                         });
70
71                         if(newgoal)
72                         {
73                         //      te_wizspike(newgoal.origin);
74                                 navigation_pushroute(this, newgoal);
75                         }
76                 }
77
78                 // token has been used this frame
79                 bot_strategytoken_taken = true;
80         }
81
82         if(IS_DEAD(this))
83                 return;
84
85         havocbot_chooseenemy(this);
86         if (this.bot_chooseweapontime < time )
87         {
88                 this.bot_chooseweapontime = time + autocvar_bot_ai_chooseweaponinterval;
89                 havocbot_chooseweapon(this);
90         }
91         havocbot_aim(this);
92         lag_update(this);
93         if (this.bot_aimtarg)
94         {
95                 this.aistatus |= AI_STATUS_ATTACKING;
96                 this.aistatus &= ~AI_STATUS_ROAMING;
97
98                 if(this.weapons)
99                 {
100                         Weapon w = PS(this).m_weapon;
101                         w.wr_aim(w, this);
102                         if (autocvar_bot_nofire || IS_INDEPENDENT_PLAYER(this))
103                         {
104                                 PHYS_INPUT_BUTTON_ATCK(this) = false;
105                                 PHYS_INPUT_BUTTON_ATCK2(this) = false;
106                         }
107                         else
108                         {
109                                 if(PHYS_INPUT_BUTTON_ATCK(this) || PHYS_INPUT_BUTTON_ATCK2(this))
110                                         this.lastfiredweapon = PS(this).m_weapon.m_id;
111                         }
112                 }
113                 else
114                 {
115                         if(IS_PLAYER(this.bot_aimtarg))
116                                 bot_aimdir(this, this.bot_aimtarg.origin + this.bot_aimtarg.view_ofs - this.origin - this.view_ofs , -1);
117                 }
118         }
119         else if (this.goalcurrent)
120         {
121                 this.aistatus |= AI_STATUS_ROAMING;
122                 this.aistatus &= ~AI_STATUS_ATTACKING;
123
124                 vector now,v,next;//,heading;
125                 float aimdistance,skillblend,distanceblend,blend;
126                 next = now = ( (this.goalcurrent.absmin + this.goalcurrent.absmax) * 0.5) - (this.origin + this.view_ofs);
127                 aimdistance = vlen(now);
128                 //heading = this.velocity;
129                 //dprint(this.goalstack01.classname,etos(this.goalstack01),"\n");
130                 if(
131                         this.goalstack01 != this && this.goalstack01 != NULL && ((this.aistatus & AI_STATUS_RUNNING) == 0) &&
132                         !(this.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT)
133                 )
134                         next = ((this.goalstack01.absmin + this.goalstack01.absmax) * 0.5) - (this.origin + this.view_ofs);
135
136                 skillblend=bound(0,(skill+this.bot_moveskill-2.5)*0.5,1); //lower skill player can't preturn
137                 distanceblend=bound(0,aimdistance/autocvar_bot_ai_keyboard_distance,1);
138                 blend = skillblend * (1-distanceblend);
139                 //v = (now * (distanceblend) + next * (1-distanceblend)) * (skillblend) + now * (1-skillblend);
140                 //v = now * (distanceblend) * (skillblend) + next * (1-distanceblend) * (skillblend) + now * (1-skillblend);
141                 //v = now * ((1-skillblend) + (distanceblend) * (skillblend)) + next * (1-distanceblend) * (skillblend);
142                 v = now + blend * (next - now);
143                 //dprint(etos(this), " ");
144                 //dprint(vtos(now), ":", vtos(next), "=", vtos(v), " (blend ", ftos(blend), ")\n");
145                 //v = now * (distanceblend) + next * (1-distanceblend);
146                 if (this.waterlevel < WATERLEVEL_SWIMMING)
147                         v.z = 0;
148                 //dprint("walk at:", vtos(v), "\n");
149                 //te_lightning2(NULL, this.origin, this.goalcurrent.origin);
150                 bot_aimdir(this, v, -1);
151         }
152         havocbot_movetogoal(this);
153
154         // if the bot is not attacking, consider reloading weapons
155         if (!(this.aistatus & AI_STATUS_ATTACKING))
156         {
157                 // we are currently holding a weapon that's not fully loaded, reload it
158                 if(skill >= 2) // bots can only reload the held weapon on purpose past this skill
159                 if(this.clip_load < this.clip_size)
160                         this.impulse = 20; // "press" the reload button, not sure if this is done right
161
162                 // if we're not reloading a weapon, switch to any weapon in our invnetory that's not fully loaded to reload it next
163                 // the code above executes next frame, starting the reloading then
164                 if(skill >= 5) // bots can only look for unloaded weapons past this skill
165                 if(this.clip_load >= 0) // only if we're not reloading a weapon already
166                 {
167                         FOREACH(Weapons, it != WEP_Null, LAMBDA(
168                                 if((this.weapons & (it.m_wepset)) && (it.spawnflags & WEP_FLAG_RELOADABLE) && (this.weapon_load[it.m_id] < it.reloading_ammo))
169                                         PS(this).m_switchweapon = it;
170                         ));
171                 }
172         }
173 }
174
175 void havocbot_keyboard_movement(entity this, vector destorg)
176 {
177         vector keyboard;
178         float blend, maxspeed;
179         float sk;
180
181         sk = skill + this.bot_moveskill;
182
183         maxspeed = autocvar_sv_maxspeed;
184
185         if (time < this.havocbot_keyboardtime)
186                 return;
187
188         this.havocbot_keyboardtime =
189                 max(
190                         this.havocbot_keyboardtime
191                                 + 0.05/max(1, sk+this.havocbot_keyboardskill)
192                                 + random()*0.025/max(0.00025, skill+this.havocbot_keyboardskill)
193                 , time);
194         keyboard = this.movement * (1.0 / maxspeed);
195
196         float trigger, trigger1;
197         blend = bound(0,sk*0.1,1);
198         trigger = autocvar_bot_ai_keyboard_threshold;
199         trigger1 = 0 - trigger;
200
201         // categorize forward movement
202         // at skill < 1.5 only forward
203         // at skill < 2.5 only individual directions
204         // at skill < 4.5 only individual directions, and forward diagonals
205         // at skill >= 4.5, all cases allowed
206         if (keyboard.x > trigger)
207         {
208                 keyboard.x = 1;
209                 if (sk < 2.5)
210                         keyboard.y = 0;
211         }
212         else if (keyboard.x < trigger1 && sk > 1.5)
213         {
214                 keyboard.x = -1;
215                 if (sk < 4.5)
216                         keyboard.y = 0;
217         }
218         else
219         {
220                 keyboard.x = 0;
221                 if (sk < 1.5)
222                         keyboard.y = 0;
223         }
224         if (sk < 4.5)
225                 keyboard.z = 0;
226
227         if (keyboard.y > trigger)
228                 keyboard.y = 1;
229         else if (keyboard.y < trigger1)
230                 keyboard.y = -1;
231         else
232                 keyboard.y = 0;
233
234         if (keyboard.z > trigger)
235                 keyboard.z = 1;
236         else if (keyboard.z < trigger1)
237                 keyboard.z = -1;
238         else
239                 keyboard.z = 0;
240
241         this.havocbot_keyboard = keyboard * maxspeed;
242         if (this.havocbot_ducktime>time) PHYS_INPUT_BUTTON_CROUCH(this) = true;
243
244         keyboard = this.havocbot_keyboard;
245         blend = bound(0,vlen(destorg-this.origin)/autocvar_bot_ai_keyboard_distance,1); // When getting close move with 360 degree
246         //dprint("movement ", vtos(this.movement), " keyboard ", vtos(keyboard), " blend ", ftos(blend), "\n");
247         this.movement = this.movement + (keyboard - this.movement) * blend;
248 }
249
250 void havocbot_bunnyhop(entity this, vector dir)
251 {
252         float bunnyhopdistance;
253         vector deviation;
254         float maxspeed;
255         vector gco, gno;
256
257         // Don't jump when attacking
258         if(this.aistatus & AI_STATUS_ATTACKING)
259                 return;
260
261         if(IS_PLAYER(this.goalcurrent))
262                 return;
263
264         maxspeed = autocvar_sv_maxspeed;
265
266         if(this.aistatus & AI_STATUS_DANGER_AHEAD)
267         {
268                 this.aistatus &= ~AI_STATUS_RUNNING;
269                 PHYS_INPUT_BUTTON_JUMP(this) = false;
270                 this.bot_canruntogoal = 0;
271                 this.bot_timelastseengoal = 0;
272                 return;
273         }
274
275         if(this.waterlevel > WATERLEVEL_WETFEET)
276         {
277                 this.aistatus &= ~AI_STATUS_RUNNING;
278                 return;
279         }
280
281         if(this.bot_lastseengoal != this.goalcurrent && !(this.aistatus & AI_STATUS_RUNNING))
282         {
283                 this.bot_canruntogoal = 0;
284                 this.bot_timelastseengoal = 0;
285         }
286
287         gco = (this.goalcurrent.absmin + this.goalcurrent.absmax) * 0.5;
288         bunnyhopdistance = vlen(this.origin - gco);
289
290         // Run only to visible goals
291         if(IS_ONGROUND(this))
292         if(this.speed==maxspeed)
293         if(checkpvs(this.origin + this.view_ofs, this.goalcurrent))
294         {
295                         this.bot_lastseengoal = this.goalcurrent;
296
297                         // seen it before
298                         if(this.bot_timelastseengoal)
299                         {
300                                 // for a period of time
301                                 if(time - this.bot_timelastseengoal > autocvar_bot_ai_bunnyhop_firstjumpdelay)
302                                 {
303                                         float checkdistance;
304                                         checkdistance = true;
305
306                                         // don't run if it is too close
307                                         if(this.bot_canruntogoal==0)
308                                         {
309                                                 if(bunnyhopdistance > autocvar_bot_ai_bunnyhop_startdistance)
310                                                         this.bot_canruntogoal = 1;
311                                                 else
312                                                         this.bot_canruntogoal = -1;
313                                         }
314
315                                         if(this.bot_canruntogoal != 1)
316                                                 return;
317
318                                         if(this.aistatus & AI_STATUS_ROAMING)
319                                         if(this.goalcurrent.classname=="waypoint")
320                                         if (!(this.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL))
321                                         if(fabs(gco.z - this.origin.z) < this.maxs.z - this.mins.z)
322                                         if(this.goalstack01!=NULL)
323                                         {
324                                                 gno = (this.goalstack01.absmin + this.goalstack01.absmax) * 0.5;
325                                                 deviation = vectoangles(gno - this.origin) - vectoangles(gco - this.origin);
326                                                 while (deviation.y < -180) deviation.y = deviation.y + 360;
327                                                 while (deviation.y > 180) deviation.y = deviation.y - 360;
328
329                                                 if(fabs(deviation.y) < 20)
330                                                 if(bunnyhopdistance < vlen(this.origin - gno))
331                                                 if(fabs(gno.z - gco.z) < this.maxs.z - this.mins.z)
332                                                 {
333                                                         if(vdist(gco - gno, >, autocvar_bot_ai_bunnyhop_startdistance))
334                                                         if(checkpvs(this.origin + this.view_ofs, this.goalstack01))
335                                                         {
336                                                                 checkdistance = false;
337                                                         }
338                                                 }
339                                         }
340
341                                         if(checkdistance)
342                                         {
343                                                 this.aistatus &= ~AI_STATUS_RUNNING;
344                                                 if(bunnyhopdistance > autocvar_bot_ai_bunnyhop_stopdistance)
345                                                         PHYS_INPUT_BUTTON_JUMP(this) = true;
346                                         }
347                                         else
348                                         {
349                                                 this.aistatus |= AI_STATUS_RUNNING;
350                                                 PHYS_INPUT_BUTTON_JUMP(this) = true;
351                                         }
352                                 }
353                         }
354                         else
355                         {
356                                 this.bot_timelastseengoal = time;
357                         }
358         }
359         else
360         {
361                 this.bot_timelastseengoal = 0;
362         }
363
364 #if 0
365         // Release jump button
366         if(!cvar("sv_pogostick"))
367         if((IS_ONGROUND(this)) == 0)
368         {
369                 if(this.velocity.z < 0 || vlen(this.velocity)<maxspeed)
370                         PHYS_INPUT_BUTTON_JUMP(this) = false;
371
372                 // Strafe
373                 if(this.aistatus & AI_STATUS_RUNNING)
374                 if(vlen(this.velocity)>maxspeed)
375                 {
376                         deviation = vectoangles(dir) - vectoangles(this.velocity);
377                         while (deviation.y < -180) deviation.y = deviation.y + 360;
378                         while (deviation.y > 180) deviation.y = deviation.y - 360;
379
380                         if(fabs(deviation.y)>10)
381                                 this.movement_x = 0;
382
383                         if(deviation.y>10)
384                                 this.movement_y = maxspeed * -1;
385                         else if(deviation.y<10)
386                                 this.movement_y = maxspeed;
387
388                 }
389         }
390 #endif
391 }
392
393 void havocbot_movetogoal(entity this)
394 {
395         vector destorg;
396         vector diff;
397         vector dir;
398         vector flatdir;
399         vector m1;
400         vector m2;
401         vector evadeobstacle;
402         vector evadelava;
403         float s;
404         float maxspeed;
405         vector gco;
406         //float dist;
407         vector dodge;
408         //if (this.goalentity)
409         //      te_lightning2(this, this.origin, (this.goalentity.absmin + this.goalentity.absmax) * 0.5);
410         this.movement = '0 0 0';
411         maxspeed = autocvar_sv_maxspeed;
412
413         // Jetpack navigation
414         if(this.goalcurrent)
415         if(this.navigation_jetpack_goal)
416         if(this.goalcurrent==this.navigation_jetpack_goal)
417         if(this.ammo_fuel)
418         {
419                 if(autocvar_bot_debug_goalstack)
420                 {
421                         debuggoalstack(this);
422                         te_wizspike(this.navigation_jetpack_point);
423                 }
424
425                 // Take off
426                 if (!(this.aistatus & AI_STATUS_JETPACK_FLYING))
427                 {
428                         // Brake almost completely so it can get a good direction
429                         if(vdist(this.velocity, >, 10))
430                                 return;
431                         this.aistatus |= AI_STATUS_JETPACK_FLYING;
432                 }
433
434                 makevectors(this.v_angle.y * '0 1 0');
435                 dir = normalize(this.navigation_jetpack_point - this.origin);
436
437                 // Landing
438                 if(this.aistatus & AI_STATUS_JETPACK_LANDING)
439                 {
440                         // Calculate brake distance in xy
441                         float db, v, d;
442                         vector dxy;
443
444                         dxy = this.origin - ( ( this.goalcurrent.absmin + this.goalcurrent.absmax ) * 0.5 ); dxy.z = 0;
445                         d = vlen(dxy);
446                         v = vlen(this.velocity -  this.velocity.z * '0 0 1');
447                         db = (pow(v,2) / (autocvar_g_jetpack_acceleration_side * 2)) + 100;
448                 //      dprint("distance ", ftos(ceil(d)), " velocity ", ftos(ceil(v)), " brake at ", ftos(ceil(db)), "\n");
449                         if(d < db || d < 500)
450                         {
451                                 // Brake
452                                 if(fabs(this.velocity.x)>maxspeed*0.3)
453                                 {
454                                         this.movement_x = dir * v_forward * -maxspeed;
455                                         return;
456                                 }
457                                 // Switch to normal mode
458                                 this.navigation_jetpack_goal = NULL;
459                                 this.aistatus &= ~AI_STATUS_JETPACK_LANDING;
460                                 this.aistatus &= ~AI_STATUS_JETPACK_FLYING;
461                                 return;
462                         }
463                 }
464                 else if(checkpvs(this.origin,this.goalcurrent))
465                 {
466                         // If I can see the goal switch to landing code
467                         this.aistatus &= ~AI_STATUS_JETPACK_FLYING;
468                         this.aistatus |= AI_STATUS_JETPACK_LANDING;
469                         return;
470                 }
471
472                 // Flying
473                 PHYS_INPUT_BUTTON_HOOK(this) = true;
474                 if(this.navigation_jetpack_point.z - STAT(PL_MAX, NULL).z + STAT(PL_MIN, NULL).z < this.origin.z)
475                 {
476                         this.movement_x = dir * v_forward * maxspeed;
477                         this.movement_y = dir * v_right * maxspeed;
478                 }
479                 return;
480         }
481
482         // Handling of jump pads
483         if(this.jumppadcount)
484         {
485                 // If got stuck on the jump pad try to reach the farthest visible waypoint
486                 if(this.aistatus & AI_STATUS_OUT_JUMPPAD)
487                 {
488                         if(fabs(this.velocity.z)<50)
489                         {
490                                 entity newgoal = NULL;
491                                 IL_EACH(g_waypoints, vdist(it.origin - this.origin, <=, 1000),
492                                 {
493                                         traceline(this.origin + this.view_ofs, ((it.absmin + it.absmax) * 0.5), true, this);
494
495                                         if(trace_fraction < 1)
496                                                 continue;
497
498                                         if(!newgoal || vlen2(it.origin - this.origin) > vlen2(newgoal.origin - this.origin))
499                                                 newgoal = it;
500                                 });
501
502                                 if(newgoal)
503                                 {
504                                         this.ignoregoal = this.goalcurrent;
505                                         this.ignoregoaltime = time + autocvar_bot_ai_ignoregoal_timeout;
506                                         navigation_clearroute(this);
507                                         navigation_routetogoal(this, newgoal, this.origin);
508                                         this.aistatus &= ~AI_STATUS_OUT_JUMPPAD;
509                                 }
510                         }
511                         else
512                                 return;
513                 }
514                 else
515                 {
516                         if(this.velocity.z>0)
517                         {
518                                 float threshold;
519                                 vector velxy = this.velocity; velxy_z = 0;
520                                 threshold = maxspeed * 0.2;
521                                 if(vdist(velxy, <, threshold))
522                                 {
523                                         LOG_TRACE("Warning: ", this.netname, " got stuck on a jumppad (velocity in xy is ", vtos(velxy), "), trying to get out of it now");
524                                         this.aistatus |= AI_STATUS_OUT_JUMPPAD;
525                                 }
526                                 return;
527                         }
528
529                         // Don't chase players while using a jump pad
530                         if(IS_PLAYER(this.goalcurrent) || IS_PLAYER(this.goalstack01))
531                                 return;
532                 }
533         }
534         else if(this.aistatus & AI_STATUS_OUT_JUMPPAD)
535                 this.aistatus &= ~AI_STATUS_OUT_JUMPPAD;
536
537         // If there is a trigger_hurt right below try to use the jetpack or make a rocketjump
538         if(skill>6)
539         if (!(IS_ONGROUND(this)))
540         {
541                 tracebox(this.origin, this.mins, this.maxs, this.origin + '0 0 -65536', MOVE_NOMONSTERS, this);
542                 if(tracebox_hits_trigger_hurt(this.origin, this.mins, this.maxs, trace_endpos ))
543                 if(this.items & IT_JETPACK)
544                 {
545                         tracebox(this.origin, this.mins, this.maxs, this.origin + '0 0 65536', MOVE_NOMONSTERS, this);
546                         if(tracebox_hits_trigger_hurt(this.origin, this.mins, this.maxs, trace_endpos + '0 0 1' ))
547                         {
548                                 if(this.velocity.z<0)
549                                 {
550                                         PHYS_INPUT_BUTTON_HOOK(this) = true;
551                                 }
552                         }
553                         else
554                                 PHYS_INPUT_BUTTON_HOOK(this) = true;
555
556                         // If there is no goal try to move forward
557
558                         if(this.goalcurrent==NULL)
559                                 dir = v_forward;
560                         else
561                                 dir = normalize(( ( this.goalcurrent.absmin + this.goalcurrent.absmax ) * 0.5 ) - this.origin);
562
563                         vector xyvelocity = this.velocity; xyvelocity_z = 0;
564                         float xyspeed = xyvelocity * dir;
565
566                         if(xyspeed < (maxspeed / 2))
567                         {
568                                 makevectors(this.v_angle.y * '0 1 0');
569                                 tracebox(this.origin, this.mins, this.maxs, this.origin + (dir * maxspeed * 3), MOVE_NOMONSTERS, this);
570                                 if(trace_fraction==1)
571                                 {
572                                         this.movement_x = dir * v_forward * maxspeed;
573                                         this.movement_y = dir * v_right * maxspeed;
574                                         if (skill < 10)
575                                                 havocbot_keyboard_movement(this, this.origin + dir * 100);
576                                 }
577                         }
578
579                         this.havocbot_blockhead = true;
580
581                         return;
582                 }
583                 else if(this.health>WEP_CVAR(devastator, damage)*0.5)
584                 {
585                         if(this.velocity.z < 0)
586                         if(client_hasweapon(this, WEP_DEVASTATOR, true, false))
587                         {
588                                 this.movement_x = maxspeed;
589
590                                 if(this.rocketjumptime)
591                                 {
592                                         if(time > this.rocketjumptime)
593                                         {
594                                                 PHYS_INPUT_BUTTON_ATCK2(this) = true;
595                                                 this.rocketjumptime = 0;
596                                         }
597                                         return;
598                                 }
599
600                                 PS(this).m_switchweapon = WEP_DEVASTATOR;
601                                 this.v_angle_x = 90;
602                                 PHYS_INPUT_BUTTON_ATCK(this) = true;
603                                 this.rocketjumptime = time + WEP_CVAR(devastator, detonatedelay);
604                                 return;
605                         }
606                 }
607                 else
608                 {
609                         // If there is no goal try to move forward
610                         if(this.goalcurrent==NULL)
611                                 this.movement_x = maxspeed;
612                 }
613         }
614
615         // If we are under water with no goals, swim up
616         if(this.waterlevel)
617         if(this.goalcurrent==NULL)
618         {
619                 dir = '0 0 0';
620                 if(this.waterlevel>WATERLEVEL_SWIMMING)
621                         dir.z = 1;
622                 else if(this.velocity.z >= 0 && !(this.waterlevel == WATERLEVEL_WETFEET && this.watertype == CONTENT_WATER))
623                         PHYS_INPUT_BUTTON_JUMP(this) = true;
624                 else
625                         PHYS_INPUT_BUTTON_JUMP(this) = false;
626                 makevectors(this.v_angle.y * '0 1 0');
627                 this.movement_x = dir * v_forward * maxspeed;
628                 this.movement_y = dir * v_right * maxspeed;
629                 this.movement_z = dir * v_up * maxspeed;
630         }
631
632         // if there is nowhere to go, exit
633         if (this.goalcurrent == NULL)
634                 return;
635
636         if (this.goalcurrent)
637                 navigation_poptouchedgoals(this);
638
639         // if ran out of goals try to use an alternative goal or get a new strategy asap
640         if(this.goalcurrent == NULL)
641         {
642                 this.bot_strategytime = 0;
643                 return;
644         }
645
646
647         if(autocvar_bot_debug_goalstack)
648                 debuggoalstack(this);
649
650         m1 = this.goalcurrent.origin + this.goalcurrent.mins;
651         m2 = this.goalcurrent.origin + this.goalcurrent.maxs;
652         destorg = this.origin;
653         destorg.x = bound(m1_x, destorg.x, m2_x);
654         destorg.y = bound(m1_y, destorg.y, m2_y);
655         destorg.z = bound(m1_z, destorg.z, m2_z);
656         diff = destorg - this.origin;
657         //dist = vlen(diff);
658         dir = normalize(diff);
659         flatdir = diff;flatdir.z = 0;
660         flatdir = normalize(flatdir);
661         gco = (this.goalcurrent.absmin + this.goalcurrent.absmax) * 0.5;
662
663         //if (this.bot_dodgevector_time < time)
664         {
665         //      this.bot_dodgevector_time = time + cvar("bot_ai_dodgeupdateinterval");
666         //      this.bot_dodgevector_jumpbutton = 1;
667                 evadeobstacle = '0 0 0';
668                 evadelava = '0 0 0';
669
670                 if (this.waterlevel)
671                 {
672                         if(this.waterlevel>WATERLEVEL_SWIMMING)
673                         {
674                         //      flatdir_z = 1;
675                                 this.aistatus |= AI_STATUS_OUT_WATER;
676                         }
677                         else
678                         {
679                                 if(this.velocity.z >= 0 && !(this.watertype == CONTENT_WATER && gco.z < this.origin.z) &&
680                                         ( !(this.waterlevel == WATERLEVEL_WETFEET && this.watertype == CONTENT_WATER) || this.aistatus & AI_STATUS_OUT_WATER))
681                                         PHYS_INPUT_BUTTON_JUMP(this) = true;
682                                 else
683                                         PHYS_INPUT_BUTTON_JUMP(this) = false;
684                         }
685                         dir = normalize(flatdir);
686                         makevectors(this.v_angle.y * '0 1 0');
687                 }
688                 else
689                 {
690                         if(this.aistatus & AI_STATUS_OUT_WATER)
691                                 this.aistatus &= ~AI_STATUS_OUT_WATER;
692
693                         // jump if going toward an obstacle that doesn't look like stairs we
694                         // can walk up directly
695                         tracebox(this.origin, this.mins, this.maxs, this.origin + this.velocity * 0.2, false, this);
696                         if (trace_fraction < 1)
697                         if (trace_plane_normal.z < 0.7)
698                         {
699                                 s = trace_fraction;
700                                 tracebox(this.origin + stepheightvec, this.mins, this.maxs, this.origin + this.velocity * 0.2 + stepheightvec, false, this);
701                                 if (trace_fraction < s + 0.01)
702                                 if (trace_plane_normal.z < 0.7)
703                                 {
704                                         s = trace_fraction;
705                                         tracebox(this.origin + jumpstepheightvec, this.mins, this.maxs, this.origin + this.velocity * 0.2 + jumpstepheightvec, false, this);
706                                         if (trace_fraction > s)
707                                                 PHYS_INPUT_BUTTON_JUMP(this) = true;
708                                 }
709                         }
710
711                         // avoiding dangers and obstacles
712                         vector dst_ahead, dst_down;
713                         makevectors(this.v_angle.y * '0 1 0');
714                         dst_ahead = this.origin + this.view_ofs + (this.velocity * 0.4) + (v_forward * 32 * 3);
715                         dst_down = dst_ahead - '0 0 1500';
716
717                         // Look ahead
718                         traceline(this.origin + this.view_ofs, dst_ahead, true, NULL);
719
720                         // Check head-banging against walls
721                         if(vdist(this.origin + this.view_ofs - trace_endpos, <, 25) && !(this.aistatus & AI_STATUS_OUT_WATER))
722                         {
723                                 PHYS_INPUT_BUTTON_JUMP(this) = true;
724                                 if(this.facingwalltime && time > this.facingwalltime)
725                                 {
726                                         this.ignoregoal = this.goalcurrent;
727                                         this.ignoregoaltime = time + autocvar_bot_ai_ignoregoal_timeout;
728                                         this.bot_strategytime = 0;
729                                         return;
730                                 }
731                                 else
732                                 {
733                                         this.facingwalltime = time + 0.05;
734                                 }
735                         }
736                         else
737                         {
738                                 this.facingwalltime = 0;
739
740                                 if(this.ignoregoal != NULL && time > this.ignoregoaltime)
741                                 {
742                                         this.ignoregoal = NULL;
743                                         this.ignoregoaltime = 0;
744                                 }
745                         }
746
747                         // Check for water/slime/lava and dangerous edges
748                         // (only when the bot is on the ground or jumping intentionally)
749                         this.aistatus &= ~AI_STATUS_DANGER_AHEAD;
750
751                         if(trace_fraction == 1 && this.jumppadcount == 0 && !this.goalcurrent.wphardwired )
752                         if((IS_ONGROUND(this)) || (this.aistatus & AI_STATUS_RUNNING) || PHYS_INPUT_BUTTON_JUMP(this))
753                         {
754                                 // Look downwards
755                                 traceline(dst_ahead , dst_down, true, NULL);
756                         //      te_lightning2(NULL, this.origin, dst_ahead);    // Draw "ahead" look
757                         //      te_lightning2(NULL, dst_ahead, dst_down);               // Draw "downwards" look
758                                 if(trace_endpos.z < this.origin.z + this.mins.z)
759                                 {
760                                         s = pointcontents(trace_endpos + '0 0 1');
761                                         if (s != CONTENT_SOLID)
762                                         if (s == CONTENT_LAVA || s == CONTENT_SLIME)
763                                                 evadelava = normalize(this.velocity) * -1;
764                                         else if (s == CONTENT_SKY)
765                                                 evadeobstacle = normalize(this.velocity) * -1;
766                                         else if (!boxesoverlap(dst_ahead - this.view_ofs + this.mins, dst_ahead - this.view_ofs + this.maxs,
767                                                                 this.goalcurrent.absmin, this.goalcurrent.absmax))
768                                         {
769                                                 // if ain't a safe goal with "holes" (like the jumpad on soylent)
770                                                 // and there is a trigger_hurt below
771                                                 if(tracebox_hits_trigger_hurt(dst_ahead, this.mins, this.maxs, trace_endpos))
772                                                 {
773                                                         // Remove dangerous dynamic goals from stack
774                                                         LOG_TRACE("bot ", this.netname, " avoided the goal ", this.goalcurrent.classname, " ", etos(this.goalcurrent), " because it led to a dangerous path; goal stack cleared");
775                                                         navigation_clearroute(this);
776                                                         return;
777                                                 }
778                                         }
779                                 }
780                         }
781
782                         dir = flatdir;
783                         evadeobstacle.z = 0;
784                         evadelava.z = 0;
785                         makevectors(this.v_angle.y * '0 1 0');
786
787                         if(evadeobstacle!='0 0 0'||evadelava!='0 0 0')
788                                 this.aistatus |= AI_STATUS_DANGER_AHEAD;
789                 }
790
791                 dodge = havocbot_dodge(this);
792                 dodge = dodge * bound(0,0.5+(skill+this.bot_dodgeskill)*0.1,1);
793                 evadelava = evadelava * bound(1,3-(skill+this.bot_dodgeskill),3); //Noobs fear lava a lot and take more distance from it
794                 traceline(this.origin, ( ( this.enemy.absmin + this.enemy.absmax ) * 0.5 ), true, NULL);
795                 if(IS_PLAYER(trace_ent))
796                         dir = dir * bound(0,(skill+this.bot_dodgeskill)/7,1);
797
798                 dir = normalize(dir + dodge + evadeobstacle + evadelava);
799         //      this.bot_dodgevector = dir;
800         //      this.bot_dodgevector_jumpbutton = PHYS_INPUT_BUTTON_JUMP(this);
801         }
802
803         if(time < this.ladder_time)
804         {
805                 if(this.goalcurrent.origin.z + this.goalcurrent.mins.z > this.origin.z + this.mins.z)
806                 {
807                         if(this.origin.z + this.mins.z  < this.ladder_entity.origin.z + this.ladder_entity.maxs.z)
808                                 dir.z = 1;
809                 }
810                 else
811                 {
812                         if(this.origin.z + this.mins.z  > this.ladder_entity.origin.z + this.ladder_entity.mins.z)
813                                 dir.z = -1;
814                 }
815         }
816
817         //dir = this.bot_dodgevector;
818         //if (this.bot_dodgevector_jumpbutton)
819         //      PHYS_INPUT_BUTTON_JUMP(this) = true;
820         this.movement_x = dir * v_forward * maxspeed;
821         this.movement_y = dir * v_right * maxspeed;
822         this.movement_z = dir * v_up * maxspeed;
823
824         // Emulate keyboard interface
825         if (skill < 10)
826                 havocbot_keyboard_movement(this, destorg);
827
828         // Bunnyhop!
829 //      if(this.aistatus & AI_STATUS_ROAMING)
830         if(this.goalcurrent)
831         if(skill+this.bot_moveskill >= autocvar_bot_ai_bunnyhop_skilloffset)
832                 havocbot_bunnyhop(this, dir);
833
834         if ((dir * v_up) >= autocvar_sv_jumpvelocity*0.5 && (IS_ONGROUND(this))) PHYS_INPUT_BUTTON_JUMP(this) = true;
835         if (((dodge * v_up) > 0) && random()*frametime >= 0.2*bound(0,(10-skill-this.bot_dodgeskill)*0.1,1)) PHYS_INPUT_BUTTON_JUMP(this) = true;
836         if (((dodge * v_up) < 0) && random()*frametime >= 0.5*bound(0,(10-skill-this.bot_dodgeskill)*0.1,1)) this.havocbot_ducktime=time+0.3/bound(0.1,skill+this.bot_dodgeskill,10);
837 }
838
839 void havocbot_chooseenemy(entity this)
840 {
841         entity head, best, head2;
842         float rating, bestrating, hf;
843         vector eye, v;
844         if (autocvar_bot_nofire || IS_INDEPENDENT_PLAYER(this))
845         {
846                 this.enemy = NULL;
847                 return;
848         }
849         if (this.enemy)
850         {
851                 if (!bot_shouldattack(this, this.enemy))
852                 {
853                         // enemy died or something, find a new target
854                         this.enemy = NULL;
855                         this.havocbot_chooseenemy_finished = time;
856                 }
857                 else if (this.havocbot_stickenemy)
858                 {
859                         // tracking last chosen enemy
860                         // if enemy is visible
861                         // and not really really far away
862                         // and we're not severely injured
863                         // then keep tracking for a half second into the future
864                         traceline(this.origin+this.view_ofs, ( this.enemy.absmin + this.enemy.absmax ) * 0.5,false,NULL);
865                         if (trace_ent == this.enemy || trace_fraction == 1)
866                         if (vdist(((this.enemy.absmin + this.enemy.absmax) * 0.5) - this.origin, <, 1000))
867                         if (this.health > 30)
868                         {
869                                 // remain tracking him for a shot while (case he went after a small corner or pilar
870                                 this.havocbot_chooseenemy_finished = time + 0.5;
871                                 return;
872                         }
873                         // enemy isn't visible, or is far away, or we're injured severely
874                         // so stop preferring this enemy
875                         // (it will still take a half second until a new one is chosen)
876                         this.havocbot_stickenemy = 0;
877                 }
878         }
879         if (time < this.havocbot_chooseenemy_finished)
880                 return;
881         this.havocbot_chooseenemy_finished = time + autocvar_bot_ai_enemydetectioninterval;
882         eye = this.origin + this.view_ofs;
883         best = NULL;
884         bestrating = 100000000;
885         head = head2 = findchainfloat(bot_attack, true);
886
887         // Backup hit flags
888         hf = this.dphitcontentsmask;
889
890         // Search for enemies, if no enemy can be seen directly try to look through transparent objects
891
892         this.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_CORPSE;
893
894         bool scan_transparent = false;
895         bool scan_secondary_targets = false;
896         bool have_secondary_targets = false;
897         while(true)
898         {
899                 scan_secondary_targets = false;
900 LABEL(scan_targets)
901                 for( ; head; head = head.chain)
902                 {
903                         if(!scan_secondary_targets)
904                         {
905                                 if(head.classname == "misc_breakablemodel")
906                                 {
907                                         have_secondary_targets = true;
908                                         continue;
909                                 }
910                         }
911                         else
912                         {
913                                 if(head.classname != "misc_breakablemodel")
914                                         continue;
915                         }
916
917                         v = (head.absmin + head.absmax) * 0.5;
918                         rating = vlen(v - eye);
919                         if (rating<autocvar_bot_ai_enemydetectionradius)
920                         if (bestrating > rating)
921                         if (bot_shouldattack(this, head))
922                         {
923                                 traceline(eye, v, true, this);
924                                 if (trace_ent == head || trace_fraction >= 1)
925                                 {
926                                         best = head;
927                                         bestrating = rating;
928                                 }
929                         }
930                 }
931
932                 if(!best && have_secondary_targets && !scan_secondary_targets)
933                 {
934                         scan_secondary_targets = true;
935                         // restart the loop
936                         head = head2;
937                         bestrating = 100000000;
938                         goto scan_targets;
939                 }
940
941                 // I want to do a second scan if no enemy was found or I don't have weapons
942                 // TODO: Perform the scan when using the rifle (requires changes on the rifle code)
943                 if(best || this.weapons) // || this.weapon == WEP_RIFLE.m_id
944                         break;
945                 if(scan_transparent)
946                         break;
947
948                 // Set flags to see through transparent objects
949                 this.dphitcontentsmask |= DPCONTENTS_OPAQUE;
950
951                 head = head2;
952                 scan_transparent = true;
953         }
954
955         // Restore hit flags
956         this.dphitcontentsmask = hf;
957
958         this.enemy = best;
959         this.havocbot_stickenemy = true;
960         if(best && best.classname == "misc_breakablemodel")
961                 this.havocbot_stickenemy = false;
962 }
963
964 float havocbot_chooseweapon_checkreload(entity this, int new_weapon)
965 {
966         // bots under this skill cannot find unloaded weapons to reload idly when not in combat,
967         // so skip this for them, or they'll never get to reload their weapons at all.
968         // this also allows bots under this skill to be more stupid, and reload more often during combat :)
969         if(skill < 5)
970                 return false;
971
972         // if this weapon is scheduled for reloading, don't switch to it during combat
973         if (this.weapon_load[new_weapon] < 0)
974         {
975                 bool other_weapon_available = false;
976                 FOREACH(Weapons, it != WEP_Null, LAMBDA(
977                         if(it.wr_checkammo1(it, this) + it.wr_checkammo2(it, this))
978                                 other_weapon_available = true;
979                 ));
980                 if(other_weapon_available)
981                         return true;
982         }
983
984         return false;
985 }
986
987 void havocbot_chooseweapon(entity this)
988 {
989         int i;
990
991         // ;)
992         if(g_weaponarena_weapons == WEPSET(TUBA))
993         {
994                 PS(this).m_switchweapon = WEP_TUBA;
995                 return;
996         }
997
998         // TODO: clean this up by moving it to weapon code
999         if(this.enemy==NULL)
1000         {
1001                 // If no weapon was chosen get the first available weapon
1002                 if(PS(this).m_weapon==WEP_Null)
1003                 FOREACH(Weapons, it != WEP_Null, LAMBDA(
1004                         if(client_hasweapon(this, it, true, false))
1005                         {
1006                                 PS(this).m_switchweapon = it;
1007                                 return;
1008                         }
1009                 ));
1010                 return;
1011         }
1012
1013         // Do not change weapon during the next second after a combo
1014         float f = time - this.lastcombotime;
1015         if(f < 1)
1016                 return;
1017
1018         float w;
1019         float distance; distance=bound(10,vlen(this.origin-this.enemy.origin)-200,10000);
1020
1021         // Should it do a weapon combo?
1022         float af, ct, combo_time, combo;
1023
1024         af = ATTACK_FINISHED(this, 0);
1025         ct = autocvar_bot_ai_weapon_combo_threshold;
1026
1027         // Bots with no skill will be 4 times more slower than "godlike" bots when doing weapon combos
1028         // Ideally this 4 should be calculated as longest_weapon_refire / bot_ai_weapon_combo_threshold
1029         combo_time = time + ct + (ct * ((-0.3*(skill+this.bot_weaponskill))+3));
1030
1031         combo = false;
1032
1033         if(autocvar_bot_ai_weapon_combo)
1034         if(PS(this).m_weapon.m_id == this.lastfiredweapon)
1035         if(af > combo_time)
1036         {
1037                 combo = true;
1038                 this.lastcombotime = time;
1039         }
1040
1041         distance *= pow(2, this.bot_rangepreference);
1042
1043         // Custom weapon list based on distance to the enemy
1044         if(bot_custom_weapon){
1045
1046                 // Choose weapons for far distance
1047                 if ( distance > bot_distance_far ) {
1048                         for(i=0; i < Weapons_COUNT && bot_weapons_far[i] != -1 ; ++i){
1049                                 w = bot_weapons_far[i];
1050                                 if ( client_hasweapon(this, Weapons_from(w), true, false) )
1051                                 {
1052                                         if ((PS(this).m_weapon.m_id == w && combo) || havocbot_chooseweapon_checkreload(this, w))
1053                                                 continue;
1054                                         PS(this).m_switchweapon = Weapons_from(w);
1055                                         return;
1056                                 }
1057                         }
1058                 }
1059
1060                 // Choose weapons for mid distance
1061                 if ( distance > bot_distance_close) {
1062                         for(i=0; i < Weapons_COUNT && bot_weapons_mid[i] != -1 ; ++i){
1063                                 w = bot_weapons_mid[i];
1064                                 if ( client_hasweapon(this, Weapons_from(w), true, false) )
1065                                 {
1066                                         if ((PS(this).m_weapon.m_id == w && combo) || havocbot_chooseweapon_checkreload(this, w))
1067                                                 continue;
1068                                         PS(this).m_switchweapon = Weapons_from(w);
1069                                         return;
1070                                 }
1071                         }
1072                 }
1073
1074                 // Choose weapons for close distance
1075                 for(i=0; i < Weapons_COUNT && bot_weapons_close[i] != -1 ; ++i){
1076                         w = bot_weapons_close[i];
1077                         if ( client_hasweapon(this, Weapons_from(w), true, false) )
1078                         {
1079                                 if ((PS(this).m_weapon.m_id == w && combo) || havocbot_chooseweapon_checkreload(this, w))
1080                                         continue;
1081                                 PS(this).m_switchweapon = Weapons_from(w);
1082                                 return;
1083                         }
1084                 }
1085         }
1086 }
1087
1088 void havocbot_aim(entity this)
1089 {
1090         vector myvel, enemyvel;
1091 //      if(this.flags & FL_INWATER)
1092 //              return;
1093         if (time < this.nextaim)
1094                 return;
1095         this.nextaim = time + 0.1;
1096         myvel = this.velocity;
1097         if (!this.waterlevel)
1098                 myvel.z = 0;
1099         if (this.enemy)
1100         {
1101                 enemyvel = this.enemy.velocity;
1102                 if (!this.enemy.waterlevel)
1103                         enemyvel.z = 0;
1104                 lag_additem(this, time + this.ping, 0, 0, this.enemy, this.origin, myvel, (this.enemy.absmin + this.enemy.absmax) * 0.5, enemyvel);
1105         }
1106         else
1107                 lag_additem(this, time + this.ping, 0, 0, NULL, this.origin, myvel, ( this.goalcurrent.absmin + this.goalcurrent.absmax ) * 0.5, '0 0 0');
1108 }
1109
1110 bool havocbot_moveto_refresh_route(entity this)
1111 {
1112         // Refresh path to goal if necessary
1113         entity wp;
1114         wp = this.havocbot_personal_waypoint;
1115         navigation_goalrating_start(this);
1116         navigation_routerating(this, wp, 10000, 10000);
1117         navigation_goalrating_end(this);
1118         return this.navigation_hasgoals;
1119 }
1120
1121 float havocbot_moveto(entity this, vector pos)
1122 {
1123         entity wp;
1124
1125         if(this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
1126         {
1127                 // Step 4: Move to waypoint
1128                 if(this.havocbot_personal_waypoint==NULL)
1129                 {
1130                         LOG_TRACE("Error: ", this.netname, " trying to walk to a non existent personal waypoint");
1131                         this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_GOING;
1132                         return CMD_STATUS_ERROR;
1133                 }
1134
1135                 if (!bot_strategytoken_taken)
1136                 if(this.havocbot_personal_waypoint_searchtime<time)
1137                 {
1138                         bot_strategytoken_taken = true;
1139                         if(havocbot_moveto_refresh_route(this))
1140                         {
1141                                 LOG_TRACE(this.netname, " walking to its personal waypoint (after ", ftos(this.havocbot_personal_waypoint_failcounter), " failed attempts)");
1142                                 this.havocbot_personal_waypoint_searchtime = time + 10;
1143                                 this.havocbot_personal_waypoint_failcounter = 0;
1144                         }
1145                         else
1146                         {
1147                                 this.havocbot_personal_waypoint_failcounter += 1;
1148                                 this.havocbot_personal_waypoint_searchtime = time + 2;
1149                                 if(this.havocbot_personal_waypoint_failcounter >= 30)
1150                                 {
1151                                         LOG_TRACE("Warning: can't walk to the personal waypoint located at ", vtos(this.havocbot_personal_waypoint.origin));
1152                                         this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_LINKING;
1153                                         delete(this.havocbot_personal_waypoint);
1154                                         return CMD_STATUS_ERROR;
1155                                 }
1156                                 else
1157                                         LOG_TRACE(this.netname, " can't walk to its personal waypoint (after ", ftos(this.havocbot_personal_waypoint_failcounter), " failed attempts), trying later");
1158                         }
1159                 }
1160
1161                 if(autocvar_bot_debug_goalstack)
1162                         debuggoalstack(this);
1163
1164                 // Heading
1165                 vector dir = ( ( this.goalcurrent.absmin + this.goalcurrent.absmax ) * 0.5 ) - (this.origin + this.view_ofs);
1166                 dir.z = 0;
1167                 bot_aimdir(this, dir, -1);
1168
1169                 // Go!
1170                 havocbot_movetogoal(this);
1171
1172                 if(this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_REACHED)
1173                 {
1174                         // Step 5: Waypoint reached
1175                         LOG_TRACE(this.netname, "'s personal waypoint reached");
1176                         delete(this.havocbot_personal_waypoint);
1177                         this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_REACHED;
1178                         return CMD_STATUS_FINISHED;
1179                 }
1180
1181                 return CMD_STATUS_EXECUTING;
1182         }
1183
1184         // Step 2: Linking waypoint
1185         if(this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_LINKING)
1186         {
1187                 // Wait until it is linked
1188                 if(!this.havocbot_personal_waypoint.wplinked)
1189                 {
1190                         LOG_TRACE(this.netname, " waiting for personal waypoint to be linked");
1191                         return CMD_STATUS_EXECUTING;
1192                 }
1193
1194                 this.havocbot_personal_waypoint_searchtime = time; // so we set the route next frame
1195                 this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_LINKING;
1196                 this.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_GOING;
1197
1198                 // Step 3: Route to waypoint
1199                 LOG_TRACE(this.netname, " walking to its personal waypoint");
1200
1201                 return CMD_STATUS_EXECUTING;
1202         }
1203
1204         // Step 1: Spawning waypoint
1205         wp = waypoint_spawnpersonal(this, pos);
1206         if(wp==NULL)
1207         {
1208                 LOG_TRACE("Error: Can't spawn personal waypoint at ",vtos(pos));
1209                 return CMD_STATUS_ERROR;
1210         }
1211
1212         this.havocbot_personal_waypoint = wp;
1213         this.havocbot_personal_waypoint_failcounter = 0;
1214         this.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_LINKING;
1215
1216         // if pos is inside a teleport, then let's mark it as teleport waypoint
1217         FOREACH_ENTITY_CLASS("trigger_teleport", WarpZoneLib_BoxTouchesBrush(pos, pos, it, NULL),
1218         {
1219                 wp.wpflags |= WAYPOINTFLAG_TELEPORT;
1220                 this.lastteleporttime = 0;
1221         });
1222
1223 /*
1224         if(wp.wpflags & WAYPOINTFLAG_TELEPORT)
1225                 print("routing to a teleporter\n");
1226         else
1227                 print("routing to a non-teleporter\n");
1228 */
1229
1230         return CMD_STATUS_EXECUTING;
1231 }
1232
1233 float havocbot_resetgoal(entity this)
1234 {
1235         navigation_clearroute(this);
1236         return CMD_STATUS_FINISHED;
1237 }
1238
1239 void havocbot_setupbot(entity this)
1240 {
1241         this.bot_ai = havocbot_ai;
1242         this.cmd_moveto = havocbot_moveto;
1243         this.cmd_resetgoal = havocbot_resetgoal;
1244
1245         havocbot_chooserole(this);
1246 }
1247
1248 vector havocbot_dodge(entity this)
1249 {
1250         // LordHavoc: disabled because this is too expensive
1251         return '0 0 0';
1252 #if 0
1253         entity head;
1254         vector dodge, v, n;
1255         float danger, bestdanger, vl, d;
1256         dodge = '0 0 0';
1257         bestdanger = -20;
1258         // check for dangerous objects near bot or approaching bot
1259         head = findchainfloat(bot_dodge, true);
1260         while(head)
1261         {
1262                 if (head.owner != this)
1263                 {
1264                         vl = vlen(head.velocity);
1265                         if (vl > autocvar_sv_maxspeed * 0.3)
1266                         {
1267                                 n = normalize(head.velocity);
1268                                 v = this.origin - head.origin;
1269                                 d = v * n;
1270                                 if (d > (0 - head.bot_dodgerating))
1271                                 if (d < (vl * 0.2 + head.bot_dodgerating))
1272                                 {
1273                                         // calculate direction and distance from the flight path, by removing the forward axis
1274                                         v = v - (n * (v * n));
1275                                         danger = head.bot_dodgerating - vlen(v);
1276                                         if (bestdanger < danger)
1277                                         {
1278                                                 bestdanger = danger;
1279                                                 // dodge to the side of the object
1280                                                 dodge = normalize(v);
1281                                         }
1282                                 }
1283                         }
1284                         else
1285                         {
1286                                 danger = head.bot_dodgerating - vlen(head.origin - this.origin);
1287                                 if (bestdanger < danger)
1288                                 {
1289                                         bestdanger = danger;
1290                                         dodge = normalize(this.origin - head.origin);
1291                                 }
1292                         }
1293                 }
1294                 head = head.chain;
1295         }
1296         return dodge;
1297 #endif
1298 }