Store seed into generated saplings

master
Wuzzy 2014-10-16 14:35:18 +02:00
parent 0ae9a78caf
commit a850c486a7
1 changed files with 13 additions and 5 deletions

View File

@ -155,23 +155,26 @@ end
--[[ Generates a sapling as an ItemStack to mess around later with
tree_id: ID of tree the sapling will grow
seed: Seed of the tree the sapling will grow (optional, can be nil)
returns: an ItemStack which contains one sapling of the specified tree, on success
false on failure (if tree does not exist)
]]
function ltool.generate_sapling(tree_id)
function ltool.generate_sapling(tree_id, seed)
local tree = ltool.trees[tree_id]
if(tree == nil) then
return false
end
local sapling = ItemStack("ltool:sapling")
-- TODO: Copy the seed into the sapling, too.
tree.treedef = seed
sapling:set_metadata(minetest.serialize(tree.treedef))
tree.treedef = nil
return sapling
end
--[[ Gives a L-system tree sapling to a player
tree_id: ID of tree the sapling will grow
seed: Seed of the tree (optional; can be nil)
playername: name of the player to which
ignore_priv: if true, players lplant privilige is not checked (optional argument; default: false)
@ -181,19 +184,20 @@ end
false, 2 if players inventory is full
false, 3 if tree does not exist
]]
function ltool.give_sapling(tree_id, player_name, ignore_priv)
function ltool.give_sapling(tree_id, seed, player_name, ignore_priv)
if(ignore_priv == nil) then ignore_priv = false end
if(ignore_priv == false and privs.lplant ~= true) then
return false, 1
end
local sapling = ItemStack("ltool:sapling")
local player = minetest.get_player_by_name(player_name)
-- TODO: Copy the seed into the sapling, too.
local tree = ltool.trees[tree_id]
if(tree == nil) then
return false, 3
end
tree.treedef.seed = seed
sapling:set_metadata(minetest.serialize(tree.treedef))
tree.treedef.seed = nil
local leftover = player:get_inventory():add_item("main", sapling)
if(not leftover:is_empty()) then
return false, 2
@ -766,7 +770,11 @@ function ltool.process_form(player,formname,fields)
ltool.show_dialog(playername, "ltool:treeform_error_sapling", message)
return
end
local ret, ret2 = ltool.give_sapling(seltree_id, playername, true)
local seed = nil
if(tonumber(fields.seed)~=nil) then
seed = tonumber(fields.seed)
end
local ret, ret2 = ltool.give_sapling(seltree_id, seed, playername, true)
if(ret==false and ret2==2) then
ltool.save_fields(playername, formname, fields)
ltool.show_dialog(playername, "ltool:treeform_error_sapling", "Error: The sapling could not be given to you. Probably your inventory is full.")