src/group_cmd.cpp
changeset 10207 c291a21b304e
parent 10148 ad8e8a65f6a2
child 10208 72c00af5c95d
equal deleted inserted replaced
10206:0050610c0368 10207:c291a21b304e
    46 
    46 
    47 
    47 
    48 DEFINE_OLD_POOL_GENERIC(Group, Group)
    48 DEFINE_OLD_POOL_GENERIC(Group, Group)
    49 
    49 
    50 
    50 
    51 Group::Group(PlayerID owner)
    51 Group::Group(Owner owner)
    52 {
    52 {
    53 	this->owner = owner;
    53 	this->owner = owner;
    54 
    54 
    55 	if (this->IsValid()) this->num_engines = CallocT<uint16>(GetEnginePoolSize());
    55 	if (this->IsValid()) this->num_engines = CallocT<uint16>(GetEnginePoolSize());
    56 }
    56 }
    57 
    57 
    58 Group::~Group()
    58 Group::~Group()
    59 {
    59 {
    60 	free(this->name);
    60 	free(this->name);
    61 	this->owner = INVALID_PLAYER;
    61 	this->owner = INVALID_OWNER;
    62 	free(this->num_engines);
    62 	free(this->num_engines);
    63 }
    63 }
    64 
    64 
    65 bool Group::IsValid() const
    65 bool Group::IsValid() const
    66 {
    66 {
    67 	return this->owner != INVALID_PLAYER;
    67 	return this->owner != INVALID_OWNER;
    68 }
    68 }
    69 
    69 
    70 void InitializeGroup(void)
    70 void InitializeGroup(void)
    71 {
    71 {
    72 	_Group_pool.CleanPool();
    72 	_Group_pool.CleanPool();
    81  * @param p2   unused
    81  * @param p2   unused
    82  */
    82  */
    83 CommandCost CmdCreateGroup(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
    83 CommandCost CmdCreateGroup(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
    84 {
    84 {
    85 	VehicleType vt = (VehicleType)p1;
    85 	VehicleType vt = (VehicleType)p1;
    86 	if (!IsPlayerBuildableVehicleType(vt)) return CMD_ERROR;
    86 	if (!IsCompanyBuildableVehicleType(vt)) return CMD_ERROR;
    87 
    87 
    88 	if (!Group::CanAllocateItem()) return CMD_ERROR;
    88 	if (!Group::CanAllocateItem()) return CMD_ERROR;
    89 
    89 
    90 	if (flags & DC_EXEC) {
    90 	if (flags & DC_EXEC) {
    91 		Group *g = new Group(_current_player);
    91 		Group *g = new Group(_current_company);
    92 		g->replace_protection = false;
    92 		g->replace_protection = false;
    93 		g->vehicle_type = vt;
    93 		g->vehicle_type = vt;
    94 
    94 
    95 		InvalidateWindowData(GetWindowClassForVehicleType(vt), (vt << 11) | VLW_GROUP_LIST | _current_player);
    95 		InvalidateWindowData(GetWindowClassForVehicleType(vt), (vt << 11) | VLW_GROUP_LIST | _current_company);
    96 	}
    96 	}
    97 
    97 
    98 	return CommandCost();
    98 	return CommandCost();
    99 }
    99 }
   100 
   100 
   109 CommandCost CmdDeleteGroup(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
   109 CommandCost CmdDeleteGroup(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
   110 {
   110 {
   111 	if (!IsValidGroupID(p1)) return CMD_ERROR;
   111 	if (!IsValidGroupID(p1)) return CMD_ERROR;
   112 
   112 
   113 	Group *g = GetGroup(p1);
   113 	Group *g = GetGroup(p1);
   114 	if (g->owner != _current_player) return CMD_ERROR;
   114 	if (g->owner != _current_company) return CMD_ERROR;
   115 
   115 
   116 	if (flags & DC_EXEC) {
   116 	if (flags & DC_EXEC) {
   117 		Vehicle *v;
   117 		Vehicle *v;
   118 
   118 
   119 		/* Add all vehicles belong to the group to the default group */
   119 		/* Add all vehicles belong to the group to the default group */
   123 
   123 
   124 		/* Update backupped orders if needed */
   124 		/* Update backupped orders if needed */
   125 		if (_backup_orders_data.group == g->index) _backup_orders_data.group = DEFAULT_GROUP;
   125 		if (_backup_orders_data.group == g->index) _backup_orders_data.group = DEFAULT_GROUP;
   126 
   126 
   127 		/* If we set an autoreplace for the group we delete, remove it. */
   127 		/* If we set an autoreplace for the group we delete, remove it. */
   128 		if (_current_player < MAX_PLAYERS) {
   128 		if (_current_company < MAX_COMPANIES) {
   129 			Player *p;
   129 			Company *c;
   130 			EngineRenew *er;
   130 			EngineRenew *er;
   131 
   131 
   132 			p = GetPlayer(_current_player);
   132 			c = GetCompany(_current_company);
   133 			FOR_ALL_ENGINE_RENEWS(er) {
   133 			FOR_ALL_ENGINE_RENEWS(er) {
   134 				if (er->group_id == g->index) RemoveEngineReplacementForPlayer(p, er->from, g->index, flags);
   134 				if (er->group_id == g->index) RemoveEngineReplacementForCompany(c, er->from, g->index, flags);
   135 			}
   135 			}
   136 		}
   136 		}
   137 
   137 
   138 		VehicleType vt = g->vehicle_type;
   138 		VehicleType vt = g->vehicle_type;
   139 
   139 
   140 		/* Delete the Replace Vehicle Windows */
   140 		/* Delete the Replace Vehicle Windows */
   141 		DeleteWindowById(WC_REPLACE_VEHICLE, g->vehicle_type);
   141 		DeleteWindowById(WC_REPLACE_VEHICLE, g->vehicle_type);
   142 		delete g;
   142 		delete g;
   143 
   143 
   144 		InvalidateWindowData(GetWindowClassForVehicleType(vt), (vt << 11) | VLW_GROUP_LIST | _current_player);
   144 		InvalidateWindowData(GetWindowClassForVehicleType(vt), (vt << 11) | VLW_GROUP_LIST | _current_company);
   145 	}
   145 	}
   146 
   146 
   147 	return CommandCost();
   147 	return CommandCost();
   148 }
   148 }
   149 
   149 
   171 CommandCost CmdRenameGroup(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
   171 CommandCost CmdRenameGroup(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
   172 {
   172 {
   173 	if (!IsValidGroupID(p1)) return CMD_ERROR;
   173 	if (!IsValidGroupID(p1)) return CMD_ERROR;
   174 
   174 
   175 	Group *g = GetGroup(p1);
   175 	Group *g = GetGroup(p1);
   176 	if (g->owner != _current_player) return CMD_ERROR;
   176 	if (g->owner != _current_company) return CMD_ERROR;
   177 
   177 
   178 	bool reset = StrEmpty(_cmd_text);
   178 	bool reset = StrEmpty(_cmd_text);
   179 
   179 
   180 	if (!reset) {
   180 	if (!reset) {
   181 		if (strlen(_cmd_text) >= MAX_LENGTH_GROUP_NAME_BYTES) return CMD_ERROR;
   181 		if (strlen(_cmd_text) >= MAX_LENGTH_GROUP_NAME_BYTES) return CMD_ERROR;
   186 		/* Delete the old name */
   186 		/* Delete the old name */
   187 		free(g->name);
   187 		free(g->name);
   188 		/* Assign the new one */
   188 		/* Assign the new one */
   189 		g->name = reset ? NULL : strdup(_cmd_text);
   189 		g->name = reset ? NULL : strdup(_cmd_text);
   190 
   190 
   191 		InvalidateWindowData(GetWindowClassForVehicleType(g->vehicle_type), (g->vehicle_type << 11) | VLW_GROUP_LIST | _current_player);
   191 		InvalidateWindowData(GetWindowClassForVehicleType(g->vehicle_type), (g->vehicle_type << 11) | VLW_GROUP_LIST | _current_company);
   192 	}
   192 	}
   193 
   193 
   194 	return CommandCost();
   194 	return CommandCost();
   195 }
   195 }
   196 
   196 
   211 
   211 
   212 	Vehicle *v = GetVehicle(p2);
   212 	Vehicle *v = GetVehicle(p2);
   213 
   213 
   214 	if (IsValidGroupID(new_g)) {
   214 	if (IsValidGroupID(new_g)) {
   215 		Group *g = GetGroup(new_g);
   215 		Group *g = GetGroup(new_g);
   216 		if (g->owner != _current_player || g->vehicle_type != v->type) return CMD_ERROR;
   216 		if (g->owner != _current_company || g->vehicle_type != v->type) return CMD_ERROR;
   217 	}
   217 	}
   218 
   218 
   219 	if (v->owner != _current_player || !v->IsPrimaryVehicle()) return CMD_ERROR;
   219 	if (v->owner != _current_company || !v->IsPrimaryVehicle()) return CMD_ERROR;
   220 
   220 
   221 	if (flags & DC_EXEC) {
   221 	if (flags & DC_EXEC) {
   222 		DecreaseGroupNumVehicle(v->group_id);
   222 		DecreaseGroupNumVehicle(v->group_id);
   223 		IncreaseGroupNumVehicle(new_g);
   223 		IncreaseGroupNumVehicle(new_g);
   224 
   224 
   235 				break;
   235 				break;
   236 		}
   236 		}
   237 
   237 
   238 		/* Update the Replace Vehicle Windows */
   238 		/* Update the Replace Vehicle Windows */
   239 		InvalidateWindow(WC_REPLACE_VEHICLE, v->type);
   239 		InvalidateWindow(WC_REPLACE_VEHICLE, v->type);
   240 		InvalidateWindowData(GetWindowClassForVehicleType(v->type), (v->type << 11) | VLW_GROUP_LIST | _current_player);
   240 		InvalidateWindowData(GetWindowClassForVehicleType(v->type), (v->type << 11) | VLW_GROUP_LIST | _current_company);
   241 	}
   241 	}
   242 
   242 
   243 	return CommandCost();
   243 	return CommandCost();
   244 }
   244 }
   245 
   245 
   251  * @param p2   type of vehicles
   251  * @param p2   type of vehicles
   252  */
   252  */
   253 CommandCost CmdAddSharedVehicleGroup(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
   253 CommandCost CmdAddSharedVehicleGroup(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
   254 {
   254 {
   255 	VehicleType type = (VehicleType)p2;
   255 	VehicleType type = (VehicleType)p2;
   256 	if (!IsValidGroupID(p1) || !IsPlayerBuildableVehicleType(type)) return CMD_ERROR;
   256 	if (!IsValidGroupID(p1) || !IsCompanyBuildableVehicleType(type)) return CMD_ERROR;
   257 
   257 
   258 	if (flags & DC_EXEC) {
   258 	if (flags & DC_EXEC) {
   259 		Vehicle *v;
   259 		Vehicle *v;
   260 		VehicleType type = (VehicleType)p2;
   260 		VehicleType type = (VehicleType)p2;
   261 		GroupID id_g = p1;
   261 		GroupID id_g = p1;
   271 					if (v2->group_id != id_g) CmdAddVehicleGroup(tile, flags, id_g, v2->index);
   271 					if (v2->group_id != id_g) CmdAddVehicleGroup(tile, flags, id_g, v2->index);
   272 				}
   272 				}
   273 			}
   273 			}
   274 		}
   274 		}
   275 
   275 
   276 		InvalidateWindowData(GetWindowClassForVehicleType(type), (type << 11) | VLW_GROUP_LIST | _current_player);
   276 		InvalidateWindowData(GetWindowClassForVehicleType(type), (type << 11) | VLW_GROUP_LIST | _current_company);
   277 	}
   277 	}
   278 
   278 
   279 	return CommandCost();
   279 	return CommandCost();
   280 }
   280 }
   281 
   281 
   288  * @param p2   type of vehicles
   288  * @param p2   type of vehicles
   289  */
   289  */
   290 CommandCost CmdRemoveAllVehiclesGroup(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
   290 CommandCost CmdRemoveAllVehiclesGroup(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
   291 {
   291 {
   292 	VehicleType type = (VehicleType)p2;
   292 	VehicleType type = (VehicleType)p2;
   293 	if (!IsValidGroupID(p1) || !IsPlayerBuildableVehicleType(type)) return CMD_ERROR;
   293 	if (!IsValidGroupID(p1) || !IsCompanyBuildableVehicleType(type)) return CMD_ERROR;
   294 
   294 
   295 	Group *g = GetGroup(p1);
   295 	Group *g = GetGroup(p1);
   296 	if (g->owner != _current_player) return CMD_ERROR;
   296 	if (g->owner != _current_company) return CMD_ERROR;
   297 
   297 
   298 	if (flags & DC_EXEC) {
   298 	if (flags & DC_EXEC) {
   299 		GroupID old_g = p1;
   299 		GroupID old_g = p1;
   300 		Vehicle *v;
   300 		Vehicle *v;
   301 
   301 
   307 				/* Add The Vehicle to the default group */
   307 				/* Add The Vehicle to the default group */
   308 				CmdAddVehicleGroup(tile, flags, DEFAULT_GROUP, v->index);
   308 				CmdAddVehicleGroup(tile, flags, DEFAULT_GROUP, v->index);
   309 			}
   309 			}
   310 		}
   310 		}
   311 
   311 
   312 		InvalidateWindowData(GetWindowClassForVehicleType(type), (type << 11) | VLW_GROUP_LIST | _current_player);
   312 		InvalidateWindowData(GetWindowClassForVehicleType(type), (type << 11) | VLW_GROUP_LIST | _current_company);
   313 	}
   313 	}
   314 
   314 
   315 	return CommandCost();
   315 	return CommandCost();
   316 }
   316 }
   317 
   317 
   327 CommandCost CmdSetGroupReplaceProtection(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
   327 CommandCost CmdSetGroupReplaceProtection(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
   328 {
   328 {
   329 	if (!IsValidGroupID(p1)) return CMD_ERROR;
   329 	if (!IsValidGroupID(p1)) return CMD_ERROR;
   330 
   330 
   331 	Group *g = GetGroup(p1);
   331 	Group *g = GetGroup(p1);
   332 	if (g->owner != _current_player) return CMD_ERROR;
   332 	if (g->owner != _current_company) return CMD_ERROR;
   333 
   333 
   334 	if (flags & DC_EXEC) {
   334 	if (flags & DC_EXEC) {
   335 		g->replace_protection = HasBit(p2, 0);
   335 		g->replace_protection = HasBit(p2, 0);
   336 
   336 
   337 		InvalidateWindowData(GetWindowClassForVehicleType(g->vehicle_type), (g->vehicle_type << 11) | VLW_GROUP_LIST | _current_player);
   337 		InvalidateWindowData(GetWindowClassForVehicleType(g->vehicle_type), (g->vehicle_type << 11) | VLW_GROUP_LIST | _current_company);
   338 	}
   338 	}
   339 
   339 
   340 	return CommandCost();
   340 	return CommandCost();
   341 }
   341 }
   342 
   342 
   396 
   396 
   397 	/* Update the Replace Vehicle Windows */
   397 	/* Update the Replace Vehicle Windows */
   398 	InvalidateWindow(WC_REPLACE_VEHICLE, VEH_TRAIN);
   398 	InvalidateWindow(WC_REPLACE_VEHICLE, VEH_TRAIN);
   399 }
   399 }
   400 
   400 
   401 uint GetGroupNumEngines(PlayerID p, GroupID id_g, EngineID id_e)
   401 uint GetGroupNumEngines(CompanyID company, GroupID id_g, EngineID id_e)
   402 {
   402 {
   403 	if (IsValidGroupID(id_g)) return GetGroup(id_g)->num_engines[id_e];
   403 	if (IsValidGroupID(id_g)) return GetGroup(id_g)->num_engines[id_e];
   404 
   404 
   405 	uint num = GetPlayer(p)->num_engines[id_e];
   405 	uint num = GetCompany(company)->num_engines[id_e];
   406 	if (!IsDefaultGroupID(id_g)) return num;
   406 	if (!IsDefaultGroupID(id_g)) return num;
   407 
   407 
   408 	const Group *g;
   408 	const Group *g;
   409 	FOR_ALL_GROUPS(g) {
   409 	FOR_ALL_GROUPS(g) {
   410 		if (g->owner == p) num -= g->num_engines[id_e];
   410 		if (g->owner == company) num -= g->num_engines[id_e];
   411 	}
   411 	}
   412 	return num;
   412 	return num;
   413 }
   413 }
   414 
   414 
   415 void RemoveAllGroupsForPlayer(const PlayerID p)
   415 void RemoveAllGroupsForCompany(const CompanyID company)
   416 {
   416 {
   417 	Group *g;
   417 	Group *g;
   418 
   418 
   419 	FOR_ALL_GROUPS(g) {
   419 	FOR_ALL_GROUPS(g) {
   420 		if (p == g->owner) delete g;
   420 		if (company == g->owner) delete g;
   421 	}
   421 	}
   422 }
   422 }
   423 
   423 
   424 
   424 
   425 static const SaveLoad _group_desc[] = {
   425 static const SaveLoad _group_desc[] = {