* Alter a group * @param flags type of operation * @param mode Operation to perform. * @param group_id GroupID * @param parent_id parent group index * @param text the new name or an empty string when resetting to the default * @return the cost of this operation or an error */
| 447 | * @return the cost of this operation or an error |
| 448 | */ |
| 449 | CommandCost CmdAlterGroup(DoCommandFlags flags, AlterGroupMode mode, GroupID group_id, GroupID parent_id, const std::string &text) |
| 450 | { |
| 451 | Group *g = Group::GetIfValid(group_id); |
| 452 | if (g == nullptr || g->owner != _current_company) return CMD_ERROR; |
| 453 | |
| 454 | if (mode == AlterGroupMode::Rename) { |
| 455 | /* Rename group */ |
| 456 | bool reset = text.empty(); |
| 457 | |
| 458 | if (!reset) { |
| 459 | if (Utf8StringLength(text) >= MAX_LENGTH_GROUP_NAME_CHARS) return CMD_ERROR; |
| 460 | } |
| 461 | |
| 462 | if (flags.Test(DoCommandFlag::Execute)) { |
| 463 | /* Assign the new one */ |
| 464 | if (reset) { |
| 465 | g->name.clear(); |
| 466 | } else { |
| 467 | g->name = text; |
| 468 | } |
| 469 | } |
| 470 | } else if (mode == AlterGroupMode::SetParent) { |
| 471 | /* Do nothing if the parent group isn't actually changed. */ |
| 472 | if (g->parent == parent_id) return CommandCost(); |
| 473 | |
| 474 | /* Set group parent */ |
| 475 | const Group *pg = Group::GetIfValid(parent_id); |
| 476 | |
| 477 | if (pg != nullptr) { |
| 478 | if (pg->owner != _current_company) return CMD_ERROR; |
| 479 | if (pg->vehicle_type != g->vehicle_type) return CMD_ERROR; |
| 480 | |
| 481 | /* Ensure request parent isn't child of group. |
| 482 | * This is the only place that infinite loops are prevented. */ |
| 483 | if (GroupIsInGroup(pg->index, g->index)) return CommandCost(STR_ERROR_GROUP_CAN_T_SET_PARENT_RECURSION); |
| 484 | } |
| 485 | |
| 486 | if (flags.Test(DoCommandFlag::Execute)) { |
| 487 | if (g->parent != GroupID::Invalid()) Group::Get(g->parent)->children.erase(g->index); |
| 488 | g->parent = (pg == nullptr) ? GroupID::Invalid() : pg->index; |
| 489 | if (g->parent != GroupID::Invalid()) Group::Get(g->parent)->children.insert(g->index); |
| 490 | |
| 491 | GroupStatistics::UpdateAutoreplace(g->owner); |
| 492 | |
| 493 | if (!g->livery.in_use.All({Livery::Flag::Primary, Livery::Flag::Secondary})) { |
| 494 | /* Update livery with new parent's colours if either colour is default. */ |
| 495 | const Livery *livery = GetParentLivery(g); |
| 496 | if (!g->livery.in_use.Test(Livery::Flag::Primary)) g->livery.colour1 = livery->colour1; |
| 497 | if (!g->livery.in_use.Test(Livery::Flag::Secondary)) g->livery.colour2 = livery->colour2; |
| 498 | |
| 499 | PropagateChildLivery(g, true); |
| 500 | MarkWholeScreenDirty(); |
| 501 | } |
| 502 | } |
| 503 | } else { |
| 504 | return CMD_ERROR; |
| 505 | } |
| 506 |
nothing calls this directly
no test coverage detected