Skip to content
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

Add Lua binding for Room flags #1261

Merged
merged 2 commits into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions doc/lua/room.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ The Room library provides information about a room in a [level](level.md).
| alternate_room | [Room](room.md) | R | Alternate room or nil if not present |
| cameras_and_sinks | [CameraSink](camera_sink.md)[] | R | Camera/Sinks in the room |
| items | [Item](item.md)[] | R | Items in the room |
| flags | number | R | Room flags |
| level | [Level](level.md) | R | The level that the room is in |
| lights | [Light](light.md)[] | R | Lights in the room |
| number | number | R | Room number |
Expand All @@ -26,3 +27,29 @@ The Room library provides information about a room in a [level](level.md).
| Name | Returns | Parameters | Description |
| ---- | ------- | ---------- | ----------- |
| sector | [Sector](sector.md) | number x, number y | Gets the sector at the x and z indices, 1 based |
| has_flag | boolean | [Flags](#flags) flag | Check whether the flag pattern passed in matches the room flags - uses bitwise and operation |

# Flags

```Room.Flags```

| Name | Value |
| ---- | ----- |
| Water | 0x1 |
| Bit1 | 0x2 |
| Bit2 | 0x4 |
| Outside | 0x8 |
| Bit4 | 0x10 |
| Wind | 0x20 |
| Bit6 | 0x40 |
| Bit7 | 0x80 |
| Quicksand | 0x80 |
| NoLensFlare | 0x80 |
| Caustics | 0x100 |
| WaterReflectivity | 0x200 |
| Bit10 | 0x400 |
| Bit11 | 0x800 |
| Bit12 | 0x1000 |
| Bit13 | 0x2000 |
| Bit14 | 0x4000 |
| Bit15 | 0x8000 |
27 changes: 27 additions & 0 deletions trview.app.tests/Lua/Elements/Lua_RoomTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,33 @@ TEST(Lua_Room, CamerasAndSinks)
ASSERT_EQ(2, lua_tointeger(L, -1));
}

TEST(Lua_Room, Flags)
{
auto room = mock_shared<MockRoom>()->with_flags(static_cast<uint16_t>(IRoom::Flag::Water));

LuaState L;
lua::create_room(L, room);
lua_setglobal(L, "r");

ASSERT_EQ(0, luaL_dostring(L, "return r.flags"));
ASSERT_EQ(LUA_TNUMBER, lua_type(L, -1));
ASSERT_EQ(1, lua_tointeger(L, -1));
}

TEST(Lua_Room, HasFlag)
{
auto room = mock_shared<MockRoom>()->with_flags(static_cast<uint16_t>(IRoom::Flag::Water));

LuaState L;
lua::create_room(L, room);
lua_setglobal(L, "r");
lua::room_register(L);

ASSERT_EQ(0, luaL_dostring(L, "return r:has_flag(Room.Flags.Water)"));
ASSERT_EQ(LUA_TBOOLEAN, lua_type(L, -1));
ASSERT_EQ(true, lua_toboolean(L, -1));
}

