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.
master
Ahmed Samy 11 years ago
parent bbdeac2e33
commit 520baa28ea

@ -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<int>("y");
placePos.z = cNode->readType<int>("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());

@ -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<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 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<Otc::Direction>(CreatureAttrDir); }
void setRace(CreatureRace race) { m_attribs.set(CreatureAttrRace, race); }
CreatureRace getRace() { return m_attribs.get<CreatureRace>(CreatureAttrRace); }
CreaturePtr cast();
private:

@ -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;
}

@ -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;

@ -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()))

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

@ -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

@ -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<uint16>("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;
}

@ -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

Loading…
Cancel
Save