From 91934665729693e4be7092549f885b0813fdc80e Mon Sep 17 00:00:00 2001 From: Christoph Oelckers Date: Tue, 29 Nov 2016 20:16:14 +0100 Subject: [PATCH] - scriptified ASecurityCamera and AAimingCamera. This concludes this round of script converesions of internal classes. --- src/CMakeLists.txt | 1 - src/g_shared/a_camera.cpp | 200 ------------------------ src/p_mobj.cpp | 7 + wadsrc/static/zscript/actor.txt | 1 + wadsrc/static/zscript/shared/camera.txt | 107 ++++++++++++- 5 files changed, 110 insertions(+), 206 deletions(-) delete mode 100644 src/g_shared/a_camera.cpp diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 111b2663c..d1e90581c 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1111,7 +1111,6 @@ set (PCH_SOURCES g_shared/a_armor.cpp g_shared/a_artifacts.cpp g_shared/a_bridge.cpp - g_shared/a_camera.cpp g_shared/a_decals.cpp g_shared/a_fastprojectile.cpp g_shared/a_flashfader.cpp diff --git a/src/g_shared/a_camera.cpp b/src/g_shared/a_camera.cpp deleted file mode 100644 index a0bc40ce3..000000000 --- a/src/g_shared/a_camera.cpp +++ /dev/null @@ -1,200 +0,0 @@ -/* -** a_camera.cpp -** Implements the Duke Nukem 3D-ish security camera -** -**--------------------------------------------------------------------------- -** Copyright 1998-2006 Randy Heit -** All rights reserved. -** -** Redistribution and use in source and binary forms, with or without -** modification, are permitted provided that the following conditions -** are met: -** -** 1. Redistributions of source code must retain the above copyright -** notice, this list of conditions and the following disclaimer. -** 2. Redistributions in binary form must reproduce the above copyright -** notice, this list of conditions and the following disclaimer in the -** documentation and/or other materials provided with the distribution. -** 3. The name of the author may not be used to endorse or promote products -** derived from this software without specific prior written permission. -** -** THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR -** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES -** OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. -** IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, -** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT -** NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF -** THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -**--------------------------------------------------------------------------- -** -*/ - -#include "actor.h" -#include "info.h" -#include "a_sharedglobal.h" -#include "p_local.h" -#include "serializer.h" -#include "math/cmath.h" - -/* -== SecurityCamera -== -== args[0] = pitch -== args[1] = amount camera turns to either side of its initial position -== (in degrees) -== args[2] = octics to complete one cycle -*/ - -class ASecurityCamera : public AActor -{ - DECLARE_CLASS (ASecurityCamera, AActor) -public: - void PostBeginPlay (); - void Tick (); - - - void Serialize(FSerializer &arc); -protected: - DAngle Center; - DAngle Acc; - DAngle Delta; - DAngle Range; -}; - -IMPLEMENT_CLASS(ASecurityCamera, false, false) - -void ASecurityCamera::Serialize(FSerializer &arc) -{ - Super::Serialize (arc); - arc("center", Center) - ("acc", Acc) - ("delta", Delta) - ("range", Range); -} - -void ASecurityCamera::PostBeginPlay () -{ - Super::PostBeginPlay (); - Center = Angles.Yaw; - if (args[2]) - Delta = 360. / (args[2] * TICRATE / 8); - else - Delta = 0.; - if (args[1]) - Delta /= 2; - Acc = 0.; - Angles.Pitch = (double)clamp((signed char)args[0], -89, 89); - Range = (double)args[1]; -} - -void ASecurityCamera::Tick () -{ - Acc += Delta; - if (Range != 0) - Angles.Yaw = Center + Range * Acc.Sin(); - else if (Delta != 0) - Angles.Yaw = Acc; -} - -/* -== AimingCamera -== -== args[0] = pitch -== args[1] = max turn (in degrees) -== args[2] = max pitch turn (in degrees) -== args[3] = tid of thing to look at -== -== Also uses: -== tracer: thing to look at -*/ - -class AAimingCamera : public ASecurityCamera -{ - DECLARE_CLASS (AAimingCamera, ASecurityCamera) -public: - void PostBeginPlay (); - void Tick (); - - - void Serialize(FSerializer &arc); -protected: - DAngle MaxPitchChange; -}; - -IMPLEMENT_CLASS(AAimingCamera, false, false) - -void AAimingCamera::Serialize(FSerializer &arc) -{ - Super::Serialize (arc); - arc("maxpitchchange", MaxPitchChange); -} - -void AAimingCamera::PostBeginPlay () -{ - int changepitch = args[2]; - - args[2] = 0; - Super::PostBeginPlay (); - MaxPitchChange = double(changepitch / TICRATE); - Range /= TICRATE; - - TActorIterator iterator (args[3]); - tracer = iterator.Next (); - if (tracer == NULL) - { - //Printf ("AimingCamera %d: Can't find TID %d\n", tid, args[3]); - } - else - { // Don't try for a new target upon losing this one. - args[3] = 0; - } -} - -void AAimingCamera::Tick () -{ - if (tracer == NULL && args[3] != 0) - { // Recheck, in case something with this TID was created since the last time. - TActorIterator iterator (args[3]); - tracer = iterator.Next (); - } - if (tracer != NULL) - { - DAngle delta; - int dir = P_FaceMobj (this, tracer, &delta); - if (delta > Range) - { - delta = Range; - } - if (dir) - { - Angles.Yaw += delta; - } - else - { - Angles.Yaw -= delta; - } - if (MaxPitchChange != 0) - { // Aim camera's pitch; use floats for precision - DVector2 vect = tracer->Vec2To(this); - double dz = Z() - tracer->Z() - tracer->Height/2; - double dist = vect.Length(); - DAngle desiredPitch = dist != 0.f ? VecToAngle(dist, dz) : 0.; - DAngle diff = deltaangle(Angles.Pitch, desiredPitch); - if (fabs (diff) < MaxPitchChange) - { - Angles.Pitch = desiredPitch; - } - else if (diff < 0) - { - Angles.Pitch -= MaxPitchChange; - } - else - { - Angles.Pitch += MaxPitchChange; - } - } - } -} diff --git a/src/p_mobj.cpp b/src/p_mobj.cpp index 7decbe9ad..9c0f587eb 100644 --- a/src/p_mobj.cpp +++ b/src/p_mobj.cpp @@ -7539,6 +7539,13 @@ DEFINE_ACTION_FUNCTION(AActor, Vec3To) ACTION_RETURN_VEC3(self->Vec3To(t)); } +DEFINE_ACTION_FUNCTION(AActor, Vec2To) +{ + PARAM_SELF_PROLOGUE(AActor); + PARAM_OBJECT(t, AActor) + ACTION_RETURN_VEC2(self->Vec2To(t)); +} + DEFINE_ACTION_FUNCTION(AActor, Vec3Angle) { PARAM_SELF_PROLOGUE(AActor); diff --git a/wadsrc/static/zscript/actor.txt b/wadsrc/static/zscript/actor.txt index b4b4afeb1..15c643047 100644 --- a/wadsrc/static/zscript/actor.txt +++ b/wadsrc/static/zscript/actor.txt @@ -356,6 +356,7 @@ class Actor : Thinker native native double AngleTo(Actor target, bool absolute = false); native void AddZ(double zadd, bool moving = true); native void SetZ(double z); + native vector2 Vec2To(Actor other); native vector3 Vec3To(Actor other); native vector3 Vec3Offset(double x, double y, double z, bool absolute = false); native vector3 Vec3Angle(double length, double angle, double z = 0, bool absolute = false); diff --git a/wadsrc/static/zscript/shared/camera.txt b/wadsrc/static/zscript/shared/camera.txt index 9420c98d5..abb394745 100644 --- a/wadsrc/static/zscript/shared/camera.txt +++ b/wadsrc/static/zscript/shared/camera.txt @@ -9,21 +9,118 @@ class DoomBuilderCamera : Actor } -class SecurityCamera : Actor native +class SecurityCamera : Actor { default { +NOBLOCKMAP +NOGRAVITY +DONTSPLASH - } - default - { RenderStyle "None"; CameraHeight 0; } + + double Center; + double Acc; + double Delta; + double Range; + + override void PostBeginPlay () + { + Super.PostBeginPlay (); + Center = Angle; + if (args[2]) + Delta = 360. / (args[2] * TICRATE / 8); + else + Delta = 0.; + if (args[1]) + Delta /= 2; + Acc = 0.; + Pitch = clamp(args[0], -89, 89); + Range = args[1]; + } + + override void Tick () + { + Acc += Delta; + if (Range != 0) + Angle = Center + Range * sin(Acc); + else if (Delta != 0) + Angle = Acc; + } + + } -class AimingCamera : SecurityCamera native +class AimingCamera : SecurityCamera { + double MaxPitchChange; + + override void PostBeginPlay () + { + int changepitch = args[2]; + + args[2] = 0; + Super.PostBeginPlay (); + MaxPitchChange = double(changepitch / TICRATE); + Range /= TICRATE; + + ActorIterator it = ActorIterator.Create(args[3]); + tracer = it.Next (); + if (tracer == NULL) + { + //Printf ("AimingCamera %d: Can't find TID %d\n", tid, args[3]); + } + else + { // Don't try for a new target upon losing this one. + args[3] = 0; + } + } + + override void Tick () + { + if (tracer == NULL && args[3] != 0) + { // Recheck, in case something with this TID was created since the last time. + ActorIterator it = ActorIterator.Create(args[3]); + tracer = it.Next (); + } + if (tracer != NULL) + { + double dir = deltaangle(angle, AngleTo(tracer)); + double delta = abs(dir); + if (delta > Range) + { + delta = Range; + } + if (dir > 0) + { + Angle += delta; + } + else + { + Angle -= delta; + } + if (MaxPitchChange != 0) + { // Aim camera's pitch; use floats for precision + Vector2 vect = tracer.Vec2To(self); + double dz = pos.z - tracer.pos.z - tracer.Height/2; + double dist = vect.Length(); + double desiredPitch = dist != 0.f ? VectorAngle(dist, dz) : 0.; + double diff = deltaangle(pitch, desiredPitch); + if (abs (diff) < MaxPitchChange) + { + pitch = desiredPitch; + } + else if (diff < 0) + { + pitch -= MaxPitchChange; + } + else + { + pitch += MaxPitchChange; + } + } + } + } + }