2016-08-05 06:47:51 +02:00
|
|
|
local mapProcessor = {}
|
|
|
|
|
2016-08-20 04:52:27 +02:00
|
|
|
-- imports
|
|
|
|
|
2017-06-11 02:59:06 +02:00
|
|
|
local unitGroupUtils = require("UnitGroupUtils")
|
2016-08-21 23:48:55 +02:00
|
|
|
local pheromoneUtils = require("PheromoneUtils")
|
2017-06-13 05:16:43 +02:00
|
|
|
local aiAttackWave = require("AIAttackWave")
|
2017-05-19 09:47:24 +02:00
|
|
|
local aiPredicates = require("AIPredicates")
|
2016-08-29 02:05:28 +02:00
|
|
|
local constants = require("Constants")
|
2016-10-15 02:00:18 +02:00
|
|
|
local mapUtils = require("MapUtils")
|
2017-05-19 09:47:24 +02:00
|
|
|
local playerUtils = require("PlayerUtils")
|
2017-11-21 09:27:03 +02:00
|
|
|
local chunkUtils = require("ChunkUtils")
|
2018-01-15 09:41:55 +02:00
|
|
|
local chunkPropertyUtils = require("ChunkPropertyUtils")
|
2018-01-21 09:42:47 +02:00
|
|
|
local baseUtils = require("BaseUtils")
|
2017-06-16 03:30:26 +02:00
|
|
|
|
2016-09-14 13:16:33 +02:00
|
|
|
-- constants
|
|
|
|
|
|
|
|
local PROCESS_QUEUE_SIZE = constants.PROCESS_QUEUE_SIZE
|
2016-11-04 09:26:19 +02:00
|
|
|
|
2016-09-14 13:16:33 +02:00
|
|
|
local SCAN_QUEUE_SIZE = constants.SCAN_QUEUE_SIZE
|
|
|
|
|
|
|
|
local CHUNK_SIZE = constants.CHUNK_SIZE
|
2017-06-01 03:46:53 +02:00
|
|
|
local TRIPLE_CHUNK_SIZE = constants.TRIPLE_CHUNK_SIZE
|
2016-10-15 02:00:18 +02:00
|
|
|
|
|
|
|
local PROCESS_PLAYER_BOUND = constants.PROCESS_PLAYER_BOUND
|
|
|
|
local CHUNK_TICK = constants.CHUNK_TICK
|
|
|
|
|
2017-11-21 09:27:03 +02:00
|
|
|
local SENTINEL_IMPASSABLE_CHUNK = constants.SENTINEL_IMPASSABLE_CHUNK
|
2017-05-12 06:50:06 +02:00
|
|
|
|
2016-11-04 09:26:19 +02:00
|
|
|
local AI_SQUAD_COST = constants.AI_SQUAD_COST
|
|
|
|
local AI_VENGENCE_SQUAD_COST = constants.AI_VENGENCE_SQUAD_COST
|
2018-02-13 09:10:17 +02:00
|
|
|
local AI_SETTLER_COST = constants.AI_SETTLER_COST
|
2016-11-04 09:26:19 +02:00
|
|
|
|
|
|
|
local MOVEMENT_PHEROMONE = constants.MOVEMENT_PHEROMONE
|
|
|
|
|
2018-02-12 05:21:28 +02:00
|
|
|
local INTERVAL_RALLY = constants.INTERVAL_RALLY
|
|
|
|
local INTERVAL_RETREAT = constants.INTERVAL_RETREAT
|
|
|
|
local INTERVAL_SPAWNER = constants.INTERVAL_SPAWNER
|
|
|
|
|
2018-01-20 09:15:13 +02:00
|
|
|
local BASE_PROCESS_INTERVAL = constants.BASE_PROCESS_INTERVAL
|
|
|
|
|
2016-08-20 04:52:27 +02:00
|
|
|
-- imported functions
|
|
|
|
|
2016-08-21 23:48:55 +02:00
|
|
|
local scents = pheromoneUtils.scents
|
|
|
|
local processPheromone = pheromoneUtils.processPheromone
|
2018-10-20 07:17:37 +02:00
|
|
|
local commitPheromone = pheromoneUtils.commitPheromone
|
2017-11-21 09:27:03 +02:00
|
|
|
local playerScent = pheromoneUtils.playerScent
|
2016-08-20 04:52:27 +02:00
|
|
|
|
2017-06-13 05:16:43 +02:00
|
|
|
local formSquads = aiAttackWave.formSquads
|
2018-02-13 09:10:17 +02:00
|
|
|
local formSettlers = aiAttackWave.formSettlers
|
2016-08-20 04:52:27 +02:00
|
|
|
|
2016-10-15 02:00:18 +02:00
|
|
|
local getChunkByPosition = mapUtils.getChunkByPosition
|
2017-12-29 07:38:10 +02:00
|
|
|
local getChunkByXY = mapUtils.getChunkByXY
|
2016-10-15 02:00:18 +02:00
|
|
|
|
2017-06-11 02:59:06 +02:00
|
|
|
local recycleBiters = unitGroupUtils.recycleBiters
|
|
|
|
|
2017-11-21 09:27:03 +02:00
|
|
|
local validPlayer = playerUtils.validPlayer
|
|
|
|
|
2017-12-29 07:38:10 +02:00
|
|
|
local analyzeChunk = chunkUtils.analyzeChunk
|
|
|
|
|
2018-01-15 09:41:55 +02:00
|
|
|
local getNestCount = chunkPropertyUtils.getNestCount
|
|
|
|
local getEnemyStructureCount = chunkPropertyUtils.getEnemyStructureCount
|
2016-08-20 04:52:27 +02:00
|
|
|
|
2017-05-19 09:47:24 +02:00
|
|
|
local canAttack = aiPredicates.canAttack
|
2018-02-14 10:28:42 +02:00
|
|
|
local canMigrate = aiPredicates.canMigrate
|
2017-05-14 00:32:16 +02:00
|
|
|
|
2018-02-01 06:01:28 +02:00
|
|
|
local findNearbySquad = unitGroupUtils.findNearbySquad
|
2017-05-28 06:50:37 +02:00
|
|
|
|
2018-01-21 09:42:47 +02:00
|
|
|
local processBase = baseUtils.processBase
|
|
|
|
|
2016-08-29 02:05:28 +02:00
|
|
|
local mMin = math.min
|
|
|
|
|
2017-07-01 06:36:23 +02:00
|
|
|
local mRandom = math.random
|
|
|
|
|
2016-08-20 04:52:27 +02:00
|
|
|
-- module code
|
|
|
|
|
2016-10-15 02:00:18 +02:00
|
|
|
local function nonRepeatingRandom(players)
|
|
|
|
local ordering = {}
|
|
|
|
for _,player in pairs(players) do
|
|
|
|
ordering[#ordering+1] = player.index
|
|
|
|
end
|
|
|
|
for i=#ordering,1,-1 do
|
2017-07-01 06:36:23 +02:00
|
|
|
local s = mRandom(i)
|
2016-10-15 02:00:18 +02:00
|
|
|
local t = ordering[i]
|
|
|
|
ordering[i] = ordering[s]
|
|
|
|
ordering[s] = t
|
|
|
|
end
|
|
|
|
return ordering
|
|
|
|
end
|
|
|
|
|
|
|
|
--[[
|
|
|
|
processing is not consistant as it depends on the number of chunks that have been generated
|
|
|
|
so if we process 400 chunks an iteration and 200 chunks have been generated than these are
|
|
|
|
processed 3 times a second and 1200 generated chunks would be processed once a second
|
|
|
|
In theory, this might be fine as smaller bases have less surface to attack and need to have
|
|
|
|
pheromone dissipate at a faster rate.
|
|
|
|
--]]
|
2018-01-21 09:42:47 +02:00
|
|
|
function mapProcessor.processMap(map, surface, natives, tick, evolutionFactor)
|
2018-01-14 07:48:21 +02:00
|
|
|
local roll = map.processRoll
|
|
|
|
local index = map.processIndex
|
2018-01-20 09:15:13 +02:00
|
|
|
|
|
|
|
local chunkToBase = map.chunkToBase
|
2016-08-25 01:30:45 +02:00
|
|
|
|
2016-08-29 02:05:28 +02:00
|
|
|
if (index == 1) then
|
2017-07-01 06:36:23 +02:00
|
|
|
roll = mRandom()
|
2018-01-14 07:48:21 +02:00
|
|
|
map.processRoll = roll
|
2016-08-26 00:20:06 +02:00
|
|
|
end
|
2018-01-26 09:55:09 +02:00
|
|
|
|
|
|
|
local newEnemies = natives.newEnemies
|
2018-10-20 07:17:37 +02:00
|
|
|
local scentStaging = map.scentStaging
|
2017-05-14 00:32:16 +02:00
|
|
|
|
2017-06-01 03:46:53 +02:00
|
|
|
local squads = canAttack(natives, surface) and (0.11 <= roll) and (roll <= 0.35) and (natives.points >= AI_SQUAD_COST)
|
2018-02-14 10:28:42 +02:00
|
|
|
local settlers = canMigrate(natives, surface) and (0.90 <= roll) and (natives.points >= AI_SETTLER_COST)
|
|
|
|
|
2018-01-14 07:48:21 +02:00
|
|
|
local processQueue = map.processQueue
|
2016-09-14 13:16:33 +02:00
|
|
|
local endIndex = mMin(index + PROCESS_QUEUE_SIZE, #processQueue)
|
2018-10-20 07:17:37 +02:00
|
|
|
local i = 1
|
2016-08-29 02:05:28 +02:00
|
|
|
for x=index,endIndex do
|
2018-10-20 07:17:37 +02:00
|
|
|
local chunk = processQueue[x]
|
2017-04-22 01:14:04 +02:00
|
|
|
|
2017-06-01 03:46:53 +02:00
|
|
|
if (chunk[CHUNK_TICK] ~= tick) then
|
2018-10-20 07:17:37 +02:00
|
|
|
processPheromone(map, chunk, scentStaging[i])
|
2017-04-22 01:14:04 +02:00
|
|
|
|
2018-02-13 09:10:17 +02:00
|
|
|
if (getNestCount(map, chunk) > 0) then
|
2018-02-14 10:28:42 +02:00
|
|
|
if squads then
|
2018-02-13 09:10:17 +02:00
|
|
|
squads = formSquads(map, surface, natives, chunk, AI_SQUAD_COST)
|
|
|
|
end
|
2018-02-19 06:37:19 +02:00
|
|
|
if natives.enabledMigration and settlers then
|
2018-02-17 05:31:29 +02:00
|
|
|
settlers = formSettlers(map, surface, natives, chunk, AI_SETTLER_COST, tick)
|
2018-02-13 09:10:17 +02:00
|
|
|
end
|
2017-06-01 03:46:53 +02:00
|
|
|
end
|
2018-01-20 09:15:13 +02:00
|
|
|
|
2018-01-26 09:55:09 +02:00
|
|
|
if newEnemies then
|
|
|
|
local base = chunkToBase[chunk]
|
2018-02-14 10:28:42 +02:00
|
|
|
if base and ((tick - base.tick) > BASE_PROCESS_INTERVAL) and (mRandom() < 0.10) then
|
2018-02-10 22:57:54 +02:00
|
|
|
processBase(map, chunk, surface, natives, tick, base, evolutionFactor)
|
2018-01-26 09:55:09 +02:00
|
|
|
end
|
2018-10-20 07:17:37 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
i = i + 1
|
|
|
|
end
|
|
|
|
|
|
|
|
i = 1
|
|
|
|
|
|
|
|
for x=index,endIndex do
|
|
|
|
local chunk = processQueue[x]
|
|
|
|
if (chunk[CHUNK_TICK] ~= tick) then
|
|
|
|
chunk[CHUNK_TICK] = tick
|
2017-06-01 03:46:53 +02:00
|
|
|
|
2018-10-20 07:17:37 +02:00
|
|
|
commitPheromone(map, chunk, scentStaging[i])
|
2018-01-14 07:48:21 +02:00
|
|
|
scents(map, chunk)
|
2017-06-01 03:46:53 +02:00
|
|
|
end
|
2018-10-20 07:17:37 +02:00
|
|
|
i = i + 1
|
2016-08-29 02:05:28 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
if (endIndex == #processQueue) then
|
2018-01-14 07:48:21 +02:00
|
|
|
map.processIndex = 1
|
2016-08-29 02:05:28 +02:00
|
|
|
else
|
2018-01-14 07:48:21 +02:00
|
|
|
map.processIndex = endIndex + 1
|
2016-08-29 02:05:28 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-10-15 02:00:18 +02:00
|
|
|
--[[
|
|
|
|
Localized player radius were processing takes place in realtime, doesn't store state
|
|
|
|
between calls.
|
|
|
|
vs
|
|
|
|
the slower passive version processing the entire map in multiple passes.
|
|
|
|
--]]
|
2018-01-14 07:48:21 +02:00
|
|
|
function mapProcessor.processPlayers(players, map, surface, natives, tick)
|
2016-10-15 02:00:18 +02:00
|
|
|
-- put down player pheromone for player hunters
|
|
|
|
-- randomize player order to ensure a single player isn't singled out
|
|
|
|
local playerOrdering = nonRepeatingRandom(players)
|
2017-03-25 23:46:30 +02:00
|
|
|
|
2017-07-01 06:36:23 +02:00
|
|
|
local roll = mRandom()
|
2017-06-01 03:46:53 +02:00
|
|
|
|
|
|
|
local allowingAttacks = canAttack(natives, surface)
|
2016-10-15 02:00:18 +02:00
|
|
|
|
2018-10-20 07:17:37 +02:00
|
|
|
local scentStaging = map.scentStaging
|
|
|
|
|
2017-06-01 03:46:53 +02:00
|
|
|
local squads = allowingAttacks and (0.11 <= roll) and (roll <= 0.20) and (natives.points >= AI_SQUAD_COST)
|
|
|
|
|
2016-10-15 02:00:18 +02:00
|
|
|
for i=1,#playerOrdering do
|
|
|
|
local player = players[playerOrdering[i]]
|
2018-05-24 02:25:08 +02:00
|
|
|
if validPlayer(player, natives) then
|
2018-01-14 07:48:21 +02:00
|
|
|
local playerChunk = getChunkByPosition(map, player.character.position)
|
2016-10-15 02:00:18 +02:00
|
|
|
|
2017-11-21 09:27:03 +02:00
|
|
|
if (playerChunk ~= SENTINEL_IMPASSABLE_CHUNK) then
|
2016-10-15 02:00:18 +02:00
|
|
|
playerScent(playerChunk)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-05-26 00:23:22 +02:00
|
|
|
|
2018-10-20 07:17:37 +02:00
|
|
|
local i = 1
|
|
|
|
|
2018-05-26 00:23:22 +02:00
|
|
|
-- not looping everyone because the cost is high enough already in multiplayer
|
|
|
|
if (#playerOrdering > 0) then
|
|
|
|
local player = players[playerOrdering[1]]
|
2018-05-24 02:25:08 +02:00
|
|
|
if validPlayer(player, natives) then
|
2018-01-14 07:48:21 +02:00
|
|
|
local playerChunk = getChunkByPosition(map, player.character.position)
|
2016-10-15 02:00:18 +02:00
|
|
|
|
2017-11-21 09:27:03 +02:00
|
|
|
if (playerChunk ~= SENTINEL_IMPASSABLE_CHUNK) then
|
|
|
|
local vengence = (allowingAttacks and
|
|
|
|
(natives.points >= AI_VENGENCE_SQUAD_COST) and
|
2018-01-14 07:48:21 +02:00
|
|
|
((getEnemyStructureCount(map, playerChunk) > 0) or (playerChunk[MOVEMENT_PHEROMONE] < natives.retreatThreshold)))
|
2017-06-01 03:46:53 +02:00
|
|
|
|
2017-11-21 09:27:03 +02:00
|
|
|
for x=playerChunk.x - PROCESS_PLAYER_BOUND, playerChunk.x + PROCESS_PLAYER_BOUND, 32 do
|
|
|
|
for y=playerChunk.y - PROCESS_PLAYER_BOUND, playerChunk.y + PROCESS_PLAYER_BOUND, 32 do
|
2018-10-20 07:17:37 +02:00
|
|
|
local chunk = getChunkByXY(map, x, y)
|
2017-11-21 09:27:03 +02:00
|
|
|
|
|
|
|
if (chunk ~= SENTINEL_IMPASSABLE_CHUNK) and (chunk[CHUNK_TICK] ~= tick) then
|
2018-10-20 07:17:37 +02:00
|
|
|
processPheromone(map, chunk, scentStaging[i])
|
2017-04-22 01:14:04 +02:00
|
|
|
|
2018-01-14 07:48:21 +02:00
|
|
|
if (getNestCount(map, chunk) > 0) then
|
2017-06-11 02:59:06 +02:00
|
|
|
if squads then
|
2018-02-12 05:21:28 +02:00
|
|
|
squads = formSquads(map, surface, natives, chunk, AI_SQUAD_COST)
|
2017-06-11 02:59:06 +02:00
|
|
|
end
|
|
|
|
if vengence then
|
2018-02-12 05:21:28 +02:00
|
|
|
vengence = formSquads(map, surface, natives, chunk, AI_VENGENCE_SQUAD_COST)
|
2017-06-11 02:59:06 +02:00
|
|
|
end
|
2018-10-20 07:17:37 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
i = i + 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
i = 1
|
|
|
|
for x=playerChunk.x + PROCESS_PLAYER_BOUND, playerChunk.x - PROCESS_PLAYER_BOUND, -32 do
|
|
|
|
for y=playerChunk.y + PROCESS_PLAYER_BOUND, playerChunk.y - PROCESS_PLAYER_BOUND, -32 do
|
|
|
|
local chunk = getChunkByXY(map, x, y)
|
|
|
|
if (chunk ~= SENTINEL_IMPASSABLE_CHUNK) and (chunk[CHUNK_TICK] ~= tick) then
|
|
|
|
chunk[CHUNK_TICK] = tick
|
|
|
|
commitPheromone(map, chunk, scentStaging[i])
|
2018-01-14 07:48:21 +02:00
|
|
|
scents(map, chunk)
|
2016-10-15 02:00:18 +02:00
|
|
|
end
|
2018-10-20 07:17:37 +02:00
|
|
|
i = i + 1
|
2016-10-15 02:00:18 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-11-04 01:51:35 +02:00
|
|
|
--[[
|
2017-04-17 04:46:36 +02:00
|
|
|
Passive scan to find entities that have been generated outside the factorio event system
|
2016-11-04 01:51:35 +02:00
|
|
|
--]]
|
2018-02-12 05:21:28 +02:00
|
|
|
function mapProcessor.scanMap(map, surface, natives, tick)
|
2018-01-14 07:48:21 +02:00
|
|
|
local index = map.scanIndex
|
2017-06-01 03:46:53 +02:00
|
|
|
|
2018-01-14 07:48:21 +02:00
|
|
|
local unitCountQuery = map.filteredEntitiesEnemyUnitQuery
|
2018-01-01 07:49:36 +02:00
|
|
|
local offset = unitCountQuery.area[2]
|
|
|
|
local chunkBox = unitCountQuery.area[1]
|
2017-05-24 08:46:23 +02:00
|
|
|
|
2018-02-12 05:21:28 +02:00
|
|
|
local retreats = map.chunkToRetreats
|
|
|
|
local rallys = map.chunkToRallys
|
|
|
|
local spawners = map.chunkToSpawner
|
2018-02-17 05:31:29 +02:00
|
|
|
local settlers = map.chunkToSettler
|
2018-02-12 05:21:28 +02:00
|
|
|
|
2018-01-14 07:48:21 +02:00
|
|
|
local processQueue = map.processQueue
|
2016-09-14 13:16:33 +02:00
|
|
|
local endIndex = mMin(index + SCAN_QUEUE_SIZE, #processQueue)
|
2017-06-01 03:46:53 +02:00
|
|
|
|
2016-08-29 02:05:28 +02:00
|
|
|
for x=index,endIndex do
|
2016-10-15 02:00:18 +02:00
|
|
|
local chunk = processQueue[x]
|
2017-04-17 04:46:36 +02:00
|
|
|
|
2018-01-01 07:49:36 +02:00
|
|
|
chunkBox[1] = chunk.x
|
|
|
|
chunkBox[2] = chunk.y
|
2017-05-28 06:50:37 +02:00
|
|
|
|
2017-06-01 03:46:53 +02:00
|
|
|
offset[1] = chunk.x + CHUNK_SIZE
|
|
|
|
offset[2] = chunk.y + CHUNK_SIZE
|
2018-02-12 05:21:28 +02:00
|
|
|
|
|
|
|
local retreatTick = retreats[chunk]
|
|
|
|
if retreatTick and ((tick - retreatTick) > INTERVAL_RETREAT) then
|
|
|
|
retreats[chunk] = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
local rallyTick = rallys[chunk]
|
|
|
|
if rallyTick and ((tick - rallyTick) > INTERVAL_RALLY) then
|
|
|
|
rallys[chunk] = nil
|
|
|
|
end
|
|
|
|
|
2018-02-17 05:31:29 +02:00
|
|
|
local spawnerTick = spawners[chunk]
|
|
|
|
if spawnerTick and ((tick - spawnerTick) > INTERVAL_SPAWNER) then
|
2018-02-12 05:21:28 +02:00
|
|
|
spawners[chunk] = nil
|
|
|
|
end
|
|
|
|
|
2018-02-17 05:31:29 +02:00
|
|
|
local settlerTick = spawners[chunk]
|
|
|
|
if settlerTick and ((tick - settlerTick) > 0) then
|
|
|
|
settlers[chunk] = nil
|
|
|
|
end
|
|
|
|
|
2018-05-26 00:23:22 +02:00
|
|
|
local closeBy = findNearbySquad(map, chunk, chunk)
|
2018-02-17 05:31:29 +02:00
|
|
|
|
2018-05-26 00:23:22 +02:00
|
|
|
if closeBy then
|
|
|
|
local deadGroup = surface.count_entities_filtered(unitCountQuery) > 300
|
2017-06-01 03:46:53 +02:00
|
|
|
|
2018-05-26 00:23:22 +02:00
|
|
|
if deadGroup then
|
2017-06-11 02:59:06 +02:00
|
|
|
recycleBiters(natives, surface.find_enemy_units(chunk, TRIPLE_CHUNK_SIZE))
|
2017-04-22 01:14:04 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-01-14 07:48:21 +02:00
|
|
|
analyzeChunk(chunk, natives, surface, map)
|
2016-08-05 06:47:51 +02:00
|
|
|
end
|
2017-04-22 01:14:04 +02:00
|
|
|
|
2016-08-29 02:05:28 +02:00
|
|
|
if (endIndex == #processQueue) then
|
2018-01-14 07:48:21 +02:00
|
|
|
map.scanIndex = 1
|
2016-08-29 02:05:28 +02:00
|
|
|
else
|
2018-01-14 07:48:21 +02:00
|
|
|
map.scanIndex = endIndex + 1
|
2016-08-20 04:52:27 +02:00
|
|
|
end
|
2016-08-05 06:47:51 +02:00
|
|
|
end
|
|
|
|
|
2016-09-08 22:02:23 +02:00
|
|
|
return mapProcessor
|