<commit_before>#include "TerritoryMgr.h"

#include <common/Logging/Logger.h>
#include <common/Database/DatabaseDef.h>
#include <common/Exd/ExdDataGenerated.h>

#include "Actor/Player.h"

#include "Zone.h"
#include "ZonePosition.h"
#include "InstanceContent.h"

extern Core::Logger g_log;
extern Core::Data::ExdData g_exdData;
extern Core::Data::ExdDataGenerated g_exdDataGen;

Core::TerritoryMgr::TerritoryMgr() :
   m_lastInstanceId( 10000 )
{

}

void Core::TerritoryMgr::loadTerritoryTypeDetailCache()
{
   auto idList = g_exdDataGen.getTerritoryTypeIdList();

   for( auto id : idList )
   {
      auto teri1 = g_exdDataGen.getTerritoryType( id );

      if( !teri1->name.empty() )
         m_territoryTypeDetailCacheMap[id] = teri1;
   }
}

bool Core::TerritoryMgr::isValidTerritory( uint32_t territoryTypeId ) const
{
   return !( m_territoryTypeDetailCacheMap.find( territoryTypeId ) == m_territoryTypeDetailCacheMap.end() );
}

bool Core::TerritoryMgr::init()
{
   loadTerritoryTypeDetailCache();
   loadTerritoryPositionMap();

   createDefaultTerritories();

   return true;
}

uint32_t Core::TerritoryMgr::getNextInstanceId()
{
   return ++m_lastInstanceId;
}

Core::Data::TerritoryTypePtr Core::TerritoryMgr::getTerritoryDetail( uint32_t territoryTypeId ) const
{
   auto tIt = m_territoryTypeDetailCacheMap.find( territoryTypeId );
   if( tIt == m_territoryTypeDetailCacheMap.end() )
      return nullptr;

   return tIt->second;
}

bool Core::TerritoryMgr::isInstanceContentTerritory( uint32_t territoryTypeId ) const
{
   auto pTeri = getTerritoryDetail( territoryTypeId );

   if( !pTeri )
      return false;

   return pTeri->territoryIntendedUse == TerritoryIntendedUse::AllianceRaid ||
          pTeri->territoryIntendedUse == TerritoryIntendedUse::BeforeTrialDung ||
          pTeri->territoryIntendedUse == TerritoryIntendedUse::Trial ||
          pTeri->territoryIntendedUse == TerritoryIntendedUse::Dungeon ||
          pTeri->territoryIntendedUse == TerritoryIntendedUse::OpenWorldInstanceBattle ||
          pTeri->territoryIntendedUse == TerritoryIntendedUse::PalaceOfTheDead ||
          pTeri->territoryIntendedUse == TerritoryIntendedUse::RaidFights ||
          pTeri->territoryIntendedUse == TerritoryIntendedUse::Raids ||
          pTeri->territoryIntendedUse == TerritoryIntendedUse::TreasureMapInstance;
}

bool Core::TerritoryMgr::isPrivateTerritory( uint32_t territoryTypeId ) const
{
   auto pTeri = getTerritoryDetail( territoryTypeId );

   if( !pTeri )
      return false;

   return pTeri->territoryIntendedUse == TerritoryIntendedUse::OpeningArea ||
          pTeri->territoryIntendedUse == TerritoryIntendedUse::Inn ||
          pTeri->territoryIntendedUse == TerritoryIntendedUse::HousingPrivateArea ||
          pTeri->territoryIntendedUse == TerritoryIntendedUse::JailArea ||
          pTeri->territoryIntendedUse == TerritoryIntendedUse::MSQPrivateArea;
}


bool Core::TerritoryMgr::createDefaultTerritories()
{
   // for each entry in territoryTypeExd, check if it is a normal and if so, add the zone object
   for( const auto& territory : m_territoryTypeDetailCacheMap )
   {
      auto territoryId = territory.first;
      auto territoryInfo = territory.second;

      // if the zone has no name set
      if( territoryInfo->name.empty() )
         continue;

      auto pPlaceName = g_exdDataGen.getPlaceName( territoryInfo->placeName );

      if( !pPlaceName || pPlaceName->name.empty() || !isDefaultTerritory( territoryId ) )
         continue;

      uint32_t guid = getNextInstanceId();
      g_log.Log( LoggingSeverity::info, std::to_string( territoryId ) +
                                        "\t" + std::to_string( guid ) +
                                        "\t" + std::to_string( territoryInfo->territoryIntendedUse ) +
                                        "\t" + territoryInfo->name +
                                        "\t" + pPlaceName->name );

      ZonePtr pZone( new Zone( territoryId, guid, territoryInfo->name, pPlaceName->name ) );
      pZone->init();

      InstanceIdToZonePtrMap instanceMap;
      instanceMap[guid] = pZone;
      m_instanceIdToZonePtrMap[guid] = pZone;
      m_territoryInstanceMap[territoryId] = instanceMap;

   }

   return true;
}

