forked from VoxeLibre/VoxeLibre
Villager trading: Use per-player trading inventory
This commit is contained in:
parent
9f2460c263
commit
cca841e459
|
@ -3,7 +3,6 @@
|
||||||
--made for MC like Survival game
|
--made for MC like Survival game
|
||||||
--License for code WTFPL and otherwise stated in readmes
|
--License for code WTFPL and otherwise stated in readmes
|
||||||
|
|
||||||
-- TODO/FIXME: Per-player trading inventories
|
|
||||||
-- TODO: Particles
|
-- TODO: Particles
|
||||||
-- TODO: 4s Regeneration I after trade unlock
|
-- TODO: 4s Regeneration I after trade unlock
|
||||||
-- FIXME: Possible to lock all trades
|
-- FIXME: Possible to lock all trades
|
||||||
|
@ -438,6 +437,8 @@ local function show_trade_formspec(playername, trader, tradenum)
|
||||||
disabled_img = "image[4.3,2.52;1,1;mobs_mc_trading_formspec_disabled.png]"..
|
disabled_img = "image[4.3,2.52;1,1;mobs_mc_trading_formspec_disabled.png]"..
|
||||||
"image[4.3,1.1;1,1;mobs_mc_trading_formspec_disabled.png]"
|
"image[4.3,1.1;1,1;mobs_mc_trading_formspec_disabled.png]"
|
||||||
end
|
end
|
||||||
|
local tradeinv_name = "mobs_mc:trade_"..playername
|
||||||
|
local tradeinv = minetest.formspec_escape("detached:"..tradeinv_name)
|
||||||
local formspec =
|
local formspec =
|
||||||
"size[9,8.75]"
|
"size[9,8.75]"
|
||||||
.."background[-0.19,-0.25;9.41,9.49;mobs_mc_trading_formspec_bg.png]"
|
.."background[-0.19,-0.25;9.41,9.49;mobs_mc_trading_formspec_bg.png]"
|
||||||
|
@ -448,16 +449,16 @@ local function show_trade_formspec(playername, trader, tradenum)
|
||||||
.."list[current_player;main;0,7.74;9,1;]"
|
.."list[current_player;main;0,7.74;9,1;]"
|
||||||
.."button[1,1;0.5,1;prev_trade;<]"
|
.."button[1,1;0.5,1;prev_trade;<]"
|
||||||
.."button[7.26,1;0.5,1;next_trade;>]"
|
.."button[7.26,1;0.5,1;next_trade;>]"
|
||||||
.."list[detached:mobs_mc:trade;wanted;2,1;2,1;]"
|
.."list["..tradeinv..";wanted;2,1;2,1;]"
|
||||||
.."list[detached:mobs_mc:trade;offered;5.76,1;1,1;]"
|
.."list["..tradeinv..";offered;5.76,1;1,1;]"
|
||||||
.."list[detached:mobs_mc:trade;input;2,2.5;2,1;]"
|
.."list["..tradeinv..";input;2,2.5;2,1;]"
|
||||||
.."list[detached:mobs_mc:trade;output;5.76,2.55;1,1;]"
|
.."list["..tradeinv..";output;5.76,2.55;1,1;]"
|
||||||
.."listring[detached:mobs_mc:trade;output]"
|
.."listring["..tradeinv..";output]"
|
||||||
.."listring[current_player;main]"
|
.."listring[current_player;main]"
|
||||||
.."listring[detached:mobs_mc:trade;input]"
|
.."listring["..tradeinv..";input]"
|
||||||
.."listring[current_player;main]"
|
.."listring[current_player;main]"
|
||||||
minetest.sound_play("mobs_mc_villager_trade", {to_player = playername})
|
minetest.sound_play("mobs_mc_villager_trade", {to_player = playername})
|
||||||
minetest.show_formspec(playername, "mobs_mc:trade", formspec)
|
minetest.show_formspec(playername, tradeinv_name, formspec)
|
||||||
end
|
end
|
||||||
|
|
||||||
local update_offer = function(inv, player, sound)
|
local update_offer = function(inv, player, sound)
|
||||||
|
@ -608,10 +609,105 @@ mobs:register_mob("mobs_mc:villager", {
|
||||||
|
|
||||||
player_trading_with[name] = self
|
player_trading_with[name] = self
|
||||||
|
|
||||||
-- TODO: Create per-player trading inventories
|
local inv = minetest.get_inventory({type="detached", name="mobs_mc:trade_"..name})
|
||||||
local inv = minetest.get_inventory({type="detached", name="mobs_mc:trade"})
|
|
||||||
if not inv then
|
player_tradenum[name] = 1
|
||||||
inv = minetest.create_detached_inventory("mobs_mc:trade", {
|
set_trade(self, clicker, inv, player_tradenum[name], player_tradenum[name])
|
||||||
|
|
||||||
|
show_trade_formspec(name, self)
|
||||||
|
end,
|
||||||
|
|
||||||
|
on_spawn = function(self)
|
||||||
|
init_profession(self)
|
||||||
|
end,
|
||||||
|
})
|
||||||
|
|
||||||
|
-- Returns a single itemstack in the given inventory to the player's main inventory, or drop it when there's no space left
|
||||||
|
local function return_item(itemstack, dropper, pos, inv_p)
|
||||||
|
if dropper:is_player() then
|
||||||
|
-- Return to main inventory
|
||||||
|
if inv_p:room_for_item("main", itemstack) then
|
||||||
|
inv_p:add_item("main", itemstack)
|
||||||
|
else
|
||||||
|
-- Drop item on the ground
|
||||||
|
local v = dropper:get_look_dir()
|
||||||
|
local p = {x=pos.x, y=pos.y+1.2, z=pos.z}
|
||||||
|
p.x = p.x+(math.random(1,3)*0.2)
|
||||||
|
p.z = p.z+(math.random(1,3)*0.2)
|
||||||
|
local obj = minetest.add_item(p, itemstack)
|
||||||
|
if obj then
|
||||||
|
v.x = v.x*4
|
||||||
|
v.y = v.y*4 + 2
|
||||||
|
v.z = v.z*4
|
||||||
|
obj:setvelocity(v)
|
||||||
|
obj:get_luaentity()._insta_collect = false
|
||||||
|
end
|
||||||
|
end
|
||||||
|
else
|
||||||
|
-- Fallback for unexpected cases
|
||||||
|
minetest.add_item(pos, itemstack)
|
||||||
|
end
|
||||||
|
return itemstack
|
||||||
|
end
|
||||||
|
|
||||||
|
local return_fields = function(player)
|
||||||
|
local name = player:get_player_name()
|
||||||
|
local inv_t = minetest.get_inventory({type="detached", name = "mobs_mc:trade_"..name})
|
||||||
|
local inv_p = player:get_inventory()
|
||||||
|
for i=1, inv_t:get_size("input") do
|
||||||
|
local stack = inv_t:get_stack("input", i)
|
||||||
|
return_item(stack, player, player:get_pos(), inv_p)
|
||||||
|
stack:clear()
|
||||||
|
inv_t:set_stack("input", i, stack)
|
||||||
|
end
|
||||||
|
inv_t:set_stack("output", 1, "")
|
||||||
|
end
|
||||||
|
|
||||||
|
minetest.register_on_player_receive_fields(function(player, formname, fields)
|
||||||
|
if string.sub(formname, 1, 14) == "mobs_mc:trade_" then
|
||||||
|
local name = player:get_player_name()
|
||||||
|
if fields.quit then
|
||||||
|
return_fields(player)
|
||||||
|
player_trading_with[name] = nil
|
||||||
|
elseif fields.next_trade then
|
||||||
|
local trader = player_trading_with[name]
|
||||||
|
if not trader or not trader.object:get_luaentity() then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
local trades = trader._trades
|
||||||
|
if not trades then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
player_tradenum[name] = player_tradenum[name] + 1
|
||||||
|
local inv = minetest.get_inventory({type="detached", name="mobs_mc:trade_"..name})
|
||||||
|
set_trade(trader, player, inv, player_tradenum[name])
|
||||||
|
update_offer(inv, player, false)
|
||||||
|
show_trade_formspec(name, trader, player_tradenum[name])
|
||||||
|
elseif fields.prev_trade then
|
||||||
|
local trader = player_trading_with[name]
|
||||||
|
if not trader or not trader.object:get_luaentity() then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
local trades = trader._trades
|
||||||
|
if not trades then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
player_tradenum[name] = player_tradenum[name] - 1
|
||||||
|
local inv = minetest.get_inventory({type="detached", name="mobs_mc:trade_"..name})
|
||||||
|
set_trade(trader, player, inv, player_tradenum[name])
|
||||||
|
update_offer(inv, player, false)
|
||||||
|
show_trade_formspec(name, trader, player_tradenum[name])
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end)
|
||||||
|
|
||||||
|
minetest.register_on_leaveplayer(function(player)
|
||||||
|
return_fields(player)
|
||||||
|
player_tradenum[player:get_player_name()] = nil
|
||||||
|
player_trading_with[player:get_player_name()] = nil
|
||||||
|
end)
|
||||||
|
|
||||||
|
local trade_inventory = {
|
||||||
allow_take = function(inv, listname, index, stack, player)
|
allow_take = function(inv, listname, index, stack, player)
|
||||||
if listname == "input" then
|
if listname == "input" then
|
||||||
return stack:get_count()
|
return stack:get_count()
|
||||||
|
@ -774,111 +870,23 @@ mobs:register_mob("mobs_mc:villager", {
|
||||||
minetest.sound_play("mobs_mc_villager_deny", {to_player = name})
|
minetest.sound_play("mobs_mc_villager_deny", {to_player = name})
|
||||||
end
|
end
|
||||||
end,
|
end,
|
||||||
})
|
}
|
||||||
|
|
||||||
|
|
||||||
|
minetest.register_on_joinplayer(function(player)
|
||||||
|
local name = player:get_player_name()
|
||||||
|
player_tradenum[name] = 1
|
||||||
|
player_trading_with[name] = nil
|
||||||
|
|
||||||
|
-- Create or get player-specific trading inventory
|
||||||
|
local inv = minetest.get_inventory({type="detached", name="mobs_mc:trade_"..name})
|
||||||
|
if not inv then
|
||||||
|
inv = minetest.create_detached_inventory("mobs_mc:trade_"..name, trade_inventory, name)
|
||||||
end
|
end
|
||||||
inv:set_size("input", 2)
|
inv:set_size("input", 2)
|
||||||
inv:set_size("output", 1)
|
inv:set_size("output", 1)
|
||||||
inv:set_size("wanted", 2)
|
inv:set_size("wanted", 2)
|
||||||
inv:set_size("offered", 1)
|
inv:set_size("offered", 1)
|
||||||
|
|
||||||
player_tradenum[name] = 1
|
|
||||||
set_trade(self, clicker, inv, player_tradenum[name], player_tradenum[name])
|
|
||||||
|
|
||||||
show_trade_formspec(name, self)
|
|
||||||
end,
|
|
||||||
|
|
||||||
on_spawn = function(self)
|
|
||||||
init_profession(self)
|
|
||||||
end,
|
|
||||||
})
|
|
||||||
|
|
||||||
-- Returns a single itemstack in the given inventory to the player's main inventory, or drop it when there's no space left
|
|
||||||
local function return_item(itemstack, dropper, pos, inv_p)
|
|
||||||
if dropper:is_player() then
|
|
||||||
-- Return to main inventory
|
|
||||||
if inv_p:room_for_item("main", itemstack) then
|
|
||||||
inv_p:add_item("main", itemstack)
|
|
||||||
else
|
|
||||||
-- Drop item on the ground
|
|
||||||
local v = dropper:get_look_dir()
|
|
||||||
local p = {x=pos.x, y=pos.y+1.2, z=pos.z}
|
|
||||||
p.x = p.x+(math.random(1,3)*0.2)
|
|
||||||
p.z = p.z+(math.random(1,3)*0.2)
|
|
||||||
local obj = minetest.add_item(p, itemstack)
|
|
||||||
if obj then
|
|
||||||
v.x = v.x*4
|
|
||||||
v.y = v.y*4 + 2
|
|
||||||
v.z = v.z*4
|
|
||||||
obj:setvelocity(v)
|
|
||||||
obj:get_luaentity()._insta_collect = false
|
|
||||||
end
|
|
||||||
end
|
|
||||||
else
|
|
||||||
-- Fallback for unexpected cases
|
|
||||||
minetest.add_item(pos, itemstack)
|
|
||||||
end
|
|
||||||
return itemstack
|
|
||||||
end
|
|
||||||
|
|
||||||
local return_fields = function(player)
|
|
||||||
local inv_t = minetest.get_inventory({type="detached", name = "mobs_mc:trade"})
|
|
||||||
local inv_p = player:get_inventory()
|
|
||||||
for i=1, inv_t:get_size("input") do
|
|
||||||
local stack = inv_t:get_stack("input", i)
|
|
||||||
return_item(stack, player, player:get_pos(), inv_p)
|
|
||||||
stack:clear()
|
|
||||||
inv_t:set_stack("input", i, stack)
|
|
||||||
end
|
|
||||||
inv_t:set_stack("output", 1, "")
|
|
||||||
end
|
|
||||||
|
|
||||||
minetest.register_on_player_receive_fields(function(player, formname, fields)
|
|
||||||
if formname == "mobs_mc:trade" then
|
|
||||||
local name = player:get_player_name()
|
|
||||||
if fields.quit then
|
|
||||||
return_fields(player)
|
|
||||||
player_trading_with[name] = nil
|
|
||||||
elseif fields.next_trade then
|
|
||||||
local trader = player_trading_with[name]
|
|
||||||
if not trader or not trader.object:get_luaentity() then
|
|
||||||
return
|
|
||||||
end
|
|
||||||
local trades = trader._trades
|
|
||||||
if not trades then
|
|
||||||
return
|
|
||||||
end
|
|
||||||
player_tradenum[name] = player_tradenum[name] + 1
|
|
||||||
local inv = minetest.get_inventory({type="detached", name="mobs_mc:trade"})
|
|
||||||
set_trade(trader, player, inv, player_tradenum[name])
|
|
||||||
update_offer(inv, player, false)
|
|
||||||
show_trade_formspec(name, trader, player_tradenum[name])
|
|
||||||
elseif fields.prev_trade then
|
|
||||||
local trader = player_trading_with[name]
|
|
||||||
if not trader or not trader.object:get_luaentity() then
|
|
||||||
return
|
|
||||||
end
|
|
||||||
local trades = trader._trades
|
|
||||||
if not trades then
|
|
||||||
return
|
|
||||||
end
|
|
||||||
player_tradenum[name] = player_tradenum[name] - 1
|
|
||||||
local inv = minetest.get_inventory({type="detached", name="mobs_mc:trade"})
|
|
||||||
set_trade(trader, player, inv, player_tradenum[name])
|
|
||||||
update_offer(inv, player, false)
|
|
||||||
show_trade_formspec(name, trader, player_tradenum[name])
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end)
|
|
||||||
|
|
||||||
minetest.register_on_leaveplayer(function(player)
|
|
||||||
return_fields(player)
|
|
||||||
player_tradenum[player:get_player_name()] = nil
|
|
||||||
player_trading_with[player:get_player_name()] = nil
|
|
||||||
end)
|
|
||||||
|
|
||||||
minetest.register_on_joinplayer(function(player)
|
|
||||||
player_tradenum[player:get_player_name()] = 1
|
|
||||||
player_trading_with[player:get_player_name()] = nil
|
|
||||||
end)
|
end)
|
||||||
|
|
||||||
mobs:spawn_specific("mobs_mc:villager", mobs_mc.spawn.village, {"air"}, 0, minetest.LIGHT_MAX+1, 30, 8000, 4, mobs_mc.spawn_height.water+1, mobs_mc.spawn_height.overworld_max)
|
mobs:spawn_specific("mobs_mc:villager", mobs_mc.spawn.village, {"air"}, 0, minetest.LIGHT_MAX+1, 30, 8000, 4, mobs_mc.spawn_height.water+1, mobs_mc.spawn_height.overworld_max)
|
||||||
|
|
Reference in New Issue