glx@9624: /* $Id$ */ glx@9624: glx@9624: /** @file group_cmd.cpp Handling of the engine groups */ glx@9624: glx@9624: #include "stdafx.h" glx@9624: #include "openttd.h" rubidium@9631: #include "variables.h" rubidium@9723: #include "command_func.h" glx@9624: #include "saveload.h" glx@9624: #include "debug.h" glx@9624: #include "group.h" glx@9624: #include "train.h" glx@9624: #include "aircraft.h" glx@9624: #include "vehicle_gui.h" rubidium@9723: #include "strings_func.h" rubidium@9723: #include "functions.h" rubidium@9723: #include "window_func.h" rubidium@9723: #include "vehicle_func.h" rubidium@9723: #include "autoreplace_base.h" rubidium@9723: #include "autoreplace_func.h" rubidium@9723: #include "string_func.h" rubidium@9724: #include "player_func.h" rubidium@9837: #include "order_func.h" rubidium@10142: #include "oldpool_func.h" rubidium@10455: #include "core/alloc_func.hpp" rubidium@9724: rubidium@9724: #include "table/strings.h" glx@9624: glx@9624: /** glx@9624: * Update the num engines of a groupID. Decrease the old one and increase the new one glx@9624: * @note called in SetTrainGroupID and UpdateTrainGroupID glx@9624: * @param i EngineID we have to update glx@9624: * @param old_g index of the old group glx@9624: * @param new_g index of the new group glx@9624: */ glx@9624: static inline void UpdateNumEngineGroup(EngineID i, GroupID old_g, GroupID new_g) glx@9624: { glx@9624: if (old_g != new_g) { glx@9624: /* Decrease the num engines of EngineID i of the old group if it's not the default one */ glx@9624: if (!IsDefaultGroupID(old_g) && IsValidGroupID(old_g)) GetGroup(old_g)->num_engines[i]--; glx@9624: rubidium@10249: /* Increase the num engines of EngineID i of the new group if it's not the default one */ glx@9624: if (!IsDefaultGroupID(new_g) && IsValidGroupID(new_g)) GetGroup(new_g)->num_engines[i]++; glx@9624: } glx@9624: } glx@9624: glx@9624: rubidium@9694: DEFINE_OLD_POOL_GENERIC(Group, Group) rubidium@9694: rubidium@9694: rubidium@9724: Group::Group(PlayerID owner) glx@9624: { rubidium@9724: this->owner = owner; rubidium@10455: this->num_engines = CallocT(GetEnginePoolSize()); glx@9624: } glx@9624: rubidium@9694: Group::~Group() glx@9624: { rubidium@9724: free(this->name); rubidium@9724: this->owner = INVALID_PLAYER; rubidium@10455: free(this->num_engines); rubidium@9694: } glx@9624: rubidium@9694: bool Group::IsValid() const rubidium@9694: { rubidium@9724: return this->owner != INVALID_PLAYER; glx@9624: } glx@9624: glx@9624: void InitializeGroup(void) glx@9624: { rubidium@9694: _Group_pool.CleanPool(); rubidium@9694: _Group_pool.AddBlockToPool(); glx@9624: } glx@9624: glx@9624: glx@9624: /** truelight@9641: * Create a new vehicle group. glx@9624: * @param tile unused glx@9624: * @param p1 vehicle type glx@9624: * @param p2 unused glx@9624: */ glx@9629: CommandCost CmdCreateGroup(TileIndex tile, uint32 flags, uint32 p1, uint32 p2) glx@9624: { glx@9624: VehicleType vt = (VehicleType)p1; glx@9624: if (!IsPlayerBuildableVehicleType(vt)) return CMD_ERROR; glx@9624: rubidium@10355: if (!Group::CanAllocateItem()) return CMD_ERROR; rubidium@9694: glx@9624: if (flags & DC_EXEC) { rubidium@10355: Group *g = new Group(_current_player); glx@9624: g->replace_protection = false; glx@9624: g->vehicle_type = vt; glx@9624: rubidium@10355: InvalidateWindowData(GetWindowClassForVehicleType(vt), (vt << 11) | VLW_GROUP_LIST | _current_player); glx@9624: } glx@9624: glx@9629: return CommandCost(); glx@9624: } glx@9624: glx@9624: glx@9624: /** truelight@9641: * Add all vehicles in the given group to the default group and then deletes the group. glx@9624: * @param tile unused glx@9624: * @param p1 index of array group glx@9624: * - p1 bit 0-15 : GroupID glx@9624: * @param p2 unused glx@9624: */ glx@9629: CommandCost CmdDeleteGroup(TileIndex tile, uint32 flags, uint32 p1, uint32 p2) glx@9624: { glx@9624: if (!IsValidGroupID(p1)) return CMD_ERROR; glx@9624: glx@9624: Group *g = GetGroup(p1); glx@9624: if (g->owner != _current_player) return CMD_ERROR; glx@9624: glx@9624: if (flags & DC_EXEC) { glx@9624: Vehicle *v; glx@9624: glx@9624: /* Add all vehicles belong to the group to the default group */ glx@9624: FOR_ALL_VEHICLES(v) { glx@9624: if (v->group_id == g->index && v->type == g->vehicle_type) v->group_id = DEFAULT_GROUP; glx@9624: } glx@9624: glx@9800: /* Update backupped orders if needed */ glx@9800: if (_backup_orders_data.group == g->index) _backup_orders_data.group = DEFAULT_GROUP; glx@9800: glx@9624: /* If we set an autoreplace for the group we delete, remove it. */ glx@9624: if (_current_player < MAX_PLAYERS) { glx@9624: Player *p; glx@9624: EngineRenew *er; glx@9624: glx@9624: p = GetPlayer(_current_player); glx@9624: FOR_ALL_ENGINE_RENEWS(er) { glx@9624: if (er->group_id == g->index) RemoveEngineReplacementForPlayer(p, er->from, g->index, flags); glx@9624: } glx@9624: } glx@9624: glx@9624: VehicleType vt = g->vehicle_type; glx@9624: glx@9624: /* Delete the Replace Vehicle Windows */ glx@9624: DeleteWindowById(WC_REPLACE_VEHICLE, g->vehicle_type); rubidium@9694: delete g; glx@9624: rubidium@10355: InvalidateWindowData(GetWindowClassForVehicleType(vt), (vt << 11) | VLW_GROUP_LIST | _current_player); glx@9624: } glx@9624: glx@9629: return CommandCost(); glx@9624: } glx@9624: rubidium@9631: static bool IsUniqueGroupName(const char *name) rubidium@9631: { rubidium@9631: const Group *g; rubidium@9631: char buf[512]; rubidium@9631: rubidium@9631: FOR_ALL_GROUPS(g) { rubidium@9631: SetDParam(0, g->index); rubidium@9631: GetString(buf, STR_GROUP_NAME, lastof(buf)); rubidium@9631: if (strcmp(buf, name) == 0) return false; rubidium@9631: } rubidium@9631: rubidium@9631: return true; rubidium@9631: } glx@9624: glx@9624: /** glx@9624: * Rename a group glx@9624: * @param tile unused glx@9624: * @param p1 index of array group glx@9624: * - p1 bit 0-15 : GroupID glx@9624: * @param p2 unused glx@9624: */ glx@9629: CommandCost CmdRenameGroup(TileIndex tile, uint32 flags, uint32 p1, uint32 p2) glx@9624: { glx@9624: if (!IsValidGroupID(p1) || StrEmpty(_cmd_text)) return CMD_ERROR; glx@9624: glx@9624: Group *g = GetGroup(p1); glx@9624: if (g->owner != _current_player) return CMD_ERROR; glx@9624: rubidium@9631: if (!IsUniqueGroupName(_cmd_text)) return_cmd_error(STR_NAME_MUST_BE_UNIQUE); rubidium@9631: glx@9624: if (flags & DC_EXEC) { glx@9624: /* Delete the old name */ rubidium@9724: free(g->name); glx@9624: /* Assign the new one */ rubidium@9724: g->name = strdup(_cmd_text); glx@9624: rubidium@10355: InvalidateWindowData(GetWindowClassForVehicleType(g->vehicle_type), (g->vehicle_type << 11) | VLW_GROUP_LIST | _current_player); glx@9624: } glx@9624: glx@9629: return CommandCost(); glx@9624: } glx@9624: glx@9624: glx@9624: /** glx@9624: * Add a vehicle to a group glx@9624: * @param tile unused glx@9624: * @param p1 index of array group glx@9624: * - p1 bit 0-15 : GroupID glx@9624: * @param p2 vehicle to add to a group glx@9624: * - p2 bit 0-15 : VehicleID glx@9624: */ glx@9629: CommandCost CmdAddVehicleGroup(TileIndex tile, uint32 flags, uint32 p1, uint32 p2) glx@9624: { glx@9624: GroupID new_g = p1; glx@9624: glx@9624: if (!IsValidVehicleID(p2) || (!IsValidGroupID(new_g) && !IsDefaultGroupID(new_g))) return CMD_ERROR; glx@9624: rubidium@9701: Vehicle *v = GetVehicle(p2); rubidium@9701: glx@9624: if (IsValidGroupID(new_g)) { glx@9624: Group *g = GetGroup(new_g); rubidium@9701: if (g->owner != _current_player || g->vehicle_type != v->type) return CMD_ERROR; glx@9624: } glx@9624: rubidium@9625: if (v->owner != _current_player || !v->IsPrimaryVehicle()) return CMD_ERROR; glx@9624: glx@9624: if (flags & DC_EXEC) { glx@9624: DecreaseGroupNumVehicle(v->group_id); glx@9624: IncreaseGroupNumVehicle(new_g); glx@9624: glx@9624: switch (v->type) { glx@9624: default: NOT_REACHED(); glx@9624: case VEH_TRAIN: glx@9624: SetTrainGroupID(v, new_g); glx@9624: break; glx@9624: case VEH_ROAD: glx@9624: case VEH_SHIP: glx@9624: case VEH_AIRCRAFT: glx@9624: if (IsEngineCountable(v)) UpdateNumEngineGroup(v->engine_type, v->group_id, new_g); glx@9624: v->group_id = new_g; glx@9624: break; glx@9624: } glx@9624: glx@9624: /* Update the Replace Vehicle Windows */ glx@9624: InvalidateWindow(WC_REPLACE_VEHICLE, v->type); rubidium@10355: InvalidateWindowData(GetWindowClassForVehicleType(v->type), (v->type << 11) | VLW_GROUP_LIST | _current_player); glx@9624: } glx@9624: glx@9629: return CommandCost(); glx@9624: } glx@9624: glx@9624: /** glx@9624: * Add all shared vehicles of all vehicles from a group glx@9624: * @param tile unused glx@9624: * @param p1 index of group array glx@9624: * - p1 bit 0-15 : GroupID glx@9624: * @param p2 type of vehicles glx@9624: */ glx@9629: CommandCost CmdAddSharedVehicleGroup(TileIndex tile, uint32 flags, uint32 p1, uint32 p2) glx@9624: { glx@9624: VehicleType type = (VehicleType)p2; glx@9624: if (!IsValidGroupID(p1) || !IsPlayerBuildableVehicleType(type)) return CMD_ERROR; glx@9624: glx@9624: if (flags & DC_EXEC) { glx@9624: Vehicle *v; glx@9624: VehicleType type = (VehicleType)p2; glx@9624: GroupID id_g = p1; glx@9624: glx@9624: /* Find the first front engine which belong to the group id_g glx@9624: * then add all shared vehicles of this front engine to the group id_g */ glx@9624: FOR_ALL_VEHICLES(v) { rubidium@9625: if (v->type == type && v->IsPrimaryVehicle()) { glx@9624: if (v->group_id != id_g) continue; glx@9624: glx@9624: /* For each shared vehicles add it to the group */ glx@9624: for (Vehicle *v2 = GetFirstVehicleFromSharedList(v); v2 != NULL; v2 = v2->next_shared) { glx@9624: if (v2->group_id != id_g) CmdAddVehicleGroup(tile, flags, id_g, v2->index); glx@9624: } glx@9624: } glx@9624: } glx@9624: rubidium@10355: InvalidateWindowData(GetWindowClassForVehicleType(type), (type << 11) | VLW_GROUP_LIST | _current_player); glx@9624: } glx@9624: glx@9629: return CommandCost(); glx@9624: } glx@9624: glx@9624: glx@9624: /** glx@9624: * Remove all vehicles from a group glx@9624: * @param tile unused glx@9624: * @param p1 index of group array glx@9624: * - p1 bit 0-15 : GroupID glx@9624: * @param p2 type of vehicles glx@9624: */ glx@9629: CommandCost CmdRemoveAllVehiclesGroup(TileIndex tile, uint32 flags, uint32 p1, uint32 p2) glx@9624: { glx@9624: VehicleType type = (VehicleType)p2; glx@9624: if (!IsValidGroupID(p1) || !IsPlayerBuildableVehicleType(type)) return CMD_ERROR; glx@9624: glx@9624: Group *g = GetGroup(p1); glx@9624: if (g->owner != _current_player) return CMD_ERROR; glx@9624: glx@9624: if (flags & DC_EXEC) { glx@9624: GroupID old_g = p1; glx@9624: Vehicle *v; glx@9624: glx@9624: /* Find each Vehicle that belongs to the group old_g and add it to the default group */ glx@9624: FOR_ALL_VEHICLES(v) { rubidium@9625: if (v->type == type && v->IsPrimaryVehicle()) { glx@9624: if (v->group_id != old_g) continue; glx@9624: glx@9624: /* Add The Vehicle to the default group */ glx@9624: CmdAddVehicleGroup(tile, flags, DEFAULT_GROUP, v->index); glx@9624: } glx@9624: } glx@9624: rubidium@10355: InvalidateWindowData(GetWindowClassForVehicleType(type), (type << 11) | VLW_GROUP_LIST | _current_player); glx@9624: } glx@9624: glx@9629: return CommandCost(); glx@9624: } glx@9624: glx@9624: glx@9624: /** glx@9624: * (Un)set global replace protection from a group glx@9624: * @param tile unused glx@9624: * @param p1 index of group array glx@9624: * - p1 bit 0-15 : GroupID glx@9624: * @param p2 glx@9624: * - p2 bit 0 : 1 to set or 0 to clear protection. glx@9624: */ glx@9629: CommandCost CmdSetGroupReplaceProtection(TileIndex tile, uint32 flags, uint32 p1, uint32 p2) glx@9624: { glx@9624: if (!IsValidGroupID(p1)) return CMD_ERROR; glx@9624: glx@9624: Group *g = GetGroup(p1); glx@9624: if (g->owner != _current_player) return CMD_ERROR; glx@9624: glx@9624: if (flags & DC_EXEC) { rubidium@9722: g->replace_protection = HasBit(p2, 0); glx@9624: rubidium@10355: InvalidateWindowData(GetWindowClassForVehicleType(g->vehicle_type), (g->vehicle_type << 11) | VLW_GROUP_LIST | _current_player); glx@9624: } glx@9624: glx@9629: return CommandCost(); glx@9624: } glx@9624: glx@9624: /** glx@9624: * Decrease the num_vehicle variable before delete an front engine from a group glx@9624: * @note Called in CmdSellRailWagon and DeleteLasWagon, glx@9624: * @param v FrontEngine of the train we want to remove. glx@9624: */ glx@9624: void RemoveVehicleFromGroup(const Vehicle *v) glx@9624: { rubidium@9703: if (!v->IsValid() || !v->IsPrimaryVehicle()) return; glx@9624: glx@9624: if (!IsDefaultGroupID(v->group_id)) DecreaseGroupNumVehicle(v->group_id); glx@9624: } glx@9624: glx@9624: glx@9624: /** glx@9624: * Affect the groupID of a train to new_g. glx@9624: * @note called in CmdAddVehicleGroup and CmdMoveRailVehicle glx@9624: * @param v First vehicle of the chain. glx@9624: * @param new_g index of array group glx@9624: */ glx@9624: void SetTrainGroupID(Vehicle *v, GroupID new_g) glx@9624: { glx@9624: if (!IsValidGroupID(new_g) && !IsDefaultGroupID(new_g)) return; glx@9624: rubidium@9694: assert(v->IsValid() && v->type == VEH_TRAIN && IsFrontEngine(v)); glx@9624: rubidium@9701: for (Vehicle *u = v; u != NULL; u = u->Next()) { glx@9624: if (IsEngineCountable(u)) UpdateNumEngineGroup(u->engine_type, u->group_id, new_g); glx@9624: glx@9624: u->group_id = new_g; glx@9624: } glx@9624: glx@9624: /* Update the Replace Vehicle Windows */ glx@9624: InvalidateWindow(WC_REPLACE_VEHICLE, VEH_TRAIN); glx@9624: } glx@9624: glx@9624: glx@9624: /** glx@9624: * Recalculates the groupID of a train. Should be called each time a vehicle is added glx@9624: * to/removed from the chain,. glx@9624: * @note this needs to be called too for 'wagon chains' (in the depot, without an engine) glx@9624: * @note Called in CmdBuildRailVehicle, CmdBuildRailWagon, CmdMoveRailVehicle, CmdSellRailWagon glx@9624: * @param v First vehicle of the chain. glx@9624: */ glx@9624: void UpdateTrainGroupID(Vehicle *v) glx@9624: { rubidium@9694: assert(v->IsValid() && v->type == VEH_TRAIN && (IsFrontEngine(v) || IsFreeWagon(v))); glx@9624: glx@9624: GroupID new_g = IsFrontEngine(v) ? v->group_id : (GroupID)DEFAULT_GROUP; rubidium@9701: for (Vehicle *u = v; u != NULL; u = u->Next()) { glx@9624: if (IsEngineCountable(u)) UpdateNumEngineGroup(u->engine_type, u->group_id, new_g); glx@9624: glx@9624: u->group_id = new_g; glx@9624: } glx@9624: glx@9624: /* Update the Replace Vehicle Windows */ glx@9624: InvalidateWindow(WC_REPLACE_VEHICLE, VEH_TRAIN); glx@9624: } glx@9624: rubidium@9724: uint GetGroupNumEngines(PlayerID p, GroupID id_g, EngineID id_e) rubidium@9724: { rubidium@9724: if (IsValidGroupID(id_g)) return GetGroup(id_g)->num_engines[id_e]; glx@9624: rubidium@9724: uint num = GetPlayer(p)->num_engines[id_e]; rubidium@9724: if (!IsDefaultGroupID(id_g)) return num; rubidium@9724: rubidium@9724: const Group *g; rubidium@9724: FOR_ALL_GROUPS(g) { rubidium@9724: if (g->owner == p) num -= g->num_engines[id_e]; rubidium@9724: } rubidium@9724: return num; rubidium@9724: } rubidium@9724: rubidium@9724: void RemoveAllGroupsForPlayer(const PlayerID p) glx@9624: { glx@9624: Group *g; glx@9624: glx@9624: FOR_ALL_GROUPS(g) { rubidium@9724: if (p == g->owner) delete g; glx@9624: } glx@9624: } glx@9624: glx@9624: glx@9624: static const SaveLoad _group_desc[] = { rubidium@9724: SLE_CONDVAR(Group, name, SLE_NAME, 0, 83), rubidium@9724: SLE_CONDSTR(Group, name, SLE_STR, 0, 84, SL_MAX_VERSION), glx@9624: SLE_VAR(Group, num_vehicle, SLE_UINT16), glx@9624: SLE_VAR(Group, owner, SLE_UINT8), glx@9624: SLE_VAR(Group, vehicle_type, SLE_UINT8), glx@9624: SLE_VAR(Group, replace_protection, SLE_BOOL), glx@9624: SLE_END() glx@9624: }; glx@9624: glx@9624: glx@9624: static void Save_GROUP(void) glx@9624: { glx@9624: Group *g; glx@9624: glx@9624: FOR_ALL_GROUPS(g) { glx@9624: SlSetArrayIndex(g->index); glx@9624: SlObject(g, _group_desc); glx@9624: } glx@9624: } glx@9624: glx@9624: glx@9624: static void Load_GROUP(void) glx@9624: { glx@9624: int index; glx@9624: glx@9624: while ((index = SlIterateArray()) != -1) { rubidium@9694: Group *g = new (index) Group(); glx@9624: SlObject(g, _group_desc); glx@9624: } glx@9624: } glx@9624: glx@9624: extern const ChunkHandler _group_chunk_handlers[] = { glx@9624: { 'GRPS', Save_GROUP, Load_GROUP, CH_ARRAY | CH_LAST}, glx@9624: };