From 978192daa7de7ed1619ca18ab701d6a098adeb11 Mon Sep 17 00:00:00 2001 From: SethDGamre Date: Sat, 28 Dec 2024 21:49:36 -0600 Subject: [PATCH 1/2] target prioritization proof of concept --- .../gadgets/unit_target_prioritization.lua | 122 ++++++++++++++++++ 1 file changed, 122 insertions(+) create mode 100644 luarules/gadgets/unit_target_prioritization.lua diff --git a/luarules/gadgets/unit_target_prioritization.lua b/luarules/gadgets/unit_target_prioritization.lua new file mode 100644 index 00000000000..ffd405eab42 --- /dev/null +++ b/luarules/gadgets/unit_target_prioritization.lua @@ -0,0 +1,122 @@ +function gadget:GetInfo() + return { + name = "Target Prioritization", + desc = "target prioritization", + author = "SethDGamre", + date = "2024.12.28", + license = "GNU GPL, v2 or later", + layer = 0, + enabled = true + } +end + +if not gadgetHandler:IsSyncedCode() then + return false -- no unsynced code +end + +----Localize Functions---- +local spGetUnitDefID = Spring.GetUnitDefID + +local function findMatch(string, pattern) + if string.match(string,"%f[%a]"..pattern.."%f[%A]") then --ripped this off the internet somewhere. Checks whole string patterns parsed by spaces not part of greater whole. + return true + else + return false + end +end + +----Unit Type Categorization Functions---- +local function unitIsSpam(uDef) + if (uDef.customParams.purpose and findMatch(uDef.customParams.purpose, "SPAM")) + or (uDef.health < 400 + and uDef.weapons and next(uDef.weapons) + and uDef.metalcost < 55) + then + return true + else + return false + end +end + + +----Weapon Type Categorization Functions---- +local function weaponIsAlphastrike(wDef) + if wDef.customParams.purpose and wDef.customParams.purpose == "ALPHASTRIKE" + or wDef.reloadTime and wDef.reloadTime < 3 + then + return true + else + return false + end +end + +local function weaponIsAntispam(wDef) + if wDef.customParams.purpose and wDef.customParams.purpose == "ALPHASTRIKE" + then + return true + else + return false + end +end + +----Constants---- +local WEAPONTYPES = { + HIGHALPHA = 1, + ANTISPAM = 2, +} +local UNITTYPES = { + SPAM = 1, + FAST = 2, +} + + +----Weight Table Entries---- +local weaponWeights = { + [WEAPONTYPES.ANTISPAM] = { + [UNITTYPES.FAST] = 1.1, + [UNITTYPES.SPAM] = 1.5, + }, + + [WEAPONTYPES.HIGHALPHA] = { + [UNITTYPES.FAST] = 0.2, + [UNITTYPES.SPAM] = 0.5, + } +} + +----Other Tables---- +local unitDefTypes = {} +local weaponDefTypes = {} + + +----Populate def tables---- +for uDefID, uDef in ipairs(UnitDefs) do + if unitIsSpam(uDef) then + unitDefTypes[uDefID] = UNITTYPES.SPAM + end +end + +for wDefID, wDef in ipairs(WeaponDefs) do + local typeWeights = {} + if weaponIsAlphastrike(wDef) then + typeWeights = weaponWeights[WEAPONTYPES.HIGHALPHA] + elseif weaponIsAntispam(wDef) then + typeWeights = weaponWeights[WEAPONTYPES.ANTISPAM] + end + weaponDefTypes[wDefID] = typeWeights +end + +function gadget:AllowWeaponTarget(unitID, targetID, attackerWeaponNum, attackerWeaponDefID, defPriority) + if not targetID then return true, defPriority end + + local weaponTypeWeights = weaponDefTypes[attackerWeaponDefID] + local targetDefID = spGetUnitDefID(targetID) + local targetType = unitDefTypes[targetDefID] + if targetType then + local newDefPriority = weaponTypeWeights[targetType] or defPriority + return true, newDefPriority + end + + return true, defPriority +end + +--Sprung's return code: antispam[attackerWeaponDefID] and Spring.Utilities.GetUnitPower(targetID) or defPriority From e8472e5f2900da81a92cf9d6bff995baa2122dcb Mon Sep 17 00:00:00 2001 From: SethDGamre Date: Sun, 12 Jan 2025 17:46:52 -0600 Subject: [PATCH 2/2] yee --- .../configs/weapon_prioritization_defs.lua | 86 +++++++++++++++++++ .../gadgets/unit_target_prioritization.lua | 49 ++--------- 2 files changed, 93 insertions(+), 42 deletions(-) create mode 100644 luarules/configs/weapon_prioritization_defs.lua diff --git a/luarules/configs/weapon_prioritization_defs.lua b/luarules/configs/weapon_prioritization_defs.lua new file mode 100644 index 00000000000..d5ee89fc8ce --- /dev/null +++ b/luarules/configs/weapon_prioritization_defs.lua @@ -0,0 +1,86 @@ +--[[ +WeaponDefs are assigned a behavior from the following table. +ALL +CANBEUW +COMMANDER +EMPABLE +FASTSURFACE +GROUNDSCOUT +HOVER +LIGHTAIRSCOUT +MINE +MOBILE +NOTAIR +NOTHOVER +NOTMOBILE +NOTSHIP +NOTSUB +NOWEAPON +OBJECT +RAPTOR +SHIP +SURFACE +T4AIR +UNDERWATER +VTOL +WEAPON + +]]-- +WeaponBehaviors = { + antiSpam = { + FASTSURFACE = 1.1, + GROUNDSCOUT = 1.2 + }, + slowProjectile = { + FASTSURFACE = 0.5, + GROUNDSCOUT = 0.2 + }, + highAlpha = { + FASTSURFACE = 0.9, + GROUNDSCOUT = 0.2 + }, +} + +----Unit Type Categorization Functions---- +local function unitIsSpam(uDef) + if (uDef.customParams.purpose and findMatch(uDef.customParams.purpose, "SPAM")) + or (uDef.health < 400 + and uDef.weapons and next(uDef.weapons) + and uDef.metalcost and uDef.metalcost < 55) + then + return true + else + return false + end +end + +local UnitCategorizationFunctions = { + SPAM = unitIsSpam +} + +----Weapon Type Categorization Functions---- +local function weaponIsAlphastrike(wDef) + if wDef.customParams.purpose and wDef.customParams.purpose == "ALPHASTRIKE" + or wDef.reloadTime and wDef.reloadTime < 3 + then + return true + else + return false + end +end + +local function weaponIsAntispam(wDef) + if wDef.customParams.purpose and wDef.customParams.purpose == "ANTISPAM" + then + return true + else + return false + end +end + +local UnitCategorizationFunctions = { + ALPHASTRIKE = weaponIsAlphastrike, + ANTISPAM = weaponIsAntispam +} + +--zzz next episode: figure out exactly how the weapon checks are gonna work \ No newline at end of file diff --git a/luarules/gadgets/unit_target_prioritization.lua b/luarules/gadgets/unit_target_prioritization.lua index ffd405eab42..caf580dcfd8 100644 --- a/luarules/gadgets/unit_target_prioritization.lua +++ b/luarules/gadgets/unit_target_prioritization.lua @@ -17,48 +17,6 @@ end ----Localize Functions---- local spGetUnitDefID = Spring.GetUnitDefID -local function findMatch(string, pattern) - if string.match(string,"%f[%a]"..pattern.."%f[%A]") then --ripped this off the internet somewhere. Checks whole string patterns parsed by spaces not part of greater whole. - return true - else - return false - end -end - -----Unit Type Categorization Functions---- -local function unitIsSpam(uDef) - if (uDef.customParams.purpose and findMatch(uDef.customParams.purpose, "SPAM")) - or (uDef.health < 400 - and uDef.weapons and next(uDef.weapons) - and uDef.metalcost < 55) - then - return true - else - return false - end -end - - -----Weapon Type Categorization Functions---- -local function weaponIsAlphastrike(wDef) - if wDef.customParams.purpose and wDef.customParams.purpose == "ALPHASTRIKE" - or wDef.reloadTime and wDef.reloadTime < 3 - then - return true - else - return false - end -end - -local function weaponIsAntispam(wDef) - if wDef.customParams.purpose and wDef.customParams.purpose == "ALPHASTRIKE" - then - return true - else - return false - end -end - ----Constants---- local WEAPONTYPES = { HIGHALPHA = 1, @@ -93,6 +51,13 @@ for uDefID, uDef in ipairs(UnitDefs) do if unitIsSpam(uDef) then unitDefTypes[uDefID] = UNITTYPES.SPAM end + + if uDef.springCategories then + Spring.Echo("springCategories", uDef.name, uDef.springCategories) + for category, bool in pairs( uDef.springCategories) do + Spring.Echo(category, bool, type(bool)) + end + end end for wDefID, wDef in ipairs(WeaponDefs) do