Skip to content

Commit

Permalink
Merge pull request #916 from hkAlice/bs-stuff
Browse files Browse the repository at this point in the history
[3.x - EncounterFight] add prototype encounterfight, encounterstate, ifrit normal;
  • Loading branch information
SapphireMordred authored Mar 8, 2023
2 parents 5fdbc90 + 3f2cb2f commit 2c7b790
Show file tree
Hide file tree
Showing 12 changed files with 405 additions and 39 deletions.
2 changes: 1 addition & 1 deletion src/scripts/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ foreach(_scriptDir ${children})
MODULE
${SCRIPT_BUILD_FILES}
"${SCRIPT_INCLUDE_FILES}"
"${_scriptDir}/ScriptLoader.cpp" )
"${_scriptDir}/ScriptLoader.cpp" )

target_link_libraries( "script_${_name}" world )

Expand Down
10 changes: 6 additions & 4 deletions src/world/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,26 @@ file( GLOB SERVER_SOURCE_FILES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR}
Action/*.cpp
ContentFinder/*.cpp
DebugCommand/*.cpp
Encounter/*.cpp
Encounter/InstanceContent/*.cpp
Event/*.cpp
FreeCompany/*.cpp
Inventory/*.cpp
Linkshell/*.cpp
Manager/*.cpp
Math/*.cpp
Navi/*.cpp
Network/*.cpp
Network/Util/*.cpp
Network/Handlers/*.cpp
Network/PacketWrappers/*.cpp
Quest/*.cpp
Script/*.cpp
StatusEffect/*.cpp
Task/*.cpp
Territory/*.cpp
Territory/Housing/*.cpp
Util/*.cpp
Navi/*.cpp
Task/*.cpp
Quest/*.cpp )
Util/*.cpp )

add_executable( world ${SERVER_SOURCE_FILES} )

Expand Down
75 changes: 75 additions & 0 deletions src/world/Encounter/EncounterFight.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#include <memory>
#include <set>
#include <stack>
#include <Territory/InstanceContent.h>
#include <Logging/Logger.h>
#include <Actor/BNpc.h>

namespace Sapphire
{
class EncounterState
{
public:
using EncounterStatePtr = std::shared_ptr< EncounterState >;
using StateStack = std::stack< EncounterStatePtr >;
using StateStackPtr = std::shared_ptr< StateStack >;

protected:
bool m_bShouldFinish{ false };
StateStackPtr m_stateStack;
std::shared_ptr< EncounterFight > m_pEncounter;
uint64_t m_startTime{ 0 };

public:
EncounterState( std::shared_ptr< EncounterFight > pEncounter ) :
m_pEncounter( pEncounter )
{
};

virtual ~EncounterState() = default;
bool shouldFinish() { return m_bShouldFinish; };

virtual void init() = 0;
virtual void update( uint64_t deltaTime ) = 0;

virtual void finish() = 0;
};

enum class EncounterFightStatus
{
IDLE,
ACTIVE,
FAIL,
SUCCESS
};

class EncounterFight : public std::enable_shared_from_this< EncounterFight >
{
public:
EncounterFight( InstanceContentPtr pInstance ) :
m_pInstance( pInstance )
{
};
virtual ~EncounterFight() = default;

virtual void init() = 0;
virtual void start() = 0;
virtual void update( uint64_t deltaTime ) = 0;
virtual void reset() = 0;

virtual void addState( EncounterState::EncounterStatePtr pState, bool initState = true ) = 0;
virtual void addBNpc( Entity::BNpcPtr pBNpc ) = 0;
virtual void removeBNpc( uint32_t layoutId ) = 0;
virtual Entity::BNpcPtr getBNpc( uint32_t layoutId ) = 0;

virtual EncounterFightStatus getEncounterFightStatus() const = 0;

protected:
uint64_t m_startTime{ 0 };
EncounterState::StateStackPtr m_stateStack;
std::set< Entity::PlayerPtr > m_playerList;
std::unordered_map< uint32_t, Entity::BNpcPtr > m_bnpcs;
InstanceContentPtr m_pInstance;
EncounterFightStatus m_status{ EncounterFightStatus::IDLE };
};
}
198 changes: 198 additions & 0 deletions src/world/Encounter/InstanceContent/IfritNormal.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
#include <Encounter/EncounterFight.h>

namespace Sapphire
{
class IfritNormalData
{
public:
static constexpr int IFRIT = 4126276;
static constexpr int HELLFIRE = 0;
};

class IfritStateTwo : public EncounterState
{
public:
IfritStateTwo( EncounterFightPtr pEncounter ) : EncounterState( pEncounter )
{
}

void init() override
{
Logger::info( "stage 2 init" );
}

void update( uint64_t deltaTime ) override
{
if( m_startTime == 0 )
m_startTime = deltaTime;

auto timeElapsedMs = deltaTime - m_startTime;

auto pIfrit = m_pEncounter->getBNpc( IfritNormalData::IFRIT );

pIfrit->setRot( pIfrit->getRot() - .2f );
pIfrit->sendPositionUpdate();

if( timeElapsedMs > 5000 )
{
m_bShouldFinish = true;
}
}

void finish() override
{
Logger::info( "stage 2 done, going back to stage 1" );
}
};


class IfritStateOne : public EncounterState
{
public:
IfritStateOne( EncounterFightPtr pEncounter ) : EncounterState( pEncounter )
{
}

void init() override
{
Logger::info( "stage 1 init" );
}

void update( uint64_t deltaTime ) override
{
if( m_startTime == 0 )
m_startTime = deltaTime;

auto timeElapsedMs = deltaTime - m_startTime;

auto pIfrit = m_pEncounter->getBNpc( IfritNormalData::IFRIT );

pIfrit->setRot( pIfrit->getRot() + .2f );
pIfrit->sendPositionUpdate();

if( timeElapsedMs > 5000 )
{
auto ifritTwoState = std::make_shared< IfritStateTwo >( m_pEncounter );
m_pEncounter->addState( ifritTwoState );
}

if( timeElapsedMs > 12000 )
{
pIfrit->hateListGetHighest()->die();
}
}

void finish() override
{

}
};

class IfritEncounterFight : public EncounterFight
{
public:
IfritEncounterFight( InstanceContentPtr pInstance ) : EncounterFight( pInstance )
{

};

void init() override
{
m_status = EncounterFightStatus::IDLE;
m_startTime = 0;

m_stateStack = std::make_shared< EncounterState::StateStack >();

// todo: i don't like this
auto boss = m_pInstance->createBNpcFromLayoutId( IfritNormalData::IFRIT, 13884, Common::BNpcType::Enemy );
addBNpc( boss );

//instance.sendForward();
/*
auto ifritStateTwo = std::make_shared< IfritStateTwo >( m_stateStack );
m_stateStack->push( ifritStateTwo );*/
}

