-
Notifications
You must be signed in to change notification settings - Fork 36
Custom level generation #750
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
base: main
Are you sure you want to change the base?
Changes from 9 commits
8b17521
7c047e1
ea8320c
d2bf067
2490b78
8f8c157
f9fb019
f65b5f5
d313242
e2f77b4
32e508d
1feba7f
bfab985
845072c
c2cc975
47dbdd9
7d5ce08
57a718d
05c839a
9a7e37e
40605fd
797a52d
8ad7897
2e6f9f4
e1b7d98
bd2415e
52c50b5
c308a3e
0bbb155
d89ff98
fc5098b
6edd184
2c37191
86ff2bb
062d157
4732779
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| #include "HookSystem.h" | ||
| #include "IsaacRepentance.h" | ||
| #include "LuaCore.h" | ||
| #include "Exception.h" | ||
| #include "Log.h" | ||
| #include "LuaDungeonGenerator.h" | ||
|
|
||
| DungeonGenerator* GetDungeonGenerator(lua_State* L) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I don't know exactly how important the |
||
| return *lua::GetRawUserdata<DungeonGenerator**>(L, 1, lua::metatables::DungeonGeneratorMT); | ||
| } | ||
|
|
||
| LUA_FUNCTION(place_room) { | ||
|
||
| DungeonGenerator* generator = GetDungeonGenerator(L); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it would be good for DungeonGenerator itself to contain all its functions such as PlaceRoom (ie, |
||
| RoomConfig_Room* config = lua::GetLuabridgeUserdata<RoomConfig_Room*>(L, 2, lua::Metatables::CONST_ROOM_CONFIG_ROOM, "RoomConfig"); | ||
| uint32_t row = (uint32_t)luaL_checkinteger(L, 3); | ||
| uint32_t col = (uint32_t)luaL_checkinteger(L, 4); | ||
|
||
| uint32_t seed = (uint32_t)luaL_checkinteger(L, 5); | ||
|
|
||
| DungeonGeneratorRoom* generatorRoom = new DungeonGeneratorRoom(config, row, col, seed); | ||
|
||
| generator->rooms[generator->num_rooms] = *generatorRoom; | ||
|
|
||
| generator->num_rooms++; | ||
|
|
||
| return 1; | ||
|
||
| } | ||
|
|
||
| LUA_FUNCTION(set_final_boss_room) { | ||
| DungeonGenerator* generator = GetDungeonGenerator(L); | ||
| uint32_t row = (uint32_t)luaL_checkinteger(L, 2); | ||
|
||
| uint32_t col = (uint32_t)luaL_checkinteger(L, 3); | ||
|
|
||
| for (size_t i = 0; i < generator->num_rooms; i++) { | ||
| DungeonGeneratorRoom* generator_room = &(generator->rooms[i]); | ||
|
|
||
| if (generator_room->room != NULL) { | ||
| if (row == generator_room->row && col == generator_room->col) { | ||
| generator_room->is_final_boss = true; | ||
| } else { | ||
| generator_room->is_final_boss = false; | ||
| } | ||
| } else { | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| return 1; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For lua bindings, If we dont return anything, we should Though, if we add any validation, this function could maybe return true on success ( |
||
| } | ||
|
|
||
| static void RegisterDungeonGenerator(lua_State* L) { | ||
| luaL_Reg functions[] = { | ||
| {"PlaceRoom", place_room}, | ||
| {"SetFinalBossRoom", set_final_boss_room}, | ||
| { NULL, NULL } | ||
| }; | ||
|
|
||
| lua::RegisterNewClass(L, lua::metatables::DungeonGeneratorMT, lua::metatables::DungeonGeneratorMT, functions); | ||
| } | ||
|
|
||
| HOOK_METHOD(LuaEngine, RegisterClasses, () -> void) { | ||
| super(); | ||
| RegisterDungeonGenerator(_state); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| #pragma once | ||
|
|
||
| struct DungeonGeneratorRoom { | ||
| RoomConfig_Room* room; | ||
| uint32_t row; | ||
| uint32_t col; | ||
| uint32_t seed; | ||
| bool is_final_boss = false; | ||
|
|
||
| DungeonGeneratorRoom() { | ||
| this->room = NULL; | ||
|
||
| this->row = -1; | ||
| this->col = -1; | ||
| this->seed = -1; | ||
| } | ||
|
|
||
| DungeonGeneratorRoom(RoomConfig_Room* room, uint32_t row, uint32_t col, uint32_t seed) { | ||
| this->room = room; | ||
| this->row = row; | ||
| this->col = col; | ||
| this->seed = seed; | ||
| } | ||
| }; | ||
|
|
||
| struct DungeonGenerator { | ||
| int a; | ||
|
||
| int num_rooms; | ||
| DungeonGeneratorRoom rooms[169]; | ||
|
|
||
| DungeonGenerator() { | ||
|
||
| a = 0; | ||
| num_rooms = 0; | ||
| } | ||
| }; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The code added here is actually in the wrong place, technically. This directory is primarily for Assembly (ASM) Patches Since this is just function hooks, should probably move this stuff to |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,6 +5,7 @@ | |
| #include "ASMLevel.h" | ||
| #include "../../LuaInterfaces/Level.h" | ||
| #include "../../LuaInterfaces/Room/Room.h" | ||
| #include "../../LuaInterfaces/LuaDungeonGenerator.h" | ||
|
||
|
|
||
| std::bitset<36> generateLevels; | ||
|
|
||
|
|
@@ -82,8 +83,67 @@ void ASMPatchVoidGeneration() { | |
| sASMPatcher.PatchAt(addr, &patch); | ||
| } | ||
|
|
||
| //MC_PRE_GENERATE_DUNGEON(1340) | ||
| bool ProcessGenerateDungeonCallback(Level* level, RNG* rng, int dungeonType) { | ||
| const int callbackId = 1340; | ||
| if (!CallbackState.test(callbackId - 1000)) { | ||
| return false; | ||
| } | ||
|
|
||
| lua_State* L = g_LuaEngine->_state; | ||
| lua::LuaStackProtector protector(L); | ||
| lua_rawgeti(L, LUA_REGISTRYINDEX, g_LuaEngine->runCallbackRegistry->key); | ||
|
|
||
| DungeonGenerator* generator = new DungeonGenerator(); | ||
| lua::LuaResults results = lua::LuaCaller(L) | ||
| .push(callbackId) | ||
| .push(dungeonType) | ||
| .push(generator, lua::metatables::DungeonGeneratorMT) | ||
| .push(rng, lua::Metatables::RNG) | ||
| .call(1); | ||
|
|
||
| if (results || !lua_isboolean(L, -1) || !lua_toboolean(L, -1)) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| level->reset_room_list(false); | ||
|
||
|
|
||
| for (size_t i = 0; i < 507; i++) | ||
| { | ||
| g_Game->_roomOffset[i] = -1; | ||
| } | ||
|
|
||
| g_Game->_nbRooms = 0; | ||
|
|
||
| for (size_t i = 0; i < generator->num_rooms; i++) | ||
| { | ||
| DungeonGeneratorRoom generator_room = generator->rooms[i]; | ||
|
|
||
| if (generator_room.room != NULL) { | ||
| LevelGenerator_Room* level_generator_room = new LevelGenerator_Room(); | ||
|
||
| level_generator_room->_gridColIdx = generator_room.col; | ||
| level_generator_room->_gridLineIdx = generator_room.row; | ||
| level_generator_room->_doors = 15; | ||
|
|
||
| g_Game->PlaceRoom(level_generator_room, generator_room.room, generator_room.seed, 0); | ||
|
|
||
| if (generator_room.is_final_boss) { | ||
| g_Game->_lastBossRoomListIdx = i; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| HOOK_METHOD(Level, generate_dungeon, (RNG* rng) -> void) | ||
| { | ||
| bool skip = ProcessGenerateDungeonCallback(this, rng, 0); | ||
| if (skip) { | ||
| return; | ||
| } | ||
|
|
||
| if (this->_stage == 12) | ||
| { | ||
| if (generateLevels.any()) | ||
|
|
@@ -117,6 +177,51 @@ HOOK_METHOD(Level, generate_dungeon, (RNG* rng) -> void) | |
| super(rng); | ||
| } | ||
|
|
||
| HOOK_METHOD(Level, generate_blue_womb, () -> void) { | ||
| bool skip = ProcessGenerateDungeonCallback(this, NULL, 1); | ||
| if (skip) { | ||
| return; | ||
| } | ||
|
|
||
| super(); | ||
| } | ||
|
|
||
| HOOK_METHOD(Level, generate_backwards_dungeon, () -> void) { | ||
| bool skip = ProcessGenerateDungeonCallback(this, NULL, 2); | ||
| if (skip) { | ||
| return; | ||
| } | ||
|
|
||
| super(); | ||
| } | ||
|
|
||
| HOOK_METHOD(Level, generate_home_dungeon, () -> void) { | ||
| bool skip = ProcessGenerateDungeonCallback(this, NULL, 3); | ||
| if (skip) { | ||
| return; | ||
| } | ||
|
|
||
| super(); | ||
| } | ||
|
|
||
| HOOK_METHOD(Level, generate_redkey_dungeon, () -> void) { | ||
| bool skip = ProcessGenerateDungeonCallback(this, NULL, 4); | ||
| if (skip) { | ||
| return; | ||
| } | ||
|
|
||
| super(); | ||
| } | ||
|
|
||
| HOOK_METHOD(Level, generate_greed_dungeon, () -> void) { | ||
| bool skip = ProcessGenerateDungeonCallback(this, NULL, 5); | ||
| if (skip) { | ||
| return; | ||
| } | ||
|
|
||
| super(); | ||
| } | ||
|
|
||
| bool __stdcall SpawnSpecialQuestDoorValidStageTypeCheck() { | ||
| // Always return true if we forced it via Room:TrySpawnSpecialQuestDoor | ||
| bool ret = roomASM.ForceSpecialQuestDoor; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
minor nitpick, change the name of the bool parameter to 'resetLilPortalRoom'