home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / games / volume15 / xstratego / part01 / main.c < prev    next >
C/C++ Source or Header  |  1993-01-27  |  9KB  |  336 lines

  1. /*
  2.  * Main.c
  3.  */
  4.  
  5. #include "stratego.h"
  6.  
  7. XtAppContext app_con;
  8. XtInputId EnemyID;
  9. Widget Toplevel, Stratego, Board, Tiles, Message;
  10.  
  11. static void init_actions ();
  12. void new_button_box ();
  13.  
  14. static String fallback_resources[] = { 
  15.     "*PopupShell.allowShellResize:  True",
  16.     "*PopupShell.borderWidth:       4",
  17.     "*PopupShell.Dialog*shapeStyle: oval",
  18.     "*PopupShell.Dialog.value.translations: #override \\n \
  19.                         <Key>Return: DialogOk()",
  20.     "*Board.borderWidth:             4",
  21.     "*Board.defaultDistance:         1",
  22.  
  23.     "*Water.highlightThickness:         0",
  24.  
  25.     "*BoardPosition.highlightThickness: 0",
  26.  
  27.     "*Tiles.borderWidth:             2",
  28.     "*Tiles.defaultDistance:         2",
  29.     "*TilePosition.orientation:        horizontal",
  30.  
  31.     "*TileBitmap.borderWidth:        0",
  32.     "*TileBitmap.highlightThickness:    1",
  33.  
  34.     "*ButtonSet.borderWidth:        0",
  35.     "*ButtonSet.defaultDistance:        10",
  36.  
  37.     "*Button.shapeStyle:            oval",
  38.  
  39.     "*Message.resizable:            True",
  40.     "*Message.borderWidth:            0",
  41.     "*translations:                #augment \\n \
  42.                         <Btn2Down>: CaughtByHook() \\n \
  43.                         <Btn3Down>: CaughtByHook()",
  44.     NULL
  45. };
  46.  
  47. static BUTTON setup_buttons[] = {
  48.     " Cursor  ", WipeCursor, TRUE, NULL,
  49.     "  Load   ", Load,     TRUE, NULL,
  50.     "  Ready  ", Ready,     TRUE, NULL,
  51.     " Message ", Mail,     TRUE, NULL,
  52.     "  Clear  ", Clear,     TRUE, NULL,
  53.     "  Store  ", Store,     TRUE, NULL,
  54.     "   Quit  ", Quit,     TRUE, NULL,
  55. #ifdef DEBUG
  56.     "  Debug  ", Debug,     TRUE, NULL,
  57. #endif
  58. };
  59.  
  60. static void create_board (), create_tiles ();
  61. BOARD_POS board[MAX_ROWS][MAX_COLS];
  62. PLAYER players[2];
  63.  
  64. static void Usage()
  65. {
  66.     fprintf (stderr, "Usage: xstratego opponent@machine <Your name>.\n");
  67.     fprintf (stderr, "       or xstratego -s to set up a board.\n");
  68.     exit (1);
  69. }
  70.  
  71. main (argc, argv)
  72. int argc;
  73. char *argv[];
  74. {
  75.     Arg args[2];
  76.     char *at_sym;
  77.     char name_buf[100];
  78.     int i;
  79.  
  80.     Toplevel = XtAppInitialize (&app_con, "Xstratego", NULL, ZERO,
  81.                     &argc, argv, fallback_resources, NULL,ZERO);
  82.  
  83.     if (argc == 2 && !strcmp (argv[1], "-s")) {
  84.         strcpy (players[0].name, "Nobody");
  85.         players[0].setup_only = TRUE;
  86.  
  87.         for (i = 0; i < XtNumber(setup_buttons); i++) {
  88.             if (setup_buttons[i].callback == Ready || 
  89.                           setup_buttons[i].callback == Mail)
  90.                 setup_buttons[i].sensitive = FALSE;
  91.         }
  92.     }
  93.     else if (argc == 3 && (at_sym = strchr (argv[1], '@')) != NULL) {
  94.         *at_sym++ = '\0';
  95.         setup_communication (argv[2], argv[1], at_sym);
  96.     }
  97.     else
  98.         Usage ();
  99.  
  100.     Stratego = XtCreateManagedWidget ("Stratego", formWidgetClass, Toplevel,
  101.                       NULL, ZERO);
  102.  
  103.     XtSetArg (args[0], XtNlabel, "  ");
  104.     Message = XtCreateManagedWidget ("Message", labelWidgetClass, Stratego,
  105.                      args, 1);
  106.  
  107.     init_icons ();
  108.     init_board ();
  109.     init_dialog ();
  110.     init_actions ();
  111.  
  112.     create_board (Stratego);
  113.     create_tiles (Stratego);
  114.  
  115.     new_button_box (setup_buttons, XtNumber(setup_buttons));
  116.     
  117.     XtSetArg (args[0], XtNfromHoriz, NULL);
  118.     XtSetArg (args[1], XtNfromVert, NULL);
  119.     XtSetValues (Board, args, 2);
  120.  
  121.     XtSetArg (args[0], XtNfromHoriz, Board);
  122.     XtSetArg (args[1], XtNfromVert, NULL);
  123.     XtSetValues (Tiles, args, 2);
  124.  
  125.     XtSetArg (args[0], XtNfromHoriz, NULL);
  126.     XtSetArg (args[1], XtNfromVert, Board);
  127.     XtSetValues (Message, args, 2);
  128.  
  129.     if (players[0].setup_only == FALSE) {
  130.         sprintf (name_buf, "Stratego (%s) [%s@%s.%s]", players[0].name,
  131.              players[0].login, players[0].machine,
  132.              players[0].domain);
  133.         EnemyID = XtAppAddInput (app_con, players[1].fd,
  134.                      XtInputReadMask,
  135.                      EnemyInterrupt, NULL);
  136.     }
  137.     else
  138.         strcpy (name_buf, "Stratego (Setup Board)");
  139.  
  140.     XtSetArg (args[0], XtNtitle, name_buf);
  141.     XtSetValues (Toplevel, args, 1);
  142.  
  143.     XtRealizeWidget (Toplevel);
  144.     XtAppMainLoop (app_con);
  145. }
  146.  
  147. static void CaughtByHook()
  148. {
  149.     /* Action already done by action hook proecedure "ButtonPressHook" */
  150. }
  151.  
  152. static XtActionsRec default_actions[] = {
  153.     "CaughtByHook",    CaughtByHook
  154. };
  155.  
  156. static void init_actions ()
  157. {
  158.     XtAppAddActions (app_con, default_actions, XtNumber (default_actions));
  159.     XtAppAddActionHook (app_con, ButtonPressHook, NULL);
  160. }
  161.  
  162. static Widget Underneath (y, x)
  163. int y, x;
  164. {
  165.     if (y == 0)
  166.         return NULL;
  167.     
  168.     if (y == 6 && (x == 2 || x == 3 || x == 6 || x == 7))
  169.         return board[4][x & ~0x01].widget;
  170.     
  171.     return board[y - 1][x].widget;
  172. }
  173.  
  174. static Widget Nextto (y, x)
  175. int y, x;
  176. {
  177.     if (x == 0)
  178.         return NULL;
  179.     
  180.     if ((y == 4 || y == 5) && (x == 4 || x == 8))
  181.         return board[4][x - 2].widget;
  182.     
  183.     return board[y][x - 1].widget;
  184. }
  185.  
  186. static void create_water (parent, y, x)
  187. Widget parent;
  188. int y, x;
  189. {
  190.     Dimension wg_width, wg_height, wg_borderwidth;
  191.     int df_distance, height, width;
  192.     Arg args[5];
  193.  
  194.     XtSetArg (args[0], XtNdefaultDistance, &df_distance);
  195.     XtGetValues (parent, args, 1);
  196.  
  197.     XtSetArg (args[0], XtNwidth, &wg_width);
  198.     XtSetArg (args[1], XtNheight, &wg_height);
  199.     XtSetArg (args[2], XtNborderWidth, &wg_borderwidth);
  200.     XtGetValues (board[0][0].widget, args, 3);
  201.  
  202.     width  = df_distance + 2 * (wg_width  + wg_borderwidth);
  203.     height = df_distance + 2 * (wg_height + wg_borderwidth);
  204.  
  205.     XtSetArg (args[0], XtNwidth, width);
  206.     XtSetArg (args[1], XtNheight, height);
  207.     XtSetArg (args[2], XtNcursor, tiles[BOAT_CURSOR].cursor);
  208.     XtSetArg (args[3], XtNfromVert, board[y - 1][x].widget);
  209.     XtSetArg (args[4], XtNfromHoriz, board[y][x - 1].widget);
  210.  
  211.     board[y][x].widget = XtCreateManagedWidget ("Water", commandWidgetClass,
  212.                             parent, args, 5);
  213.  
  214.     XtAddCallback (board[y][x].widget, XtNcallback,
  215.                ActivateBoardPosition, &board[y][x]);
  216. }
  217.  
  218. static void create_board (parent)
  219. Widget parent;
  220. {
  221.     Arg args[4];
  222.     int x, y;
  223.  
  224.     Board = XtCreateManagedWidget ("Board", formWidgetClass, parent,
  225.                        NULL, ZERO);
  226.  
  227.     XtSetArg (args[0], XtNbitmap, tiles[FREE].pixmap);
  228.     XtSetArg (args[1], XtNcursor, tiles[BOARD_CURSOR].cursor);
  229.     for (y = 0; y < MAX_ROWS; y++) {
  230.         for (x = 0; x < MAX_COLS; x++) {
  231.             if (board[y][x].value == WATER) {
  232.                 if (y == 4 && (x == 2 || x == 6))
  233.                     create_water (Board, y, x);
  234.                 continue;
  235.             }
  236.  
  237.             XtSetArg (args[2], XtNfromVert, Underneath (y, x));
  238.             XtSetArg (args[3], XtNfromHoriz, Nextto (y, x));
  239.             board[y][x].widget = XtCreateManagedWidget (
  240.                         "BoardPosition",
  241.                          commandWidgetClass,
  242.                          Board, args, 4);
  243.  
  244.             XtAddCallback (board[y][x].widget, XtNcallback,
  245.                        ActivateBoardPosition, &board[y][x]);
  246.  
  247.             board[y][x].y = y;
  248.             board[y][x].x = x;
  249.         }
  250.     }
  251. }
  252.  
  253. static void create_tiles (parent)
  254. Widget parent;
  255. {
  256.     Arg box_args[3], command_args[2], label_args[1];
  257.     int i;
  258.  
  259.     Tiles = XtCreateManagedWidget ("Tiles", formWidgetClass, parent,
  260.                        NULL, ZERO);
  261.  
  262.     XtSetArg (box_args[0],    XtNcursor, tiles[ACCOUNT_CURSOR].cursor);
  263.     XtSetArg (command_args[0], XtNcursor, tiles[ACCOUNT_CURSOR].cursor);
  264.  
  265.     for (i = FLAG; i <= BOMB; i++) {
  266.         XtSetArg (box_args[1], XtNfromVert,
  267.               (i == FLAG || i == LIEUTENANT) ? NULL : 
  268.                         tiles[i - 1].box_widget);
  269.         XtSetArg (box_args[2], XtNfromHoriz,
  270.               (i < LIEUTENANT) ? NULL :
  271.                 tiles[i - (LIEUTENANT - FLAG)].box_widget);
  272.         tiles[i].box_widget = XtCreateManagedWidget ("TilePosition",
  273.                                  boxWidgetClass,
  274.                                  Tiles, box_args,3);
  275.  
  276.         XtSetArg (command_args[1], XtNbitmap, tiles[i].pixmap);
  277.         tiles[i].bitmap_widget = XtCreateManagedWidget ("TileBitmap",
  278.                         commandWidgetClass,
  279.                         tiles[i].box_widget,
  280.                         command_args, 2);
  281.         XtAddCallback (tiles[i].bitmap_widget, XtNcallback, 
  282.                    SelectSetupBoardCursor, &tiles[i]);
  283.  
  284.         XtSetArg (label_args[0], XtNlabel, "   ");
  285.         tiles[i].count_widget = XtCreateManagedWidget ("TileCount",
  286.                           labelWidgetClass, 
  287.                           tiles[i].box_widget,
  288.                           label_args, 1);
  289.         UpdateTileCount (&tiles[i], 0);
  290.     }
  291. }
  292.  
  293. void new_button_box (list, nr_buttons)
  294. BUTTON *list;
  295. int nr_buttons;
  296. {
  297.     static BUTTON *prev_set;
  298.     static int prev_nr_buttons;
  299.     static Widget ButtonSet;
  300.     Arg args[3];
  301.     int i, split;
  302.  
  303.     if (prev_set != (BUTTON *) 0) {
  304.         for (i = 0; i < prev_nr_buttons; i++) {
  305.             XtUnmanageChild (prev_set[i].widget);
  306.             XtDestroyWidget (prev_set[i].widget);
  307.         }
  308.     }
  309.     else {
  310.         XtSetArg (args[0], XtNfromHoriz, Board);
  311.         XtSetArg (args[1], XtNfromVert,  Tiles);
  312.         ButtonSet = XtCreateManagedWidget ("ButtonSet", formWidgetClass,
  313.                            Stratego, args, 2);
  314.     }
  315.  
  316.     prev_set = list;
  317.     prev_nr_buttons = nr_buttons;
  318.  
  319.     split = (nr_buttons + 1) / 2;
  320.     for (i = 0; i < nr_buttons; i++) {
  321.         XtSetArg (args[0], XtNlabel, list[i].label);
  322.         XtSetArg (args[1], XtNfromHoriz,
  323.                    (i < split) ? NULL : list[i - split].widget);
  324.         XtSetArg (args[2], XtNfromVert,
  325.                    (i % split) ? list[i - 1].widget : NULL);
  326.         list[i].widget = XtCreateManagedWidget ("Button",
  327.                             commandWidgetClass,
  328.                             ButtonSet, args, 3);
  329.         if (list[i].callback)
  330.             XtAddCallback (list[i].widget, XtNcallback, 
  331.                        list[i].callback, NULL);
  332.         if (list[i].sensitive == FALSE)
  333.             XtSetSensitive(list[i].widget, FALSE);
  334.     }
  335. }
  336.