gnugo-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[gnugo-devel] gtp.c integrity patch


From: Gunnar Farneback
Subject: [gnugo-devel] gtp.c integrity patch
Date: Mon, 15 Oct 2001 00:16:40 +0200
User-agent: EMH/1.14.1 SEMI/1.14.3 (Ushinoya) FLIM/1.14.2 (Yagi-Nishiguchi) APEL/10.3 Emacs/20.7 (sparc-sun-solaris2.7) (with unibyte mode)

This patch restores the integrity of gtp.c and gtp.h after Teun's
reorientation patches. It's not tested. Dan may choose whether to
include it before or after 3.1.10. I hope Teun can verify that this
patch makes no changes to the reorientation functionality.

For the record, gtp.c and gtp.h should not depend on the rest of the
engine. If you're modifying them and find you would need to include
gnugo.h, liberty.h, or some other GNU Go include file, it's a warning
sign that you're doing something wrong.

/Gunnar

Index: interface/gtp.c
===================================================================
RCS file: /cvsroot/gnugo/gnugo/interface/gtp.c,v
retrieving revision 1.7
diff -u -r1.7 gtp.c
--- interface/gtp.c     2001/10/10 19:11:15     1.7
+++ interface/gtp.c     2001/10/14 22:13:06
@@ -41,7 +41,6 @@
 #include <string.h>
 #include <ctype.h>
 #include <assert.h>
-#include <gg_utils.h>
 
 #include "gtp.h"
 
@@ -58,8 +57,11 @@
  * unnecessarily inconvenient.
  */
 static int gtp_boardsize = -1;
-static int gtp_orientation = 0;
 
+/* Vertex transformation hooks. */
+static gtp_transform_ptr vertex_transform_input_hook = NULL;
+static gtp_transform_ptr vertex_transform_output_hook = NULL;
+
 /* Read filehandle gtp_input linewise and interpret as GTP commands. */
 void
 gtp_main_loop(struct gtp_command commands[], FILE *gtp_input)
@@ -118,18 +120,16 @@
   gtp_boardsize = size;
 }
 
-/* Set the orientation used in coordinate conversions. */
+/* If you need to transform the coordinates on input or output, use
+ * this functions to set hook functions which are called any time
+ * coordinates are read or about to be written. In GNU Go this is used
+ * to simulate rotated boards in regression tests.
+ */
 void
-gtp_internal_set_orientation(int orient)
-{
-  gtp_orientation = orient;
-}
-
-/* Set the orientation used in coordinate conversions. */
-int
-gtp_internal_get_orientation(void)
+gtp_set_vertex_transform_hooks(gtp_transform_ptr in, gtp_transform_ptr out)
 {
-  return gtp_orientation;
+  vertex_transform_input_hook = in;
+  vertex_transform_output_hook = out;
 }
 
 /*
@@ -335,7 +335,9 @@
   if (*i < 0 || *i >= gtp_boardsize || *j < 0 || *j >= gtp_boardsize)
     return 0;
 
-  rotate(*i, *j, i, j, gtp_boardsize, gtp_orientation);
+  if (vertex_transform_input_hook != NULL)
+    (*vertex_transform_input_hook)(*i, *j, i, j);
+
   return n;
 }
 
@@ -406,7 +408,12 @@
             || movej[k] < 0 || movej[k] >= gtp_boardsize)
       gtp_printf("??");
     else {
-      inv_rotate(movei[k], movej[k], &ri, &rj, gtp_boardsize, gtp_orientation);
+      if (vertex_transform_output_hook != NULL)
+       (*vertex_transform_output_hook)(movei[k], movej[k], &ri, &rj);
+      else {
+       ri = movei[k];
+       rj = movej[k];
+      }
       gtp_printf("%c%d", 'A' + rj + (rj >= 8), gtp_boardsize - ri);
     }
   }
Index: interface/gtp.h
===================================================================
RCS file: /cvsroot/gnugo/gnugo/interface/gtp.h,v
retrieving revision 1.6
diff -u -r1.6 gtp.h
--- interface/gtp.h     2001/10/09 23:05:14     1.6
+++ interface/gtp.h     2001/10/14 22:13:06
@@ -54,6 +54,9 @@
 /* Function pointer for callback functions. */
 typedef int (*gtp_fn_ptr)(char *s, int id);
 
