summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--init.lua246
-rw-r--r--tools.lua119
2 files changed, 195 insertions, 170 deletions
diff --git a/init.lua b/init.lua
index 6aeaa5b..7bd3024 100644
--- a/init.lua
+++ b/init.lua
@@ -27,6 +27,7 @@ settings.easy = {
damage_tick_min = 0,
damage_tick_max = 1,
damage_max = 25,
+ hydration = false,
}
settings.normal = {
chance = 8,
@@ -40,6 +41,7 @@ settings.normal = {
damage_tick_min = 0,
damage_tick_max = 5,
damage_max = 50,
+ hydration = true,
}
settings.difficult = {
chance = 16,
@@ -53,6 +55,7 @@ settings.difficult = {
damage_tick_min = 3,
damage_tick_max = 7,
damage_max = 100,
+ hydration = true,
}
local worldpath = minetest.get_worldpath()
@@ -86,6 +89,9 @@ if not crops.settings then
minetest.log("error", "Defaulting to \"normal\" difficulty settings")
crops.settings = settings.normal
end
+if crops.settings.hydration then
+ minetest.log("action", "[crops] Hydration and dehydration mechanics are enabled.")
+end
local find_plant = function(node)
for i = 1,table.getn(crops.plants) do
@@ -119,20 +125,24 @@ crops.can_grow = function(pos)
return false
end
local meta = minetest.get_meta(pos)
- local water = meta:get_int("crops_water")
- if water < plant.properties.wither or water > plant.properties.soak then
- if math.random(0,1) == 0 then
- return false
+ if crops.settings.hydration then
+ local water = meta:get_int("crops_water")
+ if water < plant.properties.wither or water > plant.properties.soak then
+ if math.random(0,1) == 0 then
+ return false
+ end
end
+ -- growing costs water!
+ meta:set_int("crops_water", math.max(1, water - 10))
end
+
+ -- damaged plants are less likely to grow
local damage = meta:get_int("crops_damage")
if not damage == 0 then
if math.random(math.min(50, damage), 100) > 75 then
return false
end
end
- -- growing costs water!
- meta:set_int("crops_water", math.max(1, water - 10))
-- allow the plant to grow
return true
@@ -278,115 +288,9 @@ crops.die = function(pos)
minetest.sound_play("crops_flies", {pos=pos, gain=0.8})
end
-minetest.register_tool("crops:watering_can", {
- description = "Watering Can",
- inventory_image = "crops_watering_can.png",
- liquids_pointable = true,
- range = 2.5,
- stack_max = 1,
- wear = 65535,
- tool_capabilities = {},
- on_use = function(itemstack, user, pointed_thing)
- local pos = pointed_thing.under
- local ppos = pos
- if not pos then
- return itemstack
- end
- -- filling it up?
- local wear = itemstack:get_wear()
- if minetest.get_item_group(minetest.get_node(pos).name, "water") >= 3 then
- if wear ~= 1 then
- minetest.sound_play("crops_watercan_entering", {pos=pos, gain=0.8})
- minetest.after(math.random()/2, function(pos)
- if math.random(2) == 1 then
- minetest.sound_play("crops_watercan_splash_quiet", {pos=pos, gain=0.1})
- end
- if math.random(3) == 1 then
- minetest.after(math.random()/2, function(pos)
- minetest.sound_play("crops_watercan_splash_small", {pos=pos, gain=0.7})
- end, pos)
- end
- if math.random(3) == 1 then
- minetest.after(math.random()/2, function(pos)
- minetest.sound_play("crops_watercan_splash_big", {pos=pos, gain=0.7})
- end, pos)
- end
- end, pos)
- itemstack:set_wear(1)
- end
- 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
- meta = minetest.get_meta({x=pos.x, y=pos.y-1, z=pos.z})
- end
- -- using it on a plant?
- local water = meta:get_int("crops_water")
- if water < 1 then
- return itemstack
- end
- -- empty?
- if wear == 65534 then
- return itemstack
- end
- crops.particles(ppos, 2)
- minetest.sound_play("crops_watercan_watering", {pos=pos, gain=0.8})
- water = math.min(water + crops.settings.watercan, crops.settings.watercan_max)
- meta:set_int("crops_water", water)
-
- itemstack:set_wear(math.min(65534, wear + (65535 / crops.settings.watercan_uses)))
- return itemstack
- end,
-})
-
-minetest.register_tool("crops:hydrometer", {
- description = "Hydrometer",
- inventory_image = "crops_hydrometer.png",
- liquids_pointable = false,
- range = 2.5,
- stack_max = 1,
- tool_capabilities = {
- },
- on_use = function(itemstack, user, pointed_thing)
- local pos = pointed_thing.under
- if not pos then
- return itemstack
- end
- -- doublesize plant?
- local meta = minetest.get_meta(pos)
- if meta:get_int("crops_top_half") == 1 then
- meta = minetest.get_meta({x=pos.x, y=pos.y-1, z=pos.z})
- end
-
- -- using it on a plant?
- local water = meta:get_int("crops_water")
- if water == nil then
- itemstack:set_wear(65534)
- return itemstack
- end
- itemstack:set_wear(65535 - ((65534 / 100) * water))
- return itemstack
- end,
-})
-
-minetest.register_craft({
- output = "crops:watering_can",
- recipe = {
- { "default:steel_ingot", "", "" },
- { "default:steel_ingot", "", "default:steel_ingot" },
- { "", "default:steel_ingot", "" },
- },
-})
-
-minetest.register_craft({
- output = "crops:hydrometer",
- recipe = {
- { "default:mese_crystal_fragment", "", "" },
- { "", "default:steel_ingot", "" },
- { "", "", "default:steel_ingot" },
- },
-})
+if crops.settings.hydration then
+ dofile(modpath .. "/tools.lua")
+end
-- crop nodes, crafts, craftitems
dofile(modpath .. "/melon.lua")
@@ -402,70 +306,72 @@ for i = 1,table.getn(crops.plants) do
end
-- water handling code
-minetest.register_abm({
- nodenames = nodenames,
- interval = crops.settings.damage_interval,
- chance = crops.settings.damage_chance,
- action = function(pos, node, active_object_count, active_object_count_wider)
- local meta = minetest.get_meta(pos)
- local water = meta:get_int("crops_water")
- local damage = meta:get_int("crops_damage")
-
- -- get plant specific data
- local plant = find_plant(node)
- if plant == nil then
- return
- end
+if crops.settings.hydration then
+ minetest.register_abm({
+ nodenames = nodenames,
+ interval = crops.settings.damage_interval,
+ chance = crops.settings.damage_chance,
+ action = function(pos, node, active_object_count, active_object_count_wider)
+ local meta = minetest.get_meta(pos)
+ local water = meta:get_int("crops_water")
+ local damage = meta:get_int("crops_damage")
+
+ -- get plant specific data
+ local plant = find_plant(node)
+ if plant == nil then
+ return
+ end
- -- increase water for nearby water sources
- local f = minetest.find_node_near(pos, 1, {"default:water_source", "default:water_flowing"})
- if not f == nil then
- water = math.min(100, water + 2)
- else
- local f = minetest.find_node_near(pos, 2, {"default:water_source", "default:water_flowing"})
+ -- increase water for nearby water sources
+ local f = minetest.find_node_near(pos, 1, {"default:water_source", "default:water_flowing"})
if not f == nil then
+ water = math.min(100, water + 2)
+ else
+ local f = minetest.find_node_near(pos, 2, {"default:water_source", "default:water_flowing"})
+ if not f == nil then
+ water = math.min(100, water + 1)
+ end
+ end
+
+ if minetest.get_node_light(pos, nil) < plant.properties.night then
+ -- compensate for light: at night give some water back to the plant
water = math.min(100, water + 1)
+ else
+ -- dry out the plant
+ water = math.max(1, water - plant.properties.wateruse)
end
- end
-
- if minetest.get_node_light(pos, nil) < plant.properties.night then
- -- compensate for light: at night give some water back to the plant
- water = math.min(100, water + 1)
- else
- -- dry out the plant
- water = math.max(1, water - plant.properties.wateruse)
- end
- 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(crops.settings.damage_tick_min, crops.settings.damage_tick_max)
- elseif water <= plant.properties.wither then
- crops.particles(pos, 0)
- return
- elseif water >= plant.properties.soak_damage then
- crops.particles(pos, 1)
- damage = damage + math.random(crops.settings.damage_tick_min, crops.settings.damage_tick_max)
- elseif water >= plant.properties.soak then
- crops.particles(pos, 1)
- return
- end
- meta:set_int("crops_damage", math.min(crops.settings.damage_max, damage))
+ -- 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
- -- is it dead?
- if damage >= 100 then
- crops.die(pos)
+ if water <= plant.properties.wither_damage then
+ crops.particles(pos, 0)
+ damage = damage + math.random(crops.settings.damage_tick_min, crops.settings.damage_tick_max)
+ elseif water <= plant.properties.wither then
+ crops.particles(pos, 0)
+ return
+ elseif water >= plant.properties.soak_damage then
+ crops.particles(pos, 1)
+ damage = damage + math.random(crops.settings.damage_tick_min, crops.settings.damage_tick_max)
+ elseif water >= plant.properties.soak then
+ crops.particles(pos, 1)
+ return
+ end
+ meta:set_int("crops_damage", math.min(crops.settings.damage_max, damage))
+
+ -- is it dead?
+ if damage >= 100 then
+ crops.die(pos)
+ end
end
- end
-})
+ })
+end
-- cooking recipes that mix craftitems
dofile(modpath .. "/cooking.lua")
diff --git a/tools.lua b/tools.lua
new file mode 100644
index 0000000..5605254
--- /dev/null
+++ b/tools.lua
@@ -0,0 +1,119 @@
+--[[
+
+Copyright (C) 2015 - Auke Kok <sofar@foo-projects.org>
+
+"crops" is free software; you can redistribute it and/or modify
+it under the terms of the GNU Lesser General Public License as
+published by the Free Software Foundation; either version 2.1
+of the license, or (at your option) any later version.
+
+--]]
+minetest.register_tool("crops:watering_can", {
+ description = "Watering Can",
+ inventory_image = "crops_watering_can.png",
+ liquids_pointable = true,
+ range = 2.5,
+ stack_max = 1,
+ wear = 65535,
+ tool_capabilities = {},
+ on_use = function(itemstack, user, pointed_thing)
+ local pos = pointed_thing.under
+ local ppos = pos
+ if not pos then
+ return itemstack
+ end
+ -- filling it up?
+ local wear = itemstack:get_wear()
+ if minetest.get_item_group(minetest.get_node(pos).name, "water") >= 3 then
+ if wear ~= 1 then
+ minetest.sound_play("crops_watercan_entering", {pos=pos, gain=0.8})
+ minetest.after(math.random()/2, function(pos)
+ if math.random(2) == 1 then
+ minetest.sound_play("crops_watercan_splash_quiet", {pos=pos, gain=0.1})
+ end
+ if math.random(3) == 1 then
+ minetest.after(math.random()/2, function(pos)
+ minetest.sound_play("crops_watercan_splash_small", {pos=pos, gain=0.7})
+ end, pos)
+ end
+ if math.random(3) == 1 then
+ minetest.after(math.random()/2, function(pos)
+ minetest.sound_play("crops_watercan_splash_big", {pos=pos, gain=0.7})
+ end, pos)
+ end
+ end, pos)
+ itemstack:set_wear(1)
+ end
+ 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
+ meta = minetest.get_meta({x=pos.x, y=pos.y-1, z=pos.z})
+ end
+ -- using it on a plant?
+ local water = meta:get_int("crops_water")
+ if water < 1 then
+ return itemstack
+ end
+ -- empty?
+ if wear == 65534 then
+ return itemstack
+ end
+ crops.particles(ppos, 2)
+ minetest.sound_play("crops_watercan_watering", {pos=pos, gain=0.8})
+ water = math.min(water + crops.settings.watercan, crops.settings.watercan_max)
+ meta:set_int("crops_water", water)
+
+ itemstack:set_wear(math.min(65534, wear + (65535 / crops.settings.watercan_uses)))
+ return itemstack
+ end,
+})
+
+minetest.register_tool("crops:hydrometer", {
+ description = "Hydrometer",
+ inventory_image = "crops_hydrometer.png",
+ liquids_pointable = false,
+ range = 2.5,
+ stack_max = 1,
+ tool_capabilities = {
+ },
+ on_use = function(itemstack, user, pointed_thing)
+ local pos = pointed_thing.under
+ if not pos then
+ return itemstack
+ end
+ -- doublesize plant?
+ local meta = minetest.get_meta(pos)
+ if meta:get_int("crops_top_half") == 1 then
+ meta = minetest.get_meta({x=pos.x, y=pos.y-1, z=pos.z})
+ end
+
+ -- using it on a plant?
+ local water = meta:get_int("crops_water")
+ if water == nil then
+ itemstack:set_wear(65534)
+ return itemstack
+ end
+ itemstack:set_wear(65535 - ((65534 / 100) * water))
+ return itemstack
+ end,
+})
+
+minetest.register_craft({
+ output = "crops:watering_can",
+ recipe = {
+ { "default:steel_ingot", "", "" },
+ { "default:steel_ingot", "", "default:steel_ingot" },
+ { "", "default:steel_ingot", "" },
+ },
+})
+
+minetest.register_craft({
+ output = "crops:hydrometer",
+ recipe = {
+ { "default:mese_crystal_fragment", "", "" },
+ { "", "default:steel_ingot", "" },
+ { "", "", "default:steel_ingot" },
+ },
+})