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