Merge branch 'master' into Mario/vehicles
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / vehicles / unit / spiderbot.qc
1 #ifdef REGISTER_VEHICLE
2 REGISTER_VEHICLE(
3 /* VEH_##id   */ SPIDERBOT,
4 /* function   */ v_spiderbot,
5 /* spawnflags */ VHF_DMGSHAKE,
6 /* mins,maxs  */ '-75 -75 10', '75 75 125',
7 /* model          */ "models/vehicles/spiderbot.dpm",
8 /* head_model */ "models/vehicles/spiderbot_top.dpm",
9 /* hud_model  */ "models/vehicles/spiderbot_cockpit.dpm",
10 /* tags           */ "tag_head", "tag_hud", "",
11 /* netname        */ "spiderbot",
12 /* fullname   */ _("Spiderbot")
13 );
14 #else
15
16 const int SBRM_FIRST = 1;
17 const int SBRM_VOLLY = 1;
18 const int SBRM_GUIDE = 2;
19 const int SBRM_ARTILLERY = 3;
20 const int SBRM_LAST = 3;
21
22 #ifdef SVQC
23 bool autocvar_g_vehicle_spiderbot;
24
25 float autocvar_g_vehicle_spiderbot_respawntime;
26
27 float autocvar_g_vehicle_spiderbot_speed_stop;
28 float autocvar_g_vehicle_spiderbot_speed_strafe;
29 float autocvar_g_vehicle_spiderbot_speed_walk;
30 float autocvar_g_vehicle_spiderbot_speed_run = 700;
31 float autocvar_g_vehicle_spiderbot_turnspeed;
32 float autocvar_g_vehicle_spiderbot_turnspeed_strafe;
33 float autocvar_g_vehicle_spiderbot_movement_inertia;
34
35 float autocvar_g_vehicle_spiderbot_springlength;
36 float autocvar_g_vehicle_spiderbot_springup;
37 float autocvar_g_vehicle_spiderbot_springblend;
38 float autocvar_g_vehicle_spiderbot_tiltlimit;
39
40 float autocvar_g_vehicle_spiderbot_head_pitchlimit_down;
41 float autocvar_g_vehicle_spiderbot_head_pitchlimit_up;
42 float autocvar_g_vehicle_spiderbot_head_turnlimit;
43 float autocvar_g_vehicle_spiderbot_head_turnspeed;
44
45 int autocvar_g_vehicle_spiderbot_health;
46 float autocvar_g_vehicle_spiderbot_health_regen;
47 float autocvar_g_vehicle_spiderbot_health_regen_pause;
48
49 int autocvar_g_vehicle_spiderbot_shield;
50 float autocvar_g_vehicle_spiderbot_shield_regen;
51 float autocvar_g_vehicle_spiderbot_shield_regen_pause;
52
53 float autocvar_g_vehicle_spiderbot_minigun_damage;
54 float autocvar_g_vehicle_spiderbot_minigun_refire;
55 float autocvar_g_vehicle_spiderbot_minigun_spread;
56 int autocvar_g_vehicle_spiderbot_minigun_ammo_cost;
57 int autocvar_g_vehicle_spiderbot_minigun_ammo_max;
58 int autocvar_g_vehicle_spiderbot_minigun_ammo_regen;
59 float autocvar_g_vehicle_spiderbot_minigun_ammo_regen_pause;
60 float autocvar_g_vehicle_spiderbot_minigun_force;
61 float autocvar_g_vehicle_spiderbot_minigun_solidpenetration;
62
63 float autocvar_g_vehicle_spiderbot_rocket_damage;
64 float autocvar_g_vehicle_spiderbot_rocket_force;
65 float autocvar_g_vehicle_spiderbot_rocket_radius;
66 float autocvar_g_vehicle_spiderbot_rocket_speed;
67 float autocvar_g_vehicle_spiderbot_rocket_spread;
68 float autocvar_g_vehicle_spiderbot_rocket_refire;
69 float autocvar_g_vehicle_spiderbot_rocket_refire2;
70 float autocvar_g_vehicle_spiderbot_rocket_reload;
71 float autocvar_g_vehicle_spiderbot_rocket_health;
72 float autocvar_g_vehicle_spiderbot_rocket_noise;
73 float autocvar_g_vehicle_spiderbot_rocket_turnrate;
74 float autocvar_g_vehicle_spiderbot_rocket_lifetime;
75
76 vector autocvar_g_vehicle_spiderbot_bouncepain;
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         vector v;
216         entity rocket = world;
217
218         if (self.wait != -10)
219         {
220                 if (self.owner.BUTTON_ATCK2 && self.vehicle_weapon2mode == SBRM_GUIDE)
221                 {
222                         if (self.wait == 1)
223                         if (self.tur_head.frame == 9 || self.tur_head.frame == 1)
224                         {
225                                 if(self.gun2.cnt < time && self.tur_head.frame == 9)
226                                         self.tur_head.frame = 1;
227
228                                 return;
229                         }
230                         self.wait = 1;
231                 }
232                 else
233                 {
234                         if(self.wait)
235                                 spiderbot_guide_release();
236
237                         self.wait = 0;
238                 }
239         }
240
241         if(self.gun2.cnt > time)
242                 return;
243
244         if (self.tur_head.frame >= 9)
245         {
246                 self.tur_head.frame = 1;
247                 self.wait = 0;
248         }
249
250         if(self.wait != -10)
251         if(!self.owner.BUTTON_ATCK2)
252                 return;
253                 
254         if(forbidWeaponUse(self.owner))
255                 return;
256
257         v = gettaginfo(self.tur_head,gettagindex(self.tur_head,"tag_fire"));
258
259         switch(self.vehicle_weapon2mode)
260         {
261                 case SBRM_VOLLY:
262                         rocket = vehicles_projectile("spiderbot_rocket_launch", "weapons/rocket_fire.wav",
263                                                                    v, normalize(randomvec() * autocvar_g_vehicle_spiderbot_rocket_spread + v_forward) * autocvar_g_vehicle_spiderbot_rocket_speed,
264                                                                    autocvar_g_vehicle_spiderbot_rocket_damage, autocvar_g_vehicle_spiderbot_rocket_radius, autocvar_g_vehicle_spiderbot_rocket_force, 1,
265                                                                    DEATH_VH_SPID_ROCKET, PROJECTILE_SPIDERROCKET, autocvar_g_vehicle_spiderbot_rocket_health, false, true, self.owner);
266                         crosshair_trace(self.owner);
267                         float _dist = (random() * autocvar_g_vehicle_spiderbot_rocket_radius) + vlen(v - trace_endpos);
268                         _dist -= (random() * autocvar_g_vehicle_spiderbot_rocket_radius) ;
269                         rocket.nextthink  = time + (_dist / autocvar_g_vehicle_spiderbot_rocket_speed);
270                         rocket.think     = vehicles_projectile_explode;
271
272                         if(self.owner.BUTTON_ATCK2 && self.tur_head.frame == 1)
273                                 self.wait = -10;
274                         break;
275                 case SBRM_GUIDE:
276                         rocket = vehicles_projectile("spiderbot_rocket_launch", "weapons/rocket_fire.wav",
277                                                                    v, normalize(v_forward) * autocvar_g_vehicle_spiderbot_rocket_speed,
278                                                                    autocvar_g_vehicle_spiderbot_rocket_damage, autocvar_g_vehicle_spiderbot_rocket_radius, autocvar_g_vehicle_spiderbot_rocket_force, 1,
279                                                                    DEATH_VH_SPID_ROCKET, PROJECTILE_SPIDERROCKET, autocvar_g_vehicle_spiderbot_rocket_health, false, false, self.owner);
280                         crosshair_trace(self.owner);
281                         rocket.pos1        = trace_endpos;
282                         rocket.nextthink  = time;
283                         rocket.think      = spiderbot_rocket_guided;
284
285
286                 break;
287                 case SBRM_ARTILLERY:
288                         rocket = vehicles_projectile("spiderbot_rocket_launch", "weapons/rocket_fire.wav",
289                                                                    v, normalize(v_forward) * autocvar_g_vehicle_spiderbot_rocket_speed,
290                                                                    autocvar_g_vehicle_spiderbot_rocket_damage, autocvar_g_vehicle_spiderbot_rocket_radius, autocvar_g_vehicle_spiderbot_rocket_force, 1,
291                                                                    DEATH_VH_SPID_ROCKET, PROJECTILE_SPIDERROCKET, autocvar_g_vehicle_spiderbot_rocket_health, false, true, self.owner);
292
293                         crosshair_trace(self.owner);
294
295                         rocket.pos1        = trace_endpos + randomvec() * (0.75 * autocvar_g_vehicle_spiderbot_rocket_radius);
296                         rocket.pos1_z      = trace_endpos_z;
297
298                         traceline(v, v + '0 0 1' * MAX_SHOT_DISTANCE, MOVE_WORLDONLY, self);
299                         float h1 = 0.75 * vlen(v - trace_endpos);
300
301                         //v = trace_endpos;
302                         traceline(v , rocket.pos1 + '0 0 1' * MAX_SHOT_DISTANCE, MOVE_WORLDONLY, self);
303                         float h2 = 0.75 * vlen(rocket.pos1 - v);
304
305                         rocket.velocity  = spiberbot_calcartillery(v, rocket.pos1, ((h1 < h2) ? h1 : h2));
306                         rocket.movetype  = MOVETYPE_TOSS;
307                         rocket.gravity   = 1;
308                         //rocket.think   = spiderbot_rocket_artillery;
309                 break;
310         }
311         rocket.classname  = "spiderbot_rocket";
312
313         rocket.cnt = time + autocvar_g_vehicle_spiderbot_rocket_lifetime;
314
315         self.tur_head.frame += 1;
316         if (self.tur_head.frame == 9)
317                 self.attack_finished_single = autocvar_g_vehicle_spiderbot_rocket_reload;
318         else
319                 self.attack_finished_single = ((self.vehicle_weapon2mode ==  SBRM_VOLLY) ? autocvar_g_vehicle_spiderbot_rocket_refire2 : autocvar_g_vehicle_spiderbot_rocket_refire);
320
321         self.gun2.cnt = time + self.attack_finished_single;
322 }
323
324 .float jump_delay;
325 float spiderbot_frame()
326 {
327         vector ad, vf;
328         entity player, spider;
329         float ftmp;
330
331         if(intermission_running)
332         {
333                 self.vehicle.velocity = '0 0 0';
334                 self.vehicle.avelocity = '0 0 0';
335                 return 1;
336         }
337
338         player = self;
339         spider = self.vehicle;
340         self   = spider;
341
342         vehicles_painframe();
343
344         player.BUTTON_ZOOM        = 0;
345         player.BUTTON_CROUCH    = 0;
346         player.switchweapon      = 0;
347         player.vehicle_weapon2mode = spider.vehicle_weapon2mode;
348
349
350 #if 1 // 0 to enable per-gun impact aux crosshairs
351         // Avarage gun impact point's -> aux cross
352         ad = gettaginfo(spider.tur_head, gettagindex(spider.tur_head, "tag_hardpoint01"));
353         vf = v_forward;
354         ad += gettaginfo(spider.tur_head, gettagindex(spider.tur_head, "tag_hardpoint02"));
355         vf += v_forward;
356         ad = ad * 0.5;
357         v_forward = vf * 0.5;
358         traceline(ad, ad + v_forward * MAX_SHOT_DISTANCE, MOVE_NORMAL, spider);
359         UpdateAuxiliaryXhair(player, trace_endpos, ('1 0 0' * player.vehicle_reload1) + ('0 1 0' * (1 - player.vehicle_reload1)), 0);
360 #else
361         ad = gettaginfo(spider.gun1, gettagindex(spider.gun1, "barrels"));
362         traceline(ad, ad + v_forward * MAX_SHOT_DISTANCE, MOVE_NORMAL, spider);
363         UpdateAuxiliaryXhair(player, trace_endpos, ('1 0 0' * player.vehicle_reload1) + ('0 1 0' * (1 - player.vehicle_reload1)), 0);
364         vf = ad;
365         ad = gettaginfo(spider.gun2, gettagindex(spider.gun2, "barrels"));
366         traceline(ad, ad + v_forward * MAX_SHOT_DISTANCE, MOVE_NORMAL, spider);
367         UpdateAuxiliaryXhair(player, trace_endpos, ('1 0 0' * player.vehicle_reload1) + ('0 1 0' * (1 - player.vehicle_reload1)), 1);
368         ad = 0.5 * (ad + vf);
369 #endif
370
371         crosshair_trace(player);
372         ad = vectoangles(normalize(trace_endpos - ad));
373         ad = AnglesTransform_ToAngles(AnglesTransform_LeftDivide(AnglesTransform_FromAngles(spider.angles), AnglesTransform_FromAngles(ad))) - spider.tur_head.angles;
374         ad = AnglesTransform_Normalize(ad, true);
375         //UpdateAuxiliaryXhair(player, trace_endpos, ('1 0 0' * player.vehicle_reload2) + ('0 1 0' * (1 - player.vehicle_reload2)), 2);
376
377         // Rotate head
378         ftmp = autocvar_g_vehicle_spiderbot_head_turnspeed * sys_frametime;
379         ad_y = bound(-ftmp, ad_y, ftmp);
380         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);
381
382         // Pitch head
383         ad_x = bound(ftmp * -1, ad_x, ftmp);
384         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);
385
386
387         //fixedmakevectors(spider.angles);
388         makevectors(spider.angles + '-2 0 0' * spider.angles_x);
389
390         movelib_groundalign4point(autocvar_g_vehicle_spiderbot_springlength, autocvar_g_vehicle_spiderbot_springup, autocvar_g_vehicle_spiderbot_springblend, autocvar_g_vehicle_spiderbot_tiltlimit);
391
392         if(spider.flags & FL_ONGROUND)
393                 spider.jump_delay = time; // reset now so movement can begin
394
395         //if(spider.flags & FL_ONGROUND)
396         {
397                 if(spider.flags & FL_ONGROUND)
398                 if(spider.frame == 4 && self.tur_head.wait != 0)
399                 {
400                         sound (self, CH_TRIGGER_SINGLE, "vehicles/spiderbot_land.wav", VOL_VEHICLEENGINE, ATTEN_NORM);
401                         spider.frame = 5;
402                 }
403
404                 if(!player.BUTTON_JUMP)
405                         spider.BUTTON_JUMP = 0;
406
407                 if((spider.flags & FL_ONGROUND) && player.BUTTON_JUMP && !spider.BUTTON_JUMP && self.tur_head.wait < time)
408                 {
409                         sound (self, CH_TRIGGER_SINGLE, "vehicles/spiderbot_jump.wav", VOL_VEHICLEENGINE, ATTEN_NORM);
410                         //dprint("spiderbot_jump:", ftos(soundlength("vehicles/spiderbot_jump.wav")), "\n");
411                         self.delay = 0;
412
413                         self.tur_head.wait = time + 2;
414                         spider.jump_delay = time + 2;
415                         spider.BUTTON_JUMP = 1; // set spider's jump
416                         //player.BUTTON_JUMP = 0;
417
418                         vector movefix = '0 0 0';
419                         if(player.movement_x > 0) movefix_x = 1;
420                         if(player.movement_x < 0) movefix_x = -1;
421                         if(player.movement_y > 0) movefix_y = 1;
422                         if(player.movement_y < 0) movefix_y = -1;
423
424                         vector rt = movefix_y * v_right;
425                         vector sd = movefix_x * v_forward;
426                         if(movefix_y == 0 && movefix_x == 0)
427                                 sd = v_forward; // always do forward
428
429                         spider.flags &= ~FL_ONGROUND;
430
431                         spider.velocity = sd * 700 + rt * 600 + v_up * 600;
432                         spider.frame = 4;
433                 }
434                 else if(time >= spider.jump_delay)
435                 {
436                         if(vlen(player.movement) == 0)
437                         {
438                                 if(spider.flags & FL_ONGROUND)
439                                 {
440                                         if(self.sound_nexttime < time || self.delay != 3)
441                                         {
442                                                 self.delay = 3;
443                                                 self.sound_nexttime = time + 6.486500; //soundlength("vehicles/spiderbot_idle.wav");
444                                                 //dprint("spiderbot_idle:", ftos(soundlength("vehicles/spiderbot_idle.wav")), "\n");
445                                                 sound (self, CH_TRIGGER_SINGLE, "vehicles/spiderbot_idle.wav", VOL_VEHICLEENGINE, ATTEN_NORM);
446                                         }
447                                         movelib_beak_simple(autocvar_g_vehicle_spiderbot_speed_stop);
448                                         spider.frame = 5;
449                                 }
450                         }
451                         else
452                         {
453                                 // Turn Body
454                                 if(player.movement_x == 0 && player.movement_y != 0)
455                                         ftmp = autocvar_g_vehicle_spiderbot_turnspeed_strafe * sys_frametime;
456                                 else
457                                         ftmp = autocvar_g_vehicle_spiderbot_turnspeed * sys_frametime;
458
459                                 ftmp = bound(-ftmp, spider.tur_head.angles_y, ftmp);
460                                 spider.angles_y = anglemods(spider.angles_y + ftmp);
461                                 spider.tur_head.angles_y -= ftmp;
462
463                                 if(player.movement_x != 0)
464                                 {
465                                         if(player.movement_x > 0)
466                                         {
467                                                 player.movement_x = 1;
468                                                 if(spider.flags & FL_ONGROUND)
469                                                         spider.frame = 0;
470                                         }
471                                         else if(player.movement_x < 0)
472                                         {
473                                                 player.movement_x = -1;
474                                                 if(spider.flags & FL_ONGROUND)
475                                                         spider.frame = 1;
476                                         }
477                                         player.movement_y = 0;
478                                         float oldvelz = spider.velocity_z;
479                                         movelib_move_simple(normalize(v_forward * player.movement_x),((player.BUTTON_JUMP) ? autocvar_g_vehicle_spiderbot_speed_run : autocvar_g_vehicle_spiderbot_speed_walk),autocvar_g_vehicle_spiderbot_movement_inertia);
480                                         spider.velocity_z = oldvelz;
481                                         float g = ((autocvar_sv_gameplayfix_gravityunaffectedbyticrate) ? 0.5 : 1);
482                                         if(spider.velocity_z <= 20) // not while jumping
483                                                 spider.velocity_z -= g * sys_frametime * autocvar_sv_gravity;
484                                         if(spider.flags & FL_ONGROUND)
485                                         if(self.sound_nexttime < time || self.delay != 1)
486                                         {
487                                                 self.delay = 1;
488                                                 self.sound_nexttime = time + 6.486500; //soundlength("vehicles/spiderbot_walk.wav");
489                                                 sound (self, CH_TRIGGER_SINGLE, "vehicles/spiderbot_walk.wav", VOL_VEHICLEENGINE, ATTEN_NORM);
490                                                 //dprint("spiderbot_walk:", ftos(soundlength("vehicles/spiderbot_walk.wav")), "\n");
491                                         }
492                                 }
493                                 else if(player.movement_y != 0)
494                                 {
495                                         if(player.movement_y < 0)
496                                         {
497                                                 player.movement_y = -1;
498                                                 if(spider.flags & FL_ONGROUND)
499                                                         spider.frame = 2;
500                                         }
501                                         else if(player.movement_y > 0)
502                                         {
503                                                 player.movement_y = 1;
504                                                 if(spider.flags & FL_ONGROUND)
505                                                         spider.frame = 3;
506                                         }
507
508                                         float oldvelz = spider.velocity_z;
509                                         movelib_move_simple(normalize(v_right * player.movement_y),autocvar_g_vehicle_spiderbot_speed_strafe,autocvar_g_vehicle_spiderbot_movement_inertia);
510                                         spider.velocity_z = oldvelz;
511                                         float g = ((autocvar_sv_gameplayfix_gravityunaffectedbyticrate) ? 0.5 : 1);
512                                         if(spider.velocity_z <= 20) // not while jumping
513                                                 spider.velocity_z -= g * sys_frametime * autocvar_sv_gravity;
514                                         if(spider.flags & FL_ONGROUND)
515                                         if(self.sound_nexttime < time || self.delay != 2)
516                                         {
517                                                 self.delay = 2;
518                                                 self.sound_nexttime = time + 6.486500; //soundlength("vehicles/spiderbot_strafe.wav");
519                                                 sound (self, CH_TRIGGER_SINGLE, "vehicles/spiderbot_strafe.wav", VOL_VEHICLEENGINE, ATTEN_NORM);
520                                                 //dprint("spiderbot_strafe:", ftos(soundlength("vehicles/spiderbot_strafe.wav")), "\n");
521                                         }
522                                 }
523                         }
524                 }
525         }
526
527         self.angles_x = bound(-autocvar_g_vehicle_spiderbot_tiltlimit, self.angles_x, autocvar_g_vehicle_spiderbot_tiltlimit);
528         self.angles_z = bound(-autocvar_g_vehicle_spiderbot_tiltlimit, self.angles_z, autocvar_g_vehicle_spiderbot_tiltlimit);
529
530         if(!forbidWeaponUse(player))
531         if(player.BUTTON_ATCK)
532         {
533                 spider.cnt = time;
534                 if(spider.vehicle_ammo1 >= autocvar_g_vehicle_spiderbot_minigun_ammo_cost && spider.tur_head.attack_finished_single <= time)
535                 {
536                         entity gun;
537                         vector v;
538                         spider.misc_bulletcounter += 1;
539
540                         self = player;
541
542                         gun = (spider.misc_bulletcounter % 2) ? spider.gun1 : spider.gun2;
543
544                         v = gettaginfo(gun, gettagindex(gun, "barrels"));
545                         v_forward = normalize(v_forward);
546                         v += v_forward * 50;
547
548                         fireBullet(v, v_forward, autocvar_g_vehicle_spiderbot_minigun_spread, autocvar_g_vehicle_spiderbot_minigun_solidpenetration,
549                                 autocvar_g_vehicle_spiderbot_minigun_damage, autocvar_g_vehicle_spiderbot_minigun_force, DEATH_VH_SPID_MINIGUN, 0);
550
551                         sound (gun, CH_WEAPON_A, "weapons/uzi_fire.wav", VOL_BASE, ATTEN_NORM);
552                         //trailparticles(self, particleeffectnum("spiderbot_minigun_trail"), v, trace_endpos);
553                         pointparticles(particleeffectnum("spiderbot_minigun_muzzleflash"), v, v_forward * 2500, 1);
554
555                         self = spider;
556
557                         spider.vehicle_ammo1 -= autocvar_g_vehicle_spiderbot_minigun_ammo_cost;
558                         spider.tur_head.attack_finished_single = time + autocvar_g_vehicle_spiderbot_minigun_refire;
559                         player.vehicle_ammo1 = (spider.vehicle_ammo1 / autocvar_g_vehicle_spiderbot_minigun_ammo_max) * 100;
560                         spider.gun1.angles_z += 45;
561                         spider.gun2.angles_z -= 45;
562                         if(spider.gun1.angles_z >= 360)
563                         {
564                                 spider.gun1.angles_z = 0;
565                                 spider.gun2.angles_z = 0;
566                         }
567                 }
568         }
569         else
570                 vehicles_regen(spider.cnt, vehicle_ammo1, autocvar_g_vehicle_spiderbot_minigun_ammo_max,
571                                                                                    autocvar_g_vehicle_spiderbot_minigun_ammo_regen_pause,
572                                                                                    autocvar_g_vehicle_spiderbot_minigun_ammo_regen, frametime, false);
573
574
575         spiderbot_rocket_do();
576
577         if(self.vehicle_flags  & VHF_SHIELDREGEN)
578                 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);
579
580         if(self.vehicle_flags  & VHF_HEALTHREGEN)
581                 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);
582
583         player.BUTTON_ATCK = player.BUTTON_ATCK2 = 0;
584         //player.vehicle_ammo2 = spider.tur_head.frame;
585         player.vehicle_ammo2 = (9 - spider.tur_head.frame) / 8 * 100; // Percentage, like ammo1
586
587         if(spider.gun2.cnt <= time)
588                 player.vehicle_reload2 = 100;
589         else
590                 player.vehicle_reload2 = 100 - ((spider.gun2.cnt - time) / spider.attack_finished_single) * 100;
591
592         setorigin(player, spider.origin + '0 0 1' * spider.maxs_z);
593         player.velocity = spider.velocity;
594
595         VEHICLE_UPDATE_PLAYER(player, health, spiderbot);
596
597         if(self.vehicle_flags & VHF_HASSHIELD)
598                 VEHICLE_UPDATE_PLAYER(player, shield, spiderbot);
599
600         self = player;
601         return 1;
602 }
603
604 void spiderbot_exit(float eject)
605 {
606         entity e;
607         vector spot;
608
609         e = findchain(classname,"spiderbot_rocket");
610         while(e)
611         {
612                 if(e.owner == self.owner)
613                 {
614                         e.realowner = self.owner;
615                         e.owner = world;
616                 }
617                 e = e.chain;
618         }
619
620         self.think = vehicles_think;
621         self.nextthink = time;
622         self.frame = 5;
623         self.movetype = MOVETYPE_WALK;
624
625         if(!self.owner)
626                 return;
627
628         makevectors(self.angles);
629         if(eject)
630         {
631                 spot = self.origin + v_forward * 100 + '0 0 64';
632                 spot = vehicles_findgoodexit(spot);
633                 setorigin(self.owner , spot);
634                 self.owner.velocity = (v_up + v_forward * 0.25) * 750;
635                 self.owner.oldvelocity = self.owner.velocity;
636         }
637         else
638         {
639                 if(vlen(self.velocity) > autocvar_g_vehicle_spiderbot_speed_strafe)
640                 {
641                         self.owner.velocity = normalize(self.velocity) * vlen(self.velocity);
642                         self.owner.velocity_z += 200;
643                         spot = self.origin + v_forward * 128 + '0 0 64';
644                         spot = vehicles_findgoodexit(spot);
645                 }
646                 else
647                 {
648                         self.owner.velocity = self.velocity * 0.5;
649                         self.owner.velocity_z += 10;
650                         spot = self.origin + v_forward * 256 + '0 0 64';
651                         spot = vehicles_findgoodexit(spot);
652                 }
653                 self.owner.oldvelocity = self.owner.velocity;
654                 setorigin(self.owner , spot);
655         }
656
657         antilag_clear(self.owner);
658         self.owner = world;
659 }
660
661 void spiderbot_headfade()
662 {
663         self.think = spiderbot_headfade;
664         self.nextthink = self.fade_time;
665         self.alpha = 1 - (time - self.fade_time) * self.fade_rate;
666
667         if(self.cnt < time || self.alpha < 0.1)
668         {
669                 if(self.alpha > 0.1)
670                 {
671                         sound (self, CH_SHOTS, "weapons/rocket_impact.wav", VOL_BASE, ATTEN_NORM);
672                         Send_Effect("explosion_big", self.origin + '0 0 100', '0 0 0', 1);
673                 }
674                 remove(self);
675         }
676 }
677
678 void spiderbot_blowup()
679 {
680         if(self.cnt > time)
681         {
682                 if(random() < 0.1)
683                 {
684                         sound (self, CH_SHOTS, "weapons/rocket_impact.wav", VOL_BASE, ATTEN_NORM);
685                         Send_Effect("explosion_small", randomvec() * 80 + (self.origin + '0 0 100'), '0 0 0', 1);
686                 }
687                 self.nextthink = time + 0.1;
688                 return;
689         }
690
691         entity h, g1, g2, b;
692         b = spawn();
693         h = spawn();
694         g1 = spawn();
695         g2 = spawn();
696
697         setmodel(b, "models/vehicles/spiderbot.dpm");
698         setmodel(h, "models/vehicles/spiderbot_top.dpm");
699         setmodel(g1, "models/vehicles/spiderbot_barrels.dpm");
700         setmodel(g2, "models/vehicles/spiderbot_barrels.dpm");
701
702         setorigin(b, self.origin);
703         b.frame = 11;
704         b.angles = self.angles;
705         setsize(b, self.mins, self.maxs);
706
707         setorigin(h, gettaginfo(self, gettagindex(self, "tag_head")));
708         h.movetype = MOVETYPE_BOUNCE;
709         h.solid = SOLID_BBOX;
710         h.velocity = v_up * (500 + random() * 500) + randomvec() * 128;
711         h.modelflags = MF_ROCKET;
712         h.effects = EF_FLAME | EF_LOWPRECISION;
713         h.avelocity = randomvec() * 360;
714
715         h.alpha = 1;
716         h.cnt = time + (3.5 * random());
717         h.fade_rate = 1 / min(self.respawntime, 10);
718         h.fade_time = time;
719         h.think = spiderbot_headfade;
720         h.nextthink = time;
721
722         setorigin(g1, gettaginfo(self.tur_head, gettagindex(self.tur_head, "tag_hardpoint01")));
723         g1.movetype = MOVETYPE_TOSS;
724         g1.solid = SOLID_CORPSE;
725         g1.velocity = v_forward * 700 + (randomvec() * 32);
726         g1.avelocity = randomvec() * 180;
727
728         setorigin(g2, gettaginfo(self.tur_head, gettagindex(self.tur_head, "tag_hardpoint02")));
729         g2.movetype = MOVETYPE_TOSS;
730         g2.solid = SOLID_CORPSE;
731         g2.velocity = v_forward * 700 + (randomvec() * 32);
732         g2.avelocity = randomvec() * 180;
733
734         h.colormod = b.colormod = g1.colormod = g2.colormod = '-2 -2 -2';
735
736         SUB_SetFade(b,  time + 5, min(self.respawntime, 1));
737         //SUB_SetFade(h,  time, min(self.respawntime, 10));
738         SUB_SetFade(g1, time, min(self.respawntime, 10));
739         SUB_SetFade(g2, time, min(self.respawntime, 10));
740
741         RadiusDamage (self, self.enemy, 250, 15, 250, world, world, 250, DEATH_VH_SPID_DEATH, world);
742
743         self.alpha = self.tur_head.alpha = self.gun1.alpha = self.gun2.alpha = -1;
744         self.movetype = MOVETYPE_NONE;
745         self.deadflag = DEAD_DEAD;
746         self.solid = SOLID_NOT;
747         self.tur_head.effects &= ~EF_FLAME;
748         self.vehicle_hudmodel.viewmodelforclient = self;
749 }
750
751 bool spiderbot_impulse(int _imp)
752 {
753         switch(_imp)
754         {
755                 case 1:
756                 case 230:
757                         self.vehicle.vehicle_weapon2mode = SBRM_VOLLY;
758                         CSQCVehicleSetup(self, 0);
759                         return true;
760                 case 2:
761                 case 231:
762                         self.vehicle.vehicle_weapon2mode = SBRM_GUIDE;
763                         CSQCVehicleSetup(self, 0);
764                         return true;
765                 case 3:
766                 case 232:
767                 case 251:
768                         self.vehicle.vehicle_weapon2mode = SBRM_ARTILLERY;
769                         CSQCVehicleSetup(self, 0);
770                         return true;
771
772                 case 10:
773                 case 15:
774                 case 18:
775                         self.vehicle.vehicle_weapon2mode += 1;
776                         if(self.vehicle.vehicle_weapon2mode > SBRM_LAST)
777                                 self.vehicle.vehicle_weapon2mode = SBRM_FIRST;
778
779                         //centerprint(self, strcat("Rocket mode is ", ftos(self.vehicle.vehicle_weapon2mode)));
780                         CSQCVehicleSetup(self, 0);
781                         return true;
782                 case 11:
783                 case 12:
784                 case 16:
785                 case 19:
786                         self.vehicle.vehicle_weapon2mode -= 1;
787                         if(self.vehicle.vehicle_weapon2mode < SBRM_FIRST)
788                                 self.vehicle.vehicle_weapon2mode = SBRM_LAST;
789
790                         //centerprint(self, strcat("Rocket mode is ", ftos(self.vehicle.vehicle_weapon2mode)));
791                         CSQCVehicleSetup(self, 0);
792                         return true;
793
794                 /*
795                 case 17: // toss gun, could be used to exit?
796                         break;
797                 case 20: // Manual minigun reload?
798                         break;
799                 */
800         }
801         return false;
802 }
803
804 void spawnfunc_vehicle_spiderbot()
805 {
806         if(!autocvar_g_vehicle_spiderbot) { remove(self); return; }
807         if(!vehicle_initialize(VEH_SPIDERBOT, false)) { remove(self); return; }
808 }
809
810 float v_spiderbot(float req)
811 {
812         switch(req)
813         {
814                 case VR_IMPACT:
815                 {
816                         if(autocvar_g_vehicle_spiderbot_bouncepain)
817                                 vehicles_impact(autocvar_g_vehicle_spiderbot_bouncepain_x, autocvar_g_vehicle_spiderbot_bouncepain_y, autocvar_g_vehicle_spiderbot_bouncepain_z);
818                 
819                         return true;
820                 }
821                 case VR_ENTER:
822                 {
823                         self.vehicle_weapon2mode = SBRM_GUIDE;
824                         self.movetype = MOVETYPE_WALK;
825                         CSQCVehicleSetup(self.owner, 0);
826                         self.owner.vehicle_health = (self.vehicle_health / autocvar_g_vehicle_spiderbot_health) * 100;
827                         self.owner.vehicle_shield = (self.vehicle_shield / autocvar_g_vehicle_spiderbot_shield) * 100;
828
829                         if(self.owner.flagcarried)
830                         {
831                                 setattachment(self.owner.flagcarried, self.tur_head, "");
832                                 setorigin(self.owner.flagcarried, '-20 0 120');
833                         }
834                 
835                         return true;
836                 }
837                 case VR_THINK:
838                 {
839                         if(self.flags & FL_ONGROUND)
840                                 movelib_beak_simple(autocvar_g_vehicle_spiderbot_speed_stop);
841                         
842                         return true;
843                 }
844                 case VR_DEATH:
845                 {
846                         self.health                             = 0;
847                         self.event_damage               = func_null;
848                         self.takedamage                 = DAMAGE_NO;
849                         self.touch                              = func_null;
850                         self.cnt                                = 3.4 + time + random() * 2;
851                         self.think                              = spiderbot_blowup;
852                         self.nextthink                  = time;
853                         self.deadflag                   = DEAD_DYING;
854                         self.frame                              = 5;
855                         self.tur_head.effects  |= EF_FLAME;
856                         self.colormod                   = self.tur_head.colormod = '-1 -1 -1';
857                         self.frame                              = 10;
858                         self.movetype                   = MOVETYPE_TOSS;
859                         
860                         CSQCModel_UnlinkEntity(); // networking the death scene would be a nightmare
861
862                         return true;
863                 }
864                 case VR_SPAWN:
865                 {
866                         if(!self.gun1)
867                         {
868                                 self.vehicles_impulse = spiderbot_impulse;
869                                 self.gun1 = spawn();
870                                 self.gun2 = spawn();
871                                 setmodel(self.gun1, "models/vehicles/spiderbot_barrels.dpm");
872                                 setmodel(self.gun2, "models/vehicles/spiderbot_barrels.dpm");
873                                 setattachment(self.gun1, self.tur_head, "tag_hardpoint01");
874                                 setattachment(self.gun2, self.tur_head, "tag_hardpoint02");
875                                 self.gravity = 2;
876                                 self.mass = 5000;
877                         }
878
879                         self.frame = 5;
880                         self.tur_head.frame = 1;
881                         self.movetype = MOVETYPE_WALK;
882                         self.solid = SOLID_SLIDEBOX;
883                         self.alpha = self.tur_head.alpha = self.gun1.alpha = self.gun2.alpha = 1;
884                         self.tur_head.angles = '0 0 0';
885                         self.vehicle_exit = spiderbot_exit;
886
887                         setorigin(self, self.pos1 + '0 0 128');
888                         self.angles = self.pos2;
889                         self.damageforcescale = 0.03;
890                         self.vehicle_health = autocvar_g_vehicle_spiderbot_health;
891                         self.vehicle_shield = autocvar_g_vehicle_spiderbot_shield;
892                         
893                         self.PlayerPhysplug = spiderbot_frame;
894
895                         return true;
896                 }
897                 case VR_SETUP:
898                 {
899                         if(autocvar_g_vehicle_spiderbot_shield)
900                                 self.vehicle_flags |= VHF_HASSHIELD;
901
902                         if(autocvar_g_vehicle_spiderbot_shield_regen)
903                                 self.vehicle_flags |= VHF_SHIELDREGEN;
904
905                         if(autocvar_g_vehicle_spiderbot_health_regen)
906                                 self.vehicle_flags |= VHF_HEALTHREGEN;
907
908                         self.respawntime = autocvar_g_vehicle_spiderbot_respawntime;
909                         self.vehicle_health = autocvar_g_vehicle_spiderbot_health;
910                         self.vehicle_shield = autocvar_g_vehicle_spiderbot_shield;
911                         self.max_health = self.vehicle_health;
912                         self.pushable = true; // spiderbot can use jumppads
913
914                         return true;
915                 }
916                 case VR_PRECACHE:
917                 {
918                         precache_model ("models/vhshield.md3");
919                         precache_model ("models/vehicles/spiderbot.dpm");
920                         precache_model ("models/vehicles/spiderbot_top.dpm");
921                         precache_model ("models/vehicles/spiderbot_barrels.dpm");
922                         precache_model ("models/vehicles/spiderbot_cockpit.dpm");
923                         precache_model ( "models/uziflash.md3");
924
925                         precache_sound ("weapons/uzi_fire.wav" );
926                         precache_sound ("weapons/rocket_impact.wav");
927
928                         precache_sound ("vehicles/spiderbot_die.wav");
929                         precache_sound ("vehicles/spiderbot_idle.wav");
930                         precache_sound ("vehicles/spiderbot_jump.wav");
931                         precache_sound ("vehicles/spiderbot_strafe.wav");
932                         precache_sound ("vehicles/spiderbot_walk.wav");
933                         precache_sound ("vehicles/spiderbot_land.wav");
934                         return true;
935                 }
936         }
937
938         return true;
939 }
940
941 #endif // SVQC
942 #ifdef CSQC
943 float autocvar_cl_vehicle_spiderbot_cross_alpha = 0.6;
944 float autocvar_cl_vehicle_spiderbot_cross_size = 1;
945
946 float v_spiderbot(float req)
947 {
948         switch(req)
949         {
950                 case VR_HUD:
951                 {
952                         string crosshair;
953
954                         switch(weapon2mode)
955                         {
956                                 case SBRM_VOLLY:     crosshair = vCROSS_BURST; break;
957                                 case SBRM_GUIDE:     crosshair = vCROSS_GUIDE; break;
958                                 case SBRM_ARTILLERY: crosshair = vCROSS_RAIN;  break;
959                                 default:             crosshair = vCROSS_BURST;
960                         }
961
962                         Vehicles_drawHUD("vehicle_spider", "vehicle_spider_weapon1", "vehicle_spider_weapon2",
963                                                          "vehicle_icon_ammo1", autocvar_hud_progressbar_vehicles_ammo1_color,
964                                                          "vehicle_icon_ammo2", autocvar_hud_progressbar_vehicles_ammo2_color,
965                                                          crosshair);
966                         return true;
967                 }
968                 case VR_SETUP:
969                 {
970                         AuxiliaryXhair[0].axh_image = vCROSS_HINT; // Minigun1
971                         AuxiliaryXhair[1].axh_image = vCROSS_HINT; // Minigun2
972                 
973                         return true;
974                 }
975                 case VR_PRECACHE:
976                 {
977                         return true;
978                 }
979         }
980
981         return true;
982 }
983
984 #endif // CSQC
985 #endif // REGISTER_VEHICLE