Crossfire Server, Branches 1.12  R18729
sounds.c
Go to the documentation of this file.
1 /*
2  * static char *rcsid_sound_c =
3  * "$Id: sounds.c 11578 2009-02-23 22:02:27Z lalo $";
4  */
5 
6 /* Send bug reports to Raphael Quinet (quinet@montefiore.ulg.ac.be) */
7 
15 #include <global.h>
16 #include <sproto.h>
17 #include <sounds.h>
18 
24 #define MAX_SOUND_DISTANCE 10
25 
40 void play_sound_player_only(player *pl, sint8 sound_type, object *emitter, int dir, const char *action) {
41  SockList sl;
42  int volume = 50;
43  sstring name;
44  object *source;
45 
46  if (pl->socket.sound&SND_MUTE || !(pl->socket.sound&SND_EFFECTS))
47  return;
49  return;
50  if (!emitter->map && !(emitter->env && emitter->env->map))
51  return;
52 
53  source = emitter->map ? emitter : emitter->env;
54 
55  if ((RANDOM()%100) >= emitter->sound_chance)
56  return;
57 
58  pl->socket.sounds_this_tick = 0;
59 
60  name = emitter->type == PLAYER ? emitter->race : emitter->name;
61  if (!source)
62  source = emitter;
63 
64  SockList_Init(&sl);
65  SockList_AddString(&sl, "sound2 ");
66  SockList_AddChar(&sl, (sint8)(source->x-pl->ob->x));
67  SockList_AddChar(&sl, (sint8)(source->y-pl->ob->y));
68  SockList_AddChar(&sl, dir);
69  SockList_AddChar(&sl, volume);
70  SockList_AddChar(&sl, sound_type);
71  SockList_AddLen8Data(&sl, action, strlen(action));
72  SockList_AddLen8Data(&sl, name, strlen(name));
73  Send_With_Handling(&pl->socket, &sl);
74  SockList_Term(&sl);
75 }
76 
77 #define POW2(x) ((x)*(x))
78 
90 void play_sound_map(sint8 sound_type, object *emitter, int dir, const char *action) {
91  player *pl;
92  object *source;
93 
94  if ((RANDOM()%100) >= emitter->sound_chance)
95  return;
96 
97  if (!emitter->map && !(emitter->env && emitter->env->map))
98  return;
99 
100  source = emitter->map ? emitter : emitter->env;
101 
102  for (pl = first_player; pl; pl = pl->next) {
103  if (pl->ob->map == emitter->map) {
104  int distance = isqrt(POW2(pl->ob->x-source->x)+POW2(pl->ob->y-source->y));
105 
106  if (distance <= MAX_SOUND_DISTANCE) {
107  play_sound_player_only(pl, sound_type, emitter, dir, action);
108  }
109  }
110  }
111 }
112 
121 void send_background_music(player *pl, const char *music) {
122  SockList sl;
123 
124  if (pl->socket.sound&SND_MUTE || !(pl->socket.sound&SND_MUSIC))
125  return;
126 
127  SockList_Init(&sl);
128  SockList_AddString(&sl, "music ");
129  SockList_AddString(&sl, music == NULL ? "NONE" : music);
130  Send_With_Handling(&pl->socket, &sl);
131  SockList_Term(&sl);
132 }
signed char sint8
Definition: global.h:80
Definition: player.h:146
const char * race
Definition: object.h:171
void SockList_Init(SockList *sl)
Definition: lowlevel.c:67
#define SND_EFFECTS
Definition: sounds.h:46
#define SND_MUTE
Definition: sounds.h:48
void send_background_music(player *pl, const char *music)
Definition: sounds.c:121
socket_struct socket
Definition: player.h:148
sint16 x
Definition: object.h:179
int distance(const object *ob1, const object *ob2)
Definition: object.c:3364
sint8 sounds_this_tick
Definition: newserver.h:148
#define MAX_SOUND_DISTANCE
Definition: sounds.c:24
#define PLAYER
Definition: define.h:113
uint32 sound
Definition: newserver.h:139
void play_sound_map(sint8 sound_type, object *emitter, int dir, const char *action)
Definition: sounds.c:90
struct mapdef * map
Definition: object.h:155
void SockList_Term(SockList *sl)
Definition: lowlevel.c:77
const char * name
Definition: object.h:167
struct obj * env
Definition: object.h:151
void SockList_AddString(SockList *sl, const char *data)
Definition: lowlevel.c:154
sint16 y
Definition: object.h:179
void SockList_AddChar(SockList *sl, char c)
Definition: lowlevel.c:103
object * ob
Definition: player.h:207
const char * sstring
Definition: global.h:84
void play_sound_player_only(player *pl, sint8 sound_type, object *emitter, int dir, const char *action)
Definition: sounds.c:40
#define POW2(x)
Definition: sounds.c:77
int isqrt(int n)
Definition: porting.c:573
void SockList_AddLen8Data(SockList *sl, const void *data, size_t len)
Definition: lowlevel.c:176
sint8 sound_chance
Definition: object.h:244
EXTERN player * first_player
Definition: global.h:190
struct pl * next
Definition: player.h:147
#define MAX_SOUNDS_TICK
Definition: sounds.h:50
#define SND_MUSIC
Definition: sounds.h:47
uint8 type
Definition: object.h:189
void Send_With_Handling(socket_struct *ns, SockList *sl)
Definition: lowlevel.c:541