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

Commit c2afe14

Browse files
committed
Improve the targetting system and fix initial game starts
1 parent 335e96c commit c2afe14

File tree

5 files changed

+48
-28
lines changed

5 files changed

+48
-28
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
PACKAGE_NAME := Misanthrope
2-
VERSION_STRING := 0.4.0
2+
VERSION_STRING := 0.4.1
33

44
OUTPUT_NAME := $(PACKAGE_NAME)_$(VERSION_STRING)
55
OUTPUT_DIR := build/$(OUTPUT_NAME)

libs/biter/ai/attack_area.lua

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,19 @@ AttackArea.stages.spawning = function(base, data)
3030
end
3131

3232
AttackArea.stages.plan_attack = function(base, data)
33-
local end_pos = Area.center(Chunk.to_area(base.target.chunk_pos))
33+
local candidates = base.targets.candidates
34+
if #candidates == 0 then
35+
base.targets = nil
36+
return 'fail'
37+
end
38+
local idx = math.random(#candidates)
39+
local candidate = table.remove(candidates, idx)
40+
Log("Attack candidate: %s", base, serpent.line(candidate))
41+
if #candidates == 0 then
42+
base.targets = nil
43+
end
44+
45+
local end_pos = Area.center(Chunk.to_area(candidate.chunk_pos))
3446
local command = {type = defines.command.attack_area, destination = end_pos, radius = 16}
3547
local entities = table.filter(base.entities, Game.VALID_FILTER)
3648

@@ -62,6 +74,9 @@ function AttackArea.tick(base, data)
6274
end
6375

6476
function AttackArea.is_expired(base, data)
77+
if data.stage == 'fail' or data.stage == 'success' then
78+
return true
79+
end
6580
return data.attack_group and (not data.attack_group.valid or game.tick > data.attack_tick + Time.MINUTE * 6)
6681
end
6782

libs/biter/ai/identify_targets.lua

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ local Log = function(str, ...) BiterBase.LogAI("[IdentifyTargets] " .. str, ...)
55

66
IdentifyTargets.stages.setup = function(base, data)
77
local chunk_pos = Chunk.from_position(base.queen.position)
8-
local search_area = Position.expand_to_area(chunk_pos, 15)
8+
local search_area = Position.expand_to_area(chunk_pos, 20)
99
data.start_chunk = chunk_pos
1010
data.search_queue = {}
1111
data.search_idx = 1
@@ -28,8 +28,8 @@ IdentifyTargets.stages.search = function(base, data)
2828
if chunk_data and chunk_data.player_value and chunk_data.player_value > 0 then
2929
local dist = Position.manhattan_distance(chunk_pos, data.start_chunk)
3030

31-
value = chunk_data.player_value / ((1 + dist) * (1 + dist))
32-
table.insert(data.candidates, { chunk_pos = chunk_pos, data = chunk_data, value = value})
31+
value = (chunk_data.player_value * chunk_data.player_value) / ((1 + dist) * (1 + dist))
32+
table.insert(data.candidates, { chunk_pos = chunk_pos, value = value})
3333
end
3434

3535
data.search_idx = data.search_idx + 1
@@ -46,15 +46,16 @@ IdentifyTargets.stages.sort = function(base, data)
4646
end)
4747

4848
Log("All candidates: %s", base, serpent.block(data.candidates))
49+
data.candidates = table.filter(data.candidates, function(candidate)
50+
return candidate.value > 100
51+
end)
52+
if #data.candidates == 0 then
53+
Log("No candidates, unable to identify any valuable targets.", base)
54+
return 'fail'
55+
end
56+
Log("Filtered candidates: %s", base, serpent.block(data.candidates))
4957

50-
return 'decide'
51-
end
52-
53-
IdentifyTargets.stages.decide = function(base, data)
54-
local idx = math.random(#data.candidates)
55-
local choosen_candidate = data.candidates[idx]
56-
Log("Randomly chosen candidate was %s", base, serpent.line(choosen_candidate))
57-
base.target = { type = 'player_value', chunk_pos = choosen_candidate.chunk_pos, tick = game.tick}
58+
base.targets = { candidates = data.candidates, tick = game.tick }
5859
return 'success'
5960
end
6061

libs/biter/base.lua

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -219,16 +219,13 @@ function BiterBase.create_plan(base)
219219
LogAI("Current Number of Worms in Base: %d", base, #base.worms)
220220

221221
if base:can_afford('identify_targets') then
222-
if not base.target then
222+
if not base.targets then
223223
LogAI("No active targets, chooses AI plan to identify targets", base)
224224
BiterBase.set_active_plan(base, 'identify_targets')
225225
return true
226226
end
227227

228-
if not base.target.tick then
229-
base.target.tick = game.tick
230-
end
231-
local age = game.tick - base.target.tick
228+
local age = game.tick - base.targets.tick
232229
if math.random(200) < (age / Time.MINUTE) then
233230
LogAI("Recalculating targets, previous target is %s minutes old", base, serpent.line((age / Time.MINUTE)))
234231
BiterBase.set_active_plan(base, 'identify_targets')
@@ -256,7 +253,7 @@ function BiterBase.create_plan(base)
256253
return true
257254
end
258255

259-
if base:can_afford('attack_area') and base.target and base.target.type == 'player_value' then
256+
if base:can_afford('attack_area') and base.targets then
260257
local active_chunk = BiterBase.is_in_active_chunk(base)
261258
if active_chunk then LogAI("Is in an active chunk: true", base) else LogAI("Is in an active chunk: false", base) end
262259

libs/world.lua

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,22 @@ require 'stdlib/surface'
55
require 'libs/biter/base'
66

77
World = {}
8-
World.version = 40
8+
World.version = 41
99
World.Logger = Logger.new("Misanthrope", "world", DEBUG_MODE)
1010
local Log = function(str, ...) World.Logger.log(string.format(str, ...)) end
1111

1212
function World.setup()
13-
local mod_version = global.mod_version
1413
if not global.mod_version then
1514
-- goodbye fair world
1615
local old_global = global
1716
global = {}
18-
global.mod_version = 40
19-
mod_version = 0
17+
global.mod_version = World.version
2018

2119
Harpa.migrate(old_global)
2220
World.recalculate_chunk_values()
2321
end
24-
if mod_version ~= global.mod_version then
25-
World.migrate(mod_version, World.version)
22+
if World.version ~= global.mod_version then
23+
World.migrate(global.mod_version, World.version)
2624
global.mod_version = World.version
2725
end
2826

@@ -43,6 +41,14 @@ function World.migrate(old_version, new_version)
4341
end
4442
end
4543
end
44+
elseif old_version < 41 then
45+
for i = #global.bases, 1, -1 do
46+
local base = global.bases[i]
47+
base.target = nil
48+
end
49+
if global.overmind then
50+
global.overmind.currency = 0
51+
end
4652
end
4753
end
4854

@@ -68,10 +74,11 @@ end
6874

6975
Event.register(defines.events.on_player_created, World.resync_players)
7076

71-
Event.register(Event.core_events.configuration_changed, function(event)
77+
Event.register({Event.core_events.init, Event.core_events.configuration_changed}, function(event)
7278
Log("Setting up world...")
73-
Log("Mod data: %s" , serpent.line(event.data))
79+
if event.data then
80+
Log("Mod data: %s", serpent.line(event.data))
81+
end
7482
World.setup()
75-
Event.remove(defines.events.on_tick, event._handler)
7683
Log("World setup complete.")
7784
end)

0 commit comments

Comments
 (0)