-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayer.h
More file actions
122 lines (102 loc) · 4.03 KB
/
Copy pathPlayer.h
File metadata and controls
122 lines (102 loc) · 4.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#pragma once
#include <cmath>
#include <string>
#include <pybind11/embed.h>
#include "../../ue/Engine.h"
/**
* @file
* @brief pybind11 module exposing the local character/controller to user scripts as
* `SplitgateInternal.Player.*` — location/teleport, health, view rotation, console, and chat.
* Every call guards on a valid, in-game controller and pawn, so a script can't crash the game by
* poking a null pointer.
*/
namespace py = pybind11;
namespace Scripts
{
namespace Modules
{
/// The local pawn (AcknowledgedPawn) or nullptr if not in game.
inline AActor* LocalPawn()
{
if (!Engine::PlayerController) return nullptr;
return reinterpret_cast<AActor*>(Engine::PlayerController->AcknowledgedPawn);
}
/// The local character (for health / character-specific fields) or nullptr.
inline APortalWarsCharacter* LocalCharacter()
{
if (!Engine::PlayerController) return nullptr;
return reinterpret_cast<APortalWarsCharacter*>(Engine::PlayerController->Character);
}
/// Registers the `Player` submodule: local location/teleport, health, view rotation, and the
/// console/chat helpers.
void Player(py::module_& m)
{
auto player = m.def_submodule("Player");
player.def("is_in_game", []
{ return Engine::IsInGame.load(); });
player.def("location", []() -> py::object
{
auto* pawn = LocalPawn();
if (!pawn) return py::none();
FVector v = pawn->K2_GetActorLocation();
return py::make_tuple(v.X, v.Y, v.Z); });
// Teleport keeping the current view rotation. Returns whether the move succeeded.
player.def("teleport", [](float x, float y, float z) -> bool
{
auto* pawn = LocalPawn();
if (!pawn) return false;
FRotator rot = Engine::PlayerController->ControlRotation;
return pawn->K2_TeleportTo(FVector{x, y, z}, rot); }, py::arg("x"), py::arg("y"), py::arg("z"));
player.def("health", []() -> py::object
{
auto* c = LocalCharacter();
if (!c) return py::none();
return py::float_(c->Health); });
player.def("set_health", [](float value)
{
auto* c = LocalCharacter();
if (c) c->Health = value; }, py::arg("value"));
player.def("view_rotation", []() -> py::object
{
if (!Engine::PlayerController) return py::none();
FRotator r = Engine::PlayerController->ControlRotation;
return py::make_tuple(r.Pitch, r.Yaw, r.Roll); });
player.def("set_view_rotation", [](float pitch, float yaw, float roll)
{
if (!Engine::PlayerController) return;
FRotator r{pitch, yaw, roll};
Engine::PlayerController->SetControlRotation(r); }, py::arg("pitch"), py::arg("yaw"), py::arg("roll") = 0.f);
player.def("velocity", []() -> py::object
{
auto* pawn = LocalPawn();
if (!pawn) return py::none();
FVector v = pawn->GetVelocity();
return py::make_tuple(v.X, v.Y, v.Z); });
// Point the view at a world position (eye-height origin), instantly. Handy for scripted aim.
player.def("aim_at", [](float x, float y, float z) -> bool
{
auto* pawn = LocalPawn();
if (!pawn) return false;
FVector eye = pawn->K2_GetActorLocation();
eye.Z += 80.f; // rough eye height
const float dx = x - eye.X, dy = y - eye.Y, dz = z - eye.Z;
constexpr float toDeg = 180.f / 3.14159265f;
FRotator r;
r.Yaw = std::atan2(dy, dx) * toDeg;
r.Pitch = std::atan2(dz, std::sqrt(dx * dx + dy * dy)) * toDeg;
r.Roll = 0.f;
Engine::PlayerController->SetControlRotation(r);
return true; }, py::arg("x"), py::arg("y"), py::arg("z"));
player.def("console", [](std::string command)
{
if (Engine::PlayerController) Engine::PlayerController->SendToConsole(FString(command)); }, py::arg("command"));
player.def("chat", [](std::string message)
{
if (Engine::PlayerController) Engine::PlayerController->SendChatMessage(FString(message)); }, py::arg("message"));
player.def("respawn", []
{
auto* c = LocalCharacter();
if (c) c->RequestSuicide(); }, "Kill the local character so it respawns.");
}
} // namespace Modules
} // namespace Scripts