maidroid/maidroid_tool/core_writer.lua

374 lines
12 KiB
Lua
Raw Normal View History

2016-09-14 02:35:28 +02:00
------------------------------------------------------------
-- Copyright (c) 2016 tacigar. All rights reserved.
-- https://github.com/tacigar/maidroid
------------------------------------------------------------
2016-09-14 17:24:48 +02:00
local dye_core_map = {
["dye:red"] = "maidroid_core:basic",
}
2016-09-14 02:35:28 +02:00
-- register a definition of a core writer.
2016-09-16 03:13:39 +02:00
;(function()
2016-09-14 04:23:27 +02:00
local node_box = {
type = "fixed",
fixed = {
{-0.4375, -0.25, -0.4375, 0.4375, 0.1875, 0.4375},
{ 0.1875, 0.3125, 0.0625, 0.4375, 0.375, 0.125},
{ -0.375, 0.1875, -0.375, 0.375, 0.25, 0.375},
{-0.0625, -0.5, -0.0625, 0.0625, 0.375, 0.0625},
{ 0.375, 0.1875, 0.0625, 0.4375, 0.375, 0.125},
{ -0.375, -0.5, -0.375, 0.375, -0.25, 0.375},
},
2016-09-14 13:04:21 +02:00
}
local selection_box = {
type = "fixed",
fixed = {
{-0.4375, -0.5, -0.4375, 0.4375, 0.25, 0.4375},
},
}
2016-09-16 00:35:13 +02:00
local formspec_inactive = "size[8,9]"
2016-09-16 01:35:22 +02:00
.. default.gui_bg
.. default.gui_bg_img
.. default.gui_slots
.. "label[3.75,0;Core]"
.. "list[current_name;core;3.5,0.5;1,1;]"
.. "label[2.75,2;Coal]"
.. "list[current_name;fuel;2.5,2.5;1,1;]"
.. "label[4.75,2;Dye]"
.. "list[current_name;dye;4.5,2.5;1,1;]"
.. "image[3.5,1.5;1,2;maidroid_tool_gui_arrow.png]"
.. "image[3.1,3.5;2,1;maidroid_tool_gui_meter.png^[transformR270]"
2016-09-16 00:35:13 +02:00
.. "list[current_player;main;0,5;8,1;]"
.. "list[current_player;main;0,6.2;8,3;8]"
2016-09-16 03:13:39 +02:00
local function generate_formspec_active(writing_time)
local arrow_percent = (100 / 40) * writing_time
local merter_percent = 0
if writing_time % 16 >= 8 then
meter_percent = (8 - (writing_time % 8)) * (100 / 8)
else
meter_percent = (writing_time % 8) * (100 / 8)
end
return "size[8,9]"
.. default.gui_bg
.. default.gui_bg_img
.. default.gui_slots
.. "label[3.75,0;Core]"
.. "list[current_name;core;3.5,0.5;1,1;]"
.. "label[2.75,2;Coal]"
.. "list[current_name;fuel;2.5,2.5;1,1;]"
.. "label[4.75,2;Dye]"
.. "list[current_name;dye;4.5,2.5;1,1;]"
.. "image[3.5,1.5;1,2;maidroid_tool_gui_arrow.png^[lowpart:"
.. arrow_percent
.. ":maidroid_tool_gui_arrow_filled.png]"
.. "image[3.1,3.5;2,1;maidroid_tool_gui_meter.png^[lowpart:"
.. meter_percent
.. ":maidroid_tool_gui_meter_filled.png^[transformR270]"
.. "list[current_player;main;0,5;8,1;]"
.. "list[current_player;main;0,6.2;8,3;8]"
end
2016-09-15 02:49:29 +02:00
-- get_nearest_core_entity returns the nearest core entity.
local function get_nearest_core_entity(pos)
local all_objects = minetest.get_objects_inside_radius(pos, 1.0)
for _, object in ipairs(all_objects) do
if object:get_luaentity().name == "maidroid_tool:core_entity" then
return object:get_luaentity()
end
end
return nil
end
-- can_dig is a common callback for the core writer.
local function can_dig(pos, player)
2016-09-14 13:04:21 +02:00
local meta = minetest.get_meta(pos)
local inventory = meta:get_inventory()
return (
inventory:is_empty("core") and
inventory:is_empty("fuel") and
inventory:is_empty("dye")
)
2016-09-14 13:20:14 +02:00
end
2016-09-14 13:04:21 +02:00
2016-09-16 10:29:27 +02:00
-- swap_node is a helper function that swap two nodes.
local function swap_node(pos, name)
local node = minetest.get_node(pos)
node.name = name
minetest.swap_node(pos, node)
end
-- on_timer is a common callback for the core writer.
local function on_timer(pos, elapsed)
2016-09-14 17:24:48 +02:00
local meta = minetest.get_meta(pos)
local inventory = meta:get_inventory()
local core_list = inventory:get_list("core")
local fuel_list = inventory:get_list("fuel")
local dye_list = inventory:get_list("dye")
2016-09-14 17:24:48 +02:00
local writing_time = meta:get_float("writing_time")
2016-09-16 03:13:39 +02:00
local writing_total_time = 40
local output_core = meta:get_string("output_core")
2016-09-14 17:24:48 +02:00
-- if writing time is positive, the core writer is active.
if writing_time >= 0 then
if writing_time <= writing_total_time then
meta:set_float("writing_time", writing_time + 1)
2016-09-16 03:13:39 +02:00
meta:set_string("formspec", generate_formspec_active(writing_time))
2016-09-14 18:22:07 +02:00
else -- else place output core to core list.
2016-09-14 18:22:07 +02:00
meta:set_float("writing_time", -1)
meta:set_string("output_core", "")
2016-09-16 03:13:39 +02:00
meta:set_string("formspec", formspec_inactive)
2016-09-14 18:22:07 +02:00
inventory:set_stack("core", 1, ItemStack(output_core))
2016-09-16 10:29:27 +02:00
swap_node(pos, "maidroid_tool:core_writer")
2016-09-15 02:49:29 +02:00
local core_entity = get_nearest_core_entity(pos)
core_entity:stop_rotate()
end
2016-09-14 18:22:07 +02:00
else -- else the core writer is inactive.
local core_name = core_list[1]:get_name()
2016-09-14 18:22:07 +02:00
if core_name == "maidroid_core:empty" and (not fuel_list[1]:is_empty()) and (not dye_list[1]:is_empty()) then
meta:set_float("writing_time", 0)
meta:set_string("output_core", dye_core_map[dye_list[1]:get_name()])
2016-09-14 18:22:07 +02:00
local fuel_stack = fuel_list[1]
fuel_stack:take_item()
inventory:set_stack("fuel", 1, fuel_stack)
local dye_stack = dye_list[1]
dye_stack:take_item()
inventory:set_stack("dye", 1, dye_stack)
2016-09-16 10:29:27 +02:00
swap_node(pos, "maidroid_tool:core_writer_active")
2016-09-15 02:49:29 +02:00
local core_entity = get_nearest_core_entity(pos)
core_entity:start_rotate()
end
2016-09-14 17:24:48 +02:00
end
2016-09-14 18:22:07 +02:00
return true
end
-- allow_metadata_inventory_put is a common callback for the core writer.
local function allow_metadata_inventory_put(pos, listname, index, stack, player)
2016-09-14 13:04:21 +02:00
local meta = minetest.get_meta(pos)
local inventory = meta:get_inventory()
local itemname = stack:get_name()
if (listname == "fuel" and itemname == "default:coal_lump") then
return stack:get_count()
2016-09-15 03:36:22 +02:00
elseif listname == "dye" and dye_core_map[itemname] ~= nil then
2016-09-14 13:04:21 +02:00
return stack:get_count()
2016-09-14 13:20:14 +02:00
elseif listname == "core" and maidroid.is_core(itemname) then
2016-09-14 13:04:21 +02:00
return stack:get_count()
end
return 0
end
-- allow_metadata_inventory_move is a common callback for the core writer.
local function allow_metadata_inventory_move(pos, from_list, from_index, to_list, to_index, count, player)
2016-09-14 13:04:21 +02:00
local meta = minetest.get_meta(pos)
local inventory = meta:get_inventory()
local stack = inventory:get_stack(from_list, from_index)
return allow_metadata_inventory_put(pos, listname, to_index, stack, player)
end
--------------------------------------------------------------------
2016-09-14 02:35:28 +02:00
2016-09-14 13:20:14 +02:00
;(function() -- register a definition of an inactive core writer.
local tiles = {
"maidroid_tool_core_writer_top.png",
"maidroid_tool_core_writer_bottom.png",
"maidroid_tool_core_writer_right.png",
"maidroid_tool_core_writer_right.png^[transformFX",
"maidroid_tool_core_writer_front.png^[transformFX",
"maidroid_tool_core_writer_front.png",
}
local function on_construct(pos)
local meta = minetest.get_meta(pos)
2016-09-16 00:35:13 +02:00
meta:set_string("formspec", formspec_inactive)
2016-09-14 18:22:07 +02:00
meta:set_string("output_core", "")
meta:set_float("writing_time", -1)
local inventory = meta:get_inventory()
inventory:set_size("core", 1)
inventory:set_size("fuel", 1)
inventory:set_size("dye", 1)
end
2016-09-15 02:49:29 +02:00
local function on_metadata_inventory_put(pos, listname, index, stack, player)
2016-09-14 18:22:07 +02:00
local timer = minetest.get_node_timer(pos)
2016-09-16 03:13:39 +02:00
timer:start(0.25)
2016-09-15 02:49:29 +02:00
local meta = minetest.get_meta(pos)
if listname == "core" then
local entity_position = {
x = pos.x, y = pos.y + 0.65, z = pos.z
}
2016-09-16 03:13:39 +02:00
minetest.add_entity(entity_position, "maidroid_tool:core_entity")
2016-09-15 02:49:29 +02:00
end
2016-09-14 18:22:07 +02:00
end
2016-09-15 02:49:29 +02:00
local function on_metadata_inventory_move(pos, from_list, from_index, to_list, to_index, count, player)
local meta = minetest.get_meta(pos)
local inventory = meta:get_inventory()
local stack = inventory:get_stack(from_list, from_index)
on_metadata_inventory_put(pos, listname, to_index, stack, player)
end
local function on_metadata_inventory_take(pos, listname, index, stack, player)
if listname == "core" then
local core_entity = get_nearest_core_entity(pos)
core_entity.object:remove()
end
end
local function allow_metadata_inventory_take(pos, listname, index, stack, player)
return stack:get_count() -- maybe add more.
end
minetest.register_node("maidroid_tool:core_writer", {
description = "maidroid tool : core writer",
drawtype = "nodebox",
paramtype = "light",
paramtype2 = "facedir",
groups = {cracky = 2},
is_ground_content = false,
sounds = default.node_sound_stone_defaults(),
node_box = node_box,
selection_box = selection_box,
tiles = tiles,
can_dig = can_dig,
2016-09-14 17:24:48 +02:00
on_timer = on_timer,
on_construct = on_construct,
2016-09-14 18:22:07 +02:00
on_metadata_inventory_put = on_metadata_inventory_put,
on_metadata_inventory_move = on_metadata_inventory_move,
2016-09-15 02:49:29 +02:00
on_metadata_inventory_take = on_metadata_inventory_take,
allow_metadata_inventory_put = allow_metadata_inventory_put,
allow_metadata_inventory_move = allow_metadata_inventory_move,
allow_metadata_inventory_take = allow_metadata_inventory_take,
})
2016-09-14 13:20:14 +02:00
end) ()
2016-09-14 13:04:21 +02:00
--------------------------------------------------------------------
2016-09-14 13:20:14 +02:00
;(function () -- register a definition of an active core writer.
2016-09-14 13:04:21 +02:00
local tiles = {
"maidroid_tool_core_writer_top.png",
"maidroid_tool_core_writer_bottom.png",
"maidroid_tool_core_writer_right.png",
"maidroid_tool_core_writer_right.png^[transformFX",
{
backface_culling = false,
image = "maidroid_tool_core_writer_front_active.png^[transformFX",
animation = {
type = "vertical_frames",
aspect_w = 16,
aspect_h = 16,
length = 1.5,
},
},
2016-09-14 13:04:21 +02:00
{
backface_culling = false,
image = "maidroid_tool_core_writer_front_active.png",
animation = {
type = "vertical_frames",
aspect_w = 16,
aspect_h = 16,
length = 1.5,
},
},
}
local function allow_metadata_inventory_take(pos, listname, index, stack, player)
if listname == "core" then
return 0
end
return stack:get_count()
2016-09-14 13:04:21 +02:00
end
2016-09-14 13:04:21 +02:00
minetest.register_node("maidroid_tool:core_writer_active", {
drawtype = "nodebox",
paramtype = "light",
paramtype2 = "facedir",
groups = {cracky = 2},
is_ground_content = false,
sounds = default.node_sound_stone_defaults(),
node_box = node_box,
selection_box = selection_box,
2016-09-14 13:04:21 +02:00
tiles = tiles,
can_dig = can_dig,
2016-09-14 17:24:48 +02:00
on_timer = on_timer,
2016-09-14 13:04:21 +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,
})
2016-09-14 13:20:14 +02:00
end) ()
end) ()
2016-09-14 04:23:27 +02:00
-- register a definition of a core entity.
2016-09-14 13:20:14 +02:00
;(function()
2016-09-14 10:34:16 +02:00
local node_box = {
type = "fixed",
fixed = {
{ -0.5, -0.5, -0.125, 0.5, -0.4375, 0.125},
{ -0.125, -0.5, -0.5, 0.125, -0.4375, 0.5},
{ -0.25, -0.5, -0.4375, 0.25, -0.4375, 0.4375},
{ -0.375, -0.5, -0.375, 0.375, -0.4375, 0.375},
{-0.4375, -0.5, -0.25, 0.4375, -0.4375, 0.25},
},
}
2016-09-14 05:33:23 +02:00
2016-09-14 10:34:16 +02:00
local tiles = {
"maidroid_tool_core_top.png",
"maidroid_tool_core_top.png",
"maidroid_tool_core_right.png",
"maidroid_tool_core_right.png",
"maidroid_tool_core_right.png",
"maidroid_tool_core_right.png",
}
2016-09-14 05:33:23 +02:00
2016-09-14 10:34:16 +02:00
minetest.register_node("maidroid_tool:core_node", {
drawtype = "nodebox",
tiles = tiles,
node_box = node_box,
paramtype = "light",
paramtype2 = "facedir",
})
2016-09-14 05:33:23 +02:00
2016-09-15 00:16:25 +02:00
local function on_activate(self, staticdata)
2016-09-15 02:49:29 +02:00
self.object:set_properties{textures = {"maidroid_tool:core_node"}}
2016-09-15 00:16:25 +02:00
end
local function start_rotate(self)
2016-09-15 02:49:29 +02:00
self.object:set_properties{automatic_rotate = 1}
2016-09-15 00:16:25 +02:00
end
local function stop_rotate(self)
2016-09-15 02:49:29 +02:00
self.object:set_properties{automatic_rotate = 0}
2016-09-15 00:16:25 +02:00
end
2016-09-14 05:33:23 +02:00
minetest.register_entity("maidroid_tool:core_entity", {
2016-09-15 02:49:29 +02:00
physical = false,
visual = "wielditem",
visual_size = {x = 0.5, y = 0.5},
collisionbox = {0, 0, 0, 0, 0, 0},
on_activate = on_activate,
start_rotate = start_rotate,
stop_rotate = stop_rotate,
2016-09-14 05:33:23 +02:00
})
2016-09-14 13:20:14 +02:00
end) ()