| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 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 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
#define MAX_SOUND_DISTANCE 10 |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 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; |
| 59 |
if (pl->socket.sounds_this_tick >= MAX_SOUNDS_TICK) |
| 60 |
return; |
| 61 |
if (!emitter->map && !(emitter->env && emitter->env->map)) |
| 62 |
return; |
| 63 |
|
| 64 |
source = emitter->map ? emitter : emitter->env; |
| 65 |
|
| 66 |
|
| 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 |
|
| 71 |
distance >>= 1; |
| 72 |
|
| 73 |
volume = distance ? volume / distance : volume; |
| 74 |
|
| 75 |
pl->socket.sounds_this_tick = 0; |
| 76 |
|
| 77 |
name = emitter->type == PLAYER ? emitter->race : emitter->name; |
| 78 |
if (!source) |
| 79 |
source = emitter; |
| 80 |
|
| 81 |
SockList_Init(&sl); |
| 82 |
SockList_AddString(&sl, "sound2 "); |
| 83 |
SockList_AddChar(&sl, (int8_t)(source->x-pl->ob->x)); |
| 84 |
SockList_AddChar(&sl, (int8_t)(source->y-pl->ob->y)); |
| 85 |
SockList_AddChar(&sl, dir); |
| 86 |
SockList_AddChar(&sl, volume); |
| 87 |
SockList_AddChar(&sl, sound_type); |
| 88 |
SockList_AddLen8Data(&sl, action, strlen(action)); |
| 89 |
SockList_AddLen8Data(&sl, name, strlen(name)); |
| 90 |
Send_With_Handling(&pl->socket, &sl); |
| 91 |
SockList_Term(&sl); |
| 92 |
} |
| 93 |
|
| 94 |
#define POW2(x) ((x)*(x)) |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
void play_sound_map(int8_t sound_type, object *emitter, int dir, const char *action) { |
| 108 |
player *pl; |
| 109 |
object *source; |
| 110 |
|
| 111 |
if (!emitter->map && !(emitter->env && emitter->env->map)) |
| 112 |
return; |
| 113 |
|
| 114 |
source = emitter->map ? emitter : emitter->env; |
| 115 |
|
| 116 |
for (pl = first_player; pl; pl = pl->next) { |
| 117 |
if (pl->ob->map == emitter->map) { |
| 118 |
int distance = isqrt(POW2(pl->ob->x-source->x)+POW2(pl->ob->y-source->y)); |
| 119 |
|
| 120 |
if (distance <= MAX_SOUND_DISTANCE) { |
| 121 |
play_sound_player_only(pl, sound_type, emitter, dir, action); |
| 122 |
} |
| 123 |
} |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
void send_background_music(player *pl, const char *music) { |
| 136 |
SockList sl; |
| 137 |
|
| 138 |
if (pl->socket.sound&SND_MUTE || !(pl->socket.sound&SND_MUSIC)) |
| 139 |
return; |
| 140 |
|
| 141 |
SockList_Init(&sl); |
| 142 |
SockList_AddString(&sl, "music "); |
| 143 |
SockList_AddString(&sl, music == NULL ? "NONE" : music); |
| 144 |
Send_With_Handling(&pl->socket, &sl); |
| 145 |
SockList_Term(&sl); |
| 146 |
} |
| 147 |
|
| 148 |
static char const* pick_bg_music(mapstruct map[static 1]) { |
| 149 |
if (map->background_music != NULL) { |
| 150 |
return map->background_music; |
| 151 |
} |
| 152 |
return get_name_of_region_for_map(map); |
| 153 |
} |
| 154 |
|
| 155 |
void player_update_bg_music(object player[static 1]) { |
| 156 |
assert(player->contr); |
| 157 |
assert(player->type == PLAYER); |
| 158 |
send_background_music(player->contr, pick_bg_music(player->map)); |
| 159 |
} |
| 160 |
|