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);
120 if ((la->id < 0) || (la->id >= field->
nb))
121 NLUA_ERROR(L,_(
"Asteroid '%d' in field '%d' is out of range!"),la->id,la->parent);
126 luaL_typerror(L, ind, ASTEROID_METATABLE);
131 NLUA_ERROR(L, _(
"Asteroid is invalid"));
146 luaL_getmetatable(L, ASTEROID_METATABLE);
147 lua_setmetatable(L, -2);
161 if (lua_getmetatable(L,ind)==0)
163 lua_getfield(L, LUA_REGISTRYINDEX, ASTEROID_METATABLE);
166 if (lua_rawequal(L, -1, -2))
192 lua_pushboolean( L, (memcmp( a1, a2,
sizeof(
LuaAsteroid_t) )==0) );
208 for (
int j=0; j<ast->
nb; j++) {
216 lua_rawseti( L, -2, n++ );
234 const vec2 *pos = NULL;
246 else if (lua_isnoneornil(L,1)) {
248 field = (max_field < 0 ? max_field : RNG(0, max_field));
250 else if (lua_isnumber(L,1)) {
251 field = luaL_checkinteger(L,1)-1;
253 NLUA_INVALID_PARAMETER(L);
256 NLUA_INVALID_PARAMETER(L);
259 if ((pos==NULL) && (field >= 0)) {
261 int ast = RNG(0,
cur_system->asteroids[field].nb-1);
262 int bad_asteroid = 0;
265 if (a->state != ASTEROID_FG) {
268 for (
int i=0; i<
cur_system->asteroids[field].nb; i++) {
269 ast = (ast+1) %
cur_system->asteroids[field].nb;
270 a = &
cur_system->asteroids[field].asteroids[ast];
271 if (a->state == ASTEROID_FG) {
289 double dist2 = HUGE_VAL;
292 for (
int j=0; j<ast->
nb; j++) {
296 if (a->state != ASTEROID_FG)
299 d2 = vec2_dist2( pos, &a->pos );
311 la.parent = a_closest->
parent;
312 la.id = a_closest->
id;
327 lua_pushboolean(L, 0);
333 lua_pushboolean(L, 0);
338 if ((la->id < 0) || (la->id >= field->
nb)) {
339 lua_pushboolean(L, 0);
344 lua_pushboolean(L, (a->state==ASTEROID_FG));
358 const char *state = NULL;
359 switch (ast->
state) {
369 case ASTEROID_XX_TO_BG:
372 case ASTEROID_FG_TO_BG:
375 case ASTEROID_BG_TO_FG:
378 case ASTEROID_BG_TO_XX:
385 lua_pushstring( L, state );
399 const char *state = luaL_checkstring(L,2);
400 if (strcmp(state,
"FG")==0)
401 ast->
state = ASTEROID_FG;
402 else if (strcmp(state,
"XB")==0)
403 ast->
state = ASTEROID_XB;
404 else if (strcmp(state,
"BX")==0)
405 ast->
state = ASTEROID_BX;
406 else if (strcmp(state,
"XX_TO_BG")==0)
407 ast->
state = ASTEROID_XX_TO_BG;
408 else if (strcmp(state,
"FG_TO_BG")==0)
409 ast->
state = ASTEROID_FG_TO_BG;
410 else if (strcmp(state,
"BG_TO_FG")==0)
411 ast->
state = ASTEROID_BG_TO_FG;
412 else if (strcmp(state,
"BG_TO_XX")==0)
413 ast->
state = ASTEROID_BG_TO_XX;
414 else if (strcmp(state,
"XX")==0)
415 ast->
state = ASTEROID_XX;
417 NLUA_INVALID_PARAMETER(L);
431 lua_pushinteger(L,la->parent+1);
503 lua_pushboolean(L,a->scanned);
518 lua_pushnumber(L,a->timer);
519 lua_pushnumber(L,a->timer_max);
533 a->timer = luaL_checknumber(L,2);
534 a->timer_max =
MAX( a->timer_max, a->timer );
548 lua_pushnumber(L,a->armour);
562 a->armour = luaL_checknumber(L,2);
578 lua_pushnumber(L,a->type->alert_range);
600 lua_setfield(L,-2,
"commodity");
603 lua_setfield(L,-2,
"quantity");
605 lua_pushnumber( L, mat->
rarity );
606 lua_setfield(L,-2,
"rarity");
608 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.