JNR
aiAgent.h
1 /*
2 
3 Jump'n'Run Engine
4 http://www.atanaslaskov.com/jnr/
5 
6 BSD LICENSE
7 Copyright (c) 2007-2013, Atanas Laskov
8 All rights reserved.
9 
10 Redistribution and use in source and binary forms, with or without
11 modification, are permitted provided that the following conditions are met:
12 1. Redistributions of source code must retain the above copyright notice,
13 this list of conditions and the following disclaimer.
14 2. Redistributions in binary form must reproduce the above copyright notice,
15 this list of conditions and the following disclaimer in the documentation
16 and/or other materials provided with the distribution.
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 DISCLAIMED. IN NO EVENT SHALL ATANAS LASKOV BE LIABLE FOR ANY
21 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 
28 */
29 #ifndef AI_AGNET_H
30 #define AI_AGNET_H
31 
33 
34 
35 #define P__PLAYER_DISTANCE 0 //distance to the player
36 
37 #define P__PLAYER_IN_TERRITORY 1 //player in creature territory
38 #define P__PLAYER_IN_RANGE 2 //player in creature attack/act range
39 #define P__PLAYER_TOUCHED 3 //player touches the creature
40 #define P__PLAYER_ATTACKS 4 //player attacks the creature
41 
42 #define P__PLAYER_ACTION 5 //player performs action
43 
44 
59 class aiAgent: public laLoadableObj, public laClassFactory
60 {
61 protected:
62 
63  // AI activation flag
64  //
65  M_BOOL _bActive;
66  M_BOOL _bInitialized;
67 
68  // List of agent percepts
69  //
70  aiPercepts _percepts; //< Personal percepts
71  aiPercepts* _perceptsGlobal; //< Global percepts; Stored in the level object to represent global events
72 
73  // Agent controllers,
74  // i.e. game objects that can be manipulated by the AI agent
75  //
76  aiController* _self; //< Self controller
77  aiController* _player; //< Player controller
78 
79  // List of agent states,
80  // The active state responds to percept events
81  //
82  // Note: String identifiers are more convenient,
83  // because they can be copied directly when the
84  // aiAgent object is loaded from file;
85  // There is a small number of states, and only 1 active,
86  // so this should not be reasonably efficient
87  //
88  std::map<std::string, aiAgentState*> _vStates;
89  std::string _strCurrentState; // ID of active state
90  aiAgentState* _pCurrentState; // also store a pointer for quick access
91 
92  // Map of aiAgentState types
93  //std::map<std::string, aiAgentState*>
94 
95  //virtual void perceive_any(laTimer *pt) = 0;
96 
97 public:
98 
99  // Constructor
100  //
101  aiAgent();
102 
103  // AI activation
104  //
105  void activate(M_BOOL active = M_TRUE) { _bActive = active; }
106  M_BOOL isActive() { return _bActive; }
107 
108  // Reset agent state
109  //
110  void reset() { _bInitialized = M_FALSE; } // triggers the on activate percept
111 
112  // Set controller objects
113  //
114  void controllers(aiController* pSelf, aiController *pPlayer, aiPercepts* pGlobalPercepts);
115 
116  // State management
117  //
118  void add(std::string id, aiAgentState* pState); //< Add state
119  void state(std::string id); //< Set current state
120  std::string state() { return _strCurrentState; } //< Get current state
121 
122  // Percept management
123  //
124  void perceive(unsigned id, aiPerceptData data = aiPerceptData() ); //< Set percept
125 
126  M_BOOL isPerceiving(unsigned id) //< Check if percept is present
127  {
128  if( _percepts.find(id) != _percepts.end()) return M_TRUE;
129  else return M_FALSE;
130  }
131 
132  void perceptsClear(); //< Clear percepts
133  //< NOTE: This call should be made after every simulation step,
134  //< otherwise old percepts will accumulate
135 
136  // Execute a discrete step of the AI agent,
137  // This function calls all the percept handlers
138  //
139  virtual void execute(class laTimer *pt);
140 
141  // Load aiAgent form file
142  // The function reads up a list of aiAgentState identifiers,
143  // creates an appropriate aiAgent state object for each one
144  // and registers them with aiAgent::add()
145  virtual void load(class laFileParser *fp);
146 };
148 #endif
Single state of the AI-controlled creature.
Definition: aiAgentState.h:48
Abstract base class for AI agents.
Definition: aiAgent.h:59
Interface for loadable objects.
Definition: laLoadableObj.h:43
Named class factory.
Class aiController is an abstract class for behaviours which can be performed by objects.
Definition: aiController.h:41
File Parser.
Definition: laFileParser.h:41