Plasma Engine  2.0
Loading...
Searching...
No Matches
AiAction.h
1#pragma once
2
3#include <AiPlugin/AiPluginDLL.h>
4#include <Foundation/Strings/HashedString.h>
5#include <GameEngine/GameEngineDLL.h>
6
7class plGameObject;
9
10enum class [[nodiscard]] plAiActionResult
11{
12 Succeded,
13 Finished,
14 Failed,
15};
16
17template <typename TYPE>
19{
20public:
21 TYPE* Acquire()
22 {
23 PL_LOCK(m_Mutex);
24
25 TYPE* pType = nullptr;
26
27 if (m_FreeList.IsEmpty())
28 {
29 pType = &m_Allocated.ExpandAndGetRef();
30 }
31 else
32 {
33 pType = m_FreeList.PeekBack();
34 m_FreeList.PopBack();
35 }
36
37 pType->Reset();
38 return pType;
39 }
40
41 void Release(TYPE* pType)
42 {
43 PL_LOCK(m_Mutex);
44 PL_ASSERT_DEBUG(!m_FreeList.Contains(pType), "");
45 m_FreeList.PushBack(pType);
46 }
47
48private:
49 plMutex m_Mutex;
50 plHybridArray<TYPE*, 16> m_FreeList;
51 plDeque<TYPE> m_Allocated;
52};
53
54#define PL_DECLARE_AICMD(OwnType) \
55public: \
56 static OwnType* Create() \
57 { \
58 OwnType* pType = s_Allocator.Acquire(); \
59 pType->m_bFromAllocator = true; \
60 return pType; \
61 } \
62 \
63private: \
64 virtual void Destroy() override \
65 { \
66 Reset(); \
67 if (m_bFromAllocator) \
68 s_Allocator.Release(this); \
69 } \
70 bool m_bFromAllocator = false; \
71 static plAiActionAlloc<OwnType> s_Allocator;
72
73#define PL_IMPLEMENT_AICMD(OwnType) \
74 plAiActionAlloc<OwnType> OwnType::s_Allocator;
75
79
80class PL_AIPLUGIN_DLL plAiAction
81{
82public:
83 plAiAction() = default;
84 virtual ~plAiAction() = default;
85
86 virtual void Reset() = 0;
87 virtual void GetDebugDesc(plStringBuilder& inout_sText) = 0;
88 virtual plAiActionResult Execute(plGameObject& owner, plTime tDiff, plLogInterface* pLog) = 0;
89 virtual void Cancel(plGameObject& owner) = 0;
90
91private:
92 friend class plAiActionQueue;
93 virtual void Destroy() = 0;
94};
Definition AiAction.h:19
Definition AiActions.h:20
Definition AiActionQueue.h:9
bool Contains(const T &value) const
Checks whether the given value can be found in the array. O(n) complexity.
Definition ArrayBase_inl.h:191
void PushBack(const T &value)
Pushes value at the end of the array.
Definition ArrayBase_inl.h:333
void PopBack(plUInt32 uiCountToRemove=1)
Removes count elements from the end of the array.
Definition ArrayBase_inl.h:379
T & PeekBack()
Returns the last element of the array.
Definition ArrayBase_inl.h:388
bool IsEmpty() const
Returns true, if the array does not contain any elements.
Definition ArrayBase_inl.h:178
T & ExpandAndGetRef()
Grows the deque by one element and returns a reference to the newly created element.
Definition Deque_inl.h:473
Definition Deque.h:270
This class represents an object inside the world.
Definition GameObject.h:32
A hybrid array uses in-place storage to handle the first few elements without any allocation....
Definition HybridArray.h:12
Base class for all logging classes.
Definition Log.h:77
Provides a simple mechanism for mutual exclusion to prevent multiple threads from accessing a shared ...
Definition Mutex.h:13
plStringBuilder is a class that is meant for creating and modifying strings.
Definition StringBuilder.h:35
The time class encapsulates a double value storing the time in seconds.
Definition Time.h:12