diff --git a/snd/music.lua b/snd/music.lua index b87e634..523c512 100644 --- a/snd/music.lua +++ b/snd/music.lua @@ -87,6 +87,8 @@ end last_sysFaction = nil last_sysNebuDens = nil last_sysNebuVol = nil +ambient_neutral = { "ambient2", "mission", + "peace1", "peace2", "peace4", "peace6" } -- Choose ambient songs. function choose_ambient() force = true @@ -108,9 +110,9 @@ function choose_ambient() -- Check to see if changing faction zone. if not factions[last_sysFaction] then -- Table must not be empty. - --if next(factions) ~= nil then + if next(factions) ~= nil then force = true - --end + end if force then -- Give first value to last faction. @@ -130,20 +132,39 @@ function choose_ambient() -- Must be forced. if force then - -- Stop playing first and have this trigger again. - if music.isPlaying() then - music.stop() - return - end - -- Choose the music, bias by faction first. + add_neatral = false if factions["Collective"] then ambient = { "collective1" } - elseif nebu and rnd.int(0,1) == 0 then - ambient = { "ambient1" } + elseif factions["Empire"] then + ambient = { "empire1", "empire1", "empire1", "empire1" } + add_neutral = true + elseif nebu then + ambient = { "ambient1", "ambient1", "ambient1", "ambient1" } + add_neutral = true else - ambient = { "ambient2", "mission", - "peace1", "peace2", "peace4", "peace6" } + ambient = ambient_neutral + end + + -- Check if we need to append neutral ambient songs. + if add_neutral then + for k,v in pairs(ambient_neutral) do + table.insert(ambient, v) + end + end + + -- Make sure it's not already in the list or that we have to stop the + -- currently playing song. + if music.isPlaying() then + cur = music.current() + for k,v in pairs(ambient) do + if cur == v then + return + end + end + + music.stop() + return end -- Load music and play. diff --git a/snd/music/empire1.ogg b/snd/music/empire1.ogg new file mode 100644 index 0000000..adc8df5 Binary files /dev/null and b/snd/music/empire1.ogg differ diff --git a/src/llua.c b/src/llua.c index 2248d3e..b9d9e17 100644 --- a/src/llua.c +++ b/src/llua.c @@ -118,7 +118,8 @@ int llua_loadBasic(lua_State* L) { lua_setglobal(L, override[i]); } - llua_load(L, luaopen_math); /* Open math. */ + llua_load(L, luaopen_math); /* Open math. */ + llua_load(L, luaopen_table); /* Open table. */ /* Add our own. */ lua_register(L, "include", llua_packfileLoader); @@ -126,6 +127,13 @@ int llua_loadBasic(lua_State* L) { return 0; } +/** + * @brief include(string module) + * + * Loads a module into the current Lua state from inside the data file. + * @param module Name of the module to load. + * @return An error string on error. + */ static int llua_packfileLoader(lua_State* L) { char* buf, *filename; uint32_t bufsize; diff --git a/src/music.c b/src/music.c index 19a0cb5..538ac09 100644 --- a/src/music.c +++ b/src/music.c @@ -45,18 +45,22 @@ static int musicL_load(lua_State* L); static int musicL_play(lua_State* L); static int musicL_stop(lua_State* L); static int musicL_isPlaying(lua_State* L); +static int musicL_current(lua_State* L); static const luaL_reg music_methods[] = { { "load", musicL_load }, { "play", musicL_play }, { "stop", musicL_stop }, { "isPlaying", musicL_isPlaying }, - {0, 0 } + { "current", musicL_current }, + { 0, 0 } }; /**< Music specific methods. */ /* What is available? */ static char** music_selection = NULL; /**< Available music selection. */ static int nmusic_selection = 0; /**< Size of available music selection. */ +/* Current music. */ +static char* music_name = NULL; /**< Current music name. */ static void* music_data = NULL; /**< Current music data. */ static SDL_RWops* music_rw = NULL; /**< Current music RWops. */ static Mix_Music* music_music = NULL; /**< Current music. */ @@ -111,8 +115,6 @@ static int music_runLua(char* situation) { } /** - * @fn int music init(void) - * * @brief Initializes the music subsystem. * @return 0 on success. */ @@ -130,8 +132,6 @@ int music_init(void) { } /** - * @fn void music_exit(void) - * * @brief Exits the music subsystem. */ void music_exit(void) { @@ -145,8 +145,6 @@ void music_exit(void) { } /** - * @fn static void music_free(void) - * * @brief Free the current playing music. */ static void music_free(void) { @@ -156,6 +154,8 @@ static void music_free(void) { Mix_FreeMusic(music_music); /*SDL_FreeRW(music_rw);*/ /*FreeMusic frees it itself. */ free(music_data); + free(music_name); + music_name = NULL; music_music = NULL; music_rw = NULL; music_data = NULL; @@ -163,8 +163,6 @@ static void music_free(void) { } /** - * @fn static int music_find(void) - * * @brief Internal music loading routines. * @return 0 on success. */ @@ -217,8 +215,6 @@ static int music_find(void) { } /** - * @fn int music_volume(const double vol) - * * @brief Set the music volume. * @param vol Volume to set to (between 0 and 1). * @return 0 on success. @@ -230,8 +226,6 @@ int music_volume(const double vol) { } /** - * @fn void music_load(const char* name) - * * @brief Load the music by name. * @param name Name of the file to load. */ @@ -245,6 +239,7 @@ void music_load(const char* name) { /* Load the data. */ snprintf(filename, PATH_MAX, MUSIC_PREFIX"%s"MUSIC_SUFFIX, name); + music_name = strdup(name); music_data = ldata_read(filename, &size); music_rw = SDL_RWFromMem(music_data, size); music_music = Mix_LoadMUS_RW(music_rw); @@ -255,8 +250,6 @@ void music_load(const char* name) { } /** - * @fn void music_play(void) - * * @brief Play the loaded music. */ void music_play(void) { @@ -325,8 +318,6 @@ void music_setPos(double sec) { /* Music lua stuff. */ /** - * @fn static int music_luaInit(void) - * * @brief Initializes the music Lua control system. * @return 0 on success. */ @@ -364,8 +355,6 @@ static int music_luaInit(void) { } /** - * @fn static void music_luaQuit(void) - * * @brief Quit the music Lua control system. */ static void music_luaQuit(void) { @@ -379,8 +368,6 @@ static void music_luaQuit(void) { } /** - * @fn int lua_loadMusic(lua_State* L, int read_only) - * * @brief Load the music functions into a lua_State. * @param L Lua State to load the music functions into. * @param read_only Load the write functions? @@ -393,8 +380,6 @@ int lua_loadMusic(lua_State* L, int read_only) { } /** - * @fn int music_choose(char* situation) - * * @brief Actually run the music stuff, based on situation. * @param situation choose a new music to play. * @return 0 on success. @@ -408,8 +393,6 @@ int music_choose(char* situation) { } /** - * @fn static void music_rechoose(void) - * * @brief Attempts to rechoose the music. * * ARGH! DO NOT CALL MIX_* FUNCTIONS FROM WITHIN THE CALLBACKS! @@ -454,3 +437,8 @@ static int musicL_isPlaying(lua_State* L) { return 1; } +static int musicL_current(lua_State* L) { + lua_pushstring(L, (music_name != NULL) ? music_name : "none"); + return 1; +} +