1
0
mirror of https://github.com/veden/Rampant.git synced 2024-12-30 21:19:46 +02:00

Going without the temps in global, no real difference for a loss of clarity

This commit is contained in:
veden 2016-09-12 16:23:26 -07:00
parent 039bfa10d1
commit 9e2d97cba6
8 changed files with 41 additions and 96 deletions

View File

@ -1,6 +1,5 @@
-- imports
local setupUtils = require("setupUtils")
local entityUtils = require("libs/EntityUtils")
local unitGroupUtils = require("libs/UnitGroupUtils")
local chunkProcessor = require("libs/ChunkProcessor")
@ -42,7 +41,6 @@ local regionMap
local natives
local pheromoneTotals
local pendingChunks
local temps
-- hook functions
@ -52,7 +50,6 @@ local function onLoad()
natives = global.natives
pendingChunks = global.pendingChunks
pheromoneTotals = global.pheromoneTotals
temps = global.temps
end
local function onChunkGenerated(event)
@ -93,8 +90,6 @@ local function onConfigChanged()
regionMap.scanPointer = 1
regionMap.processRoll = -1
setupUtils.initTemps(temps)
pheromoneTotals[constants.DEATH_PHEROMONE] = 0
pheromoneTotals[constants.ENEMY_BASE_PHEROMONE] = 0
pheromoneTotals[constants.PLAYER_PHEROMONE] = 0
@ -127,15 +122,15 @@ local function onTick(event)
-- put down player pheromone for player hunters
playerScent(regionMap, game.players)
regroupSquads(natives, temps)
regroupSquads(natives)
-- scouting(regionMap, natives)
squadBeginAttack(natives, game.players, game.evolution_factor)
squadAttack(regionMap, surface, natives, temps)
squadAttack(regionMap, surface, natives)
end
processMap(regionMap, surface, natives, game.evolution_factor, temps)
processMap(regionMap, surface, natives, game.evolution_factor)
end
end
@ -164,8 +159,7 @@ local function onDeath(event)
entity.unit_group),
regionMap,
surface,
natives,
temps)
natives)
end
removeScout(entity, global.natives)
@ -178,7 +172,7 @@ local function onDeath(event)
end
end
local function onPutItem(event)
local function onSurfaceTileChange(event)
-- local player = game.players[event.player_index]
-- if (player.surface.index==1) then
-- aiBuilding.fillTunnel(global.regionMap, player.surface, global.natives, event.positions)
@ -191,13 +185,11 @@ local function onInit()
global.pendingChunks = {}
global.natives = {}
global.pheromoneTotals = {}
global.temps = {}
regionMap = global.regionMap
natives = global.natives
pendingChunks = global.pendingChunks
pheromoneTotals = global.pheromoneTotals
temps = global.temps
onConfigChanged()
end
@ -209,7 +201,7 @@ script.on_init(onInit)
script.on_load(onLoad)
script.on_configuration_changed(onConfigChanged)
script.on_event(defines.events.on_player_built_tile, onPutItem)
script.on_event(defines.events.on_player_built_tile, onSurfaceTileChange)
script.on_event({defines.events.on_preplayer_mined_item,
defines.events.on_robot_pre_mined},

View File

@ -68,9 +68,6 @@ end
function aiAttack.squadAttack(regionMap, surface, natives, temps)
local squads = natives.squads
local attackPosition = temps[constants.ATTACK_POSITION]
local cmd = temps[constants.ATTACK_COMMAND]
local neighborsWithDirection = temps[constants.ATTACK_DIRECTION]
for i=1,#squads do
local squad = squads[i]
local group = squad.group
@ -88,10 +85,10 @@ function aiAttack.squadAttack(regionMap, surface, natives, temps)
if (group.state == defines.group_state.finished) or (group.state == defines.group_state.gathering) then
local chunk = getChunkByPosition(regionMap, group.position.x, group.position.y)
if (chunk ~= nil) then
local attackPosition = {x=0, y=0}
addSquadMovementPenalty(squad, chunk.cX, chunk.cY)
getCardinalChunksWithDirection(regionMap, chunk.cX, chunk.cY, neighborsWithDirection)
local attackChunk, attackDirection = scoreNeighborsWithDirection(chunk,
neighborsWithDirection,
getCardinalChunksWithDirection(regionMap, chunk.cX, chunk.cY),
validLocation,
scoreLocation,
squad,
@ -103,7 +100,10 @@ function aiAttack.squadAttack(regionMap, surface, natives, temps)
positionFromDirectionAndChunkCardinal(attackDirection, attackChunk, attackPosition)
group.set_command(cmd)
group.set_command({ type = defines.command.attack_area,
destination = attackPosition,
radius = 32,
distraction = defines.distraction.by_anything })
group.start_moving()
end
end

View File

@ -110,11 +110,11 @@ function aiBuilding.scouting(regionMap, natives)
--]]
end
function aiBuilding.formSquads(regionMap, surface, natives, chunk, evolution_factor, temps)
function aiBuilding.formSquads(regionMap, surface, natives, chunk, evolution_factor)
if (natives.points > AI_SQUAD_COST) then
local score = chunk[PLAYER_BASE_PHEROMONE] + chunk[PLAYER_PHEROMONE] + chunk[PLAYER_DEFENSE_PHEROMONE] + surface.get_pollution({chunk.pX, chunk.pY})
if (score > 70) and (chunk[ENEMY_BASE_GENERATOR] ~= 0) and (#natives.squads < (AI_MAX_SQUAD_COUNT * evolution_factor)) and (math.random() < 0.03) then
local squadPosition = temps[constants.SQUAD_POSITION]
local squadPosition = {x=0, y=0}
local squadPath, squadScore = scoreNeighbors(chunk,
getNeighborChunks(regionMap, chunk.cX, chunk.cY),
@ -127,14 +127,13 @@ function aiBuilding.formSquads(regionMap, surface, natives, chunk, evolution_fac
squadPosition.x = squadPath.pX + HALF_CHUNK_SIZE
squadPosition.y = squadPath.pY + HALF_CHUNK_SIZE
local multiGroupCmd = temps[constants.MULTI_GROUP_COMMAND]
local groupCmd = temps[constants.GROUP_COMMAND]
local squad = createSquad(squadPosition, surface, natives)
multiGroupCmd.unit_count = evolution_factor * AI_MAX_SQUAD_SIZE
groupCmd.group = squad.group
local foundUnits = surface.set_multi_command(multiGroupCmd)
local foundUnits = surface.set_multi_command({ command = { type = defines.command.group,
group = squad.group,
distraction = defines.distraction.none },
unit_count = evolution_factor * AI_MAX_SQUAD_SIZE,
unit_search_distance = (constants.CHUNK_SIZE * 2)})
if (foundUnits > 0) then
natives.points = natives.points - AI_SQUAD_COST
end

View File

@ -50,7 +50,7 @@ local function scoreRetreatLocation(position, squad, neighborChunk, surface)
return safeScore - dangerScore
end
function aiDefense.retreatUnits(position, squad, regionMap, surface, natives, temps)
function aiDefense.retreatUnits(position, squad, regionMap, surface, natives)
local chunk = getChunkByPosition(regionMap, position.x, position.y)
if (chunk ~= nil) and (chunk[DEATH_PHEROMONE] > (game.evolution_factor * RETREAT_DEATH_PHEROMONE_LEVEL)) then
local performRetreat = false
@ -68,12 +68,9 @@ function aiDefense.retreatUnits(position, squad, regionMap, surface, natives, te
end
if performRetreat then
local retreatPosition = temps[constants.RETREAT_POSITION]
local retreatNeighborsWithDirection = temps[constants.RETREAT_NEIGHBORS_WITH_DIRECTION]
getNeighborChunksWithDirection(regionMap, chunk.cX, chunk.cY, retreatNeighborsWithDirection)
local retreatPosition = {x=0, y=0}
local exitPath,_ = scoreNeighborsWithDirection(chunk,
retreatNeighborsWithDirection,
getNeighborChunksWithDirection(regionMap, chunk.cX, chunk.cY),
validRetreatLocation,
scoreRetreatLocation,
nil,
@ -97,9 +94,9 @@ function aiDefense.retreatUnits(position, squad, regionMap, surface, natives, te
end
if (enemiesToSquad ~= nil) then
membersToSquad(newSquad, enemiesToSquad, false, temps)
membersToSquad(newSquad, enemiesToSquad, false)
else
membersToSquad(newSquad, squad.group.members, true, temps)
membersToSquad(newSquad, squad.group.members, true)
end
end
end

View File

@ -81,8 +81,8 @@ function mapUtils.getNeighborChunks(regionMap, chunkX, chunkY)
return neighbors
end
function mapUtils.getNeighborChunksWithDirection(regionMap, chunkX, chunkY, neighbors)
-- local neighbors = {{d=1},{d=2},{d=3},{d=4},{d=5},{d=6},{d=7},{d=8}}
function mapUtils.getNeighborChunksWithDirection(regionMap, chunkX, chunkY)
local neighbors = {{d=1},{d=2},{d=3},{d=4},{d=5},{d=6},{d=7},{d=8}}
local xChunks = regionMap[chunkX-1]
if (xChunks ~= nil) then
neighbors[1].c = xChunks[chunkY-1]
@ -112,8 +112,8 @@ end
|
4
]]--
function mapUtils.getCardinalChunksWithDirection(regionMap, chunkX, chunkY, neighbors)
-- local neighbors = {{d=1},{d=2},{d=3},{d=4}}
function mapUtils.getCardinalChunksWithDirection(regionMap, chunkX, chunkY)
local neighbors = {{d=1},{d=2},{d=3},{d=4}}
local xChunks = regionMap[chunkX]
if (xChunks ~= nil) then
neighbors[1].c = xChunks[chunkY-1]
@ -129,7 +129,7 @@ function mapUtils.getCardinalChunksWithDirection(regionMap, chunkX, chunkY, neig
if (xChunks ~= nil) then
neighbors[3].c = xChunks[chunkY]
end
-- return neighbors
return neighbors
end
function mapUtils.getCardinalChunks(regionMap, chunkX, chunkY)
@ -236,4 +236,4 @@ function mapUtils.positionFromDirectionAndChunk(direction, chunk)
return position
end
return mapUtils
return mapUtils

View File

@ -92,9 +92,9 @@ function pheromoneUtils.processPheromone(chunk, neighbors)
diffusionAmount = STANDARD_PHERONOME_DIFFUSION_AMOUNT
persistence = STANDARD_PHEROMONE_PERSISTANCE
for x=2,6 do
local totalDiffused = 0
local chunkValue = chunk[x] * persistence
local diffusedAmount = chunkValue * diffusionAmount
totalDiffused = 0
chunkValue = chunk[x] * persistence
diffusedAmount = chunkValue * diffusionAmount
for i=1,#neighbors do
local neighborChunk = neighbors[i]
if (neighborChunk ~= nil) then
@ -106,4 +106,4 @@ function pheromoneUtils.processPheromone(chunk, neighbors)
end
end
return pheromoneUtils
return pheromoneUtils

View File

@ -46,14 +46,14 @@ function unitGroupUtils.createSquad(position, surface, natives)
return squad
end
function unitGroupUtils.membersToSquad(squad, members, overwriteGroup, temps)
function unitGroupUtils.membersToSquad(squad, members, overwriteGroup)
if (members ~= nil) then
local groupCmd = temps[constants.GROUP_COMMAND]
groupCmd.group = squad.group
for i=1,#members do
for i=1,#members do
local member = members[i]
if member.valid and (overwriteGroup or (not overwriteGroup and (member.unit_group == nil))) then
member.set_command(groupCmd)
member.set_command({ type = defines.command.group,
group = squad.group,
distraction = defines.distraction.by_anything })
end
end
end
@ -107,7 +107,7 @@ function unitGroupUtils.lookupSquadMovementPenalty(squad, chunkX, chunkY)
return 0
end
function unitGroupUtils.regroupSquads(natives, temps)
function unitGroupUtils.regroupSquads(natives)
local squads = natives.squads
for i=1,#squads do
local squad = squads[i]
@ -117,7 +117,7 @@ function unitGroupUtils.regroupSquads(natives, temps)
local mergeSquad = squads[x]
local mergeGroup = mergeSquad.group
if mergeGroup.valid and (mergeSquad.status == squad.status) and (euclideanDistanceNamed(squadPosition, mergeGroup.position) < 16) then
unitGroupUtils.membersToSquad(squad, mergeGroup.members, true, temps)
unitGroupUtils.membersToSquad(squad, mergeGroup.members, true)
mergeGroup.destroy()
end
end

View File

@ -1,43 +0,0 @@
local setupUtils = {}
-- imports
local constants = require("libs/Constants")
-- module code
function setupUtils.initTemps(temps)
-- minimize garbage by preallocating all tables needed for operation
if (temps[constants.ATTACK_COMMAND] == nil) then
local attackPosition = {x=0, y=0}
temps[constants.ATTACK_POSITION] = attackPosition
temps[constants.ATTACK_COMMAND] = { type = defines.command.attack_area,
destination = attackPosition,
radius = 32,
distraction = defines.distraction.by_anything }
temps[constants.ATTACK_DIRECTION] = {{d=1}, {d=2}, {d=3}, {d=4}}
local groupCmd = { type = defines.command.group,
group = 1,
distraction = defines.distraction.none }
temps[constants.GROUP_COMMAND] = groupCmd
temps[constants.SQUAD_POSITION] = {x=0, y=0}
local retreatPosition = {x=0, y=0}
temps[constants.RETREAT_POSITION] = retreatPosition
temps[constants.RETREAT_NEIGHBORS_WITH_DIRECTION] = {{d=1},{d=2},{d=3},{d=4},{d=5},{d=6},{d=7},{d=8}}
-- temps[constants.RETREAT_COMMAND] = { type = defines.command.go_to_location,
-- destination = retreatPosition,
-- distraction = defines.distraction.none }
temps[constants.MULTI_GROUP_COMMAND] = {command = groupCmd,
unit_count = 1,
unit_search_distance = (constants.CHUNK_SIZE*2) }
end
end
return setupUtils