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