mirror of
https://git.0x7be.net/dirk/uniham.git
synced 2024-11-22 07:13:49 +01:00
modularize the mod for easier addition of hammers
This commit is contained in:
parent
eaa7f3afea
commit
d5e48bdc32
154
init.lua
154
init.lua
@ -1,139 +1,27 @@
|
||||
local replacements = {
|
||||
-- default sandstone to sand
|
||||
['default:sandstone'] = 'default:sand',
|
||||
['default:desert_sandstone'] = 'default:desert_sand',
|
||||
['default:silver_sandstone'] = 'default:silver_sand',
|
||||
-- Init
|
||||
uniham = {}
|
||||
uniham.modpath = minetest.get_modpath(minetest.get_current_modname())
|
||||
uniham.syspath = uniham.modpath..DIR_DELIM..'system'
|
||||
|
||||
-- stone crushing
|
||||
['default:obsidian'] = 'default:stone',
|
||||
['default:stone'] = 'default:cobble',
|
||||
['default:cobble'] = 'default:gravel',
|
||||
['default:gravel'] = 'default:sand'
|
||||
|
||||
-- Register replacements for the hammer. Entry ID is the ID of the node to
|
||||
-- replace and entry value is the ID of the node to replace with.
|
||||
uniham.replacements = {
|
||||
['default:cobble'] = 'default:gravel',
|
||||
['default:desert_sandstone'] = 'default:desert_sand',
|
||||
['default:gravel'] = 'default:sand',
|
||||
['default:obsidian'] = 'default:stone',
|
||||
['default:sandstone'] = 'default:sand',
|
||||
['default:silver_sandstone'] = 'default:silver_sand',
|
||||
['default:stone'] = 'default:cobble',
|
||||
}
|
||||
|
||||
-- Hammer use function
|
||||
--
|
||||
-- The function is called when using the hammer. It either converts the node to
|
||||
-- its replacement or does nothing. When converting the modified (added wear)
|
||||
-- itemstack is returned.
|
||||
--
|
||||
-- @param itemstack The `itemstack` as per on_use definition
|
||||
-- @param player The `player` as per on_use definition
|
||||
-- @param pointed_thing The `pointed_thing` as per on_use definition
|
||||
-- @see <https://dev.minetest.net/on_use#on_use>
|
||||
-- @return itemstack|void
|
||||
local use_hammer = function (itemstack, player, pointed_thing)
|
||||
if pointed_thing.type == 'nothing' then return end
|
||||
local node_pos = minetest.get_pointed_thing_position(pointed_thing)
|
||||
local node_name = minetest.get_node(node_pos).name
|
||||
local node_sounds = minetest.registered_nodes[node_name].sounds
|
||||
|
||||
if not replacements[node_name] then
|
||||
minetest.sound_play(node_sounds.dug.name, {
|
||||
pos = node_pos,
|
||||
max_hear_distance = 8,
|
||||
gain = node_sounds.dug.gain,
|
||||
})
|
||||
return
|
||||
end
|
||||
|
||||
local replacement = replacements[node_name]
|
||||
local replacement_sounds = minetest.registered_nodes[replacement].sounds
|
||||
local uses = minetest.registered_tools[itemstack:get_name()]._uniham.uses
|
||||
|
||||
minetest.sound_play(replacement_sounds.dug.name, {
|
||||
pos = node_pos,
|
||||
max_hear_distance = 8,
|
||||
gain = replacement_sounds.dug.gain,
|
||||
})
|
||||
|
||||
minetest.set_node(node_pos, { name = replacement })
|
||||
minetest.check_for_falling(node_pos)
|
||||
|
||||
itemstack:add_wear(math.ceil(65535 / uses) + 1)
|
||||
return itemstack
|
||||
end
|
||||
|
||||
|
||||
-- Register a new hammer
|
||||
--
|
||||
-- definition = {
|
||||
-- name = 'Name of the hammer',
|
||||
-- head = 'head_material_texture.png',
|
||||
-- craft = 'crafting:material',
|
||||
-- uses = 123,
|
||||
-- }
|
||||
--
|
||||
-- @param short_id Unprefixed ID for the new hammer
|
||||
-- @param definition Definition table as described
|
||||
-- @return void
|
||||
local register_hammer = function (short_id, definition)
|
||||
local texture = '([combine:16x16:-1,1=+s)^((+h^+m)^[makealpha:0,0,0)'
|
||||
texture = texture:gsub('+.', {
|
||||
['+s'] = 'default_stick.png',
|
||||
['+h'] = definition.head,
|
||||
['+m'] = 'uniham_head_mask.png'
|
||||
})
|
||||
|
||||
minetest.register_tool('uniham:'..short_id, {
|
||||
description = definition.name,
|
||||
inventory_image = texture,
|
||||
_uniham = { uses = definition.uses },
|
||||
on_use = function(itemstack, player, pointed_thing)
|
||||
return use_hammer(itemstack, player, pointed_thing)
|
||||
end
|
||||
})
|
||||
|
||||
minetest.register_craft({
|
||||
output = 'uniham:'..short_id,
|
||||
recipe = {
|
||||
{ '', definition.craft, '' },
|
||||
{ '', 'default:stick', definition.craft },
|
||||
{ 'default:stick', '', '' }
|
||||
}
|
||||
})
|
||||
end
|
||||
|
||||
register_hammer('hammer_bronze', {
|
||||
name = 'Bronze Hammer',
|
||||
head = 'default_bronze_block.png',
|
||||
craft = 'default:bronze_ingot',
|
||||
uses = 100
|
||||
})
|
||||
|
||||
register_hammer('hammer_diamond', {
|
||||
name = 'Diamond Hammer',
|
||||
head = 'default_diamond_block.png',
|
||||
craft = 'default:diamond',
|
||||
uses = 200
|
||||
})
|
||||
|
||||
register_hammer('hammer_mese', {
|
||||
name = 'Mese Hammer',
|
||||
head = 'default_mese_block.png',
|
||||
craft = 'default:mese_crystal',
|
||||
uses = 300
|
||||
})
|
||||
|
||||
register_hammer('hammer_steel', {
|
||||
name = 'Steel Hammer',
|
||||
head = 'default_steel_block.png',
|
||||
craft = 'default:iron_ingot',
|
||||
uses = 150
|
||||
})
|
||||
|
||||
register_hammer('hammer_stone', {
|
||||
name = 'Stone Hammer',
|
||||
head = 'default_stone.png',
|
||||
craft = 'default:cobble',
|
||||
uses = 50
|
||||
})
|
||||
|
||||
register_hammer('hammer_wood', {
|
||||
name = 'Wooden Hammer',
|
||||
head = 'default_wood.png',
|
||||
craft = 'default:wood',
|
||||
uses = 10
|
||||
})
|
||||
-- Load mod
|
||||
dofile(uniham.syspath..DIR_DELIM..'use_hammer.lua')
|
||||
dofile(uniham.syspath..DIR_DELIM..'register_hammer.lua')
|
||||
dofile(uniham.syspath..DIR_DELIM..'registry.lua')
|
||||
|
||||
|
||||
-- Clean up
|
||||
uniham = nil
|
||||
|
45
registry/default.lua
Normal file
45
registry/default.lua
Normal file
@ -0,0 +1,45 @@
|
||||
-- Localize
|
||||
local register_hammer = uniham.register_hammer
|
||||
|
||||
|
||||
register_hammer('hammer_bronze', {
|
||||
name = 'Bronze Hammer',
|
||||
head = 'default_bronze_block.png',
|
||||
craft = 'default:bronze_ingot',
|
||||
uses = 100
|
||||
})
|
||||
|
||||
register_hammer('hammer_diamond', {
|
||||
name = 'Diamond Hammer',
|
||||
head = 'default_diamond_block.png',
|
||||
craft = 'default:diamond',
|
||||
uses = 200
|
||||
})
|
||||
|
||||
register_hammer('hammer_mese', {
|
||||
name = 'Mese Hammer',
|
||||
head = 'default_mese_block.png',
|
||||
craft = 'default:mese_crystal',
|
||||
uses = 300
|
||||
})
|
||||
|
||||
register_hammer('hammer_steel', {
|
||||
name = 'Steel Hammer',
|
||||
head = 'default_steel_block.png',
|
||||
craft = 'default:iron_ingot',
|
||||
uses = 150
|
||||
})
|
||||
|
||||
register_hammer('hammer_stone', {
|
||||
name = 'Stone Hammer',
|
||||
head = 'default_stone.png',
|
||||
craft = 'default:cobble',
|
||||
uses = 50
|
||||
})
|
||||
|
||||
register_hammer('hammer_wood', {
|
||||
name = 'Wooden Hammer',
|
||||
head = 'default_wood.png',
|
||||
craft = 'default:wood',
|
||||
uses = 10
|
||||
})
|
42
system/register_hammer.lua
Normal file
42
system/register_hammer.lua
Normal file
@ -0,0 +1,42 @@
|
||||
-- Localize
|
||||
local use_hammer = uniham.use_hammer
|
||||
|
||||
|
||||
-- Register a new hammer
|
||||
--
|
||||
-- definition = {
|
||||
-- name = 'Name of the hammer',
|
||||
-- head = 'head_material_texture.png',
|
||||
-- craft = 'crafting:material',
|
||||
-- uses = 123,
|
||||
-- }
|
||||
--
|
||||
-- @param short_id Unprefixed ID for the new hammer
|
||||
-- @param definition Definition table as described
|
||||
-- @return void
|
||||
uniham.register_hammer = function (short_id, definition)
|
||||
local texture = '([combine:16x16:-1,1=+s)^((+h^+m)^[makealpha:0,0,0)'
|
||||
texture = texture:gsub('+.', {
|
||||
['+s'] = 'default_stick.png',
|
||||
['+h'] = definition.head,
|
||||
['+m'] = 'uniham_head_mask.png'
|
||||
})
|
||||
|
||||
minetest.register_tool('uniham:'..short_id, {
|
||||
description = definition.name,
|
||||
inventory_image = texture,
|
||||
_uniham = { uses = definition.uses },
|
||||
on_use = function(itemstack, player, pointed_thing)
|
||||
return use_hammer(itemstack, player, pointed_thing)
|
||||
end
|
||||
})
|
||||
|
||||
minetest.register_craft({
|
||||
output = 'uniham:'..short_id,
|
||||
recipe = {
|
||||
{ '', definition.craft, '' },
|
||||
{ '', 'default:stick', definition.craft },
|
||||
{ 'default:stick', '', '' }
|
||||
}
|
||||
})
|
||||
end
|
44
system/registry.lua
Normal file
44
system/registry.lua
Normal file
@ -0,0 +1,44 @@
|
||||
-- Localize & Initialize
|
||||
local register_hammer = uniham.register_hammer
|
||||
local modpath = uniham.modpath
|
||||
local registry = modpath..DIR_DELIM..'registry'
|
||||
local depends = io.open(modpath..DIR_DELIM..'depends.txt', 'rb')
|
||||
local log_prefix = '['..minetest.get_current_modname()..'] '
|
||||
|
||||
|
||||
-- Check if a file exists
|
||||
--
|
||||
-- This function validates the existence of a file by trying to open the file
|
||||
-- for reading. If this succeeds the file is present.
|
||||
--
|
||||
-- @param file_name The file name (path) to be checked
|
||||
local file_exists = function (file_name)
|
||||
local f = io.open(file_name ,'r')
|
||||
if f ~= nil then
|
||||
io.close(f)
|
||||
return true
|
||||
else
|
||||
return false
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
-- Check if the depends.txt file was read properly
|
||||
-- Iterate over all lines
|
||||
-- Check if the mod exists
|
||||
-- Check if a registry file exists
|
||||
-- dofile() the file and print a log message
|
||||
if depends ~= nil then
|
||||
local lines = {}
|
||||
for line in depends:lines() do
|
||||
local modname = line:gsub('?', '')
|
||||
if minetest.get_modpath(modname) then
|
||||
local registry_file = registry..DIR_DELIM..modname..'.lua'
|
||||
if file_exists(registry_file) then
|
||||
dofile(registry_file)
|
||||
local message = 'Loaded built-in '..modname..' support'
|
||||
minetest.log('action', log_prefix..message)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
46
system/use_hammer.lua
Normal file
46
system/use_hammer.lua
Normal file
@ -0,0 +1,46 @@
|
||||
-- Localize
|
||||
local replacements = uniham.replacements
|
||||
|
||||
|
||||
-- Hammer use function
|
||||
--
|
||||
-- The function is called when using the hammer. It either converts the node to
|
||||
-- its replacement or does nothing. When converting the modified (added wear)
|
||||
-- itemstack is returned.
|
||||
--
|
||||
-- @param itemstack The `itemstack` as per on_use definition
|
||||
-- @param player The `player` as per on_use definition
|
||||
-- @param pointed_thing The `pointed_thing` as per on_use definition
|
||||
-- @see <https://dev.minetest.net/on_use#on_use>
|
||||
-- @return itemstack|void
|
||||
uniham.use_hammer = function (itemstack, player, pointed_thing)
|
||||
if pointed_thing.type == 'nothing' then return end
|
||||
local node_pos = minetest.get_pointed_thing_position(pointed_thing)
|
||||
local node_name = minetest.get_node(node_pos).name
|
||||
local node_sounds = minetest.registered_nodes[node_name].sounds
|
||||
|
||||
if not replacements[node_name] then
|
||||
minetest.sound_play(node_sounds.dug.name, {
|
||||
pos = node_pos,
|
||||
max_hear_distance = 8,
|
||||
gain = node_sounds.dug.gain,
|
||||
})
|
||||
return
|
||||
end
|
||||
|
||||
local replacement = replacements[node_name]
|
||||
local replacement_sounds = minetest.registered_nodes[replacement].sounds
|
||||
local uses = minetest.registered_tools[itemstack:get_name()]._uniham.uses
|
||||
|
||||
minetest.sound_play(replacement_sounds.dug.name, {
|
||||
pos = node_pos,
|
||||
max_hear_distance = 8,
|
||||
gain = replacement_sounds.dug.gain,
|
||||
})
|
||||
|
||||
minetest.set_node(node_pos, { name = replacement })
|
||||
minetest.check_for_falling(node_pos)
|
||||
|
||||
itemstack:add_wear(math.ceil(65535 / uses) + 1)
|
||||
return itemstack
|
||||
end
|
Loading…
Reference in New Issue
Block a user