src/station_cmd.cpp
changeset 6095 55d5c0e6be3a
parent 6088 c11f7acc18bd
child 6118 99a95cda1174
equal deleted inserted replaced
6094:3fd6237f0d94 6095:55d5c0e6be3a
    32 #include "newgrf_callbacks.h"
    32 #include "newgrf_callbacks.h"
    33 #include "newgrf_station.h"
    33 #include "newgrf_station.h"
    34 #include "yapf/yapf.h"
    34 #include "yapf/yapf.h"
    35 #include "date.h"
    35 #include "date.h"
    36 #include "helpers.hpp"
    36 #include "helpers.hpp"
    37 
    37 #include "misc/autoptr.hpp"
    38 #include <memory>     // for auto_ptr
       
    39 
    38 
    40 /**
    39 /**
    41  * Called if a new block is added to the station-pool
    40  * Called if a new block is added to the station-pool
    42  */
    41  */
    43 static void StationPoolNewBlock(uint start_item)
    42 static void StationPoolNewBlock(uint start_item)
   930 		if (st != NULL && st->facilities) st = NULL;
   929 		if (st != NULL && st->facilities) st = NULL;
   931 	}
   930 	}
   932 
   931 
   933 	/* In case of new station if DC_EXEC is NOT set we still need to create the station
   932 	/* In case of new station if DC_EXEC is NOT set we still need to create the station
   934 	 * to test if everything is OK. In this case we need to delete it before return. */
   933 	 * to test if everything is OK. In this case we need to delete it before return. */
   935 	std::auto_ptr<Station> st_auto_delete;
   934 	AutoPtrT<Station> st_auto_delete;
   936 
   935 
   937 	if (st != NULL) {
   936 	if (st != NULL) {
   938 		// Reuse an existing station.
   937 		// Reuse an existing station.
   939 		if (st->owner != OWNER_NONE && st->owner != _current_player)
   938 		if (st->owner != OWNER_NONE && st->owner != _current_player)
   940 			return_cmd_error(STR_3009_TOO_CLOSE_TO_ANOTHER_STATION);
   939 			return_cmd_error(STR_3009_TOO_CLOSE_TO_ANOTHER_STATION);
   953 		/* allocate and initialize new station */
   952 		/* allocate and initialize new station */
   954 		st = new Station(tile_org);
   953 		st = new Station(tile_org);
   955 		if (st == NULL) return CMD_ERROR;
   954 		if (st == NULL) return CMD_ERROR;
   956 
   955 
   957 		/* ensure that in case of error (or no DC_EXEC) the station gets deleted upon return */
   956 		/* ensure that in case of error (or no DC_EXEC) the station gets deleted upon return */
   958 		st_auto_delete = std::auto_ptr<Station>(st);
   957 		st_auto_delete = st;
   959 
   958 
   960 		st->town = ClosestTownFromTile(tile_org, (uint)-1);
   959 		st->town = ClosestTownFromTile(tile_org, (uint)-1);
   961 		if (!GenerateStationName(st, tile_org, 0)) return CMD_ERROR;
   960 		if (!GenerateStationName(st, tile_org, 0)) return CMD_ERROR;
   962 
   961 
   963 		if (IsValidPlayer(_current_player) && (flags & DC_EXEC) != 0) {
   962 		if (IsValidPlayer(_current_player) && (flags & DC_EXEC) != 0) {
  1042 		UpdateStationVirtCoordDirty(st);
  1041 		UpdateStationVirtCoordDirty(st);
  1043 		UpdateStationAcceptance(st, false);
  1042 		UpdateStationAcceptance(st, false);
  1044 		RebuildStationLists();
  1043 		RebuildStationLists();
  1045 		InvalidateWindow(WC_STATION_LIST, st->owner);
  1044 		InvalidateWindow(WC_STATION_LIST, st->owner);
  1046 		/* success, so don't delete the new station */
  1045 		/* success, so don't delete the new station */
  1047 		st_auto_delete.release();
  1046 		st_auto_delete.Release();
  1048 	}
  1047 	}
  1049 
  1048 
  1050 	return cost;
  1049 	return cost;
  1051 }
  1050 }
  1052 
  1051 
  1364 	if (road_stop == NULL) {
  1363 	if (road_stop == NULL) {
  1365 		return_cmd_error(type ? STR_3008B_TOO_MANY_TRUCK_STOPS : STR_3008A_TOO_MANY_BUS_STOPS);
  1364 		return_cmd_error(type ? STR_3008B_TOO_MANY_TRUCK_STOPS : STR_3008A_TOO_MANY_BUS_STOPS);
  1366 	}
  1365 	}
  1367 
  1366 
  1368 	/* ensure that in case of error (or no DC_EXEC) the new road stop gets deleted upon return */
  1367 	/* ensure that in case of error (or no DC_EXEC) the new road stop gets deleted upon return */
  1369 	std::auto_ptr<RoadStop> rs_auto_delete(road_stop);
  1368 	AutoPtrT<RoadStop> rs_auto_delete(road_stop);
  1370 
  1369 
  1371 	if (st != NULL &&
  1370 	if (st != NULL &&
  1372 			GetNumRoadStopsInStation(st, RoadStop::BUS) + GetNumRoadStopsInStation(st, RoadStop::TRUCK) >= RoadStop::LIMIT) {
  1371 			GetNumRoadStopsInStation(st, RoadStop::BUS) + GetNumRoadStopsInStation(st, RoadStop::TRUCK) >= RoadStop::LIMIT) {
  1373 		return_cmd_error(type ? STR_3008B_TOO_MANY_TRUCK_STOPS : STR_3008A_TOO_MANY_BUS_STOPS);
  1372 		return_cmd_error(type ? STR_3008B_TOO_MANY_TRUCK_STOPS : STR_3008A_TOO_MANY_BUS_STOPS);
  1374 	}
  1373 	}
  1375 
  1374 
  1376 	/* In case of new station if DC_EXEC is NOT set we still need to create the station
  1375 	/* In case of new station if DC_EXEC is NOT set we still need to create the station
  1377 	* to test if everything is OK. In this case we need to delete it before return. */
  1376 	* to test if everything is OK. In this case we need to delete it before return. */
  1378 	std::auto_ptr<Station> st_auto_delete;
  1377 	AutoPtrT<Station> st_auto_delete;
  1379 
  1378 
  1380 	if (st != NULL) {
  1379 	if (st != NULL) {
  1381 		if (st->owner != OWNER_NONE && st->owner != _current_player) {
  1380 		if (st->owner != OWNER_NONE && st->owner != _current_player) {
  1382 			return_cmd_error(STR_3009_TOO_CLOSE_TO_ANOTHER_STATION);
  1381 			return_cmd_error(STR_3009_TOO_CLOSE_TO_ANOTHER_STATION);
  1383 		}
  1382 		}
  1389 		/* allocate and initialize new station */
  1388 		/* allocate and initialize new station */
  1390 		st = new Station(tile);
  1389 		st = new Station(tile);
  1391 		if (st == NULL) return CMD_ERROR;
  1390 		if (st == NULL) return CMD_ERROR;
  1392 
  1391 
  1393 		/* ensure that in case of error (or no DC_EXEC) the new station gets deleted upon return */
  1392 		/* ensure that in case of error (or no DC_EXEC) the new station gets deleted upon return */
  1394 		st_auto_delete = std::auto_ptr<Station>(st);
  1393 		st_auto_delete = st;
  1395 
  1394 
  1396 
  1395 
  1397 		Town *t = st->town = ClosestTownFromTile(tile, (uint)-1);
  1396 		Town *t = st->town = ClosestTownFromTile(tile, (uint)-1);
  1398 		if (!GenerateStationName(st, tile, 0)) return CMD_ERROR;
  1397 		if (!GenerateStationName(st, tile, 0)) return CMD_ERROR;
  1399 
  1398 
  1423 		UpdateStationVirtCoordDirty(st);
  1422 		UpdateStationVirtCoordDirty(st);
  1424 		UpdateStationAcceptance(st, false);
  1423 		UpdateStationAcceptance(st, false);
  1425 		RebuildStationLists();
  1424 		RebuildStationLists();
  1426 		InvalidateWindow(WC_STATION_LIST, st->owner);
  1425 		InvalidateWindow(WC_STATION_LIST, st->owner);
  1427 		/* success, so don't delete the new station and the new road stop */
  1426 		/* success, so don't delete the new station and the new road stop */
  1428 		st_auto_delete.release();
  1427 		st_auto_delete.Release();
  1429 		rs_auto_delete.release();
  1428 		rs_auto_delete.Release();
  1430 	}
  1429 	}
  1431 	return cost;
  1430 	return cost;
  1432 }
  1431 }
  1433 
  1432 
  1434 // Remove a bus station
  1433 // Remove a bus station
  1631 		return CMD_ERROR;
  1630 		return CMD_ERROR;
  1632 	}
  1631 	}
  1633 
  1632 
  1634 	/* In case of new station if DC_EXEC is NOT set we still need to create the station
  1633 	/* In case of new station if DC_EXEC is NOT set we still need to create the station
  1635 	 * to test if everything is OK. In this case we need to delete it before return. */
  1634 	 * to test if everything is OK. In this case we need to delete it before return. */
  1636 	std::auto_ptr<Station> st_auto_delete;
  1635 	AutoPtrT<Station> st_auto_delete;
  1637 
  1636 
  1638 	if (st != NULL) {
  1637 	if (st != NULL) {
  1639 		if (st->owner != OWNER_NONE && st->owner != _current_player)
  1638 		if (st->owner != OWNER_NONE && st->owner != _current_player)
  1640 			return_cmd_error(STR_3009_TOO_CLOSE_TO_ANOTHER_STATION);
  1639 			return_cmd_error(STR_3009_TOO_CLOSE_TO_ANOTHER_STATION);
  1641 
  1640 
  1649 		/* allocate and initialize new station */
  1648 		/* allocate and initialize new station */
  1650 		st = new Station(tile);
  1649 		st = new Station(tile);
  1651 		if (st == NULL) return CMD_ERROR;
  1650 		if (st == NULL) return CMD_ERROR;
  1652 
  1651 
  1653 		/* ensure that in case of error (or no DC_EXEC) the station gets deleted upon return */
  1652 		/* ensure that in case of error (or no DC_EXEC) the station gets deleted upon return */
  1654 		st_auto_delete = std::auto_ptr<Station>(st);
  1653 		st_auto_delete = st;
  1655 
  1654 
  1656 		st->town = t;
  1655 		st->town = t;
  1657 
  1656 
  1658 		if (IsValidPlayer(_current_player) && (flags & DC_EXEC) != 0) {
  1657 		if (IsValidPlayer(_current_player) && (flags & DC_EXEC) != 0) {
  1659 			SETBIT(t->have_ratings, _current_player);
  1658 			SETBIT(t->have_ratings, _current_player);
  1697 		UpdateStationVirtCoordDirty(st);
  1696 		UpdateStationVirtCoordDirty(st);
  1698 		UpdateStationAcceptance(st, false);
  1697 		UpdateStationAcceptance(st, false);
  1699 		RebuildStationLists();
  1698 		RebuildStationLists();
  1700 		InvalidateWindow(WC_STATION_LIST, st->owner);
  1699 		InvalidateWindow(WC_STATION_LIST, st->owner);
  1701 		/* success, so don't delete the new station */
  1700 		/* success, so don't delete the new station */
  1702 		st_auto_delete.release();
  1701 		st_auto_delete.Release();
  1703 	}
  1702 	}
  1704 
  1703 
  1705 	return cost;
  1704 	return cost;
  1706 }
  1705 }
  1707 
  1706 
  1769 	/* allocate and initialize new station */
  1768 	/* allocate and initialize new station */
  1770 	st = new Station(tile);
  1769 	st = new Station(tile);
  1771 	if (st == NULL) return CMD_ERROR;
  1770 	if (st == NULL) return CMD_ERROR;
  1772 
  1771 
  1773 	/* ensure that in case of error (or no DC_EXEC) the station gets deleted upon return */
  1772 	/* ensure that in case of error (or no DC_EXEC) the station gets deleted upon return */
  1774 	std::auto_ptr<Station> st_auto_delete(st);
  1773 	AutoPtrT<Station> st_auto_delete(st);
  1775 
  1774 
  1776 	st->town = ClosestTownFromTile(tile, (uint)-1);
  1775 	st->town = ClosestTownFromTile(tile, (uint)-1);
  1777 	st->sign.width_1 = 0;
  1776 	st->sign.width_1 = 0;
  1778 
  1777 
  1779 	if (!GenerateStationName(st, tile, 4)) return CMD_ERROR;
  1778 	if (!GenerateStationName(st, tile, 4)) return CMD_ERROR;
  1793 		UpdateStationVirtCoordDirty(st);
  1792 		UpdateStationVirtCoordDirty(st);
  1794 		UpdateStationAcceptance(st, false);
  1793 		UpdateStationAcceptance(st, false);
  1795 		RebuildStationLists();
  1794 		RebuildStationLists();
  1796 		InvalidateWindow(WC_STATION_LIST, st->owner);
  1795 		InvalidateWindow(WC_STATION_LIST, st->owner);
  1797 		/* success, so don't delete the new station */
  1796 		/* success, so don't delete the new station */
  1798 		st_auto_delete.release();
  1797 		st_auto_delete.Release();
  1799 	}
  1798 	}
  1800 
  1799 
  1801 	return _price.build_dock;
  1800 	return _price.build_dock;
  1802 }
  1801 }
  1803 
  1802 
  1913 		if (st!=NULL && st->facilities) st = NULL;
  1912 		if (st!=NULL && st->facilities) st = NULL;
  1914 	}
  1913 	}
  1915 
  1914 
  1916 	/* In case of new station if DC_EXEC is NOT set we still need to create the station
  1915 	/* In case of new station if DC_EXEC is NOT set we still need to create the station
  1917 	* to test if everything is OK. In this case we need to delete it before return. */
  1916 	* to test if everything is OK. In this case we need to delete it before return. */
  1918 	std::auto_ptr<Station> st_auto_delete;
  1917 	AutoPtrT<Station> st_auto_delete;
  1919 
  1918 
  1920 	if (st != NULL) {
  1919 	if (st != NULL) {
  1921 		if (st->owner != OWNER_NONE && st->owner != _current_player)
  1920 		if (st->owner != OWNER_NONE && st->owner != _current_player)
  1922 			return_cmd_error(STR_3009_TOO_CLOSE_TO_ANOTHER_STATION);
  1921 			return_cmd_error(STR_3009_TOO_CLOSE_TO_ANOTHER_STATION);
  1923 
  1922 
  1928 		/* allocate and initialize new station */
  1927 		/* allocate and initialize new station */
  1929 		st = new Station(tile);
  1928 		st = new Station(tile);
  1930 		if (st == NULL) return CMD_ERROR;
  1929 		if (st == NULL) return CMD_ERROR;
  1931 
  1930 
  1932 		/* ensure that in case of error (or no DC_EXEC) the station gets deleted upon return */
  1931 		/* ensure that in case of error (or no DC_EXEC) the station gets deleted upon return */
  1933 		st_auto_delete = std::auto_ptr<Station>(st);
  1932 		st_auto_delete = st;
  1934 
  1933 
  1935 		Town *t = st->town = ClosestTownFromTile(tile, (uint)-1);
  1934 		Town *t = st->town = ClosestTownFromTile(tile, (uint)-1);
  1936 
  1935 
  1937 		if (IsValidPlayer(_current_player) && (flags & DC_EXEC) != 0) {
  1936 		if (IsValidPlayer(_current_player) && (flags & DC_EXEC) != 0) {
  1938 			SETBIT(t->have_ratings, _current_player);
  1937 			SETBIT(t->have_ratings, _current_player);
  1954 		UpdateStationVirtCoordDirty(st);
  1953 		UpdateStationVirtCoordDirty(st);
  1955 		UpdateStationAcceptance(st, false);
  1954 		UpdateStationAcceptance(st, false);
  1956 		RebuildStationLists();
  1955 		RebuildStationLists();
  1957 		InvalidateWindow(WC_STATION_LIST, st->owner);
  1956 		InvalidateWindow(WC_STATION_LIST, st->owner);
  1958 		/* success, so don't delete the new station */
  1957 		/* success, so don't delete the new station */
  1959 		st_auto_delete.release();
  1958 		st_auto_delete.Release();
  1960 	}
  1959 	}
  1961 	return _price.build_dock;
  1960 	return _price.build_dock;
  1962 }
  1961 }
  1963 
  1962 
  1964 static int32 RemoveDock(Station *st, uint32 flags)
  1963 static int32 RemoveDock(Station *st, uint32 flags)