Added support for electro hitscan
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / w_electro.qc
1 #ifdef REGISTER_WEAPON
2 REGISTER_WEAPON(ELECTRO, w_electro, IT_CELLS, 5, WEP_FLAG_NORMAL | WEP_TYPE_SPLASH, BOT_PICKUP_RATING_MID, "electro", "electro", "Electro");
3 #else
4 #ifdef SVQC
5 .float electro_count;
6 .float electro_secondarytime;
7
8 void W_Plasma_Explode_Combo (void);
9
10 void W_Plasma_TriggerCombo(vector org, float rad, entity own)
11 {
12         local entity e;
13         e = WarpZone_FindRadius(org, rad, TRUE);
14         while (e)
15         {
16                 if (e.classname == "plasma")
17                 {
18                         // change owner to whoever caused the combo explosion
19                         e.owner = own;
20                         e.takedamage = DAMAGE_NO;
21                         e.classname = "plasma_chain";
22                         e.think = W_Plasma_Explode_Combo;
23                         e.nextthink = time + vlen(e.WarpZone_findradius_dist) / autocvar_g_balance_electro_combo_speed; // delay combo chains, looks cooler
24                 }
25                 e = e.chain;
26         }
27 }
28
29 void W_Plasma_Explode (void)
30 {
31         if(other.takedamage == DAMAGE_AIM)
32                 if(other.classname == "player")
33                         if(IsDifferentTeam(self.owner, other))
34                                 if(other.deadflag == DEAD_NO)
35                                         if(IsFlying(other))
36                                                 AnnounceTo(self.owner, "electrobitch");
37
38         self.event_damage = SUB_Null;
39         self.takedamage = DAMAGE_NO;
40         if (self.movetype == MOVETYPE_BOUNCE)
41         {
42                 RadiusDamage (self, self.owner, autocvar_g_balance_electro_secondary_damage, autocvar_g_balance_electro_secondary_edgedamage, autocvar_g_balance_electro_secondary_radius, world, autocvar_g_balance_electro_secondary_force, self.projectiledeathtype, other);
43         }
44         else
45         {
46                 W_Plasma_TriggerCombo(self.origin, autocvar_g_balance_electro_primary_comboradius, self.owner);
47                 RadiusDamage (self, self.owner, autocvar_g_balance_electro_primary_damage, autocvar_g_balance_electro_primary_edgedamage, autocvar_g_balance_electro_primary_radius, world, autocvar_g_balance_electro_primary_force, self.projectiledeathtype, other);
48         }
49
50         remove (self);
51 }
52
53 void W_Plasma_Explode_Combo (void)
54 {
55         W_Plasma_TriggerCombo(self.origin, autocvar_g_balance_electro_combo_comboradius, self.owner);
56
57         self.event_damage = SUB_Null;
58         RadiusDamage (self, self.owner, autocvar_g_balance_electro_combo_damage, autocvar_g_balance_electro_combo_edgedamage, autocvar_g_balance_electro_combo_radius, world, autocvar_g_balance_electro_combo_force, WEP_ELECTRO | HITTYPE_BOUNCE, world); // use THIS type for a combo because primary can't bounce
59         remove (self);
60 }
61
62 void W_Plasma_Touch (void)
63 {
64         //self.velocity = self.velocity  * 0.1;
65
66         PROJECTILE_TOUCH;
67         if (other.takedamage == DAMAGE_AIM) {
68                 W_Plasma_Explode ();
69         } else {
70                 //UpdateCSQCProjectile(self);
71                 spamsound (self, CHAN_PROJECTILE, "weapons/electro_bounce.wav", VOL_BASE, ATTN_NORM);
72                 self.projectiledeathtype |= HITTYPE_BOUNCE;
73         }
74 }
75
76 void W_Plasma_TouchExplode (void)
77 {
78         PROJECTILE_TOUCH;
79         W_Plasma_Explode ();
80 }
81
82 void W_Plasma_Damage (entity inflictor, entity attacker, float damage, float deathtype, vector hitloc, vector force)
83 {
84         if(self.health <= 0)
85                 return;
86         // note: combos are usually triggered by W_Plasma_TriggerCombo, not damage
87         self.health = self.health - damage;
88         if (self.health <= 0)
89         {
90                 self.takedamage = DAMAGE_NO;
91                 self.nextthink = time;
92                 if (inflictor.classname == "plasma_chain" || inflictor.classname == "plasma_prim")
93                 {
94                         // change owner to whoever caused the combo explosion
95                         self.owner = inflictor.owner;
96                         self.classname = "plasma_chain";
97                         self.think = W_Plasma_Explode_Combo;
98                         self.nextthink = time + min(autocvar_g_balance_electro_combo_radius, vlen(self.origin - inflictor.origin)) / autocvar_g_balance_electro_combo_speed; // delay combo chains, looks cooler
99                                 //                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ bounding the length, because inflictor may be in a galaxy far far away (warpzones)
100                 }
101                 else
102                 {
103                         self.use = W_Plasma_Explode;
104                         self.think = adaptor_think2use; // not _hittype_splash, as this runs "immediately"
105                 }
106         }
107 }
108
109 void W_Electro_Attack()
110 {
111         local entity proj;
112
113         W_SetupShot_ProjectileSize (self, '0 0 -3', '0 0 -3', FALSE, 2, "weapons/electro_fire.wav", CHAN_WEAPON, autocvar_g_balance_electro_primary_damage);
114
115         pointparticles(particleeffectnum("electro_muzzleflash"), w_shotorg, w_shotdir * 1000, 1);
116
117         proj = spawn ();
118         proj.classname = "plasma_prim";
119         proj.owner = self;
120         proj.bot_dodge = TRUE;
121         proj.bot_dodgerating = autocvar_g_balance_electro_primary_damage;
122         proj.use = W_Plasma_Explode;
123         proj.think = adaptor_think2use_hittype_splash;
124         proj.nextthink = time + autocvar_g_balance_electro_primary_lifetime;
125         PROJECTILE_MAKETRIGGER(proj);
126         proj.projectiledeathtype = WEP_ELECTRO;
127         setorigin(proj, w_shotorg);
128
129         if not(self.items & IT_UNLIMITED_WEAPON_AMMO)
130                 self.ammo_cells = self.ammo_cells - autocvar_g_balance_electro_primary_ammo;
131         proj.movetype = MOVETYPE_FLY;
132         W_SETUPPROJECTILEVELOCITY(proj, g_balance_electro_primary);
133         proj.angles = vectoangles(proj.velocity);
134         proj.touch = W_Plasma_TouchExplode;
135         setsize(proj, '0 0 -3', '0 0 -3');
136         proj.flags = FL_PROJECTILE;
137
138         //sound (proj, CHAN_PAIN, "weapons/electro_fly.wav", VOL_BASE, ATTN_NORM);
139         //sounds bad
140
141         CSQCProjectile(proj, TRUE, PROJECTILE_ELECTRO_BEAM, TRUE);
142
143         other = proj; MUTATOR_CALLHOOK(EditProjectile);
144 }
145
146 void W_Electro_Attack2()
147 {
148         local entity proj;
149
150         W_SetupShot_ProjectileSize (self, '0 0 -4', '0 0 -4', FALSE, 2, "weapons/electro_fire2.wav", CHAN_WEAPON, autocvar_g_balance_electro_secondary_damage);
151
152         w_shotdir = v_forward; // no TrueAim for grenades please
153
154         pointparticles(particleeffectnum("electro_muzzleflash"), w_shotorg, w_shotdir * 1000, 1);
155
156         proj = spawn ();
157         proj.classname = "plasma";
158         proj.owner = self;
159         proj.use = W_Plasma_Explode;
160         proj.think = adaptor_think2use_hittype_splash;
161         proj.bot_dodge = TRUE;
162         proj.bot_dodgerating = autocvar_g_balance_electro_secondary_damage;
163         proj.nextthink = time + autocvar_g_balance_electro_secondary_lifetime;
164         PROJECTILE_MAKETRIGGER(proj);
165         proj.projectiledeathtype = WEP_ELECTRO | HITTYPE_SECONDARY;
166         setorigin(proj, w_shotorg);
167
168         if not(self.items & IT_UNLIMITED_WEAPON_AMMO)
169                 self.ammo_cells = self.ammo_cells - autocvar_g_balance_electro_secondary_ammo;
170         //proj.glow_size = 50;
171         //proj.glow_color = 45;
172         proj.movetype = MOVETYPE_BOUNCE;
173         W_SETUPPROJECTILEVELOCITY_UP(proj, g_balance_electro_secondary);
174         proj.touch = W_Plasma_Touch;
175         setsize(proj, '0 0 -4', '0 0 -4');
176         proj.takedamage = DAMAGE_YES;
177         proj.damageforcescale = autocvar_g_balance_electro_secondary_damageforcescale;
178         proj.health = autocvar_g_balance_electro_secondary_health;
179         proj.event_damage = W_Plasma_Damage;
180         proj.flags = FL_PROJECTILE;
181
182         proj.bouncefactor = autocvar_g_balance_electro_secondary_bouncefactor;
183         proj.bouncestop = autocvar_g_balance_electro_secondary_bouncestop;
184
185 #if 0
186         entity p2;
187         p2 = spawn();
188         copyentity(proj, p2);
189         setmodel(p2, "models/ebomb.mdl");
190         setsize(p2, proj.mins, proj.maxs);
191 #endif
192
193         CSQCProjectile(proj, TRUE, PROJECTILE_ELECTRO, FALSE); // no culling, it has sound
194
195         other = proj; MUTATOR_CALLHOOK(EditProjectile);
196         }
197
198 .vector hook_start, hook_end;
199 float lgbeam_send(entity to, float sf)
200 {
201         WriteByte(MSG_ENTITY, ENT_CLIENT_LGBEAM);
202         sf = sf & 0x7F;
203         if(sound_allowed(MSG_BROADCAST, self.owner))
204                 sf |= 0x80;
205         WriteByte(MSG_ENTITY, sf);
206         if(sf & 1)
207         {
208                 WriteByte(MSG_ENTITY, num_for_edict(self.owner));
209                 WriteCoord(MSG_ENTITY, autocvar_g_balance_electro_primary_range);
210         }
211         if(sf & 2)
212         {
213                 WriteCoord(MSG_ENTITY, self.hook_start_x);
214                 WriteCoord(MSG_ENTITY, self.hook_start_y);
215                 WriteCoord(MSG_ENTITY, self.hook_start_z);
216         }
217         if(sf & 4)
218         {
219                 WriteCoord(MSG_ENTITY, self.hook_end_x);
220                 WriteCoord(MSG_ENTITY, self.hook_end_y);
221                 WriteCoord(MSG_ENTITY, self.hook_end_z);
222         }
223         return TRUE;
224 }
225 .entity lgbeam;
226 .float prevlgfire;
227 void lgbeam_think()
228 {
229         self.owner.prevlgfire = time;
230         if (self != self.owner.lgbeam)
231         {
232                 remove(self);
233                 return;
234         }
235         if (self.owner.weaponentity.state != WS_INUSE || (self.owner.ammo_cells <= 0 && !(self.owner.items & IT_UNLIMITED_WEAPON_AMMO)) || self.owner.deadflag != DEAD_NO || !self.owner.BUTTON_ATCK || (g_freezetag && self.owner.freezetag_frozen))
236         {
237                 if(self == self.owner.lgbeam)
238                         self.owner.lgbeam = world;
239                 remove(self);
240                 return;
241         }
242
243         self.nextthink = time;
244
245         makevectors(self.owner.v_angle);
246
247         float dt, f;
248         dt = frametime;
249         if not(self.owner.items & IT_UNLIMITED_WEAPON_AMMO)
250         {
251                 if(autocvar_g_balance_electro_primary_ammo)
252                 {
253                         dt = min(dt, self.owner.ammo_cells / autocvar_g_balance_electro_primary_ammo);
254                         self.owner.ammo_cells = max(0, self.owner.ammo_cells - autocvar_g_balance_electro_primary_ammo * frametime);
255                 }
256         }
257
258         W_SetupShot_Range(self.owner, TRUE, 0, "", 0, autocvar_g_balance_electro_primary_damage * dt, autocvar_g_balance_electro_primary_range);
259         WarpZone_traceline_antilag(self.owner, w_shotorg, w_shotend, MOVE_NORMAL, self.owner, ANTILAG_LATENCY(self.owner));
260
261         // apply the damage
262         if(trace_ent)
263         {
264                 vector force;
265                 force = w_shotdir * autocvar_g_balance_electro_primary_force + '0 0 1' * autocvar_g_balance_electro_primary_force_up;
266
267                 f = ExponentialFalloff(autocvar_g_balance_electro_primary_falloff_mindist, autocvar_g_balance_electro_primary_falloff_maxdist, autocvar_g_balance_electro_primary_falloff_halflifedist, vlen(WarpZone_UnTransformOrigin(WarpZone_trace_transform, trace_endpos) - w_shotorg));
268
269                 if(accuracy_isgooddamage(self.owner, trace_ent))
270                         accuracy_add(self.owner, WEP_ELECTRO, 0, autocvar_g_balance_electro_primary_damage * dt * f);
271                 Damage (trace_ent, self.owner, self.owner, autocvar_g_balance_electro_primary_damage * dt * f, WEP_ELECTRO, trace_endpos, force * dt);
272         }
273         W_Plasma_TriggerCombo(trace_endpos, autocvar_g_balance_electro_primary_comboradius, self.owner);
274
275         // draw effect
276         if(w_shotorg != self.hook_start)
277         {
278                 self.SendFlags |= 2;
279                 self.hook_start = w_shotorg;
280         }
281         if(w_shotend != self.hook_end)
282         {
283                 self.SendFlags |= 4;
284                 self.hook_end = w_shotend;
285         }
286 }
287
288 // experimental lightning gun
289 void W_Electro_Attack3 (void)
290 {
291         // only play fire sound if 0.5 sec has passed since player let go the fire button
292         if(time - self.prevlgfire > 0.5)
293                 sound (self, CHAN_WEAPON, "weapons/lgbeam_fire.wav", VOL_BASE, ATTN_NORM);
294
295         entity beam, oldself;
296
297         self.lgbeam = beam = spawn();
298         beam.solid = SOLID_NOT;
299         beam.think = lgbeam_think;
300         beam.owner = self;
301         beam.movetype = MOVETYPE_NONE;
302         beam.shot_spread = 0;
303         beam.bot_dodge = TRUE;
304         beam.bot_dodgerating = autocvar_g_balance_electro_primary_damage;
305         Net_LinkEntity(beam, FALSE, 0, lgbeam_send);
306
307         oldself = self;
308         self = beam;
309         self.think();
310         self = oldself;
311 }
312
313 void ElectroInit()
314 {
315         weapon_action(WEP_ELECTRO, WR_PRECACHE);
316         electro_shotorigin[0] = shotorg_adjust_values(CL_Weapon_GetShotOrg(WEP_ELECTRO), FALSE, FALSE, 1);
317         electro_shotorigin[1] = shotorg_adjust_values(CL_Weapon_GetShotOrg(WEP_ELECTRO), FALSE, FALSE, 2);
318         electro_shotorigin[2] = shotorg_adjust_values(CL_Weapon_GetShotOrg(WEP_ELECTRO), FALSE, FALSE, 3);
319         electro_shotorigin[3] = shotorg_adjust_values(CL_Weapon_GetShotOrg(WEP_ELECTRO), FALSE, FALSE, 4);
320 }
321
322 void spawnfunc_weapon_electro (void)
323 {
324         weapon_defaultspawnfunc(WEP_ELECTRO);
325 }
326
327 void w_electro_checkattack()
328 {
329         if(self.electro_count > 1)
330         if(self.BUTTON_ATCK2)
331         if(weapon_prepareattack(1, -1))
332         {
333                 W_Electro_Attack2();
334                 self.electro_count -= 1;
335                 weapon_thinkf(WFRAME_FIRE2, autocvar_g_balance_electro_secondary_animtime, w_electro_checkattack);
336                 return;
337         }
338
339         w_ready();
340 }
341
342 .float bot_secondary_electromooth;
343 .float BUTTON_ATCK_prev;
344 float w_electro(float req)
345 {
346         if (req == WR_AIM)
347         {
348                 self.BUTTON_ATCK=FALSE;
349                 self.BUTTON_ATCK2=FALSE;
350                 if(vlen(self.origin-self.enemy.origin) > 1000)
351                         self.bot_secondary_electromooth = 0;
352                 if(self.bot_secondary_electromooth == 0)
353                 {
354                         bool shoot;
355
356                         if(autocvar_g_balance_electro_primary_speed)
357                                 shoot = bot_aim(autocvar_g_balance_electro_primary_speed, 0, autocvar_g_balance_electro_primary_lifetime, FALSE);
358                         else
359                                 shoot = bot_aim(1000000, 0, 0.001, FALSE);
360
361                         if(shoot)
362                         {
363                                 self.BUTTON_ATCK = TRUE;
364                                 if(random() < 0.01) self.bot_secondary_electromooth = 1;
365                         }
366                 }
367                 else
368                 {
369                         if(bot_aim(autocvar_g_balance_electro_secondary_speed, autocvar_g_balance_grenadelauncher_secondary_speed_up, autocvar_g_balance_electro_secondary_lifetime, TRUE))
370                         {
371                                 self.BUTTON_ATCK2 = TRUE;
372                                 if(random() < 0.03) self.bot_secondary_electromooth = 0;
373                         }
374                 }
375         }
376         else if (req == WR_THINK)
377         {
378                 if (self.BUTTON_ATCK)
379                 {
380                         if (weapon_prepareattack(0, (autocvar_g_balance_electro_lightning ? 0 : autocvar_g_balance_electro_primary_refire)))
381                         {
382                                 if(autocvar_g_balance_electro_lightning)
383                                 {
384                                         if ((!self.lgbeam) || wasfreed(self.lgbeam))
385                                         {
386                                                 W_Electro_Attack3();
387                                         }
388                                         self.BUTTON_ATCK_prev = 1;
389                                 }
390                                 else
391                                 {
392                                         W_Electro_Attack();
393                                 }
394                                 weapon_thinkf(WFRAME_FIRE1, autocvar_g_balance_electro_primary_animtime, w_ready);
395                         }
396                 } else {
397                         if(autocvar_g_balance_electro_lightning)
398                         {
399                                 if (self.BUTTON_ATCK_prev != 0)
400                                 {
401                                         ATTACK_FINISHED(self) = time + autocvar_g_balance_electro_primary_refire * W_WeaponRateFactor();
402                                 }
403                                 self.BUTTON_ATCK_prev = 0;
404                         }
405                 }
406
407                 if (self.BUTTON_ATCK2)
408                 if (time >= self.electro_secondarytime)
409                 if (weapon_prepareattack(1, autocvar_g_balance_electro_secondary_refire))
410                 {
411                         W_Electro_Attack2();
412                         self.electro_count = autocvar_g_balance_electro_secondary_count;
413                         weapon_thinkf(WFRAME_FIRE2, autocvar_g_balance_electro_secondary_animtime, w_electro_checkattack);
414                         self.electro_secondarytime = time + autocvar_g_balance_electro_secondary_refire2;
415                 }
416         }
417         else if (req == WR_PRECACHE)
418         {
419                 precache_model ("models/weapons/g_electro.md3");
420                 precache_model ("models/weapons/v_electro.md3");
421                 precache_model ("models/weapons/h_electro.iqm");
422                 precache_sound ("weapons/electro_bounce.wav");
423                 precache_sound ("weapons/electro_fire.wav");
424                 precache_sound ("weapons/electro_fire2.wav");
425                 precache_sound ("weapons/electro_impact.wav");
426                 precache_sound ("weapons/electro_impact_combo.wav");
427                 if(autocvar_g_balance_electro_lightning)
428                 {
429                         precache_sound ("weapons/lgbeam_fire.wav");
430                 }
431         }
432         else if (req == WR_SETUP)
433                 weapon_setup(WEP_ELECTRO);
434         else if (req == WR_CHECKAMMO1)
435         {
436                 if(autocvar_g_balance_electro_lightning)
437                         return !autocvar_g_balance_electro_primary_ammo || (self.ammo_cells > 0);
438                 else
439                         return self.ammo_cells >= autocvar_g_balance_electro_primary_ammo;
440         }
441         else if (req == WR_CHECKAMMO2)
442                 return self.ammo_cells >= autocvar_g_balance_electro_secondary_ammo;
443         else if (req == WR_RESETPLAYER)
444         {
445                 self.electro_secondarytime = time;
446         }
447         return TRUE;
448 };
449 #endif
450 #ifdef CSQC
451 float w_electro(float req)
452 {
453         if(req == WR_IMPACTEFFECT)
454         {
455                 vector org2;
456                 org2 = w_org + w_backoff * 6;
457                 if(w_deathtype & HITTYPE_SECONDARY)
458                 {
459                         pointparticles(particleeffectnum("electro_ballexplode"), org2, '0 0 0', 1);
460                         if(!w_issilent)
461                                 sound(self, CHAN_PROJECTILE, "weapons/electro_impact.wav", VOL_BASE, ATTN_NORM);
462                 }
463                 else
464                 {
465                         if(w_deathtype & HITTYPE_BOUNCE)
466                         {
467                                 // this is sent as "primary (w_deathtype & HITTYPE_BOUNCE)" to distinguish it from (w_deathtype & HITTYPE_SECONDARY) bounced balls
468                                 pointparticles(particleeffectnum("electro_combo"), org2, '0 0 0', 1);
469                                 if(!w_issilent)
470                                         sound(self, CHAN_PROJECTILE, "weapons/electro_impact_combo.wav", VOL_BASE, ATTN_NORM);
471                         }
472                         else
473                         {
474                                 pointparticles(particleeffectnum("electro_impact"), org2, '0 0 0', 1);
475                                 if(!w_issilent)
476                                         sound(self, CHAN_PROJECTILE, "weapons/electro_impact.wav", VOL_BASE, ATTN_NORM);
477                         }
478                 }
479         }
480         else if(req == WR_PRECACHE)
481         {
482                 precache_sound("weapons/electro_impact.wav");
483                 precache_sound("weapons/electro_impact_combo.wav");
484         }
485         else if (req == WR_SUICIDEMESSAGE)
486         {
487                 if(w_deathtype & HITTYPE_SECONDARY)
488                         w_deathtypestring = "%s could not remember where they put plasma";
489                 else
490                         w_deathtypestring = "%s played with plasma";
491         }
492         else if (req == WR_KILLMESSAGE)
493         {
494                 if(w_deathtype & HITTYPE_SECONDARY)
495                 {
496                         if(w_deathtype & HITTYPE_SPLASH) // unchecked: BOUNCE
497                                 w_deathtypestring = "%s just noticed %s's blue ball";
498                         else // unchecked: BOUNCE
499                                 w_deathtypestring = "%s got in touch with %s's blue ball";
500                 }
501                 else
502                 {
503                         if(w_deathtype & HITTYPE_BOUNCE) // combo
504                                 w_deathtypestring = "%s felt the electrifying air of %s's combo";
505                         else if(w_deathtype & HITTYPE_SPLASH)
506                                 w_deathtypestring = "%s got too close to %s's blue beam";
507                         else
508                                 w_deathtypestring = "%s was blasted by %s's blue beam";
509                 }
510         }
511         return TRUE;
512 }
513 #endif
514 #endif