Skip to content

Commit

Permalink
Backport IGame_Level::SoundEvent methods from IX-Ray 1.6
Browse files Browse the repository at this point in the history
  • Loading branch information
Drombeys committed Dec 13, 2023
1 parent d9802d6 commit 04f40eb
Show file tree
Hide file tree
Showing 5 changed files with 189 additions and 116 deletions.
128 changes: 125 additions & 3 deletions src/xr_3da/IGame_Level.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@
#include "gamefont.h"
#include "xrLevel.h"
#include "CameraManager.h"
#include "xr_object.h"
#include "feel_sound.h"

ENGINE_API IGame_Level* g_pGameLevel = NULL;
extern BOOL g_bLoaded;

IGame_Level::IGame_Level ()
{
Expand Down Expand Up @@ -39,6 +42,9 @@ IGame_Level::~IGame_Level ()
Device.seqRender.Remove (this);
Device.seqFrame.Remove (this);
CCameraManager::ResetPP ();

Sound->set_geometry_occ(NULL);
Sound->set_handler(NULL);
}

void IGame_Level::net_Stop ()
Expand All @@ -53,7 +59,14 @@ void IGame_Level::net_Stop ()
}

//-------------------------------------------------------------------------------------------
extern CStatTimer tscreate;
void __stdcall _sound_event (ref_sound_data_ptr S, float range)
{
if ( g_pGameLevel && S && S->feedback ) g_pGameLevel->SoundEvent_Register (S,range);
}
static void __stdcall build_callback (Fvector* V, int Vcnt, CDB::TRI* T, int Tcnt, void* params)
{
g_pGameLevel->Load_GameSpecific_CFORM( T, Tcnt );
}

BOOL IGame_Level::Load (u32 dwNum)
{
Expand All @@ -76,7 +89,10 @@ BOOL IGame_Level::Load (u32 dwNum)

// CForms
g_pGamePersistent->LoadTitle ("st_loading_cform");
ObjectSpace.Load ();
ObjectSpace.Load(build_callback);
//Sound->set_geometry_occ ( &Static );
Sound->set_geometry_occ (ObjectSpace.GetStaticModel ());
Sound->set_handler ( _sound_event );
pApp->LoadSwitch ();


Expand All @@ -89,7 +105,6 @@ BOOL IGame_Level::Load (u32 dwNum)

// Render-level Load
Render->level_Load (LL_Stream);
tscreate.FrameEnd ();
// Msg ("* S-CREATE: %f ms, %d times",tscreate.result,tscreate.count);

// Objects
Expand Down Expand Up @@ -180,3 +195,110 @@ void CServerInfo::AddItem( shared_str& name_, LPCSTR value_, u32 color_ )
data.push_back( it );
}
}

void IGame_Level::SetEntity( CObject* O )
{
pCurrentEntity=pCurrentViewEntity=O;
}

void IGame_Level::SetViewEntity( CObject* O )
{
pCurrentViewEntity=O;
}

void IGame_Level::SoundEvent_Register ( ref_sound_data_ptr S, float range )
{
if (!g_bLoaded) return;
if (!S) return;
if (S->g_object && S->g_object->getDestroy()) {S->g_object=0; return;}
if (0==S->feedback) return;

clamp (range,0.1f,500.f);

const CSound_params* p = S->feedback->get_params();
Fvector snd_position = p->position;
if(S->feedback->is_2D()){
snd_position.add (Sound->listener_position());
}

VERIFY (p && _valid(range) );
range = _min(range,p->max_ai_distance);
VERIFY (_valid(snd_position));
VERIFY (_valid(p->max_ai_distance));
VERIFY (_valid(p->volume));

// Query objects
Fvector bb_size = {range,range,range};
g_SpatialSpace->q_box (snd_ER,0,STYPE_REACTTOSOUND,snd_position,bb_size);

// Iterate
xr_vector<ISpatial*>::iterator it = snd_ER.begin ();
xr_vector<ISpatial*>::iterator end = snd_ER.end ();
for (; it!=end; it++) {
Feel::Sound* L = (*it)->dcast_FeelSound ();
if (0==L) continue;
CObject* CO = (*it)->dcast_CObject(); VERIFY(CO);
if (CO->getDestroy()) continue;

// Energy and signal
VERIFY (_valid((*it)->spatial.sphere.P));
float dist = snd_position.distance_to((*it)->spatial.sphere.P);
if (dist>p->max_ai_distance) continue;
VERIFY (_valid(dist));
VERIFY2 (!fis_zero(p->max_ai_distance), S->handle->file_name());
float Power = (1.f-dist/p->max_ai_distance)*p->volume;
VERIFY (_valid(Power));
if (Power>EPS_S) {
float occ = Sound->get_occlusion_to((*it)->spatial.sphere.P,snd_position);
VERIFY (_valid(occ)) ;
Power *= occ;
if (Power>EPS_S) {
_esound_delegate D = { L, S, Power };
snd_Events.push_back (D) ;
}
}
}
snd_ER.clear();
}

