Crossfire Server, Trunk
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 
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  // Approximate the distance to the emitter from the source
67  int dx = FABS(source->x-pl->ob->x),
68  dy = FABS(source->y-pl->ob->y);
69  int distance = (MIN(dx, dy) * 3 + FABS(dx-dy) * 2) / 2;
70  // Make the sound dissipation more gradual.
71  distance >>= 1;
72  // Downscale the volume by distance
73  volume = distance ? volume / distance : volume;
74 
76 
77  name = emitter->name;
78  if (emitter->type == PLAYER) {
79  name = emitter->race;
80  } else if (emitter->type == EXIT) {
81  name = emitter->arch->clone.name; /* The name may be custom, so use generic exit name */
82  }
83  if (name == NULL) {
84  return;
85  }
86 
87  SockList_Init(&sl);
88  SockList_AddString(&sl, "sound2 ");
89  SockList_AddChar(&sl, (int8_t)(source->x-pl->ob->x));
90  SockList_AddChar(&sl, (int8_t)(source->y-pl->ob->y));
91  SockList_AddChar(&sl, dir);
92  SockList_AddChar(&sl, volume);
93  SockList_AddChar(&sl, sound_type);
94  SockList_AddLen8Data(&sl, action, strlen(action));
95  SockList_AddLen8Data(&sl, name, strlen(name));
97  SockList_Term(&sl);
98 }
99 
100 #define POW2(x) ((x)*(x))
101 
113 void play_sound_map(int8_t sound_type, object *emitter, int dir, const char *action) {
114  player *pl;
115  object *source;
116 
117  if (!emitter->map && !(emitter->env && emitter->env->map))
118  return;
119 
120  source = emitter->map ? emitter : emitter->env;
121 
122  for (pl = first_player; pl; pl = pl->next) {
123  if (pl->ob->map == emitter->map) {
124  int distance = isqrt(POW2(pl->ob->x-source->x)+POW2(pl->ob->y-source->y));
125 
126  if (distance <= MAX_SOUND_DISTANCE) {
127  play_sound_player_only(pl, sound_type, emitter, dir, action);
128  }
129  }
130  }
131 }
132 
141 void send_background_music(player *pl, const char *music) {
142  SockList sl;
143 
145  return;
146 
147  SockList_Init(&sl);
148  SockList_AddString(&sl, "music ");
149  SockList_AddString(&sl, music == NULL ? "NONE" : music);
150  Send_With_Handling(&pl->socket, &sl);
151  SockList_Term(&sl);
152 }
153 
154 static char const* pick_bg_music(mapstruct map[static 1]) {
155  if (map->background_music != NULL) {
156  return map->background_music;
157  }
159 }
160 
161 void player_update_bg_music(object player[static 1]) {
162  assert(player->contr);
163  assert(player->type == PLAYER);
165 }
166 
PLAYER
@ PLAYER
Definition: object.h:107
get_name_of_region_for_map
const char * get_name_of_region_for_map(const mapstruct *m)
Definition: region.cpp:94
global.h
MAX_SOUND_DISTANCE
#define MAX_SOUND_DISTANCE
Definition: sounds.c:35
FABS
#define FABS(x)
Definition: define.h:22
obj::map
struct mapdef * map
Definition: object.h:300
obj::race
sstring race
Definition: object.h:321
socket_struct::sound
uint32_t sound
Definition: newserver.h:111
player_update_bg_music
void player_update_bg_music(object player[static 1])
Definition: sounds.c:161
SockList_AddString
void SockList_AddString(SockList *sl, const char *data)
Definition: lowlevel.c:154
pl::socket
socket_struct socket
Definition: player.h:107
pl
Definition: player.h:105
MIN
#define MIN(x, y)
Definition: compat.h:21
pl::ob
object * ob
Definition: player.h:176
POW2
#define POW2(x)
Definition: sounds.c:100
isqrt
int isqrt(int n)
Definition: utils.c:569
SND_EFFECTS
#define SND_EFFECTS
Definition: sounds.h:12
pl::next
struct pl * next
Definition: player.h:106
disinfect.map
map
Definition: disinfect.py:4
obj::name
sstring name
Definition: object.h:314
send_background_music
void send_background_music(player *pl, const char *music)
Definition: sounds.c:141
SockList_AddChar
void SockList_AddChar(SockList *sl, unsigned char c)
Definition: lowlevel.c:103
convert.action
action
Definition: convert.py:25
first_player
EXTERN player * first_player
Definition: global.h:115
obj::x
int16_t x
Definition: object.h:330
MAX_SOUNDS_TICK
#define MAX_SOUNDS_TICK
Definition: sounds.h:16
sstring
const typedef char * sstring
Definition: global.h:40
obj::env
struct obj * env
Definition: object.h:296
sproto.h
SND_MUTE
#define SND_MUTE
Definition: sounds.h:14
mapdef
Definition: map.h:317
SockList_Init
void SockList_Init(SockList *sl)
Definition: lowlevel.c:52
play_sound_player_only
void play_sound_player_only(player *pl, int8_t sound_type, object *emitter, int dir, const char *action)
Definition: sounds.c:51
pick_bg_music
static char const * pick_bg_music(mapstruct map[static 1])
Definition: sounds.c:154
SockList_Term
void SockList_Term(SockList *sl)
Definition: lowlevel.c:62
EXIT
@ EXIT
Definition: object.h:181
obj::y
int16_t y
Definition: object.h:330
socket_struct::sounds_this_tick
int8_t sounds_this_tick
Definition: newserver.h:121
obj::arch
struct archt * arch
Definition: object.h:417
sounds.h
obj::type
uint8_t type
Definition: object.h:343
archt::clone
object clone
Definition: object.h:473
SND_MUSIC
#define SND_MUSIC
Definition: sounds.h:13
SockList_AddLen8Data
void SockList_AddLen8Data(SockList *sl, const void *data, size_t len)
Definition: lowlevel.c:176
play_sound_map
void play_sound_map(int8_t sound_type, object *emitter, int dir, const char *action)
Definition: sounds.c:113
altar_valkyrie.pl
pl
Definition: altar_valkyrie.py:28
Send_With_Handling
void Send_With_Handling(socket_struct *ns, SockList *sl)
Definition: lowlevel.c:440
SockList
Definition: newclient.h:681
give.name
name
Definition: give.py:27