Some fixes/improvements, Solar Generators

This commit is contained in:
Evert Prants 2018-09-14 15:14:34 +03:00
parent 7f281eadf0
commit b8aa8d735f
Signed by: evert
GPG Key ID: 1688DA83D222D0B5
10 changed files with 199 additions and 17 deletions

View File

@ -144,7 +144,7 @@ function elepm.register_storage(nodename, nodedef)
end
for i = 0, levels do
local cpdef = ele.helpers.table_copy(nodedef)
local cpdef = table.copy(nodedef)
-- Add overlay to the tile texture
if cpdef.tiles and cpdef.tiles[6] and i > 0 then

View File

@ -5,18 +5,6 @@
ele.helpers = {}
function ele.helpers.table_copy(tab)
local retval = {}
for k, v in pairs(tab) do
if type(v) == "table" then
retval[k] = ele.helpers.table_copy(v)
else
retval[k] = v
end
end
return retval
end
function ele.helpers.round(v)
return math.floor(v + 0.5)
end
@ -25,7 +13,7 @@ function ele.helpers.swap_node(pos, noded)
local node = minetest.get_node(pos)
if type(noded) ~= "table" then
local n = ele.helpers.table_copy(node)
local n = table.copy(node)
n.name = noded
noded = n
end
@ -90,7 +78,7 @@ end
function ele.helpers.face_front(pos, fd)
local back = minetest.facedir_to_dir(fd)
local front = ele.helpers.table_copy(back)
local front = table.copy(back)
front.x = front.x * -1 + pos.x
front.y = front.y * -1 + pos.y

View File

@ -391,7 +391,7 @@ function ele.register_base_device(nodename, nodedef)
-- Register an active variant if configured.
if nodedef.ele_active_node then
local active_nodedef = ele.helpers.table_copy(nodedef)
local active_nodedef = table.copy(nodedef)
active_name = nodename.."_active"
if nodedef.ele_active_node ~= true then
@ -413,7 +413,6 @@ function ele.register_base_device(nodename, nodedef)
minetest.register_node(active_name, active_nodedef)
end
-- tubelib support
if tlsupp then
local extras = {}

View File

@ -0,0 +1,10 @@
-- Solar Generator
minetest.register_craft({
output = "elepower_solar:solar_generator",
recipe = {
{"group:glass", "elepower_dynamics:pv_cell", "group:glass"},
{"elepower_dynamics:pv_cell", "group:glass", "elepower_dynamics:pv_cell"},
{"default:steel_ingot", "default:steel_ingot", "default:steel_ingot"},
}
})

View File

@ -0,0 +1,2 @@
elepower_papi
elepower_dynamics

View File

