View difference between Paste ID: NRXZCGDQ and NiECPZQP
SHOW: | | - or go back to the newest paste.
1-
function fn_returnBestInfantryUnit( i_city )
1+
--[[-----------------------------------------
2
Name:		 GetCivSpecificUnit
3-
	local i_plyID		= i_city:GetOwner()
3+
Purpose: 	 Get the UnitType for a specific
4-
	local i_gdr		= GameInfo.Units.UNIT_MECH.ID
4+
civ from a UnitClassType.
5-
	local i_inf_mechanized	= GameInfo.Units.UNIT_MECHANIZED_INFANTRY.ID
5+
-------------------------------------------]]
6-
	local i_inf		= GameInfo.Units.UNIT_INFANTRY.ID
6+
function fnGetCivSpecificUnit(pPly, sUnitClass) -- Thanks whoward69
7-
	local i_inf_gw		= GameInfo.Units.UNIT_GREAT_WAR_INFANTRY.ID
7+
8-
	local i_rifleman	= GameInfo.Units.UNIT_RIFLEMAN.ID
8+
	-- BEGIN DEFINES
9-
	local i_musketman	= GameInfo.Units.UNIT_MUSKETMAN.ID
9+
	local sUnitType = nil
10-
	local i_longswordsman	= GameInfo.Units.UNIT_LONGSWORDSMAN.ID
10+
	local sCivType = GameInfo.Civilizations[pPly:GetCivilizationType()].Type
11-
	local i_pikeman		= GameInfo.Units.UNIT_PIKEMAN.ID
11+
	-- END DEFINES
12-
	local i_swordsman	= GameInfo.Units.UNIT_SWORDSMAN.ID
12+
	
13-
	local i_phalanx		= GameInfo.Units.UNIT_SPARTAN_PHALANX.ID  -- Can I get modded units like this?
13+
	-- Loop through civilization-specific UnitClass overrides, id est their unique units, and yield to be returned the proper UnitType. 
14-
	local i_warrior		= GameInfo.Units.UNIT_WARRIOR.ID
14+
	for pOverride in GameInfo.Civilization_UnitClassOverrides{CivilizationType = sCivType, UnitClassType = sUnitClass} do
15
		sUnitType = pOverride.UnitType
16-
	if i_city:CanTrain( i_gdr ) then  -- Is there a better way than this giant if, then, elseif, else statement?
16+
		break
17-
		return i_gdr
17+
18-
	elseif i_city:CanTrain( i_inf_mechanized ) then
18+
19-
		return i_inf_mechanized
19+
	-- If we didn't get anything, yield to be returned the default UnitType for the UnitClass.
20-
	elseif i_city:CanTrain( i_inf ) then
20+
	if (sUnitType == nil) then
21-
		return i_inf
21+
		sUnitType = GameInfo.UnitClasses[sUnitClass].DefaultUnit
22-
	elseif i_city:CanTrain( i_inf_gw ) then
22+
23-
		return i_inf_gw
23+
24-
	elseif i_city:CanTrain( i_rifleman ) then
24+
	-- Give whatever function called this the UnitType we yielded.
25-
		return i_rifleman
25+
	return sUnitType
26-
	elseif i_city:CanTrain( i_musketman ) then
26+
27-
		return i_musketman
27+
28-
	elseif i_city:CanTrain( i_longswordsman ) then
28+
--[[-----------------------------------------
29-
		return i_longswordsman
29+
Name:  		ReturnBestInfantryUnit
30-
	elseif i_city:CanTrain( i_pikeman ) then
30+
Purpose:	Return the best land melee unit
31-
		return i_pikeman 
31+
that a city can build.  
32-
	elseif i_city:CanTrain( i_swordsman ) then
32+
-------------------------------------------]]
33-
		return i_swordsman
33+
function fnReturnBestInfantryUnit( pCity )
34-
	elseif i_city:CanTrain( i_phalanx ) then
34+
35-
		return i_phalanx
35+
	--BEGIN DEFINES
36-
	else
36+
	local pPly = pCity:GetOwner()
37-
		return i_warrior
37+
	
