Files
@ r28541:42e13f513738
Branch filter:
Location: cpp/openttd-patchpack/source/src/league_cmd.cpp
r28541:42e13f513738
6.4 KiB
text/x-c
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 | /*
* This file is part of OpenTTD.
* 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.
* 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.
* 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/>.
*/
/** @file league_cmd.cpp Handling of league tables. */
#include "stdafx.h"
#include "league_cmd.h"
#include "league_base.h"
#include "command_type.h"
#include "command_func.h"
#include "industry.h"
#include "story_base.h"
#include "town.h"
#include "window_func.h"
#include "core/pool_func.hpp"
#include "safeguards.h"
LeagueTableElementPool _league_table_element_pool("LeagueTableElement");
INSTANTIATE_POOL_METHODS(LeagueTableElement)
LeagueTablePool _league_table_pool("LeagueTable");
INSTANTIATE_POOL_METHODS(LeagueTable)
/**
* Checks whether a link is valid, i.e. has a valid target.
* @param link the link to check
* @return true iff the link is valid
*/
bool IsValidLink(Link link)
{
switch (link.type) {
case LT_NONE: return (link.target == 0);
case LT_TILE: return IsValidTile(link.target);
case LT_INDUSTRY: return Industry::IsValidID(link.target);
case LT_TOWN: return Town::IsValidID(link.target);
case LT_COMPANY: return Company::IsValidID(link.target);
case LT_STORY_PAGE: return StoryPage::IsValidID(link.target);
default: return false;
}
return false;
}
/**
* Create a new league table.
* @param flags type of operation
* @param title Title of the league table
* @param header Text to show above the table
* @param footer Text to show below the table
* @return the cost of this operation or an error
*/
std::tuple<CommandCost, LeagueTableID> CmdCreateLeagueTable(DoCommandFlag flags, const std::string &title, const std::string &header, const std::string &footer)
{
if (_current_company != OWNER_DEITY) return { CMD_ERROR, INVALID_LEAGUE_TABLE };
if (!LeagueTable::CanAllocateItem()) return { CMD_ERROR, INVALID_LEAGUE_TABLE };
if (title.empty()) return { CMD_ERROR, INVALID_LEAGUE_TABLE };
if (flags & DC_EXEC) {
LeagueTable *lt = new LeagueTable();
lt->title = title;
lt->header = header;
lt->footer = footer;
return { CommandCost(), lt->index };
}
return { CommandCost(), INVALID_LEAGUE_TABLE };
}
/**
* Create a new element in a league table.
* @param flags type of operation
* @param table Id of the league table this element belongs to
* @param rating Value that elements are ordered by
* @param company Company to show the color blob for or INVALID_COMPANY
* @param text Text of the element
* @param score String representation of the score associated with the element
* @param link_type Type of the referenced object
* @param link_target Id of the referenced object
* @return the cost of this operation or an error
*/
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)
{
if (_current_company != OWNER_DEITY) return { CMD_ERROR, INVALID_LEAGUE_TABLE_ELEMENT };
if (!LeagueTableElement::CanAllocateItem()) return { CMD_ERROR, INVALID_LEAGUE_TABLE_ELEMENT };
Link link{link_type, link_target};
if (!IsValidLink(link)) return { CMD_ERROR, INVALID_LEAGUE_TABLE_ELEMENT };
if (company != INVALID_COMPANY && !Company::IsValidID(company)) return { CMD_ERROR, INVALID_LEAGUE_TABLE_ELEMENT };
if (flags & DC_EXEC) {
LeagueTableElement *lte = new LeagueTableElement();
lte->table = table;
lte->rating = rating;
lte->company = company;
lte->text = text;
lte->score = score;
lte->link = link;
InvalidateWindowData(WC_COMPANY_LEAGUE, table);
return { CommandCost(), lte->index };
}
return { CommandCost(), INVALID_LEAGUE_TABLE_ELEMENT };
}
/**
* Update the attributes of a league table element.
* @param flags type of operation
* @param element Id of the element to update
* @param company Company to show the color blob for or INVALID_COMPANY
* @param text Text of the element
* @param link_type Type of the referenced object
* @param link_target Id of the referenced object
* @return the cost of this operation or an error
*/
CommandCost CmdUpdateLeagueTableElementData(DoCommandFlag flags, LeagueTableElementID element, CompanyID company, const std::string &text, LinkType link_type, LinkTargetID link_target)
{
if (_current_company != OWNER_DEITY) return CMD_ERROR;
auto lte = LeagueTableElement::GetIfValid(element);
if (lte == nullptr) return CMD_ERROR;
if (company != INVALID_COMPANY && !Company::IsValidID(company)) return CMD_ERROR;
Link link{link_type, link_target};
if (!IsValidLink(link)) return CMD_ERROR;
if (flags & DC_EXEC) {
lte->company = company;
lte->text = text;
lte->link = link;
InvalidateWindowData(WC_COMPANY_LEAGUE, lte->table);
}
return CommandCost();
}
/**
* Update the score of a league table element.
* @param flags type of operation
* @param element Id of the element to update
* @param rating Value that elements are ordered by
* @param score String representation of the score associated with the element
* @return the cost of this operation or an error
*/
CommandCost CmdUpdateLeagueTableElementScore(DoCommandFlag flags, LeagueTableElementID element, int64_t rating, const std::string &score)
{
if (_current_company != OWNER_DEITY) return CMD_ERROR;
auto lte = LeagueTableElement::GetIfValid(element);
if (lte == nullptr) return CMD_ERROR;
if (flags & DC_EXEC) {
lte->rating = rating;
lte->score = score;
InvalidateWindowData(WC_COMPANY_LEAGUE, lte->table);
}
return CommandCost();
}
/**
* Remove a league table element.
* @param flags type of operation
* @param element Id of the element to update
* @return the cost of this operation or an error
*/
CommandCost CmdRemoveLeagueTableElement(DoCommandFlag flags, LeagueTableElementID element)
{
if (_current_company != OWNER_DEITY) return CMD_ERROR;
auto lte = LeagueTableElement::GetIfValid(element);
if (lte == nullptr) return CMD_ERROR;
if (flags & DC_EXEC) {
auto table = lte->table;
delete lte;
InvalidateWindowData(WC_COMPANY_LEAGUE, table);
}
return CommandCost();
}
|