Atrinik Server  4.0
sounds.c
Go to the documentation of this file.
1 /*************************************************************************
2  * Atrinik, a Multiplayer Online Role Playing Game *
3  * *
4  * Copyright (C) 2009-2014 Alex Tokar and Atrinik Development Team *
5  * *
6  * Fork from Crossfire (Multiplayer game for X-windows). *
7  * *
8  * This program is free software; you can redistribute it and/or modify *
9  * it under the terms of the GNU General Public License as published by *
10  * the Free Software Foundation; either version 2 of the License, or *
11  * (at your option) any later version. *
12  * *
13  * This program is distributed in the hope that it will be useful, *
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16  * GNU General Public License for more details. *
17  * *
18  * You should have received a copy of the GNU General Public License *
19  * along with this program; if not, write to the Free Software *
20  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
21  * *
22  * The author can be reached at admin@atrinik.org *
23  ************************************************************************/
24 
30 #include <global.h>
31 #include <toolkit/packet.h>
32 #include <player.h>
33 #include <object.h>
34 
41 #define MAX_SOUND_DISTANCE 12
42 
43 #define MAX_SOUND_DISTANCE_SQUARED POW2(MAX_SOUND_DISTANCE)
44 
47 #define MAP_LEVEL_VOLUME_ADJUST 10
48 
66 void play_sound_player_only(player *pl, int type, const char *filename, int x, int y, int loop, int volume)
67 {
68  packet_struct *packet;
69 
70  /* Player has disabled sound */
71  if (!pl->cs->sound) {
72  return;
73  }
74 
75  packet = packet_new(CLIENT_CMD_SOUND, 64, 64);
76  packet_debug_data(packet, 0, "Sound command type");
77  packet_append_uint8(packet, type);
78  packet_debug_data(packet, 0, "Filename");
79  packet_append_string_terminated(packet, filename);
80  packet_debug_data(packet, 0, "Loop");
81  packet_append_int8(packet, loop);
82  packet_debug_data(packet, 0, "Volume");
83  packet_append_int8(packet, volume);
84 
85  /* Add X/Y offset for sound effects. */
86  if (type == CMD_SOUND_EFFECT) {
87  packet_debug_data(packet, 0, "X coordinate");
88  packet_append_uint8(packet, x);
89  packet_debug_data(packet, 0, "Y coordinate");
90  packet_append_uint8(packet, y);
91  }
92 
93  socket_send_packet(pl->cs, packet);
94 }
95 
118  mapstruct *orig, int type, const char *filename, int x, int y, int loop,
119  int volume)
120 {
121  object *pl;
122  rv_vector rv;
123  int volume_adj;
124 
125  volume_adj = abs(orig->coords[2] - map->coords[2]) *
127 
128  for (pl = tiled->player_first; pl != NULL; pl = CONTR(pl)->map_above) {
129  if (get_rangevector_from_mapcoords(map, x, y, pl->map, pl->x, pl->y,
130  &rv, RV_NO_DISTANCE) && POW2(rv.distance_x) +
132  play_sound_player_only(CONTR(pl), type, filename, rv.distance_x,
133  rv.distance_y, loop, volume - volume_adj);
134  }
135  }
136 
137  return 0;
138 }
139 
159 void play_sound_map(mapstruct *map, int type, const char *filename, int x, int y, int loop, int volume)
160 {
161  if (map == NULL) {
162  return;
163  }
164 
165  MAP_TILES_WALK_START(map, play_sound_map_internal, map, type, filename, x,
166  y, loop, volume)
167  {
168  }
170 }
#define RV_NO_DISTANCE
Definition: map.h:813
uint8_t type
One of operation types.
Definition: sound_ambient.c:45
int distance_x
Definition: map.h:778
#define MAP_TILES_WALK_START(_m, _fnc,...)
Definition: map.h:733
int distance_y
Definition: map.h:781
int16_t y
Definition: object.h:276
uint32_t sound
Definition: newserver.h:143
struct mapdef * map
Definition: object.h:139
void play_sound_player_only(player *pl, int type, const char *filename, int x, int y, int loop, int volume)
Definition: sounds.c:66
int16_t x
Definition: object.h:273
void play_sound_map(mapstruct *map, int type, const char *filename, int x, int y, int loop, int volume)
Definition: sounds.c:159
#define MAP_LEVEL_VOLUME_ADJUST
Definition: sounds.c:47
#define MAX_SOUND_DISTANCE_SQUARED
Definition: sounds.c:43
int get_rangevector_from_mapcoords(mapstruct *map1, int x, int y, mapstruct *map2, int x2, int y2, rv_vector *retval, int flags)
Definition: map.c:2297
socket_struct * cs
Definition: player.h:148
object * player_first
Definition: map.h:594
int16_t coords[3]
X, Y and Z coordinates.
Definition: map.h:678
#define MAP_TILES_WALK_END
Definition: map.h:766
Definition: map.h:536
static int play_sound_map_internal(mapstruct *tiled, mapstruct *map, mapstruct *orig, int type, const char *filename, int x, int y, int loop, int volume)
Definition: sounds.c:117