How the AI decides what ship type to build

How the AI decides what ship type to build; support/discussion/questions

Moderator: thunderchero

Post Reply
User avatar
DCER
Code Master
Code Master
Posts: 683
Joined: Sat Apr 26, 2008 2:00 am

How the AI decides what ship type to build

Post by DCER »

Each empire has a list of ship types to build for each agenda. Each list can contain up to 36 entries.

Valid agendas are (per the wording in trek.exe and checked indexes):

Normal Expansion (0)
Unopposed Expansion (1)
Normal Consolidation (2)
Defensive Consolidation (3)
Offensive Consolidation (4)
Cold War (5)
Defensive War (6)
Offensive War (7)
Jihad (8 )

The lists begin at 0x18A0C0 in trek.exe. The first one is for empire 0 (Cardassians) and agenda 0, next one is empire 0 agenda 1, and so on. All together the lists take up 12960 bytes.

Each entry contains ship type id (4 bytes (as read by botf - but only 2 bytes actually matter - botf shifts the bits) and another 4 bytes which have unknown function and are usually set to 0, but sometimes 1.

Let's say the AI is playing the Cardassians. At the begining their agenda is Normal expansion. One would expect Unopposed expansion, but that is not the case.

In vanilla trek.exe the first entries are:

Scout, Colony, Transport, Destroyer, Transporter, Destroyer, Colony, Transporter, Command Cruiser...

When the AI builds a ship it also runs through the list to pick the next ship type to build, the result is saved until the AI decides to build a ship. It then builds the one previously picked and picks the one for the next time again.

Now we start with a colony and a scout. AI goes through the list like this:

if the number of scouts < 1 build scout
else continue next item
if the number of colony ships < 1 build colony
else continue next item
if the number of transporters ships < 1 build transporter
else continue next item
if the number of destroyers < 1 build destroyer
else continue next item
if the number of transporters ships < 2 build transporter
else continue next item
and so on

So the first ship our Cardassians would build is a transporter.

When the next item is -1 (FFFF) or the end of the list is reached, the AI starts again from the begining of the list with the limits increased accordingly. So if the list contained only two Colony ships, a transporter and FFFF. The logic would look like this:

if the number of colony ships < 1 build colony
else continue next item
if the number of colony ships < 2 build colony
else continue next item
if the number of transporters ships < 1 build transporter
else continue next item
read FFFF (end of list) so start from begining
if the number of colony ships < 3 build colony
else continue next item
if the number of colony ships < 4 build colony
else continue next item
if the number of transporters ships < 2 build transporter
else continue next item
etc..

The forum keeps messing up the post so read on in next post...
Last edited by DCER on Tue Feb 24, 2009 9:31 am, edited 1 time in total.
User avatar
DCER
Code Master
Code Master
Posts: 683
Joined: Sat Apr 26, 2008 2:00 am

Post by DCER »

Now what if it encounters a ship type it can't build. Lets say the AI can't build the command cruiser and encounters it on the list. It will build a "lower level" equivalent and begin the list from the start (and not continue to next item). However unlike FFFF, it won't increase the limits. This means that if no ship is destroyed, the AI will keep building the lower equivalent until it can finally build the command cruiser and will then move on to the next item. The AI will normally not scrap it's surplus of ships unless it needs credits.

The "lower level" counter parts are (if ship type is unavailable move along the arrow):

Command Cruiser -> Cruiser -> Destroyer -> Scout -> skip

Strike Cruiser -> Cruiser -> Destroyer -> Scout -> skip

Colony -> skip
Transporter -> skip

This behaviour could be changed by modifying subroutine sub_40CB20.

Valid ship types are 0-5 and 9.

I couldn't figure out what the unknown value does if anything. Here's what it doesn't mean: stop here, new list start here, build more of this, skip this if not available

I couldn't discern any difference in AI behaviour when the unknown value is 0 or 1.

One fun thing you could do is to decide that one ship type not be available to any empire, you could use that to force the ai to build cruisers exclusively instead of making more colony ships etc. You would then have the number of colony ships etc. capped, while cruisers would not be limited in numbers. By modifying the relevant subroutine you could change the cruiser type to command cruiser and have those unlimited instead.
Dr_Breen
Commodore
Commodore
Posts: 889
Joined: Wed Apr 30, 2008 2:00 am
Location: Zurich, Switzerland
Contact:

Post by Dr_Breen »

so we can get the AI to build less TT's (i once met an AI fleet of 112 TT's and i destroyed them all witha cloaked battleship)
of course TT's are needed but not that many of them, it should rather build a few more battleships
Public BotF / EF2 Teamspeak 3 Server: 83.169.13.55
User avatar
DCER
Code Master
Code Master
Posts: 683
Joined: Sat Apr 26, 2008 2:00 am

