#pragma region Copyright (c) 2014-2016 OpenRCT2 Developers
/*****************************************************************************
 * OpenRCT2, an open source clone of Roller Coaster Tycoon 2.
 *
 * OpenRCT2 is the work of many authors, a full list can be found in contributors.md
 * For more information, visit https://github.com/OpenRCT2/OpenRCT2
 *
 * OpenRCT2 is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * A full copy of the GNU General Public License can be found in licence.txt
 *****************************************************************************/
#pragma endregion

#include "../addresses.h"
#include "../audio/audio.h"
#include "../cheats.h"
#include "../config.h"
#include "../game.h"
#include "../input.h"
#include "../interface/themes.h"
#include "../interface/viewport.h"
#include "../interface/widget.h"
#include "../interface/window.h"
#include "../localisation/date.h"
#include "../localisation/localisation.h"
#include "../peep/staff.h"
#include "../ride/ride.h"
#include "../ride/ride_data.h"
#include "../ride/track.h"
#include "../ride/track_design.h"
#include "../sprites.h"
#include "../windows/error.h"
#include "../world/map.h"
#include "../world/sprite.h"
#include "dropdown.h"
#include "../rct1.h"

#define var_496(w)	RCT2_GLOBAL((int)w + 0x496, uint16)

enum {
	WINDOW_RIDE_PAGE_MAIN,
	WINDOW_RIDE_PAGE_VEHICLE,
	WINDOW_RIDE_PAGE_OPERATING,
	WINDOW_RIDE_PAGE_MAINTENANCE,
	WINDOW_RIDE_PAGE_COLOUR,
	WINDOW_RIDE_PAGE_MUSIC,
	WINDOW_RIDE_PAGE_MEASUREMENTS,
	WINDOW_RIDE_PAGE_GRAPHS,
	WINDOW_RIDE_PAGE_INCOME,
	WINDOW_RIDE_PAGE_CUSTOMER,
	WINDOW_RIDE_PAGE_COUNT
};

#pragma region Widgets

enum {
	WIDX_BACKGROUND,
	WIDX_TITLE,
	WIDX_CLOSE,
	WIDX_PAGE_BACKGROUND,
	WIDX_TAB_1,
	WIDX_TAB_2,
	WIDX_TAB_3,
	WIDX_TAB_4,
	WIDX_TAB_5,
	WIDX_TAB_6,
	WIDX_TAB_7,
	WIDX_TAB_8,
	WIDX_TAB_9,
	WIDX_TAB_10,

	WIDX_VIEWPORT = 14,
	WIDX_VIEW,
	WIDX_VIEW_DROPDOWN,
	WIDX_STATUS,
	WIDX_OPEN,
	WIDX_CONSTRUCTION,
	WIDX_RENAME,
	WIDX_LOCATE,
	WIDX_DEMOLISH,
	WIDX_CLOSE_LIGHT,
	WIDX_TEST_LIGHT,
	WIDX_OPEN_LIGHT,

	WIDX_VEHICLE_TYPE = 14,
	WIDX_VEHICLE_TYPE_DROPDOWN,
	WIDX_VEHICLE_TRAINS_PREVIEW,
	WIDX_VEHICLE_TRAINS,
	WIDX_VEHICLE_TRAINS_INCREASE,
	WIDX_VEHICLE_TRAINS_DECREASE,
	WIDX_VEHICLE_CARS_PER_TRAIN,
	WIDX_VEHICLE_CARS_PER_TRAIN_INCREASE,
	WIDX_VEHICLE_CARS_PER_TRAIN_DECREASE,

	WIDX_MODE_TWEAK = 14,
	WIDX_MODE_TWEAK_INCREASE,
	WIDX_MODE_TWEAK_DECREASE,
	WIDX_LIFT_HILL_SPEED,
	WIDX_LIFT_HILL_SPEED_INCREASE,
	WIDX_LIFT_HILL_SPEED_DECREASE,
	WIDX_LOAD_CHECKBOX,
	WIDX_LEAVE_WHEN_ANOTHER_ARRIVES_CHECKBOX,
	WIDX_MINIMUM_LENGTH_CHECKBOX,
	WIDX_MINIMUM_LENGTH,
	WIDX_MINIMUM_LENGTH_INCREASE,
	WIDX_MINIMUM_LENGTH_DECREASE,
	WIDX_MAXIMUM_LENGTH_CHECKBOX,
	WIDX_MAXIMUM_LENGTH,
	WIDX_MAXIMUM_LENGTH_INCREASE,
	WIDX_MAXIMUM_LENGTH_DECREASE,
	WIDX_SYNCHRONISE_WITH_ADJACENT_STATIONS_CHECKBOX,
	WIDX_MODE_TWEAK_LABEL,
	WIDX_LIFT_HILL_SPEED_LABEL,
	WIDX_MODE,
	WIDX_MODE_DROPDOWN,
	WIDX_LOAD,
	WIDX_LOAD_DROPDOWN,
	WIDX_OPERATE_NUMBER_OF_CIRCUITS_LABEL,
	WIDX_OPERATE_NUMBER_OF_CIRCUITS,
	WIDX_OPERATE_NUMBER_OF_CIRCUITS_INCREASE,
	WIDX_OPERATE_NUMBER_OF_CIRCUITS_DECREASE,

	WIDX_INSPECTION_INTERVAL = 14,
	WIDX_INSPECTION_INTERVAL_DROPDOWN,
	WIDX_LOCATE_MECHANIC,
	WIDX_FORCE_BREAKDOWN,

	WIDX_TRACK_PREVIEW = 14,
	WIDX_TRACK_COLOUR_SCHEME,
	WIDX_TRACK_COLOUR_SCHEME_DROPDOWN,
	WIDX_TRACK_MAIN_COLOUR,
	WIDX_TRACK_ADDITIONAL_COLOUR,
	WIDX_TRACK_SUPPORT_COLOUR,
	WIDX_MAZE_STYLE,
	WIDX_MAZE_STYLE_DROPDOWN,
	WIDX_PAINT_INDIVIDUAL_AREA,
	WIDX_ENTRANCE_PREVIEW,
	WIDX_ENTRANCE_STYLE,
	WIDX_ENTRANCE_STYLE_DROPDOWN,
	WIDX_VEHICLE_PREVIEW,
	WIDX_VEHICLE_COLOUR_SCHEME,
	WIDX_VEHICLE_COLOUR_SCHEME_DROPDOWN,
	WIDX_VEHICLE_COLOUR_INDEX,
	WIDX_VEHICLE_COLOUR_INDEX_DROPDOWN,
	WIDX_VEHICLE_MAIN_COLOUR,
	WIDX_VEHICLE_ADDITIONAL_COLOUR_1,
	WIDX_VEHICLE_ADDITIONAL_COLOUR_2,

	WIDX_PLAY_MUSIC = 14,
	WIDX_MUSIC,
	WIDX_MUSIC_DROPDOWN,

	WIDX_SAVE_TRACK_DESIGN = 14,
	WIDX_SELECT_NEARBY_SCENERY,
	WIDX_RESET_SELECTION,
	WIDX_SAVE_DESIGN,
	WIDX_CANCEL_DESIGN,

	WIDX_GRAPH = 14,
	WIDX_GRAPH_VELOCITY,
	WIDX_GRAPH_ALTITUDE,
	WIDX_GRAPH_VERTICAL,
	WIDX_GRAPH_LATERAL,

	WIDX_PRIMARY_PRICE_LABEL = 14,
	WIDX_PRIMARY_PRICE,
	WIDX_PRIMARY_PRICE_INCREASE,
	WIDX_PRIMARY_PRICE_DECREASE,
	WIDX_PRIMARY_PRICE_SAME_THROUGHOUT_PARK,
	WIDX_SECONDARY_PRICE_LABEL,
	WIDX_SECONDARY_PRICE,
	WIDX_SECONDARY_PRICE_INCREASE,
	WIDX_SECONDARY_PRICE_DECREASE,
	WIDX_SECONDARY_PRICE_SAME_THROUGHOUT_PARK,

	WIDX_SHOW_GUESTS_THOUGHTS = 14,
	WIDX_SHOW_GUESTS_ON_RIDE,
	WIDX_SHOW_GUESTS_QUEUING
};

#define RCT1_LIGHT_OFFSET 4

// 0x009ADC34
static rct_widget window_ride_main_widgets[] = {
	{ WWT_FRAME,			0,	0,		315,	0,		206,	0x0FFFFFFFF,					STR_NONE									},
	{ WWT_CAPTION,			0,	1,		314,	1,		14,		0x3DD,							STR_WINDOW_TITLE_TIP						},
	{ WWT_CLOSEBOX,			0,	303,	313,	2,		13,		STR_CLOSE_X,					STR_CLOSE_WINDOW_TIP						},
	{ WWT_RESIZE,			1,	0,		315,	43,		179,	0x0FFFFFFFF,					STR_NONE									},
	{ WWT_TAB,				1,	3,		33,		17,		43,		0x2000144E,						STR_VIEW_OF_RIDE_ATTRACTION_TIP				},
	{ WWT_TAB,				1,	34,		64,		17,		46,		0x2000144E,						STR_VEHICLE_DETAILS_AND_OPTIONS_TIP			},
	{ WWT_TAB,				1,	65,		95,		17,		43,		0x2000144E,						STR_OPERATING_OPTIONS_TIP					},
	{ WWT_TAB,				1,	96,		126,	17,		43,		0x2000144E,						STR_MAINTENANCE_OPTIONS_TIP					},
	{ WWT_TAB,				1,	127,	157,	17,		43,		0x2000144E,						STR_COLOUR_SCHEME_OPTIONS_TIP				},
	{ WWT_TAB,				1,	158,	188,	17,		43,		0x2000144E,						STR_SOUND_AND_MUSIC_OPTIONS_TIP				},
	{ WWT_TAB,				1,	189,	219,	17,		43,		0x2000144E,						STR_MEASUREMENTS_AND_TEST_DATA_TIP			},
	{ WWT_TAB,				1,	220,	250,	17,		43,		0x2000144E,						STR_GRAPHS_TIP								},
	{ WWT_TAB,				1,	251,	281,	17,		43,		0x2000144E,						STR_INCOME_AND_COSTS_TIP					},
	{ WWT_TAB,				1,	282,	312,	17,		43,		0x2000144E,						STR_CUSTOMER_INFORMATION_TIP				},

	{ WWT_VIEWPORT,			1,	3,		290,	60,		166,	0x0FFFFFFFE,					STR_NONE									},
	{ WWT_DROPDOWN,			1,	35,		256,	46,		57,		0x0FFFFFFFF,					STR_VIEW_SELECTION							},
	{ WWT_DROPDOWN_BUTTON,	1,	245,	255,	47,		56,		STR_DROPDOWN_GLYPH,				STR_VIEW_SELECTION							},
	{ WWT_12,				1,	3,		290,	167,	177,	0x0FFFFFFFF,					STR_NONE									},
	{ WWT_FLATBTN,			1,	291,	314,	46,		69,		0x0FFFFFFFF,					STR_OPEN_CLOSE_OR_TEST_RIDE					},
	{ WWT_FLATBTN,			1,	291,	314,	70,		93,		SPR_CONSTRUCTION,				STR_CONSTRUCTION							},
	{ WWT_FLATBTN,			1,	291,	314,	94,		117,	SPR_RENAME,						STR_NAME_RIDE_TIP							},
	{ WWT_FLATBTN,			1,	291,	314,	118,	141,	SPR_LOCATE,						STR_LOCATE_SUBJECT_TIP						},
	{ WWT_FLATBTN,			1,	291,	314,	142,	165,	SPR_DEMOLISH,					STR_DEMOLISH_RIDE_TIP						},
	{ WWT_IMGBTN,			1,	296,	309,	48,		61,		SPR_G2_RCT1_CLOSE_BUTTON_0,		STR_CLOSE_RIDE_TIP							},
	{ WWT_IMGBTN,			1,	296,	309,	62,		75,		SPR_G2_RCT1_TEST_BUTTON_0,		STR_TEST_RIDE_TIP							},
	{ WWT_IMGBTN,			1,	296,	309,	76,		89,		SPR_G2_RCT1_OPEN_BUTTON_0,		STR_OPEN_RIDE_TIP							},
	{ WIDGETS_END },
};

// 0x009ADDA8
static rct_widget window_ride_vehicle_widgets[] = {
	{ WWT_FRAME,			0,	0,		315,	0,		206,	0x0FFFFFFFF,								STR_NONE										},
	{ WWT_CAPTION,			0,	1,		314,	1,		14,		0x3DD,										STR_WINDOW_TITLE_TIP							},
	{ WWT_CLOSEBOX,			0,	303,	313,	2,		13,		STR_CLOSE_X,								STR_CLOSE_WINDOW_TIP							},
	{ WWT_RESIZE,			1,	0,		315,	43,		179,	0x0FFFFFFFF,								STR_NONE										},
	{ WWT_TAB,				1,	3,		33,		17,		43,		0x2000144E,									STR_VIEW_OF_RIDE_ATTRACTION_TIP					},
	{ WWT_TAB,				1,	34,		64,		17,		46,		0x2000144E,									STR_VEHICLE_DETAILS_AND_OPTIONS_TIP				},
	{ WWT_TAB,				1,	65,		95,		17,		43,		0x2000144E,									STR_OPERATING_OPTIONS_TIP						},
	{ WWT_TAB,				1,	96,		126,	17,		43,		0x2000144E,									STR_MAINTENANCE_OPTIONS_TIP						},
	{ WWT_TAB,				1,	127,	157,	17,		43,		0x2000144E,									STR_COLOUR_SCHEME_OPTIONS_TIP					},
	{ WWT_TAB,				1,	158,	188,	17,		43,		0x2000144E,									STR_SOUND_AND_MUSIC_OPTIONS_TIP					},
	{ WWT_TAB,				1,	189,	219,	17,		43,		0x2000144E,									STR_MEASUREMENTS_AND_TEST_DATA_TIP				},
	{ WWT_TAB,				1,	220,	250,	17,		43,		0x2000144E,									STR_GRAPHS_TIP									},
	{ WWT_TAB,				1,	251,	281,	17,		43,		0x2000144E,									STR_INCOME_AND_COSTS_TIP						},
	{ WWT_TAB,				1,	282,	312,	17,		43,		0x2000144E,									STR_CUSTOMER_INFORMATION_TIP					},

	{ WWT_DROPDOWN,			1,	7,		308,	50,		61,		0xFFFFFFFF,									STR_NONE										},
	{ WWT_DROPDOWN_BUTTON,	1,	297,	307,	51,		60,		STR_DROPDOWN_GLYPH,							STR_NONE										},
	{ WWT_SCROLL,			1,	7,		308,	141,	183,	0,											STR_NONE										},
	{ WWT_SPINNER,			1,	7,		151,	190,	201,	1021,										STR_NONE										},
	{ WWT_DROPDOWN_BUTTON,	1,	140,	150,	191,	195,	STR_NUMERIC_UP,								STR_NONE										},
	{ WWT_DROPDOWN_BUTTON,	1,	140,	150,	196,	200,	STR_NUMERIC_DOWN,							STR_NONE										},
	{ WWT_SPINNER,			1,	164,	308,	190,	201,	STR_1_CAR_PER_TRAIN,						STR_NONE										},
	{ WWT_DROPDOWN_BUTTON,	1,	297,	307,	191,	195,	STR_NUMERIC_UP,								STR_NONE										},
	{ WWT_DROPDOWN_BUTTON,	1,	297,	307,	196,	200,	STR_NUMERIC_DOWN,							STR_NONE										},
	{ WIDGETS_END },
};

// 0x009ADEFC
static rct_widget window_ride_operating_widgets[] = {
	{ WWT_FRAME,			0,	0,		315,	0,		206,	0x0FFFFFFFF,								STR_NONE										},
	{ WWT_CAPTION,			0,	1,		314,	1,		14,		0x3DD,										STR_WINDOW_TITLE_TIP							},
	{ WWT_CLOSEBOX,			0,	303,	313,	2,		13,		STR_CLOSE_X,								STR_CLOSE_WINDOW_TIP							},
	{ WWT_RESIZE,			1,	0,		315,	43,		179,	0x0FFFFFFFF,								STR_NONE										},
	{ WWT_TAB,				1,	3,		33,		17,		43,		0x2000144E,									STR_VIEW_OF_RIDE_ATTRACTION_TIP					},
	{ WWT_TAB,				1,	34,		64,		17,		46,		0x2000144E,									STR_VEHICLE_DETAILS_AND_OPTIONS_TIP				},
	{ WWT_TAB,				1,	65,		95,		17,		43,		0x2000144E,									STR_OPERATING_OPTIONS_TIP						},
	{ WWT_TAB,				1,	96,		126,	17,		43,		0x2000144E,									STR_MAINTENANCE_OPTIONS_TIP						},
	{ WWT_TAB,				1,	127,	157,	17,		43,		0x2000144E,									STR_COLOUR_SCHEME_OPTIONS_TIP					},
	{ WWT_TAB,				1,	158,	188,	17,		43,		0x2000144E,									STR_SOUND_AND_MUSIC_OPTIONS_TIP					},
	{ WWT_TAB,				1,	189,	219,	17,		43,		0x2000144E,									STR_MEASUREMENTS_AND_TEST_DATA_TIP				},
	{ WWT_TAB,				1,	220,	250,	17,		43,		0x2000144E,									STR_GRAPHS_TIP									},
	{ WWT_TAB,				1,	251,	281,	17,		43,		0x2000144E,									STR_INCOME_AND_COSTS_TIP						},
	{ WWT_TAB,				1,	282,	312,	17,		43,		0x2000144E,									STR_CUSTOMER_INFORMATION_TIP					},

	{ WWT_SPINNER,			1,	157,	308,	61,		72,		874,										STR_NONE										},
	{ WWT_DROPDOWN_BUTTON,	1,	297,	307,	62,		66,		STR_NUMERIC_UP,								STR_NONE										},
	{ WWT_DROPDOWN_BUTTON,	1,	297,	307,	67,		71,		STR_NUMERIC_DOWN,							STR_NONE										},
	{ WWT_SPINNER,			1,	157,	308,	75,		86,		3096,										STR_NONE										},
	{ WWT_DROPDOWN_BUTTON,	1,	297,	307,	76,		80,		STR_NUMERIC_UP,								STR_SELECT_LIFT_HILL_CHAIN_SPEED_TIP			},
	{ WWT_DROPDOWN_BUTTON,	1,	297,	307,	81,		85,		STR_NUMERIC_DOWN,							STR_SELECT_LIFT_HILL_CHAIN_SPEED_TIP			},
	{ WWT_CHECKBOX,			1,	7,		86,		109,	120,	STR_WAIT_FOR,								STR_WAIT_FOR_PASSENGERS_BEFORE_DEPARTING_TIP	},
	{ WWT_CHECKBOX,			1,	7,		308,	124,	135,	0xFFFFFFFF,									STR_NONE										},
	{ WWT_CHECKBOX,			1,	7,		156,	139,	150,	STR_MINIMUM_WAITING_TIME,					STR_MINIMUM_LENGTH_BEFORE_DEPARTING_TIP			},
	{ WWT_SPINNER,			1,	157,	308,	139,	150,	870,										STR_NONE										},
	{ WWT_DROPDOWN_BUTTON,	1,	297,	307,	140,	144,	STR_NUMERIC_UP,								STR_NONE										},
	{ WWT_DROPDOWN_BUTTON,	1,	297,	307,	145,	149,	STR_NUMERIC_DOWN,							STR_NONE										},
	{ WWT_CHECKBOX,			1,	7,		156,	154,	165,	STR_MAXIMUM_WAITING_TIME,					STR_MAXIMUM_LENGTH_BEFORE_DEPARTING_TIP			},
	{ WWT_SPINNER,			1,	157,	308,	154,	165,	872,										STR_NONE										},
	{ WWT_DROPDOWN_BUTTON,	1,	297,	307,	155,	159,	STR_NUMERIC_UP,								STR_NONE										},
	{ WWT_DROPDOWN_BUTTON,	1,	297,	307,	160,	164,	STR_NUMERIC_DOWN,							STR_NONE										},
	{ WWT_CHECKBOX,			1,	7,		308,	169,	180,	STR_SYNCHRONISE_WITH_ADJACENT_STATIONS,		STR_SYNCHRONISE_WITH_ADJACENT_STATIONS_TIP		},
	{ WWT_24,				1,	7,		149,	61,		72,		0xFFFFFFFF,									STR_NONE										},
	{ WWT_24,				1,	7,		149,	75,		86,		STR_LIFT_HILL_CHAIN_SPEED,					STR_NONE										},
	{ WWT_DROPDOWN,			1,	7,		308,	47,		58,		0xFFFFFFFF,									STR_SELECT_OPERATING_MODE						},
	{ WWT_DROPDOWN_BUTTON,	1,	297,	307,	48,		57,		STR_DROPDOWN_GLYPH,							STR_SELECT_OPERATING_MODE						},
	{ WWT_DROPDOWN,			1,	87,		308,	109,	120,	0xFFFFFFFF,									STR_NONE										},
	{ WWT_DROPDOWN_BUTTON,	1,	297,	307,	110,	119,	STR_DROPDOWN_GLYPH,							STR_NONE										},
	{ WWT_24,				1,	7,		149,	89,		100,	STR_NUMBER_OF_CIRCUITS,						STR_NUMBER_OF_CIRCUITS_TIP						},
	{ WWT_SPINNER,			1,	157,	308,	89,		100,	1957,										STR_NONE										},
	{ WWT_DROPDOWN_BUTTON,	1,	297,	307,	90,		94,		STR_NUMERIC_UP,								STR_SELECT_NUMBER_OF_CIRCUITS_TIP				},
	{ WWT_DROPDOWN_BUTTON,	1,	297,	307,	95,		99,		STR_NUMERIC_DOWN,							STR_SELECT_NUMBER_OF_CIRCUITS_TIP				},
	{ WIDGETS_END },
};

// 0x009AE190
static rct_widget window_ride_maintenance_widgets[] = {
	{ WWT_FRAME,			0,	0,		315,	0,		206,	0x0FFFFFFFF,					STR_NONE													},
	{ WWT_CAPTION,			0,	1,		314,	1,		14,		0x3DD,							STR_WINDOW_TITLE_TIP										},
	{ WWT_CLOSEBOX,			0,	303,	313,	2,		13,		STR_CLOSE_X,					STR_CLOSE_WINDOW_TIP										},
	{ WWT_RESIZE,			1,	0,		315,	43,		179,	0x0FFFFFFFF,					STR_NONE													},
	{ WWT_TAB,				1,	3,		33,		17,		43,		0x2000144E,						STR_VIEW_OF_RIDE_ATTRACTION_TIP								},
	{ WWT_TAB,				1,	34,		64,		17,		46,		0x2000144E,						STR_VEHICLE_DETAILS_AND_OPTIONS_TIP							},
	{ WWT_TAB,				1,	65,		95,		17,		43,		0x2000144E,						STR_OPERATING_OPTIONS_TIP									},
	{ WWT_TAB,				1,	96,		126,	17,		43,		0x2000144E,						STR_MAINTENANCE_OPTIONS_TIP									},
	{ WWT_TAB,				1,	127,	157,	17,		43,		0x2000144E,						STR_COLOUR_SCHEME_OPTIONS_TIP								},
	{ WWT_TAB,				1,	158,	188,	17,		43,		0x2000144E,						STR_SOUND_AND_MUSIC_OPTIONS_TIP								},
	{ WWT_TAB,				1,	189,	219,	17,		43,		0x2000144E,						STR_MEASUREMENTS_AND_TEST_DATA_TIP							},
	{ WWT_TAB,				1,	220,	250,	17,		43,		0x2000144E,						STR_GRAPHS_TIP												},
	{ WWT_TAB,				1,	251,	281,	17,		43,		0x2000144E,						STR_INCOME_AND_COSTS_TIP									},
	{ WWT_TAB,				1,	282,	312,	17,		43,		0x2000144E,						STR_CUSTOMER_INFORMATION_TIP								},

	{ WWT_DROPDOWN,			1,	107,	308,	71,		82,		0,								STR_SELECT_HOW_OFTEN_A_MECHANIC_SHOULD_CHECK_THIS_RIDE		},
	{ WWT_DROPDOWN_BUTTON,	1,	297,	307,	72,		81,		STR_DROPDOWN_GLYPH,				STR_SELECT_HOW_OFTEN_A_MECHANIC_SHOULD_CHECK_THIS_RIDE		},
	{ WWT_FLATBTN,			1,	289,	312,	108,	131,	0xFFFFFFFF,						STR_LOCATE_NEAREST_AVAILABLE_MECHANIC_TIP					},
	{ WWT_FLATBTN,			1,	265,	288,	108,	131,	SPR_NO_ENTRY,					STR_DEBUG_FORCE_BREAKDOWN_TIP								},
	{ WIDGETS_END },
};

// 0x009AE2A4
static rct_widget window_ride_colour_widgets[] = {
	{ WWT_FRAME,			0,	0,		315,	0,		206,	0x0FFFFFFFF,					STR_NONE													},
	{ WWT_CAPTION,			0,	1,		314,	1,		14,		0x3DD,							STR_WINDOW_TITLE_TIP										},
	{ WWT_CLOSEBOX,			0,	303,	313,	2,		13,		STR_CLOSE_X,					STR_CLOSE_WINDOW_TIP										},
	{ WWT_RESIZE,			1,	0,		315,	43,		179,	0x0FFFFFFFF,					STR_NONE													},
	{ WWT_TAB,				1,	3,		33,		17,		43,		0x2000144E,						STR_VIEW_OF_RIDE_ATTRACTION_TIP								},
	{ WWT_TAB,				1,	34,		64,		17,		46,		0x2000144E,						STR_VEHICLE_DETAILS_AND_OPTIONS_TIP							},
	{ WWT_TAB,				1,	65,		95,		17,		43,		0x2000144E,						STR_OPERATING_OPTIONS_TIP									},
	{ WWT_TAB,				1,	96,		126,	17,		43,		0x2000144E,						STR_MAINTENANCE_OPTIONS_TIP									},
	{ WWT_TAB,				1,	127,	157,	17,		43,		0x2000144E,						STR_COLOUR_SCHEME_OPTIONS_TIP								},
	{ WWT_TAB,				1,	158,	188,	17,		43,		0x2000144E,						STR_SOUND_AND_MUSIC_OPTIONS_TIP								},
	{ WWT_TAB,				1,	189,	219,	17,		43,		0x2000144E,						STR_MEASUREMENTS_AND_TEST_DATA_TIP							},
	{ WWT_TAB,				1,	220,	250,	17,		43,		0x2000144E,						STR_GRAPHS_TIP												},
	{ WWT_TAB,				1,	251,	281,	17,		43,		0x2000144E,						STR_INCOME_AND_COSTS_TIP									},
	{ WWT_TAB,				1,	282,	312,	17,		43,		0x2000144E,						STR_CUSTOMER_INFORMATION_TIP								},

	{ WWT_SPINNER,			1,	3,		70,		47,		93,		0xFFFFFFFF,						STR_NONE													},
	{ WWT_DROPDOWN,			1,	74,		312,	49,		60,		872,							STR_NONE													},
	{ WWT_DROPDOWN_BUTTON,	1,	301,	311,	50,		59,		STR_DROPDOWN_GLYPH,				STR_COLOUR_SCHEME_TO_CHANGE_TIP								},
	{ WWT_COLOURBTN,		1,	79,		90,		74,		85,		0xFFFFFFFF,						STR_SELECT_MAIN_COLOUR_TIP									},
	{ WWT_COLOURBTN,		1,	99,		110,	74,		85,		0xFFFFFFFF,						STR_SELECT_ADDITIONAL_COLOUR_1_TIP							},
	{ WWT_COLOURBTN,		1,	119,	130,	74,		85,		0xFFFFFFFF,						STR_SELECT_SUPPORT_STRUCTURE_COLOUR_TIP						},
	{ WWT_DROPDOWN,			1,	74,		312,	49,		60,		0xFFFFFFFF,						STR_NONE													},
	{ WWT_DROPDOWN_BUTTON,	1,	301,	311,	50,		59,		STR_DROPDOWN_GLYPH,				STR_NONE													},
	{ WWT_FLATBTN,			1,	289,	312,	68,		91,		SPR_PAINTBRUSH,					STR_PAINT_INDIVIDUAL_AREA_TIP								},
	{ WWT_SPINNER,			1,	245,	312,	101,	147,	0xFFFFFFFF,						STR_NONE													},
	{ WWT_DROPDOWN,			1,	3,		241,	103,	114,	0,								STR_NONE													},
	{ WWT_DROPDOWN_BUTTON,	1,	230,	240,	104,	113,	STR_DROPDOWN_GLYPH,				STR_SELECT_STYLE_OF_ENTRANCE_EXIT_STATION_TIP				},
	{ WWT_SCROLL,			1,	3,		70,		157,	203,	0,								STR_NONE													},
	{ WWT_DROPDOWN,			1,	74,		312,	157,	168,	868,							STR_NONE													},
	{ WWT_DROPDOWN_BUTTON,	1,	301,	311,	158,	167,	STR_DROPDOWN_GLYPH,				STR_SELECT_VEHICLE_COLOUR_SCHEME_TIP						},
	{ WWT_DROPDOWN,			1,	74,		312,	173,	184,	0xFFFFFFFF,						STR_NONE													},
	{ WWT_DROPDOWN_BUTTON,	1,	301,	311,	174,	183,	STR_DROPDOWN_GLYPH,				STR_SELECT_VEHICLE_TO_MODIFY_TIP							},
	{ WWT_COLOURBTN,		1,	79,		90,		190,	201,	0xFFFFFFFF,						STR_SELECT_MAIN_COLOUR_TIP									},
	{ WWT_COLOURBTN,		1,	99,		110,	190,	201,	0xFFFFFFFF,						STR_SELECT_ADDITIONAL_COLOUR_1_TIP							},
	{ WWT_COLOURBTN,		1,	119,	130,	190,	201,	0xFFFFFFFF,						STR_SELECT_ADDITIONAL_COLOUR_2_TIP							},
	{ WIDGETS_END },
};

// 0x009AE4C8
static rct_widget window_ride_music_widgets[] = {
	{ WWT_FRAME,			0,	0,		315,	0,		206,	0x0FFFFFFFF,					STR_NONE									},
	{ WWT_CAPTION,			0,	1,		314,	1,		14,		0x3DD,							STR_WINDOW_TITLE_TIP						},
	{ WWT_CLOSEBOX,			0,	303,	313,	2,		13,		STR_CLOSE_X,					STR_CLOSE_WINDOW_TIP						},
	{ WWT_RESIZE,			1,	0,		315,	43,		179,	0x0FFFFFFFF,					STR_NONE									},
	{ WWT_TAB,				1,	3,		33,		17,		43,		0x2000144E,						STR_VIEW_OF_RIDE_ATTRACTION_TIP				},
	{ WWT_TAB,				1,	34,		64,		17,		46,		0x2000144E,						STR_VEHICLE_DETAILS_AND_OPTIONS_TIP			},
	{ WWT_TAB,				1,	65,		95,		17,		43,		0x2000144E,						STR_OPERATING_OPTIONS_TIP					},
	{ WWT_TAB,				1,	96,		126,	17,		43,		0x2000144E,						STR_MAINTENANCE_OPTIONS_TIP					},
	{ WWT_TAB,				1,	127,	157,	17,		43,		0x2000144E,						STR_COLOUR_SCHEME_OPTIONS_TIP				},
	{ WWT_TAB,				1,	158,	188,	17,		43,		0x2000144E,						STR_SOUND_AND_MUSIC_OPTIONS_TIP				},
	{ WWT_TAB,				1,	189,	219,	17,		43,		0x2000144E,						STR_MEASUREMENTS_AND_TEST_DATA_TIP			},
	{ WWT_TAB,				1,	220,	250,	17,		43,		0x2000144E,						STR_GRAPHS_TIP								},
	{ WWT_TAB,				1,	251,	281,	17,		43,		0x2000144E,						STR_INCOME_AND_COSTS_TIP					},
	{ WWT_TAB,				1,	282,	312,	17,		43,		0x2000144E,						STR_CUSTOMER_INFORMATION_TIP				},

	{ WWT_CHECKBOX,			1,	7,		308,	47,		58,		STR_PLAY_MUSIC,					STR_SELECT_MUSIC_TIP						},
	{ WWT_DROPDOWN,			1,	7,		308,	62,		73,		0,								STR_NONE									},
	{ WWT_DROPDOWN_BUTTON,	1,	297,	307,	63,		72,		STR_DROPDOWN_GLYPH,				STR_SELECT_MUSIC_STYLE_TIP					},
	{ WIDGETS_END },
};

