tunnelbridge_cmd.c
changeset 2916 b687477adcba
parent 2870 0dc0bcee256a
child 2931 d832ee22696d
equal deleted inserted replaced
2915:e08f661f1b8e 2916:b687477adcba
    16 #include "viewport.h"
    16 #include "viewport.h"
    17 #include "command.h"
    17 #include "command.h"
    18 #include "player.h"
    18 #include "player.h"
    19 #include "town.h"
    19 #include "town.h"
    20 #include "sound.h"
    20 #include "sound.h"
    21 #include "pbs.h"
       
    22 #include "debug.h"
       
    23 #include "variables.h"
    21 #include "variables.h"
    24 #include "bridge.h"
    22 #include "bridge.h"
    25 #include "train.h"
    23 #include "train.h"
    26 
    24 
    27 #include "table/bridge_land.h"
    25 #include "table/bridge_land.h"
   202 	uint odd_middle_part;
   200 	uint odd_middle_part;
   203 	uint direction;
   201 	uint direction;
   204 	uint i;
   202 	uint i;
   205 	int32 cost, terraformcost, ret;
   203 	int32 cost, terraformcost, ret;
   206 	bool allow_on_slopes;
   204 	bool allow_on_slopes;
   207 	bool reserved = false;
       
   208 
   205 
   209 	SET_EXPENSES_TYPE(EXPENSES_CONSTRUCTION);
   206 	SET_EXPENSES_TYPE(EXPENSES_CONSTRUCTION);
   210 
   207 
   211 	/* unpack parameters */
   208 	/* unpack parameters */
   212 	bridge_type = GB(p2, 0, 8);
   209 	bridge_type = GB(p2, 0, 8);
   350 				if (ti.map5 != 2) goto not_valid_below;
   347 				if (ti.map5 != 2) goto not_valid_below;
   351 			} else {
   348 			} else {
   352 				if (ti.map5 != 1) goto not_valid_below;
   349 				if (ti.map5 != 1) goto not_valid_below;
   353 			}
   350 			}
   354 			m5 = 0xE0;
   351 			m5 = 0xE0;
   355 			reserved = PBSTileReserved(ti.tile) != 0;
       
   356 		} else if (ti.type == MP_STREET) {
   352 		} else if (ti.type == MP_STREET) {
   357 			if (direction == 0) {
   353 			if (direction == 0) {
   358 				if (ti.map5 != 5) goto not_valid_below;
   354 				if (ti.map5 != 5) goto not_valid_below;
   359 			} else {
   355 			} else {
   360 				if (ti.map5 != 10) goto not_valid_below;
   356 				if (ti.map5 != 10) goto not_valid_below;
   400 			}
   396 			}
   401 
   397 
   402 			_m[ti.tile].m2 = (bridge_type << 4) | m5;
   398 			_m[ti.tile].m2 = (bridge_type << 4) | m5;
   403 			SB(_m[ti.tile].m3, 4, 4, railtype);
   399 			SB(_m[ti.tile].m3, 4, 4, railtype);
   404 
   400 
   405 			if (ti.type == MP_RAILWAY) {
       
   406 				// Set or clear PBS reservation status. direction here is of
       
   407 				// the bridge, not the track below.
       
   408 				if (reserved) {
       
   409 					PBSReserveTrack(ti.tile, direction ? TRACK_DIAG1 : TRACK_DIAG2);
       
   410 				} else {
       
   411 					PBSClearTrack(ti.tile, direction ? TRACK_DIAG1 : TRACK_DIAG2);
       
   412 				}
       
   413 			}
       
   414 
       
   415 			MarkTileDirtyByTile(ti.tile);
   401 			MarkTileDirtyByTile(ti.tile);
   416 		}
   402 		}
   417 	}
   403 	}
   418 
   404 
   419 	SetSignalsOnBothDir(ti_start.tile, (direction & 1) ? 1 : 0);
   405 	SetSignalsOnBothDir(ti_start.tile, (direction & 1) ? 1 : 0);
   805 
   791 
   806 	if (flags & DC_EXEC) {
   792 	if (flags & DC_EXEC) {
   807 		byte m5;
   793 		byte m5;
   808 		uint c = tile;
   794 		uint c = tile;
   809 		uint16 new_data;
   795 		uint16 new_data;
   810 		byte pbs;
       
   811 
   796 
   812 		//checks if the owner is town then decrease town rating by RATING_TUNNEL_BRIDGE_DOWN_STEP until
   797 		//checks if the owner is town then decrease town rating by RATING_TUNNEL_BRIDGE_DOWN_STEP until
   813 		// you have a "Poor" (0) town rating
   798 		// you have a "Poor" (0) town rating
   814 		if (IsTileOwner(tile, OWNER_TOWN) && _game_mode != GM_EDITOR)
   799 		if (IsTileOwner(tile, OWNER_TOWN) && _game_mode != GM_EDITOR)
   815 			ChangeTownRating(t, RATING_TUNNEL_BRIDGE_DOWN_STEP, RATING_TUNNEL_BRIDGE_MINIMUM);
   800 			ChangeTownRating(t, RATING_TUNNEL_BRIDGE_DOWN_STEP, RATING_TUNNEL_BRIDGE_MINIMUM);
   816 
   801 
   817 		do {
   802 		do {
   818 			m5 = _m[c].m5;
   803 			m5 = _m[c].m5;
   819 			pbs = PBSTileReserved(c);
       
   820 
   804 
   821 			if (m5 & 0x40) {
   805 			if (m5 & 0x40) {
   822 				if (m5 & 0x20) {
   806 				if (m5 & 0x20) {
   823 					static const uint16 _new_data_table[] = {0x1002, 0x1001, 0x2005, 0x200A, 0, 0, 0, 0};
   807 					static const uint16 _new_data_table[] = {0x1002, 0x1001, 0x2005, 0x200A, 0, 0, 0, 0};
   824 					new_data = _new_data_table[((m5 & 0x18) >> 2) | (m5 & 1)];
   808 					new_data = _new_data_table[((m5 & 0x18) >> 2) | (m5 & 1)];
   829 
   813 
   830 				SetTileType(c, new_data >> 12);
   814 				SetTileType(c, new_data >> 12);
   831 				_m[c].m5 = (byte)new_data;
   815 				_m[c].m5 = (byte)new_data;
   832 				_m[c].m2 = 0;
   816 				_m[c].m2 = 0;
   833 				_m[c].m4 &= 0x0F;
   817 				_m[c].m4 &= 0x0F;
   834 				if (direction ? HASBIT(pbs,0) : HASBIT(pbs,1))
       
   835 					PBSReserveTrack(c, direction ? 0 : 1);
       
   836 
   818 
   837 				MarkTileDirtyByTile(c);
   819 				MarkTileDirtyByTile(c);
   838 
   820 
   839 			} else {
   821 			} else {
   840 clear_it:;
   822 clear_it:;
  1210 				// draw pillars below for high bridges
  1192 				// draw pillars below for high bridges
  1211 				DrawBridgePillars(ti, x, y, z);
  1193 				DrawBridgePillars(ti, x, y, z);
  1212 			}
  1194 			}
  1213 		}
  1195 		}
  1214 	}
  1196 	}
  1215 
       
  1216 	if (_debug_pbs_level >= 1) {
       
  1217 		byte pbs = PBSTileReserved(ti->tile);
       
  1218 		if (pbs & TRACK_BIT_DIAG1) DrawGroundSprite(0x3ED | PALETTE_CRASH);
       
  1219 		if (pbs & TRACK_BIT_DIAG2) DrawGroundSprite(0x3EE | PALETTE_CRASH);
       
  1220 		if (pbs & TRACK_BIT_UPPER) DrawGroundSprite(0x3EF | PALETTE_CRASH);
       
  1221 		if (pbs & TRACK_BIT_LOWER) DrawGroundSprite(0x3F0 | PALETTE_CRASH);
       
  1222 		if (pbs & TRACK_BIT_LEFT)  DrawGroundSprite(0x3F2 | PALETTE_CRASH);
       
  1223 		if (pbs & TRACK_BIT_RIGHT) DrawGroundSprite(0x3F1 | PALETTE_CRASH);
       
  1224 	}
       
  1225 }
  1197 }
  1226 
  1198 
  1227 static uint GetSlopeZ_TunnelBridge(const TileInfo* ti)
  1199 static uint GetSlopeZ_TunnelBridge(const TileInfo* ti)
  1228 {
  1200 {
  1229 	uint z = ti->z;
  1201 	uint z = ti->z;
  1502 					if (v->spritenum < 4)
  1474 					if (v->spritenum < 4)
  1503 						SndPlayVehicleFx(SND_05_TRAIN_THROUGH_TUNNEL, v);
  1475 						SndPlayVehicleFx(SND_05_TRAIN_THROUGH_TUNNEL, v);
  1504 					return 0;
  1476 					return 0;
  1505 				}
  1477 				}
  1506 				if (fc == _tunnel_fractcoord_2[dir]) {
  1478 				if (fc == _tunnel_fractcoord_2[dir]) {
  1507 					if (v->next == NULL)
       
  1508 						PBSClearTrack(v->tile, FIND_FIRST_BIT(v->u.rail.track));
       
  1509 					v->tile = tile;
  1479 					v->tile = tile;
  1510 					v->u.rail.track = 0x40;
  1480 					v->u.rail.track = 0x40;
  1511 					v->vehstatus |= VS_HIDDEN;
  1481 					v->vehstatus |= VS_HIDDEN;
  1512 					return 4;
  1482 					return 4;
  1513 				}
  1483 				}