347 lines
9.4 KiB
Lua
Raw Normal View History

2017-06-14 19:52:34 +02:00
--[[
Gravel Sieve Mod
================
v0.01 by JoSt
2017-06-14 22:48:03 +02:00
Derived from the work of celeron55, Perttu Ahola (furnace)
2017-06-14 19:52:34 +02:00
Copyright (C) 2017 Joachim Stolberg
2017-06-14 22:48:03 +02:00
Copyright (C) 2011-2016 celeron55, Perttu Ahola <celeron55@gmail.com>
Copyright (C) 2011-2016 Various Minetest developers and contributors
2017-06-14 19:52:34 +02:00
LGPLv2.1+
See LICENSE.txt for more information
History:
2017-06-14 21:45:19 +02:00
2017-06-14 v0.01 first version
2017-06-15 12:41:42 +02:00
2017-06-15 v0.02 manually use of the sieve added
2017-06-14 19:52:34 +02:00
]]--
gravelsieve = {
2017-06-14 22:48:03 +02:00
rand = PseudoRandom(1234)
2017-06-14 19:52:34 +02:00
}
2017-06-15 12:41:42 +02:00
dofile(minetest.get_modpath("gravelsieve") .. "/hammer.lua")
2017-06-16 00:28:25 +02:00
gravelsieve.manually = minetest.settings:get("gravelsieve_manually") or false
2017-06-14 19:52:34 +02:00
2017-06-14 21:45:19 +02:00
-- Ore probability table (1/n)
2017-06-15 12:41:42 +02:00
local ore_probability = {
2017-06-14 22:48:03 +02:00
iron_lump = 15,
copper_lump = 15,
--tin_lump = 15, not available in V0.4.15
2017-06-14 22:48:03 +02:00
gold_lump = 25,
mese_crystal = 25,
diamond = 50,
2017-06-14 19:52:34 +02:00
}
2017-06-15 12:41:42 +02:00
-- gravel probability factor
local probability_factor = {
["default:gravel"] = 1,
["gravelsieve:gravel1"] = 2,
["gravelsieve:gravel2"] = 4,
["gravelsieve:gravel3"] = 8,
}
2017-06-14 19:52:34 +02:00
local sieve_formspec =
2017-06-14 22:48:03 +02:00
"size[8,8]"..
"list[context;src;1,1;1,1;]"..
"image[3,1;1,1;gui_furnace_arrow_bg.png^[transformR270]"..
"list[context;dst;4,0;4,3;]"..
"list[current_player;main;0,4;8,4;]"
2017-06-14 19:52:34 +02:00
local function allow_metadata_inventory_put(pos, listname, index, stack, player)
if minetest.is_protected(pos, player:get_player_name()) then
return 0
end
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
if listname == "src" then
return stack:get_count()
elseif listname == "dst" then
return 0
end
end
local function allow_metadata_inventory_move(pos, from_list, from_index, to_list, to_index, count, player)
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
local stack = inv:get_stack(from_list, from_index)
return allow_metadata_inventory_put(pos, to_list, to_index, stack, player)
end
local function allow_metadata_inventory_take(pos, listname, index, stack, player)
if minetest.is_protected(pos, player:get_player_name()) then
return 0
end
return stack:get_count()
end
2017-06-15 12:41:42 +02:00
-- handle the sieve animation
local function swap_node(pos, meta, start)
2017-06-14 19:52:34 +02:00
local node = minetest.get_node(pos)
2017-06-15 12:41:42 +02:00
local idx = meta:get_int("idx")
if start then
if idx == 3 then
idx = 0
end
else
idx = (idx + 1) % 4
end
2017-06-14 22:48:03 +02:00
meta:set_int("idx", idx)
node.name = "gravelsieve:sieve"..idx
2017-06-14 19:52:34 +02:00
minetest.swap_node(pos, node)
2017-06-14 22:48:03 +02:00
return idx == 3
2017-06-14 19:52:34 +02:00
end
2017-06-15 12:41:42 +02:00
-- place ores to dst according to the calculated probability
2017-06-14 19:52:34 +02:00
local function random_ore(inv, src)
2017-06-14 22:48:03 +02:00
local num
2017-06-15 12:41:42 +02:00
for ore, probability in pairs(ore_probability) do
probability = probability * probability_factor[src:get_name()]
if probability ~= nil then
num = gravelsieve.rand:next(0, probability)
if num == probability then
item = ItemStack("default:"..ore)
if inv:room_for_item("dst", item) then
inv:add_item("dst", item)
return true -- ore placed
end
end
2017-06-14 22:48:03 +02:00
end
end
2017-06-15 12:41:42 +02:00
return false -- gravel has to be moved
2017-06-14 19:52:34 +02:00
end
2017-06-15 12:41:42 +02:00
-- move gravel and ores to dst
2017-06-14 19:52:34 +02:00
local function move_src2dst(meta, pos, inv, src, dst)
2017-06-14 22:48:03 +02:00
if inv:room_for_item("dst", dst) and inv:contains_item("src", src) then
2017-06-15 12:41:42 +02:00
local res = swap_node(pos, meta, false)
2017-06-14 22:48:03 +02:00
if res then
if not random_ore(inv, src) then
inv:add_item("dst", dst)
end
inv:remove_item("src", src)
end
return true
end
return false
2017-06-14 19:52:34 +02:00
end
2017-06-15 12:41:42 +02:00
-- timer callback, alternatively called by on_punch
2017-06-14 19:52:34 +02:00
local function sieve_node_timer(pos, elapsed)
2017-06-14 22:48:03 +02:00
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
local gravel = ItemStack("default:gravel")
local gravel1 = ItemStack("gravelsieve:gravel1")
local gravel2 = ItemStack("gravelsieve:gravel2")
local gravel3 = ItemStack("gravelsieve:gravel3")
if move_src2dst(meta, pos, inv, gravel, gravel1) then
return true
elseif move_src2dst(meta, pos, inv, gravel1, gravel2) then
return true
elseif move_src2dst(meta, pos, inv, gravel2, gravel3) then
return true
2017-06-15 12:41:42 +02:00
elseif move_src2dst(meta, pos, inv, gravel3, gravel3) then
return true
2017-06-14 22:48:03 +02:00
else
2017-06-15 12:41:42 +02:00
if not gravelsieve.manually then
minetest.get_node_timer(pos):stop()
2017-06-14 22:48:03 +02:00
return false
2017-06-15 12:41:42 +02:00
end
2017-06-14 22:48:03 +02:00
end
2017-06-14 19:52:34 +02:00
end
for idx = 0,4 do
2017-06-14 22:48:03 +02:00
local nodebox_data = {
{ -8/16, -8/16, -8/16, 8/16, 4/16, -6/16 },
{ -8/16, -8/16, 6/16, 8/16, 4/16, 8/16 },
{ -8/16, -8/16, -8/16, -6/16, 4/16, 8/16 },
{ 6/16, -8/16, -8/16, 8/16, 4/16, 8/16 },
{ -6/16, -2/16, -6/16, 6/16, 8/16, 6/16 },
}
nodebox_data[5][5] = (8 - 2*idx) / 16
local tiles_data = {
-- up, down, right, left, back, front
"gravelsieve_gravel.png",
"gravelsieve_gravel.png",
"gravelsieve_sieve.png",
"gravelsieve_sieve.png",
"gravelsieve_sieve.png",
"gravelsieve_sieve.png",
}
if idx == 3 then
tiles_data[1] = "gravelsieve_top.png"
not_in_creative_inventory = 0
else
not_in_creative_inventory = 1
end
minetest.register_node("gravelsieve:sieve"..idx, {
description = "Gravel Sieve",
tiles = tiles_data,
drawtype = "nodebox",
node_box = {
type = "fixed",
fixed = nodebox_data,
},
2017-06-16 00:28:25 +02:00
selection_box = {
type = "fixed",
fixed = { -8/16, -8/16, -8/16, 8/16, 4/16, 8/16 },
},
2017-06-14 22:48:03 +02:00
can_dig = can_dig,
on_timer = sieve_node_timer,
on_construct = function(pos)
local meta = minetest.get_meta(pos)
meta:set_int("idx", idx)
meta:set_string("formspec", sieve_formspec)
local inv = meta:get_inventory()
inv:set_size('src', 1)
inv:set_size('dst', 12)
end,
on_metadata_inventory_move = function(pos)
2017-06-15 12:41:42 +02:00
if gravelsieve.manually then
local meta = minetest.get_meta(pos)
swap_node(pos, meta, true)
else
minetest.get_node_timer(pos):start(1.0)
end
2017-06-14 22:48:03 +02:00
end,
on_metadata_inventory_take = function(pos)
2017-06-15 12:41:42 +02:00
if gravelsieve.manually then
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
if inv:is_empty("src") then
-- sieve should be empty
meta:set_int("idx", 2)
swap_node(pos, meta, false)
end
else
minetest.get_node_timer(pos):start(1.0)
end
2017-06-14 22:48:03 +02:00
end,
on_metadata_inventory_put = function(pos)
2017-06-15 12:41:42 +02:00
if gravelsieve.manually then
local meta = minetest.get_meta(pos)
swap_node(pos, meta, true)
else
minetest.get_node_timer(pos):start(1.0)
end
2017-06-14 22:48:03 +02:00
end,
2017-06-15 12:41:42 +02:00
on_punch = function(pos, node, puncher, pointed_thing)
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
if inv:is_empty("dst") and inv:is_empty("src") then
minetest.node_punch(pos, node, puncher, pointed_thing)
else
-- punching the sieve speeds up the process
sieve_node_timer(pos, 0)
end
end,
on_dig = function(pos, node, puncher, pointed_thing)
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
if inv:is_empty("dst") and inv:is_empty("src") then
minetest.node_dig(pos, node, puncher, pointed_thing)
end
end,
2017-06-14 22:48:03 +02:00
allow_metadata_inventory_put = allow_metadata_inventory_put,
allow_metadata_inventory_move = allow_metadata_inventory_move,
allow_metadata_inventory_take = allow_metadata_inventory_take,
paramtype2 = "facedir",
sunlight_propagates = true,
is_ground_content = false,
groups = {cracky=3, stone=1, uber=1, not_in_creative_inventory=not_in_creative_inventory},
})
2017-06-14 19:52:34 +02:00
end
minetest.register_node("gravelsieve:gravel1", {
description = "Gravel sifted 1",
tiles = {"default_gravel.png"},
2017-06-14 21:45:19 +02:00
groups = {crumbly = 2, falling_node = 1, not_in_creative_inventory=1},
2017-06-14 19:52:34 +02:00
sounds = default.node_sound_gravel_defaults(),
drop = {
max_items = 1,
items = {
{items = {'default:flint'}, rarity = 16},
{items = {'default:gravel'}}
}
}
})
minetest.register_node("gravelsieve:gravel2", {
description = "Gravel sifted 2",
tiles = {"default_gravel.png"},
2017-06-14 21:45:19 +02:00
groups = {crumbly = 2, falling_node = 1, not_in_creative_inventory=1},
2017-06-14 19:52:34 +02:00
sounds = default.node_sound_gravel_defaults(),
drop = {
max_items = 1,
items = {
{items = {'default:flint'}, rarity = 16},
{items = {'default:gravel'}}
}
}
})
minetest.register_node("gravelsieve:gravel3", {
description = "Gravel sifted 3",
tiles = {"default_gravel.png"},
2017-06-14 21:45:19 +02:00
groups = {crumbly = 2, falling_node = 1, not_in_creative_inventory=1},
2017-06-14 19:52:34 +02:00
sounds = default.node_sound_gravel_defaults(),
drop = {
max_items = 1,
items = {
{items = {'default:flint'}, rarity = 16},
{items = {'default:gravel'}}
}
}
})
2017-06-14 22:48:03 +02:00
minetest.register_craft({
output = "gravelsieve:sieve",
recipe = {
{"group:wood", "", "group:wood"},
{"group:wood", "default:steel_ingot", "group:wood"},
{"group:wood", "", "group:wood"},
},
})
minetest.register_alias("gravelsieve:sieve", "gravelsieve:sieve3")
2017-06-16 00:28:25 +02:00
--~ -- particle effects
--~ function tp_effect(pos)
--~ minetest.add_particlespawner({
--~ amount = 20,
--~ time = 0.25,
--~ minpos = pos,
--~ maxpos = pos,
--~ minvel = {x = -2, y = 1, z = -2},
--~ maxvel = {x = 2, y = 2, z = 2},
--~ minacc = {x = 0, y = -2, z = 0},
--~ maxacc = {x = 0, y = -4, z = 0},
--~ minexptime = 0.1,
--~ maxexptime = 1,
--~ minsize = 0.5,
--~ maxsize = 1.5,
--~ texture = "particle.png",
--~ glow = 15,
--~ })
--~ end