// 0x009AE5DC
static rct_widget window_ride_measurements_widgets[] = {
	{ WWT_FRAME,			0,	0,		315,	0,		206,	0x0FFFFFFFF,					STR_NONE									},
	{ WWT_CAPTION,			0,	1,		314,	1,		14,		0x3DD,							STR_WINDOW_TITLE_TIP						},
	{ WWT_CLOSEBOX,			0,	303,	313,	2,		13,		STR_CLOSE_X,					STR_CLOSE_WINDOW_TIP						},
	{ WWT_RESIZE,			1,	0,		315,	43,		179,	0x0FFFFFFFF,					STR_NONE									},
	{ WWT_TAB,				1,	3,		33,		17,		43,		0x2000144E,						STR_VIEW_OF_RIDE_ATTRACTION_TIP				},
	{ WWT_TAB,				1,	34,		64,		17,		46,		0x2000144E,						STR_VEHICLE_DETAILS_AND_OPTIONS_TIP			},
	{ WWT_TAB,				1,	65,		95,		17,		43,		0x2000144E,						STR_OPERATING_OPTIONS_TIP					},
	{ WWT_TAB,				1,	96,		126,	17,		43,		0x2000144E,						STR_MAINTENANCE_OPTIONS_TIP					},
	{ WWT_TAB,				1,	127,	157,	17,		43,		0x2000144E,						STR_COLOUR_SCHEME_OPTIONS_TIP				},
	{ WWT_TAB,				1,	158,	188,	17,		43,		0x2000144E,						STR_SOUND_AND_MUSIC_OPTIONS_TIP				},
	{ WWT_TAB,				1,	189,	219,	17,		43,		0x2000144E,						STR_MEASUREMENTS_AND_TEST_DATA_TIP			},
	{ WWT_TAB,				1,	220,	250,	17,		43,		0x2000144E,						STR_GRAPHS_TIP								},
	{ WWT_TAB,				1,	251,	281,	17,		43,		0x2000144E,						STR_INCOME_AND_COSTS_TIP					},
	{ WWT_TAB,				1,	282,	312,	17,		43,		0x2000144E,						STR_CUSTOMER_INFORMATION_TIP				},

	{ WWT_FLATBTN,			1,	288,	311,	164,	187,	SPR_FLOPPY,						STR_SAVE_TRACK_DESIGN						},
	{ WWT_DROPDOWN_BUTTON,	1,	4,		157,	128,	139,	STR_SELECT_NEARBY_SCENERY,		STR_NONE									},
	{ WWT_DROPDOWN_BUTTON,	1,	158,	311,	128,	139,	STR_RESET_SELECTION,			STR_NONE									},
	{ WWT_DROPDOWN_BUTTON,	1,	4,		157,	178,	189,	STR_DESIGN_SAVE,				STR_NONE									},
	{ WWT_DROPDOWN_BUTTON,	1,	158,	311,	178,	189,	STR_DESIGN_CANCEL,				STR_NONE									},
	{ WIDGETS_END },
};

// 0x009AE710
static rct_widget window_ride_graphs_widgets[] = {
	{ WWT_FRAME,			0,	0,		315,	0,		206,	0x0FFFFFFFF,					STR_NONE													},
	{ WWT_CAPTION,			0,	1,		314,	1,		14,		0x3DD,							STR_WINDOW_TITLE_TIP										},
	{ WWT_CLOSEBOX,			0,	303,	313,	2,		13,		STR_CLOSE_X,					STR_CLOSE_WINDOW_TIP										},
	{ WWT_RESIZE,			1,	0,		315,	43,		179,	0x0FFFFFFFF,					STR_NONE													},
	{ WWT_TAB,				1,	3,		33,		17,		43,		0x2000144E,						STR_VIEW_OF_RIDE_ATTRACTION_TIP								},
	{ WWT_TAB,				1,	34,		64,		17,		46,		0x2000144E,						STR_VEHICLE_DETAILS_AND_OPTIONS_TIP							},
	{ WWT_TAB,				1,	65,		95,		17,		43,		0x2000144E,						STR_OPERATING_OPTIONS_TIP									},
	{ WWT_TAB,				1,	96,		126,	17,		43,		0x2000144E,						STR_MAINTENANCE_OPTIONS_TIP									},
	{ WWT_TAB,				1,	127,	157,	17,		43,		0x2000144E,						STR_COLOUR_SCHEME_OPTIONS_TIP								},
	{ WWT_TAB,				1,	158,	188,	17,		43,		0x2000144E,						STR_SOUND_AND_MUSIC_OPTIONS_TIP								},
	{ WWT_TAB,				1,	189,	219,	17,		43,		0x2000144E,						STR_MEASUREMENTS_AND_TEST_DATA_TIP							},
	{ WWT_TAB,				1,	220,	250,	17,		43,		0x2000144E,						STR_GRAPHS_TIP												},
	{ WWT_TAB,				1,	251,	281,	17,		43,		0x2000144E,						STR_INCOME_AND_COSTS_TIP									},
	{ WWT_TAB,				1,	282,	312,	17,		43,		0x2000144E,						STR_CUSTOMER_INFORMATION_TIP								},

	{ WWT_SCROLL,			1,	3,		308,	46,		157,	1,								STR_LOGGING_DATA_FROM_TIP									},
	{ WWT_DROPDOWN_BUTTON,	1,	3,		75,		163,	176,	STR_RIDE_STATS_VELOCITY,		STR_SHOW_GRAPH_OF_VELOCITY_AGAINST_TIME_TIP					},
	{ WWT_DROPDOWN_BUTTON,	1,	76,		148,	163,	176,	STR_RIDE_STATS_ALTITUDE,		STR_SHOW_GRAPH_OF_ALTITUDE_AGAINST_TIME_TIP					},
	{ WWT_DROPDOWN_BUTTON,	1,	149,	221,	163,	176,	STR_RIDE_STATS_VERT_G,			STR_SHOW_GRAPH_OF_VERTICAL_ACCELERATION_AGAINST_TIME_TIP	},
	{ WWT_DROPDOWN_BUTTON,	1,	222,	294,	163,	176,	STR_RIDE_STATS_LAT_G,			STR_SHOW_GRAPH_OF_LATERAL_ACCELERATION_AGAINST_TIME_TIP 	},
	{ WIDGETS_END },
};

// 0x009AE844
static rct_widget window_ride_income_widgets[] = {
	{ WWT_FRAME,			0,	0,		315,	0,		206,	0x0FFFFFFFF,					STR_NONE													},
	{ WWT_CAPTION,			0,	1,		314,	1,		14,		0x3DD,							STR_WINDOW_TITLE_TIP										},
	{ WWT_CLOSEBOX,			0,	303,	313,	2,		13,		STR_CLOSE_X,					STR_CLOSE_WINDOW_TIP										},
	{ WWT_RESIZE,			1,	0,		315,	43,		179,	0x0FFFFFFFF,					STR_NONE													},
	{ WWT_TAB,				1,	3,		33,		17,		43,		0x2000144E,						STR_VIEW_OF_RIDE_ATTRACTION_TIP								},
	{ WWT_TAB,				1,	34,		64,		17,		46,		0x2000144E,						STR_VEHICLE_DETAILS_AND_OPTIONS_TIP							},
	{ WWT_TAB,				1,	65,		95,		17,		43,		0x2000144E,						STR_OPERATING_OPTIONS_TIP									},
	{ WWT_TAB,				1,	96,		126,	17,		43,		0x2000144E,						STR_MAINTENANCE_OPTIONS_TIP									},
	{ WWT_TAB,				1,	127,	157,	17,		43,		0x2000144E,						STR_COLOUR_SCHEME_OPTIONS_TIP								},
	{ WWT_TAB,				1,	158,	188,	17,		43,		0x2000144E,						STR_SOUND_AND_MUSIC_OPTIONS_TIP								},
	{ WWT_TAB,				1,	189,	219,	17,		43,		0x2000144E,						STR_MEASUREMENTS_AND_TEST_DATA_TIP							},
	{ WWT_TAB,				1,	220,	250,	17,		43,		0x2000144E,						STR_GRAPHS_TIP												},
	{ WWT_TAB,				1,	251,	281,	17,		43,		0x2000144E,						STR_INCOME_AND_COSTS_TIP									},
	{ WWT_TAB,				1,	282,	312,	17,		43,		0x2000144E,						STR_CUSTOMER_INFORMATION_TIP								},

	{ WWT_24,				1,	5,		144,	50,		61,		0xFFFFFFFF,						STR_NONE													},
	{ WWT_SPINNER,			1,	147,	308,	50,		61,		1429,							STR_NONE													},
	{ WWT_DROPDOWN_BUTTON,	1,	297,	307,	51,		55,		STR_NUMERIC_UP,					STR_NONE													},
	{ WWT_DROPDOWN_BUTTON,	1,	297,	307,	56,		60,		STR_NUMERIC_DOWN,				STR_NONE													},
	{ WWT_CHECKBOX,			1,	5,		310,	61,		72,		STR_SAME_PRICE_THROUGHOUT_PARK,	STR_SAME_PRICE_THROUGHOUT_PARK_TIP							},
	{ WWT_24,				1,	5,		144,	89,		100,	0xFFFFFFFF,						STR_NONE													},
	{ WWT_SPINNER,			1,	147,	308,	89,		100,	1799,							STR_NONE													},
	{ WWT_DROPDOWN_BUTTON,	1,	297,	307,	90,		94,		STR_NUMERIC_UP,					STR_NONE													},
	{ WWT_DROPDOWN_BUTTON,	1,	297,	307,	95,		99,		STR_NUMERIC_DOWN,				STR_NONE													},
	{ WWT_CHECKBOX,			1,	5,		310,	100,	111,	STR_SAME_PRICE_THROUGHOUT_PARK,	STR_SAME_PRICE_THROUGHOUT_PARK_TIP							},
	{ WIDGETS_END },
};

// 0x009AE9C8
static rct_widget window_ride_customer_widgets[] = {
	{ WWT_FRAME,			0,	0,		315,	0,		206,	0x0FFFFFFFF,					STR_NONE													},
	{ WWT_CAPTION,			0,	1,		314,	1,		14,		0x3DD,							STR_WINDOW_TITLE_TIP										},
	{ WWT_CLOSEBOX,			0,	303,	313,	2,		13,		STR_CLOSE_X,					STR_CLOSE_WINDOW_TIP										},
	{ WWT_RESIZE,			1,	0,		315,	43,		179,	0x0FFFFFFFF,					STR_NONE													},
	{ WWT_TAB,				1,	3,		33,		17,		43,		0x2000144E,						STR_VIEW_OF_RIDE_ATTRACTION_TIP								},
	{ WWT_TAB,				1,	34,		64,		17,		46,		0x2000144E,						STR_VEHICLE_DETAILS_AND_OPTIONS_TIP							},
	{ WWT_TAB,				1,	65,		95,		17,		43,		0x2000144E,						STR_OPERATING_OPTIONS_TIP									},
	{ WWT_TAB,				1,	96,		126,	17,		43,		0x2000144E,						STR_MAINTENANCE_OPTIONS_TIP									},
	{ WWT_TAB,				1,	127,	157,	17,		43,		0x2000144E,						STR_COLOUR_SCHEME_OPTIONS_TIP								},
	{ WWT_TAB,				1,	158,	188,	17,		43,		0x2000144E,						STR_SOUND_AND_MUSIC_OPTIONS_TIP								},
	{ WWT_TAB,				1,	189,	219,	17,		43,		0x2000144E,						STR_MEASUREMENTS_AND_TEST_DATA_TIP							},
	{ WWT_TAB,				1,	220,	250,	17,		43,		0x2000144E,						STR_GRAPHS_TIP												},
	{ WWT_TAB,				1,	251,	281,	17,		43,		0x2000144E,						STR_INCOME_AND_COSTS_TIP									},
	{ WWT_TAB,				1,	282,	312,	17,		43,		0x2000144E,						STR_CUSTOMER_INFORMATION_TIP								},

	{ WWT_FLATBTN,			1,	289,	312,	54,		77,		SPR_SHOW_GUESTS_THOUGHTS_ABOUT_THIS_RIDE_ATTRACTION,	STR_SHOW_GUESTS_THOUGHTS_ABOUT_THIS_RIDE_ATTRACTION_TIP	},
	{ WWT_FLATBTN,			1,	289,	312,	78,		101,	SPR_SHOW_GUESTS_ON_THIS_RIDE_ATTRACTION,				STR_SHOW_GUESTS_ON_THIS_RIDE_ATTRACTION_TIP				},
	{ WWT_FLATBTN,			1,	289,	312,	102,	125,	SPR_SHOW_GUESTS_QUEUING_FOR_THIS_RIDE_ATTRACTION,		STR_SHOW_GUESTS_QUEUING_FOR_THIS_RIDE_ATTRACTION_TIP	},
	{ WIDGETS_END },
};

static rct_widget *window_ride_page_widgets[] = {
	window_ride_main_widgets,
	window_ride_vehicle_widgets,
	window_ride_operating_widgets,
	window_ride_maintenance_widgets,
	window_ride_colour_widgets,
	window_ride_music_widgets,
	window_ride_measurements_widgets,
	window_ride_graphs_widgets,
	window_ride_income_widgets,
	window_ride_customer_widgets
};

const uint64 window_ride_page_enabled_widgets[] = {
	0x0000000003FDBFF4,
	0x00000000007EFFF4,
	0x0000019E777DBFF4,
	0x000000000003FFF4,
	0x00000003F37F3FF4,
	0x000000000001FFF4,
	0x000000000007FFF4,
	0x000000000007BFF4,
	0x0000000000E73FF4,
	0x000000000001FFF4
};

const uint64 window_ride_page_hold_down_widgets[] = {
	0x0000000000000000,
	0x00000000007E0000,
	0x00000000330D8000,
	0x0000000000000000,
	0x0000000000000000,
	0x0000000000000000,
	0x0000000000000000,
	0x0000000000000000,
	0x0000000000630000,
	0x0000000000000000
};

#pragma endregion

#pragma region Events

static void window_ride_init_viewport(rct_window *w);

static void window_ride_main_mouseup(rct_window *w, int widgetIndex);
static void window_ride_main_resize(rct_window *w);
static void window_ride_main_mousedown(int widgetIndex, rct_window *w, rct_widget *widget);
static void window_ride_main_dropdown(rct_window *w, int widgetIndex, int dropdownIndex);
static void window_ride_main_update(rct_window *w);
static void window_ride_main_textinput(rct_window *w, int widgetIndex, char *text);
static void window_ride_main_unknown_14(rct_window *w);
static void window_ride_main_invalidate(rct_window *w);
static void window_ride_main_paint(rct_window *w, rct_drawpixelinfo *dpi);

static void window_ride_vehicle_mouseup(rct_window *w, int widgetIndex);
static void window_ride_vehicle_resize(rct_window *w);
static void window_ride_vehicle_mousedown(int widgetIndex, rct_window *w, rct_widget *widget);
static void window_ride_vehicle_dropdown(rct_window *w, int widgetIndex, int dropdownIndex);
static void window_ride_vehicle_update(rct_window *w);
static void window_ride_vehicle_invalidate(rct_window *w);
static void window_ride_vehicle_paint(rct_window *w, rct_drawpixelinfo *dpi);
static void window_ride_vehicle_scrollpaint(rct_window *w, rct_drawpixelinfo *dpi, int scrollIndex);

static void window_ride_operating_mouseup(rct_window *w, int widgetIndex);
static void window_ride_operating_resize(rct_window *w);
static void window_ride_operating_mousedown(int widgetIndex, rct_window *w, rct_widget *widget);
static void window_ride_operating_dropdown(rct_window *w, int widgetIndex, int dropdownIndex);
static void window_ride_operating_update(rct_window *w);
static void window_ride_operating_invalidate(rct_window *w);
static void window_ride_operating_paint(rct_window *w, rct_drawpixelinfo *dpi);

static void window_ride_maintenance_mouseup(rct_window *w, int widgetIndex);
static void window_ride_maintenance_resize(rct_window *w);
static void window_ride_maintenance_mousedown(int widgetIndex, rct_window *w, rct_widget *widget);
static void window_ride_maintenance_dropdown(rct_window *w, int widgetIndex, int dropdownIndex);
static void window_ride_maintenance_update(rct_window *w);
static void window_ride_maintenance_invalidate(rct_window *w);
static void window_ride_maintenance_paint(rct_window *w, rct_drawpixelinfo *dpi);

static void window_ride_colour_close(rct_window *w);
static void window_ride_colour_mouseup(rct_window *w, int widgetIndex);
static void window_ride_colour_resize(rct_window *w);
static void window_ride_colour_mousedown(int widgetIndex, rct_window *w, rct_widget *widget);
static void window_ride_colour_dropdown(rct_window *w, int widgetIndex, int dropdownIndex);
static void window_ride_colour_update(rct_window *w);
static void window_ride_colour_tooldown(rct_window *w, int widgetIndex, int x, int y);
static void window_ride_colour_tooldrag(rct_window *w, int widgetIndex, int x, int y);
static void window_ride_colour_invalidate(rct_window *w);
static void window_ride_colour_paint(rct_window *w, rct_drawpixelinfo *dpi);
static void window_ride_colour_scrollpaint(rct_window *w, rct_drawpixelinfo *dpi, int scrollIndex);

static void window_ride_music_mouseup(rct_window *w, int widgetIndex);
static void window_ride_music_resize(rct_window *w);
static void window_ride_music_mousedown(int widgetIndex, rct_window *w, rct_widget *widget);
static void window_ride_music_dropdown(rct_window *w, int widgetIndex, int dropdownIndex);
static void window_ride_music_update(rct_window *w);
static void window_ride_music_invalidate(rct_window *w);
static void window_ride_music_paint(rct_window *w, rct_drawpixelinfo *dpi);

static void window_ride_measurements_close(rct_window *w);
static void window_ride_measurements_mouseup(rct_window *w, int widgetIndex);
static void window_ride_measurements_resize(rct_window *w);
static void window_ride_measurements_mousedown(int widgetIndex, rct_window *w, rct_widget *widget);
static void window_ride_measurements_dropdown(rct_window *w, int widgetIndex, int dropdownIndex);
static void window_ride_measurements_update(rct_window *w);
static void window_ride_measurements_tooldown(rct_window *w, int widgetIndex, int x, int y);
static void window_ride_measurements_toolabort(rct_window *w, int widgetIndex);
static void window_ride_measurements_invalidate(rct_window *w);
static void window_ride_measurements_paint(rct_window *w, rct_drawpixelinfo *dpi);

static void window_ride_graphs_mouseup(rct_window *w, int widgetIndex);
static void window_ride_graphs_resize(rct_window *w);
static void window_ride_graphs_mousedown(int widgetIndex, rct_window *w, rct_widget *widget);
static void window_ride_graphs_update(rct_window *w);
static void window_ride_graphs_scrollgetheight(rct_window *w, int scrollIndex, int *width, int *height);
static void window_ride_graphs_15(rct_window *w, int scrollIndex, int scrollAreaType);
static void window_ride_graphs_tooltip(rct_window* w, int widgetIndex, rct_string_id *stringId);
static void window_ride_graphs_invalidate(rct_window *w);
static void window_ride_graphs_paint(rct_window *w, rct_drawpixelinfo *dpi);
static void window_ride_graphs_scrollpaint(rct_window *w, rct_drawpixelinfo *dpi, int scrollIndex);

static void window_ride_income_mouseup(rct_window *w, int widgetIndex);
static void window_ride_income_resize(rct_window *w);
static void window_ride_income_mousedown(int widgetIndex, rct_window *w, rct_widget *widget);
static void window_ride_income_update(rct_window *w);
static void window_ride_income_invalidate(rct_window *w);
static void window_ride_income_paint(rct_window *w, rct_drawpixelinfo *dpi);

static void window_ride_customer_mouseup(rct_window *w, int widgetIndex);
static void window_ride_customer_resize(rct_window *w);
static void window_ride_customer_update(rct_window *w);
static void window_ride_customer_invalidate(rct_window *w);
static void window_ride_customer_paint(rct_window *w, rct_drawpixelinfo *dpi);

static void window_ride_set_colours();

// 0x0098DFD4
static rct_window_event_list window_ride_main_events = {
	NULL,
	window_ride_main_mouseup,
	window_ride_main_resize,
	window_ride_main_mousedown,
	window_ride_main_dropdown,
	NULL,
	window_ride_main_update,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	window_ride_main_textinput,
	window_ride_main_unknown_14,
	NULL,
	NULL,
	NULL,
	NULL,
	window_ride_main_invalidate,
	window_ride_main_paint,
	NULL
};

// 0x0098E204
static rct_window_event_list window_ride_vehicle_events = {
	NULL,
	window_ride_vehicle_mouseup,
	window_ride_vehicle_resize,
	window_ride_vehicle_mousedown,
	window_ride_vehicle_dropdown,
	NULL,
	window_ride_vehicle_update,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	window_ride_vehicle_invalidate,
	window_ride_vehicle_paint,
	window_ride_vehicle_scrollpaint
};

// 0x0098E0B4
static rct_window_event_list window_ride_operating_events = {
	NULL,
	window_ride_operating_mouseup,
	window_ride_operating_resize,
	window_ride_operating_mousedown,
	window_ride_operating_dropdown,
	NULL,
	window_ride_operating_update,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	window_ride_operating_invalidate,
	window_ride_operating_paint,
	NULL
};

// 0x0098E124
static rct_window_event_list window_ride_maintenance_events = {
	NULL,
	window_ride_maintenance_mouseup,
	window_ride_maintenance_resize,
	window_ride_maintenance_mousedown,
	window_ride_maintenance_dropdown,
	NULL,
	window_ride_maintenance_update,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	window_ride_maintenance_invalidate,
	window_ride_maintenance_paint,
	NULL
};

// 0x0098E044
static rct_window_event_list window_ride_colour_events = {
	window_ride_colour_close,
	window_ride_colour_mouseup,
	window_ride_colour_resize,
	window_ride_colour_mousedown,
	window_ride_colour_dropdown,
	NULL,
	window_ride_colour_update,
	NULL,
	NULL,
	NULL,
	window_ride_colour_tooldown,
	window_ride_colour_tooldrag,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	window_ride_colour_invalidate,
	window_ride_colour_paint,
	window_ride_colour_scrollpaint
};

// 0x0098E194
static rct_window_event_list window_ride_music_events = {
	NULL,
	window_ride_music_mouseup,
	window_ride_music_resize,
	window_ride_music_mousedown,
	window_ride_music_dropdown,
	NULL,
	window_ride_music_update,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	window_ride_music_invalidate,
	window_ride_music_paint,
	NULL
};

// 0x0098DE14
static rct_window_event_list window_ride_measurements_events = {
	window_ride_measurements_close,
	window_ride_measurements_mouseup,
	window_ride_measurements_resize,
	window_ride_measurements_mousedown,
	window_ride_measurements_dropdown,
	NULL,
	window_ride_measurements_update,
	NULL,
	NULL,
	NULL,
	window_ride_measurements_tooldown,
	NULL,
	NULL,
	window_ride_measurements_toolabort,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	window_ride_measurements_invalidate,
	window_ride_measurements_paint,
	NULL
};

// 0x0098DF64
static rct_window_event_list window_ride_graphs_events = {
	NULL,
	window_ride_graphs_mouseup,
	window_ride_graphs_resize,
	window_ride_graphs_mousedown,
	NULL,
	NULL,
	window_ride_graphs_update,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	window_ride_graphs_scrollgetheight,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	window_ride_graphs_15,
	window_ride_graphs_tooltip,
	NULL,
	NULL,
	window_ride_graphs_invalidate,
	window_ride_graphs_paint,
	window_ride_graphs_scrollpaint
};

// 0x0098DEF4
static rct_window_event_list window_ride_income_events = {
	NULL,
	window_ride_income_mouseup,
	window_ride_income_resize,
	window_ride_income_mousedown,
	NULL,
	NULL,
	window_ride_income_update,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	window_ride_income_invalidate,
	window_ride_income_paint,
	NULL
};

// 0x0098DE84
static rct_window_event_list window_ride_customer_events = {
	NULL,
	window_ride_customer_mouseup,
	window_ride_customer_resize,
	NULL,
	NULL,
	NULL,
	window_ride_customer_update,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	window_ride_customer_invalidate,
	window_ride_customer_paint,
	NULL
};

static rct_window_event_list *window_ride_page_events[] = {
	&window_ride_main_events,
	&window_ride_vehicle_events,
	&window_ride_operating_events,
	&window_ride_maintenance_events,
	&window_ride_colour_events,
	&window_ride_music_events,
	&window_ride_measurements_events,
	&window_ride_graphs_events,
	&window_ride_income_events,
	&window_ride_customer_events
};

#pragma endregion

// Cached overall view for each ride
// (Re)calculated when the ride window is opened
typedef struct ride_overall_view_t {
	sint16 x, y, z;
	uint8 zoom;
} ride_overall_view;

ride_overall_view ride_overall_views[MAX_RIDES] = {0};

const int window_ride_tab_animation_divisor[] = { 0, 0, 2, 2, 4, 2, 8, 8, 2, 0 };
const int window_ride_tab_animation_frames[] = { 0, 0, 4, 16, 8, 16, 8, 8, 8, 0 };

static void window_ride_draw_tab_image(rct_drawpixelinfo *dpi, rct_window *w, int page, int spriteIndex)
{
	int widgetIndex = WIDX_TAB_1 + page;

	if (!(w->disabled_widgets & (1LL << widgetIndex))) {
		if (w->page == page) {
			int frame = w->frame_no / window_ride_tab_animation_divisor[w->page];
			spriteIndex += (frame % window_ride_tab_animation_frames[w->page]);
		}

		gfx_draw_sprite(dpi, spriteIndex, w->x + w->widgets[widgetIndex].left, w->y + w->widgets[widgetIndex].top, 0);
	}
}

/**
 *
 *  rct2: 0x006B2E88
 */
static void window_ride_draw_tab_main(rct_drawpixelinfo *dpi, rct_window *w)
{
	int widgetIndex = WIDX_TAB_1 + WINDOW_RIDE_PAGE_MAIN;

	if (!(w->disabled_widgets & (1LL << widgetIndex))) {
		int spriteIndex = 0;
		int rideType = get_ride(w->number)->type;

		switch (gRideClassifications[rideType]) {
		case RIDE_CLASS_RIDE:
			spriteIndex = SPR_TAB_RIDE_0;
			if (w->page == WINDOW_RIDE_PAGE_MAIN)
				spriteIndex += (w->frame_no / 4) % 16;
			break;
		case RIDE_CLASS_SHOP_OR_STALL:
			spriteIndex = SPR_TAB_SHOPS_AND_STALLS_0;
			if (w->page == WINDOW_RIDE_PAGE_MAIN)
				spriteIndex += (w->frame_no / 4) % 16;
			break;
		case RIDE_CLASS_KIOSK_OR_FACILITY:
			spriteIndex = SPR_TAB_KIOSKS_AND_FACILITIES_0;
			if (w->page == WINDOW_RIDE_PAGE_MAIN)
				spriteIndex += (w->frame_no / 4) % 8;
			break;
		}

		gfx_draw_sprite(dpi, spriteIndex, w->x + w->widgets[widgetIndex].left, w->y + w->widgets[widgetIndex].top, 0);
	}
}

/**
 *
 *  rct2: 0x006B2B68
 */
static void window_ride_draw_tab_vehicle(rct_drawpixelinfo *dpi, rct_window *w)
{
	rct_ride *ride;
	rct_widget *widget;
	int widgetIndex, spriteIndex, x, y, width, height;
	rct_ride_entry *rideEntry;
	vehicle_colour vehicleColour;

	widgetIndex = WIDX_TAB_1 + WINDOW_RIDE_PAGE_VEHICLE;
	widget = &w->widgets[widgetIndex];

	if (!(w->disabled_widgets & (1LL << widgetIndex))) {
		x = widget->left + 1;
		y = widget->top + 1;
		width = widget->right - x;
		height = widget->bottom - 3 - y;
		if (w->page == WINDOW_RIDE_PAGE_VEHICLE)
			height += 4;

		x += w->x;
		y += w->y;

		rct_drawpixelinfo clipDPI;
		if (!clip_drawpixelinfo(&clipDPI, dpi, x, y, width, height)) {
			return;
		}

		x = (widget->right - widget->left) / 2;
		y = (widget->bottom - widget->top) - 12;

		ride = get_ride(w->number);

		rideEntry = get_ride_entry_by_ride(ride);
		if (rideEntry->flags & RIDE_ENTRY_FLAG_0) {
			clipDPI.zoom_level = 1;
			clipDPI.width *= 2;
			clipDPI.height *= 2;
			x *= 2;
			y *= 2;
			clipDPI.x *= 2;
			clipDPI.y *= 2;
		}

		const uint8 vehicle = ride_entry_get_vehicle_at_position(ride->subtype, ride->num_cars_per_train, rideEntry->tab_vehicle);
		rct_ride_entry_vehicle* rideVehicleEntry = &rideEntry->vehicles[vehicle];
		height += rideVehicleEntry->tab_height;

		vehicleColour = ride_get_vehicle_colour(ride, 0);

		spriteIndex = 32;
		if (w->page == WINDOW_RIDE_PAGE_VEHICLE)
			spriteIndex += w->frame_no;
		spriteIndex /= (rideVehicleEntry->flags_a & VEHICLE_ENTRY_FLAG_A_11) ? 4 : 2;
		spriteIndex &= rideVehicleEntry->rotation_frame_mask;
		spriteIndex *= rideVehicleEntry->var_16;
		spriteIndex += rideVehicleEntry->base_image_id;
		spriteIndex |= (vehicleColour.additional_1 << 24) | (vehicleColour.main << 19);
		spriteIndex |= 0x80000000;

		gfx_draw_sprite(&clipDPI, spriteIndex, x, y, vehicleColour.additional_2);
	}
}

/**
 *
 *  rct2: 0x006B2F42
 */
static void window_ride_draw_tab_customer(rct_drawpixelinfo *dpi, rct_window *w)
{
	int spriteIndex;
	int widgetIndex = WIDX_TAB_1 + WINDOW_RIDE_PAGE_CUSTOMER;
	rct_widget *widget = &w->widgets[widgetIndex];

	if (!(w->disabled_widgets & (1LL << widgetIndex))) {
		spriteIndex = 0;
		if (w->page == WINDOW_RIDE_PAGE_CUSTOMER)
			spriteIndex = w->var_492 & ~3;

		spriteIndex += g_sprite_entries[0].sprite_image->base_image;
		spriteIndex += 1;
		spriteIndex |= 0xA9E00000;

		gfx_draw_sprite(dpi, spriteIndex, w->x + (widget->left + widget->right) / 2, w->y + widget->bottom - 6, 0);
	}
}

/**
 *
 *  rct2: 0x006B2B35
 */
static void window_ride_draw_tab_images(rct_drawpixelinfo *dpi, rct_window *w)
{
	window_ride_draw_tab_vehicle(dpi, w);
	window_ride_draw_tab_image(dpi, w, WINDOW_RIDE_PAGE_OPERATING, 5201);
	window_ride_draw_tab_image(dpi, w, WINDOW_RIDE_PAGE_MAINTENANCE, 5205);
	window_ride_draw_tab_image(dpi, w, WINDOW_RIDE_PAGE_INCOME, SPR_TAB_ADMISSION_0);
	window_ride_draw_tab_main(dpi, w);
	window_ride_draw_tab_image(dpi, w, WINDOW_RIDE_PAGE_MEASUREMENTS, 5229);
	window_ride_draw_tab_image(dpi, w, WINDOW_RIDE_PAGE_COLOUR, 5221);
	window_ride_draw_tab_image(dpi, w, WINDOW_RIDE_PAGE_GRAPHS, 5237);
	window_ride_draw_tab_customer(dpi, w);
	window_ride_draw_tab_image(dpi, w, WINDOW_RIDE_PAGE_MUSIC, 5335);
}

/**
*
* rct2: 0x006AEB9F
*/
void window_ride_disable_tabs(rct_window *w)
{
	uint32 disabled_tabs = 0;
	rct_ride *ride = get_ride(w->number & 0xFF);

	uint8 ride_type = ride->type; // ecx

	if (!ride_type_has_flag(ride_type, RIDE_TYPE_FLAG_HAS_DATA_LOGGING))
		disabled_tabs |= (1 << WIDX_TAB_8); // 0x800

	if (ride_type == RIDE_TYPE_MINI_GOLF)
		disabled_tabs |= (1 << WIDX_TAB_2 | 1 << WIDX_TAB_3 | 1 << WIDX_TAB_4); // 0xE0

	if (ride_type_has_flag(ride_type, RIDE_TYPE_FLAG_13))
		disabled_tabs |= (1 << WIDX_TAB_2); // 0x20

	if (
		!ride_type_has_flag(ride_type, RIDE_TYPE_FLAG_HAS_TRACK_COLOUR_MAIN) &&
		!ride_type_has_flag(ride_type, RIDE_TYPE_FLAG_HAS_TRACK_COLOUR_ADDITIONAL) &&
		!ride_type_has_flag(ride_type, RIDE_TYPE_FLAG_HAS_TRACK_COLOUR_SUPPORTS) &&
		!ride_type_has_flag(ride_type, RIDE_TYPE_FLAG_26) &&
		!(RideData4[ride->type].flags & RIDE_TYPE_FLAG4_HAS_ENTRANCE_EXIT)
	) {
		disabled_tabs |= (1 << WIDX_TAB_5); // 0x100
	}

	if (ride_type_has_flag(ride_type, RIDE_TYPE_FLAG_IS_SHOP))
		disabled_tabs |= (1 << WIDX_TAB_3 | 1 << WIDX_TAB_4 | 1 << WIDX_TAB_7); // 0x4C0

	if (!(RideData4[ride->type].flags & RIDE_TYPE_FLAG4_ALLOW_MUSIC)) {
		disabled_tabs |= (1 << WIDX_TAB_6); // 0x200
	}

	if (ride_type == RIDE_TYPE_CASH_MACHINE ||
		ride_type == RIDE_TYPE_FIRST_AID ||
		(gParkFlags & PARK_FLAGS_NO_MONEY) != 0)
		disabled_tabs |= (1 << WIDX_TAB_9); // 0x1000

	if ((gScreenFlags & SCREEN_FLAGS_TRACK_DESIGNER) != 0)
		disabled_tabs |= (1 << WIDX_TAB_4 | 1 << WIDX_TAB_6 | 1 << WIDX_TAB_9 | 1 << WIDX_TAB_10); // 0x3280

	rct_ride_entry *type = get_ride_entry(ride->subtype);

	if (type == NULL) {
		disabled_tabs |= 1 << WIDX_TAB_2 | 1 << WIDX_TAB_3 | 1 << WIDX_TAB_4 | 1 << WIDX_TAB_5 | 1 << WIDX_TAB_6
					   | 1 << WIDX_TAB_7 | 1 << WIDX_TAB_8 | 1 << WIDX_TAB_9 | 1 << WIDX_TAB_10;
	} else if ((type->flags & RIDE_ENTRY_FLAG_19) != 0)
		disabled_tabs |= (1 << WIDX_TAB_5); // 0x100

	w->disabled_widgets = disabled_tabs;
}

