Skip to content

Commit

Permalink
Move gResearch* globals to GameState_t
Browse files Browse the repository at this point in the history
  • Loading branch information
Gymnasiast committed Jan 29, 2024
1 parent eb99798 commit 8208afe
Show file tree
Hide file tree
Showing 12 changed files with 286 additions and 230 deletions.
21 changes: 15 additions & 6 deletions src/openrct2-ui/windows/EditorInventionsList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <openrct2-ui/interface/Widget.h>
#include <openrct2-ui/windows/Window.h>
#include <openrct2/Editor.h>
#include <openrct2/GameState.h>
#include <openrct2/Input.h>
#include <openrct2/OpenRCT2.h>
#include <openrct2/interface/Cursors.h>
Expand All @@ -27,6 +28,8 @@

#pragma region Widgets

using namespace OpenRCT2;

static constexpr int32_t WW = 600;
static constexpr int32_t WH = 400;
static constexpr StringId WINDOW_TITLE = STR_INVENTION_LIST;
Expand Down Expand Up @@ -220,14 +223,15 @@ class InventionListWindow final : public Window

ScreenSize OnScrollGetSize(int32_t scrollIndex) override
{
const auto& gameState = GetGameState();
ScreenSize size{};
if (scrollIndex == 0)
{
size.height = static_cast<int32_t>(gResearchItemsInvented.size()) * SCROLLABLE_ROW_HEIGHT;
size.height = static_cast<int32_t>(gameState.ResearchItemsInvented.size()) * SCROLLABLE_ROW_HEIGHT;
}
else
{
size.height = static_cast<int32_t>(gResearchItemsUninvented.size()) * SCROLLABLE_ROW_HEIGHT;
size.height = static_cast<int32_t>(gameState.ResearchItemsUninvented.size()) * SCROLLABLE_ROW_HEIGHT;
}
return size;
}
Expand Down Expand Up @@ -265,6 +269,8 @@ class InventionListWindow final : public Window

