[Add] Added Planet:pos() and Vec2:dist() plus much more.

This commit is contained in:
Allanis 2013-11-17 02:55:03 +00:00
parent ef19939647
commit 5d17f0e31d
4 changed files with 205 additions and 30 deletions

View File

@ -21,9 +21,10 @@ static int pilot_addFleet(lua_State* L);
static int pilot_clear(lua_State* L);
static int pilot_toggleSpawn(lua_State* L);
static const luaL_reg pilot_methods[] = {
{ "add", pilot_addFleet },
{ "clear", pilot_clear },
{ "clear", pilot_toggleSpawn },
{ "add", pilot_addFleet },
{ "clear", pilot_clear },
{ "toggleSpawn", pilot_toggleSpawn },
{ "clear", pilot_toggleSpawn },
{ 0, 0 }
}; /**< Pilot lua methods. */
@ -165,11 +166,12 @@ int lua_ispilot(lua_State* L, int ind) {
* @fn static int pilot_addFleet(lua_State* L)
* @ingroup PILOT
*
* @brief table add(string fleetname [, string ai ])
* @brief table add(string fleetname [, string ai, Vec2 pos ])
*
* Adds a fleet to the system.
* @param fleetname Name of the fleet to add.
* @param ai If set will override the standard fleet AI.
* @param ai If set will override the standard fleet AI. "def" means use default.
* @param pos Position to create pilots around instead of choosing randomly.
* @return Table populated with all the identifiers of the pilots created.
*/
static int pilot_addFleet(lua_State* L) {
@ -182,6 +184,7 @@ static int pilot_addFleet(lua_State* L) {
Vec2 vv, vp, vn;
FleetPilot* plt;
LuaPilot lp;
LuaVector* lv;
/* Parse first argument - Fleet name */
@ -189,10 +192,26 @@ static int pilot_addFleet(lua_State* L) {
else LLUA_INVALID_PARAMETER();
/* Parse second argument - Fleet IA override. */
if((lua_gettop(L) > 1) && lua_isstring(L, 2))
fltai = (char*)lua_tostring(L, 2);
if(lua_gettop(L) > 1) {
if(lua_isstring(L, 2)) {
fltai = (char*)lua_tostring(L, 2);
if(strcmp(fltai, "def")==0) /* Check if set to default. */
fltai = NULL;
}
else LLUA_INVALID_PARAMETER();
}
else fltai = NULL;
if(lua_gettop(L) > 2) {
if(lua_isvector(L,2))
lv = lua_tovector(L,2);
else LLUA_INVALID_PARAMETER();
}
else lv = NULL;
/* Needed to determine angle. */
vectnull(&vn);
/* Pull the fleet. */
flt = fleet_get(fltname);
if(flt == NULL) {
@ -200,10 +219,12 @@ static int pilot_addFleet(lua_State* L) {
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);
/* Use position passed if possible. */
if(lv != NULL)
vectcpy(&vp, &lv->vec);
else
vect_pset(&vp, RNG(MIN_HYPERSPACE_DIST*2, MIN_HYPERSPACE_DIST*3),
RNG(0,360)*M_PI/180.);
j = 0;
lua_newtable(L);
@ -215,8 +236,15 @@ static int pilot_addFleet(lua_State* L) {
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);
/* Set velocity only if no position is set.. */
if(lv != NULL)
vectnull(&vv);
else { /* Enterting via hyperspace. */
a = vect_angle(&vp, &vn);
vect_pset(&vv, plt->ship->speed * 3., a);
}
/* Create the pilot. */
p = pilot_create(plt->ship,
plt->name,
flt->faction,

View File

@ -27,12 +27,14 @@ static int planetL_name(lua_State* L);
static int planetL_faction(lua_State* L);
static int planetL_class(lua_State* L);
static int planetL_services(lua_State* L);
static int planetL_position(lua_State* L);
static const luaL_reg planet_methods[] = {
{ "__eq", planetL_eq },
{ "name", planetL_name },
{ "faction", planetL_faction },
{ "class", planetL_class },
{ "services", planetL_services },
{ "pos", planetL_position },
{ 0, 0 }
};
@ -50,15 +52,23 @@ static const luaL_reg system_methods[] = {
};
/* Vector metatable methods. */
static int vectorL_new(lua_State* L);
static int vectorL_add(lua_State* L);
static int vectorL_sub(lua_State* L);
static int vectorL_mul(lua_State* L);
static int vectorL_div(lua_State* L);
static int vectorL_get(lua_State* L);
static int vectorL_set(lua_State* L);
static int vectorL_distance(lua_State* L);
static const luaL_reg vector_methods[] = {
{ "__add", vectorL_add },
{ "__sub", vectorL_sub },
{ "__mul", vectorL_mul },
{ "__div", vectorL_div },
{ "new", vectorL_new },
{ "__add", vectorL_add },
{ "__sub", vectorL_sub },
{ "__mul", vectorL_mul },
{ "__div", vectorL_div },
{ "get", vectorL_get },
{ "set", vectorL_set },
{ "dist", vectorL_distance },
{ 0, 0 }
}; /**< Vector metatable methods. */
@ -304,6 +314,24 @@ static int planetL_services(lua_State* L) {
return 1;
}
/**
* @fn static int planetL_position(lua_State* L)
* @ingroup META_PLANET
*
* @brief Vec2 pos(nil)
*
* Get the position of the planet in the system.
* @return The position of the planet in the system.
*/
static int planetL_position(lua_State* L) {
LuaPlanet* p;
LuaVector v;
p = lua_toplanet(L, 1);
vectcpy(&v.vec, &p->p->pos);
lua_pushvector(L, v);
return 1;
}
/* -- SYSTEM -- */
/* Get system at index. */
@ -497,6 +525,29 @@ int lua_isvector(lua_State* L, int ind) {
return ret;
}
/**
* @fn static int vectorL_new(lua_State* L)
* @ingroup META_VECTOR
*
* @brief
*/
static int vectorL_new(lua_State* L) {
LuaVector v;
double x, y;
if((lua_gettop(L) > 1) && lua_isnumber(L,1) && lua_isnumber(L,2)) {
x = lua_tonumber(L,1);
y = lua_tonumber(L,2);
} else {
x = 0.;
y = 0.;
}
vect_cset(&v.vec, x, y);
lua_pushvector(L, v);
return 1;
}
/**
* @fn static int vectorL_add(lua_State* L)
* @ingroup META_VECTOR
@ -513,9 +564,7 @@ static int vectorL_add(lua_State* L) {
double x, y;
/* Get self. */
if(lua_isvector(L, 1))
v1 = lua_tovector(L, 1);
else LLUA_INVALID_PARAMETER();
v1 = lua_tovector(L, 1);
/* Get rest of parameters. */
v2 = NULL;
@ -547,9 +596,7 @@ static int vectorL_sub(lua_State* L) {
double x, y;
/* Get self. */
if(lua_isvector(L, 1))
v1 = lua_tovector(L, 1);
else LLUA_INVALID_PARAMETER();
v1 = lua_tovector(L,1);
/* Get rest of parameters. */
v2 = NULL;
@ -584,9 +631,7 @@ static int vectorL_mul(lua_State* L) {
double mod;
/* Get self. */
if(lua_isvector(L, 1))
v1 = lua_tovector(L, 1);
else LLUA_INVALID_PARAMETER();
v1 = lua_tovector(L,1);
/* Get rest of parameters. */
if(lua_isnumber(L, 2))
@ -613,9 +658,7 @@ static int vectorL_div(lua_State* L) {
double mod;
/* Get self. */
if(lua_isvector(L, 1))
v1 = lua_tovector(L, 1);
else LLUA_INVALID_PARAMETER();
v1 = lua_tovector(L,1);
/* Get rest of parameters. */
if(lua_isnumber(L, 2))
@ -627,3 +670,89 @@ static int vectorL_div(lua_State* L) {
return 0;
}
/**
* @fn static int vectorL_get(lua_State* L)
* @ingroup META_VECTOR
*
* @brief number, number get(nil)
*
* Get the catesian positions of the vector.
* @return X and Y position of the vector.
*/
static int vectorL_get(lua_State* L) {
LLUA_MIN_ARGS(1);
LuaVector* v1;
/* Get self. */
v1 = lua_tovector(L,1);
/* Push the vector. */
lua_pushnumber(L, v1->vec.x);
lua_pushnumber(L, v1->vec.y);
return 2;
}
/**
* @fn static int vectorL_set(lua_State* L)
*
* @brief set(number x, number y)
*
* Set the vector by cartesian coordinates.
* @param x X coordinate to set.
* @param y Y coordinate to set.
*/
static int vectorL_set(lua_State* L) {
LLUA_MIN_ARGS(3);
LuaVector* v1;
double x, y;
/* Get self */
v1 = lua_tovector(L, 1);
/* Get parameters. */
if(lua_isnumber(L,2))
x = lua_tonumber(L,2);
else LLUA_INVALID_PARAMETER();
if(lua_isnumber(L, 3))
y = lua_tonumber(L, 3);
else LLUA_INVALID_PARAMETER();
vect_cset(&v1->vec, x, y);
return 0;
}
/**
* @fn static int vectorL_distance(lua_State* L)
* @ingroup META_VECTOR
*
* @brief number dist( [Vec2 vector] )
*
* Get the distance from the Vec2.
* @param vector Vector to get distance from, uses origin(0,0) if not set.
* @return The distance calculated.
*/
static int vectorL_distance(lua_State* L) {
LLUA_MIN_ARGS(1);
LuaVector* v1, *v2;
double dist;
/* Get self. */
v1 = lua_tovector(L, 1);
/* Get rest of parameters. */
v2 = NULL;
if((lua_gettop(L) > 1) && lua_isvector(L,2))
v2 = lua_tovector(L,2);
else LLUA_INVALID_PARAMETER();
/* Get distance. */
if(v2 == NULL)
dist = vect_odist(&v1->vec);
else
dist = vect_dist(&v1->vec, &v2->vec);
/* Return the distance. */
lua_pushnumber(L, dist);
return 1;
}

View File

@ -109,6 +109,7 @@ static int player_modFaction(lua_State* L);
static int player_modFactionRaw(lua_State* L);
static int player_getFaction(lua_State* L);
static int player_getRating(lua_State* L);
static int player_getPosition(lua_State* L);
static const luaL_reg player_methods[] = {
{ "name", player_getname },
{ "ship", player_shipname },
@ -121,6 +122,7 @@ static const luaL_reg player_methods[] = {
{ "modFactionRaw", player_modFactionRaw },
{ "getFaction", player_getFaction },
{ "getRating", player_getRating },
{ "pos", player_getPosition },
{ 0, 0 }
};
@ -751,6 +753,22 @@ static int hook_enter(lua_State* L) {
return 0;
}
/**
* @fn static int player_getPosition(lua_State* L)
*
* @brief Vec2 getPos(nil)
*
* Get the players position.
* @return The position of the player.
*/
static int player_getPosition(lua_State* L) {
LuaVector v;
vectcpy(&v.vec, &player->solid->pos);
lua_pushvector(L, v);
return 1;
}
/**
* @fn static int hook_pilot(lua_State* L)
* @ingroup HOOK

View File

@ -413,7 +413,7 @@ static void space_addFleet(Fleet* fleet, int init) {
Vec2 vv, vp, vn;
/* Simulate them coming from hyperspace. */
vect_pset(&vp, RNG(MIN_HYPERSPACE_DIST, MIN_HYPERSPACE_DIST*3),
vect_pset(&vp, RNG(MIN_HYPERSPACE_DIST*2, MIN_HYPERSPACE_DIST*3),
RNG(0, 360)*M_PI/180.);
/* Needed to determine angle. */