Files
@ r14856:49dbd79b7511
Branch filter:
Location: cpp/openttd-patchpack/source/src/group_cmd.cpp - annotation
r14856:49dbd79b7511
12.2 KiB
text/x-c
(svn r19462) -Codechange: use the AS_GENERIC macro also for the dummy airport
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 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 | r6643:fa000ce7441e r6643:fa000ce7441e r12768:980ae0491352 r12768:980ae0491352 r12768:980ae0491352 r12768:980ae0491352 r12768:980ae0491352 r12768:980ae0491352 r12768:980ae0491352 r6643:fa000ce7441e r6643:fa000ce7441e r6643:fa000ce7441e r8116:df67d3c5e4fd r6643:fa000ce7441e r6643:fa000ce7441e r10960:e97ebf9cf99b r6667:b4b3f745e39c r8131:7a50db7be0ff r8144:1432edd15267 r8212:13ec7a6b1407 r8212:13ec7a6b1407 r8214:9a3935f9ef4e r10208:ef8fcc3dc4ca r11967:df0600d2c7e7 r6643:fa000ce7441e r8264:d493cb51fe8a r8264:d493cb51fe8a r10696:8dfe83e30d01 r10696:8dfe83e30d01 r11967:df0600d2c7e7 r11967:df0600d2c7e7 r11967:df0600d2c7e7 r6643:fa000ce7441e r6643:fa000ce7441e r6643:fa000ce7441e r6643:fa000ce7441e r6643:fa000ce7441e r6643:fa000ce7441e r6643:fa000ce7441e r6643:fa000ce7441e r6643:fa000ce7441e r6643:fa000ce7441e r6643:fa000ce7441e r11919:363b629324b9 r6643:fa000ce7441e r8939:7f7d26c313bf r11919:363b629324b9 r6643:fa000ce7441e r6643:fa000ce7441e r6643:fa000ce7441e r6643:fa000ce7441e r7382:750cdd87ddef r10207:a1fc2f2a33db r6643:fa000ce7441e r8253:2016acbc61ce r9700:d86fb8ec80d5 r11967:df0600d2c7e7 r11967:df0600d2c7e7 r11967:df0600d2c7e7 r6643:fa000ce7441e r6643:fa000ce7441e r7382:750cdd87ddef r6643:fa000ce7441e r8258:08100da56269 r9070:e059c65164f3 r7382:750cdd87ddef r6643:fa000ce7441e r11962:0e3a6981e508 r6643:fa000ce7441e r11967:df0600d2c7e7 r6643:fa000ce7441e r6643:fa000ce7441e r6643:fa000ce7441e r6643:fa000ce7441e r7197:b688b7300a65 r6643:fa000ce7441e r13057:58af81fcdcf8 r6643:fa000ce7441e r6643:fa000ce7441e r13057:58af81fcdcf8 r13057:58af81fcdcf8 r6643:fa000ce7441e r11090:9276cea703d4 r6643:fa000ce7441e r6643:fa000ce7441e r10207:a1fc2f2a33db r6643:fa000ce7441e r9036:e33a0264e0c3 r7382:750cdd87ddef r6643:fa000ce7441e r10207:a1fc2f2a33db r6643:fa000ce7441e r6643:fa000ce7441e r6667:b4b3f745e39c r10696:8dfe83e30d01 r10696:8dfe83e30d01 r10207:a1fc2f2a33db r6643:fa000ce7441e r6643:fa000ce7441e r6950:1a54b1afb12a r6643:fa000ce7441e r6643:fa000ce7441e r6643:fa000ce7441e r6643:fa000ce7441e r7197:b688b7300a65 r6643:fa000ce7441e r13057:58af81fcdcf8 r6643:fa000ce7441e r6643:fa000ce7441e r6643:fa000ce7441e r13057:58af81fcdcf8 r13057:58af81fcdcf8 r6643:fa000ce7441e r11090:9276cea703d4 r6643:fa000ce7441e r11944:3b8f9f948e8d r11944:3b8f9f948e8d r6643:fa000ce7441e r6643:fa000ce7441e r6643:fa000ce7441e r6643:fa000ce7441e r6643:fa000ce7441e r6643:fa000ce7441e r6643:fa000ce7441e r6643:fa000ce7441e r6643:fa000ce7441e r8650:a2162c17e10b r8650:a2162c17e10b r8650:a2162c17e10b r6643:fa000ce7441e r10207:a1fc2f2a33db r10207:a1fc2f2a33db r6643:fa000ce7441e r6643:fa000ce7441e r11917:612c11f7ab47 r6643:fa000ce7441e r10207:a1fc2f2a33db r6643:fa000ce7441e r6643:fa000ce7441e r6643:fa000ce7441e r6667:b4b3f745e39c r6667:b4b3f745e39c r6643:fa000ce7441e r6643:fa000ce7441e r7382:750cdd87ddef r6667:b4b3f745e39c r10207:a1fc2f2a33db r6643:fa000ce7441e r6643:fa000ce7441e r6950:1a54b1afb12a r6643:fa000ce7441e r6643:fa000ce7441e r7097:1aa026536399 r7097:1aa026536399 r7097:1aa026536399 r7097:1aa026536399 r7097:1aa026536399 r10656:1e2ea0b81776 r7097:1aa026536399 r7097:1aa026536399 r7097:1aa026536399 r7097:1aa026536399 r6643:fa000ce7441e r6643:fa000ce7441e r6643:fa000ce7441e r6643:fa000ce7441e r13057:58af81fcdcf8 r6643:fa000ce7441e r6643:fa000ce7441e r6643:fa000ce7441e r13057:58af81fcdcf8 r13057:58af81fcdcf8 r6643:fa000ce7441e r11090:9276cea703d4 r6643:fa000ce7441e r11944:3b8f9f948e8d r11944:3b8f9f948e8d r6667:b4b3f745e39c r10499:570896340d7a r10148:ec65442a5187 r10148:ec65442a5187 r10499:570896340d7a r12622:202e83a6cee7 r10148:ec65442a5187 r7097:1aa026536399 r6643:fa000ce7441e r6643:fa000ce7441e r8258:08100da56269 r6643:fa000ce7441e r10499:570896340d7a r6667:b4b3f745e39c r10207:a1fc2f2a33db r6643:fa000ce7441e r6643:fa000ce7441e r6950:1a54b1afb12a r6643:fa000ce7441e r6643:fa000ce7441e r6643:fa000ce7441e r6643:fa000ce7441e r6643:fa000ce7441e r6643:fa000ce7441e r13057:58af81fcdcf8 r6643:fa000ce7441e r6643:fa000ce7441e r6643:fa000ce7441e r6643:fa000ce7441e r13057:58af81fcdcf8 r13057:58af81fcdcf8 r6643:fa000ce7441e r11090:9276cea703d4 r6643:fa000ce7441e r11944:3b8f9f948e8d r6643:fa000ce7441e r6643:fa000ce7441e r11944:3b8f9f948e8d r7504:5cc8b8f95e1d r11919:363b629324b9 r11917:612c11f7ab47 r10207:a1fc2f2a33db r6671:62a3a94a8752 r6667:b4b3f745e39c r10207:a1fc2f2a33db r6643:fa000ce7441e r6643:fa000ce7441e r6643:fa000ce7441e r6643:fa000ce7441e r6643:fa000ce7441e r6643:fa000ce7441e r6643:fa000ce7441e r6643:fa000ce7441e r12290:442d5d533a57 r6643:fa000ce7441e r6643:fa000ce7441e r6643:fa000ce7441e r6643:fa000ce7441e r12373:30da669f3dfa r6643:fa000ce7441e r6643:fa000ce7441e r6643:fa000ce7441e r6643:fa000ce7441e r6643:fa000ce7441e r13024:48c81d0b078a r10207:a1fc2f2a33db r6643:fa000ce7441e r6643:fa000ce7441e r6950:1a54b1afb12a r6643:fa000ce7441e r6643:fa000ce7441e r6643:fa000ce7441e r6643:fa000ce7441e r6643:fa000ce7441e r13057:58af81fcdcf8 r6643:fa000ce7441e r6643:fa000ce7441e r6643:fa000ce7441e r13057:58af81fcdcf8 r13057:58af81fcdcf8 r6643:fa000ce7441e r11090:9276cea703d4 r6643:fa000ce7441e r6643:fa000ce7441e r11919:363b629324b9 r6643:fa000ce7441e r6643:fa000ce7441e r6643:fa000ce7441e r6643:fa000ce7441e r6643:fa000ce7441e r6643:fa000ce7441e r6643:fa000ce7441e r6643:fa000ce7441e r6643:fa000ce7441e r6773:93083dcf60ec r6643:fa000ce7441e r6643:fa000ce7441e r6643:fa000ce7441e r9941:945cc0c6ccbc r14823:60c9b3978fe5 r6643:fa000ce7441e r6643:fa000ce7441e r6643:fa000ce7441e r6667:b4b3f745e39c r10207:a1fc2f2a33db r6643:fa000ce7441e r6643:fa000ce7441e r6950:1a54b1afb12a r6643:fa000ce7441e r6643:fa000ce7441e r6643:fa000ce7441e r6643:fa000ce7441e r6643:fa000ce7441e r6643:fa000ce7441e r13057:58af81fcdcf8 r6643:fa000ce7441e r6643:fa000ce7441e r6643:fa000ce7441e r13057:58af81fcdcf8 r13057:58af81fcdcf8 r6643:fa000ce7441e r11090:9276cea703d4 r6643:fa000ce7441e r11944:3b8f9f948e8d r6643:fa000ce7441e r6643:fa000ce7441e r11944:3b8f9f948e8d r6667:b4b3f745e39c r6643:fa000ce7441e r6643:fa000ce7441e r6643:fa000ce7441e r6643:fa000ce7441e r6643:fa000ce7441e r6643:fa000ce7441e r6773:93083dcf60ec r6643:fa000ce7441e r6643:fa000ce7441e r6643:fa000ce7441e r14823:60c9b3978fe5 r6643:fa000ce7441e r6643:fa000ce7441e r6667:b4b3f745e39c r10207:a1fc2f2a33db r6667:b4b3f745e39c r6667:b4b3f745e39c r6950:1a54b1afb12a r6667:b4b3f745e39c r6667:b4b3f745e39c r6667:b4b3f745e39c r6667:b4b3f745e39c r6667:b4b3f745e39c r6667:b4b3f745e39c r13057:58af81fcdcf8 r6667:b4b3f745e39c r6667:b4b3f745e39c r6667:b4b3f745e39c r6667:b4b3f745e39c r13057:58af81fcdcf8 r13057:58af81fcdcf8 r6667:b4b3f745e39c r11090:9276cea703d4 r6667:b4b3f745e39c r11944:3b8f9f948e8d r11944:3b8f9f948e8d r6667:b4b3f745e39c r6667:b4b3f745e39c r7928:a80e7e05d6c5 r6667:b4b3f745e39c r10207:a1fc2f2a33db r10392:1272a9df6a2f r6643:fa000ce7441e r6643:fa000ce7441e r6950:1a54b1afb12a r6643:fa000ce7441e r6643:fa000ce7441e r6643:fa000ce7441e r6643:fa000ce7441e r6643:fa000ce7441e r6643:fa000ce7441e r6643:fa000ce7441e r6643:fa000ce7441e r6643:fa000ce7441e r11967:df0600d2c7e7 r6643:fa000ce7441e r6643:fa000ce7441e r6643:fa000ce7441e r6643:fa000ce7441e r6643:fa000ce7441e r6643:fa000ce7441e r6643:fa000ce7441e r6643:fa000ce7441e r6643:fa000ce7441e r6643:fa000ce7441e r6643:fa000ce7441e r12290:442d5d533a57 r6643:fa000ce7441e r11919:363b629324b9 r6643:fa000ce7441e r13948:ab833d93e755 r6643:fa000ce7441e r7492:75510449064b r12373:30da669f3dfa r6643:fa000ce7441e r6643:fa000ce7441e r6643:fa000ce7441e r6643:fa000ce7441e r6643:fa000ce7441e r13024:48c81d0b078a r6643:fa000ce7441e r6643:fa000ce7441e r6643:fa000ce7441e r6643:fa000ce7441e r6643:fa000ce7441e r6643:fa000ce7441e r6643:fa000ce7441e r6643:fa000ce7441e r6643:fa000ce7441e r6643:fa000ce7441e r12290:442d5d533a57 r6643:fa000ce7441e r12291:690f4158878e r6643:fa000ce7441e r12290:442d5d533a57 r7492:75510449064b r12373:30da669f3dfa r6643:fa000ce7441e r6643:fa000ce7441e r6643:fa000ce7441e r6643:fa000ce7441e r6643:fa000ce7441e r13024:48c81d0b078a r6643:fa000ce7441e r6643:fa000ce7441e r10207:a1fc2f2a33db r8237:d8e599249043 r11919:363b629324b9 r6643:fa000ce7441e r11917:612c11f7ab47 r8237:d8e599249043 r8237:d8e599249043 r8237:d8e599249043 r8237:d8e599249043 r10207:a1fc2f2a33db r8237:d8e599249043 r8237:d8e599249043 r8237:d8e599249043 r8237:d8e599249043 r10207:a1fc2f2a33db r6643:fa000ce7441e r6643:fa000ce7441e r6643:fa000ce7441e r6643:fa000ce7441e r10207:a1fc2f2a33db r6643:fa000ce7441e r6643:fa000ce7441e | /* $Id$ */
/*
* 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 group_cmd.cpp Handling of the engine groups */
#include "stdafx.h"
#include "command_func.h"
#include "group.h"
#include "train.h"
#include "engine_base.h"
#include "vehicle_gui.h"
#include "window_func.h"
#include "vehicle_func.h"
#include "autoreplace_base.h"
#include "autoreplace_func.h"
#include "string_func.h"
#include "company_func.h"
#include "core/pool_func.hpp"
#include "table/strings.h"
GroupID _new_group_id;
GroupPool _group_pool("Group");
INSTANTIATE_POOL_METHODS(Group)
/**
* Update the num engines of a groupID. Decrease the old one and increase the new one
* @note called in SetTrainGroupID and UpdateTrainGroupID
* @param i EngineID we have to update
* @param old_g index of the old group
* @param new_g index of the new group
*/
static inline void UpdateNumEngineGroup(EngineID i, GroupID old_g, GroupID new_g)
{
if (old_g != new_g) {
/* Decrease the num engines of EngineID i of the old group if it's not the default one */
if (!IsDefaultGroupID(old_g) && Group::IsValidID(old_g)) Group::Get(old_g)->num_engines[i]--;
/* Increase the num engines of EngineID i of the new group if it's not the default one */
if (!IsDefaultGroupID(new_g) && Group::IsValidID(new_g)) Group::Get(new_g)->num_engines[i]++;
}
}
Group::Group(Owner owner)
{
this->owner = owner;
if (!Company::IsValidID(owner)) return;
this->num_engines = CallocT<uint16>(Engine::GetPoolSize());
}
Group::~Group()
{
free(this->name);
free(this->num_engines);
}
void InitializeGroup()
{
_group_pool.CleanPool();
}
/**
* Create a new vehicle group.
* @param tile unused
* @param flags type of operation
* @param p1 vehicle type
* @param p2 unused
* @param text unused
* @return the cost of this operation or an error
*/
CommandCost CmdCreateGroup(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
{
VehicleType vt = (VehicleType)p1;
if (!IsCompanyBuildableVehicleType(vt)) return CMD_ERROR;
if (!Group::CanAllocateItem()) return CMD_ERROR;
if (flags & DC_EXEC) {
Group *g = new Group(_current_company);
g->replace_protection = false;
g->vehicle_type = vt;
_new_group_id = g->index;
InvalidateWindowData(GetWindowClassForVehicleType(vt), (vt << 11) | VLW_GROUP_LIST | _current_company);
}
return CommandCost();
}
/**
* Add all vehicles in the given group to the default group and then deletes the group.
* @param tile unused
* @param flags type of operation
* @param p1 index of array group
* - p1 bit 0-15 : GroupID
* @param p2 unused
* @param text unused
* @return the cost of this operation or an error
*/
CommandCost CmdDeleteGroup(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
{
Group *g = Group::GetIfValid(p1);
if (g == NULL || g->owner != _current_company) return CMD_ERROR;
if (flags & DC_EXEC) {
Vehicle *v;
/* Add all vehicles belong to the group to the default group */
FOR_ALL_VEHICLES(v) {
if (v->group_id == g->index && v->type == g->vehicle_type) v->group_id = DEFAULT_GROUP;
}
/* Update backupped orders if needed */
if (_backup_orders_data.group == g->index) _backup_orders_data.group = DEFAULT_GROUP;
/* If we set an autoreplace for the group we delete, remove it. */
if (_current_company < MAX_COMPANIES) {
Company *c;
EngineRenew *er;
c = Company::Get(_current_company);
FOR_ALL_ENGINE_RENEWS(er) {
if (er->group_id == g->index) RemoveEngineReplacementForCompany(c, er->from, g->index, flags);
}
}
VehicleType vt = g->vehicle_type;
/* Delete the Replace Vehicle Windows */
DeleteWindowById(WC_REPLACE_VEHICLE, g->vehicle_type);
delete g;
InvalidateWindowData(GetWindowClassForVehicleType(vt), (vt << 11) | VLW_GROUP_LIST | _current_company);
}
return CommandCost();
}
static bool IsUniqueGroupName(const char *name)
{
const Group *g;
FOR_ALL_GROUPS(g) {
if (g->name != NULL && strcmp(g->name, name) == 0) return false;
}
return true;
}
/**
* Rename a group
* @param tile unused
* @param flags type of operation
* @param p1 index of array group
* - p1 bit 0-15 : GroupID
* @param p2 unused
* @param text the new name or an empty string when resetting to the default
* @return the cost of this operation or an error
*/
CommandCost CmdRenameGroup(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
{
Group *g = Group::GetIfValid(p1);
if (g == NULL || g->owner != _current_company) return CMD_ERROR;
bool reset = StrEmpty(text);
if (!reset) {
if (strlen(text) >= MAX_LENGTH_GROUP_NAME_BYTES) return CMD_ERROR;
if (!IsUniqueGroupName(text)) return_cmd_error(STR_ERROR_NAME_MUST_BE_UNIQUE);
}
if (flags & DC_EXEC) {
/* Delete the old name */
free(g->name);
/* Assign the new one */
g->name = reset ? NULL : strdup(text);
InvalidateWindowData(GetWindowClassForVehicleType(g->vehicle_type), (g->vehicle_type << 11) | VLW_GROUP_LIST | _current_company);
}
return CommandCost();
}
/**
* Add a vehicle to a group
* @param tile unused
* @param flags type of operation
* @param p1 index of array group
* - p1 bit 0-15 : GroupID
* @param p2 vehicle to add to a group
* - p2 bit 0-15 : VehicleID
* @param text unused
* @return the cost of this operation or an error
*/
CommandCost CmdAddVehicleGroup(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
{
Vehicle *v = Vehicle::GetIfValid(p2);
GroupID new_g = p1;
if (v == NULL || (!Group::IsValidID(new_g) && !IsDefaultGroupID(new_g))) return CMD_ERROR;
if (Group::IsValidID(new_g)) {
Group *g = Group::Get(new_g);
if (g->owner != _current_company || g->vehicle_type != v->type) return CMD_ERROR;
}
if (v->owner != _current_company || !v->IsPrimaryVehicle()) return CMD_ERROR;
if (flags & DC_EXEC) {
DecreaseGroupNumVehicle(v->group_id);
IncreaseGroupNumVehicle(new_g);
switch (v->type) {
default: NOT_REACHED();
case VEH_TRAIN:
SetTrainGroupID(Train::From(v), new_g);
break;
case VEH_ROAD:
case VEH_SHIP:
case VEH_AIRCRAFT:
if (v->IsEngineCountable()) UpdateNumEngineGroup(v->engine_type, v->group_id, new_g);
v->group_id = new_g;
break;
}
/* Update the Replace Vehicle Windows */
SetWindowDirty(WC_REPLACE_VEHICLE, v->type);
InvalidateWindowData(GetWindowClassForVehicleType(v->type), (v->type << 11) | VLW_GROUP_LIST | _current_company);
}
return CommandCost();
}
/**
* Add all shared vehicles of all vehicles from a group
* @param tile unused
* @param flags type of operation
* @param p1 index of group array
* - p1 bit 0-15 : GroupID
* @param p2 type of vehicles
* @param text unused
* @return the cost of this operation or an error
*/
CommandCost CmdAddSharedVehicleGroup(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
{
VehicleType type = (VehicleType)p2;
if (!Group::IsValidID(p1) || !IsCompanyBuildableVehicleType(type)) return CMD_ERROR;
if (flags & DC_EXEC) {
Vehicle *v;
VehicleType type = (VehicleType)p2;
GroupID id_g = p1;
/* Find the first front engine which belong to the group id_g
* then add all shared vehicles of this front engine to the group id_g */
FOR_ALL_VEHICLES(v) {
if (v->type == type && v->IsPrimaryVehicle()) {
if (v->group_id != id_g) continue;
/* For each shared vehicles add it to the group */
for (Vehicle *v2 = v->FirstShared(); v2 != NULL; v2 = v2->NextShared()) {
if (v2->group_id != id_g) DoCommand(tile, id_g, v2->index, flags, CMD_ADD_VEHICLE_GROUP, text);
}
}
}
InvalidateWindowData(GetWindowClassForVehicleType(type), (type << 11) | VLW_GROUP_LIST | _current_company);
}
return CommandCost();
}
/**
* Remove all vehicles from a group
* @param tile unused
* @param flags type of operation
* @param p1 index of group array
* - p1 bit 0-15 : GroupID
* @param p2 type of vehicles
* @param text unused
* @return the cost of this operation or an error
*/
CommandCost CmdRemoveAllVehiclesGroup(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
{
Group *g = Group::GetIfValid(p1);
VehicleType type = (VehicleType)p2;
if (g == NULL || g->owner != _current_company || !IsCompanyBuildableVehicleType(type)) return CMD_ERROR;
if (flags & DC_EXEC) {
GroupID old_g = p1;
Vehicle *v;
/* Find each Vehicle that belongs to the group old_g and add it to the default group */
FOR_ALL_VEHICLES(v) {
if (v->type == type && v->IsPrimaryVehicle()) {
if (v->group_id != old_g) continue;
/* Add The Vehicle to the default group */
DoCommand(tile, DEFAULT_GROUP, v->index, flags, CMD_ADD_VEHICLE_GROUP, text);
}
}
InvalidateWindowData(GetWindowClassForVehicleType(type), (type << 11) | VLW_GROUP_LIST | _current_company);
}
return CommandCost();
}
/**
* (Un)set global replace protection from a group
* @param tile unused
* @param flags type of operation
* @param p1 index of group array
* - p1 bit 0-15 : GroupID
* @param p2
* - p2 bit 0 : 1 to set or 0 to clear protection.
* @param text unused
* @return the cost of this operation or an error
*/
CommandCost CmdSetGroupReplaceProtection(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
{
Group *g = Group::GetIfValid(p1);
if (g == NULL || g->owner != _current_company) return CMD_ERROR;
if (flags & DC_EXEC) {
g->replace_protection = HasBit(p2, 0);
InvalidateWindowData(GetWindowClassForVehicleType(g->vehicle_type), (g->vehicle_type << 11) | VLW_GROUP_LIST | _current_company);
InvalidateWindowData(WC_REPLACE_VEHICLE, g->vehicle_type);
}
return CommandCost();
}
/**
* Decrease the num_vehicle variable before delete an front engine from a group
* @note Called in CmdSellRailWagon and DeleteLasWagon,
* @param v FrontEngine of the train we want to remove.
*/
void RemoveVehicleFromGroup(const Vehicle *v)
{
if (!v->IsPrimaryVehicle()) return;
if (!IsDefaultGroupID(v->group_id)) DecreaseGroupNumVehicle(v->group_id);
}
/**
* Affect the groupID of a train to new_g.
* @note called in CmdAddVehicleGroup and CmdMoveRailVehicle
* @param v First vehicle of the chain.
* @param new_g index of array group
*/
void SetTrainGroupID(Train *v, GroupID new_g)
{
if (!Group::IsValidID(new_g) && !IsDefaultGroupID(new_g)) return;
assert(v->IsFrontEngine() || IsDefaultGroupID(new_g));
for (Vehicle *u = v; u != NULL; u = u->Next()) {
if (u->IsEngineCountable()) UpdateNumEngineGroup(u->engine_type, u->group_id, new_g);
u->group_id = new_g;
}
/* Update the Replace Vehicle Windows */
SetWindowDirty(WC_REPLACE_VEHICLE, VEH_TRAIN);
}
/**
* Recalculates the groupID of a train. Should be called each time a vehicle is added
* to/removed from the chain,.
* @note this needs to be called too for 'wagon chains' (in the depot, without an engine)
* @note Called in CmdBuildRailVehicle, CmdBuildRailWagon, CmdMoveRailVehicle, CmdSellRailWagon
* @param v First vehicle of the chain.
*/
void UpdateTrainGroupID(Train *v)
{
assert(v->IsFrontEngine() || v->IsFreeWagon());
GroupID new_g = v->IsFrontEngine() ? v->group_id : (GroupID)DEFAULT_GROUP;
for (Vehicle *u = v; u != NULL; u = u->Next()) {
if (u->IsEngineCountable()) UpdateNumEngineGroup(u->engine_type, u->group_id, new_g);
u->group_id = new_g;
}
/* Update the Replace Vehicle Windows */
SetWindowDirty(WC_REPLACE_VEHICLE, VEH_TRAIN);
}
uint GetGroupNumEngines(CompanyID company, GroupID id_g, EngineID id_e)
{
if (Group::IsValidID(id_g)) return Group::Get(id_g)->num_engines[id_e];
uint num = Company::Get(company)->num_engines[id_e];
if (!IsDefaultGroupID(id_g)) return num;
const Group *g;
FOR_ALL_GROUPS(g) {
if (g->owner == company) num -= g->num_engines[id_e];
}
return num;
}
void RemoveAllGroupsForCompany(const CompanyID company)
{
Group *g;
FOR_ALL_GROUPS(g) {
if (company == g->owner) delete g;
}
}
|