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