Page 1 of 1

Tier Code, Alpha 2

Posted: 13 Jan 2009, 09:55
by Argh
Here is some PD gamecode that allows your Factories to have tiers that can be bought, which change their build-menus when they're purchased.

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

Re: Tier Code, Alpha 2

Posted: 14 Jan 2009, 00:56
by Argh
Here's Alpha 2. Refined it a bit more- now each Unit you want to give Tiers to can have separate costs assigned in its customParams, and the Tooltip changes appropriately. Changed the language, and added the feature allowing the Unit's buildspeed to actually grow as advertised in the text. It would be trivial to add another customParam or two to govern that ramp, instead of leaving it flat across your game design.

Probably about as far as I'm going to develop this, it's feature-complete for P.U.R.E. RC4.

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="placeholder",
		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="placeholder",
		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)
		local cmdID = Spring.FindUnitCmdDesc(u, CMD_BUY_TIER_TWO)
		Spring.EditUnitCmdDesc(u,cmdID,{tooltip = "Upgrades this Factory to produce second-tier units or buildings.\r\nCosts "..UnitDefs[ud].customParams.tier_cost2.." Materials.\r\nHint:  

this will also boost this builder's output.",})


		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')
			local cost = tonumber(UnitDefs[ud].customParams.tier_cost2)
			if materials >= cost then

				Spring.UseTeamResource(team,'m',cost)
				local buildSpeed = tonumber(UnitDefs[ud].buildSpeed)
				Spring.SetUnitBuildSpeed(u,buildSpeed * 1.25)
				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_THREE)
				Spring.EditUnitCmdDesc(u,cmdID,{tooltip = "Upgrades this Factory to produce third-tier units or buildings.\r\nCosts "..UnitDefs[ud].customParams.tier_cost3.." 

Materials.\r\nHint:  this will also boost this builder's output.",})

				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')
			local cost = tonumber(UnitDefs[ud].customParams.tier_cost3)
			if materials >= cost then

				Spring.UseTeamResource(team,'m',cost)
				local buildSpeed = tonumber(UnitDefs[ud].buildSpeed)
				Spring.SetUnitBuildSpeed(u,buildSpeed * 1.5)
				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

Re: Tier Code, Alpha 2

Posted: 14 Jan 2009, 14:30
by SpikedHelmet
Atm we use basic morphs for factories to morph into a different factory, though this seems like it could be beneficial (not having extraneous factory units)

Re: Tier Code, Alpha 2

Posted: 15 Jan 2009, 00:09
by Argh
Yeah, I mainly see this as a way to build mechanics for factories that allow them to stay the same basic content (or just put some new greebles on with Lua-->COB) while morphing in capabilities over time. For example, in that second version, my factories change their output.

But I could also do stuff where you could buy stat upgrades for new units, etc., in combination with this, for extra flavor. The possibilities of this sort of code are pretty much endless, in terms of stretching out a single piece of content.