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