Phantom-Dilithium Bug (fix)

Phantom Dilithium Bug (fix) -> OPTION; support/discussion/questions

Moderator: thunderchero

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

Phantom-Dilithium Bug (fix)

Post by Spocks-cuddly-tribble »

As we all know a bug of BotF enabels keeping more ships under construction than the total of running dilithium sources (unless it's zero). Real source only limits the maximum of finished ships per turn.

Oftentimes this method/bug called "Phantom Dilithium" induces wastages of system production at turns consuming the total amount of real dilithium for ship completions. (Thanks to The_Nighthawk for pointing to the issue).

That depends on beginning of ships production. BotF sorts all ships under construction by beginning of production. I.e. by the earliest setting into FIRST position of Production Queue.
NOTE: BotF is watching in which order things will be done during a turn !

In case of maximal ship finishing, all ships with a later beginning of production than the current completed have an interrupt. I.e. the industrial production of the system is wasted.


Example:

sh1 = ship with the earliest beginning of production
sh2 = ship with the second earliest beginning of production
and so on

shX# = ship X finished

S1 = system producing sh1
S2 = system producing sh2
and so on

DS = amount of running dilithium sources


DS = 1
sh1 - sh2# - sh3 -> S1/OK - S3/interrupt



The good news is if multiple ships completed on the same turn then decides the ship with the latest beginning of production upon interrupt:


DS = 2
sh1 - sh2# -sh3 - sh4# - sh5 -> S1 & S3/OK ; S5/interrupt -> sh4# decides



If there were additional ships in Production Queue behind sh2# & sh4# then will the RELATIVE order for the next turn still remain.



Btw leaving systems displaying "Dilithium Shortage" instead of "Turns Remainig X" won't affect BotF. Just ignore it.

The same applies to "On Hold" instead of "X turn(s)" behind ships in systems list ->Build column.

In case of a REAL dilithium shortage only the ships with the earlier beginning of production will be finished. The others will interrupt explained above.
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: 1884
Joined: Sun Apr 27, 2008 2:00 am

Phantom-Dilithium bug fix (with AI Workaround)

Post by Spocks-cuddly-tribble »

Now from the in-game effects to the modding part. :)


We've three displays and (aside of the one click delay of No.3) they work all correctly and as intended:

1.) F1 Empire Info -> Count of unused dilithium (Red warning if negative)

2.) Summary / Systems -> (On Hold) warning for ALL ships

3.) F2 Build Queue -> Dilithium Shortage warning for ALL ships


This strongly indicates Dilithium Shortage was meant to affect all ships under construction and not only the surplus i.e. similar effect as "Credit Deficit".

Hence the new code restores the functionality of BotF as initially intended by the developers :


Phantom-Dilithium Bug (fix) -> outdated; see below :!:

Code: Select all

In trek.exe at 0x5F313 replace:

8D 04 ED 00 00 00 00 01 E8 C1 E0 02 01 E8 C1 E0 02 89 C5 C1 E0 02 29 E8 05 98 1A 5B 00

with:

69 C5 BC 01 00 00 05 98 1A 5B 00 8B 68 64 3B 68 60 76 0A 31 D2 E9 C8 02 00 00 90 90 90


replacement at asm 45FF13:

imul    eax, ebp, 1BCh
add     eax, offset save_EmpsInfo
mov     ebp, [eax+64h]
cmp     ebp, [eax+60h]
jbe     short loc_45FF30
xor     edx, edx
jmp     loc_4601F5 (dilithium_shortage)
nop    (3x)
The AI cannot handle the strict phantom dilithium fix since it cannot remove surplus ships under construction when losing part of its dilithium sources.
So the AI can get stuck indefinitely without any ship production (even with dilithium) and also loses the industry outputs of said systems. :!:


Alternative method: Remove at 460159 dec dword ptr [eax+0Ch] & insert at 45FF94

Code: Select all