static void window_ride_update_overall_view(uint8 ride_index) {
	// Calculate x, y, z bounds of the entire ride using its track elements
	map_element_iterator it;

	map_element_iterator_begin(&it);

	int minx = INT_MAX, miny = INT_MAX, minz = INT_MAX;
	int maxx = INT_MIN, maxy = INT_MIN, maxz = INT_MIN;

	while (map_element_iterator_next(&it)) {
		if (map_element_get_type(it.element) != MAP_ELEMENT_TYPE_TRACK)
			continue;

		if (it.element->properties.track.ride_index != ride_index)
			continue;

		int x = it.x * 32;
		int y = it.y * 32;
		int z1 = it.element->base_height * 8;
		int z2 = it.element->clearance_height * 8;

		minx = min(minx, x);
		miny = min(miny, y);
		minz = min(minz, z1);

		maxx = max(maxx, x);
		maxy = max(maxy, y);
		maxz = max(maxz, z2);
	}

	ride_overall_view *view = &ride_overall_views[ride_index];
	view->x = (minx + maxx) / 2;
	view->y = (miny + maxy) / 2;
	view->z = (minz + maxz) / 2 + 8;

	// Calculate size to determine from how far away to view the ride
	int dx = maxx - minx;
	int dy = maxy - miny;
	int dz = maxz - minz;

	int size = (int) sqrt(dx*dx + dy*dy + dz*dz);

	// Each farther zoom level shows twice as many tiles (log)
	// Appropriate zoom is lowered by one to fill the entire view with the ride
	double zoom = ceil(log(size / 80)) - 1;
	view->zoom = (uint8)clamp(0, zoom, 3);
}

/**
 *
 *  rct2: 0x006AEAB4
 */
rct_window *window_ride_open(int rideIndex)
{
	rct_window *w;
	rct_ride *ride;
	uint8 *rideEntryIndexPtr;
	int numSubTypes;

	w = window_create_auto_pos(316, 207, window_ride_page_events[0], WC_RIDE, WF_10 | WF_RESIZABLE);
	w->widgets = window_ride_page_widgets[0];
	w->enabled_widgets = window_ride_page_enabled_widgets[0];
	w->number = rideIndex;

	w->page = 0;
	w->var_48C = 0;
	w->frame_no = 0;
	w->list_information_type = 0;
	w->var_492 = 0;
	w->ride_colour = 0;
	window_ride_disable_tabs(w);
	w->min_width = 316;
	w->min_height = 180;
	w->max_width = 500;
	w->max_height = 450;

	window_ride_update_overall_view((uint8) rideIndex);

	ride = get_ride(rideIndex);
	numSubTypes = 0;
	rideEntryIndexPtr = get_ride_entry_indices_for_ride_type(ride->type);
	for (; *rideEntryIndexPtr != 0xFF; rideEntryIndexPtr++) {
		if (ride_entry_is_invented(*rideEntryIndexPtr)) {
			numSubTypes++;
		}
	}
	var_496(w) = numSubTypes;
	return w;
}

/**
 *
 *  rct2: 0x006ACC28
 */
rct_window *window_ride_main_open(int rideIndex)
{
	rct_window *w;

	w = window_bring_to_front_by_number(WC_RIDE, rideIndex);
	if (w == NULL) {
		w = window_ride_open(rideIndex);
		w->ride.var_482 = -1;
	}

	if (gInputFlags & INPUT_FLAG_TOOL_ACTIVE) {
		if (w->classification == gCurrentToolWidget.window_classification &&
			w->number == gCurrentToolWidget.window_number
		) {
			tool_cancel();
		}
	}

	w->page = WINDOW_RIDE_PAGE_MAIN;
	w->width = 316;
	w->height = 180;
	window_invalidate(w);
	w->widgets = window_ride_page_widgets[WINDOW_RIDE_PAGE_MAIN];
	w->enabled_widgets = window_ride_page_enabled_widgets[WINDOW_RIDE_PAGE_MAIN];
	w->hold_down_widgets = window_ride_page_hold_down_widgets[WINDOW_RIDE_PAGE_MAIN];
	w->event_handlers = window_ride_page_events[WINDOW_RIDE_PAGE_MAIN];
	w->pressed_widgets = 0;
	window_ride_disable_tabs(w);
	window_init_scroll_widgets(w);
	w->ride.view = 0;
	window_ride_init_viewport(w);

	return w;
}

/**
 *
 *  rct2: 0x006ACCCE
 */
rct_window *window_ride_open_station(int rideIndex, int stationIndex)
{
	int i;
	rct_ride *ride;
	rct_window *w;

	ride = get_ride(rideIndex);

	if (ride_type_has_flag(ride->type, RIDE_TYPE_FLAG_13))
		return window_ride_main_open(rideIndex);

	w = window_bring_to_front_by_number(WC_RIDE, rideIndex);
	if (w == NULL) {
		w = window_ride_open(rideIndex);
		w->ride.var_482 = -1;
	}

	if (
		gInputFlags & INPUT_FLAG_TOOL_ACTIVE &&
		gCurrentToolWidget.window_classification == w->classification &&
		gCurrentToolWidget.window_number == w->number
	) {
		tool_cancel();
	}

	w->page = WINDOW_RIDE_PAGE_MAIN;
	w->width = 316;
	w->height = 180;
	window_invalidate(w);

	w->widgets = window_ride_page_widgets[w->page];
	w->enabled_widgets = window_ride_page_enabled_widgets[w->page];
	w->hold_down_widgets = window_ride_page_hold_down_widgets[w->page];
	w->event_handlers = window_ride_page_events[w->page];
	w->pressed_widgets = 0;
	window_ride_disable_tabs(w);
	window_init_scroll_widgets(w);

	// View
	for (i = stationIndex; i >= 0; i--) {
		if (ride->station_starts[i] == 0xFFFF)
			stationIndex--;
	}

	w->ride.view = 1 + ride->num_vehicles + stationIndex;
	window_ride_init_viewport(w);

	return w;
}

rct_window *window_ride_open_track(rct_map_element *mapElement)
{
	int rideIndex = mapElement->properties.track.ride_index;
	if (
		(map_element_get_type(mapElement) == MAP_ELEMENT_TYPE_ENTRANCE) ||
		(RCT2_ADDRESS(0x0099BA64, uint8)[mapElement->properties.track.type * 16] & 0x10)
	) {
		// Open ride window in station view
		return window_ride_open_station(rideIndex, map_get_station(mapElement));
	} else {
		// Open ride window in overview mode.
		return window_ride_main_open(rideIndex);
	}
}

/**
 *
 *  rct2: 0x006ACAC2
 */
rct_window *window_ride_open_vehicle(rct_vehicle *vehicle)
{
	int i, rideIndex, view, numPeepsLeft, openedPeepWindow;
	uint16 headVehicleSpriteIndex, peepSpriteIndex;
	rct_ride *ride;
	rct_vehicle *headVehicle;
	rct_window *w, *w2;

	headVehicle = vehicle_get_head(vehicle);
	headVehicleSpriteIndex = headVehicle->sprite_index;
	rideIndex = headVehicle->ride;
	ride = get_ride(rideIndex);

	// Get view index
	view = 1;
	for (i = 0; i < 32; i++) {
		if (ride->vehicles[i] == headVehicleSpriteIndex)
			break;

		view++;
	}


	w = window_find_by_number(WC_RIDE, rideIndex);
	if (w != NULL) {
		window_invalidate(w);

		if (
			gInputFlags & INPUT_FLAG_TOOL_ACTIVE &&
			gCurrentToolWidget.window_classification == w->classification &&
			gCurrentToolWidget.window_number == w->number
		) {
			tool_cancel();
		}

		openedPeepWindow = 0;
		if (w->ride.view == view) {
			numPeepsLeft = vehicle->num_peeps;
			for (i = 0; i < 32 && numPeepsLeft > 0; i++) {
				peepSpriteIndex = vehicle->peep[i];
				if (peepSpriteIndex == SPRITE_INDEX_NULL)
					continue;

				numPeepsLeft--;
				w2 = window_find_by_number(WC_PEEP, peepSpriteIndex);
				if (w2 == NULL) {
					rct_peep *peep = &(g_sprite_list[peepSpriteIndex].peep);
					window_guest_open(peep);
					openedPeepWindow = 1;

					break;
				}
			}
		}

		w = openedPeepWindow ?
			window_find_by_number(WC_RIDE, rideIndex) :
			window_bring_to_front_by_number(WC_RIDE, rideIndex);
	}

	if (w == NULL) {
		w = window_ride_open(rideIndex);
		w->ride.var_482 = -1;
	}

	w->page = WINDOW_RIDE_PAGE_MAIN;
	w->width = 316;
	w->height = 180;
	window_invalidate(w);

	w->widgets = window_ride_page_widgets[w->page];
	w->enabled_widgets = window_ride_page_enabled_widgets[w->page];
	w->hold_down_widgets = window_ride_page_hold_down_widgets[w->page];
	w->event_handlers = window_ride_page_events[w->page];
	w->pressed_widgets = 0;
	window_ride_disable_tabs(w);
	window_init_scroll_widgets(w);

	w->ride.view = view;
	window_ride_init_viewport(w);
	window_invalidate(w);

	return w;
}

/**
 *
 *  rct2: 0x006AF1D2
 */
static void window_ride_set_page(rct_window *w, int page)
{
	int listen;

	if (gInputFlags & INPUT_FLAG_TOOL_ACTIVE)
		if (w->classification == gCurrentToolWidget.window_classification && w->number == gCurrentToolWidget.window_number)
			tool_cancel();

	// Set listen only to viewport
	listen = 0;
	if (page == WINDOW_RIDE_PAGE_MAIN && w->page == WINDOW_RIDE_PAGE_MAIN && w->viewport != NULL && !(w->viewport->flags & VIEWPORT_FLAG_SOUND_ON))
		listen++;

	w->page = page;
	w->frame_no = 0;
	w->var_492 = 0;

	//There doesn't seem to be any need for this call, and it can sometimes modify the reported number of cars per train, so I've removed it
	//if (page == WINDOW_RIDE_PAGE_VEHICLE) {
		//ride_update_max_vehicles(w->number);
	//}

	if (w->viewport != NULL) {
		w->viewport->width = 0;
		w->viewport = NULL;
	}

	w->enabled_widgets = window_ride_page_enabled_widgets[page];
	w->hold_down_widgets = window_ride_page_hold_down_widgets[page];
	w->event_handlers = window_ride_page_events[page];
	w->pressed_widgets = 0;
	w->widgets = window_ride_page_widgets[page];
	window_ride_disable_tabs(w);
	window_invalidate(w);

	window_event_resize_call(w);
	window_event_invalidate_call(w);
	window_init_scroll_widgets(w);
	window_invalidate(w);

	if (listen != 0 && w->viewport != NULL)
		w->viewport->flags |= VIEWPORT_FLAG_SOUND_ON;
}

static void window_ride_set_pressed_tab(rct_window *w)
{
	int i;
	for (i = 0; i < WINDOW_RIDE_PAGE_COUNT; i++)
		w->pressed_widgets &= ~(1 << (WIDX_TAB_1 + i));
	w->pressed_widgets |= 1LL << (WIDX_TAB_1 + w->page);
}

static void window_ride_anchor_border_widgets(rct_window *w)
{
	w->widgets[WIDX_BACKGROUND].right = w->width - 1;
	w->widgets[WIDX_BACKGROUND].bottom = w->height - 1;
	w->widgets[WIDX_PAGE_BACKGROUND].right = w->width - 1;
	w->widgets[WIDX_PAGE_BACKGROUND].bottom = w->height - 1;
	w->widgets[WIDX_TITLE].right = w->width - 2;
	w->widgets[WIDX_CLOSE].left = w->width - 13;
	w->widgets[WIDX_CLOSE].right = w->width - 3;
}

#pragma region Main

/**
 *
 *  rct2: 0x006AF994
 */
static void window_ride_init_viewport(rct_window *w)
{
	if (w->page != WINDOW_RIDE_PAGE_MAIN) return;

	rct_ride* ride = get_ride(w->number);
	int eax = w->viewport_focus_coordinates.var_480 - 1;

	union{
		sprite_focus sprite;
		coordinate_focus coordinate;
	} focus;

	focus.coordinate.x = 0;
	focus.coordinate.y = 0;
	focus.coordinate.z = 0;
	focus.sprite.sprite_id = -1;
	focus.coordinate.zoom = 0;
	focus.coordinate.rotation = get_current_rotation();


	if (eax >= 0 && eax < ride->num_vehicles && ride->lifecycle_flags & RIDE_LIFECYCLE_ON_TRACK){
		focus.sprite.sprite_id = ride->vehicles[eax];

		rct_ride_entry* ride_entry = get_ride_entry_by_ride(ride);
		if (ride_entry->tab_vehicle != 0){
			rct_vehicle* vehicle = GET_VEHICLE(focus.sprite.sprite_id);
			focus.sprite.sprite_id = vehicle->next_vehicle_on_train;
		}
		focus.sprite.type |= 0xC0;
	}
	else if (eax >= ride->num_vehicles && eax < (ride->num_vehicles + ride->num_stations)){
		int stationIndex = -1;
		int count = eax - ride->num_vehicles;
		do {
			stationIndex++;
			if (ride->station_starts[stationIndex] != 0xFFFF)
				count--;
		} while (count >= 0);

		eax = ride->station_starts[stationIndex];

		focus.coordinate.x = (eax & 0xFF) << 5;
		focus.coordinate.y = (eax & 0xFF00) >> 3;
		focus.coordinate.z = ride->station_heights[stationIndex] << 3;
		focus.sprite.type |= 0x40;
	}
	else{
		if (eax > 0){
			w->viewport_focus_coordinates.var_480 = 0;
		}

		ride_overall_view *view = &ride_overall_views[w->number];

		focus.coordinate.x = view->x;
		focus.coordinate.y = view->y;
		focus.coordinate.z = view->z;
		focus.coordinate.zoom = view->zoom;

		focus.sprite.type |= 0x40;
	}
	focus.coordinate.var_480 = w->viewport_focus_coordinates.var_480;

	uint16 viewport_flags = 0;

	if (w->viewport != 0){
		if (focus.coordinate.x == w->viewport_focus_coordinates.x &&
			focus.coordinate.y == w->viewport_focus_coordinates.y &&
			focus.coordinate.z == w->viewport_focus_coordinates.z &&
			focus.coordinate.rotation == w->viewport_focus_coordinates.rotation &&
			focus.coordinate.zoom == w->viewport_focus_coordinates.zoom )
			return;
		viewport_flags = w->viewport->flags;
		w->viewport->width = 0;
		w->viewport = 0;

		viewport_update_pointers();
	}
	else{
		if (RCT2_GLOBAL(RCT2_ADDRESS_CONFIG_FLAGS, uint8) & 0x1)
			viewport_flags |= VIEWPORT_FLAG_GRIDLINES;
	}

	window_event_invalidate_call(w);

	w->viewport_focus_coordinates.x = focus.coordinate.x;
	w->viewport_focus_coordinates.y = focus.coordinate.y;
	w->viewport_focus_coordinates.z = focus.coordinate.z;
	w->viewport_focus_coordinates.rotation = focus.coordinate.rotation;
	w->viewport_focus_coordinates.zoom = focus.coordinate.zoom;

	//rct2: 0x006aec9c only used here so brought it into the function
	if (!w->viewport && ride->overall_view != 0xFFFF){
		rct_widget* view_widget = &w->widgets[WIDX_VIEWPORT];

		int x = view_widget->left + 1 + w->x;
		int y = view_widget->top + 1 + w->y;
		int width = view_widget->right - view_widget->left - 1;
		int height = view_widget->bottom - view_widget->top - 1;
		viewport_create(
			w,
			x,
			y,
			width,
			height,
			focus.coordinate.zoom,
			focus.coordinate.x,
			focus.coordinate.y & VIEWPORT_FOCUS_Y_MASK,
			focus.coordinate.z,
			focus.sprite.type & VIEWPORT_FOCUS_TYPE_MASK,
			focus.sprite.sprite_id);

		w->flags |= WF_NO_SCROLLING;
		window_invalidate(w);
	}
	if (w->viewport){
		w->viewport->flags = viewport_flags;
		window_invalidate(w);
	}
}

/**
 *
 *  rct2: 0x006AF315
 */
static void window_ride_rename(rct_window *w)
{
	rct_ride *ride;

	ride = get_ride(w->number);
	set_format_arg(16, uint32, ride->name_arguments);
	window_text_input_open(w, WIDX_RENAME, STR_RIDE_ATTRACTION_NAME, STR_ENTER_NEW_NAME_FOR_THIS_RIDE_ATTRACTION, ride->name, ride->name_arguments, 32);
}

/**
 *
 *  rct2: 0x006AF17E
 */
static void window_ride_main_mouseup(rct_window *w, int widgetIndex)
{
	uint8 rideIndex;
	rct_ride *ride;
	int status;

	switch (widgetIndex) {
	case WIDX_CLOSE:
		window_close(w);
		break;
	case WIDX_TAB_1:
	case WIDX_TAB_2:
	case WIDX_TAB_3:
	case WIDX_TAB_4:
	case WIDX_TAB_5:
	case WIDX_TAB_6:
	case WIDX_TAB_7:
	case WIDX_TAB_8:
	case WIDX_TAB_9:
	case WIDX_TAB_10:
		window_ride_set_page(w, widgetIndex - WIDX_TAB_1);
		break;
	case WIDX_CONSTRUCTION:
		rideIndex = (uint8)w->number;
		ride_construct(rideIndex);
		if (window_find_by_number(WC_RIDE_CONSTRUCTION, rideIndex) != NULL) {
			window_close(w);
		}
		break;
	case WIDX_RENAME:
		window_ride_rename(w);
		break;
	case WIDX_LOCATE:
		window_scroll_to_viewport(w);
		break;
	case WIDX_DEMOLISH:
		window_ride_demolish_prompt_open(w->number);
		break;
	case WIDX_CLOSE_LIGHT:
	case WIDX_TEST_LIGHT:
	case WIDX_OPEN_LIGHT:

		ride = get_ride(w->number);

		switch (widgetIndex - WIDX_CLOSE_LIGHT) {
		case 0:
			status = RIDE_STATUS_CLOSED;
			gGameCommandErrorTitle = STR_CANT_CLOSE;
			break;
		case 1:
			status = RIDE_STATUS_TESTING;
			gGameCommandErrorTitle = STR_CANT_TEST;
			break;
		case 2:
			status = RIDE_STATUS_OPEN;
			gGameCommandErrorTitle = STR_CANT_OPEN;
			break;
		}

		set_format_arg(6, uint16, ride->name);
		set_format_arg(8, uint32, ride->name_arguments);
		ride_set_status(w->number, status);
		break;
	}
}

/**
 *
 *  rct2: 0x006AF4A2
 */
static void window_ride_main_resize(rct_window *w)
{
	rct_viewport *viewport;
	int width, height;

	w->flags |= WF_RESIZABLE;
	int minHeight = 180;
	if (theme_get_flags() & UITHEME_FLAG_USE_LIGHTS_RIDE)
		minHeight = 200 + RCT1_LIGHT_OFFSET - (ride_type_has_flag(get_ride(w->number)->type, RIDE_TYPE_FLAG_NO_TEST_MODE) ? 14 : 0);
	window_set_resize(w, 316, minHeight, 500, 450);

	viewport = w->viewport;
	if (viewport != NULL) {
		width = w->width - 30;
		height = w->height - 75;
		if (viewport->width != width || viewport->height != height) {
			viewport->width = width;
			viewport->height = height;
			viewport->view_width = width << viewport->zoom;
			viewport->view_height = height << viewport->zoom;
		}
	}

	window_ride_init_viewport(w);
}

/**
 *
 *  rct2: 0x006AF825
 */
static void window_ride_show_view_dropdown(rct_window *w, rct_widget *widget)
{
	rct_widget *dropdownWidget;
	rct_ride *ride;
	int numItems, currentItem, i, j, name;

	dropdownWidget = widget - 1;
	ride = get_ride(w->number);

	numItems = 1;
	if (!ride_type_has_flag(ride->type, RIDE_TYPE_FLAG_13)) {
		numItems += ride->num_stations;
		numItems += ride->num_vehicles;
	}

	window_dropdown_show_text_custom_width(
		w->x + dropdownWidget->left,
		w->y + dropdownWidget->top,
		dropdownWidget->bottom - dropdownWidget->top + 1,
		w->colours[1],
		0,
		numItems,
		widget->right - dropdownWidget->left
	);

	// First item
	gDropdownItemsFormat[0] = STR_DROPDOWN_MENU_LABEL;
	gDropdownItemsArgs[0] = STR_OVERALL_VIEW;
	currentItem = 1;

	// Vehicles
	name = RideNameConvention[ride->type].vehicle_name + 6;
	for (i = 1; i <= ride->num_vehicles; i++) {
		gDropdownItemsFormat[currentItem] = STR_DROPDOWN_MENU_LABEL;
		gDropdownItemsArgs[currentItem] = name | (currentItem << 16);
		currentItem++;
	}

	// Stations
	name = RideNameConvention[ride->type].station_name + 6;
	for (i = 1; i <= ride->num_stations; i++) {
		gDropdownItemsFormat[currentItem] = STR_DROPDOWN_MENU_LABEL;
		gDropdownItemsArgs[currentItem] = name | (i << 16);
		currentItem++;
	}

	// Set highlighted item
	if (!(ride->lifecycle_flags & RIDE_LIFECYCLE_ON_TRACK)) {
		j = 2;
		for (i = 0; i < ride->num_vehicles; i++) {
			gDropdownItemsDisabled |= j;
			j <<= 1;
		}
	}

	// Set checked item
	dropdown_set_checked(w->ride.view, true);
}

/**
 *
 *  rct2: 0x006AF64C
 */
static void window_ride_show_open_dropdown(rct_window *w, rct_widget *widget)
{
	rct_ride *ride;
	int numItems, highlightedIndex = 0, checkedIndex;

	ride = get_ride(w->number);

	numItems = 0;
	gDropdownItemsFormat[numItems] = STR_DROPDOWN_MENU_LABEL;
	gDropdownItemsArgs[numItems] = STR_CLOSE_RIDE;
	numItems++;

	if (!ride_type_has_flag(ride->type, RIDE_TYPE_FLAG_NO_TEST_MODE)) {
		gDropdownItemsFormat[numItems] = STR_DROPDOWN_MENU_LABEL;
		gDropdownItemsArgs[numItems] = STR_TEST_RIDE;
		numItems++;
	}

	gDropdownItemsFormat[numItems] = STR_DROPDOWN_MENU_LABEL;
	gDropdownItemsArgs[numItems] = STR_OPEN_RIDE;
	numItems++;

	window_dropdown_show_text(
		w->x + widget->left,
		w->y + widget->top,
		widget->bottom - widget->top + 1,
		w->colours[1],
		0,
		numItems
	);

	checkedIndex = ride->status;
	switch (ride->status) {
	case RIDE_STATUS_CLOSED:
		highlightedIndex = 0;
		if ((ride->lifecycle_flags & RIDE_LIFECYCLE_CRASHED) || (ride->lifecycle_flags & RIDE_LIFECYCLE_11))
			break;

		highlightedIndex = 2;
		if (ride_type_has_flag(ride->type, RIDE_TYPE_FLAG_NO_TEST_MODE))
			break;
		if (ride->lifecycle_flags & RIDE_LIFECYCLE_TESTED)
			break;

		highlightedIndex = 1;
		break;
	case RIDE_STATUS_TESTING:
		highlightedIndex = 2;
		if (ride->lifecycle_flags & RIDE_LIFECYCLE_TESTED)
			break;

		highlightedIndex = 0;
		break;
	case RIDE_STATUS_OPEN:
		highlightedIndex = 0;
		break;
	}

	if (checkedIndex != RIDE_STATUS_CLOSED)
		checkedIndex = 3 - checkedIndex;

	if (ride_type_has_flag(ride->type, RIDE_TYPE_FLAG_NO_TEST_MODE)) {
		if (checkedIndex != 0)
			checkedIndex--;
		if (highlightedIndex != 0)
			highlightedIndex--;
	}

	dropdown_set_checked(checkedIndex, true);
	gDropdownDefaultIndex = highlightedIndex;
}

/**
 *
 *  rct2: 0x006AF1BD
 */
static void window_ride_main_mousedown(int widgetIndex, rct_window *w, rct_widget *widget)
{
	switch (widgetIndex) {
	case WIDX_VIEW_DROPDOWN:
		window_ride_show_view_dropdown(w, widget);
		break;
	case WIDX_OPEN:
		window_ride_show_open_dropdown(w, widget);
		break;
	}
}

/**
 *
 *  rct2: 0x006AF300
 */
static void window_ride_main_dropdown(rct_window *w, int widgetIndex, int dropdownIndex)
{
	rct_ride *ride;
	int status = 0;

	switch (widgetIndex) {
	case WIDX_VIEW_DROPDOWN:
		if (dropdownIndex == -1) {
			dropdownIndex = w->ride.view;
			ride = get_ride(w->number);
			dropdownIndex++;
			if (dropdownIndex != 0 && dropdownIndex <= ride->num_vehicles && !(ride->lifecycle_flags & RIDE_LIFECYCLE_ON_TRACK))
				dropdownIndex = ride->num_vehicles + 1;

			if (dropdownIndex >= gDropdownNumItems)
				dropdownIndex = 0;
		}

		w->ride.view = dropdownIndex;
		window_ride_init_viewport(w);
		window_invalidate(w);
		break;
	case WIDX_OPEN:
		if (dropdownIndex == -1)
			dropdownIndex = gDropdownHighlightedIndex;

		ride = get_ride(w->number);
		if (ride_type_has_flag(ride->type, RIDE_TYPE_FLAG_NO_TEST_MODE) && dropdownIndex != 0)
			dropdownIndex++;

		switch (dropdownIndex) {
		case 0:
			status = RIDE_STATUS_CLOSED;
			gGameCommandErrorTitle = STR_CANT_CLOSE;
			break;
		case 1:
			status = RIDE_STATUS_TESTING;
			gGameCommandErrorTitle = STR_CANT_TEST;
			break;
		case 2:
			status = RIDE_STATUS_OPEN;
			gGameCommandErrorTitle = STR_CANT_OPEN;
			break;
		}

		set_format_arg(6, uint16, ride->name);
		set_format_arg(8, uint32, ride->name_arguments);
		ride_set_status(w->number, status);
		break;
	}
}

/**
 *
 *  rct2: 0x006AF40F
 */
static void window_ride_main_update(rct_window *w)
{
	rct_ride *ride;
	int vehicleIndex;
	uint16 vehicleSpriteIndex;
	rct_vehicle *vehicle;

	// Update tab animation
	w->frame_no++;
	window_event_invalidate_call(w);
	widget_invalidate(w, WIDX_TAB_1);

	// Update status
	ride = get_ride(w->number);
	if (!(ride->window_invalidate_flags & RIDE_INVALIDATE_RIDE_MAIN)) {
		if (w->ride.view == 0)
			return;

		if (w->ride.view <= ride->num_vehicles) {
			vehicleIndex = w->ride.view - 1;
			vehicleSpriteIndex = ride->vehicles[vehicleIndex];
			if (vehicleSpriteIndex == 0xFFFF)
				return;

			vehicle = &(g_sprite_list[vehicleSpriteIndex].vehicle);
			if (
				vehicle->status != 4 &&
				vehicle->status != 22 &&
				vehicle->status != 10 &&
				vehicle->status != 7
			) {
				return;
			}
		}
	}

	ride->window_invalidate_flags &= ~RIDE_INVALIDATE_RIDE_MAIN;
	widget_invalidate(w, WIDX_STATUS);
}

/**
 *
 *  rct2: 0x006AF2F9
 */
static void window_ride_main_textinput(rct_window *w, int widgetIndex, char *text)
{
	if (widgetIndex != WIDX_RENAME || text == NULL)
		return;

	ride_set_name(w->number, text);
}

/**
 *
 *  rct2: 0x006AF55A
 */
static void window_ride_main_unknown_14(rct_window *w)
{
	window_ride_init_viewport(w);
}

/**
 *
 *  rct2: 0x006AECF6
 */
static void window_ride_main_invalidate(rct_window *w)
{
	rct_widget *widgets;
	int i, height;

	colour_scheme_update(w);

	widgets = window_ride_page_widgets[w->page];
	if (w->widgets != widgets) {
		w->widgets = widgets;
		window_init_scroll_widgets(w);
	}

	window_ride_set_pressed_tab(w);

	rct_ride *ride = get_ride(w->number);
	w->disabled_widgets &= ~((1 << 22) | (1 << 19));
	if (ride->lifecycle_flags & (RIDE_LIFECYCLE_INDESTRUCTIBLE | RIDE_LIFECYCLE_INDESTRUCTIBLE_TRACK))
		w->disabled_widgets |= (1 << 22);

	set_format_arg(0, uint16, ride->name);
	set_format_arg(2, uint32, ride->name_arguments);
	window_ride_main_widgets[WIDX_OPEN].image = SPR_CLOSED + ride->status;

	window_ride_main_widgets[WIDX_CLOSE_LIGHT].image = SPR_G2_RCT1_CLOSE_BUTTON_0 + (ride->status == RIDE_STATUS_CLOSED) * 2 + widget_is_pressed(w, WIDX_CLOSE_LIGHT);
	window_ride_main_widgets[WIDX_TEST_LIGHT].image = SPR_G2_RCT1_TEST_BUTTON_0 + (ride->status == RIDE_STATUS_TESTING) * 2 + widget_is_pressed(w, WIDX_TEST_LIGHT);
	window_ride_main_widgets[WIDX_OPEN_LIGHT].image = SPR_G2_RCT1_OPEN_BUTTON_0 + (ride->status == RIDE_STATUS_OPEN) * 2 + widget_is_pressed(w, WIDX_OPEN_LIGHT);

	window_ride_anchor_border_widgets(w);

	// Anchor main page specific widgets
	window_ride_main_widgets[WIDX_VIEWPORT].right = w->width - 26;
	window_ride_main_widgets[WIDX_VIEWPORT].bottom = w->height - 14;
	window_ride_main_widgets[WIDX_STATUS].right = w->width - 26;
	window_ride_main_widgets[WIDX_STATUS].top = w->height - 13;
	window_ride_main_widgets[WIDX_STATUS].bottom = w->height - 3;
	window_ride_main_widgets[WIDX_VIEW].right = w->width - 60;
	window_ride_main_widgets[WIDX_VIEW_DROPDOWN].right = w->width - 61;
	window_ride_main_widgets[WIDX_VIEW_DROPDOWN].left = w->width - 71;

	window_align_tabs(w, WIDX_TAB_1, WIDX_TAB_10);

	if (theme_get_flags() & UITHEME_FLAG_USE_LIGHTS_RIDE) {
		window_ride_main_widgets[WIDX_OPEN].type = WWT_EMPTY;
		window_ride_main_widgets[WIDX_CLOSE_LIGHT].type = WWT_IMGBTN;
		window_ride_main_widgets[WIDX_TEST_LIGHT].type = (ride_type_has_flag(ride->type, RIDE_TYPE_FLAG_NO_TEST_MODE) ? WWT_EMPTY : WWT_IMGBTN);
		window_ride_main_widgets[WIDX_OPEN_LIGHT].type = WWT_IMGBTN;

		height = 62;
		if (window_ride_main_widgets[WIDX_TEST_LIGHT].type != WWT_EMPTY) {
			window_ride_main_widgets[WIDX_TEST_LIGHT].top = height;
			window_ride_main_widgets[WIDX_TEST_LIGHT].bottom = height + 13;
			height += 14;
		}
		window_ride_main_widgets[WIDX_OPEN_LIGHT].top = height;
		window_ride_main_widgets[WIDX_OPEN_LIGHT].bottom = height + 13;
		height += 14 - 24 + RCT1_LIGHT_OFFSET;

		w->min_height = 200 + RCT1_LIGHT_OFFSET - (ride_type_has_flag(ride->type, RIDE_TYPE_FLAG_NO_TEST_MODE) ? 14 : 0);
		if (w->height < w->min_height)
			window_event_resize_call(w);
	}
	else {
		window_ride_main_widgets[WIDX_OPEN].type = WWT_FLATBTN;
		window_ride_main_widgets[WIDX_CLOSE_LIGHT].type = WWT_EMPTY;
		window_ride_main_widgets[WIDX_TEST_LIGHT].type = WWT_EMPTY;
		window_ride_main_widgets[WIDX_OPEN_LIGHT].type = WWT_EMPTY;
		height = 46;
	}
	for (i = WIDX_CLOSE_LIGHT; i <= WIDX_OPEN_LIGHT; i++) {
		window_ride_main_widgets[i].left = w->width - 20;
		window_ride_main_widgets[i].right = w->width - 7;
	}
	for (i = WIDX_OPEN; i <= WIDX_DEMOLISH; i++, height += 24) {
		window_ride_main_widgets[i].left = w->width - 25;
		window_ride_main_widgets[i].right = w->width - 2;
		window_ride_main_widgets[i].top = height;
		window_ride_main_widgets[i].bottom = height + 23;
	}
}

