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