2020-02-29 21:20:54 +03:00
|
|
|
--[[
|
|
|
|
|
|
|
|
TechAge
|
|
|
|
=======
|
|
|
|
|
2022-06-05 22:20:43 +03:00
|
|
|
Copyright (C) 2019-2022 Joachim Stolberg
|
2020-02-29 21:20:54 +03:00
|
|
|
|
2020-10-19 20:09:17 +03:00
|
|
|
AGPL v3
|
2020-02-29 21:20:54 +03:00
|
|
|
See LICENSE.txt for more information
|
2022-01-03 23:40:31 +03:00
|
|
|
|
2020-02-29 21:20:54 +03:00
|
|
|
ICTA Controller - Battery
|
|
|
|
|
|
|
|
]]--
|
|
|
|
|
|
|
|
-- for lazy programmers
|
|
|
|
local M = minetest.get_meta
|
|
|
|
local S = techage.S
|
2020-03-15 01:02:13 +03:00
|
|
|
local BATTERY_CAPACITY = 10000000
|
2020-02-29 21:20:54 +03:00
|
|
|
|
|
|
|
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-08 22:02:17 +03:00
|
|
|
local number = meta:get_string("node_number")
|
2020-02-29 21:20:54 +03:00
|
|
|
local percent = calc_percent(meta:get_int("content"))
|
2020-10-08 22:02:17 +03:00
|
|
|
meta:set_string("infotext", S("Battery").." "..number..": "..percent.." %")
|
2020-02-29 21:20:54 +03:00
|
|
|
if percent == 0 then
|
|
|
|
local node = minetest.get_node(pos)
|
2020-03-01 14:32:11 +03:00
|
|
|
node.name = "techage:ta4_battery_empty"
|
2020-02-29 21:20:54 +03:00
|
|
|
minetest.swap_node(pos, node)
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
2020-09-12 10:12:45 +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-02-29 21:20:54 +03:00
|
|
|
|
2020-09-12 10:12:45 +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-02-29 21:20:54 +03:00
|
|
|
},
|
2020-09-12 10:12:45 +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-02-29 21:20:54 +03:00
|
|
|
end
|
2020-09-12 10:12:45 +03:00
|
|
|
end
|
|
|
|
M(pos):set_int("content", content)
|
2020-10-08 22:02:17 +03:00
|
|
|
local number = techage.add_node(pos, "techage:ta4_battery")
|
|
|
|
M(pos):set_string("node_number", number)
|
2020-09-12 10:12:45 +03:00
|
|
|
on_timer(pos, 1)
|
|
|
|
minetest.get_node_timer(pos):start(30)
|
|
|
|
end,
|
2020-02-29 21:20:54 +03:00
|
|
|
|
2020-10-08 22:02:17 +03:00
|
|
|
after_dig_node = function(pos, oldnode, oldmetadata, digger)
|
|
|
|
techage.remove_node(pos, oldnode, oldmetadata)
|
|
|
|
end,
|
2022-01-03 23:40:31 +03:00
|
|
|
|
2020-09-12 10:12:45 +03:00
|
|
|
on_timer = on_timer,
|
|
|
|
|
|
|
|
preserve_metadata = function(pos, oldnode, oldmetadata, drops)
|
|
|
|
local content = M(pos):get_int("content")
|
2020-02-29 21:20:54 +03:00
|
|
|
|
2020-09-12 10:12:45 +03:00
|
|
|
local meta = drops[1]:get_meta()
|
|
|
|
meta:set_int("content", content)
|
|
|
|
local percent = calc_percent(content)
|
2021-04-29 23:04:17 +03:00
|
|
|
local text = S("Battery").." ("..percent.." %)"
|
2020-09-12 10:12:45 +03:00
|
|
|
meta:set_string("description", text)
|
|
|
|
end,
|
|
|
|
|
|
|
|
paramtype = "light",
|
2021-03-19 22:06:22 +03:00
|
|
|
use_texture_alpha = techage.CLIP,
|
2020-09-12 10:12:45 +03:00
|
|
|
sunlight_propagates = true,
|
|
|
|
paramtype2 = "facedir",
|
2020-09-13 14:15:31 +03:00
|
|
|
groups = {choppy=1, cracky=1, crumbly=1},
|
2020-09-12 10:12:45 +03:00
|
|
|
is_ground_content = false,
|
|
|
|
sounds = default.node_sound_stone_defaults(),
|
|
|
|
})
|
2020-02-29 21:20:54 +03:00
|
|
|
|
2020-03-01 14:32:11 +03:00
|
|
|
minetest.register_node("techage:ta4_battery_empty", {
|
2020-02-29 21:20:54 +03:00
|
|
|
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},
|
|
|
|
},
|
|
|
|
},
|
2022-01-03 23:40:31 +03:00
|
|
|
|
2020-02-29 21:20:54 +03:00
|
|
|
after_place_node = function(pos, placer)
|
|
|
|
local meta = minetest.get_meta(pos)
|
|
|
|
meta:set_int("content", 0)
|
|
|
|
end,
|
2022-01-03 23:40:31 +03:00
|
|
|
|
2020-02-29 21:20:54 +03:00
|
|
|
paramtype = "light",
|
2021-03-19 22:06:22 +03:00
|
|
|
use_texture_alpha = techage.CLIP,
|
2020-02-29 21:20:54 +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({
|
2020-03-01 14:32:11 +03:00
|
|
|
output = "techage:ta4_battery 2",
|
2020-02-29 21:20:54 +03:00
|
|
|
recipe = {
|
|
|
|
{"", "moreores:silver_ingot", ""},
|
|
|
|
{"", "default:copper_ingot", ""},
|
|
|
|
{"", "moreores:silver_ingot", ""},
|
|
|
|
}
|
|
|
|
})
|
|
|
|
else
|
|
|
|
minetest.register_craft({
|
2020-03-01 14:32:11 +03:00
|
|
|
output = "techage:ta4_battery 2",
|
2020-02-29 21:20:54 +03:00
|
|
|
recipe = {
|
|
|
|
{"", "default:tin_ingot", ""},
|
|
|
|
{"", "default:copper_ingot", ""},
|
|
|
|
{"", "default:tin_ingot", ""},
|
|
|
|
}
|
|
|
|
})
|
|
|
|
end
|
|
|
|
|
2020-10-08 22:02:17 +03:00
|
|
|
techage.register_node({"techage:ta4_battery"}, {
|
|
|
|
on_node_load = function(pos)
|
|
|
|
minetest.get_node_timer(pos):start(30)
|
|
|
|
end,
|
2022-01-03 23:40:31 +03:00
|
|
|
|
2020-10-08 22:02:17 +03:00
|
|
|
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,
|
2022-06-05 22:20:43 +03:00
|
|
|
on_beduino_request_data = function(pos, src, topic, payload)
|
|
|
|
if topic == 134 then
|
|
|
|
local meta = minetest.get_meta(pos)
|
|
|
|
return 0, {calc_percent(meta:get_int("content"))}
|
|
|
|
else
|
|
|
|
return 2, ""
|
|
|
|
end
|
|
|
|
end,
|
2022-01-04 21:40:27 +03:00
|
|
|
})
|