[Add] More planet functions for ai: ai.getrndplanet() and ai.getlandplanet().
This commit is contained in:
parent
09636a6c90
commit
b8bafb4700
@ -12,7 +12,7 @@ function control()
|
||||
ai.hostile(enemy)
|
||||
ai.pushtask(0, "attack", enemy)
|
||||
elseif task == "none" then
|
||||
planet = ai.rndplanet()
|
||||
planet = ai.landplanet()
|
||||
-- Planet needs to exist..
|
||||
if planet == nil then
|
||||
ai.pushtask(0, "hyperspace")
|
||||
|
@ -13,7 +13,7 @@ function control()
|
||||
ai.hyperspace()
|
||||
end
|
||||
elseif task == "none" then
|
||||
planet = ai.rndplanet()
|
||||
planet = ai.landplanet()
|
||||
-- Planet must exist.
|
||||
if planet == nil then
|
||||
ai.pushtask(0, "hyperspace")
|
||||
|
@ -16,12 +16,12 @@ function control()
|
||||
-- There is an enemy.
|
||||
if enemy ~= 0 then
|
||||
-- Make hostile to the enemy (mainly for player).
|
||||
if ai.dist(enemy) < enemy_dist then
|
||||
if ai.dist(enemy) < enemy_dist or ai.haslockon() then
|
||||
ai.pushtask(0, "runaway", enemy)
|
||||
return
|
||||
end
|
||||
end
|
||||
|
||||
-- No enemy.
|
||||
else
|
||||
-- Nothing to do so check if we are too far from the planet.
|
||||
-- (If there is one).
|
||||
planet = ai.rndplanet()
|
||||
@ -29,30 +29,33 @@ function control()
|
||||
if planet ~= nil then
|
||||
if ai.dist(planet) > planet_dist then
|
||||
ai.pushtask(0, "approach", planet)
|
||||
end
|
||||
return
|
||||
end
|
||||
end
|
||||
|
||||
-- Go idle if no task.
|
||||
if task == "none" then
|
||||
ai.pushtask(0, "idle")
|
||||
return
|
||||
end
|
||||
|
||||
-- Check if we are near enough.
|
||||
elseif task == "approach" then
|
||||
planet = ai.target()
|
||||
|
||||
if ai.dist(planet) < planet_dist then
|
||||
if ai.dist(planet) < planet_dist + ai.minbrakedist() then
|
||||
ai.poptask()
|
||||
ai.pushtask(0, "idle")
|
||||
return
|
||||
end
|
||||
|
||||
-- Check if we need to run more.
|
||||
elseif task == "runaway" then
|
||||
enemy = ai.targetid()
|
||||
|
||||
if ai.dist(enemy) > enemy_dist then
|
||||
if ai.dist(enemy) > enemy_dist and ai.haslockon() == false then
|
||||
ai.poptask()
|
||||
return
|
||||
end
|
||||
end
|
||||
end
|
||||
|
21
src/ai.c
21
src/ai.c
@ -120,6 +120,7 @@ static int ai_face(lua_State* L); /* face(number/pointer) */
|
||||
static int ai_brake(lua_State* L); /* Brake() */
|
||||
static int ai_getnearestplanet(lua_State* L); /* pointer getnearestplanet() */
|
||||
static int ai_getrndplanet(lua_State* L); /* pointer getrndplanet() */
|
||||
static int ai_getlandplanet(lua_State* L); /* pointer getlandplanet() */
|
||||
static int ai_hyperspace(lua_State* L); /* [number] hyperspace() */
|
||||
static int ai_stop(lua_State* L); /* stop() */
|
||||
/* Combat. */
|
||||
@ -169,6 +170,7 @@ static const luaL_Reg ai_methods[] = {
|
||||
{ "cargofree", ai_cargofree },
|
||||
{ "nearestplanet", ai_getnearestplanet },
|
||||
{ "rndplanet", ai_getrndplanet },
|
||||
{ "landplanet", ai_getlandplanet },
|
||||
/* Movement. */
|
||||
{ "accel", ai_accel },
|
||||
{ "turn", ai_turn },
|
||||
@ -771,15 +773,32 @@ static int ai_getnearestplanet(lua_State* L) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* Return a random friendly planet's position to the pilot. */
|
||||
/* Return a random planet's position to the pilot. */
|
||||
static int ai_getrndplanet(lua_State* L) {
|
||||
Vec2 v;
|
||||
int p;
|
||||
|
||||
if(cur_system->nplanets == 0) return 0; /* No planets. */
|
||||
|
||||
/* Get a random planet. */
|
||||
p = RNG(0, cur_system->nplanets-1);
|
||||
|
||||
/* Copy the data into a vector. */
|
||||
vectcpy(&v, &cur_system->planets[p].pos);
|
||||
lua_pushlightuserdata(L, &v);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* Return a random friendly planet's position to the pilot. */
|
||||
static int ai_getlandplanet(lua_State* L) {
|
||||
Planet** planets;
|
||||
int nplanets, i;
|
||||
Vec2 v;
|
||||
planets = malloc(sizeof(Planet*) * cur_system->nplanets);
|
||||
|
||||
if(cur_system->nplanets == 0) return 0; /* No planets. */
|
||||
|
||||
for(nplanets = 0, i = 0; i < cur_system->nplanets; i++)
|
||||
if(planet_hasService(&cur_system->planets[i], PLANET_SERVICE_BASIC) &&
|
||||
!areEnemies(cur_pilot->faction, cur_system->planets[i].faction))
|
||||
|
Loading…
Reference in New Issue
Block a user