Reviewed to comply with lua guidelines

This commit is contained in:
h-v-smacker 2017-10-27 22:03:43 +03:00
parent be5bb54bc7
commit 6190b6cafa

View File

@ -1,27 +1,33 @@
--[[
The Lawn Trimmer, also known as Weed Whacker, is a common gardening power tool.
In minetest, it has several uses. While it removes all members of 'flora' group
and can be used for literally mowing grass or trimming it around vegetable beds,
it's not its most important application.
The Lawn Trimmer, also known as Weed Whacker, is a common gardening power
tool. In minetest, it has several uses. While it removes all members of
'flora' group and can be used for literally mowing grass or trimming it
around vegetable beds, it's not its most important application.
1. The tool can be used when searching for plants that can be cultivated in the wilderness.
Some of them are hard to see through grass; some of them are hard to tell from the grass;
some of them are actually obtained by removing the grass (e.g. barley seeds).
1. The tool can be used when searching for plants that can be cultivated
in the wilderness. Some of them are hard to see through grass; some of
them are hard to tell from the grass; some of them are actually obtained
by removing the grass (e.g. barley seeds).
2. Producing organic dye pigments. While growing flowers is a matter of fertilizing the soil
with bone meal, harvesting them by hand is a chore.
2. Producing organic dye pigments. While growing flowers is a matter of
fertilizing the soil with bone meal, harvesting them by hand is a chore.
In both scenarios, the tool will be very handy for the player.
It comes with 4 modes of operation, defined by how wide its sweep is: from 0 (at one's feet)
to 3 nodes in radius (square radius, as most things in minetest are).
It comes with 4 modes of operation, defined by how wide its sweep is:
from 0 (at one's feet) to 3 nodes in radius (square radius, as most
things in minetest are).
The sound is an edited fragment from https://www.cutestockfootage.com/sound-effect/9251/grass-trimmer-01
used in accordance with its licensing terms (free use for any purpose in an altered form)
The sound is an edited fragment from
https://www.cutestockfootage.com/sound-effect/9251/grass-trimmer-01
used in accordance with its licensing terms (free use for any purpose in
an altered form)
]]
-- Configuration
local lawn_trimmer_max_charge = 10000 -- default: 10000 - Same as the chainsaw
local lawn_trimmer_charge_per_object = 25 -- default: 25 - Can mow 400 'group:flora' blocks
-- Intended to hold as much as the chainsaw, 10000 units
local lawn_trimmer_max_charge = 10000
-- With 25 units per object can mow 400 'group:flora' blocks
local lawn_trimmer_charge_per_object = 25
local S = technic.getter
@ -35,23 +41,24 @@ local lawn_trimmer_mode_text = {
-- Mode switcher for the tool
local function lawn_trimmer_setmode(user,itemstack)
local player_name=user:get_player_name()
local meta=minetest.deserialize(itemstack:get_metadata())
local player_name = user:get_player_name()
local meta = minetest.deserialize(itemstack:get_metadata())
if meta == nil then
if not meta then
meta = {mode = 0}
end
if meta.mode == nil then
minetest.chat_send_player(player_name, S("Use while sneaking to change Lawn Trimmer modes."))
if not meta.mode then
minetest.chat_send_player(player_name,
S("Use while sneaking to change Lawn Trimmer modes."))
meta.mode = 0
end
meta.mode = meta.mode + 1
if meta.mode > 4 then meta.mode = 1 end
if meta.mode > 4 then
meta.mode = 1
end
minetest.chat_send_player(player_name, S("Lawn Trimmer Mode %d"):format(meta.mode) .. ": " .. lawn_trimmer_mode_text[meta.mode])
minetest.chat_send_player(player_name,
S("Lawn Trimmer Mode %d"):format(meta.mode) .. ": "
.. lawn_trimmer_mode_text[meta.mode])
itemstack:set_name("technic:lawn_trimmer_" .. meta.mode);
itemstack:set_metadata(minetest.serialize(meta))
return itemstack
@ -66,10 +73,10 @@ local function trim_the_lawn(itemstack, user)
if not meta or not meta.mode or keys.sneak then
return lawn_trimmer_setmode(user, itemstack)
end
if not meta or not meta.charge then
return
end
if meta.charge > lawn_trimmer_charge_per_object then
minetest.sound_play("technic_lawn_trimmer", {
to_player = user:get_player_name(),
@ -80,28 +87,34 @@ local function trim_the_lawn(itemstack, user)
local pos = user:getpos()
local inv = user:get_inventory()
-- Defining the area for the search needs two positions
-- The tool has a limited range in the vertical axis, which is capped at +/- 1 node
local start_pos = {
x = pos.x - meta.mode + 1,
z = pos.z - meta.mode + 1,
y = pos.y - 1
} -- the tool can hardly be lowered a lot while standing, but we'll allow 1 node
}
local end_pos = {
x = pos.x + meta.mode - 1,
z = pos.z + meta.mode - 1,
y = pos.y + 1
} -- cannot be raised too high either, though mostly for safety reasons
}
-- Since nodes sometimes cannot be removed, we cannot rely on repeating find_node_near() and removing found nodes
-- Since nodes sometimes cannot be removed, we cannot rely on repeating
-- find_node_near() and removing found nodes
local found_flora = minetest.find_nodes_in_area(start_pos, end_pos, {"group:flora"})
for _,f in ipairs(found_flora) do
if meta.charge < lawn_trimmer_charge_per_object then break end -- no charge left for this node
if minetest.is_protected(f, user:get_player_name()) then break end -- may not dig this node
-- Abort if no charge left for a node
if meta.charge < lawn_trimmer_charge_per_object then break end
-- Skip to the next one if this node cannot be dug
if not minetest.is_protected(f, user:get_player_name()) then
meta.charge = meta.charge - lawn_trimmer_charge_per_object
local node = minetest.get_node_or_nil(f)
minetest.node_dig(f, node, user)
end
end
-- the charge won't expire in creative mode, but the tool still has to be charged prior to use
-- The charge won't expire in creative mode, but the tool still
-- has to be charged prior to use
if not technic.creative_mode then
technic.set_RE_wear(itemstack, meta.charge, lawn_trimmer_max_charge)
itemstack:set_metadata(minetest.serialize(meta))
@ -121,29 +134,30 @@ minetest.register_tool("technic:lawn_trimmer", {
on_use = function(itemstack, user, pointed_thing)
trim_the_lawn(itemstack, user)
return itemstack
end,
end
})
for i=1,4,1 do
technic.register_power_tool("technic:lawn_trimmer_"..i, lawn_trimmer_max_charge)
minetest.register_tool("technic:lawn_trimmer_"..i, {
for i = 1,4,1 do
technic.register_power_tool("technic:lawn_trimmer_" .. i, lawn_trimmer_max_charge)
minetest.register_tool("technic:lawn_trimmer_" .. i, {
description = S("Lawn Trimmer Mode %d"):format(i),
inventory_image = "technic_lawn_trimmer.png^technic_tool_mode"..i..".png",
inventory_image = "technic_lawn_trimmer.png^technic_tool_mode" .. i .. ".png",
wield_image = "technic_lawn_trimmer.png",
wear_represents = "technic_RE_charge",
on_refill = technic.refill_RE_charge,
groups = {not_in_creative_inventory=1},
groups = {not_in_creative_inventory = 1},
on_use = function(itemstack, user, pointed_thing)
trim_the_lawn(itemstack, user)
return itemstack
end,
end
})
end
-- Provide a crafting recipe
local mesecons_button = minetest.get_modpath("mesecons_button")
local trigger = mesecons_button and "mesecons_button:button_off" or "default:mese_crystal_fragment"
local trigger = mesecons_button and "mesecons_button:button_off"
or "default:mese_crystal_fragment"
minetest.register_craft({
output = 'technic:lawn_trimmer',