OpenTTD Source: src/goal.cpp Source File

OpenTTD Source 13.1

goal.cpp
Go to the documentation of this file.
1 /*
2 * This file is part of OpenTTD.
3 * 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.
4 * 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.
5 * 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/>.
6 */
7
10 #include "stdafx.h"
11 #include "company_func.h"
12 #include "industry.h"
13 #include "town.h"
14 #include "window_func.h"
15 #include "goal_base.h"
16 #include "core/pool_func.hpp"
17 #include "game/game.hpp"
18 #include "command_func.h"
19 #include "company_base.h"
20 #include "story_base.h"
21 #include "string_func.h"
22 #include "gui.h"
23 #include "network/network.h"
24 #include "network/network_base.h"
25 #include "network/network_func.h"
26 #include "goal_cmd.h"
27
28 #include "safeguards.h"
29
30
31 GoalPool _goal_pool("Goal");
32 INSTANTIATE_POOL_METHODS(Goal)
33
34
43 std::tuple<CommandCost, GoalID> CmdCreateGoal(DoCommandFlag flags, CompanyID company, GoalType type, GoalTypeID dest, const std::string &text)
44 {
45 if (!Goal::CanAllocateItem()) return { CMD_ERROR, INVALID_GOAL };
46
47 if (_current_company != OWNER_DEITY) return { CMD_ERROR, INVALID_GOAL };
48 if (text.empty()) return { CMD_ERROR, INVALID_GOAL };
49 if (company != INVALID_COMPANY && !Company::IsValidID(company)) return { CMD_ERROR, INVALID_GOAL };
50
51 switch (type) {
52 case GT_NONE:
53 if (dest != 0) return { CMD_ERROR, INVALID_GOAL };
54 break;
55
56 case GT_TILE:
57 if (!IsValidTile(dest)) return { CMD_ERROR, INVALID_GOAL };
58 break;
59
60 case GT_INDUSTRY:
61 if (!Industry::IsValidID(dest)) return { CMD_ERROR, INVALID_GOAL };
62 break;
63
64 case GT_TOWN:
65 if (!Town::IsValidID(dest)) return { CMD_ERROR, INVALID_GOAL };
66 break;
67
68 case GT_COMPANY:
69 if (!Company::IsValidID(dest)) return { CMD_ERROR, INVALID_GOAL };
70 break;
71
72 case GT_STORY_PAGE: {
73 if (!StoryPage::IsValidID(dest)) return { CMD_ERROR, INVALID_GOAL };
74 CompanyID story_company = StoryPage::Get(dest)->company;
75 if (company == INVALID_COMPANY ? story_company != INVALID_COMPANY : story_company != INVALID_COMPANY && story_company != company) return { CMD_ERROR, INVALID_GOAL };
76 break;
77 }
78
79 default: return { CMD_ERROR, INVALID_GOAL };
80 }
81
82 if (flags & DC_EXEC) {
83 Goal *g = new Goal();
84 g->type = type;
85 g->dst = dest;
86 g->company = company;
87 g->text = stredup(text.c_str());
88 g->progress = nullptr;
89 g->completed = false;
90
91 if (g->company == INVALID_COMPANY) {
92 InvalidateWindowClassesData(WC_GOALS_LIST);
93 } else {
94 InvalidateWindowData(WC_GOALS_LIST, g->company);
95 }
96 if (Goal::GetNumItems() == 1) InvalidateWindowData(WC_MAIN_TOOLBAR, 0);
97
98 return { CommandCost(), g->index };
99 }
100
101 return { CommandCost(), INVALID_GOAL };
102 }
103
110 CommandCost CmdRemoveGoal(DoCommandFlag flags, GoalID goal)
111 {
112 if (_current_company != OWNER_DEITY) return CMD_ERROR;
113 if (!Goal::IsValidID(goal)) return CMD_ERROR;
114
115 if (flags & DC_EXEC) {
116 Goal *g = Goal::Get(goal);
117 CompanyID c = g->company;
118 delete g;
119
120 if (c == INVALID_COMPANY) {
121 InvalidateWindowClassesData(WC_GOALS_LIST);
122 } else {
123 InvalidateWindowData(WC_GOALS_LIST, c);
124 }
125 if (Goal::GetNumItems() == 0) InvalidateWindowData(WC_MAIN_TOOLBAR, 0);
126 }
127
128 return CommandCost();
129 }
130
138 CommandCost CmdSetGoalText(DoCommandFlag flags, GoalID goal, const std::string &text)
139 {
140 if (_current_company != OWNER_DEITY) return CMD_ERROR;
141 if (!Goal::IsValidID(goal)) return CMD_ERROR;
142 if (text.empty()) return CMD_ERROR;
143
144 if (flags & DC_EXEC) {
145 Goal *g = Goal::Get(goal);
146 free(g->text);
147 g->text = stredup(text.c_str());
148
149 if (g->company == INVALID_COMPANY) {
150 InvalidateWindowClassesData(WC_GOALS_LIST);
151 } else {
152 InvalidateWindowData(WC_GOALS_LIST, g->company);
153 }
154 }
155
156 return CommandCost();
157 }
158
166 CommandCost CmdSetGoalProgress(DoCommandFlag flags, GoalID goal, const std::string &text)
167 {
168 if (_current_company != OWNER_DEITY) return CMD_ERROR;
169 if (!Goal::IsValidID(goal)) return CMD_ERROR;
170
171 if (flags & DC_EXEC) {
172 Goal *g = Goal::Get(goal);
173 free(g->progress);
174 if (text.empty()) {
175 g->progress = nullptr;
176 } else {
177 g->progress = stredup(text.c_str());
178 }
179
180 if (g->company == INVALID_COMPANY) {
181 InvalidateWindowClassesData(WC_GOALS_LIST);
182 } else {
183 InvalidateWindowData(WC_GOALS_LIST, g->company);
184 }
185 }
186
187 return CommandCost();
188 }
189
197 CommandCost CmdSetGoalCompleted(DoCommandFlag flags, GoalID goal, bool completed)
198 {
199 if (_current_company != OWNER_DEITY) return CMD_ERROR;
200 if (!Goal::IsValidID(goal)) return CMD_ERROR;
201
202 if (flags & DC_EXEC) {
203 Goal *g = Goal::Get(goal);
204 g->completed = completed;
205
206 if (g->company == INVALID_COMPANY) {
207 InvalidateWindowClassesData(WC_GOALS_LIST);
208 } else {
209 InvalidateWindowData(WC_GOALS_LIST, g->company);
210 }
211 }
212
213 return CommandCost();
214 }
215
227 CommandCost CmdGoalQuestion(DoCommandFlag flags, uint16 uniqueid, uint16 target, bool is_client, uint32 button_mask, GoalQuestionType type, const std::string &text)
228 {
229 CompanyID company = (CompanyID)target;
230 ClientID client = (ClientID)target;
231
232 static_assert(GOAL_QUESTION_BUTTON_COUNT < 29);
233 button_mask &= (1U << GOAL_QUESTION_BUTTON_COUNT) - 1;
234
235 if (_current_company != OWNER_DEITY) return CMD_ERROR;
236 if (text.empty()) return CMD_ERROR;
237 if (is_client) {
238 /* Only check during pre-flight; the client might have left between
239 * testing and executing. In that case it is fine to just ignore the
240 * fact the client is no longer here. */
241 if (!(flags & DC_EXEC) && _network_server && NetworkClientInfo::GetByClientID(client) == nullptr) return CMD_ERROR;
242 } else {
243 if (company != INVALID_COMPANY && !Company::IsValidID(company)) return CMD_ERROR;
244 }
245 uint min_buttons = (type == GQT_QUESTION ? 1 : 0);
246 if (CountBits(button_mask) < min_buttons || CountBits(button_mask) > 3) return CMD_ERROR;
247 if (type >= GQT_END) return CMD_ERROR;
248
249 if (flags & DC_EXEC) {
250 if (is_client) {
251 if (client != _network_own_client_id) return CommandCost();
252 } else {
253 if (company == INVALID_COMPANY && !Company::IsValidID(_local_company)) return CommandCost();
254 if (company != INVALID_COMPANY && company != _local_company) return CommandCost();
255 }
256 ShowGoalQuestion(uniqueid, type, button_mask, text.c_str());
257 }
258
259 return CommandCost();
260 }
261
269 CommandCost CmdGoalQuestionAnswer(DoCommandFlag flags, uint16 uniqueid, uint8 button)
270 {
271 if (button >= GOAL_QUESTION_BUTTON_COUNT) return CMD_ERROR;
272
273 if (_current_company == OWNER_DEITY) {
274 /* It has been requested to close this specific question on all clients */
275 if (flags & DC_EXEC) CloseWindowById(WC_GOAL_QUESTION, uniqueid);
276 return CommandCost();
277 }
278
279 if (_networking && _local_company == _current_company) {
280 /* Somebody in the same company answered the question. Close the window */
281 if (flags & DC_EXEC) CloseWindowById(WC_GOAL_QUESTION, uniqueid);
282 if (!_network_server) return CommandCost();
283 }
284
285 if (flags & DC_EXEC) {
286 Game::NewEvent(new ScriptEventGoalQuestionAnswer(uniqueid, (ScriptCompany::CompanyID)(byte)_current_company, (ScriptGoal::QuestionButton)(1 << button)));
287 }
288
289 return CommandCost();
290 }
game.hpp
InvalidateWindowData
void InvalidateWindowData(WindowClass cls, WindowNumber number, int data, bool gui_scope)
Mark window data of the window of a given class and specific window number as invalid (in need of re-...
Definition: window.cpp:3252
Goal
Struct about goals, current and completed.
Definition: goal_base.h:21
Pool::PoolItem<&_story_page_pool >::Get
static Titem * Get(size_t index)
Returns Titem with given index.
Definition: pool_type.hpp:337
command_func.h
CMD_ERROR
static const CommandCost CMD_ERROR
Define a default return value for a failed command.
Definition: command_func.h:28
GT_INDUSTRY
@ GT_INDUSTRY
Destination is an industry.
Definition: goal_type.h:29
company_base.h
_network_server
bool _network_server
network-server is active
Definition: network.cpp:59
Pool::PoolItem::index
Tindex index
Index of this pool item.
Definition: pool_type.hpp:235
GT_TILE
@ GT_TILE
Destination is a tile.
Definition: goal_type.h:28
goal_base.h
town.h
Goal::dst
GoalTypeID dst
Index of type.
Definition: goal_base.h:24
WC_GOALS_LIST
@ WC_GOALS_LIST
Goals list; Window numbers:
Definition: window_type.h:283
Owner
Owner
Enum for all companies/owners.
Definition: company_type.h:18
DC_EXEC
@ DC_EXEC
execute the given command
Definition: command_type.h:357
DoCommandFlag
DoCommandFlag
List of flags for a command.
Definition: command_type.h:355
CmdRemoveGoal
CommandCost CmdRemoveGoal(DoCommandFlag flags, GoalID goal)
Remove a goal.
Definition: goal.cpp:110
network_base.h
CountBits
static uint CountBits(T value)
Counts the number of set bits in a variable.
Definition: bitmath_func.hpp:251
GT_TOWN
@ GT_TOWN
Destination is a town.
Definition: goal_type.h:30
NetworkClientInfo::GetByClientID
static NetworkClientInfo * GetByClientID(ClientID client_id)
Return the CI given it's client-identifier.
Definition: network.cpp:114
CommandCost
Common return value for all commands.
Definition: command_type.h:24
ClientID
ClientID
'Unique' identifier to be given to clients
Definition: network_type.h:47
Game::NewEvent
static void NewEvent(class ScriptEvent *event)
Queue a new event for a Game Script.
Definition: game_core.cpp:146
_local_company
CompanyID _local_company
Company controlled by the human player at this client. Can also be COMPANY_SPECTATOR.
Definition: company_cmd.cpp:46
industry.h
safeguards.h
IsValidTile
static bool IsValidTile(TileIndex tile)
Checks if a tile is valid.
Definition: tile_map.h:161
_networking
bool _networking
are we in networking mode?
Definition: network.cpp:58
CmdSetGoalText
CommandCost CmdSetGoalText(DoCommandFlag flags, GoalID goal, const std::string &text)
Update goal text of a goal.
Definition: goal.cpp:138
stdafx.h
GT_NONE
@ GT_NONE
Destination is not linked.
Definition: goal_type.h:27
_network_own_client_id
ClientID _network_own_client_id
Our client identifier.
Definition: network.cpp:64
CmdSetGoalCompleted
CommandCost CmdSetGoalCompleted(DoCommandFlag flags, GoalID goal, bool completed)
Update completed state of a goal.
Definition: goal.cpp:197
Goal::progress
char * progress
Progress text of the goal.
Definition: goal_base.h:26
CmdGoalQuestion
CommandCost CmdGoalQuestion(DoCommandFlag flags, uint16 uniqueid, uint16 target, bool is_client, uint32 button_mask, GoalQuestionType type, const std::string &text)
Ask a goal related question.
Definition: goal.cpp:227
string_func.h
_current_company
CompanyID _current_company
Company currently doing an action.
Definition: company_cmd.cpp:47
Pool
Base class for all pools.
Definition: pool_type.hpp:81
GOAL_QUESTION_BUTTON_COUNT
static const uint32 GOAL_QUESTION_BUTTON_COUNT
Amount of buttons available.
Definition: goal_type.h:15
Pool::PoolItem<&_goal_pool >::GetNumItems
static size_t GetNumItems()
Returns number of valid items in the pool.
Definition: pool_type.hpp:367
GT_COMPANY
@ GT_COMPANY
Destination is a company.
Definition: goal_type.h:31
Goal::completed
bool completed
Is the goal completed or not?
Definition: goal_base.h:27
InvalidateWindowClassesData
void InvalidateWindowClassesData(WindowClass cls, int data, bool gui_scope)
Mark window data of all windows of a given class as invalid (in need of re-computing) Note that by de...
Definition: window.cpp:3269
Goal::company
CompanyID company
Goal is for a specific company; INVALID_COMPANY if it is global.
Definition: goal_base.h:22
GoalTypeID
uint32 GoalTypeID
Contains either tile, industry ID, town ID or company ID (or INVALID_GOALTYPE)
Definition: goal_type.h:35
INVALID_GOAL
static const GoalID INVALID_GOAL
Constant representing a non-existing goal.
Definition: goal_type.h:40
WC_GOAL_QUESTION
@ WC_GOAL_QUESTION
Popup with a set of buttons, designed to ask the user a question from a GameScript.
Definition: window_type.h:130
goal_cmd.h
Pool::PoolItem<&_goal_pool >::CanAllocateItem
static bool CanAllocateItem(size_t n=1)
Helper functions so we can use PoolItem::Function() instead of _poolitem_pool.Function()
Definition: pool_type.hpp:307
GT_STORY_PAGE
@ GT_STORY_PAGE
Destination is a story page.
Definition: goal_type.h:32
CmdGoalQuestionAnswer
CommandCost CmdGoalQuestionAnswer(DoCommandFlag flags, uint16 uniqueid, uint8 button)
Reply to a goal question.
Definition: goal.cpp:269
OWNER_DEITY
@ OWNER_DEITY
The object is owned by a superuser / goal script.
Definition: company_type.h:27
company_func.h
INSTANTIATE_POOL_METHODS
#define INSTANTIATE_POOL_METHODS(name)
Force instantiation of pool methods so we don't get linker errors.
Definition: pool_func.hpp:224
stredup
char * stredup(const char *s, const char *last)
Create a duplicate of the given string.
Definition: string.cpp:138
network.h
window_func.h
CmdSetGoalProgress
CommandCost CmdSetGoalProgress(DoCommandFlag flags, GoalID goal, const std::string &text)
Update progress text of a goal.
Definition: goal.cpp:166
CloseWindowById
void CloseWindowById(WindowClass cls, WindowNumber number, bool force)
Close a window by its class and window number (if it is open).
Definition: window.cpp:1187
INVALID_COMPANY
@ INVALID_COMPANY
An invalid company.
Definition: company_type.h:30
CmdCreateGoal
std::tuple< CommandCost, GoalID > CmdCreateGoal(DoCommandFlag flags, CompanyID company, GoalType type, GoalTypeID dest, const std::string &text)
Create a new goal.
Definition: goal.cpp:43
gui.h
Pool::PoolItem<&_company_pool >::IsValidID
static bool IsValidID(size_t index)
Tests whether given index can be used to get valid (non-nullptr) Titem.
Definition: pool_type.hpp:326
GoalType
GoalType
Types of goal destinations.
Definition: goal_type.h:26
pool_func.hpp
free
static void free(const void *ptr)
Version of the standard free that accepts const pointers.
Definition: stdafx.h:470
story_base.h
GoalID
uint16 GoalID
ID of a goal.
Definition: goal_type.h:38
WC_MAIN_TOOLBAR
@ WC_MAIN_TOOLBAR
Main toolbar (the long bar at the top); Window numbers:
Definition: window_type.h:51
Goal::type
GoalType type
Type of the goal.
Definition: goal_base.h:23
network_func.h
Goal::text
char * text
Text of the goal.
Definition: goal_base.h:25
ShowGoalQuestion
void ShowGoalQuestion(uint16 id, byte type, uint32 button_mask, const char *question)
Display a goal question.
Definition: goal_gui.cpp:558