[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.
|
||||
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;
|
||||
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;
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user