Economic and Military Score (Overflow Fix)

Economic and Military Score Overflow Fix; support/discussion/questions

Moderator: thunderchero

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

Economic and Military Score (Overflow Fix)

Post by Spocks-cuddly-tribble »

The total score (base of power graph) is mainly based on the military and economic sub scores.

But all values suffer from a VERY hard coded two byte limit with a predisposition for overflow bugs, except the hall of fame max score:

Turner wrote:Botf seems to have a problem with the correct calculation of the score from an empire, if the cumulative value of these empire exceeds a specific threshold. If this happens, the calculation of the score begins again with zero.

This does affect:

- score percentages (intel screen - empire status)
- power graph (end game screen)
- total score after winning the match
- some espionage reports
- AI behavior



:arrow: Military Score (sub_44EE20)

Military Score = [build cost of all: ships except colony ships + (stations / systems held) + (orbital batteries and shield generators / 2)] / 20

Turner wrote:Value of the ships (without colony ships) + (Value of all stations * 0,3) + Value of all orbital batteries and shield generators
An excellent analysis, pretty accurate, except for the cumulated build cost of the stations, which gets divided by systems held; and OB & SH costs are divided by 2:

Code: Select all

-> stations
0044EF65                 div     ecx  // divide cumulated build cost of the stations by systems held

-> ships
loc_44F0A4            // add build cost if ship function is 0-4 or 9 = no colony ships

-> buildings
0044EFF6                 cmp     dl, 13h   // check building output ID -> SH
0044F0F3                 cmp     dl, 11h   // check building output ID -> OB
0044F011                 shr     eax, 1    // divide building costs by 2
Turner wrote:I've boosted the price for a sovereign up to 65535 and so I've needed only 21 of them to exceed the threshold... close by 1310500
Again pretty accurate, it's exactly 1310700 = 65535 (2 byte word/unsigned) * 20

Code: Select all

0044F035                 mov     ecx, 14h    // divide end result by 20
Consequently the simplest solution, without loss of data reliability for low tech games, would be a divisor adjustment to the price of the cheapest of the above listed reference objects (default 250 = OB / SH build cost [500] / 2).

Just the cumulated build cost of war fleets (ship functions 0-4) i.e. the effective offensive firepower or military dangerousness of empires would be even better to help the AI to determine times for aggressive and/or demanding behavior (provided there is an actual ratio of build costs & fighting capacities of war ships).



:arrow: Economic Score (sub_44EC20)

Economic Score = [(total credits / 10) + (income * 10) + current food, industry, energy, research & intel output of all systems held] / 4

Code: Select all

0044EC62                 mov     ecx, 0Ah  // divisor for total credits from [empsInfo+98h]

0044EC73                 lea     eax, [edx*4]  // 4* income from [empsInfo+9C]
0044EC7A                 add     eax, edx   // 4* income + income = 5* income
0044EC7C                 add     eax, eax  // double (5* income) = 10* income

-systems held / read systInfo loop-
0044EC9E                 add     ecx, [eax+2ACh]  // Food output total after bonuses 
0044ECA6                 mov     esi, [eax+2C4h]  // Industry output total after bonuses
0044ECAC                 mov     bx, [eax+2B6h]  // Energy output total after bonuses
0044ECB3                 mov     ebp, [eax+2C8h]  // Research output total after bonuses
0044ECBB                 mov     ebx, [eax+2D8h]  // Intel output total after bonuses

0044ECD9                 shr     ecx, 2   // end divisor 2^2 = 4
Max result to prevent overflow is again 65535 (2 byte word/unsigned)

Needless to say that we, as well as the AI, really don't care about the totally unrelated food, energy, research and intel outputs. :?

An example for an alternative approach:

Economic Score = [(total credits / 32) + income + (current + nominal -industry output of all systems held / 8 )] / 8

Code: Select all

trek.exe at 0x4E06A new code 0x72 bytes:

E8 05 90 90 89 C1 8B 53 04 90 90 90 90 90 90 90 90 90 90 90 03 CA 8B 46 50 E8 78 CE 0C 00 89 C2 85 C0 74 46 0F BF 02 69 D8 28 03 00 00 A1 C8 36 5A 00 01 D8 8B 98 C4 02 00 00 03 98 90 01 00 00 C1 EB 03 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 89 D0 01 F1 E8 D6 CA 0C 00 90 90 89 C2 01 D9 85 C0 75 BA 31 C0 8A 04 24 C1 E9 03

asm code changes:
0044EC69     C1E8 05           SHR EAX, 5 // total credits /2^5 = /32
0044EC6C     9090              NOP

0044EC73-44EC7D     90         NOP
0044EC7E     03CA              ADD ECX, EDX // income

