Move gNewsItems to GameState

This commit is contained in:
Harry Hopkinson 2024-02-14 10:54:15 +00:00
parent 2d0a04ada9
commit 691f5f88e5
9 changed files with 54 additions and 41 deletions

View File

@ -11,6 +11,7 @@
#include <openrct2-ui/interface/Widget.h>
#include <openrct2-ui/windows/Window.h>
#include <openrct2/Context.h>
#include <openrct2/GameState.h>
#include <openrct2/audio/audio.h>
#include <openrct2/drawing/Drawing.h>
#include <openrct2/entity/EntityRegistry.h>
@ -92,13 +93,14 @@ public:
size_t j = _pressedNewsItemIndex;
_pressedNewsItemIndex = -1;
auto& gameState = OpenRCT2::GetGameState();
if (j >= gNewsItems.GetArchived().size())
if (j >= gameState.NewsItems.GetArchived().size())
{
return;
}
const auto& newsItem = gNewsItems.GetArchived()[j];
const auto& newsItem = gameState.NewsItems.GetArchived()[j];
if (newsItem.HasButton())
{
return;
@ -121,7 +123,8 @@ public:
ScreenSize OnScrollGetSize(int32_t scrollIndex) override
{
int32_t scrollHeight = static_cast<int32_t>(gNewsItems.GetArchived().size()) * CalculateItemHeight();
int32_t scrollHeight = static_cast<int32_t>(OpenRCT2::GetGameState().NewsItems.GetArchived().size())
* CalculateItemHeight();
return { WW, scrollHeight };
}
@ -131,7 +134,7 @@ public:
int32_t i = 0;
int32_t buttonIndex = 0;
auto mutableScreenCoords = screenCoords;
for (const auto& newsItem : gNewsItems.GetArchived())
for (const auto& newsItem : OpenRCT2::GetGameState().NewsItems.GetArchived())
{
if (mutableScreenCoords.y < itemHeight)
{
@ -178,7 +181,7 @@ public:
int32_t y = 0;
int32_t i = 0;
for (const auto& newsItem : gNewsItems.GetArchived())
for (const auto& newsItem : OpenRCT2::GetGameState().NewsItems.GetArchived())
{
if (y >= dpi.y + dpi.height)
break;

View File

@ -11,6 +11,7 @@
#include "Date.h"
#include "management/Finance.h"
#include "management/NewsItem.h"
#include "scenario/Scenario.h"
#include "world/Banner.h"
#include "world/Climate.h"
@ -73,6 +74,8 @@ namespace OpenRCT2
std::vector<Banner> Banners;
News::ItemQueues NewsItems;
colour_t StaffHandymanColour;
colour_t StaffMechanicColour;
colour_t StaffSecurityColour;

View File

@ -10,6 +10,7 @@
#include "NewsItem.h"
#include "../Context.h"
#include "../GameState.h"
#include "../Input.h"
#include "../OpenRCT2.h"
#include "../audio/audio.h"
@ -29,7 +30,7 @@
#include "../windows/Intent.h"
#include "../world/Location.hpp"
News::ItemQueues gNewsItems;
using namespace OpenRCT2;
News::Item& News::ItemQueues::Current()
{
@ -53,7 +54,7 @@ bool News::IsValidIndex(int32_t index)
News::Item* News::GetItem(int32_t index)
{
return gNewsItems.At(index);
return GetGameState().NewsItems.At(index);
}
News::Item& News::ItemQueues::operator[](size_t index)
@ -86,7 +87,7 @@ const News::Item* News::ItemQueues::At(int32_t index) const
bool News::IsQueueEmpty()
{
return gNewsItems.IsEmpty();
return GetGameState().NewsItems.IsEmpty();
}
bool News::ItemQueues::IsEmpty() const
@ -106,8 +107,9 @@ void News::ItemQueues::Clear()
void News::InitQueue()
{
gNewsItems.Clear();
assert(gNewsItems.IsEmpty());
auto& gameState = GetGameState();
gameState.NewsItems.Clear();
assert(gameState.NewsItems.IsEmpty());
// Throttles for warning types (PEEP_*_WARNING)
for (auto& warningThrottle : gPeepWarningThrottle)
@ -126,7 +128,7 @@ uint16_t News::ItemQueues::IncrementTicks()
static void TickCurrent()
{
int32_t ticks = gNewsItems.IncrementTicks();
int32_t ticks = GetGameState().NewsItems.IncrementTicks();
// Only play news item sound when in normal playing mode
if (ticks == 1 && (gScreenFlags == SCREEN_FLAGS_PLAYING))
{
@ -157,8 +159,9 @@ void News::UpdateCurrentItem()
{
PROFILED_FUNCTION();
auto& gameState = GetGameState();
// Check if there is a current news item
if (gNewsItems.IsEmpty())
if (gameState.NewsItems.IsEmpty())
return;
auto intent = Intent(INTENT_ACTION_INVALIDATE_TICKER_NEWS);
@ -168,8 +171,8 @@ void News::UpdateCurrentItem()
TickCurrent();
// Removal of current news item
if (gNewsItems.CurrentShouldBeArchived())
gNewsItems.ArchiveCurrent();
if (gameState.NewsItems.CurrentShouldBeArchived())
gameState.NewsItems.ArchiveCurrent();
}
/**
@ -178,7 +181,7 @@ void News::UpdateCurrentItem()
*/
void News::CloseCurrentItem()
{
gNewsItems.ArchiveCurrent();
GetGameState().NewsItems.ArchiveCurrent();
}
void News::ItemQueues::ArchiveCurrent()
@ -324,7 +327,7 @@ News::Item* News::AddItemToQueue(ItemType type, StringId string_id, EntityId ass
News::Item* News::AddItemToQueue(News::ItemType type, const utf8* text, uint32_t assoc)
{
auto& date = GetDate();
News::Item* newsItem = gNewsItems.FirstOpenOrNewSlot();
News::Item* newsItem = GetGameState().NewsItems.FirstOpenOrNewSlot();
newsItem->Type = type;
newsItem->Flags = 0;
newsItem->Assoc = assoc; // Make optional for Award, Money, Graph and Null
@ -434,12 +437,13 @@ void News::OpenSubject(News::ItemType type, int32_t subject)
*/
void News::DisableNewsItems(News::ItemType type, uint32_t assoc)
{
auto& gameState = GetGameState();
// TODO: write test invalidating windows
gNewsItems.ForeachRecentNews([type, assoc](auto& newsItem) {
gameState.NewsItems.ForeachRecentNews([type, assoc, gameState](auto& newsItem) {
if (type == newsItem.Type && assoc == newsItem.Assoc)
{
newsItem.SetFlags(News::ItemFlags::HasButton);
if (&newsItem == &gNewsItems.Current())
if (&newsItem == &gameState.NewsItems.Current())
{
auto intent = Intent(INTENT_ACTION_INVALIDATE_TICKER_NEWS);
ContextBroadcastIntent(&intent);
@ -447,7 +451,7 @@ void News::DisableNewsItems(News::ItemType type, uint32_t assoc)
}
});
gNewsItems.ForeachArchivedNews([type, assoc](auto& newsItem) {
gameState.NewsItems.ForeachArchivedNews([type, assoc](auto& newsItem) {
if (type == newsItem.Type && assoc == newsItem.Assoc)
{
newsItem.SetFlags(News::ItemFlags::HasButton);
@ -458,7 +462,7 @@ void News::DisableNewsItems(News::ItemType type, uint32_t assoc)
void News::AddItemToQueue(News::Item* newNewsItem)
{
News::Item* newsItem = gNewsItems.FirstOpenOrNewSlot();
News::Item* newsItem = GetGameState().NewsItems.FirstOpenOrNewSlot();
*newsItem = *newNewsItem;
}
@ -467,14 +471,15 @@ void News::RemoveItem(int32_t index)
if (index < 0 || index >= News::MaxItems)
return;
auto& gameState = GetGameState();
// News item is already null, no need to remove it
if (gNewsItems[index].Type == News::ItemType::Null)
if (gameState.NewsItems[index].Type == News::ItemType::Null)
return;
size_t newsBoundary = index < News::ItemHistoryStart ? News::ItemHistoryStart : News::MaxItems;
for (size_t i = index; i < newsBoundary - 1; i++)
{
gNewsItems[i] = gNewsItems[i + 1];
gameState.NewsItems[i] = gameState.NewsItems[i + 1];
}
gNewsItems[newsBoundary - 1].Type = News::ItemType::Null;
gameState.NewsItems[newsBoundary - 1].Type = News::ItemType::Null;
}

View File

@ -313,5 +313,3 @@ namespace News
void AddItemToQueue(News::Item* newNewsItem);
void RemoveItem(int32_t index);
} // namespace News
extern News::ItemQueues gNewsItems;

View File

@ -1000,16 +1000,16 @@ namespace OpenRCT2
void ReadWriteNotificationsChunk(GameState_t& gameState, OrcaStream& os)
{
os.ReadWriteChunk(ParkFileChunkType::NOTIFICATIONS, [](OrcaStream::ChunkStream& cs) {
os.ReadWriteChunk(ParkFileChunkType::NOTIFICATIONS, [&gameState](OrcaStream::ChunkStream& cs) {
if (cs.GetMode() == OrcaStream::Mode::READING)
{
gNewsItems.Clear();
gameState.NewsItems.Clear();
std::vector<News::Item> recent;
cs.ReadWriteVector(recent, [&cs](News::Item& item) { ReadWriteNewsItem(cs, item); });
for (size_t i = 0; i < std::min<size_t>(recent.size(), News::ItemHistoryStart); i++)
{
gNewsItems[i] = recent[i];
gameState.NewsItems[i] = recent[i];
}
std::vector<News::Item> archived;
@ -1017,19 +1017,21 @@ namespace OpenRCT2
size_t offset = News::ItemHistoryStart;
for (size_t i = 0; i < std::min<size_t>(archived.size(), News::MaxItemsArchive); i++)
{
gNewsItems[offset + i] = archived[i];
gameState.NewsItems[offset + i] = archived[i];
}
// Still need to set the correct type to properly terminate the queue
if (archived.size() < News::MaxItemsArchive)
gNewsItems[offset + archived.size()].Type = News::ItemType::Null;
gameState.NewsItems[offset + archived.size()].Type = News::ItemType::Null;
}
else
{
std::vector<News::Item> recent(std::begin(gNewsItems.GetRecent()), std::end(gNewsItems.GetRecent()));
std::vector<News::Item> recent(
std::begin(gameState.NewsItems.GetRecent()), std::end(gameState.NewsItems.GetRecent()));
cs.ReadWriteVector(recent, [&cs](News::Item& item) { ReadWriteNewsItem(cs, item); });
std::vector<News::Item> archived(std::begin(gNewsItems.GetArchived()), std::end(gNewsItems.GetArchived()));
std::vector<News::Item> archived(
std::begin(gameState.NewsItems.GetArchived()), std::end(gameState.NewsItems.GetArchived()));
cs.ReadWriteVector(archived, [&cs](News::Item& item) { ReadWriteNewsItem(cs, item); });
}
});

View File

@ -2165,7 +2165,7 @@ namespace RCT1
for (size_t i = 0; i < Limits::MaxNewsItems; i++)
{
const RCT12NewsItem* src = &_s4.Messages[i];
News::Item* dst = &gNewsItems[i];
News::Item* dst = &gameState.NewsItems[i];
dst->Type = static_cast<News::ItemType>(src->Type);
dst->Flags = src->Flags;

View File

@ -474,7 +474,7 @@ namespace RCT2
for (size_t i = 0; i < Limits::MaxNewsItems; i++)
{
const RCT12NewsItem* src = &_s6.NewsItems[i];
News::Item* dst = &gNewsItems[i];
News::Item* dst = &gameState.NewsItems[i];
if (src->Type < News::ItemTypeCount)
{
dst->Type = static_cast<News::ItemType>(src->Type);

View File

@ -315,11 +315,12 @@ namespace OpenRCT2::Scripting
std::vector<std::shared_ptr<ScParkMessage>> ScPark::messages_get() const
{
std::vector<std::shared_ptr<ScParkMessage>> result;
for (size_t i = 0, newsSize = gNewsItems.GetRecent().size(); i < newsSize; i++)
auto& gameState = GetGameState();
for (size_t i = 0, newsSize = gameState.NewsItems.GetRecent().size(); i < newsSize; i++)
{
result.push_back(std::make_shared<ScParkMessage>(i));
}
for (size_t i = 0, newsSize = gNewsItems.GetArchived().size(); i < newsSize; i++)
for (size_t i = 0, newsSize = gameState.NewsItems.GetArchived().size(); i < newsSize; i++)
{
result.push_back(std::make_shared<ScParkMessage>(i + News::ItemHistoryStart));
}
@ -330,6 +331,7 @@ namespace OpenRCT2::Scripting
{
int32_t index = 0;
int32_t archiveIndex = News::ItemHistoryStart;
auto& gameState = GetGameState();
for (const auto& item : value)
{
auto isArchived = item["isArchived"].as_bool();
@ -338,7 +340,7 @@ namespace OpenRCT2::Scripting
{
if (archiveIndex < News::MaxItems)
{
gNewsItems[archiveIndex] = newsItem;
gameState.NewsItems[archiveIndex] = newsItem;
archiveIndex++;
}
}
@ -346,7 +348,7 @@ namespace OpenRCT2::Scripting
{
if (index < News::ItemHistoryStart)
{
gNewsItems[index] = newsItem;
gameState.NewsItems[index] = newsItem;
index++;
}
}
@ -355,11 +357,11 @@ namespace OpenRCT2::Scripting
// End the lists by setting next item to null
if (index < News::ItemHistoryStart)
{
gNewsItems[index].Type = News::ItemType::Null;
gameState.NewsItems[index].Type = News::ItemType::Null;
}
if (archiveIndex < News::MaxItems)
{
gNewsItems[archiveIndex].Type = News::ItemType::Null;
gameState.NewsItems[archiveIndex].Type = News::ItemType::Null;
}
}

View File

@ -46,7 +46,7 @@ namespace OpenRCT2::Scripting
News::Item* ScParkMessage::GetMessage() const
{
return &gNewsItems[_index];
return &GetGameState().NewsItems[_index];
}
bool ScParkMessage::isArchived_get() const