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

Function CmdAddVehicleGroup

src/group_cmd.cpp:561–617  ·  view source on GitHub ↗

* Add a vehicle to a group * @param flags type of operation * @param group_id index of group * @param veh_id vehicle to add to a group * @param add_shared Add shared vehicles as well. * @return the cost of this operation or an error */

Source from the content-addressed store, hash-verified

559 * @return the cost of this operation or an error
560 */
561std::tuple<CommandCost, GroupID> CmdAddVehicleGroup(DoCommandFlags flags, GroupID group_id, VehicleID veh_id, bool add_shared, const VehicleListIdentifier &vli)
562{
563 GroupID new_g = group_id;
564 if (!Group::IsValidID(new_g) && !IsDefaultGroupID(new_g) && new_g != NEW_GROUP) return { CMD_ERROR, GroupID::Invalid() };
565
566 VehicleList list;
567 if (veh_id == VehicleID::Invalid() && vli.Valid()) {
568 if (!GenerateVehicleSortList(&list, vli) || list.empty()) return { CMD_ERROR, GroupID::Invalid() };
569 } else {
570 const Vehicle *v = Vehicle::GetIfValid(veh_id);
571 if (v == nullptr) return { CMD_ERROR, GroupID::Invalid() };
572 list.push_back(v);
573 }
574
575 VehicleType vtype = list.front()->type;
576 for (const Vehicle *v : list) {
577 if (v->owner != _current_company || !v->IsPrimaryVehicle()) return { CMD_ERROR, GroupID::Invalid() };
578 }
579
580 if (Group::IsValidID(new_g)) {
581 const Group *g = Group::Get(new_g);
582 if (g->owner != _current_company || g->vehicle_type != vtype) return { CMD_ERROR, GroupID::Invalid() };
583 }
584
585 if (new_g == NEW_GROUP) {
586 /* Create new group. */
587 auto [ret, new_group_id] = CmdCreateGroup(flags, vtype, GroupID::Invalid());
588 if (ret.Failed()) return { ret, new_group_id };
589
590 new_g = new_group_id;
591 }
592
593 if (flags.Test(DoCommandFlag::Execute)) {
594 for (const Vehicle *vc : list) {
595 /* VehicleList is const but we need to modify the vehicle. */
596 Vehicle *v = Vehicle::Get(vc->index);
597 AddVehicleToGroup(v, new_g);
598
599 if (add_shared) {
600 /* Add vehicles in the shared order list as well. */
601 for (Vehicle *v2 = v->FirstShared(); v2 != nullptr; v2 = v2->NextShared()) {
602 if (v2->group_id != new_g) AddVehicleToGroup(v2, new_g);
603 }
604 }
605
606 SetWindowDirty(WC_VEHICLE_DEPOT, v->tile);
607 }
608
609 GroupStatistics::UpdateAutoreplace(_current_company);
610
611 /* Update the Replace Vehicle Windows */
612 SetWindowDirty(WC_REPLACE_VEHICLE, vtype);
613 InvalidateWindowData(GetWindowClassForVehicleType(vtype), VehicleListIdentifier(VL_GROUP_LIST, vtype, _current_company).ToWindowNumber());
614 }
615
616 return { CommandCost(), new_g };
617}
618

Callers

nothing calls this directly

Calls 15

IsDefaultGroupIDFunction · 0.85
InvalidFunction · 0.85
GenerateVehicleSortListFunction · 0.85
CmdCreateGroupFunction · 0.85
AddVehicleToGroupFunction · 0.85
SetWindowDirtyFunction · 0.85
CommandCostClass · 0.85
ValidMethod · 0.80
push_backMethod · 0.80
FailedMethod · 0.80

Tested by

no test coverage detected