OpenTTD Source 20261112-master-gbcbec5e63f
story.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 "story_base.h"
12#include "core/pool_func.hpp"
13#include "command_func.h"
14#include "company_base.h"
15#include "company_func.h"
17#include "tile_map.h"
18#include "goal_type.h"
19#include "goal_base.h"
20#include "window_func.h"
21#include "gui.h"
22#include "vehicle_base.h"
23#include "game/game.hpp"
24#include "script/api/script_event_types.hpp"
25#include "story_cmd.h"
26
27#include "safeguards.h"
28
29
30uint32_t _story_page_element_next_sort_value;
31uint32_t _story_page_next_sort_value;
32
33StoryPageElementPool _story_page_element_pool("StoryPageElement");
34StoryPagePool _story_page_pool("StoryPage");
37
38
39StoryPage::~StoryPage()
40{
41 if (CleaningPool()) return;
42
44 if (spe->page == this->index) delete spe;
45 }
46}
47
58static bool VerifyElementContentParameters(StoryPageID page_id, StoryPageElementType type, TileIndex tile, uint32_t reference, const EncodedString &text)
59{
60 StoryPageButtonData button_data{ reference };
61
62 switch (type) {
64 if (text.empty()) return false;
65 break;
67 if (text.empty()) return false;
68 if (!IsValidTile(tile)) return false;
69 break;
71 if (!Goal::IsValidID((GoalID)reference)) return false;
72 /* Reject company specific goals on global pages */
73 if (StoryPage::Get(page_id)->company == CompanyID::Invalid() && Goal::Get((GoalID)reference)->company != CompanyID::Invalid()) return false;
74 break;
76 if (!button_data.ValidateColour()) return false;
77 if (!button_data.ValidateFlags()) return false;
78 return true;
80 if (!button_data.ValidateColour()) return false;
81 if (!button_data.ValidateFlags()) return false;
82 if (!button_data.ValidateCursor()) return false;
83 return true;
85 if (!button_data.ValidateColour()) return false;
86 if (!button_data.ValidateFlags()) return false;
87 if (!button_data.ValidateCursor()) return false;
88 if (!button_data.ValidateVehicleType()) return false;
89 return true;
90 default:
91 return false;
92 }
93
94 return true;
95}
96
105static void UpdateElement(StoryPageElement &pe, TileIndex tile, uint32_t reference, const EncodedString &text)
106{
107 switch (pe.type) {
109 pe.text = text;
110 break;
112 pe.text = text;
113 pe.referenced_id = tile.base();
114 break;
116 pe.referenced_id = reference;
117 break;
121 pe.text = text;
122 pe.referenced_id = reference;
123 break;
124 default: NOT_REACHED();
125 }
126}
127
133{
134 assert(button_colour < Colours::End);
135 SB(this->referenced_id, 0, 8, to_underlying(button_colour));
136}
137
143{
144 SB(this->referenced_id, 24, 8, flags.base());
145}
146
152{
153 assert(cursor < StoryPageButtonCursor::End);
154 SB(this->referenced_id, 8, 8, to_underlying(cursor));
155}
156
162{
163 assert(vehtype == VehicleType::Invalid || vehtype < VehicleType::CompanyEnd);
164 SB(this->referenced_id, 16, 8, to_underlying(vehtype));
165}
166
172{
173 Colours colour = static_cast<Colours>(GB(this->referenced_id, 0, 8));
174 if (!IsValidColours(colour)) return Colours::Invalid;
175 return colour;
176}
177
183{
184 return static_cast<StoryPageButtonFlags>(GB(this->referenced_id, 24, 8));
185}
186
192{
193 StoryPageButtonCursor cursor = (StoryPageButtonCursor)GB(this->referenced_id, 8, 8);
195 return cursor;
196}
197
203{
204 return (VehicleType)GB(this->referenced_id, 16, 8);
205}
206
212{
213 return GB(this->referenced_id, 0, 8) < to_underlying(Colours::End);
214}
215
222{
223 StoryPageButtonFlags flags = static_cast<StoryPageButtonFlags>(GB(this->referenced_id, 24, 8));
224 /* Don't allow float left and right together */
225 if (flags.All({StoryPageButtonFlag::FloatLeft, StoryPageButtonFlag::FloatRight})) return false;
226 /* Don't allow undefined flags */
227 if (flags.Reset({StoryPageButtonFlag::FloatLeft, StoryPageButtonFlag::FloatRight}).Any()) return false;
228 return true;
229}
230
236{
237 return static_cast<StoryPageButtonCursor>(GB(this->referenced_id, 8, 8)) < StoryPageButtonCursor::End;
238}
239
245{
246 VehicleType vehtype = static_cast<VehicleType>(GB(this->referenced_id, 16, 8));
247 return vehtype == VehicleType::Invalid || vehtype < VehicleType::CompanyEnd;
248}
249
257std::tuple<CommandCost, StoryPageID> CmdCreateStoryPage(DoCommandFlags flags, CompanyID company, const EncodedString &text)
258{
259 if (!StoryPage::CanAllocateItem()) return { CMD_ERROR, StoryPageID::Invalid() };
260
261 if (_current_company != OWNER_DEITY) return { CMD_ERROR, StoryPageID::Invalid() };
262 if (company != CompanyID::Invalid() && !Company::IsValidID(company)) return { CMD_ERROR, StoryPageID::Invalid() };
263
264 if (flags.Test(DoCommandFlag::Execute)) {
265 if (StoryPage::GetNumItems() == 0) {
266 /* Initialize the next sort value variable. */
267 _story_page_next_sort_value = 0;
268 }
269
270 StoryPage *s = StoryPage::Create(_story_page_next_sort_value, TimerGameCalendar::date, company, text);
271
272 InvalidateWindowClassesData(WindowClass::StoryBook, -1);
273 if (StoryPage::GetNumItems() == 1) InvalidateWindowData(WindowClass::MainToolbar, 0);
274
275 _story_page_next_sort_value++;
276 return { CommandCost(), s->index };
277 }
278
279 return { CommandCost(), StoryPageID::Invalid() };
280}
281
292std::tuple<CommandCost, StoryPageElementID> CmdCreateStoryPageElement(DoCommandFlags flags, TileIndex tile, StoryPageID page_id, StoryPageElementType type, uint32_t reference, const EncodedString &text)
293{
294 if (!StoryPageElement::CanAllocateItem()) return { CMD_ERROR, StoryPageElementID::Invalid() };
295
296 /* Allow at most 128 elements per page. */
297 uint16_t element_count = 0;
299 if (iter->page == page_id) element_count++;
300 }
301 if (element_count >= 128) return { CMD_ERROR, StoryPageElementID::Invalid() };
302
303 if (_current_company != OWNER_DEITY) return { CMD_ERROR, StoryPageElementID::Invalid() };
304 if (!StoryPage::IsValidID(page_id)) return { CMD_ERROR, StoryPageElementID::Invalid() };
305 if (!VerifyElementContentParameters(page_id, type, tile, reference, text)) return { CMD_ERROR, StoryPageElementID::Invalid() };
306
307
308 if (flags.Test(DoCommandFlag::Execute)) {
310 /* Initialize the next sort value variable. */
311 _story_page_element_next_sort_value = 0;
312 }
313
314 StoryPageElement *pe = StoryPageElement::Create(_story_page_element_next_sort_value, type, page_id);
315 UpdateElement(*pe, tile, reference, text);
316
317 InvalidateWindowClassesData(WindowClass::StoryBook, page_id);
318
319 _story_page_element_next_sort_value++;
320 return { CommandCost(), pe->index };
321 }
322
323 return { CommandCost(), StoryPageElementID::Invalid() };
324}
325
335CommandCost CmdUpdateStoryPageElement(DoCommandFlags flags, TileIndex tile, StoryPageElementID page_element_id, uint32_t reference, const EncodedString &text)
336{
338 if (!StoryPageElement::IsValidID(page_element_id)) return CMD_ERROR;
339
340 StoryPageElement *pe = StoryPageElement::Get(page_element_id);
341 StoryPageID page_id = pe->page;
342 StoryPageElementType type = pe->type;
343
344 if (!VerifyElementContentParameters(page_id, type, tile, reference, text)) return CMD_ERROR;
345
346 if (flags.Test(DoCommandFlag::Execute)) {
347 UpdateElement(*pe, tile, reference, text);
348 InvalidateWindowClassesData(WindowClass::StoryBook, pe->page);
349 }
350
351 return CommandCost();
352}
353
362{
364 if (!StoryPage::IsValidID(page_id)) return CMD_ERROR;
365
366 if (flags.Test(DoCommandFlag::Execute)) {
367 StoryPage *p = StoryPage::Get(page_id);
368 p->title = text;
369
370 InvalidateWindowClassesData(WindowClass::StoryBook, page_id);
371 }
372
373 return CommandCost();
374}
375
384{
386 if (!StoryPage::IsValidID(page_id)) return CMD_ERROR;
387
388 if (flags.Test(DoCommandFlag::Execute)) {
389 StoryPage *p = StoryPage::Get(page_id);
390 p->date = date;
391
392 InvalidateWindowClassesData(WindowClass::StoryBook, page_id);
393 }
394
395 return CommandCost();
396}
397
406{
408 if (!StoryPage::IsValidID(page_id)) return CMD_ERROR;
409
410 if (flags.Test(DoCommandFlag::Execute)) {
411 StoryPage *g = StoryPage::Get(page_id);
412 if ((g->company != CompanyID::Invalid() && g->company == _local_company) || (g->company == CompanyID::Invalid() && Company::IsValidID(_local_company))) ShowStoryBook(_local_company, page_id, true);
413 }
414
415 return CommandCost();
416}
417
424{
426 if (!StoryPage::IsValidID(page_id)) return CMD_ERROR;
427
428 if (flags.Test(DoCommandFlag::Execute)) {
429 StoryPage *p = StoryPage::Get(page_id);
430
432 if (pe->page == p->index) {
433 delete pe;
434 }
435 }
436
437 delete p;
438
439 InvalidateWindowClassesData(WindowClass::StoryBook, -1);
440 if (StoryPage::GetNumItems() == 0) InvalidateWindowData(WindowClass::MainToolbar, 0);
441 }
442
443 return CommandCost();
444}
445
453{
455 if (!StoryPageElement::IsValidID(page_element_id)) return CMD_ERROR;
456
457 if (flags.Test(DoCommandFlag::Execute)) {
458 StoryPageElement *pe = StoryPageElement::Get(page_element_id);
459 StoryPageID page_id = pe->page;
460
461 delete pe;
462
463 InvalidateWindowClassesData(WindowClass::StoryBook, page_id);
464 }
465
466 return CommandCost();
467}
468
478{
479 if (!StoryPageElement::IsValidID(page_element_id)) return CMD_ERROR;
480 const StoryPageElement *const pe = StoryPageElement::Get(page_element_id);
481
482 /* Check the player belongs to the company that owns the page. */
483 const StoryPage *const sp = StoryPage::Get(pe->page);
484 if (sp->company != CompanyID::Invalid() && sp->company != _current_company) return CMD_ERROR;
485
486 switch (pe->type) {
488 /* No validation required */
489 if (flags.Test(DoCommandFlag::Execute)) Game::NewEvent(new ScriptEventStoryPageButtonClick(_current_company, pe->page, page_element_id));
490 break;
492 if (!IsValidTile(tile)) return CMD_ERROR;
493 if (flags.Test(DoCommandFlag::Execute)) Game::NewEvent(new ScriptEventStoryPageTileSelect(_current_company, pe->page, page_element_id, tile));
494 break;
496 if (!Vehicle::IsValidID(reference)) return CMD_ERROR;
497 if (flags.Test(DoCommandFlag::Execute)) Game::NewEvent(new ScriptEventStoryPageVehicleSelect(_current_company, pe->page, page_element_id, reference));
498 break;
499 default:
500 /* Invalid page element type, not a button. */
501 return CMD_ERROR;
502 }
503
504 return CommandCost();
505}
506
constexpr T SB(T &x, const uint8_t s, const uint8_t n, const U d)
Set n bits in x starting at bit s to d.
static constexpr uint GB(const T x, const uint8_t s, const uint8_t n)
Fetch n bits from x, started at bit s.
constexpr bool All(const Timpl &other) const
Test if all of the values are set.
constexpr bool Test(Tvalue_type value) const
Test if the value-th bit is set.
constexpr Tstorage base() const noexcept
Retrieve the raw value behind this bit set.
constexpr Timpl & Reset()
Reset all bits.
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.
static Date date
Current date in days (day counter).
StrongType::Typedef< int32_t, DateTag< struct Calendar >, StrongType::Compare, StrongType::Integer > Date
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.
constexpr std::underlying_type_t< enum_type > to_underlying(enum_type e)
Implementation of std::to_underlying (from C++23).
Definition enum_type.hpp:21
Base functions for all Games.
Colours
One of 16 base colours used for companies and windows/widgets.
Definition gfx_type.h:283
@ Invalid
Invalid marker.
Definition gfx_type.h:302
@ End
End-of-array marker.
Definition gfx_type.h:301
Goal base class.
Basic types related to goals.
PoolID< uint16_t, struct GoalIDTag, 64000, 0xFFFF > GoalID
ID of a goal.
Definition goal_type.h:63
GUI functions that shouldn't be here.
void ShowStoryBook(CompanyID company, StoryPageID page_id=StoryPageID::Invalid(), bool centered=false)
Raise or create the story book window for company, at page page_id.
bool IsValidColours(Colours colours)
Checks if a Colours value is valid.
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.
std::tuple< CommandCost, StoryPageElementID > CmdCreateStoryPageElement(DoCommandFlags flags, TileIndex tile, StoryPageID page_id, StoryPageElementType type, uint32_t reference, const EncodedString &text)
Create a new story page element.
Definition story.cpp:292
static bool VerifyElementContentParameters(StoryPageID page_id, StoryPageElementType type, TileIndex tile, uint32_t reference, const EncodedString &text)
This helper for Create/Update PageElement Cmd procedure verifies if the page element parameters are c...
Definition story.cpp:58
std::tuple< CommandCost, StoryPageID > CmdCreateStoryPage(DoCommandFlags flags, CompanyID company, const EncodedString &text)
Create a new story page.
Definition story.cpp:257
CommandCost CmdUpdateStoryPageElement(DoCommandFlags flags, TileIndex tile, StoryPageElementID page_element_id, uint32_t reference, const EncodedString &text)
Update a new story page element.
Definition story.cpp:335
static void UpdateElement(StoryPageElement &pe, TileIndex tile, uint32_t reference, const EncodedString &text)
This helper for Create/Update PageElement Cmd procedure updates a page element with new content data.
Definition story.cpp:105
CommandCost CmdRemoveStoryPageElement(DoCommandFlags flags, StoryPageElementID page_element_id)
Remove a story page element.
Definition story.cpp:452
CommandCost CmdRemoveStoryPage(DoCommandFlags flags, StoryPageID page_id)
Remove a story page and associated story page elements.
Definition story.cpp:423
CommandCost CmdStoryPageButton(DoCommandFlags flags, TileIndex tile, StoryPageElementID page_element_id, VehicleID reference)
Clicked/used a button on a story page.
Definition story.cpp:477
CommandCost CmdSetStoryPageTitle(DoCommandFlags flags, StoryPageID page_id, const EncodedString &text)
Update title of a story page.
Definition story.cpp:361
CommandCost CmdSetStoryPageDate(DoCommandFlags flags, StoryPageID page_id, TimerGameCalendar::Date date)
Update date of a story page.
Definition story.cpp:383
CommandCost CmdShowStoryPage(DoCommandFlags flags, StoryPageID page_id)
Display a story page for all clients that are allowed to view the story page.
Definition story.cpp:405
StoryPage base class.
StoryPageElementType
Each story page element is one of these types.
Definition story_base.h:29
@ Goal
An element that references a goal.
Definition story_base.h:32
@ ButtonTile
A button that allows the player to select a tile, and triggers an event with the tile.
Definition story_base.h:34
@ ButtonVehicle
A button that allows the player to select a vehicle, and triggers an event with the vehicle.
Definition story_base.h:35
@ ButtonPush
A push button that triggers an immediate event.
Definition story_base.h:33
@ Text
A text element.
Definition story_base.h:30
@ Location
An element that references a tile along with a one-line text.
Definition story_base.h:31
StoryPageButtonCursor
Mouse cursors usable by story page buttons.
Definition story_base.h:49
@ Invalid
Invalid story page button cursor.
Definition story_base.h:106
EnumBitSet< StoryPageButtonFlag, uint8_t > StoryPageButtonFlags
Bitset of StoryPageButtonFlag elements.
Definition story_base.h:46
bool IsValidStoryPageButtonCursor(StoryPageButtonCursor cursor)
Checks if a StoryPageButtonCursor value is valid.
Definition story_base.h:115
Command definitions related to stories.
PoolID< uint16_t, struct StoryPageIDTag, 64000, 0xFFFF > StoryPageID
ID of a story page.
Definition story_type.h:16
PoolID< uint16_t, struct StoryPageElementIDTag, 64000, 0xFFFF > StoryPageElementID
ID of a story page element.
Definition story_type.h:15
static Pool::IterateWrapper< StoryPageElement > Iterate(size_t from=0)
static T * Create(Targs &&... args)
static StoryPage * Get(auto index)
static bool IsValidID(auto index)
Helper to construct packed "id" values for button-type StoryPageElement.
Definition story_base.h:121
Colours GetColour() const
Get the button background colour.
Definition story.cpp:171
VehicleType GetVehicleType() const
Get the type of vehicles that are accepted by the button.
Definition story.cpp:202
void SetColour(Colours button_colour)
Set the button background colour.
Definition story.cpp:132
bool ValidateVehicleType() const
Verity that the data stored a valid VehicleType value.
Definition story.cpp:244
StoryPageButtonFlags GetFlags() const
Get the button flags.
Definition story.cpp:182
bool ValidateFlags() const
Verify that valid flags were set.
Definition story.cpp:221
bool ValidateColour() const
Verify that the data stored a valid Colour value.
Definition story.cpp:211
void SetVehicleType(VehicleType vehtype)
Set the type of vehicles that are accepted by the button.
Definition story.cpp:161
StoryPageButtonCursor GetCursor() const
Get the mouse cursor used while waiting for input for the button.
Definition story.cpp:191
bool ValidateCursor() const
Verify that the data stores a valid StoryPageButtonCursor value.
Definition story.cpp:235
void SetCursor(StoryPageButtonCursor cursor)
Set the mouse cursor used while waiting for input for the button.
Definition story.cpp:151
void SetFlags(StoryPageButtonFlags flags)
Set the button flags.
Definition story.cpp:142
Struct about story page elements.
Definition story_base.h:143
uint32_t referenced_id
Id of referenced object (location, goal etc.).
Definition story_base.h:148
EncodedString text
Static content text of page element.
Definition story_base.h:149
StoryPageElementType type
Type of page element.
Definition story_base.h:146
StoryPageID page
Id of the page which the page element belongs to.
Definition story_base.h:145
Struct about stories, current and completed.
Definition story_base.h:157
EncodedString title
Title of story page.
Definition story_base.h:162
CompanyID company
StoryPage is for a specific company; CompanyID::Invalid() if it is global.
Definition story_base.h:160
TimerGameCalendar::Date date
Date when the page was created.
Definition story_base.h:159
Map writing/reading functions for tiles.
bool IsValidTile(Tile tile)
Checks if a tile is valid.
Definition tile_map.h:161
StrongType::Typedef< uint32_t, struct TileIndexTag, StrongType::Compare, StrongType::Integer, StrongType::Compatible< int32_t >, StrongType::Compatible< int64_t > > TileIndex
The index/ID of a Tile.
Definition tile_type.h:92
Definition of the game-calendar-timer.
Base class for all vehicles.
PoolID< uint32_t, struct VehicleIDTag, 0xFF000, 0xFFFFF > VehicleID
The type all our vehicle IDs have.
VehicleType
Available vehicle types.
@ Invalid
Non-existing type of vehicle.
@ CompanyEnd
Last company-ownable type.
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:3377
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:3395
Window functions not directly related to making/drawing windows.