From 589eb9dc3d02f9961dd4eb8d8d10582b7430e714 Mon Sep 17 00:00:00 2001 From: Wuzzy Date: Mon, 13 Mar 2017 22:30:37 +0100 Subject: [PATCH] Farming: Add gourd API to reduce redundancy, rework pumpkin --- mods/ITEMS/mcl_farming/gourd.lua | 160 ++++++++++++ mods/ITEMS/mcl_farming/init.lua | 3 + mods/ITEMS/mcl_farming/pumpkin.lua | 235 ++---------------- ..._connnect.png => farming_tige_connect.png} | Bin 4 files changed, 185 insertions(+), 213 deletions(-) create mode 100644 mods/ITEMS/mcl_farming/gourd.lua rename mods/ITEMS/mcl_farming/textures/{farming_tige_connnect.png => farming_tige_connect.png} (100%) diff --git a/mods/ITEMS/mcl_farming/gourd.lua b/mods/ITEMS/mcl_farming/gourd.lua new file mode 100644 index 00000000..af89a048 --- /dev/null +++ b/mods/ITEMS/mcl_farming/gourd.lua @@ -0,0 +1,160 @@ +--[[ Helper function to create a gourd (e.g. melon, pumpkin), the connected stem nodes as + +- full_unconnected_stem: itemstring of the full-grown but unconnceted stem node. This node must already be done +- connected_stem_basename: prefix of the itemstrings used for the 4 connected stem nodes to create +- stemdrop: Drop probability table for all stem +- gourd_itemstring: Desired itemstring of the full gourd node +- gourd_def: (almost) full definition of the gourd node, except for +]] + +function mcl_farming.register_gourd(full_unconnected_stem, connected_stem_basename, stemdrop, gourd_itemstring, gourd_def) + + local connected_stem_names = { + connected_stem_basename .. "_r", + connected_stem_basename .. "_l", + connected_stem_basename .. "_t", + connected_stem_basename .. "_b", + } + + -- Register gourd + if not gourd_def.after_dig_node then + gourd_def.after_dig_node = function(blockpos, oldnode, oldmetadata, user) + -- Disconnect any connected stems, turning them back to normal stems + local neighbors = { + { x=-1, y=0, z=0 }, + { x=1, y=0, z=0 }, + { x=0, y=0, z=-1 }, + { x=0, y=0, z=1 }, + } + for n=1, #neighbors do + local offset = neighbors[n] + local expected_stem = connected_stem_names[n] + local stempos = vector.add(blockpos, offset) + local stem = minetest.get_node(stempos) + if stem.name == expected_stem then + minetest.add_node(stempos, {name=full_unconnected_stem}) + end + end + end + end + minetest.register_node(gourd_itemstring, gourd_def) + + -- Register connected stems + + local connected_stem_tiles = { + { "blank.png", --top + "blank.png", -- bottom + "blank.png", -- right + "blank.png", -- left + "farming_tige_connect.png", -- back + "farming_tige_connect.png^[transformFX90" --front + }, + { "blank.png", --top + "blank.png", -- bottom + "blank.png", -- right + "blank.png", -- left + "farming_tige_connect.png^[transformFX90", --back + "farming_tige_connect.png", -- front + }, + { "blank.png", --top + "blank.png", -- bottom + "farming_tige_connect.png^[transformFX90", -- right + "farming_tige_connect.png", -- left + "blank.png", --back + "blank.png", -- front + }, + { "blank.png", --top + "blank.png", -- bottom + "farming_tige_connect.png", -- right + "farming_tige_connect.png^[transformFX90", -- left + "blank.png", --back + "blank.png", -- front + } + } + local connected_stem_nodebox = { + {-0.5, -0.5, 0, 0.5, 0.5, 0}, + {-0.5, -0.5, 0, 0.5, 0.5, 0}, + {0, -0.5, -0.5, 0, 0.5, 0.5}, + {0, -0.5, -0.5, 0, 0.5, 0.5}, + } + + for i=1, 4 do + minetest.register_node(connected_stem_names[i], { + _doc_items_create_entry = false, + paramtype = "light", + sunlight_propagates = true, + walkable = false, + drop = stemdrop, + drawtype = "nodebox", + paramtype = "light", + node_box = { + type = "fixed", + fixed = connected_stem_nodebox[i] + }, + selection_box = { + type = "fixed", + fixed = {-0.2, -0.5, -0.2, 0.2, 0.2, 0.2} + }, + tiles = connected_stem_tiles[i], + groups = {dig_immediate=3, not_in_creative_inventory=1 ,dig_by_water=1}, + sounds = mcl_sounds.node_sound_leaves_defaults(), + _mcl_blast_resistance = 0, + }) + end + + minetest.register_abm({ + label = "Grow gourd stem to gourd ("..full_unconnected_stem.." → "..gourd_itemstring..")", + nodenames = {full_unconnected_stem}, + neighbors = {"air"}, + -- FIXME: Times + interval = 1, + chance = 1, + action = function(stempos) + local light = minetest.get_node_light(stempos) + if light and light > 10 then + -- Check the four neighbors and filter out neighbors where gourds can't grow + local neighbors = { + { x=-1, y=0, z=0 }, + { x=1, y=0, z=0 }, + { x=0, y=0, z=-1 }, + { x=0, y=0, z=1 }, + } + for n=#neighbors, 1, -1 do + local offset = neighbors[n] + local blockpos = vector.add(stempos, offset) + local floorpos = { x=blockpos.x, y=blockpos.y-1, z=blockpos.z } + local floor = minetest.get_node(floorpos) + local block = minetest.get_node(blockpos) + local soilgroup = minetest.get_item_group(floor.name, "soil") + if not ((floor.name=="mcl_core:dirt_with_grass" or floor.name=="mcl_core:dirt" or soilgroup == 2 or soilgroup == 3) and block.name == "air") then + table.remove(neighbors, n) + end + end + + -- Pumpkins need at least 1 free neighbor to grow + if #neighbors > 0 then + -- From the remaining neighbors, grow randomly + local r = math.random(1, #neighbors) + local offset = neighbors[r] + local blockpos = vector.add(stempos, offset) + local p2 + if offset.x == 1 then + minetest.set_node(stempos, {name=connected_stem_names[1]}) + p2 = 3 + elseif offset.x == -1 then + minetest.set_node(stempos, {name=connected_stem_names[2]}) + p2 = 1 + elseif offset.z == 1 then + minetest.set_node(stempos, {name=connected_stem_names[3]}) + p2 = 2 + elseif offset.z == -1 then + minetest.set_node(stempos, {name=connected_stem_names[4]}) + p2 = 0 + end + minetest.add_node(blockpos, {name=gourd_itemstring, param2=p2}) + end + end + end, + }) +end + diff --git a/mods/ITEMS/mcl_farming/init.lua b/mods/ITEMS/mcl_farming/init.lua index 0857c77b..dbb5db19 100644 --- a/mods/ITEMS/mcl_farming/init.lua +++ b/mods/ITEMS/mcl_farming/init.lua @@ -84,6 +84,9 @@ dofile(minetest.get_modpath("mcl_farming").."/hoes.lua") -- ========= WHEAT ========= dofile(minetest.get_modpath("mcl_farming").."/wheat.lua") +-- = HELPER FUNCTION FOR PUMPKIN AND MELON = +dofile(minetest.get_modpath("mcl_farming").."/gourd.lua") + -- ========= PUMPKIN ========= dofile(minetest.get_modpath("mcl_farming").."/pumpkin.lua") diff --git a/mods/ITEMS/mcl_farming/pumpkin.lua b/mods/ITEMS/mcl_farming/pumpkin.lua index 90136ae0..c5937958 100644 --- a/mods/ITEMS/mcl_farming/pumpkin.lua +++ b/mods/ITEMS/mcl_farming/pumpkin.lua @@ -31,6 +31,8 @@ local stemdrop = { }, } +-- Growing unconnected stem + minetest.register_node("mcl_farming:pumpkin_1", { description = "Pumpkin Stem (First Stage)", _doc_items_entry_name = "Pumpkin Stem", @@ -71,39 +73,9 @@ minetest.register_node("mcl_farming:pumpkin_2", { _mcl_blast_resistance = 0, }) - -minetest.register_node("mcl_farming:pumpkin_face", { - description = "Pumpkin", - _doc_items_longdesc = "A pumpkin is a block which can be grown from pumpkin seeds.", - stack_max = 64, - paramtype2 = "facedir", - tiles = {"farming_pumpkin_top.png", "farming_pumpkin_top.png", "farming_pumpkin_side.png", "farming_pumpkin_side.png", "farming_pumpkin_side.png", "farming_pumpkin_face.png"}, - groups = {handy=1,axey=1, building_block=1}, - after_dig_node = function(blockpos, oldnode, oldmetadata, user) - -- Disconnect any connected stems, turning them back to normal stems - local neighbors = { - { { x=-1, y=0, z=0 }, "mcl_farming:pumpkintige_linked_r" }, - { { x=1, y=0, z=0 }, "mcl_farming:pumpkintige_linked_l" }, - { { x=0, y=0, z=-1 }, "mcl_farming:pumpkintige_linked_t" }, - { { x=0, y=0, z=1 }, "mcl_farming:pumpkintige_linked_b" }, - } - for n=1, #neighbors do - local offset = neighbors[n][1] - local expected_stem = neighbors[n][2] - local stempos = vector.add(blockpos, offset) - local stem = minetest.get_node(stempos) - if stem.name == expected_stem then - minetest.add_node(stempos, {name="mcl_farming:pumpkintige_unconnect"}) - end - end - end, - sounds = mcl_sounds.node_sound_wood_defaults(), - _mcl_blast_resistance = 5, - _mcl_hardness = 1, -}) - +-- Full stem (not connected) minetest.register_node("mcl_farming:pumpkintige_unconnect", { - description = "Pumpkin Stem (Not Connected)", + description = "Mature Pumpkin Stem", _doc_items_create_entry = false, paramtype = "light", walkable = false, @@ -116,193 +88,28 @@ minetest.register_node("mcl_farming:pumpkintige_unconnect", { _mcl_blast_resistance = 0, }) +-- Template for pumpkin +local pumpkin_base_def = { + description = "Pumpkin", + _doc_items_longdesc = "A pumpkin is a block which can be grown from pumpkin seeds.", + stack_max = 64, + paramtype2 = "facedir", + tiles = {"farming_pumpkin_top.png", "farming_pumpkin_top.png", "farming_pumpkin_side.png", "farming_pumpkin_side.png", "farming_pumpkin_side.png", "farming_pumpkin_face.png"}, + groups = {handy=1,axey=1, building_block=1}, + sounds = mcl_sounds.node_sound_wood_defaults(), + _mcl_blast_resistance = 5, + _mcl_hardness = 1, +} -minetest.register_node("mcl_farming:pumpkintige_linked_r", { - description = "Pumpkin Stem (Linked to the Right)", - _doc_items_create_entry = false, - paramtype = "light", - sunlight_propagates = true, - walkable = false, - drop = stemdrop, - drawtype = "nodebox", - paramtype = "light", - node_box = { - type = "fixed", - fixed = { - {-0.5, -0.5, 0, 0.5, 0.5, 0}, -- NodeBox1 - } - }, - selection_box = { - type = "fixed", - fixed = {-0.2, -0.5, -0.2, 0.2, 0.2, 0.2} - }, - tiles = { - "farming_tige_connnect.png", --top - "farming_tige_connnect.png", -- bottom - "farming_tige_connnect.png", -- right - "farming_tige_connnect.png", -- left - "farming_tige_connnect.png", -- back - "farming_tige_connnect.png^[transformFX90" --front - }, - groups = {dig_immediate=3, not_in_creative_inventory=1 ,dig_by_water=1}, - sounds = mcl_sounds.node_sound_leaves_defaults(), - _mcl_blast_resistance = 0, -}) - -minetest.register_node("mcl_farming:pumpkintige_linked_l", { - description = "Pumpkin Stem (Linked to the Left)", - _doc_items_create_entry = false, - paramtype = "light", - walkable = false, - sunlight_propagates = true, - drop = stemdrop, - drawtype = "nodebox", - paramtype = "light", - node_box = { - type = "fixed", - fixed = { - {-0.5, -0.5, 0, 0.5, 0.5, 0}, -- NodeBox1 - } - }, - selection_box = { - type = "fixed", - fixed = {-0.2, -0.5, -0.2, 0.2, 0.2, 0.2} - }, - tiles = { - "farming_tige_connnect.png", --top - "farming_tige_connnect.png", -- bottom - "farming_tige_connnect.png", -- right - "farming_tige_connnect.png", -- left - "farming_tige_connnect.png^[transformFX90", -- back - "farming_tige_connnect.png" --front - }, - groups = {dig_immediate=3, not_in_creative_inventory=1 ,dig_by_water=1}, - sounds = mcl_sounds.node_sound_leaves_defaults(), - _mcl_blast_resistance = 0, -}) - -minetest.register_node("mcl_farming:pumpkintige_linked_t", { - description = "Pumpkin Stem (Linked to the Top)", - _doc_items_create_entry = false, - paramtype = "light", - walkable = false, - sunlight_propagates = true, - drop = stemdrop, - drawtype = "nodebox", - paramtype = "light", - node_box = { - type = "fixed", - fixed = { - {0, -0.5, -0.5, 0, 0.5, 0.5}, -- NodeBox1 - } - }, - selection_box = { - type = "fixed", - fixed = {-0.2, -0.5, -0.2, 0.2, 0.2, 0.2} - }, - tiles = { - "farming_tige_connnect.png", --top - "farming_tige_connnect.png", -- bottom - "farming_tige_connnect.png^[transformFX90", -- right - "farming_tige_connnect.png", -- left - "farming_tige_connnect.png", -- back - "farming_tige_connnect.png" --front - }, - groups = {dig_immediate=3, not_in_creative_inventory=1 ,dig_by_water=1}, - sounds = mcl_sounds.node_sound_leaves_defaults(), - _mcl_blast_resistance = 0, -}) - -minetest.register_node("mcl_farming:pumpkintige_linked_b", { - description = "Pumpkin Stem (Linked to the Bottom)", - _doc_items_create_entry = false, - paramtype = "light", - walkable = false, - sunlight_propagates = true, - drop = stemdrop, - drawtype = "nodebox", - paramtype = "light", - node_box = { - type = "fixed", - fixed = { - {0, -0.5, -0.5, 0, 0.5, 0.5}, -- NodeBox1 - } - }, - selection_box = { - type = "fixed", - fixed = {-0.2, -0.5, -0.2, 0.2, 0.2, 0.2} - }, - tiles = { - "farming_tige_connnect.png", --top - "farming_tige_connnect.png", -- bottom - "farming_tige_connnect.png", -- right - "farming_tige_connnect.png^[transformFX90", -- left - "farming_tige_connnect.png", -- back - "farming_tige_connnect.png" --front - }, - groups = {dig_immediate=3, not_in_creative_inventory=1 ,dig_by_water=1}, - sounds = mcl_sounds.node_sound_leaves_defaults(), - _mcl_blast_resistance = 0, -}) - +-- Register stem growth mcl_farming:add_plant("mcl_farming:pumpkintige_unconnect", {"mcl_farming:pumpkin_1", "mcl_farming:pumpkin_2"}, 80, 20) - -minetest.register_abm({ - nodenames = {"mcl_farming:pumpkintige_unconnect"}, - neighbors = {"air"}, - interval = 1, - chance = 1, - action = function(stempos) - local light = minetest.get_node_light(stempos) - if light and light > 10 then - -- Check the four neighbors and filter out neighbors where pumpkins can't grow - local neighbors = { - { x=-1, y=0, z=0 }, - { x=1, y=0, z=0 }, - { x=0, y=0, z=-1 }, - { x=0, y=0, z=1 }, - } - for n=#neighbors, 1, -1 do - local offset = neighbors[n] - local blockpos = vector.add(stempos, offset) - local floorpos = { x=blockpos.x, y=blockpos.y-1, z=blockpos.z } - local floor = minetest.get_node(floorpos) - local block = minetest.get_node(blockpos) - local soilgroup = minetest.get_item_group(floor.name, "soil") - if not ((floor.name=="mcl_core:dirt_with_grass" or floor.name=="mcl_core:dirt" or soilgroup == 2 or soilgroup == 3) and block.name == "air") then - table.remove(neighbors, n) - end - end - - -- Pumpkins need at least 1 free neighbor to grow - if #neighbors > 0 then - -- From the remaining neighbors, grow randomly - local r = math.random(1, #neighbors) - local offset = neighbors[r] - local blockpos = vector.add(stempos, offset) - local p2 - if offset.x == 1 then - minetest.set_node(stempos, {name="mcl_farming:pumpkintige_linked_r" }) - p2 = 3 - elseif offset.x == -1 then - minetest.set_node(stempos, {name="mcl_farming:pumpkintige_linked_l"}) - p2 = 1 - elseif offset.z == 1 then - minetest.set_node(stempos, {name="mcl_farming:pumpkintige_linked_t"}) - p2 = 2 - elseif offset.z == -1 then - minetest.set_node(stempos, {name="mcl_farming:pumpkintige_linked_b"}) - p2 = 0 - end - minetest.add_node(blockpos, {name="mcl_farming:pumpkin_face", param2=p2}) - end - end - end, -}) +-- Register actual pumpkin, connected stems and stem-to-pumpkin growth +mcl_farming.register_gourd("mcl_farming:pumpkintige_unconnect", "mcl_farming:pumpkintige_linked", stemdrop, "mcl_farming:pumpkin_face", pumpkin_base_def) +-- Jack o'Lantern minetest.register_node("mcl_farming:pumpkin_face_light", { description = "Jack o'Lantern", _doc_items_longdesc = "A Jack o'lantern is a traditional halloween decoration made from a pumpkin and glows brightly.", @@ -318,6 +125,8 @@ minetest.register_node("mcl_farming:pumpkin_face_light", { _mcl_hardness = 1, }) +-- Crafting + minetest.register_craft({ output = "mcl_farming:pumpkin_face_light", recipe = {{"mcl_farming:pumpkin_face"}, diff --git a/mods/ITEMS/mcl_farming/textures/farming_tige_connnect.png b/mods/ITEMS/mcl_farming/textures/farming_tige_connect.png similarity index 100% rename from mods/ITEMS/mcl_farming/textures/farming_tige_connnect.png rename to mods/ITEMS/mcl_farming/textures/farming_tige_connect.png