short farming code and reduce farming texture sizes (adv- and optipng)

This commit is contained in:
HybridDog 2015-08-21 12:20:38 +02:00 committed by HybridDog
parent 9e54b379c8
commit 8f7299271f
19 changed files with 181 additions and 195 deletions

View file

@ -142,21 +142,17 @@ farming.register_plant(name, Plant definition)
---------------
{
description = "", -- Description for tooltip
inventory_image = "unknown_item.png", -- Image to be used as wield- and inventory image
inventory_image = , -- Image to be used as wield- and inventory image
max_uses = 30, -- Uses until destroyed
material = "", -- Material for recipes
recipe = { -- Craft recipe, if material isn't used
{"air", "air", "air"},
{"", "group:stick"},
{"", "group:stick"},
}
recipe = , -- Craft recipe, if material isn't used
}
#Plant definition
-----------------
{
description = "", -- Description of seed item
inventory_image = "unknown_item.png", -- Image to be used as seed's wield- and inventory image
inventory_image = , -- Image to be used as seed's wield- and inventory image
steps = 8, -- How many steps the plant has to grow, until it can be harvested
^ Always provide a plant texture for each step, format: modname_plantname_i.png (i = stepnumber)
minlight = 13, -- Minimum light to grow

View file

@ -1,43 +1,35 @@
-- Wear out hoes, place soil
-- TODO Ignore group:flower
farming.hoe_on_use = function(itemstack, user, pointed_thing, uses)
local pt = pointed_thing
-- check if pointing at a node
if not pt then
local creative = minetest.setting_getbool("creative_mode")
function farming.hoe_on_use(itemstack, user, pt, uses)
-- check if pointing at a node's top
if not pt
or pt.type ~= "node"
or pt.above.y ~= pt.under.y+1 then
return
end
if pt.type ~= "node" then
local above = minetest.get_node(pt.above)
if above.name ~= "air"
or not minetest.registered_nodes[above.name] then
return
end
local under = minetest.get_node(pt.under)
local p = {x=pt.under.x, y=pt.under.y+1, z=pt.under.z}
local above = minetest.get_node(p)
-- return if any of the nodes is not registered
if not minetest.registered_nodes[under.name] then
if not minetest.registered_nodes[under.name]
or minetest.get_item_group(under.name, "soil") ~= 1 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 soil
if minetest.get_item_group(under.name, "soil") ~= 1 then
return
end
-- check if (wet) soil defined
local regN = minetest.registered_nodes
if regN[under.name].soil == nil or regN[under.name].soil.wet == nil or regN[under.name].soil.dry == nil then
local soil = minetest.registered_nodes[under.name].soil
if not soil
or not soil.wet
or not soil.dry then
return
end
if minetest.is_protected(pt.under, user:get_player_name()) then
minetest.record_protection_violation(pt.under, user:get_player_name())
return
@ -47,41 +39,33 @@ farming.hoe_on_use = function(itemstack, user, pointed_thing, uses)
return
end
-- turn the node into soil, wear out item and play sound
minetest.set_node(pt.under, {name = regN[under.name].soil.dry})
minetest.set_node(pt.under, {name = soil.dry})
minetest.sound_play("default_dig_crumbly", {
pos = pt.under,
gain = 0.5,
})
if not minetest.setting_getbool("creative_mode") then
itemstack:add_wear(65535/(uses-1))
if creative then
return
end
itemstack:add_wear(65535/(uses-1))
return itemstack
end
-- Register new hoes
farming.register_hoe = function(name, def)
function farming.register_hoe(name, def)
-- Check for : prefix (register new hoes in your mod's namespace)
if name:sub(1,1) ~= ":" then
name = ":" .. name
end
-- Check def table
if def.description == nil then
if not def.description then
def.description = "Hoe"
end
if def.inventory_image == nil then
def.inventory_image = "unknown_item.png"
end
if def.recipe == nil then
def.recipe = {
{"air","air",""},
{"","group:stick",""},
{"","group:stick",""}
}
end
if def.max_uses == nil then
assert(def.inventory_image, "[farming] missing field inventory_image ("..name..")")
if not def.max_uses then
def.max_uses = 30
end
-- Register the tool
@ -93,108 +77,86 @@ farming.register_hoe = function(name, def)
end
})
-- Register its recipe
if def.material == nil then
minetest.register_craft({
output = name:sub(2),
recipe = def.recipe
})
else
minetest.register_craft({
output = name:sub(2),
recipe = {
{def.material, def.material, ""},
{"", "group:stick", ""},
{"", "group:stick", ""}
}
})
-- Reverse Recipe
minetest.register_craft({
output = name:sub(2),
recipe = {
{"", def.material, def.material},
{"", "group:stick", ""},
{"", "group:stick", ""}
}
})
if not def.material then
if def.recipe then
minetest.register_craft({
output = name:sub(2),
recipe = def.recipe
})
end
return
end
minetest.register_craft({
output = name:sub(2),
recipe = {
{def.material, def.material, ""},
{"", "group:stick", ""},
{"", "group:stick", ""}
}
})
-- Reverse Recipe
minetest.register_craft({
output = name:sub(2),
recipe = {
{"", def.material, def.material},
{"", "group:stick", ""},
{"", "group:stick", ""}
}
})
end
-- Seed placement
farming.place_seed = function(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
local under = minetest.get_node(pt.under)
local above = minetest.get_node(pt.above)
if minetest.is_protected(pt.under, placer:get_player_name()) then
minetest.record_protection_violation(pt.under, placer:get_player_name())
return
end
if minetest.is_protected(pt.above, placer:get_player_name()) then
minetest.record_protection_violation(pt.above, placer:get_player_name())
function farming.place_seed(itemstack, placer, pt, plantname)
-- check if pointing at a node's top
if not pt
or pt.type ~= "node"
or pt.above.y ~= pt.under.y+1 then
return
end
-- return if any of the nodes is not registered
if not minetest.registered_nodes[under.name] then
local playername = placer:get_player_name()
if minetest.is_protected(pt.under, playername) then
minetest.record_protection_violation(pt.under, playername)
return
end
if not minetest.registered_nodes[above.name] then
if minetest.is_protected(pt.above, playername) then
minetest.record_protection_violation(pt.above, playername)
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
local above = minetest.get_node(pt.above)
if not minetest.registered_nodes[above.name]
or not minetest.registered_nodes[above.name].buildable_to then
return
end
-- check if pointing at soil
if minetest.get_item_group(under.name, "soil") < 2 then
local under = minetest.get_node(pt.under)
if not minetest.registered_nodes[under.name]
or minetest.get_item_group(under.name, "soil") < 2 then
return
end
-- add the node and remove 1 item from the itemstack
minetest.add_node(pt.above, {name = plantname, param2 = 1})
if not minetest.setting_getbool("creative_mode") then
itemstack:take_item()
if creative then
return
end
itemstack:take_item()
return itemstack
end
-- Register plants
farming.register_plant = function(name, def)
local mname = name:split(":")[1]
local pname = name:split(":")[2]
function farming.register_plant(name, def)
if not def.steps then
return
end
-- Check def table
if not def.description then
def.description = "Seed"
end
if not def.inventory_image then
def.inventory_image = "unknown_item.png"
end
if not def.steps then
return nil
end
if not def.minlight then
def.minlight = 1
end
if not def.maxlight then
def.maxlight = 14
end
assert(def.inventory_image, "[farming] missing field inventory_image ("..name..")")
if not def.fertility then
def.fertility = {}
end
@ -204,8 +166,11 @@ farming.register_plant = function(name, def)
for k, v in pairs(def.fertility) do
g[v] = 1
end
local mname, pname = unpack(name:split(":"))
minetest.register_node(":" .. mname .. ":seed_" .. pname, {
description = def.description,
description = def.description or "Seed",
tiles = {def.inventory_image},
inventory_image = def.inventory_image,
wield_image = def.inventory_image,
@ -233,16 +198,9 @@ farming.register_plant = function(name, def)
-- Register growing steps
for i=1,def.steps do
local drop = {
items = {
{items = {mname .. ":" .. pname}, rarity = 9 - i},
{items = {mname .. ":" .. pname}, rarity= 18 - i * 2},
{items = {mname .. ":seed_" .. pname}, rarity = 9 - i},
{items = {mname .. ":seed_" .. pname}, rarity = 18 - i * 2},
}
}
local nodegroups = {snappy = 3, flammable = 2, plant = 1, not_in_creative_inventory = 1, attached_node = 1}
nodegroups[pname] = i
minetest.register_node(mname .. ":" .. pname .. "_" .. i, {
drawtype = "plantlike",
waving = 1,
@ -250,7 +208,14 @@ farming.register_plant = function(name, def)
paramtype = "light",
walkable = false,
buildable_to = true,
drop = drop,
drop = {
items = {
{items = {mname .. ":" .. pname}, rarity = 9 - i},
{items = {mname .. ":" .. pname}, rarity= 18 - i * 2},
{items = {mname .. ":seed_" .. pname}, rarity = 9 - i},
{items = {mname .. ":seed_" .. pname}, rarity = 18 - i * 2},
}
},
selection_box = {
type = "fixed",
fixed = {-0.5, -0.5, -0.5, 0.5, -5/16, 0.5},
@ -260,6 +225,13 @@ farming.register_plant = function(name, def)
})
end
if not def.minlight then
def.minlight = 1
end
if not def.maxlight then
def.maxlight = 14
end
-- Growing ABM
minetest.register_abm({
nodenames = {"group:" .. pname, "group:seed"},
@ -267,57 +239,61 @@ farming.register_plant = function(name, def)
interval = 90,
chance = 2,
action = function(pos, node)
local plant_height = minetest.get_item_group(node.name, pname)
-- return if already full grown
if plant_height == def.steps then
if minetest.get_item_group(node.name, pname) == def.steps then
return
end
local node_def = minetest.registered_items[node.name] or nil
pos.y = pos.y-1
local soil_node = minetest.get_node_or_nil(pos)
if not soil_node then
return
end
pos.y = pos.y+1
local fertility = minetest.registered_items[node.name].fertility
-- grow seed
if minetest.get_item_group(node.name, "seed") and node_def.fertility then
local can_grow = false
local soil_node = minetest.get_node_or_nil({x = pos.x, y = pos.y - 1, z = pos.z})
if not soil_node then
return
end
for _, v in pairs(node_def.fertility) do
if fertility
and minetest.get_item_group(node.name, "seed") then
local can_grow
for _, v in pairs(fertility) do
if minetest.get_item_group(soil_node.name, v) ~= 0 then
can_grow = true
break
end
end
if can_grow then
minetest.set_node(pos, {name = node.name:gsub("seed_", "") .. "_1"})
if not can_grow then
return
end
node.name = node.name:gsub("seed_", "") .. "_1"
minetest.set_node(pos, node)
return
end
-- check if on wet soil
pos.y = pos.y - 1
local n = minetest.get_node(pos)
if minetest.get_item_group(n.name, "soil") < 3 then
if minetest.get_item_group(soil_node.name, "soil") < 3 then
return
end
pos.y = pos.y + 1
-- check light
local ll = minetest.get_node_light(pos)
if not ll or ll < def.minlight or ll > def.maxlight then
if not ll
or ll < def.minlight
or ll > def.maxlight then
return
end
-- grow
minetest.set_node(pos, {name = mname .. ":" .. pname .. "_" .. plant_height + 1})
node.name = mname .. ":" .. pname .. "_" .. plant_height + 1
minetest.set_node(pos, node)
end
})
-- Return
local r = {
return {
seed = mname .. ":seed_" .. pname,
harvest = mname .. ":" .. pname
}
return r
end

View file

@ -89,64 +89,78 @@ minetest.register_abm({
interval = 15,
chance = 4,
action = function(pos, node)
local n_def = minetest.registered_nodes[node.name] or nil
local wet = n_def.soil.wet or nil
local base = n_def.soil.base or nil
local dry = n_def.soil.dry or nil
if not n_def or not n_def.soil or not wet or not base or not dry then
local soil = minetest.registered_nodes[node.name].soil
if not soil then
return
end
local wet = soil.wet
local base = soil.base
local dry = soil.dry
if not (wet and base and dry) then
return
end
pos.y = pos.y + 1
local nn = minetest.get_node_or_nil(pos)
if not nn or not nn.name then
if not nn then
return
end
local nn_def = minetest.registered_nodes[nn.name] or nil
local nn_def = minetest.registered_nodes[nn.name]
pos.y = pos.y - 1
if nn_def and nn_def.walkable and minetest.get_item_group(nn.name, "plant") == 0 then
minetest.set_node(pos, {name = base})
if nn_def
and nn_def.walkable
and minetest.get_item_group(nn.name, "plant") == 0 then
node.name = base
minetest.set_node(pos, node)
return
end
-- check if there is water nearby
local wet_lvl = minetest.get_item_group(node.name, "wet")
if minetest.find_node_near(pos, 3, {"group:water"}) then
-- if it is dry soil and not base node, turn it into wet soil
if wet_lvl == 0 then
minetest.set_node(pos, {name = wet})
node.name = wet
minetest.set_node(pos, node)
end
else
-- only turn back if there are no unloaded blocks (and therefore
-- possible water sources) nearby
if not minetest.find_node_near(pos, 3, {"ignore"}) then
-- turn it back into base if it is already dry
if wet_lvl == 0 then
-- only turn it back if there is no plant/seed on top of it
if minetest.get_item_group(nn.name, "plant") == 0 and minetest.get_item_group(nn.name, "seed") == 0 then
minetest.set_node(pos, {name = base})
end
-- if its wet turn it back into dry soil
elseif wet_lvl == 1 then
minetest.set_node(pos, {name = dry})
end
return
end
-- only turn back if there are no unloaded blocks (and therefore
-- possible water sources) nearby
if minetest.find_node_near(pos, 3, {"ignore"}) then
return
end
-- turn it back into base if it is already dry
if wet_lvl == 0 then
-- only turn it back if there is no plant/seed on top of it
if minetest.get_item_group(nn.name, "plant") == 0
and minetest.get_item_group(nn.name, "seed") == 0 then
node.name = base
minetest.set_node(pos, node)
end
-- if its wet turn it back into dry soil
elseif wet_lvl == 1 then
node.name = dry
minetest.set_node(pos, node)
end
end,
})
for i = 1, 5 do
minetest.override_item("default:grass_"..i, {drop = {
max_items = 1,
items = {
{items = {'farming:seed_wheat'},rarity = 5},
{items = {'default:grass_1'}},
}
}})
local defchange = {drop = {
max_items = 1,
items = {
{items = {'farming:seed_wheat'},rarity = 5},
{items = {'default:grass_1'}},
}
}}
for i = 1, 5 do
minetest.override_item("default:grass_"..i, defchange)
end
minetest.override_item("default:junglegrass", {drop = {
max_items = 1,
items = {

Binary file not shown.

Before

Width:  |  Height:  |  Size: 393 B

After

Width:  |  Height:  |  Size: 388 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 159 B

After

Width:  |  Height:  |  Size: 158 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 170 B

After

Width:  |  Height:  |  Size: 163 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 196 B

After

Width:  |  Height:  |  Size: 192 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 467 B

After

Width:  |  Height:  |  Size: 467 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 351 B

After

Width:  |  Height:  |  Size: 351 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 706 B

After

Width:  |  Height:  |  Size: 703 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 677 B

After

Width:  |  Height:  |  Size: 676 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 212 B

After

Width:  |  Height:  |  Size: 212 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 241 B

After

Width:  |  Height:  |  Size: 240 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 172 B

After

Width:  |  Height:  |  Size: 168 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 191 B

After

Width:  |  Height:  |  Size: 185 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 218 B

After

Width:  |  Height:  |  Size: 208 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 234 B

After

Width:  |  Height:  |  Size: 223 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 253 B

After

Width:  |  Height:  |  Size: 240 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 310 B

After

Width:  |  Height:  |  Size: 310 B