diff options
Diffstat (limited to 'example')
-rw-r--r-- | example/scripts/crafting.lua | 176 | ||||
-rw-r--r-- | example/scripts/npcs/shaker.lua | 88 |
2 files changed, 132 insertions, 132 deletions
diff --git a/example/scripts/crafting.lua b/example/scripts/crafting.lua index afe1e1b5..1a7d0e54 100644 --- a/example/scripts/crafting.lua +++ b/example/scripts/crafting.lua @@ -1,88 +1,88 @@ ---[[
-
- This file provides an example of a simple crafting system.
-
---]]
-
--- a primitive example crafting system which cares about item order and exact amount
-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
- chr_inv_change(ch,
- 8, -2, --take away the iron
- 9, -1, --take away the wood
- 5, 1 ) -- give a sword
- chat_message(ch, "You've crafted a sword")
- return
- end
- 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.
-local function make_condensed_and_sorted_item_list(recipe)
- local condensed = {}
- 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 = {}
- item.id = id
- 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
- chr_inv_change(ch,
- 8, -2, -- take away the iron
- 9, -1, -- take away the wood
- 5, 1 ) -- give a sword
- chat_message(ch, "You've crafted a sword")
- return
- end
- 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 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
-
- chat_message(ch, "There is no crafting in this game world.")
- --craft_strict(ch, recipe)
- --craft_lax(ch, recipe)
-end
-
-on_craft(craft)
+--[[ + + This file provides an example of a simple crafting system. + +--]] + +-- a primitive example crafting system which cares about item order and exact amount +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 + chr_inv_change(ch, + 8, -2, --take away the iron + 9, -1, --take away the wood + 5, 1 ) -- give a sword + chat_message(ch, "You've crafted a sword") + return + end + 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. +local function make_condensed_and_sorted_item_list(recipe) + local condensed = {} + 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 = {} + item.id = id + 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 + chr_inv_change(ch, + 8, -2, -- take away the iron + 9, -1, -- take away the wood + 5, 1 ) -- give a sword + chat_message(ch, "You've crafted a sword") + return + end + 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 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 + + chat_message(ch, "There is no crafting in this game world.") + --craft_strict(ch, recipe) + --craft_lax(ch, recipe) +end + +on_craft(craft) diff --git a/example/scripts/npcs/shaker.lua b/example/scripts/npcs/shaker.lua index 768fc126..c6be0638 100644 --- a/example/scripts/npcs/shaker.lua +++ b/example/scripts/npcs/shaker.lua @@ -1,44 +1,44 @@ -----------------------------------------------------------------------------------
--- Copyright 2009-2010 The Mana World Development Team --
--- --
--- This file is part of The Mana World. --
--- --
--- The Mana World is free software; you can redistribute it and/or modify it --
--- under the terms of the GNU General Public License as published by the Free --
--- Software Foundation; either version 2 of the License, or any later version. --
-----------------------------------------------------------------------------------
-
-shake_count = 0
-
-function shaker_update(npc)
- shake_count = shake_count + 1
- if shake_count > 20 then
- shake_count = 0
-
- center_x = posX(npc)
- center_y = posY(npc)
- tremor(center_x, center_y, 300)
- end
-end
-
--- function which causes a screen shake effect for all players near a
--- certain point with an intensity and direction relative to said point
-function square(x)
- return x * x
-end
-
-function tremor (center_x, center_y, intensity)
- for dummy, object in ipairs(get_beings_in_circle(center_x, center_y, intensity)) do
- if being_type(object) == TYPE_CHARACTER then
- object_x = posX(object)
- object_y = posY(object)
- dist_x = object_x - center_x
- dist_y = object_y - center_y
- dist = math.sqrt(square(dist_x) + square(dist_y))
- intensity_local = intensity - dist
- intensity_x = (intensity - dist) * (dist_x / dist) / 5
- intensity_y = (intensity - dist) * (dist_y / dist) / 5
- chr_shake_screen(object, intensity_x, intensity_y)
- end
- end
-end
+---------------------------------------------------------------------------------- +-- Copyright 2009-2010 The Mana World Development Team -- +-- -- +-- This file is part of The Mana World. -- +-- -- +-- The Mana World is free software; you can redistribute it and/or modify it -- +-- under the terms of the GNU General Public License as published by the Free -- +-- Software Foundation; either version 2 of the License, or any later version. -- +---------------------------------------------------------------------------------- + +shake_count = 0 + +function shaker_update(npc) + shake_count = shake_count + 1 + if shake_count > 20 then + shake_count = 0 + + center_x = posX(npc) + center_y = posY(npc) + tremor(center_x, center_y, 300) + end +end + +-- function which causes a screen shake effect for all players near a +-- certain point with an intensity and direction relative to said point +function square(x) + return x * x +end + +function tremor (center_x, center_y, intensity) + for dummy, object in ipairs(get_beings_in_circle(center_x, center_y, intensity)) do + if being_type(object) == TYPE_CHARACTER then + object_x = posX(object) + object_y = posY(object) + dist_x = object_x - center_x + dist_y = object_y - center_y + dist = math.sqrt(square(dist_x) + square(dist_y)) + intensity_local = intensity - dist + intensity_x = (intensity - dist) * (dist_x / dist) / 5 + intensity_y = (intensity - dist) * (dist_y / dist) / 5 + chr_shake_screen(object, intensity_x, intensity_y) + end + end +end |