Skip to content

Commit

Permalink
Code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
mattyx14 committed Jan 19, 2015
1 parent 0e2697b commit 0e2ac13
Show file tree
Hide file tree
Showing 47 changed files with 700 additions and 749 deletions.
2 changes: 1 addition & 1 deletion sources/src/bed.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ BedItem* BedItem::getNextBedItem() const
Direction dir = Item::items[id].bedPartnerDir;
Position targetPos = getNextPosition(dir, getPosition());

Tile* tile = g_game.getTile(targetPos);
Tile* tile = g_game.map.getTile(targetPos);
if (!tile) {
return nullptr;
}
Expand Down
8 changes: 4 additions & 4 deletions sources/src/combat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,10 @@ void Combat::getCombatArea(const Position& centerPos, const Position& targetPos,
if (area) {
area->getList(centerPos, targetPos, list);
} else {
Tile* tile = g_game.getTile(targetPos);
Tile* tile = g_game.map.getTile(targetPos);
if (!tile) {
tile = new StaticTile(targetPos.x, targetPos.y, targetPos.z);
g_game.setTile(tile);
g_game.map.setTile(targetPos, tile);
}
list.push_back(tile);
}
Expand Down Expand Up @@ -1127,10 +1127,10 @@ void AreaCombat::getList(const Position& centerPos, const Position& targetPos, s
for (uint32_t x = 0; x < cols; ++x) {
if (area->getValue(y, x) != 0) {
if (g_game.isSightClear(targetPos, tmpPos, true)) {
Tile* tile = g_game.getTile(tmpPos);
Tile* tile = g_game.map.getTile(tmpPos);
if (!tile) {
tile = new StaticTile(tmpPos.x, tmpPos.y, tmpPos.z);
g_game.setTile(tile);
g_game.map.setTile(tmpPos, tile);
}
list.push_back(tile);
}
Expand Down
16 changes: 8 additions & 8 deletions sources/src/commands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -268,8 +268,8 @@ void Commands::reloadInfo(Player& player, const std::string& param)
Npcs::reload();
player.sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, "Reloaded npcs.");
} else if (tmpParam == "raid" || tmpParam == "raids") {
Raids::getInstance()->reload();
Raids::getInstance()->startup();
g_game.raids.reload();
g_game.raids.startup();
player.sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, "Reloaded raids.");
} else if (tmpParam == "spell" || tmpParam == "spells") {
g_spells->reload();
Expand All @@ -286,10 +286,10 @@ void Commands::reloadInfo(Player& player, const std::string& param)
g_weapons->loadDefaults();
player.sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, "Reloaded weapons.");
} else if (tmpParam == "quest" || tmpParam == "quests") {
Quests::getInstance()->reload();
g_game.quests.reload();
player.sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, "Reloaded quests.");
} else if (tmpParam == "mount" || tmpParam == "mounts") {
Mounts::getInstance()->reload();
g_game.mounts.reload();
player.sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, "Reloaded mounts.");
} else if (tmpParam == "globalevents" || tmpParam == "globalevent") {
g_globalEvents->reload();
Expand Down Expand Up @@ -339,7 +339,7 @@ void Commands::sellHouse(Player& player, const std::string& param)
return;
}

if (Houses::getInstance().getHouseByPlayerId(tradePartner->getGUID())) {
if (g_game.map.houses.getHouseByPlayerId(tradePartner->getGUID())) {
player.sendCancel("Trade player already owns a house.");
return;
}
Expand All @@ -364,18 +364,18 @@ void Commands::sellHouse(Player& player, const std::string& param)

void Commands::forceRaid(Player& player, const std::string& param)
{
Raid* raid = Raids::getInstance()->getRaidByName(param);
Raid* raid = g_game.raids.getRaidByName(param);
if (!raid || !raid->isLoaded()) {
player.sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, "No such raid exists.");
return;
}

if (Raids::getInstance()->getRunning()) {
if (g_game.raids.getRunning()) {
player.sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, "Another raid is already being executed.");
return;
}

Raids::getInstance()->setRunning(raid);
g_game.raids.setRunning(raid);

RaidEvent* event = raid->getNextRaidEvent();
if (!event) {
Expand Down
8 changes: 4 additions & 4 deletions sources/src/connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ void Connection::parseHeader(const boost::system::error_code& error)
std::placeholders::_1));

