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