Inexor
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ai.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <limits.h> // for USHRT_MAX
4 #include <string.h> // for memset, memmove
5 
6 #include "inexor/network/SharedVar.hpp" // for SharedVar
7 #include "inexor/shared/cube_loops.hpp" // for i, loopv, loopi, k, loopk
8 #include "inexor/shared/cube_types.hpp" // for ushort
9 #include "inexor/shared/cube_vector.hpp" // for vector
10 #include "inexor/shared/geom.hpp" // for vec
11 #include "inexor/util/legacy_time.hpp" // for lastmillis
12 
13 struct fpsent;
14 
15 #define MAXBOTS 32
16 
17 namespace ai
18 {
19  const int MAXWAYPOINTS = USHRT_MAX - 2;
20  const int MAXWAYPOINTLINKS = 6;
21  const int WAYPOINTRADIUS = 16;
22 
23  const float MINWPDIST = 4.f; // is on top of
24  const float CLOSEDIST = 32.f; // is close
25  const float FARDIST = 128.f; // too far to remap close
26  const float JUMPMIN = 4.f; // decides to jump
27  const float JUMPMAX = 32.f; // max jump
28  const float SIGHTMIN = 64.f; // minimum line of sight
29  const float SIGHTMAX = 1024.f; // maximum line of sight
30  const float VIEWMIN = 90.f; // minimum field of view
31  const float VIEWMAX = 180.f; // maximum field of view
32 
33  struct waypoint
34  {
35  vec o;
37  int weight;
40 
41  waypoint() {}
42  waypoint(const vec &o, int weight = 0) : o(o), weight(weight), route(0) { memset(links, 0, sizeof(links)); }
43 
44  int score() const { return int(curscore) + int(estscore); }
45 
46  int find(int wp)
47  {
48  loopi(MAXWAYPOINTLINKS) if(links[i] == wp) return i;
49  return -1;
50  }
51 
52  bool haslinks() { return links[0]!=0; }
53  };
55 
56  static inline bool iswaypoint(int n)
57  {
58  return n > 0 && n < waypoints.length();
59  }
60 
62  extern int closestwaypoint(const vec &pos, float mindist, bool links, fpsent *d = nullptr);
63  extern void findwaypointswithin(const vec &pos, float mindist, float maxdist, vector<int> &results);
64  extern void inferwaypoints(fpsent *d, const vec &o, const vec &v, float mindist = ai::CLOSEDIST);
65 
66  struct avoidset
67  {
68  struct obstacle
69  {
70  void *owner;
72  float above;
73 
74  obstacle(void *owner, float above = -1) : owner(owner), numwaypoints(0), above(above) {}
75  };
76 
79 
80  void clear()
81  {
82  obstacles.setsize(0);
83  waypoints.setsize(0);
84  }
85 
86  void add(void *owner, float above)
87  {
88  obstacles.add(obstacle(owner, above));
89  }
90 
91  void add(void *owner, float above, int wp)
92  {
93  if(obstacles.empty() || owner != obstacles.last().owner) add(owner, above);
94  obstacles.last().numwaypoints++;
95  waypoints.add(wp);
96  }
97 
99  {
100  waypoints.put(avoid.waypoints.getbuf(), avoid.waypoints.length());
101  loopv(avoid.obstacles)
102  {
103  obstacle &o = avoid.obstacles[i];
104  if(obstacles.empty() || o.owner != obstacles.last().owner) add(o.owner, o.above);
105  obstacles.last().numwaypoints += o.numwaypoints;
106  }
107  }
108 
109  void avoidnear(void *owner, float above, const vec &pos, float limit);
110 
111  #define loopavoid(v, d, body) \
112  if(!(v).obstacles.empty()) \
113  { \
114  int cur = 0; \
115  loopv((v).obstacles) \
116  { \
117  const ai::avoidset::obstacle &ob = (v).obstacles[i]; \
118  int next = cur + ob.numwaypoints; \
119  if(ob.owner != d) \
120  { \
121  for(; cur < next; cur++) \
122  { \
123  int wp = (v).waypoints[cur]; \
124  body; \
125  } \
126  } \
127  cur = next; \
128  } \
129  }
130 
131  bool find(int n, fpsent *d) const
132  {
133  loopavoid(*this, d, { if(wp == n) return true; });
134  return false;
135  }
136 
137  int remap(fpsent *d, int n, vec &pos, bool retry = false);
138  };
139 
140  extern bool route(fpsent *d, int node, int goal, vector<int> &route, const avoidset &obstacles, int retries = 0);
141  extern void navigate();
142  extern void clearwaypoints(bool full = false);
143  extern void seedwaypoints();
144  extern void loadwaypoints(bool force = false, const char *mname = nullptr);
145  extern void savewaypoints(bool force = false, const char *mname = nullptr);
146 
147  // ai state information for the owner client
148  enum
149  {
150  AI_S_WAIT = 0, // waiting for next command
151  AI_S_DEFEND, // defend goal target
152  AI_S_PURSUE, // pursue goal target
153  AI_S_INTEREST, // interest in goal entity
155  };
156 
157  enum
158  {
164  };
165 
166  struct interest
167  {
169  float score;
170  interest() : state(-1), node(-1), target(-1), targtype(-1), score(0.f) {}
172  };
173 
174  struct aistate
175  {
177  bool override;
178 
179  aistate(int m, int t, int r = -1, int v = -1) : type(t), millis(m), targtype(r), target(v)
180  {
181  reset();
182  }
183  ~aistate() {}
184 
185  void reset()
186  {
187  idle = 0;
188  override = false;
189  }
190  };
191 
192  const int NUMPREVNODES = 6;
193 
194  struct aiinfo
195  {
201  float targyaw, targpitch, views[3], aimrnd[3];
203 
205  {
206  clearsetup();
207  reset();
208  loopk(3) views[k] = 0.f;
209  }
210  ~aiinfo() {}
211 
212  void clearsetup();
213 
214  void clear(bool prev = false)
215  {
216  if(prev) memset(prevnodes, -1, sizeof(prevnodes));
217  route.setsize(0);
218  }
219 
220  void wipe(bool prev = false)
221  {
222  clear(prev);
223  state.setsize(0);
225  trywipe = false;
226  }
227 
228  void clean(bool tryit = false);
229 
230  void reset(bool tryit = false) { wipe(); clean(tryit); }
231 
232  bool hasprevnode(int n) const
233  {
234  loopi(NUMPREVNODES) if(prevnodes[i] == n) return true;
235  return false;
236  }
237 
238  void addprevnode(int n)
239  {
240  if(prevnodes[0] != n)
241  {
242  memmove(&prevnodes[1], prevnodes, sizeof(prevnodes) - sizeof(prevnodes[0]));
243  prevnodes[0] = n;
244  }
245  }
246 
247  aistate &addstate(int t, int r = -1, int v = -1)
248  {
249  return state.add(aistate(lastmillis, t, r, v));
250  }
251 
252  void removestate(int index = -1)
253  {
254  if(index < 0) state.pop();
255  else if(state.inrange(index)) state.remove(index);
256  if(!state.length()) addstate(AI_S_WAIT);
257  }
258 
259  aistate &getstate(int idx = -1)
260  {
261  if(state.inrange(idx)) return state[idx];
262  return state.last();
263  }
264 
265  aistate &switchstate(aistate &b, int t, int r = -1, int v = -1)
266  {
267  if((b.type == t && b.targtype == r) || (b.type == AI_S_INTEREST && b.targtype == AI_T_NODE))
268  {
269  b.millis = lastmillis;
270  b.target = v;
271  b.reset();
272  return b;
273  }
274  return addstate(t, r, v);
275  }
276  };
277 
278  extern avoidset obstacles;
279  extern vec aitarget;
280 
281  extern float viewdist(int x = 101);
282  extern float viewfieldx(int x = 101);
283  extern float viewfieldy(int x = 101);
284  extern bool targetable(fpsent *d, fpsent *e);
285  extern bool cansee(fpsent *d, vec &x, vec &y, vec &targ = aitarget);
286 
287  extern void init(fpsent *d, int at, int on, int sk, int bn, int pm, const char *name, const char *team, const char *tag);
288  extern void update();
289  extern void avoid();
290  extern void think(fpsent *d, bool run);
291 
292  extern bool badhealth(fpsent *d);
293  extern bool checkothers(vector<int> &targets, fpsent *d = nullptr, int state = -1, int targtype = -1, int target = -1, bool teams = false, int *members = nullptr);
294  extern bool makeroute(fpsent *d, aistate &b, int node, bool changed = true, int retries = 0);
295  extern bool makeroute(fpsent *d, aistate &b, const vec &pos, bool changed = true, int retries = 0);
296  extern bool randomnode(fpsent *d, aistate &b, const vec &pos, float guard = SIGHTMIN, float wander = SIGHTMAX);
297  extern bool randomnode(fpsent *d, aistate &b, float guard = SIGHTMIN, float wander = SIGHTMAX);
298  extern bool violence(fpsent *d, aistate &b, fpsent *e, int pursue = 0);
299  extern bool patrol(fpsent *d, aistate &b, const vec &pos, float guard = SIGHTMIN, float wander = SIGHTMAX, int walk = 1, bool retry = false);
300  extern bool defend(fpsent *d, aistate &b, const vec &pos, float guard = SIGHTMIN, float wander = SIGHTMAX, int walk = 1);
301  extern void assist(fpsent *d, aistate &b, vector<interest> &interests, bool all = false, bool force = false);
302  extern bool parseinterests(fpsent *d, aistate &b, vector<interest> &interests, bool override = false, bool ignore = false);
303 
304  extern void spawned(fpsent *d);
305  extern void damaged(fpsent *d, fpsent *e);
306  extern void killed(fpsent *d, fpsent *e);
307  extern void itemspawned(int ent);
308 
309  extern void render();
310 }
311 
312 
void killed(fpsent *d, fpsent *e)
Definition: ai.cpp:632
const float FARDIST
Definition: ai.hpp:25
bool patrol(fpsent *d, aistate &b, const vec &pos, float guard, float wander, int walk, bool retry)
Definition: ai.cpp:333
void savewaypoints(bool force=false, const char *mname=nullptr)
Definition: waypoint.cpp:760
int enemyseen
Definition: ai.hpp:199
void put(const T &v)
write bytes to vector
Definition: cube_vector.hpp:235
void add(void *owner, float above, int wp)
Definition: ai.hpp:91
bool becareful
Definition: ai.hpp:202
Vector template.
Definition: cube_vector.hpp:22
int idle
Definition: ai.hpp:176
void reset(bool tryit=false)
Definition: ai.hpp:230
void add(void *owner, float above)
Definition: ai.hpp:86
ushort links[MAXWAYPOINTLINKS]
Definition: ai.hpp:39
bool randomnode(fpsent *d, aistate &b, const vec &pos, float guard, float wander)
Definition: ai.cpp:287
bool parseinterests(fpsent *d, aistate &b, vector< interest > &interests, bool override, bool ignore)
Definition: ai.cpp:500
void addprevnode(int n)
Definition: ai.hpp:238
avoidset obstacles
Definition: ai.cpp:46
int huntseq
Definition: ai.hpp:199
const float JUMPMIN
Definition: ai.hpp:26
int millis
Definition: ai.hpp:176
int jumprand
Definition: ai.hpp:199
float estscore
Definition: ai.hpp:36
int blockseq
Definition: ai.hpp:199
bool violence(fpsent *d, aistate &b, fpsent *e, int pursue)
Definition: ai.cpp:373
Definition: ai.hpp:150
void wipe(bool prev=false)
Definition: ai.hpp:220
int prevnodes[NUMPREVNODES]
Definition: ai.hpp:199
int weappref
Definition: ai.hpp:199
~aistate()
Definition: ai.hpp:183
const float MINWPDIST
Definition: ai.hpp:23
const float CLOSEDIST
Definition: ai.hpp:24
Definition: ai.hpp:152
const float VIEWMIN
Definition: ai.hpp:30
waypoint(const vec &o, int weight=0)
Definition: ai.hpp:42
int weight
Definition: ai.hpp:37
T * getbuf()
get the whole vector
Definition: cube_vector.hpp:169
int enemymillis
Definition: ai.hpp:199
float viewdist(int x)
Definition: ai.cpp:60
int targseq
Definition: ai.hpp:199
bool checkothers(vector< int > &targets, fpsent *d, int state, int targtype, int target, bool teams, int *members)
Definition: ai.cpp:248
int enemy
Definition: ai.hpp:199
bool makeroute(fpsent *d, aistate &b, int node, bool changed, int retries)
Definition: ai.cpp:267
vector< waypoint > waypoints
Definition: waypoint.cpp:40
Definition: ai.hpp:154
Definition: ai.hpp:153
bool defend(fpsent *d, aistate &b, const vec &pos, float guard, float wander, int walk)
Definition: ai.cpp:358
void clear(bool prev=false)
Definition: ai.hpp:214
int targtype
Definition: ai.hpp:168
vector< aistate > state
Definition: ai.hpp:196
Definition: ai.hpp:163
vector< obstacle > obstacles
Definition: ai.hpp:77
Definition: ai.hpp:66
int score() const
Definition: ai.hpp:44
bool trywipe
Definition: ai.hpp:202
float targyaw
Definition: ai.hpp:201
vec o
Definition: ai.hpp:35
void damaged(fpsent *d, fpsent *e)
Definition: ai.cpp:583
Definition: ai.hpp:174
void clearsetup()
Definition: ai.cpp:1515
void avoid()
Definition: ai.cpp:1301
SharedVar< int > showwaypoints
void clean(bool tryit=false)
Definition: ai.cpp:1508
int find(int wp)
Definition: ai.hpp:46
float aimrnd[3]
Definition: ai.hpp:201
Definition: ai.hpp:166
ICOMMAND * f(float *a, float *b), floatret(*a **b)
float viewfieldy(int x)
Definition: ai.cpp:70
#define loopavoid(v, d, body)
Definition: ai.hpp:111
unsigned short ushort
Definition: cube_types.hpp:8
void at(tagval *args, int numargs)
Definition: command.cpp:2678
int node
Definition: ai.hpp:168
void setsize(int i)
shrink vector memory size
Definition: cube_vector.hpp:163
int lastrun
Definition: ai.hpp:199
obstacle(void *owner, float above=-1)
Definition: ai.hpp:74
Definition: ai.hpp:151
else loopi(numargs)
Definition: command.cpp:3019
bool hasprevnode(int n) const
Definition: ai.hpp:232
void update()
Definition: ai.cpp:226
aistate(int m, int t, int r=-1, int v=-1)
Definition: ai.hpp:179
int state
Definition: ai.hpp:168
GLuint index
Definition: glexts.hpp:412
static enum recorder::@19 state
void run(int dir, int mode)
Definition: octaedit.cpp:1964
int targtime
Definition: ai.hpp:199
int lastmillis
Definition: legacy_time.cpp:14
int remap(fpsent *d, int n, vec &pos, bool retry=false)
Definition: waypoint.cpp:403
int targnode
Definition: ai.hpp:199
const float SIGHTMIN
Definition: ai.hpp:28
T & add(const T &x)
Add new index to vector.
Definition: cube_vector.hpp:73
vec target
Definition: ai.hpp:198
void removestate(int index=-1)
Definition: ai.hpp:252
vec aitarget(0, 0, 0)
Definition: ai.hpp:279
vector with 3 floats and some useful methods.
Definition: geom.hpp:110
void assist(fpsent *d, aistate &b, vector< interest > &interests, bool all, bool force)
Definition: ai.cpp:433
void loadwaypoints(bool force=false, const char *mname=nullptr)
Definition: waypoint.cpp:719
static bool iswaypoint(int n)
Definition: ai.hpp:56
const float JUMPMAX
Definition: ai.hpp:27
void clearwaypoints(bool full=false)
Definition: waypoint.cpp:630
void inferwaypoints(fpsent *d, const vec &o, const vec &v, float mindist=ai::CLOSEDIST)
Definition: waypoint.cpp:575
float views[3]
Definition: ai.hpp:201
float viewfieldx(int x)
Definition: ai.cpp:65
ushort prev
Definition: ai.hpp:38
void clear()
Definition: ai.hpp:80
bool tryreset
Definition: ai.hpp:202
const int MAXWAYPOINTLINKS
Definition: ai.hpp:20
SharedVar< int > dropwaypoints
vector< int > route
Definition: ai.hpp:197
static vector< int > targets
Definition: ai.cpp:498
Definition: ai.hpp:33
void ignore(int cn)
ignore all chat messags from a certain client number
Definition: client.cpp:509
const int WAYPOINTRADIUS
Definition: ai.hpp:21
int numwaypoints
Definition: ai.hpp:71
int length() const
return size of used memory
Definition: cube_vector.hpp:146
Definition: ai.hpp:159
int target
Definition: ai.hpp:168
void add(avoidset &avoid)
Definition: ai.hpp:98
bool find(int n, fpsent *d) const
Definition: ai.hpp:131
Definition: ai.hpp:162
Definition: ai.hpp:194
bool dontmove
Definition: ai.hpp:202
void reset()
Definition: ai.hpp:185
int d
Definition: octaedit.cpp:1749
void t(T x, const char *cmp)
Definition: utilTest.cpp:52
aistate & switchstate(aistate &b, int t, int r=-1, int v=-1)
Definition: ai.hpp:265
void think(fpsent *d, bool run)
Definition: ai.cpp:1317
float targpitch
Definition: ai.hpp:201
bool haslinks()
Definition: ai.hpp:52
void init(fpsent *d, int at, int ocn, int sk, int bn, int pm, const char *name, const char *team, const char *tag)
Definition: ai.cpp:179
#define loopk(m)
Definition: cube_loops.hpp:10
int lasthunt
Definition: ai.hpp:199
int targtype
Definition: ai.hpp:176
int target
Definition: ai.hpp:176
waypoint()
Definition: ai.hpp:41
Definition: ai.hpp:160
int lastaimrnd
Definition: ai.hpp:199
const float SIGHTMAX
Definition: ai.hpp:29
aiinfo()
Definition: ai.hpp:204
bool badhealth(fpsent *d)
Definition: ai.cpp:306
float score
Definition: ai.hpp:169
float above
Definition: ai.hpp:72
void itemspawned(int ent)
Definition: ai.cpp:637
Definition: fpsent.hpp:12
vector< int > waypoints
Definition: ai.hpp:78
aistate & addstate(int t, int r=-1, int v=-1)
Definition: ai.hpp:247
void navigate()
Definition: waypoint.cpp:624
void seedwaypoints()
Definition: waypoint.cpp:642
Definition: ai.hpp:68
bool targetable(fpsent *d, fpsent *e)
Definition: ai.cpp:96
const int NUMPREVNODES
Definition: ai.hpp:192
void findwaypointswithin(const vec &pos, float mindist, float maxdist, vector< int > &results)
Definition: waypoint.cpp:298
SharedVar wrapper for primitive/immutable objects.
Definition: SharedVar.hpp:55
int targlast
Definition: ai.hpp:199
int type
Definition: ai.hpp:176
void changed(const block3 &sel, bool commit=true)
validates editing changes using readychanges() and calls commitchanges()
Definition: octaedit.cpp:725
float curscore
Definition: ai.hpp:36
bool route(fpsent *d, int node, int goal, vector< int > &route, const avoidset &obstacles, int retries=0)
Definition: waypoint.cpp:453
interest()
Definition: ai.hpp:170
mathmatics for vectors, matrices, quaterions and more
aistate & getstate(int idx=-1)
Definition: ai.hpp:259
int lastaction
Definition: ai.hpp:199
bool cansee(fpsent *d, vec &x, vec &y, vec &targ)
Definition: ai.cpp:115
~interest()
Definition: ai.hpp:171
const float VIEWMAX
Definition: ai.hpp:31
void * owner
Definition: ai.hpp:70
~aiinfo()
Definition: ai.hpp:210
bool target(fpsent *d, aistate &b, int pursue=0, bool force=false, float mindist=0.f)
Definition: ai.cpp:393
#define loopv(v)
Definition: cube_loops.hpp:21
const int MAXWAYPOINTS
Definition: ai.hpp:19
ushort route
Definition: ai.hpp:38
void avoidnear(void *owner, float above, const vec &pos, float limit)
Definition: waypoint.cpp:351
int lastcheck
Definition: ai.hpp:199
int jumpseed
Definition: ai.hpp:199
int closestwaypoint(const vec &pos, float mindist, bool links, fpsent *d=nullptr)
Definition: waypoint.cpp:241
int blocktime
Definition: ai.hpp:199
Definition: ai.hpp:161
vec spot
Definition: ai.hpp:198
void render()
Definition: ai.cpp:1412
void spawned(fpsent *d)
Definition: ai.cpp:627