]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/w_laser.qc
Merge branch 'master' into mirceakitsune/universal_reload_system
[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_CANCLIMB | WEP_TYPE_SPLASH, 0, "laser", "laser", _("Laser"))
3 #else
4 #ifdef SVQC
5 void(float imp) W_SwitchWeapon;
6
7 // weapon load persistence, for weapons that support reloading
8 .float laser_load;
9
10 void W_Laser_SetAmmoCounter()
11 {
12         // set clip_load to the weapon we have switched to, if the gun uses reloading
13         if(!autocvar_g_balance_laser_reload_ammo)
14                 self.clip_load = 0; // also keeps crosshair ammo from displaying
15         else
16         {
17                 self.clip_load = self.laser_load;
18                 self.clip_size = autocvar_g_balance_laser_reload_ammo; // for the crosshair ammo display
19         }
20 }
21
22 void W_Laser_ReloadedAndReady()
23 {
24         float t;
25
26         self.clip_load = autocvar_g_balance_laser_reload_ammo; // maximum load since this weapon uses no ammo
27         self.laser_load = self.clip_load;
28
29         t = ATTACK_FINISHED(self) - autocvar_g_balance_laser_reload_time - 1;
30         ATTACK_FINISHED(self) = t;
31         w_ready();
32 }
33
34 void W_Laser_Reload()
35 {
36         // return if reloading is disabled for this weapon
37         if(!autocvar_g_balance_laser_reload_ammo)
38                 return;
39
40         if(!W_ReloadCheck(1, 0))
41                 return;
42
43         float t;
44
45         sound (self, CHAN_WEAPON2, "weapons/reload.wav", VOL_BASE, ATTN_NORM);
46
47         t = max(time, ATTACK_FINISHED(self)) + autocvar_g_balance_laser_reload_time + 1;
48         ATTACK_FINISHED(self) = t;
49
50         weapon_thinkf(WFRAME_RELOAD, autocvar_g_balance_laser_reload_time, W_Laser_ReloadedAndReady);
51
52         self.clip_load = -1;
53 }
54
55 void W_Laser_Touch (void)
56 {
57         PROJECTILE_TOUCH;
58
59         self.event_damage = SUB_Null;
60         if (self.dmg)
61                 RadiusDamage (self, self.owner, 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);
62         else
63                 RadiusDamage (self, self.owner, 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);
64
65         remove (self);
66 }
67
68 void W_Laser_Think()
69 {
70         self.movetype = MOVETYPE_FLY;
71         self.think = SUB_Remove;
72         if (self.dmg)
73                 self.nextthink = time + autocvar_g_balance_laser_secondary_lifetime;
74         else
75                 self.nextthink = time + autocvar_g_balance_laser_primary_lifetime;
76         CSQCProjectile(self, TRUE, PROJECTILE_LASER, TRUE);
77 }
78
79 void W_Laser_Attack (float issecondary)
80 {
81         local entity missile;
82         vector s_forward;
83         float a;
84         float nodamage;
85
86         if(issecondary == 2) // minstanex shot
87                 nodamage = g_minstagib;
88         else
89                 nodamage = FALSE;
90
91         a = autocvar_g_balance_laser_primary_shotangle;
92         s_forward = v_forward * cos(a * DEG2RAD) + v_up * sin(a * DEG2RAD);
93
94         if(nodamage)
95                 W_SetupShot_Dir (self, s_forward, FALSE, 3, "weapons/lasergun_fire.wav", CHAN_WEAPON2, 0);
96         else if(issecondary == 1)
97                 W_SetupShot_Dir (self, s_forward, FALSE, 3, "weapons/lasergun_fire.wav", CHAN_WEAPON2, autocvar_g_balance_laser_secondary_damage);
98         else
99                 W_SetupShot_Dir (self, s_forward, FALSE, 3, "weapons/lasergun_fire.wav", CHAN_WEAPON2, autocvar_g_balance_laser_primary_damage);
100         pointparticles(particleeffectnum("laser_muzzleflash"), w_shotorg, w_shotdir * 1000, 1);
101
102         missile = spawn ();
103         missile.owner = self;
104         missile.classname = "laserbolt";
105         missile.dmg = 0;
106         if(!nodamage)
107         {
108                 missile.bot_dodge = TRUE;
109                 missile.bot_dodgerating = autocvar_g_balance_laser_primary_damage;
110         }
111
112         PROJECTILE_MAKETRIGGER(missile);
113         missile.projectiledeathtype = WEP_LASER;
114
115         setorigin (missile, w_shotorg);
116         setsize(missile, '0 0 0', '0 0 0');
117
118         W_SETUPPROJECTILEVELOCITY(missile, g_balance_laser_primary);
119         missile.angles = vectoangles (missile.velocity);
120         //missile.glow_color = 250; // 244, 250
121         //missile.glow_size = 120;
122         missile.touch = W_Laser_Touch;
123
124         missile.flags = FL_PROJECTILE;
125
126         missile.think = W_Laser_Think;
127         missile.nextthink = time + autocvar_g_balance_laser_primary_delay;
128
129         other = missile; MUTATOR_CALLHOOK(EditProjectile);
130
131         if(time >= missile.nextthink)
132         {
133                 entity oldself;
134                 oldself = self;
135                 self = missile;
136                 self.think();
137                 self = oldself;
138         }
139 }
140
141 .vector hook_start, hook_end;
142 float gauntletbeam_send(entity to, float sf)
143 {
144         WriteByte(MSG_ENTITY, ENT_CLIENT_GAUNTLET);
145         sf = sf & 0x7F;
146         if(sound_allowed(MSG_BROADCAST, self.owner))
147                 sf |= 0x80;
148         WriteByte(MSG_ENTITY, sf);
149         if(sf & 1)
150         {
151                 WriteByte(MSG_ENTITY, num_for_edict(self.owner));
152         }
153         if(sf & 2)
154         {
155                 WriteCoord(MSG_ENTITY, self.hook_start_x);
156                 WriteCoord(MSG_ENTITY, self.hook_start_y);
157                 WriteCoord(MSG_ENTITY, self.hook_start_z);
158         }
159         if(sf & 4)
160         {
161                 WriteCoord(MSG_ENTITY, self.hook_end_x);
162                 WriteCoord(MSG_ENTITY, self.hook_end_y);
163                 WriteCoord(MSG_ENTITY, self.hook_end_z);
164         }
165         return TRUE;
166 }
167 .entity gauntletbeam;
168 .float prevgauntletfire;
169 void gauntletbeam_think()
170 {
171         float damage, myforce, myradius;
172         damage = autocvar_g_balance_laser_secondary_damage;
173         myforce = autocvar_g_balance_laser_secondary_force;
174         myradius = autocvar_g_balance_laser_secondary_radius;
175
176         self.owner.prevgauntletfire = time;
177         if (self.owner.weaponentity.state != WS_INUSE || self != self.owner.gauntletbeam || self.owner.deadflag != DEAD_NO || !self.owner.BUTTON_ATCK2)
178         {
179                 remove(self);
180                 return;
181         }
182
183         self.nextthink = time;
184
185         makevectors(self.owner.v_angle);
186
187         float dt;
188         dt = frametime;
189
190         W_SetupShot_Range(self.owner, TRUE, 0, "", 0, damage * dt, myradius);
191         WarpZone_traceline_antilag(self.owner, w_shotorg, w_shotend, MOVE_NORMAL, self.owner, ANTILAG_LATENCY(self.owner));
192
193         // apply the damage
194         if(trace_ent)
195         {
196                 vector force;
197                 force = w_shotdir * myforce;
198                 if(accuracy_isgooddamage(self.owner, trace_ent))
199                         accuracy_add(self.owner, WEP_LASER, 0, damage * dt);
200                 Damage (trace_ent, self.owner, self.owner, damage * dt, WEP_LASER | HITTYPE_SECONDARY, trace_endpos, force * dt);
201         }
202
203         // draw effect
204         if(w_shotorg != self.hook_start)
205         {
206                 self.SendFlags |= 2;
207                 self.hook_start = w_shotorg;
208         }
209         if(w_shotend != self.hook_end)
210         {
211                 self.SendFlags |= 4;
212                 self.hook_end = w_shotend;
213         }
214 }
215
216 // experimental gauntlet
217 void W_Laser_Attack2 ()
218 {
219         // only play fire sound if 0.5 sec has passed since player let go the fire button
220         if(time - self.prevgauntletfire > 0.5)
221         {
222                 sound (self, CHAN_WEAPON, "weapons/gauntlet_fire.wav", VOL_BASE, ATTN_NORM);
223         }
224
225         entity beam, oldself;
226
227         self.gauntletbeam = beam = spawn();
228         beam.solid = SOLID_NOT;
229         beam.think = gauntletbeam_think;
230         beam.owner = self;
231         beam.movetype = MOVETYPE_NONE;
232         beam.shot_spread = 0;
233         beam.bot_dodge = TRUE;
234         beam.bot_dodgerating = autocvar_g_balance_laser_primary_damage;
235         Net_LinkEntity(beam, FALSE, 0, gauntletbeam_send);
236
237         oldself = self;
238         self = beam;
239         self.think();
240         self = oldself;
241 }
242
243 void LaserInit()
244 {
245         weapon_action(WEP_LASER, WR_PRECACHE);
246         gauntlet_shotorigin[0] = shotorg_adjust_values(CL_Weapon_GetShotOrg(WEP_LASER), FALSE, FALSE, 1);
247         gauntlet_shotorigin[1] = shotorg_adjust_values(CL_Weapon_GetShotOrg(WEP_LASER), FALSE, FALSE, 2);
248         gauntlet_shotorigin[2] = shotorg_adjust_values(CL_Weapon_GetShotOrg(WEP_LASER), FALSE, FALSE, 3);
249         gauntlet_shotorigin[3] = shotorg_adjust_values(CL_Weapon_GetShotOrg(WEP_LASER), FALSE, FALSE, 4);
250 }
251
252 void spawnfunc_weapon_laser (void)
253 {
254         weapon_defaultspawnfunc(WEP_LASER);
255 }
256
257 float w_laser(float req)
258 {
259         local float r1;
260         local float r2;
261         if (req == WR_AIM)
262         {
263                 if(autocvar_g_balance_laser_secondary)
264                 {
265                         r1 = autocvar_g_balance_laser_primary_damage;
266                         r2 = autocvar_g_balance_laser_secondary_damage;
267                         if (random() * (r2 + r1) > r1)
268                                 self.BUTTON_ATCK2 = bot_aim(autocvar_g_balance_laser_secondary_speed, 0, autocvar_g_balance_laser_secondary_lifetime, FALSE);
269                         else
270                                 self.BUTTON_ATCK = bot_aim(autocvar_g_balance_laser_primary_speed, 0, autocvar_g_balance_laser_primary_lifetime, FALSE);
271                 }
272                 else
273                         self.BUTTON_ATCK = bot_aim(autocvar_g_balance_laser_primary_speed, 0, autocvar_g_balance_laser_primary_lifetime, FALSE);
274         }
275         else if (req == WR_THINK)
276         {
277                 if(autocvar_g_balance_laser_reload_ammo && self.clip_load < 1) // forced reload
278                         W_Laser_Reload();
279                 else if (self.BUTTON_ATCK)
280                 {
281                         if (weapon_prepareattack(0, autocvar_g_balance_laser_primary_refire))
282                         {
283                                 // if this weapon is reloadable, decrease its load
284                                 if not(self.items & IT_UNLIMITED_WEAPON_AMMO)
285                                 {
286                                         if(autocvar_g_balance_laser_reload_ammo)
287                                         {
288                                                 self.clip_load -= 1;
289                                                 self.laser_load = self.clip_load;
290                                         }
291                                 }
292
293                                 W_Laser_Attack(0);
294                                 weapon_thinkf(WFRAME_FIRE1, autocvar_g_balance_laser_primary_animtime, w_ready);
295                         }
296                 }
297                 else if (self.BUTTON_ATCK2)
298                 {
299                         if(autocvar_g_balance_laser_secondary)
300                         {
301                                 // if this weapon is reloadable, decrease its load
302                                 if not(self.items & IT_UNLIMITED_WEAPON_AMMO)
303                                 {
304                                         if(autocvar_g_balance_laser_reload_ammo)
305                                         {
306                                                 self.clip_load -= 1;
307                                                 self.laser_load = self.clip_load;
308                                         }
309                                 }
310
311                                 if (weapon_prepareattack(0, 0))
312                                 {
313                                         W_Laser_Attack2();
314                                         weapon_thinkf(WFRAME_FIRE2, autocvar_g_balance_laser_secondary_animtime, w_ready);
315                                 }
316                         }
317                         else
318                         {
319                                 if(self.switchweapon == WEP_LASER) // don't do this if already switching
320                                         W_SwitchWeapon (self.cnt);
321                         }
322                 }
323         }
324         else if (req == WR_PRECACHE)
325         {
326                 precache_model ("models/weapons/g_laser.md3");
327                 precache_model ("models/weapons/v_laser.md3");
328                 precache_model ("models/weapons/h_laser.iqm");
329                 precache_sound ("weapons/lasergun_fire.wav");
330                 precache_sound ("weapons/gauntlet_fire.wav");
331                 precache_sound ("weapons/reload.wav");
332         }
333         else if (req == WR_SETUP)
334         {
335                 weapon_setup(WEP_LASER);
336                 W_Laser_SetAmmoCounter();
337         }
338         else if (req == WR_CHECKAMMO1)
339         {
340                 return TRUE;
341         }
342         else if (req == WR_CHECKAMMO2)
343         {
344                 return TRUE;
345         }
346         else if (req == WR_RESETPLAYER)
347         {
348                 // all weapons must be fully loaded when we spawn
349                 self.laser_load = autocvar_g_balance_laser_reload_ammo;
350         }
351         else if (req == WR_RELOAD)
352         {
353                 W_Laser_Reload();
354         }
355         return TRUE;
356 };
357 #endif
358 #ifdef CSQC
359 float w_laser(float req)
360 {
361         if(req == WR_IMPACTEFFECT)
362         {
363                 vector org2;
364                 org2 = w_org + w_backoff * 6;
365                 pointparticles(particleeffectnum("laser_impact"), org2, w_backoff * 1000, 1);
366                 if(!w_issilent)
367                         sound(self, CHAN_PROJECTILE, "weapons/laserimpact.wav", VOL_BASE, ATTN_NORM);
368         }
369         else if(req == WR_PRECACHE)
370         {
371                 precache_sound("weapons/laserimpact.wav");
372         }
373         else if (req == WR_SUICIDEMESSAGE)
374                 w_deathtypestring = _("%s lasered themself to hell");
375         else if (req == WR_KILLMESSAGE)
376         {
377                 if(w_deathtype & HITTYPE_SECONDARY)
378                         w_deathtypestring = _("%s was cut in half by %s's gauntlet"); // unchecked: SPLASH
379                 else
380                         w_deathtypestring = _("%s was lasered to death by %s"); // unchecked: SPLASH
381         }
382         return TRUE;
383 }
384 #endif
385 #endif