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