Post by DCER »

Yes, you could. Download latest dev version of UE, it'll allow editing these lists when you open trek.exe
User avatar
Gowron
Code Master
Code Master
Posts: 304
Joined: Sat Apr 26, 2008 2:00 am
Location: 50° N, 11° E

Post by Gowron »

This looks very interesting.
Many thanks, DCER :)
A discovery consists in seeing something everybody has seen and at the same time thinking something nobody has thought yet.
User avatar
thunderchero
Site Administrator aka Fleet Admiral
Site  Administrator aka Fleet Admiral
Posts: 6756
Joined: Fri Apr 25, 2008 2:00 am
Location: On a three month training mission, in command of the USS Valiant.
Contact:

Post by thunderchero »

Weirdness wrote:A question are slots 6-8 used for things like outposts, starbases, alien, minors?
ship types
0=scout
1=destroyer
2=cruiser
3=strike
4=command
5=colony
6=outpost
7=starbase
8=alien
9=troop

this was by memory but think it is right :wink:

thunderchero
User avatar
Spocks-cuddly-tribble
Code Master
Code Master
Posts: 984
Joined: Sun Apr 27, 2008 2:00 am

Re: How the AI decides what ship type to build

Post by Spocks-cuddly-tribble »

Trying to understand more about the selection of the AI tasks lead me back to this great work by DCER.

