techage_modpack/techage/lamps/growlight.lua

225 lines
5.3 KiB
Lua
Raw Normal View History

2020-05-31 23:31:18 +03:00
--[[
TechAge
=======
Copyright (C) 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
2022-01-04 21:43:30 +03:00
2020-05-31 23:31:18 +03:00
TA4 LED Grow Light
]]--
local S = techage.S
local CYCLE_TIME = 2
local RANDOM_VAL = 20
2021-08-01 12:00:22 +03:00
local PWR_NEEDED = 1
2020-05-31 23:31:18 +03:00
local Cable = techage.ElectricCable
2021-08-01 12:00:22 +03:00
local power = networks.power
2020-05-31 23:31:18 +03:00
local Flowers = {}
local Plants = {}
2020-12-11 20:07:29 +03:00
local Ignore = { ["flowers:waterlily_waving"] = true }
2020-05-31 23:31:18 +03:00
-- 9 plant positions below the light
2022-01-04 21:43:30 +03:00
local Positions = {
2020-05-31 23:31:18 +03:00
{x = 0, y =-1, z = 0},
{x =-1, y =-1, z = 0},
{x = 0, y =-1, z =-1},
{x = 1, y =-1, z = 0},
{x = 0, y =-1, z = 1},
{x =-1, y =-1, z =-1},
{x = 1, y =-1, z = 1},
{x =-1, y =-1, z = 1},
{x = 1, y =-1, z =-1},
}
2021-08-01 12:00:22 +03:00
local function swap_node(pos, postfix)
local node = techage.get_node_lvm(pos)
local parts = string.split(node.name, "_")
if postfix == parts[2] then
return
end
node.name = parts[1].."_"..postfix
minetest.swap_node(pos, node)
techage.light_ring(pos, postfix == "on")
end
local function on_nopower(pos)
swap_node(pos, "off")
local nvm = techage.get_nvm(pos)
nvm.turned_on = false
end
local function on_power(pos)
swap_node(pos, "on")
local nvm = techage.get_nvm(pos)
nvm.turned_on = true
end
local function grow_flowers(pos)
2020-05-31 23:31:18 +03:00
local nvm = techage.get_nvm(pos)
local mem = techage.get_mem(pos)
2020-07-21 18:45:15 +03:00
mem.grow_pos = mem.grow_pos or {} -- keep the pos blank for some time
2020-05-31 23:31:18 +03:00
nvm.tick = nvm.tick or math.random(RANDOM_VAL, RANDOM_VAL*2)
nvm.tick = nvm.tick - 1
if nvm.tick == 0 then
nvm.tick = math.random(RANDOM_VAL, RANDOM_VAL*2)
local plant_idx = math.random(1, 9)
local plant_pos = vector.add(pos, Positions[plant_idx])
local soil_pos = {x = plant_pos.x, y = plant_pos.y - 1, z = plant_pos.z}
local plant_node = minetest.get_node(plant_pos)
local soil_node = minetest.get_node(soil_pos)
if soil_node and soil_node.name == "compost:garden_soil" then
if plant_node and plant_node.name == "air" then
2024-08-25 20:49:57 +03:00
if mem.grow_pos[plant_idx] and #Flowers > 1 then
2020-05-31 23:31:18 +03:00
local idx = math.floor(math.random(1, #Flowers))
2020-07-02 22:30:02 +03:00
if Flowers[idx] then
minetest.set_node(plant_pos, {name = Flowers[idx]})
mem.grow_pos[plant_idx] = false
end
2020-05-31 23:31:18 +03:00
else
mem.grow_pos[plant_idx] = true
end
elseif plant_node and Plants[plant_node.name] then
local ndef = minetest.registered_nodes[plant_node.name]
ndef.on_timer(plant_pos, 200)
else
mem.grow_pos[plant_idx] = false
end
end
end
2021-08-01 12:00:22 +03:00
end
local function node_timer_on(pos, elapsed)
grow_flowers(pos)
local consumed = power.consume_power(pos, Cable, nil, PWR_NEEDED)
if consumed < PWR_NEEDED then
on_nopower(pos)
end
return true
end
local function node_timer_off(pos, elapsed)
local consumed = power.consume_power(pos, Cable, nil, PWR_NEEDED)
if consumed == PWR_NEEDED then
on_power(pos)
end
2020-05-31 23:31:18 +03:00
return true
end
local function on_switch_lamp(pos, on)
techage.light_ring(pos, on)
end
techage.register_lamp("techage:growlight", {
description = S("TA4 LED Grow Light"),
tiles = {
-- up, down, right, left, back, front
'techage_growlight_off.png',
'techage_growlight_back.png',
'techage_growlight_off.png',
'techage_growlight_side.png',
'techage_growlight_side.png',
'techage_growlight_side.png',
'techage_growlight_side.png',
'techage_growlight_side.png',
},
drawtype = "nodebox",
node_box = {
type = "fixed",
fixed = {
{-8/16, -8/16, -8/16, 8/16, -13/32, 8/16},
},
},
2021-08-16 21:22:44 +03:00
on_timer = node_timer_off,
on_switch_lamp = on_switch_lamp,
2020-05-31 23:31:18 +03:00
high_power = true,
},{
description = S("TA4 LED Grow Light"),
tiles = {
-- up, down, right, left, back, front
'techage_growlight_on.png',
'techage_growlight_back.png',
'techage_growlight_side.png',
'techage_growlight_side.png',
'techage_growlight_side.png',
'techage_growlight_side.png',
},
drawtype = "nodebox",
node_box = {
type = "fixed",
fixed = {
{-8/16, -8/16, -8/16, 8/16, -13/32, 8/16},
},
},
2021-08-01 12:00:22 +03:00
on_timer = node_timer_on,
2021-08-16 21:22:44 +03:00
on_switch_lamp = on_switch_lamp,
2020-05-31 23:31:18 +03:00
high_power = true,
})
minetest.register_craft({
output = "techage:growlight_off",
recipe = {
{"techage:ta4_leds", "techage:basalt_glass_thin", "techage:ta4_leds"},
{"techage:ta4_leds", "techage:ta4_leds", "techage:ta4_leds"},
{"techage:ta4_leds", "techage:aluminum", "techage:ta4_leds"},
},
})
2023-02-04 17:45:23 +03:00
local function contains(table, element)
for _, value in pairs(table) do
if value == element then
return true
end
end
return false
end
2020-05-31 23:31:18 +03:00
function techage.register_flower(name)
2023-05-09 19:41:56 +03:00
if contains(Flowers, name) then
return
2023-02-04 17:45:23 +03:00
end
2020-05-31 23:31:18 +03:00
Flowers[#Flowers+1] = name
end
function techage.register_plant(name)
Plants[name] = true
end
minetest.after(1, function()
2024-08-25 20:49:57 +03:00
local function add_flower(name)
local def = minetest.registered_nodes[name]
if def and (def.groups.mushroom == 1 or def.groups.flower == 1) then
if not Ignore[name] then
techage.register_flower(name)
end
end
end
2020-09-13 14:24:06 +03:00
for _,def in pairs(minetest.registered_decorations) do
local name = def.decoration
2024-08-25 20:49:57 +03:00
if type(name) == "string" then
add_flower(name)
elseif type(name) == "table" then
for _,sub_name in ipairs(name) do
add_flower(sub_name)
2020-05-31 23:31:18 +03:00
end
end
end
for name,ndef in pairs(minetest.registered_nodes) do
if type(name) == "string" then
local mod = string.split(name, ":")[1]
2020-09-13 14:24:06 +03:00
if mod == "farming" and ndef.on_timer then -- probably a plant that still needs to grow
2020-12-11 20:07:29 +03:00
if not Ignore[name] then
techage.register_plant(name)
end
2020-05-31 23:31:18 +03:00
end
end
end
end)