2007-05-19 09:40:18 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
2009-08-21 20:21:05 +00:00
|
|
|
/*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2007-05-19 09:40:18 +00:00
|
|
|
/** @file group_cmd.cpp Handling of the engine groups */
|
|
|
|
|
|
|
|
#include "stdafx.h"
|
2010-04-17 13:31:41 +00:00
|
|
|
#include "cmd_helper.h"
|
2007-12-21 21:50:46 +00:00
|
|
|
#include "command_func.h"
|
2007-05-19 09:40:18 +00:00
|
|
|
#include "train.h"
|
2010-09-08 21:37:13 +00:00
|
|
|
#include "vehiclelist.h"
|
2007-12-27 13:35:39 +00:00
|
|
|
#include "vehicle_func.h"
|
2008-01-07 09:19:53 +00:00
|
|
|
#include "autoreplace_base.h"
|
|
|
|
#include "autoreplace_func.h"
|
2008-01-07 14:23:25 +00:00
|
|
|
#include "string_func.h"
|
2008-09-30 20:51:04 +00:00
|
|
|
#include "company_func.h"
|
2009-05-22 15:13:50 +00:00
|
|
|
#include "core/pool_func.hpp"
|
2010-08-18 15:15:59 +00:00
|
|
|
#include "order_backup.h"
|
2007-05-19 09:40:18 +00:00
|
|
|
|
2008-01-13 01:21:35 +00:00
|
|
|
#include "table/strings.h"
|
|
|
|
|
2009-01-12 17:11:45 +00:00
|
|
|
GroupID _new_group_id;
|
|
|
|
|
2009-05-22 15:13:50 +00:00
|
|
|
GroupPool _group_pool("Group");
|
|
|
|
INSTANTIATE_POOL_METHODS(Group)
|
|
|
|
|
2011-10-03 17:20:26 +00:00
|
|
|
GroupStatistics::GroupStatistics()
|
|
|
|
{
|
|
|
|
this->num_engines = CallocT<uint16>(Engine::GetPoolSize());
|
|
|
|
}
|
|
|
|
|
|
|
|
GroupStatistics::~GroupStatistics()
|
|
|
|
{
|
|
|
|
free(this->num_engines);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clear all caches.
|
|
|
|
*/
|
|
|
|
void GroupStatistics::Clear()
|
|
|
|
{
|
|
|
|
this->num_vehicle = 0;
|
2011-10-03 17:25:44 +00:00
|
|
|
this->num_profit_vehicle = 0;
|
|
|
|
this->profit_last_year = 0;
|
2011-10-03 17:20:26 +00:00
|
|
|
|
|
|
|
/* This is also called when NewGRF change. So the number of engines might have changed. Reallocate. */
|
|
|
|
free(this->num_engines);
|
|
|
|
this->num_engines = CallocT<uint16>(Engine::GetPoolSize());
|
|
|
|
}
|
|
|
|
|
2011-10-03 17:21:41 +00:00
|
|
|
/**
|
|
|
|
* Returns the GroupStatistics for a specific group.
|
|
|
|
* @param company Owner of the group.
|
|
|
|
* @param id_g GroupID of the group.
|
|
|
|
* @param type VehicleType of the vehicles in the group.
|
|
|
|
* @return Statistics for the group.
|
|
|
|
*/
|
|
|
|
/* static */ GroupStatistics &GroupStatistics::Get(CompanyID company, GroupID id_g, VehicleType type)
|
|
|
|
{
|
|
|
|
if (Group::IsValidID(id_g)) {
|
|
|
|
Group *g = Group::Get(id_g);
|
|
|
|
assert(g->owner == company);
|
|
|
|
assert(g->vehicle_type == type);
|
|
|
|
return g->statistics;
|
|
|
|
}
|
|
|
|
|
2011-10-03 17:22:56 +00:00
|
|
|
if (IsDefaultGroupID(id_g)) return Company::Get(company)->group_default[type];
|
2011-10-03 17:23:41 +00:00
|
|
|
if (IsAllGroupID(id_g)) return Company::Get(company)->group_all[type];
|
2011-10-03 17:22:56 +00:00
|
|
|
|
2011-10-03 17:21:41 +00:00
|
|
|
NOT_REACHED();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the GroupStatistic for the group of a vehicle.
|
|
|
|
* @param v Vehicle.
|
|
|
|
* @return GroupStatistics for the group of the vehicle.
|
|
|
|
*/
|
|
|
|
/* static */ GroupStatistics &GroupStatistics::Get(const Vehicle *v)
|
|
|
|
{
|
|
|
|
return GroupStatistics::Get(v->owner, v->group_id, v->type);
|
|
|
|
}
|
|
|
|
|
2011-10-03 17:23:41 +00:00
|
|
|
/**
|
|
|
|
* Returns the GroupStatistic for the ALL_GROUPO of a vehicle type.
|
|
|
|
* @param v Vehicle.
|
|
|
|
* @return GroupStatistics for the ALL_GROUP of the vehicle type.
|
|
|
|
*/
|
|
|
|
/* static */ GroupStatistics &GroupStatistics::GetAllGroup(const Vehicle *v)
|
|
|
|
{
|
|
|
|
return GroupStatistics::Get(v->owner, ALL_GROUP, v->type);
|
|
|
|
}
|
|
|
|
|
2011-10-03 17:20:56 +00:00
|
|
|
/**
|
|
|
|
* Update all caches after loading a game, changing NewGRF etc..
|
|
|
|
*/
|
|
|
|
/* static */ void GroupStatistics::UpdateAfterLoad()
|
|
|
|
{
|
|
|
|
/* Set up the engine count for all companies */
|
|
|
|
Company *c;
|
|
|
|
FOR_ALL_COMPANIES(c) {
|
2011-10-03 17:22:56 +00:00
|
|
|
for (VehicleType type = VEH_BEGIN; type < VEH_COMPANY_END; type++) {
|
2011-10-03 17:23:41 +00:00
|
|
|
c->group_all[type].Clear();
|
2011-10-03 17:22:56 +00:00
|
|
|
c->group_default[type].Clear();
|
|
|
|
}
|
2011-10-03 17:20:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Recalculate */
|
|
|
|
Group *g;
|
|
|
|
FOR_ALL_GROUPS(g) {
|
|
|
|
g->statistics.Clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
const Vehicle *v;
|
|
|
|
FOR_ALL_VEHICLES(v) {
|
|
|
|
if (!v->IsEngineCountable()) continue;
|
|
|
|
|
2011-10-03 17:22:09 +00:00
|
|
|
GroupStatistics::CountEngine(v, 1);
|
2011-10-03 17:21:41 +00:00
|
|
|
if (v->IsPrimaryVehicle()) GroupStatistics::CountVehicle(v, 1);
|
2011-10-03 17:20:56 +00:00
|
|
|
}
|
2011-10-03 17:26:37 +00:00
|
|
|
|
|
|
|
FOR_ALL_COMPANIES(c) {
|
|
|
|
GroupStatistics::UpdateAutoreplace(c->index);
|
|
|
|
}
|
2011-10-03 17:20:56 +00:00
|
|
|
}
|
|
|
|
|
2011-10-03 17:21:41 +00:00
|
|
|
/**
|
|
|
|
* Update num_vehicle when adding or removing a vehicle.
|
|
|
|
* @param v Vehicle to count.
|
|
|
|
* @param delta +1 to add, -1 to remove.
|
|
|
|
*/
|
|
|
|
/* static */ void GroupStatistics::CountVehicle(const Vehicle *v, int delta)
|
|
|
|
{
|
|
|
|
assert(delta == 1 || delta == -1);
|
|
|
|
|
2011-10-03 17:23:41 +00:00
|
|
|
GroupStatistics &stats_all = GroupStatistics::GetAllGroup(v);
|
2011-10-03 17:21:41 +00:00
|
|
|
GroupStatistics &stats = GroupStatistics::Get(v);
|
|
|
|
|
2011-10-03 17:23:41 +00:00
|
|
|
stats_all.num_vehicle += delta;
|
2011-10-03 17:21:41 +00:00
|
|
|
stats.num_vehicle += delta;
|
2011-10-03 17:25:44 +00:00
|
|
|
|
|
|
|
if (v->age > VEHICLE_PROFIT_MIN_AGE) {
|
|
|
|
stats_all.num_profit_vehicle += delta;
|
|
|
|
stats_all.profit_last_year += v->GetDisplayProfitLastYear() * delta;
|
|
|
|
stats.num_profit_vehicle += delta;
|
|
|
|
stats.profit_last_year += v->GetDisplayProfitLastYear() * delta;
|
|
|
|
}
|
2011-10-03 17:21:41 +00:00
|
|
|
}
|
|
|
|
|
2011-10-03 17:22:09 +00:00
|
|
|
/**
|
|
|
|
* Update num_engines when adding/removing an engine.
|
|
|
|
* @param v Engine to count.
|
|
|
|
* @param delta +1 to add, -1 to remove.
|
|
|
|
*/
|
|
|
|
/* static */ void GroupStatistics::CountEngine(const Vehicle *v, int delta)
|
|
|
|
{
|
|
|
|
assert(delta == 1 || delta == -1);
|
2011-10-03 17:23:41 +00:00
|
|
|
GroupStatistics::GetAllGroup(v).num_engines[v->engine_type] += delta;
|
2011-10-03 17:22:09 +00:00
|
|
|
GroupStatistics::Get(v).num_engines[v->engine_type] += delta;
|
|
|
|
}
|
|
|
|
|
2011-10-03 17:25:44 +00:00
|
|
|
/**
|
|
|
|
* Add a vehicle to the profit sum of its group.
|
|
|
|
*/
|
|
|
|
/* static */ void GroupStatistics::VehicleReachedProfitAge(const Vehicle *v)
|
|
|
|
{
|
|
|
|
GroupStatistics &stats_all = GroupStatistics::GetAllGroup(v);
|
|
|
|
GroupStatistics &stats = GroupStatistics::Get(v);
|
|
|
|
|
|
|
|
stats_all.num_profit_vehicle++;
|
|
|
|
stats_all.profit_last_year += v->GetDisplayProfitLastYear();
|
|
|
|
stats.num_profit_vehicle++;
|
|
|
|
stats.profit_last_year += v->GetDisplayProfitLastYear();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Recompute the profits for all groups.
|
|
|
|
*/
|
|
|
|
/* static */ void GroupStatistics::UpdateProfits()
|
|
|
|
{
|
|
|
|
/* Set up the engine count for all companies */
|
|
|
|
Company *c;
|
|
|
|
FOR_ALL_COMPANIES(c) {
|
|
|
|
for (VehicleType type = VEH_BEGIN; type < VEH_COMPANY_END; type++) {
|
|
|
|
c->group_all[type].ClearProfits();
|
|
|
|
c->group_default[type].ClearProfits();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Recalculate */
|
|
|
|
Group *g;
|
|
|
|
FOR_ALL_GROUPS(g) {
|
|
|
|
g->statistics.ClearProfits();
|
|
|
|
}
|
|
|
|
|
|
|
|
const Vehicle *v;
|
|
|
|
FOR_ALL_VEHICLES(v) {
|
|
|
|
if (v->IsPrimaryVehicle() && v->age > VEHICLE_PROFIT_MIN_AGE) GroupStatistics::VehicleReachedProfitAge(v);
|
|
|
|
}
|
|
|
|
}
|
2011-10-03 17:20:26 +00:00
|
|
|
|
2011-10-03 17:26:37 +00:00
|
|
|
/**
|
|
|
|
* Update autoreplace_defined and autoreplace_finished of all statistics of a company.
|
|
|
|
* @param company Company to update statistics for.
|
|
|
|
*/
|
|
|
|
/* static */ void GroupStatistics::UpdateAutoreplace(CompanyID company)
|
|
|
|
{
|
|
|
|
/* Set up the engine count for all companies */
|
|
|
|
Company *c = Company::Get(company);
|
|
|
|
for (VehicleType type = VEH_BEGIN; type < VEH_COMPANY_END; type++) {
|
|
|
|
c->group_all[type].ClearAutoreplace();
|
|
|
|
c->group_default[type].ClearAutoreplace();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Recalculate */
|
|
|
|
Group *g;
|
|
|
|
FOR_ALL_GROUPS(g) {
|
|
|
|
if (g->owner != company) continue;
|
|
|
|
g->statistics.ClearAutoreplace();
|
|
|
|
}
|
|
|
|
|
|
|
|
for (EngineRenewList erl = c->engine_renew_list; erl != NULL; erl = erl->next) {
|
|
|
|
const Engine *e = Engine::Get(erl->from);
|
|
|
|
GroupStatistics &stats = GroupStatistics::Get(company, erl->group_id, e->type);
|
|
|
|
if (!stats.autoreplace_defined) {
|
|
|
|
stats.autoreplace_defined = true;
|
|
|
|
stats.autoreplace_finished = true;
|
|
|
|
}
|
|
|
|
if (stats.num_engines[erl->from] > 0) stats.autoreplace_finished = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-05-19 09:40:18 +00:00
|
|
|
/**
|
|
|
|
* Update the num engines of a groupID. Decrease the old one and increase the new one
|
|
|
|
* @note called in SetTrainGroupID and UpdateTrainGroupID
|
2011-10-03 17:22:09 +00:00
|
|
|
* @param v Vehicle we have to update
|
2007-05-19 09:40:18 +00:00
|
|
|
* @param old_g index of the old group
|
|
|
|
* @param new_g index of the new group
|
|
|
|
*/
|
2011-10-03 17:22:09 +00:00
|
|
|
static inline void UpdateNumEngineGroup(const Vehicle *v, GroupID old_g, GroupID new_g)
|
2007-05-19 09:40:18 +00:00
|
|
|
{
|
|
|
|
if (old_g != new_g) {
|
2011-10-03 17:22:09 +00:00
|
|
|
/* Decrease the num engines in the old group */
|
2011-10-03 17:22:56 +00:00
|
|
|
GroupStatistics::Get(v->owner, old_g, v->type).num_engines[v->engine_type]--;
|
2007-05-19 09:40:18 +00:00
|
|
|
|
2011-10-03 17:22:09 +00:00
|
|
|
/* Increase the num engines in the new group */
|
2011-10-03 17:22:56 +00:00
|
|
|
GroupStatistics::Get(v->owner, new_g, v->type).num_engines[v->engine_type]++;
|
2007-05-19 09:40:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2008-09-30 20:39:50 +00:00
|
|
|
Group::Group(Owner owner)
|
2007-05-19 09:40:18 +00:00
|
|
|
{
|
2008-01-12 13:36:22 +00:00
|
|
|
this->owner = owner;
|
2007-08-02 12:51:57 +00:00
|
|
|
}
|
2007-05-19 09:40:18 +00:00
|
|
|
|
2007-08-02 12:51:57 +00:00
|
|
|
Group::~Group()
|
|
|
|
{
|
2008-01-12 19:58:06 +00:00
|
|
|
free(this->name);
|
2007-08-02 12:51:57 +00:00
|
|
|
}
|
2007-05-19 09:40:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
2007-07-08 09:35:39 +00:00
|
|
|
* Create a new vehicle group.
|
2007-05-19 09:40:18 +00:00
|
|
|
* @param tile unused
|
2009-09-18 14:23:58 +00:00
|
|
|
* @param flags type of operation
|
2007-05-19 09:40:18 +00:00
|
|
|
* @param p1 vehicle type
|
|
|
|
* @param p2 unused
|
2009-09-18 14:23:58 +00:00
|
|
|
* @param text unused
|
|
|
|
* @return the cost of this operation or an error
|
2007-05-19 09:40:18 +00:00
|
|
|
*/
|
2009-02-09 21:20:05 +00:00
|
|
|
CommandCost CmdCreateGroup(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
|
2007-05-19 09:40:18 +00:00
|
|
|
{
|
2010-04-17 13:31:41 +00:00
|
|
|
VehicleType vt = Extract<VehicleType, 0, 3>(p1);
|
2008-09-30 20:39:50 +00:00
|
|
|
if (!IsCompanyBuildableVehicleType(vt)) return CMD_ERROR;
|
2007-05-19 09:40:18 +00:00
|
|
|
|
2008-04-23 20:56:08 +00:00
|
|
|
if (!Group::CanAllocateItem()) return CMD_ERROR;
|
2007-08-02 12:51:57 +00:00
|
|
|
|
2007-05-19 09:40:18 +00:00
|
|
|
if (flags & DC_EXEC) {
|
2008-09-30 20:39:50 +00:00
|
|
|
Group *g = new Group(_current_company);
|
2007-05-19 09:40:18 +00:00
|
|
|
g->replace_protection = false;
|
|
|
|
g->vehicle_type = vt;
|
2007-05-22 19:48:11 +00:00
|
|
|
|
2009-01-12 17:11:45 +00:00
|
|
|
_new_group_id = g->index;
|
|
|
|
|
2010-09-08 21:37:13 +00:00
|
|
|
InvalidateWindowData(GetWindowClassForVehicleType(vt), VehicleListIdentifier(VL_GROUP_LIST, vt, _current_company).Pack());
|
2007-05-19 09:40:18 +00:00
|
|
|
}
|
|
|
|
|
2007-06-18 19:53:50 +00:00
|
|
|
return CommandCost();
|
2007-05-19 09:40:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2007-07-08 09:35:39 +00:00
|
|
|
* Add all vehicles in the given group to the default group and then deletes the group.
|
2007-05-19 09:40:18 +00:00
|
|
|
* @param tile unused
|
2009-09-18 14:23:58 +00:00
|
|
|
* @param flags type of operation
|
2007-05-19 09:40:18 +00:00
|
|
|
* @param p1 index of array group
|
|
|
|
* - p1 bit 0-15 : GroupID
|
|
|
|
* @param p2 unused
|
2009-09-18 14:23:58 +00:00
|
|
|
* @param text unused
|
|
|
|
* @return the cost of this operation or an error
|
2007-05-19 09:40:18 +00:00
|
|
|
*/
|
2009-02-09 21:20:05 +00:00
|
|
|
CommandCost CmdDeleteGroup(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
|
2007-05-19 09:40:18 +00:00
|
|
|
{
|
2009-05-18 16:21:28 +00:00
|
|
|
Group *g = Group::GetIfValid(p1);
|
|
|
|
if (g == NULL || g->owner != _current_company) return CMD_ERROR;
|
2007-05-19 09:40:18 +00:00
|
|
|
|
2011-11-14 16:09:58 +00:00
|
|
|
/* Remove all vehicles from the group */
|
|
|
|
DoCommand(0, p1, 0, flags, CMD_REMOVE_ALL_VEHICLES_GROUP);
|
2007-05-19 09:40:18 +00:00
|
|
|
|
2011-11-14 16:09:58 +00:00
|
|
|
if (flags & DC_EXEC) {
|
2008-02-27 21:46:57 +00:00
|
|
|
/* Update backupped orders if needed */
|
2010-08-18 15:15:59 +00:00
|
|
|
OrderBackup::ClearGroup(g->index);
|
2008-02-27 21:46:57 +00:00
|
|
|
|
2007-05-19 09:40:18 +00:00
|
|
|
/* If we set an autoreplace for the group we delete, remove it. */
|
2008-09-30 20:39:50 +00:00
|
|
|
if (_current_company < MAX_COMPANIES) {
|
|
|
|
Company *c;
|
2007-05-19 09:40:18 +00:00
|
|
|
EngineRenew *er;
|
|
|
|
|
2009-05-16 23:34:14 +00:00
|
|
|
c = Company::Get(_current_company);
|
2007-05-19 09:40:18 +00:00
|
|
|
FOR_ALL_ENGINE_RENEWS(er) {
|
2008-09-30 20:39:50 +00:00
|
|
|
if (er->group_id == g->index) RemoveEngineReplacementForCompany(c, er->from, g->index, flags);
|
2007-05-19 09:40:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-05-22 19:48:11 +00:00
|
|
|
VehicleType vt = g->vehicle_type;
|
|
|
|
|
2007-05-19 09:40:18 +00:00
|
|
|
/* Delete the Replace Vehicle Windows */
|
|
|
|
DeleteWindowById(WC_REPLACE_VEHICLE, g->vehicle_type);
|
2007-08-02 12:51:57 +00:00
|
|
|
delete g;
|
2007-05-22 19:48:11 +00:00
|
|
|
|
2010-09-08 21:37:13 +00:00
|
|
|
InvalidateWindowData(GetWindowClassForVehicleType(vt), VehicleListIdentifier(VL_GROUP_LIST, vt, _current_company).Pack());
|
2007-05-19 09:40:18 +00:00
|
|
|
}
|
|
|
|
|
2007-06-18 19:53:50 +00:00
|
|
|
return CommandCost();
|
2007-05-19 09:40:18 +00:00
|
|
|
}
|
|
|
|
|
2013-01-22 14:31:01 +00:00
|
|
|
static bool IsUniqueGroupNameForVehicleType(const char *name, VehicleType type)
|
2007-06-27 20:53:25 +00:00
|
|
|
{
|
|
|
|
const Group *g;
|
|
|
|
|
|
|
|
FOR_ALL_GROUPS(g) {
|
2013-01-22 14:31:01 +00:00
|
|
|
if (g->name != NULL && g->vehicle_type == type && strcmp(g->name, name) == 0) return false;
|
2007-06-27 20:53:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2007-05-19 09:40:18 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Rename a group
|
|
|
|
* @param tile unused
|
2009-09-18 14:23:58 +00:00
|
|
|
* @param flags type of operation
|
2007-05-19 09:40:18 +00:00
|
|
|
* @param p1 index of array group
|
|
|
|
* - p1 bit 0-15 : GroupID
|
|
|
|
* @param p2 unused
|
2009-09-18 14:23:58 +00:00
|
|
|
* @param text the new name or an empty string when resetting to the default
|
|
|
|
* @return the cost of this operation or an error
|
2007-05-19 09:40:18 +00:00
|
|
|
*/
|
2009-02-09 21:20:05 +00:00
|
|
|
CommandCost CmdRenameGroup(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
|
2007-05-19 09:40:18 +00:00
|
|
|
{
|
2009-05-18 16:21:28 +00:00
|
|
|
Group *g = Group::GetIfValid(p1);
|
|
|
|
if (g == NULL || g->owner != _current_company) return CMD_ERROR;
|
2007-05-22 19:48:11 +00:00
|
|
|
|
2008-12-28 14:37:19 +00:00
|
|
|
bool reset = StrEmpty(text);
|
2008-09-15 19:02:50 +00:00
|
|
|
|
|
|
|
if (!reset) {
|
2010-12-05 22:24:34 +00:00
|
|
|
if (Utf8StringLength(text) >= MAX_LENGTH_GROUP_NAME_CHARS) return CMD_ERROR;
|
2013-01-22 14:31:01 +00:00
|
|
|
if (!IsUniqueGroupNameForVehicleType(text, g->vehicle_type)) return_cmd_error(STR_ERROR_NAME_MUST_BE_UNIQUE);
|
2008-09-15 19:02:50 +00:00
|
|
|
}
|
2007-06-27 20:53:25 +00:00
|
|
|
|
2007-05-19 09:40:18 +00:00
|
|
|
if (flags & DC_EXEC) {
|
|
|
|
/* Delete the old name */
|
2008-01-12 19:58:06 +00:00
|
|
|
free(g->name);
|
2007-05-19 09:40:18 +00:00
|
|
|
/* Assign the new one */
|
2008-12-28 14:37:19 +00:00
|
|
|
g->name = reset ? NULL : strdup(text);
|
2007-05-22 19:48:11 +00:00
|
|
|
|
2012-04-17 19:44:30 +00:00
|
|
|
SetWindowDirty(WC_REPLACE_VEHICLE, g->vehicle_type);
|
2010-09-08 21:37:13 +00:00
|
|
|
InvalidateWindowData(GetWindowClassForVehicleType(g->vehicle_type), VehicleListIdentifier(VL_GROUP_LIST, g->vehicle_type, _current_company).Pack());
|
2007-05-19 09:40:18 +00:00
|
|
|
}
|
|
|
|
|
2007-06-18 19:53:50 +00:00
|
|
|
return CommandCost();
|
2007-05-19 09:40:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-04-17 19:44:12 +00:00
|
|
|
/**
|
|
|
|
* Do add a vehicle to a group.
|
|
|
|
* @param v Vehicle to add.
|
|
|
|
* @param new_g Group to add to.
|
|
|
|
*/
|
|
|
|
static void AddVehicleToGroup(Vehicle *v, GroupID new_g)
|
|
|
|
{
|
|
|
|
GroupStatistics::CountVehicle(v, -1);
|
|
|
|
|
|
|
|
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, v->group_id, new_g);
|
|
|
|
v->group_id = new_g;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
GroupStatistics::CountVehicle(v, 1);
|
|
|
|
}
|
|
|
|
|
2007-05-19 09:40:18 +00:00
|
|
|
/**
|
|
|
|
* Add a vehicle to a group
|
|
|
|
* @param tile unused
|
2009-09-18 14:23:58 +00:00
|
|
|
* @param flags type of operation
|
2007-05-19 09:40:18 +00:00
|
|
|
* @param p1 index of array group
|
|
|
|
* - p1 bit 0-15 : GroupID
|
|
|
|
* @param p2 vehicle to add to a group
|
2010-08-19 20:42:00 +00:00
|
|
|
* - p2 bit 0-19 : VehicleID
|
2012-04-17 19:44:12 +00:00
|
|
|
* - p2 bit 31 : Add shared vehicles as well.
|
2009-09-18 14:23:58 +00:00
|
|
|
* @param text unused
|
|
|
|
* @return the cost of this operation or an error
|
2007-05-19 09:40:18 +00:00
|
|
|
*/
|
2009-02-09 21:20:05 +00:00
|
|
|
CommandCost CmdAddVehicleGroup(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
|
2007-05-19 09:40:18 +00:00
|
|
|
{
|
2012-04-17 19:44:12 +00:00
|
|
|
Vehicle *v = Vehicle::GetIfValid(GB(p2, 0, 20));
|
2007-05-19 09:40:18 +00:00
|
|
|
GroupID new_g = p1;
|
|
|
|
|
2012-04-17 19:44:16 +00:00
|
|
|
if (v == NULL || (!Group::IsValidID(new_g) && !IsDefaultGroupID(new_g) && new_g != NEW_GROUP)) return CMD_ERROR;
|
2007-08-31 17:31:54 +00:00
|
|
|
|
2009-05-17 01:00:56 +00:00
|
|
|
if (Group::IsValidID(new_g)) {
|
2009-05-16 23:34:14 +00:00
|
|
|
Group *g = Group::Get(new_g);
|
2008-09-30 20:39:50 +00:00
|
|
|
if (g->owner != _current_company || g->vehicle_type != v->type) return CMD_ERROR;
|
2007-05-23 14:02:10 +00:00
|
|
|
}
|
2007-05-22 19:48:11 +00:00
|
|
|
|
2008-09-30 20:39:50 +00:00
|
|
|
if (v->owner != _current_company || !v->IsPrimaryVehicle()) return CMD_ERROR;
|
2007-05-19 09:40:18 +00:00
|
|
|
|
2012-04-17 19:44:16 +00:00
|
|
|
if (new_g == NEW_GROUP) {
|
|
|
|
/* Create new group. */
|
|
|
|
CommandCost ret = CmdCreateGroup(0, flags, v->type, 0, NULL);
|
|
|
|
if (ret.Failed()) return ret;
|
|
|
|
|
|
|
|
new_g = _new_group_id;
|
|
|
|
}
|
|
|
|
|
2007-05-19 09:40:18 +00:00
|
|
|
if (flags & DC_EXEC) {
|
2012-04-17 19:44:12 +00:00
|
|
|
AddVehicleToGroup(v, new_g);
|
|
|
|
|
|
|
|
if (HasBit(p2, 31)) {
|
|
|
|
/* Add vehicles in the shared order list as well. */
|
|
|
|
for (Vehicle *v2 = v->FirstShared(); v2 != NULL; v2 = v2->NextShared()) {
|
|
|
|
if (v2->group_id != new_g) AddVehicleToGroup(v2, new_g);
|
|
|
|
}
|
2007-05-19 09:40:18 +00:00
|
|
|
}
|
|
|
|
|
2011-10-03 17:26:37 +00:00
|
|
|
GroupStatistics::UpdateAutoreplace(v->owner);
|
2011-10-03 17:21:41 +00:00
|
|
|
|
2007-05-19 09:40:18 +00:00
|
|
|
/* Update the Replace Vehicle Windows */
|
2009-09-13 19:15:59 +00:00
|
|
|
SetWindowDirty(WC_REPLACE_VEHICLE, v->type);
|
2010-09-08 21:37:13 +00:00
|
|
|
InvalidateWindowData(GetWindowClassForVehicleType(v->type), VehicleListIdentifier(VL_GROUP_LIST, v->type, _current_company).Pack());
|
2007-05-19 09:40:18 +00:00
|
|
|
}
|
|
|
|
|
2007-06-18 19:53:50 +00:00
|
|
|
return CommandCost();
|
2007-05-19 09:40:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add all shared vehicles of all vehicles from a group
|
|
|
|
* @param tile unused
|
2009-09-18 14:23:58 +00:00
|
|
|
* @param flags type of operation
|
2007-05-19 09:40:18 +00:00
|
|
|
* @param p1 index of group array
|
|
|
|
* - p1 bit 0-15 : GroupID
|
|
|
|
* @param p2 type of vehicles
|
2009-09-18 14:23:58 +00:00
|
|
|
* @param text unused
|
|
|
|
* @return the cost of this operation or an error
|
2007-05-19 09:40:18 +00:00
|
|
|
*/
|
2009-02-09 21:20:05 +00:00
|
|
|
CommandCost CmdAddSharedVehicleGroup(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
|
2007-05-19 09:40:18 +00:00
|
|
|
{
|
2010-04-17 13:31:41 +00:00
|
|
|
VehicleType type = Extract<VehicleType, 0, 3>(p2);
|
|
|
|
GroupID id_g = p1;
|
|
|
|
if (!Group::IsValidID(id_g) || !IsCompanyBuildableVehicleType(type)) return CMD_ERROR;
|
2007-05-19 09:40:18 +00:00
|
|
|
|
|
|
|
if (flags & DC_EXEC) {
|
|
|
|
Vehicle *v;
|
|
|
|
|
|
|
|
/* 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) {
|
2007-06-01 12:03:10 +00:00
|
|
|
if (v->type == type && v->IsPrimaryVehicle()) {
|
2007-05-19 09:40:18 +00:00
|
|
|
if (v->group_id != id_g) continue;
|
|
|
|
|
|
|
|
/* For each shared vehicles add it to the group */
|
2008-08-17 19:56:17 +00:00
|
|
|
for (Vehicle *v2 = v->FirstShared(); v2 != NULL; v2 = v2->NextShared()) {
|
2010-03-14 20:53:31 +00:00
|
|
|
if (v2->group_id != id_g) DoCommand(tile, id_g, v2->index, flags, CMD_ADD_VEHICLE_GROUP, text);
|
2007-05-19 09:40:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-05-22 19:48:11 +00:00
|
|
|
|
2010-09-08 21:37:13 +00:00
|
|
|
InvalidateWindowData(GetWindowClassForVehicleType(type), VehicleListIdentifier(VL_GROUP_LIST, type, _current_company).Pack());
|
2007-05-19 09:40:18 +00:00
|
|
|
}
|
|
|
|
|
2007-06-18 19:53:50 +00:00
|
|
|
return CommandCost();
|
2007-05-19 09:40:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove all vehicles from a group
|
|
|
|
* @param tile unused
|
2009-09-18 14:23:58 +00:00
|
|
|
* @param flags type of operation
|
2007-05-19 09:40:18 +00:00
|
|
|
* @param p1 index of group array
|
|
|
|
* - p1 bit 0-15 : GroupID
|
2011-11-20 14:39:12 +00:00
|
|
|
* @param p2 unused
|
2009-09-18 14:23:58 +00:00
|
|
|
* @param text unused
|
|
|
|
* @return the cost of this operation or an error
|
2007-05-19 09:40:18 +00:00
|
|
|
*/
|
2009-02-09 21:20:05 +00:00
|
|
|
CommandCost CmdRemoveAllVehiclesGroup(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
|
2007-05-19 09:40:18 +00:00
|
|
|
{
|
2010-04-17 13:31:41 +00:00
|
|
|
GroupID old_g = p1;
|
|
|
|
Group *g = Group::GetIfValid(old_g);
|
2007-05-19 09:40:18 +00:00
|
|
|
|
2011-11-20 14:39:12 +00:00
|
|
|
if (g == NULL || g->owner != _current_company) return CMD_ERROR;
|
2007-05-22 19:48:11 +00:00
|
|
|
|
2007-05-19 09:40:18 +00:00
|
|
|
if (flags & DC_EXEC) {
|
|
|
|
Vehicle *v;
|
|
|
|
|
|
|
|
/* Find each Vehicle that belongs to the group old_g and add it to the default group */
|
|
|
|
FOR_ALL_VEHICLES(v) {
|
2011-11-20 14:39:12 +00:00
|
|
|
if (v->IsPrimaryVehicle()) {
|
2007-05-19 09:40:18 +00:00
|
|
|
if (v->group_id != old_g) continue;
|
|
|
|
|
|
|
|
/* Add The Vehicle to the default group */
|
2010-03-14 20:53:31 +00:00
|
|
|
DoCommand(tile, DEFAULT_GROUP, v->index, flags, CMD_ADD_VEHICLE_GROUP, text);
|
2007-05-19 09:40:18 +00:00
|
|
|
}
|
|
|
|
}
|
2007-05-22 19:48:11 +00:00
|
|
|
|
2011-11-20 14:39:12 +00:00
|
|
|
InvalidateWindowData(GetWindowClassForVehicleType(g->vehicle_type), VehicleListIdentifier(VL_GROUP_LIST, g->vehicle_type, _current_company).Pack());
|
2007-05-22 19:48:11 +00:00
|
|
|
}
|
|
|
|
|
2007-06-18 19:53:50 +00:00
|
|
|
return CommandCost();
|
2007-05-22 19:48:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* (Un)set global replace protection from a group
|
|
|
|
* @param tile unused
|
2009-09-18 14:23:58 +00:00
|
|
|
* @param flags type of operation
|
2007-05-22 19:48:11 +00:00
|
|
|
* @param p1 index of group array
|
|
|
|
* - p1 bit 0-15 : GroupID
|
|
|
|
* @param p2
|
|
|
|
* - p2 bit 0 : 1 to set or 0 to clear protection.
|
2009-09-18 14:23:58 +00:00
|
|
|
* @param text unused
|
|
|
|
* @return the cost of this operation or an error
|
2007-05-22 19:48:11 +00:00
|
|
|
*/
|
2009-02-09 21:20:05 +00:00
|
|
|
CommandCost CmdSetGroupReplaceProtection(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
|
2007-05-22 19:48:11 +00:00
|
|
|
{
|
2009-05-18 16:21:28 +00:00
|
|
|
Group *g = Group::GetIfValid(p1);
|
|
|
|
if (g == NULL || g->owner != _current_company) return CMD_ERROR;
|
2007-05-22 19:48:11 +00:00
|
|
|
|
|
|
|
if (flags & DC_EXEC) {
|
2007-11-19 21:02:30 +00:00
|
|
|
g->replace_protection = HasBit(p2, 0);
|
2007-05-22 19:48:11 +00:00
|
|
|
|
2012-10-01 19:43:10 +00:00
|
|
|
SetWindowDirty(GetWindowClassForVehicleType(g->vehicle_type), VehicleListIdentifier(VL_GROUP_LIST, g->vehicle_type, _current_company).Pack());
|
2008-11-29 21:27:15 +00:00
|
|
|
InvalidateWindowData(WC_REPLACE_VEHICLE, g->vehicle_type);
|
2007-05-19 09:40:18 +00:00
|
|
|
}
|
|
|
|
|
2007-06-18 19:53:50 +00:00
|
|
|
return CommandCost();
|
2007-05-19 09:40:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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)
|
|
|
|
{
|
2009-05-22 15:13:50 +00:00
|
|
|
if (!v->IsPrimaryVehicle()) return;
|
2007-05-19 09:40:18 +00:00
|
|
|
|
2011-10-03 17:21:41 +00:00
|
|
|
if (!IsDefaultGroupID(v->group_id)) GroupStatistics::CountVehicle(v, -1);
|
2007-05-19 09:40:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2009-07-01 22:22:01 +00:00
|
|
|
void SetTrainGroupID(Train *v, GroupID new_g)
|
2007-05-19 09:40:18 +00:00
|
|
|
{
|
2009-05-17 01:00:56 +00:00
|
|
|
if (!Group::IsValidID(new_g) && !IsDefaultGroupID(new_g)) return;
|
2007-05-19 09:40:18 +00:00
|
|
|
|
2009-12-13 22:17:12 +00:00
|
|
|
assert(v->IsFrontEngine() || IsDefaultGroupID(new_g));
|
2007-05-19 09:40:18 +00:00
|
|
|
|
2007-08-30 13:03:56 +00:00
|
|
|
for (Vehicle *u = v; u != NULL; u = u->Next()) {
|
2011-10-03 17:22:09 +00:00
|
|
|
if (u->IsEngineCountable()) UpdateNumEngineGroup(u, u->group_id, new_g);
|
2007-05-19 09:40:18 +00:00
|
|
|
|
|
|
|
u->group_id = new_g;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Update the Replace Vehicle Windows */
|
2011-10-03 17:26:37 +00:00
|
|
|
GroupStatistics::UpdateAutoreplace(v->owner);
|
2009-09-13 19:15:59 +00:00
|
|
|
SetWindowDirty(WC_REPLACE_VEHICLE, VEH_TRAIN);
|
2007-05-19 09:40:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2009-07-01 22:22:01 +00:00
|
|
|
void UpdateTrainGroupID(Train *v)
|
2007-05-19 09:40:18 +00:00
|
|
|
{
|
2009-07-01 23:49:13 +00:00
|
|
|
assert(v->IsFrontEngine() || v->IsFreeWagon());
|
2007-05-19 09:40:18 +00:00
|
|
|
|
2009-07-01 22:22:01 +00:00
|
|
|
GroupID new_g = v->IsFrontEngine() ? v->group_id : (GroupID)DEFAULT_GROUP;
|
2007-08-30 13:03:56 +00:00
|
|
|
for (Vehicle *u = v; u != NULL; u = u->Next()) {
|
2011-10-03 17:22:09 +00:00
|
|
|
if (u->IsEngineCountable()) UpdateNumEngineGroup(u, u->group_id, new_g);
|
2007-05-19 09:40:18 +00:00
|
|
|
|
|
|
|
u->group_id = new_g;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Update the Replace Vehicle Windows */
|
2011-10-03 17:26:37 +00:00
|
|
|
GroupStatistics::UpdateAutoreplace(v->owner);
|
2009-09-13 19:15:59 +00:00
|
|
|
SetWindowDirty(WC_REPLACE_VEHICLE, VEH_TRAIN);
|
2007-05-19 09:40:18 +00:00
|
|
|
}
|
|
|
|
|
2011-01-18 22:31:06 +00:00
|
|
|
/**
|
|
|
|
* Get the number of engines with EngineID id_e in the group with GroupID
|
|
|
|
* id_g
|
|
|
|
* @param company The company the group belongs to
|
|
|
|
* @param id_g The GroupID of the group used
|
|
|
|
* @param id_e The EngineID of the engine to count
|
|
|
|
* @return The number of engines with EngineID id_e in the group
|
|
|
|
*/
|
2008-09-30 20:39:50 +00:00
|
|
|
uint GetGroupNumEngines(CompanyID company, GroupID id_g, EngineID id_e)
|
2008-01-09 21:27:39 +00:00
|
|
|
{
|
2011-10-03 17:22:56 +00:00
|
|
|
const Engine *e = Engine::Get(id_e);
|
|
|
|
return GroupStatistics::Get(company, id_g, e->type).num_engines[id_e];
|
2008-01-09 21:27:39 +00:00
|
|
|
}
|
2007-05-19 09:40:18 +00:00
|
|
|
|
2008-09-30 20:39:50 +00:00
|
|
|
void RemoveAllGroupsForCompany(const CompanyID company)
|
2007-05-19 09:40:18 +00:00
|
|
|
{
|
|
|
|
Group *g;
|
|
|
|
|
|
|
|
FOR_ALL_GROUPS(g) {
|
2008-09-30 20:39:50 +00:00
|
|
|
if (company == g->owner) delete g;
|
2007-05-19 09:40:18 +00:00
|
|
|
}
|
|
|
|
}
|