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