Skip to content

Commit

Permalink
small ui fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Dregu committed Dec 5, 2023
1 parent 33cc662 commit e4260e4
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 14 deletions.
6 changes: 6 additions & 0 deletions src/game_api/entities_floors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,10 @@ void Floor::remove_decoration(FLOOR_SIDE side)
if (Entity* deco = find_corner_decoration(side))
{
if (items.contains(deco))
{
deco->color.a = 0;
deco->kill(false, nullptr);
}
}
}
else
Expand All @@ -209,7 +212,10 @@ void Floor::remove_decoration(FLOOR_SIDE side)
if (Entity* deco = get_entity_ptr(decos[side]))
{
if (items.contains(deco))
{
deco->color.a = 0;
deco->kill(false, nullptr);
}
}
decos[side] = -1;

Expand Down
14 changes: 14 additions & 0 deletions src/game_api/entity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,20 @@ void Entity::set_layer(LAYER layer_to)
}
}

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

using AddToLayer = void(Layer*, Entity*);
static AddToLayer* add_to_layer = (AddToLayer*)get_address("add_to_layer");
add_to_layer(ptr_to, this);

for (auto item : items.entities())
{
item->apply_layer();
}
}

void Entity::remove()
{
if (layer != 2)
Expand Down
2 changes: 2 additions & 0 deletions src/game_api/entity.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ class Entity

/// Moves the entity to specified layer, nothing else happens, so this does not emulate a door transition
void set_layer(LAYER layer);
/// Adds the entity to its own layer, to add it to entity lookup tables without waiting for a state update
void apply_layer();
/// Moves the entity to the limbo-layer where it can later be retrieved from again via `respawn`
void remove();
/// Moves the entity from the limbo-layer (where it was previously put by `remove`) to `layer`
Expand Down
6 changes: 6 additions & 0 deletions src/game_api/script/lua_vm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1925,6 +1925,12 @@ end
return fmt::format("{:X}", get_virtual_function_address(offset, index));
};

/// Get memory address from a lua object
lua["get_address"] = [&lua](sol::object o)
{
return fmt::format("{:X}", *(size_t*)lua_touserdata(lua, 1));
};

/// Log to spelunky.log
lua["log_print"] = game_log;

