From 5047b2ef4e35b16fb6cff30aab3d144d190efd4d Mon Sep 17 00:00:00 2001 From: Jonathan G Rennison Date: Tue, 27 Feb 2024 18:15:07 +0000 Subject: [PATCH] Plans: Allow taking ownership of unowned plans Except for unauthed network clients --- src/command.cpp | 2 ++ src/command_type.h | 1 + src/lang/extra/english.txt | 2 ++ src/plans_cmd.cpp | 30 ++++++++++++++++++++++++++++++ src/plans_gui.cpp | 26 +++++++++++++++++++++++--- src/widgets/plans_widget.h | 2 ++ 6 files changed, 60 insertions(+), 3 deletions(-) diff --git a/src/command.cpp b/src/command.cpp index fd6b9a9d9e..644b8ccfa0 100644 --- a/src/command.cpp +++ b/src/command.cpp @@ -315,6 +315,7 @@ CommandProc CmdRemovePlanLine; CommandProc CmdChangePlanVisibility; CommandProc CmdChangePlanColour; CommandProc CmdRenamePlan; +CommandProc CmdAcquireUnownedPlan; CommandProc CmdDesyncCheck; @@ -581,6 +582,7 @@ static const Command _command_proc_table[] = { DEF_CMD(CmdChangePlanVisibility, 0, CMDT_OTHER_MANAGEMENT ), // CMD_CHANGE_PLAN_VISIBILITY DEF_CMD(CmdChangePlanColour, 0, CMDT_OTHER_MANAGEMENT ), // CMD_CHANGE_PLAN_COLOUR DEF_CMD(CmdRenamePlan, 0, CMDT_OTHER_MANAGEMENT ), // CMD_RENAME_PLAN + DEF_CMD(CmdAcquireUnownedPlan, CMD_SERVER_NS, CMDT_OTHER_MANAGEMENT ), // CMD_ACQUIRE_UNOWNED_PLAN DEF_CMD(CmdDesyncCheck, CMD_SERVER, CMDT_SERVER_SETTING ), // CMD_DESYNC_CHECK }; diff --git a/src/command_type.h b/src/command_type.h index 9d02060716..4ba223ffbf 100644 --- a/src/command_type.h +++ b/src/command_type.h @@ -531,6 +531,7 @@ enum Commands { CMD_CHANGE_PLAN_VISIBILITY, CMD_CHANGE_PLAN_COLOUR, CMD_RENAME_PLAN, + CMD_ACQUIRE_UNOWNED_PLAN, CMD_DESYNC_CHECK, ///< Force desync checks to be run diff --git a/src/lang/extra/english.txt b/src/lang/extra/english.txt index d14c4c5db1..b0813d54c8 100644 --- a/src/lang/extra/english.txt +++ b/src/lang/extra/english.txt @@ -1854,6 +1854,8 @@ STR_PLANS_COLOUR :{BLACK}Colour STR_PLANS_COLOUR_TOOLTIP :{BLACK}Set the colour of a plan. STR_PLANS_DELETE :{BLACK}Delete STR_PLANS_DELETE_TOOLTIP :{BLACK}Delete the selected item in the list +STR_PLANS_TAKE_OWNERSHIP :{BLACK}Take ownership +STR_PLANS_TAKE_OWNERSHIP_TOOLTIP :{BLACK}Take ownership of the selected unowned item in the list STR_PLANS_LIST_ITEM_PLAN_PRIVATE :[Private]: {STRING3} STR_PLANS_LIST_ITEM_PLAN :Plan #{NUM}: {NUM} line{P "" s} ({DATE_SHORT}) STR_PLANS_LIST_ITEM_NAMED_PLAN :{RAW_STRING}: {NUM} line{P "" s} ({DATE_SHORT}) diff --git a/src/plans_cmd.cpp b/src/plans_cmd.cpp index 19ca84eb74..b44e04ee84 100644 --- a/src/plans_cmd.cpp +++ b/src/plans_cmd.cpp @@ -14,6 +14,7 @@ #include "plans_func.h" #include "window_func.h" #include "company_func.h" +#include "company_base.h" #include "string_func.h" #include "window_gui.h" #include "table/strings.h" @@ -247,3 +248,32 @@ CommandCost CmdRenamePlan(TileIndex tile, DoCommandFlag flags, uint32_t p1, uint return CommandCost(); } + +/** +* Acquire an unowned plan +* @param tile unused +* @param flags type of operation +* @param p1 ID of plan +* @param p2 unused +* @param text unused +* @return the cost of this operation or an error +*/ +CommandCost CmdAcquireUnownedPlan(TileIndex tile, DoCommandFlag flags, uint32_t p1, uint32_t p2, const char *text) +{ + Plan *p = Plan::GetIfValid(p1); + if (p == nullptr) return CMD_ERROR; + if (Company::IsValidID(p->owner)) return CMD_ERROR; + if (!Company::IsValidID(_current_company)) return CMD_ERROR; + + if (flags & DC_EXEC) { + p->owner = _current_company; + InvalidateWindowClassesData(WC_PLANS); + if (p->visible) { + for (PlanLine *line : p->lines) { + if (line->visible) line->MarkDirty(); + } + } + } + + return CommandCost(); +} diff --git a/src/plans_gui.cpp b/src/plans_gui.cpp index d45a16f32f..2258bcd909 100644 --- a/src/plans_gui.cpp +++ b/src/plans_gui.cpp @@ -59,7 +59,10 @@ static constexpr NWidgetPart _nested_plans_widgets[] = { NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_PLN_SHOW_ALL), SetResize(1, 0), SetFill(1, 0), SetDataTip(STR_PLANS_SHOW_ALL, STR_PLANS_SHOW_ALL_TOOLTIP), EndContainer(), NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_PLN_DELETE), SetResize(1, 0), SetFill(1, 0), SetDataTip(STR_PLANS_DELETE, STR_PLANS_DELETE_TOOLTIP), - NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_PLN_RENAME), SetResize(1, 0), SetFill(1, 0), SetDataTip(STR_BUTTON_RENAME, STR_NULL), + NWidget(NWID_SELECTION, INVALID_COLOUR, WID_PLN_RENAME_SEL), + NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_PLN_RENAME), SetResize(1, 0), SetFill(1, 0), SetDataTip(STR_BUTTON_RENAME, STR_NULL), + NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_PLN_TAKE_OWNERSHIP), SetResize(1, 0), SetFill(1, 0), SetDataTip(STR_PLANS_TAKE_OWNERSHIP, STR_PLANS_TAKE_OWNERSHIP_TOOLTIP), + EndContainer(), EndContainer(), NWidget(WWT_RESIZEBOX, COLOUR_GREY), EndContainer(), @@ -82,6 +85,7 @@ struct PlansWindow : Window { Scrollbar *vscroll; NWidgetStacked *hide_all_sel; + NWidgetStacked *rename_sel; std::vector list; ///< The translation table linking panel indices to their related PlanID. int selected; ///< What item is currently selected in the panel. uint vis_btn_left; ///< left offset of visibility button @@ -94,6 +98,8 @@ struct PlansWindow : Window { this->vscroll = this->GetScrollbar(WID_PLN_SCROLLBAR); this->hide_all_sel = this->GetWidget(WID_PLN_HIDE_ALL_SEL); this->hide_all_sel->SetDisplayedPlane(0); + this->rename_sel = this->GetWidget(WID_PLN_RENAME_SEL); + this->rename_sel->SetDisplayedPlane(0); this->FinishInitNested(); this->selected = INT_MAX; @@ -145,6 +151,13 @@ struct PlansWindow : Window { break; } + case WID_PLN_TAKE_OWNERSHIP: { + if (_current_plan != nullptr && !IsNonAdminNetworkClient()) { + DoCommandP(0, this->list[this->selected].plan_id, 0, CMD_ACQUIRE_UNOWNED_PLAN); + } + break; + } + case WID_PLN_SHOW_ALL: { for (Plan *p : Plan::Iterate()) { if (p->IsListable()) p->SetVisibility(true); @@ -255,11 +268,14 @@ struct PlansWindow : Window { this->SetWidgetDisabledState(WID_PLN_HIDE_ALL, this->vscroll->GetCount() == 0); this->SetWidgetDisabledState(WID_PLN_SHOW_ALL, this->vscroll->GetCount() == 0); this->hide_all_sel->SetDisplayedPlane(this->vscroll->GetCount() != 0 && this->AllPlansHidden() ? 1 : 0); - if (_current_plan) { + if (_current_plan != nullptr) { this->SetWidgetsDisabledState(_current_plan->owner != _local_company, WID_PLN_ADD_LINES, WID_PLN_VISIBILITY, WID_PLN_DELETE, WID_PLN_RENAME, WID_PLN_COLOUR); this->GetWidget(WID_PLN_VISIBILITY)->widget_data = _current_plan->visible_by_all ? STR_PLANS_VISIBILITY_PRIVATE : STR_PLANS_VISIBILITY_PUBLIC; + this->SetWidgetDisabledState(WID_PLN_TAKE_OWNERSHIP, Company::IsValidID(_current_plan->owner) || IsNonAdminNetworkClient()); + this->rename_sel->SetDisplayedPlane(Company::IsValidID(_current_plan->owner) || !Company::IsValidID(_current_company) ? 0 : 1); } else { - this->SetWidgetsDisabledState(true, WID_PLN_ADD_LINES, WID_PLN_VISIBILITY, WID_PLN_DELETE, WID_PLN_RENAME, WID_PLN_COLOUR); + this->SetWidgetsDisabledState(true, WID_PLN_ADD_LINES, WID_PLN_VISIBILITY, WID_PLN_DELETE, WID_PLN_RENAME, WID_PLN_COLOUR, WID_PLN_TAKE_OWNERSHIP); + this->rename_sel->SetDisplayedPlane(0); } this->DrawWidgets(); } @@ -369,6 +385,10 @@ struct PlansWindow : Window { case WID_PLN_RENAME: *size = adddim(GetStringBoundingBox(STR_BUTTON_RENAME), padding); break; + + case WID_PLN_TAKE_OWNERSHIP: + *size = adddim(GetStringBoundingBox(STR_PLANS_TAKE_OWNERSHIP), padding); + break; } } diff --git a/src/widgets/plans_widget.h b/src/widgets/plans_widget.h index 0e3a07faf5..7ab11f6d4b 100644 --- a/src/widgets/plans_widget.h +++ b/src/widgets/plans_widget.h @@ -24,6 +24,8 @@ enum PlansWidgets : WidgetID { WID_PLN_DELETE, WID_PLN_HIDE_ALL_SEL, WID_PLN_RENAME, + WID_PLN_RENAME_SEL, + WID_PLN_TAKE_OWNERSHIP, }; #endif /* WIDGETS_PLANS_WIDGET_H */