[Change] Improved the chase ai_face with customizable velocity weight.
This commit is contained in:
parent
63f814c695
commit
2d49ee188e
16
src/ai.c
16
src/ai.c
@ -607,7 +607,7 @@ static int ai_turn(lua_State* L) {
|
|||||||
// Face the target.
|
// Face the target.
|
||||||
static int ai_face(lua_State* L) {
|
static int ai_face(lua_State* L) {
|
||||||
MIN_ARGS(1);
|
MIN_ARGS(1);
|
||||||
Vec2* v; // Grab the position to face.
|
Vec2* v, sv, tv; // Grab the position to face.
|
||||||
Pilot* p;
|
Pilot* p;
|
||||||
double mod, diff;
|
double mod, diff;
|
||||||
int invert = 0;
|
int invert = 0;
|
||||||
@ -619,16 +619,28 @@ static int ai_face(lua_State* L) {
|
|||||||
if(n >= 0) {
|
if(n >= 0) {
|
||||||
p = pilot_get(n);
|
p = pilot_get(n);
|
||||||
if(p == NULL) return 0; // Make sure pilot is valid.
|
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);
|
else if(lua_islightuserdata(L,1)) v = (Vec2*)lua_topointer(L,1);
|
||||||
|
|
||||||
mod = -10;
|
mod = -10;
|
||||||
if(lua_gettop(L) > 1 && lua_isnumber(L,2)) invert = (int)lua_tonumber(L,2);
|
if(lua_gettop(L) > 1 && lua_isnumber(L,2)) invert = (int)lua_tonumber(L,2);
|
||||||
if(invert) mod *= -1;
|
if(invert) mod *= -1;
|
||||||
|
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,
|
diff = angle_diff(cur_pilot->solid->dir,
|
||||||
(n==-1) ? VANGLE(cur_pilot->solid->pos) :
|
(n==-1) ? VANGLE(cur_pilot->solid->pos) :
|
||||||
vect_angle(&cur_pilot->solid->pos, v));
|
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;
|
pilot_turn = mod*diff;
|
||||||
|
|
||||||
|
1
src/ai.h
1
src/ai.h
@ -5,6 +5,7 @@
|
|||||||
#define MAX_DIR_ERR 0.1*M_PI/180.
|
#define MAX_DIR_ERR 0.1*M_PI/180.
|
||||||
#define MIN_VEL_ERR 1.0
|
#define MIN_VEL_ERR 1.0
|
||||||
|
|
||||||
|
#define FACE_WVEL 0.1 // Weight of velocity compared to position to face.
|
||||||
|
|
||||||
// Max number of AI timers.
|
// Max number of AI timers.
|
||||||
#define MAX_AI_TIMERS 2
|
#define MAX_AI_TIMERS 2
|
||||||
|
Loading…
Reference in New Issue
Block a user