]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/w_laser.qc
Remove guantlet entirely from code
[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                 return bound(0, (distance_from_line / spreadlimit), 1);
58         else
59                 return FALSE;
60 }
61
62 float W_Laser_Shockwave_IsVisible(entity head, vector nearest_on_line, vector sw_shotorg, vector attack_hitpos)
63 {
64         vector nearest_to_attacker = head.WarpZone_findradius_nearest;
65         vector center = (head.origin + (head.mins + head.maxs) * 0.5);
66         vector corner;
67         float i;
68
69         // STEP ONE: Check if the nearest point is clear
70         if(W_Laser_Shockwave_CheckSpread(nearest_to_attacker, nearest_on_line, sw_shotorg, attack_hitpos))
71         {
72                 WarpZone_TraceLine(sw_shotorg, nearest_to_attacker, MOVE_WORLDONLY, self);
73                 if(trace_fraction == 1) { return TRUE; } // yes, the nearest point is clear and we can allow the damage
74         }
75
76         // STEP TWO: Check if shotorg to center point is clear
77         if(W_Laser_Shockwave_CheckSpread(center, nearest_on_line, sw_shotorg, attack_hitpos))
78         {
79                 WarpZone_TraceLine(sw_shotorg, center, MOVE_WORLDONLY, self);
80                 if(trace_fraction == 1) { return TRUE; } // yes, the center point is clear and we can allow the damage
81         }
82
83         // STEP THREE: Check each corner to see if they are clear
84         for(i=1; i<=8; ++i)
85         {
86                 corner = get_corner_position(head, i);
87                 if(W_Laser_Shockwave_CheckSpread(corner, nearest_on_line, sw_shotorg, attack_hitpos))
88                 {
89                         WarpZone_TraceLine(sw_shotorg, corner, MOVE_WORLDONLY, self);
90                         if(trace_fraction == 1) { return TRUE; } // yes, this corner is clear and we can allow the damage
91                 }
92         }
93
94         return FALSE;
95 }
96
97 void W_Laser_Shockwave (void)
98 {
99         // declarations
100         float multiplier, multiplier_from_accuracy, multiplier_from_distance;
101         float final_damage, final_spread;
102         vector final_force, center;
103         entity head, next;
104         
105         // set up the shot direction
106         vector wanted_shot_direction = (v_forward * cos(autocvar_g_balance_laser_primary_shotangle * DEG2RAD) + v_up * sin(autocvar_g_balance_laser_primary_shotangle * DEG2RAD));
107         W_SetupShot_Dir(self, wanted_shot_direction, FALSE, 3, "weapons/lasergun_fire.wav", CH_WEAPON_B, autocvar_g_balance_laser_primary_damage);
108         vector attack_endpos = (w_shotorg + (w_shotdir * autocvar_g_balance_laser_primary_radius));
109
110         // find out what we're pointing at and acquire the warpzone transform
111         WarpZone_TraceLine(w_shotorg, attack_endpos, FALSE, self);
112         entity aim_ent = trace_ent;
113         vector attack_hitpos = trace_endpos;
114         float distance_of_attack = vlen(w_shotorg - attack_hitpos);
115         
116         // do the jump explosion now (also handles the impact effect)
117         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);
118         
119         // also do the firing effect now
120         SendCSQCShockwaveParticle(autocvar_g_balance_laser_primary_spread);
121         
122         // did we hit a player directly?
123         if(aim_ent.takedamage)
124         {
125                 // if it's a player, use the view origin as reference (stolen from RadiusDamage functions in g_damage.qc)
126                 center = (aim_ent.origin + ((aim_ent.classname == "player") ? aim_ent.view_ofs : ((aim_ent.mins + aim_ent.maxs) * 0.5)));
127                 
128                 multiplier_from_accuracy = 1;
129                 multiplier_from_distance = (1 - (distance_of_attack ? min(1, (distance_of_attack / autocvar_g_balance_laser_primary_radius)) : 0));
130                 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)));
131                 
132                 final_force = ((normalize(center - attack_hitpos) * autocvar_g_balance_laser_primary_force) * multiplier);
133                 final_damage = (autocvar_g_balance_laser_primary_damage * multiplier);
134                 Damage(aim_ent, self, self, final_damage, WEP_LASER, aim_ent.origin, final_force);
135                 
136                 print("multiplier = ", ftos(multiplier), ", multiplier_from_accuracy = ", ftos(multiplier_from_accuracy), ", multiplier_from_distance = ", ftos(multiplier_from_distance), ", ");
137                 print(strcat("direct hit damage = ", ftos(autocvar_g_balance_laser_primary_damage), ", force = ", vtos(final_force), ".\n"));
138         }
139
140         // now figure out if I hit anything else than what my aim directly pointed at...
141         head = WarpZone_FindRadius(w_shotorg, autocvar_g_balance_laser_primary_radius, FALSE);
142         while(head)
143         {
144                 next = head.chain;
145                 
146                 if((head != self && head != aim_ent) && (head.takedamage))
147                 {
148                         // if it's a player, use the view origin as reference (stolen from RadiusDamage functions in g_damage.qc) 
149                         center = (head.origin + ((head.classname == "player") ? head.view_ofs : ((head.mins + head.maxs) * 0.5)));
150
151                         // find the closest point on the enemy to the center of the attack
152                         float ang; // angle between shotdir and h
153                         float h; // hypotenuse, which is the distance between attacker to head
154                         float a; // adjacent side, which is the distance between attacker and the point on w_shotdir that is closest to head.origin
155                         
156                         h = vlen(center - self.origin);
157                         ang = acos(dotproduct(normalize(center - self.origin), w_shotdir));
158                         a = h * cos(ang);
159
160                         vector nearest_on_line = (w_shotorg + a * w_shotdir);
161                         vector nearest_to_attacker = WarpZoneLib_NearestPointOnBox(center + head.mins, center + head.maxs, nearest_on_line);
162                         float distance_to_target = vlen(w_shotorg - nearest_to_attacker);
163
164                         if(distance_to_target <= autocvar_g_balance_laser_primary_radius)
165                         {
166                                 if(W_Laser_Shockwave_IsVisible(head, nearest_on_line, w_shotorg, attack_hitpos))
167                                 {
168                                         multiplier_from_accuracy = (1 - W_Laser_Shockwave_CheckSpread(nearest_to_attacker, nearest_on_line, w_shotorg, attack_hitpos));
169                                         multiplier_from_distance = (1 - (distance_of_attack ? min(1, (distance_to_target / autocvar_g_balance_laser_primary_radius)) : 0));
170                                         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)));
171
172                                         final_force = ((normalize(center - nearest_on_line) * autocvar_g_balance_laser_primary_force) * multiplier);
173                                         final_damage = (autocvar_g_balance_laser_primary_damage * multiplier + autocvar_g_balance_laser_primary_edgedamage * (1 - multiplier));
174                                         Damage(head, self, self, final_damage, WEP_LASER, head.origin, final_force);
175
176                                         print("multiplier = ", ftos(multiplier), ", multiplier_from_accuracy = ", ftos(multiplier_from_accuracy), ", multiplier_from_distance = ", ftos(multiplier_from_distance), ", ");
177                                         print(strcat("edge hit damage = ", ftos(final_damage), ", force = ", vtos(final_force), ".\n"));
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 void spawnfunc_weapon_laser (void)
252 {
253         weapon_defaultspawnfunc(WEP_LASER);
254 }
255
256 float w_laser(float req)
257 {
258         float r1;
259         float r2;
260         if (req == WR_AIM)
261         {
262                 if(autocvar_g_balance_laser_secondary)
263                 {
264                         r1 = autocvar_g_balance_laser_primary_damage;
265                         r2 = autocvar_g_balance_laser_secondary_damage;
266                         if (random() * (r2 + r1) > r1)
267                                 self.BUTTON_ATCK2 = bot_aim(autocvar_g_balance_laser_secondary_speed, 0, autocvar_g_balance_laser_secondary_lifetime, FALSE);
268                         else
269                                 self.BUTTON_ATCK = bot_aim(autocvar_g_balance_laser_primary_speed, 0, autocvar_g_balance_laser_primary_lifetime, FALSE);
270                 }
271                 else
272                         self.BUTTON_ATCK = bot_aim(autocvar_g_balance_laser_primary_speed, 0, autocvar_g_balance_laser_primary_lifetime, FALSE);
273         }
274         else if (req == WR_THINK)
275         {
276                 if(autocvar_g_balance_laser_reload_ammo && self.clip_load < 1) // forced reload
277                         weapon_action(self.weapon, WR_RELOAD);
278                 else if (self.BUTTON_ATCK)
279                 {
280                         if (weapon_prepareattack(0, autocvar_g_balance_laser_primary_refire))
281                         {
282                                 W_DecreaseAmmo(ammo_none, 1, TRUE);
283
284
285                                 if not(autocvar_g_balance_laser_oldprimary)
286                                         W_Laser_Shockwave();
287                                 else
288                                         W_Laser_Attack(FALSE);
289
290                                 weapon_thinkf(WFRAME_FIRE1, autocvar_g_balance_laser_primary_animtime, w_ready);
291                         }
292                 }
293                 else if (self.BUTTON_ATCK2)
294                 {
295                         switch(autocvar_g_balance_laser_secondary)
296                         {
297                                 case 0: // switch to last used weapon
298                                 {
299                                         if(self.switchweapon == WEP_LASER) // don't do this if already switching
300                                                 W_LastWeapon();
301
302                                         break;
303                                 }
304
305                                 case 1: // normal projectile secondary
306                                 {
307                                         if(weapon_prepareattack(0, autocvar_g_balance_laser_secondary_refire))
308                                         {
309                                                 W_DecreaseAmmo(ammo_none, 1, TRUE);
310                                                 W_Laser_Attack(TRUE);
311                                                 weapon_thinkf(WFRAME_FIRE2, autocvar_g_balance_laser_secondary_animtime, w_ready);
312                                         }
313
314                                         break;
315                                 }
316                         }
317                 }
318         }
319         else if (req == WR_PRECACHE)
320         {
321                 precache_model ("models/weapons/g_laser.md3");
322                 precache_model ("models/weapons/v_laser.md3");
323                 precache_model ("models/weapons/h_laser.iqm");
324                 precache_sound ("weapons/lasergun_fire.wav");
325                 //precache_sound ("weapons/reload.wav"); // until weapons have individual reload sounds, precache the reload sound somewhere else
326         }
327         else if (req == WR_SETUP)
328         {
329                 weapon_setup(WEP_LASER);
330                 self.current_ammo = ammo_none;
331         }
332         else if (req == WR_CHECKAMMO1)
333         {
334                 return TRUE;
335         }
336         else if (req == WR_CHECKAMMO2)
337         {
338                 return TRUE;
339         }
340         else if (req == WR_RELOAD)
341         {
342                 W_Reload(0, autocvar_g_balance_laser_reload_ammo, autocvar_g_balance_laser_reload_time, "weapons/reload.wav");
343         }
344         return TRUE;
345 }
346 #endif
347 #ifdef CSQC
348 float w_laser(float req)
349 {
350         if(req == WR_IMPACTEFFECT)
351         {
352                 vector org2;
353                 org2 = w_org + w_backoff * 6;
354                 pointparticles(particleeffectnum("new_laser_impact"), org2, w_backoff * 1000, 1);
355                 if(!w_issilent)
356                         sound(self, CH_SHOTS, "weapons/laserimpact.wav", VOL_BASE, ATTN_NORM);
357         }
358         else if(req == WR_PRECACHE)
359         {
360                 precache_sound("weapons/laserimpact.wav");
361         }
362         else if (req == WR_SUICIDEMESSAGE)
363                 w_deathtypestring = _("%s lasered themself to hell");
364         else if (req == WR_KILLMESSAGE)
365         {
366                 if(w_deathtype & HITTYPE_SECONDARY)
367                         w_deathtypestring = _("%s was cut in half by %s's gauntlet"); // unchecked: SPLASH // TODO 
368                 else
369                         w_deathtypestring = _("%s was lasered to death by %s"); // unchecked: SPLASH
370         }
371         return TRUE;
372 }
373 #endif
374 #endif