6fdd7261be846e3a699a0e4372889338fd05abf7
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / weapons / weapon / vaporizer.qc
1 #include "vaporizer.qh"
2
3 REGISTER_NET_TEMP(TE_CSQC_VAPORBEAMPARTICLE)
4
5 #if defined(SVQC)
6 void SendCSQCVaporizerBeamParticle(entity player, int hit) {
7         vector v;
8         v = WarpZone_UnTransformOrigin(WarpZone_trace_transform, trace_endpos);
9         WriteHeader(MSG_BROADCAST, TE_CSQC_VAPORBEAMPARTICLE);
10         WriteVector(MSG_BROADCAST, w_shotorg);
11         WriteVector(MSG_BROADCAST, v);
12         WriteByte(MSG_BROADCAST, hit);
13         WriteByte(MSG_BROADCAST, etof(player));
14         WriteByte(MSG_BROADCAST, player.team);
15 }
16 #elif defined(CSQC)
17 bool autocvar_cl_vaporizerbeam_particle = false;
18 float autocvar_cl_vaporizerbeam_lifetime = 0.8;
19 float autocvar_cl_vaporizerbeam_colorboost = 0.7;
20
21 string Draw_VaporizerBeam_trace_callback_tex;
22 float Draw_VaporizerBeam_trace_callback_rnd;
23 vector Draw_VaporizerBeam_trace_callback_rgb;
24 float Draw_VaporizerBeam_trace_callback_a;
25 void Draw_VaporizerBeam_trace_callback(vector start, vector hit, vector end)
26 {
27         float i;
28         vector vorg;
29         vorg = WarpZone_TransformOrigin(WarpZone_trace_transform, view_origin);
30         for(i = 0; i < Draw_VaporizerBeam_trace_callback_a; ++i)
31                 Draw_CylindricLine(hit, start, 8, Draw_VaporizerBeam_trace_callback_tex, 0.25, Draw_VaporizerBeam_trace_callback_rnd, Draw_VaporizerBeam_trace_callback_rgb, min(1, Draw_VaporizerBeam_trace_callback_a - i), DRAWFLAG_NORMAL, vorg);
32         Draw_VaporizerBeam_trace_callback_rnd += 0.25 * vlen(hit - start) / 8;
33 }
34
35 .vector vorg1, vorg2;
36 .float spawn_time;
37 void VaporizerBeam_Draw(entity this)
38 {
39         //draw either the old v2.3 beam or the new beam
40         particles_alphamin = particles_alphamax = particles_fade = 1;
41
42         string tex = "particles/lgbeam";
43         if(this.cnt)
44                 tex = "particles/gauntletbeam";
45         vector rgb;
46         //entity e = CSQCModel_server2csqc(this.sv_entnum - 1);
47         //if (e == NULL)
48         //{
49                 rgb = colormapPaletteColor(entcs_GetClientColors(this.sv_entnum - 1) & 0x0F, true);
50                 //rgb = '1 1 1';
51         //}
52         //else
53         //      rgb = e.glowmod;
54         rgb *= (1 + autocvar_cl_vaporizerbeam_colorboost);
55
56         float fail = (this.nextthink - time);
57
58         Draw_VaporizerBeam_trace_callback_tex = tex;
59         Draw_VaporizerBeam_trace_callback_rnd = 0;
60         Draw_VaporizerBeam_trace_callback_rgb = rgb;
61         Draw_VaporizerBeam_trace_callback_a = bound(0, fail, 1);
62         WarpZone_TraceBox_ThroughZone(this.vorg1, '0 0 0', '0 0 0', this.vorg2, MOVE_NOTHING, NULL, NULL, Draw_VaporizerBeam_trace_callback);
63         Draw_VaporizerBeam_trace_callback_tex = string_null;
64
65         /*if(!MUTATOR_CALLHOOK(Particles_VaporizerBeam, this.vorg1, this.vorg2))
66         if(autocvar_cl_particles_oldvortexbeam && (STAT(ALLOW_OLDVORTEXBEAM) || isdemo()))
67                 WarpZone_TrailParticles_WithMultiplier(NULL, particleeffectnum(EFFECT_VORTEX_BEAM_OLD), this.vorg1, this.vorg2, 1, PARTICLES_USEALPHA | PARTICLES_USEFADE);
68         else
69                 WarpZone_TrailParticles_WithMultiplier(NULL, particleeffectnum(EFFECT_VORTEX_BEAM), this.vorg1, this.vorg2, 1, PARTICLES_USEALPHA | PARTICLES_USEFADE);*/
70 }
71
72 NET_HANDLE(TE_CSQC_VAPORBEAMPARTICLE, bool isNew)
73 {
74         Net_Accept(vortex_beam);
75         setthink(this, SUB_Remove);
76         this.nextthink = time + bound(0, autocvar_cl_vaporizerbeam_lifetime, 10);
77         this.draw = VaporizerBeam_Draw;
78         if (isNew) IL_PUSH(g_drawables, this);
79         this.drawmask = MASK_NORMAL;
80
81         this.vorg1 = ReadVector();
82         this.vorg2 = ReadVector();
83         this.cnt = ReadByte();
84         int myowner = ReadByte();
85         this.owner = playerslots[myowner - 1];
86         this.sv_entnum = myowner;
87         this.team = ReadByte() - 1;
88
89         //pointparticles(EFFECT_VORTEX_MUZZLEFLASH, this.vorg1, normalize(this.vorg2 - this.vorg1) * 1000, 1);
90
91         if(autocvar_cl_vaporizerbeam_particle)
92         {
93                 WarpZone_TrailParticles(NULL, particleeffectnum(((this.cnt) ? EFFECT_VAPORIZER_HIT(this.team) : EFFECT_VAPORIZER(this.team))), this.vorg1, this.vorg2);
94                 this.draw = func_null;
95                 this.drawmask = MASK_NORMAL;
96                 delete(this);
97         }
98
99         return true;
100 }
101 #endif
102
103 #ifdef SVQC
104
105 void W_RocketMinsta_Explosion(entity actor, .entity weaponentity, vector loc)
106 {
107         if(accuracy_canbegooddamage(actor))
108                 accuracy_add(actor, WEP_DEVASTATOR, autocvar_g_rm_damage, 0);
109         entity dmgent = spawn();
110         dmgent.owner = dmgent.realowner = actor;
111         setorigin(dmgent, loc);
112         RadiusDamage (dmgent, actor, autocvar_g_rm_damage, autocvar_g_rm_edgedamage, autocvar_g_rm_radius, NULL, NULL, autocvar_g_rm_force, WEP_DEVASTATOR.m_id | HITTYPE_SPLASH, weaponentity, NULL);
113         delete(dmgent);
114 }
115
116 void W_Vaporizer_Attack(Weapon thiswep, entity actor, .entity weaponentity)
117 {
118         bool flying = IsFlying(actor); // do this BEFORE to make the trace values from FireRailgunBullet last
119         float vaporizer_damage = ((WEP_CVAR_PRI(vaporizer, damage) > 0) ? WEP_CVAR_PRI(vaporizer, damage) : 10000);
120
121         W_SetupShot(actor, weaponentity, true, 0, SND_Null, CH_WEAPON_A, vaporizer_damage, thiswep.m_id);
122         // handle sound separately so we can change the volume
123         // added bonus: no longer plays the strength sound (strength gives no bonus to instakill anyway)
124         sound (actor, CH_WEAPON_A, SND_MINSTANEXFIRE, VOL_BASE * 0.8, ATTEN_NORM);
125
126         yoda = 0;
127         damage_goodhits = 0;
128         FireRailgunBullet(actor, weaponentity, w_shotorg, w_shotorg + w_shotdir * max_shot_distance, vaporizer_damage, WEP_CVAR_PRI(vaporizer, force), 0, 0, 0, 0, thiswep.m_id);
129
130         // do this now, as goodhits is disabled below
131         W_MuzzleFlash(actor, weaponentity, EFFECT_VORTEX_MUZZLEFLASH, w_shotorg, w_shotdir * 1000);
132         SendCSQCVaporizerBeamParticle(actor, damage_goodhits);
133
134         if(yoda && flying)
135                 Send_Notification(NOTIF_ONE, actor, MSG_ANNCE, ANNCE_ACHIEVEMENT_YODA);
136         if(damage_goodhits && actor.vaporizer_lasthit)
137         {
138                 Send_Notification(NOTIF_ONE, actor, MSG_ANNCE, ANNCE_ACHIEVEMENT_IMPRESSIVE);
139                 damage_goodhits = 0; // only every second time
140         }
141
142         actor.vaporizer_lasthit = damage_goodhits;
143
144         if(autocvar_g_rm)
145         if(!(trace_dphitq3surfaceflags & (Q3SURFACEFLAG_SKY | Q3SURFACEFLAG_NOIMPACT)))
146                 W_RocketMinsta_Explosion(actor, weaponentity, trace_endpos);
147
148         W_DecreaseAmmo(thiswep, actor, ((autocvar_g_instagib) ? 1 : WEP_CVAR_PRI(vaporizer, ammo)), weaponentity);
149 }
150
151 void W_RocketMinsta_Laser_Explode (entity this, entity directhitentity)
152 {
153         if(directhitentity.takedamage == DAMAGE_AIM)
154                 if(IS_PLAYER(directhitentity))
155                         if(DIFF_TEAM(this.realowner, directhitentity))
156                                 if(!IS_DEAD(directhitentity))
157                                         if(IsFlying(directhitentity))
158                                                 Send_Notification(NOTIF_ONE, this.realowner, MSG_ANNCE, ANNCE_ACHIEVEMENT_ELECTROBITCH);
159
160         this.event_damage = func_null;
161         this.takedamage = DAMAGE_NO;
162         RadiusDamage (this, this.realowner, this.rm_damage, this.rm_edmg, autocvar_g_rm_laser_radius, NULL, NULL, this.rm_force, this.projectiledeathtype, this.weaponentity_fld, directhitentity);
163         delete(this);
164 }
165
166 void W_RocketMinsta_Laser_Explode_use(entity this, entity actor, entity trigger)
167 {
168         W_RocketMinsta_Laser_Explode(this, trigger); // we probably don't want trigger used here, but this matches closest to old behaviour
169 }
170
171 void W_RocketMinsta_Laser_Touch(entity this, entity toucher)
172 {
173         PROJECTILE_TOUCH(this, toucher);
174         //W_RocketMinsta_Laser_Explode ();
175         RadiusDamage(this, this.realowner, this.rm_damage, this.rm_edmg, autocvar_g_rm_laser_radius, NULL, NULL, this.rm_force, this.projectiledeathtype, this.weaponentity_fld, toucher);
176         delete(this);
177 }
178
179 void W_RocketMinsta_Attack2(entity actor, .entity weaponentity)
180 {
181         makevectors(actor.v_angle);
182
183         entity proj;
184         float counter = 0;
185         float total = autocvar_g_rm_laser_count;
186         float spread = autocvar_g_rm_laser_spread;
187         float rndspread = autocvar_g_rm_laser_spread_random;
188
189         W_SetupShot_ProjectileSize (actor, weaponentity, '0 0 -3', '0 0 -3', false, 2, SND_CRYLINK_FIRE, CH_WEAPON_A, autocvar_g_rm_laser_damage, WEP_ELECTRO.m_id);
190
191         W_MuzzleFlash(actor, weaponentity, EFFECT_ELECTRO_MUZZLEFLASH, w_shotorg, w_shotdir * 1000);
192
193     while(counter < total)
194         {
195         proj = new(plasma_prim);
196         proj.owner = proj.realowner = actor;
197         proj.bot_dodge = true;
198         proj.bot_dodgerating = autocvar_g_rm_laser_damage;
199         proj.use = W_RocketMinsta_Laser_Explode_use;
200         setthink(proj, adaptor_think2use_hittype_splash);
201         proj.nextthink = time + autocvar_g_rm_laser_lifetime;
202         PROJECTILE_MAKETRIGGER(proj);
203         proj.projectiledeathtype = WEP_ELECTRO.m_id;
204         proj.weaponentity_fld = weaponentity;
205         setorigin(proj, w_shotorg);
206
207                 proj.rm_force = autocvar_g_rm_laser_force / total;
208                 proj.rm_damage = autocvar_g_rm_laser_damage / total;
209                 proj.rm_edmg = proj.rm_damage;
210
211         //W_SetupProjectileVelocity(proj, autocvar_g_rm_laser_speed, spread * (rndspread ? random() : 1) * autocvar_g_rm_laser_speed);
212
213         set_movetype(proj, MOVETYPE_BOUNCEMISSILE);
214         //W_SETUPPROJECTILEVELOCITY(proj, g_balance_minstanex_laser);
215                 proj.velocity = (w_shotdir + (((counter + 0.5) / total) * 2 - 1) * v_right * (spread * (rndspread ? random() : 1))) * cvar("g_rm_laser_speed");
216                 proj.velocity_z = proj.velocity_z + cvar("g_rm_laser_zspread") * (random() - 0.5);
217                 proj.velocity = W_CalculateProjectileVelocity(actor, actor.velocity, proj.velocity, true);
218         proj.angles = vectoangles(proj.velocity);
219         settouch(proj, W_RocketMinsta_Laser_Touch);
220         setsize(proj, '0 0 -3', '0 0 -3');
221         proj.flags = FL_PROJECTILE;
222         IL_PUSH(g_projectiles, proj);
223         IL_PUSH(g_bot_dodge, proj);
224         proj.missile_flags = MIF_SPLASH;
225
226         CSQCProjectile(proj, true, PROJECTILE_ROCKETMINSTA_LASER, true);
227
228         MUTATOR_CALLHOOK(EditProjectile, actor, proj);
229         counter++;
230     }
231 }
232
233 void W_RocketMinsta_Attack3 (entity actor, .entity weaponentity)
234 {
235         makevectors(actor.v_angle);
236
237         entity proj;
238         float counter = 0;
239         float total = 1;
240
241         W_SetupShot_ProjectileSize (actor, weaponentity, '0 0 -3', '0 0 -3', false, 2, SND_ELECTRO_FIRE2, CH_WEAPON_A, autocvar_g_rm_laser_damage, WEP_ELECTRO.m_id);
242
243         W_MuzzleFlash(actor, weaponentity, EFFECT_ELECTRO_MUZZLEFLASH, w_shotorg, w_shotdir * 1000);
244
245     while(counter < total)
246         {
247         proj = new(plasma_prim);
248         proj.owner = proj.realowner = actor;
249         proj.bot_dodge = true;
250         proj.bot_dodgerating = autocvar_g_rm_laser_damage;
251         proj.use = W_RocketMinsta_Laser_Explode_use;
252         setthink(proj, adaptor_think2use_hittype_splash);
253         proj.nextthink = time + autocvar_g_rm_laser_lifetime;
254         PROJECTILE_MAKETRIGGER(proj);
255         proj.projectiledeathtype = WEP_ELECTRO.m_id;
256         proj.weaponentity_fld = weaponentity;
257         setorigin(proj, w_shotorg);
258
259                 proj.rm_force = autocvar_g_rm_laser_force / total;
260                 proj.rm_damage = autocvar_g_rm_laser_damage / total;
261                 proj.rm_edmg = proj.rm_damage;
262
263         //W_SetupProjectileVelocity(proj, autocvar_g_rm_laser_speed, spread * (rndspread ? random() : 1) * autocvar_g_rm_laser_speed);
264
265         set_movetype(proj, MOVETYPE_BOUNCEMISSILE);
266                 proj.velocity = w_shotdir * autocvar_g_rm_laser_speed;
267                 proj.velocity = W_CalculateProjectileVelocity(actor, actor.velocity, proj.velocity, true);
268         proj.angles = vectoangles(proj.velocity);
269         settouch(proj, W_RocketMinsta_Laser_Touch);
270         setsize(proj, '0 0 -3', '0 0 -3');
271         proj.flags = FL_PROJECTILE;
272         IL_PUSH(g_projectiles, proj);
273         IL_PUSH(g_bot_dodge, proj);
274         proj.missile_flags = MIF_SPLASH;
275
276         CSQCProjectile(proj, true, PROJECTILE_ROCKETMINSTA_LASER, true);
277
278         MUTATOR_CALLHOOK(EditProjectile, actor, proj);
279         counter++;
280     }
281 }
282
283 METHOD(Vaporizer, wr_aim, void(entity thiswep, entity actor, .entity weaponentity))
284 {
285     if(GetResource(actor, thiswep.ammo_type) > 0)
286         PHYS_INPUT_BUTTON_ATCK(actor) = bot_aim(actor, weaponentity, 1000000, 0, 1, false);
287     else
288         PHYS_INPUT_BUTTON_ATCK2(actor) = bot_aim(actor, weaponentity, WEP_CVAR_SEC(vaporizer, speed), 0, WEP_CVAR_SEC(vaporizer, lifetime), false); // WEAPONTODO: replace with proper vaporizer cvars
289 }
290 METHOD(Vaporizer, wr_think, void(entity thiswep, entity actor, .entity weaponentity, int fire))
291 {
292     float vaporizer_ammo = ((autocvar_g_instagib) ? 1 : WEP_CVAR_PRI(vaporizer, ammo));
293     // if the laser uses load, we also consider its ammo for reloading
294     if(WEP_CVAR(vaporizer, reload_ammo) && WEP_CVAR_SEC(vaporizer, ammo) && actor.(weaponentity).clip_load < min(vaporizer_ammo, WEP_CVAR_SEC(vaporizer, ammo))) { // forced reload
295         thiswep.wr_reload(thiswep, actor, weaponentity);
296     } else if(WEP_CVAR(vaporizer, reload_ammo) && actor.(weaponentity).clip_load < vaporizer_ammo) { // forced reload
297         thiswep.wr_reload(thiswep, actor, weaponentity);
298     }
299     if((fire & 1) && (GetResource(actor, RES_CELLS) || !autocvar_g_rm) && !weaponLocked(actor))
300     {
301         if(weapon_prepareattack(thiswep, actor, weaponentity, false, WEP_CVAR_PRI(vaporizer, refire)))
302         {
303             W_Vaporizer_Attack(thiswep, actor, weaponentity);
304             weapon_thinkf(actor, weaponentity, WFRAME_FIRE1, WEP_CVAR_PRI(vaporizer, animtime), w_ready);
305         }
306     }
307     if((fire & 2) || ((fire & 1) && !GetResource(actor, RES_CELLS) && autocvar_g_rm))
308     {
309         if((autocvar_g_rm && autocvar_g_rm_laser) || autocvar_g_rm_laser == 2)
310         {
311             bool rapid = autocvar_g_rm_laser_rapid;
312             if(actor.(weaponentity).jump_interval <= time && !actor.(weaponentity).held_down)
313             {
314                 if(rapid)
315                     actor.(weaponentity).held_down = true;
316                 actor.(weaponentity).jump_interval = time + autocvar_g_rm_laser_refire;
317                 actor.(weaponentity).jump_interval2 = time + autocvar_g_rm_laser_rapid_delay;
318                 damage_goodhits = 0;
319                 W_RocketMinsta_Attack2(actor, weaponentity);
320             }
321             else if(rapid && actor.(weaponentity).jump_interval2 <= time && actor.(weaponentity).held_down)
322             {
323                 actor.(weaponentity).jump_interval2 = time + autocvar_g_rm_laser_rapid_refire;
324                 damage_goodhits = 0;
325                 W_RocketMinsta_Attack3(actor, weaponentity);
326                 //weapon_thinkf(actor, WFRAME_FIRE2, autocvar_g_rm_laser_rapid_animtime, w_ready);
327             }
328         }
329         else if (actor.(weaponentity).jump_interval <= time)
330         {
331             // handle refire manually, so that primary and secondary can be fired without conflictions (important for instagib)
332             actor.(weaponentity).jump_interval = time + WEP_CVAR_SEC(vaporizer, refire) * W_WeaponRateFactor(actor);
333
334             // decrease ammo for the laser?
335             if(WEP_CVAR_SEC(vaporizer, ammo))
336                 W_DecreaseAmmo(thiswep, actor, WEP_CVAR_SEC(vaporizer, ammo), weaponentity);
337
338             BLASTER_SECONDARY_ATTACK(vaporizer, actor, weaponentity);
339
340             // now do normal refire
341             weapon_thinkf(actor, weaponentity, WFRAME_FIRE2, WEP_CVAR_SEC(vaporizer, animtime), w_ready);
342         }
343     }
344     else
345         actor.(weaponentity).held_down = false;
346 }
347 METHOD(Vaporizer, wr_setup, void(entity thiswep, entity actor, .entity weaponentity))
348 {
349     actor.vaporizer_lasthit = 0;
350 }
351 METHOD(Vaporizer, wr_checkammo1, bool(entity thiswep, entity actor, .entity weaponentity))
352 {
353     float vaporizer_ammo = ((autocvar_g_instagib) ? 1 : WEP_CVAR_PRI(vaporizer, ammo));
354     float ammo_amount = GetResource(actor, thiswep.ammo_type) >= vaporizer_ammo;
355     ammo_amount += actor.(weaponentity).(weapon_load[thiswep.m_id]) >= vaporizer_ammo;
356     return ammo_amount;
357 }
358 METHOD(Vaporizer, wr_checkammo2, bool(entity thiswep, entity actor, .entity weaponentity))
359 {
360     if(!WEP_CVAR_SEC(vaporizer, ammo))
361         return true;
362     float ammo_amount = GetResource(actor, thiswep.ammo_type) >= WEP_CVAR_SEC(vaporizer, ammo);
363     ammo_amount += actor.(weaponentity).(weapon_load[thiswep.m_id]) >= WEP_CVAR_SEC(vaporizer, ammo);
364     return ammo_amount;
365 }
366 METHOD(Vaporizer, wr_resetplayer, void(entity thiswep, entity actor))
367 {
368     actor.vaporizer_lasthit = 0;
369 }
370 METHOD(Vaporizer, wr_reload, void(entity thiswep, entity actor, .entity weaponentity))
371 {
372     float vaporizer_ammo = ((autocvar_g_instagib) ? 1 : WEP_CVAR_PRI(vaporizer, ammo));
373     float used_ammo;
374     if(WEP_CVAR_SEC(vaporizer, ammo))
375         used_ammo = min(vaporizer_ammo, WEP_CVAR_SEC(vaporizer, ammo));
376     else
377         used_ammo = vaporizer_ammo;
378
379     W_Reload(actor, weaponentity, used_ammo, SND_RELOAD);
380 }
381 METHOD(Vaporizer, wr_suicidemessage, Notification(entity thiswep))
382 {
383     return WEAPON_THINKING_WITH_PORTALS;
384 }
385 METHOD(Vaporizer, wr_killmessage, Notification(entity thiswep))
386 {
387     return WEAPON_VAPORIZER_MURDER;
388 }
389
390 #endif
391 #ifdef CSQC
392
393 METHOD(Vaporizer, wr_impacteffect, void(entity thiswep, entity actor))
394 {
395     vector org2 = w_org + w_backoff * 6;
396     if(w_deathtype & HITTYPE_SECONDARY)
397     {
398         pointparticles(EFFECT_BLASTER_IMPACT, org2, w_backoff * 1000, 1);
399         if(!w_issilent) { sound(actor, CH_SHOTS, SND_LASERIMPACT, VOL_BASE, ATTN_NORM); }
400     }
401     else
402     {
403         pointparticles(EFFECT_VORTEX_IMPACT, org2, '0 0 0', 1);
404         if(!w_issilent) { sound(actor, CH_SHOTS, SND_NEXIMPACT, VOL_BASE, ATTN_NORM); }
405     }
406 }
407 METHOD(Vaporizer, wr_init, void(entity thiswep))
408 {
409     if(autocvar_cl_reticle && autocvar_cl_reticle_weapon)
410     {
411         precache_pic("gfx/reticle_nex");
412     }
413 }
414 METHOD(Vaporizer, wr_zoom, bool(entity thiswep, entity actor))
415 {
416     if(button_zoom || zoomscript_caught)
417     {
418         return true;
419     }
420     else
421     {
422         // no weapon specific image for this weapon
423         return false;
424     }
425 }
426
427 #endif