From 38f92367ce0f47b97fa8c4613535a7f9b0e7ce16 Mon Sep 17 00:00:00 2001 From: Wuzzy Date: Thu, 9 Feb 2017 13:30:31 +0100 Subject: [PATCH] Add seed parameter in lplant cmd and function --- API.md | 3 ++- init.lua | 18 +++++++++++++----- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/API.md b/API.md index 648ce49..f1d8feb 100644 --- a/API.md +++ b/API.md @@ -133,12 +133,13 @@ It depends: -### `ltool.plant_tree(tree_id, pos)` +### `ltool.plant_tree(tree_id, pos, seed)` Plants a tree as the specified position. #### Parameters * `tree_id`: ID of tree to be planted * `pos`: Position of tree, in the format `{x=?, y=?, z=?}` +* `seed`: Optional randomness seed, equal seed creates equal trees #### Return value `false` on failure, `nil` otherwise. diff --git a/init.lua b/init.lua index 162b3aa..45d136e 100644 --- a/init.lua +++ b/init.lua @@ -204,15 +204,23 @@ end --[[ Plants a tree as the specified position tree_id: ID of tree to be planted pos: Position of tree, in format {x=?, y=?, z=?} + seed: Optional seed for randomness, equal seed makes equal trees returns false on failure, nil otherwise ]] -function ltool.plant_tree(tree_id, pos) +function ltool.plant_tree(tree_id, pos, seed) local tree = ltool.trees[tree_id] if(tree==nil) then return false end - minetest.spawn_tree(pos, tree.treedef) + local treedef + if seed ~= nil then + treedef = table.copy(tree.treedef) + treedef.seed = seed + else + treedef = tree.treedef + end + minetest.spawn_tree(pos, treedef) end --[[ Tries to return a tree data structure for a given tree_id @@ -821,8 +829,8 @@ minetest.register_chatcommand("lplant", params = " ", func = function(playername, param) local p = {} - local tree_id, x, y, z = string.match(param, "^([^ ]+) +([%d.-]+)[, ] *([%d.-]+)[, ] *([%d.-]+)$") - tree_id, p.x, p.y, p.z = tonumber(tree_id), tonumber(x), tonumber(y), tonumber(z) + local tree_id, x, y, z, seed = string.match(param, "^([^ ]+) +([%d.-]+)[, ] *([%d.-]+)[, ] *([%d.-]+) *([%d.-]*)") + tree_id, p.x, p.y, p.z, seed = tonumber(tree_id), tonumber(x), tonumber(y), tonumber(z), tonumber(seed) if not tree_id or not p.x or not p.y or not p.z then return false, "Invalid usage, see /help lplant." end @@ -831,7 +839,7 @@ minetest.register_chatcommand("lplant", return false, "Cannot plant tree out of map bounds!" end - local success = ltool.plant_tree(tree_id, p) + local success = ltool.plant_tree(tree_id, p, seed) if success == false then return false, "Unknown tree ID!" else