| | 1 | #include "system.h" |
| | 2 | #include "prototyp.h" |
| | 3 | #include "stratdef.h" |
| | 4 | #include "bh_snds.h" |
| | 5 | #include "psndplat.h" |
| | 6 | #include "bh_types.h" |
| | 7 | #include <assert.h> |
| | 8 | #include <stdlib.h> |
| | 9 | #include <string.h> |
| | 10 | |
| | 11 | void SoundBehaveInit(void* bhdata, STRATEGYBLOCK* sbptr) |
| | 12 | { |
| | 13 | SOUND_TOOLS_TEMPLATE * stt = (SOUND_TOOLS_TEMPLATE *)bhdata; |
| | 14 | |
| | 15 | assert (sbptr && stt); |
| | 16 | |
| | 17 | SOUND_BEHAV_BLOCK * sbb = malloc (sizeof (SOUND_BEHAV_BLOCK)); |
| | 18 | |
| | 19 | if(NULL != sbb) |
| | 20 | { |
| | 21 | sbptr->dataptr = sbb; |
| | 22 | sbb->sound_number = stt->sound_number; |
| | 23 | sbb->position = stt->position; |
| | 24 | sbb->inner_range = stt->inner_range; |
| | 25 | sbb->outer_range = stt->outer_range; |
| | 26 | sbb->max_volume = stt->max_volume; |
| | 27 | sbb->pitch = stt->pitch; |
| | 28 | |
| | 29 | sbb->sound_not_started = 1; |
| | 30 | |
| | 31 | sbb->wav_name = malloc (strlen (stt->sound_name) + 1); |
| | 32 | if(NULL == sbb->wav_name) |
| | 33 | { |
| | 34 | RemoveBehaviourStrategy(sbptr); |
| | 35 | return; |
| | 36 | } |
| | 37 | |
| | 38 | strcpy (sbb->wav_name, stt->sound_name); |
| | 39 | sbb->playing=stt->playing; |
| | 40 | sbb->loop = stt->loop; |
| | 41 | sbb->activ_no = SOUND_NOACTIVEINDEX; |
| | 42 | } |
| | 43 | else |
| | 44 | { |
| | 45 | RemoveBehaviourStrategy(sbptr); |
| | 46 | } |
| | 47 | } |
| | 48 | |
| | 49 | void SoundBehaveFun (STRATEGYBLOCK * sbptr) |
| | 50 | { |
| | 51 | SOUND_BEHAV_BLOCK * sbb = (SOUND_BEHAV_BLOCK*)sbptr->dataptr; |
| | 52 | |
| | 53 | if(SID_NOSOUND == sbb->sound_number) |
| | 54 | return; |
| | 55 | |
| | 56 | int dist = VectorDistance(&PlayerStatus.DisplayBlock->ObWorld, &sbb->position); |
| | 57 | |
| | 58 | if (sbb->sound_not_started && PlayerStatus.DisplayBlock) |
| | 59 | { |
| | 60 | SOUND3DDATA s3d; |
| | 61 | |
| | 62 | if(sbb->playing) |
| | 63 | { |
| | 64 | //start playing the sound , but only if it is close enough |
| | 65 | |
| | 66 | if(dist <= sbb->outer_range) |
| | 67 | { |
| | 68 | s3d.position = sbb->position; |
| | 69 | s3d.inner_range = sbb->inner_range; |
| | 70 | s3d.outer_range = sbb->outer_range; |
| | 71 | s3d.velocity.vx = s3d.velocity.vy = s3d.velocity.vz = 0; |
| | 72 | |
| | 73 | Sound_Play ((SOUNDINDEX)sbb->sound_number, (sbb->loop ? "nelh" : "neh"), &s3d, &sbb->activ_no); |
| | 74 | |
| | 75 | if (sbb->activ_no != SOUND_NOACTIVEINDEX) |
| | 76 | { |
| | 77 | Sound_ChangeVolume (sbb->activ_no, sbb->max_volume); |
| | 78 | Sound_ChangePitch (sbb->activ_no, sbb->pitch); |
| | 79 | } |
| | 80 | } |
| | 81 | } |
| | 82 | |
| | 83 | sbb->sound_not_started = 0; |
| | 84 | } |
| | 85 | |
| | 86 | /* KJL 14:15:39 12/8/97 - kill any objects that are too far away */ |
| | 87 | if(sbb->playing) |
| | 88 | { |
| | 89 | if(dist > sbb->outer_range) |
| | 90 | { |
| | 91 | //stop sound if it is playing |
| | 92 | |
| | 93 | if (sbb->activ_no != SOUND_NOACTIVEINDEX) |
| | 94 | Sound_Stop (sbb->activ_no); |
| | 95 | |
| | 96 | //not much point in restarting a non-looping sound, since it won't restart at the right place |
| | 97 | if(!sbb->loop && sbb->playing) |
| | 98 | sbb->playing = 0; |
| | 99 | |
| | 100 | return; |
| | 101 | } |
| | 102 | } |
| | 103 | |
| | 104 | if (sbb->activ_no == SOUND_NOACTIVEINDEX && (SID_NOSOUND != sbb->sound_number) && sbb->playing) |
| | 105 | { |
| | 106 | if(sbb->loop) |
| | 107 | { |
| | 108 | SOUND3DDATA s3d; |
| | 109 | |
| | 110 | s3d.position = sbb->position; |
| | 111 | s3d.inner_range = sbb->inner_range; |
| | 112 | s3d.outer_range = sbb->outer_range; |
| | 113 | s3d.velocity.vx = s3d.velocity.vy = s3d.velocity.vz = 0; |
| | 114 | |
| | 115 | Sound_Play((SOUNDINDEX)sbb->sound_number, (sbb->loop ? "nelh" : "neh"), &s3d, &sbb->activ_no); |
| | 116 | |
| | 117 | if (sbb->activ_no != SOUND_NOACTIVEINDEX) |
| | 118 | { |
| | 119 | Sound_ChangeVolume (sbb->activ_no, sbb->max_volume); |
| | 120 | Sound_ChangePitch (sbb->activ_no, sbb->pitch); |
| | 121 | } |
| | 122 | } |
| | 123 | else //sound has finished playing |
| | 124 | { |
| | 125 | sbb->playing = 0; |
| | 126 | } |
| | 127 | } |
| | 128 | } |
| | 129 | |
| | 130 | void StartPlacedSoundPlaying(STRATEGYBLOCK* sbptr) |
| | 131 | { |
| | 132 | assert(sbptr); |
| | 133 | assert(sbptr->type == I_BehaviourPlacedSound); |
| | 134 | SOUND_BEHAV_BLOCK * sbb = (SOUND_BEHAV_BLOCK*)sbptr->dataptr; |
| | 135 | |
| | 136 | if(SID_NOSOUND == sbb->sound_number) |
| | 137 | return; |
| | 138 | |
| | 139 | if(!sbb->playing) |
| | 140 | { |
| | 141 | //if sound is within range start it |
| | 142 | int dist = VectorDistance(&PlayerStatus.DisplayBlock->ObWorld, &sbb->position); |
| | 143 | |
| | 144 | if(dist < sbb->outer_range) |
| | 145 | { |
| | 146 | SOUND3DDATA s3d; |
| | 147 | |
| | 148 | s3d.position = sbb->position; |
| | 149 | s3d.inner_range = sbb->inner_range; |
| | 150 | s3d.outer_range = sbb->outer_range; |
| | 151 | s3d.velocity.vx = s3d.velocity.vy = s3d.velocity.vz = 0; |
| | 152 | |
| | 153 | Sound_Play ((SOUNDINDEX)sbb->sound_number, (sbb->loop ? "nelh" : "neh"), &s3d, &sbb->activ_no); |
| | 154 | |
| | 155 | if (sbb->activ_no != SOUND_NOACTIVEINDEX) |
| | 156 | { |
| | 157 | Sound_ChangeVolume (sbb->activ_no, sbb->max_volume); |
| | 158 | Sound_ChangePitch (sbb->activ_no, sbb->pitch); |
| | 159 | } |
| | 160 | } |
| | 161 | sbb->playing = 1; |
| | 162 | } |
| | 163 | } |
| | 164 | |
| | 165 | void StopPlacedSoundPlaying(STRATEGYBLOCK* sbptr) |
| | 166 | { |
| | 167 | assert(sbptr); |
| | 168 | assert(sbptr->type == I_BehaviourPlacedSound); |
| | 169 | SOUND_BEHAV_BLOCK * sbb = (SOUND_BEHAV_BLOCK*)sbptr->dataptr; |
| | 170 | |
| | 171 | if(SID_NOSOUND == sbb->sound_number) |
| | 172 | return; |
| | 173 | |
| | 174 | if(sbb->playing) |
| | 175 | { |
| | 176 | sbb->playing = 0; |
| | 177 | |
| | 178 | if (sbb->activ_no != SOUND_NOACTIVEINDEX) |
| | 179 | Sound_Stop (sbb->activ_no); |
| | 180 | } |
| | 181 | } |
| | 182 | |
| | 183 | /*--------------------** |
| | 184 | ** Loading and Saving ** |
| | 185 | **--------------------*/ |
| | 186 | #include "savegame.h" |
| | 187 | typedef struct placed_sound_save_block |
| | 188 | { |
| | 189 | SAVE_BLOCK_STRATEGY_HEADER header; |
| | 190 | |
| | 191 | int playing; |
| | 192 | |
| | 193 | } PLACED_SOUND_SAVE_BLOCK; |
| | 194 | |
| | 195 | //defines for load/save macros |
| | 196 | #define SAVELOAD_BLOCK block |
| | 197 | #define SAVELOAD_BEHAV sbb |
| | 198 | |
| | 199 | void LoadStrategy_PlacedSound(SAVE_BLOCK_STRATEGY_HEADER* header) |
| | 200 | { |
| | 201 | PLACED_SOUND_SAVE_BLOCK* block = (PLACED_SOUND_SAVE_BLOCK*) header; |
| | 202 | |
| | 203 | //check the size of the save block |
| | 204 | if(header->size != sizeof(*block)) |
| | 205 | return; |
| | 206 | |
| | 207 | //find the existing strategy block |
| | 208 | STRATEGYBLOCK* sbPtr = FindSBWithName(header->SBname); |
| | 209 | |
| | 210 | if(!sbPtr) |
| | 211 | return; |
| | 212 | |
| | 213 | //make sure the strategy found is of the right type |
| | 214 | if(sbPtr->type != I_BehaviourPlacedSound) |
| | 215 | return; |
| | 216 | |
| | 217 | SOUND_BEHAV_BLOCK * sbb = (SOUND_BEHAV_BLOCK *)sbPtr->dataptr; |
| | 218 | |
| | 219 | //start copying stuff |
| | 220 | COPYELEMENT_LOAD(playing) |
| | 221 | |
| | 222 | sbb->sound_not_started = 0; |
| | 223 | Load_SoundState(&sbb->activ_no); |
| | 224 | } |
| | 225 | |
| | 226 | void SaveStrategy_PlacedSound(STRATEGYBLOCK* sbPtr) |
| | 227 | { |
| | 228 | PLACED_SOUND_SAVE_BLOCK* block; |
| | 229 | SOUND_BEHAV_BLOCK * sbb = (SOUND_BEHAV_BLOCK *)sbPtr->dataptr; |
| | 230 | |
| | 231 | GET_STRATEGY_SAVE_BLOCK(block,sbPtr); |
| | 232 | |
| | 233 | //start copying stuff |
| | 234 | COPYELEMENT_SAVE(playing) |
| | 235 | |
| | 236 | Save_SoundState(&sbb->activ_no); |
| | 237 | } |