Curation round 10: combat, pathfinding, death and game over, items and stats, room state, Continue, Lua callbacks
Seven agents took over tear and contact collision, NPC death with blood and gibs, champions, PathFinder, the Gaper's kin, the player's death with the game over screen, item pedestals with collectible pickup, AddCollectible and EvaluateItems, room state save and restore, GameState::read, pressure plates, fireplaces, entity lights, ambient sounds, and one Lua callback enum with gated runners. Integration reconstructed Room::Update's loot preview and Entity_NPC::UpdatePickupGhosts and replaced a leftover Sacred Orb stub that stopped every pedestal roll. Under gdb: Gapers die with blood and gibs, a cleared room opens its doors, the treasure room's item is taken (banner, costume, stats: a Magic Mushroom gives damage 3.5 -> 6.12 and a heart), death leads to the game over diary, and a saved run is read back on the next boot with Continue offered. 3028 functions taken over. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
48933cd312
commit
d8385e6c64
64 files changed
+8665
-3059
No files matched your search
+6
-266
@@ -1,14 +1,6 @@
|
||||
#include "lua_engine.h"
|
||||
#include "classes.h"
|
||||
#include "data.h"
|
||||
#include "entity_bomb.h"
|
||||
#include "entity_familiar.h"
|
||||
#include "entity_knife.h"
|
||||
#include "entity_npc.h"
|
||||
#include "entity_pickup.h"
|
||||
#include "entity_player.h"
|
||||
#include "entity_projectile.h"
|
||||
#include "entity_tear.h"
|
||||
#include "game.h"
|
||||
#include "manager.h"
|
||||
#include "platform.h"
|
||||
@@ -43,7 +35,6 @@ constexpr const char* UNLOAD_MOD = "_UnloadMod";
|
||||
|
||||
constexpr int API_VERSION_NONE = -1;
|
||||
constexpr int API_VERSION_CURRENT = 1;
|
||||
constexpr int PILL_EFFECT_UNCHANGED = -1;
|
||||
constexpr int GC_COOLDOWN_FRAMES = 600;
|
||||
constexpr int GC_SHRINK_DIVISOR = 100;
|
||||
constexpr int GC_SHRINK_MIN = 10;
|
||||
@@ -58,21 +49,6 @@ constexpr luaL_Reg SANDBOX_LIBRARIES[] = {
|
||||
{LUA_STRLIBNAME, luaopen_string}, {LUA_MATHLIBNAME, luaopen_math}, {LUA_UTF8LIBNAME, luaopen_utf8},
|
||||
{LUA_DBLIBNAME, luaopen_debug}};
|
||||
|
||||
// A game object a callback hands to Lua by pointer, and the class whose metatable it gets.
|
||||
struct LuaObject
|
||||
{
|
||||
void* object;
|
||||
LuaClass luaClass;
|
||||
};
|
||||
|
||||
// One run of a callback: the name its errors log, its id, and the parameter mods filter it by (-1 for none).
|
||||
struct CallbackRun
|
||||
{
|
||||
const char* name;
|
||||
LuaCallback id;
|
||||
int param = -1;
|
||||
};
|
||||
|
||||
// One distinct address per class, the registry keys of their metatables, as LuaBridge's class keys are.
|
||||
static std::array<char, LUA_CLASS_COUNT> classKeys;
|
||||
|
||||
@@ -81,105 +57,18 @@ const void* lua_class_key(LuaClass luaClass)
|
||||
return &classKeys[luaClass];
|
||||
}
|
||||
|
||||
// Stops where a mod has a function for a callback LuaEngine does not run yet.
|
||||
void lua_callback_check(int callback, const char* name)
|
||||
// Logs the error a failed call left on top of the stack, popping it, and shows it on the console once a game exists.
|
||||
// The original leaves the message on the stack.
|
||||
void lua_error_log(lua_State* state, const char* name)
|
||||
{
|
||||
if (static_cast<LuaEngine*>(g_luaEngine)->enabledCallbacks[callback]) reconstruction_missing(name);
|
||||
}
|
||||
|
||||
// Pushes a callback argument: integers and booleans as themselves.
|
||||
static void value_push(lua_State* state, int value)
|
||||
{
|
||||
lua_pushinteger(state, value);
|
||||
}
|
||||
|
||||
static void value_push(lua_State* state, unsigned int value)
|
||||
{
|
||||
lua_pushinteger(state, value);
|
||||
}
|
||||
|
||||
static void value_push(lua_State* state, bool value)
|
||||
{
|
||||
lua_pushboolean(state, value);
|
||||
}
|
||||
|
||||
// Pushes an object as a userdata holding its pointer, with its class's metatable, or nil for none.
|
||||
static void value_push(lua_State* state, LuaObject value)
|
||||
{
|
||||
if (!value.object) return lua_pushnil(state);
|
||||
*(void**)lua_newuserdata(state, sizeof(void*)) = value.object;
|
||||
lua_rawgetp(state, LUA_REGISTRYINDEX, lua_class_key(value.luaClass));
|
||||
lua_setmetatable(state, -2);
|
||||
}
|
||||
|
||||
// The message a failed call left on top of the stack, popped. The original leaves it there.
|
||||
static std::string error_pop(lua_State* state)
|
||||
{
|
||||
if (lua_gettop(state) < 1) return "missing error";
|
||||
const char* text = lua_tostring(state, -1);
|
||||
const char* text = lua_gettop(state) < 1 ? "missing error" : lua_tostring(state, -1);
|
||||
std::string error = text ? text : "";
|
||||
lua_pop(state, 1);
|
||||
return error;
|
||||
}
|
||||
if (lua_gettop(state) > 0) lua_pop(state, 1);
|
||||
|
||||
// Logs a callback's error, and shows it on the console once a game exists.
|
||||
static void error_log(const char* name, const std::string& error)
|
||||
{
|
||||
log_printf(LOG_INFO, "Error in \"%s\" call: %s\n", name, error.c_str());
|
||||
if (g_game) g_game->console.print_format(CONSOLE_ERROR, "\"%s\" failed: %s\n", name, error.c_str());
|
||||
}
|
||||
|
||||
// Calls _RunCallback with a callback's id, its filter parameter and its arguments when a mod added a function for it,
|
||||
// leaving its one result on the stack. Whether it ran, false without a function or on an error, which is logged.
|
||||
// The callers, result readers and error throws the original instantiates per callback are this and its readers
|
||||
// (00872980, 00872c60, 00873470, 00873980, 00873d00, 00873fc0, 00874120, 00874210, 00874310, 00874a10, 00874f60)
|
||||
// (0085c080, 008727d0, 0086130e, 00861225, 00861d96, 00861e7f, 008623a6, 0086248f, 008628b6, 0086299f, 00864453)
|
||||
// (0086453c, 00865bbc), and their catches log through error_log (00860ea3, 008611da, 0086191f, 00861d4b, 0086235b)
|
||||
// (0086286b, 00862ea4, 008632e1, 00863706, 00863b6a, 00863e31, 00863f1e, 008640de, 00864408, 00864f6e, 00865b69)
|
||||
// (008661cc).
|
||||
template <typename... Args>
|
||||
static bool callback_run(LuaEngine& engine, CallbackRun run, Args... args)
|
||||
{
|
||||
if (!engine.enabledCallbacks[run.id]) return false;
|
||||
lua_State* state = engine.state;
|
||||
|
||||
lua_rawgeti(state, LUA_REGISTRYINDEX, engine.runCallbackRef);
|
||||
value_push(state, (int)run.id);
|
||||
value_push(state, run.param);
|
||||
(value_push(state, args), ...);
|
||||
if (lua_pcall(state, sizeof...(Args) + 2, 1, 0) == LUA_OK) return true;
|
||||
|
||||
error_log(run.name, error_pop(state));
|
||||
return false;
|
||||
}
|
||||
|
||||
// Runs a callback whose result the game ignores.
|
||||
template <typename... Args>
|
||||
static void callback_notify(LuaEngine& engine, CallbackRun run, Args... args)
|
||||
{
|
||||
if (callback_run(engine, run, args...)) lua_pop(engine.state, 1);
|
||||
}
|
||||
|
||||
// Runs a Pre*Collision callback for an entity and what it hits, and reads its result as true, false or nothing.
|
||||
static LuaResult collision_run(LuaEngine& engine, CallbackRun run, LuaObject entity, Entity* collider, bool isLow)
|
||||
{
|
||||
if (!callback_run(engine, run, entity, LuaObject{collider, LUA_CLASS_ENTITY}, isLow)) return LUA_RESULT_NIL;
|
||||
|
||||
LuaResult result = lua_isnil(engine.state, -1) ? LUA_RESULT_NIL
|
||||
: lua_toboolean(engine.state, -1) ? LUA_RESULT_TRUE
|
||||
: LUA_RESULT_FALSE;
|
||||
lua_pop(engine.state, 1);
|
||||
return result;
|
||||
}
|
||||
|
||||
// Sets value to a field of the table on top of the stack, when the field is not nil.
|
||||
static void field_read(lua_State* state, const char* key, bool& value)
|
||||
{
|
||||
lua_getfield(state, -1, key);
|
||||
if (!lua_isnil(state, -1)) value = lua_toboolean(state, -1);
|
||||
lua_pop(state, 1);
|
||||
}
|
||||
|
||||
// A field of the package table, empty when it is not a string.
|
||||
static std::string package_field_get(lua_State* state, const char* field)
|
||||
{
|
||||
@@ -256,7 +145,7 @@ static void mod_unload(LuaEngine& engine, LuaMod& mod)
|
||||
lua_rawgeti(state, LUA_REGISTRYINDEX, engine.unloadModRef);
|
||||
lua_rawgeti(state, LUA_REGISTRYINDEX, mod.tableRef);
|
||||
if (lua_pcall(state, 1, 1, 0) == LUA_OK) lua_pop(state, 1);
|
||||
else error_log(UNLOAD_MOD, error_pop(state));
|
||||
else lua_error_log(state, UNLOAD_MOD);
|
||||
luaL_unref(state, LUA_REGISTRYINDEX, mod.tableRef);
|
||||
mod.tableRef = LUA_REF_NONE;
|
||||
}
|
||||
@@ -416,152 +305,3 @@ int LuaEngine::Isaac_GetItemIdByName(std::string_view name)
|
||||
auto found = names.find(name);
|
||||
return found == names.end() ? -1 : found->second;
|
||||
}
|
||||
|
||||
// 00862250 Runs MC_PRE_BOMB_COLLISION, filtered by the bomb's variant.
|
||||
LuaResult LuaEngine::PreBombCollision(Entity_Bomb* bomb, Entity* collider, bool isLow)
|
||||
{
|
||||
CallbackRun run = {"PreBombCollision", MC_PRE_BOMB_COLLISION, (int)bomb->variant};
|
||||
return collision_run(*this, run, {bomb, LUA_CLASS_BOMB}, collider, isLow);
|
||||
}
|
||||
|
||||
// 008610d0 Runs MC_PRE_FAMILIAR_COLLISION, filtered by the familiar's variant.
|
||||
LuaResult LuaEngine::PreFamiliarCollision(Entity_Familiar* familiar, Entity* collider, bool isLow)
|
||||
{
|
||||
CallbackRun run = {"PreFamiliarCollision", MC_PRE_FAMILIAR_COLLISION, (int)familiar->variant};
|
||||
return collision_run(*this, run, {familiar, LUA_CLASS_FAMILIAR}, collider, isLow);
|
||||
}
|
||||
|
||||
// 00861c40 Runs MC_PRE_KNIFE_COLLISION, filtered by the knife's subtype.
|
||||
LuaResult LuaEngine::PreKnifeCollision(Entity_Knife* knife, Entity* collider, bool isLow)
|
||||
{
|
||||
CallbackRun run = {"PreKnifeCollision", MC_PRE_KNIFE_COLLISION, (int)knife->subtype};
|
||||
return collision_run(*this, run, {knife, LUA_CLASS_KNIFE}, collider, isLow);
|
||||
}
|
||||
|
||||
// 00860d70 Runs MC_PRE_NPC_COLLISION, filtered by the NPC's type.
|
||||
LuaResult LuaEngine::PreNPCCollision(Entity_NPC* npc, Entity* collider, bool isLow)
|
||||
{
|
||||
CallbackRun run = {"PreNPCCollision", MC_PRE_NPC_COLLISION, (int)npc->type};
|
||||
return collision_run(*this, run, {npc, LUA_CLASS_NPC}, collider, isLow);
|
||||
}
|
||||
|
||||
// 008631b0 Runs MC_PRE_PICKUP_COLLISION, filtered by the pickup's variant.
|
||||
LuaResult LuaEngine::PrePickupCollision(Entity_Pickup* pickup, Entity* collider, bool isLow)
|
||||
{
|
||||
CallbackRun run = {"PrePickupCollision", MC_PRE_PICKUP_COLLISION, (int)pickup->variant};
|
||||
return collision_run(*this, run, {pickup, LUA_CLASS_PICKUP}, collider, isLow);
|
||||
}
|
||||
|
||||
// 00864300 Runs MC_PRE_PLAYER_COLLISION, filtered by the player's variant.
|
||||
LuaResult LuaEngine::PrePlayerCollide(Entity_Player* player, Entity* collider, bool isLow)
|
||||
{
|
||||
CallbackRun run = {"PrePlayerCollision", MC_PRE_PLAYER_COLLISION, (int)player->variant};
|
||||
return collision_run(*this, run, {player, LUA_CLASS_PLAYER}, collider, isLow);
|
||||
}
|
||||
|
||||
// 00862760 Runs MC_PRE_PROJECTILE_COLLISION, filtered by the projectile's variant.
|
||||
LuaResult LuaEngine::PreProjectileCollision(Entity_Projectile* projectile, Entity* collider, bool isLow)
|
||||
{
|
||||
CallbackRun run = {"PreProjectileCollision", MC_PRE_PROJECTILE_COLLISION, (int)projectile->variant};
|
||||
return collision_run(*this, run, {projectile, LUA_CLASS_PROJECTILE}, collider, isLow);
|
||||
}
|
||||
|
||||
// 00862d70 Runs MC_PRE_TEAR_COLLISION, filtered by the tear's variant.
|
||||
LuaResult LuaEngine::PreTearCollision(Entity_Tear* tear, Entity* collider, bool isLow)
|
||||
{
|
||||
CallbackRun run = {"PreTearCollision", MC_PRE_TEAR_COLLISION, (int)tear->variant};
|
||||
return collision_run(*this, run, {tear, LUA_CLASS_TEAR}, collider, isLow);
|
||||
}
|
||||
|
||||
// 00863e90 Runs MC_EVALUATE_CACHE for a player's stat, filtered by the cache flag.
|
||||
void LuaEngine::EvaluateItems(Entity_Player* player, int cacheFlag)
|
||||
{
|
||||
callback_notify(*this, {"PostEvaluateItems", MC_EVALUATE_CACHE, cacheFlag}, LuaObject{player, LUA_CLASS_PLAYER},
|
||||
cacheFlag);
|
||||
}
|
||||
|
||||
// 00865ad0 Runs MC_GET_PILL_EFFECT for the effect a pill color got. The effect a mod chose, -1 to keep it.
|
||||
// The original aborts on a result that is not an integer. This keeps the effect instead.
|
||||
int LuaEngine::GetPillEffect(int pillEffect, int pillColor)
|
||||
{
|
||||
CallbackRun run = {"GetPillEffect", MC_GET_PILL_EFFECT};
|
||||
if (!callback_run(*this, run, pillEffect, pillColor)) return PILL_EFFECT_UNCHANGED;
|
||||
|
||||
int isInteger = 0;
|
||||
lua_Integer effect = lua_tointegerx(state, -1, &isInteger);
|
||||
lua_pop(state, 1);
|
||||
return isInteger ? (int)effect : PILL_EFFECT_UNCHANGED;
|
||||
}
|
||||
|
||||
// 00866140 Runs MC_POST_ENTITY_KILL, filtered by the entity's type.
|
||||
void LuaEngine::PostEntityKill(Entity* entity)
|
||||
{
|
||||
callback_notify(*this, {"PostEntityKill", MC_POST_ENTITY_KILL, (int)entity->type},
|
||||
LuaObject{entity, LUA_CLASS_ENTITY});
|
||||
}
|
||||
|
||||
// 00864ee0 Runs MC_POST_GAME_STARTED with whether the run continues a saved one.
|
||||
void LuaEngine::PostGameStart(bool isContinued)
|
||||
{
|
||||
callback_notify(*this, {"PostGameStart", MC_POST_GAME_STARTED}, isContinued);
|
||||
}
|
||||
|
||||
// 00861890 Runs MC_POST_KNIFE_INIT, filtered by the knife's subtype.
|
||||
void LuaEngine::PostKnifeInit(Entity_Knife* knife)
|
||||
{
|
||||
callback_notify(*this, {"PostKnifeInit", MC_POST_KNIFE_INIT, (int)knife->subtype},
|
||||
LuaObject{knife, LUA_CLASS_KNIFE});
|
||||
}
|
||||
|
||||
// 00865260 Runs MC_POST_NEW_LEVEL once a floor is generated and its first room entered.
|
||||
void LuaEngine::PostNewLevel()
|
||||
{
|
||||
callback_notify(*this, {"PostNewLevel", MC_POST_NEW_LEVEL});
|
||||
}
|
||||
|
||||
// 00865180 Runs MC_POST_NEW_ROOM once a room is loaded.
|
||||
void LuaEngine::PostNewRoom()
|
||||
{
|
||||
callback_notify(*this, {"PostNewRoom", MC_POST_NEW_ROOM});
|
||||
}
|
||||
|
||||
// 00864050 Runs MC_POST_PLAYER_INIT, filtered by the player's variant.
|
||||
void LuaEngine::PostPlayerInit(Entity_Player* player)
|
||||
{
|
||||
callback_notify(*this, {"PostPlayerInit", MC_POST_PLAYER_INIT, (int)player->variant},
|
||||
LuaObject{player, LUA_CLASS_PLAYER});
|
||||
}
|
||||
|
||||
// 00863680 Runs MC_POST_RENDER after the game renders.
|
||||
void LuaEngine::PostRender()
|
||||
{
|
||||
callback_notify(*this, {"PostRender", MC_POST_RENDER});
|
||||
}
|
||||
|
||||
// 008638d0 Runs MC_USE_ITEM for an active item, filtered by its type. A table result sets Discharge, Remove and
|
||||
// ShowAnim, any other result ShowAnim alone. Whether to show the use animation, false without a result.
|
||||
bool LuaEngine::UseItem(LuaItemUse& use)
|
||||
{
|
||||
CallbackRun run = {"PostUseItem", MC_USE_ITEM, use.collectibleType};
|
||||
if (!callback_run(*this, run, use.collectibleType, LuaObject{use.rng, LUA_CLASS_RNG},
|
||||
LuaObject{use.player, LUA_CLASS_PLAYER}, use.useFlags, use.activeSlot, use.customVarData))
|
||||
return false;
|
||||
|
||||
bool isShowAnimation = false;
|
||||
if (!lua_istable(state, -1)) isShowAnimation = lua_toboolean(state, -1);
|
||||
else
|
||||
{
|
||||
field_read(state, "Discharge", use.isDischarge);
|
||||
field_read(state, "Remove", use.isRemove);
|
||||
field_read(state, "ShowAnim", isShowAnimation);
|
||||
}
|
||||
lua_pop(state, 1);
|
||||
return isShowAnimation;
|
||||
}
|
||||
|
||||
// 00863da0 Runs MC_USE_PILL for a pill effect, filtered by it.
|
||||
void LuaEngine::UsePill(int pillEffect, Entity_Player* player, unsigned int useFlags)
|
||||
{
|
||||
callback_notify(*this, {"PostUsePillEffect", MC_USE_PILL, pillEffect}, pillEffect,
|
||||
LuaObject{player, LUA_CLASS_PLAYER}, useFlags);
|
||||
}
|
||||
Reference in new issue
Block a user