Core::ZonePtr Core::TerritoryMgr::createTerritoryInstance( uint32_t territoryTypeId )
{
   if( !isValidTerritory( territoryTypeId ) )
      return nullptr;

   if( isInstanceContentTerritory( territoryTypeId ) )
      return nullptr;

   auto pTeri = getTerritoryDetail( territoryTypeId );
   auto pPlaceName = g_exdDataGen.getPlaceName( pTeri->placeName );

   if( !pTeri || !pPlaceName )
      return nullptr;

   g_log.debug( "Starting instance for territory: " + std::to_string( territoryTypeId ) + " (" + pPlaceName->name + ")" );

   ZonePtr pZone = ZonePtr( new Zone( territoryTypeId, getNextInstanceId(), pTeri->name, pPlaceName->name ) );
   pZone->init();

   m_territoryInstanceMap[pZone->getTerritoryId()][pZone->getGuId()] = pZone;
   m_instanceIdToZonePtrMap[pZone->getGuId()] = pZone;

   return pZone;
}

Core::ZonePtr Core::TerritoryMgr::createInstanceContent( uint32_t instanceContentId )
{
   auto pInstanceContent = g_exdDataGen.getInstanceContent( instanceContentId );
   if( !pInstanceContent )
      return nullptr;

   if( !isInstanceContentTerritory( pInstanceContent->territoryType ) )
      return nullptr;

   auto pTeri = getTerritoryDetail( pInstanceContent->territoryType );
   auto pPlaceName = g_exdDataGen.getPlaceName( pTeri->placeName );

   if( !pTeri || !pPlaceName )
      return nullptr;

   g_log.debug( "Starting instance for InstanceContent id: " + std::to_string( instanceContentId ) + " (" + pPlaceName->name + ")" );

   ZonePtr pZone = ZonePtr( new InstanceContent( pInstanceContent, getNextInstanceId(), pTeri->name, pPlaceName->name, instanceContentId ) );
   pZone->init();

   m_instanceContentToInstanceMap[instanceContentId][pZone->getGuId()] = pZone;
   m_instanceIdToZonePtrMap[pZone->getGuId()] = pZone;

   return pZone;
}

