From 663c4d59b93364c0be8c4b05fc0225db61d54963 Mon Sep 17 00:00:00 2001 From: Afforess Date: Sat, 7 Nov 2015 16:42:04 -0500 Subject: [PATCH] Update to remember previously attacked positions, de-value frequent targets --- control.lua | 1 + libs/biter_expansion.lua | 5 +++++ libs/map.lua | 26 +++++++++++++++----------- libs/region.lua | 33 +++++++++++++++++++++++++++++++++ remote.lua | 6 ++++++ 5 files changed, 60 insertions(+), 11 deletions(-) create mode 100644 remote.lua diff --git a/control.lua b/control.lua index bd1f880..82b5cab 100644 --- a/control.lua +++ b/control.lua @@ -1,5 +1,6 @@ require "defines" require "util" +require 'remote' require 'libs/EvoGUI' require 'libs/map' require 'libs/biter_expansion' diff --git a/libs/biter_expansion.lua b/libs/biter_expansion.lua index e51d997..10e97c0 100644 --- a/libs/biter_expansion.lua +++ b/libs/biter_expansion.lua @@ -23,6 +23,11 @@ function BiterExpansion.new(logger) end function self:tick() + if global.expansion_phase_request ~= nil then + self:set_expansion_state(self.expansion[global.expansion_phase_request.index]) + global.expansion_phase_request = nil + end + if global.expansion_timer > 0 then global.expansion_timer = global.expansion_timer - 1 diff --git a/libs/map.lua b/libs/map.lua index 3b20cd2..94347f9 100644 --- a/libs/map.lua +++ b/libs/map.lua @@ -84,10 +84,12 @@ function Map.new(logger) --self.l:log(region:tostring() .. " - Danger cache calculated: " .. region:getDangerCache():tostring()) end - local value = target_data.value + local value = target_data.value * 10000 local defenses = region:getDangerCache():getDanger(targets[i].position) + local attack_count = region:get_count_attack_on_position(targets[i].position) value = value / (1 + defenses) - self.l:log("Potential Target: " .. targets[i].name .. " at position " .. self.l:toString(targets[i].position) .. ". Base value: " .. target_data.value .. ". Defense level: " .. defenses .. ". Calculated value: " .. value .. ". Highest value: " .. highest_value) + value = value / (1 + attack_count) + self.l:log("Potential Target: " .. targets[i].name .. " at position " .. self.l:toString(targets[i].position) .. ". Base value: " .. target_data.value .. ". Defense level: " .. defenses .. ". Attack count: " .. attack_count .. ". Calculated value: " .. value .. ". Highest value: " .. highest_value) if value > highest_value then highest_value = value highest_value_entity = targets[i] @@ -98,8 +100,12 @@ function Map.new(logger) if highest_value_entity.type == "electric-pole" then self:trackPowerLine(highest_value_entity) end + self.l:log("Region attacked_positions: " .. self.l:toString(region.attacked_positions)) self.l:log("Highest value target: " .. highest_value_entity.name .. " at position " .. self.l:toString(highest_value_entity.position) .. ", with a value of " .. highest_value) highest_value_entity.surface.set_multi_command({command = {type=defines.command.attack, target=highest_value_entity, distraction=defines.distraction.none}, unit_count = math.random(10, 200) + 1, unit_search_distance = 256}) + region:mark_attack_position(highest_value_entity.position, self.l) + region:mark_attack_position(highest_value_entity.position, self.l) + return true else self.l:log("No valuable targets.") @@ -179,24 +185,22 @@ function Map.new(logger) function Map:iterateEnemyRegions() -- check and update enemy regions every 1 s in non-peaceful, and every 30s in peaceful - local frequency = 60 + local frequency = 30 if global.expansion_state == "peaceful" then frequency = 30 * 60 end if (game.tick % frequency == 0) then - - local region = global.enemyRegions:pop_front() - if region == nil then + local enemyRegion = global.enemyRegions:pop_front() + if enemyRegion == nil then self.l:log("No enemy regions found.") else - local enemyRegion = Region.byRegionCoords(region) if #enemyRegion:findEntities({"biter-spawner", "spitter-spawner"}) == 0 then -- enemy spawners have been destroyed self.l:log(enemyRegion:tostring() .. " no longer has enemy spawners. Removing from list of enemy regions.") else -- add back to end of linked list - global.enemyRegions:push_back(region) + global.enemyRegions:push_back(enemyRegion) if global.expansion_state ~= "Peaceful" then self:updateRegionAI(enemyRegion, false) @@ -213,7 +217,7 @@ function Map.new(logger) local region = self:nextRegion() if not self:isEnemyRegion(region) and #region:findEntities({"biter-spawner", "spitter-spawner"}) > 0 then - global.enemyRegions:push_back({x = region:getX(), y = region:getY()}) + global.enemyRegions:push_back(region) end self.l:log("Enemy Regions: " .. global.enemyRegions.length .. ". Queued regions: " .. #global.regionQueue .. ". Iterate region: " .. region:tostring() .. ". Enemy Spawners: " .. #region:findEntities({"biter-spawner", "spitter-spawner"}) .. ". Fully charted: ".. self.l:toString(region:isFullyCharted()) .. ". Partially charted: " .. self.l:toString(region:isPartiallyCharted())) @@ -270,8 +274,8 @@ function Map.new(logger) end function Map:isEnemyRegion(region) - for regionCoords in global.enemyRegions:iterate() do - if (regionCoords.x == region:getX() and regionCoords.y == region:getY()) then + for enemyRegion in global.enemyRegions:iterate() do + if (enemyRegion:getX() == region:getX() and enemyRegion:getY() == region:getY()) then return true end end diff --git a/libs/region.lua b/libs/region.lua index b14330d..f42d6b8 100644 --- a/libs/region.lua +++ b/libs/region.lua @@ -129,6 +129,37 @@ function Region:getDangerCache() return self.danger_cache end +function Region:mark_attack_position(pos, logger) + logger:log("mark_attack_position") + + x = math.floor(pos.x) + y = math.floor(pos.y) + found = false + for i = 1, #self.attacked_positions do + if self.attacked_positions[i].x == x and self.attacked_positions[i].y == y then + self.attacked_positions[i].count = self.attacked_positions[i].count + 1 + found = true + logger:log("Found position " .. logger:toString(self.attacked_positions[i]) .. " with count of " .. self.attacked_positions[i].count) + break + end + end + if not found then + logger:log("Adding position " .. logger:toString( { x = x, y = y, count = 1 } )) + table.insert(self.attacked_positions, { x = x, y = y, count = 1 }) + end +end + +function Region:get_count_attack_on_position(pos) + x = math.floor(pos.x) + y = math.floor(pos.y) + for i = 1, #self.attacked_positions do + if self.attacked_positions[i].x == x and self.attacked_positions[i].y == y then + return self.attacked_positions[i].count + end + end + return 0 +end + -- create region from factorio position function RegionClass.new(pos) local self = setmetatable({}, Region) @@ -142,6 +173,7 @@ function RegionClass.new(pos) self.y = self.y - MAX_UINT end self.danger_cache = DangerCache.new(self) + self.attacked_positions = {} return self end @@ -152,6 +184,7 @@ function RegionClass.byRegionCoords(regionCoords) self.x = math.floor(regionCoords.x) self.y = math.floor(regionCoords.y) self.danger_cache = DangerCache.new(self) + self.attacked_positions = {} return self end diff --git a/remote.lua b/remote.lua new file mode 100644 index 0000000..ca111c9 --- /dev/null +++ b/remote.lua @@ -0,0 +1,6 @@ +remote.add_interface("misanthrope", { + set_expansion_phase = function(index, target) + global.expansion_target_index = target + global.expansion_phase_request = { index = index } + end +})