[LUA] Restricting faith purchase to era

Xenomorph

King
Joined
Oct 7, 2007
Messages
972
I'm trying to add a new unique unit that can always be purchased with faith, regardless of whether or not Holy Warriors was chosen for the religion. Initially I set RequiresFaithPurchaseEnabled to false, but this allow any civilization to get the unit. I have since repurposed an existing LUA script to limit who can build it by civilization, but now I need to limit the unit to a specific era or better yet, the existence of a specific tech. Here's my current code.
Code:
print("This is the 'Unique Unit - War Captain Guard' mod script.")

local iBuildWarGuard = GameInfo.Units["UNIT_UU_WAR_CAPTAIN_GUARD"].ID
local iCivMayan = GameInfo.Civilizations["CIVILIZATION_MAYA"].ID

GameEvents.CityCanConstruct.Add(function(iPlayerId, iCityId, iUnitType) 
  if (iUnitType == iBuildWarGuard) then
    local pPlayer = Players[iPlayerId]

	if (pPlayer:GetCivilizationType() == iCivMayan) then
	    return true
	else
	  return false
	end
  end
  return true
end)
I haven't tested this yet, since I need to restrict it by tech and era first, i.e. not available until Steel and not available after the Renaissance era. I'm not familiar with all the lua script tags available, so I don't really know where to start.
 
As for era, you could make an upgrade for the unit... for example, it the unit is "Crusader Knight" have it upgrade to lancer.

I did the following to create a religious unit using xml:

The unit:

