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