diff --git a/cmake/compiler-flags-gl.txt b/cmake/compiler-flags-gl.txt index 226cdc7ca6..8926a894ea 100644 --- a/cmake/compiler-flags-gl.txt +++ b/cmake/compiler-flags-gl.txt @@ -118,6 +118,22 @@ src/OpenSHC/UI/MenuItems/BuildingAndStatusMenu/MenuItemRenderFunction_BuildingAn src/OpenSHC/UI/MenuItems/BuildingAndStatusMenu/MenuItemRenderFunction_BuildingAndStatusMenu_GatehouseOpenClose.cpp src/OpenSHC/UI/MenuItems/BuildingAndStatusMenu/MenuItemRenderFunction_BuildingAndStatusMenu_WorkshopWeaponSelection.cpp src/OpenSHC/UI/MenuItems/General/MenuItemRenderFunction_General_RenderCurrentButtonWithPossibleAlphaTexOnScreenMenuSurface.cpp +src/OpenSHC/Map/TileMapState/getBuildingSizeIndexMappingData.cpp +src/OpenSHC/Map/TileMapState/setSignpostDistanceForCampaignMission.cpp +src/OpenSHC/Map/TileMapState/clearDrawBridgeWater.cpp +src/OpenSHC/Map/TileMapState/clearFixedSizeTwoBuildingFootprint.cpp +src/OpenSHC/Map/TileMapState/markBuildingFootprintFlag.cpp +src/OpenSHC/Map/TileMapState/storeMinAndMaxHeightOfArea.cpp +src/OpenSHC/Map/TileMapState/unmarkBuildingFootprintFlag.cpp +src/OpenSHC/Map/TileMapState/updateGameRelatedValue.cpp +src/OpenSHC/Map/TileMapState/clearBuildingFromTerrain.cpp +src/OpenSHC/Map/TileMapState/clearBuildingFootprintAndResetUnits.cpp +src/OpenSHC/Map/TileMapState/computeWallCornerRenderRotation.cpp +src/OpenSHC/Map/TileMapState/isWallConnectionHeightValid.cpp +src/OpenSHC/Map/TileMapState/getWallFlagForOrientedDirection.cpp +src/OpenSHC/Map/TileMapState/setupAllMapSections.cpp +src/OpenSHC/Map/TileMapState/setupLogicalMapBorders.cpp +src/OpenSHC/Map/TileMapState/updateShowHiLayerOrResetChangedLayer.cpp src/OpenSHC/UI/DisplayElements/RenderTimeUntilDefeatDisplayElement.cpp src/OpenSHC/UI/DisplayElements/GetIfDisplayElementStateNotZero.cpp src/OpenSHC/UI/DisplayElements/FindDisplayElementWithID.cpp diff --git a/src/OpenSHC/Map/TileMapState/Constructor_TileMapState.cpp b/src/OpenSHC/Map/TileMapState/Constructor_TileMapState.cpp new file mode 100644 index 0000000000..4a5e166de1 --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/Constructor_TileMapState.cpp @@ -0,0 +1,65 @@ +#include "../TileMapState.func.hpp" + +#include "OpenSHC/Commands/MappersEnum.hpp" +#include "OpenSHC/Map/Enums/LoweredViewTransition.hpp" +#include "OpenSHC/Map/Rocks/Enums/EditorRockType.hpp" +#include "OpenSHC/WindowsHelper/Enums/BOOLEnum.hpp" + +#include "OpenSHC/Globals/DAT_GameCore.hpp" +#include "OpenSHC/Globals/DAT_GameState.hpp" + +namespace OpenSHC { +namespace Map { + + // FUNCTION: STRONGHOLDCRUSADER 0x00515F40 + TileMapState* TileMapState::Constructor_TileMapState() + { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::setupMovementDirectionTranslationMatrix, this)(); + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::setupBuildingSizeIndexMapping, this)(); + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::setupTileMapSections, this)(); + this->editorActiveBrush = 7; + this->refreshCertainTileMap = OpenSHC::Map::Enums::LVT_SET_NORMAL; + this->counter1 = 0; + this->mapperMax = FALSE; + this->editorSnapToMode = 0; + this->editorAffectType = 0; + this->unknownZero_0x554900 = 0; + this->currentMapperCommand = OpenSHC::Commands::M_MAPPER_NULL; + this->refreshRelatedOne = 1; + this->disableElevationRendering = 0; + this->refreshRelatedTwo = 0; + this->forceUpdateLogicalAndMiscDisplayLayers = TRUE; + this->forceUpdateTextureTilemap = TRUE; + this->forceUpdateGFXLayers = TRUE; + this->refreshCertainTileMap_old = OpenSHC::Map::Enums::LVT_RAISE; + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::setChangedLayerToThreeAndMapping0x40x40, this)(); + this->moatTileCount = 0; + this->skipPlacementCheck = 0; + this->gateOrientationVariant = 80; + this->lastTime = timeGetTime(); + this->rockOrientation = OpenSHC::Map::Enums::MO_NORTH; + this->editorRockType = OpenSHC::Map::Rocks::Enums::ERT_NONE; + this->cursorAnimationTime = timeGetTime(); + this->cursorOverlayAnimationFrame = 1; + this->temporaryTerrainTypeIndex = 0; + this->skipPlacementFailChecks = 0; + DAT_GameCore::instance.isTimeHalted = FALSE; + DAT_GameState::instance.mapAndTime.skirmishFogOfWar = 0; + this->ptr_LogicLayer = this->LogicLayer; + this->ptr_TerrainTypeTileMap = this->Logic2Layer; + this->ptr_TerrainHeightTileMap = this->HeightLayer; + this->ptr_ChangedLayer = this->ChangedLayer; + this->ptr_OccupancyLayer = this->OccupancyLayer; + this->ptr_DamageLayer = this->DamageLayer; + this->ptr_PathConnectionLayer = this->PathConnectionLayer; + this->ptr_MovementDirectionTranslationMatrix = this; + this->ptr_SpecialAreasArray = this->specialAreasArray; + this->ptr_AIZoneLayer = this->AIZoneLayer; + this->ptr_MiscDisplayLayer = this->MiscDisplayLayer; + this->currentMoatCount = 16000; + this->maxPitchDitchCount = 4000; + return this; + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/advanceMoatDigProgress.cpp b/src/OpenSHC/Map/TileMapState/advanceMoatDigProgress.cpp new file mode 100644 index 0000000000..b9fb0b022f --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/advanceMoatDigProgress.cpp @@ -0,0 +1,43 @@ +#include "../TileMapState.func.hpp" + +#include "OpenSHC/Map/Navigation/PathFindingState.func.hpp" +#include "OpenSHC/UI/MinimapViewState.func.hpp" + +#include "OpenSHC/Globals/DAT_MinimapViewState.hpp" +#include "OpenSHC/Globals/DAT_PathFindingState.hpp" + +namespace OpenSHC { +namespace Map { + + using OpenSHC::Map::LogicHelpers::L2_NONE; + using OpenSHC::Map::LogicHelpers::L_MOAT; + using OpenSHC::Map::LogicHelpers::L_MOAT_DUG_OR_PLANNED; + using OpenSHC::Map::LogicHelpers::L_PLAIN2_AND_PITCH; + + // FUNCTION: STRONGHOLDCRUSADER 0x00500500 + BOOLEnum TileMapState::advanceMoatDigProgress(int moatID) + { + this->moats[moatID].fillProgress += 1; + if (this->moats[moatID].fillProgress >= 4) { + int tile = this->moats[moatID].tile; + this->moats[moatID].someCountDown = 0; + this->moats[moatID].fillProgress = 4; + this->moats[moatID].stage = 2; + this->HeightLayer[tile] = 0; + this->LogicLayer[this->moats[moatID].tile] &= ~L_MOAT_DUG_OR_PLANNED; + this->LogicLayer[this->moats[moatID].tile] &= ~L_PLAIN2_AND_PITCH; + this->LogicLayer[this->moats[moatID].tile] |= L_MOAT; + this->Logic2Layer[this->moats[moatID].tile] = L2_NONE; + MACRO_CALL_MEMBER(OpenSHC::Map::Navigation::PathFindingState_Func::updateWalkAndPathLayer, + DAT_PathFindingState::ptr)(2, this->moats[moatID].x, this->moats[moatID].y); + MACRO_CALL_MEMBER(OpenSHC::Map::Navigation::PathFindingState_Func::updatePathLinkagesInAllEightDirections, + DAT_PathFindingState::ptr)(this->moats[moatID].y, this->moats[moatID].tile); + DAT_PathFindingState::instance.toggleUpdateSeparateAreaTileMap = 1; + MACRO_CALL_MEMBER(OpenSHC::UI::MinimapViewState_Func::triggerMinimapRedraw, DAT_MinimapViewState::ptr)(1); + return TRUE; + } + return FALSE; + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/applyRockGraphicsToFootprint.cpp b/src/OpenSHC/Map/TileMapState/applyRockGraphicsToFootprint.cpp new file mode 100644 index 0000000000..d4590824e7 --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/applyRockGraphicsToFootprint.cpp @@ -0,0 +1,59 @@ +#include "../TileMapState.func.hpp" + +#include "OpenSHC/Map/LandscapeState.func.hpp" +#include "OpenSHC/Map/Enums/CardinalDirection.hpp" + +#include "OpenSHC/Globals/DAT_LandscapeState.hpp" +#include "OpenSHC/Globals/DAT_TileMapState.hpp" +#include "OpenSHC/Globals/DAT_ViewportRenderState.hpp" +#include "OpenSHC/Globals/GMFirstPictureIndex.hpp" + +namespace OpenSHC { +namespace Map { + + using OpenSHC::Map::Enums::CD_UPPER_LIMIT; + using OpenSHC::Map::LogicHelpers::L_WALL_OR_GATEHOUSE; + + // FUNCTION: STRONGHOLDCRUSADER 0x004FB770 + void TileMapState::applyRockGraphicsToFootprint(int rockID) + { + int rockX = DAT_LandscapeState::instance.rocks[rockID].x; + int rockY = DAT_LandscapeState::instance.rocks[rockID].y; + int orientation = DAT_LandscapeState::instance.rocks[rockID].orientation + this->mapOrientation; + int index = 0; + if (orientation >= CD_UPPER_LIMIT) { + orientation -= CD_UPPER_LIMIT; + } + int size = DAT_LandscapeState::instance.rocks[rockID].size; + int surface = (orientation / 2) * size * size; + + do { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::getBuildingSizeIndexMappingData, this)( + index, DAT_LandscapeState::instance.rocks[rockID].size); + int tile = DAT_ViewportRenderState::instance.translationMatrix[this->buildingY + rockY].addXgetTile + rockX + + this->buildingX; + if ((this->LogicLayer[tile] & L_WALL_OR_GATEHOUSE) == 0) { + if (this->disableElevationRendering == 0) { + this->GfxLayer[tile] + = GMFirstPictureIndex::instance[DAT_LandscapeState::instance.rocks[rockID].unknownGMID] + + DAT_LandscapeState::instance.rocks[rockID].gfx + this->buildingOrientedPartIndex + surface + - 1; + } else if (DAT_LandscapeState::instance.rocks[rockID].size == 1) { + this->GfxLayer[tile] + = GMFirstPictureIndex::instance[DAT_LandscapeState::instance.rocks[rockID].unknownGMID] + + DAT_LandscapeState::instance.rocks[rockID].gfx + this->buildingOrientedPartIndex + surface + - 1; + } else { + this->GfxLayer[tile] + = MACRO_CALL_MEMBER(OpenSHC::Map::LandscapeState_Func::getRandomRockImageOffset, + DAT_LandscapeState::ptr)((DAT_TileMapState::instance.RandomLayer[tile] & 3) + 1) + + GMFirstPictureIndex::instance[DAT_LandscapeState::instance.rocks[rockID].unknownGMID] + + surface - 1; + } + } + index++; + } while (index < this->constructionTileCount); + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/changeMapOrientation.cpp b/src/OpenSHC/Map/TileMapState/changeMapOrientation.cpp new file mode 100644 index 0000000000..7e6992e9b2 --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/changeMapOrientation.cpp @@ -0,0 +1,58 @@ +#include "../TileMapState.func.hpp" + +#include "OpenSHC/Map/Buildings/BuildingsState.func.hpp" +#include "OpenSHC/Rendering/ViewportRenderState.func.hpp" +#include "OpenSHC/Map/Enums/CardinalDirection.hpp" + +#include "OpenSHC/Globals/DAT_BuildingsState.hpp" +#include "OpenSHC/Globals/DAT_TileMapState.hpp" +#include "OpenSHC/Globals/DAT_ViewportRenderState.hpp" + +namespace OpenSHC { +namespace Map { + + // FUNCTION: STRONGHOLDCRUSADER 0x00501B90 + void TileMapState::changeMapOrientation(MapOrientation mapOrientation) + { + MACRO_CALL_MEMBER(OpenSHC::Rendering::ViewportRenderState_Func::saveFocusTileAndCenterPreview, + DAT_ViewportRenderState::ptr)(); + DAT_TileMapState::instance.mapOrientation = mapOrientation; + MACRO_CALL_MEMBER( + OpenSHC::Rendering::ViewportRenderState_Func::restoreFocusTile, DAT_ViewportRenderState::ptr)(); + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::forceFullTileMapRedraw, this)(); + MACRO_CALL_MEMBER( + OpenSHC::Map::Buildings::BuildingsState_Func::refreshAllBuildingTileDisplays, DAT_BuildingsState::ptr)(); + DAT_BuildingsState::instance.field_0x10 = 5; + switch (this->mapOrientation) { + case OpenSHC::Map::Enums::MO_NORTH: + this->screenSouthEastDirection = OpenSHC::Map::Enums::CD_SOUTHEAST; + this->orientedRowStep = 1; + this->screenEastDirection = OpenSHC::Map::Enums::CD_EAST; + this->screenSouthDirection = OpenSHC::Map::Enums::CD_SOUTH; + this->screenSouthWestDirection = OpenSHC::Map::Enums::CD_SOUTHWEST; + return; + case OpenSHC::Map::Enums::MO_EAST: + this->screenSouthEastDirection = OpenSHC::Map::Enums::CD_SOUTHWEST; + this->orientedRowStep = 1; + this->screenEastDirection = OpenSHC::Map::Enums::CD_SOUTH; + this->screenSouthDirection = OpenSHC::Map::Enums::CD_WEST; + this->screenSouthWestDirection = OpenSHC::Map::Enums::CD_NORTHWEST; + return; + case OpenSHC::Map::Enums::MO_SOUTH: + this->screenSouthEastDirection = OpenSHC::Map::Enums::CD_NORTHWEST; + this->screenEastDirection = OpenSHC::Map::Enums::CD_WEST; + this->screenSouthDirection = OpenSHC::Map::Enums::CD_NORTH; + this->screenSouthWestDirection = OpenSHC::Map::Enums::CD_NORTHEAST; + this->orientedRowStep = -1; + return; + case OpenSHC::Map::Enums::MO_WEST: + this->screenSouthEastDirection = OpenSHC::Map::Enums::CD_NORTHEAST; + this->screenEastDirection = OpenSHC::Map::Enums::CD_NORTH; + this->screenSouthDirection = OpenSHC::Map::Enums::CD_EAST; + this->screenSouthWestDirection = OpenSHC::Map::Enums::CD_SOUTHEAST; + this->orientedRowStep = -1; + } + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/clearBuildingDisplayFlagsAndEntities.cpp b/src/OpenSHC/Map/TileMapState/clearBuildingDisplayFlagsAndEntities.cpp new file mode 100644 index 0000000000..765f309fe3 --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/clearBuildingDisplayFlagsAndEntities.cpp @@ -0,0 +1,38 @@ +#include "../TileMapState.func.hpp" + +#include "OpenSHC/Map/Entities/EntityState.func.hpp" +#include "OpenSHC/Map/LogicHelpers/MiscDisplay.hpp" + +#include "OpenSHC/Globals/DAT_BuildingsState.hpp" +#include "OpenSHC/Globals/DAT_EntityState.hpp" +#include "OpenSHC/Globals/DAT_ViewportRenderState.hpp" + +namespace OpenSHC { +namespace Map { + + using OpenSHC::Map::LogicHelpers::MD_BUILDING_TILES; + + // FUNCTION: STRONGHOLDCRUSADER 0x004FB360 + void TileMapState::clearBuildingDisplayFlagsAndEntities(int buildingID, int destroyEntities) + { + int buildingSizeTileIndex = 0; + int buildingX = DAT_BuildingsState::instance.buildings[buildingID].x; + int buildingY = DAT_BuildingsState::instance.buildings[buildingID].y; + uint widthOrHeight = DAT_BuildingsState::instance.buildings[buildingID].widthOrHeight; + + do { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::getBuildingSizeIndexMappingData, this)( + buildingSizeTileIndex, widthOrHeight); + int tileUnk = DAT_ViewportRenderState::instance.translationMatrix[this->buildingY + buildingY].addXgetTile + + this->buildingX + buildingX; + this->MiscDisplayLayer[tileUnk] &= ~MD_BUILDING_TILES; + if (destroyEntities == 1) { + MACRO_CALL_MEMBER( + OpenSHC::Map::Entities::EntityState_Func::destroyEntitiesOnTile, DAT_EntityState::ptr)(tileUnk); + } + buildingSizeTileIndex++; + } while (buildingSizeTileIndex < this->constructionTileCount); + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/clearBuildingFootprintAndRemoveSiegeTower.cpp b/src/OpenSHC/Map/TileMapState/clearBuildingFootprintAndRemoveSiegeTower.cpp new file mode 100644 index 0000000000..14a43193bf --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/clearBuildingFootprintAndRemoveSiegeTower.cpp @@ -0,0 +1,46 @@ +#include "../TileMapState.func.hpp" + +#include "OpenSHC/Map/Navigation/PathFindingState.func.hpp" +#include "OpenSHC/Map/Units/States/UnitState.hpp" +#include "OpenSHC/Map/Units/UnitType.hpp" + +#include "OpenSHC/Globals/DAT_PathFindingState.hpp" +#include "OpenSHC/Globals/DAT_UnitsState.hpp" +#include "OpenSHC/Globals/DAT_ViewportRenderState.hpp" + +namespace OpenSHC { +namespace Map { + + using OpenSHC::Map::LogicHelpers::L_BUILDING; + using OpenSHC::Map::LogicHelpers::L_CRENEL; + using OpenSHC::Map::LogicHelpers::L_CRENEL_VARIATIONUnk; + using OpenSHC::Map::LogicHelpers::L_KEEP_NON_MANOR_HOUSE; + using OpenSHC::Map::LogicHelpers::L_STAIRS; + using OpenSHC::Map::LogicHelpers::L_WALL_OR_GATEHOUSE; + + // FUNCTION: STRONGHOLDCRUSADER 0x004FAC70 + void TileMapState::clearBuildingFootprintAndRemoveSiegeTower(int x, int y, int size) + { + int tileIndex = 0; + + do { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::getBuildingSizeIndexMappingData, this)(tileIndex, size); + int tile = DAT_ViewportRenderState::instance.translationMatrix[this->buildingY + y].addXgetTile + + this->buildingX + x; + this->LogicLayer[tile] &= ~(L_WALL_OR_GATEHOUSE | L_CRENEL | L_BUILDING | L_STAIRS | L_CRENEL_VARIATIONUnk + | L_KEEP_NON_MANOR_HOUSE); + this->BuildingLayer[tile] = 0; + this->HeightLayer[tile] = this->DefaultHeightLayer[tile]; + MACRO_CALL_MEMBER(OpenSHC::Map::Navigation::PathFindingState_Func::updatePathLinkagesInAllEightDirections, + DAT_PathFindingState::ptr)( + DAT_ViewportRenderState::instance.tileTranslationMatrix_YComponent[tile], tile); + int unitID = this->UnitLayer[tile]; + if (unitID != 0 && DAT_UnitsState::instance.units[unitID].unitType == OpenSHC::Map::Units::UT_S_TOWER) { + DAT_UnitsState::instance.units[unitID].state.generic = OpenSHC::Map::Units::States::US_DISAPPEAR; + } + tileIndex++; + } while (tileIndex < this->constructionTileCount); + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/clearBuildingFootprintAndResetUnits.cpp b/src/OpenSHC/Map/TileMapState/clearBuildingFootprintAndResetUnits.cpp new file mode 100644 index 0000000000..daada49a3e --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/clearBuildingFootprintAndResetUnits.cpp @@ -0,0 +1,60 @@ +#include "../TileMapState.func.hpp" + +#include "OpenSHC/Map/LogicHelpers/MiscDisplay.hpp" +#include "OpenSHC/WindowsHelper/Enums/BOOLEnum.hpp" + +#include "OpenSHC/Globals/DAT_BuildingsState.hpp" +#include "OpenSHC/Globals/DAT_UnitsState.hpp" +#include "OpenSHC/Globals/DAT_ViewportRenderState.hpp" + +namespace OpenSHC { +namespace Map { + + using OpenSHC::Map::LogicHelpers::L_BUILDING; + using OpenSHC::Map::LogicHelpers::L_CRENEL; + using OpenSHC::Map::LogicHelpers::L_CRENEL_VARIATIONUnk; + using OpenSHC::Map::LogicHelpers::L_KEEP_NON_MANOR_HOUSE; + using OpenSHC::Map::LogicHelpers::L_STAIRS; + using OpenSHC::Map::LogicHelpers::L_WALL_OR_GATEHOUSE; + using OpenSHC::Map::LogicHelpers::MD_GATEHOUSE_RAISED; + using OpenSHC::Map::LogicHelpers::MD_RUBBLE_EDGEUnk; + using OpenSHC::Map::LogicHelpers::MD_RUBBLE_INNERUnk; + + // FUNCTION: STRONGHOLDCRUSADER 0x004FAB70 + void TileMapState::clearBuildingFootprintAndResetUnits(int x, int y, int size) + { + int buildingSizeTileIndex = 0; + + do { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::getBuildingSizeIndexMappingData, this)( + buildingSizeTileIndex, size); + int tile = DAT_ViewportRenderState::instance.translationMatrix[this->buildingY + y].addXgetTile + + this->buildingX + x; + this->LogicLayer[tile] &= ~(L_WALL_OR_GATEHOUSE | L_CRENEL | L_BUILDING | L_STAIRS | L_CRENEL_VARIATIONUnk + | L_KEEP_NON_MANOR_HOUSE); + if ((this->MiscDisplayLayer[tile] & MD_GATEHOUSE_RAISED) != 0) { + this->MiscDisplayLayer[tile] &= ~MD_GATEHOUSE_RAISED; + this->HeightLayer[tile] = this->DefaultHeightLayer[tile]; + } + int buildingID = this->BuildingLayer[tile]; + this->BuildingLayer[tile] = 0; + if (DAT_BuildingsState::instance.buildings[buildingID].noRubble == 0) { + this->BuildingWasLayer[tile] = 0; + } else if (MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::isFootprintIndexOnEdge, this)( + buildingSizeTileIndex, size)) { + this->MiscDisplayLayer[tile] |= MD_RUBBLE_EDGEUnk; + } else { + this->MiscDisplayLayer[tile] |= MD_RUBBLE_INNERUnk; + } + for (int unitID = this->UnitLayer[tile]; unitID != 0; + unitID = DAT_UnitsState::instance.units[unitID].nextUnitOnTheSameTile) { + DAT_UnitsState::instance.units[unitID].vanish = 0; + DAT_UnitsState::instance.units[unitID].unknownCountdown_0x402 = 0; + } + buildingSizeTileIndex++; + this->ChangedLayer[tile] = 2; + } while (buildingSizeTileIndex < this->constructionTileCount); + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/clearBuildingFootprintWithEdgeRubble.cpp b/src/OpenSHC/Map/TileMapState/clearBuildingFootprintWithEdgeRubble.cpp new file mode 100644 index 0000000000..a0c0e390e3 --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/clearBuildingFootprintWithEdgeRubble.cpp @@ -0,0 +1,54 @@ +#include "../TileMapState.func.hpp" + +#include "OpenSHC/Map/LogicHelpers/MiscDisplay.hpp" +#include "OpenSHC/WindowsHelper/Enums/BOOLEnum.hpp" + +#include "OpenSHC/Globals/DAT_BuildingsState.hpp" +#include "OpenSHC/Globals/DAT_ViewportRenderState.hpp" + +namespace OpenSHC { +namespace Map { + + using OpenSHC::Map::LogicHelpers::L_BUILDING; + using OpenSHC::Map::LogicHelpers::L_CRENEL; + using OpenSHC::Map::LogicHelpers::L_CRENEL_VARIATIONUnk; + using OpenSHC::Map::LogicHelpers::L_KEEP_NON_MANOR_HOUSE; + using OpenSHC::Map::LogicHelpers::L_STAIRS; + using OpenSHC::Map::LogicHelpers::L_WALL_OR_GATEHOUSE; + using OpenSHC::Map::LogicHelpers::MD_GATEHOUSE_RAISED; + using OpenSHC::Map::LogicHelpers::MD_RUBBLE_EDGEUnk; + using OpenSHC::Map::LogicHelpers::MD_RUBBLE_INNERUnk; + + // FUNCTION: STRONGHOLDCRUSADER 0x004FAD70 + void TileMapState::clearBuildingFootprintWithEdgeRubble(int x, int y, int size) + { + int buildingSizeTileIndex = 0; + + do { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::getBuildingSizeIndexMappingData, this)( + buildingSizeTileIndex, size); + int tile = DAT_ViewportRenderState::instance.translationMatrix[this->buildingY + y].addXgetTile + + this->buildingX + x; + this->LogicLayer[tile] &= ~(L_WALL_OR_GATEHOUSE | L_CRENEL | L_BUILDING | L_STAIRS | L_CRENEL_VARIATIONUnk + | L_KEEP_NON_MANOR_HOUSE); + if ((this->MiscDisplayLayer[tile] & MD_GATEHOUSE_RAISED) != 0) { + this->MiscDisplayLayer[tile] &= ~MD_GATEHOUSE_RAISED; + this->HeightLayer[tile] = this->DefaultHeightLayer[tile]; + } + int buildingID = this->BuildingLayer[tile]; + this->BuildingLayer[tile] = 0; + if (DAT_BuildingsState::instance.buildings[buildingID].noRubble == 0) { + this->BuildingWasLayer[tile] = 0; + } else if (MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::isFootprintIndexOnEdge, this)( + buildingSizeTileIndex, size)) { + this->MiscDisplayLayer[tile] |= MD_RUBBLE_EDGEUnk; + } else { + this->MiscDisplayLayer[tile] |= MD_RUBBLE_INNERUnk; + } + buildingSizeTileIndex++; + this->ChangedLayer[tile] = 2; + } while (buildingSizeTileIndex < this->constructionTileCount); + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/clearBuildingFromTerrain.cpp b/src/OpenSHC/Map/TileMapState/clearBuildingFromTerrain.cpp new file mode 100644 index 0000000000..85b7592e92 --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/clearBuildingFromTerrain.cpp @@ -0,0 +1,118 @@ +#include "../TileMapState.func.hpp" + +#include "OpenSHC/Map/Navigation/PathFindingState.func.hpp" +#include "OpenSHC/UI/MinimapViewState.func.hpp" +#include "OpenSHC/Map/Buildings/BuildingType.hpp" +#include "OpenSHC/WindowsHelper/Enums/BOOLEnum.hpp" + +#include "OpenSHC/Globals/DAT_BuildingsState.hpp" +#include "OpenSHC/Globals/DAT_MinimapViewState.hpp" +#include "OpenSHC/Globals/DAT_PathFindingState.hpp" +#include "OpenSHC/Globals/DAT_TileMapState.hpp" + +namespace OpenSHC { +namespace Map { + + // FUNCTION: STRONGHOLDCRUSADER 0x00515A20 + void TileMapState::clearBuildingFromTerrain(int buildingID) + { + uint x = DAT_BuildingsState::instance.buildings[buildingID].x; + uint y = DAT_BuildingsState::instance.buildings[buildingID].y; + uint size = DAT_BuildingsState::instance.buildings[buildingID].widthOrHeight; + if (DAT_BuildingsState::instance.buildings[buildingID].buildingType != OpenSHC::Map::Buildings::BT_KILLINGPIT) { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::validateBuildingPlacementAtTile, this)( + DAT_BuildingsState::instance.buildings[buildingID].owner, x, y); + if (this->buildingPlacementFail != FALSE) { + return; + } + } + + if (DAT_BuildingsState::instance.buildings[buildingID].buildingType == OpenSHC::Map::Buildings::BT_STOCKPILE) { + if (DAT_BuildingsState::instance.buildings[buildingID].field_0x190 == 1) { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::clearStockpileFootprintTiles, this)(x, y); + } + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::clearFixedSizeTwoBuildingFootprint, this)(x, y); + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::updateAreaBasedOnSurrounding, this)(x, y, size); + } else if (DAT_BuildingsState::instance.buildings[buildingID].buildingType + == OpenSHC::Map::Buildings::BT_WHEATFARM) { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::clearBuildingFootprintAndResetUnits, this)(x, y, size); + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::clearBuildingDisplayFlagsAndEntities, this)( + buildingID, 1); + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::clearBuildingTilesAndTrees, this)(buildingID, 36); + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::unmarkBuildingFootprintFlag, this)(x, y, 9); + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::updateAreaBasedOnSurrounding, this)(x, y, 9); + size = 6; + } else if (DAT_BuildingsState::instance.buildings[buildingID].buildingType + == OpenSHC::Map::Buildings::BT_HOPFARM) { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::clearBuildingFootprintAndResetUnits, this)(x, y, size); + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::clearBuildingDisplayFlagsAndEntities, this)( + buildingID, 1); + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::clearBuildingTilesAndTrees, this)(buildingID, 24); + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::unmarkBuildingFootprintFlag, this)(x, y, 9); + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::updateAreaBasedOnSurrounding, this)(x, y, 9); + size = 6; + } else if (DAT_BuildingsState::instance.buildings[buildingID].buildingType + == OpenSHC::Map::Buildings::BT_DAIRYFARM) { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::clearBuildingFootprintAndResetUnits, this)(x, y, size); + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::clearBuildingDisplayFlagsAndEntities, this)( + buildingID, 1); + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::clearBuildingTilesAndTrees, this)(buildingID, 27); + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::unmarkBuildingFootprintFlag, this)(x, y, 10); + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::updateAreaBasedOnSurrounding, this)(x, y, 10); + size = 6; + } else if (DAT_BuildingsState::instance.buildings[buildingID].buildingType + == OpenSHC::Map::Buildings::BT_APPLEFARM) { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::clearBuildingFootprintAndResetUnits, this)(x, y, size); + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::clearBuildingDisplayFlagsAndEntities, this)( + buildingID, 1); + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::clearBuildingTilesAndTrees, this)(buildingID, 8); + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::unmarkBuildingFootprintFlag, this)(x, y, 11); + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::updateAreaBasedOnSurrounding, this)(x, y, 11); + size = 6; + } else if (DAT_BuildingsState::instance.buildings[buildingID].buildingType + == OpenSHC::Map::Buildings::BT_DRAWBRIDGE) { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::clearSizeFiveBuildingFootprintAndMoats, this)(x, y); + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::clearBuildingDisplayFlagsAndEntities, this)( + buildingID, 1); + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::updateAreaBasedOnSurrounding, this)(x, y, size); + } else if (DAT_BuildingsState::instance.buildings[buildingID].buildingType + == OpenSHC::Map::Buildings::BT_CATAPULT + || DAT_BuildingsState::instance.buildings[buildingID].buildingType == OpenSHC::Map::Buildings::BT_TREBUCHET + || DAT_BuildingsState::instance.buildings[buildingID].buildingType + == OpenSHC::Map::Buildings::BT_BATTERINGRAM + || DAT_BuildingsState::instance.buildings[buildingID].buildingType == OpenSHC::Map::Buildings::BT_SIEGETOWER + || DAT_BuildingsState::instance.buildings[buildingID].buildingType == OpenSHC::Map::Buildings::BT_SHIELD + || DAT_BuildingsState::instance.buildings[buildingID].buildingType + == OpenSHC::Map::Buildings::BT_FIREBALLISTA) { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::clearBuildingFootprintWithEdgeRubble, this)(x, y, size); + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::clearBuildingDisplayFlagsAndEntities, this)( + buildingID, 1); + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::updateAreaBasedOnSurrounding, this)(x, y, size); + } else if (DAT_BuildingsState::instance.buildings[buildingID].buildingType + == OpenSHC::Map::Buildings::BT_SIEGETOWER_PLACED) { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::clearBuildingFootprintAndRemoveSiegeTower, this)( + x, y, size); + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::clearBuildingFootprintAndResetUnits, this)(x, y, size); + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::clearBuildingDisplayFlagsAndEntities, this)( + buildingID, 1); + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::updateAreaBasedOnSurrounding, this)(x, y, size); + } else { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::clearBuildingFootprintAndResetUnits, this)(x, y, size); + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::clearBuildingDisplayFlagsAndEntities, this)( + buildingID, 1); + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::updateAreaBasedOnSurrounding, this)(x, y, size); + } + + if (DAT_BuildingsState::instance.buildings[buildingID].buildingType + != OpenSHC::Map::Buildings::BT_SIEGETOWER_PLACED) { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::updatePathLinkagesForBuilding, this)(buildingID); + } + MACRO_CALL_MEMBER(OpenSHC::Map::Navigation::PathFindingState_Func::updateWalkAndPathLayer, + DAT_PathFindingState::ptr)(size + 6, x, y); + DAT_PathFindingState::instance.toggleUpdateSeparateAreaTileMap = 1; + DAT_TileMapState::instance.field_0x554a30 = 1; + MACRO_CALL_MEMBER(OpenSHC::UI::MinimapViewState_Func::triggerMinimapRedraw, DAT_MinimapViewState::ptr)(1); + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/clearBuildingTilesAndTrees.cpp b/src/OpenSHC/Map/TileMapState/clearBuildingTilesAndTrees.cpp new file mode 100644 index 0000000000..9cfe8f2fc9 --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/clearBuildingTilesAndTrees.cpp @@ -0,0 +1,47 @@ +#include "../TileMapState.func.hpp" + +#include "OpenSHC/Map/LandscapeState.func.hpp" +#include "OpenSHC/Map/Navigation/PathFindingState.func.hpp" +#include "OpenSHC/Map/LogicHelpers/Logic1.hpp" + +#include "OpenSHC/Globals/DAT_BuildingsState.hpp" +#include "OpenSHC/Globals/DAT_LandscapeState.hpp" +#include "OpenSHC/Globals/DAT_PathFindingState.hpp" +#include "OpenSHC/Globals/DAT_ViewportRenderState.hpp" + +namespace OpenSHC { +namespace Map { + + using OpenSHC::Map::LogicHelpers::L_BUILDING; + using OpenSHC::Map::LogicHelpers::L_FARM_FIELD_APPLE; + using OpenSHC::Map::LogicHelpers::L_FARM_FIELD_DAIRY; + using OpenSHC::Map::LogicHelpers::L_FARM_FIELD_HOP; + using OpenSHC::Map::LogicHelpers::L_FARM_FIELD_WHEAT; + + // FUNCTION: STRONGHOLDCRUSADER 0x004FAAB0 + void TileMapState::clearBuildingTilesAndTrees(int buildingID, int surfaceArea) + { + for (int i = 0; i < surfaceArea; i++) { + int tileUnk = DAT_BuildingsState::instance.buildings[buildingID].tileRefs[i]; + if ((this->LogicLayer[tileUnk] & L_FARM_FIELD_APPLE) != 0) { + int treeID = this->OrganismLayer[tileUnk]; + if (treeID != 0 && treeID < countof(DAT_LandscapeState::instance.trees)) { + MACRO_CALL_MEMBER(OpenSHC::Map::LandscapeState_Func::removeTree, DAT_LandscapeState::ptr)(treeID); + } + } + this->LogicLayer[tileUnk] + &= ~(L_BUILDING | L_FARM_FIELD_WHEAT | L_FARM_FIELD_HOP | L_FARM_FIELD_APPLE | L_FARM_FIELD_DAIRY); + this->DamageLayer[tileUnk] = 0; + this->BuildingLayer[tileUnk] = 0; + if (DAT_BuildingsState::instance.buildings[buildingID].noRubble == 0) { + this->BuildingWasLayer[tileUnk] = 0; + } + this->ChangedLayer[tileUnk] = 2; + MACRO_CALL_MEMBER(OpenSHC::Map::Navigation::PathFindingState_Func::updatePathLinkagesInAllEightDirections, + DAT_PathFindingState::ptr)( + DAT_ViewportRenderState::instance.tileTranslationMatrix_YComponent[tileUnk], tileUnk); + } + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/clearDrawBridgeWater.cpp b/src/OpenSHC/Map/TileMapState/clearDrawBridgeWater.cpp new file mode 100644 index 0000000000..3544c9e857 --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/clearDrawBridgeWater.cpp @@ -0,0 +1,36 @@ +#include "../TileMapState.func.hpp" + +#include "OpenSHC/Globals/DAT_ViewportRenderState.hpp" + +namespace OpenSHC { +namespace Map { + + using OpenSHC::Map::LogicHelpers::L_MOAT; + + // FUNCTION: STRONGHOLDCRUSADER 0x00508870 + void TileMapState::clearDrawBridgeWater(int x, int y) + { + int buildingSizeTileIndex = 0; + + do { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::getBuildingSizeIndexMappingData, this)( + buildingSizeTileIndex, 5); + int targetedTile = DAT_ViewportRenderState::instance.translationMatrix[this->buildingY + y].addXgetTile + + this->buildingX + x; + this->LogicLayer[targetedTile] &= ~L_MOAT; + this->HeightLayer[targetedTile] = 8; + uint moatID = MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::returnOwnedMoatAtTile, this)(targetedTile); + if (moatID != 0) { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::clearMoatData, this)(moatID); + this->LogicLayer[targetedTile] &= ~L_MOAT; + this->BuildingWasLayer[targetedTile] = 0; + } + this->BuildingLayer[targetedTile] = 0; + buildingSizeTileIndex++; + this->BuildingWasLayer[targetedTile] = 0; + this->ChangedLayer[targetedTile] = 2; + } while (buildingSizeTileIndex < this->constructionTileCount); + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/clearFixedSizeTwoBuildingFootprint.cpp b/src/OpenSHC/Map/TileMapState/clearFixedSizeTwoBuildingFootprint.cpp new file mode 100644 index 0000000000..59f30b309a --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/clearFixedSizeTwoBuildingFootprint.cpp @@ -0,0 +1,45 @@ +#include "../TileMapState.func.hpp" + +#include "OpenSHC/Map/LogicHelpers/MiscDisplay.hpp" + +#include "OpenSHC/Globals/DAT_BuildingsState.hpp" +#include "OpenSHC/Globals/DAT_ViewportRenderState.hpp" + +namespace OpenSHC { +namespace Map { + + using OpenSHC::Map::LogicHelpers::L_BUILDING; + using OpenSHC::Map::LogicHelpers::L_CRENEL; + using OpenSHC::Map::LogicHelpers::L_CRENEL_VARIATIONUnk; + using OpenSHC::Map::LogicHelpers::L_KEEP_NON_MANOR_HOUSE; + using OpenSHC::Map::LogicHelpers::L_STAIRS; + using OpenSHC::Map::LogicHelpers::L_STOCKPILEUnk; + using OpenSHC::Map::LogicHelpers::L_WALL_OR_GATEHOUSE; + using OpenSHC::Map::LogicHelpers::MD_RUBBLE_INNERUnk; + + // FUNCTION: STRONGHOLDCRUSADER 0x004FAE50 + void TileMapState::clearFixedSizeTwoBuildingFootprint(int x, int y) + { + int buildingSizeTileIndex = 0; + + do { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::getBuildingSizeIndexMappingData, this)( + buildingSizeTileIndex, 2); + int tile = DAT_ViewportRenderState::instance.translationMatrix[this->buildingY + y].addXgetTile + + this->buildingX + x; + this->LogicLayer[tile] &= ~(L_STOCKPILEUnk | L_WALL_OR_GATEHOUSE | L_CRENEL | L_BUILDING | L_STAIRS + | L_CRENEL_VARIATIONUnk | L_KEEP_NON_MANOR_HOUSE); + int buildingID = this->BuildingLayer[tile]; + this->BuildingLayer[tile] = 0; + if (DAT_BuildingsState::instance.buildings[buildingID].noRubble == 0) { + this->BuildingWasLayer[tile] = 0; + } else { + this->MiscDisplayLayer[tile] |= MD_RUBBLE_INNERUnk; + } + buildingSizeTileIndex++; + this->HeightLayer[tile] = this->DefaultHeightLayer[tile]; + } while (buildingSizeTileIndex < this->constructionTileCount); + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/clearInvalidMoatEntries.cpp b/src/OpenSHC/Map/TileMapState/clearInvalidMoatEntries.cpp new file mode 100644 index 0000000000..9387b69c7f --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/clearInvalidMoatEntries.cpp @@ -0,0 +1,30 @@ +#include "../TileMapState.func.hpp" + +#include "OpenSHC/Globals/DAT_TileMapState.hpp" + +namespace OpenSHC { +namespace Map { + + using OpenSHC::Map::LogicHelpers::L_MOAT; + using OpenSHC::Map::LogicHelpers::L_MOAT_DUG_OR_PLANNED; + + // FUNCTION: STRONGHOLDCRUSADER 0x005006C0 + void TileMapState::clearInvalidMoatEntries() + { + for (int moatID = 1; moatID < this->currentMoatCount; moatID++) { + if (this->moats[moatID].owner == 0) { + continue; + } + int tile = this->moats[moatID].tile; + if ((DAT_TileMapState::instance.LogicLayer[tile] & (L_MOAT_DUG_OR_PLANNED | L_MOAT)) != 0) { + continue; + } + if (this->BuildingLayer[tile] != 0) { + continue; + } + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::clearMoatData, this)(moatID); + } + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/clearMoat.cpp b/src/OpenSHC/Map/TileMapState/clearMoat.cpp new file mode 100644 index 0000000000..f873e33228 --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/clearMoat.cpp @@ -0,0 +1,26 @@ +#include "../TileMapState.func.hpp" + +#include "OpenSHC/Globals/DAT_ViewportRenderState.hpp" + +namespace OpenSHC { +namespace Map { + + using OpenSHC::Map::LogicHelpers::L_MOAT_DUG_OR_PLANNED; + + // FUNCTION: STRONGHOLDCRUSADER 0x005005F0 + void TileMapState::clearMoat(int tile) + { + if ((this->LogicLayer[tile] & L_MOAT_DUG_OR_PLANNED) == 0) { + return; + } + + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::clearMoatDataAtTile, this)(tile + - DAT_ViewportRenderState::instance + .translationMatrix[DAT_ViewportRenderState::instance.tileTranslationMatrix_YComponent[tile]] + .addXgetTile, + DAT_ViewportRenderState::instance.tileTranslationMatrix_YComponent[tile]); + this->LogicLayer[tile] &= ~L_MOAT_DUG_OR_PLANNED; + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/clearMoatDataAtTile.cpp b/src/OpenSHC/Map/TileMapState/clearMoatDataAtTile.cpp new file mode 100644 index 0000000000..f06fe16930 --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/clearMoatDataAtTile.cpp @@ -0,0 +1,23 @@ +#include "../TileMapState.func.hpp" + +#include "OpenSHC/IO/LowLevelMemory.func.hpp" + +#include "OpenSHC/Globals/DAT_LowLevelMemory.hpp" + +namespace OpenSHC { +namespace Map { + + // FUNCTION: STRONGHOLDCRUSADER 0x00500310 + void TileMapState::clearMoatDataAtTile(int x, int y) + { + for (int moatID = 0; moatID < this->currentMoatCount; moatID++) { + if (this->moats[moatID].x == x && this->moats[moatID].y == y) { + MACRO_CALL_MEMBER(OpenSHC::IO::LowLevelMemory_Func::fillMemory_ByteValue, DAT_LowLevelMemory::ptr)( + sizeof(Moat), 0, &this->moats[moatID]); + this->moatTileCount -= 1; + } + } + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/collectCliffEdgeTilesForClimbData.cpp b/src/OpenSHC/Map/TileMapState/collectCliffEdgeTilesForClimbData.cpp new file mode 100644 index 0000000000..10cb7ac7cb --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/collectCliffEdgeTilesForClimbData.cpp @@ -0,0 +1,61 @@ +#include "../TileMapState.func.hpp" + +#include "OpenSHC/Globals/DAT_ClimbLogicDefinedData.hpp" +#include "OpenSHC/Globals/DAT_ViewportRenderState.hpp" + +namespace OpenSHC { +namespace Map { + + using OpenSHC::Map::LogicHelpers::L2_OASIS_GRASS; + using OpenSHC::Map::LogicHelpers::L2_PLATEAU_HIGH; + using OpenSHC::Map::LogicHelpers::L2_STONES_OR_DRIVEN_SANDUnk; + using OpenSHC::Map::LogicHelpers::L_BORDER; + using OpenSHC::Map::LogicHelpers::L_BORDER_EDGE; + using OpenSHC::Map::LogicHelpers::L_RIVER; + using OpenSHC::Map::LogicHelpers::L_SEA; + + // FUNCTION: STRONGHOLDCRUSADER 0x004FC340 + void TileMapState::collectCliffEdgeTilesForClimbData() + { + this->temporaryTerrainTypeIndex = 0; + for (int tile = 0; tile < countof(this->LogicLayer); tile++) { + if ((this->LogicLayer[tile] & (L_BORDER | L_BORDER_EDGE)) != 0) { + continue; + } + + if ((this->LogicLayer[tile] & L_RIVER) != 0 + && (this->Logic2Layer[tile] & L2_STONES_OR_DRIVEN_SANDUnk) != 0) { + this->temporaryTerrainTypeBinaryArray[this->temporaryTerrainTypeIndex] = 1; + this->temporaryTerrainTypeArray[this->temporaryTerrainTypeIndex] = tile; + int const row = DAT_ViewportRenderState::instance.tileTranslationMatrix_YComponent[tile]; + int const height = this->HeightLayer[tile]; + for (int index = 0; index < countof(DAT_ClimbLogicDefinedData::instance.neighbourSearchOrder); + index++) { + int neighbour = this->directionTranslationMatrix[row][DAT_ClimbLogicDefinedData::instance + .neighbourSearchOrder[index]] + + tile; + if ((this->LogicLayer[neighbour] & L_RIVER) != 0 && this->HeightLayer[neighbour] > height + 80) { + this->temporaryTerrainTypeBinaryArray[this->temporaryTerrainTypeIndex] = 0; + break; + } + } + this->temporaryTerrainTypeIndex += 1; + if (this->temporaryTerrainTypeIndex >= countof(this->temporaryTerrainTypeArray)) { + return; + } + } + + if (((this->LogicLayer[tile] & L_RIVER) != 0 && (this->Logic2Layer[tile] & L2_OASIS_GRASS) != 0) + || ((this->LogicLayer[tile] & L_SEA) != 0 && (this->Logic2Layer[tile] & L2_PLATEAU_HIGH) != 0)) { + this->temporaryTerrainTypeBinaryArray[this->temporaryTerrainTypeIndex] = 1; + this->temporaryTerrainTypeArray[this->temporaryTerrainTypeIndex] = tile; + this->temporaryTerrainTypeIndex += 1; + if (this->temporaryTerrainTypeIndex >= countof(this->temporaryTerrainTypeArray)) { + return; + } + } + } + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/computeClimbRampRotation.cpp b/src/OpenSHC/Map/TileMapState/computeClimbRampRotation.cpp new file mode 100644 index 0000000000..fa6835c971 --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/computeClimbRampRotation.cpp @@ -0,0 +1,93 @@ +#include "../TileMapState.func.hpp" + +namespace OpenSHC { +namespace Map { + + using OpenSHC::Map::LogicHelpers::L_WALL_OR_GATEHOUSE; + + // FUNCTION: STRONGHOLDCRUSADER 0x004FC810 + int TileMapState::computeClimbRampRotation(int tileUnk, uint x, uint y) + { + int height; + if ((this->LogicLayer[tileUnk] & L_WALL_OR_GATEHOUSE) != 0) { + height = this->DefaultHeightLayer[tileUnk]; + } else { + height = this->HeightLayer[tileUnk]; + } + + int neighbour = this->directionTranslationMatrix[y][this->screenSouthDirection] + tileUnk; + int southHeight; + if ((this->LogicLayer[neighbour] & L_WALL_OR_GATEHOUSE) != 0) { + southHeight = this->DefaultHeightLayer[neighbour]; + } else { + southHeight = this->HeightLayer[neighbour]; + } + + neighbour = this->directionTranslationMatrix[y][this->screenEastDirection] + tileUnk; + int eastHeight; + if ((this->LogicLayer[neighbour] & L_WALL_OR_GATEHOUSE) != 0) { + eastHeight = this->DefaultHeightLayer[neighbour]; + } else { + eastHeight = this->HeightLayer[neighbour]; + } + + neighbour = this->directionTranslationMatrix[y][this->screenSouthEastDirection] + tileUnk; + int southEastHeight; + if ((this->LogicLayer[neighbour] & L_WALL_OR_GATEHOUSE) != 0) { + southEastHeight = this->DefaultHeightLayer[neighbour]; + } else { + southEastHeight = this->HeightLayer[neighbour]; + } + + uint rotation; + if (height > southHeight + 20 && southHeight < 136) { + if (height > eastHeight + 20 && eastHeight < 136) { + if (height > southEastHeight + 20 && southEastHeight < 136) { + rotation = 5; + } else { + rotation = 3; + } + } else { + rotation = 1; + } + } else if (height > eastHeight + 20 && eastHeight < 136) { + if (height > southHeight + 20 && southHeight < 136) { + rotation = 3; + } else { + rotation = 2; + } + } else if (southHeight < 136 || eastHeight < 136) { + if (height > southEastHeight + 20) { + rotation = 5; + } else { + rotation = 0; + } + } else { + rotation = 4; + } + + if (this->mapOrientation == OpenSHC::Map::Enums::MO_NORTH) { + if (rotation == 1) { + this->wallCornerRotation = 32 - (x & 0x1f); + } else { + this->wallCornerRotation = 32 - (y & 0x1f); + } + } else if (this->mapOrientation == OpenSHC::Map::Enums::MO_SOUTH) { + if (rotation == 1) { + this->wallCornerRotation = (x & 0x1f) + 1; + } else { + this->wallCornerRotation = (y & 0x1f) + 1; + } + } else if (this->mapOrientation == OpenSHC::Map::Enums::MO_EAST) { + this->wallCornerRotation = (y & 0x1f) + 1; + } else { + this->wallCornerRotation = 32 - (y & 0x1f); + } + if ((int)this->wallCornerRotation > 31) { + this->wallCornerRotation = 1; + } + return rotation; + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/computeTileAlongAxisOffset.cpp b/src/OpenSHC/Map/TileMapState/computeTileAlongAxisOffset.cpp new file mode 100644 index 0000000000..d237fdbed0 --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/computeTileAlongAxisOffset.cpp @@ -0,0 +1,31 @@ +#include "../TileMapState.func.hpp" + +#include "OpenSHC/Globals/DAT_ViewportRenderState.hpp" + +namespace OpenSHC { +namespace Map { + + using OpenSHC::Map::Enums::CD_NORTH; + using OpenSHC::Map::Enums::CD_SOUTH; + + // FUNCTION: STRONGHOLDCRUSADER 0x00500B50 + int TileMapState::computeTileAlongAxisOffset(int tileUnk, int addend, int offset) + { + int yStep = 1; + int direction = CD_SOUTH; + int y = DAT_ViewportRenderState::instance.tileTranslationMatrix_YComponent[tileUnk]; + if (offset < 0) { + yStep = -1; + direction = CD_NORTH; + } + + int count = abs(offset); + for (int i = 0; i < count; i++) { + tileUnk += this->directionTranslationMatrix[y][direction]; + y += yStep; + } + return tileUnk + addend; + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/computeWallCornerRenderRotation.cpp b/src/OpenSHC/Map/TileMapState/computeWallCornerRenderRotation.cpp new file mode 100644 index 0000000000..af0ce9197c --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/computeWallCornerRenderRotation.cpp @@ -0,0 +1,81 @@ +#include "../TileMapState.func.hpp" + +#include "OpenSHC/Map/Enums/CardinalDirection.hpp" +#include "OpenSHC/WindowsHelper/Enums/BOOLEnum.hpp" + +namespace OpenSHC { +namespace Map { + + // FUNCTION: STRONGHOLDCRUSADER 0x004FC650 + int TileMapState::computeWallCornerRenderRotation(uint x) + { + int tileUnk = this->DAT_SomeTile; + int y = this->DAT_SomeY; + int result = 0; + if (MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::isWallConnectionHeightValid, this)( + tileUnk, y, OpenSHC::Map::Enums::CD_EAST)) { + if (this->mapOrientation == OpenSHC::Map::Enums::MO_NORTH) { + if ((x & 0xf) == 0xf) { + result = 2; + } else { + result = 17 - (x & 0xf); + } + } else if (this->mapOrientation == OpenSHC::Map::Enums::MO_SOUTH) { + if ((x & 0xf) == 0) { + result = 2; + } else { + result = (x & 0xf) + 2; + } + } else if (this->mapOrientation == OpenSHC::Map::Enums::MO_EAST) { + if ((x & 0xf) == 0) { + result = 1; + } else { + result = (x & 0xf) + 17; + } + } else if (this->mapOrientation == OpenSHC::Map::Enums::MO_WEST) { + if ((x & 0xf) == 0xf) { + result = 1; + } else { + result = 32 - (x & 0xf); + } + } + } else if (MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::isWallConnectionHeightValid, this)( + tileUnk, y, OpenSHC::Map::Enums::CD_NORTH)) { + if (this->mapOrientation == OpenSHC::Map::Enums::MO_NORTH) { + if ((y & 0xf) == 0xf) { + result = 1; + } else { + result = 32 - (y & 0xf); + } + } else if (this->mapOrientation == OpenSHC::Map::Enums::MO_SOUTH) { + if ((y & 0xf) == 0) { + result = 1; + } else { + result = (y & 0xf) + 17; + } + } else if (this->mapOrientation == OpenSHC::Map::Enums::MO_WEST) { + if ((y & 0xf) == 0) { + result = 2; + } else { + result = (y & 0xf) + 2; + } + } else if (this->mapOrientation == OpenSHC::Map::Enums::MO_EAST) { + if ((y & 0xf) == 0xf) { + result = 2; + } else { + result = 17 - (y & 0xf); + } + } + } else if (MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::getWallFlagForOrientedDirection, this)( + tileUnk, y, OpenSHC::Map::Enums::CD_EAST)) { + result = 48; + } else if (MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::getWallFlagForOrientedDirection, this)( + tileUnk, y, OpenSHC::Map::Enums::CD_SOUTH)) { + result = 63; + } else { + result = 33; + } + return result; + } +} +} diff --git a/src/OpenSHC/Map/TileMapState/countLogicPropertyInSurroundingTiles.cpp b/src/OpenSHC/Map/TileMapState/countLogicPropertyInSurroundingTiles.cpp new file mode 100644 index 0000000000..101c4c9ce9 --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/countLogicPropertyInSurroundingTiles.cpp @@ -0,0 +1,31 @@ +#include "../TileMapState.func.hpp" + +#include "OpenSHC/Map/Enums/CardinalDirection.hpp" + +namespace OpenSHC { +namespace Map { + + // FUNCTION: STRONGHOLDCRUSADER 0x004F8530 + void TileMapState::countLogicPropertyInSurroundingTiles(int tile, int y, uint logic1) + { + this->DAT_CardinalTilesAroundTile = 0; + if ((this->LogicLayer[this->directionTranslationMatrix[y][OpenSHC::Map::Enums::CD_NORTH] + tile] & logic1) + != 0) { + this->DAT_CardinalTilesAroundTile++; + } + if ((this->LogicLayer[this->directionTranslationMatrix[y][OpenSHC::Map::Enums::CD_EAST] + tile] & logic1) + != 0) { + this->DAT_CardinalTilesAroundTile++; + } + if ((this->LogicLayer[this->directionTranslationMatrix[y][OpenSHC::Map::Enums::CD_SOUTH] + tile] & logic1) + != 0) { + this->DAT_CardinalTilesAroundTile++; + } + if ((this->LogicLayer[this->directionTranslationMatrix[y][OpenSHC::Map::Enums::CD_WEST] + tile] & logic1) + != 0) { + this->DAT_CardinalTilesAroundTile++; + } + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/countMoatsOwnedByEachPlayer.cpp b/src/OpenSHC/Map/TileMapState/countMoatsOwnedByEachPlayer.cpp new file mode 100644 index 0000000000..1cfd18f958 --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/countMoatsOwnedByEachPlayer.cpp @@ -0,0 +1,24 @@ +#include "../TileMapState.func.hpp" + +#include "OpenSHC/Globals/DAT_GameState.hpp" + +namespace OpenSHC { +namespace Map { + + // FUNCTION: STRONGHOLDCRUSADER 0x005000E0 + void TileMapState::countMoatsOwnedByEachPlayer() + { + for (int playerID = 1; playerID < countof(DAT_GameState::instance.playerDataArray); playerID++) { + DAT_GameState::instance.playerDataArray[playerID].moatsOwned = 0; + } + for (int moatID = 1; moatID < countof(this->moats); moatID++) { + char const owner = this->moats[moatID].owner; + if (owner != 0) { + int const ownerIndex = owner; + DAT_GameState::instance.playerDataArray[ownerIndex].moatsOwned++; + } + } + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/countUnfinishedMoatTilesForPlayer.cpp b/src/OpenSHC/Map/TileMapState/countUnfinishedMoatTilesForPlayer.cpp new file mode 100644 index 0000000000..80daf39b88 --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/countUnfinishedMoatTilesForPlayer.cpp @@ -0,0 +1,22 @@ +#include "../TileMapState.func.hpp" + +namespace OpenSHC { +namespace Map { + + using OpenSHC::Map::LogicHelpers::L_MOAT; + + // FUNCTION: STRONGHOLDCRUSADER 0x00500180 + int TileMapState::countUnfinishedMoatTilesForPlayer(int playerID) + { + int unfinishedMoatTiles = 0; + for (int moatID = 1; moatID < countof(this->moats); moatID++) { + if (this->moats[moatID].owner != 0 && this->moats[moatID].owner == playerID + && (this->LogicLayer[this->moats[moatID].tile] & L_MOAT) == 0) { + unfinishedMoatTiles++; + } + } + return unfinishedMoatTiles; + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/countUnownedPitchDitches.cpp b/src/OpenSHC/Map/TileMapState/countUnownedPitchDitches.cpp new file mode 100644 index 0000000000..a9c81b01c5 --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/countUnownedPitchDitches.cpp @@ -0,0 +1,19 @@ +#include "../TileMapState.func.hpp" + +namespace OpenSHC { +namespace Map { + + // FUNCTION: STRONGHOLDCRUSADER 0x00500800 + int TileMapState::countUnownedPitchDitches() + { + int unownedPitchDitches = 0; + for (int pitchDitchID = 0; pitchDitchID < countof(this->pitchDitches); pitchDitchID++) { + if (this->pitchDitches[pitchDitchID].owner == 0) { + unownedPitchDitches++; + } + } + return unownedPitchDitches; + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/createMoatData.cpp b/src/OpenSHC/Map/TileMapState/createMoatData.cpp new file mode 100644 index 0000000000..0155860a04 --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/createMoatData.cpp @@ -0,0 +1,63 @@ +#include "../TileMapState.func.hpp" + +#include "OpenSHC/Map/Buildings/BuildingsState.func.hpp" +#include "OpenSHC/Game/GameMode2.hpp" +#include "OpenSHC/Map/MapConstants.hpp" + +#include "OpenSHC/Globals/DAT_BuildingsState.hpp" +#include "OpenSHC/Globals/DAT_GameCore.hpp" +#include "OpenSHC/Globals/DAT_ViewportRenderState.hpp" + +namespace OpenSHC { +namespace Map { + + // FUNCTION: STRONGHOLDCRUSADER 0x005110B0 + int TileMapState::createMoatData(undefined4 playerID, uint x, uint y, int filled) + { + if (x > MC_MAP_XY_LIMIT_INCLUSIVE || y > MC_MAP_XY_LIMIT_INCLUSIVE) { + return 0; + } + if (DAT_ViewportRenderState::instance.DAT_BinaryTileMap400x400[y * MC_MAP_XY_LIMIT + x] == 0) { + return 0; + } + + int existingMoatID = MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::returnOwnedMoatAtTile, this)( + DAT_ViewportRenderState::instance.translationMatrix[y].addXgetTile + x); + if (existingMoatID != 0) { + return existingMoatID; + } + if (DAT_GameCore::instance.gameMode_2 == OpenSHC::Game::GM_SIEGE_THAT + && MACRO_CALL_MEMBER( + OpenSHC::Map::Buildings::BuildingsState_Func::hasEnoughGoldForMoat, DAT_BuildingsState::ptr)() + == 0) { + return 0; + } + + for (int moatID = 1; moatID < countof(this->moats); moatID++) { + if (this->moats[moatID].owner != 0) { + continue; + } + + if (moatID >= this->currentMoatCount) { + this->currentMoatCount = moatID + 1; + } + this->moats[moatID].owner = playerID; + this->moats[moatID].x = x; + this->moats[moatID].y = y; + this->moats[moatID].tile = DAT_ViewportRenderState::instance.translationMatrix[y].addXgetTile + x; + this->moats[moatID].zeroOrTwo = 2; + if (filled != 0) { + this->moats[moatID].stage = 2; + this->moats[moatID].fillProgress = 4; + } else { + this->moats[moatID].stage = 1; + this->moats[moatID].fillProgress = 0; + } + this->moatTileCount += 1; + return moatID; + } + return 0; + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/destroyPitchDitch.cpp b/src/OpenSHC/Map/TileMapState/destroyPitchDitch.cpp new file mode 100644 index 0000000000..9d83313741 --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/destroyPitchDitch.cpp @@ -0,0 +1,36 @@ +#include "../TileMapState.func.hpp" + +#include "OpenSHC/IO/LowLevelMemory.func.hpp" +#include "OpenSHC/Map/Navigation/PathFindingState.func.hpp" +#include "OpenSHC/Map/LogicHelpers/MiscDisplay.hpp" + +#include "OpenSHC/Globals/DAT_LowLevelMemory.hpp" +#include "OpenSHC/Globals/DAT_PathFindingState.hpp" + +namespace OpenSHC { +namespace Map { + + using OpenSHC::Map::LogicHelpers::L_PLAIN2_AND_PITCH; + using OpenSHC::Map::LogicHelpers::L_WALL_OR_GATEHOUSE; + using OpenSHC::Map::LogicHelpers::MD_RUBBLE_EDGEUnk; + using OpenSHC::Map::LogicHelpers::MD_RUBBLE_INNERUnk; + + // FUNCTION: STRONGHOLDCRUSADER 0x00500890 + void TileMapState::destroyPitchDitch(int pitchDitchID) + { + this->LogicLayer[this->pitchDitches[pitchDitchID].tile] &= ~L_PLAIN2_AND_PITCH; + this->MiscDisplayLayer[this->pitchDitches[pitchDitchID].tile] &= ~MD_RUBBLE_EDGEUnk; + this->MiscDisplayLayer[this->pitchDitches[pitchDitchID].tile] &= ~MD_RUBBLE_INNERUnk; + if ((this->LogicLayer[this->pitchDitches[pitchDitchID].tile] & L_WALL_OR_GATEHOUSE) == 0 + && this->BuildingLayer[this->pitchDitches[pitchDitchID].tile] == 0) { + this->HeightLayer[this->pitchDitches[pitchDitchID].tile] + = this->DefaultHeightLayer[this->pitchDitches[pitchDitchID].tile]; + } + MACRO_CALL_MEMBER(OpenSHC::Map::Navigation::PathFindingState_Func::updateWalkAndPathLayer, + DAT_PathFindingState::ptr)(4, this->pitchDitches[pitchDitchID].x, this->pitchDitches[pitchDitchID].y); + MACRO_CALL_MEMBER(OpenSHC::IO::LowLevelMemory_Func::fillMemory_ByteValue, DAT_LowLevelMemory::ptr)( + sizeof(PitchDitch), 0, &this->pitchDitches[pitchDitchID]); + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/destroyWallsOfPlayer.cpp b/src/OpenSHC/Map/TileMapState/destroyWallsOfPlayer.cpp new file mode 100644 index 0000000000..ad113f22d6 --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/destroyWallsOfPlayer.cpp @@ -0,0 +1,51 @@ +#include "../TileMapState.func.hpp" + +#include "OpenSHC/Map/Entities/EntityState.func.hpp" +#include "OpenSHC/Map/Navigation/PathFindingState.func.hpp" +#include "OpenSHC/Map/LogicHelpers/WallOwner.hpp" + +#include "OpenSHC/Globals/DAT_EntityState.hpp" +#include "OpenSHC/Globals/DAT_PathFindingState.hpp" +#include "OpenSHC/Globals/DAT_TileMapState.hpp" +#include "OpenSHC/Globals/DAT_ViewportRenderState.hpp" + +namespace OpenSHC { +namespace Map { + + using OpenSHC::Map::LogicHelpers::L_CRENEL; + using OpenSHC::Map::LogicHelpers::L_CRENEL_VARIATIONUnk; + using OpenSHC::Map::LogicHelpers::L_STAIRS; + using OpenSHC::Map::LogicHelpers::L_UNKNOWN_WALL_RELATED; + using OpenSHC::Map::LogicHelpers::L_WALL_OR_GATEHOUSE; + using OpenSHC::Map::LogicHelpers::WO_OWNER_MASK; + + // FUNCTION: STRONGHOLDCRUSADER 0x004F93E0 + void TileMapState::destroyWallsOfPlayer(int playerID) + { + for (int tile = 0; tile < countof(this->LogicLayer); tile++) { + if ((this->LogicLayer[tile] & L_WALL_OR_GATEHOUSE) == 0) { + continue; + } + if ((this->WallOwnerLayer[tile] & WO_OWNER_MASK) + 1 != playerID) { + continue; + } + + MACRO_CALL_MEMBER(OpenSHC::Map::Entities::EntityState_Func::destroyEntitiesOnTile, DAT_EntityState::ptr)( + tile); + this->LogicLayer[tile] + &= ~(L_WALL_OR_GATEHOUSE | L_CRENEL | L_STAIRS | L_UNKNOWN_WALL_RELATED | L_CRENEL_VARIATIONUnk); + this->HeightLayer[tile] = this->DefaultHeightLayer[tile]; + this->DamageLayer[tile] = 0; + int y = DAT_ViewportRenderState::instance.tileTranslationMatrix_YComponent[tile]; + DAT_PathFindingState::instance.toggleUpdateSeparateAreaTileMap = 1; + DAT_TileMapState::instance.field_0x554a30 = 1; + int x = tile - DAT_ViewportRenderState::instance.translationMatrix[y].addXgetTile; + MACRO_CALL_MEMBER(OpenSHC::Map::Navigation::PathFindingState_Func::updatePathLinkagesInAllEightDirections, + DAT_PathFindingState::ptr)(y, tile); + MACRO_CALL_MEMBER(OpenSHC::Map::Navigation::PathFindingState_Func::updateWalkAndPathLayer, + DAT_PathFindingState::ptr)(7, x, y); + } + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/findNearestValidDigTileNearTarget.cpp b/src/OpenSHC/Map/TileMapState/findNearestValidDigTileNearTarget.cpp new file mode 100644 index 0000000000..e3fef9ccb4 --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/findNearestValidDigTileNearTarget.cpp @@ -0,0 +1,77 @@ +#include "../TileMapState.func.hpp" + +#include "OpenSHC/Map/Buildings/BuildingsState.func.hpp" +#include "OpenSHC/Map/Navigation/DirectionAlgorithmState.func.hpp" +#include "OpenSHC/Map/MapConstants.hpp" + +#include "OpenSHC/Globals/DAT_BuildingsState.hpp" +#include "OpenSHC/Globals/DAT_DirectionAlgorithmState.hpp" +#include "OpenSHC/Globals/DAT_TerrainDefinedData.hpp" +#include "OpenSHC/Globals/DAT_ViewportRenderState.hpp" + +namespace OpenSHC { +namespace Map { + + // FUNCTION: STRONGHOLDCRUSADER 0x00500980 + BOOLEnum TileMapState::findNearestValidDigTileNearTarget(int x1, int y1, int x2, int y2) + { + if (x1 < 0 || x1 > MC_MAP_XY_LIMIT_INCLUSIVE || y1 < 0 || y1 > MC_MAP_XY_LIMIT_INCLUSIVE) { + return FALSE; + } + if (DAT_ViewportRenderState::instance.DAT_BinaryTileMap400x400[y1 * MC_MAP_XY_LIMIT + x1] == 0) { + return FALSE; + } + if (x2 < 0 || x2 > MC_MAP_XY_LIMIT_INCLUSIVE || y2 < 0 || y2 > MC_MAP_XY_LIMIT_INCLUSIVE + || DAT_ViewportRenderState::instance.DAT_BinaryTileMap400x400[y2 * MC_MAP_XY_LIMIT + x2] == 0) { + return FALSE; + } + + int area = this->PathConnectionLayer[DAT_ViewportRenderState::instance.translationMatrix[y1].addXgetTile + x1]; + int targetTile = DAT_ViewportRenderState::instance.translationMatrix[y2].addXgetTile + x2; + int bestDistance = 10000; + int bestIndex = -1; + int targetHeight = this->HeightLayer[targetTile]; + int targetBuildingID = this->BuildingLayer[targetTile]; + if (targetBuildingID != 0) { + targetHeight + += MACRO_CALL_MEMBER(OpenSHC::Map::Buildings::BuildingsState_Func::getBuildingHeightForBuildingID, + DAT_BuildingsState::ptr)(targetBuildingID); + } + + for (int index = 0; index < countof(DAT_TerrainDefinedData::instance.twoTileAxisOffsets); index++) { + int fromXPosition = DAT_TerrainDefinedData::instance.twoTileAxisOffsets[index].x + x2; + int fromYPosition = DAT_TerrainDefinedData::instance.twoTileAxisOffsets[index].y + y2; + int tile = DAT_ViewportRenderState::instance.translationMatrix[fromYPosition].addXgetTile + fromXPosition; + if (this->PathConnectionLayer[tile] != area) { + continue; + } + int height = this->HeightLayer[tile]; + int buildingID = this->BuildingLayer[tile]; + if (buildingID != 0) { + height + += MACRO_CALL_MEMBER(OpenSHC::Map::Buildings::BuildingsState_Func::getBuildingHeightForBuildingID, + DAT_BuildingsState::ptr)(buildingID); + } + if (height >= targetHeight + 16 || height <= targetHeight - 16) { + continue; + } + MACRO_CALL_MEMBER(OpenSHC::Map::Navigation::DirectionAlgorithmState_Func::setAxisBasedDistanceResult, + DAT_DirectionAlgorithmState::ptr)(x1, y1, fromXPosition, fromYPosition); + if (DAT_DirectionAlgorithmState::instance.distanceHigh < bestDistance) { + bestDistance = DAT_DirectionAlgorithmState::instance.distanceHigh; + bestIndex = index; + } + } + + if (bestIndex <= -1) { + this->destinationNearTargetX = x2; + this->destinationNearTargetY = y2; + return FALSE; + } + this->destinationNearTargetX = DAT_TerrainDefinedData::instance.twoTileAxisOffsets[bestIndex].x + x2; + this->destinationNearTargetY = DAT_TerrainDefinedData::instance.twoTileAxisOffsets[bestIndex].y + y2; + return TRUE; + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/findTileInSameAreaAndNoTooHeightDifference.cpp b/src/OpenSHC/Map/TileMapState/findTileInSameAreaAndNoTooHeightDifference.cpp new file mode 100644 index 0000000000..fb2a258513 --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/findTileInSameAreaAndNoTooHeightDifference.cpp @@ -0,0 +1,25 @@ +#include "../TileMapState.func.hpp" + +#include "OpenSHC/WindowsHelper/Enums/BOOLEnum.hpp" + +namespace OpenSHC { +namespace Map { + + // FUNCTION: STRONGHOLDCRUSADER 0x00500370 + BOOLEnum TileMapState::findTileInSameAreaAndNoTooHeightDifference(int area, int tile, int yUnk) + { + for (int attempts = 0; attempts < countof(this->directionTranslationMatrix[yUnk]); attempts++) { + int neighbour = this->directionTranslationMatrix[yUnk][attempts] + tile; + if (this->HeightLayer[neighbour] > this->HeightLayer[tile] + 16) { + continue; + } + if (this->PathConnectionLayer[neighbour] != area) { + continue; + } + return TRUE; + } + return FALSE; + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/forceFullTileMapRedraw.cpp b/src/OpenSHC/Map/TileMapState/forceFullTileMapRedraw.cpp index 50d530182b..01611f2753 100644 --- a/src/OpenSHC/Map/TileMapState/forceFullTileMapRedraw.cpp +++ b/src/OpenSHC/Map/TileMapState/forceFullTileMapRedraw.cpp @@ -6,9 +6,9 @@ namespace Map { // FUNCTION: STRONGHOLDCRUSADER 0x005017A0 void TileMapState::forceFullTileMapRedraw() { - this->forceUpdateLogicalAndMiscDisplayLayers = 1; - this->forceUpdateTextureTilemap = 1; - this->forceUpdateGFXLayers = 1; + this->forceUpdateLogicalAndMiscDisplayLayers = TRUE; + this->forceUpdateTextureTilemap = TRUE; + this->forceUpdateGFXLayers = TRUE; MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::setChangedLayerToThreeAndMapping0x40x40, this)(); } diff --git a/src/OpenSHC/Map/TileMapState/getBuildingHurtSFXID.cpp b/src/OpenSHC/Map/TileMapState/getBuildingHurtSFXID.cpp new file mode 100644 index 0000000000..74f893a873 --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/getBuildingHurtSFXID.cpp @@ -0,0 +1,64 @@ +#include "../TileMapState.func.hpp" + +#include "OpenSHC/Map/Buildings/BuildingLogicalState.hpp" + +#include "OpenSHC/Globals/DAT_BuildingsState.hpp" + +namespace OpenSHC { +namespace Map { + + using OpenSHC::Map::Enums::HSM_NONE; + using OpenSHC::Map::Enums::HSM_STONE; + using OpenSHC::Map::Enums::HSM_WOOD; + using OpenSHC::Map::LogicHelpers::L_WALL_OR_GATEHOUSE; + + // FUNCTION: STRONGHOLDCRUSADER 0x004F8210 + OpenSHC::Map::Enums::HitSoundMaterialInt TileMapState::getBuildingHurtSFXID(int tile) + { + short buildingID = this->BuildingLayer[tile]; + if (buildingID != 0) { + if (DAT_BuildingsState::instance.buildings[buildingID].logicalState == 0) { + return HSM_NONE; + } + if (DAT_BuildingsState::instance.buildings[buildingID].logicalState + == OpenSHC::Map::Buildings::BLS_REMOVE) { + return HSM_NONE; + } + if (DAT_BuildingsState::instance.buildings[buildingID].currentHealth == 0) { + return HSM_NONE; + } + + switch (DAT_BuildingsState::instance.buildings[buildingID].buildingType) { + case OpenSHC::Map::Buildings::BT_BARRACKS: + case OpenSHC::Map::Buildings::BT_STOCKPILE: + case OpenSHC::Map::Buildings::BT_ARMORY: + case OpenSHC::Map::Buildings::BT_ENGINEERSGUILD: + case OpenSHC::Map::Buildings::BT_TUNNELERSGUILD: + case OpenSHC::Map::Buildings::BT_CHAPEL: + case OpenSHC::Map::Buildings::BT_CHURCH: + case OpenSHC::Map::Buildings::BT_CATHEDRAL: + case OpenSHC::Map::Buildings::BT_MANORHOUSE: + case OpenSHC::Map::Buildings::BT_STONEKEEP: + case OpenSHC::Map::Buildings::BT_STRONGHOLD: + case OpenSHC::Map::Buildings::BT_GATEHOUSELARGE: + case OpenSHC::Map::Buildings::BT_GATEHOUSESMALL: + case OpenSHC::Map::Buildings::BT_GATEHOUSE: + case OpenSHC::Map::Buildings::BT_TOWER1: + case OpenSHC::Map::Buildings::BT_TOWER2: + case OpenSHC::Map::Buildings::BT_TOWER3: + case OpenSHC::Map::Buildings::BT_TOWER4: + case OpenSHC::Map::Buildings::BT_TOWER5: + case OpenSHC::Map::Buildings::BT_OUTPOST_EUROPEAN: + return HSM_STONE; + default: + return HSM_WOOD; + } + } + if ((this->LogicLayer[tile] & L_WALL_OR_GATEHOUSE) != 0) { + return HSM_STONE; + } + return HSM_NONE; + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/getBuildingSizeForCommandBuildingType.cpp b/src/OpenSHC/Map/TileMapState/getBuildingSizeForCommandBuildingType.cpp new file mode 100644 index 0000000000..1f3e1f74a7 --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/getBuildingSizeForCommandBuildingType.cpp @@ -0,0 +1,212 @@ +#include "../TileMapState.func.hpp" + +#include "OpenSHC/Commands/MappersEnum.hpp" + +#include "OpenSHC/Globals/DAT_TileMapState.hpp" + +namespace OpenSHC { +namespace Map { + + // FUNCTION: STRONGHOLDCRUSADER 0x004FA550 + int TileMapState::getBuildingSizeForCommandBuildingType(MappersEnum commandBuildingType) + { + switch (commandBuildingType) { + case OpenSHC::Commands::M_MAPPER_FLETCHER: + case OpenSHC::Commands::M_MAPPER_HOVEL: + case OpenSHC::Commands::M_MAPPER_BAKER: + case OpenSHC::Commands::M_MAPPER_BREWER: + case OpenSHC::Commands::M_MAPPER_GRANARY: + case OpenSHC::Commands::M_MAPPER_ARMOURY: + case OpenSHC::Commands::M_MAPPER_POLETURNER: + case OpenSHC::Commands::M_MAPPER_BLACKSMITH: + case OpenSHC::Commands::M_MAPPER_ARMOURER: + case OpenSHC::Commands::M_MAPPER_TANNER: + case OpenSHC::Commands::M_MAPPER_IRON_MINE: + case OpenSHC::Commands::M_MAPPER_PITCH_WORKINGS: + case OpenSHC::Commands::M_MAPPER_TOWER2: + case OpenSHC::Commands::M_MAPPER_GARDEN10: + case OpenSHC::Commands::M_MAPPER_GARDEN11: + case OpenSHC::Commands::M_MAPPER_GARDEN12: + case OpenSHC::Commands::M_MAPPER_OIL_SMELTER: + case OpenSHC::Commands::M_MAPPER_UNKNOWN_258: + case OpenSHC::Commands::M_MAPPER_UNKNOWN_259: + case OpenSHC::Commands::M_MAPPER_UNKNOWN_260: + case OpenSHC::Commands::M_MAPPER_WATERPOT: + return 4; + case OpenSHC::Commands::M_MAPPER_STORES: + case OpenSHC::Commands::M_MAPPER_TRADEPOST: + case OpenSHC::Commands::M_MAPPER_BARRACKS_EURO: + case OpenSHC::Commands::M_MAPPER_BARRACKS_ARAB: + case OpenSHC::Commands::M_MAPPER_ENGINEERS_GUILD: + case OpenSHC::Commands::M_MAPPER_TUNNELERS_GUILD: + case OpenSHC::Commands::M_MAPPER_INN: + case OpenSHC::Commands::M_MAPPER_GATE_INNER: + case OpenSHC::Commands::M_MAPPER_DRAWBRIDGE: + case OpenSHC::Commands::M_MAPPER_TOWER3: + case OpenSHC::Commands::M_MAPPER_GATE_STONE1A: + case OpenSHC::Commands::M_MAPPER_GATE_STONE1B: + case OpenSHC::Commands::M_MAPPER_OUTPOST_EURO: + case OpenSHC::Commands::M_MAPPER_OUTPOST_ARAB: + case OpenSHC::Commands::M_MAPPER_CESS_PIT1: + case OpenSHC::Commands::M_MAPPER_CESS_PIT2: + case OpenSHC::Commands::M_MAPPER_CESS_PIT3: + case OpenSHC::Commands::M_MAPPER_CESS_PIT4: + case OpenSHC::Commands::M_MAPPER_DUNGEON: + case OpenSHC::Commands::M_MAPPER_DUNKING_STOOL: + case OpenSHC::Commands::M_MAPPER_DANCING_BEAR: + case OpenSHC::Commands::M_MAPPER_POND1: + case OpenSHC::Commands::M_MAPPER_POND2_SMALL: + return 5; + case OpenSHC::Commands::M_MAPPER_QUARRY: + case OpenSHC::Commands::M_MAPPER_STABLES: + case OpenSHC::Commands::M_MAPPER_HEALER: + case OpenSHC::Commands::M_MAPPER_CHURCH1: + case OpenSHC::Commands::M_MAPPER_TOWER4: + case OpenSHC::Commands::M_MAPPER_TOWER5: + case OpenSHC::Commands::M_MAPPER_POND3_LARGE1: + case OpenSHC::Commands::M_MAPPER_POND4_LARGE2: + return 6; + case OpenSHC::Commands::M_MAPPER_WOODSMAN: + case OpenSHC::Commands::M_MAPPER_TUNNEL: + case OpenSHC::Commands::M_MAPPER_TUNNEL_CONSTRUCTION: + case OpenSHC::Commands::M_MAPPER_MILL: + case OpenSHC::Commands::M_MAPPER_HUNTER: + case OpenSHC::Commands::M_MAPPER_TOWER1: + case OpenSHC::Commands::M_MAPPER_GATE_WOOD1A: + case OpenSHC::Commands::M_MAPPER_GATE_WOOD1B: + case OpenSHC::Commands::M_MAPPER_GATE_WOOD1C: + case OpenSHC::Commands::M_MAPPER_GATE_WOOD1D: + case OpenSHC::Commands::M_MAPPER_GARDEN7: + case OpenSHC::Commands::M_MAPPER_GARDEN8: + case OpenSHC::Commands::M_MAPPER_GARDEN9: + case OpenSHC::Commands::M_MAPPER_MAYPOLE: + case OpenSHC::Commands::M_MAPPER_STOCKS: + case OpenSHC::Commands::M_MAPPER_CATAPULT: + case OpenSHC::Commands::M_MAPPER_TREBUCHET: + case OpenSHC::Commands::M_MAPPER_SIEGE_TOWER: + case OpenSHC::Commands::M_MAPPER_BATTERING_RAM: + case OpenSHC::Commands::M_MAPPER_PORTABLE_SHIELD: + case OpenSHC::Commands::M_MAPPER_BURNING_STAKE: + case OpenSHC::Commands::M_MAPPER_RACK_STRETCHING: + case OpenSHC::Commands::M_MAPPER_CHOPPING_BLOCK: + case OpenSHC::Commands::M_MAPPER_DOG_CAGE: + case OpenSHC::Commands::M_MAPPER_WELL: + case OpenSHC::Commands::M_MAPPER_ARAB_BALLISTA: + return 3; + case OpenSHC::Commands::M_MAPPER_KEEP1: + case OpenSHC::Commands::M_MAPPER_KEEP2: + case OpenSHC::Commands::M_MAPPER_GATE_MAIN: + case OpenSHC::Commands::M_MAPPER_GATE_STONE2A: + case OpenSHC::Commands::M_MAPPER_GATE_STONE2B: + return 7; + case OpenSHC::Commands::M_MAPPER_CAMP_FIRE: + case OpenSHC::Commands::M_MAPPER_KILLING_PIT: + case OpenSHC::Commands::M_MAPPER_PITCH_DITCH: + case OpenSHC::Commands::M_MAPPER_UNKNOWN_248: + case OpenSHC::Commands::M_MAPPER_UNKNOWN_249: + case OpenSHC::Commands::M_MAPPER_UNKNOWN_250: + case OpenSHC::Commands::M_MAPPER_UNKNOWN_251: + case OpenSHC::Commands::M_MAPPER_SHRINE5: + return 1; + case OpenSHC::Commands::M_MAPPER_KEEP3: + case OpenSHC::Commands::M_MAPPER_APPLEFARM: + return 11; + case OpenSHC::Commands::M_MAPPER_WHEATFARM: + case OpenSHC::Commands::M_MAPPER_HOPSFARM: + case OpenSHC::Commands::M_MAPPER_CHURCH2: + return 9; + case OpenSHC::Commands::M_MAPPER_CHURCH3: + return 13; + case OpenSHC::Commands::M_MAPPER_CATTLEFARM: + return 10; + case OpenSHC::Commands::M_MAPPER_UNDUGMOAT: + case OpenSHC::Commands::M_MAPPER_DUGMOAT: + case OpenSHC::Commands::M_MAPPER_MOAT: + case OpenSHC::Commands::M_MAPPER_ANTIMOAT: + DAT_TileMapState::instance.field_0x5549b8 = 1; + return 2; + case OpenSHC::Commands::M_MAPPER_OXENBASE: + case OpenSHC::Commands::M_MAPPER_SIGNPOST: + case OpenSHC::Commands::M_MAPPER_GATE_POSTERN: + case OpenSHC::Commands::M_MAPPER_GARDEN1: + case OpenSHC::Commands::M_MAPPER_GARDEN2: + case OpenSHC::Commands::M_MAPPER_GARDEN3: + case OpenSHC::Commands::M_MAPPER_GARDEN4: + case OpenSHC::Commands::M_MAPPER_GARDEN5: + case OpenSHC::Commands::M_MAPPER_GARDEN6: + case OpenSHC::Commands::M_MAPPER_GALLOWS: + case OpenSHC::Commands::M_MAPPER_UNKNOWN_252: + case OpenSHC::Commands::M_MAPPER_UNKNOWN_253: + case OpenSHC::Commands::M_MAPPER_UNKNOWN_254: + case OpenSHC::Commands::M_MAPPER_UNKNOWN_255: + case OpenSHC::Commands::M_MAPPER_UNKNOWN_256: + case OpenSHC::Commands::M_MAPPER_UNKNOWN_257: + case OpenSHC::Commands::M_MAPPER_GIBBET: + case OpenSHC::Commands::M_MAPPER_STATUE1: + case OpenSHC::Commands::M_MAPPER_STATUE2: + case OpenSHC::Commands::M_MAPPER_STATUE3: + case OpenSHC::Commands::M_MAPPER_STATUE4: + case OpenSHC::Commands::M_MAPPER_STATUE5: + case OpenSHC::Commands::M_MAPPER_SHRINE1: + case OpenSHC::Commands::M_MAPPER_SHRINE2: + case OpenSHC::Commands::M_MAPPER_SHRINE3: + case OpenSHC::Commands::M_MAPPER_SHRINE4: + return 2; + case OpenSHC::Commands::M_MAPPER_FLAG_TYPE0: + case OpenSHC::Commands::M_MAPPER_FLAG_TYPE1: + case OpenSHC::Commands::M_MAPPER_FLAG_TYPE2: + case OpenSHC::Commands::M_MAPPER_FLAG_TYPE3: + case OpenSHC::Commands::M_MAPPER_HEADS: + case OpenSHC::Commands::M_MAPPER_BRAZIER: + case OpenSHC::Commands::M_MAPPER_MANGONEL: + case OpenSHC::Commands::M_MAPPER_BALLISTA: + case OpenSHC::Commands::M_MAPPER_PEOPLE_ARCHERS: + case OpenSHC::Commands::M_MAPPER_PEOPLE_SPEARMEN: + case OpenSHC::Commands::M_MAPPER_PEOPLE_PIKEMEN: + case OpenSHC::Commands::M_MAPPER_PEOPLE_MACEMEN: + case OpenSHC::Commands::M_MAPPER_PEOPLE_XBOWMEN: + case OpenSHC::Commands::M_MAPPER_PEOPLE_SWORDSMEN: + case OpenSHC::Commands::M_MAPPER_PEOPLE_KNIGHTS: + case OpenSHC::Commands::M_MAPPER_PEOPLE_LADDERMEN: + case OpenSHC::Commands::M_MAPPER_PEOPLE_ENGINEERS: + case OpenSHC::Commands::M_MAPPER_PEOPLE_ENGINEERS_POTS: + case OpenSHC::Commands::M_MAPPER_PEOPLE_MONKS: + case OpenSHC::Commands::M_MAPPER_PEOPLE_CATAPULTS: + case OpenSHC::Commands::M_MAPPER_PEOPLE_TREBUCHETS: + case OpenSHC::Commands::M_MAPPER_PEOPLE_BATTERING_RAMS: + case OpenSHC::Commands::M_MAPPER_PEOPLE_SIEGE_TOWERS: + case OpenSHC::Commands::M_MAPPER_PEOPLE_PORTABLE_SHIELDS: + case OpenSHC::Commands::M_MAPPER_PEOPLE_TUNNELERS: + case OpenSHC::Commands::M_MAPPER_PLACE_ASSEMBLY_POINT1: + case OpenSHC::Commands::M_MAPPER_PLACE_ASSEMBLY_POINT2: + case OpenSHC::Commands::M_MAPPER_PLACE_ASSEMBLY_POINT3: + case OpenSHC::Commands::M_MAPPER_PLACE_ASSEMBLY_POINT4: + case OpenSHC::Commands::M_MAPPER_PLACE_ASSEMBLY_POINT5: + case OpenSHC::Commands::M_MAPPER_PLACE_ASSEMBLY_POINT6: + case OpenSHC::Commands::M_MAPPER_PLACE_ASSEMBLY_POINT7: + case OpenSHC::Commands::M_MAPPER_PEOPLE_ARAB_BOW: + case OpenSHC::Commands::M_MAPPER_PEOPLE_ARAB_SLAVE: + case OpenSHC::Commands::M_MAPPER_PEOPLE_ARAB_SLINGER: + case OpenSHC::Commands::M_MAPPER_PEOPLE_ARAB_ASSASIN: + case OpenSHC::Commands::M_MAPPER_PEOPLE_ARAB_HORSEMAN: + case OpenSHC::Commands::M_MAPPER_PEOPLE_ARAB_SWORDSMAN: + case OpenSHC::Commands::M_MAPPER_PEOPLE_ARAB_GRENADIER: + case OpenSHC::Commands::M_MAPPER_PEOPLE_ARAB_BALLISTA: + case OpenSHC::Commands::M_MAPPER_PLACE_ASSEMBLY_POINTM1: + case OpenSHC::Commands::M_MAPPER_PLACE_ASSEMBLY_POINTM2: + case OpenSHC::Commands::M_MAPPER_PLACE_ASSEMBLY_POINTM3: + case OpenSHC::Commands::M_MAPPER_PLACE_ASSEMBLY_POINTM4: + case OpenSHC::Commands::M_MAPPER_PLACE_ASSEMBLY_POINTM5: + case OpenSHC::Commands::M_MAPPER_PLACE_ASSEMBLY_POINTM6: + case OpenSHC::Commands::M_MAPPER_PLACE_ASSEMBLY_POINTM7: + case OpenSHC::Commands::M_MAPPER_PLACE_ASSEMBLY_POINTE1: + case OpenSHC::Commands::M_MAPPER_PLACE_ASSEMBLY_POINTE2: + case OpenSHC::Commands::M_MAPPER_PLACE_ASSEMBLY_POINTT1: + case OpenSHC::Commands::M_MAPPER_PLACE_ASSEMBLY_POINTK1: + return 0; + } + return -1; + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/getBuildingSizeIndexMappingData.cpp b/src/OpenSHC/Map/TileMapState/getBuildingSizeIndexMappingData.cpp new file mode 100644 index 0000000000..f5256b9b9b --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/getBuildingSizeIndexMappingData.cpp @@ -0,0 +1,38 @@ +#include "../TileMapState.func.hpp" + +#include "OpenSHC/Globals/DAT_BuildingSizeIndexMapping.hpp" + +namespace OpenSHC { +namespace Map { + + // FUNCTION: STRONGHOLDCRUSADER 0x004F9880 + void TileMapState::getBuildingSizeIndexMappingData(int buildingSizeTileIndex, int buildingWidthOrHeight) + { + this->constructionTileCount = buildingWidthOrHeight * buildingWidthOrHeight; + this->buildingX = DAT_BuildingSizeIndexMapping::instance[buildingWidthOrHeight][buildingSizeTileIndex].x; + this->buildingY = DAT_BuildingSizeIndexMapping::instance[buildingWidthOrHeight][buildingSizeTileIndex].y; + switch (this->mapOrientation) { + case OpenSHC::Map::Enums::MO_NORTH: + this->buildingOrientedPartIndex + = DAT_BuildingSizeIndexMapping::instance[buildingWidthOrHeight][buildingSizeTileIndex] + .orientedPartIndex[0]; + return; + case OpenSHC::Map::Enums::MO_EAST: + this->buildingOrientedPartIndex + = DAT_BuildingSizeIndexMapping::instance[buildingWidthOrHeight][buildingSizeTileIndex] + .orientedPartIndex[1]; + return; + case OpenSHC::Map::Enums::MO_SOUTH: + this->buildingOrientedPartIndex + = DAT_BuildingSizeIndexMapping::instance[buildingWidthOrHeight][buildingSizeTileIndex] + .orientedPartIndex[2]; + return; + case OpenSHC::Map::Enums::MO_WEST: + this->buildingOrientedPartIndex + = DAT_BuildingSizeIndexMapping::instance[buildingWidthOrHeight][buildingSizeTileIndex] + .orientedPartIndex[3]; + } + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/getCastleBuildRangeForMapSize.cpp b/src/OpenSHC/Map/TileMapState/getCastleBuildRangeForMapSize.cpp new file mode 100644 index 0000000000..db73420a98 --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/getCastleBuildRangeForMapSize.cpp @@ -0,0 +1,25 @@ +#include "../TileMapState.func.hpp" + +#include "OpenSHC/Map/MapConstants.hpp" + +namespace OpenSHC { +namespace Map { + + // FUNCTION: STRONGHOLDCRUSADER 0x004FBC30 + int TileMapState::getCastleBuildRangeForMapSize() + { + switch (this->mapSize) { + case MC_MAP_SIZE_160: + return 45; + case MC_MAP_SIZE_200: + return 50; + case MC_MAP_SIZE_300: + return 60; + case MC_MAP_SIZE_400: + return 70; + } + return 70; + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/getMaxWallHeightInBrushArea.cpp b/src/OpenSHC/Map/TileMapState/getMaxWallHeightInBrushArea.cpp new file mode 100644 index 0000000000..3cbd94fdd0 --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/getMaxWallHeightInBrushArea.cpp @@ -0,0 +1,36 @@ +#include "../TileMapState.func.hpp" + +namespace OpenSHC { +namespace Map { + + using OpenSHC::Map::LogicHelpers::L_CRENEL; + using OpenSHC::Map::LogicHelpers::L_STOCKPILEUnk; + using OpenSHC::Map::LogicHelpers::L_WALL_OR_GATEHOUSE; + + // FUNCTION: STRONGHOLDCRUSADER 0x00502950 + int TileMapState::getMaxWallHeightInBrushArea(int tile, int y) + { + int baseTile = tile; + int baseY = y; + int maxWallHeight = 0; + for (int index = 1; index < 5; index++) { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::getTileForBrush, this)( + 1, index, &tile, &y, baseTile, baseY); + if ((this->LogicLayer[tile] & L_WALL_OR_GATEHOUSE) == 0) { + continue; + } + if ((this->LogicLayer[tile] & L_STOCKPILEUnk) != 0) { + continue; + } + if ((this->LogicLayer[tile] & L_CRENEL) != 0) { + continue; + } + if (this->HeightLayer[tile] > maxWallHeight) { + maxWallHeight = this->HeightLayer[tile]; + } + } + return maxWallHeight; + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/getNonFarmFieldBuildingHealthAtTileOr1000.cpp b/src/OpenSHC/Map/TileMapState/getNonFarmFieldBuildingHealthAtTileOr1000.cpp new file mode 100644 index 0000000000..73f072874a --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/getNonFarmFieldBuildingHealthAtTileOr1000.cpp @@ -0,0 +1,46 @@ +#include "../TileMapState.func.hpp" + +#include "OpenSHC/Map/Buildings/BuildingLogicalState.hpp" + +#include "OpenSHC/Globals/DAT_BuildingDefinedData.hpp" +#include "OpenSHC/Globals/DAT_BuildingsState.hpp" + +namespace OpenSHC { +namespace Map { + + using OpenSHC::Map::LogicHelpers::L_FARM_FIELD_APPLE; + using OpenSHC::Map::LogicHelpers::L_FARM_FIELD_DAIRY; + using OpenSHC::Map::LogicHelpers::L_FARM_FIELD_HOP; + using OpenSHC::Map::LogicHelpers::L_FARM_FIELD_WHEAT; + + // FUNCTION: STRONGHOLDCRUSADER 0x004F8160 + int TileMapState::getNonFarmFieldBuildingHealthAtTileOr1000(int tile) + { + short buildingID = this->BuildingLayer[tile]; + if (buildingID <= 0) { + return 1000; + } + if (DAT_BuildingsState::instance.buildings[buildingID].logicalState == 0) { + return 1000; + } + if (DAT_BuildingsState::instance.buildings[buildingID].logicalState == OpenSHC::Map::Buildings::BLS_REMOVE) { + return 1000; + } + if (DAT_BuildingsState::instance.buildings[buildingID].currentHealth == 0) { + return 1000; + } + if (DAT_BuildingDefinedData::instance + .BuildingTypeHasHealth[DAT_BuildingsState::instance.buildings[buildingID].buildingType] + == 0) { + return 1000; + } + + if ((this->LogicLayer[tile] & (L_FARM_FIELD_WHEAT | L_FARM_FIELD_HOP | L_FARM_FIELD_APPLE | L_FARM_FIELD_DAIRY)) + != 0) { + return 1000; + } + return DAT_BuildingsState::instance.buildings[buildingID].currentHealth; + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/getOrientationThatIsWallTowerOrGatehouse.cpp b/src/OpenSHC/Map/TileMapState/getOrientationThatIsWallTowerOrGatehouse.cpp new file mode 100644 index 0000000000..e4170f3bf3 --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/getOrientationThatIsWallTowerOrGatehouse.cpp @@ -0,0 +1,38 @@ +#include "../TileMapState.func.hpp" + +#include "OpenSHC/Map/MapConstants.hpp" + +#include "OpenSHC/Globals/DAT_TerrainDefinedData.hpp" +#include "OpenSHC/Globals/DAT_ViewportRenderState.hpp" + +namespace OpenSHC { +namespace Map { + + using OpenSHC::Map::LogicHelpers::L_CRENEL_VARIATIONUnk; + using OpenSHC::Map::LogicHelpers::L_STOCKPILEUnk; + using OpenSHC::Map::LogicHelpers::L_WALL_OR_GATEHOUSE; + + // FUNCTION: STRONGHOLDCRUSADER 0x004F8B50 + CardinalDirection TileMapState::getOrientationThatIsWallTowerOrGatehouse(uint x, uint y) + { + if (x > MC_MAP_XY_LIMIT_INCLUSIVE || y > MC_MAP_XY_LIMIT_INCLUSIVE) { + return OpenSHC::Map::Enums::CD_UNSPECIFIED; + } + if (DAT_ViewportRenderState::instance.DAT_BinaryTileMap400x400[y * MC_MAP_XY_LIMIT + x] == 0) { + return OpenSHC::Map::Enums::CD_UNSPECIFIED; + } + + int rowTile = DAT_ViewportRenderState::instance.translationMatrix[y].addXgetTile + x; + for (int index = 0; index < countof(DAT_TerrainDefinedData::instance.SomeOrientationOrderArray); index++) { + CardinalDirection const orientation = DAT_TerrainDefinedData::instance.SomeOrientationOrderArray[index]; + int neighbour = this->directionTranslationMatrix[y][orientation] + rowTile; + if ((this->LogicLayer[neighbour] & L_WALL_OR_GATEHOUSE) != 0 + && (this->LogicLayer[neighbour] & (L_STOCKPILEUnk | L_CRENEL_VARIATIONUnk)) == 0) { + return orientation; + } + } + return OpenSHC::Map::Enums::CD_UNSPECIFIED; + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/getPitchDitchIDForTile.cpp b/src/OpenSHC/Map/TileMapState/getPitchDitchIDForTile.cpp new file mode 100644 index 0000000000..e2966b9158 --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/getPitchDitchIDForTile.cpp @@ -0,0 +1,18 @@ +#include "../TileMapState.func.hpp" + +namespace OpenSHC { +namespace Map { + + // FUNCTION: STRONGHOLDCRUSADER 0x00500850 + int TileMapState::getPitchDitchIDForTile(int tile) + { + for (int pitchDitchID = 1; pitchDitchID < this->maxPitchDitchCount; pitchDitchID++) { + if (this->pitchDitches[pitchDitchID].owner != 0 && tile == this->pitchDitches[pitchDitchID].tile) { + return pitchDitchID; + } + } + return 0; + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/getRubbleGraphicStageForDamageLevel.cpp b/src/OpenSHC/Map/TileMapState/getRubbleGraphicStageForDamageLevel.cpp new file mode 100644 index 0000000000..1790a0c806 --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/getRubbleGraphicStageForDamageLevel.cpp @@ -0,0 +1,81 @@ +#include "../TileMapState.func.hpp" + +namespace OpenSHC { +namespace Map { + + using OpenSHC::Map::Enums::CD_NORTHEAST; + using OpenSHC::Map::Enums::CD_NORTHWEST; + using OpenSHC::Map::Enums::CD_SOUTHEAST; + using OpenSHC::Map::Enums::CD_SOUTHWEST; + using OpenSHC::Map::Enums::MO_EAST; + using OpenSHC::Map::Enums::MO_NORTH; + using OpenSHC::Map::Enums::MO_SOUTH; + using OpenSHC::Map::Enums::MO_WEST; + + // FUNCTION: STRONGHOLDCRUSADER 0x004FA460 + int TileMapState::getRubbleGraphicStageForDamageLevel(int fencePiece) + { + int result = 0; + int orientation = this->mapOrientation; + if (orientation == MO_NORTH) { + if (fencePiece == 80) { + result = 1; + } else if (fencePiece == 81) { + result = 0; + } else if (fencePiece == CD_NORTHEAST) { + result = 2; + } else if (fencePiece == CD_NORTHWEST) { + result = 5; + } else if (fencePiece == CD_SOUTHEAST) { + result = 3; + } else if (fencePiece == CD_SOUTHWEST) { + result = 4; + } + } else if (orientation == MO_SOUTH) { + if (fencePiece == 80) { + result = 1; + } else if (fencePiece == 81) { + result = 0; + } else if (fencePiece == CD_NORTHEAST) { + result = 4; + } else if (fencePiece == CD_NORTHWEST) { + result = 3; + } else if (fencePiece == CD_SOUTHEAST) { + result = 5; + } else if (fencePiece == CD_SOUTHWEST) { + result = 2; + } + } else if (orientation == MO_EAST) { + if (fencePiece == 80) { + result = 0; + } else if (fencePiece == 81) { + result = 1; + } else if (fencePiece == CD_NORTHEAST) { + result = 5; + } else if (fencePiece == CD_NORTHWEST) { + result = 4; + } else if (fencePiece == CD_SOUTHEAST) { + result = 2; + } else if (fencePiece == CD_SOUTHWEST) { + result = 3; + } + } else if (orientation == MO_WEST) { + if (fencePiece == 80) { + result = 0; + } else if (fencePiece == 81) { + result = 1; + } else if (fencePiece == CD_NORTHEAST) { + result = 3; + } else if (fencePiece == CD_NORTHWEST) { + result = 2; + } else if (fencePiece == CD_SOUTHEAST) { + result = 4; + } else if (fencePiece == CD_SOUTHWEST) { + result = 5; + } + } + return result; + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/getTileForBrush.cpp b/src/OpenSHC/Map/TileMapState/getTileForBrush.cpp new file mode 100644 index 0000000000..8dbd3f0a7d --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/getTileForBrush.cpp @@ -0,0 +1,56 @@ +#include "../TileMapState.func.hpp" + +#include "OpenSHC/Map/Navigation/PathFindingState.func.hpp" +#include "OpenSHC/Map/Enums/CardinalDirection.hpp" + +#include "OpenSHC/Globals/DAT_PathFindingState.hpp" +#include "OpenSHC/Globals/DAT_TerrainDefinedData.hpp" +#include "OpenSHC/Globals/DAT_ViewportRenderState.hpp" + +namespace OpenSHC { +namespace Map { + + // FUNCTION: STRONGHOLDCRUSADER 0x004FBD80 + void TileMapState::getTileForBrush(int square, int index, int* tilePointer, int* yPointer, int baseTile, int y) + { + if (square != 0) { + MACRO_CALL_MEMBER( + OpenSHC::Map::Navigation::PathFindingState_Func::getTileInSquareBrush, DAT_PathFindingState::ptr)( + index, baseTile - DAT_ViewportRenderState::instance.translationMatrix[y].addXgetTile, y); + *tilePointer = DAT_PathFindingState::instance.resultTile; + *yPointer = DAT_PathFindingState::instance.resultY; + return; + } + + if (index >= countof(DAT_TerrainDefinedData::instance.field_0xc)) { + index = 0; + } + int brushStep = DAT_TerrainDefinedData::instance.field_0xc[index]; + if (brushStep == OpenSHC::Map::Enums::CD_UNSPECIFIED) { + return; + } + if (brushStep < OpenSHC::Map::Enums::CD_UPPER_LIMIT) { + *tilePointer += this->directionTranslationMatrix[*yPointer][brushStep]; + *yPointer += DAT_TerrainDefinedData::instance.clockwiseCardinalTranslationMatrix[brushStep].int_.yOffset; + return; + } + + if (brushStep < 28) { + brushStep -= 20; + for (int i = 0; i < 2; i++) { + *tilePointer += this->directionTranslationMatrix[*yPointer][brushStep]; + *yPointer + += DAT_TerrainDefinedData::instance.clockwiseCardinalTranslationMatrix[brushStep].int_.yOffset; + } + } else if (brushStep < 48) { + brushStep -= 40; + for (int i = 0; i < 3; i++) { + *tilePointer += this->directionTranslationMatrix[*yPointer][brushStep]; + *yPointer + += DAT_TerrainDefinedData::instance.clockwiseCardinalTranslationMatrix[brushStep].int_.yOffset; + } + } + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/getTotalHeightAtTile.cpp b/src/OpenSHC/Map/TileMapState/getTotalHeightAtTile.cpp new file mode 100644 index 0000000000..07da7bc445 --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/getTotalHeightAtTile.cpp @@ -0,0 +1,36 @@ +#include "../TileMapState.func.hpp" + +#include "OpenSHC/Map/Buildings/BuildingsState.func.hpp" +#include "OpenSHC/Map/Buildings/BuildingType.hpp" + +#include "OpenSHC/Globals/DAT_BuildingsState.hpp" + +namespace OpenSHC { +namespace Map { + + // FUNCTION: STRONGHOLDCRUSADER 0x004FFB10 + int TileMapState::getTotalHeightAtTile(int tile) + { + int buildingID = this->BuildingLayer[tile]; + if (buildingID != 0) { + if (DAT_BuildingsState::instance.buildings[buildingID].buildingType + == OpenSHC::Map::Buildings::BT_GATEHOUSELARGE) { + return MACRO_CALL_MEMBER(OpenSHC::Map::Buildings::BuildingsState_Func::getBuildingHeightForBuildingID, + DAT_BuildingsState::ptr)(buildingID) + + this->DefaultHeightLayer[tile]; + } + if (DAT_BuildingsState::instance.buildings[buildingID].buildingType + == OpenSHC::Map::Buildings::BT_GATEHOUSESMALL) { + return MACRO_CALL_MEMBER(OpenSHC::Map::Buildings::BuildingsState_Func::getBuildingHeightForBuildingID, + DAT_BuildingsState::ptr)(buildingID) + + this->DefaultHeightLayer[tile]; + } + return MACRO_CALL_MEMBER(OpenSHC::Map::Buildings::BuildingsState_Func::getBuildingHeightForBuildingID, + DAT_BuildingsState::ptr)(buildingID) + + this->HeightLayer[tile]; + } + return this->HeightLayer[tile]; + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/getWallFlagForOrientedDirection.cpp b/src/OpenSHC/Map/TileMapState/getWallFlagForOrientedDirection.cpp new file mode 100644 index 0000000000..8aeeef0108 --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/getWallFlagForOrientedDirection.cpp @@ -0,0 +1,45 @@ +#include "../TileMapState.func.hpp" + +#include "OpenSHC/Map/Enums/CardinalDirection.hpp" +#include "OpenSHC/WindowsHelper/Enums/BOOLEnum.hpp" + +namespace OpenSHC { +namespace Map { + + using OpenSHC::Map::LogicHelpers::L_WALL_OR_GATEHOUSE; + + // FUNCTION: STRONGHOLDCRUSADER 0x004F89C0 + BOOLEnum TileMapState::getWallFlagForOrientedDirection(int tileUnk, int y, int screenDirection) + { + int direction = OpenSHC::Map::Enums::CD_NORTH; + if (screenDirection == OpenSHC::Map::Enums::CD_EAST) { + if (this->mapOrientation == OpenSHC::Map::Enums::MO_NORTH) { + direction = OpenSHC::Map::Enums::CD_EAST; + } else if (this->mapOrientation == OpenSHC::Map::Enums::MO_EAST) { + direction = OpenSHC::Map::Enums::CD_SOUTH; + } else if (this->mapOrientation == OpenSHC::Map::Enums::MO_SOUTH) { + direction = OpenSHC::Map::Enums::CD_WEST; + } else if (this->mapOrientation == OpenSHC::Map::Enums::MO_WEST) { + direction = OpenSHC::Map::Enums::CD_NORTH; + } + } else if (screenDirection == OpenSHC::Map::Enums::CD_SOUTH) { + if (this->mapOrientation == OpenSHC::Map::Enums::MO_NORTH) { + direction = OpenSHC::Map::Enums::CD_SOUTH; + } else if (this->mapOrientation == OpenSHC::Map::Enums::MO_EAST) { + direction = OpenSHC::Map::Enums::CD_WEST; + } else if (this->mapOrientation == OpenSHC::Map::Enums::MO_SOUTH) { + direction = OpenSHC::Map::Enums::CD_NORTH; + } else if (this->mapOrientation == OpenSHC::Map::Enums::MO_WEST) { + direction = OpenSHC::Map::Enums::CD_EAST; + } + } else { + return FALSE; + } + if ((this->LogicLayer[this->directionTranslationMatrix[y][direction] + tileUnk] & L_WALL_OR_GATEHOUSE) != 0) { + return TRUE; + } + return FALSE; + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/hasHigherNeighborWithStairs.cpp b/src/OpenSHC/Map/TileMapState/hasHigherNeighborWithStairs.cpp new file mode 100644 index 0000000000..3f774191fb --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/hasHigherNeighborWithStairs.cpp @@ -0,0 +1,39 @@ +#include "../TileMapState.func.hpp" + +#include "OpenSHC/Map/Enums/CardinalDirection.hpp" +#include "OpenSHC/WindowsHelper/Enums/BOOLEnum.hpp" + +namespace OpenSHC { +namespace Map { + + using OpenSHC::Map::LogicHelpers::L_STAIRS; + + // FUNCTION: STRONGHOLDCRUSADER 0x004F8A40 + BOOLEnum TileMapState::hasHigherNeighborWithStairs(int tileUnk, int y, int direction) + { + int rotatedDirection = OpenSHC::Map::Enums::CD_NORTH; + if (this->mapOrientation == OpenSHC::Map::Enums::MO_NORTH) { + rotatedDirection = direction; + } else if (this->mapOrientation == OpenSHC::Map::Enums::MO_SOUTH) { + rotatedDirection = direction + OpenSHC::Map::Enums::MO_SOUTH; + } else if (this->mapOrientation == OpenSHC::Map::Enums::MO_EAST) { + rotatedDirection = direction + OpenSHC::Map::Enums::MO_EAST; + } else if (this->mapOrientation == OpenSHC::Map::Enums::MO_WEST) { + rotatedDirection = direction + OpenSHC::Map::Enums::MO_WEST; + } + if (rotatedDirection >= OpenSHC::Map::Enums::CD_UPPER_LIMIT) { + rotatedDirection -= OpenSHC::Map::Enums::CD_UPPER_LIMIT; + } + + int neighbour = this->directionTranslationMatrix[y][rotatedDirection] + tileUnk; + if (this->HeightLayer[neighbour] <= this->HeightLayer[tileUnk]) { + return FALSE; + } + if ((this->LogicLayer[neighbour] & L_STAIRS) != 0) { + return TRUE; + } + return FALSE; + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/hasHigherPlainNeighborWithWallOrGatehouse.cpp b/src/OpenSHC/Map/TileMapState/hasHigherPlainNeighborWithWallOrGatehouse.cpp new file mode 100644 index 0000000000..0695b08278 --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/hasHigherPlainNeighborWithWallOrGatehouse.cpp @@ -0,0 +1,47 @@ +#include "../TileMapState.func.hpp" + +#include "OpenSHC/Map/Enums/CardinalDirection.hpp" +#include "OpenSHC/WindowsHelper/Enums/BOOLEnum.hpp" + +namespace OpenSHC { +namespace Map { + + using OpenSHC::Map::LogicHelpers::L_CRENEL; + using OpenSHC::Map::LogicHelpers::L_STOCKPILEUnk; + using OpenSHC::Map::LogicHelpers::L_WALL_OR_GATEHOUSE; + + // FUNCTION: STRONGHOLDCRUSADER 0x004F8AC0 + BOOLEnum TileMapState::hasHigherPlainNeighborWithWallOrGatehouse(int tile, int y, int direction) + { + int rotatedDirection = OpenSHC::Map::Enums::CD_NORTH; + if (this->mapOrientation == OpenSHC::Map::Enums::MO_NORTH) { + rotatedDirection = direction; + } else if (this->mapOrientation == OpenSHC::Map::Enums::MO_SOUTH) { + rotatedDirection = direction + OpenSHC::Map::Enums::MO_SOUTH; + } else if (this->mapOrientation == OpenSHC::Map::Enums::MO_EAST) { + rotatedDirection = direction + OpenSHC::Map::Enums::MO_EAST; + } else if (this->mapOrientation == OpenSHC::Map::Enums::MO_WEST) { + rotatedDirection = direction + OpenSHC::Map::Enums::MO_WEST; + } + if (rotatedDirection >= OpenSHC::Map::Enums::CD_UPPER_LIMIT) { + rotatedDirection -= OpenSHC::Map::Enums::CD_UPPER_LIMIT; + } + + int neighbour = this->directionTranslationMatrix[y][rotatedDirection] + tile; + if (this->HeightLayer[neighbour] <= this->HeightLayer[tile]) { + return FALSE; + } + if ((this->LogicLayer[neighbour] & L_CRENEL) != 0) { + return FALSE; + } + if ((this->LogicLayer[neighbour] & L_STOCKPILEUnk) != 0) { + return FALSE; + } + if ((this->LogicLayer[neighbour] & L_WALL_OR_GATEHOUSE) != 0) { + return TRUE; + } + return FALSE; + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/hasOnlyTowerNeighborsNoWalls.cpp b/src/OpenSHC/Map/TileMapState/hasOnlyTowerNeighborsNoWalls.cpp new file mode 100644 index 0000000000..fdc777d85c --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/hasOnlyTowerNeighborsNoWalls.cpp @@ -0,0 +1,48 @@ +#include "../TileMapState.func.hpp" + +#include "OpenSHC/Map/Enums/CardinalDirection.hpp" +#include "OpenSHC/WindowsHelper/Enums/BOOLEnum.hpp" + +namespace OpenSHC { +namespace Map { + + using OpenSHC::Map::LogicHelpers::L_UNKNOWN_WALL_RELATED; + using OpenSHC::Map::LogicHelpers::L_WALL_OR_GATEHOUSE; + + // FUNCTION: STRONGHOLDCRUSADER 0x004F8900 + BOOLEnum TileMapState::hasOnlyTowerNeighborsNoWalls(int tile, int y) + { + int unknownWallRelated = 0; + int wallOrGatehouse = 0; + int offset = this->directionTranslationMatrix[y][OpenSHC::Map::Enums::CD_NORTH] + tile; + if ((this->LogicLayer[offset] & L_UNKNOWN_WALL_RELATED) != 0) { + unknownWallRelated++; + } else if ((this->LogicLayer[offset] & L_WALL_OR_GATEHOUSE) != 0) { + wallOrGatehouse++; + } + offset = this->directionTranslationMatrix[y][OpenSHC::Map::Enums::CD_EAST] + tile; + if ((this->LogicLayer[offset] & L_UNKNOWN_WALL_RELATED) != 0) { + unknownWallRelated++; + } else if ((this->LogicLayer[offset] & L_WALL_OR_GATEHOUSE) != 0) { + wallOrGatehouse++; + } + offset = this->directionTranslationMatrix[y][OpenSHC::Map::Enums::CD_SOUTH] + tile; + if ((this->LogicLayer[offset] & L_UNKNOWN_WALL_RELATED) != 0) { + unknownWallRelated++; + } else if ((this->LogicLayer[offset] & L_WALL_OR_GATEHOUSE) != 0) { + wallOrGatehouse++; + } + offset = this->directionTranslationMatrix[y][OpenSHC::Map::Enums::CD_WEST] + tile; + if ((this->LogicLayer[offset] & L_UNKNOWN_WALL_RELATED) != 0) { + unknownWallRelated++; + } else if ((this->LogicLayer[offset] & L_WALL_OR_GATEHOUSE) != 0) { + wallOrGatehouse++; + } + if (unknownWallRelated != 0 && wallOrGatehouse == 0) { + return TRUE; + } + return FALSE; + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/increaseHeightForTunnelSingleTile.cpp b/src/OpenSHC/Map/TileMapState/increaseHeightForTunnelSingleTile.cpp new file mode 100644 index 0000000000..ab69728a85 --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/increaseHeightForTunnelSingleTile.cpp @@ -0,0 +1,48 @@ +#include "../TileMapState.func.hpp" + +#include "OpenSHC/Map/Navigation/PathFindingState.func.hpp" + +#include "OpenSHC/Globals/DAT_PathFindingState.hpp" + +namespace OpenSHC { +namespace Map { + + using OpenSHC::Map::LogicHelpers::L_BORDER; + using OpenSHC::Map::LogicHelpers::L_BORDER_EDGE; + using OpenSHC::Map::LogicHelpers::L_BUILDING; + using OpenSHC::Map::LogicHelpers::L_FORD; + using OpenSHC::Map::LogicHelpers::L_KEEP_NON_MANOR_HOUSE; + using OpenSHC::Map::LogicHelpers::L_MARSH; + using OpenSHC::Map::LogicHelpers::L_MOAT; + using OpenSHC::Map::LogicHelpers::L_RIVER; + using OpenSHC::Map::LogicHelpers::L_ROCKY; + using OpenSHC::Map::LogicHelpers::L_SEA; + using OpenSHC::Map::LogicHelpers::L_WALL_OR_GATEHOUSE; + + // FUNCTION: STRONGHOLDCRUSADER 0x004F80E0 + void TileMapState::increaseHeightForTunnelSingleTile(int tile, int x, int y, int increment) + { + if ((this->LogicLayer[tile] + & (L_WALL_OR_GATEHOUSE | L_BUILDING | L_RIVER | L_FORD | L_KEEP_NON_MANOR_HOUSE | L_MARSH | L_MOAT)) + != 0) { + return; + } + if ((this->LogicLayer[tile] & (L_SEA | L_BORDER | L_BORDER_EDGE | L_ROCKY)) != 0) { + return; + } + if (this->BuildingLayer[tile] != 0) { + return; + } + + if ((this->HeightLayer[tile] < 16 || this->DefaultHeightLayer[tile] < 16) + && this->HeightLayer[tile] + increment >= 0) { + this->HeightLayer[tile] += increment; + } + MACRO_CALL_MEMBER(OpenSHC::Map::Navigation::PathFindingState_Func::updateWalkAndPathLayer, + DAT_PathFindingState::ptr)(4, x, y); + MACRO_CALL_MEMBER(OpenSHC::Map::Navigation::PathFindingState_Func::updatePathLinkagesInAllEightDirections, + DAT_PathFindingState::ptr)(y, tile); + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/increaseHeightForTunnelWithBrush.cpp b/src/OpenSHC/Map/TileMapState/increaseHeightForTunnelWithBrush.cpp new file mode 100644 index 0000000000..2d47e44cb4 --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/increaseHeightForTunnelWithBrush.cpp @@ -0,0 +1,56 @@ +#include "../TileMapState.func.hpp" + +#include "OpenSHC/Map/Navigation/PathFindingState.func.hpp" + +#include "OpenSHC/Globals/DAT_PathFindingState.hpp" + +namespace OpenSHC { +namespace Map { + + using OpenSHC::Map::LogicHelpers::L_BORDER; + using OpenSHC::Map::LogicHelpers::L_BORDER_EDGE; + using OpenSHC::Map::LogicHelpers::L_BUILDING; + using OpenSHC::Map::LogicHelpers::L_FORD; + using OpenSHC::Map::LogicHelpers::L_KEEP_NON_MANOR_HOUSE; + using OpenSHC::Map::LogicHelpers::L_MARSH; + using OpenSHC::Map::LogicHelpers::L_MOAT; + using OpenSHC::Map::LogicHelpers::L_RIVER; + using OpenSHC::Map::LogicHelpers::L_ROCKY; + using OpenSHC::Map::LogicHelpers::L_SEA; + using OpenSHC::Map::LogicHelpers::L_WALL_OR_GATEHOUSE; + + // FUNCTION: STRONGHOLDCRUSADER 0x00501CD0 + void TileMapState::increaseHeightForTunnelWithBrush(int tile, int x, int y, int increment) + { + int baseTile = tile; + int baseY = y; + for (int index = 0; index < 9; index++) { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::getTileForBrush, this)( + 0, index, &tile, &y, baseTile, baseY); + int const brushTile = tile; + if ((this->LogicLayer[tile] & (L_WALL_OR_GATEHOUSE | L_BUILDING | L_KEEP_NON_MANOR_HOUSE)) != 0) { + continue; + } + if (this->BuildingLayer[tile] != 0) { + continue; + } + if ((this->LogicLayer[tile] & (L_SEA | L_BORDER | L_BORDER_EDGE | L_ROCKY)) != 0) { + continue; + } + if ((this->LogicLayer[tile] & (L_RIVER | L_FORD | L_MARSH | L_MOAT)) != 0) { + continue; + } + + if ((this->HeightLayer[tile] < 16 || this->DefaultHeightLayer[tile] < 16) + && this->HeightLayer[tile] + increment >= 0) { + this->HeightLayer[tile] += increment; + } + MACRO_CALL_MEMBER(OpenSHC::Map::Navigation::PathFindingState_Func::updatePathLinkagesInAllEightDirections, + DAT_PathFindingState::ptr)(y, brushTile); + } + MACRO_CALL_MEMBER(OpenSHC::Map::Navigation::PathFindingState_Func::updateWalkAndPathLayer, + DAT_PathFindingState::ptr)(5, x, y); + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/isCliffDropInDirection.cpp b/src/OpenSHC/Map/TileMapState/isCliffDropInDirection.cpp new file mode 100644 index 0000000000..362b530099 --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/isCliffDropInDirection.cpp @@ -0,0 +1,17 @@ +#include "../TileMapState.func.hpp" + +#include "OpenSHC/Map/Enums/CardinalDirection.hpp" + +namespace OpenSHC { +namespace Map { + + // FUNCTION: STRONGHOLDCRUSADER 0x004FC7C0 + BOOLEnum TileMapState::isCliffDropInDirection(int tileUnk, undefined4 unused, int y) + { + int direction = (this->screenSouthEastDirection + 4) % OpenSHC::Map::Enums::CD_UPPER_LIMIT; + return this->HeightLayer[tileUnk] + > this->HeightLayer[this->directionTranslationMatrix[y][direction] + tileUnk] + 20; + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/isFootprintIndexOnEdge.cpp b/src/OpenSHC/Map/TileMapState/isFootprintIndexOnEdge.cpp new file mode 100644 index 0000000000..132e9010cd --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/isFootprintIndexOnEdge.cpp @@ -0,0 +1,27 @@ +#include "../TileMapState.func.hpp" + +#include "OpenSHC/WindowsHelper/Enums/BOOLEnum.hpp" + +namespace OpenSHC { +namespace Map { + + // FUNCTION: STRONGHOLDCRUSADER 0x004F9930 + BOOLEnum TileMapState::isFootprintIndexOnEdge(int counter, int size) + { + if (size < 3) { + return TRUE; + } + if (counter < size) { + return TRUE; + } + if (counter >= (size - 1) * size) { + return TRUE; + } + if (counter % size == 0) { + return TRUE; + } + return counter % size == size - 1; + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/isTileEnclosedByWalls.cpp b/src/OpenSHC/Map/TileMapState/isTileEnclosedByWalls.cpp new file mode 100644 index 0000000000..c89e67f7a3 --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/isTileEnclosedByWalls.cpp @@ -0,0 +1,34 @@ +#include "../TileMapState.func.hpp" + +#include "OpenSHC/Map/Enums/CardinalDirection.hpp" + +namespace OpenSHC { +namespace Map { + + using OpenSHC::Map::LogicHelpers::L_STOCKPILEUnk; + using OpenSHC::Map::LogicHelpers::L_WALL_OR_GATEHOUSE; + + // FUNCTION: STRONGHOLDCRUSADER 0x004F8640 + BOOLEnum TileMapState::isTileEnclosedByWalls(int tile, int y) + { + int offset = this->directionTranslationMatrix[y][OpenSHC::Map::Enums::CD_NORTH] + tile; + if ((this->LogicLayer[offset] & L_WALL_OR_GATEHOUSE) != 0 && (this->LogicLayer[offset] & L_STOCKPILEUnk) == 0) { + return TRUE; + } + offset = this->directionTranslationMatrix[y][OpenSHC::Map::Enums::CD_SOUTH] + tile; + if ((this->LogicLayer[offset] & L_WALL_OR_GATEHOUSE) != 0 && (this->LogicLayer[offset] & L_STOCKPILEUnk) == 0) { + return TRUE; + } + offset = this->directionTranslationMatrix[y][OpenSHC::Map::Enums::CD_EAST] + tile; + if ((this->LogicLayer[offset] & L_WALL_OR_GATEHOUSE) != 0 && (this->LogicLayer[offset] & L_STOCKPILEUnk) == 0) { + return TRUE; + } + offset = this->directionTranslationMatrix[y][OpenSHC::Map::Enums::CD_WEST] + tile; + if ((this->LogicLayer[offset] & L_WALL_OR_GATEHOUSE) != 0 && (this->LogicLayer[offset] & L_STOCKPILEUnk) == 0) { + return TRUE; + } + return FALSE; + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/isTileEnclosedByWallsOrGates.cpp b/src/OpenSHC/Map/TileMapState/isTileEnclosedByWallsOrGates.cpp new file mode 100644 index 0000000000..328d6c80ee --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/isTileEnclosedByWallsOrGates.cpp @@ -0,0 +1,40 @@ +#include "../TileMapState.func.hpp" + +#include "OpenSHC/Map/Enums/CardinalDirection.hpp" +#include "OpenSHC/WindowsHelper/Enums/BOOLEnum.hpp" + +namespace OpenSHC { +namespace Map { + + using OpenSHC::Map::LogicHelpers::L_CRENEL; + using OpenSHC::Map::LogicHelpers::L_STOCKPILEUnk; + using OpenSHC::Map::LogicHelpers::L_WALL_OR_GATEHOUSE; + + // FUNCTION: STRONGHOLDCRUSADER 0x004F85B0 + BOOLEnum TileMapState::isTileEnclosedByWallsOrGates(int tile, int y) + { + int offset = this->directionTranslationMatrix[y][OpenSHC::Map::Enums::CD_NORTH] + tile; + if ((this->LogicLayer[offset] & L_WALL_OR_GATEHOUSE) != 0 + && (this->LogicLayer[offset] & (L_STOCKPILEUnk | L_CRENEL)) == 0) { + return TRUE; + } + offset = this->directionTranslationMatrix[y][OpenSHC::Map::Enums::CD_SOUTH] + tile; + if ((this->LogicLayer[offset] & L_WALL_OR_GATEHOUSE) != 0 + && (this->LogicLayer[offset] & (L_STOCKPILEUnk | L_CRENEL)) == 0) { + return TRUE; + } + offset = this->directionTranslationMatrix[y][OpenSHC::Map::Enums::CD_EAST] + tile; + if ((this->LogicLayer[offset] & L_WALL_OR_GATEHOUSE) != 0 + && (this->LogicLayer[offset] & (L_STOCKPILEUnk | L_CRENEL)) == 0) { + return TRUE; + } + offset = this->directionTranslationMatrix[y][OpenSHC::Map::Enums::CD_WEST] + tile; + if ((this->LogicLayer[offset] & L_WALL_OR_GATEHOUSE) != 0 + && (this->LogicLayer[offset] & (L_STOCKPILEUnk | L_CRENEL)) == 0) { + return TRUE; + } + return FALSE; + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/isWallConnectionHeightValid.cpp b/src/OpenSHC/Map/TileMapState/isWallConnectionHeightValid.cpp new file mode 100644 index 0000000000..ddb60fc378 --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/isWallConnectionHeightValid.cpp @@ -0,0 +1,57 @@ +#include "../TileMapState.func.hpp" + +#include "OpenSHC/Map/Enums/CardinalDirection.hpp" +#include "OpenSHC/WindowsHelper/Enums/BOOLEnum.hpp" + +namespace OpenSHC { +namespace Map { + + using OpenSHC::Map::LogicHelpers::L_STOCKPILEUnk; + using OpenSHC::Map::LogicHelpers::L_WALL_OR_GATEHOUSE; + + // FUNCTION: STRONGHOLDCRUSADER 0x004F8840 + BOOLEnum TileMapState::isWallConnectionHeightValid(int tile, int y, int direction) + { + int tileHeight = this->HeightLayer[tile]; + int firstDirection = OpenSHC::Map::Enums::CD_NORTH; + int secondDirection = OpenSHC::Map::Enums::CD_NORTH; + if (direction == OpenSHC::Map::Enums::CD_NORTH) { + secondDirection = OpenSHC::Map::Enums::CD_SOUTH; + } else if (direction == OpenSHC::Map::Enums::CD_EAST) { + firstDirection = OpenSHC::Map::Enums::CD_EAST; + secondDirection = OpenSHC::Map::Enums::CD_WEST; + } else if (direction == OpenSHC::Map::Enums::CD_SOUTH) { + firstDirection = OpenSHC::Map::Enums::CD_NORTH; + secondDirection = OpenSHC::Map::Enums::CD_SOUTH; + } else if (direction == OpenSHC::Map::Enums::CD_WEST) { + firstDirection = OpenSHC::Map::Enums::CD_EAST; + secondDirection = OpenSHC::Map::Enums::CD_WEST; + } + + int first = this->directionTranslationMatrix[y][firstDirection] + tile; + if ((this->LogicLayer[first] & L_WALL_OR_GATEHOUSE) == 0) { + return FALSE; + } + if ((this->LogicLayer[first] & L_STOCKPILEUnk) != 0) { + return FALSE; + } + int minHeight = tileHeight - 16; + if (this->HeightLayer[first] < minHeight && this->DamageLayer[first] == 0) { + return FALSE; + } + + int second = this->directionTranslationMatrix[y][secondDirection] + tile; + if ((this->LogicLayer[second] & L_WALL_OR_GATEHOUSE) == 0) { + return FALSE; + } + if ((this->LogicLayer[second] & L_STOCKPILEUnk) != 0) { + return FALSE; + } + if (this->HeightLayer[second] < minHeight && this->DamageLayer[second] == 0) { + return FALSE; + } + return TRUE; + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/isWallCornerForCardinalDirection.cpp b/src/OpenSHC/Map/TileMapState/isWallCornerForCardinalDirection.cpp new file mode 100644 index 0000000000..00b0ff1dc0 --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/isWallCornerForCardinalDirection.cpp @@ -0,0 +1,56 @@ +#include "../TileMapState.func.hpp" + +#include "OpenSHC/Map/Enums/CardinalDirection.hpp" +#include "OpenSHC/WindowsHelper/Enums/BOOLEnum.hpp" + +namespace OpenSHC { +namespace Map { + + using OpenSHC::Map::LogicHelpers::L_CRENEL; + + // FUNCTION: STRONGHOLDCRUSADER 0x004F86D0 + BOOLEnum TileMapState::isWallCornerForCardinalDirection(int tile, int y, int direction) + { + int first = OpenSHC::Map::Enums::CD_NORTH; + int second = OpenSHC::Map::Enums::CD_NORTH; + int third = OpenSHC::Map::Enums::CD_NORTH; + int fourth = OpenSHC::Map::Enums::CD_NORTH; + if (direction == OpenSHC::Map::Enums::CD_NORTH) { + first = OpenSHC::Map::Enums::CD_NORTH; + second = OpenSHC::Map::Enums::CD_SOUTH; + third = OpenSHC::Map::Enums::CD_EAST; + fourth = OpenSHC::Map::Enums::CD_WEST; + } else if (direction == OpenSHC::Map::Enums::CD_EAST) { + first = OpenSHC::Map::Enums::CD_EAST; + second = OpenSHC::Map::Enums::CD_WEST; + third = OpenSHC::Map::Enums::CD_NORTH; + fourth = OpenSHC::Map::Enums::CD_SOUTH; + } else if (direction == OpenSHC::Map::Enums::CD_SOUTH) { + first = OpenSHC::Map::Enums::CD_NORTH; + second = OpenSHC::Map::Enums::CD_SOUTH; + third = OpenSHC::Map::Enums::CD_EAST; + fourth = OpenSHC::Map::Enums::CD_WEST; + } else if (direction == OpenSHC::Map::Enums::CD_WEST) { + first = OpenSHC::Map::Enums::CD_EAST; + second = OpenSHC::Map::Enums::CD_WEST; + third = OpenSHC::Map::Enums::CD_NORTH; + fourth = OpenSHC::Map::Enums::CD_SOUTH; + } + + if ((this->LogicLayer[this->directionTranslationMatrix[y][first] + tile] & L_CRENEL) == 0) { + return FALSE; + } + if ((this->LogicLayer[this->directionTranslationMatrix[y][second] + tile] & L_CRENEL) == 0) { + return FALSE; + } + if ((this->LogicLayer[this->directionTranslationMatrix[y][third] + tile] & L_CRENEL) != 0) { + return FALSE; + } + if ((this->LogicLayer[this->directionTranslationMatrix[y][fourth] + tile] & L_CRENEL) != 0) { + return FALSE; + } + return TRUE; + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/isWallCornerForDiagonalDirection.cpp b/src/OpenSHC/Map/TileMapState/isWallCornerForDiagonalDirection.cpp new file mode 100644 index 0000000000..8ee0b685b6 --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/isWallCornerForDiagonalDirection.cpp @@ -0,0 +1,55 @@ +#include "../TileMapState.func.hpp" + +#include "OpenSHC/Map/Enums/CardinalDirection.hpp" + +namespace OpenSHC { +namespace Map { + + using OpenSHC::Map::LogicHelpers::L_CRENEL; + + // FUNCTION: STRONGHOLDCRUSADER 0x004F8780 + BOOLEnum TileMapState::isWallCornerForDiagonalDirection(int tile, int y, int direction) + { + int first = OpenSHC::Map::Enums::CD_NORTH; + int second = OpenSHC::Map::Enums::CD_NORTH; + int third = OpenSHC::Map::Enums::CD_NORTH; + int fourth = OpenSHC::Map::Enums::CD_NORTH; + if (direction == OpenSHC::Map::Enums::CD_NORTHEAST) { + first = OpenSHC::Map::Enums::CD_SOUTH; + second = OpenSHC::Map::Enums::CD_WEST; + third = OpenSHC::Map::Enums::CD_NORTH; + fourth = OpenSHC::Map::Enums::CD_EAST; + } else if (direction == OpenSHC::Map::Enums::CD_SOUTHEAST) { + first = OpenSHC::Map::Enums::CD_NORTH; + second = OpenSHC::Map::Enums::CD_WEST; + third = OpenSHC::Map::Enums::CD_SOUTH; + fourth = OpenSHC::Map::Enums::CD_EAST; + } else if (direction == OpenSHC::Map::Enums::CD_SOUTHWEST) { + first = OpenSHC::Map::Enums::CD_NORTH; + second = OpenSHC::Map::Enums::CD_EAST; + third = OpenSHC::Map::Enums::CD_SOUTH; + fourth = OpenSHC::Map::Enums::CD_WEST; + } else if (direction == OpenSHC::Map::Enums::CD_NORTHWEST) { + first = OpenSHC::Map::Enums::CD_SOUTH; + second = OpenSHC::Map::Enums::CD_EAST; + third = OpenSHC::Map::Enums::CD_NORTH; + fourth = OpenSHC::Map::Enums::CD_WEST; + } + + if ((this->LogicLayer[this->directionTranslationMatrix[y][first] + tile] & L_CRENEL) == 0) { + return FALSE; + } + if ((this->LogicLayer[this->directionTranslationMatrix[y][second] + tile] & L_CRENEL) == 0) { + return FALSE; + } + if ((this->LogicLayer[this->directionTranslationMatrix[y][third] + tile] & L_CRENEL) != 0) { + return FALSE; + } + if ((this->LogicLayer[this->directionTranslationMatrix[y][fourth] + tile] & L_CRENEL) != 0) { + return FALSE; + } + return TRUE; + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/markBuildingFootprintFlag.cpp b/src/OpenSHC/Map/TileMapState/markBuildingFootprintFlag.cpp new file mode 100644 index 0000000000..882a70e418 --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/markBuildingFootprintFlag.cpp @@ -0,0 +1,30 @@ +#include "../TileMapState.func.hpp" + +#include "OpenSHC/Map/LogicHelpers/MiscDisplay.hpp" + +#include "OpenSHC/Globals/DAT_ViewportRenderState.hpp" + +namespace OpenSHC { +namespace Map { + + using OpenSHC::Map::LogicHelpers::L_PLAIN1_AND_FARM; + using OpenSHC::Map::LogicHelpers::MD_BUILDING_TILES; + + // FUNCTION: STRONGHOLDCRUSADER 0x004FA9D0 + void TileMapState::markBuildingFootprintFlag(int x, int y, int buildingSize) + { + int buildingSizeTileIndex = 0; + + do { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::getBuildingSizeIndexMappingData, this)( + buildingSizeTileIndex, buildingSize); + buildingSizeTileIndex++; + int tile = DAT_ViewportRenderState::instance.translationMatrix[this->buildingY + y].addXgetTile + + this->buildingX + x; + this->LogicLayer[tile] |= L_PLAIN1_AND_FARM; + this->MiscDisplayLayer[tile] &= ~MD_BUILDING_TILES; + } while (buildingSizeTileIndex < this->constructionTileCount); + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/noop1.cpp b/src/OpenSHC/Map/TileMapState/noop1.cpp new file mode 100644 index 0000000000..cc1385e676 --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/noop1.cpp @@ -0,0 +1,10 @@ +#include "../TileMapState.func.hpp" + +namespace OpenSHC { +namespace Map { + + // FUNCTION: STRONGHOLDCRUSADER 0x004F70D0 + void TileMapState::noop1(undefined4 unused) {} + +} +} diff --git a/src/OpenSHC/Map/TileMapState/placeDefensiveStructureTile.cpp b/src/OpenSHC/Map/TileMapState/placeDefensiveStructureTile.cpp new file mode 100644 index 0000000000..6df9fa22a2 --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/placeDefensiveStructureTile.cpp @@ -0,0 +1,112 @@ +#include "../TileMapState.func.hpp" + +#include "OpenSHC/Map/Buildings/BuildingsState.func.hpp" +#include "OpenSHC/Map/Navigation/PathFindingState.func.hpp" +#include "OpenSHC/UI/MinimapViewState.func.hpp" +#include "OpenSHC/Commands/MappersEnum.hpp" +#include "OpenSHC/Map/LogicHelpers/WallOwner.hpp" +#include "OpenSHC/WindowsHelper/Enums/BOOLEnum.hpp" + +#include "OpenSHC/Globals/DAT_BuildingsState.hpp" +#include "OpenSHC/Globals/DAT_LandscapeState.hpp" +#include "OpenSHC/Globals/DAT_MinimapViewState.hpp" +#include "OpenSHC/Globals/DAT_PathFindingState.hpp" +#include "OpenSHC/Globals/DAT_ViewportRenderState.hpp" + +namespace OpenSHC { +namespace Map { + + using OpenSHC::Map::LogicHelpers::L_BOULDERS; + using OpenSHC::Map::LogicHelpers::L_CRENEL; + using OpenSHC::Map::LogicHelpers::L_CRENEL_VARIATIONUnk; + using OpenSHC::Map::LogicHelpers::L_IRON; + using OpenSHC::Map::LogicHelpers::L_PEBBLES; + using OpenSHC::Map::LogicHelpers::L_PLAIN2_AND_PITCH; + using OpenSHC::Map::LogicHelpers::L_STAIRS; + using OpenSHC::Map::LogicHelpers::L_TREE; + using OpenSHC::Map::LogicHelpers::L_UNKNOWN_WALL_RELATED; + using OpenSHC::Map::LogicHelpers::L_WALL_OR_GATEHOUSE; + using OpenSHC::Map::LogicHelpers::WO_OWNER_MASK; + + // FUNCTION: STRONGHOLDCRUSADER 0x005034A0 + void TileMapState::placeDefensiveStructureTile(int playerID, int x, int y, MappersEnum mapper) + { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::validateWallPlacementAtTile, this)(playerID, x, y, mapper); + if (this->illegalBuild) { + return; + } + + int tile = DAT_ViewportRenderState::instance.translationMatrix[y].addXgetTile + x; + short command = mapper; + this->DamageLayer[tile] = 0; + this->HeightLayer[tile] = this->DefaultHeightLayer[tile]; + if (command == OpenSHC::Commands::M_MAPPER_WALL) { + this->HeightLayer[tile] = this->DefaultHeightLayer[tile] + 90; + } else if (command == OpenSHC::Commands::M_MAPPER_WOODWALL) { + this->HeightLayer[tile] = this->DefaultHeightLayer[tile] + 60; + } else if (command == OpenSHC::Commands::M_MAPPER_CRENAL) { + this->LogicLayer[tile] |= L_CRENEL; + this->HeightLayer[tile] += 98; + if ((x & 1) == 1 && (y & 1) == 0) { + this->LogicLayer[tile] |= L_CRENEL_VARIATIONUnk; + } else if ((x & 1) == 0 && (y & 1) == 1) { + this->LogicLayer[tile] |= L_CRENEL_VARIATIONUnk; + } + } else if (command == OpenSHC::Commands::M_MAPPER_CRENAL2) { + this->LogicLayer[tile] |= L_CRENEL; + this->HeightLayer[tile] += 68; + if ((x & 1) == 1 && (y & 1) == 0) { + this->LogicLayer[tile] |= L_CRENEL_VARIATIONUnk; + } else if ((x & 1) == 0 && (y & 1) == 1) { + this->LogicLayer[tile] |= L_CRENEL_VARIATIONUnk; + } + } else if (command == OpenSHC::Commands::M_MAPPER_STAIR1) { + this->LogicLayer[tile] |= L_STAIRS; + this->HeightLayer[tile] += 80; + } else if (command == OpenSHC::Commands::M_MAPPER_STAIR2) { + this->LogicLayer[tile] |= L_STAIRS; + this->HeightLayer[tile] += 64; + } else if (command == OpenSHC::Commands::M_MAPPER_STAIR3) { + this->LogicLayer[tile] |= L_STAIRS; + this->HeightLayer[tile] += 48; + } else if (command == OpenSHC::Commands::M_MAPPER_STAIR4) { + this->LogicLayer[tile] |= L_STAIRS; + this->HeightLayer[tile] += 32; + } else if (command == OpenSHC::Commands::M_MAPPER_STAIR5) { + this->LogicLayer[tile] |= L_STAIRS; + this->HeightLayer[tile] += 16; + } + + this->LogicLayer[tile] = this->LogicLayer[tile] & ~(L_UNKNOWN_WALL_RELATED | L_BOULDERS | L_PEBBLES | L_IRON) + | L_WALL_OR_GATEHOUSE; + if ((this->LogicLayer[tile] & L_PLAIN2_AND_PITCH) != 0) { + this->LogicLayer[tile] &= ~L_PLAIN2_AND_PITCH; + this->HeightLayer[tile] += 4; + } + if ((this->LogicLayer[tile] & L_TREE) != 0) { + int organism = this->OrganismLayer[tile]; + if (organism < countof(DAT_LandscapeState::instance.trees)) { + DAT_LandscapeState::instance.trees[organism].state = 3; + } + } + if (command == OpenSHC::Commands::M_MAPPER_WOODWALL) { + this->LogicLayer[tile] |= L_UNKNOWN_WALL_RELATED; + } + this->WallOwnerLayer[tile] = (this->WallOwnerLayer[tile] & ~WO_OWNER_MASK) | (playerID - 1); + MACRO_CALL_MEMBER(OpenSHC::Map::Navigation::PathFindingState_Func::updatePathLinkagesInAllEightDirections, + DAT_PathFindingState::ptr)(y, tile); + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::clearMoat, this)(tile); + MACRO_CALL_MEMBER(OpenSHC::Map::Navigation::PathFindingState_Func::updateWalkAndPathLayer, + DAT_PathFindingState::ptr)(7, x, y); + if (command != OpenSHC::Commands::M_MAPPER_STAIR1 && command != OpenSHC::Commands::M_MAPPER_STAIR2 + && command != OpenSHC::Commands::M_MAPPER_STAIR3 && command != OpenSHC::Commands::M_MAPPER_STAIR4 + && command != OpenSHC::Commands::M_MAPPER_STAIR5 && command != OpenSHC::Commands::M_MAPPER_STAIR6) { + MACRO_CALL_MEMBER(OpenSHC::Map::Buildings::BuildingsState_Func::processWallBuildingLoss, + DAT_BuildingsState::ptr)(playerID, 1, 0, 0); + } + DAT_PathFindingState::instance.toggleUpdateSeparateAreaTileMap = 1; + MACRO_CALL_MEMBER(OpenSHC::UI::MinimapViewState_Func::triggerMinimapRedraw, DAT_MinimapViewState::ptr)(1); + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/planMoat.cpp b/src/OpenSHC/Map/TileMapState/planMoat.cpp new file mode 100644 index 0000000000..ec30ca6bb9 --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/planMoat.cpp @@ -0,0 +1,62 @@ +#include "../TileMapState.func.hpp" + +#include "OpenSHC/Map/Navigation/PathFindingState.func.hpp" + +#include "OpenSHC/Globals/DAT_PathFindingState.hpp" +#include "OpenSHC/Globals/DAT_ViewportRenderState.hpp" + +namespace OpenSHC { +namespace Map { + + using OpenSHC::Map::LogicHelpers::L_BORDER; + using OpenSHC::Map::LogicHelpers::L_BORDER_EDGE; + using OpenSHC::Map::LogicHelpers::L_BOULDERS; + using OpenSHC::Map::LogicHelpers::L_BUILDING; + using OpenSHC::Map::LogicHelpers::L_FORD; + using OpenSHC::Map::LogicHelpers::L_IRON; + using OpenSHC::Map::LogicHelpers::L_KEEP_NON_MANOR_HOUSE; + using OpenSHC::Map::LogicHelpers::L_MARSH; + using OpenSHC::Map::LogicHelpers::L_MOAT; + using OpenSHC::Map::LogicHelpers::L_MOAT_DUG_OR_PLANNED; + using OpenSHC::Map::LogicHelpers::L_PLAIN1_AND_FARM; + using OpenSHC::Map::LogicHelpers::L_RIVER; + using OpenSHC::Map::LogicHelpers::L_ROCKY; + using OpenSHC::Map::LogicHelpers::L_SEA; + using OpenSHC::Map::LogicHelpers::L_WALL_OR_GATEHOUSE; + + // FUNCTION: STRONGHOLDCRUSADER 0x00514480 + void TileMapState::planMoat(undefined4 playerID, int tile, uint tileY) + { + uint logic = this->LogicLayer[tile]; + if ((logic & (L_BORDER | L_BORDER_EDGE)) != 0) { + return; + } + if (this->OrganismLayer[tile] != 0) { + return; + } + if ((logic & (L_BUILDING | L_KEEP_NON_MANOR_HOUSE)) != 0) { + return; + } + if (this->BuildingLayer[tile] != 0) { + return; + } + if ((logic & (L_WALL_OR_GATEHOUSE | L_BOULDERS | L_IRON | L_RIVER | L_FORD | L_MARSH)) != 0) { + return; + } + if ((logic & (L_SEA | L_PLAIN1_AND_FARM | L_ROCKY)) != 0) { + return; + } + + int moatX = tile - DAT_ViewportRenderState::instance.translationMatrix[tileY].addXgetTile; + if ((logic & (L_MOAT_DUG_OR_PLANNED | L_MOAT)) == 0 && this->HeightLayer[tile] <= 12) { + if (MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::createMoatData, this)(playerID, moatX, tileY, 0) + != 0) { + this->LogicLayer[tile] |= L_MOAT_DUG_OR_PLANNED; + } + MACRO_CALL_MEMBER(OpenSHC::Map::Navigation::PathFindingState_Func::updatePathLinkagesInAllEightDirections, + DAT_PathFindingState::ptr)(tileY, tile); + } + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/processMapOrientationChange.cpp b/src/OpenSHC/Map/TileMapState/processMapOrientationChange.cpp index b1afd2b135..1189846f3b 100644 --- a/src/OpenSHC/Map/TileMapState/processMapOrientationChange.cpp +++ b/src/OpenSHC/Map/TileMapState/processMapOrientationChange.cpp @@ -6,10 +6,10 @@ namespace Map { // FUNCTION: STRONGHOLDCRUSADER 0x00512920 void TileMapState::processMapOrientationChange() { - if ((int)this->DAT_FutureMapOrientation < 8) { + if ((int)this->DAT_FutureMapOrientation < OpenSHC::Map::Enums::MO_UPPER_LIMIT) { MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::changeMapOrientation, this)( (MapOrientation)this->DAT_FutureMapOrientation); - this->DAT_FutureMapOrientation = 8; + this->DAT_FutureMapOrientation = OpenSHC::Map::Enums::MO_UPPER_LIMIT; } } diff --git a/src/OpenSHC/Map/TileMapState/rebuildShowHiLayerFromHeights.cpp b/src/OpenSHC/Map/TileMapState/rebuildShowHiLayerFromHeights.cpp new file mode 100644 index 0000000000..2587837363 --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/rebuildShowHiLayerFromHeights.cpp @@ -0,0 +1,17 @@ +#include "../TileMapState.func.hpp" + +namespace OpenSHC { +namespace Map { + + // FUNCTION: STRONGHOLDCRUSADER 0x004F79D0 + void TileMapState::rebuildShowHiLayerFromHeights() + { + for (int tile = 0; tile < countof(this->ShowHiLayer); tile += 8) { + for (int i = 0; i < 8; i++) { + this->ShowHiLayer[tile + i] = this->heightBasedScreenYOffset[this->HeightLayer[tile + i]]; + } + } + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/recountTotalOwnedMoats.cpp b/src/OpenSHC/Map/TileMapState/recountTotalOwnedMoats.cpp index a4bb5f1a70..e199706384 100644 --- a/src/OpenSHC/Map/TileMapState/recountTotalOwnedMoats.cpp +++ b/src/OpenSHC/Map/TileMapState/recountTotalOwnedMoats.cpp @@ -7,9 +7,8 @@ namespace Map { void TileMapState::recountTotalOwnedMoats() { this->currentMoatCount = 0; - for (int i = 1; i < 16000; ++i) { + for (int i = 1; i < countof(this->moats); ++i) { if (this->moats[i].owner != 0) { - // not a count: one past the highest owned moat index, used as loop bound this->currentMoatCount = i + 1; } } diff --git a/src/OpenSHC/Map/TileMapState/removeMoat.cpp b/src/OpenSHC/Map/TileMapState/removeMoat.cpp new file mode 100644 index 0000000000..fa3df6df54 --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/removeMoat.cpp @@ -0,0 +1,40 @@ +#include "../TileMapState.func.hpp" + +#include "OpenSHC/Map/Navigation/PathFindingState.func.hpp" +#include "OpenSHC/UI/MinimapViewState.func.hpp" +#include "OpenSHC/WindowsHelper/Enums/BOOLEnum.hpp" + +#include "OpenSHC/Globals/DAT_MinimapViewState.hpp" +#include "OpenSHC/Globals/DAT_PathFindingState.hpp" + +namespace OpenSHC { +namespace Map { + + using OpenSHC::Map::LogicHelpers::L_MOAT; + + // FUNCTION: STRONGHOLDCRUSADER 0x005113F0 + BOOLEnum TileMapState::removeMoat(uint moatID, int unused) + { + this->moats[moatID].fillProgress -= 1; + if (this->moats[moatID].fillProgress <= 0) { + this->HeightLayer[this->moats[moatID].tile] = 8; + this->LogicLayer[this->moats[moatID].tile] &= ~L_MOAT; + MACRO_CALL_MEMBER(OpenSHC::Map::Navigation::PathFindingState_Func::updateWalkAndPathLayer, + DAT_PathFindingState::ptr)(2, this->moats[moatID].x, this->moats[moatID].y); + MACRO_CALL_MEMBER(OpenSHC::Map::Navigation::PathFindingState_Func::updatePathLinkagesInAllEightDirections, + DAT_PathFindingState::ptr)(this->moats[moatID].y, this->moats[moatID].tile); + DAT_PathFindingState::instance.toggleUpdateSeparateAreaTileMap = 1; + int tile = this->moats[moatID].tile; + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::clearMoatData, this)(moatID); + if (this->BuildingLayer[tile] != 0) { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::updateBuildingGraphicsLayer, this)( + this->BuildingLayer[tile]); + } + MACRO_CALL_MEMBER(OpenSHC::UI::MinimapViewState_Func::triggerMinimapRedraw, DAT_MinimapViewState::ptr)(1); + return TRUE; + } + return FALSE; + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/resetAreaBasedOnLogicalLayer.cpp b/src/OpenSHC/Map/TileMapState/resetAreaBasedOnLogicalLayer.cpp new file mode 100644 index 0000000000..8001534aa3 --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/resetAreaBasedOnLogicalLayer.cpp @@ -0,0 +1,65 @@ +#include "../TileMapState.func.hpp" + +#include "OpenSHC/Globals/DAT_ViewportRenderState.hpp" + +namespace OpenSHC { +namespace Map { + + using OpenSHC::Map::LogicHelpers::L_BORDER; + using OpenSHC::Map::LogicHelpers::L_BORDER_EDGE; + + // FUNCTION: STRONGHOLDCRUSADER 0x005127B0 + void TileMapState::resetAreaBasedOnLogicalLayer() + { + for (int tile = 0; tile < countof(this->LogicLayer); tile++) { + if ((this->LogicLayer[tile] & (L_BORDER | L_BORDER_EDGE)) == 0) { + continue; + } + + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::useEraserBrush, this)(tile + - DAT_ViewportRenderState::instance + .translationMatrix[DAT_ViewportRenderState::instance.tileTranslationMatrix_YComponent[tile]] + .addXgetTile, + DAT_ViewportRenderState::instance.tileTranslationMatrix_YComponent[tile], 1); + this->GfxLayer[tile] = 0; + this->AlphaGFXLayer[tile] = 0; + this->ConstructionGFXLayer[tile] = 0; + this->PillarGFXLayer[tile] = 1; + this->WallGFXLayer[tile] = 0; + this->FloatingLayer[tile] = 0; + this->UnitLayer[tile] = 0; + this->EntityLayer[tile] = 0; + this->EntityLayerLT25[tile] = 0; + this->BuildingLayer[tile] = 0; + this->LogicLayer[tile] = 0; + this->Logic2Layer[tile] = 0; + this->ChangedLayer[tile] = 0; + this->OrganismLayer[tile] = 0; + this->DamageLayer[tile] = 0; + this->MacroLayer[tile] = 0; + this->HeightLayer[tile] = 8; + this->DefaultHeightLayer[tile] = 8; + this->ShowHiLayer[tile] = 0; + this->MiscDisplayLayer[tile] = 0; + this->LuminesenceLayer[tile] = 0; + this->WallOwnerLayer[tile] = 0; + this->BuildingWasLayer[tile] = 0; + this->OccupancyLayer[tile] = 0; + this->AIZoneLayer[tile] = 0; + this->AIInfoLayer[tile] = 0; + this->unitDeathHeatMap[tile] = 0; + this->SEC_TileMap1104[tile] = 0; + this->SEC_PathfindingCostTileMap1105[0][tile] = 0; + this->SEC_PathfindingCostTileMap1105[1][tile] = 0; + this->SEC_PathfindingCostTileMap1105[2][tile] = 0; + this->SEC_PathfindingCostTileMap1105[3][tile] = 0; + this->SEC_PathfindingCostTileMap1105[4][tile] = 0; + this->SEC_PathfindingCostTileMap1105[5][tile] = 0; + this->SEC_PathfindingCostTileMap1105[6][tile] = 0; + this->SEC_PathfindingCostTileMap1105[7][tile] = 0; + this->SEC_PathfindingCostTileMap1105[8][tile] = 0; + } + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/resetHeightAndMapBorders.cpp b/src/OpenSHC/Map/TileMapState/resetHeightAndMapBorders.cpp new file mode 100644 index 0000000000..2df49efedf --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/resetHeightAndMapBorders.cpp @@ -0,0 +1,103 @@ +#include "../TileMapState.func.hpp" + +#include "OpenSHC/Map/Navigation/PathFindingState.func.hpp" +#include "OpenSHC/Map/MapConstants.hpp" + +#include "OpenSHC/Globals/DAT_PathFindingState.hpp" + +namespace OpenSHC { +namespace Map { + + using OpenSHC::Map::LogicHelpers::L_BORDER; + using OpenSHC::Map::LogicHelpers::L_BORDER_EDGE; + + // FUNCTION: STRONGHOLDCRUSADER 0x004F6CF0 + void TileMapState::resetHeightAndMapBorders(int mapSize) + { + int inset = (MC_MAP_XY_LIMIT - mapSize) / 2; + this->mapSize = mapSize; + for (int tile = 0; tile < countof(this->LogicLayer); tile++) { + if ((this->LogicLayer[tile] & (L_BORDER | L_BORDER_EDGE)) != 0) { + this->HeightLayer[tile] = this->DefaultHeightLayer[tile]; + } + this->LogicLayer[tile] &= ~(L_BORDER | L_BORDER_EDGE); + } + + int rowLength = 0; + int tile = 0; + for (; rowLength < MC_MAP_XY_LIMIT; rowLength += 2) { + this->LogicLayer[tile] |= L_BORDER; + tile++; + if (rowLength > 0) { + this->LogicLayer[tile] |= L_BORDER_EDGE; + } + tile += rowLength; + if (rowLength > 0 && (this->LogicLayer[tile - 1] & L_BORDER) == 0) { + this->LogicLayer[tile - 1] = L_BORDER_EDGE; + } + this->LogicLayer[tile] = L_BORDER; + tile++; + } + + for (rowLength = MC_MAP_XY_LIMIT - 2; rowLength >= 0; rowLength -= 2) { + this->LogicLayer[tile] = L_BORDER; + tile++; + if (rowLength > 0) { + this->LogicLayer[tile] = L_BORDER_EDGE; + } + tile += rowLength; + if (rowLength > 0 && (this->LogicLayer[tile - 1] & L_BORDER) == 0) { + this->LogicLayer[tile - 1] = L_BORDER_EDGE; + } + this->LogicLayer[tile] = L_BORDER; + tile++; + } + + int row = 0; + tile = 0; + for (int skipped = 0; skipped < inset; skipped++) { + tile += 2 + row; + row += 2; + } + int firstRow = row; + for (; row < MC_MAP_XY_LIMIT; row += 2) { + this->LogicLayer[tile + inset] |= L_BORDER; + tile++; + if (row - inset > 0) { + this->LogicLayer[tile + inset] |= L_BORDER_EDGE; + } + tile += row; + if (row - inset > 0 && (this->LogicLayer[tile - inset] & L_BORDER) == 0) { + this->LogicLayer[tile - inset - 1] |= L_BORDER_EDGE; + } + this->LogicLayer[tile - inset] |= L_BORDER; + tile++; + } + for (row = MC_MAP_XY_LIMIT - 2; row >= firstRow; row -= 2) { + this->LogicLayer[tile + inset] |= L_BORDER; + tile++; + if (row - inset > 0) { + this->LogicLayer[tile + inset] |= L_BORDER_EDGE; + } + tile += row; + if (row - inset > 0 && (this->LogicLayer[tile - inset] & L_BORDER) == 0) { + this->LogicLayer[tile - inset - 1] |= L_BORDER_EDGE; + } + this->LogicLayer[tile - inset] |= L_BORDER; + tile++; + } + if (mapSize != MC_MAP_XY_LIMIT) { + MACRO_CALL_MEMBER( + OpenSHC::Map::Navigation::PathFindingState_Func::setWalkabilityBorderLogicLayerForSmallerMapSizes, + DAT_PathFindingState::ptr)(); + } + + for (int borderTile = 0; borderTile < countof(this->LogicLayer); borderTile++) { + if ((this->LogicLayer[borderTile] & (L_BORDER | L_BORDER_EDGE)) != 0) { + this->HeightLayer[borderTile] = 8; + } + } + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/resetTileAndClearMoat.cpp b/src/OpenSHC/Map/TileMapState/resetTileAndClearMoat.cpp new file mode 100644 index 0000000000..58a4a87ec9 --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/resetTileAndClearMoat.cpp @@ -0,0 +1,63 @@ +#include "../TileMapState.func.hpp" + +#include "OpenSHC/Map/LogicHelpers/MiscDisplay.hpp" +#include "OpenSHC/Map/Trees/TreeType.hpp" + +#include "OpenSHC/Globals/DAT_LandscapeState.hpp" +#include "OpenSHC/Globals/DAT_TileMapState.hpp" +#include "OpenSHC/Globals/DAT_ViewportRenderState.hpp" + +namespace OpenSHC { +namespace Map { + + using OpenSHC::Map::LogicHelpers::L_CRENEL; + using OpenSHC::Map::LogicHelpers::L_CRENEL_VARIATIONUnk; + using OpenSHC::Map::LogicHelpers::L_MOAT; + using OpenSHC::Map::LogicHelpers::L_MOAT_DUG_OR_PLANNED; + using OpenSHC::Map::LogicHelpers::L_PLAIN2_AND_PITCH; + using OpenSHC::Map::LogicHelpers::L_STAIRS; + using OpenSHC::Map::LogicHelpers::L_TREE; + using OpenSHC::Map::LogicHelpers::L_UNKNOWN_WALL_RELATED; + using OpenSHC::Map::LogicHelpers::L_WALL_OR_GATEHOUSE; + using OpenSHC::Map::LogicHelpers::MD_SEA_EDGES; + + // FUNCTION: STRONGHOLDCRUSADER 0x00507350 + void TileMapState::resetTileAndClearMoat(int tile) + { + int const y = DAT_ViewportRenderState::instance.tileTranslationMatrix_YComponent[tile]; + int const x = tile - DAT_ViewportRenderState::instance.translationMatrix[y].addXgetTile; + this->LogicLayer[tile] &= ~(L_PLAIN2_AND_PITCH | L_WALL_OR_GATEHOUSE | L_CRENEL | L_STAIRS + | L_UNKNOWN_WALL_RELATED | L_CRENEL_VARIATIONUnk); + this->HeightLayer[tile] = DAT_TileMapState::instance.DefaultHeightLayer[tile]; + this->DamageLayer[tile] = 0; + if ((this->LogicLayer[tile] & L_TREE) != 0) { + int treeID = this->OrganismLayer[tile]; + if (treeID < countof(DAT_LandscapeState::instance.trees)) { + switch (DAT_LandscapeState::instance.trees[treeID].treeType) { + case OpenSHC::Map::Trees::TT_UNKNOWN_5: + case OpenSHC::Map::Trees::TT_UNKNOWN_6: + case OpenSHC::Map::Trees::TT_UNKNOWN_7: + case OpenSHC::Map::Trees::TT_UNKNOWN_8: + case OpenSHC::Map::Trees::TT_UNKNOWN_9: + case OpenSHC::Map::Trees::TT_UNKNOWN_10: + case OpenSHC::Map::Trees::TT_UNKNOWN_11: + case OpenSHC::Map::Trees::TT_UNKNOWN_12: + case OpenSHC::Map::Trees::TT_UNKNOWN_13: + case OpenSHC::Map::Trees::TT_UNKNOWN_14: + case OpenSHC::Map::Trees::TT_UNKNOWN_16: + case OpenSHC::Map::Trees::TT_UNKNOWN_17: + case OpenSHC::Map::Trees::TT_UNKNOWN_18: + case OpenSHC::Map::Trees::TT_UNKNOWN_19: + DAT_LandscapeState::instance.trees[treeID].state = 3; + } + } + } + if ((this->LogicLayer[tile] & (L_MOAT | L_MOAT_DUG_OR_PLANNED)) != 0) { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::clearMoatDataAtTile, this)(x, y); + this->LogicLayer[tile] &= ~(L_MOAT | L_MOAT_DUG_OR_PLANNED); + } + this->MiscDisplayLayer[tile] &= ~MD_SEA_EDGES; + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/resetTileToDefaultState.cpp b/src/OpenSHC/Map/TileMapState/resetTileToDefaultState.cpp new file mode 100644 index 0000000000..f21491c8d6 --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/resetTileToDefaultState.cpp @@ -0,0 +1,30 @@ +#include "../TileMapState.func.hpp" + +#include "OpenSHC/Map/Entities/EntityState.func.hpp" +#include "OpenSHC/Map/Navigation/PathFindingState.func.hpp" + +#include "OpenSHC/Globals/DAT_EntityState.hpp" +#include "OpenSHC/Globals/DAT_PathFindingState.hpp" + +namespace OpenSHC { +namespace Map { + + using OpenSHC::Map::LogicHelpers::L_CRENEL; + using OpenSHC::Map::LogicHelpers::L_CRENEL_VARIATIONUnk; + using OpenSHC::Map::LogicHelpers::L_STAIRS; + using OpenSHC::Map::LogicHelpers::L_UNKNOWN_WALL_RELATED; + using OpenSHC::Map::LogicHelpers::L_WALL_OR_GATEHOUSE; + + // FUNCTION: STRONGHOLDCRUSADER 0x004F81D0 + void TileMapState::resetTileToDefaultState(int tile, uint x, uint y) + { + this->LogicLayer[tile] + &= ~(L_WALL_OR_GATEHOUSE | L_CRENEL | L_STAIRS | L_UNKNOWN_WALL_RELATED | L_CRENEL_VARIATIONUnk); + this->HeightLayer[tile] = this->DefaultHeightLayer[tile]; + MACRO_CALL_MEMBER(OpenSHC::Map::Entities::EntityState_Func::destroyEntitiesOnTile, DAT_EntityState::ptr)(tile); + MACRO_CALL_MEMBER(OpenSHC::Map::Navigation::PathFindingState_Func::updateWalkAndPathLayer, + DAT_PathFindingState::ptr)(5, x, y); + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/returnMoatIDForPlayerIDs.cpp b/src/OpenSHC/Map/TileMapState/returnMoatIDForPlayerIDs.cpp new file mode 100644 index 0000000000..71e68644fa --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/returnMoatIDForPlayerIDs.cpp @@ -0,0 +1,18 @@ +#include "../TileMapState.func.hpp" + +namespace OpenSHC { +namespace Map { + + // FUNCTION: STRONGHOLDCRUSADER 0x00500290 + int TileMapState::returnMoatIDForPlayerIDs(int playerID, int playerID2) + { + for (int moatID = 1; moatID < this->currentMoatCount; moatID++) { + if (this->moats[moatID].tile == playerID && this->moats[moatID].owner == playerID2) { + return moatID; + } + } + return 0; + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/returnOwnedMoatAtTile.cpp b/src/OpenSHC/Map/TileMapState/returnOwnedMoatAtTile.cpp new file mode 100644 index 0000000000..e31e800025 --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/returnOwnedMoatAtTile.cpp @@ -0,0 +1,18 @@ +#include "../TileMapState.func.hpp" + +namespace OpenSHC { +namespace Map { + + // FUNCTION: STRONGHOLDCRUSADER 0x00500250 + int TileMapState::returnOwnedMoatAtTile(int targetedTile) + { + for (int moatID = 1; moatID < this->currentMoatCount; moatID++) { + if (this->moats[moatID].owner != 0 && this->moats[moatID].tile == targetedTile) { + return moatID; + } + } + return 0; + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/rotateFearFactorBuildingVariations.cpp b/src/OpenSHC/Map/TileMapState/rotateFearFactorBuildingVariations.cpp new file mode 100644 index 0000000000..51fdba803f --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/rotateFearFactorBuildingVariations.cpp @@ -0,0 +1,99 @@ +#include "../TileMapState.func.hpp" + +#include "OpenSHC/Commands/MappersEnum.hpp" +#include "OpenSHC/Map/Rocks/Enums/EditorRockType.hpp" + +#include "OpenSHC/Globals/DAT_TileMapState.hpp" + +namespace OpenSHC { +namespace Map { + + // FUNCTION: STRONGHOLDCRUSADER 0x004F8300 + void TileMapState::rotateFearFactorBuildingVariations() + { + int now = timeGetTime(); + if (this->editorRockType == OpenSHC::Map::Rocks::Enums::ERT_BIGROCK5) { + if (this->rockOrientation == OpenSHC::Map::Enums::MO_NORTH) { + this->rockOrientation = OpenSHC::Map::Enums::MO_EAST; + } else if (this->rockOrientation == OpenSHC::Map::Enums::MO_EAST) { + this->rockOrientation = OpenSHC::Map::Enums::MO_SOUTH; + } else if (this->rockOrientation == OpenSHC::Map::Enums::MO_SOUTH) { + this->rockOrientation = OpenSHC::Map::Enums::MO_WEST; + } else { + this->rockOrientation = OpenSHC::Map::Enums::MO_NORTH; + } + } + if (now < this->lastTime + 1000) { + return; + } + + this->lastTime = now; + if (this->gateOrientationVariant == 80) { + this->gateOrientationVariant = 81; + } else { + this->gateOrientationVariant = 80; + } + if (this->editorRockType == OpenSHC::Map::Rocks::Enums::ERT_NONE) { + if (this->rockOrientation == OpenSHC::Map::Enums::MO_NORTH) { + this->rockOrientation = OpenSHC::Map::Enums::MO_EAST; + } else if (this->rockOrientation == OpenSHC::Map::Enums::MO_EAST) { + this->rockOrientation = OpenSHC::Map::Enums::MO_SOUTH; + } else if (this->rockOrientation == OpenSHC::Map::Enums::MO_SOUTH) { + this->rockOrientation = OpenSHC::Map::Enums::MO_WEST; + } else { + this->rockOrientation = OpenSHC::Map::Enums::MO_NORTH; + } + } + + switch (DAT_TileMapState::instance.currentMapperCommand) { + case OpenSHC::Commands::M_MAPPER_GARDEN1: + case OpenSHC::Commands::M_MAPPER_GARDEN2: + case OpenSHC::Commands::M_MAPPER_GARDEN3: + case OpenSHC::Commands::M_MAPPER_GARDEN4: + case OpenSHC::Commands::M_MAPPER_GARDEN5: + case OpenSHC::Commands::M_MAPPER_GARDEN7: + case OpenSHC::Commands::M_MAPPER_GARDEN8: + case OpenSHC::Commands::M_MAPPER_GARDEN10: + case OpenSHC::Commands::M_MAPPER_GARDEN11: + case OpenSHC::Commands::M_MAPPER_CESS_PIT1: + case OpenSHC::Commands::M_MAPPER_CESS_PIT2: + case OpenSHC::Commands::M_MAPPER_CESS_PIT3: + case OpenSHC::Commands::M_MAPPER_STATUE1: + case OpenSHC::Commands::M_MAPPER_STATUE2: + case OpenSHC::Commands::M_MAPPER_STATUE3: + case OpenSHC::Commands::M_MAPPER_STATUE4: + case OpenSHC::Commands::M_MAPPER_SHRINE1: + case OpenSHC::Commands::M_MAPPER_POND1: + case OpenSHC::Commands::M_MAPPER_POND3_LARGE1: + DAT_TileMapState::instance.currentMapperCommand + = (MappersEnum)(DAT_TileMapState::instance.currentMapperCommand + OpenSHC::Commands::M_MAPPER_AREA); + return; + case OpenSHC::Commands::M_MAPPER_GARDEN6: + DAT_TileMapState::instance.currentMapperCommand = OpenSHC::Commands::M_MAPPER_GARDEN1; + return; + case OpenSHC::Commands::M_MAPPER_GARDEN9: + DAT_TileMapState::instance.currentMapperCommand = OpenSHC::Commands::M_MAPPER_GARDEN7; + return; + case OpenSHC::Commands::M_MAPPER_GARDEN12: + DAT_TileMapState::instance.currentMapperCommand = OpenSHC::Commands::M_MAPPER_GARDEN10; + return; + case OpenSHC::Commands::M_MAPPER_STATUE5: + DAT_TileMapState::instance.currentMapperCommand = OpenSHC::Commands::M_MAPPER_STATUE1; + return; + case OpenSHC::Commands::M_MAPPER_POND2_SMALL: + DAT_TileMapState::instance.currentMapperCommand = OpenSHC::Commands::M_MAPPER_POND1; + return; + case OpenSHC::Commands::M_MAPPER_POND4_LARGE2: + DAT_TileMapState::instance.currentMapperCommand = OpenSHC::Commands::M_MAPPER_POND3_LARGE1; + return; + case OpenSHC::Commands::M_MAPPER_SHRINE2: + DAT_TileMapState::instance.currentMapperCommand = OpenSHC::Commands::M_MAPPER_SHRINE1; + return; + case OpenSHC::Commands::M_MAPPER_CESS_PIT4: + DAT_TileMapState::instance.currentMapperCommand = OpenSHC::Commands::M_MAPPER_CESS_PIT1; + return; + } + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/setChangedLayerToThreeAndMapping0x40x40.cpp b/src/OpenSHC/Map/TileMapState/setChangedLayerToThreeAndMapping0x40x40.cpp new file mode 100644 index 0000000000..4e41e3e7e0 --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/setChangedLayerToThreeAndMapping0x40x40.cpp @@ -0,0 +1,24 @@ +#include "../TileMapState.func.hpp" + +#include "OpenSHC/IO/LowLevelMemory.func.hpp" +#include "OpenSHC/Map/Navigation/PathFindingState.func.hpp" +#include "OpenSHC/OS.func.hpp" + +#include "OpenSHC/Globals/DAT_LowLevelMemory.hpp" +#include "OpenSHC/Globals/DAT_PathFindingState.hpp" + +namespace OpenSHC { +namespace Map { + + // FUNCTION: STRONGHOLDCRUSADER 0x004F6AE0 + void TileMapState::setChangedLayerToThreeAndMapping0x40x40() + { + MACRO_CALL_MEMBER(OpenSHC::IO::LowLevelMemory_Func::fillMemory_ByteValue, DAT_LowLevelMemory::ptr)( + sizeof(this->ChangedLayer), 3, this->ChangedLayer); + MACRO_CALL(OpenSHC::OS_Func::_memset)(this->mapping40x40, 3, sizeof(this->mapping40x40)); + MACRO_CALL_MEMBER(OpenSHC::Map::Navigation::PathFindingState_Func::setChangedLayerZeroBasedOn40x40Layer, + DAT_PathFindingState::ptr)(1); + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/setMapSize.cpp b/src/OpenSHC/Map/TileMapState/setMapSize.cpp new file mode 100644 index 0000000000..c0c4b15a4b --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/setMapSize.cpp @@ -0,0 +1,22 @@ +#include "../TileMapState.func.hpp" + +#include "OpenSHC/Globals/DAT_TerrainDefinedData.hpp" + +namespace OpenSHC { +namespace Map { + + // FUNCTION: STRONGHOLDCRUSADER 0x004F6F90 + void TileMapState::setMapSize(int size) + { + int index = 0; + for (; size <= DAT_TerrainDefinedData::instance.MapSizes[index]; index++) {} + + if (DAT_TerrainDefinedData::instance.MapSizes[index] == -1) { + this->mapSize = DAT_TerrainDefinedData::instance.MapSizes[0]; + return; + } + this->mapSize = DAT_TerrainDefinedData::instance.MapSizes[index]; + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/setSignpostDistanceForCampaignMission.cpp b/src/OpenSHC/Map/TileMapState/setSignpostDistanceForCampaignMission.cpp new file mode 100644 index 0000000000..bbb367ee7e --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/setSignpostDistanceForCampaignMission.cpp @@ -0,0 +1,39 @@ +#include "../TileMapState.func.hpp" + +#include "OpenSHC/Game/GameMode2.hpp" + +#include "OpenSHC/Globals/DAT_GameCore.hpp" +#include "OpenSHC/Globals/DAT_GameState.hpp" +#include "OpenSHC/Globals/DAT_MapPropertiesState.hpp" + +namespace OpenSHC { +namespace Map { + + // FUNCTION: STRONGHOLDCRUSADER 0x00500BB0 + void TileMapState::setSignpostDistanceForCampaignMission() + { + DAT_GameState::instance.mapAndTime.unk_signpostDistance = 30; + if (DAT_GameCore::instance.gameMode_2 == OpenSHC::Game::GM_CAMPAIGN_MISSION) { + if (DAT_GameCore::instance.missionNumber1to20 <= 2) { + DAT_GameState::instance.mapAndTime.unk_signpostDistance = 0; + return; + } + if (DAT_GameCore::instance.missionNumber1to20 <= 4) { + DAT_GameState::instance.mapAndTime.unk_signpostDistance = 15; + return; + } + if (DAT_GameCore::instance.missionNumber1to20 == 6) { + DAT_GameState::instance.mapAndTime.unk_signpostDistance = 37; + } + return; + } + + if (DAT_GameCore::instance.gameMode_2 == OpenSHC::Game::GM_ECONOMIC_CAMPAIGN_SH1 + || (DAT_GameCore::instance.gameMode_2 == OpenSHC::Game::GM_BUILDERUnk + && DAT_MapPropertiesState::instance.SEC_U3_MapType2_1 >= OpenSHC::Map::MT_ECONOMIC)) { + DAT_GameState::instance.mapAndTime.unk_signpostDistance = 7; + } + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/setupAllMapSections.cpp b/src/OpenSHC/Map/TileMapState/setupAllMapSections.cpp new file mode 100644 index 0000000000..6ad8329258 --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/setupAllMapSections.cpp @@ -0,0 +1,138 @@ +#include "../TileMapState.func.hpp" + +#include "OpenSHC/Game/GameStateStructures.func.hpp" +#include "OpenSHC/Global.func.hpp" +#include "OpenSHC/IO/LowLevelMemory.func.hpp" +#include "OpenSHC/Map.func.hpp" +#include "OpenSHC/Map/Buildings/BuildingsState.func.hpp" +#include "OpenSHC/Map/Entities/EntityState.func.hpp" +#include "OpenSHC/Map/LandscapeState.func.hpp" +#include "OpenSHC/Map/Navigation/PathFindingState.func.hpp" +#include "OpenSHC/Map/Units/TribesState.func.hpp" +#include "OpenSHC/Map/Units/UnitsState.func.hpp" +#include "OpenSHC/Rendering/ViewportRenderState.func.hpp" +#include "OpenSHC/UI/MinimapViewState.func.hpp" +#include "OpenSHC/Map/MapConstants.hpp" +#include "OpenSHC/WindowsHelper/Enums/BOOLEnum.hpp" + +#include "OpenSHC/Globals/DAT_AICState.hpp" +#include "OpenSHC/Globals/DAT_BuildingsState.hpp" +#include "OpenSHC/Globals/DAT_EntityState.hpp" +#include "OpenSHC/Globals/DAT_GameCore.hpp" +#include "OpenSHC/Globals/DAT_GameState.hpp" +#include "OpenSHC/Globals/DAT_LandscapeState.hpp" +#include "OpenSHC/Globals/DAT_LowLevelMemory.hpp" +#include "OpenSHC/Globals/DAT_MinimapViewState.hpp" +#include "OpenSHC/Globals/DAT_PathFindingState.hpp" +#include "OpenSHC/Globals/DAT_TileMapState.hpp" +#include "OpenSHC/Globals/DAT_TribesState.hpp" +#include "OpenSHC/Globals/DAT_UnitsState.hpp" +#include "OpenSHC/Globals/DAT_ViewportRenderState.hpp" +#include "OpenSHC/Globals/DAT_WallAndPitchState.hpp" +#include "OpenSHC/Globals/DAT_WindowAndDirectDraw.hpp" +#include "OpenSHC/Globals/GMFirstPictureIndex.hpp" + +namespace OpenSHC { +namespace Map { + + using OpenSHC::Map::LogicHelpers::L_BORDER; + using OpenSHC::Map::LogicHelpers::L_BORDER_EDGE; + + // FUNCTION: STRONGHOLDCRUSADER 0x00512100 + void TileMapState::setupAllMapSections() + { + this->temporaryTerrainTypeIndex = 0; + this->SEC_Section1052 = 0; + this->SEC_Section1053 = 0; + this->SEC_Section1054 = 0; + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::setupTileMapSections, this)(); + DAT_WallAndPitchState::instance.countdown = 0; + MACRO_CALL_MEMBER(OpenSHC::Rendering::ViewportRenderState_Func::setTileSystemMemoryLookupArrays, + DAT_ViewportRenderState::ptr)(); + MACRO_CALL_MEMBER(OpenSHC::Rendering::ViewportRenderState_Func::tweakValidTilesToExcludeMapBorders, + DAT_ViewportRenderState::ptr)(); + MACRO_CALL_MEMBER(OpenSHC::Rendering::ViewportRenderState_Func::setupViewport, DAT_ViewportRenderState::ptr)( + 0, 0, DAT_WindowAndDirectDraw::instance.resolutionX, DAT_WindowAndDirectDraw::instance.resolutionY - 128); + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::setupLogicalMapBorders, this)(); + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::updateShowHiLayerOrResetChangedLayer, this)(); + + for (int tile = 0; tile < countof(this->LogicLayer); tile++) { + if (this->LogicLayer[tile] == L_BORDER) { + this->GfxLayer[tile] = GMFirstPictureIndex::instance[2]; + } else if (this->LogicLayer[tile] == L_BORDER_EDGE) { + this->GfxLayer[tile] = GMFirstPictureIndex::instance[2] + 1; + } else { + this->GfxLayer[tile] = GMFirstPictureIndex::instance[2] + 2; + } + } + + MACRO_CALL_MEMBER(OpenSHC::IO::LowLevelMemory_Func::fillMemory_ShortValue, DAT_LowLevelMemory::ptr)( + MC_MAP_TILES_COUNT, 1, this->PillarGFXLayer); + MACRO_CALL_MEMBER(OpenSHC::IO::LowLevelMemory_Func::fillMemory_ByteValue, DAT_LowLevelMemory::ptr)( + sizeof(this->HeightLayer), 8, this->HeightLayer); + MACRO_CALL_MEMBER(OpenSHC::IO::LowLevelMemory_Func::fillMemory_ByteValue, DAT_LowLevelMemory::ptr)( + sizeof(this->DefaultHeightLayer), 8, this->DefaultHeightLayer); + DAT_PathFindingState::instance.searchGeneration = 1; + MACRO_CALL_MEMBER(OpenSHC::IO::LowLevelMemory_Func::fillMemory_ByteValue, DAT_LowLevelMemory::ptr)( + sizeof(DAT_TileMapState::instance.WalkLayer), 0, DAT_TileMapState::instance.WalkLayer); + MACRO_CALL_MEMBER(OpenSHC::Map::Navigation::PathFindingState_Func::setChangedLayerZeroBasedOn40x40Layer, + DAT_PathFindingState::ptr)(0); + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::updateLogicalTileMapRelatedSections, this)(); + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::updateGfxLayer, this)(); + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::updateGFXLayers, this)(); + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::resetMoatArray, this)(); + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::resetPitchDitchArray, this)(); + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::toggleFlatView, this)(0); + DAT_TileMapState::instance.field_0x5548d0 = 0; + DAT_TileMapState::instance.flatViewToggleValue2 = 0; + MACRO_CALL_MEMBER(OpenSHC::Map::Buildings::BuildingsState_Func::clearBuildings, DAT_BuildingsState::ptr)(); + MACRO_CALL_MEMBER(OpenSHC::Map::LandscapeState_Func::clearRocksAndTrees, DAT_LandscapeState::ptr)(); + MACRO_CALL_MEMBER(OpenSHC::Map::LandscapeState_Func::resetWind, DAT_LandscapeState::ptr)(); + ((void(__fastcall*)(OpenSHC::Map::Units::UnitsState*))MACRO_CALL(OpenSHC::Global_Func::DoNothing))( + DAT_UnitsState::ptr); + MACRO_CALL_MEMBER(OpenSHC::Map::Units::UnitsState_Func::clearAllUnits, DAT_UnitsState::ptr)(); + MACRO_CALL_MEMBER(OpenSHC::Map::Units::TribesState_Func::clearAllTribes, DAT_TribesState::ptr)(); + MACRO_CALL_MEMBER(OpenSHC::Game::GameStateStructures_Func::clearMapAndTimeAndPlayerData, DAT_GameState::ptr)(); + MACRO_CALL_MEMBER( + OpenSHC::Game::GameStateStructures_Func::resetVariousCountsAndStatisticsAndStartGoodsAndResources, + DAT_GameState::ptr)(); + MACRO_CALL_MEMBER( + OpenSHC::Game::GameStateStructures_Func::clearCurrentResourcesAndStrongWalls, DAT_GameState::ptr)(); + MACRO_CALL_MEMBER( + OpenSHC::Game::GameStateStructures_Func::initializeGameStateAfterMapLoad, DAT_GameState::ptr)(); + MACRO_CALL_MEMBER(OpenSHC::Game::GameStateStructures_Func::fillWith0xFF, DAT_GameState::ptr)(); + MACRO_CALL_MEMBER(OpenSHC::Map::Units::TribesState_Func::clearAnimalSpawnLocationsUnk, DAT_TribesState::ptr)(); + + for (int tribe = 0; tribe < countof(DAT_AICState::instance.tribeIDArray); tribe++) { + DAT_AICState::instance.tribeIDArray[tribe] = 0; + DAT_AICState::instance.tribeUIDArray[tribe] = 0; + } + ((void(__fastcall*)(OpenSHC::Map::Units::UnitsState*))MACRO_CALL(OpenSHC::Global_Func::DoNothing))( + DAT_UnitsState::ptr); + ((void(__fastcall*)(OpenSHC::Map::LandscapeState*))MACRO_CALL(OpenSHC::Global_Func::DoNothing))( + DAT_LandscapeState::ptr); + MACRO_CALL_MEMBER( + OpenSHC::Map::Entities::EntityState_Func::clearEntityArrayAndSeagullArray, DAT_EntityState::ptr)(); + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::setChangedLayerToThreeAndMapping0x40x40, this)(); + this->forceUpdateLogicalAndMiscDisplayLayers = TRUE; + this->forceUpdateTextureTilemap = TRUE; + this->forceUpdateGFXLayers = TRUE; + this->forceUpdateMacroLayerFlag = TRUE; + this->field_0x55487c = 200; + MACRO_CALL_MEMBER( + OpenSHC::Map::Navigation::PathFindingState_Func::updatePathLinkageLayerForEachBuildingAtEachTile, + DAT_PathFindingState::ptr)(); + MACRO_CALL_MEMBER( + OpenSHC::Map::Navigation::PathFindingState_Func::updateSeparateAreaTileMap, DAT_PathFindingState::ptr)(1); + this->forceUpdateMacroLayerFlag = TRUE; + this->field_0x55487c = 500; + MACRO_CALL_MEMBER( + OpenSHC::UI::MinimapViewState_Func::setTileColorsDependingOnMapSize, DAT_MinimapViewState::ptr)(0, 100); + MACRO_CALL_MEMBER( + OpenSHC::UI::MinimapViewState_Func::setMapPropertyDependingOnMapSize, DAT_MinimapViewState::ptr)(0, 100); + DAT_GameCore::instance.currentlyInGameUnk_0xa4 = FALSE; + MACRO_CALL(OpenSHC::Map_Func::ResetSomeValuesFunctionUnk)(); + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/setupBuildingSizeIndexMapping.cpp b/src/OpenSHC/Map/TileMapState/setupBuildingSizeIndexMapping.cpp new file mode 100644 index 0000000000..6a5414abd3 --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/setupBuildingSizeIndexMapping.cpp @@ -0,0 +1,26 @@ +#include "../TileMapState.func.hpp" + +namespace OpenSHC { +namespace Map { + + // FUNCTION: STRONGHOLDCRUSADER 0x00503720 + void TileMapState::setupBuildingSizeIndexMapping() + { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::setupBuildingSizeIndexMappingForBuildingWithSize, this)(0); + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::setupBuildingSizeIndexMappingForBuildingWithSize, this)(1); + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::setupBuildingSizeIndexMappingForBuildingWithSize, this)(2); + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::setupBuildingSizeIndexMappingForBuildingWithSize, this)(3); + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::setupBuildingSizeIndexMappingForBuildingWithSize, this)(4); + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::setupBuildingSizeIndexMappingForBuildingWithSize, this)(5); + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::setupBuildingSizeIndexMappingForBuildingWithSize, this)(6); + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::setupBuildingSizeIndexMappingForBuildingWithSize, this)(7); + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::setupBuildingSizeIndexMappingForBuildingWithSize, this)(8); + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::setupBuildingSizeIndexMappingForBuildingWithSize, this)(9); + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::setupBuildingSizeIndexMappingForBuildingWithSize, this)(10); + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::setupBuildingSizeIndexMappingForBuildingWithSize, this)(11); + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::setupBuildingSizeIndexMappingForBuildingWithSize, this)(12); + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::setupBuildingSizeIndexMappingForBuildingWithSize, this)(13); + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/setupLogicalMapBorders.cpp b/src/OpenSHC/Map/TileMapState/setupLogicalMapBorders.cpp new file mode 100644 index 0000000000..2d1f303cba --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/setupLogicalMapBorders.cpp @@ -0,0 +1,45 @@ +#include "../TileMapState.func.hpp" + +#include "OpenSHC/Map/MapConstants.hpp" + +namespace OpenSHC { +namespace Map { + + using OpenSHC::Map::LogicHelpers::L_BORDER; + using OpenSHC::Map::LogicHelpers::L_BORDER_EDGE; + + // FUNCTION: STRONGHOLDCRUSADER 0x004F6C60 + void TileMapState::setupLogicalMapBorders() + { + int rowWidth = 0; + int index = 0; + for (; rowWidth < MC_MAP_XY_LIMIT; rowWidth += 2) { + this->LogicLayer[index] = L_BORDER; + index++; + if (rowWidth > 0) { + this->LogicLayer[index] = L_BORDER_EDGE; + } + index += rowWidth; + if (rowWidth > 0) { + this->LogicLayer[index - 1] = L_BORDER_EDGE; + } + this->LogicLayer[index] = L_BORDER; + index++; + } + for (rowWidth = MC_MAP_XY_LIMIT - 2; rowWidth >= 0; rowWidth -= 2) { + this->LogicLayer[index] = L_BORDER; + index++; + if (rowWidth > 0) { + this->LogicLayer[index] = L_BORDER_EDGE; + } + index += rowWidth; + if (rowWidth > 0) { + this->LogicLayer[index - 1] = L_BORDER_EDGE; + } + this->LogicLayer[index] = L_BORDER; + index++; + } + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/setupTileMapSections.cpp b/src/OpenSHC/Map/TileMapState/setupTileMapSections.cpp new file mode 100644 index 0000000000..30099d5573 --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/setupTileMapSections.cpp @@ -0,0 +1,84 @@ +#include "../TileMapState.func.hpp" + +#include "OpenSHC/IO/LowLevelMemory.func.hpp" +#include "OpenSHC/Random/RNG.func.hpp" +#include "OpenSHC/Map/Enums/CardinalDirection.hpp" + +#include "OpenSHC/Globals/DAT_LowLevelMemory.hpp" +#include "OpenSHC/Globals/SEC_RNG.hpp" + +namespace OpenSHC { +namespace Map { + + // FUNCTION: STRONGHOLDCRUSADER 0x00511DF0 + void TileMapState::setupTileMapSections() + { + MACRO_CALL_MEMBER(OpenSHC::IO::LowLevelMemory_Func::fillMemory_ByteValue, DAT_LowLevelMemory::ptr)( + sizeof(this->GfxLayer), 0, this->GfxLayer); + MACRO_CALL_MEMBER(OpenSHC::IO::LowLevelMemory_Func::fillMemory_ByteValue, DAT_LowLevelMemory::ptr)( + sizeof(this->AlphaGFXLayer), 0, this->AlphaGFXLayer); + MACRO_CALL_MEMBER(OpenSHC::IO::LowLevelMemory_Func::fillMemory_ByteValue, DAT_LowLevelMemory::ptr)( + sizeof(this->ConstructionGFXLayer), 0, this->ConstructionGFXLayer); + MACRO_CALL_MEMBER(OpenSHC::IO::LowLevelMemory_Func::fillMemory_ByteValue, DAT_LowLevelMemory::ptr)( + sizeof(this->PillarGFXLayer), 0, this->PillarGFXLayer); + MACRO_CALL_MEMBER(OpenSHC::IO::LowLevelMemory_Func::fillMemory_ByteValue, DAT_LowLevelMemory::ptr)( + sizeof(this->WallGFXLayer), 0, this->WallGFXLayer); + MACRO_CALL_MEMBER(OpenSHC::IO::LowLevelMemory_Func::fillMemory_ByteValue, DAT_LowLevelMemory::ptr)( + sizeof(this->FloatingLayer), 0, this->FloatingLayer); + MACRO_CALL_MEMBER(OpenSHC::IO::LowLevelMemory_Func::fillMemory_ByteValue, DAT_LowLevelMemory::ptr)( + sizeof(this->UnitLayer), 0, this->UnitLayer); + MACRO_CALL_MEMBER(OpenSHC::IO::LowLevelMemory_Func::fillMemory_ByteValue, DAT_LowLevelMemory::ptr)( + sizeof(this->EntityLayer), 0, this->EntityLayer); + MACRO_CALL_MEMBER(OpenSHC::IO::LowLevelMemory_Func::fillMemory_ByteValue, DAT_LowLevelMemory::ptr)( + sizeof(this->EntityLayerLT25), 0, this->EntityLayerLT25); + MACRO_CALL_MEMBER(OpenSHC::IO::LowLevelMemory_Func::fillMemory_ByteValue, DAT_LowLevelMemory::ptr)( + sizeof(this->BuildingLayer), 0, this->BuildingLayer); + MACRO_CALL_MEMBER(OpenSHC::IO::LowLevelMemory_Func::fillMemory_ByteValue, DAT_LowLevelMemory::ptr)( + sizeof(this->LogicLayer), 0, this->LogicLayer); + MACRO_CALL_MEMBER(OpenSHC::IO::LowLevelMemory_Func::fillMemory_ByteValue, DAT_LowLevelMemory::ptr)( + sizeof(this->Logic2Layer), 0, this->Logic2Layer); + MACRO_CALL_MEMBER(OpenSHC::IO::LowLevelMemory_Func::fillMemory_ByteValue, DAT_LowLevelMemory::ptr)( + sizeof(this->ChangedLayer), 0, this->ChangedLayer); + MACRO_CALL_MEMBER(OpenSHC::IO::LowLevelMemory_Func::fillMemory_ByteValue, DAT_LowLevelMemory::ptr)( + sizeof(this->OrganismLayer), 0, this->OrganismLayer); + MACRO_CALL_MEMBER(OpenSHC::IO::LowLevelMemory_Func::fillMemory_ByteValue, DAT_LowLevelMemory::ptr)( + sizeof(this->DamageLayer), 0, this->DamageLayer); + MACRO_CALL_MEMBER(OpenSHC::IO::LowLevelMemory_Func::fillMemory_ByteValue, DAT_LowLevelMemory::ptr)( + sizeof(this->MacroLayer), 0, this->MacroLayer); + MACRO_CALL_MEMBER(OpenSHC::IO::LowLevelMemory_Func::fillMemory_ByteValue, DAT_LowLevelMemory::ptr)( + sizeof(this->HeightLayer), 0, this->HeightLayer); + MACRO_CALL_MEMBER(OpenSHC::IO::LowLevelMemory_Func::fillMemory_ByteValue, DAT_LowLevelMemory::ptr)( + sizeof(this->DefaultHeightLayer), 0, this->DefaultHeightLayer); + MACRO_CALL_MEMBER(OpenSHC::IO::LowLevelMemory_Func::fillMemory_ByteValue, DAT_LowLevelMemory::ptr)( + sizeof(this->ShowHiLayer), 0, this->ShowHiLayer); + MACRO_CALL_MEMBER(OpenSHC::IO::LowLevelMemory_Func::fillMemory_ByteValue, DAT_LowLevelMemory::ptr)( + sizeof(this->MiscDisplayLayer), 0, this->MiscDisplayLayer); + MACRO_CALL_MEMBER(OpenSHC::IO::LowLevelMemory_Func::fillMemory_ByteValue, DAT_LowLevelMemory::ptr)( + sizeof(this->LuminesenceLayer), 0, this->LuminesenceLayer); + MACRO_CALL_MEMBER(OpenSHC::IO::LowLevelMemory_Func::fillMemory_ByteValue, DAT_LowLevelMemory::ptr)( + sizeof(this->WallOwnerLayer), 0, this->WallOwnerLayer); + MACRO_CALL_MEMBER(OpenSHC::IO::LowLevelMemory_Func::fillMemory_ByteValue, DAT_LowLevelMemory::ptr)( + sizeof(this->BuildingWasLayer), 0, this->BuildingWasLayer); + MACRO_CALL_MEMBER(OpenSHC::IO::LowLevelMemory_Func::fillMemory_ByteValue, DAT_LowLevelMemory::ptr)( + sizeof(this->OccupancyLayer), 0, this->OccupancyLayer); + MACRO_CALL_MEMBER(OpenSHC::IO::LowLevelMemory_Func::fillMemory_ByteValue, DAT_LowLevelMemory::ptr)( + sizeof(this->AIZoneLayer), 0, this->AIZoneLayer); + MACRO_CALL_MEMBER(OpenSHC::IO::LowLevelMemory_Func::fillMemory_ByteValue, DAT_LowLevelMemory::ptr)( + sizeof(this->AIInfoLayer), 0, this->AIInfoLayer); + MACRO_CALL_MEMBER(OpenSHC::IO::LowLevelMemory_Func::fillMemory_ByteValue, DAT_LowLevelMemory::ptr)( + sizeof(this->unitDeathHeatMap), 0, this->unitDeathHeatMap); + MACRO_CALL_MEMBER(OpenSHC::IO::LowLevelMemory_Func::fillMemory_ByteValue, DAT_LowLevelMemory::ptr)( + sizeof(this->SEC_TileMap1104), 0, this->SEC_TileMap1104); + MACRO_CALL_MEMBER(OpenSHC::IO::LowLevelMemory_Func::fillMemory_ByteValue, DAT_LowLevelMemory::ptr)( + sizeof(this->SEC_PathfindingCostTileMap1105), 0, this->SEC_PathfindingCostTileMap1105); + + for (int tile = 0; tile < countof(this->RandomLayer); tile++) { + MACRO_CALL_MEMBER(OpenSHC::Random::RNG_Func::nextRandomNumber2, SEC_RNG::ptr)(); + this->RandomLayer[tile] = SEC_RNG::instance.currentNumber2; + } + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::changeMapOrientation, this)(OpenSHC::Map::Enums::MO_NORTH); + this->DAT_FutureMapOrientation = OpenSHC::Map::Enums::MO_UPPER_LIMIT; + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/spawnEraserTileEffect.cpp b/src/OpenSHC/Map/TileMapState/spawnEraserTileEffect.cpp new file mode 100644 index 0000000000..99d1a8f677 --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/spawnEraserTileEffect.cpp @@ -0,0 +1,63 @@ +#include "../TileMapState.func.hpp" + +#include "OpenSHC/Map/Entities/EntityState.func.hpp" +#include "OpenSHC/Map/Navigation/PathFindingState.func.hpp" +#include "OpenSHC/UI/MinimapViewState.func.hpp" +#include "OpenSHC/Map/Entities/EntityType.hpp" +#include "OpenSHC/Map/LogicHelpers/MiscDisplay.hpp" + +#include "OpenSHC/Globals/DAT_EntityState.hpp" +#include "OpenSHC/Globals/DAT_MinimapViewState.hpp" +#include "OpenSHC/Globals/DAT_PathFindingState.hpp" +#include "OpenSHC/Globals/DAT_TileMapState.hpp" +#include "OpenSHC/Globals/DAT_ViewportRenderState.hpp" + +namespace OpenSHC { +namespace Map { + + using OpenSHC::Map::LogicHelpers::L_CRENEL; + using OpenSHC::Map::LogicHelpers::L_CRENEL_VARIATIONUnk; + using OpenSHC::Map::LogicHelpers::L_STAIRS; + using OpenSHC::Map::LogicHelpers::L_UNKNOWN_WALL_RELATED; + using OpenSHC::Map::LogicHelpers::L_WALL_OR_GATEHOUSE; + using OpenSHC::Map::LogicHelpers::MD_DESTROY_PENDING; + + using OpenSHC::Map::Entities::EntityType; + + // FUNCTION: STRONGHOLDCRUSADER 0x004F9F00 + void TileMapState::spawnEraserTileEffect(undefined4 unused, int tile) + { + uint y = DAT_ViewportRenderState::instance.tileTranslationMatrix_YComponent[tile]; + int const x = tile - DAT_ViewportRenderState::instance.translationMatrix[y].addXgetTile; + this->MiscDisplayLayer[tile] &= ~MD_DESTROY_PENDING; + if (this->UnitLayer[tile] != 0) { + return; + } + + this->HeightLayer[tile] = this->DefaultHeightLayer[tile]; + MACRO_CALL_MEMBER(OpenSHC::Map::Entities::EntityState_Func::destroyEntitiesOnTile, DAT_EntityState::ptr)(tile); + this->LogicLayer[tile] + &= ~(L_WALL_OR_GATEHOUSE | L_CRENEL | L_STAIRS | L_UNKNOWN_WALL_RELATED | L_CRENEL_VARIATIONUnk); + int microX + = (tile + - DAT_ViewportRenderState::instance + .translationMatrix[DAT_ViewportRenderState::instance.tileTranslationMatrix_YComponent[tile]] + .addXgetTile) + * 8; + int microY = DAT_ViewportRenderState::instance.tileTranslationMatrix_YComponent[tile] * 8; + MACRO_CALL_MEMBER(OpenSHC::Map::Entities::EntityState_Func::spawnProjectileEntity, DAT_EntityState::ptr)(0, 0, + 0, microX, microY, DAT_TileMapState::instance.DefaultHeightLayer[tile], microX + 1, microY + 1, + DAT_TileMapState::instance.DefaultHeightLayer[tile], (EntityType)30, 0); // EntityType 30 has no name yet + this->BuildingWasLayer[tile] = 0; + this->DamageLayer[tile] = 0; + MACRO_CALL_MEMBER(OpenSHC::Map::Navigation::PathFindingState_Func::updatePathLinkagesInAllEightDirections, + DAT_PathFindingState::ptr)(y, tile); + MACRO_CALL_MEMBER(OpenSHC::Map::Navigation::PathFindingState_Func::updateWalkAndPathLayer, + DAT_PathFindingState::ptr)(9, x, y); + MACRO_CALL_MEMBER(OpenSHC::UI::MinimapViewState_Func::triggerMinimapRedraw, DAT_MinimapViewState::ptr)(1); + DAT_PathFindingState::instance.toggleUpdateSeparateAreaTileMap = 1; + DAT_TileMapState::instance.field_0x554a30 = 1; + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/storeMinAndMaxHeightOfArea.cpp b/src/OpenSHC/Map/TileMapState/storeMinAndMaxHeightOfArea.cpp new file mode 100644 index 0000000000..b9ec740972 --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/storeMinAndMaxHeightOfArea.cpp @@ -0,0 +1,46 @@ +#include "../TileMapState.func.hpp" + +#include "OpenSHC/Map/MapConstants.hpp" + +#include "OpenSHC/Globals/DAT_ViewportRenderState.hpp" + +namespace OpenSHC { +namespace Map { + + // FUNCTION: STRONGHOLDCRUSADER 0x004F9980 + void TileMapState::storeMinAndMaxHeightOfArea(uint x, uint y, int buildingWidthAndHeight) + { + this->buildingMinHeight = 1000; + this->buildingMaxHeight = 0; + if (x > MC_MAP_XY_LIMIT_INCLUSIVE || y > MC_MAP_XY_LIMIT_INCLUSIVE) { + return; + } + if (DAT_ViewportRenderState::instance.DAT_BinaryTileMap400x400[y * MC_MAP_XY_LIMIT + x] == 0) { + return; + } + + int buildingSizeTileIndex = 0; + do { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::getBuildingSizeIndexMappingData, this)( + buildingSizeTileIndex, buildingWidthAndHeight); + uint footprintY = this->buildingY + y; + uint footprintX = this->buildingX + x; + if (footprintX <= MC_MAP_XY_LIMIT_INCLUSIVE && footprintY <= MC_MAP_XY_LIMIT_INCLUSIVE + && DAT_ViewportRenderState::instance.DAT_BinaryTileMap400x400[footprintY * MC_MAP_XY_LIMIT + footprintX] + != 0) { + int const height + = this->HeightLayer[DAT_ViewportRenderState::instance.translationMatrix[footprintY].addXgetTile + + this->buildingX + x]; + if (height > this->buildingMaxHeight) { + this->buildingMaxHeight = height; + } + if (height < this->buildingMinHeight) { + this->buildingMinHeight = height; + } + } + buildingSizeTileIndex++; + } while (buildingSizeTileIndex < this->constructionTileCount); + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/swapMoatOwnership.cpp b/src/OpenSHC/Map/TileMapState/swapMoatOwnership.cpp new file mode 100644 index 0000000000..ae9cf6f89d --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/swapMoatOwnership.cpp @@ -0,0 +1,22 @@ +#include "../TileMapState.func.hpp" + +namespace OpenSHC { +namespace Map { + + // FUNCTION: STRONGHOLDCRUSADER 0x00500790 + void TileMapState::swapMoatOwnership(int playerID, int otherPlayerID) + { + for (int moatID = 1; moatID < countof(this->moats); moatID++) { + if (this->moats[moatID].owner == 0) { + continue; + } + if (this->moats[moatID].owner == playerID) { + this->moats[moatID].owner = otherPlayerID; + } else if (this->moats[moatID].owner == otherPlayerID) { + this->moats[moatID].owner = playerID; + } + } + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/swapPitchOwnership.cpp b/src/OpenSHC/Map/TileMapState/swapPitchOwnership.cpp new file mode 100644 index 0000000000..82d2cc90cb --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/swapPitchOwnership.cpp @@ -0,0 +1,22 @@ +#include "../TileMapState.func.hpp" + +namespace OpenSHC { +namespace Map { + + // FUNCTION: STRONGHOLDCRUSADER 0x00500940 + void TileMapState::swapPitchOwnership(int playerID, int otherPlayerID) + { + for (int pitchDitchID = 1; pitchDitchID < countof(this->pitchDitches); pitchDitchID++) { + if (this->pitchDitches[pitchDitchID].owner == 0) { + continue; + } + if (this->pitchDitches[pitchDitchID].owner == playerID) { + this->pitchDitches[pitchDitchID].owner = otherPlayerID; + } else if (this->pitchDitches[pitchDitchID].owner == otherPlayerID) { + this->pitchDitches[pitchDitchID].owner = playerID; + } + } + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/swapWallOwnership.cpp b/src/OpenSHC/Map/TileMapState/swapWallOwnership.cpp new file mode 100644 index 0000000000..653768a000 --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/swapWallOwnership.cpp @@ -0,0 +1,26 @@ +#include "../TileMapState.func.hpp" + +#include "OpenSHC/Map/LogicHelpers/WallOwner.hpp" + +namespace OpenSHC { +namespace Map { + + using OpenSHC::Map::LogicHelpers::L_WALL_OR_GATEHOUSE; + using OpenSHC::Map::LogicHelpers::WO_OWNER_MASK; + + // FUNCTION: STRONGHOLDCRUSADER 0x004F9220 + void TileMapState::swapWallOwnership(int playerID, int otherPlayerID) + { + for (int tile = 0; tile < countof(this->LogicLayer); tile++) { + if ((this->LogicLayer[tile] & L_WALL_OR_GATEHOUSE) != 0) { + if ((this->WallOwnerLayer[tile] & WO_OWNER_MASK) + 1 == playerID) { + this->WallOwnerLayer[tile] = (this->WallOwnerLayer[tile] & ~WO_OWNER_MASK) | (otherPlayerID - 1); + } else if ((this->WallOwnerLayer[tile] & WO_OWNER_MASK) + 1 == otherPlayerID) { + this->WallOwnerLayer[tile] = (this->WallOwnerLayer[tile] & ~WO_OWNER_MASK) | (playerID - 1); + } + } + } + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/triggerLoweredView.cpp b/src/OpenSHC/Map/TileMapState/triggerLoweredView.cpp new file mode 100644 index 0000000000..77f7332603 --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/triggerLoweredView.cpp @@ -0,0 +1,59 @@ +#include "../TileMapState.func.hpp" + +#include "OpenSHC/Map/Enums/LoweredViewTransition.hpp" +#include "OpenSHC/Map/Units/UnitLogicState.hpp" +#include "OpenSHC/Map/Units/UnitType.hpp" + +#include "OpenSHC/Globals/DAT_GameCore.hpp" +#include "OpenSHC/Globals/DAT_UnitsState.hpp" + +namespace OpenSHC { +namespace Map { + + // FUNCTION: STRONGHOLDCRUSADER 0x004F6FD0 + void TileMapState::triggerLoweredView(OpenSHC::Map::Enums::LoweredViewTransitionInt requestedView) + { + OpenSHC::Map::Enums::LoweredViewTransition view = OpenSHC::Map::Enums::LVT_SET_NORMAL; + if (this->flatViewToggleValue2 == 0) { + view = (OpenSHC::Map::Enums::LoweredViewTransition)requestedView; + } + if (this->refreshCertainTileMap_old == view) { + return; + } + this->refreshCertainTileMap = view; + this->counter1 = 0; + if (view != OpenSHC::Map::Enums::LVT_LOWER) { + return; + } + if (DAT_GameCore::instance.gamePausedLogical != 0) { + return; + } + + for (int unitID = 1; unitID < DAT_UnitsState::instance.maxUnitCount; unitID++) { + if (DAT_UnitsState::instance.units[unitID].logicalState == OpenSHC::Map::Units::ULS_INVISIBLE) { + continue; + } + if (DAT_UnitsState::instance.units[unitID].unitType != OpenSHC::Map::Units::UT_DRUNK + && DAT_UnitsState::instance.units[unitID].unitType != OpenSHC::Map::Units::UT_RABBIT + && DAT_UnitsState::instance.units[unitID].unitType != OpenSHC::Map::Units::UT_HUNTERDOG + && DAT_UnitsState::instance.units[unitID].unitType != OpenSHC::Map::Units::UT_JESTER + && DAT_UnitsState::instance.units[unitID].unitType != OpenSHC::Map::Units::UT_CHICKEN + && DAT_UnitsState::instance.units[unitID].unitType != OpenSHC::Map::Units::UT_CHILD + && DAT_UnitsState::instance.units[unitID].unitType != OpenSHC::Map::Units::UT_JUGGLER + && DAT_UnitsState::instance.units[unitID].unitType != OpenSHC::Map::Units::UT_FIREEATER) { + continue; + } + + int height = (DAT_UnitsState::instance.units[unitID].buildingHeight + + DAT_UnitsState::instance.units[unitID].terrainOrClimbHeight) + / 10; + if (height > 14) { + height = 14; + } + DAT_UnitsState::instance.units[unitID].heightDiv10 = height; + DAT_UnitsState::instance.units[unitID].field_0x83 = 0; + } + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/unmarkBuildingFootprintFlag.cpp b/src/OpenSHC/Map/TileMapState/unmarkBuildingFootprintFlag.cpp new file mode 100644 index 0000000000..c6da6b3904 --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/unmarkBuildingFootprintFlag.cpp @@ -0,0 +1,25 @@ +#include "../TileMapState.func.hpp" + +#include "OpenSHC/Globals/DAT_ViewportRenderState.hpp" + +namespace OpenSHC { +namespace Map { + + using OpenSHC::Map::LogicHelpers::L_PLAIN1_AND_FARM; + + // FUNCTION: STRONGHOLDCRUSADER 0x004FAD20 + void TileMapState::unmarkBuildingFootprintFlag(int x, int y, int buildingSize) + { + int buildingSizeTileIndex = 0; + do { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::getBuildingSizeIndexMappingData, this)( + buildingSizeTileIndex, buildingSize); + buildingSizeTileIndex++; + int tile = DAT_ViewportRenderState::instance.translationMatrix[this->buildingY + y].addXgetTile + + this->buildingX + x; + this->LogicLayer[tile] &= ~L_PLAIN1_AND_FARM; + } while (buildingSizeTileIndex < this->constructionTileCount); + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/updateAreaBasedOnSurrounding.cpp b/src/OpenSHC/Map/TileMapState/updateAreaBasedOnSurrounding.cpp new file mode 100644 index 0000000000..4948baadab --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/updateAreaBasedOnSurrounding.cpp @@ -0,0 +1,32 @@ +#include "../TileMapState.func.hpp" + +#include "OpenSHC/Map/Buildings/BuildingsState.func.hpp" + +#include "OpenSHC/Globals/DAT_BuildingsState.hpp" +#include "OpenSHC/Globals/DAT_ViewportRenderState.hpp" + +namespace OpenSHC { +namespace Map { + + // FUNCTION: STRONGHOLDCRUSADER 0x004FAA30 + void TileMapState::updateAreaBasedOnSurrounding(int x, int y, int buildingSize) + { + int buildingSizeTileIndex = 0; + int area + = MACRO_CALL_MEMBER(OpenSHC::Map::Buildings::BuildingsState_Func::findAccessibleAreaNearBuildingLocation, + DAT_BuildingsState::ptr)(x, y, buildingSize); + + do { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::getBuildingSizeIndexMappingData, this)( + buildingSizeTileIndex, buildingSize); + int tileUnk = DAT_ViewportRenderState::instance.translationMatrix[this->buildingY + y].addXgetTile + + this->buildingX + x; + if (this->PathConnectionLayer[tileUnk] == 0) { + this->PathConnectionLayer[tileUnk] = area; + } + buildingSizeTileIndex++; + } while (buildingSizeTileIndex < this->constructionTileCount); + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/updateGameRelatedValue.cpp b/src/OpenSHC/Map/TileMapState/updateGameRelatedValue.cpp new file mode 100644 index 0000000000..62afd7f731 --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/updateGameRelatedValue.cpp @@ -0,0 +1,36 @@ +#include "../TileMapState.func.hpp" + +#include "OpenSHC/Map/Navigation/PathFindingState.func.hpp" + +#include "OpenSHC/Globals/DAT_GameState.hpp" +#include "OpenSHC/Globals/DAT_PathFindingState.hpp" + +namespace OpenSHC { +namespace Map { + + // FUNCTION: STRONGHOLDCRUSADER 0x00511850 + void TileMapState::updateGameRelatedValue() + { + if (DAT_GameState::instance.mapAndTime.gameEventRelatedCountdown == 0) { + return; + } + + if (DAT_GameState::instance.mapAndTime.gameEventRelatedCountdown == 40) { + this->field_0x554a10 = -1; + } + DAT_GameState::instance.mapAndTime.gameEventRelatedCountdown -= 1; + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::setSignpostDistanceForCampaignMission, this)(); + int signpostDistance = ((40 - DAT_GameState::instance.mapAndTime.gameEventRelatedCountdown) + * DAT_GameState::instance.mapAndTime.unk_signpostDistance) + / 40; + if (signpostDistance == this->field_0x554a10) { + return; + } + + this->field_0x554a10 = signpostDistance; + MACRO_CALL_MEMBER(OpenSHC::Map::Navigation::PathFindingState_Func::pathfindingUpdate_0x4a8ab0, + DAT_PathFindingState::ptr)(signpostDistance); + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/updateMacroLayer.cpp b/src/OpenSHC/Map/TileMapState/updateMacroLayer.cpp new file mode 100644 index 0000000000..0ce6a927d5 --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/updateMacroLayer.cpp @@ -0,0 +1,39 @@ +#include "../TileMapState.func.hpp" + +namespace OpenSHC { +namespace Map { + + using OpenSHC::Map::LogicHelpers::L2_BEACH; + using OpenSHC::Map::LogicHelpers::L2_EARTH_AND_STONES; + using OpenSHC::Map::LogicHelpers::L2_NONE; + using OpenSHC::Map::LogicHelpers::L2_OASIS_GRASS; + using OpenSHC::Map::LogicHelpers::L2_PLATEAU_HIGH; + using OpenSHC::Map::LogicHelpers::L2_PLATEAU_MEDIUM; + using OpenSHC::Map::LogicHelpers::L2_SCRUB; + using OpenSHC::Map::LogicHelpers::L_DEFAULT_EARTH_OR_TEXTURE; + using OpenSHC::Map::LogicHelpers::L_MARSH; + using OpenSHC::Map::LogicHelpers::L_NONE; + + // FUNCTION: STRONGHOLDCRUSADER 0x00511020 + void TileMapState::updateMacroLayer() + { + if (this->forceUpdateMacroLayerFlag <= 0) { + return; + } + + this->forceUpdateMacroLayerFlag = FALSE; + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::updateMacroLayerRelated, this)( + L_DEFAULT_EARTH_OR_TEXTURE, L2_SCRUB, 0); + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::updateMacroLayerRelated, this)(L_MARSH, L2_NONE, 0); + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::updateMacroLayerRelated, this)( + L_NONE, L2_EARTH_AND_STONES, 0); + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::updateMacroLayerRelated, this)(L_NONE, L2_OASIS_GRASS, 0); + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::updateMacroLayerRelated, this)(L_NONE, L2_BEACH, 0); + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::updateMacroLayerRelated, this)(L_NONE, L2_PLATEAU_MEDIUM, 0); + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::updateMacroLayerRelated, this)(L_NONE, L2_PLATEAU_HIGH, 0); + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::updateMacroLayerRelated2, this)(); + this->forceUpdateTextureTilemap = TRUE; + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/updatePathLinkagesForBuilding.cpp b/src/OpenSHC/Map/TileMapState/updatePathLinkagesForBuilding.cpp new file mode 100644 index 0000000000..92caea6403 --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/updatePathLinkagesForBuilding.cpp @@ -0,0 +1,76 @@ +#include "../TileMapState.func.hpp" + +#include "OpenSHC/Map/Navigation/PathFindingState.func.hpp" +#include "OpenSHC/Map/Buildings/BuildingType.hpp" + +#include "OpenSHC/Globals/DAT_BuildingsState.hpp" +#include "OpenSHC/Globals/DAT_PathFindingState.hpp" +#include "OpenSHC/Globals/DAT_ViewportRenderState.hpp" + +namespace OpenSHC { +namespace Map { + + // FUNCTION: STRONGHOLDCRUSADER 0x00506AD0 + void TileMapState::updatePathLinkagesForBuilding(int buildingID) + { + int buildingSizeTileIndex = 0; + + do { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::getBuildingSizeIndexMappingData, this)( + buildingSizeTileIndex, DAT_BuildingsState::instance.buildings[buildingID].widthOrHeight); + int tileUnk = DAT_ViewportRenderState::instance + .translationMatrix[DAT_BuildingsState::instance.buildings[buildingID].y + this->buildingY] + .addXgetTile + + DAT_BuildingsState::instance.buildings[buildingID].x + this->buildingX; + MACRO_CALL_MEMBER(OpenSHC::Map::Navigation::PathFindingState_Func::updatePathLinkagesInAllEightDirections, + DAT_PathFindingState::ptr)( + DAT_BuildingsState::instance.buildings[buildingID].y + this->buildingY, tileUnk); + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::clearMoat, this)(tileUnk); + buildingSizeTileIndex++; + } while (buildingSizeTileIndex < this->constructionTileCount); + + short buildingType = DAT_BuildingsState::instance.buildings[buildingID].buildingType; + if (buildingType == OpenSHC::Map::Buildings::BT_GATEHOUSELARGE) { + MACRO_CALL_MEMBER(OpenSHC::Map::Navigation::PathFindingState_Func::updatePathLinkageTileMapRelatedToGates, + DAT_PathFindingState::ptr)(buildingID); + return; + } + if (buildingType == OpenSHC::Map::Buildings::BT_GATEHOUSESMALL) { + MACRO_CALL_MEMBER(OpenSHC::Map::Navigation::PathFindingState_Func::updatePathLinkageTileMapRelatedToGates, + DAT_PathFindingState::ptr)(buildingID); + return; + } + if (buildingType == OpenSHC::Map::Buildings::BT_WOODGATE1) { + MACRO_CALL_MEMBER(OpenSHC::Map::Navigation::PathFindingState_Func::updatePathLinkageTileMapRelatedToGates, + DAT_PathFindingState::ptr)(buildingID); + return; + } + if (buildingType == OpenSHC::Map::Buildings::BT_STONEKEEP) { + MACRO_CALL_MEMBER(OpenSHC::Map::Navigation::PathFindingState_Func::updatePathLinkageTileMapRelatedToKeeps, + DAT_PathFindingState::ptr)(buildingID); + return; + } + if (buildingType == OpenSHC::Map::Buildings::BT_STRONGHOLD) { + MACRO_CALL_MEMBER(OpenSHC::Map::Navigation::PathFindingState_Func::updatePathLinkageTileMapRelatedToKeeps, + DAT_PathFindingState::ptr)(buildingID); + return; + } + if (buildingType == OpenSHC::Map::Buildings::BT_KEEPFOUR) { + MACRO_CALL_MEMBER(OpenSHC::Map::Navigation::PathFindingState_Func::updatePathLinkageTileMapRelatedToKeeps, + DAT_PathFindingState::ptr)(buildingID); + return; + } + if (buildingType == OpenSHC::Map::Buildings::BT_KEEPFIVE) { + MACRO_CALL_MEMBER(OpenSHC::Map::Navigation::PathFindingState_Func::updatePathLinkageTileMapRelatedToKeeps, + DAT_PathFindingState::ptr)(buildingID); + return; + } + if (buildingType == OpenSHC::Map::Buildings::BT_SIEGETOWER_PLACED) { + MACRO_CALL_MEMBER( + OpenSHC::Map::Navigation::PathFindingState_Func::updatePathLinkageTileMapRelatedToSiegeTower, + DAT_PathFindingState::ptr)(buildingID); + } + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/updatePitchDitches.cpp b/src/OpenSHC/Map/TileMapState/updatePitchDitches.cpp new file mode 100644 index 0000000000..22b2ad26a1 --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/updatePitchDitches.cpp @@ -0,0 +1,76 @@ +#include "../TileMapState.func.hpp" + +#include "OpenSHC/Map/Entities.func.hpp" + +#include "OpenSHC/Globals/DAT_GameState.hpp" +#include "OpenSHC/Globals/DAT_TerrainDefinedData.hpp" +#include "OpenSHC/Globals/DAT_TileMapState.hpp" +#include "OpenSHC/Globals/DAT_ViewportRenderState.hpp" + +namespace OpenSHC { +namespace Map { + + using OpenSHC::Map::LogicHelpers::L_PLAIN2_AND_PITCH; + + // FUNCTION: STRONGHOLDCRUSADER 0x005114C0 + void TileMapState::updatePitchDitches() + { + if (DAT_GameState::instance.gameTicksLoadBalancer % 10 == 3) { + this->maxPitchDitchCount = 0; + for (int id = 1; id < countof(this->pitchDitches); id++) { + if (this->pitchDitches[id].owner != 0) { + this->maxPitchDitchCount = id + 1; + } + } + } + + for (int id = 1; id < this->maxPitchDitchCount; id++) { + if (this->pitchDitches[id].owner == 0) { + continue; + } + int tile = this->pitchDitches[id].tile; + if ((this->LogicLayer[tile] & L_PLAIN2_AND_PITCH) == 0 || this->pitchDitches[id].state == PDS_REMOVE) { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::destroyPitchDitch, this)(id); + } else if (this->pitchDitches[id].state == PDS_PLACED) { + this->pitchDitches[id].state += 1; + this->pitchDitches[id].burnTimer = 0; + } else if (this->pitchDitches[id].state == PDS_BURNING) { + this->pitchDitches[id].burnTimer += 1; + if (this->pitchDitches[id].burnTimer == 20) { + this->pitchDitches[id].state += 1; + this->pitchDitches[id].burnTimer = 0; + int height = DAT_TileMapState::instance.HeightLayer[tile]; + for (int i = 0; i < countof(DAT_TerrainDefinedData::instance.field_0x376c); i++) { + int neighbour = DAT_ViewportRenderState::instance + .translationMatrix[this->pitchDitches[id].y + + DAT_TerrainDefinedData::instance.field_0x376c[i].y] + .addXgetTile + + this->pitchDitches[id].x + DAT_TerrainDefinedData::instance.field_0x376c[i].x; + if ((this->LogicLayer[neighbour] & L_PLAIN2_AND_PITCH) == 0) { + continue; + } + int other = MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::getPitchDitchIDForTile, this)( + neighbour); + if (other == 0 || this->pitchDitches[other].state != PDS_READY) { + continue; + } + if (abs(height - DAT_TileMapState::instance.HeightLayer[neighbour]) < 25) { + MACRO_CALL(OpenSHC::Map::Entities_Func::SetPlaceOnFire)(this->pitchDitches[other].owner, + this->pitchDitches[other].x * 8, this->pitchDitches[other].y * 8, + DAT_TileMapState::instance.HeightLayer[this->pitchDitches[other].tile], 3); + this->pitchDitches[other].state = PDS_BURNING; + } + } + } + } else if (this->pitchDitches[id].state == PDS_BURNT_OUT) { + this->pitchDitches[id].burnTimer += 1; + if (this->pitchDitches[id].burnTimer == 200) { + DAT_TileMapState::instance.HeightLayer[tile] = DAT_TileMapState::instance.DefaultHeightLayer[tile]; + this->pitchDitches[id].state = PDS_REMOVE; + } + } + } + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/updateShowHiLayerOrResetChangedLayer.cpp b/src/OpenSHC/Map/TileMapState/updateShowHiLayerOrResetChangedLayer.cpp new file mode 100644 index 0000000000..1171cae30a --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/updateShowHiLayerOrResetChangedLayer.cpp @@ -0,0 +1,94 @@ +#include "../TileMapState.func.hpp" + +#include "OpenSHC/Map/Enums/LoweredViewTransition.hpp" + +#include "OpenSHC/Globals/DAT_RotateMapOrPullDownTerrain.hpp" + +namespace OpenSHC { +namespace Map { + + using OpenSHC::Map::Enums::LVT_LOWER; + using OpenSHC::Map::Enums::LVT_NONE; + using OpenSHC::Map::Enums::LVT_RAISE; + using OpenSHC::Map::Enums::LVT_SET_LOWERED; + using OpenSHC::Map::Enums::LVT_SET_NORMAL; + + // FUNCTION: STRONGHOLDCRUSADER 0x00501A20 + void TileMapState::updateShowHiLayerOrResetChangedLayer() + { + BOOLEnum importHeight = FALSE; + this->refreshRelatedTwo = 0; + if (this->refreshCertainTileMap == LVT_NONE) { + return; + } + + this->counter1 += 1; + this->refreshCertainTileMap_old = this->refreshCertainTileMap; + this->refreshRelatedOne = 0; + for (int row = 0; row < countof(this->heightBasedScreenYOffset); row++) { + int const loweredOffset = row / 4 + 1; + int step; + if (this->refreshCertainTileMap == LVT_LOWER) { + step = this->heightBasedScreenYOffset[row] - loweredOffset; + } else if (this->refreshCertainTileMap == LVT_RAISE) { + step = row - this->heightBasedScreenYOffset[row]; + } else { + step = 0; + } + if (step > 60) { + step = 30; + } else if (step > 40) { + step = 20; + } else if (step > 30) { + step = 14; + } else if (step > 20) { + step = 8; + } else if (step > 10) { + step = 4; + } else if (step > 6) { + step = 3; + } else if (step > 2) { + step = 2; + } else if (step > 0) { + step = 1; + } else { + step = 0; + } + + if (this->refreshCertainTileMap == LVT_SET_LOWERED) { + this->heightBasedScreenYOffset[row] = loweredOffset; + } else if (this->refreshCertainTileMap == LVT_SET_NORMAL) { + this->heightBasedScreenYOffset[row] = row; + } else if (this->refreshCertainTileMap == LVT_LOWER) { + this->heightBasedScreenYOffset[row] -= step; + if (this->heightBasedScreenYOffset[row] > loweredOffset) { + importHeight = TRUE; + } + } else if (this->refreshCertainTileMap == LVT_RAISE) { + this->heightBasedScreenYOffset[row] += step; + if (this->heightBasedScreenYOffset[row] != row) { + importHeight = TRUE; + } + } + } + + this->refreshRelatedTwo = 1; + if (!importHeight) { + if (this->refreshCertainTileMap == LVT_RAISE || this->refreshCertainTileMap == LVT_SET_NORMAL) { + this->refreshRelatedOne = 1; + } else { + this->refreshRelatedOne = 0; + this->refreshRelatedTwo = 2; + } + this->refreshCertainTileMap = LVT_NONE; + this->forceUpdateLogicalAndMiscDisplayLayers = TRUE; + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::setChangedLayerToThreeAndMapping0x40x40, this)(); + this->counter1 = 0; + DAT_RotateMapOrPullDownTerrain::instance = 1; + return; + } + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::rebuildShowHiLayerFromHeights, this)(); + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/upgradeMapFormatLogicLayer.cpp b/src/OpenSHC/Map/TileMapState/upgradeMapFormatLogicLayer.cpp new file mode 100644 index 0000000000..ee74ccb5d0 --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/upgradeMapFormatLogicLayer.cpp @@ -0,0 +1,44 @@ +#include "../TileMapState.func.hpp" + +#include "OpenSHC/Map/Version.func.hpp" +#include "OpenSHC/Map/MapConstants.hpp" + +namespace OpenSHC { +namespace Map { + + // FUNCTION: STRONGHOLDCRUSADER 0x00511D70 + void TileMapState::upgradeMapFormatLogicLayer( + PackagedFileMagicNum receivedMapVersion, PackagedFileMagicNum packagerMapVersion) + { + MACRO_CALL(OpenSHC::Map::Version_Func::UpgradeMapLogicToVersion_Unknown1)(); + MACRO_CALL(OpenSHC::Map::Version_Func::UpgradeMapLogicToVersion_Unknown2)(); + MACRO_CALL(OpenSHC::Map::Version_Func::UpgradeMapLogicToVersion_Unknown3)(); + if (this->mapSize == 0) { + this->mapSize = MC_MAP_XY_LIMIT; + } + if (receivedMapVersion == packagerMapVersion) { + return; + } + + if (receivedMapVersion < 102) { + MACRO_CALL(OpenSHC::Map::Version_Func::UpgradeMapLogicToVersion_102)(); + } + if (receivedMapVersion < 120) { + MACRO_CALL(OpenSHC::Map::Version_Func::UpgradeMapLogicToVersion_120)(); + } + if (receivedMapVersion < 125) { + MACRO_CALL(OpenSHC::Map::Version_Func::UpgradeMapLogicToVersion_125)(); + } + if (receivedMapVersion < 128) { + MACRO_CALL(OpenSHC::Map::Version_Func::UpgradeMapLogicToVersion_128)(); + } + if (receivedMapVersion < 145) { + MACRO_CALL(OpenSHC::Map::Version_Func::UpgradeMapLogicToVersion_145)(); + } + if (receivedMapVersion < 147) { + MACRO_CALL(OpenSHC::Map::Version_Func::UpgradeMapLogicToVersion_147)(); + } + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/validateBuildingPlacementAtTile.cpp b/src/OpenSHC/Map/TileMapState/validateBuildingPlacementAtTile.cpp new file mode 100644 index 0000000000..07727e108c --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/validateBuildingPlacementAtTile.cpp @@ -0,0 +1,63 @@ +#include "../TileMapState.func.hpp" + +#include "OpenSHC/Map/Buildings/BuildingType.hpp" +#include "OpenSHC/Map/LogicHelpers/WallOwner.hpp" +#include "OpenSHC/WindowsHelper/Enums/BOOLEnum.hpp" + +#include "OpenSHC/Globals/DAT_BuildingsState.hpp" +#include "OpenSHC/Globals/DAT_ViewportRenderState.hpp" + +namespace OpenSHC { +namespace Map { + + using OpenSHC::Map::LogicHelpers::L_BUILDING; + using OpenSHC::Map::LogicHelpers::L_KEEP_NON_MANOR_HOUSE; + using OpenSHC::Map::LogicHelpers::L_WALL_OR_GATEHOUSE; + using OpenSHC::Map::LogicHelpers::WO_OWNER_MASK; + + // FUNCTION: STRONGHOLDCRUSADER 0x004F9DF0 + void TileMapState::validateBuildingPlacementAtTile(int playerID, int x, int y) + { + this->buildingPlacementFail = FALSE; + int tile = DAT_ViewportRenderState::instance.translationMatrix[y].addXgetTile + x; + int buildingID = this->BuildingLayer[tile]; + if (buildingID <= 0 + || (DAT_BuildingsState::instance.buildings[buildingID].owner != playerID && playerID != 0)) { + this->buildingPlacementFail = TRUE; + } + if ((this->LogicLayer[tile] & L_WALL_OR_GATEHOUSE) != 0 && this->BuildingLayer[tile] == 0) { + if ((this->WallOwnerLayer[tile] & WO_OWNER_MASK) + 1 != playerID) { + return; + } + this->buildingPlacementFail = FALSE; + } + if ((this->LogicLayer[tile] & (L_BUILDING | L_KEEP_NON_MANOR_HOUSE)) == 0 + && DAT_BuildingsState::instance.buildings[buildingID].buildingType + != OpenSHC::Map::Buildings::BT_GATEHOUSELARGE + && DAT_BuildingsState::instance.buildings[buildingID].buildingType + != OpenSHC::Map::Buildings::BT_GATEHOUSESMALL + && DAT_BuildingsState::instance.buildings[buildingID].buildingType != OpenSHC::Map::Buildings::BT_WOODGATE1 + && DAT_BuildingsState::instance.buildings[buildingID].buildingType != OpenSHC::Map::Buildings::BT_WOODGATE2 + && DAT_BuildingsState::instance.buildings[buildingID].buildingType != OpenSHC::Map::Buildings::BT_KEEPDOOR + && DAT_BuildingsState::instance.buildings[buildingID].buildingType != OpenSHC::Map::Buildings::BT_DRAWBRIDGE + && DAT_BuildingsState::instance.buildings[buildingID].buildingType != OpenSHC::Map::Buildings::BT_CAMPGROUND + && DAT_BuildingsState::instance.buildings[buildingID].buildingType + != OpenSHC::Map::Buildings::BT_PARADEGROUND + && DAT_BuildingsState::instance.buildings[buildingID].buildingType + != OpenSHC::Map::Buildings::BT_PARADEGROUND2 + && DAT_BuildingsState::instance.buildings[buildingID].buildingType + != OpenSHC::Map::Buildings::BT_PARADEGROUND3 + && DAT_BuildingsState::instance.buildings[buildingID].buildingType + != OpenSHC::Map::Buildings::BT_PARADEGROUND4 + && DAT_BuildingsState::instance.buildings[buildingID].buildingType + != OpenSHC::Map::Buildings::BT_PARADEGROUND5 + && DAT_BuildingsState::instance.buildings[buildingID].buildingType != OpenSHC::Map::Buildings::BT_CAMPFIRE + && DAT_BuildingsState::instance.buildings[buildingID].buildingType + != OpenSHC::Map::Buildings::BT_KILLINGPIT) { + this->buildingPlacementFail = TRUE; + } + this->cursorTargetID = buildingID; + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/validateWallPlacementAtTile.cpp b/src/OpenSHC/Map/TileMapState/validateWallPlacementAtTile.cpp new file mode 100644 index 0000000000..bdc2007eac --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/validateWallPlacementAtTile.cpp @@ -0,0 +1,67 @@ +#include "../TileMapState.func.hpp" + +#include "OpenSHC/Map/Navigation/PathFindingState.func.hpp" +#include "OpenSHC/Map/Units/UnitLogicState.hpp" +#include "OpenSHC/WindowsHelper/Enums/BOOLEnum.hpp" + +#include "OpenSHC/Globals/DAT_PathFindingState.hpp" +#include "OpenSHC/Globals/DAT_UnitsState.hpp" +#include "OpenSHC/Globals/DAT_ViewportRenderState.hpp" + +namespace OpenSHC { +namespace Map { + + using OpenSHC::Map::LogicHelpers::L_BORDER; + using OpenSHC::Map::LogicHelpers::L_BUILDING; + using OpenSHC::Map::LogicHelpers::L_FORD; + using OpenSHC::Map::LogicHelpers::L_MARSH; + using OpenSHC::Map::LogicHelpers::L_MOAT; + using OpenSHC::Map::LogicHelpers::L_PLAIN1_AND_FARM; + using OpenSHC::Map::LogicHelpers::L_RIVER; + using OpenSHC::Map::LogicHelpers::L_ROCKY; + using OpenSHC::Map::LogicHelpers::L_SEA; + using OpenSHC::Map::LogicHelpers::L_WALL_OR_GATEHOUSE; + + // FUNCTION: STRONGHOLDCRUSADER 0x004F94A0 + void TileMapState::validateWallPlacementAtTile(int playerID, uint x, uint y, short mapperCommand) + { + this->illegalBuild = TRUE; + int tile = DAT_ViewportRenderState::instance.translationMatrix[y].addXgetTile + x; + uint logic = this->LogicLayer[tile]; + int const building = this->BuildingLayer[tile]; + if ((logic & L_WALL_OR_GATEHOUSE) != 0 && this->DamageLayer[tile] < 12) { + return; + } + if ((logic & L_BUILDING) != 0) { + return; + } + if (building != 0) { + return; + } + if ((logic & (L_PLAIN1_AND_FARM | L_BORDER)) != 0) { + return; + } + if ((logic & (L_SEA | L_ROCKY | L_RIVER | L_FORD | L_MARSH | L_MOAT)) != 0) { + return; + } + if (MACRO_CALL_MEMBER(OpenSHC::Map::Navigation::PathFindingState_Func::isEnemyTooCloseUnk, + DAT_PathFindingState::ptr)(playerID, x, y, 5)) { + return; + } + if (MACRO_CALL_MEMBER(OpenSHC::Map::Navigation::PathFindingState_Func::findSomeSuitableLocationUnk, + DAT_PathFindingState::ptr)(playerID, x, y, 2)) { + return; + } + + if (this->UnitLayer[tile] != 0 && (this->LogicLayer[tile] & L_WALL_OR_GATEHOUSE) == 0) { + int const unitID = this->UnitLayer[tile]; + if (mapperCommand == OpenSHC::Commands::M_MAPPER_CRENAL + || mapperCommand == OpenSHC::Commands::M_MAPPER_CRENAL2) { + DAT_UnitsState::instance.units[unitID].logicalState = OpenSHC::Map::Units::ULS_REMOVE; + } + } + this->illegalBuild = FALSE; + } + +} +} diff --git a/src/OpenSHC/UI/MenuItems/MapEditorLandscaping/MenuItemActionHandler_MapEditorLandscaping_GeneralButtons.cpp b/src/OpenSHC/UI/MenuItems/MapEditorLandscaping/MenuItemActionHandler_MapEditorLandscaping_GeneralButtons.cpp index 4882016c87..8c18ece75c 100644 --- a/src/OpenSHC/UI/MenuItems/MapEditorLandscaping/MenuItemActionHandler_MapEditorLandscaping_GeneralButtons.cpp +++ b/src/OpenSHC/UI/MenuItems/MapEditorLandscaping/MenuItemActionHandler_MapEditorLandscaping_GeneralButtons.cpp @@ -79,8 +79,8 @@ namespace UI { return; // each rock steps the brush up to the next odd size; one shared round-up for the five loses the match case OpenSHC::Commands::M_MAPPER_BIGROCK1: - DAT_TileMapState::instance.editorRockType = 1; - DAT_TileMapState::instance.rockOrientation = 0; + DAT_TileMapState::instance.editorRockType = OpenSHC::Map::Rocks::Enums::ERT_BIGROCK1; + DAT_TileMapState::instance.rockOrientation = OpenSHC::Map::Enums::MO_NORTH; DAT_TileMapState::instance.currentMapperCommand = command; if (DAT_TileMapState::instance.editorActiveBrush == 2) { DAT_TileMapState::instance.editorActiveBrush = 3; @@ -91,8 +91,8 @@ namespace UI { } return; case OpenSHC::Commands::M_MAPPER_BIGROCK2: - DAT_TileMapState::instance.editorRockType = 2; - DAT_TileMapState::instance.rockOrientation = 2; + DAT_TileMapState::instance.editorRockType = OpenSHC::Map::Rocks::Enums::ERT_BIGROCK2; + DAT_TileMapState::instance.rockOrientation = OpenSHC::Map::Enums::MO_EAST; DAT_TileMapState::instance.currentMapperCommand = command; if (DAT_TileMapState::instance.editorActiveBrush == 2) { DAT_TileMapState::instance.editorActiveBrush = 3; @@ -103,8 +103,8 @@ namespace UI { } return; case OpenSHC::Commands::M_MAPPER_BIGROCK3: - DAT_TileMapState::instance.editorRockType = 3; - DAT_TileMapState::instance.rockOrientation = 4; + DAT_TileMapState::instance.editorRockType = OpenSHC::Map::Rocks::Enums::ERT_BIGROCK3; + DAT_TileMapState::instance.rockOrientation = OpenSHC::Map::Enums::MO_SOUTH; DAT_TileMapState::instance.currentMapperCommand = command; if (DAT_TileMapState::instance.editorActiveBrush == 2) { DAT_TileMapState::instance.editorActiveBrush = 3; @@ -115,8 +115,8 @@ namespace UI { } return; case OpenSHC::Commands::M_MAPPER_BIGROCK4: - DAT_TileMapState::instance.editorRockType = 4; - DAT_TileMapState::instance.rockOrientation = 6; + DAT_TileMapState::instance.editorRockType = OpenSHC::Map::Rocks::Enums::ERT_BIGROCK4; + DAT_TileMapState::instance.rockOrientation = OpenSHC::Map::Enums::MO_WEST; DAT_TileMapState::instance.currentMapperCommand = command; if (DAT_TileMapState::instance.editorActiveBrush == 2) { DAT_TileMapState::instance.editorActiveBrush = 3; @@ -127,8 +127,8 @@ namespace UI { } return; case OpenSHC::Commands::M_MAPPER_BIGROCK5: - DAT_TileMapState::instance.editorRockType = 5; - DAT_TileMapState::instance.rockOrientation = 0; + DAT_TileMapState::instance.editorRockType = OpenSHC::Map::Rocks::Enums::ERT_BIGROCK5; + DAT_TileMapState::instance.rockOrientation = OpenSHC::Map::Enums::MO_NORTH; DAT_TileMapState::instance.currentMapperCommand = command; if (DAT_TileMapState::instance.editorActiveBrush == 2) { DAT_TileMapState::instance.editorActiveBrush = 3; diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index 3a97b43162..06784349bc 100644 --- a/status/addresses-SHC-3BB0A8C1.txt +++ b/status/addresses-SHC-3BB0A8C1.txt @@ -38739,25 +38739,25 @@ SHC_3BB0A8C1_0x004F6A70 | 100.0% | Reimplemented SHC_3BB0A8C1_0x004F6A80 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x004F6AE0 | 0.0% | Pending +SHC_3BB0A8C1_0x004F6AE0 | 100.0% | Reimplemented SHC_3BB0A8C1_0x004F6B20 | 0.0% | Pending SHC_3BB0A8C1_0x004F6C20 | 0.0% | Pending -SHC_3BB0A8C1_0x004F6C60 | 0.0% | Pending +SHC_3BB0A8C1_0x004F6C60 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x004F6CF0 | 0.0% | Pending +SHC_3BB0A8C1_0x004F6CF0 | 100.0% | Reimplemented SHC_3BB0A8C1_0x004F6DC0 | 0.0% | Pending -SHC_3BB0A8C1_0x004F6F90 | 0.0% | Pending +SHC_3BB0A8C1_0x004F6F90 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x004F6FD0 | 0.0% | Pending +SHC_3BB0A8C1_0x004F6FD0 | 100.0% | Reimplemented SHC_3BB0A8C1_0x004F70B0 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x004F70D0 | 0.0% | Pending +SHC_3BB0A8C1_0x004F70D0 | 100.0% | Reimplemented SHC_3BB0A8C1_0x004F70E0 | 100.0% | Reimplemented @@ -38767,7 +38767,7 @@ SHC_3BB0A8C1_0x004F7140 | 0.0% | Pending SHC_3BB0A8C1_0x004F7170 | 0.0% | Pending -SHC_3BB0A8C1_0x004F79D0 | 0.0% | Pending +SHC_3BB0A8C1_0x004F79D0 | 100.0% | Reimplemented SHC_3BB0A8C1_0x004F7A80 | 0.0% | Pending @@ -38787,13 +38787,13 @@ SHC_3BB0A8C1_0x004F80D4 | 0.0% | Pending SHC_3BB0A8C1_0x004F80D8 | 0.0% | Pending -SHC_3BB0A8C1_0x004F80E0 | 0.0% | Pending +SHC_3BB0A8C1_0x004F80E0 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x004F8160 | 0.0% | Pending +SHC_3BB0A8C1_0x004F8160 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x004F81D0 | 0.0% | Pending +SHC_3BB0A8C1_0x004F81D0 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x004F8210 | 0.0% | Pending +SHC_3BB0A8C1_0x004F8210 | 100.0% | Reimplemented SHC_3BB0A8C1_0x004F8288 | 0.0% | Pending @@ -38995,7 +38995,7 @@ SHC_3BB0A8C1_0x004F82F0 | 0.0% | Pending SHC_3BB0A8C1_0x004F82F1 | 0.0% | Pending -SHC_3BB0A8C1_0x004F8300 | 0.0% | Pending +SHC_3BB0A8C1_0x004F8300 | 100.0% | Reimplemented SHC_3BB0A8C1_0x004F8454 | 0.0% | Pending @@ -39355,43 +39355,43 @@ SHC_3BB0A8C1_0x004F8523 | 0.0% | Pending SHC_3BB0A8C1_0x004F8524 | 0.0% | Pending -SHC_3BB0A8C1_0x004F8530 | 0.0% | Pending +SHC_3BB0A8C1_0x004F8530 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x004F85B0 | 0.0% | Pending +SHC_3BB0A8C1_0x004F85B0 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x004F8640 | 0.0% | Pending +SHC_3BB0A8C1_0x004F8640 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x004F86D0 | 0.0% | Pending +SHC_3BB0A8C1_0x004F86D0 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x004F8780 | 0.0% | Pending +SHC_3BB0A8C1_0x004F8780 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x004F8840 | 0.0% | Pending +SHC_3BB0A8C1_0x004F8840 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x004F8900 | 0.0% | Pending +SHC_3BB0A8C1_0x004F8900 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x004F89C0 | 0.0% | Pending +SHC_3BB0A8C1_0x004F89C0 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x004F8A40 | 0.0% | Pending +SHC_3BB0A8C1_0x004F8A40 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x004F8AC0 | 0.0% | Pending +SHC_3BB0A8C1_0x004F8AC0 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x004F8B50 | 0.0% | Pending +SHC_3BB0A8C1_0x004F8B50 | 100.0% | Reimplemented SHC_3BB0A8C1_0x004F8BD0 | 0.0% | Pending SHC_3BB0A8C1_0x004F9000 | 0.0% | Pending -SHC_3BB0A8C1_0x004F9220 | 0.0% | Pending +SHC_3BB0A8C1_0x004F9220 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x004F93E0 | 0.0% | Pending +SHC_3BB0A8C1_0x004F93E0 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x004F94A0 | 0.0% | Pending +SHC_3BB0A8C1_0x004F94A0 | 100.0% | Reimplemented SHC_3BB0A8C1_0x004F9590 | 0.0% | Pending SHC_3BB0A8C1_0x004F9630 | 0.0% | Pending -SHC_3BB0A8C1_0x004F9880 | 0.0% | Pending +SHC_3BB0A8C1_0x004F9880 | 100.0% | Reimplemented SHC_3BB0A8C1_0x004F990C | 0.0% | Pending @@ -39407,9 +39407,9 @@ SHC_3BB0A8C1_0x004F9920 | 0.0% | Pending SHC_3BB0A8C1_0x004F9924 | 0.0% | Pending -SHC_3BB0A8C1_0x004F9930 | 0.0% | Pending +SHC_3BB0A8C1_0x004F9930 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x004F9980 | 0.0% | Pending +SHC_3BB0A8C1_0x004F9980 | 100.0% | Reimplemented SHC_3BB0A8C1_0x004F99D0 | 0.0% | Pending @@ -39649,17 +39649,17 @@ SHC_3BB0A8C1_0x004F9DE9 | 0.0% | Pending SHC_3BB0A8C1_0x004F9DEA | 0.0% | Pending -SHC_3BB0A8C1_0x004F9DF0 | 0.0% | Pending +SHC_3BB0A8C1_0x004F9DF0 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x004F9F00 | 0.0% | Pending +SHC_3BB0A8C1_0x004F9F00 | 100.0% | Reimplemented SHC_3BB0A8C1_0x004FA000 | 0.0% | Pending SHC_3BB0A8C1_0x004FA2D0 | 0.0% | Pending -SHC_3BB0A8C1_0x004FA460 | 0.0% | Pending +SHC_3BB0A8C1_0x004FA460 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x004FA550 | 0.0% | Pending +SHC_3BB0A8C1_0x004FA550 | 100.0% | Reimplemented SHC_3BB0A8C1_0x004FA5DC | 0.0% | Pending @@ -40347,23 +40347,23 @@ SHC_3BB0A8C1_0x004FA760 | 0.0% | Pending SHC_3BB0A8C1_0x004FA8D0 | 0.0% | Pending -SHC_3BB0A8C1_0x004FA9D0 | 0.0% | Pending +SHC_3BB0A8C1_0x004FA9D0 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x004FAA30 | 0.0% | Pending +SHC_3BB0A8C1_0x004FAA30 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x004FAAB0 | 0.0% | Pending +SHC_3BB0A8C1_0x004FAAB0 | 100.0% | Reimplemented SHC_3BB0A8C1_0x004FAAE0 | 0.0% | Pending -SHC_3BB0A8C1_0x004FAB70 | 0.0% | Pending +SHC_3BB0A8C1_0x004FAB70 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x004FAC70 | 0.0% | Pending +SHC_3BB0A8C1_0x004FAC70 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x004FAD20 | 0.0% | Pending +SHC_3BB0A8C1_0x004FAD20 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x004FAD70 | 0.0% | Pending +SHC_3BB0A8C1_0x004FAD70 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x004FAE50 | 0.0% | Pending +SHC_3BB0A8C1_0x004FAE50 | 100.0% | Reimplemented SHC_3BB0A8C1_0x004FAEE0 | 0.0% | Pending @@ -40565,13 +40565,13 @@ SHC_3BB0A8C1_0x004FB354 | 0.0% | Pending SHC_3BB0A8C1_0x004FB358 | 0.0% | Pending -SHC_3BB0A8C1_0x004FB360 | 0.0% | Pending +SHC_3BB0A8C1_0x004FB360 | 100.0% | Reimplemented SHC_3BB0A8C1_0x004FB3F0 | 0.0% | Pending SHC_3BB0A8C1_0x004FB4D0 | 0.0% | Pending -SHC_3BB0A8C1_0x004FB770 | 0.0% | Pending +SHC_3BB0A8C1_0x004FB770 | 100.0% | Reimplemented SHC_3BB0A8C1_0x004FB7D0 | 0.0% | Pending @@ -40583,7 +40583,7 @@ SHC_3BB0A8C1_0x004FB990 | 0.0% | Pending SHC_3BB0A8C1_0x004FB9E0 | 0.0% | Pending -SHC_3BB0A8C1_0x004FBC30 | 0.0% | Pending +SHC_3BB0A8C1_0x004FBC30 | 100.0% | Reimplemented SHC_3BB0A8C1_0x004FBC6C | 0.0% | Pending @@ -41077,7 +41077,7 @@ SHC_3BB0A8C1_0x004FBD6F | 0.0% | Pending SHC_3BB0A8C1_0x004FBD70 | 0.0% | Pending -SHC_3BB0A8C1_0x004FBD80 | 0.0% | Pending +SHC_3BB0A8C1_0x004FBD80 | 100.0% | Reimplemented SHC_3BB0A8C1_0x004FBE70 | 0.0% | Pending @@ -41085,15 +41085,15 @@ SHC_3BB0A8C1_0x004FC0B0 | 0.0% | Pending SHC_3BB0A8C1_0x004FC280 | 0.0% | Pending -SHC_3BB0A8C1_0x004FC340 | 0.0% | Pending +SHC_3BB0A8C1_0x004FC340 | 100.0% | Reimplemented SHC_3BB0A8C1_0x004FC490 | 0.0% | Pending -SHC_3BB0A8C1_0x004FC650 | 0.0% | Pending +SHC_3BB0A8C1_0x004FC650 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x004FC7C0 | 0.0% | Pending +SHC_3BB0A8C1_0x004FC7C0 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x004FC810 | 0.0% | Pending +SHC_3BB0A8C1_0x004FC810 | 100.0% | Reimplemented SHC_3BB0A8C1_0x004FC9E0 | 0.0% | Pending @@ -41259,7 +41259,7 @@ SHC_3BB0A8C1_0x004FFB02 | 0.0% | Pending SHC_3BB0A8C1_0x004FFB03 | 0.0% | Pending -SHC_3BB0A8C1_0x004FFB10 | 0.0% | Pending +SHC_3BB0A8C1_0x004FFB10 | 100.0% | Reimplemented SHC_3BB0A8C1_0x004FFBA0 | 0.0% | Pending @@ -41421,55 +41421,55 @@ SHC_3BB0A8C1_0x00500091 | 0.0% | Pending SHC_3BB0A8C1_0x005000A0 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x005000E0 | 0.0% | Pending +SHC_3BB0A8C1_0x005000E0 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x00500180 | 0.0% | Pending +SHC_3BB0A8C1_0x00500180 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00500210 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x00500250 | 0.0% | Pending +SHC_3BB0A8C1_0x00500250 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x00500290 | 0.0% | Pending +SHC_3BB0A8C1_0x00500290 | 100.0% | Reimplemented SHC_3BB0A8C1_0x005002D0 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x00500310 | 0.0% | Pending +SHC_3BB0A8C1_0x00500310 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x00500370 | 0.0% | Pending +SHC_3BB0A8C1_0x00500370 | 100.0% | Reimplemented SHC_3BB0A8C1_0x005003D0 | 0.0% | Pending -SHC_3BB0A8C1_0x00500500 | 0.0% | Pending +SHC_3BB0A8C1_0x00500500 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x005005F0 | 0.0% | Pending +SHC_3BB0A8C1_0x005005F0 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00500640 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00500680 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x005006C0 | 0.0% | Pending +SHC_3BB0A8C1_0x005006C0 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00500720 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00500750 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x00500790 | 0.0% | Pending +SHC_3BB0A8C1_0x00500790 | 100.0% | Reimplemented SHC_3BB0A8C1_0x005007D0 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x00500800 | 0.0% | Pending +SHC_3BB0A8C1_0x00500800 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x00500850 | 0.0% | Pending +SHC_3BB0A8C1_0x00500850 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x00500890 | 0.0% | Pending +SHC_3BB0A8C1_0x00500890 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x00500940 | 0.0% | Pending +SHC_3BB0A8C1_0x00500940 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x00500980 | 0.0% | Pending +SHC_3BB0A8C1_0x00500980 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x00500B50 | 0.0% | Pending +SHC_3BB0A8C1_0x00500B50 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x00500BB0 | 0.0% | Pending +SHC_3BB0A8C1_0x00500BB0 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00500C20 | 100.0% | Reimplemented @@ -41543,9 +41543,9 @@ SHC_3BB0A8C1_0x005017A0 | 100.0% | Reimplemented SHC_3BB0A8C1_0x005017C0 | 0.0% | Pending -SHC_3BB0A8C1_0x00501A20 | 0.0% | Pending +SHC_3BB0A8C1_0x00501A20 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x00501B90 | 0.0% | Pending +SHC_3BB0A8C1_0x00501B90 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00501CB0 | 0.0% | Pending @@ -41561,7 +41561,7 @@ SHC_3BB0A8C1_0x00501CC4 | 0.0% | Pending SHC_3BB0A8C1_0x00501CC8 | 0.0% | Pending -SHC_3BB0A8C1_0x00501CD0 | 0.0% | Pending +SHC_3BB0A8C1_0x00501CD0 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00501D90 | 0.0% | Pending @@ -41577,7 +41577,7 @@ SHC_3BB0A8C1_0x005024F0 | 0.0% | Pending SHC_3BB0A8C1_0x00502680 | 0.0% | Pending -SHC_3BB0A8C1_0x00502950 | 0.0% | Pending +SHC_3BB0A8C1_0x00502950 | 100.0% | Reimplemented SHC_3BB0A8C1_0x005029D0 | 0.0% | Pending @@ -41651,9 +41651,9 @@ SHC_3BB0A8C1_0x00503491 | 0.0% | Pending SHC_3BB0A8C1_0x00503492 | 0.0% | Pending -SHC_3BB0A8C1_0x005034A0 | 0.0% | Pending +SHC_3BB0A8C1_0x005034A0 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x00503720 | 0.0% | Pending +SHC_3BB0A8C1_0x00503720 | 100.0% | Reimplemented SHC_3BB0A8C1_0x005037B0 | 0.0% | Pending @@ -42083,7 +42083,7 @@ SHC_3BB0A8C1_0x005060C0 | 0.0% | Pending SHC_3BB0A8C1_0x00506370 | 0.0% | Pending -SHC_3BB0A8C1_0x00506AD0 | 0.0% | Pending +SHC_3BB0A8C1_0x00506AD0 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00506BD0 | 0.0% | Pending @@ -42099,7 +42099,7 @@ SHC_3BB0A8C1_0x00507130 | 0.0% | Pending SHC_3BB0A8C1_0x00507280 | 0.0% | Pending -SHC_3BB0A8C1_0x00507350 | 0.0% | Pending +SHC_3BB0A8C1_0x00507350 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00507408 | 0.0% | Pending @@ -42161,7 +42161,7 @@ SHC_3BB0A8C1_0x00508590 | 0.0% | Pending SHC_3BB0A8C1_0x00508760 | 0.0% | Pending -SHC_3BB0A8C1_0x00508870 | 0.0% | Pending +SHC_3BB0A8C1_0x00508870 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00508910 | 0.0% | Pending @@ -44173,23 +44173,23 @@ SHC_3BB0A8C1_0x00511018 | 0.0% | Pending SHC_3BB0A8C1_0x0051101C | 0.0% | Pending -SHC_3BB0A8C1_0x00511020 | 0.0% | Pending +SHC_3BB0A8C1_0x00511020 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x005110B0 | 0.0% | Pending +SHC_3BB0A8C1_0x005110B0 | 100.0% | Reimplemented SHC_3BB0A8C1_0x005111D0 | 0.0% | Pending SHC_3BB0A8C1_0x00511240 | 0.0% | Pending -SHC_3BB0A8C1_0x005113F0 | 0.0% | Pending +SHC_3BB0A8C1_0x005113F0 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x005114C0 | 0.0% | Pending +SHC_3BB0A8C1_0x005114C0 | 100.0% | Reimplemented SHC_3BB0A8C1_0x005115C0 | 0.0% | Pending SHC_3BB0A8C1_0x005116E0 | 0.0% | Pending -SHC_3BB0A8C1_0x00511850 | 0.0% | Pending +SHC_3BB0A8C1_0x00511850 | 100.0% | Reimplemented SHC_3BB0A8C1_0x005118C0 | 100.0% | Reimplemented @@ -44723,19 +44723,19 @@ SHC_3BB0A8C1_0x00511D60 | 0.0% | Pending SHC_3BB0A8C1_0x00511D64 | 0.0% | Pending -SHC_3BB0A8C1_0x00511D70 | 0.0% | Pending +SHC_3BB0A8C1_0x00511D70 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x00511DF0 | 0.0% | Pending +SHC_3BB0A8C1_0x00511DF0 | 100.0% | Reimplemented SHC_3BB0A8C1_0x005120C0 | 0.0% | Pending -SHC_3BB0A8C1_0x00512100 | 0.0% | Pending +SHC_3BB0A8C1_0x00512100 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00512180 | 0.0% | Pending SHC_3BB0A8C1_0x00512450 | 0.0% | Pending -SHC_3BB0A8C1_0x005127B0 | 0.0% | Pending +SHC_3BB0A8C1_0x005127B0 | 100.0% | Reimplemented SHC_3BB0A8C1_0x005127D0 | 0.0% | Pending @@ -44743,7 +44743,7 @@ SHC_3BB0A8C1_0x00512920 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00512940 | 0.0% | Pending -SHC_3BB0A8C1_0x00514480 | 0.0% | Pending +SHC_3BB0A8C1_0x00514480 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00514520 | 0.0% | Pending @@ -44767,13 +44767,13 @@ SHC_3BB0A8C1_0x00515680 | 0.0% | Pending SHC_3BB0A8C1_0x00515740 | 0.0% | Pending -SHC_3BB0A8C1_0x00515A20 | 0.0% | Pending +SHC_3BB0A8C1_0x00515A20 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00515C80 | 0.0% | Pending SHC_3BB0A8C1_0x00515DA0 | 0.0% | Pending -SHC_3BB0A8C1_0x00515F40 | 0.0% | Pending +SHC_3BB0A8C1_0x00515F40 | 100.0% | Reimplemented SHC_3BB0A8C1_0x005160C0 | 0.0% | Pending diff --git a/tools/compiler-internals/color_probe.py b/tools/compiler-internals/color_probe.py new file mode 100644 index 0000000000..88c4bf2b17 --- /dev/null +++ b/tools/compiler-internals/color_probe.py @@ -0,0 +1,231 @@ +"""Dump every global register candidate at each stage of c2's `Color`, for one function. + +usage: color_probe.py FILE.cpp --name FUNC [--shift N] [--watch ID] + color_probe.py --compare A.txt B.txt + +Built to explain why one function compiles differently when only the TU's symbol count changes +(msvc-backend.md, "The TU symbol count decides some register choices"): run it on the committed +file and again with `--shift N` (N dummy `typedef short` lines prepended to a temporary copy, the +same thing shift_sweep.py does), then `--compare` the two outputs. Candidates are matched by +symbol id and name, so the comparison shows which fields differ, not just that the numbers moved. + +Output lines (saved to the probe folder as color---.txt and printed): + + CK cls= checkpoint; every candidate follows as a G line + G num id key c24 c28 c2c c40 c38 f4 L.. N=name + num +0x1c candidate number (the order the greedy passes walk) + id +0x44 symbol id (the colouring tie-break) + key +0x0c colouring key, c40 +0x40 spill benefit, c24 +0x24 + reference count, f4 +0x04 flags (byte +5 bit 4 = never fold) + FV num id f5 .. FoldTooCheapDefs looks at a candidate (number order) + FK1 / FK2 its two veto tests (ComponentKilled, the def check) + FOLD num id ...and folds it into its uses (DoFoldDef) + SPILL num id c24 c40 N=name SpillObvious spills it (number order) + SUBST num id N=name ...and substitutes its def + SERVE / SOD / HCU num id key c40 colouring pops it / SpillOrDefer / HandleCurrUncolorable + CPR id legal=.. ChoosePhysReg is asked for it + CPRDONE id reg=.. w=.. ...and chose that register (1 eax .. 8 edi) with these weights + WATCH / FLAGW with --watch ID: who writes that candidate's flag byte +5 + +Stages: 0 before SubsumeGlobregs, 1 after it, 2 after FoldTooCheapDefs, 3 after +InitialSplitPoints, 4 after Spillcosts, 5 after SpillObvious, 6 after OrderGlobregs, 7/8 the +re-split round (before Spillcosts / after SpillObvious), 9 class done. +""" + +import collections +import os +import re +import sys + +import c2base +from slot_probe import EQ, NL, GLOBAL_REG_ALLOC, COLOR_ENTRY, run, name_test, out + +GLOBREG_HASH = '0x10d15090' # _GlobregHash: 0x400 chains, bucket = number & 0x3ff, next at +0x30 +STAGES = [ + ('0x10b0d0a5', 0), # before SubsumeGlobregs + ('0x10b0d0aa', 1), # after SubsumeGlobregs + ('0x10b0d0bf', 2), # after FoldTooCheapDefs + CreateBlockBvs + ('0x10b0d149', 3), # after InitialSplitPoints + ('0x10b0d15a', 4), # after Spillcosts + ('0x10b0d169', 5), # after SpillObvious + ('0x10b0d22f', 6), # after OrderGlobregs + ('0x10b0d2ab', 7), # re-split round: before Spillcosts + ('0x10b0d2bf', 8), # re-split round: after SpillObvious + ('0x10b0d497', 9), # class done +] + +# one candidate per G line; run with $$>< so the whole walk is one command. $t9 = stage number +DUMP = (r'r $t1=0' '\n' + r'.while (@$t1 < 0x400) { r $t2=poi(' + GLOBREG_HASH + r'+@$t1*4); .while (@$t2 != 0) { ' + r'.printf "G %x num=%x id=%x key=%d c24=%x c28=%x c2c=%x c40=%d c38=%x f4=%x ", @$t9, poi(@$t2+0x1c), ' + r'poi(@$t2+0x44), poi(@$t2+0xc), poi(@$t2+0x24), poi(@$t2+0x28), poi(@$t2+0x2c), poi(@$t2+0x40), ' + r'poi(@$t2+0x38), poi(@$t2+4); .if (poi(@$t2+0x20) != 0) { r $t3=poi(poi(@$t2+0x20)); .while (@$t3 != 0) ' + r'{ .printf "L%x:%x:%x ", poi(@$t3), poi(@$t3+4), poi(@$t3+8); r $t3=poi(@$t3+0xc) } }; r $t4=poi(@$t2); ' + r'.if (@$t4 != 0) { .if (poi(@$t4) != 0) { .if (poi(poi(@$t4)+4) != 0) { .printf "N=%ma", ' + r'poi(poi(@$t4)+4)+1 } } }; .printf "\n"; r $t2=poi(@$t2+0x30) }; r $t1=@$t1+1 }' '\n') + +# the symbol name of the candidate in $t2, for the one-line breakpoints +NAME = ('r $t4=poi(@$t2); .if (@$t4 != 0) { .if (poi(@$t4) != 0) { .if (poi(poi(@$t4)+4) != 0) { .printf ' + + EQ + 'N=%ma' + EQ + ', poi(poi(@$t4)+4)+1 } } }; .printf ' + EQ + NL + EQ) + + +def one(label, reg): + return ('"r $t2=' + reg + '; .printf ' + EQ + label + ' num=%x id=%x key=%d c40=%d' + NL + EQ + + ', poi(@$t2+0x1c), poi(@$t2+0x44), poi(@$t2+0xc), poi(@$t2+0x40); gc"') + + +def breakpoints(dump_path, watch): + dp = str(dump_path).replace('\\', '/') + bps = [(a, '"r $t9=0n%d; .printf ' % n + EQ + 'CK %d cls=%%x' % n + NL + EQ + ', @ebx; $$><' + dp + '; gc"') + for a, n in STAGES] + bps += [ + ('0x10b0d350', one('SERVE', '@ebp')), + ('0x10b0d37e', one('SOD', '@ebp')), + ('0x10b0d3c5', one('HCU', '@ebp')), + ('0x10b0d471', '"r $t2=@ebp; .printf ' + EQ + 'CPR id=%x legal=%x/%x/%x' + NL + EQ + + ', poi(@$t2+0x44), poi(poi(poi(@$t2+0x20))), poi(poi(poi(@$t2+0x20))+4), poi(poi(poi(@$t2+0x20))+8); gc"'), + ('0x10b0d476', '"r $t2=@ebp; .printf ' + EQ + 'CPRDONE id=%x reg=%x w=%d,%d,%d,%d,%d,%d,%d,%d' + NL + EQ + + ', poi(@$t2+0x44), (poi(@$t2+0x10)-0x10d09f70)/0x60, poi(0x10d14f14), poi(0x10d14f18), poi(0x10d14f1c), poi(0x10d14f20), ' + 'poi(0x10d14f24), poi(0x10d14f28), poi(0x10d14f2c), poi(0x10d14f30); gc"'), + # FoldTooCheapDefs phase 2 walks _GlobregHash in number order; esi = candidate + ('0x10b0dd20', '"r $t2=@esi; .printf ' + EQ + 'FV num=%x id=%x f5=%x u14=%x def=%x defop=%x c34=%x' + NL + + EQ + ', poi(@$t2+0x1c), poi(@$t2+0x44), by(@$t2+5), poi(@$t2+0x14), poi(@$t2+0x3c), ' + '(poi(@$t2+0x3c)!=0)*poi(poi(@$t2+0x3c)+4), by(@$t2+0x34); gc"'), + ('0x10b0ddc5', '".printf ' + EQ + 'FK1 %x' + NL + EQ + ', @eax; gc"'), + ('0x10b0dde4', '".printf ' + EQ + 'FK2 %x' + NL + EQ + ', @eax; gc"'), + ('0x10b0de18', '"r $t2=@esi; .printf ' + EQ + 'FOLD num=%x id=%x' + NL + EQ + + ', poi(@$t2+0x1c), poi(@$t2+0x44); gc"'), + # SpillObvious: eax = the candidate at the Spill call and at the CmdSubstDef call + ('0x10b115d6', '"r $t2=@eax; .printf ' + EQ + 'SPILL num=%x id=%x c24=%x c40=%d ' + EQ + + ', poi(@$t2+0x1c), poi(@$t2+0x44), poi(@$t2+0x24), poi(@$t2+0x40); ' + NAME + '; gc"'), + ('0x10b11630', '"r $t2=@eax; .printf ' + EQ + 'SUBST num=%x id=%x ' + EQ + + ', poi(@$t2+0x1c), poi(@$t2+0x44); ' + NAME + '; gc"'), + ] + if watch is not None: + # after SubsumeGlobregs: find the candidate with that symbol id, hardware-watch its flag byte + bps.append(('0x10b0d0b0', '"r $t5=0; r $t1=0; .while (@$t1 < 0x400) { r $t2=poi(' + GLOBREG_HASH + + '+@$t1*4); .while (@$t2 != 0) { .if (poi(@$t2+0x44) == ' + watch + ') { r $t5=@$t2 }; ' + 'r $t2=poi(@$t2+0x30) }; r $t1=@$t1+1 }; .printf ' + EQ + 'WATCH %x f5=%x' + NL + EQ + + ', @$t5, by(@$t5+5); ba30 w1 @$t5+5 ' + EQ + '.echo FLAGW; ? by(@$t5+5); r eip; k 8; gc' + + EQ + '; gc"')) + return bps + + +KEEP = re.compile(r'^(FOUND|CK|G |SERVE|SOD|CPR|HCU|COLORENTRY|FV|FK|FOLD|SPILL|SUBST|WATCH|FLAGW|Evaluate|eip=|' + r'[0-9a-f]{8} [0-9a-f]{8} )') + + +def probe(src, name, shift, watch): + tag = 'color-%s-%s-%d' % (os.path.basename(src)[:-4], name, shift) + dump = out / (tag + '.dump') + dump.write_text(c2base.rebase(DUMP)) + bps = breakpoints(dump, watch) + n = len(bps) + gate = ('r $t7=poi(poi(@eax)+4); ' + name_test(name) + '; .if (@$t6 != 0) { .printf ' + EQ + 'FOUND' + NL + + EQ + '; be 1-%d } .else { bd 1-%d }' % (n, n)) + lines = ['sxe ld:c2', 'g'] + lines += ['bp%d %s %s' % (i, a, c) for i, (a, c) in enumerate(bps, 1)] + # Color runs again for the stack colouring (COLOR_ENTRY): stop logging there + lines += ['bp%d %s ".printf ' % (n + 1, COLOR_ENTRY) + EQ + 'COLORENTRY' + NL + EQ + '; bd 1-%d; gc"' % n, + 'bd 1-%d' % n, + 'bp0 ' + GLOBAL_REG_ALLOC + ' "' + gate + '; gc"', + 'g', 'q', ''] + target = src + if shift: + # beside the original, so its relative #includes still resolve + target = src[:-4] + '__colorpad%d.cpp' % shift + pad = ''.join('typedef short ShiftPad%d;\n' % i for i in range(shift)).encode() + open(target, 'wb').write(pad + open(src, 'rb').read()) + try: + txt = run(lines, target, tag) + finally: + if shift: + os.remove(target) + if 'Malformed' in txt or 'Syntax error' in txt: + sys.exit('cdb rejected a command - see ' + str(out / (tag + '.log'))) + if 'FOUND' not in txt: + sys.exit('GlobalRegAlloc never ran for ?%s@ - check --name' % name) + result = '\n'.join(l.rstrip() for l in txt.splitlines() if KEEP.match(l)) + '\n' + (out / (tag + '.txt')).write_text(result) + return result, out / (tag + '.txt') + + +REGS = dict(enumerate(['-', 'eax', 'ecx', 'edx', 'ebx', 'esp', 'ebp', 'esi', 'edi'])) +EVENTS = ('FOLD', 'SPILL', 'SUBST', 'SERVE', 'CPRDONE') + + +def load(path): + """-> ({(stage, occurrence): [candidate dicts]}, {event kind: [event dicts]}, num -> (id, name))""" + stages, events, seen, cur = {}, collections.defaultdict(list), collections.Counter(), None + for line in open(path): + line = line.rstrip() + if line.startswith('CK '): + stage = int(line.split()[1]) + cur = stages.setdefault((stage, seen[stage]), []) + seen[stage] += 1 + elif line.startswith('G ') and cur is not None: + cur.append(dict(re.findall(r'(\w+)=(\S+)', line))) + elif line.split(' ', 1)[0] in EVENTS: + events[line.split(' ', 1)[0]].append(dict(re.findall(r'(\w+)=(\S+)', line))) + return stages, events + + +def compare(pa, pb): + """Per checkpoint, the candidates whose fields differ other than by their number; per kind of + event, the sequence of symbols in both runs up to and past the first divergence.""" + (sa, ea), (sb, eb) = load(pa), load(pb) + names = {} + for stages in (sa, sb): + for gs in stages.values(): + for d in gs: + if d.get('N'): + names[d['id']] = d['N'] + + def label(d): + if 'num' not in d: # CPRDONE carries the register instead + r = int(d.get('reg', '0'), 16) + return '%s%s=%s' % (d['id'], '(' + names[d['id']] + ')' if d['id'] in names else '', REGS.get(r, hex(r))) + tail = '#' + d['num'] + return '%s%s%s' % (d['id'], '(' + names[d['id']] + ')' if d['id'] in names else '', tail) + + def by_symbol(gs): + m = collections.defaultdict(list) + for d in gs: + m[d['id']].append((d['key'], d['c24'], d['c40'], d['f4'], int(d['num'], 16))) + return {k: sorted(v) for k, v in m.items()} + + print('candidates per checkpoint, compared by symbol id (fields: key c24 c40 f4 #num)') + for k in sorted(set(sa) | set(sb)): + x, y = by_symbol(sa.get(k, [])), by_symbol(sb.get(k, [])) + diffs = [i for i in sorted(set(x) | set(y)) if [t[:4] for t in x.get(i, [])] != [t[:4] for t in y.get(i, [])]] + print('CK %d.%d candidates %d/%d differing %d' % (k[0], k[1], len(sa.get(k, [])), len(sb.get(k, [])), + len(diffs))) + for i in diffs[:12]: + print(' id=%s %s\n A %s\n B %s' % (i, names.get(i, ''), x.get(i), y.get(i))) + print('\nevent order, as symbol id(name)#number') + for kind in EVENTS: + la, lb = [label(d) for d in ea.get(kind, [])], [label(d) for d in eb.get(kind, [])] + if [s.split('#')[0] for s in la] == [s.split('#')[0] for s in lb]: + print('%-7s same order (%d)' % (kind, len(la))) + continue + print('%-7s A: %s\n B: %s' % (kind, ' '.join(la), ' '.join(lb))) + + +def main(): + if '--compare' in sys.argv: + i = sys.argv.index('--compare') + compare(sys.argv[i + 1], sys.argv[i + 2]) + return + if '--name' not in sys.argv: + sys.exit(__doc__) + src = os.path.abspath(sys.argv[1]) + name = sys.argv[sys.argv.index('--name') + 1] + shift = int(sys.argv[sys.argv.index('--shift') + 1]) if '--shift' in sys.argv else 0 + watch = sys.argv[sys.argv.index('--watch') + 1] if '--watch' in sys.argv else None + txt, path = probe(src, name, shift, watch) + print(txt) + print('saved to ' + str(path)) + + +if __name__ == '__main__': + main() diff --git a/tools/compiler-internals/msvc-backend.md b/tools/compiler-internals/msvc-backend.md index c38bc6e8a5..1db83c70b6 100644 --- a/tools/compiler-internals/msvc-backend.md +++ b/tools/compiler-internals/msvc-backend.md @@ -3041,3 +3041,137 @@ Two more `quick_diff.py` artefacts came out of this batch and are fixed: a `swit (`$LN38@f[edi]`, `jmp dword ptr [ecx*4 + 0x41c9d0]`) were not collapsed to `G` on either side, which alone held `lightUpBuilding` at 0.8988 where it is 0.9312; and the original's indirect jumps were skipping normalisation entirely. + +## The TU symbol count decides some register choices (`shift_sweep.py`, `color_probe.py`) + +`placeDefensiveStructureTile` (TileMapState, PR #366) was 100% before a merge of `main` and 96.3% after it, with +the `.cpp` byte-identical and the IL identical up to `regalloc`. The merge had only added three enums to headers +the file includes. None of them is used by the function; what they changed is **how many symbols the front end +had numbered before the function**, and that number reaches the register allocator through a hash. + +### The chain, in c2.dll + +1. **`GrgImmedTupleHash`** (0x10b5c050) files every constant operand into the 64-bucket `_ImmedHashTable` + (0x10d12170). An address constant (tuple kind 3, the `&Instance<...>::instance` of a resolver global) hashes + by `[[[tuple+0x18]+8]]+0x28 & 0x3f` - a symbol number counted over the whole translation unit. Measured: an + `enum` with one enumerator adds 2 to it, a `typedef` 1, a `#define` 0, and only declarations *before* the + function count (four dummy enums after the last `#include` moved the result; the same four after the body + did not). +2. **`CreateGlobregs`** (0x10b5abe0) walks that table bucket by bucket and frees every immediate candidate that + turned out worthless (`ColRemoveGlobregFromHashTable`, 0x10b18790, called from 0x10b5ac39). The bucket + decides where each one lands in the free order. +3. **`GrgNewGlobreg`** (0x10b5b7b0) reuses `_FreeGlobs` last-in-first-out and **keeps the freed record's number** + (`+0x1c`). The same `CreateGlobregs` call then creates the candidates for the real symbols (`this`, `tile`, + `x`, temporaries), so they receive a permutation of the freed numbers. +4. What the number does **not** change: the colouring key `+0x0c`, the benefit `+0x40`, the symbol id `+0x44`, + and therefore `InsertInList`'s serve order, whose tie-break is the id. `color_probe.py --compare` on a bad + and a good shift shows all of them equal right up to the pass that diverges. +5. What it does change: any **greedy** pass that visits candidates in number order - walking `_GlobregHash` + (0x10d15090, bucket `number & 0x3ff`, chained through `+0x30`) or `bsf` over a bitset of numbers - where + acting on one candidate changes what the pass can do with a later one. Two were caught doing it. + +### Pass 1: `FoldTooCheapDefs` (0x10b0da80) - `placeDefensiveStructureTile` + +Phase 2 of `FoldTooCheapDefs` walks `_GlobregHash` and folds each cheap single-definition candidate back into its +uses (`ComponentKilled` 0x10b19fe0 as the veto, then `DoFoldDef` 0x10b195e0 -> `CmdDoFoldDef`). This function +has two such candidates, one built on the other (`tuple_dump.py` at `FoldTooCheapDefs` entry; the `t` numbers +are the candidate numbers): + +``` +shift 0 (bad) shift 46 (good) +lea t8, dword ptr [this + tile] lea t11, dword ptr [this + tile] +mov byte ptr [t8 + 3313824], t1 mov byte ptr [t11 + 3313824], t1 +lea t21, dword ptr [t8 + 2751024] lea t8, dword ptr [t11 + 2751024] ; &HeightLayer[tile] +mov t3, byte ptr [t8 + 2831424] mov t21, byte ptr [t11 + 2831424] +mov byte ptr [t21], t3 mov byte ptr [t8], t21 ; ... and ~16 more uses +``` + +With the inner `lea` (A) numbered lower, it is folded first: every `[A + d]` becomes `[this + tile + d]`, which +turns the outer `lea` (B) from a based address (`0x27c`) into an indexed one (`0x27d`). `CmdDoFoldDef`'s end +block notices and sets B's never-fold bit (byte `+5 |= 4`, the write at 0x10c248f5, caught with `--watch 0x1b`), +so B is skipped when the walk reaches it. With B numbered lower, B folds first (into `[A + 2751024]`) and A +after it, and both disappear: + +``` +color_probe.py ... --compare (A = shift 0, B = shift 46) +FOLD A: 46#2 34#6 1d#8 5c#f 62#17 75#1a 68#1e 6d#25 56#26 + B: 56#2 34#3 46#6 1b#8 1d#b 5c#f 62#17 6d#1a 68#1e 75#23 +CK 4.0 id=1e tile A [key 385, refs 0x26, benefit 76] B [key 451, refs 0x38, benefit 112] + id=e this A [key 311, refs 0x25, benefit 74] B [key 383, refs 0x37, benefit 110] +``` + +The surviving B is gone again before colouring proper, but it is still there when `Spillcosts` counts +references, so 18 uses each of `tile` and `this` are booked against B instead. The lower keys reorder the tail +of the serve list, the `x & 1` temporaries tie with `x` and take `eax` instead of `ecx` (`CPRDONE` in the same +comparison), a `mov eax, ecx` copy survives, and `FgDoCommonTailMerge` can no longer merge the two crenel arms' +identical tails - seven instructions more: + +``` +shift 0 (ours, 96.3%) shift 46 (= original) +add byte ptr [esi+edi+2751024], 98 add byte ptr [esi+edi+2751024], 98 +mov eax, ecx $LN54: +and eax, 1 and ecx, 1 +je $LN18 je $LN18 +test bl, 1 test bl, 1 +jne $LN18 jne $LN18 +or dword ptr [edi+esi*4+1462624], 0x400000 or dword ptr [edi+esi*4+1462624], 0x400000 +jmp $LN52 jmp $LN52 +... ... +add byte ptr [esi+edi+2751024], 68 add byte ptr [esi+edi+2751024], 68 +mov eax, ecx jmp $LN54 ; the second arm reuses the first's tail +and eax, 1 +... (the whole tail again) +``` + +### Pass 2: `SpillObvious` (0x10b11560) - the sliders and `DisplayMapDescriptionAndAllocatePlayersToSlots` + +`SpillObvious` spills every candidate with fewer than two references (`+0x24 < 2`) in `bsf` order (the `Spill` +call at 0x10b115d6, `CmdSubstDef` at 0x10b11630; `eax` is the candidate at both). Each spill inserts its reload +right before the use, so the spill order is the order of the loads. In +`MenuItemRenderFunction_GameplayOptions_SpeedSlider`, `thumbX = thumbXPos + DAT_ButtonX::instance`: + +``` +SPILL A: 7(thumbXPos)#3 29(thumbWidth)#6 8(DAT_ButtonX)#7 ... shift 0 (= original) + B: 29(thumbWidth)#6 8(DAT_ButtonX)#7 7(thumbXPos)#8 ... shift 30 + +shift 0 shift 30 +mov ecx, dword ptr [DAT_ButtonX] mov ecx, dword ptr [thumbXPos] +mov eax, dword ptr [thumbXPos] mov eax, dword ptr [DAT_ButtonX] +add eax, ecx add eax, ecx +``` + +Serve order, `ChoosePhysReg` costs and chosen registers are identical in both runs; only the load order flips, +so the two operands trade registers. + +### Periodic in 64, and how many functions it reaches + +Because the only path is `number & 0x3f`, the outcome repeats every 64 symbols - predicted and confirmed for +nine combinations of dummy enums and typedefs on `placeDefensiveStructureTile`, including 32 enums = 64 +typedefs = 128 typedefs = no change. `shift_sweep.py --all` screened all 2469 marked functions at shifts +0/17/34/51 (no compile errors), then swept the six that varied over the full period: + +| function | shift 0 | best | shifts at best (of 64) | +|---|---|---|---| +| `placeDefensiveStructureTile` | 0.9630 | **1.0000** | 39: 13-23, 25-52 | +| `MenuItemRenderFunction_GameplayOptions_SpeedSlider` | 1.0000 | 1.0000 | 21: 0-14, 16-19, 62-63 | +| `MenuItemRenderFunction_SoundOptions_VolumeSlider` | 1.0000 | 1.0000 | 21: 0-10, 12-15, 58-63 | +| `MenuItemActionHandler_UnusedChooseAvailableKeeps_Main` | 1.0000 | 1.0000 | 50: 0-48, 63 | +| `DisplayMapDescriptionAndAllocatePlayersToSlots` | 0.9507 | 0.9686 | 45 | +| `UpdateFireBallistaBuilding` | 0.9936 | 0.9936 | 55 | + +About 0.25% of functions, and only one is held below a reachable 100% today; three sit at 100% by luck and an +unrelated header edit can knock them down (the two sliders hardest). A function whose sensitive window is +narrower than 17 shifts can slip between the screen's four points, so six is a lower bound. + +### What to do with it + +- A header-only change that moves one function's match is a candidate for this. Check with + `shift_sweep.py OUT.jsonl FILE.cpp --name FUNC` (0-63 by default, about 30 s at `-j 12`). +- If any shift reaches 1.0, the source is right: record the function as "matches modulo TU symbol count" in the + commit remark, like "only call targets differ". Do **not** pad the translation unit with dummy declarations + or steer the enum/typedef count by hand - the next header edit anywhere above the function moves it again. + Converting enums to `#define`s does not fix it either; it only removes symbols, which helps or hurts by luck. +- To see which pass decides it for a new case: `color_probe.py FILE.cpp --name FUNC` and again with + `--shift N` for a good N, then `color_probe.py --compare A.txt B.txt`. The first checkpoint where a + candidate differs by more than its number, and the first event list that differs, name the pass. `--watch ID` + puts a hardware breakpoint on that candidate's flag byte and prints the stack of every write. diff --git a/tools/compiler-internals/shift_sweep.py b/tools/compiler-internals/shift_sweep.py new file mode 100644 index 0000000000..82c6e1acd8 --- /dev/null +++ b/tools/compiler-internals/shift_sweep.py @@ -0,0 +1,173 @@ +"""Does a function's match depend on how many symbols its translation unit declares before it? + +c2.dll numbers its global register candidates through a 64-bucket hash keyed on each global's +TU-wide symbol number (msvc-backend.md, "The TU symbol count decides some register choices"), so +a header edit anywhere above a function can move its match with no change to its source. This +tool compiles copies of a .cpp with N dummy `typedef short` lines prepended - each one shifts every +later symbol number by 1 - and records quick_diff's ratio per N. + +usage: shift_sweep.py OUT.jsonl FILE.cpp [--name FUNC] [--shifts 0-63] [-j 14] + shift_sweep.py OUT.jsonl --all [--shifts 0,17,34,51] [-j 14] every // FUNCTION: in src/OpenSHC + shift_sweep.py OUT.jsonl --only LIST.txt [--shifts ...] LIST.txt lines: pathname + shift_sweep.py OUT.jsonl --report functions whose ratio varies + +--shifts takes a comma list and/or ranges (`0-63`, `0,17,34,51`). OUT.jsonl is appended to and +resumable: (file, name, shift) triples already in it are skipped. The default for one file is the +whole period 0-63, for --all the 4-point screen 0,17,34,51 (cheap, but a function whose sensitive +window is narrower than 17 can slip through it). + +Reading the report: `now` is shift 0 (the TU as committed), `best` the highest ratio over the +shifts measured and `at best` how many shifts reach it. A function whose best is 1.0 has the right +source - record it as "matches modulo TU symbol count" rather than padding the TU, because the +next header edit moves the count again. A function at 1.0 now with few shifts at best is fragile: +an unrelated header change can drop it. +""" + +import json +import os +import re +import subprocess +import sys +import threading +from concurrent.futures import ThreadPoolExecutor + +ROOT = os.path.abspath(os.path.join(os.path.dirname(os.path.abspath(__file__)), '..', '..')) +QD = os.path.join(ROOT, 'tools', 'reimplementation-helper', 'batch', 'quick_diff.py') +MARK = re.compile(r'FUNCTION: STRONGHOLDCRUSADER (0x[0-9A-Fa-f]+)[^\n]*\n[^\n(]*?(?:(\w+)::)?(\w+)\(') +PAD = '__shiftpad' + + +def arg(flag, default=None): + return sys.argv[sys.argv.index(flag) + 1] if flag in sys.argv else default + + +def parse_shifts(text): + out = [] + for part in text.split(','): + a, _, b = part.partition('-') + out += range(int(a), int(b) + 1) if b else [int(a)] + return out + + +def functions_in(path): + src = open(os.path.join(ROOT, path), encoding='utf-8', errors='replace').read() + return [m.group(3) for m in MARK.finditer(src)] + + +def targets(): + if '--only' in sys.argv: + for line in open(arg('--only'), encoding='utf-8'): + if line.strip(): + p, n = line.rstrip('\n').split('\t') + yield p, n + return + if '--all' in sys.argv: + for d, _, fs in os.walk(os.path.join(ROOT, 'src', 'OpenSHC')): + for f in sorted(fs): + if f.endswith('.cpp') and PAD not in f: + p = os.path.relpath(os.path.join(d, f), ROOT).replace('\\', '/') + for n in functions_in(p): + yield p, n + return + p = os.path.relpath(os.path.abspath(sys.argv[2]), ROOT).replace('\\', '/') + names = functions_in(p) + want = arg('--name') + for n in names: + if want is None or n == want: + yield p, n + if want is None: + return + + +def report(out_path): + runs = {} + for line in open(out_path, encoding='utf-8'): + r = json.loads(line) + if 'ratio' in r: + runs.setdefault((r['file'], r['name']), {})[r['shift']] = r['ratio'] + rows = [] + for (f, n), by in runs.items(): + vals = set(by.values()) + if len(vals) > 1: + best = max(vals) + rows.append((n, by.get(0), best, sum(v == best for v in by.values()), len(by), + [s for s in sorted(by) if by[s] == best])) + print('%d functions measured, %d vary with the symbol count' % (len(runs), len(rows))) + for n, now, best, k, total, good in sorted(rows, key=lambda r: (r[1] or 0) - r[2]): + print('%-60s now %s best %.4f at %d/%d %s' % ( + n, '%.4f' % now if now is not None else ' - ', best, k, total, spans(good))) + + +def spans(xs): + out, start = [], None + for i, x in enumerate(xs): + if start is None: + start = x + if i + 1 == len(xs) or xs[i + 1] != x + 1: + out.append(str(start) if start == x else '%d-%d' % (start, x)) + start = None + return ','.join(out) + + +def main(): + out_path = os.path.abspath(sys.argv[1]) + if '--report' in sys.argv: + report(out_path) + return + default = '0,17,34,51' if ('--all' in sys.argv or '--only' in sys.argv) else '0-63' + shifts = parse_shifts(arg('--shifts', default)) + jobs = int(arg('-j', '14')) + + done = set() + if os.path.exists(out_path): + for line in open(out_path, encoding='utf-8'): + r = json.loads(line) + done.add((r['file'], r['name'], r['shift'])) + lock = threading.Lock() + slots = list(range(jobs)) + slot_lock = threading.Lock() + + def run(task): + path, name, n, multi = task + with slot_lock: + slot = slots.pop() + try: + full = os.path.join(ROOT, path) + src = open(full, 'rb').read() + # beside the original, so its relative #includes still resolve + tmp = full[:-4] + '%s%d_%d.cpp' % (PAD, n, slot) + pad = ''.join('typedef short ShiftPad%d;\n' % i for i in range(n)).encode() + open(tmp, 'wb').write(pad + src) + try: + cmd = [sys.executable, QD, tmp, '-q'] + (['--name', name] if multi else []) + env = dict(os.environ, QD_OUT='shift%d' % slot) + r = subprocess.run(cmd, cwd=ROOT, capture_output=True, text=True, env=env, timeout=600) + o = r.stdout + r.stderr + finally: + os.remove(tmp) + m = re.search(r'ratio (\S+)\s+orig (\d+) ours (\d+)', o) + rec = {'file': path, 'name': name, 'shift': n} + if m: + rec.update(ratio=float(m.group(1)), orig=int(m.group(2)), ours=int(m.group(3))) + else: + rec['error'] = o.strip()[-300:] + with lock: + with open(out_path, 'a', encoding='utf-8') as fh: + fh.write(json.dumps(rec) + '\n') + finally: + with slot_lock: + slots.append(slot) + + tl = list(targets()) + count = {} + for p, n in tl: + count[p] = len(functions_in(p)) + tasks = [(p, n, s, count[p] > 1) for p, n in tl for s in shifts if (p, n, s) not in done] + print('%d functions, %d compiles to do' % (len(tl), len(tasks)), flush=True) + with ThreadPoolExecutor(jobs) as ex: + list(ex.map(run, tasks)) + report(out_path) + + +if __name__ == '__main__': + main()