Complete.Org: Mailing Lists: Archives: freeciv-dev: July 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: "Gregory Richards" <akaquinn@xxxxxxxxxxx>
Date: Fri, 23 Jul 2004 16:16:07 -0700
Reply-to: rt@xxxxxxxxxxx

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

This adds "mood" background music to FreeCiv.  Every turn, it chooses 
what background music to play based on the "mood" of the game.  At 
this point, I have 5 "moods" 
Here are the moods and their corresponing soundspec entry: 
pregame (music_start) 
beginning (music_begin) 
ambient (music_ambient) 
offensive (music_offensive) 
defensive (music_defensive) 
diff -ruN -X freeciv-cvs-Jul-21/diff_ignore freeciv-cvs-Jul-21/client/audio.c 
freeciv-cvs-Jul-21-music/client/audio.c
--- freeciv-cvs-Jul-21/client/audio.c   2004-04-19 22:14:21.000000000 -0700
+++ freeciv-cvs-Jul-21-music/client/audio.c     2004-07-23 16:18:18.000000000 
-0700
@@ -1,4 +1,4 @@
-/********************************************************************** 
+/**********************************************************************
  Freeciv - Copyright (C) 1996 - A Kjeldberg, L Gregersen, P Unold
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
@@ -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 char *curmusic;
 
 /**********************************************************************
   Returns a static, NULL-terminated list of all sound plugins
@@ -66,7 +72,7 @@
 {
   static const char* plugin_list[MAX_NUM_PLUGINS + 1];
   int i;
-  
+
   for (i = 0; i < num_plugins_used; i++) {
     plugin_list[i] = plugins[i].name;
   }
@@ -169,6 +175,8 @@
 #ifdef AMIGA
   audio_amiga_init();
 #endif
+
+  curmusic = (char *) malloc(256);
 }
 
 /**************************************************************************
@@ -277,7 +285,7 @@
   }
 
 #ifdef ESD
-  if (audio_select_plugin("esd")) return; 
+  if (audio_select_plugin("esd")) return;
 #endif
 #ifdef SDL
   if (audio_select_plugin("sdl")) return; 
@@ -348,17 +356,28 @@
 **************************************************************************/
 void audio_play_music(const char *const tag, char *const alt_tag)
 {
-  char *pretty_alt_tag = alt_tag ? alt_tag : "(null)";
+       char *pretty_alt_tag = alt_tag ? alt_tag : "(null)";
 
-  assert(tag != NULL);
+       assert(tag != NULL);
 
-  freelog(LOG_DEBUG, "audio_play_music('%s', '%s')", tag, pretty_alt_tag);
+       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 (strcmp(curmusic, tag)) {
+               if (audio_play_tag(tag, TRUE)) {
+                       strcpy(curmusic, tag);
+                       return;
+               }
+
+               if (strcmp(curmusic, alt_tag)) {
+                       if (audio_play_tag(alt_tag, TRUE)) {
+                               strcpy(curmusic, alt_tag);
+                               return;
+                       }
+
+                       freelog(LOG_VERBOSE, "Neither of tags %s or %s found", 
tag,
+                               pretty_alt_tag);
+               }
+       }
 }
 
 /**************************************************************************
@@ -407,3 +426,59 @@
   sz_strlcat(buffer, "]");
   return buffer;
 }
+
+/**************************************************************************
+  Sets the audio according to the "mood" of the game
+**************************************************************************/
+void mood_music()
+{
+  /* ESD does not stop music, and having many playing at once is bad at best */
+  if (strcmp(plugins[selected_plugin].name, "esd")) {
+    struct player *pplayer = game.player_ptr;
+    struct player *cplayer;
+    struct unit *punit;
+    int offensive = 0;
+    int defensive = 0;
+
+    /* 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 (cplayer && pplayer) {
+        if (pplayers_at_war(pplayer, cplayer)) {
+          unit_list_iterate(cplayer->units, punit) {
+            if (map_get_owner(punit->x, punit->y) == pplayer) {
+              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)) {
+          offensive++;
+        }
+      }
+    } unit_list_iterate_end
+
+    /* C) Choose whether to do offensive/defensive or ambient music */
+    if (offensive && defensive) {
+      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-Jul-21/diff_ignore freeciv-cvs-Jul-21/client/audio.h 
freeciv-cvs-Jul-21-music/client/audio.h
--- freeciv-cvs-Jul-21/client/audio.h   2002-11-01 10:11:42.000000000 -0800
+++ freeciv-cvs-Jul-21-music/client/audio.h     2004-07-23 16:18:10.000000000 
-0700
@@ -1,4 +1,4 @@
-/********************************************************************** 
+/**********************************************************************
  Freeciv - Copyright (C) 1996 - A Kjeldberg, L Gregersen, P Unold
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
@@ -44,4 +44,6 @@
 bool audio_select_plugin(const char *const name);
 const char *audio_get_all_plugin_names(void);
 
+void mood_music(void);
+
 #endif                         /* FC__AUDIO_H */
diff -ruN -X freeciv-cvs-Jul-21/diff_ignore 
freeciv-cvs-Jul-21/client/packhand.c freeciv-cvs-Jul-21-music/client/packhand.c
--- freeciv-cvs-Jul-21/client/packhand.c        2004-07-20 22:14:34.000000000 
-0700
+++ freeciv-cvs-Jul-21-music/client/packhand.c  2004-07-22 20:42:28.000000000 
-0700
@@ -1,4 +1,4 @@
-/********************************************************************** 
+/**********************************************************************
  Freeciv - Copyright (C) 1996 - A Kjeldberg, L Gregersen, P Unold
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
@@ -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]