Do name generation at client, fix localisation

Use existing group creation command
This commit is contained in:
Jonathan G Rennison
2021-06-27 02:28:52 +01:00
parent dee05f520f
commit 6c07758cc3
7 changed files with 45 additions and 159 deletions

View File

@@ -649,119 +649,19 @@ CommandCost CmdAddVehicleGroup(TileIndex tile, DoCommandFlag flags, uint32 p1, u
return CommandCost();
}
Group* CreateAutoGroup(const VehicleType vt, const std::string& name, Group* parent)
{
Group *group = nullptr;
if (Group::CanAllocateItem())
{
const Company *company = Company::Get(_current_company);
group = new Group(_current_company);
group->vehicle_type = vt;
group->parent = parent != nullptr ? parent->index : INVALID_GROUP;
group->livery.colour1 = company->livery[LS_DEFAULT].colour1;
group->livery.colour2 = company->livery[LS_DEFAULT].colour2;
if (company->settings.renew_keep_length) {
SetBit(group->flags, GF_REPLACE_WAGON_REMOVAL);
}
group->name = name;
}
return group;
}
std::string GetCargoList(const Vehicle *vehicle)
{
auto checked_vehicle = vehicle;
std::vector<StringID> cargoes;
do {
if (checked_vehicle->cargo_cap == 0) continue;
const StringID cargo_name = CargoSpec::Get(checked_vehicle->cargo_type)->name;
if(cargo_name == INVALID_STRING_ID) continue;
if (std::find(cargoes.begin(), cargoes.end(), cargo_name) == cargoes.end()) {
cargoes.push_back(cargo_name);
}
} while ((checked_vehicle = checked_vehicle->Next()) != nullptr);
std::string cargo_list;
if (!cargoes.empty()) {
for (auto cargo : cargoes) {
if (cargo_list.empty()) {
cargo_list += " (";
} else {
cargo_list += ", ";
}
char cargo_name_buffer[255];
SetDParam(0, cargo);
GetString(cargo_name_buffer, STR_JUST_STRING, lastof(cargo_name_buffer));
cargo_list += cargo_name_buffer;
}
}
if (!cargo_list.empty()) {
cargo_list += ")";
}
return cargo_list;
}
/**
* Adds a vehicle to an auto group.
* @param vehicle the vehicle
* @param from the first town in the orders list
* @param to the last town in the orders list
* @return the group to add the vehicle to.
*/
Group* CreateVehicleAutoGroup(const Vehicle *vehicle, const Town *from, const Town *to)
{
assert(from != nullptr);
Group *group;
if (from == to || to == nullptr)
{
char local_name[255];
GetString(local_name, STR_VEHICLE_GROUP_LOCAL_ROUTE, lastof(local_name));
std::string name;
name += from->GetCachedName();
name += " ";
name += local_name;
name += GetCargoList(vehicle);
group = CreateAutoGroup(vehicle->type, name, nullptr);
}
else
{
std::string name;
name += from->GetCachedName();
name += " to ";
name += to->GetCachedName();
name += GetCargoList(vehicle);
group = CreateAutoGroup(vehicle->type, name, nullptr);
}
return group;
}
Town* GetTownFromDestination(const DestinationID destination)
static Town* GetTownFromDestination(const DestinationID destination)
{
Town* town = nullptr;
if (BaseStation *st = BaseStation::GetIfValid(destination); st != nullptr)
{
BaseStation *st = BaseStation::GetIfValid(destination);
if (st != nullptr) {
town = st->town;
}
return town;
}
void GetAutoGroupMostRelevantTowns(const Vehicle *vehicle, Town* &from, Town* &to)
static void GetAutoGroupMostRelevantTowns(const Vehicle *vehicle, Town* &from, Town* &to)
{
std::vector<Town*> unique_destinations;
@@ -791,62 +691,42 @@ void GetAutoGroupMostRelevantTowns(const Vehicle *vehicle, Town* &from, Town* &t
}
}
/**
* Create a new group, rename it with automatically generated name and add vehicle to this group
* @param tile unused
* @param flags type of operation
* @param p1 vehicle to add to a group
* - p1 bit 0-19 : VehicleID
* - p1 bit 31 : Add shared vehicles as well.
* @param p2 unused
* @param text unused
* @return the cost of this operation or an error
*/
CommandCost CmdCreateGroupAutoName(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
static CargoTypes GetVehicleCargoList(const Vehicle *vehicle)
{
Vehicle *vehicle = Vehicle::GetIfValid(GB(p1, 0, 20));
CargoTypes cargoes = 0;
if (vehicle == nullptr) return CMD_ERROR;
if (vehicle->owner != _current_company || !vehicle->IsPrimaryVehicle()) return CMD_ERROR;
if (!Group::CanAllocateItem()) return CMD_ERROR;
for (const Vehicle *u = vehicle; u != nullptr; u = u->Next()) {
if (u->cargo_cap == 0) continue;
SetBit(cargoes, u->cargo_type);
}
return cargoes;
}
std::string GenerateAutoNameForVehicleGroup(const Vehicle *v)
{
Town *town_from = nullptr;
Town *town_to = nullptr;
GetAutoGroupMostRelevantTowns(vehicle, town_from, town_to);
if (town_from == nullptr) return CMD_ERROR;
GetAutoGroupMostRelevantTowns(v, town_from, town_to);
if (town_from == nullptr) return "";
if (flags & DC_EXEC) {
const auto new_group = CreateVehicleAutoGroup(vehicle, town_from, town_to);
CargoTypes cargoes = GetVehicleCargoList(v);
assert(new_group != nullptr);
AddVehicleToGroup(vehicle, new_group->index);
if (HasBit(p1, 31)) {
/* Add vehicles in the shared order list as well. */
for (Vehicle *v2 = vehicle->FirstShared(); v2 != nullptr; v2 = v2->NextShared()) {
if (v2->group_id != new_group->index) {
AddVehicleToGroup(v2, new_group->index);
}
}
}
GroupStatistics::UpdateAutoreplace(vehicle->owner);
/* Update the Replace Vehicle Windows */
SetWindowDirty(WC_REPLACE_VEHICLE, vehicle->type);
SetWindowDirty(WC_VEHICLE_DEPOT, vehicle->tile);
SetWindowDirty(WC_VEHICLE_VIEW, vehicle->index);
SetWindowDirty(WC_VEHICLE_DETAILS, vehicle->index);
InvalidateWindowData(WC_VEHICLE_VIEW, vehicle->index);
InvalidateWindowData(WC_VEHICLE_DETAILS, vehicle->index);
InvalidateWindowData(GetWindowClassForVehicleType(vehicle->type), VehicleListIdentifier(VL_GROUP_LIST, vehicle->type, _current_company).Pack());
InvalidateWindowClassesData(WC_TEMPLATEGUI_MAIN);
char group_name[512];
if (town_from == town_to || town_to == nullptr) {
SetDParam(0, town_from->index);
SetDParam(1, (cargoes != 0) ? STR_VEHICLE_AUTO_GROUP_CARGO_LIST : STR_EMPTY);
SetDParam(2, cargoes);
GetString(group_name, STR_VEHICLE_AUTO_GROUP_LOCAL_ROUTE, lastof(group_name));
} else {
SetDParam(0, town_from->index);
SetDParam(1, town_to->index);
SetDParam(2, (cargoes != 0) ? STR_VEHICLE_AUTO_GROUP_CARGO_LIST : STR_EMPTY);
SetDParam(3, cargoes);
GetString(group_name, STR_VEHICLE_AUTO_GROUP_ROUTE, lastof(group_name));
}
return CommandCost();
return std::string(group_name);
}
/**