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