0044EC9E     8B98 C4020000     MOV EBX, [EAX+2C4]  // current industry output
0044ECA4     0398 90010000     ADD EBX, [EAX+190]  // add nominal industry output
0044ECAA     C1EB 03           SHR EBX, 3   // divide by 2^3 = 8
0044ECAD-44ECC0    90          NOP

0044ECCA     9090              NOP

0044ECD9     C1E9 03           SHR ECX, 3  // divide end result by 2^3 = 8
I.e. sum of:
+ income / 8
+ total credits / 256
+ for all systems held [arithmetic average of current & nominal industry output] / 32

Income / Industry ratio = 4:1 (for ship scrap resp. trade goods 1:1 mods, this can be up to 1:1, of course)



Empire Status - Intel Screen - issues:

- the vanilla economic & military percentages (based on the results of sub 44EC20 & 44EE20) seem to be bugged
- also when meeting an other empire before turn 3 the empire status shows just question marks instead of the percentages

Code: Select all

sub_4BF0A0 -> sub_4BE528
004BE714                 mov     edx, 64h // percentage factor loaded as divisor into FPU
Last edited by Spocks-cuddly-tribble on Wed Apr 27, 2022 4:30 pm, edited 3 times in total.
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: 1870
Joined: Sun Apr 27, 2008 2:00 am

Total Score & Power Graph

Post by Spocks-cuddly-tribble »

Based on the above explained economic and military scores, sub_506CA0 calculates the total score as follows:

[100 - (Turn Number / 10) + (economic score * 0.5) + (arithmetic average of the last 10 turn military scores * 0.25)]

* [84 +1 per higher resp -1 per lower opponent starting level] % (e.g. T1 vs. 4 * T5 = 100% resp T5 vs. 4 * T1 = 68%)

- [3.75 * difficulty level lower 5 aka impossible] % (e.g. simple -15% or * 0.85)

Code: Select all

00506CBB                 mov     edx, 64h // base value 100
00506CAD                 mov     ecx, 0Ah // divisor for turn number

multipliers for:
(8 byte floats)
ds:583298 economic score (0.5) at 0x181098
ds:5832A0 military score (0.25) at 0x1810A0
(4 byte float)
ds:5832A8 lower difficulty level penalty (3.75) at 0x1810A8
When lowering economic and military score to prevent overflows (via increasing of end divisors or altering of the calculations) just increase the corresponding multipliers by the same factor to keep the total score on the default level (code example below).

By default total credits and build costs of warships influence the total score by the same factor of /80 = /10 /4 *0.5 = /20 *0.25

Like the military score, the economic score stores the last 10 turn scores, but they don't seem to be used.



Sub_44E810 stores up to 250 snapshots of increases of the total score (i.e. current score - starting score) as base for the power graph.

Issues with the power graph: (not true for total score/famehall)

- total score & snapshots are loaded/stored as two byte values i.e. max score to prevent power graph overflow is 65535 :!:

The last power graph snapshot value can be displayed via map ai data type F 'Game Statistics': viewtopic.php?p=34204#p34204

Tech score = sum of all tech levels + highest tech level + lowest tech level (i.e. they count twice)

- military, economic & tech score, store each the ranks and the top ranked empire ID



Economic/Military score overflow workaround with Total Score on default level (power graph issues on larger maps still possible!)

Code: Select all

0x4E0DB              -> 05
0x4E435 (0x10 bytes) -> 8B C3 33 C9 B1 05 D3 E8 33 D2 F7 F1 8B D8 0F B6
0x106112 (8 bytes)   -> C1 E0 02 EB 16 90 90 90
0x106184 (5 bytes)   -> 8D 2C 09 EB 38

-asm-
0044ECD9   C1E9 02              shr  ecx, 5     //   /2^5 = /32

0044F035   8BC3                 MOV EAX,EBX
0044F037   33C9                 XOR ECX,ECX
0044F039   B1 05                MOV CL,5 
0044F03B   D3E8                 SHR EAX,CL      //   /2^5   = /32
0044F03D   33D2                 XOR EDX,EDX
0044F03F   F7F1                 DIV ECX         //   /32 /5 = /160
0044F041   8BD8                 MOV EBX,EAX
0044F043   0FB68424 DC010000    MOVZX EAX,BYTE[ESP+1DC]

00506D12   C1E0 02    SHL EAX,2                 // *4
00506D15   EB 16      JMP SHORT 00506D2D
00506D17   909090     NOP

00506D84   8D2C09     LEA EBP, DWORD [ECX+ECX]  // *2
00506D87   EB 38      JMP SHORT 00506DC1
-> divisors set to 32 & 160 -> multipliers set to 4 & 2 via integer bypass
Last edited by Spocks-cuddly-tribble on Thu Apr 28, 2022 3:05 pm, edited 6 times in total.
I don't know how many bugs is too many but that point is reached somewhere before however many in BotF is.
User avatar
thunderchero
Site Administrator aka Fleet Admiral
Site  Administrator aka Fleet Admiral
Posts: 7824
Joined: Fri Apr 25, 2008 2:00 am
Location: On a three month training mission, in command of the USS Valiant.

