[Add] More planet functions for ai: ai.getrndplanet() and ai.getlandplanet().

This commit is contained in:
Allanis 2013-07-10 19:30:58 +01:00
parent 09636a6c90
commit b8bafb4700
4 changed files with 37 additions and 15 deletions

View File

@ -12,7 +12,7 @@ function control()
ai.hostile(enemy) ai.hostile(enemy)
ai.pushtask(0, "attack", enemy) ai.pushtask(0, "attack", enemy)
elseif task == "none" then elseif task == "none" then
planet = ai.rndplanet() planet = ai.landplanet()
-- Planet needs to exist.. -- Planet needs to exist..
if planet == nil then if planet == nil then
ai.pushtask(0, "hyperspace") ai.pushtask(0, "hyperspace")

View File

@ -13,7 +13,7 @@ function control()
ai.hyperspace() ai.hyperspace()
end end
elseif task == "none" then elseif task == "none" then
planet = ai.rndplanet() planet = ai.landplanet()
-- Planet must exist. -- Planet must exist.
if planet == nil then if planet == nil then
ai.pushtask(0, "hyperspace") ai.pushtask(0, "hyperspace")

View File

@ -16,43 +16,46 @@ function control()
-- There is an enemy. -- There is an enemy.
if enemy ~= 0 then if enemy ~= 0 then
-- Make hostile to the enemy (mainly for player). -- 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) ai.pushtask(0, "runaway", enemy)
return
end end
end
-- No enemy. -- Nothing to do so check if we are too far from the planet.
else -- (If there is one).
-- Nothing to do so check if we are too far from the planet. planet = ai.rndplanet()
-- (If there is one).
planet = ai.rndplanet()
if planet ~= nil then if planet ~= nil then
if ai.dist(planet) > planet_dist then if ai.dist(planet) > planet_dist then
ai.pushtask(0, "approach", planet) ai.pushtask(0, "approach", planet)
end return
end end
end end
-- Go idle if no task. -- Go idle if no task.
if task == "none" then if task == "none" then
ai.pushtask(0, "idle") ai.pushtask(0, "idle")
return
end end
-- Check if we are near enough. -- Check if we are near enough.
elseif task == "approach" then elseif task == "approach" then
planet = ai.target() planet = ai.target()
if ai.dist(planet) < planet_dist then if ai.dist(planet) < planet_dist + ai.minbrakedist() then
ai.poptask() ai.poptask()
ai.pushtask(0, "idle") ai.pushtask(0, "idle")
return
end end
-- Check if we need to run more. -- Check if we need to run more.
elseif task == "runaway" then elseif task == "runaway" then
enemy = ai.targetid() enemy = ai.targetid()
if ai.dist(enemy) > enemy_dist then if ai.dist(enemy) > enemy_dist and ai.haslockon() == false then
ai.poptask() ai.poptask()
return
end end
end end
end end

View File

@ -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_brake(lua_State* L); /* Brake() */
static int ai_getnearestplanet(lua_State* L); /* pointer getnearestplanet() */ static int ai_getnearestplanet(lua_State* L); /* pointer getnearestplanet() */
static int ai_getrndplanet(lua_State* L); /* pointer getrndplanet() */ 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_hyperspace(lua_State* L); /* [number] hyperspace() */
static int ai_stop(lua_State* L); /* stop() */ static int ai_stop(lua_State* L); /* stop() */
/* Combat. */ /* Combat. */
@ -169,6 +170,7 @@ static const luaL_Reg ai_methods[] = {
{ "cargofree", ai_cargofree }, { "cargofree", ai_cargofree },
{ "nearestplanet", ai_getnearestplanet }, { "nearestplanet", ai_getnearestplanet },
{ "rndplanet", ai_getrndplanet }, { "rndplanet", ai_getrndplanet },
{ "landplanet", ai_getlandplanet },
/* Movement. */ /* Movement. */
{ "accel", ai_accel }, { "accel", ai_accel },
{ "turn", ai_turn }, { "turn", ai_turn },
@ -771,15 +773,32 @@ static int ai_getnearestplanet(lua_State* L) {
return 1; 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) { static int ai_getrndplanet(lua_State* L) {
Vec2 v;
int p;
if(cur_system->nplanets == 0) return 0; /* No planets. */ 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; Planet** planets;
int nplanets, i; int nplanets, i;
Vec2 v; Vec2 v;
planets = malloc(sizeof(Planet*) * cur_system->nplanets); 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++) for(nplanets = 0, i = 0; i < cur_system->nplanets; i++)
if(planet_hasService(&cur_system->planets[i], PLANET_SERVICE_BASIC) && if(planet_hasService(&cur_system->planets[i], PLANET_SERVICE_BASIC) &&
!areEnemies(cur_pilot->faction, cur_system->planets[i].faction)) !areEnemies(cur_pilot->faction, cur_system->planets[i].faction))