1
Fork 0
mirror of https://github.com/SapphireServer/Sapphire.git synced 2025-04-26 14:37:44 +00:00
sapphire/src/world/Territory/HousingZone.cpp

401 lines
12 KiB
C++
Raw Normal View History

2018-07-15 23:59:15 +02:00
#include <Common.h>
#include <Logging/Logger.h>
#include <Util/Util.h>
#include <Util/UtilMath.h>
2018-11-01 00:18:19 +01:00
#include <Database/DatabaseDef.h>
#include <Exd/ExdData.h>
2019-03-08 15:34:38 +01:00
#include <Network/GamePacket.h>
2018-07-15 23:59:15 +02:00
#include <Network/PacketDef/Zone/ServerZoneDef.h>
#include <Network/PacketWrappers/ActorControlSelfPacket.h>
#include <Network/CommonActorControl.h>
2020-03-01 01:00:57 +11:00
#include <Service.h>
2018-07-15 23:59:15 +02:00
#include "Actor/Player.h"
#include "Actor/GameObject.h"
#include "Actor/EventObject.h"
2018-11-01 23:56:43 +01:00
#include "Land.h"
2018-11-25 01:55:53 +11:00
#include "House.h"
2018-12-26 00:43:27 +11:00
#include "Inventory/HousingItem.h"
#include "Inventory/ItemContainer.h"
#include "WorldServer.h"
2018-07-15 23:59:15 +02:00
#include "Forwards.h"
#include "HousingZone.h"
#include "Manager/HousingMgr.h"
2018-07-15 23:59:15 +02:00
using namespace Sapphire::Common;
using namespace Sapphire::Network::Packets;
using namespace Sapphire::Network::Packets::WorldPackets::Server;
using namespace Sapphire::World::Manager;
2018-07-15 23:59:15 +02:00
Sapphire::HousingZone::HousingZone( uint8_t wardNum, uint16_t territoryTypeId, uint32_t guId,
const std::string& internalName, const std::string& contentName ) :
2020-03-01 01:00:57 +11:00
Territory( territoryTypeId, guId, internalName, contentName ),
2018-11-04 23:47:10 +01:00
m_wardNum( wardNum ),
m_territoryTypeId( territoryTypeId ),
2020-03-01 01:00:57 +11:00
m_landSetId( ( static_cast< uint32_t >( territoryTypeId ) << 16 ) | wardNum )
2018-07-15 23:59:15 +02:00
{
}
bool Sapphire::HousingZone::init()
2018-07-15 23:59:15 +02:00
{
2018-11-04 23:47:10 +01:00
2020-03-01 01:00:57 +11:00
auto& db = Common::Service< Db::DbWorkerPool< Db::ZoneDbConnection > >::ref();
2018-11-04 23:47:10 +01:00
{
2020-03-01 01:00:57 +11:00
auto res = db.query( "SELECT * FROM landset WHERE landsetid = " + std::to_string( m_landSetId ) );
if( !res->next() )
{
2020-03-01 01:00:57 +11:00
db.directExecute( "INSERT INTO landset ( landsetid ) VALUES ( " + std::to_string( m_landSetId ) + " );" );
}
2018-11-04 23:47:10 +01:00
}
2018-11-04 23:47:10 +01:00
int housingIndex;
if( m_territoryTypeId == 339 )
2018-11-04 23:47:10 +01:00
housingIndex = 0;
else if( m_territoryTypeId == 340 )
2018-11-04 23:47:10 +01:00
housingIndex = 1;
else if( m_territoryTypeId == 341 )
2018-11-04 23:47:10 +01:00
housingIndex = 2;
else if( m_territoryTypeId == 641 )
2018-11-04 23:47:10 +01:00
housingIndex = 3;
auto& exdData = Common::Service< Data::ExdData >::ref();
auto info = exdData.getRow< Component::Excel::HousingLandSet >( housingIndex );
2018-11-04 23:47:10 +01:00
2018-12-26 00:43:27 +11:00
// build yard objects array indices
int16_t cursor = -1;
uint16_t index = 0;
for( const auto entry : info->data().Lands )
2018-12-26 00:43:27 +11:00
{
uint16_t itemMax = 0;
switch( entry.Size )
2018-12-26 00:43:27 +11:00
{
case 0:
itemMax = 20;
break;
case 1:
itemMax = 30;
break;
case 2:
itemMax = 40;
break;
}
int16_t start = cursor + 1;
int16_t end = cursor + itemMax;
m_yardObjectArrayBounds[ index++ ] = std::make_pair( start, end );
// reset cursor for subdivision
if( index == 30 )
{
cursor = -1;
continue;
}
cursor += itemMax;
}
2018-12-26 22:39:00 +11:00
// zero out the yard obj arrays so we don't leak memory like SE does :^)
Common::Furniture obj {};
memset( &obj, 0x0, sizeof( Common::Furniture ) );
2018-12-26 22:39:00 +11:00
for( auto& arr : m_yardObjects )
{
memcpy( &arr, &m_yardObjects, sizeof( Common::Furniture ) );
2018-12-26 22:39:00 +11:00
}
2018-12-26 00:43:27 +11:00
2020-03-01 01:00:57 +11:00
auto& housingMgr = Common::Service< World::Manager::HousingMgr >::ref();
auto landCache = housingMgr.getLandCacheMap();
// make sure the landset exists
auto landSetCache = landCache.find( m_landSetId );
if( landSetCache == landCache.end() )
{
Logger::fatal( "LandSet {0} is missing from the land cache.", m_landSetId );
return false;
}
2018-07-15 23:59:15 +02:00
// init the lands
for( HousingMgr::LandCacheEntry& entry : landSetCache->second )
{
2020-03-01 01:00:57 +11:00
auto land = make_Land( m_territoryTypeId, getWardNum(), entry.m_landId, m_landSetId, info );
// setup house
if( entry.m_houseId )
{
2018-12-26 00:43:27 +11:00
auto house = make_House( entry.m_houseId, m_landSetId, land->getLandIdent(), entry.m_estateName,
2020-03-01 01:00:57 +11:00
entry.m_estateComment );
2018-12-22 15:58:54 +11:00
2020-03-01 01:00:57 +11:00
housingMgr.updateHouseModels( house );
2018-12-22 15:58:54 +11:00
land->setHouse( house );
}
land->init( entry.m_type, entry.m_size, entry.m_status, entry.m_currentPrice, entry.m_ownerId, entry.m_houseId );
m_landPtrMap[ entry.m_landId ] = land;
2018-07-15 23:59:15 +02:00
// TODO: Fixme
// if( entry.m_houseId > 0 )
// registerEstateEntranceEObj( entry.m_landId );
2018-12-26 00:43:27 +11:00
2018-12-26 22:39:00 +11:00
updateYardObjects( land->getLandIdent() );
}
return true;
2018-07-15 23:59:15 +02:00
}
void Sapphire::HousingZone::onPlayerZoneIn( Entity::Player& player )
2018-07-15 23:59:15 +02:00
{
auto& server = Common::Service< World::WorldServer >::ref();
2019-07-21 22:33:33 +10:00
Logger::debug( "HousingZone::onPlayerZoneIn: Territory#{0}|{1}, Entity#{2}",
getGuId(), getTerritoryTypeId(), player.getId() );
2018-07-15 23:59:15 +02:00
2018-12-25 23:43:39 +11:00
uint32_t yardPacketNum;
2018-07-15 23:59:15 +02:00
2018-11-04 23:47:10 +01:00
sendLandSet( player );
2018-07-15 23:59:15 +02:00
for( yardPacketNum = 1; yardPacketNum > 2; yardPacketNum++ )
{
auto housingObjectInit = makeZonePacket< FFXIVIpcYardObjectList >( player.getId() );
housingObjectInit->data().PacketIndex = yardPacketNum;
2018-11-01 00:18:19 +01:00
auto yardObjectSize = sizeof( Common::Furniture );
2018-12-25 23:43:39 +11:00
auto& objects = m_yardObjects[ 0 ];
2018-12-25 23:43:39 +11:00
memcpy( &housingObjectInit->data().YardObjects, &objects + ( yardPacketNum * 400 ), yardObjectSize * 400 );
2018-11-01 00:18:19 +01:00
server.queueForPlayer( player.getCharacterId(), housingObjectInit );
}
2018-11-01 00:18:19 +01:00
}
2018-07-15 23:59:15 +02:00
void Sapphire::HousingZone::sendLandSet( Entity::Player& player )
2018-11-01 00:18:19 +01:00
{
auto& server = Common::Service< World::WorldServer >::ref();
auto landsetInitializePacket = makeZonePacket< FFXIVIpcHouseList >( player.getId() );
2018-07-16 12:37:04 +02:00
landsetInitializePacket->data().LandSetId.wardNum = m_wardNum;
landsetInitializePacket->data().LandSetId.landId = m_landSetId;
landsetInitializePacket->data().LandSetId.territoryTypeId = m_territoryTypeId;
2018-11-01 00:18:19 +01:00
//TODO: get current WorldId
landsetInitializePacket->data().LandSetId.worldId = 67;
2018-07-15 23:59:15 +02:00
for( uint8_t i = 0, count = 0; i < 30; ++i, ++count )
2018-11-01 00:18:19 +01:00
{
auto pLand = getLand( i );
// todo: move this and sendLandUpdate building logic to its own function
auto& landData = landsetInitializePacket->data().Houses[ count ];
landData.size = pLand->getSize();
landData.status = pLand->getStatus();
landData.flags = pLand->getSharing();
/* //disbaled until we managed fc's
landData.fcCrestId = pLand->getFcId();
landData.fcIcon = pLand->getFcIcon();
landData.fcIconColor = pLand->getFcColor();
*/
if( auto house = pLand->getHouse() )
{
landData.flags = 1;
2018-12-22 15:58:54 +11:00
auto& parts = house->getHouseModels();
for( auto ii = 0; ii != parts.size(); ii++ )
{
landData.patternIds[ ii ] = parts[ ii ].first;
landData.colors[ ii ] = parts[ ii ].second;
}
}
}
2018-07-15 23:59:15 +02:00
server.queueForPlayer( player.getCharacterId(), landsetInitializePacket );
2018-11-01 00:18:19 +01:00
}
void Sapphire::HousingZone::sendLandUpdate( uint8_t landId )
2018-11-07 11:56:49 +01:00
{
auto& server = Common::Service< World::WorldServer >::ref();
auto pLand = getLand( landId );
2018-11-07 11:56:49 +01:00
for( const auto& playerIt : m_playerMap )
{
auto pPlayer = playerIt.second;
auto landUpdatePacket = makeZonePacket< FFXIVIpcHouse >( pPlayer->getId() );
landUpdatePacket->data().Block = landId;
auto& landData = landUpdatePacket->data().House;
landData.size = pLand->getSize();
landData.status = pLand->getStatus();
landData.flags = pLand->getSharing();
/*
landData.fcId = pLand->getFcId();
landData.fcIcon = pLand->getFcIcon();
landData.fcIconColor = pLand->getFcColor();
*/
2018-11-07 11:56:49 +01:00
2018-11-25 01:55:53 +11:00
if( auto house = pLand->getHouse() )
{
landData.flags = 1;
2018-12-22 15:58:54 +11:00
auto& parts = house->getHouseModels();
for( auto i = 0; i != parts.size(); i++ )
2018-11-25 01:55:53 +11:00
{
landData.patternIds[ i ] = parts[ i ].first;
landData.colors[ i ] = parts[ i ].second;
2018-11-25 01:55:53 +11:00
}
}
server.queueForPlayer( pPlayer->getCharacterId(), landUpdatePacket );
2018-11-07 11:56:49 +01:00
}
}
void Sapphire::HousingZone::onUpdate( uint64_t tickCount )
2018-11-01 00:18:19 +01:00
{
for( auto pLandItr : m_landPtrMap )
2018-11-01 00:18:19 +01:00
{
pLandItr.second->update( tickCount );
2018-11-01 00:18:19 +01:00
}
2018-07-15 23:59:15 +02:00
}
uint8_t Sapphire::HousingZone::getWardNum() const
2018-11-04 23:47:10 +01:00
{
return m_wardNum;
}
uint32_t Sapphire::HousingZone::getLandSetId() const
2018-07-15 23:59:15 +02:00
{
2018-11-01 23:56:43 +01:00
return m_landSetId;
2018-07-15 23:59:15 +02:00
}
2018-11-01 00:18:19 +01:00
Sapphire::LandPtr Sapphire::HousingZone::getLand( uint8_t id )
2018-11-01 00:18:19 +01:00
{
2018-11-01 23:56:43 +01:00
auto it = m_landPtrMap.find( id );
if( it == m_landPtrMap.end() )
2018-11-01 00:18:19 +01:00
return nullptr;
return it->second;
}
2018-12-25 23:43:39 +11:00
Sapphire::Entity::EventObjectPtr Sapphire::HousingZone::registerEstateEntranceEObj( uint8_t landId )
{
2018-12-25 23:43:39 +11:00
auto land = getLand( landId );
assert( land );
auto eObj = Entity::make_EventObject( getNextEObjId(), 2002737, 0, 4, land->getMapMarkerPosition(), 0.f, "entrance" );
2018-12-25 23:43:39 +11:00
eObj->setHousingLink( landId << 8 );
eObj->setScale( 1.f );
registerEObj( eObj );
return eObj;
2018-12-26 00:43:27 +11:00
}
void Sapphire::HousingZone::updateYardObjects( Sapphire::Common::LandIdent ident )
{
2020-03-01 01:00:57 +11:00
auto& housingMgr = Common::Service< World::Manager::HousingMgr >::ref();
auto& landStorage = housingMgr.getEstateInventory( ident );
2018-12-26 00:43:27 +11:00
auto yardContainer = landStorage[ InventoryType::HousingExteriorPlacedItems ];
2018-12-26 22:39:00 +11:00
auto arrayBounds = m_yardObjectArrayBounds[ ident.landId ];
2018-12-26 00:43:27 +11:00
for( const auto& item : yardContainer->getItemMap() )
{
2018-12-26 22:39:00 +11:00
auto housingItem = std::dynamic_pointer_cast< Inventory::HousingItem >( item.second );
assert( housingItem );
2018-12-26 00:43:27 +11:00
2018-12-26 22:39:00 +11:00
auto idx = item.first + arrayBounds.first;
m_yardObjects[ idx ] = housingMgr.getYardObjectForItem( housingItem );
2018-12-26 00:43:27 +11:00
}
2018-12-26 18:11:18 +11:00
}
void Sapphire::HousingZone::spawnYardObject( uint8_t landId, uint16_t slotId, Inventory::HousingItem& item )
2018-12-26 18:11:18 +11:00
{
auto& server = Common::Service< World::WorldServer >::ref();
2018-12-26 18:11:18 +11:00
auto bounds = m_yardObjectArrayBounds[ landId ];
auto offset = bounds.first + slotId;
Common::Furniture obj {};
2018-12-26 18:11:18 +11:00
obj.patternId = item.getAdditionalData();
obj.dir = Util::floatToUInt16Rot( item.getRot() );
2018-12-26 18:11:18 +11:00
obj.pos[ 0 ] = Util::floatToUInt16( item.getPos().x );
obj.pos[ 1 ] = Util::floatToUInt16( item.getPos().y );
obj.pos[ 2 ] = Util::floatToUInt16( item.getPos().z );
2018-12-26 18:11:18 +11:00
// link obj
m_yardObjects[ offset ] = obj;
2018-12-26 18:11:18 +11:00
// spawn obj in zone
for( const auto& player : m_playerMap )
{
auto packet = makeZonePacket< FFXIVIpcYardObject >( player.second->getId() );
2018-12-26 18:11:18 +11:00
packet->data().YardObject = obj;
2018-12-26 18:11:18 +11:00
server.queueForPlayer( player.second->getCharacterId(), packet );
}
}
void Sapphire::HousingZone::updateYardObjectPos( Entity::Player& sourcePlayer, uint16_t slot, uint16_t landId,
Inventory::HousingItem& item )
{
auto& server = Common::Service< World::WorldServer >::ref();
auto bounds = m_yardObjectArrayBounds[ landId ];
auto offset = bounds.first + slot;
auto& obj = m_yardObjects[ offset ];
obj.dir = Util::floatToUInt16( item.getRot() );
obj.pos[ 0 ] = Util::floatToUInt16( item.getPos().x );
obj.pos[ 1 ] = Util::floatToUInt16( item.getPos().y );
obj.pos[ 2 ] = Util::floatToUInt16( item.getPos().z );
for( const auto& player : m_playerMap )
{
// don't send it from the origin player, it already has the position from the move request
if( player.second->getId() == sourcePlayer.getId() )
continue;
auto packet = makeZonePacket< FFXIVIpcHousingObjectTransform >( player.second->getId() );
packet->data().Dir = Util::floatToUInt16( item.getRot() );
packet->data().Pos[ 0 ] = Util::floatToUInt16( item.getPos().x );
packet->data().Pos[ 1 ] = Util::floatToUInt16( item.getPos().y );
packet->data().Pos[ 2 ] = Util::floatToUInt16( item.getPos().z );
packet->data().UserData1 = static_cast< uint8_t >( landId );
packet->data().UserData2 = static_cast< uint8_t >( slot );
server.queueForPlayer( player.second->getCharacterId(), packet );
2018-12-26 18:11:18 +11:00
}
}
void Sapphire::HousingZone::despawnYardObject( uint16_t landId, uint16_t slot )
{
auto& server = Common::Service< World::WorldServer >::ref();
auto bounds = m_yardObjectArrayBounds[ landId ];
auto offset = bounds.first + slot;
memset( &m_yardObjects[ offset ], 0x00, sizeof( Common::Furniture ) );
for( const auto& player : m_playerMap )
{
auto param = ( landId << 16 ) | slot;
auto pkt = makeActorControlSelf( player.second->getId(), Network::ActorControl::RemoveExteriorHousingItem, param );
server.queueForPlayer( player.second->getCharacterId(), pkt );
}
2019-03-08 15:34:38 +01:00
}