Move "getNPC" that was introduced in commit

00729bbc2e
from Spawn to CreatureType

I didn't have a closer look at how his code was structured, what he
basically did
is that he set all creatures in a spawn as NPC's even if it's a monster
which is
so erroneous.

Highlights:
- Add branch prediction macros
- Minor code style fixes & some others

Hopefully the branch prediction thing will speed up OTB since it's
awfully slow.
This commit is contained in:
Ahmed Samy 2013-08-27 03:39:07 +02:00
parent bbdeac2e33
commit 520baa28ea
9 changed files with 48 additions and 27 deletions

View File

@ -62,7 +62,6 @@ void Spawn::load(TiXmlElement* node)
if(cNode->ValueStr() != "monster" && cNode->ValueStr() != "npc") if(cNode->ValueStr() != "monster" && cNode->ValueStr() != "npc")
stdext::throw_exception(stdext::format("invalid spawn-subnode %s", cNode->ValueStr())); stdext::throw_exception(stdext::format("invalid spawn-subnode %s", cNode->ValueStr()));
setNPC(cNode->ValueStr() == "npc");
std::string cName = cNode->Attribute("name"); std::string cName = cNode->Attribute("name");
stdext::tolower(cName); stdext::tolower(cName);
stdext::trim(cName); stdext::trim(cName);
@ -82,6 +81,7 @@ void Spawn::load(TiXmlElement* node)
placePos.y = centerPos.y + cNode->readType<int>("y"); placePos.y = centerPos.y + cNode->readType<int>("y");
placePos.z = cNode->readType<int>("z"); placePos.z = cNode->readType<int>("z");
cType->setRace(cNode->ValueStr() == "npc" ? CreatureRaceNpc : CreatureRaceMonster);
addCreature(placePos, cType); addCreature(placePos, cType);
} }
} }
@ -98,10 +98,10 @@ void Spawn::save(TiXmlElement* node)
TiXmlElement* creatureNode = nullptr; TiXmlElement* creatureNode = nullptr;
for(const auto& pair : m_creatures) { 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?"); stdext::throw_exception("oom?");
const CreatureTypePtr& creature = pair.second;
creatureNode->SetAttribute("name", creature->getName()); creatureNode->SetAttribute("name", creature->getName());
creatureNode->SetAttribute("spawntime", creature->getSpawnTime()); creatureNode->SetAttribute("spawntime", creature->getSpawnTime());

View File

@ -33,14 +33,20 @@ enum CreatureAttr : uint8
CreatureAttrName = 1, CreatureAttrName = 1,
CreatureAttrOutfit = 2, CreatureAttrOutfit = 2,
CreatureAttrSpawnTime = 3, CreatureAttrSpawnTime = 3,
CreatureAttrDir = 4 CreatureAttrDir = 4,
CreatureAttrRace = 5
};
enum CreatureRace : uint8
{
CreatureRaceNpc = 0,
CreatureRaceMonster = 1
}; };
enum SpawnAttr : uint8 enum SpawnAttr : uint8
{ {
SpawnAttrRadius = 0, SpawnAttrRadius = 0,
SpawnAttrCenter = 1, SpawnAttrCenter = 1,
SpawnAttrNPC = 2,
}; };
class Spawn : public LuaObject class Spawn : public LuaObject
@ -55,9 +61,6 @@ public:
void setCenterPos(const Position& pos) { m_attribs.set(SpawnAttrCenter, pos); } void setCenterPos(const Position& pos) { m_attribs.set(SpawnAttrCenter, pos); }
Position getCenterPos() { return m_attribs.get<Position>(SpawnAttrCenter); } Position getCenterPos() { return m_attribs.get<Position>(SpawnAttrCenter); }
void setNPC(bool n) { m_attribs.set(SpawnAttrNPC, n); }
bool getNPC() { return m_attribs.get<bool>(SpawnAttrNPC); }
void addCreature(const Position& placePos, const CreatureTypePtr& cType); void addCreature(const Position& placePos, const CreatureTypePtr& cType);
void removeCreature(const Position& pos); void removeCreature(const Position& pos);
void clear() { m_creatures.clear(); } void clear() { m_creatures.clear(); }
@ -90,6 +93,9 @@ public:
void setDirection(Otc::Direction dir) { m_attribs.set(CreatureAttrDir, dir); } void setDirection(Otc::Direction dir) { m_attribs.set(CreatureAttrDir, dir); }
Otc::Direction getDirection() { return m_attribs.get<Otc::Direction>(CreatureAttrDir); } Otc::Direction getDirection() { return m_attribs.get<Otc::Direction>(CreatureAttrDir); }
void setRace(CreatureRace race) { m_attribs.set(CreatureAttrRace, race); }
CreatureRace getRace() { return m_attribs.get<CreatureRace>(CreatureAttrRace); }
CreaturePtr cast(); CreaturePtr cast();
private: private:

