From 10307f23a78b33af50dc4a5f3d1baafb4ee4b0d9 Mon Sep 17 00:00:00 2001
From: Maciej 'agaran' Pijanka <agaran@pld-linux.org>
Date: Thu, 16 Mar 2017 00:00:22 +0100
Subject: [PATCH] Do not run converters twice.

---
 technic/machines/supply_converter.lua |  155 +++++++++++++++++++++++++++++++++++----------------
 1 files changed, 106 insertions(+), 49 deletions(-)

diff --git a/technic/machines/supply_converter.lua b/technic/machines/supply_converter.lua
index fd59401..a94b9e2 100644
--- a/technic/machines/supply_converter.lua
+++ b/technic/machines/supply_converter.lua
@@ -9,70 +9,127 @@
 
 local S = technic.getter
 
+local function set_supply_converter_formspec(meta)
+	local formspec = "size[5,2.25]"..
+		"field[0.3,0.5;2,1;power;"..S("Input Power")..";"..meta:get_int("power").."]"
+	-- The names for these toggle buttons are explicit about which
+	-- state they'll switch to, so that multiple presses (arising
+	-- from the ambiguity between lag and a missed press) only make
+	-- the single change that the user expects.
+	if meta:get_int("mesecon_mode") == 0 then
+		formspec = formspec.."button[0,1;5,1;mesecon_mode_1;"..S("Ignoring Mesecon Signal").."]"
+	else
+		formspec = formspec.."button[0,1;5,1;mesecon_mode_0;"..S("Controlled by Mesecon Signal").."]"
+	end
+	if meta:get_int("enabled") == 0 then
+		formspec = formspec.."button[0,1.75;5,1;enable;"..S("%s Disabled"):format(S("Supply Converter")).."]"
+	else
+		formspec = formspec.."button[0,1.75;5,1;disable;"..S("%s Enabled"):format(S("Supply Converter")).."]"
+	end
+	meta:set_string("formspec", formspec)
+end
+
+local supply_converter_receive_fields = function(pos, formname, fields, sender)
+	local meta = minetest.get_meta(pos)
+	local power = nil
+	if fields.power then
+		power = tonumber(fields.power) or 0
+		power = 100 * math.floor(power / 100)
+		power = math.max(power, 0)
+		power = math.min(power, 10000)
+		if power == meta:get_int("power") then power = nil end
+	end
+	if power then meta:set_int("power", power) end
+	if fields.enable then meta:set_int("enabled", 1) end
+	if fields.disable then meta:set_int("enabled", 0) end
+	if fields.mesecon_mode_0 then meta:set_int("mesecon_mode", 0) end
+	if fields.mesecon_mode_1 then meta:set_int("mesecon_mode", 1) end
+	set_supply_converter_formspec(meta)
+end
+
+local mesecons = {
+	effector = {
+		action_on = function(pos, node)
+			minetest.get_meta(pos):set_int("mesecon_effect", 1)
+		end,
+		action_off = function(pos, node)
+			minetest.get_meta(pos):set_int("mesecon_effect", 0)
+		end
+	}
+}
+
+local run = function(pos, node, run_stage)
+	-- run only in producer stage.
+	if run_stage == technic.receiver then
+		return
+	end
+
+	local remain = 0.9
+	-- Machine information
+	local machine_name  = S("Supply Converter")
+	local meta          = minetest.get_meta(pos)
+	local enabled = meta:get_int("enabled") ~= 0 and (meta:get_int("mesecon_mode") == 0 or meta:get_int("mesecon_effect") ~= 0)
+	local demand = enabled and meta:get_int("power") or 0
+
+	local pos_up        = {x=pos.x, y=pos.y+1, z=pos.z}
+	local pos_down      = {x=pos.x, y=pos.y-1, z=pos.z}
+	local name_up       = minetest.get_node(pos_up).name
+	local name_down     = minetest.get_node(pos_down).name
+
+	local from = technic.get_cable_tier(name_up)
+	local to   = technic.get_cable_tier(name_down)
+
+	if from and to then
+		local input = meta:get_int(from.."_EU_input")
+		meta:set_int(from.."_EU_demand", demand)
+		meta:set_int(from.."_EU_supply", 0)
+		meta:set_int(to.."_EU_demand", 0)
+		meta:set_int(to.."_EU_supply", input * remain)
+		meta:set_string("infotext", S("@1 (@2 @3 -> @4 @5)", machine_name, technic.pretty_num(input), from, technic.pretty_num(input * remain), to))
+	else
+		meta:set_string("infotext", S("%s Has Bad Cabling"):format(machine_name))
+		if to then
+			meta:set_int(to.."_EU_supply", 0)
+		end
+		if from then
+			meta:set_int(from.."_EU_demand", 0)
+		end
+		return
+	end
+
+end
+
 minetest.register_node("technic:supply_converter", {
 	description = S("Supply Converter"),
 	tiles  = {"technic_supply_converter_top.png", "technic_supply_converter_bottom.png",
 	          "technic_supply_converter_side.png", "technic_supply_converter_side.png",
 	          "technic_supply_converter_side.png", "technic_supply_converter_side.png"},
-	groups = {snappy=2, choppy=2, oddly_breakable_by_hand=2},
+	groups = {snappy=2, choppy=2, oddly_breakable_by_hand=2,
+		technic_machine=1, technic_all_tiers=1},
+	connect_sides = {"top", "bottom"},
 	sounds = default.node_sound_wood_defaults(),
-	drawtype = "nodebox",
-	paramtype = "light",
-	node_box = {
-		type = "fixed",
-		fixed = {-0.5, -0.5, -0.5, 0.5, 0.5, 0.5},
-	},
+	on_receive_fields = supply_converter_receive_fields,
 	on_construct = function(pos)
-		local meta = minetest.env:get_meta(pos)
+		local meta = minetest.get_meta(pos)
 		meta:set_string("infotext", S("Supply Converter"))
-		meta:set_float("active", false)
+		meta:set_int("power", 10000)
+		meta:set_int("enabled", 1)
+		meta:set_int("mesecon_mode", 0)
+		meta:set_int("mesecon_effect", 0)
+		set_supply_converter_formspec(meta)
 	end,
+	mesecons = mesecons,
+	technic_run = run,
+	technic_on_disable = run,
 })
 
 minetest.register_craft({
 	output = 'technic:supply_converter 1',
 	recipe = {
-		{'technic:stainless_steel_ingot', 'technic:rubber',         'technic:stainless_steel_ingot'},
-		{'technic:mv_transformer',        'technic:machine_casing', 'technic:lv_transformer'},
-		{'technic:mv_cable0',             'technic:rubber',         'technic:lv_cable0'},
+		{'technic:fine_gold_wire', 'technic:rubber',         'technic:doped_silicon_wafer'},
+		{'technic:mv_transformer', 'technic:machine_casing', 'technic:lv_transformer'},
+		{'technic:mv_cable',       'technic:rubber',         'technic:lv_cable'},
 	}
-})
-
-minetest.register_abm({
-	nodenames = {"technic:supply_converter"},
-	interval   = 1,
-	chance     = 1,
-	action = function(pos, node, active_object_count, active_object_count_wider)
-		local demand = 10000
-		local remain = 0.9
-		-- Machine information
-		local machine_name  = S("Supply Converter")
-		local meta          = minetest.get_meta(pos)
-
-		local pos_up        = {x=pos.x, y=pos.y+1, z=pos.z}
-		local pos_down      = {x=pos.x, y=pos.y-1, z=pos.z}
-		local name_up       = minetest.get_node(pos_up).name
-		local name_down     = minetest.get_node(pos_down).name
-
-		local from = technic.get_cable_tier(name_up)
-		local to   = technic.get_cable_tier(name_down)
-
-		if from and to then
-			technic.switching_station_timeout_count(pos, from)
-			local input = meta:get_int(from.."_EU_input")
-			meta:set_int(from.."_EU_demand", demand)
-			meta:set_int(from.."_EU_supply", 0)
-			meta:set_int(to.."_EU_demand", 0)
-			meta:set_int(to.."_EU_supply", input * remain)
-			meta:set_string("infotext", machine_name
-				.." ("..input.." "..from.." -> "
-				..input * remain.." "..to..")")
-		else
-			meta:set_string("infotext", S("%s Has Bad Cabling"):format(machine_name))
-			return
-		end
-
-	end,
 })
 
 for tier, machines in pairs(technic.machines) do

--
Gitblit v1.8.0