[Fix] Improved sanity on some AI functions.
This commit is contained in:
parent
869a6d7dbe
commit
e10841653d
@ -6,12 +6,24 @@ control_rate = 2
|
|||||||
-- Required "control" function.
|
-- Required "control" function.
|
||||||
function control()
|
function control()
|
||||||
task = ai.taskname()
|
task = ai.taskname()
|
||||||
if task == "hyperspace" then
|
enemy = ai.getenemy()
|
||||||
|
|
||||||
|
-- Runaway if enemy is near.
|
||||||
|
if task ~= "runaway" and enemy ~= nil and ai.dist(enemy) < 500 then
|
||||||
|
ai.poptask();
|
||||||
|
ai.pushtask(0, "runaway", enemy)
|
||||||
|
|
||||||
|
-- Enter hyperspace if possible.
|
||||||
|
elseif task == "hyperspace" then
|
||||||
ai.hyperspace() -- Try to go to hyperspace.
|
ai.hyperspace() -- Try to go to hyperspace.
|
||||||
|
|
||||||
|
-- Try to jump when far enough away.
|
||||||
elseif task == "runaway" then
|
elseif task == "runaway" then
|
||||||
if ai.dist(ai.pos(ai.targetid())) > 300 then
|
if ai.dist(ai.pos(ai.targetid())) > 400 then
|
||||||
ai.hyperspace()
|
ai.hyperspace()
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- Find something to do.
|
||||||
elseif task == "none" then
|
elseif task == "none" then
|
||||||
planet = ai.landplanet()
|
planet = ai.landplanet()
|
||||||
-- Planet must exist.
|
-- Planet must exist.
|
||||||
@ -80,7 +92,7 @@ function stop()
|
|||||||
if ai.isstopped() then
|
if ai.isstopped() then
|
||||||
ai.stop()
|
ai.stop()
|
||||||
ai.poptask()
|
ai.poptask()
|
||||||
ai.settimer(0, rnd.int(8000, 15000))
|
ai.settimer(0, rnd.int(8000, 15000)) -- We wait during a while.
|
||||||
ai.pushtask(0, "land")
|
ai.pushtask(0, "land")
|
||||||
else
|
else
|
||||||
ai.brake()
|
ai.brake()
|
||||||
|
21
src/ai.c
21
src/ai.c
@ -548,6 +548,7 @@ static int ai_pshield(lua_State* L) {
|
|||||||
static int ai_getdistance(lua_State* L) {
|
static int ai_getdistance(lua_State* L) {
|
||||||
Vec2* vect;
|
Vec2* vect;
|
||||||
Pilot* pilot;
|
Pilot* pilot;
|
||||||
|
unsigned int n;
|
||||||
|
|
||||||
LLUA_MIN_ARGS(1);
|
LLUA_MIN_ARGS(1);
|
||||||
|
|
||||||
@ -557,13 +558,16 @@ static int ai_getdistance(lua_State* L) {
|
|||||||
|
|
||||||
/* Pilot id as parameter. */
|
/* Pilot id as parameter. */
|
||||||
else if(lua_isnumber(L, 1)) {
|
else if(lua_isnumber(L, 1)) {
|
||||||
|
n = (unsigned int)lua_tonumber(L,1);
|
||||||
pilot = pilot_get((unsigned int) lua_tonumber(L, 1));
|
pilot = pilot_get((unsigned int) lua_tonumber(L, 1));
|
||||||
vect = &pilot->solid->pos;
|
if(pilot == NULL) {
|
||||||
} else {
|
LLUA_DEBUG("Pilot '%d' not found in stack", n);
|
||||||
/* Wrong parameter. */
|
|
||||||
LLUA_INVALID_PARAMETER();
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
vect = &pilot->solid->pos;
|
||||||
|
} else
|
||||||
|
/* Wrong parameter. */
|
||||||
|
LLUA_INVALID_PARAMETER();
|
||||||
|
|
||||||
lua_pushnumber(L, vect_dist(vect, &cur_pilot->solid->pos));
|
lua_pushnumber(L, vect_dist(vect, &cur_pilot->solid->pos));
|
||||||
return 1;
|
return 1;
|
||||||
@ -917,7 +921,14 @@ static int ai_shoot(lua_State* L) {
|
|||||||
|
|
||||||
/* Get the nearest enemy. */
|
/* Get the nearest enemy. */
|
||||||
static int ai_getenemy(lua_State* L) {
|
static int ai_getenemy(lua_State* L) {
|
||||||
lua_pushnumber(L,pilot_getNearestEnemy(cur_pilot));
|
unsigned int p;
|
||||||
|
|
||||||
|
p = pilot_getNearestEnemy(cur_pilot);
|
||||||
|
|
||||||
|
if(p == 0) /* No enemy found. */
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
lua_pushnumber(L,p);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user