+/* Function pointer for vertex transform functions. */
+typedef void (*gtp_transform_ptr)(int ai, int aj, int *bi, int *bj);
+
 /* Elements in the array of commands required by gtp_main_loop. */
 struct gtp_command {
   const char *name;
@@ -62,8 +65,8 @@
 
 void gtp_main_loop(struct gtp_command commands[], FILE *gtp_input);
 void gtp_internal_set_boardsize(int size);
-void gtp_internal_set_orientation(int orient);
-int gtp_internal_get_orientation(void);
+void gtp_set_vertex_transform_hooks(gtp_transform_ptr in,
+                                   gtp_transform_ptr out);
 void gtp_mprintf(const char *format, ...);
 void gtp_printf(const char *format, ...);
 void gtp_printid(int id, int status);
Index: interface/play_gtp.c
===================================================================
RCS file: /cvsroot/gnugo/gnugo/interface/play_gtp.c,v
retrieving revision 1.27
diff -u -r1.27 play_gtp.c
--- interface/play_gtp.c        2001/10/13 15:29:15     1.27
+++ interface/play_gtp.c        2001/10/14 22:13:07
@@ -29,23 +29,26 @@
 #include <ctype.h>
 #include <string.h>
 
-#include "gnugo.h"
 #include "interface.h"
 #include "liberty.h"
 #include "gtp.h"
 #include "random.h"
+#include "gg_utils.h"
 
 /* Internal state that's not part of the engine. */
-int color_to_move;
 float komi;
 int handicap;
 
+static int gtp_orientation = 0;
+
 static void
 print_influence(float white_influence[MAX_BOARD][MAX_BOARD],
                float black_influence[MAX_BOARD][MAX_BOARD],
                int influence_regions[MAX_BOARD][MAX_BOARD]);
 static void gtp_print_code(int c);
 static void gtp_print_vertices2(int n, int *moves);
+static void rotate_on_input(int ai, int aj, int *bi, int *bj);
+static void rotate_on_output(int ai, int aj, int *bi, int *bj);
 static int report_uncertainty = 0;
 
 /* For undo, we keep the starting position, and a stack
@@ -350,7 +353,8 @@
     return gtp_failure(id, "unacceptable orientation");
 
   clear_board();
-  gtp_internal_set_orientation(orientation);
+  gtp_orientation = orientation;
+  gtp_set_vertex_transform_hooks(rotate_on_input, rotate_on_output);
   store_position(&starting_position);
   return gtp_success(id, "");
 }
@@ -365,7 +369,7 @@
 {
   UNUSED(s);
 
-  return gtp_success(id, "%d", gtp_internal_get_orientation());
+  return gtp_success(id, "%d", gtp_orientation);
 }
 
 /***************************
@@ -507,7 +511,8 @@
   char untilstring[GTP_BUFSIZE];
   SGFNode *sgf;
   Gameinfo gameinfo;
-  int nread, orient;
+  int nread;
+  int color_to_move;
   
   nread = sscanf(s, "%s %s", filename, untilstring);
   if (nread < 1)
@@ -519,12 +524,12 @@
   gameinfo_clear(&gameinfo, 19, 5.5); /* Probably unnecessary. */
   gameinfo_load_sgfheader(&gameinfo, sgf);
 
-  orient = gtp_internal_get_orientation();
   if (nread == 1)
-    color_to_move = gameinfo_play_sgftree_rot(&gameinfo, sgf, NULL, orient);
+    color_to_move = gameinfo_play_sgftree_rot(&gameinfo, sgf, NULL,
+                                             gtp_orientation);
   else
     color_to_move = gameinfo_play_sgftree_rot(&gameinfo, sgf, untilstring,
-                                              orient);
+                                              gtp_orientation);
 
   gnugo_force_to_globals(&gameinfo.position);
   movenum = gameinfo.move_number;
@@ -2377,6 +2382,15 @@
   gtp_print_vertices(n, movei, movej);
 }
 
+static void rotate_on_input(int ai, int aj, int *bi, int *bj)
+{
+  rotate(ai, aj, bi, bj, board_size, gtp_orientation);
+}
+
+static void rotate_on_output(int ai, int aj, int *bi, int *bj)
+{
+  inv_rotate(ai, aj, bi, bj, board_size, gtp_orientation);
+}
 
 /*
  * Local Variables:



reply via email to

[Prev in Thread] Current Thread [Next in Thread]