2017-02-19 05:26:42 +00:00
//
// Created by koncord on 18.02.17.
//
# include "Cell.hpp"
# include <iostream>
# include "Player.hpp"
using namespace std ;
2017-02-19 08:07:44 +00:00
void Cell : : addPlayer ( Player * player )
2017-02-19 05:26:42 +00:00
{
auto it = find ( player - > cells . begin ( ) , player - > cells . end ( ) , this ) ;
2017-02-19 14:53:15 +00:00
if ( it = = player - > cells . end ( ) )
2017-02-19 05:26:42 +00:00
player - > cells . push_back ( this ) ;
players . push_back ( player ) ;
}
2017-02-19 08:07:44 +00:00
void Cell : : removePlayer ( Player * player )
2017-02-19 05:26:42 +00:00
{
2017-02-19 14:53:15 +00:00
for ( Iterator it = begin ( ) ; it ! = end ( ) ; it + + )
2017-02-19 05:26:42 +00:00
{
2017-02-19 14:53:15 +00:00
if ( * it = = player )
2017-02-19 05:26:42 +00:00
{
auto it2 = find ( player - > cells . begin ( ) , player - > cells . end ( ) , this ) ;
2017-02-19 14:53:15 +00:00
if ( it2 ! = player - > cells . end ( ) )
2017-02-19 05:26:42 +00:00
player - > cells . erase ( it2 ) ;
players . erase ( it ) ;
return ;
}
}
}
2017-02-24 04:33:59 +00:00
Cell : : TPlayers Cell : : getPlayers ( ) const
2017-02-19 05:26:42 +00:00
{
return players ;
}
2017-02-24 04:33:59 +00:00
void Cell : : sendToLoaded ( mwmp : : WorldPacket * worldPacket , mwmp : : BaseEvent * baseEvent ) const
2017-02-19 17:42:16 +00:00
{
2017-02-25 10:44:20 +00:00
if ( players . empty ( ) )
{
// Remove this once we are sure it can't happen
LOG_MESSAGE_SIMPLE ( Log : : LOG_WARN , " - Attempt to send packet to players in Cell::sendToLoaded when there are no players! \n - Please report to a developer " ) ;
return ;
}
2017-02-19 17:42:16 +00:00
std : : list < Player * > plList ;
2017-02-24 04:33:59 +00:00
for ( auto pl : players )
2017-02-19 17:42:16 +00:00
plList . push_back ( pl ) ;
plList . sort ( ) ;
plList . unique ( ) ;
for ( auto pl : plList )
{
if ( pl - > guid = = baseEvent - > guid ) continue ;
2017-02-19 18:20:44 +00:00
worldPacket - > Send ( baseEvent , pl - > guid ) ;
2017-02-19 17:42:16 +00:00
}
}
2017-02-19 15:27:38 +00:00
std : : string Cell : : getDescription ( ) const
{
return cell . getDescription ( ) ;
}
2017-02-19 05:26:42 +00:00
CellController : : CellController ( )
{
}
CellController : : ~ CellController ( )
{
2017-02-24 05:58:40 +00:00
for ( auto cell : cells )
{
delete cell ;
}
2017-02-19 05:26:42 +00:00
}
CellController * CellController : : sThis = nullptr ;
2017-02-19 08:07:44 +00:00
void CellController : : create ( )
2017-02-19 05:26:42 +00:00
{
2017-02-24 04:33:59 +00:00
assert ( ! sThis ) ;
2017-02-19 05:26:42 +00:00
sThis = new CellController ;
}
2017-02-19 08:07:44 +00:00
void CellController : : destroy ( )
2017-02-19 05:26:42 +00:00
{
assert ( sThis ) ;
delete sThis ;
sThis = nullptr ;
}
2017-02-19 08:07:44 +00:00
CellController * CellController : : get ( )
2017-02-19 05:26:42 +00:00
{
2017-02-24 04:33:59 +00:00
assert ( sThis ) ;
2017-02-19 05:26:42 +00:00
return sThis ;
}
2017-02-19 17:39:53 +00:00
Cell * CellController : : getCell ( ESM : : Cell * esmCell )
{
if ( esmCell - > isExterior ( ) )
return getCellByXY ( esmCell - > mData . mX , esmCell - > mData . mY ) ;
else
return getCellByName ( esmCell - > mName ) ;
}
2017-02-19 08:07:44 +00:00
Cell * CellController : : getCellByXY ( int x , int y )
2017-02-19 05:26:42 +00:00
{
auto it = find_if ( cells . begin ( ) , cells . end ( ) , [ x , y ] ( const Cell * c ) {
return c - > cell . mData . mX = = x & & c - > cell . mData . mY = = y ;
} ) ;
2017-02-19 14:53:15 +00:00
if ( it = = cells . end ( ) )
2017-02-19 05:26:42 +00:00
return nullptr ;
return * it ;
}
2017-02-19 16:37:20 +00:00
Cell * CellController : : getCellByName ( std : : string cellName )
2017-02-19 05:26:42 +00:00
{
2017-02-19 16:37:20 +00:00
auto it = find_if ( cells . begin ( ) , cells . end ( ) , [ cellName ] ( const Cell * c ) {
return c - > cell . mName = = cellName ;
2017-02-19 05:26:42 +00:00
} ) ;
2017-02-19 14:53:15 +00:00
if ( it = = cells . end ( ) )
2017-02-19 05:26:42 +00:00
return nullptr ;
return * it ;
}
2017-02-19 08:07:44 +00:00
Cell * CellController : : addCell ( ESM : : Cell cellData )
2017-02-19 05:26:42 +00:00
{
2017-02-19 11:29:14 +00:00
LOG_MESSAGE_SIMPLE ( Log : : LOG_VERBOSE , " Loaded cells: %d " , cells . size ( ) ) ;
2017-02-19 05:26:42 +00:00
auto it = find_if ( cells . begin ( ) , cells . end ( ) , [ cellData ] ( const Cell * c ) {
//return c->cell.sRecordId == cellData.sRecordId; // Currently we cannot compare because plugin lists can be loaded in different order
2017-02-25 08:46:57 +00:00
return c - > cell . isExterior ( ) ? ( c - > cell . mData . mX = = cellData . mData . mX & & c - > cell . mData . mY = = cellData . mData . mY ) :
( c - > cell . mName = = cellData . mName ) ;
2017-02-19 05:26:42 +00:00
} ) ;
Cell * cell ;
2017-02-19 14:53:15 +00:00
if ( it = = cells . end ( ) )
2017-02-19 05:26:42 +00:00
{
cell = new Cell ( cellData ) ;
cells . push_back ( cell ) ;
}
else
cell = * it ;
return cell ;
}
2017-02-19 08:07:44 +00:00
void CellController : : removeCell ( Cell * cell )
2017-02-19 05:26:42 +00:00
{
2017-02-19 14:53:15 +00:00
if ( cell = = nullptr )
2017-02-19 05:26:42 +00:00
return ;
for ( auto it = cells . begin ( ) ; it ! = cells . end ( ) ; )
{
2017-02-19 14:53:15 +00:00
if ( * it ! = nullptr & & * it = = cell )
2017-02-19 05:26:42 +00:00
{
delete * it ;
it = cells . erase ( it ) ;
}
else
+ + it ;
}
}
2017-02-19 08:07:44 +00:00
void CellController : : removePlayer ( Cell * cell , Player * player )
2017-02-19 05:26:42 +00:00
{
2017-02-19 08:07:44 +00:00
cell - > removePlayer ( player ) ;
2017-02-19 05:26:42 +00:00
2017-02-19 14:53:15 +00:00
if ( cell - > players . empty ( ) )
2017-02-19 05:26:42 +00:00
{
2017-02-21 10:14:02 +00:00
LOG_MESSAGE_SIMPLE ( Log : : LOG_VERBOSE , " Deleting empty cell from memory: %s " , player - > npc . mName . c_str ( ) ,
player - > getId ( ) , cell - > cell . getDescription ( ) . c_str ( ) ) ;
2017-02-19 05:26:42 +00:00
auto it = find ( cells . begin ( ) , cells . end ( ) , cell ) ;
delete * it ;
cells . erase ( it ) ;
}
}
2017-02-19 14:27:00 +00:00
void CellController : : deletePlayer ( Player * player )
{
2017-02-19 23:47:39 +00:00
2017-02-20 12:43:51 +00:00
for_each ( player - > getCells ( ) - > begin ( ) , player - > getCells ( ) - > end ( ) , [ & player ] ( Cell * cell ) {
2017-02-19 14:27:00 +00:00
for ( auto it = cell - > begin ( ) ; it ! = cell - > end ( ) ; + + it )
{
if ( * it = = player )
{
cell - > players . erase ( it ) ;
break ;
}
}
} ) ;
}
2017-02-19 05:26:42 +00:00
void CellController : : update ( Player * player )
{
2017-02-19 14:53:15 +00:00
for ( auto cell : player - > cellStateChanges . cellStates )
2017-02-19 05:26:42 +00:00
{
2017-02-19 14:53:15 +00:00
if ( cell . type = = mwmp : : CellState : : LOAD )
2017-02-19 05:26:42 +00:00
{
2017-02-19 08:07:44 +00:00
Cell * c = addCell ( cell . cell ) ;
c - > addPlayer ( player ) ;
2017-02-19 05:26:42 +00:00
}
else
{
2017-02-20 11:45:35 +00:00
LOG_MESSAGE_SIMPLE ( Log : : LOG_VERBOSE , " Player %s (%d) unloaded cell: %s " , player - > npc . mName . c_str ( ) , player - > getId ( ) , cell . cell . getDescription ( ) . c_str ( ) ) ;
2017-02-19 05:26:42 +00:00
Cell * c ;
2017-02-19 14:53:15 +00:00
if ( ! cell . cell . isExterior ( ) )
2017-02-19 16:37:20 +00:00
c = getCellByName ( cell . cell . mName ) ;
2017-02-19 05:26:42 +00:00
else
2017-02-19 08:07:44 +00:00
c = getCellByXY ( cell . cell . getGridX ( ) , cell . cell . getGridY ( ) ) ;
2017-02-19 05:26:42 +00:00
2017-02-19 14:53:15 +00:00
if ( c ! = nullptr )
2017-02-19 08:07:44 +00:00
removePlayer ( c , player ) ;
2017-02-19 05:26:42 +00:00
}
}
}
Cell : : Cell ( ESM : : Cell cell ) : cell ( cell )
{
}
2017-02-19 08:21:40 +00:00
2017-02-22 04:03:03 +00:00
Cell : : Iterator Cell : : begin ( ) const
2017-02-19 08:21:40 +00:00
{
return players . begin ( ) ;
}
2017-02-22 04:03:03 +00:00
Cell : : Iterator Cell : : end ( ) const
2017-02-19 08:21:40 +00:00
{
return players . end ( ) ;
}