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