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 "window_gui.h"
19 #include "table/strings.h"
24 * @param flags type of operation
25 * @param p1 owner of the plan
28 * @return the cost of this operation or an error
30 CommandCost
CmdAddPlan(TileIndex tile
, DoCommandFlag flags
, uint32 p1
, uint32 p2
, const char *text
)
32 if (!Plan::CanAllocateItem()) return_cmd_error(STR_ERROR_TOO_MANY_PLANS
);
33 if (flags
& DC_EXEC
) {
35 _new_plan
= new Plan(o
);
36 if (o
== _local_company
) {
37 _new_plan
->SetVisibility(true);
38 Window
*w
= FindWindowById(WC_PLANS
, 0);
39 if (w
) w
->InvalidateData(INVALID_PLAN
, false);
46 * Create a new line in a plan.
48 * @param flags type of operation
50 * @param p2 number of nodes
51 * @param text list of tile indexes that compose the line, encoded in base64
52 * @return the cost of this operation or an error
54 CommandCost
CmdAddPlanLine(TileIndex tile
, DoCommandFlag flags
, uint32 p1
, uint32 p2
, const char *text
)
56 if (flags
& DC_EXEC
) {
57 Plan
*p
= Plan::Get(p1
);
58 PlanLine
*pl
= p
->NewLine();
59 if (!pl
) return_cmd_error(STR_ERROR_NO_MORE_SPACE_FOR_LINES
);
60 if (p2
> (MAX_CMD_TEXT_LENGTH
/ sizeof(TileIndex
))) return_cmd_error(STR_ERROR_TOO_MANY_NODES
);
61 pl
->Import((const TileIndex
*)text
, p2
);
62 if (p
->IsListable()) {
63 pl
->SetVisibility(p
->visible
);
64 if (p
->visible
) pl
->MarkDirty();
65 Window
*w
= FindWindowById(WC_PLANS
, 0);
66 if (w
) w
->InvalidateData(INVALID_PLAN
, false);
73 * Edit the visibility of a plan.
75 * @param flags type of operation
77 * @param p2 visibility
79 * @return the cost of this operation or an error
81 CommandCost
CmdChangePlanVisibility(TileIndex tile
, DoCommandFlag flags
, uint32 p1
, uint32 p2
, const char *text
)
83 if (flags
& DC_EXEC
) {
84 Plan
*p
= Plan::Get(p1
);
85 p
->visible_by_all
= p2
!= 0;
86 Window
*w
= FindWindowById(WC_PLANS
, 0);
87 if (w
) w
->InvalidateData(INVALID_PLAN
, false);
95 * @param flags type of operation
99 * @return the cost of this operation or an error
101 CommandCost
CmdRemovePlan(TileIndex tile
, DoCommandFlag flags
, uint32 p1
, uint32 p2
, const char *text
)
103 if (flags
& DC_EXEC
) {
104 Plan
*p
= Plan::Get(p1
);
105 if (p
->IsListable()) {
106 p
->SetVisibility(false);
107 Window
*w
= FindWindowById(WC_PLANS
, 0);
108 if (w
) w
->InvalidateData(p
->index
, false);
110 if (p
== _current_plan
) _current_plan
= NULL
;
113 return CommandCost();
117 * Remove a line from a plan.
119 * @param flags type of operation
123 * @return the cost of this operation or an error
125 CommandCost
CmdRemovePlanLine(TileIndex tile
, DoCommandFlag flags
, uint32 p1
, uint32 p2
, const char *text
)
127 if (flags
& DC_EXEC
) {
128 Plan
*p
= Plan::Get(p1
);
129 PlanLineVector::iterator it
= p
->lines
.begin();
130 std::advance(it
, p2
);
131 (*it
)->SetVisibility(false);
134 if (p
->IsListable()) {
135 Window
*w
= FindWindowById(WC_PLANS
, 0);
136 if (w
) w
->InvalidateData(p
->index
, false);
139 return CommandCost();