MapEditor: more changes for ease of use

This commit is contained in:
Allan Ference 2013-08-04 14:21:12 +02:00
parent e6db43ac3a
commit 9aa667da26
12 changed files with 96 additions and 39 deletions

View File

@ -83,10 +83,8 @@ void Spawn::load(TiXmlElement* node)
} }
} }
void Spawn::save(TiXmlElement*& node) void Spawn::save(TiXmlElement* node)
{ {
node = new TiXmlElement("spawn");
const Position& c = getCenterPos(); const Position& c = getCenterPos();
node->SetAttribute("centerx", c.x); node->SetAttribute("centerx", c.x);
node->SetAttribute("centery", c.y); node->SetAttribute("centery", c.y);
@ -260,7 +258,7 @@ void CreatureManager::saveSpawns(const std::string& fileName)
doc.LinkEndChild(root); doc.LinkEndChild(root);
for(auto pair : m_spawns) { for(auto pair : m_spawns) {
TiXmlElement* elem; TiXmlElement* elem = new TiXmlElement("spawn");
pair.second->save(elem); pair.second->save(elem);
root->LinkEndChild(elem); root->LinkEndChild(elem);
} }

View File

@ -61,7 +61,7 @@ public:
protected: protected:
void load(TiXmlElement* node); void load(TiXmlElement* node);
void save(TiXmlElement*& node); void save(TiXmlElement* node);
private: private:
stdext::dynamic_storage<uint8> m_attribs; stdext::dynamic_storage<uint8> m_attribs;

View File

@ -77,8 +77,10 @@ typedef stdext::shared_object_ptr<Spawn> SpawnPtr;
typedef std::vector<ThingPtr> ThingList; typedef std::vector<ThingPtr> ThingList;
typedef std::vector<ThingTypePtr> ThingTypeList; typedef std::vector<ThingTypePtr> ThingTypeList;
typedef std::vector<ItemTypePtr> ItemTypeList; typedef std::vector<ItemTypePtr> ItemTypeList;
typedef std::vector<HousePtr> HouseList; typedef std::list<HousePtr> HouseList;
typedef std::vector<TownPtr> TownList; typedef std::list<TownPtr> TownList;
typedef std::list<ItemPtr> ItemList;
typedef std::vector<ItemPtr> ItemVector;
typedef std::unordered_map<Position, TilePtr, PositionHasher> TileMap; typedef std::unordered_map<Position, TilePtr, PositionHasher> TileMap;
// net // net

View File

@ -28,12 +28,10 @@ HouseManager g_houses;
House::House() House::House()
{ {
m_nullTile = TilePtr(new Tile(Position()));
} }
House::House(uint32 hId, const std::string &name, const Position &pos) House::House(uint32 hId, const std::string &name, const Position &pos)
{ {
m_nullTile = TilePtr(new Tile(Position()));
setId(hId); setId(hId);
setName(name); setName(name);
if(pos.isValid()) if(pos.isValid())
@ -46,12 +44,27 @@ void House::setTile(const TilePtr& tile)
m_tiles.insert(std::make_pair(tile->getPosition(), tile)); m_tiles.insert(std::make_pair(tile->getPosition(), tile));
} }
const TilePtr& House::getTile(const Position& position) TilePtr House::getTile(const Position& position)
{ {
TileMap::const_iterator iter = m_tiles.find(position); TileMap::const_iterator iter = m_tiles.find(position);
if(iter != m_tiles.end()) if(iter != m_tiles.end())
return iter->second; return iter->second;
return m_nullTile; return nullptr;
}
void House::addDoor(const ItemPtr& door)
{
if (!door) return;
door->setDoorId(m_lastDoorId);
m_doors[m_lastDoorId++] = door;
}
void House::removeDoorById(uint32 doorId)
{
if(doorId >= m_lastDoorId)
stdext::throw_exception(stdext::format("Failed to remove door of id %d (would overflow), max id: %d",
doorId, m_lastDoorId));
m_doors[doorId] = nullptr;
} }
void House::load(const TiXmlElement *elem) void House::load(const TiXmlElement *elem)
@ -73,10 +86,8 @@ void House::load(const TiXmlElement *elem)
setEntry(entryPos); setEntry(entryPos);
} }
void House::save(TiXmlElement*& elem) void House::save(TiXmlElement* elem)
{ {
elem = new TiXmlElement("house");
elem->SetAttribute("name", getName()); elem->SetAttribute("name", getName());
elem->SetAttribute("houseid", getId()); elem->SetAttribute("houseid", getId());
@ -93,7 +104,6 @@ void House::save(TiXmlElement*& elem)
HouseManager::HouseManager() HouseManager::HouseManager()
{ {
m_nullHouse = HousePtr(new House);
} }
void HouseManager::addHouse(const HousePtr& house) void HouseManager::addHouse(const HousePtr& house)
@ -109,11 +119,17 @@ void HouseManager::removeHouse(uint32 houseId)
m_houses.erase(it); m_houses.erase(it);
} }
const HousePtr& HouseManager::getHouse(uint32 houseId) HousePtr HouseManager::getHouse(uint32 houseId)
{
auto it = findHouse(houseId);
return it != m_houses.end() ? *it : nullptr;
}
HousePtr HouseManager::getHouseByName(std::string name)
{ {
auto it = std::find_if(m_houses.begin(), m_houses.end(), auto it = std::find_if(m_houses.begin(), m_houses.end(),
[=] (const HousePtr& house) -> bool { return house->getId() == houseId; }); [=] (const HousePtr& house) -> bool { return house->getName() == name; });
return it != m_houses.end() ? *it : m_nullHouse; return it != m_houses.end() ? *it : nullptr;
} }
void HouseManager::load(const std::string& fileName) void HouseManager::load(const std::string& fileName)
@ -152,7 +168,7 @@ void HouseManager::save(const std::string& fileName)
doc.LinkEndChild(root); doc.LinkEndChild(root);
for(auto house : m_houses) { for(auto house : m_houses) {
TiXmlElement *elem; TiXmlElement *elem = new TiXmlElement("house");
house->save(elem); house->save(elem);
root->LinkEndChild(elem); root->LinkEndChild(elem);
} }
@ -161,8 +177,18 @@ void HouseManager::save(const std::string& fileName)
stdext::throw_exception(stdext::format("failed to save houses XML %s: %s", fileName, doc.ErrorDesc())); stdext::throw_exception(stdext::format("failed to save houses XML %s: %s", fileName, doc.ErrorDesc()));
} }
HouseList HouseManager::filterHouses(uint32 townId)
{
HouseList ret;
for(const HousePtr& house : m_houses)
if(house->getTownId() == townId)
ret.push_back(house);
return ret;
}
HouseList::iterator HouseManager::findHouse(uint32 houseId) HouseList::iterator HouseManager::findHouse(uint32 houseId)
{ {
return std::find_if(m_houses.begin(), m_houses.end(), return std::find_if(m_houses.begin(), m_houses.end(),
[=] (const HousePtr& house) -> bool { return house->getId() == houseId; }); [=] (const HousePtr& house) -> bool { return house->getId() == houseId; });
} }

