Skip to content

Commit

Permalink
Feature: Show depot signs of removed depots. (based on patch by adf88,
Browse files Browse the repository at this point in the history
  • Loading branch information
J0anJosep committed Feb 19, 2022
1 parent 3da794c commit f78301e
Show file tree
Hide file tree
Showing 10 changed files with 119 additions and 2 deletions.
13 changes: 13 additions & 0 deletions src/depot.cpp
Expand Up @@ -17,6 +17,7 @@
#include "vehiclelist.h"
#include "command_func.h"
#include "vehicle_base.h"
#include "viewport_kdtree.h"

#include "safeguards.h"

Expand Down Expand Up @@ -63,6 +64,10 @@ Depot::~Depot()
this->veh_type, this->company, this->index).Pack());

InvalidateWindowData(WC_SELECT_DEPOT, this->veh_type);

/* The sign will now disappear. */
_viewport_sign_kdtree.Remove(ViewportSignKdtreeItem::MakeDepot(this->index));
this->sign.MarkDirty();
}

/**
Expand All @@ -77,6 +82,10 @@ void Depot::Reuse(TileIndex xy)
this->xy = xy;
this->ta.tile = xy;
this->ta.h = this->ta.w = 1;

/* Ensure the sign is not drawn */
_viewport_sign_kdtree.Remove(ViewportSignKdtreeItem::MakeDepot(this->index));
this->sign.MarkDirty();
}

/**
Expand All @@ -93,6 +102,10 @@ void Depot::Disuse()
{
/* Mark that the depot is demolished and start the countdown. */
this->delete_ctr = 8;

/* Update the sign, it will be visible from now. */
this->UpdateVirtCoord();
_viewport_sign_kdtree.Insert(ViewportSignKdtreeItem::MakeDepot(this->index));
}

/**
Expand Down
3 changes: 3 additions & 0 deletions src/depot_base.h
Expand Up @@ -11,6 +11,7 @@
#define DEPOT_BASE_H

#include "depot_map.h"
#include "viewport_type.h"
#include "core/pool_type.hpp"
#include "rail_type.h"
#include "road_type.h"
Expand All @@ -32,6 +33,7 @@ struct Depot : DepotPool::PoolItem<&_depot_pool> {
CompanyID company;
VehicleType veh_type;
byte delete_ctr; ///< Delete counter. If greater than 0 then it is decremented until it reaches 0; the depot is then deleted.
ViewportSign sign; ///< NOSAVE: Dimensions of sign

union {
RoadTypes road_types;
Expand Down Expand Up @@ -84,6 +86,7 @@ struct Depot : DepotPool::PoolItem<&_depot_pool> {

void Reuse(TileIndex xy);
void Disuse();
void UpdateVirtCoord();

/* Check we can add some tiles to this depot. */
CommandCost BeforeAddTiles(TileArea ta);
Expand Down
27 changes: 27 additions & 0 deletions src/depot_cmd.cpp
Expand Up @@ -18,6 +18,10 @@
#include "window_func.h"
#include "depot_cmd.h"
#include "date_func.h"
#include "strings_func.h"
#include "landscape.h"
#include "viewport_kdtree.h"


#include "table/strings.h"

Expand Down Expand Up @@ -60,6 +64,8 @@ CommandCost CmdRenameDepot(DoCommandFlag flags, DepotID depot_id, const std::str
}