Re: Economic and Military Score (Overflow Fix)

Post by thunderchero »

here is an interesting graph from my last game.

final turn
last.jpg
last.jpg (516.07 KiB) Viewed 3607 times
here is graph
graph.jpg
graph.jpg (221.04 KiB) Viewed 3607 times
my score was 262316 (it was top score)

it looks like graph dropped after each time I took out a major?
User avatar
Spocks-cuddly-tribble
Code Master
Code Master
Posts: 1870
Joined: Sun Apr 27, 2008 2:00 am

Total Score Overflow Workaround for Power Graph & AI

Post by Spocks-cuddly-tribble »

thunderchero wrote: Wed Feb 03, 2021 12:04 amImage
my score was 262316

Code: Select all

NAME: Total Score Overflow Workaround for Power Graph & AI (Fixed)
DESC: New limit is about half a million. Use WITH base patch for Economic & Military Scores (max fleet cost above 10 million).
AUTHOR: Spocks-cuddly-tribble
URL: https://www.armadafleetcommand.com/onscreen/botf/viewtopic.php?p=56621#p56621

>> 0x0004dcb1 31 f6 01 eb e8 e6 83 0b 00 66 89 c6
>> 0x0004dcd3 f3
>> 0x0004dcda 31 31 f6 66 8b 31
>> 0x0004dce3 77
>> 0x0004dce7 44 59
>> 0x0004dd2c 81
>> 0x0004dd2e f4 01 00 00
>> 0x0004dd4b 05 f4 01 00 00
>> 0x0004ddf1 81 c1 84 2b 5a 00
>> 0x0004ddf8 a4 82 0b 00
>> 0x0004de01 d8 09 00
>> 0x0004de25 7c 5e 0c 66 8b 74 59 0c

<< 0x0004dcb1 03 dd e8 e8 83 0b 00 c1 e8 03 8b f0
<< 0x0004dcd3 cb
<< 0x0004dcda 29 0f b7 4d 00 90
<< 0x0004dce3 4f
<< 0x0004dce7 74 5d
<< 0x0004dd2c 83
<< 0x0004dd2e 02 90 90 90
<< 0x0004dd4b 83 c0 02 90 90
<< 0x0004ddf1 e8 aa 82 0b 00 c1
<< 0x0004ddf8 03 90 90 90
<< 0x0004de01 5c 35 5a
<< 0x0004de25 7e 02 66 8b 71 02 90 90

#0044E8B1     03DD               ADD EBX,EBP
#0044E8B3     E8 E8830B00        CALL 506CA0
#0044E8B8     C1E8 03            SHR EAX,3
#0044E8BB     8BF0               MOV ESI,EAX

#0044E8D2     69CB F4010000      IMUL ECX,EBX,1F4
#0044E8D8     8D3C29             LEA EDI,[ECX+EBP]
#0044E8DB     0FB74D 00          MOVZX ECX,WORD[EBP]
#0044E8DF     90                 NOP
#0044E8E0     66:89444F 0C       MOV [EDI+ECX*2+C],AX
#0044E8E5     66:89745D 02       MOV [EBP+EBX*2+2],SI  // AI FIX (ignore starting score)

#0044E92C     83C1 02            ADD ECX,2
#0044E92F     909090             NOP

#0044E94B     83C0 02            ADD EAX,2
#0044E94E     9090               NOP

#0044E9F1     E8 AA820B00        CALL 506CA0
#0044E9F6     C1E8 03            SHR EAX,3
#0044E9F9     909090             NOP

#0044E9FE     66:8981 5C355A00   MOV [ECX+5A355C],AX  // 5A2B84 +9D8 = 5A355C // set starting score

#0044EA23     66:8B7E 02         MOV DI,[ESI+2]       // total score (ignore starting deviation) for rank
#0044EA27     66:8B71 02         MOV SI,[ECX+2]       // total score (ignore starting deviation) for rank
#0044EA2B     9090               NOP


Outdated old patch: File: ..\..\source\ai\aidipevl.c, Line: 2569, firstPlace != INVALID_EMPIRE_ID && secondPlace != INVALID_EMPIRE_ID

Code: Select all

NAME: Outdated -> ERROR -> Remove & Use Above Patch!

>> 0x0004dcb1 31 f6 01 eb e8 e6 83 0b 00 66 89 c6
>> 0x0004dcd3 f3
>> 0x0004dcda 31 31 f6 66 8b 31
>> 0x0004dce3 77
>> 0x0004dce7 44 59
>> 0x0004ddf1 81 c1 84 2b 5a 00
>> 0x0004ddf8 a4 82 0b 00
>> 0x0004de01 d8 09 00
>> 0x0004de25 7c 5e 0c
>> 0x0004de2b 59 0c

