src/group_cmd.cpp
changeset 6667 dba8ef60ffd2
parent 6651 671ccc278e9c
child 6671 1448c1051d40
equal deleted inserted replaced
6666:41ceafa4c85e 6667:dba8ef60ffd2
    13 #include "debug.h"
    13 #include "debug.h"
    14 #include "group.h"
    14 #include "group.h"
    15 #include "train.h"
    15 #include "train.h"
    16 #include "aircraft.h"
    16 #include "aircraft.h"
    17 #include "string.h"
    17 #include "string.h"
       
    18 #include "window.h"
       
    19 #include "vehicle_gui.h"
    18 
    20 
    19 /**
    21 /**
    20  * Update the num engines of a groupID. Decrease the old one and increase the new one
    22  * Update the num engines of a groupID. Decrease the old one and increase the new one
    21  * @note called in SetTrainGroupID and UpdateTrainGroupID
    23  * @note called in SetTrainGroupID and UpdateTrainGroupID
    22  * @param i     EngineID we have to update
    24  * @param i     EngineID we have to update
    71 	CleanPool(&_Group_pool);
    73 	CleanPool(&_Group_pool);
    72 	AddBlockToPool(&_Group_pool);
    74 	AddBlockToPool(&_Group_pool);
    73 }
    75 }
    74 
    76 
    75 
    77 
       
    78 static WindowClass GetWCForVT(VehicleType vt)
       
    79 {
       
    80 	switch (vt) {
       
    81 		default:
       
    82 		case VEH_TRAIN:    return WC_TRAINS_LIST;
       
    83 		case VEH_ROAD:     return WC_ROADVEH_LIST;
       
    84 		case VEH_SHIP:     return WC_SHIPS_LIST;
       
    85 		case VEH_AIRCRAFT: return WC_AIRCRAFT_LIST;
       
    86 	}
       
    87 }
       
    88 
       
    89 
    76 /**
    90 /**
    77  * Add a vehicle to a group
    91  * Add a vehicle to a group
    78  * @param tile unused
    92  * @param tile unused
    79  * @param p1   vehicle type
    93  * @param p1   vehicle type
    80  * @param p2   unused
    94  * @param p2   unused
    90 	if (flags & DC_EXEC) {
   104 	if (flags & DC_EXEC) {
    91 		g->owner = _current_player;
   105 		g->owner = _current_player;
    92 		g->string_id = STR_SV_GROUP_NAME;
   106 		g->string_id = STR_SV_GROUP_NAME;
    93 		g->replace_protection = false;
   107 		g->replace_protection = false;
    94 		g->vehicle_type = vt;
   108 		g->vehicle_type = vt;
       
   109 
       
   110 		InvalidateWindowData(GetWCForVT(vt), (vt << 11) | VLW_GROUP_LIST | _current_player);
    95 	}
   111 	}
    96 
   112 
    97 	return 0;
   113 	return 0;
    98 }
   114 }
    99 
   115 
   129 			FOR_ALL_ENGINE_RENEWS(er) {
   145 			FOR_ALL_ENGINE_RENEWS(er) {
   130 				if (er->group_id == g->index) RemoveEngineReplacementForPlayer(p, er->from, g->index, flags);
   146 				if (er->group_id == g->index) RemoveEngineReplacementForPlayer(p, er->from, g->index, flags);
   131 			}
   147 			}
   132 		}
   148 		}
   133 
   149 
       
   150 		VehicleType vt = g->vehicle_type;
       
   151 
   134 		/* Delete the Replace Vehicle Windows */
   152 		/* Delete the Replace Vehicle Windows */
   135 		DeleteWindowById(WC_REPLACE_VEHICLE, g->vehicle_type);
   153 		DeleteWindowById(WC_REPLACE_VEHICLE, g->vehicle_type);
   136 		DeleteGroup(g);
   154 		DeleteGroup(g);
       
   155 
       
   156 		InvalidateWindowData(GetWCForVT(vt), (vt << 11) | VLW_GROUP_LIST | _current_player);
   137 	}
   157 	}
   138 
   158 
   139 	return 0;
   159 	return 0;
   140 }
   160 }
   141 
   161 
   149  */
   169  */
   150 int32 CmdRenameGroup(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
   170 int32 CmdRenameGroup(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
   151 {
   171 {
   152 	if (!IsValidGroupID(p1) || StrEmpty(_cmd_text)) return CMD_ERROR;
   172 	if (!IsValidGroupID(p1) || StrEmpty(_cmd_text)) return CMD_ERROR;
   153 
   173 
       
   174 	Group *g = GetGroup(p1);
       
   175 	if (g->owner != _current_player) return CMD_ERROR;
       
   176 
   154 	/* Create the name */
   177 	/* Create the name */
   155 	StringID str = AllocateName(_cmd_text, 0);
   178 	StringID str = AllocateName(_cmd_text, 0);
   156 	if (str == STR_NULL) return CMD_ERROR;
   179 	if (str == STR_NULL) return CMD_ERROR;
   157 
   180 
   158 	if (flags & DC_EXEC) {
   181 	if (flags & DC_EXEC) {
   159 		Group *g = GetGroup(p1);
       
   160 
       
   161 		/* Delete the old name */
   182 		/* Delete the old name */
   162 		DeleteName(g->string_id);
   183 		DeleteName(g->string_id);
   163 		/* Assign the new one */
   184 		/* Assign the new one */
   164 		g->string_id = str;
   185 		g->string_id = str;
   165 		g->owner = _current_player;
   186 
       
   187 		InvalidateWindowData(GetWCForVT(g->vehicle_type), (g->vehicle_type << 11) | VLW_GROUP_LIST | _current_player);
   166 	}
   188 	}
   167 
   189 
   168 	return 0;
   190 	return 0;
   169 }
   191 }
   170 
   192 
   180 int32 CmdAddVehicleGroup(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
   202 int32 CmdAddVehicleGroup(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
   181 {
   203 {
   182 	GroupID new_g = p1;
   204 	GroupID new_g = p1;
   183 
   205 
   184 	if (!IsValidVehicleID(p2) || (!IsValidGroupID(new_g) && !IsDefaultGroupID(new_g))) return CMD_ERROR;
   206 	if (!IsValidVehicleID(p2) || (!IsValidGroupID(new_g) && !IsDefaultGroupID(new_g))) return CMD_ERROR;
       
   207 
       
   208 	Group *g = GetGroup(new_g);
       
   209 	if (g->owner != _current_player) return CMD_ERROR;
   185 
   210 
   186 	Vehicle *v = GetVehicle(p2);
   211 	Vehicle *v = GetVehicle(p2);
   187 	if (v->owner != _current_player || (v->type == VEH_TRAIN && !IsFrontEngine(v))) return CMD_ERROR;
   212 	if (v->owner != _current_player || (v->type == VEH_TRAIN && !IsFrontEngine(v))) return CMD_ERROR;
   188 
   213 
   189 	if (flags & DC_EXEC) {
   214 	if (flags & DC_EXEC) {
   203 				break;
   228 				break;
   204 		}
   229 		}
   205 
   230 
   206 		/* Update the Replace Vehicle Windows */
   231 		/* Update the Replace Vehicle Windows */
   207 		InvalidateWindow(WC_REPLACE_VEHICLE, v->type);
   232 		InvalidateWindow(WC_REPLACE_VEHICLE, v->type);
       
   233 		InvalidateWindowData(GetWCForVT(v->type), (v->type << 11) | VLW_GROUP_LIST | _current_player);
   208 	}
   234 	}
   209 
   235 
   210 	return 0;
   236 	return 0;
   211 }
   237 }
   212 
   238 
   240 				for (Vehicle *v2 = GetFirstVehicleFromSharedList(v); v2 != NULL; v2 = v2->next_shared) {
   266 				for (Vehicle *v2 = GetFirstVehicleFromSharedList(v); v2 != NULL; v2 = v2->next_shared) {
   241 					if (v2->group_id != id_g) CmdAddVehicleGroup(tile, flags, id_g, v2->index);
   267 					if (v2->group_id != id_g) CmdAddVehicleGroup(tile, flags, id_g, v2->index);
   242 				}
   268 				}
   243 			}
   269 			}
   244 		}
   270 		}
       
   271 
       
   272 		InvalidateWindowData(GetWCForVT(type), (type << 11) | VLW_GROUP_LIST | _current_player);
   245 	}
   273 	}
   246 
   274 
   247 	return 0;
   275 	return 0;
   248 }
   276 }
   249 
   277 
   257  */
   285  */
   258 int32 CmdRemoveAllVehiclesGroup(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
   286 int32 CmdRemoveAllVehiclesGroup(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
   259 {
   287 {
   260 	VehicleType type = (VehicleType)p2;
   288 	VehicleType type = (VehicleType)p2;
   261 	if (!IsValidGroupID(p1) || !IsPlayerBuildableVehicleType(type)) return CMD_ERROR;
   289 	if (!IsValidGroupID(p1) || !IsPlayerBuildableVehicleType(type)) return CMD_ERROR;
       
   290 
       
   291 	Group *g = GetGroup(p1);
       
   292 	if (g->owner != _current_player) return CMD_ERROR;
   262 
   293 
   263 	if (flags & DC_EXEC) {
   294 	if (flags & DC_EXEC) {
   264 		GroupID old_g = p1;
   295 		GroupID old_g = p1;
   265 		uint subtype = (type == VEH_AIRCRAFT) ? AIR_AIRCRAFT : 0;
   296 		uint subtype = (type == VEH_AIRCRAFT) ? AIR_AIRCRAFT : 0;
   266 		Vehicle *v;
   297 		Vehicle *v;
   274 
   305 
   275 				/* Add The Vehicle to the default group */
   306 				/* Add The Vehicle to the default group */
   276 				CmdAddVehicleGroup(tile, flags, DEFAULT_GROUP, v->index);
   307 				CmdAddVehicleGroup(tile, flags, DEFAULT_GROUP, v->index);
   277 			}
   308 			}
   278 		}
   309 		}
       
   310 
       
   311 		InvalidateWindowData(GetWCForVT(type), (type << 11) | VLW_GROUP_LIST | _current_player);
       
   312 	}
       
   313 
       
   314 	return 0;
       
   315 }
       
   316 
       
   317 
       
   318 /**
       
   319  * (Un)set global replace protection from a group
       
   320  * @param tile unused
       
   321  * @param p1   index of group array
       
   322  * - p1 bit 0-15 : GroupID
       
   323  * @param p2
       
   324  * - p2 bit 0    : 1 to set or 0 to clear protection.
       
   325  */
       
   326 int32 CmdSetGroupReplaceProtection(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
       
   327 {
       
   328 	if (!IsValidGroupID(p1)) return CMD_ERROR;
       
   329 
       
   330 	Group *g = GetGroup(p1);
       
   331 	if (g->owner != _current_player) return CMD_ERROR;
       
   332 
       
   333 	if (flags & DC_EXEC) {
       
   334 		g->replace_protection = HASBIT(p2, 0);
       
   335 
       
   336 		InvalidateWindowData(GetWCForVT(g->vehicle_type), (g->vehicle_type << 11) | VLW_GROUP_LIST | _current_player);
   279 	}
   337 	}
   280 
   338 
   281 	return 0;
   339 	return 0;
   282 }
   340 }
   283 
   341