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

511 lines
19 KiB
Lua
Raw Normal View History

2019-02-15 20:17:30 -08: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
local MAGIC_MAXIMUM_NUMBER = constants.MAGIC_MAXIMUM_NUMBER
2019-03-05 22:18:03 -08:00
local CHUNK_TICK = constants.CHUNK_TICK
2019-10-19 14:04:38 -07:00
local NEIGHBOR_DIVIDER = constants.NEIGHBOR_DIVIDER
2019-03-05 22:18:03 -08:00
2019-10-19 12:13:48 -07: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-14 17:00:18 -07:00
local MOVEMENT_PHEROMONE = constants.MOVEMENT_PHEROMONE
local BASE_PHEROMONE = constants.BASE_PHEROMONE
local PLAYER_PHEROMONE = constants.PLAYER_PHEROMONE
2017-06-07 17:57:24 -07:00
local RESOURCE_PHEROMONE = constants.RESOURCE_PHEROMONE
2017-04-21 16:14:04 -07:00
local BUILDING_PHEROMONES = constants.BUILDING_PHEROMONES
2019-10-19 14:04:38 -07:00
local VICTORY_SCENT = constants.VICTORY_SCENT
2017-04-21 16:14:04 -07:00
local PLAYER_PHEROMONE_GENERATOR_AMOUNT = constants.PLAYER_PHEROMONE_GENERATOR_AMOUNT
2016-10-14 17:00:18 -07:00
local MOVEMENT_PHEROMONE_PERSISTANCE = constants.MOVEMENT_PHEROMONE_PERSISTANCE
2017-04-21 16:14:04 -07:00
local BASE_PHEROMONE_PERSISTANCE = constants.BASE_PHEROMONE_PERSISTANCE
local PLAYER_PHEROMONE_PERSISTANCE = constants.PLAYER_PHEROMONE_PERSISTANCE
2017-06-07 17:57:24 -07:00
local RESOURCE_PHEROMONE_PERSISTANCE = constants.RESOURCE_PHEROMONE_PERSISTANCE
2016-08-21 14:48:55 -07:00
2018-09-25 22:14:13 -07:00
local DEATH_PHEROMONE_GENERATOR_AMOUNT = constants.DEATH_PHEROMONE_GENERATOR_AMOUNT
2019-10-19 14:04:38 -07:00
local DOUBLE_DEATH_PHEROMONE_GENERATOR_AMOUNT = constants.DOUBLE_DEATH_PHEROMONE_GENERATOR_AMOUNT
2018-09-25 22:14:13 -07:00
-- imported functions
local getPlayersOnChunk = chunkPropertyUtils.getPlayersOnChunk
2018-09-25 22:14:13 -07:00
local getNeighborChunks = mapUtils.getNeighborChunks
local getEnemyStructureCount = chunkPropertyUtils.getEnemyStructureCount
2018-09-23 21:56:45 -07:00
local getPathRating = chunkPropertyUtils.getPathRating
2019-10-19 12:13:48 -07:00
local getPassable = chunkPropertyUtils.getPassable
local getPlayerBaseGenerator = chunkPropertyUtils.getPlayerBaseGenerator
local getResourceGenerator = chunkPropertyUtils.getResourceGenerator
2018-09-23 21:56:45 -07:00
local getDeathGenerator = chunkPropertyUtils.getDeathGenerator
local addDeathGenerator = chunkPropertyUtils.addDeathGenerator
local decayDeathGenerator = chunkPropertyUtils.decayDeathGenerator
2017-08-08 01:19:51 -07:00
2019-02-10 22:14:17 -08:00
local linearInterpolation = mathUtils.linearInterpolation
-- module code
2017-01-19 21:58:36 -08:00
2018-09-25 22:14:13 -07:00
function pheromoneUtils.victoryScent(map, chunk, entityType)
2019-10-19 14:04:38 -07:00
local value = VICTORY_SCENT[entityType]
2017-11-20 23:27:03 -08:00
if value then
2019-10-19 14:04:38 -07:00
addDeathGenerator(map, chunk, -value)
2017-04-21 16:14:04 -07:00
end
end
2018-09-23 21:56:45 -07:00
function pheromoneUtils.deathScent(map, chunk)
2018-09-25 22:14:13 -07:00
addDeathGenerator(map, chunk, DEATH_PHEROMONE_GENERATOR_AMOUNT)
end
function pheromoneUtils.processStaticPheromone(map, chunk)
local chunkBase = -MAGIC_MAXIMUM_NUMBER
local chunkResource = -MAGIC_MAXIMUM_NUMBER
local chunkPathRating = getPathRating(map, chunk)
2019-02-10 22:14:17 -08:00
local clear = getEnemyStructureCount(map, chunk)
2019-03-05 22:18:03 -08:00
local tempNeighbors = getNeighborChunks(map, chunk.x, chunk.y)
local neighbor
local neighborPass
local chunkPass = getPassable(map, chunk)
local pheromone
if (chunkPass == CHUNK_ALL_DIRECTIONS) then
neighbor = tempNeighbors[2]
if (neighbor ~= -1) then
neighborPass = getPassable(map, neighbor)
if ((neighborPass == CHUNK_ALL_DIRECTIONS) or (neighborPass == CHUNK_NORTH_SOUTH)) then
pheromone = neighbor[BASE_PHEROMONE]
if chunkBase < pheromone then
chunkBase = pheromone
end
pheromone = neighbor[RESOURCE_PHEROMONE]
if chunkResource < pheromone then
chunkResource = pheromone
end
end
end
neighbor = tempNeighbors[7]
if (neighbor ~= -1) then
neighborPass = getPassable(map, neighbor)
if ((neighborPass == CHUNK_ALL_DIRECTIONS) or (neighborPass == CHUNK_NORTH_SOUTH)) then
pheromone = neighbor[BASE_PHEROMONE]
if chunkBase < pheromone then
chunkBase = pheromone
end
pheromone = neighbor[RESOURCE_PHEROMONE]
if chunkResource < pheromone then
chunkResource = pheromone
end
end
end
neighbor = tempNeighbors[4]
if (neighbor ~= -1) then
neighborPass = getPassable(map, neighbor)
if ((neighborPass == CHUNK_ALL_DIRECTIONS) or (neighborPass == CHUNK_EAST_WEST)) then
pheromone = neighbor[BASE_PHEROMONE]
if chunkBase < pheromone then
chunkBase = pheromone
end
pheromone = neighbor[RESOURCE_PHEROMONE]
if chunkResource < pheromone then
chunkResource = pheromone
end
end
end
neighbor = tempNeighbors[5]
if (neighbor ~= -1) then
neighborPass = getPassable(map, neighbor)
if ((neighborPass == CHUNK_ALL_DIRECTIONS) or (neighborPass == CHUNK_EAST_WEST)) then
pheromone = neighbor[BASE_PHEROMONE]
if chunkBase < pheromone then
chunkBase = pheromone
end
pheromone = neighbor[RESOURCE_PHEROMONE]
if chunkResource < pheromone then
chunkResource = pheromone
end
end
end
neighbor = tempNeighbors[1]
if (neighbor ~= -1) then
neighborPass = getPassable(map, neighbor)
if (neighborPass == CHUNK_ALL_DIRECTIONS) then
pheromone = neighbor[BASE_PHEROMONE]
if chunkBase < pheromone then
chunkBase = pheromone
end
pheromone = neighbor[RESOURCE_PHEROMONE]
if chunkResource < pheromone then
chunkResource = pheromone
end
end
end
neighbor = tempNeighbors[3]
if (neighbor ~= -1) then
neighborPass = getPassable(map, neighbor)
if (neighborPass == CHUNK_ALL_DIRECTIONS) then
pheromone = neighbor[BASE_PHEROMONE]
if chunkBase < pheromone then
chunkBase = pheromone
end
pheromone = neighbor[RESOURCE_PHEROMONE]
if chunkResource < pheromone then
chunkResource = pheromone
end
end
end
neighbor = tempNeighbors[6]
if (neighbor ~= -1) then
neighborPass = getPassable(map, neighbor)
if (neighborPass == CHUNK_ALL_DIRECTIONS) then
pheromone = neighbor[BASE_PHEROMONE]
if chunkBase < pheromone then
chunkBase = pheromone
end
pheromone = neighbor[RESOURCE_PHEROMONE]
if chunkResource < pheromone then
chunkResource = pheromone
end
end
end
neighbor = tempNeighbors[8]
if (neighbor ~= -1) then
neighborPass = getPassable(map, neighbor)
if (neighborPass == CHUNK_ALL_DIRECTIONS) then
pheromone = neighbor[BASE_PHEROMONE]
if chunkBase < pheromone then
chunkBase = pheromone
end
pheromone = neighbor[RESOURCE_PHEROMONE]
if chunkResource < pheromone then
chunkResource = pheromone
end
end
end
elseif (chunkPass == CHUNK_EAST_WEST) then
neighbor = tempNeighbors[4]
if (neighbor ~= -1) then
neighborPass = getPassable(map, neighbor)
if ((neighborPass == CHUNK_ALL_DIRECTIONS) or (neighborPass == CHUNK_EAST_WEST)) then
pheromone = neighbor[BASE_PHEROMONE]
if chunkBase < pheromone then
chunkBase = pheromone
end
pheromone = neighbor[RESOURCE_PHEROMONE]
if chunkResource < pheromone then
chunkResource = pheromone
end
end
end
2019-10-19 14:04:38 -07:00
neighbor = tempNeighbors[5]
if (neighbor ~= -1) then
neighborPass = getPassable(map, neighbor)
if ((neighborPass == CHUNK_ALL_DIRECTIONS) or (neighborPass == CHUNK_EAST_WEST)) then
pheromone = neighbor[BASE_PHEROMONE]
if chunkBase < pheromone then
chunkBase = pheromone
end
pheromone = neighbor[RESOURCE_PHEROMONE]
if chunkResource < pheromone then
chunkResource = pheromone
end
end
end
elseif (chunkPass == CHUNK_NORTH_SOUTH) then
neighbor = tempNeighbors[2]
if (neighbor ~= -1) then
neighborPass = getPassable(map, neighbor)
if ((neighborPass == CHUNK_ALL_DIRECTIONS) or (neighborPass == CHUNK_NORTH_SOUTH)) then
pheromone = neighbor[BASE_PHEROMONE]
if chunkBase < pheromone then
chunkBase = pheromone
end
pheromone = neighbor[RESOURCE_PHEROMONE]
if chunkResource < pheromone then
chunkResource = pheromone
end
end
end
neighbor = tempNeighbors[7]
if (neighbor ~= -1) then
neighborPass = getPassable(map, neighbor)
if ((neighborPass == CHUNK_ALL_DIRECTIONS) or (neighborPass == CHUNK_NORTH_SOUTH)) then
pheromone = neighbor[BASE_PHEROMONE]
if chunkBase < pheromone then
chunkBase = pheromone
end
pheromone = neighbor[RESOURCE_PHEROMONE]
if chunkResource < pheromone then
chunkResource = pheromone
end
end
end
end
chunkBase = chunkBase * 0.9
local pheromone = getPlayerBaseGenerator(map, chunk)
if chunkBase < pheromone then
chunk[BASE_PHEROMONE] = pheromone * chunkPathRating
else
chunk[BASE_PHEROMONE] = chunkBase * chunkPathRating
end
chunkResource = chunkResource * 0.9
local pheromone = getResourceGenerator(map, chunk)
if chunkResource < pheromone then
if clear then
chunk[RESOURCE_PHEROMONE] = pheromone * chunkPathRating
else
chunk[RESOURCE_PHEROMONE] = pheromone * chunkPathRating * 0.1
end
else
if clear then
chunk[RESOURCE_PHEROMONE] = chunkResource * chunkPathRating
else
chunk[RESOURCE_PHEROMONE] = chunkResource * chunkPathRating * 0.1
end
end
2018-10-19 22:17:37 -07:00
end
function pheromoneUtils.processPheromone(map, chunk)
local chunkMovement = -MAGIC_MAXIMUM_NUMBER
local chunkPlayer = -MAGIC_MAXIMUM_NUMBER
2018-09-23 21:56:45 -07:00
local chunkPathRating = getPathRating(map, chunk)
2018-02-12 23:10:17 -08:00
2018-10-19 22:17:37 -07:00
local tempNeighbors = getNeighborChunks(map, chunk.x, chunk.y)
2017-11-20 23:27:03 -08:00
2019-10-19 14:04:38 -07:00
local neighbor
local neighborPass
2019-10-19 12:13:48 -07:00
local chunkPass = getPassable(map, chunk)
local pheromone
2019-10-19 12:13:48 -07:00
if (chunkPass == CHUNK_ALL_DIRECTIONS) then
neighbor = tempNeighbors[2]
2020-05-19 19:37:16 -07:00
if (neighbor ~= -1) then
neighborPass = getPassable(map, neighbor)
if ((neighborPass == CHUNK_ALL_DIRECTIONS) or (neighborPass == CHUNK_NORTH_SOUTH)) then
pheromone = neighbor[MOVEMENT_PHEROMONE]
if chunkMovement < pheromone then
chunkMovement = pheromone
end
pheromone = neighbor[PLAYER_PHEROMONE]
if chunkPlayer < pheromone then
chunkPlayer = pheromone
end
2020-05-19 19:37:16 -07:00
end
2019-10-19 12:13:48 -07:00
end
2019-10-19 14:04:38 -07:00
neighbor = tempNeighbors[7]
if (neighbor ~= -1) then
2020-05-19 19:37:16 -07:00
neighborPass = getPassable(map, neighbor)
if ((neighborPass == CHUNK_ALL_DIRECTIONS) or (neighborPass == CHUNK_NORTH_SOUTH)) then
pheromone = neighbor[MOVEMENT_PHEROMONE]
if chunkMovement < pheromone then
chunkMovement = pheromone
end
pheromone = neighbor[PLAYER_PHEROMONE]
if chunkPlayer < pheromone then
chunkPlayer = pheromone
end
2020-05-19 19:37:16 -07:00
end
2019-10-19 12:13:48 -07:00
end
2019-10-19 14:04:38 -07:00
neighbor = tempNeighbors[4]
if (neighbor ~= -1) then
2020-05-19 19:37:16 -07:00
neighborPass = getPassable(map, neighbor)
if ((neighborPass == CHUNK_ALL_DIRECTIONS) or (neighborPass == CHUNK_EAST_WEST)) then
pheromone = neighbor[MOVEMENT_PHEROMONE]
if chunkMovement < pheromone then
chunkMovement = pheromone
end
pheromone = neighbor[PLAYER_PHEROMONE]
if chunkPlayer < pheromone then
chunkPlayer = pheromone
end
2020-05-19 19:37:16 -07:00
end
2019-10-19 12:13:48 -07:00
end
2019-10-19 14:04:38 -07:00
neighbor = tempNeighbors[5]
2020-05-19 19:37:16 -07:00
if (neighbor ~= -1) then
neighborPass = getPassable(map, neighbor)
if ((neighborPass == CHUNK_ALL_DIRECTIONS) or (neighborPass == CHUNK_EAST_WEST)) then
pheromone = neighbor[MOVEMENT_PHEROMONE]
if chunkMovement < pheromone then
chunkMovement = pheromone
end
pheromone = neighbor[PLAYER_PHEROMONE]
if chunkPlayer < pheromone then
chunkPlayer = pheromone
end
2020-05-19 19:37:16 -07:00
end
2019-10-19 12:13:48 -07:00
end
2019-10-19 14:04:38 -07:00
neighbor = tempNeighbors[1]
if (neighbor ~= -1) then
2020-05-19 19:37:16 -07:00
neighborPass = getPassable(map, neighbor)
if (neighborPass == CHUNK_ALL_DIRECTIONS) then
pheromone = neighbor[MOVEMENT_PHEROMONE]
if chunkMovement < pheromone then
chunkMovement = pheromone
end
pheromone = neighbor[PLAYER_PHEROMONE]
if chunkPlayer < pheromone then
chunkPlayer = pheromone
end
2020-05-19 19:37:16 -07:00
end
2019-10-19 12:13:48 -07:00
end
2019-10-19 14:04:38 -07:00
neighbor = tempNeighbors[3]
if (neighbor ~= -1) then
2020-05-19 19:37:16 -07:00
neighborPass = getPassable(map, neighbor)
if (neighborPass == CHUNK_ALL_DIRECTIONS) then
pheromone = neighbor[MOVEMENT_PHEROMONE]
if chunkMovement < pheromone then
chunkMovement = pheromone
end
pheromone = neighbor[PLAYER_PHEROMONE]
if chunkPlayer < pheromone then
chunkPlayer = pheromone
end
2020-05-19 19:37:16 -07:00
end
2019-10-19 12:13:48 -07:00
end
2019-10-19 14:04:38 -07:00
neighbor = tempNeighbors[6]
if (neighbor ~= -1) then
2020-05-19 19:37:16 -07:00
neighborPass = getPassable(map, neighbor)
if (neighborPass == CHUNK_ALL_DIRECTIONS) then
pheromone = neighbor[MOVEMENT_PHEROMONE]
if chunkMovement < pheromone then
chunkMovement = pheromone
end
pheromone = neighbor[PLAYER_PHEROMONE]
if chunkPlayer < pheromone then
chunkPlayer = pheromone
end
2020-05-19 19:37:16 -07:00
end
2019-10-19 12:13:48 -07:00
end
2019-10-19 14:04:38 -07:00
neighbor = tempNeighbors[8]
2020-05-19 19:37:16 -07:00
if (neighbor ~= -1) then
neighborPass = getPassable(map, neighbor)
if (neighborPass == CHUNK_ALL_DIRECTIONS) then
pheromone = neighbor[MOVEMENT_PHEROMONE]
if chunkMovement < pheromone then
chunkMovement = pheromone
end
pheromone = neighbor[PLAYER_PHEROMONE]
if chunkPlayer < pheromone then
chunkPlayer = pheromone
end
2020-05-19 19:37:16 -07:00
end
2019-10-19 12:13:48 -07:00
end
elseif (chunkPass == CHUNK_EAST_WEST) then
2019-10-19 14:04:38 -07:00
neighbor = tempNeighbors[4]
if (neighbor ~= -1) then
2020-05-19 19:37:16 -07:00
neighborPass = getPassable(map, neighbor)
if ((neighborPass == CHUNK_ALL_DIRECTIONS) or (neighborPass == CHUNK_EAST_WEST)) then
pheromone = neighbor[MOVEMENT_PHEROMONE]
if chunkMovement < pheromone then
chunkMovement = pheromone
end
pheromone = neighbor[PLAYER_PHEROMONE]
if chunkPlayer < pheromone then
chunkPlayer = pheromone
end
2020-05-19 19:37:16 -07:00
end
2019-10-19 12:13:48 -07:00
end
2019-10-19 14:04:38 -07:00
neighbor = tempNeighbors[5]
if (neighbor ~= -1) then
2020-05-19 19:37:16 -07:00
neighborPass = getPassable(map, neighbor)
if ((neighborPass == CHUNK_ALL_DIRECTIONS) or (neighborPass == CHUNK_EAST_WEST)) then
pheromone = neighbor[MOVEMENT_PHEROMONE]
if chunkMovement < pheromone then
chunkMovement = pheromone
end
pheromone = neighbor[PLAYER_PHEROMONE]
if chunkPlayer < pheromone then
chunkPlayer = pheromone
end
2020-05-19 19:37:16 -07:00
end
2019-10-19 12:13:48 -07:00
end
elseif (chunkPass == CHUNK_NORTH_SOUTH) then
2019-10-19 14:04:38 -07:00
neighbor = tempNeighbors[2]
if (neighbor ~= -1) then
2020-05-19 19:37:16 -07:00
neighborPass = getPassable(map, neighbor)
if ((neighborPass == CHUNK_ALL_DIRECTIONS) or (neighborPass == CHUNK_NORTH_SOUTH)) then
pheromone = neighbor[MOVEMENT_PHEROMONE]
if chunkMovement < pheromone then
chunkMovement = pheromone
end
pheromone = neighbor[PLAYER_PHEROMONE]
if chunkPlayer < pheromone then
chunkPlayer = pheromone
end
2020-05-19 19:37:16 -07:00
end
2019-10-19 12:13:48 -07:00
end
2019-10-19 14:04:38 -07:00
neighbor = tempNeighbors[7]
if (neighbor ~= -1) then
2020-05-19 19:37:16 -07:00
neighborPass = getPassable(map, neighbor)
if ((neighborPass == CHUNK_ALL_DIRECTIONS) or (neighborPass == CHUNK_NORTH_SOUTH)) then
pheromone = neighbor[MOVEMENT_PHEROMONE]
if chunkMovement < pheromone then
chunkMovement = pheromone
end
pheromone = neighbor[PLAYER_PHEROMONE]
if chunkPlayer < pheromone then
chunkPlayer = pheromone
end
2020-05-19 19:37:16 -07:00
end
2019-10-19 12:13:48 -07:00
end
2018-10-19 22:17:37 -07:00
end
2019-02-10 22:14:17 -08:00
chunkMovement = chunkMovement * 0.9
local pheromone = -getDeathGenerator(map, chunk)
if chunkMovement < pheromone then
chunk[MOVEMENT_PHEROMONE] = pheromone * chunkPathRating
else
chunk[MOVEMENT_PHEROMONE] = chunkMovement * chunkPathRating
2019-02-10 22:14:17 -08:00
end
decayDeathGenerator(map, chunk)
2019-02-10 22:14:17 -08:00
chunkPlayer = chunkPlayer * 0.9
local pheromone = getPlayersOnChunk(map, chunk) * PLAYER_PHEROMONE_GENERATOR_AMOUNT
if chunkPlayer < pheromone then
chunk[PLAYER_PHEROMONE] = pheromone * chunkPathRating
2018-02-12 23:10:17 -08:00
else
chunk[PLAYER_PHEROMONE] = chunkPlayer * chunkPathRating
2018-02-12 23:10:17 -08:00
end
end
2019-02-15 20:17:30 -08:00
pheromoneUtilsG = pheromoneUtils
return pheromoneUtils