diff --git a/src/client/creatures.cpp b/src/client/creatures.cpp index e083edab..3b827614 100644 --- a/src/client/creatures.cpp +++ b/src/client/creatures.cpp @@ -62,7 +62,6 @@ void Spawn::load(TiXmlElement* node) if(cNode->ValueStr() != "monster" && cNode->ValueStr() != "npc") stdext::throw_exception(stdext::format("invalid spawn-subnode %s", cNode->ValueStr())); - setNPC(cNode->ValueStr() == "npc"); std::string cName = cNode->Attribute("name"); stdext::tolower(cName); stdext::trim(cName); @@ -82,6 +81,7 @@ void Spawn::load(TiXmlElement* node) placePos.y = centerPos.y + cNode->readType("y"); placePos.z = cNode->readType("z"); + cType->setRace(cNode->ValueStr() == "npc" ? CreatureRaceNpc : CreatureRaceMonster); addCreature(placePos, cType); } } @@ -98,10 +98,10 @@ void Spawn::save(TiXmlElement* node) TiXmlElement* creatureNode = nullptr; for(const auto& pair : m_creatures) { - if(!(creatureNode = new TiXmlElement(getNPC()? "npc" : "monster"))) + const CreatureTypePtr& creature = pair.second; + if(!(creatureNode = new TiXmlElement(creature->getRace() == CreatureRaceNpc ? "npc" : "monster"))) stdext::throw_exception("oom?"); - const CreatureTypePtr& creature = pair.second; creatureNode->SetAttribute("name", creature->getName()); creatureNode->SetAttribute("spawntime", creature->getSpawnTime()); diff --git a/src/client/creatures.h b/src/client/creatures.h index 81617770..b20b1d58 100644 --- a/src/client/creatures.h +++ b/src/client/creatures.h @@ -33,14 +33,20 @@ enum CreatureAttr : uint8 CreatureAttrName = 1, CreatureAttrOutfit = 2, CreatureAttrSpawnTime = 3, - CreatureAttrDir = 4 + CreatureAttrDir = 4, + CreatureAttrRace = 5 +}; + +enum CreatureRace : uint8 +{ + CreatureRaceNpc = 0, + CreatureRaceMonster = 1 }; enum SpawnAttr : uint8 { SpawnAttrRadius = 0, SpawnAttrCenter = 1, - SpawnAttrNPC = 2, }; class Spawn : public LuaObject @@ -55,9 +61,6 @@ public: void setCenterPos(const Position& pos) { m_attribs.set(SpawnAttrCenter, pos); } Position getCenterPos() { return m_attribs.get(SpawnAttrCenter); } - void setNPC(bool n) { m_attribs.set(SpawnAttrNPC, n); } - bool getNPC() { return m_attribs.get(SpawnAttrNPC); } - void addCreature(const Position& placePos, const CreatureTypePtr& cType); void removeCreature(const Position& pos); void clear() { m_creatures.clear(); } @@ -90,6 +93,9 @@ public: void setDirection(Otc::Direction dir) { m_attribs.set(CreatureAttrDir, dir); } Otc::Direction getDirection() { return m_attribs.get(CreatureAttrDir); } + void setRace(CreatureRace race) { m_attribs.set(CreatureAttrRace, race); } + CreatureRace getRace() { return m_attribs.get(CreatureAttrRace); } + CreaturePtr cast(); private: diff --git a/src/client/houses.cpp b/src/client/houses.cpp index 6e3abe14..8cf7d5e8 100644 --- a/src/client/houses.cpp +++ b/src/client/houses.cpp @@ -191,7 +191,7 @@ HouseList HouseManager::filterHouses(uint32 townId) HouseList ret; for(const HousePtr& house : m_houses) if(house->getTownId() == townId) - ret.push_back(house); + ret.push_back(house); return ret; } diff --git a/src/client/itemtype.cpp b/src/client/itemtype.cpp index 57faf8c3..8c164df3 100644 --- a/src/client/itemtype.cpp +++ b/src/client/itemtype.cpp @@ -53,7 +53,6 @@ void ItemType::unserialize(const BinaryTreePtr& node) if(serverId > 20000 && serverId < 20100) { serverId -= 20000; } else if(lastId > 99 && lastId != serverId - 1) { - while(lastId != serverId - 1) { ItemTypePtr tmp(new ItemType); tmp->setServerId(lastId++); @@ -64,14 +63,12 @@ void ItemType::unserialize(const BinaryTreePtr& node) lastId = serverId; break; } - case ItemTypeAttrClientId: { + case ItemTypeAttrClientId: setClientId(node->getU16()); break; - } - case ItemTypeAttrName: { + case ItemTypeAttrName: setName(node->getString(len)); break; - } default: node->skip(len); // skip attribute break; diff --git a/src/client/map.cpp b/src/client/map.cpp index 71bb7b61..e17362cf 100644 --- a/src/client/map.cpp +++ b/src/client/map.cpp @@ -390,7 +390,7 @@ void Map::removeCreatureById(uint32 id) void Map::removeUnawareThings() { - // remove creatures from tiles that we are not aware anymore + // remove creatures from tiles that we are not aware of anymore for(const auto& pair : m_knownCreatures) { const CreaturePtr& creature = pair.second; if(!isAwareOfPosition(creature->getPosition())) diff --git a/src/client/map.h b/src/client/map.h index 99dbdba1..8423eed6 100644 --- a/src/client/map.h +++ b/src/client/map.h @@ -254,3 +254,5 @@ private: extern Map g_map; #endif + +/* vim: set ts=4 sw=4 et: */ diff --git a/src/client/mapio.cpp b/src/client/mapio.cpp index 1ea7da42..5da46593 100644 --- a/src/client/mapio.cpp +++ b/src/client/mapio.cpp @@ -74,7 +74,7 @@ void Map::loadOtbm(const std::string& fileName) if(node->getU8() != OTBM_MAP_DATA) stdext::throw_exception("Could not read root data node"); - while (node->canRead()) { + while(node->canRead()) { uint8 attribute = node->getU8(); std::string tmp = node->getString(); switch (attribute) { @@ -102,7 +102,7 @@ void Map::loadOtbm(const std::string& fileName) for(const BinaryTreePtr &nodeTile : nodeMapData->getChildren()) { uint8 type = nodeTile->getU8(); - if(type != OTBM_TILE && type != OTBM_HOUSETILE) + if(unlikely(type != OTBM_TILE && type != OTBM_HOUSETILE)) stdext::throw_exception(stdext::format("invalid node tile type %d", (int)type)); HousePtr house = nullptr; @@ -151,7 +151,7 @@ void Map::loadOtbm(const std::string& fileName) } for(const BinaryTreePtr& nodeItem : nodeTile->getChildren()) { - if(nodeItem->getU8() != OTBM_ITEM) + if(unlikely(nodeItem->getU8() != OTBM_ITEM)) stdext::throw_exception("invalid item node"); ItemPtr item = Item::createFromOtb(nodeItem->getU16()); @@ -198,7 +198,7 @@ void Map::loadOtbm(const std::string& fileName) if(!(town = g_towns.getTown(townId))) g_towns.addTown(TownPtr(new Town(townId, townName, townCoords))); - } + } } else if(mapDataType == OTBM_WAYPOINTS && headerVersion > 1) { for(const BinaryTreePtr &nodeWaypoint : nodeMapData->getChildren()) { if(nodeWaypoint->getU8() != OTBM_WAYPOINT) @@ -306,11 +306,11 @@ void Map::saveOtbm(const std::string& fileName) for(const auto& it : m_tileBlocks[z]) { const TileBlock& block = it.second; for(const TilePtr& tile : block.getTiles()) { - if(!tile || tile->isEmpty()) + if(unlikely(!tile || tile->isEmpty())) continue; const Position& pos = tile->getPosition(); - if(!pos.isValid()) + if(unlikely(!pos.isValid())) continue; if(pos.x < px || pos.x >= px + 256 diff --git a/src/client/thingtypemanager.cpp b/src/client/thingtypemanager.cpp index facae0d8..c43105c1 100644 --- a/src/client/thingtypemanager.cpp +++ b/src/client/thingtypemanager.cpp @@ -156,6 +156,7 @@ void ThingTypeManager::loadOtb(const std::string& file) m_reverseItemTypes.clear(); m_itemTypes.resize(root->getChildren().size() + 1, m_nullItemType); + m_reverseItemTypes.resize(root->getChildren().size() + 1, m_nullItemType); for(const BinaryTreePtr& node : root->getChildren()) { ItemTypePtr itemType(new ItemType); @@ -163,8 +164,8 @@ void ThingTypeManager::loadOtb(const std::string& file) addItemType(itemType); uint16 clientId = itemType->getClientId(); - if(clientId >= m_reverseItemTypes.size()) - m_reverseItemTypes.resize(clientId+1); + if(unlikely(clientId >= m_reverseItemTypes.size())) + m_reverseItemTypes.resize(clientId + 1); m_reverseItemTypes[clientId] = itemType; } @@ -189,8 +190,8 @@ void ThingTypeManager::loadXml(const std::string& file) if(!root || root->ValueTStr() != "items") stdext::throw_exception("invalid root tag name"); - for (TiXmlElement *element = root->FirstChildElement(); element; element = element->NextSiblingElement()) { - if(element->ValueTStr() != "item") + for(TiXmlElement *element = root->FirstChildElement(); element; element = element->NextSiblingElement()) { + if(unlikely(element->ValueTStr() != "item")) continue; uint16 id = element->readType("id"); @@ -274,7 +275,7 @@ void ThingTypeManager::parseItemType(uint16 id, TiXmlElement* elem) void ThingTypeManager::addItemType(const ItemTypePtr& itemType) { uint16 id = itemType->getServerId(); - if(id >= m_itemTypes.size()) + if(unlikely(id >= m_itemTypes.size())) m_itemTypes.resize(id + 1, m_nullItemType); m_itemTypes[id] = itemType; } diff --git a/src/framework/stdext/compiler.h b/src/framework/stdext/compiler.h index 8139ff24..93d9f372 100644 --- a/src/framework/stdext/compiler.h +++ b/src/framework/stdext/compiler.h @@ -33,8 +33,23 @@ #error "Compiler not supported." #endif +/// Branch Prediction. See the GCC Manual for more information. +/// NB: These are used when speed is need most; do not use in normal +/// code, they may slow down stuff. +#if defined(__clang__) || defined(__GNUC__) +#define likely(x) __builtin_expect(!!(x), 1) +#define unlikely(x) __builtin_expect(!!(x), 0) +#else +#define likely(x) (x) +#define unlikely(x) (x) +#endif + #if !defined(__GXX_EXPERIMENTAL_CXX0X__) -#error "Sorry, you must enable C++0x to compile." +#error "C++0x is required to compile this application. Try updating your compiler." +#endif + +#ifdef _MSC_VER +#warning "MSVC lacks some C++11 features used in this application; compilation is most likely to fail." #endif #endif