View File

@ -43,10 +43,10 @@ class House : public LuaObject
public: public:
House(); House();
House(uint32 hId, const std::string& name = "", const Position& pos=Position()); House(uint32 hId, const std::string& name = "", const Position& pos=Position());
~House() { m_tiles.clear(); m_nullTile = nullptr; } ~House() { m_tiles.clear(); }
void setTile(const TilePtr& tile); void setTile(const TilePtr& tile);
const TilePtr& getTile(const Position& pos); TilePtr getTile(const Position& pos);
void setName(const std::string& name) { m_attribs.set(HouseAttrName, name); } void setName(const std::string& name) { m_attribs.set(HouseAttrName, name); }
std::string getName() { return m_attribs.get<std::string>(HouseAttrName); } std::string getName() { return m_attribs.get<std::string>(HouseAttrName); }
@ -66,14 +66,19 @@ public:
void setEntry(const Position& p) { m_attribs.set(HouseAttrEntry, p); } void setEntry(const Position& p) { m_attribs.set(HouseAttrEntry, p); }
Position getEntry() { return m_attribs.get<Position>(HouseAttrEntry); } Position getEntry() { return m_attribs.get<Position>(HouseAttrEntry); }
void addDoor(const ItemPtr& door);
void removeDoor(const ItemPtr& door) { removeDoorById(door->getDoorId()); }
void removeDoorById(uint32 doorId);
protected: protected:
void load(const TiXmlElement* elem); void load(const TiXmlElement* elem);
void save(TiXmlElement*& elem); void save(TiXmlElement* elem);
private: private:
stdext::packed_storage<uint8> m_attribs; stdext::packed_storage<uint8> m_attribs;
TileMap m_tiles; TileMap m_tiles;
TilePtr m_nullTile; ItemVector m_doors;
uint32 m_lastDoorId;
stdext::boolean<false> m_isGuildHall; stdext::boolean<false> m_isGuildHall;
friend class HouseManager; friend class HouseManager;
@ -85,16 +90,18 @@ public:
void addHouse(const HousePtr& house); void addHouse(const HousePtr& house);
void removeHouse(uint32 houseId); void removeHouse(uint32 houseId);
HouseList getHouseList() { return m_houses; } HousePtr getHouse(uint32 houseId);
const HousePtr& getHouse(uint32 houseId); HousePtr getHouseByName(std::string name);
void clear() { m_houses.clear(); m_nullHouse = nullptr; }
void load(const std::string& fileName); void load(const std::string& fileName);
void save(const std::string& fileName); void save(const std::string& fileName);
void clear() { m_houses.clear(); }
HouseList getHouseList() { return m_houses; }
HouseList filterHouses(uint32 townId);
private: private:
HouseList m_houses; HouseList m_houses;
HousePtr m_nullHouse;
protected: protected:
HouseList::iterator findHouse(uint32 houseId); HouseList::iterator findHouse(uint32 houseId);

