]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/weapons/weapon/electro.qc
Merge branch 'Lyberta/URS3' into 'master'
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / weapons / weapon / electro.qc
1 #include "electro.qh"
2
3 #ifdef SVQC
4 spawnfunc(weapon_electro) { weapon_defaultspawnfunc(this, WEP_ELECTRO); }
5
6 void W_Electro_TriggerCombo(vector org, float rad, entity own)
7 {
8         entity e = WarpZone_FindRadius(org, rad, !WEP_CVAR(electro, combo_comboradius_thruwall));
9         while(e)
10         {
11                 if(e.classname == "electro_orb")
12                 {
13                         // do we allow thruwall triggering?
14                         if(WEP_CVAR(electro, combo_comboradius_thruwall))
15                         {
16                                 // if distance is greater than thruwall distance, check to make sure it's not through a wall
17                                 if(vdist(e.WarpZone_findradius_dist, >, WEP_CVAR(electro, combo_comboradius_thruwall)))
18                                 {
19                                         WarpZone_TraceLine(org, e.origin, MOVE_NOMONSTERS, e);
20                                         if(trace_fraction != 1)
21                                         {
22                                                 // trigger is through a wall and outside of thruwall range, abort
23                                                 e = e.chain;
24                                                 continue;
25                                         }
26                                 }
27                         }
28
29                         // change owner to whoever caused the combo explosion
30                         e.realowner = own;
31                         e.takedamage = DAMAGE_NO;
32                         e.classname = "electro_orb_chain";
33
34                         // now set the next one to trigger as well
35                         setthink(e, W_Electro_ExplodeCombo);
36
37                         // delay combo chains, looks cooler
38                         e.nextthink =
39                                 (
40                                         time
41                                         +
42                                         (WEP_CVAR(electro, combo_speed) ?
43                                                 (vlen(e.WarpZone_findradius_dist) / WEP_CVAR(electro, combo_speed))
44                                                 :
45                                                 0
46                                         )
47                                 );
48                 }
49                 e = e.chain;
50         }
51 }
52
53 void W_Electro_ExplodeCombo(entity this)
54 {
55         W_Electro_TriggerCombo(this.origin, WEP_CVAR(electro, combo_comboradius), this.realowner);
56
57         this.event_damage = func_null;
58
59         RadiusDamage(
60                 this,
61                 this.realowner,
62                 WEP_CVAR(electro, combo_damage),
63                 WEP_CVAR(electro, combo_edgedamage),
64                 WEP_CVAR(electro, combo_radius),
65                 NULL,
66                 NULL,
67                 WEP_CVAR(electro, combo_force),
68                 WEP_ELECTRO.m_id | HITTYPE_BOUNCE, // use THIS type for a combo because primary can't bounce
69                 NULL
70         );
71
72         delete(this);
73 }
74
75 void W_Electro_Explode(entity this, entity directhitentity)
76 {
77         if(directhitentity.takedamage == DAMAGE_AIM)
78                 if(IS_PLAYER(directhitentity))
79                         if(DIFF_TEAM(this.realowner, directhitentity))
80                                 if(!IS_DEAD(directhitentity))
81                                         if(IsFlying(directhitentity))
82                                                 Send_Notification(NOTIF_ONE, this.realowner, MSG_ANNCE, ANNCE_ACHIEVEMENT_ELECTROBITCH);
83
84         this.event_damage = func_null;
85         this.takedamage = DAMAGE_NO;
86
87         if(this.move_movetype == MOVETYPE_BOUNCE || this.classname == "electro_orb") // TODO: classname is more reliable anyway?
88         {
89                 RadiusDamage(
90                         this,
91                         this.realowner,
92                         WEP_CVAR_SEC(electro, damage),
93                         WEP_CVAR_SEC(electro, edgedamage),
94                         WEP_CVAR_SEC(electro, radius),
95                         NULL,
96                         NULL,
97                         WEP_CVAR_SEC(electro, force),
98                         this.projectiledeathtype,
99                         directhitentity
100                 );
101         }
102         else
103         {
104                 W_Electro_TriggerCombo(this.origin, WEP_CVAR_PRI(electro, comboradius), this.realowner);
105                 RadiusDamage(
106                         this,
107                         this.realowner,
108                         WEP_CVAR_PRI(electro, damage),
109                         WEP_CVAR_PRI(electro, edgedamage),
110                         WEP_CVAR_PRI(electro, radius),
111                         NULL,
112                         NULL,
113                         WEP_CVAR_PRI(electro, force),
114                         this.projectiledeathtype,
115                         directhitentity
116                 );
117         }
118
119         delete(this);
120 }
121
122 void W_Electro_Explode_use(entity this, entity actor, entity trigger)
123 {
124         W_Electro_Explode(this, trigger);
125 }
126
127 void W_Electro_TouchExplode(entity this, entity toucher)
128 {
129         PROJECTILE_TOUCH(this, toucher);
130         W_Electro_Explode(this, toucher);
131 }
132
133
134 void sys_phys_update_single(entity this);
135
136 void W_Electro_Bolt_Think(entity this)
137 {
138         // sys_phys_update_single(this);
139         if(time >= this.ltime)
140         {
141                 this.use(this, NULL, NULL);
142                 return;
143         }
144
145         if(WEP_CVAR_PRI(electro, midaircombo_radius))
146         {
147                 float found = 0;
148                 entity e = WarpZone_FindRadius(this.origin, WEP_CVAR_PRI(electro, midaircombo_radius), true);
149
150                 // loop through nearby orbs and trigger them
151                 while(e)
152                 {
153                         if(e.classname == "electro_orb")
154                         {
155                                 // change owner to whoever caused the combo explosion
156                                 e.realowner = this.realowner;
157                                 e.takedamage = DAMAGE_NO;
158                                 e.classname = "electro_orb_chain";
159
160                                 // now set the next one to trigger as well
161                                 setthink(e, W_Electro_ExplodeCombo);
162
163                                 // delay combo chains, looks cooler
164                                 e.nextthink =
165                                         (
166                                                 time
167                                                 +
168                                                 (WEP_CVAR(electro, combo_speed) ?
169                                                         (vlen(e.WarpZone_findradius_dist) / WEP_CVAR(electro, combo_speed))
170                                                         :
171                                                         0
172                                                 )
173                                         );
174
175                                 ++found;
176                         }
177                         e = e.chain;
178                 }
179
180                 // if we triggered an orb, should we explode? if not, lets try again next time
181                 if(found && WEP_CVAR_PRI(electro, midaircombo_explode))
182                         { this.use(this, NULL, NULL); }
183                 else
184                         { this.nextthink = min(time + WEP_CVAR_PRI(electro, midaircombo_interval), this.ltime); }
185         }
186         else { this.nextthink = this.ltime; }
187         // this.nextthink = time;
188 }
189
190 void W_Electro_Attack_Bolt(Weapon thiswep, entity actor, .entity weaponentity)
191 {
192         entity proj;
193
194         W_DecreaseAmmo(thiswep, actor, WEP_CVAR_PRI(electro, ammo), weaponentity);
195
196         W_SetupShot_ProjectileSize(
197                 actor,
198                 weaponentity,
199                 '0 0 -3',
200                 '0 0 -3',
201                 false,
202                 2,
203                 SND_ELECTRO_FIRE,
204                 CH_WEAPON_A,
205                 WEP_CVAR_PRI(electro, damage)
206         );
207
208         Send_Effect(EFFECT_ELECTRO_MUZZLEFLASH, w_shotorg, w_shotdir * 1000, 1);
209
210         proj = new(electro_bolt);
211         proj.owner = proj.realowner = actor;
212         proj.bot_dodge = true;
213         proj.bot_dodgerating = WEP_CVAR_PRI(electro, damage);
214         proj.use = W_Electro_Explode_use;
215         setthink(proj, W_Electro_Bolt_Think);
216         proj.nextthink = time;
217         proj.ltime = time + WEP_CVAR_PRI(electro, lifetime);
218         PROJECTILE_MAKETRIGGER(proj);
219         proj.projectiledeathtype = WEP_ELECTRO.m_id;
220         setorigin(proj, w_shotorg);
221
222         // if (IS_CSQC)
223         set_movetype(proj, MOVETYPE_FLY);
224         W_SetupProjVelocity_PRI(proj, electro);
225         proj.angles = vectoangles(proj.velocity);
226         settouch(proj, W_Electro_TouchExplode);
227         setsize(proj, '0 0 -3', '0 0 -3');
228         proj.flags = FL_PROJECTILE;
229         IL_PUSH(g_projectiles, proj);
230         IL_PUSH(g_bot_dodge, proj);
231         proj.missile_flags = MIF_SPLASH;
232
233         CSQCProjectile(proj, true, PROJECTILE_ELECTRO_BEAM, true);
234
235         MUTATOR_CALLHOOK(EditProjectile, actor, proj);
236         // proj.com_phys_pos = proj.origin;
237         // proj.com_phys_vel = proj.velocity;
238 }
239
240 void W_Electro_Orb_Stick(entity this, entity to)
241 {
242         entity newproj = spawn();
243         newproj.classname = this.classname;
244
245         newproj.bot_dodge = this.bot_dodge;
246         newproj.bot_dodgerating = this.bot_dodgerating;
247
248         newproj.owner = this.owner;
249         newproj.realowner = this.realowner;
250         setsize(newproj, this.mins, this.maxs);
251         setorigin(newproj, this.origin);
252         setmodel(newproj, MDL_PROJECTILE_ELECTRO);
253         newproj.angles = vectoangles(-trace_plane_normal); // face against the surface
254
255         newproj.takedamage = this.takedamage;
256         newproj.damageforcescale = this.damageforcescale;
257         newproj.health = this.health;
258         newproj.event_damage = this.event_damage;
259         newproj.spawnshieldtime = this.spawnshieldtime;
260         newproj.damagedbycontents = true;
261         IL_PUSH(g_damagedbycontents, newproj);
262
263         set_movetype(newproj, MOVETYPE_NONE); // lock the orb in place
264         newproj.projectiledeathtype = this.projectiledeathtype;
265
266         settouch(newproj, func_null);
267         setthink(newproj, getthink(this));
268         newproj.nextthink = this.nextthink;
269         newproj.use = this.use;
270         newproj.flags = this.flags;
271         IL_PUSH(g_projectiles, newproj);
272         IL_PUSH(g_bot_dodge, newproj);
273
274         delete(this);
275
276         if(to)
277                 SetMovetypeFollow(this, to);
278 }
279
280 void W_Electro_Orb_Touch(entity this, entity toucher)
281 {
282         PROJECTILE_TOUCH(this, toucher);
283         if(toucher.takedamage == DAMAGE_AIM)
284                 { if(WEP_CVAR_SEC(electro, touchexplode)) { W_Electro_Explode(this, toucher); } }
285         else
286         {
287                 //UpdateCSQCProjectile(this);
288                 spamsound(this, CH_SHOTS, SND_ELECTRO_BOUNCE, VOL_BASE, ATTEN_NORM);
289                 this.projectiledeathtype |= HITTYPE_BOUNCE;
290
291                 if(WEP_CVAR_SEC(electro, stick))
292                         W_Electro_Orb_Stick(this, toucher);
293         }
294 }
295
296 void W_Electro_Orb_Damage(entity this, entity inflictor, entity attacker, float damage, int deathtype, vector hitloc, vector force)
297 {
298         if(this.health <= 0)
299                 return;
300
301         // note: combos are usually triggered by W_Electro_TriggerCombo, not damage
302         float is_combo = (inflictor.classname == "electro_orb_chain" || inflictor.classname == "electro_bolt");
303
304         if(!W_CheckProjectileDamage(inflictor.realowner, this.realowner, deathtype, (is_combo ? 1 : -1)))
305                 return; // g_projectiles_damage says to halt
306
307         this.health = this.health - damage;
308         if(this.health <= 0)
309         {
310                 this.takedamage = DAMAGE_NO;
311                 this.nextthink = time;
312                 if(is_combo)
313                 {
314                         // change owner to whoever caused the combo explosion
315                         this.realowner = inflictor.realowner;
316                         this.classname = "electro_orb_chain";
317                         setthink(this, W_Electro_ExplodeCombo);
318                         this.nextthink = time +
319                                 (
320                                         // bound the length, inflictor may be in a galaxy far far away (warpzones)
321                                         min(
322                                                 WEP_CVAR(electro, combo_radius),
323                                                 vlen(this.origin - inflictor.origin)
324                                         )
325                                         /
326                                         // delay combo chains, looks cooler
327                                         WEP_CVAR(electro, combo_speed)
328                                 );
329                 }
330                 else
331                 {
332                         this.use = W_Electro_Explode_use;
333                         setthink(this, adaptor_think2use); // not _hittype_splash, as this runs "immediately"
334                 }
335         }
336 }
337
338 void W_Electro_Attack_Orb(Weapon thiswep, entity actor, .entity weaponentity)
339 {
340         W_DecreaseAmmo(thiswep, actor, WEP_CVAR_SEC(electro, ammo), weaponentity);
341
342         W_SetupShot_ProjectileSize(
343                 actor,
344                 weaponentity,
345                 '-4 -4 -4',
346                 '4 4 4',
347                 false,
348                 2,
349                 SND_ELECTRO_FIRE2,
350                 CH_WEAPON_A,
351                 WEP_CVAR_SEC(electro, damage)
352         );
353
354         w_shotdir = v_forward; // no TrueAim for grenades please
355
356         Send_Effect(EFFECT_ELECTRO_MUZZLEFLASH, w_shotorg, w_shotdir * 1000, 1);
357
358         entity proj = new(electro_orb);
359         proj.owner = proj.realowner = actor;
360         proj.use = W_Electro_Explode_use;
361         setthink(proj, adaptor_think2use_hittype_splash);
362         proj.bot_dodge = true;
363         proj.bot_dodgerating = WEP_CVAR_SEC(electro, damage);
364         proj.nextthink = time + WEP_CVAR_SEC(electro, lifetime);
365         PROJECTILE_MAKETRIGGER(proj);
366         proj.projectiledeathtype = WEP_ELECTRO.m_id | HITTYPE_SECONDARY;
367         setorigin(proj, w_shotorg);
368
369         //proj.glow_size = 50;
370         //proj.glow_color = 45;
371         set_movetype(proj, MOVETYPE_BOUNCE);
372         W_SetupProjVelocity_UP_SEC(proj, electro);
373         settouch(proj, W_Electro_Orb_Touch);
374         setsize(proj, '-4 -4 -4', '4 4 4');
375         proj.takedamage = DAMAGE_YES;
376         proj.damageforcescale = WEP_CVAR_SEC(electro, damageforcescale);
377         proj.health = WEP_CVAR_SEC(electro, health);
378         proj.event_damage = W_Electro_Orb_Damage;
379         proj.flags = FL_PROJECTILE;
380         IL_PUSH(g_projectiles, proj);
381         IL_PUSH(g_bot_dodge, proj);
382         proj.damagedbycontents = (WEP_CVAR_SEC(electro, damagedbycontents));
383         if(proj.damagedbycontents)
384                 IL_PUSH(g_damagedbycontents, proj);
385
386         proj.bouncefactor = WEP_CVAR_SEC(electro, bouncefactor);
387         proj.bouncestop = WEP_CVAR_SEC(electro, bouncestop);
388         proj.missile_flags = MIF_SPLASH | MIF_ARC;
389
390 #if 0
391         entity p2;
392         p2 = spawn();
393         copyentity(proj, p2);
394         setmodel(p2, MDL_PROJECTILE_ELECTRO);
395         setsize(p2, proj.mins, proj.maxs);
396 #endif
397
398         CSQCProjectile(proj, true, PROJECTILE_ELECTRO, false); // no culling, it has sound
399
400         MUTATOR_CALLHOOK(EditProjectile, actor, proj);
401 }
402
403 void W_Electro_CheckAttack(Weapon thiswep, entity actor, .entity weaponentity, int fire)
404 {
405         if(actor.(weaponentity).electro_count > 1)
406         if(PHYS_INPUT_BUTTON_ATCK2(actor))
407         if(weapon_prepareattack(thiswep, actor, weaponentity, true, -1))
408         {
409                 W_Electro_Attack_Orb(WEP_ELECTRO, actor, weaponentity);
410                 actor.(weaponentity).electro_count -= 1;
411                 weapon_thinkf(actor, weaponentity, WFRAME_FIRE2, WEP_CVAR_SEC(electro, animtime), W_Electro_CheckAttack);
412                 return;
413         }
414         // WEAPONTODO: when the player releases the button, cut down the length of refire2?
415         w_ready(thiswep, actor, weaponentity, fire);
416 }
417
418 .float bot_secondary_electromooth;
419
420 METHOD(Electro, wr_aim, void(entity thiswep, entity actor, .entity weaponentity))
421 {
422     PHYS_INPUT_BUTTON_ATCK(actor) = PHYS_INPUT_BUTTON_ATCK2(actor) = false;
423     if(vdist(actor.origin - actor.enemy.origin, >, 1000)) { actor.bot_secondary_electromooth = 0; }
424     if(actor.bot_secondary_electromooth == 0)
425     {
426         float shoot;
427
428         if(WEP_CVAR_PRI(electro, speed))
429             shoot = bot_aim(actor, weaponentity, WEP_CVAR_PRI(electro, speed), 0, WEP_CVAR_PRI(electro, lifetime), false);
430         else
431             shoot = bot_aim(actor, weaponentity, 1000000, 0, 0.001, false);
432
433         if(shoot)
434         {
435             PHYS_INPUT_BUTTON_ATCK(actor) = true;
436             if(random() < 0.01) actor.bot_secondary_electromooth = 1;
437         }
438     }
439     else
440     {
441         if(bot_aim(actor, weaponentity, WEP_CVAR_SEC(electro, speed), WEP_CVAR_SEC(electro, speed_up), WEP_CVAR_SEC(electro, lifetime), true))
442         {
443             PHYS_INPUT_BUTTON_ATCK2(actor) = true;
444             if(random() < 0.03) actor.bot_secondary_electromooth = 0;
445         }
446     }
447 }
448 METHOD(Electro, wr_think, void(entity thiswep, entity actor, .entity weaponentity, int fire))
449 {
450     if(autocvar_g_balance_electro_reload_ammo) // forced reload // WEAPONTODO
451     {
452         float ammo_amount = 0;
453         if(actor.(weaponentity).clip_load >= WEP_CVAR_PRI(electro, ammo))
454             ammo_amount = 1;
455         if(actor.(weaponentity).clip_load >= WEP_CVAR_SEC(electro, ammo))
456             ammo_amount += 1;
457
458         if(!ammo_amount)
459         {
460             thiswep.wr_reload(thiswep, actor, weaponentity);
461             return;
462         }
463     }
464
465     if(fire & 1)
466     {
467         if(weapon_prepareattack(thiswep, actor, weaponentity, false, WEP_CVAR_PRI(electro, refire)))
468         {
469                 W_Electro_Attack_Bolt(thiswep, actor, weaponentity);
470                 weapon_thinkf(actor, weaponentity, WFRAME_FIRE1, WEP_CVAR_PRI(electro, animtime), w_ready);
471         }
472     }
473     else if(fire & 2)
474     {
475         if(time >= actor.(weaponentity).electro_secondarytime)
476         if(weapon_prepareattack(thiswep, actor, weaponentity, true, WEP_CVAR_SEC(electro, refire)))
477         {
478             W_Electro_Attack_Orb(thiswep, actor, weaponentity);
479             actor.(weaponentity).electro_count = WEP_CVAR_SEC(electro, count);
480             weapon_thinkf(actor, weaponentity, WFRAME_FIRE2, WEP_CVAR_SEC(electro, animtime), W_Electro_CheckAttack);
481             actor.(weaponentity).electro_secondarytime = time + WEP_CVAR_SEC(electro, refire2) * W_WeaponRateFactor(actor);
482         }
483     }
484 }
485 METHOD(Electro, wr_checkammo1, bool(entity thiswep, entity actor, .entity weaponentity))
486 {
487     float ammo_amount = GetResourceAmount(actor, thiswep.ammo_type) >= WEP_CVAR_PRI(electro, ammo);
488     ammo_amount += actor.(weaponentity).(weapon_load[WEP_ELECTRO.m_id]) >= WEP_CVAR_PRI(electro, ammo);
489     return ammo_amount;
490 }
491 METHOD(Electro, wr_checkammo2, bool(entity thiswep, entity actor, .entity weaponentity))
492 {
493     float ammo_amount;
494     if(WEP_CVAR(electro, combo_safeammocheck)) // true if you can fire at least one secondary blob AND one primary shot after it, otherwise false.
495     {
496         ammo_amount = GetResourceAmount(actor, thiswep.ammo_type) >= WEP_CVAR_SEC(electro, ammo) + WEP_CVAR_PRI(electro, ammo);
497         ammo_amount += actor.(weaponentity).(weapon_load[WEP_ELECTRO.m_id]) >= WEP_CVAR_SEC(electro, ammo) + WEP_CVAR_PRI(electro, ammo);
498     }
499     else
500     {
501         ammo_amount = GetResourceAmount(actor, thiswep.ammo_type) >= WEP_CVAR_SEC(electro, ammo);
502         ammo_amount += actor.(weaponentity).(weapon_load[WEP_ELECTRO.m_id]) >= WEP_CVAR_SEC(electro, ammo);
503     }
504     return ammo_amount;
505 }
506 METHOD(Electro, wr_resetplayer, void(entity thiswep, entity actor))
507 {
508     for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
509     {
510         .entity weaponentity = weaponentities[slot];
511         actor.(weaponentity).electro_secondarytime = time;
512     }
513 }
514 METHOD(Electro, wr_reload, void(entity thiswep, entity actor, .entity weaponentity))
515 {
516     W_Reload(actor, weaponentity, min(WEP_CVAR_PRI(electro, ammo), WEP_CVAR_SEC(electro, ammo)), SND_RELOAD);
517 }
518 METHOD(Electro, wr_suicidemessage, Notification(entity thiswep))
519 {
520     if(w_deathtype & HITTYPE_SECONDARY)
521         return WEAPON_ELECTRO_SUICIDE_ORBS;
522     else
523         return WEAPON_ELECTRO_SUICIDE_BOLT;
524 }
525 METHOD(Electro, wr_killmessage, Notification(entity thiswep))
526 {
527     if(w_deathtype & HITTYPE_SECONDARY)
528     {
529         return WEAPON_ELECTRO_MURDER_ORBS;
530     }
531     else
532     {
533         if(w_deathtype & HITTYPE_BOUNCE)
534             return WEAPON_ELECTRO_MURDER_COMBO;
535         else
536             return WEAPON_ELECTRO_MURDER_BOLT;
537     }
538 }
539
540 #endif
541 #ifdef CSQC
542
543 METHOD(Electro, wr_impacteffect, void(entity thiswep, entity actor))
544 {
545     vector org2;
546     org2 = w_org + w_backoff * 6;
547     if(w_deathtype & HITTYPE_SECONDARY)
548     {
549         pointparticles(EFFECT_ELECTRO_BALLEXPLODE, org2, '0 0 0', 1);
550         if(!w_issilent)
551             sound(actor, CH_SHOTS, SND_ELECTRO_IMPACT, VOL_BASE, ATTEN_NORM);
552     }
553     else
554     {
555         if(w_deathtype & HITTYPE_BOUNCE)
556         {
557             // this is sent as "primary (w_deathtype & HITTYPE_BOUNCE)" to distinguish it from (w_deathtype & HITTYPE_SECONDARY) bounced balls
558             pointparticles(EFFECT_ELECTRO_COMBO, org2, '0 0 0', 1);
559             if(!w_issilent)
560                 sound(actor, CH_SHOTS, SND_ELECTRO_IMPACT_COMBO, VOL_BASE, ATTEN_NORM);
561         }
562         else
563         {
564             pointparticles(EFFECT_ELECTRO_IMPACT, org2, '0 0 0', 1);
565             if(!w_issilent)
566                 sound(actor, CH_SHOTS, SND_ELECTRO_IMPACT, VOL_BASE, ATTEN_NORM);
567         }
568     }
569 }
570
571 #endif