RealBadAngel
2013-07-25 d8ec3f8bbee5c3c489e4c5740fc8c84449eb86c3
commit | author | age
ee5c6c 1 -- MV Battery box
8e03d7 2 minetest.register_craft(
13e885 3    {output = 'technic:mv_battery_box 1',
K 4     recipe = {
5        {'technic:battery_box', 'technic:battery_box', 'technic:battery_box'},
6        {'technic:battery_box', 'technic:mv_transformer', 'technic:battery_box'},
7        {'', 'technic:mv_cable', ''},
8     }
9  })
82cba9 10
ee5c6c 11 local battery_box_formspec =
3bc6ca 12     "invsize[8,9;]"..
R 13     "image[1,1;1,2;technic_power_meter_bg.png]"..
14     "list[current_name;src;3,1;1,1;]"..
15     "list[current_name;dst;5,1;1,1;]"..
16     "label[0,0;MV Battery Box]"..
17     "label[3,0;Charge]"..
18     "label[5,0;Discharge]"..
19     "label[1,3;Power level]"..
20     "list[current_player;main;0,5;8,4;]"..
21     "background[-0.19,-0.25;8.4,9.75;ui_form_bg.png]"..
22     "background[0,0;8,4;ui_mv_battery_box.png]"..
23     "background[0,5;8,4;ui_main_inventory.png]"
82cba9 24
8e03d7 25 minetest.register_node(
R 26    "technic:mv_battery_box", {
27       description = "MV Battery Box",
28       tiles  = {"technic_mv_battery_box_top.png", "technic_mv_battery_box_bottom.png", "technic_mv_battery_box_side0.png",
29             "technic_mv_battery_box_side0.png", "technic_mv_battery_box_side0.png", "technic_mv_battery_box_side0.png"},
30       groups = {snappy=2,choppy=2,oddly_breakable_by_hand=2},
31       sounds = default.node_sound_wood_defaults(),
32       drop   = "technic:mv_battery_box",
33       on_construct = function(pos)
34             if pos==nil then return end
35             local meta = minetest.env:get_meta(pos);
36             local inv = meta:get_inventory()
37             meta:set_string("infotext", "MV Battery box")
38             meta:set_float("technic_mv_power_machine", 1)
39             meta:set_string("formspec", battery_box_formspec)
ee5c6c 40             meta:set_int("MV_EU_demand", 0) -- How much can this node charge
K 41             meta:set_int("MV_EU_supply", 0) -- How much can this node discharge
42             meta:set_int("MV_EU_input",  0) -- How much power is this machine getting.
43             meta:set_float("internal_EU_charge", 0)
8e03d7 44             inv:set_size("src", 1)
R 45             inv:set_size("dst", 1)
46              end,
47       can_dig = function(pos,player)
48            local meta = minetest.env:get_meta(pos);
49            local inv = meta:get_inventory()
ee5c6c 50            if not inv:is_empty("src") or not inv:is_empty("dst") then
K 51               minetest.chat_send_player(player:get_player_name(), "Machine cannot be removed because it is not empty");
8e03d7 52               return false
ee5c6c 53            else
K 54               return true
8e03d7 55            end
ee5c6c 56         end,
8e03d7 57    })
82cba9 58
R 59
60 for i=1,8,1 do
8e03d7 61    minetest.register_node(
ee5c6c 62       "technic:mv_battery_box"..i,
K 63       {
8e03d7 64      description = "MV Battery Box",
R 65      tiles  = {"technic_mv_battery_box_top.png", "technic_mv_battery_box_bottom.png", "technic_mv_battery_box_side0.png^technic_power_meter"..i..".png",
66            "technic_mv_battery_box_side0.png^technic_power_meter"..i..".png", "technic_mv_battery_box_side0.png^technic_power_meter"..i..".png", "technic_mv_battery_box_side0.png^technic_power_meter"..i..".png"},
67      groups = {snappy=2,choppy=2,oddly_breakable_by_hand=2,not_in_creative_inventory=1},
68      sounds = default.node_sound_wood_defaults(),
69      drop   = "technic:mv_battery_box",
70      can_dig = function(pos,player)
71               local meta = minetest.env:get_meta(pos);
72               local inv = meta:get_inventory()
ee5c6c 73               if not inv:is_empty("src") or not inv:is_empty("dst") then
K 74              minetest.chat_send_player(player:get_player_name(), "Machine cannot be removed because it is not empty");
8e03d7 75              return false
ee5c6c 76               else
K 77              return true
8e03d7 78               end
ee5c6c 79            end,
8e03d7 80       })
82cba9 81 end
R 82
8e03d7 83 minetest.register_abm(
ee5c6c 84    {
K 85       nodenames = {"technic:mv_battery_box","technic:mv_battery_box1","technic:mv_battery_box2","technic:mv_battery_box3","technic:mv_battery_box4",
86            "technic:mv_battery_box5","technic:mv_battery_box6","technic:mv_battery_box7","technic:mv_battery_box8"
87         },
88       interval = 1,
89       chance   = 1,
90       action = function(pos, node, active_object_count, active_object_count_wider)
91         local meta               = minetest.env:get_meta(pos)
92         local max_charge         = 300000 -- Set maximum charge for the device here
93         local max_charge_rate    = 2000   -- Set maximum rate of charging (4000)
94         local max_discharge_rate = 3000   -- Set maximum rate of discharging
95         local eu_input           = meta:get_int("MV_EU_input")
96         local current_charge     = meta:get_int("internal_EU_charge") -- Battery charge right now
97
98         -- Power off automatically if no longer connected to a switching station
99         technic.switching_station_timeout_count(pos, "MV")
100
101         -- Charge/discharge the battery with the input EUs
102         if eu_input >=0 then
103            current_charge = math.min(current_charge+eu_input, max_charge)
104         else
105            current_charge = math.max(current_charge+eu_input, 0)
106         end
107
108         -- Charging/discharging tools here
3bc6ca 109         current_charge = charge_tools(meta, current_charge, 4000)
R 110         current_charge = discharge_tools(meta, current_charge, max_charge, 4000)
ee5c6c 111
K 112         -- Set a demand (we allow batteries to charge on less than the demand though)
113         meta:set_int("MV_EU_demand", math.min(max_charge_rate, max_charge-current_charge))
114
115         -- Set how much we can supply
116         meta:set_int("MV_EU_supply", math.min(max_discharge_rate, current_charge))
117
118         meta:set_int("internal_EU_charge", current_charge)
119         --dprint("BA: input:"..eu_input.." supply="..meta:get_int("MV_EU_supply").." demand="..meta:get_int("MV_EU_demand").." current:"..current_charge)
82cba9 120
8e03d7 121         -- Select node textures
ee5c6c 122         local i=math.ceil((current_charge/max_charge)*8)
8e03d7 123         if i > 8 then i = 8 end
R 124         local j = meta:get_float("last_side_shown")
125         if i~=j then
126            if i>0 then hacky_swap_node(pos,"technic:mv_battery_box"..i)
127            elseif i==0 then hacky_swap_node(pos,"technic:mv_battery_box") end
128            meta:set_float("last_side_shown",i)
82cba9 129         end
e23f87 130
ee5c6c 131         local load = math.floor(current_charge/max_charge * 100)
8e03d7 132         meta:set_string("formspec",
ee5c6c 133                 battery_box_formspec..
8e03d7 134                    "image[1,1;1,2;technic_power_meter_bg.png^[lowpart:"..
R 135                    (load)..":technic_power_meter_fg.png]"
136                  )
82cba9 137
ee5c6c 138         if eu_input == 0 then
K 139            meta:set_string("infotext", "MV Battery box: "..current_charge.."/"..max_charge.." (idle)")
140         else
141            meta:set_string("infotext", "MV Battery box: "..current_charge.."/"..max_charge)
8e03d7 142         end
R 143          end
ee5c6c 144    })
82cba9 145
8e03d7 146 -- Register as a battery type
R 147 -- Battery type machines function as power reservoirs and can both receive and give back power
ee5c6c 148 technic.register_MV_machine("technic:mv_battery_box","BA")
8e03d7 149 for i=1,8,1 do
ee5c6c 150    technic.register_MV_machine("technic:mv_battery_box"..i,"BA")
82cba9 151 end
R 152