123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393 |
- workshop = workshop or {}
- workshop.modpath = minetest.get_modpath("machines")
- -- Localize for performance.
- local math_random = math.random
- local BUFFER_SIZE = tech.workshop.buffer
- local ENERGY_AMOUNT = tech.workshop.power
- local REPAIR_RATE = tech.workshop.repair
- workshop.compose_formspec =
- function(pos)
- local formspec =
- "size[8,4.5]" ..
- default.gui_bg ..
- default.gui_bg_img ..
- default.gui_slots ..
- "label[1.0,0.5;Upgrade]" ..
- "list[context;upg;1.0,1;1,1]" ..
- "label[3.5,0.5;Energy Buffer]" ..
- "list[context;buffer;3.5,1;1,1]" ..
- "label[6.0,0.5;Tool Repair]" ..
- "list[context;main;6.0,1;1,1]" ..
- "list[current_player;main;0,3.5;8,1;]" ..
- "listring[context;main]" ..
- "listring[current_player;main]" ..
- default.get_hotbar_bg(0, 3.5)
- return formspec
- end
- workshop.on_receive_fields =
- function(pos, formname, fields, sender)
- end
- workshop.compose_infotext =
- function(pos)
- local meta = minetest.get_meta(pos)
- local state = "Standby"
- local eups = 0
- if meta:get_int("active") == 1 then
- state = "Active"
- eups = ENERGY_AMOUNT
- end
- local infotext = "LV Tool Repair Shop (" .. state .. ")\n" ..
- "Demand: " .. eups .. " EU Per/Sec"
- return infotext
- end
- workshop.trigger_update =
- function(pos)
- local timer = minetest.get_node_timer(pos)
- -- Restart timer even if already running.
- timer:start(1.0)
- end
- workshop.on_punch =
- function(pos, node, puncher, pointed_thing)
- workshop.trigger_update(pos)
- local meta = minetest.get_meta(pos)
- local inv = meta:get_inventory()
- inv:set_size("upg", 1)
- end
- workshop.can_dig =
- function(pos, player)
- local meta = minetest.get_meta(pos)
- local inv = meta:get_inventory()
- return inv:is_empty("main") and inv:is_empty("upg")
- end
- workshop.on_timer =
- function(pos, elapsed)
- --minetest.chat_send_all("# Server: Elapsed time is " .. elapsed .. "!")
- local keeprunning = false
- local meta = minetest.get_meta(pos)
- local inv = meta:get_inventory()
- local the_tool -- Set to tool's itemstack if we have one.
- local got_energy = false
- local energy_gotten = 0
- -- Assuming we can keep running unless someone says otherwise.
- keeprunning = true
- do
- local tool = inv:get_stack("main", 1)
- if tool:get_count() == 1 and tool:get_stack_max() == 1 then
- local name = tool:get_name()
- local def = minetest.registered_tools[name]
- if def and not def.wear_represents then -- Wear cannot be anything but default.
- local wear = tool:get_wear()
- if wear == 0 then
- -- Tool fully repaired.
- -- (Or, it could be something that looks like a tool but doesn't implement wear.)
- keeprunning = false
- goto cancel
- else
- -- We got a tool and it needs repair.
- the_tool = tool
- end
- else
- -- Unknown item.
- keeprunning = false
- goto cancel
- end
- else
- -- No tool.
- keeprunning = false
- goto cancel
- end
- end
- -- Consume energy (but only if there is a tool to charge).
- if the_tool then
- local energy = inv:get_stack("buffer", 1)
- if energy:get_count() >= ENERGY_AMOUNT then
- energy:set_count(energy:get_count() - ENERGY_AMOUNT)
- inv:set_stack("buffer", 1, energy)
- -- We have enough energy.
- energy_gotten = ENERGY_AMOUNT
- got_energy = true
- else
- -- Try to get energy from network.
- local owner = meta:get_string("owner")
- local gotten = net2.get_energy(pos, owner, BUFFER_SIZE, "lv")
- if gotten >= ENERGY_AMOUNT then
- energy = ItemStack("atomic:energy " .. (energy:get_count() + gotten))
- inv:set_stack("buffer", 1, energy)
- -- Wait for next iteration before repairing again.
- goto cancel
- end
- -- Not enough energy!
- keeprunning = false
- goto cancel
- end
- end
- -- Repair the tool.
- if the_tool and got_energy then
- local wear = the_tool:get_wear()
- wear = wear - REPAIR_RATE
- if wear < 0 then
- wear = 0
- end
- the_tool:set_wear(wear)
- inv:set_stack("main", 1, the_tool)
- end
- -- Jump here if something prevents machine from working.
- ::cancel::
- -- Determine mode (active or sleep) and set timer accordingly.
- if keeprunning then
- minetest.get_node_timer(pos):start(1.0)
- meta:set_int("active", 1)
- else
- -- Slow down timer during sleep periods to reduce load.
- minetest.get_node_timer(pos):start(math_random(1, 3*60))
- meta:set_int("active", 0)
- end
- -- Update infotext.
- meta:set_string("formspec", workshop.compose_formspec(pos))
- meta:set_string("infotext", workshop.compose_infotext(pos))
- end
- workshop.on_construct =
- function(pos)
- end
- workshop.after_place_node =
- function(pos, placer, itemstack, pointed_thing)
- local meta = minetest.get_meta(pos)
- local node = minetest.get_node(pos)
- local owner = placer:get_player_name()
- local inv = meta:get_inventory()
- meta:set_string("owner", owner)
- meta:set_string("nodename", node.name)
- inv:set_size("buffer", 1)
- inv:set_size("main", 1)
- inv:set_size("upg", 1)
- net2.clear_caches(pos, owner, "lv")
- meta:set_string("formspec", workshop.compose_formspec(pos))
- meta:set_string("infotext", workshop.compose_infotext(pos))
- nodestore.add_node(pos)
- local timer = minetest.get_node_timer(pos)
- timer:start(1.0)
- end
- workshop.on_blast =
- function(pos)
- local drops = {}
- drops[#drops+1] = "workshop:workshop"
- default.get_inventory_drops(pos, "main", drops)
- minetest.remove_node(pos)
- return drops
- end
- workshop.has_public_access =
- function(pos)
- local meta = minetest.get_meta(pos)
- local inv = meta:get_inventory()
- -- There is only 1 upgrade slot.
- local s1 = inv:get_stack("upg", 1)
- if s1 and s1:get_count() > 0 then
- if minetest.get_item_group(s1:get_name(), "chest") > 0 then
- return true
- end
- end
- end
- workshop.allow_metadata_inventory_put =
- function(pos, listname, index, stack, player)
- local pname = player:get_player_name()
- local public = workshop.has_public_access(pos)
- local protected = false
- if minetest.test_protection(pos, pname) then
- protected = true
- end
- if listname == "main" and (not protected or public) then
- local def = minetest.registered_items[stack:get_name()]
- if def and not def.wear_represents then
- -- Only if wear amount represents the default usage.
- return 1
- end
- elseif listname == "upg" and not protected then
- if minetest.get_item_group(stack:get_name(), "chest") > 0 then
- return stack:get_count()
- elseif stack:get_name() == "battery:battery" then
- return stack:get_count()
- elseif stack:get_name() == "techcrafts:control_logic_unit" then
- return stack:get_count()
- end
- end
- return 0
- end
- workshop.allow_metadata_inventory_move =
- function(pos, from_list, from_index, to_list, to_index, count, player)
- local pname = player:get_player_name()
- local public = workshop.has_public_access(pos)
- local protected = false
- if minetest.test_protection(pos, pname) then
- protected = true
- end
- if (from_list == "upg" or to_list == "upg") and protected then
- -- Don't permit public users to mess with the upgrades.
- return 0
- end
- if not protected or public then
- -- Can't touch the output energy buffer.
- if from_list == "buffer" or to_list == "buffer" then
- return 0
- end
- if from_list == to_list then
- return count
- end
- end
- return 0
- end
- workshop.allow_metadata_inventory_take =
- function(pos, listname, index, stack, player)
- local pname = player:get_player_name()
- local public = workshop.has_public_access(pos)
- local protected = false
- if minetest.test_protection(pos, pname) then
- protected = true
- end
- if listname == "main" and (not protected or public) then
- return stack:get_count()
- elseif listname == "upg" and not protected then
- return stack:get_count()
- end
- return 0
- end
- workshop.on_metadata_inventory_move =
- function(pos)
- workshop.trigger_update(pos)
- end
- workshop.on_metadata_inventory_put =
- function(pos)
- workshop.trigger_update(pos)
- end
- workshop.on_metadata_inventory_take =
- function(pos, listname, index, stack, player)
- workshop.trigger_update(pos)
- end
- workshop.on_destruct =
- function(pos)
- local meta = minetest.get_meta(pos)
- net2.clear_caches(pos, meta:get_string("owner"), "lv")
- nodestore.del_node(pos)
- end
- if not workshop.run_once then
- minetest.register_node(":workshop:workshop", {
- description = "LV Tool Repair Shop\n\nRepairs tools.\nConnects to a power-network.",
- tiles = {
- "workshop_top.png",
- "workshop_bottom.png",
- "workshop_side.png",
- "workshop_side.png",
- "workshop_side.png",
- "workshop_side.png",
- },
- groups = utility.dig_groups("machine"),
- paramtype2 = "facedir",
- is_ground_content = false,
- sounds = default.node_sound_metal_defaults(),
- drop = "workshop:workshop",
- on_rotate = function(...)
- return screwdriver.rotate_simple(...) end,
- allow_metadata_inventory_put = function(...)
- return workshop.allow_metadata_inventory_put(...) end,
- allow_metadata_inventory_move = function(...)
- return workshop.allow_metadata_inventory_move(...) end,
- allow_metadata_inventory_take = function(...)
- return workshop.allow_metadata_inventory_take(...) end,
- on_metadata_inventory_move = function(...)
- return workshop.on_metadata_inventory_move(...) end,
- on_metadata_inventory_put = function(...)
- return workshop.on_metadata_inventory_put(...) end,
- on_metadata_inventory_take = function(...)
- return workshop.on_metadata_inventory_take(...) end,
- on_punch = function(...)
- return workshop.on_punch(...) end,
- can_dig = function(...)
- return workshop.can_dig(...) end,
- on_timer = function(...)
- return workshop.on_timer(...) end,
- on_construct = function(...)
- return workshop.on_construct(...) end,
- on_destruct = function(...)
- return workshop.on_destruct(...) end,
- on_blast = function(...)
- return workshop.on_blast(...) end,
- after_place_node = function(...)
- return workshop.after_place_node(...) end,
- on_receive_fields = function(...)
- return workshop.on_receive_fields(...) end,
- })
- ---[[
- minetest.register_craft({
- output = 'workshop:workshop',
- recipe = {
- {'group:wood', 'default:diamond', 'group:wood'},
- {'anvil:anvil', 'techcrafts:machine_casing', 'techcrafts:carbon_cloth'},
- {'default:obsidian', 'techcrafts:control_logic_unit', 'cobble_furnace:inactive'},
- }
- })
- --]]
- local c = "workshop:core"
- local f = workshop.modpath .. "/workshop.lua"
- reload.register_file(c, f, false)
- workshop.run_once = true
- end
|