]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blobdiff - qcsrc/common/triggers/misc/laser.qc
Merge branch 'master' into DefaultUser/trigger_cleanup
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / triggers / misc / laser.qc
index f0aafc7c40e5d06238324f87b0eb6d14dec518bb..df88b750f245dbc7000deccd0a6785bf5ae1a300 100644 (file)
@@ -1,3 +1,4 @@
+#include "laser.qh"
 #if defined(CSQC)
        #include <lib/csqcmodel/interpolate.qh>
        #include <client/main.qh>
@@ -15,12 +16,12 @@ void misc_laser_aim(entity this)
        vector a;
        if(this.enemy)
        {
-               if(this.spawnflags & 2)
+               if(this.spawnflags & LASER_FINITE)
                {
                        if(this.enemy.origin != this.mangle)
                        {
                                this.mangle = this.enemy.origin;
-                               this.SendFlags |= 2;
+                               this.SendFlags |= SF_LASER_UPDATE_TARGET;
                        }
                }
                else
@@ -30,7 +31,7 @@ void misc_laser_aim(entity this)
                        if(a != this.mangle)
                        {
                                this.mangle = a;
-                               this.SendFlags |= 2;
+                               this.SendFlags |= SF_LASER_UPDATE_TARGET;
                        }
                }
        }
@@ -39,20 +40,20 @@ void misc_laser_aim(entity this)
                if(this.angles != this.mangle)
                {
                        this.mangle = this.angles;
-                       this.SendFlags |= 2;
+                       this.SendFlags |= SF_LASER_UPDATE_TARGET;
                }
        }
        if(this.origin != this.oldorigin)
        {
-               this.SendFlags |= 1;
+               this.SendFlags |= SF_LASER_UPDATE_ORIGIN;
                this.oldorigin = this.origin;
        }
 }
 
 void misc_laser_init(entity this)
 {
-       if(self.target != "")
-               self.enemy = find(world, targetname, self.target);
+       if(this.target != "")
+               this.enemy = find(NULL, targetname, this.target);
 }
 
 .entity pusher;
@@ -62,116 +63,112 @@ void misc_laser_think(entity this)
        entity hitent;
        vector hitloc;
 
-       self.nextthink = time;
+       this.nextthink = time;
 
-       if(!self.state)
+       if(this.active == ACTIVE_NOT)
                return;
 
        misc_laser_aim(this);
 
-       if(self.enemy)
+       if(this.enemy)
        {
-               o = self.enemy.origin;
-               if (!(self.spawnflags & 2))
-                       o = self.origin + normalize(o - self.origin) * 32768;
+               o = this.enemy.origin;
+               if (!(this.spawnflags & LASER_FINITE))
+                       o = this.origin + normalize(o - this.origin) * LASER_BEAM_MAXLENGTH;
        }
        else
        {
-               makevectors(self.mangle);
-               o = self.origin + v_forward * 32768;
+               makevectors(this.mangle);
+               o = this.origin + v_forward * LASER_BEAM_MAXLENGTH;
        }
 
-       if(self.dmg || self.enemy.target != "")
+       if(this.dmg || this.enemy.target != "")
        {
-               traceline(self.origin, o, MOVE_NORMAL, self);
+               traceline(this.origin, o, MOVE_NORMAL, this);
        }
        hitent = trace_ent;
        hitloc = trace_endpos;
 
-       if(self.enemy.target != "") // DETECTOR laser
+       if(this.enemy.target != "") // DETECTOR laser
        {
                if(trace_ent.iscreature)
                {
-                       self.pusher = hitent;
-                       if(!self.count)
+                       this.pusher = hitent;
+                       if(!this.count)
                        {
-                               self.count = 1;
+                               this.count = 1;
 
-                               SUB_UseTargets(self.enemy, self.enemy.pusher, NULL);
+                               SUB_UseTargets(this.enemy, this.enemy.pusher, NULL);
                        }
                }
                else
                {
-                       if(self.count)
+                       if(this.count)
                        {
-                               self.count = 0;
+                               this.count = 0;
 
-                               SUB_UseTargets(self.enemy, self.enemy.pusher, NULL);
+                               SUB_UseTargets(this.enemy, this.enemy.pusher, NULL);
                        }
                }
        }
 
