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