/**
 *
 *  rct2: 0x006AF10A
 */
static rct_string_id window_ride_get_status_overall_view(rct_window *w, void *arguments)
{
	int formatSecondary, argument;
	rct_string_id stringId;

	ride_get_status(w->number, &formatSecondary, &argument);
	RCT2_GLOBAL((int)arguments + 0, uint16) = formatSecondary;
	RCT2_GLOBAL((int)arguments + 2, uint32) = argument;
	stringId = STR_LOSS;
	if (formatSecondary != STR_BROKEN_DOWN && formatSecondary != STR_CRASHED)
		stringId = STR_BLACK_STRING;

	return stringId;
}

/**
 *
 *  rct2: 0x006AEFEF
 */
static rct_string_id window_ride_get_status_vehicle(rct_window *w, void *arguments)
{
	rct_ride *ride;
	rct_vehicle *vehicle;
	int vehicleIndex;
	uint16 vehicleSpriteIndex;
	rct_string_id stringId;

	ride = get_ride(w->number);

	vehicleIndex = w->ride.view - 1;
	vehicleSpriteIndex = ride->vehicles[vehicleIndex];
	if (vehicleSpriteIndex == 0xFFFF)
		return 0;

	vehicle = &(g_sprite_list[vehicleSpriteIndex].vehicle);
	if (vehicle->status != VEHICLE_STATUS_CRASHING && vehicle->status != VEHICLE_STATUS_CRASHED) {
		int trackType = vehicle->track_type >> 2;
		if (trackType == TRACK_ELEM_BLOCK_BRAKES ||
			trackType == TRACK_ELEM_CABLE_LIFT_HILL ||
			trackType == TRACK_ELEM_25_DEG_UP_TO_FLAT ||
			trackType == TRACK_ELEM_60_DEG_UP_TO_FLAT ||
			trackType == TRACK_ELEM_DIAG_25_DEG_UP_TO_FLAT ||
			trackType == TRACK_ELEM_DIAG_60_DEG_UP_TO_FLAT) {
			if (track_type_is_invented(ride->type, TRACK_BLOCK_BRAKES) && vehicle->velocity == 0) {
				RCT2_GLOBAL((int)arguments + 0, uint16) = STR_STOPPED_BY_BLOCK_BRAKES;
				return STR_BLACK_STRING;
			}
		}
	}

	stringId = STR_MOVING_TO_END_OF + vehicle->status;

	// Get speed in mph
	RCT2_GLOBAL((int)arguments + 2, uint16) = (abs(vehicle->velocity) * 9) >> 18;

	if (ride->type == RIDE_TYPE_MINI_GOLF)
		return 0;

	if ((RideData4[ride->type].flags & RIDE_TYPE_FLAG4_SINGLE_SESSION) && stringId <= STR_UNLOADING_PASSENGERS_AT)
		stringId += 23;

	RCT2_GLOBAL((int)arguments + 4, uint16) = RideNameConvention[ride->type].station_name;
	RCT2_GLOBAL((int)arguments + 6, uint16) = vehicle->current_station + 1;
	if (ride->num_stations > 1)
		RCT2_GLOBAL((int)arguments + 4, uint16) += 6;

	RCT2_GLOBAL((int)arguments + 0, uint16) = stringId;
	return stringId != STR_CRASHING && stringId != STR_CRASHED_0 ? STR_BLACK_STRING : STR_LOSS;
}

/**
 *
 *  rct2: 0x006AEF65
 */
static rct_string_id window_ride_get_status_station(rct_window *w, void *arguments)
{
	rct_ride *ride;
	int stationIndex, count, queueLength;
	rct_string_id stringId;

	ride = get_ride(w->number);

	count = w->ride.view - ride->num_vehicles - 1;
	stationIndex = -1;
	do {
		stationIndex++;
		if (ride->station_starts[stationIndex] != 0xFFFF)
			count--;
	} while (count >= 0);

	stringId = 0;

	// Entrance / exit
	if (ride->status == RIDE_STATUS_CLOSED) {
		if (ride->entrances[stationIndex] == 0xFFFF)
			stringId = STR_NO_ENTRANCE;
		else if (ride->exits[stationIndex] == 0xFFFF)
			stringId = STR_NO_EXIT;
	} else {
		if (ride->entrances[stationIndex] == 0xFFFF)
			stringId = STR_EXIT_ONLY;
	}

	// Queue length
	if (stringId == 0) {
		queueLength = ride->queue_length[stationIndex];
		RCT2_GLOBAL((int)arguments + 2, uint16) = queueLength;
		stringId = STR_QUEUE_EMPTY;
		if (queueLength == 1)
			stringId++;
		else if (queueLength > 1)
			stringId += 2;
	}

	RCT2_GLOBAL((int)arguments + 0, uint16) = stringId;
	return STR_BLACK_STRING;
}

/**
 *
 *  rct2: 0x006AEE73
 */
static rct_string_id window_ride_get_status(rct_window *w, void *arguments)
{
	rct_ride *ride = get_ride(w->number);

	if (w->ride.view == 0)
		return window_ride_get_status_overall_view(w, arguments);
	if (w->ride.view <= ride->num_vehicles)
		return window_ride_get_status_vehicle(w, arguments);
	if (ride->lifecycle_flags & RIDE_LIFECYCLE_BROKEN_DOWN)
		return window_ride_get_status_overall_view(w, arguments);
	return window_ride_get_status_station(w, arguments);
}

/**
 *
 *  rct2: 0x006AEE73
 */
static void window_ride_main_paint(rct_window *w, rct_drawpixelinfo *dpi)
{
	rct_ride *ride;
	rct_widget *widget;
	rct_string_id stringId;

	window_draw_widgets(w, dpi);
	window_ride_draw_tab_images(dpi, w);

	// Viewport and ear icon
	if (w->viewport != NULL) {
		window_draw_viewport(dpi, w);
		if (w->viewport->flags & VIEWPORT_FLAG_SOUND_ON)
			gfx_draw_sprite(dpi, SPR_HEARING_VIEWPORT, w->x + 2, w->y + 2, 0);
	}

	// View dropdown
	ride = get_ride(w->number);
	stringId = STR_OVERALL_VIEW;
	if (w->ride.view != 0) {
		stringId = RideNameConvention[ride->type].vehicle_name + 6;
		if (w->ride.view > ride->num_vehicles) {
			set_format_arg(2, uint16, w->ride.view - ride->num_vehicles);
			stringId = RideNameConvention[ride->type].station_name + 6;
		} else {
			set_format_arg(2, uint16, w->ride.view);
		}
	}
	set_format_arg(0, uint16, stringId);

	widget = &window_ride_main_widgets[WIDX_VIEW];
	gfx_draw_string_centred(
		dpi,
		STR_WINDOW_COLOUR_2_STRING,
		w->x + (widget->left + widget->right - 11) / 2,
		w->y + widget->top,
		0,
		gCommonFormatArgs
	);

	// Status
	widget = &window_ride_main_widgets[WIDX_STATUS];
	gfx_draw_string_centred_clipped(
		dpi,
		window_ride_get_status(w, gCommonFormatArgs),
		gCommonFormatArgs,
		0,
		w->x + (widget->left + widget->right) / 2,
		w->y + widget->top,
		widget->right - widget->left
	);
}

#pragma endregion

#pragma region Vehicle

/**
 *
 *  rct2: 0x006B272D
 */
static void window_ride_vehicle_mouseup(rct_window *w, int widgetIndex)
{
	rct_ride *ride;

	ride = get_ride(w->number);

	switch (widgetIndex) {
	case WIDX_CLOSE:
		window_close(w);
		break;
	case WIDX_TAB_1:
	case WIDX_TAB_2:
	case WIDX_TAB_3:
	case WIDX_TAB_4:
	case WIDX_TAB_5:
	case WIDX_TAB_6:
	case WIDX_TAB_7:
	case WIDX_TAB_8:
	case WIDX_TAB_9:
	case WIDX_TAB_10:
		window_ride_set_page(w, widgetIndex - WIDX_TAB_1);
		break;
	}
}

/**
 *
 *  rct2: 0x006B2ABB
 */
static void window_ride_vehicle_resize(rct_window *w)
{
	window_set_resize(w, 316, 208, 316, 208);
}

/**
 *
 *  rct2: 0x006B2748
 */
static void window_ride_vehicle_mousedown(int widgetIndex, rct_window *w, rct_widget *widget)
{
	rct_widget *dropdownWidget = widget - 1;
	rct_ride *ride;
	rct_ride_entry *rideEntry, *currentRideEntry;
	int numItems, rideEntryIndex, selectedIndex, rideTypeIterator, rideTypeIteratorMax;
	uint8 *rideEntryIndexPtr;
	bool selectionShouldBeExpanded;

	ride = get_ride(w->number);
	rideEntry = get_ride_entry_by_ride(ride);

	if(gCheatsShowVehiclesFromOtherTrackTypes && !(ride_type_has_flag(ride->type, RIDE_TYPE_FLAG_FLAT_RIDE) || ride->type==RIDE_TYPE_MAZE || ride->type==RIDE_TYPE_MINI_GOLF)) {
		selectionShouldBeExpanded=true;
		rideTypeIterator=0;
		rideTypeIteratorMax=90;
	}
	else {
		selectionShouldBeExpanded=false;
		rideTypeIterator=ride->type;
		rideTypeIteratorMax=ride->type;
	}

	switch (widgetIndex) {
	case WIDX_VEHICLE_TYPE_DROPDOWN:
		selectedIndex = -1;
		numItems = 0;

		// Dropdowns with more items start acting weird, so cap it to 63.
		for (; rideTypeIterator<=rideTypeIteratorMax && numItems<=63; rideTypeIterator++) {

			if(selectionShouldBeExpanded && ride_type_has_flag(rideTypeIterator, RIDE_TYPE_FLAG_FLAT_RIDE))
				continue;
			if(selectionShouldBeExpanded && (rideTypeIterator==RIDE_TYPE_MAZE || rideTypeIterator==RIDE_TYPE_MINI_GOLF))
				continue;

			rideEntryIndexPtr = get_ride_entry_indices_for_ride_type(rideTypeIterator);

			for (uint8 *currentRideEntryIndex = rideEntryIndexPtr; *currentRideEntryIndex != 0xFF && numItems <= 63; currentRideEntryIndex++) {
				rideEntryIndex = *currentRideEntryIndex;
				currentRideEntry = get_ride_entry(rideEntryIndex);
				// Skip if vehicle has the same track type, but not same subtype, unless subtype switching is enabled
				if ((currentRideEntry->flags & (RIDE_ENTRY_FLAG_SEPARATE_RIDE | RIDE_ENTRY_FLAG_SEPARATE_RIDE_NAME)) && !(gConfigInterface.select_by_track_type || selectionShouldBeExpanded))
					continue;

				// Skip if vehicle type is not invented yet
				if (!ride_entry_is_invented(rideEntryIndex))
					continue;

				if (ride->subtype == rideEntryIndex)
					selectedIndex = numItems;

				gDropdownItemsFormat[numItems] = STR_DROPDOWN_MENU_LABEL;
				gDropdownItemsArgs[numItems] = (rideEntryIndex << 16) | currentRideEntry->name;

				numItems++;
			}
		}

		window_dropdown_show_text_custom_width(
			w->x + dropdownWidget->left,
			w->y + dropdownWidget->top,
			dropdownWidget->bottom - dropdownWidget->top + 1,
			w->colours[1],
			DROPDOWN_FLAG_STAY_OPEN,
			numItems,
			widget->right - dropdownWidget->left
		);

		dropdown_set_checked(selectedIndex, true);
		break;
	case WIDX_VEHICLE_TRAINS_INCREASE:
		if (ride->num_vehicles < 32)
			ride_set_num_vehicles(w->number, ride->num_vehicles + 1);
		break;
	case WIDX_VEHICLE_TRAINS_DECREASE:
		if (ride->num_vehicles > 1)
			ride_set_num_vehicles(w->number, ride->num_vehicles - 1);
		break;
	case WIDX_VEHICLE_CARS_PER_TRAIN_INCREASE:
		if (ride->num_cars_per_train < 255)
			ride_set_num_cars_per_vehicle(w->number, ride->num_cars_per_train + 1);
		break;
	case WIDX_VEHICLE_CARS_PER_TRAIN_DECREASE:
		if (ride->num_cars_per_train > rideEntry->zero_cars + 1)
			ride_set_num_cars_per_vehicle(w->number, ride->num_cars_per_train - 1);
		break;
	}
}

/**
 *
 *  rct2: 0x006B2767
 */
static void window_ride_vehicle_dropdown(rct_window *w, int widgetIndex, int dropdownIndex)
{
	rct_ride *ride;
	rct_ride_entry *rideEntry;

	if (dropdownIndex == -1)
		return;

	ride = get_ride(w->number);
	rideEntry = get_ride_entry_by_ride(ride);

	switch (widgetIndex) {
	case WIDX_VEHICLE_TYPE_DROPDOWN:
		dropdownIndex = (gDropdownItemsArgs[dropdownIndex] >> 16) & 0xFFFF;
		ride_set_ride_entry(w->number, dropdownIndex);
		break;
	}
}

/**
 *
 *  rct2: 0x006B2AA1
 */
static void window_ride_vehicle_update(rct_window *w)
{
	w->frame_no++;
	window_event_invalidate_call(w);
	widget_invalidate(w, WIDX_TAB_2);
}

/**
 *
 *  rct2: 0x006B222C
 */
static void window_ride_vehicle_invalidate(rct_window *w)
{
	rct_widget *widgets;
	rct_ride *ride;
	rct_ride_entry *rideEntry;
	rct_string_id stringId;
	int carsPerTrain;

	colour_scheme_update(w);

	widgets = window_ride_page_widgets[w->page];
	if (w->widgets != widgets) {
		w->widgets = widgets;
		window_init_scroll_widgets(w);
	}

	window_ride_set_pressed_tab(w);

	ride = get_ride(w->number);
	rideEntry = get_ride_entry_by_ride(ride);

	set_format_arg(0, uint16, ride->name);
	set_format_arg(2, uint32, ride->name_arguments);

	// Widget setup
	carsPerTrain = ride->num_cars_per_train - rideEntry->zero_cars;

	// Vehicle type
	window_ride_vehicle_widgets[WIDX_VEHICLE_TYPE].image = rideEntry->name;
	// Always show a dropdown button when changing subtypes is allowed
	if ((var_496(w) <= 1 || (rideEntry->flags & RIDE_ENTRY_FLAG_SEPARATE_RIDE)) && !(gConfigInterface.select_by_track_type || gCheatsShowVehiclesFromOtherTrackTypes)) {
		window_ride_vehicle_widgets[WIDX_VEHICLE_TYPE].type = WWT_14;
		window_ride_vehicle_widgets[WIDX_VEHICLE_TYPE_DROPDOWN].type = WWT_EMPTY;
		w->enabled_widgets &= ~(1 << WIDX_VEHICLE_TYPE);
	} else {
		window_ride_vehicle_widgets[WIDX_VEHICLE_TYPE].type = WWT_DROPDOWN;
		window_ride_vehicle_widgets[WIDX_VEHICLE_TYPE_DROPDOWN].type = WWT_DROPDOWN_BUTTON;
		w->enabled_widgets |= (1 << WIDX_VEHICLE_TYPE);
	}

	// Trains
	if (rideEntry->cars_per_flat_ride > 1) {
		window_ride_vehicle_widgets[WIDX_VEHICLE_TRAINS].type = WWT_SPINNER;
		window_ride_vehicle_widgets[WIDX_VEHICLE_TRAINS_INCREASE].type = WWT_DROPDOWN_BUTTON;
		window_ride_vehicle_widgets[WIDX_VEHICLE_TRAINS_DECREASE].type = WWT_DROPDOWN_BUTTON;

	} else {
		window_ride_vehicle_widgets[WIDX_VEHICLE_TRAINS].type = WWT_EMPTY;
		window_ride_vehicle_widgets[WIDX_VEHICLE_TRAINS_INCREASE].type = WWT_EMPTY;
		window_ride_vehicle_widgets[WIDX_VEHICLE_TRAINS_DECREASE].type = WWT_EMPTY;
	}

	// Cars per train
	if (rideEntry->zero_cars + 1 < rideEntry->max_cars_in_train||gCheatsDisableTrainLengthLimit) {
		window_ride_vehicle_widgets[WIDX_VEHICLE_CARS_PER_TRAIN].type = WWT_SPINNER;
		window_ride_vehicle_widgets[WIDX_VEHICLE_CARS_PER_TRAIN_INCREASE].type = WWT_DROPDOWN_BUTTON;
		window_ride_vehicle_widgets[WIDX_VEHICLE_CARS_PER_TRAIN_DECREASE].type = WWT_DROPDOWN_BUTTON;
	} else {
		window_ride_vehicle_widgets[WIDX_VEHICLE_CARS_PER_TRAIN].type = WWT_EMPTY;
		window_ride_vehicle_widgets[WIDX_VEHICLE_CARS_PER_TRAIN_INCREASE].type = WWT_EMPTY;
		window_ride_vehicle_widgets[WIDX_VEHICLE_CARS_PER_TRAIN_DECREASE].type = WWT_EMPTY;

	}

	set_format_arg(6, uint16, carsPerTrain);
	stringId = RideNameConvention[ride->type].vehicle_name + 4;
	if (ride->num_vehicles > 1)
		stringId++;
	set_format_arg(8, uint16, stringId);
	set_format_arg(10, uint16, ride->num_vehicles);

	window_ride_anchor_border_widgets(w);
	window_align_tabs(w, WIDX_TAB_1, WIDX_TAB_10);

	if (ride->num_cars_per_train > (rideEntry->zero_cars + 1)) {
		window_ride_vehicle_widgets[WIDX_VEHICLE_CARS_PER_TRAIN].image = STR_X_CARS_PER_TRAIN;
	} else {
		window_ride_vehicle_widgets[WIDX_VEHICLE_CARS_PER_TRAIN].image = STR_1_CAR_PER_TRAIN;
	}
}

/**
 *
 *  rct2: 0x006B23DC
 */
static void window_ride_vehicle_paint(rct_window *w, rct_drawpixelinfo *dpi)
{
	rct_ride *ride;
	rct_ride_entry *rideEntry;
	rct_string_id stringId;
	int x, y;
	sint16 factor;

	window_draw_widgets(w, dpi);
	window_ride_draw_tab_images(dpi, w);

	ride = get_ride(w->number);
	rideEntry = get_ride_entry_by_ride(ride);

	x = w->x + 8;
	y = w->y + 64;

	// Description
	y += gfx_draw_string_left_wrapped(dpi, &rideEntry->description, x, y, 300, STR_BLACK_STRING, 0);
	y += 5;

	// Capacity
	stringId = rideEntry->description + 1;
	gfx_draw_string_left(dpi, STR_CAPACITY, &stringId, 0, x, y);
	y += 15;

	if ((!(rideEntry->flags & RIDE_ENTRY_FLAG_SEPARATE_RIDE) || rideTypeShouldLoseSeparateFlag(rideEntry)) && var_496(w) > 1) {
		// Excitement Factor
		factor = rideEntry->excitement_multipler;
		if (factor > 0) {
			gfx_draw_string_left(dpi, STR_EXCITEMENT_FACTOR, &factor, 0, x, y);
			y += 10;
		}

		// Intensity Factor
		factor = rideEntry->intensity_multipler;
		if (factor > 0) {
			gfx_draw_string_left(dpi, STR_INTENSITY_FACTOR, &factor, 0, x, y);
			y += 10;
		}

		// Nausea Factor
		factor = rideEntry->nausea_multipler;
		if (factor > 0) {
			gfx_draw_string_left(dpi, STR_NAUSEA_FACTOR, &factor, 0, x, y);
			y += 10;
		}
	}
}

typedef struct rct_vehichle_paintinfo {
	short x;
	short y;
	int sprite_index;
	int tertiary_colour;
} rct_vehichle_paintinfo;

rct_vehichle_paintinfo _sprites_to_draw[144];

/**
 *
 *  rct2: 0x006B2502
 */
static void window_ride_vehicle_scrollpaint(rct_window *w, rct_drawpixelinfo *dpi, int scrollIndex)
{
	rct_ride *ride;
	rct_ride_entry *rideEntry;
	rct_widget *widget;
	int x, y, startX, startY, i, j, vehicleColourIndex = 0, spriteIndex;
	rct_vehichle_paintinfo *nextSpriteToDraw, *current, tmp;
	vehicle_colour vehicleColour;

	ride = get_ride(w->number);
	rideEntry = get_ride_entry_by_ride(ride);

	// Background
	gfx_fill_rect(dpi, dpi->x, dpi->y, dpi->x + dpi->width, dpi->y + dpi->height, 12);

	widget = &window_ride_vehicle_widgets[WIDX_VEHICLE_TRAINS_PREVIEW];
	startX = max(2, ((widget->right - widget->left) - ((ride->num_vehicles - 1) * 36)) / 2 - 25);
	startY = widget->bottom - widget->top - 4;

	rct_ride_entry_vehicle* rideVehicleEntry = &rideEntry->vehicles[ride_entry_get_vehicle_at_position(ride->subtype, ride->num_cars_per_train, 0)];
	startY += rideVehicleEntry->tab_height;

	// For each train
	for (i = 0; i < ride->num_vehicles; i++) {
		nextSpriteToDraw = _sprites_to_draw;
		x = startX;
		y = startY;

		// For each car in train
		for (j = 0; j < ride->num_cars_per_train; j++) {
			rct_ride_entry_vehicle* rideVehicleEntry = &rideEntry->vehicles[ride_entry_get_vehicle_at_position(ride->subtype, ride->num_cars_per_train, j)];
			x += rideVehicleEntry->spacing / 17432;
			y -= (rideVehicleEntry->spacing / 2) / 17432;

			// Get colour of vehicle
			switch (ride->colour_scheme_type & 3) {
			case VEHICLE_COLOUR_SCHEME_SAME:
				vehicleColourIndex = 0;
				break;
			case VEHICLE_COLOUR_SCHEME_PER_TRAIN:
				vehicleColourIndex = i;
				break;
			case VEHICLE_COLOUR_SCHEME_PER_VEHICLE:
				vehicleColourIndex = j;
				break;
			}
			vehicleColour = ride_get_vehicle_colour(ride, vehicleColourIndex);

			spriteIndex = 16;
			if (rideVehicleEntry->flags_a & VEHICLE_ENTRY_FLAG_A_11)
				spriteIndex /= 2;

			spriteIndex &= rideVehicleEntry->rotation_frame_mask;
			spriteIndex *= rideVehicleEntry->var_16;
			spriteIndex += rideVehicleEntry->base_image_id;
			spriteIndex |= (vehicleColour.additional_1 << 24) | (vehicleColour.main << 19);
			spriteIndex |= 0x80000000;

			nextSpriteToDraw->x = x;
			nextSpriteToDraw->y = y;
			nextSpriteToDraw->sprite_index = spriteIndex;
			nextSpriteToDraw->tertiary_colour = vehicleColour.additional_2;
			nextSpriteToDraw++;

			x += rideVehicleEntry->spacing / 17432;
			y -= (rideVehicleEntry->spacing / 2) / 17432;
		}

		if (ride->type == RIDE_TYPE_REVERSER_ROLLER_COASTER) {
			tmp = *(nextSpriteToDraw - 1);
			*(nextSpriteToDraw - 1) = *(nextSpriteToDraw - 2);
			*(nextSpriteToDraw - 2) = tmp;
		}

		current = nextSpriteToDraw;
		while (--current >= _sprites_to_draw)
			gfx_draw_sprite(dpi, current->sprite_index, current->x, current->y, current->tertiary_colour);

		startX += 36;
	}
}

#pragma endregion

#pragma region Operating

static void set_operating_setting(int rideNumber, uint8 setting, uint8 value)
{
	gGameCommandErrorTitle = STR_CANT_CHANGE_OPERATING_MODE;
	game_do_command(0, (value << 8) | 1, 0, (setting << 8) | rideNumber, GAME_COMMAND_SET_RIDE_SETTING, 0, 0);
}

static void window_ride_mode_tweak_set(rct_window *w, uint8 value)
{
	rct_ride *ride = get_ride(w->number);

	gGameCommandErrorTitle = STR_CANT_CHANGE_LAUNCH_SPEED;
	if (ride->mode == RIDE_MODE_STATION_TO_STATION)
		gGameCommandErrorTitle = STR_CANT_CHANGE_SPEED;
	if (ride->mode == RIDE_MODE_RACE)
		gGameCommandErrorTitle = STR_CANT_CHANGE_NUMBER_OF_LAPS;
	if (ride_type_has_flag(ride->type, RIDE_TYPE_FLAG_13))
		gGameCommandErrorTitle = STR_CANT_CHANGE_THIS;
	if (ride->mode == RIDE_MODE_BUMPERCAR)
		gGameCommandErrorTitle = STR_CANT_CHANGE_TIME_LIMIT;
	if (ride->mode == RIDE_MODE_SWING)
		gGameCommandErrorTitle = STR_CANT_CHANGE_NUMBER_OF_SWINGS;
	if (ride->mode == RIDE_MODE_ROTATION)
		gGameCommandErrorTitle = STR_CANT_CHANGE_NUMBER_OF_ROTATIONS;
	if (
		ride->mode == RIDE_MODE_ROTATION ||
		ride->mode == RIDE_MODE_FORWARD_ROTATION ||
		ride->mode == RIDE_MODE_BACKWARD_ROTATION
	)
		gGameCommandErrorTitle = STR_CANT_CHANGE_NUMBER_OF_ROTATIONS;

	set_operating_setting(w->number, RIDE_SETTING_OPERATION_OPTION, value);
}

/**
 *
 *  rct2: 0x006B11D5
 */
static void window_ride_mode_tweak_increase(rct_window *w)
{
	rct_ride *ride = get_ride(w->number);

	uint8 maxValue = RCT2_GLOBAL(RCT2_ADDRESS_RIDE_FLAGS + (ride->type * 8) + 5, uint8);
	uint8 minValue = gCheatsFastLiftHill ? 0 : RCT2_GLOBAL(RCT2_ADDRESS_RIDE_FLAGS + (ride->type * 8) + 4, uint8);
	if (ride->mode == RIDE_MODE_MAZE) {
		// Allow 64 people in mazes under non-cheat settings. The old maximum of 16 was too little for even moderately big mazes.
		maxValue = 64;
	}
	if (gCheatsFastLiftHill) {
		maxValue = 255;
	}

	uint8 increment = ride->mode == RIDE_MODE_BUMPERCAR ? 10 : 1;

	window_ride_mode_tweak_set(w, clamp(minValue, ride->operation_option + increment, maxValue));
}

/**
 *
 *  rct2: 0x006B120A
 */
static void window_ride_mode_tweak_decrease(rct_window *w)
{
	rct_ride *ride = get_ride(w->number);

	uint8 maxValue = RCT2_GLOBAL(RCT2_ADDRESS_RIDE_FLAGS + (ride->type * 8) + 5, uint8);
	uint8 minValue = gCheatsFastLiftHill ? 0 : RCT2_GLOBAL(RCT2_ADDRESS_RIDE_FLAGS + (ride->type * 8) + 4, uint8);
	if (ride->mode == RIDE_MODE_MAZE) {
		// Allow 64 people in mazes under non-cheat settings. The old maximum of 16 was too little for even moderately big mazes.
		maxValue = 64;
	}
	if (gCheatsFastLiftHill) {
		maxValue = 255;
	}

	uint8 decrement = ride->mode == RIDE_MODE_BUMPERCAR ? 10 : 1;

	window_ride_mode_tweak_set(w, clamp(minValue, ride->operation_option - decrement, maxValue));
}

/**
 *
 *  rct2: 0x006B1631
 */
static void window_ride_mode_dropdown(rct_window *w, rct_widget *widget)
{
	rct_ride_entry *rideEntry;
	rct_widget *dropdownWidget;
	rct_ride *ride;
	const uint8 *availableModes, *mode;
	int i, numAvailableModes;

	dropdownWidget = widget - 1;
	ride = get_ride(w->number);
	rideEntry = get_ride_entry_by_ride(ride);

	// Seek to available modes for this ride
	availableModes = ride_seek_available_modes(ride);

	// Count number of available modes
	mode = availableModes;
	numAvailableModes = -1;
	do {
		numAvailableModes++;
	} while (*(mode++) != 255);

	// Hide the last operating mode if the vehicle is not intended for it.
	if (rideEntry->flags & RIDE_ENTRY_DISABLE_LAST_OPERATING_MODE && !gCheatsShowAllOperatingModes)
		numAvailableModes--;

	// If the vehicle is not intended for them, hide those two modes (these are usually (or perhaps always) both continuous circuit modes).
	if ((rideEntry->flags & RIDE_ENTRY_DISABLE_FIRST_TWO_OPERATING_MODES) && !gCheatsShowAllOperatingModes) {
		availableModes += 2;
		numAvailableModes -= 2;
	}

	// Create dropdown list
	for (i = 0; i < numAvailableModes; i++) {
		gDropdownItemsFormat[i] = STR_DROPDOWN_MENU_LABEL;
		gDropdownItemsArgs[i] = STR_RIDE_MODE_START + availableModes[i];
	}
	window_dropdown_show_text_custom_width(
		w->x + dropdownWidget->left,
		w->y + dropdownWidget->top,
		dropdownWidget->bottom - dropdownWidget->top + 1,
		w->colours[1],
		DROPDOWN_FLAG_STAY_OPEN,
		numAvailableModes,
		widget->right - dropdownWidget->left
	);

	// Set checked item
	for (i = 0; i < numAvailableModes; i++) {
		if (ride->mode == availableModes[i]) {
			dropdown_set_checked(i, true);
		}
	}
}

/**
 *
 *  rct2: 0x006B15C0
 */
static void window_ride_load_dropdown(rct_window *w, rct_widget *widget)
{
	rct_widget *dropdownWidget;
	int i;

	dropdownWidget = widget - 1;
	rct_ride *ride = get_ride(w->number);

	for (i = 0; i < 5; i++) {
		gDropdownItemsFormat[i] = STR_DROPDOWN_MENU_LABEL;
		gDropdownItemsArgs[i] = STR_QUARTER_LOAD + i;
	}
	window_dropdown_show_text_custom_width(
		w->x + dropdownWidget->left,
		w->y + dropdownWidget->top,
		dropdownWidget->bottom - dropdownWidget->top + 1,
		w->colours[1],
		DROPDOWN_FLAG_STAY_OPEN,
		5,
		widget->right - dropdownWidget->left
	);

	dropdown_set_checked(ride->depart_flags & RIDE_DEPART_WAIT_FOR_LOAD_MASK, true);
}

/**
 *
 *  rct2: 0x006B10A7
 */
static void window_ride_operating_mouseup(rct_window *w, int widgetIndex)
{
	rct_ride *ride;

	ride = get_ride(w->number);

	switch (widgetIndex) {
	case WIDX_CLOSE:
		window_close(w);
		break;
	case WIDX_TAB_1:
	case WIDX_TAB_2:
	case WIDX_TAB_3:
	case WIDX_TAB_4:
	case WIDX_TAB_5:
	case WIDX_TAB_6:
	case WIDX_TAB_7:
	case WIDX_TAB_8:
	case WIDX_TAB_9:
	case WIDX_TAB_10:
		window_ride_set_page(w, widgetIndex - WIDX_TAB_1);
		break;
	case WIDX_LOAD_CHECKBOX:
		set_operating_setting(w->number, RIDE_SETTING_DEPARTURE, ride->depart_flags ^ RIDE_DEPART_WAIT_FOR_LOAD);
		break;
	case WIDX_LEAVE_WHEN_ANOTHER_ARRIVES_CHECKBOX:
		set_operating_setting(w->number, RIDE_SETTING_DEPARTURE, ride->depart_flags ^ RIDE_DEPART_LEAVE_WHEN_ANOTHER_ARRIVES);
		break;
	case WIDX_MINIMUM_LENGTH_CHECKBOX:
		set_operating_setting(w->number, RIDE_SETTING_DEPARTURE, ride->depart_flags ^ RIDE_DEPART_WAIT_FOR_MINIMUM_LENGTH);
		break;
	case WIDX_MAXIMUM_LENGTH_CHECKBOX:
		set_operating_setting(w->number, RIDE_SETTING_DEPARTURE, ride->depart_flags ^ RIDE_DEPART_WAIT_FOR_MAXIMUM_LENGTH);
		break;
	case WIDX_SYNCHRONISE_WITH_ADJACENT_STATIONS_CHECKBOX:
		set_operating_setting(w->number, RIDE_SETTING_DEPARTURE, ride->depart_flags ^ RIDE_DEPART_SYNCHRONISE_WITH_ADJACENT_STATIONS);
		break;
	}
}

