diff --git a/src/company_cmd.cpp b/src/company_cmd.cpp index 0fc1126150..bdf920ae3b 100644 --- a/src/company_cmd.cpp +++ b/src/company_cmd.cpp @@ -42,6 +42,7 @@ #include "timer/timer.h" #include "timer/timer_game_tick.h" #include "tilehighlight_func.h" +#include "plans_func.h" #include "table/strings.h" @@ -147,6 +148,7 @@ void SetLocalCompany(CompanyID new_company) InvalidateWindowClassesData(WC_SIGN_LIST, -1); InvalidateWindowClassesData(WC_GOALS_LIST); ClearZoningCaches(); + InvalidatePlanCaches(); } /** diff --git a/src/economy.cpp b/src/economy.cpp index f61031b315..32e689a806 100644 --- a/src/economy.cpp +++ b/src/economy.cpp @@ -58,6 +58,7 @@ #include "pathfinder/yapf/yapf_cache.h" #include "debug_desync.h" #include "event_logs.h" +#include "plans_func.h" #include "table/strings.h" #include "table/pricebase.h" @@ -669,6 +670,8 @@ void ChangeOwnershipOfCompanyItems(Owner old_owner, Owner new_owner) extern void MarkAllViewportMapLandscapesDirty(); MarkAllViewportMapLandscapesDirty(); + + InvalidatePlanCaches(); } /** diff --git a/src/misc.cpp b/src/misc.cpp index 17e3e539f7..f73dc92c95 100644 --- a/src/misc.cpp +++ b/src/misc.cpp @@ -41,6 +41,7 @@ #include "tbtr_template_vehicle_func.h" #include "event_logs.h" #include "string_func.h" +#include "plans_func.h" #include "3rdparty/monocypher/monocypher.h" #include "safeguards.h" @@ -176,6 +177,7 @@ void InitializeGame(uint size_x, uint size_y, bool reset_date, bool reset_settin ClearAllSignalSpeedRestrictions(); ClearZoningCaches(); + InvalidatePlanCaches(); IntialiseOrderDestinationRefcountMap(); ResetPersistentNewGRFData(); diff --git a/src/openttd.cpp b/src/openttd.cpp index 4175b7ee78..2e3672142f 100644 --- a/src/openttd.cpp +++ b/src/openttd.cpp @@ -93,6 +93,7 @@ #include "timer/timer_game_realtime.h" #include "timer/timer_game_tick.h" #include "network/network_sync.h" +#include "plans_func.h" #include "linkgraph/linkgraphschedule.h" #include "tracerestrict.h" @@ -507,6 +508,7 @@ static void ShutdownGame() ClearAllSignalSpeedRestrictions(); ClearZoningCaches(); + InvalidatePlanCaches(); ClearOrderDestinationRefcountMap(); /* No NewGRFs were loaded when it was still bootstrapping. */ diff --git a/src/plans.cpp b/src/plans.cpp index 9f75fce3df..df7e8b28fc 100644 --- a/src/plans.cpp +++ b/src/plans.cpp @@ -17,6 +17,9 @@ INSTANTIATE_POOL_METHODS(Plan) Plan *_current_plan = nullptr; Plan *_new_plan = nullptr; +uint64_t _plan_update_counter = 0; +uint64_t _last_plan_visibility_check = 0; +bool _last_plan_visibility_check_result = false; void PlanLine::UpdateVisualExtents() { @@ -63,3 +66,25 @@ bool Plan::ValidateNewLine() } return ret; } + +void UpdateAreAnyPlansVisible() +{ + _last_plan_visibility_check = _plan_update_counter; + + if (_current_plan && _current_plan->temp_line->tiles.size() > 1) { + _last_plan_visibility_check_result = true; + return; + } + + for (const Plan *p : Plan::Iterate()) { + if (!p->IsVisible()) continue; + for (const PlanLine *pl : p->lines) { + if (pl->visible) { + _last_plan_visibility_check_result = true; + return; + } + } + } + + _last_plan_visibility_check_result = false; +} diff --git a/src/plans_base.h b/src/plans_base.h index 8000ce714e..b7c1c5a6d4 100644 --- a/src/plans_base.h +++ b/src/plans_base.h @@ -36,6 +36,7 @@ struct PlanLine { { this->visible = true; this->focused = false; + _plan_update_counter++; } ~PlanLine() @@ -46,6 +47,7 @@ struct PlanLine { void Clear() { this->tiles.clear(); + _plan_update_counter++; } bool AppendTile(TileIndex tile) @@ -81,12 +83,16 @@ struct PlanLine { if (this->tiles.size() * sizeof(TileIndex) >= MAX_CMD_TEXT_LENGTH) return false; this->tiles.push_back(tile); + _plan_update_counter++; return true; } void SetFocus(bool focused) { - if (this->focused != focused) this->MarkDirty(); + if (this->focused != focused) { + this->MarkDirty(); + _plan_update_counter++; + } this->focused = focused; } @@ -98,7 +104,10 @@ struct PlanLine { void SetVisibility(bool visible) { - if (this->visible != visible) this->MarkDirty(); + if (this->visible != visible) { + this->MarkDirty(); + _plan_update_counter++; + } this->visible = visible; } @@ -176,6 +185,7 @@ struct Plan : PlanPool::PoolItem<&_plan_pool> { void SetVisibility(bool visible, bool do_lines = true) { this->visible = visible; + _plan_update_counter++; if (!do_lines) return; for (PlanLineVector::iterator it = lines.begin(); it != lines.end(); it++) { diff --git a/src/plans_cmd.cpp b/src/plans_cmd.cpp index 8852869de3..f0c314817b 100644 --- a/src/plans_cmd.cpp +++ b/src/plans_cmd.cpp @@ -160,6 +160,7 @@ CommandCost CmdChangePlanColour(TileIndex tile, DoCommandFlag flags, uint32_t p1 if (ret.Failed()) return ret; if (flags & DC_EXEC) { p->colour = (Colours)p2; + _plan_update_counter++; Window *w = FindWindowById(WC_PLANS, 0); if (w) w->InvalidateData(INVALID_PLAN, false); for (const PlanLine *line : p->lines) { diff --git a/src/plans_func.h b/src/plans_func.h index 53ff5ad153..38a6ddee7a 100644 --- a/src/plans_func.h +++ b/src/plans_func.h @@ -12,9 +12,24 @@ #include "plans_type.h" -void ShowPlansWindow(); - extern Plan *_new_plan; extern Plan *_current_plan; +extern uint64_t _plan_update_counter; +extern uint64_t _last_plan_visibility_check; +extern bool _last_plan_visibility_check_result; + +void ShowPlansWindow(); +void UpdateAreAnyPlansVisible(); + +inline bool AreAnyPlansVisible() +{ + if (_plan_update_counter != _last_plan_visibility_check) UpdateAreAnyPlansVisible(); + return _last_plan_visibility_check_result; +} + +inline void InvalidatePlanCaches() +{ + _plan_update_counter++; +} #endif /* PLANS_FUNC_H */ diff --git a/src/viewport.cpp b/src/viewport.cpp index e0b719e3e8..b3971d7f72 100644 --- a/src/viewport.cpp +++ b/src/viewport.cpp @@ -2726,7 +2726,7 @@ static void ViewportDrawVehicleRouteSteps(const Viewport * const vp) void ViewportDrawPlans(const Viewport *vp) { - if (Plan::GetNumItems() == 0 && !(_current_plan && _current_plan->temp_line->tiles.size() > 1)) return; + if (!AreAnyPlansVisible()) return; DrawPixelInfo dpi_for_text = _vdd->MakeDPIForText(); _cur_dpi = &dpi_for_text;