2017-05-08 08:56:11 +02:00
|
|
|
local baseUtils = {}
|
|
|
|
|
|
|
|
-- imports
|
|
|
|
|
2018-01-14 09:07:29 +02:00
|
|
|
local stringUtils = require("StringUtils")
|
2017-06-16 03:30:26 +02:00
|
|
|
local mathUtils = require("MathUtils")
|
2017-05-08 08:56:11 +02:00
|
|
|
local constants = require("Constants")
|
2018-01-15 09:41:55 +02:00
|
|
|
local mapUtils = require("MapUtils")
|
|
|
|
local chunkPropertyUtils = require("ChunkPropertyUtils")
|
2017-06-08 02:57:24 +02:00
|
|
|
|
2017-05-08 08:56:11 +02:00
|
|
|
-- constants
|
|
|
|
|
|
|
|
local BASE_DISTANCE_THRESHOLD = constants.BASE_DISTANCE_THRESHOLD
|
|
|
|
|
|
|
|
local BASE_ALIGNMENT_NEUTRAL = constants.BASE_ALIGNMENT_NEUTRAL
|
|
|
|
|
2018-01-15 09:41:55 +02:00
|
|
|
local CHUNK_SIZE = constants.CHUNK_SIZE
|
|
|
|
|
2017-05-08 08:56:11 +02:00
|
|
|
local MAGIC_MAXIMUM_NUMBER = constants.MAGIC_MAXIMUM_NUMBER
|
2017-05-19 09:47:24 +02:00
|
|
|
local MAGIC_MAXIMUM_BASE_NUMBER = constants.MAGIC_MAXIMUM_BASE_NUMBER
|
2017-05-08 08:56:11 +02:00
|
|
|
|
|
|
|
-- imported functions
|
|
|
|
|
2017-06-16 03:30:26 +02:00
|
|
|
local euclideanDistancePoints = mathUtils.euclideanDistancePoints
|
2017-05-08 08:56:11 +02:00
|
|
|
|
2018-01-14 09:07:29 +02:00
|
|
|
local isRampant = stringUtils.isRampant
|
2017-05-08 08:56:11 +02:00
|
|
|
|
2017-06-01 03:46:53 +02:00
|
|
|
local mFloor = math.floor
|
|
|
|
|
2018-01-15 09:41:55 +02:00
|
|
|
local positionToChunkXY = mapUtils.positionToChunkXY
|
|
|
|
local getChunkBase = chunkPropertyUtils.getChunkBase
|
|
|
|
local setChunkBase = chunkPropertyUtils.setChunkBase
|
2017-06-01 03:46:53 +02:00
|
|
|
|
2017-07-01 06:36:23 +02:00
|
|
|
local mRandom = math.random
|
|
|
|
|
2017-05-19 09:47:24 +02:00
|
|
|
-- module code
|
2017-05-08 08:56:11 +02:00
|
|
|
|
2018-01-15 09:41:55 +02:00
|
|
|
function baseUtils.findNearbyBase(map, chunk, chunkRadius)
|
|
|
|
local x = chunk.x
|
|
|
|
local y = chunk.y
|
|
|
|
|
|
|
|
local foundBase = getChunkBase(map, chunk)
|
|
|
|
if foundBase then
|
|
|
|
return foundBase
|
|
|
|
end
|
|
|
|
|
2017-05-12 06:50:06 +02:00
|
|
|
local closest = MAGIC_MAXIMUM_NUMBER
|
2018-01-15 09:41:55 +02:00
|
|
|
for xi = x-chunkRadius, x+chunkRadius, CHUNK_SIZE do
|
|
|
|
for yi = y-chunkRadius, y+chunkRadius, CHUNK_SIZE do
|
|
|
|
if (xi ~= x) and (yi ~= y) then
|
|
|
|
local base = getChunkBase(map, positionToChunkXY(map, xi, yi))
|
|
|
|
if base then
|
|
|
|
local distance = euclideanDistancePoints(base.x, base.y, x, y)
|
|
|
|
if (distance <= (BASE_DISTANCE_THRESHOLD + (base.level * 100))) and (distance < closest) then
|
|
|
|
closest = distance
|
|
|
|
foundBase = base
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-05-08 08:56:11 +02:00
|
|
|
end
|
|
|
|
end
|
2018-01-15 09:41:55 +02:00
|
|
|
|
2017-05-28 06:50:37 +02:00
|
|
|
return foundBase
|
2017-05-19 09:47:24 +02:00
|
|
|
end
|
|
|
|
|
2018-01-15 09:41:55 +02:00
|
|
|
function baseUtils.upgradeEntity(map, entity, surface, natives, evolutionFactor, tick)
|
2018-01-14 09:07:29 +02:00
|
|
|
if not isRampant(entity.name) then
|
|
|
|
local position = entity.position
|
|
|
|
entity.die()
|
2018-01-15 09:41:55 +02:00
|
|
|
local chunk = positionToChunkXY(map, position)
|
|
|
|
local base = getChunkBase(map, chunk)
|
|
|
|
|
|
|
|
if not base then
|
|
|
|
baseUtils.createBase(map, natives, evolutionFactor, chunk, surface, tick)
|
|
|
|
end
|
|
|
|
|
|
|
|
entity = surface.creaate_entity({name = "rampant-suicide-nest-v" .. mRandom(5) .. "-t1",
|
|
|
|
position = position})
|
|
|
|
|
2018-01-14 09:07:29 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
return entity
|
|
|
|
end
|
|
|
|
|
2018-01-15 09:41:55 +02:00
|
|
|
function baseUtils.createBase(map, natives, evolutionFactor, chunk, surface, tick)
|
|
|
|
local x = chunk.x
|
|
|
|
local y = chunk.y
|
|
|
|
local distance = euclideanDistancePoints(x, y, 0, 0)
|
2017-05-12 06:50:06 +02:00
|
|
|
local base = {
|
2018-01-15 09:41:55 +02:00
|
|
|
x = x,
|
|
|
|
y = y,
|
2017-05-12 06:50:06 +02:00
|
|
|
created = tick,
|
|
|
|
alignment = { BASE_ALIGNMENT_NEUTRAL },
|
2017-07-01 06:36:23 +02:00
|
|
|
pattern = mRandom(MAGIC_MAXIMUM_BASE_NUMBER),
|
2017-06-01 03:46:53 +02:00
|
|
|
level = mFloor(distance / 200)
|
2017-05-12 06:50:06 +02:00
|
|
|
}
|
2018-01-15 09:41:55 +02:00
|
|
|
|
|
|
|
setChunkBase(map, chunk, base)
|
|
|
|
|
|
|
|
-- if not buildHive(map, base, surface) then
|
|
|
|
-- return nil
|
|
|
|
-- end
|
|
|
|
|
|
|
|
natives.bases[natives.bases+1] = base
|
|
|
|
|
2017-05-12 06:50:06 +02:00
|
|
|
return base
|
2017-05-08 08:56:11 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
return baseUtils
|
|
|
|
|