diff options
author | Auke Kok <auke-jan.h.kok@intel.com> | 2015-05-05 23:47:42 -0700 |
---|---|---|
committer | Auke Kok <auke-jan.h.kok@intel.com> | 2015-05-05 23:47:42 -0700 |
commit | fa07d3307ab454b16f84ddc1d5abf60298a5cdeb (patch) | |
tree | a6be8973a8f04fd3451ce52f9c92290680d6ed49 | |
parent | a4a53ba854114ff2cafb0fd7f76d8d81f485a306 (diff) |
Humidity for corn.
Adding some code to handle double sized plants easily. The
watering can now redirects water to the bottom, and the hydrometer
reads the bottom half water value instead of the top. No ABM's for
hydration happen on the top half, so no risk of double damage there.
-rw-r--r-- | corn.lua | 64 | ||||
-rw-r--r-- | init.lua | 20 |
2 files changed, 73 insertions, 11 deletions
@@ -31,7 +31,7 @@ minetest.register_node("crops:corn", { if minetest.get_item_group(under.name, "soil") <= 1 then return end - minetest.set_node(pointed_thing.above, {name="crops:corn_base_seed"}) + crops.plant(pointed_thing.above, {name="crops:corn_base_seed"}) if not minetest.setting_getbool("creative_mode") then itemstack:take_item() end @@ -87,10 +87,10 @@ minetest.register_abm({ interval = crops.interval, chance = crops.chance, action = function(pos, node, active_object_count, active_object_count_wider) - if minetest.get_node_light(pos, nil) < crops.light then + if not crops.can_grow(pos) then return end - minetest.set_node(pos, { name = "crops:corn_base_1" }) + minetest.swap_node(pos, { name = "crops:corn_base_1" }) end }) @@ -115,14 +115,20 @@ minetest.register_abm({ interval = crops.interval, chance = crops.chance, action = function(pos, node, active_object_count, active_object_count_wider) - if minetest.get_node_light(pos, nil) < crops.light then + if not crops.can_grow(pos) then return end if not minetest.get_node({x = pos.x, y = pos.y + 1, z = pos.z}).name == "air" then return end - minetest.set_node(pos, { name = "crops:corn_base_2" }) - minetest.set_node({x = pos.x, y = pos.y + 1, z = pos.z} , { name = "crops:corn_top_1" }) + local meta = minetest.get_meta(pos) + local water = meta:get_int("crops_water") + minetest.swap_node(pos, { name = "crops:corn_base_2" }) + local above = {x = pos.x, y = pos.y + 1, z = pos.z} + minetest.set_node(above , { name = "crops:corn_top_1" }) + local meta = minetest.get_meta(above) + meta:set_int("crops_top_half", 1) + meta:set_int("crops_water", water) end }) @@ -150,8 +156,13 @@ minetest.register_node("crops:corn_base_2", { minetest.remove_node(pos) end + local meta = minetest.get_meta(pos) + local damage = meta:get_int("crops_damage") local drops = {} - for i = 1,math.random(2,4) do + -- 0 - 2-4 + -- 50 - 2-3 + -- 100 - 1-1 + for i = 1,math.random(2 - (damage / 100), 4 - (3 * (damage / 100))) do table.insert(drops, ('crops:corn_cob')) end minetest.set_node(pos, { name = "crops:corn_base_3" }) @@ -214,7 +225,7 @@ minetest.register_abm({ if minetest.get_node_light(pos, nil) < crops.light then return end - minetest.set_node(pos, { name = "crops:corn_top_2" }) + minetest.swap_node(pos, { name = "crops:corn_top_2" }) end }) @@ -247,10 +258,12 @@ minetest.register_abm({ interval = crops.interval, chance = crops.chance, action = function(pos, node, active_object_count, active_object_count_wider) + -- we don't call crops.grow here otherwise there would be 2 abm's hitting + -- this stack, and dmg needs to be applied to the bottom part if minetest.get_node_light(pos, nil) < crops.light then return end - minetest.set_node(pos, { name = "crops:corn_top_3" }) + minetest.swap_node(pos, { name = "crops:corn_top_3" }) end }) @@ -268,12 +281,18 @@ minetest.register_node("crops:corn_top_3", { drop = {}, sounds = default.node_sound_leaves_defaults(), on_dig = function(pos, node, digger) + local below = { x = pos.x, y = pos.y - 1, z = pos.z } + local meta = minetest.get_meta(below) + local damage = meta:get_int("crops_damage") local drops = {} - for i = 1,math.random(2,4) do + -- 0 - 2-4 + -- 50 - 2-3 + -- 100 - 1-1 + for i = 1,math.random(2 - (damage / 100), 4 - (3 * (damage / 100))) do table.insert(drops, ('crops:corn_cob')) end minetest.set_node(pos, { name = "crops:corn_top_4" }) - minetest.set_node({x = pos.x, y = pos.y - 1, z = pos.z}, { name = "crops:corn_base_3" }) + minetest.set_node(below, { name = "crops:corn_base_3" }) core.handle_node_drops(pos, drops, digger) end }) @@ -300,3 +319,26 @@ minetest.register_node("crops:corn_top_4", { end }) +crops.corn_die = function(pos) + minetest.set_node(pos, { name = "crops:corn_base_4" }) + local above = {x = pos.x, y = pos.y + 1, z = pos.z} + minetest.set_node(above, { name = "crops:corn_top_4" }) +end + +local properties = { + wither = crops.corn_die, + waterstart = 40, + wateruse = 1, + night = 5, + soak = 60, + soak_damage = 75, + wither = 10, + wither_damage = 0, + doublesize = true, +} + +table.insert(crops.plants, { name = "crops:corn_base_seed", properties = properties }) +table.insert(crops.plants, { name = "crops:corn_base_1", properties = properties }) +table.insert(crops.plants, { name = "crops:corn_base_2", properties = properties }) +table.insert(crops.plants, { name = "crops:corn_base_3", properties = properties }) + @@ -146,6 +146,11 @@ minetest.register_tool("crops:watering_can", { itemstack:set_wear(1) return itemstack end + -- using it on a top-half part of a plant? + local meta = minetest.get_meta(pos) + if meta:get_int("crops_top_half") == 1 then + pos = {x = pos.x, y = pos.y - 1, z = pos.z} + end -- using it on a plant? local meta = minetest.get_meta(pos) local water = meta:get_int("crops_water") @@ -159,6 +164,7 @@ minetest.register_tool("crops:watering_can", { end water = math.min(water + crops.watercan, crops.watercan_max) meta:set_int("crops_water", water) + itemstack:set_wear(math.min(65534, wear + (65535 / crops.watercanuses))) return itemstack end, @@ -177,6 +183,12 @@ minetest.register_tool("crops:hydrometer", { if pos == nil then return itemstack end + -- doublesize plant? + local meta = minetest.get_meta(pos) + if meta:get_int("crops_top_half") == 1 then + pos = {x = pos.x, y = pos.y - 1, z = pos.z} + end + local meta = minetest.get_meta(pos) -- using it on a plant? local water = meta:get_int("crops_water") @@ -254,6 +266,14 @@ minetest.register_abm({ -- dry out the plant water = math.max(0, water - plant.properties.wateruse ) meta:set_int("crops_water", water) + + -- for convenience, copy water attribute to top half + if not plant.properties.doublesize == nil and plant.properties.doublesize then + local above = { x = pos.x, y = pos.y + 1, z = pos.z} + local meta = minetest.get_meta(above) + meta:set_int("crops_water", water) + end + if water < plant.properties.wither_damage then crops.particles(pos, 0) damage = damage + math.random(0,5) |