Skip to content

Commit

Permalink
Justice clients can connect
Browse files Browse the repository at this point in the history
  • Loading branch information
greghaynes committed Jul 20, 2010
1 parent ee5b558 commit 030fb22
Show file tree
Hide file tree
Showing 7 changed files with 448 additions and 242 deletions.
2 changes: 1 addition & 1 deletion src/engine/command.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ void parsemacro(const char *&p, int level, vector<char> &wordbuf)
return;
}
static vector<char> ident;
ident.setsizenodelete(0);
ident.setsize(0);
while(isalnum(*p) || *p=='_') ident.add(*p++);
ident.add(0);
const char *alias = getalias(ident.getbuf());
Expand Down
135 changes: 80 additions & 55 deletions src/fpsgame/ai.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,24 @@ namespace ai
const int MAXWAYPOINTLINKS = 6;
const int WAYPOINTRADIUS = 16;

const float CLOSEDIST = WAYPOINTRADIUS; // is close
const float NEARDIST = CLOSEDIST*4.f; // is near
const float NEARDISTSQ = NEARDIST*NEARDIST; // .. squared (constant for speed)
const float FARDIST = CLOSEDIST*16.f; // too far
const float JUMPMIN = CLOSEDIST*0.25f; // decides to jump
const float JUMPMAX = CLOSEDIST*1.5f; // max jump
const float SIGHTMIN = CLOSEDIST*2.f; // minimum line of sight
const float SIGHTMAX = CLOSEDIST*64.f; // maximum line of sight
const float VIEWMIN = 70.f; // minimum field of view
const float VIEWMAX = 150.f; // maximum field of view
const float CLOSEDIST = 16.f; // is close
const float JUMPMIN = 4.f; // decides to jump
const float JUMPMAX = 32.f; // max jump
const float SIGHTMIN = 64.f; // minimum line of sight
const float SIGHTMAX = 1024.f; // maximum line of sight
const float VIEWMIN = 90.f; // minimum field of view
const float VIEWMAX = 180.f; // maximum field of view

struct waypoint
{
vec o;
short curscore, estscore;
float curscore, estscore;
int weight;
ushort route, prev;
ushort links[MAXWAYPOINTLINKS];

waypoint() {}
waypoint(const vec &o) : o(o), route(0) { memset(links, 0, sizeof(links)); }
waypoint(const vec &o, int weight = 0) : o(o), weight(weight), route(0) { memset(links, 0, sizeof(links)); }

int score() const { return int(curscore) + int(estscore); }

Expand All @@ -42,7 +40,7 @@ namespace ai
};
extern vector<waypoint> waypoints;

extern int closestwaypoint(const vec &pos, float mindist, bool links);
extern int closestwaypoint(const vec &pos, float mindist, bool links, fpsent *d = NULL);
extern void findwaypointswithin(const vec &pos, float mindist, float maxdist, vector<int> &results);
extern void inferwaypoints(fpsent *d, const vec &o, const vec &v, float mindist = ai::CLOSEDIST);

Expand All @@ -62,8 +60,8 @@ namespace ai

void clear()
{
obstacles.setsizenodelete(0);
waypoints.setsizenodelete(0);
obstacles.setsize(0);
waypoints.setsize(0);
}

void add(void *owner, float above)
Expand All @@ -78,6 +76,17 @@ namespace ai
waypoints.add(wp);
}

void add(avoidset &avoid)
{
waypoints.put(avoid.waypoints.getbuf(), avoid.waypoints.length());
loopv(avoid.obstacles)
{
obstacle &o = avoid.obstacles[i];
if(obstacles.empty() || o.owner != &obstacles.last().owner) add(o.owner, o.above);
obstacles.last().numwaypoints += o.numwaypoints;
}
}

void avoidnear(void *owner, float above, const vec &pos, float limit);

#define loopavoid(v, d, body) \
Expand Down Expand Up @@ -106,12 +115,11 @@ namespace ai
return false;
}

int remap(fpsent *d, int n, vec &pos);
int remap(fpsent *d, int n, vec &pos, bool retry = false);
};

extern bool route(fpsent *d, int node, int goal, vector<int> &route, const avoidset &obstacles, bool check = true);
extern void trydropwaypoint(fpsent *d);
extern void trydropwaypoints();
extern bool route(fpsent *d, int node, int goal, vector<int> &route, const avoidset &obstacles, bool retry = false);
extern void navigate();
extern void clearwaypoints(bool full = false);
extern void seedwaypoints();
extern void loadwaypoints(bool force = false, const char *mname = NULL);
Expand Down Expand Up @@ -146,7 +154,7 @@ namespace ai

struct aistate
{
int type, millis, next, targtype, target, idle;
int type, millis, targtype, target, idle;
bool override;

aistate(int m, int t, int r = -1, int v = -1) : type(t), millis(m), targtype(r), target(v)
Expand All @@ -157,63 +165,71 @@ namespace ai

void reset()
{
next = millis;
idle = 0;
override = false;
}
};

const int NUMPREVNODES = 3;
const int NUMPREVNODES = 6;

