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
Changes from 1 commit
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
122 changes: 122 additions & 0 deletions luarules/gadgets/unit_target_prioritization.lua
Original file line number Diff line number Diff line change
@@ -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
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