DCER wrote:The lists begin at 0x18A0C0 in trek.exe. The first one is for empire 0 (Cardassians) and agenda 0, next one is empire 0 agenda 1, and so on. All together the lists take up 12960 bytes.
There's one more list following at 0x18D360 / asm-58F560 shared for all AI empires. It's used when potentially dangerous fleets (unfederated and not set to "explore") occupy ALL own systems with shipyards (i.e. if it's too late anyway).

DCER wrote:Each entry contains ship type id (4 bytes (as read by botf - but only 2 bytes actually matter - botf shifts the bits) and another 4 bytes which have unknown function and are usually set to 0, but sometimes 1.
The first entry with the second 4 bytes set to 1 marks the position to start read for list iterations. This means ship types set exclusively in entries above the marker limit the absolut number of those ship types.

DCER wrote:Now what if it encounters a ship type it can't build. Lets say the AI can't build the command cruiser and encounters it on the list. It will build a "lower level" equivalent and begin the list from the start (and not continue to next item). However unlike FFFF, it won't increase the limits. This means that if no ship is destroyed, the AI will keep building the lower equivalent until it can finally build the command cruiser and will then move on to the next item.
In fact it's just a simple bug wrt the internal ship type index values of unavailable i.e. replaced ship types. Means the AI still searchs for number of e.g. command ships and, since there are none, repeats building the lower level ships instead of reading the next entry.


:arrow: "Lower Level" Ship Type Index Bug Fix

Code: Select all

trek.exe at 0xBF59 change 89 C8 -> 8BFB

and at 0xC181 new code 0x44 bytes:

8B C7 90 C1 E0 02 BB 01 00 00 00 66 2B 1C 04 66 85 DB 7E 24 7E AB 31 D2 0F BF DB 89 14 04 8D 04 19 39 E8 77 41 8B C7 90 66 8B 7C 44 30 01 DF 01 D9 66 89 7C 44 30 EB 89 8B C7 90 83 3C 84 01 72 0B 8B C7 90

-asm code:

0040CB59   8BFB      MOV EDI,EBX

at 0040CD81, 40CDA6, 0040CDB9 & 0040CDC2

8BC7      MOV EAX,EDI
90        NOP
I don't know how many bugs is too many but that point is reached somewhere before however many in BotF is.
User avatar
Spocks-cuddly-tribble
Code Master
Code Master
Posts: 984
Joined: Sun Apr 27, 2008 2:00 am

Capping TTs in AI Fleet Buildup (for Thunderchero)

Post by Spocks-cuddly-tribble »

Code: Select all

NAME: Capping TTs in AI Fleet Buildup
DESC: Fifth byte in patch code is AI TT limit (default 0x16 i.e. 22 TTs).
AUTHOR: Spocks-cuddly-tribble
URL: https://www.armadafleetcommand.com/onscreen/botf/viewtopic.php?p=54715#p54715

>> 0xBFD9 45 83 FE 0C 7D 2B 83 FD 06 77 F4 FF 24 AD 90 CB 40 00
<< 0xBFD9 83 7C 24 24 16 7C 06 C6 44 24 24 FF 90 83 FE 07 7F 1F

# 0040CBD9    837C24 24 16   CMP DWORD [ESP+24], 16    // AI TT limit 0x16 i.e. 22 TTs
# 0040CBDE    7C 06          JL SHORT 40CBE6
# 0040CBE0    C64424 24 FF   MOV BYTE [ESP+24],0FF
# 0040CBE5    90             NOP
# 0040CBE6    83FE 07        CMP ESI,7
# 0040CBE9    7F 1F          JG SHORT 40CC0A

Code: Select all

NAME: Number of unused dilithium sources (fix) for AI build ship tasks
DESC: Keeps AI from assigning invalid ship tasks. Not for mods with disabled dilithium requirements for finishing ships (e.g. UDM3)!
AUTHOR: Spocks-cuddly-tribble
URL: https://www.armadafleetcommand.com/onscreen/botf/viewtopic.php?p=54715#p54715
 
>> 0xBDB0 8D 04 CD 00 00 00 00 01 C8 C1 E0 02 01 C8 C1 E0 02 89 C1 C1 E0 02 29 C8 05 98 1A 5B 00 8D 48 54 8B 40 50 66 8B 59 0C
<< 0xBDB0 69 C1 BC 01 00 00 05 98 1A 5B 00 8B 58 60 3B 58 64 7F 04 33 DB EB 03 2B 58 64 8B 40 50 90 90 90 90 90 90 90 90 90 90

# 0040C9B0     69C1 BC010000      IMUL EAX,ECX,1BC
# 0040C9B6     05 981A5B00        ADD EAX, 5B1A98  adr empsInfo
# 0040C9BB     8B58 60            MOV EBX,[EAX+60] number of running dilithium sources
# 0040C9BE     3B58 64            CMP EBX,[EAX+64] number of ships under construction
# 0040C9C1     7F 04              JG SHORT 40C9C7
# 0040C9C3     33DB               XOR EBX,EBX
# 0040C9C5     EB 03              JMP SHORT 40C9CA
# 0040C9C7     2B58 64            SUB EBX,[EAX+64] number of ships under construction
# 0040C9CA     8B40 50            MOV EAX,[EAX+50] adr provInfo
# 40C9CD-40C9D6  90....           NOP
I don't know how many bugs is too many but that point is reached somewhere before however many in BotF is.
Post Reply

Return to “How the AI decides what ship type to build”