src/road_cmd.cpp
branchcustombridgeheads
changeset 5650 aefc131bf5ce
parent 5649 55c8267c933f
child 5888 3117320a611b
equal deleted inserted replaced
5649:55c8267c933f 5650:aefc131bf5ce
    54 	if (owner != OWNER_TOWN) return (owner == OWNER_NONE) || CheckOwnership(owner);
    54 	if (owner != OWNER_TOWN) return (owner == OWNER_NONE) || CheckOwnership(owner);
    55 
    55 
    56 	if (_cheats.magic_bulldozer.value) return true;
    56 	if (_cheats.magic_bulldozer.value) return true;
    57 
    57 
    58 	// Get a bitmask of which neighbouring roads has a tile
    58 	// Get a bitmask of which neighbouring roads has a tile
    59 	n = 0;
    59 	n = ROAD_NONE;
    60 	present = GetAnyRoadBits(tile);
    60 	present = GetAnyRoadBits(tile);
    61 	if (present & ROAD_NE && GetAnyRoadBits(TILE_ADDXY(tile,-1, 0)) & ROAD_SW) n |= ROAD_NE;
    61 	if (present & ROAD_NE && GetAnyRoadBits(TILE_ADDXY(tile,-1, 0)) & ROAD_SW) n |= ROAD_NE;
    62 	if (present & ROAD_SE && GetAnyRoadBits(TILE_ADDXY(tile, 0, 1)) & ROAD_NW) n |= ROAD_SE;
    62 	if (present & ROAD_SE && GetAnyRoadBits(TILE_ADDXY(tile, 0, 1)) & ROAD_NW) n |= ROAD_SE;
    63 	if (present & ROAD_SW && GetAnyRoadBits(TILE_ADDXY(tile, 1, 0)) & ROAD_NE) n |= ROAD_SW;
    63 	if (present & ROAD_SW && GetAnyRoadBits(TILE_ADDXY(tile, 1, 0)) & ROAD_NE) n |= ROAD_SW;
    64 	if (present & ROAD_NW && GetAnyRoadBits(TILE_ADDXY(tile, 0,-1)) & ROAD_SE) n |= ROAD_NW;
    64 	if (present & ROAD_NW && GetAnyRoadBits(TILE_ADDXY(tile, 0,-1)) & ROAD_SE) n |= ROAD_NW;
   101 
   101 
   102 	SET_EXPENSES_TYPE(EXPENSES_CONSTRUCTION);
   102 	SET_EXPENSES_TYPE(EXPENSES_CONSTRUCTION);
   103 
   103 
   104 	/* Road pieces are max 4 bitset values (NE, NW, SE, SW) */
   104 	/* Road pieces are max 4 bitset values (NE, NW, SE, SW) */
   105 	if (p1 >> 4) return CMD_ERROR;
   105 	if (p1 >> 4) return CMD_ERROR;
   106 	pieces = p1;
   106 	pieces = (RoadBits)p1;
   107 
   107 
   108 	if (!IsTileType(tile, MP_STREET)) return CMD_ERROR;
   108 	if (!IsTileType(tile, MP_STREET)) return CMD_ERROR;
   109 
   109 
   110 	owner = IsLevelCrossingTile(tile) ? GetCrossingRoadOwner(tile) : GetTileOwner(tile);
   110 	owner = IsLevelCrossingTile(tile) ? GetCrossingRoadOwner(tile) : GetTileOwner(tile);
   111 
   111 
   130 
   130 
   131 			if (HasRoadWorks(tile)) return_cmd_error(STR_ROAD_WORKS_IN_PROGRESS);
   131 			if (HasRoadWorks(tile)) return_cmd_error(STR_ROAD_WORKS_IN_PROGRESS);
   132 
   132 
   133 			if (GetTileSlope(tile, NULL) != SLOPE_FLAT  &&
   133 			if (GetTileSlope(tile, NULL) != SLOPE_FLAT  &&
   134 					(present == ROAD_Y || present == ROAD_X)) {
   134 					(present == ROAD_Y || present == ROAD_X)) {
   135 				c |= (c & 0xC) >> 2;
   135 				c |= (RoadBits)((c & 0xC) >> 2);
   136 				c |= (c & 0x3) << 2;
   136 				c |= (RoadBits)((c & 0x3) << 2);
   137 			}
   137 			}
   138 
   138 
   139 			// limit the bits to delete to the existing bits.
   139 			// limit the bits to delete to the existing bits.
   140 			c &= present;
   140 			c &= present;
   141 			if (c == 0) return CMD_ERROR;
   141 			if (c == 0) return CMD_ERROR;
   162 			if (flags & DC_EXEC) {
   162 			if (flags & DC_EXEC) {
   163 				ChangeTownRating(t, -road_remove_cost[(byte)edge_road], RATING_ROAD_MINIMUM);
   163 				ChangeTownRating(t, -road_remove_cost[(byte)edge_road], RATING_ROAD_MINIMUM);
   164 
   164 
   165 				MakeRailNormal(tile, GetTileOwner(tile), GetCrossingRailBits(tile), GetRailTypeCrossing(tile));
   165 				MakeRailNormal(tile, GetTileOwner(tile), GetCrossingRailBits(tile), GetRailTypeCrossing(tile));
   166 				MarkTileDirtyByTile(tile);
   166 				MarkTileDirtyByTile(tile);
   167 				YapfNotifyTrackLayoutChange(tile, FIND_FIRST_BIT(GetTrackBits(tile)));
   167 				YapfNotifyTrackLayoutChange(tile, FindFirstTrack(GetTrackBits(tile)));
   168 			}
   168 			}
   169 			return _price.remove_road * 2;
   169 			return _price.remove_road * 2;
   170 		}
   170 		}
   171 
   171 
   172 		default:
   172 		default:
   177 
   177 
   178 
   178 
   179 static const RoadBits _valid_tileh_slopes_road[][15] = {
   179 static const RoadBits _valid_tileh_slopes_road[][15] = {
   180 	// set of normal ones
   180 	// set of normal ones
   181 	{
   181 	{
   182 		ROAD_ALL, 0, 0,
   182 		ROAD_ALL, ROAD_NONE, ROAD_NONE,
   183 		ROAD_X,   0, 0,  // 3, 4, 5
   183 		ROAD_X,   ROAD_NONE, ROAD_NONE,  // 3, 4, 5
   184 		ROAD_Y,   0, 0,
   184 		ROAD_Y,   ROAD_NONE, ROAD_NONE,
   185 		ROAD_Y,   0, 0,  // 9, 10, 11
   185 		ROAD_Y,   ROAD_NONE, ROAD_NONE,  // 9, 10, 11
   186 		ROAD_X,   0, 0
   186 		ROAD_X,   ROAD_NONE, ROAD_NONE
   187 	},
   187 	},
   188 	// allowed road for an evenly raised platform
   188 	// allowed road for an evenly raised platform
   189 	{
   189 	{
   190 		0,
   190 		ROAD_NONE,
   191 		ROAD_SW | ROAD_NW,
   191 		ROAD_SW | ROAD_NW,
   192 		ROAD_SW | ROAD_SE,
   192 		ROAD_SW | ROAD_SE,
   193 		ROAD_Y  | ROAD_SW,
   193 		ROAD_Y  | ROAD_SW,
   194 
   194 
   195 		ROAD_SE | ROAD_NE, // 4
   195 		ROAD_SE | ROAD_NE, // 4
   214 	RoadBits road_bits;
   214 	RoadBits road_bits;
   215 
   215 
   216 	if (IsSteepSlope(tileh)) {
   216 	if (IsSteepSlope(tileh)) {
   217 		if (existing == 0) {
   217 		if (existing == 0) {
   218 			// force full pieces.
   218 			// force full pieces.
   219 			*pieces |= (*pieces & 0xC) >> 2;
   219 			*pieces |= (RoadBits)((*pieces & 0xC) >> 2);
   220 			*pieces |= (*pieces & 0x3) << 2;
   220 			*pieces |= (RoadBits)((*pieces & 0x3) << 2);
   221 			if (*pieces == ROAD_X || *pieces == ROAD_Y) return _price.terraform;
   221 			if (*pieces == ROAD_X || *pieces == ROAD_Y) return _price.terraform;
   222 		}
   222 		}
   223 		return CMD_ERROR;
   223 		return CMD_ERROR;
   224 	}
   224 	}
   225 	road_bits = *pieces | existing;
   225 	road_bits = *pieces | existing;
   237 	}
   237 	}
   238 
   238 
   239 	// partly leveled up tile, only if there's no road on that tile
   239 	// partly leveled up tile, only if there's no road on that tile
   240 	if (existing == 0 && (tileh == SLOPE_W || tileh == SLOPE_S || tileh == SLOPE_E || tileh == SLOPE_N)) {
   240 	if (existing == 0 && (tileh == SLOPE_W || tileh == SLOPE_S || tileh == SLOPE_E || tileh == SLOPE_N)) {
   241 		// force full pieces.
   241 		// force full pieces.
   242 		*pieces |= (*pieces & 0xC) >> 2;
   242 		*pieces |= (RoadBits)((*pieces & 0xC) >> 2);
   243 		*pieces |= (*pieces & 0x3) << 2;
   243 		*pieces |= (RoadBits)((*pieces & 0x3) << 2);
   244 		if (*pieces == ROAD_X || *pieces == ROAD_Y) return _price.terraform;
   244 		if (*pieces == ROAD_X || *pieces == ROAD_Y) return _price.terraform;
   245 	}
   245 	}
   246 	return CMD_ERROR;
   246 	return CMD_ERROR;
   247 }
   247 }
   248 
   248 
   253  */
   253  */
   254 int32 CmdBuildRoad(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
   254 int32 CmdBuildRoad(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
   255 {
   255 {
   256 	int32 cost = 0;
   256 	int32 cost = 0;
   257 	int32 ret;
   257 	int32 ret;
   258 	RoadBits existing = 0;
   258 	RoadBits existing = ROAD_NONE;
   259 	RoadBits pieces;
   259 	RoadBits pieces;
   260 	Slope tileh;
   260 	Slope tileh;
   261 
   261 
   262 	SET_EXPENSES_TYPE(EXPENSES_CONSTRUCTION);
   262 	SET_EXPENSES_TYPE(EXPENSES_CONSTRUCTION);
   263 
   263 
   264 	/* Road pieces are max 4 bitset values (NE, NW, SE, SW) and town can only be non-zero
   264 	/* Road pieces are max 4 bitset values (NE, NW, SE, SW) and town can only be non-zero
   265 	 * if a non-player is building the road */
   265 	 * if a non-player is building the road */
   266 	if ((p1 >> 4) || (IsValidPlayer(_current_player) && p2 != 0) || !IsValidTownID(p2)) return CMD_ERROR;
   266 	if ((p1 >> 4) || (IsValidPlayer(_current_player) && p2 != 0) || !IsValidTownID(p2)) return CMD_ERROR;
   267 	pieces = p1;
   267 	pieces = (RoadBits)p1;
   268 
   268 
   269 	tileh = GetTileSlope(tile, NULL);
   269 	tileh = GetTileSlope(tile, NULL);
   270 
   270 
   271 	switch (GetTileType(tile)) {
   271 	switch (GetTileType(tile)) {
   272 		case MP_STREET:
   272 		case MP_STREET:
   323 			}
   323 			}
   324 
   324 
   325 			if (!EnsureNoVehicle(tile)) return CMD_ERROR;
   325 			if (!EnsureNoVehicle(tile)) return CMD_ERROR;
   326 
   326 
   327 			if (flags & DC_EXEC) {
   327 			if (flags & DC_EXEC) {
   328 				YapfNotifyTrackLayoutChange(tile, FIND_FIRST_BIT(GetTrackBits(tile)));
   328 				YapfNotifyTrackLayoutChange(tile, FindFirstTrack(GetTrackBits(tile)));
   329 				MakeRoadCrossing(tile, _current_player, GetTileOwner(tile), roaddir, GetRailType(tile), p2);
   329 				MakeRoadCrossing(tile, _current_player, GetTileOwner(tile), roaddir, GetRailType(tile), p2);
   330 				MarkTileDirtyByTile(tile);
   330 				MarkTileDirtyByTile(tile);
   331 			}
   331 			}
   332 			return _price.build_road * 2;
   332 			return _price.build_road * 2;
   333 		}
   333 		}
   380 	if (_patches.disable_elrails && totype == RAILTYPE_RAIL && GetRailTypeCrossing(tile) == RAILTYPE_ELECTRIC) return CMD_ERROR;
   380 	if (_patches.disable_elrails && totype == RAILTYPE_RAIL && GetRailTypeCrossing(tile) == RAILTYPE_ELECTRIC) return CMD_ERROR;
   381 
   381 
   382 	if (exec) {
   382 	if (exec) {
   383 		SetRailTypeCrossing(tile, totype);
   383 		SetRailTypeCrossing(tile, totype);
   384 		MarkTileDirtyByTile(tile);
   384 		MarkTileDirtyByTile(tile);
   385 		YapfNotifyTrackLayoutChange(tile, FIND_FIRST_BIT(GetCrossingRailBits(tile)));
   385 		YapfNotifyTrackLayoutChange(tile, FindFirstTrack(GetCrossingRailBits(tile)));
   386 	}
   386 	}
   387 
   387 
   388 	return _price.build_rail >> 1;
   388 	return _price.build_rail >> 1;
   389 }
   389 }
   390 
   390 
   538 
   538 
   539 	if (flags & DC_EXEC) {
   539 	if (flags & DC_EXEC) {
   540 		dep->xy = tile;
   540 		dep->xy = tile;
   541 		dep->town_index = ClosestTownFromTile(tile, (uint)-1)->index;
   541 		dep->town_index = ClosestTownFromTile(tile, (uint)-1)->index;
   542 
   542 
   543 		MakeRoadDepot(tile, _current_player, p1);
   543 		MakeRoadDepot(tile, _current_player, (DiagDirection)p1);
   544 		MarkTileDirtyByTile(tile);
   544 		MarkTileDirtyByTile(tile);
   545 	}
   545 	}
   546 	return cost + _price.build_road_depot;
   546 	return cost + _price.build_road_depot;
   547 }
   547 }
   548 
   548 
  1046 		}
  1046 		}
  1047 	}
  1047 	}
  1048 }
  1048 }
  1049 
  1049 
  1050 
  1050 
  1051 const TileTypeProcs _tile_type_road_procs = {
  1051 extern const TileTypeProcs _tile_type_road_procs = {
  1052 	DrawTile_Road,           /* draw_tile_proc */
  1052 	DrawTile_Road,           /* draw_tile_proc */
  1053 	GetSlopeZ_Road,          /* get_slope_z_proc */
  1053 	GetSlopeZ_Road,          /* get_slope_z_proc */
  1054 	ClearTile_Road,          /* clear_tile_proc */
  1054 	ClearTile_Road,          /* clear_tile_proc */
  1055 	GetAcceptedCargo_Road,   /* get_accepted_cargo_proc */
  1055 	GetAcceptedCargo_Road,   /* get_accepted_cargo_proc */
  1056 	GetTileDesc_Road,        /* get_tile_desc_proc */
  1056 	GetTileDesc_Road,        /* get_tile_desc_proc */