Merge branch 'master' into Mario/vaporizer_damage
[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 {
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(EFFECT_LASER_DEADLY);
225                 }
226         }
227         else if(!self.cnt)
228         {
229                 if(self.dmg)
230                         self.cnt = particleeffectnum(EFFECT_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 entityclass(Laser);
273 class(Laser) .int cnt; // end effect
274 class(Laser) .vector colormod;
275 class(Laser) .int state; // on-off
276 class(Laser) .int count; // flags for the laser
277 class(Laser) .vector velocity;
278 class(Laser) .float alpha;
279 class(Laser) .float scale; // scaling factor of the thickness
280 class(Laser) .float modelscale; // scaling factor of the dlight
281
282 void Draw_Laser()
283 {
284         if(!self.state)
285                 return;
286         InterpolateOrigin_Do();
287         if(self.count & 0x80)
288         {
289                 if(self.count & 0x10)
290                 {
291                         trace_endpos = self.velocity;
292                         trace_dphitq3surfaceflags = 0;
293                 }
294                 else
295                         traceline(self.origin, self.velocity, 0, self);
296         }
297         else
298         {
299                 if(self.count & 0x10)
300                 {
301                         makevectors(self.angles);
302                         trace_endpos = self.origin + v_forward * 1048576;
303                         trace_dphitq3surfaceflags = Q3SURFACEFLAG_SKY;
304                 }
305                 else
306                 {
307                         makevectors(self.angles);
308                         traceline(self.origin, self.origin + v_forward * 32768, 0, self);
309                         if(trace_dphitq3surfaceflags & Q3SURFACEFLAG_SKY)
310                                 trace_endpos = self.origin + v_forward * 1048576;
311                 }
312         }
313         if(self.scale != 0)
314         {
315                 if(self.alpha)
316                 {
317                         Draw_CylindricLine(self.origin, trace_endpos, self.scale, "particles/laserbeam", 0, time * 3, self.colormod, self.alpha, DRAWFLAG_NORMAL, view_origin);
318                 }
319                 else
320                 {
321                         Draw_CylindricLine(self.origin, trace_endpos, self.scale, "particles/laserbeam", 0, time * 3, self.colormod, 0.5, DRAWFLAG_ADDITIVE, view_origin);
322                 }
323         }
324         if (!(trace_dphitq3surfaceflags & (Q3SURFACEFLAG_SKY | Q3SURFACEFLAG_NOIMPACT)))
325         {
326                 if(self.cnt >= 0)
327                         pointparticles(self.cnt, trace_endpos, trace_plane_normal, drawframetime * 1000);
328                 if(self.colormod != '0 0 0' && self.modelscale != 0)
329                         adddynamiclight(trace_endpos + trace_plane_normal * 1, self.modelscale, self.colormod * 5);
330         }
331 }
332
333 void Ent_Laser()
334 {
335         InterpolateOrigin_Undo();
336
337         // 30 bytes, or 13 bytes for just moving
338         int f = ReadByte();
339         self.count = (f & 0xF0);
340
341         if(self.count & 0x80)
342                 self.iflags = IFLAG_VELOCITY | IFLAG_ORIGIN;
343         else
344                 self.iflags = IFLAG_ANGLES | IFLAG_ORIGIN;
345
346         if(f & 1)
347         {
348                 self.origin_x = ReadCoord();
349                 self.origin_y = ReadCoord();
350                 self.origin_z = ReadCoord();
351                 setorigin(self, self.origin);
352         }
353         if(f & 8)
354         {
355                 self.colormod_x = ReadByte() / 255.0;
356                 self.colormod_y = ReadByte() / 255.0;
357                 self.colormod_z = ReadByte() / 255.0;
358                 if(f & 0x40)
359                         self.alpha = ReadByte() / 255.0;
360                 else
361                         self.alpha = 0;
362                 self.scale = 2;
363                 self.modelscale = 50;
364                 if(f & 0x20)
365                 {
366                         self.scale *= ReadByte() / 16.0; // beam radius
367                         self.modelscale *= ReadByte() / 16.0; // dlight radius
368                 }
369                 if((f & 0x80) || !(f & 0x10))
370                         self.cnt = ReadShort() - 1; // effect number
371                 else
372                         self.cnt = 0;
373         }
374         if(f & 2)
375         {
376                 if(f & 0x80)
377                 {
378                         self.velocity_x = ReadCoord();
379                         self.velocity_y = ReadCoord();
380                         self.velocity_z = ReadCoord();
381                 }
382                 else
383                 {
384                         self.angles_x = ReadAngle();
385                         self.angles_y = ReadAngle();
386                 }
387         }
388         if(f & 4)
389                 self.state = ReadByte();
390         InterpolateOrigin_Note();
391         self.draw = Draw_Laser;
392 }
393 #endif