player_api/mods/farming/init.lua

471 lines
11 KiB
Lua
Raw Normal View History

2012-11-11 11:47:53 +01:00
-- Minetest 0.4 mod: farming
-- See README.txt for licensing and other information.
farming = {}
2012-11-11 11:47:53 +01:00
--
-- Soil
--
minetest.register_node("farming:soil", {
description = "Soil",
tiles = {"farming_soil.png", "default_dirt.png"},
drop = "default:dirt",
is_ground_content = true,
groups = {crumbly=3, not_in_creative_inventory=1, soil=2},
sounds = default.node_sound_dirt_defaults(),
})
minetest.register_node("farming:soil_wet", {
description = "Wet Soil",
tiles = {"farming_soil_wet.png", "farming_soil_wet_side.png"},
drop = "default:dirt",
is_ground_content = true,
groups = {crumbly=3, not_in_creative_inventory=1, soil=3},
sounds = default.node_sound_dirt_defaults(),
})
minetest.register_abm({
nodenames = {"farming:soil", "farming:soil_wet"},
interval = 15,
chance = 4,
action = function(pos, node)
pos.y = pos.y+1
2013-05-25 00:40:03 +02:00
local nn = minetest.get_node(pos).name
2012-11-11 11:47:53 +01:00
pos.y = pos.y-1
2013-06-27 20:08:51 +02:00
if minetest.registered_nodes[nn] and
minetest.registered_nodes[nn].walkable and
minetest.get_item_group(nn, "plant") == 0
then
2013-05-25 00:40:03 +02:00
minetest.set_node(pos, {name="default:dirt"})
2012-11-11 11:47:53 +01:00
end
-- check if there is water nearby
2013-05-25 00:40:03 +02:00
if minetest.find_node_near(pos, 3, {"group:water"}) then
2012-11-11 11:47:53 +01:00
-- if it is dry soil turn it into wet soil
if node.name == "farming:soil" then
2013-05-25 00:40:03 +02:00
minetest.set_node(pos, {name="farming:soil_wet"})
2012-11-11 11:47:53 +01:00
end
else
-- turn it back into dirt if it is already dry
if node.name == "farming:soil" then
-- only turn it back if there is no plant on top of it
if minetest.get_item_group(nn, "plant") == 0 then
2013-05-25 00:40:03 +02:00
minetest.set_node(pos, {name="default:dirt"})
2012-11-11 11:47:53 +01:00
end
-- if its wet turn it back into dry soil
elseif node.name == "farming:soil_wet" then
2013-05-25 00:40:03 +02:00
minetest.set_node(pos, {name="farming:soil"})
2012-11-11 11:47:53 +01:00
end
end
end,
})
--
-- Hoes
--
2013-06-03 01:15:40 +02:00
-- turns nodes with group soil=1 into soil
function farming.hoe_on_use(itemstack, user, pointed_thing, uses)
2012-11-11 11:47:53 +01:00
local pt = pointed_thing
-- check if pointing at a node
if not pt then
return
end
if pt.type ~= "node" then
return
end
2013-05-25 00:40:03 +02:00
local under = minetest.get_node(pt.under)
2012-11-11 11:47:53 +01:00
local p = {x=pt.under.x, y=pt.under.y+1, z=pt.under.z}
2013-05-25 00:40:03 +02:00
local above = minetest.get_node(p)
2012-11-11 11:47:53 +01:00
-- return if any of the nodes is not registered
if not minetest.registered_nodes[under.name] then
return
end
if not minetest.registered_nodes[above.name] then
return
end
-- check if the node above the pointed thing is air
if above.name ~= "air" then
return
end
-- check if pointing at dirt
if minetest.get_item_group(under.name, "soil") ~= 1 then
return
end
-- turn the node into soil, wear out item and play sound
2013-05-25 00:40:03 +02:00
minetest.set_node(pt.under, {name="farming:soil"})
2012-11-11 11:47:53 +01:00
minetest.sound_play("default_dig_crumbly", {
pos = pt.under,
gain = 0.5,
})
itemstack:add_wear(65535/(uses-1))
return itemstack
end
minetest.register_tool("farming:hoe_wood", {
description = "Wooden Hoe",
inventory_image = "farming_tool_woodhoe.png",
on_use = function(itemstack, user, pointed_thing)
return farming.hoe_on_use(itemstack, user, pointed_thing, 30)
2012-11-11 11:47:53 +01:00
end,
})
minetest.register_tool("farming:hoe_stone", {
description = "Stone Hoe",
inventory_image = "farming_tool_stonehoe.png",
on_use = function(itemstack, user, pointed_thing)
return farming.hoe_on_use(itemstack, user, pointed_thing, 90)
2012-11-11 11:47:53 +01:00
end,
})
minetest.register_tool("farming:hoe_steel", {
description = "Steel Hoe",
inventory_image = "farming_tool_steelhoe.png",
on_use = function(itemstack, user, pointed_thing)
return farming.hoe_on_use(itemstack, user, pointed_thing, 200)
2012-11-11 11:47:53 +01:00
end,
})
minetest.register_tool("farming:hoe_bronze", {
description = "Bronze Hoe",
inventory_image = "farming_tool_bronzehoe.png",
on_use = function(itemstack, user, pointed_thing)
return farming.hoe_on_use(itemstack, user, pointed_thing, 220)
2012-11-11 11:47:53 +01:00
end,
})
minetest.register_tool("farming:hoe_mese", {
description = "Mese Hoe",
inventory_image = "farming_tool_mesehoe.png",
on_use = function(itemstack, user, pointed_thing)
return farming.hoe_on_use(itemstack, user, pointed_thing, 350)
end,
})
minetest.register_tool("farming:hoe_diamond", {
description = "Diamond Hoe",
inventory_image = "farming_tool_diamondhoe.png",
on_use = function(itemstack, user, pointed_thing)
return farming.hoe_on_use(itemstack, user, pointed_thing, 500)
end,
})
2012-11-11 11:47:53 +01:00
minetest.register_craft({
output = "farming:hoe_wood",
recipe = {
{"group:wood", "group:wood"},
2013-09-16 05:49:56 +02:00
{"", "group:stick"},
{"", "group:stick"},
2012-11-11 11:47:53 +01:00
}
})
minetest.register_craft({
output = "farming:hoe_stone",
recipe = {
{"group:stone", "group:stone"},
2013-09-16 05:49:56 +02:00
{"", "group:stick"},
{"", "group:stick"},
2012-11-11 11:47:53 +01:00
}
})
minetest.register_craft({
output = "farming:hoe_steel",
recipe = {
{"default:steel_ingot", "default:steel_ingot"},
2013-09-16 05:49:56 +02:00
{"", "group:stick"},
{"", "group:stick"},
2012-11-11 11:47:53 +01:00
}
})
minetest.register_craft({
output = "farming:hoe_bronze",
recipe = {
{"default:bronze_ingot", "default:bronze_ingot"},
2013-09-16 05:49:56 +02:00
{"", "group:stick"},
{"", "group:stick"},
2012-11-11 11:47:53 +01:00
}
})
minetest.register_craft({
output = "farming:hoe_mese",
recipe = {
{"default:mese_crystal", "default:mese_crystal"},
{"", "group:stick"},
{"", "group:stick"},
}
})
minetest.register_craft({
output = "farming:hoe_diamond",
recipe = {
{"default:diamond", "default:diamond"},
{"", "group:stick"},
{"", "group:stick"},
}
})
2012-11-11 11:47:53 +01:00
--
-- Override grass for drops
--
for i = 1, 5 do
2014-04-14 20:26:30 +02:00
minetest.override_item("default:grass_"..i, {drop = {
2012-11-11 11:47:53 +01:00
max_items = 1,
items = {
2013-06-03 01:15:40 +02:00
{items = {'farming:seed_wheat'},rarity = 5},
2012-11-11 11:47:53 +01:00
{items = {'default:grass_1'}},
}
2014-04-14 20:26:30 +02:00
}})
2012-11-11 11:47:53 +01:00
end
2014-04-14 20:26:30 +02:00
minetest.override_item("default:junglegrass", {drop = {
2012-11-11 11:47:53 +01:00
max_items = 1,
items = {
2013-06-03 01:15:40 +02:00
{items = {'farming:seed_cotton'},rarity = 8},
2012-11-11 11:47:53 +01:00
{items = {'default:junglegrass'}},
}
2014-04-14 20:26:30 +02:00
}})
2012-11-11 11:47:53 +01:00
--
-- Place seeds
--
local function place_seed(itemstack, placer, pointed_thing, plantname)
local pt = pointed_thing
-- check if pointing at a node
if not pt then
return
end
if pt.type ~= "node" then
return
end
2013-05-25 00:40:03 +02:00
local under = minetest.get_node(pt.under)
local above = minetest.get_node(pt.above)
2012-11-11 11:47:53 +01:00
-- return if any of the nodes is not registered
if not minetest.registered_nodes[under.name] then
return
end
if not minetest.registered_nodes[above.name] then
return
end
-- check if pointing at the top of the node
if pt.above.y ~= pt.under.y+1 then
return
end
-- check if you can replace the node above the pointed node
if not minetest.registered_nodes[above.name].buildable_to then
return
end
-- check if pointing at soil
if minetest.get_item_group(under.name, "soil") <= 1 then
return
end
-- add the node and remove 1 item from the itemstack
2013-05-25 00:40:03 +02:00
minetest.add_node(pt.above, {name=plantname})
2012-11-11 11:47:53 +01:00
if not minetest.setting_getbool("creative_mode") then
itemstack:take_item()
end
return itemstack
end
--
-- Wheat
--
minetest.register_craftitem("farming:seed_wheat", {
description = "Wheat Seed",
inventory_image = "farming_wheat_seed.png",
on_place = function(itemstack, placer, pointed_thing)
return place_seed(itemstack, placer, pointed_thing, "farming:wheat_1")
end,
})
minetest.register_craftitem("farming:wheat", {
description = "Wheat",
inventory_image = "farming_wheat.png",
})
minetest.register_craftitem("farming:flour", {
description = "Flour",
inventory_image = "farming_flour.png",
})
minetest.register_craftitem("farming:bread", {
description = "Bread",
inventory_image = "farming_bread.png",
on_use = minetest.item_eat(4),
})
minetest.register_craft({
type = "shapeless",
output = "farming:flour",
recipe = {"farming:wheat", "farming:wheat", "farming:wheat", "farming:wheat"}
})
minetest.register_craft({
type = "cooking",
cooktime = 15,
output = "farming:bread",
recipe = "farming:flour"
})
for i=1,8 do
local drop = {
items = {
{items = {'farming:wheat'},rarity=9-i},
{items = {'farming:wheat'},rarity=18-i*2},
{items = {'farming:seed_wheat'},rarity=9-i},
{items = {'farming:seed_wheat'},rarity=18-i*2},
}
}
minetest.register_node("farming:wheat_"..i, {
drawtype = "plantlike",
waving = 1,
2012-11-11 11:47:53 +01:00
tiles = {"farming_wheat_"..i..".png"},
paramtype = "light",
walkable = false,
buildable_to = true,
is_ground_content = true,
drop = drop,
selection_box = {
type = "fixed",
fixed = {-0.5, -0.5, -0.5, 0.5, -5/16, 0.5},
},
groups = {snappy=3,flammable=2,plant=1,wheat=i,not_in_creative_inventory=1,attached_node=1},
sounds = default.node_sound_leaves_defaults(),
})
end
minetest.register_abm({
nodenames = {"group:wheat"},
neighbors = {"group:soil"},
interval = 90,
chance = 2,
action = function(pos, node)
-- return if already full grown
if minetest.get_item_group(node.name, "wheat") == 8 then
return
end
-- check if on wet soil
pos.y = pos.y-1
2013-05-25 00:40:03 +02:00
local n = minetest.get_node(pos)
2012-11-11 11:47:53 +01:00
if minetest.get_item_group(n.name, "soil") < 3 then
return
end
pos.y = pos.y+1
-- check light
2013-05-25 00:40:03 +02:00
if not minetest.get_node_light(pos) then
2012-11-11 11:47:53 +01:00
return
end
2013-05-25 00:40:03 +02:00
if minetest.get_node_light(pos) < 13 then
2012-11-11 11:47:53 +01:00
return
end
-- grow
local height = minetest.get_item_group(node.name, "wheat") + 1
2013-05-25 00:40:03 +02:00
minetest.set_node(pos, {name="farming:wheat_"..height})
2012-11-11 11:47:53 +01:00
end
})
--
-- Cotton
--
minetest.register_craftitem("farming:seed_cotton", {
description = "Cotton Seed",
inventory_image = "farming_cotton_seed.png",
on_place = function(itemstack, placer, pointed_thing)
return place_seed(itemstack, placer, pointed_thing, "farming:cotton_1")
end,
})
minetest.register_craftitem("farming:string", {
description = "String",
inventory_image = "farming_string.png",
})
minetest.register_craft({
output = "wool:white",
recipe = {
{"farming:string", "farming:string"},
{"farming:string", "farming:string"},
}
})
for i=1,8 do
local drop = {
items = {
{items = {'farming:string'},rarity=9-i},
{items = {'farming:string'},rarity=18-i*2},
{items = {'farming:string'},rarity=27-i*3},
{items = {'farming:seed_cotton'},rarity=9-i},
{items = {'farming:seed_cotton'},rarity=18-i*2},
{items = {'farming:seed_cotton'},rarity=27-i*3},
}
}
minetest.register_node("farming:cotton_"..i, {
drawtype = "plantlike",
waving = 1,
2012-11-11 11:47:53 +01:00
tiles = {"farming_cotton_"..i..".png"},
paramtype = "light",
walkable = false,
buildable_to = true,
is_ground_content = true,
drop = drop,
selection_box = {
type = "fixed",
fixed = {-0.5, -0.5, -0.5, 0.5, -5/16, 0.5},
},
groups = {snappy=3,flammable=2,plant=1,cotton=i,not_in_creative_inventory=1,attached_node=1},
sounds = default.node_sound_leaves_defaults(),
})
end
minetest.register_abm({
nodenames = {"group:cotton"},
neighbors = {"group:soil"},
interval = 80,
chance = 2,
action = function(pos, node)
-- return if already full grown
if minetest.get_item_group(node.name, "cotton") == 8 then
return
end
-- check if on wet soil
pos.y = pos.y-1
2013-05-25 00:40:03 +02:00
local n = minetest.get_node(pos)
2012-11-11 11:47:53 +01:00
if minetest.get_item_group(n.name, "soil") < 3 then
return
end
pos.y = pos.y+1
-- check light
2013-05-25 00:40:03 +02:00
if not minetest.get_node_light(pos) then
2012-11-11 11:47:53 +01:00
return
end
2013-05-25 00:40:03 +02:00
if minetest.get_node_light(pos) < 13 then
2012-11-11 11:47:53 +01:00
return
end
-- grow
local height = minetest.get_item_group(node.name, "cotton") + 1
2013-05-25 00:40:03 +02:00
minetest.set_node(pos, {name="farming:cotton_"..height})
2012-11-11 11:47:53 +01:00
end
})