commit deb4f789cd53a56c143bc636668ab7e8e1aa79b3 Author: CosmosScripter <50247544+CosmosScripter@users.noreply.github.com> Date: Fri Jul 23 20:30:52 2021 -0300 First version is out! So, I had this random idea and decided to upload it. diff --git a/LICENSE.txt b/LICENSE.txt new file mode 100644 index 0000000..a439587 --- /dev/null +++ b/LICENSE.txt @@ -0,0 +1,31 @@ +sound_distance Copyright (C) 2021 Cosmos + +sound_distance mod by Cosmos- Adds a function which plays sounds using the Doppler Effect. + +DISCLAIMER: This software is free, and will always be free. If you paid anything for obtaining this software, you have been bamboozled! +===================================================================== +get_distance function (by TenPlus1): + The MIT License (MIT) + + Copyright (c) 2016 TenPlus1 + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +-------------------------------------------------------------------- +sound_distance_play function (by Cosmos) is licensed under LGPL-2.1 +Full license: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html diff --git a/README.md b/README.md new file mode 100644 index 0000000..7da1ed9 --- /dev/null +++ b/README.md @@ -0,0 +1,28 @@ +Sound distance mod [v1.0] by Cosmos +========================== +This mod only adds a function, which plays a sound with a different gain depending on the distance to the sound origin. +It can be used so players can know if they're either closer or further to the sound origin. + +How to use: + +sound_distance_play(pos, sound, sound_gain, distance) + +Example: + +sound_distance_play(self.object:get_pos(), "example_sound", 1, 16) +--------------------------------------------- +See: license.txt for license. + +Special thanks to TenPlus1, as the "get_distance" function is from mobs redo. +--------------------------------------------- +Depends on: default. +--------------------------------------------- + +==HOW TO INSTALL THE MOD== +1.Download the mod, it may be a ZIP file. +2. Extract the folder inside to somewhere on the computer (if its a ZIP file). +3.Make sure that when you open the folder, you can directly find "README.md" in the listing. If you just see another folder, move that folder up one level and delete the old one. +4.Rename the folder to "sound_distance" incase it has another name. +5.Move the mod to "minetest/mods/" or "~/.minetest/mods/". +6.Here we go! +============================ diff --git a/depends.txt b/depends.txt new file mode 100644 index 0000000..4ad96d5 --- /dev/null +++ b/depends.txt @@ -0,0 +1 @@ +default diff --git a/description.txt b/description.txt new file mode 100644 index 0000000..e066ccf --- /dev/null +++ b/description.txt @@ -0,0 +1 @@ +Adds a function to play a sound using the Doppler Effect diff --git a/init.lua b/init.lua new file mode 100644 index 0000000..ff493a7 --- /dev/null +++ b/init.lua @@ -0,0 +1,52 @@ +--Fun fact: the "Doppler Effect" is physics law about waves, according to it, basically waves change depending on their distance. +--The Doppler Effect is what makes us able to tell if the sound origin is getting closer or further. + +--Note: "a" stands for the target position, while "b" is the origin position +local get_distance = function(a, b) --This function is from mobs redo + + if not a or not b then return 50 end -- nil check + + local x, y, z = a.x - b.x, a.y - b.y, a.z - b.z + + return math.sqrt(x * x + y * y + z * z) +end + +function sound_distance_play(pos, sound, sound_gain, distance) + if not pos or not sound or not distance then return end + if not sound_gain then sound_gain = 1 end + for _,player in ipairs(minetest.get_connected_players()) do + local dist = get_distance(player:get_pos(), pos) + if not dist then return end + if dist > distance then return end + local doppler = distance - dist + if not doppler then return end + if not player then return end--Avoid glitching incase player leaves + if doppler == 0 then + minetest.sound_play(sound, {gain = 0.1, to_player = player:get_player_name()}) + else + if sound_gain*(doppler/10) > sound_gain then--This avoids a possible ear exploder incase the sound has a huge hear distance for example + minetest.sound_play(sound, {gain = sound_gain, to_player = player:get_player_name()})--The sound is played only for that specific player + else + minetest.sound_play(sound, {gain = sound_gain*(doppler/10), to_player = player:get_player_name()}) + end + end + end +end + +--[[minetest.register_node("sound_distance:test_node", { + description = "Sound distance test", + tiles = {"default_steel_block.png"}, + groups = {oddly_breakable_by_hand=1, snappy=1, cracky=1}, + sounds = default_stone_sounds, + paramtype = "light", + is_ground_content = false, + on_rightclick = function(pos, node, clicker) + sound_distance_play(pos, "tnt_explode", 1, 16) + minetest.after(2, function() + sound_distance_play(pos, "tnt_explode", 1, 16) + end) + minetest.after(5, function() + sound_distance_play(pos, "tnt_explode", 1, 16) + end) + end, +})]]