From 6a08071d86ee4e83ce9ccc67c5e06010893cb60c Mon Sep 17 00:00:00 2001 From: Novatux Date: Fri, 23 May 2014 19:51:23 +0200 Subject: [PATCH] Upgrades to battery boxes too, prevent frames from moving too often (one move every second max) --- technic/machines/HV/battery_box.lua | 2 + technic/machines/MV/battery_box.lua | 2 + technic/machines/other/frames.lua | 8 ++ technic/machines/register/battery_box.lua | 103 ++++++++++++++++++---- technic/machines/register/common.lua | 18 ++-- 5 files changed, 110 insertions(+), 23 deletions(-) diff --git a/technic/machines/HV/battery_box.lua b/technic/machines/HV/battery_box.lua index b009d80..1e054ec 100644 --- a/technic/machines/HV/battery_box.lua +++ b/technic/machines/HV/battery_box.lua @@ -15,5 +15,7 @@ technic.register_battery_box({ discharge_rate = 400000, charge_step = 10000, discharge_step = 40000, + upgrade = 1, + tube = 1, }) diff --git a/technic/machines/MV/battery_box.lua b/technic/machines/MV/battery_box.lua index 6a996b8..45437c1 100644 --- a/technic/machines/MV/battery_box.lua +++ b/technic/machines/MV/battery_box.lua @@ -16,5 +16,7 @@ technic.register_battery_box({ discharge_rate = 80000, charge_step = 2000, discharge_step = 8000, + upgrade = 1, + tube = 1, }) diff --git a/technic/machines/other/frames.lua b/technic/machines/other/frames.lua index 2a5a554..427e60b 100644 --- a/technic/machines/other/frames.lua +++ b/technic/machines/other/frames.lua @@ -542,11 +542,15 @@ local function frame_motor_on(pos, node) nnode.name = frames_pos[pos_to_string(nnodepos)] end local meta = minetest.get_meta(pos) + if meta:get_int("last_moved") == minetest.get_gametime() then + return + end local owner = meta:get_string("owner") if minetest.registered_nodes[nnode.name].frame==1 then local connected_nodes=get_connected_nodes(nnodepos) move_nodes_vect(connected_nodes,dir,pos,owner) end + minetest.get_meta(vector.add(pos, dir)):set_int("last_moved", minetest.get_gametime()) end minetest.register_node("technic:frame_motor",{ @@ -843,11 +847,15 @@ local function template_motor_on(pos, node) local dir = minetest.facedir_to_dir(node.param2) local nnode=minetest.get_node(nnodepos) local meta = minetest.get_meta(pos) + if meta:get_int("last_moved") == minetest.get_gametime() then + return + end local owner = meta:get_string("owner") if nnode.name == "technic:template" then local connected_nodes=get_template_nodes(nnodepos) move_nodes_vect(connected_nodes,dir,pos,owner) end + minetest.get_meta(vector.add(pos, dir)):set_int("last_moved", minetest.get_gametime()) end minetest.register_node("technic:template_motor",{ diff --git a/technic/machines/register/battery_box.lua b/technic/machines/register/battery_box.lua index 05c14f1..c8b3337 100644 --- a/technic/machines/register/battery_box.lua +++ b/technic/machines/register/battery_box.lua @@ -29,6 +29,33 @@ minetest.register_tool("technic:battery", { } }) +local tube = { + insert_object = function(pos, node, stack, direction) + if direction.y == 0 then + return stack + end + local meta = minetest.get_meta(pos) + local inv = meta:get_inventory() + if direction.y > 0 then + return inv:add_item("src", stack) + else + return inv:add_item("dst", stack) + end + end, + can_insert = function(pos, node, stack, direction) + if direction.y == 0 then + return false + end + local meta = minetest.get_meta(pos) + local inv = meta:get_inventory() + if direction.y > 0 then + return inv:room_for_item("src", stack) + else + return inv:room_for_item("dst", stack) + end + end, + connect_sides = {left=1, right=1, back=1, top=1, bottom=1}, +} function technic.register_battery_box(data) local tier = data.tier @@ -45,12 +72,25 @@ function technic.register_battery_box(data) "label[5,0;"..S("Discharge").."]".. "label[1,3;"..S("Power level").."]".. "list[current_player;main;0,5;8,4;]" + + if data.upgrade then + formspec = formspec.. + "list[current_name;upgrade1;3.5,3;1,1;]".. + "list[current_name;upgrade2;4.5,3;1,1;]".. + "label[3.5,4;"..S("Upgrade Slots").."]" + end for i = 0, 8 do local groups = {snappy=2, choppy=2, oddly_breakable_by_hand=2} if i ~= 0 then groups.not_in_creative_inventory = 1 end + + if data.tube then + groups.tubedevice = 1 + groups.tubedevice_receiver = 1 + end + minetest.register_node("technic:"..ltier.."_battery_box"..i, { description = S("%s Battery Box"):format(tier), tiles = {"technic_"..ltier.."_battery_box_top.png", @@ -60,6 +100,8 @@ function technic.register_battery_box(data) "technic_"..ltier.."_battery_box_side.png^technic_power_meter"..i..".png", "technic_"..ltier.."_battery_box_side.png^technic_power_meter"..i..".png"}, groups = groups, + tube = data.tube and tube or nil, + paramtype2 = "facedir", sounds = default.node_sound_wood_defaults(), drop = "technic:"..ltier.."_battery_box0", on_construct = function(pos) @@ -75,6 +117,8 @@ function technic.register_battery_box(data) meta:set_float("internal_EU_charge", 0) inv:set_size("src", 1) inv:set_size("dst", 1) + inv:set_size("upgrade1", 1) + inv:set_size("upgrade2", 1) end, can_dig = technic.machine_can_dig, allow_metadata_inventory_put = technic.machine_inventory_put, @@ -100,30 +144,49 @@ function technic.register_battery_box(data) -- Power off automatically if no longer connected to a switching station technic.switching_station_timeout_count(pos, tier) + local EU_upgrade, tube_upgrade = 0, 0 + if data.upgrade then + EU_upgrade, tube_upgrade = technic.handle_machine_upgrades(meta) + end + local max_charge = data.max_charge * (1 + EU_upgrade / 10) + -- Charge/discharge the battery with the input EUs if eu_input >= 0 then - current_charge = math.min(current_charge + eu_input, data.max_charge) + current_charge = math.min(current_charge + eu_input, max_charge) else current_charge = math.max(current_charge + eu_input, 0) end -- Charging/discharging tools here - current_charge = technic.charge_tools(meta, + local tool_full, tool_empty + current_charge, tool_full = technic.charge_tools(meta, current_charge, data.charge_step) - current_charge = technic.discharge_tools(meta, + current_charge, tool_empty = technic.discharge_tools(meta, current_charge, data.discharge_step, - data.max_charge) + max_charge) + + if data.tube then + local inv = meta:get_inventory() + technic.handle_machine_pipeworks(pos, tube_upgrade, + function(pos, x_velocity, z_velocity) + if tool_full and not inv:is_empty("src") then + technic.send_items(pos, x_velocity, z_velocity, "src") + elseif tool_empty and not inv:is_empty("dst") then + technic.send_items(pos, x_velocity, z_velocity, "dst") + end + end) + end -- We allow batteries to charge on less than the demand meta:set_int(tier.."_EU_demand", - math.min(data.charge_rate, data.max_charge - current_charge)) + math.min(data.charge_rate, max_charge - current_charge)) meta:set_int(tier.."_EU_supply", math.min(data.discharge_rate, current_charge)) meta:set_int("internal_EU_charge", current_charge) -- Select node textures - local charge_count = math.ceil((current_charge / data.max_charge) * 8) + local charge_count = math.ceil((current_charge / max_charge) * 8) charge_count = math.min(charge_count, 8) charge_count = math.max(charge_count, 0) local last_count = meta:get_float("last_side_shown") @@ -132,7 +195,7 @@ function technic.register_battery_box(data) meta:set_float("last_side_shown", charge_count) end - local charge_percent = math.floor(current_charge / data.max_charge * 100) + local charge_percent = math.floor(current_charge / max_charge * 100) meta:set_string("formspec", formspec.. "image[1,1;1,2;technic_power_meter_bg.png" @@ -140,7 +203,7 @@ function technic.register_battery_box(data) ..":technic_power_meter_fg.png]") local infotext = S("%s Battery Box: %d/%d"):format(tier, - current_charge, data.max_charge) + current_charge, max_charge) if eu_input == 0 then infotext = S("%s Idle"):format(infotext) end @@ -160,13 +223,13 @@ end -- End registration function technic.charge_tools(meta, batt_charge, charge_step) local inv = meta:get_inventory() if inv:is_empty("src") then - return batt_charge + return batt_charge, false end local srcstack = inv:get_stack("src", 1) local toolname = srcstack:get_name() if not technic.power_tools[toolname] then - return batt_charge + return batt_charge, false end -- Set meta data for the tool if it didn't do it itself src_meta = minetest.deserialize(srcstack:get_metadata()) @@ -177,8 +240,10 @@ function technic.charge_tools(meta, batt_charge, charge_step) -- Do the charging local item_max_charge = technic.power_tools[toolname] local tool_charge = src_meta.charge - if tool_charge >= item_max_charge or batt_charge <= 0 then - return batt_charge + if tool_charge >= item_max_charge then + return batt_charge, true + elseif batt_charge <= 0 then + return batt_charge, false end charge_step = math.min(charge_step, batt_charge) charge_step = math.min(charge_step, item_max_charge - tool_charge) @@ -188,19 +253,19 @@ function technic.charge_tools(meta, batt_charge, charge_step) src_meta.charge = tool_charge srcstack:set_metadata(minetest.serialize(src_meta)) inv:set_stack("src", 1, srcstack) - return batt_charge + return batt_charge, (tool_charge == item_max_charge) end function technic.discharge_tools(meta, batt_charge, charge_step, max_charge) local inv = meta:get_inventory() if inv:is_empty("dst") then - return batt_charge + return batt_charge, false end srcstack = inv:get_stack("dst", 1) local toolname = srcstack:get_name() if technic.power_tools[toolname] == nil then - return batt_charge + return batt_charge, false end -- Set meta data for the tool if it didn't do it itself :-( local src_meta = minetest.deserialize(srcstack:get_metadata()) @@ -212,8 +277,10 @@ function technic.discharge_tools(meta, batt_charge, charge_step, max_charge) -- Do the discharging local item_max_charge = technic.power_tools[toolname] local tool_charge = src_meta.charge - if tool_charge <= 0 or batt_charge >= max_charge then - return batt_charge + if tool_charge <= 0 then + return batt_charge, true + elseif batt_charge >= max_charge then + return batt_charge, false end charge_step = math.min(charge_step, max_charge - batt_charge) charge_step = math.min(charge_step, tool_charge) @@ -223,6 +290,6 @@ function technic.discharge_tools(meta, batt_charge, charge_step, max_charge) src_meta.charge = tool_charge srcstack:set_metadata(minetest.serialize(src_meta)) inv:set_stack("dst", 1, srcstack) - return batt_charge + return batt_charge, (tool_charge == 0) end diff --git a/technic/machines/register/common.lua b/technic/machines/register/common.lua index 8aab96e..3933b0c 100644 --- a/technic/machines/register/common.lua +++ b/technic/machines/register/common.lua @@ -37,12 +37,16 @@ function technic.handle_machine_upgrades(meta) end -function technic.send_items(pos, x_velocity, z_velocity) +function technic.send_items(pos, x_velocity, z_velocity, output_name) -- Send items on their way in the pipe system. + if output_name == nil then + output_name = "dst" + end + local meta = minetest.get_meta(pos) local inv = meta:get_inventory() local i = 0 - for _, stack in ipairs(inv:get_list("dst")) do + for _, stack in ipairs(inv:get_list(output_name)) do i = i + 1 if stack then local item0 = stack:to_table() @@ -53,7 +57,7 @@ function technic.send_items(pos, x_velocity, z_velocity) item1:setvelocity({x=x_velocity, y=0, z=z_velocity}) item1:setacceleration({x=0, y=0, z=0}) stack:take_item(1) - inv:set_stack("dst", i, stack) + inv:set_stack(output_name, i, stack) return end end @@ -81,7 +85,11 @@ function technic.smelt_item(meta, result, speed) end end -function technic.handle_machine_pipeworks(pos, tube_upgrade) +function technic.handle_machine_pipeworks(pos, tube_upgrade, send_function) + if send_function == nil then + send_function = technic.send_items + end + local node = minetest.get_node(pos) local meta = minetest.get_meta(pos) local inv = meta:get_inventory() @@ -105,7 +113,7 @@ function technic.handle_machine_pipeworks(pos, tube_upgrade) if tube_time >= 2 then tube_time = 0 if output_tube_connected then - technic.send_items(pos, x_velocity, z_velocity) + send_function(pos, x_velocity, z_velocity) end end meta:set_int("tube_time", tube_time) -- 2.30.2