sfence
2022-11-04 66e20a9231b9fea823f9b683e495c57ef9a4ad41
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:
0f6bdb 39 --  has_supply: pos of supply node if the appliance has a power radiator near
S 40 --              with sufficient power for the demand else ""
ee0765 41 --  EU_demand: The power demand of the device.
S 42 --  EU_charge: Actual use. set to EU_demand if active==1
43 --  active: set to 1 if the device is on
44 technic.inductive_on_construct = function(pos, eu_demand, infotext)
45     local meta = minetest.get_meta(pos)
46     meta:set_string("infotext", infotext)
47     meta:set_int("technic_inductive_power_machine", 1)
48     meta:set_int("EU_demand", eu_demand)     -- The power demand of this appliance
49     meta:set_int("EU_charge", 0)       -- The actual power draw of this appliance
50     meta:set_string("has_supply", "") -- Register whether we are powered or not. For use with several radiators.
51     meta:set_int("active", 0)    -- If the appliance can be turned on and off by using it use this.
52 end
53
54 technic.inductive_on_punch_off = function(pos, eu_charge, swapnode)
55     local meta = minetest.get_meta(pos)
56     if meta:get_string("has_supply") ~= "" then
f3d8b4 57         technic.swap_node(pos, swapnode)
ee0765 58         meta:set_int("active", 1)
S 59         meta:set_int("EU_charge",eu_charge)
60         --print("-----------")
61         --print("Turn on:")
62         --print("EU_charge: "..meta:get_int("EU_charge"))
63         --print("has_supply: "..meta:get_string("has_supply"))
64         --print("<----------->")
65     end
66 end
67
68 technic.inductive_on_punch_on = function(pos, eu_charge, swapnode)
69     local meta = minetest.get_meta(pos)
f3d8b4 70     technic.swap_node(pos, swapnode)
ee0765 71     meta:set_int("active", 0)
S 72     meta:set_int("EU_charge",eu_charge)
73     --print("-----------")
74     --print("Turn off:")
75     --print("EU_charge: "..meta:get_int("EU_charge"))
76     --print("has_supply: "..meta:get_string("has_supply"))
77     --print("<---------->")
78 end
79
80 local shutdown_inductive_appliances = function(pos)
81     -- The supply radius
82     local rad = power_radius
83     -- If the radiator is removed. turn off all appliances in region
84     -- If another radiator is near it will turn on the appliances again
85     local positions = minetest.find_nodes_in_area(
86         {x=pos.x-rad, y=pos.y-rad, z=pos.z-rad},
87         {x=pos.x+rad, y=pos.y+rad, z=pos.z+rad},
88         technic.inductive_nodes)
89     for _, pos1 in pairs(positions) do
90         local meta1 = minetest.get_meta(pos1)
91         -- If the appliance is belonging to this node
92         if meta1:get_string("has_supply") == pos.x..pos.y..pos.z then
93             local nodename = minetest.get_node(pos1).name
94             -- Swap the node and make sure it is off and unpowered
95             if string.sub(nodename, -7) == "_active" then
f3d8b4 96                 technic.swap_node(pos1, string.sub(nodename, 1, -8))
ee0765 97                 meta1:set_int("active", 0)
S 98                 meta1:set_int("EU_charge", 0)
99             end
100             meta1:set_string("has_supply", "")
101         end
102     end
103 end
104
105 local toggle_on_off_inductive_appliances = function(pos, node, puncher)
106     if pos == nil then return end
107     -- The supply radius
108     local rad = power_radius
109     local positions = minetest.find_nodes_in_area(
110         {x=pos.x-rad, y=pos.y-rad, z=pos.z-rad},
111         {x=pos.x+rad, y=pos.y+rad, z=pos.z+rad},
112         technic.inductive_nodes)
113     for _, pos1 in pairs(positions) do
114         local meta1 = minetest.get_meta(pos1)
115         if meta1:get_string("has_supply") == pos.x..pos.y..pos.z then
116             minetest.punch_node(pos1)
117         end
118     end
119 end
120
121 minetest.register_node("technic:power_radiator", {
7c4b70 122     description = "MV Power Radiator",
ee0765 123     tiles  = {"technic_lv_cable.png", "technic_lv_cable.png", "technic_lv_cable.png",
S 124               "technic_lv_cable.png", "technic_lv_cable.png", "technic_lv_cable.png"},
125     groups = {snappy=2, choppy=2, oddly_breakable_by_hand=2},
126     sounds = default.node_sound_wood_defaults(),
127     drawtype = "nodebox",
128     paramtype = "light",
129     is_ground_content = true,
130     node_box = {
131         type = "fixed",
132         fixed = {-0.5, -0.5, -0.5, 0.5, 0.5, 0.5},
133     },
134     on_construct = function(pos)
135         local meta = minetest.get_meta(pos)
136         meta:set_int("MV_EU_demand",1)               -- Demand on the primary side when idle
137         meta:set_int("connected_EU_demand",0)        -- Potential demand of connected appliances
7c4b70 138         meta:set_string("infotext", "MV Power Radiator")
ee0765 139     end,
S 140     on_dig = function(pos, node, digger)
141         shutdown_inductive_appliances(pos)
142         return minetest.node_dig(pos, node, digger)
143     end,
144     on_punch = function(pos, node, puncher)
145         toggle_on_off_inductive_appliances(pos, node, puncher)
146     end
147 })
148
149 minetest.register_abm({
78f16c 150     label = "Machines: run power radiator",
ee0765 151     nodenames = {"technic:power_radiator"},
S 152     interval   = 1,
153     chance     = 1,
154     action = function(pos, node, active_object_count, active_object_count_wider)
a41390 155         local meta             = minetest.get_meta(pos)
ee0765 156         local eu_input  = meta:get_int("MV_EU_input")
S 157         local eu_demand = meta:get_int("MV_EU_demand")
158
159         -- Power off automatically if no longer connected to a switching station
160         technic.switching_station_timeout_count(pos, "MV")
161
162         if eu_input == 0 then
163             -- No power
7c4b70 164             meta:set_string("infotext", "MV Power Radiator is unpowered");
ee0765 165             -- meta:set_int("active", 1) -- used for setting textures someday maybe
S 166             shutdown_inductive_appliances(pos)
167             meta:set_int("connected_EU_demand", 0)
168             meta:set_int("MV_EU_demand",1)
169         elseif eu_input == eu_demand then
170             -- Powered and ready
171
172             -- The maximum EU sourcing a single radiator can provide.
173             local max_charge          = 30000 -- == the max EU demand of the radiator
174             local connected_EU_demand = meta:get_int("connected_EU_demand")
175
176             -- Efficiency factor
177             local eff_factor = 0.06
178             -- The supply radius
179             local rad = power_radius
180
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