a73997fda5be350ca8469496fcd56033691ae9f7
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / vehicles / spiderbot.qc
1 const vector SPIDERBOT_MIN = '-75 -75 10';
2 const vector SPIDERBOT_MAX  = '75 75 125';
3
4 #ifdef SVQC
5 float autocvar_g_vehicle_spiderbot;
6
7 float autocvar_g_vehicle_spiderbot_respawntime;
8
9 float autocvar_g_vehicle_spiderbot_speed_stop;
10 float autocvar_g_vehicle_spiderbot_speed_strafe;
11 float autocvar_g_vehicle_spiderbot_speed_walk;
12 float autocvar_g_vehicle_spiderbot_turnspeed;
13 float autocvar_g_vehicle_spiderbot_turnspeed_strafe;
14 float autocvar_g_vehicle_spiderbot_movement_inertia;
15
16 float autocvar_g_vehicle_spiderbot_springlength;
17 float autocvar_g_vehicle_spiderbot_springup;
18 float autocvar_g_vehicle_spiderbot_springblend;
19 float autocvar_g_vehicle_spiderbot_tiltlimit;
20
21 float autocvar_g_vehicle_spiderbot_head_pitchlimit_down;
22 float autocvar_g_vehicle_spiderbot_head_pitchlimit_up;
23 float autocvar_g_vehicle_spiderbot_head_turnlimit;
24 float autocvar_g_vehicle_spiderbot_head_turnspeed;
25
26 //float autocvar_g_vehicle_spiderbot_energy;
27 //float autocvar_g_vehicle_spiderbot_energy_regen;
28 //float autocvar_g_vehicle_spiderbot_energy_regen_pause;
29
30 float autocvar_g_vehicle_spiderbot_health;
31 float autocvar_g_vehicle_spiderbot_health_regen;
32 float autocvar_g_vehicle_spiderbot_health_regen_pause;
33
34 float autocvar_g_vehicle_spiderbot_shield;
35 float autocvar_g_vehicle_spiderbot_shield_regen;
36 float autocvar_g_vehicle_spiderbot_shield_regen_pause;
37
38 float autocvar_g_vehicle_spiderbot_minigun_damage;
39 float autocvar_g_vehicle_spiderbot_minigun_refire;
40 float autocvar_g_vehicle_spiderbot_minigun_spread;
41 float autocvar_g_vehicle_spiderbot_minigun_ammo_cost;
42 float autocvar_g_vehicle_spiderbot_minigun_ammo_max;
43 float autocvar_g_vehicle_spiderbot_minigun_ammo_regen;
44 float autocvar_g_vehicle_spiderbot_minigun_ammo_regen_pause;
45 float autocvar_g_vehicle_spiderbot_minigun_force;
46 float autocvar_g_vehicle_spiderbot_minigun_solidpenetration;
47
48 float autocvar_g_vehicle_spiderbot_rocket_damage;
49 float autocvar_g_vehicle_spiderbot_rocket_force;
50 float autocvar_g_vehicle_spiderbot_rocket_radius;
51 float autocvar_g_vehicle_spiderbot_rocket_speed;
52 float autocvar_g_vehicle_spiderbot_rocket_spread;
53 float autocvar_g_vehicle_spiderbot_rocket_refire;
54 float autocvar_g_vehicle_spiderbot_rocket_refire2;
55 float autocvar_g_vehicle_spiderbot_rocket_reload;
56 float autocvar_g_vehicle_spiderbot_rocket_health;
57 float autocvar_g_vehicle_spiderbot_rocket_noise;
58 float autocvar_g_vehicle_spiderbot_rocket_turnrate;
59 float autocvar_g_vehicle_spiderbot_rocket_lifetime;
60
61 vector autocvar_g_vehicle_spiderbot_bouncepain;
62
63
64 void spiderbot_exit(float eject);
65 void spiderbot_enter();
66 void spiderbot_spawn(float);
67 #define SBRM_FIRST 0
68 #define SBRM_VOLLY 0
69 #define SBRM_GUIDE 1
70 #define SBRM_ARTILLERY 2
71 #define SBRM_LAST 2
72
73 void spiderbot_rocket_artillery()
74 {
75     self.nextthink  = time;
76     UpdateCSQCProjectile(self);
77 }
78
79 void spiderbot_rocket_unguided()
80 {
81     vector newdir, olddir;
82
83     self.nextthink  = time;
84
85     olddir = normalize(self.velocity);
86     newdir = normalize(self.pos1 - self.origin) + randomvec() * autocvar_g_vehicle_spiderbot_rocket_noise;
87     self.velocity = normalize(olddir + newdir * autocvar_g_vehicle_spiderbot_rocket_turnrate) * autocvar_g_vehicle_spiderbot_rocket_speed;
88
89     UpdateCSQCProjectile(self);
90
91     if (self.owner.deadflag != DEAD_NO || self.cnt < time || vlen(self.pos1 - self.origin) < 16)
92         self.use();
93 }
94
95 void spiderbot_rocket_guided()
96 {
97     vector newdir, olddir;
98
99     self.nextthink  = time;
100
101     if (!self.realowner.vehicle)
102         self.think = spiderbot_rocket_unguided;
103
104     crosshair_trace(self.realowner);
105     olddir = normalize(self.velocity);
106     newdir = normalize(trace_endpos - self.origin) + randomvec() * autocvar_g_vehicle_spiderbot_rocket_noise;
107     self.velocity = normalize(olddir + newdir * autocvar_g_vehicle_spiderbot_rocket_turnrate) * autocvar_g_vehicle_spiderbot_rocket_speed;
108
109     UpdateCSQCProjectile(self);
110
111     if (self.owner.deadflag != DEAD_NO || self.cnt < time)
112         self.use();
113 }
114
115 void spiderbot_guide_release()
116 {
117     entity rkt;
118     rkt = findchainentity(realowner, self.owner);
119     if (!rkt)
120         return;
121
122     crosshair_trace(self.owner);
123     while(rkt)
124     {
125         if(rkt.think == spiderbot_rocket_guided)
126         {
127             rkt.pos1 = trace_endpos;
128             rkt.think = spiderbot_rocket_unguided;
129         }
130         rkt = rkt.chain;
131     }
132 }
133
134 float spiberbot_calcartillery_flighttime;
135 vector spiberbot_calcartillery(vector org, vector tgt, float ht)
136 {
137         float grav, sdist, zdist, vs, vz, jumpheight;
138         vector sdir;
139
140         grav  = autocvar_sv_gravity;
141         zdist = tgt_z - org_z;
142         sdist = vlen(tgt - org - zdist * '0 0 1');
143         sdir  = normalize(tgt - org - zdist * '0 0 1');
144
145         // how high do we need to go?
146         jumpheight = fabs(ht);
147         if(zdist > 0)
148                 jumpheight = jumpheight + zdist;
149
150         // push so high...
151         vz = sqrt(2 * grav * jumpheight); // NOTE: sqrt(positive)!
152
153         // we start with downwards velocity only if it's a downjump and the jump apex should be outside the jump!
154         if(ht < 0)
155                 if(zdist < 0)
156                         vz = -vz;
157
158         vector solution;
159         solution = solve_quadratic(0.5 * grav, -vz, zdist); // equation "z(ti) = zdist"
160         // ALWAYS solvable because jumpheight >= zdist
161         if(!solution_z)
162                 solution_y = solution_x; // just in case it is not solvable due to roundoff errors, assume two equal solutions at their center (this is mainly for the usual case with ht == 0)
163         if(zdist == 0)
164                 solution_x = solution_y; // solution_x is 0 in this case, so don't use it, but rather use solution_y (which will be sqrt(0.5 * jumpheight / grav), actually)
165
166         if(zdist < 0)
167         {
168                 // down-jump
169                 if(ht < 0)
170                 {
171                         // almost straight line type
172                         // jump apex is before the jump
173                         // we must take the larger one
174                         spiberbot_calcartillery_flighttime = solution_y;
175                 }
176                 else
177                 {
178                         // regular jump
179                         // jump apex is during the jump
180                         // we must take the larger one too
181                         spiberbot_calcartillery_flighttime = solution_y;
182                 }
183         }
184         else
185         {
186                 // up-jump
187                 if(ht < 0)
188                 {
189                         // almost straight line type
190                         // jump apex is after the jump
191                         // we must take the smaller one
192                         spiberbot_calcartillery_flighttime = solution_x;
193                 }
194                 else
195                 {
196                         // regular jump
197                         // jump apex is during the jump
198                         // we must take the larger one
199                         spiberbot_calcartillery_flighttime = solution_y;
200                 }
201         }
202         vs = sdist / spiberbot_calcartillery_flighttime;
203
204         // finally calculate the velocity
205         return sdir * vs + '0 0 1' * vz;
206 }
207
208 void spiderbot_rocket_do()
209 {
210
211     vector v;
212     entity rocket = world;
213
214     if (self.wait != -10)
215     {
216         if (self.owner.BUTTON_ATCK2 && self.vehicle_weapon2mode == SBRM_GUIDE)
217         {
218             if (self.wait == 1)
219             if (self.tur_head.frame == 9 || self.tur_head.frame == 1)
220             {
221                 if(self.gun2.cnt < time && self.tur_head.frame == 9)
222                     self.tur_head.frame = 1;
223
224                 return;
225             }
226             self.wait = 1;
227         }
228         else
229         {
230             if(self.wait)
231                 spiderbot_guide_release();
232
233             self.wait = 0;
234         }
235     }
236
237     if(self.gun2.cnt > time)
238         return;
239
240     if (self.tur_head.frame >= 9)
241     {
242         self.tur_head.frame = 1;
243         self.wait = 0;
244     }
245
246     if (self.wait != -10)
247         if (!self.owner.BUTTON_ATCK2)
248             return;
249
250
251     v = gettaginfo(self.tur_head,gettagindex(self.tur_head,"tag_fire"));
252
253     switch(self.vehicle_weapon2mode)
254     {
255         case SBRM_VOLLY:
256             rocket = vehicles_projectile("spiderbot_rocket_launch", "weapons/rocket_fire.wav",
257                                    v, normalize(randomvec() * autocvar_g_vehicle_spiderbot_rocket_spread + v_forward) * autocvar_g_vehicle_spiderbot_rocket_speed,
258                                    autocvar_g_vehicle_spiderbot_rocket_damage, autocvar_g_vehicle_spiderbot_rocket_radius, autocvar_g_vehicle_spiderbot_rocket_force, 1,
259                                    DEATH_VH_SPID_ROCKET, PROJECTILE_SPIDERROCKET, autocvar_g_vehicle_spiderbot_rocket_health, FALSE, TRUE, self.owner);
260             crosshair_trace(self.owner);
261             float _dist = (random() * autocvar_g_vehicle_spiderbot_rocket_radius) + vlen(v - trace_endpos);
262             _dist -= (random() * autocvar_g_vehicle_spiderbot_rocket_radius) ;
263             rocket.nextthink  = time + (_dist / autocvar_g_vehicle_spiderbot_rocket_speed);
264             rocket.think     = vehicles_projectile_explode;
265
266             if(self.owner.BUTTON_ATCK2 && self.tur_head.frame == 1)
267                 self.wait = -10;
268             break;
269         case SBRM_GUIDE:
270             rocket = vehicles_projectile("spiderbot_rocket_launch", "weapons/rocket_fire.wav",
271                                    v, normalize(v_forward) * autocvar_g_vehicle_spiderbot_rocket_speed,
272                                    autocvar_g_vehicle_spiderbot_rocket_damage, autocvar_g_vehicle_spiderbot_rocket_radius, autocvar_g_vehicle_spiderbot_rocket_force, 1,
273                                    DEATH_VH_SPID_ROCKET, PROJECTILE_SPIDERROCKET, autocvar_g_vehicle_spiderbot_rocket_health, FALSE, FALSE, self.owner);
274             crosshair_trace(self.owner);
275             rocket.pos1       = trace_endpos;
276             rocket.nextthink  = time;
277             rocket.think      = spiderbot_rocket_guided;
278
279
280         break;
281         case SBRM_ARTILLERY:
282             rocket = vehicles_projectile("spiderbot_rocket_launch", "weapons/rocket_fire.wav",
283                                    v, normalize(v_forward) * autocvar_g_vehicle_spiderbot_rocket_speed,
284                                    autocvar_g_vehicle_spiderbot_rocket_damage, autocvar_g_vehicle_spiderbot_rocket_radius, autocvar_g_vehicle_spiderbot_rocket_force, 1,
285                                    DEATH_VH_SPID_ROCKET, PROJECTILE_SPIDERROCKET, autocvar_g_vehicle_spiderbot_rocket_health, FALSE, TRUE, self.owner);
286
287             crosshair_trace(self.owner);
288
289             rocket.pos1       = trace_endpos + randomvec() * (0.75 * autocvar_g_vehicle_spiderbot_rocket_radius);
290             rocket.pos1_z       = trace_endpos_z;
291
292             traceline(v, v + '0 0 1' * MAX_SHOT_DISTANCE, MOVE_WORLDONLY, self);
293             float h1 = 0.75 * vlen(v - trace_endpos);
294
295             //v = trace_endpos;
296             traceline(v , rocket.pos1 + '0 0 1' * MAX_SHOT_DISTANCE, MOVE_WORLDONLY, self);
297             float h2 = 0.75 * vlen(rocket.pos1 - v);
298
299             rocket.velocity  = spiberbot_calcartillery(v, rocket.pos1, ((h1 < h2) ? h1 : h2));
300             rocket.movetype  = MOVETYPE_TOSS;
301             rocket.gravity   = 1;
302             //rocket.think     = spiderbot_rocket_artillery;
303         break;
304     }
305     rocket.classname  = "spiderbot_rocket";
306
307     rocket.cnt = time + autocvar_g_vehicle_spiderbot_rocket_lifetime;
308
309     self.tur_head.frame += 1;
310     if (self.tur_head.frame == 9)
311         self.attack_finished_single = autocvar_g_vehicle_spiderbot_rocket_reload;
312     else
313         self.attack_finished_single = ((self.vehicle_weapon2mode ==  SBRM_VOLLY) ? autocvar_g_vehicle_spiderbot_rocket_refire2 : autocvar_g_vehicle_spiderbot_rocket_refire);
314
315     self.gun2.cnt = time + self.attack_finished_single;
316 }
317
318 float spiderbot_aiframe()
319 {
320     return FALSE;
321 }
322
323 float spiderbot_frame()
324 {
325     vector ad, vf;
326     entity player, spider;
327     float ftmp;
328
329         if(intermission_running)
330                 return 1;
331
332     player = self;
333     spider = self.vehicle;
334     self   = spider;
335
336     vehicles_painframe();
337
338     player.BUTTON_ZOOM      = 0;
339     player.BUTTON_CROUCH    = 0;
340     player.switchweapon     = 0;
341
342
343 #if 1 // 0 to enable per-gun impact aux crosshairs
344     // Avarage gun impact point's -> aux cross
345     ad = gettaginfo(spider.tur_head, gettagindex(spider.tur_head, "tag_hardpoint01"));
346     vf = v_forward;
347     ad += gettaginfo(spider.tur_head, gettagindex(spider.tur_head, "tag_hardpoint02"));
348     vf += v_forward;
349     ad = ad * 0.5;
350     v_forward = vf * 0.5;
351     traceline(ad, ad + v_forward * MAX_SHOT_DISTANCE, MOVE_NORMAL, spider);
352     UpdateAuxiliaryXhair(player, trace_endpos, ('1 0 0' * player.vehicle_reload1) + ('0 1 0' * (1 - player.vehicle_reload1)), 0);
353 #else
354     ad = gettaginfo(spider.gun1, gettagindex(spider.gun1, "barrels"));
355     traceline(ad, ad + v_forward * MAX_SHOT_DISTANCE, MOVE_NORMAL, spider);
356     UpdateAuxiliaryXhair(player, trace_endpos, ('1 0 0' * player.vehicle_reload1) + ('0 1 0' * (1 - player.vehicle_reload1)), 0);
357     vf = ad;
358     ad = gettaginfo(spider.gun2, gettagindex(spider.gun2, "barrels"));
359     traceline(ad, ad + v_forward * MAX_SHOT_DISTANCE, MOVE_NORMAL, spider);
360     UpdateAuxiliaryXhair(player, trace_endpos, ('1 0 0' * player.vehicle_reload1) + ('0 1 0' * (1 - player.vehicle_reload1)), 1);
361     ad = 0.5 * (ad + vf);
362 #endif
363
364     crosshair_trace(player);
365     ad = vectoangles(normalize(trace_endpos - ad));
366     ad = AnglesTransform_ToAngles(AnglesTransform_LeftDivide(AnglesTransform_FromAngles(spider.angles), AnglesTransform_FromAngles(ad))) - spider.tur_head.angles;
367     ad = AnglesTransform_Normalize(ad, TRUE);
368     //UpdateAuxiliaryXhair(player, trace_endpos, ('1 0 0' * player.vehicle_reload2) + ('0 1 0' * (1 - player.vehicle_reload2)), 2);
369
370     // Rotate head
371     ftmp = autocvar_g_vehicle_spiderbot_head_turnspeed * sys_frametime;
372     ad_y = bound(-ftmp, ad_y, ftmp);
373     spider.tur_head.angles_y = bound(autocvar_g_vehicle_spiderbot_head_turnlimit * -1, spider.tur_head.angles_y + ad_y, autocvar_g_vehicle_spiderbot_head_turnlimit);
374
375     // Pitch head
376     ad_x = bound(ftmp * -1, ad_x, ftmp);
377     spider.tur_head.angles_x = bound(autocvar_g_vehicle_spiderbot_head_pitchlimit_down, spider.tur_head.angles_x + ad_x, autocvar_g_vehicle_spiderbot_head_pitchlimit_up);
378
379
380     //fixedmakevectors(spider.angles);
381     makevectors(spider.angles + '-2 0 0' * spider.angles_x);
382
383     movelib_groundalign4point(autocvar_g_vehicle_spiderbot_springlength, autocvar_g_vehicle_spiderbot_springup, autocvar_g_vehicle_spiderbot_springblend, autocvar_g_vehicle_spiderbot_tiltlimit);
384
385     if(spider.flags & FL_ONGROUND)
386     {
387         if(spider.frame == 4 && self.tur_head.wait != 0)
388         {
389             sound (self, CH_TRIGGER_SINGLE, "vehicles/spiderbot_land.wav", VOL_VEHICLEENGINE, ATTEN_NORM);
390             spider.frame = 5;
391         }
392
393         if(player.BUTTON_JUMP && self.tur_head.wait < time)
394         {
395             sound (self, CH_TRIGGER_SINGLE, "vehicles/spiderbot_jump.wav", VOL_VEHICLEENGINE, ATTEN_NORM);
396             //dprint("spiderbot_jump:", ftos(soundlength("vehicles/spiderbot_jump.wav")), "\n");
397             self.delay = 0;
398
399             self.tur_head.wait = time + 2;
400             player.BUTTON_JUMP = 0;
401             spider.velocity   = v_forward * 700 + v_up * 600;
402             spider.frame = 4;
403         }
404         else
405         {
406             if(vlen(player.movement) == 0)
407             {
408                 if(self.sound_nexttime < time || self.delay != 3)
409                 {
410                     self.delay = 3;
411                     self.sound_nexttime = time + 6.486500; //soundlength("vehicles/spiderbot_idle.wav");
412                     //dprint("spiderbot_idle:", ftos(soundlength("vehicles/spiderbot_idle.wav")), "\n");
413                     sound (self, CH_TRIGGER_SINGLE, "vehicles/spiderbot_idle.wav", VOL_VEHICLEENGINE, ATTEN_NORM);
414                 }
415                 movelib_beak_simple(autocvar_g_vehicle_spiderbot_speed_stop);
416                 spider.frame = 5;
417             }
418             else
419             {
420                 // Turn Body
421                 if(player.movement_x == 0 && player.movement_y != 0)
422                     ftmp = autocvar_g_vehicle_spiderbot_turnspeed_strafe * sys_frametime;
423                 else
424                     ftmp = autocvar_g_vehicle_spiderbot_turnspeed * sys_frametime;
425
426                 ftmp = bound(-ftmp, spider.tur_head.angles_y, ftmp);
427                 spider.angles_y = anglemods(spider.angles_y + ftmp);
428                 spider.tur_head.angles_y -= ftmp;
429
430                 if(player.movement_x != 0)
431                 {
432                     if(player.movement_x > 0)
433                     {
434                         player.movement_x = 1;
435                         spider.frame = 0;
436                     }
437                     else if(player.movement_x < 0)
438                     {
439                         player.movement_x = -1;
440                         spider.frame = 1;
441                     }
442                     player.movement_y = 0;
443                     movelib_move_simple(normalize(v_forward * player.movement_x),autocvar_g_vehicle_spiderbot_speed_walk,autocvar_g_vehicle_spiderbot_movement_inertia);
444
445                     if(self.sound_nexttime < time || self.delay != 1)
446                     {
447                         self.delay = 1;
448                         self.sound_nexttime = time + 6.486500; //soundlength("vehicles/spiderbot_walk.wav");
449                         sound (self, CH_TRIGGER_SINGLE, "vehicles/spiderbot_walk.wav", VOL_VEHICLEENGINE, ATTEN_NORM);
450                         //dprint("spiderbot_walk:", ftos(soundlength("vehicles/spiderbot_walk.wav")), "\n");
451                     }
452                 }
453                 else if(player.movement_y != 0)
454                 {
455                     if(player.movement_y < 0)
456                     {
457                         player.movement_y = -1;
458                         spider.frame = 2;
459                     }
460                     else if(player.movement_y > 0)
461                     {
462                         player.movement_y = 1;
463                         spider.frame = 3;
464                     }
465                     movelib_move_simple(normalize(v_right * player.movement_y),autocvar_g_vehicle_spiderbot_speed_strafe,autocvar_g_vehicle_spiderbot_movement_inertia);
466                     if(self.sound_nexttime < time || self.delay != 2)
467                     {
468                         self.delay = 2;
469                         self.sound_nexttime = time + 6.486500; //soundlength("vehicles/spiderbot_strafe.wav");
470                         sound (self, CH_TRIGGER_SINGLE, "vehicles/spiderbot_strafe.wav", VOL_VEHICLEENGINE, ATTEN_NORM);
471                         //dprint("spiderbot_strafe:", ftos(soundlength("vehicles/spiderbot_strafe.wav")), "\n");
472                     }
473                 }
474             }
475         }
476     }
477
478     self.angles_x = bound(-autocvar_g_vehicle_spiderbot_tiltlimit, self.angles_x, autocvar_g_vehicle_spiderbot_tiltlimit);
479     self.angles_z = bound(-autocvar_g_vehicle_spiderbot_tiltlimit, self.angles_z, autocvar_g_vehicle_spiderbot_tiltlimit);
480
481     if(player.BUTTON_ATCK)
482     {
483         spider.cnt = time;
484         if(spider.vehicle_ammo1 >= autocvar_g_vehicle_spiderbot_minigun_ammo_cost && spider.tur_head.attack_finished_single <= time)
485         {
486             entity gun;
487             vector v;
488             spider.misc_bulletcounter += 1;
489
490             self = player;
491
492             mod(spider.misc_bulletcounter, 2) ? gun = spider.gun1 : gun = spider.gun2;
493             v = gettaginfo(gun, gettagindex(gun, "barrels"));
494             v_forward = normalize(v_forward);
495             v += v_forward * 50;
496
497             fireBullet(v, v_forward, autocvar_g_vehicle_spiderbot_minigun_spread, autocvar_g_vehicle_spiderbot_minigun_solidpenetration,
498                                 autocvar_g_vehicle_spiderbot_minigun_damage, autocvar_g_vehicle_spiderbot_minigun_force, DEATH_VH_SPID_MINIGUN, 0);
499
500 //            fireBullet (v, v_forward, autocvar_g_vehicle_spiderbot_minigun_spread, autocvar_g_vehicle_spiderbot_minigun_damage,
501 //                autocvar_g_vehicle_spiderbot_minigun_spread, DEATH_VH_SPID_MINIGUN, 0);
502
503             sound (gun, CH_WEAPON_A, "weapons/uzi_fire.wav", VOL_BASE, ATTEN_NORM);
504             //trailparticles(self, particleeffectnum("spiderbot_minigun_trail"), v, trace_endpos);
505             pointparticles(particleeffectnum("spiderbot_minigun_muzzleflash"), v, v_forward * 2500, 1);
506
507             self = spider;
508
509             spider.vehicle_ammo1 -= autocvar_g_vehicle_spiderbot_minigun_ammo_cost;
510             spider.tur_head.attack_finished_single = time + autocvar_g_vehicle_spiderbot_minigun_refire;
511             player.vehicle_ammo1 = (spider.vehicle_ammo1 / autocvar_g_vehicle_spiderbot_minigun_ammo_max) * 100;
512             spider.gun1.angles_z += 45;
513             spider.gun2.angles_z -= 45;
514             if(spider.gun1.angles_z >= 360)
515             {
516                 spider.gun1.angles_z = 0;
517                 spider.gun2.angles_z = 0;
518             }
519         }
520     }
521     else
522         vehicles_regen(spider.cnt, vehicle_ammo1, autocvar_g_vehicle_spiderbot_minigun_ammo_max,
523                                            autocvar_g_vehicle_spiderbot_minigun_ammo_regen_pause,
524                                            autocvar_g_vehicle_spiderbot_minigun_ammo_regen, frametime, FALSE);
525
526
527     spiderbot_rocket_do();
528
529     if(self.vehicle_flags  & VHF_SHIELDREGEN)
530         vehicles_regen(spider.dmg_time, vehicle_shield, autocvar_g_vehicle_spiderbot_shield, autocvar_g_vehicle_spiderbot_shield_regen_pause, autocvar_g_vehicle_spiderbot_shield_regen, frametime, TRUE);
531
532     if(self.vehicle_flags  & VHF_HEALTHREGEN)
533         vehicles_regen(spider.dmg_time, vehicle_health, autocvar_g_vehicle_spiderbot_health, autocvar_g_vehicle_spiderbot_health_regen_pause, autocvar_g_vehicle_spiderbot_health_regen, frametime, FALSE);
534
535     player.BUTTON_ATCK = player.BUTTON_ATCK2 = 0;
536     player.vehicle_ammo2 = spider.tur_head.frame;
537
538     if(spider.gun2.cnt <= time)
539         player.vehicle_reload2 = 100;
540     else
541         player.vehicle_reload2 = 100 - ((spider.gun2.cnt - time) / spider.attack_finished_single) * 100;
542
543     setorigin(player, spider.origin + '0 0 1' * SPIDERBOT_MAX_z);
544     player.velocity = spider.velocity;
545
546     VEHICLE_UPDATE_PLAYER(player, health, spiderbot);
547
548     if(self.vehicle_flags & VHF_HASSHIELD)
549         VEHICLE_UPDATE_PLAYER(player, shield, spiderbot);
550
551     self = player;
552     return 1;
553 }
554 void spiderbot_think()
555 {
556     if(self.flags & FL_ONGROUND)
557         movelib_beak_simple(autocvar_g_vehicle_spiderbot_speed_stop);
558
559     self.nextthink = time;
560 }
561
562 void spiderbot_enter()
563 {
564     self.vehicle_weapon2mode = SBRM_GUIDE;
565     self.movetype   = MOVETYPE_WALK;
566     CSQCVehicleSetup(self.owner, 0);
567     self.owner.vehicle_health = (self.vehicle_health / autocvar_g_vehicle_spiderbot_health) * 100;
568     self.owner.vehicle_shield = (self.vehicle_shield / autocvar_g_vehicle_spiderbot_shield) * 100;
569
570     if(self.owner.flagcarried)
571     {
572         setattachment(self.owner.flagcarried, self.tur_head, "");
573         setorigin(self.owner.flagcarried, '-20 0 120');
574     }
575 }
576
577 void spiderbot_exit(float eject)
578 {
579     entity e;
580     vector spot;
581
582     e = findchain(classname,"spiderbot_rocket");
583     while(e)
584     {
585         if(e.owner == self.owner)
586         {
587             e.realowner = self.owner;
588             e.owner = world;
589         }
590         e = e.chain;
591     }
592
593     //self.velocity   = '0 0 0';
594     self.think      = spiderbot_think;
595     self.nextthink  = time;
596     self.frame      = 5;
597     self.movetype   = MOVETYPE_WALK;
598
599     if (!self.owner)
600         return;
601
602         makevectors(self.angles);
603         if(eject)
604         {
605             spot = self.origin + v_forward * 100 + '0 0 64';
606             spot = vehicles_findgoodexit(spot);
607             setorigin(self.owner , spot);
608             self.owner.velocity = (v_up + v_forward * 0.25) * 750;
609             self.owner.oldvelocity = self.owner.velocity;
610         }
611         else
612         {
613                 if(vlen(self.velocity) > autocvar_g_vehicle_spiderbot_speed_strafe)
614                 {
615                         self.owner.velocity = normalize(self.velocity) * vlen(self.velocity);
616                         self.owner.velocity_z += 200;
617                         spot = self.origin + v_forward * 128 + '0 0 64';
618                         spot = vehicles_findgoodexit(spot);
619                 }
620                 else
621                 {
622                         self.owner.velocity = self.velocity * 0.5;
623                         self.owner.velocity_z += 10;
624                         spot = self.origin + v_forward * 256 + '0 0 64';
625                         spot = vehicles_findgoodexit(spot);
626                 }
627             self.owner.oldvelocity = self.owner.velocity;
628             setorigin(self.owner , spot);
629         }
630
631         antilag_clear(self.owner);
632     self.owner = world;
633 }
634
635 void spider_impact()
636 {
637     if(autocvar_g_vehicle_spiderbot_bouncepain_x)
638         vehilces_impact(autocvar_g_vehicle_spiderbot_bouncepain_x, autocvar_g_vehicle_spiderbot_bouncepain_y, autocvar_g_vehicle_spiderbot_bouncepain_z);
639 }
640
641 void spiderbot_headfade()
642 {
643         self.think = spiderbot_headfade;
644         self.nextthink = self.fade_time;
645         self.alpha = 1 - (time - self.fade_time) * self.fade_rate;
646
647     if(self.cnt < time || self.alpha < 0.1)
648     {
649         if(self.alpha > 0.1)
650         {
651             sound (self, CH_SHOTS, "weapons/rocket_impact.wav", VOL_BASE, ATTEN_NORM);
652             pointparticles(particleeffectnum("explosion_big"), self.origin + '0 0 100', '0 0 0', 1);
653         }
654         remove(self);
655     }
656 }
657
658 void spiderbot_blowup()
659 {
660     if(self.cnt > time)
661     {
662         if(random() < 0.1)
663         {
664             sound (self, CH_SHOTS, "weapons/rocket_impact.wav", VOL_BASE, ATTEN_NORM);
665             pointparticles(particleeffectnum("explosion_small"), randomvec() * 80 + (self.origin + '0 0 100'), '0 0 0', 1);
666         }
667         self.nextthink = time + 0.1;
668         return;
669     }
670
671     entity h, g1, g2, b;
672     b = spawn();
673     h = spawn();
674     g1 = spawn();
675     g2 = spawn();
676
677     setmodel(b,  "models/vehicles/spiderbot.dpm");
678     setmodel(h,  "models/vehicles/spiderbot_top.dpm");
679     setmodel(g1, "models/vehicles/spiderbot_barrels.dpm");
680     setmodel(g2, "models/vehicles/spiderbot_barrels.dpm");
681
682     setorigin(b, self.origin);
683     b.frame         = 11;
684     b.angles        = self.angles;
685     setsize(b, self.mins, self.maxs);
686
687     setorigin(h, gettaginfo(self, gettagindex(self, "tag_head")));
688     h.movetype      = MOVETYPE_BOUNCE;
689     h.solid         = SOLID_BBOX;
690     h.velocity      = v_up * (500 + random() * 500) + randomvec() * 128;
691     h.modelflags    = MF_ROCKET;
692     h.effects       = EF_FLAME | EF_LOWPRECISION;
693     h.avelocity     = randomvec() * 360;
694
695     h.alpha         = 1;
696     h.cnt           = time + (3.5 * random());
697     h.fade_rate     = 1 / min(autocvar_g_vehicle_spiderbot_respawntime, 10);
698     h.fade_time     = time;
699     h.think         = spiderbot_headfade;
700     h.nextthink     = time;
701
702     setorigin(g1, gettaginfo(self.tur_head, gettagindex(self.tur_head, "tag_hardpoint01")));
703     g1.movetype     = MOVETYPE_TOSS;
704     g1.solid        = SOLID_CORPSE;
705     g1.velocity     = v_forward * 700 + (randomvec() * 32);
706     g1.avelocity    = randomvec() * 180;
707
708     setorigin(g2, gettaginfo(self.tur_head, gettagindex(self.tur_head, "tag_hardpoint02")));
709     g2.movetype     = MOVETYPE_TOSS;
710     g2.solid        = SOLID_CORPSE;
711     g2.velocity     = v_forward * 700 + (randomvec() * 32);
712     g2.avelocity    = randomvec() * 180;
713
714     h.colormod = b.colormod = g1.colormod = g2.colormod = '-2 -2 -2';
715
716     SUB_SetFade(b,  time + 5, min(autocvar_g_vehicle_spiderbot_respawntime, 1));
717     //SUB_SetFade(h,  time, min(autocvar_g_vehicle_spiderbot_respawntime, 10));
718     SUB_SetFade(g1, time, min(autocvar_g_vehicle_spiderbot_respawntime, 10));
719     SUB_SetFade(g2, time, min(autocvar_g_vehicle_spiderbot_respawntime, 10));
720
721     RadiusDamage (self, self.enemy, 250, 15, 250, world, 250, DEATH_VH_SPID_DEATH, world);
722
723     self.alpha = self.tur_head.alpha = self.gun1.alpha = self.gun2.alpha = -1;
724     self.movetype   = MOVETYPE_NONE;
725     self.deadflag   = DEAD_DEAD;
726     self.solid      = SOLID_NOT;
727     self.tur_head.effects  &=  ~EF_FLAME;
728     self.vehicle_hudmodel.viewmodelforclient = self;
729 }
730
731 void spiderbot_die()
732 {
733     self.health             = 0;
734     self.event_damage       = func_null;
735     self.takedamage         = DAMAGE_NO;
736     self.touch              = func_null;
737     self.cnt                = 3.4 + time + random() * 2;
738     self.think              = spiderbot_blowup;
739     self.nextthink          = time;
740     self.deadflag           = DEAD_DYING;
741         self.frame              = 5;
742         self.tur_head.effects  |= EF_FLAME;
743         self.colormod           = self.tur_head.colormod = '-1 -1 -1';
744         self.frame              = 10;
745         self.movetype           = MOVETYPE_TOSS;
746 }
747
748 float spiderbot_impulse(float _imp)
749 {
750     switch(_imp)
751     {
752         case 10:
753         case 15:
754         case 18:
755             self.vehicle.vehicle_weapon2mode += 1;
756             if(self.vehicle.vehicle_weapon2mode > SBRM_LAST)
757                 self.vehicle.vehicle_weapon2mode = SBRM_FIRST;
758
759             //centerprint(self, strcat("Rocket mode is ", ftos(self.vehicle.vehicle_weapon2mode)));
760             CSQCVehicleSetup(self, 0);
761             return TRUE;
762         case 12:
763         case 16:
764         case 19:
765             self.vehicle.vehicle_weapon2mode -= 1;
766             if(self.vehicle.vehicle_weapon2mode < SBRM_FIRST)
767                 self.vehicle.vehicle_weapon2mode = SBRM_LAST;
768
769             //centerprint(self, strcat("Rocket mode is ", ftos(self.vehicle.vehicle_weapon2mode)));
770             CSQCVehicleSetup(self, 0);
771             return TRUE;
772
773         /*
774         case 17: // toss gun, could be used to exit?
775             break;
776         case 20: // Manual minigun reload?
777             break;
778         */
779     }
780     return FALSE;
781 }
782
783 void spiderbot_spawn(float _f)
784 {
785     if(!self.gun1)
786     {
787         self.vehicles_impusle   = spiderbot_impulse;
788         self.gun1               = spawn();
789         self.gun2               = spawn();
790         setmodel(self.gun1, "models/vehicles/spiderbot_barrels.dpm");
791         setmodel(self.gun2, "models/vehicles/spiderbot_barrels.dpm");
792         setattachment(self.gun1, self.tur_head, "tag_hardpoint01");
793         setattachment(self.gun2, self.tur_head, "tag_hardpoint02");
794         self.gravity            = 2;
795         self.mass               = 5000;
796     }
797
798     self.frame              = 5;
799     self.tur_head.frame     = 1;
800     self.think              = spiderbot_think;
801     self.nextthink          = time;
802     self.vehicle_health     = autocvar_g_vehicle_spiderbot_health;
803     self.vehicle_shield     = autocvar_g_vehicle_spiderbot_shield;
804     self.movetype           = MOVETYPE_WALK;
805     self.solid              = SOLID_SLIDEBOX;
806     self.alpha              = self.tur_head.alpha = self.gun1.alpha = self.gun2.alpha = 1;
807     self.tur_head.angles    = '0 0 0';
808
809     setorigin(self, self.pos1 + '0 0 128');
810     self.angles = self.pos2;
811     self.vehicle_impact = spider_impact;
812     self.damageforcescale = 0.03;
813 }
814
815 void spawnfunc_vehicle_spiderbot()
816 {
817     if(!autocvar_g_vehicle_spiderbot)
818     {
819         remove(self);
820         return;
821     }
822
823     self.vehicle_flags |= VHF_DMGSHAKE;
824     //self.vehicle_flags |= VHF_DMGROLL;
825     //self.vehicle_flags |= VHF_DMGHEADROLL;
826
827     precache_model ( "models/vhshield.md3");
828     precache_model ( "models/vehicles/spiderbot.dpm");
829     precache_model ( "models/vehicles/spiderbot_top.dpm");
830     precache_model ( "models/vehicles/spiderbot_barrels.dpm");
831     precache_model ( "models/vehicles/spiderbot_cockpit.dpm");
832     precache_model ( "models/uziflash.md3");
833
834     precache_sound ( "weapons/uzi_fire.wav" );
835     precache_sound ( "weapons/rocket_impact.wav");
836
837     precache_sound ( "vehicles/spiderbot_die.wav");
838     precache_sound ( "vehicles/spiderbot_idle.wav");
839     precache_sound ( "vehicles/spiderbot_jump.wav");
840     precache_sound ( "vehicles/spiderbot_strafe.wav");
841     precache_sound ( "vehicles/spiderbot_walk.wav");
842     precache_sound ( "vehicles/spiderbot_land.wav");
843
844     if(autocvar_g_vehicle_spiderbot_shield)
845         self.vehicle_flags |= VHF_HASSHIELD;
846
847     if(autocvar_g_vehicle_spiderbot_shield_regen)
848         self.vehicle_flags |= VHF_SHIELDREGEN;
849
850     if(autocvar_g_vehicle_spiderbot_health_regen)
851         self.vehicle_flags |= VHF_HEALTHREGEN;
852
853     if(!vehicle_initialize(
854              "Spiderbot",
855              "models/vehicles/spiderbot.dpm",
856              "models/vehicles/spiderbot_top.dpm",
857              "models/vehicles/spiderbot_cockpit.dpm",
858              "tag_head", "tag_hud", "",
859              HUD_SPIDERBOT,
860              SPIDERBOT_MIN, SPIDERBOT_MAX,
861              FALSE,
862              spiderbot_spawn, autocvar_g_vehicle_spiderbot_respawntime,
863              spiderbot_frame,
864              spiderbot_enter, spiderbot_exit,
865              spiderbot_die,   spiderbot_think,
866              FALSE,
867              autocvar_g_vehicle_spiderbot_health,
868              autocvar_g_vehicle_spiderbot_shield))
869     {
870         remove(self);
871         return;
872     }
873 }
874 #endif // SVQC