2017-02-05 07:08:38 +01:00
-- internationalization boilerplate
local MP = minetest.get_modpath ( minetest.get_current_modname ( ) )
local S , NS = dofile ( MP .. " /intllib.lua " )
2017-10-13 06:53:30 +02:00
local modpath_awards = minetest.get_modpath ( " awards " )
2017-01-16 00:12:07 +01:00
minetest.register_node ( " digtron:empty_crate " , {
2017-02-05 07:08:38 +01:00
description = S ( " Digtron Crate (Empty) " ) ,
2017-01-18 03:57:20 +01:00
_doc_items_longdesc = digtron.doc . empty_crate_longdesc ,
_doc_items_usagehelp = digtron.doc . empty_crate_usagehelp ,
2017-01-16 00:12:07 +01:00
groups = { cracky = 3 , oddly_breakable_by_hand = 3 } ,
drop = " digtron:empty_crate " ,
sounds = default.node_sound_wood_defaults ( ) ,
tiles = { " digtron_crate.png " } ,
is_ground_content = false ,
drawtype = " nodebox " ,
paramtype = " light " ,
on_rightclick = function ( pos , node , clicker , itemstack , pointed_thing )
local layout = DigtronLayout.create ( pos , clicker )
if layout.contains_protected_node then
local meta = minetest.get_meta ( pos )
minetest.sound_play ( " buzzer " , { gain = 0.5 , pos = pos } )
2017-02-05 07:08:38 +01:00
meta : set_string ( " infotext " , S ( " Digtron can't be packaged, it contains protected blocks " ) )
2017-01-16 00:12:07 +01:00
-- no stealing other peoples' digtrons
return
end
2017-10-23 03:48:28 +02:00
if # layout.all == 1 then
local meta = minetest.get_meta ( pos )
minetest.sound_play ( " buzzer " , { gain = 0.5 , pos = pos } )
meta : set_string ( " infotext " , S ( " No Digtron components adjacent to package " ) )
return
end
2017-01-16 00:12:07 +01:00
2017-01-20 08:30:28 +01:00
digtron.award_crate ( layout , clicker : get_player_name ( ) )
2017-01-16 00:12:07 +01:00
local layout_string = layout : serialize ( )
-- destroy everything. Note that this includes the empty crate, which will be bundled up with the layout.
for _ , node_image in pairs ( layout.all ) do
2017-10-12 11:06:24 +02:00
local old_pos = node_image.pos
local old_node = node_image.node
minetest.remove_node ( old_pos )
2017-10-13 06:53:30 +02:00
if modpath_awards then
-- We're about to tell the awards mod that we're digging a node, but we
-- don't want it to count toward any actual awards. Pre-decrement.
local data = awards.players [ clicker : get_player_name ( ) ]
awards.increment_item_counter ( data , " count " , old_node.name , - 1 )
end
2017-10-12 11:06:24 +02:00
for _ , callback in ipairs ( minetest.registered_on_dignodes ) do
-- Copy pos and node because callback can modify them
local pos_copy = { x = old_pos.x , y = old_pos.y , z = old_pos.z }
local oldnode_copy = { name = old_node.name , param1 = old_node.param1 , param2 = old_node.param2 }
callback ( pos_copy , oldnode_copy , clicker )
end
2017-01-16 00:12:07 +01:00
end
-- Create the loaded crate node
minetest.set_node ( pos , { name = " digtron:loaded_crate " , param1 = node.param1 , param2 = node.param2 } )
2017-01-17 08:31:41 +01:00
minetest.sound_play ( " machine1 " , { gain = 1.0 , pos = pos } )
2017-01-16 00:12:07 +01:00
local meta = minetest.get_meta ( pos )
meta : set_string ( " crated_layout " , layout_string )
2017-02-05 07:08:38 +01:00
meta : set_string ( " title " , S ( " Crated Digtron " ) )
meta : set_string ( " infotext " , S ( " Crated Digtron " ) )
2017-01-16 00:12:07 +01:00
end ,
} )
2017-10-22 06:29:18 +02:00
local modpath_doc = minetest.get_modpath ( " doc " )
local loaded_formspec_string
if modpath_doc then
loaded_formspec_string =
2017-02-02 04:32:58 +01:00
" size[4.1,1.5] " ..
2017-01-16 00:12:07 +01:00
default.gui_bg ..
default.gui_bg_img ..
default.gui_slots ..
2017-02-05 07:08:38 +01:00
" field[0.3,0.5;4,0.5;title; " .. S ( " Digtron Name " ) .. " ;${title}] " ..
" button_exit[0.0,1.2;1,0.1;save; " .. S ( " Save \n Title " ) .. " ] " ..
" tooltip[save; " .. S ( " Saves the title of this Digtron " ) .. " ] " ..
" button_exit[1.0,1.2;1,0.1;show; " .. S ( " Show \n Blocks " ) .. " ] " ..
" tooltip[show; " .. S ( " Shows which blocks the packed Digtron will occupy if unpacked " ) .. " ] " ..
" button_exit[2.0,1.2;1,0.1;unpack; " .. S ( " Unpack " ) .. " ] " ..
" tooltip[unpack; " .. S ( " Attempts to unpack the Digtron on this location " ) .. " ] " ..
" button_exit[3.0,1.2;1,0.1;help; " .. S ( " Help " ) .. " ] " ..
" tooltip[help; " .. S ( " Show documentation about this block " ) .. " ] "
2017-02-02 04:32:58 +01:00
else
2017-10-22 06:29:18 +02:00
loaded_formspec_string =
2017-02-02 04:32:58 +01:00
" size[4,1.5] " ..
default.gui_bg ..
default.gui_bg_img ..
default.gui_slots ..
2017-02-05 07:08:38 +01:00
" field[0.3,0.5;4,0.5;title; " .. S ( " Digtron Name " ) .. " ;${title}] " ..
" button_exit[0.5,1.2;1,0.1;save; " .. S ( " Save \n Title " ) .. " ] " ..
" tooltip[show; " .. S ( " Saves the title of this Digtron " ) .. " ] " ..
" button_exit[1.5,1.2;1,0.1;show; " .. S ( " Show \n Blocks " ) .. " ] " ..
" tooltip[save; " .. S ( " Shows which blocks the packed Digtron will occupy if unpacked " ) .. " ] " ..
" button_exit[2.5,1.2;1,0.1;unpack; " .. S ( " Unpack " ) .. " ] " ..
" tooltip[unpack; " .. S ( " Attempts to unpack the Digtron on this location " ) .. " ] "
2017-02-02 04:32:58 +01:00
end
2017-01-16 00:12:07 +01:00
2017-10-22 06:29:18 +02:00
local loaded_formspec = function ( pos , meta )
return loaded_formspec_string
end
2017-01-16 00:12:07 +01:00
minetest.register_node ( " digtron:loaded_crate " , {
2017-02-05 07:08:38 +01:00
description = S ( " Digtron Crate (Loaded) " ) ,
2017-01-18 03:57:20 +01:00
_doc_items_longdesc = digtron.doc . loaded_crate_longdesc ,
_doc_items_usagehelp = digtron.doc . loaded_crate_usagehelp ,
2017-09-20 06:57:40 +02:00
_digtron_formspec = loaded_formspec ,
2017-02-02 03:38:00 +01:00
groups = { cracky = 3 , oddly_breakable_by_hand = 3 , not_in_creative_inventory = 1 , digtron_protected = 1 } ,
2017-01-16 00:12:07 +01:00
stack_max = 1 ,
sounds = default.node_sound_wood_defaults ( ) ,
tiles = { " digtron_plate.png^digtron_crate.png " } ,
is_ground_content = false ,
on_construct = function ( pos )
local meta = minetest.get_meta ( pos )
2017-10-22 06:29:18 +02:00
meta : set_string ( " formspec " , loaded_formspec ( pos , meta ) )
2017-01-16 00:12:07 +01:00
end ,
on_receive_fields = function ( pos , formname , fields , sender )
local meta = minetest.get_meta ( pos )
2017-01-17 08:56:54 +01:00
if fields.unpack or fields.save or fields.show then
2017-01-16 00:12:07 +01:00
meta : set_string ( " title " , fields.title )
meta : set_string ( " infotext " , fields.title )
end
2017-02-02 04:32:58 +01:00
if fields.help and minetest.get_modpath ( " doc " ) then --check for mod in case someone disabled it after this digger was built
2017-09-11 07:24:09 +02:00
minetest.after ( 0.5 , doc.show_entry , sender : get_player_name ( ) , " nodes " , " digtron:loaded_crate " , true )
2017-02-02 04:32:58 +01:00
end
2017-01-17 08:56:54 +01:00
if not ( fields.unpack or fields.show ) then
2017-01-16 00:12:07 +01:00
return
end
local layout_string = meta : get_string ( " crated_layout " )
local layout = DigtronLayout.deserialize ( layout_string )
2017-01-17 08:56:54 +01:00
2017-01-16 00:12:07 +01:00
if layout == nil then
2017-02-05 07:08:38 +01:00
meta : set_string ( " infotext " , meta : get_string ( " title " ) .. " \n " .. S ( " Unable to read layout from crate metadata, regrettably this Digtron may be corrupted or lost. " ) )
2017-01-16 00:12:07 +01:00
minetest.sound_play ( " buzzer " , { gain = 0.5 , pos = pos } )
-- Something went horribly wrong
return
end
2017-01-18 03:57:20 +01:00
local protected_node = false
local obstructed_node = false
local pos_diff = vector.subtract ( pos , layout.controller )
layout.controller = pos
2017-01-17 08:56:54 +01:00
for _ , node_image in pairs ( layout.all ) do
2017-01-18 03:57:20 +01:00
node_image.pos = vector.add ( pos_diff , node_image.pos )
2017-01-17 08:56:54 +01:00
if not vector.equals ( pos , node_image.pos ) then
2017-01-18 03:57:20 +01:00
if minetest.is_protected ( node_image.pos , sender : get_player_name ( ) ) and not minetest.check_player_privs ( sender , " protection_bypass " ) then
protected_node = true
minetest.add_entity ( node_image.pos , " digtron:marker_crate_bad " )
elseif not minetest.registered_nodes [ minetest.get_node ( node_image.pos ) . name ] . buildable_to then
obstructed_node = true
minetest.add_entity ( node_image.pos , " digtron:marker_crate_bad " )
else
minetest.add_entity ( node_image.pos , " digtron:marker_crate_good " )
end
2017-01-17 08:56:54 +01:00
end
end
if not fields.unpack then
return
end
2017-01-18 03:57:20 +01:00
if protected_node then
2017-02-05 07:08:38 +01:00
meta : set_string ( " infotext " , meta : get_string ( " title " ) .. " \n " .. S ( " Unable to deploy Digtron due to protected blocks in target area " ) )
2017-01-18 03:57:20 +01:00
minetest.sound_play ( " buzzer " , { gain = 0.5 , pos = pos } )
return
2017-01-16 00:12:07 +01:00
end
2017-01-18 03:57:20 +01:00
if obstructed_node then
2017-02-05 07:08:38 +01:00
meta : set_string ( " infotext " , meta : get_string ( " title " ) .. " \n " .. S ( " Unable to deploy Digtron due to obstruction in target area " ) )
2017-01-18 03:57:20 +01:00
minetest.sound_play ( " buzzer " , { gain = 0.5 , pos = pos } )
return
2017-01-16 00:12:07 +01:00
end
2017-01-18 03:57:20 +01:00
-- build digtron. Since the empty crate was included in the layout, that will overwrite this loaded crate and destroy it.
minetest.sound_play ( " machine2 " , { gain = 1.0 , pos = pos } )
layout : write_layout_image ( sender )
2017-01-16 00:12:07 +01:00
end ,
on_dig = function ( pos , node , player )
local meta = minetest.get_meta ( pos )
local to_serialize = { title = meta : get_string ( " title " ) , layout = meta : get_string ( " crated_layout " ) }
local stack = ItemStack ( { name = " digtron:loaded_crate " , count = 1 , wear = 0 , metadata = minetest.serialize ( to_serialize ) } )
local inv = player : get_inventory ( )
local stack = inv : add_item ( " main " , stack )
if stack : get_count ( ) > 0 then
minetest.add_item ( pos , stack )
end
-- call on_dignodes callback
minetest.remove_node ( pos )
end ,
on_place = function ( itemstack , placer , pointed_thing )
local pos = minetest.get_pointed_thing_position ( pointed_thing , true )
local deserialized = minetest.deserialize ( itemstack : get_metadata ( ) )
if pos and deserialized then
minetest.set_node ( pos , { name = " digtron:loaded_crate " } )
local meta = minetest.get_meta ( pos )
meta : set_string ( " crated_layout " , deserialized.layout )
meta : set_string ( " title " , deserialized.title )
meta : set_string ( " infotext " , deserialized.title )
2017-10-22 06:29:18 +02:00
--meta:set_string("formspec", loaded_formspec(pos, meta)) -- not needed, on_construct handles this
2017-01-16 00:12:07 +01:00
itemstack : take_item ( 1 )
return itemstack
end
-- after-place callbacks
end ,
} )