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