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