struct aiinfo
{
vector<aistate> state;
vector<int> route;
vec target, spot;
int enemy, enemyseen, enemymillis, weappref, prevnodes[NUMPREVNODES],
lastrun, lasthunt, lastaction, jumpseed, jumprand;
float targyaw, targpitch, views[3];
bool dontmove, tryreset, clear;
int enemy, enemyseen, enemymillis, weappref, prevnodes[NUMPREVNODES], targnode, targlast, targtime, targseq,
lastrun, lasthunt, lastaction, jumpseed, jumprand, blocktime, huntseq, blockseq, lastaimrnd;
float targyaw, targpitch, views[3], aimrnd[3];
bool dontmove, becareful, tryreset, trywipe;

aiinfo()
{
clearsetup();
reset();
loopk(3) views[k] = 0.f;
}
~aiinfo() {}

void clearsetup()
{
weappref = GUN_PISTOL;
spot = target = vec(0, 0, 0);
lastaction = lasthunt = enemyseen = enemymillis = blocktime = huntseq = blockseq = targtime = targseq = lastaimrnd = 0;
lastrun = jumpseed = lastmillis;
jumprand = lastmillis+5000;
targnode = targlast = enemy = -1;
}

void clear(bool prev = true)
{
if(prev) memset(prevnodes, -1, sizeof(prevnodes));
route.setsize(0);
}

void wipe()
{
clear(true);
state.setsize(0);
route.setsize(0);
addstate(AI_S_WAIT);
clear = false;
trywipe = false;
}

void reset(bool tryit = false)
void clean(bool tryit = false)
{
wipe();
if(!tryit)
{
weappref = GUN_PISTOL;
spot = target = vec(0, 0, 0);
enemy = -1;
lastaction = lasthunt = enemyseen = enemymillis = 0;
lastrun = jumpseed = lastmillis;
jumprand = lastmillis+5000;
dontmove = false;
}
memset(prevnodes, -1, sizeof(prevnodes));
if(!tryit) becareful = dontmove = false;
targyaw = rnd(360);
targpitch = 0.f;
tryreset = tryit;
}

void reset(bool tryit = false) { wipe(); clean(tryit); }

bool hasprevnode(int n) const
{
loopi(NUMPREVNODES) if(prevnodes[i] == n) return true;
return false;
}

void addprevnode(int n)
{
if(prevnodes[0] != n)
Expand All @@ -235,25 +251,32 @@ namespace ai
if(!state.length()) addstate(AI_S_WAIT);
}

aistate &setstate(int t, int r = 1, int v = -1, bool pop = true)
{
if(pop) removestate();
return addstate(t, r, v);
}

aistate &getstate(int idx = -1)
{
if(state.inrange(idx)) return state[idx];
return state.last();
}

aistate &switchstate(aistate &b, int t, int r = -1, int v = -1)
{
if(b.type == t && b.targtype == r)
{
b.millis = lastmillis;
b.target = v;
b.reset();
return b;
}
return addstate(t, r, v);
}
};

extern avoidset obstacles;
extern vec aitarget;

extern float viewdist(int x = 101);
extern float viewfieldx(int x = 101);
extern float viewfieldy(int x = 101);
extern bool targetable(fpsent *d, fpsent *e, bool anyone = true);
extern bool targetable(fpsent *d, fpsent *e);
extern bool cansee(fpsent *d, vec &x, vec &y, vec &targ = aitarget);

extern void init(fpsent *d, int at, int on, int sk, int bn, int pm, const char *name, const char *team);
Expand All @@ -263,18 +286,20 @@ namespace ai

extern bool badhealth(fpsent *d);
extern bool checkothers(vector<int> &targets, fpsent *d = NULL, int state = -1, int targtype = -1, int target = -1, bool teams = false);
extern bool makeroute(fpsent *d, aistate &b, int node, bool changed = true, bool check = true);
extern bool makeroute(fpsent *d, aistate &b, const vec &pos, bool changed = true, bool check = true);
extern bool randomnode(fpsent *d, aistate &b, const vec &pos, float guard = NEARDIST, float wander = FARDIST);
extern bool randomnode(fpsent *d, aistate &b, float guard = NEARDIST, float wander = FARDIST);
extern bool makeroute(fpsent *d, aistate &b, int node, bool changed = true, bool retry = false);
extern bool makeroute(fpsent *d, aistate &b, const vec &pos, bool changed = true, bool retry = false);
extern bool randomnode(fpsent *d, aistate &b, const vec &pos, float guard = SIGHTMIN, float wander = SIGHTMAX);
extern bool randomnode(fpsent *d, aistate &b, float guard = SIGHTMIN, float wander = SIGHTMAX);
extern bool violence(fpsent *d, aistate &b, fpsent *e, bool pursue = false);
extern bool patrol(fpsent *d, aistate &b, const vec &pos, float guard = NEARDIST, float wander = FARDIST, int walk = 1, bool retry = false);
extern bool defend(fpsent *d, aistate &b, const vec &pos, float guard = NEARDIST, float wander = FARDIST, int walk = 1);
extern bool patrol(fpsent *d, aistate &b, const vec &pos, float guard = SIGHTMIN, float wander = SIGHTMAX, int walk = 1, bool retry = false);
extern bool defend(fpsent *d, aistate &b, const vec &pos, float guard = SIGHTMIN, float wander = SIGHTMAX, int walk = 1);
extern void assist(fpsent *d, aistate &b, vector<interest> &interests, bool all = false, bool force = false);
extern bool parseinterests(fpsent *d, aistate &b, vector<interest> &interests, bool override = false, bool ignore = false);

extern void spawned(fpsent *d);
extern void damaged(fpsent *d, fpsent *e);
extern void killed(fpsent *d, fpsent *e);
extern void pickup(fpsent *d, extentity &e);
extern void itemspawned(int ent);

extern void render();
}
Expand Down

0 comments on commit 030fb22

Please sign in to comment.