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