Skip to content

Commit 35ba241

Browse files
authored
Player position and velocity (#514)
Set elevator speed
1 parent 1e994f6 commit 35ba241

File tree

5 files changed

+122
-4
lines changed

5 files changed

+122
-4
lines changed

TheForceEngine/TFE_DarkForces/Scripting/gs_player.cpp

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
#include <TFE_DarkForces/mission.h>
55
#include <TFE_DarkForces/pickup.h>
66
#include <TFE_DarkForces/weapon.h>
7+
#include <TFE_Jedi/Collision/collision.h>
8+
#include <TFE_FrontEndUI/frontEndUi.h>
79
#include <TFE_System/system.h>
810
#include <angelscript.h>
911

@@ -33,6 +35,91 @@ namespace TFE_DarkForces
3335
s_reviveTick = s_curTick + 436;
3436
}
3537

38+
vec3_float getPlayerPosition()
39+
{
40+
return
41+
{
42+
fixed16ToFloat(s_playerObject->posWS.x),
43+
-fixed16ToFloat(s_playerObject->posWS.y),
44+
fixed16ToFloat(s_playerObject->posWS.z),
45+
};
46+
}
47+
48+
void setPlayerPosition(vec3_float position)
49+
{
50+
fixed16_16 newX = floatToFixed16(position.x);
51+
fixed16_16 newY = -floatToFixed16(position.y);
52+
fixed16_16 newZ = floatToFixed16(position.z);
53+
54+
// Check if player object has moved out of its current sector
55+
// Start with a bounds check
56+
bool outOfHorzBounds = newX < s_playerObject->sector->boundsMin.x || newX > s_playerObject->sector->boundsMax.x ||
57+
newZ < s_playerObject->sector->boundsMin.z || newZ > s_playerObject->sector->boundsMax.z;
58+
bool outOfVertBounds = newY < s_playerObject->sector->ceilingHeight || newY > s_playerObject->sector->floorHeight;
59+
60+
// If within bounds, check if it has passed through a wall
61+
RWall* collisionWall = nullptr;
62+
if (!outOfHorzBounds && !outOfVertBounds)
63+
{
64+
collisionWall = collision_wallCollisionFromPath(s_playerObject->sector, s_playerObject->posWS.x, s_playerObject->posWS.z, newX, newZ);
65+
}
66+
67+
if (collisionWall || outOfHorzBounds || outOfVertBounds)
68+
{
69+
// Try to find a new sector for the player object; if there is none, don't move it
70+
RSector* sector = sector_which3D(newX, newY, newZ);
71+
if (sector)
72+
{
73+
if (sector != s_playerObject->sector)
74+
{
75+
sector_addObject(sector, s_playerObject);
76+
}
77+
}
78+
else
79+
{
80+
TFE_FrontEndUI::logToConsole("Cannot find a sector to move player to.");
81+
return;
82+
}
83+
}
84+
85+
s_playerObject->posWS.x = newX;
86+
s_playerObject->posWS.y = newY;
87+
s_playerObject->posWS.z = newZ;
88+
s_playerYPos = newY;
89+
}
90+
91+
float getPlayerYaw()
92+
{
93+
return angleToFloat(s_playerObject->yaw);
94+
}
95+
96+
void setPlayerYaw(float value)
97+
{
98+
float yaw = floatToAngle(value);
99+
s_playerObject->yaw = yaw;
100+
s_playerYaw = yaw;
101+
}
102+
103+
vec3_float getPlayerVelocity()
104+
{
105+
vec3_fixed velFixed;
106+
TFE_DarkForces::player_getVelocity(&velFixed);
107+
108+
return {
109+
fixed16ToFloat(velFixed.x),
110+
-fixed16ToFloat(velFixed.y),
111+
fixed16ToFloat(velFixed.z),
112+
};
113+
}
114+
115+
void setPlayerVelocity(vec3_float vel)
116+
{
117+
s_playerVelX = floatToFixed16(vel.x);
118+
s_playerUpVel = -floatToFixed16(vel.y);
119+
s_playerUpVel2 = -floatToFixed16(vel.y);
120+
s_playerVelZ = floatToFixed16(vel.z);
121+
}
122+
36123
s32 getBatteryPercent()
37124
{
38125
return (s32)(100.0 * s_batteryPower / FIXED(2) );
@@ -492,6 +579,14 @@ namespace TFE_DarkForces
492579

493580
ScriptObjFunc("void kill()", killPlayer);
494581

582+
// Position and velocity
583+
ScriptPropertyGetFunc("float3 get_position()", getPlayerPosition);
584+
ScriptPropertySetFunc("void set_position(float3)", setPlayerPosition);
585+
ScriptPropertyGetFunc("float get_yaw()", getPlayerYaw);
586+
ScriptPropertySetFunc("void set_yaw(float)", setPlayerYaw);
587+
ScriptPropertyGetFunc("float3 get_velocity()", getPlayerVelocity);
588+
ScriptPropertySetFunc("void set_velocity(float3)", setPlayerVelocity);
589+
495590
// Health / ammo getters
496591
ScriptLambdaPropertyGet("int get_health()", s32, { return s_playerInfo.health; });
497592
ScriptLambdaPropertyGet("int get_shields()", s32, { return s_playerInfo.shields; });

TheForceEngine/TFE_DarkForces/Scripting/scriptElev.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,14 @@ namespace TFE_DarkForces
2323
return (data->updateFlags & ELEV_MASTER_ON) != 0;
2424
}
2525

