From 843067a0f38210f719c0c6735b091dbf0791038d Mon Sep 17 00:00:00 2001 From: Allanis Date: Tue, 31 Dec 2013 04:01:11 +0000 Subject: [PATCH] [Add] Escorts and fighter bays are starting to work quite nicely. --- scripts/ai/include/attack.lua | 7 +++++++ scripts/ai/tpl/escort.lua | 17 +++++++++++++---- src/escort.c | 7 ++++++- src/faction.c | 8 ++++++-- src/pilot.c | 4 +++- src/weapon.c | 9 +++++++-- 6 files changed, 42 insertions(+), 10 deletions(-) diff --git a/scripts/ai/include/attack.lua b/scripts/ai/include/attack.lua index a8b9b1f..07db9aa 100644 --- a/scripts/ai/include/attack.lua +++ b/scripts/ai/include/attack.lua @@ -5,10 +5,17 @@ include("../scripts/ai/include/attack_generic.lua") include("../scripts/ai/include/attack_bomber.lua") +atk_think = true + --[[ -- Wrapper for the think functions. --]] function attack_think() + -- Not everyone wants to think.. + if atk_think == false then + return + end + if mem.atk_think ~= nil then mem.atk_think() else diff --git a/scripts/ai/tpl/escort.lua b/scripts/ai/tpl/escort.lua index da1815d..92a0ff8 100644 --- a/scripts/ai/tpl/escort.lua +++ b/scripts/ai/tpl/escort.lua @@ -1,6 +1,11 @@ include("../scripts/ai/tpl/generic.lua") -- Simple create function. -function create() +-- Shouldn't think, should only obey orders. +atk_think = false + +function create(master) + mem.escort = master + mem.command = true -- On by default. attack_choose() end @@ -22,12 +27,16 @@ function escort() dist = ai.dist(ai.pos(target)) bdist = ai.mindbrakedist() + -- Close enough. + if ai.isstopped() and dist < 300 then + return + -- Brake. - if not ai.isstopped() and dist < bdist then + elseif dist < bdist then ai.pushtask(0, "brake") -- Must approach. - elseif dir < 10 and dist > 200 then + elseif dir < 10 and dist > 300 then ai.accel() end end @@ -96,7 +105,7 @@ end -- Clear orders. function e_clear() if mem.command then - while ai.taskname ~= "none" do + while ai.taskname() ~= "none" do ai.poptask() end end diff --git a/src/escort.c b/src/escort.c index f35e230..93a9b64 100644 --- a/src/escort.c +++ b/src/escort.c @@ -42,6 +42,7 @@ int escort_create(unsigned int parent, char* ship, Pilot* p, *pe; char buf[16]; unsigned int e, f; + double dir; /* Get important stuff. */ p = pilot_get(parent); @@ -52,8 +53,12 @@ int escort_create(unsigned int parent, char* ship, f = PILOT_ESCORT; if(carried) f |= PILOT_CARRIED; + /* Get the direction. */ + if(carried) dir = p->solid->dir; + else dir = 0.; + /* Create the pilot. */ - e = pilot_create(s, NULL, p->faction, buf, 0., pos, vel, f); + e = pilot_create(s, NULL, p->faction, buf, dir, pos, vel, f); pe = pilot_get(e); pe->parent = parent; diff --git a/src/faction.c b/src/faction.c index 9c3e8f6..be325c9 100644 --- a/src/faction.c +++ b/src/faction.c @@ -91,6 +91,9 @@ char* faction_name(int f) { WARN("Faction id '%d' is invalid.", f); return NULL; } + /* Don't want player to see her escorts as "Player" faction. */ + if(f == FACTION_PLAYER) + return "Escort"; return faction_stack[f].name; } @@ -347,6 +350,9 @@ int areAllies(int a, int b) { Faction* fa, *fb; int i; + /* If they are the same, they must be allies. */ + if(a == b) return 1; + /* We assume player becomes allies with high rating. */ if(a == FACTION_PLAYER) { if(faction_isFaction(b)) { @@ -387,8 +393,6 @@ int areAllies(int a, int b) { return 0; } - if(a == b) return 0; - if(fa && fb) { /* Both are factions. */ for(i = 0; i < fa->nallies; i++) diff --git a/src/pilot.c b/src/pilot.c index cd2a3b9..30af198 100644 --- a/src/pilot.c +++ b/src/pilot.c @@ -388,6 +388,7 @@ void pilot_hit(Pilot* p, const Solid* w, const unsigned int shooter, int mod; double damage_shield, damage_armour, knockback, dam_mod; + Pilot* pshooter; /* Calculate the damage. */ outfit_calcDamage(&damage_shield, &damage_armour, &knockback, dtype, damage); @@ -421,7 +422,8 @@ void pilot_hit(Pilot* p, const Solid* w, const unsigned int shooter, if(!pilot_isFlag(p, PILOT_DEAD)) { pilot_dead(p); /* Adjust the combat rating based on pilot mass and ditto faction. */ - if(shooter == PLAYER_ID) { + pshooter = pilot_get(shooter); + if(pshooter->faction == FACTION_PLAYER) { mod = (int)MAX(1, ceil(pow(p->ship->mass, 1./3.))-1.); player_crating += MAX(1, mod); faction_modPlayer(p->faction, -mod); diff --git a/src/weapon.c b/src/weapon.c index e792873..e1a89e9 100644 --- a/src/weapon.c +++ b/src/weapon.c @@ -593,10 +593,13 @@ static void weapon_update(Weapon* w, const double dt, WeaponLayer layer) { * @param pos Position of the hit. */ static void weapon_hit(Weapon* w, Pilot* p, WeaponLayer layer, Vec2* pos) { + Pilot* parent; + /* Someone should let the ai know it's been attacked. */ if(!pilot_isPlayer(p)) { if((player->target == p->id) || (RNGF() > 0.33)) { /* 33% chance. */ - if((w->parent == PLAYER_ID) && + parent = pilot_get(w->parent); + if((parent->faction == FACTION_PLAYER) && (!pilot_isFlag(p, PILOT_HOSTILE) || (RNGF() < 0.5))) { /* 50% chance. */ faction_modPlayer(p->faction, -1); /* Slowly lower faction. */ pilot_setFlag(p, PILOT_HOSTILE); @@ -631,11 +634,13 @@ static void weapon_hitBeam(Weapon* w, Pilot* p, WeaponLayer layer, Vec2 pos[2], const double dt) { (void)layer; + Pilot* parent; /* Inform the ai it has been attacked, useless if player. */ if(!pilot_isPlayer(p)) { if((player->target == p->id) || (RNGF() < 0.30*dt)) { /* 30% chance per second. */ - if((w->parent == PLAYER_ID) && + parent = pilot_get(w->parent); + if((parent->faction == FACTION_PLAYER) && (!pilot_isFlag(p, PILOT_HOSTILE) || (RNGF() < 0.5))) { /* 50% chance. */ faction_modPlayer(p->faction, -1); /* Slowly lower faction. */ pilot_setFlag(p, PILOT_HOSTILE);