OpenTTD Source 20260621-master-g720d10536d
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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
9
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 "goal_gui.h"
23#include "network/network.h"
26#include "goal_cmd.h"
27
28#include "safeguards.h"
29
30
31GoalPool _goal_pool("Goal");
33
34/* static */ bool Goal::IsValidGoalDestination(CompanyID company, GoalType type, GoalTypeID dest)
35{
36 switch (type) {
37 case GoalType::None:
38 if (dest != 0) return false;
39 break;
40
41 case GoalType::Tile:
42 if (!IsValidTile(dest)) return false;
43 break;
44
46 if (!Industry::IsValidID(dest)) return false;
47 break;
48
49 case GoalType::Town:
50 if (!Town::IsValidID(dest)) return false;
51 break;
52
54 if (!Company::IsValidID(dest)) return false;
55 break;
56
58 if (!StoryPage::IsValidID(dest)) return false;
59 CompanyID story_company = StoryPage::Get(dest)->company;
60 if (company == CompanyID::Invalid() ? story_company != CompanyID::Invalid() : story_company != CompanyID::Invalid() && story_company != company) return false;
61 break;
62 }
63
64 default: return false;
65 }
66 return true;
67}
68
78std::tuple<CommandCost, GoalID> CmdCreateGoal(DoCommandFlags flags, CompanyID company, GoalType type, GoalTypeID dest, const EncodedString &text)
79{
80 if (!Goal::CanAllocateItem()) return { CMD_ERROR, GoalID::Invalid() };
81
82 if (_current_company != OWNER_DEITY) return { CMD_ERROR, GoalID::Invalid() };
83 if (text.empty()) return { CMD_ERROR, GoalID::Invalid() };
84 if (company != CompanyID::Invalid() && !Company::IsValidID(company)) return { CMD_ERROR, GoalID::Invalid() };
85 if (!Goal::IsValidGoalDestination(company, type, dest)) return { CMD_ERROR, GoalID::Invalid() };
86
87 if (flags.Test(DoCommandFlag::Execute)) {
88 Goal *g = Goal::Create(type, dest, company, text);
89
90 if (g->company == CompanyID::Invalid()) {
91 InvalidateWindowClassesData(WindowClass::GoalList);
92 } else {
93 InvalidateWindowData(WindowClass::GoalList, g->company);
94 }
95 if (Goal::GetNumItems() == 1) InvalidateWindowData(WindowClass::MainToolbar, 0);
96
97 return { CommandCost(), g->index };
98 }
99
100 return { CommandCost(), GoalID::Invalid() };
101}
102
110{
112 if (!Goal::IsValidID(goal)) return CMD_ERROR;
113
114 if (flags.Test(DoCommandFlag::Execute)) {
115 Goal *g = Goal::Get(goal);
116 CompanyID c = g->company;
117 delete g;
118
119 if (c == CompanyID::Invalid()) {
120 InvalidateWindowClassesData(WindowClass::GoalList);
121 } else {
122 InvalidateWindowData(WindowClass::GoalList, c);
123 }
124 if (Goal::GetNumItems() == 0) InvalidateWindowData(WindowClass::MainToolbar, 0);
125 }
126
127 return CommandCost();
128}
129
139{
141 if (!Goal::IsValidID(goal)) return CMD_ERROR;
142 Goal *g = Goal::Get(goal);
143 if (!Goal::IsValidGoalDestination(g->company, type, dest)) return CMD_ERROR;
144
145 if (flags.Test(DoCommandFlag::Execute)) {
146 g->type = type;
147 g->dst = dest;
148 }
149
150 return CommandCost();
151}
152
161{
163 if (!Goal::IsValidID(goal)) return CMD_ERROR;
164 if (text.empty()) return CMD_ERROR;
165
166 if (flags.Test(DoCommandFlag::Execute)) {
167 Goal *g = Goal::Get(goal);
168 g->text = text;
169
170 if (g->company == CompanyID::Invalid()) {
171 InvalidateWindowClassesData(WindowClass::GoalList);
172 } else {
173 InvalidateWindowData(WindowClass::GoalList, g->company);
174 }
175 }
176
177 return CommandCost();
178}
179
188{
190 if (!Goal::IsValidID(goal)) return CMD_ERROR;
191
192 if (flags.Test(DoCommandFlag::Execute)) {
193 Goal *g = Goal::Get(goal);
194 g->progress = text;
195
196 if (g->company == CompanyID::Invalid()) {
197 InvalidateWindowClassesData(WindowClass::GoalList);
198 } else {
199 InvalidateWindowData(WindowClass::GoalList, g->company);
200 }
201 }
202
203 return CommandCost();
204}
205
214{
216 if (!Goal::IsValidID(goal)) return CMD_ERROR;
217
218 if (flags.Test(DoCommandFlag::Execute)) {
219 Goal *g = Goal::Get(goal);
220 g->completed = completed;
221
222 if (g->company == CompanyID::Invalid()) {
223 InvalidateWindowClassesData(WindowClass::GoalList);
224 } else {
225 InvalidateWindowData(WindowClass::GoalList, g->company);
226 }
227 }
228
229 return CommandCost();
230}
231
243CommandCost CmdGoalQuestion(DoCommandFlags flags, uint16_t uniqueid, uint32_t target, bool is_client, GoalQuestionButtons buttons, GoalQuestionType type, const EncodedString &text)
244{
245 static_assert(sizeof(uint32_t) >= sizeof(CompanyID));
246 CompanyID company = (CompanyID)target;
247 static_assert(sizeof(uint32_t) >= sizeof(ClientID));
248 ClientID client = (ClientID)target;
249
251 if (text.empty()) return CMD_ERROR;
252 if (is_client) {
253 /* Only check during pre-flight; the client might have left between
254 * testing and executing. In that case it is fine to just ignore the
255 * fact the client is no longer here. */
256 if (!flags.Test(DoCommandFlag::Execute) && _network_server && NetworkClientInfo::GetByClientID(client) == nullptr) return CMD_ERROR;
257 } else {
258 if (company != CompanyID::Invalid() && !Company::IsValidID(company)) return CMD_ERROR;
259 }
260 uint min_buttons = (type == GoalQuestionType::Question ? 1 : 0);
261 if (!buttons.IsValid()) return CMD_ERROR;
262 if (buttons.Count() < min_buttons || buttons.Count() > 3) return CMD_ERROR;
263 if (type >= GoalQuestionType::End) return CMD_ERROR;
264
265 if (flags.Test(DoCommandFlag::Execute)) {
266 if (is_client) {
267 if (client != _network_own_client_id) return CommandCost();
268 } else {
269 if (company == CompanyID::Invalid() && !Company::IsValidID(_local_company)) return CommandCost();
270 if (company != CompanyID::Invalid() && company != _local_company) return CommandCost();
271 }
272 ShowGoalQuestion(uniqueid, type, buttons, text);
273 }
274
275 return CommandCost();
276}
277
286{
287 if (button >= GoalQuestionButton::End) return CMD_ERROR;
288
290 /* It has been requested to close this specific question on all clients */
291 if (flags.Test(DoCommandFlag::Execute)) CloseWindowById(WindowClass::GoalQuestion, uniqueid);
292 return CommandCost();
293 }
294
296 /* Somebody in the same company answered the question. Close the window */
297 if (flags.Test(DoCommandFlag::Execute)) CloseWindowById(WindowClass::GoalQuestion, uniqueid);
298 if (!_network_server) return CommandCost();
299 }
300
301 if (flags.Test(DoCommandFlag::Execute)) {
302 Game::NewEvent(new ScriptEventGoalQuestionAnswer(uniqueid, _current_company, static_cast<ScriptGoal::QuestionButton>(GoalQuestionButtons{button}.base())));
303 }
304
305 return CommandCost();
306}
uint Count() const
Count the number of set bits.
constexpr bool Test(Tvalue_type value) const
Test if the value-th bit is set.
constexpr bool IsValid() const
Test that the raw value of this bit set is valid.
Common return value for all commands.
Container for an encoded string, created by GetEncodedString.
static void NewEvent(class ScriptEvent *event)
Queue a new event for the game script.
Functions related to commands.
static const CommandCost CMD_ERROR
Define a default return value for a failed command.
@ Execute
execute the given command
EnumBitSet< DoCommandFlag, uint16_t > DoCommandFlags
Bitset of DoCommandFlag elements.
Definition of stuff that is very close to a company, like the company struct itself.
CompanyID _local_company
Company controlled by the human player at this client. Can also be COMPANY_SPECTATOR.
CompanyID _current_company
Company currently doing an action.
Functions related to companies.
static constexpr Owner OWNER_DEITY
The object is owned by a superuser / goal script.
Base functions for all Games.
CommandCost CmdGoalQuestionAnswer(DoCommandFlags flags, uint16_t uniqueid, GoalQuestionButton button)
Reply to a goal question.
Definition goal.cpp:285
CommandCost CmdGoalQuestion(DoCommandFlags flags, uint16_t uniqueid, uint32_t target, bool is_client, GoalQuestionButtons buttons, GoalQuestionType type, const EncodedString &text)
Ask a goal related question.
Definition goal.cpp:243
CommandCost CmdSetGoalProgress(DoCommandFlags flags, GoalID goal, const EncodedString &text)
Update progress text of a goal.
Definition goal.cpp:187
CommandCost CmdRemoveGoal(DoCommandFlags flags, GoalID goal)
Remove a goal.
Definition goal.cpp:109
std::tuple< CommandCost, GoalID > CmdCreateGoal(DoCommandFlags flags, CompanyID company, GoalType type, GoalTypeID dest, const EncodedString &text)
Create a new goal.
Definition goal.cpp:78
CommandCost CmdSetGoalText(DoCommandFlags flags, GoalID goal, const EncodedString &text)
Update goal text of a goal.
Definition goal.cpp:160
CommandCost CmdSetGoalDestination(DoCommandFlags flags, GoalID goal, GoalType type, GoalTypeID dest)
Update goal destination of a goal.
Definition goal.cpp:138
CommandCost CmdSetGoalCompleted(DoCommandFlags flags, GoalID goal, bool completed)
Update completed state of a goal.
Definition goal.cpp:213
Goal base class.
Command definitions related to goals.
void ShowGoalQuestion(uint16_t id, GoalQuestionType type, GoalQuestionButtons buttons, const EncodedString &question)
Display a goal question.
Definition goal_gui.cpp:462
Goal GUI functions.
GoalType
Types of goal destinations.
Definition goal_type.h:51
@ Company
Destination is a company.
Definition goal_type.h:56
@ Industry
Destination is an industry.
Definition goal_type.h:54
@ None
Destination is not linked.
Definition goal_type.h:52
@ StoryPage
Destination is a story page.
Definition goal_type.h:57
@ Town
Destination is a town.
Definition goal_type.h:55
@ Tile
Destination is a tile.
Definition goal_type.h:53
uint32_t GoalTypeID
Contains either tile, industry ID, town ID, company ID, or story page ID.
Definition goal_type.h:60
GoalQuestionType
Types of goal questions.
Definition goal_type.h:16
@ End
End marker.
Definition goal_type.h:21
@ Question
Asking a simple question; title: Question.
Definition goal_type.h:17
EnumBitSet< GoalQuestionButton, uint32_t, GoalQuestionButton::End > GoalQuestionButtons
Bitset of GoalQuestionButton elements.
Definition goal_type.h:48
GoalQuestionButton
Types of buttons that can be in the question window.
Definition goal_type.h:25
@ End
End marker.
Definition goal_type.h:44
PoolID< uint16_t, struct GoalIDTag, 64000, 0xFFFF > GoalID
ID of a goal.
Definition goal_type.h:63
Base of all industries.
bool _networking
are we in networking mode?
Definition network.cpp:67
bool _network_server
network-server is active
Definition network.cpp:68
ClientID _network_own_client_id
Our client identifier.
Definition network.cpp:72
Basic functions/variables used all over the place.
Base core network types and some helper functions to access them.
Network functions used by other parts of OpenTTD.
ClientID
'Unique' identifier to be given to clients
Some methods of Pool are placed here in order to reduce compilation time and binary size.
#define INSTANTIATE_POOL_METHODS(name)
Force instantiation of pool methods so we don't get linker errors.
A number of safeguards to prevent using unsafe methods.
Definition of base types and functions in a cross-platform compatible way.
StoryPage base class.
Functions related to low-level strings.
Struct about goals, current and completed.
Definition goal_base.h:22
EncodedString progress
Progress text of the goal.
Definition goal_base.h:27
EncodedString text
Text of the goal.
Definition goal_base.h:26
bool completed
Is the goal completed or not?
Definition goal_base.h:28
GoalType type
Type of the goal.
Definition goal_base.h:24
GoalTypeID dst
Index of type.
Definition goal_base.h:25
CompanyID company
Goal is for a specific company; CompanyID::Invalid() if it is global.
Definition goal_base.h:23
static NetworkClientInfo * GetByClientID(ClientID client_id)
Return the CI given it's client-identifier.
Definition network.cpp:118
static StoryPage * Get(auto index)
static bool CanAllocateItem(size_t n=1)
static T * Create(Targs &&... args)
CompanyID company
StoryPage is for a specific company; CompanyID::Invalid() if it is global.
Definition story_base.h:165
bool IsValidTile(Tile tile)
Checks if a tile is valid.
Definition tile_map.h:161
Base of the town class.
void CloseWindowById(WindowClass cls, WindowNumber number, bool force, int data)
Close a window by its class and window number (if it is open).
Definition window.cpp:1201
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:3315
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:3333
Window functions not directly related to making/drawing windows.