<< 0x0004dcb1 03 dd e8 e8 83 0b 00 c1 e8 03 8b f0
<< 0x0004dcd3 cb
<< 0x0004dcda 29 0f b7 4d 00 90
<< 0x0004dce3 4f
<< 0x0004dce7 74 5d
<< 0x0004ddf1 e8 aa 82 0b 00 c1
<< 0x0004ddf8 03 90 90 90
<< 0x0004de01 5c 35 5a
<< 0x0004de25 7e 02 90
<< 0x0004de2b 46 02
Last edited by Spocks-cuddly-tribble on Sun Oct 02, 2022 10:29 pm, edited 4 times in total.
I don't know how many bugs is too many but that point is reached somewhere before however many in BotF is.
User avatar
thunderchero
Site Administrator aka Fleet Admiral
Site  Administrator aka Fleet Admiral
Posts: 7824
Joined: Fri Apr 25, 2008 2:00 am
Location: On a three month training mission, in command of the USS Valiant.

Re: Economic and Military Score (Overflow Fix)

Post by thunderchero »

I am guessing this will not work on saved games? I just finished a game and added patch and did last turn and it was up and down still.
User avatar
Flocke
BORG Trouble Maker
BORG Trouble Maker
Posts: 3179
Joined: Sun Apr 27, 2008 2:00 am
Location: Hamburg, Germany
Contact:

Re: Economic and Military Score (Overflow Fix)

Post by Flocke »

There are two issues with the highscore. One is the unsigned short limit of 65,535, and the other that the power graph is limited to 250 turns.

From preceding posts of SCT I'd say he sticked to adjust the formula, to reduce the computed score, which is a valid workaround and perfectly compatible since it doesn't change the data format. Any change to the data format on the other hand would be incompatible indeed, but at the same time require lots more of code changes.

What the above patch actually changed, I however would have to study the asm code as well. The few code annotations above neither explain the fixed issues nor how it got fixed. But there obviously was an issue with the AI rank calculation. :roll:

I also wonder what the "starting score" and "starting deviation" is.
I can only guess the "starting score" is the computed first turn score, which can be substracted from any power graph score to begin with 0. But is it some value stored to the save game? How is it determined in later turns when a save game is loaded? I don't find a reference for that value. :?:
The "starting deviation" then likely is the resulting offset from the "starting score".
User avatar
Spocks-cuddly-tribble
Code Master
Code Master
Posts: 1870
Joined: Sun Apr 27, 2008 2:00 am

Total Score Overflow Workaround for Power Graph & AI (corrected)

Post by Spocks-cuddly-tribble »

thunderchero wrote: Sat May 28, 2022 9:03 pmI am guessing this will not work on saved games?
Correct, as told above, up to 250 (two byte) values per empire are stored in the saved game. So the patch must be used from the start.

Power graph values divided by 8 before storing as two byte word (total score & starting score).

The 250 slots per empire limit is NOT an issue, but with the patch power graphs might look a bit less smooth for T1 small map.

Flocke wrote: Sun May 29, 2022 3:12 amthere obviously was an issue with the AI rank calculation.

I also wonder what the "starting score" and "starting deviation" is.
I can only guess the "starting score" is the computed first turn score, which can be substracted from any power graph score to begin with 0. But is it some value stored to the save game? How is it determined in later turns when a save game is loaded? I don't find a reference for that value. :?:
The "starting deviation" then likely is the resulting offset from the "starting score".
AI reads total score increases (i.e. total score - starting score) to compare the empires' power ratios. My patch corrects this to current total score as base for AI judgments. Otherwise a T1 Ferengi AI with better score increases might get the idea to take on a T8 Klingon AI....

It's a complex matter. In fact that intricate that I made an error in the patch for AI rank check (post edited with correction).

Code: Select all

44F190 GameUpdateStatistics - (AI mapData 4E4FCD) -> saved in GameInfo?
5A2B84 Total_Score_word(+2) Base rank +9E2
5A2B84 +C +500 dec * empire power graph values word
5A355C Starting_Score_word +9D8 
5A3570 Military_Score_word +9EC  rank +0A5A
5A35E8 Economic_Score_word +0A64 rank +0A6E
5A3660 Tech_Score_word     +0ADC rank +0AE6
5A3674 Minors_word         +0AF0
5A367E Systems_Held_word   +0AFA
5A3688 Dilithium_word      +0B04
gameInfo +2AC might be stored at asm-5A2B84 (fixed position dynamic data)

