src/station_cmd.cpp
changeset 9281 d8cd9ac52a68
parent 9259 088d3649dd4f
child 9302 4aa3c4a44b16
equal deleted inserted replaced
9280:9c03416d26b1 9281:d8cd9ac52a68
     9 #include "cmd_helper.h"
     9 #include "cmd_helper.h"
    10 #include "debug.h"
    10 #include "debug.h"
    11 #include "tile_cmd.h"
    11 #include "tile_cmd.h"
    12 #include "landscape.h"
    12 #include "landscape.h"
    13 #include "station_map.h"
    13 #include "station_map.h"
    14 #include "station.h"
       
    15 #include "viewport_func.h"
    14 #include "viewport_func.h"
    16 #include "command_func.h"
    15 #include "command_func.h"
    17 #include "town.h"
    16 #include "town.h"
    18 #include "news_func.h"
    17 #include "news_func.h"
    19 #include "saveload.h"
    18 #include "saveload.h"
    69 	}
    68 	}
    70 
    69 
    71 	return false;
    70 	return false;
    72 }
    71 }
    73 
    72 
    74 RoadStop* GetRoadStopByTile(TileIndex tile, RoadStop::Type type)
    73 RoadStop* GetRoadStopByTile(TileIndex tile, RoadStopType type)
    75 {
    74 {
    76 	const Station* st = GetStationByTile(tile);
    75 	const Station* st = GetStationByTile(tile);
    77 
    76 
    78 	for (RoadStop *rs = st->GetPrimaryRoadStop(type);; rs = rs->next) {
    77 	for (RoadStop *rs = st->GetPrimaryRoadStop(type);; rs = rs->next) {
    79 		if (rs->xy == tile) return rs;
    78 		if (rs->xy == tile) return rs;
    80 		assert(rs->next != NULL);
    79 		assert(rs->next != NULL);
    81 	}
    80 	}
    82 }
    81 }
    83 
    82 
    84 
    83 
    85 static uint GetNumRoadStopsInStation(const Station* st, RoadStop::Type type)
    84 static uint GetNumRoadStopsInStation(const Station* st, RoadStopType type)
    86 {
    85 {
    87 	uint num = 0;
    86 	uint num = 0;
    88 
    87 
    89 	assert(st != NULL);
    88 	assert(st != NULL);
    90 	for (const RoadStop *rs = st->GetPrimaryRoadStop(type); rs != NULL; rs = rs->next) {
    89 	for (const RoadStop *rs = st->GetPrimaryRoadStop(type); rs != NULL; rs = rs->next) {
  1296 
  1295 
  1297 	return cost;
  1296 	return cost;
  1298 }
  1297 }
  1299 
  1298 
  1300 /**
  1299 /**
  1301  * @param truck_station Determines whether a stop is RoadStop::BUS or RoadStop::TRUCK
  1300  * @param truck_station Determines whether a stop is ROADSTOP_BUS or ROADSTOP_TRUCK
  1302  * @param st The Station to do the whole procedure for
  1301  * @param st The Station to do the whole procedure for
  1303  * @return a pointer to where to link a new RoadStop*
  1302  * @return a pointer to where to link a new RoadStop*
  1304  */
  1303  */
  1305 static RoadStop **FindRoadStopSpot(bool truck_station, Station* st)
  1304 static RoadStop **FindRoadStopSpot(bool truck_station, Station* st)
  1306 {
  1305 {
  1395 
  1394 
  1396 	/* ensure that in case of error (or no DC_EXEC) the new road stop gets deleted upon return */
  1395 	/* ensure that in case of error (or no DC_EXEC) the new road stop gets deleted upon return */
  1397 	AutoPtrT<RoadStop> rs_auto_delete(road_stop);
  1396 	AutoPtrT<RoadStop> rs_auto_delete(road_stop);
  1398 
  1397 
  1399 	if (st != NULL &&
  1398 	if (st != NULL &&
  1400 			GetNumRoadStopsInStation(st, RoadStop::BUS) + GetNumRoadStopsInStation(st, RoadStop::TRUCK) >= RoadStop::LIMIT) {
  1399 			GetNumRoadStopsInStation(st, ROADSTOP_BUS) + GetNumRoadStopsInStation(st, ROADSTOP_TRUCK) >= RoadStop::LIMIT) {
  1401 		return_cmd_error(type ? STR_TOO_MANY_TRUCK_STOPS : STR_TOO_MANY_BUS_STOPS);
  1400 		return_cmd_error(type ? STR_TOO_MANY_TRUCK_STOPS : STR_TOO_MANY_BUS_STOPS);
  1402 	}
  1401 	}
  1403 
  1402 
  1404 	/* In case of new station if DC_EXEC is NOT set we still need to create the station
  1403 	/* In case of new station if DC_EXEC is NOT set we still need to create the station
  1405 	 * to test if everything is OK. In this case we need to delete it before return. */
  1404 	 * to test if everything is OK. In this case we need to delete it before return. */
  1440 		/*initialize an empty station */
  1439 		/*initialize an empty station */
  1441 		st->AddFacility((type) ? FACIL_TRUCK_STOP : FACIL_BUS_STOP, tile);
  1440 		st->AddFacility((type) ? FACIL_TRUCK_STOP : FACIL_BUS_STOP, tile);
  1442 
  1441 
  1443 		st->rect.BeforeAddTile(tile, StationRect::ADD_TRY);
  1442 		st->rect.BeforeAddTile(tile, StationRect::ADD_TRY);
  1444 
  1443 
  1445 		RoadStop::Type rs_type = type ? RoadStop::TRUCK : RoadStop::BUS;
  1444 		RoadStopType rs_type = type ? ROADSTOP_TRUCK : ROADSTOP_BUS;
  1446 		if (is_drive_through) {
  1445 		if (is_drive_through) {
  1447 			MakeDriveThroughRoadStop(tile, st->owner, st->index, rs_type, rts, (Axis)p1, town_owned_road);
  1446 			MakeDriveThroughRoadStop(tile, st->owner, st->index, rs_type, rts, (Axis)p1, town_owned_road);
  1448 		} else {
  1447 		} else {
  1449 			MakeRoadStop(tile, st->owner, st->index, rs_type, rts, (DiagDirection)p1);
  1448 			MakeRoadStop(tile, st->owner, st->index, rs_type, rts, (DiagDirection)p1);
  1450 		}
  1449 		}
  1486 
  1485 
  1487 	RoadStop **primary_stop;
  1486 	RoadStop **primary_stop;
  1488 	RoadStop *cur_stop;
  1487 	RoadStop *cur_stop;
  1489 	if (is_truck) { // truck stop
  1488 	if (is_truck) { // truck stop
  1490 		primary_stop = &st->truck_stops;
  1489 		primary_stop = &st->truck_stops;
  1491 		cur_stop = GetRoadStopByTile(tile, RoadStop::TRUCK);
  1490 		cur_stop = GetRoadStopByTile(tile, ROADSTOP_TRUCK);
  1492 	} else {
  1491 	} else {
  1493 		primary_stop = &st->bus_stops;
  1492 		primary_stop = &st->bus_stops;
  1494 		cur_stop = GetRoadStopByTile(tile, RoadStop::BUS);
  1493 		cur_stop = GetRoadStopByTile(tile, ROADSTOP_BUS);
  1495 	}
  1494 	}
  1496 
  1495 
  1497 	assert(cur_stop != NULL);
  1496 	assert(cur_stop != NULL);
  1498 
  1497 
  1499 	/* don't do the check for drive-through road stops when company bankrupts */
  1498 	/* don't do the check for drive-through road stops when company bankrupts */
  2438 					byte side = ((DirToDiagDir(v->direction) == ReverseDiagDir(GetRoadStopDir(tile))) == (v->u.road.overtaking == 0)) ? 0 : 1;
  2437 					byte side = ((DirToDiagDir(v->direction) == ReverseDiagDir(GetRoadStopDir(tile))) == (v->u.road.overtaking == 0)) ? 0 : 1;
  2439 
  2438 
  2440 					if (!rs->IsFreeBay(side)) return VETSB_CANNOT_ENTER;
  2439 					if (!rs->IsFreeBay(side)) return VETSB_CANNOT_ENTER;
  2441 
  2440 
  2442 					/* Check if the vehicle is stopping at this road stop */
  2441 					/* Check if the vehicle is stopping at this road stop */
  2443 					if (GetRoadStopType(tile) == (IsCargoInClass(v->cargo_type, CC_PASSENGERS) ? RoadStop::BUS : RoadStop::TRUCK) &&
  2442 					if (GetRoadStopType(tile) == (IsCargoInClass(v->cargo_type, CC_PASSENGERS) ? ROADSTOP_BUS : ROADSTOP_TRUCK) &&
  2444 							v->current_order.dest == GetStationIndex(tile)) {
  2443 							v->current_order.dest == GetStationIndex(tile)) {
  2445 						SetBit(v->u.road.state, RVS_IS_STOPPING);
  2444 						SetBit(v->u.road.state, RVS_IS_STOPPING);
  2446 						rs->AllocateDriveThroughBay(side);
  2445 						rs->AllocateDriveThroughBay(side);
  2447 					}
  2446 					}
  2448 
  2447 
  2911 		RebuildStationLists();
  2910 		RebuildStationLists();
  2912 		InvalidateWindowClasses(WC_STATION_LIST);
  2911 		InvalidateWindowClasses(WC_STATION_LIST);
  2913 	} else {
  2912 	} else {
  2914 		if (IsDriveThroughStopTile(tile)) {
  2913 		if (IsDriveThroughStopTile(tile)) {
  2915 			/* Remove the drive-through road stop */
  2914 			/* Remove the drive-through road stop */
  2916 			DoCommand(tile, 0, (GetStationType(tile) == STATION_TRUCK) ? RoadStop::TRUCK : RoadStop::BUS, DC_EXEC | DC_BANKRUPT, CMD_REMOVE_ROAD_STOP);
  2915 			DoCommand(tile, 0, (GetStationType(tile) == STATION_TRUCK) ? ROADSTOP_TRUCK : ROADSTOP_BUS, DC_EXEC | DC_BANKRUPT, CMD_REMOVE_ROAD_STOP);
  2917 			assert(IsTileType(tile, MP_ROAD));
  2916 			assert(IsTileType(tile, MP_ROAD));
  2918 			/* Change owner of tile and all roadtypes */
  2917 			/* Change owner of tile and all roadtypes */
  2919 			ChangeTileOwner(tile, old_player, new_player);
  2918 			ChangeTileOwner(tile, old_player, new_player);
  2920 		} else {
  2919 		} else {
  2921 			DoCommand(tile, 0, 0, DC_EXEC | DC_BANKRUPT, CMD_LANDSCAPE_CLEAR);
  2920 			DoCommand(tile, 0, 0, DC_EXEC | DC_BANKRUPT, CMD_LANDSCAPE_CLEAR);