mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-12 07:04:52 +09:00
Merge pull request #31 from Danielv123/dev
Resolves #30 adds inventory combinator
This commit is contained in:
commit
528165367c
8 files changed
+150
-52
No files matched your search
@@ -107,6 +107,22 @@ setInterval(function() {
|
||||
}
|
||||
}, 3000)
|
||||
// COMBINATOR SIGNALS ---------------------------------------------------------
|
||||
// get inventory from Master and RCON it to our slave
|
||||
setInterval(function() {
|
||||
needle.get(config.masterIP + ":" + config.masterPort + '/inventory', function(err, response, body){
|
||||
if(response && response.body) {
|
||||
// Take the inventory we (hopefully) got and turn it into the format LUA accepts
|
||||
// console.log(response.body)
|
||||
var inventory = response.body;
|
||||
var inventoryFrame = {};
|
||||
for(i = 0;i<inventory.length;i++) {
|
||||
inventoryFrame[inventory[i].name] = Number(inventory[i].count);
|
||||
}
|
||||
console.log("RCONing inventory! " + JSON.stringify(inventoryFrame));
|
||||
client.exec("/silent-command remote.call('clusterio', 'receiveInventory', '" + JSON.stringify(inventoryFrame) + "')");
|
||||
}
|
||||
});
|
||||
}, 1000)
|
||||
// send any signals the slave has been told to send
|
||||
setInterval(function() {
|
||||
// Fetch combinator signals from the server
|
||||
|
||||
@@ -21,12 +21,12 @@ OUTPUT_TANK_ICON_PATH = nil
|
||||
MAX_RX_BUFFER_SIZE = 40 -- slightly more than one second
|
||||
RX_COMBINATOR_NAME = "get-combinator"
|
||||
TX_COMBINATOR_NAME = "put-combinator"
|
||||
|
||||
INV_COMBINATOR_NAME = "inventory-combinator"
|
||||
|
||||
OUTPUT_FILE = "output.txt"
|
||||
ORDER_FILE = "orders.txt"
|
||||
TX_BUFFER_FILE = "txbuffer.txt"
|
||||
|
||||
|
||||
UPATE_RATE = 60
|
||||
CIRCUIT_UPATE_RATE = 5
|
||||
UPDATE_RATE = 60
|
||||
CIRCUIT_UPDATE_RATE = 5
|
||||
@@ -1,10 +1,6 @@
|
||||
require("config")
|
||||
local json = require("json")
|
||||
|
||||
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
|
||||
@@ -27,26 +23,25 @@ end
|
||||
|
||||
function AddEntity(entity)
|
||||
if entity.name == INPUT_CHEST_NAME then
|
||||
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
|
||||
global.inputChests[entity.unit_number] = entity
|
||||
elseif entity.name == OUTPUT_CHEST_NAME then
|
||||
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
|
||||
global.outputChests[entity.unit_number] = entity
|
||||
elseif entity.name == INPUT_TANK_NAME then
|
||||
global.inputTanks = global.inputTanks or {}
|
||||
--add the chests to a lists if these chests so they can be interated over
|
||||
global.inputTanks[HashPosition(entity.position)] = entity
|
||||
global.inputTanks[entity.unit_number] = entity
|
||||
elseif entity.name == OUTPUT_TANK_NAME then
|
||||
global.outputTanks = global.outputTanks or {}
|
||||
--add the chests to a lists if these chests so they can be interated over
|
||||
global.outputTanks[HashPosition(entity.position)] = entity
|
||||
global.outputTanks[entity.unit_number] = entity
|
||||
entity.active = false
|
||||
elseif entity.name == TX_COMBINATOR_NAME then
|
||||
table.insert(global.txControls, entity.get_or_create_control_behavior())
|
||||
global.txControls[entity.unit_number] = entity.get_or_create_control_behavior()
|
||||
elseif entity.name == RX_COMBINATOR_NAME then
|
||||
table.insert(global.rxControls, entity.get_or_create_control_behavior())
|
||||
global.rxControls[entity.unit_number] = entity.get_or_create_control_behavior()
|
||||
entity.operable=false
|
||||
elseif entity.name == INV_COMBINATOR_NAME then
|
||||
global.invControls[entity.unit_number] = entity.get_or_create_control_behavior()
|
||||
entity.operable=false
|
||||
end
|
||||
end
|
||||
@@ -56,14 +51,20 @@ function OnKilledEntity(event)
|
||||
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
|
||||
global.inputChests[entity.unit_number] = nil
|
||||
elseif entity.name == OUTPUT_CHEST_NAME then
|
||||
global.outputChests[HashPosition(entity.position)] = nil
|
||||
global.outputChests[entity.unit_number] = nil
|
||||
elseif entity.name == INPUT_TANK_NAME then
|
||||
global.inputTanks[HashPosition(entity.position)] = nil
|
||||
global.inputTanks[entity.unit_number] = nil
|
||||
elseif entity.name == OUTPUT_TANK_NAME then
|
||||
global.outputTanks[HashPosition(entity.position)] = nil
|
||||
end
|
||||
global.outputTanks[entity.unit_number] = nil
|
||||
elseif entity.name == TX_COMBINATOR_NAME then
|
||||
global.txControls[entity.unit_number] = nil
|
||||
elseif entity.name == RX_COMBINATOR_NAME then
|
||||
global.rxControls[entity.unit_number] = nil
|
||||
elseif entity.name == INV_COMBINATOR_NAME then
|
||||
global.invControls[entity.unit_number] = nil
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -89,22 +90,21 @@ script.on_event(defines.events.on_preplayer_mined_item, function(event)
|
||||
end)
|
||||
|
||||
|
||||
script.on_init(function()
|
||||
Reset()
|
||||
end)
|
||||
|
||||
script.on_configuration_changed(function(data)
|
||||
if data.mod_changes and data.mod_changes["clusterio"] then
|
||||
Reset()
|
||||
end
|
||||
end)
|
||||
|
||||
script.on_event(defines.events.on_tick, function(event)
|
||||
|
||||
global.inputChests = global.inputChests or {}
|
||||
global.outputChests = global.outputChests or {}
|
||||
global.inputTanks = global.inputTanks or {}
|
||||
global.outputTanks = global.outputTanks or {}
|
||||
global.outputList = global.outputList or {}
|
||||
global.inputList = global.inputList or {}
|
||||
global.rxControls = global.rxControls or {}
|
||||
global.txControls = global.txControls or {}
|
||||
|
||||
|
||||
-- TX Combinators must run every tick to catch single pulses
|
||||
HandleTXCombinators()
|
||||
|
||||
local todo = game.tick % UPATE_RATE
|
||||
local todo = game.tick % UPDATE_RATE
|
||||
|
||||
local onlinePlayers = GetOnlinePlayerCount()
|
||||
|
||||
@@ -127,7 +127,7 @@ script.on_event(defines.events.on_tick, function(event)
|
||||
ExportOutputList()
|
||||
end
|
||||
|
||||
local rxstate = game.tick % CIRCUIT_UPATE_RATE
|
||||
local rxstate = game.tick % CIRCUIT_UPDATE_RATE
|
||||
-- RX Combinators are set and then cleared on sequential ticks to create pulses
|
||||
if rxstate == 0 then
|
||||
SetRXCombinators()
|
||||
@@ -153,13 +153,23 @@ function Reset()
|
||||
|
||||
global.inputChests = {}
|
||||
global.outputChests = {}
|
||||
global.inputTanks = {}
|
||||
|
||||
global.inputTanks = {}
|
||||
global.outputTanks = {}
|
||||
|
||||
global.rxControls = {}
|
||||
global.txControls = {}
|
||||
global.invControls = {}
|
||||
|
||||
AddAllEntitiesOfName(INPUT_CHEST_NAME)
|
||||
AddAllEntitiesOfName(OUTPUT_CHEST_NAME)
|
||||
|
||||
AddAllEntitiesOfName(INPUT_TANK_NAME)
|
||||
AddAllEntitiesOfName(OUTPUT_TANK_NAME)
|
||||
|
||||
AddAllEntitiesOfName(RX_COMBINATOR_NAME)
|
||||
AddAllEntitiesOfName(TX_COMBINATOR_NAME)
|
||||
AddAllEntitiesOfName(INV_COMBINATOR_NAME)
|
||||
game.print("reset")
|
||||
end
|
||||
|
||||
@@ -283,7 +293,7 @@ function ExportInputList()
|
||||
--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_FILE, table.concat(exportStrings), true)
|
||||
game.write_file(OUTPUT_FILE, table.concat(exportStrings), true, global.write_file_player or 0)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -298,13 +308,12 @@ function ExportOutputList()
|
||||
--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(ORDER_FILE, table.concat(exportStrings), true)
|
||||
game.write_file(ORDER_FILE, table.concat(exportStrings), true, global.write_file_player or 0)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
function RequestItemsFromStorage(itemName, itemCount)
|
||||
global.itemStorage = global.itemStorage or {}
|
||||
--if result is nil then there is no items in storage
|
||||
--which means that no items can be given
|
||||
if global.itemStorage[itemName] == nil then
|
||||
@@ -319,7 +328,6 @@ function RequestItemsFromStorage(itemName, itemCount)
|
||||
end
|
||||
|
||||
function GiveItemsToStorage(itemName, itemCount)
|
||||
global.itemStorage = global.itemStorage or {}
|
||||
--if this is called for the first time for an item then the result
|
||||
--is nil. if that's the case then set the result to 0 so it can
|
||||
--be used in arithmetic operations
|
||||
@@ -379,8 +387,6 @@ function HandleTXCombinators()
|
||||
(signals[signal.signal.type][signal.signal.name] or 0) + signal.count
|
||||
end
|
||||
end
|
||||
else
|
||||
table.remove(global.txControls,i)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -392,8 +398,11 @@ function HandleTXCombinators()
|
||||
end
|
||||
|
||||
if #frame > 0 then
|
||||
if global.worldID then
|
||||
table.insert(frame,1,{count=global.worldID,name="signal-srcid",type="virtual"})
|
||||
end
|
||||
table.insert(frame,{count=game.tick,name="signal-srctick",type="virtual"})
|
||||
game.write_file(TX_BUFFER_FILE, json:encode(frame).."\n", true)
|
||||
game.write_file(TX_BUFFER_FILE, json:encode(frame).."\n", true, global.write_file_player or 0)
|
||||
|
||||
-- Loopback for testing
|
||||
--AddFrameToRXBuffer(frame)
|
||||
@@ -409,8 +418,6 @@ function SetRXCombinators()
|
||||
if rxControl.valid then
|
||||
rxControl.parameters={parameters=frame}
|
||||
rxControl.enabled=true
|
||||
else
|
||||
table.remove(global.rxControls,i)
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -423,12 +430,36 @@ function ClearRXCombinators()
|
||||
for i,rxControl in pairs(global.rxControls) do
|
||||
if rxControl.valid then
|
||||
rxControl.enabled=false
|
||||
else
|
||||
table.remove(global.rxControls,i)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function UpdateInvCombinators()
|
||||
-- Update all inventory Combinators
|
||||
-- Prepare a frame from the last inventory report, plus any virtuals
|
||||
local invframe = {}
|
||||
if global.worldID then
|
||||
table.insert(invframe,{count=global.worldID,index=#invframe+1,signal={name="signal-localid",type="virtual"}})
|
||||
end
|
||||
|
||||
local items = game.item_prototypes
|
||||
if global.invdata then
|
||||
for name,count in pairs(global.invdata) do
|
||||
if items[name] then
|
||||
invframe[#invframe+1] = {count=count,index=#invframe+1,signal={name=name,type="item"}}
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
for i,invControl in pairs(global.invControls) do
|
||||
if invControl.valid then
|
||||
invControl.parameters={parameters=invframe}
|
||||
invControl.enabled=true
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
--[[ Remote Thing ]]--
|
||||
remote.add_interface("clusterio",
|
||||
{
|
||||
@@ -465,4 +496,17 @@ remote.add_interface("clusterio",
|
||||
end
|
||||
return buffer
|
||||
end,
|
||||
setFilePlayer = function(i)
|
||||
global.write_file_player = i
|
||||
end,
|
||||
receiveInventory = function(jsoninvdata)
|
||||
local invdata = json:decode(jsoninvdata)
|
||||
-- invdata = {["iron-plates"]=1234,["copper-plates"]=5678,...}
|
||||
global.invdata = invdata
|
||||
UpdateInvCombinators()
|
||||
end,
|
||||
setWorldID = function(newid)
|
||||
global.worldID = newid
|
||||
UpdateInvCombinators()
|
||||
end
|
||||
})
|
||||
@@ -161,20 +161,20 @@ data:extend{
|
||||
subgroup = "virtual-signal-clusterio",
|
||||
order = "e[clusterio]-[1srctick]"
|
||||
},
|
||||
--[[{
|
||||
{
|
||||
type = "virtual-signal",
|
||||
name = "signal-srcid",
|
||||
icon = "__base__/graphics/icons/signal/signal_grey.png",
|
||||
subgroup = "virtual-signal-clusterio",
|
||||
order = "e[clusterio]-[2srcid]"
|
||||
},]]
|
||||
--[[{
|
||||
},
|
||||
{
|
||||
type = "virtual-signal",
|
||||
name = "signal-localid",
|
||||
icon = "__base__/graphics/icons/signal/signal_grey.png",
|
||||
subgroup = "virtual-signal-clusterio",
|
||||
order = "e[clusterio]-[3localid]"
|
||||
},]]
|
||||
},
|
||||
}
|
||||
|
||||
-- TX Combinator
|
||||
@@ -239,3 +239,34 @@ data:extend{
|
||||
requester_paste_multiplier = 1
|
||||
},
|
||||
}
|
||||
-- Inventory Combinator
|
||||
local inv = table.deepcopy(data.raw["constant-combinator"]["constant-combinator"])
|
||||
inv.name = INV_COMBINATOR_NAME
|
||||
inv.minable.result = INV_COMBINATOR_NAME
|
||||
inv.item_slot_count = 100
|
||||
data:extend{
|
||||
inv,
|
||||
{
|
||||
type = "item",
|
||||
name = INV_COMBINATOR_NAME,
|
||||
icon = inv.icon,
|
||||
flags = {"goes-to-quickbar"},
|
||||
subgroup = "storage",
|
||||
place_result=INV_COMBINATOR_NAME,
|
||||
order = "a[items]-b["..INV_COMBINATOR_NAME.."]",
|
||||
stack_size = 50,
|
||||
},
|
||||
{
|
||||
type = "recipe",
|
||||
name = INV_COMBINATOR_NAME,
|
||||
enabled = true, -- TODO do this on a tech somewhere
|
||||
ingredients =
|
||||
{
|
||||
{"constant-combinator", 1},
|
||||
{"electronic-circuit", 3},
|
||||
{"advanced-circuit", 1}
|
||||
},
|
||||
result = INV_COMBINATOR_NAME,
|
||||
requester_paste_multiplier = 1
|
||||
},
|
||||
}
|
||||
File renamed without changes.
|
Before Width: | Height: | Size: 227 B After Width: | Height: | Size: 227 B |
@@ -2,7 +2,7 @@
|
||||
{
|
||||
"name": "clusterio",
|
||||
"title": "Clusterio",
|
||||
"version": "0.0.2",
|
||||
"version": "0.0.3",
|
||||
"date": "19-08-2016",
|
||||
"author": "Danielv123 & Keyboardhack & justarandomgeek",
|
||||
"dependencies": ["base >= 0.13"],
|
||||
File renamed without changes.
@@ -5,6 +5,7 @@ put-tank=Put tank
|
||||
get-tank=Get tank
|
||||
put-combinator=Transmit combinator
|
||||
get-combinator=Receive combinator
|
||||
inventory-combinator=Inventory combinator
|
||||
|
||||
[item-name]
|
||||
put-chest=Put chest
|
||||
@@ -13,3 +14,9 @@ put-tank=Put tank
|
||||
get-tank=Get tank
|
||||
put-combinator=Transmit combinator
|
||||
get-combinator=Receive combinator
|
||||
inventory-combinator=Inventory combinator
|
||||
|
||||
[virtual-signal-name]
|
||||
signal-srctick=Source World Tick
|
||||
signal-srcid=Source World ID
|
||||
signal-localid=Local World ID
|
||||
Reference in new issue
Block a user