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

State refactor #407

Open
wants to merge 25 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
77febfb
add `is_active` method for `Online`
Mr-Auto Dec 29, 2024
8e62788
Implement `HeapBase` struct, change `savestate` using `HeapBase`, fix…
Mr-Auto Dec 30, 2024
fb730a3
fix overlunky initializing UI to fast @estebanfer
Mr-Auto Dec 30, 2024
0862cab
HeapBase get rid of `get_local`, rename `get_local_safe` to `get`, re…
Mr-Auto Dec 31, 2024
6d7c873
move API init out of `State::get`, remove leftover `Spelunky_UpdateLi…
Mr-Auto Dec 31, 2024
bf47001
move `get_correct_liquid_engine` to `LiquidPhysics`
Mr-Auto Dec 31, 2024
b50e2fa
simplify `refresh_illumination`, update doc
Mr-Auto Jan 1, 2025
62e872e
move `layer` from `State` to `StateMemory`, remove `flags`, `set_flag…
Mr-Auto Jan 4, 2025
a24ede2
fix `get_frame` comment
Mr-Auto Jan 4, 2025
7dd2036
move copying HeapBase and heap clone hook into `thread_utils`
Mr-Auto Jan 4, 2025
e993b59
some save state changes, exposing prng and frame of the saved state
Mr-Auto Jan 4, 2025
e1862a4
get rid of prng getter, also change `seed_prng` to use local
Mr-Auto Jan 4, 2025
c4b5e9c
Some extra renderer stuff
Mr-Auto Jan 5, 2025
320b2f9
move camera function to `Camera`, also fold some one line functions i…
Mr-Auto Jan 5, 2025
b711e82
move `lowbias32` to rpc, move and rename `fix_liquid_out_of_bounds` t…
Mr-Auto Jan 5, 2025
d4b6044
move `enum_to_layer` declaration to aliases, move last stand alone fu…
Mr-Auto Jan 5, 2025
36a9dab
move `warp`, `set_seed` to `StateMemory`. Move and rename `State::fin…
Mr-Auto Jan 6, 2025
d907986
remove `State::get_frame_count`, make event deal with HeapBase instea…
Mr-Auto Jan 6, 2025
90f4f4f
get rid of `State`
Mr-Auto Jan 7, 2025
6a6eda8
rename `thread_utils` to `heap_base` and mess around with some `#incl…
Mr-Auto Jan 7, 2025
bf6c489
fix `get_global_frame` return in doc
Mr-Auto Jan 8, 2025
b21287c
use `get_state_ptr` in `render_api` since we only use state and only …
Mr-Auto Jan 10, 2025
6104fea
fix `info_dump`
Mr-Auto Jan 12, 2025
f84aed2
switch `OnHeapPointer` method `decode` with `decode_local` and name i…
Mr-Auto Jan 16, 2025
d47fe85
f you visual studio
Mr-Auto Jan 17, 2025
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
23 changes: 15 additions & 8 deletions docs/game_data/spel2.lua

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions docs/src/includes/_globals.md
Original file line number Diff line number Diff line change
Expand Up @@ -1473,7 +1473,7 @@ short for state->money_shop_total + loop[inventory.money + inventory.collected_m

#### int get_frame()

Get the current frame count since the game was started. You can use this to make some timers yourself, the engine runs at 60fps. This counter is paused if you block PRE_UPDATE from running, and also doesn't increment during some loading screens, even though state update still runs.
Get the current frame count since the game was started*. You can use this to make some timers yourself, the engine runs at 60fps. This counter is paused if the pause is set with flags [PAUSE](#PAUSE).[FADE](#FADE) or [PAUSE](#PAUSE).ANKH.

### get_frametime

Expand Down Expand Up @@ -2215,7 +2215,7 @@ Run state update manually, i.e. simulate one logic frame. Use in e.g. POST_UPDAT

> Search script examples for [warp](https://github.com/spelunky-fyi/overlunky/search?l=Lua&q=warp)

#### nil warp(int w, int l, int t)
#### nil warp(int world, int level, int theme)

Warp to a level immediately.

Expand Down Expand Up @@ -2281,7 +2281,7 @@ Creates a new [Illumination](#Illumination). Don't forget to continuously call [

#### nil refresh_illumination([Illumination](#Illumination) illumination)

Refreshes an [Illumination](#Illumination), keeps it from fading out (updates the timer, keeping it in sync with the game render)
Refreshes an [Illumination](#Illumination), keeps it from fading out, short for `illumination.timer = get_frame()`

## Message functions

Expand Down
4 changes: 4 additions & 0 deletions docs/src/includes/_types.md
Original file line number Diff line number Diff line change
Expand Up @@ -1017,6 +1017,9 @@ nil | [load()](https://github.com/spelunky-fyi/overlunky/search?l=Lua&q=load) |
nil | [save()](https://github.com/spelunky-fyi/overlunky/search?l=Lua&q=save) | Save over a previously allocated [SaveState](#SaveState)
nil | [clear()](https://github.com/spelunky-fyi/overlunky/search?l=Lua&q=clear) | Delete the [SaveState](#SaveState) and free the memory. The [SaveState](#SaveState) can't be used after this.
[StateMemory](#StateMemory) | [get_state()](https://github.com/spelunky-fyi/overlunky/search?l=Lua&q=get_state) | Access the [StateMemory](#StateMemory) inside a [SaveState](#SaveState)
int | [get_frame()](https://github.com/spelunky-fyi/overlunky/search?l=Lua&q=get_frame) | Get the current frame from the [SaveState](#SaveState), equivelent to the [get_frame](#Get_frame) global function that returns the frame from the "loaded in state"
[PRNG](#PRNG) | [get_prng()](https://github.com/spelunky-fyi/overlunky/search?l=Lua&q=get_prng) | Access the [PRNG](#PRNG) inside a [SaveState](#SaveState)
[SaveState](#SaveState) | [get(int save_slot)](https://github.com/spelunky-fyi/overlunky/search?l=Lua&q=get) | Get the pre-allocated by the game save slot 1-4

### SharedIO

Expand Down Expand Up @@ -1830,6 +1833,7 @@ Type | Name | Description
array<[OnlinePlayer](#OnlinePlayer), 4> | [online_players](https://github.com/spelunky-fyi/overlunky/search?l=Lua&q=online_players) |
[OnlinePlayer](#OnlinePlayer) | [local_player](https://github.com/spelunky-fyi/overlunky/search?l=Lua&q=local_player) |
[OnlineLobby](#OnlineLobby) | [lobby](https://github.com/spelunky-fyi/overlunky/search?l=Lua&q=lobby) |
bool | [is_active()](https://github.com/spelunky-fyi/overlunky/search?l=Lua&q=is_active) |

### OnlineLobby

Expand Down
5 changes: 5 additions & 0 deletions src/game_api/aliases.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -221,3 +221,8 @@ bool test_mask(T flags, T mask)
{
return static_cast<std::underlying_type_t<T>>(flags & mask) != 0;
}

// safe function, returns only 0 or 1. returns 0 for LAYER::BOTH
uint8_t enum_to_layer(const LAYER layer, struct Vec2& player_position);
// safe function, returns only 0 or 1. returns 0 for LAYER::BOTH
uint8_t enum_to_layer(const LAYER layer);
35 changes: 17 additions & 18 deletions src/game_api/bucket.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
#include "bucket.hpp"

#include "containers/game_allocator.hpp"
#include "entities_chars.hpp"
#include "game_manager.hpp"
#include "items.hpp"
#include "memory.hpp"
#include "screen.hpp"
#include "state.hpp"
#include "entities_chars.hpp" // for Player
#include "game_manager.hpp" // for GameManager, get_game_manager
#include "items.hpp" // for Items
#include "screen.hpp" // for ScreenCharacterSelect
#include "search.hpp" // for get_address
#include "state.hpp" // for StateMemory, get_state_ptr

Bucket* Bucket::get()
{
Expand All @@ -23,21 +22,21 @@ Bucket* Bucket::get()

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

void PauseAPI::set_pause(PAUSE_TYPE flags)
{
auto state = State::get().ptr();
auto state = get_state_ptr();
pause = flags;
state->pause = (uint8_t)(((uint32_t)flags) & 0x3f);
}

bool PauseAPI::check_trigger(PAUSE_TRIGGER& trigger, PAUSE_SCREEN& screen) const
{
bool match = false;
auto state = State::get().ptr();
auto state = get_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 @@ -54,15 +53,15 @@ bool PauseAPI::check_trigger(PAUSE_TRIGGER& trigger, PAUSE_SCREEN& screen) const
if (match && (trigger & PAUSE_TRIGGER::ONCE) != PAUSE_TRIGGER::NONE)
trigger = PAUSE_TRIGGER::NONE;

if (match && last_trigger_frame == get_global_update_count())
if (match && (uint64_t)last_trigger_frame == API::get_global_update_count())
match = false;

return match;
}

bool PauseAPI::loading()
{
auto state = State::get().ptr();
auto state = get_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 @@ -74,7 +73,7 @@ bool PauseAPI::event(PAUSE_TYPE pause_event)
{
bool block = false;
std::optional<bool> force;
auto state = State::get().ptr();
auto state = get_state_ptr();

if (skip_fade)
{
Expand All @@ -95,13 +94,13 @@ bool PauseAPI::event(PAUSE_TYPE pause_event)
{
set_paused(true);
force = true;
last_trigger_frame = get_global_update_count();
last_trigger_frame = API::get_global_update_count();
}
if (check_trigger(unpause_trigger, unpause_screen))
{
set_paused(false);
force = false;
last_trigger_frame = get_global_update_count();
last_trigger_frame = API::get_global_update_count();
}
last_fade_timer = state->fade_timer;
last_level_flags = state->level_flags;
Expand Down Expand Up @@ -132,7 +131,7 @@ bool PauseAPI::event(PAUSE_TYPE pause_event)
set_paused(true);

if (update_camera && ((block && (pause_event == PAUSE_TYPE::PRE_UPDATE || pause_event == PAUSE_TYPE::PRE_GAME_LOOP)) || ((pause_event == PAUSE_TYPE::PRE_UPDATE && (uint8_t)pause_type & 0x3f) && state->pause > 0)) && ((state->pause & 1) == 0 || (uint8_t)pause_type & 1))
update_camera_position();
state->camera->update_position();

if ((pause_type & pause_event) != PAUSE_TYPE::NONE)
blocked = block;
Expand All @@ -142,7 +141,7 @@ bool PauseAPI::event(PAUSE_TYPE pause_event)

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

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

if (bucket->pause_api->modifiers_block & bucket->pause_api->modifiers_down)
{
Expand Down
2 changes: 1 addition & 1 deletion src/game_api/color.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ uColor get_color(const std::string& color_name, std::optional<uint8_t> alpha)
{
// #RRGGBBAA
// 0xAABBGGRR
// treating not even number of hex characters as user error = behavior is undefined
// treating non even number of hex characters as user error = behavior is undefined

if (color_name.size() == 0)
return 0;
Expand Down
10 changes: 5 additions & 5 deletions src/game_api/containers/custom_allocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

#include <string> // for operator""sv

#include "memory.hpp" // for memory_read, Memory
#include "search.hpp" // for get_address
#include "thread_utils.hpp" // for OnHeapPointer
#include "heap_base.hpp" // for OnHeapPointer
#include "memory.hpp" // for memory_read, Memory
#include "search.hpp" // for get_address

using CustomMallocFun = void*(void*, std::size_t);
using CustomFreeFun = void*(void*, void*);
Expand All @@ -13,13 +13,13 @@ void* custom_malloc(std::size_t size)
{
static CustomMallocFun* _malloc = (CustomMallocFun*)get_address("custom_malloc"sv);
static size_t _heap_ptr_malloc_base = *reinterpret_cast<size_t*>(get_address("malloc_base"sv));
void* _alloc_base = OnHeapPointer<void>(_heap_ptr_malloc_base).decode_local();
void* _alloc_base = OnHeapPointer<void>(_heap_ptr_malloc_base).decode();
return _malloc(_alloc_base, size);
}
void custom_free(void* mem)
{
static CustomFreeFun* _free = (CustomFreeFun*)get_address("custom_free"sv);
static size_t _heap_ptr_malloc_base = *reinterpret_cast<size_t*>(get_address("malloc_base"sv));
void* _alloc_base = OnHeapPointer<void>(_heap_ptr_malloc_base).decode_local();
void* _alloc_base = OnHeapPointer<void>(_heap_ptr_malloc_base).decode();
_free(_alloc_base, mem);
}
2 changes: 1 addition & 1 deletion src/game_api/custom_types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include <utility> // for min, find
#include <vector> // for vector, allocator, vector<>::iterator

#include "entity.hpp" // for to_id
#include "entity_db.hpp" // for to_id

const std::vector<std::pair<CUSTOM_TYPE, std::string_view>> custom_type_names = {
{CUSTOM_TYPE::ACIDBUBBLE, "ACIDBUBBLE"},
Expand Down
6 changes: 3 additions & 3 deletions src/game_api/drops.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
#include <string_view> //
#include <utility> // for min, max

#include "entity.hpp" // for to_id
#include "memory.hpp" // for Memory, recover_mem, write_mem_recoverable
#include "search.hpp" // for find_inst
#include "entity_db.hpp" // for to_id
#include "memory.hpp" // for Memory, recover_mem, write_mem_recoverable
#include "search.hpp" // for find_inst

std::vector<DropEntry> drop_entries{
{"ALTAR_DICE_CLIMBINGGLOVES", "\xBA\x0D\x02\x00\x00\xEB\x05"sv, VTABLE_OFFSET::NONE, 0, 1}, // VTABLE_OFFSET::FLOOR_ALTAR, 26
Expand Down
3 changes: 1 addition & 2 deletions src/game_api/entities_activefloors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
#include "entity.hpp" // for Entity, to_id, EntityDB
#include "items.hpp" // IWYU pragma: keep
#include "layer.hpp" // for EntityList::Range, EntityList, EntityList::Ent...
#include "search.hpp" // for get_address
#include "sound_manager.hpp" //
#include "sound_manager.hpp" // for construct_soundmeta

uint8_t Olmec::broken_floaters()
{
Expand Down
14 changes: 6 additions & 8 deletions src/game_api/entities_floors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include "layer.hpp" // for EntityList, EntityList::Range, Layer, Entit...
#include "movable.hpp" // for Movable
#include "spawn_api.hpp" // for spawn_entity_over
#include "state.hpp" // for State
#include "state.hpp" // for StateMemory
#include "texture.hpp" // for Texture

void Floor::fix_border_tile_animation()
Expand Down Expand Up @@ -88,8 +88,7 @@ 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);
auto layer_ptr = get_state_ptr()->layer(layer);

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

auto& state = State::get();
auto layer_ptr = state.layer(layer);
auto layer_ptr = get_state_ptr()->layer(layer);
add_decoration_opt(side, decoration_entity_type, layer_ptr);
}
void Floor::remove_decoration(FLOOR_SIDE side)
Expand Down Expand Up @@ -741,7 +739,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();
auto state = get_state_ptr();

if (ent_type == locked_door || ent_type == locked_door + 1) // plus one for DOOR_LOCKED_PEN
{
Expand Down Expand Up @@ -772,7 +770,7 @@ void Door::unlock(bool unlock)
if (ent_type == entrance_door || ent_type == entrance_door + 1 || ent_type == entrance_door + 3)
{
static const ENT_TYPE door_bg = to_id("ENT_TYPE_BG_DOOR");
const auto entities = state.layer(this->layer)->get_entities_overlapping_grid_at(x, y);
const auto entities = state->layer(this->layer)->get_entities_overlapping_grid_at(x, y);
if (entities == nullptr)
return;
for (const auto& item : entities->entities())
Expand Down Expand Up @@ -817,7 +815,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->layer(layer)->spawn_entity_over(door_bg_large, this, 0, 2.0);
main_door->door_blocker->animation_frame = 1;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/game_api/entities_floors.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ class Door : public Floor
// can't be bother to look into the functions
virtual void on_enter_attempt(Entity* who) = 0;

// check if it's CHAR_*, then sets State.level_flags -> 21 (Hide hud, transition)
// check if it's CHAR_*, then sets state.level_flags -> 21 (Hide hud, transition)
virtual void hide_ui(Entity* who) = 0;

/// Returns the entity state / behavior id to set the entity to after the entering animation.
Expand Down
7 changes: 3 additions & 4 deletions src/game_api/entities_items.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@
#include <type_traits> // for move
#include <vector> // for vector, _Vector_iterator, _Vector_cons...

#include "entities_chars.hpp" // for PowerupCapable
#include "entity.hpp" // for to_id, SHAPE
#include "entity_hooks_info.hpp" // for HookWithId, EntityHooksInfo
#include "vtable_hook.hpp" // for hook_vtable
#include "entities_chars.hpp" // for PowerupCapable
#include "entity.hpp" // SHAPE
#include "entity_db.hpp" // to_id

void ParachutePowerup::deploy()
{
Expand Down
Loading
Loading