]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/bot/havocbot/havocbot.qc
Merge branch 'master' into Mario/fullbright_skins
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / bot / havocbot / havocbot.qc
1 #include "havocbot.qh"
2
3 #include "../aim.qh"
4 #include "../bot.qh"
5 #include "../navigation.qh"
6 #include "../scripting.qh"
7 #include "../waypoints.qh"
8
9 #include <common/constants.qh>
10 #include <common/physics/player.qh>
11 #include <common/state.qh>
12 #include <common/items/all.qh>
13
14 #include <common/triggers/trigger/jumppads.qh>
15
16 #include <lib/warpzone/common.qh>
17
18 .float speed;
19
20 void havocbot_ai(entity this)
21 {
22         if(this.draggedby)
23                 return;
24
25         if(bot_execute_commands(this))
26                 return;
27
28         if (bot_strategytoken == this)
29         if (!bot_strategytoken_taken)
30         {
31                 if(this.havocbot_blockhead)
32                 {
33                         this.havocbot_blockhead = false;
34                 }
35                 else
36                 {
37                         if (!this.jumppadcount)
38                                 this.havocbot_role(this); // little too far down the rabbit hole
39                 }
40
41                 // TODO: tracewalk() should take care of this job (better path finding under water)
42                 // if we don't have a goal and we're under water look for a waypoint near the "shore" and push it
43                 if(IS_DEAD(this))
44                 if(this.goalcurrent==NULL)
45                 if(this.waterlevel==WATERLEVEL_SWIMMING || (this.aistatus & AI_STATUS_OUT_WATER))
46                 {
47                         // Look for the closest waypoint out of water
48                         entity newgoal, head;
49                         float bestdistance, distance;
50
51                         newgoal = NULL;
52                         bestdistance = 10000;
53                         for (head = findchain(classname, "waypoint"); head; head = head.chain)
54                         {
55                                 distance = vlen(head.origin - this.origin);
56                                 if(distance>10000)
57                                         continue;
58
59                                 if(head.origin.z < this.origin.z)
60                                         continue;
61
62                                 if(head.origin.z - this.origin.z - this.view_ofs.z > 100)
63                                         continue;
64
65                                 if (pointcontents(head.origin + head.maxs + '0 0 1') != CONTENT_EMPTY)
66                                         continue;
67
68                                 traceline(this.origin + this.view_ofs , head.origin, true, head);
69
70                                 if(trace_fraction<1)
71                                         continue;
72
73                                 if(distance<bestdistance)
74                                 {
75                                         newgoal = head;
76                                         bestdistance = distance;
77                                 }
78                         }
79
80                         if(newgoal)
81                         {
82                         //      te_wizspike(newgoal.origin);
83                                 navigation_pushroute(this, newgoal);
84                         }
85                 }
86
87                 // token has been used this frame
88                 bot_strategytoken_taken = true;
89         }
90
91         if(IS_DEAD(this))
92                 return;
93
94         havocbot_chooseenemy(this);
95         if (this.bot_chooseweapontime < time )
96         {
97                 this.bot_chooseweapontime = time + autocvar_bot_ai_chooseweaponinterval;
98                 havocbot_chooseweapon(this);
99         }
100         havocbot_aim(this);
101         lag_update(this);
102         if (this.bot_aimtarg)
103         {
104                 this.aistatus |= AI_STATUS_ATTACKING;
105                 this.aistatus &= ~AI_STATUS_ROAMING;
106
107                 if(this.weapons)
108                 {
109                         Weapon w = PS(this).m_weapon;
110                         w.wr_aim(w, this);
111                         if (autocvar_bot_nofire || IS_INDEPENDENT_PLAYER(this))
112                         {
113                                 PHYS_INPUT_BUTTON_ATCK(this) = false;
114                                 PHYS_INPUT_BUTTON_ATCK2(this) = false;
115                         }
116                         else
117                         {
118                                 if(PHYS_INPUT_BUTTON_ATCK(this) || PHYS_INPUT_BUTTON_ATCK2(this))
119                                         this.lastfiredweapon = PS(this).m_weapon.m_id;
120                         }
121                 }
122                 else
123                 {
124                         if(IS_PLAYER(this.bot_aimtarg))
125                                 bot_aimdir(this, this.bot_aimtarg.origin + this.bot_aimtarg.view_ofs - this.origin - this.view_ofs , -1);
126                 }
127         }
128         else if (this.goalcurrent)
129         {
130                 this.aistatus |= AI_STATUS_ROAMING;
131                 this.aistatus &= ~AI_STATUS_ATTACKING;
132
133                 vector now,v,next;//,heading;
134                 float aimdistance,skillblend,distanceblend,blend;
135                 next = now = ( (this.goalcurrent.absmin + this.goalcurrent.absmax) * 0.5) - (this.origin + this.view_ofs);
136                 aimdistance = vlen(now);
137                 //heading = this.velocity;
138                 //dprint(this.goalstack01.classname,etos(this.goalstack01),"\n");
139                 if(
140                         this.goalstack01 != this && this.goalstack01 != NULL && ((this.aistatus & AI_STATUS_RUNNING) == 0) &&
141                         !(this.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT)
142                 )
143                         next = ((this.goalstack01.absmin + this.goalstack01.absmax) * 0.5) - (this.origin + this.view_ofs);
144
145                 skillblend=bound(0,(skill+this.bot_moveskill-2.5)*0.5,1); //lower skill player can't preturn
146                 distanceblend=bound(0,aimdistance/autocvar_bot_ai_keyboard_distance,1);
147                 blend = skillblend * (1-distanceblend);
148                 //v = (now * (distanceblend) + next * (1-distanceblend)) * (skillblend) + now * (1-skillblend);
149                 //v = now * (distanceblend) * (skillblend) + next * (1-distanceblend) * (skillblend) + now * (1-skillblend);
150                 //v = now * ((1-skillblend) + (distanceblend) * (skillblend)) + next * (1-distanceblend) * (skillblend);
151                 v = now + blend * (next - now);
152                 //dprint(etos(this), " ");
153                 //dprint(vtos(now), ":", vtos(next), "=", vtos(v), " (blend ", ftos(blend), ")\n");
154                 //v = now * (distanceblend) + next * (1-distanceblend);
155                 if (this.waterlevel < WATERLEVEL_SWIMMING)
156                         v.z = 0;
157                 //dprint("walk at:", vtos(v), "\n");
158                 //te_lightning2(NULL, this.origin, this.goalcurrent.origin);
159                 bot_aimdir(this, v, -1);
160         }
161         havocbot_movetogoal(this);
162
163         // if the bot is not attacking, consider reloading weapons
164         if (!(this.aistatus & AI_STATUS_ATTACKING))
165         {
166                 // we are currently holding a weapon that's not fully loaded, reload it
167                 if(skill >= 2) // bots can only reload the held weapon on purpose past this skill
168                 if(this.clip_load < this.clip_size)
169                         this.impulse = 20; // "press" the reload button, not sure if this is done right
170
171                 // if we're not reloading a weapon, switch to any weapon in our invnetory that's not fully loaded to reload it next
172                 // the code above executes next frame, starting the reloading then
173                 if(skill >= 5) // bots can only look for unloaded weapons past this skill
174                 if(this.clip_load >= 0) // only if we're not reloading a weapon already
175                 {
176                         FOREACH(Weapons, it != WEP_Null, LAMBDA(
177                                 if((this.weapons & (it.m_wepset)) && (it.spawnflags & WEP_FLAG_RELOADABLE) && (this.weapon_load[it.m_id] < it.reloading_ammo))
178                                         PS(this).m_switchweapon = it;
179                         ));
180                 }
181         }
182 }
183
184 void havocbot_keyboard_movement(entity this, vector destorg)
185 {
186         vector keyboard;
187         float blend, maxspeed;
188         float sk;
189
190         sk = skill + this.bot_moveskill;
191
192         maxspeed = autocvar_sv_maxspeed;
193
194         if (time < this.havocbot_keyboardtime)
195                 return;
196
197         this.havocbot_keyboardtime =
198                 max(
199                         this.havocbot_keyboardtime
200                                 + 0.05/max(1, sk+this.havocbot_keyboardskill)
201                                 + random()*0.025/max(0.00025, skill+this.havocbot_keyboardskill)
202                 , time);
203         keyboard = this.movement * (1.0 / maxspeed);
204
205         float trigger, trigger1;
206         blend = bound(0,sk*0.1,1);
207         trigger = autocvar_bot_ai_keyboard_threshold;
208         trigger1 = 0 - trigger;
209
210         // categorize forward movement
211         // at skill < 1.5 only forward
212         // at skill < 2.5 only individual directions
213         // at skill < 4.5 only individual directions, and forward diagonals
214         // at skill >= 4.5, all cases allowed
215         if (keyboard.x > trigger)
216         {
217                 keyboard.x = 1;
218                 if (sk < 2.5)
219                         keyboard.y = 0;
220         }
221         else if (keyboard.x < trigger1 && sk > 1.5)
222         {
223                 keyboard.x = -1;
224                 if (sk < 4.5)
225                         keyboard.y = 0;
226         }
227         else
228         {
229                 keyboard.x = 0;
230                 if (sk < 1.5)
231                         keyboard.y = 0;
232         }
233         if (sk < 4.5)
234                 keyboard.z = 0;
235
236         if (keyboard.y > trigger)
237                 keyboard.y = 1;
238         else if (keyboard.y < trigger1)
239                 keyboard.y = -1;
240         else
241                 keyboard.y = 0;
242
243         if (keyboard.z > trigger)
244                 keyboard.z = 1;
245         else if (keyboard.z < trigger1)
246                 keyboard.z = -1;
247         else
248                 keyboard.z = 0;
249
250         this.havocbot_keyboard = keyboard * maxspeed;
251         if (this.havocbot_ducktime>time) PHYS_INPUT_BUTTON_CROUCH(this) = true;
252
253         keyboard = this.havocbot_keyboard;
254         blend = bound(0,vlen(destorg-this.origin)/autocvar_bot_ai_keyboard_distance,1); // When getting close move with 360 degree
255         //dprint("movement ", vtos(this.movement), " keyboard ", vtos(keyboard), " blend ", ftos(blend), "\n");
256         this.movement = this.movement + (keyboard - this.movement) * blend;
257 }
258
259 void havocbot_bunnyhop(entity this, vector dir)
260 {
261         float bunnyhopdistance;
262         vector deviation;
263         float maxspeed;
264         vector gco, gno;
265
266         // Don't jump when attacking
267         if(this.aistatus & AI_STATUS_ATTACKING)
268                 return;
269
270         if(IS_PLAYER(this.goalcurrent))
271                 return;
272
273         maxspeed = autocvar_sv_maxspeed;
274
275         if(this.aistatus & AI_STATUS_DANGER_AHEAD)
276         {
277                 this.aistatus &= ~AI_STATUS_RUNNING;
278                 PHYS_INPUT_BUTTON_JUMP(this) = false;
279                 this.bot_canruntogoal = 0;
280                 this.bot_timelastseengoal = 0;
281                 return;
282         }
283
284         if(this.waterlevel > WATERLEVEL_WETFEET)
285         {
286                 this.aistatus &= ~AI_STATUS_RUNNING;
287                 return;
288         }
289
290         if(this.bot_lastseengoal != this.goalcurrent && !(this.aistatus & AI_STATUS_RUNNING))
291         {
292                 this.bot_canruntogoal = 0;
293                 this.bot_timelastseengoal = 0;
294         }
295
296         gco = (this.goalcurrent.absmin + this.goalcurrent.absmax) * 0.5;
297         bunnyhopdistance = vlen(this.origin - gco);
298
299         // Run only to visible goals
300         if(IS_ONGROUND(this))
301         if(this.speed==maxspeed)
302         if(checkpvs(this.origin + this.view_ofs, this.goalcurrent))
303         {
304                         this.bot_lastseengoal = this.goalcurrent;
305
306                         // seen it before
307                         if(this.bot_timelastseengoal)
308                         {
309                                 // for a period of time
310                                 if(time - this.bot_timelastseengoal > autocvar_bot_ai_bunnyhop_firstjumpdelay)
311                                 {
312                                         float checkdistance;
313                                         checkdistance = true;
314
315                                         // don't run if it is too close
316                                         if(this.bot_canruntogoal==0)
317                                         {
318                                                 if(bunnyhopdistance > autocvar_bot_ai_bunnyhop_startdistance)
319                                                         this.bot_canruntogoal = 1;
320                                                 else
321                                                         this.bot_canruntogoal = -1;
322                                         }
323
324                                         if(this.bot_canruntogoal != 1)
325                                                 return;
326
327                                         if(this.aistatus & AI_STATUS_ROAMING)
328                                         if(this.goalcurrent.classname=="waypoint")
329                                         if (!(this.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL))
330                                         if(fabs(gco.z - this.origin.z) < this.maxs.z - this.mins.z)
331                                         if(this.goalstack01!=NULL)
332                                         {
333                                                 gno = (this.goalstack01.absmin + this.goalstack01.absmax) * 0.5;
334                                                 deviation = vectoangles(gno - this.origin) - vectoangles(gco - this.origin);
335                                                 while (deviation.y < -180) deviation.y = deviation.y + 360;
336                                                 while (deviation.y > 180) deviation.y = deviation.y - 360;
337
338                                                 if(fabs(deviation.y) < 20)
339                                                 if(bunnyhopdistance < vlen(this.origin - gno))
340                                                 if(fabs(gno.z - gco.z) < this.maxs.z - this.mins.z)
341                                                 {
342                                                         if(vdist(gco - gno, >, autocvar_bot_ai_bunnyhop_startdistance))
343                                                         if(checkpvs(this.origin + this.view_ofs, this.goalstack01))
344                                                         {
345                                                                 checkdistance = false;
346                                                         }
347                                                 }
348                                         }
349
350                                         if(checkdistance)
351                                         {
352                                                 this.aistatus &= ~AI_STATUS_RUNNING;
353                                                 if(bunnyhopdistance > autocvar_bot_ai_bunnyhop_stopdistance)
354                                                         PHYS_INPUT_BUTTON_JUMP(this) = true;
355                                         }
356                                         else
357                                         {
358                                                 this.aistatus |= AI_STATUS_RUNNING;
359                                                 PHYS_INPUT_BUTTON_JUMP(this) = true;
360                                         }
361                                 }
362                         }
363                         else
364                         {
365                                 this.bot_timelastseengoal = time;
366                         }
367         }
368         else
369         {
370                 this.bot_timelastseengoal = 0;
371         }
372
373 #if 0
374         // Release jump button
375         if(!cvar("sv_pogostick"))
376         if((IS_ONGROUND(this)) == 0)
377         {
378                 if(this.velocity.z < 0 || vlen(this.velocity)<maxspeed)
379                         PHYS_INPUT_BUTTON_JUMP(this) = false;
380
381                 // Strafe
382                 if(this.aistatus & AI_STATUS_RUNNING)
383                 if(vlen(this.velocity)>maxspeed)
384                 {
385                         deviation = vectoangles(dir) - vectoangles(this.velocity);
386                         while (deviation.y < -180) deviation.y = deviation.y + 360;
387                         while (deviation.y > 180) deviation.y = deviation.y - 360;
388
389                         if(fabs(deviation.y)>10)
390                                 this.movement_x = 0;
391
392                         if(deviation.y>10)
393                                 this.movement_y = maxspeed * -1;
394                         else if(deviation.y<10)
395                                 this.movement_y = maxspeed;
396
397                 }
398         }
399 #endif
400 }
401
402 void havocbot_movetogoal(entity this)
403 {
404         vector destorg;
405         vector diff;
406         vector dir;
407         vector flatdir;
408         vector m1;
409         vector m2;
410         vector evadeobstacle;
411         vector evadelava;
412         float s;
413         float maxspeed;
414         vector gco;
415         //float dist;
416         vector dodge;
417         //if (this.goalentity)
418         //      te_lightning2(this, this.origin, (this.goalentity.absmin + this.goalentity.absmax) * 0.5);
419         this.movement = '0 0 0';
420         maxspeed = autocvar_sv_maxspeed;
421
422         // Jetpack navigation
423         if(this.goalcurrent)
424         if(this.navigation_jetpack_goal)
425         if(this.goalcurrent==this.navigation_jetpack_goal)
426         if(this.ammo_fuel)
427         {
428                 if(autocvar_bot_debug_goalstack)
429                 {
430                         debuggoalstack(this);
431                         te_wizspike(this.navigation_jetpack_point);
432                 }
433
434                 // Take off
435                 if (!(this.aistatus & AI_STATUS_JETPACK_FLYING))
436                 {
437                         // Brake almost completely so it can get a good direction
438                         if(vdist(this.velocity, >, 10))
439                                 return;
440                         this.aistatus |= AI_STATUS_JETPACK_FLYING;
441                 }
442
443                 makevectors(this.v_angle.y * '0 1 0');
444                 dir = normalize(this.navigation_jetpack_point - this.origin);
445
446                 // Landing
447                 if(this.aistatus & AI_STATUS_JETPACK_LANDING)
448                 {
449                         // Calculate brake distance in xy
450                         float db, v, d;
451                         vector dxy;
452
453                         dxy = this.origin - ( ( this.goalcurrent.absmin + this.goalcurrent.absmax ) * 0.5 ); dxy.z = 0;
454                         d = vlen(dxy);
455                         v = vlen(this.velocity -  this.velocity.z * '0 0 1');
456                         db = (pow(v,2) / (autocvar_g_jetpack_acceleration_side * 2)) + 100;
457                 //      dprint("distance ", ftos(ceil(d)), " velocity ", ftos(ceil(v)), " brake at ", ftos(ceil(db)), "\n");
458                         if(d < db || d < 500)
459                         {
460                                 // Brake
461                                 if(fabs(this.velocity.x)>maxspeed*0.3)
462                                 {
463                                         this.movement_x = dir * v_forward * -maxspeed;
464                                         return;
465                                 }
466                                 // Switch to normal mode
467                                 this.navigation_jetpack_goal = NULL;
468                                 this.aistatus &= ~AI_STATUS_JETPACK_LANDING;
469                                 this.aistatus &= ~AI_STATUS_JETPACK_FLYING;
470                                 return;
471                         }
472                 }
473                 else if(checkpvs(this.origin,this.goalcurrent))
474                 {
475                         // If I can see the goal switch to landing code
476                         this.aistatus &= ~AI_STATUS_JETPACK_FLYING;
477                         this.aistatus |= AI_STATUS_JETPACK_LANDING;
478                         return;
479                 }
480
481                 // Flying
482                 PHYS_INPUT_BUTTON_HOOK(this) = true;
483                 if(this.navigation_jetpack_point.z - STAT(PL_MAX, NULL).z + STAT(PL_MIN, NULL).z < this.origin.z)
484                 {
485                         this.movement_x = dir * v_forward * maxspeed;
486                         this.movement_y = dir * v_right * maxspeed;
487                 }
488                 return;
489         }
490
491         // Handling of jump pads
492         if(this.jumppadcount)
493         {
494                 // If got stuck on the jump pad try to reach the farthest visible waypoint
495                 if(this.aistatus & AI_STATUS_OUT_JUMPPAD)
496                 {
497                         if(fabs(this.velocity.z)<50)
498                         {
499                                 entity head, newgoal = NULL;
500                                 float distance, bestdistance = 0;
501
502                                 for (head = findchain(classname, "waypoint"); head; head = head.chain)
503                                 {
504
505                                         distance = vlen(head.origin - this.origin);
506                                         if(distance>1000)
507                                                 continue;
508
509                                         traceline(this.origin + this.view_ofs , ( ( head.absmin + head.absmax ) * 0.5 ), true, NULL);
510
511                                         if(trace_fraction<1)
512                                                 continue;
513
514                                         if(distance>bestdistance)
515                                         {
516                                                 newgoal = head;
517                                                 bestdistance = distance;
518                                         }
519                                 }
520
521                                 if(newgoal)
522                                 {
523                                         this.ignoregoal = this.goalcurrent;
524                                         this.ignoregoaltime = time + autocvar_bot_ai_ignoregoal_timeout;
525                                         navigation_clearroute(this);
526                                         navigation_routetogoal(this, newgoal, this.origin);
527                                         this.aistatus &= ~AI_STATUS_OUT_JUMPPAD;
528                                 }
529                         }
530                         else
531                                 return;
532                 }
533                 else
534                 {
535                         if(this.velocity.z>0)
536                         {
537                                 float threshold;
538                                 vector velxy = this.velocity; velxy_z = 0;
539                                 threshold = maxspeed * 0.2;
540                                 if(vdist(velxy, <, threshold))
541                                 {
542                                         LOG_TRACE("Warning: ", this.netname, " got stuck on a jumppad (velocity in xy is ", vtos(velxy), "), trying to get out of it now\n");
543                                         this.aistatus |= AI_STATUS_OUT_JUMPPAD;
544                                 }
545                                 return;
546                         }
547
548                         // Don't chase players while using a jump pad
549                         if(IS_PLAYER(this.goalcurrent) || IS_PLAYER(this.goalstack01))
550                                 return;
551                 }
552         }
553         else if(this.aistatus & AI_STATUS_OUT_JUMPPAD)
554                 this.aistatus &= ~AI_STATUS_OUT_JUMPPAD;
555
556         // If there is a trigger_hurt right below try to use the jetpack or make a rocketjump
557         if(skill>6)
558         if (!(IS_ONGROUND(this)))
559         {
560                 tracebox(this.origin, this.mins, this.maxs, this.origin + '0 0 -65536', MOVE_NOMONSTERS, this);
561                 if(tracebox_hits_trigger_hurt(this.origin, this.mins, this.maxs, trace_endpos ))
562                 if(this.items & IT_JETPACK)
563                 {
564                         tracebox(this.origin, this.mins, this.maxs, this.origin + '0 0 65536', MOVE_NOMONSTERS, this);
565                         if(tracebox_hits_trigger_hurt(this.origin, this.mins, this.maxs, trace_endpos + '0 0 1' ))
566                         {
567                                 if(this.velocity.z<0)
568                                 {
569                                         PHYS_INPUT_BUTTON_HOOK(this) = true;
570                                 }
571                         }
572                         else
573                                 PHYS_INPUT_BUTTON_HOOK(this) = true;
574
575                         // If there is no goal try to move forward
576
577                         if(this.goalcurrent==NULL)
578                                 dir = v_forward;
579                         else
580                                 dir = normalize(( ( this.goalcurrent.absmin + this.goalcurrent.absmax ) * 0.5 ) - this.origin);
581
582                         vector xyvelocity = this.velocity; xyvelocity_z = 0;
583                         float xyspeed = xyvelocity * dir;
584
585                         if(xyspeed < (maxspeed / 2))
586                         {
587                                 makevectors(this.v_angle.y * '0 1 0');
588                                 tracebox(this.origin, this.mins, this.maxs, this.origin + (dir * maxspeed * 3), MOVE_NOMONSTERS, this);
589                                 if(trace_fraction==1)
590                                 {
591                                         this.movement_x = dir * v_forward * maxspeed;
592                                         this.movement_y = dir * v_right * maxspeed;
593                                         if (skill < 10)
594                                                 havocbot_keyboard_movement(this, this.origin + dir * 100);
595                                 }
596                         }
597
598                         this.havocbot_blockhead = true;
599
600                         return;
601                 }
602                 else if(this.health>WEP_CVAR(devastator, damage)*0.5)
603                 {
604                         if(this.velocity.z < 0)
605                         if(client_hasweapon(this, WEP_DEVASTATOR, true, false))
606                         {
607                                 this.movement_x = maxspeed;
608
609                                 if(this.rocketjumptime)
610                                 {
611                                         if(time > this.rocketjumptime)
612                                         {
613                                                 PHYS_INPUT_BUTTON_ATCK2(this) = true;
614                                                 this.rocketjumptime = 0;
615                                         }
616                                         return;
617                                 }
618
619                                 PS(this).m_switchweapon = WEP_DEVASTATOR;
620                                 this.v_angle_x = 90;
621                                 PHYS_INPUT_BUTTON_ATCK(this) = true;
622                                 this.rocketjumptime = time + WEP_CVAR(devastator, detonatedelay);
623                                 return;
624                         }
625                 }
626                 else
627                 {
628                         // If there is no goal try to move forward
629                         if(this.goalcurrent==NULL)
630                                 this.movement_x = maxspeed;
631                 }
632         }
633
634         // If we are under water with no goals, swim up
635         if(this.waterlevel)
636         if(this.goalcurrent==NULL)
637         {
638                 dir = '0 0 0';
639                 if(this.waterlevel>WATERLEVEL_SWIMMING)
640                         dir.z = 1;
641                 else if(this.velocity.z >= 0 && !(this.waterlevel == WATERLEVEL_WETFEET && this.watertype == CONTENT_WATER))
642                         PHYS_INPUT_BUTTON_JUMP(this) = true;
643                 else
644                         PHYS_INPUT_BUTTON_JUMP(this) = false;
645                 makevectors(this.v_angle.y * '0 1 0');
646                 this.movement_x = dir * v_forward * maxspeed;
647                 this.movement_y = dir * v_right * maxspeed;
648                 this.movement_z = dir * v_up * maxspeed;
649         }
650
651         // if there is nowhere to go, exit
652         if (this.goalcurrent == NULL)
653                 return;
654
655         if (this.goalcurrent)
656                 navigation_poptouchedgoals(this);
657
658         // if ran out of goals try to use an alternative goal or get a new strategy asap
659         if(this.goalcurrent == NULL)
660         {
661                 this.bot_strategytime = 0;
662                 return;
663         }
664
665
666         if(autocvar_bot_debug_goalstack)
667                 debuggoalstack(this);
668
669         m1 = this.goalcurrent.origin + this.goalcurrent.mins;
670         m2 = this.goalcurrent.origin + this.goalcurrent.maxs;
671         destorg = this.origin;
672         destorg.x = bound(m1_x, destorg.x, m2_x);
673         destorg.y = bound(m1_y, destorg.y, m2_y);
674         destorg.z = bound(m1_z, destorg.z, m2_z);
675         diff = destorg - this.origin;
676         //dist = vlen(diff);
677         dir = normalize(diff);
678         flatdir = diff;flatdir.z = 0;
679         flatdir = normalize(flatdir);
680         gco = (this.goalcurrent.absmin + this.goalcurrent.absmax) * 0.5;
681
682         //if (this.bot_dodgevector_time < time)
683         {
684         //      this.bot_dodgevector_time = time + cvar("bot_ai_dodgeupdateinterval");
685         //      this.bot_dodgevector_jumpbutton = 1;
686                 evadeobstacle = '0 0 0';
687                 evadelava = '0 0 0';
688
689                 if (this.waterlevel)
690                 {
691                         if(this.waterlevel>WATERLEVEL_SWIMMING)
692                         {
693                         //      flatdir_z = 1;
694                                 this.aistatus |= AI_STATUS_OUT_WATER;
695                         }
696                         else
697                         {
698                                 if(this.velocity.z >= 0 && !(this.watertype == CONTENT_WATER && gco.z < this.origin.z) &&
699                                         ( !(this.waterlevel == WATERLEVEL_WETFEET && this.watertype == CONTENT_WATER) || this.aistatus & AI_STATUS_OUT_WATER))
700                                         PHYS_INPUT_BUTTON_JUMP(this) = true;
701                                 else
702                                         PHYS_INPUT_BUTTON_JUMP(this) = false;
703                         }
704                         dir = normalize(flatdir);
705                         makevectors(this.v_angle.y * '0 1 0');
706                 }
707                 else
708                 {
709                         if(this.aistatus & AI_STATUS_OUT_WATER)
710                                 this.aistatus &= ~AI_STATUS_OUT_WATER;
711
712                         // jump if going toward an obstacle that doesn't look like stairs we
713                         // can walk up directly
714                         tracebox(this.origin, this.mins, this.maxs, this.origin + this.velocity * 0.2, false, this);
715                         if (trace_fraction < 1)
716                         if (trace_plane_normal.z < 0.7)
717                         {
718                                 s = trace_fraction;
719                                 tracebox(this.origin + stepheightvec, this.mins, this.maxs, this.origin + this.velocity * 0.2 + stepheightvec, false, this);
720                                 if (trace_fraction < s + 0.01)
721                                 if (trace_plane_normal.z < 0.7)
722                                 {
723                                         s = trace_fraction;
724                                         tracebox(this.origin + jumpstepheightvec, this.mins, this.maxs, this.origin + this.velocity * 0.2 + jumpstepheightvec, false, this);
725                                         if (trace_fraction > s)
726                                                 PHYS_INPUT_BUTTON_JUMP(this) = true;
727                                 }
728                         }
729
730                         // avoiding dangers and obstacles
731                         vector dst_ahead, dst_down;
732                         makevectors(this.v_angle.y * '0 1 0');
733                         dst_ahead = this.origin + this.view_ofs + (this.velocity * 0.4) + (v_forward * 32 * 3);
734                         dst_down = dst_ahead - '0 0 1500';
735
736                         // Look ahead
737                         traceline(this.origin + this.view_ofs, dst_ahead, true, NULL);
738
739                         // Check head-banging against walls
740                         if(vdist(this.origin + this.view_ofs - trace_endpos, <, 25) && !(this.aistatus & AI_STATUS_OUT_WATER))
741                         {
742                                 PHYS_INPUT_BUTTON_JUMP(this) = true;
743                                 if(this.facingwalltime && time > this.facingwalltime)
744                                 {
745                                         this.ignoregoal = this.goalcurrent;
746                                         this.ignoregoaltime = time + autocvar_bot_ai_ignoregoal_timeout;
747                                         this.bot_strategytime = 0;
748                                         return;
749                                 }
750                                 else
751                                 {
752                                         this.facingwalltime = time + 0.05;
753                                 }
754                         }
755                         else
756                         {
757                                 this.facingwalltime = 0;
758
759                                 if(this.ignoregoal != NULL && time > this.ignoregoaltime)
760                                 {
761                                         this.ignoregoal = NULL;
762                                         this.ignoregoaltime = 0;
763                                 }
764                         }
765
766                         // Check for water/slime/lava and dangerous edges
767                         // (only when the bot is on the ground or jumping intentionally)
768                         this.aistatus &= ~AI_STATUS_DANGER_AHEAD;
769
770                         if(trace_fraction == 1 && this.jumppadcount == 0 && !this.goalcurrent.wphardwired )
771                         if((IS_ONGROUND(this)) || (this.aistatus & AI_STATUS_RUNNING) || PHYS_INPUT_BUTTON_JUMP(this))
772                         {
773                                 // Look downwards
774                                 traceline(dst_ahead , dst_down, true, NULL);
775                         //      te_lightning2(NULL, this.origin, dst_ahead);    // Draw "ahead" look
776                         //      te_lightning2(NULL, dst_ahead, dst_down);               // Draw "downwards" look
777                                 if(trace_endpos.z < this.origin.z + this.mins.z)
778                                 {
779                                         s = pointcontents(trace_endpos + '0 0 1');
780                                         if (s != CONTENT_SOLID)
781                                         if (s == CONTENT_LAVA || s == CONTENT_SLIME)
782                                                 evadelava = normalize(this.velocity) * -1;
783                                         else if (s == CONTENT_SKY)
784                                                 evadeobstacle = normalize(this.velocity) * -1;
785                                         else if (!boxesoverlap(dst_ahead - this.view_ofs + this.mins, dst_ahead - this.view_ofs + this.maxs,
786                                                                 this.goalcurrent.absmin, this.goalcurrent.absmax))
787                                         {
788                                                 // if ain't a safe goal with "holes" (like the jumpad on soylent)
789                                                 // and there is a trigger_hurt below
790                                                 if(tracebox_hits_trigger_hurt(dst_ahead, this.mins, this.maxs, trace_endpos))
791                                                 {
792                                                         // Remove dangerous dynamic goals from stack
793                                                         LOG_TRACE("bot ", this.netname, " avoided the goal ", this.goalcurrent.classname, " ", etos(this.goalcurrent), " because it led to a dangerous path; goal stack cleared\n");
794                                                         navigation_clearroute(this);
795                                                         return;
796                                                 }
797                                         }
798                                 }
799                         }
800
801                         dir = flatdir;
802                         evadeobstacle.z = 0;
803                         evadelava.z = 0;
804                         makevectors(this.v_angle.y * '0 1 0');
805
806                         if(evadeobstacle!='0 0 0'||evadelava!='0 0 0')
807                                 this.aistatus |= AI_STATUS_DANGER_AHEAD;
808                 }
809
810                 dodge = havocbot_dodge(this);
811                 dodge = dodge * bound(0,0.5+(skill+this.bot_dodgeskill)*0.1,1);
812                 evadelava = evadelava * bound(1,3-(skill+this.bot_dodgeskill),3); //Noobs fear lava a lot and take more distance from it
813                 traceline(this.origin, ( ( this.enemy.absmin + this.enemy.absmax ) * 0.5 ), true, NULL);
814                 if(IS_PLAYER(trace_ent))
815                         dir = dir * bound(0,(skill+this.bot_dodgeskill)/7,1);
816
817                 dir = normalize(dir + dodge + evadeobstacle + evadelava);
818         //      this.bot_dodgevector = dir;
819         //      this.bot_dodgevector_jumpbutton = PHYS_INPUT_BUTTON_JUMP(this);
820         }
821
822         if(time < this.ladder_time)
823         {
824                 if(this.goalcurrent.origin.z + this.goalcurrent.mins.z > this.origin.z + this.mins.z)
825                 {
826                         if(this.origin.z + this.mins.z  < this.ladder_entity.origin.z + this.ladder_entity.maxs.z)
827                                 dir.z = 1;
828                 }
829                 else
830                 {
831                         if(this.origin.z + this.mins.z  > this.ladder_entity.origin.z + this.ladder_entity.mins.z)
832                                 dir.z = -1;
833                 }
834         }
835
836         //dir = this.bot_dodgevector;
837         //if (this.bot_dodgevector_jumpbutton)
838         //      PHYS_INPUT_BUTTON_JUMP(this) = true;
839         this.movement_x = dir * v_forward * maxspeed;
840         this.movement_y = dir * v_right * maxspeed;
841         this.movement_z = dir * v_up * maxspeed;
842
843         // Emulate keyboard interface
844         if (skill < 10)
845                 havocbot_keyboard_movement(this, destorg);
846
847         // Bunnyhop!
848 //      if(this.aistatus & AI_STATUS_ROAMING)
849         if(this.goalcurrent)
850         if(skill+this.bot_moveskill >= autocvar_bot_ai_bunnyhop_skilloffset)
851                 havocbot_bunnyhop(this, dir);
852
853         if ((dir * v_up) >= autocvar_sv_jumpvelocity*0.5 && (IS_ONGROUND(this))) PHYS_INPUT_BUTTON_JUMP(this) = true;
854         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;
855         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);
856 }
857
858 void havocbot_chooseenemy(entity this)
859 {
860         entity head, best, head2;
861         float rating, bestrating, hf;
862         vector eye, v;
863         if (autocvar_bot_nofire || IS_INDEPENDENT_PLAYER(this))
864         {
865                 this.enemy = NULL;
866                 return;
867         }
868         if (this.enemy)
869         {
870                 if (!bot_shouldattack(this, this.enemy))
871                 {
872                         // enemy died or something, find a new target
873                         this.enemy = NULL;
874                         this.havocbot_chooseenemy_finished = time;
875                 }
876                 else if (this.havocbot_stickenemy)
877                 {
878                         // tracking last chosen enemy
879                         // if enemy is visible
880                         // and not really really far away
881                         // and we're not severely injured
882                         // then keep tracking for a half second into the future
883                         traceline(this.origin+this.view_ofs, ( this.enemy.absmin + this.enemy.absmax ) * 0.5,false,NULL);
884                         if (trace_ent == this.enemy || trace_fraction == 1)
885                         if (vdist(((this.enemy.absmin + this.enemy.absmax) * 0.5) - this.origin, <, 1000))
886                         if (this.health > 30)
887                         {
888                                 // remain tracking him for a shot while (case he went after a small corner or pilar
889                                 this.havocbot_chooseenemy_finished = time + 0.5;
890                                 return;
891                         }
892                         // enemy isn't visible, or is far away, or we're injured severely
893                         // so stop preferring this enemy
894                         // (it will still take a half second until a new one is chosen)
895                         this.havocbot_stickenemy = 0;
896                 }
897         }
898         if (time < this.havocbot_chooseenemy_finished)
899                 return;
900         this.havocbot_chooseenemy_finished = time + autocvar_bot_ai_enemydetectioninterval;
901         eye = this.origin + this.view_ofs;
902         best = NULL;
903         bestrating = 100000000;
904         head = head2 = findchainfloat(bot_attack, true);
905
906         // Backup hit flags
907         hf = this.dphitcontentsmask;
908
909         // Search for enemies, if no enemy can be seen directly try to look through transparent objects
910
911         this.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_CORPSE;
912
913         bool scan_transparent = false;
914         bool scan_secondary_targets = false;
915         bool have_secondary_targets = false;
916         while(true)
917         {
918                 scan_secondary_targets = false;
919 LABEL(scan_targets)
920                 for( ; head; head = head.chain)
921                 {
922                         if(!scan_secondary_targets)
923                         {
924                                 if(head.classname == "misc_breakablemodel")
925                                 {
926                                         have_secondary_targets = true;
927                                         continue;
928                                 }
929                         }
930                         else
931                         {
932                                 if(head.classname != "misc_breakablemodel")
933                                         continue;
934                         }
935
936                         v = (head.absmin + head.absmax) * 0.5;
937                         rating = vlen(v - eye);
938                         if (rating<autocvar_bot_ai_enemydetectionradius)
939                         if (bestrating > rating)
940                         if (bot_shouldattack(this, head))
941                         {
942                                 traceline(eye, v, true, this);
943                                 if (trace_ent == head || trace_fraction >= 1)
944                                 {
945                                         best = head;
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                         head = head2;
956                         bestrating = 100000000;
957                         goto scan_targets;
958                 }
959
960                 // I want to do a second scan if no enemy was found or I don't have weapons
961                 // TODO: Perform the scan when using the rifle (requires changes on the rifle code)
962                 if(best || this.weapons) // || this.weapon == WEP_RIFLE.m_id
963                         break;
964                 if(scan_transparent)
965                         break;
966
967                 // Set flags to see through transparent objects
968                 this.dphitcontentsmask |= DPCONTENTS_OPAQUE;
969
970                 head = head2;
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\n");
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)\n");
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),"\n");
1171                                         this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_LINKING;
1172                                         remove(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\n");
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\n");
1195                         remove(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\n");
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\n");
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),"\n");
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         entity head;
1237         for(head = NULL; (head = find(head, classname, "trigger_teleport")); )
1238         {
1239                 if(WarpZoneLib_BoxTouchesBrush(pos, pos, head, NULL))
1240                 {
1241                         wp.wpflags |= WAYPOINTFLAG_TELEPORT;
1242                         this.lastteleporttime = 0;
1243                 }
1244         }
1245
1246 /*
1247         if(wp.wpflags & WAYPOINTFLAG_TELEPORT)
1248                 print("routing to a teleporter\n");
1249         else
1250                 print("routing to a non-teleporter\n");
1251 */
1252
1253         return CMD_STATUS_EXECUTING;
1254 }
1255
1256 float havocbot_resetgoal(entity this)
1257 {
1258         navigation_clearroute(this);
1259         return CMD_STATUS_FINISHED;
1260 }
1261
1262 void havocbot_setupbot(entity this)
1263 {
1264         this.bot_ai = havocbot_ai;
1265         this.cmd_moveto = havocbot_moveto;
1266         this.cmd_resetgoal = havocbot_resetgoal;
1267
1268         havocbot_chooserole(this);
1269 }
1270
1271 vector havocbot_dodge(entity this)
1272 {
1273         // LordHavoc: disabled because this is too expensive
1274         return '0 0 0';
1275 #if 0
1276         entity head;
1277         vector dodge, v, n;
1278         float danger, bestdanger, vl, d;
1279         dodge = '0 0 0';
1280         bestdanger = -20;
1281         // check for dangerous objects near bot or approaching bot
1282         head = findchainfloat(bot_dodge, true);
1283         while(head)
1284         {
1285                 if (head.owner != this)
1286                 {
1287                         vl = vlen(head.velocity);
1288                         if (vl > autocvar_sv_maxspeed * 0.3)
1289                         {
1290                                 n = normalize(head.velocity);
1291                                 v = this.origin - head.origin;
1292                                 d = v * n;
1293                                 if (d > (0 - head.bot_dodgerating))
1294                                 if (d < (vl * 0.2 + head.bot_dodgerating))
1295                                 {
1296                                         // calculate direction and distance from the flight path, by removing the forward axis
1297                                         v = v - (n * (v * n));
1298                                         danger = head.bot_dodgerating - vlen(v);
1299                                         if (bestdanger < danger)
1300                                         {
1301                                                 bestdanger = danger;
1302                                                 // dodge to the side of the object
1303                                                 dodge = normalize(v);
1304                                         }
1305                                 }
1306                         }
1307                         else
1308                         {
1309                                 danger = head.bot_dodgerating - vlen(head.origin - this.origin);
1310                                 if (bestdanger < danger)
1311                                 {
1312                                         bestdanger = danger;
1313                                         dodge = normalize(this.origin - head.origin);
1314                                 }
1315                         }
1316                 }
1317                 head = head.chain;
1318         }
1319         return dodge;
1320 #endif
1321 }