2016-12-31 07:38:18 +01:00
local controller_nodebox = {
{ - 0.3125 , - 0.3125 , - 0.3125 , 0.3125 , 0.3125 , 0.3125 } , -- Core
{ - 0.1875 , 0.3125 , - 0.1875 , 0.1875 , 0.5 , 0.1875 } , -- +y_connector
{ - 0.1875 , - 0.5 , - 0.1875 , 0.1875 , - 0.3125 , 0.1875 } , -- -y_Connector
{ 0.3125 , - 0.1875 , - 0.1875 , 0.5 , 0.1875 , 0.1875 } , -- +x_connector
{ - 0.5 , - 0.1875 , - 0.1875 , - 0.3125 , 0.1875 , 0.1875 } , -- -x_connector
{ - 0.1875 , - 0.1875 , 0.3125 , 0.1875 , 0.1875 , 0.5 } , -- +z_connector
{ - 0.5 , 0.125 , - 0.5 , - 0.125 , 0.5 , - 0.3125 } , -- back_connector_3
{ 0.125 , 0.125 , - 0.5 , 0.5 , 0.5 , - 0.3125 } , -- back_connector_1
{ 0.125 , - 0.5 , - 0.5 , 0.5 , - 0.125 , - 0.3125 } , -- back_connector_2
{ - 0.5 , - 0.5 , - 0.5 , - 0.125 , - 0.125 , - 0.3125 } , -- back_connector_4
}
-- Master controller. Most complicated part of the whole system. Determines which direction a digtron moves and triggers all of its component parts.
minetest.register_node ( " digtron:controller " , {
description = " Digtron Control Unit " ,
2017-01-03 04:07:15 +01:00
groups = { cracky = 3 , oddly_breakable_by_hand = 3 , digtron = 1 } ,
2017-01-04 03:43:32 +01:00
drop = " digtron:controller " ,
2017-01-04 07:56:34 +01:00
sounds = digtron.metal_sounds ,
2017-01-06 07:52:09 +01:00
paramtype = " light " ,
2017-01-04 03:43:32 +01:00
paramtype2 = " facedir " ,
2017-01-06 07:52:09 +01:00
is_ground_content = false ,
2016-12-31 07:38:18 +01:00
-- Aims in the +Z direction by default
tiles = {
" digtron_plate.png^[transformR90 " ,
" digtron_plate.png^[transformR270 " ,
" digtron_plate.png " ,
" digtron_plate.png^[transformR180 " ,
" digtron_plate.png " ,
2017-01-11 06:59:11 +01:00
" digtron_plate.png^digtron_control.png " ,
2016-12-31 07:38:18 +01:00
} ,
drawtype = " nodebox " ,
node_box = {
type = " fixed " ,
fixed = controller_nodebox ,
} ,
2017-01-02 01:12:32 +01:00
on_construct = function ( pos )
2017-01-04 03:43:32 +01:00
local meta = minetest.get_meta ( pos )
2017-01-02 01:12:32 +01:00
meta : set_float ( " fuel_burning " , 0.0 )
meta : set_string ( " infotext " , " Heat remaining in controller furnace: 0 " )
end ,
2016-12-31 07:38:18 +01:00
on_rightclick = function ( pos , node , clicker , itemstack , pointed_thing )
2017-01-04 07:03:41 +01:00
local meta = minetest.get_meta ( pos )
if meta : get_string ( " waiting " ) == " true " then
-- Been too soon since last time the digtron did a cycle.
return
end
2017-01-09 05:34:27 +01:00
local newpos , status , return_code = digtron.execute_dig_cycle ( pos , clicker )
2017-01-04 07:03:41 +01:00
meta = minetest.get_meta ( newpos )
2017-01-03 04:07:15 +01:00
if status ~= nil then
meta : set_string ( " infotext " , status )
2016-12-31 07:38:18 +01:00
end
2017-01-04 07:03:41 +01:00
2017-01-07 17:52:39 +01:00
-- Start the delay before digtron can run again.
2017-01-04 07:03:41 +01:00
minetest.get_meta ( newpos ) : set_string ( " waiting " , " true " )
2017-01-08 09:23:10 +01:00
minetest.get_node_timer ( newpos ) : start ( digtron.cycle_time )
end ,
on_timer = function ( pos , elapsed )
minetest.get_meta ( pos ) : set_string ( " waiting " , nil )
2016-12-31 07:38:18 +01:00
end ,
} )
2017-01-07 17:52:39 +01:00
-- Auto-controller
2017-01-04 07:03:41 +01:00
---------------------------------------------------------------------------------------------------------------
local auto_formspec = " size[4.5,1] " ..
default.gui_bg ..
default.gui_bg_img ..
default.gui_slots ..
" field[0.5,0.8;1,0.1;offset;Cycles;${offset}] " ..
" tooltip[offset;When triggered, this controller will try to run for the given number of cycles. The cycle count will decrement as it runs, so if it gets halted by a problem you can fix the problem and restart.] " ..
" field[1.5,0.8;1,0.1;period;Period;${period}] " ..
" tooltip[period;Number of seconds to wait between each cycle] " ..
" button_exit[2.2,0.5;1,0.1;set;Set] " ..
" tooltip[set;Saves the cycle setting without starting the controller running] " ..
" button_exit[3.2,0.5;1,0.1;execute;Set & \n Execute] " ..
" tooltip[execute;Begins executing the given number of cycles] "
-- Needed to make this global so that it could recurse into minetest.after
digtron.auto_cycle = function ( pos )
local meta = minetest.get_meta ( pos )
local player = minetest.get_player_by_name ( meta : get_string ( " triggering_player " ) )
if player == nil or meta : get_string ( " waiting " ) == " true " then
return
end
2017-01-09 05:34:27 +01:00
local newpos , status , return_code = digtron.execute_dig_cycle ( pos , player )
2017-01-04 07:03:41 +01:00
local cycle = 0
if vector.equals ( pos , newpos ) then
cycle = meta : get_int ( " offset " )
status = status .. string.format ( " \n Cycles remaining: %d \n Halted! " , cycle )
meta : set_string ( " infotext " , status )
2017-01-08 01:00:15 +01:00
if return_code == 1 then --return code 1 happens when there's unloaded nodes adjacent, just keep trying.
minetest.after ( meta : get_int ( " period " ) , digtron.auto_cycle , newpos )
else
meta : set_string ( " formspec " , auto_formspec )
end
2017-01-04 07:03:41 +01:00
return
end
meta = minetest.get_meta ( newpos )
cycle = meta : get_int ( " offset " ) - 1
meta : set_int ( " offset " , cycle )
status = status .. string.format ( " \n Cycles remaining: %d " , cycle )
meta : set_string ( " infotext " , status )
if cycle > 0 then
2017-01-04 07:56:34 +01:00
minetest.after ( meta : get_int ( " period " ) , digtron.auto_cycle , newpos )
2017-01-04 07:03:41 +01:00
else
meta : set_string ( " formspec " , auto_formspec )
end
end
minetest.register_node ( " digtron:auto_controller " , {
description = " Digtron Automatic Control Unit " ,
groups = { cracky = 3 , oddly_breakable_by_hand = 3 , digtron = 1 } ,
drop = " digtron:auto_controller " ,
2017-01-04 07:56:34 +01:00
sounds = digtron.metal_sounds ,
2017-01-06 07:52:09 +01:00
paramtype = " light " ,
2017-01-04 07:03:41 +01:00
paramtype2 = " facedir " ,
2017-01-06 07:52:09 +01:00
is_ground_content = false ,
2017-01-04 07:03:41 +01:00
-- Aims in the +Z direction by default
tiles = {
2017-01-11 16:02:40 +01:00
" digtron_plate.png^[transformR90^digtron_auto_control_tint.png " ,
" digtron_plate.png^[transformR270^digtron_auto_control_tint.png " ,
" digtron_plate.png^digtron_auto_control_tint.png " ,
" digtron_plate.png^[transformR180^digtron_auto_control_tint.png " ,
" digtron_plate.png^digtron_auto_control_tint.png " ,
" digtron_plate.png^digtron_control.png^digtron_auto_control_tint.png " ,
2017-01-04 07:03:41 +01:00
} ,
drawtype = " nodebox " ,
node_box = {
type = " fixed " ,
fixed = controller_nodebox ,
} ,
on_construct = function ( pos )
local meta = minetest.get_meta ( pos )
meta : set_float ( " fuel_burning " , 0.0 )
meta : set_string ( " infotext " , " Heat remaining in controller furnace: 0 " )
meta : set_string ( " formspec " , auto_formspec )
-- Reusing offset and period to keep the digtron node-moving code simple, and the names still fit well
2017-01-04 07:56:34 +01:00
meta : set_int ( " period " , digtron.cycle_time )
2017-01-04 07:03:41 +01:00
meta : set_int ( " offset " , 0 )
end ,
on_receive_fields = function ( pos , formname , fields , sender )
local meta = minetest.get_meta ( pos )
local offset = tonumber ( fields.offset )
local period = tonumber ( fields.period )
if period and period > 0 then
2017-01-04 07:56:34 +01:00
meta : set_int ( " period " , math.max ( digtron.cycle_time , math.floor ( period ) ) )
2017-01-04 07:03:41 +01:00
end
if offset and offset >= 0 then
meta : set_int ( " offset " , math.floor ( offset ) )
if sender : is_player ( ) and offset > 0 then
meta : set_string ( " triggering_player " , sender : get_player_name ( ) )
if fields.execute then
meta : set_string ( " waiting " , nil )
meta : set_string ( " formspec " , nil )
digtron.auto_cycle ( pos )
end
end
end
end ,
on_rightclick = function ( pos , node , clicker , itemstack , pointed_thing )
local meta = minetest.get_meta ( pos )
meta : set_string ( " infotext " , meta : get_string ( " infotext " ) .. " \n Interrupted! " )
meta : set_string ( " waiting " , " true " )
meta : set_string ( " formspec " , auto_formspec )
end ,
2017-01-08 09:23:10 +01:00
on_timer = function ( pos , elapsed )
minetest.get_meta ( pos ) : set_string ( " waiting " , nil )
end ,
2017-01-04 07:03:41 +01:00
} )
---------------------------------------------------------------------------------------------------------------
2016-12-31 07:38:18 +01:00
-- A much simplified control unit that only moves the digtron, and doesn't trigger the diggers or builders.
-- Handy for shoving a digtron to the side if it's been built a bit off.
minetest.register_node ( " digtron:pusher " , {
description = " Digtron Pusher Unit " ,
2017-01-03 04:07:15 +01:00
groups = { cracky = 3 , oddly_breakable_by_hand = 3 , digtron = 1 } ,
2017-01-04 03:43:32 +01:00
drop = " digtron:pusher " ,
2017-01-04 07:56:34 +01:00
sounds = digtron.metal_sounds ,
2017-01-06 07:52:09 +01:00
paramtype = " light " ,
2017-01-04 03:43:32 +01:00
paramtype2 = " facedir " ,
2017-01-06 07:52:09 +01:00
is_ground_content = false ,
2016-12-31 07:38:18 +01:00
-- Aims in the +Z direction by default
tiles = {
2017-01-11 16:02:40 +01:00
" digtron_plate.png^[transformR90^digtron_pusher_tint.png " ,
" digtron_plate.png^[transformR270^digtron_pusher_tint.png " ,
" digtron_plate.png^digtron_pusher_tint.png " ,
" digtron_plate.png^[transformR180^digtron_pusher_tint.png " ,
" digtron_plate.png^digtron_pusher_tint.png " ,
" digtron_plate.png^digtron_control.png^digtron_pusher_tint.png " ,
2016-12-31 07:38:18 +01:00
} ,
drawtype = " nodebox " ,
node_box = {
type = " fixed " ,
fixed = controller_nodebox ,
} ,
on_rightclick = function ( pos , node , clicker , itemstack , pointed_thing )
local meta = minetest.get_meta ( pos )
if meta : get_string ( " waiting " ) == " true " then
-- Been too soon since last time the digtron did a cycle.
return
end
2017-01-09 05:34:27 +01:00
local newpos , status_text , return_code = digtron.execute_move_cycle ( pos , clicker )
2017-01-10 08:29:09 +01:00
meta = minetest.get_meta ( newpos )
2017-01-09 05:34:27 +01:00
meta : set_string ( " infotext " , status_text )
2016-12-31 07:38:18 +01:00
2017-01-09 05:34:27 +01:00
-- Start the delay before digtron can run again.
minetest.get_meta ( newpos ) : set_string ( " waiting " , " true " )
minetest.get_node_timer ( newpos ) : start ( digtron.cycle_time )
2017-01-08 09:23:10 +01:00
end ,
on_timer = function ( pos , elapsed )
minetest.get_meta ( pos ) : set_string ( " waiting " , nil )
2016-12-31 07:38:18 +01:00
end ,
2017-01-08 09:23:10 +01:00
2016-12-31 07:38:18 +01:00
} )