Skip to content

Commit

Permalink
pulling master to continue coding (#9)
Browse files Browse the repository at this point in the history
* Fix on build/node_modules folder

* Added neighbour detection

Behaviour not implemented yet

* neighbours get added/merging to the correct builders

* Finished implementing neighbours behaviour when placing a new assembler
  • Loading branch information
voske123 committed Jul 9, 2018
1 parent d1a6f40 commit 489bade
Show file tree
Hide file tree
Showing 3 changed files with 261 additions and 19 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ pids
build/Release

# Dependency directories
node_modules/
*/node_modules/

# Typescript v1 declaration files
typings/
Expand Down
40 changes: 40 additions & 0 deletions lib/table.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
-- Make sure this part of the library exists
if not lib then lib = {} end
if not lib.table then lib.table = {} end



function lib.table.hasValue(table, value)
for _,val in pairs(table) do
if value == val then
return true
end
end

return false
end



function lib.table.isEmpty(t)
return not next(t)
end



function lib.table.areEqual(t1, t2)
if type(t1) ~= 'table' or type(t2) ~= 'table' then
return t1 == t2
end
for k,v in pairs(t1) do
if not tablesAreEqual(v, t2[k]) then
return false
end
end
for k,v in pairs(t2) do
if not tablesAreEqual(v, t1[k]) then
return false
end
end
return true
end
238 changes: 220 additions & 18 deletions src/trainassembly.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require 'util'
require "lib.directions"
require "lib.table"

-- Create class
Trainassembly = {}
Expand All @@ -22,13 +23,17 @@ function Trainassembly:initGlobalData()
["version"] = 1, -- version of the global data
["prototypeData"] = self:initPrototypeData(), -- data storing info about the prototypes

["trainBuilders"] = {}, -- keep track of all builders containing builderEntities
["trainAssemblers"] = {}, -- keep track of all assembling machines

["trainBuilders"] = {}, -- keep track of all builders that contain one or more trainAssemblers
["nextTrainBuilderIndex"] = 1,
}

return util.table.deepcopy(TA_data)
end



-- Initialisation of the prototye data inside the global data
function Trainassembly:initPrototypeData()
return
Expand Down Expand Up @@ -57,44 +62,239 @@ function Trainassembly:saveNewStructure(machineEntity)
if not (machineEntity and machineEntity.valid) then
return nil
end
local machineSurface = machineEntity.surface
local machinePosition = machineEntity.position

-- STEP 2: Make sure we can index it, meaning, check if the table already
-- STEP 2: Save the assembler in the trainAssemblers datastructure
-- STEP 2a:Make sure we can index it, meaning, check if the table already
-- excists for the surface, if not, we make one. Afther that we also
-- have to check if the surface table has a table we can index for
-- the y-position, if not, we make one.
if not global.TA_data["trainBuilders"][machineEntity.surface.index] then
global.TA_data["trainBuilders"][machineEntity.surface.index] = {}
if not global.TA_data["trainAssemblers"][machineSurface.index] then
global.TA_data["trainAssemblers"][machineSurface.index] = {}
end
if not global.TA_data["trainBuilders"][machineEntity.surface.index][machineEntity.position.y] then
global.TA_data["trainBuilders"][machineEntity.surface.index][machineEntity.position.y] = {}
if not global.TA_data["trainAssemblers"][machineSurface.index][machinePosition.y] then
global.TA_data["trainAssemblers"][machineSurface.index][machinePosition.y] = {}
end

-- STEP 3: Now we know we can index (without crashing) to the position as:
-- STEP 2b:Now we know we can index (without crashing) to the position as:
-- dataStructure[surfaceIndex][positionY][positionX]
-- Now we can store our wanted data at this position
global.TA_data["trainBuilders"][machineEntity.surface.index][machineEntity.position.y][machineEntity.position.x] =
global.TA_data["trainAssemblers"][machineSurface.index][machinePosition.y][machinePosition.x] =
{
["entity"] = machineEntity,
["direction"] = machineEntity.direction,
["entity"] = machineEntity, -- the entity
["direction"] = machineEntity.direction, -- the direction its facing
["trainBuilderIndex"] = nil, -- the trainBuilder it belongs to (see further down)
}