void OnScrollDraw(int32_t scrollIndex, DrawPixelInfo& dpi) override
{
const auto& gameState = GetGameState();

// Draw background
uint8_t paletteIndex = ColourMapA[colours[1]].mid_light;
GfxClear(&dpi, paletteIndex);
Expand All @@ -273,7 +279,7 @@ class InventionListWindow final : public Window
int32_t itemY = -SCROLLABLE_ROW_HEIGHT;
auto* dragItem = WindowEditorInventionsListDragGetItem();

const auto& researchList = scrollIndex == 0 ? gResearchItemsInvented : gResearchItemsUninvented;
const auto& researchList = scrollIndex == 0 ? gameState.ResearchItemsInvented : gameState.ResearchItemsUninvented;
for (const auto& researchItem : researchList)
{
itemY += SCROLLABLE_ROW_HEIGHT;
Expand Down Expand Up @@ -512,6 +518,7 @@ class InventionListWindow final : public Window

void MoveResearchItem(const ResearchItem& item, ResearchItem* beforeItem, bool isInvented)
{
auto& gameState = GetGameState();
_selectedResearchItem = nullptr;
Invalidate();

Expand All @@ -524,7 +531,7 @@ class InventionListWindow final : public Window

ResearchRemove(item);

auto& researchList = isInvented ? gResearchItemsInvented : gResearchItemsUninvented;
auto& researchList = isInvented ? gameState.ResearchItemsInvented : gameState.ResearchItemsUninvented;
if (beforeItem != nullptr)
{
for (size_t i = 0; i < researchList.size(); i++)
Expand All @@ -544,7 +551,8 @@ class InventionListWindow final : public Window
private:
ResearchItem* GetItemFromScrollY(bool isInvented, int32_t y) const
{
auto& researchList = isInvented ? gResearchItemsInvented : gResearchItemsUninvented;
auto& gameState = GetGameState();
auto& researchList = isInvented ? gameState.ResearchItemsInvented : gameState.ResearchItemsUninvented;
for (auto& researchItem : researchList)
{
y -= SCROLLABLE_ROW_HEIGHT;
Expand All @@ -559,7 +567,8 @@ class InventionListWindow final : public Window

ResearchItem* GetItemFromScrollYIncludeSeps(bool isInvented, int32_t y) const
{
auto& researchList = isInvented ? gResearchItemsInvented : gResearchItemsUninvented;
auto& gameState = GetGameState();
auto& researchList = isInvented ? gameState.ResearchItemsInvented : gameState.ResearchItemsUninvented;
for (auto& researchItem : researchList)
{
y -= SCROLLABLE_ROW_HEIGHT;
Expand Down
75 changes: 42 additions & 33 deletions src/openrct2-ui/windows/Research.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -329,24 +329,26 @@ static WidgetIndex GetWidgetIndexOffset(WidgetIndex baseWidgetIndex, WidgetIndex

void WindowResearchDevelopmentMouseUp(WidgetIndex widgetIndex, WidgetIndex baseWidgetIndex)
{
const auto& gameState = GetGameState();
auto widgetOffset = GetWidgetIndexOffset(baseWidgetIndex, WIDX_CURRENTLY_IN_DEVELOPMENT_GROUP);

if (widgetIndex == (WIDX_LAST_DEVELOPMENT_BUTTON + widgetOffset))
{
News::OpenSubject(News::ItemType::Research, gResearchLastItem->rawValue);
News::OpenSubject(News::ItemType::Research, gameState.ResearchLastItem->rawValue);
}
}

void WindowResearchDevelopmentPrepareDraw(WindowBase* w, WidgetIndex baseWidgetIndex)
{
const auto& gameState = GetGameState();
// Offset the widget index to allow reuse from other windows
auto widgetOffset = GetWidgetIndexOffset(baseWidgetIndex, WIDX_CURRENTLY_IN_DEVELOPMENT_GROUP);
w->widgets[WIDX_LAST_DEVELOPMENT_BUTTON + widgetOffset].type = WindowWidgetType::Empty;

// Display button to link to the last development, if there is one
if (gResearchLastItem.has_value())
if (gameState.ResearchLastItem.has_value())
{
auto type = gResearchLastItem->type;
auto type = gameState.ResearchLastItem->type;
w->widgets[WIDX_LAST_DEVELOPMENT_BUTTON + widgetOffset].type = WindowWidgetType::FlatBtn;
const auto image = type == Research::EntryType::Ride ? SPR_NEW_RIDE : SPR_NEW_SCENERY;
w->widgets[WIDX_LAST_DEVELOPMENT_BUTTON + widgetOffset].image = ImageId(image);
Expand All @@ -355,11 +357,13 @@ void WindowResearchDevelopmentPrepareDraw(WindowBase* w, WidgetIndex baseWidgetI

void WindowResearchDevelopmentDraw(WindowBase* w, DrawPixelInfo& dpi, WidgetIndex baseWidgetIndex)
{
const auto& gameState = GetGameState();

auto widgetOffset = GetWidgetIndexOffset(baseWidgetIndex, WIDX_CURRENTLY_IN_DEVELOPMENT_GROUP);
auto screenCoords = w->windowPos
+ ScreenCoordsXY{ 10, w->widgets[WIDX_CURRENTLY_IN_DEVELOPMENT_GROUP + widgetOffset].top + 12 };

if (gResearchProgressStage == RESEARCH_STAGE_FINISHED_ALL)
if (gameState.ResearchProgressStage == RESEARCH_STAGE_FINISHED_ALL)
{
// Research type
auto ft = Formatter();
Expand All @@ -383,53 +387,53 @@ void WindowResearchDevelopmentDraw(WindowBase* w, DrawPixelInfo& dpi, WidgetInde
// Research type
auto ft = Formatter();
StringId label = STR_RESEARCH_TYPE_LABEL;
if (gResearchProgressStage == RESEARCH_STAGE_INITIAL_RESEARCH)
if (gameState.ResearchProgressStage == RESEARCH_STAGE_INITIAL_RESEARCH)
{
ft.Add<StringId>(STR_RESEARCH_UNKNOWN);
}
else if (gResearchProgressStage == RESEARCH_STAGE_DESIGNING)
else if (gameState.ResearchProgressStage == RESEARCH_STAGE_DESIGNING)
{
ft.Add<StringId>(gResearchNextItem->GetCategoryName());
ft.Add<StringId>(gameState.ResearchNextItem->GetCategoryName());
}
else if (gResearchNextItem->type == Research::EntryType::Ride)
else if (gameState.ResearchNextItem->type == Research::EntryType::Ride)
{
const auto& rtd = GetRideTypeDescriptor(gResearchNextItem->baseRideType);
const auto& rtd = GetRideTypeDescriptor(gameState.ResearchNextItem->baseRideType);
if (rtd.HasFlag(RIDE_TYPE_FLAG_LIST_VEHICLES_SEPARATELY))
{
ft.Add<StringId>(gResearchNextItem->GetName());
ft.Add<StringId>(gameState.ResearchNextItem->GetName());
}
else if (gResearchNextItem->flags & RESEARCH_ENTRY_FLAG_FIRST_OF_TYPE)
else if (gameState.ResearchNextItem->flags & RESEARCH_ENTRY_FLAG_FIRST_OF_TYPE)
{
ft.Add<StringId>(rtd.Naming.Name);
}
else
{
ft.Add<StringId>(gResearchNextItem->GetName());
ft.Add<StringId>(gameState.ResearchNextItem->GetName());
ft.Add<StringId>(rtd.Naming.Name);
label = STR_RESEARCH_TYPE_LABEL_VEHICLE;
}
}
else
{
ft.Add<StringId>(gResearchNextItem->GetName());
ft.Add<StringId>(gameState.ResearchNextItem->GetName());
}
DrawTextWrapped(dpi, screenCoords, 296, label, ft);
screenCoords.y += 25;

// Progress
ft = Formatter();
ft.Add<StringId>(ResearchStageNames[gResearchProgressStage]);
ft.Add<StringId>(ResearchStageNames[gameState.ResearchProgressStage]);
DrawTextWrapped(dpi, screenCoords, 296, STR_RESEARCH_PROGRESS_LABEL, ft);
screenCoords.y += 15;

// Expected
ft = Formatter();
if (gResearchProgressStage != RESEARCH_STAGE_INITIAL_RESEARCH && gResearchExpectedDay != 255)
if (gameState.ResearchProgressStage != RESEARCH_STAGE_INITIAL_RESEARCH && gameState.ResearchExpectedDay != 255)
{
// TODO: Should probably use game date format setting
ft.Add<StringId>(STR_RESEARCH_EXPECTED_FORMAT);
ft.Add<StringId>(DateDayNames[gResearchExpectedDay]);
ft.Add<StringId>(DateGameMonthNames[gResearchExpectedMonth]);
ft.Add<StringId>(DateDayNames[gameState.ResearchExpectedDay]);
ft.Add<StringId>(DateGameMonthNames[gameState.ResearchExpectedMonth]);
}
else
{
Expand All @@ -441,30 +445,30 @@ void WindowResearchDevelopmentDraw(WindowBase* w, DrawPixelInfo& dpi, WidgetInde
// Last development
screenCoords = w->windowPos + ScreenCoordsXY{ 10, w->widgets[WIDX_LAST_DEVELOPMENT_GROUP + widgetOffset].top + 12 };

if (gResearchLastItem.has_value())
if (gameState.ResearchLastItem.has_value())
{
StringId lastDevelopmentFormat = STR_EMPTY;
auto ft = Formatter();
if (gResearchLastItem->type == Research::EntryType::Scenery)
if (gameState.ResearchLastItem->type == Research::EntryType::Scenery)
{
lastDevelopmentFormat = STR_RESEARCH_SCENERY_LABEL;
ft.Add<StringId>(gResearchLastItem->GetName());
ft.Add<StringId>(gameState.ResearchLastItem->GetName());
}
else
{
lastDevelopmentFormat = STR_RESEARCH_RIDE_LABEL;
const auto& rtd = GetRideTypeDescriptor(gResearchLastItem->baseRideType);
const auto& rtd = GetRideTypeDescriptor(gameState.ResearchLastItem->baseRideType);
if (rtd.HasFlag(RIDE_TYPE_FLAG_LIST_VEHICLES_SEPARATELY))
{
ft.Add<StringId>(gResearchLastItem->GetName());
ft.Add<StringId>(gameState.ResearchLastItem->GetName());
}
else if (gResearchLastItem->flags & RESEARCH_ENTRY_FLAG_FIRST_OF_TYPE)
else if (gameState.ResearchLastItem->flags & RESEARCH_ENTRY_FLAG_FIRST_OF_TYPE)
{
ft.Add<StringId>(rtd.Naming.Name);
}
else
{
ft.Add<StringId>(gResearchLastItem->GetName());
ft.Add<StringId>(gameState.ResearchLastItem->GetName());
ft.Add<StringId>(rtd.Naming.Name);
lastDevelopmentFormat = STR_RESEARCH_VEHICLE_LABEL;
}
Expand All @@ -480,6 +484,7 @@ void WindowResearchDevelopmentDraw(WindowBase* w, DrawPixelInfo& dpi, WidgetInde

void WindowResearchFundingMouseDown(WindowBase* w, WidgetIndex widgetIndex, WidgetIndex baseWidgetIndex)
{
const auto& gameState = GetGameState();
auto widgetOffset = GetWidgetIndexOffset(baseWidgetIndex, WIDX_RESEARCH_FUNDING);

if (widgetIndex != (WIDX_RESEARCH_FUNDING_DROPDOWN_BUTTON + widgetOffset))
Expand All @@ -496,12 +501,13 @@ void WindowResearchFundingMouseDown(WindowBase* w, WidgetIndex widgetIndex, Widg
{ w->windowPos.x + dropdownWidget->left, w->windowPos.y + dropdownWidget->top }, dropdownWidget->height() + 1,
w->colours[1], 0, Dropdown::Flag::StayOpen, 4, dropdownWidget->width() - 3);

int32_t currentResearchLevel = gResearchFundingLevel;
int32_t currentResearchLevel = gameState.ResearchFundingLevel;
Dropdown::SetChecked(currentResearchLevel, true);
}

void WindowResearchFundingMouseUp(WidgetIndex widgetIndex, WidgetIndex baseWidgetIndex)
{
const auto& gameState = GetGameState();
auto widgetOffset = GetWidgetIndexOffset(baseWidgetIndex, WIDX_RESEARCH_FUNDING);

switch (widgetIndex - widgetOffset)
Expand All @@ -514,9 +520,9 @@ void WindowResearchFundingMouseUp(WidgetIndex widgetIndex, WidgetIndex baseWidge
case WIDX_SHOPS_AND_STALLS:
case WIDX_SCENERY_AND_THEMING:
{
auto activeResearchTypes = gResearchPriorities;
auto activeResearchTypes = gameState.ResearchPriorities;
activeResearchTypes ^= 1uLL << (widgetIndex - (WIDX_TRANSPORT_RIDES + widgetOffset));
auto gameAction = ParkSetResearchFundingAction(activeResearchTypes, gResearchFundingLevel);
auto gameAction = ParkSetResearchFundingAction(activeResearchTypes, gameState.ResearchFundingLevel);
GameActions::Execute(&gameAction);
break;
}
Expand All @@ -525,20 +531,22 @@ void WindowResearchFundingMouseUp(WidgetIndex widgetIndex, WidgetIndex baseWidge

void WindowResearchFundingDropdown(WidgetIndex widgetIndex, int32_t selectedIndex, WidgetIndex baseWidgetIndex)
{
const auto& gameState = GetGameState();
auto widgetOffset = GetWidgetIndexOffset(baseWidgetIndex, WIDX_RESEARCH_FUNDING);

if (widgetIndex != (WIDX_RESEARCH_FUNDING_DROPDOWN_BUTTON + widgetOffset) || selectedIndex == -1)
return;

auto gameAction = ParkSetResearchFundingAction(gResearchPriorities, selectedIndex);
auto gameAction = ParkSetResearchFundingAction(gameState.ResearchPriorities, selectedIndex);
GameActions::Execute(&gameAction);
}

void WindowResearchFundingPrepareDraw(WindowBase* w, WidgetIndex baseWidgetIndex)
{
const auto& gameState = GetGameState();
auto widgetOffset = GetWidgetIndexOffset(baseWidgetIndex, WIDX_RESEARCH_FUNDING);

if ((GetGameState().ParkFlags & PARK_FLAGS_NO_MONEY) || gResearchProgressStage == RESEARCH_STAGE_FINISHED_ALL)
if ((GetGameState().ParkFlags & PARK_FLAGS_NO_MONEY) || gameState.ResearchProgressStage == RESEARCH_STAGE_FINISHED_ALL)
{
w->widgets[WIDX_RESEARCH_FUNDING + widgetOffset].type = WindowWidgetType::Empty;
w->widgets[WIDX_RESEARCH_FUNDING_DROPDOWN_BUTTON + widgetOffset].type = WindowWidgetType::Empty;
Expand All @@ -550,12 +558,12 @@ void WindowResearchFundingPrepareDraw(WindowBase* w, WidgetIndex baseWidgetIndex
}

// Current funding
int32_t currentResearchLevel = gResearchFundingLevel;
int32_t currentResearchLevel = gameState.ResearchFundingLevel;
w->widgets[WIDX_RESEARCH_FUNDING + widgetOffset].text = ResearchFundingLevelNames[currentResearchLevel];

// Checkboxes
uint8_t activeResearchTypes = gResearchPriorities;
int32_t uncompletedResearchTypes = gResearchUncompletedCategories;
uint8_t activeResearchTypes = gameState.ResearchPriorities;
int32_t uncompletedResearchTypes = gameState.ResearchUncompletedCategories;
for (int32_t i = 0; i < 7; i++)
{
int32_t mask = 1 << i;
Expand Down Expand Up @@ -585,7 +593,8 @@ void WindowResearchFundingDraw(WindowBase* w, DrawPixelInfo& dpi)
if (GetGameState().ParkFlags & PARK_FLAGS_NO_MONEY)
return;

int32_t currentResearchLevel = gResearchFundingLevel;
const auto& gameState = GetGameState();
int32_t currentResearchLevel = gameState.ResearchFundingLevel;
auto ft = Formatter();
ft.Add<money64>(research_cost_table[currentResearchLevel]);
DrawTextBasic(dpi, w->windowPos + ScreenCoordsXY{ 10, 77 }, STR_RESEARCH_COST_PER_MONTH, ft);
Expand Down
13 changes: 13 additions & 0 deletions src/openrct2/GameState.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,19 @@ namespace OpenRCT2
colour_t StaffHandymanColour;
colour_t StaffMechanicColour;
colour_t StaffSecurityColour;

uint8_t ResearchFundingLevel;
uint8_t ResearchPriorities;
uint16_t ResearchProgress;
uint8_t ResearchProgressStage;
uint8_t ResearchExpectedMonth;
uint8_t ResearchExpectedDay;
std::optional<ResearchItem> ResearchLastItem;
std::optional<ResearchItem> ResearchNextItem;

std::vector<ResearchItem> ResearchItemsUninvented;
std::vector<ResearchItem> ResearchItemsInvented;
uint8_t ResearchUncompletedCategories;
};

GameState_t& GetGameState();
Expand Down
8 changes: 6 additions & 2 deletions src/openrct2/actions/ParkSetResearchFundingAction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,16 @@
#include "ParkSetResearchFundingAction.h"

#include "../Context.h"
#include "../GameState.h"
#include "../core/MemoryStream.h"
#include "../localisation/StringIds.h"
#include "../management/Research.h"
#include "../ui/UiContext.h"
#include "../ui/WindowManager.h"
#include "../windows/Intent.h"

using namespace OpenRCT2;

ParkSetResearchFundingAction::ParkSetResearchFundingAction(uint32_t priorities, uint8_t fundingAmount)
: _priorities(priorities)
, _fundingAmount(fundingAmount)
Expand Down Expand Up @@ -52,8 +55,9 @@ GameActions::Result ParkSetResearchFundingAction::Query() const

GameActions::Result ParkSetResearchFundingAction::Execute() const
{
gResearchPriorities = _priorities;
gResearchFundingLevel = _fundingAmount;
auto& gameState = GetGameState();
gameState.ResearchPriorities = _priorities;
gameState.ResearchFundingLevel = _fundingAmount;

auto windowManager = OpenRCT2::GetContext()->GetUiContext()->GetWindowManager();
windowManager->BroadcastIntent(Intent(INTENT_ACTION_UPDATE_RESEARCH));
Expand Down
7 changes: 4 additions & 3 deletions src/openrct2/management/Finance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,13 @@ void FinancePayWages()
**/
void FinancePayResearch()
{
if (GetGameState().ParkFlags & PARK_FLAGS_NO_MONEY)
const auto& gameState = GetGameState();
if (gameState.ParkFlags & PARK_FLAGS_NO_MONEY)
{
return;
}

const uint8_t level = gResearchFundingLevel;
const uint8_t level = gameState.ResearchFundingLevel;
FinancePayment(research_cost_table[level] / 4, ExpenditureType::Research);
}

Expand Down Expand Up @@ -266,7 +267,7 @@ void FinanceUpdateDailyProfit()
}

// Research costs
uint8_t level = gResearchFundingLevel;
uint8_t level = gameState.ResearchFundingLevel;
current_profit -= research_cost_table[level];

// Loan costs
Expand Down
Loading

0 comments on commit 8208afe

Please sign in to comment.