est31
2015-06-18 a793747d92d9b1d93153c7fb4e0c82fe90624c78
commit | author | age
ee0765 1
be2f30 2 local S = technic.getter
ee0765 3
354ee6 4 technic.register_power_tool("technic:battery", 10000)
1b7fa3 5 technic.register_power_tool("technic:red_energy_crystal", 50000)
BM 6 technic.register_power_tool("technic:green_energy_crystal", 150000)
7 technic.register_power_tool("technic:blue_energy_crystal", 450000)
354ee6 8
S 9 minetest.register_craft({
10     output = 'technic:battery',
11     recipe = {
12         {'group:wood', 'default:copper_ingot', 'group:wood'},
13         {'group:wood', 'moreores:tin_ingot',   'group:wood'},
14         {'group:wood', 'default:copper_ingot', 'group:wood'},
15     }
16 })
17
18 minetest.register_tool("technic:battery", {
19     description = S("RE Battery"),
20     inventory_image = "technic_battery.png",
99fd5d 21     wear_represents = "technic_RE_charge",
00d7c9 22     on_refill = technic.refill_RE_charge,
354ee6 23     tool_capabilities = {
S 24         charge = 0,
25         max_drop_level = 0,
26         groupcaps = {
27             fleshy = {times={}, uses=10000, maxlevel=0}
28         }
29     }
30 })
31
6a0807 32 local tube = {
N 33     insert_object = function(pos, node, stack, direction)
34         if direction.y == 0 then
35             return stack
36         end
37         local meta = minetest.get_meta(pos)
38         local inv = meta:get_inventory()
39         if direction.y > 0 then
40             return inv:add_item("src", stack)
41         else
42             return inv:add_item("dst", stack)
43         end
44     end,
45     can_insert = function(pos, node, stack, direction)
46         if direction.y == 0 then
47             return false
48         end
49         local meta = minetest.get_meta(pos)
50         local inv = meta:get_inventory()
51         if direction.y > 0 then
52             return inv:room_for_item("src", stack)
53         else
54             return inv:room_for_item("dst", stack)
55         end
56     end,
57     connect_sides = {left=1, right=1, back=1, top=1, bottom=1},
58 }
354ee6 59
ee0765 60 function technic.register_battery_box(data)
S 61     local tier = data.tier
62     local ltier = string.lower(tier)
be2f30 63
76a8ac 64     local formspec =
be2f30 65         "invsize[8,9;]"..
S 66         "image[1,1;1,2;technic_power_meter_bg.png]"..
67         "list[current_name;src;3,1;1,1;]"..
68         "image[4,1;1,1;technic_battery_reload.png]"..
69         "list[current_name;dst;5,1;1,1;]"..
70         "label[0,0;"..S("%s Battery Box"):format(tier).."]"..
71         "label[3,0;"..S("Charge").."]"..
72         "label[5,0;"..S("Discharge").."]"..
73         "label[1,3;"..S("Power level").."]"..
d732c8 74         "list[current_player;main;0,5;8,4;]"..
E 75         "listring[current_name;dst]"..
76         "listring[current_player;main]"..
77         "listring[current_name;src]"..
78         "listring[current_player;main]"
79
6a0807 80     if data.upgrade then
N 81         formspec = formspec..
82             "list[current_name;upgrade1;3.5,3;1,1;]"..
83             "list[current_name;upgrade2;4.5,3;1,1;]"..
d732c8 84             "label[3.5,4;"..S("Upgrade Slots").."]"..
E 85             "listring[current_name;upgrade1]"..
86             "listring[current_player;main]"..
87             "listring[current_name;upgrade2]"..
88             "listring[current_player;main]"
6a0807 89     end
ee0765 90
563a4c 91     local run = function(pos, node)
N 92         local meta           = minetest.get_meta(pos)
93         local eu_input       = meta:get_int(tier.."_EU_input")
94         local current_charge = meta:get_int("internal_EU_charge")
95
96         local EU_upgrade, tube_upgrade = 0, 0
97         if data.upgrade then
98             EU_upgrade, tube_upgrade = technic.handle_machine_upgrades(meta)
99         end
100         local max_charge = data.max_charge * (1 + EU_upgrade / 10)
101             
102         -- Charge/discharge the battery with the input EUs
103         if eu_input >= 0 then
104             current_charge = math.min(current_charge + eu_input, max_charge)
105         else
106             current_charge = math.max(current_charge + eu_input, 0)
107         end
108
109         -- Charging/discharging tools here
110         local tool_full, tool_empty
111         current_charge, tool_full = technic.charge_tools(meta,
112                 current_charge, data.charge_step)
113         current_charge, tool_empty = technic.discharge_tools(meta,
114                 current_charge, data.discharge_step,
115                 max_charge)
116             
117         if data.tube then
118             local inv = meta:get_inventory()
119             technic.handle_machine_pipeworks(pos, tube_upgrade,
120             function(pos, x_velocity, z_velocity)
121                 if tool_full and not inv:is_empty("src") then
122                     technic.send_items(pos, x_velocity, z_velocity, "src")
123                 elseif tool_empty and not inv:is_empty("dst") then
124                     technic.send_items(pos, x_velocity, z_velocity, "dst")
125                 end
126             end)
127         end
128
129         -- We allow batteries to charge on less than the demand
130         meta:set_int(tier.."_EU_demand",
131                 math.min(data.charge_rate, max_charge - current_charge))
132         meta:set_int(tier.."_EU_supply",
133                 math.min(data.discharge_rate, current_charge))
134             meta:set_int("internal_EU_charge", current_charge)
135
136         -- Select node textures
137         local charge_count = math.ceil((current_charge / max_charge) * 8)
138         charge_count = math.min(charge_count, 8)
139         charge_count = math.max(charge_count, 0)
140         local last_count = meta:get_float("last_side_shown")
141         if charge_count ~= last_count then
142             technic.swap_node(pos,"technic:"..ltier.."_battery_box"..charge_count)
143             meta:set_float("last_side_shown", charge_count)
144         end
145
146         local charge_percent = math.floor(current_charge / max_charge * 100)
147         meta:set_string("formspec",
148             formspec..
149             "image[1,1;1,2;technic_power_meter_bg.png"
150             .."^[lowpart:"..charge_percent
151             ..":technic_power_meter_fg.png]")
152
4b1798 153         local infotext = S("@1 Battery Box: @2/@3", tier,
E 154                 technic.prettynum(current_charge), technic.prettynum(max_charge))
563a4c 155         if eu_input == 0 then
N 156             infotext = S("%s Idle"):format(infotext)
157         end
158         meta:set_string("infotext", infotext)
159     end
160     
ee0765 161     for i = 0, 8 do
563a4c 162         local groups = {snappy=2, choppy=2, oddly_breakable_by_hand=2, technic_machine=1}
ee0765 163         if i ~= 0 then
S 164             groups.not_in_creative_inventory = 1
165         end
6a0807 166         
N 167         if data.tube then
168             groups.tubedevice = 1
169             groups.tubedevice_receiver = 1
170         end
171         
ee0765 172         minetest.register_node("technic:"..ltier.."_battery_box"..i, {
be2f30 173             description = S("%s Battery Box"):format(tier),
ee0765 174             tiles = {"technic_"..ltier.."_battery_box_top.png",
S 175                      "technic_"..ltier.."_battery_box_bottom.png",
176                  "technic_"..ltier.."_battery_box_side.png^technic_power_meter"..i..".png",
177                  "technic_"..ltier.."_battery_box_side.png^technic_power_meter"..i..".png",
178                  "technic_"..ltier.."_battery_box_side.png^technic_power_meter"..i..".png",
179                  "technic_"..ltier.."_battery_box_side.png^technic_power_meter"..i..".png"},
180             groups = groups,
6a0807 181             tube = data.tube and tube or nil,
N 182             paramtype2 = "facedir",
ee0765 183             sounds = default.node_sound_wood_defaults(),
S 184             drop = "technic:"..ltier.."_battery_box0",
185             on_construct = function(pos)
186                 local meta = minetest.get_meta(pos)
187                 local inv = meta:get_inventory()
188                 local node = minetest.get_node(pos)
189
76a8ac 190                 meta:set_string("infotext", S("%s Battery Box"):format(tier))
S 191                 meta:set_string("formspec", formspec)
192                 meta:set_int(tier.."_EU_demand", 0)
193                 meta:set_int(tier.."_EU_supply", 0)
194                 meta:set_int(tier.."_EU_input",  0)
ee0765 195                 meta:set_float("internal_EU_charge", 0)
S 196                 inv:set_size("src", 1)
197                 inv:set_size("dst", 1)
6a0807 198                 inv:set_size("upgrade1", 1)
N 199                 inv:set_size("upgrade2", 1)
ee0765 200             end,
0809dd 201             can_dig = technic.machine_can_dig,
S 202             allow_metadata_inventory_put = technic.machine_inventory_put,
203             allow_metadata_inventory_take = technic.machine_inventory_take,
204             allow_metadata_inventory_move = technic.machine_inventory_move,
563a4c 205             technic_run = run,
011397 206             after_place_node = data.tube and pipeworks.after_place,
c8cbd2 207             after_dig_node = technic.machine_after_dig_node
ee0765 208         })
S 209     end
210
211     -- Register as a battery type
212     -- Battery type machines function as power reservoirs and can both receive and give back power
213     for i = 0, 8 do
214         technic.register_machine(tier, "technic:"..ltier.."_battery_box"..i, technic.battery)
215     end
216
217 end -- End registration
218
219
eac484 220 function technic.charge_tools(meta, batt_charge, charge_step)
ee0765 221     local inv = meta:get_inventory()
eac484 222     if inv:is_empty("src") then
6a0807 223         return batt_charge, false
ee0765 224     end
5382a8 225     local src_stack = inv:get_stack("src", 1)
eac484 226
5382a8 227     local tool_name = src_stack:get_name()
S 228     if not technic.power_tools[tool_name] then
6a0807 229         return batt_charge, false
eac484 230     end
S 231     -- Set meta data for the tool if it didn't do it itself
5382a8 232     local src_meta = minetest.deserialize(src_stack:get_metadata()) or {}
eac484 233     if not src_meta.charge then
S 234         src_meta.charge = 0
235     end
236     -- Do the charging
5382a8 237     local item_max_charge = technic.power_tools[tool_name]
eac484 238     local tool_charge     = src_meta.charge
6a0807 239     if tool_charge >= item_max_charge then
N 240         return batt_charge, true
241     elseif batt_charge <= 0 then
242         return batt_charge, false
eac484 243     end
S 244     charge_step = math.min(charge_step, batt_charge)
245     charge_step = math.min(charge_step, item_max_charge - tool_charge)
246     tool_charge = tool_charge + charge_step
247     batt_charge = batt_charge - charge_step
5382a8 248     technic.set_RE_wear(src_stack, tool_charge, item_max_charge)
eac484 249     src_meta.charge = tool_charge
5382a8 250     src_stack:set_metadata(minetest.serialize(src_meta))
S 251     inv:set_stack("src", 1, src_stack)
6a0807 252     return batt_charge, (tool_charge == item_max_charge)
ee0765 253 end
S 254
255
eac484 256 function technic.discharge_tools(meta, batt_charge, charge_step, max_charge)
ee0765 257     local inv = meta:get_inventory()
eac484 258     if inv:is_empty("dst") then
6a0807 259         return batt_charge, false
ee0765 260     end
eac484 261     srcstack = inv:get_stack("dst", 1)
S 262     local toolname = srcstack:get_name()
263     if technic.power_tools[toolname] == nil then
6a0807 264         return batt_charge, false
eac484 265     end
S 266     -- Set meta data for the tool if it didn't do it itself :-(
5cf765 267     local src_meta = minetest.deserialize(srcstack:get_metadata())
eac484 268     src_meta = src_meta or {}
S 269     if not src_meta.charge then
270         src_meta.charge = 0
271     end
272
273     -- Do the discharging
274     local item_max_charge = technic.power_tools[toolname]
275     local tool_charge     = src_meta.charge
6a0807 276     if tool_charge <= 0 then
N 277         return batt_charge, true
278     elseif batt_charge >= max_charge then
279         return batt_charge, false
eac484 280     end
S 281     charge_step = math.min(charge_step, max_charge - batt_charge)
282     charge_step = math.min(charge_step, tool_charge)
283     tool_charge = tool_charge - charge_step
284     batt_charge = batt_charge + charge_step
285     technic.set_RE_wear(srcstack, tool_charge, item_max_charge)
286     src_meta.charge = tool_charge
5cf765 287     srcstack:set_metadata(minetest.serialize(src_meta))
eac484 288     inv:set_stack("dst", 1, srcstack)
6a0807 289     return batt_charge, (tool_charge == 0)
ee0765 290 end
S 291