/**
 *
 *  rct2: 0x006B1715
 */
static void window_ride_operating_resize(rct_window *w)
{
	window_set_resize(w, 316, 186, 316, 186);
}

/**
 *
 *  rct2: 0x006B10F4
 */
static void window_ride_operating_mousedown(int widgetIndex, rct_window *w, rct_widget *widget)
{
	rct_ride *ride = get_ride(w->number);
	uint8 upper_bound, lower_bound;

	switch (widgetIndex) {
	case WIDX_MODE_TWEAK_INCREASE:
		window_ride_mode_tweak_increase(w);
		break;
	case WIDX_MODE_TWEAK_DECREASE:
		window_ride_mode_tweak_decrease(w);
		break;
	case WIDX_LIFT_HILL_SPEED_INCREASE:
		upper_bound = gCheatsFastLiftHill ? 255 : RideLiftData[ride->type].maximum_speed;
		lower_bound = gCheatsFastLiftHill ? 0 : RideLiftData[ride->type].minimum_speed;
		set_operating_setting(w->number, RIDE_SETTING_LIFT_HILL_SPEED, clamp(lower_bound, ride->lift_hill_speed + 1, upper_bound));

		break;
	case WIDX_LIFT_HILL_SPEED_DECREASE:
		upper_bound = gCheatsFastLiftHill ? 255 : RideLiftData[ride->type].maximum_speed;
		lower_bound = gCheatsFastLiftHill ? 0 : RideLiftData[ride->type].minimum_speed;
		set_operating_setting(w->number, RIDE_SETTING_LIFT_HILL_SPEED, clamp(lower_bound, ride->lift_hill_speed - 1, upper_bound));
		break;
	case WIDX_MINIMUM_LENGTH_INCREASE:
		upper_bound = 250;
		lower_bound = 0;
		set_operating_setting(w->number, RIDE_SETTING_MIN_WAITING_TIME, clamp(lower_bound, ride->min_waiting_time + 1, upper_bound));
		break;
	case WIDX_MINIMUM_LENGTH_DECREASE:
		upper_bound = 250;
		lower_bound = 0;
		set_operating_setting(w->number, RIDE_SETTING_MIN_WAITING_TIME, clamp(lower_bound, ride->min_waiting_time - 1, upper_bound));
		break;
	case WIDX_MAXIMUM_LENGTH_INCREASE:
		upper_bound = 250;
		lower_bound = 0;
		set_operating_setting(w->number, RIDE_SETTING_MAX_WAITING_TIME, clamp(lower_bound, ride->max_waiting_time + 1, upper_bound));
		break;
	case WIDX_MAXIMUM_LENGTH_DECREASE:
		upper_bound = 250;
		lower_bound = 0;
		set_operating_setting(w->number, RIDE_SETTING_MAX_WAITING_TIME, clamp(lower_bound, ride->max_waiting_time - 1, upper_bound));
		break;
	case WIDX_MODE_DROPDOWN:
		window_ride_mode_dropdown(w, widget);
		break;
	case WIDX_LOAD_DROPDOWN:
		window_ride_load_dropdown(w, widget);
		break;
	case WIDX_OPERATE_NUMBER_OF_CIRCUITS_INCREASE:
		upper_bound = gCheatsFastLiftHill ? 255 : 20;
		lower_bound = 1;
		set_operating_setting(w->number, RIDE_SETTING_NUM_CIRCUITS, clamp(lower_bound, ride->num_circuits + 1, upper_bound));
		break;
	case WIDX_OPERATE_NUMBER_OF_CIRCUITS_DECREASE:
		upper_bound = gCheatsFastLiftHill ? 255 : 20;
		lower_bound = 1;
		set_operating_setting(w->number, RIDE_SETTING_NUM_CIRCUITS, clamp(lower_bound, ride->num_circuits - 1, upper_bound));
		break;
	}
}

/**
 *
 *  rct2: 0x006B1165
 */
static void window_ride_operating_dropdown(rct_window *w, int widgetIndex, int dropdownIndex)
{
	rct_ride *ride;
	const uint8 *availableModes;

	if (dropdownIndex == -1)
		return;

	ride = get_ride(w->number);

	switch (widgetIndex) {
	case WIDX_MODE_DROPDOWN:
		// Seek to available modes for this ride
		availableModes = ride_seek_available_modes(ride);

		set_operating_setting(w->number, RIDE_SETTING_MODE, availableModes[dropdownIndex]);
		break;
	case WIDX_LOAD_DROPDOWN:
		set_operating_setting(w->number, RIDE_SETTING_DEPARTURE, (ride->depart_flags & ~RIDE_DEPART_WAIT_FOR_LOAD_MASK) | dropdownIndex);
		break;
	}
}

/**
 *
 *  rct2: 0x006B178E
 */
static void window_ride_operating_update(rct_window *w)
{
	rct_ride *ride;

	w->frame_no++;
	window_event_invalidate_call(w);
	widget_invalidate(w, WIDX_TAB_3);

	ride = get_ride(w->number);
	if (ride->window_invalidate_flags & RIDE_INVALIDATE_RIDE_OPERATING) {
		ride->window_invalidate_flags &= ~RIDE_INVALIDATE_RIDE_OPERATING;
		window_invalidate(w);
	}
}

/**
 *
 *  rct2: 0x006B0B30
 */
static void window_ride_operating_invalidate(rct_window *w)
{
	rct_widget *widgets;
	rct_ride *ride;
	rct_ride_entry *rideEntry;
	rct_string_id format, caption, tooltip;

	colour_scheme_update(w);

	widgets = window_ride_page_widgets[w->page];
	if (w->widgets != widgets) {
		w->widgets = widgets;
		window_init_scroll_widgets(w);
	}

	window_ride_set_pressed_tab(w);

	ride = get_ride(w->number);
	rideEntry = get_ride_entry_by_ride(ride);

	set_format_arg(0, uint16, ride->name);
	set_format_arg(2, uint32, ride->name_arguments);

	// Widget setup
	w->pressed_widgets &= ~0x44700000;

	// Lift hill speed
	if ((rideEntry->enabledTrackPiecesA & (1UL << TRACK_LIFT_HILL)) && track_type_is_invented(ride->type, TRACK_LIFT_HILL)) {
		window_ride_operating_widgets[WIDX_LIFT_HILL_SPEED_LABEL].type = WWT_24;
		window_ride_operating_widgets[WIDX_LIFT_HILL_SPEED].type = WWT_SPINNER;
		window_ride_operating_widgets[WIDX_LIFT_HILL_SPEED_INCREASE].type = WWT_DROPDOWN_BUTTON;
		window_ride_operating_widgets[WIDX_LIFT_HILL_SPEED_DECREASE].type = WWT_DROPDOWN_BUTTON;
		set_format_arg(20, uint16, ride->lift_hill_speed);
	} else {
		window_ride_operating_widgets[WIDX_LIFT_HILL_SPEED_LABEL].type = WWT_EMPTY;
		window_ride_operating_widgets[WIDX_LIFT_HILL_SPEED].type = WWT_EMPTY;
		window_ride_operating_widgets[WIDX_LIFT_HILL_SPEED_INCREASE].type = WWT_EMPTY;
		window_ride_operating_widgets[WIDX_LIFT_HILL_SPEED_DECREASE].type = WWT_EMPTY;
	}

	// Number of circuits
	if (ride_can_have_multiple_circuits(ride)) {
		window_ride_operating_widgets[WIDX_OPERATE_NUMBER_OF_CIRCUITS_LABEL].type = WWT_24;
		window_ride_operating_widgets[WIDX_OPERATE_NUMBER_OF_CIRCUITS].type = WWT_SPINNER;
		window_ride_operating_widgets[WIDX_OPERATE_NUMBER_OF_CIRCUITS_INCREASE].type = WWT_DROPDOWN_BUTTON;
		window_ride_operating_widgets[WIDX_OPERATE_NUMBER_OF_CIRCUITS_DECREASE].type = WWT_DROPDOWN_BUTTON;
		set_format_arg(22, uint16, ride->num_circuits);
	} else {
		window_ride_operating_widgets[WIDX_OPERATE_NUMBER_OF_CIRCUITS_LABEL].type = WWT_EMPTY;
		window_ride_operating_widgets[WIDX_OPERATE_NUMBER_OF_CIRCUITS].type = WWT_EMPTY;
		window_ride_operating_widgets[WIDX_OPERATE_NUMBER_OF_CIRCUITS_INCREASE].type = WWT_EMPTY;
		window_ride_operating_widgets[WIDX_OPERATE_NUMBER_OF_CIRCUITS_DECREASE].type = WWT_EMPTY;
	}

	// Leave if another vehicle arrives at station
	if (
		ride_type_has_flag(ride->type, RIDE_TYPE_FLAG_HAS_LEAVE_WHEN_ANOTHER_VEHICLE_ARRIVES_AT_STATION) &&
		ride->num_vehicles > 1 &&
		ride->mode != RIDE_MODE_CONTINUOUS_CIRCUIT_BLOCK_SECTIONED &&
		ride->mode != RIDE_MODE_POWERED_LAUNCH_BLOCK_SECTIONED
	) {
		window_ride_operating_widgets[WIDX_LEAVE_WHEN_ANOTHER_ARRIVES_CHECKBOX].type = WWT_CHECKBOX;
		window_ride_operating_widgets[WIDX_LEAVE_WHEN_ANOTHER_ARRIVES_CHECKBOX].tooltip = STR_LEAVE_IF_ANOTHER_VEHICLE_ARRIVES_TIP;
		window_ride_operating_widgets[WIDX_LEAVE_WHEN_ANOTHER_ARRIVES_CHECKBOX].image = RideNameConvention[ride->type].vehicle_name == STR_VEHICLE_LOWERCASE_BOAT ?
			STR_LEAVE_IF_ANOTHER_BOAT_ARRIVES :
			STR_LEAVE_IF_ANOTHER_TRAIN_ARRIVES;
	} else {
		window_ride_operating_widgets[WIDX_LEAVE_WHEN_ANOTHER_ARRIVES_CHECKBOX].type = WWT_EMPTY;
	}

	// Synchronise with adjacent stations
	if (ride_type_has_flag(ride->type, RIDE_TYPE_FLAG_CAN_SYNCHRONISE_ADJACENT_STATIONS)) {
		window_ride_operating_widgets[WIDX_SYNCHRONISE_WITH_ADJACENT_STATIONS_CHECKBOX].type = WWT_CHECKBOX;
		window_ride_operating_widgets[WIDX_SYNCHRONISE_WITH_ADJACENT_STATIONS_CHECKBOX].image = STR_SYNCHRONISE_WITH_ADJACENT_STATIONS;
		window_ride_operating_widgets[WIDX_SYNCHRONISE_WITH_ADJACENT_STATIONS_CHECKBOX].tooltip = STR_SYNCHRONISE_WITH_ADJACENT_STATIONS_TIP;
	} else {
		window_ride_operating_widgets[WIDX_SYNCHRONISE_WITH_ADJACENT_STATIONS_CHECKBOX].type = WWT_EMPTY;
	}

	// Mode
	window_ride_operating_widgets[WIDX_MODE].image = STR_RIDE_MODE_START + ride->mode;

	// Waiting
	window_ride_operating_widgets[WIDX_LOAD].image = STR_QUARTER_LOAD + (ride->depart_flags & RIDE_DEPART_WAIT_FOR_LOAD_MASK);
	if (ride_type_has_flag(ride->type, RIDE_TYPE_FLAG_HAS_LOAD_OPTIONS)) {
		window_ride_operating_widgets[WIDX_LOAD_CHECKBOX].type = WWT_CHECKBOX;
		window_ride_operating_widgets[WIDX_LOAD].type = WWT_DROPDOWN;
		window_ride_operating_widgets[WIDX_LOAD_DROPDOWN].type = WWT_DROPDOWN_BUTTON;

		window_ride_operating_widgets[WIDX_MINIMUM_LENGTH_CHECKBOX].type = WWT_CHECKBOX;
		window_ride_operating_widgets[WIDX_MINIMUM_LENGTH].type = WWT_SPINNER;
		window_ride_operating_widgets[WIDX_MINIMUM_LENGTH_INCREASE].type = WWT_DROPDOWN_BUTTON;
		window_ride_operating_widgets[WIDX_MINIMUM_LENGTH_DECREASE].type = WWT_DROPDOWN_BUTTON;

		window_ride_operating_widgets[WIDX_MAXIMUM_LENGTH_CHECKBOX].type = WWT_CHECKBOX;
		window_ride_operating_widgets[WIDX_MAXIMUM_LENGTH].type = WWT_SPINNER;
		window_ride_operating_widgets[WIDX_MAXIMUM_LENGTH_INCREASE].type = WWT_DROPDOWN_BUTTON;
		window_ride_operating_widgets[WIDX_MAXIMUM_LENGTH_DECREASE].type = WWT_DROPDOWN_BUTTON;

		set_format_arg(10, uint16, STR_FORMAT_SECONDS);
		set_format_arg(12, uint16, ride->min_waiting_time);
		set_format_arg(14, uint16, STR_FORMAT_SECONDS);
		set_format_arg(16, uint16, ride->max_waiting_time);

		if (ride->depart_flags & RIDE_DEPART_WAIT_FOR_LOAD)
			w->pressed_widgets |= (1 << WIDX_LOAD_CHECKBOX);
	} else {
		window_ride_operating_widgets[WIDX_LOAD_CHECKBOX].type = WWT_EMPTY;
		window_ride_operating_widgets[WIDX_LOAD].type = WWT_EMPTY;
		window_ride_operating_widgets[WIDX_LOAD_DROPDOWN].type = WWT_EMPTY;

		window_ride_operating_widgets[WIDX_MINIMUM_LENGTH_CHECKBOX].type = WWT_EMPTY;
		window_ride_operating_widgets[WIDX_MINIMUM_LENGTH].type = WWT_EMPTY;
		window_ride_operating_widgets[WIDX_MINIMUM_LENGTH_INCREASE].type = WWT_EMPTY;
		window_ride_operating_widgets[WIDX_MINIMUM_LENGTH_DECREASE].type = WWT_EMPTY;

		window_ride_operating_widgets[WIDX_MAXIMUM_LENGTH_CHECKBOX].type = WWT_EMPTY;
		window_ride_operating_widgets[WIDX_MAXIMUM_LENGTH].type = WWT_EMPTY;
		window_ride_operating_widgets[WIDX_MAXIMUM_LENGTH_INCREASE].type = WWT_EMPTY;
		window_ride_operating_widgets[WIDX_MAXIMUM_LENGTH_DECREASE].type = WWT_EMPTY;
	}

	if (ride->depart_flags & RIDE_DEPART_LEAVE_WHEN_ANOTHER_ARRIVES)
		w->pressed_widgets |= (1 << WIDX_LEAVE_WHEN_ANOTHER_ARRIVES_CHECKBOX);
	if (ride->depart_flags & RIDE_DEPART_SYNCHRONISE_WITH_ADJACENT_STATIONS)
		w->pressed_widgets |= (1 << WIDX_SYNCHRONISE_WITH_ADJACENT_STATIONS_CHECKBOX);
	if (ride->depart_flags & RIDE_DEPART_WAIT_FOR_MINIMUM_LENGTH)
		w->pressed_widgets |= (1 << WIDX_MINIMUM_LENGTH_CHECKBOX);
	if (ride->depart_flags & RIDE_DEPART_WAIT_FOR_MAXIMUM_LENGTH)
		w->pressed_widgets |= (1 << WIDX_MAXIMUM_LENGTH_CHECKBOX);

	// Mode specific functionality
	set_format_arg(18, uint16, ride->operation_option);
	switch (ride->mode) {
	case RIDE_MODE_POWERED_LAUNCH_PASSTROUGH:
	case RIDE_MODE_POWERED_LAUNCH:
	case RIDE_MODE_UPWARD_LAUNCH:
	case RIDE_MODE_POWERED_LAUNCH_BLOCK_SECTIONED:
		set_format_arg(18, uint16, (ride->launch_speed * 9) / 4);
		format = 1331;
		caption = STR_LAUNCH_SPEED;
		tooltip = STR_LAUNCH_SPEED_TIP;
		break;
	case RIDE_MODE_STATION_TO_STATION:
		set_format_arg(18, uint16, (ride->speed * 9) / 4);
		format = 1331;
		caption = STR_SPEED;
		tooltip = STR_SPEED_TIP;
		break;
	case RIDE_MODE_RACE:
		set_format_arg(18, uint16, ride->num_laps);
		format = 1736;
		caption = STR_NUMBER_OF_LAPS;
		tooltip = STR_NUMBER_OF_LAPS_TIP;
		break;
	case RIDE_MODE_BUMPERCAR:
		format = 1749;
		caption = STR_TIME_LIMIT;
		tooltip = STR_TIME_LIMIT_TIP;
		break;
	case RIDE_MODE_SWING:
		format = 1771;
		caption = STR_NUMBER_OF_SWINGS;
		tooltip = STR_NUMBER_OF_SWINGS_TIP;
		break;
	case RIDE_MODE_ROTATION:
	case RIDE_MODE_FORWARD_ROTATION:
	case RIDE_MODE_BACKWARD_ROTATION:
		format = 1871;
		caption = STR_NUMBER_OF_ROTATIONS;
		tooltip = STR_NUMBER_OF_ROTATIONS_TIP;
		break;
	default:
		format = 1736;
		caption = STR_MAX_PEOPLE_ON_RIDE;
		tooltip = STR_MAX_PEOPLE_ON_RIDE_TIP;
		if (!ride_type_has_flag(ride->type, RIDE_TYPE_FLAG_13))
			format = 0;
		break;
	}

	if (format != 0) {
		if (ride->type == RIDE_TYPE_TWIST) {
			uint16 arg;
			memcpy(&arg, gCommonFormatArgs + 18, sizeof(uint16));
			set_format_arg(18, uint16, arg * 3);
		}

		window_ride_operating_widgets[WIDX_MODE_TWEAK_LABEL].type = WWT_24;
		window_ride_operating_widgets[WIDX_MODE_TWEAK_LABEL].image = caption;
		window_ride_operating_widgets[WIDX_MODE_TWEAK_LABEL].tooltip = tooltip;
		window_ride_operating_widgets[WIDX_MODE_TWEAK].type = WWT_SPINNER;
		window_ride_operating_widgets[WIDX_MODE_TWEAK].image = format;
		window_ride_operating_widgets[WIDX_MODE_TWEAK_INCREASE].type = WWT_DROPDOWN_BUTTON;
		window_ride_operating_widgets[WIDX_MODE_TWEAK_DECREASE].type = WWT_DROPDOWN_BUTTON;
		w->pressed_widgets &= ~(1 << WIDX_LEAVE_WHEN_ANOTHER_ARRIVES_CHECKBOX);
	} else {
		window_ride_operating_widgets[WIDX_MODE_TWEAK_LABEL].type = WWT_EMPTY;
		window_ride_operating_widgets[WIDX_MODE_TWEAK].type = WWT_EMPTY;
		window_ride_operating_widgets[WIDX_MODE_TWEAK_INCREASE].type = WWT_EMPTY;
		window_ride_operating_widgets[WIDX_MODE_TWEAK_DECREASE].type = WWT_EMPTY;
	}

	window_ride_anchor_border_widgets(w);
	window_align_tabs(w, WIDX_TAB_1, WIDX_TAB_10);
}

/**
 *
 *  rct2: 0x006B1001
 */
static void window_ride_operating_paint(rct_window *w, rct_drawpixelinfo *dpi)
{
	rct_ride *ride;
	uint16 blockSections;

	window_draw_widgets(w, dpi);
	window_ride_draw_tab_images(dpi, w);

	ride = get_ride(w->number);

	// Horizontal rule between mode settings and depart settings
	gfx_fill_rect_inset(
		dpi,
		w->x + window_ride_operating_widgets[WIDX_PAGE_BACKGROUND].left + 4,
		w->y + 103,
		w->x + window_ride_operating_widgets[WIDX_PAGE_BACKGROUND].right - 5,
		w->y + 104,
		w->colours[1],
		0x20
	);

	// Number of block sections
	if (ride->mode == RIDE_MODE_CONTINUOUS_CIRCUIT_BLOCK_SECTIONED || ride->mode == RIDE_MODE_POWERED_LAUNCH_BLOCK_SECTIONED) {
		blockSections = ride->num_block_brakes + ride->num_stations;
		gfx_draw_string_left(dpi, STR_BLOCK_SECTIONS, &blockSections, 0, w->x + 21,
							 ride->mode == RIDE_MODE_POWERED_LAUNCH_BLOCK_SECTIONED ? w->y + 89 : w->y + 61);
	}
}

#pragma endregion

#pragma region Maintenance

/**
 *
 *  rct2: 0x006B1AE4
 */
static void window_ride_locate_mechanic(rct_window *w)
{
	rct_ride *ride;
	rct_peep *mechanic;

	ride = get_ride(w->number);

	// First check if there is a mechanic assigned
	mechanic = ride_get_assigned_mechanic(ride);

	// Otherwise find the closest mechanic
	if (mechanic == NULL)
		mechanic = ride_find_closest_mechanic(ride, 1);

	if (mechanic == NULL)
		window_error_open(STR_UNABLE_TO_LOCATE_MECHANIC, STR_NONE);
	else
		window_staff_open(mechanic);
}

/**
 *
 *  rct2: 0x006B7D08
 */
static void window_ride_maintenance_draw_bar(rct_window *w, rct_drawpixelinfo *dpi, int x, int y, int value, int unk)
{
	gfx_fill_rect_inset(dpi, x, y, x + 149, y + 8, w->colours[1], 0x30);
	if (unk & (1u << 31)) {
		unk &= ~(1u << 31);
		if (game_is_not_paused() && (gCurrentTicks & 8))
			return;
	}

	value = ((186 * ((value * 2) & 0xFF)) >> 8) & 0xFF;
	if (value > 2) {
		gfx_fill_rect_inset(dpi, x + 2, y + 1, x + value + 1, y + 7, unk, 0);
	}
}

/**
 *
 *  rct2: 0x006B1AAD
 */
static void window_ride_maintenance_mouseup(rct_window *w, int widgetIndex)
{
	switch (widgetIndex) {
	case WIDX_CLOSE:
		window_close(w);
		break;
	case WIDX_TAB_1:
	case WIDX_TAB_2:
	case WIDX_TAB_3:
	case WIDX_TAB_4:
	case WIDX_TAB_5:
	case WIDX_TAB_6:
	case WIDX_TAB_7:
	case WIDX_TAB_8:
	case WIDX_TAB_9:
	case WIDX_TAB_10:
		window_ride_set_page(w, widgetIndex - WIDX_TAB_1);
		break;
	case WIDX_LOCATE_MECHANIC:
		window_ride_locate_mechanic(w);
		break;
	}
}

/**
 *
 *  rct2: 0x006B1D70
 */
static void window_ride_maintenance_resize(rct_window *w)
{
	window_set_resize(w, 316, 135, 316, 135);
}

/**
 *
 *  rct2: 0x006B1ACE
 */
static void window_ride_maintenance_mousedown(int widgetIndex, rct_window *w, rct_widget *widget)
{
	rct_ride *ride;
	rct_ride_entry *ride_type;
	rct_widget *dropdownWidget;
	int i, j, num_items;
	uint8 breakdownReason;

	dropdownWidget = widget;

	ride = get_ride(w->number);
	ride_type = get_ride_entry(ride->subtype);
	if (ride_type == NULL) {
		return;
	}

	switch (widgetIndex) {
	case WIDX_INSPECTION_INTERVAL_DROPDOWN:
		dropdownWidget--;
		for (i = 0; i < 7; i++) {
			gDropdownItemsFormat[i] = STR_DROPDOWN_MENU_LABEL;
			gDropdownItemsArgs[i] = STR_EVERY_10_MINUTES + i;
		}
		window_dropdown_show_text_custom_width(
			w->x + dropdownWidget->left,
			w->y + dropdownWidget->top,
			dropdownWidget->bottom - dropdownWidget->top + 1,
			w->colours[1],
			DROPDOWN_FLAG_STAY_OPEN,
			7,
			widget->right - dropdownWidget->left
		);

		dropdown_set_checked(ride->inspection_interval, true);
		break;

	case WIDX_FORCE_BREAKDOWN:
		num_items = 1;
		for (j = 0; j < 3; j++) {
			if (ride_type->ride_type[j] != 0xFF)
				break;
		}
		gDropdownItemsFormat[0] = STR_DROPDOWN_MENU_LABEL;
		gDropdownItemsArgs[0] = STR_DEBUG_FIX_RIDE;
		for (i = 0; i < 8; i++) {
			if (RideAvailableBreakdowns[ride_type->ride_type[j]] & (uint8)(1 << i)) {
				if (i == BREAKDOWN_BRAKES_FAILURE && (ride->mode == RIDE_MODE_CONTINUOUS_CIRCUIT_BLOCK_SECTIONED || ride->mode == RIDE_MODE_POWERED_LAUNCH_BLOCK_SECTIONED)) {
					if (ride->num_vehicles != 1)
						continue;
				}
				gDropdownItemsFormat[num_items] = STR_DROPDOWN_MENU_LABEL;
				gDropdownItemsArgs[num_items] = STR_SAFETY_CUT_OUT + i;
				num_items++;
			}
		}
		if (num_items == 1) {
			window_error_open(STR_DEBUG_NO_BREAKDOWNS_AVAILABLE, STR_NONE);
		}
		else {
			window_dropdown_show_text(
				w->x + dropdownWidget->left,
				w->y + dropdownWidget->top,
				dropdownWidget->bottom - dropdownWidget->top + 1,
				w->colours[1],
				DROPDOWN_FLAG_STAY_OPEN,
				num_items
				);

			num_items = 1;
			breakdownReason = ride->breakdown_reason_pending;
			if (breakdownReason != BREAKDOWN_NONE && (ride->lifecycle_flags & RIDE_LIFECYCLE_BREAKDOWN_PENDING)) {
				for (i = 0; i < 8; i++) {
					if (RideAvailableBreakdowns[ride_type->ride_type[j]] & (uint8)(1 << i)) {
						if (i == BREAKDOWN_BRAKES_FAILURE && (ride->mode == RIDE_MODE_CONTINUOUS_CIRCUIT_BLOCK_SECTIONED || ride->mode == RIDE_MODE_POWERED_LAUNCH_BLOCK_SECTIONED)) {
							if (ride->num_vehicles != 1)
								continue;
						}
						if (i == breakdownReason) {
							dropdown_set_checked(num_items, true);
							break;
						}
						gDropdownItemsFormat[num_items] = STR_DROPDOWN_MENU_LABEL;
						gDropdownItemsArgs[num_items] = STR_SAFETY_CUT_OUT + i;
						num_items++;
					}
				}
			}

			if ((ride->lifecycle_flags & RIDE_LIFECYCLE_BREAKDOWN_PENDING) == 0) {
				gDropdownItemsDisabled = (1 << 0);
			}
		}
		break;
	}

}

/**
 *
 *  rct2: 0x006B1AD9
 */
static void window_ride_maintenance_dropdown(rct_window *w, int widgetIndex, int dropdownIndex)
{
	rct_ride *ride;
	rct_ride_entry *ride_type;
	rct_vehicle *vehicle;
	int i, j, num_items;

	if (dropdownIndex == -1)
		return;

	ride = get_ride(w->number);
	ride_type = get_ride_entry(ride->subtype);

	switch (widgetIndex) {
	case WIDX_INSPECTION_INTERVAL_DROPDOWN:
		gGameCommandErrorTitle = STR_CANT_CHANGE_OPERATING_MODE;
		game_do_command(0, (dropdownIndex << 8) | 1, 0, (5 << 8) | w->number, GAME_COMMAND_SET_RIDE_SETTING, 0, 0);
		break;

	case WIDX_FORCE_BREAKDOWN:
		if (dropdownIndex == 0) {
			switch (ride->breakdown_reason_pending) {
			case BREAKDOWN_SAFETY_CUT_OUT:
				if (!(ride->lifecycle_flags & RIDE_LIFECYCLE_ON_TRACK))
					break;
				for (int i = 0; i < ride->num_vehicles; ++i) {
					uint16 spriteId = ride->vehicles[i];
					do {
						vehicle = GET_VEHICLE(spriteId);
						vehicle->update_flags &= ~(
							VEHICLE_UPDATE_FLAG_BROKEN_CAR |
							VEHICLE_UPDATE_FLAG_7 |
							VEHICLE_UPDATE_FLAG_BROKEN_TRAIN
							);
					} while ((spriteId = vehicle->next_vehicle_on_train) != 0xFFFF);
				}
				break;
			case BREAKDOWN_RESTRAINTS_STUCK_CLOSED:
			case BREAKDOWN_RESTRAINTS_STUCK_OPEN:
			case BREAKDOWN_DOORS_STUCK_CLOSED:
			case BREAKDOWN_DOORS_STUCK_OPEN:
				vehicle = &(g_sprite_list[ride->vehicles[ride->broken_vehicle]].vehicle);
				vehicle->update_flags &= ~VEHICLE_UPDATE_FLAG_BROKEN_CAR;
				break;
			case BREAKDOWN_VEHICLE_MALFUNCTION:
				vehicle = &(g_sprite_list[ride->vehicles[ride->broken_vehicle]].vehicle);
				vehicle->update_flags &= ~VEHICLE_UPDATE_FLAG_BROKEN_TRAIN;
				break;
			}
			ride->lifecycle_flags &= ~(RIDE_LIFECYCLE_BREAKDOWN_PENDING | RIDE_LIFECYCLE_BROKEN_DOWN);
			window_invalidate_by_number(WC_RIDE, w->number);
			break;
		}
		if (ride->lifecycle_flags & (RIDE_LIFECYCLE_BREAKDOWN_PENDING | RIDE_LIFECYCLE_BROKEN_DOWN | RIDE_LIFECYCLE_CRASHED)) {
			window_error_open(STR_DEBUG_CANT_FORCE_BREAKDOWN, STR_DEBUG_RIDE_ALREADY_BROKEN);
		}
		else if (ride->status == RIDE_STATUS_CLOSED) {
			window_error_open(STR_DEBUG_CANT_FORCE_BREAKDOWN, STR_DEBUG_RIDE_IS_CLOSED);
		}
		else {
			num_items = 1;
			for (j = 0; j < 3; j++) {
				if (ride_type->ride_type[j] != 0xFF)
					break;
			}
			for (i = 0; i < 8; i++) {
				if (RideAvailableBreakdowns[ride_type->ride_type[j]] & (uint8)(1 << i)) {
					if (i == BREAKDOWN_BRAKES_FAILURE && (ride->mode == RIDE_MODE_CONTINUOUS_CIRCUIT_BLOCK_SECTIONED || ride->mode == RIDE_MODE_POWERED_LAUNCH_BLOCK_SECTIONED)) {
						if (ride->num_vehicles != 1)
							continue;
					}
					if (num_items == dropdownIndex)
						break;
					num_items++;
				}
			}
			ride_prepare_breakdown(w->number, i);
		}
		break;
	}

}

/**
 *
 *  rct2: 0x006B1D37
 */
static void window_ride_maintenance_update(rct_window *w)
{
	rct_ride *ride;

	w->frame_no++;
	window_event_invalidate_call(w);
	widget_invalidate(w, WIDX_TAB_4);

	ride = get_ride(w->number);
	if (ride->window_invalidate_flags & RIDE_INVALIDATE_RIDE_MAINTENANCE) {
		ride->window_invalidate_flags &= ~RIDE_INVALIDATE_RIDE_MAINTENANCE;
		window_invalidate(w);
	}
}

/**
 *
 *  rct2: 0x006B17C8
 */
static void window_ride_maintenance_invalidate(rct_window *w)
{
	rct_widget *widgets;

	colour_scheme_update(w);

	widgets = window_ride_page_widgets[w->page];
	if (w->widgets != widgets) {
		w->widgets = widgets;
		window_init_scroll_widgets(w);
	}

	window_ride_set_pressed_tab(w);

	rct_ride *ride = get_ride(w->number);
	set_format_arg(0, uint16, ride->name);
	set_format_arg(2, uint32, ride->name_arguments);

	window_ride_maintenance_widgets[WIDX_INSPECTION_INTERVAL].image = STR_EVERY_10_MINUTES + ride->inspection_interval;

	window_ride_anchor_border_widgets(w);
	window_align_tabs(w, WIDX_TAB_1, WIDX_TAB_10);

	if (gConfigGeneral.debugging_tools) {
		window_ride_maintenance_widgets[WIDX_FORCE_BREAKDOWN].type = WWT_FLATBTN;
	}
	else {
		window_ride_maintenance_widgets[WIDX_FORCE_BREAKDOWN].type = WWT_EMPTY;

	}
}