View File

@ -187,6 +187,10 @@ void Item::serializeItem(const OutputBinaryTreePtr& out)
out->addU16(getDepotId()); out->addU16(getDepotId());
} }
if(isHouseDoor()) {
out->addU8(ATTR_HOUSEDOORID);
out->addU8(getDoorId());
}
uint16 aid = m_attribs.get<uint16>(ATTR_ACTION_ID); uint16 aid = m_attribs.get<uint16>(ATTR_ACTION_ID);
uint16 uid = m_attribs.get<uint16>(ATTR_UNIQUE_ID); uint16 uid = m_attribs.get<uint16>(ATTR_UNIQUE_ID);
if(aid) { if(aid) {

View File

@ -97,8 +97,6 @@ public:
uint16 getServerId() { return m_serverId; } uint16 getServerId() { return m_serverId; }
bool isValid(); bool isValid();
ItemPtr clone();
void unserializeItem(const BinaryTreePtr& in); void unserializeItem(const BinaryTreePtr& in);
void serializeItem(const OutputBinaryTreePtr& out); void serializeItem(const OutputBinaryTreePtr& out);
@ -111,6 +109,7 @@ public:
void setActionId(uint16 actionId) { m_attribs.set(ATTR_ACTION_ID, actionId); } void setActionId(uint16 actionId) { m_attribs.set(ATTR_ACTION_ID, actionId); }
void setUniqueId(uint16 uniqueId) { m_attribs.set(ATTR_UNIQUE_ID, uniqueId); } void setUniqueId(uint16 uniqueId) { m_attribs.set(ATTR_UNIQUE_ID, uniqueId); }
bool isHouseDoor() { return m_attribs.has(ATTR_HOUSEDOORID); }
bool isDepot() { return m_attribs.has(ATTR_DEPOT_ID); } bool isDepot() { return m_attribs.has(ATTR_DEPOT_ID); }
bool isContainer() { return m_attribs.has(ATTR_CONTAINER_ITEMS); } bool isContainer() { return m_attribs.has(ATTR_CONTAINER_ITEMS); }
bool isDoor() { return m_attribs.has(ATTR_HOUSEDOORID); } bool isDoor() { return m_attribs.has(ATTR_HOUSEDOORID); }
@ -118,6 +117,7 @@ public:
bool isMoveable(); bool isMoveable();
bool isGround(); bool isGround();
ItemPtr clone();
ItemPtr asItem() { return static_self_cast<Item>(); } ItemPtr asItem() { return static_self_cast<Item>(); }
bool isItem() { return true; } bool isItem() { return true; }
@ -136,7 +136,7 @@ private:
uint16 m_serverId; uint16 m_serverId;
uint8 m_countOrSubType; uint8 m_countOrSubType;
stdext::packed_storage<uint8> m_attribs; stdext::packed_storage<uint8> m_attribs;
std::vector<ItemPtr> m_containerItems; ItemList m_containerItems;
}; };
#pragma pack(pop) #pragma pack(pop)