Code:
<Units>
		<Row>
			<Class>UNITCLASS_FOOT_KNIGHT</Class>
			<Type>UNIT_FOOT_KNIGHT</Type>
			<PrereqTech>TECH_STEEL</PrereqTech>
			<Combat>21</Combat>
			<Cost>120</Cost>
			<FaithCost>240</FaithCost>		
			<Moves>2</Moves>
			<HurryCostModifier>20</HurryCostModifier>
			<CombatClass>UNITCOMBAT_MELEE</CombatClass>
			<Domain>DOMAIN_LAND</Domain>
			<DefaultUnitAI>UNITAI_ATTACK</DefaultUnitAI>
			<Description>TXT_KEY_UNIT_FOOT_KNIGHT</Description>
			<Civilopedia>TXT_KEY_CIV5_FOOT_KNIGHT_TEXT</Civilopedia>
			<Strategy>TXT_KEY_CIV5_FOOT_KNIGHT_STRATEGY</Strategy>
			<Help>TXT_KEY_CIV5_FOOT_KNIGHT_HELP</Help>
			<MilitarySupport>false</MilitarySupport>
			<MilitaryProduction>true</MilitaryProduction>
			<Pillage>true</Pillage>
			<ObsoleteTech>TECH_METALLURGY</ObsoleteTech>
			<GoodyHutUpgradeUnitClass>UNITCLASS_MUSKETMAN</GoodyHutUpgradeUnitClass>
			<AdvancedStartCost>25</AdvancedStartCost>
			<XPValueAttack>3</XPValueAttack>
			<XPValueDefense>3</XPValueDefense>
			<Conscription>3</Conscription>
			<UnitArtInfo>ART_DEF_UNIT_LONGSWORDSMAN</UnitArtInfo>
			<IconAtlas>UNIT_ATLAS_1</IconAtlas>
			<UnitFlagIconOffset>35</UnitFlagIconOffset>
			<PortraitIndex>36</PortraitIndex>
			<MoveRate>HEAVY_BIPED</MoveRate>
		</Row>
	</Units>
	<Unit_AITypes>
		<Row>
			<UnitType>UNIT_FOOT_KNIGHT</UnitType>
			<UnitAIType>UNITAI_ATTACK</UnitAIType>
		</Row>
		<Row>
			<UnitType>UNIT_FOOT_KNIGHT</UnitType>
			<UnitAIType>UNITAI_DEFENSE</UnitAIType>
		</Row>
	</Unit_AITypes>
	<Unit_ClassUpgrades>
		<Row>
			<UnitType>UNIT_FOOT_KNIGHT</UnitType>
			<UnitClassType>UNITCLASS_RIFLEMAN</UnitClassType>
		</Row>
	</Unit_ClassUpgrades>
	<Unit_Flavors>
		<Row>
			<UnitType>UNIT_FOOT_KNIGHT</UnitType>
			<FlavorType>FLAVOR_OFFENSE</FlavorType>
			<Flavor>13</Flavor>
		</Row>
		<Row>
			<UnitType>UNIT_FOOT_KNIGHT</UnitType>
			<FlavorType>FLAVOR_DEFENSE</FlavorType>
			<Flavor>7</Flavor>
		</Row>
	</Unit_Flavors>
	<Unit_FreePromotions>
		<Row>
			<UnitType>UNIT_FOOT_KNIGHT</UnitType>
			<PromotionType>PROMOTION_SHOCK_1</PromotionType>
		</Row>
		<Row>
			<UnitType>UNIT_FOOT_KNIGHT</UnitType>
		<PromotionType>PROMOTION_SIEGE</PromotionType>
	</Row>
	</Unit_FreePromotions>
	<Unit_ResourceQuantityRequirements>
		<Row>
			<UnitType>UNIT_FOOT_KNIGHT</UnitType>
			<ResourceType>RESOURCE_URANIUM</ResourceType>
		</Row>
	</Unit_ResourceQuantityRequirements>
	<UnitClasses>
		<Row>
			<Type>UNITCLASS_FOOT_KNIGHT</Type>
			<Description>Foot Knight</Description>
			<DefaultUnit>UNIT_FOOT_KNIGHT</DefaultUnit>
		</Row>
	</UnitClasses>
	<Unit_BuildingClassRequireds>
		<Row>
			<UnitType>UNIT_FOOT_KNIGHT</UnitType>
			<BuildingClassType>BUILDINGCLASS_MOSQUE</BuildingClassType>
		</Row>
	</Unit_BuildingClassRequireds>
	<Language_en_US>
		<Row Tag="TXT_KEY_UNIT_FOOT_KNIGHT">
			<Text>Order FootKnight</Text>
		</Row>
		<Row Tag="TXT_KEY_CIV5_FOOT_KNIGHT_HEADING">
			<Text>Order FootKnight</Text>
		</Row>
		<Row Tag="TXT_KEY_CIV5_FOOT_KNIGHT_TEXT">
			<Text>
				As a member of the Great Crusader Armies, the Order Foot Knight was one of the most feared units of the early 2nd millenium.  This large army of Knights, warriors, and mariners was formed over a series of attacks on the Holy Land at the behest of the Pope in the early part of the millenium, where they bolstered their numbers and refined their tactics. The armies briefly retook Jerusalem but lost it later to muslim armies.  Other wars were carried on by Crusaders in various parts of Europe... by the Teutonic knights in norther Europe and various other military orders went on Crusade against various heresies that arose in mediveal times.
			</Text>
		</Row>
		<Row Tag="TXT_KEY_CIV5_FOOT_KNIGHT_HELP">
			<Text>Strong, front-line land Unit of the Medieval Era that specializes in attacks on cities. Only cities with an Order Chapter House may build it. They cost no maintenance as the Order makes certain of their needs. </Text>
		</Row>
		<Row Tag="TXT_KEY_CIV5_FOOT_KNIGHT_STRATEGY">
			<Text>The Order Foot Knight is a special religious Unit. This Unit is has the shock and city attack promotions allowing it to be a superior city attack unit.</Text>
		</Row>
	</Language_en_US>

The building (updated mosque rather than new type):

Code:
<Buildings>
		
		<Update>
			<Where Type="BUILDING_MOSQUE"/>
			<Set ArtDefineTag="ART_DEF_BUILDING_BARRACKS" Happiness="0" IconAtlas="BW_ATLAS_1" PortraitIndex="5" PrereqTech="TECH_CHIVALRY" />
		</Update>
</Buildings>
<Building_Flavors>
		<Update>
			<Where Type="BUILDING_MOSQUE" FlavorType="FLAVOR_HAPPINESS"/>
			<Set Flavor="0" />
		</Update>
		<Row>
			<BuildingType>BUILDING_MOSQUE</BuildingType>
			<FlavorType>FLAVOR_MILITARY_TRAINING</FlavorType>
			<Flavor>16</Flavor>
		</Row>
