1
0
mirror of https://github.com/veden/Rampant.git synced 2025-01-03 22:52:20 +02:00
Rampant/libs/BaseUtils.lua

111 lines
2.7 KiB
Lua
Raw Normal View History

2017-05-08 08:56:11 +02:00
local baseUtils = {}
-- imports
2018-01-14 09:07:29 +02:00
local stringUtils = require("StringUtils")
local mathUtils = require("MathUtils")
2017-05-08 08:56:11 +02:00
local constants = require("Constants")
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
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
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
local positionToChunkXY = mapUtils.positionToChunkXY
local getChunkBase = chunkPropertyUtils.getChunkBase
local setChunkBase = chunkPropertyUtils.setChunkBase
2017-06-01 03:46:53 +02:00
local mRandom = math.random
2017-05-19 09:47:24 +02:00
-- module code
2017-05-08 08:56:11 +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
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
2017-05-28 06:50:37 +02:00
return foundBase
2017-05-19 09:47:24 +02:00
end
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()
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
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 = {
x = x,
y = y,
2017-05-12 06:50:06 +02:00
created = tick,
alignment = { BASE_ALIGNMENT_NEUTRAL },
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
}
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