Merged researchSync

This commit is contained in:
Danielv123 committed 2018-04-11 15:20:15 +02:00
commit 3af343c5a0
31 files changed
+2152 -393

No files matched your search

+1 -1
View File
@@ -12,7 +12,7 @@ describe("remoteMap/chunkStore.js", () => {
it("is a constructor", ()=>{
assert.equal(typeof chunkStore, "function");
// create a new store and elevate it
store = new chunkStore("910239"/* instanceID, use defaults for the rest */);
store = new chunkStore("910239"/* instanceID, use defaults for the rest */, undefined, "./database/unitTests/chunkStore/");
assert.equal(store.name, "910239");
});
it("allows you to store the locations of entities", done => {
+55 -16
View File
@@ -22,22 +22,33 @@
-- 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)
stringsToWriteNextTick = stringsToWriteNextTick .. data .. '\n'
-- game.write_file("remoteMap.txt", data, true)
end
function dump_cached_writes()
game.write_file("remoteMap.txt", stringsToWriteNextTick, true)
end
function complain(text)
print(text)
game.forces["player"].print(text)
if false then -- only print during development
print(text)
game.forces["player"].print(text)
end
end
local todo_next_tick = {}
must_write_initstuff = true
function on_tick(event)
dump_cached_writes()
stringsToWriteNextTick = ''
if must_write_initstuff then
must_write_initstuff = false
log("I must write init stuff!")
writeout_initial_stuff()
end
if #todo_next_tick > 0 then
print("on_tick executing "..#todo_next_tick.." stored callbacks")
complain("on_tick executing "..#todo_next_tick.." stored callbacks")
for _,func in ipairs(todo_next_tick) do
func()
end
@@ -45,26 +56,50 @@ function on_tick(event)
end
end
function writeout_initial_stuff()
log("Writing out initial stuff...")
writeout_objects(game.surfaces['nauvis'], {left_top={x=-64,y=-64},right_bottom={x=64,y=64}})
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 = ''
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.."\n"
-- line = line .. ent.name.." "..ent.position.x.." "..ent.position.y.."\n"
-- local line = ent.name..","..ent.position.x..","..ent.position.y
-- if ent.supports_direction then
-- line = line .. ",rot="..ent.direction
-- end
-- write_file(line)
on_some_entity_created(nil, ent)
end
end
end
write_file(line)
--write_file(header..table.concat(lines,"").."\n")
--write_file(table.concat(lines,"").."\n")
line=nil
end
function on_chunk_generated(event)
local area = event.area
local surface = event.surface
--print("chunk generated at ("..area.left_top.x..","..area.left_top.y..") -- ("..area.right_bottom.x..","..area.right_bottom.y..")")
function on_some_entity_created(event)
local ent = event.entity or event.created_entity or nil
if surface ~= game.surfaces['nauvis'] then -- we only support one surface
return
end
-- writeout_resources(surface, area)
writeout_objects(surface, area)
-- writeout_tiles(surface, area)
end
function on_some_entity_created(event, entp)
local ent
if entp then
ent = entp
else
ent = event.entity or event.created_entity or nil
end
if ent == nil then
complain("wtf, on_some_entity_created has nil entity")
return
@@ -73,6 +108,7 @@ function on_some_entity_created(event)
if ent.supports_direction then
entityData = entityData .. ",rot="..ent.direction
end
-- log(entityData)
write_file(entityData)
--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)
@@ -95,12 +131,15 @@ function on_some_entity_deleted(event)
end
script.on_event(defines.events.on_tick, on_tick)
script.on_event(defines.events.on_chunk_generated, on_chunk_generated)
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_player_rotated_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
@@ -0,0 +1,5 @@
/c
local toDelete = game.surfaces[1].find_entities({{3,7},{4,8}})
for i, entity in pairs(toDelete) do
entity.die()
end
+46 -10
View File
@@ -13,8 +13,8 @@ class remoteMap {
// initialize chunk database
this.chunkMap = new chunkStore(this.config.unique, 64, "./database/chunkStore/");
this.chunkMap.onEntityChange(entity => {
console.log("Entity changed: ")
console.log(entity);
// this.messageInterface("Entity changed: ")
// this.messageInterface(entity);
// check valid coordinates (dunno what could go wrong, but can never be too sure)
if(entity && entity.x !== undefined && !isNaN(Number(entity.x)) && entity.y !== undefined && !isNaN(Number(entity.y))){
this.socket.emit("sendEntity", entity);
@@ -31,7 +31,7 @@ class remoteMap {
},10000);
});
this.socket.on("getChunk", req => {
console.log("getChunk is not supported!");
this.messageInterface("getChunk is not supported!");
/*this.chunkMap.getChunk(req.x, req.y).then(chunk => {
chunk.requesterID = req.requesterID;
this.socket.emit("sendChunk", chunk);
@@ -51,17 +51,54 @@ class remoteMap {
this.messageInterface("socket.getEntity triggered called with invalid parameters");
}
});
// both placing and deleting entities via the web interface
this.socket.on("placeEntity", req => {
// game.player.surface.create_entity{name="small-ship-wreck", position={game.player.position.x-2, game.player.position.y+1}, direction=..., force=...}
this.messageInterface("placing "+JSON.stringify(req));
if(req.name && typeof req.name == "string" && req.position && !isNaN(Number(req.position.x)) && !isNaN(Number(req.position.y))){
if(req.name == "deleted"){ // {{'+req.position.x+','+req.position.y+'},{'+req.position.x+','+req.position.y+'}}
// delete entity from game world using LUA command
this.messageInterface('/silent-command local toDelete = game.surfaces[1].find_entities({{'+req.position.x+','+req.position.y+'},{'+(req.position.x+1)+','+(req.position.y+1)+'}}) for i, entity in pairs(toDelete) do entity.die() end');
// this position is now empty, delete whatever was there from DB
this.deleteEntity(req.position.x, req.position.y);
} else {
let command = '/silent-command game.surfaces[1].create_entity{name="'+req.name+'", position={'+Number(req.position.x)+', '+Number(req.position.y)+'}, force="neutral"'
if(req.direction) command += ', direction='+req.direction;
command += '}';
this.messageInterface(command); // execute command
// add entity to the chunkStore database because create_entity does not trigger any events
let name = req.name;
let entity = {name};
if(typeof req.direction == "string"){
entity.rot = req.direction;
}
this.addEntity(req.position.x, req.position.y, entity);
}
}
});
this.messageInterface("/silent-command game.print('"+moduleConfig.name+" version "+moduleConfig.version+" enabled')");
}
addEntity(x,y,entity){ // add entity to database
this.chunkMap.setEntity(Number(x), Number(y), entity).then(data => {
this.messageInterface("Added "+entity.name+" to data "+Number(x)+", "+Number(y));
}).catch((err)=>this.messageInterface(err));
}
deleteEntity(x,y){ // remove entity from database
// this position is now empty, delete whatever was there from DB
this.chunkMap.setEntity(x, y, "delete this entity").then(() => {
this.messageInterface("Deleted entity at "+x+", "+y);
}).catch((err)=>this.messageInterface(err));
}
scriptOutput(data){
if(data){
this.messageInterface(data);
// express-transport-belt,-35.5,-14.5,[...]
data = data.split(",");
if(data && data[0] == undefined){
console.log("empty: ");
console.log(data);
this.messageInterface("empty: ");
this.messageInterface(data);
return
}
let name = data[0];
@@ -70,9 +107,7 @@ class remoteMap {
if(xPos && yPos && name){
if(name == "deleted"){
// this position is now empty, delete whatever was there from DB
this.chunkMap.setEntity(xPos, yPos, "delete this entity").then(() => {
this.messageInterface("Deleted data "+xPos+", "+yPos);
}).catch((err)=>this.messageInterface(err));
this.deleteEntity(xPos, yPos);
} else {
let entity = {name};
for(let i = 3; i < data.length; i++){
@@ -82,9 +117,10 @@ class remoteMap {
}
}
// save this entity in the database
this.chunkMap.setEntity(xPos, yPos, entity).then(data => {
this.addEntity(xPos, yPos, entity);
/* this.chunkMap.setEntity(xPos, yPos, entity).then(data => {
this.messageInterface("Added "+name+" to data "+xPos+", "+yPos);
}).catch((err)=>this.messageInterface(err));
}).catch((err)=>this.messageInterface(err)); */
}
}
+2 -2
View File
@@ -2,10 +2,10 @@
{
"name": "remoteMap",
"title": "Clusterio remote map",
"version": "2.0.0",
"version": "2.2.0",
"date": "18-11-2017",
"author": "Danielv123",
"dependencies": ["base >= 0.15"],
"factorio_version": "0.16",
"description": "Clusterio plugin for external mapping and possibly building?"
"description": "Clusterio plugin for mapping and building through the browser. No more need to start game, download and catching up for every game, also mobile factorio confirmed?"
}