4 * This file is part of OpenTTD.
5 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
6 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
7 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
10 /** @file plans_cmd.cpp Handling of plan related commands. */
13 #include "command_func.h"
14 #include "plans_base.h"
15 #include "plans_func.h"
16 #include "window_func.h"
17 #include "company_func.h"
18 #include "string_func.h"
19 #include "window_gui.h"
20 #include "table/strings.h"
25 * @param flags type of operation
26 * @param p1 owner of the plan
29 * @return the cost of this operation or an error
31 CommandCost
CmdAddPlan(TileIndex tile
, DoCommandFlag flags
, uint32 p1
, uint32 p2
, const char *text
)
33 if (!Plan::CanAllocateItem()) return CommandError(STR_ERROR_TOO_MANY_PLANS
);
34 if (flags
& DC_EXEC
) {
36 _new_plan
= new Plan(o
);
37 if (o
== _local_company
) {
38 _new_plan
->SetVisibility(true);
39 Window
*w
= FindWindowById(WC_PLANS
, 0);
40 if (w
) w
->InvalidateData(INVALID_PLAN
, false);
47 * Create a new line in a plan.
49 * @param flags type of operation
51 * @param p2 number of nodes
52 * @param text list of tile indexes that compose the line, encoded in base64
53 * @return the cost of this operation or an error
55 CommandCost
CmdAddPlanLine(TileIndex tile
, DoCommandFlag flags
, uint32 p1
, uint32 p2
, const char *text
)
57 if (flags
& DC_EXEC
) {
58 Plan
*p
= Plan::Get(p1
);
59 PlanLine
*pl
= p
->NewLine();
60 if (!pl
) return CommandError(STR_ERROR_NO_MORE_SPACE_FOR_LINES
);
61 if (p2
> (MAX_CMD_TEXT_LENGTH
/ sizeof(TileIndex
))) return CommandError(STR_ERROR_TOO_MANY_NODES
);
62 pl
->Import((const TileIndex
*)text
, p2
);
63 if (p
->IsListable()) {
64 pl
->SetVisibility(p
->visible
);
65 if (p
->visible
) pl
->MarkDirty();
66 Window
*w
= FindWindowById(WC_PLANS
, 0);
67 if (w
) w
->InvalidateData(INVALID_PLAN
, false);
74 * Edit the visibility of a plan.
76 * @param flags type of operation
78 * @param p2 visibility
80 * @return the cost of this operation or an error
82 CommandCost
CmdChangePlanVisibility(TileIndex tile
, DoCommandFlag flags
, uint32 p1
, uint32 p2
, const char *text
)
84 if (flags
& DC_EXEC
) {
85 Plan
*p
= Plan::Get(p1
);
86 p
->visible_by_all
= p2
!= 0;
87 Window
*w
= FindWindowById(WC_PLANS
, 0);
88 if (w
) w
->InvalidateData(INVALID_PLAN
, false);
96 * @param flags type of operation
100 * @return the cost of this operation or an error
102 CommandCost
CmdRemovePlan(TileIndex tile
, DoCommandFlag flags
, uint32 p1
, uint32 p2
, const char *text
)
104 if (flags
& DC_EXEC
) {
105 Plan
*p
= Plan::Get(p1
);
106 if (p
->IsListable()) {
107 p
->SetVisibility(false);
108 Window
*w
= FindWindowById(WC_PLANS
, 0);
109 if (w
) w
->InvalidateData(p
->index
, false);
111 if (p
== _current_plan
) _current_plan
= nullptr;
114 return CommandCost();
118 * Remove a line from a plan.
120 * @param flags type of operation
124 * @return the cost of this operation or an error
126 CommandCost
CmdRemovePlanLine(TileIndex tile
, DoCommandFlag flags
, uint32 p1
, uint32 p2
, const char *text
)
128 if (flags
& DC_EXEC
) {
129 Plan
*p
= Plan::Get(p1
);
130 PlanLineVector::iterator it
= p
->lines
.begin();
131 std::advance(it
, p2
);
132 (*it
)->SetVisibility(false);
135 if (p
->IsListable()) {
136 Window
*w
= FindWindowById(WC_PLANS
, 0);
137 if (w
) w
->InvalidateData(p
->index
, false);
140 return CommandCost();
144 * Give a custom name to your plan
146 * @param flags type of operation
147 * @param p1 ID of plan to name
149 * @param text the new name
150 * @return the cost of this operation or an error
152 CommandCost
CmdRenamePlan(TileIndex tile
, DoCommandFlag flags
, uint32 p1
, uint32 p2
, const char *text
)
154 if (text
== nullptr) return CommandError();
156 Plan
* plan
= Plan::GetIfValid(p1
);
157 if (plan
== nullptr) return CommandError();
159 CommandCost ret
= CheckOwnership(plan
->owner
);
161 if (ret
.Failed()) return ret
;
163 if (Utf8StringLength(text
) >= MAX_LENGTH_PLAN_NAME_CHARS
) return CommandError();
165 if (flags
& DC_EXEC
) {
167 InvalidateWindowClassesData(WC_PLANS
);
170 return CommandCost();