Is AI for Merchant hard-coded for Venice type civs?

Craig_Sutter

Deity
Joined
Aug 13, 2002
Messages
2,773
Location
Calgary, Canada
I have two civilizations in my scenario that are derived from the no-annex particulars for Venice. They do not have "Merchant of Venice" type units. Instead, I generate ordinary Merchant units using lua in those situations where the civilizations might otherwise generate a Merchant of Venice.

Only thing is, these units do not do anything. They simply move around without either doing a trade mission nor creating an custom house. I think the same problem arises with any Merchant generated by great person points as well.

Is the AI for Venice type civs merchants hard-coded in some way? Is it the civ itself that not correctly using the merchants?

Anyway, here is the lua code I use (I don't think there should be any problem with it...)

Code:
--this is to add merchants at adoption of a certain policy

function OnPolicyAdopted(playerID, policyID)

	if policyID == (GameInfo.Policies["POLICY_COLLECTIVE_RULE"].ID) then
	
	local pPlayer = Players[playerID];
			
		if pPlayer:IsEverAlive() then 

			if GameInfo.Civilizations.CIVILIZATION_PERSIA.ID == pPlayer:GetCivilizationType() or 
			GameInfo.Civilizations.CIVILIZATION_FRANCE.ID == pPlayer:GetCivilizationType() then
						
			local pCity = pPlayer:GetCapitalCity()
			local pPlot = pCity:GetCityIndexPlot();
			local Spawnunit;
			local iSpawnX = pPlot:GetX();
			local iSpawnY = pPlot:GetY();

			Spawnunit = pPlayer:InitUnit(GameInfoTypes["UNIT_MERCHANT"], iSpawnX, iSpawnY, UNITAI_MERCHANT );
			print (pCity:GetName() ,"...is spawning a Merchant from policy... ");
				
			end
		end
	end
end

GameEvents.PlayerAdoptPolicy.Add(OnPolicyAdopted);


--this is to add merchants at acquisition of a certain technology

function OnTechResearched(team, tech, change)

local pPlayer
local pPlayerTeamID

--local pPlayerTeam

if	(tech == (GameInfoTypes["TECH_OPTICS"])) then

	for playerID=0, GameDefines.MAX_MAJOR_CIVS-1 do

	local pPlayer = Players[playerID];

		if pPlayer:IsEverAlive() then 

			if (GameInfo.Civilizations.CIVILIZATION_PERSIA.ID == pPlayer:GetCivilizationType()) or 
			(GameInfo.Civilizations.CIVILIZATION_FRANCE.ID == pPlayer:GetCivilizationType()) then
	
			local pPlayerTeamID = pPlayer:GetTeam()
			
				if	pPlayerTeamID == team then
						
				local pCity = pPlayer:GetCapitalCity()
				local pPlot = pCity:GetCityIndexPlot();
				local Spawnunit;
				local iSpawnX = pPlot:GetX();
				local iSpawnY = pPlot:GetY();

				Spawnunit = pPlayer:InitUnit(GameInfoTypes["UNIT_MERCHANT"], iSpawnX, iSpawnY, UNITAI_MERCHANT, DIRECTION_NORTHWEST );
				print (pCity:GetName() ,"...is spawning a Merchant from technology... ");
				end
			end
		end
	end
end
end

GameEvents.TeamTechResearched.Add(OnTechResearched);

Anyone know what could be affecting the merchants?
 
I've sometimes seen great people of other civilizations wondering around aimlessly, so I wonder if it's just the effects of an AI thing.

By the way, you don't need to go down the lua route for this. I've found out, in my own experiments for my mod, that as long as you add to the trait both:

<NoAnnexing>true</NoAnnexing>

and also

<FreeUnit>UNITCLASS_MERCHANT</FreeUnit>
<FreeUnitPrereqTech>TECH_OPTICS</FreeUnitPrereqTech>

then the free unit given with the collective rule policy is the free unit you state above. Perhaps you could give them something else other than a merchant?
 
I am thinking I will give a unit other than merchant... the French will get a great writer. My HRE will get an engineer I think. Still leaves the problem of merchants that arise outside of the trait... may have to kill/replace those with lua.

Thanks for the xml code... missed that. Still need lua for the policy though.
 
Still need lua for the policy though.

Why? If it's for the policy that would normally get the settler, it will automatically give it the unit you've assigned as your free unit with x tech. Therefore you'd get two of the same unit, one at Collective Rule and one when the tech is discovered.
 
I made the suggested changes to my scenario... however, while the merchant unit appears with the appropriate technology (optics), it is not arriving when the collective rule policy is chosen.

I will do further testing on this as a player... I have just been watching on autoplay, so I cannot confirm this definitively. Later today, I'll do a test using IGE. Give myself culture and but policies until collective rule... that'll prove it one way or the other.
 
I tested it...

While the xml generates a merchant with the Optics tech, it did not generate one with the collective rule policy.

The relevant xml:

Code:
<Traits>
		<Row>
			<Type>TRAIT_CITY_OF_LIGHT</Type>
			<Description>TXT_KEY_TRAIT_CITY_OF_LIGHT</Description>
			<ShortDescription>TXT_KEY_TRAIT_CITY_OF_LIGHT_SHORT</ShortDescription>
			<NoAnnexing>true</NoAnnexing>
			<FreeUnit>UNITCLASS_MERCHANT</FreeUnit>
			<FreeUnitPrereqTech>TECH_OPTICS</FreeUnitPrereqTech>
		</Row>
	</Traits>

	<Trait_NoTrain>
		<Row>
			<TraitType>TRAIT_CITY_OF_LIGHT</TraitType>
			<UnitClassType>UNITCLASS_SETTLER</UnitClassType>
		</Row>
	</Trait_NoTrain>

So it looks like I'll have to reinstitute the lua function in part...

Which still leaves the question in the OP :)... why aren't my Merchants doing anything? As a human, I can use them normally, but the AI just has them sitting around.

