[Lua] Cleaning up this script.

Irkalla

ENTP POWWWEEEEEER
Joined
Sep 25, 2012
Messages
1,009
Location
Way down around Vicksburg
My questions are outlined in the comments. I'd like a bit of advice, too. This is my first Lua script for Civ! :V

l
 
Not tested, but try

Code:
-- An ordered array of what units can be trained, best to worst
local possibleUnits = {
  GameInfoTypes.UNIT_MECH,
  GameInfoTypes.UNIT_MECHANIZED_INFANTRY, 
  GameInfoTypes.UNIT_INFANTRY, 
  GameInfotypes.UNIT_GREAT_WAR_INFANTRY, 
  GameInfoTypes.UNIT_RIFLEMAN, 
  GameInfoTypes.UNIT_MUSKETMAN, 
  GameInfoTypes.UNIT_LONGSWORDSMAN, 
  GameInfoTypes.UNIT_PIKEMAN, 
  GameInfoTypes.UNIT_SWORDSMAN, 
  GameInfoTypes.UNIT_SPARTAN_PHALANX, 
  GameInfoTypes.UNIT_WARRIOR
}

function fn_returnBestInfantryUnit( pCity )
  for _, iUnit in ipairs(possibleUnits) do
    if pCity:CanTrain(iUnit) then
      return iUnit
    end
  end

  -- We should never get here as the Warrior should always be available
  return -1
end
 
"-- Gotta be a better way then this if/then bollocks" - probably not, if the logic reads "if this and this and this and this and this then that" you are going to need all those conditions.

But you do have a syntax error "&& then if" (split over two lines)

Edit: And if you could use the [ CODE ] tags (and not link as an image to some off-site script dumping site) it makes editing the code when replying a whole load easier :D
 
"-- Gotta be a better way then this if/then bollocks" - probably not, if the logic reads "if this and this and this and this and this then that" you are going to need all those conditions.

But you do have a syntax error "&& then if" (split over two lines)

Edit: And if you could use the [ CODE ] tags (and not link as an image to some off-site script dumping site) it makes editing the code when replying a whole load easier :D

I really hate code tags... I love my syntax highlighting. I should bug the admins about finding us a syntax highlighting script that uses pastebin or something, and get rid of the php, code, and html tags.

Also, I completely forgot about looping through a table. That's a load cleaner. Been too long since I messed about with Lua.

pastebin
Code:
--[[-----------------------------------------
Name:		 GetCivSpecificUnit
Purpose: 	 Get the UnitType for a specific
civ from a UnitClassType.
-------------------------------------------]]
function fnGetCivSpecificUnit(pPly, sUnitClass) -- Thanks whoward69

	-- BEGIN DEFINES
	local sUnitType = nil
	local sCivType = GameInfo.Civilizations[pPly:GetCivilizationType()].Type
	-- END DEFINES
	
	-- Loop through civilization-specific UnitClass overrides, id est their unique units, and yield to be returned the proper UnitType. 
	for pOverride in GameInfo.Civilization_UnitClassOverrides{CivilizationType = sCivType, UnitClassType = sUnitClass} do
		sUnitType = pOverride.UnitType
		break
	end

	-- If we didn't get anything, yield to be returned the default UnitType for the UnitClass.
	if (sUnitType == nil) then
		sUnitType = GameInfo.UnitClasses[sUnitClass].DefaultUnit
	end

	-- Give whatever function called this the UnitType we yielded.
	return sUnitType
end

--[[-----------------------------------------
Name:  		ReturnBestInfantryUnit
Purpose:	Return the best land melee unit
that a city can build.  
-------------------------------------------]]
function fnReturnBestInfantryUnit( pCity )

	--BEGIN DEFINES
	local pPly = pCity:GetOwner()
	
	local possibleUnitClasses = { 
	GameInfoTypes.UNITCLASS_MECH,
	GameInfoTypes.UNITCLASS_MECHANIZED_INFANTRY, 
	GameInfoTypes.UNITCLASS_INFANTRY, 
	GameInfotypes.UNITCLASS_GREAT_WAR_INFANTRY, 
	GameInfoTypes.UNITCLASS_RIFLEMAN, 
	GameInfoTypes.UNITCLASS_MUSKETMAN, 
	GameInfoTypes.UNITCLASS_LONGSWORDSMAN, 
	GameInfoTypes.UNITCLASS_PIKEMAN, 
	GameInfoTypes.UNITCLASS_SWORDSMAN, 
	GameInfoTypes.UNITCLASS_SPEARMAN, 
	GameInfoTypes.UNITCLASS_WARRIOR
	} -- Thanks whoward69
	--END DEFINES
	
	-- Loop through each UnitClassType in the above defined table, see if the city can 
	-- train it's owner's specific UnitType.  Yield to be returned the best land melee 
	-- UnitType the city can build.
	for _, iUnitClass in ipairs(possibleUnitClasses) do -- Thanks whoward69
		if pCity:CanTrain( fn_GetCivSpecificUnit( pPly, iUnitClass ) ) then
			return fn_GetCivSpecificUnit( pPly, iUnitClass )
		end
	end

	-- Uh-oh!
	return -1
end

GameEvents.SetPopulation.Add( function( xOffset, yOffset, fOldPop, fNewPop )

	-- BEGIN DEFINES
	local pPlot				= Map.GetPlot( xOffset, yOffset )
	local pCity  			= pPlot:GetPlotCity()
	local pPly 				= pCity:GetOwner()
	local iUnitMostCurrent	= nil
	local nTrait			= GameInfo.Traits[pPly].UnitPerCapitalGrowths
	local sUnitAIType		= GameInfo.UnitAITypes.UNITAI_DEFENSE.ID
	local nZenith			= math.floor( pCity:GetHighestPopulation() )
	-- END DEFINES

	-- On every growth of every city, run through the following list of conditions:
	-- 1:  Does the city's owner have the trait, and is its population evenly divisible by the trait?
	-- 2:  Is the city a capital?
	-- 3:  Has the city grown instead of shrinking, and is this the city's highest population?
	-- If all of the above are true, get the best land infantry unit we can, and spawn it at the city.
	if ( nTrait > 0 &&
	( math.floor( fNewPop ) % nTrait ) == 0 ) && then 
		if pCity:IsCapital() then
			if ( fNewPop > fOldPop ) &&
			fNewPop >= nZenith then
				iFreeUnit = fnReturnBestInfantryUnit( pCity )

				pPly:AddFreeUnit( iFreeUnit, iUnitAIType ) -- What are the implications of this line?
			end
		end
	end
end )
 
Back
Top Bottom