MCPcopy Create free account
hub / github.com/OpenTTD/OpenTTD / TryBuildLightHouse

Function TryBuildLightHouse

src/object_cmd.cpp:750–789  ·  view source on GitHub ↗

* Try to build a lighthouse. * @return True iff building a lighthouse succeeded. */

Source from the content-addressed store, hash-verified

748 * @return True iff building a lighthouse succeeded.
749 */
750static 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.

Callers 1

GenerateObjectsFunction · 0.85

Calls 13

GBFunction · 0.85
DiagDirToAxisFunction · 0.85
TileXYFunction · 0.85
IsTileTypeFunction · 0.85
GetWaterClassFunction · 0.85
IsTileFlatFunction · 0.85
IsBridgeAboveFunction · 0.85
SpiralTileSequenceClass · 0.85
IsObjectTypeTileFunction · 0.85
BuildObjectFunction · 0.85
TileOffsByDiagDirFunction · 0.85
IsValidTileFunction · 0.85

Tested by

no test coverage detected