1
0
mirror of https://github.com/veden/Rampant.git synced 2025-05-27 22:57:38 +02:00
Rampant/libs/BaseUtils.lua

153 lines
4.1 KiB
Lua
Raw Normal View History

2017-05-07 23:56:11 -07:00
local baseUtils = {}
-- imports
local mapUtils = require("MapUtils")
local constants = require("Constants")
2017-05-19 00:47:24 -07:00
local mathUtils = require("MathUtils")
2017-05-27 21:50:37 -07:00
2017-06-07 17:57:24 -07:00
local baseRegisterUtils = require("BaseRegisterUtils")
local tendrilUtils = require("TendrilUtils")
2017-05-07 23:56:11 -07:00
-- constants
local BASE_DISTANCE_THRESHOLD = constants.BASE_DISTANCE_THRESHOLD
local BASE_ALIGNMENT_NEUTRAL = constants.BASE_ALIGNMENT_NEUTRAL
2017-05-19 00:47:24 -07:00
local AI_NEST_COST = constants.AI_NEST_COST
local AI_WORM_COST = constants.AI_WORM_COST
2017-05-31 18:46:53 -07:00
local DOUBLE_CHUNK_SIZE = constants.DOUBLE_CHUNK_SIZE
2017-05-07 23:56:11 -07:00
local MAGIC_MAXIMUM_NUMBER = constants.MAGIC_MAXIMUM_NUMBER
2017-05-19 00:47:24 -07:00
local MAGIC_MAXIMUM_BASE_NUMBER = constants.MAGIC_MAXIMUM_BASE_NUMBER
2017-05-07 23:56:11 -07:00
-- imported functions
local euclideanDistancePoints = mapUtils.euclideanDistancePoints
2017-05-27 21:50:37 -07:00
local gaussianRandomRange = mathUtils.gaussianRandomRange
2017-05-07 23:56:11 -07:00
2017-05-31 18:46:53 -07:00
local mFloor = math.floor
2017-06-07 17:57:24 -07:00
local buildTendril = tendrilUtils.buildTendril
2017-05-31 18:46:53 -07:00
2017-06-07 17:57:24 -07:00
local registerEnemyBaseStructure = baseRegisterUtils.registerEnemyBaseStructure
2017-05-31 18:46:53 -07:00
2017-05-19 00:47:24 -07:00
-- module code
2017-05-07 23:56:11 -07:00
2017-05-27 21:50:37 -07:00
function baseUtils.findNearbyBase(natives, position)
2017-05-11 21:50:06 -07:00
local bases = natives.bases
2017-05-27 21:50:37 -07:00
local foundBase
2017-05-11 21:50:06 -07:00
local closest = MAGIC_MAXIMUM_NUMBER
for i=1,#bases do
local base = bases[i]
2017-05-19 00:47:24 -07:00
local distance = euclideanDistancePoints(base.x, base.y, position.x, position.y)
2017-05-31 18:46:53 -07:00
if (distance <= (BASE_DISTANCE_THRESHOLD + (base.level * 100))) and (distance < closest) then
2017-05-11 21:50:06 -07:00
closest = distance
2017-05-27 21:50:37 -07:00
foundBase = base
2017-05-07 23:56:11 -07:00
end
end
2017-05-27 21:50:37 -07:00
return foundBase
2017-05-19 00:47:24 -07:00
end
function baseUtils.buildHive(regionMap, base, surface)
local valid = false
2017-05-31 18:46:53 -07:00
local position = surface.find_non_colliding_position("biter-spawner-hive", base, DOUBLE_CHUNK_SIZE, 2)
2017-05-19 00:47:24 -07:00
if position then
2017-05-27 21:50:37 -07:00
local biterSpawner = {name="biter-spawner-hive", position=position}
2017-05-31 18:46:53 -07:00
local hive = surface.create_entity(biterSpawner)
if (#base.hives == 0) then
base.x = hive.position.x
base.y = hive.position.y
end
base.hives[#base.hives+1] = hive
2017-06-07 17:57:24 -07:00
registerEnemyBaseStructure(regionMap, hive, base)
2017-05-19 00:47:24 -07:00
valid = true
2017-05-11 21:50:06 -07:00
end
2017-05-19 00:47:24 -07:00
return valid
2017-05-07 23:56:11 -07:00
end
2017-05-19 00:47:24 -07:00
function baseUtils.buildOutpost(natives, base, surface, tick, position)
2017-05-07 23:56:11 -07:00
end
2017-05-19 00:47:24 -07:00
function baseUtils.buildOrder(regionMap, natives, base, surface, tick)
2017-05-31 18:46:53 -07:00
local foundHive = false
for _,_ in pairs(base.hives) do
foundHive = true
break
end
if not foundHive or (base.upgradePoints < 10) then
2017-05-19 00:47:24 -07:00
return
end
local generator = natives.randomGenerator
generator.re_seed(base.pattern)
for level=0,base.level do
local slices = (level * 3)
2017-05-31 18:46:53 -07:00
slices = gaussianRandomRange(slices, slices * 0.1, slices * 0.6, slices * 1.4, generator)
2017-05-19 00:47:24 -07:00
local slice = (2 * math.pi) / slices
local pos = 0
local thing
local cost
local radiusAdjustment
if (generator() < 0.3) then
thing = "small-worm-turret"
cost = AI_WORM_COST
radiusAdjustment = -4
else
thing = "biter-spawner"
cost = AI_NEST_COST
radiusAdjustment = 0
end
for _ = 1, slices do
if (base.upgradePoints < 10) then
return
end
local radius = 10 * level
local distortion = gaussianRandomRange(radius, 10, radius - 7.5 + radiusAdjustment, radius + 7.5 + radiusAdjustment, generator)
local nestPosition = {x = base.x + (distortion * math.cos(pos)),
y = base.y + (distortion * math.sin(pos))}
local biterSpawner = {name=thing, position=nestPosition}
2017-05-31 18:46:53 -07:00
if surface.can_place_entity(biterSpawner) then
2017-06-07 17:57:24 -07:00
registerEnemyBaseStructure(regionMap, surface.create_entity(biterSpawner), base)
2017-05-19 00:47:24 -07:00
base.upgradePoints = base.upgradePoints - cost
end
pos = pos + slice
end
end
end
function baseUtils.createBase(regionMap, natives, position, surface, tick)
2017-05-07 23:56:11 -07:00
local bases = natives.bases
2017-05-31 18:46:53 -07:00
local distance = euclideanDistancePoints(position.x, position.y, 0, 0)
2017-05-11 21:50:06 -07:00
local base = {
2017-05-19 00:47:24 -07:00
x = position.x,
y = position.y,
2017-05-11 21:50:06 -07:00
created = tick,
alignment = { BASE_ALIGNMENT_NEUTRAL },
2017-05-27 21:50:37 -07:00
hives = {},
2017-06-07 17:57:24 -07:00
tendrils = {},
2017-05-11 21:50:06 -07:00
nests = {},
worms = {},
2017-05-19 00:47:24 -07:00
eggs = {},
2017-05-11 21:50:06 -07:00
upgradePoints = 0,
growth = tick,
2017-05-19 00:47:24 -07:00
pattern = math.random(MAGIC_MAXIMUM_BASE_NUMBER),
2017-05-31 18:46:53 -07:00
level = mFloor(distance / 200)
2017-05-11 21:50:06 -07:00
}
2017-05-19 00:47:24 -07:00
if not baseUtils.buildHive(regionMap, base, surface) then
return nil
2017-06-07 17:57:24 -07:00
end
buildTendril(regionMap, natives, base, surface, tick)
2017-05-07 23:56:11 -07:00
bases[#bases+1] = base
2017-05-11 21:50:06 -07:00
return base
2017-05-07 23:56:11 -07:00
end
return baseUtils