[Add] Missions can now add fleets.

This commit is contained in:
Allanis 2013-05-06 17:14:32 +01:00
parent c769e71c72
commit 1c9bc7ed9d
2 changed files with 76 additions and 11 deletions

View File

@ -157,6 +157,13 @@ static const luaL_reg hook_methods[] = {
{ 0, 0 }
};
// Pilots.
static int pilot_addFleet(lua_State* L);
static const luaL_reg pilot_methods[] = {
{ "add", pilot_addFleet },
{ 0, 0 }
};
// Register all the libaries.
int misn_loadLibs(lua_State* L) {
luaL_register(L, "lephisto", lephisto_methods);
@ -168,6 +175,7 @@ int misn_loadLibs(lua_State* L) {
luaL_register(L, "rnd", rnd_methods);
luaL_register(L, "tk", tk_methods);
luaL_register(L, "hook", hook_methods);
luaL_register(L, "pilot", pilot_methods);
return 0;
}
@ -771,3 +779,59 @@ static int hook_pilotDeath(lua_State* L) {
return 0;
}
// -- Pilot. --
static int pilot_addFleet(lua_State* L) {
MIN_ARGS(1);
Fleet* flt;
char* fltname;
int i, j;
unsigned int p;
double a;
Vec2 vv, vp, vn;
if(lua_isstring(L, -1)) fltname = (char*) lua_tostring(L, -1);
else {
MISN_DEBUG("Invalid parameter");
return 0;
}
// Pull the fleet.
flt = fleet_get(fltname);
if(flt == NULL) {
MISN_DEBUG("Fleet not found!");
return 0;
}
// This should probably be done better..
vect_pset(&vp, RNG(MIN_HYPERSPACE_DIST, MIN_HYPERSPACE_DIST*1.5),
RNG(0, 360)*M_PI/180.);
vectnull(&vn);
// Now we start adding pilots and toss ids into the table we return.
j = 0;
for(i = 0; i < flt->npilots; i++) {
if(RNG(0, 100) <= flt->pilots[i].chance) {
// Fleet displacement.
vect_cadd(&vp, RNG(75, 150) & (RNG(0, 1) ? 1: -1),
RNG(75, 150) * (RNG(0, 1) ? 1 : -1));
a = vect_angle(&vp, &vn);
vectnull(&vv);
p = pilot_create(flt->pilots[i].ship,
flt->pilots[i].name,
flt->faction,
flt->ai,
a,
&vp,
&vv,
0);
// We push each pilot created into a table and return it.
lua_pushnumber(L, ++j); // Index start with 1.
lua_pushnumber(L, p); // value = pilot id.
lua_rawset(L, -3); // Store the value in the table.
}
}
return 1;
}

View File

@ -125,6 +125,17 @@ Pilot* pilot_get(const unsigned int id) {
return NULL;
}
// Grab a fleet out of the stack.
Fleet* fleet_get(const char* name) {
int i;
for(i = 0; i < nfleets; i++)
if(strcmp(fleet_stack[i].name, name)==0)
return &fleet_stack[i];
WARN("Fleet '%s' not found in stack", name);
return NULL;
}
// Attempt to turn the pilot to face dir.
double pilot_face(Pilot* p, const float dir) {
double diff, turn;
@ -957,16 +968,6 @@ void pilots_render(void) {
pilot_stack[i]->render(pilot_stack[i]);
}
// Return the fleet based on 'name'
Fleet* fleet_get(const char* name) {
int i;
for(i = 0; i < nfleets; i++)
if(strcmp(name, fleet_stack[i].name)==0)
return fleet_stack+i;
return NULL;
}
// Parse the fleet node.
static Fleet* fleet_parse(const xmlNodePtr parent) {
xmlNodePtr cur, node;