Skip to content

Commit

Permalink
Misc API Enhancements (#5975)
Browse files Browse the repository at this point in the history
- Write map tags array in `wl_map_info`
- Add function `LuaWorker::evict`
- Add property `LuaMapObject.exists`
- Expose worker buildcost to `modify_unit`
  • Loading branch information
Noordfrees committed Jul 13, 2023
1 parent eec3c3d commit 10b692a
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 4 deletions.
3 changes: 3 additions & 0 deletions src/logic/map_objects/tribes/worker_descr.h
Expand Up @@ -58,6 +58,9 @@ class WorkerDescr : public BobDescr {
assert(is_buildable());
return buildcost_;
}
[[nodiscard]] Buildcost& mutable_buildcost() {
return buildcost_;
}

[[nodiscard]] const Vector2i& ware_hotspot() const {
return ware_hotspot_;
Expand Down
34 changes: 30 additions & 4 deletions src/scripting/lua_map.cc
Expand Up @@ -4585,10 +4585,8 @@ const MethodType<LuaMapObject> LuaMapObject::Methods[] = {
{nullptr, nullptr},
};
const PropertyType<LuaMapObject> LuaMapObject::Properties[] = {
PROP_RO(LuaMapObject, __hash),
PROP_RO(LuaMapObject, descr),
PROP_RO(LuaMapObject, serial),
{nullptr, nullptr, nullptr},
PROP_RO(LuaMapObject, __hash), PROP_RO(LuaMapObject, descr), PROP_RO(LuaMapObject, serial),
PROP_RO(LuaMapObject, exists), {nullptr, nullptr, nullptr},
};

void LuaMapObject::__persist(lua_State* L) {
Expand Down Expand Up @@ -4664,6 +4662,20 @@ int LuaMapObject::get_descr(lua_State* L) {
return upcasted_map_object_descr_to_lua(L, desc);
}

/* RST
.. attribute:: exists
.. versionadded:: 1.2
(RO) Whether the map object represented by this Lua object still exists.
If it does not exist, no other attributes or functions of this object may be accessed.
*/
int LuaMapObject::get_exists(lua_State* L) {
lua_pushboolean(L, static_cast<int>(get_or_zero(get_egbase(L)) != nullptr));
return 1;
}

/*
==========================================================
LUA METHODS
Expand Down Expand Up @@ -7491,6 +7503,7 @@ Worker

const char LuaWorker::className[] = "Worker";
const MethodType<LuaWorker> LuaWorker::Methods[] = {
METHOD(LuaWorker, evict),
{nullptr, nullptr},
};
const PropertyType<LuaWorker> LuaWorker::Properties[] = {
Expand Down Expand Up @@ -7539,6 +7552,19 @@ int LuaWorker::get_location(lua_State* L) {
==========================================================
*/

/* RST
.. method:: evict()
.. versionadded:: 1.2
Evict this worker from his current workplace.
*/
int LuaWorker::evict(lua_State* L) {
Widelands::Game& game = get_game(L);
get(L, game)->evict(game);
return 0;
}

/*
==========================================================
C METHODS
Expand Down
2 changes: 2 additions & 0 deletions src/scripting/lua_map.h
Expand Up @@ -877,6 +877,7 @@ class LuaMapObject : public LuaMapModuleClass {
CLANG_DIAG_RESERVED_IDENTIFIER_ON
int get_descr(lua_State*);
int get_serial(lua_State*);
int get_exists(lua_State*);

/*
* Lua Methods
Expand Down Expand Up @@ -1359,6 +1360,7 @@ class LuaWorker : public LuaBob {
/*
* Lua methods
*/
int evict(lua_State* L);

/*
* C methods
Expand Down
14 changes: 14 additions & 0 deletions src/scripting/lua_root.cc
Expand Up @@ -758,6 +758,9 @@ int LuaDescriptions::new_tribe(lua_State* L) {
:const:`"becomes"` **worker_name** (*string*) 1.0
:const:`"programs"`, :const:`"set"` **program_name** (*string*), 1.0
**actions_table** (*table*)
:const:`"buildcost"`, :const:`"set"` **ware_name** (*string*), 1.2
**amount** (*int*)
:const:`"buildcost"`, :const:`"remove"` **ware_name** (*string*), 1.2
============================================ ======================================= =============
.. table:: ``"building"``
Expand Down Expand Up @@ -1106,6 +1109,17 @@ void LuaDescriptions::do_modify_worker(lua_State* L,
} else {
report_error(L, "modify_unit - worker - programs: invalid command '%s'", cmd.c_str());
}
} else if (property == "buildcost") {
const std::string cmd = luaL_checkstring(L, 5);
const std::string item_name = luaL_checkstring(L, 6);
Widelands::WorkerDescr::Buildcost& bc = worker_descr.mutable_buildcost();
if (cmd == "remove") {
bc.erase(item_name);
} else if (cmd == "set") {
bc[item_name] = luaL_checkuint32(L, 7);
} else {
report_error(L, "modify_unit - worker - buildcost: invalid command '%s'", cmd.c_str());
}
} else {
report_error(L, "modify_unit: invalid worker property '%s'", property.c_str());
}
Expand Down
5 changes: 5 additions & 0 deletions src/website/map_info.cc
Expand Up @@ -92,6 +92,11 @@ int main(int argc, char** argv) {
json->add_string(
"minimum_required_widelands_version", map->minimum_required_widelands_version());

JSON::Array* tags_array = json->add_array("tags");
for (const std::string& tag : map->get_tags()) {
tags_array->add_empty(tag);
}

const std::string world_name =
dynamic_cast<Widelands::WidelandsMapLoader*>(ml.get())->old_world_name();
json->add_string("world_name", world_name);
Expand Down
21 changes: 21 additions & 0 deletions test/maps/plain.wmf/scripting/test_casern.lua
Expand Up @@ -86,6 +86,27 @@ run(function()
rv = hq:get_workers("all")
assert_equal(5, rv.barbarians_soldier)

-- Cut the road
fhq.fields[1].rn.immovable:destroy()

-- Test evicting and map object existence checks
assert_equal(1, br:get_workers("barbarians_trainer"))
local main_worker
for i,bob in ipairs(br.fields[1].bobs) do
if bob.descr.name == "barbarians_trainer" then
assert_nil(main_worker)
main_worker = bob
end
end
assert_not_nil(main_worker)
main_worker:evict()
sleep(10000)
assert_equal(0, br:get_workers("barbarians_trainer"))

assert(br.exists)
br:destroy()
assert_equal(false, br.exists)

print("# All Tests passed.")
wl.ui.MapView():close()
end)

0 comments on commit 10b692a

Please sign in to comment.