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