ShadowNinja
2013-10-06 363f0332788e04e2e4bb63af5cd21fac5ae56ae5
commit | author | age
ee0765 1
S 2 function technic.register_solar_array(data)
3     local tier = data.tier
4     local ltier = string.lower(tier)
5     minetest.register_node("technic:solar_array_"..ltier, {
6         tiles = {"technic_"..ltier.."_solar_array_top.png",  "technic_"..ltier.."_solar_array_bottom.png",
7              "technic_"..ltier.."_solar_array_side.png", "technic_"..ltier.."_solar_array_side.png",
8              "technic_"..ltier.."_solar_array_side.png", "technic_"..ltier.."_solar_array_side.png"},
9         groups = {snappy=2, choppy=2, oddly_breakable_by_hand=2},
10         sounds = default.node_sound_wood_defaults(),
11         description = tier.." Solar Array",
12         active = false,
13         drawtype = "nodebox",
14         paramtype = "light",
15         node_box = {
16             type = "fixed",
17             fixed = {-0.5, -0.5, -0.5, 0.5, 0, 0.5},
18         },
19         technic = data,
20         on_construct = function(pos)
21             local meta = minetest.get_meta(pos)
22             local name = minetest.get_node(pos).name
23             local tier = minetest.registered_nodes[name].technic.tier
24             meta:set_int(tier.."_EU_supply", 0)
25         end,
26     })
27
28     minetest.register_abm({
29         nodenames = {"technic:solar_array_"..ltier},
30         interval = 1,
31         chance = 1,
32         action = function(pos, node, active_object_count, active_object_count_wider)
33             -- The action here is to make the solar array produce power
34             -- Power is dependent on the light level and the height above ground
35             -- 130m and above is optimal as it would be above cloud level.
36             -- Height gives 1/4 of the effect, light 3/4. Max. effect is 2880EU for the array.
37             -- There are many ways to cheat by using other light sources like lamps.
38             -- As there is no way to determine if light is sunlight that is just a shame.
39             -- To take care of some of it solar panels do not work outside daylight hours or if
40             -- built below -10m
41             local pos1 = {}
42             pos1.y = pos.y + 1
43             pos1.x = pos.x
44             pos1.z = pos.z
45             local light = minetest.get_node_light(pos1, nil)
46             local time_of_day = minetest.get_timeofday()
47             local meta = minetest.get_meta(pos)
48             light = light or 0
49             local data = minetest.registered_nodes[node.name].technic
50
51
52             -- turn on array only during day time and if sufficient light
53             -- I know this is counter intuitive when cheating by using other light sources.
54             if light >= 12 and time_of_day >= 0.24 and time_of_day <= 0.76 and pos.y > 0 then
55                 local charge_to_give = math.floor((light + pos.y) * data.power)
56                 charge_to_give = math.max(charge_to_give, 0)
57                 charge_to_give = math.min(charge_to_give, data.power * 50)
58                 meta:set_string("infotext", "Solar Array is active ("..charge_to_give.."EU)")
59                 meta:set_int(data.tier.."_EU_supply", charge_to_give)
60             else
61                 meta:set_string("infotext", "Solar Array is inactive");
62                 meta:set_int(data.tier.."_EU_supply", 0)
63             end
64         end,
65     })
66
67     technic.register_machine(tier, "technic:solar_array_"..ltier, technic.producer)
68 end
69