// Read packet content
m_msg.setMessageLength(size + NetworkMessage::header_length);
m_msg.setLength(size + NetworkMessage::header_length);
boost::asio::async_read(getHandle(), boost::asio::buffer(m_msg.getBodyBuffer(), size),
std::bind(&Connection::parsePacket, shared_from_this(), std::placeholders::_1));
} catch (boost::system::system_error& e) {
Expand Down Expand Up @@ -278,9 +278,9 @@ void Connection::parsePacket(const boost::system::error_code& error)

//Check packet checksum
uint32_t checksum;
int32_t len = m_msg.getMessageLength() - m_msg.getReadPos() - 4;
int32_t len = m_msg.getLength() - m_msg.getPosition() - 4;
if (len > 0) {
checksum = adlerChecksum(m_msg.getBuffer() + m_msg.getReadPos() + 4, len);
checksum = adlerChecksum(m_msg.getBuffer() + m_msg.getPosition() + 4, len);
} else {
checksum = 0;
}
Expand Down Expand Up @@ -364,7 +364,7 @@ void Connection::internalSend(OutputMessage_ptr msg)
std::placeholders::_1));

boost::asio::async_write(getHandle(),
boost::asio::buffer(msg->getOutputBuffer(), msg->getMessageLength()),
boost::asio::buffer(msg->getOutputBuffer(), msg->getLength()),
std::bind(&Connection::onWriteOperation, shared_from_this(), msg, std::placeholders::_1));
} catch (boost::system::system_error& e) {
if (m_logError) {
Expand Down
10 changes: 5 additions & 5 deletions sources/src/creature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ void Creature::updateMapCache()
for (int32_t x = -maxWalkCacheWidth; x <= maxWalkCacheWidth; ++x) {
pos.x = myPos.getX() + x;
pos.y = myPos.getY() + y;
tile = g_game.getTile(pos.x, pos.y, myPos.z);
tile = g_game.map.getTile(pos);
updateTileCache(tile, pos);
}
}
Expand Down Expand Up @@ -560,7 +560,7 @@ void Creature::onCreatureMove(Creature* creature, const Tile* newTile, const Pos

//update 0
for (int32_t x = -maxWalkCacheWidth; x <= maxWalkCacheWidth; ++x) {
tile = g_game.getTile(myPos.getX() + x, myPos.getY() - maxWalkCacheHeight, myPos.z);
tile = g_game.map.getTile(myPos.getX() + x, myPos.getY() - maxWalkCacheHeight, myPos.z);
updateTileCache(tile, x, -maxWalkCacheHeight);
}
} else if (oldPos.y < newPos.y) { // south
Expand All @@ -571,7 +571,7 @@ void Creature::onCreatureMove(Creature* creature, const Tile* newTile, const Pos

//update mapWalkHeight - 1
for (int32_t x = -maxWalkCacheWidth; x <= maxWalkCacheWidth; ++x) {
tile = g_game.getTile(myPos.getX() + x, myPos.getY() + maxWalkCacheHeight, myPos.z);
tile = g_game.map.getTile(myPos.getX() + x, myPos.getY() + maxWalkCacheHeight, myPos.z);
updateTileCache(tile, x, maxWalkCacheHeight);
}
}
Expand All @@ -596,7 +596,7 @@ void Creature::onCreatureMove(Creature* creature, const Tile* newTile, const Pos

//update mapWalkWidth - 1
for (int32_t y = -maxWalkCacheHeight; y <= maxWalkCacheHeight; ++y) {
tile = g_game.getTile(myPos.x + maxWalkCacheWidth, myPos.y + y, myPos.z);
tile = g_game.map.getTile(myPos.x + maxWalkCacheWidth, myPos.y + y, myPos.z);
updateTileCache(tile, maxWalkCacheWidth, y);
}
} else if (oldPos.x > newPos.x) { // west
Expand All @@ -619,7 +619,7 @@ void Creature::onCreatureMove(Creature* creature, const Tile* newTile, const Pos

//update 0
for (int32_t y = -maxWalkCacheHeight; y <= maxWalkCacheHeight; ++y) {
tile = g_game.getTile(myPos.x - maxWalkCacheWidth, myPos.y + y, myPos.z);
tile = g_game.map.getTile(myPos.x - maxWalkCacheWidth, myPos.y + y, myPos.z);
updateTileCache(tile, -maxWalkCacheWidth, y);
}
}
Expand Down
4 changes: 3 additions & 1 deletion sources/src/databasetasks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,5 +115,7 @@ void DatabaseTasks::shutdown()

void DatabaseTasks::join()
{
thread.join();
if (thread.joinable()) {
thread.join();
}
}
65 changes: 27 additions & 38 deletions sources/src/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ Game::Game() :
gameState = GAME_STATE_NORMAL;
worldType = WORLD_TYPE_PVP;

services = nullptr;
serviceManager = nullptr;
lastStageLevel = 0;
playersRecord = 0;
motdNum = 0;
Expand Down Expand Up @@ -103,9 +103,9 @@ Game::~Game()
}
}

void Game::start(ServiceManager* servicer)
void Game::start(ServiceManager* manager)
{
services = servicer;
serviceManager = manager;

g_scheduler.addEvent(createSchedulerTask(EVENT_LIGHTINTERVAL, std::bind(&Game::checkLight, this)));
g_scheduler.addEvent(createSchedulerTask(EVENT_CREATURE_THINK_INTERVAL, std::bind(&Game::checkCreatures, this, 0)));
Expand Down Expand Up @@ -142,14 +142,13 @@ void Game::setGameState(GameState_t newState)
groups.load();
g_chat->load();

Spawns::getInstance()->startup();
map.spawns.startup();

Raids::getInstance()->loadFromXml();
Raids::getInstance()->startup();
raids.loadFromXml();
raids.startup();

Quests::getInstance()->loadFromXml();

Mounts::getInstance()->loadFromXml();
quests.loadFromXml();
mounts.loadFromXml();

loadMotdNum();
loadPlayersRecord();
Expand Down Expand Up @@ -236,7 +235,7 @@ void Game::loadMap(const std::string& path)
Cylinder* Game::internalGetCylinder(Player* player, const Position& pos) const
{
if (pos.x != 0xFFFF) {
return getTile(pos.x, pos.y, pos.z);
return map.getTile(pos);
}

//container
Expand All @@ -252,7 +251,7 @@ Cylinder* Game::internalGetCylinder(Player* player, const Position& pos) const
Thing* Game::internalGetThing(Player* player, const Position& pos, int32_t index, uint32_t spriteId /*= 0*/, stackPosType_t type /*= STACKPOS_NORMAL*/) const
{
if (pos.x != 0xFFFF) {
Tile* tile = getTile(pos.x, pos.y, pos.z);
Tile* tile = map.getTile(pos);
if (tile) {
/*look at*/
if (type == STACKPOS_LOOK) {
Expand Down Expand Up @@ -382,11 +381,6 @@ void Game::internalGetPosition(Item* item, Position& pos, uint8_t& stackpos)
}
}

void Game::setTile(Tile* newTile)
{
return map.setTile(newTile->getPosition(), newTile);
}

Creature* Game::getCreatureByID(uint32_t id)
{
if (id <= Player::playerAutoID) {
Expand Down Expand Up @@ -866,7 +860,7 @@ void Game::playerMoveCreature(uint32_t playerId, uint32_t movingCreatureId,
return;
}

Tile* toTile = getTile(toPos);
Tile* toTile = map.getTile(toPos);
if (!toTile) {
player->sendCancelMessage(RETURNVALUE_NOTPOSSIBLE);
return;
Expand Down Expand Up @@ -933,7 +927,7 @@ void Game::playerMoveCreature(uint32_t playerId, uint32_t movingCreatureId,
}

Npc* movingNpc = movingCreature->getNpc();
if (movingNpc && !Spawns::getInstance()->isInZone(movingNpc->getMasterPos(), movingNpc->getMasterRadius(), toPos)) {
if (movingNpc && !Spawns::isInZone(movingNpc->getMasterPos(), movingNpc->getMasterRadius(), toPos)) {
player->sendCancelMessage(RETURNVALUE_NOTENOUGHROOM);
return;
}
Expand Down Expand Up @@ -977,9 +971,9 @@ ReturnValue Game::internalMoveCreature(Creature* creature, Direction direction,
if (creature->getPlayer() && !diagonalMovement) {
//try go up
if (currentPos.z != 8 && creature->getTile()->hasHeight(3)) {
Tile* tmpTile = getTile(currentPos.x, currentPos.y, currentPos.getZ() - 1);
Tile* tmpTile = map.getTile(currentPos.x, currentPos.y, currentPos.getZ() - 1);
if (tmpTile == nullptr || (tmpTile->ground == nullptr && !tmpTile->hasProperty(CONST_PROP_BLOCKSOLID))) {
tmpTile = getTile(destPos.x, destPos.y, destPos.getZ() - 1);
tmpTile = map.getTile(destPos.x, destPos.y, destPos.getZ() - 1);
if (tmpTile && tmpTile->ground && !tmpTile->hasProperty(CONST_PROP_BLOCKSOLID)) {
flags = flags | FLAG_IGNOREBLOCKITEM | FLAG_IGNOREBLOCKCREATURE;

Expand All @@ -990,9 +984,9 @@ ReturnValue Game::internalMoveCreature(Creature* creature, Direction direction,
}
} else {
//try go down
Tile* tmpTile = getTile(destPos);
Tile* tmpTile = map.getTile(destPos.x, destPos.y, destPos.z);
if (currentPos.z != 7 && (tmpTile == nullptr || (tmpTile->ground == nullptr && !tmpTile->hasProperty(CONST_PROP_BLOCKSOLID)))) {
tmpTile = getTile(destPos.x, destPos.y, destPos.z + 1);
tmpTile = map.getTile(destPos.x, destPos.y, destPos.z + 1);
if (tmpTile && tmpTile->hasHeight(3)) {
flags |= FLAG_IGNOREBLOCKITEM | FLAG_IGNOREBLOCKCREATURE;
destPos.z++;
Expand All @@ -1001,7 +995,7 @@ ReturnValue Game::internalMoveCreature(Creature* creature, Direction direction,
}
}

toTile = getTile(destPos);
toTile = map.getTile(destPos.x, destPos.y, destPos.z);
ReturnValue ret = RETURNVALUE_NOTPOSSIBLE;

if (toTile != nullptr) {
Expand Down Expand Up @@ -1857,7 +1851,7 @@ ReturnValue Game::internalTeleport(Thing* thing, const Position& newPos, bool pu
return RETURNVALUE_NOTPOSSIBLE;
}

Tile* toTile = getTile(newPos.x, newPos.y, newPos.z);
Tile* toTile = map.getTile(newPos);
if (toTile) {
if (Creature* creature = thing->getCreature()) {
ReturnValue ret = toTile->queryAdd(0, *creature, 1, FLAG_NOLIMIT);
Expand Down Expand Up @@ -2505,7 +2499,7 @@ void Game::playerBrowseField(uint32_t playerId, const Position& pos)
return;
}

Tile* tile = getTile(pos);
Tile* tile = map.getTile(pos);
if (!tile) {
return;
}
Expand Down Expand Up @@ -3326,7 +3320,7 @@ void Game::playerChangeOutfit(uint32_t playerId, Outfit_t outfit)
player->hasRequestedOutfit(false);

if (outfit.lookMount != 0) {
Mount* mount = Mounts::getInstance()->getMountByClientID(outfit.lookMount);
Mount* mount = mounts.getMountByClientID(outfit.lookMount);
if (!mount) {
return;
}
Expand All @@ -3336,7 +3330,7 @@ void Game::playerChangeOutfit(uint32_t playerId, Outfit_t outfit)
}

if (player->isMounted()) {
Mount* prevMount = Mounts::getInstance()->getMountByID(player->getCurrentMount());
Mount* prevMount = mounts.getMountByID(player->getCurrentMount());
if (prevMount) {
changeSpeed(player, mount->speed - prevMount->speed);
}
Expand Down Expand Up @@ -3378,7 +3372,7 @@ void Game::playerShowQuestLine(uint32_t playerId, uint16_t questId)
return;
}

Quest* quest = Quests::getInstance()->getQuestByID(questId);
Quest* quest = quests.getQuestByID(questId);
if (!quest) {
return;
}
Expand Down Expand Up @@ -4575,13 +4569,13 @@ void Game::shutdown()
g_scheduler.shutdown();
g_databaseTasks.shutdown();
g_dispatcher.shutdown();
Spawns::getInstance()->clear();
Raids::getInstance()->clear();
map.spawns.clear();
raids.clear();

cleanup();

if (services) {
services->stop();
if (serviceManager) {
serviceManager->stop();
}

ConnectionManager::getInstance()->closeAll();
Expand Down Expand Up @@ -5724,7 +5718,7 @@ void Game::addGuild(Guild* guild)

void Game::decreaseBrowseFieldRef(const Position& pos)
{
Tile* tile = getTile(pos);
Tile* tile = map.getTile(pos.x, pos.y, pos.z);
if (!tile) {
return;
}
Expand All @@ -5735,11 +5729,6 @@ void Game::decreaseBrowseFieldRef(const Position& pos)
}
}

Group* Game::getGroup(uint32_t id)
{
return groups.getGroup(id);
}

void Game::internalRemoveItems(std::vector<Item*> itemList, uint32_t amount, bool stackable)
{
if (stackable) {
Expand Down
Loading

0 comments on commit 0e2ac13

Please sign in to comment.