</Building_Flavors>
<Building_ResourceQuantity>
		<Row>
			<BuildingType>BUILDING_MOSQUE</BuildingType>
			<ResourceType>RESOURCE_URANIUM</ResourceType>
			<Quantity>2</Quantity>
		</Row>
	</Building_ResourceQuantity>	
<Language_en_US>
		<Update>
			<Where Tag="TXT_KEY_BUILDING_MOSQUE"/>
			<Set Text="Order Chapter House" />
		</Update>
		<Update>
			<Where Tag="TXT_KEY_BELIEF_MOSQUES"/>
			<Set Text="Use [ICON_PEACE] Faith to purchase Order Chapter Houses (+3 [ICON_PEACE] Faith, +2 [ICON_CULTURE] Culture). Can purchase Crusader units." />
		</Update>
		<Update>
			<Where Tag="TXT_KEY_BELIEF_MOSQUES_SHORT"/>
			<Set Text="Chapter Houses" />
		</Update>	
</Language_en_US>

The resource (updated uranium rather than new one):

Code:
<Resources>
		<Update>
			<Set TechReveal="NULL" TechCityTrade="NULL" Description="TXT_KEY_RESOURCE_FERVOR" Civilopedia="TXT_KEY_CIV5_RESOURCE_FERVOR_TEXT" Help="TXT_KEY_RESOURCE_FERVOR_HELP" ArtDefineTag="" ArtDefineTagHeavy="" AltArtDefineTag="" AltArtDefineTagHeavy="" IconAtlas="CIV_COLOR_ATLAS_VIKING"  PortraitIndex="19" />
			<Where Type="RESOURCE_URANIUM"/>
		</Update>
		<Update>
			<Set ResourceUsage="1" AITradeModifier="0" AIObjective="0" PlacementOrder="0" ConstAppearance="0" MinAreaSize="0" MaxLatitude="0" RandApp1="0" RandApp2="0" Unique="0" Hills="false" Flatlands="false" NoRiverSide="false" IconString="[ICON_RES_FERVOR]" />
			<Where Type="RESOURCE_URANIUM"/>
		</Update>
		<!--<Row>
			<Type>RESOURCE_URANIUM</Type>
			<Description>TXT_KEY_RESOURCE_FERVOR</Description>
			<Civilopedia>TXT_KEY_CIV5_RESOURCE_FERVOR_TEXT</Civilopedia>
			<Help>TXT_KEY_RESOURCE_FERVOR_HELP</Help>
			<ResourceClassType>RESOURCECLASS_MODERN</ResourceClassType>
			<ResourceUsage>1</ResourceUsage>
			<Player>100</Player>
			<IconString>[ICON_RES_FERVOR]</IconString>
			<PortraitIndex>19</PortraitIndex>
			<IconAtlas>CIV_COLOR_ATLAS_VIKING</IconAtlas>
			</Row>-->
	</Resources>
	<Resource_YieldChanges>
		<Delete ResourceType="RESOURCE_URANIUM"/>
	</Resource_YieldChanges>
	<IconFontTextures>
		<Row>
			<IconFontTexture>ICON_FONT_TEXTURE_FERVOR</IconFontTexture>
			<IconFontTextureFile>fervor-fonticons</IconFontTextureFile>
		</Row>
	</IconFontTextures>
	<IconFontMapping>
		<Row>
			<IconName>ICON_RES_FERVOR</IconName>
			<IconFontTexture>ICON_FONT_TEXTURE_FERVOR</IconFontTexture>
			<IconMapping>1</IconMapping>
		</Row>
	</IconFontMapping>
	<Resource_Flavors>
	</Resource_Flavors>
	<Resource_TerrainBooleans>
		<Delete ResourceType="RESOURCE_URANIUM"/>
	</Resource_TerrainBooleans>
	<Resource_FeatureBooleans>
		<Delete ResourceType="RESOURCE_URANIUM"/>
	</Resource_FeatureBooleans>
	<Resource_FeatureTerrainBooleans>
		<Delete ResourceType="RESOURCE_URANIUM"/>
	</Resource_FeatureTerrainBooleans>
	<Resource_QuantityTypes>
		<Delete ResourceType="RESOURCE_URANIUM"/>
	</Resource_QuantityTypes>
	<Improvement_ResourceTypes>
		<Delete ResourceType="RESOURCE_URANIUM"/>
	</Improvement_ResourceTypes>
	<Improvement_ResourceType_Yields>
		<Delete ResourceType="RESOURCE_URANIUM"/>
	</Improvement_ResourceType_Yields>
	<Language_en_US>
		<Row Tag="TXT_KEY_RESOURCE_FERVOR">
			<Text>Fervor</Text>
			<Gender>neuter:an</Gender>
		</Row>
		<Row Tag="TXT_KEY_RESOURCE_FERVOR_HELP">
			<Text>Used for certain religious Units.</Text>
		</Row>
		<Row Tag="TXT_KEY_CIV5_RESOURCE_FERVOR">
			<Text>Fervor</Text>
		</Row>
		<Row Tag="TXT_KEY_CIV5_RESOURCE_FERVOR_TEXT">
			<Text>
				Fervor is a quality associated with warriors who are ardent supporters of certain religious causes.  Certain religious orders will indoctrinate their followers to enter into a battle in the cause of their gods or God.  Often called crusaders, these fighters do battle with great vigor.
			</Text>
		</Row>
	</Language_en_US>


