1
0
mirror of https://github.com/veden/Rampant.git synced 2025-01-05 22:53:24 +02:00
Rampant/libs/PheromoneUtils.lua

290 lines
13 KiB
Lua
Raw Normal View History

2019-02-16 06:17:30 +02:00
if pheromoneUtilsG then
return pheromoneUtilsG
end
local pheromoneUtils = {}
-- imports
local mathUtils = require("MathUtils")
local mapUtils = require("MapUtils")
local constants = require("Constants")
local chunkPropertyUtils = require("ChunkPropertyUtils")
-- constants
2019-03-06 08:18:03 +02:00
local CHUNK_TICK = constants.CHUNK_TICK
2019-10-19 23:04:38 +02:00
local NEIGHBOR_DIVIDER = constants.NEIGHBOR_DIVIDER
2019-03-06 08:18:03 +02:00
2019-10-19 21:13:48 +02:00
local CHUNK_ALL_DIRECTIONS = constants.CHUNK_ALL_DIRECTIONS
local CHUNK_NORTH_SOUTH = constants.CHUNK_NORTH_SOUTH
local CHUNK_EAST_WEST = constants.CHUNK_EAST_WEST
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
2019-10-19 23:04:38 +02:00
local VICTORY_SCENT = constants.VICTORY_SCENT
2017-04-22 01:14:04 +02:00
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
2019-10-19 23:04:38 +02:00
local DOUBLE_DEATH_PHEROMONE_GENERATOR_AMOUNT = constants.DOUBLE_DEATH_PHEROMONE_GENERATOR_AMOUNT
2018-09-26 07:14:13 +02:00
-- imported functions
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
2019-10-19 21:13:48 +02:00
local getPassable = chunkPropertyUtils.getPassable
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-09-26 07:14:13 +02:00
function pheromoneUtils.victoryScent(map, chunk, entityType)
2019-10-19 23:04:38 +02:00
local value = VICTORY_SCENT[entityType]
2017-11-21 09:27:03 +02:00
if value then
2019-10-19 23:04:38 +02:00
addDeathGenerator(map, chunk, -value)
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)
2019-10-19 23:04:38 +02:00
chunk[MOVEMENT_PHEROMONE] = chunk[MOVEMENT_PHEROMONE] - DOUBLE_DEATH_PHEROMONE_GENERATOR_AMOUNT
2018-09-26 07:14:13 +02:00
addDeathGenerator(map, chunk, 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
2019-03-06 08:18:03 +02:00
function pheromoneUtils.commitPheromone(map, chunk, staging, tick)
chunk[CHUNK_TICK] = tick
2019-02-11 08:14:17 +02:00
2019-03-06 08:18:03 +02:00
local resourceGenerator = getResourceGenerator(map, chunk)
2019-10-19 23:04:38 +02:00
decayDeathGenerator(map, chunk)
chunk[MOVEMENT_PHEROMONE] = staging[MOVEMENT_PHEROMONE] - getDeathGenerator(map, chunk)
chunk[BASE_PHEROMONE] = staging[BASE_PHEROMONE] + getPlayerBaseGenerator(map, chunk)
chunk[PLAYER_PHEROMONE] = staging[PLAYER_PHEROMONE]
if (resourceGenerator > 0) and (getEnemyStructureCount(map, chunk) == 0) then
2019-12-07 07:57:20 +02:00
chunk[RESOURCE_PHEROMONE] = staging[RESOURCE_PHEROMONE] + (linearInterpolation(resourceGenerator, 15000, 20000))
2019-10-20 23:25:47 +02:00
else
chunk[RESOURCE_PHEROMONE] = staging[RESOURCE_PHEROMONE]
2019-03-06 08:18:03 +02:00
end
2018-10-20 07:17:37 +02:00
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
2020-05-20 04:37:16 +02:00
2019-02-11 08:14:17 +02:00
local neighborCount = 0
2019-10-19 23:04:38 +02:00
local neighbor
local neighborPass
2019-10-19 21:13:48 +02:00
local chunkPass = getPassable(map, chunk)
2019-10-19 23:04:38 +02:00
2019-10-19 21:13:48 +02:00
if (chunkPass == CHUNK_ALL_DIRECTIONS) then
2020-05-20 04:37:16 +02:00
neighbor = tempNeighbors[2]
if (neighbor ~= -1) then
neighborPass = getPassable(map, neighbor)
if ((neighborPass == CHUNK_ALL_DIRECTIONS) or (neighborPass == CHUNK_NORTH_SOUTH)) then
neighborCount = neighborCount + 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)
movementTotal = movementTotal + neighbor[MOVEMENT_PHEROMONE]
baseTotal = baseTotal + neighbor[BASE_PHEROMONE]
playerTotal = playerTotal + neighbor[PLAYER_PHEROMONE]
resourceTotal = resourceTotal + neighbor[RESOURCE_PHEROMONE]
end
2019-10-19 21:13:48 +02:00
end
2019-10-19 23:04:38 +02:00
neighbor = tempNeighbors[7]
2020-05-20 04:37:16 +02:00
if (neighbor ~= -1) then
neighborPass = getPassable(map, neighbor)
if ((neighborPass == CHUNK_ALL_DIRECTIONS) or (neighborPass == CHUNK_NORTH_SOUTH)) then
neighborCount = neighborCount + 1
movementTotal = movementTotal + neighbor[MOVEMENT_PHEROMONE]
baseTotal = baseTotal + neighbor[BASE_PHEROMONE]
playerTotal = playerTotal + neighbor[PLAYER_PHEROMONE]
resourceTotal = resourceTotal + neighbor[RESOURCE_PHEROMONE]
end
2019-10-19 21:13:48 +02:00
end
2019-10-19 23:04:38 +02:00
neighbor = tempNeighbors[4]
2020-05-20 04:37:16 +02:00
if (neighbor ~= -1) then
neighborPass = getPassable(map, neighbor)
if ((neighborPass == CHUNK_ALL_DIRECTIONS) or (neighborPass == CHUNK_EAST_WEST)) then
neighborCount = neighborCount + 1
movementTotal = movementTotal + neighbor[MOVEMENT_PHEROMONE]
baseTotal = baseTotal + neighbor[BASE_PHEROMONE]
playerTotal = playerTotal + neighbor[PLAYER_PHEROMONE]
resourceTotal = resourceTotal + neighbor[RESOURCE_PHEROMONE]
end
2019-10-19 21:13:48 +02:00
end
2019-10-19 23:04:38 +02:00
neighbor = tempNeighbors[5]
2020-05-20 04:37:16 +02:00
if (neighbor ~= -1) then
neighborPass = getPassable(map, neighbor)
if ((neighborPass == CHUNK_ALL_DIRECTIONS) or (neighborPass == CHUNK_EAST_WEST)) then
neighborCount = neighborCount + 1
movementTotal = movementTotal + neighbor[MOVEMENT_PHEROMONE]
baseTotal = baseTotal + neighbor[BASE_PHEROMONE]
playerTotal = playerTotal + neighbor[PLAYER_PHEROMONE]
resourceTotal = resourceTotal + neighbor[RESOURCE_PHEROMONE]
end
2019-10-19 21:13:48 +02:00
end
2019-10-19 23:04:38 +02:00
neighbor = tempNeighbors[1]
2020-05-20 04:37:16 +02:00
if (neighbor ~= -1) then
neighborPass = getPassable(map, neighbor)
if (neighborPass == CHUNK_ALL_DIRECTIONS) then
neighborCount = neighborCount + 1
movementTotal = movementTotal + neighbor[MOVEMENT_PHEROMONE]
baseTotal = baseTotal + neighbor[BASE_PHEROMONE]
playerTotal = playerTotal + neighbor[PLAYER_PHEROMONE]
resourceTotal = resourceTotal + neighbor[RESOURCE_PHEROMONE]
end
2019-10-19 21:13:48 +02:00
end
2019-10-19 23:04:38 +02:00
neighbor = tempNeighbors[3]
2020-05-20 04:37:16 +02:00
if (neighbor ~= -1) then
neighborPass = getPassable(map, neighbor)
if (neighborPass == CHUNK_ALL_DIRECTIONS) then
neighborCount = neighborCount + 1
movementTotal = movementTotal + neighbor[MOVEMENT_PHEROMONE]
baseTotal = baseTotal + neighbor[BASE_PHEROMONE]
playerTotal = playerTotal + neighbor[PLAYER_PHEROMONE]
resourceTotal = resourceTotal + neighbor[RESOURCE_PHEROMONE]
end
2019-10-19 21:13:48 +02:00
end
2019-10-19 23:04:38 +02:00
neighbor = tempNeighbors[6]
2020-05-20 04:37:16 +02:00
if (neighbor ~= -1) then
neighborPass = getPassable(map, neighbor)
if (neighborPass == CHUNK_ALL_DIRECTIONS) then
neighborCount = neighborCount + 1
movementTotal = movementTotal + neighbor[MOVEMENT_PHEROMONE]
baseTotal = baseTotal + neighbor[BASE_PHEROMONE]
playerTotal = playerTotal + neighbor[PLAYER_PHEROMONE]
resourceTotal = resourceTotal + neighbor[RESOURCE_PHEROMONE]
end
2019-10-19 21:13:48 +02:00
end
2019-10-19 23:04:38 +02:00
neighbor = tempNeighbors[8]
2020-05-20 04:37:16 +02:00
if (neighbor ~= -1) then
neighborPass = getPassable(map, neighbor)
if (neighborPass == CHUNK_ALL_DIRECTIONS) then
neighborCount = neighborCount + 1
movementTotal = movementTotal + neighbor[MOVEMENT_PHEROMONE]
baseTotal = baseTotal + neighbor[BASE_PHEROMONE]
playerTotal = playerTotal + neighbor[PLAYER_PHEROMONE]
resourceTotal = resourceTotal + neighbor[RESOURCE_PHEROMONE]
end
2019-10-19 21:13:48 +02:00
end
elseif (chunkPass == CHUNK_EAST_WEST) then
2019-10-19 23:04:38 +02:00
neighbor = tempNeighbors[4]
2020-05-20 04:37:16 +02:00
if (neighbor ~= -1) then
neighborPass = getPassable(map, neighbor)
if ((neighborPass == CHUNK_ALL_DIRECTIONS) or (neighborPass == CHUNK_EAST_WEST)) then
neighborCount = neighborCount + 1
movementTotal = movementTotal + neighbor[MOVEMENT_PHEROMONE]
baseTotal = baseTotal + neighbor[BASE_PHEROMONE]
playerTotal = playerTotal + neighbor[PLAYER_PHEROMONE]
resourceTotal = resourceTotal + neighbor[RESOURCE_PHEROMONE]
end
2019-10-19 21:13:48 +02:00
end
2019-10-19 23:04:38 +02:00
neighbor = tempNeighbors[5]
2020-05-20 04:37:16 +02:00
if (neighbor ~= -1) then
neighborPass = getPassable(map, neighbor)
if ((neighborPass == CHUNK_ALL_DIRECTIONS) or (neighborPass == CHUNK_EAST_WEST)) then
neighborCount = neighborCount + 1
movementTotal = movementTotal + neighbor[MOVEMENT_PHEROMONE]
baseTotal = baseTotal + neighbor[BASE_PHEROMONE]
playerTotal = playerTotal + neighbor[PLAYER_PHEROMONE]
resourceTotal = resourceTotal + neighbor[RESOURCE_PHEROMONE]
end
2019-10-19 21:13:48 +02:00
end
2019-10-19 23:04:38 +02:00
2019-10-19 21:13:48 +02:00
elseif (chunkPass == CHUNK_NORTH_SOUTH) then
2019-10-19 23:04:38 +02:00
neighbor = tempNeighbors[2]
2020-05-20 04:37:16 +02:00
if (neighbor ~= -1) then
neighborPass = getPassable(map, neighbor)
if ((neighborPass == CHUNK_ALL_DIRECTIONS) or (neighborPass == CHUNK_NORTH_SOUTH)) then
neighborCount = neighborCount + 1
movementTotal = movementTotal + neighbor[MOVEMENT_PHEROMONE]
baseTotal = baseTotal + neighbor[BASE_PHEROMONE]
playerTotal = playerTotal + neighbor[PLAYER_PHEROMONE]
resourceTotal = resourceTotal + neighbor[RESOURCE_PHEROMONE]
end
2019-10-19 21:13:48 +02:00
end
2019-10-19 23:04:38 +02:00
neighbor = tempNeighbors[7]
2020-05-20 04:37:16 +02:00
if (neighbor ~= -1) then
neighborPass = getPassable(map, neighbor)
if ((neighborPass == CHUNK_ALL_DIRECTIONS) or (neighborPass == CHUNK_NORTH_SOUTH)) then
neighborCount = neighborCount + 1
movementTotal = movementTotal + neighbor[MOVEMENT_PHEROMONE]
baseTotal = baseTotal + neighbor[BASE_PHEROMONE]
playerTotal = playerTotal + neighbor[PLAYER_PHEROMONE]
resourceTotal = resourceTotal + neighbor[RESOURCE_PHEROMONE]
end
2019-10-19 21:13:48 +02:00
end
2018-10-20 07:17:37 +02:00
end
2019-02-11 08:14:17 +02:00
2019-10-19 23:04:38 +02:00
local neighborDiv = 0
if neighborCount ~= 0 then
neighborDiv = NEIGHBOR_DIVIDER[neighborCount]
2020-05-20 04:37:16 +02:00
chunkMovement = chunkMovement + (neighborDiv * (movementTotal - (chunkMovement * neighborCount)))
chunkBase = chunkBase + (neighborDiv * (baseTotal - (chunkBase * neighborCount)))
chunkPlayer = chunkPlayer + (neighborDiv * (playerTotal - (chunkPlayer * neighborCount)))
chunkResource = chunkResource + (neighborDiv * (resourceTotal - (chunkResource * neighborCount)))
2019-02-11 08:14:17 +02:00
end
2020-05-20 04:37:16 +02:00
staging[MOVEMENT_PHEROMONE] = chunkMovement * MOVEMENT_PHEROMONE_PERSISTANCE * chunkPathRating
staging[BASE_PHEROMONE] = chunkBase * BASE_PHEROMONE_PERSISTANCE * chunkPathRating
staging[PLAYER_PHEROMONE] = chunkPlayer * PLAYER_PHEROMONE_PERSISTANCE * chunkPathRating
2018-02-13 09:10:17 +02:00
if clear then
2020-05-20 04:37:16 +02:00
staging[RESOURCE_PHEROMONE] = chunkResource * RESOURCE_PHEROMONE_PERSISTANCE * chunkPathRating
2018-02-13 09:10:17 +02:00
else
2020-05-20 04:37:16 +02:00
staging[RESOURCE_PHEROMONE] = chunkResource * 0.01
2018-02-13 09:10:17 +02:00
end
end
2019-02-16 06:17:30 +02:00
pheromoneUtilsG = pheromoneUtils
return pheromoneUtils