View File

@ -70,16 +70,19 @@ void Client::registerLuaFunctions()
g_lua.bindSingletonFunction("g_things", "findItemTypeByCategory", &ThingTypeManager::findItemTypeByCategory, &g_things); g_lua.bindSingletonFunction("g_things", "findItemTypeByCategory", &ThingTypeManager::findItemTypeByCategory, &g_things);
g_lua.registerSingletonClass("g_houses"); g_lua.registerSingletonClass("g_houses");
g_lua.bindSingletonFunction("g_houses", "clear", &HouseManager::clear, &g_houses); g_lua.bindSingletonFunction("g_houses", "clear", &HouseManager::clear, &g_houses);
g_lua.bindSingletonFunction("g_houses", "load", &HouseManager::load, &g_houses); g_lua.bindSingletonFunction("g_houses", "load", &HouseManager::load, &g_houses);
g_lua.bindSingletonFunction("g_houses", "save", &HouseManager::save, &g_houses); g_lua.bindSingletonFunction("g_houses", "save", &HouseManager::save, &g_houses);
g_lua.bindSingletonFunction("g_houses", "getHouse", &HouseManager::getHouse, &g_houses); g_lua.bindSingletonFunction("g_houses", "getHouse", &HouseManager::getHouse, &g_houses);
g_lua.bindSingletonFunction("g_houses", "addHouse", &HouseManager::addHouse, &g_houses); g_lua.bindSingletonFunction("g_houses", "getHouseByName", &HouseManager::getHouseByName, &g_houses);
g_lua.bindSingletonFunction("g_houses", "removeHouse", &HouseManager::removeHouse, &g_houses); g_lua.bindSingletonFunction("g_houses", "addHouse", &HouseManager::addHouse, &g_houses);
g_lua.bindSingletonFunction("g_houses", "getHouseList", &HouseManager::getHouseList, &g_houses); g_lua.bindSingletonFunction("g_houses", "removeHouse", &HouseManager::removeHouse, &g_houses);
g_lua.bindSingletonFunction("g_houses", "getHouseList", &HouseManager::getHouseList, &g_houses);
g_lua.bindSingletonFunction("g_houses", "filterHouses", &HouseManager::filterHouses, &g_houses);
g_lua.registerSingletonClass("g_towns"); g_lua.registerSingletonClass("g_towns");
g_lua.bindSingletonFunction("g_towns", "getTown", &TownManager::getTown, &g_towns); g_lua.bindSingletonFunction("g_towns", "getTown", &TownManager::getTown, &g_towns);
g_lua.bindSingletonFunction("g_towns", "getTownByName",&TownManager::getTownByName,&g_towns);
g_lua.bindSingletonFunction("g_towns", "addTown", &TownManager::addTown, &g_towns); g_lua.bindSingletonFunction("g_towns", "addTown", &TownManager::addTown, &g_towns);
g_lua.bindSingletonFunction("g_towns", "removeTown", &TownManager::removeTown, &g_towns); g_lua.bindSingletonFunction("g_towns", "removeTown", &TownManager::removeTown, &g_towns);
g_lua.bindSingletonFunction("g_towns", "getTowns", &TownManager::getTowns, &g_towns); g_lua.bindSingletonFunction("g_towns", "getTowns", &TownManager::getTowns, &g_towns);
@ -336,6 +339,7 @@ void Client::registerLuaFunctions()
g_lua.registerClass<House>(); g_lua.registerClass<House>();
g_lua.bindClassStaticFunction<House>("create", []{ return HousePtr(new House); }); g_lua.bindClassStaticFunction<House>("create", []{ return HousePtr(new House); });
g_lua.bindClassMemberFunction<House>("setId", &House::setId); g_lua.bindClassMemberFunction<House>("setId", &House::setId);
g_lua.bindClassMemberFunction<House>("getId", &House::getId);
g_lua.bindClassMemberFunction<House>("setName", &House::setName); g_lua.bindClassMemberFunction<House>("setName", &House::setName);
g_lua.bindClassMemberFunction<House>("getName", &House::getName); g_lua.bindClassMemberFunction<House>("getName", &House::getName);
g_lua.bindClassMemberFunction<House>("setTownId", &House::setTownId); g_lua.bindClassMemberFunction<House>("setTownId", &House::setTownId);
@ -344,6 +348,9 @@ void Client::registerLuaFunctions()
g_lua.bindClassMemberFunction<House>("getTile", &House::getTile); g_lua.bindClassMemberFunction<House>("getTile", &House::getTile);
g_lua.bindClassMemberFunction<House>("setEntry", &House::setEntry); g_lua.bindClassMemberFunction<House>("setEntry", &House::setEntry);
g_lua.bindClassMemberFunction<House>("getEntry", &House::getEntry); g_lua.bindClassMemberFunction<House>("getEntry", &House::getEntry);
g_lua.bindClassMemberFunction<House>("addDoor", &House::addDoor);
g_lua.bindClassMemberFunction<House>("removeDoor", &House::removeDoor);
g_lua.bindClassMemberFunction<House>("removeDoorById", &House::removeDoorById);
g_lua.bindClassMemberFunction<House>("setSize", &House::setSize); g_lua.bindClassMemberFunction<House>("setSize", &House::setSize);
g_lua.bindClassMemberFunction<House>("getSize", &House::getSize); g_lua.bindClassMemberFunction<House>("getSize", &House::getSize);
g_lua.bindClassMemberFunction<House>("setRent", &House::setRent); g_lua.bindClassMemberFunction<House>("setRent", &House::setRent);

