OpenTTD Source  20241121-master-g67a0fccfad
league_cmd.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 "league_cmd.h"
12 #include "league_base.h"
13 #include "command_type.h"
14 #include "command_func.h"
15 #include "industry.h"
16 #include "story_base.h"
17 #include "town.h"
18 #include "window_func.h"
19 #include "core/pool_func.hpp"
20 
21 #include "safeguards.h"
22 
23 LeagueTableElementPool _league_table_element_pool("LeagueTableElement");
25 
26 LeagueTablePool _league_table_pool("LeagueTable");
28 
29 
34 bool IsValidLink(Link link)
35 {
36  switch (link.type) {
37  case LT_NONE: return (link.target == 0);
38  case LT_TILE: return IsValidTile(link.target);
39  case LT_INDUSTRY: return Industry::IsValidID(link.target);
40  case LT_TOWN: return Town::IsValidID(link.target);
41  case LT_COMPANY: return Company::IsValidID(link.target);
42  case LT_STORY_PAGE: return StoryPage::IsValidID(link.target);
43  default: return false;
44  }
45  return false;
46 }
47 
56 std::tuple<CommandCost, LeagueTableID> CmdCreateLeagueTable(DoCommandFlag flags, const std::string &title, const std::string &header, const std::string &footer)
57 {
60  if (title.empty()) return { CMD_ERROR, INVALID_LEAGUE_TABLE };
61 
62  if (flags & DC_EXEC) {
63  LeagueTable *lt = new LeagueTable();
64  lt->title = title;
65  lt->header = header;
66  lt->footer = footer;
67  return { CommandCost(), lt->index };
68  }
69 
70  return { CommandCost(), INVALID_LEAGUE_TABLE };
71 }
72 
73 
86 std::tuple<CommandCost, LeagueTableElementID> CmdCreateLeagueTableElement(DoCommandFlag flags, LeagueTableID table, int64_t rating, CompanyID company, const std::string &text, const std::string &score, LinkType link_type, LinkTargetID link_target)
87 {
90  Link link{link_type, link_target};
91  if (!IsValidLink(link)) return { CMD_ERROR, INVALID_LEAGUE_TABLE_ELEMENT };
92  if (company != INVALID_COMPANY && !Company::IsValidID(company)) return { CMD_ERROR, INVALID_LEAGUE_TABLE_ELEMENT };
93 
94  if (flags & DC_EXEC) {
96  lte->table = table;
97  lte->rating = rating;
98  lte->company = company;
99  lte->text = text;
100  lte->score = score;
101  lte->link = link;
103  return { CommandCost(), lte->index };
104  }
106 }
107 
118 CommandCost CmdUpdateLeagueTableElementData(DoCommandFlag flags, LeagueTableElementID element, CompanyID company, const std::string &text, LinkType link_type, LinkTargetID link_target)
119 {
120  if (_current_company != OWNER_DEITY) return CMD_ERROR;
121  auto lte = LeagueTableElement::GetIfValid(element);
122  if (lte == nullptr) return CMD_ERROR;
123  if (company != INVALID_COMPANY && !Company::IsValidID(company)) return CMD_ERROR;
124  Link link{link_type, link_target};
125  if (!IsValidLink(link)) return CMD_ERROR;
126 
127  if (flags & DC_EXEC) {
128  lte->company = company;
129  lte->text = text;
130  lte->link = link;
132  }
133  return CommandCost();
134 }
135 
144 CommandCost CmdUpdateLeagueTableElementScore(DoCommandFlag flags, LeagueTableElementID element, int64_t rating, const std::string &score)
145 {
146  if (_current_company != OWNER_DEITY) return CMD_ERROR;
147  auto lte = LeagueTableElement::GetIfValid(element);
148  if (lte == nullptr) return CMD_ERROR;
149 
150  if (flags & DC_EXEC) {
151  lte->rating = rating;
152  lte->score = score;
154  }
155  return CommandCost();
156 }
157 
165 {
166  if (_current_company != OWNER_DEITY) return CMD_ERROR;
167  auto lte = LeagueTableElement::GetIfValid(element);
168  if (lte == nullptr) return CMD_ERROR;
169 
170  if (flags & DC_EXEC) {
171  auto table = lte->table;
172  delete lte;
174  }
175  return CommandCost();
176 }
Common return value for all commands.
Definition: command_type.h:23
Functions related to commands.
static const CommandCost CMD_ERROR
Define a default return value for a failed command.
Definition: command_func.h:28
Types related to commands.
DoCommandFlag
List of flags for a command.
Definition: command_type.h:374
@ DC_EXEC
execute the given command
Definition: command_type.h:376
CompanyID _current_company
Company currently doing an action.
Definition: company_cmd.cpp:53
Owner
Enum for all companies/owners.
Definition: company_type.h:18
@ INVALID_COMPANY
An invalid company.
Definition: company_type.h:30
@ OWNER_DEITY
The object is owned by a superuser / goal script.
Definition: company_type.h:27
Base of all industries.
LeagueTable base class.
CommandCost CmdRemoveLeagueTableElement(DoCommandFlag flags, LeagueTableElementID element)
Remove a league table element.
Definition: league_cmd.cpp:164
std::tuple< CommandCost, LeagueTableID > CmdCreateLeagueTable(DoCommandFlag flags, const std::string &title, const std::string &header, const std::string &footer)
Create a new league table.
Definition: league_cmd.cpp:56
CommandCost CmdUpdateLeagueTableElementScore(DoCommandFlag flags, LeagueTableElementID element, int64_t rating, const std::string &score)
Update the score of a league table element.
Definition: league_cmd.cpp:144
bool IsValidLink(Link link)
Checks whether a link is valid, i.e.
Definition: league_cmd.cpp:34
std::tuple< CommandCost, LeagueTableElementID > CmdCreateLeagueTableElement(DoCommandFlag flags, LeagueTableID table, int64_t rating, CompanyID company, const std::string &text, const std::string &score, LinkType link_type, LinkTargetID link_target)
Create a new element in a league table.
Definition: league_cmd.cpp:86
CommandCost CmdUpdateLeagueTableElementData(DoCommandFlag flags, LeagueTableElementID element, CompanyID company, const std::string &text, LinkType link_type, LinkTargetID link_target)
Update the attributes of a league table element.
Definition: league_cmd.cpp:118
Command definitions related to league tables.
static const LeagueTableElementID INVALID_LEAGUE_TABLE_ELEMENT
Invalid/unknown index of LeagueTableElement.
Definition: league_type.h:38
uint16_t LeagueTableElementID
ID of a league table element.
Definition: league_type.h:36
uint8_t LeagueTableID
ID of a league table.
Definition: league_type.h:32
LinkType
Types of the possible link targets.
Definition: league_type.h:14
@ LT_COMPANY
Link a company.
Definition: league_type.h:19
@ LT_STORY_PAGE
Link a story page.
Definition: league_type.h:20
@ LT_TILE
Link a tile.
Definition: league_type.h:16
@ LT_NONE
No link.
Definition: league_type.h:15
@ LT_INDUSTRY
Link an industry.
Definition: league_type.h:17
@ LT_TOWN
Link a town.
Definition: league_type.h:18
uint32_t LinkTargetID
Contains either tile, industry ID, town ID, story page ID or company ID.
Definition: league_type.h:23
static const LeagueTableID INVALID_LEAGUE_TABLE
Invalid/unknown index of LeagueTable.
Definition: league_type.h:34
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.
Definition: pool_func.hpp:237
A number of safeguards to prevent using unsafe methods.
Definition of base types and functions in a cross-platform compatible way.
StoryPage base class.
Struct about league table elements.
Definition: league_base.h:31
std::string score
String representation of the score associated with the element.
Definition: league_base.h:36
std::string text
Text of the element.
Definition: league_base.h:35
int64_t rating
Value that determines ordering of elements in the table (higher=better)
Definition: league_base.h:33
Link link
What opens when element is clicked.
Definition: league_base.h:37
LeagueTableID table
Id of the table which this element belongs to.
Definition: league_base.h:32
CompanyID company
Company Id to show the color blob for or INVALID_COMPANY.
Definition: league_base.h:34
Struct about custom league tables.
Definition: league_base.h:52
std::string header
Text to show above the table.
Definition: league_base.h:54
std::string title
Title of the table.
Definition: league_base.h:53
std::string footer
Text to show below the table.
Definition: league_base.h:55
Tindex index
Index of this pool item.
Definition: pool_type.hpp:238
static bool IsValidID(size_t index)
Tests whether given index can be used to get valid (non-nullptr) Titem.
Definition: pool_type.hpp:328
static bool CanAllocateItem(size_t n=1)
Helper functions so we can use PoolItem::Function() instead of _poolitem_pool.Function()
Definition: pool_type.hpp:309
static Titem * GetIfValid(size_t index)
Returns Titem with given index.
Definition: pool_type.hpp:350
Base class for all pools.
Definition: pool_type.hpp:80
bool IsValidTile(Tile tile)
Checks if a tile is valid.
Definition: tile_map.h:161
Base of the town class.
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:3211
Window functions not directly related to making/drawing windows.
@ WC_COMPANY_LEAGUE
Company league window; Window numbers:
Definition: window_type.h:563