Skip to content

Commit

Permalink
Track various teleportations
Browse files Browse the repository at this point in the history
  • Loading branch information
timoschwarzer committed Nov 17, 2024
1 parent a30190a commit 986f94b
Show file tree
Hide file tree
Showing 3 changed files with 153 additions and 17 deletions.
19 changes: 15 additions & 4 deletions projects/Randomizer/stats/game_stats.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,21 @@ namespace randomizer::timing {
++this->area_stats[area].deaths;
}

void SaveFileGameStats::report_teleport(const app::Vector2& from, const app::Vector2& to, TeleportReason reason) {
teleports.emplace_back(
from.x,
from.y,
to.x,
to.y,
this->in_game_time,
reason
);

if (reason == TeleportReason::Teleporter) {
++this->teleport_count;
}
}

nlohmann::json SaveFileGameStats::json_serialize() {
nlohmann::json j = *this;
return j;
Expand All @@ -50,10 +65,6 @@ namespace randomizer::timing {
this->time_since_last_checkpoint = 0.f;
}

void SaveFileGameStats::report_teleport() {
++this->teleport_count;
}

void SaveFileGameStats::report_pickup(GameArea area, const std::string& location_name) {
if (!this->collected_pickups.contains(location_name)) {
this->recent_pickup_timers.push_back(PPM_TIMESPAN);
Expand Down
49 changes: 46 additions & 3 deletions projects/Randomizer/stats/game_stats.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
#include <Core/enums/game_areas.h>
#include <Core/enums/async_loading_state.h>
#include <Core/enums/world_events.h>
#include <Core/mood_guid.h>
#include <Core/save_meta/save_meta.h>
#include <nlohmann/json.hpp>
#include <unordered_map>

#include <Modloader/app/structs/AbilityType__Enum.h>
#include <Modloader/app/structs/Vector2.h>

NLOHMANN_JSON_NAMESPACE_BEGIN
template <typename T>
Expand Down Expand Up @@ -110,6 +110,43 @@ namespace randomizer::timing {
);
};

enum class TeleportReason {
Unknown,
Teleporter,
Death,
Door,
Portal,
};

NLOHMANN_JSON_SERIALIZE_ENUM(
TeleportReason,
{
{TeleportReason::Unknown, "Unknown" },
{TeleportReason::Teleporter, "Teleporter"},
{TeleportReason::Death, "Death" },
{TeleportReason::Door, "Door" },
{TeleportReason::Portal, "Portal" },
}
);

struct Teleport {
float from_x = 0.f;
float from_y = 0.f;
float to_x = 0.f;
float to_y = 0.f;
float in_game_time = 0.f;
TeleportReason reason = TeleportReason::Unknown;

NLOHMANN_DEFINE_TYPE_INTRUSIVE_WITH_DEFAULT(
Teleport,
from_x,
from_y,
to_x,
to_y,
reason
)
};

// Tracking
float time_since_last_checkpoint = 0.f;
float in_game_time = 0.f;
Expand Down Expand Up @@ -137,6 +174,11 @@ namespace randomizer::timing {
*/
std::map<WorldEvent, float> world_event_timestamps;

/**
* Teleports
*/
std::vector<Teleport> teleports;

// Stats
float max_ppm_over_timespan = 0.f;
float max_ppm_over_timespan_at = 0.f;
Expand All @@ -155,6 +197,7 @@ namespace randomizer::timing {
collected_pickups,
ability_timestamps,
world_event_timestamps,
teleports,
max_ppm_over_timespan,
max_ppm_over_timespan_at,
time_lost_to_deaths,
Expand All @@ -176,12 +219,12 @@ namespace randomizer::timing {

void report_death(GameArea area);

void report_teleport(const app::Vector2& from, const app::Vector2& to, TeleportReason reason);

void report_checkpoint_created();

void report_respawn();

void report_teleport();

float get_total_async_loading_time();

nlohmann::json json_serialize() override;
Expand Down
102 changes: 92 additions & 10 deletions projects/Randomizer/tracking/game_tracker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
#include <Modloader/app/methods/GameStateMachine.h>
#include <Modloader/app/methods/PlayerAbilities.h>
#include <Modloader/app/methods/TimeUtility.h>
#include <Modloader/app/methods/SavePedestalController.h>
#include <Modloader/app/methods/ScenesManager.h>
#include <Modloader/app/methods/SeinDoorHandler.h>
#include <Modloader/app/methods/Portal.h>
#include <Modloader/app/methods/PlatformMovementPortalVisitor.h>
#include <Modloader/interception_macros.h>
#include <Modloader/modloader.h>
#include <Modloader/windows_api/console.h>
Expand Down Expand Up @@ -146,6 +151,8 @@ namespace randomizer::timing {
}
);

std::optional<app::Vector2> death_position_before_respawn = std::nullopt;

auto on_respawn = core::api::game::event_bus().register_handler(
GameEvent::Respawn,
EventTiming::Before,
Expand All @@ -154,19 +161,17 @@ namespace randomizer::timing {
return;
}

save_stats->report_respawn();
}
);
if (death_position_before_respawn.has_value()) {
save_stats->report_teleport(
death_position_before_respawn.value(),
modloader::math::convert(core::api::game::player::get_position()),
SaveFileGameStats::TeleportReason::Death
);

auto on_teleport = core::api::game::event_bus().register_handler(
GameEvent::Teleport,
EventTiming::Before,
[](GameEvent event, EventTiming timing) {
if (!timer_should_run()) {
return;
death_position_before_respawn = std::nullopt;
}

save_stats->report_teleport();
save_stats->report_respawn();
}
);

Expand All @@ -177,6 +182,7 @@ namespace randomizer::timing {
return;
}

death_position_before_respawn = modloader::math::convert(core::api::game::player::get_position());
save_stats->report_death(core::api::game::player::get_current_area());
}
);
Expand Down Expand Up @@ -307,6 +313,82 @@ namespace randomizer::timing {

next::PlayerAbilities::SetAbility(this_ptr, ability, value);
}

auto scenes_manager_on_teleport_called_since_last_sein_door_handler_fixed_update = false;

IL2CPP_INTERCEPT(ScenesManager, void, OnTeleport, (app::ScenesManager * this_ptr, bool update_camera_target, bool move_camera_to_target)) {
next::ScenesManager::OnTeleport(this_ptr, update_camera_target, move_camera_to_target);
scenes_manager_on_teleport_called_since_last_sein_door_handler_fixed_update = true;
}

IL2CPP_INTERCEPT(SeinDoorHandler, void, FixedUpdate, (app::SeinDoorHandler* this_ptr)) {
scenes_manager_on_teleport_called_since_last_sein_door_handler_fixed_update = false;

const auto previous_position = core::api::game::player::get_position();
next::SeinDoorHandler::FixedUpdate(this_ptr);

if (!timer_should_run()) {
return;
}

if (scenes_manager_on_teleport_called_since_last_sein_door_handler_fixed_update) {
const auto new_position = core::api::game::player::get_position();

save_stats->report_teleport(
modloader::math::convert(previous_position),
modloader::math::convert(new_position),
SaveFileGameStats::TeleportReason::Door
);
}
}

IL2CPP_INTERCEPT(SavePedestalController, void, OnFadedToBlack, (app::SavePedestalController* this_ptr)) {
const auto previous_position = core::api::game::player::get_position();
next::SavePedestalController::OnFadedToBlack(this_ptr);

if (!timer_should_run()) {
return;
}

const auto new_position = core::api::game::player::get_position();

save_stats->report_teleport(
modloader::math::convert(previous_position),
modloader::math::convert(new_position),
SaveFileGameStats::TeleportReason::Teleporter
);
}

std::optional<app::Vector2> new_position_after_portal_teleportation = std::nullopt;

IL2CPP_INTERCEPT(Portal, void, PerformPortalTeleportation, (app::Portal * this_ptr, app::IPortalVisitor* portal_visitor)) {
const auto previous_position = core::api::game::player::get_position();
new_position_after_portal_teleportation = std::nullopt;

// This method will call set_Position on PlatformMovementPortalVisitor one or multiple times
next::Portal::PerformPortalTeleportation(this_ptr, portal_visitor);

if (!timer_should_run()) {
return;
}

if (!Portal::IsSein(this_ptr, portal_visitor)) {
return;
}

if (!new_position_after_portal_teleportation.has_value()) {
return;
}

save_stats->report_teleport(
modloader::math::convert(previous_position), *new_position_after_portal_teleportation, SaveFileGameStats::TeleportReason::Portal
);
}

IL2CPP_INTERCEPT(PlatformMovementPortalVisitor, void, set_Position, (app::PlatformMovementPortalVisitor* this_ptr, app::Vector3 value)) {
next::PlatformMovementPortalVisitor::set_Position(this_ptr, value);
new_position_after_portal_teleportation = modloader::math::convert(value);
}
} // namespace

void notify_pickup_collected(const GameArea area, const std::string& location_name) {
Expand Down

0 comments on commit 986f94b

Please sign in to comment.