26+
void setElevSpeed(s32 value, ScriptElev* elev)
27+
{
28+
if (!ScriptElev::isScriptElevValid(elev)) { return; }
29+
30+
InfElevator* data = (InfElevator*)allocator_getByIndex(s_infSerState.infElevators, elev->m_id);
31+
data->speed = data->type == IELEV_ROTATE_WALL ? FIXED(floatToAngle(value)) : FIXED(value);
32+
}
33+
2634
void ScriptElev::registerType()
2735
{
2836
s32 res = 0;
@@ -35,5 +43,7 @@ namespace TFE_DarkForces
3543
ScriptObjFunc("bool isValid()", isScriptElevValid);
3644
// Properties
3745
ScriptPropertyGetFunc("bool get_master()", getElevMaster);
46+
47+
ScriptPropertySetFunc("void set_speed(int)", setElevSpeed);
3848
}
3949
}

TheForceEngine/TFE_DarkForces/player.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -142,10 +142,6 @@ namespace TFE_DarkForces
142142
static fixed16_16 s_minEyeDistFromFloor;
143143
static fixed16_16 s_postLandVel;
144144
static fixed16_16 s_landUpVel = 0;
145-
static fixed16_16 s_playerVelX;
146-
static fixed16_16 s_playerUpVel;
147-
static fixed16_16 s_playerUpVel2;
148-
static fixed16_16 s_playerVelZ;
149145
static fixed16_16 s_externalVelX;
150146
static fixed16_16 s_externalVelZ;
151147
static fixed16_16 s_playerCrouchSpd;
@@ -241,6 +237,12 @@ namespace TFE_DarkForces
241237
angle14_32 s_camOffsetRoll = 0;
242238
angle14_32 s_playerYaw;
243239

240+
// Expose player velocity for scripting
241+
fixed16_16 s_playerVelX;
242+
fixed16_16 s_playerUpVel;
243+
fixed16_16 s_playerUpVel2;
244+
fixed16_16 s_playerVelZ;
245+
244246
// Based on positioning in inventory, these were probably meant to represent thermal
245247
// detonators and mines - see player_readInfo() and player_writeInfo()
246248
JBool s_itemUnknown1; // 0x282428

TheForceEngine/TFE_DarkForces/player.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,12 @@ namespace TFE_DarkForces
111111
extern Tick s_prevPlayerTick;
112112
extern Tick s_reviveTick;
113113

114+
// Velocity - exposed for scripting
115+
extern fixed16_16 s_playerVelX;
116+
extern fixed16_16 s_playerUpVel;
117+
extern fixed16_16 s_playerUpVel2;
118+
extern fixed16_16 s_playerVelZ;
119+
114120
extern SecObject* s_playerObject;
115121
extern SecObject* s_playerEye;
116122

TheForceEngine/TFE_Jedi/Math/fixedPoint.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,11 @@ namespace TFE_Jedi
118118
return angle14_16(angle * 45.5111f);
119119
}
120120

121+
inline f32 angleToFloat(angle14_16 angle)
122+
{
123+
return angle / 45.5111f;
124+
}
125+
121126
// Convert a floating point angle from [0, 2pi) to a fixed point value where 0 -> 0 and 2pi -> 16384
122127
// This isn't really the same as fixed16_16 but matches the original source.
123128
inline fixed16_16 floatAngleToFixed(f32 angle)

0 commit comments

Comments
 (0)