-       if(self.dmg)
+       if(this.dmg)
        {
-               if(self.team)
-                       if(((self.spawnflags & 8) == 0) == (self.team != hitent.team))
+               if(this.team)
+                       if(((this.spawnflags & LASER_INVERT_TEAM) == 0) == (this.team != hitent.team))
                                return;
                if(hitent.takedamage)
-                       Damage(hitent, self, self, ((self.dmg < 0) ? 100000 : (self.dmg * frametime)), DEATH_HURTTRIGGER.m_id, hitloc, '0 0 0');
+                       Damage(hitent, this, this, ((this.dmg < 0) ? 100000 : (this.dmg * frametime)), DEATH_HURTTRIGGER.m_id, DMG_NOWEP, hitloc, '0 0 0');
        }
 }
 
-bool laser_SendEntity(entity this, entity to, float fl)
+bool laser_SendEntity(entity this, entity to, float sendflags)
 {
        WriteHeader(MSG_ENTITY, ENT_CLIENT_LASER);
-       fl = fl - (fl & 0xF0); // use that bit to indicate finite length laser
-       if(self.spawnflags & 2)
-               fl |= 0x80;
-       if(self.alpha)
-               fl |= 0x40;
-       if(self.scale != 1 || self.modelscale != 1)
-               fl |= 0x20;
-       if(self.spawnflags & 4)
-               fl |= 0x10;
-       WriteByte(MSG_ENTITY, fl);
-       if(fl & 1)
+       sendflags = sendflags & 0x0F; // use that bit to indicate finite length laser
+       if(this.spawnflags & LASER_FINITE)
+               sendflags |= SF_LASER_FINITE;
+       if(this.alpha)
+               sendflags |= SF_LASER_ALPHA;
+       if(this.scale != 1 || this.modelscale != 1)
+               sendflags |= SF_LASER_SCALE;
+       if(this.spawnflags & LASER_NOTRACE)
+               sendflags |= SF_LASER_NOTRACE;
+       WriteByte(MSG_ENTITY, sendflags);
+       if(sendflags & SF_LASER_UPDATE_ORIGIN)
        {
-               WriteCoord(MSG_ENTITY, self.origin_x);
-               WriteCoord(MSG_ENTITY, self.origin_y);
-               WriteCoord(MSG_ENTITY, self.origin_z);
+               WriteVector(MSG_ENTITY, this.origin);
        }
-       if(fl & 8)
+       if(sendflags & SF_LASER_UPDATE_EFFECT)
        {
-               WriteByte(MSG_ENTITY, self.colormod_x * 255.0);
-               WriteByte(MSG_ENTITY, self.colormod_y * 255.0);
-               WriteByte(MSG_ENTITY, self.colormod_z * 255.0);
-               if(fl & 0x40)
-                       WriteByte(MSG_ENTITY, self.alpha * 255.0);
-               if(fl & 0x20)
+               WriteByte(MSG_ENTITY, this.beam_color.x * 255.0);
+               WriteByte(MSG_ENTITY, this.beam_color.y * 255.0);
+               WriteByte(MSG_ENTITY, this.beam_color.z * 255.0);
+               if(sendflags & SF_LASER_ALPHA)
+                       WriteByte(MSG_ENTITY, this.alpha * 255.0);
+               if(sendflags & SF_LASER_SCALE)
                {
-                       WriteByte(MSG_ENTITY, bound(0, self.scale * 16.0, 255));
-                       WriteByte(MSG_ENTITY, bound(0, self.modelscale * 16.0, 255));
+                       WriteByte(MSG_ENTITY, bound(0, this.scale * 16.0, 255));
+                       WriteByte(MSG_ENTITY, bound(0, this.modelscale * 16.0, 255));
                }
-               if((fl & 0x80) || !(fl & 0x10)) // effect doesn't need sending if the laser is infinite and has collision testing turned off
-                       WriteShort(MSG_ENTITY, self.cnt + 1);
+               if((sendflags & SF_LASER_FINITE) || !(sendflags & SF_LASER_NOTRACE)) // effect doesn't need sending if the laser is infinite and has collision testing turned off
+                       WriteShort(MSG_ENTITY, this.cnt);
        }
-       if(fl & 2)
+       if(sendflags & SF_LASER_UPDATE_TARGET)
        {
-               if(fl & 0x80)
+               if(sendflags & SF_LASER_FINITE)
                {
-                       WriteCoord(MSG_ENTITY, self.enemy.origin_x);
-                       WriteCoord(MSG_ENTITY, self.enemy.origin_y);
-                       WriteCoord(MSG_ENTITY, self.enemy.origin_z);
+                       WriteVector(MSG_ENTITY, this.enemy.origin);
                }
                else
                {
-                       WriteAngle(MSG_ENTITY, self.mangle_x);
-                       WriteAngle(MSG_ENTITY, self.mangle_y);
+                       WriteAngle(MSG_ENTITY, this.mangle_x);
+                       WriteAngle(MSG_ENTITY, this.mangle_y);
                }
        }
-       if(fl & 4)
-               WriteByte(MSG_ENTITY, self.state);
-       return 1;
+       if(sendflags & SF_LASER_UPDATE_ACTIVE)
+               WriteByte(MSG_ENTITY, this.active);
+       return true;
 }
 
 /*QUAKED spawnfunc_misc_laser (.5 .5 .5) ? START_ON DEST_IS_FIXED
@@ -181,24 +178,41 @@ Keys:
  spawnfunc_target_position where the laser ends
 "mdl"
  name of beam end effect to use
-"colormod"
+"beam_color"
  color of the beam (default: red)
 "dmg"
  damage per second (-1 for a laser that kills immediately)
 */
