Index: src/settings.cpp
===================================================================
--- src/settings.cpp	(revision 17973)
+++ src/settings.cpp	(working copy)
@@ -66,6 +66,8 @@
 
 #include "void_map.h"
 #include "station_base.h"
+#include "infrastructure_func.h"
+#include "core/mem_func.hpp"
 
 #include "table/strings.h"
 #include "table/settings.h"
@@ -969,6 +971,97 @@
 	return true;
 }
 
+/**
+ * Copy the global sharing settings to all companies.
+ * @param force_update_signals Force updating all block signals
+ */
+static void CopySharingSettingsToCompanies(bool force_update_signals)
+{
+	Company *c;
+	byte changed_vehtypes = 0;
+	FOR_ALL_COMPANIES(c) {
+		changed_vehtypes |= c->sharing_current.CopyFromGameSettings();
+		c->sharing_future.Copy(&c->sharing_current);
+		c->sharing_delay = 0;
+	}
+	HandleSharingChange(changed_vehtypes, force_update_signals);
+}
+
+/**
+ * Update the sharing settings of all companies if sharing settings are globally fixed
+ * @param p1 unused
+ * @return always true
+ */
+static bool UpdateAllSharingSettings(int32 p1)
+{
+	if (_settings_game.sharing.sharing_mode == SHARING_GLOBAL && _game_mode != GM_MENU) {
+		CopySharingSettingsToCompanies(false);
+	}
+	return true;
+}
+
+/**
+ * Update all sharing settings if sharing is globally enabled / disabled.
+ * @param p1 New value of the setting
+ * @return always true
+ */
+static bool UpdateSharingEnabled(int32 p1)
+{
+	if (_game_mode == GM_MENU) return true;
+
+	switch (p1) {
+		case SHARING_NONE: {
+			Company *c;
+			FOR_ALL_COMPANIES(c) {
+				/* Disable all sharing options */
+				MemSetT(c->sharing_current.option, 0, NUM_SHARING_VEHTYPES);
+				/* Allow only our own company */
+				c->sharing_current.allowed_companies = 1 << c->index;
+				/* Make future settings the same as current ones */
+				c->sharing_future.Copy(&c->sharing_current);
+				c->sharing_delay = 0;
+			}
+			/* Update everything */
+			HandleSharingChange(ALL_SHARING_VEHTYPES, true);
+			break;
+		}
+		case SHARING_GLOBAL:
+			CopySharingSettingsToCompanies(true);
+			break;
+		case SHARING_INDIVIDUAL:
+			/* Update all block signals */
+			HandleSharingChange(0, true);
+			break;
+	}
+
+	SetWindowClassesDirty(WC_FINANCES);
+	SetWindowClassesDirty(WC_COMPANY);
+	if (p1 != SHARING_INDIVIDUAL) DeleteWindowByClass(WC_SHARING_OPTIONS);
+
+	return true;
+}
+
+/**
+ * Cap sharing_delay for all companies to the new delay setting
+ * @param p1 new delay setting
+ * @return always true
+ */
+static bool UpdateSharingDelay(int32 p1)
+{
+	if (_settings_game.sharing.sharing_mode != SHARING_INDIVIDUAL) return true;
+
+	byte changed_vehtypes = 0;
+	Company *c;
+	FOR_ALL_COMPANIES(c) {
+		c->sharing_delay = min(c->sharing_delay, p1);
+		/* Delay set to 0 and we have changes pending, apply them now */
+		if (p1 == 0 && !c->sharing_current.Equals(&c->sharing_future)) changed_vehtypes |= c->sharing_current.Copy(&c->sharing_future);
+	}
+	HandleSharingChange(changed_vehtypes);
+	InvalidateWindowClassesData(WC_SHARING_OPTIONS);
+	return true;
+}
+
 #ifdef ENABLE_NETWORK
 
 static bool UpdateClientName(int32 p1)
Index: src/lang/english.txt
===================================================================
--- src/lang/english.txt	(revision 17973)
+++ src/lang/english.txt	(working copy)
@@ -652,6 +652,8 @@
 
 STR_SMALLMAP_LEGENDA_ROADS                                      :{TINYFONT}{BLACK}Roads
 STR_SMALLMAP_LEGENDA_RAILROADS                                  :{TINYFONT}{BLACK}Railways
+STR_SMALLMAP_LEGENDA_RAILROADS_ACCESSIBLE                       :{TINYFONT}{BLACK}Railways (accessible)
+STR_SMALLMAP_LEGENDA_RAILROADS_INACCESSIBLE                     :{TINYFONT}{BLACK}Railways (not accessible)
 STR_SMALLMAP_LEGENDA_STATIONS_AIRPORTS_DOCKS                    :{TINYFONT}{BLACK}Stations/Airports/Docks
 STR_SMALLMAP_LEGENDA_BUILDINGS_INDUSTRIES                       :{TINYFONT}{BLACK}Buildings/Industries
 STR_SMALLMAP_LEGENDA_VEHICLES                                   :{TINYFONT}{BLACK}Vehicles
@@ -787,6 +789,12 @@
 STR_NEWS_SHIP_IS_WAITING                                        :{WHITE}{VEHICLE} is waiting in depot
 STR_NEWS_AIRCRAFT_IS_WAITING                                    :{WHITE}{VEHICLE} is waiting in the aircraft hangar
 
+STR_NEWS_SHARING_TITLE                                          :{BLACK}{BIGFONT}Infrastructure sharing news!
+STR_NEWS_SHARING_CHANGE_ANNOUNCED                               :{BLACK}{BIGFONT}{RAW_STRING} announced to change its infrastructure sharing settings in {NUM} month{P "" s}!
+STR_NEWS_SHARING_CHANGE_HAPPENED                                :{BLACK}{BIGFONT}{RAW_STRING} has changed its infrastructure sharing settings!
+STR_NEWS_SHARING_SOLD_VEHICLES                                  :{BLACK}{BIGFONT}Because of a change in infrastructure sharing settings, {COMMA} of your vehicles {P was were} sold!
+STR_NEWS_SHARING_UNABLE_TO_PAY                                  :{BLACK}{BIGFONT}{RAW_STRING} is in debt and cannot pay sharing fees to you!
+
 # Start of order review system.
 # DON'T ADD OR REMOVE LINES HERE
 STR_NEWS_VEHICLE_HAS_TOO_FEW_ORDERS                             :{WHITE}{VEHICLE} has too few orders in the schedule
@@ -1259,6 +1267,26 @@
 STR_CONFIG_SETTING_CITY_SIZE_MULTIPLIER                         :{LTBLUE}Initial city size multiplier: {ORANGE}{STRING1}
 STR_CONFIG_SETTING_MODIFIED_ROAD_REBUILD                        :{LTBLUE}Remove absurd road-elements during the road construction: {ORANGE}{STRING1}
 
+STR_CONFIG_SETTING_SHARING_ENABLE                               :{LTBLUE}Infrastructure sharing mode: {ORANGE}{STRING1}
+STR_CONFIG_SETTING_SHARING_ENABLE_OFF                           :Off
+STR_CONFIG_SETTING_SHARING_ENABLE_GLOBAL                        :On, global settings
+STR_CONFIG_SETTING_SHARING_ENABLE_INDIVIDUAL                    :On, individual settings
+STR_CONFIG_SETTING_SHARING_INDIVIDUAL_TIME                      :{LTBLUE}Delay for new individual settings to take effect: {ORANGE}{STRING1} months
+STR_CONFIG_SETTING_SHARING_PAYMENT_DEBT                         :{LTBLUE}Allow companies in debt to pay sharing fees: {ORANGE}{STRING1}
+STR_CONFIG_SETTING_SHARING_RAIL                                 :{LTBLUE}Railway sharing: {ORANGE}{STRING1}
+STR_CONFIG_SETTING_SHARING_RAIL_NONE                            :None
+STR_CONFIG_SETTING_SHARING_RAIL_RAILWAY                         :Only railway
+STR_CONFIG_SETTING_SHARING_RAIL_RAILSTATION                     :Railway and stations
+STR_CONFIG_SETTING_SHARING_RAIL_RAILDEPOT                       :Railway and rail depots
+STR_CONFIG_SETTING_SHARING_RAIL_ALL                             :Everything
+STR_CONFIG_SETTING_SHARING_ROAD                                 :{LTBLUE}Road stops and depots: {ORANGE}{STRING1}
+STR_CONFIG_SETTING_SHARING_WATER                                :{LTBLUE}Docks and ship depots: {ORANGE}{STRING1}
+STR_CONFIG_SETTING_SHARING_AIR                                  :{LTBLUE}Airports: {ORANGE}{STRING1}
+STR_CONFIG_SETTING_SHARING_RAIL_FEE                             :{LTBLUE}Monthly track toll: {ORANGE}{STRING1} per train tile
+STR_CONFIG_SETTING_SHARING_ROAD_FEE                             :{LTBLUE}Stopping fee for road vehicles: {ORANGE}{STRING1} per day
+STR_CONFIG_SETTING_SHARING_WATER_FEE                            :{LTBLUE}Docking fee for ship: {ORANGE}{STRING1} per day
+STR_CONFIG_SETTING_SHARING_AIR_FEE                              :{LTBLUE}Terminal fee for aircraft: {ORANGE}{STRING1} per day
+
 STR_CONFIG_SETTING_GUI                                          :{ORANGE}Interface
 STR_CONFIG_SETTING_CONSTRUCTION                                 :{ORANGE}Construction
 STR_CONFIG_SETTING_VEHICLES                                     :{ORANGE}Vehicles
@@ -1270,6 +1298,7 @@
 STR_CONFIG_SETTING_CONSTRUCTION_SIGNALS                         :{ORANGE}Signals
 STR_CONFIG_SETTING_STATIONS_CARGOHANDLING                       :{ORANGE}Cargo handling
 STR_CONFIG_SETTING_AI_NPC                                       :{ORANGE}Computer players
+STR_CONFIG_SETTING_AI_SHARING                                   :{ORANGE}Infrastructure sharing
 STR_CONFIG_SETTING_VEHICLES_AUTORENEW                           :{ORANGE}Autorenew
 STR_CONFIG_SETTING_VEHICLES_SERVICING                           :{ORANGE}Servicing
 STR_CONFIG_SETTING_VEHICLES_ROUTING                             :{ORANGE}Routing
@@ -2502,6 +2531,8 @@
 STR_FINANCES_SECTION_SHIP_INCOME                                :{GOLD}Ship Income
 STR_FINANCES_SECTION_LOAN_INTEREST                              :{GOLD}Loan Interest
 STR_FINANCES_SECTION_OTHER                                      :{GOLD}Other
+STR_FINANCES_SECTION_INFRASTRUCTURE_COSTS                       :{GOLD}Infrastructure Costs
+STR_FINANCES_SECTION_INFRASTRUCTURE_INCOME                      :{GOLD}Infrastructure Income
 STR_FINANCES_NEGATIVE_INCOME                                    :{BLACK}-{CURRENCY}
 STR_FINANCES_POSITIVE_INCOME                                    :{BLACK}+{CURRENCY}
 STR_FINANCES_TOTAL_CAPTION                                      :{WHITE}Total:
@@ -2535,6 +2566,9 @@
 STR_COMPANY_VIEW_RELOCATE_HQ                                    :{BLACK}Relocate HQ
 STR_COMPANY_VIEW_RELOCATE_COMPANY_HEADQUARTERS                  :{BLACK}Rebuild company headquarters elsewhere for 1% cost of company value
 
+STR_COMPANY_VIEW_SHARING_BUTTON                                 :{BLACK}Sharing
+STR_COMPANY_VIEW_SHARING_TOOLTIP                                :{BLACK}Set the sharing options for your infrastructure
+
 STR_COMPANY_VIEW_NEW_FACE_BUTTON                                :{BLACK}New Face
 STR_COMPANY_VIEW_NEW_FACE_TOOLTIP                               :{BLACK}Select new face for manager
 STR_COMPANY_VIEW_COLOUR_SCHEME_BUTTON                           :{BLACK}Colour Scheme
@@ -2554,6 +2588,35 @@
 
 STR_BUY_COMPANY_MESSAGE                                         :{WHITE}We are looking for a transport company to take-over our company.{}{}Do you want to purchase {COMPANY} for {CURRENCY}?
 
+#Infrastructure sharing window
+STR_SHARING_CAPTION                                             :{WHITE}{COMPANY} - Infrastructure sharing
+STR_SHARING_PANEL_TOOLTIP                                       :{BLACK}View and edit the infrastructure sharing settings for this company.
+STR_SHARING_OPTIONS                                             :{BLACK}Options
+STR_SHARING_OPTIONS_TOOLTIP                                     :{BLACK}View and edit the sharing options
+STR_SHARING_COMPANIES                                           :{BLACK}Companies
+STR_SHARING_COMPANIES_TOOLTIP                                   :{BLACK}View and edit which companies are allowed on infrastructure of this company
+
+STR_SHARING_CURRENT                                             :{BLACK}Currently:
+STR_SHARING_FUTURE                                              :{BLACK}In the future:
+STR_SHARING_SETTING_TRACK                                       :{BLACK}Tracks:
+STR_SHARING_SETTING_DEPOT                                       :{BLACK}Depots:
+STR_SHARING_SETTING_STATION                                     :{BLACK}Stations:
+STR_SHARING_SETTING_ROADSTOP                                    :{BLACK}Road stops:
+STR_SHARING_SETTING_DOCK                                        :{BLACK}Docks:
+STR_SHARING_SETTING_AIRPORT                                     :{BLACK}Airports:
+STR_SHARING_SETTING_MONTHLY_TOLL                                :{BLACK}Monthly toll per train tile:
+STR_SHARING_SETTING_FEE                                         :{BLACK}Loading fee per day:
+STR_SHARING_SETTING_COMPANY                                     :{BLACK}{COMPANY}:
+STR_SHARING_SETTING_NO_COMPANIES                                :{BLACK}No other companies
+
+STR_SHARING_VALUE_ALLOWED                                       :{ORANGE}Allowed
+STR_SHARING_VALUE_NOT_ALLOWED                                   :{ORANGE}Not allowed
+
+STR_SHARING_CHANGES_TAKE_EFFECT                                 :{WHITE}Changes will take effect in:
+STR_SHARING_TAKE_EFFECT_MONTHS                                  :{ORANGE}{NUM} month{P "" s}
+
+STR_SHARING_CHANGE_FEE_CAPTION                                  :{WHITE}Change the sharing fee
+
 # Industry directory
 STR_INDUSTRY_DIRECTORY_CAPTION                                  :{WHITE}Industries
 STR_INDUSTRY_DIRECTORY_NONE                                     :{ORANGE}- None -
@@ -3284,6 +3347,7 @@
 STR_ERROR_CAN_T_BUY_25_SHARE_IN_THIS                            :{WHITE}Can't buy 25% share in this company...
 STR_ERROR_CAN_T_SELL_25_SHARE_IN                                :{WHITE}Can't sell 25% share in this company...
 STR_ERROR_PROTECTED                                             :{WHITE}This company is not old enough to trade shares yet...
+STR_ERROR_CAN_T_CHANGE_SHARING_SETTING                          :{WHITE}Can't change the infrastructure sharing settings...
 
 # Town related errors
 STR_ERROR_CAN_T_GENERATE_TOWN                                   :{WHITE}Can't build any towns
Index: src/settings_gui.cpp
===================================================================
--- src/settings_gui.cpp	(revision 17973)
+++ src/settings_gui.cpp	(working copy)
@@ -1342,8 +1342,25 @@
 /** Computer players sub-page */
 static SettingsPage _settings_ai_npc_page = {_settings_ai_npc, lengthof(_settings_ai_npc)};
 
+static SettingEntry _settings_ai_sharing[] = {
+	SettingEntry("sharing.sharing_mode"),
+	SettingEntry("sharing.new_individual_delay"),
+	SettingEntry("sharing.payment_in_debt"),
+	SettingEntry("sharing.rail_sharing"),
+	SettingEntry("sharing.road_sharing"),
+	SettingEntry("sharing.water_sharing"),
+	SettingEntry("sharing.air_sharing"),
+	SettingEntry("sharing.rail_fee"),
+	SettingEntry("sharing.road_fee"),
+	SettingEntry("sharing.water_fee"),
+	SettingEntry("sharing.air_fee"),
+};
+/** Infrastructure sharing sub-page */
+static SettingsPage _settings_ai_sharing_page = {_settings_ai_sharing, lengthof(_settings_ai_sharing)};
+
 static SettingEntry _settings_ai[] = {
 	SettingEntry(&_settings_ai_npc_page, STR_CONFIG_SETTING_AI_NPC),
+	SettingEntry(&_settings_ai_sharing_page, STR_CONFIG_SETTING_AI_SHARING),
 	SettingEntry("economy.give_money"),
 	SettingEntry("economy.allow_shares"),
 };
Index: src/npf.cpp
===================================================================
--- src/npf.cpp	(revision 17973)
+++ src/npf.cpp	(working copy)
@@ -21,6 +21,7 @@
 #include "pbs.h"
 #include "pathfind.h"
 #include "train.h"
+#include "infrastructure_func.h"
 
 static AyStar _npf_aystar;
 
