]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/w_laser.qc
6b7fcdf2d4fde0f9fbe5f4871eb68d346562f057
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / w_laser.qc
1 #ifdef REGISTER_WEAPON
2 REGISTER_WEAPON(LASER, W_Laser, 0, 1, WEP_FLAG_NORMAL | WEP_FLAG_RELOADABLE | WEP_FLAG_CANCLIMB | WEP_TYPE_SPLASH, 0, "laser", "laser", _("Blaster"))
3 #else
4 #ifdef SVQC
5 void(float imp) W_SwitchWeapon;
6 void() W_LastWeapon;
7 .float swing_prev;
8 .entity swing_alreadyhit;
9
10 void SendCSQCShockwaveParticle(float spread, vector endpos, entity transform) 
11 {
12         WarpZone_UnTransformOrigin(transform, endpos);
13         WriteByte(MSG_BROADCAST, SVC_TEMPENTITY);
14         WriteByte(MSG_BROADCAST, TE_CSQC_SHOCKWAVEPARTICLE);
15         WriteCoord(MSG_BROADCAST, w_shotorg_x);
16         WriteCoord(MSG_BROADCAST, w_shotorg_y);
17         WriteCoord(MSG_BROADCAST, w_shotorg_z);
18         WriteCoord(MSG_BROADCAST, endpos_x);
19         WriteCoord(MSG_BROADCAST, endpos_y);
20         WriteCoord(MSG_BROADCAST, endpos_z);
21         WriteByte(MSG_BROADCAST, bound(0, 255 * spread, 255));
22 }
23
24 void W_Laser_Touch()
25 {
26         PROJECTILE_TOUCH;
27
28         self.event_damage = SUB_Null;
29         
30         if(self.dmg)
31                 RadiusDamage(self, self.realowner, autocvar_g_balance_laser_secondary_damage, autocvar_g_balance_laser_secondary_edgedamage, autocvar_g_balance_laser_secondary_radius, world, world, autocvar_g_balance_laser_secondary_force, self.projectiledeathtype, other);
32         else
33                 RadiusDamage(self, self.realowner, autocvar_g_balance_laser_primary_damage, autocvar_g_balance_laser_primary_edgedamage, autocvar_g_balance_laser_primary_radius, world, world, autocvar_g_balance_laser_primary_force, self.projectiledeathtype, other);
34
35         remove(self);
36 }
37
38 void W_Laser_Think()
39 {
40         self.movetype = MOVETYPE_FLY;
41         self.think = SUB_Remove;
42         
43         if(self.dmg)
44                 self.nextthink = time + autocvar_g_balance_laser_secondary_lifetime;
45         else
46                 self.nextthink = time + autocvar_g_balance_laser_primary_lifetime;
47                 
48         CSQCProjectile(self, TRUE, PROJECTILE_LASER, TRUE);
49 }
50
51
52 float W_Laser_Shockwave_CheckSpread(vector targetorg, vector nearest_on_line, vector sw_shotorg, vector attack_endpos)
53 {
54         float spreadlimit;
55         float distance_of_attack = vlen(sw_shotorg - attack_endpos);
56         float distance_from_line = vlen(targetorg - nearest_on_line);
57
58         float total_angle = (vlen(normalize(targetorg - sw_shotorg) - normalize(attack_endpos - sw_shotorg)) * RAD2DEG); 
59         
60         spreadlimit = (distance_of_attack ? min(1, (vlen(sw_shotorg - nearest_on_line) / distance_of_attack)) : 1);
61         spreadlimit = (autocvar_g_balance_laser_primary_spread_min * (1 - spreadlimit) + autocvar_g_balance_laser_primary_spread_max * spreadlimit);
62         
63         if(spreadlimit && (distance_from_line <= spreadlimit) && (total_angle <= 90))
64                 return bound(0, (distance_from_line / spreadlimit), 1);
65         else
66                 return FALSE;
67 }
68
69 float W_Laser_Shockwave_IsVisible(entity head, vector nearest_on_line, vector sw_shotorg, vector attack_endpos)
70 {
71         vector nearest_to_attacker = head.WarpZone_findradius_nearest;
72         vector center = (head.origin + (head.mins + head.maxs) * 0.5);
73         vector corner;
74         float i;
75
76         // STEP ONE: Check if the nearest point is clear
77         if(W_Laser_Shockwave_CheckSpread(nearest_to_attacker, nearest_on_line, sw_shotorg, attack_endpos))
78         {
79                 WarpZone_TraceLine(sw_shotorg, nearest_to_attacker, MOVE_WORLDONLY, self);
80                 if(trace_fraction == 1) { return TRUE; } // yes, the nearest point is clear and we can allow the damage
81         }
82
83         // STEP TWO: Check if shotorg to center point is clear
84         if(W_Laser_Shockwave_CheckSpread(center, nearest_on_line, sw_shotorg, attack_endpos))
85         {
86                 WarpZone_TraceLine(sw_shotorg, center, MOVE_WORLDONLY, self);
87                 if(trace_fraction == 1) { return TRUE; } // yes, the center point is clear and we can allow the damage
88         }
89
90         // STEP THREE: Check each corner to see if they are clear
91         for(i=1; i<=8; ++i)
92         {
93                 corner = get_corner_position(head, i);
94                 if(W_Laser_Shockwave_CheckSpread(corner, nearest_on_line, sw_shotorg, attack_endpos))
95                 {
96                         WarpZone_TraceLine(sw_shotorg, corner, MOVE_WORLDONLY, self);
97                         if(trace_fraction == 1) { return TRUE; } // yes, this corner is clear and we can allow the damage
98                 }
99         }
100
101         return FALSE;
102 }
103
104 void W_Laser_Shockwave()
105 {
106         // declarations
107         float multiplier, multiplier_from_accuracy, multiplier_from_distance;
108         float final_damage, final_spread;
109         vector final_force, center;
110         entity head, next;
111         
112         // set up the shot direction
113         vector wanted_shot_direction = (v_forward * cos(autocvar_g_balance_laser_primary_shotangle * DEG2RAD) + v_up * sin(autocvar_g_balance_laser_primary_shotangle * DEG2RAD));
114         W_SetupShot_Dir(self, wanted_shot_direction, FALSE, 3, "weapons/lasergun_fire.wav", CH_WEAPON_B, autocvar_g_balance_laser_primary_damage);
115         vector attack_endpos = (w_shotorg + (w_shotdir * autocvar_g_balance_laser_primary_radius));
116
117         // find out what we're pointing at and acquire the warpzone transform
118         WarpZone_TraceLine(w_shotorg, attack_endpos, FALSE, self);
119         entity aim_ent = trace_ent;
120         entity transform = WarpZone_trace_transform;
121         
122         vector attack_hitpos = trace_endpos;
123         float distance_to_hit = vlen(w_shotorg - attack_hitpos);
124         float distance_to_end = vlen(w_shotorg - attack_endpos);
125         
126         // do the jump explosion now (also handles the impact effect)
127         RadiusDamageForSource(self, trace_endpos, '0 0 0', self, autocvar_g_balance_laser_primary_damage, autocvar_g_balance_laser_primary_edgedamage, autocvar_g_balance_laser_primary_jumpradius, world, self, TRUE, autocvar_g_balance_laser_primary_force, WEP_LASER, world);
128         
129         // also do the firing effect now
130         SendCSQCShockwaveParticle(autocvar_g_balance_laser_primary_spread, attack_hitpos, transform);
131         
132         // did we hit a player directly?
133         if(aim_ent.takedamage)
134         {
135                 // if it's a player, use the view origin as reference (stolen from RadiusDamage functions in g_damage.qc)
136                 center = (aim_ent.origin + ((aim_ent.classname == "player") ? aim_ent.view_ofs : ((aim_ent.mins + aim_ent.maxs) * 0.5)));
137                 
138                 multiplier_from_accuracy = 1;
139                 multiplier_from_distance = (1 - (distance_to_hit ? min(1, (distance_to_hit / distance_to_end)) : 0));
140                 multiplier = max(autocvar_g_balance_laser_primary_multiplier_min, ((multiplier_from_accuracy * autocvar_g_balance_laser_primary_multiplier_accuracy) + (multiplier_from_distance * autocvar_g_balance_laser_primary_multiplier_distance)));
141                 
142                 final_force = ((normalize(center - attack_hitpos) * autocvar_g_balance_laser_primary_force) * multiplier);
143                 final_damage = (autocvar_g_balance_laser_primary_damage * multiplier + autocvar_g_balance_laser_primary_edgedamage * (1 - multiplier));
144                 Damage(aim_ent, self, self, final_damage, WEP_LASER, aim_ent.origin, final_force);
145                 
146                 print("debug: DIRECT HIT: multiplier = ", ftos(multiplier), strcat(", damage = ", ftos(final_damage), ", force = ", ftos(vlen(final_force))),"... multiplier_from_accuracy = ", ftos(multiplier_from_accuracy), ", multiplier_from_distance = ", ftos(multiplier_from_distance), ".\n");
147         }
148
149         // now figure out if I hit anything else than what my aim directly pointed at...
150         head = WarpZone_FindRadius(w_shotorg, autocvar_g_balance_laser_primary_radius, FALSE);
151         while(head)
152         {
153                 next = head.chain;
154                 
155                 if((head != self && head != aim_ent) && (head.takedamage))
156                 {
157                         // if it's a player, use the view origin as reference (stolen from RadiusDamage functions in g_damage.qc) 
158                         center = (head.origin + ((head.classname == "player") ? head.view_ofs : ((head.mins + head.maxs) * 0.5)));
159
160                         // find the closest point on the enemy to the center of the attack
161                         float ang; // angle between shotdir and h
162                         float h; // hypotenuse, which is the distance between attacker to head
163                         float a; // adjacent side, which is the distance between attacker and the point on w_shotdir that is closest to head.origin
164                         
165                         h = vlen(center - self.origin);
166                         ang = acos(dotproduct(normalize(center - self.origin), w_shotdir));
167                         a = h * cos(ang);
168
169                         vector nearest_on_line = (w_shotorg + a * w_shotdir);
170                         vector nearest_to_attacker = WarpZoneLib_NearestPointOnBox(center + head.mins, center + head.maxs, nearest_on_line);
171                         float distance_to_target = vlen(w_shotorg - nearest_to_attacker);
172
173                         if((distance_to_target <= autocvar_g_balance_laser_primary_radius) 
174                                 && (W_Laser_Shockwave_IsVisible(head, nearest_on_line, w_shotorg, attack_endpos)))
175                         {
176                                 multiplier_from_accuracy = (1 - W_Laser_Shockwave_CheckSpread(nearest_to_attacker, nearest_on_line, w_shotorg, attack_endpos));
177                                 multiplier_from_distance = (1 - (distance_to_hit ? min(1, (distance_to_target / distance_to_end)) : 0));
178                                 multiplier = max(autocvar_g_balance_laser_primary_multiplier_min, ((multiplier_from_accuracy * autocvar_g_balance_laser_primary_multiplier_accuracy) + (multiplier_from_distance * autocvar_g_balance_laser_primary_multiplier_distance)));
179
180                                 final_force = ((normalize(center - nearest_on_line) * autocvar_g_balance_laser_primary_force) * multiplier);
181                                 final_damage = (autocvar_g_balance_laser_primary_damage * multiplier + autocvar_g_balance_laser_primary_edgedamage * (1 - multiplier));
182                                 Damage(head, self, self, final_damage, WEP_LASER, head.origin, final_force);
183
184                                 print("debug: EDGE HIT: multiplier = ", ftos(multiplier), strcat(", damage = ", ftos(final_damage), ", force = ", ftos(vlen(final_force))),"... multiplier_from_accuracy = ", ftos(multiplier_from_accuracy), ", multiplier_from_distance = ", ftos(multiplier_from_distance), ".\n");
185                         }
186                 }
187                 head = next;
188         }
189 }
190
191 void W_Laser_Melee_Think()
192 {
193         // declarations
194         float i, f, swing, swing_factor, swing_damage, meleetime, is_player;
195         entity target_victim;
196         vector targpos;
197
198         if(!self.cnt) // set start time of melee
199         {
200                 self.cnt = time; 
201                 W_PlayStrengthSound(self.realowner);
202         }
203
204         makevectors(self.realowner.v_angle); // update values for v_* vectors
205         
206         // calculate swing percentage based on time
207         meleetime = autocvar_g_balance_laser_secondary_melee_time * W_WeaponRateFactor();
208         swing = bound(0, (self.cnt + meleetime - time) / meleetime, 10);
209         f = ((1 - swing) * autocvar_g_balance_laser_secondary_melee_traces);
210         
211         // check to see if we can still continue, otherwise give up now
212         if((self.realowner.deadflag != DEAD_NO) && autocvar_g_balance_laser_secondary_melee_no_doubleslap)
213         {
214                 remove(self);
215                 return;
216         }
217         
218         // if okay, perform the traces needed for this frame 
219         for(i=self.swing_prev; i < f; ++i)
220         {
221                 swing_factor = ((1 - (i / autocvar_g_balance_laser_secondary_melee_traces)) * 2 - 1);
222                 
223                 targpos = (self.realowner.origin + self.realowner.view_ofs 
224                         + (v_forward * autocvar_g_balance_laser_secondary_melee_range)
225                         + (v_up * swing_factor * autocvar_g_balance_laser_secondary_melee_swing_up)
226                         + (v_right * swing_factor * autocvar_g_balance_laser_secondary_melee_swing_side));
227
228                 WarpZone_traceline_antilag(self.realowner, self.realowner.origin + self.realowner.view_ofs, targpos, FALSE, self.realowner, ANTILAG_LATENCY(self.realowner));
229                 
230                 // draw lightning beams for debugging
231                 te_lightning2(world, targpos, self.realowner.origin + self.realowner.view_ofs + v_forward * 5 - v_up * 5); 
232                 te_customflash(targpos, 40,  2, '1 1 1');
233                 
234                 is_player = (trace_ent.classname == "player" || trace_ent.classname == "body");
235
236                 if((trace_fraction < 1) // if trace is good, apply the damage and remove self
237                         && (trace_ent.takedamage == DAMAGE_AIM)  
238                         && (trace_ent != self.swing_alreadyhit)
239                         && (is_player || autocvar_g_balance_laser_secondary_melee_nonplayerdamage))
240                 {
241                         target_victim = trace_ent; // so it persists through other calls
242                         
243                         if(is_player) // this allows us to be able to nerf the non-player damage done in e.g. assault or onslaught.
244                                 swing_damage = (autocvar_g_balance_laser_secondary_damage * min(1, swing_factor + 1));
245                         else
246                                 swing_damage = (autocvar_g_balance_laser_secondary_melee_nonplayerdamage * min(1, swing_factor + 1));
247                         
248                         //print(strcat(self.realowner.netname, " hitting ", target_victim.netname, " with ", strcat(ftos(swing_damage), " damage (factor: ", ftos(swing_factor), ") at "), ftos(time), " seconds.\n"));
249                         
250                         Damage(target_victim, self.realowner, self.realowner, 
251                                 swing_damage, WEP_LASER | HITTYPE_SECONDARY, 
252                                 self.realowner.origin + self.realowner.view_ofs, 
253                                 v_forward * autocvar_g_balance_laser_secondary_force);
254                                 
255                         if(accuracy_isgooddamage(self.realowner, target_victim)) { accuracy_add(self.realowner, WEP_LASER, 0, swing_damage); }
256                         
257                         if(autocvar_g_balance_laser_secondary_melee_multihit) // allow multiple hits with one swing, but not against the same player twice.
258                         {
259                                 self.swing_alreadyhit = target_victim;
260                                 continue; // move along to next trace
261                         }
262                         else
263                         {
264                                 remove(self);
265                                 return;
266                         }
267                 }
268         }
269         
270         if(time >= self.cnt + meleetime)
271         {
272                 // melee is finished
273                 remove(self);
274                 return;
275         }
276         else
277         {
278                 // set up next frame 
279                 self.swing_prev = i;
280                 self.nextthink = time;
281         }
282 }
283
284 void W_Laser_Melee()
285 {
286         sound(self, CH_WEAPON_A, "weapons/shotgun_melee.wav", VOL_BASE, ATTN_NORM);
287         weapon_thinkf(WFRAME_FIRE2, autocvar_g_balance_laser_secondary_animtime, w_ready);
288
289         entity meleetemp;
290         meleetemp = spawn();
291         meleetemp.owner = meleetemp.realowner = self;
292         meleetemp.think = W_Laser_Melee_Think;
293         meleetemp.nextthink = time + autocvar_g_balance_laser_secondary_melee_delay * W_WeaponRateFactor();
294         W_SetupShot_Range(self, TRUE, 0, "", 0, autocvar_g_balance_laser_secondary_damage, autocvar_g_balance_laser_secondary_melee_range);
295 }
296
297 void W_Laser_Attack(float issecondary)
298 {
299         entity missile;
300         vector s_forward;
301         float a;
302         float nodamage;
303
304         if(issecondary == 2) // minstanex shot
305                 nodamage = g_minstagib;
306         else
307                 nodamage = FALSE;
308
309         a = autocvar_g_balance_laser_primary_shotangle;
310         s_forward = v_forward * cos(a * DEG2RAD) + v_up * sin(a * DEG2RAD);
311
312         if(nodamage)
313                 W_SetupShot_Dir(self, s_forward, FALSE, 3, "weapons/lasergun_fire.wav", CH_WEAPON_B, 0);
314         else if(issecondary == 1)
315                 W_SetupShot_Dir(self, s_forward, FALSE, 3, "weapons/lasergun_fire.wav", CH_WEAPON_B, autocvar_g_balance_laser_secondary_damage);
316         else
317                 W_SetupShot_Dir(self, s_forward, FALSE, 3, "weapons/lasergun_fire.wav", CH_WEAPON_B, autocvar_g_balance_laser_primary_damage);
318         pointparticles(particleeffectnum("laser_muzzleflash"), w_shotorg, w_shotdir * 1000, 1);
319
320         missile = spawn();
321         missile.owner = missile.realowner = self;
322         missile.classname = "laserbolt";
323         missile.dmg = 0;
324         if(!nodamage)
325         {
326                 missile.bot_dodge = TRUE;
327                 missile.bot_dodgerating = autocvar_g_balance_laser_primary_damage;
328         }
329
330         PROJECTILE_MAKETRIGGER(missile);
331         missile.projectiledeathtype = WEP_LASER;
332
333         setorigin(missile, w_shotorg);
334         setsize(missile, '0 0 0', '0 0 0');
335
336         W_SETUPPROJECTILEVELOCITY(missile, g_balance_laser_primary);
337         missile.angles = vectoangles(missile.velocity);
338         //missile.glow_color = 250; // 244, 250
339         //missile.glow_size = 120;
340         missile.touch = W_Laser_Touch;
341
342         missile.flags = FL_PROJECTILE;
343         missile.missile_flags = MIF_SPLASH; 
344
345         missile.think = W_Laser_Think;
346         missile.nextthink = time + autocvar_g_balance_laser_primary_delay;
347
348         other = missile; MUTATOR_CALLHOOK(EditProjectile);
349
350         if(time >= missile.nextthink)
351         {
352                 entity oldself;
353                 oldself = self;
354                 self = missile;
355                 self.think();
356                 self = oldself;
357         }
358 }
359
360 void spawnfunc_weapon_laser(void)
361 {
362         weapon_defaultspawnfunc(WEP_LASER);
363 }
364
365 float W_Laser(float request)
366 {
367         switch(request)
368         {
369                 case WR_AIM:
370                 {
371                         if((autocvar_g_balance_laser_secondary == 2) && (vlen(self.origin-self.enemy.origin) <= autocvar_g_balance_laser_secondary_melee_range))
372                                 self.BUTTON_ATCK2 = bot_aim(1000000, 0, 0.001, FALSE);
373                         else
374                                 self.BUTTON_ATCK = bot_aim(1000000, 0, 1, FALSE);
375                         return TRUE;
376                 }
377                 
378                 case WR_THINK:
379                 {
380                         if(autocvar_g_balance_laser_reload_ammo && self.clip_load < 1) // forced reload
381                                 weapon_action(self.weapon, WR_RELOAD);
382                         else if(self.BUTTON_ATCK)
383                         {
384                                 if(weapon_prepareattack(0, autocvar_g_balance_laser_primary_refire))
385                                 {
386                                         W_DecreaseAmmo(ammo_none, 1, TRUE);
387
388
389                                         if not(autocvar_g_balance_laser_oldprimary)
390                                                 W_Laser_Shockwave();
391                                         else
392                                                 W_Laser_Attack(FALSE);
393
394                                         weapon_thinkf(WFRAME_FIRE1, autocvar_g_balance_laser_primary_animtime, w_ready);
395                                 }
396                         }
397                         else if(self.BUTTON_ATCK2)
398                         {
399                                 switch(autocvar_g_balance_laser_secondary)
400                                 {
401                                         case 0: // switch to last used weapon
402                                         {
403                                                 if(self.switchweapon == WEP_LASER) // don't do this if already switching
404                                                         W_LastWeapon();
405
406                                                 break;
407                                         }
408
409                                         case 1: // normal projectile secondary
410                                         {
411                                                 if(weapon_prepareattack(1, autocvar_g_balance_laser_secondary_refire))
412                                                 {
413                                                         W_DecreaseAmmo(ammo_none, 1, TRUE);
414                                                         W_Laser_Attack(TRUE);
415                                                         weapon_thinkf(WFRAME_FIRE2, autocvar_g_balance_laser_secondary_animtime, w_ready);
416                                                 }
417
418                                                 break;
419                                         }
420
421                                         case 2: // melee attack secondary
422                                         {
423                                                 if(!self.crouch) // we are not currently crouching; this fixes an exploit where your melee anim is not visible, and besides wouldn't make much sense
424                                                 if(weapon_prepareattack(1, autocvar_g_balance_laser_secondary_refire))
425                                                 {
426                                                         // attempt forcing playback of the anim by switching to another anim (that we never play) here...
427                                                         W_Laser_Melee();
428                                                         weapon_thinkf(WFRAME_FIRE1, autocvar_g_balance_laser_secondary_animtime, w_ready);
429                                                 }
430                                         }
431                                 }
432                         }
433                         return TRUE;
434                 }
435                 
436                 case WR_PRECACHE: 
437                 {
438                         precache_model("models/weapons/g_laser.md3");
439                         precache_model("models/weapons/v_laser.md3");
440                         precache_model("models/weapons/h_laser.iqm");
441                         precache_sound("weapons/lasergun_fire.wav");
442                         return TRUE;
443                 }
444                 
445                 case WR_SETUP:
446                 {
447                         weapon_setup(WEP_LASER);
448                         self.current_ammo = ammo_none;
449                         return TRUE;
450                 }
451                 
452                 case WR_CHECKAMMO1:
453                 case WR_CHECKAMMO2:
454                 {
455                         return TRUE; // laser has infinite ammo
456                 }
457                 
458                 case WR_RELOAD:
459                 {
460                         W_Reload(0, autocvar_g_balance_laser_reload_ammo, autocvar_g_balance_laser_reload_time, "weapons/reload.wav");
461                         return TRUE;
462                 }
463         }
464         
465         return TRUE;
466 }
467 #endif
468 #ifdef CSQC
469 float W_Laser(float request)
470 {
471         switch(request)
472         {
473                 case WR_IMPACTEFFECT:
474                 {
475                         vector org2;
476                         org2 = w_org + w_backoff * 6;
477                         pointparticles(particleeffectnum("new_laser_impact"), org2, w_backoff * 1000, 1);
478                         if(!w_issilent) { sound(self, CH_SHOTS, "weapons/laserimpact.wav", VOL_BASE, ATTN_NORM); }
479                         return TRUE;
480                 }
481                 
482                 case WR_PRECACHE:
483                 {
484                         precache_sound("weapons/laserimpact.wav");
485                         return TRUE;
486                 }
487                 case WR_SUICIDEMESSAGE:
488                 {
489                         w_deathtypestring = _("%s lasered themself to hell");
490                         return TRUE;
491                 }
492                 case WR_KILLMESSAGE:
493                 {
494                         if(w_deathtype & HITTYPE_SECONDARY)
495                                 w_deathtypestring = _("%s was cut in half by %s's gauntlet"); // unchecked: SPLASH // TODO 
496                         else
497                                 w_deathtypestring = _("%s was lasered to death by %s"); // unchecked: SPLASH
498                         return TRUE;
499                 }
500         }
501         
502         return TRUE;
503 }
504 #endif
505 #endif