[Change] Improved the chase ai_face with customizable velocity weight.

This commit is contained in:
Allanis 2013-03-14 21:58:38 +00:00
parent 63f814c695
commit 2d49ee188e
2 changed files with 18 additions and 5 deletions

View File

@ -607,7 +607,7 @@ static int ai_turn(lua_State* L) {
// Face the target.
static int ai_face(lua_State* L) {
MIN_ARGS(1);
Vec2* v; // Grab the position to face.
Vec2* v, sv, tv; // Grab the position to face.
Pilot* p;
double mod, diff;
int invert = 0;
@ -619,16 +619,28 @@ static int ai_face(lua_State* L) {
if(n >= 0) {
p = pilot_get(n);
if(p == NULL) return 0; // Make sure pilot is valid.
v = &p->solid->pos;
vect_cset(&tv, VX(p->solid->pos) + FACE_WVEL*VX(p->solid->vel),
VY(p->solid->pos) + FACE_WVEL*VY(p->solid->vel));
v = NULL;
}
else if(lua_islightuserdata(L,1)) v = (Vec2*)lua_topointer(L,1);
mod = -10;
if(lua_gettop(L) > 1 && lua_isnumber(L,2)) invert = (int)lua_tonumber(L,2);
if(invert) mod *= -1;
diff = angle_diff(cur_pilot->solid->dir,
(n==-1) ? VANGLE(cur_pilot->solid->pos) :
vect_angle(&cur_pilot->solid->pos, v));
vect_cset(&sv, VX(cur_pilot->solid->pos) + FACE_WVEL*VX(cur_pilot->solid->vel),
VY(cur_pilot->solid->pos) + FACE_WVEL*VY(cur_pilot->solid->vel));
if(v != NULL)
// Target is static.
diff = angle_diff(cur_pilot->solid->dir,
(n==-1) ? VANGLE(cur_pilot->solid->pos) :
vect_angle(&cur_pilot->solid->pos, v));
else
// Target is dynamic.
diff = angle_diff(cur_pilot->solid->dir,
(n==-1) ? VANGLE(sv) :
vect_angle(&sv, &tv));
pilot_turn = mod*diff;

View File

@ -5,6 +5,7 @@
#define MAX_DIR_ERR 0.1*M_PI/180.
#define MIN_VEL_ERR 1.0
#define FACE_WVEL 0.1 // Weight of velocity compared to position to face.
// Max number of AI timers.
#define MAX_AI_TIMERS 2