Offset 0x02AC current turn -> iteration pointer 1-250?
Offset 0x02AE last turn points (for statistic) -> current total score - starting score (default same as power graph values)?
Offset 0x02BA(maybe less)+ statistic of the cards -> up to 250 power graph values...?
Offset 0x04AE(maybe less)+ statistic of the feds
Offset 0x06A2(maybe less)+ statistic of the fergs
Offset 0x0896(maybe less)+ statistic of the klings
Offset 0x0A8A(maybe less)+ statistic of the roms
Offset 0x0C84+(?) empire state (intelligence) -> +2AC+9D8 = C84 starting score?
Offset 0x0C8E+ military
Offset 0x0D06+ economy
Offset 0x0D1A+ science
Offset 0x0D92+ occupied systems
I don't know how many bugs is too many but that point is reached somewhere before however many in BotF is.
User avatar
thunderchero
Site Administrator aka Fleet Admiral
Site  Administrator aka Fleet Admiral
Posts: 7824
Joined: Fri Apr 25, 2008 2:00 am
Location: On a three month training mission, in command of the USS Valiant.

Re: Economic and Military Score (Overflow Fix)

Post by thunderchero »

here is after starting a new game, other major look a little flat
overflow.jpg
overflow.jpg (183.27 KiB) Viewed 2440 times
score was only 153079, I accepted the first offer of "Every major government has admitted our galactic dominance. Shall we accept their surrender?"

I had already eliminated Federtion and Ferengi
klin.jpg
klin.jpg (500.44 KiB) Viewed 2440 times
User avatar
Flocke
BORG Trouble Maker
BORG Trouble Maker
Posts: 3179
Joined: Sun Apr 27, 2008 2:00 am
Location: Hamburg, Germany
Contact:

Re: Economic and Military Score (Overflow Fix)

Post by Flocke »

thunderchero wrote: Mon May 30, 2022 10:27 pm here is after starting a new game, other major look a little flat
You mean we should multiply the AI score by some factor to make it look better? :lol:

Well, once I'm done with UE, I will focus on the code extensions again and I'm sure we can tweak the AI some little.


Regarding the saved gameInfo file, I searched forums but it seems I missed to post my updated analysis a few months back. :roll:
With my latest analysis on the game7.sav from map editing topic, I assumed offset 0x0C84+ to be hidden intel score values, see:
https://gitlab.com/stbotf/ultimate-edit ... .java#L150

Comparing those values with the actual intel values from intelInfo, they however don't match. Specially the cardassians would be way too low on the intel score. It however also looks strange to me if it was the initial turn 0 score, unless I started with a higher klingon evolution level: :???:
game7_score.jpg
game7_score.jpg (124.15 KiB) Viewed 2437 times
will have to check on it


44F190 GameUpdateStatistics - (AI mapData 4E4FCD) -> saved in GameInfo?
5A2B84 Total_Score_word(+2) Base rank +9E2                      	-> matches 0x0C8E what I recognized the military rank
5A2B84 +C +500 dec * empire power graph values word
5A355C Starting_Score_word +9D8 					-> matches 0x0C84 what I recognized the intel score
5A3570 Military_Score_word rank +0A5A					-> matches 0x0D06 what I recognized the economic rank
5A35E8 Economic_Score_word rank +0A6E					-> matches 0x0D1A what I recognized the science rank
5A3660 Tech_Score_word rank +0AE6					-> matches 0x0D92 what I recognized the system rank

5A3570 Military_Score_word +9EC						-> matches 0x0C98
5A35E8 Economic_Score_word +0A64					-> matches 0x0D10
5A3660 Tech_Score_word     +0ADC 					-> matches 0x0D88
5A3674 Minors_word         +0AF0					-> matches 0x0D9C
5A367E Systems_Held_word   +0AFA					-> matches 0x0DA6
5A3688 Dilithium_word      +0B04					-> matches 0x0DB0

Spocks-cuddly-tribble wrote: Sun May 29, 2022 9:19 am gameInfo +2AC might be stored at asm-5A2B84 (fixed position dynamic data)
for 5A2B84 above you listed the total score or base rank, not the current turn number?
Spocks-cuddly-tribble wrote: Sun May 29, 2022 9:19 am Offset 0x02AC current turn -> iteration pointer 1-250?
a logic assumption I need to check
Spocks-cuddly-tribble wrote: Sun May 29, 2022 9:19 am Offset 0x02BA(maybe less)+ statistic of the cards -> up to 250 power graph values...?
250 per empire, but for UE I fixed the start offset
the empire statistics already start at 0x02B8
User avatar
Spocks-cuddly-tribble
Code Master
Code Master
Posts: 1870
Joined: Sun Apr 27, 2008 2:00 am

Re: Economic and Military Score (Overflow Fix)

Post by Spocks-cuddly-tribble »

