techage_modpack/signs_bot/cart_sensor.lua

161 lines
4.1 KiB
Lua
Raw Normal View History

2020-05-31 23:31:18 +03:00
--[[
Signs Bot
=========
2021-05-14 19:50:16 +03:00
Copyright (C) 2019-2021 Joachim Stolberg
2020-05-31 23:31:18 +03:00
GPL v3
See LICENSE.txt for more information
2022-09-24 12:01:30 +03:00
2020-05-31 23:31:18 +03:00
Cart Sensor
]]--
-- for lazy programmers
2021-05-14 19:50:16 +03:00
local P2S = function(pos) if pos then return minetest.pos_to_string(pos) end end
local S2P = minetest.string_to_pos
2020-05-31 23:31:18 +03:00
local M = minetest.get_meta
2021-05-14 19:50:16 +03:00
-- Load support for I18n.
local S = signs_bot.S
2020-05-31 23:31:18 +03:00
local lib = signs_bot.lib
local CYCLE_TIME = 2
local function update_infotext(pos, dest_pos, dest_idx)
2021-05-14 19:50:16 +03:00
M(pos):set_string("infotext", S("Cart Sensor: Connected with").." "..P2S(dest_pos).." / "..dest_idx)
2022-09-24 12:01:30 +03:00
end
2020-05-31 23:31:18 +03:00
local function swap_node(pos, name)
local node = minetest.get_node(pos)
if node.name == name then
return false
end
node.name = name
minetest.swap_node(pos, node)
return true
end
2022-09-24 12:01:30 +03:00
2020-05-31 23:31:18 +03:00
local function node_timer(pos)
2021-05-14 19:50:16 +03:00
if minecart.is_cart_available(pos, M(pos):get_int("param2"), 1) then
2020-05-31 23:31:18 +03:00
if swap_node(pos, "signs_bot:cart_sensor_on") then
signs_bot.send_signal(pos)
signs_bot.lib.activate_extender_nodes(pos, true)
end
else
swap_node(pos, "signs_bot:cart_sensor")
end
return true
end
minetest.register_node("signs_bot:cart_sensor", {
2021-05-14 19:50:16 +03:00
description = S("Cart Sensor"),
2020-05-31 23:31:18 +03:00
inventory_image = "signs_bot_sensor_cart_inv.png",
drawtype = "nodebox",
node_box = {
type = "fixed",
fixed = {
{ -11/32, -1/2, -11/32, 11/32, -5/16, 11/32},
},
},
tiles = {
-- up, down, right, left, back, front
"signs_bot_sensor1.png^signs_bot_sensor_cart.png",
"signs_bot_sensor1.png",
"signs_bot_sensor1.png^[transformFXR90",
"signs_bot_sensor1.png^[transformFXR90",
"signs_bot_sensor1.png^[transformFXR90",
"signs_bot_sensor1.png^[transformFXR180",
},
2022-09-24 12:01:30 +03:00
2020-05-31 23:31:18 +03:00
after_place_node = function(pos, placer)
local meta = M(pos)
2021-05-14 19:50:16 +03:00
meta:set_string("infotext", S("Cart Sensor: Not connected"))
2020-05-31 23:31:18 +03:00
minetest.get_node_timer(pos):start(CYCLE_TIME)
local node = minetest.get_node(pos)
meta:set_int("param2", (node.param2 + 2) % 4)
end,
2022-09-24 12:01:30 +03:00
2020-05-31 23:31:18 +03:00
on_timer = node_timer,
update_infotext = update_infotext,
on_rotate = screwdriver.disallow,
paramtype = "light",
2021-05-14 19:50:16 +03:00
use_texture_alpha = signs_bot.CLIP,
2020-05-31 23:31:18 +03:00
sunlight_propagates = true,
paramtype2 = "facedir",
is_ground_content = false,
groups = {sign_bot_sensor = 1, cracky = 1},
sounds = default.node_sound_metal_defaults(),
})
minetest.register_node("signs_bot:cart_sensor_on", {
2021-05-14 19:50:16 +03:00
description = S("Cart Sensor"),
2020-05-31 23:31:18 +03:00
drawtype = "nodebox",
node_box = {
type = "fixed",
fixed = {
{ -11/32, -1/2, -11/32, 11/32, -5/16, 11/32},
},
},
tiles = {
-- up, down, right, left, back, front
"signs_bot_sensor1.png^signs_bot_sensor_cart_on.png",
"signs_bot_sensor1.png",
"signs_bot_sensor1.png^[transformFXR90",
"signs_bot_sensor1.png^[transformFXR90",
"signs_bot_sensor1.png^[transformFXR90",
"signs_bot_sensor1.png^[transformFXR180",
},
2022-09-24 12:01:30 +03:00
2020-05-31 23:31:18 +03:00
on_timer = node_timer,
update_infotext = update_infotext,
on_rotate = screwdriver.disallow,
paramtype = "light",
2021-05-14 19:50:16 +03:00
use_texture_alpha = signs_bot.CLIP,
2020-05-31 23:31:18 +03:00
sunlight_propagates = true,
paramtype2 = "facedir",
is_ground_content = false,
diggable = false,
groups = {sign_bot_sensor = 1, not_in_creative_inventory = 1},
sounds = default.node_sound_metal_defaults(),
})
minetest.register_craft({
output = "signs_bot:cart_sensor",
recipe = {
{"", "", ""},
{"dye:black", "group:stone", "dye:yellow"},
{"default:copper_ingot", "default:mese_crystal_fragment", "default:steel_ingot"}
}
})
minetest.register_lbm({
label = "[signs_bot] Restart timer",
name = "signs_bot:crop_sensor_restart",
nodenames = {"signs_bot:cart_sensor", "signs_bot:cart_sensor_on"},
run_at_every_load = true,
action = function(pos, node)
minetest.get_node_timer(pos):start(CYCLE_TIME)
if node.name == "signs_bot:cart_sensor_on" then
signs_bot.send_signal(pos)
signs_bot.lib.activate_extender_nodes(pos, true)
end
end
})
if minetest.get_modpath("doc") then
doc.add_entry("signs_bot", "cart_sensor", {
2021-05-14 19:50:16 +03:00
name = S("Cart Sensor"),
2020-05-31 23:31:18 +03:00
data = {
item = "signs_bot:cart_sensor",
text = table.concat({
2021-05-14 19:50:16 +03:00
S("The Cart Sensor detects and sends a signal, if a cart (Minecart) is nearby."),
2022-09-24 12:01:30 +03:00
S("the sensor range is one node/meter."),
2021-05-14 19:50:16 +03:00
S("The sensor has an active side (red) that must point to the rail/cart."),
2022-09-24 12:01:30 +03:00
}, "\n")
2020-05-31 23:31:18 +03:00
},
})
end