change snowballs and play sounds; add preperation for automatic snowball throwing in creative mode

This commit is contained in:
HybridDog 2015-06-03 22:09:32 +02:00
parent 5b6592022f
commit f26ece0620
3 changed files with 134 additions and 61 deletions

View File

@ -94,27 +94,39 @@ function snow.place(pos)
if level < 63 then
if minetest.get_item_group(bnode.name, "leafdecay") == 0
and not snow.is_uneven(pos) then
minetest.sound_play("default_snow_footstep", {pos=pos})
minetest.add_node_level(pos, 7)
end
elseif level == 63 then
local p = minetest.find_node_near(pos, 10, "default:dirt_with_grass")
if p
and minetest.get_node_light(p, 0.5) == 15 then
minetest.sound_play("default_grass_footstep", {pos=pos})
minetest.place_node({x=pos.x, y=pos.y+1, z=pos.z}, {name="default:snow"})
else
minetest.sound_play("default_snow_footstep", {pos=pos})
minetest.add_node(pos, {name="default:snowblock"})
end
end
elseif node.name ~= "default:ice"
and bnode.name ~= "air" then
local drawtype = minetest.registered_nodes[node.name].drawtype
local data = minetest.registered_nodes[node.name]
local drawtype = data.drawtype
if drawtype == "normal"
or drawtype == "allfaces_optional" then
pos.y = pos.y+1
local sound = data.sounds
if sound then
sound = sound.footstep
if sound then
minetest.sound_play(sound.name, {pos=pos, gain=sound.gain})
end
end
minetest.place_node(pos, {name="default:snow"})
elseif drawtype == "plantlike" then
pos.y = pos.y - 1
if minetest.get_node(pos).name == "default:dirt_with_grass" then
minetest.sound_play("default_grass_footstep", {pos=pos})
minetest.add_node(pos, {name="default:dirt_with_snow"})
end
end
@ -164,7 +176,7 @@ local function is_uneven(pos)
end
if found then
local p = {x=pos.x+foundx, y=pos.y, z=pos.z+foundz}
if snow.is_uneven(p) ~= true then
if is_uneven(p) ~= true then
minetest.add_node_level(p, 7)
end
return true

View File

@ -6,62 +6,135 @@
-- Quite a bit of trial-and-error learning here and it boiled down to a
-- small handful of code lines making the difference. ~ LazyJ
local snowball_GRAVITY=9
local snowball_VELOCITY=19
local creative_mode = minetest.setting_getbool("creative_mode")
local function get_gravity()
local grav = tonumber(minetest.setting_get("movement_gravity")) or 9.81
return grav*snow.snowball_gravity
end
--[[local someone_digging
local timer = 0]]
--Shoot snowball
local snow_shoot_snowball=function (item, player, pointed_thing)
local playerpos=player:getpos()
local obj=minetest.add_entity({x=playerpos.x,y=playerpos.y+1.5,z=playerpos.z}, "snow:snowball_entity")
local function snow_shoot_snowball(item, player)
local pos = player:getpos()
pos.y = pos.y+1.625
local obj = minetest.add_entity(pos, "snow:snowball_entity")
local dir = player:get_look_dir()
obj:setvelocity({x=dir.x*snowball_VELOCITY, y=dir.y*snowball_VELOCITY, z=dir.z*snowball_VELOCITY})
obj:setacceleration({x=dir.x*-3, y=-snowball_GRAVITY, z=dir.z*-3})
obj:setvelocity(vector.multiply(dir, snow.snowball_velocity))
obj:setacceleration({x=dir.x*-3, y=-get_gravity(), z=dir.z*-3})
if creative_mode then
return
end
item:take_item()
return item
end
--[[ simulate the players digging with it using a globalstep
minetest.register_globalstep(function(dtime)
-- abort if noone uses a drill
if not someone_digging then
return
end
-- abort that it doesn't dig too fast
timer = timer+dtime
if timer < speed then
return
end
timer = 0
local active
for _,player in pairs(minetest.get_connected_players()) do
if player:get_player_control().LMB then
local item = player:get_wielded_item()
local itemname = item:get_name()
for name,func in pairs(drills) do
if name == itemname then
-- player has a mk3 drill as wielditem and holds left mouse button
local pt = get_pointed_thing(player, ranges[itemname])
if pt then
-- simulate the function
player:set_wielded_item(func(item, player, pt))
end
active = true
break
end
end
end
end
-- disable the function if noone currently uses a mk3 drill to reduce lag
if not active then
someone_digging = false
end
end)--]]
--The snowball Entity
snow_snowball_ENTITY={
local snow_snowball_ENTITY = {
physical = false,
timer = 0,
textures = {"default_snowball.png"},
lastpos={},
collisionbox = {0,0,0,0,0,0},
collisionbox = {-5/16,-5/16,-5/16,5/16,5/16,5/16},
}
function snow_snowball_ENTITY.on_activate(self)
self.lastpos = self.object:getpos()
minetest.after(0, function(obj)
if not obj
or obj:getvelocity().y ~= 0 then
return
end
minetest.after(0, function(obj)
if obj
and obj:getvelocity().y == 0 then
obj:remove()
end
end, obj)
end, self.object)
end
--Snowball_entity.on_step()--> called when snowball is moving.
snow_snowball_ENTITY.on_step = function(self, dtime)
function snow_snowball_ENTITY.on_step(self, dtime)
self.timer = self.timer+dtime
if self.timer > 600 then
-- 10 minutes are too long for a snowball to fly somewhere
self.object:remove()
end
local pos = self.object:getpos()
local node = minetest.get_node(pos)
--Become item when hitting a node.
if self.lastpos.x then --If there is no lastpos for some reason. ~ Splizard
-- Check to see what is one node above where the snow is
-- going to be placed. ~ LazyJ, 2014_04_08
-- Identify the name of the node that was found above. ~ LazyJ, 2014_04_08
local findwhatisabove = minetest.get_node({x=pos.x, y=pos.y+1, z=pos.z}).name
-- If the node above is air, then it's OK to go on to the next step. ~ LazyJ, 2014_04_08
if findwhatisabove == "air" then
-- If the node where the snow is going is anything except air, then it's OK to put
-- the snow on it. ~ Original line of code by Splizard, comment by LazyJ so I can
-- keep track of what this code does. ~ LazyJ, 2014_04_07
if node.name ~= "air" then
snow.place(pos) -- this is the original code, I replaced it with
-- minetest.place_node and bumped the y position up by 2 (make the snow drop
-- from a node above and pile up). ~ LazyJ, 2014_04_07
--minetest.place_node({x=pos.x, y=pos.y+2, z=pos.z}, {name="default:snow"})
self.object:remove()
if self.physical then
local fell = self.object:getvelocity().y == 0
if not fell then
return
end
else -- If findwhatisabove is not equal to "air" then cancel the snowball
-- with self.object:remove() ~ LazyJ, 2014_04_08
self.object:remove()
local pos = vector.round(self.object:getpos())
if minetest.get_node(pos).name == "air" then
pos.y = pos.y-1
if minetest.get_node(pos).name == "air" then
return
end
end
snow.place(pos)
self.object:remove()
return
end
local pos = vector.round(self.object:getpos())
if vector.equals(pos, self.lastpos) then
return
end
if minetest.get_node(pos).name ~= "air" then
self.object:setacceleration({x=0, y=-get_gravity(), z=0})
--self.object:setvelocity({x=0, y=0, z=0})
pos = self.lastpos
self.object:setpos(pos)
local gain = vector.length(self.object:getvelocity())/30
minetest.sound_play("default_snow_footstep", {pos=pos, gain=gain})
self.object:set_properties({physical = true})
self.physical = true
return
end
self.lastpos = vector.new(pos)
end
@ -121,16 +194,8 @@ minetest.override_item("default:snow", {
drop = {
max_items = 2,
items = {
{
-- player will get sapling with 1/20 chance
items = {'snow:moss'},
rarity = 20,
},
{
-- player will get leaves only if he get no saplings,
-- this is because max_items is 1
items = {'default:snow'},
}
{items = {'snow:moss'}, rarity = 20,},
{items = {'default:snow'},}
}
},
leveled = 7,
@ -153,12 +218,6 @@ minetest.override_item("default:snow", {
minetest.set_node(pos, node)
end
end,
--Remove dirt_with_snow and replace with dirt_with_grass.
--[[after_destruct = function(pos)
if minetest.get_node({x=pos.x, y=pos.y-1, z=pos.z}).name == "default:dirt_with_snow" then
minetest.set_node({x=pos.x, y=pos.y-1, z=pos.z}, {name="default:dirt_with_grass"})
end
end,]]
--Handle node drops due to node level.
on_dig = function(pos, node, digger)
local level = minetest.get_node_level(pos)
@ -170,12 +229,11 @@ minetest.override_item("default:snow", {
end
local left = inv:add_item("main", "default:snow "..tostring(level/7-1))
if not left:is_empty() then
local p = {
minetest.add_item({
x = pos.x + math.random()/2-0.25,
y = pos.y + math.random()/2-0.25,
z = pos.z + math.random()/2-0.25,
}
minetest.add_item(p, left)
}, left)
end
end
end,
@ -218,8 +276,7 @@ minetest.override_item("default:snow", {
return itemstack
end,
on_use = snow_shoot_snowball -- This line is from the 'Snow' mod,
-- the reset is default Minetest. ~ LazyJ
on_use = snow_shoot_snowball
})

View File

@ -1,5 +1,7 @@
--Global config and function table.
snow = {
snowball_gravity = 100/109,
snowball_velocity = 19,
sleds = true,
enable_snowfall = true,
lighter_snowfall = false,
@ -12,6 +14,8 @@ snow = {
--Config documentation.
local doc = {
snowball_gravity = "The gravity of thrown snowballs",
snowball_velocity = "How fast players throw snowballs",
sleds = "Disable this to prevent sleds from being riden.",
enable_snowfall = "Enables falling snow.",
lighter_snowfall = "Reduces the amount of resources and fps used by snowfall.",