15#include "nlua_asteroid.h"
21#include "nlua_commodity.h"
22#include "nlua_pilot.h"
99 luaL_typerror(L, ind, ASTEROID_METATABLE);
116 NLUA_ERROR(L,_(
"Asteroid field '%d' is out of range!"),la->parent);
122 if ((la->id < 0) || (la->id >= field->
nb)) {
123 NLUA_ERROR(L,_(
"Asteroid '%d' in field '%d' is out of range!"),la->id,la->parent);
130 luaL_typerror(L, ind, ASTEROID_METATABLE);
135 NLUA_ERROR(L, _(
"Asteroid is invalid"));
152 luaL_getmetatable(L, ASTEROID_METATABLE);
153 lua_setmetatable(L, -2);
167 if (lua_getmetatable(L,ind)==0)
169 lua_getfield(L, LUA_REGISTRYINDEX, ASTEROID_METATABLE);
172 if (lua_rawequal(L, -1, -2))
198 lua_pushboolean( L, (memcmp( a1, a2,
sizeof(
LuaAsteroid_t) )==0) );
214 for (
int j=0; j<ast->
nb; j++) {
222 lua_rawseti( L, -2, n++ );
240 const vec2 *pos = NULL;
252 else if (lua_isnoneornil(L,1)) {
254 field = (max_field < 0 ? max_field : RNG(0, max_field));
256 else if (lua_isnumber(L,1)) {
257 field = luaL_checkinteger(L,1)-1;
259 NLUA_INVALID_PARAMETER(L,1);
262 NLUA_INVALID_PARAMETER(L,1);
265 if ((pos==NULL) && (field >= 0)) {
267 int ast = RNG(0,
cur_system->asteroids[field].nb-1);
268 int bad_asteroid = 0;
271 if (a->state != ASTEROID_FG) {
274 for (
int i=0; i<
cur_system->asteroids[field].nb; i++) {
275 ast = (ast+1) %
cur_system->asteroids[field].nb;
276 a = &
cur_system->asteroids[field].asteroids[ast];
277 if (a->state == ASTEROID_FG) {
295 double dist2 = HUGE_VAL;
298 for (
int j=0; j<ast->
nb; j++) {
302 if (a->state != ASTEROID_FG)
305 d2 = vec2_dist2( pos, &a->sol.pos );
317 la.parent = a_closest->
parent;
318 la.id = a_closest->
id;
333 lua_pushboolean(L, 0);
339 lua_pushboolean(L, 0);
344 if ((la->id < 0) || (la->id >= field->
nb)) {
345 lua_pushboolean(L, 0);
350 lua_pushboolean(L, (a->state==ASTEROID_FG));
364 const char *state = NULL;
365 switch (ast->
state) {
375 case ASTEROID_XX_TO_BG:
378 case ASTEROID_FG_TO_BG:
381 case ASTEROID_BG_TO_FG:
384 case ASTEROID_BG_TO_XX:
391 lua_pushstring( L, state );
405 const char *state = luaL_checkstring(L,2);
406 if (strcmp(state,
"FG")==0)
407 ast->
state = ASTEROID_FG;
408 else if (strcmp(state,
"XB")==0)
409 ast->
state = ASTEROID_XB;
410 else if (strcmp(state,
"BX")==0)
411 ast->
state = ASTEROID_BX;
412 else if (strcmp(state,
"XX_TO_BG")==0)
413 ast->
state = ASTEROID_XX_TO_BG;
414 else if (strcmp(state,
"FG_TO_BG")==0)
415 ast->
state = ASTEROID_FG_TO_BG;
416 else if (strcmp(state,
"BG_TO_FG")==0)
417 ast->
state = ASTEROID_BG_TO_FG;
418 else if (strcmp(state,
"BG_TO_XX")==0)
419 ast->
state = ASTEROID_BG_TO_XX;
420 else if (strcmp(state,
"XX")==0)
421 ast->
state = ASTEROID_XX;
423 NLUA_INVALID_PARAMETER(L,2);
437 lua_pushinteger(L,la->parent+1);
509 lua_pushboolean(L,a->scanned);
524 lua_pushnumber(L,a->timer);
525 lua_pushnumber(L,a->timer_max);
539 a->timer = luaL_checknumber(L,2);
540 a->timer_max =
MAX( a->timer_max, a->timer );
554 lua_pushnumber(L,a->armour);
568 a->armour = luaL_checknumber(L,2);
584 lua_pushnumber(L,a->type->alert_range);
606 lua_setfield(L,-2,
"commodity");
609 lua_setfield(L,-2,
"quantity");
611 lua_pushnumber( L, mat->
rarity );
612 lua_setfield(L,-2,
"rarity");
614 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.
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.