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