home *** CD-ROM | disk | FTP | other *** search
/ Dream 57 / Amiga_Dream_57.iso / Amiga / Jeux / Reflexion / Crafty-15.19.lha / crafty-15.19 / src / interupt.c < prev    next >
C/C++ Source or Header  |  1998-09-13  |  9KB  |  258 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <signal.h>
  5. #include "chess.h"
  6. #include "data.h"
  7.  
  8. /* last modified 05/06/98 */
  9. /*
  10. ********************************************************************************
  11. *                                                                              *
  12. *   Interrupt() is used to read in a move when the operator types something    *
  13. *   while a search is in progress (during pondering as one example.)  this     *
  14. *   routine reads in a command (move) and then makes two attempts to use this  *
  15. *   input:  (1) call Option() to see if the command can be executed;  (2) try  *
  16. *   InputMove() to see if this input is a legal move;  if so, and we are       *
  17. *   pondering see if it matches the move we are pondering.                     *
  18. *                                                                              *
  19. ********************************************************************************
  20. */
  21. void Interrupt(int ply)
  22. {
  23.   int temp, *mvp, left, readstat, result, time_used;
  24.   int save_move_number;
  25.   TREE *tree=local[0];
  26.   static int busy=0;
  27. /*
  28.  ----------------------------------------------------------
  29. |                                                          |
  30. |   if another thread is already reading the input, this   |
  31. |   thread can exit now.                                   |
  32. |                                                          |
  33.  ----------------------------------------------------------
  34. */
  35. #if defined(SMP)
  36.   Lock(lock_io);
  37.   if (busy==1) {
  38.     UnLock(lock_io);
  39.     return;
  40.   }
  41.   busy=1;
  42.   UnLock(lock_io);
  43. #endif
  44.   if (abort_search) {
  45.     busy=0;
  46.     return;
  47.   }
  48. /*
  49.  ----------------------------------------------------------
  50. |                                                          |
  51. |   if trying to find a move to ponder, and the operator   |
  52. |   types a command, exit a.s.a.p.                         |
  53. |                                                          |
  54.  ----------------------------------------------------------
  55. */
  56.   if (puzzling) 
  57.     abort_search=1;
  58. /*
  59.  ----------------------------------------------------------
  60. |                                                          |
  61. |   first check to see if this is a command by calling     |
  62. |   Option().  Option() will return a 0 if it didn't       |
  63. |   recognize the command; otherwise it returns a 1 if     |
  64. |   the command was executed, or a 2 if we need to abort   |
  65. |   the search to execute the command.                     |
  66. |                                                          |
  67.  ----------------------------------------------------------
  68. */
  69.   else do {
  70.     readstat=Read(0,buffer);
  71.     if (readstat <= 0) break;
  72.     nargs=ReadParse(buffer,args,"     ;");
  73.     if (nargs == 0) {
  74.       Print(128,"ok.\n");
  75.       break;
  76.     }
  77.     if (strcmp(args[0],".")) {
  78.       save_move_number=move_number;
  79.       if (!wtm) move_number--;
  80.       if (wtm)
  81.         Print(128,"White(%d): %s\n",move_number,buffer);
  82.       else
  83.         Print(128,"Black(%d): %s\n",move_number,buffer);
  84.       move_number=save_move_number;
  85.     }
  86. /*
  87.  ----------------------------------------------------------
  88. |                                                          |
  89. |   "." command displays status of current search.         |
  90. |                                                          |
  91.  ----------------------------------------------------------
  92. */
  93.     if (!strcmp(args[0],".")) {
  94.       if (xboard) {
  95.         end_time=ReadClock(time_type);
  96.         time_used=(end_time-start_time);
  97.         printf("stat01: %d ",time_used);
  98.         printf("%d ",tree->nodes_searched);
  99.         printf("%d ",iteration_depth); 
  100.         for (left=0,mvp=tree->last[0];mvp<tree->last[1];mvp++) 
  101.           if (!tree->searched_this_root_move[mvp-tree->last[0]]) left++;
  102.         printf("%d %d\n",left,tree->last[1]-tree->last[0]);
  103.         fflush(stdout);
  104.         break;
  105.       }
  106.       else {
  107.         end_time=ReadClock(time_type);
  108.         time_used=(end_time-start_time);
  109.         printf("time:%s ",DisplayTime(time_used));
  110.         printf("nodes:%d\n",tree->nodes_searched);
  111.         DisplayTreeState(local[0],1,0,ply);
  112.       }
  113.     }
  114. /*
  115.  ----------------------------------------------------------
  116. |                                                          |
  117. |   "mn" command is used to set the move number to a       |
  118. |   specific value...                                      |
  119. |                                                          |
  120.  ----------------------------------------------------------
  121. */
  122.     else if (!strcmp("mn",args[0])) {
  123.       if (nargs == 2) {
  124.         move_number=atoi(args[1]);
  125.         Print(4095,"move number set to %d\n",move_number);
  126.       }
  127.     }
  128. /*
  129.  ----------------------------------------------------------
  130. |                                                          |
  131. |   "?" command says "move now!"                           |
  132. |                                                          |
  133.  ----------------------------------------------------------
  134. */
  135.     else if (!strcmp(args[0],"?")) {
  136.       if (thinking) {
  137.         time_abort=1;
  138.         abort_search=1;
  139.       }
  140.     }
  141. /*
  142.  ----------------------------------------------------------
  143. |                                                          |
  144. |   next see if Option() recognizes this as a command.     |
  145. |                                                          |
  146.  ----------------------------------------------------------
  147. */
  148.     else {
  149.       save_move_number=move_number;
  150.       if (!wtm) move_number--;
  151.       result=Option(tree);
  152.       move_number=save_move_number;
  153.       if (result >= 2) {
  154.         if (thinking && result!=3)
  155.           Print(4095,"command not legal now.\n");
  156.         else {
  157.           abort_search=1;
  158.           analyze_move_read=1;
  159.           break;
  160.         }
  161.       }
  162.       else if ((result != 1) && analyze_mode) {
  163.         abort_search=1;
  164.         analyze_move_read=1;
  165.         break;
  166.       }
  167. /*
  168.  ----------------------------------------------------------
  169. |                                                          |
  170. |   now, check to see if the operator typed a move.  if    |
  171. |   so, and it matched the predicted move, switch from     |
  172. |   pondering to thinking to start the timer.  if the      |
  173. |   is a move, but not the predicted move, abort the       |
  174. |   search, and start over with the right move.            |
  175. |                                                          |
  176.  ----------------------------------------------------------
  177. */
  178.       else if (!result) {
  179.         if (pondering) {
  180.           nargs=ReadParse(buffer,args,"     ;");
  181.           temp=InputMove(tree,args[0],0,ChangeSide(root_wtm),1,1);
  182.           if (temp) {
  183.             if (auto232) {
  184.               char *mv=OutputMoveICS(tree,temp);
  185.               Delay232(auto232_delay);
  186.               if (!wtm) fprintf(auto_file,"\t");
  187.               fprintf(auto_file, " %c%c-%c%c", mv[0], mv[1], mv[2], mv[3]);
  188.               if ((mv[4] != ' ') && (mv[4] != 0))
  189.                 fprintf(auto_file, "/%c", mv[4]);
  190.               fprintf(auto_file, "\n");
  191.               fflush(auto_file);
  192.             }
  193.             if ((From(temp) == From(ponder_move)) &&
  194.                 (To(temp) == To(ponder_move)) &&
  195.                 (Piece(temp) == Piece(ponder_move)) &&
  196.                 (Captured(temp) == Captured(ponder_move)) &&
  197.                 (Promote(temp) == Promote(ponder_move))) {
  198.               predicted++;
  199.               made_predicted_move=1;
  200.               pondering=0;
  201.               thinking=1;
  202.               opponent_end_time=ReadClock(elapsed);
  203.               program_start_time=ReadClock(time_type);
  204.               Print(128,"predicted move made.\n");
  205.             }
  206.             else {
  207.               abort_search=1;
  208.               break;
  209.             }
  210.           }
  211.           else if (!strcmp(args[0],"go") || !strcmp(args[0],"move") ||
  212.                    !strcmp(args[0],"SP")) {
  213.             abort_search=1;
  214.             break;
  215.           }
  216.           else {
  217.             Print(4095,"Illegal move: %s\n", args[0]);
  218.             if (log_file) DisplayChessBoard(log_file,tree->pos);
  219.           }
  220.         }
  221.         else Print(4095,"unrecognized/illegal command: %s\n", args[0]);
  222.       }
  223.     }
  224.   } while (1);
  225.   if (log_file) fflush(log_file);
  226.   busy=0;
  227. }
  228. /*
  229. ********************************************************************************
  230. *                                                                              *
  231. *   SignalInterrupt() is used to catch SIGINT and SIGTERM which are used by    *
  232. *   Xboard.  SIGINT is used when the operator clicks the "move now" button,    *
  233. *   while SIGTERM is used to detect when Crafty is terminating.                *
  234. *                                                                              *
  235. ********************************************************************************
  236. */
  237. #if defined(NT_i386) || defined(NT_AXP)
  238. void _cdecl SignalInterrupt(int signal_type)
  239. #else
  240. void SignalInterrupt(int signal_type)
  241. #endif
  242. {
  243.   switch (signal_type) {
  244.   case SIGINT:
  245.     if (xboard) {
  246.       if (thinking) {
  247.         time_abort=1;
  248.         abort_search=1;
  249.       }
  250.       signal(SIGINT,SignalInterrupt);
  251.       break;
  252.     }
  253.     break;
  254.   default:
  255.     break;
  256.   }
  257. }
  258.