@ -0,0 +1,155 @@
local function get_formspec_default(power, percent, state)
if not percent then percent = 0 end
return "size[8,8.5]"..
default.gui_bg..
default.gui_bg_img..
default.gui_slots..
ele.formspec.power_meter(power)..
ele.formspec.state_switcher(7, 0, state)..
"image[3.5,1.5;1,1;default_furnace_fire_bg.png^[lowpart:"..
(percent)..":default_furnace_fire_fg.png]"..
"list[current_player;main;0,4.25;8,1;]"..
"list[current_player;main;0,5.5;8,3;8]"..
default.get_hotbar_bg(0, 4.25)
end
-- A generator that creates power using sunlight
function elesolar.register_solar_generator(nodename, nodedef)
-- Enable de-dusting mechanics
local dedust = nodedef.ele_solar_dust == nil or nodedef.ele_solar_dust ~= false
-- Allow for custom formspec
local get_formspec = get_formspec_default
if nodedef.get_formspec then
get_formspec = nodedef.get_formspec
nodedef.get_formspec = nil
end
local defaults = {
groups = {
ele_provider = 1,
oddly_breakable_by_hand = 1,
ele_solar_generator = 1,
},
tube = false,
ele_usage = 8,
on_timer = function (pos, elapsed)
local refresh = true
local meta = minetest.get_meta(pos)
local nodename = nodename
local capacity = ele.helpers.get_node_property(meta, pos, "capacity")
local generation = ele.helpers.get_node_property(meta, pos, "usage")
local storage = ele.helpers.get_node_property(meta, pos, "storage")
local state = meta:get_int("state")
local is_enabled = ele.helpers.state_enabled(meta, pos, state)
local pow_buffer = {capacity = capacity, storage = storage, usage = 0}
local status = "Idle"
local solarp = 0
while true do
if not is_enabled then
status = "Off"
break
end
if storage + generation > capacity then
break
end
-- Raycasting: Make sure the solar generator is not obstructed
-- The ray is only casted every 5 seconds instead of every second
local raystep = meta:get_int("ray_timer")
local rayokay = meta:get_int("ray_success")
local pos1 = vector.add(pos, {x=0,y=1,z=0})
local pos2 = vector.add(pos1, {x=0,y=8,z=0})
if raystep == 5 then
local ray = minetest.raycast(pos1, pos2, false, true)
local found_obstruction = false
-- Take 8 samples
for i = 1, 8 do
local pointed_thing = ray:next()
if pointed_thing and pointed_thing.type ~= "nothing" then
found_obstruction = true
break
end
end
if found_obstruction and rayokay == 1 then
rayokay = 0
elseif not found_obstruction and rayokay == 0 then
rayokay = 1
end
meta:set_int("ray_timer", 0)
else
meta:set_int("ray_timer", raystep + 1)
end
meta:set_int("ray_success", rayokay)
if rayokay == 0 then break end
local light = minetest.get_node_light(pos1, nil) or 0
local time_of_day = minetest.get_timeofday()
-- turn on array only during day time and if sufficient light
solarp = light / (minetest.LIGHT_MAX + 1)
if light >= 12 and time_of_day >= 0.24 and time_of_day <= 0.76 then
pow_buffer.usage = math.floor(solarp * generation)
if pow_buffer.usage > 0 then
pow_buffer.storage = pow_buffer.storage + pow_buffer.usage
status = "Active"
end
end
solarp = math.floor(100 * solarp)
break
end
meta:set_string("formspec", get_formspec(pow_buffer, solarp, state))
meta:set_string("infotext", ("%s %s\n%s\nLight: %s"):format(nodedef.description, status,
ele.capacity_text(capacity, pow_buffer.storage), solarp .. " %"))
meta:set_int("storage", pow_buffer.storage)
return refresh
end,
on_construct = function (pos)
local meta = minetest.get_meta(pos)
local capacity = ele.helpers.get_node_property(meta, pos, "capacity")
local storage = ele.helpers.get_node_property(meta, pos, "storage")
meta:set_string("formspec", get_formspec({capacity = capacity, storage = storage, usage = 0}, 0))
end
}
for key,val in pairs(defaults) do
if not nodedef[key] then
nodedef[key] = val
end
end
ele.register_machine(nodename, nodedef)
end
minetest.register_lbm({
label = "Enable Solar Generators on load",
name = "elepower_solar:solar_generators",
nodenames = {"group:ele_solar_generator"},
run_at_every_load = true,
action = function (pos)
local t = minetest.get_node_timer(pos)
if not t:is_started() then
t:start(1.0)
end
end,
})

11
elepower_solar/init.lua Normal file
View File

@ -0,0 +1,11 @@
-- A Elepower Mod
-- Copyright 2018 Evert "Diamond" Prants <evert@lunasqu.ee>
local modpath = minetest.get_modpath(minetest.get_current_modname())
elesolar = rawget(_G, "elesolar") or {}
elesolar.modpath = modpath
dofile(modpath.."/generator.lua")
dofile(modpath.."/register.lua")
dofile(modpath.."/crafting.lua")

3
elepower_solar/mod.conf Normal file
View File

@ -0,0 +1,3 @@
name = elepower_solar
description = Harvest the sun!
depends = elepower_papi,elepower_dynamics

View File

@ -0,0 +1,14 @@
elesolar.register_solar_generator("elepower_solar:solar_generator", {
description = "Solar Generator",
drawtype = "nodebox",
node_box = {
type = "fixed",
fixed = {
{-0.5000, -0.5000, -0.5000, 0.5000, -0.4375, 0.5000}
}
},
tiles = {
"elesolar_simple_top.png", "elepower_machine_top.png^elepower_power_port.png", "elepower_machine_top.png"
}
})

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.7 KiB