]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/triggers/misc/laser.qc
Merge branch 'terencehill/menu_optimization' into 'master'
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / triggers / misc / laser.qc
1 #if defined(CSQC)
2         #include <lib/csqcmodel/interpolate.qh>
3         #include <client/main.qh>
4         #include <lib/csqcmodel/cl_model.qh>
5 #elif defined(MENUQC)
6 #elif defined(SVQC)
7 #endif
8
9 REGISTER_NET_LINKED(ENT_CLIENT_LASER)
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.m_id, hitloc, '0 0 0');
123         }
124 }
125
126 bool laser_SendEntity(entity this, entity to, float fl)
127 {
128         WriteHeader(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(entity this)
199 {
200         if(this.spawnflags & 1)
201                 this.state = 1;
202         else
203                 this.state = 0;
204 }
205
206 spawnfunc(misc_laser)
207 {
208         if(this.mdl)
209         {
210                 if(this.mdl == "none")
211                         this.cnt = -1;
212                 else
213                 {
214                         this.cnt = _particleeffectnum(this.mdl);
215                         if(this.cnt < 0 && this.dmg)
216                 this.cnt = particleeffectnum(EFFECT_LASER_DEADLY);
217                 }
218         }
219         else if(!this.cnt)
220         {
221                 if(this.dmg)
222                         this.cnt = particleeffectnum(EFFECT_LASER_DEADLY);
223                 else
224                         this.cnt = -1;
225         }
226         if(this.cnt < 0)
227                 this.cnt = -1;
228
229         if(this.colormod == '0 0 0')
230                 if(!this.alpha)
231                         this.colormod = '1 0 0';
232         if(this.message == "") this.message = "saw the light";
233         if (this.message2 == "") this.message2 = "was pushed into a laser by";
234         if(!this.scale) this.scale = 1;
235         if(!this.modelscale) this.modelscale = 1;
236         else if(this.modelscale < 0) this.modelscale = 0;
237         this.think = misc_laser_think;
238         this.nextthink = time;
239         InitializeEntity(this, misc_laser_init, INITPRIO_FINDTARGET);
240
241         this.mangle = this.angles;
242
243         Net_LinkEntity(this, false, 0, laser_SendEntity);
244
245         IFTARGETED
246         {
247                 this.reset = laser_reset;
248                 this.reset(this);
249                 this.use = laser_use;
250         }
251         else
252                 this.state = 1;
253 }
254 #elif defined(CSQC)
255
256 // a laser goes from origin in direction angles
257 // it has color 'colormod'
258 // and stops when something is in the way
259 entityclass(Laser);
260 class(Laser) .int cnt; // end effect
261 class(Laser) .vector colormod;
262 class(Laser) .int state; // on-off
263 class(Laser) .int count; // flags for the laser
264 class(Laser) .vector velocity;
265 class(Laser) .float alpha;
266 class(Laser) .float scale; // scaling factor of the thickness
267 class(Laser) .float modelscale; // scaling factor of the dlight
268
269 void Draw_Laser(entity this)
270 {
271         if(!this.state)
272                 return;
273         InterpolateOrigin_Do(this);
274         if(this.count & 0x80)
275         {
276                 if(this.count & 0x10)
277                 {
278                         trace_endpos = this.velocity;
279                         trace_dphitq3surfaceflags = 0;
280                 }
281                 else
282                         traceline(this.origin, this.velocity, 0, this);
283         }
284         else
285         {
286                 if(this.count & 0x10)
287                 {
288                         makevectors(this.angles);
289                         trace_endpos = this.origin + v_forward * 1048576;
290                         trace_dphitq3surfaceflags = Q3SURFACEFLAG_SKY;
291                 }
292                 else
293                 {
294                         makevectors(this.angles);
295                         traceline(this.origin, this.origin + v_forward * 32768, 0, this);
296                         if(trace_dphitq3surfaceflags & Q3SURFACEFLAG_SKY)
297                                 trace_endpos = this.origin + v_forward * 1048576;
298                 }
299         }
300         if(this.scale != 0)
301         {
302                 if(this.alpha)
303                 {
304                         Draw_CylindricLine(this.origin, trace_endpos, this.scale, "particles/laserbeam", 0, time * 3, this.colormod, this.alpha, DRAWFLAG_NORMAL, view_origin);
305                 }
306                 else
307                 {
308                         Draw_CylindricLine(this.origin, trace_endpos, this.scale, "particles/laserbeam", 0, time * 3, this.colormod, 0.5, DRAWFLAG_ADDITIVE, view_origin);
309                 }
310         }
311         if (!(trace_dphitq3surfaceflags & (Q3SURFACEFLAG_SKY | Q3SURFACEFLAG_NOIMPACT)))
312         {
313                 if(this.cnt >= 0)
314                         __pointparticles(this.cnt, trace_endpos, trace_plane_normal, drawframetime * 1000);
315                 if(this.colormod != '0 0 0' && this.modelscale != 0)
316                         adddynamiclight(trace_endpos + trace_plane_normal * 1, this.modelscale, this.colormod * 5);
317         }
318 }
319
320 NET_HANDLE(ENT_CLIENT_LASER, bool isnew)
321 {
322         InterpolateOrigin_Undo(self);
323
324         // 30 bytes, or 13 bytes for just moving
325         int f = ReadByte();
326         self.count = (f & 0xF0);
327
328         if(self.count & 0x80)
329                 self.iflags = IFLAG_VELOCITY | IFLAG_ORIGIN;
330         else
331                 self.iflags = IFLAG_ANGLES | IFLAG_ORIGIN;
332
333         if(f & 1)
334         {
335                 self.origin_x = ReadCoord();
336                 self.origin_y = ReadCoord();
337                 self.origin_z = ReadCoord();
338                 setorigin(self, self.origin);
339         }
340         if(f & 8)
341         {
342                 self.colormod_x = ReadByte() / 255.0;
343                 self.colormod_y = ReadByte() / 255.0;
344                 self.colormod_z = ReadByte() / 255.0;
345                 if(f & 0x40)
346                         self.alpha = ReadByte() / 255.0;
347                 else
348                         self.alpha = 0;
349                 self.scale = 2;
350                 self.modelscale = 50;
351                 if(f & 0x20)
352                 {
353                         self.scale *= ReadByte() / 16.0; // beam radius
354                         self.modelscale *= ReadByte() / 16.0; // dlight radius
355                 }
356                 if((f & 0x80) || !(f & 0x10))
357                         self.cnt = ReadShort() - 1; // effect number
358                 else
359                         self.cnt = 0;
360         }
361         if(f & 2)
362         {
363                 if(f & 0x80)
364                 {
365                         self.velocity_x = ReadCoord();
366                         self.velocity_y = ReadCoord();
367                         self.velocity_z = ReadCoord();
368                 }
369                 else
370                 {
371                         self.angles_x = ReadAngle();
372                         self.angles_y = ReadAngle();
373                 }
374         }
375         if(f & 4)
376                 self.state = ReadByte();
377
378         return = true;
379
380         InterpolateOrigin_Note(this);
381         self.draw = Draw_Laser;
382 }
383 #endif