bool Core::TerritoryMgr::removeTerritoryInstance( uint32_t instanceId )
{
   ZonePtr pZone;<commit_msg>Use InstanceContent name instead of placeName for InstanceContent<commit_after>#include "TerritoryMgr.h"

#include <common/Logging/Logger.h>
#include <common/Database/DatabaseDef.h>
#include <common/Exd/ExdDataGenerated.h>

#include "Actor/Player.h"

#include "Zone.h"
#include "ZonePosition.h"
#include "InstanceContent.h"

extern Core::Logger g_log;
extern Core::Data::ExdData g_exdData;
extern Core::Data::ExdDataGenerated g_exdDataGen;

Core::TerritoryMgr::TerritoryMgr() :
   m_lastInstanceId( 10000 )
{

}

void Core::TerritoryMgr::loadTerritoryTypeDetailCache()
{
   auto idList = g_exdDataGen.getTerritoryTypeIdList();

   for( auto id : idList )
   {
      auto teri1 = g_exdDataGen.getTerritoryType( id );

      if( !teri1->name.empty() )
         m_territoryTypeDetailCacheMap[id] = teri1;
   }
}

bool Core::TerritoryMgr::isValidTerritory( uint32_t territoryTypeId ) const
{
   return !( m_territoryTypeDetailCacheMap.find( territoryTypeId ) == m_territoryTypeDetailCacheMap.end() );
}

bool Core::TerritoryMgr::init()
{
   loadTerritoryTypeDetailCache();
   loadTerritoryPositionMap();

   createDefaultTerritories();

   return true;
}

uint32_t Core::TerritoryMgr::getNextInstanceId()
{
   return ++m_lastInstanceId;
}

Core::Data::TerritoryTypePtr Core::TerritoryMgr::getTerritoryDetail( uint32_t territoryTypeId ) const
{
   auto tIt = m_territoryTypeDetailCacheMap.find( territoryTypeId );
   if( tIt == m_territoryTypeDetailCacheMap.end() )
      return nullptr;

   return tIt->second;
}

bool Core::TerritoryMgr::isInstanceContentTerritory( uint32_t territoryTypeId ) const
{
   auto pTeri = getTerritoryDetail( territoryTypeId );

   if( !pTeri )
      return false;

   return pTeri->territoryIntendedUse == TerritoryIntendedUse::AllianceRaid ||
          pTeri->territoryIntendedUse == TerritoryIntendedUse::BeforeTrialDung ||
          pTeri->territoryIntendedUse == TerritoryIntendedUse::Trial ||
          pTeri->territoryIntendedUse == TerritoryIntendedUse::Dungeon ||
          pTeri->territoryIntendedUse == TerritoryIntendedUse::OpenWorldInstanceBattle ||
          pTeri->territoryIntendedUse == TerritoryIntendedUse::PalaceOfTheDead ||
          pTeri->territoryIntendedUse == TerritoryIntendedUse::RaidFights ||
          pTeri->territoryIntendedUse == TerritoryIntendedUse::Raids ||
          pTeri->territoryIntendedUse == TerritoryIntendedUse::TreasureMapInstance;
}

bool Core::TerritoryMgr::isPrivateTerritory( uint32_t territoryTypeId ) const
{
   auto pTeri = getTerritoryDetail( territoryTypeId );

   if( !pTeri )
      return false;

   return pTeri->territoryIntendedUse == TerritoryIntendedUse::OpeningArea ||
          pTeri->territoryIntendedUse == TerritoryIntendedUse::Inn ||
          pTeri->territoryIntendedUse == TerritoryIntendedUse::HousingPrivateArea ||
          pTeri->territoryIntendedUse == TerritoryIntendedUse::JailArea ||
          pTeri->territoryIntendedUse == TerritoryIntendedUse::MSQPrivateArea;
}


bool Core::TerritoryMgr::createDefaultTerritories()
{
   // for each entry in territoryTypeExd, check if it is a normal and if so, add the zone object
   for( const auto& territory : m_territoryTypeDetailCacheMap )
   {
      auto territoryId = territory.first;
      auto territoryInfo = territory.second;

      // if the zone has no name set
      if( territoryInfo->name.empty() )
         continue;

      auto pPlaceName = g_exdDataGen.getPlaceName( territoryInfo->placeName );

      if( !pPlaceName || pPlaceName->name.empty() || !isDefaultTerritory( territoryId ) )
         continue;

      uint32_t guid = getNextInstanceId();
      g_log.Log( LoggingSeverity::info, std::to_string( territoryId ) +
                                        "\t" + std::to_string( guid ) +
                                        "\t" + std::to_string( territoryInfo->territoryIntendedUse ) +
                                        "\t" + territoryInfo->name +
                                        "\t" + pPlaceName->name );

      ZonePtr pZone( new Zone( territoryId, guid, territoryInfo->name, pPlaceName->name ) );
      pZone->init();

      InstanceIdToZonePtrMap instanceMap;
      instanceMap[guid] = pZone;
      m_instanceIdToZonePtrMap[guid] = pZone;
      m_territoryInstanceMap[territoryId] = instanceMap;

   }

   return true;
}

Core::ZonePtr Core::TerritoryMgr::createTerritoryInstance( uint32_t territoryTypeId )
{
   if( !isValidTerritory( territoryTypeId ) )
      return nullptr;

   if( isInstanceContentTerritory( territoryTypeId ) )
      return nullptr;

   auto pTeri = getTerritoryDetail( territoryTypeId );
   auto pPlaceName = g_exdDataGen.getPlaceName( pTeri->placeName );

   if( !pTeri || !pPlaceName )
      return nullptr;

   g_log.debug( "Starting instance for territory: " + std::to_string( territoryTypeId ) + " (" + pPlaceName->name + ")" );

   ZonePtr pZone = ZonePtr( new Zone( territoryTypeId, getNextInstanceId(), pTeri->name, pPlaceName->name ) );
   pZone->init();

   m_territoryInstanceMap[pZone->getTerritoryId()][pZone->getGuId()] = pZone;
   m_instanceIdToZonePtrMap[pZone->getGuId()] = pZone;

   return pZone;
}

Core::ZonePtr Core::TerritoryMgr::createInstanceContent( uint32_t instanceContentId )
{
   auto pInstanceContent = g_exdDataGen.getInstanceContent( instanceContentId );
   if( !pInstanceContent )
      return nullptr;

   if( !isInstanceContentTerritory( pInstanceContent->territoryType ) )
      return nullptr;

   auto pTeri = getTerritoryDetail( pInstanceContent->territoryType );

   if( !pTeri || pInstanceContent->name.empty() )
      return nullptr;

   g_log.debug( "Starting instance for InstanceContent id: " + std::to_string( instanceContentId ) +
                                                           " (" + pInstanceContent->name + ")" );

   ZonePtr pZone = ZonePtr( new InstanceContent( pInstanceContent, getNextInstanceId(), pTeri->name,
                                                 pInstanceContent->name, instanceContentId ) );
   pZone->init();

   m_instanceContentToInstanceMap[instanceContentId][pZone->getGuId()] = pZone;
   m_instanceIdToZonePtrMap[pZone->getGuId()] = pZone;

   return pZone;
}

bool Core::TerritoryMgr::removeTerritoryInstance( uint32_t instanceId )
{
   ZonePtr pZone;
{'fraction_non_alphanumeric': 0.1095680258377069, 'fraction_numerical': 0.004198627371820751, 'mean_word_length': 3.7989151491669895, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 93, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 20, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3825817', 'n_tokens_mistral': 3980, 'n_tokens_neox': 3716, 'n_words': 676}