[Add] Add Lua calls to pew pew lazers. Now you can have a fake battle with the AI. :D

This commit is contained in:
Allanis 2013-02-05 19:26:59 +00:00
parent 7a8dc43f96
commit ccdeb8d13b
6 changed files with 35 additions and 9 deletions

View File

@ -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>

View File

@ -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

View File

@ -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;
}

View File

@ -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);

View File

@ -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);

View File

@ -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);