From 59f2ccc140258a1616e45169adf4ac733bab5b54 Mon Sep 17 00:00:00 2001 From: Freddy Date: Sat, 10 Mar 2018 21:28:29 +0100 Subject: [PATCH] misc_laser: .active instead of .state, replace magic numbers --- qcsrc/common/triggers/misc/laser.qc | 217 +++++++++++++++++----------- qcsrc/common/triggers/misc/laser.qh | 8 + qcsrc/common/triggers/spawnflags.qh | 16 ++ qcsrc/lib/spawnfunc.qh | 1 + 4 files changed, 155 insertions(+), 87 deletions(-) diff --git a/qcsrc/common/triggers/misc/laser.qc b/qcsrc/common/triggers/misc/laser.qc index 3d5ef42d8..12b67c371 100644 --- a/qcsrc/common/triggers/misc/laser.qc +++ b/qcsrc/common/triggers/misc/laser.qc @@ -16,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 @@ -31,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; } } } @@ -40,12 +40,12 @@ 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; } } @@ -65,7 +65,7 @@ void misc_laser_think(entity this) this.nextthink = time; - if(!this.state) + if(this.active == ACTIVE_NOT) return; misc_laser_aim(this); @@ -73,13 +73,13 @@ void misc_laser_think(entity this) if(this.enemy) { o = this.enemy.origin; - if (!(this.spawnflags & 2)) - o = this.origin + normalize(o - this.origin) * 32768; + if (!(this.spawnflags & LASER_FINITE)) + o = this.origin + normalize(o - this.origin) * LASER_BEAM_MAXLENGTH; } else { makevectors(this.mangle); - o = this.origin + v_forward * 32768; + o = this.origin + v_forward * LASER_BEAM_MAXLENGTH; } if(this.dmg || this.enemy.target != "") @@ -115,48 +115,48 @@ void misc_laser_think(entity this) if(this.dmg) { if(this.team) - if(((this.spawnflags & 8) == 0) == (this.team != hitent.team)) + if(((this.spawnflags & LASER_INVERT_TEAM) == 0) == (this.team != hitent.team)) return; if(hitent.takedamage) 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(this.spawnflags & 2) - fl |= 0x80; + sendflags = sendflags & 0x0F; // use that bit to indicate finite length laser + if(this.spawnflags & LASER_FINITE) + sendflags |= SF_LASER_FINITE; if(this.alpha) - fl |= 0x40; + sendflags |= SF_LASER_ALPHA; if(this.scale != 1 || this.modelscale != 1) - fl |= 0x20; - if(this.spawnflags & 4) - fl |= 0x10; - WriteByte(MSG_ENTITY, fl); - if(fl & 1) + sendflags |= SF_LASER_SCALE; + if(this.spawnflags & LASER_NOTRACE) + sendflags |= SF_LASER_NOTRACE; + WriteByte(MSG_ENTITY, sendflags); + if(sendflags & SF_LASER_UPDATE_ORIGIN) { WriteVector(MSG_ENTITY, this.origin); } - if(fl & 8) + if(sendflags & SF_LASER_UPDATE_EFFECT) { - WriteByte(MSG_ENTITY, this.colormod_x * 255.0); - WriteByte(MSG_ENTITY, this.colormod_y * 255.0); - WriteByte(MSG_ENTITY, this.colormod_z * 255.0); - if(fl & 0x40) + 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(fl & 0x20) + if(sendflags & SF_LASER_SCALE) { 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, this.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) { WriteVector(MSG_ENTITY, this.enemy.origin); } @@ -166,9 +166,9 @@ bool laser_SendEntity(entity this, entity to, float fl) WriteAngle(MSG_ENTITY, this.mangle_y); } } - if(fl & 4) - WriteByte(MSG_ENTITY, this.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 @@ -178,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) @@ -221,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); @@ -237,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) .vector beam_color; +class(Laser) .int active; // on-off class(Laser) .int count; // flags for the laser -class(Laser) .vector velocity; +class(Laser) .vector velocity; // laser endpoint if it is FINITE class(Laser) .float alpha; class(Laser) .float scale; // scaling factor of the thickness class(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; @@ -278,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); } } @@ -317,43 +360,43 @@ 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 = 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 = ReadVector(); } @@ -363,8 +406,8 @@ 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; diff --git a/qcsrc/common/triggers/misc/laser.qh b/qcsrc/common/triggers/misc/laser.qh index 6f70f09be..36a25faa0 100644 --- a/qcsrc/common/triggers/misc/laser.qh +++ b/qcsrc/common/triggers/misc/laser.qh @@ -1 +1,9 @@ #pragma once +#include "../spawnflags.qh" + + +.vector beam_color; + +const float LASER_BEAM_MAXLENGTH = 32768; // maximum length of a beam trace +// TODO: find a better way to do this +const float LASER_BEAM_MAXWORLDSIZE = 1048576; // to make sure the endpoint of the beam is not visible inside diff --git a/qcsrc/common/triggers/spawnflags.qh b/qcsrc/common/triggers/spawnflags.qh index 8c32ef9c9..50008e182 100644 --- a/qcsrc/common/triggers/spawnflags.qh +++ b/qcsrc/common/triggers/spawnflags.qh @@ -71,6 +71,11 @@ const int PROJECT_ON_TARGET4NORMAL = BIT(3); const int FOLLOW_ATTACH = BIT(0); const int FOLLOW_LOCAL = BIT(1); +// laser +const int LASER_FINITE = BIT(1); +const int LASER_NOTRACE = BIT(2); +const int LASER_INVERT_TEAM = BIT(3); + // platforms const int PLAT_LOW_TRIGGER = BIT(0); const int PLAT_CRUSH = BIT(2); @@ -97,3 +102,14 @@ const int SF_POINTPARTICLES_IMPULSE = BIT(4); const int SF_POINTPARTICLES_MOVING = BIT(5); // Send velocity and movedir const int SF_POINTPARTICLES_JITTER_AND_COUNT = BIT(6); // Send waterlevel (=jitter) and count const int SF_POINTPARTICLES_BOUNDS = BIT(7); // Send min and max of the brush + +// laser +const int SF_LASER_UPDATE_ORIGIN = BIT(0); +const int SF_LASER_UPDATE_TARGET = BIT(1); +const int SF_LASER_UPDATE_ACTIVE = BIT(2); +const int SF_LASER_UPDATE_EFFECT = BIT(3); + +const int SF_LASER_NOTRACE = BIT(4); +const int SF_LASER_SCALE = BIT(5); +const int SF_LASER_ALPHA = BIT(6); +const int SF_LASER_FINITE = BIT(7); diff --git a/qcsrc/lib/spawnfunc.qh b/qcsrc/lib/spawnfunc.qh index 877a9800e..ec8c84563 100644 --- a/qcsrc/lib/spawnfunc.qh +++ b/qcsrc/lib/spawnfunc.qh @@ -258,6 +258,7 @@ noref bool require_spawnfunc_prefix; FIELD_VEC(fld, absmin) \ FIELD_VEC(fld, angles) \ FIELD_VEC(fld, avelocity) \ + FIELD_VEC(fld, beam_color)\ FIELD_VEC(fld, debrisavelocityjitter) \ FIELD_VEC(fld, debrisvelocity) \ FIELD_VEC(fld, debrisvelocityjitter) \ -- 2.39.2