From e10841653d0891f88b5eea0b729fa4aba9572545 Mon Sep 17 00:00:00 2001 From: Allanis Date: Sat, 10 Aug 2013 16:11:54 +0100 Subject: [PATCH] [Fix] Improved sanity on some AI functions. --- scripts/ai/merchant.lua | 18 +++++++++++++++--- src/ai.c | 19 +++++++++++++++---- 2 files changed, 30 insertions(+), 7 deletions(-) diff --git a/scripts/ai/merchant.lua b/scripts/ai/merchant.lua index 7bceae5..a3ee743 100644 --- a/scripts/ai/merchant.lua +++ b/scripts/ai/merchant.lua @@ -6,12 +6,24 @@ control_rate = 2 -- Required "control" function. function control() 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. + + -- Try to jump when far enough away. elseif task == "runaway" then - if ai.dist(ai.pos(ai.targetid())) > 300 then + if ai.dist(ai.pos(ai.targetid())) > 400 then ai.hyperspace() end + + -- Find something to do. elseif task == "none" then planet = ai.landplanet() -- Planet must exist. @@ -80,7 +92,7 @@ function stop() if ai.isstopped() then ai.stop() 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") else ai.brake() diff --git a/src/ai.c b/src/ai.c index 90772d1..68094d3 100644 --- a/src/ai.c +++ b/src/ai.c @@ -548,6 +548,7 @@ static int ai_pshield(lua_State* L) { static int ai_getdistance(lua_State* L) { Vec2* vect; Pilot* pilot; + unsigned int n; LLUA_MIN_ARGS(1); @@ -557,13 +558,16 @@ static int ai_getdistance(lua_State* L) { /* Pilot id as parameter. */ else if(lua_isnumber(L, 1)) { + n = (unsigned int)lua_tonumber(L,1); pilot = pilot_get((unsigned int) lua_tonumber(L, 1)); + if(pilot == NULL) { + LLUA_DEBUG("Pilot '%d' not found in stack", n); + return 0; + } vect = &pilot->solid->pos; - } else { + } else /* Wrong parameter. */ LLUA_INVALID_PARAMETER(); - return 0; - } lua_pushnumber(L, vect_dist(vect, &cur_pilot->solid->pos)); return 1; @@ -917,7 +921,14 @@ static int ai_shoot(lua_State* L) { /* Get the nearest enemy. */ 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; }