From 8c28dbaaeffc1cf55f7c097daa96e5ea478f0cf0 Mon Sep 17 00:00:00 2001 From: ZedThree Date: Thu, 1 May 2014 09:18:29 +0200 Subject: [PATCH 1/9] Name peep variables for current train and car --- src/news_item.c | 8 +++++--- src/peep.h | 4 ++-- src/ride.h | 2 +- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/news_item.c b/src/news_item.c index 71067660fb..42ba491aee 100644 --- a/src/news_item.c +++ b/src/news_item.c @@ -214,9 +214,11 @@ void news_item_get_subject_location(int type, int subject, int *x, int *y, int * break; } - sprite_2 = &(RCT2_ADDRESS(RCT2_ADDRESS_SPRITE_LIST, rct_sprite)[ride->var_086[sprite->peep.var_6A]]); - for (i = 0; i < sprite->peep.var_6B; i++) - sprite_2 = &(RCT2_ADDRESS(RCT2_ADDRESS_SPRITE_LIST, rct_sprite)[*((uint16*)&sprite_2->pad_00[0x3E])]); + // Find the train peep is on + sprite_2 = &(RCT2_ADDRESS(RCT2_ADDRESS_SPRITE_LIST, rct_sprite)[ride->train_car_map[sprite->peep.current_train]]); + // Find the car peep is on + for (i = 0; i < sprite->peep.current_car; i++) + sprite_2 = &(RCT2_ADDRESS(RCT2_ADDRESS_SPRITE_LIST, rct_sprite)[*((uint16*)&sprite_2->pad_00[0x3e])]); *x = sprite_2->unknown.x; *y = sprite_2->unknown.y; *z = sprite_2->unknown.z; diff --git a/src/peep.h b/src/peep.h index 335cbedf0b..b2574bec2b 100644 --- a/src/peep.h +++ b/src/peep.h @@ -72,8 +72,8 @@ typedef struct { uint8 happiness; // 0x3A uint8 pad_03B[0x2D]; uint8 current_ride; // 0x68 - uint8 var_6A; - uint8 var_6B; + uint8 current_train; // 0x6A + uint8 current_car; // 0x6B uint8 pad_6C[0x30]; uint32 id; // 0x9C uint8 pad_A0[0x10]; diff --git a/src/ride.h b/src/ride.h index 8d7294bfd3..ff90abd0e8 100644 --- a/src/ride.h +++ b/src/ride.h @@ -38,7 +38,7 @@ typedef struct { uint8 pad_052[0x18]; uint16 var_06A[4]; // probably entrance map coordinates uint8 pad_072[0x14]; - uint16 var_086[1]; + uint16 train_car_map[1]; // 0x86 Points to the first car in the train uint8 pad_088[0x68]; sint16 excitement; // 0x0F0 sint16 intensity; // 0x0F2 From d5ff2f27818fc35023fa25ffcce274db906db6f9 Mon Sep 17 00:00:00 2001 From: ZedThree Date: Thu, 1 May 2014 13:16:41 +0200 Subject: [PATCH 2/9] Fix finding peep on ride news items Peep's current_* variables were offset by 1. Also fixed comparison between 32-bit and 16-bit ints. --- src/news_item.c | 6 ++++-- src/peep.h | 8 +++++--- src/window_game_bottom_toolbar.c | 2 +- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/news_item.c b/src/news_item.c index 42ba491aee..f23bdf7408 100644 --- a/src/news_item.c +++ b/src/news_item.c @@ -200,7 +200,7 @@ void news_item_get_subject_location(int type, int subject, int *x, int *y, int * *x = sprite->unknown.x; *y = sprite->unknown.y; *z = sprite->unknown.z; - if (*x != SPRITE_LOCATION_NULL) + if (*((uint16*)x) != SPRITE_LOCATION_NULL) break; if (sprite->peep.state != 3 && sprite->peep.state != 7) { @@ -208,8 +208,10 @@ void news_item_get_subject_location(int type, int subject, int *x, int *y, int * break; } + // Find which ride peep is on ride = &(RCT2_ADDRESS(RCT2_ADDRESS_RIDE_LIST, rct_ride)[sprite->peep.current_ride]); - if (ride->var_1D0 & 1) { + // Check if there are trains on the track (first bit of var_1D0) + if (!(ride->var_1D0 & 1)) { *x = SPRITE_LOCATION_NULL; break; } diff --git a/src/peep.h b/src/peep.h index b2574bec2b..40beea32d5 100644 --- a/src/peep.h +++ b/src/peep.h @@ -72,9 +72,11 @@ typedef struct { uint8 happiness; // 0x3A uint8 pad_03B[0x2D]; uint8 current_ride; // 0x68 - uint8 current_train; // 0x6A - uint8 current_car; // 0x6B - uint8 pad_6C[0x30]; + uint8 pad_6a; // 0x6A Part of current_ride? + uint8 current_train; // 0x6B + uint8 current_car; // 0x6c + uint8 current_seat; // 0x6d + uint8 pad_6C[0x2e]; uint32 id; // 0x9C uint8 pad_A0[0x10]; rct_peep_thought thoughts[PEEP_MAX_THOUGHTS]; // 0xB0 diff --git a/src/window_game_bottom_toolbar.c b/src/window_game_bottom_toolbar.c index d24152414d..721b189ee6 100644 --- a/src/window_game_bottom_toolbar.c +++ b/src/window_game_bottom_toolbar.c @@ -185,7 +185,7 @@ static void window_game_bottom_toolbar_mouseup() news_item_get_subject_location(newsItem->type, subject, &x, &y, &z); - if (x == SPRITE_LOCATION_NULL) + if ((uint16)x == SPRITE_LOCATION_NULL) break; if ((mainWindow = window_get_main()) != NULL) From 6c550cbdd65f799c7ceb1183f7b4e57fe4ac79a1 Mon Sep 17 00:00:00 2001 From: ZedThree Date: Thu, 1 May 2014 13:23:18 +0200 Subject: [PATCH 3/9] Change peep news item enum names --- src/news_item.c | 4 ++-- src/news_item.h | 4 ++-- src/window_game_bottom_toolbar.c | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/news_item.c b/src/news_item.c index f23bdf7408..bbb0be3386 100644 --- a/src/news_item.c +++ b/src/news_item.c @@ -195,7 +195,7 @@ void news_item_get_subject_location(int type, int subject, int *x, int *y, int * *z = edx; } break; - case NEWS_ITEM_PEEP_1: + case NEWS_ITEM_PEEP_ON_RIDE: sprite = &(RCT2_ADDRESS(RCT2_ADDRESS_SPRITE_LIST, rct_sprite)[subject]); *x = sprite->unknown.x; *y = sprite->unknown.y; @@ -225,7 +225,7 @@ void news_item_get_subject_location(int type, int subject, int *x, int *y, int * *y = sprite_2->unknown.y; *z = sprite_2->unknown.z; break; - case NEWS_ITEM_PEEP_2: + case NEWS_ITEM_PEEP: sprite = &(RCT2_ADDRESS(RCT2_ADDRESS_SPRITE_LIST, rct_sprite)[subject]); *x = sprite->unknown.x; *y = sprite->unknown.y; diff --git a/src/news_item.h b/src/news_item.h index 547b85e293..be8dd55e42 100644 --- a/src/news_item.h +++ b/src/news_item.h @@ -26,8 +26,8 @@ enum { NEWS_ITEM_NULL, NEWS_ITEM_RIDE, - NEWS_ITEM_PEEP_1, - NEWS_ITEM_PEEP_2, + NEWS_ITEM_PEEP_ON_RIDE, + NEWS_ITEM_PEEP, NEWS_ITEM_MONEY, NEWS_ITEM_BLANK, NEWS_ITEM_SCENERY, diff --git a/src/window_game_bottom_toolbar.c b/src/window_game_bottom_toolbar.c index 721b189ee6..2df493d1da 100644 --- a/src/window_game_bottom_toolbar.c +++ b/src/window_game_bottom_toolbar.c @@ -533,8 +533,8 @@ static void window_game_bottom_toolbar_draw_news_item(rct_drawpixelinfo *dpi, rc case NEWS_ITEM_RIDE: gfx_draw_sprite(dpi, SPR_RIDE, x, y); break; - case NEWS_ITEM_PEEP_1: - case NEWS_ITEM_PEEP_2: + case NEWS_ITEM_PEEP_ON_RIDE: + case NEWS_ITEM_PEEP: if (newsItem->flags & 1) break; From 63df868422c9744fd6d4536316712051a3513b5e Mon Sep 17 00:00:00 2001 From: ZedThree Date: Thu, 1 May 2014 14:02:28 +0200 Subject: [PATCH 4/9] Add rct_car sprite and tidy up news locator Also add (x,y,z) coordinate variables for rct_peep --- src/news_item.c | 35 ++++++++++++++++++----------------- src/peep.h | 5 ++++- src/sprite.h | 11 +++++++++++ 3 files changed, 33 insertions(+), 18 deletions(-) diff --git a/src/news_item.c b/src/news_item.c index bbb0be3386..0ddf9aa137 100644 --- a/src/news_item.c +++ b/src/news_item.c @@ -174,7 +174,8 @@ void news_item_get_subject_location(int type, int subject, int *x, int *y, int * { int i; rct_ride *ride; - rct_sprite *sprite, *sprite_2; + rct_peep *peep; + rct_car *car; switch (type) { case NEWS_ITEM_RIDE: @@ -196,20 +197,20 @@ void news_item_get_subject_location(int type, int subject, int *x, int *y, int * } break; case NEWS_ITEM_PEEP_ON_RIDE: - sprite = &(RCT2_ADDRESS(RCT2_ADDRESS_SPRITE_LIST, rct_sprite)[subject]); - *x = sprite->unknown.x; - *y = sprite->unknown.y; - *z = sprite->unknown.z; + peep = &(RCT2_ADDRESS(RCT2_ADDRESS_SPRITE_LIST, rct_sprite)[subject]); + *x = peep->x; + *y = peep->y; + *z = peep->z; if (*((uint16*)x) != SPRITE_LOCATION_NULL) break; - if (sprite->peep.state != 3 && sprite->peep.state != 7) { + if (peep->state != 3 && peep->state != 7) { *x = SPRITE_LOCATION_NULL; break; } // Find which ride peep is on - ride = &(RCT2_ADDRESS(RCT2_ADDRESS_RIDE_LIST, rct_ride)[sprite->peep.current_ride]); + ride = &(RCT2_ADDRESS(RCT2_ADDRESS_RIDE_LIST, rct_ride)[peep->current_ride]); // Check if there are trains on the track (first bit of var_1D0) if (!(ride->var_1D0 & 1)) { *x = SPRITE_LOCATION_NULL; @@ -217,19 +218,19 @@ void news_item_get_subject_location(int type, int subject, int *x, int *y, int * } // Find the train peep is on - sprite_2 = &(RCT2_ADDRESS(RCT2_ADDRESS_SPRITE_LIST, rct_sprite)[ride->train_car_map[sprite->peep.current_train]]); + car = &(RCT2_ADDRESS(RCT2_ADDRESS_SPRITE_LIST, rct_sprite)[ride->train_car_map[peep->current_train]]); // Find the car peep is on - for (i = 0; i < sprite->peep.current_car; i++) - sprite_2 = &(RCT2_ADDRESS(RCT2_ADDRESS_SPRITE_LIST, rct_sprite)[*((uint16*)&sprite_2->pad_00[0x3e])]); - *x = sprite_2->unknown.x; - *y = sprite_2->unknown.y; - *z = sprite_2->unknown.z; + for (i = 0; i < peep->current_car; i++) + car = &(RCT2_ADDRESS(RCT2_ADDRESS_SPRITE_LIST, rct_sprite)[car->next_car]); + *x = car->x; + *y = car->y; + *z = car->z; break; case NEWS_ITEM_PEEP: - sprite = &(RCT2_ADDRESS(RCT2_ADDRESS_SPRITE_LIST, rct_sprite)[subject]); - *x = sprite->unknown.x; - *y = sprite->unknown.y; - *z = sprite->unknown.z; + peep = &(RCT2_ADDRESS(RCT2_ADDRESS_SPRITE_LIST, rct_sprite)[subject]); + *x = peep->x; + *y = peep->y; + *z = peep->z; break; case NEWS_ITEM_BLANK: { diff --git a/src/peep.h b/src/peep.h index 40beea32d5..2e071d3c1e 100644 --- a/src/peep.h +++ b/src/peep.h @@ -55,7 +55,10 @@ typedef struct { uint8 pad_09; sint16 var_0A; uint16 var_0C; - uint8 var_0E[0x14]; + sint16 x; // 0x0E + sint16 y; // 0x10 + sint16 z; // 0x12 + uint8 pad_14[0x0E]; uint16 name_string_idx; // 0x22 uint16 next_x; // 0x24 uint16 next_y; // 0x26 diff --git a/src/sprite.h b/src/sprite.h index eea99b11b3..57efead6cb 100644 --- a/src/sprite.h +++ b/src/sprite.h @@ -46,6 +46,16 @@ typedef struct { sint32 var_24; } rct_litter; +typedef struct { + uint8 sprite_idetifier; // 0x00 + uint8 pad_01[0x0D]; + sint16 x; // 0x0E + sint16 y; // 0x10 + sint16 z; // 0x12 + uint8 pad_14[0x2a]; + uint16 next_car; // 0x3E +} rct_car; + /** * Sprite structure. * size: 0x0100 @@ -55,6 +65,7 @@ typedef union { rct_unk_sprite unknown; rct_peep peep; rct_litter litter; + rct_car car; } rct_sprite; #endif From a5c85f0cbecfd8c4a14baaef91a086fb2ec5490e Mon Sep 17 00:00:00 2001 From: ZedThree Date: Thu, 1 May 2014 14:12:30 +0200 Subject: [PATCH 5/9] Fix comments --- src/news_item.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/news_item.c b/src/news_item.c index 0ddf9aa137..f09f7f19c8 100644 --- a/src/news_item.c +++ b/src/news_item.c @@ -217,9 +217,9 @@ void news_item_get_subject_location(int type, int subject, int *x, int *y, int * break; } - // Find the train peep is on + // Find the first car of the train peep is on car = &(RCT2_ADDRESS(RCT2_ADDRESS_SPRITE_LIST, rct_sprite)[ride->train_car_map[peep->current_train]]); - // Find the car peep is on + // Find the actual car peep is on for (i = 0; i < peep->current_car; i++) car = &(RCT2_ADDRESS(RCT2_ADDRESS_SPRITE_LIST, rct_sprite)[car->next_car]); *x = car->x; From ae637f62913ef9d8491a52730fc25a92c7579d8f Mon Sep 17 00:00:00 2001 From: IntelOrca Date: Thu, 1 May 2014 14:13:31 +0100 Subject: [PATCH 6/9] fix case and add author to source code header --- src/peep.h | 4 ++-- src/ride.h | 2 +- src/sprite.h | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/peep.h b/src/peep.h index 2e071d3c1e..62f8b75602 100644 --- a/src/peep.h +++ b/src/peep.h @@ -1,5 +1,5 @@ /***************************************************************************** - * Copyright (c) 2014 Ted John + * Copyright (c) 2014 Ted John, Peter Hill * OpenRCT2, an open source clone of Roller Coaster Tycoon 2. * * This file is part of OpenRCT2. @@ -77,7 +77,7 @@ typedef struct { uint8 current_ride; // 0x68 uint8 pad_6a; // 0x6A Part of current_ride? uint8 current_train; // 0x6B - uint8 current_car; // 0x6c + uint8 current_car; // 0x6C uint8 current_seat; // 0x6d uint8 pad_6C[0x2e]; uint32 id; // 0x9C diff --git a/src/ride.h b/src/ride.h index ff90abd0e8..d7a251dbeb 100644 --- a/src/ride.h +++ b/src/ride.h @@ -1,5 +1,5 @@ /***************************************************************************** - * Copyright (c) 2014 Ted John + * Copyright (c) 2014 Ted John, Peter Hill * OpenRCT2, an open source clone of Roller Coaster Tycoon 2. * * This file is part of OpenRCT2. diff --git a/src/sprite.h b/src/sprite.h index 57efead6cb..5a3a071ba0 100644 --- a/src/sprite.h +++ b/src/sprite.h @@ -1,5 +1,5 @@ /***************************************************************************** - * Copyright (c) 2014 Ted John + * Copyright (c) 2014 Ted John, Peter Hill * OpenRCT2, an open source clone of Roller Coaster Tycoon 2. * * This file is part of OpenRCT2. From 297043fecd439c385becc579aba4dca1459c6415 Mon Sep 17 00:00:00 2001 From: ZedThree Date: Fri, 2 May 2014 21:25:42 +0200 Subject: [PATCH 7/9] Add map_element_height (finishes sub_662783) Possibly wrong for some slopes... --- src/map.c | 119 ++++++++++++++++++++++++++++++++++++++++++++---- src/map.h | 1 + src/news_item.c | 28 +++--------- src/news_item.h | 1 + src/title.c | 12 ++--- 5 files changed, 122 insertions(+), 39 deletions(-) diff --git a/src/map.c b/src/map.c index 1e2dc405bd..57a305981b 100644 --- a/src/map.c +++ b/src/map.c @@ -123,34 +123,135 @@ void map_update_tile_pointers() } /** - * + * Return the absolute height of an element, given its (x,y) coordinates + * * rct2: 0x00662783 * UNFINISHED */ -int sub_662783(int x, int y) +int map_element_height(int x, int y) { int i; rct_map_element *mapElement; + // Off the map if (x >= 8192 || y >= 8192) return 16; - x &= 0xFFFFFFE0; - y &= 0xFFFFFFE0; + // Find the tile the element is on + int x_tile = x & 0xFFFFFFE0; + int y_tile = y & 0xFFFFFFE0; - i = ((y * 256) + x) / 8; + i = ((y_tile * 256) + x_tile) / 32; mapElement = TILE_MAP_ELEMENT_POINTER(i); while (mapElement->type & MAP_ELEMENT_TYPE_MASK) { mapElement++; } - uint32 result = + uint32 height = ((mapElement->properties.surface.terrain & MAP_ELEMENT_WATER_HEIGHT_MASK) << 20) | (mapElement->base_height << 3); - uint32 ebx = (mapElement->properties.surface.slope & MAP_ELEMENT_SLOPE_MASK) & ~0x16; - // slope logic + uint32 slope = (mapElement->properties.surface.slope & MAP_ELEMENT_SLOPE_MASK); + uint8 extra_height = (slope & 0x10) >> 4; // 0x10 is the 5th bit - sets slope to double height + slope &= 0xF; - return result; + uint8 quad, quad_2; // which quadrant the element is in? + + uint8 xl, yl; + + uint8 TILE_SIZE = 31; + + xl = x & 0x1f; + yl = y & 0x1f; + + // slope logic + // slope == 0 is flat, and slope == 15 is not used + + // One corner up + if ((slope == 1) || (slope == 2) || (slope == 4) || (slope == 8)) { + switch(slope) { + case 1: // NE corner up + quad = xl + yl - TILE_SIZE; + break; + case 2: // SE corner up + quad = xl - yl; + break; + case 4: // SW corner up + quad = TILE_SIZE - yl - xl; + break; + case 8: // NW corner up + quad = xl - yl; + break; + } + if (quad > 0) { + height += quad / 2; + } + } + + // One side up + switch(slope) { + case 3: // E side up + height += xl / 2; + break; + case 6: // S side up + height += (TILE_SIZE - yl) / 2; + break; + case 9: // N side up + height += yl / 2; + height++; + break; + case 12: // W side up + height += (TILE_SIZE - xl) / 2; + break; + } + + // One corner down + if ((slope == 7) || (slope == 11) || (slope == 13) || (slope == 14)) { + switch(slope) { + case 7: // NW corner down + quad = xl + TILE_SIZE - yl; + quad_2 = xl - yl; + break; + case 11: // SW corner down + quad = xl + yl; + quad_2 = xl + yl - TILE_SIZE; + break; + case 13: // SE corner down + quad = TILE_SIZE - xl + yl; + quad_2 = xl - yl; + break; + case 14: // NE corner down + quad = (TILE_SIZE - xl) + (TILE_SIZE - yl); + quad_2 = TILE_SIZE - yl - xl; + break; + } + height += 0x10; + if (extra_height) { + height += quad / 2; + height++; + } + if (quad_2 < 0) { + height += quad_2 / 2; + height += 0xFF00; + } + } + + // Valleys + if (slope == 5) { // NW-SE valley + quad = xl + yl; + if (quad > TILE_SIZE + 1) { + quad = TILE_SIZE - xl - yl; + if (quad > 0) { + height += quad / 2; + } + } + } else if (slope == 10) { // NE-SW valley + if (xl > yl) { + quad = xl - yl; + height += quad / 2; + } + } + + return height; } diff --git a/src/map.h b/src/map.h index 8a42517699..5d0f1a655b 100644 --- a/src/map.h +++ b/src/map.h @@ -184,5 +184,6 @@ enum { void map_init(); void map_update_tile_pointers(); +int map_element_height(int x, int y); #endif diff --git a/src/news_item.c b/src/news_item.c index f09f7f19c8..3f50fbfd96 100644 --- a/src/news_item.c +++ b/src/news_item.c @@ -184,17 +184,9 @@ void news_item_get_subject_location(int type, int subject, int *x, int *y, int * *x = SPRITE_LOCATION_NULL; break; } - { - uint32 eax, ebx, ecx, edx, esi, edi, ebp; - eax = (ride->var_050 & 0xFF) * 32 + 16; - ecx = (ride->var_050 >> 8) * 32 + 16; - RCT2_CALLFUNC_X(0x00662783, &eax, &ebx, &ecx, &edx, &esi, &edi, &ebp); - if (edx & 0xFFFF0000) - edx >>= 16; - *x = eax; - *y = ecx; - *z = edx; - } + *x = (ride->var_050 & 0xFF) * 32 + 16; + *y = (ride->var_050 >> 8) * 32 + 16; + *z = map_element_height(*x, *y); break; case NEWS_ITEM_PEEP_ON_RIDE: peep = &(RCT2_ADDRESS(RCT2_ADDRESS_SPRITE_LIST, rct_sprite)[subject]); @@ -233,16 +225,10 @@ void news_item_get_subject_location(int type, int subject, int *x, int *y, int * *z = peep->z; break; case NEWS_ITEM_BLANK: - { - uint32 eax, ebx, ecx, edx, esi, edi, ebp; - eax = subject; - ecx = subject >> 16; - RCT2_CALLFUNC_X(0x00662783, &eax, &ebx, &ecx, &edx, &esi, &edi, &ebp); - *x = eax; - *y = ecx; - *z = edx; - } - break; + *x = subject; + *y = subject >> 16; + *z = map_element_height(x, y); + break; default: *x = SPRITE_LOCATION_NULL; break; diff --git a/src/news_item.h b/src/news_item.h index be8dd55e42..de12b44e22 100644 --- a/src/news_item.h +++ b/src/news_item.h @@ -22,6 +22,7 @@ #define _NEWS_ITEM_H_ #include "rct2.h" +#include "map.h" enum { NEWS_ITEM_NULL, diff --git a/src/title.c b/src/title.c index 61d2e78f52..6a85e4e6c0 100644 --- a/src/title.c +++ b/src/title.c @@ -152,7 +152,7 @@ static void title_update_showcase() { rct_window* w; uint8 script_opcode, script_operand; - short x, y; + short x, y, z; int i, _edx; if (_scriptWaitCounter <= 0) { @@ -201,18 +201,12 @@ static void title_update_showcase() case TITLE_SCRIPT_LOCATION: x = (*_currentScript++) * 32 + 16; y = (*_currentScript++) * 32 + 16; - - // Set location - int eax, ebx, ecx, edx, esi, edi, ebp; - eax = x; - ecx = y; - RCT2_CALLFUNC_X(0x00662783, &eax, &ebx, &ecx, &edx, &esi, &edi, &ebp); - _edx = edx; + z = map_element_height(x, y); // Update viewport w = window_get_main(); if (w != NULL) { - window_scroll_to_location(w, x, y, _edx); + window_scroll_to_location(w, x, y, z); w->flags &= ~0x08; viewport_update_position(w); } From d4583339f47097883d70fe69e26cc2768f1e0dc7 Mon Sep 17 00:00:00 2001 From: ZedThree Date: Sat, 3 May 2014 10:22:20 +0200 Subject: [PATCH 8/9] squash! Add map_element_height (finishes sub_662783) --- src/map.c | 53 ++++++++++++++++++++++++++++++----------------------- 1 file changed, 30 insertions(+), 23 deletions(-) diff --git a/src/map.c b/src/map.c index 57a305981b..4bfd297ef3 100644 --- a/src/map.c +++ b/src/map.c @@ -156,7 +156,8 @@ int map_element_height(int x, int y) uint8 extra_height = (slope & 0x10) >> 4; // 0x10 is the 5th bit - sets slope to double height slope &= 0xF; - uint8 quad, quad_2; // which quadrant the element is in? + uint8 quad, quad_extra; // which quadrant the element is in? + // quad_extra is for extra height tiles uint8 xl, yl; @@ -208,47 +209,53 @@ int map_element_height(int x, int y) // One corner down if ((slope == 7) || (slope == 11) || (slope == 13) || (slope == 14)) { - switch(slope) { + switch (slope) { case 7: // NW corner down - quad = xl + TILE_SIZE - yl; - quad_2 = xl - yl; + quad_extra = xl + TILE_SIZE - yl; + quad = xl - yl; break; case 11: // SW corner down - quad = xl + yl; - quad_2 = xl + yl - TILE_SIZE; + quad_extra = xl + yl; + quad = xl + yl - TILE_SIZE; break; case 13: // SE corner down - quad = TILE_SIZE - xl + yl; - quad_2 = xl - yl; + quad_extra = TILE_SIZE - xl + yl; + quad = xl - yl; break; case 14: // NE corner down - quad = (TILE_SIZE - xl) + (TILE_SIZE - yl); - quad_2 = TILE_SIZE - yl - xl; + quad_extra = (TILE_SIZE - xl) + (TILE_SIZE - yl); + quad = TILE_SIZE - yl - xl; break; } - height += 0x10; + if (extra_height) { - height += quad / 2; + height += quad_extra / 2; height++; + return height; } - if (quad_2 < 0) { - height += quad_2 / 2; + // This tile is essentially at the next height level + height += 0x10; + // so we move *down* the slope + if (quad < 0) { + height += quad / 2; height += 0xFF00; } } // Valleys - if (slope == 5) { // NW-SE valley - quad = xl + yl; - if (quad > TILE_SIZE + 1) { - quad = TILE_SIZE - xl - yl; - if (quad > 0) { - height += quad / 2; + if ((slope == 5) || (slope == 10)) { + switch (slope) { + case 5: // NW-SE valley + if (xl + yl <= TILE_SIZE + 1) { + return height; } - } - } else if (slope == 10) { // NE-SW valley - if (xl > yl) { + quad = TILE_SIZE - xl - yl; + break; + case 10: // NE-SW valley quad = xl - yl; + break; + } + if (quad > 0) { height += quad / 2; } } From 3cd254fbbb7bbfeaa8c8d4fb61a732d92b052faa Mon Sep 17 00:00:00 2001 From: ZedThree Date: Sat, 3 May 2014 10:24:25 +0200 Subject: [PATCH 9/9] Change formatting to VS style, improve comments --- src/map.c | 154 +++++++++++++++++++++++++----------------------- src/news_item.c | 8 +-- 2 files changed, 84 insertions(+), 78 deletions(-) diff --git a/src/map.c b/src/map.c index 4bfd297ef3..38064fe979 100644 --- a/src/map.c +++ b/src/map.c @@ -154,110 +154,116 @@ int map_element_height(int x, int y) uint32 slope = (mapElement->properties.surface.slope & MAP_ELEMENT_SLOPE_MASK); uint8 extra_height = (slope & 0x10) >> 4; // 0x10 is the 5th bit - sets slope to double height + // Remove the extra height bit slope &= 0xF; uint8 quad, quad_extra; // which quadrant the element is in? // quad_extra is for extra height tiles - uint8 xl, yl; + uint8 xl, yl; // coordinates across this tile uint8 TILE_SIZE = 31; xl = x & 0x1f; yl = y & 0x1f; - // slope logic - // slope == 0 is flat, and slope == 15 is not used + // Slope logic: + // Each of the four bits in slope represents that corner being raised + // slope == 15 (all four bits) is not used and slope == 0 is flat + // If the extra_height bit is set, then the slope goes up two z-levels + + // We arbitrarily take the SW corner to be closest to the viewer // One corner up if ((slope == 1) || (slope == 2) || (slope == 4) || (slope == 8)) { - switch(slope) { - case 1: // NE corner up - quad = xl + yl - TILE_SIZE; - break; - case 2: // SE corner up - quad = xl - yl; - break; - case 4: // SW corner up - quad = TILE_SIZE - yl - xl; - break; - case 8: // NW corner up - quad = xl - yl; - break; - } - if (quad > 0) { - height += quad / 2; - } + switch (slope) { + case 1: // NE corner up + quad = xl + yl - TILE_SIZE; + break; + case 2: // SE corner up + quad = xl - yl; + break; + case 4: // SW corner up + quad = TILE_SIZE - yl - xl; + break; + case 8: // NW corner up + quad = xl - yl; + break; + } + // If the element is in the quadrant with the slope, raise its height + if (quad > 0) { + height += quad / 2; + } } // One side up - switch(slope) { + switch (slope) { case 3: // E side up - height += xl / 2; - break; + height += xl / 2; + break; case 6: // S side up - height += (TILE_SIZE - yl) / 2; - break; + height += (TILE_SIZE - yl) / 2; + break; case 9: // N side up - height += yl / 2; - height++; - break; + height += yl / 2; + height++; + break; case 12: // W side up - height += (TILE_SIZE - xl) / 2; - break; + height += (TILE_SIZE - xl) / 2; + break; } // One corner down if ((slope == 7) || (slope == 11) || (slope == 13) || (slope == 14)) { - switch (slope) { - case 7: // NW corner down - quad_extra = xl + TILE_SIZE - yl; - quad = xl - yl; - break; - case 11: // SW corner down - quad_extra = xl + yl; - quad = xl + yl - TILE_SIZE; - break; - case 13: // SE corner down - quad_extra = TILE_SIZE - xl + yl; - quad = xl - yl; - break; - case 14: // NE corner down - quad_extra = (TILE_SIZE - xl) + (TILE_SIZE - yl); - quad = TILE_SIZE - yl - xl; - break; - } + switch (slope) { + case 7: // NW corner down + quad_extra = xl + TILE_SIZE - yl; + quad = xl - yl; + break; + case 11: // SW corner down + quad_extra = xl + yl; + quad = xl + yl - TILE_SIZE; + break; + case 13: // SE corner down + quad_extra = TILE_SIZE - xl + yl; + quad = xl - yl; + break; + case 14: // NE corner down + quad_extra = (TILE_SIZE - xl) + (TILE_SIZE - yl); + quad = TILE_SIZE - yl - xl; + break; + } - if (extra_height) { - height += quad_extra / 2; - height++; - return height; - } - // This tile is essentially at the next height level - height += 0x10; - // so we move *down* the slope - if (quad < 0) { - height += quad / 2; - height += 0xFF00; - } + if (extra_height) { + height += quad_extra / 2; + height++; + return height; + } + // This tile is essentially at the next height level + height += 0x10; + // so we move *down* the slope + if (quad < 0) { + height += quad / 2; + height += 0xFF00; + } } // Valleys if ((slope == 5) || (slope == 10)) { - switch (slope) { - case 5: // NW-SE valley - if (xl + yl <= TILE_SIZE + 1) { - return height; - } - quad = TILE_SIZE - xl - yl; - break; - case 10: // NE-SW valley - quad = xl - yl; - break; - } - if (quad > 0) { - height += quad / 2; - } + switch (slope) { + case 5: // NW-SE valley + if (xl + yl <= TILE_SIZE + 1) { + return height; + } + quad = TILE_SIZE - xl - yl; + break; + case 10: // NE-SW valley + quad = xl - yl; + break; + } + if (quad > 0) { + height += quad / 2; + } } return height; diff --git a/src/news_item.c b/src/news_item.c index 3f50fbfd96..4e862d599b 100644 --- a/src/news_item.c +++ b/src/news_item.c @@ -225,10 +225,10 @@ void news_item_get_subject_location(int type, int subject, int *x, int *y, int * *z = peep->z; break; case NEWS_ITEM_BLANK: - *x = subject; - *y = subject >> 16; - *z = map_element_height(x, y); - break; + *x = subject; + *y = subject >> 16; + *z = map_element_height(*x, *y); + break; default: *x = SPRITE_LOCATION_NULL; break;