From 9b6e13e0fa07cf87738c1433d91c5cf269344ea1 Mon Sep 17 00:00:00 2001 From: Aaron Veden Date: Sun, 14 Jan 2018 16:14:38 -0800 Subject: [PATCH] free units and unit groups from vanilla get kamikaze roll --- changelog.txt | 2 ++ libs/SquadAttack.lua | 2 +- libs/SquadDefense.lua | 8 ++++++++ libs/UnitGroupUtils.lua | 12 +++++++----- 4 files changed, 18 insertions(+), 6 deletions(-) diff --git a/changelog.txt b/changelog.txt index e117264..b1f2e34 100755 --- a/changelog.txt +++ b/changelog.txt @@ -3,6 +3,8 @@ Version: 0.16.3 Date: 1. 13. 2018 Improvements: - Removed squad limit + - Squads converted from vanilla control receive immediate kamikaze roll + - Units not under a unit group, now get a kamikaze roll before being put into a retreating squad Optimizations: - Reworked squad regrouping to be surrounding chunks instead comparing against master list Framework: diff --git a/libs/SquadAttack.lua b/libs/SquadAttack.lua index 6985229..ba561d0 100755 --- a/libs/SquadAttack.lua +++ b/libs/SquadAttack.lua @@ -129,7 +129,7 @@ function squadAttack.squadsBeginAttack(natives, players) local squad = squads[i] local group = squad.group if (squad.status == SQUAD_GUARDING) and group.valid then - local kamikazeThreshold = calculateKamikazeThreshold(squad, natives) + local kamikazeThreshold = calculateKamikazeThreshold(#squad.group.members, natives) local groupPosition = group.position if playersWithinProximityToPosition(players, groupPosition, 100) then diff --git a/libs/SquadDefense.lua b/libs/SquadDefense.lua index c9e05d6..b4f5597 100755 --- a/libs/SquadDefense.lua +++ b/libs/SquadDefense.lua @@ -23,8 +23,12 @@ local SENTINEL_IMPASSABLE_CHUNK = constants.SENTINEL_IMPASSABLE_CHUNK -- imported functions +local mRandom = math.random + local addSquadToChunk = chunkUtils.addSquadToChunk +local calculateKamikazeThreshold = unitGroupUtils.calculateKamikazeThreshold + local positionFromDirectionAndChunk = mapUtils.positionFromDirectionAndChunk local getNeighborChunks = mapUtils.getNeighborChunks local findNearbySquadFiltered = unitGroupUtils.findNearbySquadFiltered @@ -53,6 +57,10 @@ function aiDefense.retreatUnits(chunk, position, squad, map, surface, natives, t if not squad then enemiesToSquad = surface.find_enemy_units(position, radius) performRetreat = #enemiesToSquad > 0 + if (mRandom() < calculateKamikazeThreshold(#enemiesToSquad, natives)) then + setRetreatTick(map, chunk, tick + (INTERVAL_LOGIC * 10)) + performRetreat = false + end elseif squad.group.valid and (squad.status ~= SQUAD_RETREATING) and not squad.kamikaze then performRetreat = #squad.group.members > 1 end diff --git a/libs/UnitGroupUtils.lua b/libs/UnitGroupUtils.lua index 186f9cb..a195a7d 100755 --- a/libs/UnitGroupUtils.lua +++ b/libs/UnitGroupUtils.lua @@ -90,8 +90,8 @@ end -- end -- end -function unitGroupUtils.createSquad(position, surface, natives) - local unitGroup = surface.create_unit_group({position=position}) +function unitGroupUtils.createSquad(position, surface, natives, group) + local unitGroup = group or surface.create_unit_group({position=position}) local squad = { group = unitGroup, @@ -131,11 +131,13 @@ function unitGroupUtils.convertUnitGroupToSquad(natives, unitGroup) return squad end end - return nil + local squad = unitGroupUtils.createSquad(nil,nil,natives,unitGroup) + squad.kamikaze = mRandom() < unitGroupUtils.calculateKamikazeThreshold(#unitGroup.members, natives) + return squad end -function unitGroupUtils.calculateKamikazeThreshold(squad, natives) - local squadSizeBonus = mLog((#squad.group.members / natives.attackWaveMaxSize) + 0.1) + 1 +function unitGroupUtils.calculateKamikazeThreshold(memberCount, natives) + local squadSizeBonus = mLog((memberCount / natives.attackWaveMaxSize) + 0.1) + 1 return natives.kamikazeThreshold + (NO_RETREAT_SQUAD_SIZE_BONUS_MAX * squadSizeBonus) end