From b2fde0cf32348355f7725c6a7b0a523e0958c1a4 Mon Sep 17 00:00:00 2001 From: Thorbjørn Lindeijer Date: Sat, 3 Mar 2012 20:34:57 +0100 Subject: Made some global Lua variables local We have to be careful with introducing globals now that there is only a single Lua state, so we shouldn't use globals unnecessarily. Any variable should be declared 'local' unless there is a reason to make it global. For additional safety we can also think about disallowing the use of globals entirely. This also helps to catch typos in scripts. Reviewed-by: Erik Schilling --- example/scripts/crafting.lua | 89 ++++++++++++++++++++++---------------------- 1 file changed, 44 insertions(+), 45 deletions(-) (limited to 'example/scripts/crafting.lua') diff --git a/example/scripts/crafting.lua b/example/scripts/crafting.lua index 2200081e..db8c76dd 100644 --- a/example/scripts/crafting.lua +++ b/example/scripts/crafting.lua @@ -4,32 +4,12 @@ --]] --- This function is called by the game engine when a character tries to craft --- something from items in its inventory -function on_craft(ch, recipe) - -- ch is the crafting character - -- - -- recipe is a table with the ingredients. - -- it is a common 1-based array. each element of this array is a table with - -- the two keys "id" and "amount". - -- The engine has already checked that the character owns enough of those - -- things, so you needn't do this again. - - -- uncomment one (but not both!) of the following three lines to enable the - -- example crafting systems - - mana.chat_message(ch, "There is no crafting in this game world.") - --craft_strict(ch, recipe) - --craft_lax(ch, recipe) -end - - -- a primitive example crafting system which cares about item order and exact amount -function craft_strict(ch, recipe) +local function craft_strict(ch, recipe) if (recipe[1].id == 8 and recipe[1].amount == 2 and -- has two iron recipe[2].id == 9 and recipe[2].amount == 1) -- and one wood then - mana.chr_inv_change(ch, + mana.chr_inv_change(ch, 8, -2, --take away the iron 9, -1, --take away the wood 5, 1 ) -- give a sword @@ -39,38 +19,20 @@ function craft_strict(ch, recipe) mana.chat_message(ch, "This wouldn't create anything useful") end --- a primitive example crafting system which doesn't care about item order --- and amount. It even allows to mention the same item multiple times. -function craft_lax(ch, recipe) - recipe = make_condensed_and_sorted_item_list(recipe) - - if (recipe[1].id == 8 and recipe[1].amount >= 2 and -- has at least two iron - recipe[2].id == 9 and recipe[2].amount >= 1) -- and at least one wood - then - mana.chr_inv_change(ch, - 8, -2, -- take away the iron - 9, -1, -- take away the wood - 5, 1 ) -- give a sword - mana.chat_message(ch, "You've crafted a sword") - return - end - mana.chat_message(ch, "This wouldn't create anything useful") -end - -- this turns multiple occurences of the same item into one by adding up -- their amounts and sorts the recipe by item ID. -- This makes stuff a lot easier when your crafting system isn't supposed to -- care about the order items are in. -function make_condensed_and_sorted_item_list(recipe) +local function make_condensed_and_sorted_item_list(recipe) local condensed = {} - for index, item in pairs(recipe) do + for index, item in pairs(recipe) do if condensed[item.id] == nil then condensed[item.id] = item.amount else condensed[item.id] = condensed[item.id] + item.amount end end - + local sorted = {} for id, amount in pairs(condensed) do local item = {} @@ -78,12 +40,49 @@ function make_condensed_and_sorted_item_list(recipe) item.amount = amount table.insert(sorted, item) end - + table.sort(sorted, function(item1, item2) return (item1.id < item2.id) end) - + return sorted end +-- a primitive example crafting system which doesn't care about item order +-- and amount. It even allows to mention the same item multiple times. +local function craft_lax(ch, recipe) + recipe = make_condensed_and_sorted_item_list(recipe) + + if (recipe[1].id == 8 and recipe[1].amount >= 2 and -- has at least two iron + recipe[2].id == 9 and recipe[2].amount >= 1) -- and at least one wood + then + mana.chr_inv_change(ch, + 8, -2, -- take away the iron + 9, -1, -- take away the wood + 5, 1 ) -- give a sword + mana.chat_message(ch, "You've crafted a sword") + return + end + mana.chat_message(ch, "This wouldn't create anything useful") +end + +-- This function is registered with the game engine to use when a character +-- tries to craft something from items in its inventory. +local function on_craft(ch, recipe) + -- ch is the crafting character + -- + -- recipe is a table with the ingredients. + -- it is a common 1-based array. each element of this array is a table with + -- the two keys "id" and "amount". + -- The engine has already checked that the character owns enough of those + -- things, so you needn't do this again. + + -- uncomment one (but not both!) of the following three lines to enable the + -- example crafting systems + + mana.chat_message(ch, "There is no crafting in this game world.") + --craft_strict(ch, recipe) + --craft_lax(ch, recipe) +end + mana.on_craft(on_craft) -- cgit v1.2.3-70-g09d2