Skip to content

Commit

Permalink
Merge branch 'main' into PauseAPI
Browse files Browse the repository at this point in the history
  • Loading branch information
Dregu committed Jan 16, 2024
2 parents e3f23fb + 2b63f48 commit 7443215
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 82 deletions.
58 changes: 40 additions & 18 deletions src/game_api/level_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1813,9 +1813,29 @@ uint32_t ThemeInfo::get_aux_id()
void LevelGenSystem::init()
{
data->init();

for (auto theme : themes)
{
if (theme == theme_arena) // no reason to?
continue;

hook_vtable<void(ThemeInfo*), 0x15>( // spawn_transition
theme,
[](ThemeInfo* th, void (*original)(ThemeInfo*))
{
push_spawn_type_flags(SPAWN_TYPE_LEVEL_GEN_GENERAL);
OnScopeExit pop{[]
{ pop_spawn_type_flags(SPAWN_TYPE_LEVEL_GEN_GENERAL); }};

if (pre_event(ON::PRE_LEVEL_GENERATION))
return;
original(th);
post_event(ON::POST_LEVEL_GENERATION);
});
}
}

bool LevelGenSystem::pre_populate_level(ThemeInfo*, uint64_t, uint64_t, uint64_t)
void LevelGenSystem::populate_level_hook(ThemeInfo* self, uint64_t param_2, uint64_t param_3, uint64_t param_4, PopulateLevelFun* original)
{
post_room_generation();

Expand All @@ -1828,27 +1848,29 @@ bool LevelGenSystem::pre_populate_level(ThemeInfo*, uint64_t, uint64_t, uint64_t
}
}

return false;
original(self, param_2, param_3, param_4);
}
void LevelGenSystem::post_populate_level(ThemeInfo*, uint64_t, uint64_t, uint64_t)
// void LevelGenSystem::populate_transition_hook([[maybe_unused]] ThemeInfo* self, [[maybe_unused]] PopulateTransitionFun* original)
//{
// push_spawn_type_flags(SPAWN_TYPE_LEVEL_GEN_GENERAL);
// OnScopeExit pop{[]
// { pop_spawn_type_flags(SPAWN_TYPE_LEVEL_GEN_GENERAL); }};
// pre_level_generation();
// original(self);
// post_level_generation();
// }
void LevelGenSystem::do_procedural_spawn_hook(ThemeInfo* self, SpawnInfo* spawn_info, DoProceduralSpawnFun* original)
{
}
push_spawn_type_flags(SPAWN_TYPE_LEVEL_GEN_PROCEDURAL);
OnScopeExit pop{[]
{ pop_spawn_type_flags(SPAWN_TYPE_LEVEL_GEN_PROCEDURAL); }};

bool LevelGenSystem::pre_populate_transition(ThemeInfo*)
{
return pre_event(ON::PRE_LEVEL_GENERATION);
}
void LevelGenSystem::post_populate_transition(ThemeInfo*)
{
post_event(ON::POST_LEVEL_GENERATION);
}
if (handle_chance(spawn_info))
{
return;
}

bool LevelGenSystem::pre_procedural_spawn(ThemeInfo*, SpawnInfo* spawn_info)
{
return handle_chance(spawn_info);
}
void LevelGenSystem::post_procedural_spawn(ThemeInfo*, SpawnInfo*)
{
original(self, spawn_info);
}

std::pair<int, int> LevelGenSystem::get_room_index(float x, float y)
Expand Down
22 changes: 8 additions & 14 deletions src/game_api/level_api.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
#include "containers/game_vector.hpp" // for game_vector
#include "level_api_types.hpp" // for ShortTileCodeDef
#include "math.hpp" // for AABB (ptr only), Vec2
#include "spawn_api.hpp" // for SpawnType

class Entity;

Expand Down Expand Up @@ -428,26 +427,21 @@ struct LevelGenSystem
{
for (ThemeInfo* theme : themes)
{
hook_impl.template hook<PopulateLevelFun, 0xd, SPAWN_TYPE_NONE, struct PopulateLevelTag>(
theme, &pre_populate_level, &post_populate_level);
hook_impl.template hook<PopulateTransitionFun, 0x15, SPAWN_TYPE_LEVEL_GEN_GENERAL, struct PopulateTransitionTag>(
theme, &pre_populate_transition, &post_populate_transition);
hook_impl.template hook<DoProceduralSpawnFun, 0x33, SPAWN_TYPE_LEVEL_GEN_PROCEDURAL, struct ProceduralSpawnTag>(
theme, &pre_procedural_spawn, &post_procedural_spawn);
hook_impl.template hook<PopulateLevelFun, 0xd>(theme, &populate_level_hook);
hook_impl.template hook<DoProceduralSpawnFun, 0x33>(theme, &do_procedural_spawn_hook);
// this didn't work right
// hook_impl.template hook<PopulateTransitionFun, 0x15>(theme, &populate_transition_hook);
}
}

using PopulateLevelFun = void(ThemeInfo*, uint64_t, uint64_t, uint64_t);
static bool pre_populate_level(ThemeInfo*, uint64_t, uint64_t, uint64_t);
static void post_populate_level(ThemeInfo*, uint64_t, uint64_t, uint64_t);
static void populate_level_hook(ThemeInfo*, uint64_t, uint64_t, uint64_t, PopulateLevelFun*);

