missions/transport.lua

228 lines
5.4 KiB
Lua
Raw Normal View History

2018-05-08 15:28:01 +02:00
local has_xp_redo_mod = minetest.get_modpath("xp_redo")
2018-05-07 09:34:37 +02:00
local update_formspec = function(meta)
2018-05-07 12:03:38 +02:00
local inv = meta:get_inventory()
2018-05-16 15:27:27 +02:00
local mission_name = meta:get_string("mission_name")
meta:set_string("infotext", "Mission-block: " .. mission_name)
2018-05-07 12:03:38 +02:00
2018-05-08 15:28:01 +02:00
local xp_str = function(str)
if has_xp_redo_mod then
return str
else
return ""
end
end
2018-05-07 12:03:38 +02:00
2018-05-07 09:34:37 +02:00
meta:set_string("formspec", "size[8,10;]" ..
-- col 1
2018-05-16 15:27:27 +02:00
"field[0,1.5;4,1;mission_name;Mission name;" .. mission_name .. "]" ..
2018-05-07 10:54:51 +02:00
"button_exit[4,1;2,1;save;Save]" ..
"button_exit[6,1;2,1;start;Start]" ..
2018-05-07 09:34:37 +02:00
-- col 2
"label[2,2;To]" ..
2018-05-07 10:54:51 +02:00
"list[context;to;3,2;1,1;]" ..
2018-05-07 09:55:55 +02:00
"field[6,2.5;2,1;time;Time (min);" .. meta:get_int("time") .. "]" ..
2018-05-07 09:34:37 +02:00
-- col 3
"label[0,3;Reward]" ..
2018-05-08 15:28:01 +02:00
"list[context;reward;2,3;3,1;]" ..
xp_str("field[6,3.5;2,1;rewardxp;XP-Reward;" .. meta:get_int("rewardxp") .. "]") ..
2018-05-07 09:34:37 +02:00
-- col 4
"label[0,4;Transport]" ..
2018-05-08 15:28:01 +02:00
"list[context;transport;2,4;3,1;]" ..
xp_str("field[6,4.5;2,1;penaltyxp;XP-Penalty;" .. meta:get_int("penaltyxp") .. "]") ..
2018-05-07 09:34:37 +02:00
-- col 5,6,7,8
"list[current_player;main;0,5;8,4;]")
end
2018-05-07 11:16:58 +02:00
2018-05-16 14:10:45 +02:00
minetest.register_node("missions:transport", {
description = "Transport mission",
tiles = {
"default_gold_block.png",
"default_gold_block.png",
"default_gold_block.png^default_steel_ingot.png^missions_m_overlay.png",
"default_gold_block.png^default_steel_ingot.png^missions_m_overlay.png",
"default_gold_block.png^default_steel_ingot.png^missions_m_overlay.png",
"default_gold_block.png^default_steel_ingot.png^missions_m_overlay.png"
},
2018-05-07 09:34:37 +02:00
groups = {cracky=3,oddly_breakable_by_hand=3},
sounds = default.node_sound_glass_defaults(),
after_place_node = function(pos, placer)
local meta = minetest.get_meta(pos)
meta:set_string("owner", placer:get_player_name() or "")
end,
on_construct = function(pos)
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
2018-05-07 09:55:55 +02:00
inv:set_size("to", 1)
2018-05-08 15:28:01 +02:00
inv:set_size("reward", 3)
inv:set_size("transport", 3)
2018-05-16 14:21:01 +02:00
meta:set_int("time", 300)
2018-05-16 15:27:27 +02:00
meta:set_string("mission_name", "My mission")
2018-05-08 15:28:01 +02:00
-- xp stuff
if has_xp_redo_mod then
meta:set_int("rewardxp", 10)
meta:set_int("penaltyxp", 20)
meta:set_int("entryxp", 0)
end
2018-05-07 09:55:55 +02:00
2018-05-07 09:34:37 +02:00
update_formspec(meta)
end,
2018-05-07 09:55:55 +02:00
allow_metadata_inventory_move = function(pos, from_list, from_index, to_list, to_index, count, player)
local meta = minetest.get_meta(pos)
-- owner
if player:get_player_name() == meta:get_string("owner") then
-- TODO: check book moves
return count
end
-- non-owner
return 0
end,
allow_metadata_inventory_put = function(pos, listname, index, stack, player)
local meta = minetest.get_meta(pos)
-- owner
if player:get_player_name() == meta:get_string("owner") then
local name = stack:get_name()
if listname == "from" or listname == "to" or listname == "book" then
if name == "default:book_written" then
return stack:get_count()
else
-- only written books allowed
return 0
end
end
return stack:get_count()
end
-- non-owner
return 0
end,
allow_metadata_inventory_take = function(pos, listname, index, stack, player)
local meta = minetest.get_meta(pos)
-- owner
if player:get_player_name() == meta:get_string("owner") then
return stack:get_count()
end
2018-05-16 15:27:27 +02:00
-- not allowed
2018-05-07 09:55:55 +02:00
return 0
end,
2018-05-07 09:34:37 +02:00
on_receive_fields = function(pos, formname, fields, sender)
local meta = minetest.get_meta(pos)
2018-05-07 09:55:55 +02:00
local name = sender:get_player_name()
if name == meta:get_string("owner") then
-- owner
if fields.save then
2018-05-16 15:27:27 +02:00
local name = fields.mission_name
meta:set_string("mission_name", fields.mission_name)
2018-05-07 09:55:55 +02:00
local time = tonumber(fields.time)
if time ~= nil then meta:set_int("time", time) end
2018-05-08 15:28:01 +02:00
local rewardxp = tonumber(fields.rewardxp)
if rewardxp~= nil then meta:set_int("rewardxp", rewardxp) end
local penaltyxp = tonumber(fields.penaltyxp)
if penaltyxp~= nil then meta:set_int("penaltyxp", penaltyxp) end
local entryxp = tonumber(fields.entryxp)
if entryxp~= nil then meta:set_int("entryxp", entryxp) end
2018-05-07 09:55:55 +02:00
end
else
-- non-owner
end
2018-05-07 10:54:51 +02:00
if fields.start then
local inv = meta:get_inventory()
local mission = {};
2018-05-16 15:27:27 +02:00
mission.name = meta:get_string("mission_name")
mission.type = "transport"
2018-05-07 10:54:51 +02:00
mission.time = meta:get_int("time")
2018-05-08 15:28:01 +02:00
if has_xp_redo_mod then
2018-05-16 14:21:01 +02:00
mission.xp = {
reward = meta:get_int("rewardxp"),
penalty = meta:get_int("penaltyxp")
}
2018-05-08 15:28:01 +02:00
end
2018-05-07 10:54:51 +02:00
local reward = {}
reward.list = {}
local i=1
while i<=inv:get_size("reward") do
local stack = inv:get_stack("reward", i)
if stack:get_count() > 0 then
table.insert(reward.list, stack:to_string())
end
i = i + 1
end
mission.reward = reward;
local transport = {}
transport.list = {}
i = 1
while i<=inv:get_size("transport") do
local stack = inv:get_stack("transport", i)
if stack:get_count() > 0 then
table.insert(transport.list, stack:to_string())
end
i = i + 1
end
mission.transport = transport
local toBookStack = inv:get_stack("to", 1)
2018-05-16 15:27:27 +02:00
if missions.is_book(toBookStack) then
2018-05-07 10:54:51 +02:00
-- to and mission books available
local target = minetest.deserialize(toBookStack:get_meta():get_string("text"))
if target == nil then
minetest.chat_send_player(sender:get_player_name(), "to-book malformed")
return
end
mission.target = target
2018-05-07 13:35:14 +02:00
missions.start_mission(sender, mission)
2018-05-07 12:03:38 +02:00
else
2018-05-16 15:27:27 +02:00
minetest.chat_send_player(sender:get_player_name(), "to-book not available")
2018-05-07 10:54:51 +02:00
end
end
2018-05-07 09:34:37 +02:00
update_formspec(meta)
end
2018-05-07 14:59:11 +02:00
})