f64e4ca8dcecf8e38134cd28a300c82e74ee0200
[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         //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(cvar("g_balance_electro_combo_radius"), vlen(self.origin - inflictor.origin)) / cvar("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", cvar("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 = cvar("g_balance_electro_primary_damage");
122         proj.use = W_Plasma_Explode;
123         proj.think = adaptor_think2use_hittype_splash;
124         proj.nextthink = time + cvar("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 - cvar("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", cvar("g_balance_electro_secondary_damage"));
151         w_shotdir = v_forward; // no TrueAim for grenades please
152
153         pointparticles(particleeffectnum("electro_muzzleflash"), w_shotorg, w_shotdir * 1000, 1);
154
155         proj = spawn ();
156         proj.classname = "plasma";
157         proj.owner = self;
158         proj.use = W_Plasma_Explode;
159         proj.think = adaptor_think2use_hittype_splash;
160         proj.bot_dodge = TRUE;
161         proj.bot_dodgerating = cvar("g_balance_electro_secondary_damage");
162         proj.nextthink = time + cvar("g_balance_electro_secondary_lifetime");
163         PROJECTILE_MAKETRIGGER(proj);
164         proj.projectiledeathtype = WEP_ELECTRO | HITTYPE_SECONDARY;
165         setorigin(proj, w_shotorg);
166
167         if not(self.items & IT_UNLIMITED_WEAPON_AMMO)
168                 self.ammo_cells = self.ammo_cells - cvar("g_balance_electro_secondary_ammo");
169         //proj.glow_size = 50;
170         //proj.glow_color = 45;
171         proj.movetype = MOVETYPE_BOUNCE;
172         W_SETUPPROJECTILEVELOCITY_UP(proj, g_balance_electro_secondary);
173         proj.touch = W_Plasma_Touch;
174         setsize(proj, '0 0 -4', '0 0 -4');
175         proj.takedamage = DAMAGE_YES;
176         proj.damageforcescale = cvar("g_balance_electro_secondary_damageforcescale");
177         proj.health = cvar("g_balance_electro_secondary_health");
178         proj.event_damage = W_Plasma_Damage;
179         proj.flags = FL_PROJECTILE;
180         
181         proj.bouncefactor = cvar("g_balance_electro_secondary_bouncefactor");
182         proj.bouncestop = cvar("g_balance_electro_secondary_bouncestop");
183         
184 #if 0
185         entity p2;
186         p2 = spawn();
187         copyentity(proj, p2);
188         setmodel(p2, "models/ebomb.mdl");
189         setsize(p2, proj.mins, proj.maxs);
190 #endif
191
192         CSQCProjectile(proj, TRUE, PROJECTILE_ELECTRO, FALSE); // no culling, it has sound
193
194         other = proj; MUTATOR_CALLHOOK(EditProjectile);
195         }
196
197 .vector hook_start, hook_end;
198 float lgbeam_send(entity to, float sf)
199 {
200         WriteByte(MSG_ENTITY, ENT_CLIENT_LGBEAM);
201         sf = sf & 0x7F;
202         if(sound_allowed(MSG_BROADCAST, self.owner))
203                 sf |= 0x80;
204         WriteByte(MSG_ENTITY, sf);
205         if(sf & 1)
206         {
207                 WriteByte(MSG_ENTITY, num_for_edict(self.owner));
208                 WriteCoord(MSG_ENTITY, cvar("g_balance_electro_primary_range"));
209         }
210         if(sf & 2)
211         {
212                 WriteCoord(MSG_ENTITY, self.hook_start_x);
213                 WriteCoord(MSG_ENTITY, self.hook_start_y);
214                 WriteCoord(MSG_ENTITY, self.hook_start_z);
215         }
216         if(sf & 4)
217         {
218                 WriteCoord(MSG_ENTITY, self.hook_end_x);
219                 WriteCoord(MSG_ENTITY, self.hook_end_y);
220                 WriteCoord(MSG_ENTITY, self.hook_end_z);
221         }
222         return TRUE;
223 }
224 .entity lgbeam;
225 .float prevlgfire;
226 void lgbeam_think()
227 {
228         self.owner.prevlgfire = time;
229         if (self != self.owner.lgbeam)
230         {
231                 remove(self);
232                 return;
233         }
234         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)
235         {
236                 if(self == self.owner.lgbeam)
237                         self.owner.lgbeam = world;
238                 remove(self);
239                 return;
240         }
241
242         self.nextthink = time;
243
244         makevectors(self.owner.v_angle);
245
246         float dt, f;
247         dt = frametime;
248         if not(self.owner.items & IT_UNLIMITED_WEAPON_AMMO)
249         {
250                 if(cvar("g_balance_electro_primary_ammo"))
251                 {
252                         dt = min(dt, self.owner.ammo_cells / cvar("g_balance_electro_primary_ammo"));
253                         self.owner.ammo_cells = max(0, self.owner.ammo_cells - cvar("g_balance_electro_primary_ammo") * frametime);
254                 }
255         }
256
257         W_SetupShot_Range(self.owner, TRUE, 0, "", cvar("g_balance_electro_primary_damage") * dt, cvar("g_balance_electro_primary_range"));
258         WarpZone_traceline_antilag(self.owner, w_shotorg, w_shotend, MOVE_NORMAL, self.owner, ANTILAG_LATENCY(self.owner));
259
260         // apply the damage
261         if(trace_ent)
262         {
263                 vector force;
264                 force = w_shotdir * cvar("g_balance_electro_primary_force") + '0 0 1' * cvar("g_balance_electro_primary_force_up");
265
266                 f = ExponentialFalloff(cvar("g_balance_electro_primary_falloff_mindist"), cvar("g_balance_electro_primary_falloff_maxdist"), cvar("g_balance_electro_primary_falloff_halflifedist"), vlen(WarpZone_UnTransformOrigin(WarpZone_trace_transform, trace_endpos) - w_shotorg));
267
268                 if(accuracy_isgooddamage(self.owner, trace_ent))
269                         accuracy_add(self.owner, WEP_ELECTRO, 0, cvar("g_balance_electro_primary_damage") * dt * f);
270                 Damage (trace_ent, self.owner, self.owner, cvar("g_balance_electro_primary_damage") * dt * f, WEP_ELECTRO, trace_endpos, force * dt);
271         }
272         W_Plasma_TriggerCombo(trace_endpos, cvar("g_balance_electro_primary_comboradius"), self.owner);
273
274         // draw effect
275         if(w_shotorg != self.hook_start)
276         {
277                 self.SendFlags |= 2;
278                 self.hook_start = w_shotorg;
279         }
280         if(w_shotend != self.hook_end)
281         {
282                 self.SendFlags |= 4;
283                 self.hook_end = w_shotend;
284         }
285 }
286
287 // experimental lightning gun
288 void W_Electro_Attack3 (void)
289 {
290         // only play fire sound if 0.5 sec has passed since player let go the fire button
291         if(time - self.prevlgfire > 0.5)
292         {
293                 sound (self, CHAN_WEAPON, "weapons/lgbeam_fire.wav", VOL_BASE, ATTN_NORM);
294         }
295
296         entity beam, oldself;
297
298         self.lgbeam = beam = spawn();
299         beam.solid = SOLID_NOT;
300         beam.think = lgbeam_think;
301         beam.owner = self;
302         beam.movetype = MOVETYPE_NONE;
303         beam.shot_spread = 0;
304         beam.bot_dodge = TRUE;
305         beam.bot_dodgerating = cvar("g_balance_electro_primary_damage");
306         Net_LinkEntity(beam, FALSE, 0, lgbeam_send);
307
308         oldself = self;
309         self = beam;
310         self.think();
311         self = oldself;
312 }
313
314 void ElectroInit()
315 {
316         weapon_action(WEP_ELECTRO, WR_PRECACHE);
317         electro_shotorigin[0] = shotorg_adjust_values(CL_Weapon_GetShotOrg(WEP_ELECTRO), FALSE, FALSE, 1);
318         electro_shotorigin[1] = shotorg_adjust_values(CL_Weapon_GetShotOrg(WEP_ELECTRO), FALSE, FALSE, 2);
319         electro_shotorigin[2] = shotorg_adjust_values(CL_Weapon_GetShotOrg(WEP_ELECTRO), FALSE, FALSE, 3);
320         electro_shotorigin[3] = shotorg_adjust_values(CL_Weapon_GetShotOrg(WEP_ELECTRO), FALSE, FALSE, 4);
321 }
322
323 void spawnfunc_weapon_electro (void)
324 {
325         weapon_defaultspawnfunc(WEP_ELECTRO);
326 }
327
328 void w_electro_checkattack()
329 {
330         if(self.electro_count > 1)
331         if(self.BUTTON_ATCK2)
332         if(weapon_prepareattack(1, -1))
333         {
334                 W_Electro_Attack2();
335                 self.electro_count -= 1;
336                 weapon_thinkf(WFRAME_FIRE2, cvar("g_balance_electro_secondary_animtime"), w_electro_checkattack);
337                 return;
338         }
339
340         w_ready();
341 }
342
343 .float bot_secondary_electromooth;
344 .float BUTTON_ATCK_prev;
345 float w_electro(float req)
346 {
347         if (req == WR_AIM)
348         {
349                 self.BUTTON_ATCK=FALSE;
350                 self.BUTTON_ATCK2=FALSE;
351                 if(vlen(self.origin-self.enemy.origin) > 1000)
352                         self.bot_secondary_electromooth = 0;
353                 if(self.bot_secondary_electromooth == 0)
354                 {
355                         if(bot_aim(cvar("g_balance_electro_primary_speed"), 0, cvar("g_balance_electro_primary_lifetime"), FALSE))
356                         {
357                                 self.BUTTON_ATCK = TRUE;
358                                 if(random() < 0.01) self.bot_secondary_electromooth = 1;
359                         }
360                 }
361                 else
362                 {
363                         if(bot_aim(cvar("g_balance_electro_secondary_speed"), cvar("g_balance_grenadelauncher_secondary_speed_up"), cvar("g_balance_electro_secondary_lifetime"), TRUE))
364                         {
365                                 self.BUTTON_ATCK2 = TRUE;
366                                 if(random() < 0.03) self.bot_secondary_electromooth = 0;
367                         }
368                 }
369         }
370         else if (req == WR_THINK)
371         {
372                 if (self.BUTTON_ATCK)
373                 {
374                         if (weapon_prepareattack(0, (cvar("g_balance_electro_lightning") ? 0 : cvar("g_balance_electro_primary_refire"))))
375                         {
376                                 if(cvar("g_balance_electro_lightning"))
377                                 {
378                                         if ((!self.lgbeam) || wasfreed(self.lgbeam))
379                                         {
380                                                 W_Electro_Attack3();
381                                         }
382                                         self.BUTTON_ATCK_prev = 1;
383                                 }
384                                 else
385                                 {
386                                         W_Electro_Attack();
387                                 }
388                                 weapon_thinkf(WFRAME_FIRE1, cvar("g_balance_electro_primary_animtime"), w_ready);
389                         }
390                 } else {
391                         if(cvar("g_balance_electro_lightning"))
392                         {
393                                 if (self.BUTTON_ATCK_prev != 0)
394                                 {
395                                         ATTACK_FINISHED(self) = time + cvar("g_balance_electro_primary_refire") * W_WeaponRateFactor();
396                                 }
397                                 self.BUTTON_ATCK_prev = 0;
398                         }
399                 }
400
401                 if (self.BUTTON_ATCK2)
402                 if (time >= self.electro_secondarytime)
403                 if (weapon_prepareattack(1, cvar("g_balance_electro_secondary_refire")))
404                 {
405                         W_Electro_Attack2();
406                         self.electro_count = cvar("g_balance_electro_secondary_count");
407                         weapon_thinkf(WFRAME_FIRE2, cvar("g_balance_electro_secondary_animtime"), w_electro_checkattack);
408                         self.electro_secondarytime = time + cvar("g_balance_electro_secondary_refire2");
409                 }
410         }
411         else if (req == WR_PRECACHE)
412         {
413                 precache_model ("models/weapons/g_electro.md3");
414                 precache_model ("models/weapons/v_electro.md3");
415                 precache_model ("models/weapons/h_electro.iqm");
416                 precache_sound ("weapons/electro_bounce.wav");
417                 precache_sound ("weapons/electro_fire.wav");
418                 precache_sound ("weapons/electro_fire2.wav");
419                 precache_sound ("weapons/electro_impact.wav");
420                 precache_sound ("weapons/electro_impact_combo.wav");
421                 if(cvar("g_balance_electro_lightning"))
422                 {
423                         precache_sound ("weapons/lgbeam_fire.wav");
424                 }
425         }
426         else if (req == WR_SETUP)
427                 weapon_setup(WEP_ELECTRO);
428         else if (req == WR_CHECKAMMO1)
429         {
430                 if(cvar("g_balance_electro_lightning"))
431                         return !cvar("g_balance_electro_primary_ammo") || (self.ammo_cells > 0);
432                 else
433                         return self.ammo_cells >= cvar("g_balance_electro_primary_ammo");
434         }
435         else if (req == WR_CHECKAMMO2)
436                 return self.ammo_cells >= cvar("g_balance_electro_secondary_ammo");
437         else if (req == WR_RESETPLAYER)
438         {
439                 self.electro_secondarytime = time;
440         }
441         return TRUE;
442 };
443 #endif
444 #ifdef CSQC
445 float w_electro(float req)
446 {
447         if(req == WR_IMPACTEFFECT)
448         {
449                 vector org2;
450                 org2 = w_org + w_backoff * 6;
451                 if(w_deathtype & HITTYPE_SECONDARY)
452                 {
453                         pointparticles(particleeffectnum("electro_ballexplode"), org2, '0 0 0', 1);
454                         if(!w_issilent)
455                                 sound(self, CHAN_PROJECTILE, "weapons/electro_impact.wav", VOL_BASE, ATTN_NORM);
456                 }
457                 else
458                 {
459                         if(w_deathtype & HITTYPE_BOUNCE)
460                         {
461                                 // this is sent as "primary (w_deathtype & HITTYPE_BOUNCE)" to distinguish it from (w_deathtype & HITTYPE_SECONDARY) bounced balls
462                                 pointparticles(particleeffectnum("electro_combo"), org2, '0 0 0', 1);
463                                 if(!w_issilent)
464                                         sound(self, CHAN_PROJECTILE, "weapons/electro_impact_combo.wav", VOL_BASE, ATTN_NORM);
465                         }
466                         else
467                         {
468                                 pointparticles(particleeffectnum("electro_impact"), org2, '0 0 0', 1);
469                                 if(!w_issilent)
470                                         sound(self, CHAN_PROJECTILE, "weapons/electro_impact.wav", VOL_BASE, ATTN_NORM);
471                         }
472                 }
473         }
474         else if(req == WR_PRECACHE)
475         {
476                 precache_sound("weapons/electro_impact.wav");
477                 precache_sound("weapons/electro_impact_combo.wav");
478         }
479         else if (req == WR_SUICIDEMESSAGE)
480         {
481                 if(w_deathtype & HITTYPE_SECONDARY)
482                         w_deathtypestring = "%s could not remember where they put plasma";
483                 else
484                         w_deathtypestring = "%s played with plasma";
485         }
486         else if (req == WR_KILLMESSAGE)
487         {
488                 if(w_deathtype & HITTYPE_SECONDARY)
489                 {
490                         if(w_deathtype & HITTYPE_SPLASH) // unchecked: BOUNCE
491                                 w_deathtypestring = "%s just noticed %s's blue ball";
492                         else // unchecked: BOUNCE
493                                 w_deathtypestring = "%s got in touch with %s's blue ball";
494                 }
495                 else
496                 {
497                         if(w_deathtype & HITTYPE_BOUNCE) // combo
498                                 w_deathtypestring = "%s felt the electrifying air of %s's combo";
499                         else if(w_deathtype & HITTYPE_SPLASH)
500                                 w_deathtypestring = "%s got too close to %s's blue beam";
501                         else
502                                 w_deathtypestring = "%s was blasted by %s's blue beam";
503                 }
504         }
505         return TRUE;
506 }
507 #endif
508 #endif