mirror of
https://github.com/veden/Rampant.git
synced 2025-02-03 13:11:54 +02:00
added more robust resource navigation
This commit is contained in:
parent
0dd998daaf
commit
fca0fbf434
@ -7,36 +7,51 @@ local mathUtils = require("MathUtils")
|
||||
|
||||
local baseRegisterUtils = require("BaseRegisterUtils")
|
||||
|
||||
local mapUtils = require("MapUtils")
|
||||
|
||||
-- constants
|
||||
|
||||
local MAGIC_MAXIMUM_BASE_NUMBER = constants.MAGIC_MAXIMUM_BASE_NUMBER
|
||||
|
||||
-- imported functions
|
||||
|
||||
local AI_NEST_COST = constants.AI_NEST_COST
|
||||
local AI_WORM_COST = constants.AI_WORM_COST
|
||||
|
||||
local NEST_COUNT = constants.NEST_COUNT
|
||||
|
||||
local DOUBLE_CHUNK_SIZE = constants.DOUBLE_CHUNK_SIZE
|
||||
|
||||
-- imported functions
|
||||
|
||||
local registerEnemyBaseStructure = baseRegisterUtils.registerEnemyBaseStructure
|
||||
local gaussianRandomRange = mathUtils.gaussianRandomRange
|
||||
|
||||
local mRandom = math.random
|
||||
|
||||
local getChunkByPosition = mapUtils.getChunkByPosition
|
||||
|
||||
-- module code
|
||||
|
||||
function buildUtils.buildNest(regionMap, base, surface, targetPosition, name)
|
||||
local position = surface.find_non_colliding_position(name, targetPosition, DOUBLE_CHUNK_SIZE, 2)
|
||||
local chunk = getChunkByPosition(regionMap, position.x, position.y)
|
||||
local nest = nil
|
||||
if position and chunk and (chunk[NEST_COUNT] < 3) then
|
||||
local biterSpawner = {name=name, position=position}
|
||||
nest = surface.create_entity(biterSpawner)
|
||||
registerEnemyBaseStructure(regionMap, nest, base)
|
||||
end
|
||||
return nest
|
||||
end
|
||||
|
||||
function buildUtils.buildHive(regionMap, base, surface)
|
||||
local valid = false
|
||||
local position = surface.find_non_colliding_position("biter-spawner-hive", base, DOUBLE_CHUNK_SIZE, 2)
|
||||
if position then
|
||||
local biterSpawner = {name="biter-spawner-hive", position=position}
|
||||
local hive = surface.create_entity(biterSpawner)
|
||||
local hive = buildUtils.buildNest(regionMap, base, surface, base, "biter-spawner-hive")
|
||||
if hive then
|
||||
if (#base.hives == 0) then
|
||||
base.x = hive.position.x
|
||||
base.y = hive.position.y
|
||||
end
|
||||
base.hives[#base.hives+1] = hive
|
||||
registerEnemyBaseStructure(regionMap, hive, base)
|
||||
valid = true
|
||||
end
|
||||
return valid
|
||||
|
@ -180,7 +180,7 @@ end
|
||||
function chunkUtils.scoreChunk(chunk, surface, natives, tempQuery)
|
||||
tempQuery.force = nil
|
||||
tempQuery.type = "resource"
|
||||
chunk[RESOURCE_GENERATOR] = surface.count_entities_filtered(tempQuery)
|
||||
chunk[RESOURCE_GENERATOR] = surface.count_entities_filtered(tempQuery) * 0.001
|
||||
|
||||
tempQuery.type = nil
|
||||
tempQuery.force = "player"
|
||||
|
@ -35,7 +35,7 @@ constants.INTERVAL_LOGIC = 38
|
||||
|
||||
constants.PLAYER_PHEROMONE_MULTIPLER = 100
|
||||
|
||||
constants.DEV_CUSTOM_AI = false
|
||||
constants.DEV_CUSTOM_AI = true
|
||||
|
||||
-- chunk properties
|
||||
|
||||
@ -109,7 +109,7 @@ constants.IMPASSABLE_TERRAIN_GENERATOR_AMOUNT = -0.1
|
||||
constants.MOVEMENT_PHEROMONE_PERSISTANCE = 0.9
|
||||
constants.BASE_PHEROMONE_PERSISTANCE = 0.99
|
||||
constants.PLAYER_PHEROMONE_PERSISTANCE = 0.98
|
||||
constants.RESOURCE_PHEROMONE_PERSISTANCE = 0.85
|
||||
constants.RESOURCE_PHEROMONE_PERSISTANCE = 0.9
|
||||
|
||||
-- chunk attributes
|
||||
|
||||
|
@ -232,7 +232,7 @@ function mapProcessor.scanMap(regionMap, surface, natives)
|
||||
end
|
||||
end
|
||||
|
||||
chunk[RESOURCE_GENERATOR] = surface.count_entities_filtered(resourceQuery)
|
||||
chunk[RESOURCE_GENERATOR] = surface.count_entities_filtered(resourceQuery) * 0.001
|
||||
|
||||
local entities = surface.find_entities_filtered(playerQuery)
|
||||
local playerBaseGenerator = 0
|
||||
|
@ -10,6 +10,10 @@ local constants = require("Constants")
|
||||
|
||||
local MAGIC_MAXIMUM_NUMBER = constants.MAGIC_MAXIMUM_NUMBER
|
||||
|
||||
local RESOURCE_PHEROMONE = constants.RESOURCE_PHEROMONE
|
||||
|
||||
-- imported functions
|
||||
|
||||
local canMoveChunkDirection = mapUtils.canMoveChunkDirection
|
||||
|
||||
-- module code
|
||||
@ -41,6 +45,31 @@ function neighborUtils.scoreNeighborsForAttack(chunk, neighborDirectionChunks, s
|
||||
return highestChunk, highestDirection
|
||||
end
|
||||
|
||||
--[[
|
||||
Expects all neighbors adjacent to a chunk
|
||||
--]]
|
||||
function neighborUtils.scoreNeighborsForResource(chunk, neighborDirectionChunks, scoreFunction, squad, threshold)
|
||||
local highestChunk
|
||||
local highestScore = -MAGIC_MAXIMUM_NUMBER
|
||||
local highestDirection
|
||||
for x=1,8 do
|
||||
local neighborChunk = neighborDirectionChunks[x]
|
||||
if neighborChunk and canMoveChunkDirection(x, chunk, neighborChunk) and (neighborChunk[RESOURCE_PHEROMONE] > threshold) then
|
||||
local score = scoreFunction(squad, neighborChunk)
|
||||
if (score > highestScore) then
|
||||
highestScore = score
|
||||
highestChunk = neighborChunk
|
||||
highestDirection = x
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
if scoreFunction(squad, chunk) > highestScore then
|
||||
return nil, -1
|
||||
end
|
||||
|
||||
return highestChunk, highestDirection
|
||||
end
|
||||
|
||||
--[[
|
||||
Expects all neighbors adjacent to a chunk
|
||||
|
@ -47,8 +47,8 @@ function pheromoneUtils.scents(chunk)
|
||||
chunk[BASE_PHEROMONE] = IMPASSABLE_TERRAIN_GENERATOR_AMOUNT;
|
||||
else
|
||||
chunk[BASE_PHEROMONE] = chunk[BASE_PHEROMONE] + chunk[PLAYER_BASE_GENERATOR]
|
||||
if (chunk[NEST_COUNT] == 0) then
|
||||
chunk[RESOURCE_PHEROMONE] = chunk[RESOURCE_PHEROMONE] + chunk[RESOURCE_GENERATOR]
|
||||
if (chunk[NEST_COUNT] == 0) and (chunk[RESOURCE_GENERATOR] > 0) then
|
||||
chunk[RESOURCE_PHEROMONE] = chunk[RESOURCE_PHEROMONE] + math.max((chunk[RESOURCE_GENERATOR] * 100), 90)
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -15,11 +15,13 @@ local mathUtils = require("MathUtils")
|
||||
|
||||
local RESOURCE_PHEROMONE = constants.RESOURCE_PHEROMONE
|
||||
|
||||
local RESOURCE_GENERATOR = constants.RESOURCE_GENERATOR
|
||||
|
||||
local NEST_COUNT = constants.NEST_COUNT
|
||||
|
||||
-- imported functions
|
||||
|
||||
local scoreNeighborsForAttack = neighborsUtils.scoreNeighborsForAttack
|
||||
local scoreNeighborsForResource = neighborsUtils.scoreNeighborsForResource
|
||||
|
||||
local getNeighborChunks = mapUtils.getNeighborChunks
|
||||
|
||||
@ -27,12 +29,10 @@ local getChunkByPosition = mapUtils.getChunkByPosition
|
||||
|
||||
local positionFromDirectionAndChunk = mapUtils.positionFromDirectionAndChunk
|
||||
|
||||
local registerEnemyBaseStructure = baseRegisterUtils.registerEnemyBaseStructure
|
||||
local buildNest = buildUtils.buildNest
|
||||
|
||||
local buildOutpost = buildUtils.buildOutpost
|
||||
|
||||
local euclideanDistanceNamed = mathUtils.euclideanDistanceNamed
|
||||
|
||||
-- module code
|
||||
|
||||
local function scoreTendrilChunk(squad, chunk)
|
||||
@ -64,6 +64,8 @@ end
|
||||
local function buildTendrilPath(regionMap, tendril, surface, base, tick, natives)
|
||||
local tendrilUnit = tendril.unit
|
||||
if not tendrilUnit.valid then
|
||||
removeTendril(base, tendril)
|
||||
tendrilUtils.buildTendril(regionMap, natives, base, surface, tick)
|
||||
return
|
||||
end
|
||||
if (tendril.cycles > 0) then
|
||||
@ -73,41 +75,34 @@ local function buildTendrilPath(regionMap, tendril, surface, base, tick, natives
|
||||
local tendrilPosition = tendrilUnit.position
|
||||
local chunk = getChunkByPosition(regionMap, tendrilPosition.x, tendrilPosition.y)
|
||||
if chunk then
|
||||
local tendrilPath,tendrilDirection = scoreNeighborsForAttack(chunk,
|
||||
getNeighborChunks(regionMap,
|
||||
chunk.cX,
|
||||
chunk.cY),
|
||||
scoreTendrilChunk,
|
||||
nil)
|
||||
local tendrilPath,tendrilDirection = scoreNeighborsForResource(chunk,
|
||||
getNeighborChunks(regionMap,
|
||||
chunk.cX,
|
||||
chunk.cY),
|
||||
scoreTendrilChunk,
|
||||
nil)
|
||||
if (tendrilDirection == -1) then
|
||||
removeTendril(base, tendril)
|
||||
buildOutpost(regionMap, natives, base, surface, tendril)
|
||||
tendrilUtils.buildTendril(regionMap, natives, base, surface, tick)
|
||||
colorChunk(chunk.x, chunk.y, "hazard-concrete-left", surface)
|
||||
if (chunk[RESOURCE_GENERATOR] ~= 0) then
|
||||
buildOutpost(regionMap, natives, base, surface, tendril)
|
||||
removeTendril(base, tendril)
|
||||
tendrilUtils.buildTendril(regionMap, natives, base, surface, tick)
|
||||
colorChunk(chunk.x, chunk.y, "hazard-concrete-left", surface)
|
||||
end
|
||||
return
|
||||
elseif tendrilPath then
|
||||
positionFromDirectionAndChunk(tendrilDirection, tendrilPosition, tendrilPosition, 0.5)
|
||||
mathUtils.distortPosition(tendrilPosition)
|
||||
-- mathUtils.distortPosition(tendrilPosition)
|
||||
-- tendril.path[#tendril.path] = chunk
|
||||
local position = surface.find_non_colliding_position("spitter-spawner",
|
||||
tendrilPosition,
|
||||
32,
|
||||
2)
|
||||
if position then
|
||||
tendril.target = position
|
||||
buildNest(regionMap, base, surface, tendril.unit.position, "spitter-spawner")
|
||||
-- tendril.cycles = 3
|
||||
tendrilUnit.set_command({ type = defines.command.go_to_location,
|
||||
destination = position,
|
||||
distraction = defines.distraction.by_none })
|
||||
|
||||
-- needs to check to make sure unit still exists as well.
|
||||
--tendril.cycles = 2
|
||||
-- tendril.x = position.x
|
||||
-- tendril.y = position.y
|
||||
-- local biterSpawner = {name="spitter-spawner", position=position}
|
||||
-- local hive = surface.create_entity(biterSpawner)
|
||||
-- registerEnemyBaseStructure(regionMap, hive, base)
|
||||
-- elseif not position then
|
||||
-- removeTendril(base, tendril)
|
||||
distraction = defines.distraction.by_none })
|
||||
end
|
||||
end
|
||||
colorChunk(chunk.x, chunk.y, "concrete", surface)
|
||||
@ -134,7 +129,6 @@ function tendrilUtils.createTendril(base, surface)
|
||||
local tendril = {
|
||||
unit = entity,
|
||||
penalties = {},
|
||||
target = entity.position,
|
||||
cycles = 0,
|
||||
path = {}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user