Skip to content

Commit

Permalink
destroy_grid_entity function
Browse files Browse the repository at this point in the history
  • Loading branch information
SereneRuby12 committed Oct 6, 2023
1 parent 9ffff6a commit 15a769f
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 0 deletions.
5 changes: 5 additions & 0 deletions docs/game_data/spel2.lua

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

10 changes: 10 additions & 0 deletions docs/src/includes/_globals.md
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,16 @@ Change [ENT_TYPE](#ENT_TYPE)'s spawned when [Waddler](#Waddler) dies, by default
Max 255 types.
Use empty table as argument to reset to the game default

### destroy_grid_entity


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

Destroy the grid entity, and its item entities, removing them from the grid without dropping particles or gold.
Will also destroy monsters or items that are standing on a linked activefloor or chain, though excludes [MASK](#MASK).PLAYER to prevent crashes

### drop


Expand Down
32 changes: 32 additions & 0 deletions src/game_api/layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -215,3 +215,35 @@ void Layer::move_grid_entity(Entity* ent, uint32_t x, uint32_t y, Layer* dest_la
}
}
}

void Layer::destroy_grid_entity(Entity* ent)
{
if (ent)
{
auto items = ent->items.entities();
for (auto ptr = items.cend(); ptr != items.cbegin();)
{
ptr--;
Entity* item_ent = *ptr;
if (item_ent->type->search_flags & ~1) // if not player
{
destroy_grid_entity(item_ent);
}
}

const auto pos = ent->position();
const uint32_t current_grid_x = static_cast<uint32_t>(std::round(pos.first));
const uint32_t current_grid_y = static_cast<uint32_t>(std::round(pos.second));
if (current_grid_x < g_level_max_x && current_grid_y < g_level_max_y)
{
if (grid_entities[current_grid_y][current_grid_x] == ent)
{
grid_entities[current_grid_y][current_grid_x] = nullptr;
update_liquid_collision_at(pos.first, pos.second, false);
}
}

ent->flags |= 1U << (29 - 1); // set DEAD flag to prevent certain stuff like gold nuggets drop or particles from entities such as spikes
ent->destroy();
}
}
2 changes: 2 additions & 0 deletions src/game_api/layer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -210,4 +210,6 @@ struct Layer

void move_grid_entity(Entity* ent, float x, float y, Layer* dest_layer);
void move_grid_entity(Entity* ent, uint32_t x, uint32_t y, Layer* dest_layer);

void destroy_grid_entity(Entity* ent);
};
9 changes: 9 additions & 0 deletions src/game_api/rpc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1673,6 +1673,15 @@ void move_grid_entity(int32_t uid, float x, float y, LAYER layer)
}
}

void destroy_grid_entity(int32_t uid)
{
if (auto entity = get_entity_ptr(uid))
{
auto state = State::get();
state.layer(entity->layer)->destroy_grid_entity(entity);
}
}

void add_item_to_shop(int32_t item_uid, int32_t shop_owner_uid)
{
Movable* item = get_entity_ptr(item_uid)->as<Movable>();
Expand Down
1 change: 1 addition & 0 deletions src/game_api/rpc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ void change_waddler_drop(std::vector<ENT_TYPE> ent_types);
void poison_entity(int32_t entity_uid);
void modify_ankh_health_gain(uint8_t max_health, uint8_t beat_add_health);
void move_grid_entity(int32_t uid, float x, float y, LAYER layer);
void destroy_grid_entity(int32_t uid);
void add_item_to_shop(int32_t item_uid, int32_t shop_owner_uid);
void change_poison_timer(int16_t frames);
void set_adventure_seed(int64_t first, int64_t second);
Expand Down
3 changes: 3 additions & 0 deletions src/game_api/script/lua_vm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -966,6 +966,9 @@ end
lua["move_entity"] = move_entity_abs;
/// Teleport grid entity, the destination should be whole number, this ensures that the collisions will work properly
lua["move_grid_entity"] = move_grid_entity;
/// Destroy the grid entity, and its item entities, removing them from the grid without dropping particles or gold.
/// Will also destroy monsters or items that are standing on a linked activefloor or chain, though excludes MASK.PLAYER to prevent crashes
lua["destroy_grid_entity"] = destroy_grid_entity;
/// Make an ENT_TYPE.FLOOR_DOOR_EXIT go to world `w`, level `l`, theme `t`
lua["set_door_target"] = set_door_target;
/// Short for [set_door_target](#set_door_target).
Expand Down

0 comments on commit 15a769f

Please sign in to comment.