/**
 *
 *  rct2: 0x006B1877
 */
static void window_ride_maintenance_paint(rct_window *w, rct_drawpixelinfo *dpi)
{
	rct_ride *ride;
	rct_widget *widget;
	rct_peep *mechanicSprite;
	int x, y;
	uint16 reliability, downTime, lastInspection;
	rct_string_id stringId, breakdownMessage;

	window_draw_widgets(w, dpi);
	window_ride_draw_tab_images(dpi, w);

	ride = get_ride(w->number);

	// Locate mechanic button image
	widget = &window_ride_maintenance_widgets[WIDX_LOCATE_MECHANIC];
	x = w->x + widget->left;
	y = w->y + widget->top;
	gfx_draw_sprite(dpi, (gStaffMechanicColour << 24) | 0xA0000000 | 5196, x, y, 0);

	// Inspection label
	widget = &window_ride_maintenance_widgets[WIDX_INSPECTION_INTERVAL];
	x = w->x + 4;
	y = w->y + widget->top + 1;
	gfx_draw_string_left(dpi, STR_INSPECTION, NULL, 0, x, y);

	// Reliability
	widget = &window_ride_maintenance_widgets[WIDX_PAGE_BACKGROUND];
	x = w->x + widget->left + 4;
	y = w->y + widget->top + 4;

	reliability = ride->reliability >> 8;
	gfx_draw_string_left(dpi, STR_RELIABILITY_LABEL_1757, &reliability, 0, x, y);
	window_ride_maintenance_draw_bar(w, dpi, x + 103, y, max(10, reliability), 14);
	y += 11;

	downTime = ride->downtime;
	gfx_draw_string_left(dpi, STR_DOWN_TIME_LABEL_1889, &downTime, 0, x, y);
	window_ride_maintenance_draw_bar(w, dpi, x + 103, y, downTime, 28);
	y += 26;

	// Last inspection
	lastInspection = ride->last_inspection;

	// Use singular form for 1 minute of time or less
	if (lastInspection <= 1)
		stringId = STR_TIME_SINCE_LAST_INSPECTION_MINUTE;
	else if (lastInspection <= 240)
		stringId = STR_TIME_SINCE_LAST_INSPECTION_MINUTES;
	else
		stringId = STR_TIME_SINCE_LAST_INSPECTION_MORE_THAN_4_HOURS;

	gfx_draw_string_left(dpi, stringId, &lastInspection, 0, x, y);
	y += 12;

	// Last / current breakdown
	if (ride->breakdown_reason == BREAKDOWN_NONE)
		return;

	stringId = (ride->lifecycle_flags & RIDE_LIFECYCLE_BROKEN_DOWN) ?
		STR_CURRENT_BREAKDOWN :
		STR_LAST_BREAKDOWN;
	breakdownMessage = STR_SAFETY_CUT_OUT + ride->breakdown_reason;
	gfx_draw_string_left(dpi, stringId, &breakdownMessage, 0, x, y);
	y += 10;

	// Mechanic status
	if (ride->lifecycle_flags & RIDE_LIFECYCLE_BROKEN_DOWN) {
		switch (ride->mechanic_status) {
		case RIDE_MECHANIC_STATUS_CALLING:
			stringId = STR_CALLING_MECHANIC;
			break;
		case RIDE_MECHANIC_STATUS_HEADING:
			stringId = STR_MEHCANIC_IS_HEADING_FOR_THE_RIDE;
			break;
		case RIDE_MECHANIC_STATUS_FIXING:
		case 4:
			stringId = STR_MEHCANIC_IS_FIXING_THE_RIDE;
			break;
		default:
			stringId = 0;
			break;
		}

		if (stringId != 0) {
			if (stringId == STR_CALLING_MECHANIC) {
				gfx_draw_string_left_wrapped(dpi, NULL, x + 4, y, 280, stringId, 0);
			} else {
				mechanicSprite = &(g_sprite_list[ride->mechanic].peep);
				if (peep_is_mechanic(mechanicSprite)) {
					set_format_arg(0, uint16, mechanicSprite->name_string_idx);
					set_format_arg(2, uint32, mechanicSprite->id);
					gfx_draw_string_left_wrapped(dpi, gCommonFormatArgs, x + 4, y, 280, stringId, 0);
				}
			}
		}
	}
}

#pragma endregion

#pragma region Colour

const uint8 window_ride_entrance_style_list[] = {
	RIDE_ENTRANCE_STYLE_PLAIN,
	RIDE_ENTRANCE_STYLE_CANVAS_TENT,
	RIDE_ENTRANCE_STYLE_WOODEN,
	RIDE_ENTRANCE_STYLE_CASTLE_BROWN,
	RIDE_ENTRANCE_STYLE_CASTLE_GREY,
	RIDE_ENTRANCE_STYLE_LOG_CABIN,
	RIDE_ENTRANCE_STYLE_JUNGLE,
	RIDE_ENTRANCE_STYLE_CLASSICAL_ROMAN,
	RIDE_ENTRANCE_STYLE_ABSTRACT,
	RIDE_ENTRANCE_STYLE_SNOW_ICE,
	RIDE_ENTRANCE_STYLE_PAGODA,
	RIDE_ENTRANCE_STYLE_SPACE,
	RIDE_ENTRANCE_STYLE_NONE
};

static uint32 window_ride_get_colour_button_image(int colour)
{
	return 0x60000000 | (colour << 19) | 5059;
}

static int window_ride_has_track_colour(rct_ride *ride, int trackColour)
{
	uint16 colourUse = RideEntranceDefinitions[ride->entrance_style].colour_use_flags;

	switch (trackColour) {
	case 0: return ((colourUse & 1) && !ride_type_has_flag(ride->type, RIDE_TYPE_FLAG_IS_SHOP)) || ride_type_has_flag(ride->type, RIDE_TYPE_FLAG_HAS_TRACK_COLOUR_MAIN);
	case 1: return ((colourUse & 2) && !ride_type_has_flag(ride->type, RIDE_TYPE_FLAG_IS_SHOP)) || ride_type_has_flag(ride->type, RIDE_TYPE_FLAG_HAS_TRACK_COLOUR_ADDITIONAL);
	case 2: return ride_type_has_flag(ride->type, RIDE_TYPE_FLAG_HAS_TRACK_COLOUR_SUPPORTS);
	default: return 0;
	}
}

static void window_ride_set_track_colour_scheme(rct_window *w, int x, int y)
{
	rct_map_element *mapElement;
	uint8 newColourScheme;
	int interactionType, z, direction;

	newColourScheme = (uint8)w->ride_colour;

	rct_xy16 mapCoord = { 0 };
	get_map_coordinates_from_pos(x, y, VIEWPORT_INTERACTION_MASK_RIDE, &mapCoord.x, &mapCoord.y, &interactionType, &mapElement, NULL);
	x = mapCoord.x;
	y = mapCoord.y;

	if (interactionType != VIEWPORT_INTERACTION_ITEM_RIDE)
		return;
	if (mapElement->properties.track.ride_index != w->number)
		return;
	if ((mapElement->properties.track.colour & 3) == newColourScheme)
		return;

	z = mapElement->base_height * 8;
	direction = mapElement->type & MAP_ELEMENT_DIRECTION_MASK;
	sub_6C683D(&x, &y, &z, direction, mapElement->properties.track.type, newColourScheme, NULL, 4);
}

/**
 *
 *  rct2: 0x006B04FA
 */
static void window_ride_colour_close(rct_window *w)
{
	if (!(gInputFlags & INPUT_FLAG_TOOL_ACTIVE))
		return;

	if (gCurrentToolWidget.window_classification != w->classification)
		return;

	if (gCurrentToolWidget.window_number != w->number)
		return;

	tool_cancel();
}

/**
 *
 *  rct2: 0x006B02A1
 */
static void window_ride_colour_mouseup(rct_window *w, int widgetIndex)
{
	switch (widgetIndex) {
	case WIDX_CLOSE:
		window_close(w);
		break;
	case WIDX_TAB_1:
	case WIDX_TAB_2:
	case WIDX_TAB_3:
	case WIDX_TAB_4:
	case WIDX_TAB_5:
	case WIDX_TAB_6:
	case WIDX_TAB_7:
	case WIDX_TAB_8:
	case WIDX_TAB_9:
	case WIDX_TAB_10:
		window_ride_set_page(w, widgetIndex - WIDX_TAB_1);
		break;
	case WIDX_PAINT_INDIVIDUAL_AREA:
		tool_set(w, WIDX_PAINT_INDIVIDUAL_AREA, 23);
		break;
	}
}

/**
 *
 *  rct2: 0x006B0AB6
 */
static void window_ride_colour_resize(rct_window *w)
{
	window_set_resize(w, 316, 207, 316, 207);
}

/**
 *
 *  rct2: 0x006B02C6
 */
static void window_ride_colour_mousedown(int widgetIndex, rct_window *w, rct_widget *widget)
{
	rct_ride *ride;
	uint16 colourSchemeIndex;
	vehicle_colour vehicleColour;
	rct_widget *dropdownWidget;
	rct_ride_entry *rideEntry;
	int i, numItems;
	rct_string_id stringId;

	ride = get_ride(w->number);
	rideEntry = get_ride_entry_by_ride(ride);
	colourSchemeIndex = w->ride_colour;
	dropdownWidget = widget - 1;

	switch (widgetIndex) {
	case WIDX_TRACK_COLOUR_SCHEME_DROPDOWN:
		for (i = 0; i < 4; i++) {
			gDropdownItemsFormat[i] = STR_DROPDOWN_MENU_LABEL;
			gDropdownItemsArgs[i] = STR_MAIN_COLOUR_SCHEME + i;
		}

		window_dropdown_show_text_custom_width(
			w->x + dropdownWidget->left,
			w->y + dropdownWidget->top,
			dropdownWidget->bottom - dropdownWidget->top + 1,
			w->colours[1],
			DROPDOWN_FLAG_STAY_OPEN,
			4,
			widget->right - dropdownWidget->left
		);

		dropdown_set_checked(colourSchemeIndex, true);
		break;
	case WIDX_TRACK_MAIN_COLOUR:
		window_dropdown_show_colour(w, widget, w->colours[1], ride->track_colour_main[colourSchemeIndex]);
		break;
	case WIDX_TRACK_ADDITIONAL_COLOUR:
		window_dropdown_show_colour(w, widget, w->colours[1], ride->track_colour_additional[colourSchemeIndex]);
		break;
	case WIDX_TRACK_SUPPORT_COLOUR:
		window_dropdown_show_colour(w, widget, w->colours[1], ride->track_colour_supports[colourSchemeIndex]);
		break;
	case WIDX_MAZE_STYLE_DROPDOWN:
		for (i = 0; i < 4; i++) {
			gDropdownItemsFormat[i] = STR_DROPDOWN_MENU_LABEL;
			gDropdownItemsArgs[i] = STR_BRICK_WALLS + i;
		}

		window_dropdown_show_text_custom_width(
			w->x + dropdownWidget->left,
			w->y + dropdownWidget->top,
			dropdownWidget->bottom - dropdownWidget->top + 1,
			w->colours[1],
			DROPDOWN_FLAG_STAY_OPEN,
			4,
			widget->right - dropdownWidget->left
		);

		dropdown_set_checked(ride->track_colour_supports[colourSchemeIndex], true);
		break;
	case WIDX_ENTRANCE_STYLE_DROPDOWN:
		gDropdownItemsChecked = 0;
		for (i = 0; i < countof(window_ride_entrance_style_list); i++) {
			gDropdownItemsFormat[i] = STR_DROPDOWN_MENU_LABEL;
			gDropdownItemsArgs[i] = RideEntranceDefinitions[window_ride_entrance_style_list[i]].string_id;

			if (ride->entrance_style == window_ride_entrance_style_list[i]) {
				dropdown_set_checked(i, true);
			}
		}
		uint64 checked = gDropdownItemsChecked;

		window_dropdown_show_text_custom_width(
			w->x + dropdownWidget->left,
			w->y + dropdownWidget->top,
			dropdownWidget->bottom - dropdownWidget->top + 1,
			w->colours[1],
			DROPDOWN_FLAG_STAY_OPEN,
			countof(window_ride_entrance_style_list),
			widget->right - dropdownWidget->left
		);

		gDropdownItemsChecked = checked;
		break;
	case WIDX_VEHICLE_COLOUR_SCHEME_DROPDOWN:
		for (i = 0; i < 3; i++) {
			gDropdownItemsFormat[i] = STR_DROPDOWN_MENU_LABEL;
			gDropdownItemsArgs[i] = (RideNameConvention[ride->type].vehicle_name << 16) | (STR_ALL_VEHICLES_IN_SAME_COLOURS + i);
		}

		window_dropdown_show_text_custom_width(
			w->x + dropdownWidget->left,
			w->y + dropdownWidget->top,
			dropdownWidget->bottom - dropdownWidget->top + 1,
			w->colours[1],
			DROPDOWN_FLAG_STAY_OPEN,
			rideEntry->max_cars_in_train > 1 ? 3 : 2,
			widget->right - dropdownWidget->left
		);

		dropdown_set_checked(ride->colour_scheme_type & 3, true);
		break;
	case WIDX_VEHICLE_COLOUR_INDEX_DROPDOWN:
		numItems = ride->num_vehicles;
		if ((ride->colour_scheme_type & 3) != VEHICLE_COLOUR_SCHEME_PER_TRAIN)
			numItems = ride->num_cars_per_train;

		stringId = (ride->colour_scheme_type & 3) == VEHICLE_COLOUR_SCHEME_PER_TRAIN ? 1135 : 1133;
		for (i = 0; i < 32; i++) {
			gDropdownItemsFormat[i] = STR_DROPDOWN_MENU_LABEL;
			gDropdownItemsArgs[i] = ((sint64)(i + 1) << 32) | ((RideNameConvention[ride->type].vehicle_name + 2) << 16) | stringId;
		}

		window_dropdown_show_text_custom_width(
			w->x + dropdownWidget->left,
			w->y + dropdownWidget->top,
			dropdownWidget->bottom - dropdownWidget->top + 1,
			w->colours[1],
			DROPDOWN_FLAG_STAY_OPEN,
			numItems,
			widget->right - dropdownWidget->left
		);

		dropdown_set_checked(w->var_48C, true);
		break;
	case WIDX_VEHICLE_MAIN_COLOUR:
		vehicleColour = ride_get_vehicle_colour(ride, w->var_48C);
		window_dropdown_show_colour(w, widget, w->colours[1], vehicleColour.main);
		break;
	case WIDX_VEHICLE_ADDITIONAL_COLOUR_1:
		vehicleColour = ride_get_vehicle_colour(ride, w->var_48C);
		window_dropdown_show_colour(w, widget, w->colours[1], vehicleColour.additional_1);
		break;
	case WIDX_VEHICLE_ADDITIONAL_COLOUR_2:
		vehicleColour = ride_get_vehicle_colour(ride, w->var_48C);
		window_dropdown_show_colour(w, widget, w->colours[1], vehicleColour.additional_2);
		break;
	}
}

/**
 *
 *  rct2: 0x006B0331
 */
static void window_ride_colour_dropdown(rct_window *w, int widgetIndex, int dropdownIndex)
{
	if (dropdownIndex == -1)
		return;

	switch (widgetIndex) {
	case WIDX_TRACK_COLOUR_SCHEME_DROPDOWN:
		w->ride_colour = (uint16)dropdownIndex;
		window_invalidate(w);
		break;
	case WIDX_TRACK_MAIN_COLOUR:
		game_do_command(0, (0 << 8) | 1, 0, (dropdownIndex << 8) | w->number, GAME_COMMAND_SET_RIDE_APPEARANCE, w->ride_colour, 0);
		break;
	case WIDX_TRACK_ADDITIONAL_COLOUR:
		game_do_command(0, (1 << 8) | 1, 0, (dropdownIndex << 8) | w->number, GAME_COMMAND_SET_RIDE_APPEARANCE, w->ride_colour, 0);
		break;
	case WIDX_TRACK_SUPPORT_COLOUR:
		game_do_command(0, (4 << 8) | 1, 0, (dropdownIndex << 8) | w->number, GAME_COMMAND_SET_RIDE_APPEARANCE, w->ride_colour, 0);
		break;
	case WIDX_MAZE_STYLE_DROPDOWN:
		game_do_command(0, (4 << 8) | 1, 0, (dropdownIndex << 8) | w->number, GAME_COMMAND_SET_RIDE_APPEARANCE, w->ride_colour, 0);
		break;
	case WIDX_ENTRANCE_STYLE_DROPDOWN:
		game_do_command(0, (6 << 8) | 1, 0, (window_ride_entrance_style_list[dropdownIndex] << 8) | w->number, GAME_COMMAND_SET_RIDE_APPEARANCE, 0, 0);
		break;
	case WIDX_VEHICLE_COLOUR_SCHEME_DROPDOWN:
		game_do_command(0, (5 << 8) | 1, 0, (dropdownIndex << 8) | w->number, GAME_COMMAND_SET_RIDE_APPEARANCE, 0, 0);
		w->var_48C = 0;
		break;
	case WIDX_VEHICLE_COLOUR_INDEX_DROPDOWN:
		w->var_48C = dropdownIndex;
		window_invalidate(w);
		break;
	case WIDX_VEHICLE_MAIN_COLOUR:
		game_do_command(0, (2 << 8) | 1, 0, (dropdownIndex << 8) | w->number, GAME_COMMAND_SET_RIDE_APPEARANCE, w->var_48C, 0);
		break;
	case WIDX_VEHICLE_ADDITIONAL_COLOUR_1:
		game_do_command(0, (3 << 8) | 1, 0, (dropdownIndex << 8) | w->number, GAME_COMMAND_SET_RIDE_APPEARANCE, w->var_48C, 0);
		break;
	case WIDX_VEHICLE_ADDITIONAL_COLOUR_2:
		game_do_command(0, (7 << 8) | 1, 0, (dropdownIndex << 8) | w->number, GAME_COMMAND_SET_RIDE_APPEARANCE, w->var_48C, 0);
		break;
	}
}

/**
 *
 *  rct2: 0x006B0A8F
 */
static void window_ride_colour_update(rct_window *w)
{
	w->frame_no++;
	window_event_invalidate_call(w);
	widget_invalidate(w, WIDX_TAB_5);
	widget_invalidate(w, WIDX_VEHICLE_PREVIEW);
}

/**
 *
 *  rct2: 0x006B04EC
 */
static void window_ride_colour_tooldown(rct_window *w, int widgetIndex, int x, int y)
{
	if (widgetIndex == WIDX_PAINT_INDIVIDUAL_AREA)
		window_ride_set_track_colour_scheme(w, x, y);
}

/**
 *
 *  rct2: 0x006B04F3
 */
static void window_ride_colour_tooldrag(rct_window *w, int widgetIndex, int x, int y)
{
	if (widgetIndex == WIDX_PAINT_INDIVIDUAL_AREA)
		window_ride_set_track_colour_scheme(w, x, y);
}

/**
 *
 *  rct2: 0x006AFB36
 */
static void window_ride_colour_invalidate(rct_window *w)
{
	rct_widget *widgets;
	rct_ride_entry *rideEntry;
	rct_ride *ride;
	track_colour trackColour;
	vehicle_colour vehicleColour;
	int vehicleColourSchemeType;

	colour_scheme_update(w);

	widgets = window_ride_page_widgets[w->page];
	if (w->widgets != widgets) {
		w->widgets = widgets;
		window_init_scroll_widgets(w);
	}

	window_ride_set_pressed_tab(w);

	ride = get_ride(w->number);
	rideEntry = get_ride_entry_by_ride(ride);

	set_format_arg(0, uint16, ride->name);
	set_format_arg(2, uint32, ride->name_arguments);

	// Track colours
	int colourScheme = w->ride_colour;
	trackColour = ride_get_track_colour(ride, colourScheme);

	// Maze style
	if (ride->type == RIDE_TYPE_MAZE) {
		window_ride_colour_widgets[WIDX_MAZE_STYLE].type = WWT_DROPDOWN;
		window_ride_colour_widgets[WIDX_MAZE_STYLE_DROPDOWN].type = WWT_DROPDOWN_BUTTON;
		window_ride_colour_widgets[WIDX_MAZE_STYLE].image = STR_BRICK_WALLS + trackColour.supports;
	} else {
		window_ride_colour_widgets[WIDX_MAZE_STYLE].type = WWT_EMPTY;
		window_ride_colour_widgets[WIDX_MAZE_STYLE_DROPDOWN].type = WWT_EMPTY;
	}

	// Track, multiple colour schemes
	if (ride_type_has_flag(ride->type, RIDE_TYPE_FLAG_SUPPORTS_MULTIPLE_TRACK_COLOUR)) {
		window_ride_colour_widgets[WIDX_TRACK_COLOUR_SCHEME].type = WWT_DROPDOWN;
		window_ride_colour_widgets[WIDX_TRACK_COLOUR_SCHEME_DROPDOWN].type = WWT_DROPDOWN_BUTTON;
		window_ride_colour_widgets[WIDX_PAINT_INDIVIDUAL_AREA].type = WWT_FLATBTN;
	} else {
		window_ride_colour_widgets[WIDX_TRACK_COLOUR_SCHEME].type = WWT_EMPTY;
		window_ride_colour_widgets[WIDX_TRACK_COLOUR_SCHEME_DROPDOWN].type = WWT_EMPTY;
		window_ride_colour_widgets[WIDX_PAINT_INDIVIDUAL_AREA].type = WWT_EMPTY;
	}

	// Track main colour
	if (window_ride_has_track_colour(ride, 0)) {
		window_ride_colour_widgets[WIDX_TRACK_MAIN_COLOUR].type = WWT_COLOURBTN;
		window_ride_colour_widgets[WIDX_TRACK_MAIN_COLOUR].image = window_ride_get_colour_button_image(trackColour.main);
	} else {
		window_ride_colour_widgets[WIDX_TRACK_MAIN_COLOUR].type = WWT_EMPTY;
	}

	// Track additional colour
	if (window_ride_has_track_colour(ride, 1)) {
		window_ride_colour_widgets[WIDX_TRACK_ADDITIONAL_COLOUR].type = WWT_COLOURBTN;
		window_ride_colour_widgets[WIDX_TRACK_ADDITIONAL_COLOUR].image = window_ride_get_colour_button_image(trackColour.additional);
	} else {
		window_ride_colour_widgets[WIDX_TRACK_ADDITIONAL_COLOUR].type = WWT_EMPTY;
	}

	// Track supports colour
	if (window_ride_has_track_colour(ride, 2) && ride->type != RIDE_TYPE_MAZE) {
		window_ride_colour_widgets[WIDX_TRACK_SUPPORT_COLOUR].type = WWT_COLOURBTN;
		window_ride_colour_widgets[WIDX_TRACK_SUPPORT_COLOUR].image = window_ride_get_colour_button_image(trackColour.supports);
	} else {
		window_ride_colour_widgets[WIDX_TRACK_SUPPORT_COLOUR].type = WWT_EMPTY;
	}

	// Track preview
	if (ride_type_has_flag(ride->type, RIDE_TYPE_FLAG_HAS_TRACK_COLOUR_MAIN | RIDE_TYPE_FLAG_HAS_TRACK_COLOUR_ADDITIONAL | RIDE_TYPE_FLAG_HAS_TRACK_COLOUR_SUPPORTS))
		window_ride_colour_widgets[WIDX_TRACK_PREVIEW].type = WWT_SPINNER;
	else
		window_ride_colour_widgets[WIDX_TRACK_PREVIEW].type = WWT_EMPTY;

	// Entrance style
	if (RideData4[ride->type].flags & RIDE_TYPE_FLAG4_HAS_ENTRANCE_EXIT) {
		window_ride_colour_widgets[WIDX_ENTRANCE_PREVIEW].type = WWT_SPINNER;
		window_ride_colour_widgets[WIDX_ENTRANCE_STYLE].type = WWT_DROPDOWN;
		window_ride_colour_widgets[WIDX_ENTRANCE_STYLE_DROPDOWN].type = WWT_DROPDOWN_BUTTON;

		window_ride_colour_widgets[WIDX_ENTRANCE_STYLE].image = RideEntranceDefinitions[ride->entrance_style].string_id;
	} else {
		window_ride_colour_widgets[WIDX_ENTRANCE_PREVIEW].type = WWT_EMPTY;
		window_ride_colour_widgets[WIDX_ENTRANCE_STYLE].type = WWT_EMPTY;
		window_ride_colour_widgets[WIDX_ENTRANCE_STYLE_DROPDOWN].type = WWT_EMPTY;
	}

	// Vehicle colours
	if (!ride_type_has_flag(ride->type, RIDE_TYPE_FLAG_13) && ride_type_has_flag(ride->type, RIDE_TYPE_FLAG_26)) {
		vehicleColourSchemeType = ride->colour_scheme_type & 3;
		if (vehicleColourSchemeType == 0)
			w->var_48C = 0;

		vehicleColour = ride_get_vehicle_colour(ride, w->var_48C);

		window_ride_colour_widgets[WIDX_VEHICLE_PREVIEW].type = WWT_SCROLL;
		window_ride_colour_widgets[WIDX_VEHICLE_MAIN_COLOUR].type = WWT_COLOURBTN;
		window_ride_colour_widgets[WIDX_VEHICLE_MAIN_COLOUR].image = window_ride_get_colour_button_image(vehicleColour.main);

		uint32 colourFlags = 0;
		for (int i = 0; i < ride->num_cars_per_train; i++) {
			uint8 vehicleTypeIndex = ride_entry_get_vehicle_at_position(ride->subtype, ride->num_cars_per_train, i);

			colourFlags |= rideEntry->vehicles[vehicleTypeIndex].flags_b;
			colourFlags = ror32(colourFlags, 16);
			colourFlags |= rideEntry->vehicles[vehicleTypeIndex].flags_a;
			colourFlags = ror32(colourFlags, 16);
		}

		// Additional colours
		if (colourFlags & 1) {
			window_ride_colour_widgets[WIDX_VEHICLE_ADDITIONAL_COLOUR_1].type = WWT_COLOURBTN;
			window_ride_colour_widgets[WIDX_VEHICLE_ADDITIONAL_COLOUR_1].image = window_ride_get_colour_button_image(vehicleColour.additional_1);
			if (colourFlags & 0x2000000) {
				window_ride_colour_widgets[WIDX_VEHICLE_ADDITIONAL_COLOUR_2].type = WWT_COLOURBTN;
				window_ride_colour_widgets[WIDX_VEHICLE_ADDITIONAL_COLOUR_2].image = window_ride_get_colour_button_image(vehicleColour.additional_2);
			} else {
				window_ride_colour_widgets[WIDX_VEHICLE_ADDITIONAL_COLOUR_2].type = WWT_EMPTY;
			}
		} else {
			window_ride_colour_widgets[WIDX_VEHICLE_ADDITIONAL_COLOUR_1].type = WWT_EMPTY;
			window_ride_colour_widgets[WIDX_VEHICLE_ADDITIONAL_COLOUR_2].type = WWT_EMPTY;
		}

		// Vehicle colour scheme type
		if (!ride_type_has_flag(ride->type, RIDE_TYPE_FLAG_16) && (ride->num_cars_per_train | ride->num_vehicles) > 1) {
			window_ride_colour_widgets[WIDX_VEHICLE_COLOUR_SCHEME].type = WWT_DROPDOWN;
			window_ride_colour_widgets[WIDX_VEHICLE_COLOUR_SCHEME_DROPDOWN].type = WWT_DROPDOWN_BUTTON;
		} else {
			window_ride_colour_widgets[WIDX_VEHICLE_COLOUR_SCHEME].type = WWT_EMPTY;
			window_ride_colour_widgets[WIDX_VEHICLE_COLOUR_SCHEME_DROPDOWN].type = WWT_EMPTY;
		}
		set_format_arg( 6, uint16, STR_ALL_VEHICLES_IN_SAME_COLOURS + vehicleColourSchemeType);
		set_format_arg( 8, uint16, RideNameConvention[ride->type].vehicle_name);
		set_format_arg(10, uint16, RideNameConvention[ride->type].vehicle_name + 2);
		set_format_arg(12, uint16, w->var_48C + 1);
		// Vehicle index
		if (vehicleColourSchemeType != 0) {
			window_ride_colour_widgets[WIDX_VEHICLE_COLOUR_INDEX].type = WWT_DROPDOWN;
			window_ride_colour_widgets[WIDX_VEHICLE_COLOUR_INDEX_DROPDOWN].type = WWT_DROPDOWN_BUTTON;
			window_ride_colour_widgets[WIDX_VEHICLE_COLOUR_INDEX].image = vehicleColourSchemeType == 1 ? 1134 : 1132;
		} else {
			window_ride_colour_widgets[WIDX_VEHICLE_COLOUR_INDEX].type = WWT_EMPTY;
			window_ride_colour_widgets[WIDX_VEHICLE_COLOUR_INDEX_DROPDOWN].type = WWT_EMPTY;
		}
	} else {
		window_ride_colour_widgets[WIDX_VEHICLE_PREVIEW].type = WWT_EMPTY;
		window_ride_colour_widgets[WIDX_VEHICLE_COLOUR_SCHEME].type = WWT_EMPTY;
		window_ride_colour_widgets[WIDX_VEHICLE_COLOUR_SCHEME_DROPDOWN].type = WWT_EMPTY;
		window_ride_colour_widgets[WIDX_VEHICLE_COLOUR_INDEX].type = WWT_EMPTY;
		window_ride_colour_widgets[WIDX_VEHICLE_COLOUR_INDEX_DROPDOWN].type = WWT_EMPTY;
		window_ride_colour_widgets[WIDX_VEHICLE_MAIN_COLOUR].type = WWT_EMPTY;
		window_ride_colour_widgets[WIDX_VEHICLE_ADDITIONAL_COLOUR_1].type = WWT_EMPTY;
		window_ride_colour_widgets[WIDX_VEHICLE_ADDITIONAL_COLOUR_2].type = WWT_EMPTY;
	}

	set_format_arg(14, uint16, STR_MAIN_COLOUR_SCHEME + colourScheme);

	window_ride_anchor_border_widgets(w);
	window_align_tabs(w, WIDX_TAB_1, WIDX_TAB_10);
}

/**
 *
 *  rct2: 0x006AFF3E
 */
static void window_ride_colour_paint(rct_window *w, rct_drawpixelinfo *dpi)
{
	rct_drawpixelinfo clippedDpi;
	rct_widget *widget;
	rct_ride *ride;
	rct_ride_entry *rideEntry;
	int x, y, spriteIndex, terniaryColour;
	track_colour trackColour;

	ride = get_ride(w->number);
	rideEntry = get_ride_entry_by_ride(ride);

	window_draw_widgets(w, dpi);
	window_ride_draw_tab_images(dpi, w);

	// Track / shop item preview
	widget = &window_ride_colour_widgets[WIDX_TRACK_PREVIEW];
	if (widget->type != WWT_EMPTY)
		gfx_fill_rect(dpi, w->x + widget->left + 1, w->y + widget->top + 1, w->x + widget->right - 1, w->y + widget->bottom - 1, 12);

	trackColour = ride_get_track_colour(ride, w->ride_colour);

	//
	if (rideEntry->shop_item == 0xFF) {
		x = w->x + widget->left;
		y = w->y + widget->top;

		// Track
		if (ride->type == RIDE_TYPE_MAZE) {
			spriteIndex = 21990 + trackColour.supports;
			gfx_draw_sprite(dpi, spriteIndex, x, y, 0);
		} else {
			spriteIndex = 14222 + (ride->type * 2);
			spriteIndex |= (trackColour.additional << 24) | (trackColour.main << 19);
			spriteIndex |= 0xA0000000;
			gfx_draw_sprite(dpi, spriteIndex, x, y, 0);

			// Supports
			spriteIndex = 14222 + (ride->type * 2) + 1;
			spriteIndex |= trackColour.supports << 19;
			spriteIndex |= 0x20000000;
			gfx_draw_sprite(dpi, spriteIndex, x, y, 0);
		}
	} else {
		x = w->x + (widget->left + widget->right) / 2 - 8;
		y = w->y + (widget->bottom + widget->top) / 2 - 6;

		uint8 shopItem = rideEntry->shop_item_secondary == 255 ? rideEntry->shop_item : rideEntry->shop_item_secondary;
		spriteIndex = 5061 + shopItem;
		spriteIndex |= ride->track_colour_main[0] << 19;
		spriteIndex |= 0x20000000;

		gfx_draw_sprite(dpi, spriteIndex, x, y, 0);
	}

	// Entrance preview
	trackColour = ride_get_track_colour(ride, 0);
	widget = &w->widgets[WIDX_ENTRANCE_PREVIEW];
	if (widget->type != WWT_EMPTY) {
		if (clip_drawpixelinfo(
			&clippedDpi, 
			dpi,
			w->x + widget->left + 1,
			w->y + widget->top + 1,
			widget->right - widget->left,
			widget->bottom - widget->top
		)) {
			gfx_clear(&clippedDpi, 0x0C0C0C0C);

			if (ride->entrance_style != RIDE_ENTRANCE_STYLE_NONE) {
				const rct_ride_entrance_definition *entranceStyle = &RideEntranceDefinitions[ride->entrance_style];

				terniaryColour = 0;
				if (entranceStyle->base_image_id & 0x40000000) {
					terniaryColour = 0x40000000 | ((trackColour.main + 112) << 19);
				}

				spriteIndex = (trackColour.additional << 24) | (trackColour.main << 19);
				spriteIndex |= 0xA0000000;
				spriteIndex += RideEntranceDefinitions[ride->entrance_style].sprite_index;

				// Back
				gfx_draw_sprite(&clippedDpi, spriteIndex, 34, 20, terniaryColour);

				// Front
				gfx_draw_sprite(&clippedDpi, spriteIndex + 4, 34, 20, terniaryColour);

				// ?
				if (terniaryColour != 0)
					gfx_draw_sprite(&clippedDpi, ((spriteIndex + 20) & 0x7FFFF) + terniaryColour, 34, 20, terniaryColour);
			}
		}
	}
}

