Skip to content

Commit

Permalink
move warp, set_seed to StateMemory. Move and rename `State::fin…
Browse files Browse the repository at this point in the history
…d` to `StateMemory::get_entity`, move the rest of the function to the `API` namespace
  • Loading branch information
Mr-Auto committed Jan 6, 2025
1 parent d4b6044 commit 36a9dab
Show file tree
Hide file tree
Showing 9 changed files with 106 additions and 131 deletions.
5 changes: 1 addition & 4 deletions src/game_api/entity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -236,10 +236,7 @@ void Entity::set_enable_turning(bool enabled)

Entity* get_entity_ptr(uint32_t uid)
{
auto p = State::find(State::get().ptr(), uid);
// if (IsBadWritePtr(p, 0x178))
// return nullptr;
return p;
return HeapBase::get().state()->get_entity(uid);
}

std::vector<uint32_t> Movable::get_all_behaviors()
Expand Down
5 changes: 2 additions & 3 deletions src/game_api/layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include "entity.hpp" // for Entity, to_id, EntityDB, entity_factory
#include "logger.h" // for DEBUG
#include "movable.hpp" // for Movable
#include "rpc.hpp" //
#include "rpc.hpp" // for update_liquid_collision_at
#include "search.hpp" // for get_address
#include "state.hpp" // for State, StateMemory

Expand All @@ -31,8 +31,7 @@ Entity* Layer::spawn_entity(ENT_TYPE id, float x, float y, bool screen, float vx
}
else if (screen)
{
auto& state = State::get();
std::tie(x, y) = state.click_position(x, y);
std::tie(x, y) = API::click_position(x, y);
min_speed_check = 0.04f;
if (snap && abs(vx) + abs(vy) <= min_speed_check)
{
Expand Down
20 changes: 4 additions & 16 deletions src/game_api/rpc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -287,15 +287,15 @@ std::vector<Player*> get_players(StateMemory* state)

std::tuple<float, float, float, float> screen_aabb(float left, float top, float right, float bottom)
{
auto [sx1, sy1] = State::screen_position(left, top);
auto [sx2, sy2] = State::screen_position(right, bottom);
auto [sx1, sy1] = API::screen_position(left, top);
auto [sx2, sy2] = API::screen_position(right, bottom);
return std::tuple{sx1, sy1, sx2, sy2};
}

float screen_distance(float x)
{
auto a = State::screen_position(0, 0);
auto b = State::screen_position(x, 0);
auto a = API::screen_position(0, 0);
auto b = API::screen_position(x, 0);
return b.x - a.x;
}

Expand Down Expand Up @@ -451,18 +451,6 @@ void flip_entity(uint32_t uid)
}
}

void warp(uint8_t world, uint8_t level, uint8_t theme)
{
auto& state = State::get();
state.warp(world, level, theme);
}

void set_seed(uint32_t seed)
{
auto& state = State::get();
state.set_seed(seed);
}

void set_arrowtrap_projectile(ENT_TYPE regular_entity_type, ENT_TYPE poison_entity_type)
{
static const auto arrowtrap = get_address("arrowtrap_projectile");
Expand Down
2 changes: 0 additions & 2 deletions src/game_api/rpc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@ void kill_entity(uint32_t uid, std::optional<bool> destroy_corpse = std::nullopt
void destroy_entity(uint32_t uid);
void apply_entity_db(uint32_t uid);
void flip_entity(uint32_t uid);
void warp(uint8_t w, uint8_t l, uint8_t t);
void set_seed(uint32_t seed);
void set_arrowtrap_projectile(ENT_TYPE regular_entity_type, ENT_TYPE poison_entity_type);
void modify_sparktraps(float angle_increment = 0.015, float distance = 3.0);
float* get_sparktraps_parameters_ptr(); // for UI
Expand Down
22 changes: 12 additions & 10 deletions src/game_api/script/lua_vm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ end
return nullptr;
};
/// Provides access to the save data, updated as soon as something changes (i.e. before it's written to savegame.sav.) Use [save_progress](#save_progress) to save to savegame.sav.
lua["savegame"] = State::get().savedata();
lua["savegame"] = get_game_manager()->save_related->savedata.decode();

/// Standard lua print function, prints directly to the terminal but not to the game
lua["lua_print"] = lua["print"];
Expand Down Expand Up @@ -964,25 +964,27 @@ end
};

