From bd1753423e9515887feea73a3029e96ff5b77be6 Mon Sep 17 00:00:00 2001 From: Mordred Date: Sun, 15 Jan 2023 22:03:44 +0100 Subject: [PATCH] Added a onPlayerDeath function, to be reworked when battlescripts arrive --- src/common/Network/CommonActorControl.h | 3 +++ src/world/Actor/Player.cpp | 3 +++ src/world/Manager/PlayerMgr.cpp | 6 ++++- src/world/Manager/PlayerMgr.h | 2 ++ src/world/Script/NativeScriptApi.cpp | 4 ++++ src/world/Script/NativeScriptApi.h | 2 ++ src/world/Script/ScriptMgr.cpp | 30 +++++++++++++++++++++++++ src/world/Script/ScriptMgr.h | 2 ++ 8 files changed, 51 insertions(+), 1 deletion(-) diff --git a/src/common/Network/CommonActorControl.h b/src/common/Network/CommonActorControl.h index 6f385093..f80d895c 100644 --- a/src/common/Network/CommonActorControl.h +++ b/src/common/Network/CommonActorControl.h @@ -96,6 +96,9 @@ namespace Sapphire::Network::ActorControl FateReqFailMsg = 0x76, DutyQuestScreenMsg = 0x7B, + SetContentClearFlag = 0x82, + SetContentOpenFlag = 0x83, + ItemObtainIcon = 0x84, FateItemFailMsg = 0x85, ItemFailMsg = 0x86, diff --git a/src/world/Actor/Player.cpp b/src/world/Actor/Player.cpp index 8ac60b07..6eb5f3f1 100644 --- a/src/world/Actor/Player.cpp +++ b/src/world/Actor/Player.cpp @@ -1005,7 +1005,10 @@ void Player::unsetStateFlag( Common::PlayerStateFlag flag ) void Player::update( uint64_t tickCount ) { if( m_hp <= 0 && m_status != ActorStatus::Dead ) + { die(); + Service< World::Manager::PlayerMgr >::ref().onDeath( *this ); + } if( !isAlive() ) return; diff --git a/src/world/Manager/PlayerMgr.cpp b/src/world/Manager/PlayerMgr.cpp index adfacbf4..634d8df7 100644 --- a/src/world/Manager/PlayerMgr.cpp +++ b/src/world/Manager/PlayerMgr.cpp @@ -270,7 +270,11 @@ void PlayerMgr::onLogin( Entity::Player &player ) } } - +void PlayerMgr::onDeath( Entity::Player &player ) +{ + auto& scriptMgr = Common::Service< Scripting::ScriptMgr >::ref(); + scriptMgr.onPlayerDeath( player ); +} ////////// Helper /////////// diff --git a/src/world/Manager/PlayerMgr.h b/src/world/Manager/PlayerMgr.h index 3e2aea0b..efa12e84 100644 --- a/src/world/Manager/PlayerMgr.h +++ b/src/world/Manager/PlayerMgr.h @@ -44,6 +44,8 @@ class PlayerMgr void onLogin( Sapphire::Entity::Player& player ); + void onDeath( Sapphire::Entity::Player& player ); + //////////// Helpers static void sendServerNotice( Sapphire::Entity::Player& player, const std::string& message ); diff --git a/src/world/Script/NativeScriptApi.cpp b/src/world/Script/NativeScriptApi.cpp index 2b984cb1..f9e6c0e5 100644 --- a/src/world/Script/NativeScriptApi.cpp +++ b/src/world/Script/NativeScriptApi.cpp @@ -182,6 +182,10 @@ namespace Sapphire::ScriptAPI { } + void QuestScript::onPlayerDeath( World::Quest& quest, Sapphire::Entity::Player& player ) + { + } + /////////////////////////////////////////////////////////////////// EventObjectScript::EventObjectScript( uint32_t eobjId ) : diff --git a/src/world/Script/NativeScriptApi.h b/src/world/Script/NativeScriptApi.h index 9dbe0168..d08e8e4d 100644 --- a/src/world/Script/NativeScriptApi.h +++ b/src/world/Script/NativeScriptApi.h @@ -254,6 +254,8 @@ namespace Sapphire::ScriptAPI virtual void onOutsideRange( World::Quest& quest, Sapphire::Entity::Player& player, uint32_t eventId, uint32_t param1, float x, float y, float z ); + virtual void onPlayerDeath( World::Quest& quest, Sapphire::Entity::Player& player ); + virtual void onEventHandlerTradeReturn( Sapphire::Entity::Player& player, uint32_t eventId, uint16_t subEvent, uint16_t param, uint32_t catalogId ); diff --git a/src/world/Script/ScriptMgr.cpp b/src/world/Script/ScriptMgr.cpp index 2ac24dde..b05a1654 100644 --- a/src/world/Script/ScriptMgr.cpp +++ b/src/world/Script/ScriptMgr.cpp @@ -491,6 +491,36 @@ bool Sapphire::Scripting::ScriptMgr::onBNpcKill( Entity::Player& player, Entity: return true; } +bool Sapphire::Scripting::ScriptMgr::onPlayerDeath( Entity::Player& player ) +{ + auto& eventMgr = Common::Service< World::Manager::EventMgr >::ref(); + + // loop through all active quests and try to call available onPlayerDeath callbacks + for( size_t i = 0; i < 30; i++ ) + { + auto quest = player.getQuestByIndex( static_cast< uint16_t >( i ) ); + if( quest.getId() == 0 ) + continue; + + uint32_t questId = quest.getId() | Event::EventHandler::EventHandlerType::Quest << 16; + + auto script = m_nativeScriptMgr->getScript< Sapphire::ScriptAPI::QuestScript >( questId ); + if( script ) + { + std::string objName = eventMgr.getEventName( questId ); + + PlayerMgr::sendDebug( player, "Calling: {0}.onPlayerDeath name: {1}", objName, player.getName() ); + + World::Quest preQ = quest; + script->onPlayerDeath( quest, player ); + if( quest != preQ ) + player.updateQuest( quest ); + } + } + + return true; +} + bool Sapphire::Scripting::ScriptMgr::onEObjHit( Sapphire::Entity::Player& player, uint64_t actorId, uint32_t actionId ) { auto& eventMgr = Common::Service< World::Manager::EventMgr >::ref(); diff --git a/src/world/Script/ScriptMgr.h b/src/world/Script/ScriptMgr.h index 07c074c4..56fe7825 100644 --- a/src/world/Script/ScriptMgr.h +++ b/src/world/Script/ScriptMgr.h @@ -70,6 +70,8 @@ namespace Sapphire::Scripting bool onBNpcKill( Entity::Player& player, Entity::BNpc& bnpc ); + bool onPlayerDeath( Entity::Player& player ); + void onTriggerOwnerDeaggro( Entity::Player& player, Entity::BNpc& bnpc ); bool onEObjHit( Entity::Player& player, uint64_t actorId, uint32_t actionId );