mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-21 08:03:13 +09:00
using chests
This commit is contained in:
1 parent
ba3d01e2fe
commit
9b0349d518
18 files changed
+223
-1096
No files matched your search
+121
-215
@@ -1,236 +1,142 @@
|
||||
script.on_init(function()
|
||||
if global.timer == nil then
|
||||
global.timer = 0
|
||||
end
|
||||
if global.guitimer == nil then
|
||||
global.guitimer = 0
|
||||
end
|
||||
global.spentItems = {}
|
||||
global.items = {}
|
||||
global.items["iron-plate"] = 100
|
||||
end)
|
||||
-- GUI functions
|
||||
--[[
|
||||
/c game.player.gui.left.clusterio.destroy()
|
||||
game.player.gui.left.add{type="frame", name="clusterio", caption="Clusterio"}
|
||||
game.player.gui.left.clusterio.add{type="label", name="clusterioInputLabel", caption="Iron plates: "}
|
||||
game.player.gui.left.clusterio.add{type="textfield", name="clusterioInput"}
|
||||
game.player.gui.left.clusterio.add{type="button", name="clusterioButtonSave", caption="Save"}
|
||||
game.player.gui.left.clusterio.style.maximal_height=10
|
||||
]]--
|
||||
global.items = {}
|
||||
-- request items
|
||||
script.on_event(defines.events.on_tick, function(event)
|
||||
if global.guitimer > 1800 then
|
||||
global.guitimer = 0
|
||||
-- request spentItems * 2 - items left
|
||||
for k, v in pairs(spentItems) do
|
||||
_print(k, v, " | ", (v * 2) - items[k])
|
||||
game.write_file("order.txt", k .. " " .. (v * 2) - items[k] .. "\n", true)
|
||||
global.spentItems[k] = 0
|
||||
end
|
||||
|
||||
else
|
||||
global.guitimer = global.guitimer + 1
|
||||
end
|
||||
end)
|
||||
remote.add_interface("clusterio", {
|
||||
hello = function() game.player.print("Hi!") end,
|
||||
import = function(name, value)
|
||||
_print(name)
|
||||
_print(value)
|
||||
if(type(name) == "string" and type(tonumber(value)) == "number") then
|
||||
if(global.items == nil) then global.items = {} end
|
||||
global.items[name] = tonumber(value);
|
||||
require("config")
|
||||
|
||||
function HashPosition(position)
|
||||
return 40000 * (position.y) + position.x
|
||||
end
|
||||
|
||||
function OnBuiltEntity(event)
|
||||
local entity = event.created_entity
|
||||
--only add entities that are not ghosts
|
||||
if entity.type ~= "entity-ghost" then
|
||||
if entity.name == INPUT_CHEST_NAME then
|
||||
--if the table hasn't been initialized then do it
|
||||
global.inputChests = global.inputChests or {}
|
||||
--add the chests to a lists if these chests so they can be interated over
|
||||
global.inputChests[HashPosition(entity.position)] = entity
|
||||
elseif entity.name == OUTPUT_CHEST_NAME then
|
||||
--if the table hasn't been initialized then do it
|
||||
global.outputChests = global.outputChests or {}
|
||||
--add the chests to a lists if these chests so they can be interated over
|
||||
global.outputChests[HashPosition(entity.position)] = entity
|
||||
end
|
||||
end
|
||||
})
|
||||
script.on_load(function()
|
||||
if global.timer == nil then
|
||||
global.timer = 0
|
||||
end
|
||||
|
||||
function OnKilledEntity(event)
|
||||
local entity = event.entity
|
||||
if entity.type ~= "entity-ghost" then
|
||||
--remove the entities from the tables as they are dead
|
||||
if entity.name == INPUT_CHEST_NAME then
|
||||
global.inputChests[HashPosition(entity.position)] = nil
|
||||
elseif entity.name == OUTPUT_CHEST_NAME then
|
||||
global.outputChests[HashPosition(entity.position)] = nil
|
||||
end
|
||||
end
|
||||
if global.guitimer == nil then
|
||||
global.guitimer = 0
|
||||
end
|
||||
if global.spentItems == nil then
|
||||
global.spentItems = {}
|
||||
end
|
||||
global.items = {}
|
||||
end)
|
||||
script.on_event(defines.events.on_tick, function(event)
|
||||
if global.timer > 9 then
|
||||
global.timer = 0
|
||||
voidChests()
|
||||
else
|
||||
global.timer = global.timer + 1
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
|
||||
|
||||
--[[ Thing Creation Events ]]--
|
||||
script.on_event(defines.events.on_built_entity, function(event)
|
||||
if event.created_entity.name == "c-inv_voidChest" then
|
||||
if global.voidChests == nil then global.voidChests = {} end
|
||||
table.insert(global.voidChests, event.created_entity)
|
||||
end
|
||||
OnBuiltEntity(event)
|
||||
end)
|
||||
script.on_event(defines.events.on_robot_built_entity, function(event)
|
||||
OnBuiltEntity(event)
|
||||
end)
|
||||
|
||||
function voidChests()
|
||||
if global.voidChests ~= nil then
|
||||
for i = 1, #global.voidChests do
|
||||
if global.voidChests[i].valid then
|
||||
local chest = global.voidChests[i].get_inventory(1)
|
||||
if global.voidChests[i].get_inventory(defines.inventory.chest).get_contents() ~= nil then
|
||||
for k,v in pairs(global.voidChests[i].get_inventory(defines.inventory.chest).get_contents()) do
|
||||
-- WTF, string conectation in lua is done with .. instead of +?
|
||||
game.write_file("output.txt", k .. " " .. v .. "\n", true)
|
||||
end
|
||||
end
|
||||
global.voidChests[i].clear_items_inside()
|
||||
else
|
||||
table.remove(global.voidChests, i)
|
||||
break
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- spawn belts
|
||||
|
||||
local belts
|
||||
local spawn_item = "iron-ore"
|
||||
local chest_detection_rate = 50
|
||||
|
||||
|
||||
remote.add_interface("spawnbelt", {
|
||||
setitem = function(item)
|
||||
global.spawn_item = item;
|
||||
spawn_item = global.spawn_item;
|
||||
end
|
||||
})
|
||||
|
||||
script.on_load(function(event)
|
||||
if global.belts ~= nil then
|
||||
belts = global.belts;
|
||||
script.on_event(defines.events.on_tick, tick_belts);
|
||||
end
|
||||
if global.spawn_item ~= nil then
|
||||
spawn_item = global.spawn_item;
|
||||
end
|
||||
--[[ Thing Killing Events ]]--
|
||||
script.on_event(defines.events.on_entity_died, function(event)
|
||||
OnKilledEntity(event)
|
||||
end)
|
||||
script.on_event(defines.events.on_robot_pre_mined, function(event)
|
||||
OnKilledEntity(event)
|
||||
end)
|
||||
script.on_event(defines.events.on_preplayer_mined_item, function(event)
|
||||
OnKilledEntity(event)
|
||||
end)
|
||||
|
||||
script.on_event(defines.events.on_built_entity, function(event)
|
||||
if event.created_entity.name == "spawn-belt"
|
||||
or event.created_entity.name == "void-belt" then
|
||||
initalize_globals();
|
||||
new_belt = {};
|
||||
new_belt["entity"] = event.created_entity;
|
||||
new_belt["item"] = spawn_item;
|
||||
new_belt["chest"] = nil;
|
||||
table.insert(belts, new_belt)
|
||||
end
|
||||
|
||||
|
||||
|
||||
script.on_event(defines.events.on_tick, function(event)
|
||||
global.inputChests = global.inputChests or {}
|
||||
global.outputChests = global.outputChests or {}
|
||||
|
||||
HandleInputChests()
|
||||
HandleOutputChests()
|
||||
end)
|
||||
|
||||
function _print(...)
|
||||
local args = { n = select("#", ...); ... };
|
||||
local string = "";
|
||||
for i, player in pairs(game.players) do
|
||||
for i = 1, args.n do
|
||||
string = string .. serpent.block(args[i]) .. "\t";
|
||||
end
|
||||
player.print(string);
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
function initalize_globals()
|
||||
if global.belts == nil then
|
||||
global.belts = {};
|
||||
global.pumps = {};
|
||||
belts = global.belts;
|
||||
pumps = global.pumps;
|
||||
script.on_event(defines.events.on_tick, tick_belts)
|
||||
end
|
||||
end
|
||||
|
||||
function destroy_globals()
|
||||
if #global.belts == 0 then
|
||||
belts = nil;
|
||||
pumps = nil;
|
||||
global.pumps = nil;
|
||||
global.pumps = nil;
|
||||
script.on_event(defines.events.on_tick, nil);
|
||||
end
|
||||
end
|
||||
|
||||
function tick_belts(tick)
|
||||
for k, belt in ipairs(belts) do
|
||||
if belt.entity.valid ~= true then
|
||||
table.remove(belts, k)
|
||||
destroy_globals();
|
||||
else
|
||||
if belt.entity.name == "spawn-belt" then
|
||||
|
||||
-- On a lower interval rate, look for chests behind the belt to copy item type
|
||||
if tick.tick % chest_detection_rate == 0 then
|
||||
x = belt.entity.position.x;
|
||||
y = belt.entity.position.y;
|
||||
if belt.entity.direction == 0 then
|
||||
y = y + 1;
|
||||
elseif belt.entity.direction == 2 then
|
||||
x = x - 1;
|
||||
elseif belt.entity.direction == 4 then
|
||||
y = y - 1;
|
||||
elseif belt.entity.direction == 6 then
|
||||
x = x + 1;
|
||||
end
|
||||
|
||||
chests = belt.entity.surface.find_entities_filtered({area = {{x,y},{x,y}}, type="container"});
|
||||
if #chests > 0 then
|
||||
inventory = chests[1].get_inventory(defines.inventory.chest);
|
||||
if inventory ~= nil
|
||||
and inventory.valid == true
|
||||
and inventory.is_empty() == false
|
||||
and inventory[1].valid == true
|
||||
and inventory[1].valid_for_read == true then
|
||||
belt.item = inventory[1].name;
|
||||
function HandleInputChests()
|
||||
local linesToWriteToFile = {}
|
||||
for k, v in pairs(global.inputChests) do
|
||||
if v.valid then
|
||||
--get the content of the chest
|
||||
local items = v.get_inventory(defines.inventory.chest).get_contents()
|
||||
--write everything to the file
|
||||
for itemName, itemCount in pairs(items) do
|
||||
linesToWriteToFile[#linesToWriteToFile + 1] = itemName..", "..itemCount.."\n"
|
||||
end
|
||||
end
|
||||
-- clear the inventory
|
||||
v.get_inventory(defines.inventory.chest).clear()
|
||||
end
|
||||
|
||||
-- Fill the belt with selected item
|
||||
line1 = belt.entity.get_transport_line(1);
|
||||
if line1.can_insert_at_back() then
|
||||
if(global.items ~= nil and global.spentItems ~= nil) then
|
||||
if(global.items[belt.item] ~= nil and global.spentItems[belt.item] ~= nil) then
|
||||
if(global.items[belt.item] > 1) then
|
||||
global.items[belt.item] = global.items[belt.item] - 1;
|
||||
global.spentItems[belt.item] = global.spentItems[belt.item] + 1;
|
||||
line1.insert_at_back({name = belt.item});
|
||||
end
|
||||
if #linesToWriteToFile > 0 then
|
||||
--only write to file once as i/o is slow
|
||||
--it's much faster to concatenate all the lines with table.concat
|
||||
--instead of doing it with the .. operator
|
||||
game.write_file(INPUT_CHEST_FILE, table.concat(linesToWriteToFile), true)
|
||||
end
|
||||
end
|
||||
|
||||
function HandleOutputChests()
|
||||
local linesToWriteToFile = {}
|
||||
local simpleItemStack = {}
|
||||
for k, v in pairs(global.outputChests) do
|
||||
if v.valid and not v.to_be_deconstructed(v.force) then
|
||||
--get the inventory here once for faster execution
|
||||
local chestInventory = v.get_inventory(defines.inventory.chest)
|
||||
for i = 1, 10 do
|
||||
--the item the chest wants
|
||||
local requestItem = v.get_request_slot(i)
|
||||
if requestItem ~= nil then
|
||||
local itemsInChest = chestInventory.get_item_count(requestItem.name)
|
||||
--if there isn't enough items in the chest
|
||||
if itemsInChest < requestItem.count then
|
||||
simpleItemStack.name = requestItem.name
|
||||
simpleItemStack.count = requestItem.count - itemsInChest
|
||||
--insert the missing items
|
||||
local insertedItemsCount = chestInventory.insert(simpleItemStack)
|
||||
--write how many items was inserted
|
||||
linesToWriteToFile[#linesToWriteToFile + 1] = simpleItemStack.name..", "..insertedItemsCount.."\n"
|
||||
end
|
||||
else
|
||||
global.items[belt.item] = 0
|
||||
global.spentItems[belt.item] = 0
|
||||
end
|
||||
end
|
||||
end
|
||||
line2 = belt.entity.get_transport_line(2);
|
||||
if line2.can_insert_at_back() then
|
||||
if(global.items ~= nil) then
|
||||
if(global.items[belt.item] ~= nil) then
|
||||
if(global.items[belt.item] > 1) then
|
||||
global.items[belt.item] = global.items[belt.item] - 1;
|
||||
global.spentItems[belt.item] = global.spentItems[belt.item] + 1;
|
||||
line2.insert_at_back({name = belt.item});
|
||||
end
|
||||
else
|
||||
global.items[belt.item] = 0
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
elseif belt.entity.name == "void-belt" then
|
||||
belt.entity.get_transport_line(1).clear();
|
||||
belt.entity.get_transport_line(2).clear();
|
||||
end
|
||||
end
|
||||
end
|
||||
if #linesToWriteToFile > 0 then
|
||||
--only write to file once as i/o is slow
|
||||
--it's much faster to concatenate all the lines with table.concat
|
||||
--instead of doing it with the .. operator
|
||||
game.write_file(OUTPUT_CHEST_FILE, table.concat(linesToWriteToFile), true)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in new issue
Block a user