/**
 *
 *  rct2: 0x006B0192
 */
static void window_ride_colour_scrollpaint(rct_window *w, rct_drawpixelinfo *dpi, int scrollIndex)
{
	rct_ride *ride;
	rct_ride_entry *rideEntry;
	rct_widget *vehiclePreviewWidget;
	int trainCarIndex, x, y, spriteIndex;
	vehicle_colour vehicleColour;

	ride = get_ride(w->number);
	rideEntry = get_ride_entry_by_ride(ride);
	vehiclePreviewWidget = &window_ride_colour_widgets[WIDX_VEHICLE_PREVIEW];
	vehicleColour = ride_get_vehicle_colour(ride, w->var_48C);

	// Background colour
	gfx_fill_rect(dpi, dpi->x, dpi->y, dpi->x + dpi->width - 1, dpi->y + dpi->height - 1, 12);

	// ?
	x = (vehiclePreviewWidget->right - vehiclePreviewWidget->left) / 2;
	y = vehiclePreviewWidget->bottom - vehiclePreviewWidget->top - 15;

	// ?
	trainCarIndex = (ride->colour_scheme_type & 3) == RIDE_COLOUR_SCHEME_DIFFERENT_PER_CAR ?
		w->var_48C : rideEntry->tab_vehicle;

	rct_ride_entry_vehicle* rideVehicleEntry = &rideEntry->vehicles[ride_entry_get_vehicle_at_position(ride->subtype, ride->num_cars_per_train, trainCarIndex)];

	y += rideVehicleEntry->tab_height;

	// Draw the coloured spinning vehicle
	spriteIndex = rideVehicleEntry->flags_a & VEHICLE_ENTRY_FLAG_A_11 ? w->frame_no / 4 : w->frame_no / 2;
	spriteIndex &= rideVehicleEntry->rotation_frame_mask;
	spriteIndex *= rideVehicleEntry->var_16;
	spriteIndex += rideVehicleEntry->base_image_id;
	spriteIndex |= (vehicleColour.additional_1 << 24) | (vehicleColour.main << 19);
	spriteIndex |= 0x80000000;
	gfx_draw_sprite(dpi, spriteIndex, x, y, vehicleColour.additional_2);
}

#pragma endregion

#pragma region Music

const uint8 MusicStyleOrder[] = {
	MUSIC_STYLE_GENTLE,
	MUSIC_STYLE_SUMMER,
	MUSIC_STYLE_WATER,
	MUSIC_STYLE_RAGTIME,
	MUSIC_STYLE_TECHNO,
	MUSIC_STYLE_MECHANICAL,
	MUSIC_STYLE_MODERN,
	MUSIC_STYLE_WILD_WEST,
	MUSIC_STYLE_PIRATES,
	MUSIC_STYLE_ROCK,
	MUSIC_STYLE_ROCK_STYLE_2,
	MUSIC_STYLE_ROCK_STYLE_3,
	MUSIC_STYLE_FANTASY,
	MUSIC_STYLE_HORROR,
	MUSIC_STYLE_TOYLAND,
	MUSIC_STYLE_CANDY_STYLE,
	MUSIC_STYLE_ROMAN_FANFARE,
	MUSIC_STYLE_ORIENTAL,
	MUSIC_STYLE_MARTIAN,
	MUSIC_STYLE_SPACE,
	MUSIC_STYLE_JUNGLE_DRUMS,
	MUSIC_STYLE_JURASSIC,
	MUSIC_STYLE_EGYPTIAN,
	MUSIC_STYLE_DODGEMS_BEAT,
	MUSIC_STYLE_SNOW,
	MUSIC_STYLE_ICE,
	MUSIC_STYLE_MEDIEVAL,
	MUSIC_STYLE_URBAN,
	MUSIC_STYLE_ORGAN
};

static uint8 window_ride_current_music_style_order[42];

/**
 *
 *  rct2: 0x006B215D
 */
static void window_ride_toggle_music(rct_window *w)
{
	rct_ride *ride = get_ride(w->number);

	int activateMusic = (ride->lifecycle_flags & RIDE_LIFECYCLE_MUSIC) ? 0 : 1;

	gGameCommandErrorTitle = STR_CANT_CHANGE_OPERATING_MODE;
	game_do_command(0, (activateMusic << 8) | 1, 0, (6 << 8) | w->number, GAME_COMMAND_SET_RIDE_SETTING, 0, 0);
}

/**
 *
 *  rct2: 0x006B1ED7
 */
static void window_ride_music_mouseup(rct_window *w, int widgetIndex)
{
	switch (widgetIndex) {
	case WIDX_CLOSE:
		window_close(w);
		break;
	case WIDX_TAB_1:
	case WIDX_TAB_2:
	case WIDX_TAB_3:
	case WIDX_TAB_4:
	case WIDX_TAB_5:
	case WIDX_TAB_6:
	case WIDX_TAB_7:
	case WIDX_TAB_8:
	case WIDX_TAB_9:
	case WIDX_TAB_10:
		window_ride_set_page(w, widgetIndex - WIDX_TAB_1);
		break;
	case WIDX_PLAY_MUSIC:
		window_ride_toggle_music(w);
		break;
	}
}

/**
 *
 *  rct2: 0x006AF4A2
 */
static void window_ride_music_resize(rct_window *w)
{
	w->flags |= WF_RESIZABLE;
	window_set_resize(w, 316, 81, 316, 81);
}

/**
 *
 *  rct2: 0x006B1EFC
 */
static void window_ride_music_mousedown(int widgetIndex, rct_window *w, rct_widget *widget)
{
	rct_widget *dropdownWidget;
	int i;

	if (widgetIndex != WIDX_MUSIC_DROPDOWN)
		return;

	dropdownWidget = widget - 1;
	rct_ride *ride = get_ride(w->number);

	int numItems = 0;
	if (ride->type == RIDE_TYPE_MERRY_GO_ROUND) {
		window_ride_current_music_style_order[numItems++] = MUSIC_STYLE_FAIRGROUND_ORGAN;
	} else {
		for (i = 0; i < countof(MusicStyleOrder); i++)
			window_ride_current_music_style_order[numItems++] = MusicStyleOrder[i];

		if (gRideMusicInfoList[36]->length != 0)
			window_ride_current_music_style_order[numItems++] = MUSIC_STYLE_CUSTOM_MUSIC_1;
		if (gRideMusicInfoList[37]->length != 0)
			window_ride_current_music_style_order[numItems++] = MUSIC_STYLE_CUSTOM_MUSIC_2;
	}

	for (i = 0; i < numItems; i++) {
		gDropdownItemsFormat[i] = STR_DROPDOWN_MENU_LABEL;
		gDropdownItemsArgs[i] = STR_MUSIC_STYLE_START + window_ride_current_music_style_order[i];
	}

	window_dropdown_show_text_custom_width(
		w->x + dropdownWidget->left,
		w->y + dropdownWidget->top,
		dropdownWidget->bottom - dropdownWidget->top + 1,
		w->colours[1],
		DROPDOWN_FLAG_STAY_OPEN,
		numItems,
		widget->right - dropdownWidget->left
	);

	for (i = 0; i < numItems; i++) {
		if (window_ride_current_music_style_order[i] == ride->music) {
			dropdown_set_checked(i, true);
		}
	}
}

/**
 *
 *  rct2: 0x006B1F03
 */
static void window_ride_music_dropdown(rct_window *w, int widgetIndex, int dropdownIndex)
{
	rct_ride *ride;
	uint8 musicStyle;

	if (widgetIndex != WIDX_MUSIC_DROPDOWN || dropdownIndex == -1)
		return;

	ride = get_ride(w->number);
	musicStyle = window_ride_current_music_style_order[dropdownIndex];
	gGameCommandErrorTitle = STR_CANT_CHANGE_OPERATING_MODE;
	game_do_command(0, (musicStyle << 8) | 1, 0, (7 << 8) | w->number, GAME_COMMAND_SET_RIDE_SETTING, 0, 0);
}

/**
 *
 *  rct2: 0x006B2198
 */
static void window_ride_music_update(rct_window *w)
{
	w->frame_no++;
	window_event_invalidate_call(w);
	widget_invalidate(w, WIDX_TAB_6);
}

/**
 *
 *  rct2: 0x006B1DEA
 */
static void window_ride_music_invalidate(rct_window *w)
{
	rct_widget *widgets;
	int isMusicActivated;

	colour_scheme_update(w);

	widgets = window_ride_page_widgets[w->page];
	if (w->widgets != widgets) {
		w->widgets = widgets;
		window_init_scroll_widgets(w);
	}

	window_ride_set_pressed_tab(w);

	rct_ride *ride = get_ride(w->number);
	set_format_arg(0, uint16, ride->name);
	set_format_arg(2, uint32, ride->name_arguments);

	// Set selected music
	window_ride_music_widgets[WIDX_MUSIC].image = STR_MUSIC_STYLE_START + ride->music;

	// Set music activated
	isMusicActivated = ride->lifecycle_flags & (RIDE_LIFECYCLE_MUSIC);
	if (isMusicActivated) {
		w->pressed_widgets |= (1 << WIDX_PLAY_MUSIC);
		w->disabled_widgets &= ~(1 << WIDX_MUSIC);
		w->disabled_widgets &= ~(1 << WIDX_MUSIC_DROPDOWN);
	} else {
		w->pressed_widgets &= ~(1 << WIDX_PLAY_MUSIC);
		w->disabled_widgets |= (1 << WIDX_MUSIC);
		w->disabled_widgets |= (1 << WIDX_MUSIC_DROPDOWN);
	}

	window_ride_anchor_border_widgets(w);
	window_align_tabs(w, WIDX_TAB_1, WIDX_TAB_10);
}

/**
 *
 *  rct2: 0x006B1ECC
 */
static void window_ride_music_paint(rct_window *w, rct_drawpixelinfo *dpi)
{
	window_draw_widgets(w, dpi);
	window_ride_draw_tab_images(dpi, w);
}

#pragma endregion

#pragma region Measurements

/**
 *
 *  rct2: 0x006D2804
  when al == 0*/
static void cancel_scenery_selection(){
	gGamePaused &= ~GAME_PAUSED_SAVING_TRACK;
	RCT2_GLOBAL(0x9DEA6F, uint8) &= ~(1 << 0);
	audio_unpause_sounds();

	rct_window* main_w = window_get_main();

	if (main_w){
		main_w->viewport->flags &= ~(VIEWPORT_FLAG_HIDE_VERTICAL | VIEWPORT_FLAG_HIDE_BASE);
	}

	gfx_invalidate_screen();
	tool_cancel();
}

/**
 *
 *  rct2: 0x006D27A3
 */
static void setup_scenery_selection(rct_window* w)
{
	if (RCT2_GLOBAL(0x009DEA6F, uint8) & 1){
		cancel_scenery_selection();
	}

	while (tool_set(w, 0, 12));

	RCT2_GLOBAL(0x00F64DE8, uint8) = (uint8)w->number;

	track_design_save_init();
	gGamePaused |= GAME_PAUSED_SAVING_TRACK;
	RCT2_GLOBAL(0x009DEA6F, uint8) |= 1;

	audio_pause_sounds();

	rct_window* w_main = window_get_main();

	if (w_main){
		w_main->viewport->flags |= (VIEWPORT_FLAG_HIDE_VERTICAL | VIEWPORT_FLAG_HIDE_BASE);
	}

	gfx_invalidate_screen();
}

/**
 *
 *  rct2: 0x006D3026
 */
static void window_ride_measurements_design_reset()
{
	track_design_save_reset_scenery();
}

/**
 *
 *  rct2: 0x006D303D
 */
static void window_ride_measurements_design_select_nearby_scenery()
{
	track_design_save_select_nearby_scenery(RCT2_GLOBAL(0x00F64DE8, uint8));
}

/**
 *
 *  rct2: 0x006AD4DA
 */
static void window_ride_measurements_design_cancel()
{
	if (RCT2_GLOBAL(0x009DEA6F, uint8) & 1)
		cancel_scenery_selection();
}

/**
 *
 *  rct2: 0x006AD4CD
 */
static void window_ride_measurements_design_save(rct_window *w)
{
	if (track_design_save((uint8)w->number) == 0) return;

	window_ride_measurements_design_cancel();
}

/**
 *
 *  rct2: 0x006AD4DA
 */
static void window_ride_measurements_close(rct_window *w)
{
	window_ride_measurements_design_cancel();
}

/**
 *
 *  rct2: 0x006AD478
 */
static void window_ride_measurements_mouseup(rct_window *w, int widgetIndex)
{
	switch (widgetIndex) {
	case WIDX_CLOSE:
		window_close(w);
		break;
	case WIDX_TAB_1:
	case WIDX_TAB_2:
	case WIDX_TAB_3:
	case WIDX_TAB_4:
	case WIDX_TAB_5:
	case WIDX_TAB_6:
	case WIDX_TAB_7:
	case WIDX_TAB_8:
	case WIDX_TAB_9:
	case WIDX_TAB_10:
		window_ride_set_page(w, widgetIndex - WIDX_TAB_1);
		break;
	case WIDX_SELECT_NEARBY_SCENERY:
		window_ride_measurements_design_select_nearby_scenery();
		break;
	case WIDX_RESET_SELECTION:
		window_ride_measurements_design_reset();
		break;
	case WIDX_SAVE_DESIGN:
		window_ride_measurements_design_save(w);
		break;
	case WIDX_CANCEL_DESIGN:
		window_ride_measurements_design_cancel();
		break;
	}
}

/**
 *
 *  rct2: 0x006AD564
 */
static void window_ride_measurements_resize(rct_window *w)
{
	window_set_resize(w, 316, 202, 316, 202);
}

/**
 *
 *  rct2: 0x006AD4AB
 */
static void window_ride_measurements_mousedown(int widgetIndex, rct_window *w, rct_widget *widget)
{
	if (widgetIndex != WIDX_SAVE_TRACK_DESIGN)
		return;

	gDropdownItemsFormat[0] = STR_SAVE_TRACK_DESIGN_ITEM;
	gDropdownItemsFormat[1] = STR_SAVE_TRACK_DESIGN_WITH_SCENERY_ITEM;

	window_dropdown_show_text(
		w->x + widget->left,
		w->y + widget->top,
		widget->bottom - widget->top + 1,
		w->colours[1],
		0,
		2
	);
	gDropdownDefaultIndex = 0;
	if (gScreenFlags & SCREEN_FLAGS_TRACK_DESIGNER)
		gDropdownItemsDisabled |= 2;
}

/**
 *
 *  rct2: 0x006AD4B2
 */
static void window_ride_measurements_dropdown(rct_window *w, int widgetIndex, int dropdownIndex)
{
	if (widgetIndex != WIDX_SAVE_TRACK_DESIGN)
		return;

	if (dropdownIndex == -1)
		dropdownIndex = gDropdownHighlightedIndex;

	if (dropdownIndex == 0)
		track_design_save((uint8)w->number);
	else
		setup_scenery_selection(w);
}

/**
 *
 *  rct2: 0x006AD5DD
 */
static void window_ride_measurements_update(rct_window *w)
{
	w->frame_no++;
	window_event_invalidate_call(w);
	widget_invalidate(w, WIDX_TAB_7);
}

/**
 *
 *  rct2: 0x006D2AE7
 */
static void window_ride_measurements_tooldown(rct_window *w, int widgetIndex, int x, int y)
{
	rct_map_element *mapElement;
	sint16 mapX, mapY;
	int interactionType;

	get_map_coordinates_from_pos(x, y, 0xFCCF, &mapX, &mapY, &interactionType, &mapElement, NULL);
	switch (interactionType) {
	case VIEWPORT_INTERACTION_ITEM_SCENERY:
	case VIEWPORT_INTERACTION_ITEM_LARGE_SCENERY:
	case VIEWPORT_INTERACTION_ITEM_WALL:
	case VIEWPORT_INTERACTION_ITEM_FOOTPATH:
		track_design_save_toggle_map_element(interactionType, mapX, mapY, mapElement);
		break;
	}
}

/**
 *
 *  rct2: 0x006AD4DA
 */
static void window_ride_measurements_toolabort(rct_window *w, int widgetIndex)
{
	window_ride_measurements_design_cancel();
}

/**
 *
 *  rct2: 0x006ACDBC
 */
static void window_ride_measurements_invalidate(rct_window *w)
{
	rct_widget *widgets;

	colour_scheme_update(w);

	widgets = window_ride_page_widgets[w->page];
	if (w->widgets != widgets) {
		w->widgets = widgets;
		window_init_scroll_widgets(w);
	}

	window_ride_set_pressed_tab(w);

	rct_ride *ride = get_ride(w->number);
	set_format_arg(0, uint16, ride->name);
	set_format_arg(2, uint32, ride->name_arguments);

	window_ride_measurements_widgets[WIDX_SAVE_TRACK_DESIGN].tooltip = STR_SAVE_TRACK_DESIGN_NOT_POSSIBLE;
	window_ride_measurements_widgets[WIDX_SAVE_TRACK_DESIGN].type = WWT_EMPTY;
	if ((RCT2_GLOBAL(0x009DEA6F, uint8) & 1) && RCT2_GLOBAL(0x00F64DE8, uint8) == w->number) {
		window_ride_measurements_widgets[WIDX_SELECT_NEARBY_SCENERY].type = WWT_DROPDOWN_BUTTON;
		window_ride_measurements_widgets[WIDX_RESET_SELECTION].type = WWT_DROPDOWN_BUTTON;
		window_ride_measurements_widgets[WIDX_SAVE_DESIGN].type = WWT_DROPDOWN_BUTTON;
		window_ride_measurements_widgets[WIDX_CANCEL_DESIGN].type = WWT_DROPDOWN_BUTTON;
	} else {
		window_ride_measurements_widgets[WIDX_SELECT_NEARBY_SCENERY].type = WWT_EMPTY;
		window_ride_measurements_widgets[WIDX_RESET_SELECTION].type = WWT_EMPTY;
		window_ride_measurements_widgets[WIDX_SAVE_DESIGN].type = WWT_EMPTY;
		window_ride_measurements_widgets[WIDX_CANCEL_DESIGN].type = WWT_EMPTY;

		if (ride_type_has_flag(ride->type, RIDE_TYPE_FLAG_HAS_TRACK)) {
			window_ride_measurements_widgets[WIDX_SAVE_TRACK_DESIGN].type = WWT_FLATBTN;
			w->disabled_widgets |= (1 << WIDX_SAVE_TRACK_DESIGN);
			if (ride->lifecycle_flags & RIDE_LIFECYCLE_TESTED) {
				if (ride->excitement != -1) {
					w->disabled_widgets &= ~(1 << WIDX_SAVE_TRACK_DESIGN);
					window_ride_measurements_widgets[WIDX_SAVE_TRACK_DESIGN].tooltip = STR_SAVE_TRACK_DESIGN;
				}
			}
		}
	}

	window_ride_anchor_border_widgets(w);
	window_align_tabs(w, WIDX_TAB_1, WIDX_TAB_10);
}

/**
 *
 *  rct2: 0x006ACF07
 */
static void window_ride_measurements_paint(rct_window *w, rct_drawpixelinfo *dpi)
{
	rct_widget *widget;
	rct_ride *ride;
	rct_string_id stringId;
	int x, y, i, numTimes, numLengths;
	sint16 holes, maxSpeed, averageSpeed, drops, highestDropHeight, inversions, time;
	sint32 maxPositiveVerticalGs, maxNegativeVerticalGs, maxLateralGs, totalAirTime, length;

	window_draw_widgets(w, dpi);
	window_ride_draw_tab_images(dpi, w);

	if (window_ride_measurements_widgets[WIDX_SAVE_DESIGN].type == WWT_DROPDOWN_BUTTON) {
		widget = &window_ride_measurements_widgets[WIDX_PAGE_BACKGROUND];

		x = w->x + (widget->right - widget->left) / 2;
		y = w->y + widget->top + 40;
		gfx_draw_string_centred_wrapped(dpi, NULL, x, y, w->width - 8, STR_CLICK_ITEMS_OF_SCENERY_TO_SELECT, 0);

		x = w->x + 4;
		y = w->y + window_ride_measurements_widgets[WIDX_SELECT_NEARBY_SCENERY].bottom + 17;
		gfx_fill_rect_inset(dpi, x, y, w->x + 312, y + 1, w->colours[1], 0x20);
	} else {
		ride = get_ride(w->number);

		x = w->x + window_ride_measurements_widgets[WIDX_PAGE_BACKGROUND].left + 4;
		y = w->y + window_ride_measurements_widgets[WIDX_PAGE_BACKGROUND].top + 4;

		if (ride->lifecycle_flags & RIDE_LIFECYCLE_TESTED) {
			// Excitement
			set_format_arg(0, uint32, ride->excitement);
			set_format_arg(4, uint16, STR_RATING_LOW + min(ride->excitement >> 8, 5));
			stringId = ride->excitement == -1 ? STR_EXCITEMENT_RATING_NOT_YET_AVAILABLE : STR_EXCITEMENT_RATING;
			gfx_draw_string_left(dpi, stringId, gCommonFormatArgs, 0, x, y);
			y += 10;

			// Intensity
			set_format_arg(0, uint32, ride->intensity);
			set_format_arg(4, uint16, STR_RATING_LOW + min(ride->intensity >> 8, 5));

			stringId = STR_INTENSITY_RATING;
			if (ride->excitement == -1)
				stringId = STR_INTENSITY_RATING_NOT_YET_AVAILABLE;
			else if (ride->intensity >= RIDE_RATING(10,00))
				stringId = STR_INTENSITY_RATING_RED;

			gfx_draw_string_left(dpi, stringId, gCommonFormatArgs, 0, x, y);
			y += 10;

			// Nausea
			set_format_arg(0, uint32, ride->nausea);
			set_format_arg(4, uint16, STR_RATING_LOW + min(ride->nausea >> 8, 5));
			stringId = ride->excitement == -1 ? STR_NAUSEA_RATING_NOT_YET_AVAILABLE : STR_NAUSEA_RATING;
			gfx_draw_string_left(dpi, stringId, gCommonFormatArgs, 0, x, y);
			y += 20;

			// Horizontal rule
			gfx_fill_rect_inset(dpi, x, y - 6, x + 303, y - 5, w->colours[1], 0x20);

			if (!(ride->lifecycle_flags & RIDE_LIFECYCLE_NO_RAW_STATS)) {
				if (ride->type == RIDE_TYPE_MINI_GOLF) {
					// Holes
					holes = ride->holes & 0x1F;
					gfx_draw_string_left(dpi, STR_HOLES, &holes, 0, x, y);
					y += 10;
				} else {
					// Max speed
					maxSpeed = (ride->max_speed * 9) >> 18;
					gfx_draw_string_left(dpi, STR_MAX_SPEED, &maxSpeed, 0, x, y);
					y += 10;

					// Average speed
					averageSpeed = (ride->average_speed * 9) >> 18;
					gfx_draw_string_left(dpi, STR_AVERAGE_SPEED, &averageSpeed, 0, x, y);
					y += 10;

					// Ride time
					numTimes = 0;
					for (i = 0; i < ride->num_stations; i++) {
						time = ride->time[numTimes];
						if (time != 0) {
							set_format_arg(0 + (numTimes * 4), uint16, STR_RIDE_TIME_ENTRY_WITH_SEPARATOR);
							set_format_arg(2 + (numTimes * 4), uint16, time);
							numTimes++;
						}
					}
					if (numTimes == 0) {
						set_format_arg(0, uint16, STR_RIDE_TIME_ENTRY);
						set_format_arg(2, uint16, 0);
						numTimes++;
					} else {
						//sadly, STR_RIDE_TIME_ENTRY_WITH_SEPARATOR are defined with the separator AFTER an entry
						//therefore we set the last entry to use the no-separator format now, post-format
						set_format_arg(0 + ((numTimes - 1) * 4), uint16, STR_RIDE_TIME_ENTRY);
					}
					RCT2_GLOBAL(0x013CE94E + (numTimes * 4), uint16) = STR_RIDE_TIME_ENTRY;
					set_format_arg(0 + (numTimes * 4), uint16, 0);
					set_format_arg(2 + (numTimes * 4), uint16, 0);
					set_format_arg(4 + (numTimes * 4), uint16, 0);
					set_format_arg(6 + (numTimes * 4), uint16, 0);
					gfx_draw_string_left_clipped(dpi, STR_RIDE_TIME, gCommonFormatArgs, 0, x, y, 308);
					y += 10;
				}

				// Ride length
				numLengths = 0;
				for (i = 0; i < ride->num_stations; i++) {
					length = ride->length[numLengths];
					if (length != 0) {
						length >>= 16;
						set_format_arg(0 + (numLengths * 4), uint16, STR_RIDE_LENGTH_ENTRY_WITH_SEPARATOR);
						set_format_arg(2 + (numLengths * 4), uint16, (length & 0xFFFF));
						numLengths++;
					}
				}
				if (numLengths == 0) {
					set_format_arg(0, uint16, STR_RIDE_LENGTH_ENTRY);
					set_format_arg(2, uint16, 0);
					numLengths++;
				} else {
					//sadly, STR_RIDE_LENGTH_ENTRY_WITH_SEPARATOR are defined with the separator AFTER an entry
					//therefore we set the last entry to use the no-separator format now, post-format
					set_format_arg(0 + ((numLengths - 1) * 4), uint16, STR_RIDE_LENGTH_ENTRY);
				}
				RCT2_GLOBAL(0x013CE94E + (numLengths * 4), uint16) = STR_RIDE_LENGTH_ENTRY;
				set_format_arg(0 + (numLengths * 4), uint16, 0);
				set_format_arg(2 + (numLengths * 4), uint16, 0);
				set_format_arg(4 + (numLengths * 4), uint16, 0);
				set_format_arg(6 + (numLengths * 4), uint16, 0);
				gfx_draw_string_left_clipped(dpi, STR_RIDE_LENGTH, gCommonFormatArgs, 0, x, y, 308);
				y += 10;

				if (ride_type_has_flag(ride->type, RIDE_TYPE_FLAG_HAS_G_FORCES)) {
					// Max. positive vertical G's
					maxPositiveVerticalGs = ride->max_positive_vertical_g;
					stringId = maxPositiveVerticalGs >= FIXED_2DP(5,00) ?
						STR_MAX_POSITIVE_VERTICAL_G_RED : STR_MAX_POSITIVE_VERTICAL_G;
					gfx_draw_string_left(dpi, stringId, &maxPositiveVerticalGs, 0, x, y);
					y += 10;

					// Max. negative vertical G's
					maxNegativeVerticalGs = ride->max_negative_vertical_g;
					stringId = maxNegativeVerticalGs <= -FIXED_2DP(2,00) ?
						STR_MAX_NEGATIVE_VERTICAL_G_RED : STR_MAX_NEGATIVE_VERTICAL_G;
					gfx_draw_string_left(dpi, stringId, &maxNegativeVerticalGs, 0, x, y);
					y += 10;

					// Max lateral G's
					maxLateralGs = ride->max_lateral_g;
					stringId = maxLateralGs >= FIXED_2DP(2,80) ?
						STR_MAX_LATERAL_G_RED : STR_MAX_LATERAL_G;
					gfx_draw_string_left(dpi, stringId, &maxLateralGs, 0, x, y);
					y += 10;

					// Total 'air' time
					totalAirTime = ride->total_air_time * 3;
					gfx_draw_string_left(dpi, STR_TOTAL_AIR_TIME, &totalAirTime, 0, x, y);
					y += 10;
				}

				if (ride_type_has_flag(ride->type, RIDE_TYPE_FLAG_HAS_DROPS)) {
					// Drops
					drops = ride->drops & 0x3F;
					gfx_draw_string_left(dpi, STR_DROPS, &drops, 0, x, y);
					y += 10;

					// Highest drop height
					highestDropHeight = (ride->highest_drop_height * 3) / 4;
					gfx_draw_string_left(dpi, STR_HIGHEST_DROP_HEIGHT, &highestDropHeight, 0, x, y);
					y += 10;
				}

				if (ride->type != RIDE_TYPE_MINI_GOLF) {
					// Inversions
					inversions = ride->inversions & 0x1F;
					if (inversions != 0) {
						gfx_draw_string_left(dpi, STR_INVERSIONS, &inversions, 0, x, y);
						y += 10;
					}
				}
			}
		} else {
			gfx_draw_string_left(dpi, STR_NO_TEST_RESULTS_YET, NULL, 0, x, y);
		}
	}
}

#pragma endregion

#pragma region Graphs

enum {
	GRAPH_VELOCITY,
	GRAPH_ALTITUDE,
	GRAPH_VERTICAL,
	GRAPH_LATERAL
};

/**
 *
 *  rct2: 0x006AE8A6
 */
static void window_ride_set_graph(rct_window *w, int type)
{
	if ((w->list_information_type & 0xFF) == type) {
		w->list_information_type ^= 0x8000;
	} else {
		w->list_information_type &= 0xFF00;
		w->list_information_type |= type;
	}
	window_invalidate(w);
}

/**
 *
 *  rct2: 0x006AE85D
 */
static void window_ride_graphs_mouseup(rct_window *w, int widgetIndex)
{
	switch (widgetIndex) {
	case WIDX_CLOSE:
		window_close(w);
		break;
	case WIDX_TAB_1:
	case WIDX_TAB_2:
	case WIDX_TAB_3:
	case WIDX_TAB_4:
	case WIDX_TAB_5:
	case WIDX_TAB_6:
	case WIDX_TAB_7:
	case WIDX_TAB_8:
	case WIDX_TAB_9:
	case WIDX_TAB_10:
		window_ride_set_page(w, widgetIndex - WIDX_TAB_1);
		break;
	}
}

/**
 *
 *  rct2: 0x006AE8DA
 */
static void window_ride_graphs_resize(rct_window *w)
{
	window_set_resize(w, 316, 180, 500, 450);
}

/**
 *
 *  rct2: 0x006AE878
 */
static void window_ride_graphs_mousedown(int widgetIndex, rct_window *w, rct_widget *widget)
{
	switch (widgetIndex) {
	case WIDX_GRAPH_VELOCITY:
		window_ride_set_graph(w, GRAPH_VELOCITY);
		break;
	case WIDX_GRAPH_ALTITUDE:
		window_ride_set_graph(w, GRAPH_ALTITUDE);
		break;
	case WIDX_GRAPH_VERTICAL:
		window_ride_set_graph(w, GRAPH_VERTICAL);
		break;
	case WIDX_GRAPH_LATERAL:
		window_ride_set_graph(w, GRAPH_LATERAL);
		break;
	}
}

/**
 *
 *  rct2: 0x006AE95D
 */
static void window_ride_graphs_update(rct_window *w)
{
	rct_widget *widget;
	rct_ride_measurement *measurement;
	int x;

	w->frame_no++;
	window_event_invalidate_call(w);
	widget_invalidate(w, WIDX_TAB_8);
	window_event_invalidate_call(w);
	widget_invalidate(w, WIDX_GRAPH);

	widget = &window_ride_graphs_widgets[WIDX_GRAPH];
	x = w->scrolls[0].h_left;
	if (!(w->list_information_type & 0x8000)) {
		measurement = ride_get_measurement(w->number, NULL);
		x = measurement == NULL ?
			0 :
			measurement->current_item - (((widget->right - widget->left) / 4) * 3);
	}

	w->scrolls[0].h_left = clamp(0, x, w->scrolls[0].h_right - ((widget->right - widget->left) - 2));
	widget_scroll_update_thumbs(w, WIDX_GRAPH);
}

/**
 *
 *  rct2: 0x006AEA75
 */
static void window_ride_graphs_scrollgetheight(rct_window *w, int scrollIndex, int *width, int *height)
{
	rct_ride_measurement *measurement;

	window_event_invalidate_call(w);

	// Set minimum size
	*width = window_ride_graphs_widgets[WIDX_GRAPH].right - window_ride_graphs_widgets[WIDX_GRAPH].left - 2;

	// Get measurement size
	measurement = ride_get_measurement(w->number, NULL);
	if (measurement != NULL)
		*width = max(*width, measurement->num_items);
}

/**
 *
 *  rct2: 0x006AE953
 */
static void window_ride_graphs_15(rct_window *w, int scrollIndex, int scrollAreaType)
{
	w->list_information_type |= 0x8000;
}

/**
 *
 *  rct2: 0x006AEA05
 */
