From 83dbb5297684a65396ec9a2dc46a9ae40b4007ee Mon Sep 17 00:00:00 2001 From: Broxzier Date: Wed, 25 Jan 2017 13:42:04 +0100 Subject: [PATCH] Sorting tile --- src/openrct2/windows/tile_inspector.c | 31 ++++++------------ src/openrct2/world/map.c | 5 +++ src/openrct2/world/tile_inspector.c | 47 +++++++++++++++++++++++++++ src/openrct2/world/tile_inspector.h | 2 ++ 4 files changed, 64 insertions(+), 21 deletions(-) diff --git a/src/openrct2/windows/tile_inspector.c b/src/openrct2/windows/tile_inspector.c index fb0884e2d8..3709262d06 100644 --- a/src/openrct2/windows/tile_inspector.c +++ b/src/openrct2/windows/tile_inspector.c @@ -666,22 +666,16 @@ static void window_tile_inspector_swap_elements(sint16 first, sint16 second) static void window_tile_inspector_sort_elements(rct_window *w) { - const rct_map_element *const firstElement = map_get_first_element_at(windowTileInspectorTileX, windowTileInspectorTileY); - - // Bubble sort - for (sint32 loopStart = 1; loopStart < windowTileInspectorElementCount; loopStart++) { - sint32 currentId = loopStart; - const rct_map_element *currentElement = firstElement + currentId; - const rct_map_element *otherElement = currentElement - 1; - - // While current element's base height is lower, or (when their baseheight is the same) the other map element's clearance height is lower... - while (currentId > 0 && (otherElement->base_height > currentElement->base_height || (otherElement->base_height == currentElement->base_height && otherElement->clearance_height > currentElement->clearance_height))) { - window_tile_inspector_swap_elements(currentId - 1, currentId); - currentId--; - currentElement--; - otherElement--; - } - } + assert(windowTileInspectorTileSelected == true); + game_do_command( + TILE_INSPECTOR_ANY_SORT, + GAME_COMMAND_FLAG_APPLY, + windowTileInspectorTileX | (windowTileInspectorTileY << 8), + 0, + GAME_COMMAND_MODIFY_TILE, + 0, + 0 + ); } static void window_tile_inspector_copy_element(rct_window *w) @@ -1001,14 +995,9 @@ static void window_tile_inspector_mouseup(rct_window *w, sint32 widgetIndex) break; case WIDX_BUTTON_SORT: window_tile_inspector_sort_elements(w); - w->selected_list_item = -1; - window_tile_inspector_set_page(w, PAGE_DEFAULT); - window_tile_inspector_auto_set_buttons(w); - window_invalidate(w); break; case WIDX_BUTTON_COPY: window_tile_inspector_copy_element(w); - map_invalidate_tile_full(windowTileInspectorTileX << 5, windowTileInspectorTileY << 5); window_tile_inspector_auto_set_buttons(w); window_invalidate(w); break; diff --git a/src/openrct2/world/map.c b/src/openrct2/world/map.c index 74bae68e61..c7fdddfa4d 100644 --- a/src/openrct2/world/map.c +++ b/src/openrct2/world/map.c @@ -5648,6 +5648,11 @@ void game_command_modify_tile(sint32* eax, sint32* ebx, sint32* ecx, sint32* edx *ebx = tile_inspector_paste_element_at(x, y, element_to_paste, flags); return; } + case TILE_INSPECTOR_ANY_SORT: + { + *ebx = tile_inspector_sort(x, y, flags); + return; + } default: log_error("invalid instruction"); *ebx = MONEY32_UNDEFINED; diff --git a/src/openrct2/world/tile_inspector.c b/src/openrct2/world/tile_inspector.c index f36da86306..1c35dd688d 100644 --- a/src/openrct2/world/tile_inspector.c +++ b/src/openrct2/world/tile_inspector.c @@ -268,3 +268,50 @@ sint32 tile_inspector_paste_element_at(sint32 x, sint32 y, rct_map_element eleme return 0; } + +sint32 tile_inspector_sort(sint32 x, sint32 y, sint32 flags) +{ + if (flags & GAME_COMMAND_FLAG_APPLY) + { + const rct_map_element *const first_element = map_get_first_element_at(x, y); + + // Count elements on tile + sint32 num_element = 0; + const rct_map_element *element_iterator = first_element; + do + { + num_element++; + } while (!map_element_is_last_for_tile(element_iterator++)); + + // Bubble sort + for (sint32 loopStart = 1; loopStart < num_element; loopStart++) + { + sint32 currentId = loopStart; + const rct_map_element *currentElement = first_element + currentId; + const rct_map_element *otherElement = currentElement - 1; + + // While current element's base height is lower, or (when their baseheight is the same) the other map element's clearance height is lower... + while (currentId > 0 && (otherElement->base_height > currentElement->base_height || (otherElement->base_height == currentElement->base_height && otherElement->clearance_height > currentElement->clearance_height))) + { + map_swap_elements_at(x, y, currentId - 1, currentId); + currentId--; + currentElement--; + otherElement--; + } + } + + map_invalidate_tile_full(x << 5, y << 5); + + // Deselect tile for clients who had it selected + rct_window *const tile_inspector_window = window_find_by_class(WC_TILE_INSPECTOR); + if (tile_inspector_window != NULL && (uint32)x == windowTileInspectorTileX && (uint32)y == windowTileInspectorTileY) + { + window_tile_inspector_set_page(tile_inspector_window, PAGE_DEFAULT); + tile_inspector_window->selected_list_item = -1; + window_tile_inspector_auto_set_buttons(tile_inspector_window); + window_invalidate(tile_inspector_window); + } + } + + return 0; +} diff --git a/src/openrct2/world/tile_inspector.h b/src/openrct2/world/tile_inspector.h index aa984c63e7..c065a8e7ca 100644 --- a/src/openrct2/world/tile_inspector.h +++ b/src/openrct2/world/tile_inspector.h @@ -37,6 +37,7 @@ typedef enum { TILE_INSPECTOR_ANY_INSERT_CORRUPT, TILE_INSPECTOR_ANY_ROTATE, TILE_INSPECTOR_ANY_PASTE, + TILE_INSPECTOR_ANY_SORT, } tile_inspector_instruction; sint32 tile_inspector_insert_corrupt_at(sint32 x, sint32 y, sint16 element_index, sint32 flags); @@ -44,3 +45,4 @@ sint32 tile_inspector_remove_element_at(sint32 x, sint32 y, sint16 element_index sint32 tile_inspector_swap_elements(sint32 x, sint32 y, sint16 first, sint16 second, sint32 flags); sint32 tile_inspector_rotate_element_at(sint32 x, sint32 y, sint32 element_index, sint32 flags); sint32 tile_inspector_paste_element_at(sint32 x, sint32 y, rct_map_element element, sint32 flags); +sint32 tile_inspector_sort(sint32 x, sint32 y, sint32 flags);