]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/triggers/misc/laser.qc
Don't even try to translate keys if game isn't translated
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / triggers / misc / laser.qc
1 #include "laser.qh"
2 #if defined(CSQC)
3         #include <lib/csqcmodel/interpolate.qh>
4         #include <client/main.qh>
5         #include <lib/csqcmodel/cl_model.qh>
6 #elif defined(MENUQC)
7 #elif defined(SVQC)
8 #endif
9
10 REGISTER_NET_LINKED(ENT_CLIENT_LASER)
11
12 #ifdef SVQC
13 .float modelscale;
14 void misc_laser_aim(entity this)
15 {
16         vector a;
17         if(this.enemy)
18         {
19                 if(this.spawnflags & 2)
20                 {
21                         if(this.enemy.origin != this.mangle)
22                         {
23                                 this.mangle = this.enemy.origin;
24                                 this.SendFlags |= 2;
25                         }
26                 }
27                 else
28                 {
29                         a = vectoangles(this.enemy.origin - this.origin);
30                         a_x = -a_x;
31                         if(a != this.mangle)
32                         {
33                                 this.mangle = a;
34                                 this.SendFlags |= 2;
35                         }
36                 }
37         }
38         else
39         {
40                 if(this.angles != this.mangle)
41                 {
42                         this.mangle = this.angles;
43                         this.SendFlags |= 2;
44                 }
45         }
46         if(this.origin != this.oldorigin)
47         {
48                 this.SendFlags |= 1;
49                 this.oldorigin = this.origin;
50         }
51 }
52
53 void misc_laser_init(entity this)
54 {
55         if(this.target != "")
56                 this.enemy = find(NULL, targetname, this.target);
57 }
58
59 .entity pusher;
60 void misc_laser_think(entity this)
61 {
62         vector o;
63         entity hitent;
64         vector hitloc;
65
66         this.nextthink = time;
67
68         if(!this.state)
69                 return;
70
71         misc_laser_aim(this);
72
73         if(this.enemy)
74         {
75                 o = this.enemy.origin;
76                 if (!(this.spawnflags & 2))
77                         o = this.origin + normalize(o - this.origin) * 32768;
78         }
79         else
80         {
81                 makevectors(this.mangle);
82                 o = this.origin + v_forward * 32768;
83         }
84
85         if(this.dmg || this.enemy.target != "")
86         {
87                 traceline(this.origin, o, MOVE_NORMAL, this);
88         }
89         hitent = trace_ent;
90         hitloc = trace_endpos;
91
92         if(this.enemy.target != "") // DETECTOR laser
93         {
94                 if(trace_ent.iscreature)
95                 {
96                         this.pusher = hitent;
97                         if(!this.count)
98                         {
99                                 this.count = 1;
100
101                                 SUB_UseTargets(this.enemy, this.enemy.pusher, NULL);
102                         }
103                 }
104                 else
105                 {
106                         if(this.count)
107                         {
108                                 this.count = 0;
109
110                                 SUB_UseTargets(this.enemy, this.enemy.pusher, NULL);
111                         }
112                 }
113         }
114
115         if(this.dmg)
116         {
117                 if(this.team)
118                         if(((this.spawnflags & 8) == 0) == (this.team != hitent.team))
119                                 return;
120                 if(hitent.takedamage)
121                         Damage(hitent, this, this, ((this.dmg < 0) ? 100000 : (this.dmg * frametime)), DEATH_HURTTRIGGER.m_id, DMG_NOWEP, hitloc, '0 0 0');
122         }
123 }
124
125 bool laser_SendEntity(entity this, entity to, float fl)
126 {
127         WriteHeader(MSG_ENTITY, ENT_CLIENT_LASER);
128         fl = fl - (fl & 0xF0); // use that bit to indicate finite length laser
129         if(this.spawnflags & 2)
130                 fl |= 0x80;
131         if(this.alpha)
132                 fl |= 0x40;
133         if(this.scale != 1 || this.modelscale != 1)
134                 fl |= 0x20;
135         if(this.spawnflags & 4)
136                 fl |= 0x10;
137         WriteByte(MSG_ENTITY, fl);
138         if(fl & 1)
139         {
140                 WriteVector(MSG_ENTITY, this.origin);
141         }
142         if(fl & 8)
143         {
144                 WriteByte(MSG_ENTITY, this.colormod_x * 255.0);
145                 WriteByte(MSG_ENTITY, this.colormod_y * 255.0);
146                 WriteByte(MSG_ENTITY, this.colormod_z * 255.0);
147                 if(fl & 0x40)
148                         WriteByte(MSG_ENTITY, this.alpha * 255.0);
149                 if(fl & 0x20)
150                 {
151                         WriteByte(MSG_ENTITY, bound(0, this.scale * 16.0, 255));
152                         WriteByte(MSG_ENTITY, bound(0, this.modelscale * 16.0, 255));
153                 }
154                 if((fl & 0x80) || !(fl & 0x10)) // effect doesn't need sending if the laser is infinite and has collision testing turned off
155                         WriteShort(MSG_ENTITY, this.cnt + 1);
156         }
157         if(fl & 2)
158         {
159                 if(fl & 0x80)
160                 {
161                         WriteVector(MSG_ENTITY, this.enemy.origin);
162                 }
163                 else
164                 {
165                         WriteAngle(MSG_ENTITY, this.mangle_x);
166                         WriteAngle(MSG_ENTITY, this.mangle_y);
167                 }
168         }
169         if(fl & 4)
170                 WriteByte(MSG_ENTITY, this.state);
171         return 1;
172 }
173
174 /*QUAKED spawnfunc_misc_laser (.5 .5 .5) ? START_ON DEST_IS_FIXED
175 Any object touching the beam will be hurt
176 Keys:
177 "target"
178  spawnfunc_target_position where the laser ends
179 "mdl"
180  name of beam end effect to use
181 "colormod"
182  color of the beam (default: red)
183 "dmg"
184  damage per second (-1 for a laser that kills immediately)
185 */
186 void laser_use(entity this, entity actor, entity trigger)
187 {
188         this.state = !this.state;
189         this.SendFlags |= 4;
190         misc_laser_aim(this);
191 }
192
193 void laser_reset(entity this)
194 {
195         if(this.spawnflags & 1)
196                 this.state = 1;
197         else
198                 this.state = 0;
199 }
200
201 spawnfunc(misc_laser)
202 {
203         if(this.mdl)
204         {
205                 if(this.mdl == "none")
206                         this.cnt = -1;
207                 else
208                 {
209                         this.cnt = _particleeffectnum(this.mdl);
210                         if(this.cnt < 0 && this.dmg)
211                 this.cnt = particleeffectnum(EFFECT_LASER_DEADLY);
212                 }
213         }
214         else if(!this.cnt)
215         {
216                 if(this.dmg)
217                         this.cnt = particleeffectnum(EFFECT_LASER_DEADLY);
218                 else
219                         this.cnt = -1;
220         }
221         if(this.cnt < 0)
222                 this.cnt = -1;
223
224         if(this.colormod == '0 0 0')
225                 if(!this.alpha)
226                         this.colormod = '1 0 0';
227         if(this.message == "") this.message = "saw the light";
228         if (this.message2 == "") this.message2 = "was pushed into a laser by";
229         if(!this.scale) this.scale = 1;
230         if(!this.modelscale) this.modelscale = 1;
231         else if(this.modelscale < 0) this.modelscale = 0;
232         setthink(this, misc_laser_think);
233         this.nextthink = time;
234         InitializeEntity(this, misc_laser_init, INITPRIO_FINDTARGET);
235
236         this.mangle = this.angles;
237
238         Net_LinkEntity(this, false, 0, laser_SendEntity);
239
240         IFTARGETED
241         {
242                 this.reset = laser_reset;
243                 this.reset(this);
244                 this.use = laser_use;
245         }
246         else
247                 this.state = 1;
248 }
249 #elif defined(CSQC)
250
251 // a laser goes from origin in direction angles
252 // it has color 'colormod'
253 // and stops when something is in the way
254 entityclass(Laser);
255 classfield(Laser) .int cnt; // end effect
256 classfield(Laser) .vector colormod;
257 classfield(Laser) .int state; // on-off
258 classfield(Laser) .int count; // flags for the laser
259 classfield(Laser) .vector velocity;
260 classfield(Laser) .float alpha;
261 classfield(Laser) .float scale; // scaling factor of the thickness
262 classfield(Laser) .float modelscale; // scaling factor of the dlight
263
264 void Draw_Laser(entity this)
265 {
266         if(!this.state)
267                 return;
268         InterpolateOrigin_Do(this);
269         if(this.count & 0x80)
270         {
271                 if(this.count & 0x10)
272                 {
273                         trace_endpos = this.velocity;
274                         trace_dphitq3surfaceflags = 0;
275                 }
276                 else
277                         traceline(this.origin, this.velocity, 0, this);
278         }
279         else
280         {
281                 if(this.count & 0x10)
282                 {
283                         makevectors(this.angles);
284                         trace_endpos = this.origin + v_forward * 1048576;
285                         trace_dphitq3surfaceflags = Q3SURFACEFLAG_SKY;
286                 }
287                 else
288                 {
289                         makevectors(this.angles);
290                         traceline(this.origin, this.origin + v_forward * 32768, 0, this);
291                         if(trace_dphitq3surfaceflags & Q3SURFACEFLAG_SKY)
292                                 trace_endpos = this.origin + v_forward * 1048576;
293                 }
294         }
295         if(this.scale != 0)
296         {
297                 if(this.alpha)
298                 {
299                         Draw_CylindricLine(this.origin, trace_endpos, this.scale, "particles/laserbeam", 0, time * 3, this.colormod, this.alpha, DRAWFLAG_NORMAL, view_origin);
300                 }
301                 else
302                 {
303                         Draw_CylindricLine(this.origin, trace_endpos, this.scale, "particles/laserbeam", 0, time * 3, this.colormod, 0.5, DRAWFLAG_ADDITIVE, view_origin);
304                 }
305         }
306         if (!(trace_dphitq3surfaceflags & (Q3SURFACEFLAG_SKY | Q3SURFACEFLAG_NOIMPACT)))
307         {
308                 if(this.cnt >= 0)
309                         __pointparticles(this.cnt, trace_endpos, trace_plane_normal, drawframetime * 1000);
310                 if(this.colormod != '0 0 0' && this.modelscale != 0)
311                         adddynamiclight(trace_endpos + trace_plane_normal * 1, this.modelscale, this.colormod * 5);
312         }
313 }
314
315 NET_HANDLE(ENT_CLIENT_LASER, bool isnew)
316 {
317         InterpolateOrigin_Undo(this);
318
319         // 30 bytes, or 13 bytes for just moving
320         int f = ReadByte();
321         this.count = (f & 0xF0);
322
323         if(this.count & 0x80)
324                 this.iflags = IFLAG_VELOCITY | IFLAG_ORIGIN;
325         else
326                 this.iflags = IFLAG_ANGLES | IFLAG_ORIGIN;
327
328         if(f & 1)
329         {
330                 this.origin = ReadVector();
331                 setorigin(this, this.origin);
332         }
333         if(f & 8)
334         {
335                 this.colormod_x = ReadByte() / 255.0;
336                 this.colormod_y = ReadByte() / 255.0;
337                 this.colormod_z = ReadByte() / 255.0;
338                 if(f & 0x40)
339                         this.alpha = ReadByte() / 255.0;
340                 else
341                         this.alpha = 0;
342                 this.scale = 2;
343                 this.modelscale = 50;
344                 if(f & 0x20)
345                 {
346                         this.scale *= ReadByte() / 16.0; // beam radius
347                         this.modelscale *= ReadByte() / 16.0; // dlight radius
348                 }
349                 if((f & 0x80) || !(f & 0x10))
350                         this.cnt = ReadShort() - 1; // effect number
351                 else
352                         this.cnt = 0;
353         }
354         if(f & 2)
355         {
356                 if(f & 0x80)
357                 {
358                         this.velocity = ReadVector();
359                 }
360                 else
361                 {
362                         this.angles_x = ReadAngle();
363                         this.angles_y = ReadAngle();
364                 }
365         }
366         if(f & 4)
367                 this.state = ReadByte();
368
369         return = true;
370
371         InterpolateOrigin_Note(this);
372         this.draw = Draw_Laser;
373         if (isnew) IL_PUSH(g_drawables, this);
374 }
375 #endif