[LUA(/DLL?)] Problem with IsCapitalConnectedToCity

Castar

Warlord
Joined
May 26, 2007
Messages
100
I was looking for a way of checking whether a city-state has a road connection to a player's capital. I stumbled upon the IsCapitalConnectedToCity lua method, but unless I'm missing something, this method does not seem to work.

My LUA Code:
Code:
function InfluenceForRoads( iPlayer )
	local pPlayer = Players[iPlayer];
	if (pPlayer:IsAlive() and pPlayer:GetCivilizationType() == iCivilizationRome) then
		print("InfluenceForRoads: this civilization is Rome");
		for i, pOtherPlayer in pairs(Players) do
			if (pOtherPlayer ~= pPlayer and pOtherPlayer:IsMinorCiv()) then
				local pCity = pOtherPlayer:GetCapitalCity();
				print("InfluenceForRoads: other Civ is a minor Civ with capital: " .. pCity:GetName());
				if (pPlayer:IsCapitalConnectedToCity(pCity)) then
					print("InfluenceForRoads: We have a road connection. Friendship before change: " .. pOtherPlayer:GetMinorCivFriendshipWithMajor(pPlayer:GetID()));
					pOtherPlayer:ChangeMinorCivFriendshipWithMajor(pPlayer:GetID(), 1);
				end
			end
		end
	end
end
GameEvents.PlayerDoTurn.Add(InfluenceForRoads);

(the intent is to give Rome +1 influence/turn with every City State that has a road connection to Rome's capital)

The relevant part of lua.log (I have a road connection to Samarkand in this particular game):
Code:
[5583.057] CvX_Rome_Events: InfluenceForRoads: this civilization is Rome
[5583.057] CvX_Rome_Events: InfluenceForRoads: other Civ is a minor Civ with capital: Byblos
[5583.057] CvX_Rome_Events: InfluenceForRoads: other Civ is a minor Civ with capital: Ormus
[5583.057] CvX_Rome_Events: InfluenceForRoads: other Civ is a minor Civ with capital: Sidon
[5583.057] CvX_Rome_Events: InfluenceForRoads: other Civ is a minor Civ with capital: Kuala Lumpur
[5583.057] CvX_Rome_Events: InfluenceForRoads: other Civ is a minor Civ with capital: Belgrade
[5583.057] CvX_Rome_Events: InfluenceForRoads: other Civ is a minor Civ with capital: Genoa
[5583.057] CvX_Rome_Events: InfluenceForRoads: other Civ is a minor Civ with capital: Sydney
[5583.057] CvX_Rome_Events: InfluenceForRoads: other Civ is a minor Civ with capital: Quebec City
[5583.057] CvX_Rome_Events: InfluenceForRoads: other Civ is a minor Civ with capital: Wittenberg
[5583.057] CvX_Rome_Events: InfluenceForRoads: other Civ is a minor Civ with capital: Milan
[5583.057] CvX_Rome_Events: InfluenceForRoads: other Civ is a minor Civ with capital: Hong Kong
[5583.057] CvX_Rome_Events: InfluenceForRoads: other Civ is a minor Civ with capital: Monaco
[5583.057] CvX_Rome_Events: InfluenceForRoads: other Civ is a minor Civ with capital: Lhasa
[5583.057] CvX_Rome_Events: InfluenceForRoads: other Civ is a minor Civ with capital: Tyre
[5583.057] CvX_Rome_Events: InfluenceForRoads: other Civ is a minor Civ with capital: Samarkand
[5583.057] CvX_Rome_Events: InfluenceForRoads: other Civ is a minor Civ with capital: M'banza-Kongo

As you can see, it never gets to the next print statement (even though it should for Samarkand) as the IsCapitalConnectedToCity always return false.

I went digging in the DLL a bit, and found that CvLuaPlayer::lIsCapitalConnectedToCity calls CvPlayer::IsCapitalConnectedToCity, which calls CvPlayer::IsCityConnectedToCity, which calls GC.getRouteFinder().GeneratePath with the X,Y coordinates of both cities. Putting a logging statement in there, I get:
Code:
CvPlayer::IsCityConnectedToCity|Called GC.getRouteFinder().GeneratePath(iXstart = 37, iYstart = 26, iXdest = 42, iYDest = 22, iInfo = -1073741824, bReuse = false). Result = false
(Rome is at (37,26), Samarkand is at (42,22)).

So it seems to correcly get both cities, but the pathfinder then determines that there is no road between them, even though there is. My knowledge of the A* algorithm is pretty much nonexistent, so I have no clue what is going on in CvAstar::GeneratePath that could cause this.

Anyone?
 
That's the same method used to determine if there is an old style trade route from the city to the capital, and that only works through land owned by other players if you have an Open Borders agreement with the major or are allied with the minor that owns the tile, so I suspect if you ally with Samerkand you'll discover your code starts working.

My route finding utils should work regardless of open borders / ally status - see http://forums.civfanatics.com/showthread.php?t=426472 and http://www.picknmixmods.com/mods/718674fb-875f-4d9b-83fd-c8fe1ce2fb8c/mod.html
 
Code:
[11512.670] CvX_Rome_Events: InfluenceForRoads: this civilization is Rome
[11512.670] CvX_Rome_Events: InfluenceForRoads: other Civ is a minor Civ with capital: Byblos
[11512.670] CvX_Rome_Events: InfluenceForRoads: other Civ is a minor Civ with capital: Ormus
[11512.670] CvX_Rome_Events: InfluenceForRoads: other Civ is a minor Civ with capital: Sidon
[11512.670] CvX_Rome_Events: InfluenceForRoads: other Civ is a minor Civ with capital: Kuala Lumpur
[11512.670] CvX_Rome_Events: InfluenceForRoads: other Civ is a minor Civ with capital: Belgrade
[11512.670] CvX_Rome_Events: InfluenceForRoads: other Civ is a minor Civ with capital: Genoa
[11512.670] CvX_Rome_Events: InfluenceForRoads: other Civ is a minor Civ with capital: Sydney
[11512.670] CvX_Rome_Events: InfluenceForRoads: other Civ is a minor Civ with capital: Quebec City
[11512.670] CvX_Rome_Events: InfluenceForRoads: other Civ is a minor Civ with capital: Wittenberg
[11512.670] CvX_Rome_Events: InfluenceForRoads: other Civ is a minor Civ with capital: Milan
[11512.670] CvX_Rome_Events: InfluenceForRoads: other Civ is a minor Civ with capital: Hong Kong
[11512.670] CvX_Rome_Events: InfluenceForRoads: other Civ is a minor Civ with capital: Monaco
[11512.670] CvX_Rome_Events: InfluenceForRoads: other Civ is a minor Civ with capital: Lhasa
[11512.670] CvX_Rome_Events: InfluenceForRoads: other Civ is a minor Civ with capital: Tyre
[11512.670] CvX_Rome_Events: InfluenceForRoads: other Civ is a minor Civ with capital: Samarkand
[11512.670] CvX_Rome_Events: InfluenceForRoads: We have a road connection. Friendship before change: 93
[11512.670] CvX_Rome_Events: InfluenceForRoads: other Civ is a minor Civ with capital: M'banza-Kongo

It seems you're right. Sort of defeats the purpose of getting influence for road connections :) I was already using your LiveTuner Route panel, I'll see about incorporating the route-finding code into my mod, thanks :)
 
Back
Top Bottom