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