Flocke wrote: Tue May 31, 2022 3:59 am
thunderchero wrote: Mon May 30, 2022 10:27 pm here is after starting a new game, other major look a little flat
You mean we should multiply the AI score by some factor to make it look better? :lol:

Well, once I'm done with UE, I will focus on the code extensions again and I'm sure we can tweak the AI some little.
This looks right, the higher the deviation to the max score the flatter the lower values i.e. the patch works fine. @ The Ai - TC just needs to stop cheating.... :wink:

Flocke wrote: Tue May 31, 2022 3:59 amfor 5A2B84 above you listed the total score or base rank, not the current turn number?
Just hasty posted notes from code analysis. 'Base' in the sense that the adr pointer is used in combination with static (e.g. +2 current total-starting scores 1-5, +C start of power graph values) or dynamic index factors. Default code has no direct pointer to the current total scores.

There is also past 10 turns Military & Economic scores, following the locations the current scores I think.

Also I remember the gameInfo file has some issue with loading or storing data in certian areas (monster, random events, AI timer)?
I don't know how many bugs is too many but that point is reached somewhere before however many in BotF is.
User avatar
thunderchero
Site Administrator aka Fleet Admiral
Site  Administrator aka Fleet Admiral
Posts: 7824
Joined: Fri Apr 25, 2008 2:00 am
Location: On a three month training mission, in command of the USS Valiant.

Re: Economic and Military Score (Overflow Fix)

Post by thunderchero »

Spocks-cuddly-tribble wrote: Tue May 31, 2022 8:43 am This looks right, the higher the deviation to the max score the flatter the lower values i.e. the patch works fine. @ The Ai - TC just needs to stop cheating.... :wink:
I am not cheating :roll:
User avatar
Spocks-cuddly-tribble
Code Master
Code Master
Posts: 1870
Joined: Sun Apr 27, 2008 2:00 am

Re: Economic and Military Score (Overflow Fix)

Post by Spocks-cuddly-tribble »

Flocke wrote: Tue May 31, 2022 3:59 amfor UE I fixed the start offset
the empire statistics already start at 0x02B8
Remember we are talking word * 5 empires each score and rank i.e. by default copies of the first power graph values (with above rank fix corrected to current scores).


thunderchero wrote: Tue May 31, 2022 8:59 amI am not cheating :roll:
But you used that time machine to go back and sabotage the development of the BotF AI. Or how do you explain this code mess?

EDIT:
Flocke wrote: Tue May 31, 2022 3:59 amWell, once I'm done with UE, I will focus on the code extensions again and I'm sure we can tweak the AI some little.
Here is another unused AI patch - but the Threat Level feature (max value 4) needs more testing/analysis:

Code: Select all

NAME: AI 'Threat Level' Distance Check for Enemy Ships (4 Sectors)
DESC: Vanilla reads cost of ALL armed ships with the range to reach the defender system, but defender ship cost only within 4 sectors.
AUTHOR: Spocks-cuddly-tribble
URL: 

>> 0x0002c874 8b 51 04 31 f6 85 d2
>> 0x0002c890 89 c5
>> 0x0002c8bb 66 8b 45 28 39 d0 75 56 8d 94 24 20 01
>> 0x0002c8c9 00 89 c8 e8 4f d7 04 00 85 c0 74 26 31 c0 66 8b 84 24 1c 01 00 00
>> 0x0002c904 0f 8d 0a fe ff ff
>> 0x0002c919 31 d2 66 8b 94 24 4c 01 00 00 39 d0 75 d4 8d 94 24 20 01 00 00 89 c8 e8 eb d6 04 00 85 c0 74 c2
>> 0x0002c976 73
>> 0x0002c978 31 c0 66 8b 84 24 1c 01 00 00 89 84 24 48 01
>> 0x0002c988 00 db 84 24 48 01 00 00

<< 0x0002c874 33 ed 33 f6 39 71 04
<< 0x0002c890 90 90
<< 0x0002c8bb 3a 51 34 75 02 b0 08 8d 94 04 28 01 00
<< 0x0002c8c9 89 14 24 66 8b 84 24 1c 01 00 00 03 e8 89 ac 24 48 01 00 00 eb 1c
<< 0x0002c904 7d 13 90 90 90 90
<< 0x0002c919 85 ed 0f 84 f3 fd ff ff 8d 94 24 20 01 00 00 8b c1 e8 f1 d6 04 00 85 c0 0f 84 dd fd ff ff 90 90
<< 0x0002c976 0f
<< 0x0002c978 98 fd ff ff 8b 04 24 db 84 24 48 01 00 00 dc
<< 0x0002c988 dd 18 e9 85 fd ff ff 90


#0042D474     33ED                XOR EBP,EBP
#0042D476     33F6                XOR ESI,ESI
#0042D478     3971 04             CMP [ECX+4],ESI

#0042D490     9090                NOP

