643 * |
655 * |
644 * @param tile curent tile |
656 * @param tile curent tile |
645 * @param dir target direction |
657 * @param dir target direction |
646 * @param dist_multi distance multiplyer |
658 * @param dist_multi distance multiplyer |
647 * @return true if one of the neighboring tiles at the |
659 * @return true if one of the neighboring tiles at the |
648 * given distance is a road tile else |
660 * given distance is a road tile else false |
649 */ |
661 */ |
650 static bool IsNeighborRoadTile(TileIndex tile, int dir, RoadBlockTitleDistance dist_multi) |
662 static bool IsNeighborRoadTile(TileIndex tile, DiagDirection dir, uint dist_multi) |
651 { |
663 { |
652 return (HASBIT(GetTownRoadMask(TILE_ADD(tile, dist_multi * ToTileIndexDiff(_roadblock_tileadd[dir + 1]))), dir ^ 2) || |
664 static TileIndexDiff tid_lt[3]; ///< lookup table for the used diff values |
653 HASBIT(GetTownRoadMask(TILE_ADD(tile, dist_multi * ToTileIndexDiff(_roadblock_tileadd[dir + 3]))), dir ^ 2) || |
665 tid_lt[0] = TileOffsByDiagDir(ChangeDiagDir(dir, DIAGDIRDIFF_90RIGHT)); |
654 HASBIT(GetTownRoadMask(TILE_ADD(tile, dist_multi * (ToTileIndexDiff(_roadblock_tileadd[dir + 1]) + ToTileIndexDiff(_roadblock_tileadd[dir + 2])))), dir) || |
666 tid_lt[1] = TileOffsByDiagDir(ChangeDiagDir(dir, DIAGDIRDIFF_90LEFT)); |
655 HASBIT(GetTownRoadMask(TILE_ADD(tile, dist_multi * (ToTileIndexDiff(_roadblock_tileadd[dir + 3]) + ToTileIndexDiff(_roadblock_tileadd[dir + 2])))), dir)); |
667 tid_lt[2] = TileOffsByDiagDir(ReverseDiagDir(dir)); |
656 } |
668 |
657 |
669 /* We add 1 to the distance because we want to get 1 for |
658 static bool IsRoadAllowedHere(TileIndex tile, int dir) |
670 * the min distance multiplyer and not 0. |
|
671 * Therefore we start at 4. The 4 is used because |
|
672 * there are 4 tiles per distance step to check. |
|
673 */ |
|
674 dist_multi = (dist_multi + 1) * 4; |
|
675 for (uint pos = 4; pos < dist_multi; pos++) { |
|
676 TileIndexDiff cur = 0; |
|
677 /* For each even value of pos add the right TileIndexDiff |
|
678 * for each uneven value the left TileIndexDiff |
|
679 * for each with 2nd bit set (2,3,6,7,..) add the reversed TileIndexDiff |
|
680 */ |
|
681 cur += tid_lt[(pos & 1) ? 0 : 1]; |
|
682 if (pos & 2) cur += tid_lt[2]; |
|
683 |
|
684 cur = (uint)(pos / 4) * cur; ///< Multiply for the fitting distance |
|
685 if (GetTownRoadBits(TILE_ADD(tile, cur)) & DiagDirToRoadBits((pos & 2) ? dir : ReverseDiagDir(dir))) return true; |
|
686 } |
|
687 return false; |
|
688 } |
|
689 |
|
690 /** |
|
691 * Check if a Road is allowed on a given tile |
|
692 * |
|
693 * @param tile The target tile |
|
694 * @param dir The direction in which we want to extend the town |
|
695 * @return true if it is allowed else false |
|
696 */ |
|
697 static bool IsRoadAllowedHere(TileIndex tile, DiagDirection dir) |
659 { |
698 { |
660 if (TileX(tile) < 2 || TileY(tile) < 2 || MapMaxX() <= TileX(tile) || MapMaxY() <= TileY(tile)) return false; |
699 if (TileX(tile) < 2 || TileY(tile) < 2 || MapMaxX() <= TileX(tile) || MapMaxY() <= TileY(tile)) return false; |
661 |
700 |
662 Slope k; |
701 Slope cur_slope, desired_slope; |
663 Slope slope; |
|
664 |
702 |
665 /* If this assertion fails, it might be because the world contains |
703 /* If this assertion fails, it might be because the world contains |
666 * land at the edges. This is not ok. */ |
704 * land at the edges. This is not ok. */ |
667 TILE_ASSERT(tile); |
705 TILE_ASSERT(tile); |
668 |
706 |
669 for (;;) { |
707 for (;;) { |
670 /* Check if there already is a road at this point? */ |
708 /* Check if there already is a road at this point? */ |
671 if (GetAnyRoadTrackBits(tile, ROADTYPE_ROAD) == 0) { |
709 if (GetTownRoadBits(tile) == ROAD_NONE) { |
672 /* No, try to build one in the direction. |
710 /* No, try to build one in the direction. |
673 * if that fails clear the land, and if that fails exit. |
711 * if that fails clear the land, and if that fails exit. |
674 * This is to make sure that we can build a road here later. */ |
712 * This is to make sure that we can build a road here later. */ |
675 if (CmdFailed(DoCommand(tile, (dir & ROAD_NW ? ROAD_X : ROAD_Y), 0, DC_AUTO, CMD_BUILD_ROAD)) && |
713 if (CmdFailed(DoCommand(tile, ((dir == DIAGDIR_NW || dir == DIAGDIR_SE) ? ROAD_X : ROAD_Y), 0, DC_AUTO, CMD_BUILD_ROAD)) && |
676 CmdFailed(DoCommand(tile, 0, 0, DC_AUTO, CMD_LANDSCAPE_CLEAR))) |
714 CmdFailed(DoCommand(tile, 0, 0, DC_AUTO, CMD_LANDSCAPE_CLEAR))) |
677 return false; |
715 return false; |
678 } |
716 } |
679 |
717 |
680 slope = GetTileSlope(tile, NULL); |
718 cur_slope = GetTileSlope(tile, NULL); |
681 if (slope == SLOPE_FLAT) { |
719 if (cur_slope == SLOPE_FLAT) { |
682 no_slope: |
720 no_slope: |
683 /* Tile has no slope */ |
721 /* Tile has no slope */ |
684 switch (_patches.town_layout) { |
722 switch (_patches.town_layout) { |
685 default: NOT_REACHED(); |
723 default: NOT_REACHED(); |
686 |
724 |
687 case TL_ORIGINAL: /* Disallow the road if any neighboring tile has a road (distance: 1) */ |
725 case TL_ORIGINAL: /* Disallow the road if any neighboring tile has a road (distance: 1) */ |
688 return !IsNeighborRoadTile(tile, dir, RB_TILE_DIST1); |
726 return !IsNeighborRoadTile(tile, dir, 1); |
689 |
727 |
690 case TL_BETTER_ROADS: /* Disallow the road if any neighboring tile has a road (distance: 1 and 2). */ |
728 case TL_BETTER_ROADS: /* Disallow the road if any neighboring tile has a road (distance: 1 and 2). */ |
691 return !(IsNeighborRoadTile(tile, dir, RB_TILE_DIST1) || |
729 return !IsNeighborRoadTile(tile, dir, 2); |
692 IsNeighborRoadTile(tile, dir, RB_TILE_DIST2)); |
|
693 } |
730 } |
694 } |
731 } |
695 |
732 |
696 /* If the tile is not a slope in the right direction, then |
733 /* If the tile is not a slope in the right direction, then |
697 * maybe terraform some. */ |
734 * maybe terraform some. */ |
698 k = (dir & ROAD_NW) ? SLOPE_NE : SLOPE_NW; |
735 desired_slope = (dir == DIAGDIR_NW || dir == DIAGDIR_SE) ? SLOPE_NE : SLOPE_NW; |
699 if (k != slope && ComplementSlope(k) != slope) { |
736 if (desired_slope != cur_slope && ComplementSlope(desired_slope) != cur_slope) { |
700 uint32 r = Random(); |
737 uint32 r = Random(); |
701 |
738 |
702 if (CHANCE16I(1, 8, r) && !_generating_world) { |
739 if (CHANCE16I(1, 8, r) && !_generating_world) { |
703 CommandCost res; |
740 CommandCost res; |
704 |
741 |
705 if (CHANCE16I(1, 16, r)) { |
742 if (CHANCE16I(1, 16, r)) { |
706 res = DoCommand(tile, slope, 0, DC_EXEC | DC_AUTO | DC_NO_WATER, |
743 res = DoCommand(tile, cur_slope, 0, DC_EXEC | DC_AUTO | DC_NO_WATER, CMD_TERRAFORM_LAND); |
707 CMD_TERRAFORM_LAND); |
|
708 } else { |
744 } else { |
709 /* Note: Do not replace " ^ 0xF" with ComplementSlope(). The slope might be steep. */ |
745 /* Note: Do not replace " ^ 0xF" with ComplementSlope(). The slope might be steep. */ |
710 res = DoCommand(tile, slope ^ 0xF, 1, DC_EXEC | DC_AUTO | DC_NO_WATER, |
746 res = DoCommand(tile, cur_slope ^ 0xF, 1, DC_EXEC | DC_AUTO | DC_NO_WATER, CMD_TERRAFORM_LAND); |
711 CMD_TERRAFORM_LAND); |
|
712 } |
747 } |
713 if (CmdFailed(res) && CHANCE16I(1, 3, r)) { |
748 if (CmdFailed(res) && CHANCE16I(1, 3, r)) { |
714 /* We can consider building on the slope, though. */ |
749 /* We can consider building on the slope, though. */ |
715 goto no_slope; |
750 goto no_slope; |
716 } |
751 } |
753 /** |
788 /** |
754 * Generate the RoadBits of a grid tile |
789 * Generate the RoadBits of a grid tile |
755 * |
790 * |
756 * @param t current town |
791 * @param t current town |
757 * @param tile tile in reference to the town |
792 * @param tile tile in reference to the town |
|
793 * @param dir The direction to which we are growing ATM |
758 * @return the RoadBit of the current tile regarding |
794 * @return the RoadBit of the current tile regarding |
759 * the selected town layout |
795 * the selected town layout |
760 */ |
796 */ |
761 static RoadBits GetTownRoadGridElement(Town* t, TileIndex tile) |
797 static RoadBits GetTownRoadGridElement(Town* t, TileIndex tile, DiagDirection dir) |
762 { |
798 { |
763 /* align the grid to the downtown */ |
799 /* align the grid to the downtown */ |
764 TileIndexDiffC grid_pos = TileIndexToTileIndexDiffC(t->xy, tile); ///< Vector from downtown to the tile |
800 TileIndexDiffC grid_pos = TileIndexToTileIndexDiffC(t->xy, tile); ///< Vector from downtown to the tile |
765 |
801 RoadBits rcmd = ROAD_NONE; |
766 /* lx, ly description: |
|
767 * @li lx and ly are true if the tile is a crossing tile. |
|
768 * @li lx xor ly are true if the tile is a straight road tile. |
|
769 * @li lx and ly are false if the tile is a house tile. |
|
770 */ |
|
771 bool lx, ly; |
|
772 |
802 |
773 switch (_patches.town_layout) { |
803 switch (_patches.town_layout) { |
774 default: NOT_REACHED(); |
804 default: NOT_REACHED(); |
775 |
805 |
776 case TL_2X2_GRID: |
806 case TL_2X2_GRID: |
777 lx = ((grid_pos.x % 3) == 0); |
807 if ((grid_pos.x % 3) == 0) rcmd |= ROAD_Y; |
778 ly = ((grid_pos.y % 3) == 0); |
808 if ((grid_pos.y % 3) == 0) rcmd |= ROAD_X; |
779 break; |
809 break; |
780 |
810 |
781 case TL_3X3_GRID: |
811 case TL_3X3_GRID: |
782 lx = ((grid_pos.x % 4) == 0); |
812 if ((grid_pos.x % 4) == 0) rcmd |= ROAD_Y; |
783 ly = ((grid_pos.y % 4) == 0); |
813 if ((grid_pos.y % 4) == 0) rcmd |= ROAD_X; |
784 break; |
814 break; |
785 } |
815 } |
786 |
816 |
787 /* generate the basic grid structure */ |
817 /* Stop if the tile is not a part of the grid lines */ |
788 if (!lx && !ly) { ///< It is a house tile |
818 if (rcmd == ROAD_NONE) return rcmd; |
789 return ROAD_NONE; |
819 |
790 } else if (lx && !ly) { ///< It is a Y-dir road tile |
820 /* Optimise only X-junctions */ |
791 return ROAD_Y; |
821 if (COUNTBITS(rcmd) != 2) { |
792 } else if (!lx && ly) { ///< It is a X-dir road tile |
822 RoadBits rb_template; |
793 return ROAD_X; |
823 |
794 } else { ///< It is a crossing tile |
|
795 /* Presets for junctions on slopes |
|
796 * not nice :( */ |
|
797 switch (GetTileSlope(tile, NULL)) { |
824 switch (GetTileSlope(tile, NULL)) { |
798 case SLOPE_W: |
825 default: rb_template = ROAD_ALL; break; |
799 return ROAD_NW | ROAD_SW; |
826 case SLOPE_W: rb_template = ROAD_NW | ROAD_SW; break; |
800 case SLOPE_S: |
827 case SLOPE_SW: rb_template = ROAD_Y | ROAD_SW; break; |
801 return ROAD_SE | ROAD_SW; |
828 case SLOPE_S: rb_template = ROAD_SW | ROAD_SE; break; |
802 case SLOPE_SW: |
829 case SLOPE_SE: rb_template = ROAD_X | ROAD_SE; break; |
803 return ROAD_Y | ROAD_SW; |
830 case SLOPE_E: rb_template = ROAD_SE | ROAD_NE; break; |
804 case SLOPE_E: |
831 case SLOPE_NE: rb_template = ROAD_Y | ROAD_NE; break; |
805 return ROAD_NE | ROAD_SE; |
832 case SLOPE_N: rb_template = ROAD_NE | ROAD_NW; break; |
806 case SLOPE_SE: |
833 case SLOPE_NW: rb_template = ROAD_X | ROAD_NW; break; |
807 return ROAD_X | ROAD_SE; |
|
808 case SLOPE_N: |
|
809 return ROAD_NW | ROAD_NE; |
|
810 case SLOPE_NW: |
|
811 return ROAD_X | ROAD_NW; |
|
812 case SLOPE_NE: |
|
813 return ROAD_Y | ROAD_NE; |
|
814 case SLOPE_STEEP_W: |
834 case SLOPE_STEEP_W: |
815 case SLOPE_STEEP_N: |
|
816 return ROAD_X; |
|
817 case SLOPE_STEEP_S: |
835 case SLOPE_STEEP_S: |
818 case SLOPE_STEEP_E: |
836 case SLOPE_STEEP_E: |
819 return ROAD_Y; |
837 case SLOPE_STEEP_N: |
820 default: |
838 rb_template = ROAD_NONE; |
821 return ROAD_ALL; |
839 break; |
822 } |
840 } |
823 } |
841 |
|
842 /* Stop if the template is compatible to the growth dir */ |
|
843 if (DiagDirToRoadBits(ReverseDiagDir(dir)) & rb_template) return rb_template; |
|
844 /* If not generate a straight road in the direction of the growth */ |
|
845 return DiagDirToRoadBits(dir) | DiagDirToRoadBits(ReverseDiagDir(dir)); |
|
846 } |
|
847 |
|
848 return rcmd; |
824 } |
849 } |
825 |
850 |
826 /** |
851 /** |
827 * Check there are enougth neighbor house tiles next to the current tile |
852 * Check there are enough neighbor house tiles next to the current tile |
828 * |
853 * |
829 * @param tile current tile |
854 * @param tile current tile |
830 * @return true if there are more than 2 house tiles next |
855 * @return true if there are more than 2 house tiles next |
831 * to the current one |
856 * to the current one |
832 */ |
857 */ |
899 case TL_NO_ROADS: /* Disallow Roads */ |
919 case TL_NO_ROADS: /* Disallow Roads */ |
900 return; |
920 return; |
901 |
921 |
902 case TL_3X3_GRID: |
922 case TL_3X3_GRID: |
903 case TL_2X2_GRID: |
923 case TL_2X2_GRID: |
904 rcmd = GetTownRoadGridElement(t1, tile); |
924 rcmd = GetTownRoadGridElement(t1, tile, target_dir); |
905 if (rcmd == ROAD_NONE) { |
925 if (rcmd == ROAD_NONE) return; |
906 return; |
|
907 } |
|
908 break; |
926 break; |
909 |
927 |
910 case TL_BETTER_ROADS: |
928 case TL_BETTER_ROADS: |
911 case TL_ORIGINAL: |
929 case TL_ORIGINAL: |
912 if (!IsRoadAllowedHere(tile, block)) { |
930 if (!IsRoadAllowedHere(tile, target_dir)) return; |
913 return; |
931 |
|
932 DiagDirection source_dir = ReverseDiagDir(target_dir); |
|
933 |
|
934 if (CHANCE16(1, 4)) { |
|
935 /* Randomize a new target dir */ |
|
936 do target_dir = RandomDiagDir(); while (target_dir == source_dir); |
914 } |
937 } |
915 |
938 |
916 /* Randomize new road block numbers */ |
939 if (!IsRoadAllowedHere(AddDiagDirToTileIndex(tile, target_dir), target_dir)) { |
917 a = block; |
|
918 b = block ^ 2; |
|
919 if (CHANCE16(1, 4)) { |
|
920 do { |
|
921 a = GB(Random(), 0, 2); |
|
922 } while (a == b); |
|
923 } |
|
924 |
|
925 if (!IsRoadAllowedHere(TILE_ADD(tile, ToTileIndexDiff(_roadblock_tileadd[a])), a)) { |
|
926 /* A road is not allowed to continue the randomized road, |
940 /* A road is not allowed to continue the randomized road, |
927 * return if the road we're trying to build is curved. */ |
941 * return if the road we're trying to build is curved. */ |
928 if (a != (b ^ 2)) { |
942 if (target_dir != ReverseDiagDir(source_dir)) return; |
929 return; |
|
930 } |
|
931 |
943 |
932 /* Return if neither side of the new road is a house */ |
944 /* Return if neither side of the new road is a house */ |
933 if (!IsTileType(TILE_ADD(tile, ToTileIndexDiff(_roadblock_tileadd[a + 1])), MP_HOUSE) && |
945 if (!IsTileType(AddDiagDirToTileIndex(tile, ChangeDiagDir(target_dir, DIAGDIRDIFF_90RIGHT)), MP_HOUSE) && |
934 !IsTileType(TILE_ADD(tile, ToTileIndexDiff(_roadblock_tileadd[a + 3])), MP_HOUSE)) { |
946 !IsTileType(AddDiagDirToTileIndex(tile, ChangeDiagDir(target_dir, DIAGDIRDIFF_90LEFT)), MP_HOUSE)) { |
935 return; |
947 return; |
936 } |
948 } |
937 |
949 |
938 /* That means that the road is only allowed if there is a house |
950 /* That means that the road is only allowed if there is a house |
939 * at any side of the new road. */ |
951 * at any side of the new road. */ |
940 } |
952 } |
941 |
953 |
942 rcmd = (RoadBits)((ROAD_NW << a) + (ROAD_NW << b)); |
954 rcmd = DiagDirToRoadBits(target_dir) | DiagDirToRoadBits(source_dir); |
943 break; |
955 break; |
944 } |
956 } |
945 |
957 |
946 } else if (block < 5 && !HASBIT(mask, block ^ 2)) { |
958 } else if (target_dir < (DiagDirection)5 && !(cur_rb & DiagDirToRoadBits(ReverseDiagDir(target_dir)))) { |
947 /* Continue building on a partial road. |
959 /* Continue building on a partial road. |
948 * Always OK. */ |
960 * Should be allways OK, so we only generate |
|
961 * the fitting RoadBits */ |
949 _grow_town_result = 0; |
962 _grow_town_result = 0; |
950 |
963 |
951 switch (_patches.town_layout) { |
964 switch (_patches.town_layout) { |
952 default: NOT_REACHED(); |
965 default: NOT_REACHED(); |
953 |
966 |
954 case TL_NO_ROADS: /* Disallow Roads */ |
967 case TL_NO_ROADS: /* Disallow Roads */ |
955 return; |
968 return; |
956 |
969 |
957 case TL_3X3_GRID: |
970 case TL_3X3_GRID: |
958 case TL_2X2_GRID: |
971 case TL_2X2_GRID: |
959 rcmd = GetTownRoadGridElement(t1, tile); |
972 rcmd = GetTownRoadGridElement(t1, tile, target_dir); |
960 break; |
973 break; |
961 |
974 |
962 case TL_BETTER_ROADS: |
975 case TL_BETTER_ROADS: |
963 case TL_ORIGINAL: |
976 case TL_ORIGINAL: |
964 rcmd = (RoadBits)(ROAD_NW << (block ^ 2)); |
977 rcmd = DiagDirToRoadBits(ReverseDiagDir(target_dir)); |
965 break; |
978 break; |
966 } |
979 } |
967 } else { |
980 } else { |
968 int i; |
981 bool allow_house = false; ///< Value which decides if we want to construct a house |
969 bool allow_house = false; |
982 TileIndex tmptile2; ///< Yet another dummy tile |
970 TileIndex tmptile2; |
|
971 |
983 |
972 /* Reached a tunnel/bridge? Then continue at the other side of it. */ |
984 /* Reached a tunnel/bridge? Then continue at the other side of it. */ |
973 if (IsTileType(tile, MP_TUNNELBRIDGE)) { |
985 if (IsTileType(tile, MP_TUNNELBRIDGE)) { |
974 if (IsTunnel(tile) && GetTunnelTransportType(tile) == TRANSPORT_ROAD) { |
986 if (IsTunnel(tile) && GetTunnelTransportType(tile) == TRANSPORT_ROAD) { |
975 *tile_ptr = GetOtherTunnelEnd(tile); |
987 *tile_ptr = GetOtherTunnelEnd(tile); |
997 allow_house = true; |
1009 allow_house = true; |
998 break; |
1010 break; |
999 |
1011 |
1000 case TL_3X3_GRID: /* Use 2x2 grid afterwards! */ |
1012 case TL_3X3_GRID: /* Use 2x2 grid afterwards! */ |
1001 /* Fill gap if house has enougth neighbors */ |
1013 /* Fill gap if house has enougth neighbors */ |
1002 tmptile2 = TILE_ADD(tmptile, ToTileIndexDiff(_roadblock_tileadd[i])); |
1014 tmptile2 = AddDiagDirToTileIndex(tmptile, target_dir); |
1003 if (AreNeighborsHouseTiles(tmptile2) && BuildTownHouse(t1, tmptile2)) { |
1015 if (AreNeighborsHouseTiles(tmptile2) && BuildTownHouse(t1, tmptile2)) { |
1004 _grow_town_result = -1; |
1016 _grow_town_result = -1; |
1005 } |
1017 } |
1006 |
1018 |
1007 case TL_2X2_GRID: |
1019 case TL_2X2_GRID: |
1008 rcmd = GetTownRoadGridElement(t1, tmptile); |
1020 rcmd = GetTownRoadGridElement(t1, tmptile, target_dir); |
1009 allow_house = (rcmd == ROAD_NONE); |
1021 allow_house = (rcmd == ROAD_NONE); |
1010 break; |
1022 break; |
1011 |
1023 |
1012 case TL_BETTER_ROADS: /* Use original afterwards! */ |
1024 case TL_BETTER_ROADS: /* Use original afterwards! */ |
1013 /* Fill gap if house has enougth neighbors */ |
1025 /* Fill gap if house has enougth neighbors */ |
1014 tmptile2 = TILE_ADD(tmptile, ToTileIndexDiff(_roadblock_tileadd[i])); |
1026 tmptile2 = AddDiagDirToTileIndex(tmptile, target_dir); |
1015 if (AreNeighborsHouseTiles(tmptile2) && BuildTownHouse(t1, tmptile2)) { |
1027 if (AreNeighborsHouseTiles(tmptile2) && BuildTownHouse(t1, tmptile2)) { |
1016 _grow_town_result = -1; |
1028 _grow_town_result = -1; |
1017 } |
1029 } |
1018 |
1030 |
1019 case TL_ORIGINAL: |
1031 case TL_ORIGINAL: |
1020 /* Allow a house at the edge. 60% chance or |
1032 /* Allow a house at the edge. 60% chance or |
1021 * always ok if no road allowed. */ |
1033 * always ok if no road allowed. */ |
1022 allow_house = (!IsRoadAllowedHere(tmptile, i) || CHANCE16(6, 10)); |
1034 rcmd = DiagDirToRoadBits(target_dir); |
|
1035 allow_house = (!IsRoadAllowedHere(tmptile, target_dir) || CHANCE16(6, 10)); |
1023 break; |
1036 break; |
1024 } |
1037 } |
1025 |
|
1026 |
1038 |
1027 if (allow_house) { |
1039 if (allow_house) { |
1028 /* Build a house, but not if there already is a house there. */ |
1040 /* Build a house, but not if there already is a house there. */ |
1029 if (!IsTileType(tmptile, MP_HOUSE)) { |
1041 if (!IsTileType(tmptile, MP_HOUSE)) { |
1030 /* Level the land if possible */ |
1042 /* Level the land if possible */ |
1038 } |
1050 } |
1039 return; |
1051 return; |
1040 } |
1052 } |
1041 |
1053 |
1042 _grow_town_result = 0; |
1054 _grow_town_result = 0; |
1043 rcmd = (RoadBits)(ROAD_NW << i); |
|
1044 } |
1055 } |
1045 |
1056 |
1046 /* Return if a water tile */ |
1057 /* Return if a water tile */ |
1047 if (IsClearWaterTile(tile)) return; |
1058 if (IsClearWaterTile(tile)) return; |
1048 |
1059 |
|
1060 DiagDirection bridge_dir; ///< The direction of a bridge we maybe want to build |
1049 /* Determine direction of slope, |
1061 /* Determine direction of slope, |
1050 * and build a road if not a special slope. */ |
1062 * and build a road if not a special slope. */ |
1051 switch (GetTileSlope(tile, NULL)) { |
1063 switch (GetTileSlope(tile, NULL)) { |
1052 case SLOPE_SW: i = DIAGDIR_NE; break; |
1064 case SLOPE_SW: bridge_dir = DIAGDIR_NE; break; |
1053 case SLOPE_SE: i = DIAGDIR_NW; break; |
1065 case SLOPE_SE: bridge_dir = DIAGDIR_NW; break; |
1054 case SLOPE_NW: i = DIAGDIR_SE; break; |
1066 case SLOPE_NW: bridge_dir = DIAGDIR_SE; break; |
1055 case SLOPE_NE: i = DIAGDIR_SW; break; |
1067 case SLOPE_NE: bridge_dir = DIAGDIR_SW; break; |
1056 |
1068 |
1057 default: |
1069 default: |
1058 build_road_and_exit: |
1070 build_road_and_exit: |
1059 if (CmdSucceeded(DoCommand(tile, rcmd, t1->index, DC_EXEC | DC_AUTO | DC_NO_WATER, CMD_BUILD_ROAD))) { |
1071 if (CmdSucceeded(DoCommand(tile, rcmd, t1->index, DC_EXEC | DC_AUTO | DC_NO_WATER, CMD_BUILD_ROAD))) { |
1060 _grow_town_result = -1; |
1072 _grow_town_result = -1; |
1061 } |
1073 } |
1062 return; |
1074 return; |
1063 } |
1075 } |
1064 |
1076 |
1065 /* Check if the bridge is in the right direction */ |
1077 /* Check if the bridge is in the right direction */ |
1066 if ((rcmd == ROAD_X && (i == DIAGDIR_NW || i == DIAGDIR_SE)) || |
1078 if (!(rcmd & DiagDirToRoadBits(bridge_dir))) goto build_road_and_exit; |
1067 (rcmd == ROAD_Y && (i == DIAGDIR_NE || i == DIAGDIR_SW))) { |
1079 |
1068 goto build_road_and_exit; |
1080 /* We are in the right direction */ |
1069 } |
1081 uint32 bridge_length = 0; ///< This value stores the length of the possible bridge |
1070 |
1082 tmptile = tile; ///< Now we use this dummy to store the other waterside |
1071 tmptile = tile; |
|
1072 |
|
1073 /* Now it contains the direction of the slope */ |
|
1074 j = -11; // max 11 tile long bridges |
|
1075 do { |
1083 do { |
1076 if (++j == 0) |
1084 if (bridge_length++ >= 11) { |
|
1085 /* Max 11 tile long bridges */ |
1077 goto build_road_and_exit; |
1086 goto build_road_and_exit; |
1078 tmptile = TILE_MASK(tmptile + TileOffsByDiagDir(i)); |
1087 } |
|
1088 tmptile = TILE_MASK(tmptile + TileOffsByDiagDir(bridge_dir)); |
1079 } while (IsClearWaterTile(tmptile)); |
1089 } while (IsClearWaterTile(tmptile)); |
1080 |
1090 |
1081 /* no water tiles in between? */ |
1091 /* no water tiles in between? */ |
1082 if (j == -10) |
1092 if (bridge_length == 1) goto build_road_and_exit; |
1083 goto build_road_and_exit; |
1093 |
1084 |
1094 for (uint times = 0; times <= 22; times++) { |
1085 /* Quit if it selecting an appropiate bridge type fails a large number of times. */ |
|
1086 j = 22; |
|
1087 do { |
|
1088 byte bridge_type = RandomRange(MAX_BRIDGES - 1); |
1095 byte bridge_type = RandomRange(MAX_BRIDGES - 1); |
|
1096 |
1089 /* Can we actually build the bridge? */ |
1097 /* Can we actually build the bridge? */ |
1090 if (CmdSucceeded(DoCommand(tile, tmptile, bridge_type | ((0x80 | ROADTYPES_ROAD) << 8), DC_AUTO, CMD_BUILD_BRIDGE))) { |
1098 if (CmdSucceeded(DoCommand(tile, tmptile, bridge_type | ((0x80 | ROADTYPES_ROAD) << 8), DC_AUTO, CMD_BUILD_BRIDGE))) { |
1091 DoCommand(tile, tmptile, bridge_type | ((0x80 | ROADTYPES_ROAD) << 8), DC_EXEC | DC_AUTO, CMD_BUILD_BRIDGE); |
1099 DoCommand(tile, tmptile, bridge_type | ((0x80 | ROADTYPES_ROAD) << 8), DC_EXEC | DC_AUTO, CMD_BUILD_BRIDGE); |
1092 _grow_town_result = -1; |
1100 _grow_town_result = -1; |
1093 |
|
1094 /* obviously, if building any bridge would fail, there is no need to try other bridge-types */ |
|
1095 return; |
1101 return; |
1096 } |
1102 } |
1097 } while (--j != 0); |
1103 } |
|
1104 /* Quit if it selecting an appropiate bridge type fails a large number of times. */ |
1098 } |
1105 } |
1099 |
1106 |
1100 /** Returns "growth" if a house was built, or no if the build failed. |
1107 /** Returns "growth" if a house was built, or no if the build failed. |
1101 * @param t town to inquiry |
1108 * @param t town to inquiry |
1102 * @param tile to inquiry |
1109 * @param tile to inquiry |
1103 * @return something other than zero(0)if town expansion was possible |
1110 * @return something other than zero(0)if town expansion was possible |
1104 */ |
1111 */ |
1105 static int GrowTownAtRoad(Town *t, TileIndex tile) |
1112 static int GrowTownAtRoad(Town *t, TileIndex tile) |
1106 { |
1113 { |
1107 int block = 5; // special case |
1114 /* Special case. |
|
1115 * @see GrowTownInTile Check the else if |
|
1116 */ |
|
1117 DiagDirection target_dir = (DiagDirection)5; ///< The direction in which we want to extend the town |
1108 |
1118 |
1109 TILE_ASSERT(tile); |
1119 TILE_ASSERT(tile); |
1110 |
1120 |
1111 /* Number of times to search. |
1121 /* Number of times to search. |
1112 * Better roads, 2X2 and 3X3 grid grow quite fast so we give |
1122 * Better roads, 2X2 and 3X3 grid grow quite fast so we give |
1125 _grow_town_result = 10 + t->num_houses * 4 / 9; |
1135 _grow_town_result = 10 + t->num_houses * 4 / 9; |
1126 break; |
1136 break; |
1127 } |
1137 } |
1128 |
1138 |
1129 do { |
1139 do { |
1130 /* Get a bitmask of the road blocks on a tile */ |
1140 RoadBits cur_rb = GetTownRoadBits(tile); ///< The RoadBits of the current tile |
1131 RoadBits mask = GetTownRoadMask(tile); |
|
1132 |
1141 |
1133 /* Try to grow the town from this point */ |
1142 /* Try to grow the town from this point */ |
1134 GrowTownInTile(&tile, mask, block, t); |
1143 GrowTownInTile(&tile, cur_rb, target_dir, t); |
1135 |
1144 |
1136 /* Exclude the source position from the bitmask |
1145 /* Exclude the source position from the bitmask |
1137 * and return if no more road blocks available */ |
1146 * and return if no more road blocks available */ |
1138 ClrBitT(mask, (block ^ 2)); |
1147 cur_rb &= ~DiagDirToRoadBits(ReverseDiagDir(target_dir)); |
1139 if (mask == ROAD_NONE) |
1148 if (cur_rb == ROAD_NONE) |
1140 return _grow_town_result; |
1149 return _grow_town_result; |
1141 |
1150 |
1142 /* Select a random bit from the blockmask, walk a step |
1151 /* Select a random bit from the blockmask, walk a step |
1143 * and continue the search from there. */ |
1152 * and continue the search from there. */ |
1144 do block = Random() & 3; while (!HASBIT(mask, block)); |
1153 do target_dir = RandomDiagDir(); while (!(cur_rb & DiagDirToRoadBits(target_dir))); |
1145 tile += ToTileIndexDiff(_roadblock_tileadd[block]); |
1154 tile = AddDiagDirToTileIndex(tile, target_dir); |
1146 |
1155 |
1147 if (IsTileType(tile, MP_ROAD)) { |
1156 if (IsTileType(tile, MP_ROAD)) { |
1148 /* Don't allow building over roads of other cities */ |
1157 /* Don't allow building over roads of other cities */ |
1149 if (IsTileOwner(tile, OWNER_TOWN) && GetTownByTile(tile) != t) { |
1158 if (IsTileOwner(tile, OWNER_TOWN) && GetTownByTile(tile) != t) { |
1150 _grow_town_result = -1; |
1159 _grow_town_result = -1; |
1160 } while (--_grow_town_result >= 0); |
1169 } while (--_grow_town_result >= 0); |
1161 |
1170 |
1162 return (_grow_town_result == -2); |
1171 return (_grow_town_result == -2); |
1163 } |
1172 } |
1164 |
1173 |
1165 /** Generate a random road block |
1174 /** |
|
1175 * Generate a random road block. |
1166 * The probability of a straight road |
1176 * The probability of a straight road |
1167 * is somewhat higher than a curved. */ |
1177 * is somewhat higher than a curved. |
|
1178 * |
|
1179 * @return A RoadBits value with 2 bits set |
|
1180 */ |
1168 static RoadBits GenRandomRoadBits() |
1181 static RoadBits GenRandomRoadBits() |
1169 { |
1182 { |
1170 uint32 r = Random(); |
1183 uint32 r = Random(); |
1171 uint a = GB(r, 0, 2); |
1184 uint a = GB(r, 0, 2); |
1172 uint b = GB(r, 8, 2); |
1185 uint b = GB(r, 8, 2); |
1173 if (a == b) b ^= 2; |
1186 if (a == b) b ^= 2; |
1174 return (RoadBits)((1 << a) + (1 << b)); |
1187 return (RoadBits)((ROAD_NW << a) + (ROAD_NW << b)); |
1175 } |
1188 } |
1176 |
1189 |
1177 /** Grow the town |
1190 /** Grow the town |
1178 * @Return true if a house was built, or no if the build failed. */ |
1191 * @Return true if a house was built, or no if the build failed. */ |
1179 static bool GrowTown(Town *t) |
1192 static bool GrowTown(Town *t) |
1180 { |
1193 { |
1181 TileIndex tile; |
1194 |
1182 const TileIndexDiffC *ptr; |
1195 /* Let the town be a ghost town |
1183 PlayerID old_player; |
1196 * The player wanted it in such a way. Thus there he has it. ;) |
|
1197 * Never reached in editor mode. */ |
|
1198 if (_patches.town_layout == TL_NO_ROADS && _generating_world) { |
|
1199 return false; |
|
1200 } |
1184 |
1201 |
1185 static const TileIndexDiffC _town_coord_mod[] = { |
1202 static const TileIndexDiffC _town_coord_mod[] = { |
1186 {-1, 0}, |
1203 {-1, 0}, |
1187 { 1, 1}, |
1204 { 1, 1}, |
1188 { 1, -1}, |
1205 { 1, -1}, |