From ccdeb8d13ba26d5520124ac5f2735309c5bed3cf Mon Sep 17 00:00:00 2001 From: Allanis <allanis@saracraft.net> Date: Tue, 5 Feb 2013 19:26:59 +0000 Subject: [PATCH] [Add] Add Lua calls to pew pew lazers. Now you can have a fake battle with the AI. :D --- dat/ship.xml | 3 ++- scripts/ai/test.lua | 6 ++++-- src/ai.c | 27 +++++++++++++++++++++++---- src/physics.c | 2 ++ src/physics.h | 2 ++ src/pilot.c | 4 ++-- 6 files changed, 35 insertions(+), 9 deletions(-) diff --git a/dat/ship.xml b/dat/ship.xml index fb83e73..43748a4 100644 --- a/dat/ship.xml +++ b/dat/ship.xml @@ -23,7 +23,7 @@ <cap_cargo>20</cap_cargo> </characteristics> <outfits> - <outfit quantity='2'>laser</outfit> + <outfit quantity='3'>laser</outfit> </outfits> </ship> <ship name="Test"> @@ -49,6 +49,7 @@ <cap_cargo>40</cap_cargo> </characteristics> <outfits> + <outfit quantity='1'>laser</outfit> </outfits> </ship> </Ships> diff --git a/scripts/ai/test.lua b/scripts/ai/test.lua index 011b5b1..0f0acf1 100644 --- a/scripts/ai/test.lua +++ b/scripts/ai/test.lua @@ -7,11 +7,13 @@ function control() end function follow() - target =1 + target = 0 dir = face(target) dist = getdist(getpos(target)) - if -dir < 10 and dist > 100 then + if dir < 10 and dist > 100 then accel(dist/100-1) + elseif dir < 10 and dist < 100 then + shoot() end end diff --git a/src/ai.c b/src/ai.c index 075d98f..0f9e67d 100644 --- a/src/ai.c +++ b/src/ai.c @@ -78,14 +78,17 @@ static int ai_face(lua_State* L); // face(number/pointer) static int ai_brake(lua_State* L); // Brake() // Misc. static int ai_createvect(lua_State* L); // createvect(number, number) +// Combat. +static int ai_shoot(lua_State* L); // shoot(number) number = 1,2,3. // Global Lua interpreter. static lua_State* L = NULL; // Current pilot "thinking" and assorted variables. -static Pilot* cur_pilot = NULL; -static double pilot_acc = 0.; -static double pilot_turn = 0.; +static Pilot* cur_pilot = NULL; +static double pilot_acc = 0.; +static double pilot_turn = 0.; +static int pilot_primary = 0; // Destroy the AI part of the pilot. void ai_destroy(Pilot* p) { @@ -123,6 +126,8 @@ int ai_init(void) { lua_register(L, "turn", ai_turn); lua_register(L, "face", ai_face); lua_register(L, "brake", ai_brake); + // Combat. + lua_register(L, "shoot", ai_shoot); // Misc. lua_register(L, "createvect", ai_createvect); @@ -164,6 +169,8 @@ void ai_think(Pilot* pilot) { if(pilot_turn) // Set the turning velocity. cur_pilot->solid->dir_vel -= cur_pilot->ship->turn * pilot_turn; vect_pset(&cur_pilot->solid->force, cur_pilot->ship->thrust * pilot_acc, cur_pilot->solid->dir); + + if(pilot_primary) pilot_shoot(pilot, 0); // AMG, he's gunna shoot! } // ===================== @@ -249,7 +256,7 @@ static int ai_gettargetid(lua_State* L) { static int ai_getdistance(lua_State* L) { MIN_ARGS(1); Vec2* vect = (Vec2*)lua_topointer(L,1); - lua_pushnumber(L, MOD(vect->x-cur_pilot->solid->pos.x, vect->y-cur_pilot->solid->pos.y)); + lua_pushnumber(L, DIST(*vect, cur_pilot->solid->pos)); return 1; } @@ -356,3 +363,15 @@ static int ai_createvect(lua_State* L) { return 1; } +// Pew pew.. Says the pilot. +static int ai_shoot(lua_State* L) { + int n = 1; + if(lua_isnumber(L, 1)) n = (int)lua_tonumber(L,1); + + if(n == 1) pilot_primary = 1; + //else if(n == 2) pilot_secondary = 1; + //else if(n == 3) pilot_primary = pilot_secondary = 1; + + return 0; +} + diff --git a/src/physics.c b/src/physics.c index b112647..557fcc6 100644 --- a/src/physics.c +++ b/src/physics.c @@ -180,6 +180,8 @@ void solid_init(Solid* dest, const double mass, const double dir, const Vec2* po vect_cset(&dest->force, 0., 0.); dest->dir = dir; + if((dest->dir > 2.*M_PI) || (dest->dir < 0.)) + dest->dir = fmod(dest->dir, 2*M_PI); if(vel == NULL) vectnull(&dest->vel); else vectcpy(&dest->vel, vel); diff --git a/src/physics.h b/src/physics.h index 30b82fa..b95877d 100644 --- a/src/physics.h +++ b/src/physics.h @@ -9,6 +9,8 @@ #define MOD(x,y) (sqrt((x)*(x) + (y)*(y))) #define ANGLE(x,y)(((x)==0.) ? 0. : (((x)<0.)?atan((y)/(x))+M_PI:atan((y)/(x)))) +#define DIST(v,u) MOD((v).x-(u).x, (v).y-(u).y) + // Misc double angle_diff(const double ref, double a); diff --git a/src/pilot.c b/src/pilot.c index a2bfb7c..257257c 100644 --- a/src/pilot.c +++ b/src/pilot.c @@ -80,8 +80,8 @@ void pilot_render(Pilot* p) { // Update the pilot. static void pilot_update(Pilot* pilot, const double dt) { - if(pilot->solid->dir > 2*M_PI) pilot->solid->dir -= 2*M_PI; - if(pilot->solid->dir < 0.0) pilot->solid->dir += 2*M_PI; + if((pilot->solid->dir > 2.*M_PI) || (pilot->solid->dir < 0.0)) + pilot->solid->dir = fmod(pilot->solid->dir, 2.*M_PI); // Update the solid. pilot->solid->update(pilot->solid, dt);