/// Warp to a level immediately.
lua["warp"] = warp;
lua["warp"] = [](uint8_t world, uint8_t level, uint8_t theme)
{ HeapBase::get().state()->warp(world, level, theme); };
/// Set seed and reset run.
lua["set_seed"] = set_seed;
lua["set_seed"] = [](uint32_t seed)
{ HeapBase::get().state()->set_seed(seed); };
/// Enable/disable godmode for players.
lua["god"] = [](bool g)
{ State::get().godmode(g); };
{ API::godmode(g); };
/// Enable/disable godmode for companions.
lua["god_companions"] = [](bool g)
{ State::get().godmode_companions(g); };
{ API::godmode_companions(g); };
/// Deprecated
/// Set level flag 18 on post room generation instead, to properly force every level to dark
lua["force_dark_level"] = [](bool g)
{ State::get().darkmode(g); };
{ API::darkmode(g); };
/// Set the zoom level used in levels and shops. 13.5 is the default, or 12.5 for shops. See zoom_reset.
lua["zoom"] = [](float level)
{ State::get().zoom(level); };
{ API::zoom(level); };
/// Reset the default zoom levels for all areas and sets current zoom level to 13.5.
lua["zoom_reset"] = []()
{ State::get().zoom_reset(); };
{ API::zoom_reset(); };
auto move_entity_abs = sol::overload(
static_cast<void (*)(uint32_t, float, float, float, float)>(::move_entity_abs),
static_cast<void (*)(uint32_t, float, float, float, float, LAYER)>(::move_entity_abs));
Expand Down Expand Up @@ -1132,10 +1134,10 @@ end
};
/// Get the game coordinates at the screen position (`x`, `y`)
lua["game_position"] = [](float x, float y) -> std::pair<float, float>
{ return State::click_position(x, y); };
{ return API::click_position(x, y); };
/// Translate an entity position to screen position to be used in drawing functions
lua["screen_position"] = [](float x, float y) -> std::pair<float, float>
{ return State::screen_position(x, y); };
{ return API::screen_position(x, y); };
/// Translate a distance of `x` tiles to screen distance to be be used in drawing functions
lua["screen_distance"] = screen_distance;
/// Get position `x, y, layer` of entity by uid. Use this, don't use `Entity.x/y` because those are sometimes just the offset to the entity
Expand Down
117 changes: 56 additions & 61 deletions src/game_api/state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,17 @@ void API::set_write_load_opt(bool write_load_opt)
static bool g_godmode_player_active = false;
static bool g_godmode_companions_active = false;

bool is_active_player(Entity* e)
void API::godmode(bool g)
{
g_godmode_player_active = g;
}

void API::godmode_companions(bool g)
{
g_godmode_companions_active = g;
}

static bool is_active_player(Entity* e)
{
auto state = State::get().ptr();
for (uint8_t i = 0; i < MAX_PLAYERS; i++)
Expand Down Expand Up @@ -222,16 +232,6 @@ void hook_godmode_functions()
}
}

void State::godmode(bool g)
{
g_godmode_player_active = g;
}

void State::godmode_companions(bool g)
{
g_godmode_companions_active = g;
}

struct ThemeHookImpl
{
template <class FunT, class HookFunT>
Expand Down Expand Up @@ -290,13 +290,13 @@ StateMemory* State::ptr_local() const
return p.decode_local();
}

float get_zoom_level()
static float get_zoom_level()
{
auto game_api = GameAPI::get();
return game_api->get_current_zoom();
}

Vec2 State::click_position(float x, float y)
Vec2 API::click_position(float x, float y)
{
float cz = get_zoom_level();
auto [cx, cy] = Camera::get_position();
Expand All @@ -305,7 +305,7 @@ Vec2 State::click_position(float x, float y)
return {rx, ry};
}

Vec2 State::screen_position(float x, float y)
Vec2 API::screen_position(float x, float y)
{
float cz = get_zoom_level();
auto [cx, cy] = Camera::get_position();
Expand All @@ -314,9 +314,9 @@ Vec2 State::screen_position(float x, float y)
return {rx, ry};
}

void State::zoom(float level) const
void API::zoom(float level)
{
auto roomx = ptr()->w;
auto roomx = HeapBase::get().state()->w;
if (level == 0.0)
{
switch (roomx)
Expand Down Expand Up @@ -366,7 +366,7 @@ void State::zoom(float level) const
game_api->set_zoom(std::nullopt, level);
}

void State::zoom_reset()
void API::zoom_reset()
{
recover_mem("zoom");
auto game_api = GameAPI::get();
Expand All @@ -384,7 +384,7 @@ void StateMemory::force_current_theme(THEME t)
}
}

void State::darkmode(bool g)
void API::darkmode(bool g)
{
static const size_t addr_dark = get_address("force_dark_level");

Expand Down Expand Up @@ -430,69 +430,64 @@ void Camera::update_position()
calculated_focus_y = adjusted_focus_y;
}

