MCPcopy Create free account
hub / github.com/OpenTTD/OpenTTD / CmdBuildRailVehicle

Function CmdBuildRailVehicle

src/train_cmd.cpp:773–847  ·  view source on GitHub ↗

* Build a railroad vehicle. * @param flags type of operation. * @param tile tile of the depot where rail-vehicle is built. * @param e the engine to build. * @param[out] ret the vehicle that has been built. * @return the cost of this operation or an error. */

Source from the content-addressed store, hash-verified

771 * @return the cost of this operation or an error.
772 */
773CommandCost CmdBuildRailVehicle(DoCommandFlags flags, TileIndex tile, const Engine *e, Vehicle **ret)
774{
775 const RailVehicleInfo *rvi = &e->VehInfo<RailVehicleInfo>();
776
777 if (rvi->railveh_type == RAILVEH_WAGON) return CmdBuildRailWagon(flags, tile, e, ret);
778
779 /* Check if depot and new engine uses the same kind of tracks *
780 * We need to see if the engine got power on the tile to avoid electric engines in non-electric depots */
781 if (!HasPowerOnRail(rvi->railtypes, GetRailType(tile))) return CMD_ERROR;
782
783 if (flags.Test(DoCommandFlag::Execute)) {
784 DiagDirection dir = GetRailDepotDirection(tile);
785 int x = TileX(tile) * TILE_SIZE + _vehicle_initial_x_fract[dir];
786 int y = TileY(tile) * TILE_SIZE + _vehicle_initial_y_fract[dir];
787
788 Train *v = new Train();
789 *ret = v;
790 v->direction = DiagDirToDir(dir);
791 v->tile = tile;
792 v->owner = _current_company;
793 v->x_pos = x;
794 v->y_pos = y;
795 v->z_pos = GetSlopePixelZ(x, y, true);
796 v->track = TRACK_BIT_DEPOT;
797 v->vehstatus = {VehState::Hidden, VehState::Stopped, VehState::DefaultPalette};
798 v->spritenum = rvi->image_index;
799 v->cargo_type = e->GetDefaultCargoType();
800 assert(IsValidCargoType(v->cargo_type));
801 v->cargo_cap = rvi->capacity;
802 v->refit_cap = 0;
803 v->last_station_visited = StationID::Invalid();
804 v->last_loading_station = StationID::Invalid();
805
806 v->engine_type = e->index;
807 v->gcache.first_engine = EngineID::Invalid(); // needs to be set before first callback
808
809 v->reliability = e->reliability;
810 v->reliability_spd_dec = e->reliability_spd_dec;
811 v->max_age = e->GetLifeLengthInDays();
812
813 v->railtypes = rvi->railtypes;
814
815 v->SetServiceInterval(Company::Get(_current_company)->settings.vehicle.servint_trains);
816 v->date_of_last_service = TimerGameEconomy::date;
817 v->date_of_last_service_newgrf = TimerGameCalendar::date;
818 v->build_year = TimerGameCalendar::year;
819 v->sprite_cache.sprite_seq.Set(SPR_IMG_QUERY);
820 v->random_bits = Random();
821
822 if (e->flags.Test(EngineFlag::ExclusivePreview)) v->vehicle_flags.Set(VehicleFlag::BuiltAsPrototype);
823 v->SetServiceIntervalIsPercent(Company::Get(_current_company)->settings.vehicle.servint_ispercent);
824
825 v->group_id = DEFAULT_GROUP;
826
827 v->SetFrontEngine();
828 v->SetEngine();
829
830 auto prob = TestVehicleBuildProbability(v, v->engine_type, BuildProbabilityType::Reversed);

Callers 1

CmdBuildVehicleFunction · 0.85

Calls 15

CmdBuildRailWagonFunction · 0.85
HasPowerOnRailFunction · 0.85
GetRailTypeFunction · 0.85
GetRailDepotDirectionFunction · 0.85
TileXFunction · 0.85
TileYFunction · 0.85
DiagDirToDirFunction · 0.85
GetSlopePixelZFunction · 0.85
IsValidCargoTypeFunction · 0.85
InvalidFunction · 0.85

Tested by

no test coverage detected