summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorbjarni <bjarni@openttd.org>2007-02-06 11:11:12 +0000
committerbjarni <bjarni@openttd.org>2007-02-06 11:11:12 +0000
commit80125fd934af1e79aa3223f9892d5754d3b4affa (patch)
treee35fe2197ef3aba9cb1b1902312f0f010202376b
parent2918fab68d1a37c0a342dfae4a2bfa350facaf8b (diff)
downloadopenttd-80125fd934af1e79aa3223f9892d5754d3b4affa.tar.xz
(svn r8610) -Codechange/Feature: rewrote the list handling in the autoreplace window
-The user will notice the following changes: All vehicle types behaves in the same way Adding/removing an engine (new design and so on) can no longer result in the window selecting a new engine All valid replacements will be displayed since it looks at refitting options as well (this solves the missing ships with the newships GRF) Since you can't replace an engine into itself, the engine selected in the left list will no longer appear in the right list -The code changes: Instead of looping all engines all the time, each list generates a list like the build windows This ensures consistent list generation since only one function can loop all engines Unified code for all vehicle types It now use the lists to call the drawing code in the build vehicle window Works on selected EngineIDs instead of selected index in the list
-rw-r--r--src/aircraft_cmd.cpp7
-rw-r--r--src/autoreplace_gui.cpp657
-rw-r--r--src/build_vehicle_gui.cpp12
-rw-r--r--src/engine.cpp10
-rw-r--r--src/engine.h27
-rw-r--r--src/players.cpp2
-rw-r--r--src/roadveh_cmd.cpp6
-rw-r--r--src/ship_cmd.cpp7
-rw-r--r--src/train_cmd.cpp16
-rw-r--r--src/vehicle.cpp5
-rw-r--r--src/vehicle.h2
-rw-r--r--src/window.h5
12 files changed, 335 insertions, 421 deletions
diff --git a/src/aircraft_cmd.cpp b/src/aircraft_cmd.cpp
index a4b0ad68f..83564318e 100644
--- a/src/aircraft_cmd.cpp
+++ b/src/aircraft_cmd.cpp
@@ -392,13 +392,14 @@ int32 CmdBuildAircraft(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
w->u.air.state = HRS_ROTOR_STOPPED;
VehiclePositionChanged(w);
}
- GetPlayer(_current_player)->num_engines[p1]++;
InvalidateWindowData(WC_VEHICLE_DEPOT, v->tile);
RebuildVehicleLists();
InvalidateWindow(WC_COMPANY, v->owner);
if (IsLocalPlayer())
- InvalidateWindow(WC_REPLACE_VEHICLE, VEH_Aircraft); //updates the replace Aircraft window
+ InvalidateAutoreplaceWindow(VEH_Aircraft); //updates the replace Aircraft window
+
+ GetPlayer(_current_player)->num_engines[p1]++;
}
return value;
@@ -437,8 +438,6 @@ int32 CmdSellAircraft(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
// Invalidate depot
InvalidateWindow(WC_VEHICLE_DEPOT, v->tile);
DoDeleteAircraft(v);
- if (IsLocalPlayer())
- InvalidateWindow(WC_REPLACE_VEHICLE, VEH_Aircraft); // updates the replace Aircraft window
}
return -(int32)v->value;
diff --git a/src/autoreplace_gui.cpp b/src/autoreplace_gui.cpp
index 2ab57c654..89f347438 100644
--- a/src/autoreplace_gui.cpp
+++ b/src/autoreplace_gui.cpp
@@ -16,6 +16,9 @@
static RailType _railtype_selected_in_replace_gui;
+static bool _rebuild_left_list;
+static bool _rebuild_right_list;
+
static const StringID _rail_types_list[] = {
STR_RAIL_VEHICLES,
STR_ELRAIL_VEHICLES,
@@ -30,332 +33,178 @@ void InitializeVehiclesGuiList(void)
_railtype_selected_in_replace_gui = RAILTYPE_RAIL;
}
-// this define is to match engine.c, but engine.c keeps it to itself
-// ENGINE_AVAILABLE is used in ReplaceVehicleWndProc
-#define ENGINE_AVAILABLE ((e->flags & 1 && HASBIT(info->climates, _opt.landscape)) || HASBIT(e->player_avail, _local_player))
-
-/* if show_outdated is selected, it do not sort psudo engines properly but it draws all engines
- * if used compined with show_cars set to false, it will work as intended. Replace window do it like that
- * this was a big hack even before show_outdated was added. Stupid newgrf :p
+/** Rebuild the left autoreplace list if an engine is removed or added
+ * @param e Engine to check if it is removed or added
+ * Note: this function only works if it is called either
+ * - when a new vehicle is build, but before it's counted in num_engines
+ * - when a vehicle is deleted and after it's substracted from num_engines
+ * - when not changing the count (used when changing replace orders)
*/
-static void train_engine_drawing_loop(int *x, int *y, int *pos, int *sel, EngineID *selected_id, RailType railtype,
- uint8 lines_drawn, bool is_engine, bool show_cars, bool show_outdated, bool show_compatible)
+void InvalidateAutoreplaceWindow(EngineID e)
{
- EngineID j;
- byte colour;
- const Player *p = GetPlayer(_local_player);
-
- for (j = 0; j < NUM_TRAIN_ENGINES; j++) {
- EngineID i = GetRailVehAtPosition(j);
- const Engine *e = GetEngine(i);
- const RailVehicleInfo *rvi = RailVehInfo(i);
- const EngineInfo *info = EngInfo(i);
-
- if (!EngineHasReplacementForPlayer(p, i) && p->num_engines[i] == 0 && show_outdated) continue;
-
- if ((rvi->power == 0 && !show_cars) || (rvi->power != 0 && show_cars)) // show wagons or engines (works since wagons do not have power)
- continue;
-
- if (*sel == 0) *selected_id = j;
-
-
- colour = *sel == 0 ? 0xC : 0x10;
- if (!(ENGINE_AVAILABLE && show_outdated && RailVehInfo(i)->power && IsCompatibleRail(rvi->railtype, railtype))) {
- if ((!IsCompatibleRail(rvi->railtype, railtype) && show_compatible)
- || (rvi->railtype != railtype && !show_compatible)
- || (rvi->railveh_type != RAILVEH_WAGON) != is_engine ||
- !HASBIT(e->player_avail, _local_player))
- continue;
-#if 0
- } else {
- // TODO find a nice red colour for vehicles being replaced
- if ( _autoreplace_array[i] != i )
- colour = *sel == 0 ? 0x44 : 0x45;
-#endif
- }
-
- if (IS_INT_INSIDE(--*pos, -lines_drawn, 0)) {
- DrawString(*x + 59, *y + 2, GetCustomEngineName(i),
- colour);
- // show_outdated is true only for left side, which is where we show old replacements
- DrawTrainEngine(*x + 29, *y + 6, i, (p->num_engines[i] == 0 && show_outdated) ?
- PALETTE_CRASH : GetEnginePalette(i, _local_player));
- if ( show_outdated ) {
- SetDParam(0, p->num_engines[i]);
- DrawStringRightAligned(213, *y+5, STR_TINY_BLACK, 0);
- }
- *y += 14;
- }
- --*sel;
+ Player *p = GetPlayer(_local_player);
+ byte type = GetEngine(e)->type;
+
+ if (p->num_engines[e] == 0) {
+ /* We don't have any of this engine type.
+ * Either we just sold the last one, we build a new one or we stopped replacing it.
+ * In all cases, we need to update the left list */
+ _rebuild_left_list = true;
+ } else {
+ _rebuild_left_list = false;
}
+ _rebuild_right_list = false;
+ InvalidateWindowData(WC_REPLACE_VEHICLE, type);
}
-
-static void SetupScrollStuffForReplaceWindow(Window *w)
+/** When an engine is made buildable or is removed from being buildable, add/remove it from the build/autoreplace lists
+ * @param type The type of engine
+ */
+void AddRemoveEngineFromAutoreplaceAndBuildWindows(byte type)
{
- EngineID selected_id[2] = { INVALID_ENGINE, INVALID_ENGINE };
- const Player* p = GetPlayer(_local_player);
- uint sel[2];
- uint count = 0;
- uint count2 = 0;
- EngineID i;
-
- sel[0] = WP(w,replaceveh_d).sel_index[0];
- sel[1] = WP(w,replaceveh_d).sel_index[1];
+ _rebuild_left_list = false; // left list is only for the vehicles the player owns and is not related to being buildable
+ _rebuild_right_list = true;
+ InvalidateWindowData(WC_REPLACE_VEHICLE, type); // Update the autoreplace window
+ InvalidateWindowClassesData(WC_BUILD_VEHICLE); // The build windows needs updating as well
+}
- switch (WP(w,replaceveh_d).vehicletype) {
- case VEH_Train: {
- RailType railtype = _railtype_selected_in_replace_gui;
+/** Get the default cargo type for an engine
+ * @param engine the EngineID to get the cargo for
+ * @return the cargo type carried by the engine (CT_INVALID if engine got no cargo capacity)
+ */
+static CargoID EngineCargo(EngineID engine)
+{
+ if (engine == INVALID_ENGINE) return CT_INVALID; // surely INVALID_ENGINE can't carry anything but CT_INVALID
- w->widget[13].color = _player_colors[_local_player]; // sets the colour of that art thing
- w->widget[16].color = _player_colors[_local_player]; // sets the colour of that art thing
+ switch (GetEngine(engine)->type) {
+ default: NOT_REACHED();
+ case VEH_Train:
+ if (RailVehInfo(engine)->capacity == 0) return CT_INVALID; // no capacity -> can't carry cargo
+ return RailVehInfo(engine)->cargo_type;
+ case VEH_Road: return RoadVehInfo(engine)->cargo_type;
+ case VEH_Ship: return ShipVehInfo(engine)->cargo_type;
+ case VEH_Aircraft: return CT_PASSENGERS; // all planes are build with passengers by default
+ }
+}
- for (i = 0; i < NUM_TRAIN_ENGINES; i++) {
- EngineID eid = GetRailVehAtPosition(i);
- const Engine* e = GetEngine(eid);
- const RailVehicleInfo *rvi = RailVehInfo(eid);
- const EngineInfo* info = EngInfo(eid);
-
- // left window contains compatible engines while right window only contains engines of the selected type
- if (ENGINE_AVAILABLE &&
- (rvi->power != 0) == (WP(w, replaceveh_d).wagon_btnstate != 0)) {
- if (IsCompatibleRail(rvi->railtype, railtype) && (p->num_engines[eid] > 0 || EngineHasReplacementForPlayer(p, eid))) {
- if (sel[0] == count) selected_id[0] = eid;
- count++;
- }
- if (rvi->railtype == railtype && HASBIT(e->player_avail, _local_player)) {
- if (sel[1] == count2) selected_id[1] = eid;
- count2++;
- }
- }
- }
- break;
- }
+/** Figure out if an engine should be added to a list
+ * @param e The EngineID
+ * @param draw_left If true, then the left list is drawn (the engines specific to the railtype you selected)
+ * @param show_engines if truem then locomotives are drawn, else wagons (never both)
+ * @return true if the engine should be in the list (based on this check)
+ */
+static bool GenerateReplaceRailList(EngineID e, bool draw_left, bool show_engines)
+{
+ const RailVehicleInfo *rvi = RailVehInfo(e);
+
+ /* Ensure that the wagon/engine selection fits the engine. */
+ if ((rvi->railveh_type == RAILVEH_WAGON) == show_engines) return false;
+
+ if (draw_left && show_engines) {
+ /* Ensure that the railtype is specific to the selected one */
+ if (rvi->railtype != _railtype_selected_in_replace_gui) return false;
+ } else {
+ /* Ensure that it's a compatible railtype to the selected one (like electric <-> diesel)
+ * The vehicle do not have to have power on the railtype in question, only able to drive (pulled if needed) */
+ if (!IsCompatibleRail(rvi->railtype, _railtype_selected_in_replace_gui)) return false;
+ }
+ return true;
+}
- case VEH_Road: {
- for (i = ROAD_ENGINES_INDEX; i < ROAD_ENGINES_INDEX + NUM_ROAD_ENGINES; i++) {
- if (p->num_engines[i] > 0 || EngineHasReplacementForPlayer(p, i)) {
- if (sel[0] == count) selected_id[0] = i;
- count++;
- }
- }
+/** Figure out if two engines got at least one type of cargo in common (refitting if needed)
+ * @param engine_a one of the EngineIDs
+ * @param engine_b the other EngineID
+ * @return true if they can both carry the same type of cargo (or at least one of them got no capacity at all)
+ */
+static bool EnginesGotCargoInCommon(EngineID engine_a, EngineID engine_b)
+{
+ CargoID a = EngineCargo(engine_a);
+ CargoID b = EngineCargo(engine_b);
+
+ /* we should always be able to refit to/from locomotives without capacity
+ * Because of that, CT_INVALID shoudl always return true */
+ if (a == CT_INVALID || b == CT_INVALID || a == b) return true; // they carry no ro the same type by default
+ if (EngInfo(engine_a)->refit_mask & EngInfo(engine_b)->refit_mask) return true; // both can refit to the same
+ if (CanRefitTo(engine_a, b) || CanRefitTo(engine_b, a)) return true; // one can refit to what the other one carries
+ return false;
+}
- if (selected_id[0] != INVALID_ENGINE) { // only draw right array if we have anything in the left one
- CargoID cargo = RoadVehInfo(selected_id[0])->cargo_type;
+/** Generate a list
+ * @param w Window, that contains the list
+ * @param draw_left true if generating the left list, otherwise false
+ */
+static void GenerateReplaceVehList(Window *w, bool draw_left)
+{
+ Player *p = GetPlayer(_local_player);
+ EngineID e;
+ EngineID selected_engine = INVALID_ENGINE;
+ byte type = w->window_number;
+ byte i = draw_left ? 0 : 1;
- for (i = ROAD_ENGINES_INDEX; i < ROAD_ENGINES_INDEX + NUM_ROAD_ENGINES; i++) {
- if (cargo == RoadVehInfo(i)->cargo_type &&
- HASBIT(GetEngine(i)->player_avail, _local_player)) {
- if (sel[1] == count2) selected_id[1] = i;
- count2++;
- }
- }
- }
- break;
- }
+ EngineList *list = &WP(w, replaceveh_d).list[i];
+ EngList_RemoveAll(list);
- case VEH_Ship: {
- for (i = SHIP_ENGINES_INDEX; i < SHIP_ENGINES_INDEX + NUM_SHIP_ENGINES; i++) {
- if (p->num_engines[i] > 0 || EngineHasReplacementForPlayer(p, i)) {
- if (sel[0] == count) selected_id[0] = i;
- count++;
- }
- }
+ FOR_ALL_ENGINEIDS_OF_TYPE(e, type) {
+ if (type == VEH_Train && !GenerateReplaceRailList(e, draw_left, WP(w, replaceveh_d).wagon_btnstate)) continue; // special rules for trains
- if (selected_id[0] != INVALID_ENGINE) {
- const ShipVehicleInfo* svi = ShipVehInfo(selected_id[0]);
- CargoID cargo = svi->cargo_type;
- bool refittable = svi->refittable;
-
- for (i = SHIP_ENGINES_INDEX; i < SHIP_ENGINES_INDEX + NUM_SHIP_ENGINES; i++) {
- if (HASBIT(GetEngine(i)->player_avail, _local_player) && (
- ShipVehInfo(i)->cargo_type == cargo ||
- ShipVehInfo(i)->refittable && refittable
- )) {
- if (sel[1] == count2) selected_id[1] = i;
- count2++;
- }
- }
- }
- break;
+ if (draw_left) {
+ /* Skip drawing the engines we don't have any of and haven't set for replacement */
+ if (p->num_engines[e] == 0 && EngineReplacementForPlayer(GetPlayer(_local_player), e) == INVALID_ENGINE) continue;
+ } else {
+ /* This is for engines we can replace to and they should depend on what we selected to replace from */
+ if (!IsEngineBuildable(e, type, _local_player)) continue; // we need to be able to build the engine
+ if (!EnginesGotCargoInCommon(e, WP(w, replaceveh_d).sel_engine[0])) continue; // the engines needs to be able to carry the same cargo
+ if (e == WP(w, replaceveh_d).sel_engine[0]) continue; // we can't replace an engine into itself (that would be autorenew)
}
- case VEH_Aircraft: {
- for (i = AIRCRAFT_ENGINES_INDEX; i < AIRCRAFT_ENGINES_INDEX + NUM_AIRCRAFT_ENGINES; i++) {
- if (p->num_engines[i] > 0 || EngineHasReplacementForPlayer(p, i)) {
- if (sel[0] == count) selected_id[0] = i;
- count++;
- }
- }
-
- if (selected_id[0] != INVALID_ENGINE) {
- byte subtype = AircraftVehInfo(selected_id[0])->subtype;
-
- for (i = AIRCRAFT_ENGINES_INDEX; i < AIRCRAFT_ENGINES_INDEX + NUM_AIRCRAFT_ENGINES; i++) {
- if (HASBIT(GetEngine(i)->player_avail, _local_player) &&
- (subtype & AIR_CTOL) == (AircraftVehInfo(i)->subtype & AIR_CTOL)) {
- if (sel[1] == count2) selected_id[1] = i;
- count2++;
- }
- }
- }
- break;
- }
+ EngList_Add(list, e);
+ if (e == WP(w, replaceveh_d).sel_engine[i]) selected_engine = e; // The selected engine is still in the list
}
- // sets up the number of items in each list
- SetVScrollCount(w, count);
- SetVScroll2Count(w, count2);
- WP(w,replaceveh_d).sel_engine[0] = selected_id[0];
- WP(w,replaceveh_d).sel_engine[1] = selected_id[1];
-
- WP(w,replaceveh_d).count[0] = count;
- WP(w,replaceveh_d).count[1] = count2;
- return;
+ WP(w, replaceveh_d).sel_engine[i] = selected_engine; // update which engine we selected (the same or none, if it's not in the list anymore)
}
-
-static void DrawEngineArrayInReplaceWindow(Window *w, int x, int y, int x2, int y2, int pos, int pos2,
- int sel1, int sel2, EngineID selected_id1, EngineID selected_id2)
+/** Generate the lists
+ * @param w Window containing the lists
+ */
+static void GenerateLists(Window *w)
{
- int sel[2];
- EngineID selected_id[2];
- const Player *p = GetPlayer(_local_player);
-
- sel[0] = sel1;
- sel[1] = sel2;
-
- selected_id[0] = selected_id1;
- selected_id[1] = selected_id2;
-
- switch (WP(w,replaceveh_d).vehicletype) {
- case VEH_Train: {
- RailType railtype = _railtype_selected_in_replace_gui;
- DrawString(157, w->widget[14].top + 1, _rail_types_list[railtype], 0x10);
- /* draw sorting criteria string */
-
- /* Ensure that custom engines which substituted wagons
- * are sorted correctly.
- * XXX - DO NOT EVER DO THIS EVER AGAIN! GRRR hacking in wagons as
- * engines to get more types.. Stays here until we have our own format
- * then it is exit!!! */
- if (WP(w,replaceveh_d).wagon_btnstate) {
- train_engine_drawing_loop(&x, &y, &pos, &sel[0], &selected_id[0], railtype, w->vscroll.cap, true, false, true, true); // True engines
- train_engine_drawing_loop(&x2, &y2, &pos2, &sel[1], &selected_id[1], railtype, w->vscroll.cap, true, false, false, false); // True engines
- train_engine_drawing_loop(&x2, &y2, &pos2, &sel[1], &selected_id[1], railtype, w->vscroll.cap, false, false, false, false); // Feeble wagons
- } else {
- train_engine_drawing_loop(&x, &y, &pos, &sel[0], &selected_id[0], railtype, w->vscroll.cap, false, true, true, true);
- train_engine_drawing_loop(&x2, &y2, &pos2, &sel[1], &selected_id[1], railtype, w->vscroll.cap, false, true, false, true);
- }
- break;
+ EngineID e = WP(w, replaceveh_d).sel_engine[0];
+
+ if (WP(w, replaceveh_d).update_left == true) {
+ /* We need to rebuild the left list */
+ GenerateReplaceVehList(w, true);
+ SetVScrollCount(w, EngList_Count(&WP(w, replaceveh_d).list[0]));
+ if (WP(w, replaceveh_d).init_lists && WP(w, replaceveh_d).sel_engine[0] == INVALID_ENGINE && EngList_Count(&WP(w, replaceveh_d).list[0]) != 0) {
+ WP(w, replaceveh_d).sel_engine[0] = WP(w, replaceveh_d).list[0][0];
}
+ }
- case VEH_Road: {
- int num = NUM_ROAD_ENGINES;
- const Engine* e = GetEngine(ROAD_ENGINES_INDEX);
- EngineID engine_id = ROAD_ENGINES_INDEX;
-
- do {
- if (p->num_engines[engine_id] > 0 || EngineHasReplacementForPlayer(p, engine_id)) {
- if (IS_INT_INSIDE(--pos, -w->vscroll.cap, 0)) {
- DrawString(x+59, y+2, GetCustomEngineName(engine_id), sel[0]==0 ? 0xC : 0x10);
- DrawRoadVehEngine(x+29, y+6, engine_id, p->num_engines[engine_id] > 0 ? GetEnginePalette(engine_id, _local_player) : PALETTE_CRASH);
- SetDParam(0, p->num_engines[engine_id]);
- DrawStringRightAligned(213, y+5, STR_TINY_BLACK, 0);
- y += 14;
- }
- sel[0]--;
- }
-
- if (selected_id[0] != INVALID_ENGINE) {
- byte cargo = RoadVehInfo(selected_id[0])->cargo_type;
-
- if (RoadVehInfo(engine_id)->cargo_type == cargo && HASBIT(e->player_avail, _local_player)) {
- if (IS_INT_INSIDE(--pos2, -w->vscroll.cap, 0) && RoadVehInfo(engine_id)->cargo_type == cargo) {
- DrawString(x2+59, y2+2, GetCustomEngineName(engine_id), sel[1]==0 ? 0xC : 0x10);
- DrawRoadVehEngine(x2+29, y2+6, engine_id, GetEnginePalette(engine_id, _local_player));
- y2 += 14;
- }
- sel[1]--;
- }
- }
- } while (++engine_id, ++e,--num);
- break;
+ if (WP(w, replaceveh_d).update_right || e != WP(w, replaceveh_d).sel_engine[0]) {
+ /* Either we got a request to rebuild the right list or the left list selected a different engine */
+ if (WP(w, replaceveh_d).sel_engine[0] == INVALID_ENGINE) {
+ /* Always empty the right list when nothing is selected in the left list */
+ EngList_RemoveAll(&WP(w, replaceveh_d).list[1]);
+ WP(w, replaceveh_d).sel_engine[1] = INVALID_ENGINE;
+ } else {
+ GenerateReplaceVehList(w, false);
+ SetVScroll2Count(w, EngList_Count(&WP(w, replaceveh_d).list[1]));
+ if (WP(w, replaceveh_d).init_lists && WP(w, replaceveh_d).sel_engine[1] == INVALID_ENGINE && EngList_Count(&WP(w, replaceveh_d).list[1]) != 0) {
+ WP(w, replaceveh_d).sel_engine[1] = WP(w, replaceveh_d).list[1][0];
+ }
}
-
- case VEH_Ship: {
- int num = NUM_SHIP_ENGINES;
- const Engine* e = GetEngine(SHIP_ENGINES_INDEX);
- EngineID engine_id = SHIP_ENGINES_INDEX;
-
- do {
- if (p->num_engines[engine_id] > 0 || EngineHasReplacementForPlayer(p, engine_id)) {
- if (IS_INT_INSIDE(--pos, -w->vscroll.cap, 0)) {
- DrawString(x+75, y+7, GetCustomEngineName(engine_id), sel[0]==0 ? 0xC : 0x10);
- DrawShipEngine(x+35, y+10, engine_id, p->num_engines[engine_id] > 0 ? GetEnginePalette(engine_id, _local_player) : PALETTE_CRASH);
- SetDParam(0, p->num_engines[engine_id]);
- DrawStringRightAligned(213, y+15, STR_TINY_BLACK, 0);
- y += 24;
- }
- sel[0]--;
- }
-
- if (selected_id[0] != INVALID_ENGINE) {
- CargoID cargo = ShipVehInfo(selected_id[0])->cargo_type;
- bool refittable = ShipVehInfo(selected_id[0])->refittable;
-
- if (HASBIT(e->player_avail, _local_player) && ( cargo == ShipVehInfo(engine_id)->cargo_type || refittable & ShipVehInfo(engine_id)->refittable)) {
- if (IS_INT_INSIDE(--pos2, -w->vscroll.cap, 0)) {
- DrawString(x2+75, y2+7, GetCustomEngineName(engine_id), sel[1]==0 ? 0xC : 0x10);
- DrawShipEngine(x2+35, y2+10, engine_id, GetEnginePalette(engine_id, _local_player));
- y2 += 24;
- }
- sel[1]--;
- }
- }
- } while (++engine_id, ++e, --num);
- break;
- } //end of ship
-
- case VEH_Aircraft: {
- int num = NUM_AIRCRAFT_ENGINES;
- const Engine* e = GetEngine(AIRCRAFT_ENGINES_INDEX);
- EngineID engine_id = AIRCRAFT_ENGINES_INDEX;
-
- do {
- if (p->num_engines[engine_id] > 0 || EngineHasReplacementForPlayer(p, engine_id)) {
- if (sel[0] == 0) selected_id[0] = engine_id;
- if (IS_INT_INSIDE(--pos, -w->vscroll.cap, 0)) {
- DrawString(x+62, y+7, GetCustomEngineName(engine_id), sel[0]==0 ? 0xC : 0x10);
- DrawAircraftEngine(x+29, y+10, engine_id, p->num_engines[engine_id] > 0 ? GetEnginePalette(engine_id, _local_player) : PALETTE_CRASH);
- SetDParam(0, p->num_engines[engine_id]);
- DrawStringRightAligned(213, y+15, STR_TINY_BLACK, 0);
- y += 24;
- }
- sel[0]--;
- }
-
- if (selected_id[0] != INVALID_ENGINE) {
- byte subtype = AircraftVehInfo(selected_id[0])->subtype;
-
- if ((subtype & AIR_CTOL) == (AircraftVehInfo(engine_id)->subtype & AIR_CTOL) &&
- HASBIT(e->player_avail, _local_player)) {
- if (sel[1] == 0) selected_id[1] = engine_id;
- if (IS_INT_INSIDE(--pos2, -w->vscroll.cap, 0)) {
- DrawString(x2+62, y2+7, GetCustomEngineName(engine_id), sel[1]==0 ? 0xC : 0x10);
- DrawAircraftEngine(x2+29, y2+10, engine_id, GetEnginePalette(engine_id, _local_player));
- y2 += 24;
- }
- sel[1]--;
- }
- }
- } while (++engine_id, ++e,--num);
- break;
- } // end of aircraft
}
+ /* Reset the flags about needed updates */
+ WP(w, replaceveh_d).update_left = false;
+ WP(w, replaceveh_d).update_right = false;
+ WP(w, replaceveh_d).init_lists = false;
}
+
+void DrawEngineList(byte type, int x, int y, const EngineList eng_list, uint16 min, uint16 max, EngineID selected_id, bool show_count);
+
static void ReplaceVehicleWndProc(Window *w, WindowEvent *e)
{
+ /* Strings for the pulldown menu */
static const StringID _vehicle_type_names[] = {
STR_019F_TRAIN,
STR_019C_ROAD_VEHICLE,
@@ -364,93 +213,104 @@ static void ReplaceVehicleWndProc(Window *w, WindowEvent *e)
};
switch (e->event) {
+ case WE_CREATE:
+ WP(w, replaceveh_d).wagon_btnstate = true; // start with locomotives (all other vehicles will not read this bool)
+ EngList_Create(&WP(w, replaceveh_d).list[0]);
+ EngList_Create(&WP(w, replaceveh_d).list[1]);
+ WP(w, replaceveh_d).update_left = true;
+ WP(w, replaceveh_d).update_right = true;
+ WP(w, replaceveh_d).init_lists = true;
+ WP(w, replaceveh_d).sel_engine[0] = INVALID_ENGINE;
+ WP(w, replaceveh_d).sel_engine[1] = INVALID_ENGINE;
+ break;
+
case WE_PAINT: {
- Player *p = GetPlayer(_local_player);
- int pos = w->vscroll.pos;
- EngineID selected_id[2] = { INVALID_ENGINE, INVALID_ENGINE };
- int x = 1;
- int y = 15;
- int pos2 = w->vscroll2.pos;
- int x2 = 1 + 228;
- int y2 = 15;
- int sel[2];
- byte i;
- sel[0] = WP(w,replaceveh_d).sel_index[0];
- sel[1] = WP(w,replaceveh_d).sel_index[1];
-
- SetupScrollStuffForReplaceWindow(w);
-
- selected_id[0] = WP(w,replaceveh_d).sel_engine[0];
- selected_id[1] = WP(w,replaceveh_d).sel_engine[1];
-
- // Disable the "Start Replacing" button if:
- // Either list is empty
- // or Both lists have the same vehicle selected
- // or The selected replacement engine has a replacement (to prevent loops)
- // or The right list (new replacement) has the existing replacement vehicle selected
- SetWindowWidgetDisabledState(w, 4,
- selected_id[0] == INVALID_ENGINE ||
- selected_id[1] == INVALID_ENGINE ||
- selected_id[0] == selected_id[1] ||
- EngineReplacementForPlayer(p, selected_id[1]) != INVALID_ENGINE ||
- EngineReplacementForPlayer(p, selected_id[0]) == selected_id[1]);
-
- // Disable the "Stop Replacing" button if:
- // The left list (existing vehicle) is empty
- // or The selected vehicle has no replacement set up
- SetWindowWidgetDisabledState(w, 6,
- selected_id[0] == INVALID_ENGINE ||
- !EngineHasReplacementForPlayer(p, selected_id[0]));
-
- // now the actual drawing of the window itself takes place
- SetDParam(0, _vehicle_type_names[WP(w, replaceveh_d).vehicletype - VEH_Train]);
-
- if (WP(w, replaceveh_d).vehicletype == VEH_Train) {
- // set on/off for renew_keep_length
- SetDParam(1, p->renew_keep_length ? STR_CONFIG_PATCHES_ON : STR_CONFIG_PATCHES_OFF);
-
- // set wagon/engine button
- SetDParam(2, WP(w, replaceveh_d).wagon_btnstate ? STR_ENGINES : STR_WAGONS);
- }
+ if (WP(w, replaceveh_d).update_left || WP(w, replaceveh_d).update_right) GenerateLists(w);
+
+ Player *p = GetPlayer(_local_player);
+ EngineID selected_id[2];
+
+ selected_id[0] = WP(w, replaceveh_d).sel_engine[0];
+ selected_id[1] = WP(w, replaceveh_d).sel_engine[1];
+
+ /* Disable the "Start Replacing" button if:
+ * Either list is empty
+ * or The selected replacement engine has a replacement (to prevent loops)
+ * or The right list (new replacement) has the existing replacement vehicle selected */
+ SetWindowWidgetDisabledState(w, 4,
+ selected_id[0] == INVALID_ENGINE ||
+ selected_id[1] == INVALID_ENGINE ||
+ EngineReplacementForPlayer(p, selected_id[1]) != INVALID_ENGINE ||
+ EngineReplacementForPlayer(p, selected_id[0]) == selected_id[1]);
+
+ /* Disable the "Stop Replacing" button if:
+ * The left list (existing vehicle) is empty
+ * or The selected vehicle has no replacement set up */
+ SetWindowWidgetDisabledState(w, 6,
+ selected_id[0] == INVALID_ENGINE ||
+ !EngineHasReplacementForPlayer(p, selected_id[0]));
+
+ /* now the actual drawing of the window itself takes place */
+ SetDParam(0, _vehicle_type_names[VehTypeToIndex(w->window_number)]);
+
+ if (w->window_number == VEH_Train) {
+ /* set on/off for renew_keep_length */
+ SetDParam(1, p->renew_keep_length ? STR_CONFIG_PATCHES_ON : STR_CONFIG_PATCHES_OFF);
+
+ /* set wagon/engine button */
+ SetDParam(2, WP(w, replaceveh_d).wagon_btnstate ? STR_ENGINES : STR_WAGONS);
+ }
- DrawWindowWidgets(w);
+ w->widget[13].color = _player_colors[_local_player]; // sets the colour of that art thing
+ w->widget[16].color = _player_colors[_local_player]; // sets the colour of that art thing
- // sets up the string for the vehicle that is being replaced to
- if (selected_id[0] != INVALID_ENGINE) {
- if (!EngineHasReplacementForPlayer(p, selected_id[0])) {
- SetDParam(0, STR_NOT_REPLACING);
- } else {
- SetDParam(0, GetCustomEngineName(EngineReplacementForPlayer(p, selected_id[0])));
- }
+ DrawWindowWidgets(w);
+
+
+ if (w->window_number == VEH_Train) {
+ /* Draw the selected railtype in the pulldown menu */
+ RailType railtype = _railtype_selected_in_replace_gui;
+ DrawString(157, w->widget[14].top + 1, _rail_types_list[railtype], 0x10);
+ }
+
+ /* sets up the string for the vehicle that is being replaced to */
+ if (selected_id[0] != INVALID_ENGINE) {
+ if (!EngineHasReplacementForPlayer(p, selected_id[0])) {
+ SetDParam(0, STR_NOT_REPLACING);
} else {
- SetDParam(0, STR_NOT_REPLACING_VEHICLE_SELECTED);
+ SetDParam(0, GetCustomEngineName(EngineReplacementForPlayer(p, selected_id[0])));
}
+ } else {
+ SetDParam(0, STR_NOT_REPLACING_VEHICLE_SELECTED);
+ }
- DrawString(145, w->widget[5].top + 1, STR_02BD, 0x10);
+ DrawString(145, w->widget[5].top + 1, STR_02BD, 0x10);
- /* now we draw the two arrays according to what we just counted */
- DrawEngineArrayInReplaceWindow(w, x, y, x2, y2, pos, pos2, sel[0], sel[1], selected_id[0], selected_id[1]);
+ /* Draw the lists */
+ for(byte i = 0; i < 2; i++) {
+ uint16 x = i == 0 ? 2 : 230; // at what X offset
+ EngineList list = WP(w, replaceveh_d).list[i]; // which list to draw
+ EngineID start = i == 0 ? w->vscroll.pos : w->vscroll2.pos; // what is the offset for the start (scrolling)
+ EngineID end = min((i == 0 ? w->vscroll.cap : w->vscroll2.cap) + start, EngList_Count(&list));
- WP(w,replaceveh_d).sel_engine[0] = selected_id[0];
- WP(w,replaceveh_d).sel_engine[1] = selected_id[1];
- /* now we draw the info about the vehicles we selected */
- for (i = 0 ; i < 2 ; i++) {
- if (selected_id[i] != INVALID_ENGINE) {
- const Widget *wi = &w->widget[i == 0 ? 3 : 11];
- DrawVehiclePurchaseInfo(wi->left + 2 , wi->top + 1, wi->right - wi->left - 2, selected_id[i]);
- }
+ /* Do the actual drawing */
+ DrawEngineList(w->window_number, x, 15, list, start, end, WP(w, replaceveh_d).sel_engine[i], i == 0);
+
+ /* Also draw the details if an engine is selected */
+ if (WP(w, replaceveh_d).sel_engine[i] != INVALID_ENGINE) {
+ const Widget *wi = &w->widget[i == 0 ? 3 : 11];
+ DrawVehiclePurchaseInfo(wi->left + 2, wi->top + 1, wi->right - wi->left - 2, WP(w, replaceveh_d).sel_engine[i]);
}
- } break; // end of paint
+ }
- case WE_CLICK: {
- // these 3 variables is used if any of the lists is clicked
- uint16 click_scroll_pos = w->vscroll2.pos;
- uint16 click_scroll_cap = w->vscroll2.cap;
- byte click_side = 1;
+ } break; // end of paint
+ case WE_CLICK: {
switch (e->we.click.widget) {
case 12:
WP(w, replaceveh_d).wagon_btnstate = !(WP(w, replaceveh_d).wagon_btnstate);
+ WP(w, replaceveh_d).update_left = true;
+ WP(w, replaceveh_d).init_lists = true;
SetWindowDirty(w);
break;
@@ -467,42 +327,51 @@ static void ReplaceVehicleWndProc(Window *w, WindowEvent *e)
EngineID veh_from = WP(w, replaceveh_d).sel_engine[0];
EngineID veh_to = WP(w, replaceveh_d).sel_engine[1];
DoCommandP(0, 3, veh_from + (veh_to << 16), NULL, CMD_SET_AUTOREPLACE);
- break;
- }
+ } break;
case 6: { /* Stop replacing */
EngineID veh_from = WP(w, replaceveh_d).sel_engine[0];
DoCommandP(0, 3, veh_from + (INVALID_ENGINE << 16), NULL, CMD_SET_AUTOREPLACE);
- break;
- }
+ } break;
case 7:
- // sets up that the left one was clicked. The default values are for the right one (9)
- // this way, the code for 9 handles both sides
- click_scroll_pos = w->vscroll.pos;
- click_scroll_cap = w->vscroll.cap;
- click_side = 0;
- /* FALL THROUGH */
-
case 9: {
uint i = (e->we.click.pt.y - 14) / w->resize.step_height;
+ uint16 click_scroll_pos = e->we.click.widget == 7 ? w->vscroll.pos : w->vscroll2.pos;
+ uint16 click_scroll_cap = e->we.click.widget == 7 ? w->vscroll.cap : w->vscroll2.cap;
+ byte click_side = e->we.click.widget == 7 ? 0 : 1;
+ uint16 engine_count = EngList_Count(&WP(w, replaceveh_d).list[click_side]);
+
if (i < click_scroll_cap) {
- WP(w,replaceveh_d).sel_index[click_side] = i + click_scroll_pos;
+ i += click_scroll_pos;
+ EngineID e = engine_count > i ? WP(w, replaceveh_d).list[click_side][i] : INVALID_ENGINE;
+ if (e == WP(w, replaceveh_d).sel_engine[click_side]) break; // we clicked the one we already selected
+ WP(w, replaceveh_d).sel_engine[click_side] = e;
+ if (click_side == 0) {
+ WP(w, replaceveh_d).update_right = true;
+ WP(w, replaceveh_d).init_lists = true;
+ }
SetWindowDirty(w);
- }
+ }
break;
- }
+ }
}
break;
}
- case WE_DROPDOWN_SELECT: /* we have selected a dropdown item in the list */
- _railtype_selected_in_replace_gui = (RailType)e->we.dropdown.index;
+ case WE_DROPDOWN_SELECT: { /* we have selected a dropdown item in the list */
+ RailType temp = (RailType)e->we.dropdown.index;
+ if (temp == _railtype_selected_in_replace_gui) break; // we didn't select a new one. No need to change anything
+ _railtype_selected_in_replace_gui = temp;
/* Reset scrollbar positions */
w->vscroll.pos = 0;
w->vscroll2.pos = 0;
+ /* Rebuild the lists */
+ WP(w, replaceveh_d).update_left = true;
+ WP(w, replaceveh_d).update_right = true;
+ WP(w, replaceveh_d).init_lists = true;
SetWindowDirty(w);
- break;
+ } break;
case WE_RESIZE:
w->vscroll.cap += e->we.sizing.diff.y / (int)w->resize.step_height;
@@ -511,6 +380,17 @@ static void ReplaceVehicleWndProc(Window *w, WindowEvent *e)
w->widget[7].data = (w->vscroll.cap << 8) + 1;
w->widget[9].data = (w->vscroll2.cap << 8) + 1;
break;
+
+ case WE_INVALIDATE_DATA:
+ if (_rebuild_left_list) WP(w, replaceveh_d).update_left = true;
+ if (_rebuild_right_list) WP(w, replaceveh_d).update_right = true;
+ SetWindowDirty(w);
+ break;
+
+ case WE_DESTROY:
+ EngList_RemoveAll(&WP(w, replaceveh_d).list[0]);
+ EngList_RemoveAll(&WP(w, replaceveh_d).list[1]);
+ break;
}
}
@@ -626,6 +506,5 @@ void ShowReplaceVehicleWindow(byte vehicletype)
}
w->caption_color = _local_player;
- WP(w, replaceveh_d).vehicletype = vehicletype;
w->vscroll2.cap = w->vscroll.cap; // these two are always the same
}
diff --git a/src/build_vehicle_gui.cpp b/src/build_vehicle_gui.cpp
index 38b66bef5..e67fc6dd5 100644
--- a/src/build_vehicle_gui.cpp
+++ b/src/build_vehicle_gui.cpp
@@ -733,12 +733,14 @@ static void DrawVehicleEngine(byte type, int x, int y, EngineID engine, SpriteID
* @param min where to start in the list
* @param max where in the list to end
* @param selected_id what engine to highlight as selected, if any
+ * @param show_count Display the number of vehicles (used by autoreplace)
*/
-static void DrawEngineList(byte type, int x, int y, const EngineList eng_list, uint16 min, uint16 max, EngineID selected_id)
+void DrawEngineList(byte type, int x, int y, const EngineList eng_list, uint16 min, uint16 max, EngineID selected_id, bool show_count)
{
byte step_size = GetVehicleListHeight(type);
byte x_offset = 0;
byte y_offset = 0;
+ Player *p = GetPlayer(_local_player);
assert(max <= EngList_Count(&eng_list));
@@ -771,7 +773,11 @@ static void DrawEngineList(byte type, int x, int y, const EngineList eng_list, u
const EngineID engine = eng_list[min];
DrawString(x + x_offset, y, GetCustomEngineName(engine), engine == selected_id ? 0xC : 0x10);
- DrawVehicleEngine(type, x, y + y_offset, engine, GetEnginePalette(engine, _local_player));
+ DrawVehicleEngine(type, x, y + y_offset, engine, (show_count && p->num_engines[engine] == 0) ? PALETTE_CRASH : GetEnginePalette(engine, _local_player));
+ if (show_count) {
+ SetDParam(0, p->num_engines[engine]);
+ DrawStringRightAligned(213, y + (GetVehicleListHeight(type) == 14 ? 3 : 8), STR_TINY_BLACK, 0);
+ }
}
}
@@ -786,7 +792,7 @@ static void DrawBuildVehicleWindow(Window *w)
SetDParam(0, bv->filter.railtype + STR_881C_NEW_RAIL_VEHICLES); // This should only affect rail vehicles
DrawWindowWidgets(w);
- DrawEngineList(bv->vehicle_type, 2, 27, bv->eng_list, w->vscroll.pos, max, bv->sel_engine);
+ DrawEngineList(bv->vehicle_type, 2, 27, bv->eng_list, w->vscroll.pos, max, bv->sel_engine, false);
if (bv->sel_engine != INVALID_ENGINE) {
const Widget *wi = &w->widget[BUILD_VEHICLE_WIDGET_PANEL];
diff --git a/src/engine.cpp b/src/engine.cpp
index c23909f50..34f8de113 100644
--- a/src/engine.cpp
+++ b/src/engine.cpp
@@ -101,9 +101,11 @@ static void CalcEngineReliability(Engine *e)
* We will now completely retire this design */
e->player_avail = 0;
e->reliability = e->reliability_final;
- InvalidateWindowClassesData(WC_BUILD_VEHICLE); // Kick this engine out of the lists
+ /* Kick this engine out of the lists */
+ AddRemoveEngineFromAutoreplaceAndBuildWindows(e->type);
}
InvalidateWindowClasses(WC_BUILD_VEHICLE); // Update to show the new reliability
+ InvalidateWindowClasses(WC_REPLACE_VEHICLE);
}
void AddTypeToEngines(void)
@@ -195,8 +197,7 @@ static void AcceptEnginePreview(EngineID eid, PlayerID player)
e->preview_player = INVALID_PLAYER;
if (player == _local_player) {
- InvalidateWindowClassesData(WC_BUILD_VEHICLE);
- InvalidateWindowClasses(WC_REPLACE_VEHICLE);
+ AddRemoveEngineFromAutoreplaceAndBuildWindows(e->type);
}
}
@@ -319,8 +320,7 @@ static void NewVehicleAvailable(Engine *e)
}
e->flags = (e->flags & ~ENGINE_INTRODUCING) | ENGINE_AVAILABLE;
- InvalidateWindowClassesData(WC_BUILD_VEHICLE);
- InvalidateWindowClasses(WC_REPLACE_VEHICLE);
+ AddRemoveEngineFromAutoreplaceAndBuildWindows(e->type);
// Now available for all players
e->player_avail = (byte)-1;
diff --git a/src/engine.h b/src/engine.h
index af74bcbab..df24ee8d5 100644
--- a/src/engine.h
+++ b/src/engine.h
@@ -8,6 +8,7 @@
#include "oldpool.h"
#include "rail.h"
#include "sound.h"
+#include "vehicle.h"
enum RailVehicleTypes {
RAILVEH_SINGLEHEAD,
@@ -157,8 +158,29 @@ enum {
SHIP_ENGINES_INDEX = NUM_TRAIN_ENGINES + NUM_ROAD_ENGINES,
ROAD_ENGINES_INDEX = NUM_TRAIN_ENGINES,
};
+
+static inline EngineID GetFirstEngineOfType(byte type)
+{
+ const EngineID start[] = {0, ROAD_ENGINES_INDEX, SHIP_ENGINES_INDEX, AIRCRAFT_ENGINES_INDEX};
+
+ return start[VehTypeToIndex(type)];
+}
+
+static inline EngineID GetLastEngineOfType(byte type)
+{
+ const EngineID end[] = {
+ NUM_TRAIN_ENGINES,
+ ROAD_ENGINES_INDEX + NUM_ROAD_ENGINES,
+ SHIP_ENGINES_INDEX + NUM_SHIP_ENGINES,
+ AIRCRAFT_ENGINES_INDEX + NUM_AIRCRAFT_ENGINES};
+
+ return end[VehTypeToIndex(type)];
+}
+
VARDEF Engine _engines[TOTAL_NUM_ENGINES];
#define FOR_ALL_ENGINES(e) for (e = _engines; e != endof(_engines); e++)
+#define FOR_ALL_ENGINEIDS_OF_TYPE(e, type) for (e = GetFirstEngineOfType(type); e != GetLastEngineOfType(type); e++)
+
static inline Engine* GetEngine(EngineID i)
{
@@ -299,6 +321,11 @@ int32 AddEngineReplacement(EngineRenewList* erl, EngineID old_engine, EngineID n
*/
int32 RemoveEngineReplacement(EngineRenewList* erl, EngineID engine, uint32 flags);
+/** When an engine is made buildable or is removed from being buildable, add/remove it from the build/autoreplace lists
+ * @param type The type of engine
+ */
+void AddRemoveEngineFromAutoreplaceAndBuildWindows(byte type);
+
/* Engine list manipulators - current implementation is only C wrapper of CBlobT<EngineID> class (helpers.cpp) */
void EngList_Create(EngineList *el); ///< Creates engine list
void EngList_Destroy(EngineList *el); ///< Deallocate and destroy engine list
diff --git a/src/players.cpp b/src/players.cpp
index 02d30fee8..615db5528 100644
--- a/src/players.cpp
+++ b/src/players.cpp
@@ -776,7 +776,7 @@ int32 CmdSetAutoReplace(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
cost = RemoveEngineReplacementForPlayer(p, old_engine_type, flags);
}
- if (IsLocalPlayer()) InvalidateWindow(WC_REPLACE_VEHICLE, GetEngine(old_engine_type)->type);
+ if (IsLocalPlayer()) InvalidateAutoreplaceWindow(old_engine_type);
return cost;
}
diff --git a/src/roadveh_cmd.cpp b/src/roadveh_cmd.cpp
index 3edb3ca9d..8eb454f62 100644
--- a/src/roadveh_cmd.cpp
+++ b/src/roadveh_cmd.cpp
@@ -192,13 +192,14 @@ int32 CmdBuildRoadVeh(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
v->random_bits = VehicleRandomBits();
VehiclePositionChanged(v);
- GetPlayer(_current_player)->num_engines[p1]++;
InvalidateWindowData(WC_VEHICLE_DEPOT, v->tile);
RebuildVehicleLists();
InvalidateWindow(WC_COMPANY, v->owner);
if (IsLocalPlayer())
- InvalidateWindow(WC_REPLACE_VEHICLE, VEH_Road); // updates the replace Road window
+ InvalidateAutoreplaceWindow(VEH_Road); // updates the replace Road window
+
+ GetPlayer(_current_player)->num_engines[p1]++;
}
return cost;
@@ -284,7 +285,6 @@ int32 CmdSellRoadVeh(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
DeleteWindowById(WC_VEHICLE_VIEW, v->index);
DeleteDepotHighlightOfVehicle(v);
DeleteVehicle(v);
- if (IsLocalPlayer()) InvalidateWindow(WC_REPLACE_VEHICLE, VEH_Road);
}
return -(int32)v->value;
diff --git a/src/ship_cmd.cpp b/src/ship_cmd.cpp
index 03c2efbb4..e95a660ec 100644
--- a/src/ship_cmd.cpp
+++ b/src/ship_cmd.cpp
@@ -888,13 +888,14 @@ int32 CmdBuildShip(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
v->random_bits = VehicleRandomBits();
VehiclePositionChanged(v);
- GetPlayer(_current_player)->num_engines[p1]++;
InvalidateWindowData(WC_VEHICLE_DEPOT, v->tile);
RebuildVehicleLists();
InvalidateWindow(WC_COMPANY, v->owner);
if (IsLocalPlayer())
- InvalidateWindow(WC_REPLACE_VEHICLE, VEH_Ship); // updates the replace Ship window
+ InvalidateAutoreplaceWindow(VEH_Ship); // updates the replace Ship window
+
+ GetPlayer(_current_player)->num_engines[p1]++;
}
return value;
@@ -928,8 +929,6 @@ int32 CmdSellShip(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
DeleteWindowById(WC_VEHICLE_VIEW, v->index);
DeleteDepotHighlightOfVehicle(v);
DeleteVehicle(v);
- if (IsLocalPlayer())
- InvalidateWindow(WC_REPLACE_VEHICLE, VEH_Ship); // updates the replace Ship window
}
return -(int32)v->value;
diff --git a/src/train_cmd.cpp b/src/train_cmd.cpp
index 3eccd58ec..37e42cde0 100644
--- a/src/train_cmd.cpp
+++ b/src/train_cmd.cpp
@@ -670,12 +670,12 @@ static int32 CmdBuildRailWagon(EngineID engine, TileIndex tile, uint32 flags)
VehiclePositionChanged(v);
TrainConsistChanged(GetFirstVehicleInChain(v));
- GetPlayer(_current_player)->num_engines[engine]++;
InvalidateWindow(WC_VEHICLE_DEPOT, v->tile);
if (IsLocalPlayer()) {
- InvalidateWindow(WC_REPLACE_VEHICLE, VEH_Train); // updates the replace Train window
+ InvalidateAutoreplaceWindow(VEH_Train); // updates the replace Train window
}
+ GetPlayer(_current_player)->num_engines[engine]++;
}
}
@@ -853,13 +853,13 @@ int32 CmdBuildRailVehicle(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
NormalizeTrainVehInDepot(v);
}
- GetPlayer(_current_player)->num_engines[p1]++;
InvalidateWindowData(WC_VEHICLE_DEPOT, v->tile);
RebuildVehicleLists();
InvalidateWindow(WC_COMPANY, v->owner);
- if (IsLocalPlayer()) {
- InvalidateWindow(WC_REPLACE_VEHICLE, VEH_Train); // updates the replace Train window
- }
+ if (IsLocalPlayer())
+ InvalidateAutoreplaceWindow(VEH_Train); // updates the replace Train window
+
+ GetPlayer(_current_player)->num_engines[p1]++;
}
}
@@ -1356,9 +1356,6 @@ int32 CmdSellRailWagon(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
if (v == first && IsFrontEngine(first)) {
DeleteWindowById(WC_VEHICLE_VIEW, first->index);
}
- if (IsLocalPlayer() && (p1 == 1 || RailVehInfo(v->engine_type)->railveh_type != RAILVEH_WAGON)) {
- InvalidateWindow(WC_REPLACE_VEHICLE, VEH_Train);
- }
InvalidateWindow(WC_VEHICLE_DEPOT, first->tile);
RebuildVehicleLists();
}
@@ -3322,7 +3319,6 @@ static void HandleCrashedTrain(Vehicle *v)
if (state >= 4440 && !(v->tick_counter&0x1F)) {
DeleteLastWagon(v);
- InvalidateWindow(WC_REPLACE_VEHICLE, VEH_Train);
}
}
diff --git a/src/vehicle.cpp b/src/vehicle.cpp
index 94fe2660a..dc06601c2 100644
--- a/src/vehicle.cpp
+++ b/src/vehicle.cpp
@@ -560,7 +560,10 @@ bool IsEngineCountable(const Vehicle *v)
void DestroyVehicle(Vehicle *v)
{
- if (IsEngineCountable(v)) GetPlayer(v->owner)->num_engines[v->engine_type]--;
+ if (IsEngineCountable(v)) {
+ GetPlayer(v->owner)->num_engines[v->engine_type]--;
+ if (v->owner == _local_player) InvalidateAutoreplaceWindow(v->engine_type);
+ }
DeleteVehicleNews(v->index, INVALID_STRING_ID);
diff --git a/src/vehicle.h b/src/vehicle.h
index 780663725..f1500a663 100644
--- a/src/vehicle.h
+++ b/src/vehicle.h
@@ -341,6 +341,8 @@ int32 SendAllVehiclesToDepot(byte type, uint32 flags, bool service, PlayerID own
bool IsVehicleInDepot(const Vehicle *v);
void VehicleEnterDepot(Vehicle *v);
+void InvalidateAutoreplaceWindow(EngineID e);
+
/* Flags to add to p2 for goto depot commands */
/* Note: bits 8-10 are used for VLW flags */
enum {
diff --git a/src/window.h b/src/window.h
index 90600ad44..aa1c3b1c0 100644
--- a/src/window.h
+++ b/src/window.h
@@ -334,11 +334,14 @@ typedef struct {
assert_compile(WINDOW_CUSTOM_SIZE >= sizeof(buildvehicle_d));
typedef struct {
- byte vehicletype;
byte sel_index[2];
EngineID sel_engine[2];
uint16 count[2];
bool wagon_btnstate; // true means engine is selected
+ EngineList list[2];
+ bool update_left;
+ bool update_right;
+ bool init_lists;
} replaceveh_d;
assert_compile(WINDOW_CUSTOM_SIZE >= sizeof(replaceveh_d));