diff --git a/cmake/compiler-flags-gl.txt b/cmake/compiler-flags-gl.txt index 8926a894ea..948ab85226 100644 --- a/cmake/compiler-flags-gl.txt +++ b/cmake/compiler-flags-gl.txt @@ -119,10 +119,21 @@ src/OpenSHC/UI/MenuItems/BuildingAndStatusMenu/MenuItemRenderFunction_BuildingAn 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/getRubbleGraphicStageForDamageLevel.cpp +src/OpenSHC/Map/TileMapState/returnOwnedMoatAtTile.cpp src/OpenSHC/Map/TileMapState/setSignpostDistanceForCampaignMission.cpp src/OpenSHC/Map/TileMapState/clearDrawBridgeWater.cpp src/OpenSHC/Map/TileMapState/clearFixedSizeTwoBuildingFootprint.cpp +src/OpenSHC/Map/TileMapState/clearRockFootprintFlags.cpp +src/OpenSHC/Map/TileMapState/clearSizeFiveBuildingFootprintAndMoats.cpp +src/OpenSHC/Map/TileMapState/clearTreeFootprintFlags.cpp +src/OpenSHC/Map/TileMapState/floodMoatUnderRemovedBuilding.cpp +src/OpenSHC/Map/TileMapState/isUnitBlockingSizeFiveFootprint.cpp src/OpenSHC/Map/TileMapState/markBuildingFootprintFlag.cpp +src/OpenSHC/Map/TileMapState/placeBarracks.cpp +src/OpenSHC/Map/TileMapState/placeOilsmelter.cpp +src/OpenSHC/Map/TileMapState/renderWallPlacementPreview.cpp +src/OpenSHC/Map/TileMapState/setMiscDisplayLayer.cpp src/OpenSHC/Map/TileMapState/storeMinAndMaxHeightOfArea.cpp src/OpenSHC/Map/TileMapState/unmarkBuildingFootprintFlag.cpp src/OpenSHC/Map/TileMapState/updateGameRelatedValue.cpp @@ -131,9 +142,16 @@ 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/placeKeep.cpp +src/OpenSHC/Map/TileMapState/updatePathLinkagesForBuilding.cpp +src/OpenSHC/Map/TileMapState/resetTileAndClearMoat.cpp src/OpenSHC/Map/TileMapState/setupAllMapSections.cpp src/OpenSHC/Map/TileMapState/setupLogicalMapBorders.cpp src/OpenSHC/Map/TileMapState/updateShowHiLayerOrResetChangedLayer.cpp +src/OpenSHC/Map/TileMapState/validateWallBuildPath.cpp +src/OpenSHC/Map/TileMapState/isTileEnclosedByWalls.cpp +src/OpenSHC/Map/TileMapState/isTileEnclosedByWallsOrGates.cpp +src/OpenSHC/Map/TileMapState/getMaxWallHeightInBrushArea.cpp src/OpenSHC/UI/DisplayElements/RenderTimeUntilDefeatDisplayElement.cpp src/OpenSHC/UI/DisplayElements/GetIfDisplayElementStateNotZero.cpp src/OpenSHC/UI/DisplayElements/FindDisplayElementWithID.cpp @@ -152,3 +170,26 @@ src/OpenSHC/Map/Units/UpdateMiner.cpp src/OpenSHC/Map/Units/UpdateQuarryMason.cpp src/OpenSHC/Map/Units/UpdateDrunkard.cpp src/OpenSHC/Map/Units/UpdateInnkeeper.cpp +src/OpenSHC/Map/TileMapState/placeApplefarm.cpp +src/OpenSHC/Map/TileMapState/placeDairyfarm.cpp +src/OpenSHC/Map/TileMapState/placeDrawbridge.cpp +src/OpenSHC/Map/TileMapState/placeEngineersguild.cpp +src/OpenSHC/Map/TileMapState/placeGatehouseLarge.cpp +src/OpenSHC/Map/TileMapState/placeGatehouseSmall.cpp +src/OpenSHC/Map/TileMapState/placeHopfarm.cpp +src/OpenSHC/Map/TileMapState/placeKillingPit.cpp +src/OpenSHC/Map/TileMapState/placePositiveFearfactor.cpp +src/OpenSHC/Map/TileMapState/placeQuarry.cpp +src/OpenSHC/Map/TileMapState/placeSiegeTent.cpp +src/OpenSHC/Map/TileMapState/placeSiegetowerPlaced.cpp +src/OpenSHC/Map/TileMapState/placeTower.cpp +src/OpenSHC/Map/TileMapState/placeTunnelersguild.cpp +src/OpenSHC/Map/TileMapState/placeWheatfarm.cpp +src/OpenSHC/Map/TileMapState/placeWorkshopOrHovel.cpp +src/OpenSHC/Map/TileMapState/stampBuildingOntoTileMap.cpp +src/OpenSHC/Map/TileMapState/upgradeTowerLogicLayer.cpp +src/OpenSHC/Map/TileMapState/setConstructionGFXLayerBasedOnPlacementChecks.cpp +src/OpenSHC/Map/TileMapState/prepareMap.cpp +src/OpenSHC/Map/TileMapState/toggleFlatView.cpp +src/OpenSHC/Rendering/ViewportRenderState/xyAreValid.cpp +src/OpenSHC/Map/TileMapState/checkBuildingCanBePlacedHere.cpp diff --git a/src/OpenSHC/Map/TileMapState.func.hpp b/src/OpenSHC/Map/TileMapState.func.hpp index 1eeea936eb..115c18a073 100644 --- a/src/OpenSHC/Map/TileMapState.func.hpp +++ b/src/OpenSHC/Map/TileMapState.func.hpp @@ -5,6 +5,7 @@ #include "OpenSHC/Commands/MappersEnum.hpp" #include "OpenSHC/IO/PackagedFileMagicNum.hpp" #include "OpenSHC/Map/Buildings/BuildingType.hpp" +#include "OpenSHC/Map/Buildings/BuildingTypeShort.hpp" #include "OpenSHC/Map/Enums/CardinalDirection.hpp" #include "OpenSHC/Map/Enums/MapOrientation.hpp" #include "OpenSHC/Map/LogicHelpers/Logic1.hpp" @@ -18,6 +19,7 @@ namespace Map { using OpenSHC::Commands::MappersEnum; using OpenSHC::IO::PackagedFileMagicNum; using OpenSHC::Map::Buildings::BuildingType; + using OpenSHC::Map::Buildings::BuildingTypeShort; using OpenSHC::Map::Enums::CardinalDirection; using OpenSHC::Map::Enums::MapOrientation; using OpenSHC::Map::LogicHelpers::Logic1; @@ -716,7 +718,7 @@ namespace Map { &TileMapState::planMoat) planMoat; - MACRO_FUNCTION_RESOLVER(void (TileMapState::*)(int, uint, uint, BuildingType, uint, int, int), false, + MACRO_FUNCTION_RESOLVER(void (TileMapState::*)(int, uint, uint, BuildingTypeShort, uint, int, int), false, Address::SHC_3BB0A8C1_0x00514520, &TileMapState::placeKillingPit) placeKillingPit; @@ -740,7 +742,7 @@ namespace Map { Address::SHC_3BB0A8C1_0x005154D0, &TileMapState::placeDairyfarm) placeDairyfarm; - MACRO_FUNCTION_RESOLVER(void (TileMapState::*)(int, uint, uint, BuildingType, undefined4, int, int), false, + MACRO_FUNCTION_RESOLVER(void (TileMapState::*)(int, uint, uint, BuildingTypeShort, undefined4, int, int), false, Address::SHC_3BB0A8C1_0x00515740, &TileMapState::placeApplefarm) placeApplefarm; diff --git a/src/OpenSHC/Map/TileMapState.hpp b/src/OpenSHC/Map/TileMapState.hpp index b41b11cb16..39720e5d53 100644 --- a/src/OpenSHC/Map/TileMapState.hpp +++ b/src/OpenSHC/Map/TileMapState.hpp @@ -12,6 +12,7 @@ #include "OpenSHC/IO/PackagedFileMagicNum.hpp" #include "OpenSHC/Map/Buildings/BuildingFailReasonEnumInt.hpp" #include "OpenSHC/Map/Buildings/BuildingType.hpp" +#include "OpenSHC/Map/Buildings/BuildingTypeShort.hpp" #include "OpenSHC/Map/Enums/CardinalDirection.hpp" #include "OpenSHC/Map/Enums/MapOrientation.hpp" #include "OpenSHC/Map/Enums/MapOrientationInt.hpp" @@ -30,6 +31,7 @@ namespace Map { using OpenSHC::Map::PitchDitch; using OpenSHC::Map::Buildings::BuildingFailReasonEnumInt; using OpenSHC::Map::Buildings::BuildingType; + using OpenSHC::Map::Buildings::BuildingTypeShort; using OpenSHC::Map::Enums::CardinalDirection; using OpenSHC::Map::Enums::MapOrientation; using OpenSHC::Map::Enums::MapOrientationInt; @@ -413,6 +415,10 @@ namespace Map { uint getTotalHeightAtTile(int tile); + // a second function with getTotalHeightAtTile's body, which the original's linker folded into the same + // address; it has no game address of its own and therefore no resolver + uint getTotalHeightAtTile2(int tile); + int getTotalHeightAt(int tile, int playerID); uint getHeightAtTileIncludingOwnersBuildings(int tile, int playerID); @@ -610,7 +616,7 @@ namespace Map { void planMoat(undefined4 playerID, int tile, uint tileY); void placeKillingPit( - int playerID, uint x, uint y, BuildingType buildingType, uint width, int variation, int height); + int playerID, uint x, uint y, BuildingTypeShort buildingType, uint width, int variation, int height); void placeKeep(int playerID, uint x, uint y, BuildingType type, uint size, int orientation, int xyValue); @@ -627,7 +633,7 @@ namespace Map { int param_6, undefined4 param_7); void placeApplefarm( - int playerID, uint x, uint y, BuildingType buildingType, undefined4 param_5, int param_6, int param_7); + int playerID, uint x, uint y, BuildingTypeShort buildingType, undefined4 param_5, int param_6, int param_7); void clearBuildingFromTerrain(int buildingID); diff --git a/src/OpenSHC/Map/TileMapState/NeighbourFlagsAsm.hpp b/src/OpenSHC/Map/TileMapState/NeighbourFlagsAsm.hpp new file mode 100644 index 0000000000..28d814396b --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/NeighbourFlagsAsm.hpp @@ -0,0 +1,362 @@ +/** + HAND WRITTEN - not generated. + + path: 'OpenSHC/Map/TileMapState/NeighbourFlagsAsm.hpp' + + The original gathers the neighbours of DAT_SomeTile into bitFlag with handwritten assembly, and that + assembly is a MACRO: the identical instruction stream appears verbatim inside + setBitFlagBasedOnWallTowerGatehouseOrKeep (0x004FF870, once), updateGFXLayers (0x004FC9E0, eleven + times), updateMacroLayerRelated (0x004FDB00, eleven times) and updateGfxLayer (0x00509180, nineteen + times) - never as a call. It is recognisable by "mov edx, 0" (no compiler materialises zero that + way), by advancing the row pointer with repeated "add esi, ecx" instead of one scaled lea, and, in + the TERRAIN variants, by loading a dword at a one-byte offset and masking it down to the low byte. + + Five shapes occur, selected by which layer pointer is read and how many neighbours are gathered: + + LOGIC - ptr_LogicLayer, 4-byte elements: "shl eax, 2", "add esi, ecx" x4, [esi +/- 4] + MISC - ptr_MiscDisplayLayer, 2-byte elements: "shl eax, 1", "add esi, ecx" x2, [esi +/- 2] + TERRAIN - ptr_TerrainTypeTileMap, 1-byte elements: no shift, "add esi, ecx" x1, [esi +/- 1] + _4 - east, west, north and south only + _8 - also north-west, north-east, south-west and south-east + + bitFlag bits: 0x02 west, 0x20 east, 0x80 north, 0x08 south, + 0x01 north-west, 0x40 north-east, 0x04 south-west, 0x10 south-east. + + Every expansion clobbers eax, ebx, ecx, edx, esi and edi and leaves the result in this->bitFlag. + + ID must be unique within the enclosing function; it exists only to keep the asm labels of two + expansions apart. MASK must be a numeric literal or a preprocessor constant: MASM has no "|" + operator, so an enum OR expression cannot be used as an asm operand. + + The original reaches the fields through the global instance's absolute address; we go through "this" + because MSVC inline asm cannot name DAT_TileMapState::instance ("::" is not parseable in an asm + operand). That costs the "mov eax, this" instructions and nothing else. +*/ + +#pragma once + +#include "OpenSHC/Map/TileMapState.hpp" + +// A "jz" over the "or edx, imm" that follows it (SKIP = 3 for an imm8, 6 for an imm32), in the 6-byte near +// encoding 0F 84 rel32 that the original's assembler produced. MSVC's inline assembler only emits the 2-byte short +// form for a jump to a nearby label, even when written "jz near ptr label", so the bytes are emitted directly. +#ifndef MACRO_ASM_JZ_NEAR +#define MACRO_ASM_JZ_NEAR(SKIP) \ + __asm _emit 0x0F __asm _emit 0x84 __asm _emit SKIP __asm _emit 0 __asm _emit 0 __asm _emit 0 +#endif + +// clang-format off + +#define MACRO_NEIGHBOUR_FLAGS_LOGIC_4(ID, MASK) \ + __asm mov eax, this \ + __asm mov edi, dword ptr [eax]TileMapState.ptr_MovementDirectionTranslationMatrix \ + __asm mov esi, dword ptr [eax]TileMapState.ptr_LogicLayer \ + __asm mov eax, dword ptr [eax]TileMapState.DAT_SomeTile \ + __asm shl eax, 0x2 \ + __asm add esi, eax \ + __asm push esi \ + __asm mov eax, this \ + __asm mov eax, dword ptr [eax]TileMapState.DAT_SomeY \ + __asm shl eax, 0x5 \ + __asm add edi, eax \ + __asm mov edx, 0x0 \ + __asm mov eax, dword ptr [esi + 0x4] \ + __asm mov ebx, dword ptr [esi - 0x4] \ + __asm and eax, MASK \ + MACRO_ASM_JZ_NEAR(3) \ + __asm or edx, 0x20 \ + __asm east_done_##ID: \ + __asm and ebx, MASK \ + MACRO_ASM_JZ_NEAR(3) \ + __asm or edx, 0x2 \ + __asm west_done_##ID: \ + __asm mov ecx, dword ptr [edi] \ + __asm add esi, ecx \ + __asm add esi, ecx \ + __asm add esi, ecx \ + __asm add esi, ecx \ + __asm mov eax, dword ptr [esi] \ + __asm and eax, MASK \ + MACRO_ASM_JZ_NEAR(6) \ + __asm or edx, 0x80 \ + __asm north_done_##ID: \ + __asm mov ecx, dword ptr [edi + 0x10] \ + __asm pop esi \ + __asm add esi, ecx \ + __asm add esi, ecx \ + __asm add esi, ecx \ + __asm add esi, ecx \ + __asm mov eax, dword ptr [esi] \ + __asm and eax, MASK \ + MACRO_ASM_JZ_NEAR(3) \ + __asm or edx, 0x8 \ + __asm south_done_##ID: \ + __asm mov eax, this \ + __asm mov byte ptr [eax]TileMapState.bitFlag, dl + +#define MACRO_NEIGHBOUR_FLAGS_LOGIC_8(ID, MASK) \ + __asm mov eax, this \ + __asm mov edi, dword ptr [eax]TileMapState.ptr_MovementDirectionTranslationMatrix \ + __asm mov esi, dword ptr [eax]TileMapState.ptr_LogicLayer \ + __asm mov eax, dword ptr [eax]TileMapState.DAT_SomeTile \ + __asm shl eax, 0x2 \ + __asm add esi, eax \ + __asm push esi \ + __asm mov eax, this \ + __asm mov eax, dword ptr [eax]TileMapState.DAT_SomeY \ + __asm shl eax, 0x5 \ + __asm add edi, eax \ + __asm mov edx, 0x0 \ + __asm mov eax, dword ptr [esi + 0x4] \ + __asm mov ebx, dword ptr [esi - 0x4] \ + __asm and eax, MASK \ + MACRO_ASM_JZ_NEAR(3) \ + __asm or edx, 0x20 \ + __asm east_done_##ID: \ + __asm and ebx, MASK \ + MACRO_ASM_JZ_NEAR(3) \ + __asm or edx, 0x2 \ + __asm west_done_##ID: \ + __asm mov ecx, dword ptr [edi] \ + __asm add esi, ecx \ + __asm add esi, ecx \ + __asm add esi, ecx \ + __asm add esi, ecx \ + __asm mov eax, dword ptr [esi - 0x4] \ + __asm mov ebx, dword ptr [esi + 0x4] \ + __asm mov ecx, dword ptr [esi] \ + __asm and eax, MASK \ + MACRO_ASM_JZ_NEAR(3) \ + __asm or edx, 0x1 \ + __asm north_west_done_##ID: \ + __asm and ebx, MASK \ + MACRO_ASM_JZ_NEAR(3) \ + __asm or edx, 0x40 \ + __asm north_east_done_##ID: \ + __asm and ecx, MASK \ + MACRO_ASM_JZ_NEAR(6) \ + __asm or edx, 0x80 \ + __asm north_done_##ID: \ + __asm mov ecx, dword ptr [edi + 0x10] \ + __asm pop esi \ + __asm add esi, ecx \ + __asm add esi, ecx \ + __asm add esi, ecx \ + __asm add esi, ecx \ + __asm mov eax, dword ptr [esi - 0x4] \ + __asm mov ebx, dword ptr [esi + 0x4] \ + __asm mov ecx, dword ptr [esi] \ + __asm and eax, MASK \ + MACRO_ASM_JZ_NEAR(3) \ + __asm or edx, 0x4 \ + __asm south_west_done_##ID: \ + __asm and ebx, MASK \ + MACRO_ASM_JZ_NEAR(3) \ + __asm or edx, 0x10 \ + __asm south_east_done_##ID: \ + __asm and ecx, MASK \ + MACRO_ASM_JZ_NEAR(3) \ + __asm or edx, 0x8 \ + __asm south_done_##ID: \ + __asm mov eax, this \ + __asm mov byte ptr [eax]TileMapState.bitFlag, dl + +#define MACRO_NEIGHBOUR_FLAGS_MISC_8(ID, MASK) \ + __asm mov eax, this \ + __asm mov edi, dword ptr [eax]TileMapState.ptr_MovementDirectionTranslationMatrix \ + __asm mov esi, dword ptr [eax]TileMapState.ptr_MiscDisplayLayer \ + __asm mov eax, dword ptr [eax]TileMapState.DAT_SomeTile \ + __asm shl eax, 0x1 \ + __asm add esi, eax \ + __asm push esi \ + __asm mov eax, this \ + __asm mov eax, dword ptr [eax]TileMapState.DAT_SomeY \ + __asm shl eax, 0x5 \ + __asm add edi, eax \ + __asm mov edx, 0x0 \ + __asm mov eax, dword ptr [esi + 0x2] \ + __asm mov ebx, dword ptr [esi - 0x2] \ + __asm and eax, MASK \ + MACRO_ASM_JZ_NEAR(3) \ + __asm or edx, 0x20 \ + __asm east_done_##ID: \ + __asm and ebx, MASK \ + MACRO_ASM_JZ_NEAR(3) \ + __asm or edx, 0x2 \ + __asm west_done_##ID: \ + __asm mov ecx, dword ptr [edi] \ + __asm add esi, ecx \ + __asm add esi, ecx \ + __asm mov eax, dword ptr [esi - 0x2] \ + __asm mov ebx, dword ptr [esi + 0x2] \ + __asm mov ecx, dword ptr [esi] \ + __asm and eax, MASK \ + MACRO_ASM_JZ_NEAR(3) \ + __asm or edx, 0x1 \ + __asm north_west_done_##ID: \ + __asm and ebx, MASK \ + MACRO_ASM_JZ_NEAR(3) \ + __asm or edx, 0x40 \ + __asm north_east_done_##ID: \ + __asm and ecx, MASK \ + MACRO_ASM_JZ_NEAR(6) \ + __asm or edx, 0x80 \ + __asm north_done_##ID: \ + __asm mov ecx, dword ptr [edi + 0x10] \ + __asm pop esi \ + __asm add esi, ecx \ + __asm add esi, ecx \ + __asm mov eax, dword ptr [esi - 0x2] \ + __asm mov ebx, dword ptr [esi + 0x2] \ + __asm mov ecx, dword ptr [esi] \ + __asm and eax, MASK \ + MACRO_ASM_JZ_NEAR(3) \ + __asm or edx, 0x4 \ + __asm south_west_done_##ID: \ + __asm and ebx, MASK \ + MACRO_ASM_JZ_NEAR(3) \ + __asm or edx, 0x10 \ + __asm south_east_done_##ID: \ + __asm and ecx, MASK \ + MACRO_ASM_JZ_NEAR(3) \ + __asm or edx, 0x8 \ + __asm south_done_##ID: \ + __asm mov eax, this \ + __asm mov byte ptr [eax]TileMapState.bitFlag, dl + +#define MACRO_NEIGHBOUR_FLAGS_TERRAIN_4(ID, MASK) \ + __asm mov eax, this \ + __asm mov edi, dword ptr [eax]TileMapState.ptr_MovementDirectionTranslationMatrix \ + __asm mov esi, dword ptr [eax]TileMapState.ptr_TerrainTypeTileMap \ + __asm mov eax, dword ptr [eax]TileMapState.DAT_SomeTile \ + __asm add esi, eax \ + __asm push esi \ + __asm mov eax, this \ + __asm mov eax, dword ptr [eax]TileMapState.DAT_SomeY \ + __asm shl eax, 0x5 \ + __asm add edi, eax \ + __asm mov edx, 0x0 \ + __asm mov eax, dword ptr [esi + 0x1] \ + __asm mov ebx, dword ptr [esi - 0x1] \ + __asm and eax, MASK \ + MACRO_ASM_JZ_NEAR(3) \ + __asm or edx, 0x20 \ + __asm east_done_##ID: \ + __asm and ebx, MASK \ + MACRO_ASM_JZ_NEAR(3) \ + __asm or edx, 0x2 \ + __asm west_done_##ID: \ + __asm mov ecx, dword ptr [edi] \ + __asm add esi, ecx \ + __asm mov eax, dword ptr [esi] \ + __asm and eax, MASK \ + MACRO_ASM_JZ_NEAR(6) \ + __asm or edx, 0x80 \ + __asm north_done_##ID: \ + __asm mov ecx, dword ptr [edi + 0x10] \ + __asm pop esi \ + __asm add esi, ecx \ + __asm mov eax, dword ptr [esi] \ + __asm and eax, MASK \ + MACRO_ASM_JZ_NEAR(3) \ + __asm or edx, 0x8 \ + __asm south_done_##ID: \ + __asm mov eax, this \ + __asm mov byte ptr [eax]TileMapState.bitFlag, dl + +#define MACRO_NEIGHBOUR_FLAGS_TERRAIN_8(ID, MASK) \ + __asm mov eax, this \ + __asm mov edi, dword ptr [eax]TileMapState.ptr_MovementDirectionTranslationMatrix \ + __asm mov esi, dword ptr [eax]TileMapState.ptr_TerrainTypeTileMap \ + __asm mov eax, dword ptr [eax]TileMapState.DAT_SomeTile \ + __asm add esi, eax \ + __asm push esi \ + __asm mov eax, this \ + __asm mov eax, dword ptr [eax]TileMapState.DAT_SomeY \ + __asm shl eax, 0x5 \ + __asm add edi, eax \ + __asm mov edx, 0x0 \ + __asm mov eax, dword ptr [esi + 0x1] \ + __asm mov ebx, dword ptr [esi - 0x1] \ + __asm and eax, MASK \ + MACRO_ASM_JZ_NEAR(3) \ + __asm or edx, 0x20 \ + __asm east_done_##ID: \ + __asm and ebx, MASK \ + MACRO_ASM_JZ_NEAR(3) \ + __asm or edx, 0x2 \ + __asm west_done_##ID: \ + __asm mov ecx, dword ptr [edi] \ + __asm add esi, ecx \ + __asm mov eax, dword ptr [esi - 0x1] \ + __asm mov ebx, dword ptr [esi + 0x1] \ + __asm mov ecx, dword ptr [esi] \ + __asm and eax, MASK \ + MACRO_ASM_JZ_NEAR(3) \ + __asm or edx, 0x1 \ + __asm north_west_done_##ID: \ + __asm and ebx, MASK \ + MACRO_ASM_JZ_NEAR(3) \ + __asm or edx, 0x40 \ + __asm north_east_done_##ID: \ + __asm and ecx, MASK \ + MACRO_ASM_JZ_NEAR(6) \ + __asm or edx, 0x80 \ + __asm north_done_##ID: \ + __asm mov ecx, dword ptr [edi + 0x10] \ + __asm pop esi \ + __asm add esi, ecx \ + __asm mov eax, dword ptr [esi - 0x1] \ + __asm mov ebx, dword ptr [esi + 0x1] \ + __asm mov ecx, dword ptr [esi] \ + __asm and eax, MASK \ + MACRO_ASM_JZ_NEAR(3) \ + __asm or edx, 0x4 \ + __asm south_west_done_##ID: \ + __asm and ebx, MASK \ + MACRO_ASM_JZ_NEAR(3) \ + __asm or edx, 0x10 \ + __asm south_east_done_##ID: \ + __asm and ecx, MASK \ + MACRO_ASM_JZ_NEAR(3) \ + __asm or edx, 0x8 \ + __asm south_done_##ID: \ + __asm mov eax, this \ + __asm mov byte ptr [eax]TileMapState.bitFlag, dl + +/* + A second handwritten macro: mark DAT_SomeTile and its eight neighbours as changed (2) in the + changed layer. It appears verbatim in createPlateau (0x00501F50), useLevelBrush (0x00502110) and + useTerrainHeightBrush (0x00502680), each time right after DAT_SomeTile and DAT_SomeY are stored. + Same tells as above: a bare push/pop of esi around the north row, and the fields read through the + global instance's absolute address. + + Clobbers eax, ecx, edx and esi. "this" is loaded once into ecx (which the original does not need) + so that no named operand is resolved while the pushed esi is on the stack. +*/ +#define MACRO_MARK_CHANGED_NEIGHBOURS() \ + __asm mov ecx, this \ + __asm mov esi, dword ptr [ecx]TileMapState.ptr_ChangedLayer \ + __asm mov eax, dword ptr [ecx]TileMapState.DAT_SomeTile \ + __asm add esi, eax \ + __asm push esi \ + __asm mov edx, dword ptr [ecx]TileMapState.ptr_MovementDirectionTranslationMatrix \ + __asm mov eax, dword ptr [ecx]TileMapState.DAT_SomeY \ + __asm shl eax, 0x5 \ + __asm add edx, eax \ + __asm mov byte ptr [esi + 0x1], 0x2 \ + __asm mov byte ptr [esi - 0x1], 0x2 \ + __asm mov byte ptr [esi], 0x2 \ + __asm mov ecx, dword ptr [edx] \ + __asm add esi, ecx \ + __asm mov byte ptr [esi - 0x1], 0x2 \ + __asm mov byte ptr [esi + 0x1], 0x2 \ + __asm mov byte ptr [esi], 0x2 \ + __asm mov ecx, dword ptr [edx + 0x10] \ + __asm pop esi \ + __asm add esi, ecx \ + __asm mov byte ptr [esi - 0x1], 0x2 \ + __asm mov byte ptr [esi + 0x1], 0x2 \ + __asm mov byte ptr [esi], 0x2 + +// clang-format on diff --git a/src/OpenSHC/Map/TileMapState/NeighbourFlagsGlobalAsm.hpp b/src/OpenSHC/Map/TileMapState/NeighbourFlagsGlobalAsm.hpp new file mode 100644 index 0000000000..703f87fdc3 --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/NeighbourFlagsGlobalAsm.hpp @@ -0,0 +1,334 @@ +/** + HAND WRITTEN - not generated. + + path: 'OpenSHC/Map/TileMapState/NeighbourFlagsGlobalAsm.hpp' + + The same handwritten-assembly macros as NeighbourFlagsAsm.hpp (see there for what they compute, the + five shapes, the bitFlag bits and the ID/MASK rules), but reaching TileMapState's fields through the + global instance's absolute address, as the original does at every place they occur: + + mov edi, dword ptr [0x1fe7c70] ; DAT_TileMapState.ptr_MovementDirectionTranslationMatrix + ... + mov byte ptr [0x1fe7c48], dl ; DAT_TileMapState.bitFlag + + NeighbourFlagsAsm.hpp goes through "this" instead, at the cost of a "mov eax, this" per field access. + It is kept unchanged so the functions measured against it are not disturbed. + + MSVC inline asm accepts the resolver instance as a bracketed operand, + "[DAT_TileMapState::instance]TileMapState.field", but only after the static member has been odr-used + from C++ in the same function; otherwise it reports C2143/C2059. Every macro therefore starts with a + "(void)&DAT_TileMapState::instance;" statement, which emits no code. Use the macros as statements. + + The neighbour macros clobber eax, ebx, ecx, edx, esi and edi and leave the result in + DAT_TileMapState::instance.bitFlag; MACRO_MARK_CHANGED_NEIGHBOURS_GLOBAL clobbers eax, ecx, edx and esi. +*/ + +#pragma once + +#include "OpenSHC/Map/TileMapState.hpp" + +#include "OpenSHC/Globals/DAT_TileMapState.hpp" + +// A "jz" over the "or edx, imm" that follows it (SKIP = 3 for an imm8, 6 for an imm32), in the 6-byte near +// encoding 0F 84 rel32 that the original's assembler produced. MSVC's inline assembler only emits the 2-byte short +// form for a jump to a nearby label, even when written "jz near ptr label", so the bytes are emitted directly. +#ifndef MACRO_ASM_JZ_NEAR +#define MACRO_ASM_JZ_NEAR(SKIP) \ + __asm _emit 0x0F __asm _emit 0x84 __asm _emit SKIP __asm _emit 0 __asm _emit 0 __asm _emit 0 +#endif + +// clang-format off + +#define MACRO_NEIGHBOUR_FLAGS_GLOBAL_LOGIC_4(ID, MASK) \ + (void)&DAT_TileMapState::instance; \ + __asm mov edi, dword ptr [DAT_TileMapState::instance]TileMapState.ptr_MovementDirectionTranslationMatrix \ + __asm mov esi, dword ptr [DAT_TileMapState::instance]TileMapState.ptr_LogicLayer \ + __asm mov eax, dword ptr [DAT_TileMapState::instance]TileMapState.DAT_SomeTile \ + __asm shl eax, 0x2 \ + __asm add esi, eax \ + __asm push esi \ + __asm mov eax, dword ptr [DAT_TileMapState::instance]TileMapState.DAT_SomeY \ + __asm shl eax, 0x5 \ + __asm add edi, eax \ + __asm mov edx, 0x0 \ + __asm mov eax, dword ptr [esi + 0x4] \ + __asm mov ebx, dword ptr [esi - 0x4] \ + __asm and eax, MASK \ + MACRO_ASM_JZ_NEAR(3) \ + __asm or edx, 0x20 \ + __asm east_done_##ID: \ + __asm and ebx, MASK \ + MACRO_ASM_JZ_NEAR(3) \ + __asm or edx, 0x2 \ + __asm west_done_##ID: \ + __asm mov ecx, dword ptr [edi] \ + __asm add esi, ecx \ + __asm add esi, ecx \ + __asm add esi, ecx \ + __asm add esi, ecx \ + __asm mov eax, dword ptr [esi] \ + __asm and eax, MASK \ + MACRO_ASM_JZ_NEAR(6) \ + __asm or edx, 0x80 \ + __asm north_done_##ID: \ + __asm mov ecx, dword ptr [edi + 0x10] \ + __asm pop esi \ + __asm add esi, ecx \ + __asm add esi, ecx \ + __asm add esi, ecx \ + __asm add esi, ecx \ + __asm mov eax, dword ptr [esi] \ + __asm and eax, MASK \ + MACRO_ASM_JZ_NEAR(3) \ + __asm or edx, 0x8 \ + __asm south_done_##ID: \ + __asm mov byte ptr [DAT_TileMapState::instance]TileMapState.bitFlag, dl + +#define MACRO_NEIGHBOUR_FLAGS_GLOBAL_LOGIC_8(ID, MASK) \ + (void)&DAT_TileMapState::instance; \ + __asm mov edi, dword ptr [DAT_TileMapState::instance]TileMapState.ptr_MovementDirectionTranslationMatrix \ + __asm mov esi, dword ptr [DAT_TileMapState::instance]TileMapState.ptr_LogicLayer \ + __asm mov eax, dword ptr [DAT_TileMapState::instance]TileMapState.DAT_SomeTile \ + __asm shl eax, 0x2 \ + __asm add esi, eax \ + __asm push esi \ + __asm mov eax, dword ptr [DAT_TileMapState::instance]TileMapState.DAT_SomeY \ + __asm shl eax, 0x5 \ + __asm add edi, eax \ + __asm mov edx, 0x0 \ + __asm mov eax, dword ptr [esi + 0x4] \ + __asm mov ebx, dword ptr [esi - 0x4] \ + __asm and eax, MASK \ + MACRO_ASM_JZ_NEAR(3) \ + __asm or edx, 0x20 \ + __asm east_done_##ID: \ + __asm and ebx, MASK \ + MACRO_ASM_JZ_NEAR(3) \ + __asm or edx, 0x2 \ + __asm west_done_##ID: \ + __asm mov ecx, dword ptr [edi] \ + __asm add esi, ecx \ + __asm add esi, ecx \ + __asm add esi, ecx \ + __asm add esi, ecx \ + __asm mov eax, dword ptr [esi - 0x4] \ + __asm mov ebx, dword ptr [esi + 0x4] \ + __asm mov ecx, dword ptr [esi] \ + __asm and eax, MASK \ + MACRO_ASM_JZ_NEAR(3) \ + __asm or edx, 0x1 \ + __asm north_west_done_##ID: \ + __asm and ebx, MASK \ + MACRO_ASM_JZ_NEAR(3) \ + __asm or edx, 0x40 \ + __asm north_east_done_##ID: \ + __asm and ecx, MASK \ + MACRO_ASM_JZ_NEAR(6) \ + __asm or edx, 0x80 \ + __asm north_done_##ID: \ + __asm mov ecx, dword ptr [edi + 0x10] \ + __asm pop esi \ + __asm add esi, ecx \ + __asm add esi, ecx \ + __asm add esi, ecx \ + __asm add esi, ecx \ + __asm mov eax, dword ptr [esi - 0x4] \ + __asm mov ebx, dword ptr [esi + 0x4] \ + __asm mov ecx, dword ptr [esi] \ + __asm and eax, MASK \ + MACRO_ASM_JZ_NEAR(3) \ + __asm or edx, 0x4 \ + __asm south_west_done_##ID: \ + __asm and ebx, MASK \ + MACRO_ASM_JZ_NEAR(3) \ + __asm or edx, 0x10 \ + __asm south_east_done_##ID: \ + __asm and ecx, MASK \ + MACRO_ASM_JZ_NEAR(3) \ + __asm or edx, 0x8 \ + __asm south_done_##ID: \ + __asm mov byte ptr [DAT_TileMapState::instance]TileMapState.bitFlag, dl + +#define MACRO_NEIGHBOUR_FLAGS_GLOBAL_MISC_8(ID, MASK) \ + (void)&DAT_TileMapState::instance; \ + __asm mov edi, dword ptr [DAT_TileMapState::instance]TileMapState.ptr_MovementDirectionTranslationMatrix \ + __asm mov esi, dword ptr [DAT_TileMapState::instance]TileMapState.ptr_MiscDisplayLayer \ + __asm mov eax, dword ptr [DAT_TileMapState::instance]TileMapState.DAT_SomeTile \ + __asm shl eax, 0x1 \ + __asm add esi, eax \ + __asm push esi \ + __asm mov eax, dword ptr [DAT_TileMapState::instance]TileMapState.DAT_SomeY \ + __asm shl eax, 0x5 \ + __asm add edi, eax \ + __asm mov edx, 0x0 \ + __asm mov eax, dword ptr [esi + 0x2] \ + __asm mov ebx, dword ptr [esi - 0x2] \ + __asm and eax, MASK \ + MACRO_ASM_JZ_NEAR(3) \ + __asm or edx, 0x20 \ + __asm east_done_##ID: \ + __asm and ebx, MASK \ + MACRO_ASM_JZ_NEAR(3) \ + __asm or edx, 0x2 \ + __asm west_done_##ID: \ + __asm mov ecx, dword ptr [edi] \ + __asm add esi, ecx \ + __asm add esi, ecx \ + __asm mov eax, dword ptr [esi - 0x2] \ + __asm mov ebx, dword ptr [esi + 0x2] \ + __asm mov ecx, dword ptr [esi] \ + __asm and eax, MASK \ + MACRO_ASM_JZ_NEAR(3) \ + __asm or edx, 0x1 \ + __asm north_west_done_##ID: \ + __asm and ebx, MASK \ + MACRO_ASM_JZ_NEAR(3) \ + __asm or edx, 0x40 \ + __asm north_east_done_##ID: \ + __asm and ecx, MASK \ + MACRO_ASM_JZ_NEAR(6) \ + __asm or edx, 0x80 \ + __asm north_done_##ID: \ + __asm mov ecx, dword ptr [edi + 0x10] \ + __asm pop esi \ + __asm add esi, ecx \ + __asm add esi, ecx \ + __asm mov eax, dword ptr [esi - 0x2] \ + __asm mov ebx, dword ptr [esi + 0x2] \ + __asm mov ecx, dword ptr [esi] \ + __asm and eax, MASK \ + MACRO_ASM_JZ_NEAR(3) \ + __asm or edx, 0x4 \ + __asm south_west_done_##ID: \ + __asm and ebx, MASK \ + MACRO_ASM_JZ_NEAR(3) \ + __asm or edx, 0x10 \ + __asm south_east_done_##ID: \ + __asm and ecx, MASK \ + MACRO_ASM_JZ_NEAR(3) \ + __asm or edx, 0x8 \ + __asm south_done_##ID: \ + __asm mov byte ptr [DAT_TileMapState::instance]TileMapState.bitFlag, dl + +#define MACRO_NEIGHBOUR_FLAGS_GLOBAL_TERRAIN_4(ID, MASK) \ + (void)&DAT_TileMapState::instance; \ + __asm mov edi, dword ptr [DAT_TileMapState::instance]TileMapState.ptr_MovementDirectionTranslationMatrix \ + __asm mov esi, dword ptr [DAT_TileMapState::instance]TileMapState.ptr_TerrainTypeTileMap \ + __asm mov eax, dword ptr [DAT_TileMapState::instance]TileMapState.DAT_SomeTile \ + __asm add esi, eax \ + __asm push esi \ + __asm mov eax, dword ptr [DAT_TileMapState::instance]TileMapState.DAT_SomeY \ + __asm shl eax, 0x5 \ + __asm add edi, eax \ + __asm mov edx, 0x0 \ + __asm mov eax, dword ptr [esi + 0x1] \ + __asm mov ebx, dword ptr [esi - 0x1] \ + __asm and eax, MASK \ + MACRO_ASM_JZ_NEAR(3) \ + __asm or edx, 0x20 \ + __asm east_done_##ID: \ + __asm and ebx, MASK \ + MACRO_ASM_JZ_NEAR(3) \ + __asm or edx, 0x2 \ + __asm west_done_##ID: \ + __asm mov ecx, dword ptr [edi] \ + __asm add esi, ecx \ + __asm mov eax, dword ptr [esi] \ + __asm and eax, MASK \ + MACRO_ASM_JZ_NEAR(6) \ + __asm or edx, 0x80 \ + __asm north_done_##ID: \ + __asm mov ecx, dword ptr [edi + 0x10] \ + __asm pop esi \ + __asm add esi, ecx \ + __asm mov eax, dword ptr [esi] \ + __asm and eax, MASK \ + MACRO_ASM_JZ_NEAR(3) \ + __asm or edx, 0x8 \ + __asm south_done_##ID: \ + __asm mov byte ptr [DAT_TileMapState::instance]TileMapState.bitFlag, dl + +#define MACRO_NEIGHBOUR_FLAGS_GLOBAL_TERRAIN_8(ID, MASK) \ + (void)&DAT_TileMapState::instance; \ + __asm mov edi, dword ptr [DAT_TileMapState::instance]TileMapState.ptr_MovementDirectionTranslationMatrix \ + __asm mov esi, dword ptr [DAT_TileMapState::instance]TileMapState.ptr_TerrainTypeTileMap \ + __asm mov eax, dword ptr [DAT_TileMapState::instance]TileMapState.DAT_SomeTile \ + __asm add esi, eax \ + __asm push esi \ + __asm mov eax, dword ptr [DAT_TileMapState::instance]TileMapState.DAT_SomeY \ + __asm shl eax, 0x5 \ + __asm add edi, eax \ + __asm mov edx, 0x0 \ + __asm mov eax, dword ptr [esi + 0x1] \ + __asm mov ebx, dword ptr [esi - 0x1] \ + __asm and eax, MASK \ + MACRO_ASM_JZ_NEAR(3) \ + __asm or edx, 0x20 \ + __asm east_done_##ID: \ + __asm and ebx, MASK \ + MACRO_ASM_JZ_NEAR(3) \ + __asm or edx, 0x2 \ + __asm west_done_##ID: \ + __asm mov ecx, dword ptr [edi] \ + __asm add esi, ecx \ + __asm mov eax, dword ptr [esi - 0x1] \ + __asm mov ebx, dword ptr [esi + 0x1] \ + __asm mov ecx, dword ptr [esi] \ + __asm and eax, MASK \ + MACRO_ASM_JZ_NEAR(3) \ + __asm or edx, 0x1 \ + __asm north_west_done_##ID: \ + __asm and ebx, MASK \ + MACRO_ASM_JZ_NEAR(3) \ + __asm or edx, 0x40 \ + __asm north_east_done_##ID: \ + __asm and ecx, MASK \ + MACRO_ASM_JZ_NEAR(6) \ + __asm or edx, 0x80 \ + __asm north_done_##ID: \ + __asm mov ecx, dword ptr [edi + 0x10] \ + __asm pop esi \ + __asm add esi, ecx \ + __asm mov eax, dword ptr [esi - 0x1] \ + __asm mov ebx, dword ptr [esi + 0x1] \ + __asm mov ecx, dword ptr [esi] \ + __asm and eax, MASK \ + MACRO_ASM_JZ_NEAR(3) \ + __asm or edx, 0x4 \ + __asm south_west_done_##ID: \ + __asm and ebx, MASK \ + MACRO_ASM_JZ_NEAR(3) \ + __asm or edx, 0x10 \ + __asm south_east_done_##ID: \ + __asm and ecx, MASK \ + MACRO_ASM_JZ_NEAR(3) \ + __asm or edx, 0x8 \ + __asm south_done_##ID: \ + __asm mov byte ptr [DAT_TileMapState::instance]TileMapState.bitFlag, dl + +#define MACRO_MARK_CHANGED_NEIGHBOURS_GLOBAL() \ + (void)&DAT_TileMapState::instance; \ + __asm mov esi, dword ptr [DAT_TileMapState::instance]TileMapState.ptr_ChangedLayer \ + __asm mov eax, dword ptr [DAT_TileMapState::instance]TileMapState.DAT_SomeTile \ + __asm add esi, eax \ + __asm push esi \ + __asm mov edx, dword ptr [DAT_TileMapState::instance]TileMapState.ptr_MovementDirectionTranslationMatrix \ + __asm mov eax, dword ptr [DAT_TileMapState::instance]TileMapState.DAT_SomeY \ + __asm shl eax, 0x5 \ + __asm add edx, eax \ + __asm mov byte ptr [esi + 0x1], 0x2 \ + __asm mov byte ptr [esi - 0x1], 0x2 \ + __asm mov byte ptr [esi], 0x2 \ + __asm mov ecx, dword ptr [edx] \ + __asm add esi, ecx \ + __asm mov byte ptr [esi - 0x1], 0x2 \ + __asm mov byte ptr [esi + 0x1], 0x2 \ + __asm mov byte ptr [esi], 0x2 \ + __asm mov ecx, dword ptr [edx + 0x10] \ + __asm pop esi \ + __asm add esi, ecx \ + __asm mov byte ptr [esi - 0x1], 0x2 \ + __asm mov byte ptr [esi + 0x1], 0x2 \ + __asm mov byte ptr [esi], 0x2 + +// clang-format on diff --git a/src/OpenSHC/Map/TileMapState/applyTreeBrushToLogicalLayer.cpp b/src/OpenSHC/Map/TileMapState/applyTreeBrushToLogicalLayer.cpp new file mode 100644 index 0000000000..c2e967b8a7 --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/applyTreeBrushToLogicalLayer.cpp @@ -0,0 +1,53 @@ +#include "../TileMapState.func.hpp" + +#include "OpenSHC/Map/LandscapeState.func.hpp" +#include "OpenSHC/Map/Navigation/PathFindingState.func.hpp" + +#include "OpenSHC/Globals/DAT_LandscapeState.hpp" +#include "OpenSHC/Globals/DAT_PathFindingState.hpp" +#include "OpenSHC/Globals/DAT_TerrainDefinedData.hpp" +#include "OpenSHC/Globals/DAT_ViewportRenderState.hpp" + +namespace OpenSHC { +namespace Map { + + using OpenSHC::Map::LogicHelpers::L_BORDER; + using OpenSHC::Map::LogicHelpers::L_MARSH; + using OpenSHC::Map::LogicHelpers::L_MOAT; + using OpenSHC::Map::LogicHelpers::L_SEA; + using OpenSHC::Map::LogicHelpers::L_TREE_VARIATION; + + // FUNCTION: STRONGHOLDCRUSADER 0x00508DD0 + void TileMapState::applyTreeBrushToLogicalLayer(int treeID, int noVariation) + { + int const brushSize = DAT_TerrainDefinedData::instance.BrushSizeArray[MACRO_CALL_MEMBER( + OpenSHC::Map::LandscapeState_Func::getValueFrom0UpTo3ForTreeTypeAndTreeStage, DAT_LandscapeState::ptr)( + DAT_LandscapeState::instance.trees[treeID].treeType, DAT_LandscapeState::instance.trees[treeID].stage)]; + int tile = DAT_LandscapeState::instance.trees[treeID].xPosition + + DAT_ViewportRenderState::instance.translationMatrix[DAT_LandscapeState::instance.trees[treeID].yPosition] + .addXgetTile; + int brushY = DAT_LandscapeState::instance.trees[treeID].yPosition; + int const baseTile = tile; + int const baseY = brushY; + for (int tileIndex = 0; tileIndex < brushSize; tileIndex++) { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::getTileForBrush, this)( + 1, tileIndex, &tile, &brushY, baseTile, baseY); + if ((this->LogicLayer[tile] & L_BORDER) != 0) { + break; + } + if ((this->LogicLayer[tile] & (L_SEA | L_MARSH | L_MOAT)) != 0) { + continue; + } + if (noVariation != 0) { + this->LogicLayer[tile] &= ~L_TREE_VARIATION; + } else { + this->LogicLayer[tile] |= L_TREE_VARIATION; + } + } + MACRO_CALL_MEMBER( + OpenSHC::Map::Navigation::PathFindingState_Func::updateWalkAndPathLayer, DAT_PathFindingState::ptr)(7, + DAT_LandscapeState::instance.trees[treeID].xPosition, DAT_LandscapeState::instance.trees[treeID].yPosition); + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/checkBuildingCanBePlacedHere.cpp b/src/OpenSHC/Map/TileMapState/checkBuildingCanBePlacedHere.cpp new file mode 100644 index 0000000000..9d81eadb84 --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/checkBuildingCanBePlacedHere.cpp @@ -0,0 +1,775 @@ +#include "../TileMapState.func.hpp" + +#include "OpenSHC/Map/Buildings/BuildingsState.func.hpp" +#include "OpenSHC/Map/Navigation/PathFindingState.func.hpp" +#include "OpenSHC/Rendering/ViewportRenderState.func.hpp" +#include "OpenSHC/Synchrony/GameSynchronyState.func.hpp" +#include "OpenSHC/Commands/MappersEnum.hpp" +#include "OpenSHC/Game/GameMode.hpp" +#include "OpenSHC/Game/GameMode2.hpp" +#include "OpenSHC/Game/Player/Enums/BuildingEntryInfoEnum.hpp" +#include "OpenSHC/Map/Buildings/BuildingFailReasonEnum.hpp" +#include "OpenSHC/Map/Buildings/BuildingType.hpp" +#include "OpenSHC/WindowsHelper/Enums/BOOLEnum.hpp" + +#include "OpenSHC/Globals/DAT_BuildingDefinedData.hpp" +#include "OpenSHC/Globals/DAT_BuildingsState.hpp" +#include "OpenSHC/Globals/DAT_GameCore.hpp" +#include "OpenSHC/Globals/DAT_GameState.hpp" +#include "OpenSHC/Globals/DAT_GameSynchronyState.hpp" +#include "OpenSHC/Globals/DAT_PathFindingState.hpp" +#include "OpenSHC/Globals/DAT_TerrainDefinedData.hpp" +#include "OpenSHC/Globals/DAT_ViewportRenderState.hpp" + +namespace OpenSHC { +namespace Map { + + using OpenSHC::Map::Buildings::BuildingFailReasonEnum; + using OpenSHC::Map::LogicHelpers::L2_OASIS_GRASS; + using OpenSHC::Map::LogicHelpers::L2_SCRUB; + using OpenSHC::Map::LogicHelpers::L2_THICK_SCRUB; + using OpenSHC::Map::LogicHelpers::L_BORDER; + using OpenSHC::Map::LogicHelpers::L_BORDER_EDGE; + using OpenSHC::Map::LogicHelpers::L_BOULDERS; + using OpenSHC::Map::LogicHelpers::L_IRON; + using OpenSHC::Map::LogicHelpers::L_OIL; + using OpenSHC::Map::LogicHelpers::L_RIVER; + + // FUNCTION: STRONGHOLDCRUSADER 0x005037B0 + void TileMapState::checkBuildingCanBePlacedHere( + int playerID, uint x, uint y, MappersEnum commandBuildingType, int buildingSize) + { + int tileIndex; + uint const startX = x; + uint const startY = y; + int keepRangeBonus = 0; + BOOLEnum sizeDefaulted = FALSE; + if (buildingSize < 0) { + buildingSize = 2; + sizeDefaulted = TRUE; + } + this->buildingPlacementFail = FALSE; + this->placementWarning = 0; + this->field_0x554944 = 0; + this->uiBuildingRotation = 15; + MappersEnum const command = (MappersEnum)(short)commandBuildingType; + this->buildingSpriteSheetID_1 + = DAT_BuildingDefinedData::instance + .Building_SpriteSheet_ID_Array_1[MACRO_CALL_MEMBER( + OpenSHC::Map::Buildings::BuildingsState_Func::convertCommandBuildingTypeToBuildingType, + DAT_BuildingsState::ptr)(command)] + .intValue; + this->buildingSpriteID1 = MACRO_CALL_MEMBER( + OpenSHC::Map::Buildings::BuildingsState_Func::getSpriteID, DAT_BuildingsState::ptr)(command); + this->buildingSpriteID2 = MACRO_CALL_MEMBER( + OpenSHC::Map::Buildings::BuildingsState_Func::getSpriteID2, DAT_BuildingsState::ptr)(command); + this->buildingHeightLimit = DAT_BuildingDefinedData::instance.BuildingPlacement_HeightLimit[MACRO_CALL_MEMBER( + OpenSHC::Map::Buildings::BuildingsState_Func::convertCommandBuildingTypeToBuildingType, + DAT_BuildingsState::ptr)(command)]; + this->buildingMaxHeightDifference + = DAT_BuildingDefinedData::instance.BuildingPlacement_MaxHeightDifference[MACRO_CALL_MEMBER( + OpenSHC::Map::Buildings::BuildingsState_Func::convertCommandBuildingTypeToBuildingType, + DAT_BuildingsState::ptr)(command)]; + this->buildingPlacementProperty_3 + = DAT_BuildingDefinedData::instance.BuildingPlacement_Property_3[MACRO_CALL_MEMBER( + OpenSHC::Map::Buildings::BuildingsState_Func::convertCommandBuildingTypeToBuildingType, + DAT_BuildingsState::ptr)(command)]; + this->buildingPlacementProperty_4 + = DAT_BuildingDefinedData::instance.BuildingPlacement_Property_4[MACRO_CALL_MEMBER( + OpenSHC::Map::Buildings::BuildingsState_Func::convertCommandBuildingTypeToBuildingType, + DAT_BuildingsState::ptr)(command)]; + if (DAT_GameSynchronyState::instance.currentGameMode == OpenSHC::Game::GM_SOLITARY + || DAT_GameSynchronyState::instance.currentPlayerFullIDArray[playerID] != -1 + || DAT_GameSynchronyState::instance.currentAIArray[playerID] == 0) { + this->buildingPlacementProperty_4 = 0; + } + this->buildingPlacementProperty_5 + = DAT_BuildingDefinedData::instance.BuildingPlacement_Property_5[MACRO_CALL_MEMBER( + OpenSHC::Map::Buildings::BuildingsState_Func::convertCommandBuildingTypeToBuildingType, + DAT_BuildingsState::ptr)(command)]; + this->buildingPlacementProperty_6 + = DAT_BuildingDefinedData::instance.BuildingPlacement_Property_6[MACRO_CALL_MEMBER( + OpenSHC::Map::Buildings::BuildingsState_Func::convertCommandBuildingTypeToBuildingType, + DAT_BuildingsState::ptr)(command)]; + this->buildingPlacementProperty_7 + = DAT_BuildingDefinedData::instance.BuildingPlacement_Property_7[MACRO_CALL_MEMBER( + OpenSHC::Map::Buildings::BuildingsState_Func::convertCommandBuildingTypeToBuildingType, + DAT_BuildingsState::ptr)(command)]; + if ((short)commandBuildingType == OpenSHC::Commands::M_MAPPER_SIEGE_TOWER_BASE) { + return; + } + int boulderCount = 0; + int ironCount = 0; + int oilCount = 0; + int fertileCount = 0; + int grassCount = 0; + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::storeMinAndMaxHeightOfArea, this)(x, y, buildingSize); + if (!sizeDefaulted) { + BOOLEnum checkSuitability = TRUE; + switch ((short)commandBuildingType) { + case OpenSHC::Commands::M_MAPPER_WOODSMAN: + case OpenSHC::Commands::M_MAPPER_OXENBASE: + case OpenSHC::Commands::M_MAPPER_QUARRY: + case OpenSHC::Commands::M_MAPPER_TUNNEL: + case OpenSHC::Commands::M_MAPPER_TUNNEL_CONSTRUCTION: + case OpenSHC::Commands::M_MAPPER_WHEATFARM: + case OpenSHC::Commands::M_MAPPER_HOPSFARM: + case OpenSHC::Commands::M_MAPPER_APPLEFARM: + case OpenSHC::Commands::M_MAPPER_CATTLEFARM: + case OpenSHC::Commands::M_MAPPER_IRON_MINE: + case OpenSHC::Commands::M_MAPPER_PITCH_WORKINGS: + case OpenSHC::Commands::M_MAPPER_QUARRYPILE: + case OpenSHC::Commands::M_MAPPER_POND1: + case OpenSHC::Commands::M_MAPPER_POND2_SMALL: + case OpenSHC::Commands::M_MAPPER_POND3_LARGE1: + case OpenSHC::Commands::M_MAPPER_POND4_LARGE2: + // resource buildings and ponds skip the enemy and suitability checks + checkSuitability = FALSE; + break; + case OpenSHC::Commands::M_MAPPER_STORES: + case OpenSHC::Commands::M_MAPPER_GRANARY: + case OpenSHC::Commands::M_MAPPER_ARMOURY: + if (DAT_GameSynchronyState::instance.currentGameMode != OpenSHC::Game::GM_SOLITARY) { + tileIndex = 0; + do { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::getBuildingSizeIndexMappingData, this)( + tileIndex, buildingSize); + if (MACRO_CALL_MEMBER(OpenSHC::Map::Navigation::PathFindingState_Func::isEnemyTooCloseUnk, + DAT_PathFindingState::ptr)(playerID, this->buildingX + x, this->buildingY + y, 3)) { + this->buildingPlacementFailReason = OpenSHC::Map::Buildings::BRE_UNKNOWN_17; + this->buildingPlacementFail = TRUE; + break; + } + tileIndex++; + } while (tileIndex < this->constructionTileCount); + } + break; + case OpenSHC::Commands::M_MAPPER_DOG_CAGE: + if (DAT_GameState::instance.playerDataArray[playerID].dogCageCount >= 10) { + this->buildingPlacementFail = TRUE; + this->buildingPlacementFailReason = OpenSHC::Map::Buildings::BRE_UNKNOWN_1000; + return; + } + // fall through + case OpenSHC::Commands::M_MAPPER_KILLING_PIT: + case OpenSHC::Commands::M_MAPPER_PITCH_DITCH: + case OpenSHC::Commands::M_MAPPER_DRAWBRIDGE: + keepRangeBonus = 5; + // fall through + case OpenSHC::Commands::M_MAPPER_GATEHOUSE: + case OpenSHC::Commands::M_MAPPER_GATE_MAIN: + case OpenSHC::Commands::M_MAPPER_GATE_INNER: + case OpenSHC::Commands::M_MAPPER_GATE_WOOD: + case OpenSHC::Commands::M_MAPPER_GATE_POSTERN: + case OpenSHC::Commands::M_MAPPER_MOAT: + case OpenSHC::Commands::M_MAPPER_ANTIMOAT: + case OpenSHC::Commands::M_MAPPER_TOWER1: + case OpenSHC::Commands::M_MAPPER_TOWER2: + case OpenSHC::Commands::M_MAPPER_TOWER3: + case OpenSHC::Commands::M_MAPPER_TOWER4: + case OpenSHC::Commands::M_MAPPER_TOWER5: + 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_GATE_STONE1A: + case OpenSHC::Commands::M_MAPPER_GATE_STONE1B: + case OpenSHC::Commands::M_MAPPER_GATE_STONE2A: + case OpenSHC::Commands::M_MAPPER_GATE_STONE2B: + if (DAT_GameCore::instance.gameMode_2 != OpenSHC::Game::GM_EDITOR) { + if (DAT_GameCore::instance.gameMode_2 == OpenSHC::Game::GM_SIEGE_THAT) { + break; + } + int distance; + int range; + if (DAT_GameSynchronyState::instance.currentGameMode != OpenSHC::Game::GM_SOLITARY) { + if (MACRO_CALL_MEMBER(OpenSHC::Synchrony::GameSynchronyState_Func::isAIPlayer, + DAT_GameSynchronyState::ptr)(playerID)) { + distance = 15; + range = 7; + } else { + distance = 15; + range = 7; + } + } else { + distance = 30; + range = 30; + } + BOOLEnum siteClear; + tileIndex = 0; + do { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::getBuildingSizeIndexMappingData, this)( + tileIndex, buildingSize); + if (MACRO_CALL_MEMBER(OpenSHC::Map::Navigation::PathFindingState_Func::isEnemyTooCloseUnk, + DAT_PathFindingState::ptr)( + playerID, this->buildingX + x, this->buildingY + y, distance)) { + this->buildingPlacementFailReason = OpenSHC::Map::Buildings::BRE_UNKNOWN_17; + siteClear = FALSE; + break; + } + if (DAT_GameSynchronyState::instance.currentGameMode != OpenSHC::Game::GM_SOLITARY) { + if (MACRO_CALL_MEMBER( + OpenSHC::Map::Navigation::PathFindingState_Func::isOpponentBuildingInRange, + DAT_PathFindingState::ptr)( + playerID, this->buildingX + x, this->buildingY + y, range, -1, -1, -1)) { + this->buildingPlacementFailReason = OpenSHC::Map::Buildings::BRE_UNKNOWN_17; + siteClear = FALSE; + break; + } + if (MACRO_CALL_MEMBER( + OpenSHC::Map::Navigation::PathFindingState_Func::isTileInRangeOfKeepRange, + DAT_PathFindingState::ptr)(playerID, this->buildingX + x, this->buildingY + y, + MACRO_CALL_MEMBER( + OpenSHC::Map::TileMapState_Func::getCastleBuildRangeForMapSize, this)() + + keepRangeBonus)) { + this->buildingPlacementFailReason = OpenSHC::Map::Buildings::BRE_UNKNOWN_18; + siteClear = FALSE; + break; + } + siteClear = TRUE; + } else { + siteClear = TRUE; + } + tileIndex++; + } while (tileIndex < this->constructionTileCount); + if (MACRO_CALL_MEMBER(OpenSHC::Map::Navigation::PathFindingState_Func::isSignPostWithinDistance, + DAT_PathFindingState::ptr)( + x, y, DAT_GameState::instance.mapAndTime.unk_signpostDistance + 5)) { + this->buildingPlacementFailReason = OpenSHC::Map::Buildings::BRE_UNKNOWN_21; + } else if (siteClear) { + break; + } + this->buildingPlacementFail = TRUE; + if (this->buildingPlacementFailReason + == OpenSHC::Map::Buildings::BFRE_DEFAULT_CANT_PLACE_THAT_THERE) { + this->buildingPlacementFailReason = OpenSHC::Map::Buildings::BRE_UNKNOWN_18; + } + } else { + tileIndex = 0; + do { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::getBuildingSizeIndexMappingData, this)( + tileIndex, buildingSize); + if (MACRO_CALL_MEMBER( + OpenSHC::Map::Navigation::PathFindingState_Func::isOpponentBuildingInRange, + DAT_PathFindingState::ptr)( + playerID, this->buildingX + x, this->buildingY + y, 7, -1, -1, -1)) { + this->buildingPlacementFailReason = OpenSHC::Map::Buildings::BRE_UNKNOWN_17; + this->buildingPlacementFail = TRUE; + break; + } + tileIndex++; + } while (tileIndex < this->constructionTileCount); + } + break; + 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_ARAB_BALLISTA: + if (DAT_GameCore::instance.gameMode_2 != OpenSHC::Game::GM_EDITOR + && DAT_GameCore::instance.gameMode_2 != OpenSHC::Game::GM_SIEGE_THAT) { + tileIndex = 0; + do { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::getBuildingSizeIndexMappingData, this)( + tileIndex, buildingSize); + if (MACRO_CALL_MEMBER(OpenSHC::Map::Navigation::PathFindingState_Func::isEnemyTooCloseUnk, + DAT_PathFindingState::ptr)(playerID, this->buildingX + x, this->buildingY + y, 3)) { + this->buildingPlacementFailReason = OpenSHC::Map::Buildings::BRE_UNKNOWN_17; + this->buildingPlacementFail = TRUE; + break; + } + tileIndex++; + } while (tileIndex < this->constructionTileCount); + } + break; + default: + if (DAT_GameCore::instance.gameMode_2 != OpenSHC::Game::GM_EDITOR + && DAT_GameCore::instance.gameMode_2 != OpenSHC::Game::GM_SIEGE_THAT) { + int const distance + = DAT_GameSynchronyState::instance.currentGameMode != OpenSHC::Game::GM_SOLITARY ? 15 : 30; + BOOLEnum noEnemyNearby = TRUE; + tileIndex = 0; + do { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::getBuildingSizeIndexMappingData, this)( + tileIndex, buildingSize); + if (MACRO_CALL_MEMBER(OpenSHC::Map::Navigation::PathFindingState_Func::isEnemyTooCloseUnk, + DAT_PathFindingState::ptr)( + playerID, this->buildingX + x, this->buildingY + y, distance)) { + this->buildingPlacementFailReason = OpenSHC::Map::Buildings::BRE_UNKNOWN_17; + noEnemyNearby = FALSE; + break; + } + tileIndex++; + } while (tileIndex < this->constructionTileCount); + if (MACRO_CALL_MEMBER(OpenSHC::Map::Navigation::PathFindingState_Func::isSignPostWithinDistance, + DAT_PathFindingState::ptr)( + x, y, DAT_GameState::instance.mapAndTime.unk_signpostDistance + 5)) { + this->buildingPlacementFailReason = OpenSHC::Map::Buildings::BRE_UNKNOWN_21; + this->buildingPlacementFail = TRUE; + } else if (!noEnemyNearby) { + this->buildingPlacementFail = TRUE; + } + } + break; + } + if (checkSuitability && this->buildingPlacementFail == FALSE) { + tileIndex = 0; + do { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::getBuildingSizeIndexMappingData, this)( + tileIndex, buildingSize); + if (MACRO_CALL_MEMBER(OpenSHC::Map::Navigation::PathFindingState_Func::findSomeSuitableLocationUnk, + DAT_PathFindingState::ptr)(playerID, this->buildingX + x, this->buildingY + y, 2)) { + this->buildingPlacementFailReason = OpenSHC::Map::Buildings::BRE_UNKNOWN_17; + this->buildingPlacementFail = TRUE; + break; + } + tileIndex++; + } while (tileIndex < this->constructionTileCount); + } + } + if ((short)commandBuildingType == OpenSHC::Commands::M_MAPPER_DRAWBRIDGE && this->buildingMaxHeight > 12) { + this->buildingPlacementFail = TRUE; + } + tileIndex = 0; + do { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::getBuildingSizeIndexMappingData, this)( + tileIndex, buildingSize); + uint const tileX = this->buildingX + x; + uint const tileY = this->buildingY + y; + if (tileX > 399) { + this->buildingPlacementFail = 2; + return; + } + if (tileY > 399) { + this->buildingPlacementFail = 2; + return; + } + if (DAT_ViewportRenderState::instance.DAT_BinaryTileMap400x400[tileY * 400 + tileX] == 0) { + this->buildingPlacementFail = 2; + return; + } + int const tile + = DAT_ViewportRenderState::instance.translationMatrix[tileY].addXgetTile + this->buildingX + x; + uint const logic = this->LogicLayer[tile]; + if ((logic & (L_BORDER | L_BORDER_EDGE)) != 0) { + this->buildingPlacementFail = 2; + return; + } + if ((logic & L_BOULDERS) != 0) { + boulderCount++; + } + if ((logic & L_IRON) != 0) { + ironCount++; + } + if ((logic & L_OIL) != 0) { + oilCount++; + } + if ((logic & L_RIVER) == 0) { + char const logic2 = this->Logic2Layer[tile]; + if ((logic2 & L2_OASIS_GRASS) != 0) { + fertileCount++; + grassCount++; + } + if ((logic2 & L2_THICK_SCRUB) != 0) { + fertileCount++; + grassCount++; + } + if ((logic2 & L2_SCRUB) != 0) { + fertileCount++; + } + } + if (MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::isBuildingPlacementAllowedAtTile, this)( + tile, playerID, command, 0)) { + this->buildingPlacementFail = TRUE; + } + tileIndex++; + } while (tileIndex < this->constructionTileCount); + short const commandShort = (short)commandBuildingType; + if (commandShort == OpenSHC::Commands::M_MAPPER_CATTLEFARM + || commandShort == OpenSHC::Commands::M_MAPPER_WHEATFARM + || commandShort == OpenSHC::Commands::M_MAPPER_HOPSFARM + || commandShort == OpenSHC::Commands::M_MAPPER_APPLEFARM) { + if (fertileCount < this->constructionTileCount) { + this->buildingPlacementFailReason = OpenSHC::Map::Buildings::BRE_UNKNOWN_22; + this->buildingPlacementFail = TRUE; + return; + } + if (grassCount < 50) { + this->buildingPlacementFailReason = OpenSHC::Map::Buildings::BRE_UNKNOWN_23; + this->buildingPlacementFail = TRUE; + } + return; + } + if (commandShort == OpenSHC::Commands::M_MAPPER_QUARRY) { + if (boulderCount < 8) { + this->buildingPlacementFail = TRUE; + } + return; + } + if (commandShort == OpenSHC::Commands::M_MAPPER_KEEP1 || commandShort == OpenSHC::Commands::M_MAPPER_KEEP2 + || commandShort == OpenSHC::Commands::M_MAPPER_KEEP3) { + int const keepIndex = command - OpenSHC::Commands::M_MAPPER_KEEP1; + for (int part = 0; part < 3; part++) { + if (!MACRO_CALL_MEMBER( + OpenSHC::Rendering::ViewportRenderState_Func::xyAreValid, DAT_ViewportRenderState::ptr)( + DAT_TerrainDefinedData::instance.field_0x264[keepIndex][part].x + x, + DAT_TerrainDefinedData::instance.field_0x264[keepIndex][part].y + y)) { + this->buildingPlacementFail = 2; + return; + } + int const tile + = DAT_ViewportRenderState::instance + .translationMatrix[DAT_TerrainDefinedData::instance.field_0x264[keepIndex][part].y + y] + .addXgetTile + + DAT_TerrainDefinedData::instance.field_0x264[keepIndex][part].x + x; + if ((this->LogicLayer[tile] & (L_BORDER | L_BORDER_EDGE)) != 0) { + this->buildingPlacementFail = 2; + return; + } + if (MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::isBuildingPlacementAllowedAtTile, this)( + tile, playerID, command, 0)) { + this->buildingPlacementFail = TRUE; + } + } + x += DAT_TerrainDefinedData::instance.keepOutbuildingOffsets_0x384[keepIndex][0].xOffset; + y += DAT_TerrainDefinedData::instance.keepOutbuildingOffsets_0x384[keepIndex][0].yOffset; + tileIndex = 0; + do { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::getBuildingSizeIndexMappingData, this)(tileIndex, 7); + uint const tileY = this->buildingY + y; + uint const tileX = this->buildingX + x; + if (tileX > 399 || tileY > 399 + || DAT_ViewportRenderState::instance.DAT_BinaryTileMap400x400[tileY * 400 + tileX] == 0) { + this->buildingPlacementFail = 2; + return; + } + int const tile + = DAT_ViewportRenderState::instance.translationMatrix[tileY].addXgetTile + this->buildingX + x; + if ((this->LogicLayer[tile] & (L_BORDER | L_BORDER_EDGE)) != 0) { + this->buildingPlacementFail = 2; + return; + } + if (MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::isBuildingPlacementAllowedAtTile, this)( + tile, playerID, command, 0)) { + this->buildingPlacementFail = TRUE; + } + tileIndex++; + } while (tileIndex < this->constructionTileCount); + x = DAT_TerrainDefinedData::instance.keepOutbuildingOffsets_0x3e4[keepIndex][0].xOffset + startX; + y = DAT_TerrainDefinedData::instance.keepOutbuildingOffsets_0x3e4[keepIndex][0].yOffset + startY; + tileIndex = 0; + do { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::getBuildingSizeIndexMappingData, this)(tileIndex, 5); + if (!MACRO_CALL_MEMBER(OpenSHC::Rendering::ViewportRenderState_Func::xyAreValid, + DAT_ViewportRenderState::ptr)(x + this->buildingX, this->buildingY + y)) { + this->buildingPlacementFail = 2; + return; + } + int const tile = DAT_ViewportRenderState::instance.translationMatrix[this->buildingY + y].addXgetTile + + this->buildingX + x; + if ((this->LogicLayer[tile] & (L_BORDER | L_BORDER_EDGE)) != 0) { + this->buildingPlacementFail = 2; + return; + } + if (MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::isBuildingPlacementAllowedAtTile, this)( + tile, playerID, command, 0)) { + this->buildingPlacementFail = TRUE; + } + tileIndex++; + } while (tileIndex < this->constructionTileCount); + return; + } + if (commandShort == OpenSHC::Commands::M_MAPPER_GATE_WOOD1A + || commandShort == OpenSHC::Commands::M_MAPPER_GATE_WOOD1B + || commandShort == OpenSHC::Commands::M_MAPPER_GATE_WOOD1C + || commandShort == OpenSHC::Commands::M_MAPPER_GATE_WOOD1D) { + int const gateIndex = command - OpenSHC::Commands::M_MAPPER_GATE_WOOD1A; + x += DAT_TerrainDefinedData::instance.gateTowerChecks[gateIndex].x1; + y += DAT_TerrainDefinedData::instance.gateTowerChecks[gateIndex].y1; + tileIndex = 0; + do { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::getBuildingSizeIndexMappingData, this)(tileIndex, 3); + if (!MACRO_CALL_MEMBER(OpenSHC::Rendering::ViewportRenderState_Func::xyAreValid, + DAT_ViewportRenderState::ptr)(this->buildingX + x, this->buildingY + y)) { + this->buildingPlacementFail = 2; + return; + } + int const tile = DAT_ViewportRenderState::instance.translationMatrix[this->buildingY + y].addXgetTile + + this->buildingX + x; + if ((this->LogicLayer[tile] & (L_BORDER | L_BORDER_EDGE)) != 0) { + this->buildingPlacementFail = 2; + return; + } + if (MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::isBuildingPlacementAllowedAtTile, this)( + tile, playerID, command, 0)) { + this->buildingPlacementFail = TRUE; + } + tileIndex++; + } while (tileIndex < this->constructionTileCount); + x = DAT_TerrainDefinedData::instance.gateTowerChecks[gateIndex].x2 + startX; + y = DAT_TerrainDefinedData::instance.gateTowerChecks[gateIndex].y2 + startY; + tileIndex = 0; + do { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::getBuildingSizeIndexMappingData, this)(tileIndex, 3); + if (!MACRO_CALL_MEMBER(OpenSHC::Rendering::ViewportRenderState_Func::xyAreValid, + DAT_ViewportRenderState::ptr)(x + this->buildingX, this->buildingY + y)) { + this->buildingPlacementFail = 2; + return; + } + int const tile = DAT_ViewportRenderState::instance.translationMatrix[this->buildingY + y].addXgetTile + + this->buildingX + x; + if ((this->LogicLayer[tile] & (L_BORDER | L_BORDER_EDGE)) != 0) { + this->buildingPlacementFail = 2; + return; + } + if (MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::isBuildingPlacementAllowedAtTile, this)( + tile, playerID, command, 0)) { + this->buildingPlacementFail = TRUE; + } + tileIndex++; + } while (tileIndex < this->constructionTileCount); + for (int part = 0; part < 6; part++) { + if (!MACRO_CALL_MEMBER( + OpenSHC::Rendering::ViewportRenderState_Func::xyAreValid, DAT_ViewportRenderState::ptr)(startX + + DAT_TerrainDefinedData::instance + .gateHouseTiles[command - OpenSHC::Commands::M_MAPPER_GATE_WOOD1A][part] + .x, + DAT_TerrainDefinedData::instance + .gateHouseTiles[command - OpenSHC::Commands::M_MAPPER_GATE_WOOD1A][part] + .y + + startY)) { + this->buildingPlacementFail = 2; + return; + } + int const tile = DAT_ViewportRenderState::instance + .translationMatrix[DAT_TerrainDefinedData::instance + .gateHouseTiles[command + - OpenSHC::Commands::M_MAPPER_GATE_WOOD1A][part] + .y + + startY] + .addXgetTile + + DAT_TerrainDefinedData::instance + .gateHouseTiles[command - OpenSHC::Commands::M_MAPPER_GATE_WOOD1A][part] + .x + + startX; + if ((this->LogicLayer[tile] & (L_BORDER | L_BORDER_EDGE)) != 0) { + this->buildingPlacementFail = 2; + return; + } + if (MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::isBuildingPlacementAllowedAtTile, this)( + tile, playerID, command, 0)) { + this->buildingPlacementFail = TRUE; + } + } + return; + } + if (commandShort == OpenSHC::Commands::M_MAPPER_IRON_MINE) { + if (ironCount < 4) { + this->buildingPlacementFailReason = OpenSHC::Map::Buildings::BFRE_NOT_IRON_ORE; + this->buildingPlacementFail = TRUE; + } + return; + } + if (commandShort == OpenSHC::Commands::M_MAPPER_PITCH_WORKINGS) { + if (oilCount < 1) { + this->buildingPlacementFailReason = OpenSHC::Map::Buildings::BFRE_NOT_OIL_MARSH; + this->buildingPlacementFail = TRUE; + } + return; + } + if (commandShort == OpenSHC::Commands::M_MAPPER_STORES) { + if (DAT_GameState::instance.playerDataArray[playerID] + .buildingEntryInfos[OpenSHC::Game::Player::Enums::BEIE_STOCKPILE] + .id + == 0) { + return; + } + if (MACRO_CALL_MEMBER(OpenSHC::Map::Buildings::BuildingsState_Func::getEmptyBuildingCount, + DAT_BuildingsState::ptr)(playerID, OpenSHC::Map::Buildings::BT_STOCKPILE) + >= 32) { + this->buildingPlacementFail = TRUE; + return; + } + if (MACRO_CALL_MEMBER(OpenSHC::Map::Buildings::BuildingsState_Func::hasBuildingAsNeighbour, + DAT_BuildingsState::ptr)(playerID, x, y, buildingSize, OpenSHC::Map::Buildings::BT_STOCKPILE)) { + return; + } + this->buildingPlacementFail = TRUE; + this->buildingPlacementFailReason = OpenSHC::Map::Buildings::BFRE_NOT_ADJ_STOCKPILE; + return; + } + if (commandShort == OpenSHC::Commands::M_MAPPER_GRANARY) { + if (DAT_GameState::instance.playerDataArray[playerID] + .buildingEntryInfos[OpenSHC::Game::Player::Enums::BEIE_GRANARY] + .id + == 0) { + return; + } + if (MACRO_CALL_MEMBER(OpenSHC::Map::Buildings::BuildingsState_Func::getEmptyBuildingCount, + DAT_BuildingsState::ptr)(playerID, OpenSHC::Map::Buildings::BT_GRANARY) + >= 8) { + this->buildingPlacementFail = TRUE; + return; + } + if (MACRO_CALL_MEMBER(OpenSHC::Map::Buildings::BuildingsState_Func::hasBuildingAsNeighbour, + DAT_BuildingsState::ptr)(playerID, x, y, buildingSize, OpenSHC::Map::Buildings::BT_GRANARY)) { + return; + } + this->buildingPlacementFail = TRUE; + this->buildingPlacementFailReason = OpenSHC::Map::Buildings::BFRE_NOT_ADJ_GRANARY; + return; + } + if (commandShort == OpenSHC::Commands::M_MAPPER_ARMOURY) { + if (DAT_GameState::instance.playerDataArray[playerID] + .buildingEntryInfos[OpenSHC::Game::Player::Enums::BEIE_ARMORY] + .id + == 0) { + return; + } + if (MACRO_CALL_MEMBER(OpenSHC::Map::Buildings::BuildingsState_Func::getEmptyBuildingCount, + DAT_BuildingsState::ptr)(playerID, OpenSHC::Map::Buildings::BT_ARMORY) + >= 8) { + this->buildingPlacementFail = TRUE; + return; + } + if (MACRO_CALL_MEMBER(OpenSHC::Map::Buildings::BuildingsState_Func::hasBuildingAsNeighbour, + DAT_BuildingsState::ptr)(playerID, x, y, buildingSize, OpenSHC::Map::Buildings::BT_ARMORY)) { + return; + } + this->buildingPlacementFail = TRUE; + this->buildingPlacementFailReason = OpenSHC::Map::Buildings::BFRE_NOT_ADJ_ARMORY; + return; + } + if (commandShort == OpenSHC::Commands::M_MAPPER_BARRACKS_EURO + || commandShort == OpenSHC::Commands::M_MAPPER_BARRACKS_ARAB) { + x += DAT_TerrainDefinedData::instance.BuildingPartsOffsets[this->DAT_TempBuildingRotation][0].x; + y += DAT_TerrainDefinedData::instance.BuildingPartsOffsets[this->DAT_TempBuildingRotation][0].y; + tileIndex = 0; + do { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::getBuildingSizeIndexMappingData, this)(tileIndex, 5); + if (!MACRO_CALL_MEMBER(OpenSHC::Rendering::ViewportRenderState_Func::xyAreValid, + DAT_ViewportRenderState::ptr)(this->buildingX + x, this->buildingY + y)) { + this->buildingPlacementFail = 2; + return; + } + int const tile = DAT_ViewportRenderState::instance.translationMatrix[this->buildingY + y].addXgetTile + + this->buildingX + x; + if ((this->LogicLayer[tile] & (L_BORDER | L_BORDER_EDGE)) != 0) { + this->buildingPlacementFail = 2; + return; + } + if (MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::isBuildingPlacementAllowedAtTile, this)( + tile, playerID, command, 0)) { + this->buildingPlacementFail = TRUE; + } + tileIndex++; + } while (tileIndex < this->constructionTileCount); + x = startX + DAT_TerrainDefinedData::instance.BuildingPartsOffsets[this->DAT_TempBuildingRotation][1].x; + y = startY + DAT_TerrainDefinedData::instance.BuildingPartsOffsets[this->DAT_TempBuildingRotation][1].y; + tileIndex = 0; + do { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::getBuildingSizeIndexMappingData, this)(tileIndex, 5); + if (!MACRO_CALL_MEMBER(OpenSHC::Rendering::ViewportRenderState_Func::xyAreValid, + DAT_ViewportRenderState::ptr)(this->buildingX + x, this->buildingY + y)) { + this->buildingPlacementFail = 2; + return; + } + int const tile = DAT_ViewportRenderState::instance.translationMatrix[this->buildingY + y].addXgetTile + + this->buildingX + x; + if ((this->LogicLayer[tile] & (L_BORDER | L_BORDER_EDGE)) != 0) { + this->buildingPlacementFail = 2; + return; + } + if (MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::isBuildingPlacementAllowedAtTile, this)( + tile, playerID, command, 0)) { + this->buildingPlacementFail = TRUE; + } + tileIndex++; + } while (tileIndex < this->constructionTileCount); + x = startX + DAT_TerrainDefinedData::instance.BuildingPartsOffsets[this->DAT_TempBuildingRotation][2].x; + y = startY + DAT_TerrainDefinedData::instance.BuildingPartsOffsets[this->DAT_TempBuildingRotation][2].y; + tileIndex = 0; + do { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::getBuildingSizeIndexMappingData, this)(tileIndex, 5); + if (!MACRO_CALL_MEMBER(OpenSHC::Rendering::ViewportRenderState_Func::xyAreValid, + DAT_ViewportRenderState::ptr)(this->buildingX + x, this->buildingY + y)) { + this->buildingPlacementFail = 2; + return; + } + int const tile = DAT_ViewportRenderState::instance.translationMatrix[this->buildingY + y].addXgetTile + + this->buildingX + x; + if ((this->LogicLayer[tile] & (L_BORDER | L_BORDER_EDGE)) != 0) { + this->buildingPlacementFail = 2; + return; + } + if (MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::isBuildingPlacementAllowedAtTile, this)( + tile, playerID, command, 0)) { + this->buildingPlacementFail = TRUE; + } + tileIndex++; + } while (tileIndex < this->constructionTileCount); + return; + } + if (commandShort == OpenSHC::Commands::M_MAPPER_ENGINEERS_GUILD + || commandShort == OpenSHC::Commands::M_MAPPER_TUNNELERS_GUILD) { + x += DAT_TerrainDefinedData::instance.BuildingPartsOffsets[this->DAT_TempBuildingRotation][1].x; + y += DAT_TerrainDefinedData::instance.BuildingPartsOffsets[this->DAT_TempBuildingRotation][1].y; + tileIndex = 0; + do { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::getBuildingSizeIndexMappingData, this)(tileIndex, 5); + if (!MACRO_CALL_MEMBER(OpenSHC::Rendering::ViewportRenderState_Func::xyAreValid, + DAT_ViewportRenderState::ptr)(this->buildingX + x, this->buildingY + y)) { + this->buildingPlacementFail = 2; + return; + } + int const tile = DAT_ViewportRenderState::instance.translationMatrix[this->buildingY + y].addXgetTile + + this->buildingX + x; + if ((this->LogicLayer[tile] & (L_BORDER | L_BORDER_EDGE)) != 0) { + this->buildingPlacementFail = 2; + return; + } + if (MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::isBuildingPlacementAllowedAtTile, this)( + tile, playerID, command, 0)) { + this->buildingPlacementFail = TRUE; + } + tileIndex++; + } while (tileIndex < this->constructionTileCount); + return; + } + if (commandShort == OpenSHC::Commands::M_MAPPER_OIL_SMELTER) { + x += DAT_TerrainDefinedData::instance.field_0x19c[this->DAT_TempBuildingRotation].x; + y += DAT_TerrainDefinedData::instance.field_0x19c[this->DAT_TempBuildingRotation].y; + tileIndex = 0; + do { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::getBuildingSizeIndexMappingData, this)(tileIndex, 4); + if (!MACRO_CALL_MEMBER(OpenSHC::Rendering::ViewportRenderState_Func::xyAreValid, + DAT_ViewportRenderState::ptr)(this->buildingX + x, this->buildingY + y)) { + this->buildingPlacementFail = 2; + return; + } + int const tile = DAT_ViewportRenderState::instance.translationMatrix[this->buildingY + y].addXgetTile + + this->buildingX + x; + if ((this->LogicLayer[tile] & (L_BORDER | L_BORDER_EDGE)) != 0) { + this->buildingPlacementFail = 2; + return; + } + if (MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::isBuildingPlacementAllowedAtTile, this)( + tile, playerID, OpenSHC::Commands::M_MAPPER_OIL_SMELTER, 0)) { + this->buildingPlacementFail = TRUE; + } + tileIndex++; + } while (tileIndex < this->constructionTileCount); + return; + } + if (commandShort == OpenSHC::Commands::M_MAPPER_TUNNEL_CONSTRUCTION) { + MACRO_CALL_MEMBER(OpenSHC::Map::Navigation::PathFindingState_Func::algTunnelerFindTarget, + DAT_PathFindingState::ptr)(playerID, 0, 76, x, y); + if (DAT_PathFindingState::instance.ALG_TargetTile == 0) { + this->buildingPlacementFail = TRUE; + } + } + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/checkDrawbridgePlacement.cpp b/src/OpenSHC/Map/TileMapState/checkDrawbridgePlacement.cpp new file mode 100644 index 0000000000..50f116af14 --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/checkDrawbridgePlacement.cpp @@ -0,0 +1,94 @@ +#include "../TileMapState.func.hpp" + +#include "OpenSHC/Map/Buildings/BuildingLogicalState.hpp" +#include "OpenSHC/Map/Buildings/BuildingType.hpp" +#include "OpenSHC/WindowsHelper/Enums/BOOLEnum.hpp" + +#include "OpenSHC/Globals/DAT_BuildingsState.hpp" +#include "OpenSHC/Globals/DAT_TerrainDefinedData.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::Buildings::BuildingLogicalState; + + // FUNCTION: STRONGHOLDCRUSADER 0x004FA2D0 + void TileMapState::checkDrawbridgePlacement(int x, int volatile y) + { + if (this->buildingPlacementFail != FALSE) { + return; + } + this->buildingPlacementFail = TRUE; + + for (int rotation = 0; rotation < 4; rotation++) { + int buildings[9]; + for (int count = 0; count < 9; count++) { + int const baseY = y; + uint const tileY = baseY + DAT_TerrainDefinedData::instance.drawBridgeOffsets[rotation][count].y; + int const offsetX = DAT_TerrainDefinedData::instance.drawBridgeOffsets[rotation][count].x; + uint const tileX = offsetX + x; + buildings[count] = 0; + if (tileX > 399 || tileY > 399) { + break; + } + if (DAT_ViewportRenderState::instance.DAT_BinaryTileMap400x400[tileY * 400 + tileX] == 0) { + break; + } + int const tile = DAT_ViewportRenderState::instance.translationMatrix[tileY].addXgetTile + offsetX + x; + if ((this->LogicLayer[tile] & (L_BORDER | L_BORDER_EDGE)) != 0) { + break; + } + buildings[count] = this->BuildingLayer[tile]; + } + + if (buildings[0] == 0) { + continue; + } + if (DAT_BuildingsState::instance.buildings[buildings[0]].logicalState + != OpenSHC::Map::Buildings::BLS_NORMAL) { + continue; + } + int span; + if (DAT_BuildingsState::instance.buildings[buildings[0]].buildingType + == OpenSHC::Map::Buildings::BT_GATEHOUSELARGE) { + span = 7; + } else if (DAT_BuildingsState::instance.buildings[buildings[0]].buildingType + == OpenSHC::Map::Buildings::BT_GATEHOUSESMALL) { + span = 5; + } else { + continue; + } + // the gatehouse faces along one axis, so only two of the four rotations can carry a bridge + if (DAT_BuildingsState::instance.buildings[buildings[0]].buildingVariation == 81) { + if (rotation == 1 || rotation == 3) { + continue; + } + } else if (DAT_BuildingsState::instance.buildings[buildings[0]].buildingVariation == 80) { + if (rotation == 0 || rotation == 2) { + continue; + } + } + + int i = 0; + int matching = 0; + for (; i < span; i++) { + if (buildings[i] == buildings[0]) { + matching++; + } + } + if (matching != span) { + continue; + } + this->uiBuildingRotation = rotation * 2; + this->field_0x554944 = this->uiBuildingRotation * 25; + this->buildingPlacementFail = FALSE; + return; + } + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/clearRockFootprintFlags.cpp b/src/OpenSHC/Map/TileMapState/clearRockFootprintFlags.cpp new file mode 100644 index 0000000000..83a11aba29 --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/clearRockFootprintFlags.cpp @@ -0,0 +1,32 @@ +#include "../TileMapState.func.hpp" + +#include "OpenSHC/Globals/DAT_LandscapeState.hpp" +#include "OpenSHC/Globals/DAT_ViewportRenderState.hpp" + +namespace OpenSHC { +namespace Map { + + using OpenSHC::Map::LogicHelpers::L_ROCKY; + + // FUNCTION: STRONGHOLDCRUSADER 0x004FB970 + void TileMapState::clearRockFootprintFlags(int rockID) + { + int const rockX = DAT_LandscapeState::instance.rocks[rockID].x; + int const rockY = DAT_LandscapeState::instance.rocks[rockID].y; + int buildingSizeTileIndex = 0; + while (true) { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::getBuildingSizeIndexMappingData, this)( + buildingSizeTileIndex, DAT_LandscapeState::instance.rocks[rockID].size); + int const tile = DAT_ViewportRenderState::instance.translationMatrix[this->buildingY + rockY].addXgetTile + + this->buildingX + rockX; + buildingSizeTileIndex++; + this->LogicLayer[tile] &= ~L_ROCKY; + this->OrganismLayer[tile] = 0; + if (buildingSizeTileIndex >= this->constructionTileCount) { + break; + } + } + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/clearSizeFiveBuildingFootprintAndMoats.cpp b/src/OpenSHC/Map/TileMapState/clearSizeFiveBuildingFootprintAndMoats.cpp new file mode 100644 index 0000000000..1e23b501e6 --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/clearSizeFiveBuildingFootprintAndMoats.cpp @@ -0,0 +1,56 @@ +#include "../TileMapState.func.hpp" + +#include "OpenSHC/Globals/DAT_BuildingsState.hpp" +#include "OpenSHC/Globals/DAT_TerrainDefinedData.hpp" +#include "OpenSHC/Globals/DAT_ViewportRenderState.hpp" + +namespace OpenSHC { +namespace Map { + + using OpenSHC::Map::LogicHelpers::L2_EARTH_AND_STONES; + using OpenSHC::Map::LogicHelpers::L_BUILDING; + using OpenSHC::Map::LogicHelpers::L_MOAT; + + // FUNCTION: STRONGHOLDCRUSADER 0x00508760 + void TileMapState::clearSizeFiveBuildingFootprintAndMoats(int x, int y) + { + int buildingSizeTileIndex = 0; + while (true) { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::getBuildingSizeIndexMappingData, this)( + buildingSizeTileIndex, 5); + int const tile = DAT_ViewportRenderState::instance.translationMatrix[this->buildingY + y].addXgetTile + + this->buildingX + x; + int const buildingID = this->BuildingLayer[tile]; + int const variation = DAT_BuildingsState::instance.buildings[buildingID].buildingVariation; + this->LogicLayer[tile] &= ~L_BUILDING; + if (DAT_TerrainDefinedData::instance.DrawbridgeOrientationMapping[variation / 2][buildingSizeTileIndex] + != 0) { + uint const moatID + = MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::returnOwnedMoatAtTile, this)(tile); + if (moatID != 0) { + if (DAT_BuildingsState::instance.buildings[buildingID].noRubble == 0) { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::clearMoatData, this)(moatID); + this->LogicLayer[tile] &= ~L_MOAT; + this->Logic2Layer[tile] |= L2_EARTH_AND_STONES; + this->BuildingWasLayer[tile] = 0; + this->HeightLayer[tile] = 8; + } else { + this->LogicLayer[tile] |= L_MOAT; + } + } + } + if ((this->LogicLayer[tile] & L_MOAT) == 0) { + this->Logic2Layer[tile] |= L2_EARTH_AND_STONES; + this->BuildingWasLayer[tile] = 0; + } + this->BuildingLayer[tile] = 0; + buildingSizeTileIndex++; + this->ChangedLayer[tile] = 2; + if (buildingSizeTileIndex >= this->constructionTileCount) { + break; + } + } + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/clearStockpileFootprintTiles.cpp b/src/OpenSHC/Map/TileMapState/clearStockpileFootprintTiles.cpp new file mode 100644 index 0000000000..2dde1e01ac --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/clearStockpileFootprintTiles.cpp @@ -0,0 +1,33 @@ +#include "../TileMapState.func.hpp" + +#include "OpenSHC/Globals/DAT_BuildingsState.hpp" +#include "OpenSHC/Globals/DAT_TerrainDefinedData.hpp" +#include "OpenSHC/Globals/DAT_ViewportRenderState.hpp" + +namespace OpenSHC { +namespace Map { + + using OpenSHC::Map::LogicHelpers::L_STOCKPILEUnk; + using OpenSHC::Map::LogicHelpers::L_WALL_OR_GATEHOUSE; + + // FUNCTION: STRONGHOLDCRUSADER 0x004FAF70 + void TileMapState::clearStockpileFootprintTiles(int x, int y) + { + for (int i = 0; i < countof(DAT_TerrainDefinedData::instance.StockpilePathableOffsets); ++i) { + int const tile = DAT_ViewportRenderState::instance + .translationMatrix[DAT_TerrainDefinedData::instance.StockpilePathableOffsets[i].y + y] + .addXgetTile + + x + DAT_TerrainDefinedData::instance.StockpilePathableOffsets[i].x; + this->LogicLayer[tile] &= ~(L_STOCKPILEUnk | L_WALL_OR_GATEHOUSE); + this->HeightLayer[tile] = this->DefaultHeightLayer[tile]; + if (DAT_BuildingsState::instance.buildings[this->AlphaGFXLayer[tile]].noRubble == 0) { + this->BuildingWasLayer[tile] = 0; + } else { + this->MiscDisplayLayer[tile] |= 0x4000; + } + this->AlphaGFXLayer[tile] = 0; + } + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/clearTreeFootprintFlags.cpp b/src/OpenSHC/Map/TileMapState/clearTreeFootprintFlags.cpp new file mode 100644 index 0000000000..3f6541009f --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/clearTreeFootprintFlags.cpp @@ -0,0 +1,34 @@ +#include "../TileMapState.func.hpp" + +#include "OpenSHC/Globals/DAT_LandscapeState.hpp" +#include "OpenSHC/Globals/DAT_ViewportRenderState.hpp" + +namespace OpenSHC { +namespace Map { + + using OpenSHC::Map::LogicHelpers::L_TREE; + using OpenSHC::Map::LogicHelpers::L_TREE_VARIATION; + + // FUNCTION: STRONGHOLDCRUSADER 0x004FB8E0 + void TileMapState::clearTreeFootprintFlags(int treeID) + { + int const treeX = DAT_LandscapeState::instance.trees[treeID].xPosition; + int const treeY = DAT_LandscapeState::instance.trees[treeID].yPosition; + this->OrganismLayer[DAT_LandscapeState::instance.trees[treeID].tile] = 0; + this->LogicLayer[DAT_LandscapeState::instance.trees[treeID].tile] &= ~L_TREE; + int index = 0; + while (true) { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::getBuildingSizeIndexMappingData, this)( + index, DAT_LandscapeState::instance.trees[treeID].size); + int const tile = DAT_ViewportRenderState::instance.translationMatrix[this->buildingY + treeY].addXgetTile + + treeX + this->buildingX; + index++; + this->LogicLayer[tile] &= ~L_TREE_VARIATION; + if (index >= this->constructionTileCount) { + break; + } + } + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/computeTileCliffEdgeFlags.cpp b/src/OpenSHC/Map/TileMapState/computeTileCliffEdgeFlags.cpp new file mode 100644 index 0000000000..c84ed2a38e --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/computeTileCliffEdgeFlags.cpp @@ -0,0 +1,104 @@ +#include "../TileMapState.func.hpp" + +#include "OpenSHC/Game/GameMode.hpp" +#include "OpenSHC/Map/Enums/MapOrientation.hpp" + +#include "OpenSHC/Globals/DAT_GameSynchronyState.hpp" +#include "OpenSHC/Globals/DAT_TerrainDefinedData.hpp" +#include "OpenSHC/Globals/DAT_ViewportRenderState.hpp" + +namespace OpenSHC { +namespace Map { + + using OpenSHC::Map::LogicHelpers::L2_BEACH; + 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::L2_THICK_SCRUB; + using OpenSHC::Map::LogicHelpers::L_RIVER; + + // FUNCTION: STRONGHOLDCRUSADER 0x004FC0B0 + void TileMapState::computeTileCliffEdgeFlags(int tile, int x, int y) + { + int const height = this->HeightLayer[tile]; + int lowest = height; + int highest = height; + if (DAT_GameSynchronyState::instance.currentGameMode == OpenSHC::Game::GM_MULTIPLAYER) { + return; + } + + // the height of each of the eight neighbours, or the own height where there is no river + int heights[8]; + for (int i = 0; i < countof(DAT_TerrainDefinedData::instance.field_0x372c); i++) { + XYPair offset; + offset.x = DAT_TerrainDefinedData::instance.field_0x372c[i].x; + offset.y = DAT_TerrainDefinedData::instance.field_0x372c[i].y; + uint const neighbourY = offset.y + y; + uint const neighbourX = offset.x + x; + heights[i] = height; + if (neighbourX > 399 || neighbourY > 399 + || DAT_ViewportRenderState::instance.DAT_BinaryTileMap400x400[neighbourY * 400 + neighbourX] == 0) { + continue; + } + int const neighbour + = DAT_ViewportRenderState::instance.translationMatrix[neighbourY].addXgetTile + offset.x + x; + if ((this->LogicLayer[neighbour] & L_RIVER) == 0) { + continue; + } + heights[i] = this->HeightLayer[neighbour]; + if (heights[i] < lowest) { + lowest = heights[i]; + } else if (heights[i] > highest) { + highest = heights[i]; + } + } + + this->Logic2Layer[tile] &= (L2_BEACH | L2_OASIS_GRASS | L2_PLATEAU_HIGH); + byte const flags = this->Logic2Layer[tile]; + if (lowest < height) { + // the bank runs along whichever axis the map is turned to (Logic2 has no names for edge bits 1, 2, 4) + if (this->mapOrientation == OpenSHC::Map::Enums::MO_NORTH) { + if (heights[2] < height && heights[4] < height) { + this->Logic2Layer[tile] = flags | 1; + } else if (heights[4] < height) { + this->Logic2Layer[tile] = flags | 2; + } else if (heights[2] < height) { + this->Logic2Layer[tile] = flags | 4; + } + } else if (this->mapOrientation == OpenSHC::Map::Enums::MO_SOUTH) { + if (heights[0] < height && heights[6] < height) { + this->Logic2Layer[tile] = flags | 1; + } else if (heights[0] < height) { + this->Logic2Layer[tile] = flags | 2; + } else if (heights[6] < height) { + this->Logic2Layer[tile] = flags | 4; + } + } else if (this->mapOrientation == OpenSHC::Map::Enums::MO_EAST) { + if (heights[4] < height && heights[6] < height) { + this->Logic2Layer[tile] = flags | 1; + } else if (heights[6] < height) { + this->Logic2Layer[tile] = flags | 2; + } else if (heights[4] < height) { + this->Logic2Layer[tile] = flags | 4; + } + } else if (this->mapOrientation == OpenSHC::Map::Enums::MO_WEST) { + if (heights[0] < height && heights[2] < height) { + this->Logic2Layer[tile] = flags | 1; + } else if (heights[2] < height) { + this->Logic2Layer[tile] = flags | 2; + } else if (heights[0] < height) { + this->Logic2Layer[tile] = flags | 4; + } + } + } else if (highest > height) { + // a bank all round: low step becomes a beach, a height one a cliff + if (highest > height + 16) { + this->Logic2Layer[tile] = flags | L2_STONES_OR_DRIVEN_SANDUnk; + } else { + this->Logic2Layer[tile] = flags | L2_THICK_SCRUB; + } + } + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/computeTileLuminescence.cpp b/src/OpenSHC/Map/TileMapState/computeTileLuminescence.cpp new file mode 100644 index 0000000000..1dff38cb37 --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/computeTileLuminescence.cpp @@ -0,0 +1,278 @@ +#include "../TileMapState.func.hpp" + +#include "OpenSHC/Map/Buildings/BuildingsState.func.hpp" +#include "OpenSHC/Map/LandscapeState.func.hpp" + +#include "OpenSHC/Globals/DAT_BuildingsState.hpp" +#include "OpenSHC/Globals/DAT_LandscapeState.hpp" +#include "OpenSHC/Globals/DAT_TerrainDefinedData.hpp" + +// the original takes the larger of the building height and 16 with a max macro, which calls the getter twice +#define MAX_OF(A, B) ((A) < (B) ? (B) : (A)) + +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_KEEP_NON_MANOR_HOUSE; + using OpenSHC::Map::LogicHelpers::L_MOAT; + using OpenSHC::Map::LogicHelpers::L_TREE; + using OpenSHC::Map::LogicHelpers::L_TREE_VARIATION; + + // FUNCTION: STRONGHOLDCRUSADER 0x004F7A80 + void TileMapState::computeTileLuminescence(int tile, int y) + { + uint logic = this->LogicLayer[tile]; + int diagonal = 0; + int opposite = 0; + int slope = 0; + int shade = 2; + int alongShade = 2; + int acrossShade = 2; + int along = 0; + int across = 0; + int high = 0; + if ((logic & (L_BORDER | L_BORDER_EDGE | L_BUILDING | L_KEEP_NON_MANOR_HOUSE)) != 0) { + return; + } + + // the light comes from one corner of the screen, so the rays turn with the map + switch (this->mapOrientation) { + case 0: + diagonal = 5; + opposite = 1; + along = 4; + across = 6; + break; + case 2: + diagonal = 7; + opposite = 3; + along = 6; + across = 0; + break; + case 4: + diagonal = 1; + opposite = 5; + along = 0; + across = 2; + break; + case 6: + diagonal = 3; + opposite = 7; + along = 2; + across = 4; + } + + int ownHeight = this->HeightLayer[tile]; + int rayY = y; + int stage = 0; + if ((logic & L_TREE) != 0) { + stage = MACRO_CALL_MEMBER(OpenSHC::Map::LandscapeState_Func::getTreeGrowthTargetStage, + &DAT_LandscapeState::instance)(this->OrganismLayer[tile]); + } else if ((logic & L_TREE_VARIATION) != 0) { + stage = 5; + } + if (stage > 2) { + shade = stage; + } + + // first ray: along the light's diagonal, shading hardest close to the tile + int rayTile = tile; + for (int i = 0; i < 5; i++) { + rayTile += this->directionTranslationMatrix[rayY][diagonal]; + rayY += DAT_TerrainDefinedData::instance.clockwiseCardinalTranslationMatrix[diagonal].int_.yOffset; + if ((this->LogicLayer[rayTile] & (L_BORDER | L_BORDER_EDGE)) != 0) { + break; + } + if ((this->LogicLayer[rayTile] & (L_BUILDING | L_KEEP_NON_MANOR_HOUSE)) != 0) { + int building = this->BuildingLayer[rayTile]; + if (MACRO_CALL_MEMBER(OpenSHC::Map::Buildings::BuildingsState_Func::getBuildingFlag3, + &DAT_BuildingsState::instance)(building) + == 0) { + high = this->HeightLayer[rayTile] + + MAX_OF(MACRO_CALL_MEMBER( + OpenSHC::Map::Buildings::BuildingsState_Func::getBuildingHeightForBuildingID, + &DAT_BuildingsState::instance)(building), + 16); + } + } else { + high = this->HeightLayer[rayTile]; + } + if ((this->LogicLayer[rayTile] & L_TREE) != 0) { + high += MACRO_CALL_MEMBER(OpenSHC::Map::LandscapeState_Func::getTreeGrowthTargetStage, + &DAT_LandscapeState::instance)(this->OrganismLayer[rayTile]) + * 7; + } else if ((this->LogicLayer[rayTile] & L_TREE_VARIATION) != 0) { + high += 40; + } + int rise = high - ownHeight; + int lit; + if (rise <= 0) { + lit = 2; + } else if (rise > 18) { + lit = 7 - i; + } else if (rise > 14) { + lit = 6 - i; + } else if (rise > 10) { + lit = 5 - i; + } else if (rise > 6) { + lit = 4 - i; + } else if (rise > 2) { + lit = 3 - i; + } else { + lit = 0; + } + if (lit > shade) { + shade = lit; + } + } + + // second ray: one cardinal step off the diagonal + int alongTile = this->directionTranslationMatrix[y][along] + tile; + int alongY = DAT_TerrainDefinedData::instance.clockwiseCardinalTranslationMatrix[along].int_.yOffset + y; + for (int reach = 4; reach > -1; reach--) { + if ((this->LogicLayer[alongTile] & (L_BORDER | L_BORDER_EDGE)) != 0) { + break; + } + if ((this->LogicLayer[alongTile] & (L_BUILDING | L_KEEP_NON_MANOR_HOUSE)) != 0) { + int building = this->BuildingLayer[alongTile]; + if (MACRO_CALL_MEMBER(OpenSHC::Map::Buildings::BuildingsState_Func::getBuildingFlag3, + &DAT_BuildingsState::instance)(building) + == 0) { + high = this->HeightLayer[alongTile] + + MAX_OF(MACRO_CALL_MEMBER( + OpenSHC::Map::Buildings::BuildingsState_Func::getBuildingHeightForBuildingID, + &DAT_BuildingsState::instance)(building), + 16); + } + } else { + high = this->HeightLayer[alongTile]; + } + if ((this->LogicLayer[alongTile] & L_TREE) != 0) { + high += MACRO_CALL_MEMBER(OpenSHC::Map::LandscapeState_Func::getTreeGrowthTargetStage, + &DAT_LandscapeState::instance)(this->OrganismLayer[alongTile]) + * 7; + } else if ((this->LogicLayer[alongTile] & L_TREE_VARIATION) != 0) { + high += 40; + } + int rise = high - ownHeight; + int lit; + if (rise <= 0) { + lit = 2; + } else if (rise > 18) { + lit = reach + 1; + } else if (rise > 12) { + lit = reach; + } else if (rise > 6) { + lit = reach - 1; + } else { + lit = 0; + } + if (lit > alongShade) { + alongShade = lit; + } + alongTile += this->directionTranslationMatrix[alongY][diagonal]; + alongY += DAT_TerrainDefinedData::instance.clockwiseCardinalTranslationMatrix[diagonal].int_.yOffset; + } + + // third ray: the same, one cardinal step the other way + int acrossTile = this->directionTranslationMatrix[y][across] + tile; + int acrossY = DAT_TerrainDefinedData::instance.clockwiseCardinalTranslationMatrix[across].int_.yOffset + y; + for (int reach = 4; reach > -1; reach--) { + if ((this->LogicLayer[acrossTile] & (L_BORDER | L_BORDER_EDGE)) != 0) { + break; + } + if ((this->LogicLayer[acrossTile] & (L_BUILDING | L_KEEP_NON_MANOR_HOUSE)) != 0) { + int building = this->BuildingLayer[acrossTile]; + if (MACRO_CALL_MEMBER(OpenSHC::Map::Buildings::BuildingsState_Func::getBuildingFlag3, + &DAT_BuildingsState::instance)(building) + == 0) { + high = this->HeightLayer[acrossTile] + + MAX_OF(MACRO_CALL_MEMBER( + OpenSHC::Map::Buildings::BuildingsState_Func::getBuildingHeightForBuildingID, + &DAT_BuildingsState::instance)(building), + 16); + } + } else { + high = this->HeightLayer[acrossTile]; + } + if ((this->LogicLayer[acrossTile] & L_TREE) != 0) { + high += MACRO_CALL_MEMBER(OpenSHC::Map::LandscapeState_Func::getTreeGrowthTargetStage, + &DAT_LandscapeState::instance)(this->OrganismLayer[acrossTile]) + * 7; + } else if ((this->LogicLayer[acrossTile] & L_TREE_VARIATION) != 0) { + high += 40; + } + int rise = high - ownHeight; + int lit; + if (rise <= 0) { + lit = 2; + } else if (rise > 18) { + lit = reach + 1; + } else if (rise > 12) { + lit = reach; + } else if (rise > 6) { + lit = reach - 1; + } else { + lit = 0; + } + if (lit > acrossShade) { + acrossShade = lit; + } + acrossTile += this->directionTranslationMatrix[acrossY][diagonal]; + acrossY += DAT_TerrainDefinedData::instance.clockwiseCardinalTranslationMatrix[diagonal].int_.yOffset; + } + + if (acrossShade + alongShade - 4 > shade - 2) { + int sum = acrossShade + alongShade - 2; + shade = sum < 7 ? sum : 7; + } + if (shade == 2) { + // nothing casts onto the tile, so shade it by the slope it sits on + int diagonalTile = this->directionTranslationMatrix[y][diagonal] + tile; + if ((this->LogicLayer[diagonalTile] & (L_BORDER | L_BORDER_EDGE)) != 0) { + high = ownHeight; + } else { + if ((this->LogicLayer[diagonalTile] & (L_BUILDING | L_KEEP_NON_MANOR_HOUSE)) != 0) { + int building = this->BuildingLayer[diagonalTile]; + if (MACRO_CALL_MEMBER(OpenSHC::Map::Buildings::BuildingsState_Func::getBuildingFlag3, + &DAT_BuildingsState::instance)(building) + == 0) { + high = this->HeightLayer[diagonalTile] + + MAX_OF(MACRO_CALL_MEMBER( + OpenSHC::Map::Buildings::BuildingsState_Func::getBuildingHeightForBuildingID, + &DAT_BuildingsState::instance)(building), + 16); + } + } else { + high = this->HeightLayer[diagonalTile]; + } + } + int oppositeTile = this->directionTranslationMatrix[y][opposite] + tile; + int darkSide; + if ((this->LogicLayer[oppositeTile] & (L_BORDER | L_BORDER_EDGE)) != 0) { + darkSide = ownHeight; + } else { + darkSide = this->HeightLayer[oppositeTile]; + } + if (high < ownHeight - 4) { + slope = 1; + } + if (darkSide > ownHeight + 4) { + slope += 1; + } + if ((this->LogicLayer[tile] & L_MOAT) == 0) { + if (slope == 1) { + shade = 1; + } else if (slope == 2) { + shade = 0; + } + } + } + this->LuminesenceLayer[tile] = shade; + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/createPlateau.cpp b/src/OpenSHC/Map/TileMapState/createPlateau.cpp new file mode 100644 index 0000000000..7dde341eaa --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/createPlateau.cpp @@ -0,0 +1,84 @@ +#include "../TileMapState.func.hpp" + +#include "OpenSHC/Map/Navigation/PathFindingState.func.hpp" +#include "OpenSHC/Map/TileMapState/NeighbourFlagsGlobalAsm.hpp" + +#include "OpenSHC/Globals/DAT_PathFindingState.hpp" +#include "OpenSHC/Globals/DAT_TerrainDefinedData.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_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_RIVER; + using OpenSHC::Map::LogicHelpers::L_ROCKY; + using OpenSHC::Map::LogicHelpers::L_SEA; + using OpenSHC::Map::LogicHelpers::L_WALL_OR_GATEHOUSE; + + // FUNCTION: STRONGHOLDCRUSADER 0x00501F50 + void TileMapState::createPlateau(int tile, uint y, int brush, int plateauHeightSetting) + { + int const brushSize = DAT_TerrainDefinedData::instance.BrushSizeArray[brush]; + int maxHeightInBrush = 0; + int const baseTile = tile; + uint const baseY = y; + + for (int index = 0; index < brushSize; index++) { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::getTileForBrush, this)( + 1, index, &tile, (int*)&y, baseTile, baseY); + if ((this->LogicLayer[tile] & (L_BORDER | L_BORDER_EDGE)) == 0 + && this->HeightLayer[tile] > maxHeightInBrush) { + maxHeightInBrush = this->HeightLayer[tile]; + } + } + + tile = baseTile; + y = baseY; + for (int index = 0; index < brushSize; index++) { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::getTileForBrush, this)( + 1, index, &tile, (int*)&y, baseTile, baseY); + int const brushTile = *(int volatile*)&tile; + if ((this->LogicLayer[brushTile] & (L_BORDER | L_BORDER_EDGE)) != 0) { + continue; + } + if ((this->LogicLayer[brushTile] & (L_SEA | L_BUILDING | L_KEEP_NON_MANOR_HOUSE | L_MARSH | L_MOAT)) != 0) { + continue; + } + if (this->BuildingLayer[brushTile] != 0) { + continue; + } + if ((this->LogicLayer[brushTile] + & (L_WALL_OR_GATEHOUSE | L_BUILDING | L_MOAT_DUG_OR_PLANNED | L_KEEP_NON_MANOR_HOUSE | L_MOAT)) + != 0) { + continue; + } + *(uint volatile*)&this->LogicLayer[brushTile] &= ~L_ROCKY; + if (plateauHeightSetting == 4) { + this->HeightLayer[brushTile] = 80; + } else if (plateauHeightSetting == 8) { + this->HeightLayer[brushTile] = 130; + } + this->DefaultHeightLayer[brushTile] = *(byte volatile*)&this->HeightLayer[brushTile]; + if ((this->LogicLayer[brushTile] & L_RIVER) != 0) { + this->HeightLayer[brushTile] -= 8; + } + uint const brushY = *(uint volatile*)&y; + this->Logic2Layer[brushTile] = (byte)plateauHeightSetting; + this->DAT_SomeTile = brushTile; + this->DAT_SomeY = brushY; + + // handwritten assembly in the original: mark this tile and its eight neighbours changed + MACRO_MARK_CHANGED_NEIGHBOURS_GLOBAL() + MACRO_CALL_MEMBER(OpenSHC::Map::Navigation::PathFindingState_Func::updatePathLinkagesInAllEightDirections, + DAT_PathFindingState::ptr)(brushY, brushTile); + } + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/createWaterForDrawBridge.cpp b/src/OpenSHC/Map/TileMapState/createWaterForDrawBridge.cpp new file mode 100644 index 0000000000..13cd4498c0 --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/createWaterForDrawBridge.cpp @@ -0,0 +1,53 @@ +#include "../TileMapState.func.hpp" + +#include "OpenSHC/Map/Navigation/PathFindingState.func.hpp" + +#include "OpenSHC/Globals/DAT_BuildingsState.hpp" +#include "OpenSHC/Globals/DAT_PathFindingState.hpp" +#include "OpenSHC/Globals/DAT_TerrainDefinedData.hpp" +#include "OpenSHC/Globals/DAT_ViewportRenderState.hpp" + +namespace OpenSHC { +namespace Map { + + using OpenSHC::Map::LogicHelpers::L_MOAT; + + // FUNCTION: STRONGHOLDCRUSADER 0x00515C80 + void TileMapState::createWaterForDrawBridge(int buildingID) + { + Buildings::Building const& building = DAT_BuildingsState::instance.buildings[buildingID]; + int const xPosition = building.x; + int const variation = building.buildingVariation / 2; + int const yPosition = building.y; + uint const widthOrHeight = building.widthOrHeight; + int buildingSizeTileIndex = 0; + int const* drawbridgeMapping = DAT_TerrainDefinedData::instance.DrawbridgeOrientationMapping[variation]; + while (true) { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::getBuildingSizeIndexMappingData, this)( + buildingSizeTileIndex, 5); + int const tile + = DAT_ViewportRenderState::instance.translationMatrix[yPosition + this->buildingY].addXgetTile + + this->buildingX + xPosition; + if (*drawbridgeMapping != 0) { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::createMoatData, this)( + DAT_BuildingsState::instance.buildings[buildingID].owner, this->buildingX + xPosition, + yPosition + this->buildingY, 1); + this->LogicLayer[tile] &= ~L_MOAT; + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::setMoatVisualStateAtTile, this)(tile, 0); + this->HeightLayer[tile] = 0; + } + buildingSizeTileIndex++; + drawbridgeMapping++; + if (buildingSizeTileIndex >= this->constructionTileCount) { + break; + } + } + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::updateBuildingGraphicsLayer, this)(buildingID); + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::updatePathLinkagesForBuilding, this)(buildingID); + MACRO_CALL_MEMBER(OpenSHC::Map::Navigation::PathFindingState_Func::updateWalkAndPathLayer, + DAT_PathFindingState::ptr)(widthOrHeight + 6, xPosition, yPosition); + DAT_PathFindingState::instance.toggleUpdateSeparateAreaTileMap = 1; + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/demolishBuildingsInKeepsConstructionFootprint.cpp b/src/OpenSHC/Map/TileMapState/demolishBuildingsInKeepsConstructionFootprint.cpp new file mode 100644 index 0000000000..c946c16f19 --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/demolishBuildingsInKeepsConstructionFootprint.cpp @@ -0,0 +1,122 @@ +#include "../TileMapState.func.hpp" + +#include "OpenSHC/Map/Buildings/BuildingsState.func.hpp" +#include "OpenSHC/Map/Buildings/BuildingLogicalState.hpp" +#include "OpenSHC/Map/Buildings/BuildingType.hpp" + +#include "OpenSHC/Globals/DAT_BuildingsState.hpp" +#include "OpenSHC/Globals/DAT_TerrainDefinedData.hpp" +#include "OpenSHC/Globals/DAT_ViewportRenderState.hpp" + +namespace OpenSHC { +namespace Map { + + using OpenSHC::Map::Buildings::BuildingLogicalState; + + // FUNCTION: STRONGHOLDCRUSADER 0x004FA760 + void TileMapState::demolishBuildingsInKeepsConstructionFootprint(undefined4 unusedPlayerID, int x, int y, + undefined4 buildingType, int sizeIndex, int unusedOrientation, int unusedAverageHeight) + { + int index = 0; + int const keepX = x; + int const keepY = y; + while (true) { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::getBuildingSizeIndexMappingData, this)(index, sizeIndex); + int const tile = DAT_ViewportRenderState::instance.translationMatrix[this->buildingY + y].addXgetTile + + this->buildingX + x; + if (this->BuildingLayer[tile] != 0) { + this->showNoRubbleWhenDestroyingBuilding = 1; + MACRO_CALL_MEMBER(OpenSHC::Map::Buildings::BuildingsState_Func::destroyBuilding, + DAT_BuildingsState::ptr)(this->BuildingLayer[tile]); + } + index++; + if (index >= this->constructionTileCount) { + break; + } + } + + // the keep drags three towers and two outbuildings with it, laid out per keep variant + buildingType = (short)buildingType - OpenSHC::Map::Buildings::BT_MANORHOUSE; + { + int const tile = DAT_ViewportRenderState::instance + .translationMatrix[DAT_TerrainDefinedData::instance.field_0x264[buildingType][0].y + y] + .addXgetTile + + DAT_TerrainDefinedData::instance.field_0x264[buildingType][0].x + x; + if (this->BuildingLayer[tile] != 0) { + this->showNoRubbleWhenDestroyingBuilding = 1; + MACRO_CALL_MEMBER(OpenSHC::Map::Buildings::BuildingsState_Func::destroyBuilding, + DAT_BuildingsState::ptr)(this->BuildingLayer[tile]); + } + } + + { + int const tile = DAT_ViewportRenderState::instance + .translationMatrix[DAT_TerrainDefinedData::instance.field_0x264[buildingType][1].y + y] + .addXgetTile + + DAT_TerrainDefinedData::instance.field_0x264[buildingType][1].x + x; + if (this->BuildingLayer[tile] != 0) { + this->showNoRubbleWhenDestroyingBuilding = 1; + MACRO_CALL_MEMBER(OpenSHC::Map::Buildings::BuildingsState_Func::destroyBuilding, + DAT_BuildingsState::ptr)(this->BuildingLayer[tile]); + } + } + + { + int const tile = DAT_ViewportRenderState::instance + .translationMatrix[DAT_TerrainDefinedData::instance.field_0x264[buildingType][2].y + y] + .addXgetTile + + DAT_TerrainDefinedData::instance.field_0x264[buildingType][2].x + x; + if (this->BuildingLayer[tile] != 0) { + this->showNoRubbleWhenDestroyingBuilding = 1; + MACRO_CALL_MEMBER(OpenSHC::Map::Buildings::BuildingsState_Func::destroyBuilding, + DAT_BuildingsState::ptr)(this->BuildingLayer[tile]); + } + } + + x += DAT_TerrainDefinedData::instance.keepOutbuildingOffsets_0x384[buildingType][0].xOffset; + y += DAT_TerrainDefinedData::instance.keepOutbuildingOffsets_0x384[buildingType][0].yOffset; + index = 0; + while (true) { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::getBuildingSizeIndexMappingData, this)(index, 7); + int const tile = DAT_ViewportRenderState::instance.translationMatrix[this->buildingY + y].addXgetTile + + this->buildingX + x; + if (this->BuildingLayer[tile] != 0) { + this->showNoRubbleWhenDestroyingBuilding = 1; + MACRO_CALL_MEMBER(OpenSHC::Map::Buildings::BuildingsState_Func::destroyBuilding, + DAT_BuildingsState::ptr)(this->BuildingLayer[tile]); + } + index++; + if (index >= this->constructionTileCount) { + break; + } + } + + x = keepX + DAT_TerrainDefinedData::instance.keepOutbuildingOffsets_0x3e4[buildingType][0].xOffset; + y = keepY + DAT_TerrainDefinedData::instance.keepOutbuildingOffsets_0x3e4[buildingType][0].yOffset; + index = 0; + while (true) { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::getBuildingSizeIndexMappingData, this)(index, 5); + int const tile = DAT_ViewportRenderState::instance.translationMatrix[this->buildingY + y].addXgetTile + + this->buildingX + x; + if (this->BuildingLayer[tile] != 0) { + this->showNoRubbleWhenDestroyingBuilding = 1; + MACRO_CALL_MEMBER(OpenSHC::Map::Buildings::BuildingsState_Func::destroyBuilding, + DAT_BuildingsState::ptr)(this->BuildingLayer[tile]); + } + index++; + if (index >= this->constructionTileCount) { + break; + } + } + + for (int buildingID = 1; buildingID < 2000; buildingID++) { + if (DAT_BuildingsState::instance.buildings[buildingID].logicalState + == OpenSHC::Map::Buildings::BLS_REMOVE) { + MACRO_CALL_MEMBER( + OpenSHC::Map::Buildings::BuildingsState_Func::deleteBuilding, DAT_BuildingsState::ptr)(buildingID); + } + } + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/determineBuildingPlacementRotation.cpp b/src/OpenSHC/Map/TileMapState/determineBuildingPlacementRotation.cpp new file mode 100644 index 0000000000..02f35b688d --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/determineBuildingPlacementRotation.cpp @@ -0,0 +1,141 @@ +#include "../TileMapState.func.hpp" + +#include "OpenSHC/WindowsHelper/Enums/BOOLEnum.hpp" + +#include "OpenSHC/Globals/DAT_TerrainDefinedData.hpp" +#include "OpenSHC/Globals/DAT_ViewportRenderState.hpp" + +namespace OpenSHC { +namespace Map { + + using OpenSHC::Map::LogicHelpers::L_STAIRS; + using OpenSHC::Map::LogicHelpers::L_STOCKPILEUnk; + using OpenSHC::Map::LogicHelpers::L_WALL_OR_GATEHOUSE; + + // FUNCTION: STRONGHOLDCRUSADER 0x004FA000 + void TileMapState::determineBuildingPlacementRotation(int x, int y) + { + int volatile wallTileCount = 0; + this->field_0x554944 = 128; + this->uiBuildingRotation = 8; + if (this->buildingPlacementFail) { + return; + } + + // count, per rotation, how many of its four anchor tiles carry usable wall + int walls[4]; + int* volatile wall = walls; + for (int rotation = 0; rotation < 4; rotation++, wall++) { + *wall = 0; + { + int const tileY = DAT_TerrainDefinedData::instance.field_0x1e4c[rotation * 8 + 1] + y; + if ((uint)(DAT_TerrainDefinedData::instance.field_0x1e4c[rotation * 8 + 0] + x) <= 399 + && (uint)tileY <= 399 + && DAT_ViewportRenderState::instance.DAT_BinaryTileMap400x400[tileY * 400 + + DAT_TerrainDefinedData::instance.field_0x1e4c[rotation * 8 + 0] + x] + != 0 + && (this->LogicLayer[DAT_ViewportRenderState::instance.translationMatrix[tileY].addXgetTile + + DAT_TerrainDefinedData::instance.field_0x1e4c[rotation * 8 + 0] + x] + & L_WALL_OR_GATEHOUSE) + != 0 + && (this->LogicLayer[DAT_ViewportRenderState::instance.translationMatrix[tileY].addXgetTile + + DAT_TerrainDefinedData::instance.field_0x1e4c[rotation * 8 + 0] + x] + & (L_STOCKPILEUnk | L_STAIRS)) + == 0) { + wallTileCount++; + *wall = 1; + } + } + { + int const tileY = DAT_TerrainDefinedData::instance.field_0x1e4c[rotation * 8 + 3] + y; + if ((uint)(DAT_TerrainDefinedData::instance.field_0x1e4c[rotation * 8 + 2] + x) <= 399 + && (uint)tileY <= 399 + && DAT_ViewportRenderState::instance.DAT_BinaryTileMap400x400[tileY * 400 + + DAT_TerrainDefinedData::instance.field_0x1e4c[rotation * 8 + 2] + x] + != 0 + && (this->LogicLayer[DAT_ViewportRenderState::instance.translationMatrix[tileY].addXgetTile + + DAT_TerrainDefinedData::instance.field_0x1e4c[rotation * 8 + 2] + x] + & L_WALL_OR_GATEHOUSE) + != 0 + && (this->LogicLayer[DAT_ViewportRenderState::instance.translationMatrix[tileY].addXgetTile + + DAT_TerrainDefinedData::instance.field_0x1e4c[rotation * 8 + 2] + x] + & (L_STOCKPILEUnk | L_STAIRS)) + == 0) { + *wall += 1; + wallTileCount++; + } + } + { + int const tileY = DAT_TerrainDefinedData::instance.field_0x1e4c[rotation * 8 + 5] + y; + if ((uint)(DAT_TerrainDefinedData::instance.field_0x1e4c[rotation * 8 + 4] + x) <= 399 + && (uint)tileY <= 399 + && DAT_ViewportRenderState::instance.DAT_BinaryTileMap400x400[tileY * 400 + + DAT_TerrainDefinedData::instance.field_0x1e4c[rotation * 8 + 4] + x] + != 0 + && (this->LogicLayer[DAT_ViewportRenderState::instance.translationMatrix[tileY].addXgetTile + + DAT_TerrainDefinedData::instance.field_0x1e4c[rotation * 8 + 4] + x] + & L_WALL_OR_GATEHOUSE) + != 0 + && (this->LogicLayer[DAT_ViewportRenderState::instance.translationMatrix[tileY].addXgetTile + + DAT_TerrainDefinedData::instance.field_0x1e4c[rotation * 8 + 4] + x] + & (L_STOCKPILEUnk | L_STAIRS)) + == 0) { + *wall += 1; + wallTileCount++; + } + } + { + int const tileY = DAT_TerrainDefinedData::instance.field_0x1e4c[rotation * 8 + 7] + y; + if ((uint)(DAT_TerrainDefinedData::instance.field_0x1e4c[rotation * 8 + 6] + x) <= 399 + && (uint)tileY <= 399 + && DAT_ViewportRenderState::instance.DAT_BinaryTileMap400x400[tileY * 400 + + DAT_TerrainDefinedData::instance.field_0x1e4c[rotation * 8 + 6] + x] + != 0 + && (this->LogicLayer[DAT_ViewportRenderState::instance.translationMatrix[tileY].addXgetTile + + DAT_TerrainDefinedData::instance.field_0x1e4c[rotation * 8 + 6] + x] + & L_WALL_OR_GATEHOUSE) + != 0 + && (this->LogicLayer[DAT_ViewportRenderState::instance.translationMatrix[tileY].addXgetTile + + DAT_TerrainDefinedData::instance.field_0x1e4c[rotation * 8 + 6] + x] + & (L_STOCKPILEUnk | L_STAIRS)) + == 0) { + *wall += 1; + wallTileCount++; + } + } + } + + this->uiBuildingRotation = 8; + if (walls[0] == 4 && walls[3] == 4) { + this->uiBuildingRotation = 7; + } else if (walls[0] == 4 && walls[1] == 4) { + this->uiBuildingRotation = 1; + } else if (walls[2] == 4 && walls[3] == 4) { + this->uiBuildingRotation = 5; + } else if (walls[2] == 4 && walls[1] == 4) { + this->uiBuildingRotation = 3; + } else if (walls[0] == 4) { + this->uiBuildingRotation = 0; + } else if (walls[1] == 4) { + this->uiBuildingRotation = 2; + } else if (walls[2] == 4) { + this->uiBuildingRotation = 4; + } else if (walls[3] == 4) { + this->uiBuildingRotation = 6; + } + + int relative; + if (this->uiBuildingRotation != 8) { + relative = this->uiBuildingRotation - this->mapOrientation; + if (relative < 0) { + this->field_0x554944 = (relative + 8) * 16; + return; + } + } else { + relative = 8; + } + this->field_0x554944 = relative * 16; + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/eraseAreaWithBrush.cpp b/src/OpenSHC/Map/TileMapState/eraseAreaWithBrush.cpp new file mode 100644 index 0000000000..f8e5058c0f --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/eraseAreaWithBrush.cpp @@ -0,0 +1,135 @@ +#include "../TileMapState.func.hpp" + +#include "OpenSHC/Map/Buildings/BuildingsState.func.hpp" +#include "OpenSHC/Map/LandscapeState.func.hpp" +#include "OpenSHC/Map/Navigation/PathFindingState.func.hpp" +#include "OpenSHC/Game/Player/Enums/BuildingEntryInfoEnum.hpp" +#include "OpenSHC/Map/Buildings/BuildingType.hpp" +#include "OpenSHC/Map/Units/UnitLogicState.hpp" +#include "OpenSHC/WindowsHelper/Enums/BOOLEnum.hpp" + +#include "OpenSHC/Globals/DAT_BuildingsState.hpp" +#include "OpenSHC/Globals/DAT_GameState.hpp" +#include "OpenSHC/Globals/DAT_LandscapeState.hpp" +#include "OpenSHC/Globals/DAT_PathFindingState.hpp" +#include "OpenSHC/Globals/DAT_TerrainDefinedData.hpp" +#include "OpenSHC/Globals/DAT_TileMapState.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_BORDER_EDGE; + + // FUNCTION: STRONGHOLDCRUSADER 0x00508EC0 + void TileMapState::eraseAreaWithBrush(uint x, uint y, uint brush) + { + if (x > 399 || y > 399) { + return; + } + if (DAT_ViewportRenderState::instance.DAT_BinaryTileMap400x400[y * 400 + x] == 0) { + return; + } + + int const brushSize = DAT_TerrainDefinedData::instance.BrushSizeArray[brush]; + int const baseTile = DAT_ViewportRenderState::instance.translationMatrix[y].addXgetTile + x; + uint const savedY = y; + int tile = DAT_ViewportRenderState::instance.translationMatrix[y].addXgetTile + x; + + for (int index = 0; index < brushSize; index++) { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::getTileForBrush, this)( + 1, index, &tile, (int*)&y, baseTile, savedY); + if ((this->LogicLayer[tile] & (L_BORDER | L_BORDER_EDGE)) != 0) { + break; + } + int const organism = this->OrganismLayer[tile]; + if (organism != 0) { + // tree ids come first, rock ids are biased by 2000 + if (organism >= 2000) { + MACRO_CALL_MEMBER(OpenSHC::Map::LandscapeState_Func::removeRock, DAT_LandscapeState::ptr)( + organism - 2000); + MACRO_CALL_MEMBER(OpenSHC::Map::Navigation::PathFindingState_Func::updateWalkAndPathLayer, + DAT_PathFindingState::ptr)(8, x, y); + } else { + MACRO_CALL_MEMBER(OpenSHC::Map::LandscapeState_Func::removeTree, DAT_LandscapeState::ptr)(organism); + MACRO_CALL_MEMBER(OpenSHC::Map::Navigation::PathFindingState_Func::updateWalkAndPathLayer, + DAT_PathFindingState::ptr)(3, x, y); + } + DAT_PathFindingState::instance.toggleUpdateSeparateAreaTileMap = 1; + DAT_TileMapState::instance.field_0x554a30 = 1; + } + } + + y = savedY; + for (int index = 0; index < brushSize; index++) { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::getTileForBrush, this)( + 1, index, &tile, (int*)&y, baseTile, savedY); + if ((this->LogicLayer[tile] & (L_BORDER | L_BORDER_EDGE)) != 0) { + break; + } + uint const brushY = y; + this->placementOnWall = 0; + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::validateBuildingPlacementAtTile, this)(0, x, brushY); + if (this->buildingPlacementFail != FALSE) { + continue; + } + if (this->placementOnWall == 0) { + int const targetID = this->cursorTargetID; + if (targetID == -1) { + continue; + } + // razing a keep takes the stockpile with it, and the other way round + int const owner = DAT_BuildingsState::instance.buildings[targetID].owner; + if (targetID + == DAT_GameState::instance.playerDataArray[owner] + .buildingEntryInfos[OpenSHC::Game::Player::Enums::BEIE_KEEP] + .id + && DAT_GameState::instance.playerDataArray[owner] + .buildingEntryInfos[OpenSHC::Game::Player::Enums::BEIE_STOCKPILE] + .id + != 0) { + this->showNoRubbleWhenDestroyingBuilding = 1; + MACRO_CALL_MEMBER(OpenSHC::Map::Buildings::BuildingsState_Func::destroyBuildingAndLinkedDuplicates, + DAT_BuildingsState::ptr)(DAT_GameState::instance.playerDataArray[owner] + .buildingEntryInfos[OpenSHC::Game::Player::Enums::BEIE_STOCKPILE] + .id); + } + if (DAT_BuildingsState::instance.buildings[targetID].buildingType + == OpenSHC::Map::Buildings::BT_STOCKPILE + && DAT_GameState::instance.playerDataArray[owner] + .buildingEntryInfos[OpenSHC::Game::Player::Enums::BEIE_KEEP] + .id + != 0) { + this->showNoRubbleWhenDestroyingBuilding = 1; + MACRO_CALL_MEMBER(OpenSHC::Map::Buildings::BuildingsState_Func::destroyBuildingAndLinkedDuplicates, + DAT_BuildingsState::ptr)(DAT_GameState::instance.playerDataArray[owner] + .buildingEntryInfos[OpenSHC::Game::Player::Enums::BEIE_KEEP] + .id); + } + this->showNoRubbleWhenDestroyingBuilding = 1; + MACRO_CALL_MEMBER(OpenSHC::Map::Buildings::BuildingsState_Func::destroyBuildingAndLinkedDuplicates, + DAT_BuildingsState::ptr)(this->cursorTargetID); + } else { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::spawnEraserTileEffect, this)( + 0, DAT_ViewportRenderState::instance.translationMatrix[brushY].addXgetTile + x); + } + } + + y = savedY; + for (int index = 0; index < brushSize; index++) { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::getTileForBrush, this)( + 1, index, &tile, (int*)&y, baseTile, savedY); + if ((this->LogicLayer[tile] & (L_BORDER | L_BORDER_EDGE)) != 0) { + return; + } + for (int unit = DAT_TileMapState::instance.UnitLayer[tile]; unit != 0; + unit = DAT_UnitsState::instance.units[unit].nextUnitOnTheSameTile) { + DAT_UnitsState::instance.units[unit].logicalState = OpenSHC::Map::Units::ULS_REMOVE; + } + } + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/evaluateBuildingPlacementAtCursor.cpp b/src/OpenSHC/Map/TileMapState/evaluateBuildingPlacementAtCursor.cpp new file mode 100644 index 0000000000..30356b2086 --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/evaluateBuildingPlacementAtCursor.cpp @@ -0,0 +1,223 @@ +#include "../TileMapState.func.hpp" + +#include "OpenSHC/Map/Navigation/PathFindingState.func.hpp" +#include "OpenSHC/Game/GameMode.hpp" +#include "OpenSHC/Game/GameMode2.hpp" +#include "OpenSHC/Map/Buildings/BuildingFailReasonEnum.hpp" +#include "OpenSHC/Map/Buildings/BuildingType.hpp" +#include "OpenSHC/WindowsHelper/Enums/BOOLEnum.hpp" + +#include "OpenSHC/Globals/DAT_BuildingsState.hpp" +#include "OpenSHC/Globals/DAT_GameCore.hpp" +#include "OpenSHC/Globals/DAT_GameState.hpp" +#include "OpenSHC/Globals/DAT_GameSynchronyState.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_BUILDING; + using OpenSHC::Map::LogicHelpers::L_KEEP_NON_MANOR_HOUSE; + using OpenSHC::Map::LogicHelpers::L_MOAT; + using OpenSHC::Map::LogicHelpers::L_STOCKPILEUnk; + using OpenSHC::Map::LogicHelpers::L_WALL_OR_GATEHOUSE; + + using OpenSHC::Map::Buildings::BuildingFailReasonEnum; + + // FUNCTION: STRONGHOLDCRUSADER 0x00504A30 + void TileMapState::evaluateBuildingPlacementAtCursor(int playerID, uint x, uint y) + { + int moatTile = 0; + bool const valid + = x <= 399 && y <= 399 && DAT_ViewportRenderState::instance.DAT_BinaryTileMap400x400[y * 400 + x] != 0; + if (!valid) { + this->buildingPlacementFail = TRUE; + return; + } + uint anchorX = x; + uint anchorY = y; + this->buildingPlacementFail = FALSE; + int wallTile = DAT_ViewportRenderState::instance.viewportState.cursorWallTile; + if (DAT_ViewportRenderState::instance.viewportState.field_0x3c != 0) { + moatTile = DAT_ViewportRenderState::instance.viewportState.cursorTile; + this->savedCursorTile = DAT_ViewportRenderState::instance.viewportState.cursorTile; + } + + // the flat view has no ray hit, so the tile under the mouse is used directly + int buildingID; + if (this->flatViewToggleValue1 != 0) { + buildingID = this->BuildingLayer[DAT_ViewportRenderState::instance.viewportState.mouseTile]; + wallTile = 0; + if ((DAT_TileMapState::instance.LogicLayer[DAT_ViewportRenderState::instance.viewportState.mouseTile] + & L_WALL_OR_GATEHOUSE) + != 0 + && ((DAT_TileMapState::instance.LogicLayer[DAT_ViewportRenderState::instance.viewportState.mouseTile] + & L_STOCKPILEUnk) + == 0 + || buildingID == 0)) { + wallTile = DAT_ViewportRenderState::instance.viewportState.mouseTile; + DAT_ViewportRenderState::instance.viewportState.cursorWallTile = wallTile; + } + } else { + buildingID = DAT_ViewportRenderState::instance.viewportState.mouseRayBuildingID; + } + if (buildingID != 0) { + anchorX = DAT_BuildingsState::instance.buildings[buildingID].x; + anchorY = DAT_BuildingsState::instance.buildings[buildingID].y; + } + + int const enemyDistance + = DAT_GameSynchronyState::instance.currentGameMode != OpenSHC::Game::GM_SOLITARY ? 15 : 30; + if (DAT_GameCore::instance.gameMode_2 != OpenSHC::Game::GM_EDITOR + && DAT_GameCore::instance.gameMode_2 != OpenSHC::Game::GM_SIEGE_THAT + && MACRO_CALL_MEMBER(OpenSHC::Map::Navigation::PathFindingState_Func::isEnemyTooCloseUnk, + DAT_PathFindingState::ptr)(playerID, anchorX, anchorY, enemyDistance)) { + this->demolishBlocked = 1; + this->buildingPlacementFail = TRUE; + this->buildingPlacementFailReason = OpenSHC::Map::Buildings::BRE_UNKNOWN_20; + if (buildingID != 0) { + if (DAT_BuildingsState::instance.buildings[buildingID].owner == playerID || playerID == 0) { + return; + } + this->buildingPlacementFailReason = OpenSHC::Map::Buildings::BFRE_DEFAULT_CANT_PLACE_THAT_THERE; + return; + } + if (wallTile > 0) { + if ((this->LogicLayer[wallTile] & L_WALL_OR_GATEHOUSE) == 0 + || (this->WallOwnerLayer[wallTile] & 7) + 1 == playerID) { + return; + } + this->buildingPlacementFailReason = OpenSHC::Map::Buildings::BFRE_DEFAULT_CANT_PLACE_THAT_THERE; + return; + } + if (moatTile > 0 && (this->LogicLayer[moatTile] & L_MOAT) != 0) { + int const moatID + = MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::returnOwnedMoatAtTile, this)(moatTile); + if (moatID == 0) { + return; + } + if (DAT_GameState::instance.mapAndTime.playerTeams[this->moats[moatID].owner] + == DAT_GameState::instance.mapAndTime.playerTeams[playerID]) { + return; + } + } + this->buildingPlacementFailReason = OpenSHC::Map::Buildings::BFRE_DEFAULT_CANT_PLACE_THAT_THERE; + return; + } + + if (DAT_ViewportRenderState::instance.viewportState.somePitchDitchID != 0) { + if (this->pitchDitches[DAT_ViewportRenderState::instance.viewportState.somePitchDitchID].owner + == DAT_GameSynchronyState::instance.currentPlayerSlotID) { + this->cursorTargetID = -DAT_ViewportRenderState::instance.viewportState.somePitchDitchID; + return; + } + this->buildingPlacementFail = TRUE; + return; + } + + if (buildingID <= 0 + || (DAT_BuildingsState::instance.buildings[buildingID].owner != playerID && playerID != 0)) { + this->buildingPlacementFail = TRUE; + } + + bool checkMoat = true; + if (wallTile > 0) { + // a stockpile tile carries the wall flag too, but nothing may be built on it + if ((this->LogicLayer[wallTile] & L_WALL_OR_GATEHOUSE) != 0 + && (this->LogicLayer[wallTile] & L_STOCKPILEUnk) != 0) { + this->buildingPlacementFail = TRUE; + return; + } + if ((this->LogicLayer[wallTile] & L_WALL_OR_GATEHOUSE) != 0 && this->BuildingLayer[wallTile] == 0 + && this->placementOnMoat == 0) { + if ((this->WallOwnerLayer[wallTile] & 7) + 1 != playerID) { + return; + } + this->buildingPlacementFail = FALSE; + this->placementOnWall = 1; + if ((this->MiscDisplayLayer[wallTile] & 0x400) != 0) { + this->buildingPlacementFail = TRUE; + } + checkMoat = false; + } + } + if (checkMoat) { + if (moatTile > 0 && (this->LogicLayer[moatTile] & L_MOAT) != 0 && buildingID == 0) { + if (this->placementOnWall != 0) { + this->buildingPlacementFail = TRUE; + } else { + int const moatID + = MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::returnOwnedMoatAtTile, this)(moatTile); + if (moatID == 0) { + return; + } + if (DAT_GameState::instance.mapAndTime.playerTeams[this->moats[moatID].owner] + != DAT_GameState::instance.mapAndTime.playerTeams[playerID]) { + return; + } + this->buildingPlacementFail = FALSE; + this->placementOnMoat = 1; + if ((this->MiscDisplayLayer[moatTile] & 0x400) != 0) { + this->buildingPlacementFail = TRUE; + } + wallTile = moatTile; + } + } else if (this->placementOnWall != 0 || this->placementOnMoat != 0) { + this->buildingPlacementFail = TRUE; + } + } + + if (wallTile > 0 && this->UnitLayer[wallTile] != 0) { + this->buildingPlacementFail = TRUE; + } + if (buildingID != 0) { + bool const isRegularGame = DAT_GameCore::instance.gameMode_2 != OpenSHC::Game::GM_EDITOR + && DAT_GameCore::instance.gameMode_2 != OpenSHC::Game::GM_SIEGE_THAT; + if (isRegularGame) { + switch (DAT_BuildingsState::instance.buildings[buildingID].buildingType) { + case OpenSHC::Map::Buildings::BT_UNKNOWN1: + case OpenSHC::Map::Buildings::BT_MANORHOUSE: + case OpenSHC::Map::Buildings::BT_STONEKEEP: + case OpenSHC::Map::Buildings::BT_STRONGHOLD: + case OpenSHC::Map::Buildings::BT_SIGNPOST: + case OpenSHC::Map::Buildings::BT_CAMPGROUND: + case OpenSHC::Map::Buildings::BT_KEEPDOOR_LEFT: + case OpenSHC::Map::Buildings::BT_KEEPDOOR_RIGHT: + case OpenSHC::Map::Buildings::BT_KEEPDOOR: + case OpenSHC::Map::Buildings::BT_POND: + this->buildingPlacementFail = TRUE; + } + } else if (DAT_GameCore::instance.gameMode_2 == OpenSHC::Game::GM_SIEGE_THAT) { + if (DAT_BuildingsState::instance.buildings[buildingID].buildingType + == OpenSHC::Map::Buildings::BT_SIGNPOST) { + this->buildingPlacementFail = TRUE; + } + } + if (buildingID < 0) { + this->cursorTargetID = buildingID; + return; + } + } + + if ((this->LogicLayer[DAT_BuildingsState::instance.buildings[buildingID].currentTilePositionAdjusted] + & (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_KILLINGPIT + && this->placementOnWall == 0 && this->placementOnMoat == 0) { + this->buildingPlacementFail = TRUE; + } + this->cursorTargetID = buildingID; + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/findNearestFriendlyMoatTileForDigging.cpp b/src/OpenSHC/Map/TileMapState/findNearestFriendlyMoatTileForDigging.cpp new file mode 100644 index 0000000000..a7f6b6f403 --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/findNearestFriendlyMoatTileForDigging.cpp @@ -0,0 +1,98 @@ +#include "../TileMapState.func.hpp" + +#include "OpenSHC/Map/Navigation/DirectionAlgorithmState.func.hpp" +#include "OpenSHC/WindowsHelper/Enums/BOOLEnum.hpp" + +#include "OpenSHC/Globals/DAT_DirectionAlgorithmState.hpp" +#include "OpenSHC/Globals/DAT_GameState.hpp" +#include "OpenSHC/Globals/DAT_GameSynchronyState.hpp" +#include "OpenSHC/Globals/DAT_TileMapState.hpp" +#include "OpenSHC/Globals/DAT_UnitsState.hpp" +#include "OpenSHC/Globals/DAT_ViewportRenderState.hpp" + +namespace OpenSHC { +namespace Map { + + using OpenSHC::Map::LogicHelpers::L_MOAT; + using OpenSHC::Map::LogicHelpers::L_MOAT_DUG_OR_PLANNED; + + // FUNCTION: STRONGHOLDCRUSADER 0x005111D0 + BOOLEnum TileMapState::findNearestFriendlyMoatTileForDigging(int playerID, int unitID, int number) + { + int best = 1000; + int bestMoatID = -1; + int const unitX = DAT_UnitsState::instance.units[unitID].x; + int const unitY = DAT_UnitsState::instance.units[unitID].y; + int const area + = (short)this + ->PathConnectionLayer[DAT_ViewportRenderState::instance.translationMatrix[unitY].addXgetTile + unitX]; + for (int moatID = 0; moatID < this->currentMoatCount; moatID++) { + // digging works on own moat, filling in works on anyone else's + if (number == 1) { + if (DAT_GameState::instance.mapAndTime.playerTeams[this->moats[moatID].owner] + == DAT_GameState::instance.mapAndTime.playerTeams[playerID]) { + // the moat belongs to the digger's team + } else { + continue; + } + } else if (number == 2) { + if (DAT_GameState::instance.mapAndTime.playerTeams[this->moats[moatID].owner] + == DAT_GameState::instance.mapAndTime.playerTeams[playerID]) { + continue; + } + } + if (this->moats[moatID].stage != number) { + continue; + } + if (this->BuildingLayer[this->moats[moatID].tile] != 0 && this->moats[moatID].zeroOrTwo == 0) { + continue; + } + int const tile = this->moats[moatID].tile; + if ((this->LogicLayer[tile] & (L_MOAT_DUG_OR_PLANNED | L_MOAT)) == 0) { + continue; + } + if (this->moats[moatID].someCountDown >= 100) { + continue; + } + if (this->UnitLayer[tile] != 0 + && DAT_UnitsState::instance.units[this->UnitLayer[tile]].someUnitStat4 == 0) { + continue; + } + if (number == 2 + && !MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::findTileInSameAreaAndNoTooHeightDifference, + this)(area, tile, this->moats[moatID].y)) { + continue; + } + + int const pathCost + = DAT_TileMapState::instance.SEC_PathfindingCostTileMap1105[this->moats[moatID].owner][tile]; + MACRO_CALL_MEMBER(OpenSHC::Map::Navigation::DirectionAlgorithmState_Func::setAxisBasedDistanceResult, + DAT_DirectionAlgorithmState::ptr)(unitX, unitY, this->moats[moatID].x, this->moats[moatID].y); + int score = DAT_DirectionAlgorithmState::instance.distanceHigh; + int fromTarget; + if (DAT_UnitsState::instance.units[unitID].targetX > 0) { + fromTarget = MACRO_CALL_MEMBER( + OpenSHC::Map::Navigation::DirectionAlgorithmState_Func::setAxisBasedDistanceResult, + DAT_DirectionAlgorithmState::ptr)(DAT_UnitsState::instance.units[unitID].targetX, + DAT_UnitsState::instance.units[unitID].targetY, this->moats[moatID].x, this->moats[moatID].y); + } else { + fromTarget = 0; + } + if (DAT_GameSynchronyState::instance.currentPlayerFullIDArray[playerID] == -1) { + score += pathCost * 10; + } + score += fromTarget / 2 + this->moats[moatID].someCountDown; + if (score < best) { + best = score; + bestMoatID = moatID; + } + } + + // bug:fixme: when no moat qualifies (or there are none) the original indexes moats[-1] here + if (bestMoatID != 0) { + this->moats[bestMoatID].someCountDown += 20; + } + return bestMoatID; + } +} +} diff --git a/src/OpenSHC/Map/TileMapState/floodMoatUnderRemovedBuilding.cpp b/src/OpenSHC/Map/TileMapState/floodMoatUnderRemovedBuilding.cpp new file mode 100644 index 0000000000..1c909e99a1 --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/floodMoatUnderRemovedBuilding.cpp @@ -0,0 +1,49 @@ +#include "../TileMapState.func.hpp" + +#include "OpenSHC/Map/Navigation/PathFindingState.func.hpp" + +#include "OpenSHC/Globals/DAT_BuildingsState.hpp" +#include "OpenSHC/Globals/DAT_PathFindingState.hpp" +#include "OpenSHC/Globals/DAT_TerrainDefinedData.hpp" +#include "OpenSHC/Globals/DAT_ViewportRenderState.hpp" + +namespace OpenSHC { +namespace Map { + + using OpenSHC::Map::LogicHelpers::L_MOAT; + + // FUNCTION: STRONGHOLDCRUSADER 0x00508910 + void TileMapState::floodMoatUnderRemovedBuilding(int buildingID) + { + int const xPosition = DAT_BuildingsState::instance.buildings[buildingID].x; + int const yPosition = DAT_BuildingsState::instance.buildings[buildingID].y; + uint const widthOrHeight = DAT_BuildingsState::instance.buildings[buildingID].widthOrHeight; + int const variation = DAT_BuildingsState::instance.buildings[buildingID].buildingVariation / 2; + int buildingSizeTileIndex = 0; + int const* drawbridgeMapping = DAT_TerrainDefinedData::instance.DrawbridgeOrientationMapping[variation]; + while (true) { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::getBuildingSizeIndexMappingData, this)( + buildingSizeTileIndex, 5); + int const tile + = DAT_ViewportRenderState::instance.translationMatrix[this->buildingY + yPosition].addXgetTile + + this->buildingX + xPosition; + if (*drawbridgeMapping != 0 + && MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::returnOwnedMoatAtTile, this)(tile) != 0) { + this->LogicLayer[tile] |= L_MOAT; + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::setMoatVisualStateAtTile, this)(tile, 2); + } + buildingSizeTileIndex++; + drawbridgeMapping++; + if (buildingSizeTileIndex >= this->constructionTileCount) { + break; + } + } + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::updateBuildingGraphicsLayer, this)(buildingID); + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::updatePathLinkagesForBuilding, this)(buildingID); + MACRO_CALL_MEMBER(OpenSHC::Map::Navigation::PathFindingState_Func::updateWalkAndPathLayer, + DAT_PathFindingState::ptr)(widthOrHeight + 6, xPosition, yPosition); + DAT_PathFindingState::instance.toggleUpdateSeparateAreaTileMap = 1; + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/generateDustClouds.cpp b/src/OpenSHC/Map/TileMapState/generateDustClouds.cpp new file mode 100644 index 0000000000..28928928ab --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/generateDustClouds.cpp @@ -0,0 +1,69 @@ +#include "../TileMapState.func.hpp" + +#include "OpenSHC/Map/Entities/EntityState.func.hpp" +#include "OpenSHC/Random/RNG.func.hpp" +#include "OpenSHC/Map/Entities/EntityType.hpp" + +#include "OpenSHC/Globals/DAT_EntityState.hpp" +#include "OpenSHC/Globals/DAT_ViewportRenderState.hpp" +#include "OpenSHC/Globals/SEC_RNG.hpp" + +namespace OpenSHC { +namespace Map { + + using OpenSHC::Map::Entities::EntityType; + + // FUNCTION: STRONGHOLDCRUSADER 0x004FC490 + void TileMapState::generateDustClouds() + { + int const seed = SEC_RNG::instance.currentNumber2; + MACRO_CALL_MEMBER(OpenSHC::Random::RNG_Func::nextRandomNumber2, SEC_RNG::ptr)(); + this->SEC_Section1052 += 1; + if (seed % 500 == this->SEC_Section1052 % 500) { + this->SEC_Section1053 = seed % 180; + } + if (seed % 1200 == this->SEC_Section1052 % 1200) { + this->SEC_Section1054 = seed % 180; + } + if (this->SEC_Section1053 != 0) { + this->SEC_Section1053 -= 1; + } + if (this->SEC_Section1054 != 0) { + this->SEC_Section1054 -= 1; + } + + for (int i = 0; i < this->temporaryTerrainTypeIndex; i++) { + // one tile out of every 64 puffs, selected by the tile's own random value + if ((this->RandomLayer[this->temporaryTerrainTypeArray[i]] ^ seed) % 64 != this->SEC_Section1052 % 64) { + continue; + } + int const tileY = DAT_ViewportRenderState::instance + .tileTranslationMatrix_YComponent[this->temporaryTerrainTypeArray[i]]; + int const tileX = this->temporaryTerrainTypeArray[i] + - DAT_ViewportRenderState::instance.translationMatrix[tileY].addXgetTile; + int const stage = this->temporaryTerrainTypeBinaryArray[i]; + if (stage == 0) { + if (this->SEC_Section1054 == 0) { + MACRO_CALL_MEMBER( + OpenSHC::Map::Entities::EntityState_Func::spawnProjectileEntity, DAT_EntityState::ptr)(0, 0, 0, + tileX * 8 + 4, tileY * 8 + 4, this->HeightLayer[this->temporaryTerrainTypeArray[i]], 0, 0, 0, + OpenSHC::Map::Entities::ET_DUST_CLOUD, stage); + } + } else if (this->SEC_Section1053 == 0) { + if (stage == 1) { + MACRO_CALL_MEMBER( + OpenSHC::Map::Entities::EntityState_Func::spawnProjectileEntity, DAT_EntityState::ptr)(0, 0, 0, + tileX * 8 + 4, tileY * 8 + 4, this->HeightLayer[this->temporaryTerrainTypeArray[i]], 0, 0, 0, + OpenSHC::Map::Entities::ET_DUST_CLOUD, stage); + } else { + MACRO_CALL_MEMBER( + OpenSHC::Map::Entities::EntityState_Func::spawnProjectileEntity, DAT_EntityState::ptr)(0, 0, 0, + tileX * 8 + 4, tileY * 8 + 4, this->HeightLayer[this->temporaryTerrainTypeArray[i]], 0, 0, 0, + OpenSHC::Map::Entities::ET_DUST_CLOUD, 2); + } + } + } + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/getHeightAtTileIncludingOwnersBuildings.cpp b/src/OpenSHC/Map/TileMapState/getHeightAtTileIncludingOwnersBuildings.cpp new file mode 100644 index 0000000000..f04b084417 --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/getHeightAtTileIncludingOwnersBuildings.cpp @@ -0,0 +1,88 @@ +#include "../TileMapState.func.hpp" + +#include "OpenSHC/Globals/DAT_BuildingsState.hpp" +#include "OpenSHC/Globals/DAT_TileMapState.hpp" +#include "OpenSHC/Globals/DAT_ViewportRenderState.hpp" + +namespace OpenSHC { +namespace Map { + + using OpenSHC::Map::LogicHelpers::L_WALL_OR_GATEHOUSE; + + // FUNCTION: STRONGHOLDCRUSADER 0x004FFF10 + uint TileMapState::getHeightAtTileIncludingOwnersBuildings(int tile, int playerID) + { + // The early layer reads go through the global instance's absolute address, while the + // BuildingLayer reads inside the switch arms go through this; follow each one separately. + if (DAT_TileMapState::instance.PathConnectionLayer[tile] == 0) { + return 0; + } + if ((DAT_TileMapState::instance.MiscDisplayLayer[tile] & 0x1000) != 0) { + return 0; + } + + if ((DAT_TileMapState::instance.LogicLayer[tile] & L_WALL_OR_GATEHOUSE) != 0) { + if (playerID != 0 && (this->WallOwnerLayer[tile] & 7) + 1 != playerID) { + return 0; + } + return DAT_TileMapState::instance.HeightLayer[tile]; + } + + if (DAT_TileMapState::instance.BuildingLayer[tile] == 0) { + return 0; + } + int const building = DAT_TileMapState::instance.BuildingLayer[tile]; + if (playerID != 0 && DAT_BuildingsState::instance.buildings[building].owner != playerID) { + return 0; + } + + switch (DAT_BuildingsState::instance.buildings[building].buildingType) { + case OpenSHC::Map::Buildings::BT_STONEKEEP: + case OpenSHC::Map::Buildings::BT_STRONGHOLD: + case OpenSHC::Map::Buildings::BT_KEEPFOUR: + case OpenSHC::Map::Buildings::BT_KEEPFIVE: + 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: { + int const tileY = DAT_ViewportRenderState::instance.tileTranslationMatrix_YComponent[tile]; + for (int direction = 0; direction < countof(DAT_TileMapState::instance.directionTranslationMatrix[tileY]); + direction++) { + if (this->BuildingLayer[DAT_TileMapState::instance.directionTranslationMatrix[tileY][direction] + tile] + != building) { + return 0; + } + } + return MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::getTotalHeightAtTile, this)(tile); + } + case OpenSHC::Map::Buildings::BT_GATEHOUSELARGE: + case OpenSHC::Map::Buildings::BT_GATEHOUSESMALL: { + int firstDirection; + int secondDirection; + if (DAT_BuildingsState::instance.buildings[building].buildingVariation == 80) { + firstDirection = 2; + secondDirection = 6; + } else { + firstDirection = 0; + secondDirection = 4; + } + int const tileY = DAT_ViewportRenderState::instance.tileTranslationMatrix_YComponent[tile]; + if (this->BuildingLayer[DAT_TileMapState::instance.directionTranslationMatrix[tileY][firstDirection] + tile] + != building) { + return 0; + } + if (this->BuildingLayer[DAT_TileMapState::instance.directionTranslationMatrix[tileY][secondDirection] + + tile] + != building) { + return 0; + } + return this->getTotalHeightAtTile2(tile); + } + default: + return 0; + } + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/getTotalHeightAt.cpp b/src/OpenSHC/Map/TileMapState/getTotalHeightAt.cpp new file mode 100644 index 0000000000..f3386dd489 --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/getTotalHeightAt.cpp @@ -0,0 +1,159 @@ +#include "../TileMapState.func.hpp" + +#include "OpenSHC/Map/Buildings/BuildingsState.func.hpp" +#include "OpenSHC/Map/Buildings/BuildingType.hpp" + +#include "OpenSHC/Globals/DAT_BuildingDefinedData.hpp" +#include "OpenSHC/Globals/DAT_BuildingsState.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_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_ROCKY; + using OpenSHC::Map::LogicHelpers::L_WALL_OR_GATEHOUSE; + + // FUNCTION: STRONGHOLDCRUSADER 0x004FFBA0 + int TileMapState::getTotalHeightAt(int tile, int playerID) + { + int extra = 0; + BOOLEnum exposed = FALSE; + uint const logic = this->LogicLayer[tile]; + if ((logic & L_KEEP_NON_MANOR_HOUSE) != 0) { + int const buildingID = this->BuildingLayer[tile]; + extra = MACRO_CALL_MEMBER(OpenSHC::Map::Buildings::BuildingsState_Func::getBuildingHeightForBuildingID, + DAT_BuildingsState::ptr)(buildingID); + for (int i = 0; i < 8; i++) { + if (this->BuildingLayer[DAT_TileMapState::instance.directionTranslationMatrix[DAT_ViewportRenderState:: + instance.tileTranslationMatrix_YComponent[tile]][i] + + tile] + != buildingID) { + exposed = TRUE; + break; + } + } + if (playerID != 0 && DAT_BuildingsState::instance.buildings[buildingID].owner != playerID) { + exposed = FALSE; + } + } else if ((logic & L_BUILDING) != 0) { + int const buildingID = this->BuildingLayer[tile]; + int const type = DAT_BuildingsState::instance.buildings[buildingID].buildingType; + switch (type) { + case OpenSHC::Map::Buildings::BT_TOWER: + 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: + extra = MACRO_CALL_MEMBER(OpenSHC::Map::Buildings::BuildingsState_Func::getBuildingHeightForBuildingID, + DAT_BuildingsState::ptr)(buildingID) + + 20; + exposed = TRUE; + break; + case OpenSHC::Map::Buildings::BT_MANORHOUSE: + case OpenSHC::Map::Buildings::BT_STONEKEEP: + case OpenSHC::Map::Buildings::BT_STRONGHOLD: + case OpenSHC::Map::Buildings::BT_KEEPFOUR: + case OpenSHC::Map::Buildings::BT_KEEPFIVE: + extra = MACRO_CALL_MEMBER(OpenSHC::Map::Buildings::BuildingsState_Func::getBuildingHeightForBuildingID, + DAT_BuildingsState::ptr)(buildingID); + if (DAT_BuildingsState::instance.buildings[buildingID].buildingType + != OpenSHC::Map::Buildings::BT_MANORHOUSE) { + for (int i = 0; i < 8; i++) { + if (this->BuildingLayer[DAT_TileMapState::instance + .directionTranslationMatrix[DAT_ViewportRenderState::instance + .tileTranslationMatrix_YComponent[tile]][i] + + tile] + != buildingID) { + exposed = TRUE; + break; + } + } + } + break; + case OpenSHC::Map::Buildings::BT_GATEHOUSELARGE: + case OpenSHC::Map::Buildings::BT_GATEHOUSESMALL: + extra = MACRO_CALL_MEMBER(OpenSHC::Map::Buildings::BuildingsState_Func::getBuildingHeightForBuildingID, + DAT_BuildingsState::ptr)(buildingID); + exposed = TRUE; + break; + default: + extra = DAT_BuildingDefinedData::instance.BuildingHeights[type]; + break; + } + if (playerID != 0 && DAT_BuildingsState::instance.buildings[buildingID].owner != playerID) { + exposed = FALSE; + } + } else if ((logic & L_ROCKY) != 0 && (logic & L_WALL_OR_GATEHOUSE) == 0) { + // rock ids are biased by 2000 + int organism = this->OrganismLayer[tile]; + if (organism >= 2000) { + organism -= 2000; + if (DAT_LandscapeState::instance.rocks[organism].type >= 13) { + extra = 70; + } else if (DAT_LandscapeState::instance.rocks[organism].type >= 9) { + extra = 50; + } + } + if (playerID != 0 && (this->WallOwnerLayer[tile] & 7) + 1 != playerID) { + exposed = FALSE; + } + } else if ((logic & L_CRENEL_VARIATIONUnk) != 0) { + extra = 20; + exposed = TRUE; + if (playerID != 0 && (this->WallOwnerLayer[tile] & 7) + 1 != playerID) { + exposed = FALSE; + } + } else if ((logic & L_CRENEL) != 0) { + extra = 10; + exposed = TRUE; + if (playerID != 0 && (this->WallOwnerLayer[tile] & 7) + 1 != playerID) { + exposed = FALSE; + } + } else if (this->BuildingLayer[tile] != 0) { + int const buildingID = this->BuildingLayer[tile]; + extra = 30; + if (DAT_BuildingsState::instance.buildings[buildingID].buildingType + == OpenSHC::Map::Buildings::BT_GATEHOUSELARGE + || DAT_BuildingsState::instance.buildings[buildingID].buildingType + == OpenSHC::Map::Buildings::BT_GATEHOUSESMALL) { + // a gatehouse only counts as cover while both of its span tiles are still standing + int first; + int second; + if (DAT_BuildingsState::instance.buildings[buildingID].buildingVariation == 80) { + first = 2; + second = 6; + } else { + first = 0; + second = 4; + } + int const tileY = DAT_ViewportRenderState::instance.tileTranslationMatrix_YComponent[tile]; + if (this->BuildingLayer[DAT_TileMapState::instance.directionTranslationMatrix[tileY][first] + tile] + != buildingID) { + exposed = TRUE; + } + if (this->BuildingLayer[DAT_TileMapState::instance.directionTranslationMatrix[tileY][second] + tile] + != buildingID) { + exposed = TRUE; + } + } + if (playerID != 0 && DAT_BuildingsState::instance.buildings[buildingID].owner != playerID) { + exposed = FALSE; + } + } + + extra += this->HeightLayer[tile]; + if (exposed) { + return extra; + } + return -1 - extra; + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/getTotalHeightAtTile.cpp b/src/OpenSHC/Map/TileMapState/getTotalHeightAtTile.cpp index 8703894f77..d6b3c922f6 100644 --- a/src/OpenSHC/Map/TileMapState/getTotalHeightAtTile.cpp +++ b/src/OpenSHC/Map/TileMapState/getTotalHeightAtTile.cpp @@ -32,5 +32,30 @@ namespace Map { return this->HeightLayer[tile]; } + // the original's getHeightAtTileIncludingOwnersBuildings calls two functions with this body; its linker folded + // them into one address, so this one is only reachable from reimplemented callers + uint TileMapState::getTotalHeightAtTile2(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/isBuildingPlacementAllowedAtTile.cpp b/src/OpenSHC/Map/TileMapState/isBuildingPlacementAllowedAtTile.cpp new file mode 100644 index 0000000000..2989143a06 --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/isBuildingPlacementAllowedAtTile.cpp @@ -0,0 +1,220 @@ +#include "../TileMapState.func.hpp" + +#include "OpenSHC/Synchrony/GameSynchronyState.func.hpp" +#include "OpenSHC/Commands/MappersEnum.hpp" +#include "OpenSHC/Game/GameMode.hpp" +#include "OpenSHC/Map/Units/UnitType.hpp" +#include "OpenSHC/WindowsHelper/Enums/BOOLEnum.hpp" + +#include "OpenSHC/Globals/DAT_GameSynchronyState.hpp" +#include "OpenSHC/Globals/DAT_LandscapeState.hpp" +#include "OpenSHC/Globals/DAT_UnitsState.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_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; + 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_PLAIN1_AND_FARM; + using OpenSHC::Map::LogicHelpers::L_PLAIN2_AND_PITCH; + using OpenSHC::Map::LogicHelpers::L_RIVER; + using OpenSHC::Map::LogicHelpers::L_ROCKY; + using OpenSHC::Map::LogicHelpers::L_SEA; + using OpenSHC::Map::LogicHelpers::L_TREE; + using OpenSHC::Map::LogicHelpers::L_TREE_VARIATION; + using OpenSHC::Map::LogicHelpers::L_WALL_OR_GATEHOUSE; + + // FUNCTION: STRONGHOLDCRUSADER 0x004F9A60 + int TileMapState::isBuildingPlacementAllowedAtTile( + int tile, int playerID, MappersEnum commandBuildingType, int ignoreFarmland) + { + uint const logic = this->LogicLayer[tile]; + int const building = this->BuildingLayer[tile]; + BOOLEnum onRock = FALSE; + BOOLEnum onMarsh = FALSE; + BOOLEnum onMoat = FALSE; + int height = this->HeightLayer[tile]; + if ((logic & L_WALL_OR_GATEHOUSE) != 0 && (this->WallOwnerLayer[tile] & 7) + 1 == playerID) { + // a gate or tower on our own wall is measured against the ground, not the wall top + switch (commandBuildingType) { + case OpenSHC::Commands::M_MAPPER_GATEHOUSE: + case OpenSHC::Commands::M_MAPPER_GATE_MAIN: + case OpenSHC::Commands::M_MAPPER_GATE_INNER: + case OpenSHC::Commands::M_MAPPER_GATE_WOOD: + case OpenSHC::Commands::M_MAPPER_GATE_POSTERN: + case OpenSHC::Commands::M_MAPPER_TOWER1: + case OpenSHC::Commands::M_MAPPER_TOWER2: + case OpenSHC::Commands::M_MAPPER_TOWER3: + case OpenSHC::Commands::M_MAPPER_TOWER4: + case OpenSHC::Commands::M_MAPPER_TOWER5: + 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_GATE_STONE1A: + case OpenSHC::Commands::M_MAPPER_GATE_STONE1B: + case OpenSHC::Commands::M_MAPPER_GATE_STONE2A: + case OpenSHC::Commands::M_MAPPER_GATE_STONE2B: + height = this->DefaultHeightLayer[tile]; + } + } + + if (this->buildingHeightLimit >= 0) { + if (height > this->buildingHeightLimit) { + if (commandBuildingType == OpenSHC::Commands::M_MAPPER_CATTLEFARM) { + this->placementWarning = 1; + } else if (commandBuildingType == OpenSHC::Commands::M_MAPPER_APPLEFARM) { + this->placementWarning = 2; + } else if (commandBuildingType == OpenSHC::Commands::M_MAPPER_HOPSFARM) { + this->placementWarning = 3; + } else if (commandBuildingType == OpenSHC::Commands::M_MAPPER_WHEATFARM) { + this->placementWarning = 4; + } + return 1; + } + } else if (height < -this->buildingHeightLimit) { + return 1; + } + if (height > this->buildingMaxHeightDifference + this->buildingMinHeight) { + return 1; + } + if ((logic & L_PLAIN2_AND_PITCH) != 0 && commandBuildingType == OpenSHC::Commands::M_MAPPER_PITCH_DITCH) { + return 1; + } + if (building != 0) { + return 2; + } + + if (this->UnitLayer[tile] != 0) { + int const unitID = this->UnitLayer[tile]; + if (MACRO_CALL_MEMBER(OpenSHC::Synchrony::GameSynchronyState_Func::isAIPlayer, DAT_GameSynchronyState::ptr)( + playerID)) { + if (DAT_UnitsState::instance.units[unitID].unitType == OpenSHC::Map::Units::UT_LORD) { + return 1; + } + if (DAT_UnitsState::instance.units[unitID].isSelectable_OR_matchTime != 0 + && DAT_UnitsState::instance.units[unitID].owner != playerID) { + return 1; + } + } else if (DAT_UnitsState::instance.units[unitID].unitType != OpenSHC::Map::Units::UT_CHICKEN) { + return 1; + } + } + + if ((logic & L_SEA) != 0) { + return 1; + } + if ((logic & (L_BORDER | L_BORDER_EDGE)) != 0) { + return 1; + } + if ((logic & L_RIVER) != 0) { + return 1; + } + if ((logic & L_WALL_OR_GATEHOUSE) != 0) { + if ((this->WallOwnerLayer[tile] & 7) + 1 != playerID) { + return 1; + } + // only a gate or a tower may be built onto a wall + switch (commandBuildingType) { + case OpenSHC::Commands::M_MAPPER_GATEHOUSE: + case OpenSHC::Commands::M_MAPPER_GATE_MAIN: + case OpenSHC::Commands::M_MAPPER_GATE_INNER: + case OpenSHC::Commands::M_MAPPER_GATE_WOOD: + case OpenSHC::Commands::M_MAPPER_GATE_POSTERN: + case OpenSHC::Commands::M_MAPPER_TOWER1: + case OpenSHC::Commands::M_MAPPER_TOWER2: + case OpenSHC::Commands::M_MAPPER_TOWER3: + case OpenSHC::Commands::M_MAPPER_TOWER4: + case OpenSHC::Commands::M_MAPPER_TOWER5: + 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_GATE_STONE1A: + case OpenSHC::Commands::M_MAPPER_GATE_STONE1B: + case OpenSHC::Commands::M_MAPPER_GATE_STONE2A: + case OpenSHC::Commands::M_MAPPER_GATE_STONE2B: + break; + default: + return 1; + } + } + if ((logic & L_PLAIN1_AND_FARM) != 0 && ignoreFarmland == 0) { + return 1; + } + if ((logic & (L_BUILDING | L_KEEP_NON_MANOR_HOUSE)) != 0) { + return 1; + } + + if ((logic & (L_TREE | L_TREE_VARIATION)) != 0) { + int const organism = this->OrganismLayer[tile]; + if (organism != 0 && organism < 2000) { + // scrub can be built over, a real tree cannot, except by the AI outside solitary + switch (DAT_LandscapeState::instance.trees[organism].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: + break; + default: + if (DAT_GameSynchronyState::instance.currentGameMode == OpenSHC::Game::GM_SOLITARY) { + return 1; + } + if (!MACRO_CALL_MEMBER(OpenSHC::Synchrony::GameSynchronyState_Func::isAIPlayer, + DAT_GameSynchronyState::ptr)(playerID)) { + return 1; + } + } + } + } + + if ((logic & (L_FARM_FIELD_WHEAT | L_FARM_FIELD_HOP | L_FARM_FIELD_APPLE | L_FARM_FIELD_DAIRY)) != 0 + && ignoreFarmland == 0) { + return 1; + } + if ((logic & L_FORD) != 0) { + return 1; + } + + if ((logic & L_ROCKY) != 0 && (logic & L_WALL_OR_GATEHOUSE) == 0) { + onRock = TRUE; + } + if ((logic & L_MARSH) != 0) { + onMarsh = TRUE; + } + if ((logic & L_MOAT) != 0) { + onMoat = TRUE; + } + if (onRock && this->buildingPlacementProperty_4 == 0) { + return 1; + } + if (onMoat && this->buildingPlacementProperty_7 == 0) { + return 1; + } + if (this->buildingPlacementProperty_6 != 2 && onMarsh && this->buildingPlacementProperty_6 == 0) { + return 1; + } + return 0; + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/isTileSuitableForBrushPlacement.cpp b/src/OpenSHC/Map/TileMapState/isTileSuitableForBrushPlacement.cpp new file mode 100644 index 0000000000..fdc96fb844 --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/isTileSuitableForBrushPlacement.cpp @@ -0,0 +1,84 @@ +#include "../TileMapState.func.hpp" + +#include "OpenSHC/Rendering/ViewportRenderState.func.hpp" +#include "OpenSHC/WindowsHelper/Enums/BOOLEnum.hpp" + +#include "OpenSHC/Globals/DAT_ViewportRenderState.hpp" + +namespace OpenSHC { +namespace Map { + + // FUNCTION: STRONGHOLDCRUSADER 0x004FBE70 + BOOLEnum TileMapState::isTileSuitableForBrushPlacement(int tile, uint x, uint y) + { + byte height = this->HeightLayer[tile]; + // the tile suits a brush as soon as it stands above any one of its eight neighbours + uint neighbourY = y - 1; + if (x <= 399 && neighbourY <= 399 + && DAT_ViewportRenderState::instance.DAT_BinaryTileMap400x400[neighbourY * 400 + x] != 0 + && (int)(height + - this->HeightLayer[DAT_ViewportRenderState::instance.translationMatrix[y - 1].addXgetTile + x]) + > 0) { + return TRUE; + } + uint neighbourX = x + 1; + if (neighbourX <= 399 && neighbourY <= 399 + && DAT_ViewportRenderState::instance.DAT_BinaryTileMap400x400[neighbourY * 400 + neighbourX] != 0 + && (int)(height + - this->HeightLayer[DAT_ViewportRenderState::instance.translationMatrix[y - 1].addXgetTile + x + 1]) + > 0) { + return TRUE; + } + neighbourX = x + 1; + if (neighbourX <= 399 && y <= 399 + && DAT_ViewportRenderState::instance.DAT_BinaryTileMap400x400[y * 400 + neighbourX] != 0 + && (int)(height + - this->HeightLayer[DAT_ViewportRenderState::instance.translationMatrix[y].addXgetTile + x + 1]) + > 0) { + return TRUE; + } + neighbourY = y + 1; + if (neighbourX <= 399 && neighbourY <= 399 + && DAT_ViewportRenderState::instance.DAT_BinaryTileMap400x400[neighbourY * 400 + neighbourX] != 0 + && (int)(height + - this->HeightLayer[DAT_ViewportRenderState::instance.translationMatrix[y + 1].addXgetTile + x + 1]) + > 0) { + return TRUE; + } + if (x <= 399 && neighbourY <= 399 + && DAT_ViewportRenderState::instance.DAT_BinaryTileMap400x400[neighbourY * 400 + x] != 0 + && (int)(height + - this->HeightLayer[DAT_ViewportRenderState::instance.translationMatrix[y + 1].addXgetTile + x]) + > 0) { + return TRUE; + } + neighbourX = x - 1; + if (neighbourX <= 399 && neighbourY <= 399 + && DAT_ViewportRenderState::instance.DAT_BinaryTileMap400x400[neighbourY * 400 + neighbourX] != 0 + && (int)(height + - this->HeightLayer[DAT_ViewportRenderState::instance.translationMatrix[y + 1].addXgetTile + x - 1]) + > 0) { + return TRUE; + } + neighbourX = x - 1; + if (neighbourX <= 399 && y <= 399 + && DAT_ViewportRenderState::instance.DAT_BinaryTileMap400x400[y * 400 + neighbourX] != 0 + && (int)(height + - this->HeightLayer[DAT_ViewportRenderState::instance.translationMatrix[y].addXgetTile + x - 1]) + > 0) { + return TRUE; + } + + if (MACRO_CALL_MEMBER(OpenSHC::Rendering::ViewportRenderState_Func::xyAreValid, DAT_ViewportRenderState::ptr)( + neighbourX, y - 1) + != FALSE + && (int)(height + - this->HeightLayer[DAT_ViewportRenderState::instance.translationMatrix[y - 1].addXgetTile + x - 1]) + > 0) { + return TRUE; + } + return FALSE; + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/isUnitBlockingSizeFiveFootprint.cpp b/src/OpenSHC/Map/TileMapState/isUnitBlockingSizeFiveFootprint.cpp new file mode 100644 index 0000000000..dec4ff3d30 --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/isUnitBlockingSizeFiveFootprint.cpp @@ -0,0 +1,40 @@ +#include "../TileMapState.func.hpp" + +#include "OpenSHC/WindowsHelper/Enums/BOOLEnum.hpp" + +#include "OpenSHC/Globals/DAT_BuildingsState.hpp" +#include "OpenSHC/Globals/DAT_TerrainDefinedData.hpp" +#include "OpenSHC/Globals/DAT_ViewportRenderState.hpp" + +namespace OpenSHC { +namespace Map { + + // FUNCTION: STRONGHOLDCRUSADER 0x004FAEE0 + BOOLEnum TileMapState::isUnitBlockingSizeFiveFootprint(int buildingID) + { + int const buildingX = DAT_BuildingsState::instance.buildings[buildingID].x; + int const buildingY = DAT_BuildingsState::instance.buildings[buildingID].y; + int const variation = DAT_BuildingsState::instance.buildings[buildingID].buildingVariation / 2; + int buildingSizeTileIndex = 0; + int const* mapping = DAT_TerrainDefinedData::instance.field_0x21ec[variation]; + while (true) { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::getBuildingSizeIndexMappingData, this)( + buildingSizeTileIndex, 5); + if (*mapping == 0 + && this->UnitLayer[DAT_ViewportRenderState::instance.translationMatrix[this->buildingY + buildingY] + .addXgetTile + + this->buildingX + buildingX] + != 0) { + return TRUE; + } + buildingSizeTileIndex++; + mapping++; + if (buildingSizeTileIndex >= this->constructionTileCount) { + break; + } + } + return FALSE; + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/isValidCastleSiteLocation.cpp b/src/OpenSHC/Map/TileMapState/isValidCastleSiteLocation.cpp new file mode 100644 index 0000000000..3761fa75d4 --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/isValidCastleSiteLocation.cpp @@ -0,0 +1,101 @@ +#include "../TileMapState.func.hpp" + +#include "OpenSHC/Map/Navigation/PathFindingState.func.hpp" +#include "OpenSHC/Game/GameMode.hpp" +#include "OpenSHC/Game/GameMode2.hpp" +#include "OpenSHC/WindowsHelper/Enums/BOOLEnum.hpp" + +#include "OpenSHC/Globals/DAT_GameCore.hpp" +#include "OpenSHC/Globals/DAT_GameSynchronyState.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_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 0x005024F0 + BOOLEnum TileMapState::isValidCastleSiteLocation(int x, uint y, int cbt) + { + int tile = DAT_ViewportRenderState::instance.translationMatrix[y].addXgetTile + x; + int const baseTile = tile; + uint const baseY = y; + int validTiles = 0; + + int enemyRange; + int searchRange; + if (DAT_GameSynchronyState::instance.currentGameMode != OpenSHC::Game::GM_SOLITARY) { + enemyRange = 15; + searchRange = 7; + } else { + enemyRange = 30; + searchRange = 30; + } + + for (int index = 0; index < 9; index++) { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::getTileForBrush, this)( + 0, index, &tile, (int*)&y, baseTile, baseY); + int const brushTile = *(int volatile*)&tile; + if (this->OrganismLayer[brushTile] != 0) { + continue; + } + if ((this->LogicLayer[brushTile] & (L_BUILDING | L_KEEP_NON_MANOR_HOUSE)) != 0) { + continue; + } + if (this->BuildingLayer[brushTile] != 0) { + continue; + } + if ((this->LogicLayer[brushTile] & (L_WALL_OR_GATEHOUSE | L_BOULDERS | L_IRON | L_RIVER | L_FORD | L_MARSH)) + != 0) { + continue; + } + if ((this->LogicLayer[brushTile] & (L_SEA | L_PLAIN1_AND_FARM | L_ROCKY)) != 0) { + continue; + } + + uint const brushY = *(uint volatile*)&y; + int const tileX = brushTile - DAT_ViewportRenderState::instance.translationMatrix[brushY].addXgetTile; + if (DAT_GameCore::instance.gameMode_2 != OpenSHC::Game::GM_EDITOR + && DAT_GameCore::instance.gameMode_2 != OpenSHC::Game::GM_SIEGE_THAT + && MACRO_CALL_MEMBER( + OpenSHC::Map::Navigation::PathFindingState_Func::isEnemyTooCloseUnk, DAT_PathFindingState::ptr)( + DAT_GameSynchronyState::instance.currentPlayerSlotID, tileX, brushY, enemyRange)) { + continue; + } + if (DAT_GameSynchronyState::instance.currentGameMode != OpenSHC::Game::GM_SOLITARY) { + int const buildRange + = MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::getCastleBuildRangeForMapSize, this)(); + if (MACRO_CALL_MEMBER(OpenSHC::Map::Navigation::PathFindingState_Func::isOpponentBuildingInRange, + DAT_PathFindingState::ptr)(DAT_GameSynchronyState::instance.currentPlayerSlotID, tileX, brushY, + searchRange, -1, -1, buildRange + 5)) { + continue; + } + } + + // a moat site must be clear of moat, a moat-fill site must sit on one + if (cbt == 106) { + if ((this->LogicLayer[brushTile] & (L_MOAT | L_MOAT_DUG_OR_PLANNED)) == 0) { + validTiles += 1; + } + } else if (cbt != 107 || (this->LogicLayer[brushTile] & L_MOAT_DUG_OR_PLANNED) != 0) { + validTiles += 1; + } + } + return (BOOLEnum)(validTiles != 0); + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/placeApplefarm.cpp b/src/OpenSHC/Map/TileMapState/placeApplefarm.cpp new file mode 100644 index 0000000000..facb517306 --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/placeApplefarm.cpp @@ -0,0 +1,90 @@ +#include "../TileMapState.func.hpp" + +#include "OpenSHC/Map/Buildings/BuildingsState.func.hpp" +#include "OpenSHC/Map/LandscapeState.func.hpp" +#include "OpenSHC/UI/BottomLeftTextDisplayState.func.hpp" +#include "OpenSHC/DE/SHCDE/eTextSections.hpp" +#include "OpenSHC/Map/Buildings/BuildingType.hpp" +#include "OpenSHC/Map/LogicHelpers/Logic1.hpp" +#include "OpenSHC/Map/LogicHelpers/Logic2.hpp" + +#include "OpenSHC/Globals/DAT_BottomLeftTextDisplayState.hpp" +#include "OpenSHC/Globals/DAT_BuildingsState.hpp" +#include "OpenSHC/Globals/DAT_GameState.hpp" +#include "OpenSHC/Globals/DAT_GameSynchronyState.hpp" +#include "OpenSHC/Globals/DAT_LandscapeState.hpp" +#include "OpenSHC/Globals/DAT_TerrainDefinedData.hpp" +#include "OpenSHC/Globals/DAT_ViewportRenderState.hpp" + +namespace OpenSHC { +namespace Map { + + using OpenSHC::Map::LogicHelpers::L2_THICK_SCRUB; + using OpenSHC::Map::LogicHelpers::L_BUILDING; + using OpenSHC::Map::LogicHelpers::L_FARM_FIELD_APPLE; + using OpenSHC::Map::LogicHelpers::L_NONE; + + // FUNCTION: STRONGHOLDCRUSADER 0x00515740 + void TileMapState::placeApplefarm(int playerID, uint x, uint y, BuildingTypeShort buildingType, + undefined4 buildingSize, int variation, int averageHeight) + { + int index = 0; + int buildingID = MACRO_CALL_MEMBER( + OpenSHC::Map::Buildings::BuildingsState_Func::setupBuildingData, DAT_BuildingsState::ptr)( + playerID, x, y, averageHeight, (BuildingType)(short)buildingType, 3, playerID, variation); + this->placedBuildingID = buildingID; + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::markBuildingFootprintFlag, this)(x, y, 10); + + // the 8 apple tree tiles + for (int i = 0; i < countof(DAT_TerrainDefinedData::instance.AppleFarmOffsets); i++) { + DAT_BuildingsState::instance.buildings[buildingID].tileRefs[i] + = DAT_ViewportRenderState::instance + .translationMatrix[DAT_TerrainDefinedData::instance.AppleFarmOffsets[i].y + y] + .addXgetTile + + DAT_TerrainDefinedData::instance.AppleFarmOffsets[i].x + x; + } + + // the farm house + while (true) { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::getBuildingSizeIndexMappingData, this)(index, 3); + int tile = DAT_ViewportRenderState::instance.translationMatrix[this->buildingY + y].addXgetTile + x + + this->buildingX; + this->HeightLayer[tile] = (byte)averageHeight; + this->LogicLayer[tile] |= L_BUILDING; + this->BuildingLayer[tile] = (short)buildingID; + this->BuildingWasLayer[tile] = (uchar)buildingType; + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::setTerrain, this)( + playerID, tile, this->buildingY + y, 2, L_NONE, L2_THICK_SCRUB); + index++; + this->ChangedLayer[tile] = 2; + if (index >= this->constructionTileCount) { + break; + } + } + + for (int i = 0; i < countof(DAT_TerrainDefinedData::instance.AppleFarmOffsets); i++) { + int tile = DAT_BuildingsState::instance.buildings[buildingID].tileRefs[i]; + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::resetTileAndClearMoat, this)(tile); + this->LogicLayer[tile] |= L_FARM_FIELD_APPLE; + this->ChangedLayer[tile] = 2; + MACRO_CALL_MEMBER(OpenSHC::Map::LandscapeState_Func::placeAppleTree, DAT_LandscapeState::ptr)(buildingID, + DAT_TerrainDefinedData::instance.AppleFarmOffsets[i].x + x, + DAT_TerrainDefinedData::instance.AppleFarmOffsets[i].y + y); + } + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::setMiscDisplayLayer, this)(buildingID); + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::updatePathLinkagesForBuilding, this)(buildingID); + + // not enough idle peasants for the workers: tell the local player + if (playerID == DAT_GameSynchronyState::instance.currentPlayerSlotID + && DAT_BuildingsState::instance.buildings[buildingID].buildingTypeBasedEmployeeCount + > DAT_GameState::instance.playerDataArray[playerID].availablePeasantsAtFire + && (DAT_GameState::instance.playerDataArray[playerID].populationCap + <= DAT_GameState::instance.playerDataArray[playerID].currentPopulation + || DAT_GameState::instance.playerDataArray[playerID].popularity < 5000)) { + MACRO_CALL_MEMBER(OpenSHC::UI::BottomLeftTextDisplayState_Func::setBottomLeftTextDisplayText, + DAT_BottomLeftTextDisplayState::ptr)(1, OpenSHC::DE::SHCDE::TEXT_PANEL_FEEDBACK, 1, 0, 100, 6000); + } + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/placeBarracks.cpp b/src/OpenSHC/Map/TileMapState/placeBarracks.cpp new file mode 100644 index 0000000000..93ad38f6ef --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/placeBarracks.cpp @@ -0,0 +1,179 @@ +#include "../TileMapState.func.hpp" + +#include "OpenSHC/Map/Buildings/BuildingsState.func.hpp" +#include "OpenSHC/Map/Navigation/PathFindingState.func.hpp" +#include "OpenSHC/UI/BottomLeftTextDisplayState.func.hpp" +#include "OpenSHC/DE/SHCDE/eTextSections.hpp" +#include "OpenSHC/Map/Buildings/BuildingType.hpp" + +#include "OpenSHC/Globals/DAT_BottomLeftTextDisplayState.hpp" +#include "OpenSHC/Globals/DAT_BuildingsState.hpp" +#include "OpenSHC/Globals/DAT_GameCore.hpp" +#include "OpenSHC/Globals/DAT_GameState.hpp" +#include "OpenSHC/Globals/DAT_GameSynchronyState.hpp" +#include "OpenSHC/Globals/DAT_PathFindingState.hpp" +#include "OpenSHC/Globals/DAT_TerrainDefinedData.hpp" +#include "OpenSHC/Globals/DAT_ViewportRenderState.hpp" + +namespace OpenSHC { +namespace Map { + + using OpenSHC::Map::LogicHelpers::L_BUILDING; + + // FUNCTION: STRONGHOLDCRUSADER 0x005076A0 + void TileMapState::placeBarracks(int playerID, uint x, uint y, undefined4 buildingType, uint buildingSize, + int buildingOrientation, undefined4 averageHeight) + { + uint const baseX = x; + uint const baseY = y; + int const uniqueID = DAT_GameCore::instance.uniqueGameObjectTracker; + int index = 0; + int partIndex; + if (buildingOrientation == 15) { + partIndex = 0; + } else { + partIndex = buildingOrientation / 2; + } + ushort placedID = MACRO_CALL_MEMBER( + OpenSHC::Map::Buildings::BuildingsState_Func::setupBuildingData, DAT_BuildingsState::ptr)( + playerID, x, y, averageHeight, (BuildingType)(short)buildingType, buildingSize, playerID, 15); + int buildingID = (short)placedID; + this->placedBuildingID = buildingID; + DAT_BuildingsState::instance.buildings[buildingID].uidWhenPlaced = uniqueID; + while (true) { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::getBuildingSizeIndexMappingData, this)( + index, buildingSize); + int const tile = DAT_ViewportRenderState::instance.translationMatrix[this->buildingY + y].addXgetTile + + this->buildingX + x; + DAT_BuildingsState::instance.buildings[buildingID].tileRefs[index] = tile; + this->HeightLayer[tile] = (byte)averageHeight; + this->LogicLayer[tile] |= L_BUILDING; + this->BuildingLayer[tile] = placedID; + this->BuildingWasLayer[tile] = (uchar)buildingType; + this->ChangedLayer[tile] = 2; + index++; + if (index >= this->constructionTileCount) { + break; + } + } + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::setMiscDisplayLayer, this)(buildingID); + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::updatePathLinkagesForBuilding, this)(buildingID); + + x += DAT_TerrainDefinedData::instance.BuildingPartsOffsets[partIndex][0].x; + y += DAT_TerrainDefinedData::instance.BuildingPartsOffsets[partIndex][0].y; + placedID = MACRO_CALL_MEMBER( + OpenSHC::Map::Buildings::BuildingsState_Func::setupBuildingData, DAT_BuildingsState::ptr)( + playerID, x, y, averageHeight, OpenSHC::Map::Buildings::BT_PARADEGROUND3, 5, playerID, 15); + buildingID = (short)placedID; + DAT_BuildingsState::instance.buildings[buildingID].uidWhenPlaced = uniqueID; + DAT_BuildingsState::instance.buildings[buildingID].quarryStockpileID = (short)this->placedBuildingID; + int campIndex = 0; + while (true) { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::getBuildingSizeIndexMappingData, this)( + campIndex, DAT_BuildingsState::instance.buildings[buildingID].widthOrHeight); + int const tile = DAT_ViewportRenderState::instance.translationMatrix[this->buildingY + y].addXgetTile + + this->buildingX + x; + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::resetTileAndClearMoat, this)(tile); + this->HeightLayer[tile] = (byte)averageHeight; + if (this->buildingX == 2 && this->buildingY == 2) { + this->LogicLayer[tile] |= L_BUILDING; + } + this->BuildingLayer[tile] = placedID; + this->BuildingWasLayer[tile] = (uchar)buildingType; + this->ChangedLayer[tile] = 2; + campIndex++; + if (campIndex >= this->constructionTileCount) { + break; + } + } + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::setMiscDisplayLayer, this)(buildingID); + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::updatePathLinkagesForBuilding, this)(buildingID); + MACRO_CALL_MEMBER(OpenSHC::Map::Navigation::PathFindingState_Func::updateWalkAndPathLayer, + DAT_PathFindingState::ptr)(8, x, y); + + x = DAT_TerrainDefinedData::instance.BuildingPartsOffsets[partIndex][1].x + baseX; + y = DAT_TerrainDefinedData::instance.BuildingPartsOffsets[partIndex][1].y + baseY; + placedID = MACRO_CALL_MEMBER( + OpenSHC::Map::Buildings::BuildingsState_Func::setupBuildingData, DAT_BuildingsState::ptr)( + playerID, x, y, averageHeight, OpenSHC::Map::Buildings::BT_PARADEGROUND4, 5, playerID, 15); + buildingID = (short)placedID; + DAT_BuildingsState::instance.buildings[buildingID].uidWhenPlaced = uniqueID; + DAT_BuildingsState::instance.buildings[buildingID].quarryStockpileID = (short)this->placedBuildingID; + campIndex = 0; + while (true) { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::getBuildingSizeIndexMappingData, this)( + campIndex, DAT_BuildingsState::instance.buildings[buildingID].widthOrHeight); + int const tile = DAT_ViewportRenderState::instance.translationMatrix[this->buildingY + y].addXgetTile + + this->buildingX + x; + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::resetTileAndClearMoat, this)(tile); + this->HeightLayer[tile] = (byte)averageHeight; + if (this->buildingX == 2 && this->buildingY == 2) { + this->LogicLayer[tile] |= L_BUILDING; + } + this->BuildingLayer[tile] = placedID; + this->BuildingWasLayer[tile] = (uchar)buildingType; + this->ChangedLayer[tile] = 2; + campIndex++; + if (campIndex >= this->constructionTileCount) { + break; + } + } + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::setMiscDisplayLayer, this)(buildingID); + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::updatePathLinkagesForBuilding, this)(buildingID); + MACRO_CALL_MEMBER(OpenSHC::Map::Navigation::PathFindingState_Func::updateWalkAndPathLayer, + DAT_PathFindingState::ptr)(8, x, y); + + x = DAT_TerrainDefinedData::instance.BuildingPartsOffsets[partIndex][2].x + baseX; + y = DAT_TerrainDefinedData::instance.BuildingPartsOffsets[partIndex][2].y + baseY; + placedID = MACRO_CALL_MEMBER( + OpenSHC::Map::Buildings::BuildingsState_Func::setupBuildingData, DAT_BuildingsState::ptr)( + playerID, x, y, averageHeight, OpenSHC::Map::Buildings::BT_PARADEGROUND2, 5, playerID, 15); + buildingID = (short)placedID; + DAT_BuildingsState::instance.buildings[buildingID].uidWhenPlaced = uniqueID; + DAT_BuildingsState::instance.buildings[buildingID].quarryStockpileID = (short)this->placedBuildingID; + campIndex = 0; + while (true) { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::getBuildingSizeIndexMappingData, this)( + campIndex, DAT_BuildingsState::instance.buildings[buildingID].widthOrHeight); + int const tile = DAT_ViewportRenderState::instance.translationMatrix[this->buildingY + y].addXgetTile + + this->buildingX + x; + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::resetTileAndClearMoat, this)(tile); + this->HeightLayer[tile] = (byte)averageHeight; + if (this->buildingX == 2 && this->buildingY == 2) { + this->LogicLayer[tile] |= L_BUILDING; + } + this->BuildingLayer[tile] = placedID; + this->BuildingWasLayer[tile] = (uchar)buildingType; + this->ChangedLayer[tile] = 2; + campIndex++; + if (campIndex >= this->constructionTileCount) { + break; + } + } + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::setMiscDisplayLayer, this)(buildingID); + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::updatePathLinkagesForBuilding, this)(buildingID); + MACRO_CALL_MEMBER(OpenSHC::Map::Navigation::PathFindingState_Func::updateWalkAndPathLayer, + DAT_PathFindingState::ptr)(8, x, y); + + if ((short)buildingType == OpenSHC::Map::Buildings::BT_MERCENARYPOST) { + MACRO_CALL_MEMBER(OpenSHC::Map::Buildings::BuildingsState_Func::setupMercenaryPostCampgroundPositions, + DAT_BuildingsState::ptr)(playerID); + } else { + MACRO_CALL_MEMBER(OpenSHC::Map::Buildings::BuildingsState_Func::setupBarracksCampgroundPositions, + DAT_BuildingsState::ptr)(playerID); + } + + // not enough idle peasants for the workers: tell the local player + if (playerID == DAT_GameSynchronyState::instance.currentPlayerSlotID + && DAT_BuildingsState::instance.buildings[buildingID].buildingTypeBasedEmployeeCount + > DAT_GameState::instance.playerDataArray[playerID].availablePeasantsAtFire + && (DAT_GameState::instance.playerDataArray[playerID].populationCap + <= DAT_GameState::instance.playerDataArray[playerID].currentPopulation + || DAT_GameState::instance.playerDataArray[playerID].popularity < 5000)) { + MACRO_CALL_MEMBER(OpenSHC::UI::BottomLeftTextDisplayState_Func::setBottomLeftTextDisplayText, + DAT_BottomLeftTextDisplayState::ptr)(1, OpenSHC::DE::SHCDE::TEXT_PANEL_FEEDBACK, 1, 0, 100, 6000); + } + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/placeBuilding.cpp b/src/OpenSHC/Map/TileMapState/placeBuilding.cpp new file mode 100644 index 0000000000..c8f70c578e --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/placeBuilding.cpp @@ -0,0 +1,368 @@ +#include "../TileMapState.func.hpp" + +#include "OpenSHC/Game.func.hpp" +#include "OpenSHC/Map/Buildings/BuildingsState.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/Map/WallAndPitchState.func.hpp" +#include "OpenSHC/UI/Helpers.func.hpp" +#include "OpenSHC/UI/MinimapViewState.func.hpp" +#include "OpenSHC/Commands/MappersEnum.hpp" +#include "OpenSHC/Game/GameMode.hpp" +#include "OpenSHC/Game/GameMode2.hpp" +#include "OpenSHC/Map/Buildings/BuildingType.hpp" +#include "OpenSHC/WindowsHelper/Enums/BOOLEnum.hpp" + +#include "OpenSHC/Globals/DAT_BuildingsState.hpp" +#include "OpenSHC/Globals/DAT_GameCore.hpp" +#include "OpenSHC/Globals/DAT_GameState.hpp" +#include "OpenSHC/Globals/DAT_GameSynchronyState.hpp" +#include "OpenSHC/Globals/DAT_LandscapeState.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" + +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_ROCKY; + 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::Game::GameMode; + using OpenSHC::Game::GameMode2; + + // FUNCTION: STRONGHOLDCRUSADER 0x005162D0 + void TileMapState::placeBuilding( + int playerID, int x, int y, MappersEnum cbt, int buildingSize, int buildingOrientation) + { + MappersEnum command = (MappersEnum)(short)cbt; + if (this->skipPlacementCheck == 0) { + if (buildingOrientation == 15) { + this->DAT_TempBuildingRotation = 0; + } else { + this->DAT_TempBuildingRotation = buildingOrientation / 2; + } + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::checkBuildingCanBePlacedHere, this)( + playerID, x, y, cbt, buildingSize); + if (this->buildingPlacementFail != FALSE) { + return; + } + } + this->skipPlacementCheck = 0; + int pitchDitchID = 0; + + BuildingType buildingType = (BuildingType)(ushort)MACRO_CALL_MEMBER( + OpenSHC::Map::Buildings::BuildingsState_Func::convertCommandBuildingTypeToBuildingType, + DAT_BuildingsState::ptr)(command); + short shortType = (short)buildingType; + if (DAT_GameCore::instance.gameMode_2 == OpenSHC::Game::GM_CRUSADER_TUTORIAL) { + if (MACRO_CALL(OpenSHC::Game_Func::Tutorial_IsActionAllowed)(2, (short)shortType) == FALSE) { + MACRO_CALL(OpenSHC::UI::Helpers_Func::SetTutorialHintActiveWithTimestamp)(); + return; + } + MACRO_CALL(OpenSHC::UI::Helpers_Func::SetTutorialBuildingActionState)( + 7, (BuildingType)(int)(short)shortType); + } + + // clear whatever the footprint was before the building goes down + int index = 0; + do { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::getBuildingSizeIndexMappingData, this)( + index, buildingSize); + int tile = DAT_ViewportRenderState::instance.translationMatrix[this->buildingY + y].addXgetTile + + this->buildingX + x; + if (DAT_GameCore::instance.gameMode_2 == OpenSHC::Game::GM_SIEGE_THAT + && (this->LogicLayer[tile] & L_WALL_OR_GATEHOUSE) != 0) { + DAT_GameState::instance.playerDataArray[playerID].startResources[4] += 1; + } + if ((this->LogicLayer[tile] & L_ROCKY) != 0) { + int rock = this->OrganismLayer[tile]; + if (rock >= 2000) { + MACRO_CALL_MEMBER(OpenSHC::Map::LandscapeState_Func::removeRock, DAT_LandscapeState::ptr)( + rock - 2000); + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::getBuildingSizeIndexMappingData, this)( + index, buildingSize); + MACRO_CALL_MEMBER(OpenSHC::Map::Navigation::PathFindingState_Func::updateWalkAndPathLayer, + DAT_PathFindingState::ptr)(8, this->buildingX + x, this->buildingY + y); + } + } + this->LogicLayer[tile] &= ~(L_CRENEL_VARIATIONUnk | L_UNKNOWN_WALL_RELATED | L_STAIRS | L_CRENEL + | L_WALL_OR_GATEHOUSE | L_PLAIN2_AND_PITCH); + this->HeightLayer[tile] = DAT_TileMapState::instance.DefaultHeightLayer[tile]; + this->DamageLayer[tile] = 0; + if ((this->LogicLayer[tile] & L_TREE) != 0) { + int tree = this->OrganismLayer[tile]; + if (tree < 2000) { + // every tree goes; the original has two identical arms by tree type + switch (DAT_LandscapeState::instance.trees[tree].treeType) { + case 5: + case 6: + case 7: + case 8: + case 9: + case 10: + case 11: + case 12: + case 13: + case 14: + case 16: + case 17: + case 18: + case 19: + MACRO_CALL_MEMBER(OpenSHC::Map::LandscapeState_Func::removeTree, DAT_LandscapeState::ptr)(tree); + MACRO_CALL_MEMBER(OpenSHC::Map::Navigation::PathFindingState_Func::updateWalkAndPathLayer, + DAT_PathFindingState::ptr)(3, x, y); + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::getBuildingSizeIndexMappingData, this)( + index, buildingSize); + break; + default: + MACRO_CALL_MEMBER(OpenSHC::Map::LandscapeState_Func::removeTree, DAT_LandscapeState::ptr)(tree); + MACRO_CALL_MEMBER(OpenSHC::Map::Navigation::PathFindingState_Func::updateWalkAndPathLayer, + DAT_PathFindingState::ptr)(3, x, y); + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::getBuildingSizeIndexMappingData, this)( + index, buildingSize); + break; + } + } + } + int unit = this->UnitLayer[tile]; + if (unit != 0) { + // a unit standing where a walkable building goes is left alone + switch (shortType) { + case OpenSHC::Map::Buildings::BT_GATEHOUSELARGE: + case OpenSHC::Map::Buildings::BT_GATEHOUSESMALL: + case OpenSHC::Map::Buildings::BT_DRAWBRIDGE: + case OpenSHC::Map::Buildings::BT_CAMPFIRE: + case OpenSHC::Map::Buildings::BT_PARADEGROUND: + case OpenSHC::Map::Buildings::BT_CAMPGROUND: + case OpenSHC::Map::Buildings::BT_PARADEGROUND2: + case OpenSHC::Map::Buildings::BT_PARADEGROUND3: + case OpenSHC::Map::Buildings::BT_PARADEGROUND4: + case OpenSHC::Map::Buildings::BT_PARADEGROUND5: + case OpenSHC::Map::Buildings::BT_KILLINGPIT: + case OpenSHC::Map::Buildings::BT_PITCHDITCH: + case OpenSHC::Map::Buildings::BT_SIEGETOWER_PLACED: + 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: + break; + default: + MACRO_CALL_MEMBER(OpenSHC::Map::Units::UnitsState_Func::deleteUnit, DAT_UnitsState::ptr)(unit); + } + } + if ((this->LogicLayer[tile] & (L_MOAT | L_MOAT_DUG_OR_PLANNED)) != 0) { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::clearMoatDataAtTile, this)( + this->buildingX + x, this->buildingY + y); + this->LogicLayer[tile] &= ~(L_MOAT | L_MOAT_DUG_OR_PLANNED); + } + this->MiscDisplayLayer[tile] &= 0xfc3f; + index++; + DAT_PathFindingState::instance.toggleUpdateSeparateAreaTileMap = 1; + DAT_TileMapState::instance.field_0x554a30 = 1; + } while (index < this->constructionTileCount); + + // the building sits at the middle of the heights it covers + int highest = 0; + int lowest = 1000; + index = 0; + do { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::getBuildingSizeIndexMappingData, this)( + index, buildingSize); + int tileHeight + = this->HeightLayer[DAT_ViewportRenderState::instance.translationMatrix[this->buildingY + y].addXgetTile + + this->buildingX + x]; + if (tileHeight > highest) { + highest = tileHeight; + } + if (tileHeight < lowest) { + lowest = tileHeight; + } + index++; + } while (index < this->constructionTileCount); + int averageHeight = (highest - lowest) / 2 + lowest; + + if (shortType == OpenSHC::Map::Buildings::BT_TUNNEL + && playerID == DAT_GameSynchronyState::instance.currentPlayerSlotID) { + MACRO_CALL_MEMBER(OpenSHC::Map::Units::TribesState_Func::playTunnelerCommandSpeech, DAT_TribesState::ptr)(); + } + + switch ((int)shortType) { + case OpenSHC::Map::Buildings::BT_UNKNOWN1: + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::stampBuildingOntoTileMap, this)(playerID, x, y, + buildingType, command + ~OpenSHC::Commands::M_MAPPER_MP_KEEP8, buildingSize, buildingOrientation, + averageHeight); + break; + case OpenSHC::Map::Buildings::BT_GARDEN: + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::placePositiveFearfactor, this)(playerID, x, y, + buildingType, command - OpenSHC::Commands::M_MAPPER_GARDEN1, buildingSize, buildingOrientation, + averageHeight); + break; + case OpenSHC::Map::Buildings::BT_CESSPIT: + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::placePositiveFearfactor, this)(playerID, x, y, + buildingType, command - OpenSHC::Commands::M_MAPPER_CESS_PIT1, buildingSize, buildingOrientation, + averageHeight); + break; + case OpenSHC::Map::Buildings::BT_STATUE: + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::placePositiveFearfactor, this)(playerID, x, y, + buildingType, command - OpenSHC::Commands::M_MAPPER_STATUE1, buildingSize, buildingOrientation, + averageHeight); + break; + case OpenSHC::Map::Buildings::BT_SHRINE: + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::placePositiveFearfactor, this)(playerID, x, y, + buildingType, command - OpenSHC::Commands::M_MAPPER_SHRINE1, buildingSize, buildingOrientation, + averageHeight); + break; + case OpenSHC::Map::Buildings::BT_POND: + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::placePositiveFearfactor, this)(playerID, x, y, + buildingType, command - OpenSHC::Commands::M_MAPPER_POND1, buildingSize, buildingOrientation, + averageHeight); + break; + case OpenSHC::Map::Buildings::BT_STOCKPILE: + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::placeStockpile, this)( + playerID, x, y, buildingType, buildingSize, buildingOrientation, averageHeight); + break; + case OpenSHC::Map::Buildings::BT_QUARRY: + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::placeQuarry, this)( + playerID, x, y, buildingType, buildingSize, buildingOrientation, averageHeight); + break; + case OpenSHC::Map::Buildings::BT_WHEATFARM: + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::placeWheatfarm, this)( + playerID, x, y, buildingType, buildingSize, buildingOrientation, averageHeight); + break; + case OpenSHC::Map::Buildings::BT_HOPFARM: + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::placeHopfarm, this)( + playerID, x, y, buildingType, buildingSize, buildingOrientation, averageHeight); + break; + case OpenSHC::Map::Buildings::BT_DAIRYFARM: + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::placeDairyfarm, this)( + playerID, x, y, buildingType, buildingSize, buildingOrientation, averageHeight); + break; + case OpenSHC::Map::Buildings::BT_APPLEFARM: + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::placeApplefarm, this)( + playerID, x, y, buildingType, buildingSize, buildingOrientation, averageHeight); + break; + case OpenSHC::Map::Buildings::BT_MERCENARYPOST: + case OpenSHC::Map::Buildings::BT_BARRACKS: + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::placeBarracks, this)( + playerID, x, y, buildingType, buildingSize, buildingOrientation, averageHeight); + break; + case OpenSHC::Map::Buildings::BT_ENGINEERSGUILD: + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::placeEngineersguild, this)( + playerID, x, y, buildingType, buildingSize, buildingOrientation, averageHeight); + break; + case OpenSHC::Map::Buildings::BT_TUNNELERSGUILD: + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::placeTunnelersguild, this)( + playerID, x, y, buildingType, buildingSize, buildingOrientation, averageHeight); + break; + case OpenSHC::Map::Buildings::BT_OILSMELTER: + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::placeOilsmelter, this)( + playerID, x, y, buildingType, buildingSize, buildingOrientation, averageHeight); + break; + case OpenSHC::Map::Buildings::BT_MANORHOUSE: + case OpenSHC::Map::Buildings::BT_STONEKEEP: + case OpenSHC::Map::Buildings::BT_STRONGHOLD: + case OpenSHC::Map::Buildings::BT_KEEPFOUR: + case OpenSHC::Map::Buildings::BT_KEEPFIVE: + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::placeKeep, this)( + playerID, x, y, buildingType, buildingSize, buildingOrientation, averageHeight); + break; + 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: + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::placeTower, this)( + playerID, x, y, buildingType, buildingSize, buildingOrientation, averageHeight); + break; + case OpenSHC::Map::Buildings::BT_GATEHOUSELARGE: + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::placeGatehouseLarge, this)( + playerID, x, y, buildingType, buildingSize, buildingOrientation, averageHeight); + break; + case OpenSHC::Map::Buildings::BT_GATEHOUSESMALL: + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::placeGatehouseSmall, this)( + playerID, x, y, buildingType, buildingSize, buildingOrientation, averageHeight); + break; + case OpenSHC::Map::Buildings::BT_DRAWBRIDGE: + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::placeDrawbridge, this)( + playerID, x, y, buildingType, buildingSize, buildingOrientation, averageHeight); + break; + case OpenSHC::Map::Buildings::BT_FIREBALLISTA: + case OpenSHC::Map::Buildings::BT_CATAPULT: + case OpenSHC::Map::Buildings::BT_TREBUCHET: + case OpenSHC::Map::Buildings::BT_BATTERINGRAM: + case OpenSHC::Map::Buildings::BT_SIEGETOWER: + case OpenSHC::Map::Buildings::BT_SHIELD: + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::placeSiegeTent, this)( + playerID, x, y, buildingType, buildingSize, buildingOrientation, averageHeight); + break; + case OpenSHC::Map::Buildings::BT_KILLINGPIT: + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::placeKillingPit, this)( + playerID, x, y, buildingType, buildingSize, buildingOrientation, averageHeight); + break; + case OpenSHC::Map::Buildings::BT_PITCHDITCH: + pitchDitchID = MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::placePitchDitch, this)(playerID, x, y); + break; + case OpenSHC::Map::Buildings::BT_SIEGETOWER_PLACED: + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::placeSiegetowerPlaced, this)( + playerID, x, y, buildingType, buildingSize, buildingOrientation, averageHeight); + break; + default: + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::placeWorkshopOrHovel, this)( + playerID, x, y, buildingType, buildingSize, buildingOrientation, averageHeight); + break; + case OpenSHC::Map::Buildings::BT_WOODGATE1: + break; + } + + if (playerID == DAT_GameSynchronyState::instance.currentPlayerSlotID) { + if (shortType != OpenSHC::Map::Buildings::BT_PITCHDITCH) { + MACRO_CALL_MEMBER(OpenSHC::Map::WallAndPitchState_Func::startBuildingDestructionConfirmation, + DAT_WallAndPitchState::ptr)(this->placedBuildingID); + } else if (pitchDitchID != 0) { + MACRO_CALL_MEMBER(OpenSHC::Map::WallAndPitchState_Func::placePitchDitch, DAT_WallAndPitchState::ptr)( + pitchDitchID); + } + } + MACRO_CALL_MEMBER(OpenSHC::Map::Buildings::BuildingsState_Func::processPlacementResourceLossForBuildingType, + DAT_BuildingsState::ptr)(playerID, (BuildingType)(int)(short)shortType, 0); + if (MACRO_CALL_MEMBER( + OpenSHC::Map::Buildings::BuildingsState_Func::hasLessWoodThanTheCostOfAWoodcuttersHutAndNoWoodcutters, + DAT_BuildingsState::ptr)(playerID, (int)(short)shortType) + != 0) { + DAT_BuildingsState::instance.buildings[this->placedBuildingID].suppressDestroyRefund = 1; + } + MACRO_CALL_MEMBER(OpenSHC::UI::MinimapViewState_Func::triggerMinimapRedraw, DAT_MinimapViewState::ptr)(1); + int radius; + if (buildingSize <= 5) { + radius = 9; + } else { + radius = buildingSize + 5; + } + MACRO_CALL_MEMBER(OpenSHC::Map::Navigation::PathFindingState_Func::updateWalkAndPathLayer, + DAT_PathFindingState::ptr)(radius, x, y); + DAT_PathFindingState::instance.toggleUpdateSeparateAreaTileMap = 1; + DAT_TileMapState::instance.field_0x554a30 = 1; + if (DAT_GameSynchronyState::instance.currentGameMode == OpenSHC::Game::GM_SOLITARY) { + this->forceUpdateMacroLayerFlag = 1; + this->field_0x55487c = 200; + } + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/placeDairyfarm.cpp b/src/OpenSHC/Map/TileMapState/placeDairyfarm.cpp new file mode 100644 index 0000000000..0bc2da4f95 --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/placeDairyfarm.cpp @@ -0,0 +1,109 @@ +#include "../TileMapState.func.hpp" + +#include "OpenSHC/Map/Buildings/BuildingsState.func.hpp" +#include "OpenSHC/Map/Navigation/PathFindingState.func.hpp" +#include "OpenSHC/UI/BottomLeftTextDisplayState.func.hpp" +#include "OpenSHC/DE/SHCDE/eTextSections.hpp" +#include "OpenSHC/Map/Buildings/BuildingType.hpp" +#include "OpenSHC/Map/LogicHelpers/Logic1.hpp" +#include "OpenSHC/Map/LogicHelpers/Logic2.hpp" + +#include "OpenSHC/Globals/DAT_BottomLeftTextDisplayState.hpp" +#include "OpenSHC/Globals/DAT_BuildingsState.hpp" +#include "OpenSHC/Globals/DAT_GameState.hpp" +#include "OpenSHC/Globals/DAT_GameSynchronyState.hpp" +#include "OpenSHC/Globals/DAT_PathFindingState.hpp" +#include "OpenSHC/Globals/DAT_TerrainDefinedData.hpp" +#include "OpenSHC/Globals/DAT_ViewportRenderState.hpp" + +namespace OpenSHC { +namespace Map { + + using OpenSHC::Map::LogicHelpers::L2_THICK_SCRUB; + using OpenSHC::Map::LogicHelpers::L_BUILDING; + using OpenSHC::Map::LogicHelpers::L_FARM_FIELD_DAIRY; + using OpenSHC::Map::LogicHelpers::L_NONE; + + // FUNCTION: STRONGHOLDCRUSADER 0x005154D0 + void TileMapState::placeDairyfarm(int playerID, uint x, uint y, undefined4 buildingType, undefined4 buildingSize, + int variation, undefined4 averageHeight) + { + int index = 0; + int buildingID = MACRO_CALL_MEMBER( + OpenSHC::Map::Buildings::BuildingsState_Func::setupBuildingData, DAT_BuildingsState::ptr)( + playerID, x, y, averageHeight, (BuildingType)(short)buildingType, 3, playerID, variation); + this->placedBuildingID = buildingID; + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::markBuildingFootprintFlag, this)(x, y, 10); + + // 27 tiles from one of four layouts; tiles 4.. are the field and carry an initial crop stage + for (int i = 0; i < 27; i++) { + int& tileRef = DAT_BuildingsState::instance.buildings[buildingID].tileRefs[i]; + tileRef = DAT_ViewportRenderState::instance + .translationMatrix[DAT_TerrainDefinedData::instance + .field_0xf5c[DAT_GameState::instance.playerDataArray[playerID] + .dairyFarmVariationMod4][i] + .offset.y + + y] + .addXgetTile + + DAT_TerrainDefinedData::instance + .field_0xf5c[DAT_GameState::instance.playerDataArray[playerID].dairyFarmVariationMod4][i] + .offset.x + + x; + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::resetTileAndClearMoat, this)(tileRef); + if (i >= 4) { + this->DamageLayer[tileRef] + = (byte)DAT_TerrainDefinedData::instance + .field_0xf5c[DAT_GameState::instance.playerDataArray[playerID].dairyFarmVariationMod4][i] + .property; + } + } + DAT_GameState::instance.playerDataArray[playerID].dairyFarmVariationMod4++; + if (DAT_GameState::instance.playerDataArray[playerID].dairyFarmVariationMod4 > 3) { + DAT_GameState::instance.playerDataArray[playerID].dairyFarmVariationMod4 = 0; + } + + // the farm house + while (true) { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::getBuildingSizeIndexMappingData, this)(index, 3); + int tile = DAT_ViewportRenderState::instance.translationMatrix[this->buildingY + y].addXgetTile + + this->buildingX + x; + this->HeightLayer[tile] = (byte)averageHeight; + this->LogicLayer[tile] |= L_BUILDING; + this->BuildingLayer[tile] = (short)buildingID; + this->BuildingWasLayer[tile] = (uchar)buildingType; + this->ChangedLayer[tile] = 2; + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::setTerrain, this)( + playerID, tile, this->buildingY + y, 2, L_NONE, L2_THICK_SCRUB); + index++; + if (index >= this->constructionTileCount) { + break; + } + } + + for (int i = 4; i < 27; i++) { + int tile = DAT_BuildingsState::instance.buildings[buildingID].tileRefs[i]; + this->LogicLayer[tile] |= L_FARM_FIELD_DAIRY; + this->BuildingLayer[tile] = (short)buildingID; + MACRO_CALL_MEMBER(OpenSHC::Map::Navigation::PathFindingState_Func::updatePathLinkagesInAllEightDirections, + DAT_PathFindingState::ptr)( + DAT_ViewportRenderState::instance.tileTranslationMatrix_YComponent[tile], tile); + this->BuildingWasLayer[tile] = (uchar)buildingType; + this->ChangedLayer[tile] = 2; + } + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::setMiscDisplayLayer, this)(buildingID); + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::updatePathLinkagesForBuilding, this)(buildingID); + + // not enough idle peasants for the workers: tell the local player + if (playerID == DAT_GameSynchronyState::instance.currentPlayerSlotID + && DAT_BuildingsState::instance.buildings[buildingID].buildingTypeBasedEmployeeCount + > DAT_GameState::instance.playerDataArray[playerID].availablePeasantsAtFire + && (DAT_GameState::instance.playerDataArray[playerID].populationCap + <= DAT_GameState::instance.playerDataArray[playerID].currentPopulation + || DAT_GameState::instance.playerDataArray[playerID].popularity < 5000)) { + MACRO_CALL_MEMBER(OpenSHC::UI::BottomLeftTextDisplayState_Func::setBottomLeftTextDisplayText, + DAT_BottomLeftTextDisplayState::ptr)(1, OpenSHC::DE::SHCDE::TEXT_PANEL_FEEDBACK, 1, 0, 100, 6000); + } + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/placeDefensiveStructureTile.cpp b/src/OpenSHC/Map/TileMapState/placeDefensiveStructureTile.cpp index d234ad9f1d..b64e525794 100644 --- a/src/OpenSHC/Map/TileMapState/placeDefensiveStructureTile.cpp +++ b/src/OpenSHC/Map/TileMapState/placeDefensiveStructureTile.cpp @@ -44,18 +44,20 @@ namespace Map { 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 ((int)(x & 1) == 1 && (y & 1) == 0) { + byte& height = this->HeightLayer[tile]; + height += 98; + if ((x & 1) == 1 && (y & 1) == 0) { this->LogicLayer[tile] |= L_CRENEL_VARIATIONUnk; - } else if ((int)(x & 1) == 0 && (y & 1) == 1) { + } 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 ((int)(x & 1) == 1 && (y & 1) == 0) { + byte& height = this->HeightLayer[tile]; + height += 68; + if ((x & 1) == 1 && (y & 1) == 0) { this->LogicLayer[tile] |= L_CRENEL_VARIATIONUnk; - } else if ((int)(x & 1) == 0 && (y & 1) == 1) { + } else if ((x & 1) == 0 && (y & 1) == 1) { this->LogicLayer[tile] |= L_CRENEL_VARIATIONUnk; } } else if (command == OpenSHC::Commands::M_MAPPER_STAIR1) { diff --git a/src/OpenSHC/Map/TileMapState/placeDrawbridge.cpp b/src/OpenSHC/Map/TileMapState/placeDrawbridge.cpp new file mode 100644 index 0000000000..66ab296ff6 --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/placeDrawbridge.cpp @@ -0,0 +1,57 @@ +#include "../TileMapState.func.hpp" + +#include "OpenSHC/Map/Buildings/BuildingsState.func.hpp" +#include "OpenSHC/Map/Buildings/BuildingType.hpp" +#include "OpenSHC/Map/LogicHelpers/Logic1.hpp" + +#include "OpenSHC/Globals/DAT_BuildingsState.hpp" +#include "OpenSHC/Globals/DAT_TerrainDefinedData.hpp" +#include "OpenSHC/Globals/DAT_ViewportRenderState.hpp" + +namespace OpenSHC { +namespace Map { + + using OpenSHC::Map::LogicHelpers::L_BUILDING; + using OpenSHC::Map::LogicHelpers::L_MOAT; + + // FUNCTION: STRONGHOLDCRUSADER 0x00514DE0 + void TileMapState::placeDrawbridge( + int playerID, uint x, uint y, undefined4 buildingType, uint width, int orientation, undefined4 averageHeight) + { + int index = 0; + int buildingID = MACRO_CALL_MEMBER( + OpenSHC::Map::Buildings::BuildingsState_Func::setupBuildingData, DAT_BuildingsState::ptr)( + playerID, x, y, averageHeight, (BuildingType)(short)buildingType, width, playerID, orientation); + int rot = orientation / 2; + this->placedBuildingID = buildingID; + while (true) { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::getBuildingSizeIndexMappingData, this)(index, width); + int tile = DAT_ViewportRenderState::instance.translationMatrix[this->buildingY + y].addXgetTile + + this->buildingX + x; + DAT_BuildingsState::instance.buildings[buildingID].tileRefs[index] = tile; + this->BuildingLayer[tile] = (short)buildingID; + this->BuildingWasLayer[tile] = (uchar)buildingType; + this->ChangedLayer[tile] = 2; + if (DAT_TerrainDefinedData::instance.DrawbridgeTileMoatProperty[index] != 0) { + this->LogicLayer[tile] |= L_BUILDING; + } + if (DAT_TerrainDefinedData::instance.DrawbridgeOrientationMapping[rot][index] != 0) { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::createMoatData, this)( + playerID, this->buildingX + x, this->buildingY + y, 1); + this->LogicLayer[tile] &= ~L_MOAT; + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::setMoatVisualStateAtTile, this)(tile, 0); + this->HeightLayer[tile] = 0; + } else { + this->LogicLayer[tile] &= ~L_MOAT; + } + index++; + if (index >= this->constructionTileCount) { + break; + } + } + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::setMiscDisplayLayer, this)(buildingID); + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::updatePathLinkagesForBuilding, this)(buildingID); + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/placeEngineersguild.cpp b/src/OpenSHC/Map/TileMapState/placeEngineersguild.cpp new file mode 100644 index 0000000000..2af43f91fa --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/placeEngineersguild.cpp @@ -0,0 +1,88 @@ +#include "../TileMapState.func.hpp" + +#include "OpenSHC/Map/Buildings/BuildingsState.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_GameCore.hpp" +#include "OpenSHC/Globals/DAT_PathFindingState.hpp" +#include "OpenSHC/Globals/DAT_TerrainDefinedData.hpp" +#include "OpenSHC/Globals/DAT_ViewportRenderState.hpp" + +namespace OpenSHC { +namespace Map { + + using OpenSHC::Map::LogicHelpers::L_BUILDING; + + // FUNCTION: STRONGHOLDCRUSADER 0x00507BD0 + void TileMapState::placeEngineersguild(int playerID, uint x, uint y, undefined4 buildingType, uint buildingSize, + int orientation, undefined4 averageHeight) + { + int const uniqueID = DAT_GameCore::instance.uniqueGameObjectTracker; + int index = 0; + int partIndex; + if (orientation == 15) { + partIndex = 0; + } else { + partIndex = orientation / 2; + } + ushort placedID = MACRO_CALL_MEMBER( + OpenSHC::Map::Buildings::BuildingsState_Func::setupBuildingData, DAT_BuildingsState::ptr)( + playerID, x, y, averageHeight, (BuildingType)(short)buildingType, buildingSize, playerID, 15); + int buildingID = (short)placedID; + this->placedBuildingID = buildingID; + DAT_BuildingsState::instance.buildings[buildingID].uidWhenPlaced = uniqueID; + while (true) { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::getBuildingSizeIndexMappingData, this)( + index, buildingSize); + int const tile = DAT_ViewportRenderState::instance.translationMatrix[this->buildingY + y].addXgetTile + + this->buildingX + x; + DAT_BuildingsState::instance.buildings[buildingID].tileRefs[index] = tile; + this->HeightLayer[tile] = (byte)averageHeight; + this->LogicLayer[tile] |= L_BUILDING; + this->BuildingLayer[tile] = placedID; + this->BuildingWasLayer[tile] = (uchar)buildingType; + this->ChangedLayer[tile] = 2; + index++; + if (index >= this->constructionTileCount) { + break; + } + } + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::setMiscDisplayLayer, this)(buildingID); + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::updatePathLinkagesForBuilding, this)(buildingID); + + x += DAT_TerrainDefinedData::instance.BuildingPartsOffsets[partIndex][1].x; + y += DAT_TerrainDefinedData::instance.BuildingPartsOffsets[partIndex][1].y; + placedID = MACRO_CALL_MEMBER( + OpenSHC::Map::Buildings::BuildingsState_Func::setupBuildingData, DAT_BuildingsState::ptr)( + playerID, x, y, averageHeight, OpenSHC::Map::Buildings::BT_PARADEGROUND, 5, playerID, 15); + buildingID = (short)placedID; + DAT_BuildingsState::instance.buildings[buildingID].uidWhenPlaced = uniqueID; + DAT_BuildingsState::instance.buildings[buildingID].quarryStockpileID = (short)this->placedBuildingID; + int campIndex = 0; + while (true) { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::getBuildingSizeIndexMappingData, this)( + campIndex, DAT_BuildingsState::instance.buildings[buildingID].widthOrHeight); + int const tile = DAT_ViewportRenderState::instance.translationMatrix[this->buildingY + y].addXgetTile + + this->buildingX + x; + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::resetTileAndClearMoat, this)(tile); + this->HeightLayer[tile] = (byte)averageHeight; + this->BuildingLayer[tile] = placedID; + this->BuildingWasLayer[tile] = (uchar)buildingType; + this->ChangedLayer[tile] = 2; + campIndex++; + if (campIndex >= this->constructionTileCount) { + break; + } + } + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::setMiscDisplayLayer, this)(buildingID); + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::updatePathLinkagesForBuilding, this)(buildingID); + MACRO_CALL_MEMBER(OpenSHC::Map::Navigation::PathFindingState_Func::updateWalkAndPathLayer, + DAT_PathFindingState::ptr)(8, x, y); + MACRO_CALL_MEMBER(OpenSHC::Map::Buildings::BuildingsState_Func::setupEngineersGuildParagroundPositions, + DAT_BuildingsState::ptr)(playerID); + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/placeGatehouseLarge.cpp b/src/OpenSHC/Map/TileMapState/placeGatehouseLarge.cpp new file mode 100644 index 0000000000..0ac3af1f55 --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/placeGatehouseLarge.cpp @@ -0,0 +1,60 @@ +#include "../TileMapState.func.hpp" + +#include "OpenSHC/Map/Buildings/BuildingsState.func.hpp" +#include "OpenSHC/Map/Navigation/PathFindingState.func.hpp" + +#include "OpenSHC/Globals/DAT_BuildingsState.hpp" +#include "OpenSHC/Globals/DAT_GameCore.hpp" +#include "OpenSHC/Globals/DAT_PathFindingState.hpp" +#include "OpenSHC/Globals/DAT_ViewportRenderState.hpp" + +namespace OpenSHC { +namespace Map { + + using OpenSHC::Map::LogicHelpers::L_WALL_OR_GATEHOUSE; + + // FUNCTION: STRONGHOLDCRUSADER 0x00507560 + void TileMapState::placeGatehouseLarge(int playerID, uint x, uint y, undefined4 buildingType, uint buildingSize, + int variation, undefined4 averageHeight) + { + int uniqueID = DAT_GameCore::instance.uniqueGameObjectTracker; + int index = 0; + int buildingID = MACRO_CALL_MEMBER( + OpenSHC::Map::Buildings::BuildingsState_Func::setupBuildingData, DAT_BuildingsState::ptr)( + playerID, x, y, averageHeight, (BuildingType)(short)buildingType, buildingSize, playerID, variation); + this->placedBuildingID = buildingID; + DAT_BuildingsState::instance.buildings[buildingID].uidWhenPlaced = uniqueID; + while (true) { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::getBuildingSizeIndexMappingData, this)( + index, buildingSize); + int tile = DAT_ViewportRenderState::instance.translationMatrix[this->buildingY + y].addXgetTile + x + + this->buildingX; + if (index < countof(DAT_BuildingsState::instance.buildings[buildingID].tileRefs)) { + DAT_BuildingsState::instance.buildings[buildingID].tileRefs[index] = tile; + } + this->HeightLayer[tile] = (char)averageHeight + 90; + this->LogicLayer[tile] |= L_WALL_OR_GATEHOUSE; + this->WallOwnerLayer[tile] = this->WallOwnerLayer[tile] & 0xf8 | (char)playerID - 1U; + this->MiscDisplayLayer[tile] |= 0x20; + this->BuildingLayer[tile] = (short)buildingID; + index++; + this->BuildingWasLayer[tile] = (uchar)buildingType; + this->ChangedLayer[tile] = 2; + if (index >= this->constructionTileCount) { + break; + } + } + + if (variation == 80) { + MACRO_CALL_MEMBER(OpenSHC::Map::Navigation::PathFindingState_Func::createClimbData, + DAT_PathFindingState::ptr)(3, buildingID, 0, 2, 0); + } else { + MACRO_CALL_MEMBER(OpenSHC::Map::Navigation::PathFindingState_Func::createClimbData, + DAT_PathFindingState::ptr)(3, buildingID, 0, 0, 0); + } + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::setMiscDisplayLayer, this)(buildingID); + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::updatePathLinkagesForBuilding, this)(buildingID); + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/placeGatehouseSmall.cpp b/src/OpenSHC/Map/TileMapState/placeGatehouseSmall.cpp new file mode 100644 index 0000000000..516f8a959d --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/placeGatehouseSmall.cpp @@ -0,0 +1,60 @@ +#include "../TileMapState.func.hpp" + +#include "OpenSHC/Map/Buildings/BuildingsState.func.hpp" +#include "OpenSHC/Map/Navigation/PathFindingState.func.hpp" + +#include "OpenSHC/Globals/DAT_BuildingsState.hpp" +#include "OpenSHC/Globals/DAT_GameCore.hpp" +#include "OpenSHC/Globals/DAT_PathFindingState.hpp" +#include "OpenSHC/Globals/DAT_ViewportRenderState.hpp" + +namespace OpenSHC { +namespace Map { + + using OpenSHC::Map::LogicHelpers::L_WALL_OR_GATEHOUSE; + + // FUNCTION: STRONGHOLDCRUSADER 0x00507420 + void TileMapState::placeGatehouseSmall(int playerID, uint x, uint y, undefined4 buildingType, uint buildingSize, + int variation, undefined4 averageHeight) + { + int uniqueID = DAT_GameCore::instance.uniqueGameObjectTracker; + int index = 0; + int buildingID = MACRO_CALL_MEMBER( + OpenSHC::Map::Buildings::BuildingsState_Func::setupBuildingData, DAT_BuildingsState::ptr)( + playerID, x, y, averageHeight, (BuildingType)(short)buildingType, buildingSize, playerID, variation); + this->placedBuildingID = buildingID; + DAT_BuildingsState::instance.buildings[buildingID].uidWhenPlaced = uniqueID; + while (true) { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::getBuildingSizeIndexMappingData, this)( + index, buildingSize); + int tile = DAT_ViewportRenderState::instance.translationMatrix[this->buildingY + y].addXgetTile + x + + this->buildingX; + if (index < countof(DAT_BuildingsState::instance.buildings[buildingID].tileRefs)) { + DAT_BuildingsState::instance.buildings[buildingID].tileRefs[index] = tile; + } + this->HeightLayer[tile] = (char)averageHeight + 90; + this->LogicLayer[tile] |= L_WALL_OR_GATEHOUSE; + this->WallOwnerLayer[tile] = this->WallOwnerLayer[tile] & 0xf8 | (char)playerID - 1U; + this->MiscDisplayLayer[tile] |= 0x20; + this->BuildingLayer[tile] = (short)buildingID; + index++; + this->BuildingWasLayer[tile] = (uchar)buildingType; + this->ChangedLayer[tile] = 2; + if (index >= this->constructionTileCount) { + break; + } + } + + if (variation == 80) { + MACRO_CALL_MEMBER(OpenSHC::Map::Navigation::PathFindingState_Func::createClimbData, + DAT_PathFindingState::ptr)(4, buildingID, 0, 2, 0); + } else { + MACRO_CALL_MEMBER(OpenSHC::Map::Navigation::PathFindingState_Func::createClimbData, + DAT_PathFindingState::ptr)(4, buildingID, 0, 0, 0); + } + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::setMiscDisplayLayer, this)(buildingID); + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::updatePathLinkagesForBuilding, this)(buildingID); + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/placeHopfarm.cpp b/src/OpenSHC/Map/TileMapState/placeHopfarm.cpp new file mode 100644 index 0000000000..3430474fe7 --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/placeHopfarm.cpp @@ -0,0 +1,100 @@ +#include "../TileMapState.func.hpp" + +#include "OpenSHC/Map/Buildings/BuildingsState.func.hpp" +#include "OpenSHC/Map/Navigation/PathFindingState.func.hpp" +#include "OpenSHC/UI/BottomLeftTextDisplayState.func.hpp" +#include "OpenSHC/DE/SHCDE/eTextSections.hpp" +#include "OpenSHC/Map/LogicHelpers/Logic1.hpp" +#include "OpenSHC/Map/LogicHelpers/Logic2.hpp" + +#include "OpenSHC/Globals/DAT_BottomLeftTextDisplayState.hpp" +#include "OpenSHC/Globals/DAT_BuildingsState.hpp" +#include "OpenSHC/Globals/DAT_GameState.hpp" +#include "OpenSHC/Globals/DAT_GameSynchronyState.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 0x005151C0 + void TileMapState::placeHopfarm(int playerID, uint x, uint y, undefined4 buildingType, undefined4 buildingSize, + int variation, int averageHeight) + { + int index = 0; + int const buildingID = MACRO_CALL_MEMBER( + OpenSHC::Map::Buildings::BuildingsState_Func::setupBuildingData, DAT_BuildingsState::ptr)( + playerID, x, y, averageHeight, (BuildingType)(short)buildingType, 3, playerID, variation); + this->placedBuildingID = buildingID; + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::markBuildingFootprintFlag, this)(x, y, 9); + int const layout = DAT_GameState::instance.playerDataArray[playerID].unkHopFarmVariation; + for (int i = 0; i < 24; i++) { + DAT_BuildingsState::instance.buildings[buildingID].tileRefs[i] + = DAT_ViewportRenderState::instance + .translationMatrix[DAT_TerrainDefinedData::instance.HopFarmProperty1[layout][i].y + y] + .addXgetTile + + DAT_TerrainDefinedData::instance.HopFarmProperty1[layout][i].x + x; + } + DAT_BuildingsState::instance.buildings[buildingID].someFarmRelatedState = layout != 0 ? 7 : 81; + while (true) { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::getBuildingSizeIndexMappingData, this)(index, 3); + int const tile = DAT_ViewportRenderState::instance.translationMatrix[this->buildingY + y].addXgetTile + + this->buildingX + x; + this->HeightLayer[tile] = (byte)averageHeight; + this->LogicLayer[tile] |= OpenSHC::Map::LogicHelpers::L_BUILDING; + this->BuildingLayer[tile] = (short)buildingID; + this->BuildingWasLayer[tile] = (uchar)buildingType; + this->ChangedLayer[tile] = 2; + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::setTerrain, this)(playerID, tile, this->buildingY + y, 2, + OpenSHC::Map::LogicHelpers::L_NONE, OpenSHC::Map::LogicHelpers::L2_THICK_SCRUB); + index++; + if (index >= this->constructionTileCount) { + break; + } + } + for (int i = 0; i < 24; i++) { + int const tile + = DAT_ViewportRenderState::instance + .translationMatrix[DAT_TerrainDefinedData::instance + .HopFarmProperty1[DAT_GameState::instance.playerDataArray[playerID] + .unkHopFarmVariation][i] + .y + + y] + .addXgetTile + + DAT_TerrainDefinedData::instance + .HopFarmProperty1[DAT_GameState::instance.playerDataArray[playerID].unkHopFarmVariation][i] + .x + + x; + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::resetTileAndClearMoat, this)(tile); + this->LogicLayer[tile] |= OpenSHC::Map::LogicHelpers::L_FARM_FIELD_HOP; + this->BuildingLayer[tile] = (short)buildingID; + MACRO_CALL_MEMBER(OpenSHC::Map::Navigation::PathFindingState_Func::updatePathLinkagesInAllEightDirections, + DAT_PathFindingState::ptr)( + DAT_TerrainDefinedData::instance + .HopFarmProperty1[DAT_GameState::instance.playerDataArray[playerID].unkHopFarmVariation][i] + .y + + y, + tile); + this->BuildingWasLayer[tile] = (uchar)buildingType; + this->ChangedLayer[tile] = 2; + } + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::setMiscDisplayLayer, this)(buildingID); + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::updatePathLinkagesForBuilding, this)(buildingID); + DAT_GameState::instance.playerDataArray[playerID].unkHopFarmVariation += 1; + if (DAT_GameState::instance.playerDataArray[playerID].unkHopFarmVariation > 1) { + DAT_GameState::instance.playerDataArray[playerID].unkHopFarmVariation = 0; + } + if (playerID == DAT_GameSynchronyState::instance.currentPlayerSlotID + && DAT_BuildingsState::instance.buildings[buildingID].buildingTypeBasedEmployeeCount + > DAT_GameState::instance.playerDataArray[playerID].availablePeasantsAtFire + && (DAT_GameState::instance.playerDataArray[playerID].populationCap + <= DAT_GameState::instance.playerDataArray[playerID].currentPopulation + || DAT_GameState::instance.playerDataArray[playerID].popularity < 5000)) { + MACRO_CALL_MEMBER(OpenSHC::UI::BottomLeftTextDisplayState_Func::setBottomLeftTextDisplayText, + DAT_BottomLeftTextDisplayState::ptr)(1, OpenSHC::DE::SHCDE::TEXT_PANEL_FEEDBACK, 1, 0, 100, 6000); + } + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/placeKeep.cpp b/src/OpenSHC/Map/TileMapState/placeKeep.cpp new file mode 100644 index 0000000000..5ce7e54804 --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/placeKeep.cpp @@ -0,0 +1,248 @@ +#include "../TileMapState.func.hpp" + +#include "OpenSHC/Game/GameStateStructures.func.hpp" +#include "OpenSHC/Map/Buildings/BuildingsState.func.hpp" +#include "OpenSHC/Map/Navigation/PathFindingState.func.hpp" +#include "OpenSHC/Game/Player/PlayerDataBuildingCategoryEnum.hpp" +#include "OpenSHC/Map/Buildings/BuildingType.hpp" +#include "OpenSHC/Map/LogicHelpers/Logic1.hpp" +#include "OpenSHC/Map/LogicHelpers/Logic2.hpp" + +#include "OpenSHC/Globals/DAT_BuildingsState.hpp" +#include "OpenSHC/Globals/DAT_GameCore.hpp" +#include "OpenSHC/Globals/DAT_GameState.hpp" +#include "OpenSHC/Globals/DAT_PathFindingState.hpp" +#include "OpenSHC/Globals/DAT_TerrainDefinedData.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::Game::Player::PlayerDataBuildingCategoryEnum; + + // FUNCTION: STRONGHOLDCRUSADER 0x005146D0 + void TileMapState::placeKeep( + int playerID, uint x, uint y, BuildingType type, uint size, int orientation, int xyValue) + { + if (this->clearKeepFootprintOnPlace != 0) { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::demolishBuildingsInKeepsConstructionFootprint, this)( + playerID, x, y, type, size, orientation, xyValue); + this->clearKeepFootprintOnPlace = 0; + } + + int keepKind = 0; + int index = 0; + int const uid = DAT_GameCore::instance.uniqueGameObjectTracker; + int const keepOrientation = orientation; + // grass and scrub come back as scrub, anything else as bare earth + int const grassy = this->Logic2Layer[DAT_ViewportRenderState::instance.translationMatrix[y].addXgetTile + x] + & (OpenSHC::Map::LogicHelpers::L2_OASIS_GRASS | OpenSHC::Map::LogicHelpers::L2_THICK_SCRUB); + short const keepType = (short)type; + // the outbuildings below move x and y + uint const baseX = x; + uint const baseY = y; + int buildingID = MACRO_CALL_MEMBER(OpenSHC::Map::Buildings::BuildingsState_Func::setupBuildingData, + DAT_BuildingsState::ptr)(playerID, x, y, xyValue, (BuildingType)keepType, size, playerID, 15); + this->placedBuildingID = buildingID; + DAT_BuildingsState::instance.buildings[buildingID].uidWhenPlaced = uid; + DAT_BuildingsState::instance.buildings[buildingID].orientation = (short)orientation; + MACRO_CALL_MEMBER( + OpenSHC::Game::GameStateStructures_Func::computeBuildingCategoryEntryPointAndDestroyEarlierBuilding, + DAT_GameState::ptr)(buildingID, playerID, OpenSHC::Game::Player::PDBCE_KEEP); + if (orientation == 15) { + orientation = 0; + } + + int tile; + while (true) { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::getBuildingSizeIndexMappingData, this)(index, size); + tile = DAT_ViewportRenderState::instance.translationMatrix[this->buildingY + y].addXgetTile + + this->buildingX + x; + if (index < countof(DAT_BuildingsState::instance.buildings[buildingID].tileRefs)) { + DAT_BuildingsState::instance.buildings[buildingID].tileRefs[index] = tile; + } + this->HeightLayer[tile] = (byte)xyValue; + if (keepType == OpenSHC::Map::Buildings::BT_MANORHOUSE) { + this->LogicLayer[tile] |= L_BUILDING; + } else { + this->LogicLayer[tile] |= L_KEEP_NON_MANOR_HOUSE; + } + this->BuildingLayer[tile] = (short)buildingID; + this->BuildingWasLayer[tile] = (uchar)keepType; + if (grassy != 0) { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::setTerrain, this)(playerID, tile, + this->buildingY + y, 3, OpenSHC::Map::LogicHelpers::L_NONE, + OpenSHC::Map::LogicHelpers::L2_THICK_SCRUB); + } else { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::setTerrain, this)(playerID, tile, + this->buildingY + y, 6, OpenSHC::Map::LogicHelpers::L_NONE, + OpenSHC::Map::LogicHelpers::L2_EARTH_AND_STONES); + } + this->ChangedLayer[tile] = 2; + index++; + if (index >= this->constructionTileCount) { + break; + } + } + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::setMiscDisplayLayer, this)(buildingID); + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::updatePathLinkagesForBuilding, this)(buildingID); + if (keepType == OpenSHC::Map::Buildings::BT_MANORHOUSE || keepType == OpenSHC::Map::Buildings::BT_STONEKEEP) { + keepKind = 2; + } + + // the three keep doors and the two outbuildings are laid out per keep type and orientation + int const group = orientation / 2 + keepType * 4 - 160; + tile = DAT_ViewportRenderState::instance + .translationMatrix[DAT_TerrainDefinedData::instance.field_0x264[0][group * 3 + 0].y + y] + .addXgetTile + + DAT_TerrainDefinedData::instance.field_0x264[0][group * 3 + 0].x + x; + buildingID = MACRO_CALL_MEMBER(OpenSHC::Map::Buildings::BuildingsState_Func::setupBuildingData, + DAT_BuildingsState::ptr)(playerID, DAT_TerrainDefinedData::instance.field_0x264[0][group * 3 + 0].x + x, + DAT_TerrainDefinedData::instance.field_0x264[0][group * 3 + 0].y + y, xyValue, + OpenSHC::Map::Buildings::BT_KEEPDOOR_LEFT, 1, playerID, 15); + DAT_BuildingsState::instance.buildings[buildingID].uidWhenPlaced = uid; + DAT_BuildingsState::instance.buildings[buildingID].unknownManorHouseOrStoneKeepRelated = keepKind; + DAT_BuildingsState::instance.buildings[buildingID].quarryStockpileID = (short)this->placedBuildingID; + DAT_BuildingsState::instance.buildings[buildingID].orientation = (short)keepOrientation; + this->LogicLayer[tile] |= L_BUILDING; + this->BuildingLayer[tile] = (short)buildingID; + this->ChangedLayer[tile] = 2; + MACRO_CALL_MEMBER(OpenSHC::Map::Navigation::PathFindingState_Func::updatePathLinkagesInAllEightDirections, + DAT_PathFindingState::ptr)(DAT_BuildingsState::instance.buildings[buildingID].y, tile); + + tile = DAT_ViewportRenderState::instance + .translationMatrix[DAT_TerrainDefinedData::instance.field_0x264[0][group * 3 + 1].y + y] + .addXgetTile + + DAT_TerrainDefinedData::instance.field_0x264[0][group * 3 + 1].x + x; + buildingID = MACRO_CALL_MEMBER(OpenSHC::Map::Buildings::BuildingsState_Func::setupBuildingData, + DAT_BuildingsState::ptr)(playerID, DAT_TerrainDefinedData::instance.field_0x264[0][group * 3 + 1].x + x, + DAT_TerrainDefinedData::instance.field_0x264[0][group * 3 + 1].y + y, xyValue, + OpenSHC::Map::Buildings::BT_KEEPDOOR, 1, playerID, 15); + DAT_BuildingsState::instance.buildings[buildingID].uidWhenPlaced = uid; + DAT_BuildingsState::instance.buildings[buildingID].unknownManorHouseOrStoneKeepRelated = keepKind / 2; + DAT_BuildingsState::instance.buildings[buildingID].quarryStockpileID = (short)this->placedBuildingID; + DAT_BuildingsState::instance.buildings[buildingID].orientation = (short)keepOrientation; + this->MiscDisplayLayer[tile] |= 4; + this->BuildingLayer[tile] = (short)buildingID; + this->ChangedLayer[tile] = 2; + MACRO_CALL_MEMBER(OpenSHC::Map::Navigation::PathFindingState_Func::updatePathLinkagesInAllEightDirections, + DAT_PathFindingState::ptr)(DAT_BuildingsState::instance.buildings[buildingID].y, tile); + + tile = DAT_ViewportRenderState::instance + .translationMatrix[DAT_TerrainDefinedData::instance.field_0x264[0][group * 3 + 2].y + y] + .addXgetTile + + DAT_TerrainDefinedData::instance.field_0x264[0][group * 3 + 2].x + x; + buildingID = MACRO_CALL_MEMBER(OpenSHC::Map::Buildings::BuildingsState_Func::setupBuildingData, + DAT_BuildingsState::ptr)(playerID, DAT_TerrainDefinedData::instance.field_0x264[0][group * 3 + 2].x + x, + DAT_TerrainDefinedData::instance.field_0x264[0][group * 3 + 2].y + y, xyValue, + OpenSHC::Map::Buildings::BT_KEEPDOOR_RIGHT, 1, playerID, 15); + DAT_BuildingsState::instance.buildings[buildingID].uidWhenPlaced = uid; + DAT_BuildingsState::instance.buildings[buildingID].unknownManorHouseOrStoneKeepRelated = keepKind; + DAT_BuildingsState::instance.buildings[buildingID].quarryStockpileID = (short)this->placedBuildingID; + DAT_BuildingsState::instance.buildings[buildingID].orientation = (short)keepOrientation; + this->LogicLayer[tile] |= L_BUILDING; + this->BuildingLayer[tile] = (short)buildingID; + this->ChangedLayer[tile] = 2; + MACRO_CALL_MEMBER(OpenSHC::Map::Navigation::PathFindingState_Func::updatePathLinkagesInAllEightDirections, + DAT_PathFindingState::ptr)(DAT_BuildingsState::instance.buildings[buildingID].y, tile); + + // the campground + y += DAT_TerrainDefinedData::instance.keepOutbuildingOffsets_0x384[0][group].yOffset; + x += DAT_TerrainDefinedData::instance.keepOutbuildingOffsets_0x384[0][group].xOffset; + buildingID = MACRO_CALL_MEMBER(OpenSHC::Map::Buildings::BuildingsState_Func::setupBuildingData, + DAT_BuildingsState::ptr)(playerID, x, y, xyValue, OpenSHC::Map::Buildings::BT_CAMPGROUND, 7, playerID, 15); + DAT_BuildingsState::instance.buildings[buildingID].uidWhenPlaced = uid; + DAT_BuildingsState::instance.buildings[buildingID].quarryStockpileID = (short)this->placedBuildingID; + DAT_BuildingsState::instance.buildings[buildingID].orientation = (short)keepOrientation; + MACRO_CALL_MEMBER( + OpenSHC::Game::GameStateStructures_Func::computeBuildingCategoryEntryPointAndDestroyEarlierBuilding, + DAT_GameState::ptr)(buildingID, playerID, OpenSHC::Game::Player::PDBCE_CAMPGROUND); + + index = 0; + while (true) { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::getBuildingSizeIndexMappingData, this)(index, 7); + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::resetTileAndClearMoat, this)( + DAT_ViewportRenderState::instance.translationMatrix[this->buildingY + y].addXgetTile + this->buildingX + + x); + index++; + if (index >= this->constructionTileCount) { + break; + } + } + + index = 0; + while (true) { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::getBuildingSizeIndexMappingData, this)( + index, DAT_BuildingsState::instance.buildings[buildingID].widthOrHeight); + tile = DAT_ViewportRenderState::instance.translationMatrix[this->buildingY + y].addXgetTile + + this->buildingX + x; + if (index < countof(DAT_TerrainDefinedData::instance.field_0x444)) { + DAT_BuildingsState::instance.buildings[buildingID].tileRefs[index] + = DAT_ViewportRenderState::instance + .translationMatrix[DAT_BuildingsState::instance.buildings[buildingID].y + + DAT_TerrainDefinedData::instance.field_0x444[index].y] + .addXgetTile + + DAT_BuildingsState::instance.buildings[buildingID].x + + DAT_TerrainDefinedData::instance.field_0x444[index].x; + } + this->HeightLayer[tile] = (byte)xyValue; + // only the campground's cross of five middle tiles blocks movement + if (this->buildingX == 3 && this->buildingY == 3) { + this->LogicLayer[tile] |= L_BUILDING; + } + if (this->buildingX == 3 && this->buildingY == 2) { + this->LogicLayer[tile] |= L_BUILDING; + } + if (this->buildingX == 3 && this->buildingY == 4) { + this->LogicLayer[tile] |= L_BUILDING; + } + if (this->buildingX == 2 && this->buildingY == 3) { + this->LogicLayer[tile] |= L_BUILDING; + } + if (this->buildingX == 4 && this->buildingY == 3) { + this->LogicLayer[tile] |= L_BUILDING; + } + this->BuildingLayer[tile] = (short)buildingID; + this->ChangedLayer[tile] = 2; + if (grassy != 0) { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::setTerrain, this)(playerID, tile, + this->buildingY + y, 3, OpenSHC::Map::LogicHelpers::L_NONE, + OpenSHC::Map::LogicHelpers::L2_THICK_SCRUB); + } else { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::setTerrain, this)(playerID, tile, + this->buildingY + y, 6, OpenSHC::Map::LogicHelpers::L_NONE, + OpenSHC::Map::LogicHelpers::L2_EARTH_AND_STONES); + } + index++; + if (index >= this->constructionTileCount) { + break; + } + } + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::setMiscDisplayLayer, this)(buildingID); + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::updatePathLinkagesForBuilding, this)(buildingID); + MACRO_CALL_MEMBER(OpenSHC::Map::Navigation::PathFindingState_Func::updateWalkAndPathLayer, + DAT_PathFindingState::ptr)(10, x, y); + + // the stockpile is placed from the keep's own position + x = DAT_TerrainDefinedData::instance.keepOutbuildingOffsets_0x3e4[0][group].xOffset + baseX; + y = DAT_TerrainDefinedData::instance.keepOutbuildingOffsets_0x3e4[0][group].yOffset + baseY; + index = 0; + while (true) { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::getBuildingSizeIndexMappingData, this)(index, 5); + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::resetTileAndClearMoat, this)( + DAT_ViewportRenderState::instance.translationMatrix[this->buildingY + y].addXgetTile + this->buildingX + + x); + index++; + if (index >= this->constructionTileCount) { + break; + } + } + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::placeStockpile, this)( + playerID, x, y, OpenSHC::Map::Buildings::BT_STOCKPILE, 5, 15, xyValue); + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/placeKillingPit.cpp b/src/OpenSHC/Map/TileMapState/placeKillingPit.cpp new file mode 100644 index 0000000000..d80950ff41 --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/placeKillingPit.cpp @@ -0,0 +1,81 @@ +#include "../TileMapState.func.hpp" + +#include "OpenSHC/Map/Buildings/BuildingsState.func.hpp" +#include "OpenSHC/UI/BottomLeftTextDisplayState.func.hpp" +#include "OpenSHC/DE/SHCDE/eTextSections.hpp" +#include "OpenSHC/Map/LogicHelpers/Logic1.hpp" +#include "OpenSHC/Map/LogicHelpers/Logic2.hpp" + +#include "OpenSHC/Globals/DAT_BottomLeftTextDisplayState.hpp" +#include "OpenSHC/Globals/DAT_BuildingsState.hpp" +#include "OpenSHC/Globals/DAT_GameState.hpp" +#include "OpenSHC/Globals/DAT_GameSynchronyState.hpp" +#include "OpenSHC/Globals/DAT_ViewportRenderState.hpp" + +namespace OpenSHC { +namespace Map { + + // FUNCTION: STRONGHOLDCRUSADER 0x00514520 + void TileMapState::placeKillingPit(int playerID, uint x, uint y, BuildingTypeShort buildingType, uint buildingSize, + int variation, int averageHeight) + { + /* the original masks the terrain bits once, before the building is even created */ + int grassOrScrub = this->Logic2Layer[DAT_ViewportRenderState::instance.translationMatrix[y].addXgetTile + x] + & (OpenSHC::Map::LogicHelpers::L2_OASIS_GRASS | OpenSHC::Map::LogicHelpers::L2_THICK_SCRUB); + int index = 0; + int buildingID = MACRO_CALL_MEMBER( + OpenSHC::Map::Buildings::BuildingsState_Func::setupBuildingData, DAT_BuildingsState::ptr)( + playerID, x, y, averageHeight, (BuildingType)buildingType, buildingSize, playerID, variation); + this->placedBuildingID = buildingID; + if (buildingType != OpenSHC::Map::Buildings::BT_KILLINGPIT) { + averageHeight -= 4; + if (averageHeight < 0) { + averageHeight = 0; + } + } + + while (true) { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::getBuildingSizeIndexMappingData, this)( + index, buildingSize); + int tile = DAT_ViewportRenderState::instance.translationMatrix[this->buildingY + y].addXgetTile + + this->buildingX + x; + if (index < countof(DAT_BuildingsState::instance.buildings[buildingID].tileRefs)) { + DAT_BuildingsState::instance.buildings[buildingID].tileRefs[index] = tile; + } + this->HeightLayer[tile] = (byte)averageHeight; + this->BuildingLayer[tile] = (short)buildingID; + this->BuildingWasLayer[tile] = (uchar)buildingType; + if (buildingType != OpenSHC::Map::Buildings::BT_KILLINGPIT) { + /* bug:fixme: a pit dug in grass comes back as thick scrub, not as the terrain it replaced */ + if (grassOrScrub != 0) { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::setTerrain, this)(playerID, tile, + this->buildingY + y, 3, OpenSHC::Map::LogicHelpers::L_NONE, + OpenSHC::Map::LogicHelpers::L2_THICK_SCRUB); + } else { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::setTerrain, this)(playerID, tile, + this->buildingY + y, 3, OpenSHC::Map::LogicHelpers::L_NONE, + OpenSHC::Map::LogicHelpers::L2_EARTH_AND_STONES); + } + } + index++; + this->ChangedLayer[tile] = 2; + if (index >= this->constructionTileCount) { + break; + } + } + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::setMiscDisplayLayer, this)(buildingID); + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::updatePathLinkagesForBuilding, this)(buildingID); + + if (playerID == DAT_GameSynchronyState::instance.currentPlayerSlotID + && DAT_BuildingsState::instance.buildings[buildingID].buildingTypeBasedEmployeeCount + > DAT_GameState::instance.playerDataArray[playerID].availablePeasantsAtFire + && (DAT_GameState::instance.playerDataArray[playerID].populationCap + <= DAT_GameState::instance.playerDataArray[playerID].currentPopulation + || DAT_GameState::instance.playerDataArray[playerID].popularity < 5000)) { + MACRO_CALL_MEMBER(OpenSHC::UI::BottomLeftTextDisplayState_Func::setBottomLeftTextDisplayText, + DAT_BottomLeftTextDisplayState::ptr)(1, OpenSHC::DE::SHCDE::TEXT_PANEL_FEEDBACK, 1, 0, 100, 6000); + } + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/placeOilsmelter.cpp b/src/OpenSHC/Map/TileMapState/placeOilsmelter.cpp new file mode 100644 index 0000000000..b1710d78cb --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/placeOilsmelter.cpp @@ -0,0 +1,90 @@ +#include "../TileMapState.func.hpp" + +#include "OpenSHC/Map/Buildings/BuildingsState.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_GameCore.hpp" +#include "OpenSHC/Globals/DAT_PathFindingState.hpp" +#include "OpenSHC/Globals/DAT_TerrainDefinedData.hpp" +#include "OpenSHC/Globals/DAT_ViewportRenderState.hpp" + +namespace OpenSHC { +namespace Map { + + using OpenSHC::Map::LogicHelpers::L_BUILDING; + + // FUNCTION: STRONGHOLDCRUSADER 0x00508030 + void TileMapState::placeOilsmelter(int playerID, uint x, uint y, undefined4 buildingType, uint buildingSize, + int orientation, undefined4 averageHeight) + { + int const uniqueID = DAT_GameCore::instance.uniqueGameObjectTracker; + int index = 0; + int partIndex; + if (orientation == 15) { + partIndex = 0; + } else { + partIndex = orientation / 2; + } + ushort tileBuildingID = MACRO_CALL_MEMBER( + OpenSHC::Map::Buildings::BuildingsState_Func::setupBuildingData, DAT_BuildingsState::ptr)( + playerID, x, y, averageHeight, (BuildingType)(short)buildingType, buildingSize, playerID, 15); + int buildingID = (short)tileBuildingID; + this->placedBuildingID = buildingID; + DAT_BuildingsState::instance.buildings[buildingID].uidWhenPlaced = uniqueID; + while (true) { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::getBuildingSizeIndexMappingData, this)( + index, buildingSize); + int const tile = DAT_ViewportRenderState::instance.translationMatrix[this->buildingY + y].addXgetTile + + this->buildingX + x; + this->HeightLayer[tile] = (byte)averageHeight; + this->LogicLayer[tile] |= L_BUILDING; + this->BuildingLayer[tile] = tileBuildingID; + this->BuildingWasLayer[tile] = (uchar)buildingType; + this->ChangedLayer[tile] = 2; + index++; + if (index >= this->constructionTileCount) { + break; + } + } + int const smelterID = buildingID; + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::setMiscDisplayLayer, this)(buildingID); + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::updatePathLinkagesForBuilding, this)(buildingID); + + // the campfire sits at a fixed offset from the smelter, chosen by its orientation + x += DAT_TerrainDefinedData::instance.field_0x19c[partIndex].x; + y += DAT_TerrainDefinedData::instance.field_0x19c[partIndex].y; + tileBuildingID = MACRO_CALL_MEMBER( + OpenSHC::Map::Buildings::BuildingsState_Func::setupBuildingData, DAT_BuildingsState::ptr)( + playerID, x, y, averageHeight, OpenSHC::Map::Buildings::BT_CAMPFIRE, 4, playerID, 15); + buildingID = (short)tileBuildingID; + DAT_BuildingsState::instance.buildings[buildingID].uidWhenPlaced = uniqueID; + DAT_BuildingsState::instance.buildings[buildingID].quarryStockpileID = (short)this->placedBuildingID; + DAT_BuildingsState::instance.buildings[smelterID].quarryStockpileID = tileBuildingID; + int campIndex = 0; + while (true) { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::getBuildingSizeIndexMappingData, this)( + campIndex, DAT_BuildingsState::instance.buildings[buildingID].widthOrHeight); + int const tile = DAT_ViewportRenderState::instance.translationMatrix[this->buildingY + y].addXgetTile + + this->buildingX + x; + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::resetTileAndClearMoat, this)(tile); + this->HeightLayer[tile] = (byte)averageHeight; + this->BuildingLayer[tile] = tileBuildingID; + this->BuildingWasLayer[tile] = (uchar)buildingType; + this->ChangedLayer[tile] = 2; + campIndex++; + if (campIndex >= this->constructionTileCount) { + break; + } + } + MACRO_CALL_MEMBER(OpenSHC::Map::Buildings::BuildingsState_Func::setTileRefsForOilSmelter, + DAT_BuildingsState::ptr)(buildingID); + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::setMiscDisplayLayer, this)(buildingID); + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::updatePathLinkagesForBuilding, this)(buildingID); + MACRO_CALL_MEMBER(OpenSHC::Map::Navigation::PathFindingState_Func::updateWalkAndPathLayer, + DAT_PathFindingState::ptr)(8, x, y); + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/placePitchDitch.cpp b/src/OpenSHC/Map/TileMapState/placePitchDitch.cpp new file mode 100644 index 0000000000..5f3b730cd5 --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/placePitchDitch.cpp @@ -0,0 +1,65 @@ +#include "../TileMapState.func.hpp" + +#include "OpenSHC/Random/RNG.func.hpp" + +#include "OpenSHC/Globals/DAT_GameCore.hpp" +#include "OpenSHC/Globals/DAT_ViewportRenderState.hpp" +#include "OpenSHC/Globals/SEC_RNG.hpp" + +namespace OpenSHC { +namespace Map { + + using OpenSHC::Map::LogicHelpers::L_PLAIN2_AND_PITCH; + + // FUNCTION: STRONGHOLDCRUSADER 0x005116E0 + int TileMapState::placePitchDitch(undefined4 playerID, uint x, uint y) + { + if (x > 399 || y > 399) { + return 0; + } + if (DAT_ViewportRenderState::instance.DAT_BinaryTileMap400x400[y * 400 + x] == 0) { + return 0; + } + + int const existingPitchDitchID = MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::getPitchDitchIDForTile, + this)(DAT_ViewportRenderState::instance.translationMatrix[y].addXgetTile + x); + if (existingPitchDitchID != 0) { + return existingPitchDitchID; + } + + for (int pitchDitchID = 1; pitchDitchID < 4000; pitchDitchID++) { + if (this->pitchDitches[pitchDitchID].owner != 0) { + continue; + } + + if (pitchDitchID >= this->maxPitchDitchCount) { + this->maxPitchDitchCount = pitchDitchID + 1; + } + this->pitchDitches[pitchDitchID].owner = playerID; + this->pitchDitches[pitchDitchID].uid = DAT_GameCore::instance.uniqueGameObjectTracker; + DAT_GameCore::instance.uniqueGameObjectTracker++; + this->pitchDitches[pitchDitchID].y = y; + this->pitchDitches[pitchDitchID].x = x; + this->pitchDitches[pitchDitchID].tile + = DAT_ViewportRenderState::instance.translationMatrix[y].addXgetTile + x; + this->pitchDitches[pitchDitchID].state = 0; + this->pitchDitches[pitchDitchID].field_0x12 = 0; + this->pitchDitches[pitchDitchID].rng = SEC_RNG::instance.currentNumber2; + PitchDitch const& pitchDitch = this->pitchDitches[pitchDitchID]; + if (this->HeightLayer[pitchDitch.tile] > 4) { + this->HeightLayer[pitchDitch.tile] -= 4; + } else { + this->HeightLayer[pitchDitch.tile] = 0; + } + MACRO_CALL_MEMBER(OpenSHC::Random::RNG_Func::nextRandomNumber2, SEC_RNG::ptr)(); + this->LogicLayer[pitchDitch.tile] |= L_PLAIN2_AND_PITCH; + this->MiscDisplayLayer[pitchDitch.tile] &= 0xbfff; + this->MiscDisplayLayer[pitchDitch.tile] &= 0xdfff; + this->ChangedLayer[pitchDitch.tile] = 2; + return pitchDitchID; + } + return 0; + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/placePositiveFearfactor.cpp b/src/OpenSHC/Map/TileMapState/placePositiveFearfactor.cpp new file mode 100644 index 0000000000..a88a1fb7bb --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/placePositiveFearfactor.cpp @@ -0,0 +1,58 @@ +#include "../TileMapState.func.hpp" + +#include "OpenSHC/Map/Buildings/BuildingsState.func.hpp" +#include "OpenSHC/UI/BottomLeftTextDisplayState.func.hpp" +#include "OpenSHC/DE/SHCDE/eTextSections.hpp" + +#include "OpenSHC/Globals/DAT_BottomLeftTextDisplayState.hpp" +#include "OpenSHC/Globals/DAT_BuildingsState.hpp" +#include "OpenSHC/Globals/DAT_GameState.hpp" +#include "OpenSHC/Globals/DAT_GameSynchronyState.hpp" +#include "OpenSHC/Globals/DAT_ViewportRenderState.hpp" + +namespace OpenSHC { +namespace Map { + + using OpenSHC::Map::LogicHelpers::L_BUILDING; + + // FUNCTION: STRONGHOLDCRUSADER 0x00506F40 + void TileMapState::placePositiveFearfactor(int playerID, uint x, uint y, undefined4 buildingType, + undefined4 variation, uint buildingSize, undefined4 orientation, undefined4 averageHeight) + { + int index = 0; + int buildingID = MACRO_CALL_MEMBER( + OpenSHC::Map::Buildings::BuildingsState_Func::setupBuildingData, DAT_BuildingsState::ptr)( + playerID, x, y, averageHeight, (BuildingType)(short)buildingType, buildingSize, playerID, (short)variation); + this->placedBuildingID = buildingID; + while (true) { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::getBuildingSizeIndexMappingData, this)( + index, buildingSize); + int tile = DAT_ViewportRenderState::instance.translationMatrix[this->buildingY + y].addXgetTile + + this->buildingX + x; + if (index < countof(DAT_BuildingsState::instance.buildings[buildingID].tileRefs)) { + DAT_BuildingsState::instance.buildings[buildingID].tileRefs[index] = tile; + } + this->HeightLayer[tile] = (byte)averageHeight; + this->LogicLayer[tile] |= L_BUILDING; + this->BuildingLayer[tile] = (short)buildingID; + index++; + this->ChangedLayer[tile] = 2; + if (index >= this->constructionTileCount) { + break; + } + } + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::setMiscDisplayLayer, this)(buildingID); + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::updatePathLinkagesForBuilding, this)(buildingID); + if (playerID == DAT_GameSynchronyState::instance.currentPlayerSlotID + && DAT_BuildingsState::instance.buildings[buildingID].buildingTypeBasedEmployeeCount + > DAT_GameState::instance.playerDataArray[playerID].availablePeasantsAtFire + && (DAT_GameState::instance.playerDataArray[playerID].populationCap + <= DAT_GameState::instance.playerDataArray[playerID].currentPopulation + || DAT_GameState::instance.playerDataArray[playerID].popularity < 5000)) { + MACRO_CALL_MEMBER(OpenSHC::UI::BottomLeftTextDisplayState_Func::setBottomLeftTextDisplayText, + DAT_BottomLeftTextDisplayState::ptr)(1, OpenSHC::DE::SHCDE::TEXT_PANEL_FEEDBACK, 1, 0, 100, 6000); + } + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/placeQuarry.cpp b/src/OpenSHC/Map/TileMapState/placeQuarry.cpp new file mode 100644 index 0000000000..a2e97b29dd --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/placeQuarry.cpp @@ -0,0 +1,129 @@ +#include "../TileMapState.func.hpp" + +#include "OpenSHC/AI/AIVState.func.hpp" +#include "OpenSHC/Map/Buildings/BuildingsState.func.hpp" +#include "OpenSHC/UI/BottomLeftTextDisplayState.func.hpp" +#include "OpenSHC/Commands/MappersEnum.hpp" +#include "OpenSHC/DE/SHCDE/eTextSections.hpp" +#include "OpenSHC/Map/Buildings/BuildingType.hpp" +#include "OpenSHC/WindowsHelper/Enums/BOOLEnum.hpp" + +#include "OpenSHC/Globals/DAT_AIVState.hpp" +#include "OpenSHC/Globals/DAT_BottomLeftTextDisplayState.hpp" +#include "OpenSHC/Globals/DAT_BuildingsState.hpp" +#include "OpenSHC/Globals/DAT_GameCore.hpp" +#include "OpenSHC/Globals/DAT_GameState.hpp" +#include "OpenSHC/Globals/DAT_GameSynchronyState.hpp" +#include "OpenSHC/Globals/DAT_ViewportRenderState.hpp" + +namespace OpenSHC { +namespace Map { + + using OpenSHC::Map::LogicHelpers::L_BUILDING; + + // FUNCTION: STRONGHOLDCRUSADER 0x00508250 + void TileMapState::placeQuarry(int playerID, uint x, uint y, undefined4 buildingType, uint buildingSize, + int buildingOrientation, uint averageHeight) + { + int index = 0; + int uid = DAT_GameCore::instance.uniqueGameObjectTracker; + int quarryID = MACRO_CALL_MEMBER( + OpenSHC::Map::Buildings::BuildingsState_Func::setupBuildingData, DAT_BuildingsState::ptr)(playerID, x, y, + averageHeight, (BuildingType)(short)buildingType, buildingSize, playerID, buildingOrientation); + this->placedBuildingID = quarryID; + DAT_BuildingsState::instance.buildings[quarryID].uidWhenPlaced = uid; + + while (true) { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::getBuildingSizeIndexMappingData, this)( + index, buildingSize); + int tile = DAT_ViewportRenderState::instance.translationMatrix[this->buildingY + y].addXgetTile + x + + this->buildingX; + index++; + this->HeightLayer[tile] = (byte)averageHeight; + this->LogicLayer[tile] |= L_BUILDING; + this->BuildingLayer[tile] = (short)quarryID; + this->BuildingWasLayer[tile] = (uchar)buildingType; + this->ChangedLayer[tile] = 2; + if (index >= this->constructionTileCount) { + break; + } + } + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::setMiscDisplayLayer, this)(quarryID); + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::updatePathLinkagesForBuilding, this)(quarryID); + + // nine attempts at a pile location, moving further from the quarry each time + for (int attempt = 1; attempt < 10; attempt++) { + MACRO_CALL_MEMBER( + OpenSHC::Map::Buildings::BuildingsState_Func::findQuarryPileLocation, DAT_BuildingsState::ptr)( + playerID, x, y, buildingSize, 2, attempt, OpenSHC::Commands::M_MAPPER_QUARRYPILE); + if (this->buildingPlacementFail == FALSE) { + break; + } + } + if (this->buildingPlacementFail != FALSE) { + return; + } + + // x/y now name the pile corner + x = x + DAT_BuildingsState::instance.DAT_TempXOffset; + y += DAT_BuildingsState::instance.DAT_TempYOffset; + int tileIndex = 0; + int lowest = 1000; + int highest = 0; + while (true) { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::getBuildingSizeIndexMappingData, this)(tileIndex, 2); + int tileHeight + = this->HeightLayer[DAT_ViewportRenderState::instance.translationMatrix[this->buildingY + y].addXgetTile + + x + this->buildingX]; + if (tileHeight > highest) { + highest = tileHeight; + } + if (tileHeight < lowest) { + lowest = tileHeight; + } + tileIndex++; + if (tileIndex >= this->constructionTileCount) { + break; + } + } + averageHeight = (highest - lowest) / 2 + lowest; + + tileIndex = 0; + int pileID = MACRO_CALL_MEMBER( + OpenSHC::Map::Buildings::BuildingsState_Func::setupBuildingData, DAT_BuildingsState::ptr)(playerID, x, y, + averageHeight, OpenSHC::Map::Buildings::BT_QUARRYSTOCKPILE, 2, playerID, buildingOrientation); + MACRO_CALL_MEMBER(OpenSHC::AI::AIVState_Func::incrementStructureHeatMapTile, DAT_AIVState::ptr)(x, y); + DAT_BuildingsState::instance.buildings[pileID].uidWhenPlaced = uid; + DAT_BuildingsState::instance.buildings[quarryID].quarryStockpileID = (short)pileID; + while (true) { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::getBuildingSizeIndexMappingData, this)(tileIndex, 2); + int tile = DAT_ViewportRenderState::instance.translationMatrix[this->buildingY + y].addXgetTile + x + + this->buildingX; + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::resetTileAndClearMoat, this)(tile); + this->HeightLayer[tile] = (byte)averageHeight; + this->LogicLayer[tile] |= L_BUILDING; + this->BuildingLayer[tile] = (short)pileID; + tileIndex++; + this->BuildingWasLayer[tile] = (uchar)buildingType; + this->ChangedLayer[tile] = 2; + if (tileIndex >= this->constructionTileCount) { + break; + } + } + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::setMiscDisplayLayer, this)(pileID); + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::updatePathLinkagesForBuilding, this)(pileID); + + if (playerID == DAT_GameSynchronyState::instance.currentPlayerSlotID + && DAT_BuildingsState::instance.buildings[pileID].buildingTypeBasedEmployeeCount + > DAT_GameState::instance.playerDataArray[playerID].availablePeasantsAtFire + && (DAT_GameState::instance.playerDataArray[playerID].populationCap + <= DAT_GameState::instance.playerDataArray[playerID].currentPopulation + || DAT_GameState::instance.playerDataArray[playerID].popularity < 5000)) { + // "Not enough workers available to run this building." + MACRO_CALL_MEMBER(OpenSHC::UI::BottomLeftTextDisplayState_Func::setBottomLeftTextDisplayText, + DAT_BottomLeftTextDisplayState::ptr)(1, OpenSHC::DE::SHCDE::TEXT_PANEL_FEEDBACK, 1, 0, 100, 6000); + } + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/placeRock.cpp b/src/OpenSHC/Map/TileMapState/placeRock.cpp new file mode 100644 index 0000000000..a4d0212063 --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/placeRock.cpp @@ -0,0 +1,140 @@ +#include "../TileMapState.func.hpp" + +#include "OpenSHC/Map/LandscapeState.func.hpp" +#include "OpenSHC/Map/Navigation/PathFindingState.func.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_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_MARSH; + using OpenSHC::Map::LogicHelpers::L_MOAT; + using OpenSHC::Map::LogicHelpers::L_OIL; + using OpenSHC::Map::LogicHelpers::L_PEBBLES; + using OpenSHC::Map::LogicHelpers::L_PLAIN2_AND_PITCH; + using OpenSHC::Map::LogicHelpers::L_RIVER; + using OpenSHC::Map::LogicHelpers::L_ROCKY; + using OpenSHC::Map::LogicHelpers::L_SEA; + using OpenSHC::Map::LogicHelpers::L_UNNAMED_0x40; + + // FUNCTION: STRONGHOLDCRUSADER 0x004FB4D0 + void TileMapState::placeRock(uint x, uint y, uint kind) + { + int size = 1; + int rockType = 0; + if (x > 399 || y > 399) { + return; + } + if (DAT_ViewportRenderState::instance.DAT_BinaryTileMap400x400[y * 400 + x] == 0) { + return; + } + // every rock kind has its own footprint size and rock type + short const rockKind = *(short volatile*)&kind; + if (rockKind == 0) { + rockType = 1; + } else if (rockKind == 1) { + rockType = 2; + } else if (rockKind == 2) { + rockType = 3; + } else if (rockKind == 3) { + rockType = 4; + } else if (rockKind == 4) { + size = 2; + rockType = 5; + } else if (rockKind == 5) { + size = 2; + rockType = 6; + } else if (rockKind == 6) { + size = 2; + rockType = 7; + } else if (rockKind == 7) { + size = 2; + rockType = 8; + } else if (rockKind == 8) { + size = 3; + rockType = 9; + } else if (rockKind == 9) { + size = 3; + rockType = 10; + } else if (rockKind == 10) { + size = 3; + rockType = 11; + } else if (rockKind == 11) { + size = 3; + rockType = 12; + } else if (rockKind == 12) { + rockType = 13; + size = 4; + } else if (rockKind == 13) { + rockType = 14; + size = 4; + } else if (rockKind == 14) { + rockType = 15; + size = 4; + } else if (rockKind == 15) { + rockType = 16; + size = 4; + } + + // the original reuses the kind parameter to hold the lowest height under the footprint + kind = 1000; + int highest = 0; + int index = 0; + do { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::getBuildingSizeIndexMappingData, this)(index, size); + int const tile = DAT_ViewportRenderState::instance.translationMatrix[this->buildingY + y].addXgetTile + + this->buildingX + x; + if ((this->LogicLayer[tile] & (L_BORDER | L_BORDER_EDGE)) != 0) { + return; + } + if ((this->LogicLayer[tile] & L_BUILDING) != 0) { + return; + } + if (this->OrganismLayer[tile] != 0) { + return; + } + if (this->HeightLayer[tile] > highest) { + highest = this->HeightLayer[tile]; + } + if (this->HeightLayer[tile] < (int)kind) { + kind = this->HeightLayer[tile]; + } + index++; + } while (index < this->constructionTileCount); + + int const height = (int)(highest - *(uint volatile*)&kind) / 2 + kind; + int const rockID = MACRO_CALL_MEMBER(OpenSHC::Map::LandscapeState_Func::createRock, DAT_LandscapeState::ptr)( + x, y, rockType, size, this->rockOrientation); + // ... and again for the organism id the rock is stamped with + kind = rockID + 2000; + index = 0; + do { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::getBuildingSizeIndexMappingData, this)(index, size); + index++; + int const tile = DAT_ViewportRenderState::instance.translationMatrix[this->buildingY + y].addXgetTile + + this->buildingX + x; + this->HeightLayer[tile] = height; + this->DefaultHeightLayer[tile] = height; + this->LogicLayer[tile] &= ~(L_SEA | L_PLAIN2_AND_PITCH | L_UNNAMED_0x40 | L_BOULDERS | L_PEBBLES | L_IRON + | L_RIVER | L_FORD | L_MARSH | L_MOAT | L_OIL); + this->Logic2Layer[tile] = 0; + this->LogicLayer[tile] |= L_ROCKY; + this->OrganismLayer[tile] = kind; + } while (index < this->constructionTileCount); + MACRO_CALL_MEMBER(OpenSHC::Map::Navigation::PathFindingState_Func::updateWalkAndPathLayer, + DAT_PathFindingState::ptr)(9, x, y); + DAT_PathFindingState::instance.toggleUpdateSeparateAreaTileMap = 1; + this->forceUpdateMacroLayerFlag = 1; + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/placeSiegeTent.cpp b/src/OpenSHC/Map/TileMapState/placeSiegeTent.cpp new file mode 100644 index 0000000000..5cd2b6bd23 --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/placeSiegeTent.cpp @@ -0,0 +1,59 @@ +#include "../TileMapState.func.hpp" + +#include "OpenSHC/Map/Buildings/BuildingsState.func.hpp" +#include "OpenSHC/UI/BottomLeftTextDisplayState.func.hpp" +#include "OpenSHC/DE/SHCDE/eTextSections.hpp" + +#include "OpenSHC/Globals/DAT_BottomLeftTextDisplayState.hpp" +#include "OpenSHC/Globals/DAT_BuildingsState.hpp" +#include "OpenSHC/Globals/DAT_GameState.hpp" +#include "OpenSHC/Globals/DAT_GameSynchronyState.hpp" +#include "OpenSHC/Globals/DAT_ViewportRenderState.hpp" + +namespace OpenSHC { +namespace Map { + + using OpenSHC::Map::LogicHelpers::L_BUILDING; + + // FUNCTION: STRONGHOLDCRUSADER 0x00506E10 + void TileMapState::placeSiegeTent( + int playerID, uint x, uint y, short buildingType, uint buildingSize, int variation, undefined4 averageHeight) + { + int index = 0; + int buildingID = MACRO_CALL_MEMBER( + OpenSHC::Map::Buildings::BuildingsState_Func::setupBuildingData, DAT_BuildingsState::ptr)( + playerID, x, y, averageHeight, (BuildingType)buildingType, buildingSize, playerID, variation); + this->placedBuildingID = buildingID; + while (true) { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::getBuildingSizeIndexMappingData, this)( + index, buildingSize); + int tile = DAT_ViewportRenderState::instance.translationMatrix[this->buildingY + y].addXgetTile + + this->buildingX + x; + if (index < countof(DAT_BuildingsState::instance.buildings[buildingID].tileRefs)) { + DAT_BuildingsState::instance.buildings[buildingID].tileRefs[index] = tile; + } + this->HeightLayer[tile] = (byte)averageHeight; + this->LogicLayer[tile] |= L_BUILDING; + this->BuildingLayer[tile] = (short)buildingID; + index++; + this->BuildingWasLayer[tile] = (uchar)buildingType; + this->ChangedLayer[tile] = 2; + if (index >= this->constructionTileCount) { + break; + } + } + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::setMiscDisplayLayer, this)(buildingID); + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::updatePathLinkagesForBuilding, this)(buildingID); + if (playerID == DAT_GameSynchronyState::instance.currentPlayerSlotID + && DAT_BuildingsState::instance.buildings[buildingID].buildingTypeBasedEmployeeCount + > DAT_GameState::instance.playerDataArray[playerID].availablePeasantsAtFire + && (DAT_GameState::instance.playerDataArray[playerID].populationCap + <= DAT_GameState::instance.playerDataArray[playerID].currentPopulation + || DAT_GameState::instance.playerDataArray[playerID].popularity < 5000)) { + MACRO_CALL_MEMBER(OpenSHC::UI::BottomLeftTextDisplayState_Func::setBottomLeftTextDisplayText, + DAT_BottomLeftTextDisplayState::ptr)(1, OpenSHC::DE::SHCDE::TEXT_PANEL_FEEDBACK, 1, 0, 100, 6000); + } + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/placeSiegetowerPlaced.cpp b/src/OpenSHC/Map/TileMapState/placeSiegetowerPlaced.cpp new file mode 100644 index 0000000000..46392ed4ac --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/placeSiegetowerPlaced.cpp @@ -0,0 +1,45 @@ +#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 { + + using OpenSHC::Map::LogicHelpers::L_KEEP_NON_MANOR_HOUSE; + + // FUNCTION: STRONGHOLDCRUSADER 0x00506D40 + void TileMapState::placeSiegetowerPlaced( + int playerID, uint x, uint y, short buildingType, uint buildingSize, int variation, undefined4 averageHeight) + { + int index = 0; + int buildingID = MACRO_CALL_MEMBER( + OpenSHC::Map::Buildings::BuildingsState_Func::setupBuildingData, DAT_BuildingsState::ptr)( + playerID, x, y, averageHeight, (BuildingType)buildingType, buildingSize, playerID, variation); + this->placedBuildingID = buildingID; + while (true) { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::getBuildingSizeIndexMappingData, this)( + index, buildingSize); + int tile = DAT_ViewportRenderState::instance.translationMatrix[this->buildingY + y].addXgetTile + + this->buildingX + x; + if (index < countof(DAT_BuildingsState::instance.buildings[buildingID].tileRefs)) { + DAT_BuildingsState::instance.buildings[buildingID].tileRefs[index] = tile; + } + this->HeightLayer[tile] = (byte)averageHeight; + this->LogicLayer[tile] |= L_KEEP_NON_MANOR_HOUSE; + this->BuildingLayer[tile] = (short)buildingID; + index++; + this->BuildingWasLayer[tile] = (uchar)buildingType; + this->ChangedLayer[tile] = 2; + if (index >= this->constructionTileCount) { + break; + } + } + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::setMiscDisplayLayer, this)(buildingID); + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::updatePathLinkagesForBuilding, this)(buildingID); + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/placeStockpile.cpp b/src/OpenSHC/Map/TileMapState/placeStockpile.cpp new file mode 100644 index 0000000000..6c019b991a --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/placeStockpile.cpp @@ -0,0 +1,89 @@ +#include "../TileMapState.func.hpp" + +#include "OpenSHC/Map/Buildings/BuildingsState.func.hpp" +#include "OpenSHC/UI/BottomLeftTextDisplayState.func.hpp" +#include "OpenSHC/DE/SHCDE/eTextSections.hpp" + +#include "OpenSHC/Globals/DAT_BottomLeftTextDisplayState.hpp" +#include "OpenSHC/Globals/DAT_BuildingsState.hpp" +#include "OpenSHC/Globals/DAT_GameCore.hpp" +#include "OpenSHC/Globals/DAT_GameState.hpp" +#include "OpenSHC/Globals/DAT_GameSynchronyState.hpp" +#include "OpenSHC/Globals/DAT_TerrainDefinedData.hpp" +#include "OpenSHC/Globals/DAT_ViewportRenderState.hpp" + +namespace OpenSHC { +namespace Map { + + using OpenSHC::Map::LogicHelpers::L_BUILDING; + using OpenSHC::Map::LogicHelpers::L_STOCKPILEUnk; + using OpenSHC::Map::LogicHelpers::L_WALL_OR_GATEHOUSE; + + // FUNCTION: STRONGHOLDCRUSADER 0x00508540 + void TileMapState::placeStockpile(int playerID, int x, int y, undefined4 buildingType, + undefined4 unusedBuildingSize, int variation, int averageHeight) + { + int const uid = DAT_GameCore::instance.uniqueGameObjectTracker; + + int buildingID; + for (int part = 0; part < countof(DAT_TerrainDefinedData::instance.Stockpile_BuildingPartsOffsets); part++) { + buildingID = MACRO_CALL_MEMBER( + OpenSHC::Map::Buildings::BuildingsState_Func::setupBuildingData, DAT_BuildingsState::ptr)(playerID, + DAT_TerrainDefinedData::instance.Stockpile_BuildingPartsOffsets[part].x + x, + DAT_TerrainDefinedData::instance.Stockpile_BuildingPartsOffsets[part].y + y, averageHeight + 10, + (BuildingType)(short)buildingType, 2, playerID, variation); + DAT_BuildingsState::instance.buildings[buildingID].uidWhenPlaced = uid; + DAT_BuildingsState::instance.buildings[buildingID].field_0x190 = part + 1; + int index = 0; + while (true) { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::getBuildingSizeIndexMappingData, this)(index, 2); + int const tile + = DAT_ViewportRenderState::instance + .translationMatrix[DAT_TerrainDefinedData::instance.Stockpile_BuildingPartsOffsets[part].y + + this->buildingY + y] + .addXgetTile + + DAT_TerrainDefinedData::instance.Stockpile_BuildingPartsOffsets[part].x + x + this->buildingX; + this->HeightLayer[tile] = (byte)(averageHeight + 10); + // the stockpile body itself cannot be walked over + this->LogicLayer[tile] |= L_STOCKPILEUnk | L_WALL_OR_GATEHOUSE | L_BUILDING; + this->WallOwnerLayer[tile] = this->WallOwnerLayer[tile] & 0xf8 | (byte)(playerID - 1); + this->BuildingLayer[tile] = (ushort)buildingID; + this->BuildingWasLayer[tile] = (uchar)buildingType; + this->ChangedLayer[tile] = 2; + index++; + if (index >= this->constructionTileCount) { + break; + } + } + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::updatePathLinkagesForBuilding, this)(buildingID); + } + + this->placedBuildingID = buildingID; + // the surrounding tiles carry the stockpile graphic but stay walkable + for (int slot = 0; slot != countof(DAT_TerrainDefinedData::instance.StockpilePathableOffsets); slot++) { + Navigation::Algorithms::XYPair const& offset + = DAT_TerrainDefinedData::instance.StockpilePathableOffsets[slot]; + int const tile + = DAT_ViewportRenderState::instance.translationMatrix[offset.y + y].addXgetTile + offset.x + x; + this->LogicLayer[tile] |= L_STOCKPILEUnk | L_WALL_OR_GATEHOUSE; + this->HeightLayer[tile] = (byte)(averageHeight + 10); + this->AlphaGFXLayer[tile] = (ushort)buildingID; + this->BuildingWasLayer[tile] = (uchar)buildingType; + this->WallOwnerLayer[tile] &= 0xf8; + this->WallOwnerLayer[tile] |= (byte)(playerID - 1); + this->ChangedLayer[tile] = 2; + } + + if (playerID == DAT_GameSynchronyState::instance.currentPlayerSlotID + && DAT_BuildingsState::instance.buildings[buildingID].buildingTypeBasedEmployeeCount + > DAT_GameState::instance.playerDataArray[playerID].availablePeasantsAtFire + && (DAT_GameState::instance.playerDataArray[playerID].populationCap + <= DAT_GameState::instance.playerDataArray[playerID].currentPopulation + || DAT_GameState::instance.playerDataArray[playerID].popularity < 5000)) { + MACRO_CALL_MEMBER(OpenSHC::UI::BottomLeftTextDisplayState_Func::setBottomLeftTextDisplayText, + DAT_BottomLeftTextDisplayState::ptr)(1, OpenSHC::DE::SHCDE::TEXT_PANEL_FEEDBACK, 1, 0, 100, 6000); + } + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/placeTower.cpp b/src/OpenSHC/Map/TileMapState/placeTower.cpp new file mode 100644 index 0000000000..0c6aea3147 --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/placeTower.cpp @@ -0,0 +1,59 @@ +#include "../TileMapState.func.hpp" + +#include "OpenSHC/Map/Buildings/BuildingsState.func.hpp" + +#include "OpenSHC/Globals/DAT_BuildingsState.hpp" +#include "OpenSHC/Globals/DAT_GameCore.hpp" +#include "OpenSHC/Globals/DAT_TerrainDefinedData.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; + + // FUNCTION: STRONGHOLDCRUSADER 0x00507130 + void TileMapState::placeTower( + int playerID, uint x, uint y, undefined4 buildingType, uint size, int variation, undefined4 averageHeight) + { + int uniqueID = DAT_GameCore::instance.uniqueGameObjectTracker; + int index = 0; + int buildingID = MACRO_CALL_MEMBER( + OpenSHC::Map::Buildings::BuildingsState_Func::setupBuildingData, DAT_BuildingsState::ptr)( + playerID, x, y, averageHeight, (BuildingType)(short)buildingType, size, playerID, variation); + this->placedBuildingID = buildingID; + DAT_BuildingsState::instance.buildings[buildingID].uidWhenPlaced = uniqueID; + while (true) { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::getBuildingSizeIndexMappingData, this)(index, size); + int tile = DAT_ViewportRenderState::instance.translationMatrix[this->buildingY + y].addXgetTile + x + + this->buildingX; + if (index < countof(DAT_BuildingsState::instance.buildings[buildingID].tileRefs)) { + DAT_BuildingsState::instance.buildings[buildingID].tileRefs[index] = tile; + } + this->HeightLayer[tile] = (byte)averageHeight; + if (DAT_TerrainDefinedData::instance.TowerTileOffsetsBySize[size][0] == index) { + this->LogicLayer[tile] |= L_BUILDING; + } else if (DAT_TerrainDefinedData::instance.TowerTileOffsetsBySize[size][1] == index) { + this->LogicLayer[tile] |= L_BUILDING; + } else if (DAT_TerrainDefinedData::instance.TowerTileOffsetsBySize[size][2] == index) { + this->LogicLayer[tile] |= L_BUILDING; + } else if (DAT_TerrainDefinedData::instance.TowerTileOffsetsBySize[size][3] == index) { + this->LogicLayer[tile] |= L_BUILDING; + } else { + this->LogicLayer[tile] |= L_KEEP_NON_MANOR_HOUSE; + } + this->BuildingLayer[tile] = (short)buildingID; + index++; + this->BuildingWasLayer[tile] = (uchar)buildingType; + this->ChangedLayer[tile] = 2; + if (index >= this->constructionTileCount) { + break; + } + } + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::setMiscDisplayLayer, this)(buildingID); + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::updatePathLinkagesForBuilding, this)(buildingID); + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/placeTree.cpp b/src/OpenSHC/Map/TileMapState/placeTree.cpp new file mode 100644 index 0000000000..d040195b16 --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/placeTree.cpp @@ -0,0 +1,92 @@ +#include "../TileMapState.func.hpp" + +#include "OpenSHC/Map/LandscapeState.func.hpp" +#include "OpenSHC/Map/Navigation/PathFindingState.func.hpp" + +#include "OpenSHC/Globals/DAT_LandscapeState.hpp" +#include "OpenSHC/Globals/DAT_PathFindingState.hpp" +#include "OpenSHC/Globals/DAT_TerrainDefinedData.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_PEBBLES; + 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_TREE; + using OpenSHC::Map::LogicHelpers::L_TREE_VARIATION; + using OpenSHC::Map::LogicHelpers::L_WALL_OR_GATEHOUSE; + + // FUNCTION: STRONGHOLDCRUSADER 0x00515DA0 + void TileMapState::placeTree(uint x, uint y, undefined4 treeType) + { + if (x > 399 || y > 399) { + return; + } + if (DAT_ViewportRenderState::instance.DAT_BinaryTileMap400x400[y * 400 + x] == 0) { + return; + } + + int const stage = MACRO_CALL_MEMBER( + OpenSHC::Map::LandscapeState_Func::getValueFrom0UpTo3ForTreeTypeAndTreeStage, DAT_LandscapeState::ptr)( + MACRO_CALL_MEMBER(OpenSHC::Map::LandscapeState_Func::mapUITreeTypeToLogicalTreeType, + DAT_LandscapeState::ptr)((short)treeType), + 3); + int const baseTile = DAT_ViewportRenderState::instance.translationMatrix[y].addXgetTile + x; + int tile = DAT_ViewportRenderState::instance.translationMatrix[y].addXgetTile + x; + int const brushSize = DAT_TerrainDefinedData::instance.BrushSizeArray[stage]; + if ((this->LogicLayer[tile] + & (L_SEA | L_PLAIN1_AND_FARM | L_BORDER | L_BORDER_EDGE | L_ROCKY | L_WALL_OR_GATEHOUSE | L_BUILDING + | L_TREE_VARIATION | L_BOULDERS | L_PEBBLES | L_IRON | L_RIVER | L_FORD | L_KEEP_NON_MANOR_HOUSE + | L_MARSH | L_MOAT)) + != 0) { + return; + } + + int brushY = y; + for (int index = 0; index < brushSize; index++) { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::getTileForBrush, this)( + 1, index, &tile, &brushY, baseTile, y); + if ((this->LogicLayer[tile] & L_BORDER) != 0) { + break; + } + if ((this->LogicLayer[tile] & (L_WALL_OR_GATEHOUSE | L_BUILDING | L_RIVER | L_KEEP_NON_MANOR_HOUSE)) != 0) { + return; + } + if ((this->LogicLayer[tile] & L_ROCKY) != 0) { + return; + } + if (this->OrganismLayer[tile] != 0) { + return; + } + } + + ushort logicalType + = (ushort)MACRO_CALL_MEMBER(OpenSHC::Map::LandscapeState_Func::mapUITreeTypeToLogicalTreeType, + DAT_LandscapeState::ptr)((short)treeType); + int const treeID = MACRO_CALL_MEMBER(OpenSHC::Map::LandscapeState_Func::createTree, DAT_LandscapeState::ptr)( + x, y, (TreeType)(short)logicalType, stage, 0, 0, 3); + this->LogicLayer[DAT_LandscapeState::instance.trees[treeID].tile] &= ~L_ROCKY; + this->LogicLayer[DAT_LandscapeState::instance.trees[treeID].tile] |= L_TREE; + this->OrganismLayer[DAT_LandscapeState::instance.trees[treeID].tile] = (short)treeID; + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::applyTreeBrushToLogicalLayer, this)(treeID, 0); + MACRO_CALL_MEMBER(OpenSHC::Map::Navigation::PathFindingState_Func::updatePathLinkagesInAllEightDirections, + DAT_PathFindingState::ptr)( + DAT_LandscapeState::instance.trees[treeID].yPosition, DAT_LandscapeState::instance.trees[treeID].tile); + DAT_PathFindingState::instance.toggleUpdateSeparateAreaTileMap = 1; + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/placeTunnelersguild.cpp b/src/OpenSHC/Map/TileMapState/placeTunnelersguild.cpp new file mode 100644 index 0000000000..81ce055480 --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/placeTunnelersguild.cpp @@ -0,0 +1,88 @@ +#include "../TileMapState.func.hpp" + +#include "OpenSHC/Map/Buildings/BuildingsState.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_GameCore.hpp" +#include "OpenSHC/Globals/DAT_PathFindingState.hpp" +#include "OpenSHC/Globals/DAT_TerrainDefinedData.hpp" +#include "OpenSHC/Globals/DAT_ViewportRenderState.hpp" + +namespace OpenSHC { +namespace Map { + + using OpenSHC::Map::LogicHelpers::L_BUILDING; + + // FUNCTION: STRONGHOLDCRUSADER 0x00507E00 + void TileMapState::placeTunnelersguild(int playerID, uint x, uint y, undefined4 buildingType, uint buildingSize, + int orientation, undefined4 averageHeight) + { + int const uniqueID = DAT_GameCore::instance.uniqueGameObjectTracker; + int index = 0; + int partIndex; + if (orientation == 15) { + partIndex = 0; + } else { + partIndex = orientation / 2; + } + ushort placedID = MACRO_CALL_MEMBER( + OpenSHC::Map::Buildings::BuildingsState_Func::setupBuildingData, DAT_BuildingsState::ptr)( + playerID, x, y, averageHeight, (BuildingType)(short)buildingType, buildingSize, playerID, 15); + int buildingID = (short)placedID; + this->placedBuildingID = buildingID; + DAT_BuildingsState::instance.buildings[buildingID].uidWhenPlaced = uniqueID; + while (true) { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::getBuildingSizeIndexMappingData, this)( + index, buildingSize); + int const tile = DAT_ViewportRenderState::instance.translationMatrix[this->buildingY + y].addXgetTile + + this->buildingX + x; + DAT_BuildingsState::instance.buildings[buildingID].tileRefs[index] = tile; + this->HeightLayer[tile] = (byte)averageHeight; + this->LogicLayer[tile] |= L_BUILDING; + this->BuildingLayer[tile] = placedID; + this->BuildingWasLayer[tile] = (uchar)buildingType; + this->ChangedLayer[tile] = 2; + index++; + if (index >= this->constructionTileCount) { + break; + } + } + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::setMiscDisplayLayer, this)(buildingID); + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::updatePathLinkagesForBuilding, this)(buildingID); + + x += DAT_TerrainDefinedData::instance.BuildingPartsOffsets[partIndex][1].x; + y += DAT_TerrainDefinedData::instance.BuildingPartsOffsets[partIndex][1].y; + placedID = MACRO_CALL_MEMBER( + OpenSHC::Map::Buildings::BuildingsState_Func::setupBuildingData, DAT_BuildingsState::ptr)( + playerID, x, y, averageHeight, OpenSHC::Map::Buildings::BT_PARADEGROUND5, 5, playerID, 15); + buildingID = (short)placedID; + DAT_BuildingsState::instance.buildings[buildingID].uidWhenPlaced = uniqueID; + DAT_BuildingsState::instance.buildings[buildingID].quarryStockpileID = (short)this->placedBuildingID; + int campIndex = 0; + while (true) { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::getBuildingSizeIndexMappingData, this)( + campIndex, DAT_BuildingsState::instance.buildings[buildingID].widthOrHeight); + int const tile = DAT_ViewportRenderState::instance.translationMatrix[this->buildingY + y].addXgetTile + + this->buildingX + x; + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::resetTileAndClearMoat, this)(tile); + this->HeightLayer[tile] = (byte)averageHeight; + this->BuildingLayer[tile] = placedID; + this->BuildingWasLayer[tile] = (uchar)buildingType; + this->ChangedLayer[tile] = 2; + campIndex++; + if (campIndex >= this->constructionTileCount) { + break; + } + } + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::setMiscDisplayLayer, this)(buildingID); + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::updatePathLinkagesForBuilding, this)(buildingID); + MACRO_CALL_MEMBER(OpenSHC::Map::Navigation::PathFindingState_Func::updateWalkAndPathLayer, + DAT_PathFindingState::ptr)(8, x, y); + MACRO_CALL_MEMBER(OpenSHC::Map::Buildings::BuildingsState_Func::setupTunnelersGuildCampgroundPositions, + DAT_BuildingsState::ptr)(playerID); + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/placeWalls.cpp b/src/OpenSHC/Map/TileMapState/placeWalls.cpp new file mode 100644 index 0000000000..b39a8df717 --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/placeWalls.cpp @@ -0,0 +1,272 @@ +#include "../TileMapState.func.hpp" + +#include "OpenSHC/Game/GameStateStructures.func.hpp" +#include "OpenSHC/Map/Buildings/BuildingsState.func.hpp" +#include "OpenSHC/Map/Navigation/PathFindingState.func.hpp" +#include "OpenSHC/Map/WallAndPitchState.func.hpp" +#include "OpenSHC/UI/MinimapViewState.func.hpp" +#include "OpenSHC/Map/Units/States/UnitState.hpp" +#include "OpenSHC/Map/Units/UnitType.hpp" +#include "OpenSHC/WindowsHelper/Enums/BOOLEnum.hpp" + +#include "OpenSHC/Globals/DAT_BuildingsState.hpp" +#include "OpenSHC/Globals/DAT_GameState.hpp" +#include "OpenSHC/Globals/DAT_GameSynchronyState.hpp" +#include "OpenSHC/Globals/DAT_LandscapeState.hpp" +#include "OpenSHC/Globals/DAT_MinimapViewState.hpp" +#include "OpenSHC/Globals/DAT_PathFindingState.hpp" +#include "OpenSHC/Globals/DAT_TileMapState.hpp" +#include "OpenSHC/Globals/DAT_UnitsState.hpp" +#include "OpenSHC/Globals/DAT_ViewportRenderState.hpp" +#include "OpenSHC/Globals/DAT_WallAndPitchState.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::Units::UnitType; + using OpenSHC::Map::Units::States::UnitState; + + // FUNCTION: STRONGHOLDCRUSADER 0x00502F30 + void TileMapState::placeWalls( + int playerID, uint x1, uint y1, uint x2, uint y2, MappersEnum wallType, int tileCountUnk) + { + int budget = 0; + int wallCount = 0; + int placed = 0; + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::validateWallBuildPath, this)( + playerID, x1, y1, x2, y2, wallType); + if (this->illegalBuild != FALSE) { + return; + } + + this->constructionTileCount = MACRO_CALL_MEMBER( + OpenSHC::Game::GameStateStructures_Func::getWallTilesThatCanBeBuilt, DAT_GameState::ptr)(playerID, 4); + int overshoot = 2; + if ((short)wallType == OpenSHC::Commands::M_MAPPER_STAIR) { + budget = this->maxWallHeightInPath; + } + if (playerID == DAT_GameSynchronyState::instance.currentPlayerSlotID) { + MACRO_CALL_MEMBER( + OpenSHC::Map::WallAndPitchState_Func::resetWallPlacementInfo, DAT_WallAndPitchState::ptr)(); + } + + int upY = y1 - y2; + int leftX = x1 - x2; + int downY = y2 - y1; + int rightX = x2 - x1; + uint x = x1; + uint y = y1; + int* translation = &DAT_ViewportRenderState::instance.translationMatrix[y1].addXgetTile; + while (true) { + if ((short)wallType == OpenSHC::Commands::M_MAPPER_STAIR && budget < 24) { + break; + } + if (placed >= tileCountUnk || wallCount > (int)this->constructionTileCount) { + break; + } + placed++; + int const tile = *translation + x; + BOOLEnum towerNeighbours = FALSE; + if ((short)wallType == OpenSHC::Commands::M_MAPPER_STAIR + && (this->LogicLayer[tile] & L_WALL_OR_GATEHOUSE) != 0) { + break; + } + BOOLEnum applyWall = FALSE; + // a wall dragged over a crenel replaces it without counting against the tally + BOOLEnum overCrenel = FALSE; + if ((short)wallType == OpenSHC::Commands::M_MAPPER_WALL && (this->LogicLayer[tile] & L_CRENEL) != 0) { + overCrenel = TRUE; + } + if ((this->LogicLayer[tile] & L_WALL_OR_GATEHOUSE) == 0 || this->DamageLayer[tile] != 0 || overCrenel) { + if (playerID == DAT_GameSynchronyState::instance.currentPlayerSlotID) { + MACRO_CALL_MEMBER(OpenSHC::Map::WallAndPitchState_Func::addWallPlacementInfoForTile, + DAT_WallAndPitchState::ptr)(tile); + } + if (!overCrenel) { + applyWall = TRUE; + } + if (this->DamageLayer[tile] != 0 || overCrenel) { + this->DamageLayer[tile] = 0; + this->HeightLayer[tile] = this->DefaultHeightLayer[tile]; + if (overCrenel) { + this->LogicLayer[tile] + = this->LogicLayer[tile] & ~(L_CRENEL | L_CRENEL_VARIATIONUnk) | L_WALL_OR_GATEHOUSE; + } + } + + if ((short)wallType == OpenSHC::Commands::M_MAPPER_WALL) { + this->HeightLayer[tile] += 90; + } else if ((short)wallType == OpenSHC::Commands::M_MAPPER_WOODWALL) { + this->HeightLayer[tile] += 60; + } else if ((short)wallType == OpenSHC::Commands::M_MAPPER_CRENAL) { + this->LogicLayer[tile] |= L_CRENEL; + if (MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::hasOnlyTowerNeighborsNoWalls, this)( + tile, y)) { + this->HeightLayer[tile] += 68; + towerNeighbours = TRUE; + } else { + this->HeightLayer[tile] += 98; + } + // a crenel on an odd tile of one axis carries the alternate variation + if ((x & 1) != 0 && (y & 1) == 0) { + this->LogicLayer[tile] |= L_CRENEL_VARIATIONUnk; + } else if ((x & 1) == 0 && (y & 1) != 0) { + this->LogicLayer[tile] |= L_CRENEL_VARIATIONUnk; + } + } else if ((short)wallType == OpenSHC::Commands::M_MAPPER_STAIR) { + int const steps = budget - this->HeightLayer[tile] - 16; + if (steps > 0) { + this->LogicLayer[tile] |= L_STAIRS; + this->HeightLayer[tile] += (char)steps; + } else { + applyWall = FALSE; + } + } + + if (applyWall || overCrenel) { + if (applyWall && !overCrenel) { + wallCount++; + this->LogicLayer[tile] + = this->LogicLayer[tile] & ~(L_UNKNOWN_WALL_RELATED | L_BOULDERS | L_PEBBLES | L_IRON) + | L_WALL_OR_GATEHOUSE; + ushort const unit = this->UnitLayer[tile]; + if (unit != 0 + && DAT_UnitsState::instance.units[(short)unit].unitType + == OpenSHC::Map::Units::UT_CHICKEN) { + DAT_UnitsState::instance.units[(short)unit].state.generic + = OpenSHC::Map::Units::States::US_DISAPPEAR; + DAT_UnitsState::instance.units[(short)unit].updateTickTracker = 0; + DAT_UnitsState::instance.units[(short)unit].disappearFadeAlphaCountdown = 0; + } + 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 const organism = this->OrganismLayer[tile]; + if (organism < 2000) { + // scrub is flattened by the wall, a real tree is left standing + switch (DAT_LandscapeState::instance.trees[organism].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[organism].state = 3; + } + } + } + if ((short)wallType == OpenSHC::Commands::M_MAPPER_WOODWALL || towerNeighbours) { + this->LogicLayer[tile] |= L_UNKNOWN_WALL_RELATED; + } + this->WallOwnerLayer[tile] = (this->WallOwnerLayer[tile] & 0xf8) | (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); + } + } + + int const stepX = (int)x >= (int)x2 ? leftX : rightX; + int const stepY = (int)y >= (int)y2 ? upY : downY; + if ((short)wallType == OpenSHC::Commands::M_MAPPER_STAIR) { + if (stepX > stepY) { + if ((int)x >= (int)x2) { + leftX -= 1; + x -= 1; + rightX += 1; + } else { + leftX += 1; + x += 1; + rightX -= 1; + } + } else if (stepY > 0) { + if ((int)y >= (int)y2) { + y -= 1; + translation -= 3; + upY -= 1; + downY += 1; + } else { + y += 1; + translation += 3; + upY += 1; + downY -= 1; + } + } else { + overshoot -= 1; + } + } else { + if (stepX != 0) { + if ((int)x >= (int)x2) { + leftX -= 1; + x -= 1; + rightX += 1; + } else { + leftX += 1; + x += 1; + rightX -= 1; + } + } + if (stepY != 0) { + if ((int)y >= (int)y2) { + y -= 1; + translation -= 3; + upY -= 1; + downY += 1; + } else { + y += 1; + translation += 3; + upY += 1; + downY -= 1; + } + } + if (x == x2 && y == y2) { + overshoot -= 1; + } + } + if (budget >= 16) { + budget -= 16; + } + if (x == x2 && y == y2 && overshoot == 0) { + break; + } + } + + // a stair aborts with wallType still M_MAPPER_STAIR, so it always charges stone + if ((short)wallType == OpenSHC::Commands::M_MAPPER_WOODWALL) { + MACRO_CALL_MEMBER(OpenSHC::Map::Buildings::BuildingsState_Func::processWallBuildingLoss, + DAT_BuildingsState::ptr)(playerID, 0, wallCount, 0); + } else { + MACRO_CALL_MEMBER(OpenSHC::Map::Buildings::BuildingsState_Func::processWallBuildingLoss, + DAT_BuildingsState::ptr)(playerID, wallCount, 0, 0); + } + 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/placeWheatfarm.cpp b/src/OpenSHC/Map/TileMapState/placeWheatfarm.cpp new file mode 100644 index 0000000000..d12e308ad9 --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/placeWheatfarm.cpp @@ -0,0 +1,97 @@ +#include "../TileMapState.func.hpp" + +#include "OpenSHC/Map/Buildings/BuildingsState.func.hpp" +#include "OpenSHC/UI/BottomLeftTextDisplayState.func.hpp" +#include "OpenSHC/DE/SHCDE/eTextSections.hpp" +#include "OpenSHC/Map/Buildings/BuildingType.hpp" +#include "OpenSHC/Map/LogicHelpers/Logic1.hpp" +#include "OpenSHC/Map/LogicHelpers/Logic2.hpp" + +#include "OpenSHC/Globals/DAT_BottomLeftTextDisplayState.hpp" +#include "OpenSHC/Globals/DAT_BuildingsState.hpp" +#include "OpenSHC/Globals/DAT_GameState.hpp" +#include "OpenSHC/Globals/DAT_GameSynchronyState.hpp" +#include "OpenSHC/Globals/DAT_TerrainDefinedData.hpp" +#include "OpenSHC/Globals/DAT_ViewportRenderState.hpp" + +namespace OpenSHC { +namespace Map { + + using OpenSHC::Map::LogicHelpers::L2_THICK_SCRUB; + using OpenSHC::Map::LogicHelpers::L_BUILDING; + using OpenSHC::Map::LogicHelpers::L_FARM_FIELD_WHEAT; + using OpenSHC::Map::LogicHelpers::L_NONE; + + // FUNCTION: STRONGHOLDCRUSADER 0x00514F30 + void TileMapState::placeWheatfarm(int playerID, uint x, uint y, undefined4 buildingType, undefined4 buildingSize, + int variation, undefined4 averageHeight) + { + int index = 0; + int buildingID = MACRO_CALL_MEMBER( + OpenSHC::Map::Buildings::BuildingsState_Func::setupBuildingData, DAT_BuildingsState::ptr)( + playerID, x, y, averageHeight, (BuildingType)(short)buildingType, 3, playerID, variation); + this->placedBuildingID = buildingID; + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::markBuildingFootprintFlag, this)(x, y, 9); + + // the 36 field tiles, from one of two alternating layouts + for (int i = 0; i < 36; i++) { + DAT_BuildingsState::instance.buildings[buildingID].tileRefs[i] + = DAT_ViewportRenderState::instance + .translationMatrix[DAT_TerrainDefinedData::instance + .WheatFarmTiles[DAT_GameState::instance.playerDataArray[playerID] + .unkWheatFarmOrientation][i] + .y + + y] + .addXgetTile + + x + + DAT_TerrainDefinedData::instance + .WheatFarmTiles[DAT_GameState::instance.playerDataArray[playerID].unkWheatFarmOrientation][i] + .x; + } + DAT_GameState::instance.playerDataArray[playerID].unkWheatFarmOrientation++; + if (DAT_GameState::instance.playerDataArray[playerID].unkWheatFarmOrientation > 1) { + DAT_GameState::instance.playerDataArray[playerID].unkWheatFarmOrientation = 0; + } + + // the farm house + while (true) { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::getBuildingSizeIndexMappingData, this)(index, 3); + int tile = DAT_ViewportRenderState::instance.translationMatrix[this->buildingY + y].addXgetTile + x + + this->buildingX; + this->HeightLayer[tile] = (byte)averageHeight; + this->LogicLayer[tile] |= L_BUILDING; + this->BuildingLayer[tile] = (short)buildingID; + this->BuildingWasLayer[tile] = (uchar)buildingType; + this->ChangedLayer[tile] = 2; + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::setTerrain, this)( + playerID, tile, this->buildingY + y, 2, L_NONE, L2_THICK_SCRUB); + index++; + if (index >= this->constructionTileCount) { + break; + } + } + + for (int i = 0; i < 36; i++) { + int tile = DAT_BuildingsState::instance.buildings[buildingID].tileRefs[i]; + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::resetTileAndClearMoat, this)(tile); + this->LogicLayer[tile] |= L_FARM_FIELD_WHEAT; + this->BuildingLayer[tile] = (short)buildingID; + this->BuildingWasLayer[tile] = (uchar)buildingType; + this->ChangedLayer[tile] = 2; + } + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::setMiscDisplayLayer, this)(buildingID); + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::updatePathLinkagesForBuilding, this)(buildingID); + + // not enough idle peasants for the workers: tell the local player + Game::Player::PlayerData const& player = DAT_GameState::instance.playerDataArray[playerID]; + if (playerID == DAT_GameSynchronyState::instance.currentPlayerSlotID + && DAT_BuildingsState::instance.buildings[buildingID].buildingTypeBasedEmployeeCount + > player.availablePeasantsAtFire + && (player.populationCap <= player.currentPopulation || player.popularity < 5000)) { + MACRO_CALL_MEMBER(OpenSHC::UI::BottomLeftTextDisplayState_Func::setBottomLeftTextDisplayText, + DAT_BottomLeftTextDisplayState::ptr)(1, OpenSHC::DE::SHCDE::TEXT_PANEL_FEEDBACK, 1, 0, 100, 6000); + } + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/placeWorkshopOrHovel.cpp b/src/OpenSHC/Map/TileMapState/placeWorkshopOrHovel.cpp new file mode 100644 index 0000000000..e55a6bc187 --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/placeWorkshopOrHovel.cpp @@ -0,0 +1,65 @@ +#include "../TileMapState.func.hpp" + +#include "OpenSHC/Map/Buildings/BuildingsState.func.hpp" +#include "OpenSHC/UI/BottomLeftTextDisplayState.func.hpp" +#include "OpenSHC/DE/SHCDE/eTextSections.hpp" + +#include "OpenSHC/Globals/DAT_BottomLeftTextDisplayState.hpp" +#include "OpenSHC/Globals/DAT_BuildingsState.hpp" +#include "OpenSHC/Globals/DAT_GameState.hpp" +#include "OpenSHC/Globals/DAT_GameSynchronyState.hpp" +#include "OpenSHC/Globals/DAT_ViewportRenderState.hpp" + +namespace OpenSHC { +namespace Map { + + using OpenSHC::Map::LogicHelpers::L_BUILDING; + + // FUNCTION: STRONGHOLDCRUSADER 0x00506BD0 + void TileMapState::placeWorkshopOrHovel( + int playerID, uint x, uint y, BuildingType type, uint size, int orientation, undefined4 averageHeight) + { + int index = 0; + int buildingID = MACRO_CALL_MEMBER( + OpenSHC::Map::Buildings::BuildingsState_Func::setupBuildingData, DAT_BuildingsState::ptr)( + playerID, x, y, averageHeight, (BuildingType)(short)type, size, playerID, orientation); + // the original stores only the low half of the int field + *(short*)&DAT_BuildingsState::instance.buildings[buildingID].hovelVisualStyle + = (short)DAT_GameState::instance.playerDataArray[playerID].hovelCountUpToEight; + if ((short)type == OpenSHC::Map::Buildings::BT_HOVEL) { + DAT_GameState::instance.playerDataArray[playerID].hovelCountUpToEight + = (DAT_GameState::instance.playerDataArray[playerID].hovelCountUpToEight + 1) % 7; + } + this->placedBuildingID = buildingID; + while (true) { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::getBuildingSizeIndexMappingData, this)(index, size); + int tile = DAT_ViewportRenderState::instance.translationMatrix[this->buildingY + y].addXgetTile + + this->buildingX + x; + if (index < countof(DAT_BuildingsState::instance.buildings[buildingID].tileRefs)) { + DAT_BuildingsState::instance.buildings[buildingID].tileRefs[index] = tile; + } + this->HeightLayer[tile] = (byte)averageHeight; + this->LogicLayer[tile] |= L_BUILDING; + this->BuildingLayer[tile] = (short)buildingID; + index++; + this->BuildingWasLayer[tile] = (uchar)type; + this->ChangedLayer[tile] = 2; + if (index >= this->constructionTileCount) { + break; + } + } + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::setMiscDisplayLayer, this)(buildingID); + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::updatePathLinkagesForBuilding, this)(buildingID); + if (playerID == DAT_GameSynchronyState::instance.currentPlayerSlotID + && DAT_BuildingsState::instance.buildings[buildingID].buildingTypeBasedEmployeeCount + > DAT_GameState::instance.playerDataArray[playerID].availablePeasantsAtFire + && (DAT_GameState::instance.playerDataArray[playerID].populationCap + <= DAT_GameState::instance.playerDataArray[playerID].currentPopulation + || DAT_GameState::instance.playerDataArray[playerID].popularity < 5000)) { + MACRO_CALL_MEMBER(OpenSHC::UI::BottomLeftTextDisplayState_Func::setBottomLeftTextDisplayText, + DAT_BottomLeftTextDisplayState::ptr)(1, OpenSHC::DE::SHCDE::TEXT_PANEL_FEEDBACK, 1, 0, 100, 6000); + } + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/prepareAreaForBuildingPlacement.cpp b/src/OpenSHC/Map/TileMapState/prepareAreaForBuildingPlacement.cpp new file mode 100644 index 0000000000..29d46e9cbd --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/prepareAreaForBuildingPlacement.cpp @@ -0,0 +1,124 @@ +#include "../TileMapState.func.hpp" + +#include "OpenSHC/Game/GameStateStructures.func.hpp" +#include "OpenSHC/IO/LowLevelMemory.func.hpp" +#include "OpenSHC/Map/Buildings/BuildingsState.func.hpp" +#include "OpenSHC/Commands/MappersEnum.hpp" +#include "OpenSHC/Map/Buildings/BuildingType.hpp" +#include "OpenSHC/WindowsHelper/Enums/BOOLEnum.hpp" + +#include "OpenSHC/Globals/DAT_BuildingsState.hpp" +#include "OpenSHC/Globals/DAT_GameState.hpp" +#include "OpenSHC/Globals/DAT_LowLevelMemory.hpp" +#include "OpenSHC/Globals/DAT_TileMapState.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_FARM_FIELD_APPLE; + + // FUNCTION: STRONGHOLDCRUSADER 0x005160C0 + BOOLEnum TileMapState::prepareAreaForBuildingPlacement( + int playerID, uint x, uint y, MappersEnum commandBuildingType, int buildingWidthOrHeight) + { + BOOLEnum result = FALSE; + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::storeMinAndMaxHeightOfArea, this)( + x, y, buildingWidthOrHeight); + + int index = 0; + do { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::getBuildingSizeIndexMappingData, this)( + index, buildingWidthOrHeight); + uint const tileY = y + this->buildingY; + uint const tileX = x + this->buildingX; + if (tileX > 399) { + return FALSE; + } + if (tileY > 399) { + return FALSE; + } + if (DAT_ViewportRenderState::instance.DAT_BinaryTileMap400x400[tileY * 400 + tileX] == 0) { + return FALSE; + } + int const tile = DAT_ViewportRenderState::instance.translationMatrix[this->buildingY + y].addXgetTile + + this->buildingX + x; + if ((this->LogicLayer[tile] & (L_BORDER | L_BORDER_EDGE)) != 0) { + return FALSE; + } + if (MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::isBuildingPlacementAllowedAtTile, this)( + tile, playerID, (MappersEnum)(short)commandBuildingType, 1) + == 1) { + return FALSE; + } + index++; + } while (index < this->constructionTileCount); + + // the original walks the footprint again through the command parameter + commandBuildingType = OpenSHC::Commands::M_MAPPER_NULL; + while (true) { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::getBuildingSizeIndexMappingData, this)( + commandBuildingType, buildingWidthOrHeight); + int const tile = DAT_ViewportRenderState::instance.translationMatrix[this->buildingY + y].addXgetTile + x + + this->buildingX; + int buildingID = DAT_TileMapState::instance.BuildingLayer[tile]; + if (buildingID != 0) { + if (DAT_BuildingsState::instance.buildings[buildingID].buildingType + == OpenSHC::Map::Buildings::BT_WOODCUTTERSHUT + || DAT_BuildingsState::instance.buildings[buildingID].buildingType + == OpenSHC::Map::Buildings::BT_OXTETHER + || DAT_BuildingsState::instance.buildings[buildingID].buildingType + == OpenSHC::Map::Buildings::BT_QUARRY + || DAT_BuildingsState::instance.buildings[buildingID].buildingType + == OpenSHC::Map::Buildings::BT_IRONMINE + || DAT_BuildingsState::instance.buildings[buildingID].buildingType + == OpenSHC::Map::Buildings::BT_WHEATFARM + || DAT_BuildingsState::instance.buildings[buildingID].buildingType + == OpenSHC::Map::Buildings::BT_HOPFARM + || DAT_BuildingsState::instance.buildings[buildingID].buildingType + == OpenSHC::Map::Buildings::BT_APPLEFARM + || DAT_BuildingsState::instance.buildings[buildingID].buildingType + == OpenSHC::Map::Buildings::BT_DAIRYFARM) { + MACRO_CALL_MEMBER( + OpenSHC::Map::Buildings::BuildingsState_Func::giveBackResourceForDestroyedBuilding, + DAT_BuildingsState::ptr)(buildingID, playerID, 50); + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::clearBuildingFromTerrain, DAT_TileMapState::ptr)( + buildingID); + MACRO_CALL_MEMBER( + OpenSHC::Game::GameStateStructures_Func::updatePrimaryBuildingPlayerDataReferences, + DAT_GameState::ptr)(buildingID); + MACRO_CALL_MEMBER(OpenSHC::IO::LowLevelMemory_Func::fillMemory_ByteValue, DAT_LowLevelMemory::ptr)( + sizeof(DAT_BuildingsState::instance.buildings[buildingID]), 0, + &DAT_BuildingsState::instance.buildings[buildingID]); + result = TRUE; + } + } else if ((DAT_TileMapState::instance.LogicLayer[tile] & L_FARM_FIELD_APPLE) != 0) { + buildingID = MACRO_CALL_MEMBER( + OpenSHC::Map::Buildings::BuildingsState_Func::getFarmThatHasTile, DAT_BuildingsState::ptr)(tile); + if (buildingID != 0) { + MACRO_CALL_MEMBER( + OpenSHC::Map::Buildings::BuildingsState_Func::giveBackResourceForDestroyedBuilding, + DAT_BuildingsState::ptr)(buildingID, playerID, 50); + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::clearBuildingFromTerrain, DAT_TileMapState::ptr)( + buildingID); + MACRO_CALL_MEMBER( + OpenSHC::Game::GameStateStructures_Func::updatePrimaryBuildingPlayerDataReferences, + DAT_GameState::ptr)(buildingID); + MACRO_CALL_MEMBER(OpenSHC::IO::LowLevelMemory_Func::fillMemory_ByteValue, DAT_LowLevelMemory::ptr)( + sizeof(DAT_BuildingsState::instance.buildings[buildingID]), 0, + &DAT_BuildingsState::instance.buildings[buildingID]); + result = TRUE; + } + } + commandBuildingType = (MappersEnum)(commandBuildingType + 1); + if ((int)commandBuildingType >= this->constructionTileCount) { + break; + } + } + return result; + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/prepareMap.cpp b/src/OpenSHC/Map/TileMapState/prepareMap.cpp new file mode 100644 index 0000000000..c7872a2b91 --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/prepareMap.cpp @@ -0,0 +1,184 @@ +// disable deprecation warnings for strcpy +#pragma warning(disable : 4996) + +#include "../TileMapState.func.hpp" + +#include "OpenSHC/AI/AIVState.func.hpp" +#include "OpenSHC/Game/GameStateStructures.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/MapPropertiesState.func.hpp" +#include "OpenSHC/Map/Navigation/PathFindingState.func.hpp" +#include "OpenSHC/Map/Units/TribesState.func.hpp" +#include "OpenSHC/Map/Units/TroopValueState.func.hpp" +#include "OpenSHC/Map/Units/UnitsState.func.hpp" +#include "OpenSHC/Map/WildlifeState.func.hpp" +#include "OpenSHC/Rendering/ViewportRenderState.func.hpp" +#include "OpenSHC/Text/UserTextHandler.func.hpp" +#include "OpenSHC/UI/HoveredState.func.hpp" +#include "OpenSHC/UI/MinimapViewState.func.hpp" +#include "OpenSHC/Game/GameMode.hpp" +#include "OpenSHC/Game/GameMode2.hpp" +#include "OpenSHC/WindowsHelper/Enums/BOOLEnum.hpp" + +#include "OpenSHC/Globals/DAT_AIVState.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_GameSynchronyState.hpp" +#include "OpenSHC/Globals/DAT_HoveredState.hpp" +#include "OpenSHC/Globals/DAT_LandscapeState.hpp" +#include "OpenSHC/Globals/DAT_LowLevelMemory.hpp" +#include "OpenSHC/Globals/DAT_MapPropertiesState.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_TroopValueState.hpp" +#include "OpenSHC/Globals/DAT_UnitsState.hpp" +#include "OpenSHC/Globals/DAT_UserTextHandlerState.hpp" +#include "OpenSHC/Globals/DAT_ViewportRenderState.hpp" +#include "OpenSHC/Globals/DAT_WallAndPitchState.hpp" +#include "OpenSHC/Globals/DAT_WildlifeState.hpp" +#include "OpenSHC/Globals/FilePackagerObj.hpp" + +namespace OpenSHC { +namespace Map { + + using OpenSHC::Game::GameMode; + using OpenSHC::Game::GameMode2; + + // FUNCTION: STRONGHOLDCRUSADER 0x00512450 + void TileMapState::prepareMap() + { + int savedX = DAT_ViewportRenderState::instance.viewportState.viewportX; + int savedY = DAT_ViewportRenderState::instance.viewportState.viewportY; + if (DAT_GameSynchronyState::instance.currentGameModeCopy_SEC_Section1106 + == OpenSHC::Game::GM_SKIRMISH_SINGLE_PLAYER) { + DAT_GameSynchronyState::instance.currentGameMode = OpenSHC::Game::GM_SKIRMISH_SINGLE_PLAYER; + DAT_GameSynchronyState::instance.isHost = TRUE; + char* name = MACRO_CALL_MEMBER( + OpenSHC::Text::UserTextHandler_Func::getTextArrayPointer, DAT_UserTextHandlerState::ptr)(0); + // the human player's name goes into slot 1 + strcpy(DAT_GameSynchronyState::instance.DAT_PlayerNames[1], name); + MACRO_CALL_MEMBER(OpenSHC::AI::AIVState_Func::syncAIPlayerNamesAndBuildIntervals, DAT_AIVState::ptr)(); + DAT_GameSynchronyState::instance.currentPlayerSlotID = 1; + } else if (DAT_GameCore::instance.gameMode_2 == OpenSHC::Game::GM_CAMPAIGN_MISSION) { + if (DAT_GameCore::instance.missionNumber1to20 < 16) { + DAT_GameSynchronyState::instance.currentGameMode = OpenSHC::Game::GM_SOLITARY; + } + } else if (DAT_GameCore::instance.gameMode_2 == OpenSHC::Game::GM_BUILDERUnk) { + DAT_GameSynchronyState::instance.currentGameMode = OpenSHC::Game::GM_SOLITARY; + } + + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::changeMapOrientation, this)( + (MapOrientation)this->mapOrientation); + 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); + DAT_ViewportRenderState::instance.viewportState.viewportX = savedX; + DAT_ViewportRenderState::instance.viewportState.viewportY = savedY; + MACRO_CALL_MEMBER( + OpenSHC::Rendering::ViewportRenderState_Func::setViewportBasedOnMapSize, DAT_ViewportRenderState::ptr)(); + MACRO_CALL_MEMBER(OpenSHC::Rendering::ViewportRenderState_Func::setTileSystemMemoryLookupArrays, + DAT_ViewportRenderState::ptr)(); + MACRO_CALL_MEMBER(OpenSHC::Rendering::ViewportRenderState_Func::tweakValidTilesToExcludeMapBorders, + DAT_ViewportRenderState::ptr)(); + DAT_BuildingsState::instance.maxBuildingsCount = 2000; + DAT_UnitsState::instance.maxUnitCount = 2500; + DAT_PathFindingState::instance.maxClimbDataCount = 200; + DAT_EntityState::instance.maxEntityCount = 3000; + this->currentMoatCount = 16000; + DAT_LandscapeState::instance.maxTreeCount = 2000; + this->maxPitchDitchCount = 4000; + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::resetHeightAndMapBorders, this)(this->mapSize); + 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::TileMapState_Func::setChangedLayerToThreeAndMapping0x40x40, this)(); + this->forceUpdateLogicalAndMiscDisplayLayers = 1; + this->forceUpdateTextureTilemap = 1; + this->forceUpdateGFXLayers = 1; + this->forceUpdateMacroLayerFlag = 1; + this->field_0x55487c = 500; + + // every subsystem gets a chance to migrate what it loaded from an older map format + MACRO_CALL_MEMBER(OpenSHC::Map::Units::TribesState_Func::upgradeTribeArrayLayoutForMapVersion, + DAT_TribesState::ptr)((PackagedFileMagicNum)FilePackagerObj::instance.versionNumOfCurrentFileTypeUnk, + (PackagedFileMagicNum)FilePackagerObj::instance.packagerMapVersionNumUnk); + MACRO_CALL_MEMBER(OpenSHC::Map::Units::TroopValueState_Func::uselessFunction, DAT_TroopValueState::ptr)( + (PackagedFileMagicNum)FilePackagerObj::instance.versionNumOfCurrentFileTypeUnk, + (PackagedFileMagicNum)FilePackagerObj::instance.packagerMapVersionNumUnk); + MACRO_CALL_MEMBER(OpenSHC::Map::Units::UnitsState_Func::upgradeMapFormatForUnits, DAT_UnitsState::ptr)( + (PackagedFileMagicNum)FilePackagerObj::instance.versionNumOfCurrentFileTypeUnk, + (PackagedFileMagicNum)FilePackagerObj::instance.packagerMapVersionNumUnk); + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::upgradeMapFormatLogicLayer, DAT_TileMapState::ptr)( + (PackagedFileMagicNum)FilePackagerObj::instance.versionNumOfCurrentFileTypeUnk, + (PackagedFileMagicNum)FilePackagerObj::instance.packagerMapVersionNumUnk); + MACRO_CALL_MEMBER(OpenSHC::Map::Buildings::BuildingsState_Func::upgradeBuildingsForMapVersion, + DAT_BuildingsState::ptr)((PackagedFileMagicNum)FilePackagerObj::instance.versionNumOfCurrentFileTypeUnk, + (PackagedFileMagicNum)FilePackagerObj::instance.packagerMapVersionNumUnk); + MACRO_CALL_MEMBER(OpenSHC::Map::LandscapeState_Func::upgradeTreesAndRocksForMapVersion, + DAT_LandscapeState::ptr)((PackagedFileMagicNum)FilePackagerObj::instance.versionNumOfCurrentFileTypeUnk, + (PackagedFileMagicNum)FilePackagerObj::instance.packagerMapVersionNumUnk); + MACRO_CALL_MEMBER(OpenSHC::Map::Units::TribesState_Func::upgradeMapTribesState, DAT_TribesState::ptr)( + (PackagedFileMagicNum)FilePackagerObj::instance.versionNumOfCurrentFileTypeUnk, + (PackagedFileMagicNum)FilePackagerObj::instance.packagerMapVersionNumUnk); + MACRO_CALL_MEMBER(OpenSHC::Game::GameStateStructures_Func::migrateGameStateForMapVersion, DAT_GameState::ptr)( + (PackagedFileMagicNum)FilePackagerObj::instance.versionNumOfCurrentFileTypeUnk, + (PackagedFileMagicNum)FilePackagerObj::instance.packagerMapVersionNumUnk); + MACRO_CALL_MEMBER(OpenSHC::Map::Entities::EntityState_Func::handleMapVersionUpgrade, DAT_EntityState::ptr)( + (PackagedFileMagicNum)FilePackagerObj::instance.versionNumOfCurrentFileTypeUnk, + (PackagedFileMagicNum)FilePackagerObj::instance.packagerMapVersionNumUnk); + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::recountTotalOwnedMoats, this)(); + 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); + MACRO_CALL_MEMBER(OpenSHC::Map::Buildings::BuildingsState_Func::updatePathLinkageLayerForAllBuildings, + DAT_BuildingsState::ptr)(); + MACRO_CALL_MEMBER( + OpenSHC::Map::Navigation::PathFindingState_Func::updateClimbData, DAT_PathFindingState::ptr)(); + MACRO_CALL_MEMBER( + OpenSHC::Map::Buildings::BuildingsState_Func::clearBuildingValueWhenStateIsTwo, DAT_BuildingsState::ptr)(); + MACRO_CALL_MEMBER(OpenSHC::Map::LandscapeState_Func::setCurrentTimeOnSomeTrees, DAT_LandscapeState::ptr)(); + MACRO_CALL_MEMBER(OpenSHC::Map::LandscapeState_Func::resetWind, DAT_LandscapeState::ptr)(); + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::collectCliffEdgeTilesForClimbData, this)(); + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::clearInvalidMoatEntries, this)(); + MACRO_CALL_MEMBER( + OpenSHC::Game::GameStateStructures_Func::initializeGameStateAfterMapLoad, DAT_GameState::ptr)(); + MACRO_CALL_MEMBER(OpenSHC::Map::MapPropertiesState_Func::sortEventsByDate, DAT_MapPropertiesState::ptr)(); + MACRO_CALL_MEMBER(OpenSHC::Map::Buildings::BuildingsState_Func::applyVersionUpgradeAccessibilityRecompute, + DAT_BuildingsState::ptr)((PackagedFileMagicNum)FilePackagerObj::instance.versionNumOfCurrentFileTypeUnk, + (PackagedFileMagicNum)FilePackagerObj::instance.packagerMapVersionNumUnk); + MACRO_CALL_MEMBER( + OpenSHC::Map::Navigation::PathFindingState_Func::updateSeparateAreaTileMap, DAT_PathFindingState::ptr)(1); + MACRO_CALL_MEMBER(OpenSHC::Map::Buildings::BuildingsState_Func::updatePathLinkageLayerForAllBuildings, + DAT_BuildingsState::ptr)(); + + for (int grid = 0; grid < 40; grid++) { + MACRO_CALL_MEMBER(OpenSHC::Map::WildlifeState_Func::updateWildlifeGrid, DAT_WildlifeState::ptr)(grid); + } + MACRO_CALL_MEMBER(OpenSHC::Map::WildlifeState_Func::updateWildlife, DAT_WildlifeState::ptr)(); + MACRO_CALL_MEMBER(OpenSHC::Map::WildlifeState_Func::updateSection1034Info, DAT_WildlifeState::ptr)(); + MACRO_CALL_MEMBER(OpenSHC::Map::WildlifeState_Func::updateNofFpoints, DAT_WildlifeState::ptr)(); + MACRO_CALL(OpenSHC::Map_Func::ResetSomeValuesFunctionUnk)(); + DAT_WallAndPitchState::instance.countdown = 0; + MACRO_CALL_MEMBER(OpenSHC::UI::HoveredState_Func::clearHoveredState, DAT_HoveredState::ptr)(); + DAT_GameCore::instance.gamePausedLogical = 0xffffffff; + MACRO_CALL_MEMBER(OpenSHC::Game::GameStateStructures_Func::processGameTick, DAT_GameState::ptr)(); + MACRO_CALL_MEMBER( + OpenSHC::Map::Navigation::PathFindingState_Func::updateSeparateAreaTileMap, DAT_PathFindingState::ptr)(1); + 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); + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/processDamageToBuilding.cpp b/src/OpenSHC/Map/TileMapState/processDamageToBuilding.cpp new file mode 100644 index 0000000000..dab2a84be5 --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/processDamageToBuilding.cpp @@ -0,0 +1,536 @@ +#include "../TileMapState.func.hpp" + +#include "OpenSHC/AI/AICState.func.hpp" +#include "OpenSHC/Audio/MSS/SoundSystem.func.hpp" +#include "OpenSHC/Audio/SFX/SFXState.func.hpp" +#include "OpenSHC/Map/Buildings/BuildingsState.func.hpp" +#include "OpenSHC/Map/Entities.func.hpp" +#include "OpenSHC/Map/Entities/EntityState.func.hpp" +#include "OpenSHC/Map/Navigation/PathFindingState.func.hpp" +#include "OpenSHC/Map/Units/UnitsState.func.hpp" +#include "OpenSHC/Map/Version.func.hpp" +#include "OpenSHC/Random/RNG.func.hpp" +#include "OpenSHC/Commands/MappersEnum.hpp" +#include "OpenSHC/DE/SHCDE/eSFX.hpp" +#include "OpenSHC/Game/GameMode.hpp" +#include "OpenSHC/Map/Buildings/BuildingLogicalState.hpp" +#include "OpenSHC/Map/Buildings/BuildingType.hpp" +#include "OpenSHC/Map/Entities/EntityType.hpp" +#include "OpenSHC/Map/Units/States/UnitState.hpp" +#include "OpenSHC/WindowsHelper/Enums/BOOLEnum.hpp" + +#include "OpenSHC/Globals/DAT_AICState.hpp" +#include "OpenSHC/Globals/DAT_BuildingDefinedData.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_GameSynchronyState.hpp" +#include "OpenSHC/Globals/DAT_PathFindingState.hpp" +#include "OpenSHC/Globals/DAT_SFXState.hpp" +#include "OpenSHC/Globals/DAT_SoundSystemState.hpp" +#include "OpenSHC/Globals/DAT_TileMapState.hpp" +#include "OpenSHC/Globals/DAT_TroopValueState.hpp" +#include "OpenSHC/Globals/DAT_UnitsState.hpp" +#include "OpenSHC/Globals/SEC_RNG.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_UNKNOWN_WALL_RELATED; + using OpenSHC::Map::LogicHelpers::L_WALL_OR_GATEHOUSE; + + using OpenSHC::DE::SHCDE::eSFX; + using OpenSHC::Game::GameMode; + using OpenSHC::Map::Buildings::BuildingLogicalState; + using OpenSHC::Map::Entities::EntityType; + using OpenSHC::Map::Units::States::UnitState; + + // FUNCTION: STRONGHOLDCRUSADER 0x00516B80 + BOOLEnum TileMapState::processDamageToBuilding(int tile, uint xPosition, uint yPosition, int damageUnk, + int suppressDamage, int playerID, BOOLEnum aiBuildDelayRelated, int unitID) + { + int replaced = 0; + BOOLEnum volatile destroyed = FALSE; + // a negative player id is a request to spare that player's own team + BOOLEnum spareOwnTeam = FALSE; + if (playerID < 0) { + playerID = -playerID; + if (DAT_GameSynchronyState::instance.currentPlayerFullIDArray[playerID] == -1) { + spareOwnTeam = TRUE; + } + } + + uint logic = this->LogicLayer[tile]; + if ((logic & (L_BUILDING | L_KEEP_NON_MANOR_HOUSE)) != 0 || this->BuildingLayer[tile] != 0) { + int buildingID = this->BuildingLayer[tile]; + if (DAT_BuildingsState::instance.buildings[buildingID].logicalState == (BuildingLogicalState)0) { + return TRUE; + } + if (DAT_BuildingsState::instance.buildings[buildingID].logicalState + == OpenSHC::Map::Buildings::BLS_REMOVE) { + return TRUE; + } + if (DAT_BuildingsState::instance.buildings[buildingID].currentHealth == 0) { + return TRUE; + } + DAT_GameCore::instance.cowPoisonTrackerUnk += damageUnk * 10; + if (DAT_BuildingDefinedData::instance + .BuildingTypeHasHealth[(short)DAT_BuildingsState::instance.buildings[buildingID].buildingType] + == 0) { + return TRUE; + } + if ((this->LogicLayer[tile] & 0xf000000) != 0) { + return TRUE; + } + if (spareOwnTeam != FALSE + && DAT_GameState::instance.mapAndTime.playerTeams[playerID] + == DAT_GameState::instance.mapAndTime + .playerTeams[DAT_BuildingsState::instance.buildings[buildingID].owner]) { + return FALSE; + } + DAT_BuildingsState::instance.buildings[buildingID].currentHealth -= (short)damageUnk; + + if (aiBuildDelayRelated == TRUE + && (DAT_BuildingsState::instance.buildings[buildingID].buildingType + == OpenSHC::Map::Buildings::BT_GATEHOUSELARGE + || DAT_BuildingsState::instance.buildings[buildingID].buildingType + == OpenSHC::Map::Buildings::BT_GATEHOUSESMALL)) { + // a damaged gatehouse loses its drawbridges straight away + int bridge = MACRO_CALL_MEMBER(OpenSHC::Map::Buildings::BuildingsState_Func::findParticularBuilding, + DAT_BuildingsState::ptr)(DAT_BuildingsState::instance.buildings[buildingID].owner, + (short)DAT_BuildingsState::instance.buildings[buildingID].x, + (short)DAT_BuildingsState::instance.buildings[buildingID].y, + DAT_BuildingsState::instance.buildings[buildingID].widthOrHeight, + OpenSHC::Map::Buildings::BT_DRAWBRIDGE, 0); + if (bridge != 0) { + if (DAT_BuildingDefinedData::instance.BuildingShowRubbleWhenDestroyed + [(short)DAT_BuildingsState::instance.buildings[bridge].buildingType] + != 0) { + this->showNoRubbleWhenDestroyingBuilding = FALSE; + } else { + this->showNoRubbleWhenDestroyingBuilding = TRUE; + } + MACRO_CALL_MEMBER( + OpenSHC::Map::Buildings::BuildingsState_Func::destroyBuilding, DAT_BuildingsState::ptr)(bridge); + int second = MACRO_CALL_MEMBER(OpenSHC::Map::Buildings::BuildingsState_Func::findParticularBuilding, + DAT_BuildingsState::ptr)(DAT_BuildingsState::instance.buildings[buildingID].owner, + (short)DAT_BuildingsState::instance.buildings[buildingID].x, + (short)DAT_BuildingsState::instance.buildings[buildingID].y, + DAT_BuildingsState::instance.buildings[buildingID].widthOrHeight, + OpenSHC::Map::Buildings::BT_DRAWBRIDGE, bridge); + if (second != 0) { + this->showNoRubbleWhenDestroyingBuilding + = DAT_BuildingDefinedData::instance.BuildingShowRubbleWhenDestroyed + [(short)DAT_BuildingsState::instance.buildings[second].buildingType] + == 0; + MACRO_CALL_MEMBER(OpenSHC::Map::Buildings::BuildingsState_Func::destroyBuilding, + DAT_BuildingsState::ptr)(second); + } + } + } + + switch (DAT_BuildingsState::instance.buildings[buildingID].buildingType) { + case OpenSHC::Map::Buildings::BT_GATEHOUSELARGE: + case OpenSHC::Map::Buildings::BT_GATEHOUSESMALL: + 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: + DAT_GameState::instance.playerDataArray[DAT_BuildingsState::instance.buildings[buildingID].owner] + .defensesDamagedByPlayer = (short)playerID; + break; + default: + break; + } + if (unitID != 0) { + if (DAT_GameState::instance.mapAndTime + .playerTeams[DAT_BuildingsState::instance.buildings[buildingID].owner] + == DAT_GameState::instance.mapAndTime.playerTeams[playerID]) { + DAT_UnitsState::instance.units[unitID].friendlyFireCounterUnk += 1; + } else { + DAT_UnitsState::instance.units[unitID].friendlyFireCounterUnk = 0; + } + } + + short remaining = DAT_BuildingsState::instance.buildings[buildingID].currentHealth; + if (remaining <= 0) { + if (DAT_BuildingsState::instance.buildings[buildingID].owner + == DAT_GameSynchronyState::instance.currentPlayerSlotID) { + MACRO_CALL(OpenSHC::Map::Version_Func::UpdateDestroyedBuildingCountData)(1); + } + if (aiBuildDelayRelated == TRUE + && DAT_GameState::instance.playerDataArray[DAT_BuildingsState::instance.buildings[buildingID].owner] + .resourceRebuildDelay + == 0) { + DAT_GameState::instance.playerDataArray[DAT_BuildingsState::instance.buildings[buildingID].owner] + .resourceRebuildDelay = 1; + } + int owner = DAT_BuildingsState::instance.buildings[buildingID].owner; + int destroyedType = (short)DAT_BuildingsState::instance.buildings[buildingID].buildingType; + if (destroyedType != OpenSHC::Map::Buildings::BT_UNKNOWN3 + && (destroyedType <= 85 || destroyedType > 89)) { + DAT_GameSynchronyState::instance.finalResults.finalBuildingsBurned[playerID] += 1; + DAT_GameSynchronyState::instance.finalResults.finalBuildingsDestroyedWeighted[playerID] + += DAT_BuildingDefinedData::instance.BuildingDestroyedScoreWeight[destroyedType]; + DAT_GameSynchronyState::instance.finalResults.finalBuidingsDestroyed[owner] += 1; + DAT_GameState::instance.playerDataArray[owner].attackedByPlayerArrayUnk[playerID][1] += 1; + } + switch (DAT_BuildingsState::instance.buildings[buildingID].buildingType) { + case OpenSHC::Map::Buildings::BT_OILSMELTER: + case OpenSHC::Map::Buildings::BT_GATEHOUSELARGE: + case OpenSHC::Map::Buildings::BT_GATEHOUSESMALL: + 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: + MACRO_CALL_MEMBER(OpenSHC::AI::AICState_Func::playAnger2BikFromPlayerToPlayer, DAT_AICState::ptr)( + owner, playerID); + break; + default: + MACRO_CALL_MEMBER(OpenSHC::AI::AICState_Func::playVictory2BikFromPlayerToPlayer, DAT_AICState::ptr)( + owner, playerID); + } + if (DAT_GameSynchronyState::instance.currentGameMode != OpenSHC::Game::GM_SOLITARY + && playerID == DAT_GameSynchronyState::instance.currentPlayerSlotID + && DAT_GameState::instance.mapAndTime.playerTeams[owner] + != DAT_GameState::instance.mapAndTime + .playerTeams[DAT_GameSynchronyState::instance.currentPlayerSlotID] + && (DAT_BuildingsState::instance.buildings[buildingID].buildingType + == OpenSHC::Map::Buildings::BT_TOWER4 + || DAT_BuildingsState::instance.buildings[buildingID].buildingType + == OpenSHC::Map::Buildings::BT_TOWER5 + || DAT_BuildingsState::instance.buildings[buildingID].buildingType + == OpenSHC::Map::Buildings::BT_GATEHOUSELARGE + || DAT_BuildingsState::instance.buildings[buildingID].buildingType + == OpenSHC::Map::Buildings::BT_CHURCH + || DAT_BuildingsState::instance.buildings[buildingID].buildingType + == OpenSHC::Map::Buildings::BT_CATHEDRAL) + && MACRO_CALL_MEMBER( + OpenSHC::Audio::MSS::SoundSystem_Func::shouldSoundXNotBePlaying, DAT_SoundSystemState::ptr)() + == FALSE) { + MACRO_CALL_MEMBER(OpenSHC::Random::RNG_Func::nextRandomNumber1, SEC_RNG::ptr)(); + if (DAT_GameCore::instance.genieVoiceActive != FALSE) { + // "Watch it crumble" + MACRO_CALL_MEMBER(OpenSHC::Audio::SFX::SFXState_Func::playWAVSFX, DAT_SFXState::ptr)( + "Genie_36.wav"); + } + } + + switch (DAT_BuildingsState::instance.buildings[buildingID].buildingType) { + case OpenSHC::Map::Buildings::BT_TOWER1: { + MACRO_CALL_MEMBER(OpenSHC::Map::Buildings::BuildingsState_Func::processDamageToUnitsOnBuilding, + DAT_BuildingsState::ptr)(buildingID, 25); + int towerOwner = DAT_BuildingsState::instance.buildings[buildingID].owner; + int towerX = (short)DAT_BuildingsState::instance.buildings[buildingID].x; + DAT_BuildingsState::instance.buildings[buildingID].noRubble = 0; + int towerY = (short)DAT_BuildingsState::instance.buildings[buildingID].y; + MACRO_CALL_MEMBER(OpenSHC::Map::Buildings::BuildingsState_Func::deleteBuilding, + DAT_BuildingsState::ptr)(buildingID); + // a destroyed tower leaves its ruin behind + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::placeBuilding, this)( + towerOwner, towerX, towerY, OpenSHC::Commands::M_MAPPER_TOWER1_DESTROYED, 3, 15); + MACRO_CALL_MEMBER(OpenSHC::Audio::SFX::SFXState_Func::playSFXAtLocation, DAT_SFXState::ptr)( + towerX, towerY, OpenSHC::DE::SHCDE::FX_TOWER_SMASH); + replaced = 1; + destroyed = TRUE; + } break; + case OpenSHC::Map::Buildings::BT_TOWER2: { + MACRO_CALL_MEMBER(OpenSHC::Map::Buildings::BuildingsState_Func::processDamageToUnitsOnBuilding, + DAT_BuildingsState::ptr)(buildingID, 50); + int towerOwner = DAT_BuildingsState::instance.buildings[buildingID].owner; + int towerX = (short)DAT_BuildingsState::instance.buildings[buildingID].x; + DAT_BuildingsState::instance.buildings[buildingID].noRubble = 0; + int towerY = (short)DAT_BuildingsState::instance.buildings[buildingID].y; + MACRO_CALL_MEMBER(OpenSHC::Map::Buildings::BuildingsState_Func::deleteBuilding, + DAT_BuildingsState::ptr)(buildingID); + // a destroyed tower leaves its ruin behind + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::placeBuilding, this)( + towerOwner, towerX, towerY, OpenSHC::Commands::M_MAPPER_TOWER2_DESTROYED, 4, 15); + MACRO_CALL_MEMBER(OpenSHC::Audio::SFX::SFXState_Func::playSFXAtLocation, DAT_SFXState::ptr)( + towerX, towerY, OpenSHC::DE::SHCDE::FX_TOWER_SMASH); + replaced = 1; + destroyed = TRUE; + } break; + case OpenSHC::Map::Buildings::BT_TOWER3: { + MACRO_CALL_MEMBER(OpenSHC::Map::Buildings::BuildingsState_Func::processDamageToUnitsOnBuilding, + DAT_BuildingsState::ptr)(buildingID, 50); + int towerOwner = DAT_BuildingsState::instance.buildings[buildingID].owner; + int towerX = (short)DAT_BuildingsState::instance.buildings[buildingID].x; + DAT_BuildingsState::instance.buildings[buildingID].noRubble = 0; + int towerY = (short)DAT_BuildingsState::instance.buildings[buildingID].y; + MACRO_CALL_MEMBER(OpenSHC::Map::Buildings::BuildingsState_Func::deleteBuilding, + DAT_BuildingsState::ptr)(buildingID); + // a destroyed tower leaves its ruin behind + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::placeBuilding, this)( + towerOwner, towerX, towerY, OpenSHC::Commands::M_MAPPER_TOWER3_DESTROYED, 5, 15); + MACRO_CALL_MEMBER(OpenSHC::Audio::SFX::SFXState_Func::playSFXAtLocation, DAT_SFXState::ptr)( + towerX, towerY, OpenSHC::DE::SHCDE::FX_TOWER_SMASH); + replaced = 1; + destroyed = TRUE; + } break; + case OpenSHC::Map::Buildings::BT_TOWER4: { + MACRO_CALL_MEMBER(OpenSHC::Map::Buildings::BuildingsState_Func::processDamageToUnitsOnBuilding, + DAT_BuildingsState::ptr)(buildingID, 50); + int towerOwner = DAT_BuildingsState::instance.buildings[buildingID].owner; + int towerX = (short)DAT_BuildingsState::instance.buildings[buildingID].x; + DAT_BuildingsState::instance.buildings[buildingID].noRubble = 0; + int towerY = (short)DAT_BuildingsState::instance.buildings[buildingID].y; + MACRO_CALL_MEMBER(OpenSHC::Map::Buildings::BuildingsState_Func::deleteBuilding, + DAT_BuildingsState::ptr)(buildingID); + // a destroyed tower leaves its ruin behind + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::placeBuilding, this)( + towerOwner, towerX, towerY, OpenSHC::Commands::M_MAPPER_TOWER4_DESTROYED, 6, 15); + MACRO_CALL_MEMBER(OpenSHC::Audio::SFX::SFXState_Func::playSFXAtLocation, DAT_SFXState::ptr)( + towerX, towerY, OpenSHC::DE::SHCDE::FX_TOWER_SMASH); + replaced = 1; + destroyed = TRUE; + } break; + case OpenSHC::Map::Buildings::BT_TOWER5: { + MACRO_CALL_MEMBER(OpenSHC::Map::Buildings::BuildingsState_Func::processDamageToUnitsOnBuilding, + DAT_BuildingsState::ptr)(buildingID, 50); + int towerOwner = DAT_BuildingsState::instance.buildings[buildingID].owner; + int towerX = (short)DAT_BuildingsState::instance.buildings[buildingID].x; + DAT_BuildingsState::instance.buildings[buildingID].noRubble = 0; + int towerY = (short)DAT_BuildingsState::instance.buildings[buildingID].y; + MACRO_CALL_MEMBER(OpenSHC::Map::Buildings::BuildingsState_Func::deleteBuilding, + DAT_BuildingsState::ptr)(buildingID); + // a destroyed tower leaves its ruin behind + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::placeBuilding, this)( + towerOwner, towerX, towerY, OpenSHC::Commands::M_MAPPER_TOWER5_DESTROYED, 6, 15); + MACRO_CALL_MEMBER(OpenSHC::Audio::SFX::SFXState_Func::playSFXAtLocation, DAT_SFXState::ptr)( + towerX, towerY, OpenSHC::DE::SHCDE::FX_TOWER_SMASH); + replaced = 1; + destroyed = TRUE; + } break; + case OpenSHC::Map::Buildings::BT_OILSMELTER: { + int oilAmount = DAT_BuildingsState::instance.buildings[buildingID].resources[7]; + if (oilAmount == 0) { + // an empty smelter goes like any other building (the original shares that block) + goto destroyNormally; + } + // a smelter with oil in it goes up, and a full one throws burning oil about + int smelterX = (short)DAT_BuildingsState::instance.buildings[buildingID].x; + int smelterOwner = DAT_BuildingsState::instance.buildings[buildingID].owner; + uint smelterTile = DAT_BuildingsState::instance.buildings[buildingID].currentTilePositionAdjusted; + DAT_BuildingsState::instance.buildings[buildingID].noRubble = 0; + int smelterY = (short)DAT_BuildingsState::instance.buildings[buildingID].y; + MACRO_CALL_MEMBER(OpenSHC::Map::Buildings::BuildingsState_Func::deleteBuilding, + DAT_BuildingsState::ptr)(buildingID); + if (oilAmount <= 4) { + MACRO_CALL(OpenSHC::Map::Entities_Func::SetPlaceOnFire)(smelterOwner, smelterX * 8 + 16, + smelterY * 8 + 16, DAT_TileMapState::instance.HeightLayer[smelterTile], 3); + } else { + MACRO_CALL(OpenSHC::Map::Entities_Func::SetPlaceOnFire)(smelterOwner, smelterX * 8 + 16, + smelterY * 8 + 16, DAT_TileMapState::instance.HeightLayer[smelterTile], 5); + MACRO_CALL_MEMBER(OpenSHC::Map::Entities::EntityState_Func::spawnProjectileEntity, + DAT_EntityState::ptr)(0, smelterOwner, 0, smelterX * 8 + 16, smelterY * 8 + 16, + DAT_TileMapState::instance.HeightLayer[smelterTile], 0, 0, 0, + OpenSHC::Map::Entities::EntityTypeInt__ET_EXPLOSION, 0); + } + MACRO_CALL_MEMBER(OpenSHC::Audio::SFX::SFXState_Func::playSFXAtLocation, DAT_SFXState::ptr)( + smelterX, smelterY, OpenSHC::DE::SHCDE::FX_IGNITE_PITCH); + destroyed = TRUE; + } break; + case OpenSHC::Map::Buildings::BT_GATEHOUSELARGE: + case OpenSHC::Map::Buildings::BT_GATEHOUSESMALL: { + int bridge = MACRO_CALL_MEMBER(OpenSHC::Map::Buildings::BuildingsState_Func::findParticularBuilding, + DAT_BuildingsState::ptr)(DAT_BuildingsState::instance.buildings[buildingID].owner, + (short)DAT_BuildingsState::instance.buildings[buildingID].x, + (short)DAT_BuildingsState::instance.buildings[buildingID].y, + DAT_BuildingsState::instance.buildings[buildingID].widthOrHeight, + OpenSHC::Map::Buildings::BT_DRAWBRIDGE, 0); + if (bridge != 0) { + if (DAT_BuildingDefinedData::instance.BuildingShowRubbleWhenDestroyed + [(short)DAT_BuildingsState::instance.buildings[bridge].buildingType] + != 0) { + this->showNoRubbleWhenDestroyingBuilding = FALSE; + } else { + this->showNoRubbleWhenDestroyingBuilding = TRUE; + } + MACRO_CALL_MEMBER(OpenSHC::Map::Buildings::BuildingsState_Func::destroyBuilding, + DAT_BuildingsState::ptr)(bridge); + int second + = MACRO_CALL_MEMBER(OpenSHC::Map::Buildings::BuildingsState_Func::findParticularBuilding, + DAT_BuildingsState::ptr)(DAT_BuildingsState::instance.buildings[buildingID].owner, + (short)DAT_BuildingsState::instance.buildings[buildingID].x, + (short)DAT_BuildingsState::instance.buildings[buildingID].y, + DAT_BuildingsState::instance.buildings[buildingID].widthOrHeight, + OpenSHC::Map::Buildings::BT_DRAWBRIDGE, bridge); + if (second != 0) { + this->showNoRubbleWhenDestroyingBuilding + = DAT_BuildingDefinedData::instance.BuildingShowRubbleWhenDestroyed + [(short)DAT_BuildingsState::instance.buildings[second].buildingType] + == 0; + MACRO_CALL_MEMBER(OpenSHC::Map::Buildings::BuildingsState_Func::destroyBuilding, + DAT_BuildingsState::ptr)(second); + } + } + int gateX = (short)DAT_BuildingsState::instance.buildings[buildingID].x; + int gateY = (short)DAT_BuildingsState::instance.buildings[buildingID].y; + MACRO_CALL_MEMBER(OpenSHC::Map::Buildings::BuildingsState_Func::processDamageToUnitsOnBuilding, + DAT_BuildingsState::ptr)(buildingID, 50); + this->showNoRubbleWhenDestroyingBuilding + = DAT_BuildingDefinedData::instance.BuildingShowRubbleWhenDestroyed + [(short)DAT_BuildingsState::instance.buildings[buildingID].buildingType] + == 0; + MACRO_CALL_MEMBER(OpenSHC::Map::Buildings::BuildingsState_Func::destroyBuilding, + DAT_BuildingsState::ptr)(buildingID); + MACRO_CALL_MEMBER(OpenSHC::Audio::SFX::SFXState_Func::playSFXAtLocation, DAT_SFXState::ptr)( + gateX, gateY, OpenSHC::DE::SHCDE::FX_TOWER_SMASH); + destroyed = TRUE; + } break; + case OpenSHC::Map::Buildings::BT_TUNNEL: { + // the tunneller inside is buried where he stands, then the tunnel goes like any other building + int tunneller = DAT_BuildingsState::instance.buildings[buildingID].unitRefID; + if (DAT_UnitsState::instance.units[tunneller].state.generic + != (OpenSHC::Map::Units::States::US_STAND_UPUnk | OpenSHC::Map::Units::States::US_IDLEUnk)) { + DAT_UnitsState::instance.units[tunneller].totalSizeOfPathPlan + = DAT_UnitsState::instance.units[tunneller].currentIndexInPathPlan; + MACRO_CALL_MEMBER(OpenSHC::Map::Units::UnitsState_Func::applyTunnelDamageAlongPathPlan, + DAT_UnitsState::ptr)(tunneller); + } + DAT_UnitsState::instance.units[tunneller].state.generic = OpenSHC::Map::Units::States::US_DISAPPEAR; + DAT_UnitsState::instance.units[tunneller].disappearFadeAlphaCountdown = 32; + DAT_UnitsState::instance.units[tunneller].updateTickTracker = 32; + } + default: + destroyNormally: { + int rubbleType = (short)DAT_BuildingsState::instance.buildings[buildingID].buildingType; + int rubbleOwner = DAT_BuildingsState::instance.buildings[buildingID].owner; + this->showNoRubbleWhenDestroyingBuilding + = DAT_BuildingDefinedData::instance.BuildingShowRubbleWhenDestroyed[rubbleType] == 0; + MACRO_CALL_MEMBER(OpenSHC::Map::Buildings::BuildingsState_Func::destroyBuilding, + DAT_BuildingsState::ptr)(buildingID); + MACRO_CALL_MEMBER(OpenSHC::Audio::SFX::SFXState_Func::playSFXAtLocation, DAT_SFXState::ptr)( + (short)DAT_BuildingsState::instance.buildings[buildingID].x, + (short)DAT_BuildingsState::instance.buildings[buildingID].y, + OpenSHC::DE::SHCDE::FX_BUILDING_SMASH); + if (DAT_GameSynchronyState::instance.currentGameMode != OpenSHC::Game::GM_SOLITARY + && playerID == DAT_GameSynchronyState::instance.currentPlayerSlotID + && DAT_GameState::instance.mapAndTime.playerTeams[rubbleOwner] + != DAT_GameState::instance.mapAndTime + .playerTeams[DAT_GameSynchronyState::instance.currentPlayerSlotID] + && DAT_GameCore::instance.genieVoiceActive != FALSE) { + if (rubbleType == OpenSHC::Map::Buildings::BT_GRANARY + || rubbleType == OpenSHC::Map::Buildings::BT_ARMORY) { + // "Excellent" + MACRO_CALL_MEMBER(OpenSHC::Audio::SFX::SFXState_Func::playWAVSFX, DAT_SFXState::ptr)( + "Genie_23.wav"); + } + if (rubbleType == OpenSHC::Map::Buildings::BT_MERCENARYPOST + || rubbleType == OpenSHC::Map::Buildings::BT_BARRACKS) { + // "Bravo" + MACRO_CALL_MEMBER(OpenSHC::Audio::SFX::SFXState_Func::playWAVSFX, DAT_SFXState::ptr)( + "Genie_24.wav"); + } + } + destroyed = TRUE; + } break; + case OpenSHC::Map::Buildings::BT_SIEGETOWER_PLACED: { + int rider = DAT_BuildingsState::instance.buildings[buildingID].unitRefID; + if (rider != 0 + && DAT_BuildingsState::instance.buildings[buildingID].unitRefUID + == DAT_UnitsState::instance.units[rider].uid) { + DAT_UnitsState::instance.units[rider].state.generic = OpenSHC::Map::Units::States::US_DISAPPEAR; + DAT_UnitsState::instance.units[rider].disappearFadeAlphaCountdown = 0; + DAT_UnitsState::instance.units[rider].updateTickTracker = 0; + DAT_UnitsState::instance.units[rider].workplaceBuildingID_1 = 0; + } + DAT_BuildingsState::instance.buildings[buildingID].noRubble = 0; + MACRO_CALL_MEMBER(OpenSHC::Map::Buildings::BuildingsState_Func::deleteBuilding, + DAT_BuildingsState::ptr)(buildingID); + destroyed = TRUE; + } break; + } + } else if (DAT_BuildingsState::instance.buildings[buildingID].buildingType + == OpenSHC::Map::Buildings::BT_SIEGETOWER_PLACED) { + // a siege tower's rider takes the damage with it + int rider = DAT_BuildingsState::instance.buildings[buildingID].unitRefID; + if (rider != 0 + && DAT_BuildingsState::instance.buildings[buildingID].unitRefUID + == DAT_UnitsState::instance.units[rider].uid + && DAT_BuildingsState::instance.buildings[buildingID].maxHealth != 0) { + DAT_UnitsState::instance.units[rider].health + = ((int)remaining * DAT_UnitsState::instance.units[rider].maxHealth) + / (int)DAT_BuildingsState::instance.buildings[buildingID].maxHealth; + } + } + } else { + if ((logic & L_WALL_OR_GATEHOUSE) == 0) { + destroyed = TRUE; + } else { + if ((logic & L_STOCKPILEUnk) != 0) { + return FALSE; + } + int wallOwner = (this->WallOwnerLayer[tile] & 7) + 1; + if (spareOwnTeam != FALSE + && DAT_GameState::instance.mapAndTime.playerTeams[playerID] + == DAT_GameState::instance.mapAndTime.playerTeams[wallOwner]) { + return FALSE; + } + DAT_GameState::instance.playerDataArray[wallOwner].defensesDamagedByPlayer = (short)playerID; + if (unitID != 0) { + if (DAT_GameState::instance.mapAndTime.playerTeams[wallOwner] + == DAT_GameState::instance.mapAndTime.playerTeams[playerID]) { + DAT_UnitsState::instance.units[unitID].friendlyFireCounterUnk += 1; + } else { + DAT_UnitsState::instance.units[unitID].friendlyFireCounterUnk = 0; + } + } + // a wall standing alone takes half again the damage, a well connected one half + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::countLogicPropertyInSurroundingTiles, this)( + tile, yPosition, L_KEEP_NON_MANOR_HOUSE | L_WALL_OR_GATEHOUSE); + if ((int)this->DAT_CardinalTilesAroundTile <= 1) { + damageUnk += damageUnk / 2; + } else if ((int)this->DAT_CardinalTilesAroundTile > 2) { + damageUnk /= 2; + } + if (suppressDamage != 0) { + damageUnk = 0; + } + DAT_TroopValueState::instance.attackInfo.buildingDamageEventCount += 1; + DAT_GameCore::instance.cowPoisonTrackerUnk += damageUnk * 10; + if (suppressDamage == 0 && (this->LogicLayer[tile] & L_UNKNOWN_WALL_RELATED) == 0 + && this->DamageLayer[tile] == 0 && this->HeightLayer[tile] > this->DefaultHeightLayer[tile] + 60) { + this->HeightLayer[tile] -= 20; + } + for (int hit = 0; hit < damageUnk; hit++) { + if (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); + this->HeightLayer[tile] = this->DefaultHeightLayer[tile]; + this->DamageLayer[tile] = 0; + DAT_PathFindingState::instance.toggleUpdateSeparateAreaTileMap = 1; + DAT_TileMapState::instance.field_0x554a30 = 1; + destroyed = TRUE; + break; + } + this->HeightLayer[tile] -= 1; + if ((char)this->DamageLayer[tile] < 200) { + this->DamageLayer[tile] += 1; + } + } + MACRO_CALL_MEMBER( + OpenSHC::Map::Navigation::PathFindingState_Func::updatePathLinkagesInAllEightDirections, + DAT_PathFindingState::ptr)(yPosition, tile); + } + } + MACRO_CALL_MEMBER(OpenSHC::Map::Navigation::PathFindingState_Func::updateWalkAndPathLayer, + DAT_PathFindingState::ptr)(7, xPosition, yPosition); + BOOLEnum const result = destroyed; + return replaced != 0 ? FALSE : result; + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/processEntityDamageToBuildingCollateral.cpp b/src/OpenSHC/Map/TileMapState/processEntityDamageToBuildingCollateral.cpp new file mode 100644 index 0000000000..cc987da491 --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/processEntityDamageToBuildingCollateral.cpp @@ -0,0 +1,392 @@ +#include "../TileMapState.func.hpp" + +#include "OpenSHC/AI/AICState.func.hpp" +#include "OpenSHC/Audio/SFX/SFXState.func.hpp" +#include "OpenSHC/Map/Buildings/BuildingsState.func.hpp" +#include "OpenSHC/Map/Entities.func.hpp" +#include "OpenSHC/Map/Entities/EntityState.func.hpp" +#include "OpenSHC/Map/Navigation/PathFindingState.func.hpp" +#include "OpenSHC/Map/Units/UnitsState.func.hpp" +#include "OpenSHC/Commands/MappersEnum.hpp" +#include "OpenSHC/DE/SHCDE/eSFX.hpp" +#include "OpenSHC/Game/GameMode.hpp" +#include "OpenSHC/Map/Buildings/BuildingLogicalState.hpp" +#include "OpenSHC/Map/Buildings/BuildingType.hpp" +#include "OpenSHC/Map/Entities/EntityType.hpp" +#include "OpenSHC/Map/Units/States/UnitState.hpp" +#include "OpenSHC/WindowsHelper/Enums/BOOLEnum.hpp" + +#include "OpenSHC/Globals/DAT_AICState.hpp" +#include "OpenSHC/Globals/DAT_BuildingDefinedData.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_GameSynchronyState.hpp" +#include "OpenSHC/Globals/DAT_PathFindingState.hpp" +#include "OpenSHC/Globals/DAT_SFXState.hpp" +#include "OpenSHC/Globals/DAT_TileMapState.hpp" +#include "OpenSHC/Globals/DAT_UnitsState.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_UNKNOWN_WALL_RELATED; + using OpenSHC::Map::LogicHelpers::L_WALL_OR_GATEHOUSE; + + using OpenSHC::DE::SHCDE::eSFX; + using OpenSHC::Game::GameMode; + using OpenSHC::Map::Buildings::BuildingLogicalState; + using OpenSHC::Map::Entities::EntityType; + using OpenSHC::Map::Units::States::UnitState; + + // FUNCTION: STRONGHOLDCRUSADER 0x00517790 + void TileMapState::processEntityDamageToBuildingCollateral( + int tile, uint x, uint y, int damage, int playerID, undefined4 unused, int unitID) + { + int baseTile = tile; + uint baseY = y; + // a negative player id is a request to spare that player's own team + BOOLEnum spareOwnTeam = FALSE; + if (playerID < 0) { + playerID = -playerID; + if (DAT_GameSynchronyState::instance.currentPlayerFullIDArray[playerID] == -1) { + spareOwnTeam = TRUE; + } + } + + for (int index = 0; index < 9; index++) { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::getTileForBrush, this)( + 0, index, &tile, (int*)&y, baseTile, baseY); + int const brushTile = *(int volatile*)&tile; + int const logic = this->LogicLayer[brushTile]; + if ((logic & (L_BUILDING | L_KEEP_NON_MANOR_HOUSE)) == 0 && this->BuildingLayer[brushTile] == 0) { + // bare wall: grind its height down towards the ground it stands on + if ((logic & L_WALL_OR_GATEHOUSE) == 0 || (logic & L_STOCKPILEUnk) != 0) { + continue; + } + int wallOwner = (this->WallOwnerLayer[brushTile] & 7) + 1; + if (spareOwnTeam != FALSE + && DAT_GameState::instance.mapAndTime.playerTeams[playerID] + == DAT_GameState::instance.mapAndTime.playerTeams[wallOwner]) { + continue; + } + DAT_GameState::instance.playerDataArray[wallOwner].defensesDamagedByPlayer = (short)playerID; + if (unitID != 0) { + if (DAT_GameState::instance.mapAndTime.playerTeams[wallOwner] + == DAT_GameState::instance.mapAndTime.playerTeams[playerID]) { + DAT_UnitsState::instance.units[unitID].friendlyFireCounterUnk += 1; + } else { + DAT_UnitsState::instance.units[unitID].friendlyFireCounterUnk = 0; + } + } + DAT_GameCore::instance.cowPoisonTrackerUnk += damage * 10; + for (int hit = 0; hit < damage; hit++) { + if (*(byte volatile*)&this->HeightLayer[brushTile] <= this->DefaultHeightLayer[brushTile]) { + MACRO_CALL_MEMBER(OpenSHC::Map::Entities::EntityState_Func::destroyEntitiesOnTile, + DAT_EntityState::ptr)(brushTile); + this->LogicLayer[brushTile] &= ~( + L_WALL_OR_GATEHOUSE | L_CRENEL | L_STAIRS | L_UNKNOWN_WALL_RELATED | L_CRENEL_VARIATIONUnk); + byte& height = this->HeightLayer[brushTile]; + height = this->DefaultHeightLayer[brushTile]; + this->DamageLayer[brushTile] = 0; + MACRO_CALL_MEMBER( + OpenSHC::Map::Navigation::PathFindingState_Func::updatePathLinkagesInAllEightDirections, + DAT_PathFindingState::ptr)(y, brushTile); + DAT_PathFindingState::instance.toggleUpdateSeparateAreaTileMap = 1; + DAT_TileMapState::instance.field_0x554a30 = 1; + break; + } + this->HeightLayer[brushTile] -= 1; + char const& damageLevel = this->DamageLayer[brushTile]; + if (damageLevel < 200) { + this->DamageLayer[brushTile] += 1; + } + } + continue; + } + + int buildingID = this->BuildingLayer[brushTile]; + if (DAT_BuildingsState::instance.buildings[buildingID].logicalState == (BuildingLogicalState)0 + || DAT_BuildingsState::instance.buildings[buildingID].logicalState + == OpenSHC::Map::Buildings::BLS_REMOVE) { + continue; + } + if (DAT_BuildingsState::instance.buildings[buildingID].currentHealth == 0) { + continue; + } + int buildingType = (short)DAT_BuildingsState::instance.buildings[buildingID].buildingType; + if (DAT_BuildingDefinedData::instance.BuildingTypeHasHealth[buildingType] == 0) { + continue; + } + if ((logic & 0xf000000) != 0 || (logic & L_STOCKPILEUnk) != 0) { + continue; + } + if (spareOwnTeam != FALSE + && DAT_GameState::instance.mapAndTime.playerTeams[playerID] + == DAT_GameState::instance.mapAndTime + .playerTeams[DAT_BuildingsState::instance.buildings[buildingID].owner]) { + continue; + } + + DAT_BuildingsState::instance.buildings[buildingID].currentHealth -= damage; + DAT_GameCore::instance.cowPoisonTrackerUnk += damage * 10; + switch (buildingType) { + case OpenSHC::Map::Buildings::BT_GATEHOUSELARGE: + case OpenSHC::Map::Buildings::BT_GATEHOUSESMALL: + 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: + DAT_GameState::instance.playerDataArray[DAT_BuildingsState::instance.buildings[buildingID].owner] + .defensesDamagedByPlayer = (short)playerID; + } + if (unitID != 0) { + if (DAT_GameState::instance.mapAndTime + .playerTeams[DAT_BuildingsState::instance.buildings[buildingID].owner] + == DAT_GameState::instance.mapAndTime.playerTeams[playerID]) { + DAT_UnitsState::instance.units[unitID].friendlyFireCounterUnk += 1; + } else { + DAT_UnitsState::instance.units[unitID].friendlyFireCounterUnk = 0; + } + } + if (DAT_BuildingsState::instance.buildings[buildingID].currentHealth > 0) { + continue; + } + + if (buildingType != OpenSHC::Map::Buildings::BT_UNKNOWN3 && (buildingType <= 85 || buildingType > 89)) { + int owner = DAT_BuildingsState::instance.buildings[buildingID].owner; + DAT_GameSynchronyState::instance.finalResults.finalBuildingsBurned[playerID] += 1; + DAT_GameSynchronyState::instance.finalResults.finalBuidingsDestroyed[owner] += 1; + DAT_GameState::instance.playerDataArray[owner].attackedByPlayerArrayUnk[playerID][1] += 1; + } + switch (buildingType) { + case OpenSHC::Map::Buildings::BT_OILSMELTER: + case OpenSHC::Map::Buildings::BT_GATEHOUSELARGE: + case OpenSHC::Map::Buildings::BT_GATEHOUSESMALL: + 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: + MACRO_CALL_MEMBER(OpenSHC::AI::AICState_Func::playAnger2BikFromPlayerToPlayer, DAT_AICState::ptr)( + DAT_BuildingsState::instance.buildings[buildingID].owner, playerID); + break; + default: + MACRO_CALL_MEMBER(OpenSHC::AI::AICState_Func::playVictory2BikFromPlayerToPlayer, DAT_AICState::ptr)( + DAT_BuildingsState::instance.buildings[buildingID].owner, playerID); + } + + // the tower, smelter and gatehouse arms share the destroyed building's position + int destroyedX; + int destroyedY; + switch (DAT_BuildingsState::instance.buildings[buildingID].buildingType) { + case OpenSHC::Map::Buildings::BT_TOWER1: { + MACRO_CALL_MEMBER(OpenSHC::Map::Buildings::BuildingsState_Func::processDamageToUnitsOnBuilding, + DAT_BuildingsState::ptr)(buildingID, 25); + int towerOwner = DAT_BuildingsState::instance.buildings[buildingID].owner; + destroyedX = (short)DAT_BuildingsState::instance.buildings[buildingID].x; + DAT_BuildingsState::instance.buildings[buildingID].noRubble = 0; + destroyedY = (short)DAT_BuildingsState::instance.buildings[buildingID].y; + MACRO_CALL_MEMBER( + OpenSHC::Map::Buildings::BuildingsState_Func::deleteBuilding, DAT_BuildingsState::ptr)(buildingID); + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::placeBuilding, this)( + towerOwner, destroyedX, destroyedY, OpenSHC::Commands::M_MAPPER_TOWER1_DESTROYED, 3, 15); + } break; + case OpenSHC::Map::Buildings::BT_TOWER2: { + MACRO_CALL_MEMBER(OpenSHC::Map::Buildings::BuildingsState_Func::processDamageToUnitsOnBuilding, + DAT_BuildingsState::ptr)(buildingID, 50); + int towerOwner = DAT_BuildingsState::instance.buildings[buildingID].owner; + destroyedX = (short)DAT_BuildingsState::instance.buildings[buildingID].x; + DAT_BuildingsState::instance.buildings[buildingID].noRubble = 0; + destroyedY = (short)DAT_BuildingsState::instance.buildings[buildingID].y; + MACRO_CALL_MEMBER( + OpenSHC::Map::Buildings::BuildingsState_Func::deleteBuilding, DAT_BuildingsState::ptr)(buildingID); + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::placeBuilding, this)( + towerOwner, destroyedX, destroyedY, OpenSHC::Commands::M_MAPPER_TOWER2_DESTROYED, 4, 15); + } break; + case OpenSHC::Map::Buildings::BT_TOWER3: { + MACRO_CALL_MEMBER(OpenSHC::Map::Buildings::BuildingsState_Func::processDamageToUnitsOnBuilding, + DAT_BuildingsState::ptr)(buildingID, 50); + int towerOwner = DAT_BuildingsState::instance.buildings[buildingID].owner; + destroyedX = (short)DAT_BuildingsState::instance.buildings[buildingID].x; + DAT_BuildingsState::instance.buildings[buildingID].noRubble = 0; + destroyedY = (short)DAT_BuildingsState::instance.buildings[buildingID].y; + MACRO_CALL_MEMBER( + OpenSHC::Map::Buildings::BuildingsState_Func::deleteBuilding, DAT_BuildingsState::ptr)(buildingID); + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::placeBuilding, this)( + towerOwner, destroyedX, destroyedY, OpenSHC::Commands::M_MAPPER_TOWER3_DESTROYED, 5, 15); + } break; + case OpenSHC::Map::Buildings::BT_TOWER4: { + MACRO_CALL_MEMBER(OpenSHC::Map::Buildings::BuildingsState_Func::processDamageToUnitsOnBuilding, + DAT_BuildingsState::ptr)(buildingID, 50); + int towerOwner = DAT_BuildingsState::instance.buildings[buildingID].owner; + destroyedX = (short)DAT_BuildingsState::instance.buildings[buildingID].x; + DAT_BuildingsState::instance.buildings[buildingID].noRubble = 0; + destroyedY = (short)DAT_BuildingsState::instance.buildings[buildingID].y; + MACRO_CALL_MEMBER( + OpenSHC::Map::Buildings::BuildingsState_Func::deleteBuilding, DAT_BuildingsState::ptr)(buildingID); + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::placeBuilding, this)( + towerOwner, destroyedX, destroyedY, OpenSHC::Commands::M_MAPPER_TOWER4_DESTROYED, 6, 15); + } break; + case OpenSHC::Map::Buildings::BT_TOWER5: { + MACRO_CALL_MEMBER(OpenSHC::Map::Buildings::BuildingsState_Func::processDamageToUnitsOnBuilding, + DAT_BuildingsState::ptr)(buildingID, 50); + int towerOwner = DAT_BuildingsState::instance.buildings[buildingID].owner; + destroyedX = (short)DAT_BuildingsState::instance.buildings[buildingID].x; + DAT_BuildingsState::instance.buildings[buildingID].noRubble = 0; + destroyedY = (short)DAT_BuildingsState::instance.buildings[buildingID].y; + MACRO_CALL_MEMBER( + OpenSHC::Map::Buildings::BuildingsState_Func::deleteBuilding, DAT_BuildingsState::ptr)(buildingID); + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::placeBuilding, this)( + towerOwner, destroyedX, destroyedY, OpenSHC::Commands::M_MAPPER_TOWER5_DESTROYED, 6, 15); + } break; + case OpenSHC::Map::Buildings::BT_OILSMELTER: { + int oilAmount = DAT_BuildingsState::instance.buildings[buildingID].resources[7]; + if (oilAmount == 0) { + // an empty smelter goes like any other building (the original shares that block) + goto destroyNormally; + } + // a smelter with oil in it goes up, and a full one throws burning oil about + int smelterOwner = DAT_BuildingsState::instance.buildings[buildingID].owner; + uint smelterTile = DAT_BuildingsState::instance.buildings[buildingID].currentTilePositionAdjusted; + destroyedX = (short)DAT_BuildingsState::instance.buildings[buildingID].x; + DAT_BuildingsState::instance.buildings[buildingID].noRubble = 0; + destroyedY = (short)DAT_BuildingsState::instance.buildings[buildingID].y; + MACRO_CALL_MEMBER( + OpenSHC::Map::Buildings::BuildingsState_Func::deleteBuilding, DAT_BuildingsState::ptr)(buildingID); + if (oilAmount <= 4) { + MACRO_CALL(OpenSHC::Map::Entities_Func::SetPlaceOnFire)(smelterOwner, destroyedX * 8 + 16, + destroyedY * 8 + 16, DAT_TileMapState::instance.HeightLayer[smelterTile], 3); + } else { + MACRO_CALL(OpenSHC::Map::Entities_Func::SetPlaceOnFire)(smelterOwner, destroyedX * 8 + 16, + destroyedY * 8 + 16, DAT_TileMapState::instance.HeightLayer[smelterTile], 5); + MACRO_CALL_MEMBER(OpenSHC::Map::Entities::EntityState_Func::spawnProjectileEntity, + DAT_EntityState::ptr)(0, smelterOwner, 0, destroyedX * 8 + 16, destroyedY * 8 + 16, + DAT_TileMapState::instance.HeightLayer[smelterTile], 0, 0, 0, + OpenSHC::Map::Entities::EntityTypeInt__ET_EXPLOSION, 0); + } + MACRO_CALL_MEMBER(OpenSHC::Audio::SFX::SFXState_Func::playSFXAtLocation, DAT_SFXState::ptr)( + destroyedX, destroyedY, OpenSHC::DE::SHCDE::FX_IGNITE_PITCH); + } break; + case OpenSHC::Map::Buildings::BT_GATEHOUSELARGE: + case OpenSHC::Map::Buildings::BT_GATEHOUSESMALL: { + // a gatehouse takes its drawbridges with it + int bridge = MACRO_CALL_MEMBER(OpenSHC::Map::Buildings::BuildingsState_Func::findParticularBuilding, + DAT_BuildingsState::ptr)(DAT_BuildingsState::instance.buildings[buildingID].owner, + (short)DAT_BuildingsState::instance.buildings[buildingID].x, + (short)DAT_BuildingsState::instance.buildings[buildingID].y, + DAT_BuildingsState::instance.buildings[buildingID].widthOrHeight, + OpenSHC::Map::Buildings::BT_DRAWBRIDGE, 0); + if (bridge != 0) { + if (DAT_BuildingDefinedData::instance.BuildingShowRubbleWhenDestroyed + [(short)DAT_BuildingsState::instance.buildings[bridge].buildingType] + != 0) { + this->showNoRubbleWhenDestroyingBuilding = FALSE; + } else { + this->showNoRubbleWhenDestroyingBuilding = TRUE; + } + MACRO_CALL_MEMBER( + OpenSHC::Map::Buildings::BuildingsState_Func::destroyBuilding, DAT_BuildingsState::ptr)(bridge); + int second = MACRO_CALL_MEMBER(OpenSHC::Map::Buildings::BuildingsState_Func::findParticularBuilding, + DAT_BuildingsState::ptr)(DAT_BuildingsState::instance.buildings[buildingID].owner, + (short)DAT_BuildingsState::instance.buildings[buildingID].x, + (short)DAT_BuildingsState::instance.buildings[buildingID].y, + DAT_BuildingsState::instance.buildings[buildingID].widthOrHeight, + OpenSHC::Map::Buildings::BT_DRAWBRIDGE, bridge); + if (second != 0) { + this->showNoRubbleWhenDestroyingBuilding + = DAT_BuildingDefinedData::instance.BuildingShowRubbleWhenDestroyed + [(short)DAT_BuildingsState::instance.buildings[second].buildingType] + == 0; + MACRO_CALL_MEMBER(OpenSHC::Map::Buildings::BuildingsState_Func::destroyBuilding, + DAT_BuildingsState::ptr)(second); + } + } + destroyedX = (short)DAT_BuildingsState::instance.buildings[buildingID].x; + destroyedY = (short)DAT_BuildingsState::instance.buildings[buildingID].y; + MACRO_CALL_MEMBER(OpenSHC::Map::Buildings::BuildingsState_Func::processDamageToUnitsOnBuilding, + DAT_BuildingsState::ptr)(buildingID, 50); + this->showNoRubbleWhenDestroyingBuilding + = DAT_BuildingDefinedData::instance.BuildingShowRubbleWhenDestroyed + [(short)DAT_BuildingsState::instance.buildings[buildingID].buildingType] + == 0; + MACRO_CALL_MEMBER( + OpenSHC::Map::Buildings::BuildingsState_Func::destroyBuilding, DAT_BuildingsState::ptr)(buildingID); + MACRO_CALL_MEMBER(OpenSHC::Audio::SFX::SFXState_Func::playSFXAtLocation, DAT_SFXState::ptr)( + destroyedX, destroyedY, OpenSHC::DE::SHCDE::FX_TOWER_SMASH); + } break; + case OpenSHC::Map::Buildings::BT_TUNNEL: { + // the tunneller inside is buried where he stands, then the tunnel goes like any other building + int tunneller = DAT_BuildingsState::instance.buildings[buildingID].unitRefID; + if (DAT_UnitsState::instance.units[tunneller].state.generic + != (OpenSHC::Map::Units::States::US_STAND_UPUnk | OpenSHC::Map::Units::States::US_IDLEUnk)) { + DAT_UnitsState::instance.units[tunneller].totalSizeOfPathPlan + = DAT_UnitsState::instance.units[tunneller].currentIndexInPathPlan; + MACRO_CALL_MEMBER(OpenSHC::Map::Units::UnitsState_Func::applyTunnelDamageAlongPathPlan, + DAT_UnitsState::ptr)(tunneller); + } + DAT_UnitsState::instance.units[tunneller].state.generic = OpenSHC::Map::Units::States::US_DISAPPEAR; + DAT_UnitsState::instance.units[tunneller].disappearFadeAlphaCountdown = 32; + DAT_UnitsState::instance.units[tunneller].updateTickTracker = 32; + } + default: + destroyNormally: { + int destroyedType = (short)DAT_BuildingsState::instance.buildings[buildingID].buildingType; + int destroyedOwner = DAT_BuildingsState::instance.buildings[buildingID].owner; + this->showNoRubbleWhenDestroyingBuilding + = DAT_BuildingDefinedData::instance.BuildingShowRubbleWhenDestroyed[destroyedType] == 0; + MACRO_CALL_MEMBER( + OpenSHC::Map::Buildings::BuildingsState_Func::destroyBuilding, DAT_BuildingsState::ptr)(buildingID); + MACRO_CALL_MEMBER(OpenSHC::Audio::SFX::SFXState_Func::playSFXAtLocation, DAT_SFXState::ptr)( + (short)DAT_BuildingsState::instance.buildings[buildingID].x, + (short)DAT_BuildingsState::instance.buildings[buildingID].y, OpenSHC::DE::SHCDE::FX_BUILDING_SMASH); + if (DAT_GameSynchronyState::instance.currentGameMode != OpenSHC::Game::GM_SOLITARY + && playerID == DAT_GameSynchronyState::instance.currentPlayerSlotID + && DAT_GameState::instance.mapAndTime.playerTeams[destroyedOwner] + != DAT_GameState::instance.mapAndTime + .playerTeams[DAT_GameSynchronyState::instance.currentPlayerSlotID] + && DAT_GameCore::instance.genieVoiceActive != FALSE) { + if (destroyedType == OpenSHC::Map::Buildings::BT_GRANARY + || destroyedType == OpenSHC::Map::Buildings::BT_ARMORY) { + // "Excellent" + MACRO_CALL_MEMBER(OpenSHC::Audio::SFX::SFXState_Func::playWAVSFX, DAT_SFXState::ptr)( + "Genie_23.wav"); + } + if (destroyedType == OpenSHC::Map::Buildings::BT_MERCENARYPOST + || destroyedType == OpenSHC::Map::Buildings::BT_BARRACKS) { + // "Bravo" + MACRO_CALL_MEMBER(OpenSHC::Audio::SFX::SFXState_Func::playWAVSFX, DAT_SFXState::ptr)( + "Genie_24.wav"); + } + } + } break; + case OpenSHC::Map::Buildings::BT_SIEGETOWER_PLACED: { + int rider = DAT_BuildingsState::instance.buildings[buildingID].unitRefID; + if (rider != 0 + && DAT_BuildingsState::instance.buildings[buildingID].unitRefUID + == DAT_UnitsState::instance.units[rider].uid) { + DAT_UnitsState::instance.units[rider].state.generic = OpenSHC::Map::Units::States::US_DISAPPEAR; + DAT_UnitsState::instance.units[rider].disappearFadeAlphaCountdown = 0; + DAT_UnitsState::instance.units[rider].updateTickTracker = 0; + DAT_UnitsState::instance.units[rider].workplaceBuildingID_1 = 0; + } + DAT_BuildingsState::instance.buildings[buildingID].noRubble = 0; + MACRO_CALL_MEMBER( + OpenSHC::Map::Buildings::BuildingsState_Func::deleteBuilding, DAT_BuildingsState::ptr)(buildingID); + } break; + } + } + MACRO_CALL_MEMBER(OpenSHC::Map::Navigation::PathFindingState_Func::updateWalkAndPathLayer, + DAT_PathFindingState::ptr)(9, x, baseY); + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/propagateCliffEdgeFlagFromNeighbor.cpp b/src/OpenSHC/Map/TileMapState/propagateCliffEdgeFlagFromNeighbor.cpp new file mode 100644 index 0000000000..1bbc2b5347 --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/propagateCliffEdgeFlagFromNeighbor.cpp @@ -0,0 +1,51 @@ +#include "../TileMapState.func.hpp" + +#include "OpenSHC/Globals/DAT_TerrainDefinedData.hpp" +#include "OpenSHC/Globals/DAT_ViewportRenderState.hpp" + +namespace OpenSHC { +namespace Map { + + using OpenSHC::Map::LogicHelpers::L2_OASIS_GRASS; + using OpenSHC::Map::LogicHelpers::L2_STONES_OR_DRIVEN_SANDUnk; + using OpenSHC::Map::LogicHelpers::L2_THICK_SCRUB; + using OpenSHC::Map::LogicHelpers::L_RIVER; + + // FUNCTION: STRONGHOLDCRUSADER 0x004FC280 + void TileMapState::propagateCliffEdgeFlagFromNeighbor(int tile, int x, int y) + { + int const height = this->HeightLayer[tile]; + if (this->Logic2Layer[tile] != 0) { + return; + } + + for (int index = 0; index < countof(DAT_TerrainDefinedData::instance.field_0x372c); index++) { + XYPair offset; + offset.x = DAT_TerrainDefinedData::instance.field_0x372c[index].x; + offset.y = DAT_TerrainDefinedData::instance.field_0x372c[index].y; + uint const neighbourY = offset.y + y; + uint const neighbourX = offset.x + x; + if (neighbourX > 399 || neighbourY > 399) { + continue; + } + if (DAT_ViewportRenderState::instance.DAT_BinaryTileMap400x400[neighbourY * 400 + neighbourX] == 0) { + continue; + } + int const neighbour + = DAT_ViewportRenderState::instance.translationMatrix[neighbourY].addXgetTile + offset.x + x; + if ((this->LogicLayer[neighbour] & L_RIVER) == 0) { + continue; + } + if ((this->Logic2Layer[neighbour] & (L2_OASIS_GRASS | L2_STONES_OR_DRIVEN_SANDUnk)) == 0) { + continue; + } + if (this->HeightLayer[neighbour] < height) { + continue; + } + this->Logic2Layer[tile] = L2_THICK_SCRUB; + return; + } + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/raiseLand.cpp b/src/OpenSHC/Map/TileMapState/raiseLand.cpp new file mode 100644 index 0000000000..8d6eb8db08 --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/raiseLand.cpp @@ -0,0 +1,93 @@ +#include "../TileMapState.func.hpp" + +#include "OpenSHC/Map/Navigation/PathFindingState.func.hpp" + +#include "OpenSHC/Globals/DAT_PathFindingState.hpp" +#include "OpenSHC/Globals/DAT_TerrainDefinedData.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_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_RIVER; + using OpenSHC::Map::LogicHelpers::L_ROCKY; + using OpenSHC::Map::LogicHelpers::L_SEA; + using OpenSHC::Map::LogicHelpers::L_WALL_OR_GATEHOUSE; + + // FUNCTION: STRONGHOLDCRUSADER 0x00501D90 + void TileMapState::raiseLand(int tile, uint y, uint brush, int change) + { + int const baseTile = tile; + int const brushSize = DAT_TerrainDefinedData::instance.BrushSizeArray[brush]; + uint const baseY = y; + for (int index = 0; index < brushSize; index++) { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::getTileForBrush, this)( + 1, index, &tile, (int*)&y, baseTile, baseY); + int const brushTile = *(int volatile*)&tile; + uint const logic = this->LogicLayer[brushTile]; + if ((logic & (L_BORDER | L_BORDER_EDGE)) != 0) { + continue; + } + if ((logic & (L_SEA | L_MARSH)) != 0 && this->editorAffectType == 0) { + continue; + } + if ((logic & (L_SEA | L_MARSH)) == 0 && this->editorAffectType == 1) { + continue; + } + if ((logic & (L_WALL_OR_GATEHOUSE | L_BUILDING | L_MOAT_DUG_OR_PLANNED | L_KEEP_NON_MANOR_HOUSE | L_MOAT)) + != 0) { + continue; + } + if (this->BuildingLayer[brushTile] != 0) { + continue; + } + int minHeight; + int maxHeight; + if ((logic & L_SEA) != 0) { + minHeight = 0; + maxHeight = 0; + } else if ((logic & (L_SEA | L_MARSH | L_MOAT)) != 0) { + minHeight = 0; + maxHeight = 20; + } else { + minHeight = 8; + maxHeight = 128; + } + *(uint volatile*)&this->LogicLayer[brushTile] &= ~L_ROCKY; + if ((this->LogicLayer[brushTile] & L_RIVER) != 0) { + this->HeightLayer[brushTile] += 8; + } + this->ChangedLayer[brushTile] = 2; + byte const& height = this->HeightLayer[brushTile]; + if (height <= minHeight) { + this->HeightLayer[brushTile] = (byte)minHeight; + } + if (height > 0 || change >= 0) { + this->HeightLayer[brushTile] += (char)change; + this->Logic2Layer[brushTile] = 0; + } + if (height < minHeight) { + this->HeightLayer[brushTile] = (byte)minHeight; + } + if (height > maxHeight) { + this->HeightLayer[brushTile] = (byte)maxHeight; + } + this->DefaultHeightLayer[brushTile] = height; + if ((this->LogicLayer[brushTile] & L_RIVER) != 0) { + this->HeightLayer[brushTile] -= 8; + } + MACRO_CALL_MEMBER(OpenSHC::Map::Navigation::PathFindingState_Func::updatePathLinkagesInAllEightDirections, + DAT_PathFindingState::ptr)(y, brushTile); + } + this->forceUpdateTextureTilemap = 1; + this->forceUpdateLogicalAndMiscDisplayLayers = 1; + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/reassignWallOwnershipForPlayer.cpp b/src/OpenSHC/Map/TileMapState/reassignWallOwnershipForPlayer.cpp new file mode 100644 index 0000000000..ca78035fb9 --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/reassignWallOwnershipForPlayer.cpp @@ -0,0 +1,30 @@ +#include "../TileMapState.func.hpp" + +#include "OpenSHC/Globals/DAT_BuildingsState.hpp" + +namespace OpenSHC { +namespace Map { + + using OpenSHC::Map::LogicHelpers::L_WALL_OR_GATEHOUSE; + + // FUNCTION: STRONGHOLDCRUSADER 0x004F9000 + void TileMapState::reassignWallOwnershipForPlayer(int playerID, int newOwner) + { + for (int tile = 0; tile < countof(this->LogicLayer); ++tile) { + if ((this->LogicLayer[tile] & L_WALL_OR_GATEHOUSE) == 0) { + continue; + } + int const building = this->BuildingLayer[tile]; + if (building != 0) { + if (DAT_BuildingsState::instance.buildings[building].owner != newOwner) { + continue; + } + } else if ((this->WallOwnerLayer[tile] & 7) + 1 != playerID) { + continue; + } + this->WallOwnerLayer[tile] = (this->WallOwnerLayer[tile] & 0xf8) | (newOwner - 1); + } + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/renderPreviewMapperWithBrush.cpp b/src/OpenSHC/Map/TileMapState/renderPreviewMapperWithBrush.cpp new file mode 100644 index 0000000000..0a09b9380c --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/renderPreviewMapperWithBrush.cpp @@ -0,0 +1,181 @@ +#include "../TileMapState.func.hpp" + +#include "OpenSHC/Map/Navigation/PathFindingState.func.hpp" +#include "OpenSHC/Commands/MappersEnum.hpp" +#include "OpenSHC/Game/GameMode.hpp" +#include "OpenSHC/Game/GameMode2.hpp" +#include "OpenSHC/WindowsHelper/Enums/BOOLEnum.hpp" + +#include "OpenSHC/Globals/DAT_GameCore.hpp" +#include "OpenSHC/Globals/DAT_GameState.hpp" +#include "OpenSHC/Globals/DAT_GameSynchronyState.hpp" +#include "OpenSHC/Globals/DAT_PathFindingState.hpp" +#include "OpenSHC/Globals/DAT_TerrainDefinedData.hpp" +#include "OpenSHC/Globals/DAT_ViewportRenderState.hpp" + +namespace OpenSHC { +namespace Map { + + using OpenSHC::Game::GameMode; + using OpenSHC::Game::GameMode2; + + // FUNCTION: STRONGHOLDCRUSADER 0x00508A00 + void TileMapState::renderPreviewMapperWithBrush(uint x, uint y, MappersEnum mapper) + { + if (x > 399 || y > 399) { + return; + } + if (DAT_ViewportRenderState::instance.DAT_BinaryTileMap400x400[y * 400 + x] == 0) { + return; + } + + int brushSize = DAT_TerrainDefinedData::instance.BrushSizeArray[this->editorActiveBrush]; + int const baseTile = DAT_ViewportRenderState::instance.translationMatrix[y].addXgetTile + x; + this->buildingPlacementFail = FALSE; + if (mapper == OpenSHC::Commands::M_MAPPER_POND1 || mapper == OpenSHC::Commands::M_MAPPER_POND2_SMALL + || mapper == OpenSHC::Commands::M_MAPPER_POND3_LARGE1 + || mapper == OpenSHC::Commands::M_MAPPER_POND4_LARGE2) { + return; + } + this->buildingPlacementFail = TRUE; + if (mapper == OpenSHC::Commands::M_MAPPER_SIGNPOST) { + return; + } + + // every mapper draws its own preview colour + ushort gfx; + switch (mapper) { + case OpenSHC::Commands::M_MAPPER_SEA: + case OpenSHC::Commands::M_MAPPER_SHALLOWS: + case OpenSHC::Commands::M_MAPPER_RIVER: + case OpenSHC::Commands::M_MAPPER_MARSH: + case OpenSHC::Commands::M_MAPPER_DUGMOAT: + case OpenSHC::Commands::M_MAPPER_MOAT: + gfx = 57; + break; + case OpenSHC::Commands::M_MAPPER_BEACH: + case OpenSHC::Commands::M_MAPPER_FORD: + gfx = 58; + break; + case OpenSHC::Commands::M_MAPPER_ROCKY: + case OpenSHC::Commands::M_MAPPER_STONES: + case OpenSHC::Commands::M_MAPPER_BOULDERS: + case OpenSHC::Commands::M_MAPPER_PEBBLES: + case OpenSHC::Commands::M_MAPPER_IRON: + gfx = 59; + break; + case OpenSHC::Commands::M_MAPPER_UNDUGMOAT: + case OpenSHC::Commands::M_MAPPER_OIL: + gfx = 61; + break; + case OpenSHC::Commands::M_MAPPER_CHESTNUT: + case OpenSHC::Commands::M_MAPPER_OAK: + case OpenSHC::Commands::M_MAPPER_PINE: + case OpenSHC::Commands::M_MAPPER_BIRCH: + case OpenSHC::Commands::M_MAPPER_SHRUB1A: + case OpenSHC::Commands::M_MAPPER_SHRUB1B: + case OpenSHC::Commands::M_MAPPER_SHRUB1C: + case OpenSHC::Commands::M_MAPPER_SHRUB1D: + case OpenSHC::Commands::M_MAPPER_SHRUB1E: + case OpenSHC::Commands::M_MAPPER_SHRUB2A: + case OpenSHC::Commands::M_MAPPER_SHRUB2B: + case OpenSHC::Commands::M_MAPPER_SHRUB2C: + case OpenSHC::Commands::M_MAPPER_SHRUB2D: + case OpenSHC::Commands::M_MAPPER_SHRUB2E: + case OpenSHC::Commands::M_MAPPER_SHRUB3A: + case OpenSHC::Commands::M_MAPPER_SHRUB3B: + case OpenSHC::Commands::M_MAPPER_SHRUB3C: + case OpenSHC::Commands::M_MAPPER_SHRUB3D: + case OpenSHC::Commands::M_MAPPER_DEER: + case OpenSHC::Commands::M_MAPPER_LION: + case OpenSHC::Commands::M_MAPPER_RABBIT: + case OpenSHC::Commands::M_MAPPER_CAMEL: + case OpenSHC::Commands::M_MAPPER_CROW_SEAGULL: + case OpenSHC::Commands::M_MAPPER_SEAGULL: + // a single organism is placed on one tile, whatever the brush size + brushSize = 1; + case OpenSHC::Commands::M_MAPPER_RAISE: + case OpenSHC::Commands::M_MAPPER_LOWER: + case OpenSHC::Commands::M_MAPPER_EQUALISE: + case OpenSHC::Commands::M_MAPPER_MOUNTAIN: + case OpenSHC::Commands::M_MAPPER_HILL: + case OpenSHC::Commands::M_MAPPER_DELETE: + gfx = 62; + break; + case OpenSHC::Commands::M_MAPPER_SCRUB: + case OpenSHC::Commands::M_MAPPER_DIRT: + case OpenSHC::Commands::M_MAPPER_DUNES: + gfx = 60; + break; + default: + gfx = 56; + } + + switch (mapper) { + case OpenSHC::Commands::M_MAPPER_UNDUGMOAT: + case OpenSHC::Commands::M_MAPPER_DUGMOAT: + case OpenSHC::Commands::M_MAPPER_MOAT: + case OpenSHC::Commands::M_MAPPER_ANTIMOAT: + // a moat may not be dug too near an enemy, a signpost, or an opponent's buildings + if (DAT_GameCore::instance.gameMode_2 != OpenSHC::Game::GM_EDITOR + && DAT_GameCore::instance.gameMode_2 != OpenSHC::Game::GM_SIEGE_THAT) { + int enemyRange; + int searchRange; + if (DAT_GameSynchronyState::instance.currentGameMode != OpenSHC::Game::GM_SOLITARY) { + enemyRange = 15; + searchRange = 7; + } else { + enemyRange = 30; + searchRange = 30; + } + if (MACRO_CALL_MEMBER( + OpenSHC::Map::Navigation::PathFindingState_Func::isEnemyTooCloseUnk, DAT_PathFindingState::ptr)( + DAT_GameSynchronyState::instance.currentPlayerSlotID, x, y, enemyRange) + != FALSE) { + gfx = 62; + } + if (MACRO_CALL_MEMBER(OpenSHC::Map::Navigation::PathFindingState_Func::isSignPostWithinDistance, + DAT_PathFindingState::ptr)(x, y, DAT_GameState::instance.mapAndTime.unk_signpostDistance + 5) + != FALSE) { + gfx = 62; + } + if (DAT_GameSynchronyState::instance.currentGameMode != OpenSHC::Game::GM_SOLITARY) { + int const buildRange + = MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::getCastleBuildRangeForMapSize, this)(); + if (MACRO_CALL_MEMBER(OpenSHC::Map::Navigation::PathFindingState_Func::isOpponentBuildingInRange, + DAT_PathFindingState::ptr)(DAT_GameSynchronyState::instance.currentPlayerSlotID, x, y, + searchRange, -1, -1, buildRange + 5) + != 0) { + gfx = 62; + } + } + } + } + + // the brush walk reuses x as the tile it writes back + *(int volatile*)&x = DAT_ViewportRenderState::instance.translationMatrix[y].addXgetTile + x; + { + uint brushY; + if (mapper != OpenSHC::Commands::M_MAPPER_DUGMOAT && mapper != OpenSHC::Commands::M_MAPPER_UNDUGMOAT + && mapper != OpenSHC::Commands::M_MAPPER_MOAT && mapper != OpenSHC::Commands::M_MAPPER_ANTIMOAT) { + brushY = y; + for (int index = 0; index < brushSize; index++) { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::getTileForBrush, this)( + 1, index, (int*)&x, (int*)&brushY, baseTile, y); + this->ConstructionGFXLayer[x] = gfx; + } + } else { + // a moat preview always covers the nine tiles around the cursor + int index = 0; + brushY = y; + for (; index < 9; index++) { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::getTileForBrush, this)( + 0, index, (int*)&x, (int*)&brushY, baseTile, y); + this->ConstructionGFXLayer[x] = gfx; + } + } + } + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/renderWallDragPreview.cpp b/src/OpenSHC/Map/TileMapState/renderWallDragPreview.cpp new file mode 100644 index 0000000000..3b78dfc457 --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/renderWallDragPreview.cpp @@ -0,0 +1,181 @@ +#include "../TileMapState.func.hpp" + +#include "OpenSHC/Map/Buildings/BuildingsState.func.hpp" +#include "OpenSHC/WindowsHelper/Enums/BOOLEnum.hpp" + +#include "OpenSHC/Globals/DAT_BuildingsState.hpp" +#include "OpenSHC/Globals/DAT_ViewportRenderState.hpp" +#include "OpenSHC/Globals/GMFirstPictureIndex.hpp" + +namespace OpenSHC { +namespace Map { + + using OpenSHC::Map::LogicHelpers::L_CRENEL; + using OpenSHC::Map::LogicHelpers::L_WALL_OR_GATEHOUSE; + + // FUNCTION: STRONGHOLDCRUSADER 0x004F8BD0 + void TileMapState::renderWallDragPreview(int playerID, uint x1, uint y1, uint x2, uint y2, short command) + { + int budget = 0; + if (x1 > 399 || y1 > 399 || DAT_ViewportRenderState::instance.DAT_BinaryTileMap400x400[y1 * 400 + x1] == 0) { + return; + } + if (x2 > 399 || y2 > 399 || DAT_ViewportRenderState::instance.DAT_BinaryTileMap400x400[y2 * 400 + x2] == 0) { + return; + } + + this->DAT_WallTileCountCurrentDrag = 0; + int overshoot = 2; + if (command == OpenSHC::Commands::M_MAPPER_STAIR) { + budget = this->maxWallHeightInPath; + if (this->field_0x554920 == 0) { + this->ConstructionGFXLayer[DAT_ViewportRenderState::instance.translationMatrix[y1].addXgetTile + x1] + = (short)GMFirstPictureIndex::instance[6] + 19; + return; + } + } + + // the drag walks one tile at a time towards (x2, y2), on whichever axis is further away + uint x = x1; + uint y = y1; + int upY = y1 - y2; + int leftX = x1 - x2; + int downY = y2 - y1; + int rightX = x2 - x1; + int* translation = &DAT_ViewportRenderState::instance.translationMatrix[y1].addXgetTile; + while (true) { + if (command == OpenSHC::Commands::M_MAPPER_STAIR && budget < 24) { + break; + } + int const tile = *translation + x; + if (command == OpenSHC::Commands::M_MAPPER_STAIR && (this->LogicLayer[tile] & L_WALL_OR_GATEHOUSE) != 0) { + break; + } + if (this->DAT_WallTileCountCurrentDrag >= this->constructionTileCount) { + break; + } + this->DAT_WallTileCountCurrentDrag += 1; + + // a crenel dragged over an existing one does not count against the tile budget + BOOLEnum onCrenel = FALSE; + if (command == OpenSHC::Commands::M_MAPPER_WALL && (this->LogicLayer[tile] & L_CRENEL) != 0) { + onCrenel = TRUE; + } + if ((this->LogicLayer[tile] & L_WALL_OR_GATEHOUSE) != 0 && this->DamageLayer[tile] == 0 && !onCrenel) { + this->DAT_WallTileCountCurrentDrag -= 1; + } else { + if (onCrenel) { + this->DAT_WallTileCountCurrentDrag -= 1; + } + if (this->illegalBuild != FALSE) { + if (command == OpenSHC::Commands::M_MAPPER_WALL) { + this->ConstructionGFXLayer[tile] = (ushort)GMFirstPictureIndex::instance[6] + 19; + } else if (command == OpenSHC::Commands::M_MAPPER_WOODWALL) { + this->ConstructionGFXLayer[tile] = (ushort)GMFirstPictureIndex::instance[6] + 19; + } else if (command == OpenSHC::Commands::M_MAPPER_CRENAL) { + this->ConstructionGFXLayer[tile] = (ushort)GMFirstPictureIndex::instance[6] + 20; + } else if (command == OpenSHC::Commands::M_MAPPER_STAIR) { + this->ConstructionGFXLayer[tile] = (ushort)GMFirstPictureIndex::instance[6] + 19; + } + } else if (command == OpenSHC::Commands::M_MAPPER_WALL) { + this->ConstructionGFXLayer[tile] = (ushort)GMFirstPictureIndex::instance[6]; + } else if (command == OpenSHC::Commands::M_MAPPER_WOODWALL) { + this->ConstructionGFXLayer[tile] = (ushort)GMFirstPictureIndex::instance[6] + 1; + } else if (command == OpenSHC::Commands::M_MAPPER_CRENAL) { + this->ConstructionGFXLayer[tile] = (ushort)GMFirstPictureIndex::instance[6] + 4; + } else if (command == OpenSHC::Commands::M_MAPPER_STAIR) { + // a stair climbs one graphic per eight units of height below the wall top + int steps = (budget - this->HeightLayer[tile]) / 8 - 1; + if (steps <= 0) { + this->DAT_WallTileCountCurrentDrag -= 1; + } else { + this->ConstructionGFXLayer[tile] + = (steps > 10 ? 10 : steps) + GMFirstPictureIndex::instance[6] + 8; + } + } + } + + int const stepX = (int)x >= (int)x2 ? leftX : rightX; + int const stepY = (int)y >= (int)y2 ? upY : downY; + if (command == OpenSHC::Commands::M_MAPPER_STAIR) { + if (stepX > stepY) { + if ((int)x >= (int)x2) { + leftX -= 1; + x -= 1; + rightX += 1; + } else { + leftX += 1; + x += 1; + rightX -= 1; + } + } else if (stepY > 0) { + if ((int)y >= (int)y2) { + translation -= 3; + y -= 1; + upY -= 1; + downY += 1; + } else { + translation += 3; + y += 1; + upY += 1; + downY -= 1; + } + } else if (x == x2 && y == y2) { + overshoot -= 1; + } else { + overshoot -= 1; + } + } else { + if (stepX != 0) { + if ((int)x >= (int)x2) { + leftX -= 1; + x -= 1; + rightX += 1; + } else { + leftX += 1; + x += 1; + rightX -= 1; + } + } + if (stepY != 0) { + if ((int)y >= (int)y2) { + translation -= 3; + y -= 1; + upY -= 1; + downY += 1; + } else { + translation += 3; + y += 1; + upY += 1; + downY -= 1; + } + } + if (x == x2 && y == y2) { + overshoot -= 1; + } + } + if (budget > 16 && overshoot == 2) { + budget -= 16; + } + if (x == x2 && y == y2 && overshoot == 0) { + break; + } + } + + if (this->illegalBuild != FALSE) { + this->DAT_WallTileCountCurrentDrag = 0; + } + if (this->wallDragButtonUp != 0) { + return; + } + if (command == OpenSHC::Commands::M_MAPPER_WOODWALL) { + MACRO_CALL_MEMBER(OpenSHC::Map::Buildings::BuildingsState_Func::processWallBuildingLoss, + DAT_BuildingsState::ptr)(playerID, 0, this->DAT_WallTileCountCurrentDrag, 1); + return; + } + MACRO_CALL_MEMBER(OpenSHC::Map::Buildings::BuildingsState_Func::processWallBuildingLoss, + DAT_BuildingsState::ptr)(playerID, this->DAT_WallTileCountCurrentDrag, 0, 1); + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/renderWallPlacementPreview.cpp b/src/OpenSHC/Map/TileMapState/renderWallPlacementPreview.cpp new file mode 100644 index 0000000000..53f58b337e --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/renderWallPlacementPreview.cpp @@ -0,0 +1,119 @@ +#include "../TileMapState.func.hpp" + +#include "OpenSHC/WindowsHelper/Enums/BOOLEnum.hpp" + +#include "OpenSHC/Globals/DAT_ViewportRenderState.hpp" +#include "OpenSHC/Globals/GMFirstPictureIndex.hpp" + +namespace OpenSHC { +namespace Map { + + using OpenSHC::Map::LogicHelpers::L_BORDER; + using OpenSHC::Map::LogicHelpers::L_BORDER_EDGE; + using OpenSHC::Map::LogicHelpers::L_BUILDING; + + // FUNCTION: STRONGHOLDCRUSADER 0x004FB9E0 + void TileMapState::renderWallPlacementPreview(uint x, uint y, short kind) + { + int variationStep = 0; + int base = 0; + int size = 0; + int index = 0; + if (x > 399 || y > 399 || DAT_ViewportRenderState::instance.DAT_BinaryTileMap400x400[y * 400 + x] == 0) { + return; + } + this->buildingPlacementFail = FALSE; + // every wall kind has its own footprint size and first graphic + if (kind == 0) { + size = 1; + base = 529; + } else if (kind == 1) { + size = 1; + base = 533; + } else if (kind == 2) { + size = 1; + base = 537; + } else if (kind == 3) { + size = 1; + base = 541; + } else if (kind == 4) { + size = 2; + base = 465; + } else if (kind == 5) { + size = 2; + base = 481; + } else if (kind == 6) { + size = 2; + base = 497; + } else if (kind == 7) { + size = 2; + base = 513; + } else if (kind == 8) { + size = 3; + base = 321; + } else if (kind == 9) { + size = 3; + base = 357; + } else if (kind == 10) { + size = 3; + base = 393; + } else if (kind == 11) { + size = 3; + base = 429; + } else if (kind == 12) { + base = 65; + size = 4; + } else if (kind == 13) { + base = 129; + size = 4; + } else if (kind == 14) { + base = 193; + size = 4; + } else if (kind == 15) { + base = 257; + size = 4; + } + + int rotation = this->rockOrientation + this->mapOrientation; + if (this->editorRockType == 5) { + rotation = this->mapOrientation; + } + if (rotation >= 8) { + rotation -= 8; + } + // the rotation part of the graphic index does not change over the footprint + variationStep = rotation / 2 * size * size; + + do { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::getBuildingSizeIndexMappingData, this)(index, size); + int tile = DAT_ViewportRenderState::instance.translationMatrix[this->buildingY + y].addXgetTile; + tile += this->buildingX + x; + uint const logic = this->LogicLayer[tile]; + if ((logic & (L_BORDER | L_BORDER_EDGE)) != 0) { + return; + } + if ((logic & L_BUILDING) != 0) { + return; + } + if (this->OrganismLayer[tile] != 0) { + return; + } + index++; + } while (index < this->constructionTileCount); + + index = 0; + while (true) { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::getBuildingSizeIndexMappingData, this)(index, size); + this->ConstructionGFXLayer[DAT_ViewportRenderState::instance.translationMatrix[this->buildingY + y] + .addXgetTile + + this->buildingX + x] + = this->buildingRotationRelatedValue + GMFirstPictureIndex::instance[56] + variationStep + base - 1; + index++; + if (index >= this->constructionTileCount) { + break; + } + } + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/returnSomeHeight.cpp b/src/OpenSHC/Map/TileMapState/returnSomeHeight.cpp new file mode 100644 index 0000000000..264e2d8302 --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/returnSomeHeight.cpp @@ -0,0 +1,77 @@ +#include "../TileMapState.func.hpp" + +#include "OpenSHC/Map/Buildings/BuildingsState.func.hpp" + +#include "OpenSHC/Globals/DAT_BuildingDefinedData.hpp" +#include "OpenSHC/Globals/DAT_BuildingsState.hpp" +#include "OpenSHC/Globals/DAT_LandscapeState.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_ROCKY; + using OpenSHC::Map::LogicHelpers::L_WALL_OR_GATEHOUSE; + + // FUNCTION: STRONGHOLDCRUSADER 0x004FF990 + uint TileMapState::returnSomeHeight(int tile, int ignoreHuntersHut) + { + uint const logic = this->LogicLayer[tile]; + int extraHeight = 0; + if ((logic & L_KEEP_NON_MANOR_HOUSE) != 0) { + extraHeight + = MACRO_CALL_MEMBER(OpenSHC::Map::Buildings::BuildingsState_Func::getBuildingHeightForBuildingID, + DAT_BuildingsState::ptr)(this->BuildingLayer[tile]); + } else if ((logic & L_BUILDING) != 0) { + int const buildingID = this->BuildingLayer[tile]; + int const type = DAT_BuildingsState::instance.buildings[buildingID].buildingType; + switch (type) { + case OpenSHC::Map::Buildings::BT_GATEHOUSELARGE: + case OpenSHC::Map::Buildings::BT_GATEHOUSESMALL: + case OpenSHC::Map::Buildings::BT_TOWER: + 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: + extraHeight + = MACRO_CALL_MEMBER(OpenSHC::Map::Buildings::BuildingsState_Func::getBuildingHeightForBuildingID, + DAT_BuildingsState::ptr)(buildingID) + + 10; + break; + case OpenSHC::Map::Buildings::BT_HUNTERSHUT: + if (ignoreHuntersHut != 0) { + extraHeight = 0; + break; + } + default: + extraHeight = DAT_BuildingDefinedData::instance.BuildingHeights[type]; + break; + case OpenSHC::Map::Buildings::BT_MANORHOUSE: + case OpenSHC::Map::Buildings::BT_STONEKEEP: + case OpenSHC::Map::Buildings::BT_STRONGHOLD: + case OpenSHC::Map::Buildings::BT_KEEPFOUR: + case OpenSHC::Map::Buildings::BT_KEEPFIVE: + extraHeight + = MACRO_CALL_MEMBER(OpenSHC::Map::Buildings::BuildingsState_Func::getBuildingHeightForBuildingID, + DAT_BuildingsState::ptr)(buildingID); + break; + } + } else if ((logic & L_ROCKY) != 0 && (logic & L_WALL_OR_GATEHOUSE) == 0) { + // rock ids are biased by 2000 + int organism = this->OrganismLayer[tile]; + if (organism >= 2000) { + organism -= 2000; + if (DAT_LandscapeState::instance.rocks[organism].type >= 13) { + extraHeight = 70; + } else if (DAT_LandscapeState::instance.rocks[organism].type >= 9) { + extraHeight = 50; + } + } + } + return this->HeightLayer[tile] + extraHeight; + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/setBitFlagBasedOnWallTowerGatehouseOrKeep.cpp b/src/OpenSHC/Map/TileMapState/setBitFlagBasedOnWallTowerGatehouseOrKeep.cpp new file mode 100644 index 0000000000..e1bdff82c0 --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/setBitFlagBasedOnWallTowerGatehouseOrKeep.cpp @@ -0,0 +1,42 @@ +#include "../TileMapState.func.hpp" + +#include "OpenSHC/Rendering/ViewportRenderState.func.hpp" +#include "OpenSHC/Map/TileMapState/NeighbourFlagsGlobalAsm.hpp" + +#include "OpenSHC/Globals/DAT_ViewportRenderState.hpp" + +#define NEIGHBOUR_FLAGS_MASK_WALL_OR_KEEP 0x10000100 // L_WALL_OR_GATEHOUSE | L_KEEP_NON_MANOR_HOUSE + +/* the original was built without optimisation: this spilled to [ebp - 4] and reloaded for every access */ +#pragma optimize("", off) + +namespace OpenSHC { +namespace Map { + + // FUNCTION: STRONGHOLDCRUSADER 0x004FF870 + uint TileMapState::setBitFlagBasedOnWallTowerGatehouseOrKeep(int x, int y) + { + /* + The body is the handwritten neighbour-flag macro; see NeighbourFlagsGlobalAsm.hpp. Written as C++ + it would be: + + this->bitFlag = 0; + if ((this->ptr_LogicLayer[this->DAT_SomeTile + 1] & (L_WALL_OR_GATEHOUSE | L_KEEP_NON_MANOR_HOUSE)) != 0) { + this->bitFlag = 0x20; + } + ... and so on for the eight neighbours, where the north row is offset by + ptr_MovementDirectionTranslationMatrix[DAT_SomeY * 8 + 0] and the south row by [+ 4]. + */ + this->DAT_SomeY = y; + this->DAT_SomeTile = MACRO_CALL_MEMBER( + OpenSHC::Rendering::ViewportRenderState_Func::translateXYToTile, &DAT_ViewportRenderState::instance)(x, y); + + MACRO_NEIGHBOUR_FLAGS_GLOBAL_LOGIC_8(1, NEIGHBOUR_FLAGS_MASK_WALL_OR_KEEP) + + return this->bitFlag; + } + +} +} + +#pragma optimize("", on) diff --git a/src/OpenSHC/Map/TileMapState/setConstructionGFXLayerBasedOnPlacementChecks.cpp b/src/OpenSHC/Map/TileMapState/setConstructionGFXLayerBasedOnPlacementChecks.cpp new file mode 100644 index 0000000000..d28c682cde --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/setConstructionGFXLayerBasedOnPlacementChecks.cpp @@ -0,0 +1,831 @@ +#include "../TileMapState.func.hpp" + +#include "OpenSHC/Map/Buildings/BuildingsState.func.hpp" +#include "OpenSHC/Commands/MappersEnum.hpp" +#include "OpenSHC/Map/Buildings/BuildingType.hpp" +#include "OpenSHC/WindowsHelper/Enums/BOOLEnum.hpp" + +#include "OpenSHC/Globals/DAT_BuildingDefinedData.hpp" +#include "OpenSHC/Globals/DAT_BuildingsState.hpp" +#include "OpenSHC/Globals/DAT_GameState.hpp" +#include "OpenSHC/Globals/DAT_GameSynchronyState.hpp" +#include "OpenSHC/Globals/DAT_TerrainDefinedData.hpp" +#include "OpenSHC/Globals/DAT_ViewportRenderState.hpp" +#include "OpenSHC/Globals/GMFirstPictureIndex.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::Location::GateTowerPlacement; + + // FUNCTION: STRONGHOLDCRUSADER 0x00504F10 + void TileMapState::setConstructionGFXLayerBasedOnPlacementChecks(int x, int y, short command, int size) + { + int const skipPlacementFailChecks = this->skipPlacementFailChecks; + int keepSpriteVariant = 0; + int i = 0; + int const originX = x; + int const originY = y; + this->skipPlacementFailChecks = 0; + if (skipPlacementFailChecks != 0) { + BuildingType const buildingType = MACRO_CALL_MEMBER( + OpenSHC::Map::Buildings::BuildingsState_Func::convertCommandBuildingTypeToBuildingType, + DAT_BuildingsState::ptr)((MappersEnum)command); + this->buildingSpriteSheetID_1 + = DAT_BuildingDefinedData::instance.Building_SpriteSheet_ID_Array_1[buildingType].intValue; + this->buildingSpriteID1 = MACRO_CALL_MEMBER(OpenSHC::Map::Buildings::BuildingsState_Func::getSpriteID, + DAT_BuildingsState::ptr)((MappersEnum)command); + this->buildingSpriteID2 = MACRO_CALL_MEMBER(OpenSHC::Map::Buildings::BuildingsState_Func::getSpriteID2, + DAT_BuildingsState::ptr)((MappersEnum)command); + } else { + if (this->buildingPlacementFail == 2) { + return; + } + if (this->buildingPlacementFail != FALSE) { + // colour every tile of the footprint by whether it can be built on + while (true) { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::getBuildingSizeIndexMappingData, this)(i, size); + int const tile + = DAT_ViewportRenderState::instance.translationMatrix[this->buildingY + originY].addXgetTile + + this->buildingX + originX; + if ((this->LogicLayer[tile] & (L_KEEP_NON_MANOR_HOUSE | L_BUILDING | L_WALL_OR_GATEHOUSE)) == 0) { + this->ConstructionGFXLayer[tile] + = MACRO_CALL_MEMBER( + OpenSHC::Map::TileMapState_Func::isBuildingPlacementAllowedAtTile, this)( + tile, DAT_GameSynchronyState::instance.currentPlayerSlotID, (MappersEnum)command, 0) + ? 55 + : 62; + } + i++; + if (i >= this->constructionTileCount) { + break; + } + } + if (command == OpenSHC::Commands::M_MAPPER_KEEP1 || command == OpenSHC::Commands::M_MAPPER_KEEP2 + || command == OpenSHC::Commands::M_MAPPER_KEEP3) { + int const keepIndex = command - OpenSHC::Commands::M_MAPPER_KEEP1; + for (int part = 0; part != 3; part++) { + XYPair const& offset = DAT_TerrainDefinedData::instance.field_0x264[keepIndex][part]; + int const tile + = DAT_ViewportRenderState::instance.translationMatrix[offset.y + originY].addXgetTile + + offset.x + originX; + if ((this->LogicLayer[tile] & (L_KEEP_NON_MANOR_HOUSE | L_BUILDING | L_WALL_OR_GATEHOUSE)) + == 0) { + this->ConstructionGFXLayer[tile] + = MACRO_CALL_MEMBER( + OpenSHC::Map::TileMapState_Func::isBuildingPlacementAllowedAtTile, this)(tile, + DAT_GameSynchronyState::instance.currentPlayerSlotID, (MappersEnum)command, 0) + ? 55 + : 62; + } + } + x = originX + DAT_TerrainDefinedData::instance.keepOutbuildingOffsets_0x384[keepIndex][0].xOffset; + y = originY + DAT_TerrainDefinedData::instance.keepOutbuildingOffsets_0x384[keepIndex][0].yOffset; + i = 0; + while (true) { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::getBuildingSizeIndexMappingData, this)(i, 7); + int const tile + = DAT_ViewportRenderState::instance.translationMatrix[this->buildingY + y].addXgetTile + + this->buildingX + x; + if ((this->LogicLayer[tile] & (L_KEEP_NON_MANOR_HOUSE | L_BUILDING | L_WALL_OR_GATEHOUSE)) + == 0) { + this->ConstructionGFXLayer[tile] + = MACRO_CALL_MEMBER( + OpenSHC::Map::TileMapState_Func::isBuildingPlacementAllowedAtTile, this)(tile, + DAT_GameSynchronyState::instance.currentPlayerSlotID, (MappersEnum)command, 0) + ? 55 + : 62; + } + i++; + if (i >= this->constructionTileCount) { + break; + } + } + x = DAT_TerrainDefinedData::instance.keepOutbuildingOffsets_0x3e4[keepIndex][0].xOffset + originX; + y = DAT_TerrainDefinedData::instance.keepOutbuildingOffsets_0x3e4[keepIndex][0].yOffset + originY; + int j = 0; + while (true) { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::getBuildingSizeIndexMappingData, this)(j, 5); + int const tile + = DAT_ViewportRenderState::instance.translationMatrix[this->buildingY + y].addXgetTile + + this->buildingX + x; + if ((this->LogicLayer[tile] & (L_KEEP_NON_MANOR_HOUSE | L_BUILDING | L_WALL_OR_GATEHOUSE)) + == 0) { + this->ConstructionGFXLayer[tile] + = MACRO_CALL_MEMBER( + OpenSHC::Map::TileMapState_Func::isBuildingPlacementAllowedAtTile, this)(tile, + DAT_GameSynchronyState::instance.currentPlayerSlotID, (MappersEnum)command, 0) + ? 55 + : 62; + } + j++; + if (j >= this->constructionTileCount) { + break; + } + } + } + if (command == OpenSHC::Commands::M_MAPPER_GATE_WOOD1A + || command == OpenSHC::Commands::M_MAPPER_GATE_WOOD1B + || command == OpenSHC::Commands::M_MAPPER_GATE_WOOD1C + || command == OpenSHC::Commands::M_MAPPER_GATE_WOOD1D) { + MappersEnum const gateCommand = (MappersEnum)command; + int const tower1X = DAT_TerrainDefinedData::instance + .gateTowerChecks[gateCommand - OpenSHC::Commands::M_MAPPER_GATE_WOOD1A] + .x1 + + originX; + int const tower1Y = DAT_TerrainDefinedData::instance + .gateTowerChecks[gateCommand - OpenSHC::Commands::M_MAPPER_GATE_WOOD1A] + .y1 + + originY; + int j = 0; + while (true) { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::getBuildingSizeIndexMappingData, this)(j, 3); + int const tile + = DAT_ViewportRenderState::instance.translationMatrix[this->buildingY + tower1Y].addXgetTile + + this->buildingX + tower1X; + this->ConstructionGFXLayer[tile] + = MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::isBuildingPlacementAllowedAtTile, + this)(tile, DAT_GameSynchronyState::instance.currentPlayerSlotID, gateCommand, 0) + ? 55 + : 62; + j++; + if (j >= this->constructionTileCount) { + break; + } + } + int const tower2X = DAT_TerrainDefinedData::instance + .gateTowerChecks[gateCommand - OpenSHC::Commands::M_MAPPER_GATE_WOOD1A] + .x2 + + originX; + int const tower2Y = DAT_TerrainDefinedData::instance + .gateTowerChecks[gateCommand - OpenSHC::Commands::M_MAPPER_GATE_WOOD1A] + .y2 + + originY; + j = 0; + while (true) { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::getBuildingSizeIndexMappingData, this)(j, 3); + int const tile + = DAT_ViewportRenderState::instance.translationMatrix[this->buildingY + tower2Y].addXgetTile + + this->buildingX + tower2X; + this->ConstructionGFXLayer[tile] + = MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::isBuildingPlacementAllowedAtTile, + this)(tile, DAT_GameSynchronyState::instance.currentPlayerSlotID, gateCommand, 0) + ? 55 + : 62; + j++; + if (j >= this->constructionTileCount) { + break; + } + } + for (int j = 0; j != 6; j++) { + int const tile = DAT_ViewportRenderState::instance + .translationMatrix[DAT_TerrainDefinedData::instance + .gateHouseTiles[gateCommand + - OpenSHC::Commands::M_MAPPER_GATE_WOOD1A][j] + .y + + originY] + .addXgetTile + + DAT_TerrainDefinedData::instance + .gateHouseTiles[gateCommand - OpenSHC::Commands::M_MAPPER_GATE_WOOD1A][j] + .x + + originX; + this->ConstructionGFXLayer[tile] + = MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::isBuildingPlacementAllowedAtTile, + this)(tile, DAT_GameSynchronyState::instance.currentPlayerSlotID, gateCommand, 0) + ? 55 + : 62; + } + } else if (command == OpenSHC::Commands::M_MAPPER_BARRACKS_EURO + || command == OpenSHC::Commands::M_MAPPER_BARRACKS_ARAB) { + int const rightX = originX + 5; + int j = 0; + while (true) { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::getBuildingSizeIndexMappingData, this)(j, 5); + int const tile + = DAT_ViewportRenderState::instance.translationMatrix[this->buildingY + originY].addXgetTile + + this->buildingX + rightX; + if ((this->LogicLayer[tile] & (L_KEEP_NON_MANOR_HOUSE | L_BUILDING | L_WALL_OR_GATEHOUSE)) + == 0) { + this->ConstructionGFXLayer[tile] + = MACRO_CALL_MEMBER( + OpenSHC::Map::TileMapState_Func::isBuildingPlacementAllowedAtTile, this)(tile, + DAT_GameSynchronyState::instance.currentPlayerSlotID, (MappersEnum)command, 0) + ? 55 + : 62; + } + j++; + if (j >= this->constructionTileCount) { + break; + } + } + y = originY + 5; + j = 0; + while (true) { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::getBuildingSizeIndexMappingData, this)(j, 5); + int const tile + = DAT_ViewportRenderState::instance.translationMatrix[this->buildingY + y].addXgetTile + + this->buildingX + originX; + if ((this->LogicLayer[tile] & (L_KEEP_NON_MANOR_HOUSE | L_BUILDING | L_WALL_OR_GATEHOUSE)) + == 0) { + this->ConstructionGFXLayer[tile] + = MACRO_CALL_MEMBER( + OpenSHC::Map::TileMapState_Func::isBuildingPlacementAllowedAtTile, this)(tile, + DAT_GameSynchronyState::instance.currentPlayerSlotID, (MappersEnum)command, 0) + ? 55 + : 62; + } + j++; + if (j >= this->constructionTileCount) { + break; + } + } + j = 0; + while (true) { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::getBuildingSizeIndexMappingData, this)(j, 5); + int const tile + = DAT_ViewportRenderState::instance.translationMatrix[this->buildingY + y].addXgetTile + + this->buildingX + rightX; + if ((this->LogicLayer[tile] & (L_KEEP_NON_MANOR_HOUSE | L_BUILDING | L_WALL_OR_GATEHOUSE)) + == 0) { + this->ConstructionGFXLayer[tile] + = MACRO_CALL_MEMBER( + OpenSHC::Map::TileMapState_Func::isBuildingPlacementAllowedAtTile, this)(tile, + DAT_GameSynchronyState::instance.currentPlayerSlotID, (MappersEnum)command, 0) + ? 55 + : 62; + } + j++; + if (j >= this->constructionTileCount) { + break; + } + } + } else if (command == OpenSHC::Commands::M_MAPPER_ENGINEERS_GUILD + || command == OpenSHC::Commands::M_MAPPER_TUNNELERS_GUILD) { + y = originY + 5; + int j = 0; + while (true) { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::getBuildingSizeIndexMappingData, this)(j, 5); + int const tile + = DAT_ViewportRenderState::instance.translationMatrix[this->buildingY + y].addXgetTile + + this->buildingX + originX; + if ((this->LogicLayer[tile] & (L_KEEP_NON_MANOR_HOUSE | L_BUILDING | L_WALL_OR_GATEHOUSE)) + == 0) { + this->ConstructionGFXLayer[tile] + = MACRO_CALL_MEMBER( + OpenSHC::Map::TileMapState_Func::isBuildingPlacementAllowedAtTile, this)(tile, + DAT_GameSynchronyState::instance.currentPlayerSlotID, (MappersEnum)command, 0) + ? 55 + : 62; + } + j++; + if (j >= this->constructionTileCount) { + break; + } + } + } else if (command == OpenSHC::Commands::M_MAPPER_OIL_SMELTER) { + y = originY + 4; + int j = 0; + while (true) { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::getBuildingSizeIndexMappingData, this)(j, 4); + int const tile + = DAT_ViewportRenderState::instance.translationMatrix[this->buildingY + y].addXgetTile + + this->buildingX + originX; + if ((this->LogicLayer[tile] & (L_KEEP_NON_MANOR_HOUSE | L_BUILDING | L_WALL_OR_GATEHOUSE)) + == 0) { + this->ConstructionGFXLayer[tile] + = MACRO_CALL_MEMBER( + OpenSHC::Map::TileMapState_Func::isBuildingPlacementAllowedAtTile, this)(tile, + DAT_GameSynchronyState::instance.currentPlayerSlotID, (MappersEnum)command, 0) + ? 55 + : 62; + } + j++; + if (j >= this->constructionTileCount) { + break; + } + } + } + return; + } + } + if (command == OpenSHC::Commands::M_MAPPER_WHEATFARM) { + size = 3; + } else if (command == OpenSHC::Commands::M_MAPPER_HOPSFARM) { + size = 3; + } else if (command == OpenSHC::Commands::M_MAPPER_APPLEFARM) { + size = 3; + } else if (command == OpenSHC::Commands::M_MAPPER_CATTLEFARM) { + size = 3; + } else if (command == OpenSHC::Commands::M_MAPPER_GATE_WOOD1A + || command == OpenSHC::Commands::M_MAPPER_GATE_WOOD1C || command == OpenSHC::Commands::M_MAPPER_GATE_STONE1A + || command == OpenSHC::Commands::M_MAPPER_GATE_STONE2A) { + this->gateOrientationVariant = 81; + } else if (command == OpenSHC::Commands::M_MAPPER_GATE_WOOD1B + || command == OpenSHC::Commands::M_MAPPER_GATE_WOOD1D || command == OpenSHC::Commands::M_MAPPER_GATE_STONE1B + || command == OpenSHC::Commands::M_MAPPER_GATE_STONE2B) { + this->gateOrientationVariant = 80; + } + MappersEnum const buildingCommand = (MappersEnum)command; + while (true) { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::getBuildingSizeIndexMappingData, this)(i, size); + int rotation = this->uiBuildingRotation; + if (rotation != 15 && rotation != 8) { + rotation -= this->mapOrientation; + if (rotation < 0) { + rotation += 8; + } + } + int sprite; + if (this->mapOrientation == 2 || this->mapOrientation == 6) { + if (this->gateOrientationVariant == 81) { + sprite = this->buildingSpriteID1; + } else { + sprite = this->buildingSpriteID2; + } + } else if (this->gateOrientationVariant == 81) { + sprite = this->buildingSpriteID2; + } else { + sprite = this->buildingSpriteID1; + } + if (command == OpenSHC::Commands::M_MAPPER_DRAWBRIDGE && rotation != 0) { + if (rotation == 2) { + rotation = 3; + } else if (rotation == 4) { + rotation = 2; + } else if (rotation == 6) { + rotation = 1; + } + } + int const tile = DAT_ViewportRenderState::instance.translationMatrix[this->buildingY + originY].addXgetTile + + this->buildingX + originX; + if (MACRO_CALL_MEMBER( + OpenSHC::Map::Buildings::BuildingsState_Func::convertCommandBuildingTypeToBuildingType, + DAT_BuildingsState::ptr)(buildingCommand) + == OpenSHC::Map::Buildings::BT_UNKNOWN1) { + sprite += DAT_BuildingDefinedData::instance + .UnknownVariations[buildingCommand - OpenSHC::Commands::M_MAPPER_UNKNOWN_248]; + } else if (MACRO_CALL_MEMBER( + OpenSHC::Map::Buildings::BuildingsState_Func::convertCommandBuildingTypeToBuildingType, + DAT_BuildingsState::ptr)(buildingCommand) + == OpenSHC::Map::Buildings::BT_HOVEL) { + sprite = DAT_BuildingDefinedData::instance.SomeSpriteArray1[DAT_GameState::instance + .playerDataArray[DAT_GameSynchronyState::instance.currentPlayerSlotID] + .hovelCountUpToEight]; + } + if (this->uiBuildingRotation != 15) { + this->ConstructionGFXLayer[tile] = (short)(rotation * size * size) + + (short)GMFirstPictureIndex::instance[this->buildingSpriteSheetID_1] + (short)sprite + + (short)this->buildingRotationRelatedValue - 1; + } else { + this->ConstructionGFXLayer[tile] = (short)GMFirstPictureIndex::instance[this->buildingSpriteSheetID_1] + + (short)sprite + (short)this->buildingRotationRelatedValue - 1; + } + if (skipPlacementFailChecks != 0) { + this->MiscDisplayLayer[tile] |= 0x8000; + } + i++; + if (i >= this->constructionTileCount) { + break; + } + } + if (command == OpenSHC::Commands::M_MAPPER_GATE_WOOD1A || command == OpenSHC::Commands::M_MAPPER_GATE_WOOD1B + || command == OpenSHC::Commands::M_MAPPER_GATE_WOOD1C + || command == OpenSHC::Commands::M_MAPPER_GATE_WOOD1D) { + int const gateIndex = buildingCommand - OpenSHC::Commands::M_MAPPER_GATE_WOOD1A; + int const tower1X = DAT_TerrainDefinedData::instance.gateTowerPlacements[gateIndex].x1; + int const tower1Y = DAT_TerrainDefinedData::instance.gateTowerPlacements[gateIndex].y1; + int const tower1Rotation = DAT_TerrainDefinedData::instance.gateTowerPlacements[gateIndex].rotationIndex1; + int j = 0; + while (true) { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::getBuildingSizeIndexMappingData, this)(j, 2); + int const tile + = DAT_ViewportRenderState::instance.translationMatrix[tower1Y + originY + this->buildingY] + .addXgetTile + + this->buildingX + tower1X + originX; + this->ConstructionGFXLayer[tile] + = (short)this->buildingRotationRelatedValue + (short)GMFirstPictureIndex::instance[52]; + if (skipPlacementFailChecks != 0) { + this->MiscDisplayLayer[tile] |= 0x8000; + } + j++; + if (j >= this->constructionTileCount) { + break; + } + } + for (j = 0; j < 5; j++) { + XYPair const& offset = DAT_TerrainDefinedData::instance.gateTowerTileOffsets[tower1Rotation][j]; + int const tile + = DAT_ViewportRenderState::instance.translationMatrix[offset.y + tower1Y + originY].addXgetTile + + offset.x + tower1X + originX; + if (j <= 0) { + this->ConstructionGFXLayer[tile] = GMFirstPictureIndex::instance[52] + j + 1550; + } else { + int towerRotation + = DAT_TerrainDefinedData::instance.constructionSpriteRotationTable[tower1Rotation][j - 1] + - this->mapOrientation; + if (towerRotation < 0) { + towerRotation += 8; + } + this->ConstructionGFXLayer[tile] + = GMFirstPictureIndex::instance[52] + (towerRotation / 2) * 4 + j + 1550; + } + if (skipPlacementFailChecks != 0) { + this->MiscDisplayLayer[tile] |= 0x8000; + } + } + int const tower2X = DAT_TerrainDefinedData::instance.gateTowerPlacements[gateIndex].x2; + int const tower2Y = DAT_TerrainDefinedData::instance.gateTowerPlacements[gateIndex].y2; + int const tower2Rotation = DAT_TerrainDefinedData::instance.gateTowerPlacements[gateIndex].rotationIndex2; + j = 0; + while (true) { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::getBuildingSizeIndexMappingData, this)(j, 2); + int const tile + = DAT_ViewportRenderState::instance.translationMatrix[tower2Y + originY + this->buildingY] + .addXgetTile + + this->buildingX + tower2X + originX; + this->ConstructionGFXLayer[tile] + = (short)this->buildingRotationRelatedValue + (short)GMFirstPictureIndex::instance[52]; + if (skipPlacementFailChecks != 0) { + this->MiscDisplayLayer[tile] |= 0x8000; + } + j++; + if (j >= this->constructionTileCount) { + break; + } + } + for (j = 0; j < 5; j++) { + XYPair const& offset = DAT_TerrainDefinedData::instance.gateTowerTileOffsets[tower2Rotation][j]; + int const tile + = DAT_ViewportRenderState::instance.translationMatrix[offset.y + tower2Y + originY].addXgetTile + + offset.x + tower2X + originX; + if (j <= 0) { + this->ConstructionGFXLayer[tile] = GMFirstPictureIndex::instance[52] + j + 1550; + } else { + int towerRotation + = DAT_TerrainDefinedData::instance.constructionSpriteRotationTable[tower2Rotation][j - 1] + - this->mapOrientation; + if (towerRotation < 0) { + towerRotation += 8; + } + this->ConstructionGFXLayer[tile] + = GMFirstPictureIndex::instance[52] + (towerRotation / 2) * 4 + j + 1550; + } + if (skipPlacementFailChecks != 0) { + this->MiscDisplayLayer[tile] |= 0x8000; + } + } + for (int j = 0; j != 6; j++) { + int const tile + = DAT_ViewportRenderState::instance + .translationMatrix[DAT_TerrainDefinedData::instance.gateHouseTiles[gateIndex][j].y + originY] + .addXgetTile + + originX + DAT_TerrainDefinedData::instance.gateHouseTiles[gateIndex][j].x; + this->ConstructionGFXLayer[tile] + = (this->RandomLayer[tile] & 3) + GMFirstPictureIndex::instance[6] + 312; + if (skipPlacementFailChecks != 0) { + this->MiscDisplayLayer[tile] |= 0x8000; + } + } + } else if (command == OpenSHC::Commands::M_MAPPER_WHEATFARM) { + int j = 0; + do { + int const tile + = DAT_ViewportRenderState::instance + .translationMatrix + [DAT_TerrainDefinedData::instance + .WheatFarmTiles[DAT_GameState::instance + .playerDataArray[DAT_GameSynchronyState::instance.currentPlayerSlotID] + .unkWheatFarmOrientation][j] + .y + + originY] + .addXgetTile + + DAT_TerrainDefinedData::instance + .WheatFarmTiles[DAT_GameState::instance + .playerDataArray[DAT_GameSynchronyState::instance.currentPlayerSlotID] + .unkWheatFarmOrientation][j] + .x + + originX; + this->ConstructionGFXLayer[tile] = (this->RandomLayer[tile] & 3) + GMFirstPictureIndex::instance[14]; + if (skipPlacementFailChecks != 0) { + this->MiscDisplayLayer[tile] |= 0x8000; + } + j++; + } while (j < 36); + } else if (command == OpenSHC::Commands::M_MAPPER_HOPSFARM) { + int j = 0; + do { + int const tile + = DAT_ViewportRenderState::instance + .translationMatrix + [DAT_TerrainDefinedData::instance + .HopFarmProperty1[*(int*)&DAT_GameState::instance + .playerDataArray[DAT_GameSynchronyState::instance.currentPlayerSlotID] + .unkHopFarmVariation][j] + .y + + originY] + .addXgetTile + + DAT_TerrainDefinedData::instance + .HopFarmProperty1[*(int*)&DAT_GameState::instance + .playerDataArray[DAT_GameSynchronyState::instance.currentPlayerSlotID] + .unkHopFarmVariation][j] + .x + + originX; + this->ConstructionGFXLayer[tile] + = (this->RandomLayer[tile] & 1) * 9 + 37 + GMFirstPictureIndex::instance[14]; + if (skipPlacementFailChecks != 0) { + this->MiscDisplayLayer[tile] |= 0x8000; + } + j++; + } while (j < 24); + } else if (command == OpenSHC::Commands::M_MAPPER_CATTLEFARM) { + for (int j = 0; j < 23; j++) { + int const tile + = DAT_ViewportRenderState::instance + .translationMatrix + [DAT_TerrainDefinedData::instance + .field_0xf5c[DAT_GameState::instance + .playerDataArray[DAT_GameSynchronyState::instance.currentPlayerSlotID] + .dairyFarmVariationMod4][j + 4] + .offset.y + + originY] + .addXgetTile + + DAT_TerrainDefinedData::instance + .field_0xf5c[DAT_GameState::instance + .playerDataArray[DAT_GameSynchronyState::instance.currentPlayerSlotID] + .dairyFarmVariationMod4][j + 4] + .offset.x + + originX; + this->ConstructionGFXLayer[tile] + = MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::getRubbleGraphicStageForDamageLevel, this)( + DAT_TerrainDefinedData::instance + .field_0xf5c[DAT_GameState::instance + .playerDataArray[DAT_GameSynchronyState::instance.currentPlayerSlotID] + .dairyFarmVariationMod4][j + 4] + .property) + + 55 + GMFirstPictureIndex::instance[14]; + if (skipPlacementFailChecks != 0) { + this->MiscDisplayLayer[tile] |= 0x8000; + } + } + } else if (command == OpenSHC::Commands::M_MAPPER_APPLEFARM) { + int j = 0; + do { + XYPair const& offset = DAT_TerrainDefinedData::instance.AppleFarmOffsets[j]; + int const tile = DAT_ViewportRenderState::instance.translationMatrix[offset.y + originY].addXgetTile + + offset.x + originX; + this->ConstructionGFXLayer[tile] = GMFirstPictureIndex::instance[14] + 61; + if (skipPlacementFailChecks != 0) { + this->MiscDisplayLayer[tile] |= 0x8000; + } + j++; + } while (j < 8); + } else if (command == OpenSHC::Commands::M_MAPPER_BARRACKS_EURO + || command == OpenSHC::Commands::M_MAPPER_BARRACKS_ARAB) { + x = originX + 5; + int j = 0; + while (true) { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::getBuildingSizeIndexMappingData, this)(j, 5); + int const tile + = DAT_ViewportRenderState::instance.translationMatrix[this->buildingY + originY].addXgetTile + + this->buildingX + x; + this->ConstructionGFXLayer[tile] + = (short)this->buildingRotationRelatedValue + (short)GMFirstPictureIndex::instance[6] + 72; + if (skipPlacementFailChecks != 0) { + this->MiscDisplayLayer[tile] |= 0x8000; + } + j++; + if (j >= this->constructionTileCount) { + break; + } + } + y = originY + 5; + j = 0; + while (true) { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::getBuildingSizeIndexMappingData, this)(j, 5); + int const tile = DAT_ViewportRenderState::instance.translationMatrix[this->buildingY + y].addXgetTile + + this->buildingX + originX; + this->ConstructionGFXLayer[tile] + = (short)this->buildingRotationRelatedValue + (short)GMFirstPictureIndex::instance[6] + 122; + if (skipPlacementFailChecks != 0) { + this->MiscDisplayLayer[tile] |= 0x8000; + } + j++; + if (j >= this->constructionTileCount) { + break; + } + } + j = 0; + while (true) { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::getBuildingSizeIndexMappingData, this)(j, 5); + int const tile = DAT_ViewportRenderState::instance.translationMatrix[this->buildingY + y].addXgetTile + + this->buildingX + x; + this->ConstructionGFXLayer[tile] + = (short)this->buildingRotationRelatedValue + (short)GMFirstPictureIndex::instance[6] + 97; + if (skipPlacementFailChecks != 0) { + this->MiscDisplayLayer[tile] |= 0x8000; + } + j++; + if (j >= this->constructionTileCount) { + break; + } + } + } else if (command == OpenSHC::Commands::M_MAPPER_ENGINEERS_GUILD) { + y = originY + 5; + int j = 0; + while (true) { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::getBuildingSizeIndexMappingData, this)(j, 5); + int const tile = DAT_ViewportRenderState::instance.translationMatrix[this->buildingY + y].addXgetTile + + this->buildingX + originX; + this->ConstructionGFXLayer[tile] + = (short)this->buildingRotationRelatedValue + (short)GMFirstPictureIndex::instance[6] + 147; + if (skipPlacementFailChecks != 0) { + this->MiscDisplayLayer[tile] |= 0x8000; + } + j++; + if (j >= this->constructionTileCount) { + break; + } + } + } else if (command == OpenSHC::Commands::M_MAPPER_TUNNELERS_GUILD) { + y = originY + 5; + int j = 0; + while (true) { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::getBuildingSizeIndexMappingData, this)(j, 5); + int const tile = DAT_ViewportRenderState::instance.translationMatrix[this->buildingY + y].addXgetTile + + this->buildingX + originX; + this->ConstructionGFXLayer[tile] + = (short)this->buildingRotationRelatedValue + (short)GMFirstPictureIndex::instance[6] + 172; + if (skipPlacementFailChecks != 0) { + this->MiscDisplayLayer[tile] |= 0x8000; + } + j++; + if (j >= this->constructionTileCount) { + break; + } + } + } else if (command == OpenSHC::Commands::M_MAPPER_OIL_SMELTER) { + y = originY + 4; + int j = 0; + while (true) { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::getBuildingSizeIndexMappingData, this)(j, 4); + int const tile = DAT_ViewportRenderState::instance.translationMatrix[this->buildingY + y].addXgetTile + + this->buildingX + originX; + this->ConstructionGFXLayer[tile] + = (short)this->buildingRotationRelatedValue + (short)GMFirstPictureIndex::instance[6] + 296; + if (skipPlacementFailChecks != 0) { + this->MiscDisplayLayer[tile] |= 0x8000; + } + j++; + if (j >= this->constructionTileCount) { + break; + } + } + } else if (command == OpenSHC::Commands::M_MAPPER_KEEP1) { + if (this->mapOrientation == 2) { + keepSpriteVariant = 1; + } + int const tile1 = DAT_ViewportRenderState::instance + .translationMatrix[DAT_TerrainDefinedData::instance.field_0x264[0][0].y + originY] + .addXgetTile + + DAT_TerrainDefinedData::instance.field_0x264[0][0].x + originX; + this->ConstructionGFXLayer[tile1] = GMFirstPictureIndex::instance[52] + keepSpriteVariant + 1315; + if (skipPlacementFailChecks != 0) { + this->MiscDisplayLayer[tile1] |= 0x8000; + } + int const tile2 = DAT_ViewportRenderState::instance + .translationMatrix[DAT_TerrainDefinedData::instance.field_0x264[0][1].y + originY] + .addXgetTile + + DAT_TerrainDefinedData::instance.field_0x264[0][1].x + originX; + this->ConstructionGFXLayer[tile2] = GMFirstPictureIndex::instance[52] + keepSpriteVariant + 1326; + if (skipPlacementFailChecks != 0) { + this->MiscDisplayLayer[tile2] |= 0x8000; + } + int const tile3 = DAT_ViewportRenderState::instance + .translationMatrix[DAT_TerrainDefinedData::instance.field_0x264[0][2].y + originY] + .addXgetTile + + DAT_TerrainDefinedData::instance.field_0x264[0][2].x + originX; + this->ConstructionGFXLayer[tile3] = GMFirstPictureIndex::instance[52] + keepSpriteVariant + 1319; + if (skipPlacementFailChecks != 0) { + this->MiscDisplayLayer[tile3] |= 0x8000; + } + int const outbuildingX + = originX + DAT_TerrainDefinedData::instance.keepOutbuildingOffsets_0x384[0][0].xOffset; + int const outbuildingY + = originY + DAT_TerrainDefinedData::instance.keepOutbuildingOffsets_0x384[0][0].yOffset; + int j = 0; + while (true) { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::getBuildingSizeIndexMappingData, this)(j, 7); + int const tile + = DAT_ViewportRenderState::instance.translationMatrix[this->buildingY + outbuildingY].addXgetTile + + this->buildingX + outbuildingX; + this->ConstructionGFXLayer[tile] + = (short)this->buildingRotationRelatedValue + (short)GMFirstPictureIndex::instance[6] + 23; + if (skipPlacementFailChecks != 0) { + this->MiscDisplayLayer[tile] |= 0x8000; + } + j++; + if (j >= this->constructionTileCount) { + break; + } + } + int const outbuilding2X + = DAT_TerrainDefinedData::instance.keepOutbuildingOffsets_0x3e4[0][0].xOffset + originX; + int const outbuilding2Y + = DAT_TerrainDefinedData::instance.keepOutbuildingOffsets_0x3e4[0][0].yOffset + originY; + j = 0; + while (true) { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::getBuildingSizeIndexMappingData, this)(j, 5); + int const tile + = DAT_ViewportRenderState::instance.translationMatrix[this->buildingY + outbuilding2Y].addXgetTile + + this->buildingX + outbuilding2X; + this->ConstructionGFXLayer[tile] + = (short)this->buildingRotationRelatedValue + (short)GMFirstPictureIndex::instance[7] + 25; + if (skipPlacementFailChecks != 0) { + this->MiscDisplayLayer[tile] |= 0x8000; + } + j++; + if (j >= this->constructionTileCount) { + break; + } + } + } else if (command == OpenSHC::Commands::M_MAPPER_KEEP2 || command == OpenSHC::Commands::M_MAPPER_KEEP3) { + if (this->mapOrientation == 2) { + keepSpriteVariant = 1; + } + int const keepIndex = buildingCommand - OpenSHC::Commands::M_MAPPER_KEEP1; + int const tile1 + = DAT_ViewportRenderState::instance + .translationMatrix[DAT_TerrainDefinedData::instance.field_0x264[keepIndex][0].y + originY] + .addXgetTile + + DAT_TerrainDefinedData::instance.field_0x264[keepIndex][0].x + originX; + this->ConstructionGFXLayer[tile1] = GMFirstPictureIndex::instance[52] + keepSpriteVariant + 1313; + if (skipPlacementFailChecks != 0) { + this->MiscDisplayLayer[tile1] |= 0x8000; + } + int const tile2 + = DAT_ViewportRenderState::instance + .translationMatrix[DAT_TerrainDefinedData::instance.field_0x264[keepIndex][1].y + originY] + .addXgetTile + + DAT_TerrainDefinedData::instance.field_0x264[keepIndex][1].x + originX; + this->ConstructionGFXLayer[tile2] = GMFirstPictureIndex::instance[52] + keepSpriteVariant + 1321; + if (skipPlacementFailChecks != 0) { + this->MiscDisplayLayer[tile2] |= 0x8000; + } + int const tile3 + = DAT_ViewportRenderState::instance + .translationMatrix[DAT_TerrainDefinedData::instance.field_0x264[keepIndex][2].y + originY] + .addXgetTile + + DAT_TerrainDefinedData::instance.field_0x264[keepIndex][2].x + originX; + this->ConstructionGFXLayer[tile3] = GMFirstPictureIndex::instance[52] + keepSpriteVariant + 1317; + if (skipPlacementFailChecks != 0) { + this->MiscDisplayLayer[tile3] |= 0x8000; + } + int const outbuildingX + = originX + DAT_TerrainDefinedData::instance.keepOutbuildingOffsets_0x384[keepIndex][0].xOffset; + int const outbuildingY + = originY + DAT_TerrainDefinedData::instance.keepOutbuildingOffsets_0x384[keepIndex][0].yOffset; + int j = 0; + while (true) { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::getBuildingSizeIndexMappingData, this)(j, 7); + int const tile + = DAT_ViewportRenderState::instance.translationMatrix[this->buildingY + outbuildingY].addXgetTile + + this->buildingX + outbuildingX; + this->ConstructionGFXLayer[tile] + = (short)this->buildingRotationRelatedValue + (short)GMFirstPictureIndex::instance[6] + 23; + if (skipPlacementFailChecks != 0) { + this->MiscDisplayLayer[tile] |= 0x8000; + } + j++; + if (j >= this->constructionTileCount) { + break; + } + } + int const outbuilding2X + = DAT_TerrainDefinedData::instance.keepOutbuildingOffsets_0x3e4[keepIndex][0].xOffset + originX; + int const outbuilding2Y + = DAT_TerrainDefinedData::instance.keepOutbuildingOffsets_0x3e4[keepIndex][0].yOffset + originY; + j = 0; + while (true) { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::getBuildingSizeIndexMappingData, this)(j, 5); + int const tile + = DAT_ViewportRenderState::instance.translationMatrix[this->buildingY + outbuilding2Y].addXgetTile + + this->buildingX + outbuilding2X; + this->ConstructionGFXLayer[tile] + = (short)this->buildingRotationRelatedValue + (short)GMFirstPictureIndex::instance[7] + 25; + if (skipPlacementFailChecks != 0) { + this->MiscDisplayLayer[tile] |= 0x8000; + } + j++; + if (j >= this->constructionTileCount) { + break; + } + } + } + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/setLand.cpp b/src/OpenSHC/Map/TileMapState/setLand.cpp new file mode 100644 index 0000000000..66ed92db07 --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/setLand.cpp @@ -0,0 +1,91 @@ +#include "../TileMapState.func.hpp" + +#include "OpenSHC/Map/Navigation/PathFindingState.func.hpp" + +#include "OpenSHC/Globals/DAT_PathFindingState.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::L2_PLATEAU_HIGH; + using OpenSHC::Map::LogicHelpers::L2_PLATEAU_MEDIUM; + using OpenSHC::Map::LogicHelpers::L_BORDER; + using OpenSHC::Map::LogicHelpers::L_BORDER_EDGE; + using OpenSHC::Map::LogicHelpers::L_BUILDING; + using OpenSHC::Map::LogicHelpers::L_CLIMBABLE; + using OpenSHC::Map::LogicHelpers::L_DEFAULT_EARTH_OR_TEXTURE; + 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; + using OpenSHC::Map::LogicHelpers::L_KEEP_NON_MANOR_HOUSE; + 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_ROCKY; + using OpenSHC::Map::LogicHelpers::L_STOCKPILEUnk; + using OpenSHC::Map::LogicHelpers::L_TREE; + using OpenSHC::Map::LogicHelpers::L_UNKNOWN_WALL_RELATED; + using OpenSHC::Map::LogicHelpers::L_WALL_OR_GATEHOUSE; + + // FUNCTION: STRONGHOLDCRUSADER 0x00502390 + void TileMapState::setLand(int tile, uint y, uint brushType) + { + int const brushSize = DAT_TerrainDefinedData::instance.BrushSizeArray[brushType]; + int const baseTile = tile; + uint const baseY = y; + for (int index = 0; index < brushSize; index++) { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::getTileForBrush, this)( + 1, index, &tile, (int*)&y, baseTile, baseY); + int const brushTile = *(int volatile*)&tile; + if ((this->LogicLayer[brushTile] & (L_BORDER | L_BORDER_EDGE)) != 0) { + continue; + } + if ((this->LogicLayer[brushTile] & (L_WALL_OR_GATEHOUSE | L_BUILDING | L_KEEP_NON_MANOR_HOUSE)) != 0) { + continue; + } + if (this->BuildingLayer[brushTile] != 0) { + continue; + } + if ((this->LogicLayer[brushTile] & L_TREE) != 0) { + continue; + } + if ((this->LogicLayer[brushTile] & L_ROCKY) != 0 && this->OrganismLayer[brushTile] != 0) { + continue; + } + if (this->DefaultHeightLayer[brushTile] < 8) { + this->DefaultHeightLayer[brushTile] = 8; + } + this->HeightLayer[brushTile] = this->DefaultHeightLayer[brushTile]; + this->MiscDisplayLayer[brushTile] &= 0xffdf; + if ((this->LogicLayer[brushTile] & (L_MOAT | L_MOAT_DUG_OR_PLANNED)) != 0) { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::clearMoatDataAtTile, this)(brushTile + - DAT_ViewportRenderState::instance + .translationMatrix[DAT_ViewportRenderState::instance + .tileTranslationMatrix_YComponent[brushTile]] + .addXgetTile, + DAT_ViewportRenderState::instance.tileTranslationMatrix_YComponent[brushTile]); + this->LogicLayer[brushTile] &= ~(L_MOAT | L_MOAT_DUG_OR_PLANNED); + } + this->LogicLayer[brushTile] + &= (L_STOCKPILEUnk | L_PLAIN1_AND_FARM | L_BORDER | L_BORDER_EDGE | L_BUILDING | L_MOAT_DUG_OR_PLANNED + | L_DEFAULT_EARTH_OR_TEXTURE | L_UNKNOWN_WALL_RELATED | L_CLIMBABLE | L_FARM_FIELD_WHEAT + | L_FARM_FIELD_HOP | L_FARM_FIELD_APPLE | L_FARM_FIELD_DAIRY | L_KEEP_NON_MANOR_HOUSE | L_MOAT); + this->MiscDisplayLayer[brushTile] &= 0xf83f; + this->Logic2Layer[brushTile] = 0; + if (DAT_TileMapState::instance.HeightLayer[brushTile] == 80) { + this->Logic2Layer[brushTile] = L2_PLATEAU_MEDIUM; + } else if (DAT_TileMapState::instance.HeightLayer[brushTile] == 130) { + this->Logic2Layer[brushTile] = L2_PLATEAU_HIGH; + } + this->WallOwnerLayer[brushTile] = 0; + MACRO_CALL_MEMBER(OpenSHC::Map::Navigation::PathFindingState_Func::updatePathLinkagesInAllEightDirections, + DAT_PathFindingState::ptr)(y, brushTile); + } + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/setMiscDisplayLayer.cpp b/src/OpenSHC/Map/TileMapState/setMiscDisplayLayer.cpp new file mode 100644 index 0000000000..95fecbd9a0 --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/setMiscDisplayLayer.cpp @@ -0,0 +1,126 @@ +#include "../TileMapState.func.hpp" + +#include "OpenSHC/Map/Buildings/BuildingType.hpp" + +#include "OpenSHC/Globals/DAT_BuildingsState.hpp" +#include "OpenSHC/Globals/DAT_TerrainDefinedData.hpp" +#include "OpenSHC/Globals/DAT_ViewportRenderState.hpp" + +namespace OpenSHC { +namespace Map { + + // FUNCTION: STRONGHOLDCRUSADER 0x004FB0C0 + void TileMapState::setMiscDisplayLayer(int buildingID) + { + int const rotation = this->mapOrientation / 2; + if (DAT_BuildingsState::instance.buildings[buildingID].unknownStockpileOrSignpostRelated == 0) { + return; + } + + int const x = DAT_BuildingsState::instance.buildings[buildingID].x; + int const y = DAT_BuildingsState::instance.buildings[buildingID].y; + int const size = DAT_BuildingsState::instance.buildings[buildingID].widthOrHeight; + OpenSHC::Map::Buildings::BuildingTypeShort const type + = DAT_BuildingsState::instance.buildings[buildingID].buildingType; + int const typeInt = type; + int workerTile = DAT_TerrainDefinedData::instance.orientationRelativeBuildingTileOffset1[rotation + size * 4]; + int displayTile = DAT_TerrainDefinedData::instance.orientationRelativeBuildingTileOffset2[rotation + size * 4]; + int extraTile = -1; + switch (typeInt) { + case OpenSHC::Map::Buildings::BT_IRONMINE: + case OpenSHC::Map::Buildings::BT_PITCHRIG: + case OpenSHC::Map::Buildings::BT_HUNTERSHUT: + case OpenSHC::Map::Buildings::BT_BLACKSMITH: + case OpenSHC::Map::Buildings::BT_BREWERY: + case OpenSHC::Map::Buildings::BT_OILSMELTER: + case OpenSHC::Map::Buildings::BT_STABLES: + case OpenSHC::Map::Buildings::BT_MANORHOUSE: + case OpenSHC::Map::Buildings::BT_KEEPFOUR: + case OpenSHC::Map::Buildings::BT_KEEPFIVE: + case OpenSHC::Map::Buildings::BT_TUNNEL: + case OpenSHC::Map::Buildings::BT_FIREBALLISTA: + case OpenSHC::Map::Buildings::BT_MAYPOLE: + case OpenSHC::Map::Buildings::BT_CATAPULT: + case OpenSHC::Map::Buildings::BT_TREBUCHET: + case OpenSHC::Map::Buildings::BT_BATTERINGRAM: + case OpenSHC::Map::Buildings::BT_SIEGETOWER: + case OpenSHC::Map::Buildings::BT_SHIELD: + case OpenSHC::Map::Buildings::BT_UNKNOWN4: + // these carry the worker on the display tile and the display on the worker tile + displayTile = workerTile; + workerTile = DAT_TerrainDefinedData::instance.orientationRelativeBuildingTileOffset2[rotation + size * 4]; + break; + case OpenSHC::Map::Buildings::BT_DRAWBRIDGE: + workerTile = DAT_TerrainDefinedData::instance.orientationRelativeBuildingTileOffset1[rotation + size * 4]; + switch (rotation) { + case 0: + displayTile = 19; + extraTile = 6; + break; + case 1: + displayTile = 21; + extraTile = 8; + break; + case 2: + displayTile = 5; + extraTile = 18; + break; + case 3: + displayTile = 3; + extraTile = 16; + } + break; + case OpenSHC::Map::Buildings::BT_GALLOWS: + case OpenSHC::Map::Buildings::BT_GIBBET: + workerTile = -1; + } + + if (size <= 2 && type != OpenSHC::Map::Buildings::BT_GIBBET && type != OpenSHC::Map::Buildings::BT_GALLOWS) { + int const tile = DAT_ViewportRenderState::instance.translationMatrix[y].addXgetTile + x; + this->MiscDisplayLayer[tile] |= 4; + return; + } + + int clearIndex = 0; + while (true) { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::getBuildingSizeIndexMappingData, this)(clearIndex, size); + int const tile = DAT_ViewportRenderState::instance.translationMatrix[this->buildingY + y].addXgetTile + x + + this->buildingX; + this->MiscDisplayLayer[tile] &= ~0xc; + clearIndex++; + if (clearIndex >= this->constructionTileCount) { + break; + } + } + + int index = 0; + while (true) { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::getBuildingSizeIndexMappingData, this)(index, size); + int const tile = DAT_ViewportRenderState::instance.translationMatrix[this->buildingY + y].addXgetTile + x + + this->buildingX; + if (typeInt == OpenSHC::Map::Buildings::BT_GRANARY) { + this->MiscDisplayLayer[tile] |= 8; + } else if (typeInt == OpenSHC::Map::Buildings::BT_ARMORY) { + this->MiscDisplayLayer[tile] |= 8; + } else if (index == workerTile) { + this->MiscDisplayLayer[tile] |= 8; + // fixme: xPosition and the field after it are written as one int + *(int*)&DAT_BuildingsState::instance.buildings[buildingID].xPosition = tile; + } + if (index == displayTile) { + this->MiscDisplayLayer[tile] |= 4; + DAT_BuildingsState::instance.buildings[buildingID].miscDisplayLayerTile = tile; + } + if (index == extraTile) { + this->MiscDisplayLayer[tile] |= 0xc; + DAT_BuildingsState::instance.buildings[buildingID].miscDisplayLayerTile = tile; + } + index++; + if (index >= this->constructionTileCount) { + break; + } + } + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/setTerrain.cpp b/src/OpenSHC/Map/TileMapState/setTerrain.cpp new file mode 100644 index 0000000000..ad326ea05c --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/setTerrain.cpp @@ -0,0 +1,562 @@ +#include "../TileMapState.func.hpp" + +#include "OpenSHC/Map/Navigation/PathFindingState.func.hpp" +#include "OpenSHC/Rendering/ViewportRenderState.func.hpp" +#include "OpenSHC/Game/GameMode2.hpp" +#include "OpenSHC/Map/TileMapState/NeighbourFlagsGlobalAsm.hpp" + +#include "OpenSHC/Globals/DAT_GameCore.hpp" +#include "OpenSHC/Globals/DAT_GameSynchronyState.hpp" +#include "OpenSHC/Globals/DAT_PathFindingState.hpp" +#include "OpenSHC/Globals/DAT_TerrainDefinedData.hpp" +#include "OpenSHC/Globals/DAT_ViewportRenderState.hpp" + +// mask for the handwritten neighbour-flag macro; MASM has no "|", so this must be a literal +#define NEIGHBOUR_FLAGS_MASK_L2_BEACH 0x20 // L2_BEACH + +#pragma optimize("", off) + +namespace OpenSHC { +namespace Map { + + using OpenSHC::Game::GameMode2; + using OpenSHC::Map::LogicHelpers::L2_BEACH; + using OpenSHC::Map::LogicHelpers::L2_EARTH_AND_STONES; + using OpenSHC::Map::LogicHelpers::L2_MOAT_UNDUG; + using OpenSHC::Map::LogicHelpers::L2_NONE; + using OpenSHC::Map::LogicHelpers::L2_OASIS_GRASS; + using OpenSHC::Map::LogicHelpers::L2_SCRUB; + using OpenSHC::Map::LogicHelpers::L2_STONES_OR_DRIVEN_SANDUnk; + using OpenSHC::Map::LogicHelpers::L2_THICK_SCRUB; + using OpenSHC::Map::LogicHelpers::L_BORDER; + 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_NONE; + using OpenSHC::Map::LogicHelpers::L_OIL; + using OpenSHC::Map::LogicHelpers::L_PEBBLES; + using OpenSHC::Map::LogicHelpers::L_PLAIN1_AND_FARM; + using OpenSHC::Map::LogicHelpers::L_PLAIN2_AND_PITCH; + using OpenSHC::Map::LogicHelpers::L_RIVER; + using OpenSHC::Map::LogicHelpers::L_ROCKY; + using OpenSHC::Map::LogicHelpers::L_SEA; + using OpenSHC::Map::LogicHelpers::L_UNNAMED_0x40; + using OpenSHC::Map::LogicHelpers::L_WALL_OR_GATEHOUSE; + + // FUNCTION: STRONGHOLDCRUSADER 0x00512940 + void TileMapState::setTerrain(int playerID, int tile, uint yParam, uint brushType, Logic1 flags1, Logic2 flags2) + { + // Holds one expansion of the handwritten neighbour-flag macro (see NeighbourFlagsGlobalAsm.hpp), + // so the original was built without optimisation and the pragma above is needed to reproduce + // its frame-pointer, reload-per-access shape. The locals are declared together because at + // /Od the stack slots are observable and MSVC assigns them per function, not per scope. + // MSVC lays them out by a hash of their NAME (16 buckets, walked in order; inside one bucket + // the later declaration gets the higher slot), so these names and this order put them where + // the original has them (baseY at ebp-4 down to baseTile at ebp-0x1c). + // + // L_UNNAMED_0x40 is the one Logic1 bit the generated enum has no name for; the original both + // tests flags1 against it and sets it on the tile. + uint x; + int numTiles; + uint baseY; + int index; + int moatEnemyDistance; + int moatOpponentRange; + int baseTile; + + numTiles = DAT_TerrainDefinedData::instance.BrushSizeArray[brushType]; + baseTile = tile; + baseY = yParam; + if (DAT_GameSynchronyState::instance.currentGameMode != OpenSHC::Game::GM_SOLITARY) { + moatEnemyDistance = 15; + moatOpponentRange = 7; + } else { + moatEnemyDistance = 30; + moatOpponentRange = 30; + } + + if (DAT_GameCore::instance.gameMode_2 == OpenSHC::Game::GM_EDITOR && flags1 != L_MOAT + && (flags2 == L2_EARTH_AND_STONES || flags2 == L2_OASIS_GRASS || flags2 == L2_BEACH + || flags2 == L2_STONES_OR_DRIVEN_SANDUnk || flags2 == L2_SCRUB || flags2 == L2_THICK_SCRUB)) { + // this functions processes all oasis tiles + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::setLand, this)(tile, yParam, brushType); + } + if (flags1 == L_MOAT) { + numTiles = 9; + } + + for (index = 0; index < numTiles; index++) { + if (flags1 == L_MOAT) { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::getTileForBrush, this)( + 0, index, &tile, (int*)&yParam, baseTile, baseY); + } else { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::getTileForBrush, this)( + 1, index, &tile, (int*)&yParam, baseTile, baseY); + } + if ((this->LogicLayer[tile] & L_BORDER) != 0) { + continue; + } + + if (flags1 == L_SEA) { + if ((this->LogicLayer[tile] & (L_BUILDING | L_KEEP_NON_MANOR_HOUSE)) != 0) { + continue; + } + if (this->BuildingLayer[tile] != 0) { + continue; + } + if ((this->LogicLayer[tile] & L_WALL_OR_GATEHOUSE) != 0) { + continue; + } + if (this->OrganismLayer[tile] != 0) { + continue; + } + this->LogicLayer[tile] &= ~L_UNNAMED_0x40; + this->LogicLayer[tile] &= ~L_RIVER; + this->LogicLayer[tile] &= ~L_FORD; + this->LogicLayer[tile] &= ~L_ROCKY; + this->LogicLayer[tile] &= ~L_BOULDERS; + this->LogicLayer[tile] &= ~L_PEBBLES; + this->LogicLayer[tile] &= ~L_IRON; + this->LogicLayer[tile] &= ~L_MARSH; + this->LogicLayer[tile] &= ~L_OIL; + this->LogicLayer[tile] &= ~L_MOAT; + this->LogicLayer[tile] &= ~L_PLAIN2_AND_PITCH; + this->Logic2Layer[tile] = 0; + this->LogicLayer[tile] |= flags1; + this->HeightLayer[tile] = 0; + this->DefaultHeightLayer[tile] = 0; + this->forceUpdateLogicalAndMiscDisplayLayers = 1; + } else if (flags1 == L_RIVER || flags1 == L_FORD) { + if ((this->LogicLayer[tile] & (L_BUILDING | L_KEEP_NON_MANOR_HOUSE)) != 0) { + continue; + } + if (this->BuildingLayer[tile] != 0) { + continue; + } + if ((this->LogicLayer[tile] & L_WALL_OR_GATEHOUSE) != 0) { + continue; + } + if (this->OrganismLayer[tile] != 0) { + continue; + } + if (flags2 != L2_NONE && (this->LogicLayer[tile] & L_RIVER) == 0) { + continue; + } + this->LogicLayer[tile] &= ~L_UNNAMED_0x40; + if (flags1 == L_RIVER) { + this->LogicLayer[tile] &= ~L_FORD; + } else { + this->LogicLayer[tile] &= ~L_RIVER; + } + this->LogicLayer[tile] &= ~L_ROCKY; + this->LogicLayer[tile] &= ~L_BOULDERS; + this->LogicLayer[tile] &= ~L_PEBBLES; + this->LogicLayer[tile] &= ~L_IRON; + this->LogicLayer[tile] &= ~L_MARSH; + this->LogicLayer[tile] &= ~L_OIL; + this->LogicLayer[tile] &= ~L_MOAT; + this->LogicLayer[tile] &= ~L_SEA; + this->LogicLayer[tile] &= ~L_PLAIN2_AND_PITCH; + this->Logic2Layer[tile] = 0; + if (flags2 != L2_BEACH) { + this->WallGFXLayer[tile] = this->RandomLayer[tile] & 7; + } + if (flags2 == L2_OASIS_GRASS) { + this->Logic2Layer[tile] = L2_OASIS_GRASS; + } + if (flags2 == L2_BEACH) { + this->Logic2Layer[tile] = L2_BEACH; + this->WallGFXLayer[tile] = (DAT_GameCore::instance.mapTimeInTicks & 8) + + this->WallGFXLayer[tile] * 32 + (this->RandomLayer[tile] & 7) * 1024; + } + this->LogicLayer[tile] |= flags1; + if (this->HeightLayer[tile] >= 8) { + this->HeightLayer[tile] = this->DefaultHeightLayer[tile] - 8; + this->forceUpdateLogicalAndMiscDisplayLayers = 1; + } + } else if (flags1 == L_ROCKY) { + if ((this->LogicLayer[tile] & (L_BUILDING | L_KEEP_NON_MANOR_HOUSE)) != 0) { + continue; + } + if (this->BuildingLayer[tile] != 0) { + continue; + } + if ((this->LogicLayer[tile] & L_WALL_OR_GATEHOUSE) != 0) { + continue; + } + if ((this->LogicLayer[tile] & L_RIVER) != 0) { + continue; + } + if (this->OrganismLayer[tile] != 0) { + continue; + } + if ((this->LogicLayer[tile] & L_SEA) != 0) { + this->HeightLayer[tile] = 0; + this->DefaultHeightLayer[tile] = 0; + } else if (this->HeightLayer[tile] < 8) { + this->HeightLayer[tile] = 8; + this->DefaultHeightLayer[tile] = 8; + } + this->LogicLayer[tile] &= ~L_SEA; + this->LogicLayer[tile] &= ~L_RIVER; + this->LogicLayer[tile] &= ~L_FORD; + this->LogicLayer[tile] &= ~L_UNNAMED_0x40; + this->LogicLayer[tile] &= ~L_BOULDERS; + this->LogicLayer[tile] &= ~L_PEBBLES; + this->LogicLayer[tile] &= ~L_IRON; + this->LogicLayer[tile] &= ~L_MARSH; + this->LogicLayer[tile] &= ~L_OIL; + this->LogicLayer[tile] &= ~L_MOAT; + this->LogicLayer[tile] &= ~L_PLAIN2_AND_PITCH; + this->Logic2Layer[tile] = 0; + this->LogicLayer[tile] |= flags1; + } else if (flags1 == L_IRON) { + if ((this->LogicLayer[tile] & (L_BUILDING | L_KEEP_NON_MANOR_HOUSE)) != 0) { + continue; + } + if (this->BuildingLayer[tile] != 0) { + continue; + } + if ((this->LogicLayer[tile] & L_WALL_OR_GATEHOUSE) != 0) { + continue; + } + if ((this->LogicLayer[tile] & L_SEA) != 0) { + continue; + } + if ((this->LogicLayer[tile] & L_RIVER) != 0) { + continue; + } + if (this->OrganismLayer[tile] != 0) { + continue; + } + this->LogicLayer[tile] &= ~L_RIVER; + this->LogicLayer[tile] &= ~L_FORD; + this->LogicLayer[tile] &= ~L_ROCKY; + this->LogicLayer[tile] &= ~L_PEBBLES; + this->LogicLayer[tile] &= ~L_BOULDERS; + this->LogicLayer[tile] &= ~L_MARSH; + this->LogicLayer[tile] &= ~L_OIL; + this->LogicLayer[tile] &= ~L_MOAT; + this->LogicLayer[tile] &= ~L_PLAIN2_AND_PITCH; + this->Logic2Layer[tile] = 0; + this->LogicLayer[tile] |= flags1; + } else if (flags1 == L_PEBBLES) { + if ((this->LogicLayer[tile] & (L_BUILDING | L_KEEP_NON_MANOR_HOUSE)) != 0) { + continue; + } + if (this->BuildingLayer[tile] != 0) { + continue; + } + if ((this->LogicLayer[tile] & L_WALL_OR_GATEHOUSE) != 0) { + continue; + } + if ((this->LogicLayer[tile] & L_RIVER) != 0) { + continue; + } + if (this->OrganismLayer[tile] != 0) { + continue; + } + if ((this->LogicLayer[tile] & L_SEA) != 0) { + this->HeightLayer[tile] = 0; + this->DefaultHeightLayer[tile] = 0; + } else if (this->HeightLayer[tile] < 8) { + this->HeightLayer[tile] = 8; + this->DefaultHeightLayer[tile] = 8; + } + this->LogicLayer[tile] &= ~L_SEA; + this->LogicLayer[tile] &= ~L_UNNAMED_0x40; + this->LogicLayer[tile] &= ~L_RIVER; + this->LogicLayer[tile] &= ~L_FORD; + this->LogicLayer[tile] &= ~L_ROCKY; + this->LogicLayer[tile] &= ~L_BOULDERS; + this->LogicLayer[tile] &= ~L_IRON; + this->LogicLayer[tile] &= ~L_MARSH; + this->LogicLayer[tile] &= ~L_OIL; + this->LogicLayer[tile] &= ~L_MOAT; + this->LogicLayer[tile] &= ~L_PLAIN2_AND_PITCH; + this->Logic2Layer[tile] = 0; + this->LogicLayer[tile] |= flags1; + } else if (flags1 == L_MARSH) { + if ((this->LogicLayer[tile] & (L_BUILDING | L_KEEP_NON_MANOR_HOUSE)) != 0) { + continue; + } + if (this->BuildingLayer[tile] != 0) { + continue; + } + if ((this->LogicLayer[tile] & L_WALL_OR_GATEHOUSE) != 0) { + continue; + } + if ((this->LogicLayer[tile] & L_RIVER) != 0) { + continue; + } + if (this->OrganismLayer[tile] != 0) { + continue; + } + this->HeightLayer[tile] = 8; + this->DefaultHeightLayer[tile] = 8; + this->LogicLayer[tile] &= ~L_SEA; + this->LogicLayer[tile] &= ~L_UNNAMED_0x40; + this->LogicLayer[tile] &= ~L_RIVER; + this->LogicLayer[tile] &= ~L_FORD; + this->LogicLayer[tile] &= ~L_ROCKY; + this->LogicLayer[tile] &= ~L_BOULDERS; + this->LogicLayer[tile] &= ~L_IRON; + this->LogicLayer[tile] &= ~L_OIL; + this->LogicLayer[tile] &= ~L_MOAT; + this->LogicLayer[tile] &= ~L_PLAIN2_AND_PITCH; + this->Logic2Layer[tile] = 0; + this->LogicLayer[tile] |= flags1; + } else if (flags1 == L_OIL) { + if ((this->LogicLayer[tile] & (L_BUILDING | L_KEEP_NON_MANOR_HOUSE)) != 0) { + continue; + } + if (this->BuildingLayer[tile] != 0) { + continue; + } + if ((this->LogicLayer[tile] & L_WALL_OR_GATEHOUSE) != 0) { + continue; + } + if ((this->LogicLayer[tile] & L_RIVER) != 0) { + continue; + } + if (this->OrganismLayer[tile] != 0) { + continue; + } + if ((this->LogicLayer[tile] & L_MARSH) == 0) { + continue; + } + this->LogicLayer[tile] &= ~L_SEA; + this->LogicLayer[tile] &= ~L_UNNAMED_0x40; + this->LogicLayer[tile] &= ~L_RIVER; + this->LogicLayer[tile] &= ~L_FORD; + this->LogicLayer[tile] &= ~L_ROCKY; + this->LogicLayer[tile] &= ~L_BOULDERS; + this->LogicLayer[tile] &= ~L_IRON; + this->LogicLayer[tile] &= ~L_PLAIN2_AND_PITCH; + this->Logic2Layer[tile] = 0; + this->LogicLayer[tile] |= flags1; + } else if (flags1 == L_MOAT) { + if (this->OrganismLayer[tile] != 0) { + continue; + } + if ((this->LogicLayer[tile] & (L_BUILDING | L_KEEP_NON_MANOR_HOUSE)) != 0) { + continue; + } + if (this->BuildingLayer[tile] != 0) { + continue; + } + if ((this->LogicLayer[tile] & L_WALL_OR_GATEHOUSE) != 0) { + continue; + } + if ((this->LogicLayer[tile] & L_SEA) != 0) { + continue; + } + if ((this->LogicLayer[tile] & L_RIVER) != 0) { + continue; + } + if ((this->LogicLayer[tile] & L_FORD) != 0) { + continue; + } + if ((this->LogicLayer[tile] & L_ROCKY) != 0) { + continue; + } + if ((this->LogicLayer[tile] & L_BOULDERS) != 0) { + continue; + } + if ((this->LogicLayer[tile] & L_IRON) != 0) { + continue; + } + if ((this->LogicLayer[tile] & L_MARSH) != 0) { + continue; + } + if ((this->LogicLayer[tile] & L_PLAIN1_AND_FARM) != 0) { + continue; + } + x = MACRO_CALL_MEMBER(OpenSHC::Rendering::ViewportRenderState_Func::computeTileXOffset, + &DAT_ViewportRenderState::instance)(tile, yParam); + if (DAT_GameCore::instance.gameMode_2 != OpenSHC::Game::GM_EDITOR + && DAT_GameCore::instance.gameMode_2 != OpenSHC::Game::GM_SIEGE_THAT + && MACRO_CALL_MEMBER(OpenSHC::Map::Navigation::PathFindingState_Func::isEnemyTooCloseUnk, + &DAT_PathFindingState::instance)(playerID, x, yParam, moatEnemyDistance) + != FALSE) { + continue; + } + if (DAT_GameSynchronyState::instance.currentGameMode != OpenSHC::Game::GM_SOLITARY) { + if (MACRO_CALL_MEMBER(OpenSHC::Map::Navigation::PathFindingState_Func::isOpponentBuildingInRange, + &DAT_PathFindingState::instance)(playerID, x, yParam, moatOpponentRange, -1, -1, + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::getCastleBuildRangeForMapSize, this)() + + 5) + != 0) { + continue; + } + } + if ((this->LogicLayer[tile] & L_MOAT) != 0 && flags2 != L2_MOAT_UNDUG) { + continue; + } + if (this->HeightLayer[tile] > 12) { + continue; + } + if (flags2 == L2_NONE) { + if ((this->LogicLayer[tile] & L_MOAT_DUG_OR_PLANNED) == 0 + && MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::createMoatData, this)( + playerID, x, yParam, 0) + != 0) { + this->LogicLayer[tile] |= L_MOAT_DUG_OR_PLANNED; + } + } else if (flags2 == L2_SCRUB) { + if ((this->LogicLayer[tile] & L_MOAT_DUG_OR_PLANNED) != 0) { + this->LogicLayer[tile] &= ~L_MOAT_DUG_OR_PLANNED; + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::clearMoatDataAtTile, this)(x, yParam); + } + } else { + this->HeightLayer[tile] = 8; + this->LogicLayer[tile] &= ~L_PLAIN2_AND_PITCH; + this->Logic2Layer[tile] = 0; + if (flags2 == L2_EARTH_AND_STONES) { + if (MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::createMoatData, this)( + playerID, x, yParam, 1) + != 0) { + this->LogicLayer[tile] &= ~L_MOAT_DUG_OR_PLANNED; + this->LogicLayer[tile] |= L_MOAT; + this->HeightLayer[tile] = 0; + } + } else if (flags2 == L2_MOAT_UNDUG) { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::clearMoatDataAtTile, this)(x, yParam); + this->HeightLayer[tile] = 8; + this->LogicLayer[tile] &= ~L_MOAT_DUG_OR_PLANNED; + this->LogicLayer[tile] &= ~L_MOAT; + } + } + } else if (flags2 == L2_EARTH_AND_STONES || flags2 == L2_OASIS_GRASS || flags2 == L2_BEACH + || flags2 == L2_STONES_OR_DRIVEN_SANDUnk || flags2 == L2_SCRUB || flags2 == L2_THICK_SCRUB) { + if ((this->LogicLayer[tile] & (L_BUILDING | L_KEEP_NON_MANOR_HOUSE)) != 0) { + continue; + } + if (this->BuildingLayer[tile] != 0) { + continue; + } + if ((this->LogicLayer[tile] & L_WALL_OR_GATEHOUSE) != 0) { + continue; + } + if ((this->LogicLayer[tile] & (L_SEA | L_RIVER | L_FORD | L_MARSH | L_MOAT)) != 0) { + continue; + } + if (this->OrganismLayer[tile] != 0) { + continue; + } + if (flags2 == L2_EARTH_AND_STONES) { + if ((this->LogicLayer[tile] & L_IRON) != 0) { + continue; + } + if ((this->LogicLayer[tile] & L_BOULDERS) != 0) { + continue; + } + if ((this->Logic2Layer[tile] & L2_BEACH) != 0) { + continue; + } + MACRO_NEIGHBOUR_FLAGS_GLOBAL_TERRAIN_8(1, NEIGHBOUR_FLAGS_MASK_L2_BEACH) + if (this->bitFlag != 0) { + continue; + } + } + + this->MiscDisplayLayer[tile] = (short)this->MiscDisplayLayer[tile] & ~0x20; + // the original tests each kept terrain on its own and leaves the height alone for it + if (flags2 == L2_STONES_OR_DRIVEN_SANDUnk) { + } else if (flags2 == L2_SCRUB) { + } else if (flags2 == L2_THICK_SCRUB) { + } else if (flags2 == L2_EARTH_AND_STONES) { + } else if (flags2 == L2_OASIS_GRASS) { + } else if (flags2 == L2_BEACH) { + this->HeightLayer[tile] = 0; + this->DefaultHeightLayer[tile] = 0; + } else { + this->HeightLayer[tile] = 8; + this->DefaultHeightLayer[tile] = 8; + } + this->LogicLayer[tile] &= ~L_SEA; + this->LogicLayer[tile] &= ~L_UNNAMED_0x40; + this->LogicLayer[tile] &= ~L_RIVER; + this->LogicLayer[tile] &= ~L_FORD; + this->LogicLayer[tile] &= ~L_ROCKY; + this->LogicLayer[tile] &= ~L_BOULDERS; + this->LogicLayer[tile] &= ~L_IRON; + this->LogicLayer[tile] &= ~L_MARSH; + this->LogicLayer[tile] &= ~L_OIL; + this->LogicLayer[tile] &= ~L_MOAT; + if (flags2 != L2_EARTH_AND_STONES) { + this->LogicLayer[tile] &= ~L_PLAIN2_AND_PITCH; + } + this->Logic2Layer[tile] = (byte)flags2; + } else if (flags1 == L_BOULDERS) { + if ((this->LogicLayer[tile] & (L_BUILDING | L_KEEP_NON_MANOR_HOUSE)) != 0) { + continue; + } + if (this->BuildingLayer[tile] != 0) { + continue; + } + if ((this->LogicLayer[tile] & L_WALL_OR_GATEHOUSE) != 0) { + continue; + } + if ((this->LogicLayer[tile] & L_RIVER) != 0) { + continue; + } + if (this->OrganismLayer[tile] != 0) { + continue; + } + if ((this->LogicLayer[tile] & L_SEA) != 0) { + this->HeightLayer[tile] = 0; + this->DefaultHeightLayer[tile] = 0; + } else if (this->HeightLayer[tile] < 8) { + this->HeightLayer[tile] = 8; + this->DefaultHeightLayer[tile] = 8; + } + this->LogicLayer[tile] &= ~L_SEA; + this->LogicLayer[tile] &= ~L_UNNAMED_0x40; + this->LogicLayer[tile] &= ~L_RIVER; + this->LogicLayer[tile] &= ~L_FORD; + this->LogicLayer[tile] &= ~L_ROCKY; + this->LogicLayer[tile] &= ~L_PEBBLES; + this->LogicLayer[tile] &= ~L_IRON; + this->LogicLayer[tile] &= ~L_MARSH; + this->LogicLayer[tile] &= ~L_OIL; + this->LogicLayer[tile] &= ~L_MOAT; + this->LogicLayer[tile] &= ~L_PLAIN2_AND_PITCH; + this->Logic2Layer[tile] = 0; + this->LogicLayer[tile] |= flags1; + } else if (flags1 == L_UNNAMED_0x40) { + if ((this->LogicLayer[tile] & L_SEA) == 0) { + continue; + } + if ((this->LogicLayer[tile] & (L_BUILDING | L_KEEP_NON_MANOR_HOUSE)) != 0) { + continue; + } + if (this->BuildingLayer[tile] != 0) { + continue; + } + if ((this->LogicLayer[tile] & L_WALL_OR_GATEHOUSE) != 0) { + continue; + } + if (this->OrganismLayer[tile] != 0) { + continue; + } + this->LogicLayer[tile] |= flags1; + } + + if (flags1 != L_NONE) { + MACRO_CALL_MEMBER( + OpenSHC::Map::Navigation::PathFindingState_Func::updatePathLinkagesInAllEightDirections, + &DAT_PathFindingState::instance)(yParam, tile); + } + } + } + +} +} + +#pragma optimize("", on) diff --git a/src/OpenSHC/Map/TileMapState/setXYBasedOnMoatID.cpp b/src/OpenSHC/Map/TileMapState/setXYBasedOnMoatID.cpp new file mode 100644 index 0000000000..c643a4a60f --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/setXYBasedOnMoatID.cpp @@ -0,0 +1,52 @@ +#include "../TileMapState.func.hpp" + +#include "OpenSHC/Globals/DAT_TerrainDefinedData.hpp" +#include "OpenSHC/Globals/DAT_ViewportRenderState.hpp" + +namespace OpenSHC { +namespace Map { + + // FUNCTION: STRONGHOLDCRUSADER 0x005003D0 + int TileMapState::setXYBasedOnMoatID(int moatID, int mode, uint x, uint y) + { + bool const isOnMap + = x <= 399 && y <= 399 && DAT_ViewportRenderState::instance.DAT_BinaryTileMap400x400[y * 400 + x] != 0; + if (!isOnMap) { + return 0; + } + + int const tile = DAT_ViewportRenderState::instance.translationMatrix[y].addXgetTile + x; + int const tilePathConnection = (short)this->PathConnectionLayer[tile]; + int const tileHeight = this->HeightLayer[tile]; + if (mode == 1) { + this->ALG_MoatXResult = this->moats[moatID].x; + this->ALG_MoatYResult = this->moats[moatID].y; + return DAT_ViewportRenderState::instance.translationMatrix[this->ALG_MoatYResult].addXgetTile + + this->ALG_MoatXResult; + } + if (mode != 2) { + return 0; + } + + for (int direction = 0; direction < countof(this->directionTranslationMatrix[0]); direction++) { + int const neighbour + = this->directionTranslationMatrix[this->moats[moatID].y][direction] + this->moats[moatID].tile; + if (this->HeightLayer[neighbour] > tileHeight + 16) { + continue; + } + if ((short)this->PathConnectionLayer[neighbour] != tilePathConnection) { + continue; + } + + this->someMoatTile = this->moats[moatID].tile; + this->ALG_MoatXResult = this->moats[moatID].x + + DAT_TerrainDefinedData::instance.clockwiseCardinalTranslationMatrix[direction].int_.xOffset; + this->ALG_MoatYResult = this->moats[moatID].y + + DAT_TerrainDefinedData::instance.clockwiseCardinalTranslationMatrix[direction].int_.yOffset; + return neighbour; + } + return 0; + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/setupBuildingSizeIndexMappingForBuildingWithSize.cpp b/src/OpenSHC/Map/TileMapState/setupBuildingSizeIndexMappingForBuildingWithSize.cpp new file mode 100644 index 0000000000..cdc99ce27b --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/setupBuildingSizeIndexMappingForBuildingWithSize.cpp @@ -0,0 +1,128 @@ +#include "../TileMapState.func.hpp" + +#include "OpenSHC/IO/LowLevelMemory.func.hpp" + +#include "OpenSHC/Globals/DAT_BuildingSizeIndexMapping.hpp" +#include "OpenSHC/Globals/DAT_LowLevelMemory.hpp" + +namespace OpenSHC { +namespace Map { + + // FUNCTION: STRONGHOLDCRUSADER 0x004F9590 + void TileMapState::setupBuildingSizeIndexMappingForBuildingWithSize(int buildingSize) + { + int const tileCount = buildingSize * buildingSize; + int const maxCoord = buildingSize - 1; + MACRO_CALL_MEMBER(OpenSHC::IO::LowLevelMemory_Func::fillMemory_ByteValue, DAT_LowLevelMemory::ptr)( + 4056, 0, DAT_BuildingSizeIndexMapping::instance + buildingSize); + if (buildingSize <= 1) { + return; + } + + // pass 0: the unrotated footprint, in reading order + for (int y = 0; y < buildingSize; y++) { + for (int x = 0; x < buildingSize; x++) { + DAT_BuildingSizeIndexMapping::instance[buildingSize][y * buildingSize + x][0] = x; + DAT_BuildingSizeIndexMapping::instance[buildingSize][y * buildingSize + x][1] = y; + } + } + + // passes 1 to 4: the same tiles walked diagonally, one pass per rotation + int const last = tileCount - 1; + int x = 0; + int y = 0; + int run; + int index = last; + int diagonal; + for (diagonal = 0, run = 1; diagonal < buildingSize * 2 - 1; diagonal++) { + for (int step = 0; step < run; step++) { + DAT_BuildingSizeIndexMapping::instance[buildingSize][y * buildingSize + x][2] = index; + index--; + if (step < run - 1) { + x--; + y++; + } + } + if (diagonal < buildingSize - 1) { + run++; + x = diagonal + 1; + y = 0; + } else { + run--; + x = maxCoord; + y = 1 - maxCoord + diagonal; + } + } + + x = maxCoord; + y = 0; + index = last; + for (diagonal = 0, run = 1; diagonal < buildingSize * 2 - 1; diagonal++) { + for (int step = 0; step < run; step++) { + DAT_BuildingSizeIndexMapping::instance[buildingSize][y * buildingSize + x][3] = index; + index--; + if (step < run - 1) { + x--; + y--; + } + } + if (diagonal < maxCoord) { + run++; + x = maxCoord; + y = diagonal + 1; + } else { + run--; + x = maxCoord + (buildingSize - 2) - diagonal; + y = maxCoord; + } + } + + x = maxCoord; + y = maxCoord; + int secondIndex = last; + for (diagonal = 0, run = 1; diagonal < buildingSize * 2 - 1; diagonal++) { + for (int step = 0; step < run; step++) { + DAT_BuildingSizeIndexMapping::instance[buildingSize][y * buildingSize + x][4] = secondIndex; + secondIndex--; + if (step < run - 1) { + x++; + y--; + } + } + if (diagonal < maxCoord) { + run++; + x = buildingSize - 2 - diagonal; + y = maxCoord; + } else { + run--; + x = 0; + y = (buildingSize - 2 - diagonal) + maxCoord; + } + } + + x = 0; + y = maxCoord; + secondIndex = last; + for (diagonal = 0, run = 1; diagonal < buildingSize * 2 - 1; diagonal++) { + for (int step = 0; step < run; step++) { + DAT_BuildingSizeIndexMapping::instance[buildingSize][y * buildingSize + x][5] = secondIndex; + secondIndex--; + if (step < run - 1) { + x++; + y++; + } + } + if (diagonal < maxCoord) { + run++; + x = 0; + y = buildingSize - 2 - diagonal; + } else { + run--; + x = 1 - maxCoord + diagonal; + y = 0; + } + } + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/setupMovementDirectionTranslationMatrix.cpp b/src/OpenSHC/Map/TileMapState/setupMovementDirectionTranslationMatrix.cpp new file mode 100644 index 0000000000..6fb21791ab --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/setupMovementDirectionTranslationMatrix.cpp @@ -0,0 +1,61 @@ +#include "../TileMapState.func.hpp" + +namespace OpenSHC { +namespace Map { + + // FUNCTION: STRONGHOLDCRUSADER 0x004F6B20 + void TileMapState::setupMovementDirectionTranslationMatrix() + { + // the map is a diamond: rows grow by two tiles up to the two middle rows 199 and 200, then shrink + int width = 2; + for (int row = 0, offsetIndex = 0; width < 400; row++, width += 2, offsetIndex += 8) { + int* const offsets = this->directionTranslationMatrix[0] + offsetIndex; + offsets[0] = 1 - width; + offsets[1] = 2 - width; + offsets[2] = 1; + offsets[3] = width + 2; + offsets[4] = width + 1; + offsets[5] = width; + offsets[6] = -1; + offsets[7] = -width; + this->yArray1[row] = width; + } + + int (&upperMiddle)[8] = this->directionTranslationMatrix[199]; + upperMiddle[0] = 1 - width; + upperMiddle[1] = 2 - width; + upperMiddle[2] = 1; + upperMiddle[3] = width + 1; + upperMiddle[4] = width; + upperMiddle[5] = width - 1; + upperMiddle[6] = -1; + upperMiddle[7] = -width; + this->yArray1[199] = width; + int (&lowerMiddle)[8] = this->directionTranslationMatrix[200]; + lowerMiddle[0] = -width; + lowerMiddle[1] = 1 - width; + lowerMiddle[2] = 1; + lowerMiddle[3] = width; + lowerMiddle[4] = width - 1; + lowerMiddle[5] = width - 2; + lowerMiddle[6] = -1; + lowerMiddle[7] = -1 - width; + this->yArray1[200] = width; + + for (int row = 201, lowerWidth = width - 2, offsetIndex = 201 * 8; lowerWidth > 0; + row++, lowerWidth -= 2, offsetIndex += 8) { + int* const offsets = this->directionTranslationMatrix[0] + offsetIndex; + offsets[0] = -1 - lowerWidth; + offsets[1] = -lowerWidth; + offsets[2] = 1; + offsets[3] = lowerWidth; + offsets[4] = lowerWidth - 1; + offsets[5] = lowerWidth - 2; + offsets[6] = -1; + offsets[7] = -2 - lowerWidth; + this->yArray1[row] = lowerWidth; + } + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/spawnFloatingNumberAroundTile.cpp b/src/OpenSHC/Map/TileMapState/spawnFloatingNumberAroundTile.cpp new file mode 100644 index 0000000000..70b19dd4eb --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/spawnFloatingNumberAroundTile.cpp @@ -0,0 +1,43 @@ +#include "../TileMapState.func.hpp" + +#include "OpenSHC/Rendering/ViewportRenderState.func.hpp" +#include "OpenSHC/IO/Graphics/GmID.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_BUILDING; + using OpenSHC::Map::LogicHelpers::L_KEEP_NON_MANOR_HOUSE; + using OpenSHC::Map::LogicHelpers::L_WALL_OR_GATEHOUSE; + + // FUNCTION: STRONGHOLDCRUSADER 0x004FB3F0 + void TileMapState::spawnFloatingNumberAroundTile(int x, int y, int type) + { + int const radius = type / 2; + int index = 0; + // x and y become the corner of the square the offset table is measured from + x -= radius; + y -= radius; + for (int xOffset = DAT_TerrainDefinedData::instance.field_0x2764[radius * 60 + index].x; xOffset != -1; + xOffset = DAT_TerrainDefinedData::instance.field_0x2764[radius * 60 + index].x) { + int const yOffset = DAT_TerrainDefinedData::instance.field_0x2764[radius * 60 + index].y; + int const tile = DAT_ViewportRenderState::instance.translationMatrix[yOffset + y].addXgetTile + xOffset + x; + if ((this->LogicLayer[tile] & (L_WALL_OR_GATEHOUSE | L_BUILDING | L_KEEP_NON_MANOR_HOUSE)) == 0 + && (index & 1) == 0) { + int const frame + = (DAT_TileMapState::instance.cursorOverlayAnimationFrame + yOffset + xOffset + index + x + y - 1) + % 16; + MACRO_CALL_MEMBER(OpenSHC::Rendering::ViewportRenderState_Func::createFloatingLayerElement, + DAT_ViewportRenderState::ptr)( + OpenSHC::IO::Graphics::GID_FLOATS_NEW, frame + 229, 16, 14, tile, 0x100021); + } + index++; + } + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/stampBuildingOntoTileMap.cpp b/src/OpenSHC/Map/TileMapState/stampBuildingOntoTileMap.cpp new file mode 100644 index 0000000000..a80c06233b --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/stampBuildingOntoTileMap.cpp @@ -0,0 +1,44 @@ +#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 { + + using OpenSHC::Map::LogicHelpers::L_BUILDING; + + // FUNCTION: STRONGHOLDCRUSADER 0x00507060 + void TileMapState::stampBuildingOntoTileMap(int playerID, uint x, uint y, undefined4 buildingType, + undefined4 variation, uint buildingSize, undefined4 orientation, undefined4 averageHeight) + { + int index = 0; + int const buildingID = MACRO_CALL_MEMBER( + OpenSHC::Map::Buildings::BuildingsState_Func::setupBuildingData, DAT_BuildingsState::ptr)( + playerID, x, y, averageHeight, (BuildingType)(short)buildingType, buildingSize, playerID, (short)variation); + this->placedBuildingID = buildingID; + while (true) { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::getBuildingSizeIndexMappingData, this)( + index, buildingSize); + int const tile = DAT_ViewportRenderState::instance.translationMatrix[this->buildingY + y].addXgetTile + + this->buildingX + x; + if (index < countof(DAT_BuildingsState::instance.buildings[buildingID].tileRefs)) { + DAT_BuildingsState::instance.buildings[buildingID].tileRefs[index] = tile; + } + this->HeightLayer[tile] = (byte)averageHeight; + this->LogicLayer[tile] |= L_BUILDING; + this->BuildingLayer[tile] = (short)buildingID; + index++; + this->ChangedLayer[tile] = 2; + if (index >= this->constructionTileCount) { + break; + } + } + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::setMiscDisplayLayer, this)(buildingID); + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::updatePathLinkagesForBuilding, this)(buildingID); + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/updateBuildingGraphicsLayer.cpp b/src/OpenSHC/Map/TileMapState/updateBuildingGraphicsLayer.cpp new file mode 100644 index 0000000000..5c2e36af6d --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/updateBuildingGraphicsLayer.cpp @@ -0,0 +1,303 @@ +#include "../TileMapState.func.hpp" + +#include "OpenSHC/Game/GameMode2.hpp" +#include "OpenSHC/Map/Buildings/BuildingType.hpp" + +#include "OpenSHC/Globals/DAT_BuildingsState.hpp" +#include "OpenSHC/Globals/DAT_GameCore.hpp" +#include "OpenSHC/Globals/DAT_GameState.hpp" +#include "OpenSHC/Globals/DAT_GameSynchronyState.hpp" +#include "OpenSHC/Globals/DAT_TerrainDefinedData.hpp" +#include "OpenSHC/Globals/DAT_ViewportRenderState.hpp" +#include "OpenSHC/Globals/GMFirstPictureIndex.hpp" + +namespace OpenSHC { +namespace Map { + + // FUNCTION: STRONGHOLDCRUSADER 0x00506370 + void TileMapState::updateBuildingGraphicsLayer(int buildingID) + { + int const buildingXPos = DAT_BuildingsState::instance.buildings[buildingID].x; + int const buildingYPos = DAT_BuildingsState::instance.buildings[buildingID].y; + int index = 0; + DAT_BuildingsState::instance.buildings[buildingID].useOffsetGraphics = 0; + if (this->refreshRelatedOne == 0 && DAT_BuildingsState::instance.buildings[buildingID].gfxOffset != 0) { + DAT_BuildingsState::instance.buildings[buildingID].useOffsetGraphics = 1; + } + int const stored = DAT_BuildingsState::instance.buildings[buildingID].currentNumberOfResource + - DAT_BuildingsState::instance.buildings[buildingID].someResourceNumber; + + do { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::getBuildingSizeIndexMappingData, this)( + index, DAT_BuildingsState::instance.buildings[buildingID].widthOrHeight); + int const tile + = DAT_ViewportRenderState::instance.translationMatrix[this->buildingY + buildingYPos].addXgetTile + + buildingXPos + this->buildingX; + if (this->disableElevationRendering != 0) { + // the placement overlay replaces every building graphic with its outline + this->GfxLayer[tile] + = DAT_TerrainDefinedData::instance + .BrushSizeArray[DAT_BuildingsState::instance.buildings[buildingID].widthOrHeight + 7] + + this->buildingRotationRelatedValue + GMFirstPictureIndex::instance[157] - 1; + this->AlphaGFXLayer[tile] = 0; + } else if (DAT_BuildingsState::instance.buildings[buildingID].buildingType + == OpenSHC::Map::Buildings::BT_STOCKPILE + || DAT_BuildingsState::instance.buildings[buildingID].buildingType + == OpenSHC::Map::Buildings::BT_QUARRYSTOCKPILE) { + // a stockpile's pile grows with what is stored on it + if (stored > 0) { + int const pileGfx = DAT_TerrainDefinedData::instance + .field_0x84c[DAT_BuildingsState::instance.buildings[buildingID] + .currentStoredResourceType]; + int pile; + if (DAT_TerrainDefinedData::instance + .field_0x8b4[DAT_BuildingsState::instance.buildings[buildingID].currentStoredResourceType] + == 2) { + int const remaining + = DAT_BuildingsState::instance.buildings[buildingID].currentLimitOfResource - stored; + pile = this->buildingRotationRelatedValue + (remaining / 4 + (remaining & 3) * 12) * 4; + } else if (DAT_TerrainDefinedData::instance.field_0x8b4 + [DAT_BuildingsState::instance.buildings[buildingID].currentStoredResourceType] + != 0) { + pile = this->buildingRotationRelatedValue + + (DAT_BuildingsState::instance.buildings[buildingID].currentLimitOfResource - stored) * 4; + } else { + pile = this->buildingRotationRelatedValue - 4 + stored * 4; + } + this->GfxLayer[tile] = GMFirstPictureIndex::instance[15] + pileGfx + pile - 1; + } else { + this->GfxLayer[tile] = this->buildingRotationRelatedValue + GMFirstPictureIndex::instance[15] + 4; + } + } else if (DAT_BuildingsState::instance.buildings[buildingID].buildingType + == OpenSHC::Map::Buildings::BT_MERCENARYPOST + || DAT_BuildingsState::instance.buildings[buildingID].buildingType + == OpenSHC::Map::Buildings::BT_BARRACKS + || DAT_BuildingsState::instance.buildings[buildingID].buildingType + == OpenSHC::Map::Buildings::BT_OUTPOST_EUROPEAN) { + this->GfxLayer[tile] + = GMFirstPictureIndex::instance[DAT_BuildingsState::instance.buildings[buildingID].spriteSheetID] + + DAT_BuildingsState::instance.buildings[buildingID].spriteID + this->buildingRotationRelatedValue + - 1; + } else if (DAT_BuildingsState::instance.buildings[buildingID].buildingType + == OpenSHC::Map::Buildings::BT_KEEPDOOR_LEFT + || DAT_BuildingsState::instance.buildings[buildingID].buildingType + == OpenSHC::Map::Buildings::BT_KEEPDOOR_RIGHT) { + // a side door faces along the keep's axis, so its graphic follows the map rotation + int facing = this->mapOrientation; + if (DAT_BuildingsState::instance.buildings[buildingID].orientation == 6 + || DAT_BuildingsState::instance.buildings[buildingID].orientation == 2) { + facing = (facing + 2) % 8; + } + if (DAT_BuildingsState::instance.buildings[buildingID].useOffsetGraphics != 0) { + if (facing != 2 && facing != 6) { + this->GfxLayer[tile] + = GMFirstPictureIndex::instance[DAT_BuildingsState::instance.buildings[buildingID] + .spriteSheetID] + + DAT_BuildingsState::instance.buildings[buildingID].gfxOffset - 1; + } else { + this->GfxLayer[tile] + = GMFirstPictureIndex::instance[DAT_BuildingsState::instance.buildings[buildingID] + .spriteSheetID] + + DAT_BuildingsState::instance.buildings[buildingID].gfxOffset; + } + } else if (facing != 2 && facing != 6) { + this->GfxLayer[tile] + = GMFirstPictureIndex::instance[DAT_BuildingsState::instance.buildings[buildingID] + .spriteSheetID] + + DAT_BuildingsState::instance.buildings[buildingID].unknownManorHouseOrStoneKeepRelated + + DAT_BuildingsState::instance.buildings[buildingID].spriteID - 1; + } else { + this->GfxLayer[tile] + = GMFirstPictureIndex::instance[DAT_BuildingsState::instance.buildings[buildingID] + .spriteSheetID] + + DAT_BuildingsState::instance.buildings[buildingID].unknownManorHouseOrStoneKeepRelated + + DAT_BuildingsState::instance.buildings[buildingID].spriteID; + } + } else if (DAT_BuildingsState::instance.buildings[buildingID].buildingType + == OpenSHC::Map::Buildings::BT_KEEPDOOR) { + if (DAT_BuildingsState::instance.buildings[buildingID].useOffsetGraphics != 0) { + this->GfxLayer[tile] + = GMFirstPictureIndex::instance[DAT_BuildingsState::instance.buildings[buildingID] + .spriteSheetID] + + DAT_BuildingsState::instance.buildings[buildingID].unknownManorHouseOrStoneKeepRelated + + DAT_BuildingsState::instance.buildings[buildingID].gfxOffset - 1; + } else if (this->mapOrientation != 2 && this->mapOrientation != 6) { + this->GfxLayer[tile] + = GMFirstPictureIndex::instance[DAT_BuildingsState::instance.buildings[buildingID] + .spriteSheetID] + + DAT_BuildingsState::instance.buildings[buildingID].unknownManorHouseOrStoneKeepRelated + + DAT_BuildingsState::instance.buildings[buildingID].spriteID - 1; + } else { + this->GfxLayer[tile] + = GMFirstPictureIndex::instance[DAT_BuildingsState::instance.buildings[buildingID] + .spriteSheetID] + + DAT_BuildingsState::instance.buildings[buildingID].unknownManorHouseOrStoneKeepRelated + + DAT_BuildingsState::instance.buildings[buildingID].spriteID; + } + } else if (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) { + int variation; + if (this->mapOrientation != 2 && this->mapOrientation != 6) { + variation = DAT_BuildingsState::instance.buildings[buildingID].buildingVariation; + } else { + variation = (DAT_BuildingsState::instance.buildings[buildingID].buildingVariation == 80) + 80; + } + if (DAT_BuildingsState::instance.buildings[buildingID].useOffsetGraphics != 0) { + if (variation == 80) { + this->GfxLayer[tile] + = GMFirstPictureIndex::instance[DAT_BuildingsState::instance.buildings[buildingID] + .spriteSheetID] + + DAT_BuildingsState::instance.buildings[buildingID].gfxOffset + + this->buildingRotationRelatedValue - 1; + } else { + this->GfxLayer[tile] + = GMFirstPictureIndex::instance[DAT_BuildingsState::instance.buildings[buildingID] + .spriteSheetID] + + DAT_BuildingsState::instance.buildings[buildingID].gfxOffset3 + + this->buildingRotationRelatedValue - 1; + } + } else if (variation == 80) { + this->GfxLayer[tile] + = GMFirstPictureIndex::instance[DAT_BuildingsState::instance.buildings[buildingID] + .spriteSheetID] + + DAT_BuildingsState::instance.buildings[buildingID].spriteID + + this->buildingRotationRelatedValue - 1; + } else { + this->GfxLayer[tile] + = GMFirstPictureIndex::instance[DAT_BuildingsState::instance.buildings[buildingID] + .spriteSheetID] + + DAT_BuildingsState::instance.buildings[buildingID].gfxOffset2 + + this->buildingRotationRelatedValue - 1; + } + } else if (DAT_BuildingsState::instance.buildings[buildingID].buildingType + == OpenSHC::Map::Buildings::BT_MANORHOUSE) { + int variation; + if (this->mapOrientation != 2 && this->mapOrientation != 6) { + variation = 80; + } else { + variation = 81; + } + if (DAT_BuildingsState::instance.buildings[buildingID].useOffsetGraphics != 0) { + if (variation == 80) { + this->GfxLayer[tile] + = GMFirstPictureIndex::instance[DAT_BuildingsState::instance.buildings[buildingID] + .spriteSheetID] + + DAT_BuildingsState::instance.buildings[buildingID].gfxOffset + + this->buildingRotationRelatedValue - 1; + } else { + this->GfxLayer[tile] + = GMFirstPictureIndex::instance[DAT_BuildingsState::instance.buildings[buildingID] + .spriteSheetID] + + this->buildingRotationRelatedValue + 664; + } + } else if (variation == 80) { + this->GfxLayer[tile] + = GMFirstPictureIndex::instance[DAT_BuildingsState::instance.buildings[buildingID] + .spriteSheetID] + + DAT_BuildingsState::instance.buildings[buildingID].spriteID + + this->buildingRotationRelatedValue - 1; + } else { + this->GfxLayer[tile] + = GMFirstPictureIndex::instance[DAT_BuildingsState::instance.buildings[buildingID] + .spriteSheetID] + + this->buildingRotationRelatedValue + 566; + } + } else if (DAT_BuildingsState::instance.buildings[buildingID].buildingType + == OpenSHC::Map::Buildings::BT_DRAWBRIDGE) { + int const plank = DAT_TerrainDefinedData::instance + .field_0x1fec[DAT_BuildingsState::instance.buildings[buildingID].buildingVariation + / 2][index]; + if (plank != 0) { + this->GfxLayer[tile] + = GMFirstPictureIndex::instance[DAT_BuildingsState::instance.buildings[buildingID] + .spriteSheetID] + + plank + 1566; + } else if (MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::returnOwnedMoatAtTile, this)(tile) != 0) { + this->GfxLayer[tile] = (this->RandomLayer[tile] & 3) + (this->LuminesenceLayer[tile] + 51) * 4 + + GMFirstPictureIndex::instance[5]; + } else { + this->GfxLayer[tile] = (this->RandomLayer[tile] & 3) + this->LuminesenceLayer[tile] * 4 + + GMFirstPictureIndex::instance[2]; + } + } else if ((DAT_BuildingsState::instance.buildings[buildingID].buildingType + != OpenSHC::Map::Buildings::BT_KILLINGPIT + || DAT_BuildingsState::instance.buildings[buildingID].state > 0 + || DAT_GameState::instance.mapAndTime + .playerTeams[DAT_BuildingsState::instance.buildings[buildingID].owner] + == DAT_GameState::instance.mapAndTime + .playerTeams[DAT_GameSynchronyState::instance.currentPlayerSlotID] + || DAT_GameCore::instance.gameMode_2 == OpenSHC::Game::GM_EDITOR) + && DAT_BuildingsState::instance.buildings[buildingID].buildingType + != OpenSHC::Map::Buildings::BT_SIEGETOWER_PLACED) { + // a killing pit is invisible to anyone not on the owner's team + if (DAT_BuildingsState::instance.buildings[buildingID].buildingVariation != 15) { + int rotation + = DAT_BuildingsState::instance.buildings[buildingID].buildingVariation - this->mapOrientation; + if (rotation < 0) { + rotation += 8; + } + int span; + if (DAT_BuildingsState::instance.buildings[buildingID].buildingVariation == 8) { + rotation = DAT_BuildingsState::instance.buildings[buildingID].widthOrHeight; + span = DAT_BuildingsState::instance.buildings[buildingID].buildingVariation * rotation; + } else { + span = DAT_BuildingsState::instance.buildings[buildingID].widthOrHeight + * DAT_BuildingsState::instance.buildings[buildingID].widthOrHeight; + } + int const offset = span * rotation; + if (DAT_BuildingsState::instance.buildings[buildingID].buildingIsVisuallyActive != 0) { + this->GfxLayer[tile] + = GMFirstPictureIndex::instance[DAT_BuildingsState::instance.buildings[buildingID] + .spriteSheetID] + + DAT_BuildingsState::instance.buildings[buildingID].visuallyActiveSpriteID + + this->buildingRotationRelatedValue + offset - 1; + } else { + this->GfxLayer[tile] + = GMFirstPictureIndex::instance[DAT_BuildingsState::instance.buildings[buildingID] + .spriteSheetID] + + DAT_BuildingsState::instance.buildings[buildingID].spriteID + + this->buildingRotationRelatedValue + offset - 1; + } + this->AlphaGFXLayer[tile] + = GMFirstPictureIndex::instance[DAT_BuildingsState::instance.buildings[buildingID] + .spriteSheetID] + + DAT_BuildingsState::instance.buildings[buildingID].spriteID + + this->buildingRotationRelatedValue + offset - 1; + } else { + if (DAT_BuildingsState::instance.buildings[buildingID].useOffsetGraphics != 0) { + this->GfxLayer[tile] + = GMFirstPictureIndex::instance[DAT_BuildingsState::instance.buildings[buildingID] + .spriteSheetID] + + DAT_BuildingsState::instance.buildings[buildingID].gfxOffset + + this->buildingRotationRelatedValue - 1; + } else if (DAT_BuildingsState::instance.buildings[buildingID].buildingIsVisuallyActive != 0) { + this->GfxLayer[tile] + = GMFirstPictureIndex::instance[DAT_BuildingsState::instance.buildings[buildingID] + .spriteSheetID] + + DAT_BuildingsState::instance.buildings[buildingID].visuallyActiveSpriteID + + this->buildingRotationRelatedValue - 1; + } else { + this->GfxLayer[tile] + = GMFirstPictureIndex::instance[DAT_BuildingsState::instance.buildings[buildingID] + .spriteSheetID] + + DAT_BuildingsState::instance.buildings[buildingID].unknownManorHouseOrStoneKeepRelated + + DAT_BuildingsState::instance.buildings[buildingID].spriteID + + this->buildingRotationRelatedValue - 1; + } + this->AlphaGFXLayer[tile] + = GMFirstPictureIndex::instance[DAT_BuildingsState::instance.buildings[buildingID] + .spriteSheetID] + + DAT_BuildingsState::instance.buildings[buildingID].unknownManorHouseOrStoneKeepRelated + + DAT_BuildingsState::instance.buildings[buildingID].spriteID + + this->buildingRotationRelatedValue - 1; + } + } + } while (++index < this->constructionTileCount); + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/updateGFXLayers.cpp b/src/OpenSHC/Map/TileMapState/updateGFXLayers.cpp new file mode 100644 index 0000000000..b5306dfe6a --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/updateGFXLayers.cpp @@ -0,0 +1,337 @@ +#include "../TileMapState.func.hpp" + +#include "OpenSHC/Map/Navigation/DirectionAlgorithmState.func.hpp" +#include "OpenSHC/Map/Navigation/PathFindingState.func.hpp" +#include "OpenSHC/Rendering/ViewportRenderState.func.hpp" +#include "OpenSHC/Map/Enums/MapOrientation.hpp" +#include "OpenSHC/Map/TileMapState/NeighbourFlagsGlobalAsm.hpp" + +#include "OpenSHC/Globals/DAT_DirectionAlgorithmState.hpp" +#include "OpenSHC/Globals/DAT_PathFindingState.hpp" +#include "OpenSHC/Globals/DAT_TerrainDefinedData.hpp" +#include "OpenSHC/Globals/DAT_TileMapState.hpp" +#include "OpenSHC/Globals/DAT_ViewportRenderState.hpp" +#include "OpenSHC/Globals/GMFirstPictureIndex.hpp" + +// masks for the handwritten neighbour-flag macro; MASM has no "|", so these must be literals +#define NEIGHBOUR_FLAGS_MASK_MARSH 0x20000000 // L_MARSH +#define NEIGHBOUR_FLAGS_MASK_SEA 0x1 // L_SEA +#define NEIGHBOUR_FLAGS_MASK_DEFAULT_EARTH 0x8000 // L_DEFAULT_EARTH_OR_TEXTURE +#define NEIGHBOUR_FLAGS_MASK_L2_SCRUB 0x1 // L2_SCRUB +#define NEIGHBOUR_FLAGS_MASK_L2_EARTH_AND_STONES 0x2 // L2_EARTH_AND_STONES +#define NEIGHBOUR_FLAGS_MASK_L2_PLATEAU_MEDIUM 0x4 // L2_PLATEAU_MEDIUM +#define NEIGHBOUR_FLAGS_MASK_L2_PLATEAU_HIGH 0x8 // L2_PLATEAU_HIGH +#define NEIGHBOUR_FLAGS_MASK_L2_OASIS_GRASS 0x10 // L2_OASIS_GRASS +#define NEIGHBOUR_FLAGS_MASK_L2_BEACH 0x20 // L2_BEACH +#define NEIGHBOUR_FLAGS_MASK_L2_STONES 0x40 // L2_STONES_OR_DRIVEN_SANDUnk + +// the three macro-tile offset tables are one int[3][4][16] array, one table per macro size 4, 8 and 16 +#define MACRO_TILE_OFFSETS (*(int (*)[3][4][16]) & DAT_TerrainDefinedData::instance.field_0x1a54) + +#pragma optimize("", off) + +namespace OpenSHC { +namespace Map { + + using OpenSHC::Map::LogicHelpers::L2_BEACH; + using OpenSHC::Map::LogicHelpers::L2_EARTH_AND_STONES; + 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::L2_STONES_OR_DRIVEN_SANDUnk; + using OpenSHC::Map::LogicHelpers::L2_THICK_SCRUB; + using OpenSHC::Map::LogicHelpers::L_DEFAULT_EARTH_OR_TEXTURE; + using OpenSHC::Map::LogicHelpers::L_MARSH; + + // FUNCTION: STRONGHOLDCRUSADER 0x004FC9E0 + void TileMapState::updateGFXLayers() + { + // Contains the handwritten neighbour-flag macro nine times (see NeighbourFlagsGlobalAsm.hpp), which + // is why the surrounding code is unoptimised: MSVC disables optimisation in a function holding + // inline asm, so every field access reloads this from the frame. + // + // One store below is dead in this function but present in the original, because the body is + // shared with updateMacroLayerRelated: "elevation" is read from HeightLayer and never used. + // The locals are declared here because the original was built without optimisation: its + // stack slots are observable and MSVC assigns them per function, not per scope. Declaring + // them at first use instead costs ~12 points of match (52.6% vs 64.5%). + // At /Od MSVC lays the locals out by a hash of their NAME (16 buckets, walked in order; + // inside one bucket the later declaration gets the higher slot), not by declaration order. + // The names and their order below were chosen so the slots land where the original has them + // (rotation at ebp-4 down to the gfx bases at ebp-0x4c); renaming any of them moves the slots. + int rotation; + int sectorColumn; + int partIndex; + int drawMode; + int tileRow; + int perLevel; + int luminescence; + int lowGfx; + int sectorRow; + int macroTop; + int macroSizeType; + int edgeBase; + int tileColumn; + int elevation; + int macroBase; + int priorBitFlag; + int macroMid; + int flatFrame; + int cornerFrame; + + if (this->forceUpdateGFXLayers <= 0) { + return; + } + this->forceUpdateGFXLayers--; + + if (MACRO_CALL_MEMBER(OpenSHC::Map::Navigation::PathFindingState_Func::getYSmallerThanYLimit, + &DAT_PathFindingState::instance)() + == FALSE) { + return; + } + + rotation = DAT_PathFindingState::instance.mappingYRelated % 10; + for (sectorRow = this->someIndex; sectorRow <= this->someLimit; sectorRow++) { + for (sectorColumn = this->someYLike; sectorColumn <= this->someYLikeLimit; sectorColumn++) { + if (DAT_TileMapState::instance.mapping40x40[sectorRow][sectorColumn] != 0) { + tileRow = rotation; + rotation = 0; + for (; tileRow < 10; tileRow++) { + for (tileColumn = 0, this->DAT_SomeY = sectorRow * 10 + tileRow, + this->DAT_SomeX = sectorColumn * 10; + tileColumn < 10; tileColumn++, this->DAT_SomeX++) { + if (MACRO_CALL_MEMBER(OpenSHC::Rendering::ViewportRenderState_Func::xyAreValid, + &DAT_ViewportRenderState::instance)(this->DAT_SomeX, this->DAT_SomeY) + == FALSE) { + continue; + } + this->DAT_SomeTile + = MACRO_CALL_MEMBER(OpenSHC::Rendering::ViewportRenderState_Func::translateXYToTile, + &DAT_ViewportRenderState::instance)(this->DAT_SomeX, this->DAT_SomeY); + if ((this->LogicLayer[this->DAT_SomeTile] & L_DEFAULT_EARTH_OR_TEXTURE) == 0) { + continue; + } + if (this->GfxLayer[this->DAT_SomeTile] != 0) { + continue; + } + luminescence = this->LuminesenceLayer[this->DAT_SomeTile]; + if (luminescence < 2) { + luminescence = 2; + } + macroSizeType = this->MacroLayer[this->DAT_SomeTile] & 0x3f; + macroMid = (this->MacroLayer[this->DAT_SomeTile] & 0x7c0) >> 6; + macroTop = (this->MacroLayer[this->DAT_SomeTile] & 0xf000) >> 12; + macroTop &= 3; + this->PillarGFXLayer[this->DAT_SomeTile] + = *(int volatile*)&GMFirstPictureIndex::instance[9]; + + if ((this->LogicLayer[this->DAT_SomeTile] & L_MARSH) != 0) { + MACRO_NEIGHBOUR_FLAGS_GLOBAL_LOGIC_4(1, NEIGHBOUR_FLAGS_MASK_MARSH) + edgeBase = 1; + cornerFrame = 3; + flatFrame = 21; + lowGfx = 33; + macroBase = 37; + perLevel = 4; + drawMode = 0; + } else if (((char)this->Logic2Layer[this->DAT_SomeTile] & L2_EARTH_AND_STONES) != 0) { + elevation = this->HeightLayer[this->DAT_SomeTile]; + MACRO_NEIGHBOUR_FLAGS_GLOBAL_TERRAIN_4(2, NEIGHBOUR_FLAGS_MASK_L2_EARTH_AND_STONES) + edgeBase = 153; + cornerFrame = 155; + flatFrame = 173; + lowGfx = 185; + macroBase = 189; + perLevel = 4; + drawMode = 0; + } else if (((char)this->Logic2Layer[this->DAT_SomeTile] & L2_OASIS_GRASS) != 0) { + MACRO_NEIGHBOUR_FLAGS_GLOBAL_TERRAIN_4(3, NEIGHBOUR_FLAGS_MASK_L2_OASIS_GRASS) + edgeBase = 457; + cornerFrame = 459; + flatFrame = 477; + lowGfx = 489; + macroBase = 493; + perLevel = 4; + drawMode = 0; + } else if (((char)this->Logic2Layer[this->DAT_SomeTile] & L2_STONES_OR_DRIVEN_SANDUnk) + != 0) { + MACRO_NEIGHBOUR_FLAGS_GLOBAL_TERRAIN_4(4, NEIGHBOUR_FLAGS_MASK_L2_STONES) + edgeBase = 609; + cornerFrame = 611; + flatFrame = 629; + lowGfx = 641; + macroBase = 645; + perLevel = 4; + drawMode = 0; + } else if (((char)this->Logic2Layer[this->DAT_SomeTile] & L2_SCRUB) != 0) { + MACRO_NEIGHBOUR_FLAGS_GLOBAL_TERRAIN_4(5, NEIGHBOUR_FLAGS_MASK_L2_SCRUB) + edgeBase = 761; + cornerFrame = 763; + flatFrame = 781; + lowGfx = 793; + macroBase = 797; + perLevel = 4; + drawMode = 0; + } else if (((char)this->Logic2Layer[this->DAT_SomeTile] & L2_THICK_SCRUB) != 0) { + // the original gathers L2_SCRUB here, not L2_THICK_SCRUB; kept as-is + MACRO_NEIGHBOUR_FLAGS_GLOBAL_TERRAIN_4(6, NEIGHBOUR_FLAGS_MASK_L2_SCRUB) + edgeBase = 1193; + cornerFrame = 1195; + flatFrame = 1213; + lowGfx = 1225; + macroBase = 1229; + perLevel = 4; + drawMode = 0; + } else if (((char)this->Logic2Layer[this->DAT_SomeTile] & L2_PLATEAU_MEDIUM) != 0) { + MACRO_NEIGHBOUR_FLAGS_GLOBAL_TERRAIN_4(7, NEIGHBOUR_FLAGS_MASK_L2_PLATEAU_MEDIUM) + edgeBase = 305; + cornerFrame = 307; + flatFrame = 325; + lowGfx = 337; + macroBase = 341; + perLevel = 4; + drawMode = 2; + } else if (((char)this->Logic2Layer[this->DAT_SomeTile] & L2_PLATEAU_HIGH) != 0) { + MACRO_NEIGHBOUR_FLAGS_GLOBAL_TERRAIN_4(8, NEIGHBOUR_FLAGS_MASK_L2_PLATEAU_HIGH) + edgeBase = 305; + cornerFrame = 307; + flatFrame = 325; + lowGfx = 337; + macroBase = 341; + perLevel = 4; + drawMode = 2; + } else if (((char)this->Logic2Layer[this->DAT_SomeTile] & L2_BEACH) != 0) { + MACRO_NEIGHBOUR_FLAGS_GLOBAL_TERRAIN_4(9, NEIGHBOUR_FLAGS_MASK_L2_BEACH) + priorBitFlag = this->bitFlag; + MACRO_NEIGHBOUR_FLAGS_GLOBAL_LOGIC_4(10, NEIGHBOUR_FLAGS_MASK_SEA) + this->bitFlag = this->bitFlag | priorBitFlag; + edgeBase = 913; + cornerFrame = 915; + flatFrame = 933; + lowGfx = 945; + macroBase = 949; + perLevel = 4; + drawMode = 0; + } else { + MACRO_NEIGHBOUR_FLAGS_GLOBAL_LOGIC_4(11, NEIGHBOUR_FLAGS_MASK_DEFAULT_EARTH) + edgeBase = 305; + cornerFrame = 307; + flatFrame = 325; + lowGfx = 337; + macroBase = 341; + perLevel = 4; + drawMode = 2; + } + + if (macroSizeType == 1) { + this->bitFlag = ~this->bitFlag; + this->bitFlag &= 0xaa; + if (this->mapOrientation == OpenSHC::Map::Enums::MO_NORTH) { + this->bitFlag = MACRO_CALL_MEMBER( + OpenSHC::Map::Navigation::DirectionAlgorithmState_Func::rotateByteLeft, + &DAT_DirectionAlgorithmState::instance)(this->bitFlag, 0); + } else if (this->mapOrientation == OpenSHC::Map::Enums::MO_EAST) { + this->bitFlag = MACRO_CALL_MEMBER( + OpenSHC::Map::Navigation::DirectionAlgorithmState_Func::rotateByteLeft, + &DAT_DirectionAlgorithmState::instance)(this->bitFlag, 2); + } else if (this->mapOrientation == OpenSHC::Map::Enums::MO_SOUTH) { + this->bitFlag = MACRO_CALL_MEMBER( + OpenSHC::Map::Navigation::DirectionAlgorithmState_Func::rotateByteLeft, + &DAT_DirectionAlgorithmState::instance)(this->bitFlag, 4); + } else if (this->mapOrientation == OpenSHC::Map::Enums::MO_WEST) { + this->bitFlag = MACRO_CALL_MEMBER( + OpenSHC::Map::Navigation::DirectionAlgorithmState_Func::rotateByteLeft, + &DAT_DirectionAlgorithmState::instance)(this->bitFlag, 6); + } + + if (drawMode == 1) { + this->GfxLayer[this->DAT_SomeTile] = 0; + for (partIndex = 0; partIndex < 4; partIndex++) { + if (DAT_TerrainDefinedData::instance.field_0x1d54[partIndex][0] + == this->bitFlag) { + this->GfxLayer[this->DAT_SomeTile] = GMFirstPictureIndex::instance[55] + + edgeBase + ((luminescence - 2) * perLevel - 1) + + ((short)this->RandomLayer[this->DAT_SomeTile] & 1) + + DAT_TerrainDefinedData::instance.field_0x1d54[partIndex][1]; + } + } + for (partIndex = 4; partIndex < 8; partIndex++) { + if (DAT_TerrainDefinedData::instance.field_0x1d54[partIndex][0] + == this->bitFlag) { + this->GfxLayer[this->DAT_SomeTile] = GMFirstPictureIndex::instance[55] + + cornerFrame + ((luminescence - 2) * perLevel - 1) + + DAT_TerrainDefinedData::instance.field_0x1d54[partIndex][1]; + } + } + if (this->GfxLayer[this->DAT_SomeTile] == 0) { + this->GfxLayer[this->DAT_SomeTile] = GMFirstPictureIndex::instance[55] + + flatFrame + ((luminescence - 2) * perLevel - 1) + + ((short)this->RandomLayer[this->DAT_SomeTile] & 3); + } + } else if (luminescence == 2) { + if (this->bitFlag != 0) { + this->GfxLayer[this->DAT_SomeTile] = GMFirstPictureIndex::instance[55] + + flatFrame + ((short)this->RandomLayer[this->DAT_SomeTile] & 7) - 1; + } else { + this->GfxLayer[this->DAT_SomeTile] = GMFirstPictureIndex::instance[55] + lowGfx + + ((short)this->RandomLayer[this->DAT_SomeTile] & 7) - 1; + } + } else if (drawMode == 2) { + this->GfxLayer[this->DAT_SomeTile] = GMFirstPictureIndex::instance[55] + edgeBase + + ((luminescence - 3) * perLevel - 1) + + ((short)this->RandomLayer[this->DAT_SomeTile] & 3); + } else if (this->bitFlag != 0) { + this->GfxLayer[this->DAT_SomeTile] = GMFirstPictureIndex::instance[55] + edgeBase + + ((luminescence - 3) * perLevel - 1) + + ((short)this->RandomLayer[this->DAT_SomeTile] & 1); + } else { + this->GfxLayer[this->DAT_SomeTile] = GMFirstPictureIndex::instance[55] + cornerFrame + + ((luminescence - 3) * perLevel - 1) + + ((short)this->RandomLayer[this->DAT_SomeTile] & 1); + } + } else if (luminescence > 2) { + if (drawMode == 0) { + this->GfxLayer[this->DAT_SomeTile] = GMFirstPictureIndex::instance[55] + cornerFrame + + ((luminescence - 3) * perLevel - 1) + + ((short)this->RandomLayer[this->DAT_SomeTile] & 1); + } else if (drawMode == 2) { + this->GfxLayer[this->DAT_SomeTile] = GMFirstPictureIndex::instance[55] + edgeBase + + ((luminescence - 3) * perLevel - 1) + + ((short)this->RandomLayer[this->DAT_SomeTile] & 3); + } else { + this->GfxLayer[this->DAT_SomeTile] = GMFirstPictureIndex::instance[55] + lowGfx + + ((luminescence - 2) * perLevel - 1) + + ((short)this->RandomLayer[this->DAT_SomeTile] & 3); + } + } else if (macroSizeType == 2) { + if (drawMode == 0) { + this->GfxLayer[this->DAT_SomeTile] = GMFirstPictureIndex::instance[55] + lowGfx + + ((short)this->RandomLayer[this->DAT_SomeTile] & 7) - 1; + } else { + this->GfxLayer[this->DAT_SomeTile] = GMFirstPictureIndex::instance[55] + lowGfx + + ((short)this->RandomLayer[this->DAT_SomeTile] & 3) - 1; + } + } else if (macroSizeType == 4) { + macroMid = MACRO_TILE_OFFSETS[0][this->mapOrientation / 2][macroMid]; + this->GfxLayer[this->DAT_SomeTile] + = GMFirstPictureIndex::instance[55] + macroBase + macroMid + macroTop * 4 - 1; + } else if (macroSizeType == 8) { + macroMid = MACRO_TILE_OFFSETS[1][this->mapOrientation / 2][macroMid]; + this->GfxLayer[this->DAT_SomeTile] + = GMFirstPictureIndex::instance[55] + macroBase + macroTop * 9 + macroMid + 15; + } else if (macroSizeType == 16) { + macroMid = MACRO_TILE_OFFSETS[2][this->mapOrientation / 2][macroMid]; + this->GfxLayer[this->DAT_SomeTile] + = GMFirstPictureIndex::instance[55] + macroBase + macroTop * 16 + macroMid + 51; + } + } + } + } + } + } + } +} +} + +#pragma optimize("", on) diff --git a/src/OpenSHC/Map/TileMapState/updateGfxLayer.cpp b/src/OpenSHC/Map/TileMapState/updateGfxLayer.cpp new file mode 100644 index 0000000000..0f54a8b91a --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/updateGfxLayer.cpp @@ -0,0 +1,1726 @@ +#include "../TileMapState.func.hpp" + +#include "OpenSHC/Game/GameStateStructures.func.hpp" +#include "OpenSHC/Map/Navigation/DirectionAlgorithmState.func.hpp" +#include "OpenSHC/Rendering/ViewportRenderState.func.hpp" +#include "OpenSHC/Game/GameMode2.hpp" +#include "OpenSHC/Map/Buildings/BuildingType.hpp" +#include "OpenSHC/Map/Enums/MapOrientation.hpp" +#include "OpenSHC/Map/TileMapState/NeighbourFlagsGlobalAsm.hpp" +#include "OpenSHC/WindowsHelper/Enums/BOOLEnum.hpp" + +#include "OpenSHC/Globals/DAT_BuildingsState.hpp" +#include "OpenSHC/Globals/DAT_DirectionAlgorithmState.hpp" +#include "OpenSHC/Globals/DAT_GameCore.hpp" +#include "OpenSHC/Globals/DAT_GameState.hpp" +#include "OpenSHC/Globals/DAT_GameSynchronyState.hpp" +#include "OpenSHC/Globals/DAT_LandscapeState.hpp" +#include "OpenSHC/Globals/DAT_PathFindingState.hpp" +#include "OpenSHC/Globals/DAT_TerrainDefinedData.hpp" +#include "OpenSHC/Globals/DAT_TileMapState.hpp" +#include "OpenSHC/Globals/DAT_ViewportRenderState.hpp" +#include "OpenSHC/Globals/GMFirstPictureIndex.hpp" + +// The original's macro expansions read the global instance (NeighbourFlagsGlobalAsm.hpp), but here that +// measured 90.5% -> 89.8% (reccmp realigns the jump displacements of the now shorter body), so the this-based +// NeighbourFlagsAsm.hpp stays. +// masks for the handwritten neighbour-flag macro; MASM has no "|", so these must be literals +#define NEIGHBOUR_FLAGS_MASK_RIVER 0x100000 // L_RIVER +#define NEIGHBOUR_FLAGS_MASK_RIVER_OR_FORD 0x300000 // L_RIVER | L_FORD +#define NEIGHBOUR_FLAGS_MASK_WALL_OR_GATEHOUSE 0x100 // L_WALL_OR_GATEHOUSE + +// the original was optimised, but a jump from the handwritten asm to a C label (warning C4740) switches global +// optimisation off for the whole function: frame pointer, locals off ebp, sub esp, 0x118. The asm jumps to +// wallAlpha below are that flow. + +// the original indexes directionTranslationMatrix as one flat array of eight entries per row +#define DIRECTION_OFFSET(Y, DIRECTION) (this->directionTranslationMatrix[0][(Y) * 8 + (DIRECTION)]) +// the original picks a sea-edge graphic from the rotated neighbour mask with this same switch twice +#define SEA_EDGE_PICK(MASK, OFFSET, MODULUS, COUNT, BIT) \ + switch (MASK) { \ + case 0x80: \ + case 0x81: \ + case 0xc0: \ + case 0xc1: \ + OFFSET = 0; \ + MODULUS = 4; \ + break; \ + case 0x20: \ + case 0x30: \ + case 0x60: \ + case 0x70: \ + OFFSET = 4; \ + MODULUS = 4; \ + break; \ + case 0x08: \ + case 0x0c: \ + case 0x18: \ + case 0x1c: \ + OFFSET = 8; \ + MODULUS = 4; \ + break; \ + case 0x02: \ + case 0x03: \ + case 0x06: \ + case 0x07: \ + OFFSET = 12; \ + MODULUS = 4; \ + break; \ + case 0x83: \ + case 0x87: \ + case 0xc3: \ + case 0xc7: \ + OFFSET = 16; \ + break; \ + case 0xe0: \ + case 0xe1: \ + case 0xf0: \ + case 0xf1: \ + OFFSET = 18; \ + break; \ + case 0x38: \ + case 0x3c: \ + case 0x78: \ + case 0x7c: \ + OFFSET = 20; \ + break; \ + case 0x0e: \ + case 0x0f: \ + case 0x1e: \ + case 0x1f: \ + OFFSET = 22; \ + break; \ + case 0x10: \ + OFFSET = 24; \ + break; \ + case 0x04: \ + OFFSET = 26; \ + break; \ + case 0x01: \ + OFFSET = 28; \ + break; \ + case 0x40: \ + OFFSET = 30; \ + break; \ + default: \ + COUNT = 0; \ + for (BIT = 0; BIT < 8; BIT++) { \ + if ((1 << BIT & MASK) != 0) { \ + COUNT += 1; \ + } \ + } \ + if (COUNT < 4) { \ + OFFSET = 32; \ + } else { \ + OFFSET = 33; \ + } \ + MODULUS = 1; \ + } + +// rotate the gathered neighbour mask from map to screen orientation +#define ROTATE_BITFLAG_BY_ORIENTATION() \ + if (this->mapOrientation == OpenSHC::Map::Enums::MO_NORTH) { \ + this->bitFlag = MACRO_CALL_MEMBER(OpenSHC::Map::Navigation::DirectionAlgorithmState_Func::rotateByteLeft, \ + &DAT_DirectionAlgorithmState::instance)(this->bitFlag, 0); \ + } else if (this->mapOrientation == OpenSHC::Map::Enums::MO_EAST) { \ + this->bitFlag = MACRO_CALL_MEMBER(OpenSHC::Map::Navigation::DirectionAlgorithmState_Func::rotateByteLeft, \ + &DAT_DirectionAlgorithmState::instance)(this->bitFlag, 2); \ + } else if (this->mapOrientation == OpenSHC::Map::Enums::MO_SOUTH) { \ + this->bitFlag = MACRO_CALL_MEMBER(OpenSHC::Map::Navigation::DirectionAlgorithmState_Func::rotateByteLeft, \ + &DAT_DirectionAlgorithmState::instance)(this->bitFlag, 4); \ + } else if (this->mapOrientation == OpenSHC::Map::Enums::MO_WEST) { \ + this->bitFlag = MACRO_CALL_MEMBER(OpenSHC::Map::Navigation::DirectionAlgorithmState_Func::rotateByteLeft, \ + &DAT_DirectionAlgorithmState::instance)(this->bitFlag, 6); \ + } +#define MIN_OF(A, B) ((A) < (B) ? (A) : (B)) +// the original always loads a GMFirstPictureIndex entry as a whole dword before narrowing it, which MSVC only +// does for a volatile read; a plain read of an int into a short is narrowed to a word load +#define GM_FIRST_PICTURE_INDEX(I) (*(int volatile*)&GMFirstPictureIndex::instance[I]) + +namespace OpenSHC { +namespace Map { + + using OpenSHC::Map::LogicHelpers::L2_BEACH; + 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::L2_THICK_SCRUB; + 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_CRENEL; + using OpenSHC::Map::LogicHelpers::L_CRENEL_VARIATIONUnk; + using OpenSHC::Map::LogicHelpers::L_DEFAULT_EARTH_OR_TEXTURE; + 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; + 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_OIL; + using OpenSHC::Map::LogicHelpers::L_PEBBLES; + using OpenSHC::Map::LogicHelpers::L_PLAIN2_AND_PITCH; + using OpenSHC::Map::LogicHelpers::L_RIVER; + using OpenSHC::Map::LogicHelpers::L_ROCKY; + using OpenSHC::Map::LogicHelpers::L_SEA; + using OpenSHC::Map::LogicHelpers::L_STAIRS; + using OpenSHC::Map::LogicHelpers::L_WALL_OR_GATEHOUSE; + + using OpenSHC::Game::GameMode2; + + // FUNCTION: STRONGHOLDCRUSADER 0x00509180 + void TileMapState::updateGfxLayer() + { + // The original keeps all of these at fixed frame offsets. MSVC lays out the locals of a function without global + // optimisation by hash bucket of the name and then by declaration order, so every name here was picked to fall + // into one bucket (hence the odd suffixes) and they are declared from the lowest slot up. Renaming one moves + // the whole frame. + int heightStepA; + int rightAlphaTile; + int leftAlphaTileRef; + int moatTile6; + int moatDirectionValue; + int ditchIDCur; + int tileHeightAt; + int lowerTileIndex; + int riverMaskA; + int bankDirection; + int bankBase_; + int edgeBitId; + int edgeBitsValue; + int edgeOffsetId; + int edgeModulusWork; + int edgeMaskD; + int pathBitValue; + int pathBitsWork; + int pathOffsetVal; + int pathSymmetricNow; + int pathModulusWork; + int pathHighMaskCur; + int pathLowPos; + int seaMask_; + int wallMaskId; + int leftTile8; + int rightTileCalc; + int neighbourTilePos; + int shallowMaskMain; + int directionTmp; + int diagonalHeightG; + int firstTile3; + int wallHeightIndex; + int secondTileTmp; + int secondHeightHere; + int firstHeightVal; + int diagonalTileTmp; + int wallNeighboursIndex; + int cornerBitsVal; + int cornerMain; + int buildingWasOffsetOld; + int tileXTmp; + int alphaBaseCur; + int shadeLevelRef; + int buildingIDTmp; + int subColumnId; + int rockIndexId; + int cliffDropValue; + int subRow2; + int rampRotation; + int facingTile; + int blockRowVal; + int local_14_f; + int blockColumnRef; + int rotationNew; + int heightPicture_; + alphaBaseCur = 0; + if (this->forceUpdateTextureTilemap <= 0) { + return; + } + this->forceUpdateTextureTilemap -= 1; + rotationNew = DAT_PathFindingState::instance.mappingYRelated % 10; + for (blockRowVal = this->someIndex; blockRowVal <= this->someLimit; blockRowVal++) { + for (blockColumnRef = this->someYLike; blockColumnRef <= this->someYLikeLimit; blockColumnRef++) { + if (DAT_TileMapState::instance.mapping40x40[blockRowVal][blockColumnRef] != 0) { + for (subRow2 = rotationNew, rotationNew = 0; subRow2 < 10; subRow2++) { + for (subColumnId = 0, this->DAT_SomeY = blockRowVal * 10 + subRow2, + this->DAT_SomeX = blockColumnRef * 10; + subColumnId < 10; subColumnId++, this->DAT_SomeX++) { + if (MACRO_CALL_MEMBER(OpenSHC::Rendering::ViewportRenderState_Func::xyAreValid, + &DAT_ViewportRenderState::instance)(this->DAT_SomeX, this->DAT_SomeY) + == FALSE) { + continue; + } + this->DAT_SomeTile + = MACRO_CALL_MEMBER(OpenSHC::Rendering::ViewportRenderState_Func::translateXYToTile, + &DAT_ViewportRenderState::instance)(this->DAT_SomeX, this->DAT_SomeY); + if ((char)this->ChangedLayer[this->DAT_SomeTile] == 0) { + continue; + } + if ((short)this->BuildingLayer[this->DAT_SomeTile] != 0) { + buildingIDTmp = (short)this->BuildingLayer[this->DAT_SomeTile]; + if (DAT_BuildingsState::instance.buildings[buildingIDTmp].buildingType + == OpenSHC::Map::Buildings::BT_DRAWBRIDGE) { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::computeTileLuminescence, this)( + this->DAT_SomeTile, this->DAT_SomeY); + } + continue; + } + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::computeTileLuminescence, this)( + this->DAT_SomeTile, this->DAT_SomeY); + tileXTmp = this->DAT_SomeX; + this->FloatingLayer[this->DAT_SomeTile] = 0; + this->MiscDisplayLayer[this->DAT_SomeTile] + = (short)this->MiscDisplayLayer[this->DAT_SomeTile] & ~0x3c0; + this->MiscDisplayLayer[this->DAT_SomeTile] + = (short)this->MiscDisplayLayer[this->DAT_SomeTile] & ~0x800; + if ((this->LogicLayer[this->DAT_SomeTile] & L_RIVER) != 0) { + if (MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::isTileSuitableForBrushPlacement, + this)(this->DAT_SomeTile, tileXTmp, this->DAT_SomeY) + != FALSE) { + this->PillarGFXLayer[this->DAT_SomeTile] + = ((short)this->RandomLayer[this->DAT_SomeTile] & 0xf) + 35; + } else { + this->PillarGFXLayer[this->DAT_SomeTile] = 32; + } + this->GfxLayer[this->DAT_SomeTile] = GMFirstPictureIndex::instance[5] + 532; + continue; + } + if ((this->LogicLayer[this->DAT_SomeTile] & L_FORD) != 0) { + if (MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::isTileSuitableForBrushPlacement, + this)(this->DAT_SomeTile, tileXTmp, this->DAT_SomeY) + != FALSE) { + this->PillarGFXLayer[this->DAT_SomeTile] + = ((short)this->RandomLayer[this->DAT_SomeTile] & 0xf) + 35; + } else { + this->PillarGFXLayer[this->DAT_SomeTile] = 32; + } + this->GfxLayer[this->DAT_SomeTile] = GMFirstPictureIndex::instance[5] + 668; + continue; + } + if ((this->LogicLayer[this->DAT_SomeTile] & L_FARM_FIELD_WHEAT) != 0) { + rampRotation + = MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::computeClimbRampRotation, + this)(this->DAT_SomeTile, tileXTmp, this->DAT_SomeY); + this->PillarGFXLayer[this->DAT_SomeTile] + = GMFirstPictureIndex::instance[9] + this->wallCornerRotation - 1; + this->MiscDisplayLayer[this->DAT_SomeTile] + = (short)this->MiscDisplayLayer[this->DAT_SomeTile] | 0x800; + this->GfxLayer[this->DAT_SomeTile] = ((short)this->RandomLayer[this->DAT_SomeTile] & 3) + + GMFirstPictureIndex::instance[14]; + DAT_BuildingsState::instance.field_0x18e024 = 1; + continue; + } + if ((this->LogicLayer[this->DAT_SomeTile] & L_FARM_FIELD_HOP) != 0) { + rampRotation + = MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::computeClimbRampRotation, + this)(this->DAT_SomeTile, tileXTmp, this->DAT_SomeY); + this->PillarGFXLayer[this->DAT_SomeTile] + = GMFirstPictureIndex::instance[9] + this->wallCornerRotation - 1; + this->MiscDisplayLayer[this->DAT_SomeTile] + = (short)this->MiscDisplayLayer[this->DAT_SomeTile] | 0x800; + this->GfxLayer[this->DAT_SomeTile] = GMFirstPictureIndex::instance[14] + 37 + + ((short)this->RandomLayer[this->DAT_SomeTile] & 1) * 9; + DAT_BuildingsState::instance.field_0x18e024 = 1; + continue; + } + if ((this->LogicLayer[this->DAT_SomeTile] & (L_FARM_FIELD_APPLE | L_FARM_FIELD_DAIRY)) + != 0) { + continue; + } + if ((this->LogicLayer[this->DAT_SomeTile] + & (L_WALL_OR_GATEHOUSE | L_BUILDING | L_KEEP_NON_MANOR_HOUSE)) + == 0 + && this->BuildingWasLayer[this->DAT_SomeTile] != 0 + && ((short)this->MiscDisplayLayer[this->DAT_SomeTile] & 0x10) != 0) { + buildingWasOffsetOld = 0; + switch (this->BuildingWasLayer[this->DAT_SomeTile]) { + 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_KEEPFOUR: + case OpenSHC::Map::Buildings::BT_KEEPFIVE: + case OpenSHC::Map::Buildings::BT_GATEHOUSELARGE: + case OpenSHC::Map::Buildings::BT_GATEHOUSESMALL: + case OpenSHC::Map::Buildings::BT_WOODGATE2: + case OpenSHC::Map::Buildings::BT_SIGNPOST: + 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_UNKNOWN7: + case OpenSHC::Map::Buildings::BT_OUTPOST_EUROPEAN: + buildingWasOffsetOld = 12; + } + if (((short)this->MiscDisplayLayer[this->DAT_SomeTile] & 0x2000) != 0) { + this->GfxLayer[this->DAT_SomeTile] = GMFirstPictureIndex::instance[149] + + (buildingWasOffsetOld + 1) + + ((short)this->RandomLayer[this->DAT_SomeTile] & 7) - 1; + } else { + this->GfxLayer[this->DAT_SomeTile] = GMFirstPictureIndex::instance[149] + + (buildingWasOffsetOld + 5) + + ((short)this->RandomLayer[this->DAT_SomeTile] & 7) - 1; + } + } else if ((this->LogicLayer[this->DAT_SomeTile] & L_WALL_OR_GATEHOUSE) != 0) { + this->wallCornerRotation = MACRO_CALL_MEMBER( + OpenSHC::Map::TileMapState_Func::computeWallCornerRenderRotation, this)(tileXTmp); + this->WallGFXLayer[this->DAT_SomeTile] + = GMFirstPictureIndex::instance[10] + this->wallCornerRotation - 1; + if ((char)this->DamageLayer[this->DAT_SomeTile] != 0) { + this->GfxLayer[this->DAT_SomeTile] = GMFirstPictureIndex::instance[12] + 138 + + ((short)this->RandomLayer[this->DAT_SomeTile] & 7); + } else if ((this->LogicLayer[this->DAT_SomeTile] & L_CRENEL) != 0) { + if (MACRO_CALL_MEMBER( + OpenSHC::Map::TileMapState_Func::isWallCornerForCardinalDirection, this)( + this->DAT_SomeTile, this->DAT_SomeY, 0) + != FALSE) { + if (this->mapOrientation == OpenSHC::Map::Enums::MO_NORTH + || this->mapOrientation == OpenSHC::Map::Enums::MO_SOUTH) { + if ((this->LogicLayer[this->DAT_SomeTile] & L_CRENEL_VARIATIONUnk) != 0) { + this->GfxLayer[this->DAT_SomeTile] = GMFirstPictureIndex::instance[12] + + 124 + ((short)this->RandomLayer[this->DAT_SomeTile] & 3); + } else { + this->GfxLayer[this->DAT_SomeTile] = GMFirstPictureIndex::instance[12] + + 116 + ((short)this->RandomLayer[this->DAT_SomeTile] & 3); + } + } else if ((this->LogicLayer[this->DAT_SomeTile] & L_CRENEL_VARIATIONUnk) + != 0) { + this->GfxLayer[this->DAT_SomeTile] = GMFirstPictureIndex::instance[12] + 120 + + ((short)this->RandomLayer[this->DAT_SomeTile] & 3); + } else { + this->GfxLayer[this->DAT_SomeTile] = GMFirstPictureIndex::instance[12] + 112 + + ((short)this->RandomLayer[this->DAT_SomeTile] & 3); + } + } else if (MACRO_CALL_MEMBER( + OpenSHC::Map::TileMapState_Func::isWallCornerForCardinalDirection, + this)(this->DAT_SomeTile, this->DAT_SomeY, 2) + != FALSE) { + if (this->mapOrientation == OpenSHC::Map::Enums::MO_NORTH + || this->mapOrientation == OpenSHC::Map::Enums::MO_SOUTH) { + if ((this->LogicLayer[this->DAT_SomeTile] & L_CRENEL_VARIATIONUnk) != 0) { + this->GfxLayer[this->DAT_SomeTile] = GMFirstPictureIndex::instance[12] + + 120 + ((short)this->RandomLayer[this->DAT_SomeTile] & 3); + } else { + this->GfxLayer[this->DAT_SomeTile] = GMFirstPictureIndex::instance[12] + + 112 + ((short)this->RandomLayer[this->DAT_SomeTile] & 3); + } + } else if ((this->LogicLayer[this->DAT_SomeTile] & L_CRENEL_VARIATIONUnk) + != 0) { + this->GfxLayer[this->DAT_SomeTile] = GMFirstPictureIndex::instance[12] + 124 + + ((short)this->RandomLayer[this->DAT_SomeTile] & 3); + } else { + this->GfxLayer[this->DAT_SomeTile] = GMFirstPictureIndex::instance[12] + 116 + + ((short)this->RandomLayer[this->DAT_SomeTile] & 3); + } + } else if (MACRO_CALL_MEMBER( + OpenSHC::Map::TileMapState_Func::isWallCornerForDiagonalDirection, + this)(this->DAT_SomeTile, this->DAT_SomeY, 1) + != FALSE) { + if (this->mapOrientation == OpenSHC::Map::Enums::MO_NORTH) { + this->GfxLayer[this->DAT_SomeTile] + = GMFirstPictureIndex::instance[12] + 130; + } else if (this->mapOrientation == OpenSHC::Map::Enums::MO_EAST) { + this->GfxLayer[this->DAT_SomeTile] + = GMFirstPictureIndex::instance[12] + 129; + } else if (this->mapOrientation == OpenSHC::Map::Enums::MO_SOUTH) { + this->GfxLayer[this->DAT_SomeTile] + = GMFirstPictureIndex::instance[12] + 128; + } else if (this->mapOrientation == OpenSHC::Map::Enums::MO_WEST) { + this->GfxLayer[this->DAT_SomeTile] + = GMFirstPictureIndex::instance[12] + 131; + } + this->LogicLayer[this->DAT_SomeTile] |= L_CRENEL_VARIATIONUnk; + } else if (MACRO_CALL_MEMBER( + OpenSHC::Map::TileMapState_Func::isWallCornerForDiagonalDirection, + this)(this->DAT_SomeTile, this->DAT_SomeY, 3) + != FALSE) { + if (this->mapOrientation == OpenSHC::Map::Enums::MO_NORTH) { + this->GfxLayer[this->DAT_SomeTile] + = GMFirstPictureIndex::instance[12] + 131; + } else if (this->mapOrientation == OpenSHC::Map::Enums::MO_EAST) { + this->GfxLayer[this->DAT_SomeTile] + = GMFirstPictureIndex::instance[12] + 130; + } else if (this->mapOrientation == OpenSHC::Map::Enums::MO_SOUTH) { + this->GfxLayer[this->DAT_SomeTile] + = GMFirstPictureIndex::instance[12] + 129; + } else if (this->mapOrientation == OpenSHC::Map::Enums::MO_WEST) { + this->GfxLayer[this->DAT_SomeTile] + = GMFirstPictureIndex::instance[12] + 128; + } + this->LogicLayer[this->DAT_SomeTile] |= L_CRENEL_VARIATIONUnk; + } else if (MACRO_CALL_MEMBER( + OpenSHC::Map::TileMapState_Func::isWallCornerForDiagonalDirection, + this)(this->DAT_SomeTile, this->DAT_SomeY, 5) + != FALSE) { + if (this->mapOrientation == OpenSHC::Map::Enums::MO_NORTH) { + this->GfxLayer[this->DAT_SomeTile] + = GMFirstPictureIndex::instance[12] + 128; + } else if (this->mapOrientation == OpenSHC::Map::Enums::MO_EAST) { + this->GfxLayer[this->DAT_SomeTile] + = GMFirstPictureIndex::instance[12] + 131; + } else if (this->mapOrientation == OpenSHC::Map::Enums::MO_SOUTH) { + this->GfxLayer[this->DAT_SomeTile] + = GMFirstPictureIndex::instance[12] + 130; + } else if (this->mapOrientation == OpenSHC::Map::Enums::MO_WEST) { + this->GfxLayer[this->DAT_SomeTile] + = GMFirstPictureIndex::instance[12] + 129; + } + this->LogicLayer[this->DAT_SomeTile] |= L_CRENEL_VARIATIONUnk; + } else if (MACRO_CALL_MEMBER( + OpenSHC::Map::TileMapState_Func::isWallCornerForDiagonalDirection, + this)(this->DAT_SomeTile, this->DAT_SomeY, 7) + != FALSE) { + if (this->mapOrientation == OpenSHC::Map::Enums::MO_NORTH) { + this->GfxLayer[this->DAT_SomeTile] + = GMFirstPictureIndex::instance[12] + 129; + } else if (this->mapOrientation == OpenSHC::Map::Enums::MO_EAST) { + this->GfxLayer[this->DAT_SomeTile] + = GMFirstPictureIndex::instance[12] + 128; + } else if (this->mapOrientation == OpenSHC::Map::Enums::MO_SOUTH) { + this->GfxLayer[this->DAT_SomeTile] + = GMFirstPictureIndex::instance[12] + 131; + } else if (this->mapOrientation == OpenSHC::Map::Enums::MO_WEST) { + this->GfxLayer[this->DAT_SomeTile] + = GMFirstPictureIndex::instance[12] + 130; + } + this->LogicLayer[this->DAT_SomeTile] |= L_CRENEL_VARIATIONUnk; + } else { + this->GfxLayer[this->DAT_SomeTile] = GMFirstPictureIndex::instance[12] + 132; + this->LogicLayer[this->DAT_SomeTile] |= L_CRENEL_VARIATIONUnk; + } + } else if ((this->LogicLayer[this->DAT_SomeTile] & L_STAIRS) != 0) { + if (MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::hasHigherNeighborWithStairs, + this)(this->DAT_SomeTile, this->DAT_SomeY, 0) + != FALSE) { + this->GfxLayer[this->DAT_SomeTile] = GMFirstPictureIndex::instance[12] + 134; + } else if (MACRO_CALL_MEMBER( + OpenSHC::Map::TileMapState_Func::hasHigherNeighborWithStairs, this)( + this->DAT_SomeTile, this->DAT_SomeY, 2) + != FALSE) { + this->GfxLayer[this->DAT_SomeTile] = GMFirstPictureIndex::instance[12] + 135; + } else if (MACRO_CALL_MEMBER( + OpenSHC::Map::TileMapState_Func::hasHigherNeighborWithStairs, this)( + this->DAT_SomeTile, this->DAT_SomeY, 4) + != FALSE) { + this->GfxLayer[this->DAT_SomeTile] = GMFirstPictureIndex::instance[12] + 136; + } else if (MACRO_CALL_MEMBER( + OpenSHC::Map::TileMapState_Func::hasHigherNeighborWithStairs, this)( + this->DAT_SomeTile, this->DAT_SomeY, 6) + != FALSE) { + this->GfxLayer[this->DAT_SomeTile] = GMFirstPictureIndex::instance[12] + 133; + } else if (MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func:: + hasHigherPlainNeighborWithWallOrGatehouse, + this)(this->DAT_SomeTile, this->DAT_SomeY, 0) + != FALSE) { + this->GfxLayer[this->DAT_SomeTile] = GMFirstPictureIndex::instance[12] + 134; + } else if (MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func:: + hasHigherPlainNeighborWithWallOrGatehouse, + this)(this->DAT_SomeTile, this->DAT_SomeY, 2) + != FALSE) { + this->GfxLayer[this->DAT_SomeTile] = GMFirstPictureIndex::instance[12] + 135; + } else if (MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func:: + hasHigherPlainNeighborWithWallOrGatehouse, + this)(this->DAT_SomeTile, this->DAT_SomeY, 4) + != FALSE) { + this->GfxLayer[this->DAT_SomeTile] = GMFirstPictureIndex::instance[12] + 136; + } else if (MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func:: + hasHigherPlainNeighborWithWallOrGatehouse, + this)(this->DAT_SomeTile, this->DAT_SomeY, 6) + != FALSE) { + this->GfxLayer[this->DAT_SomeTile] = GMFirstPictureIndex::instance[12] + 133; + } else { + this->GfxLayer[this->DAT_SomeTile] = GMFirstPictureIndex::instance[12] + 104; + } + } else if (this->LuminesenceLayer[this->DAT_SomeTile] >= 4) { + this->GfxLayer[this->DAT_SomeTile] = GMFirstPictureIndex::instance[12] + 104 + + ((short)this->RandomLayer[this->DAT_SomeTile] & 7); + } else { + this->GfxLayer[this->DAT_SomeTile] = GMFirstPictureIndex::instance[12] + 96 + + ((short)this->RandomLayer[this->DAT_SomeTile] & 7); + } + continue; + } else if ((this->LogicLayer[this->DAT_SomeTile] + & (L_SEA | L_ROCKY | L_WALL_OR_GATEHOUSE | L_BUILDING | L_BOULDERS + | L_PEBBLES | L_IRON | L_RIVER | L_FARM_FIELD_WHEAT | L_FARM_FIELD_HOP + | L_FARM_FIELD_APPLE | L_FARM_FIELD_DAIRY | L_KEEP_NON_MANOR_HOUSE + | L_MARSH | L_OIL)) + == 0) { + cornerMain = 0; + this->WallOwnerLayer[this->DAT_SomeTile] &= ~8; + MACRO_NEIGHBOUR_FLAGS_GLOBAL_LOGIC_8(wall, NEIGHBOUR_FLAGS_MASK_WALL_OR_GATEHOUSE) + wallNeighboursIndex = this->bitFlag; + MACRO_NEIGHBOUR_FLAGS_GLOBAL_LOGIC_8(m1, 2) + wallNeighboursIndex = ~this->bitFlag & wallNeighboursIndex; + if (wallNeighboursIndex != 0) { + cornerBitsVal = wallNeighboursIndex & 0xaa; + switch (cornerBitsVal) { + case 0x82: + cornerMain = 3; + if ((wallNeighboursIndex & 0x45) == 0x45) { + cornerMain = 0; + } + break; + case 0x0a: + cornerMain = 2; + if ((wallNeighboursIndex & 0x15) == 0x15) { + cornerMain = 0; + } + break; + case 0x28: + cornerMain = 1; + if ((wallNeighboursIndex & 0x54) == 0x54) { + cornerMain = 0; + } + break; + case 0xa0: + cornerMain = 4; + // the original compares the masked bits against 0x15, which they can never + // equal, so this corner is never cleared here + if ((wallNeighboursIndex & 0x41) == 0x15) { + cornerMain = 0; + } + } + if (cornerMain != 0) { + MACRO_NEIGHBOUR_FLAGS_GLOBAL_LOGIC_8(r1, 0x800) + if (this->bitFlag != 0) { + cornerMain = 0; + } + } + if (cornerMain != 0) { + // the corner picks its two cardinal neighbours and the diagonal between them + switch (cornerMain) { + case 1: + firstTile3 = this->DAT_SomeTile + DIRECTION_OFFSET(this->DAT_SomeY, 2); + secondTileTmp = this->DAT_SomeTile + DIRECTION_OFFSET(this->DAT_SomeY, 4); + diagonalTileTmp = this->DAT_SomeTile + DIRECTION_OFFSET(this->DAT_SomeY, 3); + break; + case 2: + firstTile3 = this->DAT_SomeTile + DIRECTION_OFFSET(this->DAT_SomeY, 6); + secondTileTmp = this->DAT_SomeTile + DIRECTION_OFFSET(this->DAT_SomeY, 4); + diagonalTileTmp = this->DAT_SomeTile + DIRECTION_OFFSET(this->DAT_SomeY, 5); + break; + case 3: + firstTile3 = this->DAT_SomeTile + DIRECTION_OFFSET(this->DAT_SomeY, 6); + secondTileTmp = this->DAT_SomeTile + DIRECTION_OFFSET(this->DAT_SomeY, 0); + diagonalTileTmp = this->DAT_SomeTile + DIRECTION_OFFSET(this->DAT_SomeY, 7); + break; + case 4: + firstTile3 = this->DAT_SomeTile + DIRECTION_OFFSET(this->DAT_SomeY, 2); + secondTileTmp = this->DAT_SomeTile + DIRECTION_OFFSET(this->DAT_SomeY, 0); + diagonalTileTmp = this->DAT_SomeTile + DIRECTION_OFFSET(this->DAT_SomeY, 1); + } + firstHeightVal = this->HeightLayer[firstTile3]; + secondHeightHere = this->HeightLayer[secondTileTmp]; + diagonalHeightG = this->HeightLayer[diagonalTileTmp]; + if ((DAT_TileMapState::instance.LogicLayer[diagonalTileTmp] + & L_WALL_OR_GATEHOUSE) + == 0) { + diagonalHeightG = secondHeightHere; + } + wallHeightIndex + = MIN_OF(MIN_OF(firstHeightVal, secondHeightHere), diagonalHeightG); + if (this->HeightLayer[this->DAT_SomeTile] >= wallHeightIndex) { + cornerMain = 0; + } else { + this->WallGFXLayer[this->DAT_SomeTile] = (byte)wallHeightIndex; + if (DAT_TileMapState::instance.DamageLayer[firstTile3] != 0 + || DAT_TileMapState::instance.DamageLayer[secondTileTmp] != 0 + || (DAT_TileMapState::instance.DamageLayer[diagonalTileTmp] != 0 + && (DAT_TileMapState::instance.LogicLayer[diagonalTileTmp] + & L_WALL_OR_GATEHOUSE) + != 0)) { + this->WallOwnerLayer[this->DAT_SomeTile] |= 8; + } + } + } + if (cornerMain != 0) { + cornerMain = ((cornerMain + 3) - this->mapOrientation / 2) % 4 + 1; + switch (cornerMain) { + case 1: + this->MiscDisplayLayer[this->DAT_SomeTile] + = (short)this->MiscDisplayLayer[this->DAT_SomeTile] | 0x40; + this->AlphaGFXLayer[this->DAT_SomeTile] = 47; + break; + case 2: + this->MiscDisplayLayer[this->DAT_SomeTile] + = (short)this->MiscDisplayLayer[this->DAT_SomeTile] | 0x200; + this->AlphaGFXLayer[this->DAT_SomeTile] = 46; + break; + case 3: + this->MiscDisplayLayer[this->DAT_SomeTile] + = (short)this->MiscDisplayLayer[this->DAT_SomeTile] | 0x80; + this->AlphaGFXLayer[this->DAT_SomeTile] = 49; + break; + case 4: + this->MiscDisplayLayer[this->DAT_SomeTile] + = (short)this->MiscDisplayLayer[this->DAT_SomeTile] | 0x100; + this->AlphaGFXLayer[this->DAT_SomeTile] = 48; + } + } + } + } + } + } + } + } + } + rotationNew = DAT_PathFindingState::instance.mappingYRelated % 10; + for (blockRowVal = this->someIndex; blockRowVal <= this->someLimit; blockRowVal++) { + for (blockColumnRef = this->someYLike; blockColumnRef <= this->someYLikeLimit; blockColumnRef++) { + if (DAT_TileMapState::instance.mapping40x40[blockRowVal][blockColumnRef] != 0) { + for (subRow2 = rotationNew, rotationNew = 0; subRow2 < 10; subRow2++) { + for (subColumnId = 0, this->DAT_SomeY = blockRowVal * 10 + subRow2, + this->DAT_SomeX = blockColumnRef * 10; + subColumnId < 10; subColumnId++, this->DAT_SomeX++) { + if (MACRO_CALL_MEMBER(OpenSHC::Rendering::ViewportRenderState_Func::xyAreValid, + &DAT_ViewportRenderState::instance)(this->DAT_SomeX, this->DAT_SomeY) + == FALSE) { + continue; + } + this->DAT_SomeTile + = MACRO_CALL_MEMBER(OpenSHC::Rendering::ViewportRenderState_Func::translateXYToTile, + &DAT_ViewportRenderState::instance)(this->DAT_SomeX, this->DAT_SomeY); + if ((this->LogicLayer[this->DAT_SomeTile] & (L_SEA | L_CRENEL | L_RIVER)) == 0) { + continue; + } + if ((char)this->ChangedLayer[this->DAT_SomeTile] == 0) { + continue; + } + if ((short)this->BuildingLayer[this->DAT_SomeTile] != 0) { + continue; + } + if ((this->LogicLayer[this->DAT_SomeTile] + & (L_BORDER | L_BORDER_EDGE | L_KEEP_NON_MANOR_HOUSE)) + != 0) { + continue; + } + tileXTmp = this->DAT_SomeX; + if ((this->LogicLayer[this->DAT_SomeTile] & L_SEA) != 0) { + this->MiscDisplayLayer[this->DAT_SomeTile] + = (short)this->MiscDisplayLayer[this->DAT_SomeTile] & 0xfffc; + facingTile + = this->DAT_SomeTile + DIRECTION_OFFSET(this->DAT_SomeY, this->mapOrientation); + this->gfxTileHeight = this->HeightLayer[facingTile]; + if ((this->LogicLayer[facingTile] & L_SEA) != 0) { + this->gfxTileHeight = 0; + } + if ((this->LogicLayer[facingTile] & L_ROCKY) != 0) { + this->gfxTileHeight = 20; + } + if ((short)this->gfxTileHeight >= 20) { + if ((short)this->gfxTileHeight > 90) { + this->GfxLayer[this->DAT_SomeTile] = GMFirstPictureIndex::instance[166] + 1518; + } else if ((short)this->gfxTileHeight > 40) { + this->GfxLayer[this->DAT_SomeTile] = GMFirstPictureIndex::instance[166] + 1506; + } else { + this->GfxLayer[this->DAT_SomeTile] = GMFirstPictureIndex::instance[166] + 1500; + } + this->PillarGFXLayer[this->DAT_SomeTile] = GM_FIRST_PICTURE_INDEX(9); + this->MiscDisplayLayer[this->DAT_SomeTile] + = (short)this->MiscDisplayLayer[this->DAT_SomeTile] | 0x40; + } + shallowMaskMain = 0; + this->bitFlag = 0; + for (directionTmp = 0; directionTmp < 8; directionTmp++) { + neighbourTilePos + = this->DAT_SomeTile + DIRECTION_OFFSET(this->DAT_SomeY, directionTmp); + if (DAT_TileMapState::instance.HeightLayer[neighbourTilePos] < 20) { + if ((this->LogicLayer[neighbourTilePos] + & (L_SEA | L_BORDER | L_BORDER_EDGE | L_RIVER)) + == 0) { + this->bitFlag |= 0x80 >> directionTmp; + } + if ((this->LogicLayer[neighbourTilePos] & (L_SEA | L_RIVER)) == 0 + && ((char)this->Logic2Layer[neighbourTilePos] & 0x20) != 0) { + shallowMaskMain = 0x80 >> directionTmp | shallowMaskMain; + } + } + } + if (this->bitFlag != 0) { + this->MiscDisplayLayer[this->DAT_SomeTile] + = (short)this->MiscDisplayLayer[this->DAT_SomeTile] | 0x100; + this->CertainPathLayer[this->DAT_SomeTile] = this->bitFlag | shallowMaskMain << 8; + } + } else if ((this->LogicLayer[this->DAT_SomeTile] & L_RIVER) != 0) { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::computeTileCliffEdgeFlags, this)( + this->DAT_SomeTile, tileXTmp, this->DAT_SomeY); + } + if ((this->LogicLayer[this->DAT_SomeTile] & L_CRENEL) != 0) { + leftTile8 = this->DAT_SomeTile + + DIRECTION_OFFSET(this->DAT_SomeY, (this->screenSouthEastDirection + 7) % 8); + rightTileCalc = this->DAT_SomeTile + + DIRECTION_OFFSET(this->DAT_SomeY, (this->screenSouthEastDirection + 1) % 8); + if (((this->LogicLayer[leftTile8] | this->LogicLayer[rightTileCalc]) & L_SEA) == 0 + && ((short)this->MiscDisplayLayer[leftTile8] & 0x3c0) != 0 + && ((short)this->MiscDisplayLayer[rightTileCalc] & 0x3c0) != 0) { + if (this->LuminesenceLayer[this->DAT_SomeTile] >= 4) { + this->GfxLayer[this->DAT_SomeTile] = GMFirstPictureIndex::instance[12] + 104 + + ((short)this->RandomLayer[this->DAT_SomeTile] & 7); + } else { + this->GfxLayer[this->DAT_SomeTile] = GMFirstPictureIndex::instance[12] + 96 + + ((short)this->RandomLayer[this->DAT_SomeTile] & 7); + } + } + leftTile8 = this->DAT_SomeTile + + DIRECTION_OFFSET(this->DAT_SomeY, (this->screenSouthEastDirection + 1) % 8); + rightTileCalc = this->DAT_SomeTile + + DIRECTION_OFFSET(this->DAT_SomeY, (this->screenSouthEastDirection + 3) % 8); + if (((this->LogicLayer[leftTile8] | this->LogicLayer[rightTileCalc]) & L_SEA) == 0 + && ((short)this->MiscDisplayLayer[leftTile8] & 0x3c0) != 0 + && ((short)this->MiscDisplayLayer[rightTileCalc] & 0x3c0) != 0) { + if ((this->DAT_SomeY & 1) != 0) { + this->GfxLayer[this->DAT_SomeTile] = GMFirstPictureIndex::instance[12] + 311; + } else { + this->GfxLayer[this->DAT_SomeTile] = GMFirstPictureIndex::instance[12] + 312; + } + this->MiscDisplayLayer[this->DAT_SomeTile] + = (short)this->MiscDisplayLayer[this->DAT_SomeTile] | 0x3c0; + } + leftTile8 = this->DAT_SomeTile + + DIRECTION_OFFSET(this->DAT_SomeY, (this->screenSouthEastDirection + 5) % 8); + rightTileCalc = this->DAT_SomeTile + + DIRECTION_OFFSET(this->DAT_SomeY, (this->screenSouthEastDirection + 7) % 8); + if (((this->LogicLayer[leftTile8] | this->LogicLayer[rightTileCalc]) & L_SEA) == 0 + && ((short)this->MiscDisplayLayer[leftTile8] & 0x3c0) != 0 + && ((short)this->MiscDisplayLayer[rightTileCalc] & 0x3c0) != 0) { + if ((this->DAT_SomeY & 1) != 0) { + this->GfxLayer[this->DAT_SomeTile] = GMFirstPictureIndex::instance[12] + 313; + } else { + this->GfxLayer[this->DAT_SomeTile] = GMFirstPictureIndex::instance[12] + 314; + } + this->MiscDisplayLayer[this->DAT_SomeTile] + = (short)this->MiscDisplayLayer[this->DAT_SomeTile] | 0x3c0; + } + leftTile8 = this->DAT_SomeTile + + DIRECTION_OFFSET(this->DAT_SomeY, (this->screenSouthEastDirection + 3) % 8); + rightTileCalc = this->DAT_SomeTile + + DIRECTION_OFFSET(this->DAT_SomeY, (this->screenSouthEastDirection + 5) % 8); + if (((this->LogicLayer[leftTile8] | this->LogicLayer[rightTileCalc]) & L_SEA) == 0 + && ((short)this->MiscDisplayLayer[leftTile8] & 0x3c0) != 0 + && ((short)this->MiscDisplayLayer[rightTileCalc] & 0x3c0) != 0) { + this->GfxLayer[this->DAT_SomeTile] + = GMFirstPictureIndex::instance[12] + 303 + this->DAT_SomeY % 8; + this->MiscDisplayLayer[this->DAT_SomeTile] + = (short)this->MiscDisplayLayer[this->DAT_SomeTile] | 0x3c0; + } + } + } + } + } + } + } + rotationNew = DAT_PathFindingState::instance.mappingYRelated % 10; + for (blockRowVal = this->someIndex; blockRowVal <= this->someLimit; blockRowVal++) { + for (blockColumnRef = this->someYLike; blockColumnRef <= this->someYLikeLimit; blockColumnRef++) { + if (DAT_TileMapState::instance.mapping40x40[blockRowVal][blockColumnRef] != 0) { + for (subRow2 = rotationNew, rotationNew = 0; subRow2 < 10; subRow2++) { + for (subColumnId = 0, this->DAT_SomeY = blockRowVal * 10 + subRow2, + this->DAT_SomeX = blockColumnRef * 10; + subColumnId < 10; subColumnId++, this->DAT_SomeX++) { + if (MACRO_CALL_MEMBER(OpenSHC::Rendering::ViewportRenderState_Func::xyAreValid, + &DAT_ViewportRenderState::instance)(this->DAT_SomeX, this->DAT_SomeY) + == FALSE) { + continue; + } + this->DAT_SomeTile + = MACRO_CALL_MEMBER(OpenSHC::Rendering::ViewportRenderState_Func::translateXYToTile, + &DAT_ViewportRenderState::instance)(this->DAT_SomeX, this->DAT_SomeY); + if ((this->LogicLayer[this->DAT_SomeTile] & L_SEA) == 0) { + continue; + } + if ((char)this->ChangedLayer[this->DAT_SomeTile] == 0) { + continue; + } + if ((this->LogicLayer[this->DAT_SomeTile] + & (L_BORDER | L_BORDER_EDGE | L_KEEP_NON_MANOR_HOUSE)) + != 0) { + continue; + } + if ((short)this->BuildingLayer[this->DAT_SomeTile] != 0) { + continue; + } + if (((short)this->MiscDisplayLayer[this->DAT_SomeTile] & 0x40) != 0) { + } else { + MACRO_NEIGHBOUR_FLAGS_GLOBAL_MISC_8(m2, 0x80) + wallMaskId = this->bitFlag; + MACRO_NEIGHBOUR_FLAGS_GLOBAL_MISC_8(m3, 0x40) + this->bitFlag &= ~wallMaskId; + this->bitFlag &= 0xe3; + if (this->bitFlag != 0) { + this->GfxLayer[this->DAT_SomeTile] = GMFirstPictureIndex::instance[166] + 1488; + this->PillarGFXLayer[this->DAT_SomeTile] = GM_FIRST_PICTURE_INDEX(9); + this->MiscDisplayLayer[this->DAT_SomeTile] + = (short)this->MiscDisplayLayer[this->DAT_SomeTile] | 0x80; + } + } + } + } + } + } + } + rotationNew = DAT_PathFindingState::instance.mappingYRelated % 10; + for (blockRowVal = this->someIndex; blockRowVal <= this->someLimit; blockRowVal++) { + for (blockColumnRef = this->someYLike; blockColumnRef <= this->someYLikeLimit; blockColumnRef++) { + if (DAT_TileMapState::instance.mapping40x40[blockRowVal][blockColumnRef] != 0) { + for (subRow2 = rotationNew, rotationNew = 0; subRow2 < 10; subRow2++) { + for (subColumnId = 0, this->DAT_SomeY = blockRowVal * 10 + subRow2, + this->DAT_SomeX = blockColumnRef * 10; + subColumnId < 10; subColumnId++, this->DAT_SomeX++) { + if (MACRO_CALL_MEMBER(OpenSHC::Rendering::ViewportRenderState_Func::xyAreValid, + &DAT_ViewportRenderState::instance)(this->DAT_SomeX, this->DAT_SomeY) + == FALSE) { + continue; + } + this->DAT_SomeTile + = MACRO_CALL_MEMBER(OpenSHC::Rendering::ViewportRenderState_Func::translateXYToTile, + &DAT_ViewportRenderState::instance)(this->DAT_SomeX, this->DAT_SomeY); + if ((this->LogicLayer[this->DAT_SomeTile] & (L_BORDER | L_BORDER_EDGE)) != 0) { + continue; + } + if ((char)this->ChangedLayer[this->DAT_SomeTile] == 0) { + continue; + } + this->ChangedLayer[this->DAT_SomeTile] = (char)this->ChangedLayer[this->DAT_SomeTile] - 1; + tileXTmp = this->DAT_SomeX; + if ((this->LogicLayer[this->DAT_SomeTile] & L_SEA) != 0) { + this->Logic2Layer[this->DAT_SomeTile] + = (char)this->Logic2Layer[this->DAT_SomeTile] & ~8; + MACRO_NEIGHBOUR_FLAGS_GLOBAL_LOGIC_4(sea1, NEIGHBOUR_FLAGS_MASK_RIVER) + if (this->bitFlag != 0) { + this->GfxLayer[this->DAT_SomeTile] = GMFirstPictureIndex::instance[5] + 764; + this->Logic2Layer[this->DAT_SomeTile] + = (char)this->Logic2Layer[this->DAT_SomeTile] | 8; + continue; + } + if (((short)this->MiscDisplayLayer[this->DAT_SomeTile] & 0xc0) != 0) { + } else { + MACRO_NEIGHBOUR_FLAGS_GLOBAL_MISC_8(sea2, 0x40) + seaMask_ = this->bitFlag; + MACRO_NEIGHBOUR_FLAGS_GLOBAL_MISC_8(sea3, 0x80) + this->bitFlag &= ~seaMask_; + this->bitFlag &= 0xe3; + if (this->bitFlag != 0) { + this->GfxLayer[this->DAT_SomeTile] = GMFirstPictureIndex::instance[166] + 1476; + this->PillarGFXLayer[this->DAT_SomeTile] = GM_FIRST_PICTURE_INDEX(9); + this->MiscDisplayLayer[this->DAT_SomeTile] + = (short)this->MiscDisplayLayer[this->DAT_SomeTile] | 0xc0; + } + } + if (this->LuminesenceLayer[this->DAT_SomeTile] > 2) { + this->MiscDisplayLayer[this->DAT_SomeTile] + = (short)this->MiscDisplayLayer[this->DAT_SomeTile] & ~0x40; + this->MiscDisplayLayer[this->DAT_SomeTile] + = (short)this->MiscDisplayLayer[this->DAT_SomeTile] & ~0x80; + } + shadeLevelRef = this->LuminesenceLayer[this->DAT_SomeTile] - 2; + if (shadeLevelRef < 0) { + shadeLevelRef = 0; + } + if (((short)this->MiscDisplayLayer[this->DAT_SomeTile] & 0xc0) != 0) { + } else if (((short)this->MiscDisplayLayer[this->DAT_SomeTile] & 0x100) != 0) { + pathModulusWork = 2; + pathLowPos = this->CertainPathLayer[this->DAT_SomeTile] & 0xff; + pathHighMaskCur = this->CertainPathLayer[this->DAT_SomeTile] >> 8 & 0xff; + pathSymmetricNow = 0; + if (pathLowPos == pathHighMaskCur) { + pathSymmetricNow = 1; + } + pathLowPos <<= DAT_TileMapState::instance.mapOrientation; + pathLowPos = pathLowPos & 0xff | pathLowPos >> 8; + SEA_EDGE_PICK( + pathLowPos, pathOffsetVal, pathModulusWork, pathBitsWork, pathBitValue) + pathOffsetVal + = this->RandomLayer[this->DAT_SomeTile] % pathModulusWork + pathOffsetVal; + if (pathSymmetricNow == 0) { + pathOffsetVal += 102; + } + this->GfxLayer[this->DAT_SomeTile] = GMFirstPictureIndex::instance[166] + + (pathOffsetVal + 43) + shadeLevelRef * 246 - 1; + } else { + MACRO_NEIGHBOUR_FLAGS_GLOBAL_MISC_8(sea4, 0x100) + if (this->bitFlag != 0) { + edgeModulusWork = 2; + edgeMaskD = this->bitFlag; + edgeMaskD <<= DAT_TileMapState::instance.mapOrientation; + edgeMaskD = edgeMaskD & 0xff | edgeMaskD >> 8; + SEA_EDGE_PICK( + edgeMaskD, edgeOffsetId, edgeModulusWork, edgeBitsValue, edgeBitId) + edgeOffsetId + = this->RandomLayer[this->DAT_SomeTile] % edgeModulusWork + edgeOffsetId; + this->GfxLayer[this->DAT_SomeTile] = GMFirstPictureIndex::instance[166] + + (edgeOffsetId + 9) + shadeLevelRef * 246 - 1; + this->MiscDisplayLayer[this->DAT_SomeTile] + = (short)this->MiscDisplayLayer[this->DAT_SomeTile] | 0x200; + } + } + if (((short)this->MiscDisplayLayer[this->DAT_SomeTile] & 0x3c0) == 0) { + this->GfxLayer[this->DAT_SomeTile] + = ((short)this->RandomLayer[this->DAT_SomeTile] & 7) + + GMFirstPictureIndex::instance[166] + shadeLevelRef * 246; + this->PillarGFXLayer[this->DAT_SomeTile] = GM_FIRST_PICTURE_INDEX(9); + } + continue; + } else if ((this->LogicLayer[this->DAT_SomeTile] & L_RIVER) != 0) { + this->Logic2Layer[this->DAT_SomeTile] + = (char)this->Logic2Layer[this->DAT_SomeTile] & ~8; + MACRO_NEIGHBOUR_FLAGS_GLOBAL_LOGIC_4(riv1, 1) + if (this->bitFlag != 0) { + this->Logic2Layer[this->DAT_SomeTile] + = (char)this->Logic2Layer[this->DAT_SomeTile] | 8; + if (((char)this->Logic2Layer[this->DAT_SomeTile] & L2_BEACH) != 0) { + this->Logic2Layer[this->DAT_SomeTile] + = (char)this->Logic2Layer[this->DAT_SomeTile] & ~L2_BEACH; + this->WallGFXLayer[this->DAT_SomeTile] + = (short)this->RandomLayer[this->DAT_SomeTile] & 7; + } + } + MACRO_NEIGHBOUR_FLAGS_GLOBAL_LOGIC_4(riv2, 0x100031) + this->bitFlag = ~this->bitFlag; + this->bitFlag &= 0xaa; + if (this->bitFlag == 0) { + MACRO_NEIGHBOUR_FLAGS_GLOBAL_LOGIC_8(riv3, 0x100031) + this->bitFlag = ~this->bitFlag; + this->bitFlag &= 0x55; + } + ROTATE_BITFLAG_BY_ORIENTATION() + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::propagateCliffEdgeFlagFromNeighbor, + this)(this->DAT_SomeTile, tileXTmp, this->DAT_SomeY); + if ((char)this->Logic2Layer[this->DAT_SomeTile] == 0) { + this->GfxLayer[this->DAT_SomeTile] = GMFirstPictureIndex::instance[5] + 532 + + this->LuminesenceLayer[this->DAT_SomeTile] * 4 + + ((short)this->RandomLayer[this->DAT_SomeTile] & 3); + for (local_14_f = 0; local_14_f < 7; local_14_f++) { + if (DAT_TerrainDefinedData::instance.TerrainFlagGraphicLookup[local_14_f].unk1 + == this->bitFlag) { + MACRO_NEIGHBOUR_FLAGS_GLOBAL_LOGIC_4(riv4, 0x200000) + if (local_14_f < 4) { + if (this->bitFlag == 0) { + this->GfxLayer[this->DAT_SomeTile] + = GMFirstPictureIndex::instance[5] + 564 + + this->LuminesenceLayer[this->DAT_SomeTile] * 4 + + DAT_TerrainDefinedData::instance + .TerrainFlagGraphicLookup[local_14_f] + .unk2; + } else { + this->GfxLayer[this->DAT_SomeTile] + = GMFirstPictureIndex::instance[5] + 732 + + this->LuminesenceLayer[this->DAT_SomeTile] * 4 + + DAT_TerrainDefinedData::instance + .TerrainFlagGraphicLookup[local_14_f] + .unk2; + } + } else if (this->bitFlag != 0) { + } else if (local_14_f == 4) { + this->GfxLayer[this->DAT_SomeTile] = GMFirstPictureIndex::instance[5] + + 596 + this->LuminesenceLayer[this->DAT_SomeTile] * 4 + + ((short)this->RandomLayer[this->DAT_SomeTile] & 3); + } else if (local_14_f == 5) { + this->GfxLayer[this->DAT_SomeTile] = GMFirstPictureIndex::instance[5] + + 628 + this->LuminesenceLayer[this->DAT_SomeTile] * 4 + + ((short)this->RandomLayer[this->DAT_SomeTile] & 3); + } else if (local_14_f == 6) { + this->GfxLayer[this->DAT_SomeTile] = GMFirstPictureIndex::instance[5] + + 660 + this->LuminesenceLayer[this->DAT_SomeTile]; + } + break; + } + } + } + if (((char)this->Logic2Layer[this->DAT_SomeTile] & L2_BEACH) != 0) { + bankBase_ = (this->WallGFXLayer[this->DAT_SomeTile] & 8) * 8 + 909; + switch (this->mapOrientation) { + case OpenSHC::Map::Enums::MO_NORTH: + this->GfxLayer[this->DAT_SomeTile] = GMFirstPictureIndex::instance[5] + + bankBase_ + ((this->WallGFXLayer[this->DAT_SomeTile] & 0x3e0) >> 5) * 8 + - 1; + break; + case OpenSHC::Map::Enums::MO_EAST: + this->GfxLayer[this->DAT_SomeTile] = GMFirstPictureIndex::instance[5] + + bankBase_ + + (((this->WallGFXLayer[this->DAT_SomeTile] & 0x3e0) >> 5) - 2 & 7) * 8 - 1; + break; + case OpenSHC::Map::Enums::MO_SOUTH: + this->GfxLayer[this->DAT_SomeTile] = GMFirstPictureIndex::instance[5] + + bankBase_ + + (((this->WallGFXLayer[this->DAT_SomeTile] & 0x3e0) >> 5) - 4 & 7) * 8 - 1; + break; + default: + this->GfxLayer[this->DAT_SomeTile] = GMFirstPictureIndex::instance[5] + + bankBase_ + + (((this->WallGFXLayer[this->DAT_SomeTile] & 0x3e0) >> 5) - 6 & 7) * 8 - 1; + } + } + if (((char)this->Logic2Layer[this->DAT_SomeTile] + & (L2_OASIS_GRASS | L2_STONES_OR_DRIVEN_SANDUnk)) + != 0) { + this->GfxLayer[this->DAT_SomeTile] = GMFirstPictureIndex::instance[5] + 764; + } else if (((char)this->Logic2Layer[this->DAT_SomeTile] + & (L2_PLATEAU_HIGH | L2_THICK_SCRUB)) + != 0) { + MACRO_NEIGHBOUR_FLAGS_GLOBAL_LOGIC_8(riv5, 1) + riverMaskA = this->bitFlag; + MACRO_NEIGHBOUR_FLAGS_GLOBAL_TERRAIN_8(riv6, 0x50) + this->bitFlag = this->bitFlag | riverMaskA; + bankDirection = -1; + if (this->bitFlag != 0) { + if ((this->bitFlag & 0x80) != 0) { + if ((this->bitFlag & 0x20) != 0) { + bankDirection = 5; + } else if ((this->bitFlag & 2) != 0) { + bankDirection = 3; + } else if ((this->bitFlag & 0x41) == 0x41) { + bankDirection = 4; + } else { + bankDirection = 4; + } + } else if ((this->bitFlag & 0x20) != 0) { + if ((this->bitFlag & 8) != 0) { + bankDirection = 7; + } else if ((this->bitFlag & 0x80) != 0) { + bankDirection = 5; + } else if ((this->bitFlag & 0x50) == 0x50) { + bankDirection = 6; + } else { + bankDirection = 6; + } + } else if ((this->bitFlag & 8) != 0) { + if ((this->bitFlag & 2) != 0) { + bankDirection = 1; + } else if ((this->bitFlag & 0x20) != 0) { + bankDirection = 7; + } else if ((this->bitFlag & 0x14) == 0x14) { + bankDirection = 0; + } else { + bankDirection = 0; + } + } else if ((this->bitFlag & 2) != 0) { + if ((this->bitFlag & 0x80) != 0) { + bankDirection = 3; + } else if ((this->bitFlag & 8) != 0) { + bankDirection = 1; + } else if ((this->bitFlag & 5) == 5) { + bankDirection = 2; + } else { + bankDirection = 2; + } + } else if (this->bitFlag == 0x40) { + bankDirection = 5; + } else if (this->bitFlag == 1) { + bankDirection = 3; + } else if (this->bitFlag == 4) { + bankDirection = 1; + } else if (this->bitFlag == 0x10) { + bankDirection = 7; + } else if (this->bitFlag == 0x41) { + bankDirection = 4; + } else if (this->bitFlag == 0x50) { + bankDirection = 6; + } else if (this->bitFlag == 0x14) { + bankDirection = 0; + } else if (this->bitFlag == 5) { + bankDirection = 2; + } + if (bankDirection == -1) { + this->GfxLayer[this->DAT_SomeTile] = GMFirstPictureIndex::instance[5] + 532 + + this->LuminesenceLayer[this->DAT_SomeTile] * 4 + + ((short)this->RandomLayer[this->DAT_SomeTile] & 3); + this->Logic2Layer[this->DAT_SomeTile] + = (char)this->Logic2Layer[this->DAT_SomeTile] & ~L2_THICK_SCRUB; + } else { + bankDirection = (bankDirection - this->mapOrientation + 8) % 8; + this->GfxLayer[this->DAT_SomeTile] + = GMFirstPictureIndex::instance[5] + bankDirection * 16 + 780; + } + } else { + MACRO_NEIGHBOUR_FLAGS_GLOBAL_LOGIC_8(riv7, NEIGHBOUR_FLAGS_MASK_RIVER) + if (this->bitFlag != 0) { + tileHeightAt = this->HeightLayer[this->DAT_SomeTile]; + for (local_14_f = 0; local_14_f < 8; local_14_f++) { + if ((this->bitFlag & 1 << local_14_f) != 0) { + lowerTileIndex = this->DAT_SomeTile + + DIRECTION_OFFSET(this->DAT_SomeY, + DAT_TerrainDefinedData::instance.field_0x370c[local_14_f]); + if (this->HeightLayer[lowerTileIndex] <= tileHeightAt) { + this->bitFlag &= ~(1 << local_14_f); + } + } + } + if (this->bitFlag != 0) { + if ((this->bitFlag & 0x80) != 0) { + if ((this->bitFlag & 0x20) != 0) { + bankDirection = 5; + } else if ((this->bitFlag & 2) != 0) { + bankDirection = 3; + } else if ((this->bitFlag & 0x41) == 0x41) { + bankDirection = 4; + } else { + bankDirection = 4; + } + } else if ((this->bitFlag & 0x20) != 0) { + if ((this->bitFlag & 8) != 0) { + bankDirection = 7; + } else if ((this->bitFlag & 0x80) != 0) { + bankDirection = 5; + } else if ((this->bitFlag & 0x50) == 0x50) { + bankDirection = 6; + } else { + bankDirection = 6; + } + } else if ((this->bitFlag & 8) != 0) { + if ((this->bitFlag & 2) != 0) { + bankDirection = 1; + } else if ((this->bitFlag & 0x20) != 0) { + bankDirection = 7; + } else if ((this->bitFlag & 0x14) == 0x14) { + bankDirection = 0; + } else { + bankDirection = 0; + } + } else if ((this->bitFlag & 2) != 0) { + if ((this->bitFlag & 0x80) != 0) { + bankDirection = 3; + } else if ((this->bitFlag & 8) != 0) { + bankDirection = 1; + } else if ((this->bitFlag & 5) == 5) { + bankDirection = 2; + } else { + bankDirection = 2; + } + } else if (this->bitFlag == 0x41) { + bankDirection = 4; + } else if (this->bitFlag == 0x50) { + bankDirection = 6; + } else if (this->bitFlag == 0x14) { + bankDirection = 0; + } else if (this->bitFlag == 5) { + bankDirection = 2; + } else if ((this->bitFlag & 0x40) != 0) { + bankDirection = 5; + } else if ((this->bitFlag & 1) != 0) { + bankDirection = 3; + } else if ((this->bitFlag & 4) != 0) { + bankDirection = 1; + } else if ((this->bitFlag & 0x10) != 0) { + bankDirection = 7; + } + bankDirection = (bankDirection - this->mapOrientation + 8) % 8; + this->GfxLayer[this->DAT_SomeTile] + = GMFirstPictureIndex::instance[5] + bankDirection * 16 + 780; + } + } + if (bankDirection == -1) { + this->GfxLayer[this->DAT_SomeTile] = GMFirstPictureIndex::instance[5] + 532 + + this->LuminesenceLayer[this->DAT_SomeTile] * 4 + + ((short)this->RandomLayer[this->DAT_SomeTile] & 3); + this->Logic2Layer[this->DAT_SomeTile] + = (char)this->Logic2Layer[this->DAT_SomeTile] & ~L2_THICK_SCRUB; + } + } + } else if (((char)this->Logic2Layer[this->DAT_SomeTile] & 1) != 0) { + this->GfxLayer[this->DAT_SomeTile] = GMFirstPictureIndex::instance[5] + 1036; + } else if (((char)this->Logic2Layer[this->DAT_SomeTile] & 2) != 0) { + this->GfxLayer[this->DAT_SomeTile] = GMFirstPictureIndex::instance[5] + 1044; + } else if (((char)this->Logic2Layer[this->DAT_SomeTile] & 4) != 0) { + this->GfxLayer[this->DAT_SomeTile] = GMFirstPictureIndex::instance[5] + 1052; + } + } else if ((this->LogicLayer[this->DAT_SomeTile] & L_FORD) != 0) { + MACRO_NEIGHBOUR_FLAGS_GLOBAL_LOGIC_4(ford1, NEIGHBOUR_FLAGS_MASK_RIVER_OR_FORD) + this->bitFlag = ~this->bitFlag; + this->bitFlag &= 0xaa; + if (this->bitFlag == 0) { + MACRO_NEIGHBOUR_FLAGS_GLOBAL_LOGIC_8(ford2, NEIGHBOUR_FLAGS_MASK_RIVER_OR_FORD) + this->bitFlag = ~this->bitFlag; + this->bitFlag &= 0x55; + } + ROTATE_BITFLAG_BY_ORIENTATION() + this->GfxLayer[this->DAT_SomeTile] = GMFirstPictureIndex::instance[5] + 668 + + this->LuminesenceLayer[this->DAT_SomeTile] * 8 + + ((short)this->RandomLayer[this->DAT_SomeTile] & 7); + for (local_14_f = 0; local_14_f < 7; local_14_f++) { + if (DAT_TerrainDefinedData::instance.TerrainFlagGraphicLookup[local_14_f].unk1 + == this->bitFlag) { + if (local_14_f < 4) { + this->GfxLayer[this->DAT_SomeTile] = GMFirstPictureIndex::instance[5] + 564 + + this->LuminesenceLayer[this->DAT_SomeTile] * 4 + + DAT_TerrainDefinedData::instance.TerrainFlagGraphicLookup[local_14_f] + .unk2; + } else if (local_14_f == 4) { + this->GfxLayer[this->DAT_SomeTile] = GMFirstPictureIndex::instance[5] + 596 + + this->LuminesenceLayer[this->DAT_SomeTile] * 4 + + ((short)this->RandomLayer[this->DAT_SomeTile] & 3); + } else if (local_14_f == 5) { + this->GfxLayer[this->DAT_SomeTile] = GMFirstPictureIndex::instance[5] + 628 + + this->LuminesenceLayer[this->DAT_SomeTile] * 4 + + ((short)this->RandomLayer[this->DAT_SomeTile] & 3); + } else if (local_14_f == 6) { + this->GfxLayer[this->DAT_SomeTile] = GMFirstPictureIndex::instance[5] + 660 + + this->LuminesenceLayer[this->DAT_SomeTile]; + } + break; + } + } + } else if ((this->LogicLayer[this->DAT_SomeTile] & L_PLAIN2_AND_PITCH) != 0) { + this->MiscDisplayLayer[this->DAT_SomeTile] + = (short)this->MiscDisplayLayer[this->DAT_SomeTile] & ~0x2000; + ditchIDCur = MACRO_CALL_MEMBER( + OpenSHC::Map::TileMapState_Func::getPitchDitchIDForTile, this)(this->DAT_SomeTile); + if (ditchIDCur != 0) { + if (this->pitchDitches[ditchIDCur].state < 2 + && MACRO_CALL_MEMBER(OpenSHC::Game::GameStateStructures_Func::isSameTeam, + &DAT_GameState::instance)(this->pitchDitches[ditchIDCur].owner, + DAT_GameSynchronyState::instance.currentPlayerSlotID) + == FALSE + && DAT_GameCore::instance.gameMode_2 != OpenSHC::Game::GM_EDITOR) { + if ((this->LogicLayer[this->DAT_SomeTile] & L_DEFAULT_EARTH_OR_TEXTURE) != 0) { + this->GfxLayer[this->DAT_SomeTile] = 0; + continue; + } else { + goto plainTerrain; + } + } + rampRotation + = MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::computeClimbRampRotation, + this)(this->DAT_SomeTile, tileXTmp, this->DAT_SomeY); + this->PillarGFXLayer[this->DAT_SomeTile] + = GMFirstPictureIndex::instance[9] + this->wallCornerRotation - 1; + this->MiscDisplayLayer[this->DAT_SomeTile] + = (short)this->MiscDisplayLayer[this->DAT_SomeTile] | 0x800; + this->GfxLayer[this->DAT_SomeTile] + = (this->pitchDitches[ditchIDCur].rng & 7) + GMFirstPictureIndex::instance[140]; + this->MiscDisplayLayer[this->DAT_SomeTile] + = (short)this->MiscDisplayLayer[this->DAT_SomeTile] | 0x2000; + if (((short)this->MiscDisplayLayer[this->DAT_SomeTile] & 0x80) != 0) { + __asm jmp wallAlpha __asm jmp wallAlpha + } + continue; + } + goto plainTerrain; + } else if ((short)this->BuildingLayer[this->DAT_SomeTile] != 0) { + buildingIDTmp = (short)this->BuildingLayer[this->DAT_SomeTile]; + if (DAT_BuildingsState::instance.buildings[buildingIDTmp].buildingType + == OpenSHC::Map::Buildings::BT_STOCKPILE) { + this->wallCornerRotation = MACRO_CALL_MEMBER( + OpenSHC::Map::TileMapState_Func::computeWallCornerRenderRotation, this)( + tileXTmp); + this->WallGFXLayer[this->DAT_SomeTile] + = GMFirstPictureIndex::instance[10] + this->wallCornerRotation - 1; + } + rampRotation + = MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::computeClimbRampRotation, + this)(this->DAT_SomeTile, tileXTmp, this->DAT_SomeY); + switch (DAT_BuildingsState::instance.buildings[buildingIDTmp].buildingType) { + case OpenSHC::Map::Buildings::BT_FIREBALLISTA: + case OpenSHC::Map::Buildings::BT_CATAPULT: + case OpenSHC::Map::Buildings::BT_TREBUCHET: + case OpenSHC::Map::Buildings::BT_BATTERINGRAM: + case OpenSHC::Map::Buildings::BT_SIEGETOWER: + case OpenSHC::Map::Buildings::BT_SHIELD: + if (rampRotation == 0) { + heightPicture_ = (this->HeightLayer[this->DAT_SomeTile] / 8 - 1) * 64 + 1; + this->PillarGFXLayer[this->DAT_SomeTile] = (heightPicture_ - 1) / 2 + + GMFirstPictureIndex::instance[3] + + this->LuminesenceLayer[this->DAT_SomeTile] * 4 + + ((short)this->RandomLayer[this->DAT_SomeTile] & 3); + break; + } + default: + this->PillarGFXLayer[this->DAT_SomeTile] + = GMFirstPictureIndex::instance[9] + this->wallCornerRotation - 1; + this->MiscDisplayLayer[this->DAT_SomeTile] + = (short)this->MiscDisplayLayer[this->DAT_SomeTile] | 0x800; + } + if (DAT_BuildingsState::instance.buildings[buildingIDTmp].buildingType + == OpenSHC::Map::Buildings::BT_KILLINGPIT + && DAT_BuildingsState::instance.buildings[buildingIDTmp].state <= 0 + && MACRO_CALL_MEMBER(OpenSHC::Game::GameStateStructures_Func::isSameTeam, + &DAT_GameState::instance)( + DAT_BuildingsState::instance.buildings[buildingIDTmp].owner, + DAT_GameSynchronyState::instance.currentPlayerSlotID) + == FALSE + && DAT_GameCore::instance.gameMode_2 != OpenSHC::Game::GM_EDITOR) { + if ((this->LogicLayer[this->DAT_SomeTile] & L_DEFAULT_EARTH_OR_TEXTURE) != 0) { + this->GfxLayer[this->DAT_SomeTile] = 0; + continue; + } + goto plainTerrain; + } + if (DAT_BuildingsState::instance.buildings[buildingIDTmp].buildingType + == OpenSHC::Map::Buildings::BT_SIEGETOWER_PLACED) { + goto plainTerrain; + } + if (DAT_BuildingsState::instance.buildings[buildingIDTmp].currentTilePositionAdjusted + != this->DAT_SomeTile) { + continue; + } + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::updateBuildingGraphicsLayer, this)( + buildingIDTmp); + } else if ((this->LogicLayer[this->DAT_SomeTile] & L_KEEP_NON_MANOR_HOUSE) != 0) { + buildingIDTmp = (short)this->BuildingLayer[this->DAT_SomeTile]; + if (DAT_BuildingsState::instance.buildings[buildingIDTmp].currentTilePositionAdjusted + != this->DAT_SomeTile) { + continue; + } + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::updateBuildingGraphicsLayer, this)( + buildingIDTmp); + } else if ((this->LogicLayer[this->DAT_SomeTile] & L_ROCKY) != 0 + && (this->LogicLayer[this->DAT_SomeTile] & L_WALL_OR_GATEHOUSE) == 0) { + rampRotation + = MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::computeClimbRampRotation, + this)(this->DAT_SomeTile, tileXTmp, this->DAT_SomeY); + this->PillarGFXLayer[this->DAT_SomeTile] + = GMFirstPictureIndex::instance[9] + this->wallCornerRotation - 1; + this->MiscDisplayLayer[this->DAT_SomeTile] + = (short)this->MiscDisplayLayer[this->DAT_SomeTile] | 0x800; + rockIndexId = this->OrganismLayer[this->DAT_SomeTile]; + if (rockIndexId >= 2000) { + rockIndexId -= 2000; + if (DAT_LandscapeState::instance.rocks[rockIndexId].tile != this->DAT_SomeTile) { + continue; + } + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::applyRockGraphicsToFootprint, + this)(rockIndexId); + continue; + } + this->wallCornerRotation = (short)this->RandomLayer[this->DAT_SomeTile] & 7; + this->GfxLayer[this->DAT_SomeTile] = GMFirstPictureIndex::instance[56] + + this->LuminesenceLayer[this->DAT_SomeTile] * 8 + this->wallCornerRotation; + } else if ((this->LogicLayer[this->DAT_SomeTile] & L_BOULDERS) != 0) { + rampRotation + = MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::computeClimbRampRotation, + this)(this->DAT_SomeTile, tileXTmp, this->DAT_SomeY); + this->PillarGFXLayer[this->DAT_SomeTile] + = GMFirstPictureIndex::instance[9] + this->wallCornerRotation - 1; + this->MiscDisplayLayer[this->DAT_SomeTile] + = (short)this->MiscDisplayLayer[this->DAT_SomeTile] | 0x800; + this->wallCornerRotation = (short)this->RandomLayer[this->DAT_SomeTile] & 0xf; + if (this->LuminesenceLayer[this->DAT_SomeTile] >= 6) { + this->GfxLayer[this->DAT_SomeTile] + = GMFirstPictureIndex::instance[12] + 80 + this->wallCornerRotation; + } else { + this->GfxLayer[this->DAT_SomeTile] + = GMFirstPictureIndex::instance[12] + 64 + this->wallCornerRotation; + } + } else if ((this->LogicLayer[this->DAT_SomeTile] & L_PEBBLES) != 0) { + rampRotation + = MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::computeClimbRampRotation, + this)(this->DAT_SomeTile, tileXTmp, this->DAT_SomeY); + this->PillarGFXLayer[this->DAT_SomeTile] + = GMFirstPictureIndex::instance[9] + this->wallCornerRotation - 1; + this->MiscDisplayLayer[this->DAT_SomeTile] + = (short)this->MiscDisplayLayer[this->DAT_SomeTile] | 0x800; + this->wallCornerRotation = (short)this->RandomLayer[this->DAT_SomeTile] & 0xf; + if (this->LuminesenceLayer[this->DAT_SomeTile] >= 6) { + this->GfxLayer[this->DAT_SomeTile] + = GMFirstPictureIndex::instance[12] + 48 + this->wallCornerRotation; + } else { + this->GfxLayer[this->DAT_SomeTile] + = GMFirstPictureIndex::instance[12] + 32 + this->wallCornerRotation; + } + } else if ((this->LogicLayer[this->DAT_SomeTile] & L_IRON) != 0) { + rampRotation + = MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::computeClimbRampRotation, + this)(this->DAT_SomeTile, tileXTmp, this->DAT_SomeY); + this->PillarGFXLayer[this->DAT_SomeTile] + = GMFirstPictureIndex::instance[9] + this->wallCornerRotation - 1; + this->MiscDisplayLayer[this->DAT_SomeTile] + = (short)this->MiscDisplayLayer[this->DAT_SomeTile] | 0x800; + this->wallCornerRotation = (short)this->RandomLayer[this->DAT_SomeTile] & 0xf; + this->GfxLayer[this->DAT_SomeTile] + = GMFirstPictureIndex::instance[12] + 238 + this->wallCornerRotation; + } else if ((this->LogicLayer[this->DAT_SomeTile] & L_OIL) != 0) { + this->PillarGFXLayer[this->DAT_SomeTile] = GM_FIRST_PICTURE_INDEX(9); + this->wallCornerRotation = (short)this->RandomLayer[this->DAT_SomeTile] & 7; + this->GfxLayer[this->DAT_SomeTile] + = GMFirstPictureIndex::instance[55] + 1064 + this->wallCornerRotation * 16; + if (this->wallCornerRotation == 0) { + this->WallGFXLayer[this->DAT_SomeTile] + = (this->RandomLayer[this->DAT_SomeTile] + this->DAT_SomeX + this->DAT_SomeY) + & 0x1ff; + } else if (this->wallCornerRotation == 1) { + this->WallGFXLayer[this->DAT_SomeTile] + = (this->RandomLayer[this->DAT_SomeTile] + this->DAT_SomeX + this->DAT_SomeY) + & 0x1ff; + } else if (this->wallCornerRotation == 2) { + this->WallGFXLayer[this->DAT_SomeTile] + = (this->RandomLayer[this->DAT_SomeTile] + this->DAT_SomeX + this->DAT_SomeY) + & 0x7f; + } else if (this->wallCornerRotation == 3) { + this->WallGFXLayer[this->DAT_SomeTile] + = (this->RandomLayer[this->DAT_SomeTile] + this->DAT_SomeX + this->DAT_SomeY) + & 0x7f; + } else if (this->wallCornerRotation == 4) { + this->WallGFXLayer[this->DAT_SomeTile] + = (this->RandomLayer[this->DAT_SomeTile] + this->DAT_SomeX + this->DAT_SomeY) + & 0xff; + } else if (this->wallCornerRotation == 5) { + this->WallGFXLayer[this->DAT_SomeTile] + = (this->RandomLayer[this->DAT_SomeTile] + this->DAT_SomeX + this->DAT_SomeY) + & 0xff; + } else if (this->wallCornerRotation == 6) { + this->WallGFXLayer[this->DAT_SomeTile] + = (this->RandomLayer[this->DAT_SomeTile] + this->DAT_SomeX + this->DAT_SomeY) + & 0x7f; + } else if (this->wallCornerRotation == 7) { + this->WallGFXLayer[this->DAT_SomeTile] + = (this->RandomLayer[this->DAT_SomeTile] + this->DAT_SomeX + this->DAT_SomeY) + & 0x7f; + } + } else if ((this->LogicLayer[this->DAT_SomeTile] & L_MOAT) != 0) { + this->bitFlag = 0; + for (moatDirectionValue = 0; moatDirectionValue < 8; + moatDirectionValue = moatDirectionValue + 2) { + moatTile6 + = this->DAT_SomeTile + DIRECTION_OFFSET(this->DAT_SomeY, moatDirectionValue); + if ((this->LogicLayer[moatTile6] & L_MOAT) != 0) { + this->bitFlag |= 0x80 >> moatDirectionValue; + } else if ((short)this->BuildingLayer[moatTile6] != 0) { + buildingIDTmp = (short)this->BuildingLayer[moatTile6]; + if (DAT_BuildingsState::instance.buildings[buildingIDTmp].buildingType + == OpenSHC::Map::Buildings::BT_DRAWBRIDGE + && MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::returnOwnedMoatAtTile, + this)(moatTile6) + != 0) { + this->bitFlag |= 0x80 >> moatDirectionValue; + } + } + } + this->bitFlag = ~this->bitFlag; + this->bitFlag &= 0xaa; + if (this->bitFlag == 0) { + this->bitFlag = 0; + for (moatDirectionValue = 1; moatDirectionValue < 8; + moatDirectionValue = moatDirectionValue + 2) { + moatTile6 = this->DAT_SomeTile + + DIRECTION_OFFSET(this->DAT_SomeY, moatDirectionValue); + if ((this->LogicLayer[moatTile6] & L_MOAT) != 0) { + this->bitFlag |= 0x80 >> moatDirectionValue; + } else if ((short)this->BuildingLayer[moatTile6] != 0) { + buildingIDTmp = (short)this->BuildingLayer[moatTile6]; + if (DAT_BuildingsState::instance.buildings[buildingIDTmp].buildingType + == OpenSHC::Map::Buildings::BT_DRAWBRIDGE + && MACRO_CALL_MEMBER( + OpenSHC::Map::TileMapState_Func::returnOwnedMoatAtTile, this)( + moatTile6) + != 0) { + this->bitFlag |= 0x80 >> moatDirectionValue; + } + } + } + this->bitFlag = ~this->bitFlag; + this->bitFlag &= 0x55; + } + ROTATE_BITFLAG_BY_ORIENTATION() + this->GfxLayer[this->DAT_SomeTile] = GMFirstPictureIndex::instance[5] + 204 + + this->LuminesenceLayer[this->DAT_SomeTile] * 4 + + ((short)this->RandomLayer[this->DAT_SomeTile] & 3); + for (local_14_f = 0; local_14_f < 7; local_14_f++) { + if (DAT_TerrainDefinedData::instance.TerrainFlagGraphicLookup[local_14_f].unk1 + == this->bitFlag) { + if (local_14_f < 4) { + this->GfxLayer[this->DAT_SomeTile] = GMFirstPictureIndex::instance[5] + 236 + + this->LuminesenceLayer[this->DAT_SomeTile] * 4 + + DAT_TerrainDefinedData::instance.TerrainFlagGraphicLookup[local_14_f] + .unk2; + } else if (local_14_f == 4) { + this->GfxLayer[this->DAT_SomeTile] = GMFirstPictureIndex::instance[5] + 268 + + this->LuminesenceLayer[this->DAT_SomeTile] * 4 + + ((short)this->RandomLayer[this->DAT_SomeTile] & 3); + } else if (local_14_f == 5) { + this->GfxLayer[this->DAT_SomeTile] = GMFirstPictureIndex::instance[5] + 300 + + this->LuminesenceLayer[this->DAT_SomeTile] * 4 + + ((short)this->RandomLayer[this->DAT_SomeTile] & 3); + } else if (local_14_f == 6) { + this->GfxLayer[this->DAT_SomeTile] = GMFirstPictureIndex::instance[5] + 332 + + this->LuminesenceLayer[this->DAT_SomeTile]; + } + break; + } + } + if (((short)this->MiscDisplayLayer[this->DAT_SomeTile] & 0x80) != 0) { + goto wallAlpha; + } + } else if ((this->LogicLayer[this->DAT_SomeTile] + & (L_WALL_OR_GATEHOUSE | L_FARM_FIELD_WHEAT | L_FARM_FIELD_HOP + | L_FARM_FIELD_APPLE | L_FARM_FIELD_DAIRY)) + == 0 + || (this->LogicLayer[this->DAT_SomeTile] & L_FARM_FIELD_APPLE) != 0) { + alphaBaseCur = 0; + if ((this->WallOwnerLayer[this->DAT_SomeTile] & 8) != 0) { + alphaBaseCur = 45; + } + if (((short)this->MiscDisplayLayer[this->DAT_SomeTile] & 0x80) != 0) { + // the original jumps here from the pitch-ditch and moat arms: its gotos show as a + // doubled jmp + wallAlpha: + leftAlphaTileRef = this->DAT_SomeTile + + DIRECTION_OFFSET(this->DAT_SomeY, (13 - this->mapOrientation) % 8); + rightAlphaTile = this->DAT_SomeTile + + DIRECTION_OFFSET(this->DAT_SomeY, (9 - this->mapOrientation) % 8); + if (((short)this->MiscDisplayLayer[leftAlphaTileRef] & 0x80) != 0) { + if (((short)this->MiscDisplayLayer[rightAlphaTile] & 0x80) != 0) { + if (this->mapOrientation == OpenSHC::Map::Enums::MO_NORTH + || this->mapOrientation == OpenSHC::Map::Enums::MO_WEST) { + this->AlphaGFXLayer[this->DAT_SomeTile] + = 0x3e - this->DAT_SomeY % 8 + alphaBaseCur; + } else { + this->AlphaGFXLayer[this->DAT_SomeTile] + = this->DAT_SomeY % 8 + alphaBaseCur + 55; + } + } else if (this->mapOrientation == OpenSHC::Map::Enums::MO_NORTH + || this->mapOrientation == OpenSHC::Map::Enums::MO_SOUTH) { + this->AlphaGFXLayer[this->DAT_SomeTile] + = ((tileXTmp ^ this->DAT_SomeY) & 1) + alphaBaseCur + 53; + } else { + this->AlphaGFXLayer[this->DAT_SomeTile] + = ((tileXTmp ^ this->DAT_SomeY) & 1) + alphaBaseCur + 51; + } + } else if (((short)this->MiscDisplayLayer[rightAlphaTile] & 0x80) != 0) { + if (this->mapOrientation == OpenSHC::Map::Enums::MO_NORTH + || this->mapOrientation == OpenSHC::Map::Enums::MO_SOUTH) { + this->AlphaGFXLayer[this->DAT_SomeTile] + = ((tileXTmp ^ this->DAT_SomeY) & 1) + alphaBaseCur + 51; + } else { + this->AlphaGFXLayer[this->DAT_SomeTile] + = ((tileXTmp ^ this->DAT_SomeY) & 1) + alphaBaseCur + 53; + } + } else { + this->AlphaGFXLayer[this->DAT_SomeTile] += (tileXTmp ^ this->DAT_SomeY) & 1; + } + MACRO_NEIGHBOUR_FLAGS_GLOBAL_LOGIC_8(alpha, 0x200) + if (this->bitFlag != 0) { + switch (this->mapOrientation) { + case OpenSHC::Map::Enums::MO_NORTH: + if ((this->bitFlag & 0x82) == 0x82) { + this->AlphaGFXLayer[this->DAT_SomeTile] += 14; + } + break; + case OpenSHC::Map::Enums::MO_SOUTH: + if ((this->bitFlag & 0x28) == 0x28) { + this->AlphaGFXLayer[this->DAT_SomeTile] += 14; + } + break; + case OpenSHC::Map::Enums::MO_WEST: + if ((this->bitFlag & 0xa) == 0xa) { + this->AlphaGFXLayer[this->DAT_SomeTile] += 14; + } + break; + case OpenSHC::Map::Enums::MO_EAST: + if ((this->bitFlag & 0xa0) == 0xa0) { + this->AlphaGFXLayer[this->DAT_SomeTile] += 14; + } + } + } + if ((this->LogicLayer[this->DAT_SomeTile] & (L_MOAT | L_PLAIN2_AND_PITCH)) == 0) { + goto plainTerrain; + } + } else if ((this->LogicLayer[this->DAT_SomeTile] + & (L_WALL_OR_GATEHOUSE | L_BUILDING | L_KEEP_NON_MANOR_HOUSE)) + == 0 + && this->BuildingWasLayer[this->DAT_SomeTile] != 0 + && ((short)this->MiscDisplayLayer[this->DAT_SomeTile] & 0x10) != 0) { + } else { + // the plain terrain graphic; the original jumps here with goto from several arms above + plainTerrain: + heightStepA = (this->HeightLayer[this->DAT_SomeTile] / 8 - 1) % 16; + heightPicture_ = heightStepA * 64 + 1; + rampRotation + = MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::computeClimbRampRotation, + this)(this->DAT_SomeTile, tileXTmp, this->DAT_SomeY); + if (this->HeightLayer[this->DAT_SomeTile] >= 136 || rampRotation != 0) { + this->PillarGFXLayer[this->DAT_SomeTile] + = GMFirstPictureIndex::instance[9] + this->wallCornerRotation - 1; + this->MiscDisplayLayer[this->DAT_SomeTile] + = (short)this->MiscDisplayLayer[this->DAT_SomeTile] | 0x800; + cliffDropValue = 1; + } else { + if (heightPicture_ <= 0) { + heightPicture_ = 1; + } + this->PillarGFXLayer[this->DAT_SomeTile] = (heightPicture_ - 1) / 2 + + GMFirstPictureIndex::instance[3] + + this->LuminesenceLayer[this->DAT_SomeTile] * 4 + + ((short)this->RandomLayer[this->DAT_SomeTile] & 3); + if (MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::isCliffDropInDirection, + this)(this->DAT_SomeTile, tileXTmp, this->DAT_SomeY) + != 0) { + cliffDropValue = 1; + } else { + cliffDropValue = 0; + } + } + heightPicture_ = (this->HeightLayer[this->DAT_SomeTile] / 8 - 1) * 32 + 1; + if (((char)this->Logic2Layer[this->DAT_SomeTile] & L2_BEACH) != 0) { + this->GfxLayer[this->DAT_SomeTile] = 0; + } else if (this->HeightLayer[this->DAT_SomeTile] < 8) { + if (((char)this->Logic2Layer[this->DAT_SomeTile] & 2) != 0) { + this->GfxLayer[this->DAT_SomeTile] = GMFirstPictureIndex::instance[55] + 180 + + ((short)this->RandomLayer[this->DAT_SomeTile] & 7); + } else { + this->GfxLayer[this->DAT_SomeTile] = GMFirstPictureIndex::instance[2] + + this->LuminesenceLayer[this->DAT_SomeTile] * 4 + + ((short)this->RandomLayer[this->DAT_SomeTile] & 3); + } + } else if ((this->LogicLayer[this->DAT_SomeTile] & L_DEFAULT_EARTH_OR_TEXTURE) + != 0) { + this->GfxLayer[this->DAT_SomeTile] = 0; + } else if (cliffDropValue != 0 + && DAT_GameCore::instance.gameMode_2 == OpenSHC::Game::GM_EDITOR) { + this->LogicLayer[this->DAT_SomeTile] |= L_ROCKY; + this->GfxLayer[this->DAT_SomeTile] = GMFirstPictureIndex::instance[56] + + this->LuminesenceLayer[this->DAT_SomeTile] * 8 + + ((short)this->RandomLayer[this->DAT_SomeTile] & 7); + } else { + this->GfxLayer[this->DAT_SomeTile] = GMFirstPictureIndex::instance[2] + + heightPicture_ + (this->LuminesenceLayer[this->DAT_SomeTile] * 4 - 1) + + ((short)this->RandomLayer[this->DAT_SomeTile] & 3); + } + } + } + if ((this->LogicLayer[this->DAT_SomeTile] & (L_WALL_OR_GATEHOUSE | L_KEEP_NON_MANOR_HOUSE)) + != 0) { + heightPicture_ = (this->DefaultHeightLayer[this->DAT_SomeTile] / 8 - 1) * 64 + 1; + rampRotation + = MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::computeClimbRampRotation, + this)(this->DAT_SomeTile, tileXTmp, this->DAT_SomeY); + if (this->DefaultHeightLayer[this->DAT_SomeTile] >= 136) { + this->PillarGFXLayer[this->DAT_SomeTile] + = GMFirstPictureIndex::instance[9] + this->wallCornerRotation - 1; + this->MiscDisplayLayer[this->DAT_SomeTile] + = (short)this->MiscDisplayLayer[this->DAT_SomeTile] | 0x800; + } else if (rampRotation != 0) { + this->PillarGFXLayer[this->DAT_SomeTile] + = GMFirstPictureIndex::instance[9] + this->wallCornerRotation - 1; + this->MiscDisplayLayer[this->DAT_SomeTile] + = (short)this->MiscDisplayLayer[this->DAT_SomeTile] | 0x800; + } else if (((char)this->Logic2Layer[this->DAT_SomeTile] & 2) != 0) { + this->PillarGFXLayer[this->DAT_SomeTile] + = GMFirstPictureIndex::instance[9] + this->wallCornerRotation - 1; + this->MiscDisplayLayer[this->DAT_SomeTile] + = (short)this->MiscDisplayLayer[this->DAT_SomeTile] | 0x800; + } else { + this->PillarGFXLayer[this->DAT_SomeTile] = (heightPicture_ - 1) / 2 + + GMFirstPictureIndex::instance[3] + + this->LuminesenceLayer[this->DAT_SomeTile] * 4 + + ((short)this->RandomLayer[this->DAT_SomeTile] & 3); + } + } + } + } + } + } + } + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/updateLogicalTileMapRelatedSections.cpp b/src/OpenSHC/Map/TileMapState/updateLogicalTileMapRelatedSections.cpp new file mode 100644 index 0000000000..233c384283 --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/updateLogicalTileMapRelatedSections.cpp @@ -0,0 +1,350 @@ +#include "../TileMapState.func.hpp" + +#include "OpenSHC/Map/Buildings/BuildingsState.func.hpp" +#include "OpenSHC/Map/Buildings/BuildingType.hpp" +#include "OpenSHC/WindowsHelper/Enums/BOOLEnum.hpp" + +#include "OpenSHC/Globals/DAT_BuildingsState.hpp" +#include "OpenSHC/Globals/DAT_PathFindingState.hpp" +#include "OpenSHC/Globals/DAT_ViewportRenderState.hpp" + +namespace OpenSHC { +namespace Map { + + using OpenSHC::Map::LogicHelpers::L2_BEACH; + using OpenSHC::Map::LogicHelpers::L2_EARTH_AND_STONES; + 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::L2_STONES_OR_DRIVEN_SANDUnk; + using OpenSHC::Map::LogicHelpers::L2_THICK_SCRUB; + using OpenSHC::Map::LogicHelpers::L_BORDER; + using OpenSHC::Map::LogicHelpers::L_BORDER_EDGE; + using OpenSHC::Map::LogicHelpers::L_BUILDING; + using OpenSHC::Map::LogicHelpers::L_CRENEL; + using OpenSHC::Map::LogicHelpers::L_DEFAULT_EARTH_OR_TEXTURE; + using OpenSHC::Map::LogicHelpers::L_FARM_FIELD_HOP; + using OpenSHC::Map::LogicHelpers::L_FARM_FIELD_WHEAT; + using OpenSHC::Map::LogicHelpers::L_KEEP_NON_MANOR_HOUSE; + using OpenSHC::Map::LogicHelpers::L_MOAT; + using OpenSHC::Map::LogicHelpers::L_RIVER; + using OpenSHC::Map::LogicHelpers::L_SEA; + using OpenSHC::Map::LogicHelpers::L_STAIRS; + using OpenSHC::Map::LogicHelpers::L_STOCKPILEUnk; + using OpenSHC::Map::LogicHelpers::L_TREE_VARIATION; + using OpenSHC::Map::LogicHelpers::L_WALL_OR_GATEHOUSE; + + // FUNCTION: STRONGHOLDCRUSADER 0x004F70F0 + void TileMapState::updateLogicalTileMapRelatedSections() + { + if (this->forceUpdateLogicalAndMiscDisplayLayers == 0) { + return; + } + this->forceUpdateLogicalAndMiscDisplayLayers = 0; + int tile = DAT_ViewportRenderState::instance.translationMatrix[DAT_PathFindingState::instance.mappingYRelated] + .firstTileOfRow; + DAT_BuildingsState::instance.field_0x18e074 = 1; + for (int row = DAT_PathFindingState::instance.mappingYRelated; + row < 400 && row <= DAT_PathFindingState::instance.yLimit; row++) { + int const columns = this->yArray1[row]; + for (int column = 0; column < columns; column++, tile++) { + if ((this->LogicLayer[tile] & (L_BORDER | L_BORDER_EDGE)) != 0 || this->ChangedLayer[tile] == 0) { + continue; + } + + int const height = this->HeightLayer[tile]; + int const screenY = this->heightBasedScreenYOffset[height]; + this->ShowHiLayer[tile] = 0; + this->MiscDisplayLayer[tile] &= 0xffef; + // whether the tile draws its own texture or the default earth + if (height == 0 + && ((this->LogicLayer[tile] & L_RIVER) != 0 || (this->Logic2Layer[tile] & L2_BEACH) == 0) + && (this->LogicLayer[tile] & L_MOAT) == 0) { + this->LogicLayer[tile] &= ~L_DEFAULT_EARTH_OR_TEXTURE; + } else if (height <= 8) { + this->LogicLayer[tile] |= L_DEFAULT_EARTH_OR_TEXTURE; + } else if ((this->Logic2Layer[tile] & L2_PLATEAU_MEDIUM) != 0) { + this->LogicLayer[tile] |= L_DEFAULT_EARTH_OR_TEXTURE; + } else if ((this->Logic2Layer[tile] & L2_PLATEAU_HIGH) != 0) { + this->LogicLayer[tile] |= L_DEFAULT_EARTH_OR_TEXTURE; + } else if ((this->Logic2Layer[tile] & L2_EARTH_AND_STONES) != 0) { + this->LogicLayer[tile] |= L_DEFAULT_EARTH_OR_TEXTURE; + } else if ((this->Logic2Layer[tile] & L2_OASIS_GRASS) != 0) { + this->LogicLayer[tile] |= L_DEFAULT_EARTH_OR_TEXTURE; + } else if ((this->Logic2Layer[tile] & L2_SCRUB) != 0) { + this->LogicLayer[tile] |= L_DEFAULT_EARTH_OR_TEXTURE; + } else if ((this->Logic2Layer[tile] & L2_THICK_SCRUB) != 0) { + this->LogicLayer[tile] |= L_DEFAULT_EARTH_OR_TEXTURE; + } else if ((this->Logic2Layer[tile] & L2_STONES_OR_DRIVEN_SANDUnk) != 0) { + this->LogicLayer[tile] |= L_DEFAULT_EARTH_OR_TEXTURE; + } else { + this->LogicLayer[tile] &= ~L_DEFAULT_EARTH_OR_TEXTURE; + } + + // a tile level with all eight neighbours keeps its texture + for (int i = 0; i < 8; i++) { + int const neighbour = this->directionTranslationMatrix[row][i] + tile; + int const neighbourHeight = this->HeightLayer[neighbour]; + if ((height > 8 || neighbourHeight == 0) && height != neighbourHeight) { + if ((this->LogicLayer[neighbour] & (L_RIVER | L_MOAT)) == 0) { + this->LogicLayer[tile] &= ~L_DEFAULT_EARTH_OR_TEXTURE; + } + break; + } + } + int const building = (short)this->BuildingLayer[tile]; + if (building != 0 + && DAT_BuildingsState::instance.buildings[building].buildingType + == OpenSHC::Map::Buildings::BT_DRAWBRIDGE) { + this->MiscDisplayLayer[tile] |= 0x10; + } + + if (this->disableElevationRendering == 0) { + // look up the light's diagonal for anything standing high enough to shadow us + int walkTile = tile; + for (int step = 0; step < 8; step++) { + walkTile += this->directionTranslationMatrix[row + step * this->orientedRowStep] + [this->screenSouthEastDirection]; + if ((this->LogicLayer[walkTile] & (L_BORDER | L_BORDER_EDGE)) != 0) { + break; + } + if (screenY <= this->heightBasedScreenYOffset[this->HeightLayer[walkTile]] - step * 16 - 16) { + this->ShowHiLayer[tile] = 0xff; + break; + } + } + if (height == 0) { + continue; + } + if (this->ShowHiLayer[tile] == 0xff) { + continue; + } + } else { + this->ShowHiLayer[tile] = 8; + } + + BOOLEnum occluded; + if ((this->LogicLayer[tile] & (L_BUILDING | L_KEEP_NON_MANOR_HOUSE)) != 0) { + occluded = TRUE; + } else if ((this->LogicLayer[tile] & L_TREE_VARIATION) != 0) { + occluded = TRUE; + } else if ((this->LogicLayer[tile] & L_FARM_FIELD_WHEAT) != 0) { + occluded = TRUE; + } else if ((this->LogicLayer[tile] & L_FARM_FIELD_HOP) != 0) { + occluded = TRUE; + } else if ((this->LogicLayer[tile] & L_CRENEL) != 0) { + occluded = TRUE; + } else if ((this->LogicLayer[tile] & L_STAIRS) != 0) { + occluded = TRUE; + } else { + this->MiscDisplayLayer[tile] |= 0x10; + occluded = FALSE; + } + for (int i = 0; i < 8; i++) { + int const neighbour = this->directionTranslationMatrix[row][i] + tile; + if ((height > 8 && height != this->HeightLayer[neighbour]) + || (this->LogicLayer[neighbour] & L_TREE_VARIATION) != 0) { + this->MiscDisplayLayer[tile] &= 0xffef; + break; + } + if ((this->LogicLayer[neighbour] & L_FARM_FIELD_WHEAT) != 0) { + occluded = TRUE; + } + if ((this->LogicLayer[neighbour] & L_FARM_FIELD_HOP) != 0) { + occluded = TRUE; + } + } + this->MiscDisplayLayer[tile] &= 0xfffc; + + BOOLEnum stepRight = FALSE; + BOOLEnum stepLeft = FALSE; + int shadowTile = this->directionTranslationMatrix[row][this->screenSouthEastDirection] + tile; + int lowest = 250; + if (this->heightBasedScreenYOffset[this->HeightLayer[shadowTile]] < 250) { + lowest = this->heightBasedScreenYOffset[this->HeightLayer[shadowTile]]; + } + int const nearly = screenY + 2; + if (this->heightBasedScreenYOffset[this->HeightLayer[shadowTile]] > nearly) { + occluded = TRUE; + } + if ((this->LogicLayer[tile] & L_WALL_OR_GATEHOUSE) != 0) { + if (DAT_BuildingsState::instance.buildings[this->BuildingLayer[shadowTile]].flag2 != 0) { + occluded = TRUE; + } + if (DAT_BuildingsState::instance.buildings[this->BuildingLayer[shadowTile]].buildingType + == OpenSHC::Map::Buildings::BT_STOCKPILE) { + occluded = TRUE; + } + if (DAT_BuildingsState::instance + .buildings[this->BuildingLayer[this->directionTranslationMatrix[this->orientedRowStep + + row][this->screenSouthEastDirection] + + shadowTile]] + .flag2 + != 0) { + occluded = TRUE; + } + } else { + if ((this->LogicLayer[shadowTile] & (L_BUILDING | L_KEEP_NON_MANOR_HOUSE)) != 0 + && MACRO_CALL_MEMBER(OpenSHC::Map::Buildings::BuildingsState_Func::getBuildingFlag1, + DAT_BuildingsState::ptr)(this->BuildingLayer[shadowTile]) + != 0) { + occluded = TRUE; + } + int const southTile = this->directionTranslationMatrix[row][this->screenSouthDirection] + tile; + if (((this->LogicLayer[southTile] & (L_BUILDING | L_KEEP_NON_MANOR_HOUSE)) != 0 + && MACRO_CALL_MEMBER(OpenSHC::Map::Buildings::BuildingsState_Func::getBuildingFlag1, + DAT_BuildingsState::ptr)(this->BuildingLayer[southTile]) + != 0) + || ((this->LogicLayer[southTile] & L_WALL_OR_GATEHOUSE) != 0 + && (this->LogicLayer[southTile] & L_STOCKPILEUnk) == 0)) { + occluded = TRUE; + } + int const eastTile = this->directionTranslationMatrix[row][this->screenEastDirection] + tile; + if (((this->LogicLayer[eastTile] & (L_BUILDING | L_KEEP_NON_MANOR_HOUSE)) != 0 + && MACRO_CALL_MEMBER(OpenSHC::Map::Buildings::BuildingsState_Func::getBuildingFlag1, + DAT_BuildingsState::ptr)(this->BuildingLayer[eastTile]) + != 0) + || ((this->LogicLayer[eastTile] & L_WALL_OR_GATEHOUSE) != 0 + && (this->LogicLayer[eastTile] & L_STOCKPILEUnk) == 0)) { + occluded = TRUE; + } + int const southTile2 + = this->directionTranslationMatrix[this->orientedRowStep + row][this->screenSouthDirection] + + shadowTile; + if (((this->LogicLayer[southTile2] & (L_BUILDING | L_KEEP_NON_MANOR_HOUSE)) != 0 + && MACRO_CALL_MEMBER(OpenSHC::Map::Buildings::BuildingsState_Func::getBuildingFlag1, + DAT_BuildingsState::ptr)(this->BuildingLayer[southTile2]) + != 0) + || ((this->LogicLayer[southTile2] & L_WALL_OR_GATEHOUSE) != 0 + && (this->LogicLayer[southTile2] & L_STOCKPILEUnk) == 0)) { + occluded = TRUE; + } + int const eastTile2 + = this->directionTranslationMatrix[this->orientedRowStep + row][this->screenEastDirection] + + shadowTile; + if (((this->LogicLayer[eastTile2] & (L_BUILDING | L_KEEP_NON_MANOR_HOUSE)) != 0 + && MACRO_CALL_MEMBER(OpenSHC::Map::Buildings::BuildingsState_Func::getBuildingFlag1, + DAT_BuildingsState::ptr)(this->BuildingLayer[eastTile2]) + != 0) + || ((this->LogicLayer[eastTile2] & L_WALL_OR_GATEHOUSE) != 0 + && (this->LogicLayer[eastTile2] & L_STOCKPILEUnk) == 0)) { + occluded = TRUE; + } + shadowTile += this->directionTranslationMatrix[this->orientedRowStep + row] + [this->screenSouthEastDirection]; + if ((this->LogicLayer[shadowTile] & (L_BUILDING | L_KEEP_NON_MANOR_HOUSE)) != 0 + && MACRO_CALL_MEMBER(OpenSHC::Map::Buildings::BuildingsState_Func::getBuildingFlag1, + DAT_BuildingsState::ptr)(this->BuildingLayer[shadowTile]) + != 0) { + occluded = TRUE; + } + int const southTile3 + = this->directionTranslationMatrix[row + this->orientedRowStep * 2][this->screenSouthDirection] + + shadowTile; + if (((this->LogicLayer[southTile3] & (L_BUILDING | L_KEEP_NON_MANOR_HOUSE)) != 0 + && MACRO_CALL_MEMBER(OpenSHC::Map::Buildings::BuildingsState_Func::getBuildingFlag1, + DAT_BuildingsState::ptr)(this->BuildingLayer[southTile3]) + != 0) + || ((this->LogicLayer[southTile3] & L_WALL_OR_GATEHOUSE) != 0 + && (this->LogicLayer[southTile3] & L_STOCKPILEUnk) == 0)) { + occluded = TRUE; + } + int const eastTile3 + = this->directionTranslationMatrix[row + this->orientedRowStep * 2][this->screenEastDirection] + + shadowTile; + if (((this->LogicLayer[eastTile3] & (L_BUILDING | L_KEEP_NON_MANOR_HOUSE)) != 0 + && MACRO_CALL_MEMBER(OpenSHC::Map::Buildings::BuildingsState_Func::getBuildingFlag1, + DAT_BuildingsState::ptr)(this->BuildingLayer[eastTile3]) + != 0) + || ((this->LogicLayer[eastTile3] & L_WALL_OR_GATEHOUSE) != 0 + && (this->LogicLayer[eastTile3] & L_STOCKPILEUnk) == 0)) { + occluded = TRUE; + } + shadowTile += this->directionTranslationMatrix[row + this->orientedRowStep * 2] + [this->screenSouthEastDirection]; + if ((this->LogicLayer[shadowTile] & (L_BUILDING | L_KEEP_NON_MANOR_HOUSE)) != 0 + && MACRO_CALL_MEMBER(OpenSHC::Map::Buildings::BuildingsState_Func::getBuildingFlag1, + DAT_BuildingsState::ptr)(this->BuildingLayer[shadowTile]) + != 0) { + occluded = TRUE; + } + } + + int const southTile = this->directionTranslationMatrix[row][this->screenSouthDirection] + tile; + if (this->heightBasedScreenYOffset[this->HeightLayer[southTile]] < lowest) { + lowest = this->heightBasedScreenYOffset[this->HeightLayer[southTile]]; + } + if (this->heightBasedScreenYOffset[this->HeightLayer[southTile]] >= screenY) { + stepLeft = TRUE; + if (this->heightBasedScreenYOffset[this->HeightLayer[southTile]] > nearly) { + occluded = TRUE; + } + } + if ((this->LogicLayer[tile] & L_WALL_OR_GATEHOUSE) != 0) { + if (DAT_BuildingsState::instance.buildings[this->BuildingLayer[southTile]].flag2 != 0) { + occluded = TRUE; + } + if (DAT_BuildingsState::instance.buildings[this->BuildingLayer[southTile]].buildingType + == OpenSHC::Map::Buildings::BT_STOCKPILE) { + occluded = TRUE; + } + } else { + if ((this->LogicLayer[southTile] & (L_BUILDING | L_KEEP_NON_MANOR_HOUSE)) != 0 + && MACRO_CALL_MEMBER(OpenSHC::Map::Buildings::BuildingsState_Func::getBuildingFlag1, + DAT_BuildingsState::ptr)(this->BuildingLayer[southTile]) + != 0) { + occluded = TRUE; + } + } + + int const eastTile = this->directionTranslationMatrix[row][this->screenEastDirection] + tile; + if (this->heightBasedScreenYOffset[this->HeightLayer[eastTile]] < lowest) { + lowest = this->heightBasedScreenYOffset[this->HeightLayer[eastTile]]; + } + if (this->heightBasedScreenYOffset[this->HeightLayer[eastTile]] >= screenY) { + stepRight = TRUE; + if (this->heightBasedScreenYOffset[this->HeightLayer[eastTile]] > nearly) { + occluded = TRUE; + } + } + if ((this->LogicLayer[tile] & L_WALL_OR_GATEHOUSE) != 0) { + if (DAT_BuildingsState::instance.buildings[this->BuildingLayer[eastTile]].flag2 != 0) { + occluded = TRUE; + } + if (DAT_BuildingsState::instance.buildings[this->BuildingLayer[eastTile]].buildingType + == OpenSHC::Map::Buildings::BT_STOCKPILE) { + occluded = TRUE; + } + } else if ((this->LogicLayer[tile] & L_KEEP_NON_MANOR_HOUSE) == 0 + && (this->LogicLayer[eastTile] & (L_BUILDING | L_KEEP_NON_MANOR_HOUSE)) != 0 + && MACRO_CALL_MEMBER(OpenSHC::Map::Buildings::BuildingsState_Func::getBuildingFlag1, + DAT_BuildingsState::ptr)(this->BuildingLayer[eastTile]) + != 0) { + occluded = TRUE; + } + if (!occluded) { + this->MiscDisplayLayer[tile] |= 0x10; + } else { + this->MiscDisplayLayer[tile] &= 0xffef; + } + + if (screenY > lowest) { + if (this->disableElevationRendering == 0) { + this->ShowHiLayer[tile] = (char)screenY - (char)lowest; + } + if ((this->LogicLayer[tile] & L_SEA) == 0) { + if (stepLeft) { + this->MiscDisplayLayer[tile] |= 1; + } + if (stepRight) { + this->MiscDisplayLayer[tile] |= 2; + } + } + } + } + } + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/updateMacroLayerRelated.cpp b/src/OpenSHC/Map/TileMapState/updateMacroLayerRelated.cpp new file mode 100644 index 0000000000..e2c1aa7b39 --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/updateMacroLayerRelated.cpp @@ -0,0 +1,345 @@ +#include "../TileMapState.func.hpp" + +#include "OpenSHC/Map/Navigation/PathFindingState.func.hpp" +#include "OpenSHC/Rendering/ViewportRenderState.func.hpp" +#include "OpenSHC/Map/TileMapState/NeighbourFlagsGlobalAsm.hpp" + +#include "OpenSHC/Globals/DAT_PathFindingState.hpp" +#include "OpenSHC/Globals/DAT_TerrainDefinedData.hpp" +#include "OpenSHC/Globals/DAT_TileMapState.hpp" +#include "OpenSHC/Globals/DAT_ViewportRenderState.hpp" + +// masks for the handwritten neighbour-flag macro; MASM has no "|", so these must be literals +#define NEIGHBOUR_FLAGS_MASK_DEFAULT_EARTH 0x8000 // L_DEFAULT_EARTH_OR_TEXTURE +#define NEIGHBOUR_FLAGS_MASK_MARSH 0x20000000 // L_MARSH +#define NEIGHBOUR_FLAGS_MASK_SEA 0x1 // L_SEA +#define NEIGHBOUR_FLAGS_MASK_L2_SCRUB 0x1 // L2_SCRUB +#define NEIGHBOUR_FLAGS_MASK_L2_EARTH_AND_STONES 0x2 // L2_EARTH_AND_STONES +#define NEIGHBOUR_FLAGS_MASK_L2_PLATEAU_MEDIUM 0x4 // L2_PLATEAU_MEDIUM +#define NEIGHBOUR_FLAGS_MASK_L2_PLATEAU_HIGH 0x8 // L2_PLATEAU_HIGH +#define NEIGHBOUR_FLAGS_MASK_L2_OASIS_GRASS 0x10 // L2_OASIS_GRASS +#define NEIGHBOUR_FLAGS_MASK_L2_BEACH 0x20 // L2_BEACH +#define NEIGHBOUR_FLAGS_MASK_L2_STONES 0x40 // L2_STONES_OR_DRIVEN_SANDUnk +#define NEIGHBOUR_FLAGS_MASK_L2_THICK_SCRUB 0x80 // L2_THICK_SCRUB + +#pragma optimize("", off) + +namespace OpenSHC { +namespace Map { + + using OpenSHC::Map::LogicHelpers::L2_BEACH; + using OpenSHC::Map::LogicHelpers::L2_EARTH_AND_STONES; + 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::L2_STONES_OR_DRIVEN_SANDUnk; + using OpenSHC::Map::LogicHelpers::L2_THICK_SCRUB; + using OpenSHC::Map::LogicHelpers::L_BORDER; + using OpenSHC::Map::LogicHelpers::L_BORDER_EDGE; + using OpenSHC::Map::LogicHelpers::L_DEFAULT_EARTH_OR_TEXTURE; + using OpenSHC::Map::LogicHelpers::L_MARSH; + using OpenSHC::Map::LogicHelpers::L_OIL; + + // FUNCTION: STRONGHOLDCRUSADER 0x004FDB00 + void TileMapState::updateMacroLayerRelated(uint logic1, uint logic2, int clearMacroFirst) + { + // Five copies of the same block/sub-tile loop skeleton, and eleven expansions of the + // handwritten neighbour-flag macro (see NeighbourFlagsGlobalAsm.hpp). The original was built + // without optimisation, so the pragma above is needed to reproduce its frame-pointer, + // reload-per-access code shape. + // + // At /Od MSVC lays the locals out by a hash of their NAME (16 buckets, walked in order; + // inside one bucket the later declaration gets the higher slot), not by declaration order. + // The names and their order below were chosen so the slots land where the original has them + // (oldMacro at ebp-4 down to savedBitFlag at ebp-0x34); renaming any of them moves the slots. + int oldMacro; + int rotation; + int sectorColumn; + int tileRow; + int offsetTile; + int sectorRow; + int scanIndex; + int savedBitFlag; + int tileX; + int offsetX; + int randomVal; + int tableY; + int tileColumn; + + rotation = DAT_PathFindingState::instance.mappingYRelated % 10; + for (sectorRow = this->someIndex; sectorRow <= this->someLimit; sectorRow++) { + for (sectorColumn = this->someYLike; sectorColumn <= this->someYLikeLimit; sectorColumn++) { + if (DAT_TileMapState::instance.mapping40x40[sectorRow][sectorColumn] != 0) { + tileRow = rotation; + rotation = 0; + for (; tileRow < 10; tileRow++) { + for (tileColumn = 0, this->DAT_SomeY = sectorRow * 10 + tileRow, + this->DAT_SomeX = sectorColumn * 10; + tileColumn < 10; tileColumn++, this->DAT_SomeX++) { + if (MACRO_CALL_MEMBER(OpenSHC::Rendering::ViewportRenderState_Func::xyAreValid, + &DAT_ViewportRenderState::instance)(this->DAT_SomeX, this->DAT_SomeY) + == FALSE) { + continue; + } + this->DAT_SomeTile + = MACRO_CALL_MEMBER(OpenSHC::Rendering::ViewportRenderState_Func::translateXYToTile, + &DAT_ViewportRenderState::instance)(this->DAT_SomeX, this->DAT_SomeY); + if ((char)this->ChangedLayer[this->DAT_SomeTile] == 0) { + continue; + } + oldMacro = this->MacroLayer[this->DAT_SomeTile]; + if (clearMacroFirst != 0) { + this->MacroLayer[this->DAT_SomeTile] = 0; + } + // the original tests each layer separately and skips the tile only when neither matches + if ((this->LogicLayer[this->DAT_SomeTile] & logic1) != 0) { + } else if (((char)this->Logic2Layer[this->DAT_SomeTile] & logic2) != 0) { + } else { + continue; + } + if ((this->LogicLayer[this->DAT_SomeTile] & (L_BORDER | L_BORDER_EDGE)) != 0) { + this->bitFlag = 0; + } else if (logic1 == L_DEFAULT_EARTH_OR_TEXTURE) { + MACRO_NEIGHBOUR_FLAGS_GLOBAL_LOGIC_8(1, NEIGHBOUR_FLAGS_MASK_DEFAULT_EARTH) + } else if (logic1 == L_MARSH) { + if ((this->LogicLayer[this->DAT_SomeTile] & L_OIL) != 0) { + continue; + } + MACRO_NEIGHBOUR_FLAGS_GLOBAL_LOGIC_8(2, NEIGHBOUR_FLAGS_MASK_MARSH) + } else if (logic2 == L2_EARTH_AND_STONES) { + MACRO_NEIGHBOUR_FLAGS_GLOBAL_TERRAIN_8(3, NEIGHBOUR_FLAGS_MASK_L2_EARTH_AND_STONES) + } else if (logic2 == L2_OASIS_GRASS) { + MACRO_NEIGHBOUR_FLAGS_GLOBAL_TERRAIN_8(4, NEIGHBOUR_FLAGS_MASK_L2_OASIS_GRASS) + } else if (logic2 == L2_SCRUB) { + MACRO_NEIGHBOUR_FLAGS_GLOBAL_TERRAIN_8(5, NEIGHBOUR_FLAGS_MASK_L2_SCRUB) + } else if (logic2 == L2_THICK_SCRUB) { + MACRO_NEIGHBOUR_FLAGS_GLOBAL_TERRAIN_8(6, NEIGHBOUR_FLAGS_MASK_L2_THICK_SCRUB) + } else if (logic2 == L2_STONES_OR_DRIVEN_SANDUnk) { + MACRO_NEIGHBOUR_FLAGS_GLOBAL_TERRAIN_8(7, NEIGHBOUR_FLAGS_MASK_L2_STONES) + } else if (logic2 == L2_BEACH) { + MACRO_NEIGHBOUR_FLAGS_GLOBAL_TERRAIN_8(8, NEIGHBOUR_FLAGS_MASK_L2_BEACH) + savedBitFlag = this->bitFlag; + MACRO_NEIGHBOUR_FLAGS_GLOBAL_LOGIC_8(9, NEIGHBOUR_FLAGS_MASK_SEA) + this->bitFlag |= savedBitFlag; + } else if (logic2 == L2_PLATEAU_MEDIUM) { + MACRO_NEIGHBOUR_FLAGS_GLOBAL_TERRAIN_8(10, NEIGHBOUR_FLAGS_MASK_L2_PLATEAU_MEDIUM) + } else if (logic2 == L2_PLATEAU_HIGH) { + MACRO_NEIGHBOUR_FLAGS_GLOBAL_TERRAIN_8(11, NEIGHBOUR_FLAGS_MASK_L2_PLATEAU_HIGH) + } else { + this->bitFlag = 0; + } + if (this->bitFlag != 0xff) { + this->MacroLayer[this->DAT_SomeTile] = 1; + } else if (((short)this->RandomLayer[this->DAT_SomeTile] & 0xf) == 0) { + this->MacroLayer[this->DAT_SomeTile] = 2; + } else { + this->MacroLayer[this->DAT_SomeTile] = 2048; + } + if (oldMacro != this->MacroLayer[this->DAT_SomeTile]) { + MACRO_CALL_MEMBER( + OpenSHC::Map::Navigation::PathFindingState_Func::updateWalkAndPathLayer, + &DAT_PathFindingState::instance)(1, this->DAT_SomeX, this->DAT_SomeY); + } + } + } + } + } + } + rotation = DAT_PathFindingState::instance.mappingYRelated % 10; + for (sectorRow = this->someIndex; sectorRow <= this->someLimit; sectorRow++) { + for (sectorColumn = this->someYLike; sectorColumn <= this->someYLikeLimit; sectorColumn++) { + if (DAT_TileMapState::instance.mapping40x40[sectorRow][sectorColumn] != 0) { + tileRow = rotation; + rotation = 0; + for (; tileRow < 10; tileRow++) { + for (tileColumn = 0, this->DAT_SomeY = sectorRow * 10 + tileRow, + this->DAT_SomeX = sectorColumn * 10; + tileColumn < 10; tileColumn++, this->DAT_SomeX++) { + if (MACRO_CALL_MEMBER(OpenSHC::Rendering::ViewportRenderState_Func::xyAreValid, + &DAT_ViewportRenderState::instance)(this->DAT_SomeX, this->DAT_SomeY) + == FALSE) { + continue; + } + this->DAT_SomeTile + = MACRO_CALL_MEMBER(OpenSHC::Rendering::ViewportRenderState_Func::translateXYToTile, + &DAT_ViewportRenderState::instance)(this->DAT_SomeX, this->DAT_SomeY); + if (this->MacroLayer[this->DAT_SomeTile] != 2048) { + continue; + } + tileX = MACRO_CALL_MEMBER(OpenSHC::Rendering::ViewportRenderState_Func::computeTileXOffset, + &DAT_ViewportRenderState::instance)(this->DAT_SomeTile, this->DAT_SomeY); + randomVal = (short)this->RandomLayer[this->DAT_SomeTile] & 0xf; + for (scanIndex = 0; + scanIndex < countof(DAT_TerrainDefinedData::instance.MacroLayerScanOffsets); + scanIndex++) { + offsetX = DAT_TerrainDefinedData::instance.MacroLayerScanOffsets[scanIndex].x; + tableY = DAT_TerrainDefinedData::instance.MacroLayerScanOffsets[scanIndex].y; + offsetTile + = MACRO_CALL_MEMBER(OpenSHC::Rendering::ViewportRenderState_Func::translateXYToTile, + &DAT_ViewportRenderState::instance)(tileX + offsetX, this->DAT_SomeY + tableY); + if (this->MacroLayer[offsetTile] != 2048) { + break; + } + } + if (scanIndex < countof(DAT_TerrainDefinedData::instance.MacroLayerScanOffsets)) { + continue; + } + for (scanIndex = 0; + scanIndex < countof(DAT_TerrainDefinedData::instance.MacroLayerScanOffsets); + scanIndex++) { + offsetX = DAT_TerrainDefinedData::instance.MacroLayerScanOffsets[scanIndex].x; + tableY = DAT_TerrainDefinedData::instance.MacroLayerScanOffsets[scanIndex].y; + offsetTile + = MACRO_CALL_MEMBER(OpenSHC::Rendering::ViewportRenderState_Func::translateXYToTile, + &DAT_ViewportRenderState::instance)(tileX + offsetX, this->DAT_SomeY + tableY); + this->MacroLayer[offsetTile] = 16; + this->MacroLayer[offsetTile] += scanIndex << 6; + this->MacroLayer[offsetTile] += randomVal * 4096; + } + } + } + } + } + } + rotation = DAT_PathFindingState::instance.mappingYRelated % 10; + for (sectorRow = this->someIndex; sectorRow <= this->someLimit; sectorRow++) { + for (sectorColumn = this->someYLike; sectorColumn <= this->someYLikeLimit; sectorColumn++) { + if (DAT_TileMapState::instance.mapping40x40[sectorRow][sectorColumn] != 0) { + tileRow = rotation; + rotation = 0; + for (; tileRow < 10; tileRow++) { + for (tileColumn = 0, this->DAT_SomeY = sectorRow * 10 + tileRow, + this->DAT_SomeX = sectorColumn * 10; + tileColumn < 10; tileColumn++, this->DAT_SomeX++) { + if (MACRO_CALL_MEMBER(OpenSHC::Rendering::ViewportRenderState_Func::xyAreValid, + &DAT_ViewportRenderState::instance)(this->DAT_SomeX, this->DAT_SomeY) + == FALSE) { + continue; + } + this->DAT_SomeTile + = MACRO_CALL_MEMBER(OpenSHC::Rendering::ViewportRenderState_Func::translateXYToTile, + &DAT_ViewportRenderState::instance)(this->DAT_SomeX, this->DAT_SomeY); + if (this->MacroLayer[this->DAT_SomeTile] != 2048) { + continue; + } + tileX = MACRO_CALL_MEMBER(OpenSHC::Rendering::ViewportRenderState_Func::computeTileXOffset, + &DAT_ViewportRenderState::instance)(this->DAT_SomeTile, this->DAT_SomeY); + randomVal = (short)this->RandomLayer[this->DAT_SomeTile] & 0xf; + for (scanIndex = 0; scanIndex < countof(DAT_TerrainDefinedData::instance.field_0x198c); + scanIndex++) { + offsetX = DAT_TerrainDefinedData::instance.field_0x198c[scanIndex].x; + tableY = DAT_TerrainDefinedData::instance.field_0x198c[scanIndex].y; + offsetTile + = MACRO_CALL_MEMBER(OpenSHC::Rendering::ViewportRenderState_Func::translateXYToTile, + &DAT_ViewportRenderState::instance)(tileX + offsetX, this->DAT_SomeY + tableY); + if (this->MacroLayer[offsetTile] != 2048) { + break; + } + } + if (scanIndex < countof(DAT_TerrainDefinedData::instance.field_0x198c)) { + continue; + } + for (scanIndex = 0; scanIndex < countof(DAT_TerrainDefinedData::instance.field_0x198c); + scanIndex++) { + offsetX = DAT_TerrainDefinedData::instance.field_0x198c[scanIndex].x; + tableY = DAT_TerrainDefinedData::instance.field_0x198c[scanIndex].y; + offsetTile + = MACRO_CALL_MEMBER(OpenSHC::Rendering::ViewportRenderState_Func::translateXYToTile, + &DAT_ViewportRenderState::instance)(tileX + offsetX, this->DAT_SomeY + tableY); + this->MacroLayer[offsetTile] = 8; + this->MacroLayer[offsetTile] += scanIndex << 6; + this->MacroLayer[offsetTile] += randomVal * 4096; + } + } + } + } + } + } + rotation = DAT_PathFindingState::instance.mappingYRelated % 10; + for (sectorRow = this->someIndex; sectorRow <= this->someLimit; sectorRow++) { + for (sectorColumn = this->someYLike; sectorColumn <= this->someYLikeLimit; sectorColumn++) { + if (DAT_TileMapState::instance.mapping40x40[sectorRow][sectorColumn] != 0) { + tileRow = rotation; + rotation = 0; + for (; tileRow < 10; tileRow++) { + for (tileColumn = 0, this->DAT_SomeY = sectorRow * 10 + tileRow, + this->DAT_SomeX = sectorColumn * 10; + tileColumn < 10; tileColumn++, this->DAT_SomeX++) { + if (MACRO_CALL_MEMBER(OpenSHC::Rendering::ViewportRenderState_Func::xyAreValid, + &DAT_ViewportRenderState::instance)(this->DAT_SomeX, this->DAT_SomeY) + == FALSE) { + continue; + } + this->DAT_SomeTile + = MACRO_CALL_MEMBER(OpenSHC::Rendering::ViewportRenderState_Func::translateXYToTile, + &DAT_ViewportRenderState::instance)(this->DAT_SomeX, this->DAT_SomeY); + if (this->MacroLayer[this->DAT_SomeTile] != 2048) { + continue; + } + tileX = MACRO_CALL_MEMBER(OpenSHC::Rendering::ViewportRenderState_Func::computeTileXOffset, + &DAT_ViewportRenderState::instance)(this->DAT_SomeTile, this->DAT_SomeY); + randomVal = (short)this->RandomLayer[this->DAT_SomeTile] & 0xf; + for (scanIndex = 0; scanIndex < countof(DAT_TerrainDefinedData::instance.field_0x196c); + scanIndex++) { + offsetX = DAT_TerrainDefinedData::instance.field_0x196c[scanIndex].x; + tableY = DAT_TerrainDefinedData::instance.field_0x196c[scanIndex].y; + offsetTile + = MACRO_CALL_MEMBER(OpenSHC::Rendering::ViewportRenderState_Func::translateXYToTile, + &DAT_ViewportRenderState::instance)(tileX + offsetX, this->DAT_SomeY + tableY); + if (this->MacroLayer[offsetTile] != 2048) { + break; + } + } + if (scanIndex < countof(DAT_TerrainDefinedData::instance.field_0x196c)) { + continue; + } + for (scanIndex = 0; scanIndex < countof(DAT_TerrainDefinedData::instance.field_0x196c); + scanIndex++) { + offsetX = DAT_TerrainDefinedData::instance.field_0x196c[scanIndex].x; + tableY = DAT_TerrainDefinedData::instance.field_0x196c[scanIndex].y; + offsetTile + = MACRO_CALL_MEMBER(OpenSHC::Rendering::ViewportRenderState_Func::translateXYToTile, + &DAT_ViewportRenderState::instance)(tileX + offsetX, this->DAT_SomeY + tableY); + this->MacroLayer[offsetTile] = 4; + this->MacroLayer[offsetTile] += scanIndex << 6; + this->MacroLayer[offsetTile] += randomVal * 4096; + } + } + } + } + } + } + rotation = DAT_PathFindingState::instance.mappingYRelated % 10; + for (sectorRow = this->someIndex; sectorRow <= this->someLimit; sectorRow++) { + for (sectorColumn = this->someYLike; sectorColumn <= this->someYLikeLimit; sectorColumn++) { + if (DAT_TileMapState::instance.mapping40x40[sectorRow][sectorColumn] != 0) { + tileRow = rotation; + rotation = 0; + for (; tileRow < 10; tileRow++) { + for (tileColumn = 0, this->DAT_SomeY = sectorRow * 10 + tileRow, + this->DAT_SomeX = sectorColumn * 10; + tileColumn < 10; tileColumn++, this->DAT_SomeX++) { + if (MACRO_CALL_MEMBER(OpenSHC::Rendering::ViewportRenderState_Func::xyAreValid, + &DAT_ViewportRenderState::instance)(this->DAT_SomeX, this->DAT_SomeY) + == FALSE) { + continue; + } + this->DAT_SomeTile + = MACRO_CALL_MEMBER(OpenSHC::Rendering::ViewportRenderState_Func::translateXYToTile, + &DAT_ViewportRenderState::instance)(this->DAT_SomeX, this->DAT_SomeY); + if (this->MacroLayer[this->DAT_SomeTile] != 2048) { + continue; + } + this->MacroLayer[this->DAT_SomeTile] = 2; + } + } + } + } + } + } + +} +} + +#pragma optimize("", on) diff --git a/src/OpenSHC/Map/TileMapState/updateMacroLayerRelated2.cpp b/src/OpenSHC/Map/TileMapState/updateMacroLayerRelated2.cpp new file mode 100644 index 0000000000..087ba32ff9 --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/updateMacroLayerRelated2.cpp @@ -0,0 +1,143 @@ +#include "../TileMapState.func.hpp" + +#include "OpenSHC/Map/MapConstants.hpp" + +#include "OpenSHC/Globals/DAT_PathFindingState.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_SEA; + + // FUNCTION: STRONGHOLDCRUSADER 0x004FF080 + void TileMapState::updateMacroLayerRelated2() + { + int block; + int sub; + int startRow = DAT_PathFindingState::instance.mappingYRelated % 10; + for (block = this->someIndex; block <= this->someLimit; block++) { + for (sub = this->someYLike; sub <= this->someYLikeLimit; sub++) { + if (DAT_TileMapState::instance.mapping40x40[block][sub] == 0) { + continue; + } + int row = startRow; + startRow = 0; + for (; row < 10; row++) { + this->DAT_SomeY = block * 10 + row; + this->DAT_SomeX = sub * 10; + for (int column = 0; column < 10; column++, this->DAT_SomeX++) { + uint const x = this->DAT_SomeX; + uint const y = this->DAT_SomeY; + if (x > MC_MAP_XY_LIMIT_MIN_1 || y > MC_MAP_XY_LIMIT_MIN_1 + || DAT_ViewportRenderState::instance.DAT_BinaryTileMap400x400[y * MC_MAP_XY_LIMIT + x] + == 0) { + continue; + } + this->DAT_SomeTile + = DAT_ViewportRenderState::instance.translationMatrix[this->DAT_SomeY].addXgetTile + + this->DAT_SomeX; + if (this->ChangedLayer[this->DAT_SomeTile] != 0 + && (this->LogicLayer[this->DAT_SomeTile] & L_SEA) != 0) { + this->MacroLayer[this->DAT_SomeTile] = 2048; + } + } + } + } + } + + startRow = DAT_PathFindingState::instance.mappingYRelated % 10; + for (block = this->someIndex; block <= this->someLimit; block++) { + for (sub = this->someYLike; sub <= this->someYLikeLimit; sub++) { + if (DAT_TileMapState::instance.mapping40x40[block][sub] == 0) { + continue; + } + int row = startRow; + startRow = 0; + for (; row < 10; row++) { + this->DAT_SomeY = block * 10 + row; + this->DAT_SomeX = sub * 10; + for (int column = 0; column < 10; column++, this->DAT_SomeX++) { + uint const x = this->DAT_SomeX; + uint const y = this->DAT_SomeY; + if (x > MC_MAP_XY_LIMIT_MIN_1 || y > MC_MAP_XY_LIMIT_MIN_1 + || DAT_ViewportRenderState::instance.DAT_BinaryTileMap400x400[y * MC_MAP_XY_LIMIT + x] + == 0) { + continue; + } + this->DAT_SomeTile + = DAT_ViewportRenderState::instance.translationMatrix[this->DAT_SomeY].addXgetTile + + this->DAT_SomeX; + if (this->MacroLayer[this->DAT_SomeTile] != 2048 + || (this->LogicLayer[this->DAT_SomeTile] & L_SEA) == 0) { + continue; + } + int const corner = this->DAT_SomeTile + - DAT_ViewportRenderState::instance.translationMatrix[this->DAT_SomeY].addXgetTile; + int const random = this->RandomLayer[this->DAT_SomeTile] & 0xf; + // a wave sprite only fits where all sixteen of its tiles are still marked + int i; + for (i = 0; i < countof(DAT_TerrainDefinedData::instance.MacroLayerScanOffsets); i++) { + int const scanTile + = DAT_ViewportRenderState::instance + .translationMatrix[DAT_TerrainDefinedData::instance.MacroLayerScanOffsets[i].y + + this->DAT_SomeY] + .addXgetTile + + DAT_TerrainDefinedData::instance.MacroLayerScanOffsets[i].x + corner; + if (this->MacroLayer[scanTile] != 2048) { + break; + } + } + if (i < countof(DAT_TerrainDefinedData::instance.MacroLayerScanOffsets)) { + continue; + } + for (i = 0; i < countof(DAT_TerrainDefinedData::instance.MacroLayerScanOffsets); i++) { + int const scanTile + = DAT_ViewportRenderState::instance + .translationMatrix[DAT_TerrainDefinedData::instance.MacroLayerScanOffsets[i].y + + this->DAT_SomeY] + .addXgetTile + + DAT_TerrainDefinedData::instance.MacroLayerScanOffsets[i].x + corner; + this->MacroLayer[scanTile] = random * 4096; + this->MacroLayer[scanTile] += i * 64 + 16; + } + } + } + } + } + + startRow = DAT_PathFindingState::instance.mappingYRelated % 10; + for (block = this->someIndex; block <= this->someLimit; block++) { + for (sub = this->someYLike; sub <= this->someYLikeLimit; sub++) { + if (DAT_TileMapState::instance.mapping40x40[block][sub] == 0) { + continue; + } + int row = startRow; + startRow = 0; + for (; row < 10; row++) { + this->DAT_SomeY = block * 10 + row; + this->DAT_SomeX = sub * 10; + for (int column = 0; column < 10; column++, this->DAT_SomeX++) { + uint const x = this->DAT_SomeX; + uint const y = this->DAT_SomeY; + if (x > MC_MAP_XY_LIMIT_MIN_1 || y > MC_MAP_XY_LIMIT_MIN_1 + || DAT_ViewportRenderState::instance.DAT_BinaryTileMap400x400[y * MC_MAP_XY_LIMIT + x] + == 0) { + continue; + } + this->DAT_SomeTile + = DAT_ViewportRenderState::instance.translationMatrix[this->DAT_SomeY].addXgetTile + + this->DAT_SomeX; + if (this->MacroLayer[this->DAT_SomeTile] == 2048) { + this->MacroLayer[this->DAT_SomeTile] = 2; + } + } + } + } + } + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/upgradeTowerLogicLayer.cpp b/src/OpenSHC/Map/TileMapState/upgradeTowerLogicLayer.cpp new file mode 100644 index 0000000000..7dbb1e517a --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/upgradeTowerLogicLayer.cpp @@ -0,0 +1,53 @@ +#include "../TileMapState.func.hpp" + +#include "OpenSHC/Globals/DAT_BuildingsState.hpp" +#include "OpenSHC/Globals/DAT_TerrainDefinedData.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; + + // FUNCTION: STRONGHOLDCRUSADER 0x00507280 + void TileMapState::upgradeTowerLogicLayer(int buildingID) + { + int buildingSizeTileIndex = 0; + while (true) { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::getBuildingSizeIndexMappingData, this)( + buildingSizeTileIndex, DAT_BuildingsState::instance.buildings[buildingID].widthOrHeight); + int tile = DAT_ViewportRenderState::instance + .translationMatrix[DAT_BuildingsState::instance.buildings[buildingID].y + this->buildingY] + .addXgetTile + + DAT_BuildingsState::instance.buildings[buildingID].x + this->buildingX; + this->LogicLayer[tile] &= ~L_BUILDING; + if (DAT_TerrainDefinedData::instance + .TowerTileOffsetsBySize[DAT_BuildingsState::instance.buildings[buildingID].widthOrHeight][0] + == buildingSizeTileIndex) { + this->LogicLayer[tile] |= L_BUILDING; + } else if (DAT_TerrainDefinedData::instance + .TowerTileOffsetsBySize[DAT_BuildingsState::instance.buildings[buildingID].widthOrHeight][1] + == buildingSizeTileIndex) { + this->LogicLayer[tile] |= L_BUILDING; + } else if (DAT_TerrainDefinedData::instance + .TowerTileOffsetsBySize[DAT_BuildingsState::instance.buildings[buildingID].widthOrHeight][2] + == buildingSizeTileIndex) { + this->LogicLayer[tile] |= L_BUILDING; + } else if (DAT_TerrainDefinedData::instance + .TowerTileOffsetsBySize[DAT_BuildingsState::instance.buildings[buildingID].widthOrHeight][3] + == buildingSizeTileIndex) { + this->LogicLayer[tile] |= L_BUILDING; + } else { + this->LogicLayer[tile] |= L_KEEP_NON_MANOR_HOUSE; + } + buildingSizeTileIndex++; + if (buildingSizeTileIndex >= this->constructionTileCount) { + break; + } + } + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::updatePathLinkagesForBuilding, this)(buildingID); + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/useEraserBrush.cpp b/src/OpenSHC/Map/TileMapState/useEraserBrush.cpp new file mode 100644 index 0000000000..590840c8ad --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/useEraserBrush.cpp @@ -0,0 +1,118 @@ +#include "../TileMapState.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/Game/Player/Enums/BuildingEntryInfoEnum.hpp" +#include "OpenSHC/Map/Buildings/BuildingType.hpp" +#include "OpenSHC/Map/Units/UnitLogicState.hpp" +#include "OpenSHC/WindowsHelper/Enums/BOOLEnum.hpp" + +#include "OpenSHC/Globals/DAT_BuildingsState.hpp" +#include "OpenSHC/Globals/DAT_EntityState.hpp" +#include "OpenSHC/Globals/DAT_GameState.hpp" +#include "OpenSHC/Globals/DAT_LandscapeState.hpp" +#include "OpenSHC/Globals/DAT_PathFindingState.hpp" +#include "OpenSHC/Globals/DAT_TerrainDefinedData.hpp" +#include "OpenSHC/Globals/DAT_TileMapState.hpp" +#include "OpenSHC/Globals/DAT_UnitsState.hpp" +#include "OpenSHC/Globals/DAT_ViewportRenderState.hpp" + +namespace OpenSHC { +namespace Map { + + // FUNCTION: STRONGHOLDCRUSADER 0x005017C0 + void TileMapState::useEraserBrush(uint x, uint y, int brush) + { + int const brushSize = DAT_TerrainDefinedData::instance.BrushSizeArray[brush]; + int const baseTile = DAT_ViewportRenderState::instance.translationMatrix[y].addXgetTile + x; + uint const baseY = y; + int tile = DAT_ViewportRenderState::instance.translationMatrix[y].addXgetTile + x; + MACRO_CALL_MEMBER(OpenSHC::Map::Entities::EntityState_Func::destroyEntitiesOnTile, DAT_EntityState::ptr)(tile); + + for (int index = 0; index < brushSize; index++) { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::getTileForBrush, this)( + 1, index, &tile, (int*)&y, baseTile, baseY); + int const organism = this->OrganismLayer[tile]; + if (organism != 0) { + // tree ids come first, rock ids are biased by 2000 + if (organism >= 2000) { + MACRO_CALL_MEMBER(OpenSHC::Map::LandscapeState_Func::removeRock, DAT_LandscapeState::ptr)( + organism - 2000); + MACRO_CALL_MEMBER(OpenSHC::Map::Navigation::PathFindingState_Func::updateWalkAndPathLayer, + DAT_PathFindingState::ptr)(8, x, y); + } else { + MACRO_CALL_MEMBER(OpenSHC::Map::LandscapeState_Func::removeTree, DAT_LandscapeState::ptr)(organism); + MACRO_CALL_MEMBER(OpenSHC::Map::Navigation::PathFindingState_Func::updateWalkAndPathLayer, + DAT_PathFindingState::ptr)(3, x, y); + } + DAT_PathFindingState::instance.toggleUpdateSeparateAreaTileMap = 1; + } + } + + y = baseY; + for (int index = 0; index < brushSize; index++) { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::getTileForBrush, this)( + 1, index, &tile, (int*)&y, baseTile, baseY); + uint const brushY = *(uint volatile*)&y; + this->placementOnWall = 0; + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::validateBuildingPlacementAtTile, this)(0, x, brushY); + if (this->buildingPlacementFail != FALSE) { + continue; + } + if (this->placementOnWall == 0) { + int const targetID = this->cursorTargetID; + if (targetID == -1) { + continue; + } + // razing a keep takes the stockpile with it, and the other way round + int const owner = DAT_BuildingsState::instance.buildings[targetID].owner; + if (targetID + == DAT_GameState::instance.playerDataArray[owner] + .buildingEntryInfos[OpenSHC::Game::Player::Enums::BEIE_KEEP] + .id + && DAT_GameState::instance.playerDataArray[owner] + .buildingEntryInfos[OpenSHC::Game::Player::Enums::BEIE_STOCKPILE] + .id + != 0) { + this->showNoRubbleWhenDestroyingBuilding = 1; + MACRO_CALL_MEMBER(OpenSHC::Map::Buildings::BuildingsState_Func::destroyBuildingAndLinkedDuplicates, + DAT_BuildingsState::ptr)(DAT_GameState::instance.playerDataArray[owner] + .buildingEntryInfos[OpenSHC::Game::Player::Enums::BEIE_STOCKPILE] + .id); + } + if (DAT_BuildingsState::instance.buildings[targetID].buildingType + == OpenSHC::Map::Buildings::BT_STOCKPILE + && DAT_GameState::instance.playerDataArray[owner] + .buildingEntryInfos[OpenSHC::Game::Player::Enums::BEIE_KEEP] + .id + != 0) { + this->showNoRubbleWhenDestroyingBuilding = 1; + MACRO_CALL_MEMBER(OpenSHC::Map::Buildings::BuildingsState_Func::destroyBuildingAndLinkedDuplicates, + DAT_BuildingsState::ptr)(DAT_GameState::instance.playerDataArray[owner] + .buildingEntryInfos[OpenSHC::Game::Player::Enums::BEIE_KEEP] + .id); + } + this->showNoRubbleWhenDestroyingBuilding = 1; + MACRO_CALL_MEMBER(OpenSHC::Map::Buildings::BuildingsState_Func::destroyBuildingAndLinkedDuplicates, + DAT_BuildingsState::ptr)(this->cursorTargetID); + } else { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::spawnEraserTileEffect, this)( + 0, DAT_ViewportRenderState::instance.translationMatrix[brushY].addXgetTile + x); + } + } + + y = baseY; + for (int index = 0; index < brushSize; index++) { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::getTileForBrush, this)( + 1, index, &tile, (int*)&y, baseTile, baseY); + for (int unit = DAT_TileMapState::instance.UnitLayer[tile]; unit != 0; + unit = DAT_UnitsState::instance.units[unit].nextUnitOnTheSameTile) { + DAT_UnitsState::instance.units[unit].logicalState = OpenSHC::Map::Units::ULS_REMOVE; + } + } + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/useLevelBrush.cpp b/src/OpenSHC/Map/TileMapState/useLevelBrush.cpp new file mode 100644 index 0000000000..366e8e0b06 --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/useLevelBrush.cpp @@ -0,0 +1,116 @@ +#include "../TileMapState.func.hpp" + +#include "OpenSHC/Map/Navigation/PathFindingState.func.hpp" +#include "OpenSHC/Map/TileMapState/NeighbourFlagsGlobalAsm.hpp" + +#include "OpenSHC/Globals/DAT_PathFindingState.hpp" +#include "OpenSHC/Globals/DAT_TerrainDefinedData.hpp" +#include "OpenSHC/Globals/DAT_TileMapState.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_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_RIVER; + using OpenSHC::Map::LogicHelpers::L_ROCKY; + using OpenSHC::Map::LogicHelpers::L_SEA; + using OpenSHC::Map::LogicHelpers::L_WALL_OR_GATEHOUSE; + + // FUNCTION: STRONGHOLDCRUSADER 0x00502110 + void TileMapState::useLevelBrush(int tile, uint y, uint brush, int unused) + { + int const brushSize = DAT_TerrainDefinedData::instance.BrushSizeArray[brush]; + int highest = 0; + // the original reuses the brush parameter to hold the lowest height seen + brush = 200; + int const baseTile = tile; + uint const baseY = y; + int index; + for (index = 0; index < brushSize; index++) { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::getTileForBrush, this)( + 1, index, &tile, (int*)&y, baseTile, baseY); + if ((this->LogicLayer[tile] & (L_BORDER | L_BORDER_EDGE)) == 0) { + int const height = this->HeightLayer[tile]; + if (height > highest) { + highest = height; + } + if (height < (int)brush) { + brush = height; + } + } + } + + int const average = (int)(brush + highest) / 2; + tile = baseTile; + y = baseY; + for (index = 0; index < brushSize; index++) { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::getTileForBrush, this)( + 1, index, &tile, (int*)&y, baseTile, baseY); + int const brushTile = *(int volatile*)&tile; + uint const logic = this->LogicLayer[brushTile]; + if ((logic & (L_BORDER | L_BORDER_EDGE)) != 0) { + continue; + } + if ((logic & (L_SEA | L_MARSH)) != 0 && this->editorAffectType == 0) { + continue; + } + if ((logic & (L_SEA | L_MARSH)) == 0 && this->editorAffectType == 1) { + continue; + } + if ((logic & (L_WALL_OR_GATEHOUSE | L_BUILDING | L_MOAT_DUG_OR_PLANNED | L_KEEP_NON_MANOR_HOUSE | L_MOAT)) + != 0) { + continue; + } + if (this->BuildingLayer[brushTile] != 0 || (logic & L_SEA) != 0) { + continue; + } + + if ((logic & L_RIVER) != 0) { + this->HeightLayer[brushTile] += 8; + } + this->LogicLayer[brushTile] &= ~L_ROCKY; + // the tile moves a tenth of the way towards the brush average, plus one + byte levelled; + bool levelChanged = true; + if (this->HeightLayer[brushTile] < average) { + byte const height = *(byte volatile*)&this->HeightLayer[brushTile]; + levelled = (char)((average - height) / 10) + height + 1; + } else if (this->HeightLayer[brushTile] > average) { + byte const height = *(byte volatile*)&this->HeightLayer[brushTile]; + levelled = height - (char)((height - average) / 10) - 1; + } else { + levelChanged = false; + } + if (levelChanged) { + this->HeightLayer[brushTile] = levelled; + this->Logic2Layer[brushTile] = 0; + } + if (this->HeightLayer[brushTile] < 8) { + this->HeightLayer[brushTile] = 8; + } + if (this->HeightLayer[brushTile] > 156) { + this->HeightLayer[brushTile] = 156; + } + this->DefaultHeightLayer[brushTile] = *(byte volatile*)&this->HeightLayer[brushTile]; + if ((this->LogicLayer[brushTile] & L_RIVER) != 0) { + this->HeightLayer[brushTile] -= 8; + } + uint const brushY = *(uint volatile*)&y; + this->DAT_SomeTile = brushTile; + this->DAT_SomeY = brushY; + + // handwritten assembly in the original: mark this tile and its eight neighbours changed + MACRO_MARK_CHANGED_NEIGHBOURS_GLOBAL(); + MACRO_CALL_MEMBER(OpenSHC::Map::Navigation::PathFindingState_Func::updatePathLinkagesInAllEightDirections, + DAT_PathFindingState::ptr)(brushY, brushTile); + } + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/useTerrainHeightBrush.cpp b/src/OpenSHC/Map/TileMapState/useTerrainHeightBrush.cpp new file mode 100644 index 0000000000..385d4807af --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/useTerrainHeightBrush.cpp @@ -0,0 +1,124 @@ +#include "../TileMapState.func.hpp" + +#include "OpenSHC/Map/LandscapeState.func.hpp" +#include "OpenSHC/Map/Navigation/PathFindingState.func.hpp" +#include "OpenSHC/Map/TileMapState/NeighbourFlagsGlobalAsm.hpp" + +#include "OpenSHC/Globals/DAT_LandscapeState.hpp" +#include "OpenSHC/Globals/DAT_PathFindingState.hpp" +#include "OpenSHC/Globals/DAT_TerrainDefinedData.hpp" +#include "OpenSHC/Globals/DAT_TileMapState.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_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 0x00502680 + void TileMapState::useTerrainHeightBrush(int tile, uint y, uint brush, short mapper) + { + int const baseTile = tile; + uint const baseY = y; + int const brushSize = DAT_TerrainDefinedData::instance.BrushSizeArray[brush]; + + int highest = 0; + int lowest = 200; + for (int index = 0; index < brushSize; index++) { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::getTileForBrush, this)( + 1, index, &tile, (int*)&y, baseTile, baseY); + if ((this->LogicLayer[tile] & (L_BORDER | L_BORDER_EDGE)) != 0) { + continue; + } + if (this->HeightLayer[tile] > highest) { + highest = this->HeightLayer[tile]; + } + if (this->HeightLayer[tile] < lowest) { + lowest = this->HeightLayer[tile]; + } + } + + int const average = (lowest + highest) / 2; + tile = baseTile; + y = baseY; + for (int index = 0; index < brushSize; index++) { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::getTileForBrush, this)( + 1, index, &tile, (int*)&y, baseTile, baseY); + int const brushTile = *(int volatile*)&tile; + if ((this->LogicLayer[brushTile] & (L_BORDER | L_BORDER_EDGE)) != 0) { + continue; + } + if ((this->LogicLayer[brushTile] + & (L_SEA | L_WALL_OR_GATEHOUSE | L_BUILDING | L_KEEP_NON_MANOR_HOUSE | L_MARSH | L_MOAT)) + != 0) { + continue; + } + if (this->BuildingLayer[brushTile] != 0) { + continue; + } + + this->HeightLayer[brushTile] += 1; + byte& height = this->HeightLayer[brushTile]; + if (mapper == 0) { + // mapper min + height = 8; + this->Logic2Layer[brushTile] &= 0xf3; + } else if (mapper == 1) { + // mapper max, bug:fixme: is 156 really the maximum height? + height = 156; + this->LogicLayer[brushTile] |= 0x8000; + } else if (mapper == 5) { + height = 100; + } else if (mapper == 2) { + // scale towards the brush average, in eighths of the ratio to it + int ratio = 100; + if (average != 0) { + ratio = height * 100 / average; + } + ratio = (ratio / 8 + 100) * height / 100; + if (ratio > 156) { + ratio = 156; + } + height = (byte)ratio; + } + + if ((this->LogicLayer[brushTile] & L_ROCKY) != 0) { + int const organism = this->OrganismLayer[brushTile]; + if (organism >= 2000) { + MACRO_CALL_MEMBER(OpenSHC::Map::LandscapeState_Func::removeRock, DAT_LandscapeState::ptr)( + organism - 2000); + } + } + this->LogicLayer[brushTile] &= ~L_ROCKY; + if (this->HeightLayer[brushTile] < 8) { + this->HeightLayer[brushTile] = 8; + } + if (this->HeightLayer[brushTile] > 156) { + this->HeightLayer[brushTile] = 156; + } + this->DefaultHeightLayer[brushTile] = height; + if ((this->LogicLayer[brushTile] & L_RIVER) != 0) { + this->HeightLayer[brushTile] -= 8; + } + uint const brushY = *(uint volatile*)&y; + this->DAT_SomeTile = brushTile; + this->DAT_SomeY = brushY; + + // handwritten assembly in the original: mark this tile and its eight neighbours changed + MACRO_MARK_CHANGED_NEIGHBOURS_GLOBAL(); + MACRO_CALL_MEMBER(OpenSHC::Map::Navigation::PathFindingState_Func::updatePathLinkagesInAllEightDirections, + DAT_PathFindingState::ptr)(brushY, brushTile); + } + this->forceUpdateLogicalAndMiscDisplayLayers = 1; + } + +} +} diff --git a/src/OpenSHC/Map/TileMapState/validateWallBuildPath.cpp b/src/OpenSHC/Map/TileMapState/validateWallBuildPath.cpp new file mode 100644 index 0000000000..f48b7ee8b3 --- /dev/null +++ b/src/OpenSHC/Map/TileMapState/validateWallBuildPath.cpp @@ -0,0 +1,282 @@ +#include "../TileMapState.func.hpp" + +#include "OpenSHC/Map/Navigation/PathFindingState.func.hpp" +#include "OpenSHC/Game/GameMode.hpp" +#include "OpenSHC/Game/GameMode2.hpp" +#include "OpenSHC/Map/Buildings/BuildingFailReasonEnum.hpp" +#include "OpenSHC/Map/Units/UnitType.hpp" +#include "OpenSHC/WindowsHelper/Enums/BOOLEnum.hpp" + +#include "OpenSHC/Globals/DAT_GameCore.hpp" +#include "OpenSHC/Globals/DAT_GameState.hpp" +#include "OpenSHC/Globals/DAT_GameSynchronyState.hpp" +#include "OpenSHC/Globals/DAT_LandscapeState.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_KEEP_NON_MANOR_HOUSE; + 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_TREE; + using OpenSHC::Map::LogicHelpers::L_TREE_VARIATION; + using OpenSHC::Map::LogicHelpers::L_WALL_OR_GATEHOUSE; + + using OpenSHC::Game::GameMode; + using OpenSHC::Game::GameMode2; + using OpenSHC::Map::Buildings::BuildingFailReasonEnum; + using OpenSHC::Map::Units::UnitType; + + // FUNCTION: STRONGHOLDCRUSADER 0x005029D0 + void TileMapState::validateWallBuildPath(int playerID, uint x1, uint y1, uint x2, uint y2, short command) + { + int overshoot = 2; + int budget = 0; + int enemyRange; + int searchRange; + if (DAT_GameSynchronyState::instance.currentGameMode != OpenSHC::Game::GM_SOLITARY) { + enemyRange = 15; + searchRange = 7; + } else { + enemyRange = 30; + searchRange = 30; + } + this->buildingPlacementFail = FALSE; + this->illegalBuild = TRUE; + this->field_0x554920 = 0; + if (x1 > 399 || y1 > 399) { + this->buildingPlacementFail = TRUE; + return; + } + if (DAT_ViewportRenderState::instance.DAT_BinaryTileMap400x400[y1 * 400 + x1] == 0) { + this->buildingPlacementFail = TRUE; + return; + } + bool const endOutsideMap = x2 > 399 || y2 > 399; + if (endOutsideMap) { + this->buildingPlacementFail = TRUE; + return; + } + if (DAT_ViewportRenderState::instance.DAT_BinaryTileMap400x400[y2 * 400 + x2] == 0) { + this->buildingPlacementFail = TRUE; + return; + } + + this->DAT_SomeTile = DAT_ViewportRenderState::instance.translationMatrix[y1].addXgetTile + x1; + this->DAT_SomeY = y1; + if (command == OpenSHC::Commands::M_MAPPER_STAIR) { + // a stair needs an enclosed wall to lean against, and enough height on it + if (MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::isTileEnclosedByWallsOrGates, this)( + this->DAT_SomeTile, y1) + == FALSE) { + return; + } + int const height = MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::getMaxWallHeightInBrushArea, this)( + this->DAT_SomeTile, y1); + this->maxWallHeightInPath = height; + if (height <= 16) { + return; + } + budget = height; + overshoot = 2; + } else if (command == OpenSHC::Commands::M_MAPPER_CRENAL + && MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::isTileEnclosedByWalls, this)(this->DAT_SomeTile, y1) + == FALSE) { + this->buildingPlacementFailReason = OpenSHC::Map::Buildings::BRE_UNKNOWN_19; + return; + } + + this->field_0x554920 = 1; + int upY = y1 - y2; + int leftX = x1 - x2; + int downY = y2 - y1; + int rightX = x2 - x1; + uint x = x1; + uint y = y1; + do { + int const tile = DAT_ViewportRenderState::instance.translationMatrix[y].addXgetTile + x; + uint const logic = this->LogicLayer[tile]; + if ((logic & (L_PLAIN1_AND_FARM | L_BORDER)) != 0) { + return; + } + if ((logic & (L_SEA | L_BUILDING | L_KEEP_NON_MANOR_HOUSE | L_MARSH | L_MOAT)) != 0) { + return; + } + if (this->BuildingLayer[tile] != 0) { + return; + } + if ((logic & (L_TREE | L_TREE_VARIATION)) != 0) { + int const organism = this->OrganismLayer[tile]; + if (organism != 0 && organism < 2000) { + // scrub can be built through, a real tree cannot + switch (DAT_LandscapeState::instance.trees[organism].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: + break; + default: + return; + } + } + } + if ((logic & (L_RIVER | L_FORD)) != 0) { + return; + } + if ((logic & L_ROCKY) != 0) { + return; + } + if (this->UnitLayer[tile] != 0 && (logic & L_WALL_OR_GATEHOUSE) == 0 + && DAT_UnitsState::instance.units[(short)this->UnitLayer[tile]].unitType + != OpenSHC::Map::Units::UT_CHICKEN) { + return; + } + if (DAT_GameCore::instance.gameMode_2 != OpenSHC::Game::GM_EDITOR + && DAT_GameCore::instance.gameMode_2 != OpenSHC::Game::GM_SIEGE_THAT + && MACRO_CALL_MEMBER(OpenSHC::Map::Navigation::PathFindingState_Func::isEnemyTooCloseUnk, + DAT_PathFindingState::ptr)(playerID, x, y, enemyRange) + != FALSE) { + this->buildingPlacementFailReason = OpenSHC::Map::Buildings::BRE_UNKNOWN_17; + return; + } + if (DAT_GameSynchronyState::instance.currentGameMode != OpenSHC::Game::GM_SOLITARY) { + if (MACRO_CALL_MEMBER(OpenSHC::Map::Navigation::PathFindingState_Func::isOpponentBuildingInRange, + DAT_PathFindingState::ptr)(playerID, x, y, searchRange, -1, -1, -1) + != FALSE) { + if (this->buildingPlacementFailReason == OpenSHC::Map::Buildings::BRE_UNKNOWN_18) { + return; + } + this->buildingPlacementFailReason = OpenSHC::Map::Buildings::BRE_UNKNOWN_19; + return; + } + if (DAT_GameSynchronyState::instance.currentGameMode != OpenSHC::Game::GM_SOLITARY) { + int buildRange + = MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::getCastleBuildRangeForMapSize, this)(); + if (MACRO_CALL_MEMBER(OpenSHC::Map::Navigation::PathFindingState_Func::isTileInRangeOfKeepRange, + DAT_PathFindingState::ptr)(playerID, x, y, buildRange) + != FALSE) { + if (this->buildingPlacementFailReason == OpenSHC::Map::Buildings::BRE_UNKNOWN_18) { + return; + } + this->buildingPlacementFailReason = OpenSHC::Map::Buildings::BRE_UNKNOWN_19; + return; + } + } + } + if (DAT_GameCore::instance.gameMode_2 == OpenSHC::Game::GM_EDITOR + && MACRO_CALL_MEMBER(OpenSHC::Map::Navigation::PathFindingState_Func::isOpponentBuildingInRange, + DAT_PathFindingState::ptr)(playerID, x, y, 7, -1, -1, -1) + != FALSE) { + this->buildingPlacementFailReason = OpenSHC::Map::Buildings::BRE_UNKNOWN_17; + return; + } + if (MACRO_CALL_MEMBER(OpenSHC::Map::Navigation::PathFindingState_Func::findSomeSuitableLocationUnk, + DAT_PathFindingState::ptr)(playerID, x, y, 2) + != FALSE) { + this->buildingPlacementFailReason = OpenSHC::Map::Buildings::BRE_UNKNOWN_17; + return; + } + if (MACRO_CALL_MEMBER(OpenSHC::Map::Navigation::PathFindingState_Func::isSignPostWithinDistance, + DAT_PathFindingState::ptr)(x, y, DAT_GameState::instance.mapAndTime.unk_signpostDistance + 5) + != FALSE) { + this->buildingPlacementFailReason = OpenSHC::Map::Buildings::BRE_UNKNOWN_21; + return; + } + if (command == OpenSHC::Commands::M_MAPPER_CRENAL) { + if (MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::isTileEnclosedByWalls, this)(tile, y) == FALSE) { + this->buildingPlacementFailReason = OpenSHC::Map::Buildings::BRE_UNKNOWN_19; + return; + } + } else if (command == OpenSHC::Commands::M_MAPPER_STAIR + && (budget <= 16 || (this->LogicLayer[tile] & L_WALL_OR_GATEHOUSE) != 0)) { + break; + } + + int stepX = leftX; + if ((int)x < (int)x2) { + stepX = rightX; + } + int stepY = upY; + if ((int)y < (int)y2) { + stepY = downY; + } + if (command == OpenSHC::Commands::M_MAPPER_STAIR) { + if (stepX > stepY) { + if ((int)x >= (int)x2) { + leftX -= 1; + x -= 1; + rightX += 1; + } else { + leftX += 1; + x += 1; + rightX -= 1; + } + } else if (stepY > 0) { + if ((int)y >= (int)y2) { + y -= 1; + upY -= 1; + downY += 1; + } else { + y += 1; + upY += 1; + downY -= 1; + } + } else { + overshoot -= 1; + } + } else { + if (stepX != 0) { + if ((int)x >= (int)x2) { + leftX -= 1; + x -= 1; + rightX += 1; + } else { + leftX += 1; + x += 1; + rightX -= 1; + } + } + if (stepY != 0) { + if ((int)y >= (int)y2) { + y -= 1; + upY -= 1; + downY += 1; + } else { + y += 1; + upY += 1; + downY -= 1; + } + } + if (x == x2 && y == y2) { + overshoot -= 1; + } + } + if (budget > 16) { + budget -= 16; + } + } while (x != x2 || y != y2 || overshoot != 0); + this->illegalBuild = FALSE; + } + +} +} diff --git a/src/OpenSHC/UI/HoveredState/calculateHoveredTile.cpp b/src/OpenSHC/UI/HoveredState/calculateHoveredTile.cpp index d2f2251753..414ee570b7 100644 --- a/src/OpenSHC/UI/HoveredState/calculateHoveredTile.cpp +++ b/src/OpenSHC/UI/HoveredState/calculateHoveredTile.cpp @@ -136,7 +136,7 @@ namespace UI { // any other mapper is a building placement preview, in the element's rotation DAT_TileMapState::instance.skipPlacementFailChecks = 1; DAT_TileMapState::instance.uiBuildingRotation = this->elements[i].rotationOrExtraInfo; - // the type goes as a word (movzx), though the header's parameter is a MappersEnum + // the type goes as a word (movzx) MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::setConstructionGFXLayerBasedOnPlacementChecks, DAT_TileMapState::ptr)(this->elements[i].x, this->elements[i].y, (Commands::MappersEnum)(ushort)this->elements[i].type, this->elements[i].size); diff --git a/src/precomp/FunctionResolver.h b/src/precomp/FunctionResolver.h index 2e2fbc4609..85996482de 100644 --- a/src/precomp/FunctionResolver.h +++ b/src/precomp/FunctionResolver.h @@ -207,7 +207,7 @@ struct FunctionResolver { template struct FunctionPtrUnifier; template struct FunctionPtrUnifier { - inline static FuncPtrType const get() { return funcAddress; } + __forceinline static FuncPtrType const get() { return funcAddress; } }; // At the moment, there is not much sense in allowing the wrapper function for the exe. @@ -216,10 +216,10 @@ struct FunctionResolver { #ifdef OPEN_SHC_DLL template struct FunctionPtrUnifier { - inline static FuncPtrType const get() { return GameFunction::get(); } + __forceinline static FuncPtrType const get() { return GameFunction::get(); } }; template struct FunctionPtrUnifier { - inline static FuncPtrType const get() + __forceinline static FuncPtrType const get() { return reinterpret_cast(&Wrapper::Function::call); } @@ -372,7 +372,7 @@ struct FunctionResolver { #undef MACRO_STREAM_PRINT_PARAMETER template struct GameFunction { - inline static FuncPtrType get() { return reinterpret_cast(&trampoline); } + __forceinline static FuncPtrType get() { return reinterpret_cast(&trampoline); } }; #define MACRO_GAME_THIS_FUNCTION(N) \ @@ -395,7 +395,7 @@ struct FunctionResolver { }; \ \ public: \ - inline static FuncPtrType get() { return reinterpret_cast(&CallHelper::call); } \ + __forceinline static FuncPtrType get() { return reinterpret_cast(&CallHelper::call); } \ }; MACRO_INDEX_ITERATE_DEPTH_1(MACRO_NUMBER_OF_FUNCTIONS_TO_GENERATE, MACRO_GAME_THIS_FUNCTION, M_SPACE) #undef MACRO_GAME_THIS_FUNCTION diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index cf0d6a79de..80c3aed4eb 100644 --- a/status/addresses-SHC-3BB0A8C1.txt +++ b/status/addresses-SHC-3BB0A8C1.txt @@ -38741,7 +38741,7 @@ SHC_3BB0A8C1_0x004F6A80 | 100.0% | Reimplemented SHC_3BB0A8C1_0x004F6AE0 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x004F6B20 | 0.0% | Pending +SHC_3BB0A8C1_0x004F6B20 | 100.0% | Reimplemented SHC_3BB0A8C1_0x004F6C20 | 0.0% | Pending @@ -38761,7 +38761,7 @@ SHC_3BB0A8C1_0x004F70D0 | 100.0% | Reimplemented SHC_3BB0A8C1_0x004F70E0 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x004F70F0 | 0.0% | Pending +SHC_3BB0A8C1_0x004F70F0 | 100.0% | Reimplemented SHC_3BB0A8C1_0x004F7140 | 0.0% | Pending @@ -38769,7 +38769,7 @@ SHC_3BB0A8C1_0x004F7170 | 0.0% | Pending SHC_3BB0A8C1_0x004F79D0 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x004F7A80 | 0.0% | Pending +SHC_3BB0A8C1_0x004F7A80 | 100.0% | Reimplemented SHC_3BB0A8C1_0x004F7E50 | 0.0% | Pending @@ -39377,9 +39377,9 @@ SHC_3BB0A8C1_0x004F8AC0 | 100.0% | Reimplemented SHC_3BB0A8C1_0x004F8B50 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x004F8BD0 | 0.0% | Pending +SHC_3BB0A8C1_0x004F8BD0 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x004F9000 | 0.0% | Pending +SHC_3BB0A8C1_0x004F9000 | 100.0% | Reimplemented SHC_3BB0A8C1_0x004F9220 | 100.0% | Reimplemented @@ -39387,7 +39387,7 @@ SHC_3BB0A8C1_0x004F93E0 | 100.0% | Reimplemented SHC_3BB0A8C1_0x004F94A0 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x004F9590 | 0.0% | Pending +SHC_3BB0A8C1_0x004F9590 | 100.0% | Reimplemented SHC_3BB0A8C1_0x004F9630 | 0.0% | Pending @@ -39413,7 +39413,7 @@ SHC_3BB0A8C1_0x004F9980 | 100.0% | Reimplemented SHC_3BB0A8C1_0x004F99D0 | 0.0% | Pending -SHC_3BB0A8C1_0x004F9A60 | 0.0% | Pending +SHC_3BB0A8C1_0x004F9A60 | 100.0% | Reimplemented SHC_3BB0A8C1_0x004F9D64 | 0.0% | Pending @@ -39653,9 +39653,9 @@ SHC_3BB0A8C1_0x004F9DF0 | 100.0% | Reimplemented SHC_3BB0A8C1_0x004F9F00 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x004FA000 | 0.0% | Pending +SHC_3BB0A8C1_0x004FA000 | 68.7% | final else 8; y reload -SHC_3BB0A8C1_0x004FA2D0 | 0.0% | Pending +SHC_3BB0A8C1_0x004FA2D0 | 100.0% | Reimplemented SHC_3BB0A8C1_0x004FA460 | 100.0% | Reimplemented @@ -40343,7 +40343,7 @@ SHC_3BB0A8C1_0x004FA759 | 0.0% | Pending SHC_3BB0A8C1_0x004FA75A | 0.0% | Pending -SHC_3BB0A8C1_0x004FA760 | 0.0% | Pending +SHC_3BB0A8C1_0x004FA760 | 98.6% | offset array partition SHC_3BB0A8C1_0x004FA8D0 | 0.0% | Pending @@ -40365,11 +40365,11 @@ SHC_3BB0A8C1_0x004FAD70 | 100.0% | Reimplemented SHC_3BB0A8C1_0x004FAE50 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x004FAEE0 | 0.0% | Pending +SHC_3BB0A8C1_0x004FAEE0 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x004FAF70 | 0.0% | Pending +SHC_3BB0A8C1_0x004FAF70 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x004FB0C0 | 0.0% | Pending +SHC_3BB0A8C1_0x004FB0C0 | 100.0% | Reimplemented SHC_3BB0A8C1_0x004FB2E0 | 0.0% | Pending @@ -40567,21 +40567,21 @@ SHC_3BB0A8C1_0x004FB358 | 0.0% | Pending SHC_3BB0A8C1_0x004FB360 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x004FB3F0 | 0.0% | Pending +SHC_3BB0A8C1_0x004FB3F0 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x004FB4D0 | 0.0% | Pending +SHC_3BB0A8C1_0x004FB4D0 | 93.5% | Style pass, asm differs SHC_3BB0A8C1_0x004FB770 | 100.0% | Reimplemented SHC_3BB0A8C1_0x004FB7D0 | 0.0% | Pending -SHC_3BB0A8C1_0x004FB8E0 | 0.0% | Pending +SHC_3BB0A8C1_0x004FB8E0 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x004FB970 | 0.0% | Pending +SHC_3BB0A8C1_0x004FB970 | 100.0% | Reimplemented SHC_3BB0A8C1_0x004FB990 | 0.0% | Pending -SHC_3BB0A8C1_0x004FB9E0 | 0.0% | Pending +SHC_3BB0A8C1_0x004FB9E0 | 100.0% | Reimplemented SHC_3BB0A8C1_0x004FBC30 | 100.0% | Reimplemented @@ -41079,15 +41079,15 @@ SHC_3BB0A8C1_0x004FBD70 | 0.0% | Pending SHC_3BB0A8C1_0x004FBD80 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x004FBE70 | 0.0% | Pending +SHC_3BB0A8C1_0x004FBE70 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x004FC0B0 | 0.0% | Pending +SHC_3BB0A8C1_0x004FC0B0 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x004FC280 | 0.0% | Pending +SHC_3BB0A8C1_0x004FC280 | 100.0% | Reimplemented SHC_3BB0A8C1_0x004FC340 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x004FC490 | 0.0% | Pending +SHC_3BB0A8C1_0x004FC490 | 100.0% | Reimplemented SHC_3BB0A8C1_0x004FC650 | 100.0% | Reimplemented @@ -41095,17 +41095,17 @@ SHC_3BB0A8C1_0x004FC7C0 | 100.0% | Reimplemented SHC_3BB0A8C1_0x004FC810 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x004FC9E0 | 0.0% | Pending +SHC_3BB0A8C1_0x004FC9E0 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x004FDB00 | 0.0% | Pending +SHC_3BB0A8C1_0x004FDB00 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x004FF080 | 0.0% | Pending +SHC_3BB0A8C1_0x004FF080 | 100.0% | Reimplemented SHC_3BB0A8C1_0x004FF420 | 0.0% | Pending -SHC_3BB0A8C1_0x004FF870 | 0.0% | Pending +SHC_3BB0A8C1_0x004FF870 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x004FF990 | 0.0% | Pending +SHC_3BB0A8C1_0x004FF990 | 100.0% | Reimplemented SHC_3BB0A8C1_0x004FFAAC | 0.0% | Pending @@ -41261,7 +41261,7 @@ SHC_3BB0A8C1_0x004FFB03 | 0.0% | Pending SHC_3BB0A8C1_0x004FFB10 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x004FFBA0 | 0.0% | Pending +SHC_3BB0A8C1_0x004FFBA0 | 100.0% | Reimplemented SHC_3BB0A8C1_0x004FFECC | 0.0% | Pending @@ -41349,7 +41349,7 @@ SHC_3BB0A8C1_0x004FFF01 | 0.0% | Pending SHC_3BB0A8C1_0x004FFF02 | 0.0% | Pending -SHC_3BB0A8C1_0x004FFF10 | 0.0% | Pending +SHC_3BB0A8C1_0x004FFF10 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00500060 | 0.0% | Pending @@ -41437,7 +41437,7 @@ SHC_3BB0A8C1_0x00500310 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00500370 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x005003D0 | 0.0% | Pending +SHC_3BB0A8C1_0x005003D0 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00500500 | 100.0% | Reimplemented @@ -41541,7 +41541,7 @@ SHC_3BB0A8C1_0x00501760 | 100.0% | Reimplemented SHC_3BB0A8C1_0x005017A0 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x005017C0 | 0.0% | Pending +SHC_3BB0A8C1_0x005017C0 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00501A20 | 100.0% | Reimplemented @@ -41563,23 +41563,23 @@ SHC_3BB0A8C1_0x00501CC8 | 0.0% | Pending SHC_3BB0A8C1_0x00501CD0 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x00501D90 | 0.0% | Pending +SHC_3BB0A8C1_0x00501D90 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x00501F50 | 0.0% | Pending +SHC_3BB0A8C1_0x00501F50 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00501FE0 | 0.0% | Pending -SHC_3BB0A8C1_0x00502110 | 0.0% | Pending +SHC_3BB0A8C1_0x00502110 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x00502390 | 0.0% | Pending +SHC_3BB0A8C1_0x00502390 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x005024F0 | 0.0% | Pending +SHC_3BB0A8C1_0x005024F0 | 83.8% | register colouring tie -SHC_3BB0A8C1_0x00502680 | 0.0% | Pending +SHC_3BB0A8C1_0x00502680 | 76.4% | FgCodeMotion arm placement SHC_3BB0A8C1_0x00502950 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x005029D0 | 0.0% | Pending +SHC_3BB0A8C1_0x005029D0 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00502F14 | 0.0% | Pending @@ -41615,7 +41615,7 @@ SHC_3BB0A8C1_0x00502F29 | 0.0% | Pending SHC_3BB0A8C1_0x00502F2A | 0.0% | Pending -SHC_3BB0A8C1_0x00502F30 | 0.0% | Pending +SHC_3BB0A8C1_0x00502F30 | 100.0% | Reimplemented SHC_3BB0A8C1_0x0050347C | 0.0% | Pending @@ -41655,7 +41655,7 @@ SHC_3BB0A8C1_0x005034A0 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00503720 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x005037B0 | 0.0% | Pending +SHC_3BB0A8C1_0x005037B0 | 95.5% | slot weights; keep/gate slots SHC_3BB0A8C1_0x005046A0 | 0.0% | Pending @@ -41959,7 +41959,7 @@ SHC_3BB0A8C1_0x00504A26 | 0.0% | Pending SHC_3BB0A8C1_0x00504A27 | 0.0% | Pending -SHC_3BB0A8C1_0x00504A30 | 0.0% | Pending +SHC_3BB0A8C1_0x00504A30 | 94.2% | register allocation SHC_3BB0A8C1_0x00504E88 | 0.0% | Pending @@ -42069,7 +42069,7 @@ SHC_3BB0A8C1_0x00504ED1 | 0.0% | Pending SHC_3BB0A8C1_0x00504EE0 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x00504F10 | 0.0% | Pending +SHC_3BB0A8C1_0x00504F10 | 78.0% | register allocation SHC_3BB0A8C1_0x005050F0 | 0.0% | Pending @@ -42081,23 +42081,23 @@ SHC_3BB0A8C1_0x00505A10 | 0.0% | Pending SHC_3BB0A8C1_0x005060C0 | 0.0% | Pending -SHC_3BB0A8C1_0x00506370 | 0.0% | Pending +SHC_3BB0A8C1_0x00506370 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00506AD0 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x00506BD0 | 0.0% | Pending +SHC_3BB0A8C1_0x00506BD0 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x00506D40 | 0.0% | Pending +SHC_3BB0A8C1_0x00506D40 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x00506E10 | 0.0% | Pending +SHC_3BB0A8C1_0x00506E10 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x00506F40 | 0.0% | Pending +SHC_3BB0A8C1_0x00506F40 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x00507060 | 0.0% | Pending +SHC_3BB0A8C1_0x00507060 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x00507130 | 0.0% | Pending +SHC_3BB0A8C1_0x00507130 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x00507280 | 0.0% | Pending +SHC_3BB0A8C1_0x00507280 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00507350 | 100.0% | Reimplemented @@ -42135,37 +42135,37 @@ SHC_3BB0A8C1_0x0050741D | 0.0% | Pending SHC_3BB0A8C1_0x0050741E | 0.0% | Pending -SHC_3BB0A8C1_0x00507420 | 0.0% | Pending +SHC_3BB0A8C1_0x00507420 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x00507560 | 0.0% | Pending +SHC_3BB0A8C1_0x00507560 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x005076A0 | 0.0% | Pending +SHC_3BB0A8C1_0x005076A0 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00507830 | 0.0% | Pending -SHC_3BB0A8C1_0x00507BD0 | 0.0% | Pending +SHC_3BB0A8C1_0x00507BD0 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00507D50 | 0.0% | Pending -SHC_3BB0A8C1_0x00507E00 | 0.0% | Pending +SHC_3BB0A8C1_0x00507E00 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00507F80 | 0.0% | Pending -SHC_3BB0A8C1_0x00508030 | 0.0% | Pending +SHC_3BB0A8C1_0x00508030 | 98.0% | imul register naming -SHC_3BB0A8C1_0x00508250 | 0.0% | Pending +SHC_3BB0A8C1_0x00508250 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x00508540 | 0.0% | Pending +SHC_3BB0A8C1_0x00508540 | 99.3% | jl loop end needs unroll SHC_3BB0A8C1_0x00508590 | 0.0% | Pending -SHC_3BB0A8C1_0x00508760 | 0.0% | Pending +SHC_3BB0A8C1_0x00508760 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00508870 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x00508910 | 0.0% | Pending +SHC_3BB0A8C1_0x00508910 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x00508A00 | 0.0% | Pending +SHC_3BB0A8C1_0x00508A00 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00508C7C | 0.0% | Pending @@ -42763,11 +42763,11 @@ SHC_3BB0A8C1_0x00508DC2 | 0.0% | Pending SHC_3BB0A8C1_0x00508DC3 | 0.0% | Pending -SHC_3BB0A8C1_0x00508DD0 | 0.0% | Pending +SHC_3BB0A8C1_0x00508DD0 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x00508EC0 | 0.0% | Pending +SHC_3BB0A8C1_0x00508EC0 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x00509180 | 0.0% | Pending +SHC_3BB0A8C1_0x00509180 | 100.0% | Reimplemented SHC_3BB0A8C1_0x0050A74E | 0.0% | Pending @@ -44177,7 +44177,7 @@ SHC_3BB0A8C1_0x00511020 | 100.0% | Reimplemented SHC_3BB0A8C1_0x005110B0 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x005111D0 | 0.0% | Pending +SHC_3BB0A8C1_0x005111D0 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00511240 | 0.0% | Pending @@ -44187,7 +44187,7 @@ SHC_3BB0A8C1_0x005114C0 | 100.0% | Reimplemented SHC_3BB0A8C1_0x005115C0 | 0.0% | Pending -SHC_3BB0A8C1_0x005116E0 | 0.0% | Pending +SHC_3BB0A8C1_0x005116E0 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00511850 | 100.0% | Reimplemented @@ -44733,7 +44733,7 @@ SHC_3BB0A8C1_0x00512100 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00512180 | 0.0% | Pending -SHC_3BB0A8C1_0x00512450 | 0.0% | Pending +SHC_3BB0A8C1_0x00512450 | 100.0% | Reimplemented SHC_3BB0A8C1_0x005127B0 | 100.0% | Reimplemented @@ -44741,43 +44741,43 @@ SHC_3BB0A8C1_0x005127D0 | 0.0% | Pending SHC_3BB0A8C1_0x00512920 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x00512940 | 0.0% | Pending +SHC_3BB0A8C1_0x00512940 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00514480 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x00514520 | 0.0% | Pending +SHC_3BB0A8C1_0x00514520 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x005146D0 | 0.0% | Pending +SHC_3BB0A8C1_0x005146D0 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00514BC0 | 0.0% | Pending -SHC_3BB0A8C1_0x00514DE0 | 0.0% | Pending +SHC_3BB0A8C1_0x00514DE0 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x00514F30 | 0.0% | Pending +SHC_3BB0A8C1_0x00514F30 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x005151C0 | 0.0% | Pending +SHC_3BB0A8C1_0x005151C0 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00515250 | 0.0% | Pending -SHC_3BB0A8C1_0x005154D0 | 0.0% | Pending +SHC_3BB0A8C1_0x005154D0 | 100.0% | Reimplemented SHC_3BB0A8C1_0x005155E0 | 0.0% | Pending SHC_3BB0A8C1_0x00515680 | 0.0% | Pending -SHC_3BB0A8C1_0x00515740 | 0.0% | Pending +SHC_3BB0A8C1_0x00515740 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00515A20 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x00515C80 | 0.0% | Pending +SHC_3BB0A8C1_0x00515C80 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x00515DA0 | 0.0% | Pending +SHC_3BB0A8C1_0x00515DA0 | 98.1% | register allocation SHC_3BB0A8C1_0x00515F40 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x005160C0 | 0.0% | Pending +SHC_3BB0A8C1_0x005160C0 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x005162D0 | 0.0% | Pending +SHC_3BB0A8C1_0x005162D0 | 97.3% | Style pass, asm differs SHC_3BB0A8C1_0x00516A64 | 0.0% | Pending @@ -45133,7 +45133,7 @@ SHC_3BB0A8C1_0x00516B73 | 0.0% | Pending SHC_3BB0A8C1_0x00516B74 | 0.0% | Pending -SHC_3BB0A8C1_0x00516B80 | 0.0% | Pending +SHC_3BB0A8C1_0x00516B80 | 89.5% | volatile tail merge SHC_3BB0A8C1_0x005176C8 | 0.0% | Pending @@ -45435,7 +45435,7 @@ SHC_3BB0A8C1_0x00517789 | 0.0% | Pending SHC_3BB0A8C1_0x0051778A | 0.0% | Pending -SHC_3BB0A8C1_0x00517790 | 0.0% | Pending +SHC_3BB0A8C1_0x00517790 | 99.9% | one store order SHC_3BB0A8C1_0x00518068 | 0.0% | Pending diff --git a/tools/compiler-internals/merge_probe.py b/tools/compiler-internals/merge_probe.py index 0875e93532..345fe9ba0e 100644 --- a/tools/compiler-internals/merge_probe.py +++ b/tools/compiler-internals/merge_probe.py @@ -79,7 +79,7 @@ ('epilog_attempt', '0x10b321e0'), # @FgTryEpilogMerge@20 entry ] -out = (common.ROOT / common.TMP / 'mergeprobe').resolve() +out = (common.ROOT / common.TMP / ('mergeprobe' + ('_' + os.environ['QD_OUT'] if os.environ.get('QD_OUT') else ''))).resolve() out.mkdir(parents=True, exist_ok=True) BS = chr(92) Q = chr(34) diff --git a/tools/compiler-internals/msvc-backend.md b/tools/compiler-internals/msvc-backend.md index c38bc6e8a5..134af1a2f5 100644 --- a/tools/compiler-internals/msvc-backend.md +++ b/tools/compiler-internals/msvc-backend.md @@ -3041,3 +3041,141 @@ 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. + +## Decoded on the TileMapState last mile (2026-11-15) + +Eight more c2 decisions, traced with Ghidra on `c2.dll` and the cdb probes while finishing +`Map::TileMapState`. Each entry says what decides, what feeds it from the source, and whether a lever was found. +Addresses are for the unrebased load at `0x10b00000`. + +### Inlining is gated on `_Globopt_flag`: only `__forceinline` callees inline in an `/Od` function + +`InlCallTupleIsCandidate` (`0x10b69bd0`) ends at `0x10b69d48` with + +``` +test edi, edi ; edi = _Globopt_flag (0x10d0d708) +jnz accept +test ecx, 0x2080 ; ecx = callee function record flags (+0x50) +jz reject +``` + +so with global optimisation off a callee is an inline candidate only with bit `0x2000` (`__forceinline`, which +also skips the `_MaxInlineeSize` test at `0x10b69d26`) or `0x80` (not decoded). Plain `inline` is bit `0x40` and +is not enough. The module gate in `Inline` (`0x10b69110`) is the `/Ob` level (`DAT_10d127d0`, written by +`MscProcessSwitches`), nonzero under `/O2`, so passing `/Ob` changes nothing. `#pragma inline_depth`, +`inline_recursion` and `auto_inline` are not honoured either. + +`_Globopt_flag` is cleared two ways: the `g` letter of `#pragma optimize` (`MscProcessPragstat`), and +`dop2_GetInitialTuples` (`0x10c06670`, at `0x10c06834`-`0x10c06879`), which calls `MscCheckAsm` (`0x10b9d6a0`) +on any function with inline asm (func `+0xac` bit `0x10`) and, if control flows into or out of an `__asm` block, +raises C4740 and clears `_Globopt_flag` and `0x10d0d714` for that function. That is the whole of the `/Od` look: +`optimize("g", off)` alone gives byte-identical code to `optimize("", off)`; `y` does nothing once `g` is off; +`t` off switches to favour-size and is much worse. + +Consequence for this project: every `MACRO_CALL` in an `/Od` function (pragma or C4740) compiled to +`call Resolver::get` + `call eax` where the original has one direct `call`, two bytes longer per call, so every +jump displacement after the first call shifted too. `get()` in `src/precomp/FunctionResolver.h` is now +`__forceinline` (all five definitions - the implemented, the game-function and the wrapper unifiers, `GameFunction` +and the member `GameFunction` macro; one alone leaves calls to disabled callees indirect). Optimised functions are +unaffected because they inlined `get()` already. Measured with reccmp: `setBitFlagBasedOnWallTowerGatehouseOrKeep`, +`setTerrain`, `updateGFXLayers`, `updateMacroLayerRelated` and `updateGfxLayer` (C4740) went from 92.8-98.1% to +100%, and nothing else in the TileMapState build list moved. A build of all 2515 sources under `src/OpenSHC` +with each header (2533 functions) agreed: those five better, and the only two "worse" functions +(`UI::Menu::Constructor_Menu`, `handleMenuItems`) differ solely in a `cmp .., 0x11000000` immediate that reccmp +now prints as a symbol name, because the layout shift put a compiler-generated destructor of our DLL at that VA. +The bytes are unchanged. + +### Loop unrolling: `!=` is not a counted loop, and loads == stores vetoes the unroll + +`LurClassifyNests` -> `LurClassifyLoop` (`0x10b90650`) -> `LurClassifyDo` (`0x10b8f4f0`) -> +`LurDetermineDoUnrollAmount` (`0x10b91160`, body counted by `LurCountTuples` `0x10b8e090`). A `!=` bound is +classified 5 (not counted) and never reaches the unroller - that is why `!=` suppresses unrolling. A `<` bound is +classified 1, and the amount is 0 (no unroll, `jl` back-edge) when + +- the body has more than 128 tuples, or +- it stores to memory but never loads, or +- **it has no calls and its memory loads equal its memory stores**, or +- it has more than 15 blocks, unless call-free with more than 50 tuples. + +Otherwise the factor is the largest of 6..2 (5..2 above 50 tuples) dividing the trip count. The counts are taken +before CSE and store-to-load forwarding, but after dead loads are removed, so a duplicated read before any store +counts and a dead read does not. `placeStockpile` keeps `!=` (one store more than loads; a `<` gives x3 unroll). + +### Post-allocation scheduler: address-taken locals add store -> load edges + +`SchSelectInstrs` (`0x10c722c0`) with `SchComputeStaticPriority` (`0x10c71f00`): priority = +branch/compare flag << 23 + memory-source flag << 15 + (height + field `+0x26`) << 10, ties by IL order, one +instruction per cycle on these functions. A store to an address-taken local (`(int*)&y` passed to +`getTileForBrush`), or through a `T&` that may alias one, orders every later load after it, so the IL position of +such a store relative to a test decides the issue order (`placeTree`, `processEntityDamageToBuildingCollateral`). +Conversely a store through a `byte&` keeps it ahead of a following read-modify-write on another layer +(`useTerrainHeightBrush`). + +### Copy preferences go only to partners that are not coloured yet + +`InsertSplitPoints` (`0x10b13460`) fills `_UpdatePrefsBv` (`0x10d15088`) at `0x10b139b3` (candidate defined by a +copy from an uncoloured globreg) and `0x10b14047` (candidate's first use in a block is the source of a copy into a +globreg). At the end of `ChoosePhysReg` (`0x10b14b70`) each uncoloured member gets +1 for the chosen register in +its `+0x38` list. So of two copy partners the one coloured first takes a register without preference and the copy +survives. Lever: when copy propagation rewrote a call argument to the source variable (a separate, higher-keyed +candidate that then wins), `T const v = *(T volatile*)&src;` stops the propagation (`useEraserBrush` -> 1.0). + +### Spill reload on a new edge block: the latch's first predecessor decides + +`RewriteSplitGlobregs` (`0x10b12d20`, from `ColProcessSplitGlobregs` `0x10b11b20`) picks the piece of a split +counter at the loop latch from the latch's **first** predecessor. If that predecessor holds it in memory, +`GrgAirlockEnterloads` (`0x10b59320`) must reload on the latch -> head edge and, the edge source having several +successors, `FgInsertAirlockBlock` adds a block: a stray `jmp` + reload and every later displacement 4 bytes off. +`do { ... } while (++i < N);` instead of `i++; if (i >= N) break;` changes the first predecessor +(`updateBuildingGraphicsLayer`). + +### Branch-free `?:` is refused for a volatile arm + +`BuildQuestion` (`0x10b9e5c0`, via `MscBuildQuestion` `0x10b9dfe0` from `dop2_PreLowerGlobalOpts`) -> +`MscIsQuestionable` (`0x10b9e0e0`) -> `QuesNoJumpsNeeded` (`0x10c5dc60`, `FindSbbPattern` / `FindSetccPattern` +against `DAT_10cdf0a0[_Speed == 0]`). An arm is refused when `MscUnsafeQuestionArmLeaf` (`0x10b9e970`) is set - +bit 6 of the leaf's byte `+0x14`, which a volatile symbol has - or when the else label has a second reference. +If the original has `neg/sbb/and` or `setcc` on a value we made volatile, copy it into a plain local just before +the `?:` (`processDamageToBuilding`). The converse also holds: explicit branches in the original where we get +`setcc`/`lea` mean the original's arm failed one of these checks. + +### Code motion past a `ret` needs a non-falling-through predecessor + +`FgCodeMotion` (`0x10b312c0`, called only from `dop2_FinalFlowOpts` after `FgDoLateFlowOpts(2)`) replaces a +`goto L` (or a `jcc A; goto L` pair, trying A after `FgInvertAndSwapBranches`) with the code range at L. +`FgCanMoveCode` (`0x10b319b0`) allows it only if L is forward, the tuple after the jump is another label, the range +ends at the first non-falling-through instruction without reaching `OPSEGMENT`, and **the last real instruction +before L does not fall through** (`FgSafeLocation` `0x10b31aa0`; exit gotos count as not falling through). An +original with arms placed after the `ret` that jump back therefore had a goto target preceded by a jump - usually +a cross-jump that retargeted a then-arm into the else chain's last block. The input order comes from +`FgDFOReorderLoopRecog`: `GrInitDFOWalk` (`0x10b5cd30`) follows successor lists that `FgAddAllArcs` +(`0x10b28910`) builds by pushing each arc to the front, so the physically later successor is explored first and +`FgChangeNextToDFOEx` (`0x10b281e0`) keeps source order in reverse post-order. Any if/else-if chain thus puts its +last arm right before the join; `useTerrainHeightBrush` is still open on this. + +### Stack slots: weights are counted before cross-jumping, and small frames are not sorted + +`ColorStackElements` (`0x10beaef0`, with `colorElemental` `0x10beb760`): parameter homes become colours first, in +element-list order; every other element takes the first parameter colour it does not interfere with, else an +existing local colour from the newest backwards, else a new colour (first created = lowest address). The list is +size ascending then weight descending (`AddStackElement` `0x10be9190`), and for a frame of 0x80 bytes or less +without a /GS buffer `QSortColors` never runs, so first reference in `CollectStackElements`' block walk +(`0x10be8f10`, via `EnumerateStackElements` `0x10be92d0`) decides ties. The weight counts references **before** +tail merging: two identical stores that later fold into one still count twice. That explains an original calling +`isAIPlayer` and discarding the result - both arms assigned the same `distance`/`range` - and is a lever: writing +the duplicated stores back raises a local's weight without changing the code (`checkBuildingCanBePlacedHere`). +Reusing a parameter for a later coordinate moves that value's spills into the parameter's home. + +### Smaller notes + +- c2 packs locals into dead parameter home slots, including a 4-byte local into a 2-byte parameter's slot, so a + dword store to a parameter's slot does not prove the parameter is a dword (`placeRock`). +- A trailing `+ret N` on our side of a reccmp diff, when ours is shorter in bytes, is reccmp clipping the + original's stream at our length, not a tail difference. +- `quick_diff.py` mishandles `/Od` and asm-heavy listings (`_x$[ebp]` slots, `DB`-encoded jcc, `global+offset`), + and cannot measure a header change while the PCH is used - the header is baked in. Compile such variants + without the PCH. +- `QD_OUT` is a folder name under `build-RelWithDebInfo/batch/`, not a path. +- Editing a header under `src/precomp/` does **not** rebuild the PCH: `cmake_pch.cxx.obj.d` is empty, so the + build reports `BUILD_OK`, recompiles nothing that matters and reccmp shows the old code. Delete + `build-RelWithDebInfo/CMakeFiles/OpenSHC.dll.dir/cmake_pch.cxx.obj` and `cmake_pch.cxx.pch` first. diff --git a/tools/compiler-internals/reg_scores.py b/tools/compiler-internals/reg_scores.py index 81e51b4288..c6ad0b1825 100644 --- a/tools/compiler-internals/reg_scores.py +++ b/tools/compiler-internals/reg_scores.py @@ -109,7 +109,7 @@ and not whycost and not inv): sys.exit(__doc__) src = os.path.abspath(args[0]) -out = (common.ROOT / common.TMP / 'regscores').resolve() +out = (common.ROOT / common.TMP / ('regscores' + ('_' + os.environ['QD_OUT'] if os.environ.get('QD_OUT') else ''))).resolve() out.mkdir(parents=True, exist_ok=True) diff --git a/tools/compiler-internals/slot_probe.py b/tools/compiler-internals/slot_probe.py index cf45aa795b..00b1cb930a 100644 --- a/tools/compiler-internals/slot_probe.py +++ b/tools/compiler-internals/slot_probe.py @@ -49,7 +49,7 @@ ADD_STACK_ELEMENT = '0x10be9190' COLOR_ENTRY = '0x10beaef0' -out = (common.ROOT / common.TMP / 'slotprobe').resolve() +out = (common.ROOT / common.TMP / ('slotprobe' + ('_' + os.environ['QD_OUT'] if os.environ.get('QD_OUT') else ''))).resolve() out.mkdir(parents=True, exist_ok=True) BS = chr(92) Q = chr(34) diff --git a/tools/reimplementation-helper/batch/README.md b/tools/reimplementation-helper/batch/README.md index c5172b77a2..b6e5ef8793 100644 --- a/tools/reimplementation-helper/batch/README.md +++ b/tools/reimplementation-helper/batch/README.md @@ -45,6 +45,7 @@ functions and the traps - is in [../../compiler-internals/finding-mismatches.md] | `diff_lea.py [--run] [NAME]` | `lea reg, [a + b]` on our side where the original has the destructive `add a, b`: `field = snapshotLocal + delta` has to keep the snapshot, `field = field + delta` updates the cached read in place. Drop the local when nothing between the read and the use writes the field - that was `updatePopularity`'s last 6.1%. `RLEA` is the reverse, a local the original keeps and we do not. | | `diff_signed.py [NAME-SUBSTRING]` | Comparisons the original makes signed and we make unsigned (or the reverse): `jg/jge/jl/jle` against our `ja/jae/jb/jbe` over the same `cmp`. Fix it by comparing through `(int)` in the source, not by editing the generated header, and re-run the tool to confirm - one instruction barely moves the percentage. | | `quick_diff.py FILE.cpp... [-q] [--name FUNCTION]` | Compile one file with `/FA` and diff it against the original function disassembled straight from `_original/` - about 6 seconds, no link and no reccmp, so a dozen source variants can be tried in the time of one build. Handles free functions (the mangled scope is the innermost namespace), x87 operand spellings (`fmulp ST(2), ST(0)` vs `fmulp st(2)`, `__real@` constants) and indexed local-array slots (`_arr$[esp+edi*8+112]`); before those three fixes every floating-point instruction and every local-array element access on our side mismatched and `structural_diff.py` reported duplicates that did not exist. Shows the whole original function, which reccmp's diff does not when ours is shorter. A triage loop: confirm a 1.0 with `reccmp_report.py --run`, and do not trust a *rise* of its ratio below about 0.6 - on a badly matching function difflib rewards a different alignment, and one such "gain" was a 10-point loss in reccmp. | +| `quick_gl.py FILE.cpp [CALLEE.cpp...] [-q] [-s]` | `quick_diff.py` for a file in `cmake/compiler-flags-gl.txt`: compiles it with `/GL` and LTCG-links it into a throw-away DLL with the `/GL` objects of the callees it calls through `X_Func::` (found in the build list and the GL list), then diffs the function against the original. quick_diff strips `/GL`, so it cannot show an `ecx`/`edx` kept across a call; this does, in about 6 seconds. Needs a full build first (for the callee objects) and the callees' resolvers enabled (`enable_local_functions.py`). `aiFindBuildingToAttack` reads 0.7027 in quick_diff and 1.0000 here, as in reccmp. | | `push_pattern.py FILE.cpp...` | Per call, the argument-push run as `pushes/interleaved` for both sides. A neat run (`0` interleaved) means every argument was already in a register or slot, i.e. the source had a **local**; a mixed run is what writing the expression inline at the call produces. A starred row is a call where the two sides disagree - add the local, or drop it, accordingly. Counts only calls whose set-up it can see, so a differing call *total* means our source has an extra call (a duplicated block), not that the tool failed. | | `perm_decls.py FILE FIRST LAST` | Permute one short run of declarations and keep the order that matches best, scored by `quick_diff.py -s`. Declaration order does **not** decide stack slots - the colorer sorts by reference weight per byte and leaves only *ties* to the order the symbols arrive in, so this pays exactly when two locals of equal weight are declared the wrong way round (`assignSelectionToKey` 95.9% -> 100% from one swapped line) and does nothing otherwise. Run `slot_weight.py` first to see whether the weights tie at all. For initialised declarations the order does decide which *register* each one gets, which is a separate effect worth permuting for. | | `reorder_search.py FILE NAME [--pairs] [--dry]` | Hill-climb the match % by reordering independent statements: permute runs of >=3 by default, or swap every adjacent independent pair with `--pairs`. Builds once per move and reverts anything that does not improve. | diff --git a/tools/reimplementation-helper/batch/quick_diff.py b/tools/reimplementation-helper/batch/quick_diff.py index aa25ea7828..d5b5ab3ebf 100644 --- a/tools/reimplementation-helper/batch/quick_diff.py +++ b/tools/reimplementation-helper/batch/quick_diff.py @@ -39,7 +39,10 @@ ENV = common.build_env() ENV['PATH'] = os.path.join(ROOT, 'MSVC1400-SP1', 'Common7', 'IDE') + os.pathsep + ENV['PATH'] cc = json.load(open('build-RelWithDebInfo/compile_commands.json')) -base = [e for e in cc if 'OpenSHC.dll.dir' in e['command'] and '/src/OpenSHC/' in e['file'].replace(chr(92), '/')][0] +cands = [e for e in cc if 'OpenSHC.dll.dir' in e['command'] and '/src/OpenSHC/' in e['file'].replace(chr(92), '/')] +# a /GL object writes no /FA listing, so take the command of a file outside cmake/compiler-flags-gl.txt +base = ([e for e in cands if ' /GL' not in e['command']] or cands)[0] +base = dict(base, command=base['command'].replace(' /GL', '')) # QD_OUT picks the output folder, so concurrent runs (quick_reorder.py) do not share x.asm out = ROOT + '/build-RelWithDebInfo/batch/' + os.environ.get('QD_OUT', 'quick') os.makedirs(out, exist_ok=True) diff --git a/tools/reimplementation-helper/batch/quick_gl.py b/tools/reimplementation-helper/batch/quick_gl.py new file mode 100644 index 0000000000..268df811d4 --- /dev/null +++ b/tools/reimplementation-helper/batch/quick_gl.py @@ -0,0 +1,122 @@ +"""quick_diff for files in cmake/compiler-flags-gl.txt: compile ONE file with /GL, LTCG-link it with its callees. + +usage: quick_gl.py FILE.cpp [CALLEE.cpp ...] [-q] [-s] + +quick_diff.py compiles without /GL, so it cannot show what LTCG buys a caller - typically a volatile register +(ecx/edx) kept alive across a call to a callee that is known not to touch it. This compiles FILE with the /GL +command line of the build, links it with /LTCG into a throw-away DLL (/NOENTRY /FORCE:UNRESOLVED) together with +the already-built /GL objects of its callees, and diffs the function's code in that DLL against the original with +quick_diff's normalizer. About a minute per run; QD_OUT picks the output folder as for quick_diff. + +Callees are found automatically and transitively: every `X_Func::name` the file (or one of its callees) calls +whose `.../X/name.cpp` is both in the build list and in cmake/compiler-flags-gl.txt (and so has a /GL object in +the build directory). Without the callee's own callees LTCG has to assume a global may change across the call +(updateGateDrawBridgeOpenCloseLogic read 0.91 with only its direct callee, 1.00 with the chain). Extra +CALLEE.cpp arguments are added to that set. The callee's MACRO_FUNCTION_RESOLVER must be enabled (enable_local_functions.py), +otherwise the call goes through the original binary and LTCG sees nothing. Run a full build first so the callee +objects exist, and do not run this while a build is running. +""" +import difflib +import os +import re +import struct +import subprocess +import sys + +args = [a for a in sys.argv[1:] if not a.startswith('-')] +quiet = '-q' in sys.argv +keep = '-s' in sys.argv +if not args: + sys.exit(__doc__) +target = os.path.abspath(args[0]) +extra = [os.path.abspath(a) for a in args[1:]] +sys.argv = [sys.argv[0]] + (['-s'] if keep else []) +import quick_diff as q # noqa: E402 (parses sys.argv on import) + +ROOT = q.ROOT +out = q.out + + +def rel(p): + return os.path.relpath(p, ROOT).replace(chr(92), '/') + + +def obj_of(src): + return ROOT + '/build-RelWithDebInfo/CMakeFiles/OpenSHC.dll.dir/' + rel(src) + '.obj' + + +gl = set(l.strip() for l in open(ROOT + '/cmake/compiler-flags-gl.txt') if l.strip()) +build_list = [l.strip() for l in open(ROOT + '/cmake/openshc-sources.txt.local') if l.strip()] +src = open(target, encoding='utf-8').read() +va = int(re.search(r'FUNCTION: STRONGHOLDCRUSADER (0x[0-9A-Fa-f]+)', src).group(1), 16) +cls, name = re.search(r'FUNCTION: STRONGHOLDCRUSADER[^\n]*\n[^\n(]*?(\w+)::(\w+)\(', src).groups() + +def direct_callees(text): + found = [] + for c, n in sorted(set(re.findall(r'(\w+)_Func::\s*(\w+)', text))): + for f in build_list: + if f.endswith('/%s/%s.cpp' % (c, n)) and f in gl and os.path.exists(obj_of(ROOT + '/' + f)): + found.append(ROOT + '/' + f) + return found + + +# transitively: LTCG only knows a callee leaves a register or a global alone if it also sees what that callee calls +callees = [] +todo = direct_callees(src) + extra +while todo: + c = todo.pop(0) + if c in callees or os.path.abspath(c) == target: + continue + callees.append(c) + todo += direct_callees(open(c, encoding='utf-8').read()) +objs = [obj_of(c) for c in callees if os.path.abspath(c) != target] + +# the mangled name comes from a plain /FA compile of the same file +if q.ours(target, cls, name) is None: + sys.exit(1) +sym = re.search(r'^(\?%s@%s@\S+) PROC' % (name, cls), open(out + '/x.asm', errors='replace').read(), re.M).group(1) + +glc =[e for e in q.cands if ' /GL' in e['command']] +if not glc: + sys.exit('no /GL compile command in compile_commands.json - is any build-list file in compiler-flags-gl.txt?') +cmd = glc[0]['command'] +glo = out + '/gl.obj' +cmd = re.sub(r'/Fo\S+', lambda m: '/Fo' + glo, cmd) +cmd = re.sub(r'-c \S+$', lambda m: '-c ' + target, cmd) +r = subprocess.run(cmd, cwd=glc[0]['directory'], env=q.ENV, capture_output=True, text=True) +if r.returncode: + print('\n'.join(l for l in r.stdout.splitlines() if 'error' in l) or r.stdout[-800:]) + sys.exit(1) + +dll = out + '/gl.dll' +for p in (dll, out + '/gl.map'): + if os.path.exists(p): + os.remove(p) +link = [ROOT + '/MSVC1400-SP1/VC/bin/link.exe', '/nologo', '/LTCG', '/DLL', '/NOENTRY', '/FORCE:UNRESOLVED', + '/OPT:NOREF', '/OPT:NOICF', '/INCLUDE:' + sym, '/MAP:' + out + '/gl.map', '/OUT:' + dll, + '/PDB:' + out + '/gl.pdb', '/NODEFAULTLIB', glo] + objs +r = subprocess.run(link, cwd=out, env=q.ENV, capture_output=True, text=True) +if not os.path.exists(dll) or 'fatal' in r.stdout: + print(r.stdout[-1500:]) + sys.exit(1) +mm = re.search(r'^\s*\S+\s+' + re.escape(sym) + r'\s+([0-9a-f]{8})', open(out + '/gl.map', errors='replace').read(), + re.M) +fva = int(mm.group(1), 16) + +a = [q.canon(x) for x in q.orig(va)] +# point quick_diff's disassembler at our DLL and reuse the same normalizer +d = open(dll, 'rb').read() +pe = struct.unpack_from('