This could be fleshed out for any number of purposes, including upgraded versions of units, etc.
I think it's finally un-ugly enough to actually use at this point. Took a minute, because I made some dreadful mistakes, wrote a very nasty orkish version, then had to re-write it to be a little more elegant.
Feel free to do whatever you want with it, this is a piece of core game logic found in a lot of major games, and I think there are probably a lot of uses for this that I haven't even thought about yet, but you could do stuff like StarCraft-style unit upgrade trees, etc., very easily with this. For P.U.R.E., I mainly just intend to lock up my factories, so that you can't buy certain units at the start of the game, but there are a lot of more creative uses for this

Code: Select all
function gadget:GetInfo()
return {
name = "Tier Control",
desc = "Allows certain Units to have tiers of Units they may build.",
author = "Argh",
date = "January 13th, 2009",
license = "Public Domain, or the least-restrictive rights of your country of residence.",
layer = 1,
enabled = true,
}
end
local tierTable = {0}
local CMD_BUY_TIER_TWO = 32700
local CMD_BUY_TIER_THREE = 32701
BuyTierTwo = {
id=CMD_BUY_TIER_TWO,
type=CMDTYPE.ICON,
name="Upgrade to Tech Level Two",
onlyTexture=true,
texture="&.9x.9&bitmaps/icons/blank.tif&bitmaps/icons/BUY_TIER_TWO.png",
tooltip="Upgrades this Factory to produce second-tier units.\r\nCosts 1000 Materials.\r\nHint: this will also boost your factory's output.",
action="buy tier two",
cursor='Guard'
}
BuyTierThree = {
id=CMD_BUY_TIER_THREE,
type=CMDTYPE.ICON,
name="Upgrade to Tech Level Three",
onlyTexture=true,
texture="&.9x.9&bitmaps/icons/blank.tif&bitmaps/icons/BUY_TIER_THREE.png",
tooltip="Upgrades this Factory to produce third-tier units.\r\nCosts 4000 Materials.\r\nHint: this will also boost your factory's output.",
action="buy tier three",
cursor='Guard'
}
if (gadgetHandler:IsSyncedCode()) then
--------------------------------------------------------------------------------
-- SYNCED
--------------------------------------------------------------------------------
function gadget:Initialize()
-- Set up tiers, and the Units that are in each tier.
for ud,_ in pairs(UnitDefs) do -- search all UnitDef entries
if UnitDefs[ud].customParams.build_tiers == 'yes' then
table.insert(tierTable,ud,0)
end
end
end
function gadget:UnitCreated(u, ud, team)
if tierTable[ud] then
Spring.InsertUnitCmdDesc(u,BuyTierTwo)
for i,k in ipairs (UnitDefs) do
CommandList = Spring.FindUnitCmdDesc(u,-i)
if CommandList ~= nil then
if UnitDefs[i].customParams.level == "two" then
Spring.EditUnitCmdDesc(u,CommandList,{hidden = true,})
end
end
end
for i,k in ipairs (UnitDefs) do
CommandList = Spring.FindUnitCmdDesc(u,-i)
if CommandList ~= nil then
if UnitDefs[i].customParams.level == "three" then
Spring.EditUnitCmdDesc(u,CommandList,{hidden = true,})
end
end
end
end
end
--NOTE: THIS USES ALLOWCOMMAND, NOT COMMANDFALLBACK, UNKNOWN WHY FALLBACK DOES NOT WORK FOR A FACTORY
function gadget:AllowCommand(u, ud, team, cmd, param, opts)
if cmd == CMD_BUY_TIER_TWO then
local materials = Spring.GetTeamResources(team, 'metal')
if materials >= 1000 then
Spring.UseTeamResource(team,'m',1000)
for i,k in ipairs (UnitDefs) do
CommandList = Spring.FindUnitCmdDesc(u,-i)
if CommandList ~= nil then
if UnitDefs[i].customParams.level == "two" then
Spring.EditUnitCmdDesc(u,CommandList,{hidden = false,})
end
end
end
Spring.InsertUnitCmdDesc(u,BuyTierThree)
local cmdID = Spring.FindUnitCmdDesc(u, CMD_BUY_TIER_TWO)
Spring.RemoveUnitCmdDesc(u, cmdID)
else
Spring.Echo("Not enough Materials!")
end
return false
end
if cmd == CMD_BUY_TIER_THREE then
local materials = Spring.GetTeamResources(team, 'metal')
if materials >= 4000 then
Spring.UseTeamResource(team,'m',4000)
for i,k in ipairs (UnitDefs) do
CommandList = Spring.FindUnitCmdDesc(u,-i)
if CommandList ~= nil then
if UnitDefs[i].customParams.level == "three" then
Spring.EditUnitCmdDesc(u,CommandList,{hidden = false,})
end
end
end
local cmdID = Spring.FindUnitCmdDesc(u, CMD_BUY_TIER_THREE)
Spring.RemoveUnitCmdDesc(u, cmdID)
else
Spring.Echo("Not enough Materials!")
end
return false
end
return true-- If all else fails.
end
--------------------------------------------------------------------------------
-- END SYNCED
--------------------------------------------------------------------------------
end