Update readme and changelog for v1.26.0
[openttd-joker.git] / src / plans_cmd.cpp
blobe3f5ed98401e88ecb29a101d526e081959817399
1 /* $Id$ */
3 /*
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/>.
8 */
10 /** @file plans_cmd.cpp Handling of plan related commands. */
12 #include "stdafx.h"
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"
22 /**
23 * Create a new plan.
24 * @param tile unused
25 * @param flags type of operation
26 * @param p1 owner of the plan
27 * @param p2 unused
28 * @param text unused
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) {
35 Owner o = (Owner)p1;
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);
43 return CommandCost();
46 /**
47 * Create a new line in a plan.
48 * @param tile unused
49 * @param flags type of operation
50 * @param p1 plan id
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);
70 return CommandCost();
73 /**
74 * Edit the visibility of a plan.
75 * @param tile unused
76 * @param flags type of operation
77 * @param p1 plan id
78 * @param p2 visibility
79 * @param text unused
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);
90 return CommandCost();
93 /**
94 * Delete a plan.
95 * @param tile unused
96 * @param flags type of operation
97 * @param p1 plan id
98 * @param p2 unused
99 * @param text unused
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;
112 delete p;
114 return CommandCost();
118 * Remove a line from a plan.
119 * @param tile unused
120 * @param flags type of operation
121 * @param p1 plan id
122 * @param p2 line id
123 * @param text unused
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);
133 delete *it;
134 p->lines.erase(it);
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
145 * @param tile unused
146 * @param flags type of operation
147 * @param p1 ID of plan to name
148 * @param p2 unused
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) {
166 plan->name = text;
167 InvalidateWindowClassesData(WC_PLANS);
170 return CommandCost();