NAME: Phantom-Dilithium bug fix (with AI Workaround)
DESC: Relocates the dec dil count to the right location to remove the Phantom Dilithium exploit.
DESC: patch file name: phantom_dilithium_with_AI_workaround.patch
AUTHOR: Spocks-cuddly-tribble
URL: https://www.armadafleetcommand.com/onscreen/botf/viewtopic.php?p=15170#p15170

>> 0x0005f394 8b 84 24 30 01 00 00 8b 94 24 38 01 00 00
>> 0x0005f3a5 8b 9a 34 02 00 00 66 8b
>> 0x0005f559 ff 48 0c

<< 0x0005f394 ff 48 0c 8b 84 24 30 01 00 00 8b 58 9c 90
<< 0x0005f3a5 90 90 90 90 90 90 0f b7
<< 0x0005f559 90 90 90

#0045FF94     FF48 0C             DEC DWORD [EAX+C]       // dil count -1
#0045FF97     8B8424 30010000     MOV EAX, [ESP+130]      // systInfo+298
#0045FF9E     8B58 9C             MOV EBX, [EAX-64]       // systInfo+234 shipbuilding bonus (e.g. utopia planetia)
#0045FFA1     90                  NOP

#0045FFA5     909090909090        NOP
#0045FFAB     0FB755 04           MOVZX EDX,WORD [EBP+4]  // systInfo+84 invested industry

#00460159     909090              NOP                     // remove old dil count -1


Note there is still the Ship Yard Unpowered bug which allows shipbuilding despite of unpowered yard, but I doubt it's worth the effort to fix it.
Last edited by Spocks-cuddly-tribble on Tue Oct 31, 2023 1:01 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
dx_chaos
Ensign
Ensign
Posts: 37
Joined: Fri Jan 02, 2009 3:00 am

Post by dx_chaos »

Spock

This is very interesting stuff and as you say seems to take the game in the direction it was intended to be.

It would be great to see this implemented in one of the mods - has anyone any plans to do this?

Chaos
User avatar
thunderchero
Site Administrator aka Fleet Admiral
Site  Administrator aka Fleet Admiral
Posts: 7849
Joined: Fri Apr 25, 2008 2:00 am
Location: On a three month training mission, in command of the USS Valiant.

Post by thunderchero »

Spocks-cuddly-tribble,

I am just wondering, Is it possible to remove the 3 warning for those mods who want to keep to this option for building ships? like UDM (major ship building mod)

F1 Empire Info -> Count of systems with dilithium only
Summary / Systems -> no (On Hold) warning Regardless how many systems are building ships
F2 Build Queue -> no Dilithium Shortage warning Regardless how many systems are building ships

I know warning can be ignored but this would be nice to have this option for those who want it.

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

Post by Spocks-cuddly-tribble »

thunderchero wrote:F1 Empire Info -> Count of systems with dilithium only
Summary / Systems -> no (On Hold) warning Regardless how many systems are building ships
F2 Build Queue -> no Dilithium Shortage warning Regardless how many systems are building ships
EDIT(2nd): dilithium count (1)

Replace -> 4EA37A mov eax, [eax+10h]
with -> xor eax, eax (&nop)
=> trek.exe at 0xE977A change 8B 40 10 to 31 C0 90

For (2) & (3) remove:

441BBD mov edi, 0FFFFFFFCh -> (on hold)
4F2ECE ja loc_4F2F9B -> DIL_shortage

Also, I'd suggest remove:

45FF8E jz loc_4601F5

(disables any need for dilithium when shipbuilding)

and:

4F2EDF jz loc_4F3001 -> ship_yard_unpowered warning
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: 1884
Joined: Sun Apr 27, 2008 2:00 am

Post by Spocks-cuddly-tribble »

This code will turn phantom dilithium into an (almost) proper feature i.e. without any silly order-of-ships production wastages (both parts of damaged code moved to ship completion).

One can keep unlimited ships under construction (even without any dilithium) but still need dilithium at the turn of completion. Finalising more ships than available dilithium results only in a not completed surplus (in the order explained above) whilst the industry will be added (until max cost).

Code: Select all

trek.exe at 0x5F382

change:
8B 84 24 34 01 00 00 8B 50 0C 85 D2 0F 84 61 02 00 00
to:
90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90

