Vanessa Ezekowitz
2017-03-10 343c7946d9014bf111e25a7a225a1b6f5746992b
commit | author | age
ee0765 1 -- The power radiator fuctions like an inductive charger
S 2 -- only better in the game setting.
3 -- The purpose is to allow small appliances to receive power
4 -- without the overhead of the wiring needed for larger machines.
5 --
6 -- The power radiator will consume power corresponding to the
7 -- sum(power rating of the attached appliances)/0.06
8 -- Using inductive power transfer is very inefficient so this is
9 -- set to the factor 0.06.
10 --
11 -- Punching the radiator will toggle the power state of all attached appliances.
12
13 local power_radius = 12
14
83c649 15
S 16 minetest.register_craft({
17     output = 'technic:power_radiator 1',
18     recipe = {
19         {'technic:stainless_steel_ingot', 'technic:mv_transformer', 'technic:stainless_steel_ingot'},
20         {'technic:copper_coil',           'technic:machine_casing', 'technic:copper_coil'},
21         {'technic:rubber',                'technic:mv_cable',       'technic:rubber'},
22     }
23 })
24
ee0765 25 ------------------------------------------------------------------
S 26 -- API for inductive powered nodes:
27 -- Use the functions below to set the corresponding callbacks
28 -- Also two nodes are needed: The inactive and the active one. The active must be called <name>_active .
29 ------------------------------------------------------------------
30 -- Register a new appliance using this function
31
32 technic.inductive_nodes = {}
33 technic.register_inductive_machine = function(name)
34     table.insert(technic.inductive_nodes, name)
35     table.insert(technic.inductive_nodes, name.."_active")
36 end
37
38 -- Appliances:
39 --  has_supply: pos of supply node if the appliance has a power radiator near with sufficient power for the demand else ""
40 --  EU_demand: The power demand of the device.
41 --  EU_charge: Actual use. set to EU_demand if active==1
42 --  active: set to 1 if the device is on
43 technic.inductive_on_construct = function(pos, eu_demand, infotext)
44     local meta = minetest.get_meta(pos)
45     meta:set_string("infotext", infotext)
46     meta:set_int("technic_inductive_power_machine", 1)
47     meta:set_int("EU_demand", eu_demand)     -- The power demand of this appliance
48     meta:set_int("EU_charge", 0)       -- The actual power draw of this appliance
49     meta:set_string("has_supply", "") -- Register whether we are powered or not. For use with several radiators.
50     meta:set_int("active", 0)    -- If the appliance can be turned on and off by using it use this.
51 end
52
53 technic.inductive_on_punch_off = function(pos, eu_charge, swapnode)
54     local meta = minetest.get_meta(pos)
55     if meta:get_string("has_supply") ~= "" then
f3d8b4 56         technic.swap_node(pos, swapnode)
ee0765 57         meta:set_int("active", 1)
S 58         meta:set_int("EU_charge",eu_charge)
59         --print("-----------")
60         --print("Turn on:")
61         --print("EU_charge: "..meta:get_int("EU_charge"))
62         --print("has_supply: "..meta:get_string("has_supply"))
63         --print("<----------->")
64     end
65 end
66
67 technic.inductive_on_punch_on = function(pos, eu_charge, swapnode)
68     local meta = minetest.get_meta(pos)
f3d8b4 69     technic.swap_node(pos, swapnode)
ee0765 70     meta:set_int("active", 0)
S 71     meta:set_int("EU_charge",eu_charge)
72     --print("-----------")
73     --print("Turn off:")
74     --print("EU_charge: "..meta:get_int("EU_charge"))
75     --print("has_supply: "..meta:get_string("has_supply"))
76     --print("<---------->")
77 end
78
79 local shutdown_inductive_appliances = function(pos)
80     -- The supply radius
81     local rad = power_radius
82     -- If the radiator is removed. turn off all appliances in region
83     -- If another radiator is near it will turn on the appliances again
84     local positions = minetest.find_nodes_in_area(
85         {x=pos.x-rad, y=pos.y-rad, z=pos.z-rad},
86         {x=pos.x+rad, y=pos.y+rad, z=pos.z+rad},
87         technic.inductive_nodes)
88     for _, pos1 in pairs(positions) do
89         local meta1 = minetest.get_meta(pos1)
90         -- If the appliance is belonging to this node
91         if meta1:get_string("has_supply") == pos.x..pos.y..pos.z then
92             local nodename = minetest.get_node(pos1).name
93             -- Swap the node and make sure it is off and unpowered
94             if string.sub(nodename, -7) == "_active" then
f3d8b4 95                 technic.swap_node(pos1, string.sub(nodename, 1, -8))
ee0765 96                 meta1:set_int("active", 0)
S 97                 meta1:set_int("EU_charge", 0)
98             end
99             meta1:set_string("has_supply", "")
100         end
101     end
102 end
103
104 local toggle_on_off_inductive_appliances = function(pos, node, puncher)
105     if pos == nil then return end
106     -- The supply radius
107     local rad = power_radius
108     local positions = minetest.find_nodes_in_area(
109         {x=pos.x-rad, y=pos.y-rad, z=pos.z-rad},
110         {x=pos.x+rad, y=pos.y+rad, z=pos.z+rad},
111         technic.inductive_nodes)
112     for _, pos1 in pairs(positions) do
113         local meta1 = minetest.get_meta(pos1)
114         if meta1:get_string("has_supply") == pos.x..pos.y..pos.z then
115             minetest.punch_node(pos1)
116         end
117     end
118 end
119
120 minetest.register_node("technic:power_radiator", {
7c4b70 121     description = "MV Power Radiator",
ee0765 122     tiles  = {"technic_lv_cable.png", "technic_lv_cable.png", "technic_lv_cable.png",
S 123               "technic_lv_cable.png", "technic_lv_cable.png", "technic_lv_cable.png"},
124     groups = {snappy=2, choppy=2, oddly_breakable_by_hand=2},
125     sounds = default.node_sound_wood_defaults(),
126     drawtype = "nodebox",
127     paramtype = "light",
128     is_ground_content = true,
129     node_box = {
130         type = "fixed",
131         fixed = {-0.5, -0.5, -0.5, 0.5, 0.5, 0.5},
132     },
133     on_construct = function(pos)
134         local meta = minetest.get_meta(pos)
135         meta:set_int("MV_EU_demand",1)               -- Demand on the primary side when idle
136         meta:set_int("connected_EU_demand",0)        -- Potential demand of connected appliances
7c4b70 137         meta:set_string("infotext", "MV Power Radiator")
ee0765 138     end,
S 139     on_dig = function(pos, node, digger)
140         shutdown_inductive_appliances(pos)
141         return minetest.node_dig(pos, node, digger)
142     end,
143     on_punch = function(pos, node, puncher)
144         toggle_on_off_inductive_appliances(pos, node, puncher)
145     end
146 })
147
148 minetest.register_abm({
149     nodenames = {"technic:power_radiator"},
150     interval   = 1,
151     chance     = 1,
152     action = function(pos, node, active_object_count, active_object_count_wider)
a41390 153         local meta             = minetest.get_meta(pos)
ee0765 154         local eu_input  = meta:get_int("MV_EU_input")
S 155         local eu_demand = meta:get_int("MV_EU_demand")
156
157         -- Power off automatically if no longer connected to a switching station
158         technic.switching_station_timeout_count(pos, "MV")
159
160         if eu_input == 0 then
161             -- No power
7c4b70 162             meta:set_string("infotext", "MV Power Radiator is unpowered");
ee0765 163             -- meta:set_int("active", 1) -- used for setting textures someday maybe
S 164             shutdown_inductive_appliances(pos)
165             meta:set_int("connected_EU_demand", 0)
166             meta:set_int("MV_EU_demand",1)
167         elseif eu_input == eu_demand then
168             -- Powered and ready
169
170             -- The maximum EU sourcing a single radiator can provide.
171             local max_charge          = 30000 -- == the max EU demand of the radiator
172             local connected_EU_demand = meta:get_int("connected_EU_demand")
173
174             -- Efficiency factor
175             local eff_factor = 0.06
176             -- The supply radius
177             local rad = power_radius
178
179             local meta1            = nil
180             local pos1             = {}
181             local used_charge      = 0
182
183             -- Index all nodes within supply range
184             local positions = minetest.find_nodes_in_area(
185                 {x=pos.x-rad, y=pos.y-rad, z=pos.z-rad},
186                 {x=pos.x+rad, y=pos.y+rad, z=pos.z+rad},
187                 technic.inductive_nodes)
188             for _, pos1 in pairs(positions) do
189                 local meta1 = minetest.get_meta(pos1)
190                 -- If not supplied see if this node can handle it.
191                 if meta1:get_string("has_supply") == "" then
192                     -- if demand surpasses the capacity of this node, don't bother adding it.
193                     local app_eu_demand = math.floor(meta1:get_int("EU_demand") / eff_factor)
194                     if connected_EU_demand + app_eu_demand <= max_charge then
195                         -- We can power the appliance. Register, and spend power if it is on.
196                         connected_EU_demand = connected_EU_demand + app_eu_demand
197
198                         meta1:set_string("has_supply", pos.x..pos.y..pos.z)
199                         --Always 0: used_charge = math.floor(used_charge + meta1:get_int("EU_charge") / eff_factor)
200                     end
201                 elseif meta1:get_string("has_supply") == pos.x..pos.y..pos.z then
202                     -- The appliance has power from this node. Spend power if it is on.
203                     used_charge = used_charge + math.floor(meta1:get_int("EU_charge") / eff_factor)
204                 end
7c4b70 205                 meta:set_string("infotext", "MV Power Radiator is powered ("
ee0765 206                     ..math.floor(used_charge / max_charge * 100)
S 207                     .."% of maximum power)");
208                 if used_charge == 0 then
209                     meta:set_int("MV_EU_demand", 1) -- Still idle
210                 else
211                     meta:set_int("MV_EU_demand", used_charge)
212                 end
213             end
214             -- Save state
215             meta:set_int("connected_EU_demand", connected_EU_demand)
216         end
217     end,
218 })
219
220 technic.register_machine("MV", "technic:power_radiator", technic.receiver)
221