AI attacking city question

I was asked to explain what caused the city capture bug. Here's what debugging for Civ can look like: :D

In the CvUnit::canMoveInto function, two times the following is used:

!(canCoexistWithEnemyUnit(NO_TEAM))

This checks if the unit is invisible. If NO_TEAM is given as a parameter, this function returns true if the unit is alwaysInvisible, but return false in all other cases.

Recently I gave the Probe Team its own invisibility type rather keeping it alwaysInvisible. This had IIRC as an unforeseen consequence that probe teams could no longer move into cities. To solve that, I changed the above piece of code to:

!(ePlotTeam != NO_TEAM ? canCoexistWithEnemyUnit(ePlotTeam) : canCoexistWithEnemyUnit(NO_TEAM))

Hmm, I just saw you might as well write that shorter as:

!(canCoexistWithEnemyUnit(ePlotTeam != NO_TEAM ? ePlotTeam : NO_TEAM))

In any case, if the plot the unit wants to move to is owned, then this checks if the unit is invisible to the owner of the plot, rather than just checking if the unit is alwaysInvisible. This allowed invisible probe teams to move into cities.

Now what some players have recently discovered, is that with this code invisible combat units still attack cities the normal way if they specifically target them, but if invisible combat units are on a path which moves them *through* a city, or any stack of enemy units for that matter, and happen to end their turn on the city plot, then all enemy units get bumped from that plot. In this case the city doesn't get captured though. You just have an invisible unit sitting in an otherwise empty city whch still belongs to the enemy.

The case of a unit moving out of a transport is a special subcase. Units in transports are invisible. So they can move into cities by the same bug as normally invisible units. Once they're in the city and out of the transport, they lose their invisibility though. I haven't checked what code exactly causes this difference, but if such a visible unit moves into a city (after it has bumped out the other units while still being considered invisible), it captures the city.

I have solved this by simply adding " && !canFight()" to the relevant piece of code:

!(ePlotTeam != NO_TEAM && !canFight() ? canCoexistWithEnemyUnit(ePlotTeam) : canCoexistWithEnemyUnit(NO_TEAM))

So combat units once again can't share plots with enemy units. Probe teams, having a combat strength of zero, still can. Problem solved.
 
Back
Top Bottom