Angel 3.2
A 2D Game Prototyping Engine
Brain.h
1 
2 // Copyright (C) 2008-2013, Shane Liesegang
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are met:
7 //
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // * Neither the name of the copyright holder nor the names of any
14 // contributors may be used to endorse or promote products derived from
15 // this software without specific prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 // POSSIBILITY OF SUCH DAMAGE.
29 
30 #pragma once
31 
32 #include "../Util/StringUtil.h"
33 
34 class AIBrainState;
35 class Sentient;
36 
37 class AIBrain
38 {
39  typedef hashmap_ns::hash_map<String, AIBrainState*> BrainStateTable;
40 public:
41  AIBrain();
42  void SetActor( Sentient* actor ) {_actor = actor;}
43 
44  virtual ~AIBrain();
45 
46  virtual void AddState( const String& id, AIBrainState* state );
47 
48  virtual void Update( float dt);
49  virtual void GotoState( const String& id );
50 
51  Sentient* GetActor() {return _actor;}
52 
53  void Render();
54  void GotoNullState();
55 
56  void EnableDrawing(bool enable);
57 
58 protected:
59  BrainStateTable _brainStateTable;
60  BrainStateTable::iterator _current;
61  Sentient* _actor;
62  bool _drawMe;
63 };
64 
65 
66 class AIEvent;
67 
68 /*abstract*/ class AIBrainState
69 {
70  typedef std::vector<AIEvent*> EventList;
71 public:
72  virtual ~AIBrainState() {}
73  virtual void Initialize( AIBrain* brain ) {_brain = brain;}
74  void Update(float dt);
75  virtual void CustomUpdate(float /*dt*/) {}
76  virtual void BeginState( AIBrainState* /*previousState*/ ) {}
77  void EndState( AIBrainState* nextState );
78  virtual void CustomEndState( AIBrainState* /*nextState*/ ) {}
79 
80 protected:
81  virtual void GotoState( const String& id );
82  virtual AIEvent* RegisterEvent( AIEvent* newEvent );
83  virtual void UnregisterEvent( AIEvent* oldEvent );
84  Sentient* GetActor() {return _brain->GetActor();}
85 private:
86  void ClearEvents();
87  void StopEvent( AIEvent* pEvent );
88 protected:
89  AIBrain* _brain;
90  EventList _eventList;
91 };
92 
93 
94 #define DECLARE_AISTATE(cls) \
95  typedef cls MyClass;
96 
97 
98 #define DECLARE_BASESTATE( BaseStateID, StateDataType ) \
99 class BaseStateID : public AIBrainState \
100 { \
101 public: \
102  BaseStateID( StateDataType& _StateData ) \
103  : StateData(_StateData) \
104 {} \
105 protected: \
106  StateDataType& StateData; \
107 };
108 
109 #define DECLARE_SHAREDDATASTATE( ThisStateID, BaseStateID, StateDataType ) \
110  DECLARE_AISTATE(ThisStateID) \
111  ThisStateID( StateDataType& _StateData ) \
112  : BaseStateID( _StateData ) \
113 {}
114 
115 
116 /*abstract*/ class AIEvent
117 {
118 public:
119  virtual ~AIEvent() {}
120  virtual void Stop() {}
121  virtual void Update(float /*dt*/) {}
122 
123  void SetBrain( AIBrain* pBrain ) {_brain = pBrain;}
124 
125 protected:
126  AIBrain* GetBrain() {return _brain;}
127  Sentient* GetActor() {return _brain->GetActor();}
128  virtual void IssueCallback() {}
129 private:
130  AIBrain* _brain;
131 };
132 
133 #define REGISTER_AIEVENT( ev, cb, params ) \
134  ((ev##Template<MyClass>*)this->RegisterEvent( new ev##Template<MyClass>(this, &MyClass::cb) ))->Initialize params
135 
136 #define UNREGISTER_AIEVENT(oldEvent) \
137  UnregisterEvent(oldEvent)
138 
139 
140 #define DECLARE_AIEVENT_BASE( FromClass ) \
141 template<class T> \
142 class FromClass##Template : public FromClass \
143 { \
144 public: \
145  typedef void(T::*AIBrainStateCallback)( FromClass* newEvent ); \
146  FromClass##Template( T* cbObj, AIBrainStateCallback cb ) \
147  : _cbObj(cbObj) \
148  , _cb(cb) \
149  {} \
150 protected: \
151  virtual void IssueCallback()\
152  { \
153  (*_cbObj.*_cb)(this); \
154  } \
155 private: \
156  T* _cbObj; \
157  AIBrainStateCallback _cb; \
158 };
159