summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThorbjørn Lindeijer <thorbjorn@lindeijer.nl>2012-03-03 20:34:57 +0100
committerThorbjørn Lindeijer <thorbjorn@lindeijer.nl>2012-03-03 21:24:45 +0100
commitb2fde0cf32348355f7725c6a7b0a523e0958c1a4 (patch)
treef12ab2fff979db589bc5a4a6de6fc7e8b4d67313
parent84c87cc99be29a694f0ffe83ab7a06ae433bb0cd (diff)
downloadmanaserv-b2fde0cf32348355f7725c6a7b0a523e0958c1a4.tar.gz
manaserv-b2fde0cf32348355f7725c6a7b0a523e0958c1a4.tar.bz2
manaserv-b2fde0cf32348355f7725c6a7b0a523e0958c1a4.tar.xz
manaserv-b2fde0cf32348355f7725c6a7b0a523e0958c1a4.zip
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
-rw-r--r--example/scripts/crafting.lua89
-rw-r--r--example/scripts/special_actions.lua4
-rw-r--r--scripts/lua/libmana.lua93
3 files changed, 93 insertions, 93 deletions
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)
diff --git a/example/scripts/special_actions.lua b/example/scripts/special_actions.lua
index 711478f2..7e42ad43 100644
--- a/example/scripts/special_actions.lua
+++ b/example/scripts/special_actions.lua
@@ -13,7 +13,7 @@ specialCost[1] = 50
specialCost[2] = 250
specialCost[3] = 1000
-function use_special(ch, id)
+local function use_special(ch, id)
-- perform whatever the special with the ID does
if id == 1 then
mana.being_say(ch, "Kaaame...Haaame... HAAAAAA!")
@@ -26,7 +26,7 @@ function use_special(ch, id)
end
end
-function get_special_recharge_cost(id)
+local function get_special_recharge_cost(id)
-- return the recharge cost for the special with the ID
return specialCost[id]
end
diff --git a/scripts/lua/libmana.lua b/scripts/lua/libmana.lua
index ec68fae0..cb92276b 100644
--- a/scripts/lua/libmana.lua
+++ b/scripts/lua/libmana.lua
@@ -171,9 +171,9 @@ local function process_npc(w, ...)
return true
end
--- Called by the game whenever a player starts talking to an NPC.
--- Creates a coroutine based on the registered NPC handler.
-function npc_start(npc, ch)
+-- Registered as the function to call whenever a player starts talking to an
+-- NPC. Creates a coroutine based on the registered NPC handler.
+local function npc_start(npc, ch)
states[ch] = nil
local h = npc_talk_functs[npc]
if not h then return end
@@ -192,9 +192,9 @@ function do_npc_close(npc, ch)
mana.npc_end(npc, ch)
end
--- Called by the game whenever a player keeps talking to an NPC.
--- Checks that the NPC expects it, and processes the respective coroutine.
-function npc_next(npc, ch)
+-- Registered as the function to call whenever a player continues talking to an
+-- NPC. Checks that the NPC expects it, and processes the respective coroutine.
+local function npc_next(npc, ch)
local w = states[ch]
if w then
local w3 = w[3]
@@ -208,23 +208,24 @@ function npc_next(npc, ch)
states[ch] = nil
end
--- Called by the game whenever a player selects a particular reply.
--- Checks that the NPC expects it, and processes the respective coroutine.
-function npc_choose(npc, ch, u)
+-- Registered as the function to call whenever a player selects a particular
+-- reply. Checks that the NPC expects it, and processes the respective
+-- coroutine.
+local function npc_choose(npc, ch, u)
local w = states[ch]
if not (w and w[1] == npc and w[3] == 2 and process_npc(w, u)) then
states[ch] = nil
end
end
-function npc_integer(npc, ch, u)
+local function npc_integer(npc, ch, u)
local w = states[ch]
if not (w and w[1] == npc and w[3] == 2 and process_npc(w, u)) then
states[ch] = nil
end
end
-function npc_string(npc, ch, u)
+local function npc_string(npc, ch, u)
local w = states[ch]
if not (w and w[1] == npc and w[3] == 2 and process_npc(w, u)) then
states[ch] = nil
@@ -232,7 +233,8 @@ function npc_string(npc, ch, u)
end
-- Called by the game when a player sends a letter.
-function npc_post(npc, ch, sender, letter)
+-- TODO: Actually this function isn't called, probably unfinished implementation
+local function npc_post(npc, ch, sender, letter)
local w = states[ch]
if not (w and w[1] == npc and w[3] == 1 and process_npc(w, sender, letter)) then
states[ch] = nil
@@ -272,16 +274,40 @@ local function npc_post_reply(ch, sender, letter)
states[ch] = nil
end
--- Called by the game every tick for each NPC.
-function npc_update(npc)
+-- Registered as the function to call every tick for each NPC.
+local function npc_update(npc)
local h = npc_update_functs[npc];
if h then h(npc) end;
end
--- Called by the game every tick.
--- Checks for scheduled function calls
--- Cleans obsolete connections.
-function update()
+-- Table of scheduled jobs. A job is an array with 3 elements:
+-- 0: the UNIX timestamp when it is executed
+-- 1: the function which is executed
+-- 2: nil when it is a one-time job. Repetition interval is seconds when it is
+-- a repeated job.
+local scheduler_jobs = {}
+
+-- checks for jobs which have to be executed, executes them and reschedules
+-- them when they are repeated jobs.
+local function check_schedule()
+ local current_time = os.time()
+
+ while #scheduler_jobs~=0 and current_time > scheduler_jobs[#scheduler_jobs][0] do
+ -- retreive the job and remove it from the schedule
+ job = scheduler_jobs[#scheduler_jobs]
+ table.remove(scheduler_jobs)
+ -- reschedule the job when it is a repeated job
+ if job[2] then
+ schedule_every(job[2], job[1])
+ end
+ -- execute the job
+ job[1]()
+ end
+end
+
+-- Registered as the function to call every tick.
+-- Checks for scheduled function calls and cleans obsolete connections.
+local function update()
-- check the scheduler
check_schedule()
@@ -314,7 +340,7 @@ end
-- Called by the game for creating NPCs embedded into maps.
-- Delays the creation until map initialization is performed.
-- Note: Assumes that the "npc_handler" global field contains the NPC handler.
-function create_npc_delayed(name, id, x, y)
+local function create_npc_delayed(name, id, x, y)
-- Bind the name to a local variable first, as it will be reused.
local h = npc_handler
atinit(function() create_npc(name, id, x, y, h, nil) end)
@@ -323,7 +349,7 @@ end
-- Called during map initialization, for each map.
-- Executes all the functions registered by atinit.
-function initialize()
+local function map_initialize()
for i,f in ipairs(init_fun) do
f()
end
@@ -333,37 +359,12 @@ end
-- SCHEDULER
--- Table of scheduled jobs. A job is an array with 3 elements:
--- 0: the UNIX timestamp when it is executed
--- 1: the function which is executed
--- 2: nil when it is a one-time job. Repetition interval is seconds when it is
--- a repeated job.
-local scheduler_jobs = {}
-
-- compare function used to sort the scheduler_jobs table.
-- the jobs which come first are at the end of the table.
local function job_cmp(job1, job2)
return (job1[0] > job2[0])
end
--- checks for jobs which have to be executed, executes them and reschedules
--- them when they are repeated jobs.
-function check_schedule()
- local current_time = os.time()
-
- while #scheduler_jobs~=0 and current_time > scheduler_jobs[#scheduler_jobs][0] do
- -- retreive the job and remove it from the schedule
- job = scheduler_jobs[#scheduler_jobs]
- table.remove(scheduler_jobs)
- -- reschedule the job when it is a repeated job
- if job[2] then
- schedule_every(job[2], job[1])
- end
- -- execute the job
- job[1]()
- end
-end
-
-- schedules a function call to be executed once in n seconds
function schedule_in(seconds, funct)
local job = {}
@@ -467,7 +468,7 @@ mana.on_npc_post_reply(npc_post_reply)
mana.on_npc_update(npc_update)
mana.on_create_npc_delayed(create_npc_delayed)
-mana.on_map_initialize(initialize)
+mana.on_map_initialize(map_initialize)
mana.on_being_death(death_notification)
mana.on_being_remove(remove_notification)