]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/triggers/misc/laser.qc
Explicit check for ACTIVE_NOT
[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 & LASER_FINITE)
20                 {
21                         if(this.enemy.origin != this.mangle)
22                         {
23                                 this.mangle = this.enemy.origin;
24                                 this.SendFlags |= SF_LASER_UPDATE_TARGET;
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 |= SF_LASER_UPDATE_TARGET;
35                         }
36                 }
37         }
38         else
39         {
40                 if(this.angles != this.mangle)
41                 {
42                         this.mangle = this.angles;
43                         this.SendFlags |= SF_LASER_UPDATE_TARGET;
44                 }
45         }
46         if(this.origin != this.oldorigin)
47         {
48                 this.SendFlags |= SF_LASER_UPDATE_ORIGIN;
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.active == ACTIVE_NOT)
69                 return;
70
71         misc_laser_aim(this);
72
73         if(this.enemy)
74         {
75                 o = this.enemy.origin;
76                 if (!(this.spawnflags & LASER_FINITE))
77                         o = this.origin + normalize(o - this.origin) * LASER_BEAM_MAXLENGTH;
78         }
79         else
80         {
81                 makevectors(this.mangle);
82                 o = this.origin + v_forward * LASER_BEAM_MAXLENGTH;
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 & LASER_INVERT_TEAM) == 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 sendflags)
126 {
127         WriteHeader(MSG_ENTITY, ENT_CLIENT_LASER);
128         sendflags = sendflags & 0x0F; // use that bit to indicate finite length laser
129         if(this.spawnflags & LASER_FINITE)
130                 sendflags |= SF_LASER_FINITE;
131         if(this.alpha)
132                 sendflags |= SF_LASER_ALPHA;
133         if(this.scale != 1 || this.modelscale != 1)
134                 sendflags |= SF_LASER_SCALE;
135         if(this.spawnflags & LASER_NOTRACE)
136                 sendflags |= SF_LASER_NOTRACE;
137         WriteByte(MSG_ENTITY, sendflags);
138         if(sendflags & SF_LASER_UPDATE_ORIGIN)
139         {
140                 WriteVector(MSG_ENTITY, this.origin);
141         }
142         if(sendflags & SF_LASER_UPDATE_EFFECT)
143         {
144                 WriteByte(MSG_ENTITY, this.beam_color.x * 255.0);
145                 WriteByte(MSG_ENTITY, this.beam_color.y * 255.0);
146                 WriteByte(MSG_ENTITY, this.beam_color.z * 255.0);
147                 if(sendflags & SF_LASER_ALPHA)
148                         WriteByte(MSG_ENTITY, this.alpha * 255.0);
149                 if(sendflags & SF_LASER_SCALE)
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((sendflags & SF_LASER_FINITE) || !(sendflags & SF_LASER_NOTRACE)) // effect doesn't need sending if the laser is infinite and has collision testing turned off
155                         WriteShort(MSG_ENTITY, this.cnt);
156         }
157         if(sendflags & SF_LASER_UPDATE_TARGET)
158         {
159                 if(sendflags & SF_LASER_FINITE)
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(sendflags & SF_LASER_UPDATE_ACTIVE)
170                 WriteByte(MSG_ENTITY, this.active);
171         return true;
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 "beam_color"
182  color of the beam (default: red)
183 "dmg"
184  damage per second (-1 for a laser that kills immediately)
185 */
186
187 void laser_setactive(entity this, int act)
188 {
189         int old_status = this.active;
190         if(act == ACTIVE_TOGGLE)
191         {
192                 if(this.active == ACTIVE_ACTIVE)
193                 {
194                         this.active = ACTIVE_NOT;
195                 }
196                 else
197                 {
198                         this.active = ACTIVE_ACTIVE;
199                 }
200         }
201         else
202         {
203                 this.active = act;
204         }
205
206         if (this.active != old_status)
207         {
208                 this.SendFlags |= SF_LASER_UPDATE_ACTIVE;
209                 misc_laser_aim(this);
210         }
211 }
212
213 void laser_use(entity this, entity actor, entity trigger)
214 {
215         this.setactive(this, ACTIVE_TOGGLE);
216 }
217
218 spawnfunc(misc_laser)
219 {
220         if(this.mdl)
221         {
222                 if(this.mdl == "none")
223                         this.cnt = -1;
224                 else
225                 {
226                         this.cnt = _particleeffectnum(this.mdl);
227                         if(this.cnt < 0 && this.dmg)
228                 this.cnt = particleeffectnum(EFFECT_LASER_DEADLY);
229                 }
230         }
231         else if(!this.cnt)
232         {
233                 if(this.dmg)
234                         this.cnt = particleeffectnum(EFFECT_LASER_DEADLY);
235                 else
236                         this.cnt = -1;
237         }
238         if(this.cnt < 0)
239                 this.cnt = -1;
240
241         if(!this.beam_color && this.colormod)
242         {
243                 LOG_WARN("misc_laser uses legacy field 'colormod', please use 'beam_color' instead");
244                 this.beam_color = this.colormod;
245         }
246
247         if(this.beam_color == '0 0 0')
248         {
249                 if(!this.alpha)
250                         this.beam_color = '1 0 0';
251         }
252
253         if(this.message == "")
254         {
255                 this.message = "saw the light";
256         }
257         if (this.message2 == "")
258         {
259                 this.message2 = "was pushed into a laser by";
260         }
261         if(!this.scale)
262         {
263                 this.scale = 1;
264         }
265         if(!this.modelscale)
266         {
267                 this.modelscale = 1;
268         }
269         else if(this.modelscale < 0)
270         {
271                 this.modelscale = 0;
272         }
273         setthink(this, misc_laser_think);
274         this.nextthink = time;
275         InitializeEntity(this, misc_laser_init, INITPRIO_FINDTARGET);
276
277         this.mangle = this.angles;
278
279         Net_LinkEntity(this, false, 0, laser_SendEntity);
280
281         this.setactive = laser_setactive;
282
283         IFTARGETED
284         {
285                 // backwards compatibility
286                 this.use = laser_use;
287         }
288
289         this.reset = generic_netlinked_reset;
290         this.reset(this);
291 }
292 #elif defined(CSQC)
293
294 // a laser goes from origin in direction angles
295 // it has color 'beam_color'
296 // and stops when something is in the way
297 entityclass(Laser);
298 class(Laser) .int cnt; // end effect
299 class(Laser) .vector beam_color;
300 class(Laser) .int active; // on-off
301 class(Laser) .int count; // flags for the laser
302 class(Laser) .vector velocity; // laser endpoint if it is FINITE
303 class(Laser) .float alpha;
304 class(Laser) .float scale; // scaling factor of the thickness
305 class(Laser) .float modelscale; // scaling factor of the dlight
306
307 void Draw_Laser(entity this)
308 {
309         if(this.active == ACTIVE_NOT)
310                 return;
311         InterpolateOrigin_Do(this);
312         if(this.count & SF_LASER_FINITE)
313         {
314                 if(this.count & SF_LASER_NOTRACE)
315                 {
316                         trace_endpos = this.velocity;
317                         trace_dphitq3surfaceflags = 0;
318                 }
319                 else
320                         traceline(this.origin, this.velocity, 0, this);
321         }
322         else
323         {
324                 if(this.count & SF_LASER_NOTRACE)
325                 {
326                         makevectors(this.angles);
327                         trace_endpos = this.origin + v_forward * LASER_BEAM_MAXWORLDSIZE;
328                         trace_dphitq3surfaceflags = Q3SURFACEFLAG_SKY;
329                 }
330                 else
331                 {
332                         makevectors(this.angles);
333                         traceline(this.origin, this.origin + v_forward * LASER_BEAM_MAXLENGTH, 0, this);
334                         if(trace_dphitq3surfaceflags & Q3SURFACEFLAG_SKY)
335                                 trace_endpos = this.origin + v_forward * LASER_BEAM_MAXWORLDSIZE;
336                 }
337         }
338         if(this.scale != 0)
339         {
340                 if(this.alpha)
341                 {
342                         Draw_CylindricLine(this.origin, trace_endpos, this.scale, "particles/laserbeam", 0, time * 3, this.beam_color, this.alpha, DRAWFLAG_NORMAL, view_origin);
343                 }
344                 else
345                 {
346                         Draw_CylindricLine(this.origin, trace_endpos, this.scale, "particles/laserbeam", 0, time * 3, this.beam_color, 0.5, DRAWFLAG_ADDITIVE, view_origin);
347                 }
348         }
349         if (!(trace_dphitq3surfaceflags & (Q3SURFACEFLAG_SKY | Q3SURFACEFLAG_NOIMPACT)))
350         {
351                 if(this.cnt >= 0)
352                         __pointparticles(this.cnt, trace_endpos, trace_plane_normal, drawframetime * 1000);
353                 if(this.beam_color != '0 0 0' && this.modelscale != 0)
354                         adddynamiclight(trace_endpos + trace_plane_normal * 1, this.modelscale, this.beam_color * 5);
355         }
356 }
357
358 NET_HANDLE(ENT_CLIENT_LASER, bool isnew)
359 {
360         InterpolateOrigin_Undo(this);
361
362         // 30 bytes, or 13 bytes for just moving
363         int sendflags = ReadByte();
364         this.count = (sendflags & 0xF0);
365
366         if(this.count & SF_LASER_FINITE)
367                 this.iflags = IFLAG_VELOCITY | IFLAG_ORIGIN;
368         else
369                 this.iflags = IFLAG_ANGLES | IFLAG_ORIGIN;
370
371         if(sendflags & SF_LASER_UPDATE_ORIGIN)
372         {
373                 this.origin = ReadVector();
374                 setorigin(this, this.origin);
375         }
376         if(sendflags & SF_LASER_UPDATE_EFFECT)
377         {
378                 this.beam_color.x = ReadByte() / 255.0;
379                 this.beam_color.y = ReadByte() / 255.0;
380                 this.beam_color.z = ReadByte() / 255.0;
381                 if(sendflags & SF_LASER_ALPHA)
382                         this.alpha = ReadByte() / 255.0;
383                 else
384                         this.alpha = 0;
385                 this.scale = 2; // NOTE: why 2?
386                 this.modelscale = 50; // NOTE: why 50?
387                 if(sendflags & SF_LASER_SCALE)
388                 {
389                         this.scale *= ReadByte() / 16.0; // beam radius
390                         this.modelscale *= ReadByte() / 16.0; // dlight radius
391                 }
392                 if((sendflags & SF_LASER_FINITE) || !(sendflags & SF_LASER_NOTRACE))
393                         this.cnt = ReadShort(); // effect number
394                 else
395                         this.cnt = 0;
396         }
397         if(sendflags & SF_LASER_UPDATE_TARGET)
398         {
399                 if(sendflags & SF_LASER_FINITE)
400                 {
401                         this.velocity = ReadVector();
402                 }
403                 else
404                 {
405                         this.angles_x = ReadAngle();
406                         this.angles_y = ReadAngle();
407                 }
408         }
409         if(sendflags & SF_LASER_UPDATE_ACTIVE)
410                 this.active = ReadByte();
411
412         return = true;
413
414         InterpolateOrigin_Note(this);
415         this.draw = Draw_Laser;
416         if (isnew) IL_PUSH(g_drawables, this);
417 }
418 #endif