2016-01-12 03:41:44 +00:00
|
|
|
//
|
|
|
|
// Created by koncord on 05.01.16.
|
|
|
|
//
|
|
|
|
|
|
|
|
#include "Player.hpp"
|
2016-08-19 00:18:25 +00:00
|
|
|
#include "Networking.hpp"
|
2016-01-12 03:41:44 +00:00
|
|
|
|
|
|
|
TPlayers Players::players;
|
|
|
|
TSlots Players::slots;
|
|
|
|
|
2016-11-16 00:05:14 +00:00
|
|
|
void Players::deletePlayer(RakNet::RakNetGUID guid)
|
2016-01-12 03:41:44 +00:00
|
|
|
{
|
2017-02-27 09:13:07 +00:00
|
|
|
LOG_MESSAGE_SIMPLE(Log::LOG_INFO, "Deleting player with guid %lu", guid.g);
|
2016-11-21 21:40:50 +00:00
|
|
|
|
2016-11-17 05:11:42 +00:00
|
|
|
if (players[guid] != 0)
|
2016-01-12 03:41:44 +00:00
|
|
|
{
|
2017-02-19 14:27:00 +00:00
|
|
|
CellController::get()->deletePlayer(players[guid]);
|
|
|
|
|
2017-02-27 09:13:07 +00:00
|
|
|
LOG_APPEND(Log::LOG_INFO, "- Emptying slot %i", players[guid]->getId());
|
2016-08-18 22:32:39 +00:00
|
|
|
|
2016-11-17 05:11:42 +00:00
|
|
|
slots[players[guid]->getId()] = 0;
|
|
|
|
delete players[guid];
|
|
|
|
players.erase(guid);
|
2016-01-12 03:41:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-16 00:05:14 +00:00
|
|
|
void Players::newPlayer(RakNet::RakNetGUID guid)
|
2016-01-12 03:41:44 +00:00
|
|
|
{
|
2017-02-27 09:13:07 +00:00
|
|
|
LOG_MESSAGE_SIMPLE(Log::LOG_INFO, "Creating new player with guid %lu", guid.g);
|
2016-08-18 22:32:39 +00:00
|
|
|
|
2016-11-17 05:11:42 +00:00
|
|
|
players[guid] = new Player(guid);
|
2017-01-25 15:06:15 +00:00
|
|
|
players[guid]->cell.blank();
|
|
|
|
players[guid]->npc.blank();
|
|
|
|
players[guid]->npcStats.blank();
|
|
|
|
players[guid]->creatureStats.blank();
|
2016-11-17 05:11:42 +00:00
|
|
|
players[guid]->charClass.blank();
|
2016-01-12 03:41:44 +00:00
|
|
|
|
2016-12-21 08:02:08 +00:00
|
|
|
for (unsigned int i = 0; i < mwmp::Networking::get().maxConnections(); i++)
|
2016-01-12 03:41:44 +00:00
|
|
|
{
|
2016-08-17 15:20:36 +00:00
|
|
|
if (slots[i] == 0)
|
2016-01-12 03:41:44 +00:00
|
|
|
{
|
2017-02-27 09:13:07 +00:00
|
|
|
LOG_APPEND(Log::LOG_INFO, "- Storing in slot %i", i);
|
2016-08-18 22:32:39 +00:00
|
|
|
|
2016-11-17 05:11:42 +00:00
|
|
|
slots[i] = players[guid];
|
2016-11-16 00:05:14 +00:00
|
|
|
slots[i]->setId(i);
|
2016-01-12 03:41:44 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-16 00:05:14 +00:00
|
|
|
Player *Players::getPlayer(RakNet::RakNetGUID guid)
|
2016-01-12 03:41:44 +00:00
|
|
|
{
|
2017-02-19 14:53:15 +00:00
|
|
|
if (players.count(guid) == 0)
|
2017-01-10 10:31:51 +00:00
|
|
|
return nullptr;
|
2016-11-17 05:11:42 +00:00
|
|
|
return players[guid];
|
2016-01-12 03:41:44 +00:00
|
|
|
}
|
|
|
|
|
2016-11-17 05:11:42 +00:00
|
|
|
TPlayers *Players::getPlayers()
|
2016-01-12 03:41:44 +00:00
|
|
|
{
|
|
|
|
return &players;
|
|
|
|
}
|
|
|
|
|
2017-02-26 21:00:51 +00:00
|
|
|
unsigned short Players::getLastPlayerId()
|
|
|
|
{
|
|
|
|
return slots.rbegin()->first;
|
|
|
|
}
|
|
|
|
|
2016-10-26 12:55:34 +00:00
|
|
|
Player::Player(RakNet::RakNetGUID guid) : BasePlayer(guid)
|
2016-01-12 03:41:44 +00:00
|
|
|
{
|
2016-11-16 00:05:14 +00:00
|
|
|
handshakeState = false;
|
2016-12-21 08:02:08 +00:00
|
|
|
loadState = NOTLOADED;
|
2016-01-12 03:41:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Player::~Player()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-11-16 00:05:14 +00:00
|
|
|
unsigned short Player::getId()
|
2016-01-12 03:41:44 +00:00
|
|
|
{
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
2016-11-16 00:05:14 +00:00
|
|
|
void Player::setId(unsigned short id)
|
2016-01-12 03:41:44 +00:00
|
|
|
{
|
|
|
|
this->id = id;
|
|
|
|
}
|
|
|
|
|
2016-11-16 00:05:14 +00:00
|
|
|
void Player::setHandshake()
|
2016-01-12 03:41:44 +00:00
|
|
|
{
|
2016-11-16 00:05:14 +00:00
|
|
|
handshakeState = true;
|
2016-01-12 03:41:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Player::isHandshaked()
|
|
|
|
{
|
2016-11-16 00:05:14 +00:00
|
|
|
return handshakeState;
|
2016-01-12 03:41:44 +00:00
|
|
|
}
|
|
|
|
|
2016-11-16 00:05:14 +00:00
|
|
|
void Player::setLoadState(int state)
|
2016-09-18 03:48:08 +00:00
|
|
|
{
|
2016-11-16 00:05:14 +00:00
|
|
|
loadState = state;
|
2016-09-18 03:48:08 +00:00
|
|
|
}
|
|
|
|
|
2016-11-16 00:05:14 +00:00
|
|
|
int Player::getLoadState()
|
2016-09-18 03:48:08 +00:00
|
|
|
{
|
2016-11-16 00:05:14 +00:00
|
|
|
return loadState;
|
2016-09-18 03:48:08 +00:00
|
|
|
}
|
2016-01-12 03:41:44 +00:00
|
|
|
|
2016-11-16 00:05:14 +00:00
|
|
|
Player *Players::getPlayer(unsigned short id)
|
2016-01-12 03:41:44 +00:00
|
|
|
{
|
2016-10-08 07:15:43 +00:00
|
|
|
if (slots.find(id) == slots.end())
|
|
|
|
return nullptr;
|
2016-01-12 03:41:44 +00:00
|
|
|
return slots[id];
|
|
|
|
}
|
2016-10-08 07:15:43 +00:00
|
|
|
|
2017-02-20 12:43:51 +00:00
|
|
|
CellController::TContainer *Player::getCells()
|
2017-02-19 05:26:42 +00:00
|
|
|
{
|
2017-02-20 12:43:51 +00:00
|
|
|
return &cells;
|
2017-02-19 05:26:42 +00:00
|
|
|
}
|
2017-02-19 08:38:47 +00:00
|
|
|
|
2017-02-19 12:37:26 +00:00
|
|
|
void Player::sendToLoaded(mwmp::PlayerPacket *myPacket)
|
2017-02-19 08:38:47 +00:00
|
|
|
{
|
2017-02-19 10:44:52 +00:00
|
|
|
std::list <Player*> plList;
|
|
|
|
|
2017-02-20 12:46:20 +00:00
|
|
|
for (auto cell : cells)
|
2017-02-19 10:44:52 +00:00
|
|
|
for (auto pl : *cell)
|
|
|
|
plList.push_back(pl);
|
2017-02-19 08:38:47 +00:00
|
|
|
|
2017-02-19 10:44:52 +00:00
|
|
|
plList.sort();
|
|
|
|
plList.unique();
|
|
|
|
|
2017-02-19 14:53:15 +00:00
|
|
|
for (auto pl : plList)
|
2017-02-19 10:44:52 +00:00
|
|
|
{
|
2017-02-19 14:53:15 +00:00
|
|
|
if (pl == this) continue;
|
2017-03-06 09:44:08 +00:00
|
|
|
myPacket->setPlayer(this);
|
2017-04-06 05:39:11 +00:00
|
|
|
myPacket->Send(pl->guid);
|
2017-02-19 08:38:47 +00:00
|
|
|
}
|
|
|
|
}
|
2017-02-19 09:42:39 +00:00
|
|
|
|
2017-02-19 13:32:25 +00:00
|
|
|
void Player::forEachLoaded(std::function<void(Player *pl, Player *other)> func)
|
2017-02-19 09:42:39 +00:00
|
|
|
{
|
2017-02-19 10:44:52 +00:00
|
|
|
std::list <Player*> plList;
|
|
|
|
|
2017-02-20 12:46:20 +00:00
|
|
|
for (auto cell : cells)
|
2017-02-19 10:44:52 +00:00
|
|
|
for (auto pl : *cell)
|
|
|
|
plList.push_back(pl);
|
2017-02-19 09:42:39 +00:00
|
|
|
|
2017-02-19 10:44:52 +00:00
|
|
|
plList.sort();
|
|
|
|
plList.unique();
|
|
|
|
|
2017-02-19 14:53:15 +00:00
|
|
|
for (auto pl : plList)
|
2017-02-19 10:44:52 +00:00
|
|
|
{
|
2017-02-19 14:53:15 +00:00
|
|
|
if (pl == this) continue;
|
2017-02-19 10:44:52 +00:00
|
|
|
func(this, pl);
|
2017-02-19 09:42:39 +00:00
|
|
|
}
|
|
|
|
}
|