]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/bot/havocbot/havocbot.qc
random, weird, bot "fixes"/hacks
[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                         self.havocbot_role();
25                 }
26
27                 // TODO: tracewalk() should take care of this job (better path finding under water)
28                 // if we don't have a goal and we're under water look for a waypoint near the "shore" and push it
29                 if(self.deadflag != DEAD_NO)
30                 if(self.goalcurrent==world)
31                 if(self.waterlevel==WATERLEVEL_SWIMMING || self.aistatus & AI_STATUS_OUT_WATER)
32                 {
33                         // Look for the closest waypoint out of water
34                         local entity newgoal, head;
35                         local float bestdistance, distance;
36
37                         newgoal = world;
38                         bestdistance = 10000;
39                         for (head = findchain(classname, "waypoint"); head; head = head.chain)
40                         {
41                                 distance = vlen(head.origin - self.origin);
42                                 if(distance>10000)
43                                         continue;
44
45                                 if(head.origin_z < self.origin_z)
46                                         continue;
47
48                                 if(head.origin_z - self.origin_z - self.view_ofs_z > 100)
49                                         continue;
50
51                                 if (pointcontents(head.origin + head.maxs + '0 0 1') != CONTENT_EMPTY)
52                                         continue;
53
54                                 traceline(self.origin + self.view_ofs , head.origin, TRUE, head);
55
56                                 if(trace_fraction<1)
57                                         continue;
58
59                                 if(distance<bestdistance)
60                                 {
61                                         newgoal = head;
62                                         bestdistance = distance;
63                                 }
64                         }
65
66                         if(newgoal)
67                         {
68                         //      te_wizspike(newgoal.origin);
69                                 navigation_pushroute(newgoal);
70                         }
71                 }
72
73                 // token has been used this frame
74                 bot_strategytoken_taken = TRUE;
75         }
76
77         if(self.deadflag != DEAD_NO)
78                 return;
79
80         havocbot_chooseenemy();
81         if (self.bot_chooseweapontime < time )
82         {
83                 self.bot_chooseweapontime = time + cvar("bot_ai_chooseweaponinterval");
84                 havocbot_chooseweapon();
85         }
86         havocbot_aim();
87         lag_update();
88         if (self.bot_aimtarg)
89         {
90                 self.aistatus |= AI_STATUS_ATTACKING;
91                 self.aistatus &~= AI_STATUS_ROAMING;
92
93                 if(self.weapons)
94                 {
95                         weapon_action(self.weapon, WR_AIM);
96                         if (cvar("bot_nofire") || IS_INDEPENDENT_PLAYER(self))
97                         {
98                                 self.BUTTON_ATCK = FALSE;
99                                 self.BUTTON_ATCK2 = FALSE;
100                         }
101                         else
102                         {
103                                 if(self.BUTTON_ATCK||self.BUTTON_ATCK2)
104                                         self.lastfiredweapon = self.weapon;
105                         }
106                 }
107                 else
108                 {
109                         if(self.bot_aimtarg.classname=="player")
110                                 bot_aimdir(self.bot_aimtarg.origin + self.bot_aimtarg.view_ofs - self.origin - self.view_ofs , -1);
111                 }
112         }
113         else if (self.goalcurrent)
114         {
115                 self.aistatus |= AI_STATUS_ROAMING;
116                 self.aistatus &~= AI_STATUS_ATTACKING;
117
118                 local vector now,v,next;//,heading;
119                 local float aimdistance,skillblend,distanceblend,blend;
120                 next = now = self.goalcurrent.origin - (self.origin + self.view_ofs);
121                 aimdistance = vlen(now);
122                 //heading = self.velocity;
123                 //dprint(self.goalstack01.classname,etos(self.goalstack01),"\n");
124                 if(
125                         self.goalstack01 != self && self.goalstack01 != world && self.aistatus & AI_STATUS_RUNNING == 0 &&
126                         !(self.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT)
127                 )
128                         next = self.goalstack01.origin - (self.origin + self.view_ofs);
129
130                 skillblend=bound(0,(skill+self.bot_moveskill-2.5)*0.5,1); //lower skill player can't preturn
131                 distanceblend=bound(0,aimdistance/cvar("bot_ai_keyboard_distance"),1);
132                 blend = skillblend * (1-distanceblend);
133                 //v = (now * (distanceblend) + next * (1-distanceblend)) * (skillblend) + now * (1-skillblend);
134                 //v = now * (distanceblend) * (skillblend) + next * (1-distanceblend) * (skillblend) + now * (1-skillblend);
135                 //v = now * ((1-skillblend) + (distanceblend) * (skillblend)) + next * (1-distanceblend) * (skillblend);
136                 v = now + blend * (next - now);
137                 //dprint(etos(self), " ");
138                 //dprint(vtos(now), ":", vtos(next), "=", vtos(v), " (blend ", ftos(blend), ")\n");
139                 //v = now * (distanceblend) + next * (1-distanceblend);
140                 if (self.waterlevel < WATERLEVEL_SWIMMING)
141                         v_z = 0;
142                 //dprint("walk at:", vtos(v), "\n");
143                 //te_lightning2(world, self.origin, self.goalcurrent.origin);
144                 bot_aimdir(v, -1);
145         }
146         havocbot_movetogoal();
147 };
148
149 void havocbot_keyboard_movement(vector destorg)
150 {
151         local vector keyboard;
152         local float blend, maxspeed;
153         float sk;
154
155         sk = skill + self.bot_moveskill;
156
157         maxspeed = cvar("sv_maxspeed");
158
159         if (time < self.havocbot_keyboardtime)
160                 return;
161
162         self.havocbot_keyboardtime =
163                 max(
164                         self.havocbot_keyboardtime
165                                 + 0.05/max(1, sk+self.havocbot_keyboardskill)
166                                 + random()*0.025/max(0.00025, skill+self.havocbot_keyboardskill)
167                 , time);
168         keyboard = self.movement * (1.0 / maxspeed);
169
170         local float trigger, trigger1;
171         blend = bound(0,sk*0.1,1);
172         trigger = cvar("bot_ai_keyboard_treshold");
173         trigger1 = 0 - trigger;
174
175         // categorize forward movement
176         // at skill < 1.5 only forward
177         // at skill < 2.5 only individual directions
178         // at skill < 4.5 only individual directions, and forward diagonals
179         // at skill >= 4.5, all cases allowed
180         if (keyboard_x > trigger)
181         {
182                 keyboard_x = 1;
183                 if (sk < 2.5)
184                         keyboard_y = 0;
185         }
186         else if (keyboard_x < trigger1 && sk > 1.5)
187         {
188                 keyboard_x = -1;
189                 if (sk < 4.5)
190                         keyboard_y = 0;
191         }
192         else
193         {
194                 keyboard_x = 0;
195                 if (sk < 1.5)
196                         keyboard_y = 0;
197         }
198         if (sk < 4.5)
199                 keyboard_z = 0;
200
201         if (keyboard_y > trigger)
202                 keyboard_y = 1;
203         else if (keyboard_y < trigger1)
204                 keyboard_y = -1;
205         else
206                 keyboard_y = 0;
207
208         if (keyboard_z > trigger)
209                 keyboard_z = 1;
210         else if (keyboard_z < trigger1)
211                 keyboard_z = -1;
212         else
213                 keyboard_z = 0;
214
215         self.havocbot_keyboard = keyboard * maxspeed;
216         if (self.havocbot_ducktime>time) self.BUTTON_CROUCH=TRUE;
217
218         keyboard = self.havocbot_keyboard;
219         blend = bound(0,vlen(destorg-self.origin)/cvar("bot_ai_keyboard_distance"),1); // When getting close move with 360 degree
220         //dprint("movement ", vtos(self.movement), " keyboard ", vtos(keyboard), " blend ", ftos(blend), "\n");
221         self.movement = self.movement + (keyboard - self.movement) * blend;
222 };
223
224 void havocbot_bunnyhop(vector dir)
225 {
226         local float bunnyhopdistance;
227         local vector deviation;
228         local float maxspeed;
229
230         if(cvar("g_midair"))
231                 return;
232
233         // Don't jump when using some weapons
234         /*
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
243         maxspeed = cvar("sv_maxspeed");
244
245         if(self.aistatus & AI_STATUS_DANGER_AHEAD)
246         {
247                 self.aistatus &~= AI_STATUS_RUNNING;
248                 self.BUTTON_JUMP = FALSE;
249                 self.bot_canruntogoal = 0;
250                 self.bot_timelastseengoal = 0;
251                 return;
252         }
253
254         if(self.waterlevel > WATERLEVEL_WETFEET)
255         {
256                 self.aistatus &~= AI_STATUS_RUNNING;
257                 return;
258         }
259
260         if(self.bot_lastseengoal != self.goalcurrent && !(self.aistatus & AI_STATUS_RUNNING))
261         {
262                 self.bot_canruntogoal = 0;
263                 self.bot_timelastseengoal = 0;
264         }
265
266         bunnyhopdistance = vlen(self.origin - self.goalcurrent.origin);
267
268         // Run only to visible goals
269         if(self.flags & FL_ONGROUND)
270         if(self.speed==maxspeed)
271         if(checkpvs(self.origin + self.view_ofs, self.goalcurrent))
272         {
273                         self.bot_lastseengoal = self.goalcurrent;
274
275                         // seen it before
276                         if(self.bot_timelastseengoal)
277                         {
278                                 // for a period of time
279                                 if(time - self.bot_timelastseengoal > cvar("bot_ai_bunnyhop_firstjumpdelay"))
280                                 {
281                                         local float checkdistance;
282                                         checkdistance = TRUE;
283
284                                         // don't run if it is too close
285                                         if(self.bot_canruntogoal==0)
286                                         {
287                                                 if(bunnyhopdistance > cvar("bot_ai_bunnyhop_startdistance"))
288                                                         self.bot_canruntogoal = 1;
289                                                 else
290                                                         self.bot_canruntogoal = -1;
291                                         }
292
293                                         if(self.bot_canruntogoal != 1)
294                                                 return;
295
296                                         if(self.aistatus & AI_STATUS_ROAMING)
297                                         if(self.goalcurrent.classname=="waypoint")
298                                         if not(self.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL)
299                                         if(fabs(self.goalcurrent.origin_z - self.origin_z) < self.maxs_z - self.mins_z)
300                                         if(self.goalstack01!=world)
301                                         {
302                                                 deviation = vectoangles(self.goalstack01.origin - self.origin) - vectoangles(self.goalcurrent.origin - self.origin);
303                                                 while (deviation_y < -180) deviation_y = deviation_y + 360;
304                                                 while (deviation_y > 180) deviation_y = deviation_y - 360;
305
306                                                 if(fabs(deviation_y) < 20)
307                                                 if(bunnyhopdistance < vlen(self.origin - self.goalstack01.origin))
308                                                 if(fabs(self.goalstack01.origin_z - self.goalcurrent.origin_z) < self.maxs_z - self.mins_z)
309                                                 {
310                                                         if(vlen(self.goalcurrent.origin - self.goalstack01.origin) > cvar("bot_ai_bunnyhop_startdistance"))
311                                                         if(checkpvs(self.origin + self.view_ofs, self.goalstack01))
312                                                         {
313                                                                 checkdistance = FALSE;
314                                                         }
315                                                 }
316                                         }
317
318                                         if(checkdistance)
319                                         {
320                                                 self.aistatus &~= AI_STATUS_RUNNING;
321                                                 if(bunnyhopdistance > cvar("bot_ai_bunnyhop_stopdistance"))
322                                                         self.BUTTON_JUMP = TRUE;
323                                         }
324                                         else
325                                         {
326                                                 self.aistatus |= AI_STATUS_RUNNING;
327                                                 self.BUTTON_JUMP = TRUE;
328                                         }
329                                 }
330                         }
331                         else
332                         {
333                                 self.bot_timelastseengoal = time;
334                         }
335         }
336         else
337         {
338                 self.bot_timelastseengoal = 0;
339         }
340
341         // Release jump button
342         if(!cvar("sv_pogostick"))
343         if(self.flags & FL_ONGROUND == 0)
344         {
345                 if(self.velocity_z < 0 || vlen(self.velocity)<maxspeed)
346                         self.BUTTON_JUMP = FALSE;
347
348                 // Strafe
349                 if(self.aistatus & AI_STATUS_RUNNING)
350                 if(vlen(self.velocity)>maxspeed)
351                 {
352                         deviation = vectoangles(dir) - vectoangles(self.velocity);
353                         while (deviation_y < -180) deviation_y = deviation_y + 360;
354                         while (deviation_y > 180) deviation_y = deviation_y - 360;
355
356                         if(fabs(deviation_y)>10)
357                                 self.movement_x = 0;
358
359                         if(deviation_y>10)
360                                 self.movement_y = maxspeed * -1;
361                         else if(deviation_y<10)
362                                 self.movement_y = maxspeed;
363
364                 }
365         }
366 };
367
368 void havocbot_movetogoal()
369 {
370         local vector destorg;
371         local vector diff;
372         local vector dir;
373         local vector flatdir;
374         local vector m1;
375         local vector m2;
376         local vector evadeobstacle;
377         local vector evadelava;
378         local float s;
379         local float maxspeed;
380         //local float dist;
381         local vector dodge;
382         //if (self.goalentity)
383         //      te_lightning2(self, self.origin, (self.goalentity.absmin + self.goalentity.absmax) * 0.5);
384         self.movement = '0 0 0';
385         maxspeed = cvar("sv_maxspeed");
386
387         // Jetpack navigation
388         if(self.goalcurrent)
389         if(self.navigation_jetpack_goal)
390         if(self.goalcurrent==self.navigation_jetpack_goal)
391         if(self.ammo_fuel)
392         {
393                 #ifdef DEBUG_BOT_GOALSTACK
394                         debuggoalstack();
395                         te_wizspike(self.navigation_jetpack_point);
396                 #endif
397
398                 // Take off
399                 if not(self.aistatus & AI_STATUS_JETPACK_FLYING)
400                 {
401                         // Brake almost completely so it can get a good direction
402                         if(vlen(self.velocity)>10)
403                                 return;
404                         self.aistatus |= AI_STATUS_JETPACK_FLYING;
405                 }
406
407                 makevectors(self.v_angle_y * '0 1 0');
408                 dir = normalize(self.navigation_jetpack_point - self.origin);
409
410                 // Landing
411                 if(self.aistatus & AI_STATUS_JETPACK_LANDING)
412                 {
413                         // Calculate brake distance in xy
414                         float db, v, d;
415                         vector dxy;
416
417                         dxy = self.origin - self.goalcurrent.origin; dxy_z = 0;
418                         d = vlen(dxy);
419                         v = vlen(self.velocity -  self.velocity_z * '0 0 1');
420                         db = (pow(v,2) / (cvar("g_jetpack_acceleration_side") * 2)) + 100;
421                 //      dprint("distance ", ftos(ceil(d)), " velocity ", ftos(ceil(v)), " brake at ", ftos(ceil(db)), "\n");
422                         if(d < db || d < 500)
423                         {
424                                 // Brake
425                                 if(fabs(self.velocity_x)>maxspeed*0.3)
426                                 {
427                                         self.movement_x = dir * v_forward * -maxspeed;
428                                         return;
429                                 }
430                                 // Switch to normal mode
431                                 self.navigation_jetpack_goal = world;
432                                 self.aistatus &~= AI_STATUS_JETPACK_LANDING;
433                                 self.aistatus &~= AI_STATUS_JETPACK_FLYING;
434                                 return;
435                         }
436                 }
437                 else if(checkpvs(self.origin,self.goalcurrent))
438                 {
439                         // If I can see the goal switch to landing code
440                         self.aistatus &~= AI_STATUS_JETPACK_FLYING;
441                         self.aistatus |= AI_STATUS_JETPACK_LANDING;
442                         return;
443                 }
444
445                 // Flying
446                 self.BUTTON_HOOK = TRUE;
447                 if(self.navigation_jetpack_point_z - PL_MAX_z + PL_MIN_z < self.origin_z)
448                 {
449                         self.movement_x = dir * v_forward * maxspeed;
450                         self.movement_y = dir * v_right * maxspeed;
451                 }
452                 return;
453         }
454
455         // Handling of jump pads
456         if(self.jumppadcount)
457         {
458                 if(self.flags & FL_ONGROUND)
459                 {
460                         self.jumppadcount = FALSE;
461                         if(self.aistatus & AI_STATUS_OUT_JUMPPAD)
462                                 self.aistatus &~= AI_STATUS_OUT_JUMPPAD;
463                 }
464
465                 // If got stuck on the jump pad try to reach the farther visible item
466                 if(self.aistatus & AI_STATUS_OUT_JUMPPAD)
467                 {
468                         if(fabs(self.velocity_z)<50)
469                         {
470                                 local entity head, newgoal;
471                                 local float distance, bestdistance;
472
473                                 for (head = findchainfloat(bot_pickup, TRUE); head; head = head.chain)
474                                 {
475                                         if(head.classname=="worldspawn")
476                                                 continue;
477
478                                         distance = vlen(head.origin - self.origin);
479                                         if(distance>1000)
480                                                 continue;
481
482                                         traceline(self.origin + self.view_ofs , head.origin, TRUE, world);
483
484                                         if(trace_fraction<1)
485                                                 continue;
486
487                                         if(distance>bestdistance)
488                                         {
489                                                 newgoal = head;
490                                                 bestdistance = distance;
491                                         }
492                                 }
493
494                                 if(newgoal)
495                                 {
496                                         self.ignoregoal = self.goalcurrent;
497                                         self.ignoregoaltime = time + cvar("bot_ai_ignoregoal_timeout");
498                                         navigation_clearroute();
499                                         navigation_routetogoal(newgoal, self.origin);
500                                         self.aistatus &~= AI_STATUS_OUT_JUMPPAD;
501                                 }
502                         }
503                         else
504                                 return;
505                 }
506                 else
507                 {
508                         if(self.velocity_z>0)
509                         {
510                                 local float threshold;
511                                 threshold = maxspeed * 0.2;
512                                 if(fabs(self.velocity_x) < threshold  &&  fabs(self.velocity_y) < threshold)
513                                         self.aistatus |= AI_STATUS_OUT_JUMPPAD;
514                                 return;
515                         }
516                 }
517         }
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>cvar("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 + cvar("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 + cvar("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)
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 >= cvar("bot_ai_bunnyhop_skilloffset"))
815                 havocbot_bunnyhop(dir);
816
817         if ((dir * v_up) >= cvar("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 (cvar("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 + cvar("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 + cvar("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<cvar("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 < cvar("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 = cvar("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(cvar("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 #ifdef 0
1034         // TODO: This disabled code is not working well and got replaced by custom weapon priorities.
1035         // However, this logic should be refactored and moved to weapons code so each new weapon can be
1036         // evaluated dynamically by bots without updating the "ai" or config files.     --mand1nga
1037         float s, distancefromfloor, currentscore;
1038
1039
1040         // Formula:
1041         //      (Damage/Sec * Weapon spefic change to get that damage)
1042         //      *(Time to get to target * weapon specfic hitchange bonus) / (in a time of maxdelaytime)
1043         //      *(Spread change of hit) // if it applies
1044         //      *(Penality for target beeing in air)
1045         // %weaponaddpoint
1046
1047         traceline(self.enemy.origin,self.enemy.origin-'0 0 1000',TRUE,world);
1048         distancefromfloor = self.enemy.origin_z - trace_endpos_z;
1049
1050         if (client_hasweapon(self, WEP_MINSTANEX, TRUE, FALSE))
1051                 minstanex = (1000/cvar("g_balance_minstanex_refire")*1.0)
1052                         * (0.5);
1053
1054         if (client_hasweapon(self, WEP_ROCKET_LAUNCHER, TRUE, FALSE)  &&
1055                 !(      cvar("bot_ai_weapon_combo") && self.weapon == WEP_ROCKET_LAUNCHER &&
1056                         af > combo_time
1057                 )
1058         )
1059                 rocket = (cvar("g_balance_rocketlauncher_damage")/cvar("g_balance_rocketlauncher_refire")*0.75)
1060                         * bound(0,(cvar("g_balance_rocketlauncher_speed")/distance*maxdelaytime),1)*1.5;
1061
1062         if (client_hasweapon(self, WEP_NEX, TRUE, FALSE)  &&
1063                 !(      cvar("bot_ai_weapon_combo") && self.weapon == WEP_NEX &&
1064                         af > combo_time
1065                 )
1066         )
1067                 nex = (cvar("g_balance_nex_damage")/cvar("g_balance_nex_refire")*1.0)
1068                         * (0.5);
1069
1070         if (client_hasweapon(self, WEP_HAGAR, TRUE, FALSE) ) // &&
1071         //      !( cvar("bot_ai_weapon_combo") && self.weapon == WEP_HAGAR &&  time < self.bot_lastshot + cvar("g_balance_hagar_primary_refire") ))
1072                 hagar = (cvar("g_balance_hagar_primary_damage")/cvar("g_balance_hagar_primary_refire")*1.0)
1073                         * bound(0,(cvar("g_balance_hagar_primary_speed")/distance*maxdelaytime),1)*0.2;
1074
1075         if (client_hasweapon(self, WEP_GRENADE_LAUNCHER, TRUE, FALSE) &&
1076                 !(
1077                         cvar("bot_ai_weapon_combo") && self.weapon == WEP_GRENADE_LAUNCHER &&
1078                         af > combo_time
1079                 )
1080         )
1081                 grenade = (cvar("g_balance_grenadelauncher_primary_damage")/cvar("g_balance_grenadelauncher_primary_refire")*1.0)
1082                         * bound(0,(cvar("g_balance_grenadelauncher_primary_speed")/distance*maxdelaytime),1)*1.1;
1083
1084         if (client_hasweapon(self, WEP_MINE_LAYER, TRUE, FALSE) &&
1085                 !(
1086                         cvar("bot_ai_weapon_combo") && self.weapon == WEP_MINE_LAYER &&
1087                         af > combo_time
1088                 )
1089         )
1090                 mine = (cvar("g_balance_minelayer_damage")/cvar("g_balance_minelayer_refire")*1.0)
1091                         * bound(0,(cvar("g_balance_minelayer_speed")/distance*maxdelaytime),1)*1.1;
1092
1093         if (client_hasweapon(self, WEP_ELECTRO, TRUE, FALSE) &&
1094                 !(      cvar("bot_ai_weapon_combo") && self.weapon == WEP_ELECTRO &&
1095                         af > combo_time
1096                 )
1097         )
1098                 electro = (cvar("g_balance_electro_primary_damage")/cvar("g_balance_electro_primary_refire")*0.75)
1099                         * bound(0,(cvar("g_balance_electro_primary_speed")/distance*maxdelaytime),1)*1.0;
1100
1101         if (client_hasweapon(self, WEP_CRYLINK, TRUE, FALSE) ) // &&
1102         //      !( self.weapon == WEP_CRYLINK &&  time < self.bot_lastshot + cvar("g_balance_crylink_primary_refire") ))
1103                 crylink = (cvar("g_balance_crylink_primary_damage")/cvar("g_balance_crylink_primary_refire")*1.0)
1104                         * bound(0,(cvar("g_balance_crylink_primary_speed")/distance*maxdelaytime),1)*(64/(32+cvar("g_balance_crylink_primary_spread")*distance))*1.0;
1105
1106         if (client_hasweapon(self, WEP_UZI, TRUE, FALSE) ) // &&
1107         //      !( self.weapon == WEP_UZI &&  time < self.bot_lastshot + cvar("g_balance_uzi_sustained_refire") ))
1108                 uzi = (cvar("g_balance_uzi_sustained_damage")/cvar("g_balance_uzi_sustained_refire")*1.0)
1109                         * bound(0,32/(32+cvar("g_balance_uzi_sustained_spread")*distance),1);
1110
1111         if (client_hasweapon(self, WEP_SHOTGUN, TRUE, FALSE) &&
1112                 !(      cvar("bot_ai_weapon_combo") && self.weapon == WEP_SHOTGUN &&
1113                         af > combo_time
1114                 )
1115         )
1116                 shotgun = (cvar("g_balance_shotgun_primary_damage")*cvar("g_balance_shotgun_primary_bullets")/cvar("g_balance_shotgun_primary_refire")*1.0)
1117                         * bound(0,32/(32+cvar("g_balance_shotgun_primary_spread")*distance),1);
1118
1119         if (client_hasweapon(self, WEP_LASER, FALSE, FALSE) &&
1120                 !(      cvar("bot_ai_weapon_combo") && self.weapon == WEP_LASER &&
1121                         af > combo_time
1122                 )
1123         )
1124                 laser = (cvar("g_balance_laser_primary_damage")/cvar("g_balance_laser_primary_refire")*1.0)
1125                         * bound(0,cvar("g_balance_laser_primary_speed")/distance*maxdelaytime,1);
1126
1127         if((self.enemy.flags & FL_ONGROUND)==FALSE){
1128                 rocket = rocket   * (1.5-bound(0, distancefromfloor/cvar("g_balance_rocketlauncher_radius"         ),0.9)); //slight bigger change
1129                 grenade = grenade * (1.5-bound(0,distancefromfloor/cvar("g_balance_grenadelauncher_primary_radius"),0.95));
1130                 electro = electro * (1.5-bound(0,distancefromfloor/cvar("g_balance_electro_primary_radius"        ),0.95));
1131                 laser = laser     * (1.5-bound(0,distancefromfloor/cvar("g_balance_laser_primary_radius"                  ),0.95));
1132         }
1133         /*
1134         dprint("Floor distance: ",ftos(distancefromfloor),"\n");
1135         dprint("Rocket: " , ftos(rocket  ), "\n");
1136         dprint("Nex: "    , ftos(nex     ), "\n");
1137         dprint("Hagar: "  , ftos(hagar   ), "\n");
1138         dprint("Grenade: ", ftos(grenade ), "\n");
1139         dprint("Mine: "   , ftos(mine    ), "\n");
1140         dprint("Electro: ", ftos(electro ), "\n");
1141         dprint("Crylink: ", ftos(crylink ), "\n");
1142         dprint("Uzi: "    , ftos(uzi     ), "\n");
1143         dprint("Shotgun :", ftos(shotgun ), "\n");
1144         dprint("Laser   :", ftos(laser   ), "\n\n");
1145         */
1146         currentscore = -1;
1147         w = WEP_MINSTANEX        ;s = minstanex;if (s > bestscore){bestscore = s;bestweapon = w;} if (self.switchweapon == w) currentscore = s;
1148         w = WEP_ROCKET_LAUNCHER  ;s = rocket   ;if (s > bestscore){bestscore = s;bestweapon = w;} if (self.switchweapon == w) currentscore = s;
1149         w = WEP_NEX              ;s = nex      ;if (s > bestscore){bestscore = s;bestweapon = w;} if (self.switchweapon == w) currentscore = s;
1150         w = WEP_HAGAR            ;s = hagar    ;if (s > bestscore){bestscore = s;bestweapon = w;} if (self.switchweapon == w) currentscore = s;
1151         w = WEP_GRENADE_LAUNCHER ;s = grenade  ;if (s > bestscore){bestscore = s;bestweapon = w;} if (self.switchweapon == w) currentscore = s;
1152         w = WEP_MINE_LAYER       ;s = mine     ;if (s > bestscore){bestscore = s;bestweapon = w;} if (self.switchweapon == w) currentscore = s;
1153         w = WEP_ELECTRO          ;s = electro  ;if (s > bestscore){bestscore = s;bestweapon = w;} if (self.switchweapon == w) currentscore = s;
1154         w = WEP_CRYLINK          ;s = crylink  ;if (s > bestscore){bestscore = s;bestweapon = w;} if (self.switchweapon == w) currentscore = s;
1155         w = WEP_UZI              ;s = uzi      ;if (s > bestscore){bestscore = s;bestweapon = w;} if (self.switchweapon == w) currentscore = s;
1156         w = WEP_SHOTGUN          ;s = shotgun  ;if (s > bestscore){bestscore = s;bestweapon = w;} if (self.switchweapon == w) currentscore = s;
1157         w = WEP_LASER            ;s = laser    ;if (s > bestscore){bestscore = s;bestweapon = w;} if (self.switchweapon == w) currentscore = s;
1158
1159         // switch if the best weapon would provide a significant damage increase
1160         if (bestscore > currentscore*1.5){
1161                 self.switchweapon = bestweapon;
1162
1163                 // buys time for detonating the rocket. not tested yet
1164                 if ( cvar("bot_ai_weapon_combo") && bestweapon == WEP_ROCKET_LAUNCHER )
1165                         self.bot_chooseweapontime += (distance  / cvar("g_balance_rocketlauncher_speed"));
1166         }
1167 #endif
1168 };
1169
1170 void havocbot_aim()
1171 {
1172         local vector selfvel, enemyvel;
1173 //      if(self.flags & FL_INWATER)
1174 //              return;
1175         if (time < self.nextaim)
1176                 return;
1177         self.nextaim = time + 0.1;
1178         selfvel = self.velocity;
1179         if (!self.waterlevel)
1180                 selfvel_z = 0;
1181         if (self.enemy)
1182         {
1183                 enemyvel = self.enemy.velocity;
1184                 if (!self.enemy.waterlevel)
1185                         enemyvel_z = 0;
1186                 lag_additem(time + self.ping, 0, 0, self.enemy, self.origin, selfvel, self.enemy.origin, enemyvel);
1187         }
1188         else
1189                 lag_additem(time + self.ping, 0, 0, world, self.origin, selfvel, self.goalcurrent.origin, '0 0 0');
1190 };
1191
1192 float havocbot_moveto_refresh_route()
1193 {
1194         // Refresh path to goal if necessary
1195         entity wp;
1196         wp = self.havocbot_personal_waypoint;
1197         navigation_goalrating_start();
1198         navigation_routerating(wp, 10000, 10000);
1199         navigation_goalrating_end();
1200         return self.navigation_hasgoals;
1201 }
1202
1203 float havocbot_moveto(vector pos)
1204 {
1205         local entity wp;
1206
1207         if(self.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
1208         {
1209                 // Step 4: Move to waypoint
1210                 if(self.havocbot_personal_waypoint==world)
1211                 {
1212                         dprint("Error: ", self.netname, " trying to walk to a non existent personal waypoint\n");
1213                         self.aistatus &~= AI_STATUS_WAYPOINT_PERSONAL_GOING;
1214                         return CMD_STATUS_ERROR;
1215                 }
1216
1217                 if (!bot_strategytoken_taken)
1218                 if(self.havocbot_personal_waypoint_searchtime<time)
1219                 {
1220                         bot_strategytoken_taken = TRUE;
1221                         if(havocbot_moveto_refresh_route())
1222                         {
1223                                 dprint(self.netname, " walking to its personal waypoint (after ", ftos(self.havocbot_personal_waypoint_failcounter), " failed attempts)\n");
1224                                 self.havocbot_personal_waypoint_searchtime = time + 10;
1225                                 self.havocbot_personal_waypoint_failcounter = 0;
1226                         }
1227                         else
1228                         {
1229                                 self.havocbot_personal_waypoint_failcounter += 1;
1230                                 self.havocbot_personal_waypoint_searchtime = time + 2;
1231                                 if(self.havocbot_personal_waypoint_failcounter >= 30)
1232                                 {
1233                                         dprint("Warning: can't walk to the personal waypoint located at ", vtos(self.havocbot_personal_waypoint.origin),"\n");
1234                                         self.aistatus &~= AI_STATUS_WAYPOINT_PERSONAL_LINKING;
1235                                         remove(self.havocbot_personal_waypoint);
1236                                         return CMD_STATUS_ERROR;
1237                                 }
1238                                 else
1239                                         dprint(self.netname, " can't walk to its personal waypoint (after ", ftos(self.havocbot_personal_waypoint_failcounter), " failed attempts), trying later\n");
1240                         }
1241                 }
1242
1243                 #ifdef DEBUG_BOT_GOALSTACK
1244                         debuggoalstack();
1245                 #endif
1246
1247                 // Heading
1248                 local vector dir = self.goalcurrent.origin - (self.origin + self.view_ofs);
1249                 dir_z = 0;
1250                 bot_aimdir(dir, -1);
1251
1252                 // Go!
1253                 havocbot_movetogoal();
1254
1255                 if(self.aistatus & AI_STATUS_WAYPOINT_PERSONAL_REACHED)
1256                 {
1257                         // Step 5: Waypoint reached
1258                         dprint(self.netname, "'s personal waypoint reached\n");
1259                         remove(self.havocbot_personal_waypoint);
1260                         self.aistatus &~= AI_STATUS_WAYPOINT_PERSONAL_REACHED;
1261                         return CMD_STATUS_FINISHED;
1262                 }
1263
1264                 return CMD_STATUS_EXECUTING;
1265         }
1266
1267         // Step 2: Linking waypoint
1268         if(self.aistatus & AI_STATUS_WAYPOINT_PERSONAL_LINKING)
1269         {
1270                 // Wait until it is linked
1271                 if(!self.havocbot_personal_waypoint.wplinked)
1272                 {
1273                         dprint(self.netname, " waiting for personal waypoint to be linked\n");
1274                         return CMD_STATUS_EXECUTING;
1275                 }
1276
1277                 self.havocbot_personal_waypoint_searchtime = time; // so we set the route next frame
1278                 self.aistatus &~= AI_STATUS_WAYPOINT_PERSONAL_LINKING;
1279                 self.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_GOING;
1280
1281                 // Step 3: Route to waypoint
1282                 dprint(self.netname, " walking to its personal waypoint\n");
1283
1284                 return CMD_STATUS_EXECUTING;
1285         }
1286
1287         // Step 1: Spawning waypoint
1288         wp = waypoint_spawnpersonal(pos);
1289         if(wp==world)
1290         {
1291                 dprint("Error: Can't spawn personal waypoint at ",vtos(pos),"\n");
1292                 return CMD_STATUS_ERROR;
1293         }
1294
1295         self.havocbot_personal_waypoint = wp;
1296         self.havocbot_personal_waypoint_failcounter = 0;
1297         self.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_LINKING;
1298
1299         // if pos is inside a teleport, then let's mark it as teleport waypoint
1300         entity head;
1301         for(head = world; (head = find(head, classname, "trigger_teleport")); )
1302         {
1303                 if(WarpZoneLib_BoxTouchesBrush(pos, pos, head, world))
1304                 {
1305                         wp.wpflags |= WAYPOINTFLAG_TELEPORT;
1306                         self.lastteleporttime = 0;
1307                 }
1308         }
1309
1310 /*
1311         if(wp.wpflags & WAYPOINTFLAG_TELEPORT)
1312                 print("routing to a teleporter\n");
1313         else
1314                 print("routing to a non-teleporter\n");
1315 */
1316
1317         return CMD_STATUS_EXECUTING;
1318 }
1319
1320 float havocbot_resetgoal()
1321 {
1322         navigation_clearroute();
1323         return CMD_STATUS_FINISHED;
1324 }
1325
1326 void havocbot_setupbot()
1327 {
1328         self.bot_ai = havocbot_ai;
1329         self.cmd_moveto = havocbot_moveto;
1330         self.cmd_resetgoal = havocbot_resetgoal;
1331
1332         havocbot_chooserole();
1333 }
1334
1335 vector havocbot_dodge()
1336 {
1337         // LordHavoc: disabled because this is too expensive
1338         return '0 0 0';
1339         local entity head;
1340         local vector dodge, v, n;
1341         local float danger, bestdanger, vl, d;
1342         dodge = '0 0 0';
1343         bestdanger = -20;
1344         // check for dangerous objects near bot or approaching bot
1345         head = findchainfloat(bot_dodge, TRUE);
1346         while(head)
1347         {
1348                 if (head.owner != self)
1349                 {
1350                         vl = vlen(head.velocity);
1351                         if (vl > sv_maxspeed * 0.3)
1352                         {
1353                                 n = normalize(head.velocity);
1354                                 v = self.origin - head.origin;
1355                                 d = v * n;
1356                                 if (d > (0 - head.bot_dodgerating))
1357                                 if (d < (vl * 0.2 + head.bot_dodgerating))
1358                                 {
1359                                         // calculate direction and distance from the flight path, by removing the forward axis
1360                                         v = v - (n * (v * n));
1361                                         danger = head.bot_dodgerating - vlen(v);
1362                                         if (bestdanger < danger)
1363                                         {
1364                                                 bestdanger = danger;
1365                                                 // dodge to the side of the object
1366                                                 dodge = normalize(v);
1367                                         }
1368                                 }
1369                         }
1370                         else
1371                         {
1372                                 danger = head.bot_dodgerating - vlen(head.origin - self.origin);
1373                                 if (bestdanger < danger)
1374                                 {
1375                                         bestdanger = danger;
1376                                         dodge = normalize(self.origin - head.origin);
1377                                 }
1378                         }
1379                 }
1380                 head = head.chain;
1381         }
1382         return dodge;
1383 };