-- STEP 3: Check if this assembler is linked to another assemblers to make
-- single but bigger trains
-- STEP 3a:Check for entities around this one. We know we only have to look
-- in the same direction as its facing, as that is the directon the
-- train will be build in
local trainAssemblerNW, trainAssemblerSE
if machineEntity.direction == defines.direction.north or machineEntity.direction == defines.direction.south then
-- machine is placed vertical, look vertical (y-axis)
-- north
trainAssemblerNW = machineSurface.find_entities_filtered{
name = machineEntity.name,
type = machineEntity.type,
force = machineEntity.force,
position = { x = machinePosition.x, y = machinePosition.y - 7 },
limit = 1,
}
-- south
trainAssemblerSE = machineSurface.find_entities_filtered{
name = machineEntity.name,
type = machineEntity.type,
force = machineEntity.force,
position = { x = machinePosition.x, y = machinePosition.y + 7 },
limit = 1,
}
else
-- machine is placed horizontal, look horizontal (x-axis)
-- west
trainAssemblerNW = machineSurface.find_entities_filtered{
name = machineEntity.name,
type = machineEntity.type,
force = machineEntity.force,
position = { x = machinePosition.x - 7, y = machinePosition.y },
limit = 1,
}
-- east
trainAssemblerSE = machineSurface.find_entities_filtered{
name = machineEntity.name,
type = machineEntity.type,
force = machineEntity.force,
position = { x = machinePosition.x + 7, y = machinePosition.y },
limit = 1,
}
end

-- find_entities_filtered returns a list, we want only the entity,
-- so we get it out of the table. Also make sure it is valid
if not lib.table.isEmpty(trainAssemblerNW) then
trainAssemblerNW = trainAssemblerNW[1]
if not trainAssemblerNW.valid then
trainAssemblerNW = nil
end
else
trainAssemblerNW = nil
end
if not lib.table.isEmpty(trainAssemblerSE) then
trainAssemblerSE = trainAssemblerSE[1]
if not trainAssemblerSE.valid then
trainAssemblerSE = nil
end
else
trainAssemblerSE = nil
end

-- STEP 3b:We found some entities now (maybe), but we still have to check if
-- they are validly placed. If they aren't valid, we discard them too
-- Validly placed item: - has same or oposite direction
if trainAssemblerNW and trainAssemblerNW.valid then
-- Check if its facing the same or oposite direction, if not, discard.
if not (trainAssemblerNW.direction == machineEntity.direction
or trainAssemblerNW.direction == lib.directions.oposite(machineEntity.direction) ) then
trainAssemblerNW = nil
end
end
if trainAssemblerSE and trainAssemblerSE.valid then
-- Check if its facing the same or oposite direction, if not, discard.
if not (trainAssemblerSE.direction == machineEntity.direction
or trainAssemblerSE.direction == lib.directions.oposite(machineEntity.direction) ) then
trainAssemblerSE = nil
end
end

-- STEP 3c:We found valid entities (maybe), either way, we have to add this
-- assembling machine to a trainBuilder
if (not trainAssemblerNW) and (not trainAssemblerSE) then
-- OPTION 3c.1: there is no neighbour detected, we create a new one
local trainBuiderIndex = global.TA_data["nextTrainBuilderIndex"]

-- add reference to the trainassembly
global.TA_data["trainAssemblers"][machineSurface.index][machinePosition.y][machinePosition.x]["trainBuilderIndex"] = trainBuiderIndex

-- and add the new trainBuilder with a single trainAssembler reference
global.TA_data["trainBuilders"][trainBuiderIndex] =
{
{
["surfaceIndex"] = machineSurface.index,
["position"] = { x = machinePosition.x, y = machinePosition.y },
},
}

