diff options
author | Philipp Sehmisch <mana@crushnet.org> | 2011-04-27 15:24:36 +0200 |
---|---|---|
committer | Philipp Sehmisch <mana@crushnet.org> | 2011-04-27 15:24:36 +0200 |
commit | c1a6e9947231cc511016378bd326a5d64b0aa18c (patch) | |
tree | 03c9b42ae97d61b5568f27ddc3f1be7a8b6648c3 /example/serverdata | |
parent | ca1dd6fcaf1dfaef0f18df0b6c114f1baa25d2ce (diff) | |
download | manaserv-c1a6e9947231cc511016378bd326a5d64b0aa18c.tar.gz manaserv-c1a6e9947231cc511016378bd326a5d64b0aa18c.tar.bz2 manaserv-c1a6e9947231cc511016378bd326a5d64b0aa18c.tar.xz manaserv-c1a6e9947231cc511016378bd326a5d64b0aa18c.zip |
Added a simple crafting system
A client can craft something using the @craft command.
The command needs a list of item names and amounts. The gameserver checks
if the character has these items in the inventory and then passes the list
together with the character handle to the lua script function on_craft in
the script file scripts/crafting.lua.
This function can then be used to evaluate if the list is a valid crafting
combination and when this is the case take or give items.
Implemented two example crafting scripts there, one which enforces exact
item order and amount and one which doesn't. Both are disabled per default
and one needs to be enabled by uncommenting a line.
Also gave the player group permission to use the @craft command in
permissions.xml and added two new items (wood and iron) required for the
example crafting combination.
Resolves: #333
Reviewed-by: bcs86, Bertram
Diffstat (limited to 'example/serverdata')
-rw-r--r-- | example/serverdata/permissions.xml | 1 | ||||
-rw-r--r-- | example/serverdata/scripts/crafting.lua | 99 |
2 files changed, 100 insertions, 0 deletions
diff --git a/example/serverdata/permissions.xml b/example/serverdata/permissions.xml index 91abd845..c07bfae2 100644 --- a/example/serverdata/permissions.xml +++ b/example/serverdata/permissions.xml @@ -6,6 +6,7 @@ <allow>@where</allow> <allow>@rights</allow> <allow>@report</allow> + <allow>@craft</allow> </class> <class level="2"> <alias>tester</alias> diff --git a/example/serverdata/scripts/crafting.lua b/example/serverdata/scripts/crafting.lua new file mode 100644 index 00000000..e8939823 --- /dev/null +++ b/example/serverdata/scripts/crafting.lua @@ -0,0 +1,99 @@ +-------------------------------------------------------------
+-- Example crafting script file --
+-- --
+-- This file allows you to implement your own crafting --
+-- system. --
+----------------------------------------------------------------------------------
+-- Copyright 2011 Manasource Development Team --
+-- --
+-- This file is part of Manasource. --
+-- --
+-- Manasource 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. --
+----------------------------------------------------------------------------------
+
+-- 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.chatmessage(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)
+ 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,
+ 8, -2, --take away the iron
+ 9, -1, --take away the wood
+ 5, 1 ) -- give a sword
+ return
+ mana.chatmessage(ch, "You've crafted a sword")
+ end
+ mana.chatmessage(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
+ return
+ mana.chatmessage(ch, "You've crafted a sword")
+ end
+ mana.chatmessage(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 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
\ No newline at end of file |