-
Notifications
You must be signed in to change notification settings - Fork 93
Replace AIMaster m_lEventQueue with a priority queue #1537
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Liareth
wants to merge
4
commits into
nwnxee:master
Choose a base branch
from
Arelith-Server:liareth/event-queue-upstream
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
#include "nwnx.hpp" | ||
|
||
#include "API/CAppManager.hpp" | ||
#include "API/CGameObjectArray.hpp" | ||
#include "API/CServerAIMaster.hpp" | ||
#include "API/CServerExoApp.hpp" | ||
#include "API/CServerExoAppInternal.hpp" | ||
#include "API/CWorldTimer.hpp" | ||
#include "API/CNWSPlayer.hpp" | ||
#include "API/CNWSObject.hpp" | ||
#include "API/CNWSCreature.hpp" | ||
#include "API/CExoBase.hpp" | ||
#include "API/CExoTimers.hpp" | ||
#include "API/CNetLayer.hpp" | ||
#include "API/CNWSMessage.hpp" | ||
|
||
#include <queue> | ||
|
||
namespace Optimizations | ||
{ | ||
|
||
using namespace NWNXLib; | ||
using namespace NWNXLib::API; | ||
|
||
struct CServerAIEventNodeComparator | ||
{ | ||
bool operator()(const CServerAIEventNode& lhs, const CServerAIEventNode& rhs) const | ||
{ | ||
return (lhs.m_nCalendarDay == rhs.m_nCalendarDay) ? (lhs.m_nTimeOfDay > rhs.m_nTimeOfDay) : lhs.m_nCalendarDay > rhs.m_nCalendarDay; | ||
} | ||
}; | ||
|
||
std::priority_queue<CServerAIEventNode, std::vector<CServerAIEventNode>, CServerAIEventNodeComparator> g_event_queue; | ||
|
||
void ClearEventQueue(CServerAIMaster*) | ||
{ | ||
g_event_queue = {}; | ||
} | ||
|
||
BOOL AddEventAbsoluteTime(CServerAIMaster*, uint32_t nCalendarDay, uint32_t nTimeOfDay, OBJECT_ID nCallerObjectId, OBJECT_ID nObjectId, uint32_t nEventId, void *pEventData) | ||
{ | ||
g_event_queue.emplace(CServerAIEventNode { nCalendarDay, nTimeOfDay, nCallerObjectId, nObjectId, nEventId, pEventData }); | ||
return true; | ||
} | ||
|
||
BOOL EventPending(CServerAIMaster* ai, uint32_t nCalendarDay, uint32_t nTimeOfDay) | ||
{ | ||
if (g_event_queue.empty()) return false; | ||
|
||
const CServerAIEventNode& eventNode = g_event_queue.top(); | ||
return ai->m_pExoAppInternal->m_pWorldTimer->CompareWorldTimes(eventNode.m_nCalendarDay, eventNode.m_nTimeOfDay, nCalendarDay, nTimeOfDay) <= 0; | ||
} | ||
|
||
BOOL GetPendingEvent(CServerAIMaster*, uint32_t *nCalendarDay, uint32_t *nTimeOfDay, OBJECT_ID *nCallerObjectId, OBJECT_ID *nObjectId, uint32_t *nEventId, void **pEventData) | ||
{ | ||
if (g_event_queue.empty()) return false; | ||
|
||
const CServerAIEventNode& eventNode = g_event_queue.top(); | ||
|
||
*nCalendarDay = eventNode.m_nCalendarDay; | ||
*nTimeOfDay = eventNode.m_nTimeOfDay; | ||
*nCallerObjectId = eventNode.m_nCallerObjectId; | ||
*nObjectId = eventNode.m_nObjectId; | ||
*nEventId = eventNode.m_nEventId; | ||
*pEventData = eventNode.m_pEventData; | ||
|
||
g_event_queue.pop(); | ||
return true; | ||
} | ||
|
||
void EventMasterPriorityQueue() __attribute__((constructor)); | ||
|
||
void EventMasterPriorityQueue() | ||
{ | ||
// Replace: | ||
// | ||
// * CServerAIMaster::ClearEventQueue() | ||
// * CServerAIMaster::AddEventAbsoluteTime() | ||
// * CServerAIMaster::AddEventAbsoluteTimeViaTail() | ||
// * CServerAIMaster::EventPending() | ||
// * CServerAIMaster::GetPendingEvent() | ||
// | ||
// Don't care about save/load event queue. | ||
|
||
if (Config::Get<bool>("EVENT_MASTER_PRIORITY_QUEUE", false)) | ||
{ | ||
static Hooks::Hook _0 = Hooks::HookFunction(API::Functions::_ZN15CServerAIMaster15ClearEventQueueEv, (void*)&ClearEventQueue, Hooks::Order::Early); | ||
static Hooks::Hook _1 = Hooks::HookFunction(API::Functions::_ZN15CServerAIMaster20AddEventAbsoluteTimeEjjjjjPv, (void*)&AddEventAbsoluteTime, Hooks::Order::Early); | ||
static Hooks::Hook _2 = Hooks::HookFunction(API::Functions::_ZN15CServerAIMaster27AddEventAbsoluteTimeViaTailEjjjjjPv, (void*)&AddEventAbsoluteTime, Hooks::Order::Early); | ||
static Hooks::Hook _3 = Hooks::HookFunction(API::Functions::_ZN15CServerAIMaster12EventPendingEjj, (void*)&EventPending, Hooks::Order::Early); | ||
static Hooks::Hook _4 = Hooks::HookFunction(API::Functions::_ZN15CServerAIMaster15GetPendingEventEPjS0_S0_S0_S0_PPv, (void*)&GetPendingEvent, Hooks::Order::Early); | ||
Liareth marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.