-void laser_use(entity this, entity actor, entity trigger)
+
+void laser_setactive(entity this, int act)
 {
-       this.state = !this.state;
-       this.SendFlags |= 4;
-       misc_laser_aim(this);
+       int old_status = this.active;
+       if(act == ACTIVE_TOGGLE)
+       {
+               if(this.active == ACTIVE_ACTIVE)
+               {
+                       this.active = ACTIVE_NOT;
+               }
+               else
+               {
+                       this.active = ACTIVE_ACTIVE;
+               }
+       }
+       else
+       {
+               this.active = act;
+       }
+
+       if (this.active != old_status)
+       {
+               this.SendFlags |= SF_LASER_UPDATE_ACTIVE;
+               misc_laser_aim(this);
+       }
 }
 
-void laser_reset(entity this)
+void laser_use(entity this, entity actor, entity trigger)
 {
-       if(this.spawnflags & 1)
-               this.state = 1;
-       else
-               this.state = 0;
+       this.setactive(this, ACTIVE_TOGGLE);
 }
 
 spawnfunc(misc_laser)
@@ -224,14 +238,38 @@ spawnfunc(misc_laser)
        if(this.cnt < 0)
                this.cnt = -1;
 
-       if(this.colormod == '0 0 0')
+       if(!this.beam_color && this.colormod)
+       {
+               LOG_WARN("misc_laser uses legacy field 'colormod', please use 'beam_color' instead");
+               this.beam_color = this.colormod;
+       }
+
+       if(this.beam_color == '0 0 0')
+       {
                if(!this.alpha)
-                       this.colormod = '1 0 0';
-       if(this.message == "") this.message = "saw the light";
-       if (this.message2 == "") this.message2 = "was pushed into a laser by";
-       if(!this.scale) this.scale = 1;
-       if(!this.modelscale) this.modelscale = 1;
-       else if(this.modelscale < 0) this.modelscale = 0;
+                       this.beam_color = '1 0 0';
+       }
+
+       if(this.message == "")
+       {
+               this.message = "saw the light";
+       }
+       if (this.message2 == "")
+       {
+               this.message2 = "was pushed into a laser by";
+       }
+       if(!this.scale)
+       {
+               this.scale = 1;
+       }
+       if(!this.modelscale)
+       {
+               this.modelscale = 1;
+       }
+       else if(this.modelscale < 0)
+       {
+               this.modelscale = 0;
+       }
        setthink(this, misc_laser_think);
        this.nextthink = time;
        InitializeEntity(this, misc_laser_init, INITPRIO_FINDTARGET);
