Complete.Org: Mailing Lists: Archives: freeciv-dev: August 2004:
[Freeciv-Dev] (PR#9490) Patch: Background "mood" music
Home

[Freeciv-Dev] (PR#9490) Patch: Background "mood" music

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: undisclosed-recipients: ;
Subject: [Freeciv-Dev] (PR#9490) Patch: Background "mood" music
From: "Gregor Richards" <akaquinn@xxxxxxxxxxx>
Date: Sat, 21 Aug 2004 18:16:46 -0700
Reply-to: rt@xxxxxxxxxxx

<URL: http://rt.freeciv.org/Ticket/Display.html?id=9490 >

Updated to the latest CVS, and removed some silly whitespace problems 
from the diff. 
diff -ruN -X freeciv-cvs-Aug-21/diff_ignore freeciv-cvs-Aug-21/client/audio.c 
freeciv-cvs-Aug-21-music/client/audio.c
--- freeciv-cvs-Aug-21/client/audio.c   2004-04-19 10:24:16.000000000 -0700
+++ freeciv-cvs-Aug-21-music/client/audio.c     2004-08-21 18:06:05.000000000 
-0700
@@ -27,6 +27,11 @@
 #include "registry.h"
 #include "shared.h"
 #include "support.h"
+#include "game.h"
+#include "player.h"
+#include "unit.h"
+#include "map.h"
+#include "unistd.h"
 
 #ifdef AMIGA
 #include "audio_amiga.h"
@@ -56,6 +61,7 @@
 static struct audio_plugin plugins[MAX_NUM_PLUGINS];
 static int num_plugins_used = 0;
 static int selected_plugin = -1;
+static const char *curmusic;
 
 /**********************************************************************
   Returns a static, NULL-terminated list of all sound plugins
@@ -154,9 +160,6 @@
   assert(num_plugins_used == 1);
   selected_plugin = 0;
 
-#ifdef ESD
-  audio_esd_init();
-#endif
 #ifdef SDL
   audio_sdl_init();
 #endif
@@ -169,6 +172,9 @@
 #ifdef AMIGA
   audio_amiga_init();
 #endif
+#ifdef ESD
+  audio_esd_init();
+#endif
 }
 
 /**************************************************************************
@@ -276,9 +282,6 @@
     return;
   }
 
-#ifdef ESD
-  if (audio_select_plugin("esd")) return; 
-#endif
 #ifdef SDL
   if (audio_select_plugin("sdl")) return; 
 #endif
@@ -291,6 +294,9 @@
 #ifdef AMIGA
   if (audio_select_plugin("amiga")) return;
 #endif
+#ifdef ESD
+  if (audio_select_plugin("esd")) return;
+#endif
   freelog(LOG_ERROR,
     _("No real audio subsystem managed to initialize!"));
   freelog(LOG_ERROR,
@@ -354,10 +360,21 @@
 
   freelog(LOG_DEBUG, "audio_play_music('%s', '%s')", tag, pretty_alt_tag);
 
-  /* try playing primary tag first, if not go to alternative tag */
-  if (!audio_play_tag(tag, TRUE) && !audio_play_tag(alt_tag, TRUE)) {
-    freelog(LOG_VERBOSE, "Neither of tags %s or %s found", tag,
-           pretty_alt_tag);
+  if (!curmusic || strcmp(curmusic, tag)) {
+    if (audio_play_tag(tag, TRUE)) {
+      curmusic = tag;
+      return;
+    }
+
+    if (!curmusic || strcmp(curmusic, alt_tag)) {
+      if (audio_play_tag(alt_tag, TRUE)) {
+        curmusic = alt_tag;
+        return;
+      }
+
+      freelog(LOG_VERBOSE, "Neither of tags %s or %s found", tag,
+              pretty_alt_tag);
+    }
   }
 }
 
@@ -407,3 +424,71 @@
   sz_strlcat(buffer, "]");
   return buffer;
 }
+
+/**************************************************************************
+  This is merely a wrapper for mood_music (below)
+**************************************************************************/
+void bg_mood_music_callback(struct client_option *option)
+{
+  if (!option->p_bool_value) return;
+  if (*(option->p_bool_value) == TRUE) {
+    bg_mood_music = TRUE;
+    mood_music();
+  } else {
+    curmusic = NULL;
+    audio_stop();
+  }
+}
+
+/**************************************************************************
+  Sets the audio according to the "mood" of the game
+**************************************************************************/
+void mood_music()
+{
+  struct player *pplayer = game.player_ptr;
+  int offensive = 0;
+  int defensive = 0;
+
+  /* ESD does not stop music, and having many playing at once is bad at best */
+  if (selected_plugin == -1) return;
+  if (!strcmp(plugins[selected_plugin].name, "esd") || !bg_mood_music) return;
+
+  /* 1) Are we at the beginning of the game? */
+  if (game.turn <= 5) {
+    audio_play_music("music_begin", "music_ambient");
+    return;
+  }
+
+  /* 2) Get number of units in enemy war zones and number of enemy units in 
our zone */
+  /* A) Iterate through all players, seek out enemies, and find if they have 
people in our zone */
+  players_iterate(cplayer) {
+    if (pplayers_at_war(pplayer, cplayer)) {
+      unit_list_iterate(cplayer->units, punit) {
+        if (map_get_owner(punit->x, punit->y) == pplayer && 
is_military_unit(punit)) {
+          defensive++;
+        }
+      } unit_list_iterate_end
+    }
+  } players_iterate_end
+
+  /* B) Iterate through own units, and find if they're in enemy positions */
+  unit_list_iterate(pplayer->units, punit) {
+    if (map_get_owner(punit->x, punit->y)) {
+      if (pplayers_at_war(map_get_owner(punit->x, punit->y), pplayer) && 
is_military_unit(punit)) {
+        offensive++;
+      }
+    }
+  } unit_list_iterate_end
+
+  /* C) Choose whether to do offensive/defensive or ambient music */
+  if (offensive > 0 || defensive > 0) {
+    if (defensive > offensive) {
+      audio_play_music("music_defense", "music_ambient");
+    } else {
+      audio_play_music("music_offense", "music_ambient");
+    }
+    return;
+  }
+
+  audio_play_music("music_ambient", NULL);
+}
diff -ruN -X freeciv-cvs-Aug-21/diff_ignore freeciv-cvs-Aug-21/client/audio.h 
freeciv-cvs-Aug-21-music/client/audio.h
--- freeciv-cvs-Aug-21/client/audio.h   2002-11-01 10:11:42.000000000 -0800
+++ freeciv-cvs-Aug-21-music/client/audio.h     2004-08-21 18:06:05.000000000 
-0700
@@ -14,6 +14,7 @@
 #define FC__AUDIO_H
 
 #include "shared.h"            /* bool type */
+#include "options.h"
 
 #define MAX_AUDIO_NAME_LEN             20
 #define MAX_AUDIO_DESCR_LEN            200
@@ -44,4 +45,7 @@
 bool audio_select_plugin(const char *const name);
 const char *audio_get_all_plugin_names(void);
 
+void mood_music(void);
+void bg_mood_music_callback(struct client_option *option);
+
 #endif                         /* FC__AUDIO_H */
diff -ruN -X freeciv-cvs-Aug-21/diff_ignore freeciv-cvs-Aug-21/client/options.c 
freeciv-cvs-Aug-21-music/client/options.c
--- freeciv-cvs-Aug-21/client/options.c 2004-08-21 16:25:54.000000000 -0700
+++ freeciv-cvs-Aug-21-music/client/options.c   2004-08-21 18:06:05.000000000 
-0700
@@ -71,6 +71,7 @@
 bool popup_new_cities = TRUE;
 bool keyboardless_goto = TRUE;
 bool show_task_icons = TRUE;
+bool bg_mood_music = TRUE;
 
 /* This option is currently set by the client - not by the user. */
 bool update_city_text_in_refresh_tile = TRUE;
@@ -105,6 +106,7 @@
   GEN_BOOL_OPTION(auto_turn_done,           N_("End Turn when done moving")),
   GEN_BOOL_OPTION(ask_city_name,            N_("Prompt for city names")),
   GEN_BOOL_OPTION(popup_new_cities,         N_("Pop up city dialog for new 
cities")),
+  GEN_BOOL_OPTION_CALLBACK(bg_mood_music,   N_("Background music"), 
bg_mood_music_callback)
 };
 #undef GEN_INT_OPTION
 #undef GEN_BOOL_OPTION
diff -ruN -X freeciv-cvs-Aug-21/diff_ignore freeciv-cvs-Aug-21/client/options.h 
freeciv-cvs-Aug-21-music/client/options.h
--- freeciv-cvs-Aug-21/client/options.h 2004-08-08 10:21:48.000000000 -0700
+++ freeciv-cvs-Aug-21-music/client/options.h   2004-08-21 18:06:05.000000000 
-0700
@@ -46,6 +46,7 @@
 extern bool update_city_text_in_refresh_tile;
 extern bool keyboardless_goto;
 extern bool show_task_icons;
+extern bool bg_mood_music;
 
 enum client_option_type {
   COT_BOOL,
@@ -80,6 +81,9 @@
 #define GEN_BOOL_OPTION(oname, desc) { #oname, desc, COT_BOOL, \
                                        NULL, &oname, NULL, 0, NULL, \
                                        NULL, NULL }
+#define GEN_BOOL_OPTION_CALLBACK(oname, desc, callback) { #oname, desc, 
COT_BOOL, \
+                                      NULL, &oname, NULL, 0, callback, \
+                                      NULL, NULL }
 #define GEN_STR_OPTION(oname, desc, str_defaults, callback) \
                                     { #oname, desc, COT_STR, \
                                       NULL, NULL, oname, sizeof(oname), \
diff -ruN -X freeciv-cvs-Aug-21/diff_ignore 
freeciv-cvs-Aug-21/client/packhand.c freeciv-cvs-Aug-21-music/client/packhand.c
--- freeciv-cvs-Aug-21/client/packhand.c        2004-08-21 16:25:55.000000000 
-0700
+++ freeciv-cvs-Aug-21-music/client/packhand.c  2004-08-21 18:06:05.000000000 
-0700
@@ -834,6 +834,8 @@
   if(game.player_ptr->ai.control && !ai_manual_turn_done) {
     user_ended_turn();
   }
+
+  mood_music();
 }
 
 /**************************************************************************

[Prev in Thread] Current Thread [Next in Thread]
  • [Freeciv-Dev] (PR#9490) Patch: Background "mood" music, Gregor Richards <=