void addState( EncounterState::EncounterStatePtr pState, bool initState = true ) override
{
m_stateStack->push( pState );
if( initState )
pState->init();
}

void start() override
{
auto ifritInitState = std::make_shared< IfritStateOne >( shared_from_this() );
addState( ifritInitState );

m_status = EncounterFightStatus::ACTIVE;
}

void update( uint64_t deltaTime ) override
{
// todo: better way to start fights here..

auto ifrit = getBNpc( IfritNormalData::IFRIT );

if( ifrit; ifrit->hateListGetHighestValue() != 0 && m_status == EncounterFightStatus::IDLE )
{
m_startTime = deltaTime;
start();
}

if( m_status == EncounterFightStatus::ACTIVE && ifrit && !ifrit->hateListGetHighest()->isAlive() )
{
m_status = EncounterFightStatus::FAIL;
}

if( m_stateStack; !m_stateStack->empty() )
{
if( m_stateStack->top()->shouldFinish() )
{
m_stateStack->top()->finish();
m_stateStack->pop();
}

m_stateStack->top()->update( deltaTime );
}
}

void reset() override
{
auto boss = m_pInstance->getActiveBNpcByLayoutId( IfritNormalData::IFRIT );
if( boss )
{
removeBNpc( IfritNormalData::IFRIT );
m_pInstance->removeActor( boss );

}


init();
}

