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