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