techage_modpack/techage/icta_controller/battery.lua

180 lines
4.5 KiB
Lua
Raw Normal View History

2020-05-31 23:31:18 +03:00
--[[
TechAge
=======
Copyright (C) 2019-2020 Joachim Stolberg
2020-10-25 23:32:47 +03:00
AGPL v3
2020-05-31 23:31:18 +03:00
See LICENSE.txt for more information
ICTA Controller - Battery
]]--
-- for lazy programmers
local M = minetest.get_meta
local S = techage.S
local BATTERY_CAPACITY = 10000000
local function calc_percent(content)
local val = (BATTERY_CAPACITY - math.min(content or 0, BATTERY_CAPACITY))
return 100 - math.floor((val * 100.0 / BATTERY_CAPACITY))
end
local function on_timer(pos, elapsed)
local meta = minetest.get_meta(pos)
2020-10-25 23:32:47 +03:00
local number = meta:get_string("node_number")
2020-05-31 23:31:18 +03:00
local percent = calc_percent(meta:get_int("content"))
2020-10-25 23:32:47 +03:00
meta:set_string("infotext", S("Battery").." "..number..": "..percent.." %")
2020-05-31 23:31:18 +03:00
if percent == 0 then
local node = minetest.get_node(pos)
node.name = "techage:ta4_battery_empty"
minetest.swap_node(pos, node)
return false
end
return true
end
2020-09-13 14:24:06 +03:00
minetest.register_alias("techage:ta4_battery75", "techage:ta4_battery")
minetest.register_alias("techage:ta4_battery50", "techage:ta4_battery")
minetest.register_alias("techage:ta4_battery25", "techage:ta4_battery")
2020-05-31 23:31:18 +03:00
2020-09-13 14:24:06 +03:00
minetest.register_node("techage:ta4_battery", {
description = S("Battery"),
inventory_image = 'techage_battery_inventory.png',
wield_image = 'techage_battery_inventory.png',
tiles = {
-- up, down, right, left, back, front
"techage_smartline.png",
"techage_smartline.png",
"techage_smartline.png",
"techage_smartline.png",
"techage_smartline.png",
"techage_smartline.png^techage_battery_green.png",
},
drawtype = "nodebox",
node_box = {
type = "fixed",
fixed = {
{ -6/32, -6/32, 14/32, 6/32, 6/32, 16/32},
2020-05-31 23:31:18 +03:00
},
2020-09-13 14:24:06 +03:00
},
after_place_node = function(pos, placer, itemstack)
local content = BATTERY_CAPACITY
if itemstack then
local stack_meta = itemstack:get_meta()
if stack_meta then
-- This ensures that dug batteries of the old system are considered full.
local string_content = stack_meta:get_string("content")
if string_content ~= "" then
-- Batteries dug in the new system are handled correctly.
content = techage.in_range(stack_meta:get_int("content"), 0, BATTERY_CAPACITY)
end
2020-05-31 23:31:18 +03:00
end
2020-09-13 14:24:06 +03:00
end
M(pos):set_int("content", content)
2020-10-25 23:32:47 +03:00
local number = techage.add_node(pos, "techage:ta4_battery")
M(pos):set_string("node_number", number)
2020-09-13 14:24:06 +03:00
on_timer(pos, 1)
minetest.get_node_timer(pos):start(30)
end,
2020-05-31 23:31:18 +03:00
2020-10-25 23:32:47 +03:00
after_dig_node = function(pos, oldnode, oldmetadata, digger)
techage.remove_node(pos, oldnode, oldmetadata)
end,
2020-09-13 14:24:06 +03:00
on_timer = on_timer,
preserve_metadata = function(pos, oldnode, oldmetadata, drops)
local content = M(pos):get_int("content")
2020-05-31 23:31:18 +03:00
2020-09-13 14:24:06 +03:00
local meta = drops[1]:get_meta()
meta:set_int("content", content)
local percent = calc_percent(content)
2021-05-14 19:50:16 +03:00
local text = S("Battery").." ("..percent.." %)"
2020-09-13 14:24:06 +03:00
meta:set_string("description", text)
end,
paramtype = "light",
2021-05-14 19:50:16 +03:00
use_texture_alpha = techage.CLIP,
2020-09-13 14:24:06 +03:00
sunlight_propagates = true,
paramtype2 = "facedir",
groups = {choppy=1, cracky=1, crumbly=1},
is_ground_content = false,
sounds = default.node_sound_stone_defaults(),
})
2020-05-31 23:31:18 +03:00
minetest.register_node("techage:ta4_battery_empty", {
description = S("Battery"),
tiles = {
-- up, down, right, left, back, front
"techage_smartline.png",
"techage_smartline.png",
"techage_smartline.png",
"techage_smartline.png",
"techage_smartline.png",
"techage_smartline.png^techage_battery_red.png",
},
drawtype = "nodebox",
node_box = {
type = "fixed",
fixed = {
{ -6/32, -6/32, 14/32, 6/32, 6/32, 16/32},
},
},
after_place_node = function(pos, placer)
local meta = minetest.get_meta(pos)
meta:set_int("content", 0)
end,
paramtype = "light",
2021-05-14 19:50:16 +03:00
use_texture_alpha = techage.CLIP,
2020-05-31 23:31:18 +03:00
sunlight_propagates = true,
paramtype2 = "facedir",
groups = {choppy=1, cracky=1, crumbly=1, not_in_creative_inventory=1},
drop = "",
is_ground_content = false,
sounds = default.node_sound_stone_defaults(),
})
if minetest.global_exists("moreores") then
minetest.register_craft({
output = "techage:ta4_battery 2",
recipe = {
{"", "moreores:silver_ingot", ""},
{"", "default:copper_ingot", ""},
{"", "moreores:silver_ingot", ""},
}
})
else
minetest.register_craft({
output = "techage:ta4_battery 2",
recipe = {
{"", "default:tin_ingot", ""},
{"", "default:copper_ingot", ""},
{"", "default:tin_ingot", ""},
}
})
end
2020-10-25 23:32:47 +03:00
techage.register_node({"techage:ta4_battery"}, {
on_node_load = function(pos)
minetest.get_node_timer(pos):start(30)
end,
on_recv_message = function(pos, src, topic, payload)
if topic == "load" then
local meta = minetest.get_meta(pos)
return calc_percent(meta:get_int("content"))
else
return "unsupported"
end
end,
2020-05-31 23:31:18 +03:00
})