View File

@ -58,6 +58,15 @@ const TownPtr& TownManager::getTown(uint32 townId)
return m_nullTown; return m_nullTown;
} }
const TownPtr& TownManager::getTownByName(std::string name)
{
auto it = std::find_if(m_towns.begin(), m_towns.end(),
[=] (const TownPtr& town) -> bool { return town->getName() == name; } );
if(it != m_towns.end())
return *it;
return m_nullTown;
}
TownList::iterator TownManager::findTown(uint32 townId) TownList::iterator TownManager::findTown(uint32 townId)
{ {
return std::find_if(m_towns.begin(), m_towns.end(), return std::find_if(m_towns.begin(), m_towns.end(),

View File

@ -54,6 +54,7 @@ public:
void addTown(const TownPtr& town); void addTown(const TownPtr& town);
void removeTown(uint32 townId); void removeTown(uint32 townId);
const TownPtr& getTown(uint32 townId); const TownPtr& getTown(uint32 townId);
const TownPtr& getTownByName(std::string name);
TownList getTowns() { return m_towns; } TownList getTowns() { return m_towns; }
void clear() { m_towns.clear(); m_nullTown = nullptr; } void clear() { m_towns.clear(); m_nullTown = nullptr; }

View File

@ -63,8 +63,8 @@ void UIItem::drawSelf(Fw::DrawPane drawPane)
g_painter->setColor(Color(231, 231, 231)); g_painter->setColor(Color(231, 231, 231));
m_font->drawText(count, Rect(m_rect.topLeft(), m_rect.bottomRight() - Point(3, 0)), Fw::AlignBottomRight); m_font->drawText(count, Rect(m_rect.topLeft(), m_rect.bottomRight() - Point(3, 0)), Fw::AlignBottomRight);
} }
// debug, show item id if(m_showId)
//m_font->drawText(stdext::to_string(m_item->getId()), m_rect, Fw::AlignBottomRight); m_font->drawText(stdext::to_string(m_item->getServerId()), m_rect, Fw::AlignBottomRight);
} }
drawBorder(m_rect); drawBorder(m_rect);
@ -98,5 +98,7 @@ void UIItem::onStyleApply(const std::string& styleName, const OTMLNodePtr& style
setItemVisible(node->value<bool>()); setItemVisible(node->value<bool>());
else if(node->tag() == "virtual") else if(node->tag() == "virtual")
setVirtual(node->value<bool>()); setVirtual(node->value<bool>());
else if(node->tag() == "show-id")
m_showId = node->value<bool>();
} }
} }

View File

@ -54,6 +54,7 @@ protected:
ItemPtr m_item; ItemPtr m_item;
stdext::boolean<false> m_virtual; stdext::boolean<false> m_virtual;
stdext::boolean<true> m_itemVisible; stdext::boolean<true> m_itemVisible;
stdext::boolean<false> m_showId;
}; };
#endif #endif