OpenTTD Source 20250312-master-gcdcc6b491d
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 <http://www.gnu.org/licenses/>.
6 */
7
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 (!this->CleaningPool()) {
44 if (spe->page == this->index) delete spe;
45 }
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
130void StoryPageButtonData::SetColour(Colours button_colour)
131{
132 assert(button_colour < COLOUR_END);
133 SB(this->referenced_id, 0, 8, button_colour);
134}
135
136void StoryPageButtonData::SetFlags(StoryPageButtonFlags flags)
137{
138 SB(this->referenced_id, 24, 8, flags);
139}
140
143{
144 assert(cursor < SPBC_END);
145 SB(this->referenced_id, 8, 8, cursor);
146}
147
150{
151 assert(vehtype == VEH_INVALID || vehtype < VEH_COMPANY_END);
152 SB(this->referenced_id, 16, 8, vehtype);
153}
154
157{
158 Colours colour = static_cast<Colours>(GB(this->referenced_id, 0, 8));
159 if (!IsValidColours(colour)) return INVALID_COLOUR;
160 return colour;
161}
162
163StoryPageButtonFlags StoryPageButtonData::GetFlags() const
164{
165 return (StoryPageButtonFlags)GB(this->referenced_id, 24, 8);
166}
167
170{
171 StoryPageButtonCursor cursor = (StoryPageButtonCursor)GB(this->referenced_id, 8, 8);
172 if (!IsValidStoryPageButtonCursor(cursor)) return INVALID_SPBC;
173 return cursor;
174}
175
178{
179 return (VehicleType)GB(this->referenced_id, 16, 8);
180}
181
184{
185 return GB(this->referenced_id, 0, 8) < COLOUR_END;
186}
187
188bool StoryPageButtonData::ValidateFlags() const
189{
190 uint8_t flags = GB(this->referenced_id, 24, 8);
191 /* Don't allow float left and right together */
192 if ((flags & SPBF_FLOAT_LEFT) && (flags & SPBF_FLOAT_RIGHT)) return false;
193 /* Don't allow undefined flags */
194 if (flags & ~(SPBF_FLOAT_LEFT | SPBF_FLOAT_RIGHT)) return false;
195 return true;
196}
197
200{
201 return GB(this->referenced_id, 8, 8) < SPBC_END;
202}
203
206{
207 uint8_t vehtype = GB(this->referenced_id, 16, 8);
208 return vehtype == VEH_INVALID || vehtype < VEH_COMPANY_END;
209}
210
218std::tuple<CommandCost, StoryPageID> CmdCreateStoryPage(DoCommandFlags flags, CompanyID company, const EncodedString &text)
219{
220 if (!StoryPage::CanAllocateItem()) return { CMD_ERROR, StoryPageID::Invalid() };
221
222 if (_current_company != OWNER_DEITY) return { CMD_ERROR, StoryPageID::Invalid() };
223 if (company != CompanyID::Invalid() && !Company::IsValidID(company)) return { CMD_ERROR, StoryPageID::Invalid() };
224
225 if (flags.Test(DoCommandFlag::Execute)) {
226 if (StoryPage::GetNumItems() == 0) {
227 /* Initialize the next sort value variable. */
228 _story_page_next_sort_value = 0;
229 }
230
231 StoryPage *s = new StoryPage(_story_page_next_sort_value, TimerGameCalendar::date, company, text);
232
235
236 _story_page_next_sort_value++;
237 return { CommandCost(), s->index };
238 }
239
240 return { CommandCost(), StoryPageID::Invalid() };
241}
242
253std::tuple<CommandCost, StoryPageElementID> CmdCreateStoryPageElement(DoCommandFlags flags, TileIndex tile, StoryPageID page_id, StoryPageElementType type, uint32_t reference, const EncodedString &text)
254{
255 if (!StoryPageElement::CanAllocateItem()) return { CMD_ERROR, StoryPageElementID::Invalid() };
256
257 /* Allow at most 128 elements per page. */
258 uint16_t element_count = 0;
260 if (iter->page == page_id) element_count++;
261 }
262 if (element_count >= 128) return { CMD_ERROR, StoryPageElementID::Invalid() };
263
264 if (_current_company != OWNER_DEITY) return { CMD_ERROR, StoryPageElementID::Invalid() };
265 if (!StoryPage::IsValidID(page_id)) return { CMD_ERROR, StoryPageElementID::Invalid() };
266 if (!VerifyElementContentParameters(page_id, type, tile, reference, text)) return { CMD_ERROR, StoryPageElementID::Invalid() };
267
268
269 if (flags.Test(DoCommandFlag::Execute)) {
271 /* Initialize the next sort value variable. */
272 _story_page_element_next_sort_value = 0;
273 }
274
275 StoryPageElement *pe = new StoryPageElement(_story_page_element_next_sort_value, type, page_id);
276 UpdateElement(*pe, tile, reference, text);
277
279
280 _story_page_element_next_sort_value++;
281 return { CommandCost(), pe->index };
282 }
283
284 return { CommandCost(), StoryPageElementID::Invalid() };
285}
286
296CommandCost CmdUpdateStoryPageElement(DoCommandFlags flags, TileIndex tile, StoryPageElementID page_element_id, uint32_t reference, const EncodedString &text)
297{
299 if (!StoryPageElement::IsValidID(page_element_id)) return CMD_ERROR;
300
301 StoryPageElement *pe = StoryPageElement::Get(page_element_id);
302 StoryPageID page_id = pe->page;
303 StoryPageElementType type = pe->type;
304
305 if (!VerifyElementContentParameters(page_id, type, tile, reference, text)) return CMD_ERROR;
306
307 if (flags.Test(DoCommandFlag::Execute)) {
308 UpdateElement(*pe, tile, reference, text);
310 }
311
312 return CommandCost();
313}
314
323{
325 if (!StoryPage::IsValidID(page_id)) return CMD_ERROR;
326
327 if (flags.Test(DoCommandFlag::Execute)) {
328 StoryPage *p = StoryPage::Get(page_id);
329 p->title = text;
330
332 }
333
334 return CommandCost();
335}
336
344CommandCost CmdSetStoryPageDate(DoCommandFlags flags, StoryPageID page_id, TimerGameCalendar::Date date)
345{
347 if (!StoryPage::IsValidID(page_id)) return CMD_ERROR;
348
349 if (flags.Test(DoCommandFlag::Execute)) {
350 StoryPage *p = StoryPage::Get(page_id);
351 p->date = date;
352
354 }
355
356 return CommandCost();
357}
358
367{
369 if (!StoryPage::IsValidID(page_id)) return CMD_ERROR;
370
371 if (flags.Test(DoCommandFlag::Execute)) {
372 StoryPage *g = StoryPage::Get(page_id);
373 if ((g->company != CompanyID::Invalid() && g->company == _local_company) || (g->company == CompanyID::Invalid() && Company::IsValidID(_local_company))) ShowStoryBook(_local_company, page_id, true);
374 }
375
376 return CommandCost();
377}
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
393 if (pe->page == p->index) {
394 delete pe;
395 }
396 }
397
398 delete p;
399
402 }
403
404 return CommandCost();
405}
406
414{
416 if (!StoryPageElement::IsValidID(page_element_id)) return CMD_ERROR;
417
418 if (flags.Test(DoCommandFlag::Execute)) {
419 StoryPageElement *pe = StoryPageElement::Get(page_element_id);
420 StoryPageID page_id = pe->page;
421
422 delete pe;
423
425 }
426
427 return CommandCost();
428}
429
439{
440 if (!StoryPageElement::IsValidID(page_element_id)) return CMD_ERROR;
441 const StoryPageElement *const pe = StoryPageElement::Get(page_element_id);
442
443 /* Check the player belongs to the company that owns the page. */
444 const StoryPage *const sp = StoryPage::Get(pe->page);
445 if (sp->company != CompanyID::Invalid() && sp->company != _current_company) return CMD_ERROR;
446
447 switch (pe->type) {
448 case SPET_BUTTON_PUSH:
449 /* No validation required */
450 if (flags.Test(DoCommandFlag::Execute)) Game::NewEvent(new ScriptEventStoryPageButtonClick(_current_company, pe->page, page_element_id));
451 break;
452 case SPET_BUTTON_TILE:
453 if (!IsValidTile(tile)) return CMD_ERROR;
454 if (flags.Test(DoCommandFlag::Execute)) Game::NewEvent(new ScriptEventStoryPageTileSelect(_current_company, pe->page, page_element_id, tile));
455 break;
457 if (!Vehicle::IsValidID(reference)) return CMD_ERROR;
458 if (flags.Test(DoCommandFlag::Execute)) Game::NewEvent(new ScriptEventStoryPageVehicleSelect(_current_company, pe->page, page_element_id, reference));
459 break;
460 default:
461 /* Invalid page element type, not a button. */
462 return CMD_ERROR;
463 }
464
465 return CommandCost();
466}
467
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.
debug_inline 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.
Enum-as-bit-set wrapper.
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
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:253
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:218
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:296
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:413
CommandCost CmdRemoveStoryPage(DoCommandFlags flags, StoryPageID page_id)
Remove a story page and associated story page elements.
Definition story.cpp:384
CommandCost CmdStoryPageButton(DoCommandFlags flags, TileIndex tile, StoryPageElementID page_element_id, VehicleID reference)
Clicked/used a button on a story page.
Definition story.cpp:438
CommandCost CmdSetStoryPageTitle(DoCommandFlags flags, StoryPageID page_id, const EncodedString &text)
Update title of a story page.
Definition story.cpp:322
CommandCost CmdSetStoryPageDate(DoCommandFlags flags, StoryPageID page_id, TimerGameCalendar::Date date)
Update date of a story page.
Definition story.cpp:344
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:366
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.
Functions related to low-level strings.
static Pool::IterateWrapper< Titem > Iterate(size_t from=0)
Returns an iterable ensemble of all valid Titem.
static Titem * Get(auto index)
Returns Titem with given index.
static size_t GetNumItems()
Returns number of valid items in the pool.
Tindex index
Index of this pool item.
static bool CanAllocateItem(size_t n=1)
Helper functions so we can use PoolItem::Function() instead of _poolitem_pool.Function()
static bool IsValidID(auto index)
Tests whether given index can be used to get valid (non-nullptr) Titem.
static bool CleaningPool()
Returns current state of pool cleaning - yes or no.
Base class for all pools.
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:156
VehicleType GetVehicleType() const
Get the type of vehicles that are accepted by the button.
Definition story.cpp:177
void SetColour(Colours button_colour)
Set the button background colour.
Definition story.cpp:130
bool ValidateVehicleType() const
Verity that the data stored a valid VehicleType value.
Definition story.cpp:205
bool ValidateColour() const
Verify that the data stored a valid Colour value.
Definition story.cpp:183
void SetVehicleType(VehicleType vehtype)
Set the type of vehicles that are accepted by the button.
Definition story.cpp:149
StoryPageButtonCursor GetCursor() const
Get the mouse cursor used while waiting for input for the button.
Definition story.cpp:169
bool ValidateCursor() const
Verify that the data stores a valid StoryPageButtonCursor value.
Definition story.cpp:199
void SetCursor(StoryPageButtonCursor cursor)
Set the mouse cursor used while waiting for input for the button.
Definition story.cpp:142
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:167
EncodedString title
Title of story page.
Definition story_base.h:172
CompanyID company
StoryPage is for a specific company; CompanyID::Invalid() if it is global.
Definition story_base.h:170
TimerGameCalendar::Date date
Date when the page was created.
Definition story_base.h:169
Map writing/reading functions for tiles.
bool IsValidTile(Tile tile)
Checks if a tile is valid.
Definition tile_map.h:161
Definition of the game-calendar-timer.
Base class for all vehicles.
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:3224
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:3241
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:60