From c797a15e8ba5f84b7b34e828a84aebad5c1aabd0 Mon Sep 17 00:00:00 2001 From: KaadmY Date: Sat, 13 May 2017 10:27:40 -0700 Subject: [PATCH 1/5] Add new crafting mod for a new crafting method --- mods/crafting/README.txt | 8 + mods/crafting/init.lua | 203 ++++++++++++++++++ mods/craftingguide/README.txt | 9 - mods/craftingguide/depends.txt | 1 - mods/craftingguide/init.lua | 183 ---------------- .../textures/craftingguide_method_cooking.png | Bin 268 -> 0 bytes .../textures/craftingguide_method_cooking.xcf | Bin 1018 -> 0 bytes .../craftingguide_method_crafting.png | Bin 200 -> 0 bytes .../craftingguide_method_crafting.xcf | Bin 1333 -> 0 bytes .../textures/craftingguide_method_fuel.png | Bin 300 -> 0 bytes .../textures/craftingguide_method_fuel.xcf | Bin 1087 -> 0 bytes .../textures/craftingguide_method_unknown.png | Bin 286 -> 0 bytes .../textures/craftingguide_method_unknown.xcf | Bin 1028 -> 0 bytes mods/default/formspec.lua | 11 +- 14 files changed, 214 insertions(+), 201 deletions(-) create mode 100644 mods/crafting/README.txt create mode 100644 mods/crafting/init.lua delete mode 100644 mods/craftingguide/README.txt delete mode 100644 mods/craftingguide/depends.txt delete mode 100644 mods/craftingguide/init.lua delete mode 100644 mods/craftingguide/textures/craftingguide_method_cooking.png delete mode 100644 mods/craftingguide/textures/craftingguide_method_cooking.xcf delete mode 100644 mods/craftingguide/textures/craftingguide_method_crafting.png delete mode 100644 mods/craftingguide/textures/craftingguide_method_crafting.xcf delete mode 100644 mods/craftingguide/textures/craftingguide_method_fuel.png delete mode 100644 mods/craftingguide/textures/craftingguide_method_fuel.xcf delete mode 100644 mods/craftingguide/textures/craftingguide_method_unknown.png delete mode 100644 mods/craftingguide/textures/craftingguide_method_unknown.xcf diff --git a/mods/crafting/README.txt b/mods/crafting/README.txt new file mode 100644 index 0000000..bb22e86 --- /dev/null +++ b/mods/crafting/README.txt @@ -0,0 +1,8 @@ +Cratfing mod +============ +By Kaadmy, for Pixture + +Custom crafting method, uses a list of possible items to craft instead of a grid +recipe. + +Source license: LGPLv2.1 diff --git a/mods/crafting/init.lua b/mods/crafting/init.lua new file mode 100644 index 0000000..5645c46 --- /dev/null +++ b/mods/crafting/init.lua @@ -0,0 +1,203 @@ +-- +-- Crafting mod +-- By Kaadmy, for Pixture +-- + +crafting = {} + +crafting.registered_crafts = {} + +-- Crafting can only take 4 itemstacks as input for sanity/interface reasons +crafting.max_inputs = 4 + +-- Default crafting definition values +crafting.default_craftdef = { + output = nil, + description = "", + items = {}, +} + +function crafting.register_craft(output, def) + -- `output` can be any ItemStack value, but count is NOT used + + local itemstack = ItemStack(output) + local itemn = itemstack:get_name() + + if crafting.registered_crafts[itemn] ~= nil then + minetest.log("warning", + "Tried to register an existing craft " .. itemn) + + return + end + + local craftdef = { + output = itemstack, + description = def.description or minetest.registered_items[itemn].description, + items = def.items or default.default_craftdef.items, + } + + if #craftdef.items > 4 then + minetest.log("warning", + "Attempting to register craft " .. itemn .." with more than " + .. crafting.max_inputs .. " inputs, keeping") + end + + for i = 1, crafting.max_inputs do + craftdef.items[i] = ItemStack(craftdef.items[i]) + end + + crafting.registered_crafts[itemn] = craftdef + + minetest.log("info", "Registered recipe for " .. itemn .. ": " .. dump(crafting.registered_crafts[itemn])) +end + +function crafting.craft(output, items) + -- `output` can be any ItemStack value + -- Duplicate items in `items` should work correctly + + print(dump(output)) + + local wanted_itemstack = ItemStack(output) + + print(dump(wanted_itemstack:to_string())) + + local craftdef = crafting.registered_crafts[wanted_itemstack:get_name()] + + if craftdef == nil then + minetest.log("warning", + "Tried to craft an unregistered item " .. wanted_itemstack:get_name()) + + return nil + end + + --print("Craftdef items: " .. dump(craftdef.items)) + print("Input before: " .. dump(items)) + + -- Check for validity + + local craft_count = wanted_itemstack:get_count() + + for i = 1, crafting.max_inputs do + local required_itemstack = ItemStack(craftdef.items[i]) + local itemc = 0 + + if required_itemstack ~= nil and required_itemstack:get_count() ~= 0 then + for j = 1, crafting.max_inputs do + local input_itemstack = ItemStack(items[j]) + + if input_itemstack:get_name() == required_itemstack:get_name() then + itemc = itemc + input_itemstack:get_count() + end + end + + craft_count = math.min(craft_count, math.floor(itemc / required_itemstack:get_count())) + + if craft_count < 1 then + minetest.log("warning", + "Not enough items to craft " .. wanted_itemstack:get_name()) + + return nil -- Not enough items + end + end + end + + --print("Craft count: " .. craft_count .. "/" .. output.count) + + -- Iterate through second time to take items used for crafting + + local function remove_used_item(itemn, count) + local items_required = count + + for i = 1, crafting.max_inputs do + local input_itemstack = ItemStack(items[i]) + + if items[i] ~= nil and input_itemstack:get_name() == itemn then + local items_left = items_required - input_itemstack:get_count() + + print("Taking " .. items_required .. " items from " .. itemn) + + input_itemstack:take_item(items_required) + + if items_left > 0 then + items_required = items_required - (items_required - items_left) + else + items[i] = input_itemstack:to_table() + break + end + + items[i] = input_itemstack:to_table() + end + end + end + + for i = 1, crafting.max_inputs do + local required_itemstack = ItemStack(craftdef.items[i]) + + if craftdef.items[i] ~= nil then + remove_used_item(required_itemstack:get_name(), required_itemstack:get_count() * craft_count) + end + end + + print("Input after: " .. dump(items)) + + return items +end + +crafting.register_craft( + "default:stone 4", + { + items = { + {name = "default:stick", count = 3}, + {name = "default:fiber", count = 2}, + {name = "group:stone", count = 2}, + }, +}) + +crafting.craft( + "default:stone 2", + { + {name = "default:stick", count = 4}, -- 0 leftover + {name = "default:stick", count = 5}, -- 3 leftover + {name = "default:fiber", count = 9}, -- 5 leftover + {name = "group:stone", count = 4}, -- 0 leftover +}) + +local function on_player_recieve_fields(player, form_name, fields) + if form_name ~= "crafting:crafting" or fields.cancel then return end + + local inv = player:get_inventory() + + if fields.trade then + local item = player:get_wielded_item() + + local trade_wanted1 = inv:get_stack("gold_trade_wanted", 1):to_string() + local trade_wanted2 = inv:get_stack("gold_trade_wanted", 2):to_string() + + local trade_in1 = inv:get_stack("gold_trade_in", 1):to_string() + local trade_in2 = inv:get_stack("gold_trade_in", 2):to_string() + + local matches = trade_wanted1 == trade_in1 and trade_wanted2 == trade_in2 + + local meta = minetest.deserialize(item:get_metadata()) + + local trade = {"gold:gold", "gold:gold", "default:stick"} + local trade_type = "" + + if meta then + trade = meta.trade + trade_type = meta.trade_type + end + + if matches then + if inv:room_for_item("gold_trade_out", trade[3]) then + inv:add_item("gold_trade_out", trade[3]) + inv:set_stack("gold_trade_in", 1, "") + inv:set_stack("gold_trade_in", 2, "") + end + end + end +end + +minetest.register_on_player_receive_fields(on_player_recieve_fields) + +default.log("mod:crafting", "loaded") diff --git a/mods/craftingguide/README.txt b/mods/craftingguide/README.txt deleted file mode 100644 index 6e72fcd..0000000 --- a/mods/craftingguide/README.txt +++ /dev/null @@ -1,9 +0,0 @@ -Crafting guide mod -================== - -By Kaadmy, for Pixture - -This mod adds a crafting guide in your inventory. - -Asset license: CC BY-SA 4.0 -Source license: LGPLv2.1 diff --git a/mods/craftingguide/depends.txt b/mods/craftingguide/depends.txt deleted file mode 100644 index 4ad96d5..0000000 --- a/mods/craftingguide/depends.txt +++ /dev/null @@ -1 +0,0 @@ -default diff --git a/mods/craftingguide/init.lua b/mods/craftingguide/init.lua deleted file mode 100644 index 5315454..0000000 --- a/mods/craftingguide/init.lua +++ /dev/null @@ -1,183 +0,0 @@ --- --- Crafting guide mod --- By Kaadmy, for Pixture --- - -craftingguide = {} -craftingguide.items = {} -craftingguide.itemlist = {} -craftingguide.users = {} -- {item = selected item, itemno = recipe no., page = page no.} - -local page_size = 8 * 4 - -function craftingguide.get_formspec(name) - local user = craftingguide.users[name] - - local page = user.page - local max_pages = math.floor(#craftingguide.itemlist / page_size) + 1 - - local form = "" - form = form .. default.ui.get_page("core_craftingguide") - - form = form .. "label[0.41,1.74;"..user.itemno.."/"..#craftingguide.items[user.item].."]" -- itemno - form = form .. "label[3.9,8.15;"..page.."/"..max_pages.."]" -- page - form = form .. "label[4.4,2.5;"..minetest.formspec_escape(minetest.registered_items[user.item].description).."]" -- itemname - - local method = craftingguide.items[user.item][user.itemno].type - if method == "normal" or method == "crafting" then - form = form .. "image[4.25,1.5;1,1;craftingguide_method_crafting.png]" - elseif method == "cooking" then - form = form .. "image[4.25,1.5;1,1;craftingguide_method_cooking.png]" --- fuel recipes are different --- elseif method == "fuel" then --- form = form .. "image[4.25,1.5;1,1;craftingguide_method_fuel.png]" - else - form = form .. "image[4.25,1.5;1,1;craftingguide_method_unknown.png]" - form = form .. "label[4.1,1.73;"..method.."]" - end - - local recipes = craftingguide.items[user.item] - local recipe = recipes[user.itemno] - - form = form .. default.ui.fake_itemstack(6.25, 1.5, ItemStack(recipe.output), "guide_craftresult") - --- print(dump(recipe)) - for slot_index, itemname in pairs(recipe.items) do - local x = slot_index - 1 - - local group = string.match(itemname, "group:(.*)") - - local w = 3 - if recipe.width ~= 0 then - w = recipe.width - end - - if group == nil then - form = form .. default.ui.fake_simple_itemstack(1.25 + (x % w), 0.5 + math.floor(x / w), itemname, "guide_craftgrid_"..itemname) - else - form = form .. default.ui.item_group(1.25 + (x % w), 0.5 + math.floor(x / w), group, "guide_craftgrid_"..itemname) - end - end - - local page_start = ((page * page_size) - page_size) + 1 - - local inv_x = 0 - local inv_y = 0 - - for item_index = page_start, (page_start + page_size) - 1 do - local recipes = craftingguide.items[craftingguide.itemlist[item_index]] - - if recipes ~= nil then - local itemname = ItemStack(recipes[1].output):get_name() - - form = form .. default.ui.fake_simple_itemstack(0.25 + inv_x, 4 + inv_y, itemname, "guide_item_"..itemname) - - inv_x = inv_x + 1 - if inv_x >= 8 then - inv_x = 0 - inv_y = inv_y + 1 - end - else - break - end - end - - return form -end - -local function receive_fields(player, form_name, fields) - if form_name == "core_craftingguide" and not fields.quit then - local name = player:get_player_name() - local user = craftingguide.users[name] - - local page = user.page - local recipes = craftingguide.items[user.item] - local itemno = user.itemno - - local max_pages = math.floor(#craftingguide.itemlist / page_size) + 1 - - if fields.guide_next_recipe then - if itemno < #recipes then - itemno = itemno + 1 - else - itemno = 1 - end - elseif fields.guide_prev_recipe then - if itemno <= 1 then - itemno = #recipes - else - itemno = itemno - 1 - end - end - - - if fields.guide_next and page < max_pages then - page = page + 1 - elseif fields.guide_prev and page > 1 then - page = page - 1 - end - - for fieldname, val in pairs(fields) do - local itemname = string.match(fieldname, "guide_item_(.*)") - - if itemname ~= nil then - itemno = 1 - craftingguide.users[name].item = itemname - end - end - - craftingguide.users[name].page = page - craftingguide.users[name].itemno = itemno - - minetest.show_formspec(name, "core_craftingguide", craftingguide.get_formspec(name)) - end -end - -local function on_joinplayer(player) - local name = player:get_player_name() - - craftingguide.users[name] = {page = 1, item = craftingguide.itemlist[1], itemno = 1} -end - -local function on_leaveplayer(player) - local name = player:get_player_name() - - craftingguide.users[name] = nil -end - -local function load_recipes() - for itemname, itemdef in pairs(minetest.registered_items) do - local recipes = minetest.get_all_craft_recipes(itemname) - - if recipes ~= nil and itemname ~= "" then --- print(dump(recipes)) - craftingguide.items[itemname] = recipes - table.insert(craftingguide.itemlist, itemname) - end - end - - table.sort(craftingguide.itemlist) - - print("Got "..#craftingguide.itemlist.." craftable items") -end - -minetest.after(0, load_recipes) - -minetest.register_on_joinplayer(on_joinplayer) -minetest.register_on_leaveplayer(on_leaveplayer) -minetest.register_on_player_receive_fields(receive_fields) - -local form_craftingguide = default.ui.get_page("core") -form_craftingguide = form_craftingguide .. default.ui.get_itemslot_bg(0.25, 4, 8, 4) -form_craftingguide = form_craftingguide .. default.ui.image_button(2.5, 7.9, 1, 1, "guide_prev", "ui_arrow_bg.png^[transformR90") -form_craftingguide = form_craftingguide .. default.ui.image_button(5, 7.9, 1, 1, "guide_next", "ui_arrow_bg.png^[transformR270") - -form_craftingguide = form_craftingguide .. default.ui.image_button(0.25, 0.5, 1, 1, "guide_next_recipe", "ui_arrow_bg.png") -form_craftingguide = form_craftingguide .. default.ui.image_button(0.25, 2.5, 1, 1, "guide_prev_recipe", "ui_arrow_bg.png^[transformFY") -form_craftingguide = form_craftingguide .. default.ui.get_itemslot_bg(1.25, 0.5, 3, 3) -form_craftingguide = form_craftingguide .. default.ui.get_itemslot_bg(6.25, 1.5, 1, 1) -form_craftingguide = form_craftingguide .. "image[5.25,1.5;1,1;"..minetest.formspec_escape("ui_arrow.png^[transformR270").."]" - -default.ui.register_page("core_craftingguide", form_craftingguide) - -default.log("mod:craftingguide", "loaded") diff --git a/mods/craftingguide/textures/craftingguide_method_cooking.png b/mods/craftingguide/textures/craftingguide_method_cooking.png deleted file mode 100644 index 9a4ed1dd8d22b9f538e81affc31afe89a21e90a9..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 268 zcmeAS@N?(olHy`uVBq!ia0vp^0wB!61|;P_|4#%`Y)RhkE)4%caKYZ?lYt_f1s;*b z3=G`DAk4@xYmNj^kiEpy*OmP~hb*Hk|K)YJlYv5#C9V-A&iT2ysd*&~&PAz-C8;S2 z<(VZJ3hti10pX2&;y^_uo-U3d7N?W{{QqyytlH4p*w}dR$dMMEv+3vNG)geAF0pEz z2o!7VY;-c<;Ym2C`ti@tVv&7ImZ*HxtN8cF^6<@`QAXS#Xx3cD$J;)oF_#{47U%?m? zab{U8jhNVhVZNF9X1~oY9uF3D<+#-4K2yLn>4;Cj34zZUczgV+DCl74;7#xY@Jor2 zcLDqf#GVJsnobl1EKDI+uz7kLGdpE7`oz;Yo!s3&U`G>F9@ri;y_nCi25h1Xl637e znnXUI4LbwZb0R;A=|BWC_NF$yxxNur14gnngwbiuXxXQK0~}o?3)|tLM@OX=6xVe{ zSu>X*Pbm+bIZJRpa@~ZbKO4m9-=(6mJBsHZjl7izVtGG!qTR9WJbH1o48^gjl7_c} zx08dnB}V$8#1NFL3hq>Jw}OurJa+G@;k9K8%e4ucbn!TAv~#n=iv6qVNs{cz{sktxZ8JVzIoI|4%40jBG1} w8ObU_8FVusNTEy$iMXGTZW#Jj2s5HrgmmcYK#)SZ6cVu%>MPy6v4{7SZ}im96951J diff --git a/mods/craftingguide/textures/craftingguide_method_crafting.png b/mods/craftingguide/textures/craftingguide_method_crafting.png deleted file mode 100644 index 47a908d037189abbc030b21c9de6cea88c98015e..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 200 zcmeAS@N?(olHy`uVBq!ia0vp^0wB!61|;P_|4#%`Y)RhkE)4%caKYZ?lYt_f1s;*b z3=G`DAk4@xYmNj^kiEpy*OmP~hb$utm&K8`FF+y564!_l=ltB<)VvY~=c3falGGH1 z^30M91$R&1fbd2>aiAgtPZ!4!i_^(}{{OdUR&8i)Y-~JuAEZdFzG_Z~B!ACIhNqofO4D1?+ zG$HlsB;U?AGv5r8UATzXbnAyS6cHm}mI87cU>I;*1N!q%LJ(j#fD^z4;7)PW-GY7r ze#Bv{>!)$d5(zQE;?-luJjnw3Cgh4Po}OQ@VwF-6dz`sE69MKxZ4f7yyNJ;|jYKdW zI3f4bD9xx-Iyd7>k4_#YrL~Z$DvhCa+0v@(^f1BJZNBz=k#IWI--6_>Eva(uCJ~Z~ z#9y%-`_nMYnfzH0yML#uOtTg5L7DNT8F>9Zc%#F)=fCnS-6Z9;nM!sF9$%*b^c6?> zx#Hl+eT@%ld|2c7x;O`I1kn5vIL_5}21M8NpP_ewk|~r>uMS%Cy_qvVFt&=`Y+uaG q>M%M-c4uXC=COTN@xSSzclyJfl3uYVg;^a&=g97?Y|cEk&m!O4gOz9i diff --git a/mods/craftingguide/textures/craftingguide_method_fuel.png b/mods/craftingguide/textures/craftingguide_method_fuel.png deleted file mode 100644 index 70744ace88f900d7d881e8762d8896dcd7ad8252..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 300 zcmeAS@N?(olHy`uVBq!ia0vp^0wB!61|;P_|4#%`Y)RhkE)4%caKYZ?lYt_f1s;*b z3=G`DAk4@xYmNj^kiEpy*OmP~hb*(Wl)D6T4p2z4#5JPCIX^cyHLrxhxhOTUBsE2$ zJhLQ2!QIn0AiR-J9H?lDr;B5V#p$(^FY+E%;Bh%0x-XA;N#8-K9KqBTYXVcb-ydZA z*Zsm@NL9pD;FW%JiU0G%9$uWbrVI_=|4obt{pgfl%G~xxiD7zxU4sc@Q6R&mvU>-M zxC>O4onbxG_2?SUlfn++PKo5d+Af>t#~t6wq1pE0g!79N|KB$DW&h>q+hdV=t9kR= mz5J(l^BK(cJRNcA_uTykw+ym^et!pfg2B_(&t;ucLK6T_Vr+E) diff --git a/mods/craftingguide/textures/craftingguide_method_fuel.xcf b/mods/craftingguide/textures/craftingguide_method_fuel.xcf deleted file mode 100644 index 7338825afa931b1992b7157a67041c5d2dfb1d11..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 1087 zcmbtT%}&BV5Z)~mK_ucY3GuKO6FgubJ@yT2h1&^ux%Wv3I@l?AQTn-z z$U6i6EyS7!k8Rt*Xyo`YtY--9KQbo>KY9@|?C8@&VR>O-UrQH0Kv%Y|V6?wwRQ1_zfTOR`*s{6rv98jB z;@h^OthJwg9y9LSk4}X1f$K(2yj&2cH%ot&{3xD-H2S3wMD>2~M5{f^e)7WL*-wv6 zle7{jokApO!Q<}`zayrYi zXOgWaC{Q!%BEN`QN;8V4l$KF9SJI|o!Zle+o3Mg8U(4x4N=x=cNt^$dZmdbK<+PEL fZZtNeZ%9E@$wI)-_alf9q(n-hOmtD%;g{qWd#>Wo diff --git a/mods/craftingguide/textures/craftingguide_method_unknown.png b/mods/craftingguide/textures/craftingguide_method_unknown.png deleted file mode 100644 index 51345980bf637754439dbca7b3abf35f73bf92a6..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 286 zcmeAS@N?(olHy`uVBq!ia0vp^0wB!61|;P_|4#%`Y)RhkE)4%caKYZ?lYt_f1s;*b z3=G`DAk4@xYmNj^kiEpy*OmP~hb*I{*yWYmmIH+(OI#yLobz*YQ}ap~oQqNuOHxx5 z$}>wc6x=<11Hv2m#DR+1JY5_^EKVN{zRlUZIsrl)!%@Od<=M^nl_ z0nb}E{=K;<=5gqEi^Ua&27#t)5z|=SUq1A#BXH|6PCWW ZVT@?ANEhyGKL>OXgQu&X%Q~loCIDAAY9s&v diff --git a/mods/craftingguide/textures/craftingguide_method_unknown.xcf b/mods/craftingguide/textures/craftingguide_method_unknown.xcf deleted file mode 100644 index e7623ccfae39aedfa4d32ea01f7fb1c0f68f7821..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 1028 zcmb7?%SyvQ6ozNUduy>?1JT7S>!Q*U@BtJN7lN-4(7u?8SNiuylemSI9^Ft3hY@jRasFLZ7f zEr&1Eiosck#UV4;~ipSHux#5;;uQ*#zm?ue-~d{a@&D|snjP_+on>QLm7N7UOWRd!>@L_+(O@gZ2IB>X%-#ZTGEp32Cq89g Dn7z%g diff --git a/mods/default/formspec.lua b/mods/default/formspec.lua index 68fe33c..1289ca2 100644 --- a/mods/default/formspec.lua +++ b/mods/default/formspec.lua @@ -171,17 +171,14 @@ form_core = form_core .. "size[8.5,9]" form_core = form_core .. default.ui.core.colors form_core = form_core .. default.ui.core.bg form_core = form_core .. default.ui.tab(-0.9, 0.5, "tab_crafting", "ui_icon_crafting.png", "Crafting") -if minetest.get_modpath("craftingguide") ~= nil then - form_core = form_core .. default.ui.tab(-0.9, 1.28, "tab_craftingguide", "ui_icon_craftingguide.png", "Crafting Guide") -end if minetest.get_modpath("armor") ~= nil then - form_core = form_core .. default.ui.tab(-0.9, 2.06, "tab_armor", "ui_icon_armor.png", "Armor") + form_core = form_core .. default.ui.tab(-0.9, 1.28, "tab_armor", "ui_icon_armor.png", "Armor") end if minetest.get_modpath("achievements") ~= nil then - form_core = form_core .. default.ui.tab(-0.9, 2.84, "tab_achievements", "ui_icon_achievements.png", "Achievements") + form_core = form_core .. default.ui.tab(-0.9, 2.06, "tab_achievements", "ui_icon_achievements.png", "Achievements") end if minetest.get_modpath("player_skins") ~= nil then - form_core = form_core .. default.ui.tab(-0.9, 3.62, "tab_player_skins", "ui_icon_player_skins.png", "Player Skins") + form_core = form_core .. default.ui.tab(-0.9, 2.84, "tab_player_skins", "ui_icon_player_skins.png", "Player Skins") end form_core = form_core .. "background[0,0;8.5,9;ui_formspec_bg_tall.png]" default.ui.register_page("core", form_core) @@ -238,8 +235,6 @@ function default.ui.receive_fields(player, form_name, fields) if fields.tab_crafting then minetest.show_formspec(name, "core_crafting", default.ui.get_page("core_crafting")) - elseif minetest.get_modpath("craftingguide") ~= nil and fields.tab_craftingguide then - minetest.show_formspec(name, "core_craftingguide", craftingguide.get_formspec(name)) elseif minetest.get_modpath("armor") ~= nil and fields.tab_armor then minetest.show_formspec(name, "core_armor", default.ui.get_page("core_armor")) elseif minetest.get_modpath("achievements") ~= nil and fields.tab_achievements then From 509827186fee85b16721687c954d9321ba383938 Mon Sep 17 00:00:00 2001 From: KaadmY Date: Sat, 13 May 2017 11:50:15 -0700 Subject: [PATCH 2/5] Cleanup a lot of interface stuff and standardize formspec page names --- mods/achievements/init.lua | 10 ++--- mods/armor/init.lua | 20 +++++---- mods/book/init.lua | 6 +-- mods/crafting/init.lua | 32 ++++++++++++-- mods/default/formspec.lua | 88 ++++++++++++++++---------------------- mods/gold/init.lua | 2 +- mods/jewels/init.lua | 8 ++-- mods/locks/init.lua | 2 +- mods/player_skins/init.lua | 14 +++--- mods/welcome/init.lua | 2 +- 10 files changed, 97 insertions(+), 87 deletions(-) diff --git a/mods/achievements/init.lua b/mods/achievements/init.lua index a279cf9..3bb18a3 100644 --- a/mods/achievements/init.lua +++ b/mods/achievements/init.lua @@ -173,10 +173,10 @@ minetest.register_on_placenode(on_place) -- Formspecs -local form = default.ui.get_page("core") +local form = default.ui.get_page("default:default") form = form .. "tableoptions[background=#DDDDDD30]" form = form .. "tablecolumns[text,align=left,width=11;text,align=left,width=28;text,align=left,width=5]" -default.ui.register_page("core_achievements", form) +default.ui.register_page("achievements:achievements", form) function achievements.get_formspec(name, row) if not row then row = 1 end @@ -211,7 +211,7 @@ function achievements.get_formspec(name, row) achievement_list = achievement_list .. progress end - local form = default.ui.get_page("core_achievements") + local form = default.ui.get_page("achievements:achievements") form = form .. "table[0.25,2.5;7.75,5.5;achievement_list;" .. achievement_list .. ";" .. row .. "]" @@ -242,7 +242,7 @@ end local function receive_fields(player, form_name, fields) local name = player:get_player_name() - if form_name ~= "core_achievements" then return end + if form_name ~= "achievements:achievements" then return end if fields.quit then return end @@ -256,7 +256,7 @@ local function receive_fields(player, form_name, fields) end end - minetest.show_formspec(name, "core_achievements", achievements.get_formspec(name, selected)) + minetest.show_formspec(name, "achievements:achievements", achievements.get_formspec(name, selected)) end minetest.register_on_player_receive_fields(receive_fields) diff --git a/mods/armor/init.lua b/mods/armor/init.lua index 0d0a987..fd0e384 100644 --- a/mods/armor/init.lua +++ b/mods/armor/init.lua @@ -6,7 +6,7 @@ armor = {} armor.player_skin = "character.png" -armor.update_time = 1 +armor.update_time = 3 armor.materials = { -- material craftitem % description @@ -18,8 +18,8 @@ armor.materials = { armor.slots = {"helmet", "chestplate", "boots"} -local form_armor = default.ui.get_page("core_2part") -default.ui.register_page("core_armor", form_armor) +local form_armor = default.ui.get_page("default:2part") +default.ui.register_page("armor:armor", form_armor) local enable_drop = minetest.setting_getbool("drop_items_on_die") or false @@ -87,7 +87,7 @@ function armor.get_groups(player) if armor.is_armor(itemname) then local item = minetest.registered_items[itemname] - + for mat_index, _ in ipairs(armor.materials) do local mat = armor.materials[mat_index][1] @@ -158,7 +158,7 @@ local function on_die(player) } local drop = minetest.add_item(rpos, item) - + if drop then drop:setvelocity( { @@ -190,7 +190,7 @@ minetest.register_craftitem( description = "Chainmail sheet", inventory_image = "armor_chainmail.png", - wield_image = "armor_chainmail.png", + wield_image = "armor_chainmail.png", stack_max = 20, }) @@ -264,12 +264,14 @@ end minetest.register_on_newplayer(on_newplayer) minetest.register_on_joinplayer(on_joinplayer) + if enable_drop then minetest.register_on_dieplayer(on_die) end + minetest.register_globalstep(step) -local form_armor = default.ui.get_page("core_2part") +local form_armor = default.ui.get_page("default:2part") form_armor = form_armor .. "list[current_player;main;0.25,4.75;8,4;]" form_armor = form_armor .. default.ui.get_hotbar_itemslot_bg(0.25, 4.75, 8, 1) form_armor = form_armor .. default.ui.get_itemslot_bg(0.25, 5.75, 8, 3) @@ -290,7 +292,7 @@ form_armor = form_armor .. "list[current_player;armor_helmet;2.25,0.75;1,1;]" form_armor = form_armor .. "listring[current_player;armor_helmet]" form_armor = form_armor .. default.ui.get_itemslot_bg(2.25, 0.75, 1, 1) -default.ui.register_page("core_armor", form_armor) +default.ui.register_page("armor:armor", form_armor) -- Achievements @@ -312,4 +314,4 @@ achievements.register_achievement( craftitem = "group:is_armor", }) -default.log("mod:armor", "loaded") \ No newline at end of file +default.log("mod:armor", "loaded") diff --git a/mods/book/init.lua b/mods/book/init.lua index 2f40895..0442f61 100644 --- a/mods/book/init.lua +++ b/mods/book/init.lua @@ -22,7 +22,7 @@ minetest.register_craftitem( title = data.title end - local form = default.ui.get_page("core_notabs") + local form = default.ui.get_page("default:notabs") form = form .. "field[0.5,1.25;8,0;title;Title:;"..minetest.formspec_escape(title).."]" form = form .. "textarea[0.5,1.75;8,6.75;text;Contents:;"..minetest.formspec_escape(text).."]" form = form .. default.ui.button_exit(2.75, 7.75, 3, 1, "write", "Write") @@ -41,7 +41,7 @@ minetest.register_on_player_receive_fields( if not data then data = {} end data.text = fields.text data.title = fields.title - + itemstack:set_metadata(minetest.serialize(data)) player:set_wielded_item(itemstack) end) @@ -57,4 +57,4 @@ achievements.register_achievement( craftitem = "default:book", }) -default.log("mod:book", "loaded") \ No newline at end of file +default.log("mod:book", "loaded") diff --git a/mods/crafting/init.lua b/mods/crafting/init.lua index 5645c46..c20127b 100644 --- a/mods/crafting/init.lua +++ b/mods/crafting/init.lua @@ -15,6 +15,7 @@ crafting.default_craftdef = { output = nil, description = "", items = {}, + groups = {}, } function crafting.register_craft(output, def) @@ -33,7 +34,8 @@ function crafting.register_craft(output, def) local craftdef = { output = itemstack, description = def.description or minetest.registered_items[itemn].description, - items = def.items or default.default_craftdef.items, + items = def.items or crafting.default_craftdef.items, + groups = def.groups or crafting.default_craftdef.groups, } if #craftdef.items > 4 then @@ -143,6 +145,28 @@ function crafting.craft(output, items) return items end +local form = default.ui.get_page("default:2part") +form = form .. "list[current_player;main;0.25,4.75;8,4;]" +form = form .. "listring[current_player;main]" +form = form .. default.ui.get_hotbar_itemslot_bg(0.25, 4.75, 8, 1) +form = form .. default.ui.get_itemslot_bg(0.25, 5.75, 8, 3) + +form = form .. "list[current_player;craft;2.25,0.75;3,3;]" +form = form .. "listring[current_player;craft]" + +form = form .. "image[5.25,1.75;1,1;ui_arrow_bg.png^[transformR270]" + +form = form .. "list[current_player;craftpreview;6.25,1.75;1,1;]" +form = form .. default.ui.get_itemslot_bg(2.25, 0.75, 3, 3) +form = form .. default.ui.get_itemslot_bg(6.25, 1.75, 1, 1) +default.ui.register_page("crafting:crafting", form) + +function crafting.get_formspec(name) + local form = default.ui.get_page("crafting:crafting") + + return form +end + crafting.register_craft( "default:stone 4", { @@ -156,14 +180,14 @@ crafting.register_craft( crafting.craft( "default:stone 2", { - {name = "default:stick", count = 4}, -- 0 leftover + "default:stick 4", -- 0 leftover {name = "default:stick", count = 5}, -- 3 leftover - {name = "default:fiber", count = 9}, -- 5 leftover + "default:fiber 9", -- 5 leftover {name = "group:stone", count = 4}, -- 0 leftover }) local function on_player_recieve_fields(player, form_name, fields) - if form_name ~= "crafting:crafting" or fields.cancel then return end + if form_name ~= "core:crafting" or fields.cancel then return end local inv = player:get_inventory() diff --git a/mods/default/formspec.lua b/mods/default/formspec.lua index 1289ca2..6cc31d1 100644 --- a/mods/default/formspec.lua +++ b/mods/default/formspec.lua @@ -1,9 +1,9 @@ default.ui = {} -default.ui.core = {} +default.ui.default = {} -default.ui.core.colors = "listcolors[#00000000;#00000010;#00000000;#68B259;#FFF]" -default.ui.core.bg = "bgcolor[#00000000;false]" +default.ui.default.colors = "listcolors[#00000000;#00000010;#00000000;#68B259;#FFF]" +default.ui.default.bg = "bgcolor[#00000000;false]" function default.ui.get_itemslot_bg(x, y, w, h) local out = "" @@ -166,58 +166,42 @@ function default.ui.register_page(name, form) default.ui.registered_pages[name] = form end -local form_core = "" -form_core = form_core .. "size[8.5,9]" -form_core = form_core .. default.ui.core.colors -form_core = form_core .. default.ui.core.bg -form_core = form_core .. default.ui.tab(-0.9, 0.5, "tab_crafting", "ui_icon_crafting.png", "Crafting") +local form_default_default = "" +form_default_default = form_default_default .. "size[8.5,9]" +form_default_default = form_default_default .. default.ui.default.colors +form_default_default = form_default_default .. default.ui.default.bg +form_default_default = form_default_default .. default.ui.tab(-0.9, 0.5, "tab_crafting", "ui_icon_crafting.png", "Crafting") if minetest.get_modpath("armor") ~= nil then - form_core = form_core .. default.ui.tab(-0.9, 1.28, "tab_armor", "ui_icon_armor.png", "Armor") + form_default_default = form_default_default .. default.ui.tab(-0.9, 1.28, "tab_armor", "ui_icon_armor.png", "Armor") end if minetest.get_modpath("achievements") ~= nil then - form_core = form_core .. default.ui.tab(-0.9, 2.06, "tab_achievements", "ui_icon_achievements.png", "Achievements") + form_default_default = form_default_default .. default.ui.tab(-0.9, 2.06, "tab_achievements", "ui_icon_achievements.png", "Achievements") end if minetest.get_modpath("player_skins") ~= nil then - form_core = form_core .. default.ui.tab(-0.9, 2.84, "tab_player_skins", "ui_icon_player_skins.png", "Player Skins") + form_default_default = form_default_default .. default.ui.tab(-0.9, 2.84, "tab_player_skins", "ui_icon_player_skins.png", "Player Skins") end -form_core = form_core .. "background[0,0;8.5,9;ui_formspec_bg_tall.png]" -default.ui.register_page("core", form_core) -default.ui.register_page("core_2part", form_core .. "background[0,0;8.5,4.5;ui_formspec_bg_short.png]") +form_default_default = form_default_default .. "background[0,0;8.5,9;ui_formspec_bg_tall.png]" +default.ui.register_page("default:default", form_default_default) +default.ui.register_page("default:2part", form_default_default .. "background[0,0;8.5,4.5;ui_formspec_bg_short.png]") -local form_core_notabs = "" -form_core_notabs = form_core_notabs .. "size[8.5,9]" -form_core_notabs = form_core_notabs .. default.ui.core.colors -form_core_notabs = form_core_notabs .. default.ui.core.bg -form_core_notabs = form_core_notabs .. "background[0,0;8.5,9;ui_formspec_bg_tall.png]" -default.ui.register_page("core_notabs", form_core_notabs) -default.ui.register_page("core_notabs_2part", form_core_notabs .. "background[0,0;8.5,4.5;ui_formspec_bg_short.png]") +local form_default_notabs = "" +form_default_notabs = form_default_notabs .. "size[8.5,9]" +form_default_notabs = form_default_notabs .. default.ui.default.colors +form_default_notabs = form_default_notabs .. default.ui.default.bg +form_default_notabs = form_default_notabs .. "background[0,0;8.5,9;ui_formspec_bg_tall.png]" +default.ui.register_page("default:notabs", form_default_notabs) +default.ui.register_page("default:notabs_2part", form_default_notabs .. "background[0,0;8.5,4.5;ui_formspec_bg_short.png]") -local form_core_field = "" -form_core_field = form_core_field .. "size[8.5,5]" -form_core_field = form_core_field .. default.ui.core.colors -form_core_field = form_core_field .. default.ui.core.bg -form_core_field = form_core_field .. "background[0,0;8.5,4.5;ui_formspec_bg_short.png]" -form_core_field = form_core_field .. default.ui.button_exit(2.75, 3, 3, 1, "", "Write", false) -form_core_field = form_core_field .. "field[1,1.75;7,0;text;;${text}]" -default.ui.register_page("core_field", form_core_field) +local form_default_field = "" +form_default_field = form_default_field .. "size[8.5,5]" +form_default_field = form_default_field .. default.ui.default.colors +form_default_field = form_default_field .. default.ui.default.bg +form_default_field = form_default_field .. "background[0,0;8.5,4.5;ui_formspec_bg_short.png]" +form_default_field = form_default_field .. default.ui.button_exit(2.75, 3, 3, 1, "", "Write", false) +form_default_field = form_default_field .. "field[1,1.75;7,0;text;;${text}]" +default.ui.register_page("default:field", form_default_field) -local form_crafting = default.ui.get_page("core_2part") -form_crafting = form_crafting .. "list[current_player;main;0.25,4.75;8,4;]" -form_crafting = form_crafting .. "listring[current_player;main]" -form_crafting = form_crafting .. default.ui.get_hotbar_itemslot_bg(0.25, 4.75, 8, 1) -form_crafting = form_crafting .. default.ui.get_itemslot_bg(0.25, 5.75, 8, 3) - -form_crafting = form_crafting .. "list[current_player;craft;2.25,0.75;3,3;]" -form_crafting = form_crafting .. "listring[current_player;craft]" - -form_crafting = form_crafting .. "image[5.25,1.75;1,1;ui_arrow_bg.png^[transformR270]" - -form_crafting = form_crafting .. "list[current_player;craftpreview;6.25,1.75;1,1;]" -form_crafting = form_crafting .. default.ui.get_itemslot_bg(2.25, 0.75, 3, 3) -form_crafting = form_crafting .. default.ui.get_itemslot_bg(6.25, 1.75, 1, 1) -default.ui.register_page("core_crafting", form_crafting) - -local form_bookshelf = default.ui.get_page("core_2part") +local form_bookshelf = default.ui.get_page("default:2part") form_bookshelf = form_bookshelf .. "list[current_player;main;0.25,4.75;8,4;]" form_bookshelf = form_bookshelf .. "listring[current_player;main]" form_bookshelf = form_bookshelf .. default.ui.get_hotbar_itemslot_bg(0.25, 4.75, 8, 1) @@ -226,7 +210,7 @@ form_bookshelf = form_bookshelf .. default.ui.get_itemslot_bg(0.25, 5.75, 8, 3) form_bookshelf = form_bookshelf .. "list[current_name;main;2.25,1.25;4,2;]" form_bookshelf = form_bookshelf .. "listring[current_name;main]" form_bookshelf = form_bookshelf .. default.ui.get_itemslot_bg(2.25, 1.25, 4, 2) -default.ui.register_page("core_bookshelf", form_bookshelf) +default.ui.register_page("default:bookshelf", form_bookshelf) function default.ui.receive_fields(player, form_name, fields) local name = player:get_player_name() @@ -234,13 +218,13 @@ function default.ui.receive_fields(player, form_name, fields) -- print("Received formspec fields from '"..name.."': "..dump(fields)) if fields.tab_crafting then - minetest.show_formspec(name, "core_crafting", default.ui.get_page("core_crafting")) + minetest.show_formspec(name, "crafting:crafting", crafting.get_formspec(name)) elseif minetest.get_modpath("armor") ~= nil and fields.tab_armor then - minetest.show_formspec(name, "core_armor", default.ui.get_page("core_armor")) + minetest.show_formspec(name, "armor:armor", default.ui.get_page("armor:armor")) elseif minetest.get_modpath("achievements") ~= nil and fields.tab_achievements then - minetest.show_formspec(name, "core_achievements", achievements.get_formspec(name)) + minetest.show_formspec(name, "achievements:achievements", achievements.get_formspec(name)) elseif minetest.get_modpath("player_skins") ~= nil and fields.tab_player_skins then - minetest.show_formspec(name, "core_player_skins", player_skins.get_formspec(name)) + minetest.show_formspec(name, "player_skins:player_skins", player_skins.get_formspec(name)) end end @@ -251,5 +235,5 @@ end) minetest.register_on_joinplayer( function(player) - player:set_inventory_formspec(default.ui.get_page("core_crafting")) + player:set_inventory_formspec(default.ui.get_page("crafting:crafting")) end) diff --git a/mods/gold/init.lua b/mods/gold/init.lua index 14e09f0..657f198 100644 --- a/mods/gold/init.lua +++ b/mods/gold/init.lua @@ -131,7 +131,7 @@ end local form_trading = "" -form_trading = form_trading .. default.ui.get_page("core_2part") +form_trading = form_trading .. default.ui.get_page("default:2part") form_trading = form_trading .. "list[current_player;gold_trade_out;3.75,2.25;1,1;]" form_trading = form_trading .. "listring[current_player;gold_trade_out]" diff --git a/mods/jewels/init.lua b/mods/jewels/init.lua index ff93e2d..22da4b9 100644 --- a/mods/jewels/init.lua +++ b/mods/jewels/init.lua @@ -99,7 +99,7 @@ function jewels.register_jewel(toolname, new_toolname, def) end end - new_tooldef.description = desc + new_tooldef.description = desc minetest.register_tool(new_toolname, new_tooldef) end @@ -156,7 +156,7 @@ minetest.register_node( end, on_punch = function(pos, node, player, pointed_thing) local itemstack = player:get_wielded_item() - + if itemstack:get_name() == "jewels:jewel" then local meta = minetest.get_meta(pos) local inv = meta:get_inventory() @@ -170,7 +170,7 @@ minetest.register_node( achievements.trigger_achievement(player, "jeweler") achievements.trigger_achievement(player, "master_jeweler") - end + end end player:set_wielded_item(itemstack) @@ -187,7 +187,7 @@ minetest.register_craft( } }) -local form_bench = default.ui.get_page("core_2part") +local form_bench = default.ui.get_page("default:2part") form_bench = form_bench .. "list[current_name;main;2.25,1.75;1,1;]" form_bench = form_bench .. "listring[current_name;main]" form_bench = form_bench .. default.ui.get_itemslot_bg(2.25, 1.75, 1, 1) diff --git a/mods/locks/init.lua b/mods/locks/init.lua index 745c90a..2cc2675 100644 --- a/mods/locks/init.lua +++ b/mods/locks/init.lua @@ -134,7 +134,7 @@ minetest.register_node( end local np = pos.x .. "," .. pos.y .. "," .. pos.z - local form = default.ui.get_page("core_2part") + local form = default.ui.get_page("default:2part") form = form .. "list[nodemeta:" .. np .. ";main;0.25,0.25;8,4;]" form = form .. "listring[nodemeta:" .. np .. ";main]" form = form .. default.ui.get_itemslot_bg(0.25, 0.25, 8, 4) diff --git a/mods/player_skins/init.lua b/mods/player_skins/init.lua index 724b463..18d9622 100644 --- a/mods/player_skins/init.lua +++ b/mods/player_skins/init.lua @@ -16,7 +16,7 @@ player_skins.skins = {} local update_time = 1 local timer = 10 -local skins_file = minetest.get_worldpath() .. "/player_skins" +local skins_file = minetest.get_worldpath() .. "/player_skins.dat" local function save_skins() local f = io.open(skins_file, "w") @@ -60,7 +60,7 @@ end function player_skins.get_skin(name) return "player_skins_" .. player_skins.skins[name] .. ".png" end - + function player_skins.set_skin(name, tex) if minetest.check_player_privs(name, {player_skin = true}) then if is_valid_skin(tex) then @@ -115,7 +115,7 @@ local function get_chatparams() end function player_skins.get_formspec(playername) - local form = default.ui.get_page("core") + local form = default.ui.get_page("default:default") form = form .. "image[4,0;0.5,10.05;ui_vertical_divider.png]" @@ -129,7 +129,7 @@ function player_skins.get_formspec(playername) end form = form .. default.ui.button(x, y, 2, 1, "skin_select_" .. name, player_skins.skin_names[i]) - form = form .. "image[" .. (x + 2.25) .. "," .. y.. ";1,1;player_skins_icon_" .. name .. ".png]" + form = form .. "image[" .. (x + 2.25) .. "," .. y.. ";1,1;player_skins_icon_" .. name .. ".png]" if player_skins.skins[playername] == name then form = form .. "image[" .. (x + 3.25) .. "," .. (y + 0.25).. ";0.5,0.5;ui_checkmark.png]" end @@ -148,7 +148,7 @@ minetest.register_on_player_receive_fields( if skinname ~= nil then player_skins.set_skin(name, skinname) - minetest.show_formspec(name, "core_player_skins", player_skins.get_formspec(name)) + minetest.show_formspec(name, "player_skins:player_skins", player_skins.get_formspec(name)) end end end) @@ -164,7 +164,7 @@ minetest.register_chatcommand( if is_valid_skin(param) then player_skins.set_skin(name, param) elseif param == "" then - minetest.chat_send_player(name, "Current player skin: " .. player_skins.skins[name]) + minetest.chat_send_player(name, "Current player skin: " .. player_skins.skins[name]) else minetest.chat_send_player(name, "Bad param for /player_skin; type /help player_skin") end @@ -173,4 +173,4 @@ minetest.register_chatcommand( minetest.after(1.0, load_skins) -default.log("mod:player_skins", "loaded") \ No newline at end of file +default.log("mod:player_skins", "loaded") diff --git a/mods/welcome/init.lua b/mods/welcome/init.lua index 1ef89ac..7f96a33 100644 --- a/mods/welcome/init.lua +++ b/mods/welcome/init.lua @@ -20,7 +20,7 @@ function welcome.get_formspec(name) return "" end - local form = default.ui.get_page("core_notabs") + local form = default.ui.get_page("default:notabs") local rules = "" From 3cef23e65bfd219c13f3d0a23039ed980450eaad Mon Sep 17 00:00:00 2001 From: KaadmY Date: Sat, 13 May 2017 15:15:43 -0700 Subject: [PATCH 3/5] New crafting system is in place and tests work; now have to hook up craft recipes --- mods/crafting/init.lua | 177 ++++++++++-------- mods/default/formspec.lua | 4 +- mods/default/furnace.lua | 32 ++-- mods/default/nodes.lua | 8 +- mods/default/textures/ui_button_1w_active.png | Bin 0 -> 294 bytes mods/default/textures/ui_button_1w_active.xcf | Bin 0 -> 1384 bytes .../textures/ui_button_1w_inactive.png | Bin 0 -> 325 bytes .../textures/ui_button_1w_inactive.xcf | Bin 0 -> 1151 bytes mods/nav/map.lua | 12 +- 9 files changed, 124 insertions(+), 109 deletions(-) create mode 100644 mods/default/textures/ui_button_1w_active.png create mode 100644 mods/default/textures/ui_button_1w_active.xcf create mode 100644 mods/default/textures/ui_button_1w_inactive.png create mode 100644 mods/default/textures/ui_button_1w_inactive.xcf diff --git a/mods/crafting/init.lua b/mods/crafting/init.lua index c20127b..050f3d0 100644 --- a/mods/crafting/init.lua +++ b/mods/crafting/init.lua @@ -53,41 +53,37 @@ function crafting.register_craft(output, def) minetest.log("info", "Registered recipe for " .. itemn .. ": " .. dump(crafting.registered_crafts[itemn])) end -function crafting.craft(output, items) +function crafting.craft(wanted, output, items) -- `output` can be any ItemStack value -- Duplicate items in `items` should work correctly - print(dump(output)) - - local wanted_itemstack = ItemStack(output) - - print(dump(wanted_itemstack:to_string())) - - local craftdef = crafting.registered_crafts[wanted_itemstack:get_name()] + local craftdef = crafting.registered_crafts[wanted:get_name()] if craftdef == nil then minetest.log("warning", - "Tried to craft an unregistered item " .. wanted_itemstack:get_name()) + "Tried to craft an unregistered item " .. wanted:get_name()) return nil end - --print("Craftdef items: " .. dump(craftdef.items)) - print("Input before: " .. dump(items)) - -- Check for validity - local craft_count = wanted_itemstack:get_count() + local craft_count = wanted:get_count() for i = 1, crafting.max_inputs do local required_itemstack = ItemStack(craftdef.items[i]) local itemc = 0 + local group = string.match(required_itemstack:get_name(), "group:(.*)") + if required_itemstack ~= nil and required_itemstack:get_count() ~= 0 then for j = 1, crafting.max_inputs do local input_itemstack = ItemStack(items[j]) - if input_itemstack:get_name() == required_itemstack:get_name() then + if (group ~= nil + and minetest.get_item_group(input_itemstack:get_name(), group) ~= 0) + or (input_itemstack ~= nil + and input_itemstack:get_name() == required_itemstack:get_name()) then itemc = itemc + input_itemstack:get_count() end end @@ -96,28 +92,42 @@ function crafting.craft(output, items) if craft_count < 1 then minetest.log("warning", - "Not enough items to craft " .. wanted_itemstack:get_name()) + "Not enough items to craft " .. wanted:to_string()) - return nil -- Not enough items + return nil end end end - --print("Craft count: " .. craft_count .. "/" .. output.count) + -- Put stuff in output stack + + craft_count = math.min(craft_count, math.floor(output:get_free_space() / craft_count)) + + if craft_count < 1 then + minetest.log("warning", + "Output stack too full to hold " .. wanted:to_string()) + + return nil + end + + output:add_item(ItemStack({name = wanted:get_name(), count = wanted:get_count() * craft_count})) -- Iterate through second time to take items used for crafting local function remove_used_item(itemn, count) local items_required = count + local group = string.match(itemn, "group:(.*)") + for i = 1, crafting.max_inputs do local input_itemstack = ItemStack(items[i]) - if items[i] ~= nil and input_itemstack:get_name() == itemn then + if (group ~= nil + and minetest.get_item_group(input_itemstack:get_name(), group) ~= 0) + or (items[i] ~= nil + and input_itemstack:get_name() == itemn) then local items_left = items_required - input_itemstack:get_count() - print("Taking " .. items_required .. " items from " .. itemn) - input_itemstack:take_item(items_required) if items_left > 0 then @@ -140,26 +150,30 @@ function crafting.craft(output, items) end end - print("Input after: " .. dump(items)) - - return items + return {items = items, output = output} end -local form = default.ui.get_page("default:2part") -form = form .. "list[current_player;main;0.25,4.75;8,4;]" -form = form .. "listring[current_player;main]" -form = form .. default.ui.get_hotbar_itemslot_bg(0.25, 4.75, 8, 1) -form = form .. default.ui.get_itemslot_bg(0.25, 5.75, 8, 3) +local form_crafting = default.ui.get_page("default:2part") -form = form .. "list[current_player;craft;2.25,0.75;3,3;]" -form = form .. "listring[current_player;craft]" +form_crafting = form_crafting .. "list[current_player;main;0.25,4.75;8,4;]" +form_crafting = form_crafting .. "listring[current_player;main]" +form_crafting = form_crafting .. default.ui.get_hotbar_itemslot_bg(0.25, 4.75, 8, 1) +form_crafting = form_crafting .. default.ui.get_itemslot_bg(0.25, 5.75, 8, 3) -form = form .. "image[5.25,1.75;1,1;ui_arrow_bg.png^[transformR270]" +form_crafting = form_crafting .. "list[current_player;craft_in;0.25,0.25;1,4;]" +form_crafting = form_crafting .. "listring[current_player;craft_in]" -form = form .. "list[current_player;craftpreview;6.25,1.75;1,1;]" -form = form .. default.ui.get_itemslot_bg(2.25, 0.75, 3, 3) -form = form .. default.ui.get_itemslot_bg(6.25, 1.75, 1, 1) -default.ui.register_page("crafting:crafting", form) +form_crafting = form_crafting .. "list[current_player;craft_out;7.25,0.25;1,1;]" + +form_crafting = form_crafting .. default.ui.get_itemslot_bg(0.25, 0.25, 1, 4) +form_crafting = form_crafting .. default.ui.get_itemslot_bg(7.25, 0.25, 1, 1) + +form_crafting = form_crafting .. "image[7.25,1.25;1,1;ui_arrow_bg.png]" + +form_crafting = form_crafting .. default.ui.button(7.25, 2.25, 1, 1, "do_craft_1", "1") +form_crafting = form_crafting .. default.ui.button(7.25, 3.25, 1, 1, "do_craft_10", "10") + +default.ui.register_page("crafting:crafting", form_crafting) function crafting.get_formspec(name) local form = default.ui.get_page("crafting:crafting") @@ -167,6 +181,52 @@ function crafting.get_formspec(name) return form end +local function on_joinplayer(player) + local inv = player:get_inventory() + + if inv:get_size("craft_in") ~= 4 then + inv:set_size("craft_in", 4) + end + + if inv:get_size("craft_out") ~= 1 then + inv:set_size("craft_out", 1) + end +end + +local function on_player_receive_fields(player, form_name, fields) + if form_name ~= "crafting:crafting" then + return + end + + local inv = player:get_inventory() + + local wanted_itemstack = ItemStack("default:stone 2") + local output_itemstack = inv:get_stack("craft_out", 1) + + if output_itemstack:get_name() ~= wanted_itemstack:get_name() and output_itemstack:get_count() ~= 0 then + minetest.log("warning", "Trying to craft " .. wanted_itemstack:to_string() .. " but output has too many/different items") + end + + if fields.do_craft_1 then + wanted_itemstack:set_count(1) + elseif fields.do_craft_10 then + wanted_itemstack:set_count(2) + else + return + end + + local crafted = crafting.craft(wanted_itemstack, output_itemstack, inv:get_list("craft_in")) + + if crafted and inv:room_for_item("craft_out", crafted.output) then + inv:add_item("craft_out", crafted.output) + + inv:set_list("craft_in", crafted.items) + end +end + +minetest.register_on_joinplayer(on_joinplayer) +minetest.register_on_player_receive_fields(on_player_receive_fields) + crafting.register_craft( "default:stone 4", { @@ -177,51 +237,4 @@ crafting.register_craft( }, }) -crafting.craft( - "default:stone 2", - { - "default:stick 4", -- 0 leftover - {name = "default:stick", count = 5}, -- 3 leftover - "default:fiber 9", -- 5 leftover - {name = "group:stone", count = 4}, -- 0 leftover -}) - -local function on_player_recieve_fields(player, form_name, fields) - if form_name ~= "core:crafting" or fields.cancel then return end - - local inv = player:get_inventory() - - if fields.trade then - local item = player:get_wielded_item() - - local trade_wanted1 = inv:get_stack("gold_trade_wanted", 1):to_string() - local trade_wanted2 = inv:get_stack("gold_trade_wanted", 2):to_string() - - local trade_in1 = inv:get_stack("gold_trade_in", 1):to_string() - local trade_in2 = inv:get_stack("gold_trade_in", 2):to_string() - - local matches = trade_wanted1 == trade_in1 and trade_wanted2 == trade_in2 - - local meta = minetest.deserialize(item:get_metadata()) - - local trade = {"gold:gold", "gold:gold", "default:stick"} - local trade_type = "" - - if meta then - trade = meta.trade - trade_type = meta.trade_type - end - - if matches then - if inv:room_for_item("gold_trade_out", trade[3]) then - inv:add_item("gold_trade_out", trade[3]) - inv:set_stack("gold_trade_in", 1, "") - inv:set_stack("gold_trade_in", 2, "") - end - end - end -end - -minetest.register_on_player_receive_fields(on_player_recieve_fields) - default.log("mod:crafting", "loaded") diff --git a/mods/default/formspec.lua b/mods/default/formspec.lua index 6cc31d1..e3ddfb5 100644 --- a/mods/default/formspec.lua +++ b/mods/default/formspec.lua @@ -38,7 +38,9 @@ function default.ui.button(x, y, w, h, name, label, noclip) nc = "true" end - if w == 2 then + if w == 1 then + return "image_button["..x..","..y..";"..w..","..h..";ui_button_1w_inactive.png;"..name..";"..label..";"..nc..";false;ui_button_1w_active.png]" + elseif w == 2 then return "image_button["..x..","..y..";"..w..","..h..";ui_button_2w_inactive.png;"..name..";"..label..";"..nc..";false;ui_button_2w_active.png]" else return "image_button["..x..","..y..";"..w..","..h..";ui_button_3w_inactive.png;"..name..";"..label..";"..nc..";false;ui_button_3w_active.png]" diff --git a/mods/default/furnace.lua b/mods/default/furnace.lua index 9e985af..85372d5 100644 --- a/mods/default/furnace.lua +++ b/mods/default/furnace.lua @@ -3,7 +3,7 @@ -- function default.furnace_active_formspec(percent, item_percent) - local form = default.ui.get_page("core_2part") + local form = default.ui.get_page("default:2part") form = form .. "list[current_player;main;0.25,4.75;8,4;]" form = form .. "listring[current_player;main]" form = form .. default.ui.get_hotbar_itemslot_bg(0.25, 4.75, 8, 1) @@ -28,7 +28,7 @@ function default.furnace_active_formspec(percent, item_percent) return form end -local form_furnace = default.ui.get_page("core_2part") +local form_furnace = default.ui.get_page("default:2part") form_furnace = form_furnace .. "list[current_player;main;0.25,4.75;8,4;]" form_furnace = form_furnace .. "listring[current_player;main]" form_furnace = form_furnace .. default.ui.get_hotbar_itemslot_bg(0.25, 4.75, 8, 1) @@ -142,7 +142,7 @@ minetest.register_abm( local fuel_time = meta:get_float("fuel_time") or 0 local src_time = meta:get_float("src_time") or 0 local fuel_totaltime = meta:get_float("fuel_totaltime") or 0 - + -- -- Initialize inventory -- @@ -160,24 +160,24 @@ minetest.register_abm( local srclist = inv:get_list("src") local fuellist = inv:get_list("fuel") local dstlist = inv:get_list("dst") - + -- -- Cooking -- - + -- Check if we have cookable content local cooked, aftercooked = minetest.get_craft_result({method = "cooking", width = 1, items = srclist}) local cookable = true - + if cooked.time == 0 then cookable = false end - + -- Check if we have enough fuel to burn if fuel_time < fuel_totaltime then -- The furnace is currently active and has enough fuel fuel_time = fuel_time + 1 - + -- If there is a cookable item then check if it is ready yet if cookable then src_time = src_time + 1 @@ -195,7 +195,7 @@ minetest.register_abm( if cookable then -- We need to get new fuel local fuel, afterfuel = minetest.get_craft_result({method = "fuel", width = 1, items = fuellist}) - + if fuel.time == 0 then -- No valid fuel in fuel list fuel_totaltime = 0 @@ -204,10 +204,10 @@ minetest.register_abm( else -- Take fuel from fuel list inv:set_stack("fuel", 1, afterfuel.items[1]) - + fuel_totaltime = fuel.time fuel_time = 0 - + end else -- We don't need to get new fuel since there is no cookable item @@ -216,7 +216,7 @@ minetest.register_abm( src_time = 0 end end - + -- -- Update formspec, infotext and node -- @@ -233,7 +233,7 @@ minetest.register_abm( item_state = "Not cookable" end end - + local fuel_state = "Empty" local active = "inactive " if fuel_time <= fuel_totaltime and fuel_totaltime ~= 0 then @@ -248,9 +248,9 @@ minetest.register_abm( end swap_node(pos, "default:furnace") end - + local infotext = "Furnace " .. active .. "(Item: " .. item_state .. "; Fuel: " .. fuel_state .. ")" - + -- -- Set meta values -- @@ -262,4 +262,4 @@ minetest.register_abm( end, }) -default.log("furnace", "loaded") \ No newline at end of file +default.log("furnace", "loaded") diff --git a/mods/default/nodes.lua b/mods/default/nodes.lua index 0b13248..9010cb8 100644 --- a/mods/default/nodes.lua +++ b/mods/default/nodes.lua @@ -662,7 +662,7 @@ minetest.register_node( sounds = default.node_sound_wood_defaults(), on_construct = function(pos) local meta = minetest.get_meta(pos) - meta:set_string("formspec", default.ui.get_page("core_bookshelf")) + meta:set_string("formspec", default.ui.get_page("default:bookshelf")) meta:set_string("infotext", "Bookshelf") local inv = meta:get_inventory() inv:set_size("main", 4*2) @@ -1226,7 +1226,7 @@ minetest.register_node( on_construct = function(pos) --local n = minetest.get_node(pos) local meta = minetest.get_meta(pos) - meta:set_string("formspec", default.ui.get_page("core_field")) + meta:set_string("formspec", default.ui.get_page("default:field")) meta:set_string("infotext", '""') meta:set_string("text", "") end, @@ -1483,7 +1483,7 @@ minetest.register_node( end, }) -local form_chest = default.ui.get_page("core_2part") +local form_chest = default.ui.get_page("default:2part") form_chest = form_chest .. "list[current_name;main;0.25,0.25;8,4;]" form_chest = form_chest .. "listring[current_name;main]" form_chest = form_chest .. default.ui.get_itemslot_bg(0.25, 0.25, 8, 4) @@ -1492,6 +1492,6 @@ form_chest = form_chest .. "list[current_player;main;0.25,4.75;8,4;]" form_chest = form_chest .. "listring[current_player;main]" form_chest = form_chest .. default.ui.get_hotbar_itemslot_bg(0.25, 4.75, 8, 1) form_chest = form_chest .. default.ui.get_itemslot_bg(0.25, 5.75, 8, 3) -default.ui.register_page("default_chest", form_chest) +default.ui.register_page("default:chest", form_chest) default.log("nodes", "loaded") diff --git a/mods/default/textures/ui_button_1w_active.png b/mods/default/textures/ui_button_1w_active.png new file mode 100644 index 0000000000000000000000000000000000000000..a3c3fc7f924270116738ce84b2c3f7aa116a7378 GIT binary patch literal 294 zcmeAS@N?(olHy`uVBq!ia0vp^0wB!61|;P_|4#%`Y)RhkE)4%caKYZ?lYt_f1s;*b z3=G`DAk4@xYmNj^kiEpy*OmPtE3dGHsDDZUP>w-1GbEzKIX^cyHLnE7WngeFN=+$`hq_^$*x*VEO{Wt~$(695}ZU)KNt literal 0 HcmV?d00001 diff --git a/mods/default/textures/ui_button_1w_active.xcf b/mods/default/textures/ui_button_1w_active.xcf new file mode 100644 index 0000000000000000000000000000000000000000..3511a64200d713f1a434f6da805be84c7da97e7f GIT binary patch literal 1384 zcmc&zO^ee&7@j2E^vhuV1cQg*sf$$B9R$xSA|49PtMh05IT>ndJlA# zG5fP2m0Z-?>D`$9jrL!mvcenY7B1@+E!NX;Sic}t`y=Gqs5zy^YBK%G{@G-D8LgA+ zm3~1sbvaH&FjQHB__2xt+| zA~MFga0Rr8J>#MyaxSbo&6)7f2-loH46rby)Plj8FGQ(j3@u}Qut(oUoa$#hg?XV2T({hGbEzKIX^cyHLnE7WngeFN=+{|O-opw4uJR|mC-BB>c63YNnAI~usoH>L zvW2PgoGE(37OlQ49@8AYg?jw|xBox?+$8;MU50ySd!}feWH|lVRxQAf&v30|#@v<} zT5-oh4Qt=bz+V53pCDgdxhp*{$Pv@*K4#1 z+IzIOR#V*@tiM9c&9-40FViAh zQw*y)Ta?wqCKhGB$<}AbizJQmO-9)$pBMiY)Ougu zhEcXl#YKM?1|PS>5?!QA_q9PqNlIZqIp(R`H|J=*t>x~9_aZ%KO1O&pr50rx&4}KNe0v&mzSEgA4s+cwK-Y3;GzwR{z|m%!gQE{EXiqnw`A{~k_O;o@^ZOP+ zPwjxf40YgVpfy0?1n#37@JG5X;P%iX7#>5Vx=@W}iUug8LYpb1QimFpry5XvD4SaO z%53BLeGQNu)xXNu-ho2GWxaNIsMe xlRmcLHlE)z06Db-43#7*DLbkaQmyDZqA~}A;1iQPWk>CV&0F_BY=X6?>=*rq0)qem literal 0 HcmV?d00001 diff --git a/mods/nav/map.lua b/mods/nav/map.lua index 59dd7de..9b2ea0f 100644 --- a/mods/nav/map.lua +++ b/mods/nav/map.lua @@ -4,7 +4,7 @@ nav.map_radius = 256 nav.waypoints = {} -local form_nav = default.ui.get_page("core") +local form_nav = default.ui.get_page("default:default") default.ui.register_page("nav:nav", form_nav) local open_formspecs = {} @@ -29,7 +29,7 @@ function nav.get_waypoints_in_square(pos, radius) for name, data in pairs(nav.waypoints) do local wp = data.pos - + if wp.x > pos.x-radius and wp.x < pos.x+radius and wp.z > pos.z-radius and wp.z < pos.z+radius then table.insert(wpts, name) end @@ -45,7 +45,7 @@ local function get_formspec_waypoint(x, y, name, label, isinfo) end local form = "" - + form = form .. "image_button["..(x-0.72)..","..(y-0.53)..";0.5,0.5;"..img..";"..name..";;false;false;"..img.."]" form = form .. "tooltip["..name..";"..minetest.formspec_escape(label).."]" @@ -131,9 +131,9 @@ local function step(dtime) for _, player in pairs(minetest.get_connected_players()) do if player ~= nil then local name = player:get_player_name() - + players[name] = player - + nav.show_map(player) end end @@ -188,4 +188,4 @@ achievements.register_achievement( description = "Craft a map", times = 1, craftitem = "nav:map", - }) \ No newline at end of file + }) From 7e825c4a6ae9e00c5ed17a1e2ee29a130f39cc75 Mon Sep 17 00:00:00 2001 From: KaadmY Date: Sat, 13 May 2017 19:15:17 -0700 Subject: [PATCH 4/5] Almost entirely works except the output amount is incorrect --- mods/armor/init.lua | 2 +- mods/crafting/init.lua | 189 +++++++++++++++++++++++++++++--------- mods/default/formspec.lua | 2 +- 3 files changed, 148 insertions(+), 45 deletions(-) diff --git a/mods/armor/init.lua b/mods/armor/init.lua index fd0e384..46a7ead 100644 --- a/mods/armor/init.lua +++ b/mods/armor/init.lua @@ -6,7 +6,7 @@ armor = {} armor.player_skin = "character.png" -armor.update_time = 3 +armor.update_time = 1 armor.materials = { -- material craftitem % description diff --git a/mods/crafting/init.lua b/mods/crafting/init.lua index 050f3d0..711a955 100644 --- a/mods/crafting/init.lua +++ b/mods/crafting/init.lua @@ -7,13 +7,15 @@ crafting = {} crafting.registered_crafts = {} +-- User table of last selected row etc. +crafting.userdata = {} + -- Crafting can only take 4 itemstacks as input for sanity/interface reasons crafting.max_inputs = 4 -- Default crafting definition values crafting.default_craftdef = { output = nil, - description = "", items = {}, groups = {}, } @@ -26,14 +28,11 @@ function crafting.register_craft(output, def) if crafting.registered_crafts[itemn] ~= nil then minetest.log("warning", - "Tried to register an existing craft " .. itemn) - - return + "Tried to register an existing craft " .. itemn .. ", allowing") end local craftdef = { output = itemstack, - description = def.description or minetest.registered_items[itemn].description, items = def.items or crafting.default_craftdef.items, groups = def.groups or crafting.default_craftdef.groups, } @@ -50,7 +49,36 @@ function crafting.register_craft(output, def) crafting.registered_crafts[itemn] = craftdef - minetest.log("info", "Registered recipe for " .. itemn .. ": " .. dump(crafting.registered_crafts[itemn])) + minetest.log("info", "Registered recipe for " .. itemn) +end + +function crafting.get_crafts(filter) + local results = {} + + local function get_filter() + for craftname, craftdef in pairs(crafting.registered_crafts) do + for filtername, filtervalue in ipairs(filter) do + if craftdef[filtername] ~= nil and craftdef[filtername] == filtervalue then + table.insert(results, craftname) + break + end + end + end + end + + local function get_all() + for craftname, _ in pairs(crafting.registered_crafts) do + table.insert(results, craftname) + end + end + + if filter == nil then + get_all() + else + get_filter() + end + + return results end function crafting.craft(wanted, output, items) @@ -101,7 +129,11 @@ function crafting.craft(wanted, output, items) -- Put stuff in output stack - craft_count = math.min(craft_count, math.floor(output:get_free_space() / craft_count)) + print("Free space: " .. output:get_free_space()) + + if craft_count - output:get_free_space() > 0 then + craft_count = craft_count - (craft_count - output:get_free_space()) + end if craft_count < 1 then minetest.log("warning", @@ -110,7 +142,10 @@ function crafting.craft(wanted, output, items) return nil end - output:add_item(ItemStack({name = wanted:get_name(), count = wanted:get_count() * craft_count})) + output:add_item(ItemStack({ + name = craftdef.output:get_name(), + count = craftdef.output:get_count() * craft_count + })) -- Iterate through second time to take items used for crafting @@ -153,37 +188,72 @@ function crafting.craft(wanted, output, items) return {items = items, output = output} end -local form_crafting = default.ui.get_page("default:2part") +local form = default.ui.get_page("default:2part") -form_crafting = form_crafting .. "list[current_player;main;0.25,4.75;8,4;]" -form_crafting = form_crafting .. "listring[current_player;main]" -form_crafting = form_crafting .. default.ui.get_hotbar_itemslot_bg(0.25, 4.75, 8, 1) -form_crafting = form_crafting .. default.ui.get_itemslot_bg(0.25, 5.75, 8, 3) +form = form .. "field[-1,-1;0,0;crafting_tracker;;]" -form_crafting = form_crafting .. "list[current_player;craft_in;0.25,0.25;1,4;]" -form_crafting = form_crafting .. "listring[current_player;craft_in]" +form = form .. "list[current_player;main;0.25,4.75;8,4;]" +form = form .. "listring[current_player;main]" +form = form .. default.ui.get_hotbar_itemslot_bg(0.25, 4.75, 8, 1) +form = form .. default.ui.get_itemslot_bg(0.25, 5.75, 8, 3) -form_crafting = form_crafting .. "list[current_player;craft_out;7.25,0.25;1,1;]" +form = form .. "list[current_player;craft_in;0.25,0.25;1,4;]" +form = form .. "listring[current_player;craft_in]" -form_crafting = form_crafting .. default.ui.get_itemslot_bg(0.25, 0.25, 1, 4) -form_crafting = form_crafting .. default.ui.get_itemslot_bg(7.25, 0.25, 1, 1) +form = form .. "list[current_player;craft_out;7.25,0.25;1,1;]" -form_crafting = form_crafting .. "image[7.25,1.25;1,1;ui_arrow_bg.png]" +form = form .. default.ui.get_itemslot_bg(0.25, 0.25, 1, 4) +form = form .. default.ui.get_itemslot_bg(7.25, 0.25, 1, 1) -form_crafting = form_crafting .. default.ui.button(7.25, 2.25, 1, 1, "do_craft_1", "1") -form_crafting = form_crafting .. default.ui.button(7.25, 3.25, 1, 1, "do_craft_10", "10") +form = form .. "image[7.25,1.25;1,1;ui_arrow_bg.png]" -default.ui.register_page("crafting:crafting", form_crafting) +form = form .. default.ui.button(7.25, 2.25, 1, 1, "do_craft_1", "1") +form = form .. default.ui.button(7.25, 3.25, 1, 1, "do_craft_10", "10") + +form = form .. "tableoptions[background=#DDDDDD30]" +form = form .. "tablecolumns[text,align=left,width=1]" + +default.ui.register_page("crafting:crafting", form) function crafting.get_formspec(name) + local row = 1 + + if crafting.userdata[name] ~= nil then + row = crafting.userdata[name].row + end + + local craft_list = "" + + local craftitems = crafting.get_crafts(nil) + + for _, itemn in ipairs(craftitems) do + if craft_list ~= "" then + craft_list = craft_list .. "," + end + + local itemdef = minetest.registered_items[itemn] + + if itemdef ~= nil then + craft_list = craft_list .. minetest.formspec_escape(itemdef.description) + end + end + local form = default.ui.get_page("crafting:crafting") + form = form .. "table[1.25,0.25;5.75,2.75;craft_list;" .. craft_list .. ";" .. row .. "]" + return form end local function on_joinplayer(player) + local name = player:get_player_name() + local inv = player:get_inventory() + if crafting.userdata[name] == nil then + crafting.userdata[name] = {row = 1} + end + if inv:get_size("craft_in") ~= 4 then inv:set_size("craft_in", 4) end @@ -194,33 +264,50 @@ local function on_joinplayer(player) end local function on_player_receive_fields(player, form_name, fields) - if form_name ~= "crafting:crafting" then + if fields.crafting_tracker == nil then return end + local name = player:get_player_name() + local inv = player:get_inventory() - local wanted_itemstack = ItemStack("default:stone 2") - local output_itemstack = inv:get_stack("craft_out", 1) + if fields.do_craft_1 or fields.do_craft_10 then + local craftitems = crafting.get_crafts(nil) - if output_itemstack:get_name() ~= wanted_itemstack:get_name() and output_itemstack:get_count() ~= 0 then - minetest.log("warning", "Trying to craft " .. wanted_itemstack:to_string() .. " but output has too many/different items") - end + local wanted_itemstack = ItemStack(craftitems[crafting.userdata[name].row]) + local output_itemstack = inv:get_stack("craft_out", 1) - if fields.do_craft_1 then - wanted_itemstack:set_count(1) - elseif fields.do_craft_10 then - wanted_itemstack:set_count(2) - else - return - end + if output_itemstack:get_name() ~= wanted_itemstack:get_name() + and output_itemstack:get_count() ~= 0 then + minetest.log("warning", + "Trying to craft " .. wanted_itemstack:to_string() .. + " but output has too many/different items") + end - local crafted = crafting.craft(wanted_itemstack, output_itemstack, inv:get_list("craft_in")) + if fields.do_craft_1 then + wanted_itemstack:set_count(1) + elseif fields.do_craft_10 then + wanted_itemstack:set_count(10) + else + return + end - if crafted and inv:room_for_item("craft_out", crafted.output) then - inv:add_item("craft_out", crafted.output) + local crafted = crafting.craft(wanted_itemstack, output_itemstack, inv:get_list("craft_in")) - inv:set_list("craft_in", crafted.items) + if crafted and inv:room_for_item("craft_out", crafted.output) then + inv:set_stack("craft_out", 1, crafted.output) + + inv:set_list("craft_in", crafted.items) + end + elseif fields.craft_list then + local selection = minetest.explode_table_event(fields.craft_list) + + if selection.type == "CHG" or selection.type == "DCL" then + crafting.userdata[name].row = selection.row + + minetest.show_formspec(name, "crafting:crafting", crafting.get_formspec(name, crafting.userdata[name].row)) + end end end @@ -228,12 +315,28 @@ minetest.register_on_joinplayer(on_joinplayer) minetest.register_on_player_receive_fields(on_player_receive_fields) crafting.register_craft( - "default:stone 4", + "default:stone", { items = { - {name = "default:stick", count = 3}, - {name = "default:fiber", count = 2}, - {name = "group:stone", count = 2}, + {name = "default:stick", count = 1}, + {name = "default:fiber", count = 1}, + }, +}) + +crafting.register_craft( + "default:apple", + { + items = { + {name = "default:fiber", count = 5}, + }, +}) + +crafting.register_craft( + "default:stick", + { + items = { + {name = "default:stone", count = 2}, + {name = "default:tree", count = 2}, }, }) diff --git a/mods/default/formspec.lua b/mods/default/formspec.lua index e3ddfb5..1f9b1ca 100644 --- a/mods/default/formspec.lua +++ b/mods/default/formspec.lua @@ -237,5 +237,5 @@ end) minetest.register_on_joinplayer( function(player) - player:set_inventory_formspec(default.ui.get_page("crafting:crafting")) + player:set_inventory_formspec(crafting.get_formspec(player:get_player_name())) end) From fdb2e11951478607dd08cf96ed8147d3bc3295bb Mon Sep 17 00:00:00 2001 From: KaadmY Date: Sun, 14 May 2017 14:50:37 -0700 Subject: [PATCH 5/5] New crafting method along with a plethora of bugfixes in general; crafts in the default mod all should be functional --- mods/crafting/api.lua | 365 +++++++++++++++++++++++++ mods/crafting/depends.txt | 1 + mods/crafting/init.lua | 339 +---------------------- mods/crafting/recipes.lua | 529 ++++++++++++++++++++++++++++++++++++ mods/default/crafting.lua | 552 -------------------------------------- mods/default/formspec.lua | 48 +++- mods/default/init.lua | 2 +- mods/gold/init.lua | 2 +- 8 files changed, 939 insertions(+), 899 deletions(-) create mode 100644 mods/crafting/api.lua create mode 100644 mods/crafting/depends.txt create mode 100644 mods/crafting/recipes.lua diff --git a/mods/crafting/api.lua b/mods/crafting/api.lua new file mode 100644 index 0000000..63f46de --- /dev/null +++ b/mods/crafting/api.lua @@ -0,0 +1,365 @@ + +-- API + +crafting = {} + +crafting.registered_crafts = {} + +-- User table of last selected row etc. +crafting.userdata = {} + +-- Crafting can only take 4 itemstacks as input for sanity/interface reasons +crafting.max_inputs = 4 + +-- Default crafting definition values +crafting.default_craftdef = { + output = nil, + items = {}, + groups = {}, +} + +function crafting.register_craft(def) + if def.output == nil then + minetest.log("warning", + "No output for craft recipe") + return + end + + local itemstack = ItemStack(def.output) + local itemkey = itemstack:to_string() + + if crafting.registered_crafts[itemkey] ~= nil then + minetest.log("warning", + "Tried to register an existing craft " .. itemkey .. ", allowing") + end + + local craftdef = { + output = itemstack, + items = def.items or crafting.default_craftdef.items, + groups = def.groups or crafting.default_craftdef.groups, + } + + if #craftdef.items > 4 then + minetest.log("warning", + "Attempting to register craft " .. itemkey .." with more than " + .. crafting.max_inputs .. " inputs, allowing") + end + + for i = 1, crafting.max_inputs do + craftdef.items[i] = ItemStack(craftdef.items[i]) + end + + crafting.registered_crafts[itemkey] = craftdef + + minetest.log("info", "Registered recipe for " .. itemkey) +end + +function crafting.get_crafts(filter) + local results = {} + + local function get_filter() + for craftname, craftdef in pairs(crafting.registered_crafts) do + for filtername, filtervalue in pairs(filter) do + if craftdef.groups[filtername] ~= nil + and filtervalue >= craftdef.groups[filtername] then + table.insert(results, craftname) + break + end + end + end + end + + local function get_all() + for craftname, _ in pairs(crafting.registered_crafts) do + table.insert(results, craftname) + end + end + + if filter == nil then + get_all() + else + get_filter() + end + + local function sort_function(a, b) + local a_itemn = ItemStack(a):get_name() + local b_itemn = ItemStack(b):get_name() + + local a_name = minetest.registered_items[a_itemn].description + local b_name = minetest.registered_items[b_itemn].description + + return a_name < b_name + end + + table.sort(results, sort_function) + + return results +end + +function crafting.craft(wanted, output, items) + -- `output` can be any ItemStack value + -- Duplicate items in `items` should work correctly + + local craftdef = crafting.registered_crafts[wanted:get_name()] + + if craftdef == nil then + minetest.log("warning", + "Tried to craft an unregistered item " .. wanted:get_name()) + + return nil + end + + -- Check for validity + + local craft_count = wanted:get_count() + + for i = 1, crafting.max_inputs do + local required_itemstack = ItemStack(craftdef.items[i]) + local itemc = 0 + + local group = string.match(required_itemstack:get_name(), "group:(.*)") + + if required_itemstack ~= nil and required_itemstack:get_count() ~= 0 then + for j = 1, crafting.max_inputs do + local input_itemstack = ItemStack(items[j]) + + if (group ~= nil + and minetest.get_item_group(input_itemstack:get_name(), group) ~= 0) + or (input_itemstack ~= nil + and input_itemstack:get_name() == required_itemstack:get_name()) then + itemc = itemc + input_itemstack:get_count() + end + end + + craft_count = math.min(craft_count, + math.floor(itemc / required_itemstack:get_count())) + + if craft_count < 1 then + return nil -- Not enough items to craft + end + end + end + + -- Put stuff in output stack + + if output:get_free_space() < (craft_count * craftdef.output:get_count()) then + craft_count = math.floor(output:get_free_space() / craftdef.output:get_count()) + end + + if craft_count < 1 then + return nil -- Can't hold any output + end + + output:add_item(ItemStack({ + name = craftdef.output:get_name(), + count = craftdef.output:get_count() * craft_count})) + + -- Iterate through second time to take items used for crafting + + local function remove_used_item(itemn, count) + local items_required = count + + local group = string.match(itemn, "group:(.*)") + + for i = 1, crafting.max_inputs do + local input_itemstack = ItemStack(items[i]) + + if (group ~= nil + and minetest.get_item_group(input_itemstack:get_name(), group) ~= 0) + or (items[i] ~= nil + and input_itemstack:get_name() == itemn) then + local items_left = items_required - input_itemstack:get_count() + + input_itemstack:take_item(items_required) + + if items_left > 0 then + items_required = items_required - (items_required - items_left) + else + items[i] = input_itemstack:to_table() + break + end + + items[i] = input_itemstack:to_table() + end + end + end + + for i = 1, crafting.max_inputs do + local required_itemstack = ItemStack(craftdef.items[i]) + + if craftdef.items[i] ~= nil then + remove_used_item(required_itemstack:get_name(), + required_itemstack:get_count() * craft_count) + end + end + + return {items = items, output = output} +end + +local form = default.ui.get_page("default:2part") + +form = form .. "field[-1,-1;0,0;crafting_tracker;;]" + +form = form .. "list[current_player;main;0.25,4.75;8,4;]" +form = form .. "listring[current_player;main]" +form = form .. default.ui.get_hotbar_itemslot_bg(0.25, 4.75, 8, 1) +form = form .. default.ui.get_itemslot_bg(0.25, 5.75, 8, 3) + +form = form .. "list[current_player;craft_in;0.25,0.25;1,4;]" +form = form .. "listring[current_player;craft_in]" + +form = form .. "list[current_player;craft_out;7.25,3.25;1,1;]" + +form = form .. default.ui.get_itemslot_bg(0.25, 0.25, 1, 4) +form = form .. default.ui.get_itemslot_bg(7.25, 3.25, 1, 1) + +form = form .. "image[5.25,3.25;1,1;ui_arrow_bg.png^[transformR270]" + +form = form .. default.ui.button(7.25, 0.25, 1, 1, "do_craft_1", "1") +form = form .. default.ui.button(7.25, 1.25, 1, 1, "do_craft_10", "10") + +form = form .. "tableoptions[background=#DDDDDD30]" +form = form .. "tablecolumns[text,align=left,width=2;text,align=left,width=40]" + +default.ui.register_page("crafting:crafting", form) + +function crafting.get_formspec(name) + local row = 1 + + if crafting.userdata[name] ~= nil then + row = crafting.userdata[name].row + end + + local craft_list = "" + + local craftitems = crafting.get_crafts(nil) + + local selected_craftdef = nil + + for i, itemn in ipairs(craftitems) do + local itemstack = ItemStack(itemn) + local itemdef = minetest.registered_items[itemstack:get_name()] + + if i == row then + selected_craftdef = crafting.registered_crafts[itemn] + end + + if itemdef ~= nil then + if craft_list ~= "" then + craft_list = craft_list .. "," + end + + if itemstack:get_count() ~= 1 then + craft_list = craft_list .. minetest.formspec_escape( itemstack:get_count()) + end + + craft_list = craft_list .. "," .. minetest.formspec_escape(itemdef.description) + end + end + + local form = default.ui.get_page("crafting:crafting") + + form = form .. "table[1.25,0.25;5.75,2.75;craft_list;" .. craft_list + .. ";" .. row .. "]" + + if selected_craftdef ~= nil then + if selected_craftdef.items[1] ~= nil then + form = form .. default.ui.fake_itemstack_any( + 4.25, 3.25, selected_craftdef.items[1]) + end + if selected_craftdef.items[2] ~= nil then + form = form .. default.ui.fake_itemstack_any( + 3.25, 3.25, selected_craftdef.items[2]) + end + if selected_craftdef.items[3] ~= nil then + form = form .. default.ui.fake_itemstack_any( + 2.25, 3.25, selected_craftdef.items[3]) + end + if selected_craftdef.items[4] ~= nil then + form = form .. default.ui.fake_itemstack_any( + 1.25, 3.25, selected_craftdef.items[4]) + end + if selected_craftdef.items[4] ~= nil then + form = form .. default.ui.fake_itemstack_any( + 6.25, 3.25, selected_craftdef.output) + end + end + + return form +end + +local function on_joinplayer(player) + local name = player:get_player_name() + + local inv = player:get_inventory() + + if crafting.userdata[name] == nil then + crafting.userdata[name] = {row = 1} + end + + if inv:get_size("craft_in") ~= 4 then + inv:set_size("craft_in", 4) + end + + if inv:get_size("craft_out") ~= 1 then + inv:set_size("craft_out", 1) + end +end + +local function on_player_receive_fields(player, form_name, fields) + if fields.crafting_tracker == nil then + return + end + + local name = player:get_player_name() + + local inv = player:get_inventory() + + if fields.do_craft_1 or fields.do_craft_10 then + local craftitems = crafting.get_crafts(nil) + + local wanted_itemstack = ItemStack(craftitems[crafting.userdata[name].row]) + local output_itemstack = inv:get_stack("craft_out", 1) + + if output_itemstack:get_name() ~= wanted_itemstack:get_name() + and output_itemstack:get_count() ~= 0 then + return -- Different item type in output already + end + + if fields.do_craft_1 then + wanted_itemstack:set_count(1) + elseif fields.do_craft_10 then + wanted_itemstack:set_count(10) + else + return + end + + local crafted = crafting.craft(wanted_itemstack, + output_itemstack, + inv:get_list("craft_in")) + + if crafted then + inv:set_stack("craft_out", 1, "") + + if inv:room_for_item("craft_out", crafted.output) then + inv:set_stack("craft_out", 1, crafted.output) + + inv:set_list("craft_in", crafted.items) + end + end + elseif fields.craft_list then + local selection = minetest.explode_table_event(fields.craft_list) + + if selection.type == "CHG" or selection.type == "DCL" then + crafting.userdata[name].row = selection.row + + minetest.show_formspec(name, "crafting:crafting", + crafting.get_formspec(name, crafting.userdata[name].row)) + end + end +end + +minetest.register_on_joinplayer(on_joinplayer) +minetest.register_on_player_receive_fields(on_player_receive_fields) + +default.log("api", "loaded") diff --git a/mods/crafting/depends.txt b/mods/crafting/depends.txt new file mode 100644 index 0000000..4ad96d5 --- /dev/null +++ b/mods/crafting/depends.txt @@ -0,0 +1 @@ +default diff --git a/mods/crafting/init.lua b/mods/crafting/init.lua index 711a955..e82c731 100644 --- a/mods/crafting/init.lua +++ b/mods/crafting/init.lua @@ -1,343 +1,10 @@ + -- -- Crafting mod -- By Kaadmy, for Pixture -- -crafting = {} - -crafting.registered_crafts = {} - --- User table of last selected row etc. -crafting.userdata = {} - --- Crafting can only take 4 itemstacks as input for sanity/interface reasons -crafting.max_inputs = 4 - --- Default crafting definition values -crafting.default_craftdef = { - output = nil, - items = {}, - groups = {}, -} - -function crafting.register_craft(output, def) - -- `output` can be any ItemStack value, but count is NOT used - - local itemstack = ItemStack(output) - local itemn = itemstack:get_name() - - if crafting.registered_crafts[itemn] ~= nil then - minetest.log("warning", - "Tried to register an existing craft " .. itemn .. ", allowing") - end - - local craftdef = { - output = itemstack, - items = def.items or crafting.default_craftdef.items, - groups = def.groups or crafting.default_craftdef.groups, - } - - if #craftdef.items > 4 then - minetest.log("warning", - "Attempting to register craft " .. itemn .." with more than " - .. crafting.max_inputs .. " inputs, keeping") - end - - for i = 1, crafting.max_inputs do - craftdef.items[i] = ItemStack(craftdef.items[i]) - end - - crafting.registered_crafts[itemn] = craftdef - - minetest.log("info", "Registered recipe for " .. itemn) -end - -function crafting.get_crafts(filter) - local results = {} - - local function get_filter() - for craftname, craftdef in pairs(crafting.registered_crafts) do - for filtername, filtervalue in ipairs(filter) do - if craftdef[filtername] ~= nil and craftdef[filtername] == filtervalue then - table.insert(results, craftname) - break - end - end - end - end - - local function get_all() - for craftname, _ in pairs(crafting.registered_crafts) do - table.insert(results, craftname) - end - end - - if filter == nil then - get_all() - else - get_filter() - end - - return results -end - -function crafting.craft(wanted, output, items) - -- `output` can be any ItemStack value - -- Duplicate items in `items` should work correctly - - local craftdef = crafting.registered_crafts[wanted:get_name()] - - if craftdef == nil then - minetest.log("warning", - "Tried to craft an unregistered item " .. wanted:get_name()) - - return nil - end - - -- Check for validity - - local craft_count = wanted:get_count() - - for i = 1, crafting.max_inputs do - local required_itemstack = ItemStack(craftdef.items[i]) - local itemc = 0 - - local group = string.match(required_itemstack:get_name(), "group:(.*)") - - if required_itemstack ~= nil and required_itemstack:get_count() ~= 0 then - for j = 1, crafting.max_inputs do - local input_itemstack = ItemStack(items[j]) - - if (group ~= nil - and minetest.get_item_group(input_itemstack:get_name(), group) ~= 0) - or (input_itemstack ~= nil - and input_itemstack:get_name() == required_itemstack:get_name()) then - itemc = itemc + input_itemstack:get_count() - end - end - - craft_count = math.min(craft_count, math.floor(itemc / required_itemstack:get_count())) - - if craft_count < 1 then - minetest.log("warning", - "Not enough items to craft " .. wanted:to_string()) - - return nil - end - end - end - - -- Put stuff in output stack - - print("Free space: " .. output:get_free_space()) - - if craft_count - output:get_free_space() > 0 then - craft_count = craft_count - (craft_count - output:get_free_space()) - end - - if craft_count < 1 then - minetest.log("warning", - "Output stack too full to hold " .. wanted:to_string()) - - return nil - end - - output:add_item(ItemStack({ - name = craftdef.output:get_name(), - count = craftdef.output:get_count() * craft_count - })) - - -- Iterate through second time to take items used for crafting - - local function remove_used_item(itemn, count) - local items_required = count - - local group = string.match(itemn, "group:(.*)") - - for i = 1, crafting.max_inputs do - local input_itemstack = ItemStack(items[i]) - - if (group ~= nil - and minetest.get_item_group(input_itemstack:get_name(), group) ~= 0) - or (items[i] ~= nil - and input_itemstack:get_name() == itemn) then - local items_left = items_required - input_itemstack:get_count() - - input_itemstack:take_item(items_required) - - if items_left > 0 then - items_required = items_required - (items_required - items_left) - else - items[i] = input_itemstack:to_table() - break - end - - items[i] = input_itemstack:to_table() - end - end - end - - for i = 1, crafting.max_inputs do - local required_itemstack = ItemStack(craftdef.items[i]) - - if craftdef.items[i] ~= nil then - remove_used_item(required_itemstack:get_name(), required_itemstack:get_count() * craft_count) - end - end - - return {items = items, output = output} -end - -local form = default.ui.get_page("default:2part") - -form = form .. "field[-1,-1;0,0;crafting_tracker;;]" - -form = form .. "list[current_player;main;0.25,4.75;8,4;]" -form = form .. "listring[current_player;main]" -form = form .. default.ui.get_hotbar_itemslot_bg(0.25, 4.75, 8, 1) -form = form .. default.ui.get_itemslot_bg(0.25, 5.75, 8, 3) - -form = form .. "list[current_player;craft_in;0.25,0.25;1,4;]" -form = form .. "listring[current_player;craft_in]" - -form = form .. "list[current_player;craft_out;7.25,0.25;1,1;]" - -form = form .. default.ui.get_itemslot_bg(0.25, 0.25, 1, 4) -form = form .. default.ui.get_itemslot_bg(7.25, 0.25, 1, 1) - -form = form .. "image[7.25,1.25;1,1;ui_arrow_bg.png]" - -form = form .. default.ui.button(7.25, 2.25, 1, 1, "do_craft_1", "1") -form = form .. default.ui.button(7.25, 3.25, 1, 1, "do_craft_10", "10") - -form = form .. "tableoptions[background=#DDDDDD30]" -form = form .. "tablecolumns[text,align=left,width=1]" - -default.ui.register_page("crafting:crafting", form) - -function crafting.get_formspec(name) - local row = 1 - - if crafting.userdata[name] ~= nil then - row = crafting.userdata[name].row - end - - local craft_list = "" - - local craftitems = crafting.get_crafts(nil) - - for _, itemn in ipairs(craftitems) do - if craft_list ~= "" then - craft_list = craft_list .. "," - end - - local itemdef = minetest.registered_items[itemn] - - if itemdef ~= nil then - craft_list = craft_list .. minetest.formspec_escape(itemdef.description) - end - end - - local form = default.ui.get_page("crafting:crafting") - - form = form .. "table[1.25,0.25;5.75,2.75;craft_list;" .. craft_list .. ";" .. row .. "]" - - return form -end - -local function on_joinplayer(player) - local name = player:get_player_name() - - local inv = player:get_inventory() - - if crafting.userdata[name] == nil then - crafting.userdata[name] = {row = 1} - end - - if inv:get_size("craft_in") ~= 4 then - inv:set_size("craft_in", 4) - end - - if inv:get_size("craft_out") ~= 1 then - inv:set_size("craft_out", 1) - end -end - -local function on_player_receive_fields(player, form_name, fields) - if fields.crafting_tracker == nil then - return - end - - local name = player:get_player_name() - - local inv = player:get_inventory() - - if fields.do_craft_1 or fields.do_craft_10 then - local craftitems = crafting.get_crafts(nil) - - local wanted_itemstack = ItemStack(craftitems[crafting.userdata[name].row]) - local output_itemstack = inv:get_stack("craft_out", 1) - - if output_itemstack:get_name() ~= wanted_itemstack:get_name() - and output_itemstack:get_count() ~= 0 then - minetest.log("warning", - "Trying to craft " .. wanted_itemstack:to_string() .. - " but output has too many/different items") - end - - if fields.do_craft_1 then - wanted_itemstack:set_count(1) - elseif fields.do_craft_10 then - wanted_itemstack:set_count(10) - else - return - end - - local crafted = crafting.craft(wanted_itemstack, output_itemstack, inv:get_list("craft_in")) - - if crafted and inv:room_for_item("craft_out", crafted.output) then - inv:set_stack("craft_out", 1, crafted.output) - - inv:set_list("craft_in", crafted.items) - end - elseif fields.craft_list then - local selection = minetest.explode_table_event(fields.craft_list) - - if selection.type == "CHG" or selection.type == "DCL" then - crafting.userdata[name].row = selection.row - - minetest.show_formspec(name, "crafting:crafting", crafting.get_formspec(name, crafting.userdata[name].row)) - end - end -end - -minetest.register_on_joinplayer(on_joinplayer) -minetest.register_on_player_receive_fields(on_player_receive_fields) - -crafting.register_craft( - "default:stone", - { - items = { - {name = "default:stick", count = 1}, - {name = "default:fiber", count = 1}, - }, -}) - -crafting.register_craft( - "default:apple", - { - items = { - {name = "default:fiber", count = 5}, - }, -}) - -crafting.register_craft( - "default:stick", - { - items = { - {name = "default:stone", count = 2}, - {name = "default:tree", count = 2}, - }, -}) +dofile(minetest.get_modpath("crafting").."/api.lua") +dofile(minetest.get_modpath("crafting").."/recipes.lua") default.log("mod:crafting", "loaded") diff --git a/mods/crafting/recipes.lua b/mods/crafting/recipes.lua new file mode 100644 index 0000000..50a039e --- /dev/null +++ b/mods/crafting/recipes.lua @@ -0,0 +1,529 @@ + +-- +-- Recipes +-- + +-- Pickaxes + +crafting.register_craft( + { + output = "default:pick_wood", + items = { + "group:planks 2", + "default:fiber 4", + "default:stick 3", + } +}) + +crafting.register_craft( + { + output = "default:pick_stone", + items = { + "group:stone 2", + "default:fiber 4", + "default:stick 3", + } +}) + +crafting.register_craft( + { + output = "default:pick_steel", + items = { + "default:ingot_steel 2", + "default:fiber 4", + "default:stick 3", + } +}) + +crafting.register_craft( + { + output = "default:pick_carbonsteel", + items = { + "default:ingot_carbonsteel 2", + "default:fiber 4", + "default:stick 3", + } +}) + +-- Shovels + +crafting.register_craft( + { + output = "default:shovel_wood", + items = { + "group:planks 2", + "default:fiber 4", + "default:stick 3", + } +}) + +crafting.register_craft( + { + output = "default:shovel_stone", + items = { + "group:stone 2", + "default:fiber 4", + "default:stick 3", + } +}) + +crafting.register_craft( + { + output = "default:shovel_steel", + items = { + "default:ingot_steel 2", + "default:fiber 4", + "default:stick 3", + } +}) + +crafting.register_craft( + { + output = "default:shovel_carbonsteel", + items = { + "default:ingot_carbonsteel 2", + "default:fiber 4", + "default:stick 3", + } +}) + +-- Axes + +crafting.register_craft( + { + output = "default:axe_wood", + items = { + "group:planks 2", + "default:fiber 4", + "default:stick 3", + } +}) + +crafting.register_craft( + { + output = "default:axe_stone", + items = { + "group:stone 2", + "default:fiber 4", + "default:stick 3", + } +}) + +crafting.register_craft( + { + output = "default:axe_steel", + items = { + "default:ingot_steel 2", + "default:fiber 4", + "default:stick 3", + } +}) + +crafting.register_craft( + { + output = "default:axe_carbonsteel", + items = { + "default:ingot_carbonsteel 2", + "default:fiber 4", + "default:stick 3", + } +}) + +-- Spears + +crafting.register_craft( + { + output = "default:spear_wood", + items = { + "group:planks 2", + "default:fiber 4", + "default:stick 3", + } +}) + +crafting.register_craft( + { + output = "default:spear_stone", + items = { + "group:stone 2", + "default:fiber 4", + "default:stick 3", + } +}) + +crafting.register_craft( + { + output = "default:spear_steel", + items = { + "default:ingot_steel 2", + "default:fiber 4", + "default:stick 3", + } +}) + +crafting.register_craft( + { + output = "default:spear_carbonsteel", + items = { + "default:ingot_carbonsteel 2", + "default:fiber 4", + "default:stick 3", + } +}) + +-- Broadsword + +crafting.register_craft( + { + output = "default:broadsword", + items = { + "default:ingot_steel 4", + "default:fiber 5", + "default:stick 2", + } +}) + +-- Items + +crafting.register_craft( + { + output = "default:dust_carbonsteel", + items = { + "default:lump_coal", + "default:lump_iron 2", + } +}) + +crafting.register_craft( + { + output = "default:rope 2", + items = { + "default:dry_grass 3", + } +}) + +crafting.register_craft( + { + output = "default:fiber 3", + items = { + "default:leaves 4", + } +}) + +crafting.register_craft( + { + output = "default:fiber", + items = { + "default:grass", + } +}) + +crafting.register_craft( + { + output = "default:stick 4", + items = { + "group:planks", + } +}) + +crafting.register_craft( + { + output = "default:flint 2", + items = { + "default:gravel", + } +}) + +crafting.register_craft( + { + output = "default:paper", + items = { + "default:papyrus 3", + } +}) + +crafting.register_craft( + { + output = "default:book", + items = { + "default:paper 3", + "default:stick", + "default:fiber", + } +}) + +crafting.register_craft( + { + output = "default:flint_and_steel", + items = { + "default:ingot_steel", + "default:fiber", + "default:flint", + } +}) + +crafting.register_craft( + { + output = "default:bucket", + items = { + "default:stick 2", + "default:fiber 4", + "group:planks 5", + } +}) + +-- Stone nodes + +crafting.register_craft( + { + output = "default:gravel", + items = { + "default:cobble", + } +}) + +crafting.register_craft( + { + output = "default:brick 2", + items = { + "group:soil 5", + "default:gravel 4", + } +}) + +-- Block nodes + +crafting.register_craft( + { + output = "default:block_steel", + items = { + "default:ingot_steel 9", + } +}) + +crafting.register_craft( + { + output = "default:block_coal", + items = { + "default:lump_coal 9", + } +}) + +-- Path nodes + +crafting.register_craft( + { + output = "default:dirt_path 8", + items = { + "group:soil 3", + "default:gravel 6", + } +}) + +crafting.register_craft( + { + output = "default:path_slab", + items = { + "group:soil_path", + } +}) + +crafting.register_craft( + { + output = "default:heated_dirt_path", + items = { + "group:soil_path", + "default:ingot_steel", + } +}) + +-- Wood nodes + +crafting.register_craft( + { + output = "default:planks 4", + items = { + "default:tree", + } +}) + +crafting.register_craft( + { + output = "default:planks_oak 4", + items = { + "default:tree_oak", + } +}) + +crafting.register_craft( + { + output = "default:planks_birch 4", + items = { + "default:tree_birch", + } +}) + +-- Frame nodes + +crafting.register_craft( + { + output = "default:frame", + items = { + "default:fiber 8", + "default:stick 6", + "group:planks", + } +}) + +crafting.register_craft( + { + output = "default:reinforced_frame", + items = { + "default:fiber 8", + "default:stick 6", + "default:frame", + } +}) + +crafting.register_craft( + { + output = "default:reinforced_cobble", + items = { + "default:fiber 8", + "default:stick 6", + "default:cobble", + } +}) + +-- Fence nodes + +crafting.register_craft( + { + output = "default:fence 4", + items = { + "default:planks", + "default:stick 4", + "default:fiber 4", + } +}) + +crafting.register_craft( + { + output = "default:fence_oak 4", + items = { + "default:planks_oak", + "default:stick 4", + "default:fiber 4", + } +}) + +crafting.register_craft( + { + output = "default:fence_birch 4", + items = { + "default:planks_birch", + "default:stick 4", + "default:fiber 4", + } +}) + +-- Misc nodes + +crafting.register_craft( + { + output = "default:sign 2", + items = { + "group:planks", + "default:fiber 2", + "default:stick 2", + } +}) + +crafting.register_craft( + { + output = "default:torch 2", + items = { + "default:lump_coal", + "default:stick", + "default:fiber", + } +}) + +crafting.register_craft( + { + output = "default:torch_weak 2", + items = { + "default:stick", + "default:fiber", + } +}) + +crafting.register_craft( + { + output = "default:ladder 2", + items = { + "default:stick 5", + "default:fiber 2", + } +}) + +-- Tool nodes (chests, furnaces, bookshelves) + +crafting.register_craft( + { + output = "default:chest", + items = { + "default:stick 12", + "default:fiber 8", + "group:planks 6", + } +}) + +crafting.register_craft( + { + output = "default:furnace", + items = { + "default:torch", + "group:stone 6", + } +}) + +crafting.register_craft( + { + output = "default:bookshelf", + items = { + "default:book 3", + "group:planks 6", + } +}) + +-- Sand nodes + +crafting.register_craft( + { + output = "default:sandstone", + items = { + "default:sand 2", + } +}) + +crafting.register_craft( + { + output = "default:compressed_sandstone", + items = { + "default:sandstone 2", + } +}) + +-- Agriculture nodes + +crafting.register_craft( + { + output = "default:fertilizer", + items = { + "default:fern 3", + "default:fiber", + } +}) + +default.log("recipes", "loaded") diff --git a/mods/default/crafting.lua b/mods/default/crafting.lua index 5fb6b50..41ed5f2 100644 --- a/mods/default/crafting.lua +++ b/mods/default/crafting.lua @@ -1,555 +1,3 @@ --- --- Crafting --- - --- Pickaxes -minetest.register_craft( - { - output = "default:pick_wood", - recipe = { - {"", "group:planks", "default:fiber"}, - {"", "default:stick", "group:planks"}, - {"default:stick", "", ""}, - } - }) - -minetest.register_craft( - { - output = "default:pick_stone", - recipe = { - {"", "group:stone", "default:fiber"}, - {"", "default:stick", "group:stone"}, - {"default:stick", "", ""}, - } - }) - -minetest.register_craft( - { - output = "default:pick_steel", - recipe = { - {"", "default:ingot_steel", "default:fiber"}, - {"", "default:stick", "default:ingot_steel"}, - {"default:stick", "", ""}, - } - }) - -minetest.register_craft( - { - output = "default:pick_carbonsteel", - recipe = { - {"", "default:ingot_carbonsteel", "default:fiber"}, - {"", "default:stick", "default:ingot_carbonsteel"}, - {"default:stick", "", ""}, - } - }) - --- Shovels - -minetest.register_craft( - { - output = "default:shovel_wood", - recipe = { - {"", "group:planks", "group:planks"}, - {"", "default:fiber", "group:planks"}, - {"default:stick", "", ""}, - } - }) - -minetest.register_craft( - { - - - output = "default:shovel_stone", - recipe = { - {"", "group:stone", "group:stone"}, - {"", "default:fiber", "group:stone"}, - {"default:stick", "", ""}, - } - }) - -minetest.register_craft( - { - output = "default:shovel_steel", - recipe = { - {"", "default:ingot_steel", "default:ingot_steel"}, - {"", "default:fiber", "default:ingot_steel"}, - {"default:stick", "", ""}, - } - }) - -minetest.register_craft( - { - output = "default:shovel_carbonsteel", - recipe = { - {"", "default:ingot_carbonsteel", "default:ingot_carbonsteel"}, - {"", "default:fiber", "default:ingot_carbonsteel"}, - {"default:stick", "", ""}, - } - }) - --- Axes - -minetest.register_craft( - { - output = "default:axe_wood", - recipe = { - {"group:planks", "default:fiber"}, - {"group:planks", "default:stick"}, - {"", "default:stick"}, - } - }) - -minetest.register_craft( - { - output = "default:axe_stone", - recipe = { - {"group:stone", "default:fiber"}, - {"group:stone", "default:stick"}, - {"", "default:stick"}, - } - }) - -minetest.register_craft( - { - output = "default:axe_steel", - recipe = { - {"default:ingot_steel", "default:fiber"}, - {"default:ingot_steel", "default:stick"}, - {"", "default:stick"}, - } - }) - -minetest.register_craft( - { - output = "default:axe_carbonsteel", - recipe = { - {"default:ingot_carbonsteel", "default:fiber"}, - {"default:ingot_carbonsteel", "default:stick"}, - {"", "default:stick"}, - } - }) - --- Spears - -minetest.register_craft( - { - output = "default:spear_wood", - recipe = { - {"", "", "group:planks"}, - {"", "default:fiber", ""}, - {"default:stick", "", ""}, - } - }) - -minetest.register_craft( - { - output = "default:spear_stone", - recipe = { - {"", "", "group:stone"}, - {"", "default:fiber", ""}, - {"default:stick", "", ""}, - } - }) - -minetest.register_craft( - { - output = "default:spear_steel", - recipe = { - {"", "", "default:ingot_steel"}, - {"", "default:fiber", ""}, - {"default:stick", "", ""}, - } - }) - -minetest.register_craft( - { - output = "default:spear_carbonsteel", - recipe = { - {"", "", "default:ingot_carbonsteel"}, - {"", "default:fiber", ""}, - {"default:stick", "", ""}, - } - }) - -minetest.register_craft( - { - output = "default:shears", - recipe = { - {"default:ingot_steel", ""}, - {"group:stick", "default:ingot_steel"}, - } - }) - --- Broadsword - -minetest.register_craft( - { - output = "default:broadsword", - recipe = { - {"", "", "default:ingot_steel"}, - {"default:fiber", "default:ingot_steel", ""}, - {"default:stick", "default:fiber", ""}, - } - }) - - --- Nodes/Items - -minetest.register_craft( - { - output = "default:dust_carbonsteel", - type = "shapeless", - recipe = {"default:lump_coal", "default:lump_iron", "default:lump_iron"} - }) - -minetest.register_craft( - { - output = "default:rope", - recipe = { - {"default:dry_grass"}, - {"default:dry_grass"}, - {"default:dry_grass"}, - } - }) - -minetest.register_craft( - { - output = "default:fiber", - type = "shapeless", - recipe = { - "default:grass" - } - }) - -minetest.register_craft( - { - output = "default:fiber 3", - type = "shapeless", - recipe = { - "group:leaves", "group:leaves", "group:leaves", "group:leaves" - } - }) - -minetest.register_craft( - { - output = "default:gravel", - recipe = { - {"default:cobble"}, - } - }) - -minetest.register_craft( - { - output = "default:bucket", - recipe = { - {"default:fiber", "default:stick", "default:fiber"}, - {"group:planks", "", "group:planks"}, - {"group:planks", "group:planks", "group:planks"}, - } - }) - -minetest.register_craft( - { - output = "default:brick", - recipe = { - {"group:soil", "default:gravel", "group:soil"}, - {"default:gravel", "group:soil", "default:gravel"}, - {"group:soil", "default:gravel", "group:soil"}, - } - }) -minetest.register_craft( - { - output = "default:block_steel", - recipe = { - {"default:ingot_steel", "default:ingot_steel", "default:ingot_steel"}, - {"default:ingot_steel", "default:ingot_steel", "default:ingot_steel"}, - {"default:ingot_steel", "default:ingot_steel", "default:ingot_steel"}, - } - }) - -minetest.register_craft( - { - output = "default:block_coal", - recipe = { - {"default:lump_coal", "default:lump_coal", "default:lump_coal"}, - {"default:lump_coal", "default:lump_coal", "default:lump_coal"}, - {"default:lump_coal", "default:lump_coal", "default:lump_coal"}, - } - }) - -minetest.register_craft( - { - output = "default:dirt_path 8", - recipe = { - {"group:soil", "group:soil", "group:soil"}, - {"default:gravel", "default:gravel", "default:gravel"}, - {"default:gravel", "default:gravel", "default:gravel"}, - } - }) - -minetest.register_craft( - { - output = "default:path_slab 3", - recipe = { - {"group:soil_path", "group:soil_path", "group:soil_path"}, - } - }) - -minetest.register_craft( - { - output = "default:heated_dirt_path", - recipe = { - {"group:soil_path"}, - {"default:ingot_steel"}, - } - }) - -minetest.register_craft( - { - output = "default:planks 4", - recipe = { - {"default:tree"}, - } - }) - -minetest.register_craft( - { - output = "default:planks_oak 4", - recipe = { - {"default:tree_oak"}, - } - }) - -minetest.register_craft( - { - output = "default:planks_birch 4", - recipe = { - {"default:tree_birch"}, - } - }) - -minetest.register_craft( - { - output = "default:frame", - recipe = { - {"default:fiber", "default:stick", "default:fiber"}, - {"default:stick", "group:planks", "default:stick"}, - {"default:fiber", "default:stick", "default:fiber"}, - } - }) - -minetest.register_craft( - { - output = "default:reinforced_frame", - recipe = { - {"default:fiber", "default:stick", "default:fiber"}, - {"default:stick", "default:frame", "default:stick"}, - {"default:fiber", "default:stick", "default:fiber"}, - } - }) - -minetest.register_craft( - { - output = "default:stick 4", - recipe = { - {"group:planks"}, - } - }) - -minetest.register_craft( - { - output = "default:fence 4", - recipe = { - {"default:stick", "", "default:stick"}, - {"default:fiber", "default:planks", "default:fiber"}, - {"default:stick", "", "default:stick"}, - } - }) - -minetest.register_craft( - { - output = "default:fence_oak 4", - recipe = { - {"default:stick", "", "default:stick"}, - {"default:fiber", "default:planks_oak", "default:fiber"}, - {"default:stick", "", "default:stick"}, - } - }) - -minetest.register_craft( - { - output = "default:fence_birch 4", - recipe = { - {"default:stick", "", "default:stick"}, - {"default:fiber", "default:planks_birch", "default:fiber"}, - {"default:stick", "", "default:stick"}, - } - }) - -minetest.register_craft( - { - output = "default:sign 2", - recipe = { - {"group:planks", "default:fiber", "group:planks"}, - {"group:planks", "group:planks", "group:planks"}, - {"", "default:stick", ""}, - } - }) - -minetest.register_craft( - { - output = "default:reinforced_cobble", - recipe = { - {"default:fiber", "default:stick", "default:fiber"}, - {"default:stick", "default:cobble", "default:stick"}, - {"default:fiber", "default:stick", "default:fiber"}, - } - }) - -minetest.register_craft( - { - output = "default:torch 2", - recipe = { - {"default:lump_coal"}, - {"default:fiber"}, - {"default:stick"}, - } - }) - -minetest.register_craft( - { - output = "default:torch_weak 2", - recipe = { - {"default:fiber"}, - {"default:stick"}, - } - }) - -minetest.register_craft( - { - output = "default:flint 2", - type = "shapeless", - recipe = {"default:gravel"}, - }) - -minetest.register_craft( - { - output = "default:flint_and_steel", - recipe = { - {"default:ingot_steel", ""}, - {"default:fiber", "default:flint"}, - }, - }) - -minetest.register_craft( - { - output = "default:chest", - recipe = { - {"group:planks", "group:planks", "group:planks"}, - {"group:planks", "default:fiber", "group:planks"}, - {"group:planks", "group:planks", "group:planks"}, - } - }) - -minetest.register_craft( - { - output = "default:chest_locked", - recipe = { - {"default:fiber", "default:stick", "default:fiber"}, - {"default:stick", "default:chest", "default:stick"}, - {"default:fiber", "default:ingot_steel", "default:fiber"}, - } - }) - -minetest.register_craft( - { - output = "default:furnace", - recipe = { - {"group:stone", "group:stone", "group:stone"}, - {"group:stone", "", "group:stone"}, - {"group:stone", "group:stone", "group:stone"}, - } - }) - -minetest.register_craft( - { - output = "default:sandstone 2", - recipe = { - {"default:sand", "default:sand"}, - {"default:sand", "default:sand"}, - } - }) - -minetest.register_craft( - { - output = "default:sandstone 2", - type = "shapeless", - recipe = {"default:compressed_sandstone"}, - }) - -minetest.register_craft( - { - output = "default:compressed_sandstone", - type = "shapeless", - recipe = {"default:sandstone", "default:sandstone"}, - }) - -minetest.register_craft( - { - output = "default:paper", - recipe = { - {"default:papyrus", "default:papyrus", "default:papyrus"}, - } - }) - -minetest.register_craft( - { - output = "default:book", - recipe = { - {"default:fiber", "default:stick", "default:paper"}, - {"default:fiber", "default:stick", "default:paper"}, - {"default:fiber", "default:stick", "default:paper"}, - } - }) - -minetest.register_craft( - { - output = "default:bookshelf", - recipe = { - {"group:planks", "group:planks", "group:planks"}, - {"default:book", "default:book", "default:book"}, - {"group:planks", "group:planks", "group:planks"}, - } - }) - -minetest.register_craft( - { - output = "default:ladder 2", - recipe = { - {"default:stick", "", "default:stick"}, - {"default:fiber", "default:stick", "default:fiber"}, - {"default:stick", "", "default:stick"}, - } - }) - -minetest.register_craft( - { - output = "default:fertilizer 3", - recipe = { - {"", "default:fiber", ""}, - {"default:fern", "default:fern", "default:fern"}, - {"default:fern", "default:fern", "default:fern"}, - }, - }) - --- --- Tool repair --- -minetest.register_craft( - { - type = "toolrepair", - additional_wear = -0.1, - }) -- -- Cooking diff --git a/mods/default/formspec.lua b/mods/default/formspec.lua index 1f9b1ca..bd4026b 100644 --- a/mods/default/formspec.lua +++ b/mods/default/formspec.lua @@ -1,10 +1,23 @@ + default.ui = {} +-- UI defaults + default.ui.default = {} +-- Colors + default.ui.default.colors = "listcolors[#00000000;#00000010;#00000000;#68B259;#FFF]" default.ui.default.bg = "bgcolor[#00000000;false]" +-- Group default items + +default.ui.group_defaults = { + planks = "default:planks", + stone = "default:stone", + soil = "default:dirt", +} + function default.ui.get_itemslot_bg(x, y, w, h) local out = "" for i = 0, w - 1, 1 do @@ -96,8 +109,8 @@ function default.ui.fake_itemstack(x, y, itemstack, name) if itemname ~= "" then -- result = result .. "image_button["..x..","..y..";1,1;"..itemimage..";"..name..";;false;false;"..itemimage.."]" result = result .. "image_button["..x..","..y..";1,1;ui_null.png;"..name..";;false;false;ui_null.png]" - result = result .. "item_image["..x..","..y..";1,1;"..minetest.formspec_escape(itemname).."]" - result = result .. "label["..(x+0.6)..","..(y+0.5)..";"..itemamt.."]" + result = result .. "item_image["..x..","..y..";1,1;"..minetest.formspec_escape(itemname .. " " .. itemamt).."]" + result = result .. "tooltip["..name..";"..itemdesc.."]" end @@ -123,14 +136,21 @@ function default.ui.fake_simple_itemstack(x, y, itemname, name) return result end -function default.ui.item_group(x, y, group, name) +function default.ui.item_group(x, y, group, count, name) local name = name or "fake_itemgroup" local itemname = "" - for itemn, itemdef in pairs(minetest.registered_items) do - if minetest.get_item_group(itemn, group) ~= 0 and minetest.get_item_group(itemn, "not_in_craftingguide") ~= 1 then - itemname = itemn + local group_default = default.ui.group_defaults[group] + + if group_default ~= nil and minetest.registered_items[group_default] then + itemname = group_default + else + for itemn, itemdef in pairs(minetest.registered_items) do + if minetest.get_item_group(itemn, group) ~= 0 + and minetest.get_item_group(itemn, "not_in_craftingguide") ~= 1 then + itemname = itemn + end end end @@ -141,15 +161,25 @@ function default.ui.item_group(x, y, group, name) local result = "" if itemname ~= "" then - -- result = result .. "image_button["..x..","..y..";1,1;"..itemimage..";"..name..";;false;false;"..itemimage.."]" - result = result .. "image_button["..x..","..y..";1,1;ui_null.png;"..name..";G;false;false;ui_null.png]" - result = result .. "item_image["..x..","..y..";1,1;"..minetest.formspec_escape(itemname).."]" + result = result .. "image_button["..x..","..y..";1,1;ui_null.png;"..name..";;false;false;ui_null.png]" + result = result .. "item_image["..x..","..y..";1,1;"..minetest.formspec_escape(itemname .. " " .. count).."]" + result = result .. "tooltip["..name..";Group: "..group.."]" end return result end +function default.ui.fake_itemstack_any(x, y, itemstack, name) + local group = string.match(itemstack:get_name(), "group:(.*)") + + if group == nil then + return default.ui.fake_itemstack(x, y, itemstack, name) + else + return default.ui.item_group(x, y, group, itemstack:get_count(), name) + end +end + default.ui.registered_pages = { } diff --git a/mods/default/init.lua b/mods/default/init.lua index 54b9b35..3b92863 100644 --- a/mods/default/init.lua +++ b/mods/default/init.lua @@ -44,4 +44,4 @@ dofile(minetest.get_modpath("default").."/hud.lua") dofile(minetest.get_modpath("default").."/player.lua") dofile(minetest.get_modpath("default").."/model.lua") -default.log("mod:default", "loaded") \ No newline at end of file +default.log("mod:default", "loaded") diff --git a/mods/gold/init.lua b/mods/gold/init.lua index 657f198..f8ff1bc 100644 --- a/mods/gold/init.lua +++ b/mods/gold/init.lua @@ -192,7 +192,7 @@ function gold.trade(trade, trade_type, player) form = form .. default.ui.fake_itemstack(1.25, 1.25, trade_wanted1, "trade_wanted1") form = form .. default.ui.fake_itemstack(1.25, 2.25, trade_wanted2, "trade_wanted2") - form = form .. default.ui.fake_itemstack(3.75, 1.25, ItemStack(trade[3]), "vistrade_result") + form = form .. default.ui.fake_itemstack(3.75, 1.25, ItemStack(trade[3]), "trade_result") minetest.show_formspec(name, "gold:trading_book", form)