* Try to build a lighthouse. * @return True iff building a lighthouse succeeded. */
| 748 | * @return True iff building a lighthouse succeeded. |
| 749 | */ |
| 750 | static bool TryBuildLightHouse() |
| 751 | { |
| 752 | uint maxx = Map::MaxX(); |
| 753 | uint maxy = Map::MaxY(); |
| 754 | uint r = Random(); |
| 755 | |
| 756 | /* Scatter the lighthouses more evenly around the perimeter */ |
| 757 | int perimeter = (GB(r, 16, 16) % (2 * (maxx + maxy))) - maxy; |
| 758 | DiagDirection dir; |
| 759 | for (dir = DIAGDIR_NE; perimeter > 0; dir++) { |
| 760 | perimeter -= (DiagDirToAxis(dir) == AXIS_X) ? maxx : maxy; |
| 761 | } |
| 762 | |
| 763 | TileIndex tile; |
| 764 | switch (dir) { |
| 765 | default: |
| 766 | case DIAGDIR_NE: tile = TileXY(maxx - 1, r % maxy); break; |
| 767 | case DIAGDIR_SE: tile = TileXY(r % maxx, 1); break; |
| 768 | case DIAGDIR_SW: tile = TileXY(1, r % maxy); break; |
| 769 | case DIAGDIR_NW: tile = TileXY(r % maxx, maxy - 1); break; |
| 770 | } |
| 771 | |
| 772 | /* Only build lighthouses at tiles where the border is sea. */ |
| 773 | if (!IsTileType(tile, MP_WATER) || GetWaterClass(tile) != WaterClass::Sea) return false; |
| 774 | |
| 775 | for (int j = 0; j < 19; j++) { |
| 776 | int h; |
| 777 | if (IsTileType(tile, MP_CLEAR) && IsTileFlat(tile, &h) && h <= 2 && !IsBridgeAbove(tile)) { |
| 778 | for (auto t : SpiralTileSequence(tile, 9)) { |
| 779 | if (IsObjectTypeTile(t, OBJECT_LIGHTHOUSE)) return false; |
| 780 | } |
| 781 | BuildObject(OBJECT_LIGHTHOUSE, tile); |
| 782 | assert(tile < Map::Size()); |
| 783 | return true; |
| 784 | } |
| 785 | tile += TileOffsByDiagDir(dir); |
| 786 | if (!IsValidTile(tile)) return false; |
| 787 | } |
| 788 | return false; |
| 789 | } |
| 790 | |
| 791 | /** |
| 792 | * Try to build a transmitter. |
no test coverage detected