src/group_cmd.cpp
branchNewGRF_ports
changeset 6872 1c4a4a609f85
parent 6871 5a9dc001e1ad
child 6878 7d1ff2f621c7
equal deleted inserted replaced
6871:5a9dc001e1ad 6872:1c4a4a609f85
     3 /** @file group_cmd.cpp Handling of the engine groups */
     3 /** @file group_cmd.cpp Handling of the engine groups */
     4 
     4 
     5 #include "stdafx.h"
     5 #include "stdafx.h"
     6 #include "openttd.h"
     6 #include "openttd.h"
     7 #include "variables.h"
     7 #include "variables.h"
     8 #include "functions.h"
     8 #include "command_func.h"
     9 #include "player.h"
       
    10 #include "table/strings.h"
       
    11 #include "command.h"
       
    12 #include "vehicle.h"
       
    13 #include "saveload.h"
     9 #include "saveload.h"
    14 #include "debug.h"
    10 #include "debug.h"
    15 #include "group.h"
    11 #include "group.h"
    16 #include "train.h"
    12 #include "train.h"
    17 #include "aircraft.h"
    13 #include "aircraft.h"
    18 #include "string.h"
       
    19 #include "window.h"
       
    20 #include "vehicle_gui.h"
    14 #include "vehicle_gui.h"
    21 #include "strings.h"
       
    22 #include "misc/autoptr.hpp"
    15 #include "misc/autoptr.hpp"
       
    16 #include "strings_func.h"
       
    17 #include "functions.h"
       
    18 #include "window_func.h"
       
    19 #include "vehicle_func.h"
       
    20 #include "autoreplace_base.h"
       
    21 #include "autoreplace_func.h"
       
    22 #include "string_func.h"
       
    23 #include "player_func.h"
       
    24 
       
    25 #include "table/strings.h"
    23 
    26 
    24 /**
    27 /**
    25  * Update the num engines of a groupID. Decrease the old one and increase the new one
    28  * Update the num engines of a groupID. Decrease the old one and increase the new one
    26  * @note called in SetTrainGroupID and UpdateTrainGroupID
    29  * @note called in SetTrainGroupID and UpdateTrainGroupID
    27  * @param i     EngineID we have to update
    30  * @param i     EngineID we have to update
    41 
    44 
    42 
    45 
    43 DEFINE_OLD_POOL_GENERIC(Group, Group)
    46 DEFINE_OLD_POOL_GENERIC(Group, Group)
    44 
    47 
    45 
    48 
    46 Group::Group(StringID str)
    49 Group::Group(PlayerID owner)
    47 {
    50 {
    48 	this->string_id = str;
    51 	this->owner = owner;
    49 }
    52 }
    50 
    53 
    51 Group::~Group()
    54 Group::~Group()
    52 {
    55 {
    53 	DeleteName(this->string_id);
    56 	free(this->name);
    54 	this->string_id = STR_NULL;
    57 	this->owner = INVALID_PLAYER;
    55 }
    58 }
    56 
    59 
    57 bool Group::IsValid() const
    60 bool Group::IsValid() const
    58 {
    61 {
    59 	return this->string_id != STR_NULL;
    62 	return this->owner != INVALID_PLAYER;
    60 }
    63 }
    61 
    64 
    62 void InitializeGroup(void)
    65 void InitializeGroup(void)
    63 {
    66 {
    64 	_Group_pool.CleanPool();
    67 	_Group_pool.CleanPool();
    89 	VehicleType vt = (VehicleType)p1;
    92 	VehicleType vt = (VehicleType)p1;
    90 	if (!IsPlayerBuildableVehicleType(vt)) return CMD_ERROR;
    93 	if (!IsPlayerBuildableVehicleType(vt)) return CMD_ERROR;
    91 
    94 
    92 	AutoPtrT<Group> g_auto_delete;
    95 	AutoPtrT<Group> g_auto_delete;
    93 
    96 
    94 	Group *g = new Group(STR_EMPTY);
    97 	Group *g = new Group(_current_player);
    95 	if (g == NULL) return CMD_ERROR;
    98 	if (g == NULL) return CMD_ERROR;
    96 
    99 
    97 	g_auto_delete = g;
   100 	g_auto_delete = g;
    98 
   101 
    99 	if (flags & DC_EXEC) {
   102 	if (flags & DC_EXEC) {
   100 		g->owner = _current_player;
       
   101 		g->replace_protection = false;
   103 		g->replace_protection = false;
   102 		g->vehicle_type = vt;
   104 		g->vehicle_type = vt;
   103 
   105 
   104 		InvalidateWindowData(GetWCForVT(vt), (vt << 11) | VLW_GROUP_LIST | _current_player);
   106 		InvalidateWindowData(GetWCForVT(vt), (vt << 11) | VLW_GROUP_LIST | _current_player);
   105 
   107 
   183 	Group *g = GetGroup(p1);
   185 	Group *g = GetGroup(p1);
   184 	if (g->owner != _current_player) return CMD_ERROR;
   186 	if (g->owner != _current_player) return CMD_ERROR;
   185 
   187 
   186 	if (!IsUniqueGroupName(_cmd_text)) return_cmd_error(STR_NAME_MUST_BE_UNIQUE);
   188 	if (!IsUniqueGroupName(_cmd_text)) return_cmd_error(STR_NAME_MUST_BE_UNIQUE);
   187 
   189 
   188 	/* Create the name */
       
   189 	StringID str = AllocateName(_cmd_text, 0);
       
   190 	if (str == STR_NULL) return CMD_ERROR;
       
   191 
       
   192 	if (flags & DC_EXEC) {
   190 	if (flags & DC_EXEC) {
   193 		/* Delete the old name */
   191 		/* Delete the old name */
   194 		DeleteName(g->string_id);
   192 		free(g->name);
   195 		/* Assign the new one */
   193 		/* Assign the new one */
   196 		g->string_id = str;
   194 		g->name = strdup(_cmd_text);
   197 
   195 
   198 		InvalidateWindowData(GetWCForVT(g->vehicle_type), (g->vehicle_type << 11) | VLW_GROUP_LIST | _current_player);
   196 		InvalidateWindowData(GetWCForVT(g->vehicle_type), (g->vehicle_type << 11) | VLW_GROUP_LIST | _current_player);
   199 	}
   197 	}
   200 
   198 
   201 	return CommandCost();
   199 	return CommandCost();
   403 
   401 
   404 	/* Update the Replace Vehicle Windows */
   402 	/* Update the Replace Vehicle Windows */
   405 	InvalidateWindow(WC_REPLACE_VEHICLE, VEH_TRAIN);
   403 	InvalidateWindow(WC_REPLACE_VEHICLE, VEH_TRAIN);
   406 }
   404 }
   407 
   405 
   408 
   406 uint GetGroupNumEngines(PlayerID p, GroupID id_g, EngineID id_e)
   409 void RemoveAllGroupsForPlayer(const Player *p)
   407 {
       
   408 	if (IsValidGroupID(id_g)) return GetGroup(id_g)->num_engines[id_e];
       
   409 
       
   410 	uint num = GetPlayer(p)->num_engines[id_e];
       
   411 	if (!IsDefaultGroupID(id_g)) return num;
       
   412 
       
   413 	const Group *g;
       
   414 	FOR_ALL_GROUPS(g) {
       
   415 		if (g->owner == p) num -= g->num_engines[id_e];
       
   416 	}
       
   417 	return num;
       
   418 }
       
   419 
       
   420 void RemoveAllGroupsForPlayer(const PlayerID p)
   410 {
   421 {
   411 	Group *g;
   422 	Group *g;
   412 
   423 
   413 	FOR_ALL_GROUPS(g) {
   424 	FOR_ALL_GROUPS(g) {
   414 		if (p->index == g->owner) delete g;
   425 		if (p == g->owner) delete g;
   415 	}
   426 	}
   416 }
   427 }
   417 
   428 
   418 
   429 
   419 static const SaveLoad _group_desc[] = {
   430 static const SaveLoad _group_desc[] = {
   420   SLE_VAR(Group, string_id,          SLE_UINT16),
   431   SLE_CONDVAR(Group, name,           SLE_NAME,    0, 83),
       
   432   SLE_CONDSTR(Group, name,           SLE_STR, 0, 84, SL_MAX_VERSION),
   421   SLE_VAR(Group, num_vehicle,        SLE_UINT16),
   433   SLE_VAR(Group, num_vehicle,        SLE_UINT16),
   422   SLE_VAR(Group, owner,              SLE_UINT8),
   434   SLE_VAR(Group, owner,              SLE_UINT8),
   423   SLE_VAR(Group, vehicle_type,       SLE_UINT8),
   435   SLE_VAR(Group, vehicle_type,       SLE_UINT8),
   424   SLE_VAR(Group, replace_protection, SLE_BOOL),
   436   SLE_VAR(Group, replace_protection, SLE_BOOL),
   425   SLE_END()
   437   SLE_END()