#0042D4BB     3A51 34             CMP DL,[ECX+34] // if enemy TF...
#0042D4BE     75 02               JNZ SHORT 42D4C2
#0042D4C0     B0 08               MOV AL,8  // adjust data position
#0042D4C2     8D9404 28010000     LEA EDX,[ESP+EAX+128] // adr fleet cost within 4 sectors from defender system
#0042D4C9     891424              MOV [ESP],EDX  // temp store
#0042D4CC     66:8B8424 1C010000  MOV AX,[ESP+11C] // ship cost
#0042D4D4     03E8                ADD EBP,EAX // TF ship cost
#0042D4D6     89AC24 48010000     MOV [ESP+148],EBP // TF ship cost
#0042D4DD     EB 1C               JMP SHORT 42D4FB

#0042D504     7D 13               JGE SHORT 42D519
#0042D506     90909090            NOP

#0042D519     85ED                TEST EBP,EBP // cost armed ships in TF
#0042D51B    ^0F84 F3FDFFFF       JE 42D314 // no armed ships check next TF
#0042D521     8D9424 20010000     LEA EDX, [ESP+120] // coordinates
#0042D528     8BC1                MOV EAX,ECX  // TF entry
#0042D52A     E8 F1D60400         CALL 47AC20 // check range
#0042D52F     85C0                TEST EAX,EAX
#0042D531    ^0F84 DDFDFFFF       JE 42D314 // out of range check next TF
#0042D537     9090                NOP

#0042D576    ^0F83 98FDFFFF       JNB 42D314
#0042D57C     8B0424              MOV EAX, [ESP] // adr fleet cost within 4 sectors from defender system
#0042D57F     DB8424 48010000     FILD DWORD [ESP+148] // cost armed ships in TF
#0042D586     DC00                FADD QWORD [EAX]
#0042D588     DD18                FSTP QWORD [EAX]
#0042D58A    ^E9 85FDFFFF         JMP 42D314
#0042D58F     90                  NOP

And the ship-task 'fleet staging' function is awful, with an ally in particular. TTs go to ally fleet but not to own invasion fleet. :mad:

-Ship Task- eg 58C0EC of_AI_Invade_targets

Code: Select all

407079         call AI_Task_AIShpUnt_c34h_dec_functions?    -> 41ED30 Invade_34_set_current_neg1_dec_functions
40A900         call_AI_Task_1Ch_set_80_82_check_skip?       -> 41ED70 Invade_1C_Fleet_Staging_Range_eax1
40AB41         call Recursive_Units_set_current_task_IDLE   -> 41EE30 Invade_14_clear_data
42739B         call_AI_Task_44h__ship_range_positions       -> 41E8A0 Invade_44_grab_more_tskSh_set_ship_functions_bitmask
426D52         call Task_Ship_c48h_last                     -> 41E820 Invade_48_Task_EAX_Ratios__eax1_satisfactory
426BEC         call_AI_Task_38h_set_Task_GShip_floats       -> 41E980 Invade_38_GShipEDX_to_FPU__modifier_add_0_1_yard_dil
426E26         call_AI_Task_30h_inc_dec_functions           -> 41ED50 Invade_30_AIShpUnt_inc_dec_functions
40A950         call_AI_Task_18h_LABOR__merge_TFs_set_orders -> 41E7D0 Invade_18_set_Orders_re_deploy_TskSh__fed_free_subjugated
I don't know how many bugs is too many but that point is reached somewhere before however many in BotF is.
User avatar
Flocke
BORG Trouble Maker
BORG Trouble Maker
Posts: 3179
Joined: Sun Apr 27, 2008 2:00 am
Location: Hamburg, Germany
Contact:

Re: Economic and Military Score (Overflow Fix)

Post by Flocke »

Spocks-cuddly-tribble wrote: Tue May 31, 2022 9:17 am
Flocke wrote: Tue May 31, 2022 3:59 amfor UE I fixed the start offset
the empire statistics already start at 0x02B8
Remember we are talking word * 5 empires each score and rank i.e. by default copies of the first power graph values (with above rank fix corrected to current scores).
Weird, you say in vanilla the rank was not based on the current turn but on the starting turn, i.e. always turn 0 unless played any more turns than 250?
That would explain why I found different offsets for the ranks.

Spocks-cuddly-tribble wrote: Tue May 31, 2022 9:17 am
thunderchero wrote: Tue May 31, 2022 8:59 amI am not cheating :roll:
But you used that time machine to go back and sabotage the development of the BotF AI. Or how do you explain this code mess?
Worse than that, for a stressed development team it is already enough to ask for additional considerations. If he even dared to make additional suggestions in that complex matter, the whole endeavour was already lost.

Think about it, they had a set time plan and then thunderchero and companies showed up starting to make suggestions... :shock:
We know the result.