=> remove asm 45FF82-45FF8E

AND

trek.exe at 0x5F53C

change:
31 C0 66 8B 55 04 66 8B 84 24 40 01 00 00 39 C2 0F 8C 18 01 00 00 8B 84 24 34 01 00 00 FF 48 0C 8B 57 12 8B 47 02 C1 FA 10 C1 F8 10
to:
0F B7 84 24 40 01 00 00 39 45 04 7C 0E 89 45 04 8B 84 24 34 01 00 00 83 78 0C 01 0F 8C 0D 01 00 00 FF 48 0C 0F B7 57 14 0F B7 47 04


=> at asm 46013C:
movzx     eax,  word ptr [esp+140h]
cmp       [ebp+4], eax
jl        short_14 bytes   //   not finished
mov       [ebp+4], eax
mov       eax, [esp+134h]
cmp       dword ptr [eax+0Ch], 1
jl        loc_46026A      //   not finished
dec       dword ptr [eax+0Ch]
movzx     edx, word ptr [edi+14h]
movzx     eax, word ptr [edi+4]
I don't know how many bugs is too many but that point is reached somewhere before however many in BotF is.
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 »

Wow, that's amazing, thank you very much :)
I did not expect this to be achievable.

Just wanting to make sure I understand you correctly:
At the global loop, every system which is currently producing a ship will still decrement the dilithium count. But no dilithium shortage will be induced.

In the second part, you modify an area that will only affect systems which are actually finishing a ship at the current turn. You read the dilithium count and stall the ship completion if there's no dilithium left.

Which leads to one question: How on earth did you free up enough space to squeeze in that many code lines? ^^

Spocks-cuddly-tribble wrote:
Gowron wrote:At the global loop, every system which is currently producing a ship will still decrement the dilithium count. But no dilithium shortage will be induced.
No, due to the bug, unmodded BotF never did decrement dil. count at this area. The "global" check (and jump statement) for dilithium shortage has been removed there.
Gowron wrote:In the second part, you modify an area that will only affect systems which are actually finishing a ship at the current turn. You read the dilithium count and stall the ship completion if there's no dilithium left.
Yes, whilst preventing project overflows.
A discovery consists in seeing something everybody has seen and at the same time thinking something nobody has thought yet.
User avatar
Spocks-cuddly-tribble
Code Master
Code Master
Posts: 1884
Joined: Sun Apr 27, 2008 2:00 am

Unfinished local/global Dilithium pools & AI Military Power Check

Post by Spocks-cuddly-tribble »

Following AI patches are slightly OT, but related via the broken/unfinished codes for local/global Dilithium pools which eventually caused the Phantom-Dilithium Bug/exploit (repost from UE Map Editor @ systInfo):

systInfo[+274/2CC] = Dilithium (running) -> loaded from systInfo[+13C/1FC]
systInfo[+278/2D0] = bugged AI (& dilithium shortage) Marker 1 = read Industry Output for Ship Construction (AI military power check)
systInfo[+27C/2D4] = build ship marker

Codes look like remains of an unfinished feature for global (+local) dilithium pools supposed to be used with ship specific dilithium costs.
Current state of affairs is the AI marker is set for ProvInfo star systems up to [number of running dilithium sources].
NOT sorted by pop, industry or even presence of shipyards i.e. just random system outputs. :mad:


The first patch corrects the marker to ship-producing systems up to the max dilithium capacity:

Code: Select all

NAME: AI Check of Industry Capacities for Ship Construction - Dilithium Allocation Fix
DESC: For mods with disabled dilithium requirements for finishing ships (e.g. UDM3) use deviation code (bottom).
AUTHOR: Spocks-cuddly-tribble
URL: https://www.armadafleetcommand.com/onscreen/botf/viewtopic.php?p=56856#p56856

>> 0x00060477 e8 54 22 fe ff 01 fb
>> 0x000604ac 8b 14
>> 0x000604af 8d 34 95 00 00 00 00 29 d6 c1 e6 02 29 d6 c1 e6 03 0f bf 18 69 db 28 03 00 00 8b 15 c8 36 5a 00 01 da 81 c2 40 02 00 00 01 f2 85 c9 76 1a
>> 0x000604ed d2

