* Try to connect two tiles (packedDst and data->packed) via a simplistic algorithm. * @param packedDst The tile to try to get to. * @param data Information about the found route, and the start point. * @param searchDirection The search direction (1 for clockwise, -1 for counterclockwise). * @param directionStart The direction to start looking at. * @return True if a route was found. */
| 1115 | * @return True if a route was found. |
| 1116 | */ |
| 1117 | static bool Script_Unit_Pathfinder_Connect(uint16 packedDst, Pathfinder_Data *data, int8 searchDirection, uint8 directionStart) |
| 1118 | { |
| 1119 | uint16 packedNext; |
| 1120 | uint16 packedCur; |
| 1121 | uint8 *buffer; |
| 1122 | uint16 bufferSize; |
| 1123 | |
| 1124 | packedCur = data->packed; |
| 1125 | buffer = data->buffer; |
| 1126 | bufferSize = 0; |
| 1127 | |
| 1128 | while (bufferSize < 100) { |
| 1129 | uint8 direction = directionStart; |
| 1130 | |
| 1131 | while (true) { |
| 1132 | /* Look around us, first in the start direction, for a valid tile */ |
| 1133 | direction = (direction + searchDirection) & 0x7; |
| 1134 | |
| 1135 | /* In case we are directly looking at our destination tile, we are pretty much done */ |
| 1136 | if ((direction & 0x1) != 0 && (packedCur + s_mapDirection[(direction + searchDirection) & 0x7]) == packedDst) { |
| 1137 | direction = (direction + searchDirection) & 0x7; |
| 1138 | packedNext = packedCur + s_mapDirection[direction]; |
| 1139 | break; |
| 1140 | } else { |
| 1141 | /* If we are back to our start direction, we didn't find a route */ |
| 1142 | if (direction == directionStart) return false; |
| 1143 | |
| 1144 | /* See if the tile next to us is a valid position */ |
| 1145 | packedNext = packedCur + s_mapDirection[direction]; |
| 1146 | if (Script_Unit_Pathfind_GetScore(packedNext, direction) <= 255) break; |
| 1147 | } |
| 1148 | } |
| 1149 | |
| 1150 | *buffer++ = direction; |
| 1151 | bufferSize++; |
| 1152 | |
| 1153 | /* If we found the destination, smooth the route and we are done */ |
| 1154 | if (packedNext == packedDst) { |
| 1155 | *buffer = 0xFF; |
| 1156 | data->routeSize = bufferSize; |
| 1157 | Script_Unit_Pathfinder_Smoothen(data); |
| 1158 | data->routeSize--; |
| 1159 | return true; |
| 1160 | } |
| 1161 | |
| 1162 | /* If we return to our start tile, we didn't find a route */ |
| 1163 | if (data->packed == packedNext) return false; |
| 1164 | |
| 1165 | /* Now look at the next tile, starting 3 directions back */ |
| 1166 | directionStart = (direction - searchDirection * 3) & 0x7; |
| 1167 | packedCur = packedNext; |
| 1168 | } |
| 1169 | |
| 1170 | /* We ran out of search space and didn't find a route */ |
| 1171 | return false; |
| 1172 | } |
| 1173 | |
| 1174 | /** |
no test coverage detected