Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions addons/sourcemod/scripting/include/movementapi.inc
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,30 @@ native void Movement_SetLandingOrigin(int client, float origin[3]);
*/
native void Movement_SetLandingVelocity(int client, float velocity[3]);

#define MOVEMENT_STATE_SIZE 64

/**
* Get what MovementAPI tracks about the player between ticks (takeoff, landing, perf, turning),
* to restore it later with Movement_SetState. Ticks and cmdnums are kept relative to the last usercmd.
*
* @param client Client index.
* @param state Buffer to store the state in.
* @param size Size of the buffer, must be MOVEMENT_STATE_SIZE.
* @error Wrong buffer size.
*/
native void Movement_GetState(int client, any[] state, int size = MOVEMENT_STATE_SIZE);

/**
* Set the player's state from Movement_GetState. Call it between the player's usercmds.
*
* @param client Client index.
* @param state State to set.
* @param size Size of the state, must be MOVEMENT_STATE_SIZE.
* @return Whether the state was set, false if another version took it.
* @error Wrong state size.
*/
native bool Movement_SetState(int client, const any[] state, int size = MOVEMENT_STATE_SIZE);

#define MOVEMENT_MAX_COLLISIONS 4

/**
Expand Down Expand Up @@ -735,6 +759,16 @@ methodmap MovementAPIPlayer < MovementPlayer {
{
Movement_GetProcessingOrigin(this.ID, buffer);
}

public void GetState(any[] state, int size = MOVEMENT_STATE_SIZE)
{
Movement_GetState(this.ID, state, size);
}

public bool SetState(const any[] state, int size = MOVEMENT_STATE_SIZE)
{
return Movement_SetState(this.ID, state, size);
}
}


Expand Down Expand Up @@ -781,6 +815,8 @@ public void __pl_movementapi_SetNTVOptional()
MarkNativeAsOptional("Movement_SetTakeoffVelocity");
MarkNativeAsOptional("Movement_SetLandingOrigin");
MarkNativeAsOptional("Movement_SetLandingVelocity");
MarkNativeAsOptional("Movement_GetState");
MarkNativeAsOptional("Movement_SetState");
MarkNativeAsOptional("Movement_GetCollisionCount");
MarkNativeAsOptional("Movement_GetCollisionStartOrigin");
MarkNativeAsOptional("Movement_GetCollisionEndOrigin");
Expand Down
121 changes: 121 additions & 0 deletions addons/sourcemod/scripting/movementapi/natives.sp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ void CreateNatives()
CreateNative("Movement_SetTakeoffVelocity", Native_SetTakeoffVelocity);
CreateNative("Movement_SetLandingOrigin", Native_SetLandingOrigin);
CreateNative("Movement_SetLandingVelocity", Native_SetLandingVelocity);
CreateNative("Movement_GetState", Native_GetState);
CreateNative("Movement_SetState", Native_SetState);

CreateNative("Movement_GetCollisionCount", Native_GetCollisionCount);
CreateNative("Movement_GetCollisionStartOrigin", Native_GetCollisionStartOrigin);
Expand Down Expand Up @@ -197,6 +199,125 @@ public int Native_SetLandingVelocity(Handle plugin, int numParams)
return 0;
}

public int Native_GetState(Handle plugin, int numParams)
{
int client = GetNativeCell(1);
if (!ValidStateSize(GetNativeCell(3)))
{
return 0;
}
any state[STATE_SIZE];
SaveState(client, state);
SetNativeArray(2, state, sizeof(state));
return 0;
}

public int Native_SetState(Handle plugin, int numParams)
{
int client = GetNativeCell(1);
if (!ValidStateSize(GetNativeCell(3)))
{
return view_as<int>(false);
}
any state[STATE_SIZE];
GetNativeArray(2, state, sizeof(state));
if (state[0] != STATE_VERSION)
{
return view_as<int>(false);
}
LoadState(client, state);
return view_as<int>(true);
}

static bool ValidStateSize(int size)
{
if (size != STATE_SIZE)
{
ThrowNativeError(SP_ERROR_NATIVE, "State size %d doesn't match MovementAPI's %d.", size, STATE_SIZE);
return false;
}
return true;
}

// Cell 0 is the layout's version: a new field bumps STATE_VERSION, and LoadState must still read older states.
static void SaveState(int client, any state[STATE_SIZE])
{
int i = 0;
state[i++] = STATE_VERSION;
state[i++] = gB_Jumped[client];
state[i++] = gB_HitPerf[client];
SaveVector(state, i, gF_NobugLandingOrigin[client]);
SaveVector(state, i, gF_LandingOrigin[client]);
SaveVector(state, i, gF_LandingVelocity[client]);
state[i++] = gI_LandingTick[client] - gI_TickCount[client];
state[i++] = gI_LandingCmdNum[client] - gI_Cmdnum[client];
SaveVector(state, i, gF_TakeoffOrigin[client]);
SaveVector(state, i, gF_TakeoffVelocity[client]);
state[i++] = gI_TakeoffTick[client] - gI_TickCount[client];
state[i++] = gI_TakeoffCmdNum[client] - gI_Cmdnum[client];
SaveVector(state, i, gF_TakeoffLadderNormal[client]);
state[i++] = gB_Turning[client];
state[i++] = gB_TurningLeft[client];
SaveVector(state, i, gF_OldOrigin[client]);
SaveVector(state, i, gF_OldVelocity[client]);
SaveVector(state, i, gF_OldEyeAngles[client]);
state[i++] = gB_OldOnGround[client];
state[i++] = gB_OldDucking[client];
state[i++] = gMT_OldMovetype[client];
state[i++] = gB_OldWalkMoved[client];
state[i++] = gB_Ducking[client];
state[i++] = gB_Duckbugged[client];
state[i++] = gB_Jumpbugged[client];
// A ladder takeoff can read it before this tick's LadderMove sets it.
SaveVector(state, i, gF_PostLadderMoveVelocity[client]);
}

static void LoadState(int client, const any state[STATE_SIZE])
{
int i = 1;
gB_Jumped[client] = state[i++];
gB_HitPerf[client] = state[i++];
LoadVector(state, i, gF_NobugLandingOrigin[client]);
LoadVector(state, i, gF_LandingOrigin[client]);
LoadVector(state, i, gF_LandingVelocity[client]);
gI_LandingTick[client] = gI_TickCount[client] + state[i++];
gI_LandingCmdNum[client] = gI_Cmdnum[client] + state[i++];
LoadVector(state, i, gF_TakeoffOrigin[client]);
LoadVector(state, i, gF_TakeoffVelocity[client]);
gI_TakeoffTick[client] = gI_TickCount[client] + state[i++];
gI_TakeoffCmdNum[client] = gI_Cmdnum[client] + state[i++];
LoadVector(state, i, gF_TakeoffLadderNormal[client]);
gB_Turning[client] = state[i++];
gB_TurningLeft[client] = state[i++];
LoadVector(state, i, gF_OldOrigin[client]);
LoadVector(state, i, gF_OldVelocity[client]);
LoadVector(state, i, gF_OldEyeAngles[client]);
gB_OldOnGround[client] = state[i++];
gB_OldDucking[client] = state[i++];
gMT_OldMovetype[client] = state[i++];
gB_OldWalkMoved[client] = state[i++];
gB_Ducking[client] = state[i++];
gB_Duckbugged[client] = state[i++];
gB_Jumpbugged[client] = state[i++];
LoadVector(state, i, gF_PostLadderMoveVelocity[client]);
}

static void SaveVector(any state[STATE_SIZE], int &i, const float vector[3])
{
for (int j = 0; j < 3; j++)
{
state[i++] = vector[j];
}
}

static void LoadVector(const any state[STATE_SIZE], int &i, float vector[3])
{
for (int j = 0; j < 3; j++)
{
vector[j] = state[i++];
}
}

public int Native_GetCollisionCount(Handle plugin, int numParams)
{
return gI_CollisionCount[GetNativeCell(1)];
Expand Down
2 changes: 2 additions & 0 deletions addons/sourcemod/scripting/movementapi/stocks.sp
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#define MAX_BUMPS 4
#define STATE_SIZE 64
#define STATE_VERSION 1

methodmap Vector
{
Expand Down
Loading