15#include "nlua_asteroid.h"
18#include "nlua_commodity.h"
19#include "nlua_pilot.h"
103 luaL_typerror( L, ind, ASTEROID_METATABLE );
119 if ( ( la->parent < 0 ) ||
121 NLUA_ERROR( L, _(
"Asteroid field '%d' is out of range!" ),
129 NLUA_ERROR( L, _(
"Asteroid '%d' in field '%d' is out of range!" ),
130 la->id, la->parent );
136 luaL_typerror( L, ind, ASTEROID_METATABLE );
141 NLUA_ERROR( L, _(
"Asteroid is invalid" ) );
159 luaL_getmetatable( L, ASTEROID_METATABLE );
160 lua_setmetatable( L, -2 );
174 if ( lua_getmetatable( L, ind ) == 0 )
176 lua_getfield( L, LUA_REGISTRYINDEX, ASTEROID_METATABLE );
179 if ( lua_rawequal( L, -1, -2 ) )
204 lua_pushboolean( L, ( memcmp( a1, a2,
sizeof(
LuaAsteroid_t ) ) == 0 ) );
221 char buf[STRMAX_SHORT];
223 snprintf( buf,
sizeof( buf ),
"Asteroid(%s)", a->type->name );
224 lua_pushstring( L, buf );
248 lua_rawseti( L, -2, n++ );
269 const vec2 *pos = NULL;
281 else if ( lua_isnoneornil( L, 1 ) ) {
283 field = ( max_field < 0 ? max_field : RNG( 0, max_field ) );
284 }
else if ( lua_isnumber( L, 1 ) ) {
285 field = luaL_checkinteger( L, 1 ) - 1;
287 NLUA_INVALID_PARAMETER( L, 1 );
289 NLUA_INVALID_PARAMETER( L, 1 );
292 if ( ( pos == NULL ) && ( field >= 0 ) ) {
297 int ast = RNG( 0, nast - 1 );
298 int bad_asteroid = 0;
301 if ( a->state != ASTEROID_FG ) {
306 for (
int i = 0; i < n; i++ ) {
307 ast = ( ast + 1 ) % n;
308 a = &
cur_system->asteroids[field].asteroids[ast];
309 if ( a->state == ASTEROID_FG ) {
327 double dist2 = HUGE_VAL;
334 if ( a->state != ASTEROID_FG )
337 d2 = vec2_dist2( pos, &a->sol.pos );
345 if ( a_closest == NULL )
349 la.parent = a_closest->
parent;
350 la.id = a_closest->
id;
365 lua_pushboolean( L, 0 );
370 if ( ( la->parent < 0 ) ||
372 lua_pushboolean( L, 0 );
378 lua_pushboolean( L, 0 );
383 lua_pushboolean( L, ( a->state == ASTEROID_FG ) );
398 const char *state = NULL;
399 switch ( ast->
state ) {
409 case ASTEROID_XX_TO_BG:
412 case ASTEROID_FG_TO_BG:
415 case ASTEROID_BG_TO_FG:
418 case ASTEROID_BG_TO_XX:
425 lua_pushstring( L, state );
440 const char *state = luaL_checkstring( L, 2 );
441 if ( strcmp( state,
"FG" ) == 0 )
442 ast->
state = ASTEROID_FG;
443 else if ( strcmp( state,
"XB" ) == 0 )
444 ast->
state = ASTEROID_XB;
445 else if ( strcmp( state,
"BX" ) == 0 )
446 ast->
state = ASTEROID_BX;
447 else if ( strcmp( state,
"XX_TO_BG" ) == 0 )
448 ast->
state = ASTEROID_XX_TO_BG;
449 else if ( strcmp( state,
"FG_TO_BG" ) == 0 )
450 ast->
state = ASTEROID_FG_TO_BG;
451 else if ( strcmp( state,
"BG_TO_FG" ) == 0 )
452 ast->
state = ASTEROID_BG_TO_FG;
453 else if ( strcmp( state,
"BG_TO_XX" ) == 0 )
454 ast->
state = ASTEROID_BG_TO_XX;
455 else if ( strcmp( state,
"XX" ) == 0 )
456 ast->
state = ASTEROID_XX;
458 NLUA_INVALID_PARAMETER( L, 2 );
473 lua_pushinteger( L, la->parent + 1 );
545 lua_pushboolean( L, a->scanned );
560 lua_pushnumber( L, a->timer );
561 lua_pushnumber( L, a->timer_max );
575 a->timer = luaL_checknumber( L, 2 );
576 a->timer_max =
MAX( a->timer_max, a->timer );
590 lua_pushnumber( L, a->armour );
605 a->armour = luaL_checknumber( L, 2 );
606 if ( a->armour <= 0. )
621 lua_pushnumber( L, a->type->alert_range );
643 lua_setfield( L, -2,
"commodity" );
646 lua_setfield( L, -2,
"quantity" );
648 lua_pushnumber( L, mat->
rarity );
649 lua_setfield( L, -2,
"rarity" );
651 lua_rawseti( L, -2, i + 1 );
Provides macros to work with dynamic arrays.
static ALWAYS_INLINE int array_size(const void *array)
Returns number of elements in the array.
void asteroid_explode(Asteroid *a, int max_rarity, double mining_bonus)
Makes an asteroid explode.
Header file with generic functions and naev-specifics.
static const luaL_Reg asteroidL_methods[]
static int asteroidL_state(lua_State *L)
Gets the state of an asteroid.
static int asteroidL_vel(lua_State *L)
Gets the velocity of an asteroid.
static int asteroidL_setState(lua_State *L)
Sets the state of an asteroid.
static int asteroidL_field(lua_State *L)
Gets the field the asteroid belongs to (useful for getting more asteroids from the same field).
static int asteroidL_scanned(lua_State *L)
Gets whether or not an asteroid got scanned.
static int asteroidL_exists(lua_State *L)
Checks to see if an asteroid exists.
static int asteroidL_materials(lua_State *L)
Gets the materials the asteroid can potentially drop.
static int asteroidL_setVel(lua_State *L)
Sets the velocity of an asteroid.
static int asteroidL_tostring(lua_State *L)
Gets the asteroid's current (translated) name or notes it is inexistent.
int nlua_loadAsteroid(nlua_env env)
Loads the asteroid library.
static int asteroidL_get(lua_State *L)
Gets an asteroid in the system.
static int asteroidL_alertRange(lua_State *L)
Gets the alert range of an asteroid.
static int asteroidL_setTimer(lua_State *L)
Sets the time left on the asteroid.
static int asteroidL_getAll(lua_State *L)
Gets all the asteroids in the system.
int lua_isasteroid(lua_State *L, int ind)
Checks to see if ind is a asteroid.
static int asteroidL_eq(lua_State *L)
Lua bindings to interact with asteroid.
LuaAsteroid_t * luaL_checkasteroid(lua_State *L, int ind)
Gets asteroid at index or raises error if there is no asteroid at index.
static int asteroidL_pos(lua_State *L)
Gets the position of an asteroid.
static int asteroidL_setPos(lua_State *L)
Sets the position of an asteroid.
static int asteroidL_setArmour(lua_State *L)
Sets the armour of the asteroid.
LuaAsteroid_t * lua_pushasteroid(lua_State *L, LuaAsteroid_t asteroid)
Pushes a asteroid on the stack.
Asteroid * luaL_validasteroid(lua_State *L, int ind)
Gets asteroid at index raising an error if type doesn't match.
LuaAsteroid_t * lua_toasteroid(lua_State *L, int ind)
Gets asteroid at index.
static int asteroidL_armour(lua_State *L)
Gets the armour (health) left on the asteroid.
static int asteroidL_timer(lua_State *L)
Gets the time left on the asteroid.
Commodity ** lua_pushcommodity(lua_State *L, Commodity *commodity)
Pushes a commodity on the stack.
Pilot * luaL_validpilot(lua_State *L, int ind)
Makes sure the pilot is valid or raises a Lua error.
int lua_ispilot(lua_State *L, int ind)
Checks to see if ind is a pilot.
int lua_isvector(lua_State *L, int ind)
Checks to see if ind is a vector.
vec2 * luaL_checkvector(lua_State *L, int ind)
Gets vector at index making sure type is valid.
vec2 * lua_tovector(lua_State *L, int ind)
Represents a 2D vector in Lua.
vec2 * lua_pushvector(lua_State *L, vec2 vec)
Pushes a vector on the stack.
Represents an asteroid field anchor.
Represents a potential reward from the asteroid.
Represents a type of asteroid.
AsteroidReward * material
Represents a single asteroid.