The idea is that a belief creates a building that generates a resource. The resource is needed to build the unit.

You could forgo the resource requirement as the unit can be restricted to require the building, but I thought a resource requirement would limit the number of these units quite nicely. It upgrades to musketman and becomes obsolete with metallurgy.

I used update a lot rather than a new building or resource. My mod has no islam and is premodern, so I simply converted existing entries rather than create new ones. The resource entry, as is, will not work as I have created a resource icon... you would have to create a resource icon for your own use or revert it back to uranium in this example by deleting updates of that section of code.

The unit does show up as a religious unit in civalopedia.

Anyhow, not certain this does what you wish, but it (and several other religious units) are working like this in my own mod.
 
Note:

The above unit is restricted, techwise, in the same way as longswordsman.

It is restricted to a particular religion, one with the belief "Mosques".

As mentioned the unit resource requirement is not needed. There is an entry for a building requirement in the unit tables.

The unit is, I think but am not certain, only purchaseable by faith. I cannot recall the in game result of this... I know it can be purchased by faith but not if it can be purchased with cash.

You could further restrict the unit by making is a UU for a particular civilization.
 
The idea I have is for this to be a second UU for the Mayans that can be built through regular means or purchased by faith without having a religion with Holy Warriors, the Follower benefit that lets you purchase pre-Industrial units with Faith. Essentially, the unique unit can be purchased with Faith independently of any religion.

Your solution requires the spawning of a new building and ties the unit to a religion, two things that I don't want to do. Although...
Can part of a leader's UA be that they get a free building? If so, I might be able to partially use this idea by creating an "invisible" building that the Mayans get for free in their cities, and is required by their UU. The problem I foresee with this solution is in the case of conquered cities and whether or not the "free" building would get added to them.

That being said, I would think it a simpler solution to just have a script that checks to see if you are of Civilization Mayan, have researched Steel, and haven't entered the Renaissance era.

This is part of an effort to give every Civilization two unique units and a unique building.
 
I can purchase the unit with Faith points before I have the technology to give me the building.

With the way Firaxis designed the Faith System adding a "Faith Cost" to units/buildings automaticaly removes all Prereq's bah the "Belief" Prereq. Highly anoying tbh.
 
I'm taking another try at this in BNW, and patch 1.0.1.332 added a exactly the GameEvent that I needed: (Lua) Added GameEvents.CityCanTrain(ownerID, cityID, unitTypeID); (TestAll)

Using this, I think I can get this to work the way I want. The only roadblock I have now is testing the player to see if he has one of two technologies; I don't know the LUA command to test for this. I need to know the GameInfo command to call up the ID of the technology, and the command to see if the player has that technology. Neither the 2Kgames wiki nor the modiki are helpful in solving this.
 
I have edited original post to show complete function...

There are elements re: tech which may be useful for you.

