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