From 2bcacb2513920154a0215b25a0248f46694d1432 Mon Sep 17 00:00:00 2001 From: Danielv123 Date: Mon, 20 Nov 2017 10:29:52 +0100 Subject: [PATCH] #61 extracting place data from game Still need to extract remove data in the LUA portion and store it --- .gitignore | 3 +- sharedPlugins/remoteMap/config.js | 17 +++++ sharedPlugins/remoteMap/control.lua | 108 ++++++++++++++++++++++++++++ sharedPlugins/remoteMap/index.js | 34 +++++++++ sharedPlugins/remoteMap/info.json | 11 +++ 5 files changed, 172 insertions(+), 1 deletion(-) create mode 100644 sharedPlugins/remoteMap/config.js create mode 100644 sharedPlugins/remoteMap/control.lua create mode 100644 sharedPlugins/remoteMap/index.js create mode 100644 sharedPlugins/remoteMap/info.json diff --git a/.gitignore b/.gitignore index 1a94620d..24ee0f36 100644 --- a/.gitignore +++ b/.gitignore @@ -39,4 +39,5 @@ package-lock.json !sharedPlugins/ sharedPlugins/* !sharedPlugins/inventoryImports/ -!sharedPlugins/UPSdisplay/ \ No newline at end of file +!sharedPlugins/UPSdisplay/ +!sharedPlugins/remoteMap/ \ No newline at end of file diff --git a/sharedPlugins/remoteMap/config.js b/sharedPlugins/remoteMap/config.js new file mode 100644 index 00000000..ce545550 --- /dev/null +++ b/sharedPlugins/remoteMap/config.js @@ -0,0 +1,17 @@ +/* + Config for clusterio remote mapping plugin. Will handle writing out new map data, aggregating it and sending it to the master (or something) for + display/interaction. +*/ +var modInfo = require("./info.json"); +module.exports = { + // Name of package. For display somewhere I guess. + name: modInfo.name, + version: modInfo.version, + // Binary entrypoint for plugin. Don't let it crash. Stdout is sent to game as server chat (run /c commands from here for interface) + // Make sure its cross platform somehow. + binary: "node", + arguments: ["index.js"], + description: modInfo.description, + // We'll send everything in this file to your stdin. Beware. + scriptOutputFileSubscription: "remoteMap.txt", +} \ No newline at end of file diff --git a/sharedPlugins/remoteMap/control.lua b/sharedPlugins/remoteMap/control.lua new file mode 100644 index 00000000..30574a6b --- /dev/null +++ b/sharedPlugins/remoteMap/control.lua @@ -0,0 +1,108 @@ +-- MIT License +-- +-- Copyright (c) 2017 Florian Jung +-- +-- Permission is hereby granted, free of charge, to any person obtaining a +-- copy of this factorio lua stub and associated +-- documentation files (the "Software"), to deal in the Software without +-- restriction, including without limitation the rights to use, copy, modify, +-- merge, publish, distribute, sublicense, and/or sell copies of the +-- Software, and to permit persons to whom the Software is furnished to do +-- so, subject to the following conditions: +-- +-- The above copyright notice and this permission notice shall be included in +-- all copies or substantial portions of the Software. +-- +-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +-- THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +-- FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +-- DEALINGS IN THE SOFTWARE. + +function write_file(data) + if must_write_initstuff then + must_write_initstuff = false + writeout_initial_stuff() + end + + game.write_file("remoteMap.txt", data, true) +end +function complain(text) + print(text) + game.forces["player"].print(text) +end + +local todo_next_tick = {} +function on_tick(event) + if #todo_next_tick > 0 then + print("on_tick executing "..#todo_next_tick.." stored callbacks") + for _,func in ipairs(todo_next_tick) do + func() + end + todo_next_tick = {} + end +end + + +function writeout_objects(surface, area) + --if my_client_id ~= 1 then return end + header = "objects "..area.left_top.x..","..area.left_top.y..";"..area.right_bottom.x..","..area.right_bottom.y..": " + line = '' + lines={} + for idx, ent in pairs(surface.find_entities(area)) do + if area.left_top.x <= ent.position.x and ent.position.x < area.right_bottom.x and area.left_top.y <= ent.position.y and ent.position.y < area.right_bottom.y then + if ent.prototype.collision_mask ~= nil and ent.prototype.collision_mask['player-layer'] then + line=line..","..ent.name.." "..ent.position.x.." "..ent.position.y + if idx % 100 == 0 then + table.insert(lines,line) + line='' + end + end + end + end + table.insert(lines,line) + write_file(header..table.concat(lines,"").."\n") + + line=nil +end + +function on_some_entity_created(event) + local ent = event.entity or event.created_entity or nil + if ent == nil then + complain("wtf, on_some_entity_created has nil entity") + return + end + + writeout_objects(ent.surface, {left_top={x=math.floor(ent.position.x), y=math.floor(ent.position.y)}, right_bottom={x=math.floor(ent.position.x)+1, y=math.floor(ent.position.y)+1}}) + + complain("on_some_entity_created: "..ent.name.." at "..ent.position.x..","..ent.position.y) +end +function on_some_entity_deleted(event) + local ent = event.entity + if ent == nil then + complain("wtf, on_some_entity_deleted has nil entity") + return + end + + -- we can't do this now, because the entity still exists at this point. instead, we schedule the writeout for the next tick + + local surface = ent.surface + local area = {left_top={x=math.floor(ent.position.x), y=math.floor(ent.position.y)}, right_bottom={x=math.floor(ent.position.x)+1, y=math.floor(ent.position.y)+1}} + + table.insert(todo_next_tick, function () writeout_objects(surface, area ) end) + + complain("on_some_entity_deleted: "..ent.name.." at "..ent.position.x..","..ent.position.y) +end + +script.on_event(defines.events.on_tick, on_tick) + +script.on_event(defines.events.on_biter_base_built, on_some_entity_created) --entity +script.on_event(defines.events.on_built_entity, on_some_entity_created) --created_entity +script.on_event(defines.events.on_robot_built_entity, on_some_entity_created) --created_entity + +script.on_event(defines.events.on_entity_died, on_some_entity_deleted) --entity +script.on_event(defines.events.on_player_mined_entity, on_some_entity_deleted) --entity +script.on_event(defines.events.on_robot_mined_entity, on_some_entity_deleted) --entity +script.on_event(defines.events.on_resource_depleted, on_some_entity_deleted) --entity diff --git a/sharedPlugins/remoteMap/index.js b/sharedPlugins/remoteMap/index.js new file mode 100644 index 00000000..de920290 --- /dev/null +++ b/sharedPlugins/remoteMap/index.js @@ -0,0 +1,34 @@ +const needle = require("needle"); +const moduleConfig = require("./config"); +console.log("/silent-command game.print('"+moduleConfig.name+" version "+moduleConfig.version+" enabled')"); + +/*setInterval(function(){ + console.log("/silent-command game.write_file('"+moduleConfig.scriptOutputFileSubscription+"', game.tick, true, 0)"); +},1000);*/ +var config = {}; +process.stdin.setEncoding('utf8'); +process.stdin.on('readable', () => { + var chunk = process.stdin.read(); + if (chunk !== null && !isNaN(chunk) && !chunk.includes("objects")){ + console.log(chunk); + // objects -29,-35;-28,-34: ,inserter -28.5 -34.5 + + let data = chunk.split(","); + data.shift();data.shift();data.shift(); + if(data && data[0] == undefined){ + // this position is now empty, delete whatever was there from DB + } + data.forEach(entity => { + let name = entity.split(" ")[0]; + let xPos = entity.split(" ")[1]; + let yPos = entity.split(" ")[2]; + }); + + } else { + // config is the same config as client.js and master.js uses + config = JSON.parse(chunk); + } +}); +process.stdin.on('end', () => { + process.stdout.write('end'); +}); diff --git a/sharedPlugins/remoteMap/info.json b/sharedPlugins/remoteMap/info.json new file mode 100644 index 00000000..04d30151 --- /dev/null +++ b/sharedPlugins/remoteMap/info.json @@ -0,0 +1,11 @@ + +{ +"name": "remoteMap", +"title": "Clusterio remote map", +"version": "1.0.0", +"date": "18-11-2017", +"author": "Danielv123", +"dependencies": ["base >= 0.15"], +"factorio_version": "0.15", +"description": "Clusterio plugin for external mapping and possibly building?" +}