summaryrefslogtreecommitdiff
path: root/src/game-server/mapreader.cpp
diff options
context:
space:
mode:
authorErik Schilling <ablu.erikschilling@googlemail.com>2011-10-08 11:19:44 +0200
committerThorbjørn Lindeijer <thorbjorn@lindeijer.nl>2011-10-15 00:10:22 +0200
commit1e6b7a30b7232dce7b8240441bed04912e06f666 (patch)
treeb5aa2d893c2bee4268a3cfc8172dc6ae7ea6ce09 /src/game-server/mapreader.cpp
parent9cd09f012ff7c01fbd51b972f9b716cfc8246b2c (diff)
downloadmanaserv-1e6b7a30b7232dce7b8240441bed04912e06f666.tar.gz
manaserv-1e6b7a30b7232dce7b8240441bed04912e06f666.tar.bz2
manaserv-1e6b7a30b7232dce7b8240441bed04912e06f666.tar.xz
manaserv-1e6b7a30b7232dce7b8240441bed04912e06f666.zip
Added script bindings for reading map objects
map_get_objects([string filter]): returns all object of the current map optionally filtered by type. map_get_object_property(handle object, string key): returns the value of the property of the object. map_get_object_bounds(handle object): returns x, y, width, height of an object. map_get_object_name(handle object): returns name of an object. map_get_object_type(handle object): returns type of an object. Mantis-issue: 397 Reviewed-by: Thorbjørn Lindeijer
Diffstat (limited to 'src/game-server/mapreader.cpp')
-rw-r--r--src/game-server/mapreader.cpp208
1 files changed, 57 insertions, 151 deletions
diff --git a/src/game-server/mapreader.cpp b/src/game-server/mapreader.cpp
index 8438c82c..e82fa10a 100644
--- a/src/game-server/mapreader.cpp
+++ b/src/game-server/mapreader.cpp
@@ -141,54 +141,44 @@ Map* MapReader::readMap(xmlNodePtr node, const std::string &path,
int objH = XML::getProperty(objectNode, "height", 0);
Rectangle rect = { objX, objY, objW, objH };
+ MapObject *newObject = new MapObject(rect, objName, objType);
- if (utils::compareStrI(objType, "WARP") == 0)
+ for_each_xml_child_node(propertiesNode, objectNode)
{
- std::string destMapName = std::string();
- int destX = -1;
- int destY = -1;
-
- for_each_xml_child_node(propertiesNode, objectNode)
+ if (!xmlStrEqual(propertiesNode->name, BAD_CAST "properties"))
{
- if (!xmlStrEqual(propertiesNode->name, BAD_CAST "properties"))
- {
- continue;
- }
-
- for_each_xml_child_node(propertyNode, propertiesNode)
+ continue;
+ }
+
+ for_each_xml_child_node(propertyNode, propertiesNode)
+ {
+ if (xmlStrEqual(propertyNode->name, BAD_CAST "property"))
{
- if (xmlStrEqual(propertyNode->name,
- BAD_CAST "property"))
- {
- std::string value = XML::getProperty(
- propertyNode, "name", std::string());
- value = utils::toUpper(value);
- if (utils::compareStrI(value, "DEST_MAP") == 0)
- {
- destMapName = getObjectProperty(propertyNode,
+ std::string key = XML::getProperty(
+ propertyNode, "name", std::string());
+ std::string value = getObjectProperty(propertyNode,
std::string());
- }
- else if (utils::compareStrI(value, "DEST_X") == 0)
- {
- destX = getObjectProperty(propertyNode, -1);
- }
- else if (utils::compareStrI(value, "DEST_Y") == 0)
- {
- destY = getObjectProperty(propertyNode, -1);
- }
- }
+ newObject->addProperty(key, value);
}
}
+ }
- if (!destMapName.empty() && destX != -1 && destY != -1)
+ if (utils::compareStrI(objType, "WARP") == 0)
+ {
+ std::string destMapName = newObject->getProperty("DEST_MAP");
+ int destX = utils::stringToInt(
+ newObject->getProperty("DEST_X"));
+ int destY = utils::stringToInt(
+ newObject->getProperty("DEST_Y"));
+
+ if (!destMapName.empty() && destX && destY)
{
MapComposite *destMap = MapManager::getMap(destMapName);
if (destMap)
{
things.push_back(new TriggerArea(
composite, rect,
- new WarpAction(destMap, destX, destY),
- false));
+ new WarpAction(destMap, destX, destY), false));
}
}
else
@@ -199,71 +189,36 @@ Map* MapReader::readMap(xmlNodePtr node, const std::string &path,
else if (utils::compareStrI(objType, "SPAWN") == 0)
{
MonsterClass *monster = 0;
- int maxBeings = 10; // Default value
- int spawnRate = 10; // Default value
-
- for_each_xml_child_node(propertiesNode, objectNode)
+ int maxBeings = utils::stringToInt(
+ newObject->getProperty("MAX_BEINGS"));
+ int spawnRate = utils::stringToInt(
+ newObject->getProperty("SPAWN_RATE"));
+ std::string monsterName =
+ newObject->getProperty("MONSTER_ID");
+ int monsterId = utils::stringToInt(monsterName);
+ if (monsterId)
{
- if (!xmlStrEqual(propertiesNode->name, BAD_CAST "properties"))
+ monster = monsterManager->getMonster(
+ monsterId);
+ if (!monster)
{
- continue;
+ LOG_WARN("Couldn't find monster ID "
+ << monsterId <<
+ " for spawn area");
}
-
- for_each_xml_child_node(propertyNode, propertiesNode)
+ }
+ else
+ {
+ monster = monsterManager->
+ getMonsterByName(monsterName);
+ if (!monster)
{
- if (xmlStrEqual(propertyNode->name, BAD_CAST "property"))
- {
- std::string value = XML::getProperty(
- propertyNode,
- "name",
- std::string());
- value = utils::toUpper(value);
- if (utils::compareStrI(value, "MONSTER_ID") == 0)
- {
- std::string monsterName =
- getObjectProperty(propertyNode,
- std::string());
- int monsterId = utils::stringToInt(monsterName);
- if (monsterId)
- {
- monster = monsterManager->getMonster(
- monsterId);
- if (!monster)
- {
- LOG_WARN("Couldn't find monster ID "
- << monsterId <<
- " for spawn area");
- }
- }
- else
- {
- monster = monsterManager->
- getMonsterByName(monsterName);
- if (!monster)
- {
- LOG_WARN("Couldn't find monster "
- << monsterName <<
- " for spawn area");
- }
- }
- }
- else if (utils::compareStrI(value,
- "MAX_BEINGS") == 0)
- {
- maxBeings = getObjectProperty(propertyNode,
- maxBeings);
- }
- else if (utils::compareStrI(value,
- "SPAWN_RATE") == 0)
- {
- spawnRate = getObjectProperty(propertyNode,
- spawnRate);
- }
- }
+ LOG_WARN("Couldn't find monster "
+ << monsterName <<
+ " for spawn area");
}
}
-
- if (monster)
+ if (monster && maxBeings && spawnRate)
{
things.push_back(new SpawnArea(composite, monster, rect,
maxBeings, spawnRate));
@@ -279,35 +234,11 @@ Map* MapReader::readMap(xmlNodePtr node, const std::string &path,
composite->setScript(s);
}
- int npcId = -1;
- std::string scriptText;
+ int npcId = utils::stringToInt(
+ newObject->getProperty("NPC_ID"));
+ std::string scriptText = newObject->getProperty("SCRIPT");
- for_each_xml_child_node(propertiesNode, objectNode)
- {
- if (!xmlStrEqual(propertiesNode->name, BAD_CAST "properties"))
- {
- continue;
- }
-
- for_each_xml_child_node(propertyNode, propertiesNode)
- {
- if (xmlStrEqual(propertyNode->name, BAD_CAST "property"))
- {
- std::string value = XML::getProperty(propertyNode, "name", std::string());
- value = utils::toUpper(value);
- if (utils::compareStrI(value, "NPC_ID") == 0)
- {
- npcId = getObjectProperty(propertyNode, npcId);
- }
- else if (utils::compareStrI(value, "SCRIPT") == 0)
- {
- scriptText = getObjectProperty(propertyNode, std::string());
- }
- }
- }
- }
-
- if (npcId != -1 && !scriptText.empty())
+ if (npcId && !scriptText.empty())
{
s->loadNPC(objName, npcId, objX, objY, scriptText.c_str());
}
@@ -326,36 +257,9 @@ Map* MapReader::readMap(xmlNodePtr node, const std::string &path,
composite->setScript(s);
}
- std::string scriptFilename;
- std::string scriptText;
-
- for_each_xml_child_node(propertiesNode, objectNode)
- {
- if (!xmlStrEqual(propertiesNode->name, BAD_CAST "properties"))
- {
- continue;
- }
-
- for_each_xml_child_node(propertyNode, propertiesNode)
- {
- if (xmlStrEqual(propertyNode->name, BAD_CAST "property"))
- {
- std::string value = XML::getProperty(propertyNode, "name",
- std::string());
- value = utils::toUpper(value);
- if (utils::compareStrI(value, "FILENAME") == 0)
- {
- scriptFilename = getObjectProperty(propertyNode,
- std::string());
- utils::trim(scriptFilename);
- }
- else if (utils::compareStrI(value, "TEXT") == 0)
- {
- scriptText = getObjectProperty(propertyNode, "");
- }
- }
- }
- }
+ std::string scriptFilename =
+ newObject->getProperty("FILENAME");
+ std::string scriptText = newObject->getProperty("TEXT");
if (!scriptFilename.empty())
{
@@ -371,6 +275,8 @@ Map* MapReader::readMap(xmlNodePtr node, const std::string &path,
LOG_WARN("Unrecognized format for script");
}
}
+
+ map->addObject(newObject);
}
}
}