Merge remote branch 'origin/master' into fruitiex/fruitbalance
[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, '0 0 -4', '0 0 -4', 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, '0 0 -4', '0 0 -4');
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 .vector hook_start, hook_end;
188 float lgbeam_send(entity to, float sf)
189 {
190         WriteByte(MSG_ENTITY, ENT_CLIENT_LGBEAM);
191         sf = sf & 0x7F;
192         if(sound_allowed(MSG_BROADCAST, self.owner))
193                 sf |= 0x80;
194         WriteByte(MSG_ENTITY, sf);
195         if(sf & 1)
196         {
197                 WriteByte(MSG_ENTITY, num_for_edict(self.owner));
198                 WriteCoord(MSG_ENTITY, cvar("g_balance_electro_primary_range"));
199         }
200         if(sf & 2)
201         {
202                 WriteCoord(MSG_ENTITY, self.hook_start_x);
203                 WriteCoord(MSG_ENTITY, self.hook_start_y);
204                 WriteCoord(MSG_ENTITY, self.hook_start_z);
205         }
206         if(sf & 4)
207         {
208                 WriteCoord(MSG_ENTITY, self.hook_end_x);
209                 WriteCoord(MSG_ENTITY, self.hook_end_y);
210                 WriteCoord(MSG_ENTITY, self.hook_end_z);
211         }
212         return TRUE;
213 }
214 .entity lgbeam;
215 .float prevlgfire;
216 void lgbeam_think()
217 {
218         self.owner.prevlgfire = time;
219         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.deadflag != DEAD_NO || !self.owner.BUTTON_ATCK)
220         {
221                 remove(self);
222                 return;
223         }
224
225         self.nextthink = time;
226
227         makevectors(self.owner.v_angle);
228
229         float dt;
230         dt = frametime;
231         if not(self.owner.items & IT_UNLIMITED_WEAPON_AMMO)
232         {
233                 if(cvar("g_balance_electro_primary_ammo"))
234                         dt = min(frametime, self.owner.ammo_cells / cvar("g_balance_electro_primary_ammo"));
235                 self.owner.ammo_cells = max(0, self.owner.ammo_cells - cvar("g_balance_electro_primary_ammo") * frametime);
236         }
237
238         W_SetupShot_Range(self.owner, TRUE, 0, "", cvar("g_balance_electro_primary_damage") * dt, cvar("g_balance_electro_primary_range"));
239         WarpZone_traceline_antilag(self.owner, w_shotorg, w_shotend, MOVE_NORMAL, self.owner, ANTILAG_LATENCY(self.owner));
240
241         // apply the damage
242         if(trace_ent)
243         {
244                 vector force;
245                 force = w_shotdir * cvar("g_balance_electro_primary_force") + '0 0 1' * cvar("g_balance_electro_primary_force_up");
246                 Damage (trace_ent, self.owner, self.owner, cvar("g_balance_electro_primary_damage") * dt, WEP_ELECTRO, trace_endpos, force * dt);
247                 Damage_RecordDamage(self.owner, WEP_ELECTRO, cvar("g_balance_electro_primary_damage") * dt);
248         }
249         W_Plasma_TriggerCombo(trace_endpos, cvar("g_balance_electro_primary_comboradius"), self.owner);
250
251         // draw effect
252         if(w_shotorg != self.hook_start)
253         {
254                 self.SendFlags |= 2;
255                 self.hook_start = w_shotorg;
256         }
257         if(w_shotend != self.hook_end)
258         {
259                 self.SendFlags |= 4;
260                 self.hook_end = w_shotend;
261         }
262 }
263
264 // experimental lightning gun
265 void W_Electro_Attack3 (void)
266 {
267         // only play fire sound if 0.5 sec has passed since player let go the fire button
268         if(time - self.prevlgfire > 0.5)
269         {
270                 sound (self, CHAN_WEAPON, "weapons/lgbeam_fire.wav", VOL_BASE, ATTN_NORM);
271         }
272
273         entity beam, oldself;
274
275         self.lgbeam = beam = spawn();
276         beam.solid = SOLID_NOT;
277         beam.think = lgbeam_think;
278         beam.owner = self;
279         beam.movetype = MOVETYPE_NONE;
280         beam.shot_spread = 0;
281         beam.bot_dodge = TRUE;
282         beam.bot_dodgerating = cvar("g_balance_electro_primary_damage");
283         Net_LinkEntity(beam, FALSE, 0, lgbeam_send);
284
285         oldself = self;
286         self = beam;
287         self.think();
288         self = oldself;
289 }
290
291 void ElectroInit()
292 {
293         weapon_action(WEP_ELECTRO, WR_PRECACHE);
294         electro_shotorigin[0] = shotorg_adjust_values(CL_Weapon_GetShotOrg(WEP_ELECTRO), FALSE, FALSE, 1);
295         electro_shotorigin[1] = shotorg_adjust_values(CL_Weapon_GetShotOrg(WEP_ELECTRO), FALSE, FALSE, 2);
296         electro_shotorigin[2] = shotorg_adjust_values(CL_Weapon_GetShotOrg(WEP_ELECTRO), FALSE, FALSE, 3);
297         electro_shotorigin[3] = shotorg_adjust_values(CL_Weapon_GetShotOrg(WEP_ELECTRO), FALSE, FALSE, 4);
298 }
299
300 void spawnfunc_weapon_electro (void)
301 {
302         weapon_defaultspawnfunc(WEP_ELECTRO);
303 }
304
305 void w_electro_checkattack()
306 {
307         if(self.electro_count > 1)
308         if(self.BUTTON_ATCK2)
309         if(weapon_prepareattack(1, -1))
310         {
311                 W_Electro_Attack2();
312                 self.electro_count -= 1;
313                 weapon_thinkf(WFRAME_FIRE2, cvar("g_balance_electro_secondary_animtime"), w_electro_checkattack);
314                 return;
315         }
316
317         w_ready();
318 }
319
320 .float bot_secondary_electromooth;
321 .float BUTTON_ATCK_prev;
322 float w_electro(float req)
323 {
324         if (req == WR_AIM)
325         {
326                 self.BUTTON_ATCK=FALSE;
327                 self.BUTTON_ATCK2=FALSE;
328                 if(vlen(self.origin-self.enemy.origin) > 1000)
329                         self.bot_secondary_electromooth = 0;
330                 if(self.bot_secondary_electromooth == 0)
331                 {
332                         if(bot_aim(cvar("g_balance_electro_primary_speed"), 0, cvar("g_balance_electro_primary_lifetime"), FALSE))
333                         {
334                                 self.BUTTON_ATCK = TRUE;
335                                 if(random() < 0.01) self.bot_secondary_electromooth = 1;
336                         }
337                 }
338                 else
339                 {
340                         if(bot_aim(cvar("g_balance_electro_secondary_speed"), cvar("g_balance_grenadelauncher_secondary_speed_up"), cvar("g_balance_electro_secondary_lifetime"), TRUE))
341                         {
342                                 self.BUTTON_ATCK2 = TRUE;
343                                 if(random() < 0.03) self.bot_secondary_electromooth = 0;
344                         }
345                 }
346         }
347         else if (req == WR_THINK)
348         {
349                 if (self.BUTTON_ATCK)
350                 {
351                         if (weapon_prepareattack(0, (cvar("g_balance_electro_lightning") ? 0 : cvar("g_balance_electro_primary_refire"))))
352                         {
353                                 if(cvar("g_balance_electro_lightning"))
354                                 {
355                                         if (self.BUTTON_ATCK_prev == 0)
356                                         {
357                                                 W_Electro_Attack3();
358                                         }
359                                         self.BUTTON_ATCK_prev = 1;
360                                 }
361                                 else
362                                 {
363                                         W_Electro_Attack();
364                                 }
365                                 weapon_thinkf(WFRAME_FIRE1, cvar("g_balance_electro_primary_animtime"), w_ready);
366                         }
367                 } else {
368                         self.BUTTON_ATCK_prev = 0;
369                 }
370
371                 if (self.BUTTON_ATCK2)
372                 if (time >= self.electro_secondarytime)
373                 if (weapon_prepareattack(1, cvar("g_balance_electro_secondary_refire")))
374                 {
375                         W_Electro_Attack2();
376                         self.electro_count = cvar("g_balance_electro_secondary_count");
377                         weapon_thinkf(WFRAME_FIRE2, cvar("g_balance_electro_secondary_animtime"), w_electro_checkattack);
378                         self.electro_secondarytime = time + cvar("g_balance_electro_secondary_refire2");
379                 }
380         }
381         else if (req == WR_PRECACHE)
382         {
383                 precache_model ("models/weapons/g_electro.md3");
384                 precache_model ("models/weapons/v_electro.md3");
385                 precache_model ("models/weapons/h_electro.iqm");
386                 precache_sound ("weapons/electro_bounce.wav");
387                 precache_sound ("weapons/electro_fire.wav");
388                 precache_sound ("weapons/electro_fire2.wav");
389                 precache_sound ("weapons/electro_impact.wav");
390                 precache_sound ("weapons/electro_impact_combo.wav");
391                 if(cvar("g_balance_electro_lightning"))
392                 {
393                         precache_sound ("weapons/lgbeam_fire.wav");
394                 }
395         }
396         else if (req == WR_SETUP)
397                 weapon_setup(WEP_ELECTRO);
398         else if (req == WR_CHECKAMMO1)
399         {
400                 if(cvar("g_balance_electro_lightning"))
401                         return self.ammo_cells >= cvar("g_balance_electro_primary_ammo") * cvar("g_balance_electro_primary_refire");
402                 else
403                         return self.ammo_cells >= cvar("g_balance_electro_primary_ammo");
404         }
405         else if (req == WR_CHECKAMMO2)
406                 return self.ammo_cells >= cvar("g_balance_electro_secondary_ammo");
407         else if (req == WR_RESETPLAYER)
408         {
409                 self.electro_secondarytime = time;
410         }
411         return TRUE;
412 };
413 #endif
414 #ifdef CSQC
415 float w_electro(float req)
416 {
417         if(req == WR_IMPACTEFFECT)
418         {
419                 vector org2;
420                 org2 = w_org + w_backoff * 6;
421                 if(w_deathtype & HITTYPE_SECONDARY)
422                 {
423                         pointparticles(particleeffectnum("electro_ballexplode"), org2, '0 0 0', 1);
424                         if(!w_issilent)
425                                 sound(self, CHAN_PROJECTILE, "weapons/electro_impact.wav", VOL_BASE, ATTN_NORM);
426                 }
427                 else
428                 {
429                         if(w_deathtype & HITTYPE_BOUNCE)
430                         {
431                                 // this is sent as "primary (w_deathtype & HITTYPE_BOUNCE)" to distinguish it from (w_deathtype & HITTYPE_SECONDARY) bounced balls
432                                 pointparticles(particleeffectnum("electro_combo"), org2, '0 0 0', 1);
433                                 if(!w_issilent)
434                                         sound(self, CHAN_PROJECTILE, "weapons/electro_impact_combo.wav", VOL_BASE, ATTN_NORM);
435                         }
436                         else
437                         {
438                                 pointparticles(particleeffectnum("electro_impact"), org2, '0 0 0', 1);
439                                 if(!w_issilent)
440                                         sound(self, CHAN_PROJECTILE, "weapons/electro_impact.wav", VOL_BASE, ATTN_NORM);
441                         }
442                 }
443         }
444         else if(req == WR_PRECACHE)
445         {
446                 precache_sound("weapons/electro_impact.wav");
447                 precache_sound("weapons/electro_impact_combo.wav");
448         }
449         else if (req == WR_SUICIDEMESSAGE)
450         {
451                 if(w_deathtype & HITTYPE_SECONDARY)
452                         w_deathtypestring = "%s could not remember where they put plasma";
453                 else
454                         w_deathtypestring = "%s played with plasma";
455         }
456         else if (req == WR_KILLMESSAGE)
457         {
458                 if(w_deathtype & HITTYPE_SECONDARY)
459                 {
460                         if(w_deathtype & HITTYPE_SPLASH) // unchecked: BOUNCE
461                                 w_deathtypestring = "%s just noticed %s's blue ball";
462                         else // unchecked: BOUNCE
463                                 w_deathtypestring = "%s got in touch with %s's blue ball";
464                 }
465                 else
466                 {
467                         if(w_deathtype & HITTYPE_BOUNCE) // combo
468                                 w_deathtypestring = "%s felt the electrifying air of %s's combo";
469                         else if(w_deathtype & HITTYPE_SPLASH)
470                                 w_deathtypestring = "%s got too close to %s's blue beam";
471                         else
472                                 w_deathtypestring = "%s was blasted by %s's blue beam";
473                 }
474         }
475         return TRUE;
476 }
477 #endif
478 #endif