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