Skip to content
This repository has been archived by the owner on Nov 21, 2017. It is now read-only.

Commit

Permalink
Update to remember previously attacked positions, de-value frequent t…
Browse files Browse the repository at this point in the history
…argets
  • Loading branch information
Afforess committed Nov 7, 2015
1 parent 9081ca6 commit 663c4d5
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 11 deletions.
1 change: 1 addition & 0 deletions control.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require "defines"
require "util"
require 'remote'
require 'libs/EvoGUI'
require 'libs/map'
require 'libs/biter_expansion'
Expand Down
5 changes: 5 additions & 0 deletions libs/biter_expansion.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
26 changes: 15 additions & 11 deletions libs/map.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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.")
Expand Down Expand Up @@ -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)
Expand All @@ -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()))
Expand Down Expand Up @@ -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
Expand Down
33 changes: 33 additions & 0 deletions libs/region.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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
Expand Down
6 changes: 6 additions & 0 deletions remote.lua
Original file line number Diff line number Diff line change
@@ -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
})

0 comments on commit 663c4d5

Please sign in to comment.