Skip to content

Commit

Permalink
Add game event support
Browse files Browse the repository at this point in the history
  • Loading branch information
mtheall committed Nov 3, 2023
1 parent 693fa68 commit be6deae
Show file tree
Hide file tree
Showing 7 changed files with 799 additions and 120 deletions.
622 changes: 542 additions & 80 deletions python-mtheall/Arena.cpp

Large diffs are not rendered by default.

56 changes: 51 additions & 5 deletions python-mtheall/Car.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,21 @@ PyMemberDef Car::Members[] = {
.offset = offsetof (Car, goals),
.flags = 0,
.doc = "Goals"},
{.name = "shots",
.type = TypeHelper<decltype (Car::shots)>::type,
.offset = offsetof (Car, shots),
.flags = 0,
.doc = "Shots"},
{.name = "saves",
.type = TypeHelper<decltype (Car::saves)>::type,
.offset = offsetof (Car, saves),
.flags = 0,
.doc = "Saves"},
{.name = "assists",
.type = TypeHelper<decltype (Car::assists)>::type,
.offset = offsetof (Car, assists),
.flags = 0,
.doc = "Assists"},
{.name = nullptr, .type = 0, .offset = 0, .flags = 0, .doc = nullptr},
};

Expand Down Expand Up @@ -161,6 +176,15 @@ PyObject *Car::InternalPickle (Car *self_) noexcept
!DictSetValue (dict.borrow (), "boost_pickups", PyLong_FromUnsignedLong (self_->boostPickups)))
return nullptr;

if (self_->shots && !DictSetValue (dict.borrow (), "shots", PyLong_FromUnsignedLong (self_->shots)))
return nullptr;

if (self_->saves && !DictSetValue (dict.borrow (), "saves", PyLong_FromUnsignedLong (self_->saves)))
return nullptr;

if (self_->assists && !DictSetValue (dict.borrow (), "assists", PyLong_FromUnsignedLong (self_->assists)))
return nullptr;

return dict.gift ();
}