<< 0x00060477 03 df 8b 43 3c 90 90
<< 0x000604ac 6b 34
<< 0x000604af 58 0f bf 18 69 db 28 03 00 00 03 1d c8 36 5a 00 8d 94 33 40 02 00 00 85 c9 76 2d 83 7a 3c 01 75 27 90 90 90 90 90 90 90 90 90 90 90 90 90
<< 0x000604ed c2

#00461077     03DF              ADD EBX,EDI
#00461079     8B43 3C           MOV EAX,[EBX+3C]
#0046107C     9090              NOP

#004610AC     6B3424 58         IMUL ESI,[ESP],58
#004610B0     0FBF18            MOVSX EBX,WORD[EAX]
#004610B3     69DB 28030000     IMUL EBX,EBX,328
#004610B9     031D C8365A00     ADD EBX,[5A36C8]
#004610BF     8D9433 40020000   LEA EDX,[EBX+ESI+240]
#004610C6     85C9              TEST ECX,ECX
#004610C8     76 2D             JBE SHORT 4610F7 -> for mods with disabled dilithium requirements for finishing ships -> NOP
#004610CA     837A 3C 01        CMP DWORD[EDX+3C],1
#004610CE     75 27             JNZ SHORT 4610F7
#004610D0-4610DC      90        NOP

#004610EC    ^75 C2             JNZ SHORT 4610B0

# Code deviation for mods with disabled dilithium requirements for finishing ships (after main patch):
# >> 0x604C8 76 2D
# << 0x604C8 90 90

Second patch adjusts the ratio for the AI offensive military power check:

Code: Select all

NAME: AI Offensive Military Power Check - Ratio & Source Data Optimisation
DESC: Offensive War Fleet Cost / 8 + Industry Outputs for Ship Construction.
AUTHOR: Spocks-cuddly-tribble
URL: https://www.armadafleetcommand.com/onscreen/botf/viewtopic.php?p=56856#p56856

>> 0x0002cad6 8d 04 d5 00
>> 0x0002cadc 00 01 d0 c1 e0 02 01 d0 c1 e0 02 89 c2 c1 e0 02 29 d0
>> 0x0002cb4e 8d 04 d5 00
>> 0x0002cb54 00 01 d0 c1 e0 02
>> 0x0002cb5e 01 d0
>> 0x0002cb65 e0 02
>> 0x0002cb6a c2 c1 e0 02
>> 0x0002cb72 29 d0

<< 0x0002cad6 69 c2 bc 01
<< 0x0002cadc 8b 90 4c 1c 5b 00 c1 ea 03 89 54 24 24 90 90 90 90 90
<< 0x0002cb4e 69 c2 bc 01
<< 0x0002cb54 8b 90 4c 1c 5b 00
<< 0x0002cb5e 90 90
<< 0x0002cb65 ea 03
<< 0x0002cb6a 54 24 20 90
<< 0x0002cb72 90 90


#0042D6D6     69C2 BC010000     IMUL EAX,EDX,1BC
#0042D6DC     8B90 4C1C5B00     MOV EDX,[EAX+5B1C4C]
#0042D6E2     C1EA 03           SHR EDX,3
#0042D6E5     895424 24         MOV [ESP+24],EDX
#0042D6E9     9090909090        NOP


#0042D74E     69C2 BC010000     IMUL EAX,EDX,1BC
#0042D754     8B90 4C1C5B00     MOV EDX,[EAX+5B1C4C]

#0042D75E     9090              NOP

#0042D764     C1EA 03           SHR EDX,3

#0042D769     895424 20         MOV [ESP+20],EDX
#0042D76D     90                NOP

#0042D772     9090              NOP

When fleetwise on the weaker side, and interrupting ship production while upgrading systems, hostile AIs might seize the moment and attack. :twisted:
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 “Phantom Dilithium Bug (fix) -> OPTION”