EncounterFightStatus getEncounterFightStatus() const override
{
return m_status;
}

void addBNpc( Entity::BNpcPtr pBNpc ) override
{
m_bnpcs[ pBNpc->getLayoutId() ] = pBNpc;
}

Entity::BNpcPtr getBNpc( uint32_t layoutId ) override
{
auto bnpc = m_bnpcs.find( layoutId );
if( bnpc != std::end( m_bnpcs ) )
return bnpc->second;

return nullptr;
}

void removeBNpc( uint32_t layoutId ) override
{
m_bnpcs.erase( layoutId );
}
};
}
1 change: 1 addition & 0 deletions src/world/ForwardsZone.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ TYPE_FORWARD( ItemContainer );
TYPE_FORWARD( Land );
TYPE_FORWARD( Linkshell );
TYPE_FORWARD( FreeCompany );
TYPE_FORWARD( EncounterFight );

namespace World
{
Expand Down
3 changes: 1 addition & 2 deletions src/world/Network/PacketWrappers/MoveActorPacket.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ namespace Sapphire::Network::Packets::WorldPackets::Server
{

/**
* @brief The Client UI Initialization packet. This must be sent to the client
* once upon connection to configure the UI.
* @brief The MoveActor packet for updating an actor's position.
*/
class MoveActorPacket : public ZoneChannelPacket< FFXIVIpcActorMove >
{
Expand Down
1 change: 0 additions & 1 deletion src/world/Script/NativeScriptApi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,5 @@ namespace Sapphire::ScriptAPI
uint16_t param1, uint16_t param2 )
{
}

}

3 changes: 1 addition & 2 deletions src/world/Script/NativeScriptApi.h
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ namespace Sapphire::ScriptAPI
};

/*!
* @brief The base class for any scripts that implement behaviour related to instance content zones
* @brief The base class for any scripts that implement behaviour related to quest battles
*/
class QuestBattleScript : public ScriptObject
{
Expand Down Expand Up @@ -405,7 +405,6 @@ namespace Sapphire::ScriptAPI
return Common::Service< World::Manager::PlayerMgr >::ref();
}
};

}

#endif
2 changes: 1 addition & 1 deletion src/world/Script/ScriptLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ Sapphire::ScriptAPI::ScriptObject** Sapphire::Scripting::ScriptLoader::getScript
}
else
{
Logger::warn( "did not find a win32initLinkshell export on a windows script target - the server will likely crash!" );
Logger::warn( "did not find a win32initWarp export on a windows script target - the server will likely crash!" );
}
#else
auto func = reinterpret_cast< getScripts >( dlsym( handle, "getScripts" ) );
Expand Down
4 changes: 4 additions & 0 deletions src/world/Script/ScriptMgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -683,10 +683,13 @@ bool Sapphire::Scripting::ScriptMgr::onZoneInit( const Territory& zone )

bool Sapphire::Scripting::ScriptMgr::onInstanceInit( InstanceContent& instance )
{
auto instId = instance.getDirectorId();
auto script = m_nativeScriptMgr->getScript< Sapphire::ScriptAPI::InstanceContentScript >( instance.getDirectorId() );

if( script )
{
script->onInit( instance );

return true;
}

Expand All @@ -700,6 +703,7 @@ bool Sapphire::Scripting::ScriptMgr::onInstanceUpdate( InstanceContent& instance
if( script )
{
script->onUpdate( instance, tickCount );

return true;
}

Expand Down
Loading

0 comments on commit 2c7b790

Please sign in to comment.