src/rail_gui.cpp
branchNewGRF_ports
changeset 6720 35756db7e577
parent 6719 4cc327ad39d5
child 6725 23339968083f
equal deleted inserted replaced
6719:4cc327ad39d5 6720:35756db7e577
    24 #include "variables.h"
    24 #include "variables.h"
    25 #include "newgrf_callbacks.h"
    25 #include "newgrf_callbacks.h"
    26 #include "newgrf_station.h"
    26 #include "newgrf_station.h"
    27 #include "train.h"
    27 #include "train.h"
    28 
    28 
       
    29 #include "bridge_map.h"
       
    30 #include "rail_map.h"
       
    31 #include "road_map.h"
       
    32 #include "station_map.h"
       
    33 #include "tunnel_map.h"
       
    34 
    29 static RailType _cur_railtype;
    35 static RailType _cur_railtype;
    30 static bool _remove_button_clicked;
    36 static bool _remove_button_clicked;
    31 static DiagDirection _build_depot_direction;
    37 static DiagDirection _build_depot_direction;
    32 static byte _waypoint_count = 1;
    38 static byte _waypoint_count = 1;
    33 static byte _cur_waypoint_type;
    39 static byte _cur_waypoint_type;
   163 	}
   169 	}
   164 }
   170 }
   165 
   171 
   166 static void GenericPlaceSignals(TileIndex tile)
   172 static void GenericPlaceSignals(TileIndex tile)
   167 {
   173 {
   168 	byte trackstat;
   174 	TrackBits trackbits = (TrackBits)GB(GetTileTrackStatus(tile, TRANSPORT_RAIL, 0), 0, 6);
   169 	uint i;
   175 
   170 
   176 	if (trackbits & TRACK_BIT_VERT) { // N-S direction
   171 	trackstat = (byte)GetTileTrackStatus(tile, TRANSPORT_RAIL, 0);
   177 		trackbits = (_tile_fract_coords.x <= _tile_fract_coords.y) ? TRACK_BIT_RIGHT : TRACK_BIT_LEFT;
   172 
   178 	}
   173 	if (trackstat & TRACK_BIT_VERT) // N-S direction
   179 
   174 		trackstat = (_tile_fract_coords.x <= _tile_fract_coords.y) ? TRACK_BIT_RIGHT : TRACK_BIT_LEFT;
   180 	if (trackbits & TRACK_BIT_HORZ) { // E-W direction
   175 
   181 		trackbits = (_tile_fract_coords.x + _tile_fract_coords.y <= 15) ? TRACK_BIT_UPPER : TRACK_BIT_LOWER;
   176 	if (trackstat & TRACK_BIT_HORZ) // E-W direction
   182 	}
   177 		trackstat = (_tile_fract_coords.x + _tile_fract_coords.y <= 15) ? TRACK_BIT_UPPER : TRACK_BIT_LOWER;
   183 
   178 
   184 	Track track = FindFirstTrack(trackbits);
   179 	// Lookup the bit index
       
   180 	i = 0;
       
   181 	if (trackstat != 0) {
       
   182 		for (; !(trackstat & 1); trackstat >>= 1) i++;
       
   183 	}
       
   184 
   185 
   185 	if (!_remove_button_clicked) {
   186 	if (!_remove_button_clicked) {
   186 		uint32 p1 = GB(i, 0, 3);
   187 		uint32 p1 = track;
   187 		SB(p1, 3, 1, _ctrl_pressed);
   188 		SB(p1, 3, 1, _ctrl_pressed);
   188 		SB(p1, 4, 1, _cur_year < _patches.semaphore_build_before);
   189 		SB(p1, 4, 1, _cur_year < _patches.semaphore_build_before);
   189 
   190 
   190 		DoCommandP(tile, p1, 0, CcPlaySound1E,
   191 		DoCommandP(tile, p1, 0, CcPlaySound1E,
   191 			CMD_BUILD_SIGNALS | CMD_AUTO | CMD_MSG(STR_1010_CAN_T_BUILD_SIGNALS_HERE));
   192 			CMD_BUILD_SIGNALS | CMD_AUTO | CMD_MSG(STR_1010_CAN_T_BUILD_SIGNALS_HERE));
   192 	} else {
   193 	} else {
   193 		DoCommandP(tile, i, 0, CcPlaySound1E,
   194 		DoCommandP(tile, track, 0, CcPlaySound1E,
   194 			CMD_REMOVE_SIGNALS | CMD_AUTO | CMD_MSG(STR_1013_CAN_T_REMOVE_SIGNALS_FROM));
   195 			CMD_REMOVE_SIGNALS | CMD_AUTO | CMD_MSG(STR_1013_CAN_T_REMOVE_SIGNALS_FROM));
   195 	}
   196 	}
   196 }
   197 }
   197 
   198 
   198 static void PlaceRail_Bridge(TileIndex tile)
   199 static void PlaceRail_Bridge(TileIndex tile)
   370 	if (thd->drawstyle == HT_RECT) { // one tile case
   371 	if (thd->drawstyle == HT_RECT) { // one tile case
   371 		GenericPlaceSignals(TileVirtXY(thd->selend.x, thd->selend.y));
   372 		GenericPlaceSignals(TileVirtXY(thd->selend.x, thd->selend.y));
   372 		return;
   373 		return;
   373 	}
   374 	}
   374 
   375 
   375 	SB(p2,  3, 1, _ctrl_pressed);
   376 	/* XXX Steal ctrl for autosignal function, until we get some GUI */
       
   377 	SB(p2,  3, 1, 0);
   376 	SB(p2,  4, 1, _cur_year < _patches.semaphore_build_before);
   378 	SB(p2,  4, 1, _cur_year < _patches.semaphore_build_before);
       
   379 	SB(p2,  6, 1, _ctrl_pressed);
   377 	SB(p2, 24, 8, _patches.drag_signals_density);
   380 	SB(p2, 24, 8, _patches.drag_signals_density);
   378 
   381 
   379 	/* _patches.drag_signals_density is given as a parameter such that each user
   382 	/* _patches.drag_signals_density is given as a parameter such that each user
   380 	 * in a network game can specify his/her own signal density */
   383 	 * in a network game can specify his/her own signal density */
   381 	DoCommandP(
   384 	DoCommandP(
   775 		DrawStringCentered(74, 15 + y_offset, STR_3002_ORIENTATION, 0);
   778 		DrawStringCentered(74, 15 + y_offset, STR_3002_ORIENTATION, 0);
   776 		DrawStringCentered(74, 76 + y_offset, STR_3003_NUMBER_OF_TRACKS, 0);
   779 		DrawStringCentered(74, 76 + y_offset, STR_3003_NUMBER_OF_TRACKS, 0);
   777 		DrawStringCentered(74, 101 + y_offset, STR_3004_PLATFORM_LENGTH, 0);
   780 		DrawStringCentered(74, 101 + y_offset, STR_3004_PLATFORM_LENGTH, 0);
   778 		DrawStringCentered(74, 141 + y_offset, STR_3066_COVERAGE_AREA_HIGHLIGHT, 0);
   781 		DrawStringCentered(74, 141 + y_offset, STR_3066_COVERAGE_AREA_HIGHLIGHT, 0);
   779 
   782 
   780 		DrawStationCoverageAreaText(2, 166 + y_offset, (uint)-1, rad);
   783 		DrawStationCoverageAreaText(2, 166 + y_offset, SCT_ALL, rad);
   781 
   784 
   782 		if (newstations) {
   785 		if (newstations) {
   783 			uint16 i;
   786 			uint16 i;
   784 			uint y = 35;
   787 			uint y = 35;
   785 
   788 
  1228 		}
  1231 		}
  1229 	}
  1232 	}
  1230 	MarkWholeScreenDirty();
  1233 	MarkWholeScreenDirty();
  1231 }
  1234 }
  1232 
  1235 
  1233 
  1236 void SetDefaultRailGui()
       
  1237 {
       
  1238 	if (_local_player == PLAYER_SPECTATOR || !IsValidPlayer(_local_player)) return;
       
  1239 
       
  1240 	extern RailType _last_built_railtype;
       
  1241 	RailType rt = (RailType)_patches.default_rail_type;
       
  1242 	if (rt >= RAILTYPE_END) {
       
  1243 		if (rt == RAILTYPE_END + 2) {
       
  1244 			/* Find the most used rail type */
       
  1245 			RailType count[RAILTYPE_END];
       
  1246 			memset(count, 0, sizeof(count));
       
  1247 			for (TileIndex t = 0; t < MapSize(); t++) {
       
  1248 				if (IsTileType(t, MP_RAILWAY) ||
       
  1249 						IsLevelCrossingTile(t) ||
       
  1250 						IsRailwayStationTile(t) ||
       
  1251 						(IsTunnelTile(t) && GetTunnelTransportType(t) == TRANSPORT_RAIL) ||
       
  1252 						(IsBridgeTile(t) && GetBridgeTransportType(t) == TRANSPORT_RAIL)
       
  1253 						) {
       
  1254 					count[GetRailType(t)]++;
       
  1255 				}
       
  1256 			}
       
  1257 
       
  1258 			rt = RAILTYPE_RAIL;
       
  1259 			for (RailType r = RAILTYPE_ELECTRIC; r < RAILTYPE_END; r++) {
       
  1260 				if (count[r] >= count[rt]) rt = r;
       
  1261 			}
       
  1262 
       
  1263 			/* No rail, just get the first available one */
       
  1264 			if (count[rt] == 0) rt = RAILTYPE_END;
       
  1265 		}
       
  1266 		switch (rt) {
       
  1267 			case RAILTYPE_END + 0:
       
  1268 				rt = RAILTYPE_RAIL;
       
  1269 				while (rt < RAILTYPE_END && !HasRailtypeAvail(GetPlayer(_local_player), rt)) rt++;
       
  1270 				break;
       
  1271 
       
  1272 			case RAILTYPE_END + 1:
       
  1273 				rt = GetBestRailtype(GetPlayer(_local_player));
       
  1274 				break;
       
  1275 
       
  1276 			default:
       
  1277 				break;
       
  1278 		}
       
  1279 	}
       
  1280 
       
  1281 	_last_built_railtype = _cur_railtype = rt;
       
  1282 	Window *w = FindWindowById(WC_BUILD_TOOLBAR, 0);
       
  1283 	if (w != NULL && w->wndproc == BuildRailToolbWndProc) {
       
  1284 		SetupRailToolbar(_cur_railtype, w);
       
  1285 		SetWindowDirty(w);
       
  1286 	}
       
  1287 }
       
  1288