change all function declarations (except builtins and data types) to ANSI C-like...
[divverent/nexuiz.git] / data / qcsrc / server / havocbot.qc
1
2 .void() havocbot_role;
3 void() havocbot_chooserole;
4 .float havocbot_keyboardskill;
5
6 vector havocbot_dodge()
7 {
8         // LordHavoc: disabled because this is too expensive
9         return '0 0 0';
10         /*
11         local entity head;
12         local vector dodge, v, n;
13         local float danger, bestdanger, vl, d;
14         dodge = '0 0 0';
15         bestdanger = -20;
16         // check for dangerous objects near bot or approaching bot
17         head = findchainfloat(bot_dodge, TRUE);
18         while(head)
19         {
20                 if (head.owner != self)
21                 {
22                         vl = vlen(head.velocity);
23                         if (vl > sv_maxspeed * 0.3)
24                         {
25                                 n = normalize(head.velocity);
26                                 v = self.origin - head.origin;
27                                 d = v * n;
28                                 if (d > (0 - head.bot_dodgerating))
29                                 if (d < (vl * 0.2 + head.bot_dodgerating))
30                                 {
31                                         // calculate direction and distance from the flight path, by removing the forward axis
32                                         v = v - (n * (v * n));
33                                         danger = head.bot_dodgerating - vlen(v);
34                                         if (bestdanger < danger)
35                                         {
36                                                 bestdanger = danger;
37                                                 // dodge to the side of the object
38                                                 dodge = normalize(v);
39                                         }
40                                 }
41                         }
42                         else
43                         {
44                                 danger = head.bot_dodgerating - vlen(head.origin - self.origin);
45                                 if (bestdanger < danger)
46                                 {
47                                         bestdanger = danger;
48                                         dodge = normalize(self.origin - head.origin);
49                                 }
50                         }
51                 }
52                 head = head.chain;
53         }
54         return dodge;
55         */
56 };
57
58 //.float havocbotignoretime;
59 .float havocbot_keyboardtime;
60 .float havocbot_ducktime;
61 .vector havocbot_keyboard;
62 //.vector bot_dodgevector;
63 //.float bot_dodgevector_time;
64 //.float bot_dodgevector_jumpbutton;
65 void havocbot_movetogoal()
66 {
67         local vector destorg;
68         local vector diff;
69         local vector dir;
70         local vector flatdir;
71         local vector m1;
72         local vector m2;
73         local vector evadeobstacle;
74         local vector evadelava;
75         local float s;
76         local float maxspeed;
77         //local float dist;
78         local vector dodge;
79         //if (self.goalentity)
80         //      te_lightning2(self, self.origin, (self.goalentity.absmin + self.goalentity.absmax) * 0.5);
81         self.movement = '0 0 0';
82         maxspeed = cvar("sv_maxspeed");
83
84         if(self.jumppadcount)
85         {
86                 if(self.flags & FL_ONGROUND)
87                         self.jumppadcount = FALSE;
88                 else
89                         return;
90         }
91
92         if (self.goalcurrent == world)
93                 return;
94         navigation_poptouchedgoals();
95         if (self.goalcurrent == world)
96         {
97                 // ran out of goals, rethink strategy as soon as possible
98                 self.bot_strategytime = 0;
99                 return;
100         }
101         m1 = self.goalcurrent.origin + self.goalcurrent.mins;
102         m2 = self.goalcurrent.origin + self.goalcurrent.maxs;
103         destorg = self.origin;
104         destorg_x = bound(m1_x, destorg_x, m2_x);
105         destorg_y = bound(m1_y, destorg_y, m2_y);
106         destorg_z = bound(m1_z, destorg_z, m2_z);
107         diff = destorg - self.origin;
108         //dist = vlen(diff);
109         dir = normalize(diff);
110         flatdir = diff;flatdir_z = 0;
111         flatdir = normalize(flatdir);
112         //if (self.bot_dodgevector_time < time)
113         {
114         //      self.bot_dodgevector_time = time + cvar("bot_ai_dodgeupdateinterval");
115         //      self.bot_dodgevector_jumpbutton = 1;
116                 evadeobstacle = '0 0 0';
117                 evadelava = '0 0 0';
118                 if (!self.waterlevel)
119                 {
120                         // Since new update in air contol, we can move in air
121                         //if (!(self.flags & FL_ONGROUND))
122                         //{
123                         //      // prevent goal checks when we can't walk
124                         //      if (self.bot_strategytime < time + 0.1)
125                         //              self.bot_strategytime = time + 0.1;
126                         //      return;
127                         //}
128
129                         // jump if going toward an obstacle that doesn't look like stairs we
130                         // can walk up directly
131                         tracebox(self.origin, self.mins, self.maxs, self.origin + self.velocity * 0.2, FALSE, self);
132                         if (trace_fraction < 1)
133                         if (trace_plane_normal_z < 0.7)
134                         {
135                                 s = trace_fraction;
136                                 tracebox(self.origin + '0 0 16', self.mins, self.maxs, self.origin + self.velocity * 0.2 + '0 0 16', FALSE, self);
137                                 if (trace_fraction < s + 0.01)
138                                 if (trace_plane_normal_z < 0.7)
139                                 {
140                                         s = trace_fraction;
141                                         tracebox(self.origin + '0 0 48', self.mins, self.maxs, self.origin + self.velocity * 0.2 + '0 0 48', FALSE, self);
142                                         if (trace_fraction > s)
143                                                 self.BUTTON_JUMP = 1;
144                                 }
145                         }
146
147                         traceline(self.origin + self.velocity * 0.3, self.origin + self.velocity * 0.3 + '0 0 -1000', TRUE, world);
148                         s = pointcontents(trace_endpos + '0 0 1');
149                         if (s == CONTENT_LAVA || s == CONTENT_SLIME)
150                                 evadelava = normalize(self.velocity) * -1;
151
152                         dir = flatdir;
153                         evadeobstacle_z = 0;
154                         evadelava_z = 0;
155                         makevectors(self.v_angle_y * '0 1 0');
156                 }
157                 else
158                         makevectors(self.v_angle);
159                 dodge = havocbot_dodge();
160                 dodge = dodge * bound(0,3+skill*0.1,1);
161                 evadelava = evadelava * bound(1,3-skill,3); //Noobs fear lava a lot and take more distance from it
162                 traceline(self.origin, self.enemy.origin, TRUE, world);
163                 if(trace_ent.classname == "player")
164                         dir = dir * bound(0,skill/7,1);
165
166                 dir = normalize(dir + dodge + evadeobstacle + evadelava);
167         //      self.bot_dodgevector = dir;
168         //      self.bot_dodgevector_jumpbutton = self.BUTTON_JUMP;
169         }
170
171         //dir = self.bot_dodgevector;
172         //if (self.bot_dodgevector_jumpbutton)
173         //      self.BUTTON_JUMP = 1;
174
175         self.movement_x = dir * v_forward * maxspeed;
176         self.movement_y = dir * v_right * maxspeed;
177         self.movement_z = dir * v_up * maxspeed;
178
179         if (skill < 10)
180         {
181                 // Emulate keyboard interface;
182                 local vector keyboard;
183                 local float blend;
184                 if (time >= self.havocbot_keyboardtime)
185                 {
186                         self.havocbot_keyboardtime =
187                                 max(
188                                         self.havocbot_keyboardtime
189                                                 + bound(0,0.05/(skill+self.havocbot_keyboardskill),0.05)
190                                                 +random()*bound(0,0.025/(skill+self.havocbot_keyboardskill),100)
191                                 , time);
192                         keyboard = self.movement * (1.0 / maxspeed);
193
194                         local float trigger, trigger1;
195                         blend = bound(0,skill*0.1,1);
196                         trigger = cvar("bot_ai_keyboard_treshold");
197                         trigger1 = 0 - trigger;
198
199                         // categorize forward movement
200                         // at skill < 1.5 only forward
201                         // at skill < 2.5 only individual directions
202                         // at skill < 4.5 only individual directions, and forward diagonals
203                         // at skill >= 4.5, all cases allowed
204                         if (keyboard_x > trigger)
205                         {
206                                 keyboard_x = 1;
207                                 if (skill < 2.5)
208                                         keyboard_y = 0;
209                         }
210                         else if (keyboard_x < trigger1 && skill > 1.5)
211                         {
212                                 keyboard_x = -1;
213                                 if (skill < 4.5)
214                                         keyboard_y = 0;
215                         }
216                         else
217                         {
218                                 keyboard_x = 0;
219                                 if (skill < 1.5)
220                                         keyboard_y = 0;
221                         }
222                         if (skill < 4.5)
223                                 keyboard_z = 0;
224
225                         if (keyboard_y > trigger)
226                                 keyboard_y = 1;
227                         else if (keyboard_y < trigger1)
228                                 keyboard_y = -1;
229                         else
230                                 keyboard_y = 0;
231
232                         if (keyboard_z > trigger)
233                                 keyboard_z = 1;
234                         else if (keyboard_z < trigger1)
235                                 keyboard_z = -1;
236                         else
237                                 keyboard_z = 0;
238
239                         self.havocbot_keyboard = keyboard * maxspeed;
240                         if (self.havocbot_ducktime>time) self.BUTTON_CROUCH=TRUE;
241                 }
242                 keyboard = self.havocbot_keyboard;
243                 blend = bound(0,vlen(destorg-self.origin)/cvar("bot_ai_keyboard_distance"),1); // When getting close move with 360 degree
244                 //dprint("movement ", vtos(self.movement), " keyboard ", vtos(keyboard), " blend ", ftos(blend), "\n");
245                 self.movement = self.movement + (keyboard - self.movement) * blend;
246         }
247
248         if ((dir * v_up) >= cvar("sv_jumpvelocity")*0.5 && (self.flags & FL_ONGROUND)) self.BUTTON_JUMP=1;
249         if (((dodge * v_up) > 0) && random()*frametime >= 0.2*bound(0,(10-skill)*0.1,1)) self.BUTTON_JUMP=TRUE;
250         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);
251
252 };
253
254 .float havocbot_chooseenemy_finished;
255 .float havocbot_stickenemy;
256 void havocbot_chooseenemy()
257 {
258         local entity head, best;
259         local float rating, bestrating;
260         local vector eye, v;
261         if (cvar("bot_nofire"))
262         {
263                 self.enemy = world;
264                 return;
265         }
266         if (self.enemy)
267         {
268                 if (!bot_shouldattack(self.enemy))
269                 {
270                         // enemy died or something, find a new target
271                         self.enemy = world;
272                         self.havocbot_chooseenemy_finished = time;
273                 }
274                 else if (self.havocbot_stickenemy)
275                 {
276                         // tracking last chosen enemy
277                         // if enemy is visible
278                         // and not really really far away
279                         // and we're not severely injured
280                         // then keep tracking for a half second into the future
281                         traceline(self.origin+self.view_ofs, self.enemy.origin+self.enemy.view_ofs*0.5,FALSE,world);
282                         if (trace_ent == self.enemy || trace_fraction == 1)
283                         if (vlen(self.enemy.origin - self.origin) < 1000)
284                         if (self.health > 30)
285                         {
286                                 // remain tracking him for a shot while (case he went after a small corner or pilar
287                                 self.havocbot_chooseenemy_finished = time + cvar("bot_ai_enemydetectioninterval");
288                                 return;
289                         }
290                         // enemy isn't visible, or is far away, or we're injured severely
291                         // so stop preferring this enemy
292                         // (it will still take a half second until a new one is chosen)
293                         self.havocbot_stickenemy = 0;
294                 }
295         }
296         if (time < self.havocbot_chooseenemy_finished)
297                 return;
298         self.havocbot_chooseenemy_finished = time + cvar("bot_ai_enemydetectioninterval");
299         eye = (self.origin + self.view_ofs);
300         best = world;
301         bestrating = 100000000;
302         head = findchainfloat(bot_attack, TRUE);
303         while (head)
304         {
305                 v = (head.absmin + head.absmax) * 0.5;
306                 rating = vlen(v - eye);
307                 if (bestrating > rating)
308                 if (bot_shouldattack(head))
309                 {
310                         traceline(eye, v, TRUE, self);
311                         if (trace_ent == head || trace_fraction >= 1)
312                         {
313                                 best = head;
314                                 bestrating = rating;
315                         }
316                 }
317                 head = head.chain;
318         }
319         self.enemy = best;
320         self.havocbot_stickenemy = 1;
321 };
322
323 float(entity e) w_getbestweapon;
324 void havocbot_chooseweapon()
325 {
326         // TODO: clean this up by moving it to weapon code
327         if(self.enemy.classname!="player")
328         {
329                 self.switchweapon = w_getbestweapon(self);
330                 return;
331         }
332
333         local float w, s;
334         local float rocket  ; rocket   =-1000;
335         local float nex     ; nex      =-1000;
336         local float hagar   ; hagar    =-1000;
337         local float grenade ; grenade  =-1000;
338         local float electro ; electro  =-1000;
339         local float crylink ; crylink  =-1000;
340         local float uzi     ; uzi      =-1000;
341         local float shotgun ; shotgun  =-1000;
342         local float laser   ; laser    =-1000;
343         local float currentscore;
344         local float bestscore; bestscore = 0;
345         local float bestweapon; bestweapon=self.switchweapon;
346         local float distance; distance=bound(10,vlen(self.origin-self.enemy.origin)-200,10000);
347         local float maxdelaytime=0.5;
348         local float spreadpenalty=10;
349         local float distancefromfloor;
350         traceline(self.enemy.origin,self.enemy.origin-'0 0 1000',TRUE,world);
351         distancefromfloor = self.enemy.origin_z - trace_endpos_z;
352         // Formula:
353         //      (Damage/Sec * Weapon spefic change to get that damage)
354         //      *(Time to get to target * weapon specfic hitchange bonus) / (in a time of maxdelaytime)
355         //      *(Spread change of hit) // if it applies
356         //      *(Penality for target beeing in air)
357         if (client_hasweapon(self, WEP_ROCKET_LAUNCHER, TRUE, FALSE))
358                 rocket = (cvar("g_balance_rocketlauncher_damage")/cvar("g_balance_rocketlauncher_refire")*0.75)
359                         * bound(0,(cvar("g_balance_rocketlauncher_speed")/distance*maxdelaytime),1)*1.5;
360         if (client_hasweapon(self, WEP_NEX, TRUE, FALSE))
361                 nex = (cvar("g_balance_nex_damage")/cvar("g_balance_nex_refire")*1.0)
362                         * (0.5);
363         if (client_hasweapon(self, WEP_HAGAR, TRUE, FALSE))
364                 hagar = (cvar("g_balance_hagar_primary_damage")/cvar("g_balance_hagar_primary_refire")*1.0)
365                         * bound(0,(cvar("g_balance_hagar_primary_speed")/distance*maxdelaytime),1)*0.2;
366         if (client_hasweapon(self, WEP_GRENADE_LAUNCHER, TRUE, FALSE))
367                 grenade = (cvar("g_balance_grenadelauncher_primary_damage")/cvar("g_balance_grenadelauncher_primary_refire")*1.0)
368                         * bound(0,(cvar("g_balance_grenadelauncher_primary_speed")/distance*maxdelaytime),1)*1.1;
369         if (client_hasweapon(self, WEP_ELECTRO, TRUE, FALSE))
370                 electro = (cvar("g_balance_electro_primary_damage")/cvar("g_balance_electro_primary_refire")*0.75)
371                         * bound(0,(cvar("g_balance_electro_primary_speed")/distance*maxdelaytime),1)*1.0;
372         if (client_hasweapon(self, WEP_CRYLINK, TRUE, FALSE))
373                 crylink = (cvar("g_balance_crylink_primary_damage")/cvar("g_balance_crylink_primary_refire")*1.0)
374                         * bound(0,(cvar("g_balance_crylink_primary_speed")/distance*maxdelaytime),1)*(64/(32+cvar("g_balance_crylink_primary_spread")*distance))*1.0;
375         if (client_hasweapon(self, WEP_UZI, TRUE, FALSE))
376                 uzi = (cvar("g_balance_uzi_sustained_damage")/cvar("g_balance_uzi_sustained_refire")*1.0)
377                         * bound(0,32/(32+cvar("g_balance_uzi_sustained_spread")*distance),1);
378         if (client_hasweapon(self, WEP_SHOTGUN, TRUE, FALSE))
379                 shotgun = (cvar("g_balance_shotgun_primary_damage")*cvar("g_balance_shotgun_primary_bullets")/cvar("g_balance_shotgun_primary_refire")*1.0)
380                         * bound(0,32/(32+cvar("g_balance_shotgun_primary_spread")*distance),1);
381         if (client_hasweapon(self, WEP_LASER, FALSE, FALSE))
382                 laser = (cvar("g_balance_laser_primary_damage")/cvar("g_balance_laser_primary_refire")*1.0)
383                         * bound(0,cvar("g_balance_laser_primary_speed")/distance*maxdelaytime,1);
384         if((self.enemy.flags & FL_ONGROUND)==FALSE){
385                 rocket = rocket   * (1.5-bound(0, distancefromfloor/cvar("g_balance_rocketlauncher_radius"         ),0.9)); //slight bigger change
386                 grenade = grenade * (1.5-bound(0,distancefromfloor/cvar("g_balance_grenadelauncher_primary_radius"),0.95));
387                 electro = electro * (1.5-bound(0,distancefromfloor/cvar("g_balance_electro_primary_radius"        ),0.95));
388                 laser = laser     * (1.5-bound(0,distancefromfloor/cvar("g_balance_laser_primary_radius"                  ),0.95));
389         }
390         /*
391         dprint("Floor distance: ",ftos(distancefromfloor),"\n");
392         dprint("Rocket: " , ftos(rocket  ), "\n");
393         dprint("Nex: "    , ftos(nex     ), "\n");
394         dprint("Hagar: "  , ftos(hagar   ), "\n");
395         dprint("Grenade: ", ftos(grenade ), "\n");
396         dprint("Electro: ", ftos(electro ), "\n");
397         dprint("Crylink: ", ftos(crylink ), "\n");
398         dprint("Uzi: "    , ftos(uzi     ), "\n");
399         dprint("Shotgun :", ftos(shotgun ), "\n");
400         dprint("Laser   :", ftos(laser   ), "\n\n");
401         */
402         currentscore = -1;
403         w = WEP_ROCKET_LAUNCHER  ;s = rocket ;if (s > bestscore){bestscore = s;bestweapon = w;} if (self.switchweapon == w) currentscore = s;
404         w = WEP_NEX              ;s = nex    ;if (s > bestscore){bestscore = s;bestweapon = w;} if (self.switchweapon == w) currentscore = s;
405         w = WEP_HAGAR            ;s = hagar  ;if (s > bestscore){bestscore = s;bestweapon = w;} if (self.switchweapon == w) currentscore = s;
406         w = WEP_GRENADE_LAUNCHER ;s = grenade;if (s > bestscore){bestscore = s;bestweapon = w;} if (self.switchweapon == w) currentscore = s;
407         w = WEP_ELECTRO          ;s = electro;if (s > bestscore){bestscore = s;bestweapon = w;} if (self.switchweapon == w) currentscore = s;
408         w = WEP_CRYLINK          ;s = crylink;if (s > bestscore){bestscore = s;bestweapon = w;} if (self.switchweapon == w) currentscore = s;
409         w = WEP_UZI              ;s = uzi    ;if (s > bestscore){bestscore = s;bestweapon = w;} if (self.switchweapon == w) currentscore = s;
410         w = WEP_SHOTGUN          ;s = shotgun;if (s > bestscore){bestscore = s;bestweapon = w;} if (self.switchweapon == w) currentscore = s;
411         w = WEP_LASER            ;s = laser  ;if (s > bestscore){bestscore = s;bestweapon = w;} if (self.switchweapon == w) currentscore = s;
412
413         // switch if the best weapon would provide a significant damage increase
414         if (bestscore > currentscore*1.5)
415                 self.switchweapon = bestweapon;
416 };
417
418 .float nextaim;
419 void havocbot_aim()
420 {
421         local vector selfvel, enemyvel;
422         if (time < self.nextaim)
423                 return;
424         self.nextaim = time + 0.1;
425         selfvel = self.velocity;
426         if (!self.waterlevel)
427                 selfvel_z = 0;
428         if (self.enemy)
429         {
430                 enemyvel = self.enemy.velocity;
431                 if (!self.enemy.waterlevel)
432                         enemyvel_z = 0;
433                 lag_additem(time + self.ping, 0, 0, self.enemy, self.origin, selfvel, self.enemy.origin, enemyvel);
434         }
435         else
436                 lag_additem(time + self.ping, 0, 0, world, self.origin, selfvel, self.goalcurrent.origin, '0 0 0');
437 };
438
439 .float bot_chooseweapontime;
440 void havocbot_ai()
441 {
442         if (bot_strategytoken == self)
443         if (!bot_strategytoken_taken)
444         {
445                 self.havocbot_role();
446                 // token has been used this frame
447                 bot_strategytoken_taken = TRUE;
448         }
449         havocbot_chooseenemy();
450         if (self.bot_chooseweapontime < time)
451         {
452                 self.bot_chooseweapontime = time + cvar("bot_ai_chooseweaponinterval");
453                 havocbot_chooseweapon();
454         }
455         havocbot_aim();
456         lag_update();
457         if (self.bot_aimtarg)
458         {
459                 weapon_action(self.weapon, WR_AIM);
460                 if (cvar("bot_nofire"))
461                 {
462                         self.BUTTON_ATCK = FALSE;
463                         self.BUTTON_ATCK2 = FALSE;
464                 }
465         }
466         else if (self.goalcurrent)
467         {
468                 local vector now,v,next;//,heading;
469                 local float distance,skillblend,distanceblend,blend;
470                 next = now = self.goalcurrent.origin - (self.origin + self.view_ofs);
471                 distance = vlen(now);
472                 //heading = self.velocity;
473                 //dprint(self.goalstack01.classname,etos(self.goalstack01),"\n");
474                 if(self.goalstack01 != self && self.goalstack01 != world)
475                         next = self.goalstack01.origin - (self.origin + self.view_ofs);
476                 skillblend=bound(0,(skill-2.5)*0.5,1); //lower skill player can't preturn
477                 distanceblend=bound(0,distance/cvar("bot_ai_keyboard_distance"),1);
478                 blend = skillblend * (1-distanceblend);
479                 //v = (now * (distanceblend) + next * (1-distanceblend)) * (skillblend) + now * (1-skillblend);
480                 //v = now * (distanceblend) * (skillblend) + next * (1-distanceblend) * (skillblend) + now * (1-skillblend);
481                 //v = now * ((1-skillblend) + (distanceblend) * (skillblend)) + next * (1-distanceblend) * (skillblend);
482                 v = now + blend * (next - now);
483                 //dprint(etos(self), " ");
484                 //dprint(vtos(now), ":", vtos(next), "=", vtos(v), " (blend ", ftos(blend), ")\n");
485                 //v = now * (distanceblend) + next * (1-distanceblend);
486                 if (self.waterlevel < 2)
487                         v_z = 0;
488                 //dprint("walk at:", vtos(v), "\n");
489                 //te_lightning2(world, self.origin, self.goalcurrent.origin);
490                 bot_aimdir(v, -1);
491         }
492         havocbot_movetogoal();
493 };
494
495 void havocbot_setupbot()
496 {
497         self.bot_ai = havocbot_ai;
498         // will be updated by think code
499         //Generate some random skill levels
500         self.havocbot_keyboardskill=random()-0.5;
501         havocbot_chooserole();
502 }