From 2bd72478eec0422844fcec006092b53cd1a1ccee Mon Sep 17 00:00:00 2001 From: Christoph Oelckers Date: Thu, 3 Jan 2019 11:57:20 +0100 Subject: [PATCH] - scriptified P_CalcHeight. This was the only code using the ViewBob member variable. This also moves the range check for this variable to its application, because a badly behaved mod can just as easily change it at run time instead of just setting an absurdly large value in the class definition. --- src/d_player.h | 8 +- src/g_game.cpp | 2 - src/namedef.h | 1 + src/p_teleport.cpp | 8 +- src/p_user.cpp | 153 ++---------------------- src/scripting/thingdef_properties.cpp | 20 ---- src/scripting/vmthunks_actors.cpp | 15 ++- wadsrc/static/zscript/shared/player.txt | 138 ++++++++++++++++++++- 8 files changed, 170 insertions(+), 175 deletions(-) diff --git a/src/d_player.h b/src/d_player.h index 2c96de927..89b10f38a 100644 --- a/src/d_player.h +++ b/src/d_player.h @@ -136,10 +136,6 @@ public: // [CW] Fades for when you are being damaged. PalEntry DamageFade; - // [SP] ViewBob Multiplier - double ViewBob; - double curBob; - // Everything below this point is only used by scripted code or through the scripted variable interface. int RunHealth; TObjPtr InvFirst; // first inventory item displayed on inventory bar @@ -147,6 +143,10 @@ public: double SideMove1, SideMove2; double HexenArmor[5]; + // [SP] ViewBob Multiplier + double ViewBob; + double curBob; + // Former class properties that were moved into the object to get rid of the meta class. FName SoundClass; // Sound class FName Portrait; diff --git a/src/g_game.cpp b/src/g_game.cpp index 70e5a517d..6006180ec 100644 --- a/src/g_game.cpp +++ b/src/g_game.cpp @@ -2001,8 +2001,6 @@ CUSTOM_CVAR (Int, autosavecount, 4, CVAR_ARCHIVE|CVAR_GLOBALCONFIG) self = 0; } -extern void P_CalcHeight (player_t *); - void G_DoAutoSave () { FString description; diff --git a/src/namedef.h b/src/namedef.h index 37e14c5d5..e7c2688b4 100644 --- a/src/namedef.h +++ b/src/namedef.h @@ -1039,6 +1039,7 @@ xx(SideMove2) xx(Face) xx(Slot) xx(SoundClass) +xx(ViewBob) xx(BlueCard) xx(YellowCard) diff --git a/src/p_teleport.cpp b/src/p_teleport.cpp index 7618b6165..4938e1860 100644 --- a/src/p_teleport.cpp +++ b/src/p_teleport.cpp @@ -48,8 +48,6 @@ static FRandom pr_teleport ("Teleport"); -extern void P_CalcHeight (player_t *player); - CVAR (Bool, telezoom, true, CVAR_ARCHIVE|CVAR_GLOBALCONFIG); //========================================================================== @@ -585,7 +583,11 @@ bool EV_SilentLineTeleport (line_t *line, int side, AActor *thing, int id, INTBO player->deltaviewheight = 0; // Set player's view according to the newly set parameters - P_CalcHeight(player); + IFVIRTUALPTR(player->mo, APlayerPawn, CalcHeight) + { + VMValue param = player->mo; + VMCall(func, ¶m, 1, nullptr, 0); + } // Reset the delta to have the same dynamics as before player->deltaviewheight = deltaviewheight; diff --git a/src/p_user.cpp b/src/p_user.cpp index 3d8a66e55..eaafa2e7a 100644 --- a/src/p_user.cpp +++ b/src/p_user.cpp @@ -195,13 +195,6 @@ FString GetPrintableDisplayName(PClassActor *cls) return cls->GetDisplayName(); } -DEFINE_ACTION_FUNCTION(APlayerPawn, GetPrintableDisplayName) -{ - PARAM_PROLOGUE; - PARAM_CLASS(type, AActor); - ACTION_RETURN_STRING(type->GetDisplayName()); -} - bool ValidatePlayerClass(PClassActor *ti, const char *name) { if (ti == NULL) @@ -297,9 +290,6 @@ CCMD (playerclasses) // Movement. // -// 16 pixels of bob -#define MAXBOB 16. - player_t::~player_t() { DestroyPSprites(); @@ -774,6 +764,17 @@ DEFINE_ACTION_FUNCTION(_PlayerInfo, GetWBobSpeed) ACTION_RETURN_FLOAT(self->userinfo.GetWBobSpeed()); } +DEFINE_ACTION_FUNCTION(_PlayerInfo, GetMoveBob) +{ + PARAM_SELF_STRUCT_PROLOGUE(player_t); + ACTION_RETURN_FLOAT(self->userinfo.GetMoveBob()); +} + +DEFINE_ACTION_FUNCTION(_PlayerInfo, GetStillBob) +{ + PARAM_SELF_STRUCT_PROLOGUE(player_t); + ACTION_RETURN_FLOAT(self->userinfo.GetStillBob()); +} //=========================================================================== // @@ -1202,138 +1203,6 @@ void P_CheckPlayerSprite(AActor *actor, int &spritenum, DVector2 &scale) } } -/* -================== -= -= P_CalcHeight -= -= -Calculate the walking / running height adjustment -= -================== -*/ - -void P_CalcHeight (player_t *player) -{ - DAngle angle; - double bob; - bool still = false; - - // Regular movement bobbing - // (needs to be calculated for gun swing even if not on ground) - - // killough 10/98: Make bobbing depend only on player-applied motion. - // - // Note: don't reduce bobbing here if on ice: if you reduce bobbing here, - // it causes bobbing jerkiness when the player moves from ice to non-ice, - // and vice-versa. - - if (player->cheats & CF_NOCLIP2) - { - player->bob = 0; - } - else if ((player->mo->flags & MF_NOGRAVITY) && !player->onground) - { - player->bob = 0.5; - } - else - { - player->bob = player->Vel.LengthSquared(); - if (player->bob == 0) - { - still = true; - } - else - { - player->bob *= player->userinfo.GetMoveBob(); - - if (player->bob > MAXBOB) - player->bob = MAXBOB; - } - } - - double defaultviewheight = player->mo->ViewHeight + player->crouchviewdelta; - - if (player->cheats & CF_NOVELOCITY) - { - player->viewz = player->mo->Z() + defaultviewheight; - - if (player->viewz > player->mo->ceilingz-4) - player->viewz = player->mo->ceilingz-4; - - return; - } - - if (still) - { - if (player->health > 0) - { - angle = level.time / (120 * TICRATE / 35.) * 360.; - bob = player->userinfo.GetStillBob() * angle.Sin(); - } - else - { - bob = 0; - } - } - else - { - angle = level.time / (20 * TICRATE / 35.) * 360.; - bob = player->bob * angle.Sin() * (player->mo->waterlevel > 1 ? 0.25f : 0.5f); - } - - // move viewheight - if (player->playerstate == PST_LIVE) - { - player->viewheight += player->deltaviewheight; - - if (player->viewheight > defaultviewheight) - { - player->viewheight = defaultviewheight; - player->deltaviewheight = 0; - } - else if (player->viewheight < (defaultviewheight/2)) - { - player->viewheight = defaultviewheight/2; - if (player->deltaviewheight <= 0) - player->deltaviewheight = 1 / 65536.; - } - - if (player->deltaviewheight) - { - player->deltaviewheight += 0.25; - if (!player->deltaviewheight) - player->deltaviewheight = 1/65536.; - } - } - - if (player->morphTics) - { - bob = 0; - } - player->viewz = player->mo->Z() + player->viewheight + (bob * player->mo->ViewBob); // [SP] Allow DECORATE changes to view bobbing speed. - if (player->mo->Floorclip && player->playerstate != PST_DEAD - && player->mo->Z() <= player->mo->floorz) - { - player->viewz -= player->mo->Floorclip; - } - if (player->viewz > player->mo->ceilingz - 4) - { - player->viewz = player->mo->ceilingz - 4; - } - if (player->viewz < player->mo->floorz + 4) - { - player->viewz = player->mo->floorz + 4; - } -} - -DEFINE_ACTION_FUNCTION(APlayerPawn, CalcHeight) -{ - PARAM_SELF_PROLOGUE(APlayerPawn); - P_CalcHeight(self->player); - return 0; -} - CUSTOM_CVAR (Float, sv_aircontrol, 0.00390625f, CVAR_SERVERINFO|CVAR_NOSAVE) { level.aircontrol = self; diff --git a/src/scripting/thingdef_properties.cpp b/src/scripting/thingdef_properties.cpp index 426197e6a..0d6f4b319 100644 --- a/src/scripting/thingdef_properties.cpp +++ b/src/scripting/thingdef_properties.cpp @@ -1696,26 +1696,6 @@ DEFINE_CLASS_PROPERTY_PREFIX(player, weaponslot, ISsssssssssssssssssssssssssssss } } -//========================================================================== -// -// [SP] Player.Viewbob -// -//========================================================================== -DEFINE_CLASS_PROPERTY_PREFIX(player, viewbob, F, PlayerPawn) -{ - PROP_DOUBLE_PARM(z, 0); - // [SP] Hard limits. This is to prevent terrywads from making players sick. - // Remember - this messes with a user option who probably has it set a - // certain way for a reason. I think a 1.5 limit is pretty generous, but - // it may be safe to increase it. I really need opinions from people who - // could be affected by this. - if (z < 0.0 || z > 1.5) - { - I_Error("ViewBob must be between 0.0 and 1.5."); - } - defaults->ViewBob = z; -} - //========================================================================== // (non-fatal with non-existent types only in DECORATE) //========================================================================== diff --git a/src/scripting/vmthunks_actors.cpp b/src/scripting/vmthunks_actors.cpp index 603ac9773..17718e024 100644 --- a/src/scripting/vmthunks_actors.cpp +++ b/src/scripting/vmthunks_actors.cpp @@ -1651,7 +1651,7 @@ DEFINE_ACTION_FUNCTION_NATIVE(AActor, CheckFor3DCeilingHit, CheckFor3DCeilingHit //=========================================================================== // -// APlayerPawn :: MarkPlayerSounds +// APlayerPawn functions // //=========================================================================== @@ -1662,6 +1662,19 @@ DEFINE_ACTION_FUNCTION_NATIVE(APlayerPawn, MarkPlayerSounds, S_MarkPlayerSounds) return 0; } +static void GetPrintableDisplayNameJit(PClassActor *cls, FString *result) +{ + *result = cls->GetDisplayName(); +} + +DEFINE_ACTION_FUNCTION_NATIVE(APlayerPawn, GetPrintableDisplayName, GetPrintableDisplayNameJit) +{ + PARAM_PROLOGUE; + PARAM_CLASS(type, AActor); + ACTION_RETURN_STRING(type->GetDisplayName()); +} + + DEFINE_FIELD(AActor, snext) DEFINE_FIELD(AActor, player) diff --git a/wadsrc/static/zscript/shared/player.txt b/wadsrc/static/zscript/shared/player.txt index 461fc8686..ca927ae0d 100644 --- a/wadsrc/static/zscript/shared/player.txt +++ b/wadsrc/static/zscript/shared/player.txt @@ -15,6 +15,8 @@ class PlayerPawn : Actor native const CROUCHSPEED = (1./12); // [RH] # of ticks to complete a turn180 const TURN180_TICKS = ((TICRATE / 4) + 1); + // 16 pixels of bob + const MAXBOB = 16.; native int crouchsprite; native int MaxHealth; @@ -72,6 +74,7 @@ class PlayerPawn : Actor native property FlechetteType: FlechetteType; property Portrait: Portrait; property TeleportFreezeTime: TeleportFreezeTime; + property ViewBob: ViewBob; Default { @@ -451,6 +454,133 @@ class PlayerPawn : Actor native } } } + + /* + ================== + = + = P_CalcHeight + = + = + Calculate the walking / running height adjustment + = + ================== + */ + + virtual void CalcHeight() + { + let player = self.player; + double angle; + double bob; + bool still = false; + + // Regular movement bobbing + // (needs to be calculated for gun swing even if not on ground) + + // killough 10/98: Make bobbing depend only on player-applied motion. + // + // Note: don't reduce bobbing here if on ice: if you reduce bobbing here, + // it causes bobbing jerkiness when the player moves from ice to non-ice, + // and vice-versa. + + if (player.cheats & CF_NOCLIP2) + { + player.bob = 0; + } + else if (bNoGravity && !player.onground) + { + player.bob = 0.5; + } + else + { + player.bob = player.Vel dot player.Vel; + if (player.bob == 0) + { + still = true; + } + else + { + player.bob *= player.GetMoveBob(); + + if (player.bob > MAXBOB) + player.bob = MAXBOB; + } + } + + double defaultviewheight = ViewHeight + player.crouchviewdelta; + + if (player.cheats & CF_NOVELOCITY) + { + player.viewz = pos.Z + defaultviewheight; + + if (player.viewz > ceilingz-4) + player.viewz = ceilingz-4; + + return; + } + + if (still) + { + if (player.health > 0) + { + angle = level.time / (120 * TICRATE / 35.) * 360.; + bob = player.GetStillBob() * sin(angle); + } + else + { + bob = 0; + } + } + else + { + angle = level.time / (20 * TICRATE / 35.) * 360.; + bob = player.bob * sin(angle) * (waterlevel > 1 ? 0.25f : 0.5f); + } + + // move viewheight + if (player.playerstate == PST_LIVE) + { + player.viewheight += player.deltaviewheight; + + if (player.viewheight > defaultviewheight) + { + player.viewheight = defaultviewheight; + player.deltaviewheight = 0; + } + else if (player.viewheight < (defaultviewheight/2)) + { + player.viewheight = defaultviewheight/2; + if (player.deltaviewheight <= 0) + player.deltaviewheight = 1 / 65536.; + } + + if (player.deltaviewheight) + { + player.deltaviewheight += 0.25; + if (!player.deltaviewheight) + player.deltaviewheight = 1/65536.; + } + } + + if (player.morphTics) + { + bob = 0; + } + player.viewz = pos.Z + player.viewheight + (bob * clamp(ViewBob, 0. , 1.5)); // [SP] Allow DECORATE changes to view bobbing speed. + if (Floorclip && player.playerstate != PST_DEAD + && pos.Z <= floorz) + { + player.viewz -= Floorclip; + } + if (player.viewz > ceilingz - 4) + { + player.viewz = ceilingz - 4; + } + if (player.viewz < floorz + 4) + { + player.viewz = floorz + 4; + } + } + //========================================================================== // @@ -1445,7 +1575,7 @@ class PlayerPawn : Actor native CheckUse(); CheckUndoMorph(); // Cycle psprites. - // Note that after self point the PlayerPawn may have changed due to getting unmorphed so 'self' is no longer safe to use. + // Note that after this point the PlayerPawn may have changed due to getting unmorphed so 'self' is no longer safe to use. player.mo.TickPSprites(); // Other Counters if (player.damagecount) player.damagecount--; @@ -1474,6 +1604,7 @@ class PlayerPawn : Actor native void BringUpWeapon () { + let player = self.player; if (player.PendingWeapon == WP_NOCHANGE) { if (player.ReadyWeapon != null) @@ -2184,7 +2315,6 @@ class PlayerPawn : Actor native native bool ResetAirSupply (bool playgasp = true); native clearscope static String GetPrintableDisplayName(Class cls); native void CheckMusicChange(); - native void CalcHeight (); native void CheckEnvironment(); native void CheckUse(); native void CheckWeaponButtons(); @@ -2452,6 +2582,8 @@ struct PlayerInfo native play // self is what internally is known as player_t native float GetAutoaim() const; native bool GetNoAutostartMap() const; native double GetWBobSpeed() const; + native double GetMoveBob() const; + native double GetStillBob() const; native void SetFOV(float fov); native bool GetClassicFlight() const; native clearscope bool HasWeaponsInSlot(int slot) const; @@ -2471,7 +2603,7 @@ struct PlayerInfo native play // self is what internally is known as player_t { if (player.mo != null) { - return player.mo.UndoPlayerMorph(self, unmorphflag, force); + return player.mo.UndoPlayerMorph(self, unmorphflag, force); } return false; }