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