[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>
</GFX>
<pos>
<y>-33</y>
<x>-57</x>
<y>-53</y>
<x>-77</x>
</pos>
<general>
<bar>The Darkshed cantina is decorated with all sorts of odd artifacts that marked different tendencies in spaceship outfitting.</bar>

View File

@ -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.

View File

@ -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

View File

@ -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

View File

@ -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;
}