-- new trainbuilder added, now increment the nextIndex
global.TA_data["nextTrainBuilderIndex"] = trainBuiderIndex + 1

else -- there is one or more neighbours
if (trainAssemblerNW and (not trainAssemblerSE)) then
-- OPTION 3c.2a: There is only one neighbour detected.
-- Only the northwest one was detected, we add it to his
-- trainbuilder.
local trainBuiderIndex = global.TA_data["trainAssemblers"][trainAssemblerNW.surface.index][trainAssemblerNW.position.y][trainAssemblerNW.position.x]["trainBuilderIndex"]

-- add reference to the trainBuilder in the trainassembly
global.TA_data["trainAssemblers"][machineSurface.index][machinePosition.y][machinePosition.x]["trainBuilderIndex"] = trainBuiderIndex

-- and add this trainAssembler reference to the existing trainBuilder
table.insert(global.TA_data["trainBuilders"][trainBuiderIndex], {
["surfaceIndex"] = machineSurface.index,
["position"] = { x = machinePosition.x, y = machinePosition.y },
})

elseif (trainAssemblerSE and (not trainAssemblerNW)) then
-- OPTION 3c.2b: There is only one neighbour detected.
-- Only the southeast one was detected, we add it to his
-- trainbuilder.
local trainBuiderIndex = global.TA_data["trainAssemblers"][trainAssemblerSE.surface.index][trainAssemblerSE.position.y][trainAssemblerSE.position.x]["trainBuilderIndex"]

-- add reference to the trainBuilder in the trainassembly
global.TA_data["trainAssemblers"][machineSurface.index][machinePosition.y][machinePosition.x]["trainBuilderIndex"] = trainBuiderIndex

-- and add this trainAssembler reference to the existing trainBuilder
table.insert(global.TA_data["trainBuilders"][trainBuiderIndex], {
["surfaceIndex"] = machineSurface.index,
["position"] = { x = machinePosition.x, y = machinePosition.y },
})

else
-- OPTION 3c.3: Both neighbours are detected
-- First we need to merge the two existing trainBuilders
-- together. Let's merge the SE one inside the NW one
local trainBuiderIndexNW = global.TA_data["trainAssemblers"][trainAssemblerNW.surface.index][trainAssemblerNW.position.y][trainAssemblerNW.position.x]["trainBuilderIndex"]
local trainBuiderIndexSE = global.TA_data["trainAssemblers"][trainAssemblerSE.surface.index][trainAssemblerSE.position.y][trainAssemblerSE.position.x]["trainBuilderIndex"]

for trainAssemblerIndex, trainAssemblerRef in pairs(global.TA_data["trainBuilders"][trainBuiderIndexSE]) do
-- Move the reference into the other trainBuilder
table.insert(global.TA_data["trainBuilders"][trainBuiderIndexNW], util.table.deepcopy(trainAssemblerRef))
end

-- Now all the assemblers of the SE one are in the NW one, we can now delete
-- the whole SE trainbuilder. If we delete it, we have a 'hole' in our list
-- to fix that, we move the last one in this spot * fixed XD *. But if this
-- is already the last one, we don't have this issue. And when we move the're
-- is a possibility we moved the NW over. We've also deleted a trainbuiler,
-- so we'll have to update our nextIndex - 1.
local lastIndex = global.TA_data["nextTrainBuilderIndex"] - 1

-- check if its the last one, if not the last one we fill the hole of the SE one
if (trainBuiderIndexSE ~= lastIndex) then
-- copy the last one over to the hole and adapt all the references to the
-- trianBuilder of all the trainAssemblers we moved
global.TA_data["trainBuilders"][trainBuiderIndexSE] = util.table.deepcopy(global.TA_data["trainBuilders"][lastIndex])
for _, trainAssemblerRef in pairs(global.TA_data["trainBuilders"][trainBuiderIndexSE]) do
global.TA_data["trainAssemblers"][trainAssemblerRef.surfaceIndex][trainAssemblerRef.position.y][trainAssemblerRef.position.x]["trainBuilderIndex"] = trainBuiderIndexSE
end

