Set version to 0.4.dev-20111202-1
[oweals/minetest.git] / data / builtin.lua
index 6fd19404e16be81e40fd704be11f866b1eec8abf..a9412a97d0a78a432ada4406f9319abf68df8129 100644 (file)
@@ -1,3 +1,10 @@
+--
+-- This file contains built-in stuff in Minetest implemented in Lua.
+--
+-- It is always loaded and executed after registration of the C API,
+-- before loading and running any mods.
+--
+
 function basic_dump2(o)
        if type(o) == "number" then
                return tostring(o)
@@ -147,3 +154,206 @@ minetest.register_node("ignore", {
        air_equivalent = true,
 })
 
+--
+-- stackstring manipulation functions
+-- example stackstring: 'craft "apple" 4'
+-- example item: {type="craft", name="apple"}
+-- example item: {type="tool", name="SteelPick", wear="23272"}
+--
+
+function stackstring_take_item(stackstring)
+       if stackstring == nil then
+               return '', nil
+       end
+       local stacktype = nil
+       stacktype = string.match(stackstring,
+                       '([%a%d]+)')
+       if stacktype == "node" or stacktype == "craft" then
+               local itemtype = nil
+               local itemname = nil
+               local itemcount = nil
+               itemtype, itemname, itemcount = string.match(stackstring,
+                               '([%a%d]+) "([^"]*)" (%d+)')
+               itemcount = tonumber(itemcount)
+               if itemcount == 0 then
+                       return '', nil
+               elseif itemcount == 1 then
+                       return '', {type=itemtype, name=itemname}
+               else
+                       return itemtype.." \""..itemname.."\" "..(itemcount-1),
+                                       {type=itemtype, name=itemname}
+               end
+       elseif stacktype == "tool" then
+               local itemtype = nil
+               local itemname = nil
+               local itemwear = nil
+               itemtype, itemname, itemwear = string.match(stackstring,
+                               '([%a%d]+) "([^"]*)" (%d+)')
+               itemwear = tonumber(itemwear)
+               return '', {type=itemtype, name=itemname, wear=itemwear}
+       end
+end
+
+function stackstring_put_item(stackstring, item)
+       if item == nil then
+               return stackstring, false
+       end
+       stackstring = stackstring or ''
+       local stacktype = nil
+       stacktype = string.match(stackstring,
+                       '([%a%d]+)')
+       stacktype = stacktype or ''
+       if stacktype ~= '' and stacktype ~= item.type then
+               return stackstring, false
+       end
+       if item.type == "node" or item.type == "craft" then
+               local itemtype = nil
+               local itemname = nil
+               local itemcount = nil
+               itemtype, itemname, itemcount = string.match(stackstring,
+                               '([%a%d]+) "([^"]*)" (%d+)')
+               itemtype = itemtype or item.type
+               itemname = itemname or item.name
+               if itemcount == nil then
+                       itemcount = 0
+               end
+               itemcount = itemcount + 1
+               return itemtype.." \""..itemname.."\" "..itemcount, true
+       elseif item.type == "tool" then
+               if stacktype ~= nil then
+                       return stackstring, false
+               end
+               local itemtype = nil
+               local itemname = nil
+               local itemwear = nil
+               itemtype, itemname, itemwear = string.match(stackstring,
+                               '([%a%d]+) "([^"]*)" (%d+)')
+               itemwear = tonumber(itemwear)
+               return itemtype.." \""..itemname.."\" "..itemwear, true
+       end
+       return stackstring, false
+end
+
+function stackstring_put_stackstring(stackstring, src)
+       while src ~= '' do
+               --print("src="..dump(src))
+               src, item = stackstring_take_item(src)
+               --print("src="..dump(src).." item="..dump(item))
+               local success
+               stackstring, success = stackstring_put_item(stackstring, item)
+               if not success then
+                       return stackstring, false
+               end
+       end
+       return stackstring, true
+end
+
+function test_stackstring()
+       local stack
+       local item
+       local success
+
+       stack, item = stackstring_take_item('node "TNT" 3')
+       assert(stack == 'node "TNT" 2')
+       assert(item.type == 'node')
+       assert(item.name == 'TNT')
+
+       stack, item = stackstring_take_item('craft "with spaces" 2')
+       assert(stack == 'craft "with spaces" 1')
+       assert(item.type == 'craft')
+       assert(item.name == 'with spaces')
+
+       stack, item = stackstring_take_item('craft "with spaces" 1')
+       assert(stack == '')
+       assert(item.type == 'craft')
+       assert(item.name == 'with spaces')
+
+       stack, item = stackstring_take_item('craft "s8df2kj3" 0')
+       assert(stack == '')
+       assert(item == nil)
+
+       stack, item = stackstring_take_item('tool "With Spaces" 32487')
+       assert(stack == '')
+       assert(item.type == 'tool')
+       assert(item.name == 'With Spaces')
+       assert(item.wear == 32487)
+
+       stack, success = stackstring_put_item('node "With Spaces" 40',
+                       {type='node', name='With Spaces'})
+       assert(stack == 'node "With Spaces" 41')
+       assert(success == true)
+
+       stack, success = stackstring_put_item('craft "With Spaces" 40',
+                       {type='craft', name='With Spaces'})
+       assert(stack == 'craft "With Spaces" 41')
+       assert(success == true)
+
+       stack, success = stackstring_put_item('tool "With Spaces" 32487',
+                       {type='tool', name='With Spaces'})
+       assert(stack == 'tool "With Spaces" 32487')
+       assert(success == false)
+
+       stack, success = stackstring_put_item('node "With Spaces" 40',
+                       {type='tool', name='With Spaces'})
+       assert(stack == 'node "With Spaces" 40')
+       assert(success == false)
+       
+       assert(stackstring_put_stackstring('node "With Spaces" 2',
+                       'node "With Spaces" 1') == 'node "With Spaces" 3')
+end
+test_stackstring()
+
+--
+-- craftitem helpers
+--
+
+minetest.craftitem_place_item = function(item, placer, pos)
+       --print("craftitem_place_item")
+       --print("item: " .. dump(item))
+       --print("placer: " .. dump(placer))
+       --print("pos: " .. dump(pos))
+       minetest.env:add_item(pos, 'craft "' .. item .. '" 1')
+       return true
+end
+
+minetest.craftitem_eat = function(hp_change)
+       return function(item, user, pointed_thing)  -- closure
+               --print("craftitem_eat(" .. hp_change .. ")")
+               --print("item: " .. dump(item))
+               --print("user: " .. dump(user))
+               --print("pointed_thing: " .. dump(pointed_thing))
+               user:set_hp(user:get_hp() + hp_change)
+               return true
+       end
+end
+
+--
+-- Creative inventory
+--
+
+minetest.creative_inventory = {}
+
+minetest.add_to_creative_inventory = function(itemstring)
+       table.insert(minetest.creative_inventory, itemstring)
+end
+
+--
+-- Callback registration
+--
+
+function make_registration()
+       local t = {}
+       local registerfunc = function(func) table.insert(t, func) end
+       return t, registerfunc
+end
+
+minetest.registered_on_chat_messages, minetest.register_on_chat_message = make_registration()
+minetest.registered_globalsteps, minetest.register_globalstep = make_registration()
+minetest.registered_on_placenodes, minetest.register_on_placenode = make_registration()
+minetest.registered_on_dignodes, minetest.register_on_dignode = make_registration()
+minetest.registered_on_punchnodes, minetest.register_on_punchnode = make_registration()
+minetest.registered_on_generateds, minetest.register_on_generated = make_registration()
+minetest.registered_on_newplayers, minetest.register_on_newplayer = make_registration()
+minetest.registered_on_respawnplayers, minetest.register_on_respawnplayer = make_registration()
+
+-- END