static void window_ride_graphs_tooltip(rct_window* w, int widgetIndex, rct_string_id *stringId)
{
	rct_ride *ride;
	rct_ride_measurement *measurement;
	rct_string_id message;

	if (widgetIndex == WIDX_GRAPH) {
		set_format_arg(0, uint16, STR_GRAPH);
		measurement = ride_get_measurement(w->number, &message);
		if (measurement != NULL && (measurement->flags & RIDE_MEASUREMENT_FLAG_RUNNING)) {
			set_format_arg(4, uint16, measurement->vehicle_index + 1);
			ride = get_ride(w->number);
			set_format_arg(2, uint16, RideNameConvention[ride->type].vehicle_name + 6);
		} else {
			*stringId = message;
		}
	} else {
		*stringId = STR_NONE;
	}
}

/**
 *
 *  rct2: 0x006AE372
 */
static void window_ride_graphs_invalidate(rct_window *w)
{
	rct_widget *widgets;
	rct_ride *ride;
	int x, y;

	colour_scheme_update(w);

	widgets = window_ride_page_widgets[w->page];
	if (w->widgets != widgets) {
		w->widgets = widgets;
		window_init_scroll_widgets(w);
	}

	window_ride_set_pressed_tab(w);

	ride = get_ride(w->number);

	set_format_arg(0, uint16, ride->name);
	set_format_arg(2, uint32, ride->name_arguments);

	// Set pressed graph button type
	w->pressed_widgets &= ~(1 << WIDX_GRAPH_VELOCITY);
	w->pressed_widgets &= ~(1 << WIDX_GRAPH_ALTITUDE);
	w->pressed_widgets &= ~(1 << WIDX_GRAPH_VERTICAL);
	w->pressed_widgets &= ~(1 << WIDX_GRAPH_LATERAL);
	w->pressed_widgets |= (1LL << (WIDX_GRAPH_VELOCITY + (w->list_information_type & 0xFF)));

	// Hide graph buttons that are not applicable
	if (ride_type_has_flag(ride->type, RIDE_TYPE_FLAG_HAS_G_FORCES)) {
		window_ride_graphs_widgets[WIDX_GRAPH_VERTICAL].type = WWT_DROPDOWN_BUTTON;
		window_ride_graphs_widgets[WIDX_GRAPH_LATERAL].type = WWT_DROPDOWN_BUTTON;
	} else {
		window_ride_graphs_widgets[WIDX_GRAPH_VERTICAL].type = WWT_EMPTY;
		window_ride_graphs_widgets[WIDX_GRAPH_LATERAL].type = WWT_EMPTY;
	}

	// Anchor graph widget
	x = w->width - 4;
	y = w->height - 18;

	window_ride_graphs_widgets[WIDX_GRAPH].right = x;
	window_ride_graphs_widgets[WIDX_GRAPH].bottom = y;
	y += 3;
	window_ride_graphs_widgets[WIDX_GRAPH_VELOCITY].top = y;
	window_ride_graphs_widgets[WIDX_GRAPH_ALTITUDE].top = y;
	window_ride_graphs_widgets[WIDX_GRAPH_VERTICAL].top = y;
	window_ride_graphs_widgets[WIDX_GRAPH_LATERAL].top = y;
	y += 11;
	window_ride_graphs_widgets[WIDX_GRAPH_VELOCITY].bottom = y;
	window_ride_graphs_widgets[WIDX_GRAPH_ALTITUDE].bottom = y;
	window_ride_graphs_widgets[WIDX_GRAPH_VERTICAL].bottom = y;
	window_ride_graphs_widgets[WIDX_GRAPH_LATERAL].bottom = y;

	window_ride_anchor_border_widgets(w);
	window_align_tabs(w, WIDX_TAB_1, WIDX_TAB_10);
}

/**
 *
 *  rct2: 0x006AE4BC
 */
static void window_ride_graphs_paint(rct_window *w, rct_drawpixelinfo *dpi)
{
	window_draw_widgets(w, dpi);
	window_ride_draw_tab_images(dpi, w);
}

/**
 *
 *  rct2: 0x006AE4C7
 */
static void window_ride_graphs_scrollpaint(rct_window *w, rct_drawpixelinfo *dpi, int scrollIndex)
{
	rct_ride_measurement *measurement;
	rct_widget *widget;
	int x, y, width, time, listType, colour, top, bottom, tmp;
	rct_string_id stringId;

	gfx_clear(dpi, RCT2_GLOBAL(0x0141FC9D, uint8) * 0x01010101);

	widget = &window_ride_graphs_widgets[WIDX_GRAPH];
	listType = w->list_information_type & 0xFF;
	measurement = ride_get_measurement(w->number, &stringId);
	if (measurement == NULL) {
		// No measurement message
		x = (widget->right - widget->left) / 2;
		y = (widget->bottom - widget->top) / 2 - 5;
		width = widget->right - widget->left - 2;
		gfx_draw_string_centred_wrapped(dpi, gCommonFormatArgs, x, y, width, stringId, 0);
		return;
	}

	// Vertical grid lines
	time = 0;
	for (x = 0; x < dpi->x + dpi->width; x += 80) {
		if (x + 80 >= dpi->x) {
			gfx_fill_rect(dpi, x +  0, dpi->y, x +  0, dpi->y + dpi->height - 1, RCT2_GLOBAL(0x0141FCA0, uint8));
			gfx_fill_rect(dpi, x + 16, dpi->y, x + 16, dpi->y + dpi->height - 1, RCT2_GLOBAL(0x0141FC9F, uint8));
			gfx_fill_rect(dpi, x + 32, dpi->y, x + 32, dpi->y + dpi->height - 1, RCT2_GLOBAL(0x0141FC9F, uint8));
			gfx_fill_rect(dpi, x + 48, dpi->y, x + 48, dpi->y + dpi->height - 1, RCT2_GLOBAL(0x0141FC9F, uint8));
			gfx_fill_rect(dpi, x + 64, dpi->y, x + 64, dpi->y + dpi->height - 1, RCT2_GLOBAL(0x0141FC9F, uint8));
		}
		time += 5;
	}

	// Horizontal grid lines
	y = widget->bottom - widget->top - 13;
	short yUnit = RCT2_GLOBAL(0x0098DD9A + (listType * 8), uint16);
	short ax = RCT2_GLOBAL(0x0098DD9E + (listType * 8), uint16);
	short yUnitInterval = RCT2_GLOBAL(0x0098DD9C + (listType * 8), uint16);
	short yInterval = RCT2_GLOBAL(0x0098DD98 + (listType * 8), uint16);

	// Scale modifier
	if (ax == 1420) {
		short unk = RCT2_GLOBAL(0x01359208, uint16);
		yUnit -= RCT2_GLOBAL(0x01359208, uint16);
		unk *= 2;
		yUnit -= unk;
	}

	for (y = widget->bottom - widget->top - 13; y >= 8; y -= yInterval, yUnit += yUnitInterval) {
		// Minor / major line
		colour = yUnit == 0 ?
			RCT2_GLOBAL(0x0141FCA0, uint8) :
			RCT2_GLOBAL(0x0141FC9F, uint8);

		gfx_fill_rect(dpi, dpi->x, y, dpi->x + dpi->width - 1, y, colour);

		sint16 scaled_yUnit = yUnit;
		// Scale modifier
		if (ax == 1420)
			scaled_yUnit /= 2;

		gfx_draw_string_left(dpi, ax, &scaled_yUnit, 0, w->scrolls[0].h_left + 1, y - 4);
	}

	// Time marks
	x = 0;
	time = 0;
	for (x = 0; x < dpi->x + dpi->width; x += 80) {
		if (x + 80 >= dpi->x)
			gfx_draw_string_left(dpi, STR_RIDE_STATS_TIME, &time, 0, x + 2, 1);
		time += 5;
	}

	// Plot
	x = dpi->x;
	for (width = 0; width < dpi->width; width++, x++) {
		if (x < 0 || x >= measurement->num_items - 1)
			continue;

		switch (listType) {
		case GRAPH_VELOCITY:
			top = measurement->velocity[x] / 2;
			bottom = measurement->velocity[x + 1] / 2;
			break;
		case GRAPH_ALTITUDE:
			top = measurement->altitude[x];
			bottom = measurement->altitude[x + 1];
			break;
		case GRAPH_VERTICAL:
			top = measurement->vertical[x] + 39;
			bottom = measurement->vertical[x + 1] + 39;
			break;
		case GRAPH_LATERAL:
			top = measurement->lateral[x] + 52;
			bottom = measurement->lateral[x + 1] + 52;
			break;
		default:
			log_error("Wrong graph type %d", listType);
			top = bottom = 0;
		}

		top = widget->bottom - widget->top - top - 13;
		bottom = widget->bottom - widget->top - bottom - 13;
		if (top > bottom) {
			tmp = top;
			top = bottom;
			bottom = tmp;
		}
		gfx_fill_rect(dpi, x, top, x, bottom, x > measurement->current_item ? 17 : 21);
	}
}

#pragma endregion

#pragma region Income

static void update_same_price_throughout_flags(uint32 shop_item)
{
	uint32 newFlags;

	if (shop_item == SHOP_ITEM_PHOTO || shop_item == SHOP_ITEM_PHOTO2 || shop_item == SHOP_ITEM_PHOTO3 || shop_item == SHOP_ITEM_PHOTO4) {
		newFlags = gSamePriceThroughoutParkA;
		newFlags ^= (1 << SHOP_ITEM_PHOTO);
		game_do_command(0, 1, 0, (0x2 << 8), GAME_COMMAND_SET_PARK_OPEN, newFlags, shop_item);

		newFlags = gSamePriceThroughoutParkB;
		newFlags ^= (SHOP_ITEM_PHOTO2 - 32) | (SHOP_ITEM_PHOTO3 - 32) | (SHOP_ITEM_PHOTO4 - 32);
		game_do_command(0, 1, 0, (0x3 << 8), GAME_COMMAND_SET_PARK_OPEN, newFlags, shop_item);
	}
	else {
		if (shop_item < 32) {
			newFlags = gSamePriceThroughoutParkA;
			newFlags ^= (1u << shop_item);
			game_do_command(0, 1, 0, (0x2 << 8), GAME_COMMAND_SET_PARK_OPEN, newFlags, shop_item);
		}
		else {
			newFlags = gSamePriceThroughoutParkB;
			newFlags ^= (1u << (shop_item - 32));
			game_do_command(0, 1, 0, (0x3 << 8), GAME_COMMAND_SET_PARK_OPEN, newFlags, shop_item);
		}
	}
}

/**
 *
 *  rct2: 0x006ADEFD
 */
static void window_ride_income_toggle_primary_price(rct_window *w)
{
	rct_ride *ride;
	rct_ride_entry *ride_type;
	uint32 shop_item;
	money16 price;

	ride = get_ride(w->number);
	ride_type = get_ride_entry(ride->subtype);

	if (ride->type == RIDE_TYPE_TOILETS) {
		shop_item = SHOP_ITEM_ADMISSION;
	}
	else {
		shop_item = ride_type->shop_item;
		if (shop_item == 0xFFFF)
			return;
	}

	update_same_price_throughout_flags(shop_item);

	price = ride->price;
	game_do_command(0, 1, 0, w->number, GAME_COMMAND_SET_RIDE_PRICE, price, 0);
}

/**
 *
 *  rct2: 0x006AE06E
 */
static void window_ride_income_toggle_secondary_price(rct_window *w)
{
	rct_ride *ride;
	rct_ride_entry *ride_type;
	uint32 shop_item;
	money16 price;

	ride = get_ride(w->number);
	ride_type = get_ride_entry(ride->subtype);

	shop_item = ride_type->shop_item_secondary;
	if (shop_item == 0xFF)
		shop_item = RCT2_GLOBAL(0x0097D7CB + (ride->type * 4), uint8);

	update_same_price_throughout_flags(shop_item);

	price = ride->price_secondary;
	game_do_command(0, 1, 0, (1 << 8) | w->number, GAME_COMMAND_SET_RIDE_PRICE, price, 0);
}

/**
 *
 *  rct2: 0x006AE1E4
 */
static void window_ride_income_increase_primary_price(rct_window *w)
{
	rct_ride *ride;
	rct_ride_entry *ride_type;

	ride = get_ride(w->number);
	ride_type = get_ride_entry(ride->subtype);

	if ((gParkFlags & PARK_FLAGS_PARK_FREE_ENTRY) == 0) {
		if (ride->type != RIDE_TYPE_TOILETS && ride_type->shop_item == 0xFF) {
			if (!gCheatsUnlockAllPrices)
				return;
		}
	}
	money16 price = ride->price;
	if (price < MONEY(20, 00))
		price++;

	game_do_command(0, 1, 0, w->number, GAME_COMMAND_SET_RIDE_PRICE, price, 0);
}

/**
 *
 *  rct2: 0x006AE237
 */
static void window_ride_income_decrease_primary_price(rct_window *w)
{
	rct_ride *ride;
	rct_ride_entry *ride_type;

	ride = get_ride(w->number);
	ride_type = get_ride_entry(ride->subtype);

	if ((gParkFlags & PARK_FLAGS_PARK_FREE_ENTRY) == 0) {
		if (ride->type != RIDE_TYPE_TOILETS && ride_type->shop_item == 0xFF) {
			if (!gCheatsUnlockAllPrices)
				return;
		}
	}
	money16 price = ride->price;
	if (price > MONEY(0, 00))
		price--;

	game_do_command(0, 1, 0, w->number, GAME_COMMAND_SET_RIDE_PRICE, price, 0);
}

/**
 *
 *  rct2: 0x006AE269
 */
static void window_ride_income_increase_secondary_price(rct_window *w)
{
	rct_ride *ride;
	rct_ride_entry *ride_type;

	ride = get_ride(w->number);
	ride_type = get_ride_entry(ride->subtype);

	money16 price = ride->price_secondary;
	if (price < MONEY(20, 00))
		price++;

	game_do_command(0, 1, 0, (w->number & 0x00FF) | 0x0100, GAME_COMMAND_SET_RIDE_PRICE, price, 0);
}

/**
 *
 *  rct2: 0x006AE28D
 */
static void window_ride_income_decrease_secondary_price(rct_window *w)
{
	rct_ride *ride;
	rct_ride_entry *ride_type;

	ride = get_ride(w->number);
	ride_type = get_ride_entry(ride->subtype);

	money16 price = ride->price_secondary;
	if (price > MONEY(0, 00))
		price--;

	game_do_command(0, 1, 0, (w->number & 0x00FF) | 0x0100, GAME_COMMAND_SET_RIDE_PRICE, price, 0);
}

/**
 *
 *  rct2: 0x006ADEA9
 */
static void window_ride_income_mouseup(rct_window *w, int widgetIndex)
{
	switch (widgetIndex) {
	case WIDX_CLOSE:
		window_close(w);
		break;
	case WIDX_TAB_1:
	case WIDX_TAB_2:
	case WIDX_TAB_3:
	case WIDX_TAB_4:
	case WIDX_TAB_5:
	case WIDX_TAB_6:
	case WIDX_TAB_7:
	case WIDX_TAB_8:
	case WIDX_TAB_9:
	case WIDX_TAB_10:
		window_ride_set_page(w, widgetIndex - WIDX_TAB_1);
		break;
	case WIDX_PRIMARY_PRICE_SAME_THROUGHOUT_PARK:
		window_ride_income_toggle_primary_price(w);
		break;
	case WIDX_SECONDARY_PRICE_SAME_THROUGHOUT_PARK:
		window_ride_income_toggle_secondary_price(w);
		break;
	}
}

/**
 *
 *  rct2: 0x006AE2F8
 */
static void window_ride_income_resize(rct_window *w)
{
	window_set_resize(w, 316, 177, 316, 177);
}

/**
 *
 *  rct2: 0x006ADED4
 */
static void window_ride_income_mousedown(int widgetIndex, rct_window *w, rct_widget *widget)
{
	switch (widgetIndex) {
	case WIDX_PRIMARY_PRICE_INCREASE:
		window_ride_income_increase_primary_price(w);
		break;
	case WIDX_PRIMARY_PRICE_DECREASE:
		window_ride_income_decrease_primary_price(w);
		break;
	case WIDX_SECONDARY_PRICE_INCREASE:
		window_ride_income_increase_secondary_price(w);
		break;
	case WIDX_SECONDARY_PRICE_DECREASE:
		window_ride_income_decrease_secondary_price(w);
		break;
	}
}

/**
 *
 *  rct2: 0x006AE2BF
 */
static void window_ride_income_update(rct_window *w)
{
	rct_ride *ride;

	w->frame_no++;
	window_event_invalidate_call(w);
	widget_invalidate(w, WIDX_TAB_9);

	ride = get_ride(w->number);
	if (ride->window_invalidate_flags & RIDE_INVALIDATE_RIDE_INCOME) {
		ride->window_invalidate_flags &= ~RIDE_INVALIDATE_RIDE_INCOME;
		window_invalidate(w);
	}
}

/**
 *
 *  rct2: 0x006ADAA3
 */
static void window_ride_income_invalidate(rct_window *w)
{
	rct_widget *widgets;
	rct_ride_entry *rideEntry;
	rct_string_id stringId;
	int primaryItem, secondaryItem;

	colour_scheme_update(w);

	widgets = window_ride_page_widgets[w->page];
	if (w->widgets != widgets) {
		w->widgets = widgets;
		window_init_scroll_widgets(w);
	}

	window_ride_set_pressed_tab(w);

	rct_ride *ride = get_ride(w->number);
	set_format_arg(0, uint16, ride->name);
	set_format_arg(2, uint32, ride->name_arguments);

	rideEntry = get_ride_entry_by_ride(ride);

	// Primary item
	w->pressed_widgets &= ~(1 << WIDX_PRIMARY_PRICE_SAME_THROUGHOUT_PARK);
	w->disabled_widgets &= ~(1 << WIDX_PRIMARY_PRICE);

	//If the park doesn't have free entry, lock the admission price, unless the cheat to unlock all prices is activated.
	if ((!(gParkFlags & PARK_FLAGS_PARK_FREE_ENTRY) && rideEntry->shop_item == SHOP_ITEM_NONE && ride->type != RIDE_TYPE_TOILETS)
		&& (!gCheatsUnlockAllPrices))
	{
		w->disabled_widgets |= (1 << WIDX_PRIMARY_PRICE);
	}

	window_ride_income_widgets[WIDX_PRIMARY_PRICE_LABEL].image = STR_RIDE_INCOME_ADMISSION_PRICE;
	window_ride_income_widgets[WIDX_SECONDARY_PRICE_LABEL].image = STR_ON_RIDE_PHOTO_PRICE;
	window_ride_income_widgets[WIDX_PRIMARY_PRICE_SAME_THROUGHOUT_PARK].type = WWT_EMPTY;

	window_ride_income_widgets[WIDX_PRIMARY_PRICE].image = 1429;
	set_format_arg(6, money32, ride->price);
	if (ride->price == 0)
		window_ride_income_widgets[WIDX_PRIMARY_PRICE].image = STR_FREE;

	primaryItem = SHOP_ITEM_ADMISSION;
	if (ride->type == RIDE_TYPE_TOILETS || ((primaryItem = rideEntry->shop_item) != SHOP_ITEM_NONE)) {
		window_ride_income_widgets[WIDX_PRIMARY_PRICE_SAME_THROUGHOUT_PARK].type = WWT_CHECKBOX;

		if (shop_item_has_common_price(primaryItem))
			w->pressed_widgets |= (1 << WIDX_PRIMARY_PRICE_SAME_THROUGHOUT_PARK);


		if (primaryItem < SHOP_ITEM_ADMISSION)
			window_ride_income_widgets[WIDX_PRIMARY_PRICE_LABEL].image = 1960 + primaryItem;
		else if (primaryItem > SHOP_ITEM_ADMISSION)
			window_ride_income_widgets[WIDX_PRIMARY_PRICE_LABEL].image = 2068 + primaryItem;
	}

	// Get secondary item
	secondaryItem = RCT2_GLOBAL(0x0097D7CB + (ride->type * 4), uint8);
	if (!(ride->lifecycle_flags & RIDE_LIFECYCLE_ON_RIDE_PHOTO)) {
		if ((secondaryItem = rideEntry->shop_item_secondary) != SHOP_ITEM_NONE) {
			// Set secondary item label
			stringId = 1960 + secondaryItem;
			if (stringId >= 1992)
				stringId += 108;

			window_ride_income_widgets[WIDX_SECONDARY_PRICE_LABEL].image = stringId;
		}
	}

	if (secondaryItem == SHOP_ITEM_NONE) {
		// Hide secondary item widgets
		window_ride_income_widgets[WIDX_SECONDARY_PRICE_LABEL].type = WWT_EMPTY;
		window_ride_income_widgets[WIDX_SECONDARY_PRICE].type = WWT_EMPTY;
		window_ride_income_widgets[WIDX_SECONDARY_PRICE_INCREASE].type = WWT_EMPTY;
		window_ride_income_widgets[WIDX_SECONDARY_PRICE_DECREASE].type = WWT_EMPTY;
		window_ride_income_widgets[WIDX_SECONDARY_PRICE_SAME_THROUGHOUT_PARK].type = WWT_EMPTY;
	} else {
		// Set same price throughout park checkbox
		w->pressed_widgets &= ~(1 << WIDX_SECONDARY_PRICE_SAME_THROUGHOUT_PARK);
		if (shop_item_has_common_price(secondaryItem))
			w->pressed_widgets |= (1 << WIDX_SECONDARY_PRICE_SAME_THROUGHOUT_PARK);

		// Show widgets
		window_ride_income_widgets[WIDX_SECONDARY_PRICE_LABEL].type = WWT_24;
		window_ride_income_widgets[WIDX_SECONDARY_PRICE].type = WWT_SPINNER;
		window_ride_income_widgets[WIDX_SECONDARY_PRICE_INCREASE].type = WWT_DROPDOWN_BUTTON;
		window_ride_income_widgets[WIDX_SECONDARY_PRICE_DECREASE].type = WWT_DROPDOWN_BUTTON;
		window_ride_income_widgets[WIDX_SECONDARY_PRICE_SAME_THROUGHOUT_PARK].type = WWT_CHECKBOX;

		// Set secondary item price
		window_ride_income_widgets[WIDX_SECONDARY_PRICE].image = 1799;
		set_format_arg(10, money32, ride->price_secondary);
		if (ride->price_secondary == 0)
			window_ride_income_widgets[WIDX_SECONDARY_PRICE].image = STR_FREE;
	}

	window_ride_anchor_border_widgets(w);
	window_align_tabs(w, WIDX_TAB_1, WIDX_TAB_10);
}

/**
 *
 *  rct2: 0x006ADCE5
 */
static void window_ride_income_paint(rct_window *w, rct_drawpixelinfo *dpi)
{
	rct_ride *ride;
	rct_ride_entry *rideEntry;
	rct_string_id stringId;
	money32 profit, costPerHour;
	int x, y, primaryItem, secondaryItem;

	window_draw_widgets(w, dpi);
	window_ride_draw_tab_images(dpi, w);

	ride = get_ride(w->number);
	rideEntry = get_ride_entry_by_ride(ride);

	x = w->x + window_ride_income_widgets[WIDX_PAGE_BACKGROUND].left + 4;
	y = w->y + window_ride_income_widgets[WIDX_PAGE_BACKGROUND].top + 29;

	// Primary item profit / loss per item sold
	primaryItem = rideEntry->shop_item;
	if (primaryItem != SHOP_ITEM_NONE) {
		profit = ride->price;

		stringId = STR_PROFIT_PER_ITEM_SOLD;
		profit -= get_shop_item_cost(primaryItem);
		if (profit < 0) {
			profit *= -1;
			stringId = STR_LOSS_PER_ITEM_SOLD;
		}

		gfx_draw_string_left(dpi, stringId, &profit, 0, x, y);
	}
	y += 39;

	// Secondary item profit / loss per item sold
	secondaryItem = RCT2_GLOBAL(0x0097D7CB + (ride->type * 4), uint8);
	if (!(ride->lifecycle_flags & RIDE_LIFECYCLE_ON_RIDE_PHOTO))
		secondaryItem = rideEntry->shop_item_secondary;

	if (secondaryItem != SHOP_ITEM_NONE) {
		profit = ride->price_secondary;

		stringId = STR_PROFIT_PER_ITEM_SOLD;
		profit -= get_shop_item_cost(secondaryItem);
		if (profit < 0) {
			profit *= -1;
			stringId = STR_LOSS_PER_ITEM_SOLD;
		}

		gfx_draw_string_left(dpi, stringId, &profit, 0, x, y);
	}
	y += 15;

	// Income per hour
	if (ride->income_per_hour != MONEY32_UNDEFINED) {
		gfx_draw_string_left(dpi, STR_INCOME_PER_HOUR, &ride->income_per_hour, 0, x, y);
		y += 10;
	}

	// Running cost per hour
	costPerHour = ride->upkeep_cost * 16;
	stringId = ride->upkeep_cost == (money16)0xFFFF ? STR_RUNNING_COST_UNKNOWN : STR_RUNNING_COST_PER_HOUR;
	gfx_draw_string_left(dpi, stringId, &costPerHour, 0, x, y);
	y += 10;

	// Profit per hour
	if (ride->profit != MONEY32_UNDEFINED) {
		gfx_draw_string_left(dpi, STR_PROFIT_PER_HOUR, &ride->profit, 0, x, y);
		y += 10;
	}
	y += 5;

	// Total profit
	gfx_draw_string_left(dpi, STR_TOTAL_PROFIT, &ride->total_profit, 0, x, y);
}

#pragma endregion

#pragma region Customer

/**
 *
 *  rct2: 0x006AD986
 */
static void window_ride_customer_mouseup(rct_window *w, int widgetIndex)
{
	switch (widgetIndex) {
	case WIDX_CLOSE:
		window_close(w);
		break;
	case WIDX_TAB_1:
	case WIDX_TAB_2:
	case WIDX_TAB_3:
	case WIDX_TAB_4:
	case WIDX_TAB_5:
	case WIDX_TAB_6:
	case WIDX_TAB_7:
	case WIDX_TAB_8:
	case WIDX_TAB_9:
	case WIDX_TAB_10:
		window_ride_set_page(w, widgetIndex - WIDX_TAB_1);
		break;
	case WIDX_SHOW_GUESTS_THOUGHTS:
		window_guest_list_open_with_filter(2, w->number);
		break;
	case WIDX_SHOW_GUESTS_ON_RIDE:
		window_guest_list_open_with_filter(0, w->number);
		break;
	case WIDX_SHOW_GUESTS_QUEUING:
		window_guest_list_open_with_filter(1, w->number);
		break;
	}
}

/**
 *
 *  rct2: 0x006ADA29
 */
static void window_ride_customer_resize(rct_window *w)
{
	w->flags |= WF_RESIZABLE;
	window_set_resize(w, 316, 149, 316, 149);
}

/**
 *
 *  rct2: 0x006AD9DD
 */
static void window_ride_customer_update(rct_window *w)
{
	rct_ride *ride;

	w->var_492++;
	if (w->var_492 >= 24)
		w->var_492 = 0;

	window_event_invalidate_call(w);
	widget_invalidate(w, WIDX_TAB_10);

	ride = get_ride(w->number);
	if (ride->window_invalidate_flags & RIDE_INVALIDATE_RIDE_CUSTOMER) {
		ride->window_invalidate_flags &= ~RIDE_INVALIDATE_RIDE_CUSTOMER;
		window_invalidate(w);
	}
}

/**
 *
 *  rct2: 0x006AD5F8
 */
static void window_ride_customer_invalidate(rct_window *w)
{
	rct_widget *widgets;

	colour_scheme_update(w);

	widgets = window_ride_page_widgets[w->page];
	if (w->widgets != widgets) {
		w->widgets = widgets;
		window_init_scroll_widgets(w);
	}

	window_ride_set_pressed_tab(w);

	rct_ride *ride = get_ride(w->number);
	set_format_arg(0, uint16, ride->name);
	set_format_arg(2, uint32, ride->name_arguments);

	window_ride_customer_widgets[WIDX_SHOW_GUESTS_THOUGHTS].type = WWT_FLATBTN;
	if (ride_type_has_flag(ride->type, RIDE_TYPE_FLAG_IS_SHOP)) {
		window_ride_customer_widgets[WIDX_SHOW_GUESTS_ON_RIDE].type = WWT_EMPTY;
		window_ride_customer_widgets[WIDX_SHOW_GUESTS_QUEUING].type = WWT_EMPTY;
	} else {
		window_ride_customer_widgets[WIDX_SHOW_GUESTS_ON_RIDE].type = WWT_FLATBTN;
		window_ride_customer_widgets[WIDX_SHOW_GUESTS_QUEUING].type = WWT_FLATBTN;
	}

	window_ride_anchor_border_widgets(w);
	window_align_tabs(w, WIDX_TAB_1, WIDX_TAB_10);
}

/**
 *
 *  rct2: 0x006AD6CD
 */
static void window_ride_customer_paint(rct_window *w, rct_drawpixelinfo *dpi)
{
	rct_ride *ride;
	int x, y;
	uint8 shopItem;
	sint16 popularity, satisfaction, queueTime, age;
	sint32 customersPerHour;
	rct_string_id stringId;

	window_draw_widgets(w, dpi);
	window_ride_draw_tab_images(dpi, w);

	ride = get_ride(w->number);
	x = w->x + window_ride_customer_widgets[WIDX_PAGE_BACKGROUND].left + 4;
	y = w->y + window_ride_customer_widgets[WIDX_PAGE_BACKGROUND].top + 4;

	// Customers currently on ride
	if (gRideClassifications[ride->type] == RIDE_CLASS_RIDE) {
		sint16 customersOnRide = ride->num_riders;
		gfx_draw_string_left(dpi, STR_CUSTOMERS_ON_RIDE, &customersOnRide, 0, x, y);
		y += 10;
	}

	// Customers per hour
	customersPerHour = ride_customers_per_hour(ride);
	gfx_draw_string_left(dpi, STR_CUSTOMERS_PER_HOUR, &customersPerHour, 0, x, y);
	y += 10;

	// Popularity
	popularity = ride->popularity;
	if (popularity == 255) {
		stringId = STR_POPULARITY_UNKNOWN;
	} else {
		stringId = STR_POPULARITY_PERCENT;
		popularity *= 4;
	}
	gfx_draw_string_left(dpi, stringId, &popularity, 0, x, y);
	y += 10;

	// Satisfaction
	satisfaction = ride->satisfaction;
	if (satisfaction == 255) {
		stringId = STR_SATISFACTION_UNKNOWN;
	} else {
		stringId = STR_SATISFACTION_PERCENT;
		satisfaction *= 5;
	}
	gfx_draw_string_left(dpi, stringId, &satisfaction, 0, x, y);
	y += 10;

	// Queue time
	if (gRideClassifications[ride->type] == RIDE_CLASS_RIDE) {
		queueTime = ride_get_max_queue_time(ride);
		stringId = queueTime == 1 ? STR_QUEUE_TIME_MINUTE : STR_QUEUE_TIME_MINUTES;
		y += gfx_draw_string_left_wrapped(dpi, &queueTime, x, y, 308, stringId, 0);
		y += 5;
	}

	// Primary shop items sold
	shopItem = get_ride_entry_by_ride(ride)->shop_item;
	if (shopItem != SHOP_ITEM_NONE) {
		set_format_arg(0, uint16, ShopItemStringIds[shopItem].plural);
		set_format_arg(2, uint32, ride->no_primary_items_sold);
		gfx_draw_string_left(dpi, STR_ITEMS_SOLD, gCommonFormatArgs, 0, x, y);
		y += 10;
	}

	// Secondary shop items sold / on-ride photos sold
	shopItem = ride->lifecycle_flags & RIDE_LIFECYCLE_ON_RIDE_PHOTO ?
		RCT2_GLOBAL(0x0097D7CB + (ride->type * 4), uint8) :
		get_ride_entry_by_ride(ride)->shop_item_secondary;
	if (shopItem != SHOP_ITEM_NONE) {
		set_format_arg(0, uint16, ShopItemStringIds[shopItem].plural);
		set_format_arg(2, uint32, ride->no_secondary_items_sold);
		gfx_draw_string_left(dpi, STR_ITEMS_SOLD, gCommonFormatArgs, 0, x, y);
		y += 10;
	}

	// Total customers
	gfx_draw_string_left(dpi, STR_TOTAL_CUSTOMERS, &ride->total_customers, 0, x, y);
	y += 10;

	// Guests favourite
	if (gRideClassifications[ride->type] == RIDE_CLASS_RIDE) {
		stringId = ride->guests_favourite == 1 ?
			STR_FAVOURITE_RIDE_OF_GUEST :
			STR_FAVOURITE_RIDE_OF_GUESTS;
		gfx_draw_string_left(dpi, stringId, &ride->guests_favourite, 0, x, y);
		y += 10;
	}
	y += 2;

	// Age
	//If the ride has a build date that is in the future, show it as built this year.
	age = max((gDateMonthsElapsed - ride->build_date) / 8, 0);
	stringId = age == 0 ?
		STR_BUILT_THIS_YEAR :
		age == 1 ?
			STR_BUILT_LAST_YEAR :
			STR_BUILT_YEARS_AGO;
	gfx_draw_string_left(dpi, stringId, &age, 0, x, y);
}

#pragma endregion
