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 new file mode 100644 index 00000000000..caf580dcfd8 --- /dev/null +++ b/luarules/gadgets/unit_target_prioritization.lua @@ -0,0 +1,87 @@ +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 + +----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 + + 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 + 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