From b8bafb47004dd00e4ddf55cd6903ef500157ea37 Mon Sep 17 00:00:00 2001 From: Allanis Date: Wed, 10 Jul 2013 19:30:58 +0100 Subject: [PATCH] [Add] More planet functions for ai: ai.getrndplanet() and ai.getlandplanet(). --- scripts/ai/empire.lua | 2 +- scripts/ai/merchant.lua | 2 +- scripts/ai/scout.lua | 27 +++++++++++++++------------ src/ai.c | 21 ++++++++++++++++++++- 4 files changed, 37 insertions(+), 15 deletions(-) diff --git a/scripts/ai/empire.lua b/scripts/ai/empire.lua index 9cddf38..a0a41bf 100644 --- a/scripts/ai/empire.lua +++ b/scripts/ai/empire.lua @@ -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") diff --git a/scripts/ai/merchant.lua b/scripts/ai/merchant.lua index 0a9e765..73fb294 100644 --- a/scripts/ai/merchant.lua +++ b/scripts/ai/merchant.lua @@ -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") diff --git a/scripts/ai/scout.lua b/scripts/ai/scout.lua index f9f013b..471081b 100644 --- a/scripts/ai/scout.lua +++ b/scripts/ai/scout.lua @@ -16,43 +16,46 @@ 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() + -- Nothing to do so check if we are too far from the planet. + -- (If there is one). + planet = ai.rndplanet() - if planet ~= nil then - if ai.dist(planet) > planet_dist then - ai.pushtask(0, "approach", planet) - end + if planet ~= nil then + if ai.dist(planet) > planet_dist then + ai.pushtask(0, "approach", planet) + 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 diff --git a/src/ai.c b/src/ai.c index 16d4e42..25492b6 100644 --- a/src/ai.c +++ b/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))