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