Files
@ r28822:57cd1bc354de
Branch filter:
Location: cpp/openttd-patchpack/source/src/goal.cpp
r28822:57cd1bc354de
9.3 KiB
text/x-c
Codechange: Add `GetVisibleRangeIterators()` to `Scrollbar`.
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 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 | /*
* 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 goal.cpp Handling of goals. */
#include "stdafx.h"
#include "company_func.h"
#include "industry.h"
#include "town.h"
#include "window_func.h"
#include "goal_base.h"
#include "core/pool_func.hpp"
#include "game/game.hpp"
#include "command_func.h"
#include "company_base.h"
#include "story_base.h"
#include "string_func.h"
#include "gui.h"
#include "network/network.h"
#include "network/network_base.h"
#include "network/network_func.h"
#include "goal_cmd.h"
#include "safeguards.h"
GoalPool _goal_pool("Goal");
INSTANTIATE_POOL_METHODS(Goal)
/* static */ bool Goal::IsValidGoalDestination(CompanyID company, GoalType type, GoalTypeID dest)
{
switch (type) {
case GT_NONE:
if (dest != 0) return false;
break;
case GT_TILE:
if (!IsValidTile(dest)) return false;
break;
case GT_INDUSTRY:
if (!Industry::IsValidID(dest)) return false;
break;
case GT_TOWN:
if (!Town::IsValidID(dest)) return false;
break;
case GT_COMPANY:
if (!Company::IsValidID(dest)) return false;
break;
case GT_STORY_PAGE: {
if (!StoryPage::IsValidID(dest)) return false;
CompanyID story_company = StoryPage::Get(dest)->company;
if (company == INVALID_COMPANY ? story_company != INVALID_COMPANY : story_company != INVALID_COMPANY && story_company != company) return false;
break;
}
default: return false;
}
return true;
}
/**
* Create a new goal.
* @param flags type of operation
* @param company Company for which this goal is.
* @param type GoalType of destination.
* @param dest GoalTypeID of destination.
* @param text Text of the goal.
* @return the cost of this operation or an error
*/
std::tuple<CommandCost, GoalID> CmdCreateGoal(DoCommandFlag flags, CompanyID company, GoalType type, GoalTypeID dest, const std::string &text)
{
if (!Goal::CanAllocateItem()) return { CMD_ERROR, INVALID_GOAL };
if (_current_company != OWNER_DEITY) return { CMD_ERROR, INVALID_GOAL };
if (text.empty()) return { CMD_ERROR, INVALID_GOAL };
if (company != INVALID_COMPANY && !Company::IsValidID(company)) return { CMD_ERROR, INVALID_GOAL };
if (!Goal::IsValidGoalDestination(company, type, dest)) return { CMD_ERROR, INVALID_GOAL };
if (flags & DC_EXEC) {
Goal *g = new Goal();
g->type = type;
g->dst = dest;
g->company = company;
g->text = text;
g->completed = false;
if (g->company == INVALID_COMPANY) {
InvalidateWindowClassesData(WC_GOALS_LIST);
} else {
InvalidateWindowData(WC_GOALS_LIST, g->company);
}
if (Goal::GetNumItems() == 1) InvalidateWindowData(WC_MAIN_TOOLBAR, 0);
return { CommandCost(), g->index };
}
return { CommandCost(), INVALID_GOAL };
}
/**
* Remove a goal.
* @param flags type of operation
* @param goal GoalID to remove.
* @return the cost of this operation or an error
*/
CommandCost CmdRemoveGoal(DoCommandFlag flags, GoalID goal)
{
if (_current_company != OWNER_DEITY) return CMD_ERROR;
if (!Goal::IsValidID(goal)) return CMD_ERROR;
if (flags & DC_EXEC) {
Goal *g = Goal::Get(goal);
CompanyID c = g->company;
delete g;
if (c == INVALID_COMPANY) {
InvalidateWindowClassesData(WC_GOALS_LIST);
} else {
InvalidateWindowData(WC_GOALS_LIST, c);
}
if (Goal::GetNumItems() == 0) InvalidateWindowData(WC_MAIN_TOOLBAR, 0);
}
return CommandCost();
}
/**
* Update goal destination of a goal.
* @param flags type of operation
* @param goal GoalID to update.
* @param type GoalType of destination.
* @param dest GoalTypeID of destination.
* @return the cost of this operation or an error
*/
CommandCost CmdSetGoalDestination(DoCommandFlag flags, GoalID goal, GoalType type, GoalTypeID dest)
{
if (_current_company != OWNER_DEITY) return CMD_ERROR;
if (!Goal::IsValidID(goal)) return CMD_ERROR;
Goal *g = Goal::Get(goal);
if (!Goal::IsValidGoalDestination(g->company, type, dest)) return CMD_ERROR;
if (flags & DC_EXEC) {
g->type = type;
g->dst = dest;
}
return CommandCost();
}
/**
* Update goal text of a goal.
* @param flags type of operation
* @param goal GoalID to update.
* @param text Text of the goal.
* @return the cost of this operation or an error
*/
CommandCost CmdSetGoalText(DoCommandFlag flags, GoalID goal, const std::string &text)
{
if (_current_company != OWNER_DEITY) return CMD_ERROR;
if (!Goal::IsValidID(goal)) return CMD_ERROR;
if (text.empty()) return CMD_ERROR;
if (flags & DC_EXEC) {
Goal *g = Goal::Get(goal);
g->text = text;
if (g->company == INVALID_COMPANY) {
InvalidateWindowClassesData(WC_GOALS_LIST);
} else {
InvalidateWindowData(WC_GOALS_LIST, g->company);
}
}
return CommandCost();
}
/**
* Update progress text of a goal.
* @param flags type of operation
* @param goal GoalID to update.
* @param text Progress text of the goal.
* @return the cost of this operation or an error
*/
CommandCost CmdSetGoalProgress(DoCommandFlag flags, GoalID goal, const std::string &text)
{
if (_current_company != OWNER_DEITY) return CMD_ERROR;
if (!Goal::IsValidID(goal)) return CMD_ERROR;
if (flags & DC_EXEC) {
Goal *g = Goal::Get(goal);
g->progress = text;
if (g->company == INVALID_COMPANY) {
InvalidateWindowClassesData(WC_GOALS_LIST);
} else {
InvalidateWindowData(WC_GOALS_LIST, g->company);
}
}
return CommandCost();
}
/**
* Update completed state of a goal.
* @param flags type of operation
* @param goal GoalID to update.
* @param completed completed state of goal.
* @return the cost of this operation or an error
*/
CommandCost CmdSetGoalCompleted(DoCommandFlag flags, GoalID goal, bool completed)
{
if (_current_company != OWNER_DEITY) return CMD_ERROR;
if (!Goal::IsValidID(goal)) return CMD_ERROR;
if (flags & DC_EXEC) {
Goal *g = Goal::Get(goal);
g->completed = completed;
if (g->company == INVALID_COMPANY) {
InvalidateWindowClassesData(WC_GOALS_LIST);
} else {
InvalidateWindowData(WC_GOALS_LIST, g->company);
}
}
return CommandCost();
}
/**
* Ask a goal related question
* @param flags type of operation
* @param uniqueid Unique ID to use for this question.
* @param target Company or client for which this question is.
* @param is_client Question target: false - company, true - client.
* @param button_mask Buttons of the question.
* @param type Question type.
* @param text Text of the question.
* @return the cost of this operation or an error
*/
CommandCost CmdGoalQuestion(DoCommandFlag flags, uint16_t uniqueid, uint32_t target, bool is_client, uint32_t button_mask, GoalQuestionType type, const std::string &text)
{
static_assert(sizeof(uint32_t) >= sizeof(CompanyID));
CompanyID company = (CompanyID)target;
static_assert(sizeof(uint32_t) >= sizeof(ClientID));
ClientID client = (ClientID)target;
static_assert(GOAL_QUESTION_BUTTON_COUNT < 29);
button_mask &= (1U << GOAL_QUESTION_BUTTON_COUNT) - 1;
if (_current_company != OWNER_DEITY) return CMD_ERROR;
if (text.empty()) return CMD_ERROR;
if (is_client) {
/* Only check during pre-flight; the client might have left between
* testing and executing. In that case it is fine to just ignore the
* fact the client is no longer here. */
if (!(flags & DC_EXEC) && _network_server && NetworkClientInfo::GetByClientID(client) == nullptr) return CMD_ERROR;
} else {
if (company != INVALID_COMPANY && !Company::IsValidID(company)) return CMD_ERROR;
}
uint min_buttons = (type == GQT_QUESTION ? 1 : 0);
if (CountBits(button_mask) < min_buttons || CountBits(button_mask) > 3) return CMD_ERROR;
if (type >= GQT_END) return CMD_ERROR;
if (flags & DC_EXEC) {
if (is_client) {
if (client != _network_own_client_id) return CommandCost();
} else {
if (company == INVALID_COMPANY && !Company::IsValidID(_local_company)) return CommandCost();
if (company != INVALID_COMPANY && company != _local_company) return CommandCost();
}
ShowGoalQuestion(uniqueid, type, button_mask, text);
}
return CommandCost();
}
/**
* Reply to a goal question.
* @param flags type of operation
* @param uniqueid Unique ID to use for this question.
* @param button Button the company pressed
* @return the cost of this operation or an error
*/
CommandCost CmdGoalQuestionAnswer(DoCommandFlag flags, uint16_t uniqueid, uint8_t button)
{
if (button >= GOAL_QUESTION_BUTTON_COUNT) return CMD_ERROR;
if (_current_company == OWNER_DEITY) {
/* It has been requested to close this specific question on all clients */
if (flags & DC_EXEC) CloseWindowById(WC_GOAL_QUESTION, uniqueid);
return CommandCost();
}
if (_networking && _local_company == _current_company) {
/* Somebody in the same company answered the question. Close the window */
if (flags & DC_EXEC) CloseWindowById(WC_GOAL_QUESTION, uniqueid);
if (!_network_server) return CommandCost();
}
if (flags & DC_EXEC) {
Game::NewEvent(new ScriptEventGoalQuestionAnswer(uniqueid, (ScriptCompany::CompanyID)(byte)_current_company, (ScriptGoal::QuestionButton)(1 << button)));
}
return CommandCost();
}
|