From 9701a724a8618aa856db8977232438861abf5234 Mon Sep 17 00:00:00 2001 From: mattsc Date: Thu, 17 Apr 2014 20:35:48 -0700 Subject: [PATCH] ai_helper.lua: avoid using table.remove MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit It’s slow. The inverse logic using table.insert is much faster, especially for large tables. --- data/ai/lua/ai_helper.lua | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/data/ai/lua/ai_helper.lua b/data/ai/lua/ai_helper.lua index a45c347c08f..4564119a723 100644 --- a/data/ai/lua/ai_helper.lua +++ b/data/ai/lua/ai_helper.lua @@ -1365,12 +1365,13 @@ function ai_helper.get_attack_combos_full(units, enemy) -- Return value: -- 1. Attack combinations in form { dst = src } - local attacks = ai_helper.get_attacks(units) + local all_attacks = ai_helper.get_attacks(units) -- Eliminate those that are not on @enemy - for i = #attacks,1,-1 do - if (attacks[i].target.x ~= enemy.x) or (attacks[i].target.y ~= enemy.y) then - table.remove(attacks, i) + local attacks = {} + for _,attack in ipairs(all_attacks) do + if (attack.target.x == enemy.x) and (attack.target.y == enemy.y) then + table.insert(attacks, attack) end end if (not attacks[1]) then return {} end @@ -1515,6 +1516,7 @@ function ai_helper.get_attack_combos(units, enemy, cfg) end -- This last step eliminates the "empty attack combo" (the one with all zeros) + -- Since this is only one, it's okay to use table.remove (even though it's slow) table.remove(attack_array, i_empty) return attack_array