Skip to content

Commit

Permalink
Feature: Load map spawns by lua (otland#4527)
Browse files Browse the repository at this point in the history
* load map spawns by lua

* fix parameter name

---------

Co-authored-by: AKnopik PC <[email protected]>
  • Loading branch information
ArturKnopik and AKnopik PC authored Nov 24, 2024
1 parent 6f05ea8 commit cf38586
Show file tree
Hide file tree
Showing 8 changed files with 19 additions and 16 deletions.
7 changes: 5 additions & 2 deletions src/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,12 @@ void Game::saveGameState()
}
}

bool Game::loadMainMap(const std::string& filename) { return map.loadMap("data/world/" + filename + ".otbm", true); }
bool Game::loadMainMap(const std::string& filename)
{
return map.loadMap("data/world/" + filename + ".otbm", true, false);
}

void Game::loadMap(const std::string& path) { map.loadMap(path, false); }
void Game::loadMap(const std::string& path, bool isCalledByLua) { map.loadMap(path, false, isCalledByLua); }

Cylinder* Game::internalGetCylinder(Player* player, const Position& pos) const
{
Expand Down
2 changes: 1 addition & 1 deletion src/game.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class Game
void forceRemoveCondition(uint32_t creatureId, ConditionType_t type);

bool loadMainMap(const std::string& filename);
void loadMap(const std::string& path);
void loadMap(const std::string& path, bool isCalledByLua = false);

/**
* Get the map size - info purpose only
Expand Down
4 changes: 2 additions & 2 deletions src/iomap.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,15 +100,15 @@ class IOMap
* \param map pointer to the Map class
* \returns Returns true if the spawns were loaded successfully
*/
static bool loadSpawns(Map* map)
static bool loadSpawns(Map* map, bool isCalledByLua)
{
if (map->spawnfile.empty()) {
// OTBM file doesn't tell us about the spawnfile, lets guess it is mapname-spawn.xml.
map->spawnfile = getString(ConfigManager::MAP_NAME);
map->spawnfile += "-spawn.xml";
}

return map->spawns.loadFromXml(map->spawnfile.string());
return map->spawns.loadFromXml(map->spawnfile.string(), isCalledByLua);
}

/* Load the houses (not house tile-data)
Expand Down
2 changes: 1 addition & 1 deletion src/luascript.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4670,7 +4670,7 @@ int LuaScriptInterface::luaGameLoadMap(lua_State* L)
const std::string& path = tfs::lua::getString(L, 1);
g_dispatcher.addTask([path]() {
try {
g_game.loadMap(path);
g_game.loadMap(path, true);
} catch (const std::exception& e) {
// FIXME: Should only catch some exceptions
std::cout << "[Error - LuaScriptInterface::luaGameLoadMap] Failed to load map: " << e.what() << '\n';
Expand Down
6 changes: 3 additions & 3 deletions src/map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,19 @@

extern Game g_game;

bool Map::loadMap(const std::string& identifier, bool loadHouses)
bool Map::loadMap(const std::string& identifier, bool loadHouses, bool isCalledByLua)
{
IOMap loader;
if (!loader.loadMap(this, identifier)) {
std::cout << "[Fatal - Map::loadMap] " << loader.getLastErrorString() << std::endl;
return false;
}

if (!IOMap::loadSpawns(this)) {
if (!IOMap::loadSpawns(this, isCalledByLua)) {
std::cout << "[Warning - Map::loadMap] Failed to load spawn data." << std::endl;
}

if (loadHouses) {
if (loadHouses && !isCalledByLua) {
if (!IOMap::loadHouses(this)) {
std::cout << "[Warning - Map::loadMap] Failed to load house data." << std::endl;
}
Expand Down
2 changes: 1 addition & 1 deletion src/map.h
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ class Map
* Load a map.
* \returns true if the map was loaded successfully
*/
bool loadMap(const std::string& identifier, bool loadHouses);
bool loadMap(const std::string& identifier, bool loadHouses, bool isCalledByLua = true);

/**
* Save a map.
Expand Down
10 changes: 5 additions & 5 deletions src/spawn.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,8 @@ extern Game g_game;
static constexpr int32_t MINSPAWN_INTERVAL = 10 * 1000; // 10 seconds to match RME
static constexpr int32_t MAXSPAWN_INTERVAL = 24 * 60 * 60 * 1000; // 1 day

bool Spawns::loadFromXml(const std::string& filename)
bool Spawns::loadFromXml(const std::string& filename, bool isCalledByLua)
{
if (loaded) {
return true;
}

pugi::xml_document doc;
pugi::xml_parse_result result = doc.load_file(filename.c_str());
if (!result) {
Expand Down Expand Up @@ -191,6 +187,10 @@ bool Spawns::loadFromXml(const std::string& filename)
npcList.push_front(npc);
}
}

if (isCalledByLua) {
spawn.startup();
}
}
return true;
}
Expand Down
2 changes: 1 addition & 1 deletion src/spawn.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class Spawns
public:
static bool isInZone(const Position& centerPos, int32_t radius, const Position& pos);

bool loadFromXml(const std::string& filename);
bool loadFromXml(const std::string& filename, bool isCalledByLua = false);
void startup();
void clear();

Expand Down

0 comments on commit cf38586

Please sign in to comment.