[Add] Gave AI some memory.

This commit is contained in:
Allanis 2013-12-15 00:30:50 +00:00
parent a782a3c60e
commit 8ef7082c84
5 changed files with 50 additions and 35 deletions

View File

@ -466,8 +466,8 @@
<space>002.png</space> <space>002.png</space>
</GFX> </GFX>
<pos> <pos>
<y>-33</y> <y>-53</y>
<x>-57</x> <x>-77</x>
</pos> </pos>
<general> <general>
<bar>The Darkshed cantina is decorated with all sorts of odd artifacts that marked different tendencies in spaceship outfitting.</bar> <bar>The Darkshed cantina is decorated with all sorts of odd artifacts that marked different tendencies in spaceship outfitting.</bar>

View File

@ -9,7 +9,7 @@
-- Attempt to land on a planet. -- Attempt to land on a planet.
--]] --]]
function land() function land()
target = ai.target() target = mem.land
dir = ai.face(target) dir = ai.face(target)
dist = ai.dist(target) dist = ai.dist(target)
bdist = ai.minbrakedist() bdist = ai.minbrakedist()
@ -28,22 +28,21 @@ function landstop()
ai.brake() ai.brake()
if ai.isstopped() then if ai.isstopped() then
target = ai.target()
ai.stop() -- Will stop the pilot if below err vel. ai.stop() -- Will stop the pilot if below err vel.
ai.settime(0, rnd.int(8000, 15000)) -- We wait during a while. ai.settime(0, rnd.int(8000, 15000)) -- We wait during a while.
ai.poptask() ai.poptask()
ai.pushtask(0, "landwait", target) ai.pushtask(0, "landwait")
end end
end end
function landwait() function landwait()
target = ai.target() target = mem.land
dist = ai.dist(target) dist = ai.dist(target)
-- In case for some reason landed far away.. -- In case for some reason landed far away..
if dist > 100 then if dist > 50 then
ai.poptask() ai.poptask()
ai.pushtask(0, "land", target) ai.pushtask(0, "land")
elseif ai.timeup(0) then elseif ai.timeup(0) then
ai.poptask() -- Ready to do whatever we were doing before. ai.poptask() -- Ready to do whatever we were doing before.

View File

@ -21,8 +21,12 @@ function control()
task = ai.taskname() task = ai.taskname()
enemy = ai.getenemy() enemy = ai.getenemy()
-- Get new task.
if task == "none" then
idle()
-- Think for attacking. -- Think for attacking.
if task == "attack" then elseif task == "attack" then
-- Runaway if needed. -- Runaway if needed.
if(shield_run > 0 and ai.pshield() < shield_run) or if(shield_run > 0 and ai.pshield() < shield_run) or
(armour_run > 0 and ai.parmour() < armour_run) then (armour_run > 0 and ai.parmour() < armour_run) then
@ -54,8 +58,6 @@ function control()
-- Enter hyperspace if possible. -- Enter hyperspace if possible.
elseif task == "hyperspace" then elseif task == "hyperspace" then
ai.hyperspace() -- Try to hyperspace. ai.hyperspace() -- Try to hyperspace.
else -- Get new task.
idle()
end end
end end
@ -82,8 +84,9 @@ function idle()
if planet == nil or land_planet == false then if planet == nil or land_planet == false then
ai.pushtask(0, "hyperspace") ai.pushtask(0, "hyperspace")
else else
mem.land = planet
ai.pushtask(0, "hyperspace") ai.pushtask(0, "hyperspace")
ai.pushtask(0, "land", planet) ai.pushtask(0, "land")
end end
end end

View File

@ -32,8 +32,9 @@ function control()
if planet == nil then if planet == nil then
ai.pushtask(0, "hyperspace") ai.pushtask(0, "hyperspace")
else else
mem.land = planet
ai.pushtask(0, "hyperspace") ai.pushtask(0, "hyperspace")
ai.pushtask(0, "land", planet) ai.pushtask(0, "land")
end end
end end
end end

View File

