From 8ef7082c841dabceb76e79cfde4c1c02c5bb1ae0 Mon Sep 17 00:00:00 2001 From: Allanis Date: Sun, 15 Dec 2013 00:30:50 +0000 Subject: [PATCH] [Add] Gave AI some memory. --- dat/planet.xml | 4 +-- scripts/ai/include/basic.lua | 17 ++++++------ scripts/ai/tpl/generic.lua | 11 +++++--- scripts/ai/tpl/merchant.lua | 3 ++- src/ai.c | 50 ++++++++++++++++++++++-------------- 5 files changed, 50 insertions(+), 35 deletions(-) diff --git a/dat/planet.xml b/dat/planet.xml index 271b1c2..bbaa66e 100644 --- a/dat/planet.xml +++ b/dat/planet.xml @@ -466,8 +466,8 @@ 002.png - -33 - -57 + -53 + -77 The Darkshed cantina is decorated with all sorts of odd artifacts that marked different tendencies in spaceship outfitting. diff --git a/scripts/ai/include/basic.lua b/scripts/ai/include/basic.lua index 42441b3..a80be04 100644 --- a/scripts/ai/include/basic.lua +++ b/scripts/ai/include/basic.lua @@ -9,10 +9,10 @@ -- Attempt to land on a planet. --]] function land() - target = ai.target() - dir = ai.face(target) - dist = ai.dist(target) - bdist = ai.minbrakedist() + target = mem.land + dir = ai.face(target) + dist = ai.dist(target) + bdist = ai.minbrakedist() -- Need to get closer. if dir < 10 and dist > bdist then @@ -28,22 +28,21 @@ function landstop() ai.brake() if ai.isstopped() then - target = ai.target() ai.stop() -- Will stop the pilot if below err vel. ai.settime(0, rnd.int(8000, 15000)) -- We wait during a while. ai.poptask() - ai.pushtask(0, "landwait", target) + ai.pushtask(0, "landwait") end end function landwait() - target = ai.target() + target = mem.land dist = ai.dist(target) -- In case for some reason landed far away.. - if dist > 100 then + if dist > 50 then ai.poptask() - ai.pushtask(0, "land", target) + ai.pushtask(0, "land") elseif ai.timeup(0) then ai.poptask() -- Ready to do whatever we were doing before. diff --git a/scripts/ai/tpl/generic.lua b/scripts/ai/tpl/generic.lua index d71bdea..42344f6 100644 --- a/scripts/ai/tpl/generic.lua +++ b/scripts/ai/tpl/generic.lua @@ -21,8 +21,12 @@ function control() task = ai.taskname() enemy = ai.getenemy() + -- Get new task. + if task == "none" then + idle() + -- Think for attacking. - if task == "attack" then + elseif task == "attack" then -- Runaway if needed. if(shield_run > 0 and ai.pshield() < shield_run) or (armour_run > 0 and ai.parmour() < armour_run) then @@ -54,8 +58,6 @@ function control() -- Enter hyperspace if possible. elseif task == "hyperspace" then ai.hyperspace() -- Try to hyperspace. - else -- Get new task. - idle() end end @@ -82,8 +84,9 @@ function idle() if planet == nil or land_planet == false then ai.pushtask(0, "hyperspace") else + mem.land = planet ai.pushtask(0, "hyperspace") - ai.pushtask(0, "land", planet) + ai.pushtask(0, "land") end end diff --git a/scripts/ai/tpl/merchant.lua b/scripts/ai/tpl/merchant.lua index e113d8e..26b6a71 100644 --- a/scripts/ai/tpl/merchant.lua +++ b/scripts/ai/tpl/merchant.lua @@ -32,8 +32,9 @@ function control() if planet == nil then ai.pushtask(0, "hyperspace") else + mem.land = planet ai.pushtask(0, "hyperspace") - ai.pushtask(0, "land", planet) + ai.pushtask(0, "land") end end end diff --git a/src/ai.c b/src/ai.c index 3ad64a0..2adc59a 100644 --- a/src/ai.c +++ b/src/ai.c @@ -58,6 +58,7 @@ #include "faction.h" #include "llua.h" #include "lluadef.h" +#include "llua_space.h" #include "ai.h" @@ -366,6 +367,9 @@ static int ai_loadProfile(char* filename) { luaL_register(L, "ai", ai_methods); lua_loadRnd(L); + /* Metatables to register. */ + lua_loadVector(L); + /* Now load the file, since all the functions have been previously loaded. */ buf = pack_readfile(DATA, filename, &bufsize); if(luaL_dobuffer(L, buf, bufsize, filename) != 0) { @@ -666,15 +670,20 @@ static int ai_pshield(lua_State* L) { /* Get the distance from the pointer. */ static int ai_getdistance(lua_State* L) { - Vec2* vect; + Vec2* v; + LuaVector* lv; Pilot* pilot; unsigned int n; LLUA_MIN_ARGS(1); /* Vector as a parameter. */ - if(lua_islightuserdata(L, 1)) - vect = (Vec2*)lua_topointer(L, 1); + if(lua_isvector(L, 1)) { + lv = lua_tovector(L, 1); + v = &lv->vec; + } + else if(lua_islightuserdata(L,1)) + v = lua_touserdata(L,1); /* Pilot id as parameter. */ else if(lua_isnumber(L, 1)) { @@ -684,12 +693,12 @@ static int ai_getdistance(lua_State* L) { LLUA_DEBUG("Pilot '%d' not found in stack", n); return 0; } - vect = &pilot->solid->pos; + v = &pilot->solid->pos; } else /* Wrong parameter. */ LLUA_INVALID_PARAMETER(); - lua_pushnumber(L, vect_dist(vect, &cur_pilot->solid->pos)); + lua_pushnumber(L, vect_dist(v, &cur_pilot->solid->pos)); return 1; } @@ -825,23 +834,26 @@ static int ai_turn(lua_State* L) { /* Face the target. */ static int ai_face(lua_State* L) { LLUA_MIN_ARGS(1); - Vec2* v, sv, tv; /* Grab the position to face. */ + LuaVector* lv; + Vec2 sv, tv; /* Grab the position to face. */ Pilot* p; double mod, diff; int n; /* Get first parameter, aka what to face. */ n = -2; - if(lua_isnumber(L, 1)) + if(lua_isnumber(L, 1)) { n = (int)lua_tonumber(L, 1); - if(n >= 0) { - p = pilot_get(n); - if(p == NULL) return 0; /* Make sure pilot is valid. */ - vect_cset(&tv, VX(p->solid->pos), VY(p->solid->pos)); - v = NULL; + if(n >= 0) { + p = pilot_get(n); + if(p == NULL) return 0; /* Make sure pilot is valid. */ + vect_cset(&tv, VX(p->solid->pos), VY(p->solid->pos)); + lv = NULL; + } } - else if(lua_islightuserdata(L,1)) v = (Vec2*)lua_topointer(L,1); + else if(lua_isvector(L, 1)) + lv = lua_tovector(L, 1); mod = 10; @@ -853,7 +865,7 @@ static int ai_face(lua_State* L) { vect_cset(&sv, VX(cur_pilot->solid->pos), VY(cur_pilot->solid->pos)); - if(v == NULL) + if(lv == NULL) /* Target is dynamic. */ diff = angle_diff(cur_pilot->solid->dir, (n==-1) ? VANGLE(sv) : @@ -862,7 +874,7 @@ static int ai_face(lua_State* L) { /* Target is static. */ diff = angle_diff(cur_pilot->solid->dir, (n==-1) ? VANGLE(cur_pilot->solid->pos) : - vect_angle(&cur_pilot->solid->pos, v)); + vect_angle(&cur_pilot->solid->pos, &lv->vec)); /* Make pilot turn. */ pilot_turn = mod*diff; @@ -934,7 +946,7 @@ static int ai_getrndplanet(lua_State* L) { static int ai_getlandplanet(lua_State* L) { Planet** planets; int nplanets, i; - Vec2 v; + LuaVector lv; planets = malloc(sizeof(Planet*) * cur_system->nplanets); if(cur_system->nplanets == 0) return 0; /* No planets. */ @@ -952,10 +964,10 @@ static int ai_getlandplanet(lua_State* L) { /* We can actually get a random planet now. */ i = RNG(0,nplanets-1); - vectcpy(&v, &planets[i]->pos); - vect_cadd(&v, RNG(0, planets[i]->gfx_space->sw)-planets[i]->gfx_space->sw/2., + vectcpy(&lv.vec, &planets[i]->pos); + vect_cadd(&lv, RNG(0, planets[i]->gfx_space->sw)-planets[i]->gfx_space->sw/2., RNG(0, planets[i]->gfx_space->sh)-planets[i]->gfx_space->sh/2.); - lua_pushlightuserdata(L, &v); + lua_pushvector(L, lv); free(planets); return 1; }