| 73 | template <class S> |
| 74 | class ShapeHistory |
| 75 | { |
| 76 | public: |
| 77 | typedef typename std::map<size_t, S>::const_iterator const_iterator; |
| 78 | |
| 79 | ShapeHistory(size_t baseId = 1) : m_HistoryBase(baseId) |
| 80 | { |
| 81 | } |
| 82 | bool isInHistory(size_t id) const |
| 83 | { |
| 84 | return id >= m_HistoryBase; |
| 85 | } |
| 86 | void add(size_t id, S const & e) |
| 87 | { |
| 88 | m_Modifications.insert(std::make_pair(id, e)); |
| 89 | if (m_Modifications.size() > 10) { |
| 90 | m_HistoryBase = m_Modifications.begin()->first + 1; |
| 91 | m_Modifications.erase(m_Modifications.begin()); |
| 92 | } |
| 93 | } |
| 94 | void reset(size_t id) |
| 95 | { |
| 96 | m_Modifications.clear(); |
| 97 | m_HistoryBase = id; |
| 98 | } |
| 99 | const_iterator end() const |
| 100 | { |
| 101 | return m_Modifications.end(); |
| 102 | } |
| 103 | const_iterator get(size_t id) const |
| 104 | { |
| 105 | return m_Modifications.lower_bound(id); |
| 106 | } |
| 107 | |
| 108 | private: |
| 109 | std::map<size_t, S> m_Modifications; |
| 110 | size_t m_HistoryBase; |
| 111 | }; |
| 112 | |
| 198 | //m_ExpandPathfinding = entity_pos_t::FromInt(CELL_SIZE / 2); |
| 199 | // Actually that's bad because units get stuck when the A* pathfinder thinks they're |
| 200 | // blocked on all sides, so it's better to underestimate |
| 201 | m_ExpandPathfinding = entity_pos_t::FromInt(0); |
| 202 | |
| 203 | // For AI building foundation planning, we want to definitely block all |
| 204 | // potentially-obstructed tiles (so we don't blindly build on top of an obstruction), |
| 205 | // so we need to expand by at least 1/sqrt(2) of a tile |
| 206 | m_ExpandFoundation = (entity_pos_t::FromInt(CELL_SIZE) * 3) / 4; |
| 207 | |
| 517 | // For tile-based pathfinding: |
| 518 | // Since we only count tiles whose centers are inside the square, |
| 519 | // we maybe want to expand the square a bit so we're less likely to think there's |
| 520 | // free space between buildings when there isn't. But this is just a random guess |
| 521 | // and needs to be tweaked until everything works nicely. |
| 522 | entity_pos_t m_ExpandPathfinding; |
| 523 | entity_pos_t m_ExpandFoundation; |
| 524 | |
| 525 | std::multimap<u32, u32> m_StaticShapesInfluence; // key : a tile index ((i << 16) + j) ; value : index of a static shape influencing the tile |
| 526 | std::multimap<u32, u32> m_UnitShapesInfluence; // key : a tile index ((i << 16) + j) ; value : index of a unit shape influencing the tile |
| 527 | |
| 529 | * Register tiles that are affected by the shape |
| 530 | */ |
| 531 | void RegisterShapeTiles(StaticShape const & shape, u32 id); |
| 532 | void RegisterShapeTiles(UnitShape const & shape, u32 id); |
| 533 | |
| 534 | /** |
| 535 | * Unregister tiles that were affected by the shape |
| 536 | */ |
| 537 | void UnregisterShapeTiles(StaticShape const & shape, u32 id); |
| 538 | void UnregisterShapeTiles(UnitShape const & shape, u32 id); |
| 539 | |
| 540 | /** |
| 541 | * Update obstruction flags of a tile |
| 542 | */ |
| 543 | void UpdateTile(u16 i, u16 j, Grid<u8>& grid); |
| 544 | |
| 545 | /** |
| 546 | * Update obstruction flags of tiles affected by the shape |
| 547 | */ |
| 548 | void UpdateTilesUnderShape(StaticShape const & shape, Grid<u8>& grid, entity_pos_t const & expand); |
| 549 | void UpdateTilesUnderShape(UnitShape const & shape, Grid<u8>& grid, entity_pos_t const & expand); |
| 550 | |
| 551 | |
| 552 | /** |
711 | | PROFILE("Rasterise"); |
| 827 | u16 i0, j0, i1, j1; |
| 828 | NearestTile(center.X - halfBound.X, center.Y - halfBound.Y, i0, j0, grid.m_W, grid.m_H); |
| 829 | NearestTile(center.X + halfBound.X, center.Y + halfBound.Y, i1, j1, grid.m_W, grid.m_H); |
| 830 | for (u16 j = j0; j <= j1; ++j) |
| 831 | { |
| 832 | for (u16 i = i0; i <= i1; ++i) |
| 833 | { |
| 834 | entity_pos_t x, z; |
| 835 | TileCenter(i, j, x, z); |
| 836 | if (Geometry::PointIsInSquare(CFixedVector2D(x, z) - center, shape.u, shape.v, halfSize)) |
| 837 | grid.set(i, j, grid.get(i, j) | modificator); |
| 838 | } |
| 839 | } |
| 840 | } |
| 841 | static void ModifyTilesUnderShape(UnitShape const & shape, Grid<u8>& grid, entity_pos_t const & expand, ICmpObstructionManager::TileObstruction modificator) |
| 842 | { |
| 843 | CFixedVector2D center(shape.x, shape.z); |
| 844 | entity_pos_t r = shape.r + expand; |
713 | | grid.m_DirtyID = m_DirtyID; |
| 846 | u16 i0, j0, i1, j1; |
| 847 | NearestTile(center.X - r, center.Y - r, i0, j0, grid.m_W, grid.m_H); |
| 848 | NearestTile(center.X + r, center.Y + r, i1, j1, grid.m_W, grid.m_H); |
| 849 | for (u16 j = j0; j <= j1; ++j) |
| 850 | for (u16 i = i0; i <= i1; ++i) |
| 851 | grid.set(i, j, grid.get(i, j) | modificator); |
| 852 | } |
715 | | // TODO: this is all hopelessly inefficient |
716 | | // What we should perhaps do is have some kind of quadtree storing Shapes so it's |
717 | | // quick to invalidate and update small numbers of tiles |
| 854 | static bool TileIsUnderShape(u16 i, u16 j, StaticShape const & shape, Grid<u8>& grid, entity_pos_t const & expand) |
| 855 | { |
| 856 | CFixedVector2D center(shape.x, shape.z); |
| 857 | CFixedVector2D halfSize(shape.hw + expand, shape.hh + expand); |
| 858 | CFixedVector2D halfBound = Geometry::GetHalfBoundingBox(shape.u, shape.v, halfSize); |
719 | | grid.reset(); |
| 860 | u16 i0, j0, i1, j1; |
| 861 | NearestTile(center.X - halfBound.X, center.Y - halfBound.Y, i0, j0, grid.m_W, grid.m_H); |
| 862 | NearestTile(center.X + halfBound.X, center.Y + halfBound.Y, i1, j1, grid.m_W, grid.m_H); |
| 863 | if (j >= j0 && j <= j1 && i >= i0 && i <= i1) { |
| 864 | entity_pos_t x, z; |
| 865 | TileCenter(i, j, x, z); |
| 866 | if (Geometry::PointIsInSquare(CFixedVector2D(x, z) - center, shape.u, shape.v, halfSize)) |
| 867 | return true; |
| 868 | } |
| 869 | return false; |
| 870 | } |
| 871 | static bool TileIsUnderShape(u16 i, u16 j, UnitShape const & shape, Grid<u8>& grid, entity_pos_t const & expand) |
| 872 | { |
| 873 | CFixedVector2D center(shape.x, shape.z); |
| 874 | entity_pos_t r = shape.r + expand; |
721 | | // For tile-based pathfinding: |
722 | | // Since we only count tiles whose centers are inside the square, |
723 | | // we maybe want to expand the square a bit so we're less likely to think there's |
724 | | // free space between buildings when there isn't. But this is just a random guess |
725 | | // and needs to be tweaked until everything works nicely. |
726 | | //entity_pos_t expandPathfinding = entity_pos_t::FromInt(CELL_SIZE / 2); |
727 | | // Actually that's bad because units get stuck when the A* pathfinder thinks they're |
728 | | // blocked on all sides, so it's better to underestimate |
729 | | entity_pos_t expandPathfinding = entity_pos_t::FromInt(0); |
| 876 | u16 i0, j0, i1, j1; |
| 877 | NearestTile(center.X - r, center.Y - r, i0, j0, grid.m_W, grid.m_H); |
| 878 | NearestTile(center.X + r, center.Y + r, i1, j1, grid.m_W, grid.m_H); |
| 879 | return (j >= j0 && j <= j1 && i >= i0 && i <= i1); |
| 880 | } |
731 | | // For AI building foundation planning, we want to definitely block all |
732 | | // potentially-obstructed tiles (so we don't blindly build on top of an obstruction), |
733 | | // so we need to expand by at least 1/sqrt(2) of a tile |
734 | | entity_pos_t expandFoundation = (entity_pos_t::FromInt(CELL_SIZE) * 3) / 4; |
| 882 | void CCmpObstructionManager::RegisterShapeTiles(StaticShape const & shape, u32 id) |
| 883 | { |
| 884 | if (shape.flags & (FLAG_BLOCK_PATHFINDING | FLAG_BLOCK_FOUNDATION)) |
| 885 | { |
| 886 | entity_pos_t expand = std::max(m_ExpandPathfinding, m_ExpandFoundation); |
| 887 | CFixedVector2D center(shape.x, shape.z); |
| 888 | CFixedVector2D halfSize(shape.hw + expand, shape.hh + expand); |
| 889 | CFixedVector2D halfBound = Geometry::GetHalfBoundingBox(shape.u, shape.v, halfSize); |
736 | | for (std::map<u32, StaticShape>::iterator it = m_StaticShapes.begin(); it != m_StaticShapes.end(); ++it) |
| 891 | u16 i0 = ((center.X - halfBound.X) / (int)CELL_SIZE).ToInt_RoundToZero(); |
| 892 | u16 j0 = ((center.Y - halfBound.Y) / (int)CELL_SIZE).ToInt_RoundToZero(); |
| 893 | u16 i1 = ((center.X + halfBound.X) / (int)CELL_SIZE).ToInt_RoundToZero(); |
| 894 | u16 j1 = ((center.Y + halfBound.Y) / (int)CELL_SIZE).ToInt_RoundToZero(); |
| 895 | for (u16 j = j0; j <= j1; ++j) |
| 896 | { |
| 897 | for (u16 i = i0; i <= i1; ++i) |
| 898 | { |
| 899 | entity_pos_t x, z; |
| 900 | TileCenter(i, j, x, z); |
| 901 | if (Geometry::PointIsInSquare(CFixedVector2D(x, z) - center, shape.u, shape.v, halfSize)) |
| 902 | { |
| 903 | u32 key = (i << 16) + j; |
| 904 | m_StaticShapesInfluence.insert(std::make_pair(key, id)); |
| 905 | } |
| 906 | } |
| 907 | } |
| 908 | } |
| 909 | } |
| 910 | |
| 911 | void CCmpObstructionManager::RegisterShapeTiles(UnitShape const & shape, u32 id) |
| 912 | { |
| 913 | if (shape.flags & (FLAG_BLOCK_PATHFINDING | FLAG_BLOCK_FOUNDATION)) |
745 | | u16 i0, j0, i1, j1; |
746 | | NearestTile(center.X - halfBound.X, center.Y - halfBound.Y, i0, j0, grid.m_W, grid.m_H); |
747 | | NearestTile(center.X + halfBound.X, center.Y + halfBound.Y, i1, j1, grid.m_W, grid.m_H); |
748 | | for (u16 j = j0; j <= j1; ++j) |
| 934 | void CCmpObstructionManager::UnregisterShapeTiles(StaticShape const & shape, u32 id) |
| 935 | { |
| 936 | if (shape.flags & (FLAG_BLOCK_PATHFINDING | FLAG_BLOCK_FOUNDATION)) |
| 937 | { |
| 938 | entity_pos_t expand = std::max(m_ExpandPathfinding, m_ExpandFoundation); |
| 939 | CFixedVector2D center(shape.x, shape.z); |
| 940 | CFixedVector2D halfSize(shape.hw + expand, shape.hh + expand); |
| 941 | CFixedVector2D halfBound = Geometry::GetHalfBoundingBox(shape.u, shape.v, halfSize); |
| 942 | |
| 943 | u16 i0 = ((center.X - halfBound.X) / (int)CELL_SIZE).ToInt_RoundToZero(); |
| 944 | u16 j0 = ((center.Y - halfBound.Y) / (int)CELL_SIZE).ToInt_RoundToZero(); |
| 945 | u16 i1 = ((center.X + halfBound.X) / (int)CELL_SIZE).ToInt_RoundToZero(); |
| 946 | u16 j1 = ((center.Y + halfBound.Y) / (int)CELL_SIZE).ToInt_RoundToZero(); |
| 947 | for (u16 j = j0; j <= j1; ++j) |
| 948 | { |
| 949 | for (u16 i = i0; i <= i1; ++i) |
752 | | entity_pos_t x, z; |
753 | | TileCenter(i, j, x, z); |
754 | | if (Geometry::PointIsInSquare(CFixedVector2D(x, z) - center, it->second.u, it->second.v, halfSize)) |
755 | | grid.set(i, j, grid.get(i, j) | TILE_OBSTRUCTED_PATHFINDING); |
| 955 | typedef std::multimap<u32, u32>::iterator iterator_t; |
| 956 | u32 key = (i << 16) + j; |
| 957 | std::pair<iterator_t, iterator_t> range = m_StaticShapesInfluence.equal_range(key); |
| 958 | while (range.first != range.second) |
| 959 | { |
| 960 | if (range.first->second == id) { |
| 961 | iterator_t to_erase = range.first; |
| 962 | ++range.first; |
| 963 | m_StaticShapesInfluence.erase(to_erase); |
| 964 | }else { |
| 965 | ++range.first; |
| 966 | } |
| 967 | } |
760 | | if (it->second.flags & FLAG_BLOCK_FOUNDATION) |
| 974 | void CCmpObstructionManager::UnregisterShapeTiles(UnitShape const & shape, u32 id) |
| 975 | { |
| 976 | if (shape.flags & (FLAG_BLOCK_PATHFINDING | FLAG_BLOCK_FOUNDATION)) |
| 977 | { |
| 978 | entity_pos_t expand = std::max(m_ExpandPathfinding, m_ExpandFoundation); |
| 979 | CFixedVector2D center(shape.x, shape.z); |
| 980 | entity_pos_t r = shape.r + expand; |
| 981 | |
| 982 | u16 i0 = ((center.X - r) / (int)CELL_SIZE).ToInt_RoundToZero(); |
| 983 | u16 j0 = ((center.Y - r) / (int)CELL_SIZE).ToInt_RoundToZero(); |
| 984 | u16 i1 = ((center.X + r) / (int)CELL_SIZE).ToInt_RoundToZero(); |
| 985 | u16 j1 = ((center.Y + r) / (int)CELL_SIZE).ToInt_RoundToZero(); |
| 986 | for (u16 j = j0; j <= j1; ++j) |
762 | | CFixedVector2D halfSize(it->second.hw + expandFoundation, it->second.hh + expandFoundation); |
763 | | CFixedVector2D halfBound = Geometry::GetHalfBoundingBox(it->second.u, it->second.v, halfSize); |
764 | | |
765 | | u16 i0, j0, i1, j1; |
766 | | NearestTile(center.X - halfBound.X, center.Y - halfBound.Y, i0, j0, grid.m_W, grid.m_H); |
767 | | NearestTile(center.X + halfBound.X, center.Y + halfBound.Y, i1, j1, grid.m_W, grid.m_H); |
768 | | for (u16 j = j0; j <= j1; ++j) |
| 988 | for (u16 i = i0; i <= i1; ++i) |
781 | | for (std::map<u32, UnitShape>::iterator it = m_UnitShapes.begin(); it != m_UnitShapes.end(); ++it) |
| 1008 | void CCmpObstructionManager::UpdateTile(u16 i, u16 j, Grid<uint8_t>& grid) |
| 1009 | { |
| 1010 | typedef std::multimap<u32, u32>::const_iterator iterator_t; |
| 1011 | u8 tile = 0; |
| 1012 | u8 full = (TILE_OBSTRUCTED_PATHFINDING | TILE_OBSTRUCTED_FOUNDATION); |
| 1013 | |
| 1014 | u32 key = (i << 16) + j; |
| 1015 | std::pair<iterator_t, iterator_t> range = m_StaticShapesInfluence.equal_range(key); |
| 1016 | while (range.first != range.second) |
783 | | CFixedVector2D center(it->second.x, it->second.z); |
| 1018 | StaticShape const & shape = m_StaticShapes[range.first->second]; |
| 1019 | if (shape.flags & FLAG_BLOCK_PATHFINDING) |
| 1020 | if (TileIsUnderShape(i, j, shape, grid, m_ExpandPathfinding)) |
| 1021 | tile |= TILE_OBSTRUCTED_PATHFINDING; |
| 1022 | if (shape.flags & FLAG_BLOCK_FOUNDATION) |
| 1023 | if (TileIsUnderShape(i, j, shape, grid, m_ExpandFoundation)) |
| 1024 | tile |= TILE_OBSTRUCTED_FOUNDATION; |
785 | | if (it->second.flags & FLAG_BLOCK_PATHFINDING) |
| 1026 | if (tile == full) |
| 1027 | break; |
| 1028 | |
| 1029 | ++range.first; |
| 1030 | } |
| 1031 | |
| 1032 | range = m_UnitShapesInfluence.equal_range(key); |
| 1033 | while (tile != full && range.first != range.second) |
| 1034 | { |
| 1035 | UnitShape const & shape = m_UnitShapes[range.first->second]; |
| 1036 | if (shape.flags & FLAG_BLOCK_PATHFINDING) |
| 1037 | if (TileIsUnderShape(i, j, shape, grid, m_ExpandPathfinding)) |
| 1038 | tile |= TILE_OBSTRUCTED_PATHFINDING; |
| 1039 | if (shape.flags & FLAG_BLOCK_FOUNDATION) |
| 1040 | if (TileIsUnderShape(i, j, shape, grid, m_ExpandFoundation)) |
| 1041 | tile |= TILE_OBSTRUCTED_FOUNDATION; |
| 1042 | |
| 1043 | ++range.first; |
| 1044 | } |
| 1045 | |
| 1046 | grid.set(i, j, tile); |
| 1047 | } |
| 1048 | |
| 1049 | void CCmpObstructionManager::UpdateTilesUnderShape(StaticShape const & shape, Grid<u8>& grid, entity_pos_t const & expand) |
| 1050 | { |
| 1051 | CFixedVector2D center(shape.x, shape.z); |
| 1052 | CFixedVector2D halfSize(shape.hw + expand, shape.hh + expand); |
| 1053 | CFixedVector2D halfBound = Geometry::GetHalfBoundingBox(shape.u, shape.v, halfSize); |
| 1054 | |
| 1055 | u16 i0, j0, i1, j1; |
| 1056 | NearestTile(center.X - halfBound.X, center.Y - halfBound.Y, i0, j0, grid.m_W, grid.m_H); |
| 1057 | NearestTile(center.X + halfBound.X, center.Y + halfBound.Y, i1, j1, grid.m_W, grid.m_H); |
| 1058 | for (u16 j = j0; j <= j1; ++j) |
| 1059 | { |
| 1060 | for (u16 i = i0; i <= i1; ++i) |
787 | | entity_pos_t r = it->second.r + expandPathfinding; |
| 1062 | entity_pos_t x, z; |
| 1063 | TileCenter(i, j, x, z); |
| 1064 | if (Geometry::PointIsInSquare(CFixedVector2D(x, z) - center, shape.u, shape.v, halfSize)) |
| 1065 | UpdateTile(i, j, grid); |
| 1066 | } |
| 1067 | } |
| 1068 | } |
| 1069 | void CCmpObstructionManager::UpdateTilesUnderShape(UnitShape const & shape, Grid<u8>& grid, entity_pos_t const & expand) |
| 1070 | { |
| 1071 | CFixedVector2D center(shape.x, shape.z); |
| 1072 | entity_pos_t r = shape.r + expand; |
789 | | u16 i0, j0, i1, j1; |
790 | | NearestTile(center.X - r, center.Y - r, i0, j0, grid.m_W, grid.m_H); |
791 | | NearestTile(center.X + r, center.Y + r, i1, j1, grid.m_W, grid.m_H); |
792 | | for (u16 j = j0; j <= j1; ++j) |
793 | | for (u16 i = i0; i <= i1; ++i) |
794 | | grid.set(i, j, grid.get(i, j) | TILE_OBSTRUCTED_PATHFINDING); |
| 1074 | u16 i0, j0, i1, j1; |
| 1075 | NearestTile(center.X - r, center.Y - r, i0, j0, grid.m_W, grid.m_H); |
| 1076 | NearestTile(center.X + r, center.Y + r, i1, j1, grid.m_W, grid.m_H); |
| 1077 | for (u16 j = j0; j <= j1; ++j) |
| 1078 | for (u16 i = i0; i <= i1; ++i) |
| 1079 | UpdateTile(i, j, grid); |
| 1080 | } |
| 1081 | |
| 1082 | bool CCmpObstructionManager::Rasterise(Grid<u8>& grid) |
| 1083 | { |
| 1084 | if (!IsDirty(grid)) |
| 1085 | return false; |
| 1086 | |
| 1087 | PROFILE("Rasterise"); |
| 1088 | TIMER_ACCRUE(tc_Rasterize); |
| 1089 | |
| 1090 | if (!m_StaticHistory.isInHistory(grid.m_DirtyID) || !m_UnitHistory.isInHistory(grid.m_DirtyID)) |
| 1091 | { |
| 1092 | // Some pieces of history are missing, we need to recompute the entire grid |
| 1093 | grid.reset(); |
| 1094 | for (std::map<u32, StaticShape>::iterator it = m_StaticShapes.begin(); it != m_StaticShapes.end(); ++it) |
| 1095 | { |
| 1096 | if (it->second.flags & FLAG_BLOCK_PATHFINDING) |
| 1097 | ModifyTilesUnderShape(it->second, grid, m_ExpandPathfinding, TILE_OBSTRUCTED_PATHFINDING); |
| 1098 | |
| 1099 | if (it->second.flags & FLAG_BLOCK_FOUNDATION) |
| 1100 | ModifyTilesUnderShape(it->second, grid, m_ExpandFoundation, TILE_OBSTRUCTED_FOUNDATION); |
801 | | u16 i0, j0, i1, j1; |
802 | | NearestTile(center.X - r, center.Y - r, i0, j0, grid.m_W, grid.m_H); |
803 | | NearestTile(center.X + r, center.Y + r, i1, j1, grid.m_W, grid.m_H); |
804 | | for (u16 j = j0; j <= j1; ++j) |
805 | | for (u16 i = i0; i <= i1; ++i) |
806 | | grid.set(i, j, grid.get(i, j) | TILE_OBSTRUCTED_FOUNDATION); |
| 1108 | if (it->second.flags & FLAG_BLOCK_FOUNDATION) |
| 1109 | ModifyTilesUnderShape(it->second, grid, m_ExpandFoundation, TILE_OBSTRUCTED_FOUNDATION); |
| 1116 | entity_pos_t maxExpand = std::max(m_ExpandPathfinding, m_ExpandFoundation); |
| 1117 | |
| 1118 | // Compute tiles under modified static shapes |
| 1119 | for (ShapeHistory<StaticShape>::const_iterator it = m_StaticHistory.get(grid.m_DirtyID); it != m_StaticHistory.end(); ++it) |
| 1120 | { |
| 1121 | if (it->second.flags & (FLAG_BLOCK_PATHFINDING | FLAG_BLOCK_FOUNDATION)) |
| 1122 | UpdateTilesUnderShape(it->second, grid, maxExpand); |
| 1123 | } |
| 1124 | |
| 1125 | // Reset tiles under removed unit shapes |
| 1126 | for (ShapeHistory<UnitShape>::const_iterator it = m_UnitHistory.get(grid.m_DirtyID); it != m_UnitHistory.end(); ++it) |
| 1127 | { |
| 1128 | if (it->second.flags & (FLAG_BLOCK_PATHFINDING | FLAG_BLOCK_FOUNDATION)) |
| 1129 | UpdateTilesUnderShape(it->second, grid, maxExpand); |
| 1130 | } |
| 1131 | } |
| 1132 | |
| 1133 | grid.m_DirtyID = m_DirtyID; |
| 1134 | |