From fb648d8772920aabfcae247e4109fb9e0c44ae87 Mon Sep 17 00:00:00 2001 From: kotijekriv Date: Wed, 2 Dec 2026 07:35:11 +0100 Subject: [PATCH] reimplement: SHC_3BB0A8C1_0x00549C70 100.0% Reimplemented UnitsState::processMeleeInitiation from 85.9% (gynt's text, on main since #238) to the original's code. reccmp reads 99.0%: the seven lines it still shows are the same instructions with the same operand under another label (below), and the status line counts the function as 100% with a note that says so, as main's file does for 0x004D9290. quick_diff below is tools/reimplementation-helper/batch/quick_diff.py: this file compiled alone against the original's disassembly, every global address folded to one token. A ratio from 0 to 1, not a reccmp percentage. - The scan of the unit's own tile is a do loop behind its own test. The original loads `this` into edi in front of the loop's test (0x00549F4E) and uses it up to 0x0054A458. As a for loop MSVC loads it inside the loop and again behind the 24 tile tests, and keeps a zero in edi from the loop's end to the function's end (quick_diff 0.8976 -> 0.9399). - goto attack: when the unit already fought is still in reach, the original turns towards it and jumps into the code that starts the attack: the jmp at 0x0054A52B lands at 0x0054A6BA, which is also reached by falling through from 0x0054A6B9. With a flag set in the loop and tested behind it MSVC keeps `this` in ebx over the rest of the function, where the original reads it from the stack for each call and has 0x6a and 8 in edi and edx (0x0054A6C5). - goto noTarget: "nobody found on the 24 tiles" and "no enemy around" end in the same three stores and the same return, which the original has once: the je at 0x00549FCB lands at 0x0054A652, which is also reached by falling through from 0x0054A650. Written twice, both copies stay. - The three together: 0.9796, with the original's 734 instructions. Other forms, in quick_diff only: 0.9640 with one goto or with none (a flag that skips the second search, the attack nested in an if), 0.9244 with the attack written out at both places. The real build is of the form with the two goto. - _adjacentTiles[13] adds [SomeY + 1][5] in front of [SomeY + 1][6], as _adjacentTiles[17] does with [2] and [3]: with [6] first MSVC put the two loads the other way round than the original has them (0x0054A2C0). quick_diff folds global addresses and did not show it; the first real build did. What reccmp still shows: seven instructions (0x0054A20F, 0x0054A233, 0x0054A239, 0x0054A3D9, 0x0054A3DF, 0x0054A403, 0x0054A425) read directionTranslationMatrix one row above the current one, six written [SomeY][-1] to [-7] (gynt's negative columns of the current row) and one [SomeY - 1][0]. The matrix is the first member of TileMapState, so their displacement is TileMapState's address less 4 to 32. In the original TileMapState is at 0x01A93208 and MinimapViewState (0x01A31610, 400376 bytes) ends where it starts, so reccmp prints MinimapViewState+400372 for TileMapState - 4. In the DLL TileMapState's instance has other data in front of it and reccmp prints that: ViewportRenderState+1699844 (it is 1699848 bytes long) with the units option on, function-resolver objects with it off. The compiler's listing has TileMapState's symbol less 4 to 32 in all seven, and all 85 global memory operands of the function are the original's in the original's order. Any form that reads the row above ([SomeY][-1], [SomeY - 1][7]) comes to the same displacement (both were compiled), and the label comes from the DLL's data layout, so no source form changes these lines; main's build shows the same seven. The status line counts the function the way the file does for 0x004D9290 (#510: the instance[i - 1] address label) and for 0x00405130, 0x004274F0 and 0x00429A40 (#408). The rest is gynt's, the handwritten block included (7005c48206 79.0%, 951fc132d0 84.5%, ff6859a51e 85.9%; main's status line still said 84.5%). The second search loses one level of nesting, so its lines show as changed. The bound of both searches is countof(field_0x11cf4[0]) in place of 24, the column count of the table they index: same object code. Measured with real builds on main 0cea04dbf4 (bundled MSVC1400-SP1, function and data resolvers on locally, reccmp), of this file together with the two /GL lines of #541: 99.0% in a small build with OPENSHC_FUNCTION_VISIBILITY_UNITS on (519 files, 529 functions), where every other function but #541's clearAllUnits reads as in the same build of main. With the option off the same list reads 99.0% as well, the same seven lines (and 98.8% for the text before the _adjacentTiles[13] change: those seven and that one sum). Two comments and the two loop bounds changed after the builds; every section of the object file has the same bytes and relocations as before. Co-authored-by: Gynt Co-Authored-By: Claude Fable 5.2 --- .../UnitsState/processMeleeInitiation.cpp | 189 +++++++++--------- status/addresses-SHC-3BB0A8C1.txt | 2 +- 2 files changed, 95 insertions(+), 96 deletions(-) diff --git a/src/OpenSHC/Map/Units/UnitsState/processMeleeInitiation.cpp b/src/OpenSHC/Map/Units/UnitsState/processMeleeInitiation.cpp index b55b9578f9..a6c96d5e9b 100644 --- a/src/OpenSHC/Map/Units/UnitsState/processMeleeInitiation.cpp +++ b/src/OpenSHC/Map/Units/UnitsState/processMeleeInitiation.cpp @@ -217,21 +217,25 @@ namespace Map { skip_0x20000: mov dword ptr [DAT_TileMapState::instance]TileMapState.field_0x554a48, edx } - for (_otherUnitID = DAT_TileMapState::instance.UnitLayer[DAT_TileMapState::instance.DAT_SomeTile]; - _otherUnitID > 0; _otherUnitID = this->units[_otherUnitID].nextUnitOnTheSameTile) - { - if (_otherUnitID != unitID) { - if (this->units[unitID].owner == 0) { - if (this->units[unitID].unitType != this->units[_otherUnitID].unitType) { + _otherUnitID = DAT_TileMapState::instance.UnitLayer[DAT_TileMapState::instance.DAT_SomeTile]; + if (_otherUnitID > 0) { + // a do loop behind its own test: a for loop loads `this` inside the loop, not in front of it + do { + if (_otherUnitID != unitID) { + if (this->units[unitID].owner == 0) { + if (this->units[unitID].unitType != this->units[_otherUnitID].unitType) { + _hasEnemyOnOwnTile = TRUE; + break; + } + } else if (DAT_GameState::instance.mapAndTime.playerTeams[_playerID] + != DAT_GameState::instance.mapAndTime + .playerTeams[this->units[_otherUnitID].owner]) { _hasEnemyOnOwnTile = TRUE; break; } - } else if (DAT_GameState::instance.mapAndTime.playerTeams[_playerID] - != DAT_GameState::instance.mapAndTime.playerTeams[this->units[_otherUnitID].owner]) { - _hasEnemyOnOwnTile = TRUE; - break; } - } + _otherUnitID = this->units[_otherUnitID].nextUnitOnTheSameTile; + } while (_otherUnitID > 0); } if ((DAT_TileMapState::instance.field_0x554a48 != 0) || (_hasEnemyOnOwnTile)) { for (int i = 0; i < countof(_adjacentTiles); ++i) { @@ -362,9 +366,9 @@ namespace Map { if ((DAT_TileMapState::instance.field_0x554a48 & (1 << 13)) != 0) { _adjacentTiles[13] = DAT_TileMapState::instance - .directionTranslationMatrix[DAT_TileMapState::instance.DAT_SomeY + 1][6] - + DAT_TileMapState::instance .directionTranslationMatrix[DAT_TileMapState::instance.DAT_SomeY + 1][5] + + DAT_TileMapState::instance + .directionTranslationMatrix[DAT_TileMapState::instance.DAT_SomeY + 1][6] + DAT_TileMapState::instance .directionTranslationMatrix[DAT_TileMapState::instance.DAT_SomeY][4] + this->units[unitID].tile; @@ -451,14 +455,15 @@ namespace Map { + DAT_TileMapState::instance .directionTranslationMatrix[DAT_TileMapState::instance.DAT_SomeY][0]; } - BOOLEnum _keepsTarget = FALSE; if (this->units[unitID].attackedUnitID != 0) { _otherUnitID = this->units[unitID].attackedUnitID; if (this->units[_otherUnitID].uid == this->units[unitID].field_0x340 && this->units[_otherUnitID].logicalState == OpenSHC::Map::Units::ULS_NORMAL && this->units[_otherUnitID].dying == 0) { - /* -- CHECK 24 TILES AROUND UNIT -- is the unit we already fight still in reach? */ - for (int _slot = -1; _slot < 24; ++_slot) { + // look for the unit already fought on its own tile and the 24 around it + for (int _slot = -1; + _slot < countof(DAT_UnitPropertiesDefinedData::instance.field_0x11cf4[0]); + ++_slot) { int _candidateTile; if (_slot == -1) { if (!_hasEnemyOnOwnTile) { @@ -481,99 +486,92 @@ namespace Map { if (_otherUnitID == MACRO_CALL_MEMBER(OpenSHC::Map::Units::UnitsState_Func::getEnemyUnitIDNearby, this)(unitID, _candidateTile, _totalHeight)) { - _keepsTarget = TRUE; - break; + MACRO_CALL_MEMBER( + OpenSHC::Map::Units::UnitsState_Func::setFacingDirectionTowardUnitMicro, + DAT_UnitsState::ptr)(unitID, _otherUnitID); + // goto for the match: a flag tested behind the loop keeps `this` in ebx + goto attack; } } } - if (!_keepsTarget) { - this->units[unitID].attackedUnitID = 0; - this->units[unitID].field_0x340 = 0; - this->units[unitID].field_0x3d2 = 0; - } + this->units[unitID].attackedUnitID = 0; + this->units[unitID].field_0x340 = 0; + this->units[unitID].field_0x3d2 = 0; } - if (_keepsTarget) { - MACRO_CALL_MEMBER(OpenSHC::Map::Units::UnitsState_Func::setFacingDirectionTowardUnitMicro, - DAT_UnitsState::ptr)(unitID, _otherUnitID); - } else { - /* -- CHECK 24 TILES AROUND UNIT -- look for someone new to fight */ - for (int _slot = -1; _slot < 24; ++_slot) { - int _candidateTile; - int _tileSlot; - if (_slot == -1) { - if (!_hasEnemyOnOwnTile) { - continue; - } - _tileSlot = this->units[unitID].facingDirection; - _candidateTile = this->units[unitID].tile; - } else { - _tileSlot = DAT_UnitPropertiesDefinedData::instance - .field_0x11cf4[this->units[unitID].facingDirection][_slot]; - _candidateTile = _adjacentTiles[_tileSlot]; - } - if (_candidateTile == 0) { + // look for someone new to fight on the 24 tiles around the unit + for (int _slot = -1; _slot < countof(DAT_UnitPropertiesDefinedData::instance.field_0x11cf4[0]); + ++_slot) { + int _candidateTile; + int _tileSlot; + if (_slot == -1) { + if (!_hasEnemyOnOwnTile) { continue; } - if (_tileSlot >= 8) { - if (abs(_totalHeight - - _neighbourHeights - [DAT_UnitPropertiesDefinedData::instance.field_0x12054[_tileSlot].x]) - > 32) { - continue; - } - int _secondHeightIndex - = DAT_UnitPropertiesDefinedData::instance.field_0x12054[_tileSlot].y; - if (_secondHeightIndex != -1) { - if (abs(_totalHeight - _neighbourHeights[_secondHeightIndex]) > 32) { - continue; - } - } - } - _otherUnitID - = MACRO_CALL_MEMBER(OpenSHC::Map::Units::UnitsState_Func::getEnemyUnitIDNearby, - this)(unitID, _candidateTile, _totalHeight); - if (_otherUnitID <= 0) { - continue; - } - if (this->units[_otherUnitID].logicalState != OpenSHC::Map::Units::ULS_NORMAL) { - continue; - } - if (this->units[_otherUnitID].dying != 0) { - continue; - } - if (abs((_totalHeight - this->units[_otherUnitID].buildingHeight) - - this->units[_otherUnitID].terrainOrClimbHeight) + _tileSlot = this->units[unitID].facingDirection; + _candidateTile = this->units[unitID].tile; + } else { + _tileSlot = DAT_UnitPropertiesDefinedData::instance + .field_0x11cf4[this->units[unitID].facingDirection][_slot]; + _candidateTile = _adjacentTiles[_tileSlot]; + } + if (_candidateTile == 0) { + continue; + } + if (_tileSlot >= 8) { + if (abs(_totalHeight + - _neighbourHeights + [DAT_UnitPropertiesDefinedData::instance.field_0x12054[_tileSlot].x]) > 32) { continue; } - if ((this->units[unitID].unitType == OpenSHC::Map::Units::UT_LIONSHWOLF - || this->units[unitID].unitType == OpenSHC::Map::Units::UT_CAGEDOG) - && this->units[_otherUnitID].isStalked != 0 && _slot >= 8) { - continue; + int _secondHeightIndex + = DAT_UnitPropertiesDefinedData::instance.field_0x12054[_tileSlot].y; + if (_secondHeightIndex != -1) { + if (abs(_totalHeight - _neighbourHeights[_secondHeightIndex]) > 32) { + continue; + } } - this->units[unitID].attackedUnitID = _otherUnitID; - this->units[unitID].field_0x340 = this->units[_otherUnitID].uid; - MACRO_CALL_MEMBER( - OpenSHC::Map::Units::UnitsState_Func::setFacingDirectionTowardUnitMicro, - DAT_UnitsState::ptr)(unitID, _otherUnitID); - break; } - if (this->units[unitID].attackedUnitID == 0) { - this->units[unitID].field_0x3d2 = 0; - this->units[unitID].attackedUnitID = 0; - this->units[unitID].field_0x340 = 0; - return; + _otherUnitID = MACRO_CALL_MEMBER(OpenSHC::Map::Units::UnitsState_Func::getEnemyUnitIDNearby, + this)(unitID, _candidateTile, _totalHeight); + if (_otherUnitID <= 0) { + continue; + } + if (this->units[_otherUnitID].logicalState != OpenSHC::Map::Units::ULS_NORMAL) { + continue; + } + if (this->units[_otherUnitID].dying != 0) { + continue; + } + if (abs((_totalHeight - this->units[_otherUnitID].buildingHeight) + - this->units[_otherUnitID].terrainOrClimbHeight) + > 32) { + continue; } - if (this->units[_otherUnitID].someUnitStat2_meleeDamageUnk != 0 - && this->units[_otherUnitID].attackedUnitID == 0 - && this->units[_otherUnitID].movementCooldown == 0) { - this->units[_otherUnitID].attackedUnitID = unitID; - this->units[_otherUnitID].field_0x340 = this->units[unitID].uid; - MACRO_CALL_MEMBER( - OpenSHC::Map::Units::UnitsState_Func::setFacingDirectionTowardUnitMicro, - DAT_UnitsState::ptr)(_otherUnitID, unitID); + if ((this->units[unitID].unitType == OpenSHC::Map::Units::UT_LIONSHWOLF + || this->units[unitID].unitType == OpenSHC::Map::Units::UT_CAGEDOG) + && this->units[_otherUnitID].isStalked != 0 && _slot >= 8) { + continue; } + this->units[unitID].attackedUnitID = _otherUnitID; + this->units[unitID].field_0x340 = this->units[_otherUnitID].uid; + MACRO_CALL_MEMBER(OpenSHC::Map::Units::UnitsState_Func::setFacingDirectionTowardUnitMicro, + DAT_UnitsState::ptr)(unitID, _otherUnitID); + break; + } + if (this->units[unitID].attackedUnitID == 0) { + // goto for the match: with the three stores written out here as well, both copies stay + goto noTarget; + } + if (this->units[_otherUnitID].someUnitStat2_meleeDamageUnk != 0 + && this->units[_otherUnitID].attackedUnitID == 0 + && this->units[_otherUnitID].movementCooldown == 0) { + this->units[_otherUnitID].attackedUnitID = unitID; + this->units[_otherUnitID].field_0x340 = this->units[unitID].uid; + MACRO_CALL_MEMBER(OpenSHC::Map::Units::UnitsState_Func::setFacingDirectionTowardUnitMicro, + DAT_UnitsState::ptr)(_otherUnitID, unitID); } + attack: if (this->units[unitID].state.generic != OpenSHC::Map::Units::States::US_MELEE_ATTACK) { if (this->units[unitID].movementRelated == 8) { MACRO_CALL_MEMBER(OpenSHC::Map::Units::UnitsState_Func::saveUnitStateBeforeInterruption, @@ -591,6 +589,7 @@ namespace Map { DAT_CurrentUnitSlotID::instance, this->units[unitID].attackedUnitID); return; } + noTarget: this->units[unitID].field_0x3d2 = 0; this->units[unitID].attackedUnitID = 0; this->units[unitID].field_0x340 = 0; diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index 98c4514966..c3fed4ed31 100644 --- a/status/addresses-SHC-3BB0A8C1.txt +++ b/status/addresses-SHC-3BB0A8C1.txt @@ -61409,7 +61409,7 @@ SHC_3BB0A8C1_0x00549B24 | 0.0% | Pending SHC_3BB0A8C1_0x00549B30 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x00549C70 | 84.5% | Style pass, asm differs +SHC_3BB0A8C1_0x00549C70 | 100.0% | Reimplemented (counted: only the address labels of the seven directionTranslationMatrix[SomeY - 1] operands differ) SHC_3BB0A8C1_0x0054A770 | 100.0% | Reimplemented