OpenTTD Source: src/league_cmd.cpp Source File

OpenTTD Source 13.1

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");
24 INSTANTIATE_POOL_METHODS(LeagueTableElement)
25
26 LeagueTablePool _league_table_pool("LeagueTable");
27 INSTANTIATE_POOL_METHODS(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 {
58 if (_current_company != OWNER_DEITY) return { CMD_ERROR, INVALID_LEAGUE_TABLE };
59 if (!LeagueTable::CanAllocateItem()) return { CMD_ERROR, INVALID_LEAGUE_TABLE };
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 rating, CompanyID company, const std::string &text, const std::string &score, LinkType link_type, LinkTargetID link_target)
87 {
88 if (_current_company != OWNER_DEITY) return { CMD_ERROR, INVALID_LEAGUE_TABLE_ELEMENT };
89 if (!LeagueTableElement::CanAllocateItem()) return { CMD_ERROR, INVALID_LEAGUE_TABLE_ELEMENT };
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) {
95 LeagueTableElement *lte = new LeagueTableElement();
96 lte->table = table;
97 lte->rating = rating;
98 lte->company = company;
99 lte->text = text;
100 lte->score = score;
101 lte->link = link;
102 InvalidateWindowData(WC_COMPANY_LEAGUE, table);
103 return { CommandCost(), lte->index };
104 }
105 return { CommandCost(), INVALID_LEAGUE_TABLE_ELEMENT };
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;
131 InvalidateWindowData(WC_COMPANY_LEAGUE, lte->table);
132 }
133 return CommandCost();
134 }
135
144 CommandCost CmdUpdateLeagueTableElementScore(DoCommandFlag flags, LeagueTableElementID element, int64 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;
153 InvalidateWindowData(WC_COMPANY_LEAGUE, lte->table);
154 }
155 return CommandCost();
156 }
157
164 CommandCost CmdRemoveLeagueTableElement(DoCommandFlag flags, LeagueTableElementID element)
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;
173 InvalidateWindowData(WC_COMPANY_LEAGUE, table);
174 }
175 return CommandCost();
176 }
CmdCreateLeagueTable
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
InvalidateWindowData
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:3252
LeagueTable::title
std::string title
Title of the table.
Definition: league_base.h:53
INVALID_LEAGUE_TABLE
static const LeagueTableID INVALID_LEAGUE_TABLE
Invalid/unknown index of LeagueTable.
Definition: league_type.h:34
LeagueTableElement
Struct about league table elements.
Definition: league_base.h:31
command_func.h
Pool::PoolItem<&_league_table_element_pool >::GetIfValid
static Titem * GetIfValid(size_t index)
Returns Titem with given index.
Definition: pool_type.hpp:348
CMD_ERROR
static const CommandCost CMD_ERROR
Define a default return value for a failed command.
Definition: command_func.h:28
Pool::PoolItem::index
Tindex index
Index of this pool item.
Definition: pool_type.hpp:235
LeagueTable
Struct about custom league tables.
Definition: league_base.h:52
LeagueTable::header
std::string header
Text to show above the table.
Definition: league_base.h:54
town.h
Owner
Owner
Enum for all companies/owners.
Definition: company_type.h:18
DC_EXEC
@ DC_EXEC
execute the given command
Definition: command_type.h:357
WC_COMPANY_LEAGUE
@ WC_COMPANY_LEAGUE
Company league window; Window numbers:
Definition: window_type.h:551
DoCommandFlag
DoCommandFlag
List of flags for a command.
Definition: command_type.h:355
CmdUpdateLeagueTableElementData
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
LT_INDUSTRY
@ LT_INDUSTRY
Link an industry.
Definition: league_type.h:17
LT_NONE
@ LT_NONE
No link.
Definition: league_type.h:15
LinkTargetID
uint32 LinkTargetID
Contains either tile, industry ID, town ID, story page ID or company ID.
Definition: league_type.h:23
LinkType
LinkType
Types of the possible link targets.
Definition: league_type.h:14
LT_STORY_PAGE
@ LT_STORY_PAGE
Link a story page.
Definition: league_type.h:20
CommandCost
Common return value for all commands.
Definition: command_type.h:24
industry.h
safeguards.h
IsValidTile
static bool IsValidTile(TileIndex tile)
Checks if a tile is valid.
Definition: tile_map.h:161
IsValidLink
bool IsValidLink(Link link)
Checks whether a link is valid, i.e.
Definition: league_cmd.cpp:34
LeagueTableID
uint8 LeagueTableID
ID of a league table.
Definition: league_type.h:32
league_base.h
LT_TILE
@ LT_TILE
Link a tile.
Definition: league_type.h:16
stdafx.h
command_type.h
LeagueTableElement::link
Link link
What opens when element is clicked.
Definition: league_base.h:37
LeagueTableElement::company
CompanyID company
Company Id to show the color blob for or INVALID_COMPANY.
Definition: league_base.h:34
LT_TOWN
@ LT_TOWN
Link a town.
Definition: league_type.h:18
_current_company
CompanyID _current_company
Company currently doing an action.
Definition: company_cmd.cpp:47
Pool
Base class for all pools.
Definition: pool_type.hpp:81
LeagueTableElement::rating
int64 rating
Value that determines ordering of elements in the table (higher=better)
Definition: league_base.h:33
LeagueTableElementID
uint16 LeagueTableElementID
ID of a league table element.
Definition: league_type.h:36
LeagueTableElement::score
std::string score
String representation of the score associated with the element.
Definition: league_base.h:36
LeagueTable::footer
std::string footer
Text to show below the table.
Definition: league_base.h:55
Pool::PoolItem<&_league_table_pool >::CanAllocateItem
static bool CanAllocateItem(size_t n=1)
Helper functions so we can use PoolItem::Function() instead of _poolitem_pool.Function()
Definition: pool_type.hpp:307
OWNER_DEITY
@ OWNER_DEITY
The object is owned by a superuser / goal script.
Definition: company_type.h:27
INVALID_LEAGUE_TABLE_ELEMENT
static const LeagueTableElementID INVALID_LEAGUE_TABLE_ELEMENT
Invalid/unknown index of LeagueTableElement.
Definition: league_type.h:38
INSTANTIATE_POOL_METHODS
#define INSTANTIATE_POOL_METHODS(name)
Force instantiation of pool methods so we don't get linker errors.
Definition: pool_func.hpp:224
CmdCreateLeagueTableElement
std::tuple< CommandCost, LeagueTableElementID > CmdCreateLeagueTableElement(DoCommandFlag flags, LeagueTableID table, int64 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
league_cmd.h
window_func.h
CmdRemoveLeagueTableElement
CommandCost CmdRemoveLeagueTableElement(DoCommandFlag flags, LeagueTableElementID element)
Remove a league table element.
Definition: league_cmd.cpp:164
LeagueTableElement::text
std::string text
Text of the element.
Definition: league_base.h:35
INVALID_COMPANY
@ INVALID_COMPANY
An invalid company.
Definition: company_type.h:30
Link
Definition: league_type.h:25
Pool::PoolItem<&_industry_pool >::IsValidID
static bool IsValidID(size_t index)
Tests whether given index can be used to get valid (non-nullptr) Titem.
Definition: pool_type.hpp:326
pool_func.hpp
story_base.h
LT_COMPANY
@ LT_COMPANY
Link a company.
Definition: league_type.h:19
CmdUpdateLeagueTableElementScore
CommandCost CmdUpdateLeagueTableElementScore(DoCommandFlag flags, LeagueTableElementID element, int64 rating, const std::string &score)
Update the score of a league table element.
Definition: league_cmd.cpp:144
LeagueTableElement::table
LeagueTableID table
Id of the table which this element belongs to.
Definition: league_base.h:32