View File

@ -191,7 +191,7 @@ HouseList HouseManager::filterHouses(uint32 townId)
HouseList ret; HouseList ret;
for(const HousePtr& house : m_houses) for(const HousePtr& house : m_houses)
if(house->getTownId() == townId) if(house->getTownId() == townId)
ret.push_back(house); ret.push_back(house);
return ret; return ret;
} }

View File

@ -53,7 +53,6 @@ void ItemType::unserialize(const BinaryTreePtr& node)
if(serverId > 20000 && serverId < 20100) { if(serverId > 20000 && serverId < 20100) {
serverId -= 20000; serverId -= 20000;
} else if(lastId > 99 && lastId != serverId - 1) { } else if(lastId > 99 && lastId != serverId - 1) {
while(lastId != serverId - 1) { while(lastId != serverId - 1) {
ItemTypePtr tmp(new ItemType); ItemTypePtr tmp(new ItemType);
tmp->setServerId(lastId++); tmp->setServerId(lastId++);
@ -64,14 +63,12 @@ void ItemType::unserialize(const BinaryTreePtr& node)
lastId = serverId; lastId = serverId;
break; break;
} }
case ItemTypeAttrClientId: { case ItemTypeAttrClientId:
setClientId(node->getU16()); setClientId(node->getU16());
break; break;
} case ItemTypeAttrName:
case ItemTypeAttrName: {
setName(node->getString(len)); setName(node->getString(len));
break; break;
}
default: default:
node->skip(len); // skip attribute node->skip(len); // skip attribute
break; break;

View File

@ -390,7 +390,7 @@ void Map::removeCreatureById(uint32 id)
void Map::removeUnawareThings() 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) { for(const auto& pair : m_knownCreatures) {
const CreaturePtr& creature = pair.second; const CreaturePtr& creature = pair.second;
if(!isAwareOfPosition(creature->getPosition())) if(!isAwareOfPosition(creature->getPosition()))

View File

@ -254,3 +254,5 @@ private:
extern Map g_map; extern Map g_map;
#endif #endif
/* vim: set ts=4 sw=4 et: */

View File

@ -74,7 +74,7 @@ void Map::loadOtbm(const std::string& fileName)
if(node->getU8() != OTBM_MAP_DATA) if(node->getU8() != OTBM_MAP_DATA)
stdext::throw_exception("Could not read root data node"); stdext::throw_exception("Could not read root data node");
while (node->canRead()) { while(node->canRead()) {
uint8 attribute = node->getU8(); uint8 attribute = node->getU8();
std::string tmp = node->getString(); std::string tmp = node->getString();
switch (attribute) { switch (attribute) {
@ -102,7 +102,7 @@ void Map::loadOtbm(const std::string& fileName)
for(const BinaryTreePtr &nodeTile : nodeMapData->getChildren()) { for(const BinaryTreePtr &nodeTile : nodeMapData->getChildren()) {
uint8 type = nodeTile->getU8(); 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)); stdext::throw_exception(stdext::format("invalid node tile type %d", (int)type));
HousePtr house = nullptr; HousePtr house = nullptr;
@ -151,7 +151,7 @@ void Map::loadOtbm(const std::string& fileName)
} }
for(const BinaryTreePtr& nodeItem : nodeTile->getChildren()) { for(const BinaryTreePtr& nodeItem : nodeTile->getChildren()) {
if(nodeItem->getU8() != OTBM_ITEM) if(unlikely(nodeItem->getU8() != OTBM_ITEM))
stdext::throw_exception("invalid item node"); stdext::throw_exception("invalid item node");
ItemPtr item = Item::createFromOtb(nodeItem->getU16()); ItemPtr item = Item::createFromOtb(nodeItem->getU16());
@ -198,7 +198,7 @@ void Map::loadOtbm(const std::string& fileName)
if(!(town = g_towns.getTown(townId))) if(!(town = g_towns.getTown(townId)))
g_towns.addTown(TownPtr(new Town(townId, townName, townCoords))); g_towns.addTown(TownPtr(new Town(townId, townName, townCoords)));
} }
} else if(mapDataType == OTBM_WAYPOINTS && headerVersion > 1) { } else if(mapDataType == OTBM_WAYPOINTS && headerVersion > 1) {
for(const BinaryTreePtr &nodeWaypoint : nodeMapData->getChildren()) { for(const BinaryTreePtr &nodeWaypoint : nodeMapData->getChildren()) {
if(nodeWaypoint->getU8() != OTBM_WAYPOINT) if(nodeWaypoint->getU8() != OTBM_WAYPOINT)
@ -306,11 +306,11 @@ void Map::saveOtbm(const std::string& fileName)
for(const auto& it : m_tileBlocks[z]) { for(const auto& it : m_tileBlocks[z]) {
const TileBlock& block = it.second; const TileBlock& block = it.second;
for(const TilePtr& tile : block.getTiles()) { for(const TilePtr& tile : block.getTiles()) {
if(!tile || tile->isEmpty()) if(unlikely(!tile || tile->isEmpty()))
continue; continue;
const Position& pos = tile->getPosition(); const Position& pos = tile->getPosition();
if(!pos.isValid()) if(unlikely(!pos.isValid()))
continue; continue;
if(pos.x < px || pos.x >= px + 256 if(pos.x < px || pos.x >= px + 256

View File

@ -156,6 +156,7 @@ void ThingTypeManager::loadOtb(const std::string& file)
m_reverseItemTypes.clear(); m_reverseItemTypes.clear();
m_itemTypes.resize(root->getChildren().size() + 1, m_nullItemType); m_itemTypes.resize(root->getChildren().size() + 1, m_nullItemType);
m_reverseItemTypes.resize(root->getChildren().size() + 1, m_nullItemType);
for(const BinaryTreePtr& node : root->getChildren()) { for(const BinaryTreePtr& node : root->getChildren()) {
ItemTypePtr itemType(new ItemType); ItemTypePtr itemType(new ItemType);
@ -163,8 +164,8 @@ void ThingTypeManager::loadOtb(const std::string& file)
addItemType(itemType); addItemType(itemType);
uint16 clientId = itemType->getClientId(); uint16 clientId = itemType->getClientId();
if(clientId >= m_reverseItemTypes.size()) if(unlikely(clientId >= m_reverseItemTypes.size()))
m_reverseItemTypes.resize(clientId+1); m_reverseItemTypes.resize(clientId + 1);
m_reverseItemTypes[clientId] = itemType; m_reverseItemTypes[clientId] = itemType;
} }
@ -189,8 +190,8 @@ void ThingTypeManager::loadXml(const std::string& file)
if(!root || root->ValueTStr() != "items") if(!root || root->ValueTStr() != "items")
stdext::throw_exception("invalid root tag name"); stdext::throw_exception("invalid root tag name");
for (TiXmlElement *element = root->FirstChildElement(); element; element = element->NextSiblingElement()) { for(TiXmlElement *element = root->FirstChildElement(); element; element = element->NextSiblingElement()) {
if(element->ValueTStr() != "item") if(unlikely(element->ValueTStr() != "item"))
continue; continue;
uint16 id = element->readType<uint16>("id"); uint16 id = element->readType<uint16>("id");
@ -274,7 +275,7 @@ void ThingTypeManager::parseItemType(uint16 id, TiXmlElement* elem)
void ThingTypeManager::addItemType(const ItemTypePtr& itemType) void ThingTypeManager::addItemType(const ItemTypePtr& itemType)
{ {
uint16 id = itemType->getServerId(); uint16 id = itemType->getServerId();
if(id >= m_itemTypes.size()) if(unlikely(id >= m_itemTypes.size()))
m_itemTypes.resize(id + 1, m_nullItemType); m_itemTypes.resize(id + 1, m_nullItemType);
m_itemTypes[id] = itemType; m_itemTypes[id] = itemType;
} }

View File

@ -33,8 +33,23 @@
#error "Compiler not supported." #error "Compiler not supported."
#endif #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__) #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
#endif #endif