home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Usenet 1994 January
/
usenetsourcesnewsgroupsinfomagicjanuary1994.iso
/
sources
/
games
/
volume15
/
xstratego
/
part01
/
setup.c
< prev
next >
Wrap
C/C++ Source or Header
|
1993-01-27
|
8KB
|
388 lines
/*
* Setup.c
*/
#include "stratego.h"
#include <sys/types.h>
#include <sys/stat.h>
/* HJV: Normally found in stdlib.h, but some systems do not
have this include file, or have it at another location.
Hoping they will not change the value of F_OK, I hereby
declare it as zero, as it should be.
*/
#ifndef F_OK
#define F_OK 0
#endif
static void SelectBoardCursor ();
/*
* Setup buttonbox Callback routines.
*/
/*
* WipeCursor Button Callback Routine.
*/
XtCallbackProc WipeCursor (widget)
Widget widget;
{
SelectBoardCursor (&tiles[FREE]);
}
/*
* Clear Button Callback Routine.
*/
XtCallbackProc Clear (widget)
Widget widget;
{
int y, x;
for (y = MAX_ROWS - 4; y < MAX_ROWS; y++) {
for (x = 0; x < MAX_COLS; x++) {
if (board[y][x].value != FREE) {
UpdateTileCount (&tiles[board[y][x].value],
1);
AssignBoardPosition (&board[y][x], &tiles[FREE],
O_NOBODY);
}
}
}
send_clear_board ();
SelectBoardCursor (&tiles[FREE]);
}
/*
* Message Button Callback Routine.
*/
XtCallbackProc Mail (widget)
Widget widget;
{
char msg_buf[100];
msg_buf[0] = '\0';
if (DialogBoxResult ("Enter Message", msg_buf, 80, ADD_CANCEL) == True)
send_mail (msg_buf);
}
/*
* Ready Button Callback Routine.
*/
static BUTTON ready_buttons[] = {
" Message ", Mail, TRUE, NULL,
" Quit ", Quit, TRUE, NULL,
};
XtCallbackProc Ready (widget)
Widget widget;
{
char msg_buf[100];
int i;
for (i = FLAG; i <= BOMB; i++) {
if (tiles[i].count) {
display_error (finished_premature);
return;
}
}
sprintf (msg_buf, "You will no longer be able to change your setup.");
if (DialogBoxResult (msg_buf, NULL, 0, ADD_CANCEL) == True) {
players[0].status = PL_SETUP_READY;
send_ready ();
if (players[1].status != PL_SETUP_READY) {
message ("Waiting for %s to finish his setup...",
players[1].name);
new_button_box (ready_buttons, XtNumber(ready_buttons));
}
else
start_play ();
}
}
/*
* Quit Button Callback Routine.
*/
XtCallbackProc Quit (widget)
Widget widget;
{
if (DialogBoxResult ("Please Confirm Quit", NULL, 0,
ADD_CANCEL) == True) {
send_quit ();
XtDestroyApplicationContext (app_con);
exit (0);
}
}
/*
* Store Button Callback Routine.
*/
static char filename[NAME_LENGTH] = "board.dat";
XtCallbackProc Store (widget)
Widget widget;
{
char file_buf[80];
char msg_buf[100];
FILE *fp;
int x, y;
strcpy (file_buf, filename);
if (DialogBoxResult ("Enter Filename to Store", file_buf, 50,
ADD_CANCEL) == False)
return;
if (access (file_buf, F_OK) == 0) {
sprintf (msg_buf, "\"%s\" already exists. Overwrite?",file_buf);
if (DialogBoxResult (msg_buf, NULL, 0, ADD_CANCEL) == False)
return;
}
if ((fp = fopen (file_buf, "w")) == NULL) {
message ("Cannot open datafile \"%s\".", file_buf);
return;
}
strcpy (filename, file_buf);
for (y = 6; y < MAX_ROWS; y++)
for (x = 0; x < MAX_COLS; x++)
fprintf (fp, "%d ", board[y][x].value);
fclose (fp);
chmod (filename, 0600);
message ("Board setup saved in \"%s\".", filename);
}
/*
* Load Button Callback Routine.
*/
XtCallbackProc Load (widget)
Widget widget;
{
char file_buf[80];
FILE *fp;
int y, x, tile_value;
TILE *tile;
BOARD_POS *board_pos;
Boolean failed = FALSE;
strcpy (file_buf, filename);
if (DialogBoxResult ("Enter Filename to Load", file_buf, 50,
ADD_CANCEL) == False)
return;
strcpy (filename, file_buf);
if ((fp = fopen (filename, "r")) == NULL) {
extern int errno, sys_nerr;
extern char *sys_errlist[];
message ("Cannot open datafile \"%s\": %s", filename,
(errno < 0 || errno >= sys_nerr) ? "Unknown error" :
sys_errlist[errno]);
return;
}
Clear (widget, (XtPointer) NULL, (XtPointer) NULL);
for (y = 6; y < MAX_ROWS && failed == False; y++) {
for (x = 0; x < MAX_COLS && failed == False; x++) {
if (fscanf (fp, "%d ", &tile_value) != 1) {
message ("Datafile damaged!");
failed = True;
}
else if (tile_value >= FLAG && tile_value <= BOMB) {
board_pos = &board[y][x];
tile = &tiles[tile_value];
if (tile->count == 0) {
message ("Hmmm, seems like a cheat file to me %s!", players[0].name);
failed = True;
}
AssignBoardPosition (board_pos, tile, O_PLAYER);
UpdateTileCount (tile, -1);
send_tile_placed (board_pos);
}
else if (tile_value != 0) {
message ("Datafile damaged!");
failed = True;
}
}
}
fclose (fp);
if (failed == True)
Clear (widget, (XtPointer) NULL, (XtPointer) NULL);
else
message ("Board setup loaded from \"%s\".", filename);
}
/*
* TileBitmap widget class callback routines.
*/
static void SelectBoardCursor (tile)
TILE *tile;
{
int y, x, limit;
Arg args[1];
if (players[0].status == PL_ENDGAME) { /* Opponent quitted the game */
message ("The game has ended!");
return;
}
limit = (players[0].status == PL_SETUP) ? MAX_ROWS - 4 : 0;
XtSetArg (args[0], XtNcursor, tile->cursor);
for (y = MAX_ROWS - 1; y >= limit; y--)
for (x = MAX_COLS - 1; x >= 0; x--)
XtSetValues (board[y][x].widget, args, 1);
players[0].tile_select = tile->value;
}
XtCallbackProc SelectSetupBoardCursor (widget, tile)
Widget widget;
TILE *tile;
{
if (players[0].status != PL_SETUP)
return;
if (tile->count == 0) {
message ("You have no more %ss left!", tile->name);
return;
}
SelectBoardCursor (tile);
}
/*
* Called from callback routine of BoardPosition widget class.
*/
void setup_tile (board_pos)
BOARD_POS *board_pos;
{
TILE *tile;
if (board_pos->value == WATER) {
message ("Splash blup blup.");
return;
}
if (board_pos->y < MAX_ROWS - 4) {
message ("You cannot put a tile on enemy territory.");
return;
}
if (board_pos->value != FREE) {
UpdateTileCount (&tiles[board_pos->value], 1);
AssignBoardPosition (board_pos, &tiles[FREE], O_NOBODY);
send_tile_placed (board_pos);
if (players[0].tile_select == FREE)
return; /* Erasing */
}
tile = &tiles[players[0].tile_select];
if (tile->value < FLAG || tile->value > BOMB) {
/* No tile selected */
message ("You must select a tile first.");
return;
}
AssignBoardPosition (board_pos, tile, O_PLAYER);
UpdateTileCount (tile, -1);
send_tile_placed (board_pos);
if (tile->count == 0)
SelectBoardCursor (&tiles[FREE]);
}
#ifdef DEBUG
XtCallbackProc Debug ()
{
char file_buf[80];
FILE *fp;
int y, x, tile_value;
TILE *tile;
BOARD_POS *board_pos;
Boolean failed = FALSE;
char msg_buf[100];
int i;
char filename[100];
strcpy (filename, "board.dat");
if (DialogBoxResult ("Enter file", filename, sizeof(filename), ADD_CANCEL) == False)
return;
if ((fp = fopen (filename, "r")) == NULL) {
extern int errno, sys_nerr;
extern char *sys_errlist[];
message ("Cannot open datafile \"%s\": %s", filename,
(errno < 0 || errno >= sys_nerr) ? "Unknown error" :
sys_errlist[errno]);
return;
}
Clear ();
for (y = 6; y < MAX_ROWS && failed == False; y++) {
for (x = 0; x < MAX_COLS && failed == False; x++) {
if (fscanf (fp, "%d ", &tile_value) != 1) {
message ("Datafile damaged!");
failed = True;
}
else if (tile_value >= FLAG && tile_value <= BOMB) {
board_pos = &board[y][x];
tile = &tiles[tile_value];
if (tile->count == 0) {
message ("Hmmm, seems like a cheat file to me %s!", players[0].name);
failed = True;
}
AssignBoardPosition (board_pos, tile, O_PLAYER);
UpdateTileCount (tile, -1);
send_tile_placed (board_pos);
}
else if (tile_value != 0) {
message ("Datafile damaged!");
failed = True;
}
}
}
fclose (fp);
if (failed == True)
Clear ();
else
message ("Board setup loaded from \"%s\".", filename);
players[0].status = PL_SETUP_READY;
send_ready ();
if (players[1].status != PL_SETUP_READY) {
message ("Waiting for %s to finish his setup...",
players[1].name);
}
else
start_play ();
}
#endif