+
+ *i1 = *i2;
+ *i2 = t;
+}
+
+void gd_cave_set_random_c64_colors(GdCave *cave)
+{
+ const int bright_colors[] = { 1, 3, 7 };
+ const int dark_colors[] = { 2, 6, 8, 9, 11 };
+
+ // always black
+ cave->colorb = gd_c64_color(0);
+ cave->color0 = gd_c64_color(0);
+
+ // choose some bright color for brick
+ cave->color3 = gd_c64_color(bright_colors[gd_random_int_range(0, ARRAY_SIZE(bright_colors))]);
+
+ // choose a dark color for dirt, but should not be == color of brick
+ do
+ {
+ cave->color1 = gd_c64_color(dark_colors[gd_random_int_range(0, ARRAY_SIZE(dark_colors))]);
+ }
+ while (cave->color1 == cave->color3); // so it is not the same as color 1
+
+ // choose any but black for steel wall, but should not be == brick or dirt
+ do
+ {
+ // between 1 and 15 - do not use black for this.
+ cave->color2 = gd_c64_color(gd_random_int_range(1, 16));
+ }
+ while (cave->color1 == cave->color2 || cave->color2 == cave->color3); // so colors are not the same
+
+ // copy amoeba and slime color
+ cave->color4 = cave->color3;
+ cave->color5 = cave->color1;
+}
+
+static void cave_set_random_indexed_colors(GdCave *cave, GdColor (*color_indexer_func) (int, int))
+{
+ int hue = gd_random_int_range(0, 15);
+ int hue_spread = gd_random_int_range(1, 6); // 1..5
+
+ // we only use 0..6, as saturation 15 is too bright (almost always white)
+ // also, saturation 0..1..2 is too dark. the color0=black is there for dark.
+ // so this is also 1..5. when hue spread is low, brightness spread is high
+ int bri_spread = 6 - hue_spread;
+ int bri1 = 8, bri2 = 8 - bri_spread, bri3 = 8 + bri_spread;
+
+ // there are 15 valid choices for hue, so we do a %15
+ int col1 = hue, col2 = (hue + hue_spread + 15) % 15, col3 = (hue - hue_spread + 15) % 15;
+
+ // this makes up a random color, and selects a color triad by hue+5 and hue+10.
+ // also creates a random saturation.
+ // color of brick is 8+sat, so it is always a bright color.
+ // another two are 8-sat and 8.
+ // order of colors is also changed randomly.
+ if (gd_random_boolean()) swap(&bri1, &bri2);
+
+ // we do not touch bri3 (8+sat), as it should be a bright color
+ if (gd_random_boolean()) swap(&col1, &col2);
+ if (gd_random_boolean()) swap(&col2, &col3);
+ if (gd_random_boolean()) swap(&col1, &col3);
+
+ cave->colorb = color_indexer_func(0, 0);
+ cave->color0 = color_indexer_func(0, 0);
+ cave->color1 = color_indexer_func(col1 + 1, bri1);
+ cave->color2 = color_indexer_func(col2 + 1, bri2);
+ cave->color3 = color_indexer_func(col3 + 1, bri3);
+ // amoeba and slime are different
+ // some green thing
+ cave->color4 = color_indexer_func(gd_random_int_range(11, 13), gd_random_int_range(6, 12));
+ // some blueish thing
+ cave->color5 = color_indexer_func(gd_random_int_range(7, 10), gd_random_int_range(0, 6));
+}
+
+static void gd_cave_set_random_atari_colors(GdCave *cave)
+{
+ cave_set_random_indexed_colors(cave, gd_atari_color_huesat);
+}
+
+static void gd_cave_set_random_c64dtv_colors(GdCave *cave)
+{
+ cave_set_random_indexed_colors(cave, gd_c64dtv_color_huesat);
+}
+
+static inline void swapd(double *i1, double *i2)
+{
+ double t = *i1;
+