2018-07-08 09:30:13 +02:00
|
|
|
--[[
|
|
|
|
Exchange Shop
|
|
|
|
|
|
|
|
This code is based on the idea of Dan Duncombe's exchange shop
|
|
|
|
https://web.archive.org/web/20160403113102/https://forum.minetest.net/viewtopic.php?id=7002
|
|
|
|
]]
|
|
|
|
|
2020-04-21 20:00:41 +02:00
|
|
|
local S = exchange_shop.S
|
|
|
|
local FS = exchange_shop.FS
|
2018-07-08 09:30:13 +02:00
|
|
|
local shop_positions = {}
|
|
|
|
|
2018-07-09 10:54:02 +02:00
|
|
|
local function get_exchange_shop_formspec(mode, pos, meta)
|
2022-03-23 23:37:58 +01:00
|
|
|
local fs_prepend = default.gui_bg .. -- default.listcolors ..
|
|
|
|
"background[0,0;0,0;formspec_background_color.png^formspec_backround.png;true]"
|
|
|
|
|
|
|
|
local name = "nodemeta:" .. pos.x .. "," .. pos.y .. "," .. pos.z
|
2018-07-09 10:54:02 +02:00
|
|
|
meta = meta or minetest.get_meta(pos)
|
2018-07-08 09:30:13 +02:00
|
|
|
|
|
|
|
local function listring(src)
|
2022-03-23 23:37:58 +01:00
|
|
|
return "listring[" .. name .. ";" .. src .. "]" ..
|
2018-07-08 09:30:13 +02:00
|
|
|
"listring[current_player;main]"
|
|
|
|
end
|
2020-04-21 21:28:10 +02:00
|
|
|
local function make_slots(x, y, list, label)
|
2022-03-23 23:37:58 +01:00
|
|
|
-- local arrow = "default_arrow_bg.png"
|
|
|
|
-- if list == "cust_ow" then
|
|
|
|
-- arrow = arrow .. "\\^\\[transformFY"
|
|
|
|
-- end
|
2020-04-21 21:28:10 +02:00
|
|
|
return table.concat({
|
2022-03-23 23:37:58 +01:00
|
|
|
("label[%f,%f;%s]"):format(x, y - 0.5, label),
|
|
|
|
-- ("image[%f,%f;0.6,0.6;shop_front.png]"):format(x + 0.15, y + 0.3),
|
|
|
|
-- ("image[%f,%f;0.6,0.6;%s]"):format(x + 0.15, y + 0.0, arrow),
|
|
|
|
("list[" .. name .. ";%s;%f,%f;2,2;]"):format(list, x, y)
|
2020-04-21 21:28:10 +02:00
|
|
|
})
|
|
|
|
end
|
2018-07-08 09:30:13 +02:00
|
|
|
if mode == "customer" then
|
2018-07-09 10:54:02 +02:00
|
|
|
local overflow = not meta:get_inventory():is_empty("cust_ej")
|
|
|
|
|
2018-07-08 09:30:13 +02:00
|
|
|
-- customer
|
2018-07-09 10:54:02 +02:00
|
|
|
local formspec = (
|
2022-03-23 23:37:58 +01:00
|
|
|
"size[9,8.75]" ..
|
|
|
|
"item_image[0,-0.1;1,1;".. exchange_shop.shopname .. "]" ..
|
|
|
|
"label[0.9,0.1;" .. S("Exchange Shop") .. "]" ..
|
|
|
|
fs_prepend ..
|
|
|
|
default.gui_close_btn() ..
|
|
|
|
make_slots(1, 1.1, "cust_ow", FS("You give:")) ..
|
|
|
|
"button[3,3.2;3,1;exchange;" .. FS("Exchange") .. "]" ..
|
|
|
|
make_slots(6, 1.1, "cust_og", FS("You get:"))
|
2018-07-09 10:54:02 +02:00
|
|
|
)
|
|
|
|
-- Insert fallback slots
|
2022-03-23 23:37:58 +01:00
|
|
|
local inv_pos = 4.5
|
2018-07-09 10:54:02 +02:00
|
|
|
if overflow then
|
|
|
|
formspec = (formspec ..
|
2022-03-23 23:37:58 +01:00
|
|
|
"label[0.7,3.5;Ejected items:]" ..
|
|
|
|
"label[0.7,3.8;(Remove me!)]" ..
|
|
|
|
"list[" .. name .. ";cust_ej;3,3.5;4,1;]"
|
2018-07-09 10:54:02 +02:00
|
|
|
)
|
|
|
|
inv_pos = 5
|
|
|
|
end
|
|
|
|
return (formspec ..
|
2022-03-23 23:37:58 +01:00
|
|
|
"list[current_player;main;0," .. inv_pos .. ";9,4;]" ..
|
2018-07-09 10:54:02 +02:00
|
|
|
(overflow and listring("cust_ej") or "")
|
2018-07-08 09:30:13 +02:00
|
|
|
)
|
|
|
|
end
|
|
|
|
if mode == "owner_custm"
|
|
|
|
or mode == "owner_stock" then
|
2018-07-09 10:54:02 +02:00
|
|
|
local overflow = not meta:get_inventory():is_empty("custm_ej")
|
2022-03-23 23:37:58 +01:00
|
|
|
-- local title = meta:get_string("title")
|
2018-07-09 10:54:02 +02:00
|
|
|
|
2018-07-08 09:30:13 +02:00
|
|
|
-- owner
|
|
|
|
local formspec = (
|
2022-03-23 23:37:58 +01:00
|
|
|
"size[10,10]" .. fs_prepend ..
|
|
|
|
"item_image[0,-0.1;1,1;".. exchange_shop.shopname .. "]" ..
|
|
|
|
"label[0.9,0.1;" .. S("Exchange Shop") .. "]" ..
|
|
|
|
default.gui_close_btn("9.3,-0.1") ..
|
|
|
|
-- "field[0.3,0.65;3,0.5;title;" .. FS("Title:") .. ";" .. title .. "]" ..
|
|
|
|
-- "field_close_on_enter[title;false]" ..
|
|
|
|
-- "button[2.8,0.3;1,0.61;set_title;" .. FS("Set") .. "]" ..
|
|
|
|
make_slots(0.1, 2, "cust_ow", FS("You need:")) ..
|
|
|
|
make_slots(2.6, 2, "cust_og", FS("You give:")) ..
|
|
|
|
"label[5,0.4;" .. FS("Current stock:") .. "]"
|
2018-07-08 09:30:13 +02:00
|
|
|
)
|
|
|
|
|
2018-07-09 10:54:02 +02:00
|
|
|
if overflow then
|
2022-03-23 23:37:58 +01:00
|
|
|
formspec = (formspec ..
|
|
|
|
"list[" .. name .. ";custm_ej;0.2,4;4,1;]" ..
|
|
|
|
"label[0.2,4.9;Ejected items: (Remove me!)]" ..
|
2018-07-09 10:54:02 +02:00
|
|
|
listring("custm_ej")
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2021-11-25 21:01:05 +01:00
|
|
|
local arrow = "default_arrow_bg.png"
|
2018-07-08 09:30:13 +02:00
|
|
|
if mode == "owner_custm" then
|
2022-03-23 23:37:58 +01:00
|
|
|
formspec = (formspec ..
|
|
|
|
"button[6,5.25;2,0.5;view_stock;" .. FS("Income") .. "]" ..
|
|
|
|
"list[" .. name .. ";custm;5,1;5,4;]" ..
|
2018-07-08 09:30:13 +02:00
|
|
|
listring("custm"))
|
2020-04-21 21:28:10 +02:00
|
|
|
arrow = arrow .. "\\^\\[transformFY"
|
2018-07-08 09:30:13 +02:00
|
|
|
else
|
2022-03-23 23:37:58 +01:00
|
|
|
formspec = (formspec ..
|
|
|
|
"button[6,5.25;2,0.5;view_custm;" .. FS("Outgoing") .. "]" ..
|
|
|
|
"list[" .. name .. ";stock;5,1;5,4;]" ..
|
2018-07-08 09:30:13 +02:00
|
|
|
listring("stock"))
|
|
|
|
end
|
2022-03-23 23:37:58 +01:00
|
|
|
return (formspec ..
|
|
|
|
"label[1,5.4;" .. FS("Use (E) + (Right click) for customer interface") .. "]" ..
|
2020-04-21 21:28:10 +02:00
|
|
|
"image[8.2,5.2;0.6,0.6;" .. arrow .. "]" ..
|
2022-03-23 23:37:58 +01:00
|
|
|
"list[current_player;main;0.5,6;9,4;]")
|
2018-07-08 09:30:13 +02:00
|
|
|
end
|
|
|
|
return ""
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
minetest.register_on_player_receive_fields(function(sender, formname, fields)
|
|
|
|
if formname ~= "exchange_shop:shop_formspec" then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local player_name = sender:get_player_name()
|
|
|
|
local pos = shop_positions[player_name]
|
|
|
|
if not pos then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
if (fields.quit and fields.quit ~= "") or
|
2022-03-23 23:37:58 +01:00
|
|
|
minetest.get_node(pos).name ~= exchange_shop.shopname then
|
2018-07-08 09:30:13 +02:00
|
|
|
shop_positions[player_name] = nil
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local meta = minetest.get_meta(pos)
|
|
|
|
local title = meta:get_string("title")
|
|
|
|
local shop_owner = meta:get_string("owner")
|
|
|
|
|
2022-03-23 23:37:58 +01:00
|
|
|
local ftitle = fields.title
|
|
|
|
if ftitle and exchange_shop.has_access(meta, player_name) then
|
2018-07-08 09:30:13 +02:00
|
|
|
-- Limit title length
|
2022-03-23 23:37:58 +01:00
|
|
|
ftitle = ftitle:sub(1, 80)
|
|
|
|
if title ~= ftitle then
|
|
|
|
local title_text = (ftitle and ftitle ~= "") and
|
|
|
|
S("Exchange Shop: \"@1\"", ftitle) or S("Exchange Shop")
|
|
|
|
meta:set_string("infotext", title_text .. "\n" .. S("Owned by @1", shop_owner))
|
|
|
|
meta:set_string("title", minetest.formspec_escape(ftitle))
|
2018-07-08 09:30:13 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if fields.exchange then
|
|
|
|
local shop_inv = meta:get_inventory()
|
|
|
|
local player_inv = sender:get_inventory()
|
|
|
|
if shop_inv:is_empty("cust_ow")
|
|
|
|
and shop_inv:is_empty("cust_og") then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2022-03-23 23:37:58 +01:00
|
|
|
local err_msg, resend = exchange_shop.exchange_action(player_inv, shop_inv, pos)
|
2018-07-08 09:30:13 +02:00
|
|
|
-- Throw error message
|
|
|
|
if err_msg then
|
|
|
|
minetest.chat_send_player(player_name, minetest.colorize("#F33",
|
2022-03-23 23:37:58 +01:00
|
|
|
S("Exchange Shop:") .. " " .. err_msg))
|
2018-07-08 09:30:13 +02:00
|
|
|
end
|
2018-07-09 10:54:02 +02:00
|
|
|
if resend then
|
|
|
|
minetest.show_formspec(player_name, "exchange_shop:shop_formspec",
|
|
|
|
get_exchange_shop_formspec("customer", pos, meta))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if (fields.view_custm or fields.view_stock)
|
|
|
|
and exchange_shop.has_access(meta, player_name) then
|
|
|
|
local mode = "owner_stock"
|
2018-07-08 09:30:13 +02:00
|
|
|
if fields.view_custm then
|
|
|
|
mode = "owner_custm"
|
|
|
|
end
|
|
|
|
minetest.show_formspec(player_name, "exchange_shop:shop_formspec",
|
2018-07-09 10:54:02 +02:00
|
|
|
get_exchange_shop_formspec(mode, pos, meta))
|
2018-07-08 09:30:13 +02:00
|
|
|
end
|
|
|
|
end)
|
|
|
|
|
|
|
|
minetest.register_node(exchange_shop.shopname, {
|
2022-03-23 23:37:58 +01:00
|
|
|
description = S("Exchange Shop"),
|
2018-07-08 09:30:13 +02:00
|
|
|
tiles = {
|
2021-11-25 21:01:05 +01:00
|
|
|
"shop_top.png", "shop_top.png",
|
2018-07-08 09:30:13 +02:00
|
|
|
"shop_side.png","shop_side.png",
|
|
|
|
"shop_side.png", "shop_front.png"
|
|
|
|
},
|
|
|
|
paramtype2 = "facedir",
|
2018-08-15 18:48:06 +02:00
|
|
|
groups = {choppy=2, oddly_breakable_by_hand=2},
|
2018-07-08 09:30:13 +02:00
|
|
|
sounds = default.node_sound_wood_defaults(),
|
|
|
|
after_place_node = function(pos, placer)
|
|
|
|
local meta = minetest.get_meta(pos)
|
|
|
|
local owner = placer:get_player_name()
|
|
|
|
meta:set_string("owner", owner)
|
2022-03-23 23:37:58 +01:00
|
|
|
meta:set_string("infotext", S("Exchange Shop") .. "\n" .. S("Owned by @1", owner))
|
2018-07-08 09:30:13 +02:00
|
|
|
end,
|
|
|
|
on_construct = function(pos)
|
|
|
|
local meta = minetest.get_meta(pos)
|
2020-04-21 20:00:41 +02:00
|
|
|
meta:set_string("infotext", S("Exchange shop (constructing)"))
|
2018-07-08 09:30:13 +02:00
|
|
|
meta:set_string("owner", "")
|
|
|
|
local inv = meta:get_inventory()
|
|
|
|
inv:set_size("stock", exchange_shop.storage_size) -- needed stock for exchanges
|
|
|
|
inv:set_size("custm", exchange_shop.storage_size) -- stock of the customers exchanges
|
|
|
|
inv:set_size("custm_ej", 4) -- ejected items if shop has no inventory room
|
|
|
|
inv:set_size("cust_ow", 2*2) -- owner wants
|
|
|
|
inv:set_size("cust_og", 2*2) -- owner gives
|
|
|
|
inv:set_size("cust_ej", 4) -- ejected items if player has no inventory room
|
|
|
|
end,
|
|
|
|
can_dig = function(pos,player)
|
|
|
|
local meta = minetest.get_meta(pos)
|
|
|
|
local inv = meta:get_inventory()
|
|
|
|
if inv:is_empty("stock") and inv:is_empty("custm")
|
|
|
|
and inv:is_empty("cust_ow") and inv:is_empty("custm_ej")
|
|
|
|
and inv:is_empty("cust_og") and inv:is_empty("cust_ej") then
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
minetest.chat_send_player(player:get_player_name(),
|
2020-04-21 20:00:41 +02:00
|
|
|
S("Cannot dig exchange shop: one or multiple stocks are in use."))
|
2018-07-08 09:30:13 +02:00
|
|
|
return false
|
|
|
|
end,
|
2022-03-23 23:37:58 +01:00
|
|
|
on_rightclick = function(pos, _, clicker)
|
2018-07-08 09:30:13 +02:00
|
|
|
local meta = minetest.get_meta(pos)
|
|
|
|
local player_name = clicker:get_player_name()
|
|
|
|
|
|
|
|
local mode = "customer"
|
|
|
|
if exchange_shop.has_access(meta, player_name) and
|
|
|
|
not clicker:get_player_control().aux1 then
|
|
|
|
mode = "owner_custm"
|
|
|
|
end
|
|
|
|
shop_positions[player_name] = pos
|
|
|
|
minetest.show_formspec(player_name, "exchange_shop:shop_formspec",
|
2018-07-09 10:54:02 +02:00
|
|
|
get_exchange_shop_formspec(mode, pos, meta))
|
2018-07-08 09:30:13 +02:00
|
|
|
end,
|
2022-03-23 23:37:58 +01:00
|
|
|
allow_metadata_inventory_move = function(pos, _, _, _, _, count, player)
|
2018-07-08 09:30:13 +02:00
|
|
|
local meta = minetest.get_meta(pos)
|
|
|
|
if exchange_shop.has_access(meta, player:get_player_name()) then
|
|
|
|
return count
|
|
|
|
end
|
|
|
|
return 0
|
|
|
|
end,
|
2022-03-23 23:37:58 +01:00
|
|
|
allow_metadata_inventory_put = function(pos, listname, _, stack, player)
|
2018-07-08 09:30:13 +02:00
|
|
|
if listname == "custm" then
|
|
|
|
minetest.chat_send_player(player:get_player_name(),
|
2020-04-21 20:00:41 +02:00
|
|
|
S("Exchange shop: Insert your trade goods into 'Outgoing'."))
|
2018-07-08 09:30:13 +02:00
|
|
|
return 0
|
|
|
|
end
|
|
|
|
local meta = minetest.get_meta(pos)
|
|
|
|
if exchange_shop.has_access(meta, player:get_player_name())
|
|
|
|
and listname ~= "cust_ej"
|
|
|
|
and listname ~= "custm_ej" then
|
|
|
|
return stack:get_count()
|
|
|
|
end
|
|
|
|
return 0
|
|
|
|
end,
|
2022-03-23 23:37:58 +01:00
|
|
|
allow_metadata_inventory_take = function(pos, listname, _, stack, player)
|
2018-07-08 09:30:13 +02:00
|
|
|
local meta = minetest.get_meta(pos)
|
|
|
|
if exchange_shop.has_access(meta, player:get_player_name())
|
|
|
|
or listname == "cust_ej" then
|
|
|
|
return stack:get_count()
|
|
|
|
end
|
|
|
|
return 0
|
|
|
|
end,
|
|
|
|
})
|
|
|
|
|
|
|
|
minetest.register_craft({
|
2022-03-23 23:37:58 +01:00
|
|
|
output = exchange_shop.shopname,
|
2018-07-08 09:30:13 +02:00
|
|
|
recipe = {
|
2022-03-23 23:37:58 +01:00
|
|
|
{"default:ruby", "default:ruby", "default:ruby"},
|
|
|
|
{"default:ruby", "default:chest", "default:ruby"},
|
|
|
|
{"default:ruby", "default:ruby", "default:ruby"}
|
2018-07-08 09:30:13 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
minetest.register_on_leaveplayer(function(player)
|
|
|
|
shop_positions[player:get_player_name()] = nil
|
|
|
|
end)
|