[Add] Add Lua calls to pew pew lazers. Now you can have a fake battle with the AI. :D
This commit is contained in:
parent
7a8dc43f96
commit
ccdeb8d13b
@ -23,7 +23,7 @@
|
|||||||
<cap_cargo>20</cap_cargo>
|
<cap_cargo>20</cap_cargo>
|
||||||
</characteristics>
|
</characteristics>
|
||||||
<outfits>
|
<outfits>
|
||||||
<outfit quantity='2'>laser</outfit>
|
<outfit quantity='3'>laser</outfit>
|
||||||
</outfits>
|
</outfits>
|
||||||
</ship>
|
</ship>
|
||||||
<ship name="Test">
|
<ship name="Test">
|
||||||
@ -49,6 +49,7 @@
|
|||||||
<cap_cargo>40</cap_cargo>
|
<cap_cargo>40</cap_cargo>
|
||||||
</characteristics>
|
</characteristics>
|
||||||
<outfits>
|
<outfits>
|
||||||
|
<outfit quantity='1'>laser</outfit>
|
||||||
</outfits>
|
</outfits>
|
||||||
</ship>
|
</ship>
|
||||||
</Ships>
|
</Ships>
|
||||||
|
@ -7,11 +7,13 @@ function control()
|
|||||||
end
|
end
|
||||||
|
|
||||||
function follow()
|
function follow()
|
||||||
target =1
|
target = 0
|
||||||
dir = face(target)
|
dir = face(target)
|
||||||
dist = getdist(getpos(target))
|
dist = getdist(getpos(target))
|
||||||
if -dir < 10 and dist > 100 then
|
if dir < 10 and dist > 100 then
|
||||||
accel(dist/100-1)
|
accel(dist/100-1)
|
||||||
|
elseif dir < 10 and dist < 100 then
|
||||||
|
shoot()
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
21
src/ai.c
21
src/ai.c
@ -78,6 +78,8 @@ static int ai_face(lua_State* L); // face(number/pointer)
|
|||||||
static int ai_brake(lua_State* L); // Brake()
|
static int ai_brake(lua_State* L); // Brake()
|
||||||
// Misc.
|
// Misc.
|
||||||
static int ai_createvect(lua_State* L); // createvect(number, number)
|
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.
|
// Global Lua interpreter.
|
||||||
static lua_State* L = NULL;
|
static lua_State* L = NULL;
|
||||||
@ -86,6 +88,7 @@ static lua_State* L = NULL;
|
|||||||
static Pilot* cur_pilot = NULL;
|
static Pilot* cur_pilot = NULL;
|
||||||
static double pilot_acc = 0.;
|
static double pilot_acc = 0.;
|
||||||
static double pilot_turn = 0.;
|
static double pilot_turn = 0.;
|
||||||
|
static int pilot_primary = 0;
|
||||||
|
|
||||||
// Destroy the AI part of the pilot.
|
// Destroy the AI part of the pilot.
|
||||||
void ai_destroy(Pilot* p) {
|
void ai_destroy(Pilot* p) {
|
||||||
@ -123,6 +126,8 @@ int ai_init(void) {
|
|||||||
lua_register(L, "turn", ai_turn);
|
lua_register(L, "turn", ai_turn);
|
||||||
lua_register(L, "face", ai_face);
|
lua_register(L, "face", ai_face);
|
||||||
lua_register(L, "brake", ai_brake);
|
lua_register(L, "brake", ai_brake);
|
||||||
|
// Combat.
|
||||||
|
lua_register(L, "shoot", ai_shoot);
|
||||||
// Misc.
|
// Misc.
|
||||||
lua_register(L, "createvect", ai_createvect);
|
lua_register(L, "createvect", ai_createvect);
|
||||||
|
|
||||||
@ -164,6 +169,8 @@ void ai_think(Pilot* pilot) {
|
|||||||
if(pilot_turn) // Set the turning velocity.
|
if(pilot_turn) // Set the turning velocity.
|
||||||
cur_pilot->solid->dir_vel -= cur_pilot->ship->turn * pilot_turn;
|
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);
|
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) {
|
static int ai_getdistance(lua_State* L) {
|
||||||
MIN_ARGS(1);
|
MIN_ARGS(1);
|
||||||
Vec2* vect = (Vec2*)lua_topointer(L,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;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -356,3 +363,15 @@ static int ai_createvect(lua_State* L) {
|
|||||||
return 1;
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
@ -180,6 +180,8 @@ void solid_init(Solid* dest, const double mass, const double dir, const Vec2* po
|
|||||||
|
|
||||||
vect_cset(&dest->force, 0., 0.);
|
vect_cset(&dest->force, 0., 0.);
|
||||||
dest->dir = dir;
|
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);
|
if(vel == NULL) vectnull(&dest->vel);
|
||||||
else vectcpy(&dest->vel, vel);
|
else vectcpy(&dest->vel, vel);
|
||||||
|
@ -9,6 +9,8 @@
|
|||||||
#define MOD(x,y) (sqrt((x)*(x) + (y)*(y)))
|
#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 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
|
// Misc
|
||||||
double angle_diff(const double ref, double a);
|
double angle_diff(const double ref, double a);
|
||||||
|
|
||||||
|
@ -80,8 +80,8 @@ void pilot_render(Pilot* p) {
|
|||||||
|
|
||||||
// Update the pilot.
|
// Update the pilot.
|
||||||
static void pilot_update(Pilot* pilot, const double dt) {
|
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 > 2.*M_PI) || (pilot->solid->dir < 0.0))
|
||||||
if(pilot->solid->dir < 0.0) pilot->solid->dir += 2*M_PI;
|
pilot->solid->dir = fmod(pilot->solid->dir, 2.*M_PI);
|
||||||
|
|
||||||
// Update the solid.
|
// Update the solid.
|
||||||
pilot->solid->update(pilot->solid, dt);
|
pilot->solid->update(pilot->solid, dt);
|
||||||
|
Loading…
Reference in New Issue
Block a user