From 601d1936c9ab4787d43f55d67900ed7c46fd3452 Mon Sep 17 00:00:00 2001 From: Perttu Ahola Date: Sat, 24 Mar 2012 19:01:26 +0200 Subject: Lua API for playing sounds --- src/client.cpp | 111 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 110 insertions(+), 1 deletion(-) (limited to 'src/client.cpp') diff --git a/src/client.cpp b/src/client.cpp index d8fb4eb77..89070d66b 100644 --- a/src/client.cpp +++ b/src/client.cpp @@ -261,7 +261,8 @@ Client::Client( m_nodedef_received(false), m_time_of_day_set(false), m_last_time_of_day_f(-1), - m_time_of_day_update_timer(0) + m_time_of_day_update_timer(0), + m_removed_sounds_check_timer(0) { m_packetcounter_timer = 0.0; //m_delete_unused_sectors_timer = 0.0; @@ -733,6 +734,63 @@ void Client::step(float dtime) m_inventory_updated = true; } } + + /* + Update positions of sounds attached to objects + */ + { + for(std::map::iterator + i = m_sounds_to_objects.begin(); + i != m_sounds_to_objects.end(); i++) + { + int client_id = i->first; + u16 object_id = i->second; + ClientActiveObject *cao = m_env.getActiveObject(object_id); + if(!cao) + continue; + v3f pos = cao->getPosition(); + m_sound->updateSoundPosition(client_id, pos); + } + } + + /* + Handle removed remotely initiated sounds + */ + m_removed_sounds_check_timer += dtime; + if(m_removed_sounds_check_timer >= 2.32) + { + m_removed_sounds_check_timer = 0; + // Find removed sounds and clear references to them + std::set removed_server_ids; + for(std::map::iterator + i = m_sounds_server_to_client.begin(); + i != m_sounds_server_to_client.end();) + { + s32 server_id = i->first; + int client_id = i->second; + i++; + if(!m_sound->soundExists(client_id)){ + m_sounds_server_to_client.erase(server_id); + m_sounds_client_to_server.erase(client_id); + m_sounds_to_objects.erase(client_id); + removed_server_ids.insert(server_id); + } + } + // Sync to server + if(removed_server_ids.size() != 0) + { + std::ostringstream os(std::ios_base::binary); + writeU16(os, TOSERVER_REMOVED_SOUNDS); + writeU16(os, removed_server_ids.size()); + for(std::set::iterator i = removed_server_ids.begin(); + i != removed_server_ids.end(); i++) + writeS32(os, *i); + std::string s = os.str(); + SharedBuffer data((u8*)s.c_str(), s.size()); + // Send as reliable + Send(0, data, true); + } + } } // Virtual methods from con::PeerHandler @@ -1610,6 +1668,57 @@ void Client::ProcessData(u8 *data, u32 datasize, u16 sender_peer_id) m_itemdef->deSerialize(tmp_is2); m_itemdef_received = true; } + else if(command == TOCLIENT_PLAY_SOUND) + { + std::string datastring((char*)&data[2], datasize-2); + std::istringstream is(datastring, std::ios_base::binary); + + s32 server_id = readS32(is); + std::string name = deSerializeString(is); + float gain = readF1000(is); + int type = readU8(is); // 0=local, 1=positional, 2=object + v3f pos = readV3F1000(is); + u16 object_id = readU16(is); + bool loop = readU8(is); + // Start playing + int client_id = -1; + switch(type){ + case 0: // local + client_id = m_sound->playSound(name, false, gain); + break; + case 1: // positional + client_id = m_sound->playSoundAt(name, false, gain, pos); + break; + case 2: { // object + ClientActiveObject *cao = m_env.getActiveObject(object_id); + if(cao) + pos = cao->getPosition(); + client_id = m_sound->playSoundAt(name, loop, gain, pos); + // TODO: Set up sound to move with object + break; } + default: + break; + } + if(client_id != -1){ + m_sounds_server_to_client[server_id] = client_id; + m_sounds_client_to_server[client_id] = server_id; + if(object_id != 0) + m_sounds_to_objects[client_id] = object_id; + } + } + else if(command == TOCLIENT_STOP_SOUND) + { + std::string datastring((char*)&data[2], datasize-2); + std::istringstream is(datastring, std::ios_base::binary); + + s32 server_id = readS32(is); + std::map::iterator i = + m_sounds_server_to_client.find(server_id); + if(i != m_sounds_server_to_client.end()){ + int client_id = i->second; + m_sound->stopSound(client_id); + } + } else { infostream<<"Client: Ignoring unknown command " -- cgit v1.2.3