Files
puzzles/CMakeLists.txt
Simon Tatham 61e9c78248 grid.c: new and improved Penrose tiling generator.
The new generator works on the same 'combinatorial coordinates' system
as the more recently written Hats and Spectre generators.

When I came up with that technique for the Hats tiling, I was already
tempted to rewrite the Penrose generator on the same principle,
because it has a lot of advantages over the previous technique of
picking a randomly selected patch out of the subdivision of a huge
starting tile. It generates the exact limiting distribution of
possible tiling patches rather than an approximation based on how big
a tile you subdivided; it doesn't use any overly large integers or
overly precise floating point to suffer overflow or significance loss,
because it never has to even _think_ about the coordinates of any
point not in the actual output area.

But I resisted the temptation to throw out our existing Penrose
generator and move to the shiny new algorithm, for just one reason:
backwards compatibility. There's no sensible way to translate existing
Loopy game IDs into the new notation, so they would stop working,
unless we kept the old generator around as well to interpret the
previous system. And although _random seeds_ aren't guaranteed to
generate the same result from one build of Puzzles to the next, I do
try to keep existing descriptive game IDs working.

So if we had to keep the old generator around anyway, it didn't seem
worth writing a new one...

... at least, until we discovered that the old generator has a latent
bug. The function that decides whether each tile is within the target
area, and hence whether to make it part of the output grid, is based
on floating-point calculation of the tile's vertices. So a change in
FP rounding behaviour between two platforms could cause them to
interpret the same grid description differently, invalidating a Loopy
game on that grid. This is _unlikely_, as long as everyone uses IEEE
754 double, but the C standard doesn't actually guarantee that.

We found this out when I started investigating a slight distortion in
large instances of Penrose Loopy. For example, the Loopy random seed
"40x40t11#12345", as of just before this commit, generates a game
description beginning with the Penrose grid string "G-4944,5110,108",
in which you can see a star shape of five darts a few tiles down the
left edge, where two of the radii of the star don't properly line up
with edges in the surrounding shell of kites that they should be
collinear with. This turns out to be due to FP error: not because
_double precision_ ran out, but because the definitions of COS54,
SIN54, COS18 and SIN18 in penrose.c were specified to only 7 digits of
precision. And when you expand a patch of tiling that large, you end
up with integer combinations of those numbers with coefficients about
7 digits long, mostly cancelling out to leave a much smaller output
value, and the inaccuracies in those constants start to be noticeable.

But having noticed that, it then became clear that these badly
computed values were also critical to _correctness_ of the grid. So
they can't be fixed without invalidating old game IDs. _And_ then we
realised that this also means existing platforms might disagree on a
game ID's validity.

So if we have to break compatibility anyway, we should go all the way,
and instead of fixing the old system, introduce the shiny new system
that gets all of this right. Therefore, the new penrose.c uses the
more reliable combinatorial-coordinates system which doesn't deal in
integers that large in the first place. Also, there's no longer any
floating point at all in the calculation of tile vertex coordinates:
the combinations of 1 and sqrt(5) are computed exactly by the
n_times_root_k function. So now a large Penrose grid should never have
obvious failures of alignment like that.

The old system is kept for backwards compatibility. It's not fully
reliable, because that bug hasn't been fixed - but any Penrose Loopy
game ID that was working before on _some_ platform should still work
on that one. However, new Penrose Loopy game IDs are based on
combinatorial coordinates, computed in exact arithmetic, and should be
properly stable.

The new code looks suspiciously like the Spectre code (though
considerably simpler - the Penrose coordinate maps are easy enough
that I just hand-typed them rather than having an elaborate auxiliary
data-generation tool). That's because they were similar enough in
concept to make it possible to clone and hack. But there are enough
divergences that it didn't seem like a good idea to try to merge them
again afterwards - in particular, the fact that output Penrose tiles
are generated by merging two triangular metatiles while Spectres are
subdivisions of their metatiles.
2023-07-07 18:17:02 +01:00

288 lines
8.1 KiB
CMake

