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->type == PLAYER ? emitter->race : emitter->name;
78  if (name == NULL) {
79  return;
80  }
81 
82  SockList_Init(&sl);
83  SockList_AddString(&sl, "sound2 ");
84  SockList_AddChar(&sl, (int8_t)(source->x-pl->ob->x));
85  SockList_AddChar(&sl, (int8_t)(source->y-pl->ob->y));
86  SockList_AddChar(&sl, dir);
87  SockList_AddChar(&sl, volume);
88  SockList_AddChar(&sl, sound_type);
89  SockList_AddLen8Data(&sl, action, strlen(action));
90  SockList_AddLen8Data(&sl, name, strlen(name));
92  SockList_Term(&sl);
93 }
94 
95 #define POW2(x) ((x)*(x))
96 
108 void play_sound_map(int8_t sound_type, object *emitter, int dir, const char *action) {
109  player *pl;
110  object *source;
111 
112  if (!emitter->map && !(emitter->env && emitter->env->map))
113  return;
114 
115  source = emitter->map ? emitter : emitter->env;
116 
117  for (pl = first_player; pl; pl = pl->next) {
118  if (pl->ob->map == emitter->map) {
119  int distance = isqrt(POW2(pl->ob->x-source->x)+POW2(pl->ob->y-source->y));
120 
121  if (distance <= MAX_SOUND_DISTANCE) {
122  play_sound_player_only(pl, sound_type, emitter, dir, action);
123  }
124  }
125  }
126 }
127 
136 void send_background_music(player *pl, const char *music) {
137  SockList sl;
138 
140  return;
141 
142  SockList_Init(&sl);
143  SockList_AddString(&sl, "music ");
144  SockList_AddString(&sl, music == NULL ? "NONE" : music);
145  Send_With_Handling(&pl->socket, &sl);
146  SockList_Term(&sl);
147 }
148 
149 static char const* pick_bg_music(mapstruct map[static 1]) {
150  if (map->background_music != NULL) {
151  return map->background_music;
152  }
154 }
155 
156 void player_update_bg_music(object player[static 1]) {
157  assert(player->contr);
158  assert(player->type == PLAYER);
160 }
161 
PLAYER
@ PLAYER
Definition: object.h:107
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:298
obj::race
sstring race
Definition: object.h:319
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:156
SockList_AddString
void SockList_AddString(SockList *sl, const char *data)
Definition: lowlevel.c:154
pl::socket
socket_struct socket
Definition: player.h:94
pl
Definition: player.h:92
get_name_of_region_for_map
const char * get_name_of_region_for_map(const mapstruct *m)
Definition: region.c:92
MIN
#define MIN(x, y)
Definition: compat.h:21
pl::ob
object * ob
Definition: player.h:162
POW2
#define POW2(x)
Definition: sounds.c:95
isqrt
int isqrt(int n)
Definition: utils.c:586
SND_EFFECTS
#define SND_EFFECTS
Definition: sounds.h:12
pl::next
struct pl * next
Definition: player.h:93
disinfect.map
map
Definition: disinfect.py:4
obj::name
sstring name
Definition: object.h:312
send_background_music
void send_background_music(player *pl, const char *music)
Definition: sounds.c:136
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:328
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:294
sproto.h
SND_MUTE
#define SND_MUTE
Definition: sounds.h:14
mapdef
Definition: map.h:324
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:149
SockList_Term
void SockList_Term(SockList *sl)
Definition: lowlevel.c:62
obj::y
int16_t y
Definition: object.h:328
socket_struct::sounds_this_tick
int8_t sounds_this_tick
Definition: newserver.h:121
sounds.h
obj::type
uint8_t type
Definition: object.h:341
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:108
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