38
	local possibleUnitClasses = { 
39
	GameInfoTypes.UNITCLASS_MECH,
40
	GameInfoTypes.UNITCLASS_MECHANIZED_INFANTRY, 
41-
GameEvents.SetPopulation.Add( function( x_offset, y_offset, f_oldPop, f_newPop )
41+
	GameInfoTypes.UNITCLASS_INFANTRY, 
42
	GameInfotypes.UNITCLASS_GREAT_WAR_INFANTRY, 
43-
	local i_plot			= Map.GetPlot( x_offset, y_offset )
43+
	GameInfoTypes.UNITCLASS_RIFLEMAN, 
44-
	local i_city  			= i_plot:GetPlotCity()
44+
	GameInfoTypes.UNITCLASS_MUSKETMAN, 
45-
	local i_plyID 			= i_city:GetOwner()
45+
	GameInfoTypes.UNITCLASS_LONGSWORDSMAN, 
46-
	local i_unit_mostCurrent	= nil
46+
	GameInfoTypes.UNITCLASS_PIKEMAN, 
47-
	local n_trait			= GameInfo.Traits[i_plyID].UnitPerCapitalGrowths
47+
	GameInfoTypes.UNITCLASS_SWORDSMAN, 
48-
	local i_unitAIType		= GameInfo.UnitAITypes.UNITAI_DEFENSE.ID
48+
	GameInfoTypes.UNITCLASS_SPEARMAN, 
49-
	local n_zenith			= math.floor( i_city:GetHighestPopulation() )
49+
	GameInfoTypes.UNITCLASS_WARRIOR
50
	} -- Thanks whoward69
51-
	if ( n_trait > 0 &&  -- Gotta be a better way then this if/then bollocks
51+
	--END DEFINES
52-
	( math.floor( f_newPop ) % n_trait ) == 0 ) && then 
52+
	
53-
		if i_city:IsCapital() then
53+
	-- Loop through each UnitClassType in the above defined table, see if the city can 
54-
			if ( f_newPop > f_oldPop ) &&
54+
	-- train it's owner's specific UnitType.  Yield to be returned the best land melee 
55-
			iNewPop >= n_zenith then
55+
	-- UnitType the city can build.
56-
				i_freeUnit = fn_returnBestInfantryUnit( i_city )
56+
	for _, iUnitClass in ipairs(possibleUnitClasses) do -- Thanks whoward69
57
		if pCity:CanTrain( fn_GetCivSpecificUnit( pPly, iUnitClass ) ) then
58-
				i_plyID:AddFreeUnit( i_freeUnit, i_unitAIType ) -- What are the implications of this line?
58+
			return fn_GetCivSpecificUnit( pPly, iUnitClass )
59
		end
60
	end
61
62
	-- Uh-oh!
63
	return -1
64
end
65
66
GameEvents.SetPopulation.Add( function( xOffset, yOffset, fOldPop, fNewPop )
67
68
	-- BEGIN DEFINES
69
	local pPlot				= Map.GetPlot( xOffset, yOffset )
70
	local pCity  			= pPlot:GetPlotCity()
71
	local pPly 				= pCity:GetOwner()
72
	local iUnitMostCurrent	= nil
73
	local nTrait			= GameInfo.Traits[pPly].UnitPerCapitalGrowths
74
	local sUnitAIType		= GameInfo.UnitAITypes.UNITAI_DEFENSE.ID
75
	local nZenith			= math.floor( pCity:GetHighestPopulation() )
76
	-- END DEFINES
77
78
	-- On every growth of every city, run through the following list of conditions:
79
	-- 1:  Does the city's owner have the trait, and is its population evenly divisible by the trait?
80
	-- 2:  Is the city a capital?
81
	-- 3:  Has the city grown instead of shrinking, and is this the city's highest population?
82
	-- If all of the above are true, get the best land infantry unit we can, and spawn it at the city.
83
	if ( nTrait > 0 &&
84
	( math.floor( fNewPop ) % nTrait ) == 0 ) && then 
85
		if pCity:IsCapital() then
86
			if ( fNewPop > fOldPop ) &&
87
			fNewPop >= nZenith then
88
				iFreeUnit = fnReturnBestInfantryUnit( pCity )
89
90
				pPly:AddFreeUnit( iFreeUnit, iUnitAIType ) -- What are the implications of this line?
91
			end
92
		end
93
	end
94
end )