From 4b07e86acd149be5c5c32a268929ffb1d51e6698 Mon Sep 17 00:00:00 2001 From: Allanis <allanis@saracraft.net> Date: Tue, 12 Mar 2013 23:45:29 +0000 Subject: [PATCH] [Add] Militia faction. Changed Test pilot to Militia and added ai_create --- dat/faction.xml | 3 ++ dat/fleet.xml | 8 ++-- dat/ship.xml | 6 +-- dat/ssys.xml | 2 +- scripts/ai/API | 2 +- scripts/ai/militia.lua | 98 ++++++++++++++++++++++++++++++++++++++++++ scripts/ai/pirate.lua | 2 + scripts/ai/skeleton | 10 ----- src/ai.c | 9 ++++ src/pilot.c | 4 +- 10 files changed, 124 insertions(+), 20 deletions(-) create mode 100644 scripts/ai/militia.lua delete mode 100644 scripts/ai/skeleton diff --git a/dat/faction.xml b/dat/faction.xml index 7679d98..3391bc3 100644 --- a/dat/faction.xml +++ b/dat/faction.xml @@ -8,10 +8,13 @@ </faction> <faction name = "Pirate"> </faction> + <faction name = "Militia"> + </faction> <Alliances> <alliance name = "Neutral"> <ally>Independent</ally> <ally>Merchant</ally> + <ally>Militia</ally> </alliance> </Alliances> <Enemies> diff --git a/dat/fleet.xml b/dat/fleet.xml index fb18c41..082ed17 100644 --- a/dat/fleet.xml +++ b/dat/fleet.xml @@ -1,10 +1,10 @@ <?xml version="1.0" encoding="UTF-8"?> <Fleets> - <fleet name="Enemy Test"> - <ai>test</ai> - <faction>Independent</faction> + <fleet name="Militia Lancer"> + <ai>militia</ai> + <faction>Militia</faction> <pilots> - <pilot chance='100' name="Enemy Test">Lancer</pilot> + <pilot chance='100' name="Lancer">Lancer</pilot> </pilots> </fleet> <fleet name="Merchant Ship"> diff --git a/dat/ship.xml b/dat/ship.xml index ec23f3e..6c011eb 100644 --- a/dat/ship.xml +++ b/dat/ship.xml @@ -67,7 +67,7 @@ <GUI>minimal</GUI> <sound>engine</sound> <class>2</class> - <price>600000</price> + <price>700000</price> <fabricator>VLSoft</fabricator> <description>On of VLSoft's prize heavey fighters. Was originally a secret design for the Empire military, but then disclosed. Now a modified version is available for civilians, although it doesn't meet up to the original specifications. Used by security agencies all over the universe for it's reliability and availability. Proudly enforcing your security since STARDATE.</description> @@ -92,8 +92,8 @@ </characteristics> <outfits> <outfit quantity="4">Laser</outfit> - <outfit quantity='2'>Missile Launcher</outfit> - <outfit quantity='20'>Missile</outfit> + <outfit quantity="2">Missile Launcher</outfit> + <outfit quantity="20">Missile</outfit> </outfits> </ship> <ship name="Merchant Mule"> diff --git a/dat/ssys.xml b/dat/ssys.xml index e2b4239..f667698 100644 --- a/dat/ssys.xml +++ b/dat/ssys.xml @@ -14,7 +14,7 @@ <planet>KonoSphere</planet> </planets> <fleets> - <fleet chance="100">Enemy Test</fleet> + <fleet chance="100">Militia Lancer</fleet> <fleet chance="60">Pirate</fleet> <fleet chance="60">Merchant Ship</fleet> <fleet chance="50">Merchant Mule</fleet> diff --git a/scripts/ai/API b/scripts/ai/API index 23162f7..59ec2ea 100644 --- a/scripts/ai/API +++ b/scripts/ai/API @@ -155,7 +155,7 @@ shoot([number weapon]) -- return nil. getenemy() - -- return the id of the nearest enemy or -1 if none is found. + -- return the id of the nearest enemy or 0 if none is found. // ================ // TIMERS! diff --git a/scripts/ai/militia.lua b/scripts/ai/militia.lua new file mode 100644 index 0000000..72f87d4 --- /dev/null +++ b/scripts/ai/militia.lua @@ -0,0 +1,98 @@ +-- Required control rate +control_rate = 2 + +-- Required "control" function. +function control() + task = ai.taskname() + + enemy = ai.getenemy() + if enemy ~= 0 then + ai.pushtask(0, "attack", enemy) + elseif ai.taskname() == "none" then + ai.pushtask(0, "fly") + end +end + +-- Required "attacked" function +function attacked(attecker) + task = ai.taskname() + if task ~= "attack" and task "runaway" then + -- Some taunting. + taunt(attacker) + + -- Now pilot fights back! + ai.pushtask(0, "attack", attacker) + + elseif task == "attack" then + if ai.targetid() ~= attaker then + ai.pushtack(0, "attack", attacker) + end + end +end + +function create() + if ai.rnd(0, 2)==0 then + ai.broadcast("This area is under survellance. Do not attemt anything funny") + end +end + +-- Taunts +function taunt(target) + num = ai.rnd(0,4) + if num == 0 then msg = "How dare you attack me!?" + elseif num == 1 then msg = "YOU! ARE NOT! PREPARED!!" + elseif num == 2 then msg = "Won't You just die already!?" + elseif num == 3 then msg = "You won't survive!" + end + if msg then ai.comm(attacker, msg) end +end + +function runaway() + target = ai.targetid() + + -- Make sure pilot exists. + if not ai.exists(target) then + ai.poptask() + return + end + + dir = ai.face(target, 1) + ai.accel() +end + +function attack() + target = ai.targetid() + + if not ai.exists(target) then + ai.poptask() + return + end + + dir = ai.face(target) + dist = ai.dist(ai.pos(target)) + second = ai.secondary() + + if ai.secondary() == "Launcher" then + ai.settarget(target) + ai.shoot(2) + end + + if ai.parmour() < 70 then + ai.poptask() + ai.pushtask(0, "runaway", target) + elseif dir < 10 and dist > 300 then + ai.accel() + elseif dir < 10 and dist < 300 then + ai.shoot() + end +end + +function fly() + target = player + dir = ai.face(target) + dist = ai.dist(ai.pos(target)) + if dir < 10 and dist > 300 then + ai.accel() + end +end + diff --git a/scripts/ai/pirate.lua b/scripts/ai/pirate.lua index a13117d..b329a09 100644 --- a/scripts/ai/pirate.lua +++ b/scripts/ai/pirate.lua @@ -10,6 +10,8 @@ function control() if ai.parmour() == 100 then -- "attack" should be called after "runaway". ai.poptask() + elseif ai.dist(ai.pos(ai.targetid())) > 300 then + ai.hyperspace() end -- Nothing to do. diff --git a/scripts/ai/skeleton b/scripts/ai/skeleton deleted file mode 100644 index b0212e1..0000000 --- a/scripts/ai/skeleton +++ /dev/null @@ -1,10 +0,0 @@ ---Required control rate. -control_rate = 2 - --- Required "control" function. -function control() -end - --- Required "attacked" function -function attacked(attacker) -end diff --git a/src/ai.c b/src/ai.c index 81ddc21..251191c 100644 --- a/src/ai.c +++ b/src/ai.c @@ -92,6 +92,8 @@ static int ai_loadProfile(char* filename); static void ai_freetask(Task* t); // External C routines. void ai_attacked(Pilot* attacked, const unsigned int attacker); // weapon.c +void ai_create(Pilot* pilot); + // Ai routines for Lua. // Tasks. static int ai_pushtask(lua_State* L); // pushtask(string, number/pointer, number) @@ -342,6 +344,13 @@ void ai_attacked(Pilot* attacked, const unsigned int attacker) { lua_pcall(L, 1, 0, 0); } +// Pilot was just created. +void ai_create(Pilot* pilot) { + cur_pilot = pilot; + L = cur_pilot->ai->L; + AI_LCALL("create"); +} + // ===================== // INTERNAL C FUNCTIONS. // ===================== diff --git a/src/pilot.c b/src/pilot.c index 6b1e7b1..0809b85 100644 --- a/src/pilot.c +++ b/src/pilot.c @@ -34,6 +34,7 @@ static int nfleets = 0; // External. extern void ai_destroy(Pilot* p); // Ai. extern void ai_think(Pilot* pilot); // Ai.c +extern void ai_create(Pilot* pilot); // ai.c extern void player_think(Pilot* pilot); // Player.c extern void player_brokeHyperspace(void); // Player.c extern int gui_load(const char* name); // Player.c @@ -551,9 +552,10 @@ void pilot_init(Pilot* pilot, Ship* ship, char* name, Faction* faction, } else { pilot->think = ai_think; pilot->render = pilot_render; + ai_create(pilot); // Will run the create function in ai. } + // All update the same way. pilot->update = pilot_update; - } // Create a new pilot - Params are same as pilot_init. Return pilot's id.