Any gotchas with Lua scripts?

completely lost here.

A player doesn't have a trait.
A leaders has a trait.
A civilizations has a leader.
A players has a civilization.

You can use Lua to find the player's civilization

To get the leader you have to query (join) the Civilization_Leaders table
To get the trait tou have to query (join) the Leader_Traits table

Which is what the SQL does in one statement

this is why we need file.Write so we can write SQL scripts on the fly for each civilization.

You can write SQL on the fly - write it into a string and then use DB.Query(sQuery) to execute it
 
Moriboe and I got the trait thing working. Now all that doesn't work properly is the function you wrote to loop through the unitclasses. I think I know what's up, though.

This is being run on a city that's just been founded. I cannot train any units except for Workers, Scouts, and Warriors.

N3bwd.png


Code:
print( "Loaded." )

--[[-----------------------------------------
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.
	print( "Got Civ-Specific UnitType: " .. tostring( sUnitType ) .. ".\n" )
	return sUnitType
end

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


	--BEGIN DEFINES
	--local iPlyID	= pCity:GetOwner()
	--local pPly		= Players[ iPlyID ]
	
	local possibleUnitClasses = { 
	UNITCLASS_MECH,
	UNITCLASS_MECHANIZED_INFANTRY, 
	UNITCLASS_INFANTRY, 
	UNITCLASS_GREAT_WAR_INFANTRY, 
	UNITCLASS_RIFLEMAN, 
	UNITCLASS_MUSKETMAN, 
	UNITCLASS_LONGSWORDSMAN, 
	NITCLASS_PIKEMAN, 
	UNITCLASS_SWORDSMAN, 
	UNITCLASS_SPEARMAN, 
	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
			
			print( "Unit to be given: " .. fn_GetCivSpecificUnit( pPly, iUnitClass ) .. "." )

			return fn_GetCivSpecificUnit( pPly, iUnitClass )
		end
	end

	-- Uh-oh!
	print("ReturnBestInfantryUnit: Irkalla ed up.")
	return -1
end

GameEvents.SetPopulation.Add( function( xOffset, yOffset, fOldPop, fNewPop )
	print( "SetPopulation was called." )
	print( "Y Offset: " .. yOffset .. "." )
	print( "X Offset: " .. xOffset .. "." )
	print( "Old Population: " .. fOldPop .. "." )
	print( "New Population: " .. fNewPop .. "." )

	-- BEGIN DEFINES
	local pPlot				= Map.GetPlot( xOffset, yOffset )
	local pCity  			= pPlot:GetPlotCity()
	print( "City: " .. pCity:GetName() .. "." )
	local iPlyID 			= pCity:GetOwner()
	local pPly				= Players[ iPlyID ] -- Dat Datatype Mismatch, son
	print( "Player: " .. pPly:GetName() .. "." )
	local iUnitMostCurrent	= nil
	local pLeader			= GameInfo.Leaders[ pPly:GetLeaderType() ]
	local iLeaderTrait		= GameInfo.Leader_Traits("LeaderType ='" .. pLeader.Type .. "'")()
	local pTrait			= GameInfo.Traits[ iLeaderTrait.TraitType ]
	local nTrait			= pTrait.UnitPerCapitalGrowths
	print( "Capital Growths Needed for Free Unit by " .. pPly:GetName() .. ": " .. nTrait .. "." )
	-- local sUnitAIType		= UNITAI_DEFENSE
	local nZenith			= math.floor( pCity:GetHighestPopulation() )
	print( "Highest Population: " .. nZenith .. "." )
	local strGrowFallen		= "done something"
	-- END DEFINES

	if ( fNewPop < fOldPop ) then
		strGrowFallen = "fallen"
	else
		strGrowFallen = "grown"
	end

	--[[if xOffset then
		print( "Got an X offset" )
	else
		print( "Don't got an X offset" )
	end--]]
	

	--[[ 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. --]]

	print( "Info: " .. tostring( pCity:GetName() ) .. "'s Population has " .. strGrowFallen .. " from " .. tostring( fOldPop ) .. " to " .. tostring( fNewPop ) .. "!  Highest Population: " .. tostring( nZenith ) .. ".\n" )

	if ( ( nTrait > 0 ) and ( math.floor( fNewPop ) % nTrait ) == 0 ) then 

	   print( pPly:GetName() .. " has the trait, and the population is divisible by the trait.\n" )

		if pCity:IsCapital() then

			print( pCity:GetName() .. " is the capital of " .. pPly:GetName() .. "'s empire.\n" )

			if ( ( fNewPop > fOldPop ) and ( fNewPop >= nZenith ) ) then

			   print( pCity:GetName() .. "'s current population is more than its previous population, and its population has reached a new level!" )

				iUnitMostCurrent = fnReturnBestInfantryUnit( pPly, pCity )

				pPly:AddFreeUnit( iUnitMostCurrent, UNITAI_DEFENSE ) --[[ What are the 
				implications of this line? --]]
				print( "Should have free unit unless Irkalla ed up." )
			end
		end
	end
end )

Please ignore all the prints and the unitclasses table up there, I've just been playing around with different stuff.

I think I know what I need to do. I need to get the ID's instead of each thing. Upon using AddFreeUnit, I discovered that if I entered the unitclass, all it would give me was a settler. If I appended the .ID, I got what I was trying for. A GDR. Gonna go make sure everything's yielding ID's.

This image backs up my hypothesis.
1Tql2.png


Yet, when I feed in an ID, it doesn't even realise that I have a spearman UU and just returns the default. Ugh.

For some reason the function's failing where it searches for the unit type, and falls back to return the default unit. To be honest, I am SO over this right now... My mind's fried and I'm just tired of thinking about it. I can't get anything done, I'm just gonna take a break for a little while.
 
Back
Top Bottom