cmake_minimum_required(VERSION 3.5)
project(puzzles
LANGUAGES C)
include(cmake/setup.cmake)
add_library(core_obj OBJECT
combi.c divvy.c drawing.c dsf.c findloop.c grid.c latin.c
laydomino.c loopgen.c malloc.c matching.c midend.c misc.c penrose.c
penrose-legacy.c ps.c random.c sort.c tdq.c tree234.c version.c
${platform_common_sources})
add_library(core $<TARGET_OBJECTS:core_obj>)
add_library(common $<TARGET_OBJECTS:core_obj> hat.c spectre.c)
include_directories(${CMAKE_CURRENT_SOURCE_DIR})
puzzle(blackbox
DISPLAYNAME "Black Box"
DESCRIPTION "Ball-finding puzzle"
OBJECTIVE "Find the hidden balls in the box by bouncing laser beams \
off them.")
puzzle(bridges
DISPLAYNAME "Bridges"
DESCRIPTION "Bridge-placing puzzle"
OBJECTIVE "Connect all the islands with a network of bridges.")
puzzle(cube
DISPLAYNAME "Cube"
DESCRIPTION "Rolling cube puzzle"
OBJECTIVE "Pick up all the blue squares by rolling the cube over them.")
puzzle(dominosa
DISPLAYNAME "Dominosa"
DESCRIPTION "Domino tiling puzzle"
OBJECTIVE "Tile the rectangle with a full set of dominoes.")
solver(dominosa)
puzzle(fifteen
DISPLAYNAME "Fifteen"
DESCRIPTION "Sliding block puzzle"
OBJECTIVE "Slide the tiles around to arrange them into order.")
solver(fifteen)
puzzle(filling
DISPLAYNAME "Filling"
DESCRIPTION "Polyomino puzzle"
OBJECTIVE "Mark every square with the area of its containing region.")
solver(filling)
puzzle(flip
DISPLAYNAME "Flip"
DESCRIPTION "Tile inversion puzzle"
OBJECTIVE "Flip groups of squares to light them all up at once.")
puzzle(flood
DISPLAYNAME "Flood"
DESCRIPTION "Flood-filling puzzle"
OBJECTIVE "Turn the grid the same colour in as few flood fills as possible.")
puzzle(galaxies
DISPLAYNAME "Galaxies"
DESCRIPTION "Symmetric polyomino puzzle"
OBJECTIVE "Divide the grid into rotationally symmetric regions each \
centred on a dot.")
solver(galaxies)
cliprogram(galaxiespicture galaxies.c
COMPILE_DEFINITIONS STANDALONE_PICTURE_GENERATOR)
guiprogram(galaxieseditor galaxies.c
COMPILE_DEFINITIONS EDITOR)
puzzle(guess
DISPLAYNAME "Guess"
DESCRIPTION "Combination-guessing puzzle"
OBJECTIVE "Guess the hidden combination of colours.")
puzzle(inertia
DISPLAYNAME "Inertia"
DESCRIPTION "Gem-collecting puzzle"
OBJECTIVE "Collect all the gems without running into any of the mines.")
puzzle(keen
DISPLAYNAME "Keen"
DESCRIPTION "Arithmetic Latin square puzzle"
OBJECTIVE "Complete the latin square in accordance with the \
arithmetic clues.")
solver(keen latin.c)
puzzle(lightup
DISPLAYNAME "Light Up"
DESCRIPTION "Light-bulb placing puzzle"
OBJECTIVE "Place bulbs to light up all the squares.")
solver(lightup)
puzzle(loopy
DISPLAYNAME "Loopy"
DESCRIPTION "Loop-drawing puzzle"
OBJECTIVE "Draw a single closed loop, given clues about number of \
adjacent edges.")
solver(loopy)
puzzle(magnets
DISPLAYNAME "Magnets"
DESCRIPTION "Magnet-placing puzzle"
OBJECTIVE "Place magnets to satisfy the clues and avoid like poles \
touching.")
solver(magnets)
puzzle(map
DISPLAYNAME "Map"
DESCRIPTION "Map-colouring puzzle"
OBJECTIVE "Colour the map so that adjacent regions are never the \
same colour.")
solver(map)
puzzle(mines
DISPLAYNAME "Mines"
DESCRIPTION "Mine-finding puzzle"
OBJECTIVE "Find all the mines without treading on any of them.")
cliprogram(mineobfusc mines.c COMPILE_DEFINITIONS STANDALONE_OBFUSCATOR)
puzzle(mosaic
DISPLAYNAME "Mosaic"
DESCRIPTION "Grid-filling puzzle"
OBJECTIVE "Fill in the grid given clues about number of \
nearby black squares.")
puzzle(net
# The Windows Net shouldn't be called 'net.exe', since Windows
# already has a reasonably important utility program by that name!
WINDOWS_EXE_NAME netgame
DISPLAYNAME "Net"
DESCRIPTION "Network jigsaw puzzle"
OBJECTIVE "Rotate each tile to reassemble the network.")
puzzle(netslide
DISPLAYNAME "Netslide"
DESCRIPTION "Toroidal sliding network puzzle"
OBJECTIVE "Slide a row at a time to reassemble the network.")
puzzle(nullgame)
puzzle(palisade
DISPLAYNAME "Palisade"
DESCRIPTION "Grid-division puzzle"
OBJECTIVE "Divide the grid into equal-sized areas in accordance with\
the clues.")
puzzle(pattern
DISPLAYNAME "Pattern"
DESCRIPTION "Pattern puzzle"
OBJECTIVE "Fill in the pattern in the grid, given only the lengths \
of runs of black squares.")
solver(pattern)
cliprogram(patternpicture pattern.c
COMPILE_DEFINITIONS STANDALONE_PICTURE_GENERATOR)
puzzle(pearl
DISPLAYNAME "Pearl"
DESCRIPTION "Loop-drawing puzzle"
OBJECTIVE "Draw a single closed loop, given clues about corner and \
straight squares.")
solver(pearl)
cliprogram(pearlbench pearl.c COMPILE_DEFINITIONS STANDALONE_SOLVER)
puzzle(pegs
DISPLAYNAME "Pegs"
DESCRIPTION "Peg solitaire puzzle"
OBJECTIVE "Jump pegs over each other to remove all but one.")
puzzle(range
DISPLAYNAME "Range"
DESCRIPTION "Visible-distance puzzle"
OBJECTIVE "Place black squares to limit the visible distance from \
each numbered cell.")
puzzle(rect
DISPLAYNAME "Rectangles"
DESCRIPTION "Rectangles puzzle"
OBJECTIVE "Divide the grid into rectangles with areas equal to the \
numbers.")
puzzle(samegame
DISPLAYNAME "Same Game"
DESCRIPTION "Block-clearing puzzle"
OBJECTIVE "Clear the grid by removing touching groups of the same \
colour squares.")
puzzle(signpost
DISPLAYNAME "Signpost"
DESCRIPTION "Square-connecting puzzle"
OBJECTIVE "Connect the squares into a path following the arrows.")
solver(signpost)
puzzle(singles
DISPLAYNAME "Singles"
DESCRIPTION "Number-removing puzzle"
OBJECTIVE "Black out the right set of duplicate numbers.")
solver(singles)
puzzle(sixteen
DISPLAYNAME "Sixteen"
DESCRIPTION "Toroidal sliding block puzzle"
OBJECTIVE "Slide a row at a time to arrange the tiles into order.")
puzzle(slant
DISPLAYNAME "Slant"
DESCRIPTION "Maze-drawing puzzle"
OBJECTIVE "Draw a maze of slanting lines that matches the clues.")
solver(slant)
puzzle(solo
DISPLAYNAME "Solo"
DESCRIPTION "Number placement puzzle"
OBJECTIVE "Fill in the grid so that each row, column and square \
block contains one of every digit.")
solver(solo)
puzzle(tents
DISPLAYNAME "Tents"
DESCRIPTION "Tent-placing puzzle"
OBJECTIVE "Place a tent next to each tree.")
solver(tents)
puzzle(towers
DISPLAYNAME "Towers"
DESCRIPTION "Tower-placing Latin square puzzle"
OBJECTIVE "Complete the latin square of towers in accordance with \
the clues.")
solver(towers latin.c)
puzzle(tracks
DISPLAYNAME "Tracks"
DESCRIPTION "Path-finding railway track puzzle"
OBJECTIVE "Fill in the railway track according to the clues.")
solver(tracks)
puzzle(twiddle
DISPLAYNAME "Twiddle"
DESCRIPTION "Rotational sliding block puzzle"
OBJECTIVE "Rotate the tiles around themselves to arrange them into order.")
puzzle(undead
DISPLAYNAME "Undead"
DESCRIPTION "Monster-placing puzzle"
OBJECTIVE "Place ghosts, vampires and zombies so that the right \
numbers of them can be seen in mirrors.")
puzzle(unequal
DISPLAYNAME "Unequal"
DESCRIPTION "Latin square puzzle"
OBJECTIVE "Complete the latin square in accordance with the > signs.")
solver(unequal latin.c)
puzzle(unruly
DISPLAYNAME "Unruly"
DESCRIPTION "Black and white grid puzzle"
OBJECTIVE "Fill in the black and white grid to avoid runs of three.")
solver(unruly)
puzzle(untangle
DISPLAYNAME "Untangle"
DESCRIPTION "Planar graph layout puzzle"
OBJECTIVE "Reposition the points so that the lines do not cross.")
add_subdirectory(unfinished)
add_subdirectory(auxiliary)
if(build_cli_programs)
write_generated_games_header()
include(CheckFunctionExists)
check_function_exists(HF_ITER HAVE_HF_ITER)
set(WITH_LIBFUZZER OFF
CACHE BOOL "Build fuzzpuzz using Clang's libFuzzer")
cliprogram(fuzzpuzz fuzzpuzz.c list.c ${puzzle_sources}
COMPILE_DEFINITIONS COMBINED $<$<BOOL:${WITH_LIBFUZZER}>:OMIT_MAIN>
$<$<BOOL:${HAVE_HF_ITER}>:HAVE_HF_ITER>)
target_include_directories(fuzzpuzz PRIVATE ${generated_include_dir})
if(WITH_LIBFUZZER)
target_compile_options(fuzzpuzz PRIVATE -fsanitize=fuzzer)
set_target_properties(fuzzpuzz PROPERTIES LINK_FLAGS -fsanitize=fuzzer)
endif()
endif()
build_extras()