Code:
local modData = Modding.OpenSaveData()
local modFranksGKey = "FranksGHaveSpawned"
local haveSpawnedFranksG = (modData.GetValue(modFranksGKey) == 1)

function SpawnFranksG()

if (haveSpawnedFranksG == false) then

if (Game.GetGameTurnYear() >= 1150) then

    -- Set up FranksG Player

     haveSpawnedFranksG = true
	 modData.SetValue(modFranksGKey, 1)

	 -- find FranksG

	for iPlayer=GameDefines.MAX_MAJOR_CIVS, GameDefines.MAX_CIV_PLAYERS-1, 1 do  

	local pFranksG = Players[iPlayer]

		if (GameInfo.MinorCivilizations.MINOR_CIV_FRANKS.ID == pFranksG:GetMinorCivType()) then
	
		FranksG = pFranksG
		FranksGTeamID = FranksG:GetTeam();
		FranksGTeam= Teams[ FranksG:GetTeam() ]

			-- Enumerate cities
			local cityCount = -1
				
			for pCity in FranksG:Cities() do
   
			cityCount = cityCount + 1
										
				if cityCount < (FranksG:GetNumCities()/3) then
										
				local pPlot = pCity:GetCityIndexPlot();
				local Spawnunit;
				local iSpawnX = pPlot:GetX();
				local iSpawnY = pPlot:GetY();

					if (FranksGTeam:GetTeamTechs():HasTech( GameInfoTypes["TECH_CHIVALRY"] )) then

					Spawnunit = FranksG:InitUnit(GameInfoTypes["UNIT_KNIGHT"], iSpawnX, iSpawnY, UNITAI_ATTACK, DIRECTION_NORTHWEST );
					print (pCity:GetName() ,"...is spawning Knight... ");

					else

					Spawnunit = FranksG:InitUnit(GameInfoTypes["UNIT_HORSEMAN"], iSpawnX, iSpawnY, UNITAI_ATTACK, DIRECTION_NORTHWEST );
					print (pCity:GetName() ,"...is spawning Horseman... ");

					end
				end
			end
		end
	end
end
end
end

Events.ActivePlayerTurnEnd.Add(SpawnFranksG)

I hope you get it working and post the code... my current method is very clunky.

Thanks.
 
Well, this is what I'm going to try.

Code:
local iBuildWarCaptain = GameInfo.Units["UNIT_UU_WAR_CAPTAIN_GUARD"].ID
local iCivMayan = GameInfo.Civilizations["CIVILIZATION_MAYA"].ID

GameEvents.CityCanTrain.Add(function(iPlayerId, iCityId, iUnitType)
	if (iUnitType == iBuildWarCaptain) then
		local pPlayer = Players[iPlayerId]

		if (pPlayer:GetCivilizationType() == iCivMayan) then
			local iTechSteel = GameInfoTypes["TECH_STEEL"]
			local iTechGun = GameInfoTypes["TECH_GUNPOWDER"]
			local pTeam = Teams[pPlayer:GetTeam()]

			if (pTeam:IsHasTech(iTechSteel)) then
				if (pTeam:IsHasTech(iTechGun))
					return false
				else
					return true
			end
		else
			return false
		end
	end

  return true
end)
 
this does not work
Spoiler :
Code:
--------------------------------------------------------------
------------------ BUY COSSACK WITH FAITH --------------------
--------------------------------------------------------------
GameEvents.CityCanTrain.Add(function(iPlayerId, iCityId, iUnitType)
	if (iUnitType == GameInfoTypes.UNIT_RUSSIAN_COSSACK) then
		local player = Players[iPlayerId];
		if (player:GetCivilizationType() == GameInfoTypes.CIVILIZATION_RUSSIA) then
			local mtID = GameInfoTypes.TECH_MILITARY_TRADITION;
			local team = Teams[player:GetTeam()];
			return team:IsHasTech(mtID);
		else
			return false
		end
	end
	return true
end);
(CityCanTrain check is not even fired for the faith purchase)

so the only way to restrict faith purchasing is to create a dummy building producing a dummy resource and make the unit require it.
is it possible to hide such a resource from the UI?
 
Top Bottom