]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/bot/default/havocbot/havocbot.qc
Similarly to the previous commit, make detection of obstacles that can be jumped...
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / bot / default / havocbot / havocbot.qc
1 #include "havocbot.qh"
2
3 #include "../cvars.qh"
4
5 #include "../aim.qh"
6 #include "../bot.qh"
7 #include "../navigation.qh"
8 #include "../scripting.qh"
9 #include "../waypoints.qh"
10
11 #include <common/constants.qh>
12 #include <common/impulses/all.qh>
13 #include <common/net_linked.qh>
14 #include <common/physics/player.qh>
15 #include <common/state.qh>
16 #include <common/items/_mod.qh>
17 #include <common/wepent.qh>
18
19 #include <common/triggers/teleporters.qh>
20 #include <common/triggers/trigger/jumppads.qh>
21
22 #include <lib/warpzone/common.qh>
23
24 .float speed;
25
26 void havocbot_ai(entity this)
27 {
28         if(this.draggedby)
29                 return;
30
31         if(bot_execute_commands(this))
32                 return;
33
34         if (bot_strategytoken == this)
35         if (!bot_strategytoken_taken)
36         {
37                 if(this.havocbot_blockhead)
38                 {
39                         this.havocbot_blockhead = false;
40                 }
41                 else
42                 {
43                         if (!this.jumppadcount)
44                                 this.havocbot_role(this); // little too far down the rabbit hole
45                 }
46
47                 // TODO: tracewalk() should take care of this job (better path finding under water)
48                 // if we don't have a goal and we're under water look for a waypoint near the "shore" and push it
49                 if(!(IS_DEAD(this)))
50                 if(!this.goalcurrent)
51                 if(this.waterlevel == WATERLEVEL_SWIMMING || (this.aistatus & AI_STATUS_OUT_WATER))
52                 {
53                         // Look for the closest waypoint out of water
54                         entity newgoal = NULL;
55                         IL_EACH(g_waypoints, vdist(it.origin - this.origin, <=, 10000),
56                         {
57                                 if(it.origin.z < this.origin.z)
58                                         continue;
59
60                                 if(it.origin.z - this.origin.z - this.view_ofs.z > 100)
61                                         continue;
62
63                                 if (pointcontents(it.origin + it.maxs + '0 0 1') != CONTENT_EMPTY)
64                                         continue;
65
66                                 traceline(this.origin + this.view_ofs, ((it.absmin + it.absmax) * 0.5), true, this);
67
68                                 if(trace_fraction < 1)
69                                         continue;
70
71                                 if(!newgoal || vlen2(it.origin - this.origin) < vlen2(newgoal.origin - this.origin))
72                                         newgoal = it;
73                         });
74
75                         if(newgoal)
76                         {
77                         //      te_wizspike(newgoal.origin);
78                                 navigation_pushroute(this, newgoal);
79                         }
80                 }
81
82                 // token has been used this frame
83                 bot_strategytoken_taken = true;
84         }
85
86         if(IS_DEAD(this))
87                 return;
88
89         havocbot_chooseenemy(this);
90
91         for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
92         {
93                 .entity weaponentity = weaponentities[slot];
94                 if(this.(weaponentity).m_weapon != WEP_Null || slot == 0)
95                 if(this.(weaponentity).bot_chooseweapontime < time)
96                 {
97                         this.(weaponentity).bot_chooseweapontime = time + autocvar_bot_ai_chooseweaponinterval;
98                         havocbot_chooseweapon(this, weaponentity);
99                 }
100         }
101         havocbot_aim(this);
102         lag_update(this);
103         if (this.bot_aimtarg)
104         {
105                 this.aistatus |= AI_STATUS_ATTACKING;
106                 this.aistatus &= ~AI_STATUS_ROAMING;
107
108                 if(this.weapons)
109                 {
110                         if (autocvar_bot_nofire || IS_INDEPENDENT_PLAYER(this))
111                         {
112                                 PHYS_INPUT_BUTTON_ATCK(this) = false;
113                                 PHYS_INPUT_BUTTON_ATCK2(this) = false;
114                         }
115                         else
116                         {
117                                 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
118                                 {
119                                         .entity weaponentity = weaponentities[slot];
120                                         Weapon w = this.(weaponentity).m_weapon;
121                                         if(w == WEP_Null && slot != 0)
122                                                 continue;
123                                         w.wr_aim(w, this, weaponentity);
124                                         if(PHYS_INPUT_BUTTON_ATCK(this) || PHYS_INPUT_BUTTON_ATCK2(this)) // TODO: what if we didn't fire this weapon, but the previous?
125                                                 this.(weaponentity).lastfiredweapon = this.(weaponentity).m_weapon.m_id;
126                                 }
127                         }
128                 }
129                 else
130                 {
131                         if(IS_PLAYER(this.bot_aimtarg))
132                                 bot_aimdir(this, this.bot_aimtarg.origin + this.bot_aimtarg.view_ofs - this.origin - this.view_ofs , -1);
133                 }
134         }
135         else if (this.goalcurrent)
136         {
137                 this.aistatus |= AI_STATUS_ROAMING;
138                 this.aistatus &= ~AI_STATUS_ATTACKING;
139
140                 vector now,v,next;//,heading;
141                 float aimdistance,skillblend,distanceblend,blend;
142                 next = now = ( (this.goalcurrent.absmin + this.goalcurrent.absmax) * 0.5) - (this.origin + this.view_ofs);
143                 aimdistance = vlen(now);
144                 //heading = this.velocity;
145                 //dprint(this.goalstack01.classname,etos(this.goalstack01),"\n");
146                 if(
147                         this.goalstack01 != this && this.goalstack01 && !wasfreed(this.goalstack01) && ((this.aistatus & AI_STATUS_RUNNING) == 0) &&
148                         !(this.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT)
149                 )
150                         next = ((this.goalstack01.absmin + this.goalstack01.absmax) * 0.5) - (this.origin + this.view_ofs);
151
152                 skillblend=bound(0,(skill+this.bot_moveskill-2.5)*0.5,1); //lower skill player can't preturn
153                 distanceblend=bound(0,aimdistance/autocvar_bot_ai_keyboard_distance,1);
154                 blend = skillblend * (1-distanceblend);
155                 //v = (now * (distanceblend) + next * (1-distanceblend)) * (skillblend) + now * (1-skillblend);
156                 //v = now * (distanceblend) * (skillblend) + next * (1-distanceblend) * (skillblend) + now * (1-skillblend);
157                 //v = now * ((1-skillblend) + (distanceblend) * (skillblend)) + next * (1-distanceblend) * (skillblend);
158                 v = now + blend * (next - now);
159                 //dprint(etos(this), " ");
160                 //dprint(vtos(now), ":", vtos(next), "=", vtos(v), " (blend ", ftos(blend), ")\n");
161                 //v = now * (distanceblend) + next * (1-distanceblend);
162                 if (this.waterlevel < WATERLEVEL_SWIMMING)
163                         v.z = 0;
164                 //dprint("walk at:", vtos(v), "\n");
165                 //te_lightning2(NULL, this.origin, this.goalcurrent.origin);
166                 bot_aimdir(this, v, -1);
167         }
168         havocbot_movetogoal(this);
169
170         // if the bot is not attacking, consider reloading weapons
171         if (!(this.aistatus & AI_STATUS_ATTACKING))
172         {
173                 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
174                 {
175                         .entity weaponentity = weaponentities[slot];
176
177                         if(this.(weaponentity).m_weapon == WEP_Null && slot != 0)
178                                 continue;
179
180                         // we are currently holding a weapon that's not fully loaded, reload it
181                         if(skill >= 2) // bots can only reload the held weapon on purpose past this skill
182                         if(this.(weaponentity).clip_load < this.(weaponentity).clip_size)
183                                 this.impulse = IMP_weapon_reload.impulse; // not sure if this is done right
184
185                         // if we're not reloading a weapon, switch to any weapon in our invnetory that's not fully loaded to reload it next
186                         // the code above executes next frame, starting the reloading then
187                         if(skill >= 5) // bots can only look for unloaded weapons past this skill
188                         if(this.(weaponentity).clip_load >= 0) // only if we're not reloading a weapon already
189                         {
190                                 FOREACH(Weapons, it != WEP_Null, LAMBDA(
191                                         if((this.weapons & (it.m_wepset)) && (it.spawnflags & WEP_FLAG_RELOADABLE) && (this.(weaponentity).weapon_load[it.m_id] < it.reloading_ammo))
192                                         {
193                                                 this.(weaponentity).m_switchweapon = it;
194                                                 break;
195                                         }
196                                 ));
197                         }
198                 }
199         }
200 }
201
202 void havocbot_keyboard_movement(entity this, vector destorg)
203 {
204         vector keyboard;
205         float blend, maxspeed;
206         float sk;
207
208         sk = skill + this.bot_moveskill;
209
210         maxspeed = autocvar_sv_maxspeed;
211
212         if (time < this.havocbot_keyboardtime)
213                 return;
214
215         this.havocbot_keyboardtime =
216                 max(
217                         this.havocbot_keyboardtime
218                                 + 0.05/max(1, sk+this.havocbot_keyboardskill)
219                                 + random()*0.025/max(0.00025, skill+this.havocbot_keyboardskill)
220                 , time);
221         keyboard = this.movement * (1.0 / maxspeed);
222
223         float trigger, trigger1;
224         blend = bound(0,sk*0.1,1);
225         trigger = autocvar_bot_ai_keyboard_threshold;
226         trigger1 = 0 - trigger;
227
228         // categorize forward movement
229         // at skill < 1.5 only forward
230         // at skill < 2.5 only individual directions
231         // at skill < 4.5 only individual directions, and forward diagonals
232         // at skill >= 4.5, all cases allowed
233         if (keyboard.x > trigger)
234         {
235                 keyboard.x = 1;
236                 if (sk < 2.5)
237                         keyboard.y = 0;
238         }
239         else if (keyboard.x < trigger1 && sk > 1.5)
240         {
241                 keyboard.x = -1;
242                 if (sk < 4.5)
243                         keyboard.y = 0;
244         }
245         else
246         {
247                 keyboard.x = 0;
248                 if (sk < 1.5)
249                         keyboard.y = 0;
250         }
251         if (sk < 4.5)
252                 keyboard.z = 0;
253
254         if (keyboard.y > trigger)
255                 keyboard.y = 1;
256         else if (keyboard.y < trigger1)
257                 keyboard.y = -1;
258         else
259                 keyboard.y = 0;
260
261         if (keyboard.z > trigger)
262                 keyboard.z = 1;
263         else if (keyboard.z < trigger1)
264                 keyboard.z = -1;
265         else
266                 keyboard.z = 0;
267
268         this.havocbot_keyboard = keyboard * maxspeed;
269         if (this.havocbot_ducktime>time) PHYS_INPUT_BUTTON_CROUCH(this) = true;
270
271         keyboard = this.havocbot_keyboard;
272         blend = bound(0,vlen(destorg-this.origin)/autocvar_bot_ai_keyboard_distance,1); // When getting close move with 360 degree
273         //dprint("movement ", vtos(this.movement), " keyboard ", vtos(keyboard), " blend ", ftos(blend), "\n");
274         this.movement = this.movement + (keyboard - this.movement) * blend;
275 }
276
277 void havocbot_bunnyhop(entity this, vector dir)
278 {
279         float bunnyhopdistance;
280         vector deviation;
281         float maxspeed;
282         vector gco, gno;
283
284         // Don't jump when attacking
285         if(this.aistatus & AI_STATUS_ATTACKING)
286                 return;
287
288         if(IS_PLAYER(this.goalcurrent))
289                 return;
290
291         maxspeed = autocvar_sv_maxspeed;
292
293         if(this.aistatus & AI_STATUS_DANGER_AHEAD)
294         {
295                 this.aistatus &= ~AI_STATUS_RUNNING;
296                 PHYS_INPUT_BUTTON_JUMP(this) = false;
297                 this.bot_canruntogoal = 0;
298                 this.bot_timelastseengoal = 0;
299                 return;
300         }
301
302         if(this.waterlevel > WATERLEVEL_WETFEET)
303         {
304                 this.aistatus &= ~AI_STATUS_RUNNING;
305                 return;
306         }
307
308         if(this.bot_lastseengoal != this.goalcurrent && !(this.aistatus & AI_STATUS_RUNNING))
309         {
310                 this.bot_canruntogoal = 0;
311                 this.bot_timelastseengoal = 0;
312         }
313
314         gco = (this.goalcurrent.absmin + this.goalcurrent.absmax) * 0.5;
315         bunnyhopdistance = vlen(this.origin - gco);
316
317         // Run only to visible goals
318         if(IS_ONGROUND(this))
319         if(vlen(this.velocity - eZ * this.velocity.z) >= autocvar_sv_maxspeed) // if -really- running
320         if(checkpvs(this.origin + this.view_ofs, this.goalcurrent))
321         {
322                         this.bot_lastseengoal = this.goalcurrent;
323
324                         // seen it before
325                         if(this.bot_timelastseengoal)
326                         {
327                                 // for a period of time
328                                 if(time - this.bot_timelastseengoal > autocvar_bot_ai_bunnyhop_firstjumpdelay)
329                                 {
330                                         float checkdistance;
331                                         checkdistance = true;
332
333                                         // don't run if it is too close
334                                         if(this.bot_canruntogoal==0)
335                                         {
336                                                 if(bunnyhopdistance > autocvar_bot_ai_bunnyhop_startdistance)
337                                                         this.bot_canruntogoal = 1;
338                                                 else
339                                                         this.bot_canruntogoal = -1;
340                                         }
341
342                                         if(this.bot_canruntogoal != 1)
343                                                 return;
344
345                                         if(this.aistatus & AI_STATUS_ROAMING)
346                                         if(this.goalcurrent.classname=="waypoint")
347                                         if (!(this.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL))
348                                         if(fabs(gco.z - this.origin.z) < this.maxs.z - this.mins.z)
349                                         if(this.goalstack01 && !wasfreed(this.goalstack01))
350                                         {
351                                                 gno = (this.goalstack01.absmin + this.goalstack01.absmax) * 0.5;
352                                                 deviation = vectoangles(gno - this.origin) - vectoangles(gco - this.origin);
353                                                 while (deviation.y < -180) deviation.y = deviation.y + 360;
354                                                 while (deviation.y > 180) deviation.y = deviation.y - 360;
355
356                                                 if(fabs(deviation.y) < 20)
357                                                 if(bunnyhopdistance < vlen(this.origin - gno))
358                                                 if(fabs(gno.z - gco.z) < this.maxs.z - this.mins.z)
359                                                 {
360                                                         if(vdist(gco - gno, >, autocvar_bot_ai_bunnyhop_startdistance))
361                                                         if(checkpvs(this.origin + this.view_ofs, this.goalstack01))
362                                                         {
363                                                                 checkdistance = false;
364                                                         }
365                                                 }
366                                         }
367
368                                         if(checkdistance)
369                                         {
370                                                 this.aistatus &= ~AI_STATUS_RUNNING;
371                                                 // increase stop distance in case the goal is on a slope or a lower platform 
372                                                 if(bunnyhopdistance > autocvar_bot_ai_bunnyhop_stopdistance + (this.origin.z - gco.z))
373                                                         PHYS_INPUT_BUTTON_JUMP(this) = true;
374                                         }
375                                         else
376                                         {
377                                                 this.aistatus |= AI_STATUS_RUNNING;
378                                                 PHYS_INPUT_BUTTON_JUMP(this) = true;
379                                         }
380                                 }
381                         }
382                         else
383                         {
384                                 this.bot_timelastseengoal = time;
385                         }
386         }
387         else
388         {
389                 this.bot_timelastseengoal = 0;
390         }
391
392 #if 0
393         // Release jump button
394         if(!cvar("sv_pogostick"))
395         if((IS_ONGROUND(this)) == 0)
396         {
397                 if(this.velocity.z < 0 || vlen(this.velocity)<maxspeed)
398                         PHYS_INPUT_BUTTON_JUMP(this) = false;
399
400                 // Strafe
401                 if(this.aistatus & AI_STATUS_RUNNING)
402                 if(vlen(this.velocity)>maxspeed)
403                 {
404                         deviation = vectoangles(dir) - vectoangles(this.velocity);
405                         while (deviation.y < -180) deviation.y = deviation.y + 360;
406                         while (deviation.y > 180) deviation.y = deviation.y - 360;
407
408                         if(fabs(deviation.y)>10)
409                                 this.movement_x = 0;
410
411                         if(deviation.y>10)
412                                 this.movement_y = maxspeed * -1;
413                         else if(deviation.y<10)
414                                 this.movement_y = maxspeed;
415
416                 }
417         }
418 #endif
419 }
420
421 void havocbot_movetogoal(entity this)
422 {
423         vector destorg;
424         vector diff;
425         vector dir;
426         vector flatdir;
427         vector m1;
428         vector m2;
429         vector evadeobstacle;
430         vector evadelava;
431         float maxspeed;
432         vector gco;
433         //float dist;
434         vector dodge;
435         //if (this.goalentity)
436         //      te_lightning2(this, this.origin, (this.goalentity.absmin + this.goalentity.absmax) * 0.5);
437         this.movement = '0 0 0';
438         maxspeed = autocvar_sv_maxspeed;
439
440         // Jetpack navigation
441         if(this.goalcurrent)
442         if(this.navigation_jetpack_goal)
443         if(this.goalcurrent==this.navigation_jetpack_goal)
444         if(this.ammo_fuel)
445         {
446                 if(autocvar_bot_debug_goalstack)
447                 {
448                         debuggoalstack(this);
449                         te_wizspike(this.navigation_jetpack_point);
450                 }
451
452                 // Take off
453                 if (!(this.aistatus & AI_STATUS_JETPACK_FLYING))
454                 {
455                         // Brake almost completely so it can get a good direction
456                         if(vdist(this.velocity, >, 10))
457                                 return;
458                         this.aistatus |= AI_STATUS_JETPACK_FLYING;
459                 }
460
461                 makevectors(this.v_angle.y * '0 1 0');
462                 dir = normalize(this.navigation_jetpack_point - this.origin);
463
464                 // Landing
465                 if(this.aistatus & AI_STATUS_JETPACK_LANDING)
466                 {
467                         // Calculate brake distance in xy
468                         float db, v, d;
469                         vector dxy;
470
471                         dxy = this.origin - ( ( this.goalcurrent.absmin + this.goalcurrent.absmax ) * 0.5 ); dxy.z = 0;
472                         d = vlen(dxy);
473                         v = vlen(this.velocity -  this.velocity.z * '0 0 1');
474                         db = (pow(v,2) / (autocvar_g_jetpack_acceleration_side * 2)) + 100;
475                 //      dprint("distance ", ftos(ceil(d)), " velocity ", ftos(ceil(v)), " brake at ", ftos(ceil(db)), "\n");
476                         if(d < db || d < 500)
477                         {
478                                 // Brake
479                                 if(fabs(this.velocity.x)>maxspeed*0.3)
480                                 {
481                                         this.movement_x = dir * v_forward * -maxspeed;
482                                         return;
483                                 }
484                                 // Switch to normal mode
485                                 this.navigation_jetpack_goal = NULL;
486                                 this.aistatus &= ~AI_STATUS_JETPACK_LANDING;
487                                 this.aistatus &= ~AI_STATUS_JETPACK_FLYING;
488                                 return;
489                         }
490                 }
491                 else if(checkpvs(this.origin,this.goalcurrent))
492                 {
493                         // If I can see the goal switch to landing code
494                         this.aistatus &= ~AI_STATUS_JETPACK_FLYING;
495                         this.aistatus |= AI_STATUS_JETPACK_LANDING;
496                         return;
497                 }
498
499                 // Flying
500                 PHYS_INPUT_BUTTON_HOOK(this) = true;
501                 if(this.navigation_jetpack_point.z - STAT(PL_MAX, this).z + STAT(PL_MIN, this).z < this.origin.z)
502                 {
503                         this.movement_x = dir * v_forward * maxspeed;
504                         this.movement_y = dir * v_right * maxspeed;
505                 }
506                 return;
507         }
508
509         // Handling of jump pads
510         if(this.jumppadcount)
511         {
512                 // If got stuck on the jump pad try to reach the farthest visible waypoint
513                 // but with some randomness so it can try out different paths
514                 if(this.aistatus & AI_STATUS_OUT_JUMPPAD)
515                 {
516                         if(fabs(this.velocity.z)<50)
517                         {
518                                 entity newgoal = NULL;
519                                 IL_EACH(g_waypoints, vdist(it.origin - this.origin, <=, 1000),
520                                 {
521                                         traceline(this.origin + this.view_ofs, ((it.absmin + it.absmax) * 0.5), true, this);
522
523                                         if(trace_fraction < 1)
524                                                 continue;
525
526                                         if(!newgoal || ((random() < 0.8) && vlen2(it.origin - this.origin) > vlen2(newgoal.origin - this.origin)))
527                                                 newgoal = it;
528                                 });
529
530                                 if(newgoal)
531                                 {
532                                         this.ignoregoal = this.goalcurrent;
533                                         this.ignoregoaltime = time + autocvar_bot_ai_ignoregoal_timeout;
534                                         navigation_clearroute(this);
535                                         navigation_routetogoal(this, newgoal, this.origin);
536                                         if(autocvar_bot_debug_goalstack)
537                                                 debuggoalstack(this);
538                                         this.aistatus &= ~AI_STATUS_OUT_JUMPPAD;
539                                 }
540                         }
541                         else
542                                 return;
543                 }
544                 else
545                 {
546                         if(this.velocity.z>0)
547                         {
548                                 float threshold;
549                                 vector velxy = this.velocity; velxy_z = 0;
550                                 threshold = maxspeed * 0.2;
551                                 if(vdist(velxy, <, threshold))
552                                 {
553                                         LOG_TRACE("Warning: ", this.netname, " got stuck on a jumppad (velocity in xy is ", vtos(velxy), "), trying to get out of it now");
554                                         this.aistatus |= AI_STATUS_OUT_JUMPPAD;
555                                 }
556                                 return;
557                         }
558
559                         // Don't chase players while using a jump pad
560                         if(IS_PLAYER(this.goalcurrent) || IS_PLAYER(this.goalstack01))
561                                 return;
562                 }
563         }
564         else if(this.aistatus & AI_STATUS_OUT_JUMPPAD)
565                 this.aistatus &= ~AI_STATUS_OUT_JUMPPAD;
566
567         // If there is a trigger_hurt right below try to use the jetpack or make a rocketjump
568         if(skill>6)
569         if (!(IS_ONGROUND(this)))
570         {
571                 tracebox(this.origin, this.mins, this.maxs, this.origin + '0 0 -65536', MOVE_NOMONSTERS, this);
572                 if(tracebox_hits_trigger_hurt(this.origin, this.mins, this.maxs, trace_endpos ))
573                 if(this.items & IT_JETPACK)
574                 {
575                         tracebox(this.origin, this.mins, this.maxs, this.origin + '0 0 65536', MOVE_NOMONSTERS, this);
576                         if(tracebox_hits_trigger_hurt(this.origin, this.mins, this.maxs, trace_endpos + '0 0 1' ))
577                         {
578                                 if(this.velocity.z<0)
579                                 {
580                                         PHYS_INPUT_BUTTON_HOOK(this) = true;
581                                 }
582                         }
583                         else
584                                 PHYS_INPUT_BUTTON_HOOK(this) = true;
585
586                         // If there is no goal try to move forward
587
588                         if(this.goalcurrent==NULL)
589                                 dir = v_forward;
590                         else
591                                 dir = normalize(( ( this.goalcurrent.absmin + this.goalcurrent.absmax ) * 0.5 ) - this.origin);
592
593                         vector xyvelocity = this.velocity; xyvelocity_z = 0;
594                         float xyspeed = xyvelocity * dir;
595
596                         if(xyspeed < (maxspeed / 2))
597                         {
598                                 makevectors(this.v_angle.y * '0 1 0');
599                                 tracebox(this.origin, this.mins, this.maxs, this.origin + (dir * maxspeed * 3), MOVE_NOMONSTERS, this);
600                                 if(trace_fraction==1)
601                                 {
602                                         this.movement_x = dir * v_forward * maxspeed;
603                                         this.movement_y = dir * v_right * maxspeed;
604                                         if (skill < 10)
605                                                 havocbot_keyboard_movement(this, this.origin + dir * 100);
606                                 }
607                         }
608
609                         this.havocbot_blockhead = true;
610
611                         return;
612                 }
613                 else if(this.health>WEP_CVAR(devastator, damage)*0.5)
614                 {
615                         if(this.velocity.z < 0)
616                         {
617                                 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
618                                 {
619                                         .entity weaponentity = weaponentities[slot];
620
621                                         if(this.(weaponentity).m_weapon == WEP_Null && slot != 0)
622                                                 continue;
623
624                                         if(client_hasweapon(this, WEP_DEVASTATOR, weaponentity, true, false))
625                                         {
626                                                 this.movement_x = maxspeed;
627
628                                                 if(this.rocketjumptime)
629                                                 {
630                                                         if(time > this.rocketjumptime)
631                                                         {
632                                                                 PHYS_INPUT_BUTTON_ATCK2(this) = true;
633                                                                 this.rocketjumptime = 0;
634                                                         }
635                                                         return;
636                                                 }
637
638                                                 this.(weaponentity).m_switchweapon = WEP_DEVASTATOR;
639                                                 this.v_angle_x = 90;
640                                                 PHYS_INPUT_BUTTON_ATCK(this) = true;
641                                                 this.rocketjumptime = time + WEP_CVAR(devastator, detonatedelay);
642                                                 return;
643                                         }
644                                 }
645                         }
646                 }
647                 else
648                 {
649                         // If there is no goal try to move forward
650                         if(this.goalcurrent==NULL)
651                                 this.movement_x = maxspeed;
652                 }
653         }
654
655         // If we are under water with no goals, swim up
656         if(this.waterlevel)
657         if(this.goalcurrent==NULL)
658         {
659                 dir = '0 0 0';
660                 if(this.waterlevel>WATERLEVEL_SWIMMING)
661                         dir.z = 1;
662                 else if(this.velocity.z >= 0 && !(this.waterlevel == WATERLEVEL_WETFEET && this.watertype == CONTENT_WATER))
663                         PHYS_INPUT_BUTTON_JUMP(this) = true;
664                 else
665                         PHYS_INPUT_BUTTON_JUMP(this) = false;
666                 makevectors(this.v_angle.y * '0 1 0');
667                 this.movement_x = dir * v_forward * maxspeed;
668                 this.movement_y = dir * v_right * maxspeed;
669                 this.movement_z = dir * v_up * maxspeed;
670         }
671
672         // if there is nowhere to go, exit
673         if (this.goalcurrent == NULL)
674                 return;
675
676         navigation_poptouchedgoals(this);
677
678         // if ran out of goals try to use an alternative goal or get a new strategy asap
679         if(this.goalcurrent == NULL)
680         {
681                 this.bot_strategytime = 0;
682                 return;
683         }
684
685
686         if(autocvar_bot_debug_goalstack)
687                 debuggoalstack(this);
688
689         m1 = this.goalcurrent.origin + this.goalcurrent.mins;
690         m2 = this.goalcurrent.origin + this.goalcurrent.maxs;
691         destorg = this.origin;
692         destorg.x = bound(m1_x, destorg.x, m2_x);
693         destorg.y = bound(m1_y, destorg.y, m2_y);
694         destorg.z = bound(m1_z, destorg.z, m2_z);
695         diff = destorg - this.origin;
696         //dist = vlen(diff);
697         dir = normalize(diff);
698         flatdir = diff;flatdir.z = 0;
699         flatdir = normalize(flatdir);
700         gco = (this.goalcurrent.absmin + this.goalcurrent.absmax) * 0.5;
701
702         //if (this.bot_dodgevector_time < time)
703         {
704         //      this.bot_dodgevector_time = time + cvar("bot_ai_dodgeupdateinterval");
705         //      this.bot_dodgevector_jumpbutton = 1;
706                 evadeobstacle = '0 0 0';
707                 evadelava = '0 0 0';
708
709                 makevectors(this.v_angle.y * '0 1 0');
710                 if (this.waterlevel)
711                 {
712                         if(this.waterlevel>WATERLEVEL_SWIMMING)
713                         {
714                         //      flatdir_z = 1;
715                                 this.aistatus |= AI_STATUS_OUT_WATER;
716                         }
717                         else
718                         {
719                                 if(this.velocity.z >= 0 && !(this.watertype == CONTENT_WATER && gco.z < this.origin.z) &&
720                                         ( !(this.waterlevel == WATERLEVEL_WETFEET && this.watertype == CONTENT_WATER) || this.aistatus & AI_STATUS_OUT_WATER))
721                                         PHYS_INPUT_BUTTON_JUMP(this) = true;
722                                 else
723                                         PHYS_INPUT_BUTTON_JUMP(this) = false;
724                         }
725                         dir = normalize(flatdir);
726                 }
727                 else
728                 {
729                         float s;
730                         vector offset;
731                         if(this.aistatus & AI_STATUS_OUT_WATER)
732                                 this.aistatus &= ~AI_STATUS_OUT_WATER;
733
734                         // jump if going toward an obstacle that doesn't look like stairs we
735                         // can walk up directly
736                         offset = (vdist(this.velocity, >, 32) ? this.velocity * 0.2 : v_forward * 32);
737                         tracebox(this.origin, this.mins, this.maxs, this.origin + offset, false, this);
738                         if (trace_fraction < 1)
739                         if (trace_plane_normal.z < 0.7)
740                         {
741                                 s = trace_fraction;
742                                 tracebox(this.origin + stepheightvec, this.mins, this.maxs, this.origin + offset + stepheightvec, false, this);
743                                 if (trace_fraction < s + 0.01)
744                                 if (trace_plane_normal.z < 0.7)
745                                 {
746                                         s = trace_fraction;
747                                         tracebox(this.origin + jumpstepheightvec, this.mins, this.maxs, this.origin + offset + jumpstepheightvec, false, this);
748                                         if (trace_fraction > s)
749                                                 PHYS_INPUT_BUTTON_JUMP(this) = true;
750                                 }
751                         }
752
753                         // avoiding dangers and obstacles
754                         offset = (vdist(this.velocity, >, 32) ? this.velocity * 0.5 : v_forward * 32);
755                         vector dst_ahead = this.origin + this.view_ofs + offset;
756                         vector dst_down = dst_ahead - '0 0 3000';
757
758                         // Look ahead
759                         traceline(this.origin + this.view_ofs, dst_ahead, true, NULL);
760
761                         // Check head-banging against walls
762                         if(vdist(this.origin + this.view_ofs - trace_endpos, <, 25) && !(this.aistatus & AI_STATUS_OUT_WATER))
763                         {
764                                 PHYS_INPUT_BUTTON_JUMP(this) = true;
765                                 if(this.facingwalltime && time > this.facingwalltime)
766                                 {
767                                         this.ignoregoal = this.goalcurrent;
768                                         this.ignoregoaltime = time + autocvar_bot_ai_ignoregoal_timeout;
769                                         this.bot_strategytime = 0;
770                                         return;
771                                 }
772                                 else
773                                 {
774                                         this.facingwalltime = time + 0.05;
775                                 }
776                         }
777                         else
778                         {
779                                 this.facingwalltime = 0;
780
781                                 if(this.ignoregoal != NULL && time > this.ignoregoaltime)
782                                 {
783                                         this.ignoregoal = NULL;
784                                         this.ignoregoaltime = 0;
785                                 }
786                         }
787
788                         // Check for water/slime/lava and dangerous edges
789                         // (only when the bot is on the ground or jumping intentionally)
790                         this.aistatus &= ~AI_STATUS_DANGER_AHEAD;
791
792                         bool unreachable = false;
793                         s = CONTENT_SOLID;
794                         if(trace_fraction == 1 && this.jumppadcount == 0 && !this.goalcurrent.wphardwired )
795                         if((IS_ONGROUND(this)) || (this.aistatus & AI_STATUS_RUNNING) || (this.aistatus & AI_STATUS_ROAMING) || PHYS_INPUT_BUTTON_JUMP(this))
796                         {
797                                 // Look downwards
798                                 traceline(dst_ahead , dst_down, true, NULL);
799                                 //te_lightning2(NULL, this.origin + this.view_ofs, dst_ahead); // Draw "ahead" look
800                                 //te_lightning2(NULL, dst_ahead, dst_down); // Draw "downwards" look
801                                 if(trace_endpos.z < this.origin.z + this.mins.z)
802                                 {
803                                         s = pointcontents(trace_endpos + '0 0 1');
804                                         if (s != CONTENT_SOLID)
805                                         if (s == CONTENT_LAVA || s == CONTENT_SLIME)
806                                                 evadelava = normalize(this.velocity) * -1;
807                                         else if (s == CONTENT_SKY)
808                                                 evadeobstacle = normalize(this.velocity) * -1;
809                                         else if (tracebox_hits_trigger_hurt(dst_ahead, this.mins, this.maxs, trace_endpos))
810                                         {
811                                                 // the traceline check isn't enough but is good as optimization,
812                                                 // when not true (most of the time) this tracebox call is avoided
813                                                 tracebox(dst_ahead, this.mins, this.maxs, dst_down, true, this);
814                                                 if (tracebox_hits_trigger_hurt(dst_ahead, this.mins, this.maxs, trace_endpos))
815                                                 {
816                                                         if (gco.z > this.origin.z + jumpstepheightvec.z)
817                                                         {
818                                                                 // the goal is probably on an upper platform, assume bot can't get there
819                                                                 unreachable = true;
820                                                         }
821                                                         else
822                                                                 evadelava = normalize(this.velocity) * -1;
823                                                 }
824                                         }
825                                 }
826                         }
827
828                         dir = flatdir;
829                         evadeobstacle.z = 0;
830                         evadelava.z = 0;
831                         makevectors(this.v_angle.y * '0 1 0');
832
833                         if(evadeobstacle || evadelava || (s == CONTENT_WATER))
834                         {
835                                 this.aistatus |= AI_STATUS_DANGER_AHEAD;
836                                 if(IS_PLAYER(this.goalcurrent))
837                                         unreachable = true;
838                         }
839                         if(unreachable)
840                         {
841                                 navigation_clearroute(this);
842                                 this.bot_strategytime = 0;
843                         }
844                 }
845
846                 dodge = havocbot_dodge(this);
847                 dodge = dodge * bound(0,0.5+(skill+this.bot_dodgeskill)*0.1,1);
848                 evadelava = evadelava * bound(1,3-(skill+this.bot_dodgeskill),3); //Noobs fear lava a lot and take more distance from it
849                 traceline(this.origin, ( ( this.enemy.absmin + this.enemy.absmax ) * 0.5 ), true, NULL);
850                 if(IS_PLAYER(trace_ent))
851                         dir = dir * bound(0,(skill+this.bot_dodgeskill)/7,1);
852
853                 dir = normalize(dir + dodge + evadeobstacle + evadelava);
854         //      this.bot_dodgevector = dir;
855         //      this.bot_dodgevector_jumpbutton = PHYS_INPUT_BUTTON_JUMP(this);
856         }
857
858         if(time < this.ladder_time)
859         {
860                 if(this.goalcurrent.origin.z + this.goalcurrent.mins.z > this.origin.z + this.mins.z)
861                 {
862                         if(this.origin.z + this.mins.z  < this.ladder_entity.origin.z + this.ladder_entity.maxs.z)
863                                 dir.z = 1;
864                 }
865                 else
866                 {
867                         if(this.origin.z + this.mins.z  > this.ladder_entity.origin.z + this.ladder_entity.mins.z)
868                                 dir.z = -1;
869                 }
870         }
871
872         //dir = this.bot_dodgevector;
873         //if (this.bot_dodgevector_jumpbutton)
874         //      PHYS_INPUT_BUTTON_JUMP(this) = true;
875         this.movement_x = dir * v_forward * maxspeed;
876         this.movement_y = dir * v_right * maxspeed;
877         this.movement_z = dir * v_up * maxspeed;
878
879         // Emulate keyboard interface
880         if (skill < 10)
881                 havocbot_keyboard_movement(this, destorg);
882
883         // Bunnyhop!
884 //      if(this.aistatus & AI_STATUS_ROAMING)
885         if(this.goalcurrent)
886         if(skill+this.bot_moveskill >= autocvar_bot_ai_bunnyhop_skilloffset)
887                 havocbot_bunnyhop(this, dir);
888
889         if ((dir * v_up) >= autocvar_sv_jumpvelocity*0.5 && (IS_ONGROUND(this))) PHYS_INPUT_BUTTON_JUMP(this) = true;
890         if (((dodge * v_up) > 0) && random()*frametime >= 0.2*bound(0,(10-skill-this.bot_dodgeskill)*0.1,1)) PHYS_INPUT_BUTTON_JUMP(this) = true;
891         if (((dodge * v_up) < 0) && random()*frametime >= 0.5*bound(0,(10-skill-this.bot_dodgeskill)*0.1,1)) this.havocbot_ducktime=time+0.3/bound(0.1,skill+this.bot_dodgeskill,10);
892 }
893
894 entity havocbot_gettarget(entity this, bool secondary)
895 {
896         entity best = NULL;
897         vector eye = CENTER_OR_VIEWOFS(this);
898         IL_EACH(g_bot_targets, boolean((secondary) ? it.classname == "misc_breakablemodel" : it.classname != "misc_breakablemodel"),
899         {
900                 vector v = CENTER_OR_VIEWOFS(it);
901                 if(vdist(v - eye, <, autocvar_bot_ai_enemydetectionradius))
902                 if(!best || vlen2(CENTER_OR_VIEWOFS(best) - eye) > vlen2(v - eye))
903                 if(bot_shouldattack(this, it))
904                 {
905                         traceline(eye, v, true, this);
906                         if (trace_ent == it || trace_fraction >= 1)
907                                 best = it;
908                 }
909         });
910
911         return best;
912 }
913
914 void havocbot_chooseenemy(entity this)
915 {
916         if (autocvar_bot_nofire || IS_INDEPENDENT_PLAYER(this))
917         {
918                 this.enemy = NULL;
919                 return;
920         }
921         if (this.enemy)
922         {
923                 if (!bot_shouldattack(this, this.enemy))
924                 {
925                         // enemy died or something, find a new target
926                         this.enemy = NULL;
927                         this.havocbot_chooseenemy_finished = time;
928                 }
929                 else if (this.havocbot_stickenemy)
930                 {
931                         // tracking last chosen enemy
932                         // if enemy is visible
933                         // and not really really far away
934                         // and we're not severely injured
935                         // then keep tracking for a half second into the future
936                         traceline(this.origin+this.view_ofs, ( this.enemy.absmin + this.enemy.absmax ) * 0.5,false,NULL);
937                         if (trace_ent == this.enemy || trace_fraction == 1)
938                         if (vdist(((this.enemy.absmin + this.enemy.absmax) * 0.5) - this.origin, <, 1000))
939                         if (this.health > 30)
940                         {
941                                 // remain tracking him for a shot while (case he went after a small corner or pilar
942                                 this.havocbot_chooseenemy_finished = time + 0.5;
943                                 return;
944                         }
945                         // enemy isn't visible, or is far away, or we're injured severely
946                         // so stop preferring this enemy
947                         // (it will still take a half second until a new one is chosen)
948                         this.havocbot_stickenemy = 0;
949                 }
950         }
951         if (time < this.havocbot_chooseenemy_finished)
952                 return;
953         this.havocbot_chooseenemy_finished = time + autocvar_bot_ai_enemydetectioninterval;
954         vector eye = this.origin + this.view_ofs;
955         entity best = NULL;
956         float bestrating = 100000000;
957
958         // Backup hit flags
959         int hf = this.dphitcontentsmask;
960
961         // Search for enemies, if no enemy can be seen directly try to look through transparent objects
962
963         this.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_CORPSE;
964
965         bool scan_transparent = false;
966         bool scan_secondary_targets = false;
967         bool have_secondary_targets = false;
968         while(true)
969         {
970                 scan_secondary_targets = false;
971 LABEL(scan_targets)
972                 IL_EACH(g_bot_targets, it.bot_attack,
973                 {
974                         if(!scan_secondary_targets)
975                         {
976                                 if(it.classname == "misc_breakablemodel")
977                                 {
978                                         have_secondary_targets = true;
979                                         continue;
980                                 }
981                         }
982                         else if(it.classname != "misc_breakablemodel")
983                                 continue;
984
985                         vector v = (it.absmin + it.absmax) * 0.5;
986                         float rating = vlen2(v - eye);
987                         if (vdist(v - eye, <, autocvar_bot_ai_enemydetectionradius))
988                         if (bestrating > rating)
989                         if (bot_shouldattack(this, it))
990                         {
991                                 traceline(eye, v, true, this);
992                                 if (trace_ent == it || trace_fraction >= 1)
993                                 {
994                                         best = it;
995                                         bestrating = rating;
996                                 }
997                         }
998                 });
999
1000                 if(!best && have_secondary_targets && !scan_secondary_targets)
1001                 {
1002                         scan_secondary_targets = true;
1003                         // restart the loop
1004                         bestrating = 100000000;
1005                         goto scan_targets;
1006                 }
1007
1008                 // I want to do a second scan if no enemy was found or I don't have weapons
1009                 // TODO: Perform the scan when using the rifle (requires changes on the rifle code)
1010                 if(best || this.weapons) // || this.weapon == WEP_RIFLE.m_id
1011                         break;
1012                 if(scan_transparent)
1013                         break;
1014
1015                 // Set flags to see through transparent objects
1016                 this.dphitcontentsmask |= DPCONTENTS_OPAQUE;
1017
1018                 scan_transparent = true;
1019         }
1020
1021         // Restore hit flags
1022         this.dphitcontentsmask = hf;
1023
1024         this.enemy = best;
1025         this.havocbot_stickenemy = true;
1026         if(best && best.classname == "misc_breakablemodel")
1027                 this.havocbot_stickenemy = false;
1028 }
1029
1030 float havocbot_chooseweapon_checkreload(entity this, .entity weaponentity, int new_weapon)
1031 {
1032         // bots under this skill cannot find unloaded weapons to reload idly when not in combat,
1033         // so skip this for them, or they'll never get to reload their weapons at all.
1034         // this also allows bots under this skill to be more stupid, and reload more often during combat :)
1035         if(skill < 5)
1036                 return false;
1037
1038         // if this weapon is scheduled for reloading, don't switch to it during combat
1039         if (this.(weaponentity).weapon_load[new_weapon] < 0)
1040         {
1041                 FOREACH(Weapons, it != WEP_Null, LAMBDA(
1042                         if(it.wr_checkammo1(it, this, weaponentity) + it.wr_checkammo2(it, this, weaponentity))
1043                                 return true; // other weapon available
1044                 ));
1045         }
1046
1047         return false;
1048 }
1049
1050 void havocbot_chooseweapon(entity this, .entity weaponentity)
1051 {
1052         int i;
1053
1054         // ;)
1055         if(g_weaponarena_weapons == WEPSET(TUBA))
1056         {
1057                 this.(weaponentity).m_switchweapon = WEP_TUBA;
1058                 return;
1059         }
1060
1061         // TODO: clean this up by moving it to weapon code
1062         if(this.enemy==NULL)
1063         {
1064                 // If no weapon was chosen get the first available weapon
1065                 if(this.(weaponentity).m_weapon==WEP_Null)
1066                 FOREACH(Weapons, it != WEP_Null, LAMBDA(
1067                         if(client_hasweapon(this, it, weaponentity, true, false))
1068                         {
1069                                 this.(weaponentity).m_switchweapon = it;
1070                                 return;
1071                         }
1072                 ));
1073                 return;
1074         }
1075
1076         // Do not change weapon during the next second after a combo
1077         float f = time - this.lastcombotime;
1078         if(f < 1)
1079                 return;
1080
1081         float w;
1082         float distance; distance=bound(10,vlen(this.origin-this.enemy.origin)-200,10000);
1083
1084         // Should it do a weapon combo?
1085         float af, ct, combo_time, combo;
1086
1087         af = ATTACK_FINISHED(this, 0);
1088         ct = autocvar_bot_ai_weapon_combo_threshold;
1089
1090         // Bots with no skill will be 4 times more slower than "godlike" bots when doing weapon combos
1091         // Ideally this 4 should be calculated as longest_weapon_refire / bot_ai_weapon_combo_threshold
1092         combo_time = time + ct + (ct * ((-0.3*(skill+this.bot_weaponskill))+3));
1093
1094         combo = false;
1095
1096         if(autocvar_bot_ai_weapon_combo)
1097         if(this.(weaponentity).m_weapon.m_id == this.(weaponentity).lastfiredweapon)
1098         if(af > combo_time)
1099         {
1100                 combo = true;
1101                 this.lastcombotime = time;
1102         }
1103
1104         distance *= pow(2, this.bot_rangepreference);
1105
1106         // Custom weapon list based on distance to the enemy
1107         if(bot_custom_weapon){
1108
1109                 // Choose weapons for far distance
1110                 if ( distance > bot_distance_far ) {
1111                         for(i=0; i < Weapons_COUNT && bot_weapons_far[i] != -1 ; ++i){
1112                                 w = bot_weapons_far[i];
1113                                 if ( client_hasweapon(this, Weapons_from(w), weaponentity, true, false) )
1114                                 {
1115                                         if ((this.(weaponentity).m_weapon.m_id == w && combo) || havocbot_chooseweapon_checkreload(this, weaponentity, w))
1116                                                 continue;
1117                                         this.(weaponentity).m_switchweapon = Weapons_from(w);
1118                                         return;
1119                                 }
1120                         }
1121                 }
1122
1123                 // Choose weapons for mid distance
1124                 if ( distance > bot_distance_close) {
1125                         for(i=0; i < Weapons_COUNT && bot_weapons_mid[i] != -1 ; ++i){
1126                                 w = bot_weapons_mid[i];
1127                                 if ( client_hasweapon(this, Weapons_from(w), weaponentity, true, false) )
1128                                 {
1129                                         if ((this.(weaponentity).m_weapon.m_id == w && combo) || havocbot_chooseweapon_checkreload(this, weaponentity, w))
1130                                                 continue;
1131                                         this.(weaponentity).m_switchweapon = Weapons_from(w);
1132                                         return;
1133                                 }
1134                         }
1135                 }
1136
1137                 // Choose weapons for close distance
1138                 for(i=0; i < Weapons_COUNT && bot_weapons_close[i] != -1 ; ++i){
1139                         w = bot_weapons_close[i];
1140                         if ( client_hasweapon(this, Weapons_from(w), weaponentity, true, false) )
1141                         {
1142                                 if ((this.(weaponentity).m_weapon.m_id == w && combo) || havocbot_chooseweapon_checkreload(this, weaponentity, w))
1143                                         continue;
1144                                 this.(weaponentity).m_switchweapon = Weapons_from(w);
1145                                 return;
1146                         }
1147                 }
1148         }
1149 }
1150
1151 void havocbot_aim(entity this)
1152 {
1153         vector myvel, enemyvel;
1154 //      if(this.flags & FL_INWATER)
1155 //              return;
1156         if (time < this.nextaim)
1157                 return;
1158         this.nextaim = time + 0.1;
1159         myvel = this.velocity;
1160         if (!this.waterlevel)
1161                 myvel.z = 0;
1162         if (this.enemy)
1163         {
1164                 enemyvel = this.enemy.velocity;
1165                 if (!this.enemy.waterlevel)
1166                         enemyvel.z = 0;
1167                 lag_additem(this, time + this.ping, 0, 0, this.enemy, this.origin, myvel, (this.enemy.absmin + this.enemy.absmax) * 0.5, enemyvel);
1168         }
1169         else
1170                 lag_additem(this, time + this.ping, 0, 0, NULL, this.origin, myvel, ( this.goalcurrent.absmin + this.goalcurrent.absmax ) * 0.5, '0 0 0');
1171 }
1172
1173 bool havocbot_moveto_refresh_route(entity this)
1174 {
1175         // Refresh path to goal if necessary
1176         entity wp;
1177         wp = this.havocbot_personal_waypoint;
1178         navigation_goalrating_start(this);
1179         navigation_routerating(this, wp, 10000, 10000);
1180         navigation_goalrating_end(this);
1181         return this.navigation_hasgoals;
1182 }
1183
1184 float havocbot_moveto(entity this, vector pos)
1185 {
1186         entity wp;
1187
1188         if(this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
1189         {
1190                 // Step 4: Move to waypoint
1191                 if(this.havocbot_personal_waypoint==NULL)
1192                 {
1193                         LOG_TRACE("Error: ", this.netname, " trying to walk to a non existent personal waypoint");
1194                         this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_GOING;
1195                         return CMD_STATUS_ERROR;
1196                 }
1197
1198                 if (!bot_strategytoken_taken)
1199                 if(this.havocbot_personal_waypoint_searchtime<time)
1200                 {
1201                         bot_strategytoken_taken = true;
1202                         if(havocbot_moveto_refresh_route(this))
1203                         {
1204                                 LOG_TRACE(this.netname, " walking to its personal waypoint (after ", ftos(this.havocbot_personal_waypoint_failcounter), " failed attempts)");
1205                                 this.havocbot_personal_waypoint_searchtime = time + 10;
1206                                 this.havocbot_personal_waypoint_failcounter = 0;
1207                         }
1208                         else
1209                         {
1210                                 this.havocbot_personal_waypoint_failcounter += 1;
1211                                 this.havocbot_personal_waypoint_searchtime = time + 2;
1212                                 if(this.havocbot_personal_waypoint_failcounter >= 30)
1213                                 {
1214                                         LOG_TRACE("Warning: can't walk to the personal waypoint located at ", vtos(this.havocbot_personal_waypoint.origin));
1215                                         this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_LINKING;
1216                                         delete(this.havocbot_personal_waypoint);
1217                                         return CMD_STATUS_ERROR;
1218                                 }
1219                                 else
1220                                         LOG_TRACE(this.netname, " can't walk to its personal waypoint (after ", ftos(this.havocbot_personal_waypoint_failcounter), " failed attempts), trying later");
1221                         }
1222                 }
1223
1224                 if(autocvar_bot_debug_goalstack)
1225                         debuggoalstack(this);
1226
1227                 // Heading
1228                 vector dir = ( ( this.goalcurrent.absmin + this.goalcurrent.absmax ) * 0.5 ) - (this.origin + this.view_ofs);
1229                 dir.z = 0;
1230                 bot_aimdir(this, dir, -1);
1231
1232                 // Go!
1233                 havocbot_movetogoal(this);
1234
1235                 if(this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_REACHED)
1236                 {
1237                         // Step 5: Waypoint reached
1238                         LOG_TRACE(this.netname, "'s personal waypoint reached");
1239                         delete(this.havocbot_personal_waypoint);
1240                         this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_REACHED;
1241                         return CMD_STATUS_FINISHED;
1242                 }
1243
1244                 return CMD_STATUS_EXECUTING;
1245         }
1246
1247         // Step 2: Linking waypoint
1248         if(this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_LINKING)
1249         {
1250                 // Wait until it is linked
1251                 if(!this.havocbot_personal_waypoint.wplinked)
1252                 {
1253                         LOG_TRACE(this.netname, " waiting for personal waypoint to be linked");
1254                         return CMD_STATUS_EXECUTING;
1255                 }
1256
1257                 this.havocbot_personal_waypoint_searchtime = time; // so we set the route next frame
1258                 this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_LINKING;
1259                 this.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_GOING;
1260
1261                 // Step 3: Route to waypoint
1262                 LOG_TRACE(this.netname, " walking to its personal waypoint");
1263
1264                 return CMD_STATUS_EXECUTING;
1265         }
1266
1267         // Step 1: Spawning waypoint
1268         wp = waypoint_spawnpersonal(this, pos);
1269         if(wp==NULL)
1270         {
1271                 LOG_TRACE("Error: Can't spawn personal waypoint at ",vtos(pos));
1272                 return CMD_STATUS_ERROR;
1273         }
1274
1275         this.havocbot_personal_waypoint = wp;
1276         this.havocbot_personal_waypoint_failcounter = 0;
1277         this.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_LINKING;
1278
1279         // if pos is inside a teleport, then let's mark it as teleport waypoint
1280         IL_EACH(g_teleporters, WarpZoneLib_BoxTouchesBrush(pos, pos, it, NULL),
1281         {
1282                 wp.wpflags |= WAYPOINTFLAG_TELEPORT;
1283                 this.lastteleporttime = 0;
1284         });
1285
1286 /*
1287         if(wp.wpflags & WAYPOINTFLAG_TELEPORT)
1288                 print("routing to a teleporter\n");
1289         else
1290                 print("routing to a non-teleporter\n");
1291 */
1292
1293         return CMD_STATUS_EXECUTING;
1294 }
1295
1296 float havocbot_resetgoal(entity this)
1297 {
1298         navigation_clearroute(this);
1299         return CMD_STATUS_FINISHED;
1300 }
1301
1302 void havocbot_setupbot(entity this)
1303 {
1304         this.bot_ai = havocbot_ai;
1305         this.cmd_moveto = havocbot_moveto;
1306         this.cmd_resetgoal = havocbot_resetgoal;
1307
1308         havocbot_chooserole(this);
1309 }
1310
1311 vector havocbot_dodge(entity this)
1312 {
1313         // LordHavoc: disabled because this is too expensive
1314         return '0 0 0';
1315 #if 0
1316         entity head;
1317         vector dodge, v, n;
1318         float danger, bestdanger, vl, d;
1319         dodge = '0 0 0';
1320         bestdanger = -20;
1321         // check for dangerous objects near bot or approaching bot
1322         head = findchainfloat(bot_dodge, true);
1323         while(head)
1324         {
1325                 if (head.owner != this)
1326                 {
1327                         vl = vlen(head.velocity);
1328                         if (vl > autocvar_sv_maxspeed * 0.3)
1329                         {
1330                                 n = normalize(head.velocity);
1331                                 v = this.origin - head.origin;
1332                                 d = v * n;
1333                                 if (d > (0 - head.bot_dodgerating))
1334                                 if (d < (vl * 0.2 + head.bot_dodgerating))
1335                                 {
1336                                         // calculate direction and distance from the flight path, by removing the forward axis
1337                                         v = v - (n * (v * n));
1338                                         danger = head.bot_dodgerating - vlen(v);
1339                                         if (bestdanger < danger)
1340                                         {
1341                                                 bestdanger = danger;
1342                                                 // dodge to the side of the object
1343                                                 dodge = normalize(v);
1344                                         }
1345                                 }
1346                         }
1347                         else
1348                         {
1349                                 danger = head.bot_dodgerating - vlen(head.origin - this.origin);
1350                                 if (bestdanger < danger)
1351                                 {
1352                                         bestdanger = danger;
1353                                         dodge = normalize(this.origin - head.origin);
1354                                 }
1355                         }
1356                 }
1357                 head = head.chain;
1358         }
1359         return dodge;
1360 #endif
1361 }