42b9887cee8579728b50254d9cf9b9d2ff5dc999
[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) / cvar("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, cvar("g_balance_electro_secondary_damage"), cvar("g_balance_electro_secondary_edgedamage"), cvar("g_balance_electro_secondary_radius"), world, cvar("g_balance_electro_secondary_force"), self.projectiledeathtype, other);
43         }
44         else
45         {
46                 W_Plasma_TriggerCombo(self.origin, cvar("g_balance_electro_primary_comboradius"), self.owner);
47                 RadiusDamage (self, self.owner, cvar("g_balance_electro_primary_damage"), cvar("g_balance_electro_primary_edgedamage"), cvar("g_balance_electro_primary_radius"), world, cvar("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, cvar("g_balance_electro_combo_comboradius"), self.owner);
56
57         self.event_damage = SUB_Null;
58         RadiusDamage (self, self.owner, cvar("g_balance_electro_combo_damage"), cvar("g_balance_electro_combo_edgedamage"), cvar("g_balance_electro_combo_radius"), world, cvar("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         PROJECTILE_TOUCH;
65         if (other.takedamage == DAMAGE_AIM) {
66                 W_Plasma_Explode ();
67         } else {
68                 spamsound (self, CHAN_PROJECTILE, "weapons/electro_bounce.wav", VOL_BASE, ATTN_NORM);
69                 self.projectiledeathtype |= HITTYPE_BOUNCE;
70         }
71 }
72
73 void W_Plasma_TouchExplode (void)
74 {
75         PROJECTILE_TOUCH;
76         W_Plasma_Explode ();
77 }
78
79 void W_Plasma_Damage (entity inflictor, entity attacker, float damage, float deathtype, vector hitloc, vector force)
80 {
81         if(self.health <= 0)
82                 return;
83         // note: combos are usually triggered by W_Plasma_TriggerCombo, not damage
84         self.health = self.health - damage;
85         if (self.health <= 0)
86         {
87                 self.takedamage = DAMAGE_NO;
88                 self.nextthink = time;
89                 if (inflictor.classname == "plasma_chain" || inflictor.classname == "plasma_prim")
90                 {
91                         // change owner to whoever caused the combo explosion
92                         self.owner = inflictor.owner;
93                         self.classname = "plasma_chain";
94                         self.think = W_Plasma_Explode_Combo;
95                         self.nextthink = time + min(cvar("g_balance_electro_combo_radius"), vlen(self.origin - inflictor.origin)) / cvar("g_balance_electro_combo_speed"); // delay combo chains, looks cooler
96                                 //                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ bounding the length, because inflictor may be in a galaxy far far away (warpzones)
97                 }
98                 else
99                 {
100                         self.use = W_Plasma_Explode;
101                         self.think = adaptor_think2use; // not _hittype_splash, as this runs "immediately"
102                 }
103         }
104 }
105
106 void W_Electro_Attack()
107 {
108         local entity proj;
109
110         W_SetupShot_ProjectileSize (self, '0 0 -3', '0 0 -3', FALSE, 2, "weapons/electro_fire.wav", cvar("g_balance_electro_primary_damage"));
111
112         pointparticles(particleeffectnum("electro_muzzleflash"), w_shotorg, w_shotdir * 1000, 1);
113
114         proj = spawn ();
115         proj.classname = "plasma_prim";
116         proj.owner = self;
117         proj.bot_dodge = TRUE;
118         proj.bot_dodgerating = cvar("g_balance_electro_primary_damage");
119         proj.use = W_Plasma_Explode;
120         proj.think = adaptor_think2use_hittype_splash;
121         proj.nextthink = time + cvar("g_balance_electro_primary_lifetime");
122         PROJECTILE_MAKETRIGGER(proj);
123         proj.projectiledeathtype = WEP_ELECTRO;
124         setorigin(proj, w_shotorg);
125
126         if not(self.items & IT_UNLIMITED_WEAPON_AMMO)
127                 self.ammo_cells = self.ammo_cells - cvar("g_balance_electro_primary_ammo");
128         proj.movetype = MOVETYPE_FLY;
129         W_SETUPPROJECTILEVELOCITY(proj, g_balance_electro_primary);
130         proj.angles = vectoangles(proj.velocity);
131         proj.touch = W_Plasma_TouchExplode;
132         setsize(proj, '0 0 -3', '0 0 -3');
133         proj.flags = FL_PROJECTILE;
134
135         //sound (proj, CHAN_PAIN, "weapons/electro_fly.wav", VOL_BASE, ATTN_NORM);
136         //sounds bad
137
138         CSQCProjectile(proj, TRUE, PROJECTILE_ELECTRO_BEAM, TRUE);
139 }
140
141 void W_Electro_Attack2()
142 {
143         local entity proj;
144
145         W_SetupShot_ProjectileSize (self, '-3 -3 -3', '3 3 3', FALSE, 2, "weapons/electro_fire2.wav", cvar("g_balance_electro_secondary_damage"));
146         w_shotdir = v_forward; // no TrueAim for grenades please
147
148         pointparticles(particleeffectnum("electro_muzzleflash"), w_shotorg, w_shotdir * 1000, 1);
149
150         proj = spawn ();
151         proj.classname = "plasma";
152         proj.owner = self;
153         proj.use = W_Plasma_Explode;
154         proj.think = adaptor_think2use_hittype_splash;
155         proj.bot_dodge = TRUE;
156         proj.bot_dodgerating = cvar("g_balance_electro_secondary_damage");
157         proj.nextthink = time + cvar("g_balance_electro_secondary_lifetime");
158         PROJECTILE_MAKETRIGGER(proj);
159         proj.projectiledeathtype = WEP_ELECTRO | HITTYPE_SECONDARY;
160         setorigin(proj, w_shotorg);
161
162         if not(self.items & IT_UNLIMITED_WEAPON_AMMO)
163                 self.ammo_cells = self.ammo_cells - cvar("g_balance_electro_secondary_ammo");
164         //proj.glow_size = 50;
165         //proj.glow_color = 45;
166         proj.movetype = MOVETYPE_BOUNCE;
167         W_SETUPPROJECTILEVELOCITY_UP(proj, g_balance_electro_secondary);
168         proj.touch = W_Plasma_Touch;
169         setsize(proj, '-3 -3 -3', '3 3 3');
170         proj.takedamage = DAMAGE_YES;
171         proj.damageforcescale = cvar("g_balance_electro_secondary_damageforcescale");
172         proj.health = cvar("g_balance_electro_secondary_health");
173         proj.event_damage = W_Plasma_Damage;
174         proj.flags = FL_PROJECTILE;
175
176 #if 0
177         entity p2;
178         p2 = spawn();
179         copyentity(proj, p2);
180         setmodel(p2, "models/ebomb.mdl");
181         setsize(p2, proj.mins, proj.maxs);
182 #endif
183
184         CSQCProjectile(proj, TRUE, PROJECTILE_ELECTRO, FALSE); // no culling, it has sound
185 }
186
187 .entity lgbeam, exteriorlgbeam;
188 .float nextdamagethink;
189 void lgbeam_think()
190 {
191         if (self.owner.weaponentity.state != WS_INUSE || (self.owner.ammo_cells <= 0 && !(self.owner.items & IT_UNLIMITED_WEAPON_AMMO)) || self != self.owner.lgbeam || self.owner.BUTTON_ATCK == 0)
192         {
193                 remove(self);
194                 return;
195         }
196
197         self.nextthink = time;
198
199         makevectors(self.owner.v_angle);
200         vector angle;
201         angle = v_forward;
202
203         vector force;
204         force = angle * cvar("g_balance_electro_primary_force");
205
206         traceline_antilag(self.owner, self.owner.origin + self.owner.view_ofs, self.owner.origin + self.owner.view_ofs + angle * cvar("g_balance_electro_primary_range"), FALSE, self.owner, ANTILAG_LATENCY(self.owner));
207
208         self.scale = vlen(self.owner.origin - trace_endpos) / 256 * (0.75 + 0.75 * random());
209 }
210
211 void exteriorlgbeam_think()
212 {
213         if (self.owner.weaponentity.state != WS_INUSE || (self.owner.ammo_cells <= 0 && !(self.owner.items & IT_UNLIMITED_WEAPON_AMMO)) || self != self.owner.exteriorlgbeam || self.owner.BUTTON_ATCK == 0)
214         {
215                 sound (self, CHAN_PROJECTILE, "sound/misc/null.wav", VOL_BASE, ATTN_NORM);
216                 remove(self);
217                 return;
218         }
219
220         if (time - self.shot_spread + random() * 0.2 > 0)
221         {
222                 self.shot_spread = time + 2;
223                 sound (self, CHAN_PROJECTILE, "turrets/phaser.wav", VOL_BASE, ATTN_NORM);
224         }
225
226         self.nextthink = time;
227
228         makevectors(self.owner.v_angle);
229         vector angle;
230         angle = v_forward;
231
232         traceline_antilag(self.owner, self.owner.origin + self.owner.view_ofs, self.owner.origin + self.owner.view_ofs + angle * cvar("g_balance_electro_primary_range"), FALSE, self.owner, ANTILAG_LATENCY(self.owner));
233
234         // apply the damage
235         if(trace_fraction < 1 && self.nextdamagethink <= time)
236         {
237                 if not(self.items & IT_UNLIMITED_WEAPON_AMMO)
238                         self.ammo_cells = max(0, self.ammo_cells - cvar("g_balance_electro_primary_ammo") * cvar("g_balance_electro_primary_refire"));
239
240                 vector force;
241                 force = angle * cvar("g_balance_electro_primary_force") + '0 0 1' * cvar("g_balance_electro_primary_force_up");
242                 Damage (trace_ent, self.owner, self.owner, cvar("g_balance_electro_primary_damage") * cvar("g_balance_electro_primary_refire"), WEP_ELECTRO, trace_endpos, force * cvar("g_balance_electro_primary_refire"));
243                 W_Plasma_TriggerCombo(trace_endpos, cvar("g_balance_electro_primary_comboradius"), self.owner);
244                 self.nextdamagethink = time + cvar("g_balance_electro_primary_refire");
245         }
246
247         self.angles = '0 0 0';
248         self.scale = vlen(gettaginfo(self, 0) - trace_endpos) / 256 * (0.75 + 0.75 * random());
249
250         // v_forward, v_right, v_up now are the ACTUAL directions
251         // express self.angles in terms of them to aim at target!
252         self.angles = vectoangles('1 0 0' * (angle * v_forward) + '0 -1 0' * (angle * v_right) + '0 0 1' * (angle * v_up));
253 }
254
255 // experimental lightning gun
256 void W_Electro_Attack3 (void)
257 {
258         entity beam, oldself;
259
260         self.lgbeam = beam = spawn();
261         setmodel(beam,"models/turrets/phaser_beam.md3");
262         beam.effects = EF_LOWPRECISION;
263         beam.solid = SOLID_NOT;
264         beam.think = lgbeam_think;
265         beam.owner = self;
266         beam.movetype = MOVETYPE_NONE;
267         if(gettagindex(self.weaponentity, "shot"))
268                 setattachment(beam, self.weaponentity, "shot");
269         else
270                 setattachment(beam, self.weaponentity, "tag_shot");
271
272         oldself = self;
273         self = beam;
274         self.think();
275         self = oldself;
276
277         self.exteriorlgbeam = beam = spawn();
278         setmodel(beam,"models/turrets/phaser_beam.md3");
279         beam.effects = EF_LOWPRECISION;
280         beam.solid = SOLID_NOT;
281         beam.think = exteriorlgbeam_think;
282         beam.owner = self;
283         beam.movetype = MOVETYPE_NONE;
284         //beam.viewmodelforclient = self;
285         if(self.weaponentity.oldorigin_x > 0)
286         {
287                 setattachment(beam, self.exteriorweaponentity, "");
288                 setorigin(beam, self.weaponentity.oldorigin);
289         }
290         else
291         {
292                 if(gettagindex(self.exteriorweaponentity, "shot"))
293                         setattachment(beam, self.exteriorweaponentity, "shot");
294                 else
295                         setattachment(beam, self.exteriorweaponentity, "tag_shot");
296         }
297
298         beam.shot_spread = 0;
299         beam.bot_dodge = TRUE;
300         beam.bot_dodgerating = cvar("g_balance_electro_primary_damage");
301
302         oldself = self;
303         self = beam;
304         self.think();
305         self = oldself;
306 }
307
308 void spawnfunc_weapon_electro (void)
309 {
310         weapon_defaultspawnfunc(WEP_ELECTRO);
311 }
312
313 void w_electro_checkattack()
314 {
315         if(self.electro_count > 1)
316         if(self.BUTTON_ATCK2)
317         if(weapon_prepareattack(1, -1))
318         {
319                 W_Electro_Attack2();
320                 self.electro_count -= 1;
321                 weapon_thinkf(WFRAME_FIRE2, cvar("g_balance_electro_secondary_animtime"), w_electro_checkattack);
322                 return;
323         }
324
325         w_ready();
326 }
327
328 .float bot_secondary_electromooth;
329 .float BUTTON_ATCK_prev;
330 float w_electro(float req)
331 {
332         if (req == WR_AIM)
333         {
334                 self.BUTTON_ATCK=FALSE;
335                 self.BUTTON_ATCK2=FALSE;
336                 if(vlen(self.origin-self.enemy.origin) > 1000)
337                         self.bot_secondary_electromooth = 0;
338                 if(self.bot_secondary_electromooth == 0)
339                 {
340                         if(bot_aim(cvar("g_balance_electro_primary_speed"), 0, cvar("g_balance_electro_primary_lifetime"), FALSE))
341                         {
342                                 self.BUTTON_ATCK = TRUE;
343                                 if(random() < 0.01) self.bot_secondary_electromooth = 1;
344                         }
345                 }
346                 else
347                 {
348                         if(bot_aim(cvar("g_balance_electro_secondary_speed"), cvar("g_balance_grenadelauncher_secondary_speed_up"), cvar("g_balance_electro_secondary_lifetime"), TRUE))
349                         {
350                                 self.BUTTON_ATCK2 = TRUE;
351                                 if(random() < 0.03) self.bot_secondary_electromooth = 0;
352                         }
353                 }
354         }
355         else if (req == WR_THINK)
356         {
357                 if (self.BUTTON_ATCK)
358                 {
359                         if (weapon_prepareattack(0, cvar("g_balance_electro_primary_refire")))
360                         {
361                                 if(cvar("g_balance_electro_lightning"))
362                                 {
363                                         if (self.BUTTON_ATCK_prev == 0)
364                                         {
365                                                 W_Electro_Attack3();
366                                         }
367                                         self.BUTTON_ATCK_prev = 1;
368                                 }
369                                 else
370                                 {
371                                         W_Electro_Attack();
372                                 }
373                                 weapon_thinkf(WFRAME_FIRE1, cvar("g_balance_electro_primary_animtime"), w_ready);
374                         }
375                 } else {
376                         self.BUTTON_ATCK_prev = 0;
377                 }
378
379                 if (self.BUTTON_ATCK2)
380                 if (time >= self.electro_secondarytime)
381                 if (weapon_prepareattack(1, cvar("g_balance_electro_secondary_refire")))
382                 {
383                         W_Electro_Attack2();
384                         self.electro_count = cvar("g_balance_electro_secondary_count");
385                         weapon_thinkf(WFRAME_FIRE2, cvar("g_balance_electro_secondary_animtime"), w_electro_checkattack);
386                         self.electro_secondarytime = time + cvar("g_balance_electro_secondary_refire2");
387                 }
388         }
389         else if (req == WR_PRECACHE)
390         {
391                 precache_model ("models/weapons/g_electro.md3");
392                 precache_model ("models/weapons/v_electro.md3");
393                 precache_model ("models/weapons/h_electro.iqm");
394                 precache_sound ("weapons/electro_bounce.wav");
395                 precache_sound ("weapons/electro_fire.wav");
396                 precache_sound ("weapons/electro_fire2.wav");
397                 precache_sound ("weapons/electro_impact.wav");
398                 precache_sound ("weapons/electro_impact_combo.wav");
399                 if(cvar("g_balance_electro_lightning"))
400                         precache_sound ("weapons/crylink_fire2.wav");
401         }
402         else if (req == WR_SETUP)
403                 weapon_setup(WEP_ELECTRO);
404         else if (req == WR_CHECKAMMO1)
405                 return self.ammo_cells >= cvar("g_balance_electro_primary_ammo");
406         else if (req == WR_CHECKAMMO2)
407                 return self.ammo_cells >= cvar("g_balance_electro_secondary_ammo");
408         else if (req == WR_RESETPLAYER)
409         {
410                 self.electro_secondarytime = time;
411         }
412         return TRUE;
413 };
414 #endif
415 #ifdef CSQC
416 float w_electro(float req)
417 {
418         if(req == WR_IMPACTEFFECT)
419         {
420                 vector org2;
421                 org2 = w_org + w_backoff * 6;
422                 if(w_deathtype & HITTYPE_SECONDARY)
423                 {
424                         pointparticles(particleeffectnum("electro_ballexplode"), org2, '0 0 0', 1);
425                         if(!w_issilent)
426                                 sound(self, CHAN_PROJECTILE, "weapons/electro_impact.wav", VOL_BASE, ATTN_NORM);
427                 }
428                 else
429                 {
430                         if(w_deathtype & HITTYPE_BOUNCE)
431                         {
432                                 // this is sent as "primary (w_deathtype & HITTYPE_BOUNCE)" to distinguish it from (w_deathtype & HITTYPE_SECONDARY) bounced balls
433                                 pointparticles(particleeffectnum("electro_combo"), org2, '0 0 0', 1);
434                                 if(!w_issilent)
435                                         sound(self, CHAN_PROJECTILE, "weapons/electro_impact_combo.wav", VOL_BASE, ATTN_NORM);
436                         }
437                         else
438                         {
439                                 pointparticles(particleeffectnum("electro_impact"), org2, '0 0 0', 1);
440                                 if(!w_issilent)
441                                         sound(self, CHAN_PROJECTILE, "weapons/electro_impact.wav", VOL_BASE, ATTN_NORM);
442                         }
443                 }
444         }
445         else if(req == WR_PRECACHE)
446         {
447                 precache_sound("weapons/electro_impact.wav");
448                 precache_sound("weapons/electro_impact_combo.wav");
449         }
450         else if (req == WR_SUICIDEMESSAGE)
451         {
452                 if(w_deathtype & HITTYPE_SECONDARY)
453                         w_deathtypestring = "could not remember where they put plasma";
454                 else
455                         w_deathtypestring = "played with plasma";
456         }
457         else if (req == WR_KILLMESSAGE)
458         {
459                 if(w_deathtype & HITTYPE_SECONDARY)
460                 {
461                         if(w_deathtype & HITTYPE_SPLASH) // unchecked: BOUNCE
462                                 w_deathtypestring = "just noticed #'s blue ball";
463                         else // unchecked: BOUNCE
464                                 w_deathtypestring = "got in touch with #'s blue ball";
465                 }
466                 else
467                 {
468                         if(w_deathtype & HITTYPE_BOUNCE) // combo
469                                 w_deathtypestring = "felt the electrifying air of #'s combo";
470                         else if(w_deathtype & HITTYPE_SPLASH)
471                                 w_deathtypestring = "got too close to #'s blue beam";
472                         else
473                                 w_deathtypestring = "was blasted by #'s blue beam";
474                 }
475         }
476         return TRUE;
477 }
478 #endif
479 #endif