OpenTTD Source 20260208-master-g43af8e94d0
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"
16#include "string_func.h"
18#include "tile_map.h"
19#include "goal_type.h"
20#include "goal_base.h"
21#include "window_func.h"
22#include "gui.h"
23#include "vehicle_base.h"
24#include "game/game.hpp"
25#include "script/api/script_story_page.hpp"
26#include "script/api/script_event_types.hpp"
27#include "story_cmd.h"
28
29#include "safeguards.h"
30
31
32uint32_t _story_page_element_next_sort_value;
33uint32_t _story_page_next_sort_value;
34
35StoryPageElementPool _story_page_element_pool("StoryPageElement");
36StoryPagePool _story_page_pool("StoryPage");
39
40StoryPage::~StoryPage()
41{
42 if (CleaningPool()) return;
43
44 for (StoryPageElement *spe : StoryPageElement::Iterate()) {
45 if (spe->page == this->index) delete spe;
46 }
47}
48
59static bool VerifyElementContentParameters(StoryPageID page_id, StoryPageElementType type, TileIndex tile, uint32_t reference, const EncodedString &text)
60{
61 StoryPageButtonData button_data{ reference };
62
63 switch (type) {
64 case SPET_TEXT:
65 if (text.empty()) return false;
66 break;
67 case SPET_LOCATION:
68 if (text.empty()) return false;
69 if (!IsValidTile(tile)) return false;
70 break;
71 case SPET_GOAL:
72 if (!Goal::IsValidID((GoalID)reference)) return false;
73 /* Reject company specific goals on global pages */
74 if (StoryPage::Get(page_id)->company == CompanyID::Invalid() && Goal::Get((GoalID)reference)->company != CompanyID::Invalid()) return false;
75 break;
77 if (!button_data.ValidateColour()) return false;
78 if (!button_data.ValidateFlags()) return false;
79 return true;
81 if (!button_data.ValidateColour()) return false;
82 if (!button_data.ValidateFlags()) return false;
83 if (!button_data.ValidateCursor()) return false;
84 return true;
86 if (!button_data.ValidateColour()) return false;
87 if (!button_data.ValidateFlags()) return false;
88 if (!button_data.ValidateCursor()) return false;
89 if (!button_data.ValidateVehicleType()) return false;
90 return true;
91 default:
92 return false;
93 }
94
95 return true;
96}
97
106static void UpdateElement(StoryPageElement &pe, TileIndex tile, uint32_t reference, const EncodedString &text)
107{
108 switch (pe.type) {
109 case SPET_TEXT:
110 pe.text = text;
111 break;
112 case SPET_LOCATION:
113 pe.text = text;
114 pe.referenced_id = tile.base();
115 break;
116 case SPET_GOAL:
117 pe.referenced_id = reference;
118 break;
119 case SPET_BUTTON_PUSH:
120 case SPET_BUTTON_TILE:
122 pe.text = text;
123 pe.referenced_id = reference;
124 break;
125 default: NOT_REACHED();
126 }
127}
128
133void StoryPageButtonData::SetColour(Colours button_colour)
134{
135 assert(button_colour < COLOUR_END);
136 SB(this->referenced_id, 0, 8, button_colour);
137}
138
144{
145 SB(this->referenced_id, 24, 8, flags);
146}
147
153{
154 assert(cursor < SPBC_END);
155 SB(this->referenced_id, 8, 8, cursor);
156}
157
163{
164 assert(vehtype == VEH_INVALID || vehtype < VEH_COMPANY_END);
165 SB(this->referenced_id, 16, 8, vehtype);
166}
167
173{
174 Colours colour = static_cast<Colours>(GB(this->referenced_id, 0, 8));
175 if (!IsValidColours(colour)) return INVALID_COLOUR;
176 return colour;
177}
178
184{
185 return (StoryPageButtonFlags)GB(this->referenced_id, 24, 8);
186}
187
193{
194 StoryPageButtonCursor cursor = (StoryPageButtonCursor)GB(this->referenced_id, 8, 8);
195 if (!IsValidStoryPageButtonCursor(cursor)) return INVALID_SPBC;
196 return cursor;
197}
198
204{
205 return (VehicleType)GB(this->referenced_id, 16, 8);
206}
207
213{
214 return GB(this->referenced_id, 0, 8) < COLOUR_END;
215}
216
223{
224 uint8_t flags = GB(this->referenced_id, 24, 8);
225 /* Don't allow float left and right together */
226 if ((flags & SPBF_FLOAT_LEFT) && (flags & SPBF_FLOAT_RIGHT)) return false;
227 /* Don't allow undefined flags */
228 if (flags & ~(SPBF_FLOAT_LEFT | SPBF_FLOAT_RIGHT)) return false;
229 return true;
230}
231
237{
238 return GB(this->referenced_id, 8, 8) < SPBC_END;
239}
240
246{
247 uint8_t vehtype = GB(this->referenced_id, 16, 8);
248 return vehtype == VEH_INVALID || vehtype < VEH_COMPANY_END;
249}
250
258std::tuple<CommandCost, StoryPageID> CmdCreateStoryPage(DoCommandFlags flags, CompanyID company, const EncodedString &text)
259{
260 if (!StoryPage::CanAllocateItem()) return { CMD_ERROR, StoryPageID::Invalid() };
261
262 if (_current_company != OWNER_DEITY) return { CMD_ERROR, StoryPageID::Invalid() };
263 if (company != CompanyID::Invalid() && !Company::IsValidID(company)) return { CMD_ERROR, StoryPageID::Invalid() };
264
265 if (flags.Test(DoCommandFlag::Execute)) {
266 if (StoryPage::GetNumItems() == 0) {
267 /* Initialize the next sort value variable. */
268 _story_page_next_sort_value = 0;
269 }
270
271 StoryPage *s = StoryPage::Create(_story_page_next_sort_value, TimerGameCalendar::date, company, text);
272
275
276 _story_page_next_sort_value++;
277 return { CommandCost(), s->index };
278 }
279
280 return { CommandCost(), StoryPageID::Invalid() };
281}
282
293std::tuple<CommandCost, StoryPageElementID> CmdCreateStoryPageElement(DoCommandFlags flags, TileIndex tile, StoryPageID page_id, StoryPageElementType type, uint32_t reference, const EncodedString &text)
294{
295 if (!StoryPageElement::CanAllocateItem()) return { CMD_ERROR, StoryPageElementID::Invalid() };
296
297 /* Allow at most 128 elements per page. */
298 uint16_t element_count = 0;
300 if (iter->page == page_id) element_count++;
301 }
302 if (element_count >= 128) return { CMD_ERROR, StoryPageElementID::Invalid() };
303
304 if (_current_company != OWNER_DEITY) return { CMD_ERROR, StoryPageElementID::Invalid() };
305 if (!StoryPage::IsValidID(page_id)) return { CMD_ERROR, StoryPageElementID::Invalid() };
306 if (!VerifyElementContentParameters(page_id, type, tile, reference, text)) return { CMD_ERROR, StoryPageElementID::Invalid() };
307
308
309 if (flags.Test(DoCommandFlag::Execute)) {
311 /* Initialize the next sort value variable. */
312 _story_page_element_next_sort_value = 0;
313 }
314
315 StoryPageElement *pe = StoryPageElement::Create(_story_page_element_next_sort_value, type, page_id);
316 UpdateElement(*pe, tile, reference, text);
317
319
320 _story_page_element_next_sort_value++;
321 return { CommandCost(), pe->index };
322 }
323
324 return { CommandCost(), StoryPageElementID::Invalid() };
325}
326
336CommandCost CmdUpdateStoryPageElement(DoCommandFlags flags, TileIndex tile, StoryPageElementID page_element_id, uint32_t reference, const EncodedString &text)
337{
339 if (!StoryPageElement::IsValidID(page_element_id)) return CMD_ERROR;
340
341 StoryPageElement *pe = StoryPageElement::Get(page_element_id);
342 StoryPageID page_id = pe->page;
343 StoryPageElementType type = pe->type;
344
345 if (!VerifyElementContentParameters(page_id, type, tile, reference, text)) return CMD_ERROR;
346
347 if (flags.Test(DoCommandFlag::Execute)) {
348 UpdateElement(*pe, tile, reference, text);
350 }
351
352 return CommandCost();
353}
354
362CommandCost CmdSetStoryPageTitle(DoCommandFlags flags, StoryPageID page_id, const EncodedString &text)
363{
365 if (!StoryPage::IsValidID(page_id)) return CMD_ERROR;
366
367 if (flags.Test(DoCommandFlag::Execute)) {
368 StoryPage *p = StoryPage::Get(page_id);
369 p->title = text;
370
372 }
373
374 return CommandCost();
375}
376
384CommandCost CmdSetStoryPageDate(DoCommandFlags flags, StoryPageID page_id, TimerGameCalendar::Date date)
385{
387 if (!StoryPage::IsValidID(page_id)) return CMD_ERROR;
388
389 if (flags.Test(DoCommandFlag::Execute)) {
390 StoryPage *p = StoryPage::Get(page_id);
391 p->date = date;
392
394 }
395
396 return CommandCost();
397}
398
406CommandCost CmdShowStoryPage(DoCommandFlags flags, StoryPageID page_id)
407{
409 if (!StoryPage::IsValidID(page_id)) return CMD_ERROR;
410
411 if (flags.Test(DoCommandFlag::Execute)) {
412 StoryPage *g = StoryPage::Get(page_id);
413 if ((g->company != CompanyID::Invalid() && g->company == _local_company) || (g->company == CompanyID::Invalid() && Company::IsValidID(_local_company))) ShowStoryBook(_local_company, page_id, true);
414 }
415
416 return CommandCost();
417}
418
424CommandCost CmdRemoveStoryPage(DoCommandFlags flags, StoryPageID page_id)
425{
427 if (!StoryPage::IsValidID(page_id)) return CMD_ERROR;
428
429 if (flags.Test(DoCommandFlag::Execute)) {
430 StoryPage *p = StoryPage::Get(page_id);
431
433 if (pe->page == p->index) {
434 delete pe;
435 }
436 }
437
438 delete p;
439
442 }
443
444 return CommandCost();
445}
446
453CommandCost CmdRemoveStoryPageElement(DoCommandFlags flags, StoryPageElementID page_element_id)
454{
456 if (!StoryPageElement::IsValidID(page_element_id)) return CMD_ERROR;
457
458 if (flags.Test(DoCommandFlag::Execute)) {
459 StoryPageElement *pe = StoryPageElement::Get(page_element_id);
460 StoryPageID page_id = pe->page;
461
462 delete pe;
463
465 }
466
467 return CommandCost();
468}
469
478CommandCost CmdStoryPageButton(DoCommandFlags flags, TileIndex tile, StoryPageElementID page_element_id, VehicleID reference)
479{
480 if (!StoryPageElement::IsValidID(page_element_id)) return CMD_ERROR;
481 const StoryPageElement *const pe = StoryPageElement::Get(page_element_id);
482
483 /* Check the player belongs to the company that owns the page. */
484 const StoryPage *const sp = StoryPage::Get(pe->page);
485 if (sp->company != CompanyID::Invalid() && sp->company != _current_company) return CMD_ERROR;
486
487 switch (pe->type) {
488 case SPET_BUTTON_PUSH:
489 /* No validation required */
490 if (flags.Test(DoCommandFlag::Execute)) Game::NewEvent(new ScriptEventStoryPageButtonClick(_current_company, pe->page, page_element_id));
491 break;
492 case SPET_BUTTON_TILE:
493 if (!IsValidTile(tile)) return CMD_ERROR;
494 if (flags.Test(DoCommandFlag::Execute)) Game::NewEvent(new ScriptEventStoryPageTileSelect(_current_company, pe->page, page_element_id, tile));
495 break;
497 if (!Vehicle::IsValidID(reference)) return CMD_ERROR;
498 if (flags.Test(DoCommandFlag::Execute)) Game::NewEvent(new ScriptEventStoryPageVehicleSelect(_current_company, pe->page, page_element_id, reference));
499 break;
500 default:
501 /* Invalid page element type, not a button. */
502 return CMD_ERROR;
503 }
504
505 return CommandCost();
506}
507
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 Test(Tvalue_type value) const
Test if the value-th bit is set.
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 a Game Script.
static Date date
Current date in days (day counter).
Functions related to commands.
static const CommandCost CMD_ERROR
Define a default return value for a failed command.
@ Execute
execute the given command
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.
Goal base class.
Basic types related to goals.
PoolID< uint16_t, struct GoalIDTag, 64000, 0xFFFF > GoalID
ID of a goal.
Definition goal_type.h:38
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:293
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:59
std::tuple< CommandCost, StoryPageID > CmdCreateStoryPage(DoCommandFlags flags, CompanyID company, const EncodedString &text)
Create a new story page.
Definition story.cpp:258
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:336
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:106
CommandCost CmdRemoveStoryPageElement(DoCommandFlags flags, StoryPageElementID page_element_id)
Remove a story page element.
Definition story.cpp:453
CommandCost CmdRemoveStoryPage(DoCommandFlags flags, StoryPageID page_id)
Remove a story page and associated story page elements.
Definition story.cpp:424
CommandCost CmdStoryPageButton(DoCommandFlags flags, TileIndex tile, StoryPageElementID page_element_id, VehicleID reference)
Clicked/used a button on a story page.
Definition story.cpp:478
CommandCost CmdSetStoryPageTitle(DoCommandFlags flags, StoryPageID page_id, const EncodedString &text)
Update title of a story page.
Definition story.cpp:362
CommandCost CmdSetStoryPageDate(DoCommandFlags flags, StoryPageID page_id, TimerGameCalendar::Date date)
Update date of a story page.
Definition story.cpp:384
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:406
StoryPage base class.
StoryPageButtonFlags
Flags available for buttons.
Definition story_base.h:43
StoryPageElementType
Definition story_base.h:31
@ SPET_LOCATION
An element that references a tile along with a one-line text.
Definition story_base.h:33
@ SPET_GOAL
An element that references a goal.
Definition story_base.h:34
@ SPET_BUTTON_PUSH
A push button that triggers an immediate event.
Definition story_base.h:35
@ SPET_BUTTON_TILE
A button that allows the player to select a tile, and triggers an event with the tile.
Definition story_base.h:36
@ SPET_TEXT
A text element.
Definition story_base.h:32
@ SPET_BUTTON_VEHICLE
A button that allows the player to select a vehicle, and triggers an event with the vehicle.
Definition story_base.h:37
StoryPageButtonCursor
Mouse cursors usable by story page buttons.
Definition story_base.h:51
bool IsValidStoryPageButtonCursor(StoryPageButtonCursor cursor)
Checks if a StoryPageButtonCursor value is valid.
Definition story_base.h:117
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
Functions related to low-level strings.
static Pool::IterateWrapper< StoryPageElement > Iterate(size_t from=0)
static StoryPage * Get(auto index)
static bool IsValidID(auto index)
static T * Create(Targs &&... args)
Helper to construct packed "id" values for button-type StoryPageElement.
Definition story_base.h:123
Colours GetColour() const
Get the button background colour.
Definition story.cpp:172
VehicleType GetVehicleType() const
Get the type of vehicles that are accepted by the button.
Definition story.cpp:203
void SetColour(Colours button_colour)
Set the button background colour.
Definition story.cpp:133
bool ValidateVehicleType() const
Verity that the data stored a valid VehicleType value.
Definition story.cpp:245
StoryPageButtonFlags GetFlags() const
Get the button flags.
Definition story.cpp:183
bool ValidateFlags() const
Verify that valid flags were set.
Definition story.cpp:222
bool ValidateColour() const
Verify that the data stored a valid Colour value.
Definition story.cpp:212
void SetVehicleType(VehicleType vehtype)
Set the type of vehicles that are accepted by the button.
Definition story.cpp:162
StoryPageButtonCursor GetCursor() const
Get the mouse cursor used while waiting for input for the button.
Definition story.cpp:192
bool ValidateCursor() const
Verify that the data stores a valid StoryPageButtonCursor value.
Definition story.cpp:236
void SetCursor(StoryPageButtonCursor cursor)
Set the mouse cursor used while waiting for input for the button.
Definition story.cpp:152
void SetFlags(StoryPageButtonFlags flags)
Set the button flags.
Definition story.cpp:143
Struct about story page elements.
Definition story_base.h:145
uint32_t referenced_id
Id of referenced object (location, goal etc.).
Definition story_base.h:150
EncodedString text
Static content text of page element.
Definition story_base.h:151
StoryPageElementType type
Type of page element.
Definition story_base.h:148
StoryPageID page
Id of the page which the page element belongs to.
Definition story_base.h:147
Struct about stories, current and completed.
Definition story_base.h:164
EncodedString title
Title of story page.
Definition story_base.h:169
CompanyID company
StoryPage is for a specific company; CompanyID::Invalid() if it is global.
Definition story_base.h:167
TimerGameCalendar::Date date
Date when the page was created.
Definition story_base.h:166
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.
@ VEH_INVALID
Non-existing type of vehicle.
@ VEH_COMPANY_END
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:3310
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:3328
Window functions not directly related to making/drawing windows.
@ WC_STORY_BOOK
Story book; Window numbers:
@ WC_MAIN_TOOLBAR
Main toolbar (the long bar at the top); Window numbers:
Definition window_type.h:63