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