Skip to content

Commit

Permalink
Lua: add support for Location to Tracker:FindObjectForCode
Browse files Browse the repository at this point in the history
  • Loading branch information
black-sliver committed Nov 28, 2023
1 parent 7ef3025 commit 63f3829
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 10 deletions.
10 changes: 10 additions & 0 deletions doc/PACKS.md
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,10 @@ a table representing an enum with the following constants: \
* `.AccessibilityLevel`: read-only, giving one of the AccssibilityLevel constants


### type Location
* `.AccessibilityLevel`: read-only, giving one of the AccssibilityLevel constants (will not give CLEARED at the moment), since 0.25.5


## JSON

### Items
Expand Down Expand Up @@ -429,12 +433,18 @@ Sections can be addressed from Lua with `Tracker:FindObjectForCode("@location_na
**Tracker Lua Interface:**

* `Tracker:FindObjectForCode('@location_name/section_name')` returns a Section or nil
* `Tracker:FindObjectForCode('@location_name')` returns a Location or nil

**Section Lua Interface:**

* `.Owner`: probably points to location, but we return an empty table at the moment. `.Owner.ModifiedByUser` is `nil`.
* `.ChestCount`: read how many chests are in the section
* `.AvailableChestCount`: read/write how many chests are NOT checked
* `.AccessibilityLevel`: read-only, giving one of the AccssibilityLevel constants

**Location Lua Interface:**

* `.AccessibilityLevel`: read-only, giving one of the AccssibilityLevel constants, since 0.25.5

**Future:**
We probably want to add a different (additional) interface to Pop for this:
Expand Down
23 changes: 23 additions & 0 deletions src/core/location.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using nlohmann::json;

const LuaInterface<LocationSection>::MethodMap LocationSection::Lua_Methods = {};
const LuaInterface<Location>::MethodMap Location::Lua_Methods = {};

int LocationSection::Lua_Index(lua_State *L, const char *key)
{
Expand Down Expand Up @@ -62,6 +63,28 @@ bool LocationSection::Lua_NewIndex(lua_State *L, const char *key)
return false;
}

int Location::Lua_Index(lua_State *L, const char *key)
{
if (strcmp(key, "Owner") == 0) {
lua_newtable(L); // dummy
return 1;
} else if (strcmp(key, "AccessibilityLevel") == 0) {
lua_getglobal(L, "Tracker");
Tracker* tracker = Tracker::luaL_testthis(L, -1);
if (!tracker) return 0;
int res = (int)tracker->isReachable(*this);
// NOTE: we don't support AccessibilityLevel::CLEARED for Locations yet
lua_pushinteger(L, res);
return 1;
}
return 0;
}

bool Location::Lua_NewIndex(lua_State *L, const char *key)
{
return false;
}


static bool parseRule(const json& v, std::list<std::string>& rule,
const char* nodeType, const char* ruleType, const std::string& name)
Expand Down
9 changes: 8 additions & 1 deletion src/core/location.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ class LocationSection final : public LuaInterface<LocationSection> {
virtual bool Lua_NewIndex(lua_State *L, const char *key) override;
};

class Location final {
class Location final : public LuaInterface<Location> {
friend class LuaInterface;
public:
class MapLocation final {
public:
Expand Down Expand Up @@ -123,6 +124,12 @@ class Location final {
#ifndef NDEBUG
void dump(bool compact=false);
#endif

protected: // lua interface
static constexpr const char Lua_Name[] = "Location";
static const LuaInterface::MethodMap Lua_Methods;
virtual int Lua_Index(lua_State *L, const char *key) override;
virtual bool Lua_NewIndex(lua_State *L, const char *key) override;
};

#endif // _CORE_LOCATION_H
23 changes: 15 additions & 8 deletions src/core/tracker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -402,17 +402,24 @@ Tracker::Object Tracker::FindObjectForCode(const char* code)

}
}
for (auto& item : _jsonItems) {
if (item.canProvideCode(code)) {
return &item;
if (*code == '@') { // location (not section)
const char *start = code+1;
auto& loc = getLocation(start, true);
if (!loc.getID().empty())
return &loc;
} else {
for (auto& item : _jsonItems) {
if (item.canProvideCode(code)) {
return &item;
}
}
}
for (auto& item : _luaItems) {
if (item.canProvideCode(code)) {
return &item;
for (auto& item : _luaItems) {
if (item.canProvideCode(code)) {
return &item;
}
}
}
printf("Did not find object for code \"%s\".\n", code);
printf("Did not find object for code \"%s\".\n", sanitize_print(code).c_str());
return nullptr;
}

Expand Down
5 changes: 4 additions & 1 deletion src/core/tracker.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,23 @@ class Tracker final : public LuaInterface<Tracker> {

struct Object final : public LuaType {
// NOTE: we could use (something like) std::variant<...> ?
enum class RT { NIL, JsonItem, LuaItem, Section } type;
enum class RT { NIL, JsonItem, LuaItem, Section, Location } type;
union {
JsonItem *jsonItem;
LuaItem *luaItem;
LocationSection *section;
Location *location;
};
Object (std::nullptr_t val) : type(RT::NIL) {}
Object (JsonItem *val) : type(RT::JsonItem), jsonItem(val) {}
Object (LuaItem *val) : type(RT::LuaItem), luaItem(val) {}
Object (LocationSection *val) : type(RT::Section), section(val) {}
Object (Location *val) : type(RT::Location), location(val) {}
virtual void Lua_Push(lua_State *L) const { // pushes instance to Lua stack
if (type == RT::JsonItem) jsonItem->Lua_Push(L);
else if (type == RT::LuaItem) luaItem->Lua_Push(L);
else if (type == RT::Section) section->Lua_Push(L);
else if (type == RT::Location) location->Lua_Push(L);
else lua_pushnil(L);
}
};
Expand Down
1 change: 1 addition & 0 deletions src/poptracker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1050,6 +1050,7 @@ bool PopTracker::loadTracker(const std::string& pack, const std::string& variant
LuaItem::Lua_Register(_L); // TODO: move this to Tracker or ScriptHost
JsonItem::Lua_Register(_L); // ^
LocationSection::Lua_Register(_L); // ^
Location::Lua_Register(_L); // ^

ImageReference::Lua_Register(_L);
_imageReference.Lua_Push(_L);
Expand Down

0 comments on commit 63f3829

Please sign in to comment.