home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Usenet 1994 January
/
usenetsourcesnewsgroupsinfomagicjanuary1994.iso
/
sources
/
games
/
volume15
/
xstratego
/
part01
/
main.c
< prev
next >
Wrap
C/C++ Source or Header
|
1993-01-27
|
9KB
|
336 lines
/*
* Main.c
*/
#include "stratego.h"
XtAppContext app_con;
XtInputId EnemyID;
Widget Toplevel, Stratego, Board, Tiles, Message;
static void init_actions ();
void new_button_box ();
static String fallback_resources[] = {
"*PopupShell.allowShellResize: True",
"*PopupShell.borderWidth: 4",
"*PopupShell.Dialog*shapeStyle: oval",
"*PopupShell.Dialog.value.translations: #override \\n \
<Key>Return: DialogOk()",
"*Board.borderWidth: 4",
"*Board.defaultDistance: 1",
"*Water.highlightThickness: 0",
"*BoardPosition.highlightThickness: 0",
"*Tiles.borderWidth: 2",
"*Tiles.defaultDistance: 2",
"*TilePosition.orientation: horizontal",
"*TileBitmap.borderWidth: 0",
"*TileBitmap.highlightThickness: 1",
"*ButtonSet.borderWidth: 0",
"*ButtonSet.defaultDistance: 10",
"*Button.shapeStyle: oval",
"*Message.resizable: True",
"*Message.borderWidth: 0",
"*translations: #augment \\n \
<Btn2Down>: CaughtByHook() \\n \
<Btn3Down>: CaughtByHook()",
NULL
};
static BUTTON setup_buttons[] = {
" Cursor ", WipeCursor, TRUE, NULL,
" Load ", Load, TRUE, NULL,
" Ready ", Ready, TRUE, NULL,
" Message ", Mail, TRUE, NULL,
" Clear ", Clear, TRUE, NULL,
" Store ", Store, TRUE, NULL,
" Quit ", Quit, TRUE, NULL,
#ifdef DEBUG
" Debug ", Debug, TRUE, NULL,
#endif
};
static void create_board (), create_tiles ();
BOARD_POS board[MAX_ROWS][MAX_COLS];
PLAYER players[2];
static void Usage()
{
fprintf (stderr, "Usage: xstratego opponent@machine <Your name>.\n");
fprintf (stderr, " or xstratego -s to set up a board.\n");
exit (1);
}
main (argc, argv)
int argc;
char *argv[];
{
Arg args[2];
char *at_sym;
char name_buf[100];
int i;
Toplevel = XtAppInitialize (&app_con, "Xstratego", NULL, ZERO,
&argc, argv, fallback_resources, NULL,ZERO);
if (argc == 2 && !strcmp (argv[1], "-s")) {
strcpy (players[0].name, "Nobody");
players[0].setup_only = TRUE;
for (i = 0; i < XtNumber(setup_buttons); i++) {
if (setup_buttons[i].callback == Ready ||
setup_buttons[i].callback == Mail)
setup_buttons[i].sensitive = FALSE;
}
}
else if (argc == 3 && (at_sym = strchr (argv[1], '@')) != NULL) {
*at_sym++ = '\0';
setup_communication (argv[2], argv[1], at_sym);
}
else
Usage ();
Stratego = XtCreateManagedWidget ("Stratego", formWidgetClass, Toplevel,
NULL, ZERO);
XtSetArg (args[0], XtNlabel, " ");
Message = XtCreateManagedWidget ("Message", labelWidgetClass, Stratego,
args, 1);
init_icons ();
init_board ();
init_dialog ();
init_actions ();
create_board (Stratego);
create_tiles (Stratego);
new_button_box (setup_buttons, XtNumber(setup_buttons));
XtSetArg (args[0], XtNfromHoriz, NULL);
XtSetArg (args[1], XtNfromVert, NULL);
XtSetValues (Board, args, 2);
XtSetArg (args[0], XtNfromHoriz, Board);
XtSetArg (args[1], XtNfromVert, NULL);
XtSetValues (Tiles, args, 2);
XtSetArg (args[0], XtNfromHoriz, NULL);
XtSetArg (args[1], XtNfromVert, Board);
XtSetValues (Message, args, 2);
if (players[0].setup_only == FALSE) {
sprintf (name_buf, "Stratego (%s) [%s@%s.%s]", players[0].name,
players[0].login, players[0].machine,
players[0].domain);
EnemyID = XtAppAddInput (app_con, players[1].fd,
XtInputReadMask,
EnemyInterrupt, NULL);
}
else
strcpy (name_buf, "Stratego (Setup Board)");
XtSetArg (args[0], XtNtitle, name_buf);
XtSetValues (Toplevel, args, 1);
XtRealizeWidget (Toplevel);
XtAppMainLoop (app_con);
}
static void CaughtByHook()
{
/* Action already done by action hook proecedure "ButtonPressHook" */
}
static XtActionsRec default_actions[] = {
"CaughtByHook", CaughtByHook
};
static void init_actions ()
{
XtAppAddActions (app_con, default_actions, XtNumber (default_actions));
XtAppAddActionHook (app_con, ButtonPressHook, NULL);
}
static Widget Underneath (y, x)
int y, x;
{
if (y == 0)
return NULL;
if (y == 6 && (x == 2 || x == 3 || x == 6 || x == 7))
return board[4][x & ~0x01].widget;
return board[y - 1][x].widget;
}
static Widget Nextto (y, x)
int y, x;
{
if (x == 0)
return NULL;
if ((y == 4 || y == 5) && (x == 4 || x == 8))
return board[4][x - 2].widget;
return board[y][x - 1].widget;
}
static void create_water (parent, y, x)
Widget parent;
int y, x;
{
Dimension wg_width, wg_height, wg_borderwidth;
int df_distance, height, width;
Arg args[5];
XtSetArg (args[0], XtNdefaultDistance, &df_distance);
XtGetValues (parent, args, 1);
XtSetArg (args[0], XtNwidth, &wg_width);
XtSetArg (args[1], XtNheight, &wg_height);
XtSetArg (args[2], XtNborderWidth, &wg_borderwidth);
XtGetValues (board[0][0].widget, args, 3);
width = df_distance + 2 * (wg_width + wg_borderwidth);
height = df_distance + 2 * (wg_height + wg_borderwidth);
XtSetArg (args[0], XtNwidth, width);
XtSetArg (args[1], XtNheight, height);
XtSetArg (args[2], XtNcursor, tiles[BOAT_CURSOR].cursor);
XtSetArg (args[3], XtNfromVert, board[y - 1][x].widget);
XtSetArg (args[4], XtNfromHoriz, board[y][x - 1].widget);
board[y][x].widget = XtCreateManagedWidget ("Water", commandWidgetClass,
parent, args, 5);
XtAddCallback (board[y][x].widget, XtNcallback,
ActivateBoardPosition, &board[y][x]);
}
static void create_board (parent)
Widget parent;
{
Arg args[4];
int x, y;
Board = XtCreateManagedWidget ("Board", formWidgetClass, parent,
NULL, ZERO);
XtSetArg (args[0], XtNbitmap, tiles[FREE].pixmap);
XtSetArg (args[1], XtNcursor, tiles[BOARD_CURSOR].cursor);
for (y = 0; y < MAX_ROWS; y++) {
for (x = 0; x < MAX_COLS; x++) {
if (board[y][x].value == WATER) {
if (y == 4 && (x == 2 || x == 6))
create_water (Board, y, x);
continue;
}
XtSetArg (args[2], XtNfromVert, Underneath (y, x));
XtSetArg (args[3], XtNfromHoriz, Nextto (y, x));
board[y][x].widget = XtCreateManagedWidget (
"BoardPosition",
commandWidgetClass,
Board, args, 4);
XtAddCallback (board[y][x].widget, XtNcallback,
ActivateBoardPosition, &board[y][x]);
board[y][x].y = y;
board[y][x].x = x;
}
}
}
static void create_tiles (parent)
Widget parent;
{
Arg box_args[3], command_args[2], label_args[1];
int i;
Tiles = XtCreateManagedWidget ("Tiles", formWidgetClass, parent,
NULL, ZERO);
XtSetArg (box_args[0], XtNcursor, tiles[ACCOUNT_CURSOR].cursor);
XtSetArg (command_args[0], XtNcursor, tiles[ACCOUNT_CURSOR].cursor);
for (i = FLAG; i <= BOMB; i++) {
XtSetArg (box_args[1], XtNfromVert,
(i == FLAG || i == LIEUTENANT) ? NULL :
tiles[i - 1].box_widget);
XtSetArg (box_args[2], XtNfromHoriz,
(i < LIEUTENANT) ? NULL :
tiles[i - (LIEUTENANT - FLAG)].box_widget);
tiles[i].box_widget = XtCreateManagedWidget ("TilePosition",
boxWidgetClass,
Tiles, box_args,3);
XtSetArg (command_args[1], XtNbitmap, tiles[i].pixmap);
tiles[i].bitmap_widget = XtCreateManagedWidget ("TileBitmap",
commandWidgetClass,
tiles[i].box_widget,
command_args, 2);
XtAddCallback (tiles[i].bitmap_widget, XtNcallback,
SelectSetupBoardCursor, &tiles[i]);
XtSetArg (label_args[0], XtNlabel, " ");
tiles[i].count_widget = XtCreateManagedWidget ("TileCount",
labelWidgetClass,
tiles[i].box_widget,
label_args, 1);
UpdateTileCount (&tiles[i], 0);
}
}
void new_button_box (list, nr_buttons)
BUTTON *list;
int nr_buttons;
{
static BUTTON *prev_set;
static int prev_nr_buttons;
static Widget ButtonSet;
Arg args[3];
int i, split;
if (prev_set != (BUTTON *) 0) {
for (i = 0; i < prev_nr_buttons; i++) {
XtUnmanageChild (prev_set[i].widget);
XtDestroyWidget (prev_set[i].widget);
}
}
else {
XtSetArg (args[0], XtNfromHoriz, Board);
XtSetArg (args[1], XtNfromVert, Tiles);
ButtonSet = XtCreateManagedWidget ("ButtonSet", formWidgetClass,
Stratego, args, 2);
}
prev_set = list;
prev_nr_buttons = nr_buttons;
split = (nr_buttons + 1) / 2;
for (i = 0; i < nr_buttons; i++) {
XtSetArg (args[0], XtNlabel, list[i].label);
XtSetArg (args[1], XtNfromHoriz,
(i < split) ? NULL : list[i - split].widget);
XtSetArg (args[2], XtNfromVert,
(i % split) ? list[i - 1].widget : NULL);
list[i].widget = XtCreateManagedWidget ("Button",
commandWidgetClass,
ButtonSet, args, 3);
if (list[i].callback)
XtAddCallback (list[i].widget, XtNcallback,
list[i].callback, NULL);
if (list[i].sensitive == FALSE)
XtSetSensitive(list[i].widget, FALSE);
}
}