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