Crossfire Server, Trunk  R21226
sounds.c
Go to the documentation of this file.
1 /*
2  * Crossfire -- cooperative multi-player graphical RPG and adventure game
3  *
4  * Copyright (c) 1999-2014 Mark Wedel and the Crossfire Development Team
5  * Copyright (c) 1992 Frank Tore Johansen
6  *
7  * Crossfire is free software and comes with ABSOLUTELY NO WARRANTY. You are
8  * welcome to redistribute it under certain conditions. For details, please
9  * see COPYING and LICENSE.
10  *
11  * The authors can be reached via e-mail at <crossfire@metalforge.org>.
12  */
13 
21 #include "global.h"
22 
23 #include <assert.h>
24 #include <stdlib.h>
25 #include <string.h>
26 
27 #include "sounds.h"
28 #include "sproto.h"
29 
35 #define MAX_SOUND_DISTANCE 10
36 
51 void play_sound_player_only(player *pl, int8_t sound_type, object *emitter, int dir, const char *action) {
52  SockList sl;
53  int volume = 50;
54  sstring name;
55  object *source;
56 
57  if (pl->socket.sound&SND_MUTE || !(pl->socket.sound&SND_EFFECTS))
58  return;
60  return;
61  if (!emitter->map && !(emitter->env && emitter->env->map))
62  return;
63 
64  source = emitter->map ? emitter : emitter->env;
65 
66  pl->socket.sounds_this_tick = 0;
67 
68  name = emitter->type == PLAYER ? emitter->race : emitter->name;
69  if (!source)
70  source = emitter;
71 
72  SockList_Init(&sl);
73  SockList_AddString(&sl, "sound2 ");
74  SockList_AddChar(&sl, (int8_t)(source->x-pl->ob->x));
75  SockList_AddChar(&sl, (int8_t)(source->y-pl->ob->y));
76  SockList_AddChar(&sl, dir);
77  SockList_AddChar(&sl, volume);
78  SockList_AddChar(&sl, sound_type);
79  SockList_AddLen8Data(&sl, action, strlen(action));
80  SockList_AddLen8Data(&sl, name, strlen(name));
81  Send_With_Handling(&pl->socket, &sl);
82  SockList_Term(&sl);
83 }
84 
85 #define POW2(x) ((x)*(x))
86 
98 void play_sound_map(int8_t sound_type, object *emitter, int dir, const char *action) {
99  player *pl;
100  object *source;
101 
102  if (!emitter->map && !(emitter->env && emitter->env->map))
103  return;
104 
105  source = emitter->map ? emitter : emitter->env;
106 
107  for (pl = first_player; pl; pl = pl->next) {
108  if (pl->ob->map == emitter->map) {
109  int distance = isqrt(POW2(pl->ob->x-source->x)+POW2(pl->ob->y-source->y));
110 
111  if (distance <= MAX_SOUND_DISTANCE) {
112  play_sound_player_only(pl, sound_type, emitter, dir, action);
113  }
114  }
115  }
116 }
117 
126 void send_background_music(player *pl, const char *music) {
127  SockList sl;
128 
129  if (pl->socket.sound&SND_MUTE || !(pl->socket.sound&SND_MUSIC))
130  return;
131 
132  SockList_Init(&sl);
133  SockList_AddString(&sl, "music ");
134  SockList_AddString(&sl, music == NULL ? "NONE" : music);
135  Send_With_Handling(&pl->socket, &sl);
136  SockList_Term(&sl);
137 }
138 
139 static char const* pick_bg_music(mapstruct map[static 1]) {
140  if (map->background_music != NULL) {
141  return map->background_music;
142  }
143  return get_name_of_region_for_map(map);
144 }
145 
146 void player_update_bg_music(object player[static 1]) {
147  assert(player->contr);
148  assert(player->type == PLAYER);
149  send_background_music(player->contr, pick_bg_music(player->map));
150 }
151 
Definition: player.h:92
const char * race
Definition: object.h:318
void SockList_Init(SockList *sl)
Definition: lowlevel.c:48
void play_sound_player_only(player *pl, int8_t sound_type, object *emitter, int dir, const char *action)
Definition: sounds.c:51
#define SND_EFFECTS
Definition: sounds.h:12
#define SND_MUTE
Definition: sounds.h:14
void send_background_music(player *pl, const char *music)
Definition: sounds.c:126
socket_struct socket
Definition: player.h:94
int isqrt(int n)
Definition: utils.c:586
int8_t sounds_this_tick
Definition: newserver.h:121
#define MAX_SOUND_DISTANCE
Definition: sounds.c:35
static char const * pick_bg_music(mapstruct map[static 1])
Definition: sounds.c:139
int16_t y
Definition: object.h:326
void play_sound_map(int8_t sound_type, object *emitter, int dir, const char *action)
Definition: sounds.c:98
uint32_t sound
Definition: newserver.h:111
struct mapdef * map
Definition: object.h:297
void SockList_Term(SockList *sl)
Definition: lowlevel.c:58
void player_update_bg_music(object player[static 1])
Definition: sounds.c:146
const char * name
Definition: object.h:311
struct obj * env
Definition: object.h:293
void SockList_AddString(SockList *sl, const char *data)
Definition: lowlevel.c:149
struct pl * contr
Definition: object.h:276
int16_t x
Definition: object.h:326
void SockList_AddChar(SockList *sl, unsigned char c)
Definition: lowlevel.c:98
object * ob
Definition: player.h:158
const char * sstring
Definition: global.h:40
Definition: object.h:107
signed char int8_t
Definition: win32.h:158
#define POW2(x)
Definition: sounds.c:85
uint8_t type
Definition: object.h:338
void SockList_AddLen8Data(SockList *sl, const void *data, size_t len)
Definition: lowlevel.c:171
const char * get_name_of_region_for_map(const mapstruct *m)
Definition: region.c:92
EXTERN player * first_player
Definition: global.h:117
struct pl * next
Definition: player.h:93
#define MAX_SOUNDS_TICK
Definition: sounds.h:16
#define SND_MUSIC
Definition: sounds.h:13
Definition: map.h:325
char * background_music
Definition: map.h:367
void Send_With_Handling(socket_struct *ns, SockList *sl)
Definition: lowlevel.c:434