void IGame_Level::SoundEvent_Dispatch ( )
{
while (!snd_Events.empty()) {
_esound_delegate& D = snd_Events.back ();
VERIFY (D.dest && D.source);
if (D.source->feedback) {
D.dest->feel_sound_new (
D.source->g_object,
D.source->g_type,
D.source->g_userdata,

D.source->feedback->is_2D() ? Device.vCameraPosition :
D.source->feedback->get_params()->position,
D.power
);
}
snd_Events.pop_back ();
}
}

// Lain: added
void IGame_Level::SoundEvent_OnDestDestroy (Feel::Sound* obj)
{
struct rem_pred
{
rem_pred(Feel::Sound* obj) : m_obj(obj) {}

bool operator () (const _esound_delegate& d)
{
return d.dest == m_obj;
}

private:
Feel::Sound* m_obj;
};

snd_Events.erase( std::remove_if(snd_Events.begin(), snd_Events.end(), rem_pred(obj)),
snd_Events.end() );
}


11 changes: 5 additions & 6 deletions src/xr_3da/IGame_Level.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#pragma once

#include "iinputreceiver.h"
//#include "CameraManager.h"
#include "xr_object_list.h"
#include "xr_area.h"

Expand Down Expand Up @@ -32,11 +31,10 @@ class ENGINE_API CServerInfo

IC SItem_ServerInfo& operator[] ( u32 id ) { VERIFY( id < max_item ); return data[id]; }

//CServerInfo() {};
//~CServerInfo() {};
CServerInfo() {};
~CServerInfo() {};
};


//-----------------------------------------------------------------------------------------------------------
class ENGINE_API IGame_Level :
public DLL_Pure,
Expand Down Expand Up @@ -99,11 +97,12 @@ class ENGINE_API IGame_Level :
// Main interface
CObject* CurrentEntity ( void ) const { return pCurrentEntity; }
CObject* CurrentViewEntity ( void ) const { return pCurrentViewEntity; }
void SetEntity ( CObject* O ) { pCurrentEntity=pCurrentViewEntity=O; }
void SetViewEntity ( CObject* O ) { pCurrentViewEntity=O; }
void SetEntity ( CObject* O );// { pCurrentEntity=pCurrentViewEntity=O; }
void SetViewEntity ( CObject* O );// { pCurrentViewEntity=O; }

void SoundEvent_Register ( ref_sound_data_ptr S, float range );
void SoundEvent_Dispatch ( );
void SoundEvent_OnDestDestroy (Feel::Sound*);

// Loader interface
ref_shader LL_CreateShader (int S, int T, int M, int C);
Expand Down
6 changes: 6 additions & 0 deletions src/xr_3da/xrGame/CustomMonster.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,12 @@ CCustomMonster::~CCustomMonster ()
xr_delete (m_movement_manager);
xr_delete (m_sound_player);

// Lain: added (asking GameLevel to forget about self)
if (g_pGameLevel)
{
g_pGameLevel->SoundEvent_OnDestDestroy(this);
}

#ifdef DEBUG
Msg ("dumping client spawn manager stuff for object with id %d",ID());
if(!g_dedicated_server)
Expand Down
Loading

0 comments on commit 04f40eb

Please sign in to comment.