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

Function CmdBuildRailWagon

src/train_cmd.cpp:639–714  ·  view source on GitHub ↗

* Build a railroad wagon. * @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

637 * @return the cost of this operation or an error.
638 */
639static CommandCost CmdBuildRailWagon(DoCommandFlags flags, TileIndex tile, const Engine *e, Vehicle **ret)
640{
641 const RailVehicleInfo *rvi = &e->VehInfo<RailVehicleInfo>();
642
643 /* Check that the wagon can drive on the track in question */
644 if (!IsCompatibleRail(rvi->railtypes, GetRailType(tile))) return CMD_ERROR;
645
646 if (flags.Test(DoCommandFlag::Execute)) {
647 Train *v = new Train();
648 *ret = v;
649 v->spritenum = rvi->image_index;
650
651 v->engine_type = e->index;
652 v->gcache.first_engine = EngineID::Invalid(); // needs to be set before first callback
653
654 DiagDirection dir = GetRailDepotDirection(tile);
655
656 v->direction = DiagDirToDir(dir);
657 v->tile = tile;
658
659 int x = TileX(tile) * TILE_SIZE | _vehicle_initial_x_fract[dir];
660 int y = TileY(tile) * TILE_SIZE | _vehicle_initial_y_fract[dir];
661
662 v->x_pos = x;
663 v->y_pos = y;
664 v->z_pos = GetSlopePixelZ(x, y, true);
665 v->owner = _current_company;
666 v->track = TRACK_BIT_DEPOT;
667 v->vehstatus = {VehState::Hidden, VehState::DefaultPalette};
668
669 v->SetWagon();
670
671 v->SetFreeWagon();
672 InvalidateWindowData(WC_VEHICLE_DEPOT, v->tile);
673
674 v->cargo_type = e->GetDefaultCargoType();
675 assert(IsValidCargoType(v->cargo_type));
676 v->cargo_cap = rvi->capacity;
677 v->refit_cap = 0;
678
679 v->railtypes = rvi->railtypes;
680
681 v->date_of_last_service = TimerGameEconomy::date;
682 v->date_of_last_service_newgrf = TimerGameCalendar::date;
683 v->build_year = TimerGameCalendar::year;
684 v->sprite_cache.sprite_seq.Set(SPR_IMG_QUERY);
685 v->random_bits = Random();
686
687 v->group_id = DEFAULT_GROUP;
688
689 auto prob = TestVehicleBuildProbability(v, v->engine_type, BuildProbabilityType::Reversed);
690 if (prob.has_value()) v->flags.Set(VehicleRailFlag::Flipped, prob.value());
691 AddArticulatedParts(v);
692
693 v->UpdatePosition();
694 v->First()->ConsistChanged(CCF_ARRANGE);
695 UpdateTrainGroupID(v->First());
696

Callers 1

CmdBuildRailVehicleFunction · 0.85

Calls 15

IsCompatibleRailFunction · 0.85
GetRailTypeFunction · 0.85
InvalidFunction · 0.85
GetRailDepotDirectionFunction · 0.85
DiagDirToDirFunction · 0.85
TileXFunction · 0.85
TileYFunction · 0.85
GetSlopePixelZFunction · 0.85
IsValidCargoTypeFunction · 0.85
AddArticulatedPartsFunction · 0.85
UpdateTrainGroupIDFunction · 0.85

Tested by

no test coverage detected