@ -58,6 +58,7 @@
#include "faction.h" #include "faction.h"
#include "llua.h" #include "llua.h"
#include "lluadef.h" #include "lluadef.h"
#include "llua_space.h"
#include "ai.h" #include "ai.h"
@ -366,6 +367,9 @@ static int ai_loadProfile(char* filename) {
luaL_register(L, "ai", ai_methods); luaL_register(L, "ai", ai_methods);
lua_loadRnd(L); lua_loadRnd(L);
/* Metatables to register. */
lua_loadVector(L);
/* Now load the file, since all the functions have been previously loaded. */ /* Now load the file, since all the functions have been previously loaded. */
buf = pack_readfile(DATA, filename, &bufsize); buf = pack_readfile(DATA, filename, &bufsize);
if(luaL_dobuffer(L, buf, bufsize, filename) != 0) { 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. */ /* Get the distance from the pointer. */
static int ai_getdistance(lua_State* L) { static int ai_getdistance(lua_State* L) {
Vec2* vect; Vec2* v;
LuaVector* lv;
Pilot* pilot; Pilot* pilot;
unsigned int n; unsigned int n;
LLUA_MIN_ARGS(1); LLUA_MIN_ARGS(1);
/* Vector as a parameter. */ /* Vector as a parameter. */
if(lua_islightuserdata(L, 1)) if(lua_isvector(L, 1)) {
vect = (Vec2*)lua_topointer(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. */ /* Pilot id as parameter. */
else if(lua_isnumber(L, 1)) { 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); LLUA_DEBUG("Pilot '%d' not found in stack", n);
return 0; return 0;
} }
vect = &pilot->solid->pos; v = &pilot->solid->pos;
} else } else
/* Wrong parameter. */ /* Wrong parameter. */
LLUA_INVALID_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; return 1;
} }
@ -825,23 +834,26 @@ 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) {
LLUA_MIN_ARGS(1); 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; Pilot* p;
double mod, diff; double mod, diff;
int n; int n;
/* Get first parameter, aka what to face. */ /* Get first parameter, aka what to face. */
n = -2; n = -2;
if(lua_isnumber(L, 1)) if(lua_isnumber(L, 1)) {
n = (int)lua_tonumber(L, 1); n = (int)lua_tonumber(L, 1);
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. */
vect_cset(&tv, VX(p->solid->pos), VY(p->solid->pos)); vect_cset(&tv, VX(p->solid->pos), VY(p->solid->pos));
v = NULL; 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; 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)); vect_cset(&sv, VX(cur_pilot->solid->pos), VY(cur_pilot->solid->pos));
if(v == NULL) if(lv == NULL)
/* Target is dynamic. */ /* Target is dynamic. */
diff = angle_diff(cur_pilot->solid->dir, diff = angle_diff(cur_pilot->solid->dir,
(n==-1) ? VANGLE(sv) : (n==-1) ? VANGLE(sv) :
@ -862,7 +874,7 @@ static int ai_face(lua_State* L) {
/* Target is static. */ /* 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, &lv->vec));
/* Make pilot turn. */ /* Make pilot turn. */
pilot_turn = mod*diff; pilot_turn = mod*diff;
@ -934,7 +946,7 @@ static int ai_getrndplanet(lua_State* L) {
static int ai_getlandplanet(lua_State* L) { static int ai_getlandplanet(lua_State* L) {
Planet** planets; Planet** planets;
int nplanets, i; int nplanets, i;
Vec2 v; LuaVector lv;
planets = malloc(sizeof(Planet*) * cur_system->nplanets); planets = malloc(sizeof(Planet*) * cur_system->nplanets);
if(cur_system->nplanets == 0) return 0; /* No planets. */ 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. */ /* We can actually get a random planet now. */
i = RNG(0,nplanets-1); i = RNG(0,nplanets-1);
vectcpy(&v, &planets[i]->pos); vectcpy(&lv.vec, &planets[i]->pos);
vect_cadd(&v, RNG(0, planets[i]->gfx_space->sw)-planets[i]->gfx_space->sw/2., 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.); RNG(0, planets[i]->gfx_space->sh)-planets[i]->gfx_space->sh/2.);
lua_pushlightuserdata(L, &v); lua_pushvector(L, lv);
free(planets); free(planets);
return 1; return 1;
} }