2017-01-17 01:11:04 +03:00
--
2017-01-25 13:45:17 +03:00
-- Snowballs and other throwable items
2017-01-17 01:11:04 +03:00
--
2017-01-25 13:45:17 +03:00
local GRAVITY = tonumber ( minetest.setting_get ( " movement_gravity " ) )
2017-01-17 01:11:04 +03:00
2017-02-15 01:41:19 +03:00
local entity_mapping = {
[ " mcl_throwing:snowball " ] = " mcl_throwing:snowball_entity " ,
[ " mcl_throwing:egg " ] = " mcl_throwing:egg_entity " ,
[ " mcl_throwing:ender_pearl " ] = " mcl_throwing:ender_pearl_entity " ,
}
2017-02-15 01:18:23 +03:00
local velocities = {
[ " mcl_throwing:snowball_entity " ] = 22 ,
[ " mcl_throwing:egg_entity " ] = 22 ,
[ " mcl_throwing:ender_pearl_entity " ] = 22 ,
}
2017-02-15 01:41:19 +03:00
mcl_throwing.throw = function ( throw_item , pos , dir , velocity )
2017-02-15 01:18:23 +03:00
if velocity == nil then
2017-06-10 18:52:21 +03:00
velocity = velocities [ throw_item ]
2017-02-15 01:18:23 +03:00
end
if velocity == nil then
velocity = 22
end
2017-02-15 01:41:19 +03:00
2017-02-15 01:54:06 +03:00
local itemstring = ItemStack ( throw_item ) : get_name ( )
2017-02-15 01:41:19 +03:00
local obj = minetest.add_entity ( pos , entity_mapping [ itemstring ] )
obj : setvelocity ( { x = dir.x * velocity , y = dir.y * velocity , z = dir.z * velocity } )
obj : setacceleration ( { x = dir.x *- 3 , y =- GRAVITY , z = dir.z *- 3 } )
return obj
end
-- Throw item
local throw_function = function ( entity_name , velocity )
2017-01-17 01:11:04 +03:00
local func = function ( item , player , pointed_thing )
2017-02-15 01:41:19 +03:00
local playerpos = player : getpos ( )
local dir = player : get_look_dir ( )
local obj = mcl_throwing.throw ( item , { x = playerpos.x , y = playerpos.y + 1.5 , z = playerpos.z } , dir , velocity )
2017-01-25 13:45:17 +03:00
obj : get_luaentity ( ) . _thrower = player : get_player_name ( )
2017-01-26 20:50:06 +03:00
if not minetest.setting_getbool ( " creative_mode " ) then
item : take_item ( )
end
2017-01-17 01:11:04 +03:00
return item
end
return func
end
2017-01-26 20:59:57 +03:00
-- Staticdata handling because objects may want to be reloaded
local get_staticdata = function ( self )
local data = {
_lastpos = self._lastpos ,
_thrower = self._thrower ,
}
return minetest.serialize ( data )
end
local on_activate = function ( self , staticdata , dtime_s )
local data = minetest.deserialize ( staticdata )
if data then
self._lastpos = data._lastpos
self._thrower = data._thrower
end
end
2017-01-17 01:11:04 +03:00
-- The snowball entity
local snowball_ENTITY = {
physical = false ,
timer = 0 ,
textures = { " mcl_throwing_snowball.png " } ,
2017-02-15 01:43:43 +03:00
visual_size = { x = 0.5 , y = 0.5 } ,
2017-01-17 01:11:04 +03:00
collisionbox = { 0 , 0 , 0 , 0 , 0 , 0 } ,
2017-01-26 20:59:57 +03:00
get_staticdata = get_staticdata ,
on_activate = on_activate ,
_lastpos = { } ,
2017-01-17 01:11:04 +03:00
}
local egg_ENTITY = {
physical = false ,
timer = 0 ,
textures = { " mcl_throwing_egg.png " } ,
2017-02-15 01:43:43 +03:00
visual_size = { x = 0.45 , y = 0.45 } ,
2017-01-17 01:11:04 +03:00
collisionbox = { 0 , 0 , 0 , 0 , 0 , 0 } ,
2017-01-26 20:59:57 +03:00
get_staticdata = get_staticdata ,
on_activate = on_activate ,
_lastpos = { } ,
2017-01-17 01:11:04 +03:00
}
2017-01-25 13:45:17 +03:00
-- Ender pearl entity
local pearl_ENTITY = {
physical = false ,
timer = 0 ,
textures = { " mcl_throwing_ender_pearl.png " } ,
2017-02-15 01:43:43 +03:00
visual_size = { x = 0.9 , y = 0.9 } ,
2017-01-25 13:45:17 +03:00
collisionbox = { 0 , 0 , 0 , 0 , 0 , 0 } ,
2017-01-26 20:59:57 +03:00
get_staticdata = get_staticdata ,
on_activate = on_activate ,
_lastpos = { } ,
2017-01-25 13:45:17 +03:00
_thrower = nil , -- Player ObjectRef of the player who threw the ender pearl
}
2017-01-17 01:11:04 +03:00
2017-02-20 03:51:09 +03:00
-- Snowball on_step()--> called when snowball is moving.
local snowball_on_step = function ( self , dtime )
2017-01-17 01:11:04 +03:00
self.timer = self.timer + dtime
local pos = self.object : getpos ( )
local node = minetest.get_node ( pos )
2017-01-26 21:05:25 +03:00
local def = minetest.registered_nodes [ node.name ]
2017-01-17 01:11:04 +03:00
2017-01-26 21:05:25 +03:00
-- Destroy when hitting a solid node
2017-01-25 13:45:17 +03:00
if self._lastpos . x ~= nil then
2017-01-26 21:05:25 +03:00
if ( def and def.walkable ) or not def then
2017-01-25 13:45:17 +03:00
self.object : remove ( )
return
end
end
self._lastpos = { x = pos.x , y = pos.y , z = pos.z } -- Set _lastpos-->Node will be added at last pos outside the node
end
2017-02-20 03:51:09 +03:00
-- Movement function of egg
local egg_on_step = function ( self , dtime )
self.timer = self.timer + dtime
local pos = self.object : getpos ( )
local node = minetest.get_node ( pos )
local def = minetest.registered_nodes [ node.name ]
-- Destroy when hitting a solid node
if self._lastpos . x ~= nil then
if ( def and def.walkable ) or not def then
-- 1/8 chance to spawn a chick
-- FIXME: Chicks have a quite good chance to spawn in walls
local r = math.random ( 1 , 8 )
2017-06-24 16:31:46 +03:00
-- Turn given object into a child
local make_child = function ( object )
local ent = object : get_luaentity ( )
object : set_properties ( {
visual_size = { x = ent.base_size . x / 2 , y = ent.base_size . y / 2 } ,
collisionbox = {
ent.base_colbox [ 1 ] / 2 ,
ent.base_colbox [ 2 ] / 2 ,
ent.base_colbox [ 3 ] / 2 ,
ent.base_colbox [ 4 ] / 2 ,
ent.base_colbox [ 5 ] / 2 ,
ent.base_colbox [ 6 ] / 2 ,
}
} )
ent.child = true
end
2017-02-20 03:51:09 +03:00
if r == 1 then
2017-06-24 16:31:46 +03:00
make_child ( minetest.add_entity ( self._lastpos , " mobs_mc:chicken " ) )
2017-02-20 03:51:09 +03:00
-- BONUS ROUND: 1/32 chance to spawn 3 additional chicks
local r = math.random ( 1 , 32 )
if r == 1 then
local offsets = {
{ x = 0.7 , y = 0 , z = 0 } ,
{ x =- 0.7 , y = 0 , z =- 0.7 } ,
{ x =- 0.7 , y = 0 , z = 0.7 } ,
}
for o = 1 , 3 do
local pos = vector.add ( self._lastpos , offsets [ o ] )
2017-06-24 16:31:46 +03:00
make_child ( minetest.add_entity ( pos , " mobs_mc:chicken " ) )
2017-02-20 03:51:09 +03:00
end
end
end
self.object : remove ( )
return
end
end
self._lastpos = { x = pos.x , y = pos.y , z = pos.z } -- Set lastpos-->Node will be added at last pos outside the node
end
2017-01-25 13:45:17 +03:00
-- Movement function of ender pearl
local pearl_on_step = function ( self , dtime )
self.timer = self.timer + dtime
local pos = self.object : getpos ( )
2017-05-26 16:10:35 +03:00
pos.y = math.floor ( pos.y )
2017-01-25 13:45:17 +03:00
local node = minetest.get_node ( pos )
2017-05-26 18:56:09 +03:00
local nn = node.name
2017-01-26 21:05:25 +03:00
local def = minetest.registered_nodes [ node.name ]
2017-01-25 13:45:17 +03:00
2017-01-26 21:05:25 +03:00
-- Destroy when hitting a solid node
2017-01-25 13:45:17 +03:00
if self._lastpos . x ~= nil then
2017-05-26 19:12:35 +03:00
local walkable = ( def and def.walkable )
2017-05-26 17:13:51 +03:00
-- No teleport for hitting ignore for now. Otherwise the player could get stuck.
-- FIXME: This also means the player loses an ender pearl for throwing into unloaded areas
if node.name == " ignore " then
self.object : remove ( )
2017-05-26 18:56:09 +03:00
-- Activate when hitting a solid node or a plant
2017-05-26 19:12:35 +03:00
elseif walkable or nn == " mcl_core:vine " or nn == " mcl_core:deadbush " or minetest.get_item_group ( nn , " flower " ) ~= 0 or minetest.get_item_group ( nn , " sapling " ) ~= 0 or minetest.get_item_group ( nn , " plant " ) ~= 0 or minetest.get_item_group ( nn , " mushroom " ) ~= 0 or not def then
2017-01-25 13:45:17 +03:00
local player = minetest.get_player_by_name ( self._thrower )
if player then
-- Teleport and hurt player
2017-05-26 18:43:07 +03:00
2017-05-26 19:12:35 +03:00
-- First determine good teleport position
2017-05-26 18:43:07 +03:00
local dir = { x = 0 , y = 0 , z = 0 }
2017-05-26 19:33:30 +03:00
local v = self.object : getvelocity ( )
2017-05-26 19:12:35 +03:00
if walkable then
2017-05-26 19:33:30 +03:00
local vc = table.copy ( v ) -- vector for calculating
2017-05-26 19:12:35 +03:00
-- Node is walkable, we have to find a place somewhere outside of that node
2017-05-26 19:33:30 +03:00
vc = vector.normalize ( vc )
2017-05-26 19:12:35 +03:00
-- Zero-out the two axes with a lower absolute value than
-- the axis with the strongest force
local lv , ld
2017-05-26 19:33:30 +03:00
lv , ld = math.abs ( vc.y ) , " y "
if math.abs ( vc.x ) > lv then
lv , ld = math.abs ( vc.x ) , " x "
2017-05-26 19:12:35 +03:00
end
2017-05-26 19:33:30 +03:00
if math.abs ( vc.z ) > lv then
lv , ld = math.abs ( vc.z ) , " z "
2017-05-26 19:12:35 +03:00
end
2017-05-26 19:33:30 +03:00
if ld ~= " x " then vc.x = 0 end
if ld ~= " y " then vc.y = 0 end
if ld ~= " z " then vc.z = 0 end
2017-05-26 19:12:35 +03:00
-- Final tweaks to the teleporting pos, based on direction
-- Impact from the side
2017-05-26 19:33:30 +03:00
dir.x = vc.x * - 1
dir.z = vc.z * - 1
2017-05-26 19:12:35 +03:00
-- Special case: top or bottom of node
2017-05-26 19:33:30 +03:00
if vc.y > 0 then
2017-05-26 19:12:35 +03:00
-- We need more space when impact is from below
dir.y = - 2.3
2017-05-26 19:33:30 +03:00
elseif vc.y < 0 then
2017-05-26 19:12:35 +03:00
-- Standing on top
dir.y = 0.5
end
2017-05-26 18:43:07 +03:00
end
2017-05-26 19:12:35 +03:00
-- If node was not walkable, no modification to pos is made.
2017-05-26 18:43:07 +03:00
-- Final teleportation position
local telepos = vector.add ( pos , dir )
2017-05-26 19:33:30 +03:00
local telenode = minetest.get_node ( telepos )
--[[ It may be possible that telepos is walkable due to the algorithm.
Especially when the ender pearl is faster horizontally than vertical .
This applies final fixing , just to be sure we ' re not in a walkable node ]]
2017-06-29 14:02:53 +03:00
if not minetest.registered_nodes [ telenode.name ] or minetest.registered_nodes [ telenode.name ] . walkable then
2017-05-26 19:33:30 +03:00
if v.y < 0 then
telepos.y = telepos.y + 0.5
else
telepos.y = telepos.y - 2.3
end
end
2017-05-26 18:43:07 +03:00
2017-07-07 18:26:55 +03:00
local oldpos = player : getpos ( )
-- Teleport and hurt player
2017-05-26 18:43:07 +03:00
player : setpos ( telepos )
2017-01-25 13:45:17 +03:00
player : set_hp ( player : get_hp ( ) - 5 )
2017-05-26 18:43:07 +03:00
2017-07-07 18:26:55 +03:00
-- 5% chance to spawn endermite at the player's origin
local r = math.random ( 1 , 20 )
if r == 1 then
minetest.add_entity ( oldpos , " mobs_mc:endermite " )
end
2017-01-25 13:45:17 +03:00
end
2017-01-17 01:11:04 +03:00
self.object : remove ( )
2017-01-25 13:45:17 +03:00
return
2017-01-17 01:11:04 +03:00
end
end
2017-01-25 13:45:17 +03:00
self._lastpos = { x = pos.x , y = pos.y , z = pos.z } -- Set lastpos-->Node will be added at last pos outside the node
2017-01-17 01:11:04 +03:00
end
2017-02-20 03:51:09 +03:00
snowball_ENTITY.on_step = snowball_on_step
egg_ENTITY.on_step = egg_on_step
2017-01-25 13:45:17 +03:00
pearl_ENTITY.on_step = pearl_on_step
2017-01-17 01:11:04 +03:00
minetest.register_entity ( " mcl_throwing:snowball_entity " , snowball_ENTITY )
minetest.register_entity ( " mcl_throwing:egg_entity " , egg_ENTITY )
2017-01-25 13:45:17 +03:00
minetest.register_entity ( " mcl_throwing:ender_pearl_entity " , pearl_ENTITY )
2017-01-17 01:11:04 +03:00
2017-03-18 19:52:41 +03:00
local how_to_throw = " Hold it in your and and leftclick to throw. "
2017-01-17 01:11:04 +03:00
-- Snowball
minetest.register_craftitem ( " mcl_throwing:snowball " , {
description = " Snowball " ,
2017-03-18 19:52:41 +03:00
_doc_items_longdesc = " Snowballs can be thrown or launched from a dispenser for fun. Hitting something with a snowball does nothing. " ,
_doc_items_usagehelp = how_to_throw ,
2017-01-17 01:11:04 +03:00
inventory_image = " mcl_throwing_snowball.png " ,
stack_max = 16 ,
2017-02-15 01:18:23 +03:00
on_use = throw_function ( " mcl_throwing:snowball_entity " ) ,
2017-01-17 01:11:04 +03:00
} )
2017-01-25 13:45:17 +03:00
-- Egg
2017-01-17 01:11:04 +03:00
minetest.register_craftitem ( " mcl_throwing:egg " , {
description = " Egg " ,
2017-03-18 19:52:41 +03:00
_doc_items_longdesc = " Eggs can be thrown or launched from a dispenser and breaks on impact. There is a small chance that 1 or even 4 chickens will pop out of the egg when it hits the ground. " ,
_doc_items_usagehelp = how_to_throw ,
2017-01-17 01:11:04 +03:00
inventory_image = " mcl_throwing_egg.png " ,
stack_max = 16 ,
2017-02-15 01:18:23 +03:00
on_use = throw_function ( " mcl_throwing:egg_entity " ) ,
2017-01-20 13:33:56 +03:00
groups = { craftitem = 1 } ,
2017-01-17 01:11:04 +03:00
} )
2017-01-25 13:45:17 +03:00
-- Ender Pearl
minetest.register_craftitem ( " mcl_throwing:ender_pearl " , {
description = " Ender Pearl " ,
2017-05-26 18:57:39 +03:00
_doc_items_longdesc = " An ender pearl is an item which can be used for teleportation at the cost of health. It can be thrown and teleport the thrower to its impact location when it hits a solid block, a plant or vines. Each teleportation hurts the user by 5 hit points. " ,
2017-03-18 19:52:41 +03:00
_doc_items_usagehelp = how_to_throw ,
2017-01-25 13:45:17 +03:00
wield_image = " mcl_throwing_ender_pearl.png " ,
inventory_image = " mcl_throwing_ender_pearl.png " ,
stack_max = 16 ,
2017-02-15 01:18:23 +03:00
on_use = throw_function ( " mcl_throwing:ender_pearl_entity " ) ,
2017-01-25 13:45:17 +03:00
} )