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