Skip to content

Commit

Permalink
Added PlayerMixin.snap_to_position(origin=None, angles=None).
Browse files Browse the repository at this point in the history
  • Loading branch information
jordanbriere committed Oct 5, 2021
1 parent 1604995 commit ff7819a
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/core/modules/players/players_entity.cpp
Expand Up @@ -39,6 +39,7 @@
// >> EXTERNALS
// ============================================================================
extern IServerGameClients *servergameclients;
extern IServerTools *servertools;


// ============================================================================
Expand Down Expand Up @@ -81,6 +82,15 @@ bool PlayerMixin::IsWeapon()
return false;
}

void PlayerMixin::SnapToPosition(Vector *pOrigin, QAngle *pAngles)
{
servertools->SnapPlayerToPosition(
(pOrigin ? *pOrigin : GetOrigin()) + GetViewOffset(),
pAngles ? *pAngles : GetViewAngle(),
(IClientEntity *)GetEdict()->GetIServerEntity()
);
}

// CBasePlayer
float PlayerMixin::GetSpeed()
{
Expand Down
2 changes: 2 additions & 0 deletions src/core/modules/players/players_entity.h
Expand Up @@ -72,6 +72,8 @@ class PlayerMixin: public CBaseEntityWrapper
bool IsPlayer();
bool IsWeapon();

void SnapToPosition(Vector *pOrigin = NULL, QAngle *pAngles = NULL);

// CBasePlayer
// TODO: Return for some of these the proper entity class instead of a handle/index
// E. g. BaseEntity, Entity, Weapon, Player, etc.
Expand Down
10 changes: 10 additions & 0 deletions src/core/modules/players/players_wrap.cpp
Expand Up @@ -384,6 +384,16 @@ void export_player_wrapper(scope _players)
":rtype: bool"
);

_PlayerMixin.def(
"snap_to_position",
&PlayerMixin::SnapToPosition,
"Teleports the player at the given position and angles.\n\n"
":param Vector origin:\n"
" The coordinates to teleport the player at.\n"
":param QAngle angles:\n"
" The angles to set the player's view to.",
("self", arg("origin")=object(), arg("angles")=object()));

_PlayerMixin.add_property(
"speed",
&PlayerMixin::GetSpeed,
Expand Down

2 comments on commit ff7819a

@Ayuto
Copy link
Member

@Ayuto Ayuto commented on ff7819a Oct 5, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this the same like teleport?

@jordanbriere
Copy link
Contributor Author

@jordanbriere jordanbriere commented on ff7819a Oct 5, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this the same like teleport?

Entity.teleport, in addition of being a dynamic function that is slightly slower to call, does everything right away (notify move hierarchy, triggers and collidables detection, targets scanning (for bots), physics, etc, etc.) while this only snap the location and angles of the player. If you don't care about whether this is all done on this very frame, or that you know you are before the engine does everything either way (e.g. into a pre_think hook, OnPlayerRunCommand callback, or at any point before the movements are actually processed on the current frame, then you will benefit from using Player.snap_to_position. For example:

from players.entity import Player
from time import time

pl = Player(1)
o, a = pl.origin, pl.view_angle

t = time()
for i in range(1000000):
    pl.teleport(o, a)
print('Entity.teleport:', time() - t)

t = time()
for i in range(1000000):
    pl.snap_to_position(o, a)
print('Player.snap_to_position:', time() - t)
Entity.teleport: 48.85346794128418
Player.snap_to_position: 0.7007880210876465

The behaviours are essentially the same as doing:

pl.origin = o
pl.rotation = a
pl.view_angle = a
pl.entity_flags |= EntityFlags.DIRTY_ABSTRANSFORM

But that is considerably slower than a direct call: 7.805805683135986 (with v708 this time at 305.80093216896057...). Though, we could certainly improve these attributes.

Please sign in to comment.