Skip to content

Commit

Permalink
Merge branch 'main' of github.com:spelunky-fyi/overlunky into main
Browse files Browse the repository at this point in the history
  • Loading branch information
Dregu committed Sep 19, 2021
2 parents fef706a + c92c08c commit 9a53e63
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 3 deletions.
1 change: 1 addition & 0 deletions docs/game_data/spel2.lua

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

8 changes: 7 additions & 1 deletion docs/script-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,9 @@ Get the [EntityDB](#entitydb) behind an ENT_TYPE...
### [`get_grid_entity_at`](https://github.com/spelunky-fyi/overlunky/search?l=Lua&q=get_grid_entity_at)
`int get_grid_entity_at(float x, float y, LAYER layer)`<br/>
Gets a grid entity, such as floor or spikes, at the given position and layer.
### [`filter_entities`](https://github.com/spelunky-fyi/overlunky/search?l=Lua&q=filter_entities)
`array<int> filter_entities(array<int> entities, function predicate)`<br/>
Returns a list of all uids in `entities` for which `predicate(get_entity(uid))` returns true
### [`get_entities_by`](https://github.com/spelunky-fyi/overlunky/search?l=Lua&q=get_entities_by)
`array<int> get_entities_by(array<ENT_TYPE> entity_types, int mask, LAYER layer)`<br/>
Get uids of entities by some conditions. Set `entity_type` or `mask` to `0` to ignore that, can also use table of entity_types
Expand Down Expand Up @@ -3079,11 +3082,14 @@ Copy an axis aligned bounding box
- [`AABB(float left_, float top_, float right_, float bottom_)`](https://github.com/spelunky-fyi/overlunky/search?l=Lua&q=AABB)
\
Create a new axis aligned bounding box by specifying its values
- [`bool overlaps_with(const AABB& other)`](https://github.com/spelunky-fyi/overlunky/search?l=Lua&q=overlaps_with) &AABB::overlaps_with
- [`float left`](https://github.com/spelunky-fyi/overlunky/search?l=Lua&q=left) &AABB::left
- [`float bottom`](https://github.com/spelunky-fyi/overlunky/search?l=Lua&q=bottom) &AABB::bottom
- [`float right`](https://github.com/spelunky-fyi/overlunky/search?l=Lua&q=right) &AABB::right
- [`float top`](https://github.com/spelunky-fyi/overlunky/search?l=Lua&q=top) &AABB::top
- [`bool overlaps_with(const AABB& other)`](https://github.com/spelunky-fyi/overlunky/search?l=Lua&q=overlaps_with) &AABB::overlaps_with
- [`AABB& abs()`](https://github.com/spelunky-fyi/overlunky/search?l=Lua&q=abs) &AABB::abs
\
Fixes the AABB if any of the sides have negative length
- [`AABB& extrude(float amount)`](https://github.com/spelunky-fyi/overlunky/search?l=Lua&q=extrude) &AABB::extrude
\
Grows or shrinks the AABB by the given amount in all directions.
Expand Down
10 changes: 10 additions & 0 deletions src/game_api/math.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@ struct AABB
return !(left == 0.0f && right == 0.0f && top == 0.0f && bottom == 0.0f);
}

/// Fixes the AABB if any of the sides have negative length
AABB& abs()
{
if (left > right)
std::swap(left, right);
if (bottom > top)
std::swap(bottom, top);
return *this;
}

/// Grows or shrinks the AABB by the given amount in all directions.
/// If `amount < 0` and `abs(amount) > right/top - left/bottom` the respective dimension of the AABB will become `0`.
AABB& extrude(float amount)
Expand Down
15 changes: 15 additions & 0 deletions src/game_api/rpc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,21 @@ float screen_distance(float x)
return b.first - a.first;
}

std::vector<uint32_t> filter_entities(std::vector<uint32_t> entities, std::function<bool(Entity*)> predicate)
{
std::vector<uint32_t> filtered_entities{std::move(entities)};
auto filter_fun = [&](uint32_t uid)
{
if (Entity* entity = get_entity_ptr(uid))
{
return !predicate(entity);
}
return false;
};
std::erase_if(filtered_entities, filter_fun);
return filtered_entities;
}

std::vector<uint32_t> get_entities()
{
return get_entities_by({}, 0, LAYER::BOTH);
Expand Down
3 changes: 3 additions & 0 deletions src/game_api/rpc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
#include "entities_chars.hpp"
#include "screen.hpp"
#include "state.hpp"

#include <cstdint>
#include <functional>
#include <vector>

void teleport(float x, float y, bool s, float vx, float vy, bool snap);
Expand Down Expand Up @@ -37,6 +39,7 @@ std::pair<float, float> screen_position(float x, float y);
std::tuple<float, float, float, float> screen_aabb(float x1, float y1, float x2, float y2);
float screen_distance(float x);
float get_zoom_level();
std::vector<uint32_t> filter_entities(std::vector<uint32_t> entities, std::function<bool(Entity*)> predicate);
std::vector<uint32_t> get_entities();
std::vector<uint32_t> get_entities_by(std::vector<ENT_TYPE> entity_types, uint32_t mask, LAYER layer);
std::vector<uint32_t> get_entities_by(ENT_TYPE entity_type, uint32_t mask, LAYER layer);
Expand Down
5 changes: 5 additions & 0 deletions src/game_api/script/lua_vm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,11 @@ end
/// Deprecated
/// Use `get_entities_by(0, MASK.ANY, LAYER.BOTH)` instead
lua["get_entities"] = get_entities;
/// Returns a list of all uids in `entities` for which `predicate(get_entity(uid))` returns true
lua["filter_entities"] = [&lua](std::vector<uint32_t> entities, sol::function predicate) -> std::vector<uint32_t> {
return filter_entities(std::move(entities), [&lua, pred = std::move(predicate)](Entity* entity) -> bool
{ return pred(lua["cast_entity"](entity)); });
};

auto get_entities_by = sol::overload(
static_cast<std::vector<uint32_t> (*)(ENT_TYPE, uint32_t, LAYER)>(::get_entities_by),
Expand Down
6 changes: 4 additions & 2 deletions src/game_api/script/usertypes/hitbox_lua.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,6 @@ void register_usertypes(sol::state& lua)
lua.new_usertype<AABB>(
"AABB",
sol::constructors<AABB(), AABB(const AABB&), AABB(float, float, float, float)>{},
"overlaps_with",
&AABB::overlaps_with,
"left",
&AABB::left,
"bottom",
Expand All @@ -64,6 +62,10 @@ void register_usertypes(sol::state& lua)
&AABB::right,
"top",
&AABB::top,
"overlaps_with",
&AABB::overlaps_with,
"abs",
&AABB::abs,
"extrude",
&AABB::extrude,
"offset",
Expand Down

0 comments on commit 9a53e63

Please sign in to comment.