Skip to content

Commit 39ca701

Browse files
committed
Improved performance of Player.view_angle.
Moved Player.get_view_angle and Player.set_view_angle to PlayerMixin. Added optional fixangle parameter to Player.set_view_angle. Fixed inaccuracy of the angle returned by Player.view_angle and PlayerMixin.get_view_angle (now matches client's getpos command). Exported fix angle constants as players.constants.FixAngle enumerator.
1 parent 1f87696 commit 39ca701

File tree

7 files changed

+64
-26
lines changed

7 files changed

+64
-26
lines changed

addons/source-python/packages/source-python/players/_base.py

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -405,22 +405,6 @@ def set_eye_location(self, eye_location):
405405

406406
eye_location = property(Entity.get_eye_location, set_eye_location)
407407

408-
def get_view_angle(self):
409-
"""Return the player's view angle.
410-
411-
:rtype: QAngle
412-
"""
413-
return super().view_angle
414-
415-
def set_view_angle(self, angle):
416-
"""Set the player's view angle."""
417-
# Make sure that only QAngle objects are passed. Otherwise you can
418-
# easily crash the server or cause unexpected behaviour
419-
assert isinstance(angle, QAngle)
420-
self.teleport(None, angle, None)
421-
422-
view_angle = property(get_view_angle, set_view_angle)
423-
424408
def push(self, horiz_mul, vert_mul, vert_override=False):
425409
"""Push the player along his view vector.
426410

addons/source-python/packages/source-python/players/constants.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
# =============================================================================
1717
# Source.Python Imports
1818
# Players
19+
from _players._constants import FIXANGLE_NONE
20+
from _players._constants import FIXANGLE_ABSOLUTE
21+
from _players._constants import FIXANGLE_RELATIVE
1922
from _players._constants import FL_ONGROUND
2023
from _players._constants import FL_DUCKING
2124
from _players._constants import FL_WATERJUMP
@@ -93,7 +96,8 @@
9396
# =============================================================================
9497
# >> ALL DECLARATION
9598
# =============================================================================
96-
__all__ = ('HideHudFlags',
99+
__all__ = ('FixAngle',
100+
'HideHudFlags',
97101
'HitGroup',
98102
'INVALID_PLAYER_USERID',
99103
'LifeState',
@@ -107,6 +111,14 @@
107111
# =============================================================================
108112
# >> ENUMERATORS
109113
# =============================================================================
114+
class FixAngle(IntEnum):
115+
"""Player fix angle enumerator."""
116+
117+
NONE = FIXANGLE_NONE
118+
ABSOLUTE = FIXANGLE_ABSOLUTE
119+
RELATIVE = FIXANGLE_RELATIVE
120+
121+
110122
class PlayerStates(IntFlag):
111123
"""Player states wrapper enumerator."""
112124

src/core/modules/players/players_constants_wrap.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ void export_hide_hud_flags(scope);
5454
void export_hit_groups(scope);
5555
void export_player_animation(scope);
5656
void export_observer_modes(scope);
57+
void export_fixangle(scope);
5758
void export_players_miscellaneous_constants(scope);
5859

5960

@@ -69,6 +70,7 @@ DECLARE_SP_SUBMODULE(_players, _constants)
6970
export_hit_groups(_constants);
7071
export_player_animation(_constants);
7172
export_observer_modes(_constants);
73+
export_fixangle(_constants);
7274
export_players_miscellaneous_constants(_constants);
7375
}
7476

@@ -223,6 +225,17 @@ void export_observer_modes(scope _constants)
223225
}
224226

225227

228+
//-----------------------------------------------------------------------------
229+
// Exports fixangle constants.
230+
//-----------------------------------------------------------------------------
231+
void export_fixangle(scope _constants)
232+
{
233+
_constants.attr("FIXANGLE_NONE") = FIXANGLE_NONE;
234+
_constants.attr("FIXANGLE_ABSOLUTE") = FIXANGLE_ABSOLUTE;
235+
_constants.attr("FIXANGLE_RELATIVE") = FIXANGLE_RELATIVE;
236+
}
237+
238+
226239
//-----------------------------------------------------------------------------
227240
// Exports miscellaneous constants.
228241
//-----------------------------------------------------------------------------

src/core/modules/players/players_entity.cpp

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,16 @@
3030
// Source.Python
3131
#include "players_entity.h"
3232

33+
// SDK
34+
#include "eiface.h"
35+
#include "public/PlayerState.h"
36+
37+
38+
// ============================================================================
39+
// >> EXTERNALS
40+
// ============================================================================
41+
extern IServerGameClients *servergameclients;
42+
3343

3444
// ============================================================================
3545
// >> PlayerMixin
@@ -448,16 +458,21 @@ void PlayerMixin::SetViewVector(Vector& value)
448458

449459
QAngle PlayerMixin::GetViewAngle()
450460
{
451-
QAngle eye_angle = GetEyeAngle();
452-
return QAngle(
453-
eye_angle.x,
454-
eye_angle.y < 0 ? eye_angle.y + 360 : eye_angle.y,
455-
GetRotation().z);
461+
CPlayerState *pState = servergameclients->GetPlayerState(GetEdict());
462+
if (!pState)
463+
BOOST_RAISE_EXCEPTION(PyExc_ValueError, "Failed to get the player state instance for this player.")
464+
465+
return pState->v_angle;
456466
}
457467

458-
void PlayerMixin::SetViewAngle(QAngle& value)
468+
void PlayerMixin::SetViewAngle(QAngle& value, int fixangle)
459469
{
460-
BOOST_RAISE_EXCEPTION(PyExc_NotImplementedError, "Setting view_angle is not implemented.");
470+
CPlayerState *pState = servergameclients->GetPlayerState(GetEdict());
471+
if (!pState)
472+
BOOST_RAISE_EXCEPTION(PyExc_ValueError, "Failed to get the player state instance for this player.")
473+
474+
pState->v_angle = value;
475+
pState->fixangle = fixangle;
461476
}
462477

463478

src/core/modules/players/players_entity.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ class PlayerMixin: public CBaseEntityWrapper
163163
void SetViewVector(Vector& value);
164164

165165
QAngle GetViewAngle();
166-
void SetViewAngle(QAngle& value);
166+
void SetViewAngle(QAngle& value, int fixangle = FIXANGLE_ABSOLUTE);
167167

168168
// Game specific
169169
// CS:S, CS:GO

src/core/modules/players/players_wrap.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -587,10 +587,22 @@ void export_player_wrapper(scope _players)
587587
"Get/set the player's view vector.\n\n"
588588
":rtype: Vector");
589589

590+
_PlayerMixin.def(
591+
"get_view_angle",
592+
&PlayerMixin::GetViewAngle,
593+
"Returns the player's view angle.\n\n"
594+
":rtype: QAngle");
595+
596+
_PlayerMixin.def(
597+
"set_view_angle",
598+
&PlayerMixin::SetViewAngle,
599+
"Sets the player's view angle.",
600+
("self", "angle", arg("fixangle")=FIXANGLE_ABSOLUTE));
601+
590602
_PlayerMixin.add_property(
591603
"view_angle",
592604
&PlayerMixin::GetViewAngle,
593-
&PlayerMixin::SetViewAngle,
605+
_PlayerMixin.attr("set_view_angle"),
594606
"Get/set the player's view angle.\n\n"
595607
":rtype: QAngle");
596608

src/core/sp_main.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ IEngineSound* enginesound = NULL;
9292
CGlobalVars* gpGlobals = NULL;
9393
IFileSystem* filesystem = NULL;
9494
IServerGameDLL* servergamedll = NULL;
95+
IServerGameClients* servergameclients = NULL;
9596
IServerTools* servertools = NULL;
9697
IPhysics* physics = NULL;
9798
IPhysicsCollision* physcollision = NULL;
@@ -153,6 +154,7 @@ InterfaceHelper_t gGameInterfaces[] = {
153154
{INTERFACEVERSION_PLAYERINFOMANAGER, (void **)&playerinfomanager},
154155
{INTERFACEVERSION_PLAYERBOTMANAGER, (void **)&botmanager},
155156
{INTERFACEVERSION_SERVERGAMEDLL, (void **)&servergamedll},
157+
{INTERFACEVERSION_SERVERGAMECLIENTS, (void **)&servergameclients},
156158
{VSERVERTOOLS_INTERFACE_VERSION, (void **)&servertools},
157159
{NULL, NULL}
158160
};

0 commit comments

Comments
 (0)