1
0
Fork 1
mirror of https://github.com/TES3MP/openmw-tes3mp.git synced 2025-01-15 23:49:55 +00:00
openmw-tes3mp/apps/openmw-mp/Player.cpp

181 lines
3.2 KiB
C++
Raw Normal View History

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