-- it could be the other one we moved
if trainBuiderIndexNW == lastIndex then
trainBuiderIndexNW = trainBuiderIndexSE
end
end

-- delete the reference of the last index
global.TA_data["trainBuilders"][lastIndex] = nil
global.TA_data["nextTrainBuilderIndex"] = lastIndex

-- and add this trainAssembler reference to that existing trainBuilder
table.insert(global.TA_data["trainBuilders"][trainBuiderIndexNW], {
["surfaceIndex"] = machineSurface.index,
["position"] = { x = machinePosition.x, y = machinePosition.y },
})

-- now we finaly merged them both together and added the new assembler, now
-- we can start updating the reference in the trainassembly to the trainbuilders
--global.TA_data["trainAssemblers"][machineSurface.index][machinePosition.y][machinePosition.x]["trainBuilderIndex"] = trainBuiderIndexNW
for _, trainAssemblerRef in pairs(global.TA_data["trainBuilders"][trainBuiderIndexNW]) do
global.TA_data["trainAssemblers"][trainAssemblerRef.surfaceIndex][trainAssemblerRef.position.y][trainAssemblerRef.position.x]["trainBuilderIndex"] = trainBuiderIndexNW
end
end
end
end



function Trainassembly:updateMachineDirection(machineEntity)

if not (machineEntity and machineEntity.valid) then
return nil
end
if not global.TA_data["trainBuilders"][machineEntity.surface.index] then
local machineSurface = machineEntity.surface
if not global.TA_data["trainAssemblers"][machineSurface.index] then
return nil
end
if not global.TA_data["trainBuilders"][machineEntity.surface.index][machineEntity.position.y] then
local machinePosition = machineEntity.position
if not global.TA_data["trainAssemblers"][machineSurface.index][machinePosition.y] then
return nil
end
if not global.TA_data["trainBuilders"][machineEntity.surface.index][machineEntity.position.y][machineEntity.position.x] then
if not global.TA_data["trainAssemblers"][machineSurface.index][machinePosition.y][machinePosition.x] then
return nil
end

global.TA_data["trainBuilders"][machineEntity.surface.index][machineEntity.position.y][machineEntity.position.x]["direction"] = machineEntity.direction
global.TA_data["trainAssemblers"][machineSurface.index][machinePosition.y][machinePosition.x]["direction"] = machineEntity.direction
end

--------------------------------------------------------------------------------
Expand All @@ -121,19 +321,21 @@ function Trainassembly:getMachineDirection(machineEntity)
-- STEP 2: If we don't have a trainBuilder saved on that surface, or not
-- on that y position or on that x position, it means that we don't
-- have a direction available for that machine.
if not global.TA_data["trainBuilders"][machineEntity.surface.index] then
local machineSurface = machineEntity.surface
if not global.TA_data["trainAssemblers"][machineSurface.index] then
return nil
end
if not global.TA_data["trainBuilders"][machineEntity.surface.index][machineEntity.position.y] then
local machinePosition = machineEntity.position
if not global.TA_data["trainAssemblers"][machineSurface.index][machinePosition.y] then
return nil
end
if not global.TA_data["trainBuilders"][machineEntity.surface.index][machineEntity.position.y][machineEntity.position.x] then
if not global.TA_data["trainAssemblers"][machineSurface.index][machinePosition.y][machinePosition.x] then
return nil
end

-- STEP 3: In step 2 we checked for an invalid data structure. So now we
-- can return the direction the machine is/was facing.
return global.TA_data["trainBuilders"][machineEntity.surface.index][machineEntity.position.y][machineEntity.position.x]["direction"]
return global.TA_data["trainAssemblers"][machineSurface.index][machinePosition.y][machinePosition.x]["direction"]
end


Expand Down

0 comments on commit 489bade

Please sign in to comment.