diff --git a/init.lua b/init.lua index fc44822..7095f53 100644 --- a/init.lua +++ b/init.lua @@ -413,12 +413,28 @@ function mobkit.animate(self,anim) end end -function mobkit.make_sound(self,sound) - if self.sounds and self.sounds[sound] then - minetest.sound_play(self.sounds[sound], {object=self.object}) +function mobkit.make_sound(self, sound) + local spec = self.sounds[sound] + local param_table = {object=self.object} + + if type(spec) == 'table' then + --pick random sound if it's a spec for random sounds + if #spec > 0 then spec = spec[random(#spec)] end + + --returns value or a random value within the range [value[1], value[2]) + local function in_range(value) + return type(value) == 'table' and value[1]+random()*(value[2]-value[1]) or value + end + + --pick random values within a range if they're a table + param_table.gain = in_range(spec.gain) + param_table.fade = in_range(spec.fade) + param_table.pitch = in_range(spec.pitch) end + return minetest.sound_play(spec, param_table) end + function mobkit.is_neighbor_node_reachable(self,neighbor) -- todo: take either number or pos local offset = neighbors[neighbor] local pos=mobkit.get_stand_pos(self) diff --git a/mobkit_api.txt b/mobkit_api.txt index d75e77a..35afe16 100644 --- a/mobkit_api.txt +++ b/mobkit_api.txt @@ -208,9 +208,32 @@ minetest.register_entity("mod:name",{ ... } sounds = { - [name] = [string filename], + [name] = [string filename], --single, simple, + + [name] = { --single, more powerful. All fields but 'name' are optional + name = [string filename], + gain=[num or range], --range is a table of the format {left_bound, right_bound} + fade=[num or range], + pitch=[num or range], + }, + + [name] = { --variant, sound is chosen randomly + { + name = [string filename], + gain=[num or range], + fade=[num or range], + pitch=[num or range], + }, + { + name = [string filename], + gain=[num or range], + fade=[num or range], + pitch=[num or range], + }, + ... + }, ... - } + }, max_speed = [num], -- m/s jump_height = [num], -- nodes/meters view_range = [num], -- nodes/meters @@ -335,7 +358,8 @@ function mobkit.animate(self,anim) function mobkit.make_sound(self,sound) -- sound is string, see entity definition - -- makes an entity play sound, or does nothing if not defined + -- makes an entity play sound, or does nothing if not defined + --returns sound handle function mobkit.go_forward_horizontal(self,speed) -- sets an entity's horizontal velocity in yaw direction. Vertical velocity unaffected.