1
0
mirror of https://github.com/veden/Rampant.git synced 2025-01-10 00:28:31 +02:00
Rampant/libs/PheromoneUtils.lua

209 lines
8.6 KiB
Lua
Raw Normal View History

local pheromoneUtils = {}
-- imports
local mathUtils = require("MathUtils")
local mapUtils = require("MapUtils")
local constants = require("Constants")
local chunkPropertyUtils = require("ChunkPropertyUtils")
-- constants
2016-10-15 02:00:18 +02:00
local MOVEMENT_PHEROMONE = constants.MOVEMENT_PHEROMONE
local BASE_PHEROMONE = constants.BASE_PHEROMONE
local PLAYER_PHEROMONE = constants.PLAYER_PHEROMONE
2017-06-08 02:57:24 +02:00
local RESOURCE_PHEROMONE = constants.RESOURCE_PHEROMONE
2017-04-22 01:14:04 +02:00
local BUILDING_PHEROMONES = constants.BUILDING_PHEROMONES
local PLAYER_PHEROMONE_GENERATOR_AMOUNT = constants.PLAYER_PHEROMONE_GENERATOR_AMOUNT
2016-10-15 02:00:18 +02:00
local MOVEMENT_PHEROMONE_PERSISTANCE = constants.MOVEMENT_PHEROMONE_PERSISTANCE
2017-04-22 01:14:04 +02:00
local BASE_PHEROMONE_PERSISTANCE = constants.BASE_PHEROMONE_PERSISTANCE
local PLAYER_PHEROMONE_PERSISTANCE = constants.PLAYER_PHEROMONE_PERSISTANCE
2017-06-08 02:57:24 +02:00
local RESOURCE_PHEROMONE_PERSISTANCE = constants.RESOURCE_PHEROMONE_PERSISTANCE
2016-08-21 23:48:55 +02:00
2018-09-26 07:14:13 +02:00
local DEATH_PHEROMONE_GENERATOR_AMOUNT = constants.DEATH_PHEROMONE_GENERATOR_AMOUNT
-- imported functions
2016-10-15 02:00:18 +02:00
local getCardinalChunks = mapUtils.getCardinalChunks
2018-09-26 07:14:13 +02:00
local getNeighborChunks = mapUtils.getNeighborChunks
local getEnemyStructureCount = chunkPropertyUtils.getEnemyStructureCount
2018-09-24 06:56:45 +02:00
local getPathRating = chunkPropertyUtils.getPathRating
local getPlayerBaseGenerator = chunkPropertyUtils.getPlayerBaseGenerator
local getResourceGenerator = chunkPropertyUtils.getResourceGenerator
2018-09-24 06:56:45 +02:00
local getDeathGenerator = chunkPropertyUtils.getDeathGenerator
local addDeathGenerator = chunkPropertyUtils.addDeathGenerator
local decayDeathGenerator = chunkPropertyUtils.decayDeathGenerator
2017-08-08 10:19:51 +02:00
2019-02-11 08:14:17 +02:00
local linearInterpolation = mathUtils.linearInterpolation
-- module code
2017-01-20 07:58:36 +02:00
2018-01-14 07:48:21 +02:00
function pheromoneUtils.scents(map, chunk)
2018-09-26 07:14:13 +02:00
chunk[BASE_PHEROMONE] = chunk[BASE_PHEROMONE] + (getPlayerBaseGenerator(map, chunk))
2018-01-14 07:48:21 +02:00
local resourceGenerator = getResourceGenerator(map, chunk)
2018-02-12 07:47:33 +02:00
local enemyCount = getEnemyStructureCount(map, chunk)
2018-09-26 07:14:13 +02:00
chunk[MOVEMENT_PHEROMONE] = chunk[MOVEMENT_PHEROMONE] - (getDeathGenerator(map, chunk))
2019-02-11 08:14:17 +02:00
2018-02-12 07:47:33 +02:00
if (resourceGenerator > 0) and (enemyCount == 0) then
2018-09-26 07:14:13 +02:00
chunk[RESOURCE_PHEROMONE] = chunk[RESOURCE_PHEROMONE] + (linearInterpolation(resourceGenerator, 9000, 10000))
2017-04-16 08:04:22 +02:00
end
end
2017-01-20 07:58:36 +02:00
2018-09-26 07:14:13 +02:00
function pheromoneUtils.victoryScent(map, chunk, entityType)
2017-04-22 01:14:04 +02:00
local value = BUILDING_PHEROMONES[entityType]
2017-11-21 09:27:03 +02:00
if value then
2018-09-26 07:14:13 +02:00
-- chunk[MOVEMENT_PHEROMONE] = chunk[MOVEMENT_PHEROMONE] + (value * 1000)
2019-02-11 08:14:17 +02:00
-- FIX ME
addDeathGenerator(map, chunk, -(value * 3))
chunk[MOVEMENT_PHEROMONE] = chunk[MOVEMENT_PHEROMONE] + value
2017-04-22 01:14:04 +02:00
end
end
2018-09-24 06:56:45 +02:00
function pheromoneUtils.deathScent(map, chunk)
2018-09-26 07:14:13 +02:00
chunk[MOVEMENT_PHEROMONE] = chunk[MOVEMENT_PHEROMONE] - (DEATH_PHEROMONE_GENERATOR_AMOUNT * 2)
addDeathGenerator(map, chunk, DEATH_PHEROMONE_GENERATOR_AMOUNT)
-- chunk[MOVEMENT_PHEROMONE] = chunk[MOVEMENT_PHEROMONE] - DEATH_PHEROMONE_GENERATOR_AMOUNT
end
2016-10-15 02:00:18 +02:00
function pheromoneUtils.playerScent(playerChunk)
2017-11-21 09:27:03 +02:00
playerChunk[PLAYER_PHEROMONE] = playerChunk[PLAYER_PHEROMONE] + PLAYER_PHEROMONE_GENERATOR_AMOUNT
end
2018-10-20 07:17:37 +02:00
function pheromoneUtils.commitPheromone(map, chunk, staging)
chunk[MOVEMENT_PHEROMONE] = staging[MOVEMENT_PHEROMONE]
chunk[BASE_PHEROMONE] = staging[BASE_PHEROMONE]
chunk[PLAYER_PHEROMONE] = staging[PLAYER_PHEROMONE]
chunk[RESOURCE_PHEROMONE] = staging[RESOURCE_PHEROMONE]
2019-02-11 08:14:17 +02:00
2018-10-20 07:17:37 +02:00
decayDeathGenerator(map, chunk)
end
function pheromoneUtils.processPheromone(map, chunk, staging)
2019-02-11 08:14:17 +02:00
local chunkMovement = chunk[MOVEMENT_PHEROMONE]
local chunkBase = chunk[BASE_PHEROMONE]
local chunkPlayer = chunk[PLAYER_PHEROMONE]
2017-06-08 02:57:24 +02:00
local chunkResource = chunk[RESOURCE_PHEROMONE]
2018-09-24 06:56:45 +02:00
local chunkPathRating = getPathRating(map, chunk)
2018-02-13 09:10:17 +02:00
local clear = (getEnemyStructureCount(map, chunk) == 0)
2019-02-11 08:14:17 +02:00
2018-10-20 07:17:37 +02:00
local tempNeighbors = getNeighborChunks(map, chunk.x, chunk.y)
2017-11-21 09:27:03 +02:00
2018-02-12 07:47:33 +02:00
local movementTotal = 0
local baseTotal = 0
local playerTotal = 0
local resourceTotal = 0
2019-02-11 08:14:17 +02:00
local neighborFlagNW = 0
local neighborFlagNE = 0
local neighborFlagSW = 0
local neighborFlagSE = 0
local neighborCount = 0
local neighbor
neighbor = tempNeighbors[2]
if not neighbor.name then
2019-02-11 08:14:17 +02:00
neighborCount = neighborCount + 1
neighborFlagNW = neighborFlagNW + 1
neighborFlagNE = neighborFlagNE + 1
movementTotal = movementTotal + (neighbor[MOVEMENT_PHEROMONE] - chunkMovement)
baseTotal = baseTotal + (neighbor[BASE_PHEROMONE] - chunkBase)
2019-02-11 08:14:17 +02:00
playerTotal = playerTotal + (neighbor[PLAYER_PHEROMONE] - chunkPlayer)
resourceTotal = resourceTotal + (neighbor[RESOURCE_PHEROMONE] - chunkResource)
2018-02-12 07:47:33 +02:00
end
2019-02-11 08:14:17 +02:00
neighbor = tempNeighbors[4]
if not neighbor.name then
2019-02-11 08:14:17 +02:00
neighborCount = neighborCount + 1
neighborFlagNW = neighborFlagNW + 1
neighborFlagSW = neighborFlagSW + 1
movementTotal = movementTotal + (neighbor[MOVEMENT_PHEROMONE] - chunkMovement)
baseTotal = baseTotal + (neighbor[BASE_PHEROMONE] - chunkBase)
playerTotal = playerTotal + (neighbor[PLAYER_PHEROMONE] - chunkPlayer)
resourceTotal = resourceTotal + (neighbor[RESOURCE_PHEROMONE] - chunkResource)
2018-02-12 07:47:33 +02:00
end
2019-02-11 08:14:17 +02:00
neighbor = tempNeighbors[5]
if not neighbor.name then
2019-02-11 08:14:17 +02:00
neighborCount = neighborCount + 1
neighborFlagNE = neighborFlagNE + 1
neighborFlagSE = neighborFlagSE + 1
movementTotal = movementTotal + (neighbor[MOVEMENT_PHEROMONE] - chunkMovement)
baseTotal = baseTotal + (neighbor[BASE_PHEROMONE] - chunkBase)
2019-02-11 08:14:17 +02:00
playerTotal = playerTotal + neighbor[PLAYER_PHEROMONE] - chunkPlayer
resourceTotal = resourceTotal + (neighbor[RESOURCE_PHEROMONE] - chunkResource)
2018-02-12 07:47:33 +02:00
end
2019-02-11 08:14:17 +02:00
neighbor = tempNeighbors[7]
if not neighbor.name then
2019-02-11 08:14:17 +02:00
neighborCount = neighborCount + 1
neighborFlagSW = neighborFlagSW + 1
neighborFlagSE = neighborFlagSE + 1
movementTotal = movementTotal + (neighbor[MOVEMENT_PHEROMONE] - chunkMovement)
baseTotal = baseTotal + (neighbor[BASE_PHEROMONE] - chunkBase)
playerTotal = playerTotal + (neighbor[PLAYER_PHEROMONE] - chunkPlayer)
resourceTotal = resourceTotal + (neighbor[RESOURCE_PHEROMONE] - chunkResource)
2018-02-12 07:47:33 +02:00
end
2018-10-20 07:17:37 +02:00
2019-02-11 08:14:17 +02:00
neighbor = tempNeighbors[1]
if (neighborFlagNW == 2) and not neighbor.name then
neighborCount = neighborCount + 1
2018-10-20 07:17:37 +02:00
movementTotal = movementTotal + (neighbor[MOVEMENT_PHEROMONE] - chunkMovement)
baseTotal = baseTotal + (neighbor[BASE_PHEROMONE] - chunkBase)
playerTotal = playerTotal + neighbor[PLAYER_PHEROMONE] - chunkPlayer
resourceTotal = resourceTotal + (neighbor[RESOURCE_PHEROMONE] - chunkResource)
end
2019-02-11 08:14:17 +02:00
neighbor = tempNeighbors[3]
if (neighborFlagNE == 2) and not neighbor.name then
neighborCount = neighborCount + 1
2018-10-20 07:17:37 +02:00
movementTotal = movementTotal + (neighbor[MOVEMENT_PHEROMONE] - chunkMovement)
baseTotal = baseTotal + (neighbor[BASE_PHEROMONE] - chunkBase)
playerTotal = playerTotal + (neighbor[PLAYER_PHEROMONE] - chunkPlayer)
resourceTotal = resourceTotal + (neighbor[RESOURCE_PHEROMONE] - chunkResource)
end
2019-02-11 08:14:17 +02:00
neighbor = tempNeighbors[6]
if (neighborFlagSW == 2) and not neighbor.name then
neighborCount = neighborCount + 1
2018-10-20 07:17:37 +02:00
movementTotal = movementTotal + (neighbor[MOVEMENT_PHEROMONE] - chunkMovement)
baseTotal = baseTotal + (neighbor[BASE_PHEROMONE] - chunkBase)
playerTotal = playerTotal + (neighbor[PLAYER_PHEROMONE] - chunkPlayer)
resourceTotal = resourceTotal + (neighbor[RESOURCE_PHEROMONE] - chunkResource)
end
2019-02-11 08:14:17 +02:00
2018-10-20 07:17:37 +02:00
neighbor = tempNeighbors[8]
2019-02-11 08:14:17 +02:00
if (neighborFlagSE == 2) and not neighbor.name then
neighborCount = neighborCount + 1
2018-10-20 07:17:37 +02:00
movementTotal = movementTotal + (neighbor[MOVEMENT_PHEROMONE] - chunkMovement)
baseTotal = baseTotal + (neighbor[BASE_PHEROMONE] - chunkBase)
playerTotal = playerTotal + (neighbor[PLAYER_PHEROMONE] - chunkPlayer)
resourceTotal = resourceTotal + (neighbor[RESOURCE_PHEROMONE] - chunkResource)
end
2019-02-11 08:14:17 +02:00
local neighborDiv
if neighborCount == 0 then
neighborDiv = 0
else
neighborDiv = ((1/neighborCount) * 1.20)
end
staging[MOVEMENT_PHEROMONE] = (chunkMovement + (neighborDiv * movementTotal)) * MOVEMENT_PHEROMONE_PERSISTANCE * chunkPathRating
staging[BASE_PHEROMONE] = (chunkBase + (neighborDiv * baseTotal)) * BASE_PHEROMONE_PERSISTANCE * chunkPathRating
staging[PLAYER_PHEROMONE] = (chunkPlayer + (neighborDiv * playerTotal)) * PLAYER_PHEROMONE_PERSISTANCE * chunkPathRating
2018-02-13 09:10:17 +02:00
if clear then
2019-02-11 08:14:17 +02:00
staging[RESOURCE_PHEROMONE] = (chunkResource + (neighborDiv * resourceTotal)) * RESOURCE_PHEROMONE_PERSISTANCE * chunkPathRating
2018-02-13 09:10:17 +02:00
else
2019-02-11 08:14:17 +02:00
staging[RESOURCE_PHEROMONE] = (chunkResource + (neighborDiv * resourceTotal)) * 0.01
2018-02-13 09:10:17 +02:00
end
end
return pheromoneUtils