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