using PopulateTransitionFun = void(ThemeInfo*);
static bool pre_populate_transition(ThemeInfo*);
static void post_populate_transition(ThemeInfo*);
// using PopulateTransitionFun = void(ThemeInfo*);
// static void populate_transition_hook(ThemeInfo*, PopulateTransitionFun*);

using DoProceduralSpawnFun = void(ThemeInfo*, SpawnInfo*);
static bool pre_procedural_spawn(ThemeInfo*, SpawnInfo*);
static void post_procedural_spawn(ThemeInfo*, SpawnInfo*);
static void do_procedural_spawn_hook(ThemeInfo*, SpawnInfo*, DoProceduralSpawnFun*);

LevelGenData* data;
uint64_t unknown2;
Expand Down
10 changes: 0 additions & 10 deletions src/game_api/spawn_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -592,11 +592,6 @@ void update_spawn_type_flags()
}
void push_spawn_type_flags(SPAWN_TYPE flags)
{
if (flags == SPAWN_TYPE_NONE)
{
return;
}

for (size_t i = 0; i < g_SpawnTypes.size(); i++)
{
if (flags & (1 << i))
Expand All @@ -608,11 +603,6 @@ void push_spawn_type_flags(SPAWN_TYPE flags)
}
void pop_spawn_type_flags(SPAWN_TYPE flags)
{
if (flags == SPAWN_TYPE_NONE)
{
return;
}

for (size_t i = 0; i < g_SpawnTypes.size(); i++)
{
if (flags & (1 << i))
Expand Down
1 change: 0 additions & 1 deletion src/game_api/spawn_api.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ enum SpawnType : SPAWN_TYPE
SPAWN_TYPE_SCRIPT = 1 << 4,
SPAWN_TYPE_SYSTEMIC = 1 << 5,
SPAWN_TYPE_NUM_FLAGS = 6,
SPAWN_TYPE_NONE = std::numeric_limits<SPAWN_TYPE>::max(),
SPAWN_TYPE_ANY = SPAWN_TYPE_LEVEL_GEN | SPAWN_TYPE_SCRIPT | SPAWN_TYPE_SYSTEMIC
};

Expand Down
55 changes: 16 additions & 39 deletions src/game_api/state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -228,58 +228,35 @@ void State::godmode_companions(bool g)

struct ThemeHookImpl
{
template <class FunT, SpawnType SPAWN_TYPE, class TagT, class PreHookFunT, class PostHookFunT>
struct hook_wrapper;
template <class... ArgsT, SpawnType SPAWN_TYPE, class TagT, class PreHookFunT, class PostHookFunT>
struct hook_wrapper<void(ArgsT...), SPAWN_TYPE, TagT, PreHookFunT, PostHookFunT>
template <class FunT, class HookFunT>
struct lua_wrapper;
template <class... ArgsT, class HookFunT>
struct lua_wrapper<void(ArgsT...), HookFunT>
{
static bool lua_pre(ArgsT... args)
static auto make(HookFunT* fun)
{
push_spawn_type_flags(SPAWN_TYPE);
if (pre(args...))
return [=](ArgsT... args)
{
pop_spawn_type_flags(SPAWN_TYPE);
return true;
}
return false;
}
static void lua_post(ArgsT... args)
{
post(args...);
pop_spawn_type_flags(SPAWN_TYPE);
thread_local bool tester;
tester = true;
fun(args..., [](ArgsT...)
{ tester = false; });
return tester;
};
}
static void raw(ArgsT... args, void (*original)(ArgsT...))
{
push_spawn_type_flags(SPAWN_TYPE);
OnScopeExit pop{[]
{ pop_spawn_type_flags(SPAWN_TYPE); }};

if (pre(args...))
return;
original(args...);
post(args...);
}

inline static PreHookFunT* pre{nullptr};
inline static PostHookFunT* post{nullptr};
};

template <class FunT, size_t Index, SpawnType SPAWN_TYPE, class TagT, class PreHookFunT, class PostHookFunT>
void hook(ThemeInfo* theme, PreHookFunT* pre_fun, PostHookFunT* post_fun)
template <class FunT, size_t Index, class HookFunT>
void hook(ThemeInfo* theme, HookFunT* fun)
{
using WrapperT = hook_wrapper<FunT, SPAWN_TYPE, TagT, PreHookFunT, PostHookFunT>;
WrapperT::pre = pre_fun;
WrapperT::post = post_fun;

if (get_do_hooks())
{
auto& vtable = NThemeVTables::get_theme_info_vtable(get_lua_vm());
vtable.set_pre<FunT, Index>(theme, vtable.reserve_callback_id(theme), WrapperT::lua_pre);
vtable.set_post<FunT, Index>(theme, vtable.reserve_callback_id(theme), WrapperT::lua_post);
vtable.set_pre<FunT, Index>(theme, vtable.reserve_callback_id(theme), lua_wrapper<FunT, HookFunT>::make(fun));
}
else
{
hook_vtable<FunT, Index>(theme, WrapperT::raw);
hook_vtable<FunT, Index>(theme, fun);
}
}
};
Expand Down

0 comments on commit 7443215

Please sign in to comment.