From fd6ecf70d91d76c1d949843c2686dd8d4adbb83e Mon Sep 17 00:00:00 2001 From: adrido Date: Sat, 13 Jun 2015 15:33:38 +0200 Subject: [PATCH] new registration system --- cannonballs.lua | 10 ++-- functions.lua | 121 +++++++++++++++++++++++++++++++++++++++++------- items.lua | 98 +++------------------------------------ 3 files changed, 117 insertions(+), 112 deletions(-) diff --git a/cannonballs.lua b/cannonballs.lua index 3aaab6a..c018bee 100644 --- a/cannonballs.lua +++ b/cannonballs.lua @@ -38,7 +38,7 @@ local exploding={ } if cannons.config:get("enable_explosion") == "true" then - cannons.register_muni("cannons:ball_exploding",exploding) + cannons.register_muni("cannons:ball_exploding_stack_1",exploding) end local fire={ physical = false, @@ -74,14 +74,14 @@ local fire={ } if cannons.config:get("enable_fire")=="true" then - cannons.register_muni("cannons:ball_fire",fire) + cannons.register_muni("cannons:ball_fire_stack_1",fire) end --++++++++++++++++++++++++++++++++++++ --+ Wooden Cannon ball + --++++++++++++++++++++++++++++++++++++ -cannons.register_muni("cannons:ball_wood",{ +cannons.register_muni("cannons:ball_wood_stack_1",{ physical = false, timer=0, textures = {"cannons_wood_bullet.png"}, @@ -131,7 +131,7 @@ cannons.register_muni("cannons:ball_wood",{ --+ Stone Cannon ball + --++++++++++++++++++++++++++++++++++++ -cannons.register_muni("cannons:ball_stone",{ +cannons.register_muni("cannons:ball_stone_stack_1",{ physical = false, timer=0, textures = {"cannons_bullet.png"}, @@ -181,7 +181,7 @@ cannons.register_muni("cannons:ball_stone",{ --+ Steel Cannon ball + --++++++++++++++++++++++++++++++++++++ -cannons.register_muni("cannons:ball_steel",{ +cannons.register_muni("cannons:ball_steel_stack_1",{ physical = false, timer=0, textures = {"cannons_bullet_iron.png"}, diff --git a/functions.lua b/functions.lua index 9403179..12750ab 100644 --- a/functions.lua +++ b/functions.lua @@ -30,8 +30,8 @@ end function cannons.inventory_modified(pos) local meta = minetest.get_meta(pos) local inv = meta:get_inventory() - local stack = inv:get_stack("muni", 1) - local muni = stack:to_table() + local muni = inv:get_stack("muni", 1):to_table() + local gunpowder = inv:get_stack("gunpowder", 1):to_table(); local addition = "" if meta:get_string("owner") ~="" then addition = " (owned by "..meta:get_string("owner")..")" @@ -41,8 +41,12 @@ function cannons.inventory_modified(pos) else muni = cannons.is_muni(muni.name) end + if gunpowder == nil then + gunpowder = false; + else + gunpowder = cannons.is_gunpowder(gunpowder.name) + end - local gunpowder = inv:contains_item("gunpowder","cannons:gunpowder 1") if not muni and not gunpowder then meta:set_string("infotext","Cannon has no muni and no gunpowder"..addition) @@ -65,7 +69,7 @@ cannons.allow_metadata_inventory_put = function(pos, listname, index, stack, pla end local inv = meta:get_inventory() stack = stack:to_table() - if listname == "gunpowder" and stack.name == "cannons:gunpowder" then + if listname == "gunpowder" and cannons.is_gunpowder(stack.name) then return stack.count elseif listname == "muni" and cannons.is_muni(stack.name) then return stack.count @@ -83,7 +87,7 @@ cannons.allow_metadata_inventory_move = function(pos, from_list, from_index, to_ local inv = meta:get_inventory() local stack = inv:get_stack(from_list, from_index) stack = stack:to_table() - if to_list == "gunpowder" and stack.name == "cannons:gunpowder" then + if to_list == "gunpowder" and cannons.is_gunpowder(stack.name) then return count elseif to_list == "muni" and cannons.is_muni(stack.name) then @@ -205,13 +209,16 @@ end function cannons.fire(pos,node,puncher) local meta = minetest.get_meta(pos) local inv = meta:get_inventory() - local stack = inv:get_stack("muni", 1) - local muni = stack:to_table() + local muni = inv:get_stack("muni", 1):to_table(); + local gunpowder = inv:get_stack("gunpowder", 1):to_table(); local dir = {} - if inv:contains_item("gunpowder","cannons:gunpowder 1") - and muni ~= nil + --print(muni ~= nil,cannons.is_muni(muni.name),inv:contains_item("muni",muni.name.." 1"),gunpowder ~= nil ,cannons.is_gunpowder(gunpowder.name),inv:contains_item("gunpowder",gunpowder.name.." 1")) + if muni ~= nil and cannons.is_muni(muni.name) - and inv:contains_item("muni",muni.name.." 1") + and inv:contains_item("muni",muni.name.." 1") + and gunpowder ~= nil + and cannons.is_gunpowder(gunpowder.name) + and inv:contains_item("gunpowder",gunpowder.name.." 1") then if puncher ~= nil then @@ -228,7 +235,7 @@ function cannons.fire(pos,node,puncher) inv:remove_item("muni", muni.name.." 1") - inv:remove_item("gunpowder", "cannons:gunpowder 1") + inv:remove_item("gunpowder", gunpowder.name.." 1") cannons.inventory_modified(pos) @@ -261,7 +268,7 @@ function cannons.punched(pos, node, puncher) end --++++++++++++++++++++++++++++++++++++ ---+ cannons.register_muni + +--+ cannons.register_muni + --++++++++++++++++++++++++++++++++++++ cannons.registered_muni = {} @@ -290,7 +297,7 @@ function cannons.register_muni(node,entity) self.on_player_hit(self,pos,player) end end - elseif node.name ~=air then + elseif node.name ~= "air" then self.on_node_hit(self,pos,node) end self.lastpos={x=pos.x, y=pos.y, z=pos.z} @@ -301,14 +308,96 @@ function cannons.register_muni(node,entity) end function cannons.is_muni(node) - return cannons.registered_muni[node] ~= nil + return cannons.registered_muni[node] ~= nil end function cannons.get_entity(node) - return cannons.registered_muni[node].obj + return cannons.registered_muni[node].obj end function cannons.get_settings(node) - return cannons.registered_muni[node].entity + return cannons.registered_muni[node].entity end + +--++++++++++++++++++++++++++++++++++++ +--+ cannons.register_gunpowder + +--++++++++++++++++++++++++++++++++++++ +cannons.registered_gunpowder = {} +function cannons.register_gunpowder(node) + cannons.registered_gunpowder[node] = true; +end + +function cannons.is_gunpowder(node) + return cannons.registered_gunpowder[node] ~= nil +end + + +--++++++++++++++++++++++++++++++++++++ +--+ cannons ball stack + +--++++++++++++++++++++++++++++++++++++ +function cannons.on_ball_punch(pos, node, puncher, pointed_thing) + if not puncher or not puncher:is_player() then + return + end + local nodearr = string.split(node.name,"_stack_"); + local item = nodearr[1]; + local level = tonumber(nodearr[2]); + puncher:get_inventory():add_item('main', item) + if level > 1 then + node.name = item.."_stack_"..level-1 + minetest.swap_node(pos,node); + else + minetest.remove_node(pos); + end +end + +function cannons.on_ball_rightclick(pos, node, player, itemstack, pointed_thing) + if not player or not player:is_player() then + return + end + local basename = string.split(itemstack:get_name(),"_stack_")[1]; + local nodearr = string.split(node.name,"_stack_"); + local item = nodearr[1]; + local level = tonumber(nodearr[2]); + if basename == item then + if level < 5 then + itemstack:take_item(1) + node.name = item.."_stack_"..level+1 + minetest.swap_node(pos,node); + return itemstack; + else + return itemstack + end + end + +end + +function cannons.generate_and_register_ball_node(name,nodedef) + minetest.register_alias(name, name.."_stack_1"); + nodedef.drawtype = "nodebox"; + nodedef.selection_box = nil; + nodedef.on_punch = cannons.on_ball_punch; + nodedef.on_rightclick = cannons.on_ball_rightclick; + local nodebox = { + type = "fixed", + fixed = {}, + }; --initialize empty nodebox + + for number = 1, 5, 1 do + nodebox.fixed[number] = cannons.nodeboxes.ball_stack.fixed[number];--copy a part to nodebox + nodedef.node_box = nodebox;--add nodebox to nodedef + nodedef.selection_box = table.copy(nodebox); + nodedef["drop"] = name.."_stack_1 "..number;--set drop + nodedef.name = name.."_stack_"..number;--set name + + minetest.register_node(nodedef.name, table.copy(nodedef))--register node + end + --register craft, to allow craft 5-stacks + minetest.register_craft({ + type = "shapeless", + output = name.."_stack_5", + recipe = { name,name,name,name,name}, + }) +end + --++++++++++++++++++++++++++++++++++++ --+ mesecons stuff + --++++++++++++++++++++++++++++++++++++ diff --git a/items.lua b/items.lua index eee8107..e4def00 100644 --- a/items.lua +++ b/items.lua @@ -7,6 +7,7 @@ minetest.register_craftitem("cannons:gunpowder", { description = "Gunpowder", inventory_image = "cannons_gunpowder.png" }) +cannons.register_gunpowder("cannons:gunpowder"); minetest.register_craftitem("cannons:salt", { description = "Salt", @@ -89,29 +90,7 @@ minetest.register_craft({ }, }) -minetest.register_craft({ - type = "shapeless", - output = 'cannons:ball_steel_stack', - recipe = { - "cannons:ball_steel", "cannons:ball_steel", "cannons:ball_steel", "cannons:ball_steel" - }, -}) -minetest.register_craft({ - type = "shapeless", - output = 'cannons:ball_stone_stack', - recipe = { - "cannons:ball_stone", "cannons:ball_stone", "cannons:ball_stone", "cannons:ball_stone" - }, -}) - -minetest.register_craft({ - type = "shapeless", - output = 'cannons:ball_wood_stack', - recipe = { - "cannons:ball_wood", "cannons:ball_wood", "cannons:ball_wood", "cannons:ball_wood" - }, -}) minetest.register_craft({ output = 'cannons:ball_wood 5', @@ -354,7 +333,7 @@ minetest.register_node("cannons:wood_stand_with_cannon_bronze", { --++++++++++++++++++++++++++++++++++++ --wood ball -minetest.register_node("cannons:ball_wood", { +cannons.generate_and_register_ball_node("cannons:ball_wood", { description = "Cannon Ball Wood", stack_max = 99, tiles = {"default_wood.png"}, @@ -367,7 +346,7 @@ minetest.register_node("cannons:ball_wood", { }) --stone ball -minetest.register_node("cannons:ball_stone", { +cannons.generate_and_register_ball_node("cannons:ball_stone", { description = "Cannon Ball Stone", stack_max = 99, tiles = {"default_stone.png"}, @@ -380,7 +359,7 @@ minetest.register_node("cannons:ball_stone", { }) --steel ball -minetest.register_node("cannons:ball_steel", { +cannons.generate_and_register_ball_node("cannons:ball_steel", { description = "Cannon Ball Steel", stack_max = 99, tiles = {"cannons_steel_top.png"}, @@ -388,13 +367,13 @@ minetest.register_node("cannons:ball_steel", { paramtype = "light", paramtype2 = "facedir", groups = {cracky=2}, + --diggable = false, sounds = cannons.sound_defaults(), - node_box = cannons.nodeboxes.ball, }) --explosion cannon ball if cannons.config:get("enable_explosion") == "true" then -minetest.register_node("cannons:ball_exploding", { +cannons.generate_and_register_ball_node("cannons:ball_exploding", { description = "Exploding Cannon Ball", stack_max = 99, tiles = {"default_mese_block.png"}, @@ -403,13 +382,12 @@ minetest.register_node("cannons:ball_exploding", { paramtype2 = "facedir", groups = {cracky=2}, sounds = default.node_sound_wood_defaults(), - node_box = cannons.nodeboxes.ball, }) end --fire cannon ball if cannons.config:get("enable_fire") == "true" then -minetest.register_node("cannons:ball_fire", { +cannons.generate_and_register_ball_node("cannons:ball_fire", { description = "Burning Cannon Ball", stack_max = 99, tiles = {"default_tree.png"}, @@ -422,65 +400,3 @@ minetest.register_node("cannons:ball_fire", { }) end ---ball wood stack -minetest.register_node("cannons:ball_wood_stack", { - description = "Cannon Ball Wood Stack", - stack_max = 99, - tiles = {"default_wood.png"}, - drawtype = "nodebox", - drop = 'cannons:ball_wood 4', - paramtype = "light", - paramtype2 = "facedir", - groups = {cracky=2}, - sounds = default.node_sound_wood_defaults(), - node_box = cannons.nodeboxes.ball_stack, - selection_box = { - type = "fixed", - fixed = { - {-0.4, -0.5, -0.5, 0.5, 0.3, 0.5}, - }, - }, -}) - ---ball stone stack -minetest.register_node("cannons:ball_stone_stack", { - description = "Cannon Ball Stone Stack", - stack_max = 99, - tiles = {"default_stone.png"}, - drawtype = "nodebox", - drop = 'cannons:ball_stone 4', - paramtype = "light", - paramtype2 = "facedir", - groups = {cracky=2}, - sounds = default.node_sound_stone_defaults(), - node_box = cannons.nodeboxes.ball_stack, - selection_box = { - type = "fixed", - fixed = { - {-0.4, -0.5, -0.5, 0.5, 0.3, 0.5}, - }, - }, -}) - ---ball steel stack -minetest.register_node("cannons:ball_steel_stack", { - description = "Cannon Ball Steel Stack", - stack_max = 99, - tiles = {"cannons_steel_top.png"}, - drawtype = "nodebox", - drop = 'cannons:ball_steel 4', - paramtype = "light", - paramtype2 = "facedir", - groups = {cracky=2}, - sounds = cannons.sound_defaults(), - node_box = cannons.nodeboxes.ball_stack, - selection_box = { - type = "fixed", - fixed = { - {-0.4, -0.5, -0.5, 0.5, 0.3, 0.5}, - }, - }, -}) -minetest.register_alias("cannons:canon_ball_steel_stack", "cannons:ball_steel_stack") - -