Vanessa Dannenberg
2018-11-27 bdd13beeff5045a34042439e39620b9ca550b214
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({
78f16c 149     label = "Machines: run power radiator",
ee0765 150     nodenames = {"technic:power_radiator"},
S 151     interval   = 1,
152     chance     = 1,
153     action = function(pos, node, active_object_count, active_object_count_wider)
a41390 154         local meta             = minetest.get_meta(pos)
ee0765 155         local eu_input  = meta:get_int("MV_EU_input")
S 156         local eu_demand = meta:get_int("MV_EU_demand")
157
158         -- Power off automatically if no longer connected to a switching station
159         technic.switching_station_timeout_count(pos, "MV")
160
161         if eu_input == 0 then
162             -- No power
7c4b70 163             meta:set_string("infotext", "MV Power Radiator is unpowered");
ee0765 164             -- meta:set_int("active", 1) -- used for setting textures someday maybe
S 165             shutdown_inductive_appliances(pos)
166             meta:set_int("connected_EU_demand", 0)
167             meta:set_int("MV_EU_demand",1)
168         elseif eu_input == eu_demand then
169             -- Powered and ready
170
171             -- The maximum EU sourcing a single radiator can provide.
172             local max_charge          = 30000 -- == the max EU demand of the radiator
173             local connected_EU_demand = meta:get_int("connected_EU_demand")
174
175             -- Efficiency factor
176             local eff_factor = 0.06
177             -- The supply radius
178             local rad = power_radius
179
180             local meta1            = nil
181             local pos1             = {}
182             local used_charge      = 0
183
184             -- Index all nodes within supply range
185             local positions = minetest.find_nodes_in_area(
186                 {x=pos.x-rad, y=pos.y-rad, z=pos.z-rad},
187                 {x=pos.x+rad, y=pos.y+rad, z=pos.z+rad},
188                 technic.inductive_nodes)
189             for _, pos1 in pairs(positions) do
190                 local meta1 = minetest.get_meta(pos1)
191                 -- If not supplied see if this node can handle it.
192                 if meta1:get_string("has_supply") == "" then
193                     -- if demand surpasses the capacity of this node, don't bother adding it.
194                     local app_eu_demand = math.floor(meta1:get_int("EU_demand") / eff_factor)
195                     if connected_EU_demand + app_eu_demand <= max_charge then
196                         -- We can power the appliance. Register, and spend power if it is on.
197                         connected_EU_demand = connected_EU_demand + app_eu_demand
198
199                         meta1:set_string("has_supply", pos.x..pos.y..pos.z)
200                         --Always 0: used_charge = math.floor(used_charge + meta1:get_int("EU_charge") / eff_factor)
201                     end
202                 elseif meta1:get_string("has_supply") == pos.x..pos.y..pos.z then
203                     -- The appliance has power from this node. Spend power if it is on.
204                     used_charge = used_charge + math.floor(meta1:get_int("EU_charge") / eff_factor)
205                 end
7c4b70 206                 meta:set_string("infotext", "MV Power Radiator is powered ("
ee0765 207                     ..math.floor(used_charge / max_charge * 100)
S 208                     .."% of maximum power)");
209                 if used_charge == 0 then
210                     meta:set_int("MV_EU_demand", 1) -- Still idle
211                 else
212                     meta:set_int("MV_EU_demand", used_charge)
213                 end
214             end
215             -- Save state
216             meta:set_int("connected_EU_demand", connected_EU_demand)
217         end
218     end,
219 })
220
221 technic.register_machine("MV", "technic:power_radiator", technic.receiver)
222