Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

target prioritization proof of concept #4088

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 86 additions & 0 deletions luarules/configs/weapon_prioritization_defs.lua
Original file line number Diff line number Diff line change
@@ -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
87 changes: 87 additions & 0 deletions luarules/gadgets/unit_target_prioritization.lua
Original file line number Diff line number Diff line change
@@ -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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can assign directly to weaponDefTypes[wDefID] inside the if/elseif clauses so you won't have an empty table for unused wDefIDs.

that way also you can return early below when you see weaponDefTypes[attackerWeaponDefID] is nil.

end

function gadget:AllowWeaponTarget(unitID, targetID, attackerWeaponNum, attackerWeaponDefID, defPriority)
if not targetID then return true, defPriority end

local weaponTypeWeights = weaponDefTypes[attackerWeaponDefID]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

after this line you can check if not weaponTypeWeights then return true, defPriority end if you follow the above advice.

local targetDefID = spGetUnitDefID(targetID)
local targetType = unitDefTypes[targetDefID]
if targetType then
local newDefPriority = weaponTypeWeights[targetType] or defPriority
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing tab here before local

return true, newDefPriority
end

return true, defPriority
end

--Sprung's return code: antispam[attackerWeaponDefID] and Spring.Utilities.GetUnitPower(targetID) or defPriority