Remove costly recalculation of a date format we already have.
[openttd-joker.git] / src / plans_cmd.cpp
blob3fab3bc02a67c73a0b5caf8a11e340a36aeb422a
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 "window_gui.h"
19 #include "table/strings.h"
21 /**
22 * Create a new plan.
23 * @param tile unused
24 * @param flags type of operation
25 * @param p1 owner of the plan
26 * @param p2 unused
27 * @param text unused
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) {
34 Owner o = (Owner)p1;
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);
42 return CommandCost();
45 /**
46 * Create a new line in a plan.
47 * @param tile unused
48 * @param flags type of operation
49 * @param p1 plan id
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);
69 return CommandCost();
72 /**
73 * Edit the visibility of a plan.
74 * @param tile unused
75 * @param flags type of operation
76 * @param p1 plan id
77 * @param p2 visibility
78 * @param text unused
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);
89 return CommandCost();
92 /**
93 * Delete a plan.
94 * @param tile unused
95 * @param flags type of operation
96 * @param p1 plan id
97 * @param p2 unused
98 * @param text unused
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;
111 delete p;
113 return CommandCost();
117 * Remove a line from a plan.
118 * @param tile unused
119 * @param flags type of operation
120 * @param p1 plan id
121 * @param p2 line id
122 * @param text unused
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);
132 delete *it;
133 p->lines.erase(it);
134 if (p->IsListable()) {
135 Window *w = FindWindowById(WC_PLANS, 0);
136 if (w) w->InvalidateData(p->index, false);
139 return CommandCost();