how to find "most important mil production city"?

davidlallen

Deity
Joined
Apr 28, 2008
Messages
4,743
Location
California
Over in Dune Wars, we are discussing how to implement Mentat units. A Mentat is a human computer, who can be a super-genius in one field. We would like these to be mobile units that are sort of like settled great people, and give a big economic bonus of some kind to the city they are in. Only one mentat per city, and only 3 mentats per civ. There will be like 6-8 different specialties, such as: "Military genius: gives +5 XP to units built in this city", or "Industrial genius: gives +50 hammers to city, recharges after 20 turns".

I want to write some python/sdk code which will select the best city for a particular mentat type to move to. We want to pick the best city and have the mentat move there, without jittering too much from one city to another.

My question is, is there already some AI routine which ranks the cities by "most need of military production"? For example, the National Epic wonder could be placed according to which city has a decent hammer rate already *and* is geographically near a war front. There is no point in putting it in a city which is near the front but has a tiny hammer rate, and no point in putting it in a city with a good hammer rate which is on the other side of the world from any military action.

If there is some routine that rates cities like this, please point me in the direction.
 
No; the AI doesn't do empire-wide priorities like that.

There is code that indicates if it is a good idea to build X, but not "is it better than elsewhere".
 
Are there some specific routines in cvcityai or somewhere else that I could use as a starting point for that?
 
Nope.

As the Civ AI is configured, deciding where a unit of a particular kind should go ends up in the unit AI code. (Cities do store information about what they want workers to do).

What I'd be tempted to do is iterate over the cities, then grab the top-5-by-stats, plus the city you are in, and do some travel-time times usefulness-when-there calculation.

Make sure you defer calculating travel time on a city until you are sure that a short travel time would make it better than just sitting still, and with a limited number of mentats, this shouldn't bog down the AI.

A bias towards sitting still in the city you are already in is a decent way to prevent jittering between cities.
 
Thanks! How can I easily evaluate the "distance from the front" for cities? Some cities are valuable to build new units because they are on the front, some cities are less valuable because they are on the other side of the world.
 
Threat is certainly one component, although a city with a lot of defenders, which is on the front, may not be threatened. Are there routines in cvcityai.cpp to find this?
 
Is distance that much of a problem?

I know when I build my unit factory city, I don't want it to be on the front lines -- I just want it to pump out high quality units. Rarely is my best unit factory city option on the other side of the world.

And as I'm guessing mentats get produced in a way that is proportional to your local oomph, the mentat going to a nearby city would make an emergent bias towards it happening in your core empire (where it is probably most guarded).
 
The AI code for where to build national wonders (basically what you're talking about) has a pretty local perspective. I've been planning on rewriting this code into a centralized version so that values between cities could be directly compared but have been distracted by a series of other issues (efficiency being the big one). Frankly, if you make these national wonders instead of units, the AI will handle them reasonably right away (as long as the benefits are all expressible in terms of XML tags buildings already have).

There are a bunch of ways to figure out if a city is a "reasonable" location for one of these though if you go the unit route. You can take a look at CvCityAI::AI_specialistValue which is what determines were GP go now, but to particular functions you'll be interested in:

1) CvCity::findYieldRateRank(YIELD_PRODUCTION) - can all find ranks for other yield types, this is the main way of determining if it's a good or bad production/economy city.

2) CvCity::getMilitaryProductionModifier()

3) CvCity::getSpecialistFreeExperience()

4) CvCity::getDomainFreeExperience(DOMAIN_LAND)

5) CvCityAI::AI_playerCloseness( eEnemyPlayer, DEFAULT_PLAYER_CLOSENESS ) - If > 0, then city is near a particular player. Using the default distance (6) means it shouldn't slow down the game much at all.

6) CvCityAI::AI_cityThreat() - Calls closeness for all other players, weights by attitude and warplan. Also adds a bunch of stuff which is less appropriate ... isCoastal, world wonder count, etc.

7) getCommerceRateModifier(COMMERCE_CULTURE)



In terms of removing jitter, the CvUnitAI.cpp logic solves this in two ways:

1) Path length has a modifier effect on city value like 1 / (5 + path length to city), so the unit will never switch to go back to a city its now further away from.

2) When you push a move mission to the unit (well, the unit's group ...), give it MISSION_MOVE_TO to the city's plot. The unit will keep moving until it gets there without reevaluating.

Enabling Python callbacks for unit movement decisions will slow your mod down noticeably. It's not nearly as bad as some callbacks (like can move into ...) which can get called millions of times per turn, but with a lot of units on the map you'll notice. Keep an eye on it and consider moving to SDK code if it gets too noticeable.
 
Frankly, if you make these national wonders instead of units, the AI will handle them reasonably right away (as long as the benefits are all expressible in terms of XML tags buildings already have).

Thanks for the pointers to certain functions. I will definitely investigate.

I did not go into the overall design of the mentat unit here; there is more detail in the DW subforum at this thread. The basic idea is that the mentat unit is a national unit with a limit of three; each mentat can have one specialty which gives the city a big bonus; and only one mentat is allowed per city. Suppose there are five bonus types (military, industrial, science, espionage, diplomatic). You might build two military and one espionage, put one military in your capitol and one in your highest production city, and put the espionage guy in whichever city seems to have the worst problem with enemy infiltrators. This is a little different from national wonders, where the number of buildings with the same bonus has a constant limit. One key challenge, which we have not solved yet, is to come up with a set of bonus types which are *not* directly implementable in existing buildings or specialist abilities.

The AI code I write will have two challenges, first figuring out which mentats to build (which bonuses are most needed), and second, given a set of mentats, what is the optimal distribution among cities. I will certainly investigate subroutines from the national wonder location code. But the application is sufficiently different that I do not think we should just turn them into national wonders.
 
Top Bottom