[Change] Better handling of music.
This commit is contained in:
parent
26e966c1dd
commit
f38da7322b
130
snd/music.lua
130
snd/music.lua
@ -9,11 +9,48 @@
|
|||||||
]]--
|
]]--
|
||||||
last = "idle"
|
last = "idle"
|
||||||
function choose(str)
|
function choose(str)
|
||||||
|
-- Means to only change song if needed.
|
||||||
|
if str == nil then
|
||||||
|
str = "ambient"
|
||||||
|
end
|
||||||
|
|
||||||
if str == "load" then
|
if str == "load" then
|
||||||
music.load("machina")
|
choose_load()
|
||||||
music.play()
|
|
||||||
|
|
||||||
elseif str == "land" then
|
elseif str == "land" then
|
||||||
|
choose_land()
|
||||||
|
|
||||||
|
elseif str == "takeoff" then
|
||||||
|
choose_takeoff()
|
||||||
|
|
||||||
|
elseif str == "ambient" then
|
||||||
|
choose_ambient()
|
||||||
|
|
||||||
|
elseif str == "combat" then
|
||||||
|
choose_battle()
|
||||||
|
|
||||||
|
elseif str == "idle" and last ~= "idle" then
|
||||||
|
-- We'll play the same as last unless it was takeoff.
|
||||||
|
if last == "takeoff" then
|
||||||
|
choose_ambient()
|
||||||
|
else
|
||||||
|
choose(last)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
if str ~= "idle" then
|
||||||
|
last = str -- Save the last string so we can use it.
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Loading songs.
|
||||||
|
function choose_load()
|
||||||
|
music.load("machina")
|
||||||
|
music.play()
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Landing songs.
|
||||||
|
function choose_land()
|
||||||
planet = space.getPlanet()
|
planet = space.getPlanet()
|
||||||
class = planet:class()
|
class = planet:class()
|
||||||
|
|
||||||
@ -33,40 +70,91 @@ function choose(str)
|
|||||||
|
|
||||||
music.load(mus)
|
music.load(mus)
|
||||||
music.play()
|
music.play()
|
||||||
|
end
|
||||||
|
|
||||||
elseif str == "takeoff" then
|
-- Takeoff songs.
|
||||||
|
function choose_takeoff()
|
||||||
|
-- No need to restart.
|
||||||
|
if last == "takeoff" and music.isPlaying() then
|
||||||
|
return
|
||||||
|
end
|
||||||
takeoff = { "liftoff", "launch2", "launch3chatstart" }
|
takeoff = { "liftoff", "launch2", "launch3chatstart" }
|
||||||
music.load(takeoff[rnd.int(1, #takeoff)])
|
music.load(takeoff[rnd.int(1, #takeoff)])
|
||||||
music.play()
|
music.play()
|
||||||
|
end
|
||||||
|
|
||||||
elseif str == "ambient" then
|
-- Save old data.
|
||||||
|
last_sysFaction = nil
|
||||||
|
last_sysNebuDens = nil
|
||||||
|
last_sysNebuVol = nil
|
||||||
|
-- Choose ambient songs.
|
||||||
|
function choose_ambient()
|
||||||
|
force = true
|
||||||
|
|
||||||
|
-- Check to see if we want to update.
|
||||||
|
if music.isPlaying() then
|
||||||
|
if last == "takeoff" then
|
||||||
|
return
|
||||||
|
elseif last == "ambient" then
|
||||||
|
force = false
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Get information about the current system.
|
||||||
sys = space.getSystem()
|
sys = space.getSystem()
|
||||||
factions = sys:faction()
|
factions = sys:faction()
|
||||||
|
nebu_dens, nebu_vol = sys:nebulae()
|
||||||
|
|
||||||
|
-- Check to see if changing faction zone.
|
||||||
|
if not factions[last_sysFaction] then
|
||||||
|
-- Table must not be empty.
|
||||||
|
--if next(factions) ~= nil then
|
||||||
|
force = true
|
||||||
|
--end
|
||||||
|
|
||||||
|
if force then
|
||||||
|
-- Give first value to last faction.
|
||||||
|
for k, v in pairs(factions) do
|
||||||
|
last_sysFaction = k
|
||||||
|
break
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Check to see if entering nebulae.
|
||||||
|
nebu = nebu_dens > 0
|
||||||
|
if nebu ~= last_sysNebuDens then
|
||||||
|
force = true
|
||||||
|
last_sysNebuDens = nebu
|
||||||
|
end
|
||||||
|
|
||||||
|
-- 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.
|
||||||
if factions["Collective"] then
|
if factions["Collective"] then
|
||||||
ambient = { "collective1" }
|
ambient = { "collective1" }
|
||||||
|
elseif nebu and rnd.int(0,1) == 0 then
|
||||||
|
ambient = { "ambient1" }
|
||||||
else
|
else
|
||||||
ambient = { "ambient1", "ambient2", "mission",
|
ambient = { "ambient2", "mission",
|
||||||
"peace1", "peace2", "peace4", "peace6" }
|
"peace1", "peace2", "peace4", "peace6" }
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- Load music and play.
|
||||||
music.load(ambient[rnd.int(1, #ambient)])
|
music.load(ambient[rnd.int(1, #ambient)])
|
||||||
music.play()
|
music.play()
|
||||||
|
|
||||||
elseif str == "combat" then
|
|
||||||
music.load("galacticbattle")
|
|
||||||
music.play()
|
|
||||||
|
|
||||||
elseif str == "idle" and last ~= "idle" then
|
|
||||||
-- We'll play the same as last unless it was takeoff.
|
|
||||||
if last == "takeoff" then
|
|
||||||
choose("ambient")
|
|
||||||
else
|
|
||||||
choose(last)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
if str ~= "idle" then
|
|
||||||
last = str -- Save the last string so we can use it.
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- Battle songs.
|
||||||
|
function choose_battle()
|
||||||
|
music.load("galacticbattle")
|
||||||
|
music.play()
|
||||||
|
end
|
||||||
|
|
||||||
|
@ -45,12 +45,14 @@ static const luaL_reg planet_methods[] = {
|
|||||||
static int systemL_eq(lua_State* L);
|
static int systemL_eq(lua_State* L);
|
||||||
static int systemL_name(lua_State* L);
|
static int systemL_name(lua_State* L);
|
||||||
static int systemL_faction(lua_State* L);
|
static int systemL_faction(lua_State* L);
|
||||||
|
static int systemL_nebulae(lua_State* L);
|
||||||
static int systemL_jumpdistance(lua_State* L);
|
static int systemL_jumpdistance(lua_State* L);
|
||||||
static const luaL_reg system_methods[] = {
|
static const luaL_reg system_methods[] = {
|
||||||
{ "__eq", systemL_eq },
|
{ "__eq", systemL_eq },
|
||||||
{ "__tostring", systemL_name },
|
{ "__tostring", systemL_name },
|
||||||
{ "name", systemL_name },
|
{ "name", systemL_name },
|
||||||
{ "faction", systemL_faction },
|
{ "faction", systemL_faction },
|
||||||
|
{ "nebulae", systemL_nebulae },
|
||||||
{ "jumpDist", systemL_jumpdistance },
|
{ "jumpDist", systemL_jumpdistance },
|
||||||
{ 0, 0 }
|
{ 0, 0 }
|
||||||
};
|
};
|
||||||
@ -618,6 +620,20 @@ static int systemL_faction(lua_State* L) {
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @ingroup META_SYSTEM
|
||||||
|
*/
|
||||||
|
static int systemL_nebulae(lua_State* L) {
|
||||||
|
LuaSystem* sys;
|
||||||
|
sys = lua_tosystem(L, 1);
|
||||||
|
|
||||||
|
/* Push the density and volatility. */
|
||||||
|
lua_pushnumber(L, sys->s->nebu_density);
|
||||||
|
lua_pushnumber(L, sys->s->nebu_volatility);
|
||||||
|
|
||||||
|
return 2;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @ingroup META_SYSTEM
|
* @ingroup META_SYSTEM
|
||||||
*
|
*
|
||||||
|
22
src/music.c
22
src/music.c
@ -44,11 +44,13 @@ static int music_runLua(char* situation);
|
|||||||
static int musicL_load(lua_State* L);
|
static int musicL_load(lua_State* L);
|
||||||
static int musicL_play(lua_State* L);
|
static int musicL_play(lua_State* L);
|
||||||
static int musicL_stop(lua_State* L);
|
static int musicL_stop(lua_State* L);
|
||||||
|
static int musicL_isPlaying(lua_State* L);
|
||||||
static const luaL_reg music_methods[] = {
|
static const luaL_reg music_methods[] = {
|
||||||
{ "load", musicL_load },
|
{ "load", musicL_load },
|
||||||
{ "play", musicL_play },
|
{ "play", musicL_play },
|
||||||
{ "stop", musicL_stop },
|
{ "stop", musicL_stop },
|
||||||
{0, 0}
|
{ "isPlaying", musicL_isPlaying },
|
||||||
|
{0, 0 }
|
||||||
}; /**< Music specific methods. */
|
}; /**< Music specific methods. */
|
||||||
|
|
||||||
/* What is available? */
|
/* What is available? */
|
||||||
@ -267,8 +269,6 @@ void music_play(void) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @fn void music_stop(void)
|
|
||||||
*
|
|
||||||
* @brief Stop the loaded music.
|
* @brief Stop the loaded music.
|
||||||
*/
|
*/
|
||||||
void music_stop(void) {
|
void music_stop(void) {
|
||||||
@ -276,7 +276,7 @@ void music_stop(void) {
|
|||||||
|
|
||||||
if(music_music == NULL) return;
|
if(music_music == NULL) return;
|
||||||
|
|
||||||
if(Mix_FadeOutMusic(500) < 0)
|
if(Mix_FadeOutMusic(2000) < 0)
|
||||||
WARN("SDL_Mixer: %s", Mix_GetError());
|
WARN("SDL_Mixer: %s", Mix_GetError());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -302,6 +302,15 @@ void music_resume(void) {
|
|||||||
Mix_ResumeMusic();
|
Mix_ResumeMusic();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Check to see if the music is playing.
|
||||||
|
*/
|
||||||
|
int music_isPlaying(void) {
|
||||||
|
if(music_disabled) return 0; /* Always playing when music is off. */
|
||||||
|
|
||||||
|
return Mix_PlayingMusic();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Sets the music to a position in seconds.
|
* @brief Sets the music to a position in seconds.
|
||||||
* @param sec Position to go to in seconds.
|
* @param sec Position to go to in seconds.
|
||||||
@ -440,3 +449,8 @@ static int musicL_stop(lua_State* L) {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int musicL_isPlaying(lua_State* L) {
|
||||||
|
lua_pushboolean(L, music_isPlaying());
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
@ -19,6 +19,7 @@ void music_stop(void);
|
|||||||
void music_pause(void);
|
void music_pause(void);
|
||||||
void music_resume(void);
|
void music_resume(void);
|
||||||
void music_setPos(double sec);
|
void music_setPos(double sec);
|
||||||
|
int music_isPlaying(void);
|
||||||
|
|
||||||
/* Lua control. */
|
/* Lua control. */
|
||||||
int lua_loadMusic(lua_State* L, int read_only);
|
int lua_loadMusic(lua_State* L, int read_only);
|
||||||
|
@ -34,6 +34,7 @@
|
|||||||
#include "intro.h"
|
#include "intro.h"
|
||||||
#include "perlin.h"
|
#include "perlin.h"
|
||||||
#include "ai.h"
|
#include "ai.h"
|
||||||
|
#include "music.h"
|
||||||
#include "player.h"
|
#include "player.h"
|
||||||
|
|
||||||
#define XML_GUI_ID "GUIs" /**< XML section identifier for GUI document. */
|
#define XML_GUI_ID "GUIs" /**< XML section identifier for GUI document. */
|
||||||
@ -2737,6 +2738,7 @@ static int player_parse(xmlNodePtr parent) {
|
|||||||
gl_bindCamera(&player->solid->pos);
|
gl_bindCamera(&player->solid->pos);
|
||||||
|
|
||||||
/* Initialize the system. */
|
/* Initialize the system. */
|
||||||
|
music_choose("takeoff");
|
||||||
space_init(planet_getSystem(planet));
|
space_init(planet_getSystem(planet));
|
||||||
map_clear(); /* Sets the map up. */
|
map_clear(); /* Sets the map up. */
|
||||||
|
|
||||||
|
@ -15,6 +15,7 @@
|
|||||||
#include "spfx.h"
|
#include "spfx.h"
|
||||||
#include "ltime.h"
|
#include "ltime.h"
|
||||||
#include "nebulae.h"
|
#include "nebulae.h"
|
||||||
|
#include "music.h"
|
||||||
#include "space.h"
|
#include "space.h"
|
||||||
|
|
||||||
#define XML_PLANET_ID "Planets"
|
#define XML_PLANET_ID "Planets"
|
||||||
@ -633,6 +634,9 @@ void space_init(const char* sysname) {
|
|||||||
if(cur_system->interference == 0.)
|
if(cur_system->interference == 0.)
|
||||||
interference_alpha = 0.;
|
interference_alpha = 0.;
|
||||||
|
|
||||||
|
/* See if we should get a new music song. */
|
||||||
|
music_choose(NULL);
|
||||||
|
|
||||||
/* Reset player enemies. */
|
/* Reset player enemies. */
|
||||||
player_enemies = 0;
|
player_enemies = 0;
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user