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