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