From 5cf765b2f19ef9bf443178e26787fe16233b3f4c Mon Sep 17 00:00:00 2001 From: ShadowNinja Date: Tue, 17 Dec 2013 13:56:37 -0500 Subject: [PATCH] Localize most variables --- technic/helpers.lua | 18 - technic/machines/MV/init.lua | 5 +- technic/machines/other/constructor.lua | 14 +- technic/machines/other/frames.lua | 2 +- technic/machines/other/injector.lua | 2 +- technic/machines/register/alloy_furnace.lua | 4 +- technic/machines/register/battery_box.lua | 10 +- technic/tools/chainsaw.lua | 170 ++++----- technic/tools/flashlight.lua | 4 +- technic/tools/mining_drill.lua | 367 ++++++++++---------- technic/tools/mining_lasers.lua | 4 +- technic/tools/sonic_screwdriver.lua | 4 +- 12 files changed, 296 insertions(+), 308 deletions(-) diff --git a/technic/helpers.lua b/technic/helpers.lua index 459b482..663a66a 100644 --- a/technic/helpers.lua +++ b/technic/helpers.lua @@ -1,21 +1,3 @@ -function get_item_meta (string) - if string.find(string, "return {") then - return minetest.deserialize(string) - else return nil - end -end - -function set_item_meta (table) - return minetest.serialize(table) -end - -function has_locked_chest_privilege(meta, player) - if player:get_player_name() ~= meta:get_string("owner") then - return false - end - return true -end - minetest.swap_node = minetest.swap_node or function(pos, node) local oldmeta = minetest.get_meta(pos):to_table() minetest.set_node(pos, node) diff --git a/technic/machines/MV/init.lua b/technic/machines/MV/init.lua index 92d1da9..d51bdf5 100644 --- a/technic/machines/MV/init.lua +++ b/technic/machines/MV/init.lua @@ -17,6 +17,7 @@ dofile(path.."/generator.lua") -- The power radiator supplies appliances with inductive coupled power: -- Lighting and associated textures is taken directly from VanessaE's homedecor and made electric. -dofile(path.."/power_radiator.lua") -dofile(path.."/lighting.lua") +-- This is currently useless, slow, and mostly copied +--dofile(path.."/power_radiator.lua") +--dofile(path.."/lighting.lua") diff --git a/technic/machines/other/constructor.lua b/technic/machines/other/constructor.lua index f4f541f..1210c5b 100644 --- a/technic/machines/other/constructor.lua +++ b/technic/machines/other/constructor.lua @@ -21,7 +21,7 @@ minetest.register_craft({ }) -mk1_on = function(pos, node) +local function mk1_on(pos, node) local meta = minetest.get_meta(pos) local inv = meta:get_inventory() local pos1={} @@ -41,7 +41,7 @@ mk1_on = function(pos, node) end end -mk1_off = function(pos, node) +local function mk1_off(pos, node) if node.name == "technic:constructor_mk1_on" then technic.swap_node(pos,"technic:constructor_mk1_off") nodeupdate(pos) @@ -97,7 +97,7 @@ minetest.register_node("technic:constructor_mk1_on", { --Constructor Mk2 -mk2_on = function(pos, node) +local function mk2_on(pos, node) local meta = minetest.get_meta(pos) local inv = meta:get_inventory() local pos1={} @@ -123,7 +123,7 @@ mk2_on = function(pos, node) end end -mk2_off = function(pos, node) +local function mk2_off(pos, node) if node.name == "technic:constructor_mk2_on" then technic.swap_node(pos,"technic:constructor_mk2_off") nodeupdate(pos) @@ -181,7 +181,7 @@ minetest.register_node("technic:constructor_mk2_on", { -- Constructor Mk3 -mk3_on = function(pos, node) +local function mk3_on(pos, node) local meta = minetest.get_meta(pos) local inv = meta:get_inventory() @@ -225,7 +225,7 @@ mk3_on = function(pos, node) end end -mk3_off = function(pos, node) +local function mk3_off(pos, node) if node.name == "technic:constructor_mk3_on" then technic.swap_node(pos,"technic:constructor_mk3_off") nodeupdate(pos) @@ -289,7 +289,7 @@ minetest.register_node("technic:constructor_mk3_on", { }) -deploy_node =function (inv, slot_name, pos1, node1, node) +local function deploy_node(inv, slot_name, pos1, node1, node) if node1.name == "air" then if not inv:is_empty(slot_name) then stack1=inv:get_list(slot_name) diff --git a/technic/machines/other/frames.lua b/technic/machines/other/frames.lua index 9eaf63e..9ee25da 100644 --- a/technic/machines/other/frames.lua +++ b/technic/machines/other/frames.lua @@ -43,7 +43,7 @@ local function get_face(pos,ppos,pvect) end end -function lines(str) +local function lines(str) local t = {} local function helper(line) table.insert(t, line) return "" end helper((str:gsub("(.-)\r?\n", helper))) diff --git a/technic/machines/other/injector.lua b/technic/machines/other/injector.lua index f59867c..205b8b4 100644 --- a/technic/machines/other/injector.lua +++ b/technic/machines/other/injector.lua @@ -74,7 +74,7 @@ minetest.register_abm({ end, }) -function inject_items (pos) +local function inject_items (pos) local meta=minetest.env:get_meta(pos) local inv = meta:get_inventory() local mode=meta:get_string("mode") diff --git a/technic/machines/register/alloy_furnace.lua b/technic/machines/register/alloy_furnace.lua index 64080c1..1195b50 100644 --- a/technic/machines/register/alloy_furnace.lua +++ b/technic/machines/register/alloy_furnace.lua @@ -6,11 +6,11 @@ technic.alloy_recipes = {} -- Register recipe in a table technic.register_alloy_recipe = function(metal1, count1, metal2, count2, result, count3) - in1 = { + local in1 = { name = metal1, count = count1, } - in2 = { + local in2 = { name = metal2, count = count2, } diff --git a/technic/machines/register/battery_box.lua b/technic/machines/register/battery_box.lua index f9bc893..55431e7 100644 --- a/technic/machines/register/battery_box.lua +++ b/technic/machines/register/battery_box.lua @@ -161,14 +161,14 @@ function technic.charge_tools(meta, batt_charge, charge_step) return batt_charge end local srcstack = inv:get_stack("src", 1) - local src_meta = get_item_meta(srcstack:get_metadata()) + local src_meta = minetest.deserialize(srcstack:get_metadata()) local toolname = srcstack:get_name() if not technic.power_tools[toolname] then return batt_charge end -- Set meta data for the tool if it didn't do it itself - src_meta = get_item_meta(srcstack:get_metadata()) + src_meta = minetest.deserialize(srcstack:get_metadata()) src_meta = src_meta or {} if not src_meta.charge then src_meta.charge = 0 @@ -185,7 +185,7 @@ function technic.charge_tools(meta, batt_charge, charge_step) batt_charge = batt_charge - charge_step technic.set_RE_wear(srcstack, tool_charge, item_max_charge) src_meta.charge = tool_charge - srcstack:set_metadata(set_item_meta(src_meta)) + srcstack:set_metadata(minetest.serialize(src_meta)) inv:set_stack("src", 1, srcstack) return batt_charge end @@ -202,7 +202,7 @@ function technic.discharge_tools(meta, batt_charge, charge_step, max_charge) return batt_charge end -- Set meta data for the tool if it didn't do it itself :-( - local src_meta = get_item_meta(srcstack:get_metadata()) + local src_meta = minetest.deserialize(srcstack:get_metadata()) src_meta = src_meta or {} if not src_meta.charge then src_meta.charge = 0 @@ -220,7 +220,7 @@ function technic.discharge_tools(meta, batt_charge, charge_step, max_charge) batt_charge = batt_charge + charge_step technic.set_RE_wear(srcstack, tool_charge, item_max_charge) src_meta.charge = tool_charge - srcstack:set_metadata(set_item_meta(src_meta)) + srcstack:set_metadata(minetest.serialize(src_meta)) inv:set_stack("dst", 1, srcstack) return batt_charge end diff --git a/technic/tools/chainsaw.lua b/technic/tools/chainsaw.lua index 67ec43d..fd8ce2c 100644 --- a/technic/tools/chainsaw.lua +++ b/technic/tools/chainsaw.lua @@ -3,44 +3,6 @@ local chainsaw_max_charge = 30000 -- 30000 - Maximum charge of the saw local chainsaw_charge_per_node = 12 -- 12 - Gives 2500 nodes on a single charge (about 50 complete normal trees) local chainsaw_leaves = true -- true - Cut down entire trees, leaves and all -local S = technic.getter - -technic.register_power_tool("technic:chainsaw", chainsaw_max_charge) - -minetest.register_tool("technic:chainsaw", { - description = S("Chainsaw"), - inventory_image = "technic_chainsaw.png", - stack_max = 1, - on_use = function(itemstack, user, pointed_thing) - if pointed_thing.type ~= "node" then - return itemstack - end - local meta = get_item_meta(itemstack:get_metadata()) - if not meta or not meta.charge then - return - end - -- Send current charge to digging function so that the chainsaw will stop after digging a number of nodes. - if meta.charge < chainsaw_charge_per_node then - return - end - - local pos = minetest.get_pointed_thing_position(pointed_thing, above) - meta.charge = chainsaw_dig_it(pos, user, meta.charge) - technic.set_RE_wear(itemstack, meta.charge, chainsaw_max_charge) - itemstack:set_metadata(set_item_meta(meta)) - return itemstack - end, -}) - -minetest.register_craft({ - output = 'technic:chainsaw', - recipe = { - {'technic:stainless_steel_ingot', 'technic:stainless_steel_ingot', 'technic:battery'}, - {'technic:stainless_steel_ingot', 'technic:motor', 'technic:battery'}, - {'', '', 'default:copper_ingot'}, - } -}) - -- The default stuff local timber_nodenames={["default:jungletree"] = true, ["default:papyrus"] = true, @@ -155,55 +117,17 @@ if( minetest.get_modpath("farming_plus") ~= nil ) then end end + +local S = technic.getter + +technic.register_power_tool("technic:chainsaw", chainsaw_max_charge) + -- Table for saving what was sawed down -local produced - --- Saw down trees entry point -chainsaw_dig_it = function(pos, player,current_charge) - local remaining_charge=current_charge - - -- Save the currently installed dropping mechanism so we can restore it. - local original_handle_node_drops = minetest.handle_node_drops - - -- A bit of trickery here: use a different node drop callback - -- and restore the original afterwards. - minetest.handle_node_drops = chainsaw_handle_node_drops - - -- clear result and start sawing things down - produced = {} - remaining_charge = recursive_dig(pos, remaining_charge, player) - minetest.sound_play("chainsaw", {pos = pos, gain = 1.0, max_hear_distance = 10,}) - - -- Restore the original noder drop handler - minetest.handle_node_drops = original_handle_node_drops - - -- Now drop items for the player - local number, produced_item, p - for produced_item,number in pairs(produced) do - --print("ADDING ITEM: " .. produced_item .. " " .. number) - -- Drop stacks of 99 or less - p = { - x = pos.x + math.random()*4, - y = pos.y, - z = pos.z + math.random()*4 - } - while number > 99 do - minetest.env:add_item(p, produced_item .. " 99") - p = { - x = pos.x + math.random()*4, - y = pos.y, - z = pos.z + math.random()*4 - } - number = number - 99 - end - minetest.env:add_item(p, produced_item .. " " .. number) - end - return remaining_charge -end +local produced = nil -- Override the default handling routine to be able to count up the -- items sawed down so that we can drop them i an nice single stack -chainsaw_handle_node_drops = function(pos, drops, digger) +local function chainsaw_handle_node_drops(pos, drops, digger) -- Add dropped items to list of collected nodes local _, dropped_item for _, dropped_item in ipairs(drops) do @@ -217,7 +141,7 @@ end -- This function does all the hard work. Recursively we dig the node at hand -- if it is in the table and then search the surroundings for more stuff to dig. -recursive_dig = function(pos, remaining_charge, player) +local function recursive_dig(pos, remaining_charge, player) local node=minetest.env:get_node(pos) local i=1 -- Lookup node name in timber table: @@ -277,3 +201,81 @@ recursive_dig = function(pos, remaining_charge, player) return remaining_charge end +-- Saw down trees entry point +local function chainsaw_dig_it(pos, player,current_charge) + local remaining_charge=current_charge + + -- Save the currently installed dropping mechanism so we can restore it. + local original_handle_node_drops = minetest.handle_node_drops + + -- A bit of trickery here: use a different node drop callback + -- and restore the original afterwards. + minetest.handle_node_drops = chainsaw_handle_node_drops + + -- clear result and start sawing things down + produced = {} + remaining_charge = recursive_dig(pos, remaining_charge, player) + minetest.sound_play("chainsaw", {pos = pos, gain = 1.0, max_hear_distance = 10,}) + + -- Restore the original noder drop handler + minetest.handle_node_drops = original_handle_node_drops + + -- Now drop items for the player + local number, produced_item, p + for produced_item,number in pairs(produced) do + --print("ADDING ITEM: " .. produced_item .. " " .. number) + -- Drop stacks of 99 or less + p = { + x = pos.x + math.random()*4, + y = pos.y, + z = pos.z + math.random()*4 + } + while number > 99 do + minetest.env:add_item(p, produced_item .. " 99") + p = { + x = pos.x + math.random()*4, + y = pos.y, + z = pos.z + math.random()*4 + } + number = number - 99 + end + minetest.env:add_item(p, produced_item .. " " .. number) + end + return remaining_charge +end + + +minetest.register_tool("technic:chainsaw", { + description = S("Chainsaw"), + inventory_image = "technic_chainsaw.png", + stack_max = 1, + on_use = function(itemstack, user, pointed_thing) + if pointed_thing.type ~= "node" then + return itemstack + end + local meta = minetest.deserialize(itemstack:get_metadata()) + if not meta or not meta.charge then + return + end + -- Send current charge to digging function so that the chainsaw will stop after digging a number of nodes. + if meta.charge < chainsaw_charge_per_node then + return + end + + local pos = minetest.get_pointed_thing_position(pointed_thing, above) + meta.charge = chainsaw_dig_it(pos, user, meta.charge) + technic.set_RE_wear(itemstack, meta.charge, chainsaw_max_charge) + itemstack:set_metadata(minetest.serialize(meta)) + return itemstack + end, +}) + +minetest.register_craft({ + output = 'technic:chainsaw', + recipe = { + {'technic:stainless_steel_ingot', 'technic:stainless_steel_ingot', 'technic:battery'}, + {'technic:stainless_steel_ingot', 'technic:motor', 'technic:battery'}, + {'', '', 'default:copper_ingot'}, + } +}) + diff --git a/technic/tools/flashlight.lua b/technic/tools/flashlight.lua index 56d3137..61eceae 100644 --- a/technic/tools/flashlight.lua +++ b/technic/tools/flashlight.lua @@ -151,14 +151,14 @@ function check_for_flashlight(player) local hotbar = inv:get_list("main") for i = 1, 8 do if hotbar[i]:get_name() == "technic:flashlight" then - local meta = get_item_meta(hotbar[i]:get_metadata()) + local meta = minetest.deserialize(hotbar[i]:get_metadata()) if not meta or not meta.charge then return false end if meta.charge - 2 > 0 then meta.charge = meta.charge - 2; technic.set_RE_wear(hotbar[i], meta.charge, flashlight_max_charge) - hotbar[i]:set_metadata(set_item_meta(meta)) + hotbar[i]:set_metadata(minetest.serialize(meta)) inv:set_stack("main", i, hotbar[i]) return true end diff --git a/technic/tools/mining_drill.lua b/technic/tools/mining_drill.lua index d5548fb..79eef53 100644 --- a/technic/tools/mining_drill.lua +++ b/technic/tools/mining_drill.lua @@ -42,7 +42,102 @@ for i = 1, 4 do }) end -function drill_dig_it(pos, player, drill_type, mode) +local mining_drill_mode_text = { + {S("Single node.")}, + {S("3 nodes deep.")}, + {S("3 nodes wide.")}, + {S("3 nodes tall.")}, + {S("3x3 nodes.")}, +} + +local function drill_dig_it0 (pos,player) + local node=minetest.env:get_node(pos) + if node.name == "air" or node.name == "ignore" then return end + if node.name == "default:lava_source" then return end + if node.name == "default:lava_flowing" then return end + if node.name == "default:water_source" then minetest.env:remove_node(pos) return end + if node.name == "default:water_flowing" then minetest.env:remove_node(pos) return end + minetest.node_dig(pos,node,player) +end + +local function drill_dig_it1 (player) + local dir=player:get_look_dir() + if math.abs(dir.x)>math.abs(dir.z) then + if dir.x>0 then return 0 end + return 1 + end + if dir.z>0 then return 2 end + return 3 +end + +local function drill_dig_it2 (pos,player) + drill_dig_it0 (pos,player) + pos.z=pos.z+1 + drill_dig_it0 (pos,player) + pos.z=pos.z-2 + drill_dig_it0 (pos,player) + pos.z=pos.z+1 + pos.y=pos.y+1 + drill_dig_it0 (pos,player) + pos.z=pos.z+1 + drill_dig_it0 (pos,player) + pos.z=pos.z-2 + drill_dig_it0 (pos,player) + pos.z=pos.z+1 + pos.y=pos.y-2 + drill_dig_it0 (pos,player) + pos.z=pos.z+1 + drill_dig_it0 (pos,player) + pos.z=pos.z-2 + drill_dig_it0 (pos,player) +end + +local function drill_dig_it3 (pos,player) + drill_dig_it0 (pos,player) + pos.x=pos.x+1 + drill_dig_it0 (pos,player) + pos.x=pos.x-2 + drill_dig_it0 (pos,player) + pos.x=pos.x+1 + pos.y=pos.y+1 + drill_dig_it0 (pos,player) + pos.x=pos.x+1 + drill_dig_it0 (pos,player) + pos.x=pos.x-2 + drill_dig_it0 (pos,player) + pos.x=pos.x+1 + pos.y=pos.y-2 + drill_dig_it0 (pos,player) + pos.x=pos.x+1 + drill_dig_it0 (pos,player) + pos.x=pos.x-2 + drill_dig_it0 (pos,player) +end + +local function drill_dig_it4 (pos,player) + drill_dig_it0 (pos,player) + pos.x=pos.x+1 + drill_dig_it0 (pos,player) + pos.x=pos.x-2 + drill_dig_it0 (pos,player) + pos.x=pos.x+1 + pos.z=pos.z+1 + drill_dig_it0 (pos,player) + pos.x=pos.x+1 + drill_dig_it0 (pos,player) + pos.x=pos.x-2 + drill_dig_it0 (pos,player) + pos.x=pos.x+1 + pos.z=pos.z-2 + drill_dig_it0 (pos,player) + pos.x=pos.x+1 + drill_dig_it0 (pos,player) + pos.x=pos.x-2 + drill_dig_it0 (pos,player) +end + + +local function drill_dig_it(pos, player, drill_type, mode) local charge if mode == 1 then drill_dig_it0(pos, player) @@ -100,9 +195,9 @@ function drill_dig_it(pos, player, drill_type, mode) if mode==4 then -- 3 tall, selected in the middle drill_dig_it0 (pos,player) - pos.y=pos.y+1 + pos.y=pos.y-1 drill_dig_it0 (pos,player) - pos.y=pos.y-2 + pos.y=pos.y-1 drill_dig_it0 (pos,player) end @@ -135,92 +230,99 @@ function drill_dig_it(pos, player, drill_type, mode) return charge end -function drill_dig_it0 (pos,player) - local node=minetest.env:get_node(pos) - if node.name == "air" or node.name == "ignore" then return end - if node.name == "default:lava_source" then return end - if node.name == "default:lava_flowing" then return end - if node.name == "default:water_source" then minetest.env:remove_node(pos) return end - if node.name == "default:water_flowing" then minetest.env:remove_node(pos) return end - minetest.node_dig(pos,node,player) -end - -function drill_dig_it1 (player) - local dir=player:get_look_dir() - if math.abs(dir.x)>math.abs(dir.z) then - if dir.x>0 then return 0 end - return 1 +local function mining_drill_mk2_setmode(user,itemstack) + local player_name=user:get_player_name() + local item=itemstack:to_table() + local meta=minetest.deserialize(item["metadata"]) + if meta==nil then + meta={} + mode=0 end - if dir.z>0 then return 2 end - return 3 + if meta["mode"]==nil then + minetest.chat_send_player(player_name, S("Hold shift and use to change Mining Drill Mk%d modes."):format(2)) + meta["mode"]=0 + mode=0 + end + mode=(meta["mode"]) + mode=mode+1 + if mode>=5 then mode=1 end + minetest.chat_send_player(player_name, S("Mining Drill Mk%d Mode %d"):format(2, mode)..": "..mining_drill_mode_text[mode][1]) + item["name"]="technic:mining_drill_mk2_"..mode + meta["mode"]=mode + item["metadata"]=minetest.serialize(meta) + itemstack:replace(item) + return itemstack end -function drill_dig_it2 (pos,player) - drill_dig_it0 (pos,player) - pos.z=pos.z+1 - drill_dig_it0 (pos,player) - pos.z=pos.z-2 - drill_dig_it0 (pos,player) - pos.z=pos.z+1 - pos.y=pos.y+1 - drill_dig_it0 (pos,player) - pos.z=pos.z+1 - drill_dig_it0 (pos,player) - pos.z=pos.z-2 - drill_dig_it0 (pos,player) - pos.z=pos.z+1 - pos.y=pos.y-2 - drill_dig_it0 (pos,player) - pos.z=pos.z+1 - drill_dig_it0 (pos,player) - pos.z=pos.z-2 - drill_dig_it0 (pos,player) -end -function drill_dig_it3 (pos,player) - drill_dig_it0 (pos,player) - pos.x=pos.x+1 - drill_dig_it0 (pos,player) - pos.x=pos.x-2 - drill_dig_it0 (pos,player) - pos.x=pos.x+1 - pos.y=pos.y+1 - drill_dig_it0 (pos,player) - pos.x=pos.x+1 - drill_dig_it0 (pos,player) - pos.x=pos.x-2 - drill_dig_it0 (pos,player) - pos.x=pos.x+1 - pos.y=pos.y-2 - drill_dig_it0 (pos,player) - pos.x=pos.x+1 - drill_dig_it0 (pos,player) - pos.x=pos.x-2 - drill_dig_it0 (pos,player) +local function mining_drill_mk3_setmode(user,itemstack) + local player_name=user:get_player_name() + local item=itemstack:to_table() + local meta=minetest.deserialize(item["metadata"]) + if meta==nil then + meta={} + mode=0 + end + if meta["mode"]==nil then + minetest.chat_send_player(player_name, S("Hold shift and use to change Mining Drill Mk%d modes."):format(3)) + meta["mode"]=0 + mode=0 + end + mode=(meta["mode"]) + mode=mode+1 + if mode>=6 then mode=1 end + minetest.chat_send_player(player_name, S("Mining Drill Mk%d Mode %d"):format(3, mode)..": "..mining_drill_mode_text[mode][1]) + item["name"]="technic:mining_drill_mk3_"..mode + meta["mode"]=mode + item["metadata"]=minetest.serialize(meta) + itemstack:replace(item) + return itemstack end -function drill_dig_it4 (pos,player) - drill_dig_it0 (pos,player) - pos.x=pos.x+1 - drill_dig_it0 (pos,player) - pos.x=pos.x-2 - drill_dig_it0 (pos,player) - pos.x=pos.x+1 - pos.z=pos.z+1 - drill_dig_it0 (pos,player) - pos.x=pos.x+1 - drill_dig_it0 (pos,player) - pos.x=pos.x-2 - drill_dig_it0 (pos,player) - pos.x=pos.x+1 - pos.z=pos.z-2 - drill_dig_it0 (pos,player) - pos.x=pos.x+1 - drill_dig_it0 (pos,player) - pos.x=pos.x-2 - drill_dig_it0 (pos,player) + +local function mining_drill_mk2_handler(itemstack, user, pointed_thing) + local keys = user:get_player_control() + local player_name = user:get_player_name() + local meta = minetest.deserialize(itemstack:get_metadata()) + if not meta or not meta.mode or keys.sneak then + return mining_drill_mk2_setmode(user, itemstack) + end + if pointed_thing.type ~= "node" or not meta.charge then + return + end + if meta.charge - mining_drill_power_usage > 0 then + local pos = minetest.get_pointed_thing_position(pointed_thing, above) + local charge_to_take = drill_dig_it(pos, user, 2, meta.mode) + meta.charge = meta.charge - charge_to_take + meta.charge = math.max(meta.charge, 0) + itemstack:set_metadata(minetest.serialize(meta)) + technic.set_RE_wear(itemstack, meta.charge, mining_drill_mk2_max_charge) + end + return itemstack +end + +local function mining_drill_mk3_handler(itemstack, user, pointed_thing) + local keys = user:get_player_control() + local player_name = user:get_player_name() + local meta = minetest.deserialize(itemstack:get_metadata()) + if not meta or not meta.mode or keys.sneak then + return mining_drill_mk3_setmode(user, itemstack) + end + if pointed_thing.type ~= "node" or not meta.charge then + return + end + if meta.charge - mining_drill_power_usage > 0 then + local pos = minetest.get_pointed_thing_position(pointed_thing, above) + local charge_to_take = drill_dig_it(pos, user, 3, meta.mode) + meta.charge = meta.charge - charge_to_take + meta.charge = math.max(meta.charge, 0) + itemstack:set_metadata(minetest.serialize(meta)) + technic.set_RE_wear(itemstack, meta.charge, mining_drill_mk3_max_charge) + end + return itemstack end technic.register_power_tool("technic:mining_drill", mining_drill_max_charge) + minetest.register_tool("technic:mining_drill", { description = S("Mining Drill Mk%d"):format(1), inventory_image = "technic_mining_drill.png", @@ -229,7 +331,7 @@ minetest.register_tool("technic:mining_drill", { if pointed_thing.type ~= "node" then return itemstack end - local meta = get_item_meta(itemstack:get_metadata()) + local meta = minetest.deserialize(itemstack:get_metadata()) if not meta or not meta.charge then return end @@ -237,7 +339,7 @@ minetest.register_tool("technic:mining_drill", { local pos = minetest.get_pointed_thing_position(pointed_thing, above) charge_to_take = drill_dig_it(pos, user, 1, 1) meta.charge = meta.charge - mining_drill_power_usage - itemstack:set_metadata(set_item_meta(meta)) + itemstack:set_metadata(minetest.serialize(meta)) technic.set_RE_wear(itemstack, meta.charge, mining_drill_max_charge) end return itemstack @@ -293,102 +395,3 @@ for i=1,5,1 do end, }) end - -function mining_drill_mk2_handler(itemstack, user, pointed_thing) - local keys = user:get_player_control() - local player_name = user:get_player_name() - local meta = get_item_meta(itemstack:get_metadata()) - if not meta or not meta.mode or keys.sneak then - return mining_drill_mk2_setmode(user, itemstack) - end - if pointed_thing.type ~= "node" or not meta.charge then - return - end - if meta.charge - mining_drill_power_usage > 0 then - local pos = minetest.get_pointed_thing_position(pointed_thing, above) - local charge_to_take = drill_dig_it(pos, user, 2, meta.mode) - meta.charge = meta.charge - charge_to_take - meta.charge = math.max(meta.charge, 0) - itemstack:set_metadata(set_item_meta(meta)) - technic.set_RE_wear(itemstack, meta.charge, mining_drill_mk2_max_charge) - end - return itemstack -end - -function mining_drill_mk3_handler(itemstack, user, pointed_thing) - local keys = user:get_player_control() - local player_name = user:get_player_name() - local meta = get_item_meta(itemstack:get_metadata()) - if not meta or not meta.mode or keys.sneak then - return mining_drill_mk3_setmode(user, itemstack) - end - if pointed_thing.type ~= "node" or not meta.charge then - return - end - if meta.charge - mining_drill_power_usage > 0 then - local pos = minetest.get_pointed_thing_position(pointed_thing, above) - local charge_to_take = drill_dig_it(pos, user, 3, meta.mode) - meta.charge = meta.charge - charge_to_take - meta.charge = math.max(meta.charge, 0) - itemstack:set_metadata(set_item_meta(meta)) - technic.set_RE_wear(itemstack, meta.charge, mining_drill_mk3_max_charge) - end - return itemstack -end - -mining_drill_mode_text = { - {S("Single node.")}, - {S("3 nodes deep.")}, - {S("3 nodes wide.")}, - {S("3 nodes tall.")}, - {S("3x3 nodes.")}, -} - -function mining_drill_mk2_setmode(user,itemstack) - local player_name=user:get_player_name() - local item=itemstack:to_table() - local meta=get_item_meta(item["metadata"]) - if meta==nil then - meta={} - mode=0 - end - if meta["mode"]==nil then - minetest.chat_send_player(player_name, S("Hold shift and use to change Mining Drill Mk%d modes."):format(2)) - meta["mode"]=0 - mode=0 - end - mode=(meta["mode"]) - mode=mode+1 - if mode>=5 then mode=1 end - minetest.chat_send_player(player_name, S("Mining Drill Mk%d Mode %d"):format(2, mode)..": "..mining_drill_mode_text[mode][1]) - item["name"]="technic:mining_drill_mk2_"..mode - meta["mode"]=mode - item["metadata"]=set_item_meta(meta) - itemstack:replace(item) - return itemstack -end - -function mining_drill_mk3_setmode(user,itemstack) - local player_name=user:get_player_name() - local item=itemstack:to_table() - local meta=get_item_meta(item["metadata"]) - if meta==nil then - meta={} - mode=0 - end - if meta["mode"]==nil then - minetest.chat_send_player(player_name, S("Hold shift and use to change Mining Drill Mk%d modes."):format(3)) - meta["mode"]=0 - mode=0 - end - mode=(meta["mode"]) - mode=mode+1 - if mode>=6 then mode=1 end - minetest.chat_send_player(player_name, S("Mining Drill Mk%d Mode %d"):format(3, mode)..": "..mining_drill_mode_text[mode][1]) - item["name"]="technic:mining_drill_mk3_"..mode - meta["mode"]=mode - item["metadata"]=set_item_meta(meta) - itemstack:replace(item) - return itemstack -end - diff --git a/technic/tools/mining_lasers.lua b/technic/tools/mining_lasers.lua index caa416f..64628eb 100644 --- a/technic/tools/mining_lasers.lua +++ b/technic/tools/mining_lasers.lua @@ -150,7 +150,7 @@ for _, m in pairs(mining_lasers_list) do inventory_image = "technic_mining_laser_mk"..m[1]..".png", stack_max = 1, on_use = function(itemstack, user) - local meta = get_item_meta(itemstack:get_metadata()) + local meta = minetest.deserialize(itemstack:get_metadata()) if not meta or not meta.charge then return end @@ -158,7 +158,7 @@ for _, m in pairs(mining_lasers_list) do laser_shoot(user, m[2], "technic_laser_beam_mk"..m[1]..".png", "technic_laser_mk"..m[1]) meta.charge = meta.charge - 400 technic.set_RE_wear(itemstack, meta.charge, m[3]) - itemstack:set_metadata(set_item_meta(meta)) + itemstack:set_metadata(minetest.serialize(meta)) end return itemstack end, diff --git a/technic/tools/sonic_screwdriver.lua b/technic/tools/sonic_screwdriver.lua index 1639834..cd2ce51 100644 --- a/technic/tools/sonic_screwdriver.lua +++ b/technic/tools/sonic_screwdriver.lua @@ -22,7 +22,7 @@ minetest.register_tool("technic:sonic_screwdriver", { if node.param2 == nil then return end - local meta1 = get_item_meta(itemstack:get_metadata()) + local meta1 = minetest.deserialize(itemstack:get_metadata()) if not meta1 or not meta1.charge then return end @@ -48,7 +48,7 @@ minetest.register_tool("technic:sonic_screwdriver", { minetest.swap_node(pos, node) meta1.charge = meta1.charge - 100 - itemstack:set_metadata(set_item_meta(meta1)) + itemstack:set_metadata(minetest.serialize(meta1)) technic.set_RE_wear(itemstack, meta1.charge, sonic_screwdriver_max_charge) end end