* Create a new vehicle group. * @param flags type of operation * @param vt vehicle type * @param parent_group parent groupid * @return the cost of this operation or an error */
| 346 | * @return the cost of this operation or an error |
| 347 | */ |
| 348 | std::tuple<CommandCost, GroupID> CmdCreateGroup(DoCommandFlags flags, VehicleType vt, GroupID parent_group) |
| 349 | { |
| 350 | if (!IsCompanyBuildableVehicleType(vt)) return { CMD_ERROR, GroupID::Invalid() }; |
| 351 | |
| 352 | if (!Group::CanAllocateItem()) return { CMD_ERROR, GroupID::Invalid() }; |
| 353 | |
| 354 | Group *pg = Group::GetIfValid(parent_group); |
| 355 | if (pg != nullptr) { |
| 356 | if (pg->owner != _current_company) return { CMD_ERROR, GroupID::Invalid() }; |
| 357 | if (pg->vehicle_type != vt) return { CMD_ERROR, GroupID::Invalid() }; |
| 358 | } |
| 359 | |
| 360 | if (flags.Test(DoCommandFlag::Execute)) { |
| 361 | Group *g = new Group(_current_company, vt); |
| 362 | |
| 363 | Company *c = Company::Get(g->owner); |
| 364 | g->number = c->freegroups.UseID(c->freegroups.NextID()); |
| 365 | if (pg == nullptr) { |
| 366 | g->livery.colour1 = c->livery[LS_DEFAULT].colour1; |
| 367 | g->livery.colour2 = c->livery[LS_DEFAULT].colour2; |
| 368 | if (c->settings.renew_keep_length) g->flags.Set(GroupFlag::ReplaceWagonRemoval); |
| 369 | } else { |
| 370 | g->parent = pg->index; |
| 371 | g->livery.colour1 = pg->livery.colour1; |
| 372 | g->livery.colour2 = pg->livery.colour2; |
| 373 | g->flags = pg->flags; |
| 374 | pg->children.insert(g->index); |
| 375 | } |
| 376 | |
| 377 | InvalidateWindowData(GetWindowClassForVehicleType(vt), VehicleListIdentifier(VL_GROUP_LIST, vt, _current_company).ToWindowNumber()); |
| 378 | InvalidateWindowData(WC_COMPANY_COLOUR, g->owner, g->vehicle_type); |
| 379 | |
| 380 | return { CommandCost(), g->index }; |
| 381 | } |
| 382 | |
| 383 | return { CommandCost(), GroupID::Invalid()}; |
| 384 | } |
| 385 | |
| 386 | |
| 387 | /** |
no test coverage detected