Skip to content

Commit 82e8ea9

Browse files
committed
Prevented failing squad-to-smart assignment being repeated ad infinitum
1 parent 30c4098 commit 82e8ea9

3 files changed

Lines changed: 21 additions & 12 deletions

File tree

gamedata/scripts/sim_board.script

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -880,6 +880,7 @@ function sim_board:reset_nearest_squads(smart)
880880
squad.current_action = nil
881881
squad:update()
882882
end
883+
squad.failed_assignments = {}
883884
end
884885
end
885886
end

gamedata/scripts/sim_faction.script

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -478,13 +478,13 @@ function se_sim_faction:calculate_squad_tasks()
478478

479479
if script_target ~= nil then
480480
self.squad_target_cache[squad.squad_id] = script_target -- if squad has script target then assign squad to that target
481-
if self.target_smart_value[script_target] ~= nil then -- if script target is a registered target smart then
481+
if self.target_smart_value[script_target] ~= nil then -- if script target is a registered target smart:
482482
self.target_smart_value[script_target].num = self.target_smart_value[script_target].num + 1 -- increase number of squads going to that target smart by 1
483483
end
484-
-- dbglog("Assigned "..self.player_name.." squad with ID "..tostring(squad.squad_id).." to scripted target ["..game.translate_string("st_name_"..board_smarts[script_target].smrt:name()).."].")
484+
-- dbglog("Assigned "..self.player_name.." squad with ID ["..squad.squad_id.."] to scripted target ["..game.translate_string("st_name_"..board_smarts[script_target].smrt:name()).."].")
485485
else
486486
if squad.smart_id == nil then
487-
abort("[sim_faction.calculate_squad_tasks]: Squad with ID '%s' has no registered source smart.", tostring(squad.squad_id))
487+
abort("[sim_faction.calculate_squad_tasks]: Squad with ID '%s' has no registered source smart.", squad.squad_id)
488488
return
489489
end
490490

@@ -493,7 +493,7 @@ function se_sim_faction:calculate_squad_tasks()
493493
local is_stay = false
494494
local dest_smart, graph_point, graph_point_dest
495495

496-
if squad.current_action == nil or squad.current_action.name == "stay_point" then -- if squad is idling around then:
496+
if squad.current_action == nil or squad.current_action.name == "stay_point" then -- if squad is idling around:
497497
is_stay = true
498498
dest_smart = board_smarts[squad.smart_id].smrt -- 'dest_smart' = squad's current smart
499499
graph_point = GG:vertex(dest_smart.m_game_vertex_id):game_point()
@@ -505,13 +505,14 @@ function se_sim_faction:calculate_squad_tasks()
505505

506506
squad_need_task[squad.squad_id] =
507507
{
508+
squad = squad,
508509
dest_smart = dest_smart,
509510
is_stay = is_stay,
510511
graph_point = graph_point,
511512
graph_point_dest = graph_point_dest
512513
}
513514

514-
-- dbglog("Inserted "..self.player_name.." squad with ID "..tostring(squad.squad_id).." and dest smart ["..game.translate_string("st_name_"..dest_smart:name()).."] to squad_need_task.")
515+
-- dbglog("Inserted "..self.player_name.." squad with ID ["..squad.squad_id.."] and dest smart ["..game.translate_string("st_name_"..dest_smart:name()).."] to squad_need_task.")
515516
end
516517
end
517518

@@ -530,13 +531,17 @@ function se_sim_faction:calculate_squad_tasks()
530531

531532
for squad_id, squad_data in pairs (squad_need_task) do -- for all squads in need of task (i.e. any squad not assigned to a script target):
532533

533-
if level_cluster[squad_data.dest_smart.smart_level] == level_cluster[target_smart_obj.smart_level] then -- if registered target smart is in the same level cluster as 'dest_smart' then
534+
if level_cluster[squad_data.dest_smart.smart_level] == level_cluster[target_smart_obj.smart_level] then -- if registered target smart is in the same level cluster as 'dest_smart':
534535

535536
target_smart_gpoint = target_smart_gpoint or GG:vertex(target_smart_obj.m_game_vertex_id):game_point()
536537
local gr_dist
537538

538539
if squad_data.is_stay then
539-
gr_dist = squad_data.graph_point:distance_to(target_smart_gpoint)
540+
if squad_data.squad.failed_assignments[target_smart_id] then -- if failed previous attempt to move to this target smart from same origin smart:
541+
gr_dist = squad_data.graph_point:distance_to(target_smart_gpoint) + 10000
542+
else
543+
gr_dist = squad_data.graph_point:distance_to(target_smart_gpoint)
544+
end
540545
else
541546
if squad_data.dest_smart.id == target_smart_id then
542547
gr_dist = squad_data.graph_point:distance_to(target_smart_gpoint) * 0.001
@@ -545,14 +550,14 @@ function se_sim_faction:calculate_squad_tasks()
545550
end
546551
end
547552

