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

109 lines
4.1 KiB
Lua
Raw Normal View History

local pheromoneUtils = {}
-- imports
local mapUtils = require("MapUtils")
local constants = require("Constants")
-- constants
local DEATH_PHEROMONE = constants.DEATH_PHEROMONE
local PLAYER_DEFENSE_PHEROMONE = constants.PLAYER_DEFENSE_PHEROMONE
local PLAYER_BASE_PHEROMONE = constants.PLAYER_BASE_PHEROMONE
local ENEMY_BASE_PHEROMONE = constants.ENEMY_BASE_PHEROMONE
local PLAYER_PHEROMONE = constants.PLAYER_PHEROMONE
local PLAYER_DEFENSE_GENERATOR = constants.PLAYER_DEFENSE_GENERATOR
local PLAYER_BASE_GENERATOR = constants.PLAYER_BASE_GENERATOR
local ENEMY_BASE_GENERATOR = constants.ENEMY_BASE_GENERATOR
local PLAYER_PHEROMONE_GENERATOR_AMOUNT = constants.PLAYER_PHEROMONE_GENERATOR_AMOUNT
2016-08-21 14:48:55 -07:00
local DEATH_PHEROMONE_GENERATOR_AMOUNT = constants.DEATH_PHEROMONE_GENERATOR_AMOUNT
local STANDARD_PHERONOME_DIFFUSION_AMOUNT = constants.STANDARD_PHERONOME_DIFFUSION_AMOUNT
local DEATH_PHEROMONE_DIFFUSION_AMOUNT = constants.DEATH_PHEROMONE_DIFFUSION_AMOUNT
2016-08-21 14:48:55 -07:00
local DEATH_PHEROMONE_PERSISTANCE = constants.DEATH_PHEROMONE_PERSISTANCE
local STANDARD_PHEROMONE_PERSISTANCE = constants.STANDARD_PHEROMONE_PERSISTANCE
-- imported functions
local getChunkByPosition = mapUtils.getChunkByPosition
-- module code
2016-08-21 14:48:55 -07:00
function pheromoneUtils.scents(chunk)
2016-08-21 14:48:55 -07:00
local amount = chunk[PLAYER_DEFENSE_GENERATOR]
if (amount > 0) then
chunk[PLAYER_DEFENSE_PHEROMONE] = chunk[PLAYER_DEFENSE_PHEROMONE] + amount
end
2016-08-06 20:38:47 -07:00
2016-08-21 14:48:55 -07:00
amount = chunk[PLAYER_BASE_GENERATOR]
if (amount > 0) then
chunk[PLAYER_BASE_PHEROMONE] = chunk[PLAYER_BASE_PHEROMONE] + amount
end
2016-08-21 14:48:55 -07:00
amount = chunk[ENEMY_BASE_GENERATOR]
if (amount > 0) then
chunk[ENEMY_BASE_PHEROMONE] = chunk[ENEMY_BASE_PHEROMONE] + amount
end
end
2016-08-21 14:48:55 -07:00
2016-08-28 17:05:28 -07:00
function pheromoneUtils.deathScent(regionMap, position, pheromoneTotals)
local chunk = getChunkByPosition(regionMap, position.x, position.y)
2016-08-21 14:48:55 -07:00
if (chunk ~= nil) then
chunk[DEATH_PHEROMONE] = chunk[DEATH_PHEROMONE] + DEATH_PHEROMONE_GENERATOR_AMOUNT
pheromoneTotals[DEATH_PHEROMONE] = pheromoneTotals[DEATH_PHEROMONE] + DEATH_PHEROMONE_GENERATOR_AMOUNT
end
end
function pheromoneUtils.playerScent(regionMap, players)
2016-08-25 15:20:06 -07:00
for i=1,#players do
local player = players[i]
2016-08-27 17:57:20 -07:00
if (player ~= nil) and player.connected and (player.character ~= nil) and player.character.valid and (player.character.surface.index == 1) then
local playerPosition = player.character.position
local playerChunk = getChunkByPosition(regionMap, playerPosition.x, playerPosition.y)
if (playerChunk ~= nil) then
playerChunk[PLAYER_PHEROMONE] = playerChunk[PLAYER_PHEROMONE] + PLAYER_PHEROMONE_GENERATOR_AMOUNT
end
end
end
end
function pheromoneUtils.processPheromone(chunk, neighbors)
-- pheromone level indexes on chunks are 1 - 6
-- unrolled loop one level
local diffusionAmount = DEATH_PHEROMONE_DIFFUSION_AMOUNT
local persistence = DEATH_PHEROMONE_PERSISTANCE
local totalDiffused = 0
local chunkValue = chunk[DEATH_PHEROMONE] * persistence
local diffusedAmount = chunkValue * diffusionAmount
for i=1,#neighbors do
local neighborChunk = neighbors[i]
if (neighborChunk ~= nil) then
totalDiffused = totalDiffused + diffusedAmount
neighborChunk[DEATH_PHEROMONE] = neighborChunk[DEATH_PHEROMONE] + diffusedAmount
2016-08-06 20:38:47 -07:00
end
end
chunk[DEATH_PHEROMONE] = (chunkValue - totalDiffused)
diffusionAmount = STANDARD_PHERONOME_DIFFUSION_AMOUNT
persistence = STANDARD_PHEROMONE_PERSISTANCE
for x=2,6 do
local totalDiffused = 0
2016-08-28 17:05:28 -07:00
local chunkValue = chunk[x] * persistence
local diffusedAmount = chunkValue * diffusionAmount
2016-08-25 15:20:06 -07:00
for i=1,#neighbors do
local neighborChunk = neighbors[i]
if (neighborChunk ~= nil) then
totalDiffused = totalDiffused + diffusedAmount
neighborChunk[x] = neighborChunk[x] + diffusedAmount
end
end
2016-08-28 17:05:28 -07:00
chunk[x] = (chunkValue - totalDiffused)
end
end
return pheromoneUtils