Expand All @@ -178,9 +202,22 @@ PyObject *Car::InternalUnpickle (std::shared_ptr<::Arena> arena_, Car *self_, Py
static char goalsKwd[] = "goals";
static char demosKwd[] = "demos";
static char boostPickupsKwd[] = "boost_pickups";

static char *dict[] = {
idKwd, teamKwd, stateKwd, configKwd, controlsKwd, goalsKwd, demosKwd, boostPickupsKwd, nullptr};
static char shotsKwd[] = "shots";
static char savesKwd[] = "saves";
static char assistsKwd[] = "assists";

static char *dict[] = {idKwd,
teamKwd,
stateKwd,
configKwd,
controlsKwd,
goalsKwd,
demosKwd,
boostPickupsKwd,
shotsKwd,
savesKwd,
assistsKwd,
nullptr};

PyObject *state = nullptr; // borrowed references
PyObject *config = nullptr;
Expand All @@ -189,10 +226,13 @@ PyObject *Car::InternalUnpickle (std::shared_ptr<::Arena> arena_, Car *self_, Py
unsigned goals = 0;
unsigned demos = 0;
unsigned boostPickups = 0;
unsigned shots = 0;
unsigned saves = 0;
unsigned assists = 0;
int team = static_cast<int> (::Team::BLUE);
if (!PyArg_ParseTupleAndKeywords (dummy.borrow (),
dict_,
"|kiO!O!O!III",
"|kiO!O!O!IIIIII",
dict,
&id,
&team,
Expand All @@ -204,7 +244,10 @@ PyObject *Car::InternalUnpickle (std::shared_ptr<::Arena> arena_, Car *self_, Py
&controls,
&goals,
&demos,
&boostPickups))
&boostPickups,
&shots,
&saves,
&assists))
return nullptr;

if (id == 0)
Expand Down Expand Up @@ -242,6 +285,9 @@ PyObject *Car::InternalUnpickle (std::shared_ptr<::Arena> arena_, Car *self_, Py
self_->goals = goals;
self_->demos = demos;
self_->boostPickups = boostPickups;
self_->shots = shots;
self_->saves = saves;
self_->assists = assists;

self_->car->SetState (CarState::ToCarState (PyCast<CarState> (state)));
self_->car->_internalState.updateCounter = PyCast<CarState> (state)->state.updateCounter;
Expand Down
13 changes: 7 additions & 6 deletions python-mtheall/CarState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -593,7 +593,6 @@ PyObject *CarState::Pickle (CarState *self_) noexcept
!DictSetValue (dict.borrow (), "demo_respawn_timer", PyFloat_FromDouble (state.demoRespawnTimer)))
return nullptr;

::BallHitInfo const ballHitInfo{};
if ((state.ballHitInfo.relativePosOnBall != model.ballHitInfo.relativePosOnBall ||
state.ballHitInfo.ballPos != model.ballHitInfo.ballPos ||
state.ballHitInfo.extraHitVel != model.ballHitInfo.extraHitVel ||
Expand All @@ -602,11 +601,13 @@ PyObject *CarState::Pickle (CarState *self_) noexcept
!DictSetValue (dict.borrow (), "ball_hit_info", PyNewRef (self_->ballHitInfo)))
return nullptr;

::CarControls const controls{};
if ((state.lastControls.throttle != controls.throttle || state.lastControls.steer != controls.steer ||
state.lastControls.pitch != controls.pitch || state.lastControls.yaw != controls.yaw ||
state.lastControls.roll != controls.roll || state.lastControls.boost != controls.boost ||
state.lastControls.jump != controls.jump || state.lastControls.handbrake != controls.handbrake) &&
if ((state.lastControls.throttle != model.lastControls.throttle ||
state.lastControls.steer != model.lastControls.steer ||
state.lastControls.pitch != model.lastControls.pitch || state.lastControls.yaw != model.lastControls.yaw ||
state.lastControls.roll != model.lastControls.roll ||
state.lastControls.boost != model.lastControls.boost ||
state.lastControls.jump != model.lastControls.jump ||
state.lastControls.handbrake != model.lastControls.handbrake) &&
!DictSetValue (dict.borrow (), "last_controls", PyNewRef (self_->lastControls)))
return nullptr;

Expand Down
66 changes: 42 additions & 24 deletions python-mtheall/Module.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "Sim/Arena/Arena.h"
#include "Sim/BallPredTracker/BallPredTracker.h"
#include "Sim/Car/Car.h"
#include "Sim/GameEventTracker/GameEventTracker.h"

#include <map>
#include <memory>
Expand Down Expand Up @@ -77,8 +78,7 @@ class GIL
PyGILState_Release (m_state);
}

GIL () noexcept
: m_state (PyGILState_Ensure ())
GIL () noexcept : m_state (PyGILState_Ensure ())
{
}

Expand All @@ -88,7 +88,7 @@ class GIL

struct GameMode
{
PyObject_HEAD
PyObject_HEAD;

static PyTypeObject *Type;
static PyType_Slot Slots[];
Expand All @@ -97,7 +97,7 @@ struct GameMode

struct Team
{
PyObject_HEAD
PyObject_HEAD;

static PyTypeObject *Type;
static PyType_Slot Slots[];
Expand All @@ -106,7 +106,7 @@ struct Team

struct DemoMode
{
PyObject_HEAD
PyObject_HEAD;

static PyTypeObject *Type;
static PyType_Slot Slots[];
Expand All @@ -115,7 +115,7 @@ struct DemoMode

struct MemoryWeightMode
{
PyObject_HEAD
PyObject_HEAD;

static PyTypeObject *Type;
static PyType_Slot Slots[];
Expand All @@ -124,7 +124,7 @@ struct MemoryWeightMode

struct Vec
{
PyObject_HEAD
PyObject_HEAD;

::Vec vec;

Expand Down Expand Up @@ -156,7 +156,7 @@ struct Vec

struct RotMat
{
PyObject_HEAD
PyObject_HEAD;

Vec *forward;
Vec *right;
Expand Down Expand Up @@ -193,7 +193,7 @@ struct RotMat

struct Angle
{
PyObject_HEAD
PyObject_HEAD;

::Angle angle;

Expand Down Expand Up @@ -224,7 +224,7 @@ struct Angle

struct BallHitInfo
{
PyObject_HEAD
PyObject_HEAD;

::BallHitInfo info;

Expand Down Expand Up @@ -258,7 +258,7 @@ struct BallHitInfo

struct BallState
{
PyObject_HEAD
PyObject_HEAD;

::BallState state;

Expand Down Expand Up @@ -294,7 +294,7 @@ struct BallState

struct Ball
{
PyObject_HEAD
PyObject_HEAD;

std::shared_ptr<::Arena> arena;
::Ball *ball;
Expand All @@ -316,7 +316,7 @@ struct Ball

struct BoostPadState
{
PyObject_HEAD
PyObject_HEAD;

::BoostPadState state;

Expand All @@ -341,7 +341,7 @@ struct BoostPadState

struct BoostPad
{
PyObject_HEAD
PyObject_HEAD;

std::shared_ptr<::Arena> arena;
::BoostPad *pad;
Expand All @@ -367,7 +367,7 @@ struct BoostPad

struct WheelPairConfig
{
PyObject_HEAD
PyObject_HEAD;

::WheelPairConfig config;
Vec *connectionPointOffset;
Expand Down Expand Up @@ -406,7 +406,7 @@ struct CarConfig
MERC,
};

PyObject_HEAD
PyObject_HEAD;

::CarConfig config;

Expand Down Expand Up @@ -443,7 +443,7 @@ struct CarConfig

struct CarControls
{
PyObject_HEAD
PyObject_HEAD;

::CarControls controls;

Expand All @@ -470,7 +470,7 @@ struct CarControls

struct CarState
{
PyObject_HEAD
PyObject_HEAD;

::CarState state;

Expand Down Expand Up @@ -514,7 +514,7 @@ struct CarState

struct Car
{
PyObject_HEAD
PyObject_HEAD;

::CarState demoState;

Expand All @@ -523,6 +523,9 @@ struct Car
unsigned goals;
unsigned demos;
unsigned boostPickups;
unsigned shots;
unsigned saves;
unsigned assists;

static PyTypeObject *Type;
static PyMemberDef Members[];
Expand Down Expand Up @@ -554,7 +557,7 @@ struct Car

struct MutatorConfig
{
PyObject_HEAD
PyObject_HEAD;

::MutatorConfig config;
Vec *gravity;
Expand All @@ -567,7 +570,8 @@ struct MutatorConfig
static PyType_Spec Spec;

static PyRef<MutatorConfig> NewFromMutatorConfig (::MutatorConfig const &config_ = {::GameMode::SOCCAR}) noexcept;
static bool InitFromMutatorConfig (MutatorConfig *self_, ::MutatorConfig const &config_ = {::GameMode::SOCCAR}) noexcept;
static bool InitFromMutatorConfig (MutatorConfig *self_,
::MutatorConfig const &config_ = {::GameMode::SOCCAR}) noexcept;
static ::MutatorConfig ToMutatorConfig (MutatorConfig *self_) noexcept;

static PyObject *New (PyTypeObject *subtype_, PyObject *args_, PyObject *kwds_) noexcept;
Expand All @@ -585,14 +589,15 @@ struct Arena
{
class ThreadPool;

PyObject_HEAD
PyObject_HEAD;

std::shared_ptr<::Arena> arena;
std::shared_ptr<ThreadPool> threadPool;
std::map<std::uint32_t, PyRef<Car>> *cars;
std::unordered_map<::BoostPad *, PyRef<BoostPad>> *boostPads;
std::vector<PyRef<BoostPad>> *boostPadsByIndex;
::BallPredTracker *ballPrediction;
::GameEventTracker *gameEvent;

Ball *ball;
PyObject *ballTouchCallback;
Expand All @@ -605,6 +610,12 @@ struct Arena
PyObject *carDemoCallbackUserData;
PyObject *goalScoreCallback;
PyObject *goalScoreCallbackUserData;
PyObject *shotEventCallback;
PyObject *shotEventCallbackUserData;
PyObject *goalEventCallback;
PyObject *goalEventCallbackUserData;
PyObject *saveEventCallback;
PyObject *saveEventCallbackUserData;

unsigned blueScore;
unsigned orangeScore;
Expand Down Expand Up @@ -645,11 +656,14 @@ struct Arena
static PyObject *ResetKickoff (Arena *self_, PyObject *args_, PyObject *kwds_) noexcept;
static PyObject *SetBallTouchCallback (Arena *self_, PyObject *args_, PyObject *kwds_) noexcept;
static PyObject *SetBoostPickupCallback (Arena *self_, PyObject *args_, PyObject *kwds_) noexcept;
static PyObject *SetCarBallCollision(Arena *self_, PyObject *args_, PyObject *kwds_) noexcept;
static PyObject *SetCarBallCollision (Arena *self_, PyObject *args_, PyObject *kwds_) noexcept;
static PyObject *SetCarBumpCallback (Arena *self_, PyObject *args_, PyObject *kwds_) noexcept;
static PyObject *SetCarCarCollision(Arena *self_, PyObject *args_, PyObject *kwds_) noexcept;
static PyObject *SetCarCarCollision (Arena *self_, PyObject *args_, PyObject *kwds_) noexcept;
static PyObject *SetCarDemoCallback (Arena *self_, PyObject *args_, PyObject *kwds_) noexcept;
static PyObject *SetGoalScoreCallback (Arena *self_, PyObject *args_, PyObject *kwds_) noexcept;
static PyObject *SetShotEventCallback (Arena *self_, PyObject *args_, PyObject *kwds_) noexcept;
static PyObject *SetGoalEventCallback (Arena *self_, PyObject *args_, PyObject *kwds_) noexcept;
static PyObject *SetSaveEventCallback (Arena *self_, PyObject *args_, PyObject *kwds_) noexcept;
static PyObject *SetMutatorConfig (Arena *self_, PyObject *args_, PyObject *kwds_) noexcept;
static PyObject *Step (Arena *self_, PyObject *args_, PyObject *kwds_) noexcept;
static PyObject *Stop (Arena *self_) noexcept;
Expand All @@ -663,6 +677,10 @@ struct Arena
HandleCarBumpCallback (::Arena *arena_, ::Car *bumper_, ::Car *victim_, bool isDemo_, void *userData_) noexcept;
static void HandleGoalScoreCallback (::Arena *arena_, ::Team scoringTeam_, void *userData_) noexcept;

static void HandleShotEventCallback (::Arena *arena_, ::Car *shooter_, ::Car *passer_, void *userData_) noexcept;
static void HandleGoalEventCallback (::Arena *arena_, ::Car *shooter_, ::Car *passer_, void *userData_) noexcept;
static void HandleSaveEventCallback (::Arena *arena_, ::Car *saver_, void *userData_) noexcept;

GETONLY_DECLARE (Arena, game_mode);
GETONLY_DECLARE (Arena, tick_count);
GETONLY_DECLARE (Arena, tick_rate);
Expand Down
Loading

0 comments on commit be6deae

Please sign in to comment.