1
0
Fork 0

Compare commits

...

9 Commits

Author SHA1 Message Date
Peter Nelson 0e4de94a2e
Merge 05286e9f2e into 9ce2aca949 2025-07-14 19:28:04 +01:00
Peter Nelson 05286e9f2e
Add: Industry accepted and waiting history graphs.
Records amount of cargo accepted, and a rolling average of the waiting amount.

Average waiting samples the waiting amount once per day for each industry, spread out over an economy day.
2025-07-14 19:27:50 +01:00
Peter Nelson f5ad989188
Codechange: Allow unused graph ranges to be masked. 2025-07-14 19:24:01 +01:00
Peter Nelson 9ce2aca949 Codechange: Get/pass ScriptStorage by reference instead of pointer. 2025-07-14 19:19:29 +01:00
Peter Nelson 55098a2f2e Codechange: Get/pass engine by reference instead of pointer. 2025-07-14 19:19:29 +01:00
Peter Nelson 7ff0c67f77 Codechange: Get/pass script controller by reference instead of pointer. 2025-07-14 19:19:29 +01:00
Peter Nelson b2de1ff66f
Fix #14433: Broken road stop drawing due to incorrect modes conversion. (#14434)
The mask was treated as a single RoadStopDrawMode instead of a RoadStopDrawModes bitset.
2025-07-14 17:25:53 +01:00
Loïc Guilloux fc924161ab
Fix 0455627d: Don't draw timetable panel if no orders (#14441) 2025-07-14 14:18:54 +00:00
Peter Nelson 61a299bc99
Codechange: Use SpriteID as GlyphID for SpriteFontCache. (#14439)
This reduces the amount of lookups in the character map as rendering a cached layout no longer needs to do so.
2025-07-14 13:28:10 +01:00
24 changed files with 259 additions and 94 deletions

View File

@ -1067,6 +1067,7 @@ static uint DeliverGoodsToIndustry(const Station *st, CargoType cargo_type, uint
uint amount = std::min(num_pieces, 0xFFFFu - it->waiting);
it->waiting += amount;
it->GetOrCreateHistory()[THIS_MONTH].accepted += amount;
it->last_accepted = TimerGameEconomy::date;
num_pieces -= amount;
accepted += amount;

View File

@ -103,14 +103,14 @@ void SpriteFontCache::ClearFontCache()
const Sprite *SpriteFontCache::GetGlyph(GlyphID key)
{
SpriteID sprite = this->GetUnicodeGlyph(static_cast<char32_t>(key & ~SPRITE_GLYPH));
SpriteID sprite = static_cast<SpriteID>(key & ~SPRITE_GLYPH);
if (sprite == 0) sprite = this->GetUnicodeGlyph('?');
return GetSprite(sprite, SpriteType::Font);
}
uint SpriteFontCache::GetGlyphWidth(GlyphID key)
{
SpriteID sprite = this->GetUnicodeGlyph(static_cast<char32_t>(key & ~SPRITE_GLYPH));
SpriteID sprite = static_cast<SpriteID>(key & ~SPRITE_GLYPH);
if (sprite == 0) sprite = this->GetUnicodeGlyph('?');
return SpriteExists(sprite) ? GetSprite(sprite, SpriteType::Font)->width + ScaleFontTrad(this->fs != FS_NORMAL ? 1 : 0) : 0;
}
@ -120,7 +120,7 @@ GlyphID SpriteFontCache::MapCharToGlyph(char32_t key, [[maybe_unused]] bool allo
assert(IsPrintable(key));
SpriteID sprite = this->GetUnicodeGlyph(key);
if (sprite == 0) return 0;
return SPRITE_GLYPH | key;
return SPRITE_GLYPH | sprite;
}
bool SpriteFontCache::GetDrawGlyphShadow()

View File

@ -182,8 +182,9 @@ protected:
static const int MIN_GRAPH_NUM_LINES_Y = 9; ///< Minimal number of horizontal lines to draw.
static const int MIN_GRID_PIXEL_SIZE = 20; ///< Minimum distance between graph lines.
uint64_t excluded_data = 0; ///< bitmask of the datasets that shouldn't be displayed.
uint64_t excluded_range = 0; ///< bitmask of ranges that should not be displayed.
uint64_t excluded_data = 0; ///< bitmask of datasets hidden by the player.
uint64_t excluded_range = 0; ///< bitmask of ranges hidden by the player.
uint64_t masked_range = 0; ///< bitmask of ranges that are not available for the current data.
uint8_t num_on_x_axis = 0;
uint8_t num_vert_lines = GRAPH_NUM_MONTHS;
@ -216,13 +217,20 @@ protected:
uint8_t highlight_range = UINT8_MAX; ///< Data range that should be highlighted, or UINT8_MAX for none.
bool highlight_state = false; ///< Current state of highlight, toggled every TIMER_BLINK_INTERVAL period.
template <typename Tprojection>
struct Filler {
struct BaseFiller {
DataSet &dataset; ///< Dataset to fill.
inline void MakeZero(uint i) const { this->dataset.values[i] = 0; }
inline void MakeInvalid(uint i) const { this->dataset.values[i] = INVALID_DATAPOINT; }
};
template <typename Tprojection>
struct Filler : BaseFiller {
const Tprojection &proj; ///< Projection to apply.
constexpr Filler(DataSet &dataset, const Tprojection &proj) : BaseFiller(dataset), proj(proj) {}
inline void Fill(uint i, const auto &data) const { this->dataset.values[i] = std::invoke(this->proj, data); }
inline void MakeInvalid(uint i) const { this->dataset.values[i] = INVALID_DATAPOINT; }
};
/**
@ -675,13 +683,17 @@ public:
uint index = 0;
Rect line = r.WithHeight(line_height);
for (const auto &str : this->ranges) {
bool lowered = !HasBit(this->excluded_range, index);
bool lowered = !HasBit(this->excluded_range, index) && !HasBit(this->masked_range, index);
/* Redraw frame if lowered */
if (lowered) DrawFrameRect(line, COLOUR_BROWN, FrameFlag::Lowered);
const Rect text = line.Shrink(WidgetDimensions::scaled.framerect);
DrawString(text, str, TC_BLACK, SA_CENTER, false, FS_SMALL);
DrawString(text, str, (this->highlight_state && this->highlight_range == index) ? TC_WHITE : TC_BLACK, SA_CENTER, false, FS_SMALL);
if (HasBit(this->masked_range, index)) {
GfxFillRect(line.Shrink(WidgetDimensions::scaled.bevel), GetColourGradient(COLOUR_BROWN, SHADE_DARKER), FILLRECT_CHECKER);
}
line = line.Translate(0, line_height);
++index;
@ -704,6 +716,7 @@ public:
case WID_GRAPH_RANGE_MATRIX: {
int row = GetRowFromWidget(pt.y, widget, 0, GetCharacterHeight(FS_SMALL) + WidgetDimensions::scaled.framerect.Vertical());
if (HasBit(this->masked_range, row)) break;
ToggleBit(this->excluded_range, row);
this->SetDirty();
break;
@ -1115,6 +1128,7 @@ struct BaseCargoGraphWindow : BaseGraphWindow {
{
this->CreateNestedTree();
this->excluded_range = this->masked_range;
this->cargo_types = this->GetCargoTypes(number);
this->vscroll = this->GetScrollbar(WID_GRAPH_MATRIX_SCROLLBAR);
@ -1608,7 +1622,9 @@ CompanyID PerformanceRatingDetailWindow::company = CompanyID::Invalid();
struct IndustryProductionGraphWindow : BaseCargoGraphWindow {
static inline constexpr StringID RANGE_LABELS[] = {
STR_GRAPH_INDUSTRY_RANGE_PRODUCED,
STR_GRAPH_INDUSTRY_RANGE_TRANSPORTED
STR_GRAPH_INDUSTRY_RANGE_TRANSPORTED,
STR_GRAPH_INDUSTRY_RANGE_DELIVERED,
STR_GRAPH_INDUSTRY_RANGE_WAITING,
};
static inline CargoTypes excluded_cargo_types{};
@ -1623,6 +1639,10 @@ struct IndustryProductionGraphWindow : BaseCargoGraphWindow {
this->draw_dates = !TimerGameEconomy::UsingWallclockUnits();
this->ranges = RANGE_LABELS;
const Industry *i = Industry::Get(window_number);
if (!i->IsCargoProduced()) this->masked_range = (1U << 0) | (1U << 1);
if (!i->IsCargoAccepted()) this->masked_range = (1U << 2) | (1U << 3);
this->InitializeWindow(window_number, STR_GRAPH_LAST_24_MINUTES_TIME_LABEL);
}
@ -1630,6 +1650,9 @@ struct IndustryProductionGraphWindow : BaseCargoGraphWindow {
{
CargoTypes cargo_types{};
const Industry *i = Industry::Get(window_number);
for (const auto &a : i->accepted) {
if (IsValidCargoType(a.cargo)) SetBit(cargo_types, a.cargo);
}
for (const auto &p : i->produced) {
if (IsValidCargoType(p.cargo)) SetBit(cargo_types, p.cargo);
}
@ -1643,7 +1666,7 @@ struct IndustryProductionGraphWindow : BaseCargoGraphWindow {
std::string GetWidgetString(WidgetID widget, StringID stringid) const override
{
if (widget == WID_GRAPH_CAPTION) return GetString(STR_GRAPH_INDUSTRY_PRODUCTION_CAPTION, this->window_number);
if (widget == WID_GRAPH_CAPTION) return GetString(STR_GRAPH_INDUSTRY_CAPTION, this->window_number);
return this->Window::GetWidgetString(widget, stringid);
}
@ -1691,6 +1714,33 @@ struct IndustryProductionGraphWindow : BaseCargoGraphWindow {
FillFromHistory<GRAPH_NUM_MONTHS>(p.history, i->valid_history, produced_filler, transported_filler);
}
for (const auto &a : i->accepted) {
if (!IsValidCargoType(a.cargo)) continue;
const CargoSpec *cs = CargoSpec::Get(a.cargo);
this->data.reserve(this->data.size() + 2);
DataSet &accepted = this->data.emplace_back();
accepted.colour = cs->legend_colour;
accepted.exclude_bit = cs->Index();
accepted.range_bit = 2;
accepted.dash = 1;
auto accepted_filler = Filler{accepted, &Industry::AcceptedHistory::accepted};
DataSet &waiting = this->data.emplace_back();
waiting.colour = cs->legend_colour;
waiting.exclude_bit = cs->Index();
waiting.range_bit = 3;
waiting.dash = 4;
auto waiting_filler = Filler{waiting, &Industry::AcceptedHistory::waiting};
if (a.history == nullptr) {
FillFromEmpty<GRAPH_NUM_MONTHS>(i->valid_history, accepted_filler, waiting_filler);
} else {
FillFromHistory<GRAPH_NUM_MONTHS>(*a.history, i->valid_history, accepted_filler, waiting_filler);
}
}
this->SetDirty();
}
};

View File

@ -77,10 +77,27 @@ struct Industry : IndustryPool::PoolItem<&_industry_pool> {
HistoryData<ProducedHistory> history{}; ///< History of cargo produced and transported for this month and 24 previous months
};
struct AcceptedHistory {
uint16_t accepted = 0; /// Total accepted.
uint16_t waiting = 0; /// Average waiting.
};
struct AcceptedCargo {
CargoType cargo = 0; ///< Cargo type
uint16_t waiting = 0; ///< Amount of cargo waiting to processed
uint32_t accumulated_waiting = 0; ///< Accumulated waiting total over the last month, used to calculate average.
TimerGameEconomy::Date last_accepted{}; ///< Last day cargo was accepted by this industry
std::unique_ptr<HistoryData<AcceptedHistory>> history{}; ///< History of accepted and waiting cargo.
/**
* Get history data, creating it if necessary.
* @return Accepted history data.
*/
inline HistoryData<AcceptedHistory> &GetOrCreateHistory()
{
if (this->history == nullptr) this->history = std::make_unique<HistoryData<AcceptedHistory>>();
return *this->history;
}
};
using ProducedCargoes = std::vector<ProducedCargo>;
@ -151,7 +168,7 @@ struct Industry : IndustryPool::PoolItem<&_industry_pool> {
*/
inline const AcceptedCargo &GetAccepted(size_t slot) const
{
static const AcceptedCargo empty{INVALID_CARGO, 0, {}};
static const AcceptedCargo empty{INVALID_CARGO, 0, 0, {}, {}};
return slot < this->accepted.size() ? this->accepted[slot] : empty;
}

View File

@ -1245,6 +1245,10 @@ void OnTick_Industry()
for (Industry *i : Industry::Iterate()) {
ProduceIndustryGoods(i);
if ((TimerGameTick::counter + i->index) % Ticks::DAY_TICKS == 0) {
for (auto &a : i->accepted) a.accumulated_waiting += a.waiting;
}
}
}
@ -2505,6 +2509,14 @@ static void UpdateIndustryStatistics(Industry *i)
RotateHistory(p.history);
}
}
for (auto &a : i->accepted) {
if (!IsValidCargoType(a.cargo)) continue;
if (a.history == nullptr) continue;
(*a.history)[THIS_MONTH].waiting = GetAndResetAccumulatedAverage<uint16_t>(a.accumulated_waiting);
RotateHistory(*a.history);
}
}
/**

View File

@ -845,7 +845,7 @@ public:
nvp->InitializeViewport(this, Industry::Get(window_number)->location.GetCenterTile(), ScaleZoomGUI(ZoomLevel::Industry));
const Industry *i = Industry::Get(window_number);
if (!i->IsCargoProduced()) this->DisableWidget(WID_IV_GRAPH);
if (!i->IsCargoProduced() && !i->IsCargoAccepted()) this->DisableWidget(WID_IV_GRAPH);
this->InvalidateData();
}
@ -1242,7 +1242,7 @@ static constexpr NWidgetPart _nested_industry_view_widgets[] = {
EndContainer(),
NWidget(NWID_HORIZONTAL),
NWidget(WWT_PUSHTXTBTN, COLOUR_CREAM, WID_IV_DISPLAY), SetFill(1, 0), SetResize(1, 0), SetStringTip(STR_INDUSTRY_DISPLAY_CHAIN, STR_INDUSTRY_DISPLAY_CHAIN_TOOLTIP),
NWidget(WWT_PUSHTXTBTN, COLOUR_CREAM, WID_IV_GRAPH), SetFill(1, 0), SetResize(1, 0), SetStringTip(STR_INDUSTRY_VIEW_PRODUCTION_GRAPH, STR_INDUSTRY_VIEW_PRODUCTION_GRAPH_TOOLTIP),
NWidget(WWT_PUSHTXTBTN, COLOUR_CREAM, WID_IV_GRAPH), SetFill(1, 0), SetResize(1, 0), SetStringTip(STR_INDUSTRY_VIEW_CARGO_GRAPH, STR_INDUSTRY_VIEW_CARGO_GRAPH_TOOLTIP),
NWidget(WWT_RESIZEBOX, COLOUR_CREAM),
EndContainer(),
};

View File

@ -634,9 +634,11 @@ STR_GRAPH_CARGO_TOOLTIP_DISABLE_ALL :{BLACK}Display
STR_GRAPH_CARGO_PAYMENT_TOGGLE_CARGO :{BLACK}Toggle graph of this cargo type
STR_GRAPH_CARGO_PAYMENT_CARGO :{TINY_FONT}{BLACK}{STRING}
STR_GRAPH_INDUSTRY_PRODUCTION_CAPTION :{WHITE}{INDUSTRY} - Production History
STR_GRAPH_INDUSTRY_CAPTION :{WHITE}{INDUSTRY} - Cargo History
STR_GRAPH_INDUSTRY_RANGE_PRODUCED :Produced
STR_GRAPH_INDUSTRY_RANGE_TRANSPORTED :Transported
STR_GRAPH_INDUSTRY_RANGE_DELIVERED :Delivered
STR_GRAPH_INDUSTRY_RANGE_WAITING :Waiting
STR_GRAPH_PERFORMANCE_DETAIL_TOOLTIP :{BLACK}Show detailed performance ratings
@ -4024,8 +4026,8 @@ STR_INDUSTRY_VIEW_PRODUCTION_LAST_MONTH_TITLE :{BLACK}Producti
STR_INDUSTRY_VIEW_PRODUCTION_LAST_MINUTE_TITLE :{BLACK}Production last minute:
STR_INDUSTRY_VIEW_TRANSPORTED :{YELLOW}{CARGO_LONG}{RAW_STRING}{BLACK} ({COMMA}% transported)
STR_INDUSTRY_VIEW_LOCATION_TOOLTIP :{BLACK}Centre the main view on industry location. Ctrl+Click to open a new viewport on industry location
STR_INDUSTRY_VIEW_PRODUCTION_GRAPH :{BLACK}Production Graph
STR_INDUSTRY_VIEW_PRODUCTION_GRAPH_TOOLTIP :{BLACK}Shows the graph of industry production history
STR_INDUSTRY_VIEW_CARGO_GRAPH :{BLACK}Cargo Graph
STR_INDUSTRY_VIEW_CARGO_GRAPH_TOOLTIP :{BLACK}Shows the graph of industry cargo history
STR_INDUSTRY_VIEW_PRODUCTION_LEVEL :{BLACK}Production level: {YELLOW}{COMMA}%
STR_INDUSTRY_VIEW_INDUSTRY_ANNOUNCED_CLOSURE :{YELLOW}The industry has announced imminent closure!

View File

@ -11,6 +11,8 @@
#define HISTORY_FUNC_HPP
#include "../core/bitmath_func.hpp"
#include "../core/math_func.hpp"
#include "../timer/timer_game_economy.h"
#include "history_type.hpp"
/**
@ -34,6 +36,19 @@ void RotateHistory(HistoryData<T> &history)
history[THIS_MONTH] = {};
}
/**
* Get an average value for the previous month, as reset for the next month.
* @param total Accrued total to average. Will be reset to zero.
* @return Average value for the month.
*/
template <typename T, typename Taccrued>
T GetAndResetAccumulatedAverage(Taccrued &total)
{
T result = ClampTo<T>(total / std::max(1U, TimerGameEconomy::days_since_last_month));
total = 0;
return result;
}
/**
* Fill some data with historical data.
* @param history Historical data to fill from.
@ -53,4 +68,21 @@ void FillFromHistory(const HistoryData<T> &history, ValidHistoryMask valid_histo
}
}
/**
* Fill some data with empty records.
* @param valid_history Mask of valid history records.
* @param fillers Fillers to fill with history data.
*/
template <uint N, typename... Tfillers>
void FillFromEmpty(ValidHistoryMask valid_history, Tfillers... fillers)
{
for (uint i = 0; i != N; ++i) {
if (HasBit(valid_history, N - i)) {
(fillers.MakeZero(i), ...);
} else {
(fillers.MakeInvalid(i), ...);
}
}
}
#endif /* HISTORY_FUNC_HPP */

View File

@ -105,8 +105,8 @@ static ChangeInfoResult RoadStopChangeInfo(uint first, uint last, int prop, Byte
AddStringForMapping(GRFStringID{buf.ReadWord()}, [rs = rs.get()](StringID str) { RoadStopClass::Get(rs->class_index)->name = str; });
break;
case 0x0C: // The draw mode
rs->draw_mode = static_cast<RoadStopDrawMode>(buf.ReadByte());
case 0x0C: // The draw modes
rs->draw_mode = static_cast<RoadStopDrawModes>(buf.ReadByte());
break;
case 0x0D: // Cargo types for random triggers

View File

@ -298,7 +298,7 @@ void DrawRoadStopTile(int x, int y, RoadType roadtype, const RoadStopSpec *spec,
RoadStopDrawModes draw_mode;
if (spec->flags.Test(RoadStopSpecFlag::DrawModeRegister)) {
draw_mode = static_cast<RoadStopDrawMode>(object.GetRegister(0x100));
draw_mode = static_cast<RoadStopDrawModes>(object.GetRegister(0x100));
} else {
draw_mode = spec->draw_mode;
}

View File

@ -19,12 +19,50 @@
static OldPersistentStorage _old_ind_persistent_storage;
class SlIndustryAcceptedHistory : public DefaultSaveLoadHandler<SlIndustryAcceptedHistory, Industry::AcceptedCargo> {
public:
static inline const SaveLoad description[] = {
SLE_VAR(Industry::AcceptedHistory, accepted, SLE_UINT16),
SLE_VAR(Industry::AcceptedHistory, waiting, SLE_UINT16),
};
static inline const SaveLoadCompatTable compat_description = _industry_produced_history_sl_compat;
void Save(Industry::AcceptedCargo *a) const override
{
if (!IsValidCargoType(a->cargo) || a->history == nullptr) {
/* Don't save any history if cargo slot isn't used. */
SlSetStructListLength(0);
return;
}
SlSetStructListLength(a->history->size());
for (auto &h : *a->history) {
SlObject(&h, this->GetDescription());
}
}
void Load(Industry::AcceptedCargo *a) const override
{
size_t len = SlGetStructListLength(UINT32_MAX);
if (len == 0) return;
auto &history = a->GetOrCreateHistory();
for (auto &h : history) {
if (--len > history.size()) break; // unsigned so wraps after hitting zero.
SlObject(&h, this->GetDescription());
}
}
};
class SlIndustryAccepted : public VectorSaveLoadHandler<SlIndustryAccepted, Industry, Industry::AcceptedCargo, INDUSTRY_NUM_INPUTS> {
public:
static inline const SaveLoad description[] = {
SLE_VAR(Industry::AcceptedCargo, cargo, SLE_UINT8),
SLE_VAR(Industry::AcceptedCargo, waiting, SLE_UINT16),
SLE_VAR(Industry::AcceptedCargo, last_accepted, SLE_INT32),
SLE_CONDVAR(Industry::AcceptedCargo, accumulated_waiting, SLE_UINT32, SLV_INDUSTRY_ACCEPTED_HISTORY, SL_MAX_VERSION),
SLEG_CONDSTRUCTLIST("history", SlIndustryAcceptedHistory, SLV_INDUSTRY_ACCEPTED_HISTORY, SL_MAX_VERSION),
};
static inline const SaveLoadCompatTable compat_description = _industry_accepts_sl_compat;

View File

@ -858,7 +858,7 @@ static bool LoadOldIndustry(LoadgameState &ls, int num)
if (i->location.tile != 0) {
/* Copy data from old fixed arrays to industry. */
std::copy(std::begin(_old_accepted), std::end(_old_accepted), std::back_inserter(i->accepted));
std::move(std::begin(_old_accepted), std::end(_old_accepted), std::back_inserter(i->accepted));
std::copy(std::begin(_old_produced), std::end(_old_produced), std::back_inserter(i->produced));
i->town = RemapTown(i->location.tile);

View File

@ -405,6 +405,7 @@ enum SaveLoadVersion : uint16_t {
SLV_FACE_STYLES, ///< 355 PR#14319 Addition of face styles, replacing gender and ethnicity.
SLV_INDUSTRY_NUM_VALID_HISTORY, ///< 356 PR#14416 Store number of valid history records for industries.
SLV_INDUSTRY_ACCEPTED_HISTORY, ///< 357 PR#14321 Add per-industry history of cargo delivered and waiting.
SL_MAX_VERSION, ///< Highest possible saveload version
};

View File

@ -76,7 +76,7 @@ ScriptController::ScriptController(::CompanyID company) :
/* static */ uint ScriptController::GetTick()
{
return ScriptObject::GetActiveInstance().GetController()->ticks;
return ScriptObject::GetActiveInstance().GetController().ticks;
}
/* static */ int ScriptController::GetOpsTillSuspend()
@ -96,9 +96,9 @@ ScriptController::ScriptController(::CompanyID company) :
/* static */ HSQOBJECT ScriptController::Import(const std::string &library, const std::string &class_name, int version)
{
ScriptController *controller = ScriptObject::GetActiveInstance().GetController();
Squirrel *engine = ScriptObject::GetActiveInstance().engine;
HSQUIRRELVM vm = engine->GetVM();
ScriptController &controller = ScriptObject::GetActiveInstance().GetController();
Squirrel &engine = *ScriptObject::GetActiveInstance().engine;
HSQUIRRELVM vm = engine.GetVM();
ScriptInfo *lib = ScriptObject::GetActiveInstance().FindLibrary(library, version);
if (lib == nullptr) {
@ -114,11 +114,11 @@ ScriptController::ScriptController(::CompanyID company) :
std::string fake_class;
LoadedLibraryList::iterator it = controller->loaded_library.find(library_name);
if (it != controller->loaded_library.end()) {
LoadedLibraryList::iterator it = controller.loaded_library.find(library_name);
if (it != controller.loaded_library.end()) {
fake_class = (*it).second;
} else {
int next_number = ++controller->loaded_library_count;
int next_number = ++controller.loaded_library_count;
/* Create a new fake internal name */
fake_class = fmt::format("_internalNA{}", next_number);
@ -128,14 +128,14 @@ ScriptController::ScriptController(::CompanyID company) :
sq_pushstring(vm, fake_class);
sq_newclass(vm, SQFalse);
/* Load the library */
if (!engine->LoadScript(vm, lib->GetMainScript(), false)) {
if (!engine.LoadScript(vm, lib->GetMainScript(), false)) {
throw sq_throwerror(vm, fmt::format("there was a compile error when importing '{}' version {}", library, version));
}
/* Create the fake class */
sq_newslot(vm, -3, SQFalse);
sq_pop(vm, 1);
controller->loaded_library[library_name] = fake_class;
controller.loaded_library[library_name] = fake_class;
}
/* Find the real class inside the fake class (like 'sets.Vector') */

View File

@ -45,7 +45,7 @@ void SimpleCountedObject::Release()
* Get the storage associated with the current ScriptInstance.
* @return The storage.
*/
static ScriptStorage *GetStorage()
static ScriptStorage &GetStorage()
{
return ScriptObject::GetActiveInstance().GetStorage();
}
@ -65,7 +65,7 @@ ScriptObject::ActiveInstance::~ActiveInstance()
}
ScriptObject::DisableDoCommandScope::DisableDoCommandScope()
: AutoRestoreBackup(GetStorage()->allow_do_command, false)
: AutoRestoreBackup(GetStorage().allow_do_command, false)
{}
/* static */ ScriptInstance &ScriptObject::GetActiveInstance()
@ -78,181 +78,181 @@ ScriptObject::DisableDoCommandScope::DisableDoCommandScope()
/* static */ void ScriptObject::SetDoCommandDelay(uint ticks)
{
assert(ticks > 0);
GetStorage()->delay = ticks;
GetStorage().delay = ticks;
}
/* static */ uint ScriptObject::GetDoCommandDelay()
{
return GetStorage()->delay;
return GetStorage().delay;
}
/* static */ void ScriptObject::SetDoCommandMode(ScriptModeProc *proc, ScriptObject *instance)
{
GetStorage()->mode = proc;
GetStorage()->mode_instance = instance;
GetStorage().mode = proc;
GetStorage().mode_instance = instance;
}
/* static */ ScriptModeProc *ScriptObject::GetDoCommandMode()
{
return GetStorage()->mode;
return GetStorage().mode;
}
/* static */ ScriptObject *ScriptObject::GetDoCommandModeInstance()
{
return GetStorage()->mode_instance;
return GetStorage().mode_instance;
}
/* static */ void ScriptObject::SetDoCommandAsyncMode(ScriptAsyncModeProc *proc, ScriptObject *instance)
{
GetStorage()->async_mode = proc;
GetStorage()->async_mode_instance = instance;
GetStorage().async_mode = proc;
GetStorage().async_mode_instance = instance;
}
/* static */ ScriptAsyncModeProc *ScriptObject::GetDoCommandAsyncMode()
{
return GetStorage()->async_mode;
return GetStorage().async_mode;
}
/* static */ ScriptObject *ScriptObject::GetDoCommandAsyncModeInstance()
{
return GetStorage()->async_mode_instance;
return GetStorage().async_mode_instance;
}
/* static */ void ScriptObject::SetLastCommand(const CommandDataBuffer &data, Commands cmd)
{
ScriptStorage *s = GetStorage();
Debug(script, 6, "SetLastCommand company={:02d} cmd={} data={}", s->root_company, cmd, FormatArrayAsHex(data));
s->last_data = data;
s->last_cmd = cmd;
ScriptStorage &s = GetStorage();
Debug(script, 6, "SetLastCommand company={:02d} cmd={} data={}", s.root_company, cmd, FormatArrayAsHex(data));
s.last_data = data;
s.last_cmd = cmd;
}
/* static */ bool ScriptObject::CheckLastCommand(const CommandDataBuffer &data, Commands cmd)
{
ScriptStorage *s = GetStorage();
Debug(script, 6, "CheckLastCommand company={:02d} cmd={} data={}", s->root_company, cmd, FormatArrayAsHex(data));
if (s->last_cmd != cmd) return false;
if (s->last_data != data) return false;
ScriptStorage &s = GetStorage();
Debug(script, 6, "CheckLastCommand company={:02d} cmd={} data={}", s.root_company, cmd, FormatArrayAsHex(data));
if (s.last_cmd != cmd) return false;
if (s.last_data != data) return false;
return true;
}
/* static */ void ScriptObject::SetDoCommandCosts(Money value)
{
GetStorage()->costs = CommandCost(INVALID_EXPENSES, value); // Expense type is never read.
GetStorage().costs = CommandCost(INVALID_EXPENSES, value); // Expense type is never read.
}
/* static */ void ScriptObject::IncreaseDoCommandCosts(Money value)
{
GetStorage()->costs.AddCost(value);
GetStorage().costs.AddCost(value);
}
/* static */ Money ScriptObject::GetDoCommandCosts()
{
return GetStorage()->costs.GetCost();
return GetStorage().costs.GetCost();
}
/* static */ void ScriptObject::SetLastError(ScriptErrorType last_error)
{
GetStorage()->last_error = last_error;
GetStorage().last_error = last_error;
}
/* static */ ScriptErrorType ScriptObject::GetLastError()
{
return GetStorage()->last_error;
return GetStorage().last_error;
}
/* static */ void ScriptObject::SetLastCost(Money last_cost)
{
GetStorage()->last_cost = last_cost;
GetStorage().last_cost = last_cost;
}
/* static */ Money ScriptObject::GetLastCost()
{
return GetStorage()->last_cost;
return GetStorage().last_cost;
}
/* static */ void ScriptObject::SetRoadType(RoadType road_type)
{
GetStorage()->road_type = road_type;
GetStorage().road_type = road_type;
}
/* static */ RoadType ScriptObject::GetRoadType()
{
return GetStorage()->road_type;
return GetStorage().road_type;
}
/* static */ void ScriptObject::SetRailType(RailType rail_type)
{
GetStorage()->rail_type = rail_type;
GetStorage().rail_type = rail_type;
}
/* static */ RailType ScriptObject::GetRailType()
{
return GetStorage()->rail_type;
return GetStorage().rail_type;
}
/* static */ void ScriptObject::SetLastCommandRes(bool res)
{
GetStorage()->last_command_res = res;
GetStorage().last_command_res = res;
}
/* static */ bool ScriptObject::GetLastCommandRes()
{
return GetStorage()->last_command_res;
return GetStorage().last_command_res;
}
/* static */ void ScriptObject::SetLastCommandResData(CommandDataBuffer data)
{
GetStorage()->last_cmd_ret = std::move(data);
GetStorage().last_cmd_ret = std::move(data);
}
/* static */ const CommandDataBuffer &ScriptObject::GetLastCommandResData()
{
return GetStorage()->last_cmd_ret;
return GetStorage().last_cmd_ret;
}
/* static */ void ScriptObject::SetCompany(::CompanyID company)
{
if (GetStorage()->root_company == INVALID_OWNER) GetStorage()->root_company = company;
GetStorage()->company = company;
if (GetStorage().root_company == INVALID_OWNER) GetStorage().root_company = company;
GetStorage().company = company;
_current_company = company;
}
/* static */ ::CompanyID ScriptObject::GetCompany()
{
return GetStorage()->company;
return GetStorage().company;
}
/* static */ ::CompanyID ScriptObject::GetRootCompany()
{
return GetStorage()->root_company;
return GetStorage().root_company;
}
/* static */ bool ScriptObject::CanSuspend()
{
Squirrel *squirrel = ScriptObject::GetActiveInstance().engine;
return GetStorage()->allow_do_command && squirrel->CanSuspend();
return GetStorage().allow_do_command && squirrel->CanSuspend();
}
/* static */ ScriptEventQueue &ScriptObject::GetEventQueue()
{
return GetStorage()->event_queue;
return GetStorage().event_queue;
}
/* static */ ScriptLogTypes::LogData &ScriptObject::GetLogData()
{
return GetStorage()->log_data;
return GetStorage().log_data;
}
/* static */ void ScriptObject::SetCallbackVariable(int index, int value)
{
if (static_cast<size_t>(index) >= GetStorage()->callback_value.size()) GetStorage()->callback_value.resize(index + 1);
GetStorage()->callback_value[index] = value;
if (static_cast<size_t>(index) >= GetStorage().callback_value.size()) GetStorage().callback_value.resize(index + 1);
GetStorage().callback_value[index] = value;
}
/* static */ int ScriptObject::GetCallbackVariable(int index)
{
return GetStorage()->callback_value[index];
return GetStorage().callback_value[index];
}
/* static */ CommandCallbackData *ScriptObject::GetDoCommandCallback()
@ -310,8 +310,8 @@ ScriptObject::DisableDoCommandScope::DisableDoCommandScope()
IncreaseDoCommandCosts(res.GetCost());
if (!_generating_world) {
/* Charge a nominal fee for asynchronously executed commands */
Squirrel *engine = ScriptObject::GetActiveInstance().engine;
Squirrel::DecreaseOps(engine->GetVM(), 100);
Squirrel &engine = *ScriptObject::GetActiveInstance().engine;
Squirrel::DecreaseOps(engine.GetVM(), 100);
}
if (callback != nullptr) {
/* Insert return value into to stack and throw a control code that

View File

@ -100,7 +100,7 @@ void ScriptInstance::Initialize(const std::string &main_script, const std::strin
void ScriptInstance::RegisterAPI()
{
squirrel_register_std(this->engine);
squirrel_register_std(*this->engine);
}
bool ScriptInstance::LoadCompatibilityScript(std::string_view api_version, Subdirectory dir)
@ -318,9 +318,10 @@ void ScriptInstance::CollectGarbage()
}
ScriptStorage *ScriptInstance::GetStorage()
ScriptStorage &ScriptInstance::GetStorage()
{
return this->storage;
assert(this->storage != nullptr);
return *this->storage;
}
ScriptLogTypes::LogData &ScriptInstance::GetLogData()

View File

@ -91,7 +91,7 @@ public:
/**
* Get the storage of this script.
*/
class ScriptStorage *GetStorage();
class ScriptStorage &GetStorage();
/**
* Get the log pointer of this script.
@ -146,7 +146,11 @@ public:
/**
* Get the controller attached to the instance.
*/
class ScriptController *GetController() { return controller; }
class ScriptController &GetController()
{
assert(this->controller != nullptr);
return *this->controller;
}
/**
* Return the "this script died" value

View File

@ -536,7 +536,7 @@ void Squirrel::Initialize()
sq_setforeignptr(this->vm, this);
sq_pushroottable(this->vm);
squirrel_register_global_std(this);
squirrel_register_global_std(*this);
/* Set consts table as delegate of root table, so consts/enums defined via require() are accessible */
sq_pushconsttable(this->vm);

View File

@ -82,20 +82,20 @@ SQInteger SquirrelStd::notifyallexceptions(HSQUIRRELVM vm)
return SQ_ERROR;
}
void squirrel_register_global_std(Squirrel *engine)
void squirrel_register_global_std(Squirrel &engine)
{
/* We don't use squirrel_helper here, as we want to register to the global
* scope and not to a class. */
engine->AddMethod("require", &SquirrelStd::require, ".s");
engine->AddMethod("notifyallexceptions", &SquirrelStd::notifyallexceptions, ".b");
engine.AddMethod("require", &SquirrelStd::require, ".s");
engine.AddMethod("notifyallexceptions", &SquirrelStd::notifyallexceptions, ".b");
}
void squirrel_register_std(Squirrel *engine)
void squirrel_register_std(Squirrel &engine)
{
/* We don't use squirrel_helper here, as we want to register to the global
* scope and not to a class. */
engine->AddMethod("min", &SquirrelStd::min, ".ii");
engine->AddMethod("max", &SquirrelStd::max, ".ii");
engine.AddMethod("min", &SquirrelStd::min, ".ii");
engine.AddMethod("max", &SquirrelStd::max, ".ii");
sqstd_register_mathlib(engine->GetVM());
sqstd_register_mathlib(engine.GetVM());
}

View File

@ -52,12 +52,12 @@ public:
/**
* Register all standard functions we want to give to a script.
*/
void squirrel_register_std(Squirrel *engine);
void squirrel_register_std(Squirrel &engine);
/**
* Register all standard functions that are available on first startup.
* @note this set is very limited, and is only meant to load other scripts and things like that.
*/
void squirrel_register_global_std(Squirrel *engine);
void squirrel_register_global_std(Squirrel &engine);
#endif /* SQUIRREL_STD_HPP */

View File

@ -3331,7 +3331,7 @@ draw_default_foundation:
auto result = GetRoadStopLayout(ti, stopspec, st, type, view, regs100);
if (result.has_value()) {
if (stopspec->flags.Test(RoadStopSpecFlag::DrawModeRegister)) {
stop_draw_mode = static_cast<RoadStopDrawMode>(regs100[0]);
stop_draw_mode = static_cast<RoadStopDrawModes>(regs100[0]);
}
if (type == StationType::RoadWaypoint && stop_draw_mode.Test(RoadStopDrawMode::WaypGround)) {
draw_ground = true;

View File

@ -37,6 +37,7 @@ TimerGameEconomy::Year TimerGameEconomy::year = {};
TimerGameEconomy::Month TimerGameEconomy::month = {};
TimerGameEconomy::Date TimerGameEconomy::date = {};
TimerGameEconomy::DateFract TimerGameEconomy::date_fract = {};
uint TimerGameEconomy::days_since_last_month = {};
/**
* Converts a Date to a Year, Month & Day.
@ -133,6 +134,7 @@ bool TimerManager<TimerGameEconomy>::Elapsed([[maybe_unused]] TimerGameEconomy::
/* increase day counter */
TimerGameEconomy::date++;
++TimerGameEconomy::days_since_last_month;
TimerGameEconomy::YearMonthDay ymd = TimerGameEconomy::ConvertDateToYMD(TimerGameEconomy::date);
@ -177,6 +179,8 @@ bool TimerManager<TimerGameEconomy>::Elapsed([[maybe_unused]] TimerGameEconomy::
}
}
if (new_month) TimerGameEconomy::days_since_last_month = 0;
/* check if we reached the maximum year, decrement dates by a year */
if (TimerGameEconomy::year == EconomyTime::MAX_YEAR + 1) {
TimerGameEconomy::year--;

View File

@ -37,6 +37,8 @@ public:
static Date date; ///< Current date in days (day counter).
static DateFract date_fract; ///< Fractional part of the day.
static uint days_since_last_month; ///< Number of days that have elapsed since the last month.
static YearMonthDay ConvertDateToYMD(Date date);
static Date ConvertYMDToDate(Year year, Month month, Day day);
static void SetDate(Date date, DateFract fract);

View File

@ -427,6 +427,7 @@ struct TimetableWindow : Window {
void DrawTimetablePanel(const Rect &r) const
{
const Vehicle *v = this->vehicle;
if (v->GetNumOrders() == 0) return;
Rect tr = r.Shrink(WidgetDimensions::scaled.framerect);
int i = this->vscroll->GetPosition();
VehicleOrderID order_id = (i + 1) / 2;