From 6e8f243cef5ea89b7d653b99362001ccd3a46563 Mon Sep 17 00:00:00 2001 From: Leo Spratt Date: Wed, 25 Aug 2021 17:01:36 +0100 Subject: [PATCH] add orb & deconstructor --- deconstructor.lua | 116 +++++++++++++++++++++++++++++++++++ init.lua | 4 ++ mod.conf | 5 ++ orb.lua | 21 +++++++ textures/ee_exchange_orb.png | Bin 0 -> 2027 bytes 5 files changed, 146 insertions(+) create mode 100644 deconstructor.lua create mode 100644 init.lua create mode 100644 mod.conf create mode 100644 orb.lua create mode 100644 textures/ee_exchange_orb.png diff --git a/deconstructor.lua b/deconstructor.lua new file mode 100644 index 0000000..341062d --- /dev/null +++ b/deconstructor.lua @@ -0,0 +1,116 @@ +function get_element_deconstructor_formspec() + local formspec = { + "size[8,9]", + "label[2,1;Fuel]", + "list[context;fuel;2,2;1,1;]", + "label[5,1;Orb]", + "list[context;dst;5,2;1,1;]", + "list[current_player;main;0,5;8,4;]", + } + return table.concat(formspec, "") +end + +local function can_dig(pos, player) + local meta = minetest.get_meta(pos); + local inv = meta:get_inventory() + return inv:is_empty("fuel") and inv:is_empty("dst") +end + +local function on_timer(pos, elapsed) + local meta = minetest.get_meta(pos) + local inv = meta:get_inventory() + local update = true + while elapsed > 0 and update do + update = false + if not inv:is_empty("dst") and not inv:is_empty("fuel") then + -- remove one item from fuel inventory + local fuel_stack = inv:get_stack("fuel", 1) + fuel_stack:set_count(fuel_stack:get_count() - 1) + inv:set_stack("fuel", 1, fuel_stack) + + -- only get 1 orb as we can only use one + local dest_orb = inv:get_stack("dst", 1) + local stored = dest_orb:get_meta():get_int("stored_charge") or 0 + -- TODO give a specific stored charge depending on node + stored = stored + 1 + dest_orb:get_meta():set_int("stored_charge", stored) + inv:set_stack("dst", 1, dest_orb) + + update = true + end + end + minetest.get_node_timer(pos):stop() + return false +end + +local function on_construct(pos) + local meta = minetest.get_meta(pos) + local inv = meta:get_inventory() + inv:set_size("fuel", 1) + inv:set_size("dst", 1) + meta:set_string("formspec", get_element_deconstructor_formspec()) + meta:set_string("infotext", "Element Deconstructor") + on_timer(pos, 0) +end + +local function allow_metadata_inventory_put(pos, listname, index, stack, player) + if minetest.is_protected(pos, player:get_player_name()) then + return 0 + end + local meta = minetest.get_meta(pos) + local inv = meta:get_inventory() + if listname == "dst" then + if stack:get_name() == "element_exchange:exchange_orb" then + return stack:get_count() + else + return 0 + end + elseif listname == "fuel" then + return stack:get_count() + end +end + +local function allow_metadata_inventory_move(pos, from_list, from_index, to_list, to_index, count, player) + local meta = minetest.get_meta(pos) + local inv = meta:get_inventory() + local stack = inv:get_stack(from_list, from_index) + return allow_metadata_inventory_put(pos, to_list, to_index, stack, player) +end + +local function allow_metadata_inventory_take(pos, listname, index, stack, player) + if minetest.is_protected(pos, player:get_player_name()) then + return 0 + end + return stack:get_count() +end + +local function on_blast(pos) + local drops = {} + default.get_inventory_drops(pos, "fuel", drops) + default.get_inventory_drops(pos, "dst", drops) + drops[#drops+1] = "element_exchange:element_deconstructor" + minetest.remove_node(pos) + return drops +end + +minetest.register_node("element_exchange:element_deconstructor", { + description = "Element Deconstructor", + groups = {cracky = 2}, + is_ground_content = false, + can_dig = can_dig, + on_timer = on_timer, + on_construct = on_construct, + on_metadata_inventory_move = function(pos) + minetest.get_node_timer(pos):start(1.0) + end, + on_metadata_inventory_put = function(pos) + minetest.get_node_timer(pos):start(1.0) + end, + on_metadata_inventory_take = function(pos) + minetest.get_node_timer(pos):start(1.0) + end, + on_blast = on_blast, + allow_metadata_inventory_put = allow_metadata_inventory_put, + allow_metadata_inventory_move = allow_metadata_inventory_move, + allow_metadata_inventory_take = allow_metadata_inventory_take, +}) diff --git a/init.lua b/init.lua new file mode 100644 index 0000000..a167ce0 --- /dev/null +++ b/init.lua @@ -0,0 +1,4 @@ +local default_path = minetest.get_modpath("element_exchange") + +dofile(default_path.."/deconstructor.lua") +dofile(default_path.."/orb.lua") diff --git a/mod.conf b/mod.conf new file mode 100644 index 0000000..dfbf2a2 --- /dev/null +++ b/mod.conf @@ -0,0 +1,5 @@ +name = element_exchange +description = Exchange nodes into other nodes +min_minetest_version = 5.4 +min_minetest_version = 5.4 +depends=default diff --git a/orb.lua b/orb.lua new file mode 100644 index 0000000..e026c3b --- /dev/null +++ b/orb.lua @@ -0,0 +1,21 @@ +function read_orb_charge(itemstack, user, pointed_thing) + local stored = itemstack:get_meta():get_int("stored_charge") or 0 + minetest.chat_send_player(user:get_player_name(), "Current Charge: "..stored) + return itemstack +end + +minetest.register_tool("element_exchange:exchange_orb", { + description = "Exchange Orb", + inventory_image = "ee_exchange_orb.png", + on_use = read_orb_charge, +}) + +minetest.register_craft({ + type = "shaped", + output = "element_exchange:exchange_orb", + recipe = { + {"", "default:diamond",""}, + {"default:diamond", "default:gold", "default:diamond"}, + {"", "default:diamond", ""} + } +}) diff --git a/textures/ee_exchange_orb.png b/textures/ee_exchange_orb.png new file mode 100644 index 0000000000000000000000000000000000000000..4238f12ff3ae77ab998c95fb2b8d8f5af577764d GIT binary patch literal 2027 zcmah~Yfuwc6kbGB@P)5}in_BDK`_}QJPd0BB0)e2<*8s5U6M_*l4QeXApt6&RxI*x z5L5;qR9afZMrqX+bX1C+GNMzEs#QT+tOBA|kz$=%rFR2@inc#C*?YfpzI)Dh&OMtQ z78*FwW|j>EK@)?6BoW}>mVL&J0l)Kl`*8>wT}4L4Ffq~)Oh#$AxSWb7xJHc*;1DG8 zHR^C#0>QxXgp$;Xk%y!1G14&DpP~ualk3UlAwD*+h?Xm`2+6WRXRs0@ zDu&TvC~7bmxCVDFMJrLhx3@RS6QBYC2Uu|QDOv_Mark4lkqf&GP8bRo(Bw9uUBokW3(X0fk z3l>Hi33ZHw)DT)dP*;rbz1{$0-vRnlYKo2oO+^4Fvk$8r7OcUQ#41wGsD`+P_d^J+ z@=bt^Or6A*Hd;%9Tm-{hF~gV`StI~W#TML57yDLds1BGpIw>Uw!MU5ghM{18|1g?T zkZOSGBLbJg!2$kWd~Yu=j(|Im2q+o`d`U%$1ZRMifG6N{ctVcAB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