2017-02-08 21:55:29 +01:00
|
|
|
local cache = smart_inventory.cache
|
2017-02-16 23:23:45 +01:00
|
|
|
local filter = smart_inventory.filter
|
2017-02-21 23:17:53 +01:00
|
|
|
local ui_tools = smart_inventory.ui_tools
|
2017-02-05 13:51:48 +01:00
|
|
|
|
2017-02-21 23:17:53 +01:00
|
|
|
-----------------------------------------------------
|
|
|
|
-- Update item informations about the selected item
|
|
|
|
-----------------------------------------------------
|
2017-02-08 21:55:29 +01:00
|
|
|
local function on_item_select(state, itemdef, recipe)
|
2017-02-11 00:00:48 +01:00
|
|
|
local inf_state = state:get("inf_area"):getContainerState()
|
2017-02-08 21:55:29 +01:00
|
|
|
if itemdef then
|
2017-02-11 00:00:48 +01:00
|
|
|
inf_state:get("info1"):setText(itemdef.description)
|
|
|
|
inf_state:get("info2"):setText("("..itemdef.name..")")
|
2017-02-11 23:30:24 +01:00
|
|
|
if itemdef._doc_items_longdesc then
|
|
|
|
inf_state:get("info3"):setText(itemdef._doc_items_longdesc)
|
|
|
|
else
|
|
|
|
inf_state:get("info3"):setText("")
|
|
|
|
end
|
2017-02-10 22:30:31 +01:00
|
|
|
if recipe.type ~="normal" then
|
2017-02-11 00:00:48 +01:00
|
|
|
inf_state:get("cr_type"):setText(recipe.type)
|
2017-02-10 22:30:31 +01:00
|
|
|
else
|
2017-02-11 00:00:48 +01:00
|
|
|
inf_state:get("cr_type"):setText("")
|
2017-02-10 22:30:31 +01:00
|
|
|
end
|
2017-02-11 23:30:24 +01:00
|
|
|
inf_state:get("craft_result"):setImage(recipe.output)
|
2017-02-14 23:26:48 +01:00
|
|
|
inf_state:get("craft_result"):setVisible()
|
2017-02-11 23:30:24 +01:00
|
|
|
state:get("craft_preview"):setCraft(recipe)
|
2017-02-08 21:55:29 +01:00
|
|
|
else
|
2017-02-11 00:00:48 +01:00
|
|
|
inf_state:get("info1"):setText("")
|
|
|
|
inf_state:get("info2"):setText("")
|
|
|
|
inf_state:get("info3"):setText("")
|
|
|
|
inf_state:get("cr_type"):setText("")
|
2017-02-14 23:26:48 +01:00
|
|
|
inf_state:get("craft_result"):setVisible(false)
|
2017-02-11 23:30:24 +01:00
|
|
|
state:get("craft_preview"):setCraft(nil)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-02-21 23:17:53 +01:00
|
|
|
-----------------------------------------------------
|
|
|
|
-- Update the group selection table
|
|
|
|
-----------------------------------------------------
|
|
|
|
local function update_group_selection(state, rebuild)
|
2017-02-12 22:48:17 +01:00
|
|
|
local grouped = state.param.crafting_grouped_items
|
2017-02-11 23:30:24 +01:00
|
|
|
local groups_sel = state:get("groups_sel")
|
2017-02-12 21:05:26 +01:00
|
|
|
local grid = state:get("buttons_grid")
|
|
|
|
local btn = state:get("groups_btn")
|
2017-02-21 23:17:53 +01:00
|
|
|
-- prepare
|
|
|
|
if rebuild then
|
|
|
|
state.param.crafting_group_list = ui_tools.update_group_selection(grouped, groups_sel, state.param.crafting_group_list)
|
|
|
|
end
|
|
|
|
|
|
|
|
-- apply
|
|
|
|
local sel_id = groups_sel:getSelected()
|
2017-02-12 22:48:17 +01:00
|
|
|
if state.param.crafting_group_list[sel_id] then
|
|
|
|
state.param.crafting_craftable_list = grouped[state.param.crafting_group_list[sel_id]].items
|
2017-02-12 21:05:26 +01:00
|
|
|
-- sort selected items
|
2017-02-12 22:48:17 +01:00
|
|
|
table.sort(state.param.crafting_craftable_list, function(a,b)
|
2017-02-12 21:05:26 +01:00
|
|
|
return a.item < b.item
|
|
|
|
end)
|
2017-02-12 22:48:17 +01:00
|
|
|
grid:setList(state.param.crafting_craftable_list)
|
2017-02-12 21:05:26 +01:00
|
|
|
btn:setText(groups_sel:getSelectedItem())
|
|
|
|
else
|
|
|
|
btn:setText("Empty List")
|
|
|
|
grid:setList({})
|
|
|
|
end
|
2017-02-05 13:51:48 +01:00
|
|
|
end
|
|
|
|
|
2017-02-21 23:17:53 +01:00
|
|
|
-----------------------------------------------------
|
|
|
|
-- Update the items list
|
|
|
|
-----------------------------------------------------
|
2017-02-15 22:01:30 +01:00
|
|
|
local function update_craftable_list(state, recipelist)
|
2017-02-08 21:55:29 +01:00
|
|
|
local duplicate_index_tmp = {}
|
2017-02-11 23:30:24 +01:00
|
|
|
local craftable_itemlist = {}
|
|
|
|
|
2017-02-15 22:01:30 +01:00
|
|
|
for recipe, _ in pairs(recipelist) do
|
2017-02-08 21:55:29 +01:00
|
|
|
local def = minetest.registered_items[recipe.output]
|
|
|
|
if not def then
|
|
|
|
recipe.output:gsub("[^%s]+", function(z)
|
|
|
|
if minetest.registered_items[z] then
|
|
|
|
def = minetest.registered_items[z]
|
2017-02-05 13:51:48 +01:00
|
|
|
end
|
2017-02-08 21:55:29 +01:00
|
|
|
end)
|
2017-02-05 13:51:48 +01:00
|
|
|
end
|
2017-02-08 21:55:29 +01:00
|
|
|
if def then
|
2017-02-11 23:30:24 +01:00
|
|
|
if duplicate_index_tmp[def.name] then
|
|
|
|
table.insert(duplicate_index_tmp[def.name].recipes, recipe)
|
2017-02-08 21:55:29 +01:00
|
|
|
else
|
|
|
|
local entry = {
|
2017-02-11 23:30:24 +01:00
|
|
|
itemdef = def,
|
2017-02-08 21:55:29 +01:00
|
|
|
recipes = {},
|
|
|
|
-- buttons_grid related
|
|
|
|
item = def.name,
|
|
|
|
is_button = true
|
|
|
|
}
|
2017-02-12 02:48:17 +01:00
|
|
|
duplicate_index_tmp[def.name] = entry
|
2017-02-11 23:30:24 +01:00
|
|
|
table.insert(entry.recipes, recipe)
|
|
|
|
table.insert(craftable_itemlist, entry)
|
2017-02-05 13:51:48 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-02-12 22:48:17 +01:00
|
|
|
state.param.crafting_grouped_items = cache.get_list_grouped(craftable_itemlist)
|
2017-02-21 23:17:53 +01:00
|
|
|
update_group_selection(state, true)
|
2017-02-05 13:51:48 +01:00
|
|
|
end
|
|
|
|
|
2017-02-21 23:17:53 +01:00
|
|
|
-----------------------------------------------------
|
|
|
|
-- Lookup inventory
|
|
|
|
-----------------------------------------------------
|
2017-02-15 22:01:30 +01:00
|
|
|
local function create_lookup_inv(state, name)
|
|
|
|
local player = minetest.get_player_by_name(name)
|
|
|
|
local invname = name.."_crafting_inv"
|
|
|
|
local plistname = "crafting_inv_lookup"
|
|
|
|
local listname = "lookup"
|
|
|
|
local pinv = player:get_inventory()
|
|
|
|
local inv = minetest.get_inventory({type="detached", name=invname})
|
|
|
|
if not inv then
|
|
|
|
inv = minetest.create_detached_inventory(invname, {
|
|
|
|
allow_move = function(inv, from_list, from_index, to_list, to_index, count, player)
|
|
|
|
return 0
|
|
|
|
end,
|
|
|
|
allow_put = function(inv, listname, index, stack, player)
|
|
|
|
if pinv:is_empty(plistname) then
|
2017-02-17 16:37:50 +01:00
|
|
|
return 99
|
2017-02-15 22:01:30 +01:00
|
|
|
else
|
|
|
|
return 0
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
allow_take = function(inv, listname, index, stack, player)
|
2017-02-17 16:37:50 +01:00
|
|
|
return 99
|
2017-02-15 22:01:30 +01:00
|
|
|
end,
|
|
|
|
on_move = function(inv, from_list, from_index, to_list, to_index, count, player)
|
|
|
|
end,
|
|
|
|
on_put = function(inv, listname, index, stack, player)
|
|
|
|
pinv:set_stack(plistname, index, stack)
|
2017-02-16 23:23:45 +01:00
|
|
|
-- get the recipes with the item. Filter for visible in docs
|
2017-02-17 15:39:29 +01:00
|
|
|
local recipes
|
|
|
|
local recipes, ciii = cache.get_recipes_craftable_atnext(name, stack:get_name())
|
|
|
|
state.param.crafting_items_in_inventory = ciii
|
2017-02-16 23:23:45 +01:00
|
|
|
update_craftable_list(state, recipes)
|
2017-02-15 22:01:30 +01:00
|
|
|
smartfs.inv[name]:show() -- we are outsite of usual smartfs processing. So trigger the formspec update byself
|
|
|
|
-- put back
|
|
|
|
minetest.after(1, function(stack)
|
|
|
|
local applied = pinv:add_item("main", stack)
|
|
|
|
pinv:set_stack(plistname, 1, applied)
|
|
|
|
inv:set_stack(listname, 1, applied)
|
|
|
|
end, stack)
|
|
|
|
end,
|
|
|
|
on_take = function(inv, listname, index, stack, player)
|
|
|
|
pinv:set_stack(plistname, index, nil)
|
|
|
|
end,
|
|
|
|
}, name)
|
|
|
|
end
|
|
|
|
-- copy the item from player:listname inventory to the detached
|
|
|
|
inv:set_size(listname, 1)
|
|
|
|
pinv:set_size(plistname, 1)
|
|
|
|
local stack = pinv:get_stack(plistname, 1)
|
|
|
|
inv:set_stack(listname, 1, stack)
|
|
|
|
end
|
|
|
|
|
2017-02-21 23:17:53 +01:00
|
|
|
-----------------------------------------------------
|
|
|
|
-- Page layout definition
|
|
|
|
-----------------------------------------------------
|
2017-02-05 13:51:48 +01:00
|
|
|
local function crafting_callback(state)
|
2017-02-08 21:55:29 +01:00
|
|
|
local player = state.location.rootState.location.player
|
2017-02-15 22:01:30 +01:00
|
|
|
|
2017-02-05 13:51:48 +01:00
|
|
|
--Inventorys / left site
|
2017-02-11 23:30:24 +01:00
|
|
|
state:inventory(1, 5, 8, 4,"main")
|
|
|
|
state:inventory(1.2, 0.2, 3, 3,"craft")
|
|
|
|
state:inventory(4.2, 2.2, 1, 1,"craftpreview")
|
|
|
|
state:background(1, 0, 4.5, 3.5, "img1", "menu_bg.png")
|
2017-02-08 23:57:06 +01:00
|
|
|
|
2017-02-17 16:37:50 +01:00
|
|
|
state:button(1, 4.2, 2.5, 0.5, "compress", "Compress"):onClick(function(self, state, player)
|
2017-02-15 14:00:52 +01:00
|
|
|
local name = state.location.rootState.location.player
|
|
|
|
local inventory = minetest.get_player_by_name(name):get_inventory()
|
|
|
|
local invsize = inventory:get_size("main")
|
|
|
|
for idx1 = invsize, 1, -1 do
|
|
|
|
local stack1 = inventory:get_stack("main", idx1)
|
|
|
|
if not stack1:is_empty() then
|
|
|
|
for idx2 = 1, idx1 do
|
|
|
|
local stack2 = inventory:get_stack("main", idx2)
|
|
|
|
if idx1 ~= idx2 and stack1:get_name() == stack2:get_name() then
|
|
|
|
stack1 = stack2:add_item(stack1)
|
|
|
|
inventory:set_stack("main", idx1, stack1)
|
|
|
|
inventory:set_stack("main", idx2, stack2)
|
|
|
|
if stack1:is_empty() then
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
|
2017-02-15 22:01:30 +01:00
|
|
|
create_lookup_inv(state, player)
|
2017-02-17 16:37:50 +01:00
|
|
|
state:inventory(10, 4.0, 1, 1,"lookup"):useDetached(player.."_crafting_inv")
|
2017-02-15 22:01:30 +01:00
|
|
|
|
2017-02-11 00:00:48 +01:00
|
|
|
-- functional buttons right site
|
2017-02-17 16:37:50 +01:00
|
|
|
local refresh_button = state:button(11, 4.2, 2, 0.5, "refresh", "Read inventory ")
|
2017-02-05 13:51:48 +01:00
|
|
|
refresh_button:onClick(function(self, state, player)
|
2017-02-17 15:39:29 +01:00
|
|
|
local craftable, ciii = cache.get_recipes_craftable(player)
|
|
|
|
state.param.crafting_items_in_inventory = ciii
|
|
|
|
update_craftable_list(state, craftable)
|
2017-02-17 16:37:50 +01:00
|
|
|
state:get("inf_area"):setVisible(false)
|
|
|
|
state:get("groups_sel"):setVisible(true)
|
2017-02-05 13:51:48 +01:00
|
|
|
end)
|
|
|
|
|
2017-02-17 16:37:50 +01:00
|
|
|
local groups_button = state:button(13, 4.2, 5, 0.5, "groups_btn", "All items")
|
2017-02-11 00:00:48 +01:00
|
|
|
groups_button:onClick(function(self, state, player)
|
2017-02-14 23:26:48 +01:00
|
|
|
if state:get("groups_sel"):getVisible() == true then
|
|
|
|
state:get("inf_area"):setVisible(true)
|
|
|
|
state:get("groups_sel"):setVisible(false)
|
2017-02-11 00:00:48 +01:00
|
|
|
else
|
2017-02-14 23:26:48 +01:00
|
|
|
state:get("inf_area"):setVisible(false)
|
|
|
|
state:get("groups_sel"):setVisible(true)
|
2017-02-11 00:00:48 +01:00
|
|
|
end
|
|
|
|
end)
|
|
|
|
|
|
|
|
-- preview area / multifunctional
|
2017-02-11 23:30:24 +01:00
|
|
|
state:background(10.0, 0.1, 8, 3.8, "craft_img2", "minimap_overlay_square.png")
|
|
|
|
local inf_area = state:container(6.4, 0.1, "inf_area", true)
|
2017-02-11 00:00:48 +01:00
|
|
|
local inf_state = inf_area:getContainerState()
|
2017-02-11 23:30:24 +01:00
|
|
|
inf_state:label(11.5,0.5,"info1", "")
|
|
|
|
inf_state:label(11.5,1.0,"info2", "")
|
|
|
|
inf_state:label(11.5,1.5,"info3", "")
|
|
|
|
smart_inventory.smartfs_elements.craft_preview(state, 6, 0, "craft_preview")
|
|
|
|
inf_state:label(6.7,3,"cr_type", "")
|
2017-02-14 23:26:48 +01:00
|
|
|
inf_state:item_image(10.2,0.3, 1, 1, "craft_result",nil):setVisible(false)
|
|
|
|
inf_area:setVisible(false)
|
2017-02-11 23:30:24 +01:00
|
|
|
|
|
|
|
local group_sel = state:listbox(10.2, 0.15, 7.6, 3.6, "groups_sel",nil, true)
|
2017-02-12 21:05:26 +01:00
|
|
|
group_sel:onClick(function(self, state, player)
|
|
|
|
local selected = self:getSelectedItem()
|
|
|
|
if selected then
|
2017-02-21 23:17:53 +01:00
|
|
|
update_group_selection(state, false)
|
2017-02-12 02:48:17 +01:00
|
|
|
end
|
2017-02-11 00:00:48 +01:00
|
|
|
end)
|
|
|
|
|
|
|
|
-- craftable items grid
|
2017-02-17 16:37:50 +01:00
|
|
|
state:background(10, 5, 8, 4, "buttons_grid_Bg", "minimap_overlay_square.png")
|
|
|
|
local grid = smart_inventory.smartfs_elements.buttons_grid(state, 10.25, 5.15, 8 , 4, "buttons_grid", 0.75,0.75)
|
2017-02-11 00:00:48 +01:00
|
|
|
grid:onClick(function(self, state, index, player)
|
2017-02-12 22:48:17 +01:00
|
|
|
local listentry = state.param.crafting_craftable_list[index]
|
2017-02-17 15:39:29 +01:00
|
|
|
local recipe = table.copy(listentry.recipes[1])
|
|
|
|
recipe.items = table.copy(listentry.recipes[1].items)
|
|
|
|
for key, recipe_item in pairs(recipe.items) do
|
|
|
|
local item = nil
|
|
|
|
if recipe_item:sub(1, 6) == "group:" then
|
|
|
|
local itemslist = cache.recipe_items_resolve_group(recipe_item)
|
2017-02-23 21:32:09 +01:00
|
|
|
for _, item_in_list in pairs(itemslist) do
|
|
|
|
if state.param.crafting_items_in_inventory[item_in_list.name] then
|
|
|
|
item = item_in_list.name
|
|
|
|
break
|
|
|
|
elseif filter.is_revealed_item(item_in_list.name, player) then
|
|
|
|
item = item_in_list.name
|
|
|
|
elseif item == nil then
|
|
|
|
item = item_in_list.name
|
2017-02-17 15:39:29 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if item then
|
|
|
|
recipe.items[key] = item
|
|
|
|
end
|
|
|
|
end
|
|
|
|
on_item_select(state, listentry.itemdef, recipe)
|
2017-02-14 23:26:48 +01:00
|
|
|
if state:get("inf_area"):getVisible() == false then
|
2017-02-11 23:30:24 +01:00
|
|
|
state:get("groups_btn"):submit()
|
|
|
|
end
|
2017-02-11 00:00:48 +01:00
|
|
|
end)
|
|
|
|
|
2017-02-08 21:55:29 +01:00
|
|
|
-- initial values
|
2017-02-15 22:01:30 +01:00
|
|
|
local player = state.location.rootState.location.player
|
2017-02-17 15:39:29 +01:00
|
|
|
|
|
|
|
local craftable, ciii = cache.get_recipes_craftable(player)
|
|
|
|
state.param.crafting_items_in_inventory = ciii
|
|
|
|
update_craftable_list(state, craftable)
|
2017-02-11 23:30:24 +01:00
|
|
|
group_sel:setSelected(1)
|
|
|
|
if group_sel:getSelectedItem() then
|
|
|
|
state:get("groups_btn"):setText(group_sel:getSelectedItem())
|
|
|
|
end
|
2017-02-05 13:51:48 +01:00
|
|
|
end
|
|
|
|
|
2017-02-09 20:57:59 +01:00
|
|
|
smart_inventory.register_page({
|
|
|
|
name = "crafting",
|
2017-02-13 22:26:43 +01:00
|
|
|
tooltip = "Craft new items",
|
2017-02-05 13:51:48 +01:00
|
|
|
icon = "inventory_btn.png",
|
2017-02-09 20:57:59 +01:00
|
|
|
smartfs_callback = crafting_callback,
|
2017-02-12 22:43:19 +01:00
|
|
|
sequence = 10
|
2017-02-05 13:51:48 +01:00
|
|
|
})
|