From 039bfa10d1fc7a07fd5aed4d06225d996a6cec01 Mon Sep 17 00:00:00 2001 From: veden Date: Mon, 12 Sep 2016 15:33:00 -0700 Subject: [PATCH] benchmarking --- control.lua | 80 ++++++++++++++++++++--------------------- libs/AIBuilding.lua | 2 +- libs/UnitGroupUtils.lua | 8 +++-- 3 files changed, 46 insertions(+), 44 deletions(-) diff --git a/control.lua b/control.lua index f6adb09..a1aad92 100644 --- a/control.lua +++ b/control.lua @@ -21,7 +21,7 @@ local scanMap = mapProcessor.scanMap local accumulatePoints = aiBuilding.accumulatePoints local removeScout = aiBuilding.removeScout -local scouting = aiBuilding.scouting +-- local scouting = aiBuilding.scouting local playerScent = pheromoneUtils.playerScent local deathScent = pheromoneUtils.deathScent @@ -46,24 +46,7 @@ local temps -- hook functions -function onInit() - -- print("init") - global.regionMap = {} - 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 - -function onLoad() +local function onLoad() -- print("load") regionMap = global.regionMap natives = global.natives @@ -72,7 +55,15 @@ function onLoad() temps = global.temps end -function onConfigChanged() +local function onChunkGenerated(event) + -- queue generated chunk for delayed processing, queuing is required because some mods (RSO) mess with chunk as they + -- are generated, which messes up the scoring. + if (event.surface.index == 1) then + pendingChunks[#pendingChunks+1] = event + end +end + +local function onConfigChanged() -- print("reprocess") if (global.version == nil) then @@ -122,18 +113,13 @@ function onConfigChanged() end end -function onChunkGenerated(event) - -- queue generated chunk for delayed processing, queuing is required because some mods (RSO) mess with chunk as they - -- are generated, which messes up the scoring. - if (event.surface.index == 1) then - pendingChunks[#pendingChunks+1] = event - end -end - -function onTick(event) +local function onTick(event) if (event.tick % 20 == 0) then - local surface = game.surfaces[1] + local surface = game.surfaces[1] + processPendingChunks(regionMap, surface, pendingChunks) + scanMap(regionMap, surface) + if (event.tick % 40 == 0) then accumulatePoints(natives) @@ -148,24 +134,20 @@ function onTick(event) squadBeginAttack(natives, game.players, game.evolution_factor) squadAttack(regionMap, surface, natives, temps) end - - processPendingChunks(regionMap, surface, pendingChunks) - - -- scanMap(regionMap, surface) - - processMap(regionMap, surface, natives, game.evolution_factor, temps) + + processMap(regionMap, surface, natives, game.evolution_factor, temps) end end -function onBuild(event) +local function onBuild(event) addRemoveEntity(regionMap, event.created_entity, natives, true) end -function onPickUp(event) +local function onPickUp(event) addRemoveEntity(regionMap, event.entity, natives, false) end -function onDeath(event) +local function onDeath(event) local entity = event.entity local surface = entity.surface if (surface.index == 1) then @@ -196,13 +178,31 @@ function onDeath(event) end end -function onPutItem(event) +local function onPutItem(event) -- local player = game.players[event.player_index] -- if (player.surface.index==1) then -- aiBuilding.fillTunnel(global.regionMap, player.surface, global.natives, event.positions) -- end end +local function onInit() + -- print("init") + global.regionMap = {} + 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 + + -- hooks script.on_init(onInit) diff --git a/libs/AIBuilding.lua b/libs/AIBuilding.lua index f11133e..d672ea1 100644 --- a/libs/AIBuilding.lua +++ b/libs/AIBuilding.lua @@ -113,7 +113,7 @@ end function aiBuilding.formSquads(regionMap, surface, natives, chunk, evolution_factor, temps) 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 > 20) and (chunk[ENEMY_BASE_GENERATOR] ~= 0) and (#natives.squads < (AI_MAX_SQUAD_COUNT * evolution_factor)) and (math.random() < 0.03) then + 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 squadPath, squadScore = scoreNeighbors(chunk, diff --git a/libs/UnitGroupUtils.lua b/libs/UnitGroupUtils.lua index 4163aeb..b7899dd 100644 --- a/libs/UnitGroupUtils.lua +++ b/libs/UnitGroupUtils.lua @@ -53,7 +53,7 @@ function unitGroupUtils.membersToSquad(squad, members, overwriteGroup, temps) 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(groupCmd) end end end @@ -120,7 +120,7 @@ function unitGroupUtils.regroupSquads(natives, temps) unitGroupUtils.membersToSquad(squad, mergeGroup.members, true, temps) mergeGroup.destroy() end - end + end end end @@ -133,15 +133,17 @@ function unitGroupUtils.regroupSquads(natives, temps) elseif (#squad.group.members == 0) then squad.group.destroy() tableRemove(squads, i) - else + else if (squad.status == SQUAD_RETREATING) and ((squad.cycles == 0) or (squad.group.state == GROUP_STATE_FINISHED)) then squad.status = SQUAD_GUARDING squad.cycles = 0 elseif (squad.cycles > 0) then squad.cycles = squad.cycles - 1 end + end end end + return unitGroupUtils