Author Topic: What's the best way to fix count.c (1) error message?  (Read 30963 times)

Offline war2Nostalgia

  • Critter
  • *
  • Posts: 2
    • View Profile
What's the best way to fix count.c (1) error message?
« on: September 25, 2025, 02:29:58 PM »
Hi!

I develop a program that allows me to modify units to be something else - for example upgrade trolls to Kargath. But that sometimes causes me count.c (1) error message. What would be the best way to fix it? When I googled it I found that War2BNE InSight has had it and fixed it but since it is closed source I can't see what they did. I was curious that since people on this forum seem to be into modding maybe someone here have experience on how to deal with count.c (1)?

Offline war2Nostalgia

  • Critter
  • *
  • Posts: 2
    • View Profile
[Solved] What's the best way to fix count.c (1) error message?
« Reply #1 on: September 28, 2025, 08:22:36 AM »
Did some debugging on my own and found some solution which I will share here in case someone else runs into the same problem. For background information, WC2 keeps counts of different units - how many grunts, trolls, ... do you have. When your unit dies WC 2 subtracts 1 from count. There is a check in WC 2 code that if count is already 0 before subtraction takes place then this error is thrown.

Now there is 2 possible solutions:
First is to keep unit counts updated every time you replace units which from programming point of view can be quite a bit of extra work.
Second solution is disable this check by replacing conditional jump jne with unconditional jump jmp. This allows unit count to underflow (0 - 1 = 65535 with 2 byte unsigned ints) but so far I did not experience any negative consequences. Scores on victory screen seem to calculated correctly and game crash.

Here is code extract from WC2 process.
Code: [Select]
Warcraft II BNE.exe+17855 - 66 83 3C 48  00       - cmp word ptr [eax+ecx*2],00 { 0 }
Warcraft II BNE.exe+1785A - 75 0F                 - jne "Warcraft II BNE.exe"+1786B { ->Warcraft II BNE.exe+1786B }
Warcraft II BNE.exe+1785C - 51                    - push ecx
Warcraft II BNE.exe+1785D - 52                    - push edx
Warcraft II BNE.exe+1785E - 68 5C564900           - push "Warcraft II BNE.exe"+9565C { ["count.c (1): %d %d"] }
Warcraft II BNE.exe+17863 - E8 78120700           - call "Warcraft II BNE.exe"+88AE0 { ->Warcraft II BNE.exe+88AE0 }
Warcraft II BNE.exe+17868 - 83 C4 0C              - add esp,0C { 12 }
Warcraft II BNE.exe+1786B - 33 D2                 - xor edx,edx

Cmp compares unit count to 0. Jne jumps to line xor edx,edx if it is not zero. If it is zero then code from push ecx to add esp,0C gets executed. Call "Warcraft II BNE.exe"+88AE0 is the function that crashes the game and displays error message. When condational jump if not equal (jne) is replaced with uncoditional jump (jmp) instruction then bad code is always skipped and error will never be thrown. It's easiest solution since it only needs to modify 1 byte.

TL;DR: At memory address Warcraft II BNE.exe+1785A replace byte 0x75 with 0xEB.

Offline Mistral

  • Administrator
  • Axe Thrower
  • *****
  • Posts: 473
    • View Profile
Re: What's the best way to fix count.c (1) error message?
« Reply #2 on: September 30, 2025, 04:29:02 AM »
consequences would be broken ai
game use those count tables to check for ai what units it have so it know what to build and when go attack
also some condtions in game checking those count tables so they will be broken too

so what program exactly you using to change units?

i have a function unit_convert in my plugins this function actually just calling default war2 function for units conversion
in normal game it happens for example when paladins upgraded and all knight units are changed to paladin
default in game function already have all stuff calculated inside and correctly update count tables.

it being called from 0x004532A0(byte player, byte unit, byte changed_unit, int allow_capture)

https://github.com/Mistral-war2ru/War2Mod-Server/blob/main/w2p.cpp#L692

Offline Mistral

  • Administrator
  • Axe Thrower
  • *****
  • Posts: 473
    • View Profile
Re: What's the best way to fix count.c (1) error message?
« Reply #3 on: September 30, 2025, 04:29:56 AM »
i also get all adress for count tables here
https://github.com/Mistral-war2ru/War2Mod-Server/blob/main/defs.h#L14
they count for both races
for example table i named PEON is counting peons AND peasants if player actually have both, like if you playing orc and captured allied human
there is also ACTIVE_* tables
they only count units that was created during the game itself and was not in pud map from start (or if they have active flag in pud map)
basicaly this thing only used for ai stuff, active units is what it can send to attack
« Last Edit: September 30, 2025, 04:43:30 AM by Mistral »