Re: (PR#10694) AI Builds Doomed Ferries and Passengers

"Benedict Adamson" <[email protected]> Thu, 30 Dec 2004 15:23:47 -0800
Newsgroups gmane.games.freeciv.ai
Message-ID <[email protected]>
<URL: http://bugs.freeciv.org/Ticket/Display.html?id=10694 >

I've implemented the change to the AI bodyguard and city guard code I 
outlined in early November. The attached patch is applicable to the CVS 
development version of 2004-12-29. This is a large change, so it will of 
course require review and amendment before being committed. I welcome 
your comments.

The patch incorporates Per's movemap patch. See PR#9610.
    http://bugs.freeciv.org/Ticket/Display.html?id=9610
I have amended the movemap code, to fix some memory leaks and to use a 
specvec for holding the unit IDs, rather than use a speclist of unit 
points, to prevent some dangling pointers.

This patch implements the improvements suggested by Per for PR#9887.
    http://bugs.freeciv.org/Ticket/Display.html?id=9887

Unlike the existing code, this code will assign guards to ferries (but 
only Cruisers are suitable in the default rule-set) and non military 
units, and often provides escorts for vulnerable attack units (escorting 
a Cannon with a Musketeer, for example) even when attacking overseas. It 
could, in theory, assign escorts for air units. It removes much of the 
cruft from the ai_military_findjob function, including all of the 
stay_and_defend function.

The code reassigns unit and city guards every turn. It tries to assign 
the best guards to the most vulnerable units and cities. It will assign 
several guards to a city, if necessary. The code therefore rearranges 
the guards of well connected cities (connected by rail roads or short 
roads) to provide the best possible defence. It tries to rationalise the 
home cities of city guards, so the units guarding a city have that as 
their home city.

The code computes 'danger' ratings for each of the AI's cities and 
units. It computes 'menace' ratings for each of the enemy units of the 
AI. Both ratings are carefully computed as the expected-loss in shields, 
rather than arbitrary WAG numbers, so sensible comparisons with unit 
costs are possible when making decisions. I assume these ratings could 
be useful to the code that decides whether to build defensive and hunter 
units, but it currently does not use them.

This code implements defensive attacks. That is, the AI's attack units 
will preferentially attack units that are menacing the AI's units. This 
is implemented by altering the stack_cost function used by the attack 
and rampage code for selecting targets. The amended function includes 
the (probable, estimated) value of our units saved by destroying the 
menacing unit. (It is a little more complex than that; see the code for 
the details).

This patch changes the semantics of the flag punit->ai.bodyguard == 
BODYGUARD_WANTED. This now indicates that the unit is on a dangerous 
mission, meaning that even if the unit is not currently in danger, it 
soon will be. The code assigns spare guards (those not required to deal 
with immediate dangers) to unguarded units flagged this way.

The code records which units belong to the 'garrison' of each city. The 
garrison are the units that have committed to defending the city. That 
includes defenders units that will arrive later in the turn, and 
excludes units that are merely passing through.

The code adds some convenience functions to unittype.[hc] and 
diplomats.[hc].

The increase in CPU time for this patch is modest. Most is in the 
movemap computation and the function aiguard_findcharge (about 3% each). 
We intend to use the movemap for other things, which will allow us to 
remove some dangermap computations and thus reduce the nett cost. We 
might be able to reduce the cost of aiguard_findcharge by improving the 
ai_military_findjob function to call it less often.

Note that this code tries to do the best it can with the available 
defensive units. It will still perform poorly if the AI fails to build 
enough defenders. Fixing that is the next stage of stopping the AI from 
building doomed attack units.
PR10694-3,1276,2004-12-29.patch (text/x-patch, 133.3 KB) - not displayed