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

98 lines
3.5 KiB
Lua
Raw Normal View History

local pheromoneUtils = {}
-- imports
local mapUtils = require("MapUtils")
local constants = require("Constants")
-- constants
local CHUNK_SIZE = constants.CHUNK_SIZE
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
2016-08-21 14:48:55 -07:00
local mFloor = math.floor
-- module code
2016-08-21 14:48:55 -07:00
function pheromoneUtils.scents(regionMap, surface, natives, chunk, neighbors, evolution_factor)
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
function pheromoneUtils.deathScent(regionMap, surface, x, y)
local chunk = getChunkByPosition(regionMap, x, y)
if (chunk ~= nil) then
chunk[DEATH_PHEROMONE] = chunk[DEATH_PHEROMONE] + DEATH_PHEROMONE_GENERATOR_AMOUNT
end
end
function pheromoneUtils.playerScent(regionMap, players)
for i=1, #players do
local playerPosition = players[i].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
function pheromoneUtils.processPheromone(regionMap, surface, natives, chunk, neighbors, evolution_factor)
for x=1,6 do
2016-08-06 20:38:47 -07:00
local diffusionAmount
local persistence
2016-08-06 20:38:47 -07:00
if (x == DEATH_PHEROMONE) then
diffusionAmount = DEATH_PHEROMONE_DIFFUSION_AMOUNT
2016-08-21 14:48:55 -07:00
persistence = DEATH_PHEROMONE_PERSISTANCE
2016-08-06 20:38:47 -07:00
else
diffusionAmount = STANDARD_PHERONOME_DIFFUSION_AMOUNT
2016-08-21 14:48:55 -07:00
persistence = STANDARD_PHEROMONE_PERSISTANCE
2016-08-06 20:38:47 -07:00
end
local totalDiffused = 0
2016-08-21 14:48:55 -07:00
for i=1,#neighbors do
local neighborChunk = neighbors[i]
if (neighborChunk ~= nil) then
local diffusedAmount = (chunk[x] * diffusionAmount)
totalDiffused = totalDiffused + diffusedAmount
neighborChunk[x] = neighborChunk[x] + diffusedAmount
end
end
chunk[x] = chunk[x] - totalDiffused
chunk[x] = chunk[x] * persistence
end
end
return pheromoneUtils