Crossfire Server, Trunk  R20513
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  if ((RANDOM()%100) >= emitter->sound_chance)
67  return;
68 
69  pl->socket.sounds_this_tick = 0;
70 
71  name = emitter->type == PLAYER ? emitter->race : emitter->name;
72  if (!source)
73  source = emitter;
74 
75  SockList_Init(&sl);
76  SockList_AddString(&sl, "sound2 ");
77  SockList_AddChar(&sl, (int8_t)(source->x-pl->ob->x));
78  SockList_AddChar(&sl, (int8_t)(source->y-pl->ob->y));
79  SockList_AddChar(&sl, dir);
80  SockList_AddChar(&sl, volume);
81  SockList_AddChar(&sl, sound_type);
82  SockList_AddLen8Data(&sl, action, strlen(action));
83  SockList_AddLen8Data(&sl, name, strlen(name));
84  Send_With_Handling(&pl->socket, &sl);
85  SockList_Term(&sl);
86 }
87 
88 #define POW2(x) ((x)*(x))
89 
101 void play_sound_map(int8_t sound_type, object *emitter, int dir, const char *action) {
102  player *pl;
103  object *source;
104 
105  if ((RANDOM()%100) >= emitter->sound_chance)
106  return;
107 
108  if (!emitter->map && !(emitter->env && emitter->env->map))
109  return;
110 
111  source = emitter->map ? emitter : emitter->env;
112 
113  for (pl = first_player; pl; pl = pl->next) {
114  if (pl->ob->map == emitter->map) {
115  int distance = isqrt(POW2(pl->ob->x-source->x)+POW2(pl->ob->y-source->y));
116 
117  if (distance <= MAX_SOUND_DISTANCE) {
118  play_sound_player_only(pl, sound_type, emitter, dir, action);
119  }
120  }
121  }
122 }
123 
132 void send_background_music(player *pl, const char *music) {
133  SockList sl;
134 
135  if (pl->socket.sound&SND_MUTE || !(pl->socket.sound&SND_MUSIC))
136  return;
137 
138  SockList_Init(&sl);
139  SockList_AddString(&sl, "music ");
140  SockList_AddString(&sl, music == NULL ? "NONE" : music);
141  Send_With_Handling(&pl->socket, &sl);
142  SockList_Term(&sl);
143 }
144 
145 static char const* pick_bg_music(mapstruct map[static 1]) {
146  if (map->background_music != NULL) {
147  return map->background_music;
148  }
149  return get_name_of_region_for_map(map);
150 }
151 
152 void player_update_bg_music(object player[static 1]) {
153  assert(player->contr);
154  assert(player->type == PLAYER);
155  send_background_music(player->contr, pick_bg_music(player->map));
156 }
157 
One player.
Definition: player.h:92
Sound-related defines.
const char * race
Human, goblin, dragon, etc.
Definition: object.h:318
void SockList_Init(SockList *sl)
Initializes the SockList instance.
Definition: lowlevel.c:48
void play_sound_player_only(player *pl, int8_t sound_type, object *emitter, int dir, const char *action)
Plays a sound for specified player only.
Definition: sounds.c:51
#define SND_EFFECTS
Those flags are for the &#39;socket.sound&#39; field.
Definition: sounds.h:12
#define SND_MUTE
Don&#39;t sent anything for now.
Definition: sounds.h:14
void send_background_music(player *pl, const char *music)
Sends background music to client.
Definition: sounds.c:132
socket_struct socket
Socket information for this player.
Definition: player.h:94
int isqrt(int n)
Compute the square root.
Definition: utils.c:585
int8_t sounds_this_tick
Number of sounds sent this tick.
Definition: newserver.h:133
Global type definitions and header inclusions.
#define MAX_SOUND_DISTANCE
Maximum distance a player may hear a sound from.
Definition: sounds.c:35
static char const * pick_bg_music(mapstruct map[static 1])
Definition: sounds.c:145
int16_t y
Position in the map for this object.
Definition: object.h:326
void play_sound_map(int8_t sound_type, object *emitter, int dir, const char *action)
Plays a sound on a map.
Definition: sounds.c:101
uint32_t sound
Client sound mode.
Definition: newserver.h:123
struct mapdef * map
Pointer to the map in which this object is present.
Definition: object.h:297
void SockList_Term(SockList *sl)
Frees all resources allocated by a SockList instance.
Definition: lowlevel.c:58
uint8_t sound_chance
Probability, 0 to 100, of the object emitting a sound.
Definition: object.h:393
void player_update_bg_music(object player[static 1])
Definition: sounds.c:152
const char * name
The name of the object, obviously...
Definition: object.h:311
struct obj * env
Pointer to the object which is the environment.
Definition: object.h:293
void SockList_AddString(SockList *sl, const char *data)
Adds a string without length.
Definition: lowlevel.c:149
struct pl * contr
Pointer to the player which control this object.
Definition: object.h:276
int16_t x
Definition: object.h:326
void SockList_AddChar(SockList *sl, char c)
Adds an 8 bit value.
Definition: lowlevel.c:98
object * ob
The object representing the player.
Definition: player.h:158
const char * sstring
Strings that should be manipulated through add_string() and free_string().
Definition: global.h:40
See Player.
Definition: object.h:107
#define RANDOM()
Definition: define.h:679
signed char int8_t
Type definitions for fixed-size integer types.
Definition: win32.h:158
#define POW2(x)
Definition: sounds.c:88
uint8_t type
PLAYER, BULLET, etc.
Definition: object.h:338
void SockList_AddLen8Data(SockList *sl, const void *data, size_t len)
Adds a data block prepended with an 8 bit length field.
Definition: lowlevel.c:171
const char * get_name_of_region_for_map(const mapstruct *m)
Gets the name of a region for a map.
Definition: region.c:92
EXTERN player * first_player
First player.
Definition: global.h:117
struct pl * next
Pointer to next player, NULL if this is last.
Definition: player.h:93
#define MAX_SOUNDS_TICK
Maximum number of sounds a player can receive for each tick.
Definition: sounds.h:16
#define SND_MUSIC
Client wants background music info.
Definition: sounds.h:13
Contains the base information we use to make up a packet we want to send.
Definition: newclient.h:680
This is a game-map.
Definition: map.h:325
char * background_music
Background music to use for this map.
Definition: map.h:367
void Send_With_Handling(socket_struct *ns, SockList *sl)
Calls Write_To_Socket to send data to the client.
Definition: lowlevel.c:542