From 01b3f2f77bb48583edac37d4879980cb49a84ffb Mon Sep 17 00:00:00 2001 From: Allanis Date: Wed, 28 Aug 2013 15:08:22 +0100 Subject: [PATCH] [Change] AI now use more weapon types and uses them more wisely. [Add] Draktharr gets mace rockets. --- dat/ship.xml | 12 +++--- scripts/ai/include/basic.lua | 27 ++++++++++-- src/ai.c | 84 +++++++++++++++++++++++++++++------- src/outfit.c | 2 - src/pilot.c | 2 +- 5 files changed, 99 insertions(+), 28 deletions(-) diff --git a/dat/ship.xml b/dat/ship.xml index bc1364c..edf0f13 100644 --- a/dat/ship.xml +++ b/dat/ship.xml @@ -338,8 +338,8 @@ Laser Turret - Missile Launcher - Missile + Mace Launcher + Mace Rocket @@ -510,7 +510,9 @@ 5 - Laser Cannon + Laser Cannon + Mace Launcher + Mace Rocket @@ -613,8 +615,8 @@ Laser Cannon - Missile Launcher - Missile + Mace Launcher + Mace Rocket diff --git a/scripts/ai/include/basic.lua b/scripts/ai/include/basic.lua index bf603f9..4f8d7b0 100644 --- a/scripts/ai/include/basic.lua +++ b/scripts/ai/include/basic.lua @@ -28,10 +28,19 @@ function attack() if dist > range then dir = ai.face(target) -- Normal face the target. + secondary, special = ai.secondary("Launcher") + -- Shoot missiles if in range. - if ai.secondary() == "Launcher" and - dist < ai.getweaprange(1) and dir < 30 then -- More leniant with aiming. - ai.shoot(2) + if secondary == "Launcher" and + dist < ai.getweaprange(1) then + -- More lenient with aiming. + if special == "Smart" and dir < 30 then + ai.shoot(2) + + -- Non-smart miss more. + elseif dir < 10 then + ai.shoot(2) + end end if dir < 10 then @@ -40,8 +49,18 @@ function attack() -- Close enough to melee. else + secondary, special = ai.secondary("Weapon") dir = ai.aim(target) -- we aim instead of face. - if (dir < 10 or ai.hasturret()) then + + -- Fire non-smart secondary weapons. + if(secondary == "Launcher" and special ~= "Smart") or + secondary == "Weapon" then + if dir < 10 then -- Need good acuracy. + ai.shoot(2) + end + end + + if dir < 10 or ai.hasturrets() then ai.shoot() end end diff --git a/src/ai.c b/src/ai.c index dda9cef..314014b 100644 --- a/src/ai.c +++ b/src/ai.c @@ -939,31 +939,83 @@ static int ai_settarget(lua_State* L) { /* Set the secondary weapon. Biassed towards launchers.. */ static int ai_secondary(lua_State* L) { - PilotOutfit* po; + PilotOutfit* co, *po; int i; + char* type; + const char* otype; - if(cur_pilot->secondary) { - lua_pushstring(L, outfit_getTypeBroad(cur_pilot->secondary->outfit)); - return 1; + /* Search for a type. */ + type = NULL; + if(lua_isstring(L, 1)) + type = (char*) lua_tostring(L, 1); + + /* Pilot has secondary selected - use that. */ + if(cur_pilot->secondary != NULL) { + co = cur_pilot->secondary; + otype = outfit_getTypeBroad(co->outfit); + + /* If we aren't looking for a type or if it matches what we want. */ + if((type == NULL) || (strcmp(otype, type) == 0)) + po = co; } - po = NULL; - for(i = 0; i < cur_pilot->noutfits; i++) { - if((po == NULL) && (outfit_isWeapon(cur_pilot->outfits[i].outfit) || - outfit_isLauncher(cur_pilot->outfits[i].outfit))) - po = &cur_pilot->outfits[i]; - else if((po != NULL) && outfit_isWeapon(po->outfit) && - outfit_isLauncher(cur_pilot->outfits[i].outfit)) - po = &cur_pilot->outfits[i]; + /* Need to get new secondary. */ + if(po == NULL) { + /* Iterate over the list. */ + po = NULL; + for(i = 0; i < cur_pilot->noutfits; i++) { + co = &cur_pilot->outfits[i]; + + /* Not a secondary weapon. */ + if(!outfit_isProp(co->outfit, OUTFIT_PROP_WEAP_SECONDARY) || + outfit_isAmmo(co->outfit)) + continue; + + /* Searching for type. */ + if(type != NULL) { + otype = outfit_getTypeBroad(co->outfit); + if(strcmp(otype, type)==0) { + po = co; + break; + } + + /* We'll grab the first weapon in case we don't find what we want. */ + if((po == NULL) && (outfit_isWeapon(co->outfit) || + outfit_isLauncher(co->outfit))) + po = co; + } + + /* Just grabbing best weapon. */ + else { + /* Grab first weapon or launcher it finds. */ + if((po == NULL) && (outfit_isWeapon(co->outfit) || + outfit_isLauncher(co->outfit))) + po = co; + + /* Grab launcher over weapon by default. */ + else if((po != NULL) && outfit_isWeapon(po->outfit) && + outfit_isLauncher(co->outfit)) + po = co; + } + } } - if(po) { + + if(po != NULL) { cur_pilot->secondary = po; pilot_setAmmo(cur_pilot); - lua_pushstring(L, outfit_getTypeBroad(po->outfit)); + otype = outfit_getTypeBroad(po->outfit); + lua_pushstring(L, otype); + + /* Set special flags. */ + if((strcmp(otype, "Launcher")==0) && + (po->outfit->type != OUTFIT_TYPE_MISSILE_DUMB)) { + lua_pushstring(L, "Smart"); + return 2; + } return 1; } - lua_pushstring(L, "None"); - return 1; + /* Nothing found. */ + return 0; } static int ai_hasturrets(lua_State* L) { diff --git a/src/outfit.c b/src/outfit.c index cd5139c..3db25e4 100644 --- a/src/outfit.c +++ b/src/outfit.c @@ -18,8 +18,6 @@ #define OUTFIT_DATA "../dat/outfit.xml" #define OUTFIT_GFX "../gfx/outfit/" -extern SDL_mutex* sound_lock; /* Sound.c */ - /* The Stack. */ static Outfit* outfit_stack = NULL; static int outfit_nstack = 0; diff --git a/src/pilot.c b/src/pilot.c index 9e0201e..7a226f6 100644 --- a/src/pilot.c +++ b/src/pilot.c @@ -349,7 +349,7 @@ void pilot_setSecondary(Pilot* p, const char* secondary) { /* Find the secondary and set ammo appropriately. */ for(i = 0; i < p->noutfits; i++) { if(strcmp(secondary, p->outfits[i].outfit->name)==0) { - p->secondary = &p->outfits[i];; + p->secondary = &p->outfits[i]; pilot_setAmmo(p); return; }