11#include "physfsrwops.h"
22#include "nlua_audio.h"
30#define SPFX_GLOBAL (1<<1)
31#define SPFX_RELATIVE (1<<2)
32#define SPFX_MOVING (1<<3)
33#define SPFX_AUDIO (1<<4)
34#define SPFX_CLEANUP (1<<5)
41typedef struct LuaSpfxData_s {
62static int lua_spfx_idgen = 0;
63static int lua_spfx_lock = 0;
94static int spfx_cmp(
const void *p1,
const void *p2 )
99 return s1->
id - s2->
id;
123 return (LuaSpfx_t*) lua_touserdata(L,ind);
136 luaL_typerror(L, ind, SPFX_METATABLE);
139static LuaSpfxData_t* luaL_checkspfxdataNoWarn( lua_State *L,
int ind )
149static LuaSpfxData_t* luaL_checkspfxdata( lua_State *L,
int ind )
153 NLUA_ERROR( L, _(
"Spfx does not exist.") );
165 LuaSpfx_t *la = (LuaSpfx_t*) lua_newuserdata(L,
sizeof(LuaSpfx_t));
167 luaL_getmetatable(L, SPFX_METATABLE);
168 lua_setmetatable(L, -2);
182 if (lua_getmetatable(L,ind)==0)
184 lua_getfield(L, LUA_REGISTRYINDEX, SPFX_METATABLE);
187 if (lua_rawequal(L, -1, -2))
210 ls->
data = LUA_NOREF;
219 audio_cleanup( &ls->
sfx );
257 lua_pushboolean( L, (memcmp( s1, s2,
sizeof(LuaSpfx_t) )==0) );
278 lua_rawseti( L, -2, n++ );
309 ls.
id = ++lua_spfx_idgen;
310 ls.
ttl = luaL_checknumber(L,1);
318 if (!lua_isnoneornil(L,2))
320 if (!lua_isnoneornil(L,3))
322 if (!lua_isnoneornil(L,4))
324 if (!lua_isnoneornil(L,5))
328 if (!lua_isnoneornil(L,6)) {
329 if (lua_isboolean( L, 6 )) {
331 if (!lua_toboolean( L, 6 ))
339 if (!lua_isnoneornil(L,7)) {
345 if (!lua_isnoneornil(L,8)) {
350 audio_clone( &ls.
sfx, la );
356 alSourcei( ls.
sfx.
source, AL_LOOPING, AL_FALSE );
357 alSourcef( ls.
sfx.
source, AL_REFERENCE_DISTANCE, SOUND_REFERENCE_DISTANCE );
358 alSourcef( ls.
sfx.
source, AL_MAX_DISTANCE, SOUND_MAX_DISTANCE );
360 alSourcei( ls.
sfx.
source, AL_SOURCE_RELATIVE, AL_TRUE );
362 alSourcef( ls.
sfx.
source, AL_PITCH, 1. );
368 alSourcei( ls.
sfx.
source, AL_SOURCE_RELATIVE, AL_FALSE );
373 alSourcefv( ls.
sfx.
source, AL_POSITION, alf );
377 alSourcefv( ls.
sfx.
source, AL_VELOCITY, alf );
391 ls.
radius = luaL_optnumber(L,9,-1.);
394 if (!lua_isnoneornil(L,10))
399 ls.
data = luaL_ref( L, LUA_REGISTRYINDEX );
403 if (lua_spfx_queue == NULL)
427 if (ls->
remove != LUA_NOREF) {
428 lua_rawgeti( naevL, LUA_REGISTRYINDEX, ls->
remove );
430 if (lua_pcall( naevL, 1, 0, 0) != 0) {
431 WARN(_(
"Spfx failed to run 'remove':\n%s"), lua_tostring( naevL, -1 ));
526 lua_rawgeti( L, LUA_REGISTRYINDEX, ls->
data );
552 alSourcef( ls->
sfx.
source, AL_PITCH, s );
588static void spfx_lock (
void)
593static void spfx_unlock (
void)
597 if (lua_spfx_queue==NULL)
600 for (
int i=0; i<
array_size(lua_spfx_queue); i++)
613 for (
int i=0; i<
array_size(lua_spfx_queue); i++)
618void spfxL_exit (
void)
624 lua_spfx_queue = NULL;
658 alSourcefv( ls->
sfx.
source, AL_POSITION, alf );
665 if (ls->
update == LUA_NOREF)
669 lua_rawgeti( naevL, LUA_REGISTRYINDEX, ls->
update );
671 lua_pushnumber( naevL, dt );
672 if (lua_pcall( naevL, 2, 0, 0) != 0) {
673 WARN(_(
"Spfx failed to run 'update':\n%s"), lua_tostring( naevL, -1 ));
680static void spfxL_renderLayer(
int func,
const char *funcname,
double dt )
701 WARN(_(
"Unknown render layer '%d'!"), func);
713 if ((r > 0.) && ((pos.
x < -r) || (pos.
y < -r) ||
714 (pos.
x > SCREEN_W+r) || (pos.
y > SCREEN_H+r)))
718 pos.
y = SCREEN_H-pos.
y;
721 lua_rawgeti( naevL, LUA_REGISTRYINDEX, funcref );
723 lua_pushnumber( naevL, pos.
x );
724 lua_pushnumber( naevL, pos.
y );
725 lua_pushnumber( naevL, z );
726 lua_pushnumber( naevL, dt );
727 if (lua_pcall( naevL, 5, 0, 0) != 0) {
728 WARN(_(
"Spfx failed to run '%s':\n%s"), funcname, lua_tostring( naevL, -1 ));
740 spfxL_renderLayer( 0,
"renderbg", dt );
748 spfxL_renderLayer( 1,
"rendermg", dt );
756 spfxL_renderLayer( 2,
"rendermg", dt );
770 double mass = luaL_checknumber( L, 1 );
771 double radius = luaL_checknumber( L, 2 );
774 debris_add( mass, radius, p->x, p->y, v->x, v->y );
Provides macros to work with dynamic arrays.
#define array_free(ptr_array)
Frees memory allocated and sets array to NULL.
#define array_end(array)
Returns a pointer to the end of the reserved memory space.
#define array_erase(ptr_array, first, last)
Erases elements in interval [first, last).
static ALWAYS_INLINE int array_size(const void *array)
Returns number of elements in the array.
#define array_push_back(ptr_array, element)
Adds a new element at the end of the array.
#define array_begin(array)
Returns a pointer to the beginning of the reserved memory space.
#define array_create(basic_type)
Creates a new dynamic array of ‘basic_type’.
double cam_getZoom(void)
Gets the camera zoom.
void debris_add(double mass, double r, double px, double py, double vx, double vy)
Creates a cloud of debris.
Header file with generic functions and naev-specifics.
void nlua_unref(lua_State *L, int idx)
Removes a reference set with nlua_ref.
int nlua_ref(lua_State *L, int idx)
Creates a new reference to a Lua structure at a position.
LuaAudio_t * lua_pushaudio(lua_State *L, LuaAudio_t audio)
Pushes a audio on the stack.
LuaAudio_t * luaL_checkaudio(lua_State *L, int ind)
Gets audio at index or raises error if there is no audio at index.
static int spfxL_new(lua_State *L)
Creates a new special effect.
static int spfxL_vel(lua_State *L)
Gets the velocity of a spfx.
static int spfxL_sfx(lua_State *L)
Gets the sound effect of a spfx.
static int spfxL_setVel(lua_State *L)
Sets the velocity of a spfx.
int lua_isspfx(lua_State *L, int ind)
Checks to see if ind is a spfx.
void spfxL_setSpeedVolume(double v)
Sets the speed volume due to autonav and the likes.
static const luaL_Reg spfxL_methods[]
void spfxL_renderbg(double dt)
Renders the Lua SPFX on the background.
static int spfxL_setPos(lua_State *L)
Sets the position of a spfx.
static int spfxL_pos(lua_State *L)
Gets the position of a spfx.
void spfxL_rendermg(double dt)
Renders the Lua SPFX in the midground.
static int spfxL_eq(lua_State *L)
Compares two spfxs to see if they are the same.
void spfxL_renderfg(double dt)
Renders the Lua SPFX in the foreground.
void spfxL_clear(void)
Clears the Lua spfx.
static void spfx_cleanup(LuaSpfxData_t *ls)
Cleans up a special effect.
static int spfxL_data(lua_State *L)
Gets the data table of a spfx.
static int spfxL_rm(lua_State *L)
Removes a special effect.
static LuaSpfxData_t * lua_spfx
List of special effects being handled.
void spfxL_update(double dt)
Updates the spfx.
LuaSpfx_t * luaL_checkspfx(lua_State *L, int ind)
Gets spfx at index or raises error if there is no spfx at index.
static int spfxL_debris(lua_State *L)
Creates a cloud of debris.
static int spfxL_getAll(lua_State *L)
Gets all the active spfx.
int nlua_loadSpfx(nlua_env env)
Loads the spfx library.
LuaSpfx_t * lua_pushspfx(lua_State *L, LuaSpfx_t spfx)
Pushes a spfx on the stack.
void spfxL_setSpeed(double s)
Sets the speed of the playing spfx sounds.
LuaSpfx_t * lua_tospfx(lua_State *L, int ind)
Gets spfx at index.
static int spfxL_gc(lua_State *L)
Lua bindings to interact with spfx.
vec2 * luaL_checkvector(lua_State *L, int ind)
Gets vector at index making sure type is valid.
vec2 * lua_pushvector(lua_State *L, vec2 vec)
Pushes a vector on the stack.
void gl_gameToScreenCoords(double *nx, double *ny, double bx, double by)
Converts in-game coordinates to screen coordinates.
double player_dt_default(void)
Returns the player's total default time delta based on time dilation stuff.
ALuint sound_efx_directSlot
Handles the special effects Lua-side.