I can direct the AI to use them (via lua), I suppose... or I can generate a different unit than Merchant.

Still, I am curious about the matter... is there something about the AI for the one-city civs?
 
That's exactly the same code I'm using for Ugarit and it works for them. Their unique unit is the diplomat, who has been coded as a separate unit from the Merchant of Venice, but actually uses the same graphics and essentially does the same thing.
 
Yes... it works for Merchant clones. I think that the tech xml would not even be necessary in that case as specified in the linked post. As I understand it, the game looks for a UU with certain abilities specific to the Merchant of Venice (your Ugarit unit has these) and uses that UU in the Collective Rule policy. The Free Unit entry does not have any relevance in such case (again, as far as I know).

My merchant, being the generic one, does not have the Merchant of Venice characteristics that prompt the game to chose the unit under the policy.

The way to test this, of course, would be to comment out the free unit entries for your trait in the xml. Then one could add culture artificially via firetuner (active player tab) until the appropriate policy can be chosen, and then see if your unit is spawned. I suspect it would be given the discussion in the other thread.

Of course, no need for testing as the trait works as you wish in your mod. I think if you put another unit in place of the Merchant clone UU, though, that that unit would no longer spawn with the policy, though the tech would still spawn it. Make the Khan the UU, for example, and place it as a free unit with tech optics.

Further... I'll put Merchant of Venice as UU for France, and not put it with the tech... I think it will still spawn.

I'll let you know when I test.

Thanks.
 
I had tested this earlier in the process; it wasn't until I had the xml exactly as I stated in post #2 above that it actually give the diplomat upon accepting "Collective rule" as a policy; previously, it simply gave me no free unit at all.
 
Well, I've tested as follows...

First, the xml... note commented out lines for FreeUnit and FreeUnitPrereqTech, but NoAnnexing is true...

Code:
<Traits>
		<Row>
			<Type>TRAIT_CITY_OF_LIGHT</Type>
			<Description>TXT_KEY_TRAIT_CITY_OF_LIGHT</Description>
			<ShortDescription>TXT_KEY_TRAIT_CITY_OF_LIGHT_SHORT</ShortDescription>
			<NoAnnexing>true</NoAnnexing>
			<!--<FreeUnit>UNITCLASS_MERCHANT</FreeUnit>
			<FreeUnitPrereqTech>TECH_OPTICS</FreeUnitPrereqTech>-->
		</Row>
	</Traits>

	<Trait_NoTrain>
		<Row>
			<TraitType>TRAIT_CITY_OF_LIGHT</TraitType>
			<UnitClassType>UNITCLASS_SETTLER</UnitClassType>
		</Row>
	</Trait_NoTrain>

Also, note the unique unit for France is the Merchant of Venice...

Code:
<Civilization_UnitClassOverrides>
		<Delete UnitType="UNIT_FRENCH_MUSKETEER"/>
		
		<Row>
			<CivilizationType>CIVILIZATION_FRANCE</CivilizationType>
			<UnitClassType>UNITCLASS_MERCHANT</UnitClassType>
			<UnitType>UNIT_VENETIAN_MERCHANT</UnitType>
		</Row>
		
</Civilization_UnitClassOverrides>

The result of my test (adding culture incrementally and purchasing policies to get to Collective Rule) was that a free Merchant of Venice spawned in Paris at Collective Rule. I then gave France the Optics tech... as expected, no Merchant of Venice was spawned (since it was not in the trait).

I think the key to whether a unit spawns or not with Collective Rule is if it has the CanBuyCityState tag... lacking this, it will not spawn, although it would still spawn with Optics if that is set.

I think you may find this to be the case if you comment out that line in your "Diplomat". I have no clone Merchant of Venice on which to test this.

Thanks for the advice and input on this matter... using the FreeUnit xml saves some lua and will be useful in future. I think I will need to use lua for the remainder of the trait.

And with the problems I'm finding with how the generic Merchants are being controlled by the AI or the one-city civs, I'm going to have to think about what to do to rectify the situation as this seems to affect all Merchants, not just those spawned by Trait.

The Merchant AI for the Merchant of Venice is the same as the generic Merchant, so I think the AI governing the Merchant's behaviour is coming from hard-coding as well since there is the addition of a mission for the Merchant of Venice (city state annexation) to the normal ones for Merchants. The trait is causing hard-coded Merchant of Venice AI to be misapplied to the generic Merchant and this is causing problems as the generic Merchant cannot full-fill all the missions the hard-coding requires. At least, that is what I suppose is happening.

I'm hoping someone familiar with the DLL can interject at this point and tell me that my suppositions regarding AI behaviour are correct :) .

I may have to create a clone of the Merchant AI with a new AI-tag so that the hard-coding will not recognize the unit as a Merchant for the purpose of Merchant of Venice hard-coded AI. But this might very well be beyond my abilities and may not work depending on how the hard-coding is activated.

Alternatively, I can replace all merchants that spawn with another great person via lua... it means that my one city civs will not have any merchants, but I might have to live with that.
 
There's nothing hard-coded in the DLL specifically for Merchant of Venice units. The AI works on the attributes of the unit (can buy city state, build custom house etc, etc) when making decisions. It's just that the AI is generally bad at using GP
 
Back
Top Bottom