Expand Down
2 changes: 2 additions & 0 deletions src/game_api/script/usertypes/entity_lua.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@ void register_usertypes(sol::state& lua)
entity_type["liberate_from_shop"] = &Entity::liberate_from_shop;
entity_type["get_held_entity"] = &Entity::get_held_entity;
entity_type["set_layer"] = &Entity::set_layer;
entity_type["apply_layer"] = &Entity::apply_layer;
entity_type["remove"] = &Entity::remove;
entity_type["respawn"] = &Entity::respawn;
entity_type["kill"] = &Entity::kill;
Expand All @@ -285,6 +286,7 @@ void register_usertypes(sol::state& lua)
entity_type["is_cursed"] = &Entity::is_cursed;
entity_type["kill_recursive"] = kill_recursive;
entity_type["destroy_recursive"] = destroy_recursive;
entity_type["update"] = &Entity::handle_state_machine;
/* Entity
// user_data
// You can put any arbitrary lua object here for custom entities or player stats, which is then saved across level transitions for players and carried items, mounts etc... This field is local to the script and multiple scripts can write different things in the same entity. The data is saved right before ON.PRE_LOAD_SCREEN from a level and loaded right before ON.POST_LOAD_SCREEN to a level or transition. It is not available yet in post_entity_spawn, but that is a good place to initialize it for new custom entities. See example for more.
Expand Down
25 changes: 11 additions & 14 deletions src/injected/ui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1305,6 +1305,9 @@ void smart_delete(Entity* ent, bool unsafe = false)
{
static auto first_door = to_id("ENT_TYPE_FLOOR_DOOR_ENTRANCE");
static auto logical_door = to_id("ENT_TYPE_LOGICAL_DOOR");
ent->flags = set_flag(ent->flags, 1);
for (auto item : ent->items.entities())
item->flags = set_flag(item->flags, 1);
UI::safe_destroy(ent, unsafe);
if ((ent->type->id >= first_door && ent->type->id <= first_door + 15) || ent->type->id == logical_door)
{
Expand Down Expand Up @@ -1401,6 +1404,7 @@ int32_t spawn_entityitem(EntityItem to_spawn, bool s, bool set_last = true)
if (to_spawn.name.find("ENT_TYPE_CHAR") != std::string::npos)
{
int spawned = UI::spawn_companion(to_spawn.id, cpos.first, cpos.second, LAYER::PLAYER, g_vx, g_vy);
get_entity_ptr(spawned)->apply_layer();
if (!lock_entity && set_last && options["draw_hitboxes"])
g_last_id = spawned;
return spawned;
Expand All @@ -1414,7 +1418,7 @@ int32_t spawn_entityitem(EntityItem to_spawn, bool s, bool set_last = true)
{
static const auto ana_spelunky = to_id("ENT_TYPE_CHAR_ANA_SPELUNKY");
auto spawned = UI::spawn_playerghost(ana_spelunky + (rand() % 19), cpos.first, cpos.second, LAYER::PLAYER, g_vx, g_vy);

get_entity_ptr(spawned)->apply_layer();
if (!lock_entity && set_last && options["draw_hitboxes"])
g_last_id = spawned;
return spawned;
Expand Down Expand Up @@ -1448,6 +1452,7 @@ int32_t spawn_entityitem(EntityItem to_spawn, bool s, bool set_last = true)
}
}
int spawned = UI::spawn_entity(to_spawn.id, g_x, g_y, s, g_vx, g_vy, snap);
get_entity_ptr(spawned)->apply_layer();
if (to_spawn.name.find("ENT_TYPE_MOUNT") != std::string::npos)
{
auto mount = get_entity_ptr(spawned)->as<Mount>();
Expand All @@ -1463,18 +1468,10 @@ int32_t spawn_entityitem(EntityItem to_spawn, bool s, bool set_last = true)
for (auto trig : floor->items.entities())
trig->flags |= (1U << 16);
}

if (floor->get_decoration_entity_type() != -1)
{
floor->fix_decorations(true, false);
}
auto fpos = floor->position();
auto layer = (LAYER)floor->layer;
Callback cb = {g_state->time_total + 2, [fpos, layer]
{
fix_decorations_at(fpos.first, fpos.second, layer);
}};
callbacks.push_back(cb);
uint32_t i_x = static_cast<uint32_t>(floor->x + 0.5f);
uint32_t i_y = static_cast<uint32_t>(floor->y + 0.5f);
State::get().layer(floor->layer)->grid_entities[i_y][i_x] = floor;
fix_decorations_at(floor->x, floor->y, (LAYER)floor->layer);
}
}
if (flip)
Expand Down Expand Up @@ -5099,7 +5096,7 @@ void render_clickhandler()
{
g_bucket->overlunky->hovered_uid = -1;
}
if (options["draw_entity_tooltip"])
if (options["draw_entity_tooltip"] && ImGui::IsWindowHovered())
{
ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, {4.0f, 4.0f});
tooltip(coords.c_str(), true);
Expand Down
9 changes: 9 additions & 0 deletions src/injected/ui_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -435,21 +435,30 @@ void UI::update_floor_at(float x, float y, LAYER l)
{
auto deco_ent = get_entity_ptr(floor->decos[i]);
if (deco_ent)
{
deco_ent->color.a = 0;
deco_ent->destroy();
}
floor->decos[i] = -1;
}
}
for (auto deco : entity_get_items_by(floor->uid, destroy_deco, 0x200))
{
auto deco_ent = get_entity_ptr(deco);
if (deco_ent)
{
deco_ent->color.a = 0;
deco_ent->destroy();
}
}
for (auto deco : get_entities_at(destroy_deco, 0, x, y, l, 0.5f))
{
auto deco_ent = get_entity_ptr(deco);
if (deco_ent)
{
deco_ent->color.a = 0;
deco_ent->destroy();
}
}
if (test_flag(floor->type->properties_flags, 1) || test_flag(floor->type->properties_flags, 2))
{
Expand Down

0 comments on commit e4260e4

Please sign in to comment.