shithub: puzzles

Download patch

ref: 96d65e852cea8a95001fa70e3ec2996d4ea5e2b4
parent: 3ae90bcd3a345a932c6bc62cbb985a610caa78f2
author: Simon Tatham <anakin@pobox.com>
date: Tue Nov 14 07:59:44 EST 2023

Untangle: turn #define SHOW_CROSSINGS into a preference.

I just found this #define in the Untangle source code, which I'd
completely forgotten was there. It causes each graph edge to be
highlighted in red if another edge crosses it, so that when you only
have a small number of crossings left to sort out, it's obvious where
they are.

Now we have a preferences system, there's no need to make this a
compile-time option! We can make it run-time selectable, for users who
want the extra help.

--- a/untangle.c
+++ b/untangle.c
@@ -52,9 +52,7 @@
     COL_SYSBACKGROUND,
     COL_BACKGROUND,
     COL_LINE,
-#ifdef SHOW_CROSSINGS
     COL_CROSSEDLINE,
-#endif
     COL_OUTLINE,
     COL_POINT,
     COL_DRAGPOINT,
@@ -95,9 +93,7 @@
     game_params params;
     int w, h;			       /* extent of coordinate system only */
     point *pts;
-#ifdef SHOW_CROSSINGS
     int *crosses;		       /* mark edges which are crossed */
-#endif
     struct graph *graph;
     bool completed, cheated, just_solved;
 };
@@ -791,10 +787,8 @@
     int i, j;
     edge *e, *e2;
 
-#ifdef SHOW_CROSSINGS
     for (i = 0; (e = index234(state->graph->edges, i)) != NULL; i++)
 	state->crosses[i] = false;
-#endif
 
     /*
      * Check correctness: for every pair of edges, see whether they
@@ -808,11 +802,7 @@
 	    if (cross(state->pts[e2->a], state->pts[e2->b],
 		      state->pts[e->a], state->pts[e->b])) {
 		ok = false;
-#ifdef SHOW_CROSSINGS
 		state->crosses[i] = state->crosses[j] = true;
-#else
-		goto done;	       /* multi-level break - sorry */
-#endif
 	    }
 	}
     }
@@ -821,9 +811,6 @@
      * e == NULL if we've gone through all the edge pairs
      * without finding a crossing.
      */
-#ifndef SHOW_CROSSINGS
-    done:
-#endif
     if (ok)
 	state->completed = true;
 }
@@ -860,10 +847,8 @@
 	addedge(state->graph->edges, a, b);
     }
 
-#ifdef SHOW_CROSSINGS
     state->crosses = snewn(count234(state->graph->edges), int);
     mark_crossings(state);	       /* sets up `crosses' and `completed' */
-#endif
 
     return state;
 }
@@ -883,11 +868,9 @@
     ret->completed = state->completed;
     ret->cheated = state->cheated;
     ret->just_solved = state->just_solved;
-#ifdef SHOW_CROSSINGS
     ret->crosses = snewn(count234(ret->graph->edges), int);
     memcpy(ret->crosses, state->crosses,
 	   count234(ret->graph->edges) * sizeof(int));
-#endif
 
     return ret;
 }
@@ -1065,6 +1048,12 @@
      * vertical.
      */
     bool snap_to_grid;
+
+    /*
+     * User preference option to highlight graph edges involved in a
+     * crossing.
+     */
+    bool show_crossed_edges;
 };
 
 static game_ui *new_ui(const game_state *state)
@@ -1073,6 +1062,7 @@
     ui->dragpoint = -1;
     ui->just_moved = ui->just_dragged = false;
     ui->snap_to_grid = false;
+    ui->show_crossed_edges = false;
     return ui;
 }
 
@@ -1080,7 +1070,7 @@
 {
     config_item *cfg;
 
-    cfg = snewn(2, config_item);
+    cfg = snewn(3, config_item);
 
     cfg[0].name = "Snap points to a grid";
     cfg[0].kw = "snap-to-grid";
@@ -1087,9 +1077,14 @@
     cfg[0].type = C_BOOLEAN;
     cfg[0].u.boolean.bval = ui->snap_to_grid;
 
-    cfg[1].name = NULL;
-    cfg[1].type = C_END;
+    cfg[1].name = "Show edges that cross another edge";
+    cfg[1].kw = "show-crossed-edges";
+    cfg[1].type = C_BOOLEAN;
+    cfg[1].u.boolean.bval = ui->show_crossed_edges;
 
+    cfg[2].name = NULL;
+    cfg[2].type = C_END;
+
     return cfg;
 }
 
@@ -1096,6 +1091,7 @@
 static void set_prefs(game_ui *ui, const config_item *cfg)
 {
     ui->snap_to_grid = cfg[0].u.boolean.bval;
+    ui->show_crossed_edges = cfg[1].u.boolean.bval;
 }
 
 static void free_ui(game_ui *ui)
@@ -1313,11 +1309,9 @@
     ret[COL_LINE * 3 + 1] = 0.0F;
     ret[COL_LINE * 3 + 2] = 0.0F;
 
-#ifdef SHOW_CROSSINGS
     ret[COL_CROSSEDLINE * 3 + 0] = 1.0F;
     ret[COL_CROSSEDLINE * 3 + 1] = 0.0F;
     ret[COL_CROSSEDLINE * 3 + 2] = 0.0F;
-#endif
 
     ret[COL_OUTLINE * 3 + 0] = 0.0F;
     ret[COL_OUTLINE * 3 + 1] = 0.0F;
@@ -1451,11 +1445,9 @@
 
     for (i = 0; (e = index234(state->graph->edges, i)) != NULL; i++) {
 	draw_line(dr, ds->x[e->a], ds->y[e->a], ds->x[e->b], ds->y[e->b],
-#ifdef SHOW_CROSSINGS
-		  (oldstate?oldstate:state)->crosses[i] ?
-		  COL_CROSSEDLINE :
-#endif
-		  COL_LINE);
+		  ui->show_crossed_edges &&
+                  (oldstate?oldstate:state)->crosses[i] ?
+		  COL_CROSSEDLINE : COL_LINE);
     }
 
     /*