train_cmd.cpp

Go to the documentation of this file.
00001 /* $Id: train_cmd.cpp 18574 2009-12-20 16:19:47Z rubidium $ */
00002 
00003 /*
00004  * This file is part of OpenTTD.
00005  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
00006  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00007  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
00008  */
00009 
00012 #include "stdafx.h"
00013 #include "gui.h"
00014 #include "articulated_vehicles.h"
00015 #include "command_func.h"
00016 #include "pathfinder/npf/npf_func.h"
00017 #include "pathfinder/yapf/yapf.hpp"
00018 #include "pathfinder/follow_track.hpp"
00019 #include "openttd.h"
00020 #include "news_func.h"
00021 #include "company_func.h"
00022 #include "vehicle_gui.h"
00023 #include "newgrf_engine.h"
00024 #include "newgrf_sound.h"
00025 #include "newgrf_text.h"
00026 #include "group.h"
00027 #include "table/sprites.h"
00028 #include "strings_func.h"
00029 #include "functions.h"
00030 #include "window_func.h"
00031 #include "vehicle_func.h"
00032 #include "sound_func.h"
00033 #include "autoreplace_gui.h"
00034 #include "gfx_func.h"
00035 #include "ai/ai.hpp"
00036 #include "newgrf_station.h"
00037 #include "effectvehicle_func.h"
00038 #include "gamelog.h"
00039 #include "network/network.h"
00040 #include "spritecache.h"
00041 
00042 #include "table/strings.h"
00043 #include "table/train_cmd.h"
00044 
00045 static Track ChooseTrainTrack(Train *v, TileIndex tile, DiagDirection enterdir, TrackBits tracks, bool force_res, bool *got_reservation, bool mark_stuck);
00046 static bool TrainCheckIfLineEnds(Train *v);
00047 static void TrainController(Train *v, Vehicle *nomove);
00048 static TileIndex TrainApproachingCrossingTile(const Train *v);
00049 static void CheckIfTrainNeedsService(Train *v);
00050 static void CheckNextTrainTile(Train *v);
00051 
00052 static const byte _vehicle_initial_x_fract[4] = {10, 8, 4,  8};
00053 static const byte _vehicle_initial_y_fract[4] = { 8, 4, 8, 10};
00054 
00055 
00063 static inline DiagDirection TrainExitDir(Direction direction, TrackBits track)
00064 {
00065   static const TrackBits state_dir_table[DIAGDIR_END] = { TRACK_BIT_RIGHT, TRACK_BIT_LOWER, TRACK_BIT_LEFT, TRACK_BIT_UPPER };
00066 
00067   DiagDirection diagdir = DirToDiagDir(direction);
00068 
00069   /* Determine the diagonal direction in which we will exit this tile */
00070   if (!HasBit(direction, 0) && track != state_dir_table[diagdir]) {
00071     diagdir = ChangeDiagDir(diagdir, DIAGDIRDIFF_90LEFT);
00072   }
00073 
00074   return diagdir;
00075 }
00076 
00077 
00082 byte FreightWagonMult(CargoID cargo)
00083 {
00084   if (!CargoSpec::Get(cargo)->is_freight) return 1;
00085   return _settings_game.vehicle.freight_trains;
00086 }
00087 
00088 
00093 void TrainPowerChanged(Train *v)
00094 {
00095   uint32 total_power = 0;
00096   uint32 max_te = 0;
00097 
00098   for (const Train *u = v; u != NULL; u = u->Next()) {
00099     RailType railtype = GetRailType(u->tile);
00100 
00101     /* Power is not added for articulated parts */
00102     if (!u->IsArticulatedPart()) {
00103       bool engine_has_power = HasPowerOnRail(u->railtype, railtype);
00104 
00105       const RailVehicleInfo *rvi_u = RailVehInfo(u->engine_type);
00106 
00107       if (engine_has_power) {
00108         uint16 power = GetVehicleProperty(u, PROP_TRAIN_POWER, rvi_u->power);
00109         if (power != 0) {
00110           /* Halve power for multiheaded parts */
00111           if (u->IsMultiheaded()) power /= 2;
00112 
00113           total_power += power;
00114           /* Tractive effort in (tonnes * 1000 * 10 =) N */
00115           max_te += (u->tcache.cached_veh_weight * 10000 * GetVehicleProperty(u, PROP_TRAIN_TRACTIVE_EFFORT, rvi_u->tractive_effort)) / 256;
00116         }
00117       }
00118     }
00119 
00120     if (HasBit(u->flags, VRF_POWEREDWAGON) && HasPowerOnRail(v->railtype, railtype)) {
00121       total_power += RailVehInfo(u->tcache.first_engine)->pow_wag_power;
00122     }
00123   }
00124 
00125   if (v->tcache.cached_power != total_power || v->tcache.cached_max_te != max_te) {
00126     /* If it has no power (no catenary), stop the train */
00127     if (total_power == 0) v->vehstatus |= VS_STOPPED;
00128 
00129     v->tcache.cached_power = total_power;
00130     v->tcache.cached_max_te = max_te;
00131     SetWindowDirty(WC_VEHICLE_DETAILS, v->index);
00132     SetWindowWidgetDirty(WC_VEHICLE_VIEW, v->index, VVW_WIDGET_START_STOP_VEH);
00133   }
00134 }
00135 
00136 
00142 static void TrainCargoChanged(Train *v)
00143 {
00144   uint32 weight = 0;
00145 
00146   for (Train *u = v; u != NULL; u = u->Next()) {
00147     uint32 vweight = CargoSpec::Get(u->cargo_type)->weight * u->cargo.Count() * FreightWagonMult(u->cargo_type) / 16;
00148 
00149     /* Vehicle weight is not added for articulated parts. */
00150     if (!u->IsArticulatedPart()) {
00151       /* vehicle weight is the sum of the weight of the vehicle and the weight of its cargo */
00152       vweight += GetVehicleProperty(u, PROP_TRAIN_WEIGHT, RailVehInfo(u->engine_type)->weight);
00153     }
00154 
00155     /* powered wagons have extra weight added */
00156     if (HasBit(u->flags, VRF_POWEREDWAGON)) {
00157       vweight += RailVehInfo(u->tcache.first_engine)->pow_wag_weight;
00158     }
00159 
00160     /* consist weight is the sum of the weight of all vehicles in the consist */
00161     weight += vweight;
00162 
00163     /* store vehicle weight in cache */
00164     u->tcache.cached_veh_weight = vweight;
00165   }
00166 
00167   /* store consist weight in cache */
00168   v->tcache.cached_weight = weight;
00169 
00170   /* Now update train power (tractive effort is dependent on weight) */
00171   TrainPowerChanged(v);
00172 }
00173 
00174 
00179 static void RailVehicleLengthChanged(const Train *u)
00180 {
00181   /* show a warning once for each engine in whole game and once for each GRF after each game load */
00182   const Engine *engine = Engine::Get(u->engine_type);
00183   uint32 grfid = engine->grffile->grfid;
00184   GRFConfig *grfconfig = GetGRFConfig(grfid);
00185   if (GamelogGRFBugReverse(grfid, engine->internal_id) || !HasBit(grfconfig->grf_bugs, GBUG_VEH_LENGTH)) {
00186     ShowNewGrfVehicleError(u->engine_type, STR_NEWGRF_BROKEN, STR_NEWGRF_BROKEN_VEHICLE_LENGTH, GBUG_VEH_LENGTH, true);
00187   }
00188 }
00189 
00191 void CheckTrainsLengths()
00192 {
00193   const Train *v;
00194 
00195   FOR_ALL_TRAINS(v) {
00196     if (v->First() == v && !(v->vehstatus & VS_CRASHED)) {
00197       for (const Train *u = v, *w = v->Next(); w != NULL; u = w, w = w->Next()) {
00198         if (u->track != TRACK_BIT_DEPOT) {
00199           if ((w->track != TRACK_BIT_DEPOT &&
00200               max(abs(u->x_pos - w->x_pos), abs(u->y_pos - w->y_pos)) != u->tcache.cached_veh_length) ||
00201               (w->track == TRACK_BIT_DEPOT && TicksToLeaveDepot(u) <= 0)) {
00202             SetDParam(0, v->index);
00203             SetDParam(1, v->owner);
00204             ShowErrorMessage(STR_BROKEN_VEHICLE_LENGTH, INVALID_STRING_ID, 0, 0, true);
00205 
00206             if (!_networking) DoCommandP(0, PM_PAUSED_ERROR, 1, CMD_PAUSE);
00207           }
00208         }
00209       }
00210     }
00211   }
00212 }
00213 
00221 void TrainConsistChanged(Train *v, bool same_length)
00222 {
00223   uint16 max_speed = UINT16_MAX;
00224 
00225   assert(v->IsFrontEngine() || v->IsFreeWagon());
00226 
00227   const RailVehicleInfo *rvi_v = RailVehInfo(v->engine_type);
00228   EngineID first_engine = v->IsFrontEngine() ? v->engine_type : INVALID_ENGINE;
00229   v->tcache.cached_total_length = 0;
00230   v->compatible_railtypes = RAILTYPES_NONE;
00231 
00232   bool train_can_tilt = true;
00233 
00234   for (Train *u = v; u != NULL; u = u->Next()) {
00235     const RailVehicleInfo *rvi_u = RailVehInfo(u->engine_type);
00236 
00237     /* Check the v->first cache. */
00238     assert(u->First() == v);
00239 
00240     /* update the 'first engine' */
00241     u->tcache.first_engine = v == u ? INVALID_ENGINE : first_engine;
00242     u->railtype = rvi_u->railtype;
00243 
00244     if (u->IsEngine()) first_engine = u->engine_type;
00245 
00246     /* Set user defined data to its default value */
00247     u->tcache.user_def_data = rvi_u->user_def_data;
00248     v->InvalidateNewGRFCache();
00249     u->InvalidateNewGRFCache();
00250   }
00251 
00252   for (Train *u = v; u != NULL; u = u->Next()) {
00253     /* Update user defined data (must be done before other properties) */
00254     u->tcache.user_def_data = GetVehicleProperty(u, PROP_TRAIN_USER_DATA, u->tcache.user_def_data);
00255     v->InvalidateNewGRFCache();
00256     u->InvalidateNewGRFCache();
00257   }
00258 
00259   for (Train *u = v; u != NULL; u = u->Next()) {
00260     const Engine *e_u = Engine::Get(u->engine_type);
00261     const RailVehicleInfo *rvi_u = &e_u->u.rail;
00262 
00263     if (!HasBit(e_u->info.misc_flags, EF_RAIL_TILTS)) train_can_tilt = false;
00264 
00265     /* Cache wagon override sprite group. NULL is returned if there is none */
00266     u->tcache.cached_override = GetWagonOverrideSpriteSet(u->engine_type, u->cargo_type, u->tcache.first_engine);
00267 
00268     /* Reset colour map */
00269     u->colourmap = PAL_NONE;
00270 
00271     if (rvi_u->visual_effect != 0) {
00272       u->tcache.cached_vis_effect = rvi_u->visual_effect;
00273     } else {
00274       if (u->IsWagon() || u->IsArticulatedPart()) {
00275         /* Wagons and articulated parts have no effect by default */
00276         u->tcache.cached_vis_effect = 0x40;
00277       } else if (rvi_u->engclass == 0) {
00278         /* Steam is offset by -4 units */
00279         u->tcache.cached_vis_effect = 4;
00280       } else {
00281         /* Diesel fumes and sparks come from the centre */
00282         u->tcache.cached_vis_effect = 8;
00283       }
00284     }
00285 
00286     /* Check powered wagon / visual effect callback */
00287     if (HasBit(e_u->info.callback_mask, CBM_TRAIN_WAGON_POWER)) {
00288       uint16 callback = GetVehicleCallback(CBID_TRAIN_WAGON_POWER, 0, 0, u->engine_type, u);
00289 
00290       if (callback != CALLBACK_FAILED) u->tcache.cached_vis_effect = GB(callback, 0, 8);
00291     }
00292 
00293     if (rvi_v->pow_wag_power != 0 && rvi_u->railveh_type == RAILVEH_WAGON &&
00294       UsesWagonOverride(u) && !HasBit(u->tcache.cached_vis_effect, 7)) {
00295       /* wagon is powered */
00296       SetBit(u->flags, VRF_POWEREDWAGON); // cache 'powered' status
00297     } else {
00298       ClrBit(u->flags, VRF_POWEREDWAGON);
00299     }
00300 
00301     if (!u->IsArticulatedPart()) {
00302       /* Do not count powered wagons for the compatible railtypes, as wagons always
00303          have railtype normal */
00304       if (rvi_u->power > 0) {
00305         v->compatible_railtypes |= GetRailTypeInfo(u->railtype)->powered_railtypes;
00306       }
00307 
00308       /* Some electric engines can be allowed to run on normal rail. It happens to all
00309        * existing electric engines when elrails are disabled and then re-enabled */
00310       if (HasBit(u->flags, VRF_EL_ENGINE_ALLOWED_NORMAL_RAIL)) {
00311         u->railtype = RAILTYPE_RAIL;
00312         u->compatible_railtypes |= RAILTYPES_RAIL;
00313       }
00314 
00315       /* max speed is the minimum of the speed limits of all vehicles in the consist */
00316       if ((rvi_u->railveh_type != RAILVEH_WAGON || _settings_game.vehicle.wagon_speed_limits) && !UsesWagonOverride(u)) {
00317         uint16 speed = GetVehicleProperty(u, PROP_TRAIN_SPEED, rvi_u->max_speed);
00318         if (speed != 0) max_speed = min(speed, max_speed);
00319       }
00320     }
00321 
00322     u->cargo_cap = GetVehicleCapacity(u);
00323 
00324     /* check the vehicle length (callback) */
00325     uint16 veh_len = CALLBACK_FAILED;
00326     if (HasBit(e_u->info.callback_mask, CBM_VEHICLE_LENGTH)) {
00327       veh_len = GetVehicleCallback(CBID_VEHICLE_LENGTH, 0, 0, u->engine_type, u);
00328     }
00329     if (veh_len == CALLBACK_FAILED) veh_len = rvi_u->shorten_factor;
00330     veh_len = 8 - Clamp(veh_len, 0, 7);
00331 
00332     /* verify length hasn't changed */
00333     if (same_length && veh_len != u->tcache.cached_veh_length) RailVehicleLengthChanged(u);
00334 
00335     /* update vehicle length? */
00336     if (!same_length) u->tcache.cached_veh_length = veh_len;
00337 
00338     v->tcache.cached_total_length += u->tcache.cached_veh_length;
00339     v->InvalidateNewGRFCache();
00340     u->InvalidateNewGRFCache();
00341   }
00342 
00343   /* store consist weight/max speed in cache */
00344   v->tcache.cached_max_speed = max_speed;
00345   v->tcache.cached_tilt = train_can_tilt;
00346   v->tcache.cached_max_curve_speed = GetTrainCurveSpeedLimit(v);
00347 
00348   /* recalculate cached weights and power too (we do this *after* the rest, so it is known which wagons are powered and need extra weight added) */
00349   TrainCargoChanged(v);
00350 
00351   if (v->IsFrontEngine()) {
00352     UpdateTrainAcceleration(v);
00353     SetWindowDirty(WC_VEHICLE_DETAILS, v->index);
00354   }
00355 }
00356 
00357 enum AccelType {
00358   AM_ACCEL,
00359   AM_BRAKE
00360 };
00361 
00372 int GetTrainStopLocation(StationID station_id, TileIndex tile, const Train *v, int *station_ahead, int *station_length)
00373 {
00374   const Station *st = Station::Get(station_id);
00375   *station_ahead  = st->GetPlatformLength(tile, DirToDiagDir(v->direction)) * TILE_SIZE;
00376   *station_length = st->GetPlatformLength(tile) * TILE_SIZE;
00377 
00378   /* Default to the middle of the station for stations stops that are not in
00379    * the order list like intermediate stations when non-stop is disabled */
00380   OrderStopLocation osl = OSL_PLATFORM_MIDDLE;
00381   if (v->tcache.cached_total_length >= *station_length) {
00382     /* The train is longer than the station, make it stop at the far end of the platform */
00383     osl = OSL_PLATFORM_FAR_END;
00384   } else if (v->current_order.IsType(OT_GOTO_STATION) && v->current_order.GetDestination() == station_id) {
00385     osl = v->current_order.GetStopLocation();
00386   }
00387 
00388   /* The stop location of the FRONT! of the train */
00389   int stop;
00390   switch (osl) {
00391     default: NOT_REACHED();
00392 
00393     case OSL_PLATFORM_NEAR_END:
00394       stop = v->tcache.cached_total_length;
00395       break;
00396 
00397     case OSL_PLATFORM_MIDDLE:
00398       stop = *station_length - (*station_length - v->tcache.cached_total_length) / 2;
00399       break;
00400 
00401     case OSL_PLATFORM_FAR_END:
00402       stop = *station_length;
00403       break;
00404   }
00405 
00406   /* Substract half the front vehicle length of the train so we get the real
00407    * stop location of the train. */
00408   return stop - (v->tcache.cached_veh_length + 1) / 2;
00409 }
00410 
00411 
00417 int GetTrainCurveSpeedLimit(Train *v)
00418 {
00419   static const int absolute_max_speed = UINT16_MAX;
00420   int max_speed = absolute_max_speed;
00421 
00422   if (_settings_game.vehicle.train_acceleration_model == TAM_ORIGINAL) return max_speed;
00423 
00424   int curvecount[2] = {0, 0};
00425 
00426   /* first find the curve speed limit */
00427   int numcurve = 0;
00428   int sum = 0;
00429   int pos = 0;
00430   int lastpos = -1;
00431   for (const Vehicle *u = v; u->Next() != NULL; u = u->Next(), pos++) {
00432     Direction this_dir = u->direction;
00433     Direction next_dir = u->Next()->direction;
00434 
00435     DirDiff dirdiff = DirDifference(this_dir, next_dir);
00436     if (dirdiff == DIRDIFF_SAME) continue;
00437 
00438     if (dirdiff == DIRDIFF_45LEFT) curvecount[0]++;
00439     if (dirdiff == DIRDIFF_45RIGHT) curvecount[1]++;
00440     if (dirdiff == DIRDIFF_45LEFT || dirdiff == DIRDIFF_45RIGHT) {
00441       if (lastpos != -1) {
00442         numcurve++;
00443         sum += pos - lastpos;
00444         if (pos - lastpos == 1) {
00445           max_speed = 88;
00446         }
00447       }
00448       lastpos = pos;
00449     }
00450 
00451     /* if we have a 90 degree turn, fix the speed limit to 60 */
00452     if (dirdiff == DIRDIFF_90LEFT || dirdiff == DIRDIFF_90RIGHT) {
00453       max_speed = 61;
00454     }
00455   }
00456 
00457   if (numcurve > 0 && max_speed > 88) {
00458     if (curvecount[0] == 1 && curvecount[1] == 1) {
00459       max_speed = absolute_max_speed;
00460     } else {
00461       sum /= numcurve;
00462       max_speed = 232 - (13 - Clamp(sum, 1, 12)) * (13 - Clamp(sum, 1, 12));
00463     }
00464   }
00465 
00466   if (max_speed != absolute_max_speed) {
00467     /* Apply the engine's rail type curve speed advantage, if it slowed by curves */
00468     const RailtypeInfo *rti = GetRailTypeInfo(v->railtype);
00469     max_speed += (max_speed / 2) * rti->curve_speed;
00470 
00471     if (v->tcache.cached_tilt) {
00472       /* Apply max_speed bonus of 20% for a tilting train */
00473       max_speed += max_speed / 5;
00474     }
00475   }
00476 
00477   return max_speed;
00478 }
00479 
00481 static int GetTrainAcceleration(Train *v, bool mode)
00482 {
00483   int max_speed = v->tcache.cached_max_curve_speed;
00484   assert(max_speed == GetTrainCurveSpeedLimit(v)); // safety check, will be removed later
00485   int speed = v->cur_speed * 10 / 16; // km-ish/h -> mp/h
00486 
00487   if (IsRailStationTile(v->tile) && v->IsFrontEngine()) {
00488     StationID sid = GetStationIndex(v->tile);
00489     if (v->current_order.ShouldStopAtStation(v, sid)) {
00490       int station_ahead;
00491       int station_length;
00492       int stop_at = GetTrainStopLocation(sid, v->tile, v, &station_ahead, &station_length);
00493 
00494       /* The distance to go is whatever is still ahead of the train minus the
00495        * distance from the train's stop location to the end of the platform */
00496       int distance_to_go = station_ahead / TILE_SIZE - (station_length - stop_at) / TILE_SIZE;
00497 
00498       if (distance_to_go > 0) {
00499         int st_max_speed = 120;
00500 
00501         int delta_v = v->cur_speed / (distance_to_go + 1);
00502         if (v->max_speed > (v->cur_speed - delta_v)) {
00503           st_max_speed = v->cur_speed - (delta_v / 10);
00504         }
00505 
00506         st_max_speed = max(st_max_speed, 25 * distance_to_go);
00507         max_speed = min(max_speed, st_max_speed);
00508       }
00509     }
00510   }
00511 
00512   int mass = v->tcache.cached_weight;
00513   int power = v->tcache.cached_power * 746;
00514   max_speed = min(max_speed, v->tcache.cached_max_speed);
00515 
00516   int num = 0; // number of vehicles, change this into the number of axles later
00517   int incl = 0;
00518   int drag_coeff = 20; //[1e-4]
00519   for (const Train *u = v; u != NULL; u = u->Next()) {
00520     num++;
00521     drag_coeff += 3;
00522 
00523     if (u->track == TRACK_BIT_DEPOT) max_speed = min(max_speed, 61);
00524 
00525     if (HasBit(u->flags, VRF_GOINGUP)) {
00526       incl += u->tcache.cached_veh_weight * 60; // 3% slope, quite a bit actually
00527     } else if (HasBit(u->flags, VRF_GOINGDOWN)) {
00528       incl -= u->tcache.cached_veh_weight * 60;
00529     }
00530   }
00531 
00532   v->max_speed = max_speed;
00533 
00534   const int area = 120;
00535   const int friction = 35; //[1e-3]
00536   int resistance;
00537   if (v->railtype != RAILTYPE_MAGLEV) {
00538     resistance = 13 * mass / 10;
00539     resistance += 60 * num;
00540     resistance += friction * mass * speed / 1000;
00541     resistance += (area * drag_coeff * speed * speed) / 10000;
00542   } else {
00543     resistance = (area * (drag_coeff / 2) * speed * speed) / 10000;
00544   }
00545   resistance += incl;
00546   resistance *= 4; //[N]
00547 
00548   const int max_te = v->tcache.cached_max_te; // [N]
00549   int force;
00550   if (speed > 0) {
00551     switch (v->railtype) {
00552       case RAILTYPE_RAIL:
00553       case RAILTYPE_ELECTRIC:
00554       case RAILTYPE_MONO:
00555         force = power / speed; //[N]
00556         force *= 22;
00557         force /= 10;
00558         if (mode == AM_ACCEL && force > max_te) force = max_te;
00559         break;
00560 
00561       default: NOT_REACHED();
00562       case RAILTYPE_MAGLEV:
00563         force = power / 25;
00564         break;
00565     }
00566   } else {
00567     /* "kickoff" acceleration */
00568     force = (mode == AM_ACCEL && v->railtype != RAILTYPE_MAGLEV) ? min(max_te, power) : power;
00569     force = max(force, (mass * 8) + resistance);
00570   }
00571 
00572   if (mode == AM_ACCEL) {
00573     return (force - resistance) / (mass * 2);
00574   } else {
00575     return min(-force - resistance, -10000) / mass;
00576   }
00577 }
00578 
00579 void UpdateTrainAcceleration(Train *v)
00580 {
00581   assert(v->IsFrontEngine());
00582 
00583   v->max_speed = v->tcache.cached_max_speed;
00584 
00585   uint power = v->tcache.cached_power;
00586   uint weight = v->tcache.cached_weight;
00587   assert(weight != 0);
00588   v->acceleration = Clamp(power / weight * 4, 1, 255);
00589 }
00590 
00596 int Train::GetDisplayImageWidth(Point *offset) const
00597 {
00598   int reference_width = TRAININFO_DEFAULT_VEHICLE_WIDTH;
00599   int vehicle_pitch = 0;
00600 
00601   const Engine *e = Engine::Get(this->engine_type);
00602   if (e->grffile != NULL && is_custom_sprite(e->u.rail.image_index)) {
00603     reference_width = e->grffile->traininfo_vehicle_width;
00604     vehicle_pitch = e->grffile->traininfo_vehicle_pitch;
00605   }
00606 
00607   if (offset != NULL) {
00608     offset->x = reference_width / 2;
00609     offset->y = vehicle_pitch;
00610   }
00611   return this->tcache.cached_veh_length * reference_width / 8;
00612 }
00613 
00614 static SpriteID GetDefaultTrainSprite(uint8 spritenum, Direction direction)
00615 {
00616   return ((direction + _engine_sprite_add[spritenum]) & _engine_sprite_and[spritenum]) + _engine_sprite_base[spritenum];
00617 }
00618 
00619 SpriteID Train::GetImage(Direction direction) const
00620 {
00621   uint8 spritenum = this->spritenum;
00622   SpriteID sprite;
00623 
00624   if (HasBit(this->flags, VRF_REVERSE_DIRECTION)) direction = ReverseDir(direction);
00625 
00626   if (is_custom_sprite(spritenum)) {
00627     sprite = GetCustomVehicleSprite(this, (Direction)(direction + 4 * IS_CUSTOM_SECONDHEAD_SPRITE(spritenum)));
00628     if (sprite != 0) return sprite;
00629 
00630     spritenum = Engine::Get(this->engine_type)->original_image_index;
00631   }
00632 
00633   sprite = GetDefaultTrainSprite(spritenum, direction);
00634 
00635   if (this->cargo.Count() >= this->cargo_cap / 2U) sprite += _wagon_full_adder[spritenum];
00636 
00637   return sprite;
00638 }
00639 
00640 static SpriteID GetRailIcon(EngineID engine, bool rear_head, int &y)
00641 {
00642   const Engine *e = Engine::Get(engine);
00643   Direction dir = rear_head ? DIR_E : DIR_W;
00644   uint8 spritenum = e->u.rail.image_index;
00645 
00646   if (is_custom_sprite(spritenum)) {
00647     SpriteID sprite = GetCustomVehicleIcon(engine, dir);
00648     if (sprite != 0) {
00649       if (e->grffile != NULL) {
00650         y += e->grffile->traininfo_vehicle_pitch;
00651       }
00652       return sprite;
00653     }
00654 
00655     spritenum = Engine::Get(engine)->original_image_index;
00656   }
00657 
00658   if (rear_head) spritenum++;
00659 
00660   return GetDefaultTrainSprite(spritenum, DIR_W);
00661 }
00662 
00663 void DrawTrainEngine(int left, int right, int preferred_x, int y, EngineID engine, SpriteID pal)
00664 {
00665   if (RailVehInfo(engine)->railveh_type == RAILVEH_MULTIHEAD) {
00666     int yf = y;
00667     int yr = y;
00668 
00669     SpriteID spritef = GetRailIcon(engine, false, yf);
00670     SpriteID spriter = GetRailIcon(engine, true, yr);
00671     const Sprite *real_spritef = GetSprite(spritef, ST_NORMAL);
00672     const Sprite *real_spriter = GetSprite(spriter, ST_NORMAL);
00673 
00674     preferred_x = Clamp(preferred_x, left - real_spritef->x_offs + 14, right - real_spriter->width - real_spriter->x_offs - 15);
00675 
00676     DrawSprite(spritef, pal, preferred_x - 14, yf);
00677     DrawSprite(spriter, pal, preferred_x + 15, yr);
00678   } else {
00679     SpriteID sprite = GetRailIcon(engine, false, y);
00680     const Sprite *real_sprite = GetSprite(sprite, ST_NORMAL);
00681     preferred_x = Clamp(preferred_x, left - real_sprite->x_offs, right - real_sprite->width - real_sprite->x_offs);
00682     DrawSprite(sprite, pal, preferred_x, y);
00683   }
00684 }
00685 
00686 static CommandCost CmdBuildRailWagon(EngineID engine, TileIndex tile, DoCommandFlag flags)
00687 {
00688   const Engine *e = Engine::Get(engine);
00689   const RailVehicleInfo *rvi = &e->u.rail;
00690   CommandCost value(EXPENSES_NEW_VEHICLES, e->GetCost());
00691 
00692   /* Engines without valid cargo should not be available */
00693   if (e->GetDefaultCargoType() == CT_INVALID) return CMD_ERROR;
00694 
00695   if (flags & DC_QUERY_COST) return value;
00696 
00697   /* Check that the wagon can drive on the track in question */
00698   if (!IsCompatibleRail(rvi->railtype, GetRailType(tile))) return CMD_ERROR;
00699 
00700   uint num_vehicles = 1 + CountArticulatedParts(engine, false);
00701 
00702   /* Allow for the wagon and the articulated parts */
00703   if (!Vehicle::CanAllocateItem(num_vehicles)) {
00704     return_cmd_error(STR_ERROR_TOO_MANY_VEHICLES_IN_GAME);
00705   }
00706 
00707   if (flags & DC_EXEC) {
00708     Train *v = new Train();
00709     v->spritenum = rvi->image_index;
00710 
00711     v->engine_type = engine;
00712     v->tcache.first_engine = INVALID_ENGINE; // needs to be set before first callback
00713 
00714     DiagDirection dir = GetRailDepotDirection(tile);
00715 
00716     v->direction = DiagDirToDir(dir);
00717     v->tile = tile;
00718 
00719     int x = TileX(tile) * TILE_SIZE | _vehicle_initial_x_fract[dir];
00720     int y = TileY(tile) * TILE_SIZE | _vehicle_initial_y_fract[dir];
00721 
00722     v->x_pos = x;
00723     v->y_pos = y;
00724     v->z_pos = GetSlopeZ(x, y);
00725     v->owner = _current_company;
00726     v->track = TRACK_BIT_DEPOT;
00727     v->vehstatus = VS_HIDDEN | VS_DEFPAL;
00728 
00729 //    v->subtype = 0;
00730     v->SetWagon();
00731 
00732     v->SetFreeWagon();
00733     InvalidateWindowData(WC_VEHICLE_DEPOT, v->tile);
00734 
00735     v->cargo_type = e->GetDefaultCargoType();
00736 //    v->cargo_subtype = 0;
00737     v->cargo_cap = rvi->capacity;
00738     v->value = value.GetCost();
00739 //    v->day_counter = 0;
00740 
00741     v->railtype = rvi->railtype;
00742 
00743     v->build_year = _cur_year;
00744     v->cur_image = SPR_IMG_QUERY;
00745     v->random_bits = VehicleRandomBits();
00746 
00747     v->group_id = DEFAULT_GROUP;
00748 
00749     AddArticulatedParts(v);
00750 
00751     _new_vehicle_id = v->index;
00752 
00753     VehicleMove(v, false);
00754     TrainConsistChanged(v->First(), false);
00755     UpdateTrainGroupID(v->First());
00756 
00757     SetWindowDirty(WC_VEHICLE_DEPOT, v->tile);
00758     if (IsLocalCompany()) {
00759       InvalidateAutoreplaceWindow(v->engine_type, v->group_id); // updates the replace Train window
00760     }
00761     Company::Get(_current_company)->num_engines[engine]++;
00762 
00763     CheckConsistencyOfArticulatedVehicle(v);
00764 
00765     /* Try to connect the vehicle to one of free chains of wagons. */
00766     Train *w;
00767     FOR_ALL_TRAINS(w) {
00768       /* do not connect new wagon with crashed/flooded consists */
00769       if (w->tile == tile && w->IsFreeWagon() &&
00770           w->engine_type == engine &&
00771           !(w->vehstatus & VS_CRASHED)) {
00772         DoCommand(0, v->index | (w->Last()->index << 16), 1, DC_EXEC, CMD_MOVE_RAIL_VEHICLE);
00773         break;
00774       }
00775     }
00776   }
00777 
00778   return value;
00779 }
00780 
00782 static void NormalizeTrainVehInDepot(const Train *u)
00783 {
00784   const Train *v;
00785   FOR_ALL_TRAINS(v) {
00786     if (v->IsFreeWagon() && v->tile == u->tile &&
00787         v->track == TRACK_BIT_DEPOT) {
00788       if (CmdFailed(DoCommand(0, v->index | (u->index << 16), 1, DC_EXEC,
00789           CMD_MOVE_RAIL_VEHICLE)))
00790         break;
00791     }
00792   }
00793 }
00794 
00795 static void AddRearEngineToMultiheadedTrain(Train *v)
00796 {
00797   Train *u = new Train();
00798   v->value >>= 1;
00799   u->value = v->value;
00800   u->direction = v->direction;
00801   u->owner = v->owner;
00802   u->tile = v->tile;
00803   u->x_pos = v->x_pos;
00804   u->y_pos = v->y_pos;
00805   u->z_pos = v->z_pos;
00806   u->track = TRACK_BIT_DEPOT;
00807   u->vehstatus = v->vehstatus & ~VS_STOPPED;
00808 //  u->subtype = 0;
00809   u->spritenum = v->spritenum + 1;
00810   u->cargo_type = v->cargo_type;
00811   u->cargo_subtype = v->cargo_subtype;
00812   u->cargo_cap = v->cargo_cap;
00813   u->railtype = v->railtype;
00814   u->engine_type = v->engine_type;
00815   u->build_year = v->build_year;
00816   u->cur_image = SPR_IMG_QUERY;
00817   u->random_bits = VehicleRandomBits();
00818   v->SetMultiheaded();
00819   u->SetMultiheaded();
00820   v->SetNext(u);
00821   VehicleMove(u, false);
00822 
00823   /* Now we need to link the front and rear engines together */
00824   v->other_multiheaded_part = u;
00825   u->other_multiheaded_part = v;
00826 }
00827 
00836 CommandCost CmdBuildRailVehicle(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00837 {
00838   /* Check if the engine-type is valid (for the company) */
00839   if (!IsEngineBuildable(p1, VEH_TRAIN, _current_company)) return_cmd_error(STR_ERROR_RAIL_VEHICLE_NOT_AVAILABLE);
00840 
00841   const Engine *e = Engine::Get(p1);
00842   const RailVehicleInfo *rvi = &e->u.rail;
00843   CommandCost value(EXPENSES_NEW_VEHICLES, e->GetCost());
00844 
00845   /* Engines with CT_INVALID should not be available */
00846   if (e->GetDefaultCargoType() == CT_INVALID) return CMD_ERROR;
00847 
00848   if (flags & DC_QUERY_COST) return value;
00849 
00850   /* Check if the train is actually being built in a depot belonging
00851    * to the company. Doesn't matter if only the cost is queried */
00852   if (!IsRailDepotTile(tile)) return CMD_ERROR;
00853   if (!IsTileOwner(tile, _current_company)) return CMD_ERROR;
00854 
00855   if (rvi->railveh_type == RAILVEH_WAGON) return CmdBuildRailWagon(p1, tile, flags);
00856 
00857   uint num_vehicles =
00858     (rvi->railveh_type == RAILVEH_MULTIHEAD ? 2 : 1) +
00859     CountArticulatedParts(p1, false);
00860 
00861   /* Check if depot and new engine uses the same kind of tracks *
00862    * We need to see if the engine got power on the tile to avoid eletric engines in non-electric depots */
00863   if (!HasPowerOnRail(rvi->railtype, GetRailType(tile))) return CMD_ERROR;
00864 
00865   /* Allow for the dual-heads and the articulated parts */
00866   if (!Vehicle::CanAllocateItem(num_vehicles)) {
00867     return_cmd_error(STR_ERROR_TOO_MANY_VEHICLES_IN_GAME);
00868   }
00869 
00870   UnitID unit_num = (flags & DC_AUTOREPLACE) ? 0 : GetFreeUnitNumber(VEH_TRAIN);
00871   if (unit_num > _settings_game.vehicle.max_trains) {
00872     return_cmd_error(STR_ERROR_TOO_MANY_VEHICLES_IN_GAME);
00873   }
00874 
00875   if (flags & DC_EXEC) {
00876     DiagDirection dir = GetRailDepotDirection(tile);
00877     int x = TileX(tile) * TILE_SIZE + _vehicle_initial_x_fract[dir];
00878     int y = TileY(tile) * TILE_SIZE + _vehicle_initial_y_fract[dir];
00879 
00880     Train *v = new Train();
00881     v->unitnumber = unit_num;
00882     v->direction = DiagDirToDir(dir);
00883     v->tile = tile;
00884     v->owner = _current_company;
00885     v->x_pos = x;
00886     v->y_pos = y;
00887     v->z_pos = GetSlopeZ(x, y);
00888 //    v->running_ticks = 0;
00889     v->track = TRACK_BIT_DEPOT;
00890     v->vehstatus = VS_HIDDEN | VS_STOPPED | VS_DEFPAL;
00891     v->spritenum = rvi->image_index;
00892     v->cargo_type = e->GetDefaultCargoType();
00893 //    v->cargo_subtype = 0;
00894     v->cargo_cap = rvi->capacity;
00895     v->max_speed = rvi->max_speed;
00896     v->value = value.GetCost();
00897     v->last_station_visited = INVALID_STATION;
00898 //    v->dest_tile = 0;
00899 
00900     v->engine_type = p1;
00901     v->tcache.first_engine = INVALID_ENGINE; // needs to be set before first callback
00902 
00903     v->reliability = e->reliability;
00904     v->reliability_spd_dec = e->reliability_spd_dec;
00905     v->max_age = e->GetLifeLengthInDays();
00906 
00907     v->name = NULL;
00908     v->railtype = rvi->railtype;
00909     _new_vehicle_id = v->index;
00910 
00911     v->service_interval = Company::Get(_current_company)->settings.vehicle.servint_trains;
00912     v->date_of_last_service = _date;
00913     v->build_year = _cur_year;
00914     v->cur_image = SPR_IMG_QUERY;
00915     v->random_bits = VehicleRandomBits();
00916 
00917 //    v->vehicle_flags = 0;
00918     if (e->flags & ENGINE_EXCLUSIVE_PREVIEW) SetBit(v->vehicle_flags, VF_BUILT_AS_PROTOTYPE);
00919 
00920     v->group_id = DEFAULT_GROUP;
00921 
00922 //    v->subtype = 0;
00923     v->SetFrontEngine();
00924     v->SetEngine();
00925 
00926     VehicleMove(v, false);
00927 
00928     if (rvi->railveh_type == RAILVEH_MULTIHEAD) {
00929       AddRearEngineToMultiheadedTrain(v);
00930     } else {
00931       AddArticulatedParts(v);
00932     }
00933 
00934     TrainConsistChanged(v, false);
00935     UpdateTrainGroupID(v);
00936 
00937     if (!HasBit(p2, 1) && !(flags & DC_AUTOREPLACE)) { // check if the cars should be added to the new vehicle
00938       NormalizeTrainVehInDepot(v);
00939     }
00940 
00941     InvalidateWindowData(WC_VEHICLE_DEPOT, v->tile);
00942     InvalidateWindowClassesData(WC_TRAINS_LIST, 0);
00943     SetWindowDirty(WC_COMPANY, v->owner);
00944     if (IsLocalCompany()) {
00945       InvalidateAutoreplaceWindow(v->engine_type, v->group_id); // updates the replace Train window
00946     }
00947 
00948     Company::Get(_current_company)->num_engines[p1]++;
00949 
00950     CheckConsistencyOfArticulatedVehicle(v);
00951   }
00952 
00953   return value;
00954 }
00955 
00956 
00957 bool Train::IsInDepot() const
00958 {
00959   /* Is the front engine stationary in the depot? */
00960   if (!IsRailDepotTile(this->tile) || this->cur_speed != 0) return false;
00961 
00962   /* Check whether the rest is also already trying to enter the depot. */
00963   for (const Train *v = this; v != NULL; v = v->Next()) {
00964     if (v->track != TRACK_BIT_DEPOT || v->tile != this->tile) return false;
00965   }
00966 
00967   return true;
00968 }
00969 
00970 bool Train::IsStoppedInDepot() const
00971 {
00972   /* Are we stopped? Ofcourse wagons don't really care... */
00973   if (this->IsFrontEngine() && !(this->vehstatus & VS_STOPPED)) return false;
00974   return this->IsInDepot();
00975 }
00976 
00977 static Train *FindGoodVehiclePos(const Train *src)
00978 {
00979   EngineID eng = src->engine_type;
00980   TileIndex tile = src->tile;
00981 
00982   Train *dst;
00983   FOR_ALL_TRAINS(dst) {
00984     if (dst->IsFreeWagon() && dst->tile == tile && !(dst->vehstatus & VS_CRASHED)) {
00985       /* check so all vehicles in the line have the same engine. */
00986       Train *t = dst;
00987       while (t->engine_type == eng) {
00988         t = t->Next();
00989         if (t == NULL) return dst;
00990       }
00991     }
00992   }
00993 
00994   return NULL;
00995 }
00996 
00998 typedef SmallVector<Train *, 16> TrainList;
00999 
01005 static void MakeTrainBackup(TrainList &list, Train *t)
01006 {
01007   for (; t != NULL; t = t->Next()) *list.Append() = t;
01008 }
01009 
01014 static void RestoreTrainBackup(TrainList &list)
01015 {
01016   /* No train, nothing to do. */
01017   if (list.Length() == 0) return;
01018 
01019   Train *prev = NULL;
01020   /* Iterate over the list and rebuild it. */
01021   for (Train **iter = list.Begin(); iter != list.End(); iter++) {
01022     Train *t = *iter;
01023     if (prev != NULL) {
01024       prev->SetNext(t);
01025     } else if (t->Previous() != NULL) {
01026       /* Make sure the head of the train is always the first in the chain. */
01027       t->Previous()->SetNext(NULL);
01028     }
01029     prev = t;
01030   }
01031 }
01032 
01038 static void RemoveFromConsist(Train *part, bool chain = false)
01039 {
01040   Train *tail = chain ? part->Last() : part->GetLastEnginePart();
01041 
01042   /* Unlink at the front, but make it point to the next
01043    * vehicle after the to be remove part. */
01044   if (part->Previous() != NULL) part->Previous()->SetNext(tail->Next());
01045 
01046   /* Unlink at the back */
01047   tail->SetNext(NULL);
01048 }
01049 
01055 static void InsertInConsist(Train *dst, Train *chain)
01056 {
01057   /* We do not want to add something in the middle of an articulated part. */
01058   assert(dst->Next() == NULL || !dst->Next()->IsArticulatedPart());
01059 
01060   chain->Last()->SetNext(dst->Next());
01061   dst->SetNext(chain);
01062 }
01063 
01069 static void NormaliseDualHeads(Train *t)
01070 {
01071   for (; t != NULL; t = t->GetNextVehicle()) {
01072     if (!t->IsMultiheaded() || !t->IsEngine()) continue;
01073 
01074     /* Make sure that there are no free cars before next engine */
01075     Train *u;
01076     for (u = t; u->Next() != NULL && !u->Next()->IsEngine(); u = u->Next()) {}
01077 
01078     if (u == t->other_multiheaded_part) continue;
01079 
01080     /* Remove the part from the 'wrong' train */
01081     RemoveFromConsist(t->other_multiheaded_part);
01082     /* And add it to the 'right' train */
01083     InsertInConsist(u, t->other_multiheaded_part);
01084   }
01085 }
01086 
01091 static void NormaliseSubtypes(Train *chain)
01092 {
01093   /* Nothing to do */
01094   if (chain == NULL) return;
01095 
01096   /* We must be the first in the chain. */
01097   assert(chain->Previous() == NULL);
01098 
01099   /* Set the appropirate bits for the first in the chain. */
01100   if (chain->IsWagon()) {
01101     chain->SetFreeWagon();
01102   } else {
01103     assert(chain->IsEngine());
01104     chain->SetFrontEngine();
01105   }
01106 
01107   /* Now clear the bits for the rest of the chain */
01108   for (Train *t = chain->Next(); t != NULL; t = t->Next()) {
01109     t->ClearFreeWagon();
01110     t->ClearFrontEngine();
01111   }
01112 }
01113 
01123 static CommandCost CheckNewTrain(Train *original_dst, Train *dst, Train *original_src, Train *src)
01124 {
01125   /* Just add 'new' engines and substract the original ones.
01126    * If that's less than or equal to 0 we can be sure we did
01127    * not add any engines (read: trains) along the way. */
01128   if ((src          != NULL && src->IsEngine()          ? 1 : 0) +
01129       (dst          != NULL && dst->IsEngine()          ? 1 : 0) -
01130       (original_src != NULL && original_src->IsEngine() ? 1 : 0) -
01131       (original_dst != NULL && original_dst->IsEngine() ? 1 : 0) <= 0) {
01132     return CommandCost();
01133   }
01134 
01135   /* Get a free unit number and check whether it's within the bounds.
01136    * There will always be a maximum of one new train. */
01137   if (GetFreeUnitNumber(VEH_TRAIN) <= _settings_game.vehicle.max_trains) return CommandCost();
01138 
01139   return_cmd_error(STR_ERROR_TOO_MANY_VEHICLES_IN_GAME);
01140 }
01141 
01147 static CommandCost CheckTrainAttachment(Train *t)
01148 {
01149   /* No multi-part train, no need to check. */
01150   if (t == NULL || t->Next() == NULL || !t->IsEngine()) return CommandCost();
01151 
01152   /* The maximum length for a train. For each part we decrease this by one
01153    * and if the result is negative the train is simply too long. */
01154   int allowed_len = _settings_game.vehicle.mammoth_trains ? 100 : 10;
01155 
01156   Train *head = t;
01157   Train *prev = t;
01158 
01159   /* Break the prev -> t link so it always holds within the loop. */
01160   t = t->Next();
01161   allowed_len--;
01162   prev->SetNext(NULL);
01163 
01164   /* Make sure the cache is cleared. */
01165   head->InvalidateNewGRFCache();
01166 
01167   while (t != NULL) {
01168     Train *next = t->Next();
01169 
01170     /* Unlink the to-be-added piece; it is already unlinked from the previous
01171      * part due to the fact that the prev -> t link is broken. */
01172     t->SetNext(NULL);
01173 
01174     /* Don't check callback for articulated or rear dual headed parts */
01175     if (!t->IsArticulatedPart() && !t->IsRearDualheaded()) {
01176       allowed_len--; // We do not count articulated parts and rear heads either.
01177 
01178       /* Back up and clear the first_engine data to avoid using wagon override group */
01179       EngineID first_engine = t->tcache.first_engine;
01180       t->tcache.first_engine = INVALID_ENGINE;
01181 
01182       /* We don't want the cache to interfere. head's cache is cleared before
01183        * the loop and after each callback does not need to be cleared here. */
01184       t->InvalidateNewGRFCache();
01185 
01186       uint16 callback = GetVehicleCallbackParent(CBID_TRAIN_ALLOW_WAGON_ATTACH, 0, 0, head->engine_type, t, head);
01187 
01188       /* Restore original first_engine data */
01189       t->tcache.first_engine = first_engine;
01190 
01191       /* We do not want to remember any cached variables from the test run */
01192       t->InvalidateNewGRFCache();
01193       head->InvalidateNewGRFCache();
01194 
01195       if (callback != CALLBACK_FAILED) {
01196         /* A failing callback means everything is okay */
01197         StringID error = STR_NULL;
01198 
01199         if (callback == 0xFD) error = STR_ERROR_INCOMPATIBLE_RAIL_TYPES;
01200         if (callback  < 0xFD) error = GetGRFStringID(GetEngineGRFID(head->engine_type), 0xD000 + callback);
01201 
01202         if (error != STR_NULL) return_cmd_error(error);
01203       }
01204     }
01205 
01206     /* And link it to the new part. */
01207     prev->SetNext(t);
01208     prev = t;
01209     t = next;
01210   }
01211 
01212   if (allowed_len <= 0) return_cmd_error(STR_ERROR_TRAIN_TOO_LONG);
01213   return CommandCost();
01214 }
01215 
01225 static CommandCost ValidateTrains(Train *original_dst, Train *dst, Train *original_src, Train *src)
01226 {
01227   /* Check whether we may actually construct the trains. */
01228   CommandCost ret = CheckTrainAttachment(src);
01229   if (ret.Failed()) return ret;
01230   ret = CheckTrainAttachment(dst);
01231   if (ret.Failed()) return ret;
01232 
01233   /* Check whether we need to build a new train. */
01234   return CheckNewTrain(original_dst, dst, original_src, src);
01235 }
01236 
01245 static void ArrangeTrains(Train **dst_head, Train *dst, Train **src_head, Train *src, bool move_chain)
01246 {
01247   /* First determine the front of the two resulting trains */
01248   if (*src_head == *dst_head) {
01249     /* If we aren't moving part(s) to a new train, we are just moving the
01250      * front back and there is not destination head. */
01251     *dst_head = NULL;
01252   } else if (*dst_head == NULL) {
01253     /* If we are moving to a new train the head of the move train would become
01254      * the head of the new vehicle. */
01255     *dst_head = src;
01256   }
01257 
01258   if (src == *src_head) {
01259     /* If we are moving the front of a train then we are, in effect, creating
01260      * a new head for the train. Point to that. Unless we are moving the whole
01261      * train in which case there is not 'source' train anymore.
01262      * In case we are a multiheaded part we want the complete thing to come
01263      * with us, so src->GetNextUnit(), however... when we are e.g. a wagon
01264      * that is followed by a rear multihead we do not want to include that. */
01265     *src_head = move_chain ? NULL :
01266         (src->IsMultiheaded() ? src->GetNextUnit() : src->GetNextVehicle());
01267   }
01268 
01269   /* Now it's just simply removing the part that we are going to move from the
01270    * source train and *if* the destination is a not a new train add the chain
01271    * at the destination location. */
01272   RemoveFromConsist(src, move_chain);
01273   if (*dst_head != src) InsertInConsist(dst, src);
01274 
01275   /* Now normalise the dual heads, that is move the dual heads around in such
01276    * a way that the head and rear of a dual head are in the same train */
01277   NormaliseDualHeads(*src_head);
01278   NormaliseDualHeads(*dst_head);
01279 }
01280 
01286 static void NormaliseTrainHead(Train *head)
01287 {
01288   /* Not much to do! */
01289   if (head == NULL) return;
01290 
01291   /* Tell the 'world' the train changed. */
01292   TrainConsistChanged(head, false);
01293   UpdateTrainGroupID(head);
01294 
01295   /* Not a front engine, i.e. a free wagon chain. No need to do more. */
01296   if (!head->IsFrontEngine()) return;
01297 
01298   /* Update the refit button and window */
01299   SetWindowDirty(WC_VEHICLE_REFIT, head->index);
01300   SetWindowWidgetDirty(WC_VEHICLE_VIEW, head->index, VVW_WIDGET_REFIT_VEH);
01301 
01302   /* If we don't have a unit number yet, set one. */
01303   if (head->unitnumber != 0) return;
01304   head->unitnumber = GetFreeUnitNumber(VEH_TRAIN);
01305 }
01306 
01318 CommandCost CmdMoveRailVehicle(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
01319 {
01320   VehicleID s = GB(p1, 0, 16);
01321   VehicleID d = GB(p1, 16, 16);
01322   bool move_chain = HasBit(p2, 0);
01323 
01324   Train *src = Train::GetIfValid(s);
01325   if (src == NULL || !CheckOwnership(src->owner)) return CMD_ERROR;
01326 
01327   /* Do not allow moving crashed vehicles inside the depot, it is likely to cause asserts later */
01328   if (src->vehstatus & VS_CRASHED) return CMD_ERROR;
01329 
01330   /* if nothing is selected as destination, try and find a matching vehicle to drag to. */
01331   Train *dst;
01332   if (d == INVALID_VEHICLE) {
01333     dst = src->IsEngine() ? NULL : FindGoodVehiclePos(src);
01334   } else {
01335     dst = Train::GetIfValid(d);
01336     if (dst == NULL || !CheckOwnership(dst->owner)) return CMD_ERROR;
01337 
01338     /* Do not allow appending to crashed vehicles, too */
01339     if (dst->vehstatus & VS_CRASHED) return CMD_ERROR;
01340   }
01341 
01342   /* if an articulated part is being handled, deal with its parent vehicle */
01343   src = src->GetFirstEnginePart();
01344   if (dst != NULL) {
01345     dst = dst->GetFirstEnginePart();
01346   }
01347 
01348   /* don't move the same vehicle.. */
01349   if (src == dst) return CommandCost();
01350 
01351   /* locate the head of the two chains */
01352   Train *src_head = src->First();
01353   Train *dst_head;
01354   if (dst != NULL) {
01355     dst_head = dst->First();
01356     if (dst_head->tile != src_head->tile) return CMD_ERROR;
01357     /* Now deal with articulated part of destination wagon */
01358     dst = dst->GetLastEnginePart();
01359   } else {
01360     dst_head = NULL;
01361   }
01362 
01363   if (src->IsRearDualheaded()) return_cmd_error(STR_ERROR_REAR_ENGINE_FOLLOW_FRONT);
01364 
01365   /* When moving all wagons, we can't have the same src_head and dst_head */
01366   if (move_chain && src_head == dst_head) return CommandCost();
01367 
01368   /* When moving a multiheaded part to be place after itself, bail out. */
01369   if (!move_chain && dst != NULL && dst->IsRearDualheaded() && src == dst->other_multiheaded_part) return CommandCost();
01370 
01371   /* Check if all vehicles in the source train are stopped inside a depot. */
01372   if (!src_head->IsStoppedInDepot()) return_cmd_error(STR_ERROR_TRAINS_CAN_ONLY_BE_ALTERED_INSIDE_A_DEPOT);
01373 
01374   /* Check if all vehicles in the destination train are stopped inside a depot. */
01375   if (dst_head != NULL && !dst_head->IsStoppedInDepot()) return_cmd_error(STR_ERROR_TRAINS_CAN_ONLY_BE_ALTERED_INSIDE_A_DEPOT);
01376 
01377   /* First make a backup of the order of the trains. That way we can do
01378    * whatever we want with the order and later on easily revert. */
01379   TrainList original_src;
01380   TrainList original_dst;
01381 
01382   MakeTrainBackup(original_src, src_head);
01383   MakeTrainBackup(original_dst, dst_head);
01384 
01385   /* Also make backup of the original heads as ArrangeTrains can change them.
01386    * For the destination head we do not care if it is the same as the source
01387    * head because in that case it's just a copy. */
01388   Train *original_src_head = src_head;
01389   Train *original_dst_head = (dst_head == src_head ? NULL : dst_head);
01390 
01391   /* (Re)arrange the trains in the wanted arrangement. */
01392   ArrangeTrains(&dst_head, dst, &src_head, src, move_chain);
01393 
01394   if ((flags & DC_AUTOREPLACE) == 0) {
01395     /* If the autoreplace flag is set we do not need to test for the validity
01396      * because we are going to revert the train to its original state. As we
01397      * assume the original state was correct autoreplace can skip this. */
01398     CommandCost ret = ValidateTrains(original_dst_head, dst_head, original_src_head, src_head);
01399     if (ret.Failed()) {
01400       /* Restore the train we had. */
01401       RestoreTrainBackup(original_src);
01402       RestoreTrainBackup(original_dst);
01403       return ret;
01404     }
01405   }
01406 
01407   /* do it? */
01408   if (flags & DC_EXEC) {
01409     /* First normalise the sub types of the chains. */
01410     NormaliseSubtypes(src_head);
01411     NormaliseSubtypes(dst_head);
01412 
01413     /* There are 14 different cases:
01414      *  1) front engine gets moved to a new train, it stays a front engine.
01415      *     a) the 'next' part is a wagon, that becomes a free wagon chain.
01416      *     b) the 'next' part is an engine, that becomes a front engine.
01417      *     c) there is no 'next' part, nothing else happens
01418      *  2) front engine gets moved to another train, it is not a front engine anymore
01419      *     a) the 'next' part is a wagon, that becomes a free wagon chain.
01420      *     b) the 'next' part is an engine, that becomes a front engine.
01421      *     c) there is no 'next' part, nothing else happens
01422      *  3) front engine gets moved to later in the current train, it is not an engine anymore.
01423      *     a) the 'next' part is a wagon, that becomes a free wagon chain.
01424      *     b) the 'next' part is an engine, that becomes a front engine.
01425      *  4) free wagon gets moved
01426      *     a) the 'next' part is a wagon, that becomes a free wagon chain.
01427      *     b) the 'next' part is an engine, that becomes a front engine.
01428      *     c) there is no 'next' part, nothing else happens
01429      *  5) non front engine gets moved and becomes a new train, nothing else happens
01430      *  6) non front engine gets moved within a train / to another train, nothing hapens
01431      *  7) wagon gets moved, nothing happens
01432      */
01433     if (src == original_src_head && src->IsEngine() && !src->IsFrontEngine()) {
01434       /* Cases #2 and #3: the front engine gets trashed. */
01435       DeleteWindowById(WC_VEHICLE_VIEW, src->index);
01436       DeleteWindowById(WC_VEHICLE_ORDERS, src->index);
01437       DeleteWindowById(WC_VEHICLE_REFIT, src->index);
01438       DeleteWindowById(WC_VEHICLE_DETAILS, src->index);
01439       DeleteWindowById(WC_VEHICLE_TIMETABLE, src->index);
01440 
01441       /* We are going to be move to another train. So we
01442        * are no part of this group anymore. In case we
01443        * are not moving group... well, then we do not need
01444        * to move.
01445        * Or we are moving to later in the train and our
01446        * new head isn't a front engine anymore.
01447        */
01448       if (dst_head != NULL ? dst_head != src : !src_head->IsFrontEngine()) {
01449         DecreaseGroupNumVehicle(src->group_id);
01450       }
01451 
01452       /* Delete orders, group stuff and the unit number as we're not the
01453        * front of any vehicle anymore. */
01454       DeleteVehicleOrders(src);
01455       RemoveVehicleFromGroup(src);
01456       src->unitnumber = 0;
01457     }
01458 
01459     /* We weren't a front engine but are becoming one. So
01460      * we should be put in the default group. */
01461     if (original_src_head != src && dst_head == src) {
01462       SetTrainGroupID(src, DEFAULT_GROUP);
01463     }
01464 
01465     /* Handle 'new engine' part of cases #1b, #2b, #3b, #4b and #5 in NormaliseTrainHead. */
01466     NormaliseTrainHead(src_head);
01467     NormaliseTrainHead(dst_head);
01468 
01469     /* We are undoubtedly changing something in the depot and train list. */
01470     InvalidateWindowData(WC_VEHICLE_DEPOT, src->tile);
01471     InvalidateWindowClassesData(WC_TRAINS_LIST, 0);
01472   } else {
01473     /* We don't want to execute what we're just tried. */
01474     RestoreTrainBackup(original_src);
01475     RestoreTrainBackup(original_dst);
01476   }
01477 
01478   return CommandCost();
01479 }
01480 
01492 CommandCost CmdSellRailWagon(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
01493 {
01494   /* Check if we deleted a vehicle window */
01495   Window *w = NULL;
01496 
01497   Train *v = Train::GetIfValid(p1);
01498   if (v == NULL || !CheckOwnership(v->owner)) return CMD_ERROR;
01499 
01500   /* Sell a chain of vehicles or not? */
01501   bool sell_chain = HasBit(p2, 0);
01502 
01503   if (v->vehstatus & VS_CRASHED) return_cmd_error(STR_ERROR_CAN_T_SELL_DESTROYED_VEHICLE);
01504 
01505   v = v->GetFirstEnginePart();
01506   Train *first = v->First();
01507 
01508   /* make sure the vehicle is stopped in the depot */
01509   if (!first->IsStoppedInDepot()) {
01510     return_cmd_error(STR_ERROR_TRAINS_CAN_ONLY_BE_ALTERED_INSIDE_A_DEPOT);
01511   }
01512 
01513   if (v->IsRearDualheaded()) return_cmd_error(STR_ERROR_REAR_ENGINE_FOLLOW_FRONT);
01514 
01515   /* First make a backup of the order of the train. That way we can do
01516    * whatever we want with the order and later on easily revert. */
01517   TrainList original;
01518   MakeTrainBackup(original, first);
01519 
01520   /* We need to keep track of the new head and the head of what we're going to sell. */
01521   Train *new_head = first;
01522   Train *sell_head = NULL;
01523 
01524   /* Split the train in the wanted way. */
01525   ArrangeTrains(&sell_head, NULL, &new_head, v, sell_chain);
01526 
01527   /* We don't need to validate the second train; it's going to be sold. */
01528   CommandCost ret = ValidateTrains(NULL, NULL, first, new_head);
01529   if (ret.Failed()) {
01530     /* Restore the train we had. */
01531     RestoreTrainBackup(original);
01532     return ret;
01533   }
01534 
01535   CommandCost cost(EXPENSES_NEW_VEHICLES);
01536   for (Train *t = sell_head; t != NULL; t = t->Next()) cost.AddCost(-t->value);
01537 
01538   /* do it? */
01539   if (flags & DC_EXEC) {
01540     /* First normalise the sub types of the chain. */
01541     NormaliseSubtypes(new_head);
01542 
01543     if (v == first && v->IsEngine() && !sell_chain && new_head != NULL && new_head->IsFrontEngine()) {
01544       /* We are selling the front engine. In this case we want to
01545        * 'give' the order, unitnumber and such to the new head. */
01546       new_head->orders.list = first->orders.list;
01547       new_head->AddToShared(first);
01548       DeleteVehicleOrders(first);
01549 
01550       /* Copy other important data from the front engine */
01551       new_head->CopyVehicleConfigAndStatistics(first);
01552       IncreaseGroupNumVehicle(new_head->group_id);
01553 
01554       /* If we deleted a window then open a new one for the 'new' train */
01555       if (IsLocalCompany() && w != NULL) ShowVehicleViewWindow(new_head);
01556     }
01557 
01558     /* We need to update the information about the train. */
01559     NormaliseTrainHead(new_head);
01560 
01561     /* We are undoubtedly changing something in the depot and train list. */
01562     InvalidateWindowData(WC_VEHICLE_DEPOT, v->tile);
01563     InvalidateWindowClassesData(WC_TRAINS_LIST, 0);
01564 
01565     /* Actually delete the sold 'goods' */
01566     delete sell_head;
01567   } else {
01568     /* We don't want to execute what we're just tried. */
01569     RestoreTrainBackup(original);
01570   }
01571 
01572   return cost;
01573 }
01574 
01575 void Train::UpdateDeltaXY(Direction direction)
01576 {
01577 #define MKIT(a, b, c, d) ((a & 0xFF) << 24) | ((b & 0xFF) << 16) | ((c & 0xFF) << 8) | ((d & 0xFF) << 0)
01578   static const uint32 _delta_xy_table[8] = {
01579     MKIT(3, 3, -1, -1),
01580     MKIT(3, 7, -1, -3),
01581     MKIT(3, 3, -1, -1),
01582     MKIT(7, 3, -3, -1),
01583     MKIT(3, 3, -1, -1),
01584     MKIT(3, 7, -1, -3),
01585     MKIT(3, 3, -1, -1),
01586     MKIT(7, 3, -3, -1),
01587   };
01588 #undef MKIT
01589 
01590   uint32 x = _delta_xy_table[direction];
01591   this->x_offs        = GB(x,  0, 8);
01592   this->y_offs        = GB(x,  8, 8);
01593   this->x_extent      = GB(x, 16, 8);
01594   this->y_extent      = GB(x, 24, 8);
01595   this->z_extent      = 6;
01596 }
01597 
01598 static inline void SetLastSpeed(Train *v, int spd)
01599 {
01600   int old = v->tcache.last_speed;
01601   if (spd != old) {
01602     v->tcache.last_speed = spd;
01603     if (_settings_client.gui.vehicle_speed || (old == 0) != (spd == 0)) {
01604       SetWindowWidgetDirty(WC_VEHICLE_VIEW, v->index, VVW_WIDGET_START_STOP_VEH);
01605     }
01606   }
01607 }
01608 
01610 static void MarkTrainAsStuck(Train *v)
01611 {
01612   if (!HasBit(v->flags, VRF_TRAIN_STUCK)) {
01613     /* It is the first time the problem occured, set the "train stuck" flag. */
01614     SetBit(v->flags, VRF_TRAIN_STUCK);
01615 
01616     /* When loading the vehicle is already stopped. No need to change that. */
01617     if (v->current_order.IsType(OT_LOADING)) return;
01618 
01619     v->time_counter = 0;
01620 
01621     /* Stop train */
01622     v->cur_speed = 0;
01623     v->subspeed = 0;
01624     SetLastSpeed(v, 0);
01625 
01626     SetWindowWidgetDirty(WC_VEHICLE_VIEW, v->index, VVW_WIDGET_START_STOP_VEH);
01627   }
01628 }
01629 
01630 static void SwapTrainFlags(uint16 *swap_flag1, uint16 *swap_flag2)
01631 {
01632   uint16 flag1 = *swap_flag1;
01633   uint16 flag2 = *swap_flag2;
01634 
01635   /* Clear the flags */
01636   ClrBit(*swap_flag1, VRF_GOINGUP);
01637   ClrBit(*swap_flag1, VRF_GOINGDOWN);
01638   ClrBit(*swap_flag2, VRF_GOINGUP);
01639   ClrBit(*swap_flag2, VRF_GOINGDOWN);
01640 
01641   /* Reverse the rail-flags (if needed) */
01642   if (HasBit(flag1, VRF_GOINGUP)) {
01643     SetBit(*swap_flag2, VRF_GOINGDOWN);
01644   } else if (HasBit(flag1, VRF_GOINGDOWN)) {
01645     SetBit(*swap_flag2, VRF_GOINGUP);
01646   }
01647   if (HasBit(flag2, VRF_GOINGUP)) {
01648     SetBit(*swap_flag1, VRF_GOINGDOWN);
01649   } else if (HasBit(flag2, VRF_GOINGDOWN)) {
01650     SetBit(*swap_flag1, VRF_GOINGUP);
01651   }
01652 }
01653 
01654 static void ReverseTrainSwapVeh(Train *v, int l, int r)
01655 {
01656   Train *a, *b;
01657 
01658   /* locate vehicles to swap */
01659   for (a = v; l != 0; l--) a = a->Next();
01660   for (b = v; r != 0; r--) b = b->Next();
01661 
01662   if (a != b) {
01663     /* swap the hidden bits */
01664     {
01665       uint16 tmp = (a->vehstatus & ~VS_HIDDEN) | (b->vehstatus&VS_HIDDEN);
01666       b->vehstatus = (b->vehstatus & ~VS_HIDDEN) | (a->vehstatus&VS_HIDDEN);
01667       a->vehstatus = tmp;
01668     }
01669 
01670     Swap(a->track, b->track);
01671     Swap(a->direction,    b->direction);
01672 
01673     /* toggle direction */
01674     if (a->track != TRACK_BIT_DEPOT) a->direction = ReverseDir(a->direction);
01675     if (b->track != TRACK_BIT_DEPOT) b->direction = ReverseDir(b->direction);
01676 
01677     Swap(a->x_pos, b->x_pos);
01678     Swap(a->y_pos, b->y_pos);
01679     Swap(a->tile,  b->tile);
01680     Swap(a->z_pos, b->z_pos);
01681 
01682     SwapTrainFlags(&a->flags, &b->flags);
01683 
01684     /* update other vars */
01685     a->UpdateViewport(true, true);
01686     b->UpdateViewport(true, true);
01687 
01688     /* call the proper EnterTile function unless we are in a wormhole */
01689     if (a->track != TRACK_BIT_WORMHOLE) VehicleEnterTile(a, a->tile, a->x_pos, a->y_pos);
01690     if (b->track != TRACK_BIT_WORMHOLE) VehicleEnterTile(b, b->tile, b->x_pos, b->y_pos);
01691   } else {
01692     if (a->track != TRACK_BIT_DEPOT) a->direction = ReverseDir(a->direction);
01693     a->UpdateViewport(true, true);
01694 
01695     if (a->track != TRACK_BIT_WORMHOLE) VehicleEnterTile(a, a->tile, a->x_pos, a->y_pos);
01696   }
01697 
01698   /* Update train's power incase tiles were different rail type */
01699   TrainPowerChanged(v);
01700 }
01701 
01702 
01708 static Vehicle *TrainOnTileEnum(Vehicle *v, void *)
01709 {
01710   return (v->type == VEH_TRAIN) ? v : NULL;
01711 }
01712 
01713 
01720 static Vehicle *TrainApproachingCrossingEnum(Vehicle *v, void *data)
01721 {
01722   if (v->type != VEH_TRAIN || (v->vehstatus & VS_CRASHED)) return NULL;
01723 
01724   Train *t = Train::From(v);
01725   if (!t->IsFrontEngine()) return NULL;
01726 
01727   TileIndex tile = *(TileIndex *)data;
01728 
01729   if (TrainApproachingCrossingTile(t) != tile) return NULL;
01730 
01731   return t;
01732 }
01733 
01734 
01741 static bool TrainApproachingCrossing(TileIndex tile)
01742 {
01743   assert(IsLevelCrossingTile(tile));
01744 
01745   DiagDirection dir = AxisToDiagDir(GetCrossingRailAxis(tile));
01746   TileIndex tile_from = tile + TileOffsByDiagDir(dir);
01747 
01748   if (HasVehicleOnPos(tile_from, &tile, &TrainApproachingCrossingEnum)) return true;
01749 
01750   dir = ReverseDiagDir(dir);
01751   tile_from = tile + TileOffsByDiagDir(dir);
01752 
01753   return HasVehicleOnPos(tile_from, &tile, &TrainApproachingCrossingEnum);
01754 }
01755 
01756 
01763 void UpdateLevelCrossing(TileIndex tile, bool sound)
01764 {
01765   assert(IsLevelCrossingTile(tile));
01766 
01767   /* train on crossing || train approaching crossing || reserved */
01768   bool new_state = HasVehicleOnPos(tile, NULL, &TrainOnTileEnum) || TrainApproachingCrossing(tile) || HasCrossingReservation(tile);
01769 
01770   if (new_state != IsCrossingBarred(tile)) {
01771     if (new_state && sound) {
01772       SndPlayTileFx(SND_0E_LEVEL_CROSSING, tile);
01773     }
01774     SetCrossingBarred(tile, new_state);
01775     MarkTileDirtyByTile(tile);
01776   }
01777 }
01778 
01779 
01785 static inline void MaybeBarCrossingWithSound(TileIndex tile)
01786 {
01787   if (!IsCrossingBarred(tile)) {
01788     BarCrossing(tile);
01789     SndPlayTileFx(SND_0E_LEVEL_CROSSING, tile);
01790     MarkTileDirtyByTile(tile);
01791   }
01792 }
01793 
01794 
01800 static void AdvanceWagonsBeforeSwap(Train *v)
01801 {
01802   Train *base = v;
01803   Train *first = base; // first vehicle to move
01804   Train *last = v->Last(); // last vehicle to move
01805   uint length = CountVehiclesInChain(v);
01806 
01807   while (length > 2) {
01808     last = last->Previous();
01809     first = first->Next();
01810 
01811     int differential = base->tcache.cached_veh_length - last->tcache.cached_veh_length;
01812 
01813     /* do not update images now
01814      * negative differential will be handled in AdvanceWagonsAfterSwap() */
01815     for (int i = 0; i < differential; i++) TrainController(first, last->Next());
01816 
01817     base = first; // == base->Next()
01818     length -= 2;
01819   }
01820 }
01821 
01822 
01828 static void AdvanceWagonsAfterSwap(Train *v)
01829 {
01830   /* first of all, fix the situation when the train was entering a depot */
01831   Train *dep = v; // last vehicle in front of just left depot
01832   while (dep->Next() != NULL && (dep->track == TRACK_BIT_DEPOT || dep->Next()->track != TRACK_BIT_DEPOT)) {
01833     dep = dep->Next(); // find first vehicle outside of a depot, with next vehicle inside a depot
01834   }
01835 
01836   Train *leave = dep->Next(); // first vehicle in a depot we are leaving now
01837 
01838   if (leave != NULL) {
01839     /* 'pull' next wagon out of the depot, so we won't miss it (it could stay in depot forever) */
01840     int d = TicksToLeaveDepot(dep);
01841 
01842     if (d <= 0) {
01843       leave->vehstatus &= ~VS_HIDDEN; // move it out of the depot
01844       leave->track = TrackToTrackBits(GetRailDepotTrack(leave->tile));
01845       for (int i = 0; i >= d; i--) TrainController(leave, NULL); // maybe move it, and maybe let another wagon leave
01846     }
01847   } else {
01848     dep = NULL; // no vehicle in a depot, so no vehicle leaving a depot
01849   }
01850 
01851   Train *base = v;
01852   Train *first = base; // first vehicle to move
01853   Train *last = v->Last(); // last vehicle to move
01854   uint length = CountVehiclesInChain(v);
01855 
01856   /* we have to make sure all wagons that leave a depot because of train reversing are moved coorectly
01857    * they have already correct spacing, so we have to make sure they are moved how they should */
01858   bool nomove = (dep == NULL); // if there is no vehicle leaving a depot, limit the number of wagons moved immediatelly
01859 
01860   while (length > 2) {
01861     /* we reached vehicle (originally) in front of a depot, stop now
01862      * (we would move wagons that are alredy moved with new wagon length) */
01863     if (base == dep) break;
01864 
01865     /* the last wagon was that one leaving a depot, so do not move it anymore */
01866     if (last == dep) nomove = true;
01867 
01868     last = last->Previous();
01869     first = first->Next();
01870 
01871     int differential = last->tcache.cached_veh_length - base->tcache.cached_veh_length;
01872 
01873     /* do not update images now */
01874     for (int i = 0; i < differential; i++) TrainController(first, (nomove ? last->Next() : NULL));
01875 
01876     base = first; // == base->Next()
01877     length -= 2;
01878   }
01879 }
01880 
01881 
01882 static void ReverseTrainDirection(Train *v)
01883 {
01884   if (IsRailDepotTile(v->tile)) {
01885     InvalidateWindowData(WC_VEHICLE_DEPOT, v->tile);
01886   }
01887 
01888   /* Clear path reservation in front if train is not stuck. */
01889   if (!HasBit(v->flags, VRF_TRAIN_STUCK)) FreeTrainTrackReservation(v);
01890 
01891   /* Check if we were approaching a rail/road-crossing */
01892   TileIndex crossing = TrainApproachingCrossingTile(v);
01893 
01894   /* count number of vehicles */
01895   int r = CountVehiclesInChain(v) - 1;  // number of vehicles - 1
01896 
01897   AdvanceWagonsBeforeSwap(v);
01898 
01899   /* swap start<>end, start+1<>end-1, ... */
01900   int l = 0;
01901   do {
01902     ReverseTrainSwapVeh(v, l++, r--);
01903   } while (l <= r);
01904 
01905   AdvanceWagonsAfterSwap(v);
01906 
01907   if (IsRailDepotTile(v->tile)) {
01908     InvalidateWindowData(WC_VEHICLE_DEPOT, v->tile);
01909   }
01910 
01911   ToggleBit(v->flags, VRF_TOGGLE_REVERSE);
01912 
01913   ClrBit(v->flags, VRF_REVERSING);
01914 
01915   /* recalculate cached data */
01916   TrainConsistChanged(v, true);
01917 
01918   /* update all images */
01919   for (Vehicle *u = v; u != NULL; u = u->Next()) u->UpdateViewport(false, false);
01920 
01921   /* update crossing we were approaching */
01922   if (crossing != INVALID_TILE) UpdateLevelCrossing(crossing);
01923 
01924   /* maybe we are approaching crossing now, after reversal */
01925   crossing = TrainApproachingCrossingTile(v);
01926   if (crossing != INVALID_TILE) MaybeBarCrossingWithSound(crossing);
01927 
01928   /* If we are inside a depot after reversing, don't bother with path reserving. */
01929   if (v->track == TRACK_BIT_DEPOT) {
01930     /* Can't be stuck here as inside a depot is always a safe tile. */
01931     if (HasBit(v->flags, VRF_TRAIN_STUCK)) SetWindowWidgetDirty(WC_VEHICLE_VIEW, v->index, VVW_WIDGET_START_STOP_VEH);
01932     ClrBit(v->flags, VRF_TRAIN_STUCK);
01933     return;
01934   }
01935 
01936   /* TrainExitDir does not always produce the desired dir for depots and
01937    * tunnels/bridges that is needed for UpdateSignalsOnSegment. */
01938   DiagDirection dir = TrainExitDir(v->direction, v->track);
01939   if (IsRailDepotTile(v->tile) || IsTileType(v->tile, MP_TUNNELBRIDGE)) dir = INVALID_DIAGDIR;
01940 
01941   if (UpdateSignalsOnSegment(v->tile, dir, v->owner) == SIGSEG_PBS || _settings_game.pf.reserve_paths) {
01942     /* If we are currently on a tile with conventional signals, we can't treat the
01943      * current tile as a safe tile or we would enter a PBS block without a reservation. */
01944     bool first_tile_okay = !(IsTileType(v->tile, MP_RAILWAY) &&
01945       HasSignalOnTrackdir(v->tile, v->GetVehicleTrackdir()) &&
01946       !IsPbsSignal(GetSignalType(v->tile, FindFirstTrack(v->track))));
01947 
01948     if (IsRailStationTile(v->tile)) SetRailStationPlatformReservation(v->tile, TrackdirToExitdir(v->GetVehicleTrackdir()), true);
01949     if (TryPathReserve(v, false, first_tile_okay)) {
01950       /* Do a look-ahead now in case our current tile was already a safe tile. */
01951       CheckNextTrainTile(v);
01952     } else if (v->current_order.GetType() != OT_LOADING) {
01953       /* Do not wait for a way out when we're still loading */
01954       MarkTrainAsStuck(v);
01955     }
01956   } else if (HasBit(v->flags, VRF_TRAIN_STUCK)) {
01957     /* A train not inside a PBS block can't be stuck. */
01958     ClrBit(v->flags, VRF_TRAIN_STUCK);
01959     v->time_counter = 0;
01960   }
01961 }
01962 
01971 CommandCost CmdReverseTrainDirection(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
01972 {
01973   Train *v = Train::GetIfValid(p1);
01974   if (v == NULL || !CheckOwnership(v->owner)) return CMD_ERROR;
01975 
01976   if (p2 != 0) {
01977     /* turn a single unit around */
01978 
01979     if (v->IsMultiheaded() || HasBit(EngInfo(v->engine_type)->callback_mask, CBM_VEHICLE_ARTIC_ENGINE)) {
01980       return_cmd_error(STR_ERROR_CAN_T_REVERSE_DIRECTION_RAIL_VEHICLE_MULTIPLE_UNITS);
01981     }
01982 
01983     Train *front = v->First();
01984     /* make sure the vehicle is stopped in the depot */
01985     if (!front->IsStoppedInDepot()) {
01986       return_cmd_error(STR_ERROR_TRAINS_CAN_ONLY_BE_ALTERED_INSIDE_A_DEPOT);
01987     }
01988 
01989     if (flags & DC_EXEC) {
01990       ToggleBit(v->flags, VRF_REVERSE_DIRECTION);
01991       SetWindowDirty(WC_VEHICLE_DEPOT, v->tile);
01992       SetWindowDirty(WC_VEHICLE_DETAILS, v->index);
01993       /* We cancel any 'skip signal at dangers' here */
01994       v->force_proceed = 0;
01995       SetWindowDirty(WC_VEHICLE_VIEW, v->index);
01996     }
01997   } else {
01998     /* turn the whole train around */
01999     if ((v->vehstatus & VS_CRASHED) || v->breakdown_ctr != 0) return CMD_ERROR;
02000 
02001     if (flags & DC_EXEC) {
02002       /* Properly leave the station if we are loading and won't be loading anymore */
02003       if (v->current_order.IsType(OT_LOADING)) {
02004         const Vehicle *last = v;
02005         while (last->Next() != NULL) last = last->Next();
02006 
02007         /* not a station || different station --> leave the station */
02008         if (!IsTileType(last->tile, MP_STATION) || GetStationIndex(last->tile) != GetStationIndex(v->tile)) {
02009           v->LeaveStation();
02010         }
02011       }
02012 
02013       /* We cancel any 'skip signal at dangers' here */
02014       v->force_proceed = 0;
02015       SetWindowDirty(WC_VEHICLE_VIEW, v->index);
02016 
02017       if (_settings_game.vehicle.train_acceleration_model != TAM_ORIGINAL && v->cur_speed != 0) {
02018         ToggleBit(v->flags, VRF_REVERSING);
02019       } else {
02020         v->cur_speed = 0;
02021         SetLastSpeed(v, 0);
02022         HideFillingPercent(&v->fill_percent_te_id);
02023         ReverseTrainDirection(v);
02024       }
02025     }
02026   }
02027   return CommandCost();
02028 }
02029 
02038 CommandCost CmdForceTrainProceed(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
02039 {
02040   Train *t = Train::GetIfValid(p1);
02041   if (t == NULL || !CheckOwnership(t->owner)) return CMD_ERROR;
02042 
02043   if (flags & DC_EXEC) {
02044     /* If we are forced to proceed, cancel that order.
02045      * If we are marked stuck we would want to force the train
02046      * to proceed to the next signal. In the other cases we
02047      * would like to pass the signal at danger and run till the
02048      * next signal we encounter. */
02049     t->force_proceed = t->force_proceed == 2 ? 0 : HasBit(t->flags, VRF_TRAIN_STUCK) || t->IsInDepot() ? 1 : 2;
02050     SetWindowDirty(WC_VEHICLE_VIEW, t->index);
02051   }
02052 
02053   return CommandCost();
02054 }
02055 
02067 CommandCost CmdRefitRailVehicle(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
02068 {
02069   CargoID new_cid = GB(p2, 0, 8);
02070   byte new_subtype = GB(p2, 8, 8);
02071   bool only_this = HasBit(p2, 16);
02072 
02073   Train *v = Train::GetIfValid(p1);
02074   if (v == NULL || !CheckOwnership(v->owner)) return CMD_ERROR;
02075 
02076   if (!v->IsStoppedInDepot()) return_cmd_error(STR_TRAIN_MUST_BE_STOPPED);
02077   if (v->vehstatus & VS_CRASHED) return_cmd_error(STR_ERROR_CAN_T_REFIT_DESTROYED_VEHICLE);
02078 
02079   /* Check cargo */
02080   if (new_cid >= NUM_CARGO) return CMD_ERROR;
02081 
02082   CommandCost cost = RefitVehicle(v, only_this, new_cid, new_subtype, flags);
02083 
02084   /* Update the train's cached variables */
02085   if (flags & DC_EXEC) {
02086     Train *front = v->First();
02087     TrainConsistChanged(front, false);
02088     SetWindowDirty(WC_VEHICLE_DETAILS, front->index);
02089     SetWindowDirty(WC_VEHICLE_DEPOT, front->tile);
02090     InvalidateWindowClassesData(WC_TRAINS_LIST, 0);
02091   } else {
02092     v->InvalidateNewGRFCacheOfChain(); // always invalidate; querycost might have filled it
02093   }
02094 
02095   return cost;
02096 }
02097 
02100 static FindDepotData FindClosestTrainDepot(Train *v, int max_distance)
02101 {
02102   assert(!(v->vehstatus & VS_CRASHED));
02103 
02104   if (IsRailDepotTile(v->tile)) return FindDepotData(v->tile, 0);
02105 
02106   PBSTileInfo origin = FollowTrainReservation(v);
02107   if (IsRailDepotTile(origin.tile)) return FindDepotData(origin.tile, 0);
02108 
02109   switch (_settings_game.pf.pathfinder_for_trains) {
02110     case VPF_NPF: return NPFTrainFindNearestDepot(v, max_distance);
02111     case VPF_YAPF: return YapfTrainFindNearestDepot(v, max_distance);
02112 
02113     default: NOT_REACHED();
02114   }
02115 }
02116 
02117 bool Train::FindClosestDepot(TileIndex *location, DestinationID *destination, bool *reverse)
02118 {
02119   FindDepotData tfdd = FindClosestTrainDepot(this, 0);
02120   if (tfdd.best_length == UINT_MAX) return false;
02121 
02122   if (location    != NULL) *location    = tfdd.tile;
02123   if (destination != NULL) *destination = GetDepotIndex(tfdd.tile);
02124   if (reverse     != NULL) *reverse     = tfdd.reverse;
02125 
02126   return true;
02127 }
02128 
02139 CommandCost CmdSendTrainToDepot(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
02140 {
02141   if (p2 & DEPOT_MASS_SEND) {
02142     /* Mass goto depot requested */
02143     if (!ValidVLWFlags(p2 & VLW_MASK)) return CMD_ERROR;
02144     return SendAllVehiclesToDepot(VEH_TRAIN, flags, p2 & DEPOT_SERVICE, _current_company, (p2 & VLW_MASK), p1);
02145   }
02146 
02147   Train *v = Train::GetIfValid(p1);
02148   if (v == NULL) return CMD_ERROR;
02149 
02150   return v->SendToDepot(flags, (DepotCommand)(p2 & DEPOT_COMMAND_MASK));
02151 }
02152 
02153 static const int8 _vehicle_smoke_pos[8] = {
02154   1, 1, 1, 0, -1, -1, -1, 0
02155 };
02156 
02157 static void HandleLocomotiveSmokeCloud(const Train *v)
02158 {
02159   bool sound = false;
02160 
02161   if ((v->vehstatus & VS_TRAIN_SLOWING) || v->cur_speed < 2) {
02162     return;
02163   }
02164 
02165   const Train *u = v;
02166 
02167   do {
02168     const RailVehicleInfo *rvi = RailVehInfo(v->engine_type);
02169     int effect_offset = GB(v->tcache.cached_vis_effect, 0, 4) - 8;
02170     byte effect_type = GB(v->tcache.cached_vis_effect, 4, 2);
02171     bool disable_effect = HasBit(v->tcache.cached_vis_effect, 6);
02172 
02173     /* no smoke? */
02174     if ((rvi->railveh_type == RAILVEH_WAGON && effect_type == 0) ||
02175         disable_effect ||
02176         v->vehstatus & VS_HIDDEN) {
02177       continue;
02178     }
02179 
02180     /* No smoke in depots or tunnels */
02181     if (IsRailDepotTile(v->tile) || IsTunnelTile(v->tile)) continue;
02182 
02183     /* No sparks for electric vehicles on nonelectrified tracks */
02184     if (!HasPowerOnRail(v->railtype, GetTileRailType(v->tile))) continue;
02185 
02186     if (effect_type == 0) {
02187       /* Use default effect type for engine class. */
02188       effect_type = rvi->engclass;
02189     } else {
02190       effect_type--;
02191     }
02192 
02193     int x = _vehicle_smoke_pos[v->direction] * effect_offset;
02194     int y = _vehicle_smoke_pos[(v->direction + 2) % 8] * effect_offset;
02195 
02196     if (HasBit(v->flags, VRF_REVERSE_DIRECTION)) {
02197       x = -x;
02198       y = -y;
02199     }
02200 
02201     switch (effect_type) {
02202       case 0:
02203         /* steam smoke. */
02204         if (GB(v->tick_counter, 0, 4) == 0) {
02205           CreateEffectVehicleRel(v, x, y, 10, EV_STEAM_SMOKE);
02206           sound = true;
02207         }
02208         break;
02209 
02210       case 1:
02211         /* diesel smoke */
02212         if (u->cur_speed <= 40 && Chance16(15, 128)) {
02213           CreateEffectVehicleRel(v, 0, 0, 10, EV_DIESEL_SMOKE);
02214           sound = true;
02215         }
02216         break;
02217 
02218       case 2:
02219         /* blue spark */
02220         if (GB(v->tick_counter, 0, 2) == 0 && Chance16(1, 45)) {
02221           CreateEffectVehicleRel(v, 0, 0, 10, EV_ELECTRIC_SPARK);
02222           sound = true;
02223         }
02224         break;
02225 
02226       default:
02227         break;
02228     }
02229   } while ((v = v->Next()) != NULL);
02230 
02231   if (sound) PlayVehicleSound(u, VSE_TRAIN_EFFECT);
02232 }
02233 
02234 void Train::PlayLeaveStationSound() const
02235 {
02236   static const SoundFx sfx[] = {
02237     SND_04_TRAIN,
02238     SND_0A_TRAIN_HORN,
02239     SND_0A_TRAIN_HORN,
02240     SND_47_MAGLEV_2,
02241     SND_41_MAGLEV
02242   };
02243 
02244   if (PlayVehicleSound(this, VSE_START)) return;
02245 
02246   EngineID engtype = this->engine_type;
02247   SndPlayVehicleFx(sfx[RailVehInfo(engtype)->engclass], this);
02248 }
02249 
02251 static void CheckNextTrainTile(Train *v)
02252 {
02253   /* Don't do any look-ahead if path_backoff_interval is 255. */
02254   if (_settings_game.pf.path_backoff_interval == 255) return;
02255 
02256   /* Exit if we reached our destination depot or are inside a depot. */
02257   if ((v->tile == v->dest_tile && v->current_order.IsType(OT_GOTO_DEPOT)) || v->track == TRACK_BIT_DEPOT) return;
02258   /* Exit if we are on a station tile and are going to stop. */
02259   if (IsRailStationTile(v->tile) && v->current_order.ShouldStopAtStation(v, GetStationIndex(v->tile))) return;
02260   /* Exit if the current order doesn't have a destination, but the train has orders. */
02261   if ((v->current_order.IsType(OT_NOTHING) || v->current_order.IsType(OT_LEAVESTATION) || v->current_order.IsType(OT_LOADING)) && v->GetNumOrders() > 0) return;
02262 
02263   Trackdir td = v->GetVehicleTrackdir();
02264 
02265   /* On a tile with a red non-pbs signal, don't look ahead. */
02266   if (IsTileType(v->tile, MP_RAILWAY) && HasSignalOnTrackdir(v->tile, td) &&
02267       !IsPbsSignal(GetSignalType(v->tile, TrackdirToTrack(td))) &&
02268       GetSignalStateByTrackdir(v->tile, td) == SIGNAL_STATE_RED) return;
02269 
02270   CFollowTrackRail ft(v);
02271   if (!ft.Follow(v->tile, td)) return;
02272 
02273   if (!HasReservedTracks(ft.m_new_tile, TrackdirBitsToTrackBits(ft.m_new_td_bits))) {
02274     /* Next tile is not reserved. */
02275     if (KillFirstBit(ft.m_new_td_bits) == TRACKDIR_BIT_NONE) {
02276       if (HasPbsSignalOnTrackdir(ft.m_new_tile, FindFirstTrackdir(ft.m_new_td_bits))) {
02277         /* If the next tile is a PBS signal, try to make a reservation. */
02278         TrackBits tracks = TrackdirBitsToTrackBits(ft.m_new_td_bits);
02279         if (_settings_game.pf.forbid_90_deg) {
02280           tracks &= ~TrackCrossesTracks(TrackdirToTrack(ft.m_old_td));
02281         }
02282         ChooseTrainTrack(v, ft.m_new_tile, ft.m_exitdir, tracks, false, NULL, false);
02283       }
02284     }
02285   }
02286 }
02287 
02288 static bool CheckTrainStayInDepot(Train *v)
02289 {
02290   /* bail out if not all wagons are in the same depot or not in a depot at all */
02291   for (const Train *u = v; u != NULL; u = u->Next()) {
02292     if (u->track != TRACK_BIT_DEPOT || u->tile != v->tile) return false;
02293   }
02294 
02295   /* if the train got no power, then keep it in the depot */
02296   if (v->tcache.cached_power == 0) {
02297     v->vehstatus |= VS_STOPPED;
02298     SetWindowDirty(WC_VEHICLE_DEPOT, v->tile);
02299     return true;
02300   }
02301 
02302   SigSegState seg_state;
02303 
02304   if (v->force_proceed == 0) {
02305     /* force proceed was not pressed */
02306     if (++v->time_counter < 37) {
02307       SetWindowClassesDirty(WC_TRAINS_LIST);
02308       return true;
02309     }
02310 
02311     v->time_counter = 0;
02312 
02313     seg_state = _settings_game.pf.reserve_paths ? SIGSEG_PBS : UpdateSignalsOnSegment(v->tile, INVALID_DIAGDIR, v->owner);
02314     if (seg_state == SIGSEG_FULL || HasDepotReservation(v->tile)) {
02315       /* Full and no PBS signal in block or depot reserved, can't exit. */
02316       SetWindowClassesDirty(WC_TRAINS_LIST);
02317       return true;
02318     }
02319   } else {
02320     seg_state = _settings_game.pf.reserve_paths ? SIGSEG_PBS : UpdateSignalsOnSegment(v->tile, INVALID_DIAGDIR, v->owner);
02321   }
02322 
02323   /* We are leaving a depot, but have to go to the exact same one; re-enter */
02324   if (v->current_order.IsType(OT_GOTO_DEPOT) && v->tile == v->dest_tile) {
02325     /* We need to have a reservation for this to work. */
02326     if (HasDepotReservation(v->tile)) return true;
02327     SetDepotReservation(v->tile, true);
02328     VehicleEnterDepot(v);
02329     return true;
02330   }
02331 
02332   /* Only leave when we can reserve a path to our destination. */
02333   if (seg_state == SIGSEG_PBS && !TryPathReserve(v) && v->force_proceed == 0) {
02334     /* No path and no force proceed. */
02335     SetWindowClassesDirty(WC_TRAINS_LIST);
02336     MarkTrainAsStuck(v);
02337     return true;
02338   }
02339 
02340   SetDepotReservation(v->tile, true);
02341   if (_settings_client.gui.show_track_reservation) MarkTileDirtyByTile(v->tile);
02342 
02343   VehicleServiceInDepot(v);
02344   SetWindowClassesDirty(WC_TRAINS_LIST);
02345   v->PlayLeaveStationSound();
02346 
02347   v->track = TRACK_BIT_X;
02348   if (v->direction & 2) v->track = TRACK_BIT_Y;
02349 
02350   v->vehstatus &= ~VS_HIDDEN;
02351   v->cur_speed = 0;
02352 
02353   v->UpdateDeltaXY(v->direction);
02354   v->cur_image = v->GetImage(v->direction);
02355   VehicleMove(v, false);
02356   UpdateSignalsOnSegment(v->tile, INVALID_DIAGDIR, v->owner);
02357   UpdateTrainAcceleration(v);
02358   InvalidateWindowData(WC_VEHICLE_DEPOT, v->tile);
02359 
02360   return false;
02361 }
02362 
02364 static void ClearPathReservation(const Train *v, TileIndex tile, Trackdir track_dir)
02365 {
02366   DiagDirection dir = TrackdirToExitdir(track_dir);
02367 
02368   if (IsTileType(tile, MP_TUNNELBRIDGE)) {
02369     /* Are we just leaving a tunnel/bridge? */
02370     if (GetTunnelBridgeDirection(tile) == ReverseDiagDir(dir)) {
02371       TileIndex end = GetOtherTunnelBridgeEnd(tile);
02372 
02373       if (!HasVehicleOnTunnelBridge(tile, end, v)) {
02374         /* Free the reservation only if no other train is on the tiles. */
02375         SetTunnelBridgeReservation(tile, false);
02376         SetTunnelBridgeReservation(end, false);
02377 
02378         if (_settings_client.gui.show_track_reservation) {
02379           MarkTileDirtyByTile(tile);
02380           MarkTileDirtyByTile(end);
02381         }
02382       }
02383     }
02384   } else if (IsRailStationTile(tile)) {
02385     TileIndex new_tile = TileAddByDiagDir(tile, dir);
02386     /* If the new tile is not a further tile of the same station, we
02387      * clear the reservation for the whole platform. */
02388     if (!IsCompatibleTrainStationTile(new_tile, tile)) {
02389       SetRailStationPlatformReservation(tile, ReverseDiagDir(dir), false);
02390     }
02391   } else {
02392     /* Any other tile */
02393     UnreserveRailTrack(tile, TrackdirToTrack(track_dir));
02394   }
02395 }
02396 
02398 void FreeTrainTrackReservation(const Train *v, TileIndex origin, Trackdir orig_td)
02399 {
02400   assert(v->IsFrontEngine());
02401 
02402   TileIndex tile = origin != INVALID_TILE ? origin : v->tile;
02403   Trackdir  td = orig_td != INVALID_TRACKDIR ? orig_td : v->GetVehicleTrackdir();
02404   bool      free_tile = tile != v->tile || !(IsRailStationTile(v->tile) || IsTileType(v->tile, MP_TUNNELBRIDGE));
02405   StationID station_id = IsRailStationTile(v->tile) ? GetStationIndex(v->tile) : INVALID_STATION;
02406 
02407   /* Don't free reservation if it's not ours. */
02408   if (TracksOverlap(GetReservedTrackbits(tile) | TrackToTrackBits(TrackdirToTrack(td)))) return;
02409 
02410   CFollowTrackRail ft(v, GetRailTypeInfo(v->railtype)->compatible_railtypes);
02411   while (ft.Follow(tile, td)) {
02412     tile = ft.m_new_tile;
02413     TrackdirBits bits = ft.m_new_td_bits & TrackBitsToTrackdirBits(GetReservedTrackbits(tile));
02414     td = RemoveFirstTrackdir(&bits);
02415     assert(bits == TRACKDIR_BIT_NONE);
02416 
02417     if (!IsValidTrackdir(td)) break;
02418 
02419     if (IsTileType(tile, MP_RAILWAY)) {
02420       if (HasSignalOnTrackdir(tile, td) && !IsPbsSignal(GetSignalType(tile, TrackdirToTrack(td)))) {
02421         /* Conventional signal along trackdir: remove reservation and stop. */
02422         UnreserveRailTrack(tile, TrackdirToTrack(td));
02423         break;
02424       }
02425       if (HasPbsSignalOnTrackdir(tile, td)) {
02426         if (GetSignalStateByTrackdir(tile, td) == SIGNAL_STATE_RED) {
02427           /* Red PBS signal? Can't be our reservation, would be green then. */
02428           break;
02429         } else {
02430           /* Turn the signal back to red. */
02431           SetSignalStateByTrackdir(tile, td, SIGNAL_STATE_RED);
02432           MarkTileDirtyByTile(tile);
02433         }
02434       } else if (HasSignalOnTrackdir(tile, ReverseTrackdir(td)) && IsOnewaySignal(tile, TrackdirToTrack(td))) {
02435         break;
02436       }
02437     }
02438 
02439     /* Don't free first station/bridge/tunnel if we are on it. */
02440     if (free_tile || (!(ft.m_is_station && GetStationIndex(ft.m_new_tile) == station_id) && !ft.m_is_tunnel && !ft.m_is_bridge)) ClearPathReservation(v, tile, td);
02441 
02442     free_tile = true;
02443   }
02444 }
02445 
02446 static const byte _initial_tile_subcoord[6][4][3] = {
02447 {{ 15, 8, 1 }, { 0, 0, 0 }, { 0, 8, 5 }, { 0,  0, 0 }},
02448 {{  0, 0, 0 }, { 8, 0, 3 }, { 0, 0, 0 }, { 8, 15, 7 }},
02449 {{  0, 0, 0 }, { 7, 0, 2 }, { 0, 7, 6 }, { 0,  0, 0 }},
02450 {{ 15, 8, 2 }, { 0, 0, 0 }, { 0, 0, 0 }, { 8, 15, 6 }},
02451 {{ 15, 7, 0 }, { 8, 0, 4 }, { 0, 0, 0 }, { 0,  0, 0 }},
02452 {{  0, 0, 0 }, { 0, 0, 0 }, { 0, 8, 4 }, { 7, 15, 0 }},
02453 };
02454 
02467 static Track DoTrainPathfind(const Train *v, TileIndex tile, DiagDirection enterdir, TrackBits tracks, bool *path_not_found, bool do_track_reservation, PBSTileInfo *dest)
02468 {
02469   switch (_settings_game.pf.pathfinder_for_trains) {
02470     case VPF_NPF: return NPFTrainChooseTrack(v, tile, enterdir, tracks, path_not_found, do_track_reservation, dest);
02471     case VPF_YAPF: return YapfTrainChooseTrack(v, tile, enterdir, tracks, path_not_found, do_track_reservation, dest);
02472 
02473     default: NOT_REACHED();
02474   }
02475 }
02476 
02482 static PBSTileInfo ExtendTrainReservation(const Train *v, TrackBits *new_tracks, DiagDirection *enterdir)
02483 {
02484   PBSTileInfo origin = FollowTrainReservation(v);
02485 
02486   CFollowTrackRail ft(v);
02487 
02488   TileIndex tile = origin.tile;
02489   Trackdir  cur_td = origin.trackdir;
02490   while (ft.Follow(tile, cur_td)) {
02491     if (KillFirstBit(ft.m_new_td_bits) == TRACKDIR_BIT_NONE) {
02492       /* Possible signal tile. */
02493       if (HasOnewaySignalBlockingTrackdir(ft.m_new_tile, FindFirstTrackdir(ft.m_new_td_bits))) break;
02494     }
02495 
02496     if (_settings_game.pf.forbid_90_deg) {
02497       ft.m_new_td_bits &= ~TrackdirCrossesTrackdirs(ft.m_old_td);
02498       if (ft.m_new_td_bits == TRACKDIR_BIT_NONE) break;
02499     }
02500 
02501     /* Station, depot or waypoint are a possible target. */
02502     bool target_seen = ft.m_is_station || (IsTileType(ft.m_new_tile, MP_RAILWAY) && !IsPlainRail(ft.m_new_tile));
02503     if (target_seen || KillFirstBit(ft.m_new_td_bits) != TRACKDIR_BIT_NONE) {
02504       /* Choice found or possible target encountered.
02505        * On finding a possible target, we need to stop and let the pathfinder handle the
02506        * remaining path. This is because we don't know if this target is in one of our
02507        * orders, so we might cause pathfinding to fail later on if we find a choice.
02508        * This failure would cause a bogous call to TryReserveSafePath which might reserve
02509        * a wrong path not leading to our next destination. */
02510       if (HasReservedTracks(ft.m_new_tile, TrackdirBitsToTrackBits(TrackdirReachesTrackdirs(ft.m_old_td)))) break;
02511 
02512       /* If we did skip some tiles, backtrack to the first skipped tile so the pathfinder
02513        * actually starts its search at the first unreserved tile. */
02514       if (ft.m_tiles_skipped != 0) ft.m_new_tile -= TileOffsByDiagDir(ft.m_exitdir) * ft.m_tiles_skipped;
02515 
02516       /* Choice found, path valid but not okay. Save info about the choice tile as well. */
02517       if (new_tracks) *new_tracks = TrackdirBitsToTrackBits(ft.m_new_td_bits);
02518       if (enterdir) *enterdir = ft.m_exitdir;
02519       return PBSTileInfo(ft.m_new_tile, ft.m_old_td, false);
02520     }
02521 
02522     tile = ft.m_new_tile;
02523     cur_td = FindFirstTrackdir(ft.m_new_td_bits);
02524 
02525     if (IsSafeWaitingPosition(v, tile, cur_td, true, _settings_game.pf.forbid_90_deg)) {
02526       bool wp_free = IsWaitingPositionFree(v, tile, cur_td, _settings_game.pf.forbid_90_deg);
02527       if (!(wp_free && TryReserveRailTrack(tile, TrackdirToTrack(cur_td)))) break;
02528       /* Safe position is all good, path valid and okay. */
02529       return PBSTileInfo(tile, cur_td, true);
02530     }
02531 
02532     if (!TryReserveRailTrack(tile, TrackdirToTrack(cur_td))) break;
02533   }
02534 
02535   if (ft.m_err == CFollowTrackRail::EC_OWNER || ft.m_err == CFollowTrackRail::EC_NO_WAY) {
02536     /* End of line, path valid and okay. */
02537     return PBSTileInfo(ft.m_old_tile, ft.m_old_td, true);
02538   }
02539 
02540   /* Sorry, can't reserve path, back out. */
02541   tile = origin.tile;
02542   cur_td = origin.trackdir;
02543   TileIndex stopped = ft.m_old_tile;
02544   Trackdir  stopped_td = ft.m_old_td;
02545   while (tile != stopped || cur_td != stopped_td) {
02546     if (!ft.Follow(tile, cur_td)) break;
02547 
02548     if (_settings_game.pf.forbid_90_deg) {
02549       ft.m_new_td_bits &= ~TrackdirCrossesTrackdirs(ft.m_old_td);
02550       assert(ft.m_new_td_bits != TRACKDIR_BIT_NONE);
02551     }
02552     assert(KillFirstBit(ft.m_new_td_bits) == TRACKDIR_BIT_NONE);
02553 
02554     tile = ft.m_new_tile;
02555     cur_td = FindFirstTrackdir(ft.m_new_td_bits);
02556 
02557     UnreserveRailTrack(tile, TrackdirToTrack(cur_td));
02558   }
02559 
02560   /* Path invalid. */
02561   return PBSTileInfo();
02562 }
02563 
02574 static bool TryReserveSafeTrack(const Train *v, TileIndex tile, Trackdir td, bool override_tailtype)
02575 {
02576   switch (_settings_game.pf.pathfinder_for_trains) {
02577     case VPF_NPF: return NPFTrainFindNearestSafeTile(v, tile, td, override_tailtype);
02578     case VPF_YAPF: return YapfTrainFindNearestSafeTile(v, tile, td, override_tailtype);
02579 
02580     default: NOT_REACHED();
02581   }
02582 }
02583 
02585 class VehicleOrderSaver
02586 {
02587 private:
02588   Vehicle        *v;
02589   Order          old_order;
02590   TileIndex      old_dest_tile;
02591   StationID      old_last_station_visited;
02592   VehicleOrderID index;
02593 
02594 public:
02595   VehicleOrderSaver(Vehicle *_v) :
02596     v(_v),
02597     old_order(_v->current_order),
02598     old_dest_tile(_v->dest_tile),
02599     old_last_station_visited(_v->last_station_visited),
02600     index(_v->cur_order_index)
02601   {
02602   }
02603 
02604   ~VehicleOrderSaver()
02605   {
02606     this->v->current_order = this->old_order;
02607     this->v->dest_tile = this->old_dest_tile;
02608     this->v->last_station_visited = this->old_last_station_visited;
02609   }
02610 
02616   bool SwitchToNextOrder(bool skip_first)
02617   {
02618     if (this->v->GetNumOrders() == 0) return false;
02619 
02620     if (skip_first) ++this->index;
02621 
02622     int conditional_depth = 0;
02623 
02624     do {
02625       /* Wrap around. */
02626       if (this->index >= this->v->GetNumOrders()) this->index = 0;
02627 
02628       Order *order = this->v->GetOrder(this->index);
02629       assert(order != NULL);
02630 
02631       switch (order->GetType()) {
02632         case OT_GOTO_DEPOT:
02633           /* Skip service in depot orders when the train doesn't need service. */
02634           if ((order->GetDepotOrderType() & ODTFB_SERVICE) && !this->v->NeedsServicing()) break;
02635         case OT_GOTO_STATION:
02636         case OT_GOTO_WAYPOINT:
02637           this->v->current_order = *order;
02638           UpdateOrderDest(this->v, order);
02639           return true;
02640         case OT_CONDITIONAL: {
02641           if (conditional_depth > this->v->GetNumOrders()) return false;
02642           VehicleOrderID next = ProcessConditionalOrder(order, this->v);
02643           if (next != INVALID_VEH_ORDER_ID) {
02644             conditional_depth++;
02645             this->index = next;
02646             /* Don't increment next, so no break here. */
02647             continue;
02648           }
02649           break;
02650         }
02651         default:
02652           break;
02653       }
02654       /* Don't increment inside the while because otherwise conditional
02655        * orders can lead to an infinite loop. */
02656       ++this->index;
02657     } while (this->index != this->v->cur_order_index);
02658 
02659     return false;
02660   }
02661 };
02662 
02663 /* choose a track */
02664 static Track ChooseTrainTrack(Train *v, TileIndex tile, DiagDirection enterdir, TrackBits tracks, bool force_res, bool *got_reservation, bool mark_stuck)
02665 {
02666   Track best_track = INVALID_TRACK;
02667   bool do_track_reservation = _settings_game.pf.reserve_paths || force_res;
02668   bool changed_signal = false;
02669 
02670   assert((tracks & ~TRACK_BIT_MASK) == 0);
02671 
02672   if (got_reservation != NULL) *got_reservation = false;
02673 
02674   /* Don't use tracks here as the setting to forbid 90 deg turns might have been switched between reservation and now. */
02675   TrackBits res_tracks = (TrackBits)(GetReservedTrackbits(tile) & DiagdirReachesTracks(enterdir));
02676   /* Do we have a suitable reserved track? */
02677   if (res_tracks != TRACK_BIT_NONE) return FindFirstTrack(res_tracks);
02678 
02679   /* Quick return in case only one possible track is available */
02680   if (KillFirstBit(tracks) == TRACK_BIT_NONE) {
02681     Track track = FindFirstTrack(tracks);
02682     /* We need to check for signals only here, as a junction tile can't have signals. */
02683     if (track != INVALID_TRACK && HasPbsSignalOnTrackdir(tile, TrackEnterdirToTrackdir(track, enterdir))) {
02684       do_track_reservation = true;
02685       changed_signal = true;
02686       SetSignalStateByTrackdir(tile, TrackEnterdirToTrackdir(track, enterdir), SIGNAL_STATE_GREEN);
02687     } else if (!do_track_reservation) {
02688       return track;
02689     }
02690     best_track = track;
02691   }
02692 
02693   PBSTileInfo   res_dest(tile, INVALID_TRACKDIR, false);
02694   DiagDirection dest_enterdir = enterdir;
02695   if (do_track_reservation) {
02696     /* Check if the train needs service here, so it has a chance to always find a depot.
02697      * Also check if the current order is a service order so we don't reserve a path to
02698      * the destination but instead to the next one if service isn't needed. */
02699     CheckIfTrainNeedsService(v);
02700     if (v->current_order.IsType(OT_DUMMY) || v->current_order.IsType(OT_CONDITIONAL) || v->current_order.IsType(OT_GOTO_DEPOT)) ProcessOrders(v);
02701 
02702     res_dest = ExtendTrainReservation(v, &tracks, &dest_enterdir);
02703     if (res_dest.tile == INVALID_TILE) {
02704       /* Reservation failed? */
02705       if (mark_stuck) MarkTrainAsStuck(v);
02706       if (changed_signal) SetSignalStateByTrackdir(tile, TrackEnterdirToTrackdir(best_track, enterdir), SIGNAL_STATE_RED);
02707       return FindFirstTrack(tracks);
02708     }
02709   }
02710 
02711   /* Save the current train order. The destructor will restore the old order on function exit. */
02712   VehicleOrderSaver orders(v);
02713 
02714   /* If the current tile is the destination of the current order and
02715    * a reservation was requested, advance to the next order.
02716    * Don't advance on a depot order as depots are always safe end points
02717    * for a path and no look-ahead is necessary. This also avoids a
02718    * problem with depot orders not part of the order list when the
02719    * order list itself is empty. */
02720   if (v->current_order.IsType(OT_LEAVESTATION)) {
02721     orders.SwitchToNextOrder(false);
02722   } else if (v->current_order.IsType(OT_LOADING) || (!v->current_order.IsType(OT_GOTO_DEPOT) && (
02723       v->current_order.IsType(OT_GOTO_STATION) ?
02724       IsRailStationTile(v->tile) && v->current_order.GetDestination() == GetStationIndex(v->tile) :
02725       v->tile == v->dest_tile))) {
02726     orders.SwitchToNextOrder(true);
02727   }
02728 
02729   if (res_dest.tile != INVALID_TILE && !res_dest.okay) {
02730     /* Pathfinders are able to tell that route was only 'guessed'. */
02731     bool      path_not_found = false;
02732     TileIndex new_tile = res_dest.tile;
02733 
02734     Track next_track = DoTrainPathfind(v, new_tile, dest_enterdir, tracks, &path_not_found, do_track_reservation, &res_dest);
02735     if (new_tile == tile) best_track = next_track;
02736 
02737     /* handle "path not found" state */
02738     if (path_not_found) {
02739       /* PF didn't find the route */
02740       if (!HasBit(v->flags, VRF_NO_PATH_TO_DESTINATION)) {
02741         /* it is first time the problem occurred, set the "path not found" flag */
02742         SetBit(v->flags, VRF_NO_PATH_TO_DESTINATION);
02743         /* and notify user about the event */
02744         AI::NewEvent(v->owner, new AIEventVehicleLost(v->index));
02745         if (_settings_client.gui.lost_train_warn && v->owner == _local_company) {
02746           SetDParam(0, v->index);
02747           AddVehicleNewsItem(
02748             STR_NEWS_TRAIN_IS_LOST,
02749             NS_ADVICE,
02750             v->index
02751           );
02752         }
02753       }
02754     } else {
02755       /* route found, is the train marked with "path not found" flag? */
02756       if (HasBit(v->flags, VRF_NO_PATH_TO_DESTINATION)) {
02757         /* clear the flag as the PF's problem was solved */
02758         ClrBit(v->flags, VRF_NO_PATH_TO_DESTINATION);
02759         /* can we also delete the "News" item somehow? */
02760       }
02761     }
02762   }
02763 
02764   /* No track reservation requested -> finished. */
02765   if (!do_track_reservation) return best_track;
02766 
02767   /* A path was found, but could not be reserved. */
02768   if (res_dest.tile != INVALID_TILE && !res_dest.okay) {
02769     if (mark_stuck) MarkTrainAsStuck(v);
02770     FreeTrainTrackReservation(v);
02771     return best_track;
02772   }
02773 
02774   /* No possible reservation target found, we are probably lost. */
02775   if (res_dest.tile == INVALID_TILE) {
02776     /* Try to find any safe destination. */
02777     PBSTileInfo origin = FollowTrainReservation(v);
02778     if (TryReserveSafeTrack(v, origin.tile, origin.trackdir, false)) {
02779       TrackBits res = GetReservedTrackbits(tile) & DiagdirReachesTracks(enterdir);
02780       best_track = FindFirstTrack(res);
02781       TryReserveRailTrack(v->tile, TrackdirToTrack(v->GetVehicleTrackdir()));
02782       if (got_reservation != NULL) *got_reservation = true;
02783       if (changed_signal) MarkTileDirtyByTile(tile);
02784     } else {
02785       FreeTrainTrackReservation(v);
02786       if (mark_stuck) MarkTrainAsStuck(v);
02787     }
02788     return best_track;
02789   }
02790 
02791   if (got_reservation != NULL) *got_reservation = true;
02792 
02793   /* Reservation target found and free, check if it is safe. */
02794   while (!IsSafeWaitingPosition(v, res_dest.tile, res_dest.trackdir, true, _settings_game.pf.forbid_90_deg)) {
02795     /* Extend reservation until we have found a safe position. */
02796     DiagDirection exitdir = TrackdirToExitdir(res_dest.trackdir);
02797     TileIndex     next_tile = TileAddByDiagDir(res_dest.tile, exitdir);
02798     TrackBits     reachable = TrackdirBitsToTrackBits((TrackdirBits)(GetTileTrackStatus(next_tile, TRANSPORT_RAIL, 0))) & DiagdirReachesTracks(exitdir);
02799     if (_settings_game.pf.forbid_90_deg) {
02800       reachable &= ~TrackCrossesTracks(TrackdirToTrack(res_dest.trackdir));
02801     }
02802 
02803     /* Get next order with destination. */
02804     if (orders.SwitchToNextOrder(true)) {
02805       PBSTileInfo cur_dest;
02806       DoTrainPathfind(v, next_tile, exitdir, reachable, NULL, true, &cur_dest);
02807       if (cur_dest.tile != INVALID_TILE) {
02808         res_dest = cur_dest;
02809         if (res_dest.okay) continue;
02810         /* Path found, but could not be reserved. */
02811         FreeTrainTrackReservation(v);
02812         if (mark_stuck) MarkTrainAsStuck(v);
02813         if (got_reservation != NULL) *got_reservation = false;
02814         changed_signal = false;
02815         break;
02816       }
02817     }
02818     /* No order or no safe position found, try any position. */
02819     if (!TryReserveSafeTrack(v, res_dest.tile, res_dest.trackdir, true)) {
02820       FreeTrainTrackReservation(v);
02821       if (mark_stuck) MarkTrainAsStuck(v);
02822       if (got_reservation != NULL) *got_reservation = false;
02823       changed_signal = false;
02824     }
02825     break;
02826   }
02827 
02828   TryReserveRailTrack(v->tile, TrackdirToTrack(v->GetVehicleTrackdir()));
02829 
02830   if (changed_signal) MarkTileDirtyByTile(tile);
02831 
02832   return best_track;
02833 }
02834 
02843 bool TryPathReserve(Train *v, bool mark_as_stuck, bool first_tile_okay)
02844 {
02845   assert(v->IsFrontEngine());
02846 
02847   /* We have to handle depots specially as the track follower won't look
02848    * at the depot tile itself but starts from the next tile. If we are still
02849    * inside the depot, a depot reservation can never be ours. */
02850   if (v->track == TRACK_BIT_DEPOT) {
02851     if (HasDepotReservation(v->tile)) {
02852       if (mark_as_stuck) MarkTrainAsStuck(v);
02853       return false;
02854     } else {
02855       /* Depot not reserved, but the next tile might be. */
02856       TileIndex next_tile = TileAddByDiagDir(v->tile, GetRailDepotDirection(v->tile));
02857       if (HasReservedTracks(next_tile, DiagdirReachesTracks(GetRailDepotDirection(v->tile)))) return false;
02858     }
02859   }
02860 
02861   /* Special check if we are in front of a two-sided conventional signal. */
02862   DiagDirection dir = TrainExitDir(v->direction, v->track);
02863   TileIndex next_tile = TileAddByDiagDir(v->tile, dir);
02864   if (IsTileType(next_tile, MP_RAILWAY) && HasReservedTracks(next_tile, DiagdirReachesTracks(dir))) {
02865     /* Can have only one reserved trackdir. */
02866     Trackdir td = FindFirstTrackdir(TrackBitsToTrackdirBits(GetReservedTrackbits(next_tile)) & DiagdirReachesTrackdirs(dir));
02867     if (HasSignalOnTrackdir(next_tile, td) && HasSignalOnTrackdir(next_tile, ReverseTrackdir(td)) &&
02868         !IsPbsSignal(GetSignalType(next_tile, TrackdirToTrack(td)))) {
02869       /* Signal already reserved, is not ours. */
02870       if (mark_as_stuck) MarkTrainAsStuck(v);
02871       return false;
02872     }
02873   }
02874 
02875   bool other_train = false;
02876   PBSTileInfo origin = FollowTrainReservation(v, &other_train);
02877   /* The path we are driving on is alread blocked by some other train.
02878    * This can only happen in certain situations when mixing path and
02879    * block signals or when changing tracks and/or signals.
02880    * Exit here as doing any further reservations will probably just
02881    * make matters worse. */
02882   if (other_train && v->tile != origin.tile) {
02883     if (mark_as_stuck) MarkTrainAsStuck(v);
02884     return false;
02885   }
02886   /* If we have a reserved path and the path ends at a safe tile, we are finished already. */
02887   if (origin.okay && (v->tile != origin.tile || first_tile_okay)) {
02888     /* Can't be stuck then. */
02889     if (HasBit(v->flags, VRF_TRAIN_STUCK)) SetWindowWidgetDirty(WC_VEHICLE_VIEW, v->index, VVW_WIDGET_START_STOP_VEH);
02890     ClrBit(v->flags, VRF_TRAIN_STUCK);
02891     return true;
02892   }
02893 
02894   /* If we are in a depot, tentativly reserve the depot. */
02895   if (v->track == TRACK_BIT_DEPOT) {
02896     SetDepotReservation(v->tile, true);
02897     if (_settings_client.gui.show_track_reservation) MarkTileDirtyByTile(v->tile);
02898   }
02899 
02900   DiagDirection exitdir = TrackdirToExitdir(origin.trackdir);
02901   TileIndex     new_tile = TileAddByDiagDir(origin.tile, exitdir);
02902   TrackBits     reachable = TrackdirBitsToTrackBits(TrackStatusToTrackdirBits(GetTileTrackStatus(new_tile, TRANSPORT_RAIL, 0)) & DiagdirReachesTrackdirs(exitdir));
02903 
02904   if (_settings_game.pf.forbid_90_deg) reachable &= ~TrackCrossesTracks(TrackdirToTrack(origin.trackdir));
02905 
02906   bool res_made = false;
02907   ChooseTrainTrack(v, new_tile, exitdir, reachable, true, &res_made, mark_as_stuck);
02908 
02909   if (!res_made) {
02910     /* Free the depot reservation as well. */
02911     if (v->track == TRACK_BIT_DEPOT) SetDepotReservation(v->tile, false);
02912     return false;
02913   }
02914 
02915   if (HasBit(v->flags, VRF_TRAIN_STUCK)) {
02916     v->time_counter = 0;
02917     SetWindowWidgetDirty(WC_VEHICLE_VIEW, v->index, VVW_WIDGET_START_STOP_VEH);
02918   }
02919   ClrBit(v->flags, VRF_TRAIN_STUCK);
02920   return true;
02921 }
02922 
02923 
02924 static bool CheckReverseTrain(const Train *v)
02925 {
02926   if (_settings_game.difficulty.line_reverse_mode != 0 ||
02927       v->track == TRACK_BIT_DEPOT || v->track == TRACK_BIT_WORMHOLE ||
02928       !(v->direction & 1)) {
02929     return false;
02930   }
02931 
02932   assert(v->track != TRACK_BIT_NONE);
02933 
02934   switch (_settings_game.pf.pathfinder_for_trains) {
02935     case VPF_NPF: return NPFTrainCheckReverse(v);
02936     case VPF_YAPF: return YapfTrainCheckReverse(v);
02937 
02938     default: NOT_REACHED();
02939   }
02940 }
02941 
02942 TileIndex Train::GetOrderStationLocation(StationID station)
02943 {
02944   if (station == this->last_station_visited) this->last_station_visited = INVALID_STATION;
02945 
02946   const Station *st = Station::Get(station);
02947   if (!(st->facilities & FACIL_TRAIN)) {
02948     /* The destination station has no trainstation tiles. */
02949     this->IncrementOrderIndex();
02950     return 0;
02951   }
02952 
02953   return st->xy;
02954 }
02955 
02956 void Train::MarkDirty()
02957 {
02958   Vehicle *v = this;
02959   do {
02960     v->UpdateViewport(false, false);
02961   } while ((v = v->Next()) != NULL);
02962 
02963   /* need to update acceleration and cached values since the goods on the train changed. */
02964   TrainCargoChanged(this);
02965   UpdateTrainAcceleration(this);
02966 }
02967 
02978 static int UpdateTrainSpeed(Train *v)
02979 {
02980   uint accel;
02981 
02982   if ((v->vehstatus & VS_STOPPED) || HasBit(v->flags, VRF_REVERSING) || HasBit(v->flags, VRF_TRAIN_STUCK)) {
02983     switch (_settings_game.vehicle.train_acceleration_model) {
02984       default: NOT_REACHED();
02985       case TAM_ORIGINAL:  accel = v->acceleration * -4; break;
02986       case TAM_REALISTIC: accel = GetTrainAcceleration(v, AM_BRAKE); break;
02987     }
02988   } else {
02989     switch (_settings_game.vehicle.train_acceleration_model) {
02990       default: NOT_REACHED();
02991       case TAM_ORIGINAL:  accel = v->acceleration * 2; break;
02992       case TAM_REALISTIC: accel = GetTrainAcceleration(v, AM_ACCEL); break;
02993     }
02994   }
02995 
02996   uint spd = v->subspeed + accel;
02997   v->subspeed = (byte)spd;
02998   {
02999     int tempmax = v->max_speed;
03000     if (v->cur_speed > v->max_speed)
03001       tempmax = v->cur_speed - (v->cur_speed / 10) - 1;
03002     v->cur_speed = spd = Clamp(v->cur_speed + ((int)spd >> 8), 0, tempmax);
03003   }
03004 
03005   /* Scale speed by 3/4. Previously this was only done when the train was
03006    * facing diagonally and would apply to however many moves the train made
03007    * regardless the of direction actually moved in. Now it is always scaled,
03008    * 256 spd is used to go straight and 192 is used to go diagonally
03009    * (3/4 of 256). This results in the same effect, but without the error the
03010    * previous method caused.
03011    *
03012    * The scaling is done in this direction and not by multiplying the amount
03013    * to be subtracted by 4/3 so that the leftover speed can be saved in a
03014    * byte in v->progress.
03015    */
03016   int scaled_spd = spd * 3 >> 2;
03017 
03018   scaled_spd += v->progress;
03019   v->progress = 0; // set later in TrainLocoHandler or TrainController
03020   return scaled_spd;
03021 }
03022 
03023 static void TrainEnterStation(Train *v, StationID station)
03024 {
03025   v->last_station_visited = station;
03026 
03027   /* check if a train ever visited this station before */
03028   Station *st = Station::Get(station);
03029   if (!(st->had_vehicle_of_type & HVOT_TRAIN)) {
03030     st->had_vehicle_of_type |= HVOT_TRAIN;
03031     SetDParam(0, st->index);
03032     AddVehicleNewsItem(
03033       STR_NEWS_FIRST_TRAIN_ARRIVAL,
03034       v->owner == _local_company ? NS_ARRIVAL_COMPANY : NS_ARRIVAL_OTHER,
03035       v->index,
03036       st->index
03037     );
03038     AI::NewEvent(v->owner, new AIEventStationFirstVehicle(st->index, v->index));
03039   }
03040 
03041   v->BeginLoading();
03042 
03043   StationAnimationTrigger(st, v->tile, STAT_ANIM_TRAIN_ARRIVES);
03044 }
03045 
03046 static byte AfterSetTrainPos(Train *v, bool new_tile)
03047 {
03048   byte old_z = v->z_pos;
03049   v->z_pos = GetSlopeZ(v->x_pos, v->y_pos);
03050 
03051   if (new_tile) {
03052     ClrBit(v->flags, VRF_GOINGUP);
03053     ClrBit(v->flags, VRF_GOINGDOWN);
03054 
03055     if (v->track == TRACK_BIT_X || v->track == TRACK_BIT_Y) {
03056       /* Any track that isn't TRACK_BIT_X or TRACK_BIT_Y cannot be sloped.
03057        * To check whether the current tile is sloped, and in which
03058        * direction it is sloped, we get the 'z' at the center of
03059        * the tile (middle_z) and the edge of the tile (old_z),
03060        * which we then can compare. */
03061       static const int HALF_TILE_SIZE = TILE_SIZE / 2;
03062       static const int INV_TILE_SIZE_MASK = ~(TILE_SIZE - 1);
03063 
03064       byte middle_z = GetSlopeZ((v->x_pos & INV_TILE_SIZE_MASK) | HALF_TILE_SIZE, (v->y_pos & INV_TILE_SIZE_MASK) | HALF_TILE_SIZE);
03065 
03066       if (middle_z != v->z_pos) {
03067         SetBit(v->flags, (middle_z > old_z) ? VRF_GOINGUP : VRF_GOINGDOWN);
03068       }
03069     }
03070   }
03071 
03072   VehicleMove(v, true);
03073   return old_z;
03074 }
03075 
03076 /* Check if the vehicle is compatible with the specified tile */
03077 static inline bool CheckCompatibleRail(const Train *v, TileIndex tile)
03078 {
03079   return
03080     IsTileOwner(tile, v->owner) && (
03081       !v->IsFrontEngine() ||
03082       HasBit(v->compatible_railtypes, GetRailType(tile))
03083     );
03084 }
03085 
03086 struct RailtypeSlowdownParams {
03087   byte small_turn, large_turn;
03088   byte z_up; // fraction to remove when moving up
03089   byte z_down; // fraction to remove when moving down
03090 };
03091 
03092 static const RailtypeSlowdownParams _railtype_slowdown[] = {
03093   /* normal accel */
03094   {256 / 4, 256 / 2, 256 / 4, 2}, 
03095   {256 / 4, 256 / 2, 256 / 4, 2}, 
03096   {256 / 4, 256 / 2, 256 / 4, 2}, 
03097   {0,       256 / 2, 256 / 4, 2}, 
03098 };
03099 
03101 static inline void AffectSpeedByZChange(Train *v, byte old_z)
03102 {
03103   if (old_z == v->z_pos || _settings_game.vehicle.train_acceleration_model != TAM_ORIGINAL) return;
03104 
03105   const RailtypeSlowdownParams *rsp = &_railtype_slowdown[v->railtype];
03106 
03107   if (old_z < v->z_pos) {
03108     v->cur_speed -= (v->cur_speed * rsp->z_up >> 8);
03109   } else {
03110     uint16 spd = v->cur_speed + rsp->z_down;
03111     if (spd <= v->max_speed) v->cur_speed = spd;
03112   }
03113 }
03114 
03115 static bool TrainMovedChangeSignals(TileIndex tile, DiagDirection dir)
03116 {
03117   if (IsTileType(tile, MP_RAILWAY) &&
03118       GetRailTileType(tile) == RAIL_TILE_SIGNALS) {
03119     TrackdirBits tracks = TrackBitsToTrackdirBits(GetTrackBits(tile)) & DiagdirReachesTrackdirs(dir);
03120     Trackdir trackdir = FindFirstTrackdir(tracks);
03121     if (UpdateSignalsOnSegment(tile,  TrackdirToExitdir(trackdir), GetTileOwner(tile)) == SIGSEG_PBS && HasSignalOnTrackdir(tile, trackdir)) {
03122       /* A PBS block with a non-PBS signal facing us? */
03123       if (!IsPbsSignal(GetSignalType(tile, TrackdirToTrack(trackdir)))) return true;
03124     }
03125   }
03126   return false;
03127 }
03128 
03132 void Train::ReserveTrackUnderConsist() const
03133 {
03134   for (const Train *u = this; u != NULL; u = u->Next()) {
03135     switch (u->track) {
03136       case TRACK_BIT_WORMHOLE:
03137         TryReserveRailTrack(u->tile, DiagDirToDiagTrack(GetTunnelBridgeDirection(u->tile)));
03138         break;
03139       case TRACK_BIT_DEPOT:
03140         break;
03141       default:
03142         TryReserveRailTrack(u->tile, TrackBitsToTrack(u->track));
03143         break;
03144     }
03145   }
03146 }
03147 
03148 uint Train::Crash(bool flooded)
03149 {
03150   uint pass = 0;
03151   if (this->IsFrontEngine()) {
03152     pass += 4; // driver
03153 
03154     /* Remove the reserved path in front of the train if it is not stuck.
03155      * Also clear all reserved tracks the train is currently on. */
03156     if (!HasBit(this->flags, VRF_TRAIN_STUCK)) FreeTrainTrackReservation(this);
03157     for (const Train *v = this; v != NULL; v = v->Next()) {
03158       ClearPathReservation(v, v->tile, v->GetVehicleTrackdir());
03159       if (IsTileType(v->tile, MP_TUNNELBRIDGE)) {
03160         /* ClearPathReservation will not free the wormhole exit
03161          * if the train has just entered the wormhole. */
03162         SetTunnelBridgeReservation(GetOtherTunnelBridgeEnd(v->tile), false);
03163       }
03164     }
03165 
03166     /* we may need to update crossing we were approaching,
03167     * but must be updated after the train has been marked crashed */
03168     TileIndex crossing = TrainApproachingCrossingTile(this);
03169     if (crossing != INVALID_TILE) UpdateLevelCrossing(crossing);
03170   }
03171 
03172   pass += Vehicle::Crash(flooded);
03173 
03174   this->crash_anim_pos = flooded ? 4000 : 1; // max 4440, disappear pretty fast when flooded
03175   return pass;
03176 }
03177 
03184 static uint TrainCrashed(Train *v)
03185 {
03186   uint num = 0;
03187 
03188   /* do not crash train twice */
03189   if (!(v->vehstatus & VS_CRASHED)) {
03190     num = v->Crash();
03191     AI::NewEvent(v->owner, new AIEventVehicleCrashed(v->index, v->tile, AIEventVehicleCrashed::CRASH_TRAIN));
03192   }
03193 
03194   /* Try to re-reserve track under already crashed train too.
03195    * SetVehicleCrashed() clears the reservation! */
03196   v->ReserveTrackUnderConsist();
03197 
03198   return num;
03199 }
03200 
03201 struct TrainCollideChecker {
03202   Train *v; 
03203   uint num; 
03204 };
03205 
03206 static Vehicle *FindTrainCollideEnum(Vehicle *v, void *data)
03207 {
03208   TrainCollideChecker *tcc = (TrainCollideChecker*)data;
03209 
03210   /* not a train or in depot */
03211   if (v->type != VEH_TRAIN || Train::From(v)->track == TRACK_BIT_DEPOT) return NULL;
03212 
03213   /* get first vehicle now to make most usual checks faster */
03214   Train *coll = Train::From(v)->First();
03215 
03216   /* can't collide with own wagons */
03217   if (coll == tcc->v) return NULL;
03218 
03219   int x_diff = v->x_pos - tcc->v->x_pos;
03220   int y_diff = v->y_pos - tcc->v->y_pos;
03221 
03222   /* Do fast calculation to check whether trains are not in close vicinity
03223    * and quickly reject trains distant enough for any collision.
03224    * Differences are shifted by 7, mapping range [-7 .. 8] into [0 .. 15]
03225    * Differences are then ORed and then we check for any higher bits */
03226   uint hash = (y_diff + 7) | (x_diff + 7);
03227   if (hash & ~15) return NULL;
03228 
03229   /* Slower check using multiplication */
03230   if (x_diff * x_diff + y_diff * y_diff > 25) return NULL;
03231 
03232   /* Happens when there is a train under bridge next to bridge head */
03233   if (abs(v->z_pos - tcc->v->z_pos) > 5) return NULL;
03234 
03235   /* crash both trains */
03236   tcc->num += TrainCrashed(tcc->v);
03237   tcc->num += TrainCrashed(coll);
03238 
03239   return NULL; // continue searching
03240 }
03241 
03248 static bool CheckTrainCollision(Train *v)
03249 {
03250   /* can't collide in depot */
03251   if (v->track == TRACK_BIT_DEPOT) return false;
03252 
03253   assert(v->track == TRACK_BIT_WORMHOLE || TileVirtXY(v->x_pos, v->y_pos) == v->tile);
03254 
03255   TrainCollideChecker tcc;
03256   tcc.v = v;
03257   tcc.num = 0;
03258 
03259   /* find colliding vehicles */
03260   if (v->track == TRACK_BIT_WORMHOLE) {
03261     FindVehicleOnPos(v->tile, &tcc, FindTrainCollideEnum);
03262     FindVehicleOnPos(GetOtherTunnelBridgeEnd(v->tile), &tcc, FindTrainCollideEnum);
03263   } else {
03264     FindVehicleOnPosXY(v->x_pos, v->y_pos, &tcc, FindTrainCollideEnum);
03265   }
03266 
03267   /* any dead -> no crash */
03268   if (tcc.num == 0) return false;
03269 
03270   SetDParam(0, tcc.num);
03271   AddVehicleNewsItem(STR_NEWS_TRAIN_CRASH,
03272     NS_ACCIDENT,
03273     v->index
03274   );
03275 
03276   ModifyStationRatingAround(v->tile, v->owner, -160, 30);
03277   SndPlayVehicleFx(SND_13_BIG_CRASH, v);
03278   return true;
03279 }
03280 
03281 static Vehicle *CheckTrainAtSignal(Vehicle *v, void *data)
03282 {
03283   if (v->type != VEH_TRAIN || (v->vehstatus & VS_CRASHED)) return NULL;
03284 
03285   Train *t = Train::From(v);
03286   DiagDirection exitdir = *(DiagDirection *)data;
03287 
03288   /* not front engine of a train, inside wormhole or depot, crashed */
03289   if (!t->IsFrontEngine() || !(t->track & TRACK_BIT_MASK)) return NULL;
03290 
03291   if (t->cur_speed > 5 || TrainExitDir(t->direction, t->track) != exitdir) return NULL;
03292 
03293   return t;
03294 }
03295 
03296 static void TrainController(Train *v, Vehicle *nomove)
03297 {
03298   Train *first = v->First();
03299   Train *prev;
03300   bool direction_changed = false; // has direction of any part changed?
03301 
03302   /* For every vehicle after and including the given vehicle */
03303   for (prev = v->Previous(); v != nomove; prev = v, v = v->Next()) {
03304     DiagDirection enterdir = DIAGDIR_BEGIN;
03305     bool update_signals_crossing = false; // will we update signals or crossing state?
03306 
03307     GetNewVehiclePosResult gp = GetNewVehiclePos(v);
03308     if (v->track != TRACK_BIT_WORMHOLE) {
03309       /* Not inside tunnel */
03310       if (gp.old_tile == gp.new_tile) {
03311         /* Staying in the old tile */
03312         if (v->track == TRACK_BIT_DEPOT) {
03313           /* Inside depot */
03314           gp.x = v->x_pos;
03315           gp.y = v->y_pos;
03316         } else {
03317           /* Not inside depot */
03318 
03319           /* Reverse when we are at the end of the track already, do not move to the new position */
03320           if (v->IsFrontEngine() && !TrainCheckIfLineEnds(v)) return;
03321 
03322           uint32 r = VehicleEnterTile(v, gp.new_tile, gp.x, gp.y);
03323           if (HasBit(r, VETS_CANNOT_ENTER)) {
03324             goto invalid_rail;
03325           }
03326           if (HasBit(r, VETS_ENTERED_STATION)) {
03327             /* The new position is the end of the platform */
03328             TrainEnterStation(v, r >> VETS_STATION_ID_OFFSET);
03329           }
03330         }
03331       } else {
03332         /* A new tile is about to be entered. */
03333 
03334         /* Determine what direction we're entering the new tile from */
03335         enterdir = DiagdirBetweenTiles(gp.old_tile, gp.new_tile);
03336         assert(IsValidDiagDirection(enterdir));
03337 
03338         /* Get the status of the tracks in the new tile and mask
03339          * away the bits that aren't reachable. */
03340         TrackStatus ts = GetTileTrackStatus(gp.new_tile, TRANSPORT_RAIL, 0, ReverseDiagDir(enterdir));
03341         TrackdirBits reachable_trackdirs = DiagdirReachesTrackdirs(enterdir);
03342 
03343         TrackdirBits trackdirbits = TrackStatusToTrackdirBits(ts) & reachable_trackdirs;
03344         TrackBits red_signals = TrackdirBitsToTrackBits(TrackStatusToRedSignals(ts) & reachable_trackdirs);
03345 
03346         TrackBits bits = TrackdirBitsToTrackBits(trackdirbits);
03347         if (_settings_game.pf.forbid_90_deg && prev == NULL) {
03348           /* We allow wagons to make 90 deg turns, because forbid_90_deg
03349            * can be switched on halfway a turn */
03350           bits &= ~TrackCrossesTracks(FindFirstTrack(v->track));
03351         }
03352 
03353         if (bits == TRACK_BIT_NONE) goto invalid_rail;
03354 
03355         /* Check if the new tile contrains tracks that are compatible
03356          * with the current train, if not, bail out. */
03357         if (!CheckCompatibleRail(v, gp.new_tile)) goto invalid_rail;
03358 
03359         TrackBits chosen_track;
03360         if (prev == NULL) {
03361           /* Currently the locomotive is active. Determine which one of the
03362            * available tracks to choose */
03363           chosen_track = TrackToTrackBits(ChooseTrainTrack(v, gp.new_tile, enterdir, bits, false, NULL, true));
03364           assert(chosen_track & (bits | GetReservedTrackbits(gp.new_tile)));
03365 
03366           if (v->force_proceed != 0 && IsPlainRailTile(gp.new_tile) && HasSignals(gp.new_tile)) {
03367             /* For each signal we find decrease the counter by one.
03368              * We start at two, so the first signal we pass decreases
03369              * this to one, then if we reach the next signal it is
03370              * decreased to zero and we won't pass that new signal. */
03371             Trackdir dir = FindFirstTrackdir(trackdirbits);
03372             if (GetSignalType(gp.new_tile, TrackdirToTrack(dir)) != SIGTYPE_PBS ||
03373                 !HasSignalOnTrackdir(gp.new_tile, ReverseTrackdir(dir))) {
03374               /* However, we do not want to be stopped by PBS signals
03375                * entered via the back. */
03376               v->force_proceed--;
03377               SetWindowDirty(WC_VEHICLE_VIEW, v->index);
03378             }
03379           }
03380 
03381           /* Check if it's a red signal and that force proceed is not clicked. */
03382           if ((red_signals & chosen_track) && v->force_proceed == 0) {
03383             /* In front of a red signal */
03384             Trackdir i = FindFirstTrackdir(trackdirbits);
03385 
03386             /* Don't handle stuck trains here. */
03387             if (HasBit(v->flags, VRF_TRAIN_STUCK)) return;
03388 
03389             if (!HasSignalOnTrackdir(gp.new_tile, ReverseTrackdir(i))) {
03390               v->cur_speed = 0;
03391               v->subspeed = 0;
03392               v->progress = 255 - 100;
03393               if (_settings_game.pf.wait_oneway_signal == 255 || ++v->time_counter < _settings_game.pf.wait_oneway_signal * 20) return;
03394             } else if (HasSignalOnTrackdir(gp.new_tile, i)) {
03395               v->cur_speed = 0;
03396               v->subspeed = 0;
03397               v->progress = 255 - 10;
03398               if (_settings_game.pf.wait_twoway_signal == 255 || ++v->time_counter < _settings_game.pf.wait_twoway_signal * 73) {
03399                 DiagDirection exitdir = TrackdirToExitdir(i);
03400                 TileIndex o_tile = TileAddByDiagDir(gp.new_tile, exitdir);
03401 
03402                 exitdir = ReverseDiagDir(exitdir);
03403 
03404                 /* check if a train is waiting on the other side */
03405                 if (!HasVehicleOnPos(o_tile, &exitdir, &CheckTrainAtSignal)) return;
03406               }
03407             }
03408 
03409             /* If we would reverse but are currently in a PBS block and
03410              * reversing of stuck trains is disabled, don't reverse. */
03411             if (_settings_game.pf.wait_for_pbs_path == 255 && UpdateSignalsOnSegment(v->tile, enterdir, v->owner) == SIGSEG_PBS) {
03412               v->time_counter = 0;
03413               return;
03414             }
03415             goto reverse_train_direction;
03416           } else {
03417             TryReserveRailTrack(gp.new_tile, TrackBitsToTrack(chosen_track));
03418           }
03419         } else {
03420           /* The wagon is active, simply follow the prev vehicle. */
03421           if (prev->tile == gp.new_tile) {
03422             /* Choose the same track as prev */
03423             if (prev->track == TRACK_BIT_WORMHOLE) {
03424               /* Vehicles entering tunnels enter the wormhole earlier than for bridges.
03425                * However, just choose the track into the wormhole. */
03426               assert(IsTunnel(prev->tile));
03427               chosen_track = bits;
03428             } else {
03429               chosen_track = prev->track;
03430             }
03431           } else {
03432             /* Choose the track that leads to the tile where prev is.
03433              * This case is active if 'prev' is already on the second next tile, when 'v' just enters the next tile.
03434              * I.e. when the tile between them has only space for a single vehicle like
03435              *  1) horizontal/vertical track tiles and
03436              *  2) some orientations of tunnelentries, where the vehicle is already inside the wormhole at 8/16 from the tileedge.
03437              *     Is also the train just reversing, the wagon inside the tunnel is 'on' the tile of the opposite tunnelentry.
03438              */
03439             static const TrackBits _connecting_track[DIAGDIR_END][DIAGDIR_END] = {
03440               {TRACK_BIT_X,     TRACK_BIT_LOWER, TRACK_BIT_NONE,  TRACK_BIT_LEFT },
03441               {TRACK_BIT_UPPER, TRACK_BIT_Y,     TRACK_BIT_LEFT,  TRACK_BIT_NONE },
03442               {TRACK_BIT_NONE,  TRACK_BIT_RIGHT, TRACK_BIT_X,     TRACK_BIT_UPPER},
03443               {TRACK_BIT_RIGHT, TRACK_BIT_NONE,  TRACK_BIT_LOWER, TRACK_BIT_Y    }
03444             };
03445             DiagDirection exitdir = DiagdirBetweenTiles(gp.new_tile, prev->tile);
03446             assert(IsValidDiagDirection(exitdir));
03447             chosen_track = _connecting_track[enterdir][exitdir];
03448           }
03449           chosen_track &= bits;
03450         }
03451 
03452         /* Make sure chosen track is a valid track */
03453         assert(
03454             chosen_track == TRACK_BIT_X     || chosen_track == TRACK_BIT_Y ||
03455             chosen_track == TRACK_BIT_UPPER || chosen_track == TRACK_BIT_LOWER ||
03456             chosen_track == TRACK_BIT_LEFT  || chosen_track == TRACK_BIT_RIGHT);
03457 
03458         /* Update XY to reflect the entrance to the new tile, and select the direction to use */
03459         const byte *b = _initial_tile_subcoord[FIND_FIRST_BIT(chosen_track)][enterdir];
03460         gp.x = (gp.x & ~0xF) | b[0];
03461         gp.y = (gp.y & ~0xF) | b[1];
03462         Direction chosen_dir = (Direction)b[2];
03463 
03464         /* Call the landscape function and tell it that the vehicle entered the tile */
03465         uint32 r = VehicleEnterTile(v, gp.new_tile, gp.x, gp.y);
03466         if (HasBit(r, VETS_CANNOT_ENTER)) {
03467           goto invalid_rail;
03468         }
03469 
03470         if (!HasBit(r, VETS_ENTERED_WORMHOLE)) {
03471           Track track = FindFirstTrack(chosen_track);
03472           Trackdir tdir = TrackDirectionToTrackdir(track, chosen_dir);
03473           if (v->IsFrontEngine() && HasPbsSignalOnTrackdir(gp.new_tile, tdir)) {
03474             SetSignalStateByTrackdir(gp.new_tile, tdir, SIGNAL_STATE_RED);
03475             MarkTileDirtyByTile(gp.new_tile);
03476           }
03477 
03478           /* Clear any track reservation when the last vehicle leaves the tile */
03479           if (v->Next() == NULL) ClearPathReservation(v, v->tile, v->GetVehicleTrackdir());
03480 
03481           v->tile = gp.new_tile;
03482 
03483           if (GetTileRailType(gp.new_tile) != GetTileRailType(gp.old_tile)) {
03484             TrainPowerChanged(v->First());
03485           }
03486 
03487           v->track = chosen_track;
03488           assert(v->track);
03489         }
03490 
03491         /* We need to update signal status, but after the vehicle position hash
03492          * has been updated by AfterSetTrainPos() */
03493         update_signals_crossing = true;
03494 
03495         if (chosen_dir != v->direction) {
03496           if (prev == NULL && _settings_game.vehicle.train_acceleration_model == TAM_ORIGINAL) {
03497             const RailtypeSlowdownParams *rsp = &_railtype_slowdown[v->railtype];
03498             DirDiff diff = DirDifference(v->direction, chosen_dir);
03499             v->cur_speed -= (diff == DIRDIFF_45RIGHT || diff == DIRDIFF_45LEFT ? rsp->small_turn : rsp->large_turn) * v->cur_speed >> 8;
03500           }
03501           direction_changed = true;
03502           v->direction = chosen_dir;
03503         }
03504 
03505         if (v->IsFrontEngine()) {
03506           v->time_counter = 0;
03507 
03508           /* If we are approching a crossing that is reserved, play the sound now. */
03509           TileIndex crossing = TrainApproachingCrossingTile(v);
03510           if (crossing != INVALID_TILE && HasCrossingReservation(crossing)) SndPlayTileFx(SND_0E_LEVEL_CROSSING, crossing);
03511 
03512           /* Always try to extend the reservation when entering a tile. */
03513           CheckNextTrainTile(v);
03514         }
03515 
03516         if (HasBit(r, VETS_ENTERED_STATION)) {
03517           /* The new position is the location where we want to stop */
03518           TrainEnterStation(v, r >> VETS_STATION_ID_OFFSET);
03519         }
03520       }
03521     } else {
03522       /* In a tunnel or on a bridge
03523        * - for tunnels, only the part when the vehicle is not visible (part of enter/exit tile too)
03524        * - for bridges, only the middle part - without the bridge heads */
03525       if (!(v->vehstatus & VS_HIDDEN)) {
03526         v->cur_speed =
03527           min(v->cur_speed, GetBridgeSpec(GetBridgeType(v->tile))->speed);
03528       }
03529 
03530       if (IsTileType(gp.new_tile, MP_TUNNELBRIDGE) && HasBit(VehicleEnterTile(v, gp.new_tile, gp.x, gp.y), VETS_ENTERED_WORMHOLE)) {
03531         /* Perform look-ahead on tunnel exit. */
03532         if (v->IsFrontEngine()) {
03533           TryReserveRailTrack(gp.new_tile, DiagDirToDiagTrack(GetTunnelBridgeDirection(gp.new_tile)));
03534           CheckNextTrainTile(v);
03535         }
03536       } else {
03537         v->x_pos = gp.x;
03538         v->y_pos = gp.y;
03539         VehicleMove(v, !(v->vehstatus & VS_HIDDEN));
03540         continue;
03541       }
03542     }
03543 
03544     /* update image of train, as well as delta XY */
03545     v->UpdateDeltaXY(v->direction);
03546 
03547     v->x_pos = gp.x;
03548     v->y_pos = gp.y;
03549 
03550     /* update the Z position of the vehicle */
03551     byte old_z = AfterSetTrainPos(v, (gp.new_tile != gp.old_tile));
03552 
03553     if (prev == NULL) {
03554       /* This is the first vehicle in the train */
03555       AffectSpeedByZChange(v, old_z);
03556     }
03557 
03558     if (update_signals_crossing) {
03559       if (v->IsFrontEngine()) {
03560         if (TrainMovedChangeSignals(gp.new_tile, enterdir)) {
03561           /* We are entering a block with PBS signals right now, but
03562            * not through a PBS signal. This means we don't have a
03563            * reservation right now. As a conventional signal will only
03564            * ever be green if no other train is in the block, getting
03565            * a path should always be possible. If the player built
03566            * such a strange network that it is not possible, the train
03567            * will be marked as stuck and the player has to deal with
03568            * the problem. */
03569           if ((!HasReservedTracks(gp.new_tile, v->track) &&
03570               !TryReserveRailTrack(gp.new_tile, FindFirstTrack(v->track))) ||
03571               !TryPathReserve(v)) {
03572             MarkTrainAsStuck(v);
03573           }
03574         }
03575       }
03576 
03577       /* Signals can only change when the first
03578        * (above) or the last vehicle moves. */
03579       if (v->Next() == NULL) {
03580         TrainMovedChangeSignals(gp.old_tile, ReverseDiagDir(enterdir));
03581         if (IsLevelCrossingTile(gp.old_tile)) UpdateLevelCrossing(gp.old_tile);
03582       }
03583     }
03584 
03585     /* Do not check on every tick to save some computing time. */
03586     if (v->IsFrontEngine() && v->tick_counter % _settings_game.pf.path_backoff_interval == 0) CheckNextTrainTile(v);
03587   }
03588 
03589   if (direction_changed) first->tcache.cached_max_curve_speed = GetTrainCurveSpeedLimit(first);
03590 
03591   return;
03592 
03593 invalid_rail:
03594   /* We've reached end of line?? */
03595   if (prev != NULL) error("Disconnecting train");
03596 
03597 reverse_train_direction:
03598   v->time_counter = 0;
03599   v->cur_speed = 0;
03600   v->subspeed = 0;
03601   ReverseTrainDirection(v);
03602 }
03603 
03609 static Vehicle *CollectTrackbitsFromCrashedVehiclesEnum(Vehicle *v, void *data)
03610 {
03611   TrackBits *trackbits = (TrackBits *)data;
03612 
03613   if (v->type == VEH_TRAIN && (v->vehstatus & VS_CRASHED) != 0) {
03614     if (Train::From(v)->track == TRACK_BIT_WORMHOLE) {
03615       /* Vehicle is inside a wormhole, v->track contains no useful value then. */
03616       *trackbits |= DiagDirToDiagTrackBits(GetTunnelBridgeDirection(v->tile));
03617     } else {
03618       *trackbits |= Train::From(v)->track;
03619     }
03620   }
03621 
03622   return NULL;
03623 }
03624 
03632 static void DeleteLastWagon(Train *v)
03633 {
03634   Train *first = v->First();
03635 
03636   /* Go to the last wagon and delete the link pointing there
03637    * *u is then the one-before-last wagon, and *v the last
03638    * one which will physicially be removed */
03639   Train *u = v;
03640   for (; v->Next() != NULL; v = v->Next()) u = v;
03641   u->SetNext(NULL);
03642 
03643   if (first != v) {
03644     /* Recalculate cached train properties */
03645     TrainConsistChanged(first, false);
03646     /* Update the depot window if the first vehicle is in depot -
03647      * if v == first, then it is updated in PreDestructor() */
03648     if (first->track == TRACK_BIT_DEPOT) {
03649       SetWindowDirty(WC_VEHICLE_DEPOT, first->tile);
03650     }
03651   }
03652 
03653   /* 'v' shouldn't be accessed after it has been deleted */
03654   TrackBits trackbits = v->track;
03655   TileIndex tile = v->tile;
03656   Owner owner = v->owner;
03657 
03658   delete v;
03659   v = NULL; // make sure nobody will try to read 'v' anymore
03660 
03661   if (trackbits == TRACK_BIT_WORMHOLE) {
03662     /* Vehicle is inside a wormhole, v->track contains no useful value then. */
03663     trackbits = DiagDirToDiagTrackBits(GetTunnelBridgeDirection(tile));
03664   }
03665 
03666   Track track = TrackBitsToTrack(trackbits);
03667   if (HasReservedTracks(tile, trackbits)) {
03668     UnreserveRailTrack(tile, track);
03669 
03670     /* If there are still crashed vehicles on the tile, give the track reservation to them */
03671     TrackBits remaining_trackbits = TRACK_BIT_NONE;
03672     FindVehicleOnPos(tile, &remaining_trackbits, CollectTrackbitsFromCrashedVehiclesEnum);
03673 
03674     /* It is important that these two are the first in the loop, as reservation cannot deal with every trackbit combination */
03675     assert(TRACK_BEGIN == TRACK_X && TRACK_Y == TRACK_BEGIN + 1);
03676     for (Track t = TRACK_BEGIN; t < TRACK_END; t++) {
03677       if (HasBit(remaining_trackbits, t)) {
03678         TryReserveRailTrack(tile, t);
03679       }
03680     }
03681   }
03682 
03683   /* check if the wagon was on a road/rail-crossing */
03684   if (IsLevelCrossingTile(tile)) UpdateLevelCrossing(tile);
03685 
03686   /* Update signals */
03687   if (IsTileType(tile, MP_TUNNELBRIDGE) || IsRailDepotTile(tile)) {
03688     UpdateSignalsOnSegment(tile, INVALID_DIAGDIR, owner);
03689   } else {
03690     SetSignalsOnBothDir(tile, track, owner);
03691   }
03692 }
03693 
03694 static void ChangeTrainDirRandomly(Train *v)
03695 {
03696   static const DirDiff delta[] = {
03697     DIRDIFF_45LEFT, DIRDIFF_SAME, DIRDIFF_SAME, DIRDIFF_45RIGHT
03698   };
03699 
03700   do {
03701     /* We don't need to twist around vehicles if they're not visible */
03702     if (!(v->vehstatus & VS_HIDDEN)) {
03703       v->direction = ChangeDir(v->direction, delta[GB(Random(), 0, 2)]);
03704       v->UpdateDeltaXY(v->direction);
03705       v->cur_image = v->GetImage(v->direction);
03706       /* Refrain from updating the z position of the vehicle when on
03707        * a bridge, because AfterSetTrainPos will put the vehicle under
03708        * the bridge in that case */
03709       if (v->track != TRACK_BIT_WORMHOLE) AfterSetTrainPos(v, false);
03710     }
03711   } while ((v = v->Next()) != NULL);
03712 }
03713 
03714 static bool HandleCrashedTrain(Train *v)
03715 {
03716   int state = ++v->crash_anim_pos;
03717 
03718   if (state == 4 && !(v->vehstatus & VS_HIDDEN)) {
03719     CreateEffectVehicleRel(v, 4, 4, 8, EV_EXPLOSION_LARGE);
03720   }
03721 
03722   uint32 r;
03723   if (state <= 200 && Chance16R(1, 7, r)) {
03724     int index = (r * 10 >> 16);
03725 
03726     Vehicle *u = v;
03727     do {
03728       if (--index < 0) {
03729         r = Random();
03730 
03731         CreateEffectVehicleRel(u,
03732           GB(r,  8, 3) + 2,
03733           GB(r, 16, 3) + 2,
03734           GB(r,  0, 3) + 5,
03735           EV_EXPLOSION_SMALL);
03736         break;
03737       }
03738     } while ((u = u->Next()) != NULL);
03739   }
03740 
03741   if (state <= 240 && !(v->tick_counter & 3)) ChangeTrainDirRandomly(v);
03742 
03743   if (state >= 4440 && !(v->tick_counter & 0x1F)) {
03744     bool ret = v->Next() != NULL;
03745     DeleteLastWagon(v);
03746     return ret;
03747   }
03748 
03749   return true;
03750 }
03751 
03752 static void HandleBrokenTrain(Train *v)
03753 {
03754   if (v->breakdown_ctr != 1) {
03755     v->breakdown_ctr = 1;
03756     v->cur_speed = 0;
03757 
03758     if (v->breakdowns_since_last_service != 255)
03759       v->breakdowns_since_last_service++;
03760 
03761     v->MarkDirty();
03762     SetWindowDirty(WC_VEHICLE_VIEW, v->index);
03763     SetWindowDirty(WC_VEHICLE_DETAILS, v->index);
03764 
03765     if (!PlayVehicleSound(v, VSE_BREAKDOWN)) {
03766       SndPlayVehicleFx((_settings_game.game_creation.landscape != LT_TOYLAND) ?
03767         SND_10_TRAIN_BREAKDOWN : SND_3A_COMEDY_BREAKDOWN_2, v);
03768     }
03769 
03770     if (!(v->vehstatus & VS_HIDDEN)) {
03771       EffectVehicle *u = CreateEffectVehicleRel(v, 4, 4, 5, EV_BREAKDOWN_SMOKE);
03772       if (u != NULL) u->animation_state = v->breakdown_delay * 2;
03773     }
03774   }
03775 
03776   if (!(v->tick_counter & 3)) {
03777     if (!--v->breakdown_delay) {
03778       v->breakdown_ctr = 0;
03779       v->MarkDirty();
03780       SetWindowDirty(WC_VEHICLE_VIEW, v->index);
03781     }
03782   }
03783 }
03784 
03786 static const uint16 _breakdown_speeds[16] = {
03787   225, 210, 195, 180, 165, 150, 135, 120, 105, 90, 75, 60, 45, 30, 15, 15
03788 };
03789 
03790 
03798 static bool TrainApproachingLineEnd(Train *v, bool signal)
03799 {
03800   /* Calc position within the current tile */
03801   uint x = v->x_pos & 0xF;
03802   uint y = v->y_pos & 0xF;
03803 
03804   /* for diagonal directions, 'x' will be 0..15 -
03805    * for other directions, it will be 1, 3, 5, ..., 15 */
03806   switch (v->direction) {
03807     case DIR_N : x = ~x + ~y + 25; break;
03808     case DIR_NW: x = y;            // FALLTHROUGH
03809     case DIR_NE: x = ~x + 16;      break;
03810     case DIR_E : x = ~x + y + 9;   break;
03811     case DIR_SE: x = y;            break;
03812     case DIR_S : x = x + y - 7;    break;
03813     case DIR_W : x = ~y + x + 9;   break;
03814     default: break;
03815   }
03816 
03817   /* do not reverse when approaching red signal */
03818   if (!signal && x + (v->tcache.cached_veh_length + 1) / 2 >= TILE_SIZE) {
03819     /* we are too near the tile end, reverse now */
03820     v->cur_speed = 0;
03821     ReverseTrainDirection(v);
03822     return false;
03823   }
03824 
03825   /* slow down */
03826   v->vehstatus |= VS_TRAIN_SLOWING;
03827   uint16 break_speed = _breakdown_speeds[x & 0xF];
03828   if (break_speed < v->cur_speed) v->cur_speed = break_speed;
03829 
03830   return true;
03831 }
03832 
03833 
03839 static bool TrainCanLeaveTile(const Train *v)
03840 {
03841   /* Exit if inside a tunnel/bridge or a depot */
03842   if (v->track == TRACK_BIT_WORMHOLE || v->track == TRACK_BIT_DEPOT) return false;
03843 
03844   TileIndex tile = v->tile;
03845 
03846   /* entering a tunnel/bridge? */
03847   if (IsTileType(tile, MP_TUNNELBRIDGE)) {
03848     DiagDirection dir = GetTunnelBridgeDirection(tile);
03849     if (DiagDirToDir(dir) == v->direction) return false;
03850   }
03851 
03852   /* entering a depot? */
03853   if (IsRailDepotTile(tile)) {
03854     DiagDirection dir = ReverseDiagDir(GetRailDepotDirection(tile));
03855     if (DiagDirToDir(dir) == v->direction) return false;
03856   }
03857 
03858   return true;
03859 }
03860 
03861 
03869 static TileIndex TrainApproachingCrossingTile(const Train *v)
03870 {
03871   assert(v->IsFrontEngine());
03872   assert(!(v->vehstatus & VS_CRASHED));
03873 
03874   if (!TrainCanLeaveTile(v)) return INVALID_TILE;
03875 
03876   DiagDirection dir = TrainExitDir(v->direction, v->track);
03877   TileIndex tile = v->tile + TileOffsByDiagDir(dir);
03878 
03879   /* not a crossing || wrong axis || unusable rail (wrong type or owner) */
03880   if (!IsLevelCrossingTile(tile) || DiagDirToAxis(dir) == GetCrossingRoadAxis(tile) ||
03881       !CheckCompatibleRail(v, tile)) {
03882     return INVALID_TILE;
03883   }
03884 
03885   return tile;
03886 }
03887 
03888 
03895 static bool TrainCheckIfLineEnds(Train *v)
03896 {
03897   /* First, handle broken down train */
03898 
03899   int t = v->breakdown_ctr;
03900   if (t > 1) {
03901     v->vehstatus |= VS_TRAIN_SLOWING;
03902 
03903     uint16 break_speed = _breakdown_speeds[GB(~t, 4, 4)];
03904     if (break_speed < v->cur_speed) v->cur_speed = break_speed;
03905   } else {
03906     v->vehstatus &= ~VS_TRAIN_SLOWING;
03907   }
03908 
03909   if (!TrainCanLeaveTile(v)) return true;
03910 
03911   /* Determine the non-diagonal direction in which we will exit this tile */
03912   DiagDirection dir = TrainExitDir(v->direction, v->track);
03913   /* Calculate next tile */
03914   TileIndex tile = v->tile + TileOffsByDiagDir(dir);
03915 
03916   /* Determine the track status on the next tile */
03917   TrackStatus ts = GetTileTrackStatus(tile, TRANSPORT_RAIL, 0, ReverseDiagDir(dir));
03918   TrackdirBits reachable_trackdirs = DiagdirReachesTrackdirs(dir);
03919 
03920   TrackdirBits trackdirbits = TrackStatusToTrackdirBits(ts) & reachable_trackdirs;
03921   TrackdirBits red_signals = TrackStatusToRedSignals(ts) & reachable_trackdirs;
03922 
03923   /* We are sure the train is not entering a depot, it is detected above */
03924 
03925   /* mask unreachable track bits if we are forbidden to do 90deg turns */
03926   TrackBits bits = TrackdirBitsToTrackBits(trackdirbits);
03927   if (_settings_game.pf.forbid_90_deg) {
03928     bits &= ~TrackCrossesTracks(FindFirstTrack(v->track));
03929   }
03930 
03931   /* no suitable trackbits at all || unusable rail (wrong type or owner) */
03932   if (bits == TRACK_BIT_NONE || !CheckCompatibleRail(v, tile)) {
03933     return TrainApproachingLineEnd(v, false);
03934   }
03935 
03936   /* approaching red signal */
03937   if ((trackdirbits & red_signals) != 0) return TrainApproachingLineEnd(v, true);
03938 
03939   /* approaching a rail/road crossing? then make it red */
03940   if (IsLevelCrossingTile(tile)) MaybeBarCrossingWithSound(tile);
03941 
03942   return true;
03943 }
03944 
03945 
03946 static bool TrainLocoHandler(Train *v, bool mode)
03947 {
03948   /* train has crashed? */
03949   if (v->vehstatus & VS_CRASHED) {
03950     return mode ? true : HandleCrashedTrain(v); // 'this' can be deleted here
03951   }
03952 
03953   if (v->force_proceed != 0) {
03954     ClrBit(v->flags, VRF_TRAIN_STUCK);
03955     SetWindowWidgetDirty(WC_VEHICLE_VIEW, v->index, VVW_WIDGET_START_STOP_VEH);
03956   }
03957 
03958   /* train is broken down? */
03959   if (v->breakdown_ctr != 0) {
03960     if (v->breakdown_ctr <= 2) {
03961       HandleBrokenTrain(v);
03962       return true;
03963     }
03964     if (!v->current_order.IsType(OT_LOADING)) v->breakdown_ctr--;
03965   }
03966 
03967   if (HasBit(v->flags, VRF_REVERSING) && v->cur_speed == 0) {
03968     ReverseTrainDirection(v);
03969   }
03970 
03971   /* exit if train is stopped */
03972   if ((v->vehstatus & VS_STOPPED) && v->cur_speed == 0) return true;
03973 
03974   bool valid_order = !v->current_order.IsType(OT_NOTHING) && v->current_order.GetType() != OT_CONDITIONAL;
03975   if (ProcessOrders(v) && CheckReverseTrain(v)) {
03976     v->time_counter = 0;
03977     v->cur_speed = 0;
03978     v->subspeed = 0;
03979     ReverseTrainDirection(v);
03980     return true;
03981   }
03982 
03983   v->HandleLoading(mode);
03984 
03985   if (v->current_order.IsType(OT_LOADING)) return true;
03986 
03987   if (CheckTrainStayInDepot(v)) return true;
03988 
03989   if (!mode) HandleLocomotiveSmokeCloud(v);
03990 
03991   /* We had no order but have an order now, do look ahead. */
03992   if (!valid_order && !v->current_order.IsType(OT_NOTHING)) {
03993     CheckNextTrainTile(v);
03994   }
03995 
03996   /* Handle stuck trains. */
03997   if (!mode && HasBit(v->flags, VRF_TRAIN_STUCK)) {
03998     ++v->time_counter;
03999 
04000     /* Should we try reversing this tick if still stuck? */
04001     bool turn_around = v->time_counter % (_settings_game.pf.wait_for_pbs_path * DAY_TICKS) == 0 && _settings_game.pf.wait_for_pbs_path < 255;
04002 
04003     if (!turn_around && v->time_counter % _settings_game.pf.path_backoff_interval != 0 && v->force_proceed == 0) return true;
04004     if (!TryPathReserve(v)) {
04005       /* Still stuck. */
04006       if (turn_around) ReverseTrainDirection(v);
04007 
04008       if (HasBit(v->flags, VRF_TRAIN_STUCK) && v->time_counter > 2 * _settings_game.pf.wait_for_pbs_path * DAY_TICKS) {
04009         /* Show message to player. */
04010         if (_settings_client.gui.lost_train_warn && v->owner == _local_company) {
04011           SetDParam(0, v->index);
04012           AddVehicleNewsItem(
04013             STR_NEWS_TRAIN_IS_STUCK,
04014             NS_ADVICE,
04015             v->index
04016           );
04017         }
04018         v->time_counter = 0;
04019       }
04020       /* Exit if force proceed not pressed, else reset stuck flag anyway. */
04021       if (v->force_proceed == 0) return true;
04022       ClrBit(v->flags, VRF_TRAIN_STUCK);
04023       v->time_counter = 0;
04024       SetWindowWidgetDirty(WC_VEHICLE_VIEW, v->index, VVW_WIDGET_START_STOP_VEH);
04025     }
04026   }
04027 
04028   if (v->current_order.IsType(OT_LEAVESTATION)) {
04029     v->current_order.Free();
04030     SetWindowWidgetDirty(WC_VEHICLE_VIEW, v->index, VVW_WIDGET_START_STOP_VEH);
04031     return true;
04032   }
04033 
04034   int j = UpdateTrainSpeed(v);
04035 
04036   /* we need to invalidate the widget if we are stopping from 'Stopping 0 km/h' to 'Stopped' */
04037   if (v->cur_speed == 0 && v->tcache.last_speed == 0 && (v->vehstatus & VS_STOPPED)) {
04038     /* If we manually stopped, we're not force-proceeding anymore. */
04039     v->force_proceed = 0;
04040     SetWindowWidgetDirty(WC_VEHICLE_VIEW, v->index, VVW_WIDGET_START_STOP_VEH);
04041   }
04042 
04043   int adv_spd = (v->direction & 1) ? 192 : 256;
04044   if (j < adv_spd) {
04045     /* if the vehicle has speed 0, update the last_speed field. */
04046     if (v->cur_speed == 0) SetLastSpeed(v, v->cur_speed);
04047   } else {
04048     TrainCheckIfLineEnds(v);
04049     /* Loop until the train has finished moving. */
04050     for (;;) {
04051       j -= adv_spd;
04052       TrainController(v, NULL);
04053       /* Don't continue to move if the train crashed. */
04054       if (CheckTrainCollision(v)) break;
04055       /* 192 spd used for going straight, 256 for going diagonally. */
04056       adv_spd = (v->direction & 1) ? 192 : 256;
04057 
04058       /* No more moving this tick */
04059       if (j < adv_spd || v->cur_speed == 0) break;
04060 
04061       OrderType order_type = v->current_order.GetType();
04062       /* Do not skip waypoints (incl. 'via' stations) when passing through at full speed. */
04063       if ((order_type == OT_GOTO_WAYPOINT || order_type == OT_GOTO_STATION) &&
04064             (v->current_order.GetNonStopType() & ONSF_NO_STOP_AT_DESTINATION_STATION) &&
04065             IsTileType(v->tile, MP_STATION) &&
04066             v->current_order.GetDestination() == GetStationIndex(v->tile)) {
04067         ProcessOrders(v);
04068       }
04069     }
04070     SetLastSpeed(v, v->cur_speed);
04071   }
04072 
04073   for (Train *u = v; u != NULL; u = u->Next()) {
04074     if ((u->vehstatus & VS_HIDDEN) != 0) continue;
04075 
04076     u->UpdateViewport(false, false);
04077   }
04078 
04079   if (v->progress == 0) v->progress = j; // Save unused spd for next time, if TrainController didn't set progress
04080 
04081   return true;
04082 }
04083 
04084 
04085 
04086 Money Train::GetRunningCost() const
04087 {
04088   Money cost = 0;
04089   const Train *v = this;
04090 
04091   do {
04092     const Engine *e = Engine::Get(v->engine_type);
04093     if (e->u.rail.running_cost_class == INVALID_PRICE) continue;
04094 
04095     uint cost_factor = GetVehicleProperty(v, PROP_TRAIN_RUNNING_COST_FACTOR, e->u.rail.running_cost);
04096     if (cost_factor == 0) continue;
04097 
04098     /* Halve running cost for multiheaded parts */
04099     if (v->IsMultiheaded()) cost_factor /= 2;
04100 
04101     cost += GetPrice(e->u.rail.running_cost_class, cost_factor, e->grffile);
04102   } while ((v = v->GetNextVehicle()) != NULL);
04103 
04104   return cost;
04105 }
04106 
04107 
04108 bool Train::Tick()
04109 {
04110   this->tick_counter++;
04111 
04112   if (this->IsFrontEngine()) {
04113     if (!(this->vehstatus & VS_STOPPED)) this->running_ticks++;
04114 
04115     this->current_order_time++;
04116 
04117     if (!TrainLocoHandler(this, false)) return false;
04118 
04119     return TrainLocoHandler(this, true);
04120   } else if (this->IsFreeWagon() && (this->vehstatus & VS_CRASHED)) {
04121     /* Delete flooded standalone wagon chain */
04122     if (++this->crash_anim_pos >= 4400) {
04123       delete this;
04124       return false;
04125     }
04126   }
04127 
04128   return true;
04129 }
04130 
04131 static void CheckIfTrainNeedsService(Train *v)
04132 {
04133   if (Company::Get(v->owner)->settings.vehicle.servint_trains == 0 || !v->NeedsAutomaticServicing()) return;
04134   if (v->IsInDepot()) {
04135     VehicleServiceInDepot(v);
04136     return;
04137   }
04138 
04139   uint max_penalty;
04140   switch (_settings_game.pf.pathfinder_for_trains) {
04141     case VPF_NPF:  max_penalty = _settings_game.pf.npf.maximum_go_to_depot_penalty;  break;
04142     case VPF_YAPF: max_penalty = _settings_game.pf.yapf.maximum_go_to_depot_penalty; break;
04143     default: NOT_REACHED();
04144   }
04145 
04146   FindDepotData tfdd = FindClosestTrainDepot(v, max_penalty);
04147   /* Only go to the depot if it is not too far out of our way. */
04148   if (tfdd.best_length == UINT_MAX || tfdd.best_length > max_penalty) {
04149     if (v->current_order.IsType(OT_GOTO_DEPOT)) {
04150       /* If we were already heading for a depot but it has
04151        * suddenly moved farther away, we continue our normal
04152        * schedule? */
04153       v->current_order.MakeDummy();
04154       SetWindowWidgetDirty(WC_VEHICLE_VIEW, v->index, VVW_WIDGET_START_STOP_VEH);
04155     }
04156     return;
04157   }
04158 
04159   DepotID depot = GetDepotIndex(tfdd.tile);
04160 
04161   if (v->current_order.IsType(OT_GOTO_DEPOT) &&
04162       v->current_order.GetDestination() != depot &&
04163       !Chance16(3, 16)) {
04164     return;
04165   }
04166 
04167   v->current_order.MakeGoToDepot(depot, ODTFB_SERVICE);
04168   v->dest_tile = tfdd.tile;
04169   SetWindowWidgetDirty(WC_VEHICLE_VIEW, v->index, VVW_WIDGET_START_STOP_VEH);
04170 }
04171 
04172 void Train::OnNewDay()
04173 {
04174   if ((++this->day_counter & 7) == 0) DecreaseVehicleValue(this);
04175 
04176   if (this->IsFrontEngine()) {
04177     CheckVehicleBreakdown(this);
04178     AgeVehicle(this);
04179 
04180     CheckIfTrainNeedsService(this);
04181 
04182     CheckOrders(this);
04183 
04184     /* update destination */
04185     if (this->current_order.IsType(OT_GOTO_STATION)) {
04186       TileIndex tile = Station::Get(this->current_order.GetDestination())->train_station.tile;
04187       if (tile != INVALID_TILE) this->dest_tile = tile;
04188     }
04189 
04190     if (this->running_ticks != 0) {
04191       /* running costs */
04192       CommandCost cost(EXPENSES_TRAIN_RUN, this->GetRunningCost() * this->running_ticks / (DAYS_IN_YEAR  * DAY_TICKS));
04193 
04194       this->profit_this_year -= cost.GetCost();
04195       this->running_ticks = 0;
04196 
04197       SubtractMoneyFromCompanyFract(this->owner, cost);
04198 
04199       SetWindowDirty(WC_VEHICLE_DETAILS, this->index);
04200       SetWindowClassesDirty(WC_TRAINS_LIST);
04201     }
04202   } else if (this->IsEngine()) {
04203     /* Also age engines that aren't front engines */
04204     AgeVehicle(this);
04205   }
04206 }
04207 
04208 Trackdir Train::GetVehicleTrackdir() const
04209 {
04210   if (this->vehstatus & VS_CRASHED) return INVALID_TRACKDIR;
04211 
04212   if (this->track == TRACK_BIT_DEPOT) {
04213     /* We'll assume the train is facing outwards */
04214     return DiagDirToDiagTrackdir(GetRailDepotDirection(this->tile)); // Train in depot
04215   }
04216 
04217   if (this->track == TRACK_BIT_WORMHOLE) {
04218     /* train in tunnel or on bridge, so just use his direction and assume a diagonal track */
04219     return DiagDirToDiagTrackdir(DirToDiagDir(this->direction));
04220   }
04221 
04222   return TrackDirectionToTrackdir(FindFirstTrack(this->track), this->direction);
04223 }

Generated on Wed Dec 23 23:27:56 2009 for OpenTTD by  doxygen 1.5.6