void State::warp(uint8_t w, uint8_t l, uint8_t t)
void StateMemory::warp(uint8_t set_world, uint8_t set_level, uint8_t set_theme)
{
// if (ptr()->screen < 11 || ptr()->screen > 20)
// if (screen < 11 || screen > 20)
// return;
if (ptr()->items->player_count < 1)
auto gm = get_game_manager();
if (items->player_count < 1)
{
ptr()->items->player_select_slots[0].activated = true;
ptr()->items->player_select_slots[0].character = savedata()->players[0] + to_id("ENT_TYPE_CHAR_ANA_SPELUNKY");
ptr()->items->player_select_slots[0].texture_id = savedata()->players[0] + 285; // TODO: magic numbers
ptr()->items->player_count = 1;
auto savedata = gm->save_related->savedata.decode_local();
items->player_select_slots[0].activated = true;
items->player_select_slots[0].character = savedata->players[0] + to_id("ENT_TYPE_CHAR_ANA_SPELUNKY");
items->player_select_slots[0].texture_id = savedata->players[0] + 285; // TODO: magic numbers
items->player_count = 1;
}
ptr()->world_next = w;
ptr()->level_next = l;
ptr()->theme_next = t;
if (ptr()->world_start < 1 || ptr()->level_start < 1 || ptr()->theme_start < 1 || ptr()->theme == 17)
world_next = set_world;
level_next = set_level;
theme_next = set_theme;
if (world_start < 1 || level_start < 1 || theme_start < 1 || theme == 17)
{
ptr()->world_start = w;
ptr()->level_start = l;
ptr()->theme_start = t;
ptr()->quest_flags = 1;
world_start = set_world;
level_start = set_level;
theme_start = set_theme;
quest_flags = 1;
}
if (t != 17)
if (set_theme != 17)
{
ptr()->screen_next = 12;
screen_next = 12;
}
else
{
ptr()->screen_next = 11;
screen_next = 11;
}
ptr()->win_state = 0;
ptr()->loading = 1;
win_state = 0;
loading = 1;

static auto gm = get_game_manager();
if (gm->main_menu_music)
{
gm->main_menu_music->kill(false);
gm->main_menu_music = nullptr;
}
}

void State::set_seed(uint32_t seed)
void StateMemory::set_seed(uint32_t set_seed)
{
if (ptr()->screen < 11 || ptr()->screen > 20)
if (screen < 11 || screen > 20)
return;
ptr()->seed = seed;
ptr()->world_start = 1;
ptr()->level_start = 1;
ptr()->theme_start = 1;
ptr()->world_next = 1;
ptr()->level_next = 1;
ptr()->theme_next = 1;
ptr()->quest_flags = 0x1e | 0x41;
ptr()->screen_next = 12;
ptr()->loading = 1;
}

SaveData* State::savedata()
{
auto gm = get_game_manager();
return gm->save_related->savedata.decode(); // wondering if it matters if it's local or not?
seed = set_seed;
world_start = 1;
level_start = 1;
theme_start = 1;
world_next = 1;
level_next = 1;
theme_next = 1;
quest_flags = 0x1e | 0x41;
screen_next = 12;
loading = 1;
}

Entity* State::find(StateMemory* state, uint32_t uid)
Entity* StateMemory::get_entity(uint32_t uid) const
{
// Ported from MauveAlert's python code in the CAT tracker

Expand All @@ -502,12 +497,12 @@ Entity* State::find(StateMemory* state, uint32_t uid)
return nullptr;
}

const uint32_t mask = state->uid_to_entity_mask;
const uint32_t mask = uid_to_entity_mask;
const uint32_t target_uid_plus_one = lowbias32(uid + 1);
uint32_t cur_index = target_uid_plus_one & mask;
while (true)
{
auto entry = state->uid_to_entity_data[cur_index];
auto entry = uid_to_entity_data[cur_index];
if (entry.uid_plus_one == target_uid_plus_one)
{
return entry.entity;
Expand All @@ -527,7 +522,7 @@ Entity* State::find(StateMemory* state, uint32_t uid)
}
}

LiquidPhysicsEngine* LiquidPhysics::get_correct_liquid_engine(ENT_TYPE liquid_type)
LiquidPhysicsEngine* LiquidPhysics::get_correct_liquid_engine(ENT_TYPE liquid_type) const
{
static const ENT_TYPE LIQUID_WATER = to_id("ENT_TYPE_LIQUID_WATER"sv);
static const ENT_TYPE LIQUID_COARSE_WATER = to_id("ENT_TYPE_LIQUID_COARSE_WATER"sv);
Expand Down
Loading

0 comments on commit 36a9dab

Please sign in to comment.