* Smoothen the route found by the pathfinder. * @param data The found route to smoothen. */
| 1010 | * @param data The found route to smoothen. |
| 1011 | */ |
| 1012 | static void Script_Unit_Pathfinder_Smoothen(Pathfinder_Data *data) |
| 1013 | { |
| 1014 | static const int8 directionOffset[8] = {0, 0, 1, 2, 3, -2, -1, 0}; |
| 1015 | |
| 1016 | uint16 packed; |
| 1017 | uint8 *bufferFrom; |
| 1018 | uint8 *bufferTo; |
| 1019 | |
| 1020 | data->buffer[data->routeSize] = 0xFF; |
| 1021 | packed = data->packed; |
| 1022 | |
| 1023 | if (data->routeSize > 1) { |
| 1024 | bufferTo = data->buffer + 1; |
| 1025 | |
| 1026 | while (*bufferTo != 0xFF) { |
| 1027 | int8 direction; |
| 1028 | uint8 dir; |
| 1029 | |
| 1030 | bufferFrom = bufferTo - 1; |
| 1031 | |
| 1032 | while (*bufferFrom == 0xFE && bufferFrom != data->buffer) bufferFrom--; |
| 1033 | |
| 1034 | if (*bufferFrom == 0xFE) { |
| 1035 | bufferTo++; |
| 1036 | continue; |
| 1037 | } |
| 1038 | |
| 1039 | direction = (*bufferTo - *bufferFrom) & 0x7; |
| 1040 | direction = directionOffset[direction]; |
| 1041 | |
| 1042 | /* The directions are opposite of each other, so they can both be removed */ |
| 1043 | if (direction == 3) { |
| 1044 | *bufferFrom = 0xFE; |
| 1045 | *bufferTo = 0xFE; |
| 1046 | |
| 1047 | bufferTo++; |
| 1048 | continue; |
| 1049 | } |
| 1050 | |
| 1051 | /* The directions are close to each other, so follow */ |
| 1052 | if (direction == 0) { |
| 1053 | packed += s_mapDirection[*bufferFrom]; |
| 1054 | bufferTo++; |
| 1055 | continue; |
| 1056 | } |
| 1057 | |
| 1058 | if ((*bufferFrom & 0x1) != 0) { |
| 1059 | dir = (*bufferFrom + (direction < 0 ? -1 : 1)) & 0x7; |
| 1060 | |
| 1061 | /* If we go 45 degrees with 90 degree difference, we can also go straight */ |
| 1062 | if (abs(direction) == 1) { |
| 1063 | if (Script_Unit_Pathfind_GetScore(packed + s_mapDirection[dir], dir) <= 255) { |
| 1064 | *bufferTo = dir; |
| 1065 | *bufferFrom = dir; |
| 1066 | } |
| 1067 | packed += s_mapDirection[*bufferFrom]; |
| 1068 | bufferTo++; |
| 1069 | continue; |
no test coverage detected