Fix crash when setting separation mode for vehicles with no orders list.
[openttd-joker.git] / src / overlay_cmd.cpp
blob203c90bb97ab72c821279ba758836ed808898f34
1 /* $Id$ */
3 /** @file overlay_cmd.cpp Handling of overlays. */
5 #include "stdafx.h"
6 #include "tile_type.h"
7 #include "tile_cmd.h"
8 #include "overlay.h"
9 #include "station_func.h"
10 #include "viewport_func.h"
11 #include "overlay_cmd.h"
13 Overlays* Overlays::instance = nullptr;
15 Overlays* Overlays::Instance()
17 if (instance == nullptr)
18 instance = new Overlays();
19 return instance;
22 void Overlays::AddStation(const Station* st)
24 this->catchmentOverlay.insert(st);
27 void Overlays::RemoveStation(const Station* st)
29 this->catchmentOverlay.erase(st);
32 void Overlays::ToggleStation(const Station* st)
34 if(this->HasStation(st)) {
35 this->RemoveStation(st);
36 } else {
37 this->AddStation(st);
41 void Overlays::HandleSignalProgramDeletion(const SignalProgram* program)
43 if (this->logic_signal_program == program) {
44 RefreshLogicSignalOverlay();
45 this->logic_signal_program = nullptr;
49 void Overlays::SetLogicSignalOverlay(const SignalProgram* program)
51 // Old program input tiles
52 RefreshLogicSignalOverlay();
54 this->logic_signal_program = program;
56 // New program input tiles
57 RefreshLogicSignalOverlay();
60 void Overlays::ClearLogicSignalOverlay()
62 SetLogicSignalOverlay(nullptr);
65 void Overlays::RefreshLogicSignalOverlay() const
67 if (this->logic_signal_program != nullptr) {
68 auto signal_references = this->logic_signal_program->GetSignalReferences();
70 for (auto reference : signal_references) {
71 MarkTileDirtyByTile(GetTileFromSignalReference(reference));
74 MarkTileDirtyByTile(this->logic_signal_program->tile);
78 void Overlays::Clear()
80 this->catchmentOverlay.clear();
82 // Old program input tiles
83 RefreshLogicSignalOverlay();
85 this->logic_signal_program = nullptr;
88 bool Overlays::IsTileLogicSignalInput(const TileInfo* ti)
90 if (this->logic_signal_program == nullptr) return false;
92 auto signal_references = this->logic_signal_program->GetSignalReferences();
94 return std::any_of(signal_references.begin(), signal_references.end(), [&](auto signal_reference) {
95 return GetTileFromSignalReference(signal_reference) == ti->tile;
96 });
99 bool Overlays::IsTileLogicSignalOutput(const TileInfo* ti) const
101 if (this->logic_signal_program == nullptr) return false;
103 return ti->tile == this->logic_signal_program->tile;
106 bool Overlays::IsTileInCatchmentArea(const TileInfo* ti, CatchmentType type)
108 for(std::set<const Station *>::iterator iter = catchmentOverlay.begin();iter != catchmentOverlay.end();) {
109 const Station *st = *iter;
110 if( st->IsTileInCatchmentArea(ti, type))
111 return true;
112 iter++;
114 return false;
117 bool Overlays::HasStation(const Station* st)
119 return (this->catchmentOverlay.find(st) != this->catchmentOverlay.end());
122 Overlays::~Overlays()
124 this->catchmentOverlay.clear();