@@ -240,38 +278,40 @@ spawnfunc(misc_laser)
 
        Net_LinkEntity(this, false, 0, laser_SendEntity);
 
+       this.setactive = laser_setactive;
+
        IFTARGETED
        {
-               this.reset = laser_reset;
-               this.reset(this);
+               // backwards compatibility
                this.use = laser_use;
        }
-       else
-               this.state = 1;
+
+       this.reset = generic_netlinked_reset;
+       this.reset(this);
 }
 #elif defined(CSQC)
 
 // a laser goes from origin in direction angles
-// it has color 'colormod'
+// it has color 'beam_color'
 // and stops when something is in the way
 entityclass(Laser);
-class(Laser) .int cnt; // end effect
-class(Laser) .vector colormod;
-class(Laser) .int state; // on-off
-class(Laser) .int count; // flags for the laser
-class(Laser) .vector velocity;
-class(Laser) .float alpha;
-class(Laser) .float scale; // scaling factor of the thickness
-class(Laser) .float modelscale; // scaling factor of the dlight
+classfield(Laser) .int cnt; // end effect
+classfield(Laser) .vector colormod;
+classfield(Laser) .int state; // on-off
+classfield(Laser) .int count; // flags for the laser
+classfield(Laser) .vector velocity; // laser endpoint if it is FINITE
+classfield(Laser) .float alpha;
+classfield(Laser) .float scale; // scaling factor of the thickness
+classfield(Laser) .float modelscale; // scaling factor of the dlight
 
 void Draw_Laser(entity this)
 {
-       if(!this.state)
+       if(this.active == ACTIVE_NOT)
                return;
        InterpolateOrigin_Do(this);
-       if(this.count & 0x80)
+       if(this.count & SF_LASER_FINITE)
        {
-               if(this.count & 0x10)
+               if(this.count & SF_LASER_NOTRACE)
                {
                        trace_endpos = this.velocity;
                        trace_dphitq3surfaceflags = 0;
@@ -281,37 +321,37 @@ void Draw_Laser(entity this)
        }
        else
        {
-               if(this.count & 0x10)
+               if(this.count & SF_LASER_NOTRACE)
                {
                        makevectors(this.angles);
-                       trace_endpos = this.origin + v_forward * 1048576;
+                       trace_endpos = this.origin + v_forward * LASER_BEAM_MAXWORLDSIZE;
                        trace_dphitq3surfaceflags = Q3SURFACEFLAG_SKY;
                }
                else
                {
                        makevectors(this.angles);
-                       traceline(this.origin, this.origin + v_forward * 32768, 0, this);
+                       traceline(this.origin, this.origin + v_forward * LASER_BEAM_MAXLENGTH, 0, this);
                        if(trace_dphitq3surfaceflags & Q3SURFACEFLAG_SKY)
-                               trace_endpos = this.origin + v_forward * 1048576;
+                               trace_endpos = this.origin + v_forward * LASER_BEAM_MAXWORLDSIZE;
                }
        }
        if(this.scale != 0)
        {
                if(this.alpha)
                {
-                       Draw_CylindricLine(this.origin, trace_endpos, this.scale, "particles/laserbeam", 0, time * 3, this.colormod, this.alpha, DRAWFLAG_NORMAL, view_origin);
+                       Draw_CylindricLine(this.origin, trace_endpos, this.scale, "particles/laserbeam", 0, time * 3, this.beam_color, this.alpha, DRAWFLAG_NORMAL, view_origin);
                }
                else
                {
-                       Draw_CylindricLine(this.origin, trace_endpos, this.scale, "particles/laserbeam", 0, time * 3, this.colormod, 0.5, DRAWFLAG_ADDITIVE, view_origin);
+                       Draw_CylindricLine(this.origin, trace_endpos, this.scale, "particles/laserbeam", 0, time * 3, this.beam_color, 0.5, DRAWFLAG_ADDITIVE, view_origin);
                }
        }
        if (!(trace_dphitq3surfaceflags & (Q3SURFACEFLAG_SKY | Q3SURFACEFLAG_NOIMPACT)))
        {
                if(this.cnt >= 0)
                        __pointparticles(this.cnt, trace_endpos, trace_plane_normal, drawframetime * 1000);
-               if(this.colormod != '0 0 0' && this.modelscale != 0)
-                       adddynamiclight(trace_endpos + trace_plane_normal * 1, this.modelscale, this.colormod * 5);
+               if(this.beam_color != '0 0 0' && this.modelscale != 0)
+                       adddynamiclight(trace_endpos + trace_plane_normal * 1, this.modelscale, this.beam_color * 5);
        }
 }
 
@@ -320,49 +360,45 @@ NET_HANDLE(ENT_CLIENT_LASER, bool isnew)
        InterpolateOrigin_Undo(this);
 
        // 30 bytes, or 13 bytes for just moving
-       int f = ReadByte();
-       this.count = (f & 0xF0);
+       int sendflags = ReadByte();
+       this.count = (sendflags & 0xF0);
 
-       if(this.count & 0x80)
+       if(this.count & SF_LASER_FINITE)
                this.iflags = IFLAG_VELOCITY | IFLAG_ORIGIN;
        else
                this.iflags = IFLAG_ANGLES | IFLAG_ORIGIN;
 
-       if(f & 1)
+       if(sendflags & SF_LASER_UPDATE_ORIGIN)
        {
-               this.origin_x = ReadCoord();
-               this.origin_y = ReadCoord();
-               this.origin_z = ReadCoord();
+               this.origin = ReadVector();
                setorigin(this, this.origin);
        }
-       if(f & 8)
+       if(sendflags & SF_LASER_UPDATE_EFFECT)
        {
-               this.colormod_x = ReadByte() / 255.0;
-               this.colormod_y = ReadByte() / 255.0;
-               this.colormod_z = ReadByte() / 255.0;
-               if(f & 0x40)
+               this.beam_color.x = ReadByte() / 255.0;
+               this.beam_color.y = ReadByte() / 255.0;
+               this.beam_color.z = ReadByte() / 255.0;
+               if(sendflags & SF_LASER_ALPHA)
                        this.alpha = ReadByte() / 255.0;
                else
                        this.alpha = 0;
-               this.scale = 2;
-               this.modelscale = 50;
-               if(f & 0x20)
+               this.scale = 2; // NOTE: why 2?
+               this.modelscale = 50; // NOTE: why 50?
+               if(sendflags & SF_LASER_SCALE)
                {
                        this.scale *= ReadByte() / 16.0; // beam radius
                        this.modelscale *= ReadByte() / 16.0; // dlight radius
                }
-               if((f & 0x80) || !(f & 0x10))
-                       this.cnt = ReadShort() - 1; // effect number
+               if((sendflags & SF_LASER_FINITE) || !(sendflags & SF_LASER_NOTRACE))
+                       this.cnt = ReadShort(); // effect number
                else
                        this.cnt = 0;
        }
-       if(f & 2)
+       if(sendflags & SF_LASER_UPDATE_TARGET)
        {
-               if(f & 0x80)
+               if(sendflags & SF_LASER_FINITE)
                {
-                       this.velocity_x = ReadCoord();
-                       this.velocity_y = ReadCoord();
-                       this.velocity_z = ReadCoord();
+                       this.velocity = ReadVector();
                }
                else
                {
@@ -370,12 +406,13 @@ NET_HANDLE(ENT_CLIENT_LASER, bool isnew)
                        this.angles_y = ReadAngle();
                }
        }
-       if(f & 4)
-               this.state = ReadByte();
+       if(sendflags & SF_LASER_UPDATE_ACTIVE)
+               this.active = ReadByte();
 
        return = true;
 
        InterpolateOrigin_Note(this);
        this.draw = Draw_Laser;
+       if (isnew) IL_PUSH(g_drawables, this);
 }
 #endif