TEST(Lua_Room, Items)
{
auto item1 = mock_shared<MockItem>()->with_number(100);
Expand Down
4 changes: 4 additions & 0 deletions trview.app/Elements/IRoom.h
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,10 @@ namespace trview
/// <returns>Whether the room has the flag.</returns>
virtual bool flag(Flag flag) const = 0;
/// <summary>
/// All room flags.
/// </summary>
virtual uint16_t flags() const = 0;
/// <summary>
/// Get the room info. This contains basic raw positional information about the room.
/// </summary>
/// <returns>The room info.</returns>
Expand Down
5 changes: 5 additions & 0 deletions trview.app/Elements/Room.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -932,6 +932,11 @@ namespace trview
{
return _flags & static_cast<uint16_t>(flag);
}

uint16_t Room::flags() const
{
return _flags;
}

float Room::y_bottom() const
{
Expand Down
1 change: 1 addition & 0 deletions trview.app/Elements/Room.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ namespace trview
virtual bool quicksand() const override;
virtual std::weak_ptr<ITrigger> trigger_at(int32_t x, int32_t z) const override;
virtual bool flag(Flag flag) const override;
uint16_t flags() const override;
virtual float y_bottom() const override;
virtual float y_top() const override;
virtual ISector::Portal sector_portal(int x1, int y1, int x2, int z2) const override;
Expand Down
48 changes: 48 additions & 0 deletions trview.app/Lua/Elements/Room/Lua_Room.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,17 @@ namespace trview
return create_sector(L, room->sector(x, z).lock());
}

int room_hasflag(lua_State* L)
{
auto room = lua::get_self<IRoom>(L);

luaL_checktype(L, -1, LUA_TNUMBER);
long long flags = lua_tointeger(L, -1);

lua_pushboolean(L, static_cast<long long>(room->flags()) & flags);
return 1;
}

int room_index(lua_State* L)
{
auto room = lua::get_self<IRoom>(L);
Expand Down Expand Up @@ -52,6 +63,16 @@ namespace trview
{
return push_list_p(L, room->camera_sinks(), create_camera_sink);
}
else if (key == "flags")
{
lua_pushinteger(L, room->flags());
return 1;
}
else if (key == "has_flag")
{
lua_pushcfunction(L, room_hasflag);
return 1;
}
else if (key == "items")
{
return push_list_p(L, room->items(), create_item);
Expand Down Expand Up @@ -132,6 +153,33 @@ namespace trview
return create(L, room, room_index, room_newindex);
}

void room_register(lua_State* L)
{
lua_newtable(L);
create_enum<IRoom::Flag>(L, "Flags",
{
{ "Water", IRoom::Flag::Water },
{ "Bit1", IRoom::Flag::Bit1 },
{ "Bit2", IRoom::Flag::Bit2 },
{ "Outside", IRoom::Flag::Outside },
{ "Bit4", IRoom::Flag::Bit4 },
{ "Wind", IRoom::Flag::Wind },
{ "Bit6", IRoom::Flag::Bit6 },
{ "Bit7", IRoom::Flag::Bit7 },
{ "Quicksand", IRoom::Flag::Quicksand },
{ "NoLensFlare", IRoom::Flag::NoLensFlare },
{ "Caustics", IRoom::Flag::Caustics },
{ "WaterReflectivity", IRoom::Flag::WaterReflectivity },
{ "Bit10", IRoom::Flag::Bit10 },
{ "Bit11", IRoom::Flag::Bit11 },
{ "Bit12", IRoom::Flag::Bit12 },
{ "Bit13", IRoom::Flag::Bit13 },
{ "Bit14", IRoom::Flag::Bit14 },
{ "Bit15", IRoom::Flag::Bit15 }
});
lua_setglobal(L, "Room");
}

std::shared_ptr<IRoom> to_room(lua_State* L, int index)
{
return get_self<IRoom>(L, index);
Expand Down
1 change: 1 addition & 0 deletions trview.app/Lua/Elements/Room/Lua_Room.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ namespace trview
{
namespace lua
{
void room_register(lua_State* L);
int create_room(lua_State* L, std::shared_ptr<IRoom> room);
std::shared_ptr<IRoom> to_room(lua_State* L, int index);
std::shared_ptr<IRoom> to_room(lua_State* L, int index, const std::string& field_name);
Expand Down
2 changes: 2 additions & 0 deletions trview.app/Lua/trview/trview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "../../Application.h"
#include <trlevel/LevelEncryptedException.h>
#include "../../UserCancelledException.h"
#include "../Elements/Room/Lua_Room.h"
#include "../Elements/Sector/Lua_Sector.h"
#include "../Route/Lua_Route.h"
#include "../Route/Lua_Waypoint.h"
Expand Down Expand Up @@ -147,6 +148,7 @@ namespace trview
lua_setmetatable(L, -2);
lua_setglobal(L, "trview");

room_register(L);
sector_register(L);
route_register(L, route_source, randomizer_route_source, dialogs, files);
waypoint_register(L, waypoint_source);
Expand Down
7 changes: 7 additions & 0 deletions trview.app/Mocks/Elements/IRoom.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ namespace trview
MOCK_METHOD(void, generate_trigger_geometry, (), (override));
MOCK_METHOD(void, get_contained_transparent_triangles, (ITransparencyBuffer&, const ICamera&, SelectionMode, RenderFilter), (override));
MOCK_METHOD(void, get_transparent_triangles, (ITransparencyBuffer&, const ICamera&, SelectionMode, RenderFilter), (override));
MOCK_METHOD(uint16_t, flags, (), (const, override));
MOCK_METHOD(RoomInfo, info, (), (const, override));
MOCK_METHOD(int16_t, light_mode, (), (const, override));
MOCK_METHOD(std::set<uint16_t>, neighbours, (), (const, override));
Expand Down Expand Up @@ -93,6 +94,12 @@ namespace trview
return shared_from_this();
}

std::shared_ptr<MockRoom> with_flags(uint16_t flags)
{
ON_CALL(*this, flags).WillByDefault(testing::Return(flags));
return shared_from_this();
}

std::shared_ptr<MockRoom> with_level(const std::weak_ptr<ILevel>& level)
{
ON_CALL(*this, level).WillByDefault(testing::Return(level));
Expand Down
Loading