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