cleaned the code of bushes_classic into conventionnal LUA style, for readability

This commit is contained in:
Gael-de-Sailly 2015-01-25 10:00:42 +01:00
parent 3eaf687da4
commit ecf175fb9c

View File

@ -4,88 +4,90 @@ plantlife_bushes = {}
-- TODO: add support for nodebreakers? those dig like mese picks -- TODO: add support for nodebreakers? those dig like mese picks
plantlife_bushes.after_dig_node = function(pos, oldnode, oldmetadata, digger) plantlife_bushes.after_dig_node = function(pos, oldnode, oldmetadata, digger)
if( not( digger ) or not( pos ) or not (oldnode )) then if not (digger and pos and oldnode) then
return nil; return
end end
-- find out which bush type we are dealing with -- find out which bush type we are dealing with
local bush_name = ""; local bush_name = ""
local can_harvest = false; local can_harvest = false
if( oldnode.name == 'bushes:fruitless_bush' ) then if oldnode.name == "bushes:fruitless_bush" then
-- this bush has not grown fruits yet (but will eventually) -- this bush has not grown fruits yet (but will eventually)
bush_name = oldmetadata[ 'fields' ][ 'bush_type' ]; bush_name = oldmetadata.fields.bush_type
-- no fruits to be found, so can_harvest stays false -- no fruits to be found, so can_harvest stays false
else else
local name_parts = oldnode.name:split( ":" ); local name_parts = oldnode.name:split(":")
if( #name_parts >= 2 and name_parts[2]~=nil ) then if #name_parts >= 2 and name_parts[2] ~= nil then
name_parts = name_parts[2]:split( "_" ); name_parts = name_parts[2]:split("_")
if( #name_parts >= 2 and name_parts[1]~=nil ) then if #name_parts >= 2 and name_parts[1] ~= nil then
bush_name = name_parts[1]; bush_name = name_parts[1]
-- this bush really carries fruits -- this bush really carries fruits
can_harvest = true; can_harvest = true
end end
end end
end end
-- find out which tool the digger was wielding (if any) -- find out which tool the digger was wielding (if any)
local toolstack = digger:get_wielded_item(); local toolstack = digger:get_wielded_item()
local capabilities = toolstack:get_tool_capabilities(); local capabilities = toolstack:get_tool_capabilities()
-- what the player will get -- what the player will get
local harvested = ""; local harvested
local amount = "";
-- failure to find out what the tool can do: destroy the bush and return nothing -- failure to find out what the tool can do: destroy the bush and return nothing
if( not( capabilities["groupcaps"] )) then local groupcaps = capabilities.groupcaps
return nil; if not groupcaps then
return
-- digging with the hand or something like that -- digging with the hand or something like that
elseif( capabilities["groupcaps"]["snappy"] ) then elseif groupcaps.snappy then
-- plant a new bush without fruits -- plant a new bush without fruits
minetest.set_node(pos,{type='node',name='bushes:fruitless_bush'}) minetest.set_node(pos, {type = "node", name = "bushes:fruitless_bush"})
local meta = minetest.get_meta( pos ); local meta = minetest.get_meta(pos)
meta:set_string( 'bush_type', bush_name ); meta:set_string('bush_type', bush_name)
-- construct the stack of fruits the player will get -- construct the stack of fruits the player will get
-- only bushes that have grown fruits can actually give fruits -- only bushes that have grown fruits can actually give fruits
if( can_harvest == true ) then if can_harvest then
amount = "4"; local amount = "4"
harvested = "bushes:"..bush_name.." "..amount; harvested = "bushes:" .. bush_name .. " " .. amount
end end
-- something like a shovel -- something like a shovel
elseif( capabilities["groupcaps"]["crumbly"] ) then elseif groupcaps.crumbly then
-- with a chance of 1/3, return 2 bushes -- with a chance of 1/3, return 2 bushes
if( math.random(1,3)==1 ) then local amount
amount = "2"; if math.random(1,3) == 1 then
amount = "2"
else else
amount = "1"; amount = "1"
end end
-- return the bush itself -- return the bush itself
harvested = "bushes:" .. bush_name .. "_bush "..amount; harvested = "bushes:" .. bush_name .. "_bush "..amount
-- something like an axe -- something like an axe
elseif( capabilities["groupcaps"]["choppy"] ) then elseif groupcaps.choppy then
-- the amount of sticks may vary -- the amount of sticks may vary
amount = math.random( 4, 20 ); local amount = math.random(4, 20)
-- return some sticks -- return some sticks
harvested = "default:stick "..amount; harvested = "default:stick " .. amount
-- nothing known - destroy the plant -- nothing known - destroy the plant
else else
return nil; return
end end
-- give the harvested result to the player -- give the harvested result to the player
if( harvested ~= "" ) then if harvested then
--minetest.chat_send_player("singleplayer","you would now get "..tostring( harvested ) ); --minetest.chat_send_player("singleplayer","you would now get "..tostring( harvested ) );
digger:get_inventory():add_item( "main", harvested ); local inventory = digger:get_inventory()
inventory:add_item( "main", harvested );
end end
end end
@ -93,26 +95,24 @@ end
plantlife_bushes.after_place_node = function(pos, placer, itemstack) plantlife_bushes.after_place_node = function(pos, placer, itemstack)
if( not( itemstack ) or not( pos )) then if not (itemstack and pos) then
return nil; return
end end
local name_parts = itemstack:get_name():split( ":" ); local name_parts = itemstack:get_name():split(":")
if( #name_parts <2 or name_parts[2]==nil ) then if #name_parts < 2 or name_parts[2] == nil then
return nil; return
end end
name_parts = name_parts[2]:split( "_" ); name_parts = name_parts[2]:split("_")
if( #name_parts <2 or name_parts[1]==nil ) then if #name_parts < 2 or name_parts[1] == nil then
return nil; return
end end
minetest.set_node( pos, {type='node',name='bushes:fruitless_bush'}); minetest.set_node(pos, {name = "bushes:fruitless_bush"})
local meta = minetest.get_meta( pos ); local meta = minetest.get_meta(pos)
meta:set_string( 'bush_type', name_parts[1] ); meta:set_string("bush_type", name_parts[1])
return nil;
end end
@ -120,22 +120,22 @@ end
-- regrow berries (uses a base abm instead of plants_lib because of the use of metadata). -- regrow berries (uses a base abm instead of plants_lib because of the use of metadata).
minetest.register_abm({ minetest.register_abm({
nodenames = { "bushes:fruitless_bush" }, nodenames = {"bushes:fruitless_bush"},
neighbors = { "group:soil", "group:potting_soil" }, neighbors = {"group:soil", "group:potting_soil"},
interval = 500, interval = 500,
chance = 5, chance = 5,
action = function(pos, node, active_object_count, active_object_count_wider) action = function(pos, node, active_object_count, active_object_count_wider)
local meta = minetest.get_meta( pos ); local meta = minetest.get_meta(pos)
local bush_name = meta:get_string( 'bush_type' ); local bush_name = meta:get_string("bush_type")
if( bush_name ~= nil and bush_name ~= '' ) then if bush_name and bush_name ~= "" then
local dirtpos = { x = pos.x, y = pos.y-1, z = pos.z } local dirtpos = {x = pos.x, y = pos.y-1, z = pos.z}
local dirt = minetest.get_node(dirtpos) local dirt = minetest.get_node(dirtpos)
local is_soil = minetest.get_item_group(dirt.name, "soil") or minetest.get_item_group(dirt.name, "potting_soil") local is_soil = minetest.get_item_group(dirt.name, "soil") or minetest.get_item_group(dirt.name, "potting_soil")
if is_soil and (dirt.name == "farming:soil_wet" or math.random(1,3) == 1) then if is_soil and (dirt.name == "farming:soil_wet" or math.random(1,3) == 1) then
minetest.set_node( pos, {type='node',name='bushes:'..bush_name..'_bush'}) minetest.set_node( pos, {name = "bushes:" .. bush_name .. "_bush"})
end end
end end
end end