ShadowNinja
2014-12-15 5382a88aefbd503c00500236510412e6f2b42f4e
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").."]"..
74         "list[current_player;main;0,5;8,4;]"
6a0807 75     
N 76     if data.upgrade then
77         formspec = formspec..
78             "list[current_name;upgrade1;3.5,3;1,1;]"..
79             "list[current_name;upgrade2;4.5,3;1,1;]"..
80             "label[3.5,4;"..S("Upgrade Slots").."]"
81     end
ee0765 82
563a4c 83     local run = function(pos, node)
N 84         local meta           = minetest.get_meta(pos)
85         local eu_input       = meta:get_int(tier.."_EU_input")
86         local current_charge = meta:get_int("internal_EU_charge")
87
88         local EU_upgrade, tube_upgrade = 0, 0
89         if data.upgrade then
90             EU_upgrade, tube_upgrade = technic.handle_machine_upgrades(meta)
91         end
92         local max_charge = data.max_charge * (1 + EU_upgrade / 10)
93             
94         -- Charge/discharge the battery with the input EUs
95         if eu_input >= 0 then
96             current_charge = math.min(current_charge + eu_input, max_charge)
97         else
98             current_charge = math.max(current_charge + eu_input, 0)
99         end
100
101         -- Charging/discharging tools here
102         local tool_full, tool_empty
103         current_charge, tool_full = technic.charge_tools(meta,
104                 current_charge, data.charge_step)
105         current_charge, tool_empty = technic.discharge_tools(meta,
106                 current_charge, data.discharge_step,
107                 max_charge)
108             
109         if data.tube then
110             local inv = meta:get_inventory()
111             technic.handle_machine_pipeworks(pos, tube_upgrade,
112             function(pos, x_velocity, z_velocity)
113                 if tool_full and not inv:is_empty("src") then
114                     technic.send_items(pos, x_velocity, z_velocity, "src")
115                 elseif tool_empty and not inv:is_empty("dst") then
116                     technic.send_items(pos, x_velocity, z_velocity, "dst")
117                 end
118             end)
119         end
120
121         -- We allow batteries to charge on less than the demand
122         meta:set_int(tier.."_EU_demand",
123                 math.min(data.charge_rate, max_charge - current_charge))
124         meta:set_int(tier.."_EU_supply",
125                 math.min(data.discharge_rate, current_charge))
126             meta:set_int("internal_EU_charge", current_charge)
127
128         -- Select node textures
129         local charge_count = math.ceil((current_charge / max_charge) * 8)
130         charge_count = math.min(charge_count, 8)
131         charge_count = math.max(charge_count, 0)
132         local last_count = meta:get_float("last_side_shown")
133         if charge_count ~= last_count then
134             technic.swap_node(pos,"technic:"..ltier.."_battery_box"..charge_count)
135             meta:set_float("last_side_shown", charge_count)
136         end
137
138         local charge_percent = math.floor(current_charge / max_charge * 100)
139         meta:set_string("formspec",
140             formspec..
141             "image[1,1;1,2;technic_power_meter_bg.png"
142             .."^[lowpart:"..charge_percent
143             ..":technic_power_meter_fg.png]")
144
145         local infotext = S("%s Battery Box: %d/%d"):format(tier,
146                 current_charge, max_charge)
147         if eu_input == 0 then
148             infotext = S("%s Idle"):format(infotext)
149         end
150         meta:set_string("infotext", infotext)
151     end
152     
ee0765 153     for i = 0, 8 do
563a4c 154         local groups = {snappy=2, choppy=2, oddly_breakable_by_hand=2, technic_machine=1}
ee0765 155         if i ~= 0 then
S 156             groups.not_in_creative_inventory = 1
157         end
6a0807 158         
N 159         if data.tube then
160             groups.tubedevice = 1
161             groups.tubedevice_receiver = 1
162         end
163         
ee0765 164         minetest.register_node("technic:"..ltier.."_battery_box"..i, {
be2f30 165             description = S("%s Battery Box"):format(tier),
ee0765 166             tiles = {"technic_"..ltier.."_battery_box_top.png",
S 167                      "technic_"..ltier.."_battery_box_bottom.png",
168                  "technic_"..ltier.."_battery_box_side.png^technic_power_meter"..i..".png",
169                  "technic_"..ltier.."_battery_box_side.png^technic_power_meter"..i..".png",
170                  "technic_"..ltier.."_battery_box_side.png^technic_power_meter"..i..".png",
171                  "technic_"..ltier.."_battery_box_side.png^technic_power_meter"..i..".png"},
172             groups = groups,
6a0807 173             tube = data.tube and tube or nil,
N 174             paramtype2 = "facedir",
ee0765 175             sounds = default.node_sound_wood_defaults(),
S 176             drop = "technic:"..ltier.."_battery_box0",
177             on_construct = function(pos)
178                 local meta = minetest.get_meta(pos)
179                 local inv = meta:get_inventory()
180                 local node = minetest.get_node(pos)
181
76a8ac 182                 meta:set_string("infotext", S("%s Battery Box"):format(tier))
S 183                 meta:set_string("formspec", formspec)
184                 meta:set_int(tier.."_EU_demand", 0)
185                 meta:set_int(tier.."_EU_supply", 0)
186                 meta:set_int(tier.."_EU_input",  0)
ee0765 187                 meta:set_float("internal_EU_charge", 0)
S 188                 inv:set_size("src", 1)
189                 inv:set_size("dst", 1)
6a0807 190                 inv:set_size("upgrade1", 1)
N 191                 inv:set_size("upgrade2", 1)
ee0765 192             end,
0809dd 193             can_dig = technic.machine_can_dig,
S 194             allow_metadata_inventory_put = technic.machine_inventory_put,
195             allow_metadata_inventory_take = technic.machine_inventory_take,
196             allow_metadata_inventory_move = technic.machine_inventory_move,
563a4c 197             technic_run = run,
ee0765 198         })
S 199     end
200
201     -- Register as a battery type
202     -- Battery type machines function as power reservoirs and can both receive and give back power
203     for i = 0, 8 do
204         technic.register_machine(tier, "technic:"..ltier.."_battery_box"..i, technic.battery)
205     end
206
207 end -- End registration
208
209
eac484 210 function technic.charge_tools(meta, batt_charge, charge_step)
ee0765 211     local inv = meta:get_inventory()
eac484 212     if inv:is_empty("src") then
6a0807 213         return batt_charge, false
ee0765 214     end
5382a8 215     local src_stack = inv:get_stack("src", 1)
eac484 216
5382a8 217     local tool_name = src_stack:get_name()
S 218     if not technic.power_tools[tool_name] then
6a0807 219         return batt_charge, false
eac484 220     end
S 221     -- Set meta data for the tool if it didn't do it itself
5382a8 222     local src_meta = minetest.deserialize(src_stack:get_metadata()) or {}
eac484 223     if not src_meta.charge then
S 224         src_meta.charge = 0
225     end
226     -- Do the charging
5382a8 227     local item_max_charge = technic.power_tools[tool_name]
eac484 228     local tool_charge     = src_meta.charge
6a0807 229     if tool_charge >= item_max_charge then
N 230         return batt_charge, true
231     elseif batt_charge <= 0 then
232         return batt_charge, false
eac484 233     end
S 234     charge_step = math.min(charge_step, batt_charge)
235     charge_step = math.min(charge_step, item_max_charge - tool_charge)
236     tool_charge = tool_charge + charge_step
237     batt_charge = batt_charge - charge_step
5382a8 238     technic.set_RE_wear(src_stack, tool_charge, item_max_charge)
eac484 239     src_meta.charge = tool_charge
5382a8 240     src_stack:set_metadata(minetest.serialize(src_meta))
S 241     inv:set_stack("src", 1, src_stack)
6a0807 242     return batt_charge, (tool_charge == item_max_charge)
ee0765 243 end
S 244
245
eac484 246 function technic.discharge_tools(meta, batt_charge, charge_step, max_charge)
ee0765 247     local inv = meta:get_inventory()
eac484 248     if inv:is_empty("dst") then
6a0807 249         return batt_charge, false
ee0765 250     end
eac484 251     srcstack = inv:get_stack("dst", 1)
S 252     local toolname = srcstack:get_name()
253     if technic.power_tools[toolname] == nil then
6a0807 254         return batt_charge, false
eac484 255     end
S 256     -- Set meta data for the tool if it didn't do it itself :-(
5cf765 257     local src_meta = minetest.deserialize(srcstack:get_metadata())
eac484 258     src_meta = src_meta or {}
S 259     if not src_meta.charge then
260         src_meta.charge = 0
261     end
262
263     -- Do the discharging
264     local item_max_charge = technic.power_tools[toolname]
265     local tool_charge     = src_meta.charge
6a0807 266     if tool_charge <= 0 then
N 267         return batt_charge, true
268     elseif batt_charge >= max_charge then
269         return batt_charge, false
eac484 270     end
S 271     charge_step = math.min(charge_step, max_charge - batt_charge)
272     charge_step = math.min(charge_step, tool_charge)
273     tool_charge = tool_charge - charge_step
274     batt_charge = batt_charge + charge_step
275     technic.set_RE_wear(srcstack, tool_charge, item_max_charge)
276     src_meta.charge = tool_charge
5cf765 277     srcstack:set_metadata(minetest.serialize(src_meta))
eac484 278     inv:set_stack("dst", 1, srcstack)
6a0807 279     return batt_charge, (tool_charge == 0)
ee0765 280 end
S 281