@@ -553,25 +554,31 @@
  */
 static bool CanEnterTileOwnerCheck(Owner owner, TileIndex tile, DiagDirection enterdir)
 {
-	if (IsTileType(tile, MP_RAILWAY) || // Rail tile (also rail depot)
-			HasStationTileRail(tile) ||     // Rail station tile/waypoint
-			IsRoadDepotTile(tile) ||        // Road depot tile
-			IsStandardRoadStopTile(tile)) { // Road station tile (but not drive-through stops)
-		return IsTileOwner(tile, owner);  // You need to own these tiles entirely to use them
-	}
+	switch (GetTileType(tile)) {
+		case MP_RAILWAY:
+			/* use the generic railway check */
+			return IsRailwayTileUsageAllowed(tile, owner);
 
-	switch (GetTileType(tile)) {
 		case MP_ROAD:
+			/* road depot */
+			if (IsRoadDepot(tile)) return IsDepotUsageAllowed(tile, owner, VEH_ROAD);
 			/* rail-road crossing : are we looking at the railway part? */
 			if (IsLevelCrossing(tile) &&
 					DiagDirToAxis(enterdir) != GetCrossingRoadAxis(tile)) {
-				return IsTileOwner(tile, owner); // Railway needs owner check, while the street is public
+				return IsRailUsageAllowed(tile, owner); // Railway needs owner check, while the street is public
 			}
 			break;
 
+		case MP_STATION:
+			/* rail station uses the same check as a normal rail tile */
+			if (HasStationTileRail(tile)) return IsRailwayTileUsageAllowed(tile, owner);
+			/* standard road stop */
+			if (IsStandardRoadStopTile(tile)) return IsStationUsageAllowed(tile, owner, VEH_ROAD);
+			break;
+
 		case MP_TUNNELBRIDGE:
 			if (GetTunnelBridgeTransportType(tile) == TRANSPORT_RAIL) {
-				return IsTileOwner(tile, owner);
+				return IsRailUsageAllowed(tile, owner);
 			}
 			break;
 
@@ -982,7 +989,7 @@
 	FOR_ALL_DEPOTS(depot) {
 		/* Check if this is really a valid depot, it is of the needed type and
 		 * owner */
-		if (IsDepotTypeTile(depot->xy, type) && IsTileOwner(depot->xy, owner))
+		if (IsDepotTypeTile(depot->xy, type) && IsDepotUsageAllowed(depot->xy, owner, (VehicleType)type))
 			/* If so, let's add it to the queue, sorted by distance */
 			depots.push(&depots, depot, DistanceManhattan(tile, depot->xy));
 	}
Index: src/smallmap_gui.cpp
===================================================================
--- src/smallmap_gui.cpp	(revision 17973)
+++ src/smallmap_gui.cpp	(working copy)
@@ -26,6 +26,7 @@
 #include "vehicle_base.h"
 #include "sound_func.h"
 #include "window_func.h"
+#include "infrastructure_func.h"
 
 #include "table/strings.h"
 #include "table/sprites.h"
@@ -197,6 +198,20 @@
 	MKEND()
 };
 
+static const LegendAndColour _legend_routes_sharing[] = {
+	MK(0xD7, STR_SMALLMAP_LEGENDA_ROADS),
+	MK(0x0E, STR_SMALLMAP_LEGENDA_RAILROADS_ACCESSIBLE),
+	MK(0x11, STR_SMALLMAP_LEGENDA_RAILROADS_INACCESSIBLE),
+	MK(0xB5, STR_SMALLMAP_LEGENDA_BUILDINGS_INDUSTRIES),
+	MS(0x56, STR_SMALLMAP_LEGENDA_RAILROAD_STATION),
+
+	MK(0xC2, STR_SMALLMAP_LEGENDA_TRUCK_LOADING_BAY),
+	MK(0xBF, STR_SMALLMAP_LEGENDA_BUS_STATION),
+	MK(0xB8, STR_SMALLMAP_LEGENDA_AIRPORT_HELIPORT),
+	MK(0x98, STR_SMALLMAP_LEGENDA_DOCK),
+	MKEND()
+};
+
 static const LegendAndColour _legend_vegetation[] = {
 	MK(0x52, STR_SMALLMAP_LEGENDA_ROUGH_LAND),
 	MK(0x54, STR_SMALLMAP_LEGENDA_GRASS_LAND),
@@ -436,6 +451,14 @@
 			case STATION_DOCK:    return MKCOLOUR(0x98989898);
 			default:              return MKCOLOUR(0xFFFFFFFF);
 		}
+	} else if (t == MP_RAILWAY && ShowSharingOnMap()) {
+		if (IsRailwayTileUsageAllowed(tile, _local_company)) {
+			/* Make rails color 0x0E (light grey), ground colour 0x54 (green) */
+			return MKCOLOUR(0x540E0E54);
+		} else {
+			/* Make rails color 0x11 (dark grey), ground colour 0x54 (green) */
+			 return MKCOLOUR(0x54111154);
+		}
 	}
 
 	/* Ground colour */
@@ -924,7 +947,8 @@
 		int x = 4;
 		int y = y_org;
 
-		for (const LegendAndColour *tbl = _legend_table[this->map_type]; !tbl->end; ++tbl) {
+		const LegendAndColour *tbl = (this->map_type == SMT_ROUTES && ShowSharingOnMap()) ? _legend_routes_sharing : _legend_table[this->map_type];
+		while (!tbl->end) {
 			if (tbl->col_break || y >= legend->bottom) {
 				/* Column break needed, continue at top, COLUMN_WIDTH pixels
 				 * (one "row") to the right. */
@@ -954,6 +978,7 @@
 			GfxFillRect(x + 1, y + 2, x + 7, y + 4, tbl->colour); // Legend colour
 
 			y += 6;
+			tbl++;
 		}
 
 		const Widget *wi = &this->widget[SM_WIDGET_MAP];
Index: src/news_func.h
===================================================================
--- src/news_func.h	(revision 17973)
+++ src/news_func.h	(working copy)
@@ -16,6 +16,7 @@
 #include "vehicle_type.h"
 #include "station_type.h"
 #include "industry_type.h"
+#include "company_type.h"
 
 void AddNewsItem(StringID string, NewsSubtype subtype, NewsReferenceType reftype1 = NR_NONE, uint32 ref1 = UINT32_MAX, NewsReferenceType reftype2 = NR_NONE, uint32 ref2 = UINT32_MAX, void *free_data = NULL);
 
Index: src/table/settings.h
===================================================================
--- src/table/settings.h	(revision 17973)
+++ src/table/settings.h	(working copy)
@@ -32,6 +32,9 @@
 static bool CheckFreeformEdges(int32 p1);
 static bool ChangeDynamicEngines(int32 p1);
 static bool StationCatchmentChanged(int32 p1);
+static bool UpdateAllSharingSettings(int32 p1);
+static bool UpdateSharingEnabled(int32 p1);
+static bool UpdateSharingDelay(int32 p1);
 
 #ifdef ENABLE_NETWORK
 static bool UpdateClientName(int32 p1);
@@ -502,6 +505,18 @@
 	 SDT_CONDVAR(GameSettings, pf.yapf.road_crossing_penalty,                  SLE_UINT, 33, SL_MAX_VERSION, 0, 0,     3 * YAPF_TILE_LENGTH,  0, 1000000, 0, STR_NULL,         NULL),
 	 SDT_CONDVAR(GameSettings, pf.yapf.road_stop_penalty,                      SLE_UINT, 47, SL_MAX_VERSION, 0, 0,     8 * YAPF_TILE_LENGTH,  0, 1000000, 0, STR_NULL,         NULL),
 
+	 SDT_CONDVAR(GameSettings, sharing.sharing_mode,                        SLE_UINT8,SL_IS, SL_MAX_VERSION, 0,MS, SHARING_NONE, SHARING_NONE, SHARING_INDIVIDUAL, 0, STR_CONFIG_SETTING_SHARING_ENABLE,UpdateSharingEnabled),
+	 SDT_CONDVAR(GameSettings, sharing.new_individual_delay,                SLE_UINT8,SL_IS, SL_MAX_VERSION, 0, 0,    12,                     0,     120, 1, STR_CONFIG_SETTING_SHARING_INDIVIDUAL_TIME,UpdateSharingDelay),
+	SDT_CONDBOOL(GameSettings, sharing.payment_in_debt,                               SL_IS, SL_MAX_VERSION, 0, 0, false,                                    STR_CONFIG_SETTING_SHARING_PAYMENT_DEBT,   NULL),
+	 SDT_CONDVAR(GameSettings, sharing.rail_sharing,                        SLE_UINT8,SL_IS, SL_MAX_VERSION, 0,MS,     0,                     0,       4, 0, STR_CONFIG_SETTING_SHARING_RAIL,           UpdateAllSharingSettings),
+	SDT_CONDBOOL(GameSettings, sharing.road_sharing,                                  SL_IS, SL_MAX_VERSION, 0, 0, false,                                    STR_CONFIG_SETTING_SHARING_ROAD,           UpdateAllSharingSettings),
+	SDT_CONDBOOL(GameSettings, sharing.water_sharing,                                 SL_IS, SL_MAX_VERSION, 0, 0, false,                                    STR_CONFIG_SETTING_SHARING_WATER,          UpdateAllSharingSettings),
+	SDT_CONDBOOL(GameSettings, sharing.air_sharing,                                   SL_IS, SL_MAX_VERSION, 0, 0, false,                                    STR_CONFIG_SETTING_SHARING_AIR,            UpdateAllSharingSettings),
+	 SDT_CONDVAR(GameSettings, sharing.rail_fee,                           SLE_UINT16,SL_IS, SL_MAX_VERSION, 0,CR,   100,            0, MAX_SHARING_FEE, 10, STR_CONFIG_SETTING_SHARING_RAIL_FEE,       UpdateAllSharingSettings),
+	 SDT_CONDVAR(GameSettings, sharing.road_fee,                           SLE_UINT16,SL_IS, SL_MAX_VERSION, 0,CR,   100,            0, MAX_SHARING_FEE, 10, STR_CONFIG_SETTING_SHARING_ROAD_FEE,       UpdateAllSharingSettings),
+	 SDT_CONDVAR(GameSettings, sharing.water_fee,                          SLE_UINT16,SL_IS, SL_MAX_VERSION, 0,CR,   100,            0, MAX_SHARING_FEE, 10, STR_CONFIG_SETTING_SHARING_WATER_FEE,      UpdateAllSharingSettings),
+	 SDT_CONDVAR(GameSettings, sharing.air_fee,                            SLE_UINT16,SL_IS, SL_MAX_VERSION, 0,CR,   100,            0, MAX_SHARING_FEE, 10, STR_CONFIG_SETTING_SHARING_AIR_FEE,        UpdateAllSharingSettings),
+
 	 SDT_CONDVAR(GameSettings, game_creation.land_generator,                  SLE_UINT8, 30, SL_MAX_VERSION, 0,MS,     1,                     0,       1, 0, STR_CONFIG_SETTING_LAND_GENERATOR,        NULL),
 	 SDT_CONDVAR(GameSettings, game_creation.oil_refinery_limit,              SLE_UINT8, 30, SL_MAX_VERSION, 0, 0,    32,                    12,      48, 0, STR_CONFIG_SETTING_OIL_REF_EDGE_DISTANCE, NULL),
 	 SDT_CONDVAR(GameSettings, game_creation.tgen_smoothness,                 SLE_UINT8, 30, SL_MAX_VERSION, 0,MS,     1,                     0,       3, 0, STR_CONFIG_SETTING_ROUGHNESS_OF_TERRAIN,  NULL),
Index: src/news_type.h
===================================================================
--- src/news_type.h	(revision 17973)
+++ src/news_type.h	(working copy)
@@ -50,6 +50,7 @@
 	NS_COMPANY_MERGER,   ///< NT_COMPANY_INFO (merger)
 	NS_COMPANY_BANKRUPT, ///< NT_COMPANY_INFO (bankrupt)
 	NS_COMPANY_NEW,      ///< NT_COMPANY_INFO (new company)
+	NS_COMPANY_SHARING,  ///< NT_COMPANY_INFO (change of sharing settings)
 	NS_INDUSTRY_OPEN,    ///< NT_INDUSTRY_OPEN
 	NS_INDUSTRY_CLOSE,   ///< NT_INDUSTRY_CLOSE
 	NS_ECONOMY,          ///< NT_ECONOMY
Index: src/ship_cmd.cpp
===================================================================
--- src/ship_cmd.cpp	(revision 17973)
+++ src/ship_cmd.cpp	(working copy)
@@ -35,6 +35,7 @@
 #include "ai/ai.hpp"
 #include "pathfind.h"
 #include "landscape_type.h"
+#include "infrastructure_func.h"
 
 #include "table/strings.h"
 #include "table/sprites.h"
@@ -119,7 +120,7 @@
 
 	FOR_ALL_DEPOTS(depot) {
 		TileIndex tile = depot->xy;
-		if (IsShipDepotTile(tile) && IsTileOwner(tile, v->owner)) {
+		if (IsShipDepotTile(tile) && IsDepotUsageAllowed(tile, v)) {
 			uint dist = DistanceManhattan(tile, v->tile);
 			if (dist < best_dist) {
 				best_dist = dist;
@@ -755,7 +756,7 @@
 	/* The ai_new queries the vehicle cost before building the route,
 	 * so we must check against cheaters no sooner than now. --pasky */
 	if (!IsShipDepotTile(tile)) return CMD_ERROR;
-	if (!IsTileOwner(tile, _current_company)) return CMD_ERROR;
+	if (!AreDepotOperationsAllowed(tile, _current_company, VEH_SHIP)) return CMD_ERROR;
 
 	unit_num = (flags & DC_AUTOREPLACE) ? 0 : GetFreeUnitNumber(VEH_SHIP);
 
Index: src/order_cmd.cpp
===================================================================
--- src/order_cmd.cpp	(revision 17973)
+++ src/order_cmd.cpp	(working copy)
@@ -29,6 +29,7 @@
 #include "roadveh.h"
 #include "station_base.h"
 #include "waypoint_base.h"
+#include "infrastructure_func.h"
 
 #include "table/strings.h"
 
@@ -466,7 +467,7 @@
 			const Station *st = Station::GetIfValid(new_order.GetDestination());
 			if (st == NULL) return CMD_ERROR;
 
-			if (st->owner != OWNER_NONE && !CheckOwnership(st->owner)) return CMD_ERROR;
+			if (!IsStationUsageAllowed(st, v)) return CMD_ERROR;
 
 			if (!CanVehicleUseStation(v, st)) return_cmd_error(STR_ERROR_CAN_T_ADD_ORDER);
 			for (Vehicle *u = v->FirstShared(); u != NULL; u = u->NextShared()) {
@@ -510,7 +511,7 @@
 				if (v->type == VEH_AIRCRAFT) {
 					const Station *st = Station::GetIfValid(new_order.GetDestination());
 
-					if (st == NULL || !CheckOwnership(st->owner) ||
+					if (st == NULL || !IsStationUsageAllowed(st, v) ||
 							!CanVehicleUseStation(v, st) ||
 							st->Airport()->nof_depots == 0) {
 						return CMD_ERROR;
@@ -518,7 +519,7 @@
 				} else {
 					const Depot *dp = Depot::GetIfValid(new_order.GetDestination());
 
-					if (dp == NULL || !CheckOwnership(GetTileOwner(dp->xy))) return CMD_ERROR;
+					if (dp == NULL || !IsDepotUsageAllowed(dp->xy, v)) return CMD_ERROR;
 
 					switch (v->type) {
 						case VEH_TRAIN:
@@ -554,13 +555,16 @@
 			switch (v->type) {
 				default: return CMD_ERROR;
 
-				case VEH_TRAIN:
-					if (!CheckOwnership(wp->owner)) return CMD_ERROR;
-					break;
+				case VEH_TRAIN: {
+					const Waypoint *wp = Waypoint::GetIfValid(new_order.GetDestination());
+					if (wp == NULL || !IsWaypointUsageAllowed(wp, v->owner)) return CMD_ERROR;
+				} break;
 
-				case VEH_SHIP:
-					if (!CheckOwnership(wp->owner) && wp->owner != OWNER_NONE) return CMD_ERROR;
-					break;
+				case VEH_SHIP: {
+					const Station *st = Station::GetIfValid(new_order.GetDestination());
+					if (st == NULL || !IsStationUsageAllowed(st, v)) return CMD_ERROR;
+				} break;
+
 			}
 
 			/* Order flags can be any of the following for waypoints:
@@ -1840,6 +1844,7 @@
 	bool is_dest_station = this->IsType(OT_GOTO_STATION) && this->dest == station;
 	return
 			(!this->IsType(OT_GOTO_DEPOT) || (this->GetDepotOrderType() & ODTFB_PART_OF_ORDERS) != 0) &&
+			IsStationUsageAllowed(Station::Get(station), v) && // Only stop when allowed to
 			v->last_station_visited != station && // Do stop only when we've not just been there
 			/* Finally do stop when there is no non-stop flag set for this type of station. */
 			!(this->GetNonStopType() & (is_dest_station ? ONSF_NO_STOP_AT_DESTINATION_STATION : ONSF_NO_STOP_AT_INTERMEDIATE_STATIONS));
Index: src/window_type.h
===================================================================
--- src/window_type.h	(revision 17973)
+++ src/window_type.h	(working copy)
@@ -106,6 +106,7 @@
 	WC_AI_DEBUG,
 	WC_AI_LIST,
 	WC_AI_SETTINGS,
+	WC_SHARING_OPTIONS,
 
 	WC_INVALID = 0xFFFF
 };
Index: src/company_cmd.cpp
===================================================================
--- src/company_cmd.cpp	(revision 17973)
+++ src/company_cmd.cpp	(working copy)
@@ -76,6 +76,7 @@
 {
 	InvalidateWindowData(WC_GRAPH_LEGEND, 0, (int)index);
 	InvalidateWindowData(WC_PERFORMANCE_DETAIL, 0, (int)index);
+	InvalidateWindowClassesData(WC_SHARING_OPTIONS);
 }
 
 /**
@@ -177,14 +178,16 @@
 	if (HasBit(1 << EXPENSES_TRAIN_INC    |
 	           1 << EXPENSES_ROADVEH_INC  |
 	           1 << EXPENSES_AIRCRAFT_INC |
-	           1 << EXPENSES_SHIP_INC, cost.GetExpensesType())) {
+	           1 << EXPENSES_SHIP_INC     |
+	           1 << EXPENSES_SHARED_INC, cost.GetExpensesType())) {
 		c->cur_economy.income -= cost.GetCost();
 	} else if (HasBit(1 << EXPENSES_TRAIN_RUN    |
 	                  1 << EXPENSES_ROADVEH_RUN  |
 	                  1 << EXPENSES_AIRCRAFT_RUN |
 	                  1 << EXPENSES_SHIP_RUN     |
 	                  1 << EXPENSES_PROPERTY     |
-	                  1 << EXPENSES_LOAN_INT, cost.GetExpensesType())) {
+	                  1 << EXPENSES_LOAN_INT     |
+	                  1 << EXPENSES_SHARED_RUN, cost.GetExpensesType())) {
 		c->cur_economy.expenses -= cost.GetCost();
 	}
 
@@ -462,11 +465,28 @@
 
 	SetDefaultCompanySettings(c->index);
 
+	/* sharing settings */
+	if (_settings_game.sharing.sharing_mode == SHARING_GLOBAL) {
+		c->sharing_current.CopyFromGameSettings();
+		c->sharing_future.Copy(&c->sharing_current);
+		/* enable this company in all companies */
+		Company *d;
+		FOR_ALL_COMPANIES(d) {
+			SetBit(d->sharing_current.allowed_companies, c->index);
+			SetBit(d->sharing_future.allowed_companies, c->index);
+		}
+	} else {
+		/* only allow own company */
+		c->sharing_current.allowed_companies = 1 << c->index;
+		c->sharing_future.allowed_companies  = 1 << c->index;
+	}
+
 	GeneratePresidentName(c);
 
 	SetWindowDirty(WC_GRAPH_LEGEND, 0);
 	SetWindowDirty(WC_TOOLBAR_MENU, 0);
 	SetWindowDirty(WC_CLIENT_LIST, 0);
+	InvalidateWindowClassesData(WC_SHARING_OPTIONS);
 
 	if (is_ai && (!_networking || _network_server)) AI::StartNew(c->index);
 
Index: src/vehicle_cmd.cpp
===================================================================
--- src/vehicle_cmd.cpp	(revision 17973)
+++ src/vehicle_cmd.cpp	(working copy)
@@ -27,6 +27,7 @@
 #include "string_func.h"
 #include "depot_map.h"
 #include "vehiclelist.h"
+#include "infrastructure_func.h"
 
 #include "table/strings.h"
 
@@ -74,7 +75,7 @@
 	if ((flags & DC_AUTOREPLACE) == 0) SetBit(p2, 0);
 
 	Vehicle *v = Vehicle::GetIfValid(p1);
-	if (v == NULL || !CheckOwnership(v->owner) || !v->IsPrimaryVehicle()) return CMD_ERROR;
+	if (v == NULL || !IsVehicleControlAllowed(v) || !v->IsPrimaryVehicle()) return CMD_ERROR;
 
 	switch (v->type) {
 		case VEH_TRAIN:
@@ -217,7 +218,7 @@
 	VehicleType vehicle_type = (VehicleType)GB(p1, 0, 8);
 	bool all_or_nothing = HasBit(p2, 0);
 
-	if (!IsDepotTile(tile) || !IsTileOwner(tile, _current_company)) return CMD_ERROR;
+	if (!IsDepotTile(tile) || !AreDepotOperationsAllowed(tile, _current_company, vehicle_type)) return CMD_ERROR;
 
 	/* Get the list of vehicles in the depot */
 	BuildDepotVehicleList(vehicle_type, tile, &list, &list, true);
Index: src/train_cmd.cpp
===================================================================
--- src/train_cmd.cpp	(revision 17973)
+++ src/train_cmd.cpp	(working copy)
@@ -35,6 +35,7 @@
 #include "effectvehicle_func.h"
 #include "gamelog.h"
 #include "network/network.h"
+#include "infrastructure_func.h"
 
 #include "table/strings.h"
 #include "table/train_cmd.h"
@@ -57,7 +58,7 @@
  * @param track vehicle track bits
  * @return side of tile the train will leave
  */
-static inline DiagDirection TrainExitDir(Direction direction, TrackBits track)
+DiagDirection TrainExitDir(Direction direction, TrackBits track)
 {
 	static const TrackBits state_dir_table[DIAGDIR_END] = { TRACK_BIT_RIGHT, TRACK_BIT_LOWER, TRACK_BIT_LEFT, TRACK_BIT_UPPER };
 
@@ -845,7 +846,7 @@
 	/* Check if the train is actually being built in a depot belonging
 	 * to the company. Doesn't matter if only the cost is queried */
 	if (!IsRailDepotTile(tile)) return CMD_ERROR;
-	if (!IsTileOwner(tile, _current_company)) return CMD_ERROR;
+	if (!AreDepotOperationsAllowed(tile, _current_company, VEH_TRAIN)) return CMD_ERROR;
 
 	if (rvi->railveh_type == RAILVEH_WAGON) return CmdBuildRailWagon(p1, tile, flags);
 
@@ -2020,7 +2021,7 @@
 CommandCost CmdReverseTrainDirection(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
 {
 	Train *v = Train::GetIfValid(p1);
-	if (v == NULL || !CheckOwnership(v->owner)) return CMD_ERROR;
+	if (v == NULL || !IsVehicleControlAllowed(v)) return CMD_ERROR;
 
 	if (p2 != 0) {
 		/* turn a single unit around */
@@ -2080,7 +2081,7 @@
 CommandCost CmdForceTrainProceed(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
 {
 	Train *t = Train::GetIfValid(p1);
-	if (t == NULL || !CheckOwnership(t->owner)) return CMD_ERROR;
+	if (t == NULL || !IsVehicleControlAllowed(t)) return CMD_ERROR;
 
 	if (flags & DC_EXEC) t->force_proceed = 0x50;
 
@@ -2143,8 +2144,8 @@
 static bool NtpCallbFindDepot(TileIndex tile, TrainFindDepotData *tfdd, int track, uint length)
 {
 	if (IsTileType(tile, MP_RAILWAY) &&
-			IsTileOwner(tile, tfdd->owner) &&
-			IsRailDepot(tile)) {
+			IsRailDepot(tile) &&
+			IsDepotUsageAllowed(tile, tfdd->owner, VEH_TRAIN)) {
 		/* approximate number of tiles by dividing by DIAG_FACTOR */
 		tfdd->best_length = length / DIAG_FACTOR;
 		tfdd->tile = tile;
@@ -2471,7 +2472,7 @@
 }
 
 /** Clear the reservation of a tile that was just left by a wagon on track_dir. */
-static void ClearPathReservation(const Train *v, TileIndex tile, Trackdir track_dir)
+void ClearPathReservation(const Train *v, TileIndex tile, Trackdir track_dir)
 {
 	DiagDirection dir = TrackdirToExitdir(track_dir);
 
@@ -3413,7 +3414,7 @@
 static inline bool CheckCompatibleRail(const Train *v, TileIndex tile)
 {
 	return
-		IsTileOwner(tile, v->owner) && (
+		IsRailwayTileUsageAllowed(tile, v->owner) && (
 			!v->IsFrontEngine() ||
 			HasBit(v->compatible_railtypes, GetRailType(tile))
 		);
@@ -4531,6 +4532,9 @@
 			/* running costs */
 			CommandCost cost(EXPENSES_TRAIN_RUN, this->GetRunningCost() * this->running_ticks / (DAYS_IN_YEAR  * DAY_TICKS));
 
+			/* sharing fee */
+			PayDailyTrackSharingFee(this);
+
 			this->profit_this_year -= cost.GetCost();
 			this->running_ticks = 0;
 
Index: src/command.cpp
===================================================================
--- src/command.cpp	(revision 17973)
+++ src/command.cpp	(working copy)
@@ -198,6 +198,7 @@
 DEF_COMMAND(CmdChangeTimetable);
 DEF_COMMAND(CmdSetVehicleOnTime);
 DEF_COMMAND(CmdAutofillTimetable);
+DEF_COMMAND(CmdChangeSharingSetting);
 #undef DEF_COMMAND
 
 /**
@@ -343,6 +344,7 @@
 	{CmdChangeTimetable,                            0}, // CMD_CHANGE_TIMETABLE
 	{CmdSetVehicleOnTime,                           0}, // CMD_SET_VEHICLE_ON_TIME
 	{CmdAutofillTimetable,                          0}, // CMD_AUTOFILL_TIMETABLE
+	{CmdChangeSharingSetting,                       0}, // CMD_CHANGE_SHARING_SETTING
 };
 
 /*!
Index: src/company_base.h
===================================================================
--- src/company_base.h	(revision 17973)
+++ src/company_base.h	(working copy)
@@ -23,6 +23,7 @@
 #include "economy_type.h"
 #include "tile_type.h"
 #include "settings_type.h"
+#include "infrastructure.h"
 
 struct CompanyEconomyEntry {
 	Money income;
@@ -87,6 +88,10 @@
 	CompanySettings settings;          ///< settings specific for each company
 	uint16 *num_engines; ///< caches the number of engines of each type the company owns (no need to save this)
 
+	byte sharing_delay;                           ///< number of months before new settings go in effect
+	CompanySharingSettingsEntry sharing_current;  ///< settings currently in effect
+	CompanySharingSettingsEntry sharing_future;   ///< future settings, will go in effect after (sharing_delay) months.
+
 	static FORCEINLINE bool IsValidAiID(size_t index)
 	{
 		const Company *c = Company::GetIfValid(index);
Index: src/settings_type.h
===================================================================
--- src/settings_type.h	(revision 17973)
+++ src/settings_type.h	(working copy)
@@ -193,6 +193,23 @@
 	uint32 ai_max_opcode_till_suspend;       ///< max opcode calls till AI will suspend
 };
 
+/** Settings related to sharing. */
+struct SharingSettings {
+	byte   sharing_mode;                     ///< main infrastructure sharing setting, (off / on with global settings / on with individual settings)
+	byte   new_individual_delay;             ///< time (in months) it takes for new individual settings to take effect
+	bool   payment_in_debt;                  ///< whether to allow fee payment for companies with more loan than money. Switch off to prevent MP exploits.
+
+	byte   rail_sharing;                     ///< sharing rail
+	bool   road_sharing;                     ///< sharing road
+	bool   water_sharing;                    ///< sharing water
+	bool   air_sharing;                      ///< sharing air
+
+	uint16 rail_fee;                         ///< train fee
+	uint16 road_fee;                         ///< road vehicle fee
+	uint16 water_fee;                        ///< ship fee
+	uint16 air_fee;                          ///< aircraft fee
+};
+
 /** Settings related to the old pathfinder. */
 struct OPFSettings {
 	uint16 pf_maxlength;                     ///< maximum length when searching for a train route for new pathfinder
@@ -375,6 +392,7 @@
 	EconomySettings      economy;            ///< settings to change the economy
 	StationSettings      station;            ///< settings related to station management
 	LocaleSettings       locale;             ///< settings related to used currency/unit system in the current game
+	SharingSettings      sharing;            ///< settings related to infrastructure sharing
 };
 
 /** All settings that are only important for the local client. */
Index: src/depot_gui.cpp
===================================================================
--- src/depot_gui.cpp	(revision 17973)
+++ src/depot_gui.cpp	(working copy)
@@ -29,6 +29,7 @@
 #include "tilehighlight_func.h"
 #include "window_gui.h"
 #include "vehiclelist.h"
+#include "infrastructure_func.h"
 
 #include "table/strings.h"
 #include "table/sprites.h"
@@ -230,7 +231,21 @@
 		this->type = type;
 
 		this->CreateNestedTree(desc);
+		this->window_number = tile;
 		this->SetupWidgetData(type);
+
+		/* Setup disabled buttons. */
+		this->SetWidgetsDisabledState(!AreDepotOperationsAllowed(tile, _local_company, type),
+			DEPOT_WIDGET_STOP_ALL,
+			DEPOT_WIDGET_START_ALL,
+			DEPOT_WIDGET_SELL,
+			DEPOT_WIDGET_SELL_CHAIN,
+			DEPOT_WIDGET_SELL_ALL,
+			DEPOT_WIDGET_BUILD,
+			DEPOT_WIDGET_CLONE,
+			DEPOT_WIDGET_AUTOREPLACE,
+			WIDGET_LIST_END);
+
 		this->FinishInitNested(desc, tile);
 
 		this->owner = GetTileOwner(tile);
Index: src/command_type.h
===================================================================
--- src/command_type.h	(revision 17973)
+++ src/command_type.h	(working copy)
@@ -294,6 +294,7 @@
 	CMD_CHANGE_TIMETABLE,             ///< change the timetable for a vehicle
 	CMD_SET_VEHICLE_ON_TIME,          ///< set the vehicle on time feature (timetable)
 	CMD_AUTOFILL_TIMETABLE,           ///< autofill the timetable
+	CMD_CHANGE_SHARING_SETTING,       ///< change one of the settings for infrastructure sharing
 };
 
 /**
Index: src/order_gui.cpp
===================================================================
--- src/order_gui.cpp	(revision 17973)
+++ src/order_gui.cpp	(working copy)
@@ -31,6 +31,7 @@
 #include "network/network.h"
 #include "station_base.h"
 #include "waypoint_base.h"
+#include "infrastructure_func.h"
 
 #include "table/sprites.h"
 #include "table/strings.h"
@@ -298,18 +299,16 @@
 	if (_settings_game.order.gotodepot) {
 		switch (GetTileType(tile)) {
 			case MP_RAILWAY:
-				if (v->type == VEH_TRAIN && IsTileOwner(tile, _local_company)) {
-					if (IsRailDepot(tile)) {
+				if (v->type == VEH_TRAIN && IsRailDepot(tile) && IsDepotUsageAllowed(tile, v)) {
 						order.MakeGoToDepot(GetDepotIndex(tile), ODTFB_PART_OF_ORDERS,
-								_settings_client.gui.new_nonstop ? ONSF_NO_STOP_AT_INTERMEDIATE_STATIONS : ONSF_STOP_EVERYWHERE);
-						if (_ctrl_pressed) order.SetDepotOrderType((OrderDepotTypeFlags)(order.GetDepotOrderType() ^ ODTFB_SERVICE));
-						return order;
-					}
+						_settings_client.gui.new_nonstop ? ONSF_NO_STOP_AT_INTERMEDIATE_STATIONS : ONSF_STOP_EVERYWHERE);
+					if (_ctrl_pressed) order.SetDepotOrderType((OrderDepotTypeFlags)(order.GetDepotOrderType() ^ ODTFB_SERVICE));
+					return order;
 				}
 				break;
 
 			case MP_ROAD:
-				if (IsRoadDepot(tile) && v->type == VEH_ROAD && IsTileOwner(tile, _local_company)) {
+				if (IsRoadDepot(tile) && v->type == VEH_ROAD && IsDepotUsageAllowed(tile, v)) {
 					order.MakeGoToDepot(GetDepotIndex(tile), ODTFB_PART_OF_ORDERS,
 							_settings_client.gui.new_nonstop ? ONSF_NO_STOP_AT_INTERMEDIATE_STATIONS : ONSF_STOP_EVERYWHERE);
 					if (_ctrl_pressed) order.SetDepotOrderType((OrderDepotTypeFlags)(order.GetDepotOrderType() ^ ODTFB_SERVICE));
@@ -319,7 +318,7 @@
 
 			case MP_STATION:
 				if (v->type != VEH_AIRCRAFT) break;
-				if (IsHangar(tile) && IsTileOwner(tile, _local_company)) {
+				if (IsHangar(tile) && IsStationUsageAllowed(tile, v)) {
 					order.MakeGoToDepot(GetStationIndex(tile), ODTFB_PART_OF_ORDERS, ONSF_STOP_EVERYWHERE);
 					if (_ctrl_pressed) order.SetDepotOrderType((OrderDepotTypeFlags)(order.GetDepotOrderType() ^ ODTFB_SERVICE));
 					return order;
@@ -328,7 +327,7 @@
 
 			case MP_WATER:
 				if (v->type != VEH_SHIP) break;
-				if (IsShipDepot(tile) && IsTileOwner(tile, _local_company)) {
+				if (IsShipDepot(tile) && IsDepotUsageAllowed(tile, v)) {
 					order.MakeGoToDepot(GetDepotIndex(tile), ODTFB_PART_OF_ORDERS, ONSF_STOP_EVERYWHERE);
 					if (_ctrl_pressed) order.SetDepotOrderType((OrderDepotTypeFlags)(order.GetDepotOrderType() ^ ODTFB_SERVICE));
 					return order;
@@ -342,7 +341,7 @@
 	/* check waypoint */
 	if (IsRailWaypointTile(tile) &&
 			v->type == VEH_TRAIN &&
-			IsTileOwner(tile, _local_company)) {
+			IsWaypointUsageAllowed(Waypoint::GetByTile(tile), v->owner)) {
 		order.MakeGoToWaypoint(Waypoint::GetByTile(tile)->index);
 		if (_settings_client.gui.new_nonstop != _ctrl_pressed) order.SetNonStopType(ONSF_NO_STOP_AT_ANY_STATION);
 		return order;
@@ -357,7 +356,7 @@
 		StationID st_index = GetStationIndex(tile);
 		const Station *st = Station::Get(st_index);
 
-		if (st->owner == _local_company || st->owner == OWNER_NONE) {
+		if (IsStationUsageAllowed(st, v)) {
 			byte facil;
 			(facil = FACIL_DOCK, v->type == VEH_SHIP) ||
 			(facil = FACIL_TRAIN, v->type == VEH_TRAIN) ||
Index: src/infrastructure.cpp
===================================================================
--- src/infrastructure.cpp	(revision 0)
+++ src/infrastructure.cpp	(revision 0)
@@ -0,0 +1,510 @@
+/* $Id$ */
+
+/*
+ * This file is part of OpenTTD.
+ * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
+ * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+ * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+/** @file infrastructure.cpp Implementation of infrastructure sharing */
+
+#include "infrastructure_func.h"
+#include "command_func.h"
+#include "window_func.h"
+#include "depot_base.h"
+#include "train.h"
+#include "aircraft.h"
+#include "vehicle_func.h"
+#include "news_func.h"
+#include "strings_func.h"
+#include "core/mem_func.hpp"
+#include "yapf/yapf.h"
+#include "date_func.h"
+#include "station_map.h"
+#include "gfx_func.h"
+
+#include "table/strings.h"
+
+extern DiagDirection TrainExitDir(Direction direction, TrackBits track);
+extern void ClearPathReservation(const Train *v, TileIndex tile, Trackdir track_dir);
+
+bool CompanySharingSettingsEntry::Equals(CompanySharingSettingsEntry *o)
+{
+	/* Since memcmp/MemCmpT is not guaranteed to work properly for structs, we do it this way */
+	return MemCmpT(this->option, o->option, NUM_SHARING_VEHTYPES) == 0
+		&& MemCmpT(this->fee, o->fee, NUM_SHARING_VEHTYPES) == 0
+		&& this->allowed_companies == o->allowed_companies;
+}
+
+byte CompanySharingSettingsEntry::Copy(CompanySharingSettingsEntry *o)
+{
+	/* (x & y) != x returns true if any of the bits in x is not set in y.
+	 * i.e. if any company, permission, etc is removed */
+	byte changed_vehtypes = 0;
+	for (VehicleType vt = VEH_TRAIN; vt < NUM_SHARING_VEHTYPES; vt++) {
+		if ((this->GetSharingOption(vt) & o->GetSharingOption(vt)) != this->GetSharingOption(vt)) {
+			SetBit(changed_vehtypes, vt);
+		}
+	}
+	if ((this->allowed_companies & o->allowed_companies) != this->allowed_companies) changed_vehtypes = ALL_SHARING_VEHTYPES;
+
+	/* Copy the entire struct */
+	MemCpyT(this, o);
+
+	return changed_vehtypes;
+}
+
+byte CompanySharingSettingsEntry::CopyFromGameSettings()
+{
+	/* Create a struct with the same settings as in the game settings,
+	 * then copy it to us using the normal Copy method. */
+	CompanySharingSettingsEntry game_settings;
+
+	game_settings.SetSharingOption(VEH_TRAIN,    _settings_game.sharing.rail_sharing > 0 ? (_settings_game.sharing.rail_sharing - 1) | SHARING_TRACK : 0);
+	game_settings.SetSharingOption(VEH_ROAD,     _settings_game.sharing.road_sharing  ? SHARING_DEPOT | SHARING_STATION : 0);
+	game_settings.SetSharingOption(VEH_SHIP,     _settings_game.sharing.water_sharing ? SHARING_DEPOT | SHARING_STATION : 0);
+	game_settings.SetSharingOption(VEH_AIRCRAFT, _settings_game.sharing.air_sharing   ? SHARING_DEPOT | SHARING_STATION : 0);
+
+	game_settings.SetSharingFee(VEH_TRAIN,    _settings_game.sharing.rail_fee);
+	game_settings.SetSharingFee(VEH_ROAD,     _settings_game.sharing.road_fee);
+	game_settings.SetSharingFee(VEH_SHIP,     _settings_game.sharing.water_fee);
+	game_settings.SetSharingFee(VEH_AIRCRAFT, _settings_game.sharing.air_fee);
+
+	game_settings.allowed_companies = 0;
+	Company *c;
+	FOR_ALL_COMPANIES(c) {
+		SetBit(game_settings.allowed_companies, c->index);
+	}
+
+	return this->Copy(&game_settings);
+}
+
+/**
+ * Helper function for transferring sharing fees
+ * @param v The vehicle involved
+ * @param owner The owner of the infrastructure
+ * @param cost Amount to transfer as money fraction (shifted 8 bits to the left)
+ */
+static void PaySharingFee(Vehicle *v, Owner owner, Money cost)
+{
+	/* the date when a warning about a company unable to pay sharing fees was issued for the last time */
+	static Date last_insolvency_news_date[MAX_COMPANIES] = {0};
+
+	/* the number of days required between two unable-to-pay-sharing-fees warnings about the same company */
+	static const int INSOLVENT_NEWS_DAYS = 60;
+
+	Company *c = Company::GetIfValid(v->owner);
+	assert(c != NULL);
+	if (_settings_game.sharing.payment_in_debt || (c->money - c->current_loan) >= (cost >> 8)) {
+		v->profit_this_year -= cost;
+		SubtractMoneyFromCompanyFract(v->owner, CommandCost(EXPENSES_SHARED_RUN, cost));
+		SubtractMoneyFromCompanyFract(owner, CommandCost(EXPENSES_SHARED_INC, -cost));
+	} else if (owner == _local_company && (last_insolvency_news_date[v->owner] + INSOLVENT_NEWS_DAYS) <= _date) {
+		AddSharingNewsItem(STR_NEWS_SHARING_UNABLE_TO_PAY, c);
+		last_insolvency_news_date[v->owner] = _date;
+	}
+}
+
+/**
+ * Pay the fee for spending a single tick inside a station.
+ * @param v The vehicle that is using the station.
+ * @param st The station that it uses.
+ */
+void PayStationSharingFee(Vehicle *v, const Station *st)
+{
+	if (v->owner == st->owner || st->owner == OWNER_NONE || v->type == VEH_TRAIN) return;
+	Money cost = GetSharingFee(st->owner, v->type);
+	PaySharingFee(v, st->owner, (cost << 8) / DAY_TICKS);
+}
+
+/**
+ * Pay the daily fee for trains on foreign tracks.
+ * @param v The vehicle to pay the fee for.
+ */
+void PayDailyTrackSharingFee(Train *v)
+{
+	Owner owner = GetTileOwner(v->tile);
+	if (owner == v->owner) return;
+	Money cost = GetSharingFee(owner, VEH_TRAIN);
+	cost = (cost << 8) * v->tcache.cached_total_length / 16; //cost is per 16 units = 1 tile
+	cost = cost * 12 / DAYS_IN_YEAR; //cost is per month, but we pay daily.
+	PaySharingFee(v, owner, cost);
+}
+
+/**
+ * Add a sharing-related news message to the news stack.
+ * @param string The message string to show.
+ * @param c Company this message is about.
+ * @param param Optional additional parameter to use for drawing the string.
+ */
+void AddSharingNewsItem(StringID string, Company *c, uint32 param)
+{
+	CompanyNewsInformation *cni = MallocT<CompanyNewsInformation>(1);
+	cni->FillData(c);
+
+	SetDParam(0, STR_NEWS_SHARING_TITLE);
+	SetDParam(1, string);
+	SetDParamStr(2, cni->company_name);
+	SetDParam(3, param);
+
+	AddNewsItem(STR_MESSAGE_NEWS_FORMAT, NS_COMPANY_SHARING, NR_NONE, UINT32_MAX, NR_NONE, UINT32_MAX, cni);
+}
+
+/**
+ * Change the sharing settings of a company.
+ * @see ModifySharingFlag
+ * @param tile unused
+ * @param flags operation to perform
+ * @param p1 bitstuffed elements regarding the variable to change:
+ * - p1 = (bit 0 -  7) What to change (boolean option, fee or allowed companies)
+ * - p1 = (bit 8 - 15) The vehicle type to change it for.
+ * @param p2 value to change the aforementioned variable to
+ * @param text unused
+ */
+CommandCost CmdChangeSharingSetting(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
+{
+	if (_settings_game.sharing.sharing_mode != SHARING_INDIVIDUAL) return CMD_ERROR;
+	VehicleType type = (VehicleType)GB(p1, 8, 8);
+	if (type >= NUM_SHARING_VEHTYPES) return CMD_ERROR;
+
+	Company *c = Company::GetIfValid(_current_company);
+	if (c == NULL) return CMD_ERROR;
+
+	ModifySharingFlag msf = (ModifySharingFlag)GB(p1, 0, 8);
+	switch (msf) {
+		case MODIFY_SHARING_OPTION:
+			if (type == VEH_TRAIN) {
+				if (p2 > (SHARING_TRACK | SHARING_DEPOT | SHARING_STATION)) return CMD_ERROR;
+				/* don't allow any sharing without tracks enabled */
+				if (p2 > 0 && (p2 & SHARING_TRACK) == 0) return CMD_ERROR;
+			} else {
+				if (p2 != 0 && p2 != (SHARING_DEPOT | SHARING_STATION)) return CMD_ERROR;
+			}
+			break;
+		case MODIFY_SHARING_FEE:
+			if (p2 > MAX_SHARING_FEE) return CMD_ERROR;
+			break;
+		case MODIFY_SHARING_COMPANIES:
+			/* a company mask should not have (1 << MAX_COMPANIES) or any higher bit set */
+			if (p2 >= (1 << MAX_COMPANIES)) return CMD_ERROR;
+			for (CompanyID cid = COMPANY_FIRST; cid < MAX_COMPANIES; cid++) {
+				if (HasBit(p2, cid) && !Company::IsValidID(cid)) return CMD_ERROR;
+			}
+			/* we prefer to keep ourselves allowed */
+			if (!HasBit(p2, _current_company)) return CMD_ERROR;
+			break;
+		default: return CMD_ERROR;
+	}
+
+	if (flags & DC_EXEC) {
+		switch (msf) {
+			case MODIFY_SHARING_OPTION:    c->sharing_future.SetSharingOption(type, p2); break;
+			case MODIFY_SHARING_FEE:       c->sharing_future.SetSharingFee(type, p2);    break;
+			case MODIFY_SHARING_COMPANIES: c->sharing_future.allowed_companies = p2;     break;
+			default: NOT_REACHED();
+		}
+
+		/* Set the sharing delay, and copy the future settings to the current settings if needed */
+		if (c->sharing_future.Equals(&c->sharing_current)) {
+			c->sharing_delay = 0;
+		} else if (_settings_game.sharing.new_individual_delay == 0) {
+			assert(c->sharing_delay == 0);
+			/* Change is applied immediately, so warn users */
+			if (_current_company != _local_company) {
+				AddSharingNewsItem(STR_NEWS_SHARING_CHANGE_HAPPENED, c);
+			}
+			HandleSharingChange(c->sharing_current.Copy(&c->sharing_future));
+		} else {
+			c->sharing_delay = _settings_game.sharing.new_individual_delay;
+			if (_current_company != _local_company) {
+				AddSharingNewsItem(STR_NEWS_SHARING_CHANGE_ANNOUNCED, c, c->sharing_delay);
+			}
+		}
+		InvalidateWindowData(WC_SHARING_OPTIONS, _current_company);
+	}
+	return CommandCost();
+}
+
+/**
+ * Update all level crossings in front of a train.
+ * This has to be done before all other actions.
+ */
+static void UpdateAllCrossings()
+{
+	Train *v;
+	FOR_ALL_TRAINS(v) {
+		TileIndex crossing = v->tile + TileOffsByDiagDir(TrainExitDir(v->direction, v->track));
+		if (IsLevelCrossingTile(crossing)) UpdateLevelCrossing(crossing, false);
+	}
+}
+
+/**
+ * Sell a vehicle, no matter where it may be.
+ * Also make sure no artifacts are left, such as PBS paths
+ * @param v The vehicle to sell
+ */
+static void RemoveAndSellVehicle(Vehicle *v)
+{
+	static SmallVector<TileIndex, 4> crossing_tiles;
+
+	/* Crashed vehicles will remove themselves, no need for action */
+	if (v->vehstatus & VS_CRASHED) return;
+
+	if (v->type == VEH_TRAIN) {
+		/* Clear path reservation. This will not clear any reservation that has become invalid, we will do that later */
+		FreeTrainTrackReservation(Train::From(v));
+		for (const Train *u = Train::From(v); u != NULL; u = u->Next()) {
+			/* We cannot remove these reservations reliably,
+			 * since wormholes can contain more than one vehicle.
+			 * Therefore, we leave the reservation, it will be dealt with later. */
+			if (!IsTileType(u->tile, MP_TUNNELBRIDGE)) {
+				ClearPathReservation(u, u->tile, u->GetVehicleTrackdir());
+			}
+			if (IsLevelCrossingTile(u->tile) && !crossing_tiles.Contains(u->tile)) {
+				*crossing_tiles.Append() = u->tile;
+			}
+		}
+	}
+	SubtractMoneyFromCompanyFract(v->owner, CommandCost(EXPENSES_NEW_VEHICLES, -(v->value << 8)));
+	delete v;
+
+	for (uint i = 0; i < crossing_tiles.Length(); i++) {
+		UpdateLevelCrossing(crossing_tiles[i], false);
+	}
+	crossing_tiles.Clear();
+}
+
+
+/**
+ * Check the positions of all vehicles.
+ * If it is not in an allowed position,
+ * (e.g. it is in a depot when sharing of depots is disabled)
+ * then it is sold immediately.
+ * @param changed_vehtypes bitmask of vehicle types that were affected by a sharing setting change.
+ * @return true if any *trains* were removed.
+ */
+static bool CheckAllVehiclePositions(byte changed_vehtypes)
+{
+	bool sold_trains = false;
+	uint num_sold_local_vehicles = 0;
+	Vehicle *v;
+	FOR_ALL_VEHICLES(v) {
+		if (!(v->IsPrimaryVehicle() || (v->type == VEH_TRAIN && Train::From(v)->IsFreeWagon()))) continue;
+		if (!HasBit(changed_vehtypes, v->type)) continue;
+		switch (v->type) {
+			case VEH_TRAIN:
+				for (Vehicle *u = v; u != NULL; u = u->Next()) {
+					/* If any wagon is on a disallowed tile, sell the entire train */
+					if (!IsRailwayTileUsageAllowed(u->tile, u->owner)) {
+						if (v->owner == _local_company) num_sold_local_vehicles++;
+						sold_trains = true;
+						RemoveAndSellVehicle(v);
+						break;
+					}
+				}
+				break;
+			case VEH_ROAD:
+				for (Vehicle *u = v; u != NULL; u = u->Next()) {
+					/* Check all articulated parts */
+					if ((IsRoadDepotTile(u->tile) && !IsDepotUsageAllowed(u->tile, u)) ||
+						(IsStandardRoadStopTile(u->tile) && !IsStationUsageAllowed(u->tile, u))) {
+							if (v->owner == _local_company) num_sold_local_vehicles++;
+							RemoveAndSellVehicle(v);
+							break;
+					}
+				}
+				break;
+			case VEH_SHIP:
+				if (IsShipDepotTile(v->tile) && v->IsStoppedInDepot() && !IsDepotUsageAllowed(v->tile, v)) {
+					if (v->owner == _local_company) num_sold_local_vehicles++;
+					RemoveAndSellVehicle(v);
+				}
+				break;
+			case VEH_AIRCRAFT: {
+				Aircraft *a = Aircraft::From(v);
+				if (a->state == FLYING) break;
+				if (Station::IsValidID(a->targetairport) &&
+					!IsStationUsageAllowed(Station::Get(a->targetairport), v)) {
+						if (v->owner == _local_company) num_sold_local_vehicles++;
+						RemoveAndSellVehicle(v);
+				}
+				break;
+			}
+			default: break;
+		}
+	}
+	if (num_sold_local_vehicles > 0) {
+		SetDParam(0, num_sold_local_vehicles);
+		AddNewsItem(STR_NEWS_SHARING_SOLD_VEHICLES, NS_GENERAL);
+	}
+
+	return sold_trains;
+}
+
+/**
+ * Check whether an order has an allowed destination.
+ * I.e. it refers to a station/depot/waypoint the vehicle is allowed to visit.
+ * @param o The order to check
+ * @param v The vehicle this order belongs to.
+ * @return true if the order has an allowed destination.
+ */
+static bool OrderDestinationIsAllowed(const Order *o, const Vehicle *v)
+{
+	DestinationID dest = o->GetDestination();
+	switch (o->GetType()) {
+		case OT_GOTO_STATION:  return IsStationUsageAllowed(Station::Get(dest), v);
+		case OT_GOTO_DEPOT:    return v->type == VEH_AIRCRAFT ? IsStationUsageAllowed(Station::Get(dest), v) : IsDepotUsageAllowed(Depot::Get(dest)->xy, v);
+		case OT_GOTO_WAYPOINT: return IsWaypointUsageAllowed(Waypoint::Get(dest), v->owner);
+		default: return true;
+	}
+}
+
+/**
+ * Review the orders of all vehicles
+ * This makes sure they don't have any orders to destinations
+ * where they are not allowed to go.
+ * @param changed_vehtypes bitmask of vehicle types that were affected by a sharing setting change.
+ */
+static void ReviewAllVehicleOrders(byte changed_vehtypes)
+{
+	Vehicle *v;
+	FOR_ALL_VEHICLES(v) {
+		if (!(v->IsPrimaryVehicle() && HasBit(changed_vehtypes, v->type))) continue;
+
+		if (!OrderDestinationIsAllowed(&v->current_order, v)) {
+			v->current_order.MakeDummy();
+			SetWindowDirty(WC_VEHICLE_VIEW, v->index);
+		} else if (v->current_order.IsType(OT_LOADING)) {
+			if (!IsStationUsageAllowed(Station::Get(v->last_station_visited), v)) {
+				v->LeaveStation();
+			}
+		}
+
+		if (v->FirstShared() != v) continue;
+
+		Order *o;
+		int id = -1;
+		FOR_VEHICLE_ORDERS(v, o) {
+			id++;
+			if (OrderDestinationIsAllowed(o, v)) continue;
+
+			o->MakeDummy();
+			for (const Vehicle *w = v->FirstShared(); w != NULL; w = w->NextShared()) {
+				/* In GUI, simulate by removing the order and adding it back */
+				InvalidateVehicleOrder(w, id | (INVALID_VEH_ORDER_ID << 8));
+				InvalidateVehicleOrder(w, (INVALID_VEH_ORDER_ID << 8) | id);
+			}
+		}
+	}
+}
+/**
+ * Helper proc for HasVehicleOnPos to determine whether there is a train on a certain track.
+ * @param v The vehicle that may be on this track
+ * @param track the track the train should be on.
+ * @return v if it is on this track, or null otherwise.
+ */
+static Vehicle *HasTrainOnTrackProc(Vehicle *v, void *track)
+{
+	if (v->type != VEH_TRAIN) return NULL;
+	Train *t = Train::From(v);
+	if (TrackBitsToTrack(t->track) == *(Track *)track || t->track == TRACK_BIT_WORMHOLE) {
+		return v;
+	}
+	return NULL;
+}
+
+/**
+ * Check the reservations of all tiles.
+ * If these to not seem to belong to a train or the train is not allowed to use them, unreserve them.
+ */
+static void CheckAllReservations()
+{
+	for (TileIndex tile = 0; tile < MapSize(); tile++) {
+		TrackBits bits = GetReservedTrackbits(tile);
+		while (bits != TRACK_BIT_NONE) {
+			Track track = RemoveFirstTrack(&bits);
+			/* Don't bother continuing if there is a vehicle on this tile */
+			if (HasVehicleOnPos(tile, &track, &HasTrainOnTrackProc)) continue;
+
+			Train *v = GetTrainForReservation(tile, track);
+			if (v == NULL) {
+				UnreserveRailTrack(tile, track);
+				/* make any PBS signal red */
+				Trackdir td = TrackToTrackdir(track);
+				if (HasPbsSignalOnTrackdir(tile, td)) SetSignalStateByTrackdir(tile, td, SIGNAL_STATE_RED);
+				if (HasPbsSignalOnTrackdir(tile, ReverseTrackdir(td))) SetSignalStateByTrackdir(tile, ReverseTrackdir(td), SIGNAL_STATE_RED);
+			}
+		}
+	}
+}
+
+/** Small struct to store a tile/track combination */
+struct SignalPosition {
+	TileIndex tile;  ///< Tile of the signal.
+	Track     track; ///< Track piece of the signal.
+	/** Create a new SignalPosition based on given tile and track piece */
+	SignalPosition(TileIndex _tile, Track _track) : tile(_tile), track(_track) {};
+};
+
+/**
+ * Update all signals on the map.
+ * Since whe do not know which ones are affected by the sharing change, we invalidate them all.
+ * Updating signals can only be done on a per-company basis.
+ * Therefore, we cache all signals into lists, one per company
+ * Then we update all signals in each list.
+ */
+static void UpdateBlockSignals()
+{
+	static SmallVector<SignalPosition, 256> signal_lists[MAX_COMPANIES];
+
+	for (TileIndex tile = 0; tile < MapSize(); tile++) {
+		if (IsTileType(tile, MP_RAILWAY) && HasSignals(tile)) {
+			TrackBits bits = GetTrackBits(tile);
+			do {
+				Track track = RemoveFirstTrack(&bits);
+				if (HasSignalOnTrack(tile, track)) {
+					*signal_lists[GetTileOwner(tile)].Append() = SignalPosition(tile, track);
+				}
+			} while (bits != TRACK_BIT_NONE);
+		}
+	}
+	for (CompanyID c = COMPANY_FIRST; c < MAX_COMPANIES; c++) {
+		for (uint i = 0; i < signal_lists[c].Length(); i++) {
+			AddTrackToSignalBuffer(signal_lists[c][i].tile, signal_lists[c][i].track, c);
+		}
+		UpdateSignalsInBuffer();
+		signal_lists[c].Reset();
+	}
+}
+
+/**
+ * Handle a change of sharing settings.
+ * This involves checking all orders,
+ * making sure all vehicles are not on disallowed territory,
+ * checking all PBS paths
+ * and updating signals.
+ * @param changed_vehtypes Bitmask of vehicle types whose sharing settings were changed. Default is 0xF (all types)
+ * @param update_signals Force updating all signals on the map.
+ */
+void HandleSharingChange(byte changed_vehtypes, bool update_signals)
+{
+	/* Invalidate the entire cache */
+	YapfNotifyTrackLayoutChange(INVALID_TILE, INVALID_TRACK);
+
+	/* Almost any change in settings can causes changes in the level crossings.
+	 * So always review those. */
+	UpdateAllCrossings();
+
+	if (changed_vehtypes != 0) {
+		update_signals |= CheckAllVehiclePositions(changed_vehtypes);
+		ReviewAllVehicleOrders(changed_vehtypes);
+	}
+	if (HasBit(changed_vehtypes, VEH_TRAIN)) {
+		CheckAllReservations();
+	}
+	if (update_signals) {
+		UpdateBlockSignals();
+	}
+	MarkWholeScreenDirty();
+}
Index: src/economy.cpp
===================================================================
--- src/economy.cpp	(revision 17973)
+++ src/economy.cpp	(working copy)
@@ -46,6 +46,7 @@
 #include "waypoint_base.h"
 #include "economy_base.h"
 #include "core/pool_func.hpp"
+#include "infrastructure_func.h"
 
 #include "table/strings.h"
 #include "table/sprites.h"
@@ -387,6 +388,41 @@
 		}
 	}
 
+	/* make the sharing settings the most permissive of the two companies */
+	if (new_owner != INVALID_OWNER) {
+		Company *c_new = Company::Get(new_owner);
+		CompanySharingSettingsEntry *old_settings = &Company::Get(old_owner)->sharing_current;
+		CompanySharingSettingsEntry *new_settings = &c_new->sharing_current;
+
+		for (VehicleType vt = VEH_TRAIN; vt < NUM_SHARING_VEHTYPES; vt++) {
+			new_settings->SetSharingOption(vt, old_settings->GetSharingOption(vt) | new_settings->GetSharingOption(vt));
+			new_settings->SetSharingFee(vt, min(old_settings->GetSharingFee(vt), new_settings->GetSharingFee(vt)));
+		}
+		new_settings->allowed_companies |= old_settings->allowed_companies;
+
+		/* Make the future settings for the new company the same as the (new) current ones */
+		c_new->sharing_future.Copy(new_settings);
+		c_new->sharing_delay = 0;
+	} else {
+		/* Disable sharing for the old company
+		 * This will cause all vehicles on the old company's infrastructure
+		 * to be removed when HandleSharingChange is called */
+		MemSetT(Company::Get(old_owner)->sharing_current.option, 0, NUM_SHARING_VEHTYPES);
+		HandleSharingChange();
+	}
+
+	/* disallow the old company from all infrastructure */
+	Company *c;
+	FOR_ALL_COMPANIES(c) {
+		ClrBit(c->sharing_future.allowed_companies, old_owner);
+		ClrBit(c->sharing_current.allowed_companies, old_owner);
+		/* Future and current settings could now be equal
+		 * In that case, set sharing_delay to 0 */
+		if (c->sharing_current.Equals(&c->sharing_future)) {
+			c->sharing_delay = 0;
+		}
+	}
+
 	/*  Change ownership of tiles */
 	{
 		TileIndex tile = 0;
@@ -701,6 +737,26 @@
 	}
 }
 
+/**
+ * Update the sharing settings of a all companies, if needed.
+ */
+static void CompaniesUpdateSharing()
+{
+	Company *c;
+	byte changed_vehtypes = 0;
+	FOR_ALL_COMPANIES(c) {
+		if (c->sharing_delay > 0 && --c->sharing_delay == 0) {
+			assert(!c->sharing_current.Equals(&c->sharing_future));
+			changed_vehtypes |= c->sharing_current.Copy(&c->sharing_future);
+			if (c->index != _local_company) {
+				AddSharingNewsItem(STR_NEWS_SHARING_CHANGE_HAPPENED, c);
+			}
+			InvalidateWindowData(WC_SHARING_OPTIONS, c->index);
+		}
+	}
+	HandleSharingChange(changed_vehtypes);
+}
+
 static void HandleEconomyFluctuations()
 {
 	if (_settings_game.difficulty.economy != 0) {
@@ -1403,6 +1459,7 @@
 		RecomputePrices();
 	}
 	CompaniesPayInterest();
+	CompaniesUpdateSharing();
 	/* Reset the _current_company flag */
 	_current_company = OWNER_NONE;
 	HandleEconomyFluctuations();
Index: src/company_gui.cpp
===================================================================
--- src/company_gui.cpp	(revision 17973)
+++ src/company_gui.cpp	(working copy)
@@ -29,7 +29,10 @@
 #include "date_func.h"
 #include "widgets/dropdown_type.h"
 #include "tilehighlight_func.h"
-
+#include "company_gui.h"
+#include "currency.h"
+#include "openttd.h"
+ 
 #include "table/strings.h"
 
 /** Company GUI constants. */
@@ -41,8 +44,9 @@
 };
 
 static void DoSelectCompanyManagerFace(Window *parent, bool show_big, int top =  FIRST_GUI_CALL, int left = FIRST_GUI_CALL);
+static void ShowCompanySharingSettings(CompanyID company);
 
-/** Standard unsorted list of expenses. */
+/** Standard unsorted list of expenses with infrastructure sharing disabled. */
 static ExpensesType _expenses_list_1[] = {
 	EXPENSES_CONSTRUCTION,
 	EXPENSES_NEW_VEHICLES,
@@ -59,7 +63,7 @@
 	EXPENSES_OTHER,
 };
 
-/** Grouped list of expenses. */
+/** Grouped list of expenses with infrastructure sharing disabled. */
 static ExpensesType _expenses_list_2[] = {
 	EXPENSES_TRAIN_INC,
 	EXPENSES_ROADVEH_INC,
@@ -79,6 +83,47 @@
 	INVALID_EXPENSES,
 };
 
+/** Standard unsorted list of expenses with infrastructure sharing enabled. */
+static ExpensesType _expenses_list_3[] = {
+	EXPENSES_CONSTRUCTION,
+	EXPENSES_NEW_VEHICLES,
+	EXPENSES_TRAIN_RUN,
+	EXPENSES_ROADVEH_RUN,
+	EXPENSES_AIRCRAFT_RUN,
+	EXPENSES_SHIP_RUN,
+	EXPENSES_SHARED_RUN,
+	EXPENSES_PROPERTY,
+	EXPENSES_TRAIN_INC,
+	EXPENSES_ROADVEH_INC,
+	EXPENSES_AIRCRAFT_INC,
+	EXPENSES_SHIP_INC,
+	EXPENSES_SHARED_INC,
+	EXPENSES_LOAN_INT,
+	EXPENSES_OTHER,
+};
+
+/** Grouped list of expenses with infrastructure sharing enabled. */
+static ExpensesType _expenses_list_4[] = {
+	EXPENSES_TRAIN_INC,
+	EXPENSES_ROADVEH_INC,
+	EXPENSES_AIRCRAFT_INC,
+	EXPENSES_SHIP_INC,
+	EXPENSES_SHARED_INC,
+	INVALID_EXPENSES,
+	EXPENSES_TRAIN_RUN,
+	EXPENSES_ROADVEH_RUN,
+	EXPENSES_AIRCRAFT_RUN,
+	EXPENSES_SHIP_RUN,
+	EXPENSES_SHARED_RUN,
+	EXPENSES_PROPERTY,
+	EXPENSES_LOAN_INT,
+	INVALID_EXPENSES,
+	EXPENSES_CONSTRUCTION,
+	EXPENSES_NEW_VEHICLES,
+	EXPENSES_OTHER,
+	INVALID_EXPENSES,
+};
+
 /** Expense list container. */
 struct ExpensesList {
 	const ExpensesType *et;   ///< Expenses items.
@@ -118,6 +163,8 @@
 static const ExpensesList _expenses_list_types[] = {
 	ExpensesList(_expenses_list_1, lengthof(_expenses_list_1), 0),
 	ExpensesList(_expenses_list_2, lengthof(_expenses_list_2), 3),
+	ExpensesList(_expenses_list_3, lengthof(_expenses_list_3), 0),
+	ExpensesList(_expenses_list_4, lengthof(_expenses_list_4), 3),
 };
 
 /** Widgets of the company finances windows. */
@@ -153,14 +200,12 @@
  */
 static void DrawCategories(const Rect &r)
 {
+	int type = _settings_game.sharing.sharing_mode != SHARING_NONE ? _settings_client.gui.expenses_layout + 2 : _settings_client.gui.expenses_layout;
 	int y = r.top;
-
-	DrawString(r.left, r.right, y, STR_FINANCES_EXPENDITURE_INCOME_TITLE, TC_FROMSTRING, SA_CENTER, true);
 	y += FONT_HEIGHT_NORMAL + EXP_LINESPACE;
 
-	int type = _settings_client.gui.expenses_layout;
 	for (uint i = 0; i < _expenses_list_types[type].length; i++) {
-		const ExpensesType et = _expenses_list_types[type].et[i];
+		ExpensesType et = _expenses_list_types[type].et[i];
 		if (et == INVALID_EXPENSES) {
 			y += EXP_LINESPACE;
 			DrawString(r.left, r.right, y, STR_FINANCES_TOTAL_CAPTION, TC_FROMSTRING, SA_RIGHT);
@@ -207,7 +252,7 @@
 
 	Money sum = 0;
 	Money subtotal = 0;
-	int type = _settings_client.gui.expenses_layout;
+	int type = _settings_game.sharing.sharing_mode != SHARING_NONE ? _settings_client.gui.expenses_layout + 2: _settings_client.gui.expenses_layout;
 	for (uint i = 0; i < _expenses_list_types[type].length; i++) {
 		const ExpensesType et = _expenses_list_types[type].et[i];
 		if (et == INVALID_EXPENSES) {
@@ -323,7 +368,7 @@
 
 	virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *resize)
 	{
-		int type = _settings_client.gui.expenses_layout;
+		int type = _settings_game.sharing.sharing_mode != SHARING_NONE ? _settings_client.gui.expenses_layout + 2: _settings_client.gui.expenses_layout;
 		switch (widget) {
 			case CFW_EXPS_CATEGORY:
 				size->width  = _expenses_list_types[type].GetCategoriesWidth();
@@ -412,8 +457,9 @@
 	virtual void OnPaint()
 	{
 		if (!small) {
+			int type = _settings_game.sharing.sharing_mode != SHARING_NONE ? _settings_client.gui.expenses_layout + 2: _settings_client.gui.expenses_layout;
+			// TODO: What is this magic 26?!
 			/* Check that the expenses panel height matches the height needed for the layout. */
-			int type = _settings_client.gui.expenses_layout;
 			if (_expenses_list_types[type].GetHeight() != this->GetWidget<NWidgetCore>(CFW_EXPS_CATEGORY)->current_y) {
 				this->SetupWidgets();
 				this->ReInit();
@@ -1425,6 +1471,7 @@
 	CW_WIDGET_COMPANY_NAME,
 	CW_WIDGET_BUILD_VIEW_HQ,
 	CW_WIDGET_RELOCATE_HQ,
+	CW_WIDGET_COMPANY_SHARING,
 	CW_WIDGET_BUY_SHARE,
 	CW_WIDGET_SELL_SHARE,
 	CW_WIDGET_COMPANY_PASSWORD,
@@ -1441,6 +1488,7 @@
 { WWT_PUSHTXTBTN,   RESIZE_NONE,  COLOUR_GREY,   270,   359,   158,   169, STR_COMPANY_VIEW_COMPANY_NAME_BUTTON,   STR_COMPANY_VIEW_COMPANY_NAME_TOOLTIP},
 {    WWT_TEXTBTN,   RESIZE_NONE,  COLOUR_GREY,   266,   355,    18,    29, STR_COMPANY_VIEW_VIEW_HQ_BUTTON,        STR_COMPANY_VIEW_BUILD_HQ_TOOLTIP},
 {    WWT_TEXTBTN,   RESIZE_NONE,  COLOUR_GREY,   266,   355,    32,    43, STR_COMPANY_VIEW_RELOCATE_HQ,                        STR_COMPANY_VIEW_RELOCATE_COMPANY_HEADQUARTERS},
+{ WWT_PUSHTXTBTN,   RESIZE_NONE,  COLOUR_GREY,   266,   355,    46,    57, STR_COMPANY_VIEW_SHARING_BUTTON,        STR_COMPANY_VIEW_SHARING_TOOLTIP},
 { WWT_PUSHTXTBTN,   RESIZE_NONE,  COLOUR_GREY,     0,   179,   158,   169, STR_COMPANY_VIEW_BUY_SHARE_BUTTON,      STR_COMPANY_VIEW_BUY_SHARE_TOOLTIP},
 { WWT_PUSHTXTBTN,   RESIZE_NONE,  COLOUR_GREY,   180,   359,   158,   169, STR_COMPANY_VIEW_SELL_SHARE_BUTTON,     STR_COMPANY_VIEW_SELL_SHARE_TOOLTIP},
 { WWT_PUSHTXTBTN,   RESIZE_NONE,  COLOUR_GREY,   266,   355,   138,   149, STR_COMPANY_VIEW_PASSWORD,                   STR_COMPANY_VIEW_PASSWORD_TOOLTIP},
@@ -1466,7 +1514,13 @@
 			NWidget(WWT_TEXTBTN, COLOUR_GREY, CW_WIDGET_RELOCATE_HQ), SetMinimalSize(90, 12), SetPadding(0, 4, 0, 0),
 										SetDataTip(STR_COMPANY_VIEW_RELOCATE_HQ, STR_COMPANY_VIEW_RELOCATE_COMPANY_HEADQUARTERS),
 		EndContainer(),
-		NWidget(NWID_SPACER), SetMinimalSize(0, 94),
+		NWidget(NWID_SPACER), SetMinimalSize(0, 2),
+		NWidget(NWID_HORIZONTAL),
+			NWidget(NWID_SPACER), SetFill(true, false),
+			NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, CW_WIDGET_COMPANY_SHARING), SetMinimalSize(90, 12), SetPadding(0, 4, 0, 0),
+										SetDataTip(STR_COMPANY_VIEW_SHARING_BUTTON, STR_COMPANY_VIEW_SHARING_TOOLTIP),
+		EndContainer(),
+		NWidget(NWID_SPACER), SetMinimalSize(0, 80),
 		/* Multi player buttons. */
 		NWidget(NWID_SELECTION, INVALID_COLOUR, -1),
 			NWidget(NWID_HORIZONTAL),
@@ -1601,6 +1655,7 @@
 		this->SetWidgetHiddenState(CW_WIDGET_RELOCATE_HQ,      !local || c->location_of_HQ == INVALID_TILE);
 		this->SetWidgetHiddenState(CW_WIDGET_BUY_SHARE,        local);
 		this->SetWidgetHiddenState(CW_WIDGET_SELL_SHARE,       local);
+		this->SetWidgetHiddenState(CW_WIDGET_COMPANY_SHARING,  _settings_game.sharing.sharing_mode != SHARING_INDIVIDUAL);
 		this->SetWidgetHiddenState(CW_WIDGET_COMPANY_PASSWORD, !local || !_networking);
 		this->SetWidgetHiddenState(CW_WIDGET_COMPANY_JOIN,     local || !_networking);
 		this->SetWidgetDisabledState(CW_WIDGET_COMPANY_JOIN,   c->is_ai);
@@ -1717,6 +1772,10 @@
 				DoCommandP(0, this->window_number, 0, CMD_SELL_SHARE_IN_COMPANY | CMD_MSG(STR_ERROR_CAN_T_SELL_25_SHARE_IN));
 				break;
 
+			case CW_WIDGET_COMPANY_SHARING:
+				ShowCompanySharingSettings((CompanyID)this->window_number);
+				break;
+
 #ifdef ENABLE_NETWORK
 			case CW_WIDGET_COMPANY_PASSWORD:
 				if (this->window_number == _local_company) ShowNetworkCompanyPasswordWindow(this);
@@ -1880,3 +1939,492 @@
 {
 	AllocateWindowDescFront<BuyCompanyWindow>(&_buy_company_desc, company);
 }
+
+/** per-company sharing settings window */
+enum SharingSettingsWindowWidgets {
+	SSW_WIDGET_CLOSEBOX = 0,
+	SSW_WIDGET_CAPTION,
+	SSW_WIDGET_STICKY,
+	SSW_WIDGET_PANEL,
+	SSW_WIDGET_ICONS,
+	SSW_WIDGET_SETTINGS,
+	SSW_WIDGET_CURRENT,
+	SSW_WIDGET_BUTTONS,
+	SSW_WIDGET_FUTURE,
+	SSW_WIDGET_SCROLL,
+	SSW_WIDGET_OPTIONS,
+	SSW_WIDGET_COMPANIES,
+	SSW_WIDGET_RESIZE,
+};
+
+/** Window that shows per-company infrastructure sharing settings and allows changing them */
+struct SharingSettingsWindow : public Window {
+private:
+	/** order of lines in the options tab */
+	enum SharingSettingsWindowLine {
+		SSWL_HEADERS = 0,
+		SSWL_COMPANY_FIRST = 1,
+		SSWL_TRAIN_TRACK = 1,
+		SSWL_TRAIN_DEPOT,
+		SSWL_TRAIN_STATION,
+		SSWL_TRAIN_FEE,
+		SSWL_ROAD_ROADSTOP = 6,
+		SSWL_ROAD_FEE,
+		SSWL_SHIP_DOCK = 9,
+		SSWL_SHIP_FEE,
+		SSWL_PLANE_AIRPORT = 12,
+		SSWL_PLANE_FEE,
+		SSWL_END,
+	};
+
+	Company *company;                     ///< The company this window belongs to
+	CompanySharingSettingsEntry *future;  ///< future sharing settings
+	CompanySharingSettingsEntry *current; ///< current sharing settings
+
+	bool companies_tab;                   ///< Is the companies tab currently open?
+
+	VehicleType current_query_type;       ///< type of vehicle for which a querystring window is open.
+	byte clicked_row;                     ///< which of the arrow sets was clicked
+	byte clicked_side;                    ///< was the left or the right arrow clicked?
+
+	uint line_height;                     ///< Height of a single line of text + buttons
+
+	/**
+	 * Draw a string on top of a certain widget.
+	 * @param widget The widget to draw the string on.
+	 * @param y The y-coordinate to draw on.
+	 * @param string The string to draw.
+	 * @param colour TextColour to use.
+	 */
+	inline void DrawWidgetString(int widget, uint y, StringID string, TextColour colour = TC_FROMSTRING)
+	{
+		NWidgetCore *wi = this->GetWidget<NWidgetCore>(widget);
+		DrawString(wi->pos_x, wi->pos_x + wi->current_x - 1, y, string, colour);
+	}
+
+	/**
+	 * Draw the value of a boolean setting ([Not] Allowed) in the column 'current' or 'future'.
+	 * @param y The y-coordinate where the string should be drawn.
+	 * @param value The boolean value to draw (Allowed / Not allowed).
+	 * @param column_future If true, draw in the column 'future', if false, draw in the column 'current'.
+	 */
+	inline void DrawBoolValue(uint y, bool value, bool column_future)
+	{
+		this->DrawWidgetString(column_future ? SSW_WIDGET_FUTURE : SSW_WIDGET_CURRENT, y, value ? STR_SHARING_VALUE_ALLOWED : STR_SHARING_VALUE_NOT_ALLOWED);
+ 	}
+
+	/**
+	 * Draw a boolean sharing setting.
+	 * @param str The string to draw. (any parameters can be set beforehand)
+	 * @param y The y-coordinate where the string should be drawn. Is increased by this->line_height after drawing.
+	 * @param value_current The boolean value to draw (Allowed / Not allowed) in the column 'current'.
+	 * @param value_future The boolean value to draw (Allowed / Not allowed) in the column 'future'.
+	 */
+	void DrawSharingOption(StringID str, uint &y, bool value_current, bool value_future)
+	{
+		static const Colours _button_colour_table[2][2] = {{COLOUR_CREAM, COLOUR_RED}, {COLOUR_DARK_GREEN, COLOUR_GREEN}};
+
+		NWidgetCore *wi = this->GetWidget<NWidgetCore>(SSW_WIDGET_BUTTONS);
+		DrawFrameRect(wi->pos_x, y, wi->pos_x + wi->current_x - 1, y + 8,
+			_button_colour_table[!!value_future][_local_company == this->owner], value_future ? FR_LOWERED : FR_NONE);
+
+		this->DrawWidgetString(SSW_WIDGET_SETTINGS, y, str);
+		this->DrawBoolValue(y, value_current, false);
+		this->DrawBoolValue(y, value_future, true);
+		y += this->line_height;
+	}
+
+	/**
+	 * Should the text line given by the parameter be drawn on de screen? (depends on scrolling)
+	 * Parameter 'line' is passed by reference, and always incremented by one.
+	 * @param Line the line to draw or not to draw.
+	 * @return True if the line is to be drawn.
+	 */
+	inline bool ShouldDrawLine(uint &line)
+	{
+		return this->vscroll.IsVisible(line++);
+	}
+
+	/**
+	 * Handle a click on a boolean option.
+	 * @param type The vehicle type that was clicked on.
+	 * @param option Bitmask of sharing options (station/depot/tracks) to be changed.
+	 */
+	void HandleOptionClick(VehicleType type, uint8 option)
+	{
+		byte value = this->company->sharing_future.GetSharingOption(type) ^ option;
+		/* for trains, we don't want tracks to be disabled while stations or depots are enabled */
+		if (type == VEH_TRAIN && value != 0 && (value & SHARING_TRACK) == 0) {
+			if (option == SHARING_TRACK) {
+				/* tracks were just disabled, so clear all other bits as well */
+				value = 0;
+			} else {
+				/* stations or depots were just enabled, so enable tracks as well */
+				value |= SHARING_TRACK;
+			}
+		}
+		DoCommandP(0, MODIFY_SHARING_OPTION | (type << 8), value, CMD_CHANGE_SHARING_SETTING | CMD_MSG(STR_ERROR_CAN_T_CHANGE_SHARING_SETTING));
+	}
+
+	/**
+	 * Handle a click on one of the fees.
+	 * @param type The vehicle type for which the fee is to be adjusted.
+	 * @param x The x-coordinate of the click, relative to the left edge of the button.
+	 * This is used to determine whether the left or right button was clicked.
+	 */
+	void HandleFeeClick(VehicleType type, int x)
+	{
+		if ((this->flags4 & WF_TIMEOUT_MASK) > WF_TIMEOUT_TRIGGER) {
+			_left_button_clicked = false;
+			return;
+		}
+		int half_button_width = this->GetWidget<NWidgetBase>(SSW_WIDGET_BUTTONS)->current_x / 2;
+
+		uint16 value = this->company->sharing_future.GetSharingFee(type);
+		uint16 new_value = value;
+		if (x < half_button_width && value > 0) {
+			/* left button */
+			this->clicked_row = type;
+			this->clicked_side = 1;
+			new_value = value - min(10, value);
+		} else if (x >= half_button_width && value < MAX_SHARING_FEE) {
+			/* right button */
+			this->clicked_row = type;
+			this->clicked_side = 2;
+			new_value = value + min(10, MAX_SHARING_FEE - value);
+		}
+		if (new_value != value) {
+			DoCommandP(0, MODIFY_SHARING_FEE | (type << 8), new_value, CMD_CHANGE_SHARING_SETTING | CMD_MSG(STR_ERROR_CAN_T_CHANGE_SHARING_SETTING));
+			this->flags4 |= WF_TIMEOUT_BEGIN;
+			_left_button_clicked = false;
+		}
+	}
+
+	/**
+	 * Compute the total number of items (text lines) in the window
+	 */
+	void ComputeScrollCount()
+	{
+		byte num_scroll;
+		if (this->companies_tab) {
+			num_scroll = SSWL_COMPANY_FIRST;
+			Company *c;
+			FOR_ALL_COMPANIES(c) {
+				if (this->company != c) num_scroll++;
+			}
+		} else {
+			num_scroll = SSWL_END;
+		}
+		if (this->company->sharing_delay > 0) {
+			num_scroll += 2; // 1 blank line and 1 line for the delay
+		}
+		this->vscroll.SetCount(num_scroll);
+	}
+
+	/**
+	 * Compute the number of items (text lines) that fit in the window
+	 */
+	void ComputeScrollCapacity()
+	{
+		this->vscroll.SetCapacity((this->GetWidget<NWidgetBase>(SSW_WIDGET_PANEL)->current_y - 2) / this->line_height);
+		/* Make sure the size is exactly correct */
+		assert(this->GetWidget<NWidgetBase>(SSW_WIDGET_PANEL)->current_y == (this->vscroll.GetCapacity() * this->line_height + 2));
+	}
+
+public:
+	/** Constructor */
+	SharingSettingsWindow(const WindowDesc *desc, WindowNumber number) : Window()
+	{
+		this->company = Company::GetIfValid(number);
+		assert(this->company != NULL);
+
+		this->InitNested(desc, number);
+
+		/* Set this->owner now, because otherwise InitNested overrides this value
+		 * This means we cannot use this variable for anything (including size computations!) in the initialization stage */
+		this->owner = (Owner)number;
+
+		this->ComputeScrollCount();
+		this->ComputeScrollCapacity();
+
+		this->DisableWidget(SSW_WIDGET_OPTIONS);
+	}
+
+	virtual void OnPaint()
+	{
+		this->DrawWidgets();
+
+		CompanySharingSettingsEntry *future = &this->company->sharing_future;
+		CompanySharingSettingsEntry *current = &this->company->sharing_current;
+
+		uint y = this->GetWidget<NWidgetBase>(SSW_WIDGET_PANEL)->pos_y + 1;
+		uint current_line = SSWL_HEADERS;
+
+		/* draw text headers */
+		if (this->ShouldDrawLine(current_line)) {
+			this->DrawWidgetString(SSW_WIDGET_CURRENT, y, STR_SHARING_CURRENT);
+			this->DrawWidgetString(SSW_WIDGET_FUTURE,  y, STR_SHARING_FUTURE);
+			y += this->line_height;
+		}
+
+		if (this->companies_tab) {
+			Company *c;
+			FOR_ALL_COMPANIES(c) {
+				if (this->company == c) continue; //Skip our own company, as it would be silly to allow / disallow yourself.
+				if (this->ShouldDrawLine(current_line)) {
+					DrawCompanyIcon(c->index, this->GetWidget<NWidgetBase>(SSW_WIDGET_ICONS)->pos_x + 2, y + 1);
+					SetDParam(0, c->index);
+					this->DrawSharingOption(STR_SHARING_SETTING_COMPANY, y, current->IsCompanyAllowed(c->index), future->IsCompanyAllowed(c->index));
+				}
+			}
+			if (current_line == SSWL_COMPANY_FIRST) {
+				/* it seems there are no companies at all, apart from ourselves */
+				if (this->ShouldDrawLine(current_line)) {
+					this->DrawWidgetString(SSW_WIDGET_SETTINGS, y, STR_SHARING_SETTING_NO_COMPANIES);
+					return; //don't draw the sharing delay
+				}
+			} else if (this->ShouldDrawLine(current_line)) {
+				/* Blank line */
+				y += this->line_height;
+			}
+		} else {
+			/* draw text and buttons for all vehicle types */
+			for (VehicleType type = VEH_TRAIN; type < NUM_SHARING_VEHTYPES; type++) {
+				SubSprite sub = {0, (this->vscroll.GetPosition() - current_line) * this->line_height, INT32_MAX, (this->vscroll.GetPosition() + this->vscroll.GetCapacity() - current_line) * this->line_height - 1};
+				DrawSprite(SPR_IMG_TRAINLIST + type, PAL_NONE, this->GetWidget<NWidgetBase>(SSW_WIDGET_ICONS)->pos_x, y, &sub);
+
+				/* Some special lines for trains */
+				if (type == VEH_TRAIN) {
+					if (this->ShouldDrawLine(current_line)) {
+						this->DrawSharingOption(STR_SHARING_SETTING_TRACK, y,
+							(current->GetSharingOption(VEH_TRAIN) & SHARING_TRACK) != 0,
+							(future->GetSharingOption(VEH_TRAIN) & SHARING_TRACK) != 0);
+					}
+					if (this->ShouldDrawLine(current_line)) {
+						this->DrawSharingOption(STR_SHARING_SETTING_DEPOT, y,
+							(current->GetSharingOption(VEH_TRAIN) & SHARING_DEPOT) != 0,
+							(future->GetSharingOption(VEH_TRAIN) & SHARING_DEPOT) != 0);
+					}
+				}
+
+				/** String lookup table for station names ('Station', 'road stop' etc) */
+				static const StringID sharing_station_string[] = {
+					STR_SHARING_SETTING_STATION,
+					STR_SHARING_SETTING_ROADSTOP,
+					STR_SHARING_SETTING_DOCK,
+					STR_SHARING_SETTING_AIRPORT,
+				};
+
+				if (this->ShouldDrawLine(current_line)) {
+					this->DrawSharingOption(sharing_station_string[type], y,
+						(current->GetSharingOption(type) & SHARING_STATION) != 0,
+						(future->GetSharingOption(type) & SHARING_STATION) != 0);
+				}
+
+				/* sharing fee */
+				if (this->ShouldDrawLine(current_line)) {
+					/* Draw arrow buttons in the correct state */
+					bool l_clickable = this->owner == _local_company && future->GetSharingFee(type) > 0;
+					bool r_clickable = this->owner == _local_company && future->GetSharingFee(type) < MAX_SHARING_FEE;
+					byte arrow_clicked = (type == this->clicked_row) ? this->clicked_side : 0;
+					if ((arrow_clicked == 1 && !l_clickable) || (arrow_clicked == 2 && !r_clickable)) {
+						arrow_clicked = 0;
+					}
+					DrawArrowButtons(this->GetWidget<NWidgetBase>(SSW_WIDGET_BUTTONS)->pos_x, y, COLOUR_YELLOW, arrow_clicked, l_clickable, r_clickable);
+
+					/* Draw the fee strings */
+					this->DrawWidgetString(SSW_WIDGET_SETTINGS, y, type == VEH_TRAIN ? STR_SHARING_SETTING_MONTHLY_TOLL : STR_SHARING_SETTING_FEE, TC_BLACK);
+					SetDParam(0, current->GetSharingFee(type));
+					this->DrawWidgetString(SSW_WIDGET_CURRENT,  y, STR_JUST_CURRENCY, TC_ORANGE);
+					SetDParam(0, future->GetSharingFee(type));
+					this->DrawWidgetString(SSW_WIDGET_FUTURE,   y, STR_JUST_CURRENCY, TC_ORANGE);
+					y += this->line_height;
+				}
+				/* Add some spacing */
+				if (this->ShouldDrawLine(current_line)) y += this->line_height;
+			}
+		}
+
+		/* draw sharing delay */
+		if (this->ShouldDrawLine(current_line) && this->company->sharing_delay > 0) {
+			this->DrawWidgetString(SSW_WIDGET_SETTINGS, y, STR_SHARING_CHANGES_TAKE_EFFECT);
+			SetDParam(0, this->company->sharing_delay);
+			this->DrawWidgetString(SSW_WIDGET_CURRENT, y, STR_SHARING_TAKE_EFFECT_MONTHS);
+		}
+	}
+
+	virtual void SetStringParameters(int widget) const
+	{
+		if (widget == SSW_WIDGET_CAPTION) SetDParam(0, this->company->index);
+	}
+
+	virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *resize)
+	{
+		Dimension d = {0, 0};
+		switch (widget) {
+			case SSW_WIDGET_ICONS:
+				for (VehicleType type = VEH_TRAIN; type < NUM_SHARING_VEHTYPES; type++) {
+					d = maxdim(d, GetSpriteSize(SPR_IMG_TRAINLIST + type));
+				}
+				break;
+			case SSW_WIDGET_SETTINGS:
+				this->line_height = max(11, FONT_HEIGHT_NORMAL + 1); //minimal line height is 11
+				resize->height = this->line_height;
+
+				d = maxdim(GetStringBoundingBox(STR_SHARING_SETTING_MONTHLY_TOLL), GetStringBoundingBox(STR_SHARING_SETTING_FEE));
+				d = maxdim(d, GetStringBoundingBox(STR_SHARING_SETTING_NO_COMPANIES));
+				d = maxdim(d, GetStringBoundingBox(STR_SHARING_CHANGES_TAKE_EFFECT));
+				for (CompanyID c = COMPANY_FIRST; c < MAX_COMPANIES; c++) {
+					if (Company::IsValidID(c) && c != this->company->index) {
+						SetDParam(0, c);
+						d = maxdim(d, GetStringBoundingBox(STR_SHARING_SETTING_COMPANY));
+					}
+				}
+				/* make the minimal height a multiple of line_height ( + 2 pixels of spacing) */
+				{
+					uint num_lines = (size->height - 1) / this->line_height + 1; //round upwards
+					d.height = num_lines * this->line_height + 2;
+				}
+				break;
+			case SSW_WIDGET_CURRENT:
+			case SSW_WIDGET_FUTURE:
+				d = maxdim(GetStringBoundingBox(STR_SHARING_VALUE_ALLOWED), GetStringBoundingBox(STR_SHARING_VALUE_NOT_ALLOWED));
+				SetDParam(0, MAX_SHARING_FEE);
+				d = maxdim(d, GetStringBoundingBox(STR_JUST_CURRENCY));
+				d = maxdim(d, GetStringBoundingBox((widget == SSW_WIDGET_CURRENT) ? STR_SHARING_CURRENT : STR_SHARING_FUTURE));
+				break;
+			default: return;
+		}
+		*size = maxdim(*size, d);
+	}
+
+	virtual void OnClick(Point pt, int widget)
+	{
+		if (widget == SSW_WIDGET_OPTIONS || widget == SSW_WIDGET_COMPANIES) {
+			this->companies_tab = (widget == SSW_WIDGET_COMPANIES);
+			this->SetWidgetDisabledState(SSW_WIDGET_OPTIONS, !this->companies_tab);
+			this->SetWidgetDisabledState(SSW_WIDGET_COMPANIES, this->companies_tab);
+			this->ComputeScrollCount();
+			this->SetDirty();
+			return;
+		}
+		if (widget != SSW_WIDGET_BUTTONS && widget != SSW_WIDGET_FUTURE) return;
+		if (this->owner != _local_company) return;
+
+		int y = pt.y - this->GetWidget<NWidgetBase>(SSW_WIDGET_BUTTONS)->pos_y - 1;
+		/* don't allow clicking on the bottommost or topmost pixel of a line */
+		if (y <= 0 || y % this->line_height == 0 || y % this->line_height == this->line_height - 1) return;
+		int line = (y / this->line_height) + this->vscroll.GetPosition();
+
+		if (widget == SSW_WIDGET_FUTURE) {
+			/* Possibly clicked a fee, if that is the case, show a query string window. */
+			switch (line) {
+				case SSWL_TRAIN_FEE: this->current_query_type = VEH_TRAIN;    break;
+				case SSWL_ROAD_FEE:  this->current_query_type = VEH_ROAD;     break;
+				case SSWL_SHIP_FEE:  this->current_query_type = VEH_SHIP;     break;
+				case SSWL_PLANE_FEE: this->current_query_type = VEH_AIRCRAFT; break;
+				default: return;
+			}
+			SetDParam(0, this->company->sharing_future.GetSharingFee(this->current_query_type) * _currency->rate);
+			ShowQueryString(STR_JUST_INT, STR_SHARING_CHANGE_FEE_CAPTION, 10, 0, this, CS_NUMERAL, QSF_NONE);
+		} else if (this->companies_tab) { //else: SSW_WIDGET_BUTTONS
+			line -=  SSWL_COMPANY_FIRST;
+			/* Find out which company button we clicked on */
+			Company *c;
+			FOR_ALL_COMPANIES(c) {
+				if (c == this->company) continue;
+				if (line-- == 0) {
+					DoCommandP(0, MODIFY_SHARING_COMPANIES, this->company->sharing_future.allowed_companies ^ (1 << c->index), CMD_CHANGE_SHARING_SETTING | CMD_MSG(STR_ERROR_CAN_T_CHANGE_SHARING_SETTING));
+					break;
+				}
+			}
+		} else {
+			int x = pt.x - this->GetWidget<NWidgetBase>(SSW_WIDGET_BUTTONS)->pos_x;
+			switch (line) {
+				case SSWL_TRAIN_TRACK:   this->HandleOptionClick(VEH_TRAIN, SHARING_TRACK); return;
+				case SSWL_TRAIN_DEPOT:   this->HandleOptionClick(VEH_TRAIN, SHARING_DEPOT); return;
+				case SSWL_TRAIN_STATION: this->HandleOptionClick(VEH_TRAIN, SHARING_STATION); return;
+				case SSWL_TRAIN_FEE:     this->HandleFeeClick(VEH_TRAIN, x); return;
+				case SSWL_ROAD_ROADSTOP: this->HandleOptionClick(VEH_ROAD, SHARING_STATION | SHARING_DEPOT); return;
+				case SSWL_ROAD_FEE:      this->HandleFeeClick(VEH_ROAD, x); return;
+				case SSWL_SHIP_DOCK:     this->HandleOptionClick(VEH_SHIP, SHARING_STATION | SHARING_DEPOT); return;
+				case SSWL_SHIP_FEE:      this->HandleFeeClick(VEH_SHIP, x); return;
+				case SSWL_PLANE_AIRPORT: this->HandleOptionClick(VEH_AIRCRAFT, SHARING_STATION | SHARING_DEPOT); return;
+				case SSWL_PLANE_FEE:     this->HandleFeeClick(VEH_AIRCRAFT, x); return;
+				default: return;
+			}
+		}
+	}
+
+	virtual void OnInvalidateData(int data = 0)
+	{
+		this->ComputeScrollCount();
+	}
+
+	virtual void OnResize()
+	{
+		this->ComputeScrollCapacity();
+	}
+
+	virtual void OnQueryTextFinished(char *str)
+	{
+		if (StrEmpty(str)) return;
+		int32 value = atoi(str);
+		value /= _currency->rate;
+		value = Clamp(value, 0, MAX_SHARING_FEE);
+		DoCommandP(0, MODIFY_SHARING_FEE | (this->current_query_type << 8), value, CMD_CHANGE_SHARING_SETTING | CMD_MSG(STR_ERROR_CAN_T_CHANGE_SHARING_SETTING));
+	}
+
+	virtual void OnTimeout()
+	{
+		this->clicked_row = 0;
+		this->clicked_side = 0;
+		this->SetDirty();
+	}
+};
+
+static const NWidgetPart _nested_sharing_settings_widgets[] = {
+	NWidget(NWID_HORIZONTAL), // Window header
+		NWidget(WWT_CLOSEBOX, COLOUR_GREY, SSW_WIDGET_CLOSEBOX), SetFill(false, true),
+		NWidget(WWT_CAPTION, COLOUR_GREY, SSW_WIDGET_CAPTION), SetDataTip(STR_SHARING_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
+		NWidget(WWT_STICKYBOX, COLOUR_GREY, SSW_WIDGET_STICKY), SetFill(false, true),
+	EndContainer(),
+	NWidget(NWID_HORIZONTAL),
+		NWidget(WWT_PANEL, COLOUR_GREY, SSW_WIDGET_PANEL), SetDataTip(0x0, STR_SHARING_PANEL_TOOLTIP), SetFill(true, true),
+			NWidget(NWID_HORIZONTAL), SetPIP(2, 2, 2),
+				/* Empty widgets to position the text columns */
+				NWidget(WWT_EMPTY, COLOUR_GREY, SSW_WIDGET_ICONS),    SetMinimalSize(20,  90), SetResize(0, 1),
+				NWidget(WWT_EMPTY, COLOUR_GREY, SSW_WIDGET_SETTINGS), SetMinimalSize(160, 90), SetResize(1, 1),
+				NWidget(WWT_EMPTY, COLOUR_GREY, SSW_WIDGET_CURRENT),  SetMinimalSize(80,  90), SetResize(1, 1),
+				NWidget(WWT_EMPTY, COLOUR_GREY, SSW_WIDGET_BUTTONS),  SetMinimalSize(20,  90), SetResize(0, 1), SetPadding(0, 2, 0, 2),
+				NWidget(WWT_EMPTY, COLOUR_GREY, SSW_WIDGET_FUTURE),   SetMinimalSize(80,  90), SetResize(1, 1),
+			EndContainer(),
+		EndContainer(),
+		NWidget(WWT_SCROLLBAR, COLOUR_GREY, SSW_WIDGET_SCROLL),
+	EndContainer(),
+	NWidget(NWID_HORIZONTAL),
+		NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, SSW_WIDGET_OPTIONS),   SetMinimalSize(0, 12), SetResize(1, 0),
+							SetDataTip(STR_SHARING_OPTIONS, STR_SHARING_OPTIONS_TOOLTIP), SetFill(true, false),
+		NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, SSW_WIDGET_COMPANIES), SetMinimalSize(0, 12), SetResize(1, 0),
+							SetDataTip(STR_SHARING_COMPANIES, STR_SHARING_COMPANIES_TOOLTIP), SetFill(true, false),
+		NWidget(WWT_RESIZEBOX,  COLOUR_GREY, SSW_WIDGET_RESIZE), SetFill(false, true),
+	EndContainer(),
+};
+
+static const WindowDesc _sharing_settings_desc(
+	WDP_AUTO, WDP_AUTO, 388, 116, 388, 215,
+	WC_SHARING_OPTIONS, WC_COMPANY,
+	WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET | WDF_UNCLICK_BUTTONS | WDF_STICKY_BUTTON | WDF_RESIZABLE,
+	NULL, _nested_sharing_settings_widgets, lengthof(_nested_sharing_settings_widgets)
+);
+
+/**
+ * Show the sharing window of a company.
+ * @param company The company whose window to show.
+ */
+static void ShowCompanySharingSettings(CompanyID company)
+{
+	if ((_settings_game.sharing.sharing_mode == SHARING_INDIVIDUAL) && Company::IsValidID(company)) {
+		AllocateWindowDescFront<SharingSettingsWindow>(&_sharing_settings_desc, company);
+	}
+}
Index: src/news_gui.cpp
===================================================================
--- src/news_gui.cpp	(revision 17973)
+++ src/news_gui.cpp	(working copy)
@@ -238,6 +238,7 @@
 	{ NT_COMPANY_INFO,     NF_NONE,                        &_company_news_desc }, ///< NS_COMPANY_MERGER
 	{ NT_COMPANY_INFO,     NF_NONE,                        &_company_news_desc }, ///< NS_COMPANY_BANKRUPT
 	{ NT_COMPANY_INFO,     NF_NONE,                        &_company_news_desc }, ///< NS_COMPANY_NEW
+	{ NT_COMPANY_INFO,     NF_NONE,                        &_company_news_desc }, ///< NS_COMPANY_SHARING
 	{ NT_INDUSTRY_OPEN,    (NF_NO_TRANSPARENT | NF_SHADE), &_thin_news_desc    }, ///< NS_INDUSTRY_OPEN
 	{ NT_INDUSTRY_CLOSE,   (NF_NO_TRANSPARENT | NF_SHADE), &_thin_news_desc    }, ///< NS_INDUSTRY_CLOSE
 	{ NT_ECONOMY,          NF_NONE,                        &_normal_news_desc  }, ///< NS_ECONOMY
@@ -311,6 +312,10 @@
 				this->GetWidget<NWidgetCore>(NTW_TITLE)->widget_data = STR_NEWS_COMPANY_LAUNCH_TITLE;
 				break;
 
+			case NS_COMPANY_SHARING:
+				this->GetWidget<NWidgetCore>(NTW_TITLE)->widget_data = STR_NEWS_SHARING_TITLE;
+				break;
+
 			default:
 				break;
 		}
@@ -526,6 +531,12 @@
 				SetDParam(1, this->ni->params[3]);
 				return STR_NEWS_COMPANY_LAUNCH_DESCRIPTION;
 
+			case NS_COMPANY_SHARING:
+				SetDParam(0, ni->params[2]);
+				SetDParam(1, ni->params[3]);
+				return ni->params[1];
+				break;
+
 			default:
 				NOT_REACHED();
 		}
Index: src/vehicle_gui.cpp
===================================================================
--- src/vehicle_gui.cpp	(revision 17973)
+++ src/vehicle_gui.cpp	(working copy)
@@ -39,6 +39,7 @@
 #include "vehiclelist.h"
 #include "articulated_vehicles.h"
 #include "cargotype.h"
+#include "infrastructure_func.h"
 
 #include "table/sprites.h"
 #include "table/strings.h"
@@ -1812,6 +1813,7 @@
 	{
 		const Vehicle *v = Vehicle::Get(this->window_number);
 		bool is_localcompany = v->owner == _local_company;
+		bool can_control = IsVehicleControlAllowed(v);
 		bool refitable_and_stopped_in_depot = IsVehicleRefitable(v);
 
 		this->SetWidgetDisabledState(VVW_WIDGET_GOTO_DEPOT, !is_localcompany);
@@ -1819,8 +1821,8 @@
 		this->SetWidgetDisabledState(VVW_WIDGET_CLONE_VEH, !is_localcompany);
 
 		if (v->type == VEH_TRAIN) {
-			this->SetWidgetDisabledState(VVW_WIDGET_FORCE_PROCEED, !is_localcompany);
-			this->SetWidgetDisabledState(VVW_WIDGET_TURN_AROUND, !is_localcompany);
+			this->SetWidgetDisabledState(VVW_WIDGET_FORCE_PROCEED, !can_control);
+			this->SetWidgetDisabledState(VVW_WIDGET_TURN_AROUND, !can_control);
 		}
 
 		this->DrawWidgets();
Index: src/saveload/saveload.cpp
===================================================================
--- src/saveload/saveload.cpp	(revision 17973)
+++ src/saveload/saveload.cpp	(working copy)
@@ -47,8 +47,9 @@
 
 #include "saveload_internal.h"
 
-extern const uint16 SAVEGAME_VERSION = 127;
+extern const uint16 SAVEGAME_VERSION = SL_IS;
 
+
 SavegameType _savegame_type; ///< type of savegame we are loading
 
 uint32 _ttdp_version;     ///< version of TTDP savegame (if applicable)
Index: src/saveload/company_sl.cpp
===================================================================
--- src/saveload/company_sl.cpp	(revision 17973)
+++ src/saveload/company_sl.cpp	(working copy)
@@ -123,6 +123,8 @@
 
 	    SLE_VAR(Company, num_valid_stat_ent,    SLE_UINT8),
 
+	SLE_CONDVAR(Company, sharing_delay,         SLE_UINT8,               SL_IS, SL_MAX_VERSION),
+
 	    SLE_VAR(Company, quarters_of_bankruptcy,SLE_UINT8),
 	SLE_CONDVAR(Company, bankrupt_asked,        SLE_FILE_U8  | SLE_VAR_U16,  0, 103),
 	SLE_CONDVAR(Company, bankrupt_asked,        SLE_UINT16,                104, SL_MAX_VERSION),
@@ -132,7 +134,8 @@
 
 	/* yearly expenses was changed to 64-bit in savegame version 2. */
 	SLE_CONDARR(Company, yearly_expenses,       SLE_FILE_I32 | SLE_VAR_I64, 3 * 13, 0, 1),
-	SLE_CONDARR(Company, yearly_expenses,       SLE_INT64, 3 * 13,                  2, SL_MAX_VERSION),
+	SLE_CONDARR(Company, yearly_expenses,       SLE_INT64, 3 * 13,                  2, SL_IS - 1),
+	SLE_CONDARR(Company, yearly_expenses,       SLE_INT64, 3 * 15,              SL_IS, SL_MAX_VERSION),
 
 	SLE_CONDVAR(Company, is_ai,                 SLE_BOOL,                    2, SL_MAX_VERSION),
 	SLE_CONDNULL(1, 107, 111), ///< is_noai
@@ -226,6 +229,13 @@
 	SLE_END()
 };
 
+static const SaveLoad _company_sharing_desc[] = {
+	SLE_CONDARR(CompanySharingSettingsEntry,            option,  SLE_UINT8, 4, SL_IS, SL_MAX_VERSION),
+	SLE_CONDARR(CompanySharingSettingsEntry,               fee, SLE_UINT16, 4, SL_IS, SL_MAX_VERSION),
+	SLE_CONDVAR(CompanySharingSettingsEntry, allowed_companies, SLE_UINT16,    SL_IS, SL_MAX_VERSION),
+	SLE_END()
+};
+
 static void SaveLoad_PLYR(Company *c)
 {
 	int i;
@@ -243,6 +253,10 @@
 		}
 	}
 
+	/* Write sharing settings */
+	SlObject(&c->sharing_current, _company_sharing_desc);
+	SlObject(&c->sharing_future,  _company_sharing_desc);
+
 	/* Write economy */
 	SlObject(&c->cur_economy, _company_economy_desc);
 
Index: src/saveload/afterload.cpp
===================================================================
--- src/saveload/afterload.cpp	(revision 17973)
+++ src/saveload/afterload.cpp	(working copy)
@@ -1946,6 +1946,33 @@
 			if (IsTileType(d->xy, MP_WATER)) _m[GetOtherShipDepotTile(d->xy)].m2 = d->index;
 		}
 	}
+	
+	if (CheckSavegameVersion(SL_IS)) {
+		/* Set the sharing settings to the standard values (no sharing)
+		 * as we do not want these to be copied from the newgame settings. */
+		_settings_game.sharing.sharing_mode         = SHARING_NONE;
+		_settings_game.sharing.new_individual_delay = 12;
+		_settings_game.sharing.rail_sharing         = 0;
+		_settings_game.sharing.road_sharing         = false;
+		_settings_game.sharing.water_sharing        = false;
+		_settings_game.sharing.air_sharing          = false;
+		_settings_game.sharing.rail_fee             = 100;
+		_settings_game.sharing.road_fee             = 100;
+		_settings_game.sharing.water_fee            = 100;
+		_settings_game.sharing.air_fee              = 100;
+	
+		Company *c;
+		FOR_ALL_COMPANIES(c) {
+			/* yearly_expenses has 3*15 entries now, saveload code gave us 3*13.
+			 * Move the old data to the right place in the new array and clear the new data.
+			 * The move has to be done in reverse order (first 2, then 1). */
+			MemMoveT(&c->yearly_expenses[2][0], &c->yearly_expenses[1][11], 13);
+			MemMoveT(&c->yearly_expenses[1][0], &c->yearly_expenses[0][13], 13);
+			/* Clear the old location of just-moved data, so sharing income/expenses is set to 0 */
+			MemSetT(&c->yearly_expenses[0][13], 0, 2);
+			MemSetT(&c->yearly_expenses[1][13], 0, 2);
+		}
+	}
 
 	AfterLoadLabelMaps();
 
Index: src/saveload/saveload.h
===================================================================
--- src/saveload/saveload.h	(revision 17973)
+++ src/saveload/saveload.h	(working copy)
@@ -80,6 +80,7 @@
 };
 
 #define SL_MAX_VERSION 255
+#define SL_IS 128
 
 enum {
 	INC_VEHICLE_COMMON = 0,
Index: src/yapf/yapf_costrail.hpp
===================================================================
--- src/yapf/yapf_costrail.hpp	(revision 17973)
+++ src/yapf/yapf_costrail.hpp	(working copy)
@@ -34,6 +34,7 @@
 		Trackdir    td;
 		TileType    tile_type;
 		RailType    rail_type;
+		Owner       owner;
 
 		TILE()
 		{
@@ -41,6 +42,7 @@
 			td = INVALID_TRACKDIR;
 			tile_type = MP_VOID;
 			rail_type = INVALID_RAILTYPE;
+			owner = INVALID_OWNER;
 		}
 
 		TILE(TileIndex tile, Trackdir td)
@@ -49,6 +51,7 @@
 			this->td = td;
 			this->tile_type = GetTileType(tile);
 			this->rail_type = GetTileRailType(tile);
+			this->owner = GetTileOwner(tile);
 		}
 
 		TILE(const TILE &src)
@@ -57,6 +60,7 @@
 			td = src.td;
 			tile_type = src.tile_type;
 			rail_type = src.rail_type;
+			owner = src.owner;
 		}
 	};
 
@@ -450,10 +454,15 @@
 			if (!tf_local.Follow(cur.tile, cur.td)) {
 				assert(tf_local.m_err != TrackFollower::EC_NONE);
 				/* Can't move to the next tile (EOL?). */
-				if (tf_local.m_err == TrackFollower::EC_RAIL_TYPE) {
-					end_segment_reason |= ESRB_RAIL_TYPE;
-				} else {
-					end_segment_reason |= ESRB_DEAD_END;
+				switch (tf_local.m_err) {
+					case TrackFollower::EC_RAIL_TYPE:
+						end_segment_reason |= ESRB_RAIL_TYPE;
+						break;
+					case TrackFollower::EC_OWNER:
+						end_segment_reason |= ESRB_OWNER;
+						break;
+					default:
+						end_segment_reason |= ESRB_DEAD_END;
 				}
 
 				if (TrackFollower::DoTrackMasking() && !HasOnewaySignalBlockingTrackdir(cur.tile, cur.td)) {
@@ -484,6 +493,13 @@
 				break;
 			}
 
+			/* Check the next tile for the owner. */
+			if (next.owner != cur.owner) {
+				/* Segment must consist from tiles with the same owner. */
+				end_segment_reason |= ESRB_OWNER;
+				break;
+			}
+
 			/* Avoid infinite looping. */
 			if (next.tile == n.m_key.m_tile && next.td == n.m_key.m_td) {
 				end_segment_reason |= ESRB_INFINITE_LOOP;
Index: src/yapf/yapf_node_rail.hpp
===================================================================
--- src/yapf/yapf_node_rail.hpp	(revision 17973)
+++ src/yapf/yapf_node_rail.hpp	(working copy)
@@ -66,6 +66,7 @@
 	/* The following reasons can be saved into cached segment */
 	ESR_DEAD_END = 0,      ///< track ends here
 	ESR_RAIL_TYPE,         ///< the next tile has a different rail type than our tiles
+	ESR_OWNER,             ///< the next tile has a different owner than our tiles
 	ESR_INFINITE_LOOP,     ///< infinite loop detected
 	ESR_SEGMENT_TOO_LONG,  ///< the segment is too long (possible infinite loop)
 	ESR_CHOICE_FOLLOWS,    ///< the next tile contains a choice (the track splits to more than one segments)
@@ -90,6 +91,7 @@
 
 	ESRB_DEAD_END          = 1 << ESR_DEAD_END,
 	ESRB_RAIL_TYPE         = 1 << ESR_RAIL_TYPE,
+	ESRB_OWNER             = 1 << ESR_OWNER,
 	ESRB_INFINITE_LOOP     = 1 << ESR_INFINITE_LOOP,
 	ESRB_SEGMENT_TOO_LONG  = 1 << ESR_SEGMENT_TOO_LONG,
 	ESRB_CHOICE_FOLLOWS    = 1 << ESR_CHOICE_FOLLOWS,
@@ -109,7 +111,7 @@
 	ESRB_POSSIBLE_TARGET = ESRB_DEPOT | ESRB_WAYPOINT | ESRB_STATION | ESRB_SAFE_TILE,
 
 	/* What reasons can be stored back into cached segment. */
-	ESRB_CACHED_MASK = ESRB_DEAD_END | ESRB_RAIL_TYPE | ESRB_INFINITE_LOOP | ESRB_SEGMENT_TOO_LONG | ESRB_CHOICE_FOLLOWS | ESRB_DEPOT | ESRB_WAYPOINT | ESRB_STATION | ESRB_SAFE_TILE,
+	ESRB_CACHED_MASK = ESRB_DEAD_END | ESRB_RAIL_TYPE | ESRB_OWNER | ESRB_INFINITE_LOOP | ESRB_SEGMENT_TOO_LONG | ESRB_CHOICE_FOLLOWS | ESRB_DEPOT | ESRB_WAYPOINT | ESRB_STATION | ESRB_SAFE_TILE,
 
 	/* Reasons to abort pathfinding in this direction. */
 	ESRB_ABORT_PF_MASK = ESRB_DEAD_END | ESRB_PATH_TOO_LONG | ESRB_INFINITE_LOOP | ESRB_FIRST_TWO_WAY_RED,
@@ -120,7 +122,7 @@
 inline CStrA ValueStr(EndSegmentReasonBits bits)
 {
 	static const char * const end_segment_reason_names[] = {
-		"DEAD_END", "RAIL_TYPE", "INFINITE_LOOP", "SEGMENT_TOO_LONG", "CHOICE_FOLLOWS",
+		"DEAD_END", "RAIL_TYPE", "OWNER", "INFINITE_LOOP", "SEGMENT_TOO_LONG", "CHOICE_FOLLOWS",
 		"DEPOT", "WAYPOINT", "STATION", "SAFE_TILE",
 		"PATH_TOO_LONG", "FIRST_TWO_WAY_RED", "LOOK_AHEAD_END", "TARGET_REACHED"
 	};
Index: src/yapf/follow_track.hpp
===================================================================
--- src/yapf/follow_track.hpp	(revision 17973)
+++ src/yapf/follow_track.hpp	(working copy)
@@ -16,6 +16,7 @@
 #include "../depot_map.h"
 #include "../roadveh.h"
 #include "../train.h"
+#include "../infrastructure_func.h"
 
 /** Track follower helper template class (can serve pathfinders and vehicle
  *  controllers). See 6 different typedefs below for 3 different transport
@@ -62,12 +63,12 @@
 	{
 		assert(!IsRailTT() || (v != NULL && v->type == VEH_TRAIN));
 		m_veh = v;
-		Init(v != NULL ? v->owner : INVALID_OWNER, IsRailTT() && railtype_override == INVALID_RAILTYPES ? Train::From(v)->compatible_railtypes : railtype_override, pPerf);
+		Init(v != NULL ? v->owner : INVALID_OWNER, (IsRailTT() && railtype_override == INVALID_RAILTYPES && v != NULL) ? Train::From(v)->compatible_railtypes : railtype_override, pPerf);
 	}
 
 	FORCEINLINE void Init(Owner o, RailTypes railtype_override, CPerformanceTimer *pPerf)
 	{
-		assert((!IsRoadTT() || m_veh != NULL) && (!IsRailTT() || railtype_override != INVALID_RAILTYPES));
+		assert(!IsRoadTT() || m_veh != NULL);
 		m_veh_owner = o;
 		m_pPerf = pPerf;
 		/* don't worry, all is inlined so compiler should remove unnecessary initializations */
@@ -272,6 +273,7 @@
 	/** return true if we can enter m_new_tile from m_exitdir */
 	FORCEINLINE bool CanEnterNewTile()
 	{
+		bool ignore_owner = (m_veh_owner == INVALID_OWNER);
 		if (IsRoadTT() && IsStandardRoadStopTile(m_new_tile)) {
 			/* road stop can be entered from one direction only unless it's a drive-through stop */
 			DiagDirection exitdir = GetRoadStopDir(m_new_tile);
@@ -279,6 +281,11 @@
 				m_err = EC_NO_WAY;
 				return false;
 			}
+			/* road stops shouldn't be entered unless allowed to */
+			if (!(ignore_owner || IsStationUsageAllowed(m_new_tile, m_veh_owner, VEH_ROAD))) {
+				m_err = EC_OWNER;
+				return false;
+			}
 		}
 
 		/* single tram bits can only be entered from one direction */
@@ -297,8 +304,8 @@
 				m_err = EC_NO_WAY;
 				return false;
 			}
-			/* don't try to enter other company's depots */
-			if (GetTileOwner(m_new_tile) != m_veh_owner) {
+			/* don't try to enter other companies' depots, unless allowed to */
+			if (!(ignore_owner || IsDepotUsageAllowed(m_new_tile, m_veh_owner, VEH_ROAD))) {
 				m_err = EC_OWNER;
 				return false;
 			}
@@ -309,18 +316,23 @@
 				m_err = EC_NO_WAY;
 				return false;
 			}
+			if (!(ignore_owner || IsDepotUsageAllowed(m_new_tile, m_veh_owner, VEH_TRAIN))) {
+				m_err = EC_OWNER;
+				return false;
+			}
 		}
 
-		/* rail transport is possible only on tiles with the same owner as vehicle */
-		if (IsRailTT() && GetTileOwner(m_new_tile) != m_veh_owner) {
+		/* rail transport is possible only on tiles where the vehicle is allowed */
+		if (IsRailTT() && !(ignore_owner || IsRailwayTileUsageAllowed(m_new_tile, m_veh_owner))) {
 			/* different owner */
-			m_err = EC_NO_WAY;
+			m_err = EC_OWNER;
 			return false;
 		}
 
 		/* rail transport is possible only on compatible rail types */
 		if (IsRailTT()) {
 			RailType rail_type = GetTileRailType(m_new_tile);
+			/* If INVALID_RAILTYPES is specified, all bits are set and this test is always passed */
 			if (!HasBit(m_railtypes, rail_type)) {
 				/* incompatible rail type */
 				m_err = EC_RAIL_TYPE;
Index: src/roadveh_cmd.cpp
===================================================================
--- src/roadveh_cmd.cpp	(revision 17973)
+++ src/roadveh_cmd.cpp	(working copy)
@@ -37,6 +37,7 @@
 #include "roadstop_base.h"
 #include "cargotype.h"
 #include "newgrf_cargo.h"
+#include "infrastructure_func.h"
 
 #include "table/strings.h"
 #include "table/sprites.h"
@@ -200,7 +201,7 @@
 	/* The ai_new queries the vehicle cost before building the route,
 	 * so we must check against cheaters no sooner than now. --pasky */
 	if (!IsRoadDepotTile(tile)) return CMD_ERROR;
-	if (!IsTileOwner(tile, _current_company)) return CMD_ERROR;
+	if (!AreDepotOperationsAllowed(tile, _current_company, VEH_ROAD)) return CMD_ERROR;
 
 	if (HasTileRoadType(tile, ROADTYPE_TRAM) != HasBit(e->info.misc_flags, EF_ROAD_TRAM)) return_cmd_error(STR_ERROR_DEPOT_WRONG_DEPOT_TYPE);
 
@@ -379,7 +380,7 @@
 	tile += TileOffsByDiagDir(_road_pf_directions[trackdir]);
 
 	if (IsRoadDepotTile(tile) &&
-			IsTileOwner(tile, rfdd->owner) &&
+			IsDepotUsageAllowed(tile, rfdd->owner, VEH_ROAD) &&
 			length < rfdd->best_length) {
 		rfdd->best_length = length;
 		rfdd->tile = tile;
@@ -476,7 +477,7 @@
 CommandCost CmdTurnRoadVeh(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
 {
 	RoadVehicle *v = RoadVehicle::GetIfValid(p1);
-	if (v == NULL || !CheckOwnership(v->owner)) return CMD_ERROR;
+	if (v == NULL || !IsVehicleControlAllowed(v)) return CMD_ERROR;
 
 	if ((v->vehstatus & VS_STOPPED) ||
 			(v->vehstatus & VS_CRASHED) ||
@@ -1040,14 +1041,14 @@
 	TrackdirBits trackdirs = TrackStatusToTrackdirBits(ts);
 
 	if (IsTileType(tile, MP_ROAD)) {
-		if (IsRoadDepot(tile) && (!IsTileOwner(tile, v->owner) || GetRoadDepotDirection(tile) == enterdir || (GetRoadTypes(tile) & v->compatible_roadtypes) == 0)) {
+		if (IsRoadDepot(tile) && (!IsDepotUsageAllowed(tile, v) || GetRoadDepotDirection(tile) == enterdir || (GetRoadTypes(tile) & v->compatible_roadtypes) == 0)) {
 			/* Road depot owned by another company or with the wrong orientation */
 			trackdirs = TRACKDIR_BIT_NONE;
 		}
 	} else if (IsTileType(tile, MP_STATION) && IsStandardRoadStopTile(tile)) {
 		/* Standard road stop (drive-through stops are treated as normal road) */
 
-		if (!IsTileOwner(tile, v->owner) || GetRoadStopDir(tile) == enterdir || v->HasArticulatedPart()) {
+		if (!IsStationUsageAllowed(tile, v) || GetRoadStopDir(tile) == enterdir || v->HasArticulatedPart()) {
 			/* different station owner or wrong orientation or the vehicle has articulated parts */
 			trackdirs = TRACKDIR_BIT_NONE;
 		} else {
@@ -1651,7 +1652,7 @@
 			_road_veh_data_1[v->state - RVSB_IN_ROAD_STOP + (_settings_game.vehicle.road_side << RVS_DRIVE_SIDE)] == v->frame) ||
 			(IsInsideMM(v->state, RVSB_IN_DT_ROAD_STOP, RVSB_IN_DT_ROAD_STOP_END) &&
 			v->current_order.ShouldStopAtStation(v, GetStationIndex(v->tile)) &&
-			v->owner == GetTileOwner(v->tile) &&
+			IsStationUsageAllowed(v->tile, v) &&
 			GetRoadStopType(v->tile) == (IsCargoInClass(v->cargo_type, CC_PASSENGERS) ? ROADSTOP_BUS : ROADSTOP_TRUCK) &&
 			v->frame == RVC_DRIVE_THROUGH_STOP_FRAME))) {
 
Index: src/economy_type.h
===================================================================
--- src/economy_type.h	(revision 17973)
+++ src/economy_type.h	(working copy)
@@ -129,6 +129,8 @@
 	EXPENSES_SHIP_INC,
 	EXPENSES_LOAN_INT,
 	EXPENSES_OTHER,
+	EXPENSES_SHARED_RUN,
+	EXPENSES_SHARED_INC,
 	EXPENSES_END,
 	INVALID_EXPENSES      = 0xFF,
 };
Index: src/pbs.cpp
===================================================================
--- src/pbs.cpp	(revision 17973)
+++ src/pbs.cpp	(working copy)
@@ -242,7 +242,7 @@
 	if (v->type != VEH_TRAIN || (v->vehstatus & VS_CRASHED)) return NULL;
 
 	Train *t = Train::From(v);
-	if (HasBit((TrackBits)t->track, TrackdirToTrack(info->res.trackdir))) {
+	if (HasBit((TrackBits)t->track, TrackdirToTrack(info->res.trackdir)) || t->track == TRACK_BIT_WORMHOLE) {
 		t = t->First();
 
 		/* ALWAYS return the lowest ID (anti-desync!) */
@@ -277,6 +277,40 @@
 }
 
 /**
+ * Find a train on a reservation in a single direction.
+ * @param tile The reserved tile.
+ * @param trackdir The reserved trackdir to start searching.
+ * @param owner The owner of the vehicle. If no owner is known yet, specify INVALID_OWNER.
+ * @param rts The railtypes the vehicle can use. If not known yet, specify INVALID_RAILTYPES.
+ * @return The vehicle this reservation (possibly) belongs to.
+ */
+static Train *FindTrainForReservationDir(TileIndex tile, Trackdir trackdir, Owner owner, RailTypes rts)
+{
+	FindTrainOnTrackInfo ftoti;
+	/* Ignore one-way signals, as we might be searching the wrong way */
+	ftoti.res = FollowReservation(owner, rts, tile, trackdir, true);
+
+	FindVehicleOnPos(ftoti.res.tile, &ftoti, FindTrainOnTrackEnum);
+	if (ftoti.best != NULL) return ftoti.best;
+
+	/* Special case for stations: check the whole platform for a vehicle. */
+	if (HasStationTileRail(ftoti.res.tile)) {
+		TileIndexDiff diff = TileOffsByDiagDir(TrackdirToExitdir(ReverseTrackdir(ftoti.res.trackdir)));
+		for (TileIndex st_tile = ftoti.res.tile + diff; IsCompatibleTrainStationTile(st_tile, ftoti.res.tile); st_tile += diff) {
+			FindVehicleOnPos(st_tile, &ftoti, FindTrainOnTrackEnum);
+			if (ftoti.best != NULL) return ftoti.best;
+		}
+	}
+
+	/* Special case for bridges/tunnels: check the other end as well. */
+	if (IsTileType(ftoti.res.tile, MP_TUNNELBRIDGE)) {
+		FindVehicleOnPos(GetOtherTunnelBridgeEnd(ftoti.res.tile), &ftoti, FindTrainOnTrackEnum);
+		if (ftoti.best != NULL) return ftoti.best;
+	}
+	return NULL;
+}
+
+/**
  * Find the train which has reserved a specific path.
  *
  * @param tile A tile on the path.
@@ -288,32 +322,23 @@
 	assert(HasReservedTracks(tile, TrackToTrackBits(track)));
 	Trackdir  trackdir = TrackToTrackdir(track);
 
-	RailTypes rts = GetRailTypeInfo(GetTileRailType(tile))->compatible_railtypes;
-
 	/* Follow the path from tile to both ends, one of the end tiles should
-	 * have a train on it. We need FollowReservation to ignore one-way signals
-	 * here, as one of the two search directions will be the "wrong" way. */
+	 * have a train on it. Since we don't know the vehicle yet,
+	 * we don't know which tiles it can access with regards to ownership and infrastructure sharing,
+	 * nor do we know which rail types it is compatible with.
+	 * Therefore we first do a test run without ownership or railtype checks.
+	 * If this yields a vehicle, we test again using the now-known owner and railtype info.
+	 * If this still yields a vehicle, we return it as it is the correct one.
+	 */
 	for (int i = 0; i < 2; ++i, trackdir = ReverseTrackdir(trackdir)) {
-		FindTrainOnTrackInfo ftoti;
-		ftoti.res = FollowReservation(GetTileOwner(tile), rts, tile, trackdir, true);
-
-		FindVehicleOnPos(ftoti.res.tile, &ftoti, FindTrainOnTrackEnum);
-		if (ftoti.best != NULL) return ftoti.best;
-
-		/* Special case for stations: check the whole platform for a vehicle. */
-		if (IsRailStationTile(ftoti.res.tile)) {
-			TileIndexDiff diff = TileOffsByDiagDir(TrackdirToExitdir(ReverseTrackdir(ftoti.res.trackdir)));
-			for (TileIndex st_tile = ftoti.res.tile + diff; IsCompatibleTrainStationTile(st_tile, ftoti.res.tile); st_tile += diff) {
-				FindVehicleOnPos(st_tile, &ftoti, FindTrainOnTrackEnum);
-				if (ftoti.best != NULL) return ftoti.best;
-			}
+		Train *t = FindTrainForReservationDir(tile, trackdir, INVALID_OWNER, INVALID_RAILTYPES);
+		if (t == NULL) continue;
+		RailTypes rts = GetRailTypeInfo(t->railtype)->compatible_railtypes;
+		t = FindTrainForReservationDir(tile, trackdir, t->owner, rts);
+		/* Also verify that the train can actally use the start tile */
+		if (t != NULL && IsRailwayTileUsageAllowed(tile, t->owner) && HasBit(rts, GetTileRailType(tile))) {
+			return t;
 		}
-
-		/* Special case for bridges/tunnels: check the other end as well. */
-		if (IsTileType(ftoti.res.tile, MP_TUNNELBRIDGE)) {
-			FindVehicleOnPos(GetOtherTunnelBridgeEnd(ftoti.res.tile), &ftoti, FindTrainOnTrackEnum);
-			if (ftoti.best != NULL) return ftoti.best;
-		}
 	}
 
 	return NULL;
Index: src/signal.cpp
===================================================================
--- src/signal.cpp	(revision 17973)
+++ src/signal.cpp	(working copy)
@@ -16,6 +16,7 @@
 #include "vehicle_func.h"
 #include "functions.h"
 #include "train.h"
+#include "infrastructure_func.h"
 
 
 /** these are the maximums used for updating signal blocks */
@@ -278,7 +279,7 @@
 
 		switch (GetTileType(tile)) {
 			case MP_RAILWAY: {
-				if (GetTileOwner(tile) != owner) continue; // do not propagate signals on others' tiles (remove for tracksharing)
+				if (!AreSignalBlocksJoined(owner, GetTileOwner(tile))) continue; // check whether tile should be part of the same block, owner-wise
 
 				if (IsRailDepot(tile)) {
 					if (enterdir == INVALID_DIAGDIR) { // from 'inside' - train just entered or left the depot
@@ -351,7 +352,7 @@
 
 			case MP_STATION:
 				if (!HasStationRail(tile)) continue;
-				if (GetTileOwner(tile) != owner) continue;
+				if (!AreSignalBlocksJoined(owner, GetTileOwner(tile))) continue;
 				if (DiagDirToAxis(enterdir) != GetRailStationAxis(tile)) continue; // different axis
 				if (IsStationTileBlocked(tile)) continue; // 'eye-candy' station tile
 
@@ -361,7 +362,7 @@
 
 			case MP_ROAD:
 				if (!IsLevelCrossing(tile)) continue;
-				if (GetTileOwner(tile) != owner) continue;
+				if (!AreSignalBlocksJoined(owner, GetTileOwner(tile))) continue;
 				if (DiagDirToAxis(enterdir) == GetCrossingRoadAxis(tile)) continue; // different axis
 
 				if (!(flags & SF_TRAIN) && HasVehicleOnPos(tile, NULL, &TrainOnTileEnum)) flags |= SF_TRAIN;
@@ -369,7 +370,7 @@
 				break;
 
 			case MP_TUNNELBRIDGE: {
-				if (GetTileOwner(tile) != owner) continue;
+				if (!AreSignalBlocksJoined(owner, GetTileOwner(tile))) continue;
 				if (GetTunnelBridgeTransportType(tile) != TRANSPORT_RAIL) continue;
 				DiagDirection dir = GetTunnelBridgeDirection(tile);
 
Index: src/vehicle.cpp
===================================================================
--- src/vehicle.cpp	(revision 17973)
+++ src/vehicle.cpp	(working copy)
@@ -45,6 +45,7 @@
 #include "network/network.h"
 #include "core/pool_func.hpp"
 #include "economy_base.h"
+#include "infrastructure_func.h"
 
 #include "table/sprites.h"
 #include "table/strings.h"
@@ -1507,6 +1508,9 @@
 		case OT_LOADING: {
 			uint wait_time = max(this->current_order.wait_time - this->lateness_counter, 0);
 
+			/* Pay the loading fee when using someone else's station */
+			if (!(mode || this->type == VEH_TRAIN)) PayStationSharingFee(this, Station::Get(this->last_station_visited));
+
 			/* Not the first call for this tick, or still loading */
 			if (mode || !HasBit(this->vehicle_flags, VF_LOADING_FINISHED) ||
 					(_settings_game.order.timetabling && this->current_order_time < wait_time)) return;
Index: src/infrastructure.h
===================================================================
--- src/infrastructure.h	(revision 0)
+++ src/infrastructure.h	(revision 0)
@@ -0,0 +1,136 @@
+/* $Id$ */
+
+/*
+ * This file is part of OpenTTD.
+ * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
+ * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+ * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+/** @file infrastructure.h Define struct and enums for (shared) infrastructure */
+
+#ifndef INFRASTRUCTURE_H
+#define INFRASTRUCTURE_H
+
+#include "stdafx.h"
+#include "core/bitmath_func.hpp"
+#include "vehicle_type.h"
+#include "company_type.h"
+
+/** Sharing modes; possible values for the sharing_mode setting. */
+enum SharingMode {
+	SHARING_NONE,       ///< No infrastructure sharing at all
+	SHARING_GLOBAL,     ///< Sharing enabled with global (advanced) settings
+	SHARING_INDIVIDUAL, ///< Sharing enabled with individually configurable settings
+};
+
+/** flags for enabling / disabling various types of infrastructure */
+enum SharingOption {
+	SHARING_STATION = 1 << 0, ///< Stations are shared
+	SHARING_DEPOT   = 1 << 1, ///< Depots are shared
+	SHARING_TRACK   = 1 << 2, ///< Tracks are shared. (Trains only)
+};
+
+/** which part of the sharing settings do we want to modify? */
+enum ModifySharingFlag {
+	MODIFY_SHARING_OPTION,    ///< Modify access to station / depot / tracks
+	MODIFY_SHARING_FEE,       ///< Modify the sharing fee
+	MODIFY_SHARING_COMPANIES, ///< Modify which companies are allowed
+};
+
+/** maximum fee (in pounds) for any of the sharing fees */
+static const uint MAX_SHARING_FEE = 50000;
+
+/** number of relevant vehicle types */
+static const byte NUM_SHARING_VEHTYPES = 4;
+
+/** Bitmask of all relevant vehicle types. */
+static const byte ALL_SHARING_VEHTYPES = (1 << VEH_TRAIN) | (1 << VEH_ROAD) | (1 << VEH_SHIP) | (1 << VEH_AIRCRAFT);
+
+/** Struct to store all the sharing settings of a company (either current or future ones) */
+struct CompanySharingSettingsEntry {
+	byte option[NUM_SHARING_VEHTYPES]; ///< sharing options for rail/road/water/air
+	uint16 fee[NUM_SHARING_VEHTYPES];  ///< fees for rail/road/water/air
+	CompanyMask allowed_companies;     ///< which companies do we allow to use our infrastructure?
+
+	/** Constructor */
+	CompanySharingSettingsEntry() {}
+
+	/**
+	 * Compare two sets of sharing settings with eachother.
+	 * @param o The sharing settings entry to compare with.
+	 * @return True if all settings are equal, false otherwise.
+	 */
+	bool Equals(CompanySharingSettingsEntry *o);
+
+	/**
+	 * Copy the data of a specified sharing settings entry to this one.
+	 * @param o The sharing settings entry to use as source.
+	 * @return a bitmask of vehicle types whose permissions were reduced.
+	 */
+	byte Copy(CompanySharingSettingsEntry *o);
+
+	/**
+	 * Fill this struct with the values from _settings_game.
+	 * This also enables all companies in the allowed_companies mask.
+	 * @return a bitmask of vehicle types whose permissions were reduced.
+	 */
+	byte CopyFromGameSettings();
+
+	/**
+	 * Get the sharing option for a vehicle type.
+	 * @param type The vehicle type.
+	 * @return The sharing option for the specified vehicle type
+	 */
+	FORCEINLINE byte GetSharingOption(VehicleType type) const
+	{
+		assert(type < NUM_SHARING_VEHTYPES);
+		return this->option[type];
+	}
+
+	/**
+	 * Get the sharing fee for a specified vehicle type.
+	 * @param type The vehicle type.
+	 * @return The sharing fee for the given vehicle type.
+	 */
+	FORCEINLINE uint16 GetSharingFee(VehicleType type) const
+	{
+		assert(type < NUM_SHARING_VEHTYPES);
+		return this->fee[type];
+	}
+
+	/**
+	 * Set the sharing fee for a vehicle type.
+	 * @param type The vehicle type.
+	 * @param value The sharing option to set for the given vehicle type.
+	 */
+	FORCEINLINE void SetSharingOption(VehicleType type, byte value)
+	{
+		assert(type < NUM_SHARING_VEHTYPES);
+		this->option[type] = value;
+	}
+
+	/**
+	 * Set the sharing fee for a specified vehicle type.
+	 * @param type The vehicle type.
+	 * @param value The new sharing fee.
+	 */
+	FORCEINLINE void SetSharingFee(VehicleType type, uint16 value)
+	{
+		assert(type < NUM_SHARING_VEHTYPES);
+		this->fee[type] = value;
+	}
+
+	/**
+	 * Check whether a company is allowed to use the infrastructure.
+	 * i.e. it's corresponding bit in allowed_companies is set.
+	 * @param c The company to check.
+	 * @return true if the given company is allowed to use the infrastructure
+	 */
+	FORCEINLINE bool IsCompanyAllowed(CompanyID c) const
+	{
+		return HasBit(this->allowed_companies, c);
+	}
+};
+
+#endif /* INFRASTRUCTURE_H */
Index: src/infrastructure_func.h
===================================================================
--- src/infrastructure_func.h	(revision 0)
+++ src/infrastructure_func.h	(revision 0)
@@ -0,0 +1,209 @@
+/* $Id$ */
+
+/*
+ * This file is part of OpenTTD.
+ * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
+ * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+ * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+/** @file infrastructure_func.h Functions for infrastructure sharing */
+
+#ifndef INFRASTRUCTURE_FUNC_H
+#define INFRASTRUCTURE_FUNC_H
+
+#include "infrastructure.h"
+#include "functions.h"
+#include "station_base.h"
+#include "waypoint_base.h"
+#include "waypoint_func.h"
+#include "vehicle_base.h"
+#include "company_func.h"
+
+void PayStationSharingFee(Vehicle *v, const Station *st);
+void PayDailyTrackSharingFee(Train *v);
+void HandleSharingChange(byte changed_vehtypes = ALL_SHARING_VEHTYPES, bool update_signals = false);
+void AddSharingNewsItem(StringID string, Company *c, uint32 param = 0);
+
+/**
+ * Get the sharing setting for a given infrastructure owner, vehicle owner and vehicle type.
+ * This also checks whether the vehicle owner is allowed according to the allowed_companies bitmask.
+ * @see SharingOption
+ * @param owner The owner of the infrastructure.
+ * @param veh_owner Owner of the vehicle in question.
+ * @param type Type of vehicle we are talking about.
+ * @return The sharing option(s) that apply here.
+ */
+static inline byte GetSharingSetting(Owner owner, Owner veh_owner, VehicleType type)
+{
+	Company *c = Company::GetIfValid(owner);
+	assert(c != NULL);
+	return c->sharing_current.IsCompanyAllowed(veh_owner) ? c->sharing_current.GetSharingOption(type) : 0;
+}
+
+/**
+ * Get the sharing fee the infrastructure owner asks for using the infrastructure for a vehicle type.
+ * @param owner The owner of the infrastructure.
+ * @param type The vehicle type.
+ * @return The sharing fee that applies here.
+ */
+static inline uint16 GetSharingFee(Owner owner, VehicleType type)
+{
+	Company *c = Company::GetIfValid(owner);
+	assert(c != NULL);
+	return c->sharing_current.GetSharingFee(type);
+}
+
+/**
+ * Check whether the given owner is allowed to use rails on this tile.
+ * It should either own the tile, or the tile owner should have sharing of tracks enabled (for this company).
+ * @param t The (railway) tile.
+ * @param owner The owner of the vehicle that wants to use this tile.
+ * @return true if the given owner is allowed to use this railway tile.
+ */
+static inline bool IsRailUsageAllowed(TileIndex t, Owner owner)
+{
+	return IsTileOwner(t, owner) || GetSharingSetting(GetTileOwner(t), owner, VEH_TRAIN) & SHARING_TRACK;
+}
+
+/**
+ * Check whether the given owner is allowed to use this waypoint for his trains.
+ * It should either own the waypoint, or the waypoint owner should have sharing of tracks enabled (for this company).
+ * @param wp The waypoint to test.
+ * @param owner The owner of the vehicle that wants to use this waypoint.
+ * @return true if the given owner is allowed to use this waypoint.
+ */
+static inline bool IsWaypointUsageAllowed(const Waypoint *wp, Owner owner)
+{
+	return wp->owner == owner || wp->owner == OWNER_NONE || GetSharingSetting(wp->owner, owner, VEH_TRAIN) & SHARING_TRACK;
+}
+
+/**
+ * Check whether the given owner is allowed to use this depot.
+ * It should either own the depot, or the depot owner should have sharing of depots enabled (for this company).
+ * For non-trains, depot sharing is enabled automagically when sharing of stations is enabled.
+ * This function has one alias, which takes a vehicle as parameter instead of owner and type.
+ * @param t The depot tile.
+ * @param owner The owner of the vehicle that wants to use this depot.
+ * @param type The vehicle type we are talking about.
+ * @return true if the given owner is allowed to use this depot.
+ */
+static inline bool IsDepotUsageAllowed(TileIndex t, Owner owner, VehicleType type)
+{
+	return IsTileOwner(t, owner) || GetSharingSetting(GetTileOwner(t), owner, type) & SHARING_DEPOT;
+}
+
+/** @see IsDepotUsageAllowed(TileIndex, Owner, VehicleType) */
+static inline bool IsDepotUsageAllowed(TileIndex t, const Vehicle *v)
+{
+	return IsDepotUsageAllowed(t, v->owner, v->type);
+}
+
+/**
+ * Check whether a vehicle of a given owner and type is allowed to use a station.
+ * The vehicle owner should either own the station, the station should have a non-company owner
+ * or the station owner should have sharing of stations enabled (for this company).
+ * Note that trains can always use the station tracks, they just can't load/unload when not allowed.
+ * This function has three aliases with different sets of parameters, but all function the same.
+ * @param st The station.
+ * @param owner The vehicle owner.
+ * @param type The vehicle type.
+ * @return true If a vehicle of the given type and owner can use this station.
+ */
+static inline bool IsStationUsageAllowed(const Station *st, Owner owner, VehicleType type)
+{
+	return st->owner == owner || st->owner == OWNER_NONE || (GetSharingSetting(st->owner, owner, type) & SHARING_STATION) != 0;
+}
+
+/** @see IsStationUsageAllowed(const Station *, Owner, VehicleType) */
+static inline bool IsStationUsageAllowed(TileIndex t, Owner owner, VehicleType type)
+{
+	assert(IsTileType(t, MP_STATION));
+	return IsStationUsageAllowed(Station::GetByTile(t), owner, type);
+}
+
+/** @see IsStationUsageAllowed(const Station *, Owner, VehicleType) */
+static inline bool IsStationUsageAllowed(const Station *st, const Vehicle *v)
+{
+	return IsStationUsageAllowed(st, v->owner, v->type);
+}
+
+/** @see IsStationUsageAllowed(const Station *, Owner, VehicleType) */
+static inline bool IsStationUsageAllowed(TileIndex t, const Vehicle *v)
+{
+	assert(IsTileType(t, MP_STATION));
+	return IsStationUsageAllowed(Station::GetByTile(t), v->owner, v->type);
+}
+
+/**
+ * Check whether a given company can control this vehicle.
+ * The vehicle owner can always control a vehicle.
+ * Trains on foreign tracks can also be controlled by the track owner.
+ * Controlling a train means permission to start, stop or reverse it or to make it ignore signals.
+ * This function has one alias, which does not take a company as parameter. Instead it uses _current_company.
+ * This is very useful in DoCommands, since it uses Check[Tile]Ownership. (It gives nice error messages when failing)
+ * @param v The vehicle which may or may not be controlled.
+ * @param o The company which may or may not control this vehicle
+ * @return true if the given company is allowed to control this vehicle.
+ */
+static inline bool IsVehicleControlAllowed(const Vehicle *v, Owner o)
+{
+	return v->owner == o || (v->type == VEH_TRAIN && IsTileOwner(v->tile, o));
+}
+
+/** @see IsVehicleControlAllowed(const Vehicle *, Owner) */
+static inline bool IsVehicleControlAllowed(const Vehicle *v)
+{
+	return CheckOwnership(v->owner) || (v->type == VEH_TRAIN && CheckTileOwnership(v->tile));
+}
+
+/**
+ * Check whether usage of a railway tile is allowed.
+ * The tile to check can be any tile with rail on it,
+ * such as normal rail, depots, stations or bridges.
+ * This is actually assumed to be the case, but not checked.
+ * @param tile The tile to check.
+ * @param owner Owner of the train that wants to use that tile.
+ * @return true if the vehicle can use the tile.
+ */
+static inline bool IsRailwayTileUsageAllowed(TileIndex tile, Owner owner)
+{
+	return IsRailDepotTile(tile) ? IsDepotUsageAllowed(tile, owner, VEH_TRAIN) : IsRailUsageAllowed(tile, owner);
+}
+
+/**
+ * Are the tracks of two owners part of the same signal block?
+ * This is the case if the owners are equal, or if infrastructure sharing is enabled
+ * @param o1 First company.
+ * @param o2 Second company.
+ * @return true if the tracks of the given owners are part of the same signal block
+ */
+static inline bool AreSignalBlocksJoined(Owner o1, Owner o2)
+{
+	return (o1 == o2) || (_settings_game.sharing.sharing_mode != SHARING_NONE);
+}
+
+/**
+ * Is a company allowed to do depot operations? (buying/selling/reordering/autoreplace)
+ * This is allowed if the company is allowed to use the depot.
+ * For the sake of flexibility, a separate function (this one) is used.
+ * @param tile The tile of the depot.
+ * @param owner The company what wants to do depot operations.
+ * @param type The type of vehicle
+ * @return True it the depot operations are allowed
+ */
+static inline bool AreDepotOperationsAllowed(TileIndex tile, Owner owner, VehicleType type)
+{
+	return IsDepotUsageAllowed(tile, owner, type);
+}
+
+/**
+ * Should the smallmap window show whether rail tracks are accessible?
+ * @return true if the local company is valid and sharing is enabled
+ */
+static inline bool ShowSharingOnMap()
+{
+	return (_settings_game.sharing.sharing_mode != SHARING_NONE) && Company::IsValidID(_local_company);
+}
+
+#endif /* INFRASTRUCTURE_FUNC_H */
Index: src/aircraft_cmd.cpp
===================================================================
--- src/aircraft_cmd.cpp	(revision 17973)
+++ src/aircraft_cmd.cpp	(working copy)
@@ -36,6 +36,7 @@
 #include "station_base.h"
 #include "cargotype.h"
 #include "newgrf_cargo.h"
+#include "infrastructure_func.h"
 
 #include "table/strings.h"
 #include "table/sprites.h"
@@ -114,7 +115,7 @@
 	const AircraftVehicleInfo *avi = AircraftVehInfo(v->engine_type);
 
 	FOR_ALL_STATIONS(st) {
-		if (st->owner != v->owner || !(st->facilities & FACIL_AIRPORT)) continue;
+		if (!(st->facilities & FACIL_AIRPORT) || !IsStationUsageAllowed(st, v)) continue;
 
 		const AirportFTAClass *afc = st->Airport();
 		if (afc->nof_depots == 0 || (
@@ -145,7 +146,7 @@
 
 	FOR_VEHICLE_ORDERS(v, order) {
 		const Station *st = Station::Get(order->station);
-		if (st->owner == v->owner && (st->facilities & FACIL_AIRPORT)) {
+		if (IsStationUsageAllowed(st, v) && (st->facilities & FACIL_AIRPORT)) {
 			/* If an airport doesn't have a hangar, skip it */
 			if (st->Airport()->nof_depots != 0)
 				return true;
@@ -245,7 +246,7 @@
 	/* to just query the cost, it is not neccessary to have a valid tile (automation/AI) */
 	if (flags & DC_QUERY_COST) return value;
 
-	if (!IsHangarTile(tile) || !IsTileOwner(tile, _current_company)) return CMD_ERROR;
+	if (!IsHangarTile(tile) || !AreDepotOperationsAllowed(tile, _current_company, VEH_AIRCRAFT)) return CMD_ERROR;
 
 	/* Prevent building aircraft types at places which can't handle them */
 	if (!CanVehicleUseStation(p1, Station::GetByTile(tile))) return CMD_ERROR;
@@ -1563,7 +1564,7 @@
 	/* runway busy or not allowed to use this airstation, circle */
 	if ((apc->flags & (v->subtype == AIR_HELICOPTER ? AirportFTAClass::HELICOPTERS : AirportFTAClass::AIRPLANES)) &&
 			st->airport_tile != INVALID_TILE &&
-			(st->owner == OWNER_NONE || st->owner == v->owner)) {
+			IsStationUsageAllowed(st, v)) {
 		/* {32,FLYING,NOTHING_block,37}, {32,LANDING,N,33}, {32,HELILANDING,N,41},
 		 * if it is an airplane, look for LANDING, for helicopter HELILANDING
 		 * it is possible to choose from multiple landing runways, so loop until a free one is found */
Index: projects/openttd_vs80.vcproj
===================================================================
--- projects/openttd_vs80.vcproj	(revision 17973)
+++ projects/openttd_vs80.vcproj	(working copy)
@@ -588,6 +588,10 @@
 				>
 			</File>
 			<File
+				RelativePath=".\..\src\infrastructure.cpp"
+				>
+			</File>
+			<File
 				RelativePath=".\..\src\ini.cpp"
 				>
 			</File>
@@ -1088,6 +1092,14 @@
 				>
 			</File>
 			<File
+				RelativePath=".\..\src\infrastructure.h"
+				>
+			</File>
+			<File
+				RelativePath=".\..\src\infrastructure_func.h"
+				>
+			</File>
+			<File
 				RelativePath=".\..\src\ini_type.h"
 				>
 			</File>
Index: projects/openttd_vs90.vcproj
===================================================================
--- projects/openttd_vs90.vcproj	(revision 17973)
+++ projects/openttd_vs90.vcproj	(working copy)
@@ -585,6 +585,10 @@
 				>
 			</File>
 			<File
+				RelativePath=".\..\src\infrastructure.cpp"
+				>
+			</File>
+			<File
 				RelativePath=".\..\src\ini.cpp"
 				>
 			</File>
@@ -1085,6 +1089,14 @@
 				>
 			</File>
 			<File
+				RelativePath=".\..\src\infrastructure.h"
+				>
+			</File>
+			<File
+				RelativePath=".\..\src\infrastructure_func.h"
+				>
+			</File>
+			<File
 				RelativePath=".\..\src\ini_type.h"
 				>
 			</File>
Index: source.list
===================================================================
--- source.list	(revision 17973)
+++ source.list	(working copy)
@@ -32,6 +32,7 @@
 gfxinit.cpp
 heightmap.cpp
 highscore.cpp
+infrastructure.cpp
 ini.cpp
 landscape.cpp
 map.cpp
@@ -181,6 +182,8 @@
 industry.h
 industry_type.h
 industrytype.h
+infrastructure.h
+infrastructure_func.h
 ini_type.h
 landscape.h
 landscape_type.h
