home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / games / volume9 / othello2 / part03 / events.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-04-27  |  10.1 KB  |  354 lines

  1. /*LINTLIBRARY*/
  2.  
  3. /*  @(#)events.c 1.5 90/04/09
  4.  *
  5.  *  Procedures for handling various events in the Othello game.
  6.  *
  7.  *  Original SunView version by Ed Falk - Sun Microsystems Inc.
  8.  *
  9.  *  Rewritten for independent use by
  10.  *          Rich Burridge, Sun Microsystems, Australia
  11.  *
  12.  *  Copyright (c) Ed Falk & Rich Burridge - Sun Microsystems.
  13.  *                                          All rights reserved.
  14.  *
  15.  *  Permission is given to distribute these sources, as long as the
  16.  *  introductory messages are not removed, and no monies are exchanged.
  17.  *
  18.  *  No responsibility is taken for any errors on inaccuracies inherent
  19.  *  either to the comments or the code of this program, but if reported
  20.  *  to me, then an attempt will be made to fix them.
  21.  */
  22.  
  23. #include "othello.h"
  24. #include "color.h"
  25. #include "extern.h"
  26.  
  27.  
  28. check_button_down(item)
  29. enum panel_type item ;
  30. {
  31.   int n ;
  32.  
  33.   n = (int) item ;
  34.   if ((curx >  items[n].x) && (curx < (items[n].x + items[n].width)) &&
  35.       (cury >  items[n].y) && (cury < (items[n].y + items[n].height)))
  36.     {
  37.       down = nextc ;
  38.       itemno = n ;
  39.       but_inverted = itemno ;
  40.       draw_button((enum panel_type) itemno, C_LGREY, BUT_INVERT) ;
  41.     }
  42. }
  43.  
  44.  
  45. check_cycle_down(item)
  46. enum panel_type item ;
  47. {
  48.   int ix, n, reply ;
  49.  
  50.   n = (int) item ;
  51.   ix = items[n].x + (3 * BWIDTH) + (2 * BGAP) - CWIDTH ;
  52.        if ((curx > ix) && (curx < (ix + (items[n].width / 2))) &&
  53.            (cury > items[n].y) && (cury < (items[n].y + items[n].height)) &&
  54.             nextc != RIGHT_DOWN)
  55.     {
  56.       direction = INCREMENT ;
  57.       down = nextc ;
  58.       itemno = n ;
  59.       draw_cycle((enum panel_type) n, C_LGREY, CY_LINVERT) ;
  60.     }
  61.   else if ((curx > ix) && (curx < (ix + items[n].width)) &&
  62.            (cury > items[n].y) && (cury < (items[n].y + items[n].height)) &&
  63.             nextc != RIGHT_DOWN)
  64.     {
  65.       direction = DECREMENT ;
  66.       down = nextc ;
  67.       itemno = n ;
  68.       draw_cycle((enum panel_type) n, C_LGREY, CY_RINVERT) ;
  69.     }
  70.   else if ((curx > ix) && (curx < (ix + items[n].width)) &&
  71.            (cury > items[n].y) && (cury < (items[n].y + items[n].height)))
  72.     {
  73.       direction = NONE ;
  74.       down = RIGHT_DOWN ;
  75.       itemno = n ;
  76.       if ((reply = do_menu((enum panel_type) itemno)))
  77.         {                                                   
  78.           nextc = RIGHT_UP ;                                
  79.           handle_item(reply - 1) ;
  80.           draw_cycle((enum panel_type) itemno, C_LGREY, CY_NORMAL) ;
  81.         }
  82.     }
  83. }
  84.  
  85.  
  86. check_item_down()
  87. {
  88.   int n ;
  89.  
  90.   for (n = 0; n < MAXITEMS; n++)
  91.     switch (items[n].type)
  92.       {
  93.         case P_BUTTON  : check_button_down((enum panel_type) n) ;
  94.                          break ;
  95.         case P_CYCLE   : check_cycle_down((enum panel_type) n) ;
  96.                          break ;
  97.         case P_MESSAGE : /* do nothing. */ ;
  98.       }
  99. }
  100.  
  101.  
  102. check_item_up()
  103. {
  104.   if ((nextc == LEFT_UP   && down == LEFT_DOWN)   ||
  105.       (nextc == MIDDLE_UP && down == MIDDLE_DOWN) ||
  106.       (nextc == RIGHT_UP  && down == RIGHT_DOWN))
  107.     {
  108.       if (items[itemno].type == P_BUTTON && but_inverted == -1) return ;
  109.       handle_item(items[itemno].value) ;
  110.       if (items[itemno].type == P_BUTTON)
  111.         draw_button((enum panel_type) itemno, C_LGREY, BUT_NORMAL) ;
  112.       else if (items[itemno].type == P_CYCLE)
  113.         draw_cycle((enum panel_type) itemno, C_LGREY, CY_NORMAL) ;
  114.     } 
  115. }
  116.  
  117.  
  118. do_action()
  119. {
  120.   switch (nextc)
  121.     {
  122.       case MOUSE_MOVING : draw_piece(next_player, piece_x, piece_y, RINV) ;
  123.                           piece_x = curx - PIECE_RAD ;
  124.                           piece_y = cury - PIECE_RAD ;
  125.                           draw_piece(next_player, piece_x, piece_y, RINV) ;
  126.                           break ;
  127.                           
  128.       case ENTER_WINDOW : 
  129.       case EXIT_WINDOW  : set_cursor(CANVASCUR) ;
  130.                           draw_piece(next_player, piece_x, piece_y, RINV) ;
  131.                           cmode = (enum cantype) ((int) cmode - 1) ;
  132.                           break ;
  133.                           
  134.       case LEFT_DOWN    : 
  135.       case LEFT_UP      :
  136.       case MIDDLE_DOWN  :
  137.       case MIDDLE_UP    :
  138.       case RIGHT_DOWN   :
  139.       case RIGHT_UP     : set_cursor(CANVASCUR) ;
  140.                           do_selection(nextc) ;
  141.     }                 
  142. }
  143.  
  144.  
  145. do_cycle_key(item, ch)
  146. enum panel_type item ;
  147. int ch ;
  148. {
  149.   int val ;
  150.  
  151.   if (!validkey)
  152.     {
  153.       validkey = cur_ch ;
  154.       message(PANEL_MES, items[(int) item].text) ;
  155.     }
  156.   else
  157.     {
  158.       val = -1 ;
  159.       switch (item)
  160.         {
  161.           case ASPIRATION     : if (ch >= '1' && ch <= '6') val = ch - '1' ;
  162.                                 break ;
  163.           case COMPUTER_PLAYS : switch (ch)
  164.                                   {
  165.                                     case 'b' :
  166.                                     case 'B' : val = 0 ;
  167.                                                break ;
  168.                                     case 'w' :
  169.                                     case 'W' : val = 1 ;
  170.                                                break ;
  171.                                     default  : return ;
  172.                                   }
  173.                                 break ;
  174.           case DIFFICULTY     : if (ch >= '1' && ch <= '4') val = ch - '1' ;
  175.                                 break ;
  176.           case REMARK         : switch (ch)
  177.                                   {
  178.                                     case 'n' :
  179.                                     case 'N' : val = 0 ;
  180.                                                break ;
  181.                                     case 'y' :
  182.                                     case 'Y' : val = 1 ;
  183.                                                break ;
  184.                                     default  : return ;
  185.                                   }
  186.         }
  187.       if (val != -1)
  188.         {
  189.           validkey = 0 ;
  190.           message(PANEL_MES, "") ;
  191.           if (item == COMPUTER_PLAYS && val == items[(int) item].value)
  192.             return ;
  193.           items[(int) item].value = item_value = val ;
  194.           direction = NONE ;
  195.           (*items[(int) item].func)() ;
  196.         }
  197.     }
  198. }
  199.  
  200.  
  201. do_key_move(n1, n2)
  202. int n1, n2 ;
  203. {
  204.   move = (n1 - 'a') * BOARD_SIZE + (n2 - '1') ;
  205.   next_player = (int) cmode - 1 ;
  206.   cmode = (enum cantype) ((int) cmode + 1) ;
  207.   make_move() ;
  208.   validkey = 0 ;
  209. }
  210.  
  211.  
  212. get_xy(n, x, y)      /* Return piece coordinates given board index. */
  213. int n, *x,*y ;
  214. {
  215.   *x = (n & 7) * CELL_SIZE + BBORDER + PIECE_MARGIN ;
  216.   *y = (n >> 3) * CELL_SIZE + BBORDER + PIECE_MARGIN ;
  217. }
  218.  
  219.  
  220. handle_board_event()
  221. {
  222.   switch (cmode)
  223.     {
  224.       case WHITE_START  :
  225.       case BLACK_START  : next_player = (int) cmode - 1 ;
  226.                           if (nextc == LEFT_DOWN || nextc == MIDDLE_DOWN ||
  227.                               nextc == RIGHT_DOWN)
  228.                             {
  229.                               set_cursor(NOCURSOR) ;
  230.                               piece_x = curx - BBORDER - (PIECE_RAD) ;
  231.                               piece_y = cury - BBORDER - (PIECE_RAD) ;
  232.                               draw_piece(next_player, piece_x, piece_y, RINV) ;
  233.                               cmode = (enum cantype) ((int) cmode + 1) ;
  234.                             }
  235.                           break ;
  236.       case WHITE_MOVING :
  237.       case BLACK_MOVING : do_action() ;
  238.     }
  239. }
  240.  
  241.  
  242. handle_event()
  243. {
  244.   process_event() ;
  245.  
  246.        if (nextc == FRAME_REPAINT) init_canvas() ;
  247.   else if (nextc == KEYBOARD)      handle_key() ;
  248.   else if (nextc == EXIT_WINDOW && but_inverted != -1)
  249.     {
  250.       draw_button((enum panel_type) but_inverted, C_LGREY, BUT_NORMAL) ;
  251.       but_inverted = -1 ;
  252.       down = 0 ;
  253.     }
  254.   else if (cury > (CY + BBORDER)) handle_board_event() ;
  255.   else if (nextc == LEFT_UP || nextc == MIDDLE_UP || nextc == RIGHT_UP)
  256.     check_item_up() ;
  257.   else if (nextc == LEFT_DOWN || nextc == MIDDLE_DOWN || nextc == RIGHT_DOWN)
  258.     check_item_down() ;
  259. }
  260.  
  261.  
  262. handle_item(val)
  263. int val ;
  264. {
  265.   items[itemno].value = item_value = val ;
  266.   (*items[itemno].func)() ;
  267.   but_inverted = -1 ;
  268.   down = 0 ;
  269. }
  270.  
  271.  
  272. handle_key()     /* Process the latest key that the user has pressed. */
  273. {
  274.   char str[8] ;  /* To display half move position. */
  275.   int nextc ;
  276.  
  277.   if (cur_ch == ESCAPE) validkey = 0 ;
  278.   if (validkey)
  279.     {
  280.       nextc = cur_ch ;
  281.       cur_ch = validkey ;
  282.     }
  283.   switch (cur_ch)
  284.     {
  285.       case 'A' : do_cycle_key(ASPIRATION, nextc) ;
  286.                  break ;
  287.       case 'C' : do_cycle_key(COMPUTER_PLAYS, nextc) ;
  288.                  break ;
  289.       case 'D' : do_cycle_key(DIFFICULTY, nextc) ;
  290.                  break ;
  291.       case 'r' :
  292.       case 'R' : do_cycle_key(REMARK, nextc) ;
  293.                  break ;
  294.       case 'l' :
  295.       case 'L' : last() ;
  296.                  break ;
  297.       case 'n' :
  298.       case 'N' : new_game() ;
  299.                  break ;
  300.       case 'q' :
  301.       case 'Q' : quit() ;
  302.       case 's' :
  303.       case 'S' : suggest() ;
  304.                  break ;
  305.       case 'u' :
  306.       case 'U' : undo() ;
  307.                  break ;
  308.       case '1' :
  309.       case '2' :
  310.       case '3' :
  311.       case '4' :
  312.       case '5' :
  313.       case '6' :
  314.       case '7' :
  315.       case '8' : if (!validkey)
  316.                    {
  317.                      validkey = cur_ch ;
  318.                      SPRINTF(str, "Move: %c", cur_ch) ;
  319.                      message(PANEL_MES, str) ;
  320.                    }
  321.                  else if (nextc >= 'a' && nextc <= 'h')
  322.                    {
  323.                      SPRINTF(str, "Move: %c%c", cur_ch, nextc) ;
  324.                      message(PANEL_MES, str) ;
  325.                      do_key_move(nextc, cur_ch) ;
  326.                    }
  327.                  else validkey = 0 ;
  328.                  break ;
  329.       case 'a' :
  330.       case 'b' :
  331.       case 'c' :
  332.       case 'd' :
  333.       case 'e' :
  334.       case 'f' :
  335.       case 'g' :
  336.       case 'h' : if (!validkey)
  337.                    {
  338.                      validkey = cur_ch ;
  339.                      SPRINTF(str, "Move: %c", cur_ch) ;
  340.                      message(PANEL_MES, str) ;
  341.                    }
  342.                  else if (nextc >= '1' && nextc <= '8')
  343.                    {
  344.                      SPRINTF(str, "Move: %c%c", cur_ch, nextc) ;
  345.                      message(PANEL_MES, str) ;
  346.                      do_key_move(cur_ch, nextc) ;
  347.                    }
  348.                  else validkey = 0 ;
  349.                  break ;
  350.       default  : message(PANEL_MES, "") ;
  351.                  validkey = 0 ;
  352.     }
  353. }
  354.