#include "system.h" #include "stratdef.h" #include "bh_snds.h" #include "psndplat.h" #include "bh_types.h" #include #include #include void SoundBehaveInit(void* bhdata, STRATEGYBLOCK* sbptr) { SOUND_TOOLS_TEMPLATE * stt = (SOUND_TOOLS_TEMPLATE *)bhdata; assert (sbptr && stt); SOUND_BEHAV_BLOCK * sbb = malloc (sizeof (SOUND_BEHAV_BLOCK)); if(NULL != sbb) { sbptr->dataptr = sbb; sbb->sound_number = stt->sound_number; sbb->position = stt->position; sbb->inner_range = stt->inner_range; sbb->outer_range = stt->outer_range; sbb->max_volume = stt->max_volume; sbb->pitch = stt->pitch; sbb->sound_not_started = 1; sbb->wav_name = malloc (strlen (stt->sound_name) + 1); if(NULL == sbb->wav_name) { RemoveBehaviourStrategy(sbptr); return; } strcpy (sbb->wav_name, stt->sound_name); sbb->playing=stt->playing; sbb->loop = stt->loop; sbb->activ_no = SOUND_NOACTIVEINDEX; } else { RemoveBehaviourStrategy(sbptr); } } void SoundBehaveFun (STRATEGYBLOCK * sbptr) { SOUND_BEHAV_BLOCK * sbb = (SOUND_BEHAV_BLOCK*)sbptr->dataptr; if(SID_NOSOUND == sbb->sound_number) return; int dist = VectorDistance(&PlayerStatus.DisplayBlock->ObWorld, &sbb->position); if (sbb->sound_not_started && PlayerStatus.DisplayBlock) { SOUND3DDATA s3d; if(sbb->playing) { //start playing the sound , but only if it is close enough if(dist <= sbb->outer_range) { s3d.position = sbb->position; s3d.inner_range = sbb->inner_range; s3d.outer_range = sbb->outer_range; s3d.velocity.vx = s3d.velocity.vy = s3d.velocity.vz = 0; Sound_Play ((SOUNDINDEX)sbb->sound_number, (sbb->loop ? "nelh" : "neh"), &s3d, &sbb->activ_no); if (sbb->activ_no != SOUND_NOACTIVEINDEX) { Sound_ChangeVolume (sbb->activ_no, sbb->max_volume); Sound_ChangePitch (sbb->activ_no, sbb->pitch); } } } sbb->sound_not_started = 0; } /* KJL 14:15:39 12/8/97 - kill any objects that are too far away */ if(sbb->playing) { if(dist > sbb->outer_range) { //stop sound if it is playing if (sbb->activ_no != SOUND_NOACTIVEINDEX) Sound_Stop (sbb->activ_no); //not much point in restarting a non-looping sound, since it won't restart at the right place if(!sbb->loop && sbb->playing) sbb->playing = 0; return; } } if (sbb->activ_no == SOUND_NOACTIVEINDEX && (SID_NOSOUND != sbb->sound_number) && sbb->playing) { if(sbb->loop) { SOUND3DDATA s3d; s3d.position = sbb->position; s3d.inner_range = sbb->inner_range; s3d.outer_range = sbb->outer_range; s3d.velocity.vx = s3d.velocity.vy = s3d.velocity.vz = 0; Sound_Play((SOUNDINDEX)sbb->sound_number, (sbb->loop ? "nelh" : "neh"), &s3d, &sbb->activ_no); if (sbb->activ_no != SOUND_NOACTIVEINDEX) { Sound_ChangeVolume (sbb->activ_no, sbb->max_volume); Sound_ChangePitch (sbb->activ_no, sbb->pitch); } } else //sound has finished playing { sbb->playing = 0; } } } void StartPlacedSoundPlaying(STRATEGYBLOCK* sbptr) { assert(sbptr); assert(sbptr->type == I_BehaviourPlacedSound); SOUND_BEHAV_BLOCK * sbb = (SOUND_BEHAV_BLOCK*)sbptr->dataptr; if(SID_NOSOUND == sbb->sound_number) return; if(!sbb->playing) { //if sound is within range start it int dist = VectorDistance(&PlayerStatus.DisplayBlock->ObWorld, &sbb->position); if(dist < sbb->outer_range) { SOUND3DDATA s3d; s3d.position = sbb->position; s3d.inner_range = sbb->inner_range; s3d.outer_range = sbb->outer_range; s3d.velocity.vx = s3d.velocity.vy = s3d.velocity.vz = 0; Sound_Play ((SOUNDINDEX)sbb->sound_number, (sbb->loop ? "nelh" : "neh"), &s3d, &sbb->activ_no); if (sbb->activ_no != SOUND_NOACTIVEINDEX) { Sound_ChangeVolume (sbb->activ_no, sbb->max_volume); Sound_ChangePitch (sbb->activ_no, sbb->pitch); } } sbb->playing = 1; } } void StopPlacedSoundPlaying(STRATEGYBLOCK* sbptr) { assert(sbptr); assert(sbptr->type == I_BehaviourPlacedSound); SOUND_BEHAV_BLOCK * sbb = (SOUND_BEHAV_BLOCK*)sbptr->dataptr; if(SID_NOSOUND == sbb->sound_number) return; if(sbb->playing) { sbb->playing = 0; if (sbb->activ_no != SOUND_NOACTIVEINDEX) Sound_Stop (sbb->activ_no); } } /*--------------------** ** Loading and Saving ** **--------------------*/ #include "savegame.h" typedef struct placed_sound_save_block { SAVE_BLOCK_STRATEGY_HEADER header; int playing; } PLACED_SOUND_SAVE_BLOCK; //defines for load/save macros #define SAVELOAD_BLOCK block #define SAVELOAD_BEHAV sbb void LoadStrategy_PlacedSound(SAVE_BLOCK_STRATEGY_HEADER* header) { PLACED_SOUND_SAVE_BLOCK* block = (PLACED_SOUND_SAVE_BLOCK*) header; //check the size of the save block if(header->size != sizeof(*block)) return; //find the existing strategy block STRATEGYBLOCK* sbPtr = FindSBWithName(header->SBname); if(!sbPtr) return; //make sure the strategy found is of the right type if(sbPtr->type != I_BehaviourPlacedSound) return; SOUND_BEHAV_BLOCK * sbb = (SOUND_BEHAV_BLOCK *)sbPtr->dataptr; //start copying stuff COPYELEMENT_LOAD(playing) sbb->sound_not_started = 0; Load_SoundState(&sbb->activ_no); } void SaveStrategy_PlacedSound(STRATEGYBLOCK* sbPtr) { PLACED_SOUND_SAVE_BLOCK* block; SOUND_BEHAV_BLOCK * sbb = (SOUND_BEHAV_BLOCK *)sbPtr->dataptr; GET_STRATEGY_SAVE_BLOCK(block,sbPtr); //start copying stuff COPYELEMENT_SAVE(playing) Save_SoundState(&sbb->activ_no); }