OpenTTD Source 20250312-master-gcdcc6b491d
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
23LeagueTableElementPool _league_table_element_pool("LeagueTableElement");
25
26LeagueTablePool _league_table_pool("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);
94 return { CommandCost(), lte->index };
95 }
96 return { CommandCost(), LeagueTableElementID::Invalid() };
97}
98
110{
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;
123 }
124 return CommandCost();
125}
126
136{
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;
145 }
146 return CommandCost();
147}
148
156{
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;
165 }
166 return CommandCost();
167}
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.
Functions related to commands.
static const CommandCost CMD_ERROR
Define a default return value for a failed command.
Types related to commands.
@ Execute
execute the given command
CompanyID _current_company
Company currently doing an action.
static constexpr Owner OWNER_DEITY
The object is owned by a superuser / goal script.
Base of all industries.
LeagueTable base class.
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.
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.
bool IsValidLink(Link link)
Checks whether a link is valid, i.e.
CommandCost CmdUpdateLeagueTableElementScore(DoCommandFlags flags, LeagueTableElementID element, int64_t rating, const EncodedString &score)
Update the score of a league table element.
CommandCost CmdRemoveLeagueTableElement(DoCommandFlags flags, LeagueTableElementID element)
Remove a league table element.
std::tuple< CommandCost, LeagueTableID > CmdCreateLeagueTable(DoCommandFlags flags, const EncodedString &title, const EncodedString &header, const EncodedString &footer)
Create a new league table.
Command definitions related to league tables.
LinkType
Types of the possible link targets.
Definition league_type.h:16
@ LT_COMPANY
Link a company.
Definition league_type.h:21
@ LT_STORY_PAGE
Link a story page.
Definition league_type.h:22
@ LT_TILE
Link a tile.
Definition league_type.h:18
@ LT_NONE
No link.
Definition league_type.h:17
@ LT_INDUSTRY
Link an industry.
Definition league_type.h:19
@ LT_TOWN
Link a town.
Definition league_type.h:20
uint32_t LinkTargetID
Contains either tile, industry ID, town ID, story page ID or company ID.
Definition league_type.h:25
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.
StoryPage base class.
Struct about league table elements.
Definition league_base.h:32
Struct about custom league tables.
Definition league_base.h:55
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 Titem * GetIfValid(auto index)
Returns Titem with given index.
Base class for all pools.
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:3224
Window functions not directly related to making/drawing windows.
@ WC_COMPANY_LEAGUE
Company league window; Window numbers: