Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

StateMemory and State structs changes #379

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/parse_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
"../src/game_api/movable_behavior.hpp",
"../src/game_api/game_manager.hpp",
"../src/game_api/state.hpp",
"../src/game_api/mod_api.hpp",
"../src/game_api/state_structs.hpp",
"../src/game_api/illumination.hpp",
"../src/game_api/prng.hpp",
Expand Down
15 changes: 8 additions & 7 deletions src/game_api/bucket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "game_manager.hpp"
#include "items.hpp"
#include "memory.hpp"
#include "mod_api.hpp"
#include "screen.hpp"
#include "state.hpp"

Expand All @@ -23,13 +24,13 @@ Bucket* Bucket::get()

PAUSE_TYPE PauseAPI::get_pause()
{
pause = (PAUSE_TYPE)(State::get().ptr()->pause | ((uint32_t)pause & ~0x3f));
pause = (PAUSE_TYPE)(State::ptr()->pause | ((uint32_t)pause & ~0x3f));
return pause;
}

void PauseAPI::set_pause(PAUSE_TYPE flags)
{
auto state = State::get().ptr();
auto state = State::ptr();
pause = flags;
state->pause = (uint8_t)(((uint32_t)flags) & 0x3f);
}
Expand Down Expand Up @@ -70,7 +71,7 @@ void PauseAPI::apply()
bool PauseAPI::check_trigger(PAUSE_TRIGGER& trigger, PAUSE_SCREEN& screen)
{
bool match = false;
auto state = State::get().ptr();
auto state = State::ptr();

if (state->loading == 2 && (trigger & PAUSE_TRIGGER::SCREEN) != PAUSE_TRIGGER::NONE && (screen == PAUSE_SCREEN::NONE || (screen & (PAUSE_SCREEN)(1 << state->screen_next)) != PAUSE_SCREEN::NONE))
match = true;
Expand All @@ -95,7 +96,7 @@ bool PauseAPI::check_trigger(PAUSE_TRIGGER& trigger, PAUSE_SCREEN& screen)

bool PauseAPI::loading()
{
auto state = State::get().ptr();
auto state = State::ptr();
auto gm = get_game_manager();
bool loading = state->loading > 0 || state->fade_timer > 0 || (state->screen == 4 && gm->screen_menu->menu_text_opacity < 1) || (state->screen == 9 && (state->screen_character_select->topleft_woodpanel_esc_slidein == 0 || state->screen_character_select->start_pressed)) || state->logic->ouroboros;
if ((state->loading == 3 && (state->fade_timer <= 1 || state->fade_length == 0)) || (state->loading == 1 && state->fade_timer == state->fade_length))
Expand All @@ -107,7 +108,7 @@ bool PauseAPI::event(PAUSE_TYPE pause_event)
{
bool block = false;
std::optional<bool> force;
auto state = State::get().ptr();
auto state = State::ptr();

if (skip_fade)
{
Expand Down Expand Up @@ -180,7 +181,7 @@ void PauseAPI::pre_loop()

void PauseAPI::post_loop()
{
auto state = State::get().ptr();
auto state = State::ptr();
if (skip)
state->pause |= (uint8_t)pause_type & 0x3f;
skip = false;
Expand All @@ -194,7 +195,7 @@ bool PauseAPI::pre_input()
return false;

auto gm = get_game_manager();
auto state = State::get().ptr();
auto state = State::ptr();

if (bucket->pause_api->modifiers_block & bucket->pause_api->modifiers_down)
{
Expand Down
14 changes: 7 additions & 7 deletions src/game_api/entities_floors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ void Floor::fix_decorations(bool fix_also_neighbors, bool fix_styled_floor)
Floor* neighbours[4]{};
bool neighbours_same[4]{};

auto& state = State::get();
auto layer_ptr = state.layer(layer);
StateMemory* state = State::ptr();
auto layer_ptr = state->layers[layer];

for (size_t i = 0; i < 4; i++)
{
Expand Down Expand Up @@ -188,8 +188,8 @@ void Floor::add_decoration(FLOOR_SIDE side)
return;
}

auto& state = State::get();
auto layer_ptr = state.layer(layer);
StateMemory* state = State::ptr();
auto layer_ptr = state->layers[layer];
add_decoration_opt(side, decoration_entity_type, layer_ptr);
}
void Floor::remove_decoration(FLOOR_SIDE side)
Expand Down Expand Up @@ -741,7 +741,7 @@ void Door::unlock(bool unlock)
static const ENT_TYPE eggchild_room_door = to_id("ENT_TYPE_FLOOR_DOOR_MOAI_STATUE");
static const ENT_TYPE EW_door = to_id("ENT_TYPE_FLOOR_DOOR_EGGPLANT_WORLD");
const auto ent_type = this->type->id;
auto& state = State::get();
StateMemory* state = State::ptr();

if (ent_type == locked_door || ent_type == locked_door + 1) // plus one for DOOR_LOCKED_PEN
{
Expand Down Expand Up @@ -772,7 +772,7 @@ void Door::unlock(bool unlock)
if (ent_type == entrence_door || ent_type == entrence_door + 1 || ent_type == entrence_door + 3)
{
static const ENT_TYPE door_bg = to_id("ENT_TYPE_BG_DOOR");
const auto state_layer = state.layer(this->layer);
const auto state_layer = state->layers[this->layer];
for (const auto& item : state_layer->entities_overlaping_grid[static_cast<int>(y)][static_cast<int>(x)].entities())
{
if (item->type->id == door_bg)
Expand Down Expand Up @@ -807,7 +807,7 @@ void Door::unlock(bool unlock)
{
if (!main_door->door_blocker)
{
main_door->door_blocker = state.layer(layer)->spawn_entity_over(door_bg_large, this, 0, 2.0);
main_door->door_blocker = state->layers[layer]->spawn_entity_over(door_bg_large, this, 0, 2.0);
main_door->door_blocker->animation_frame = 1;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/game_api/entities_liquids.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@

uint32_t Liquid::get_liquid_flags()
{
auto liquid_engine = State::get().get_correct_liquid_engine(type->id);
auto liquid_engine = State::ptr()->get_correct_liquid_engine(type->id);
return liquid_engine->liquid_flags[*liquid_id];
}

void Liquid::set_liquid_flags(uint32_t liquid_flags)
{
auto liquid_engine = State::get().get_correct_liquid_engine(type->id);
auto liquid_engine = State::ptr()->get_correct_liquid_engine(type->id);
liquid_engine->liquid_flags[*liquid_id] = liquid_flags;
}
23 changes: 11 additions & 12 deletions src/game_api/entity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,7 @@ void Entity::teleport(float dx, float dy, bool s, float vx, float vy, bool snap)
{
// screen coordinates -1..1
// log::debug!("Teleporting to screen {}, {}", x, y);
auto& state = State::get();
auto [x_pos, y_pos] = state.click_position(dx, dy);
auto [x_pos, y_pos] = StateMemory::click_position(dx, dy);
if (snap && abs(vx) + abs(vy) <= 0.04f)
{
x_pos = round(x_pos);
Expand Down Expand Up @@ -96,20 +95,20 @@ void Entity::set_layer(LAYER layer_to)
if (layer == dest_layer)
return;

auto& state = State::get();
StateMemory* state = State::ptr();
if (this != this->topmost_mount())
this->topmost_mount()->set_layer(layer_to);

if (layer == 0 || layer == 1)
{
auto ptr_from = state.ptr()->layers[layer];
auto ptr_from = state->layers[layer];

using RemoveFromLayer = void(Layer*, Entity*);
static RemoveFromLayer* remove_from_layer = (RemoveFromLayer*)get_address("remove_from_layer");
remove_from_layer(ptr_from, this);
}

auto ptr_to = state.ptr()->layers[dest_layer];
auto ptr_to = state->layers[dest_layer];

using AddToLayer = void(Layer*, Entity*);
static AddToLayer* add_to_layer = (AddToLayer*)get_address("add_to_layer");
Expand All @@ -123,7 +122,7 @@ void Entity::set_layer(LAYER layer_to)

void Entity::apply_layer()
{
auto ptr_to = State::get().ptr()->layers[layer];
auto ptr_to = State::ptr()->layers[layer];

using AddToLayer = void(Layer*, Entity*);
static AddToLayer* add_to_layer = (AddToLayer*)get_address("add_to_layer");
Expand All @@ -139,8 +138,7 @@ void Entity::remove()
{
if (layer != 2)
{
auto& state = State::get();
auto ptr_from = state.ptr()->layers[layer];
auto ptr_from = State::ptr()->layers[layer];
if ((this->type->search_flags & 1) == 0 || ((Player*)this)->ai != 0)
{
using RemoveFromLayer = void(Layer*, Entity*);
Expand Down Expand Up @@ -289,7 +287,7 @@ std::tuple<float, float> get_velocity(uint32_t uid)
}
else if (ent->is_liquid())
{
auto liquid_engine = State::get().get_correct_liquid_engine(ent->type->id);
auto liquid_engine = State::ptr()->get_correct_liquid_engine(ent->type->id);
vx = liquid_engine->entity_velocities->first;
vy = liquid_engine->entity_velocities->second;
}
Expand Down Expand Up @@ -387,7 +385,7 @@ std::span<uint32_t> Entity::get_items()

Entity* get_entity_ptr(uint32_t uid)
{
auto p = State::find(State::get().ptr(), uid);
auto p = State::ptr()->find(uid);
// if (IsBadWritePtr(p, 0x178))
// return nullptr;
return p;
Expand Down Expand Up @@ -472,8 +470,9 @@ void Movable::set_position(float to_x, float to_y)
rendering_info->x_dupe4 += dx;
rendering_info->y_dupe4 += dy;
}
if (State::get().ptr()->camera->focused_entity_uid == uid)
State::get().set_camera_position(dx, dy);
StateMemory* state_mem = State::ptr();
if (state_mem->camera->focused_entity_uid == uid)
state_mem->set_camera_position(dx, dy);
}

template <typename F>
Expand Down
24 changes: 12 additions & 12 deletions src/game_api/entity_lookup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ std::vector<ENT_TYPE> get_proper_types(std::vector<ENT_TYPE> ent_types)

int32_t get_grid_entity_at(float x, float y, LAYER layer)
{
auto& state = State::get();
StateMemory* state = State::ptr();
uint8_t actual_layer = enum_to_layer(layer);

if (Entity* ent = state.layer(actual_layer)->get_grid_entity_at(x, y))
if (Entity* ent = state->layers[actual_layer]->get_grid_entity_at(x, y))
return ent->uid;

return -1;
Expand Down Expand Up @@ -102,7 +102,7 @@ void foreach_mask(uint32_t mask, Layer* l, FunT&& fun)

std::vector<uint32_t> get_entities_by(std::vector<ENT_TYPE> entity_types, uint32_t mask, LAYER layer)
{
auto state = State::get().ptr();
auto state = State::ptr();
std::vector<uint32_t> found;
const std::vector<ENT_TYPE> proper_types = get_proper_types(std::move(entity_types));

Expand Down Expand Up @@ -170,7 +170,7 @@ std::vector<uint32_t> get_entities_by(ENT_TYPE entity_type, uint32_t mask, LAYER
std::vector<uint32_t> get_entities_at(std::vector<ENT_TYPE> entity_types, uint32_t mask, float x, float y, LAYER layer, float radius)
{
// TODO: use entity regions?
auto& state = State::get();
StateMemory* state = State::ptr();
std::vector<uint32_t> found;
const std::vector<ENT_TYPE> proper_types = get_proper_types(std::move(entity_types));
auto push_entities_at = [&x, &y, &radius, &proper_types, &found](const EntityList& entities)
Expand All @@ -187,12 +187,12 @@ std::vector<uint32_t> get_entities_at(std::vector<ENT_TYPE> entity_types, uint32
};
if (layer == LAYER::BOTH)
{
foreach_mask(mask, state.layer(0), push_entities_at);
foreach_mask(mask, state.layer(1), push_entities_at);
foreach_mask(mask, state->layers[0], push_entities_at);
foreach_mask(mask, state->layers[1], push_entities_at);
}
else
{
foreach_mask(mask, state.layer(enum_to_layer(layer)), push_entities_at);
foreach_mask(mask, state->layers[enum_to_layer(layer)], push_entities_at);
}
return found;
}
Expand All @@ -205,20 +205,20 @@ std::vector<uint32_t> get_entities_at(ENT_TYPE entity_type, uint32_t mask, float
std::vector<uint32_t> get_entities_overlapping_hitbox(std::vector<ENT_TYPE> entity_types, uint32_t mask, AABB hitbox, LAYER layer)
{
// TODO: use entity regions?
auto& state = State::get();
StateMemory* state = State::ptr();
std::vector<uint32_t> result;
const std::vector<ENT_TYPE> proper_types = get_proper_types(std::move(entity_types));
if (layer == LAYER::BOTH)
{
std::vector<uint32_t> result2;
result = get_entities_overlapping_by_pointer(proper_types, mask, hitbox.left, hitbox.bottom, hitbox.right, hitbox.top, state.layer(0));
result2 = get_entities_overlapping_by_pointer(proper_types, mask, hitbox.left, hitbox.bottom, hitbox.right, hitbox.top, state.layer(1));
result = get_entities_overlapping_by_pointer(proper_types, mask, hitbox.left, hitbox.bottom, hitbox.right, hitbox.top, state->layers[0]);
result2 = get_entities_overlapping_by_pointer(proper_types, mask, hitbox.left, hitbox.bottom, hitbox.right, hitbox.top, state->layers[1]);
result.insert(result.end(), result2.begin(), result2.end());
}
else
{
uint8_t actual_layer = enum_to_layer(layer);
result = get_entities_overlapping_by_pointer(proper_types, mask, hitbox.left, hitbox.bottom, hitbox.right, hitbox.top, state.layer(actual_layer));
result = get_entities_overlapping_by_pointer(proper_types, mask, hitbox.left, hitbox.bottom, hitbox.right, hitbox.top, state->layers[actual_layer]);
}
return result;
}
Expand Down Expand Up @@ -325,7 +325,7 @@ std::vector<uint32_t> get_entities_by_draw_depth(uint8_t draw_depth, LAYER l)

std::vector<uint32_t> get_entities_by_draw_depth(std::vector<uint8_t> draw_depths, LAYER l)
{
auto state = State::get().ptr_local();
auto state = State::ptr_local();
std::vector<uint32_t> found;
auto actual_layer = enum_to_layer(l);
for (auto draw_depth : draw_depths)
Expand Down
2 changes: 1 addition & 1 deletion src/game_api/game_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ GameAPI* GameAPI::get()

float GameAPI::get_current_zoom()
{
auto state = State::get().ptr();
auto state = State::ptr();
return renderer->current_zoom + get_layer_transition_zoom_offset(state->camera_layer);
}

Expand Down
4 changes: 2 additions & 2 deletions src/game_api/game_patches.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ void patch_orbs_limit()

bool check_if_ent_type_exists(ENT_TYPE type, int mask)
{
StateMemory* state = State::get().ptr_local();
StateMemory* state = State::ptr_local();
if (state == nullptr)
state = State::get().ptr_main();
state = State::ptr_main();

const auto entities_map = &state->layers[0]->entities_by_mask; // game code only cares about the front layer, so we do the same
auto it = entities_map->find(mask);
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 @@ -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) = StateMemory::click_position(x, y);
min_speed_check = 0.04f;
if (snap && abs(vx) + abs(vy) <= min_speed_check)
{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we remove the screen param?
if i recall, it's only used by the UI, so we could move this feature to the ui_util

Expand Down Expand Up @@ -120,7 +119,7 @@ Entity* Layer::get_entity_at(float x, float y, uint32_t search_flags, uint32_t i

Entity* Layer::spawn_door(float x, float y, uint8_t w, uint8_t l, uint8_t t)
{
auto screen = State::get().ptr()->screen_next;
auto screen = State::ptr()->screen_next;
Entity* door;
switch (screen)
{
Expand Down
Loading
Loading