OpenTTD Source: src/league_cmd.cpp Source File

OpenTTD Source 15.0-RC2

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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
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
34bool 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
56std::tuple<CommandCost, LeagueTableID> CmdCreateLeagueTable(DoCommandFlags flags, const EncodedString &title, const EncodedString &header, const EncodedString &footer)
57{
58 if (_current_company != OWNER_DEITY) return { CMD_ERROR, LeagueTableID::Invalid() };
59 if (!LeagueTable::CanAllocateItem()) return { CMD_ERROR, LeagueTableID::Invalid() };
60 if (title.empty()) return { CMD_ERROR, LeagueTableID::Invalid() };
61
62 if (flags.Test(DoCommandFlag::Execute)) {
63 LeagueTable *lt = new LeagueTable(title, header, footer);
64 return { CommandCost(), lt->index };
65 }
66
67 return { CommandCost(), LeagueTableID::Invalid() };
68}
69
70
83std::tuple<CommandCost, LeagueTableElementID> CmdCreateLeagueTableElement(DoCommandFlags flags, LeagueTableID table, int64_t rating, CompanyID company, const EncodedString &text, const EncodedString &score, LinkType link_type, LinkTargetID link_target)
84{
85 if (_current_company != OWNER_DEITY) return { CMD_ERROR, LeagueTableElementID::Invalid() };
86 if (!LeagueTableElement::CanAllocateItem()) return { CMD_ERROR, LeagueTableElementID::Invalid() };
87 Link link{link_type, link_target};
88 if (!IsValidLink(link)) return { CMD_ERROR, LeagueTableElementID::Invalid() };
89 if (company != CompanyID::Invalid() && !Company::IsValidID(company)) return { CMD_ERROR, LeagueTableElementID::Invalid() };
90
91 if (flags.Test(DoCommandFlag::Execute)) {
92 LeagueTableElement *lte = new LeagueTableElement(table, rating, company, text, score, link);
93 InvalidateWindowData(WC_COMPANY_LEAGUE, table);
94 return { CommandCost(), lte->index };
95 }
96 return { CommandCost(), LeagueTableElementID::Invalid() };
97}
98
109 CommandCost CmdUpdateLeagueTableElementData(DoCommandFlags flags, LeagueTableElementID element, CompanyID company, const EncodedString &text, LinkType link_type, LinkTargetID link_target)
110{
111 if (_current_company != OWNER_DEITY) return CMD_ERROR;
112 auto lte = LeagueTableElement::GetIfValid(element);
113 if (lte == nullptr) return CMD_ERROR;
114 if (company != CompanyID::Invalid() && !Company::IsValidID(company)) return CMD_ERROR;
115 Link link{link_type, link_target};
116 if (!IsValidLink(link)) return CMD_ERROR;
117
118 if (flags.Test(DoCommandFlag::Execute)) {
119 lte->company = company;
120 lte->text = text;
121 lte->link = link;
122 InvalidateWindowData(WC_COMPANY_LEAGUE, lte->table);
123 }
124 return CommandCost();
125}
126
135 CommandCost CmdUpdateLeagueTableElementScore(DoCommandFlags flags, LeagueTableElementID element, int64_t rating, const EncodedString &score)
136{
137 if (_current_company != OWNER_DEITY) return CMD_ERROR;
138 auto lte = LeagueTableElement::GetIfValid(element);
139 if (lte == nullptr) return CMD_ERROR;
140
141 if (flags.Test(DoCommandFlag::Execute)) {
142 lte->rating = rating;
143 lte->score = score;
144 InvalidateWindowData(WC_COMPANY_LEAGUE, lte->table);
145 }
146 return CommandCost();
147}
148
155 CommandCost CmdRemoveLeagueTableElement(DoCommandFlags flags, LeagueTableElementID element)
156{
157 if (_current_company != OWNER_DEITY) return CMD_ERROR;
158 auto lte = LeagueTableElement::GetIfValid(element);
159 if (lte == nullptr) return CMD_ERROR;
160
161 if (flags.Test(DoCommandFlag::Execute)) {
162 auto table = lte->table;
163 delete lte;
164 InvalidateWindowData(WC_COMPANY_LEAGUE, table);
165 }
166 return CommandCost();
167}
BaseBitSet::Test
constexpr bool Test(Tvalue_type value) const
Test if the value-th bit is set.
Definition base_bitset_type.hpp:141
CommandCost
Common return value for all commands.
Definition command_type.h:24
EncodedString
Container for an encoded string, created by GetEncodedString.
Definition strings_type.h:98
EnumBitSet
Enum-as-bit-set wrapper.
Definition enum_type.hpp:177
command_func.h
Functions related to commands.
CMD_ERROR
static const CommandCost CMD_ERROR
Define a default return value for a failed command.
Definition command_func.h:28
command_type.h
Types related to commands.
DoCommandFlag::Execute
@ Execute
execute the given command
_current_company
CompanyID _current_company
Company currently doing an action.
Definition company_cmd.cpp:56
OWNER_DEITY
static constexpr Owner OWNER_DEITY
The object is owned by a superuser / goal script.
Definition company_type.h:28
industry.h
Base of all industries.
league_base.h
LeagueTable base class.
CmdCreateLeagueTableElement
std::tuple< CommandCost, LeagueTableElementID > CmdCreateLeagueTableElement(DoCommandFlags flags, LeagueTableID table, int64_t rating, CompanyID company, const EncodedString &text, const EncodedString &score, LinkType link_type, LinkTargetID link_target)
Create a new element in a league table.
Definition league_cmd.cpp:83
CmdUpdateLeagueTableElementData
CommandCost CmdUpdateLeagueTableElementData(DoCommandFlags flags, LeagueTableElementID element, CompanyID company, const EncodedString &text, LinkType link_type, LinkTargetID link_target)
Update the attributes of a league table element.
Definition league_cmd.cpp:109
IsValidLink
bool IsValidLink(Link link)
Checks whether a link is valid, i.e.
Definition league_cmd.cpp:34
CmdUpdateLeagueTableElementScore
CommandCost CmdUpdateLeagueTableElementScore(DoCommandFlags flags, LeagueTableElementID element, int64_t rating, const EncodedString &score)
Update the score of a league table element.
Definition league_cmd.cpp:135
CmdRemoveLeagueTableElement
CommandCost CmdRemoveLeagueTableElement(DoCommandFlags flags, LeagueTableElementID element)
Remove a league table element.
Definition league_cmd.cpp:155
CmdCreateLeagueTable
std::tuple< CommandCost, LeagueTableID > CmdCreateLeagueTable(DoCommandFlags flags, const EncodedString &title, const EncodedString &header, const EncodedString &footer)
Create a new league table.
Definition league_cmd.cpp:56
league_cmd.h
Command definitions related to league tables.
LinkType
LinkType
Types of the possible link targets.
Definition league_type.h:16
LT_COMPANY
@ LT_COMPANY
Link a company.
Definition league_type.h:21
LT_STORY_PAGE
@ LT_STORY_PAGE
Link a story page.
Definition league_type.h:22
LT_TILE
@ LT_TILE
Link a tile.
Definition league_type.h:18
LT_NONE
@ LT_NONE
No link.
Definition league_type.h:17
LT_INDUSTRY
@ LT_INDUSTRY
Link an industry.
Definition league_type.h:19
LT_TOWN
@ LT_TOWN
Link a town.
Definition league_type.h:20
LinkTargetID
uint32_t LinkTargetID
Contains either tile, industry ID, town ID, story page ID or company ID.
Definition league_type.h:25
pool_func.hpp
Some methods of Pool are placed here in order to reduce compilation time and binary size.
INSTANTIATE_POOL_METHODS
#define INSTANTIATE_POOL_METHODS(name)
Force instantiation of pool methods so we don't get linker errors.
Definition pool_func.hpp:210
safeguards.h
A number of safeguards to prevent using unsafe methods.
stdafx.h
Definition of base types and functions in a cross-platform compatible way.
story_base.h
StoryPage base class.
LeagueTableElement
Struct about league table elements.
Definition league_base.h:32
LeagueTable
Struct about custom league tables.
Definition league_base.h:55
Link
Definition league_type.h:27
PoolID< uint8_t, struct LeagueTableIDTag, 255, 0xFF >
Pool::PoolItem::index
Tindex index
Index of this pool item.
Definition pool_type.hpp:286
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:357
Pool::PoolItem<&_industry_pool >::IsValidID
static bool IsValidID(auto index)
Tests whether given index can be used to get valid (non-nullptr) Titem.
Definition pool_type.hpp:376
Pool::PoolItem<&_league_table_element_pool >::GetIfValid
static Titem * GetIfValid(auto index)
Returns Titem with given index.
Definition pool_type.hpp:398
Pool
Base class for all pools.
Definition pool_type.hpp:133
IsValidTile
bool IsValidTile(Tile tile)
Checks if a tile is valid.
Definition tile_map.h:161
town.h
Base of the town class.
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:3294
window_func.h
Window functions not directly related to making/drawing windows.
WC_COMPANY_LEAGUE
@ WC_COMPANY_LEAGUE
Company league window; Window numbers:
Definition window_type.h:568