Spocks-cuddly-tribble wrote: Tue May 31, 2022 9:17 am Here is another unused AI patch - but the Threat Level feature (max value 4) needs more testing/analysis:
Well, nice, but again I must guess what that patch is about cause you kept the description short and didn't explain any on the context. :sad:
Is it that you limited the threat detection to 4 sectors for performance improvement of the compution or in hope it would actually perform better?
Does it make the AI any better when it only detects a threat within range of 4 sectors? I doubt so.
Is it a suitable threat calculation to just consider the build cost of those ships? I doubt so.

Well for improving ai by extension I have lots of ideas in mind I need to check. It however needs work on the framework I talked of to access the relevant data and create an extendable plugin system. Here just some teaser:
  1. By the save games I already figured how to add ships, outposts and system structures, why shouldn't that work the same for the running game when the data structures are same. I already hooked the turn progress, and when I implement same calculations for each client this should also work for multiplayer games.
  2. For the save games I analysed ship movements. It should be possible to hook and alter them on the fly. At first I'd limit this to avoid pointless battles. Later, if that works out, we could try to replace the whole ai fleet movement step by step.
  3. With access to the system structures, it should be possible to automatically power orbitals and defensive structures and re-assign population whenever a threat shows up.
  4. With mpr++ I already hooked the combat encounter. To attempt any fix on mpr++ I need to check back on it anyhow and figure how to read and alter the actually participating combatants. Given there is the auto-combat, I could e.g. attempt to alter the participating ships of the combat. E.g. a superior fleet might only catch part of its enemies.
  5. In addition to the participating ships, I still need to figure the ship, torpedo and phaser positions, orientation and status in 3d combat each rendered frame, not just the rendered models. For mpr++ this e.g. would allow to move the camera independent from the botf camera without suffering from the clipping. It however further should allow to alter the actual assigned commands of each ship as well as to prevent damage or blow up other ships and the like.
I leave the rest to imagination. It all needs to be tested, I might as well fail on it, but I'm confident we have lots of options.
Likely only part of it will keep MP compatible until the whole network engine is replaced. But I can't tell on it yet.
User avatar
Spocks-cuddly-tribble
Code Master
Code Master
Posts: 1870
Joined: Sun Apr 27, 2008 2:00 am

Re: Economic and Military Score (Overflow Fix)

Post by Spocks-cuddly-tribble »

Flocke wrote: Tue May 31, 2022 7:11 pmWeird, you say in vanilla the rank was not based on the current turn but on the starting turn, i.e. always turn 0 unless played any more turns than 250?
That would explain why I found different offsets for the ranks.
Not exactly, as you understood, the substraction of the starting score is used as anchor feature for the power graph. It's mistakenly also applied to the 'Total' Score value used for AI features:

So the same value (current - starting score) is stored at:

5A2B84 +2 + 2* empire ID -> 'Total'_Score_word for AI features

5A2B84 +C + 500dec * empire ID + 2* iteration pointer? -> Last Power Graph value

Flocke wrote: Tue May 31, 2022 7:11 pmWell, nice, but again I must guess what that patch is about cause you kept the description short and didn't explain any on the context. :sad:
Is it that you limited the threat detection to 4 sectors for performance improvement of the compution or in hope it would actually perform better?
Does it make the AI any better when it only detects a threat within range of 4 sectors? I doubt so.
Is it a suitable threat calculation to just consider the build cost of those ships? I doubt so.
That's because the feature context is unclear (yet) and a quick test didn't impove the AI. That's why the patch was not 'relased' and/or eleborated in a modding topic. It just looked odd that position of enemy ships get ignored which mistakenly might force the own fleet to stay at home for no reason.


@ #5 The lack of this data pretty much 'cripples' the AI target selection feature.

@ the other points - you might get conflicts with AI tasks i.e. you'll end up re-codding /skipping much more code and features than you intended in the first place
I don't know how many bugs is too many but that point is reached somewhere before however many in BotF is.
User avatar
Flocke
BORG Trouble Maker
BORG Trouble Maker
Posts: 3179
Joined: Sun Apr 27, 2008 2:00 am
Location: Hamburg, Germany
Contact:

Re: Economic and Military Score (Overflow Fix)

Post by Flocke »

Spocks-cuddly-tribble wrote: Wed Jun 01, 2022 8:17 am @ the other points - you might get conflicts with AI tasks i.e. you'll end up re-codding /skipping much more code and features than you intended in the first place
My plan is to not skip any of it in the first place, but just ovverride it. When I can read and write the map data, I just need to place some hook right after the ai task force assignments and change orders and movement, then cache the turn progress for next turn calculation.
It is an experiment I'd love to try. If I get the basics work, we can try to actually replace some of the ai code mess.
Post Reply

Return to “Economic and Military Score Overflow Fix”