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