if (flags & DC_EXEC) {
/* _viewport_sign_kdtree does not need to be updated, only in-use depots can be renamed */

if (reset) {
d->name.clear();
MakeDefaultName(d);
Expand All @@ -77,6 +83,27 @@ CommandCost CmdRenameDepot(DoCommandFlag flags, DepotID depot_id, const std::str
return CommandCost();
}

/** Update the virtual coords needed to draw the depot sign. */
void Depot::UpdateVirtCoord()
{
Point pt = RemapCoords2(TileX(this->xy) * TILE_SIZE, TileY(this->xy) * TILE_SIZE);

pt.y -= 32 * ZOOM_LVL_BASE;

SetDParam(0, this->veh_type);
SetDParam(1, this->index);
this->sign.UpdatePosition(pt.x, pt.y, STR_VIEWPORT_DEPOT, STR_VIEWPORT_DEPOT_TINY);

SetWindowDirty(WC_VEHICLE_DEPOT, this->index);
}

/** Update the virtual coords needed to draw the depot sign for all depots. */
void UpdateAllDepotVirtCoords()
{
/* Only demolished depots have signs. */
for (Depot *d : Depot::Iterate()) if (!d->IsInUse()) d->UpdateVirtCoord();
}

/**
* Find a demolished depot close to a tile.
* @param ta Tile area to search for.
Expand Down
1 change: 1 addition & 0 deletions src/depot_func.h
Expand Up @@ -19,6 +19,7 @@
void ShowDepotWindow(DepotID depot_id);

void DeleteDepotHighlightOfVehicle(const Vehicle *v);
void UpdateAllDepotVirtCoords();

/**
* Find out if the slope of the tile is suitable to build a depot of given direction
Expand Down
3 changes: 3 additions & 0 deletions src/lang/english.txt
Expand Up @@ -5504,6 +5504,9 @@ STR_VIEWPORT_STATION_TINY :{TINY_FONT}{STA
STR_VIEWPORT_WAYPOINT :{WAYPOINT}
STR_VIEWPORT_WAYPOINT_TINY :{TINY_FONT}{WAYPOINT}

STR_VIEWPORT_DEPOT :{DEPOT}
STR_VIEWPORT_DEPOT_TINY :{TINY_FONT}{DEPOT}

# Simple strings to get specific types of data
STR_COMPANY_NAME :{COMPANY}
STR_COMPANY_NAME_COMPANY_NUM :{COMPANY} {COMPANY_NUM}
Expand Down
2 changes: 2 additions & 0 deletions src/saveload/afterload.cpp
Expand Up @@ -11,6 +11,7 @@
#include "../void_map.h"
#include "../signs_base.h"
#include "../depot_base.h"
#include "../depot_func.h"
#include "../fios.h"
#include "../gamelog_internal.h"
#include "../network/network.h"
Expand Down Expand Up @@ -217,6 +218,7 @@ static inline RailType UpdateRailType(RailType rt, RailType min)
void UpdateAllVirtCoords()
{
UpdateAllStationVirtCoords();
UpdateAllDepotVirtCoords();
UpdateAllSignVirtCoords();
UpdateAllTownVirtCoords();
UpdateAllTextEffectVirtCoords();
Expand Down
4 changes: 4 additions & 0 deletions src/station.cpp
Expand Up @@ -712,6 +712,10 @@ void Airport::SetHangar(bool create)
dep->depot_tiles.push_back(this->GetHangarTile(i));
}
} else {
if (this->depot_id == INVALID_DEPOT) return;
assert(Depot::IsValidID(this->depot_id));
Depot *dep = Depot::Get(this->depot_id);
dep->Disuse();
delete Depot::GetIfValid(this->depot_id);
this->depot_id = INVALID_DEPOT;
}
Expand Down
3 changes: 3 additions & 0 deletions src/town_cmd.cpp
Expand Up @@ -43,6 +43,7 @@
#include "core/random_func.hpp"
#include "core/backup_type.hpp"
#include "depot_base.h"
#include "depot_func.h"
#include "object_map.h"
#include "object_base.h"
#include "ai/ai.hpp"
Expand Down Expand Up @@ -2761,6 +2762,8 @@ CommandCost CmdRenameTown(DoCommandFlag flags, TownID town_id, const std::string
ClearAllStationCachedNames();
ClearAllIndustryCachedNames();
UpdateAllStationVirtCoords();
UpdateAllDepotVirtCoords();
RebuildViewportKdtree();
}
return CommandCost();
}
Expand Down
58 changes: 56 additions & 2 deletions src/viewport.cpp
Expand Up @@ -65,6 +65,8 @@
#include "viewport_func.h"
#include "station_base.h"
#include "waypoint_base.h"
#include "depot_base.h"
#include "depot_func.h"
#include "town.h"
#include "signs_base.h"
#include "signs_func.h"
Expand Down Expand Up @@ -1355,15 +1357,18 @@ static void ViewportAddKdtreeSigns(DrawPixelInfo *dpi)
bool show_waypoints = HasBit(_display_opt, DO_SHOW_WAYPOINT_NAMES) && _game_mode != GM_MENU;
bool show_towns = HasBit(_display_opt, DO_SHOW_TOWN_NAMES) && _game_mode != GM_MENU;
bool show_signs = HasBit(_display_opt, DO_SHOW_SIGNS) && !IsInvisibilitySet(TO_SIGNS);
bool show_depotsigns = _game_mode != GM_MENU;
bool show_competitors = HasBit(_display_opt, DO_SHOW_COMPETITOR_SIGNS);

const BaseStation *st;
const Sign *si;
const Depot *depot;

/* Collect all the items first and draw afterwards, to ensure layering */
std::vector<const BaseStation *> stations;
std::vector<const Town *> towns;
std::vector<const Sign *> signs;
std::vector<const Depot *> depots;

_viewport_sign_kdtree.FindContained(search_rect.left, search_rect.top, search_rect.right, search_rect.bottom, [&](const ViewportSignKdtreeItem & item) {
switch (item.type) {
Expand Down Expand Up @@ -1397,13 +1402,25 @@ static void ViewportAddKdtreeSigns(DrawPixelInfo *dpi)
si = Sign::Get(item.id.sign);

/* Don't draw if sign is owned by another company and competitor signs should be hidden.
* Note: It is intentional that also signs owned by OWNER_NONE are hidden. Bankrupt
* companies can leave OWNER_NONE signs after them. */
* Note: It is intentional that also signs owned by OWNER_NONE are hidden. Bankrupt
* companies can leave OWNER_NONE signs after them. */
if (!show_competitors && _local_company != si->owner && si->owner != OWNER_DEITY) break;

signs.push_back(si);
break;

case ViewportSignKdtreeItem::VKI_DEPOT:
if (!show_depotsigns) break;
depot = Depot::Get(item.id.depot);

/* Only show depot name after the depot is removed. */
if (depot->IsInUse()) break;
/* Don't draw if depot is owned by another company and competitor signs are hidden. */
if (!show_competitors && _local_company != depot->company) break;

depots.push_back(depot);
break;

default:
NOT_REACHED();
}
Expand All @@ -1425,6 +1442,11 @@ static void ViewportAddKdtreeSigns(DrawPixelInfo *dpi)
si->index, 0, (si->owner == OWNER_NONE) ? COLOUR_GREY : (si->owner == OWNER_DEITY ? INVALID_COLOUR : _company_colours[si->owner]));
}

for (const auto *d : depots) {
ViewportAddString(dpi, ZOOM_LVL_OUT_16X, &d->sign, STR_VIEWPORT_DEPOT,
STR_VIEWPORT_DEPOT_TINY, STR_NULL, d->veh_type, d->index, COLOUR_GREY);
}

for (const auto *st : stations) {
if (Station::IsExpected(st)) {
/* Station */
Expand Down Expand Up @@ -2171,6 +2193,7 @@ static bool CheckClickOnViewportSign(const Viewport *vp, int x, int y)
BaseStation *st = nullptr, *last_st = nullptr;
Town *t = nullptr, *last_t = nullptr;
Sign *si = nullptr, *last_si = nullptr;
Depot *dep = nullptr, *last_dep = nullptr;

/* See ViewportAddKdtreeSigns() for details on the search logic */
_viewport_sign_kdtree.FindContained(search_rect.left, search_rect.top, search_rect.right, search_rect.bottom, [&](const ViewportSignKdtreeItem & item) {
Expand Down Expand Up @@ -2202,6 +2225,12 @@ static bool CheckClickOnViewportSign(const Viewport *vp, int x, int y)
if (CheckClickOnViewportSign(vp, x, y, &si->sign)) last_si = si;
break;

case ViewportSignKdtreeItem::VKI_DEPOT:
dep = Depot::Get(item.id.depot);
if (!show_competitors && _local_company != st->owner && st->owner != OWNER_NONE) break;
if (CheckClickOnViewportSign(vp, x, y, &dep->sign)) last_dep = dep;
break;

default:
NOT_REACHED();
}
Expand All @@ -2221,6 +2250,9 @@ static bool CheckClickOnViewportSign(const Viewport *vp, int x, int y)
} else if (last_si != nullptr) {
HandleClickOnSign(last_si);
return true;
} else if (last_dep != nullptr) {
ShowDepotWindow(last_dep->index);
return true;
} else {
return false;
}
Expand All @@ -2244,6 +2276,23 @@ ViewportSignKdtreeItem ViewportSignKdtreeItem::MakeStation(StationID id)
return item;
}

ViewportSignKdtreeItem ViewportSignKdtreeItem::MakeDepot(DepotID id)
{
ViewportSignKdtreeItem item;
item.type = VKI_DEPOT;
item.id.depot = id;

const Depot* depot = Depot::Get(id);

item.center = depot->sign.center;
item.top = depot->sign.top;

/* Assume the sign can be a candidate for drawing, so measure its width */
_viewport_sign_maxwidth = std::max<int>(_viewport_sign_maxwidth, depot->sign.width_normal);

return item;
}

ViewportSignKdtreeItem ViewportSignKdtreeItem::MakeWaypoint(StationID id)
{
ViewportSignKdtreeItem item;
Expand Down Expand Up @@ -2319,6 +2368,11 @@ void RebuildViewportKdtree()
if (sign->sign.kdtree_valid) items.push_back(ViewportSignKdtreeItem::MakeSign(sign->index));
}

for (const Depot *dep : Depot::Iterate()) {
if (dep->IsInUse()) continue;
items.push_back(ViewportSignKdtreeItem::MakeDepot(dep->index));
}

_viewport_sign_kdtree.Build(items.begin(), items.end());
}

Expand Down
7 changes: 7 additions & 0 deletions src/viewport_kdtree.h
Expand Up @@ -22,12 +22,14 @@ struct ViewportSignKdtreeItem {
VKI_WAYPOINT,
VKI_TOWN,
VKI_SIGN,
VKI_DEPOT,
};
ItemType type;
union {
StationID station;
TownID town;
SignID sign;
DepotID depot;
} id;
int32 center;
int32 top;
Expand All @@ -43,6 +45,8 @@ struct ViewportSignKdtreeItem {
return this->id.town == other.id.town;
case VKI_SIGN:
return this->id.sign == other.id.sign;
case VKI_DEPOT:
return this->id.depot == other.id.depot;
default:
NOT_REACHED();
}
Expand All @@ -59,6 +63,8 @@ struct ViewportSignKdtreeItem {
return this->id.town < other.id.town;
case VKI_SIGN:
return this->id.sign < other.id.sign;
case VKI_DEPOT:
return this->id.depot < other.id.depot;
default:
NOT_REACHED();
}
Expand All @@ -68,6 +74,7 @@ struct ViewportSignKdtreeItem {
static ViewportSignKdtreeItem MakeWaypoint(StationID id);
static ViewportSignKdtreeItem MakeTown(TownID id);
static ViewportSignKdtreeItem MakeSign(SignID id);
static ViewportSignKdtreeItem MakeDepot(DepotID id);
};

inline int32 Kdtree_ViewportSignXYFunc(const ViewportSignKdtreeItem &item, int dim)
Expand Down

0 comments on commit f78301e

Please sign in to comment.