OpenTTD Source 20260721-master-g25ec12c62d
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
9
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 "company_base.h"
16#include "industry.h"
17#include "story_base.h"
18#include "town.h"
19#include "window_func.h"
20#include "core/pool_func.hpp"
21
22#include "safeguards.h"
23
24LeagueTableElementPool _league_table_element_pool("LeagueTableElement");
26
27LeagueTablePool _league_table_pool("LeagueTable");
29
30
35bool IsValidLink(Link link)
36{
37 switch (link.type) {
38 case LinkType::None: return (link.target == 0);
39 case LinkType::Tile: return IsValidTile(link.target);
40 case LinkType::Industry: return Industry::IsValidID(link.target);
41 case LinkType::Town: return Town::IsValidID(link.target);
42 case LinkType::Company: return Company::IsValidID(link.target);
43 case LinkType::StoryPage: return StoryPage::IsValidID(link.target);
44 default: return false;
45 }
46 return false;
47}
48
57std::tuple<CommandCost, LeagueTableID> CmdCreateLeagueTable(DoCommandFlags flags, const EncodedString &title, const EncodedString &header, const EncodedString &footer)
58{
59 if (_current_company != OWNER_DEITY) return { CMD_ERROR, LeagueTableID::Invalid() };
60 if (!LeagueTable::CanAllocateItem()) return { CMD_ERROR, LeagueTableID::Invalid() };
61 if (title.empty()) return { CMD_ERROR, LeagueTableID::Invalid() };
62
63 if (flags.Test(DoCommandFlag::Execute)) {
64 LeagueTable *lt = LeagueTable::Create(title, header, footer);
65 return { CommandCost(), lt->index };
66 }
67
68 return { CommandCost(), LeagueTableID::Invalid() };
69}
70
71
84std::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)
85{
86 if (_current_company != OWNER_DEITY) return { CMD_ERROR, LeagueTableElementID::Invalid() };
87 if (!LeagueTableElement::CanAllocateItem()) return { CMD_ERROR, LeagueTableElementID::Invalid() };
88 Link link{link_type, link_target};
89 if (!IsValidLink(link)) return { CMD_ERROR, LeagueTableElementID::Invalid() };
90 if (company != CompanyID::Invalid() && !Company::IsValidID(company)) return { CMD_ERROR, LeagueTableElementID::Invalid() };
91
92 if (flags.Test(DoCommandFlag::Execute)) {
93 LeagueTableElement *lte = LeagueTableElement::Create(table, rating, company, text, score, link);
94 InvalidateWindowData(WindowClass::CompanyLeague, table);
95 return { CommandCost(), lte->index };
96 }
97 return { CommandCost(), LeagueTableElementID::Invalid() };
98}
99
110CommandCost CmdUpdateLeagueTableElementData(DoCommandFlags flags, LeagueTableElementID element, CompanyID company, const EncodedString &text, LinkType link_type, LinkTargetID link_target)
111{
113 auto lte = LeagueTableElement::GetIfValid(element);
114 if (lte == nullptr) return CMD_ERROR;
115 if (company != CompanyID::Invalid() && !Company::IsValidID(company)) return CMD_ERROR;
116 Link link{link_type, link_target};
117 if (!IsValidLink(link)) return CMD_ERROR;
118
119 if (flags.Test(DoCommandFlag::Execute)) {
120 lte->company = company;
121 lte->text = text;
122 lte->link = link;
123 InvalidateWindowData(WindowClass::CompanyLeague, lte->table);
124 }
125 return CommandCost();
126}
127
137{
139 auto lte = LeagueTableElement::GetIfValid(element);
140 if (lte == nullptr) return CMD_ERROR;
141
142 if (flags.Test(DoCommandFlag::Execute)) {
143 lte->rating = rating;
144 lte->score = score;
145 InvalidateWindowData(WindowClass::CompanyLeague, lte->table);
146 }
147 return CommandCost();
148}
149
157{
159 auto lte = LeagueTableElement::GetIfValid(element);
160 if (lte == nullptr) return CMD_ERROR;
161
162 if (flags.Test(DoCommandFlag::Execute)) {
163 auto table = lte->table;
164 delete lte;
165 InvalidateWindowData(WindowClass::CompanyLeague, table);
166 }
167 return CommandCost();
168}
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.
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
EnumBitSet< DoCommandFlag, uint16_t > DoCommandFlags
Bitset of DoCommandFlag elements.
Definition of stuff that is very close to a company, like the company struct itself.
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.
bool IsValidLink(Link link)
Checks whether a link is valid, i.e.
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
uint32_t LinkTargetID
Contains either tile, industry ID, town ID, story page ID or company ID.
Definition league_type.h:25
PoolID< uint16_t, struct LeagueTableElementIDTag, 64000, 0xFFFF > LeagueTableElementID
ID of a league table.
Definition league_type.h:38
PoolID< uint8_t, struct LeagueTableIDTag, 255, 0xFF > LeagueTableID
ID of a league table.
Definition league_type.h:35
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:52
static LeagueTableElement * GetIfValid(auto index)
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:3315
Window functions not directly related to making/drawing windows.