2014-06-04 00:34:58 +02:00
--============
--Snowballs
--============
-- Snowballs were destroying nodes if the snowballs landed just right.
2015-05-29 12:11:17 +02:00
-- Quite a bit of trial-and-error learning here and it boiled down to a
2014-06-04 00:34:58 +02:00
-- small handful of code lines making the difference. ~ LazyJ
2015-06-03 22:09:32 +02:00
local creative_mode = minetest.setting_getbool ( " creative_mode " )
2015-11-22 01:21:21 +01:00
local snowball_velocity = snow.snowball_velocity
local snowball_gravity = snow.snowball_gravity
snow.register_on_configuring ( function ( name , v )
if name == " snowball_velocity " then
snowball_velocity = v
elseif name == " snowball_gravity " then
snowball_gravity = v
end
end )
2015-06-03 22:09:32 +02:00
local function get_gravity ( )
local grav = tonumber ( minetest.setting_get ( " movement_gravity " ) ) or 9.81
2015-11-22 01:21:21 +01:00
return grav * snowball_gravity
2015-06-03 22:09:32 +02:00
end
2015-06-04 13:09:04 +02:00
local someone_throwing
local timer = 0
2014-06-04 00:34:58 +02:00
--Shoot snowball
2015-06-03 22:09:32 +02:00
local function snow_shoot_snowball ( item , player )
2015-06-04 13:09:04 +02:00
local addp = { y = 1.625 } -- + (math.random()-0.5)/5}
2015-06-03 22:09:32 +02:00
local dir = player : get_look_dir ( )
2015-06-04 13:09:04 +02:00
local dif = 2 * math.sqrt ( dir.z * dir.z + dir.x * dir.x )
addp.x = dir.z / dif -- + (math.random()-0.5)/5
addp.z = - dir.x / dif -- + (math.random()-0.5)/5
local pos = vector.add ( player : getpos ( ) , addp )
local obj = minetest.add_entity ( pos , " snow:snowball_entity " )
2015-11-22 01:21:21 +01:00
obj : setvelocity ( vector.multiply ( dir , snowball_velocity ) )
2015-06-03 22:09:32 +02:00
obj : setacceleration ( { x = dir.x *- 3 , y =- get_gravity ( ) , z = dir.z *- 3 } )
if creative_mode then
2015-06-04 13:09:04 +02:00
if not someone_throwing then
someone_throwing = true
timer = - 0.5
end
2015-06-03 22:09:32 +02:00
return
end
2014-06-04 00:34:58 +02:00
item : take_item ( )
return item
end
2015-06-04 13:09:04 +02:00
if creative_mode then
local function update_step ( dtime )
timer = timer + dtime
if timer < 0.006 then
return
end
timer = 0
2015-06-03 22:09:32 +02:00
2015-06-04 13:09:04 +02:00
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 ( )
if itemname == " default:snow " then
snow_shoot_snowball ( nil , player )
2015-06-03 22:09:32 +02:00
active = true
break
end
end
end
2015-06-04 13:09:04 +02:00
-- disable the function if noone currently throws them
if not active then
someone_throwing = false
end
2015-06-03 22:09:32 +02:00
end
2015-06-04 13:09:04 +02:00
-- do automatic throwing using a globalstep
minetest.register_globalstep ( function ( dtime )
-- only if one holds left click
if someone_throwing then
update_step ( dtime )
end
end )
end
2015-06-03 22:09:32 +02:00
2014-06-04 00:34:58 +02:00
--The snowball Entity
2015-06-03 22:09:32 +02:00
local snow_snowball_ENTITY = {
2014-06-04 00:34:58 +02:00
physical = false ,
2015-06-03 22:09:32 +02:00
timer = 0 ,
2015-06-04 14:24:11 +02:00
collisionbox = { - 5 / 16 , - 5 / 16 , - 5 / 16 , 5 / 16 , 5 / 16 , 5 / 16 } ,
2014-06-04 00:34:58 +02:00
}
2015-06-03 22:09:32 +02:00
function snow_snowball_ENTITY . on_activate ( self )
2015-06-04 14:24:11 +02:00
self.object : set_properties ( { textures = { " default_snowball.png^[transform " .. math.random ( 0 , 7 ) } } )
2015-06-04 13:09:04 +02:00
self.object : setacceleration ( { x = 0 , y =- get_gravity ( ) , z = 0 } )
2015-06-03 22:09:32 +02:00
self.lastpos = self.object : getpos ( )
2015-06-04 13:09:04 +02:00
minetest.after ( 0.1 , function ( obj )
if not obj then
return
end
local vel = obj : getvelocity ( )
if vel
and vel.y ~= 0 then
2015-06-03 22:09:32 +02:00
return
end
minetest.after ( 0 , function ( obj )
2015-06-04 13:09:04 +02:00
if not obj then
return
end
local vel = obj : getvelocity ( )
if not vel
or vel.y == 0 then
2015-06-03 22:09:32 +02:00
obj : remove ( )
end
end , obj )
end , self.object )
end
2014-06-04 00:34:58 +02:00
--Snowball_entity.on_step()--> called when snowball is moving.
2015-06-03 22:09:32 +02:00
function snow_snowball_ENTITY . on_step ( self , dtime )
2015-05-23 19:53:30 +02:00
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
2015-06-03 22:09:32 +02:00
if self.physical then
local fell = self.object : getvelocity ( ) . y == 0
if not fell then
return
end
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
2015-05-29 12:11:17 +02:00
end
end
2015-06-03 22:09:32 +02:00
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
2014-06-04 00:34:58 +02:00
end
2015-05-23 19:53:30 +02:00
self.lastpos = vector.new ( pos )
2014-06-04 00:34:58 +02:00
end
minetest.register_entity ( " snow:snowball_entity " , snow_snowball_ENTITY )
-- Snowball and Default Snowball Merged
2015-05-29 12:11:17 +02:00
-- They both look the same, they do basically the same thing (except one is a leftclick throw
-- and the other is a rightclick drop),... Why not combine snow:snowball with default:snow and
2014-06-04 00:34:58 +02:00
-- benefit from both? ~ LazyJ, 2014_04_08
--[[ Save this for reference and occasionally compare to the default code for any updates.
minetest.register_node ( " :default:snow " , {
description = " Snow " ,
tiles = { " default_snow.png " } ,
inventory_image = " default_snowball.png " ,
wield_image = " default_snowball.png " ,
is_ground_content = true ,
paramtype = " light " ,
buildable_to = true ,
leveled = 7 ,
drawtype = " nodebox " ,
freezemelt = " default:water_flowing " ,
node_box = {
type = " leveled " ,
fixed = {
{ - 0.5 , - 0.5 , - 0.5 , 0.5 , - 0.5 + 2 / 16 , 0.5 } ,
} ,
} ,
groups = { crumbly = 3 , falling_node = 1 , melts = 1 , float = 1 } ,
sounds = default.node_sound_dirt_defaults ( {
footstep = { name = " default_snow_footstep " , gain = 0.25 } ,
dug = { name = " default_snow_footstep " , gain = 0.75 } ,
} ) ,
on_construct = function ( pos )
if minetest.get_node ( { x = pos.x , y = pos.y - 1 , z = pos.z } ) . name == " default:dirt_with_grass " or minetest.get_node ( { x = pos.x , y = pos.y - 1 , z = pos.z } ) . name == " default:dirt " then
minetest.set_node ( { x = pos.x , y = pos.y - 1 , z = pos.z } , { name = " default:dirt_with_snow " } )
end
-- Now, let's turn the snow pile into a snowblock. ~ LazyJ
if minetest.get_node ( { x = pos.x , y = pos.y - 2 , z = pos.z } ) . name == " default:snow " and -- Minus 2 because at the end of this, the layer that triggers the change to a snowblock is the second layer more than a full block, starting into a second block (-2) ~ LazyJ, 2014_04_11
minetest.get_node ( { x = pos.x , y = pos.y , z = pos.z } ) . name == " default:snow " then
minetest.set_node ( { x = pos.x , y = pos.y - 2 , z = pos.z } , { name = " default:snowblock " } )
end
end ,
on_use = snow_shoot_snowball -- This line is from the 'Snow' mod, the reset is default Minetest.
} )
--]]
minetest.override_item ( " default:snow " , {
2014-06-05 06:38:54 +02:00
drop = {
max_items = 2 ,
items = {
2015-06-03 22:09:32 +02:00
{ items = { ' snow:moss ' } , rarity = 20 , } ,
{ items = { ' default:snow ' } , }
2014-06-05 06:38:54 +02:00
}
} ,
2015-01-15 21:35:24 +01:00
leveled = 7 ,
2015-01-11 23:12:51 +01:00
node_box = {
type = " leveled " ,
fixed = {
2015-01-15 21:35:24 +01:00
{ - 0.5 , - 0.5 , - 0.5 , 0.5 , - 0.5 , 0.5 } ,
2015-01-11 23:12:51 +01:00
} ,
} ,
2015-06-03 22:09:32 +02:00
groups = { cracky = 3 , crumbly = 3 , choppy = 3 , oddly_breakable_by_hand = 3 , falling_node = 1 , melts = 2 , float = 1 } ,
2014-06-05 05:51:06 +02:00
sunlight_propagates = true ,
--Disable placement prediction for snow.
node_placement_prediction = " " ,
2014-06-04 00:34:58 +02:00
on_construct = function ( pos )
2015-05-23 19:53:30 +02:00
pos.y = pos.y - 1
local node = minetest.get_node ( pos )
if node.name == " default:dirt_with_grass "
or node.name == " default:dirt " then
node.name = " default:dirt_with_snow "
minetest.set_node ( pos , node )
2014-06-04 00:34:58 +02:00
end
2014-06-05 05:51:06 +02:00
end ,
--Handle node drops due to node level.
on_dig = function ( pos , node , digger )
local level = minetest.get_node_level ( pos )
minetest.node_dig ( pos , node , digger )
if minetest.get_node ( pos ) . name ~= node.name then
2015-05-23 19:53:30 +02:00
local inv = digger : get_inventory ( )
if not inv then
return
end
local left = inv : add_item ( " main " , " default:snow " .. tostring ( level / 7 - 1 ) )
if not left : is_empty ( ) then
2015-06-03 22:09:32 +02:00
minetest.add_item ( {
2015-05-23 19:53:30 +02:00
x = pos.x + math.random ( ) / 2 - 0.25 ,
y = pos.y + math.random ( ) / 2 - 0.25 ,
z = pos.z + math.random ( ) / 2 - 0.25 ,
2015-06-03 22:09:32 +02:00
} , left )
2014-06-05 05:51:06 +02:00
end
end
end ,
--Manage snow levels.
on_place = function ( itemstack , placer , pointed_thing )
local under = pointed_thing.under
local oldnode_under = minetest.get_node_or_nil ( under )
local above = pointed_thing.above
2015-05-29 12:11:17 +02:00
2015-05-23 19:53:30 +02:00
if not oldnode_under
or not above then
2014-06-05 05:51:06 +02:00
return
end
2015-05-29 12:11:17 +02:00
2014-06-05 05:51:06 +02:00
local olddef_under = ItemStack ( { name = oldnode_under.name } ) : get_definition ( )
olddef_under = olddef_under or minetest.nodedef_default
2015-05-29 12:11:17 +02:00
2015-05-23 19:53:30 +02:00
local place_to
2014-06-05 05:51:06 +02:00
-- If node under is buildable_to, place into it instead (eg. snow)
if olddef_under.buildable_to then
2015-05-23 19:53:30 +02:00
place_to = under
else
-- Place above pointed node
place_to = above
2014-06-05 05:51:06 +02:00
end
2015-05-29 12:11:17 +02:00
2015-05-23 19:53:30 +02:00
local level = minetest.get_node_level ( place_to )
2014-06-05 05:51:06 +02:00
if level == 63 then
minetest.set_node ( place_to , { name = " default:snowblock " } )
else
minetest.set_node_level ( place_to , level + 7 )
end
2015-05-29 12:11:17 +02:00
2015-05-23 19:53:30 +02:00
if minetest.get_node ( place_to ) . name ~= " default:snow " then
2014-06-05 05:51:06 +02:00
local itemstack , placed = minetest.item_place_node ( itemstack , placer , pointed_thing )
return itemstack , placed
2014-06-04 00:34:58 +02:00
end
2015-05-29 12:11:17 +02:00
2014-06-05 07:01:14 +02:00
itemstack : take_item ( )
2015-05-29 12:11:17 +02:00
2014-06-05 05:51:06 +02:00
return itemstack
2014-06-04 00:34:58 +02:00
end ,
2015-06-03 22:09:32 +02:00
on_use = snow_shoot_snowball
2014-06-04 00:34:58 +02:00
} )
--[[
A note about default torches , melting , and " buildable_to = true " in default snow .
On servers where buckets are disabled , snow and ice stuff is used to set water for crops and
water stuff like fountains , pools , ponds , ect .. It is a common practice to set a default torch on
the snow placed where the players want water to be .
2015-05-29 12:11:17 +02:00
If you place a default torch * on * default snow to melt it , instead of melting the snow is
* replaced * by the torch . Using " buildable_to = false " would fix this but then the snow would no
2014-06-04 00:34:58 +02:00
longer pile - up in layers ; the snow would stack like thin shelves in a vertical column .
2015-05-29 12:11:17 +02:00
I tinkered with the default torch ' s code (see below) to check for snow at the position and one
node above ( layered snow logs as the next y position above ) but default snow ' s
" buildable_to = true " always happened first . An interesting exercise to better learn how Minetest
works , but otherwise not worth it . If you set a regular torch near snow , the snow will melt
and disappear leaving you with nearly the same end result anyway . I say " nearly the same "
because if you set a default torch on layered snow , the torch will replace the snow and be
2014-06-04 00:34:58 +02:00
lit on the ground . If you were able to set a default torch * on * layered snow , the snow would
melt and the torch would become a dropped item .
~ LazyJ
--]]
-- Some of the ideas I tried. ~ LazyJ
--[[
local can_place_torch_on_top = function ( pos )
if minetest.get_node ( { x = pos.x , y = pos.y , z = pos.z } ) . name == " default:snow "
or minetest.get_node ( { x = pos.x , y = pos.y + 1 , z = pos.z } ) . name == " default:snow " then
minetest.override_item ( " default:snow " , { buildable_to = false , } )
end
2015-05-29 12:11:17 +02:00
end
2014-06-04 00:34:58 +02:00
--]]
--[[
minetest.override_item ( " default:torch " , {
--on_construct = function(pos)
on_place = function ( itemstack , placer , pointed_thing )
--if minetest.get_node({x=pos.x, y=pos.y, z=pos.z}).name == "default:snow"
-- Even though layered snow doesn't look like it's in the next position above (y+1)
-- it registers in that position. Check the terminal's output to see the coord change.
--or minetest.get_node({x=pos.x, y=pos.y+1, z=pos.z}).name == "default:snow"
if pointed_thing.name == " default:snow "
then minetest.set_node ( { x = pos.x , y = pos.y + 1 , z = pos.z } , { name = " default:torch " } )
end
end
} )
--]]