Tactics: Western Philosophers Vs. Musicians  0.12
A turn-based tactical game combining rules and gameplay elements inspired by Final Fantasy Tactics and the Mayfair Exponential Game System. Unlike most games of this type, motion is in full, grid-less 3D.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
AIBase.h
Go to the documentation of this file.
1 // Copyright (C) 2004-2012 Dylan Blair
3 //
4 // email: dblair@alumni.cs.utexas.edu
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 
21 #ifndef AIBASE_H
22 #define AIBASE_H
23 
32 #include "AIConsts.h"
33 
34 #include "../Utils_LIB/UTypes.h"
35 
36 #include <vector>
37 #include <fstream>
38 
39 namespace OpenSkyNet {
40  namespace Utils {
41  class PrecisionTimer;
42  }
43 
46  namespace AI {
48 
52  class Premise {
53  public:
54  std::vector<int> _params;
55  float _entryTime;
56  bool _negated;
57 
61  Premise() : _entryTime(-1.0f), _negated(false) {}
62 
67  Premise(const std::vector<int>& params_) : _params(params_), _entryTime(-1.0f), _negated(false) {}
68 
73  virtual bool operator==(const Premise& rhs_) const;
74 
79  virtual bool equals(const Premise& rhs_, unsigned int numParamsToCheck_) const;
80 
85  Premise& operator=(int param_) {
86  _params.clear();
87  _params.push_back(param_);
88  _entryTime = -1.0f;
89  _negated = false;
90  return *this;
91  }
92 
97  Premise& operator+=(int param_) {
98  _params.push_back(param_);
99  return *this;
100  }
101 
107  virtual int serialize(std::ofstream& fout_) const;
108 
114  virtual int assemble(std::ifstream& fin_);
115 
116  virtual ~Premise() {}
117  };
118 
122  class Action : public Premise {
123  public:
124  Action() {}
125 
126  Action(const std::vector<int>& params_) : Premise(params_) {}
127 
128  Action& operator=(int param_) {
129  Premise::operator=(param_);
130  return *this;
131  }
132 
133  Action& operator+=(int param_) {
134  Premise::operator+=(param_);
135  return *this;
136  }
137 
138  virtual ~Action() {}
139  };
140 
141  class Wrapper;
142 
148  class Record : public Premise {
149  public:
153 
154  Record(OpenSkyNet::AI::Wrapper* n_=0, float badGoodScaleValue_=0.0f,
155  int stateIdAtCreation_=-1) : _n(n_),
156  _badGoodScaleValue(badGoodScaleValue_),
157  _stateIdAtCreation(stateIdAtCreation_) {}
158 
159  Record(const std::vector<int>& params_, OpenSkyNet::AI::Wrapper* n_=0,
160  float badGoodScaleValue_=0.0f, int stateIdAtCreation_=-1) : Premise(params_),
161  _n(n_), _badGoodScaleValue(badGoodScaleValue_),
162  _stateIdAtCreation(stateIdAtCreation_) {}
163 
164  Record(const Premise& premise_) : Premise(premise_), _n(0), _badGoodScaleValue(0.0f), _stateIdAtCreation(-1) {}
165 
170  Record& operator=(int param_) {
171  Premise::operator=(param_);
172  return *this;
173  }
174 
175  Record& operator+=(int param_) {
176  Premise::operator+=(param_);
177  return *this;
178  }
179 
180  virtual ~Record() {}
181  };
182 
185  namespace FileIO {
192  template <class T>
193  int serialize(std::ofstream& fout_, const std::vector<T>& vector_) {
194  static const unsigned int sizeOfType = sizeof(T);
195  unsigned int bytesWritten = 0;
196  unsigned int vectorSize = static_cast<unsigned int>(vector_.size());
197 
198  fout_.write(reinterpret_cast<char*>(&vectorSize),sizeof(vectorSize));
199  bytesWritten += sizeof(vectorSize);
200 
201  T element;
202  for (unsigned int i = 0; i < vectorSize; i++) {
203  element = vector_[i];
204  fout_.write(reinterpret_cast<char*>(&element),sizeOfType);
205  }
206  bytesWritten += vectorSize*sizeOfType;
207 
208  return bytesWritten;
209  }
210 
217  template <class T>
218  int assemble(std::ifstream& fin_, std::vector<T>& vector_) {
219  static const unsigned int sizeOfType = sizeof(T);
220  unsigned int bytesRead = 0;
221  unsigned int vectorSize = 0;
222  vector_.clear();
223 
224  fin_.read(reinterpret_cast<char*>(&vectorSize),sizeof(vectorSize));
225  bytesRead += sizeof(vectorSize);
226 
227  vector_.resize(vectorSize);
228  T element;
229  for (unsigned int i = 0; i < vectorSize; i++) {
230  fin_.read(reinterpret_cast<char*>(&element),sizeOfType);
231  vector_[i] = element;
232  }
233  bytesRead += vectorSize*sizeOfType;
234 
235  return bytesRead;
236  }
237 
244  template <>
245  int serialize(std::ofstream& fout_, const std::vector<Premise>& premVector_);
246 
253  template <>
254  int assemble(std::ifstream& fin_, std::vector<Premise>& premVector_);
255 
262  template <>
263  int serialize(std::ofstream& fout_, const std::vector<Action>& actVector_);
264 
271  template <>
272  int assemble(std::ifstream& fin_, std::vector<Action>& actVector_);
273  }
274  }
275 }
276 
277 #endif //AIBASE_H
virtual bool operator==(const Premise &rhs_) const
Definition: AIBase.cpp:19
Record & operator+=(int param_)
Definition: AIBase.h:175
Definition: AIBase.h:148
float _entryTime
of secs elapsed from time program started to time entered in a KB
Definition: AIBase.h:55
Action & operator=(int param_)
Definition: AIBase.h:128
virtual ~Record()
empty destructor
Definition: AIBase.h:180
bool _negated
look for absence of this premise in a KB
Definition: AIBase.h:56
Record(OpenSkyNet::AI::Wrapper *n_=0, float badGoodScaleValue_=0.0f, int stateIdAtCreation_=-1)
Definition: AIBase.h:154
virtual int serialize(std::ofstream &fout_) const
Definition: AIBase.cpp:37
Premise(const std::vector< int > &params_)
Definition: AIBase.h:67
int serialize(std::ofstream &fout_, const std::vector< T > &vector_)
Definition: AIBase.h:193
virtual bool equals(const Premise &rhs_, unsigned int numParamsToCheck_) const
Definition: AIBase.cpp:23
Premise & operator=(int param_)
Definition: AIBase.h:85
Premise & operator+=(int param_)
Definition: AIBase.h:97
int assemble(std::ifstream &fin_, std::vector< T > &vector_)
Definition: AIBase.h:218
Definition: AIBase.h:52
float _badGoodScaleValue
ranking of the record for the target on a bad/good axis
Definition: AIBase.h:151
OpenSkyNet::Utils::PrecisionTimer g_timer
Definition: AIBase.cpp:11
int _stateIdAtCreation
state ID of target when record is created
Definition: AIBase.h:152
Definition: AIWrapper.h:40
std::vector< int > _params
_params[0] must be the premise ID
Definition: AIBase.h:54
Record & operator=(int param_)
Definition: AIBase.h:170
OpenSkyNet::AI::Wrapper * _n
the target of the record (if any)
Definition: AIBase.h:150
virtual ~Action()
empty destructor
Definition: AIBase.h:138
Record(const Premise &premise_)
Definition: AIBase.h:164
Definition: AIBase.h:122
virtual ~Premise()
empty destructor
Definition: AIBase.h:116
Action & operator+=(int param_)
Definition: AIBase.h:133
Definition: UPrecisionTimer.h:58
Premise()
Definition: AIBase.h:61
Action(const std::vector< int > &params_)
Definition: AIBase.h:126
virtual int assemble(std::ifstream &fin_)
Definition: AIBase.cpp:51
Action()
Definition: AIBase.h:124
Record(const std::vector< int > &params_, OpenSkyNet::AI::Wrapper *n_=0, float badGoodScaleValue_=0.0f, int stateIdAtCreation_=-1)
Definition: AIBase.h:159