]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/server/havocbot.qc
Bots: Escape from jump pads :D
[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 distance;
144         local vector deviation;
145
146         if(self.goalcurrent.classname == "player")
147                 return;
148
149         if(self.aistatus & AI_STATUS_DANGER_AHEAD)
150         {
151                 self.aistatus &~= AI_STATUS_RUNNING;
152                 self.BUTTON_JUMP = FALSE;
153                 return;
154         }
155
156         if(self.bot_lastseengoal != self.goalcurrent && !(self.aistatus & AI_STATUS_RUNNING))
157         {
158                 self.bot_canruntogoal = 0;
159                 self.bot_timelastseengoal = 0;
160         }
161
162         distance = vlen(self.origin - self.goalcurrent.origin);
163
164         // Run only to visible goals
165         traceline(self.origin + self.view_ofs , self.goalcurrent.origin, TRUE, world);
166         if(self.flags & FL_ONGROUND)
167         if(trace_fraction == 1)
168         {
169                         self.bot_lastseengoal = self.goalcurrent;
170
171                         // seen it before
172                         if(self.bot_timelastseengoal)
173                         {
174                                 // for a period of time
175                                 if(time - self.bot_timelastseengoal > cvar("bot_ai_bunnyhop_firstjumpdelay"))
176                                 {
177                                         local float checkdistance;
178                                         checkdistance = TRUE;
179
180                                         // don't run if it is too close
181                                         if(self.bot_canruntogoal==0)
182                                         {
183                                                 if(distance > cvar("bot_ai_bunnyhop_startdistance"))
184                                                         self.bot_canruntogoal = 1;
185                                                 else    
186                                                         self.bot_canruntogoal = -1;
187                                         }
188
189                                         if(self.bot_canruntogoal != 1)
190                                                 return;
191
192                                         if(self.aistatus & AI_STATUS_ROAMING)
193                                         if(self.goalcurrent.classname=="waypoint")
194                                         if(fabs(self.goalcurrent.origin_z - self.origin_z) < self.maxs_z - self.mins_z)
195                                         if(self.goalstack01!=world)
196                                         {
197                                                 deviation = self.goalstack01.origin - (self.origin + self.view_ofs);
198                                                 deviation = vectoangles(deviation) - self.v_angle;
199                                                 while (deviation_y < -180) deviation_y = deviation_y + 360;
200                                                 while (deviation_y > 180) deviation_y = deviation_y - 360;
201
202                                                 if(fabs(deviation_y) < 15)
203                                                 if(distance < vlen(self.origin - self.goalstack01.origin))
204                                                 if(fabs(self.goalstack01.origin_z - self.goalcurrent.origin_z) < self.maxs_z - self.mins_z)
205                                                 {
206                                                         traceline(self.origin + self.view_ofs , self.goalstack01.origin, TRUE, world);
207                                                         if(trace_fraction==1)
208                                                         {
209                                                                 checkdistance = FALSE;
210                                                         }
211                                                 }
212                                         }
213
214                                         if(checkdistance)
215                                         {
216                                                 self.aistatus &~= AI_STATUS_RUNNING;
217                                                 if(distance > cvar("bot_ai_bunnyhop_stopdistance"))
218                                                         self.BUTTON_JUMP = TRUE;
219                                         }
220                                         else
221                                         {
222                                                 self.aistatus |= AI_STATUS_RUNNING;
223                                                 self.BUTTON_JUMP = TRUE;
224                                         }
225                                 }
226                         }
227                         else
228                         {
229                                 self.bot_timelastseengoal = time;
230                         }
231         }
232         else
233         {
234                 self.bot_timelastseengoal = 0;
235         }
236
237         // Release jump button
238         if(self.flags & FL_ONGROUND == 0)
239         {
240                 if(self.velocity_z < 0)
241                         self.BUTTON_JUMP = FALSE;
242
243                 // Strafe 
244                 local float maxspeed;
245                 maxspeed = cvar("sv_maxspeed");
246
247                 if(self.aistatus & AI_STATUS_RUNNING)
248                 if(vlen(self.velocity)>maxspeed)
249                 {
250                         deviation = vectoangles(dir) - self.v_angle;
251                         while (deviation_y < -180) deviation_y = deviation_y + 360;
252                         while (deviation_y > 180) deviation_y = deviation_y - 360;
253
254                         if ( deviation_y > 2 )
255                                 self.movement_y = maxspeed;
256                         if ( deviation_y < -2 )
257                                 self.movement_y = maxspeed * -1;
258                 }
259         }
260 };
261
262 //.float havocbotignoretime;
263 //.vector bot_dodgevector;
264 //.float bot_dodgevector_time;
265 //.float bot_dodgevector_jumpbutton;
266 void havocbot_movetogoal()
267 {
268         local vector destorg;
269         local vector diff;
270         local vector dir;
271         local vector flatdir;
272         local vector m1;
273         local vector m2;
274         local vector evadeobstacle;
275         local vector evadelava;
276         local float s;
277         local float maxspeed;
278         //local float dist;
279         local vector dodge;
280         //if (self.goalentity)
281         //      te_lightning2(self, self.origin, (self.goalentity.absmin + self.goalentity.absmax) * 0.5);
282         self.movement = '0 0 0';
283         maxspeed = cvar("sv_maxspeed");
284
285         if(self.jumppadcount)
286         {
287                 if(self.flags & FL_ONGROUND)
288                 {
289                         self.jumppadcount = FALSE;
290                         if(self.aistatus & AI_STATUS_BAD_JUMPPAD)
291                                 self.aistatus &~= AI_STATUS_BAD_JUMPPAD;
292                 }
293
294                 // If got stuck on the jump pad try to reach the farther visible item
295                 if(self.aistatus & AI_STATUS_BAD_JUMPPAD)
296                 {
297                         if(fabs(self.velocity_z)<50)
298                         {
299                                 local entity head, newgoal;
300                                 local float distance, bestdistance;
301
302                                 for (head = findchainfloat(bot_pickup, TRUE); head; head = head.chain)
303                                 {
304                                         if(head.classname=="worldspawn")
305                                                 continue;
306
307                                         distance = vlen(head.origin - self.origin);
308                                         if(distance>1000)
309                                                 continue;
310
311                                         traceline(self.origin + self.view_ofs , head.origin, TRUE, world);
312
313                                         if(trace_fraction<1)
314                                                 continue;
315
316                                         if(distance>bestdistance)
317                                         {
318                                                 newgoal = head;
319                                                 bestdistance = distance;
320                                         }
321                                 }
322
323                                 if(newgoal)
324                                 {
325                                         self.ignoregoal = self.goalcurrent;
326                                         self.ignoregoaltime = time + cvar("bot_ai_ignoregoal_timeout");
327                                         navigation_routetogoal(newgoal);
328                                         self.aistatus &~= AI_STATUS_BAD_JUMPPAD;
329                                 }
330                         }
331                         else
332                                 return;
333                 }
334                 else
335                 {
336                         if(self.velocity_z>0)
337                         {
338                                 local float threshold;
339                                 threshold = maxspeed * 0.3;
340                                 if(fabs(self.velocity_x) < threshold  &&  fabs(self.velocity_y) < threshold)
341                                         self.aistatus |= AI_STATUS_BAD_JUMPPAD;
342                                 return;
343                         }
344                 }
345         }
346
347         if (self.goalcurrent == world)
348                 return;
349
350         navigation_poptouchedgoals();
351
352         if(self.goalcurrent == world)
353         if(self.flags & FL_ONGROUND || self.aistatus & AI_STATUS_RUNNING || self.BUTTON_JUMP == TRUE)
354         {
355                 if(self.alternativegoal==world)
356                 {
357                         // ran out of goals, rethink strategy as soon as possible
358                         self.bot_strategytime = 0;
359                         return;
360                 }
361                 // try to use the alternative goal
362                 navigation_findnearestwaypoint(self.alternativegoal, TRUE);
363                 navigation_routetogoal(self.alternativegoal);
364                 self.alternativegoal = world;
365                 return;
366         }
367
368 #ifdef DEBUG_BOT_GOALSTACK
369         debuggoalstack();
370 #endif
371
372         m1 = self.goalcurrent.origin + self.goalcurrent.mins;
373         m2 = self.goalcurrent.origin + self.goalcurrent.maxs;
374         destorg = self.origin;
375         destorg_x = bound(m1_x, destorg_x, m2_x);
376         destorg_y = bound(m1_y, destorg_y, m2_y);
377         destorg_z = bound(m1_z, destorg_z, m2_z);
378         diff = destorg - self.origin;
379         //dist = vlen(diff);
380         dir = normalize(diff);
381         flatdir = diff;flatdir_z = 0;
382         flatdir = normalize(flatdir);
383
384         //if (self.bot_dodgevector_time < time)
385         {
386         //      self.bot_dodgevector_time = time + cvar("bot_ai_dodgeupdateinterval");
387         //      self.bot_dodgevector_jumpbutton = 1;
388                 evadeobstacle = '0 0 0';
389                 evadelava = '0 0 0';
390                 if (self.waterlevel)
391                 {
392                         makevectors(self.v_angle);
393                         self.BUTTON_JUMP = TRUE;
394                         evadelava_z = 1;
395                 }
396                 else
397                 {
398                         // jump if going toward an obstacle that doesn't look like stairs we
399                         // can walk up directly
400                         tracebox(self.origin, self.mins, self.maxs, self.origin + self.velocity * 0.2, FALSE, self);
401                         if (trace_fraction < 1)
402                         if (trace_plane_normal_z < 0.7)
403                         {
404                                 s = trace_fraction;
405                                 tracebox(self.origin + '0 0 16', self.mins, self.maxs, self.origin + self.velocity * 0.2 + '0 0 16', FALSE, self);
406                                 if (trace_fraction < s + 0.01)
407                                 if (trace_plane_normal_z < 0.7)
408                                 {
409                                         s = trace_fraction;
410                                         tracebox(self.origin + '0 0 48', self.mins, self.maxs, self.origin + self.velocity * 0.2 + '0 0 48', FALSE, self);
411                                         if (trace_fraction > s)
412                                                 self.BUTTON_JUMP = 1;
413                                 }
414                         }
415
416                         // avoiding dangers and obstacles
417                         local vector dst_ahead, dst_down;
418                         dst_ahead = self.origin + self.view_ofs + (self.velocity * 0.32);
419                         dst_down = dst_ahead + '0 0 -1500';                             
420                         
421                         // Look ahead
422                         traceline(self.origin + self.view_ofs , dst_ahead, TRUE, world);
423
424                         // Check head-banging against walls
425                         if(vlen(self.origin + self.view_ofs - trace_endpos) < 2)
426                         {
427                                 if(self.facingwalltime && time > self.facingwalltime)
428                                 {
429                                         self.ignoregoal = self.goalcurrent;
430                                         self.ignoregoaltime = time + cvar("bot_ai_ignoregoal_timeout");
431                                         navigation_poproute();
432                                 }
433                                 else
434                                 {
435                                         self.facingwalltime = time + 0.05;
436                                 }
437                         }
438                         else
439                         {
440                                 self.facingwalltime = 0;
441
442                                 if(self.ignoregoal != world && time > self.ignoregoaltime)
443                                 {
444                                         self.ignoregoal = world;
445                                         self.ignoregoaltime = 0;
446                                 }
447                         }
448                 
449                         // Check for water/slime/lava and dangerous edges
450                         // (only when the bot is on the ground or jumping intentionally)
451                         self.aistatus &~= AI_STATUS_DANGER_AHEAD;
452
453                         if(trace_fraction == 1)
454                         if(self.flags & FL_ONGROUND || self.aistatus & AI_STATUS_RUNNING || self.BUTTON_JUMP == TRUE)
455                         {
456                                 // Look downwards
457                                 traceline(dst_ahead , dst_down, TRUE, world);
458                                 //      te_lightning2(world, self.origin, dst_ahead);   // Draw "ahead" look 
459                                 //      te_lightning2(world, dst_ahead, dst_down);              // Draw "downwards" look
460                                 if(trace_endpos_z < self.origin_z + self.mins_z)
461                                 {
462                                         s = pointcontents(trace_endpos + '0 0 1');
463                                         if (s != CONTENT_SOLID)
464                                         if (s == CONTENT_LAVA || s == CONTENT_SLIME)
465                                                 evadelava = normalize(self.velocity) * -1;
466                                         else if (s == CONTENT_SKY)
467                                                 evadeobstacle = normalize(self.velocity) * -1;
468                                         else if (!boxesoverlap(dst_ahead - self.view_ofs + self.mins, dst_ahead - self.view_ofs + self.maxs, 
469                                                                 self.goalcurrent.absmin, self.goalcurrent.absmax))
470                                         {
471                                                 // if ain't a safe goal with "holes" (like the jumpad on soylent)
472                                                 // and there is a trigger_hurt below
473                                                 if(tracebox_hits_trigger_hurt(dst_ahead, self.mins, self.maxs, trace_endpos))
474                                                 {
475                                                         // Remove dangerous dynamic goals from stack
476                                                         if (self.goalcurrent.classname == "player" || self.goalcurrent.classname == "droppedweapon")
477                                                                 navigation_poproute();
478                                                         // try to stop
479                                                         flatdir = '0 0 0';
480                                                         evadeobstacle = normalize(self.velocity) * -1;
481                                                 }
482                                         }
483                                 }
484                         }
485
486                         dir = flatdir;
487                         evadeobstacle_z = 0;
488                         evadelava_z = 0;
489                         makevectors(self.v_angle_y * '0 1 0');
490
491                         if(evadeobstacle!='0 0 0'||evadelava!='0 0 0')
492                                 self.aistatus |= AI_STATUS_DANGER_AHEAD;
493                 }
494
495                 dodge = havocbot_dodge();
496                 dodge = dodge * bound(0,3+skill*0.1,1);
497                 evadelava = evadelava * bound(1,3-skill,3); //Noobs fear lava a lot and take more distance from it
498                 traceline(self.origin, self.enemy.origin, TRUE, world);
499                 if(trace_ent.classname == "player")
500                         dir = dir * bound(0,skill/7,1);
501
502                 dir = normalize(dir + dodge + evadeobstacle + evadelava);
503         //      self.bot_dodgevector = dir;
504         //      self.bot_dodgevector_jumpbutton = self.BUTTON_JUMP;
505         }
506
507         //dir = self.bot_dodgevector;
508         //if (self.bot_dodgevector_jumpbutton)
509         //      self.BUTTON_JUMP = 1;
510         self.movement_x = dir * v_forward * maxspeed;
511         self.movement_y = dir * v_right * maxspeed;
512         self.movement_z = dir * v_up * maxspeed;
513
514         // Emulate keyboard interface
515         if (skill < 10)
516                 havocbot_keyboard_movement(destorg);
517
518         // Bunnyhop!
519 //      if(self.aistatus & AI_STATUS_ROAMING)
520         if(skill >= cvar("bot_ai_bunnyhop_skilloffset"))
521                 havocbot_bunnyhop(dir);
522
523         if ((dir * v_up) >= cvar("sv_jumpvelocity")*0.5 && (self.flags & FL_ONGROUND)) self.BUTTON_JUMP=1;
524         if (((dodge * v_up) > 0) && random()*frametime >= 0.2*bound(0,(10-skill)*0.1,1)) self.BUTTON_JUMP=TRUE;
525         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);
526 };
527
528 .float havocbot_chooseenemy_finished;
529 .float havocbot_stickenemy;
530 void havocbot_chooseenemy()
531 {
532         local entity head, best;
533         local float rating, bestrating;
534         local vector eye, v;
535         if (cvar("bot_nofire") || IS_INDEPENDENT_PLAYER(self))
536         {
537                 self.enemy = world;
538                 return;
539         }
540         if (self.enemy)
541         {
542                 if (!bot_shouldattack(self.enemy))
543                 {
544                         // enemy died or something, find a new target
545                         self.enemy = world;
546                         self.havocbot_chooseenemy_finished = time;
547                 }
548                 else if (self.havocbot_stickenemy)
549                 {
550                         // tracking last chosen enemy
551                         // if enemy is visible
552                         // and not really really far away
553                         // and we're not severely injured
554                         // then keep tracking for a half second into the future
555                         traceline(self.origin+self.view_ofs, self.enemy.origin+self.enemy.view_ofs*0.5,FALSE,world);
556                         if (trace_ent == self.enemy || trace_fraction == 1)
557                         if (vlen(self.enemy.origin - self.origin) < 1000)
558                         if (self.health > 30)
559                         {
560                                 // remain tracking him for a shot while (case he went after a small corner or pilar
561                                 self.havocbot_chooseenemy_finished = time + cvar("bot_ai_enemydetectioninterval");
562                                 return;
563                         }
564                         // enemy isn't visible, or is far away, or we're injured severely
565                         // so stop preferring this enemy
566                         // (it will still take a half second until a new one is chosen)
567                         self.havocbot_stickenemy = 0;
568                 }
569         }
570         if (time < self.havocbot_chooseenemy_finished)
571                 return;
572         self.havocbot_chooseenemy_finished = time + cvar("bot_ai_enemydetectioninterval");
573         eye = (self.origin + self.view_ofs);
574         best = world;
575         bestrating = 100000000;
576         head = findchainfloat(bot_attack, TRUE);
577         while (head)
578         {
579                 v = (head.absmin + head.absmax) * 0.5;
580                 rating = vlen(v - eye);
581                 if (bestrating > rating)
582                 if (bot_shouldattack(head))
583                 {
584                         traceline(eye, v, TRUE, self);
585                         if (trace_ent == head || trace_fraction >= 1)
586                         {
587                                 best = head;
588                                 bestrating = rating;
589                         }
590                 }
591                 head = head.chain;
592         }
593         self.enemy = best;
594         self.havocbot_stickenemy = TRUE;
595 };
596
597 .float bot_chooseweapontime;
598 float(entity e) w_getbestweapon;
599 void havocbot_chooseweapon()
600 {
601         // TODO: clean this up by moving it to weapon code
602         if(self.enemy.classname!="player")
603         {
604                 self.switchweapon = w_getbestweapon(self);
605                 return;
606         }
607
608         local float w, s;
609         local float rocket  ; rocket   =-1000;
610         local float nex     ; nex      =-1000;
611         local float hagar   ; hagar    =-1000;
612         local float grenade ; grenade  =-1000;
613         local float electro ; electro  =-1000;
614         local float crylink ; crylink  =-1000;
615         local float uzi     ; uzi      =-1000;
616         local float shotgun ; shotgun  =-1000;
617         local float campingrifle ; campingrifle  =-1000;
618         local float laser   ; laser    =-1000;
619         local float minstanex ; minstanex =-1000;
620         local float currentscore;
621         local float bestscore; bestscore = 0;
622         local float bestweapon; bestweapon=self.switchweapon;
623         local float distance; distance=bound(10,vlen(self.origin-self.enemy.origin)-200,10000);
624         local float maxdelaytime=0.5;
625         local float spreadpenalty=10;
626         local float distancefromfloor;
627
628         local float af, ct, combo_time;
629
630         traceline(self.enemy.origin,self.enemy.origin-'0 0 1000',TRUE,world);
631         distancefromfloor = self.enemy.origin_z - trace_endpos_z;
632         
633         af = ATTACK_FINISHED(self);
634         ct = cvar("bot_ai_weapon_combo_threshold");
635
636         // Bots with no skill will be 4 times more slower than "godlike" bots when doing weapon combos
637         // Ideally this 4 should be calculated as longest_weapon_refire / bot_ai_weapon_combo_threshold
638         combo_time = time + ct + (ct * ((-0.3*skill)+3));
639
640         // Custom weapon list based on distance to the enemy
641         local float i; i = 0;
642         if(bot_custom_weapon){
643
644                 // Choose weapons for far distance
645                 if ( distance > bot_distance_far ) {
646                         for(i=0; i < WEP_COUNT && bot_weapons_far[i] != -1 ; ++i){
647                                 w = bot_weapons_far[i];
648                                 if ( client_hasweapon(self, w, TRUE, FALSE) ){
649                                         if ( self.weapon == w){
650                                                 if( cvar("bot_ai_weapon_combo") && af > combo_time)
651                                                         continue;
652                                         } else {
653                                                 self.switchweapon = w;
654                                         }
655                                         return;
656                                 }
657                         }
658                 }
659
660                 // Choose weapons for mid distance
661                 if ( distance > bot_distance_close ) {
662                         for(i= 0; i < WEP_COUNT && bot_weapons_mid[i] != -1 ; ++i){
663                                 w = bot_weapons_mid[i];
664                                 if ( client_hasweapon(self, w, TRUE, FALSE) ){
665                                         if ( self.weapon == w){
666                                                 if( cvar("bot_ai_weapon_combo") && af > combo_time)
667                                                         continue;
668                                         } else {
669                                                 self.switchweapon = w;
670                                         }
671                                         return;
672                                 }
673                         }
674                 }
675
676                 // Choose weapons for close distance
677                 for(i= 0; i < WEP_COUNT && bot_weapons_close[i] != -1 ; ++i){
678                         w = bot_weapons_close[i];
679                         if ( client_hasweapon(self, w, TRUE, FALSE) ){
680                                 if ( self.weapon == w){
681                                         if( cvar("bot_ai_weapon_combo") && af > combo_time)
682                                                 continue;
683                                 } else {
684                                         self.switchweapon = w;
685                                 }
686                                 return;
687                         }
688                 }
689                 // If now weapon was chosen by this system fall back to the previous one
690         }
691
692         // Formula:
693         //      (Damage/Sec * Weapon spefic change to get that damage)
694         //      *(Time to get to target * weapon specfic hitchange bonus) / (in a time of maxdelaytime)
695         //      *(Spread change of hit) // if it applies
696         //      *(Penality for target beeing in air)
697         // %weaponaddpoint
698         if (client_hasweapon(self, WEP_MINSTANEX, TRUE, FALSE))
699                 minstanex = (1000/cvar("g_balance_minstanex_refire")*1.0)
700                         * (0.5);
701
702         if (client_hasweapon(self, WEP_ROCKET_LAUNCHER, TRUE, FALSE)  && 
703                 !(      cvar("bot_ai_weapon_combo") && self.weapon == WEP_ROCKET_LAUNCHER && 
704                         af > combo_time
705                 )
706         )
707                 rocket = (cvar("g_balance_rocketlauncher_damage")/cvar("g_balance_rocketlauncher_refire")*0.75)
708                         * bound(0,(cvar("g_balance_rocketlauncher_speed")/distance*maxdelaytime),1)*1.5;
709                         
710         if (client_hasweapon(self, WEP_NEX, TRUE, FALSE)  && 
711                 !(      cvar("bot_ai_weapon_combo") && self.weapon == WEP_NEX && 
712                         af > combo_time
713                 )
714         )
715                 nex = (cvar("g_balance_nex_damage")/cvar("g_balance_nex_refire")*1.0)
716                         * (0.5);
717                         
718         if (client_hasweapon(self, WEP_HAGAR, TRUE, FALSE) ) // && 
719         //      !( cvar("bot_ai_weapon_combo") && self.weapon == WEP_HAGAR &&  time < self.bot_lastshot + cvar("g_balance_hagar_primary_refire") ))
720                 hagar = (cvar("g_balance_hagar_primary_damage")/cvar("g_balance_hagar_primary_refire")*1.0)
721                         * bound(0,(cvar("g_balance_hagar_primary_speed")/distance*maxdelaytime),1)*0.2;
722                         
723         if (client_hasweapon(self, WEP_GRENADE_LAUNCHER, TRUE, FALSE) && 
724                 !( 
725                         cvar("bot_ai_weapon_combo") && self.weapon == WEP_GRENADE_LAUNCHER && 
726                         af > combo_time
727                 )
728         )
729                 grenade = (cvar("g_balance_grenadelauncher_primary_damage")/cvar("g_balance_grenadelauncher_primary_refire")*1.0)
730                         * bound(0,(cvar("g_balance_grenadelauncher_primary_speed")/distance*maxdelaytime),1)*1.1;
731                         
732         if (client_hasweapon(self, WEP_ELECTRO, TRUE, FALSE) && 
733                 !(      cvar("bot_ai_weapon_combo") && self.weapon == WEP_ELECTRO && 
734                         af > combo_time
735                 )
736         )
737                 electro = (cvar("g_balance_electro_primary_damage")/cvar("g_balance_electro_primary_refire")*0.75)
738                         * bound(0,(cvar("g_balance_electro_primary_speed")/distance*maxdelaytime),1)*1.0;
739                         
740         if (client_hasweapon(self, WEP_CRYLINK, TRUE, FALSE) ) // && 
741         //      !( self.weapon == WEP_CRYLINK &&  time < self.bot_lastshot + cvar("g_balance_crylink_primary_refire") ))
742                 crylink = (cvar("g_balance_crylink_primary_damage")/cvar("g_balance_crylink_primary_refire")*1.0)
743                         * bound(0,(cvar("g_balance_crylink_primary_speed")/distance*maxdelaytime),1)*(64/(32+cvar("g_balance_crylink_primary_spread")*distance))*1.0;
744                         
745         if (client_hasweapon(self, WEP_UZI, TRUE, FALSE) ) // && 
746         //      !( self.weapon == WEP_UZI &&  time < self.bot_lastshot + cvar("g_balance_uzi_sustained_refire") ))
747                 uzi = (cvar("g_balance_uzi_sustained_damage")/cvar("g_balance_uzi_sustained_refire")*1.0)
748                         * bound(0,32/(32+cvar("g_balance_uzi_sustained_spread")*distance),1);
749                         
750         if (client_hasweapon(self, WEP_SHOTGUN, TRUE, FALSE) && 
751                 !(      cvar("bot_ai_weapon_combo") && self.weapon == WEP_SHOTGUN && 
752                         af > combo_time
753                 )
754         )
755                 shotgun = (cvar("g_balance_shotgun_primary_damage")*cvar("g_balance_shotgun_primary_bullets")/cvar("g_balance_shotgun_primary_refire")*1.0)
756                         * bound(0,32/(32+cvar("g_balance_shotgun_primary_spread")*distance),1);
757                         
758         if (client_hasweapon(self, WEP_LASER, FALSE, FALSE) && 
759                 !(      cvar("bot_ai_weapon_combo") && self.weapon == WEP_LASER && 
760                         af > combo_time
761                 )
762         )
763                 laser = (cvar("g_balance_laser_primary_damage")/cvar("g_balance_laser_primary_refire")*1.0)
764                         * bound(0,cvar("g_balance_laser_primary_speed")/distance*maxdelaytime,1);
765                         
766         if((self.enemy.flags & FL_ONGROUND)==FALSE){
767                 rocket = rocket   * (1.5-bound(0, distancefromfloor/cvar("g_balance_rocketlauncher_radius"         ),0.9)); //slight bigger change
768                 grenade = grenade * (1.5-bound(0,distancefromfloor/cvar("g_balance_grenadelauncher_primary_radius"),0.95));
769                 electro = electro * (1.5-bound(0,distancefromfloor/cvar("g_balance_electro_primary_radius"        ),0.95));
770                 laser = laser     * (1.5-bound(0,distancefromfloor/cvar("g_balance_laser_primary_radius"                  ),0.95));
771         }
772         /*
773         dprint("Floor distance: ",ftos(distancefromfloor),"\n");
774         dprint("Rocket: " , ftos(rocket  ), "\n");
775         dprint("Nex: "    , ftos(nex     ), "\n");
776         dprint("Hagar: "  , ftos(hagar   ), "\n");
777         dprint("Grenade: ", ftos(grenade ), "\n");
778         dprint("Electro: ", ftos(electro ), "\n");
779         dprint("Crylink: ", ftos(crylink ), "\n");
780         dprint("Uzi: "    , ftos(uzi     ), "\n");
781         dprint("Shotgun :", ftos(shotgun ), "\n");
782         dprint("Laser   :", ftos(laser   ), "\n\n");
783         */
784         currentscore = -1;
785         w = WEP_MINSTANEX        ;s = minstanex;if (s > bestscore){bestscore = s;bestweapon = w;} if (self.switchweapon == w) currentscore = s;
786         w = WEP_ROCKET_LAUNCHER  ;s = rocket   ;if (s > bestscore){bestscore = s;bestweapon = w;} if (self.switchweapon == w) currentscore = s;
787         w = WEP_NEX              ;s = nex      ;if (s > bestscore){bestscore = s;bestweapon = w;} if (self.switchweapon == w) currentscore = s;
788         w = WEP_HAGAR            ;s = hagar    ;if (s > bestscore){bestscore = s;bestweapon = w;} if (self.switchweapon == w) currentscore = s;
789         w = WEP_GRENADE_LAUNCHER ;s = grenade  ;if (s > bestscore){bestscore = s;bestweapon = w;} if (self.switchweapon == w) currentscore = s;
790         w = WEP_ELECTRO          ;s = electro  ;if (s > bestscore){bestscore = s;bestweapon = w;} if (self.switchweapon == w) currentscore = s;
791         w = WEP_CRYLINK          ;s = crylink  ;if (s > bestscore){bestscore = s;bestweapon = w;} if (self.switchweapon == w) currentscore = s;
792         w = WEP_UZI              ;s = uzi      ;if (s > bestscore){bestscore = s;bestweapon = w;} if (self.switchweapon == w) currentscore = s;
793         w = WEP_SHOTGUN          ;s = shotgun  ;if (s > bestscore){bestscore = s;bestweapon = w;} if (self.switchweapon == w) currentscore = s;
794         w = WEP_LASER            ;s = laser    ;if (s > bestscore){bestscore = s;bestweapon = w;} if (self.switchweapon == w) currentscore = s;
795
796         // switch if the best weapon would provide a significant damage increase
797         if (bestscore > currentscore*1.5){
798                 self.switchweapon = bestweapon;
799                 
800                 // buys time for detonating the rocket. not tested yet
801                 if ( cvar("bot_ai_weapon_combo") && bestweapon == WEP_ROCKET_LAUNCHER )
802                         self.bot_chooseweapontime += (distance  / cvar("g_balance_rocketlauncher_speed"));
803         }
804 };
805
806 .float nextaim;
807 void havocbot_aim()
808 {
809         local vector selfvel, enemyvel;
810         if(self.flags & FL_INWATER)
811                 return;
812         if (time < self.nextaim)
813                 return;
814         self.nextaim = time + 0.1;
815         selfvel = self.velocity;
816         if (!self.waterlevel)
817                 selfvel_z = 0;
818         if (self.enemy)
819         {
820                 enemyvel = self.enemy.velocity;
821                 if (!self.enemy.waterlevel)
822                         enemyvel_z = 0;
823                 lag_additem(time + self.ping, 0, 0, self.enemy, self.origin, selfvel, self.enemy.origin, enemyvel);
824         }
825         else
826                 lag_additem(time + self.ping, 0, 0, world, self.origin, selfvel, self.goalcurrent.origin, '0 0 0');
827 };
828
829 void havocbot_ai()
830 {
831         if (bot_strategytoken == self)
832         if (!bot_strategytoken_taken)
833         {
834                 self.havocbot_role();
835                 // token has been used this frame
836                 bot_strategytoken_taken = TRUE;
837         }
838         if(self.deadflag)
839                 return;
840
841         havocbot_chooseenemy();
842         if (self.bot_chooseweapontime < time )
843         {
844                 self.bot_chooseweapontime = time + cvar("bot_ai_chooseweaponinterval");
845                 havocbot_chooseweapon();
846         }
847         havocbot_aim();
848         lag_update();
849         if (self.bot_aimtarg)
850         {
851                 self.aistatus |= AI_STATUS_ATTACKING;
852                 self.aistatus &~= AI_STATUS_ROAMING;
853
854                 weapon_action(self.weapon, WR_AIM);
855                 if (cvar("bot_nofire") || IS_INDEPENDENT_PLAYER(self))
856                 {
857                         self.BUTTON_ATCK = FALSE;
858                         self.BUTTON_ATCK2 = FALSE;
859                 }
860         }
861         else if (self.goalcurrent)
862         {
863                 self.aistatus |= AI_STATUS_ROAMING;
864                 self.aistatus &~= AI_STATUS_ATTACKING;
865
866                 local vector now,v,next;//,heading;
867                 local float distance,skillblend,distanceblend,blend;
868                 next = now = self.goalcurrent.origin - (self.origin + self.view_ofs);
869                 distance = vlen(now);
870                 //heading = self.velocity;
871                 //dprint(self.goalstack01.classname,etos(self.goalstack01),"\n");
872                 if(self.goalstack01 != self && self.goalstack01 != world && self.aistatus & AI_STATUS_RUNNING == 0)
873                         next = self.goalstack01.origin - (self.origin + self.view_ofs);
874                 skillblend=bound(0,(skill-2.5)*0.5,1); //lower skill player can't preturn
875                 distanceblend=bound(0,distance/cvar("bot_ai_keyboard_distance"),1);
876                 blend = skillblend * (1-distanceblend);
877                 //v = (now * (distanceblend) + next * (1-distanceblend)) * (skillblend) + now * (1-skillblend);
878                 //v = now * (distanceblend) * (skillblend) + next * (1-distanceblend) * (skillblend) + now * (1-skillblend);
879                 //v = now * ((1-skillblend) + (distanceblend) * (skillblend)) + next * (1-distanceblend) * (skillblend);
880                 v = now + blend * (next - now);
881                 //dprint(etos(self), " ");
882                 //dprint(vtos(now), ":", vtos(next), "=", vtos(v), " (blend ", ftos(blend), ")\n");
883                 //v = now * (distanceblend) + next * (1-distanceblend);
884                 if (self.waterlevel < 2)
885                         v_z = 0;
886                 //dprint("walk at:", vtos(v), "\n");
887                 //te_lightning2(world, self.origin, self.goalcurrent.origin);
888                 bot_aimdir(v, -1);
889         }
890         havocbot_movetogoal();
891 };
892
893 void havocbot_setupbot()
894 {
895         self.bot_ai = havocbot_ai;
896         // will be updated by think code
897         //Generate some random skill levels
898         self.havocbot_keyboardskill=random()-0.5;
899         havocbot_chooserole();
900 }
901
902
903 #ifdef DEBUG_BOT_GOALSTACK
904
905 .float goalcounter;
906 .vector lastposition;
907
908 // Debug the goal stack visually
909 void debuggoalstack()
910 {
911         local entity target;
912         local vector org;
913
914         if(self.goalcounter==0)target=self.goalcurrent;
915         else if(self.goalcounter==1)target=self.goalstack01;
916         else if(self.goalcounter==2)target=self.goalstack02;
917         else if(self.goalcounter==3)target=self.goalstack03;
918         else if(self.goalcounter==4)target=self.goalstack04;
919         else if(self.goalcounter==5)target=self.goalstack05;
920         else if(self.goalcounter==6)target=self.goalstack06;
921         else if(self.goalcounter==7)target=self.goalstack07;
922         else if(self.goalcounter==8)target=self.goalstack08;
923         else if(self.goalcounter==9)target=self.goalstack09;
924         else if(self.goalcounter==10)target=self.goalstack10;
925         else if(self.goalcounter==11)target=self.goalstack11;
926         else if(self.goalcounter==12)target=self.goalstack12;
927         else if(self.goalcounter==13)target=self.goalstack13;
928         else if(self.goalcounter==14)target=self.goalstack14;
929         else if(self.goalcounter==15)target=self.goalstack15;
930         else if(self.goalcounter==16)target=self.goalstack16;
931         else if(self.goalcounter==17)target=self.goalstack17;
932         else if(self.goalcounter==18)target=self.goalstack18;
933         else if(self.goalcounter==19)target=self.goalstack19;
934         else if(self.goalcounter==20)target=self.goalstack20;
935         else if(self.goalcounter==21)target=self.goalstack21;
936         else if(self.goalcounter==22)target=self.goalstack22;
937         else if(self.goalcounter==23)target=self.goalstack23;
938         else if(self.goalcounter==24)target=self.goalstack24;
939         else if(self.goalcounter==25)target=self.goalstack25;
940         else if(self.goalcounter==26)target=self.goalstack26;
941         else if(self.goalcounter==27)target=self.goalstack27;
942         else if(self.goalcounter==28)target=self.goalstack28;
943         else if(self.goalcounter==29)target=self.goalstack29;
944         else if(self.goalcounter==30)target=self.goalstack30;
945         else if(self.goalcounter==31)target=self.goalstack31;
946
947         if(target==world)
948         {
949                 self.goalcounter = 0;
950                 self.lastposition='0 0 0';
951                 return;
952         }
953
954         if(self.lastposition=='0 0 0')
955                 org = self.origin;
956         else
957                 org = self.lastposition;
958
959
960         te_lightning2(world, org, target.origin);
961         self.lastposition = target.origin;
962
963         self.goalcounter++;
964 }
965
966 #endif