2016-10-14 17:00:18 -07:00
|
|
|
local aiPlanning = {}
|
|
|
|
|
|
|
|
-- imports
|
|
|
|
|
|
|
|
local constants = require("Constants")
|
|
|
|
local mathUtils = require("MathUtils")
|
2017-05-27 21:50:37 -07:00
|
|
|
local aiPredicates = require("AIPredicates")
|
2017-05-31 18:46:53 -07:00
|
|
|
|
2016-10-14 17:00:18 -07:00
|
|
|
-- constants
|
|
|
|
|
2017-05-31 18:46:53 -07:00
|
|
|
local NO_RETREAT_BASE_PERCENT = constants.NO_RETREAT_BASE_PERCENT
|
|
|
|
local NO_RETREAT_EVOLUTION_BONUS_MAX = constants.NO_RETREAT_EVOLUTION_BONUS_MAX
|
|
|
|
|
2016-10-14 17:00:18 -07:00
|
|
|
local AI_STATE_PEACEFUL = constants.AI_STATE_PEACEFUL
|
|
|
|
local AI_STATE_AGGRESSIVE = constants.AI_STATE_AGGRESSIVE
|
2018-02-09 23:57:04 -08:00
|
|
|
local AI_STATE_RAIDING = constants.AI_STATE_RAIDING
|
2017-05-13 15:32:16 -07:00
|
|
|
local AI_STATE_NOCTURNAL = constants.AI_STATE_NOCTURNAL
|
2016-10-14 17:00:18 -07:00
|
|
|
|
2017-05-31 18:46:53 -07:00
|
|
|
local AI_UNIT_REFUND = constants.AI_UNIT_REFUND
|
|
|
|
|
2018-02-10 00:42:17 -08:00
|
|
|
local AI_MAX_OVERFLOW_POINTS = constants.AI_MAX_OVERFLOW_POINTS
|
|
|
|
|
2016-10-14 17:00:18 -07:00
|
|
|
local AI_MAX_POINTS = constants.AI_MAX_POINTS
|
|
|
|
local AI_POINT_GENERATOR_AMOUNT = constants.AI_POINT_GENERATOR_AMOUNT
|
|
|
|
|
|
|
|
local AI_MIN_STATE_DURATION = constants.AI_MIN_STATE_DURATION
|
|
|
|
local AI_MIN_TEMPERAMENT_DURATION = constants.AI_MIN_TEMPERAMENT_DURATION
|
|
|
|
local AI_MAX_STATE_DURATION = constants.AI_MAX_STATE_DURATION
|
|
|
|
local AI_MAX_TEMPERAMENT_DURATION = constants.AI_MAX_TEMPERAMENT_DURATION
|
|
|
|
|
2018-01-23 22:12:31 -08:00
|
|
|
-- local AI_MAX_SQUAD_COUNT = constants.AI_MAX_SQUAD_COUNT
|
2017-05-26 17:58:33 -07:00
|
|
|
|
2017-05-31 18:46:53 -07:00
|
|
|
local BASE_RALLY_CHANCE = constants.BASE_RALLY_CHANCE
|
|
|
|
local BONUS_RALLY_CHANCE = constants.BONUS_RALLY_CHANCE
|
|
|
|
|
|
|
|
local RETREAT_MOVEMENT_PHEROMONE_LEVEL = constants.RETREAT_MOVEMENT_PHEROMONE_LEVEL
|
|
|
|
|
2017-04-21 16:33:17 -07:00
|
|
|
local TICKS_A_MINUTE = constants.TICKS_A_MINUTE
|
|
|
|
|
2016-10-14 17:00:18 -07:00
|
|
|
-- imported functions
|
|
|
|
|
2017-05-27 21:50:37 -07:00
|
|
|
local canAttack = aiPredicates.canAttack
|
2017-05-13 15:32:16 -07:00
|
|
|
|
2016-10-14 17:00:18 -07:00
|
|
|
local randomTickEvent = mathUtils.randomTickEvent
|
|
|
|
|
2017-06-15 18:30:26 -07:00
|
|
|
local mFloor = math.floor
|
|
|
|
|
2017-06-30 21:36:23 -07:00
|
|
|
local mRandom = math.random
|
|
|
|
|
2016-10-14 17:00:18 -07:00
|
|
|
local mMax = math.max
|
|
|
|
|
|
|
|
-- module code
|
|
|
|
|
2017-05-27 21:50:37 -07:00
|
|
|
local function isShockwaveReady(evolution_factor, natives, surface, tick, maxPoints)
|
|
|
|
return canAttack(natives, surface) and
|
|
|
|
(tick - natives.lastShakeMessage > TICKS_A_MINUTE * 5) and
|
2018-01-23 22:02:06 -08:00
|
|
|
((evolution_factor > 0.7) and (natives.points > maxPoints * 0.85) and (#natives.squads > 20))
|
2017-05-27 21:50:37 -07:00
|
|
|
end
|
|
|
|
|
2018-01-20 23:42:47 -08:00
|
|
|
function aiPlanning.planning(natives, evolution_factor, tick, surface, connectedPlayers)
|
2016-10-14 17:00:18 -07:00
|
|
|
local maxPoints = AI_MAX_POINTS * evolution_factor
|
2017-05-31 18:46:53 -07:00
|
|
|
|
2017-05-13 15:32:16 -07:00
|
|
|
if natives.aiNocturnalMode then
|
|
|
|
maxPoints = maxPoints * 0.85
|
|
|
|
end
|
2017-05-31 18:46:53 -07:00
|
|
|
|
|
|
|
local attackWaveMaxSize = natives.attackWaveMaxSize
|
|
|
|
natives.retreatThreshold = -(evolution_factor * RETREAT_MOVEMENT_PHEROMONE_LEVEL)
|
|
|
|
natives.rallyThreshold = BASE_RALLY_CHANCE + (evolution_factor * BONUS_RALLY_CHANCE)
|
|
|
|
natives.formSquadThreshold = mMax((0.25 * evolution_factor), 0.10)
|
|
|
|
natives.attackWaveSize = attackWaveMaxSize * (evolution_factor ^ 1.66667)
|
|
|
|
natives.attackWaveDeviation = (attackWaveMaxSize * 0.5) * 0.333
|
|
|
|
natives.attackWaveLowerBound = 1
|
|
|
|
natives.attackWaveUpperBound = attackWaveMaxSize + (attackWaveMaxSize * 0.25)
|
|
|
|
natives.unitRefundAmount = AI_UNIT_REFUND * evolution_factor
|
|
|
|
natives.kamikazeThreshold = NO_RETREAT_BASE_PERCENT + (evolution_factor * NO_RETREAT_EVOLUTION_BONUS_MAX)
|
|
|
|
local threshold = natives.attackThresholdRange
|
|
|
|
natives.attackWaveThreshold = (threshold - (threshold * evolution_factor)) + natives.attackThresholdMin
|
2018-01-20 23:42:47 -08:00
|
|
|
|
|
|
|
local points = mFloor((AI_POINT_GENERATOR_AMOUNT * mRandom()) + ((AI_POINT_GENERATOR_AMOUNT * 0.7) * (evolution_factor ^ 2.5)) * natives.aiPointsScaler)
|
|
|
|
|
|
|
|
natives.baseIncrement = points
|
2017-05-31 18:46:53 -07:00
|
|
|
|
2016-10-14 17:00:18 -07:00
|
|
|
if (natives.points < maxPoints) then
|
2018-01-20 23:42:47 -08:00
|
|
|
natives.points = natives.points + points
|
2016-10-14 17:00:18 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
if (natives.temperamentTick == tick) then
|
2017-06-30 21:36:23 -07:00
|
|
|
natives.temperament = mRandom()
|
2016-10-14 17:00:18 -07:00
|
|
|
natives.temperamentTick = randomTickEvent(tick, AI_MIN_TEMPERAMENT_DURATION, AI_MAX_TEMPERAMENT_DURATION)
|
|
|
|
end
|
|
|
|
|
|
|
|
if (natives.stateTick == tick) then
|
2017-06-30 21:36:23 -07:00
|
|
|
local roll = mRandom() * mMax(1 - evolution_factor, 0.15)
|
2016-10-14 17:00:18 -07:00
|
|
|
if (roll > natives.temperament) then
|
|
|
|
natives.state = AI_STATE_PEACEFUL
|
2017-05-31 18:46:53 -07:00
|
|
|
elseif natives.aiNocturnalMode then
|
2017-05-13 15:32:16 -07:00
|
|
|
natives.state = AI_STATE_NOCTURNAL
|
2016-10-14 17:00:18 -07:00
|
|
|
else
|
2018-02-09 23:57:04 -08:00
|
|
|
roll = mRandom()
|
2018-02-10 00:42:17 -08:00
|
|
|
if (roll > 0.08) then
|
2018-02-09 23:57:04 -08:00
|
|
|
natives.state = AI_STATE_AGGRESSIVE
|
|
|
|
else
|
|
|
|
natives.state = AI_STATE_RAIDING
|
2018-02-10 00:42:17 -08:00
|
|
|
|
|
|
|
natives.points = natives.points + 1000
|
|
|
|
if (natives.points > AI_MAX_OVERFLOW_POINTS) then
|
|
|
|
natives.points = AI_MAX_OVERFLOW_POINTS
|
|
|
|
end
|
2018-02-09 23:57:04 -08:00
|
|
|
end
|
2016-10-14 17:00:18 -07:00
|
|
|
end
|
|
|
|
natives.stateTick = randomTickEvent(tick, AI_MIN_STATE_DURATION, AI_MAX_STATE_DURATION)
|
2017-04-21 16:33:17 -07:00
|
|
|
end
|
|
|
|
|
2017-05-27 21:50:37 -07:00
|
|
|
if isShockwaveReady(evolution_factor, natives, surface, tick, maxPoints) then
|
2017-04-21 16:33:17 -07:00
|
|
|
natives.lastShakeMessage = tick
|
2018-01-20 23:42:47 -08:00
|
|
|
for _, player in pairs(connectedPlayers) do
|
2018-02-10 00:42:17 -08:00
|
|
|
if player.mod_settings["rampant-attack-warning"].value then
|
|
|
|
player.print("Rampant: The ground begins to shake")
|
|
|
|
end
|
2017-10-18 19:12:46 -07:00
|
|
|
end
|
2017-04-21 16:33:17 -07:00
|
|
|
end
|
2017-05-31 18:46:53 -07:00
|
|
|
|
2016-10-14 17:00:18 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
return aiPlanning
|