OpenRCT2/src/openrct2-ui/windows/NewCampaign.cpp

426 lines
15 KiB
C++
Raw Normal View History

/*****************************************************************************
2020-07-21 15:04:34 +02:00
* Copyright (c) 2014-2020 OpenRCT2 developers
*
* For a complete list of all authors, please refer to contributors.md
* Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2
*
* OpenRCT2 is licensed under the GNU General Public License version 3.
*****************************************************************************/
#include <algorithm>
2018-06-22 23:21:44 +02:00
#include <openrct2-ui/interface/Dropdown.h>
#include <openrct2-ui/interface/Widget.h>
#include <openrct2-ui/windows/Window.h>
#include <openrct2/Game.h>
Split actions hpp files into separate h and cpp files (#13548) * Split up SmallSceneryPlace/Remove Added undo function for Remove Scenery * Refactor: Balloon and Banner actions hpp=>h/cpp * Refactor: rename all action *.hpp files to *.cpp This is preparation for separation in later commits. Note that without the complete set of commits in this branch, the code will not build. * Refactor Clear, Climate, Custom, and Footpath actions hpp=>h/cpp * VSCode: add src subdirectories to includePath * Refactor Guest actions hpp=>h/cpp * Refactor Land actions hpp=>h/cpp * Refactor LargeScenery actions hpp=>h/cpp * Refactor Load, Maze, Network actions hpp=>h/cpp * Refactor Park actions hpp=>h/cpp * Refactor/style: move private function declarations in actions *.h Previous action .h files included private function declarations with private member variables, before public function declarations. This commit re-orders the header files to the following order: - public member variables - private member variables - public functions - private functions * Refactor Pause action hpp=>h/cpp * Refactor Peep, Place, Player actions hpp=>h/cpp * Refactor Ride actions hpp=>h/cpp * Refactor Scenario, Set*, Sign* actions hpp=>h/cpp * Refactor SmallScenerySetColourAction hpp=>h/cpp * Refactor Staff actions hpp=>h/cpp * Refactor Surface, Tile, Track* actions hpp=>h/cpp * Refactor Wall and Water actions hpp=>h/cpp * Fix various includes and other compile errors Update includes for tests. Move static function declarations to .h files Add explicit includes to various files that were previously implicit (the required header was a nested include in an action hpp file, and the action .h file does not include that header) Move RideSetStatus string enum to the cpp file to avoid unused imports * Xcode: modify project file for actions refactor * Cleanup whitespace and end-of-file newlines Co-authored-by: duncanspumpkin <duncans_pumpkin@hotmail.co.uk>
2020-12-10 07:39:10 +01:00
#include <openrct2/actions/ParkMarketingAction.h>
#include <openrct2/config/Config.h>
#include <openrct2/drawing/Drawing.h>
2018-01-06 18:32:25 +01:00
#include <openrct2/localisation/Localisation.h>
2017-12-31 13:21:34 +01:00
#include <openrct2/ride/Ride.h>
2018-01-10 00:00:09 +01:00
#include <openrct2/ride/RideData.h>
#include <openrct2/ride/ShopItem.h>
2014-08-12 01:27:25 +02:00
2020-05-09 18:02:16 +02:00
static constexpr const rct_string_id WINDOW_TITLE = STR_NONE;
2020-07-30 21:54:17 +02:00
static constexpr const int32_t WH = 109;
2020-05-05 22:26:14 +02:00
static constexpr const int32_t WW = 350;
2019-10-24 05:38:13 +02:00
constexpr uint16_t SELECTED_RIDE_UNDEFINED = 0xFFFF;
2014-08-12 01:27:25 +02:00
// clang-format off
2014-08-12 01:27:25 +02:00
enum WINDOW_NEW_CAMPAIGN_WIDGET_IDX {
WIDX_BACKGROUND,
WIDX_TITLE,
WIDX_CLOSE,
WIDX_RIDE_LABEL,
WIDX_RIDE_DROPDOWN,
WIDX_RIDE_DROPDOWN_BUTTON,
WIDX_WEEKS_LABEL,
WIDX_WEEKS_SPINNER,
WIDX_WEEKS_INCREASE_BUTTON,
WIDX_WEEKS_DECREASE_BUTTON,
WIDX_START_BUTTON
2014-08-12 01:27:25 +02:00
};
static rct_widget window_new_campaign_widgets[] = {
2020-05-09 16:44:21 +02:00
WINDOW_SHIM(WINDOW_TITLE, WW, WH),
MakeWidget ({ 14, 24}, {126, 12}, WindowWidgetType::Label, WindowColour::Primary, STR_EMPTY ), // ride label
MakeWidget ({100, 24}, {242, 12}, WindowWidgetType::DropdownMenu, WindowColour::Primary, STR_EMPTY ), // ride dropdown
MakeWidget ({330, 25}, { 11, 10}, WindowWidgetType::Button, WindowColour::Primary, STR_DROPDOWN_GLYPH ), // ride dropdown button
MakeWidget ({ 14, 41}, {126, 14}, WindowWidgetType::Label, WindowColour::Primary, STR_LENGTH_OF_TIME ), // weeks label
MakeSpinnerWidgets({120, 41}, {100, 14}, WindowWidgetType::Spinner, WindowColour::Primary, STR_EMPTY ), // weeks (3 widgets)
MakeWidget ({ 14, 89}, {322, 14}, WindowWidgetType::Button, WindowColour::Primary, STR_MARKETING_START_THIS_MARKETING_CAMPAIGN), // start button
{ WIDGETS_END }
2014-08-12 01:27:25 +02:00
};
2017-05-01 15:41:45 +02:00
static void window_new_campaign_mouseup(rct_window *w, rct_widgetindex widgetIndex);
static void window_new_campaign_mousedown(rct_window *w, rct_widgetindex widgetIndex, rct_widget* widget);
static void window_new_campaign_dropdown(rct_window *w, rct_widgetindex widgetIndex, int32_t dropdownIndex);
static void window_new_campaign_invalidate(rct_window *w);
static void window_new_campaign_paint(rct_window *w, rct_drawpixelinfo *dpi);
2014-08-12 01:27:25 +02:00
static rct_window_event_list window_new_campaign_events([](auto& events)
{
events.mouse_up = &window_new_campaign_mouseup;
events.mouse_down = &window_new_campaign_mousedown;
events.dropdown = &window_new_campaign_dropdown;
events.invalidate = &window_new_campaign_invalidate;
events.paint = &window_new_campaign_paint;
});
// clang-format on
2014-08-12 01:27:25 +02:00
static std::vector<ride_id_t> window_new_campaign_rides;
static uint8_t window_new_campaign_shop_items[64];
2014-08-12 01:27:25 +02:00
2018-06-22 23:21:44 +02:00
static int32_t ride_value_compare(const void* a, const void* b)
2014-08-12 01:27:25 +02:00
{
auto valueA = 0;
auto rideA = get_ride(*(static_cast<const uint8_t*>(a)));
if (rideA != nullptr)
valueA = rideA->value;
auto valueB = 0;
auto rideB = get_ride(*(static_cast<const uint8_t*>(b)));
if (rideB != nullptr)
valueB = rideB->value;
2014-08-12 01:27:25 +02:00
return valueB - valueA;
2014-08-12 01:27:25 +02:00
}
2018-06-22 23:21:44 +02:00
static int32_t ride_name_compare(const void* a, const void* b)
2014-08-12 01:27:25 +02:00
{
std::string rideAName;
auto rideA = get_ride(*(static_cast<const uint8_t*>(a)));
if (rideA != nullptr)
rideAName = rideA->GetName();
std::string rideBName;
auto rideB = get_ride(*(static_cast<const uint8_t*>(b)));
if (rideB != nullptr)
rideBName = rideB->GetName();
2019-07-21 02:28:33 +02:00
return _strcmpi(rideAName.c_str(), rideBName.c_str());
2014-08-12 01:27:25 +02:00
}
/**
2015-10-20 20:16:30 +02:00
*
* rct2: 0x0069E16F
*/
2018-06-22 23:21:44 +02:00
rct_window* window_new_campaign_open(int16_t campaignType)
{
auto w = window_bring_to_front_by_class(WC_NEW_CAMPAIGN);
2018-06-22 23:21:44 +02:00
if (w != nullptr)
{
if (w->campaign.campaign_type == campaignType)
return w;
window_close(w);
}
w = WindowCreateAutoPos(WW, WH, &window_new_campaign_events, WC_NEW_CAMPAIGN, 0);
w->widgets = window_new_campaign_widgets;
2018-06-22 23:21:44 +02:00
w->enabled_widgets = (1 << WIDX_CLOSE) | (1 << WIDX_RIDE_DROPDOWN) | (1 << WIDX_RIDE_DROPDOWN_BUTTON)
| (1 << WIDX_WEEKS_INCREASE_BUTTON) | (1 << WIDX_WEEKS_DECREASE_BUTTON) | (1 << WIDX_START_BUTTON);
w->hold_down_widgets = (1 << WIDX_WEEKS_INCREASE_BUTTON) | (1 << WIDX_WEEKS_DECREASE_BUTTON);
WindowInitScrollWidgets(w);
window_new_campaign_widgets[WIDX_TITLE].text = MarketingCampaignNames[campaignType][0];
// Campaign type
w->campaign.campaign_type = campaignType;
// Number of weeks
w->campaign.no_weeks = 2;
// Currently selected ride
w->campaign.RideId = SELECTED_RIDE_UNDEFINED;
WindowCampaignRefreshRides();
return w;
2014-08-12 01:27:25 +02:00
}
/**
2015-10-20 20:16:30 +02:00
*
2014-08-12 01:27:25 +02:00
* rct2: 0x0069E320
*/
static void window_new_campaign_get_shop_items()
{
uint64_t items = 0;
2019-08-04 17:00:15 +02:00
for (auto& ride : GetRideManager())
{
2019-08-04 17:00:15 +02:00
auto rideEntry = ride.GetRideEntry();
if (rideEntry != nullptr)
{
for (const auto itemType : rideEntry->shop_item)
2019-08-04 17:00:15 +02:00
{
if (itemType != ShopItem::None && GetShopItemDescriptor(itemType).IsFoodOrDrink())
{
items |= EnumToFlag(itemType);
}
2019-08-04 17:00:15 +02:00
}
}
}
//
2019-08-04 17:00:15 +02:00
auto numItems = 0;
for (auto i = 0; i < 64; i++)
{
if (items & (1ULL << i))
{
window_new_campaign_shop_items[numItems++] = i;
}
}
window_new_campaign_shop_items[numItems] = 255;
2014-08-12 01:27:25 +02:00
}
/**
2015-10-20 20:16:30 +02:00
*
2014-08-12 01:27:25 +02:00
* rct2: 0x0069E50B
*/
2018-06-22 23:21:44 +02:00
static void window_new_campaign_mouseup(rct_window* w, rct_widgetindex widgetIndex)
2014-08-12 01:27:25 +02:00
{
switch (widgetIndex)
{
2018-06-22 23:21:44 +02:00
case WIDX_CLOSE:
window_close(w);
break;
case WIDX_START_BUTTON:
{
auto gameAction = ParkMarketingAction(w->campaign.campaign_type, w->campaign.RideId, w->campaign.no_weeks);
gameAction.SetCallback([](const GameAction* ga, const GameActions::Result* result) {
if (result->Error == GameActions::Status::Ok)
{
2018-06-22 23:21:44 +02:00
window_close_by_class(WC_NEW_CAMPAIGN);
}
});
GameActions::Execute(&gameAction);
break;
}
}
2014-08-12 01:27:25 +02:00
}
/**
2015-10-20 20:16:30 +02:00
*
2014-08-12 01:27:25 +02:00
* rct2: 0x0069E51C
*/
2018-06-22 23:21:44 +02:00
static void window_new_campaign_mousedown(rct_window* w, rct_widgetindex widgetIndex, rct_widget* widget)
2014-08-12 01:27:25 +02:00
{
2018-06-22 23:21:44 +02:00
rct_widget* dropdownWidget;
2018-06-22 23:21:44 +02:00
switch (widgetIndex)
{
case WIDX_RIDE_DROPDOWN_BUTTON:
dropdownWidget = widget - 1;
2018-06-22 23:21:44 +02:00
if (w->campaign.campaign_type == ADVERTISING_CAMPAIGN_FOOD_OR_DRINK_FREE)
{
window_new_campaign_get_shop_items();
if (window_new_campaign_shop_items[0] != 255)
{
int32_t numItems = 0;
for (int32_t i = 0; i < Dropdown::ItemsMaxSize; i++)
2018-06-22 23:21:44 +02:00
{
if (window_new_campaign_shop_items[i] == 255)
break;
gDropdownItemsFormat[i] = STR_DROPDOWN_MENU_LABEL;
gDropdownItemsArgs[i] = GetShopItemDescriptor(ShopItem(window_new_campaign_shop_items[i]))
.Naming.Plural;
2018-06-22 23:21:44 +02:00
numItems++;
}
WindowDropdownShowTextCustomWidth(
{ w->windowPos.x + dropdownWidget->left, w->windowPos.y + dropdownWidget->top },
dropdownWidget->height() + 1, w->colours[1], 0, Dropdown::Flag::StayOpen, numItems,
dropdownWidget->width() - 3);
2018-06-22 23:21:44 +02:00
}
}
else
{
int32_t numItems = 0;
for (auto rideId : window_new_campaign_rides)
2018-06-22 23:21:44 +02:00
{
auto ride = get_ride(rideId);
if (ride != nullptr)
2019-07-21 13:09:23 +02:00
{
// HACK until dropdown items have longer argument buffers
gDropdownItemsFormat[numItems] = STR_DROPDOWN_MENU_LABEL;
Formatter ft(reinterpret_cast<uint8_t*>(&gDropdownItemsArgs[numItems]));
if (ride->custom_name.empty())
{
ride->FormatNameTo(ft);
}
else
{
gDropdownItemsFormat[numItems] = STR_OPTIONS_DROPDOWN_ITEM;
ft.Add<const char*>(ride->custom_name.c_str());
}
numItems++;
2019-07-21 13:09:23 +02:00
}
}
WindowDropdownShowTextCustomWidth(
{ w->windowPos.x + dropdownWidget->left, w->windowPos.y + dropdownWidget->top },
dropdownWidget->height() + 1, w->colours[1], 0, Dropdown::Flag::StayOpen, numItems,
dropdownWidget->width() - 3);
}
2018-06-22 23:21:44 +02:00
break;
// In RCT2, the maximum was 6 weeks
case WIDX_WEEKS_INCREASE_BUTTON:
w->campaign.no_weeks = std::min(w->campaign.no_weeks + 1, 12);
w->Invalidate();
2018-06-22 23:21:44 +02:00
break;
case WIDX_WEEKS_DECREASE_BUTTON:
w->campaign.no_weeks = std::max(w->campaign.no_weeks - 1, 2);
w->Invalidate();
2018-06-22 23:21:44 +02:00
break;
}
2014-08-12 01:27:25 +02:00
}
/**
2015-10-20 20:16:30 +02:00
*
2014-08-12 01:27:25 +02:00
* rct2: 0x0069E537
*/
2018-06-22 23:21:44 +02:00
static void window_new_campaign_dropdown(rct_window* w, rct_widgetindex widgetIndex, int32_t dropdownIndex)
2014-08-12 01:27:25 +02:00
{
if (widgetIndex != WIDX_RIDE_DROPDOWN_BUTTON)
return;
2014-08-12 01:27:25 +02:00
if (dropdownIndex < 0)
return;
2018-06-22 23:21:44 +02:00
if (w->campaign.campaign_type == ADVERTISING_CAMPAIGN_FOOD_OR_DRINK_FREE)
{
if (static_cast<size_t>(dropdownIndex) >= std::size(window_new_campaign_shop_items))
return;
if (window_new_campaign_shop_items[dropdownIndex] == 255)
return;
w->campaign.ShopItemId = window_new_campaign_shop_items[dropdownIndex];
2018-06-22 23:21:44 +02:00
}
else
{
if (static_cast<size_t>(dropdownIndex) >= window_new_campaign_rides.size())
return;
w->campaign.RideId = window_new_campaign_rides[dropdownIndex];
}
2014-08-12 01:27:25 +02:00
w->Invalidate();
2014-08-12 01:27:25 +02:00
}
/**
2015-10-20 20:16:30 +02:00
*
2014-08-12 01:27:25 +02:00
* rct2: 0x0069E397
*/
2018-06-22 23:21:44 +02:00
static void window_new_campaign_invalidate(rct_window* w)
2014-08-12 01:27:25 +02:00
{
window_new_campaign_widgets[WIDX_RIDE_LABEL].type = WindowWidgetType::Empty;
window_new_campaign_widgets[WIDX_RIDE_DROPDOWN].type = WindowWidgetType::Empty;
window_new_campaign_widgets[WIDX_RIDE_DROPDOWN_BUTTON].type = WindowWidgetType::Empty;
window_new_campaign_widgets[WIDX_RIDE_DROPDOWN].text = STR_MARKETING_NOT_SELECTED;
2018-06-22 23:21:44 +02:00
switch (w->campaign.campaign_type)
{
case ADVERTISING_CAMPAIGN_RIDE_FREE:
case ADVERTISING_CAMPAIGN_RIDE:
window_new_campaign_widgets[WIDX_RIDE_LABEL].type = WindowWidgetType::Label;
window_new_campaign_widgets[WIDX_RIDE_DROPDOWN].type = WindowWidgetType::DropdownMenu;
window_new_campaign_widgets[WIDX_RIDE_DROPDOWN_BUTTON].type = WindowWidgetType::Button;
2018-06-22 23:21:44 +02:00
window_new_campaign_widgets[WIDX_RIDE_LABEL].text = STR_MARKETING_RIDE;
if (w->campaign.RideId != SELECTED_RIDE_UNDEFINED)
2018-06-22 23:21:44 +02:00
{
auto ride = get_ride(w->campaign.RideId);
2019-07-21 02:28:33 +02:00
if (ride != nullptr)
{
window_new_campaign_widgets[WIDX_RIDE_DROPDOWN].text = STR_STRINGID;
auto ft = Formatter::Common();
ride->FormatNameTo(ft);
2019-07-21 02:28:33 +02:00
}
2018-06-22 23:21:44 +02:00
}
break;
case ADVERTISING_CAMPAIGN_FOOD_OR_DRINK_FREE:
window_new_campaign_widgets[WIDX_RIDE_LABEL].type = WindowWidgetType::Label;
window_new_campaign_widgets[WIDX_RIDE_DROPDOWN].type = WindowWidgetType::DropdownMenu;
window_new_campaign_widgets[WIDX_RIDE_DROPDOWN_BUTTON].type = WindowWidgetType::Button;
2018-06-22 23:21:44 +02:00
window_new_campaign_widgets[WIDX_RIDE_LABEL].text = STR_MARKETING_ITEM;
if (w->campaign.ShopItemId != SELECTED_RIDE_UNDEFINED)
2018-06-22 23:21:44 +02:00
{
window_new_campaign_widgets[WIDX_RIDE_DROPDOWN].text = GetShopItemDescriptor(ShopItem(w->campaign.ShopItemId))
.Naming.Plural;
2018-06-22 23:21:44 +02:00
}
break;
}
// Set current number of weeks spinner (moved to paint due to required parameter)
window_new_campaign_widgets[WIDX_WEEKS_SPINNER].text = STR_NONE;
// Enable / disable start button based on ride dropdown
w->disabled_widgets &= ~(1 << WIDX_START_BUTTON);
if (window_new_campaign_widgets[WIDX_RIDE_DROPDOWN].type == WindowWidgetType::DropdownMenu
&& w->campaign.RideId == SELECTED_RIDE_UNDEFINED)
w->disabled_widgets |= 1 << WIDX_START_BUTTON;
2014-08-12 01:27:25 +02:00
}
/**
2015-10-20 20:16:30 +02:00
*
2014-08-12 01:27:25 +02:00
* rct2: 0x0069E493
*/
2018-06-22 23:21:44 +02:00
static void window_new_campaign_paint(rct_window* w, rct_drawpixelinfo* dpi)
2014-08-12 01:27:25 +02:00
{
ScreenCoordsXY screenCoords{};
WindowDrawWidgets(w, dpi);
// Number of weeks
2018-06-22 23:21:44 +02:00
rct_widget* spinnerWidget = &window_new_campaign_widgets[WIDX_WEEKS_SPINNER];
DrawTextBasic(
dpi, w->windowPos + ScreenCoordsXY{ spinnerWidget->left + 1, spinnerWidget->top },
w->campaign.no_weeks == 1 ? STR_MARKETING_1_WEEK : STR_X_WEEKS, &w->campaign.no_weeks, { w->colours[0] });
screenCoords = w->windowPos + ScreenCoordsXY{ 14, 60 };
// Price per week
money32 pricePerWeek = AdvertisingCampaignPricePerWeek[w->campaign.campaign_type];
DrawTextBasic(dpi, screenCoords, STR_MARKETING_COST_PER_WEEK, &pricePerWeek);
screenCoords.y += 13;
// Total price
money32 totalPrice = AdvertisingCampaignPricePerWeek[w->campaign.campaign_type] * w->campaign.no_weeks;
DrawTextBasic(dpi, screenCoords, STR_MARKETING_TOTAL_COST, &totalPrice);
}
void WindowCampaignRefreshRides()
{
auto window = window_find_by_class(WC_NEW_CAMPAIGN);
if (window == nullptr)
{
return;
}
// Get all applicable rides
window_new_campaign_rides.clear();
for (const auto& ride : GetRideManager())
{
if (ride.status == RIDE_STATUS_OPEN)
{
if (!ride.GetRideTypeDescriptor().HasFlag(
RIDE_TYPE_FLAG_IS_SHOP | RIDE_TYPE_FLAG_SELLS_FOOD | RIDE_TYPE_FLAG_SELLS_DRINKS
| RIDE_TYPE_FLAG_IS_TOILET))
{
window_new_campaign_rides.push_back(ride.id);
}
}
}
// Take top 128 most valuable rides
if (window_new_campaign_rides.size() > Dropdown::ItemsMaxSize)
{
qsort(window_new_campaign_rides.data(), window_new_campaign_rides.size(), sizeof(ride_id_t), ride_value_compare);
window_new_campaign_rides.resize(Dropdown::ItemsMaxSize);
}
// Sort rides by name
qsort(window_new_campaign_rides.data(), window_new_campaign_rides.size(), sizeof(ride_id_t), ride_name_compare);
}