548-
-- dbglog("Graph distance of "..self.player_name.." squad with ID "..tostring(squad_id).." to smart ["..game.translate_string("st_name_"..target_smart_obj:name()).."] is ["..gr_dist.."].")
553+
-- dbglog("Graph distance of "..self.player_name.." squad with ID ["..squad_id.."] to smart ["..game.translate_string("st_name_"..target_smart_obj:name()).."] is ["..gr_dist.."].")
549554

550555
suitable_squads = suitable_squads + 1
551556
squads[suitable_squads] = {squad_id = squad_id, dist = gr_dist} -- add squad info to table 'squads'
552557
end
553558
end
554559

555-
if suitable_squads > 0 then -- if at least one squad is in the same level cluster as registered target smart then
560+
if suitable_squads > 0 then -- if at least one squad is in the same level cluster as registered target smart:
556561
table_sort(squads, _sort_by_dist_asc) -- sort 'squads' by squad distance to registered target smart in ascending order
557562
c = c + 1
558563
distance_table[c] = {target_smart = target_smart, squads = squads, value = target_smart.value} -- and add {target_smart, squad infos from 'squads', target_smart value} to distance_table
@@ -582,11 +587,11 @@ function se_sim_faction:calculate_squad_tasks()
582587

583588
self.squad_target_cache[tmp_squad_id] = tmp_smart.smart_id -- assign nearest squad to smart in first element of distance_table
584589

585-
-- dbglog("Assigned "..self.player_name.." squad with ID "..tostring(tmp_squad_id).." to ["..game.translate_string("st_name_"..tmp_smart.smart:name()).."].")
590+
-- dbglog("Assigned "..self.player_name.." squad with ID ["..tmp_squad_id.."] to ["..game.translate_string("st_name_"..tmp_smart.smart:name()).."].")
586591

587592
tmp_smart.num = tmp_smart.num + 1 -- increase number of squads assigned to it by 1
588593

589-
if tmp_smart.num >= tmp_smart.cap then -- if number of squads assigned to it now meets or exceeds its capacity,
594+
if tmp_smart.num >= tmp_smart.cap then -- if number of squads assigned to it now meets or exceeds its capacity:
590595
-- dbglog("Removed ["..game.translate_string("st_name_"..tmp_smart.smart:name()).."] from distance_table since its capacity ("..tostring(tmp_smart.cap)..") is saturated ("..tostring(tmp_smart.num)..").")
591596
table_remove(distance_table, 1) -- remove it from distance_table
592597
end
@@ -596,7 +601,7 @@ function se_sim_faction:calculate_squad_tasks()
596601
for j = #smart_data.squads, 1, -1 do
597602
if smart_data.squads[j].squad_id == tmp_squad_id then
598603
-- local smart_name = game.translate_string("st_name_"..smart_data.target_smart.smart:name())
599-
-- dbglog("Made "..self.player_name.." squad with ID "..tostring(tmp_squad_id).." unassignable to ["..smart_name.."].")
604+
-- dbglog("Made "..self.player_name.." squad with ID ["..tmp_squad_id.."] unassignable to ["..smart_name.."].")
600605
if #smart_data.squads == 1 then
601606
-- dbglog("Removed ["..smart_name.."] from distance_table since no "..self.player_name.." squad is assignable to it.")
602607
table_remove(distance_table, i) -- remove any smarts from distance_table to which no squad is assignable

gamedata/scripts/sim_squad_generic.script

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ function sim_squad_generic:__init(sim_board, player_id, squad_id)
6767
self.show_disabled = false
6868
self.box_items = nil
6969
self.items_spawned = false
70+
self.failed_assignments = {}
7071
end
7172

7273
-- function sim_squad_generic:init_squad() -- smart)
@@ -715,6 +716,7 @@ function sim_squad_generic:get_next_action()
715716
end
716717
end
717718
else
719+
self.failed_assignments[target_smart_id] = true
718720
self.current_action = sim_stay_point(self)
719721
self.current_action.next_point_id = next_point.id
720722
self:calculate_attack_power()
@@ -1533,6 +1535,7 @@ function sim_attack_point:make()
15331535

15341536
cur_attack_quantity[dest_smrt_id] = cur_attack_quantity[dest_smrt_id] + 1
15351537

1538+
squad.failed_assignments = {}
15361539
squad.spoted_shouted = false
15371540

15381541
local board_smarts = board.smarts

0 commit comments

Comments
 (0)