home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Usenet 1994 October
/
usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso
/
games
/
volume8
/
mgt
/
part01
/
ascii.c
next >
Wrap
C/C++ Source or Header
|
1990-02-23
|
11KB
|
554 lines
/*
"mgt" Copyright 1990 Shodan
All Rights Reserved.
Program by Greg Hale
Permission to use, copy, modify, and distribute this software and its
documentation for any purpose and without fee is hereby granted,
provided that this entire comment and copyright notice appear in all
copies and that both that copyright notice and this permission notice
appear in supporting documentation. No representations are made about
the suitability of this software for any purpose. It is provided "as
is" without express or implied warranty.
Please send copies of extensions to:
hale@scam.berkeley.edu
128.32.138.4 scam.berkeley.edu sting sting.Berkeley.EDU
Donations for the 'From My Go Teacher' series may be sent to:
Shodan
P.O. Box 4456
Berkeley, CA 94704
(415) 849-9475
*/
#include <sys/time.h>
#include <curses.h>
#include <ctype.h>
#include "mgt.h"
#include "ascii.h"
#include "var.h"
#include "file.h"
#define TOP 2
#define LEFT 4
#define ASCII_ENV_STR "_ASCII="
#define COMMENT_X 46
#define COMMENT_Y 1
#define COMMENT_WIDE (79-COMMENT_X)
#define COMMENT_HIGH (14-COMMENT_Y)
#define TREE_X COMMENT_X
#define TREE_Y 15
#define TREEWIDE (79-TREE_X)
#define TREEHIGH 6
#define COMMAND_X TREE_X
#define COMMAND_Y TREE_Y+TREEHIGH
enum { s_comment };
boolean commentExists;
int commentLine;
int treeLine;
char envAscii[]="q><.,ebcx][gnpui@O.+";
char *xAxisChars = "ABCDEFGHJKLMNOPQRSTUVWXYZ";
FUNCTION void printCmdSet()
{
commandIndex i;
mvaddstr(COMMAND_Y,COMMAND_X,"Commands:");
for (i=ASC_QUIT; i!= ASC_CHAR_BLACK; i++) {
mvaddch(COMMAND_Y,i+COMMAND_X+sizeof("Commands"),envAscii[i]);
}
refresh();
}
FUNCTION int notifyMessageAscii(s)
char *s;
{
mvaddstr(23,1,s);
refresh();
}
FUNCTION int notifyClearAscii()
{
move(23,1);
clrtoeol();
}
FUNCTION int getLine(query,dst,maxLen) /* char *, char *, int */
char *query, *dst;
int maxLen;
{
int qLen, dLen;
char c;
mvaddstr(23,1,query);
clrtoeol();
dLen = 0;
qLen = strlen(query);
do {
move(23,qLen+1+dLen);
refresh();
c = getKey();
if (isprint(c) && dLen < maxLen) {
mvaddch(23,qLen+dLen+1,c);
dst[dLen++] = c;
} else if (dLen && (c=='\b' || c=='\177')) {
dLen--;
mvaddch(23,qLen+dLen+1,' ');
}
} while (c != '\n' && c != '\r');
dst[dLen]=0;
move(23,1);
clrtoeol();
return dLen;
}
/*
* draw the tree children when node currently is at n
*/
FUNCTION int drawTreeAscii(n)
nodep n;
{
treeLine = 0;
drawTreeAscii0(n);
}
FUNCTION void printNStr(s,n)
char *s;
int n;
{
int i;
for (i=0; i<n && s[i]; i++)
addch(s[i]);
}
FUNCTION int drawTreeAscii0(n)
nodep n;
{
property *p;
move(TREE_Y,TREE_X+1);
printw(" Node #%d:",n->nodeNum);
if (p = getprop(n,t_Name))
printNStr(p->d, 22);
clrtoeol();
{
int temp;
if (treeLine < 0)
treeLine = 0;
else {
temp = treeCountSiblings(n) - TREEHIGH+1;
temp = MAX(temp,0);
treeLine = treeLine > temp ? temp : treeLine;
}
}
{
nodep ch;
int index;
index = 0;
ch = nthChild(n, treeLine, false);
while (ch && index < TREEHIGH-1) {
move(index+TREE_Y+1,TREE_X);
printw("%c:",'A'+ index + treeLine);
clrtoeol();
if (p = getprop(ch,t_Name))
printNStr(p->d, 80-(TREE_X+3));
ch = ch->nextSibling;
index++;
}
while(index<TREEHIGH-1) {
move(index++ + TREE_Y+1,TREE_X);
clrtoeol();
}
mvaddch(TREE_Y+TREEHIGH-1,TREE_X-1,ch ? '+' : ' ');
mvaddch(TREE_Y+1,TREE_X-1,treeLine ? '-' : ' ');
}
refresh();
}
FUNCTION int depthAscii(d)
depthrec *d;
{
#if 0
int i,lim;
/* lim = d->depth > DEPTH_LIMIT-1 ? DEPTH_LIMIT-1 : d->depth;
move(INDEXLINE,0);
clrtoeol();
move(INDEXLINE,0);
for (i=0 ; i<=lim ; i++) {
printw("%d.",(int)d->d[i]);
}
refresh(); */
#endif
}
FUNCTION int initAscii(i)
interface *i;
{
initscr();
noecho();
crmode();
}
FUNCTION int closeAscii()
{
clear();
refresh();
endwin();
}
FUNCTION int refreshAscii()
{
refresh();
}
FUNCTION char boardPiece(x,y)
int x,y;
{
return ((x==3 || x==9 || x == 15) && (y==3 || y==9 || y==15))
? envAscii[(int)ASC_CHAR_HANDICAP] : envAscii[(int)ASC_CHAR_NOTHING];
}
FUNCTION int plotPieceAscii(b,i,j)
pBoard b;
int i,j;
{
piece p;
p = boardGet(b,i,j);
mvaddch(TOP+j, LEFT + (i) * 2, p == P_NOTHING ? boardPiece(i,j) :
(p == P_BLACK ? envAscii[(int)ASC_CHAR_BLACK] :
envAscii[(int)ASC_CHAR_WHITE]));
}
FUNCTION int highlightAscii(x,y,movenum,turn)
int x,y,movenum,turn;
{
if (movenum>0 && x>=0) {
mvprintw(23,1,"%s #%d at '%c%d' ",
turn ? "White" : "Black",
movenum,xAxisChars[x],19-y);
} else {
mvprintw(23,1," ");
}
move(TOP+y, LEFT+x*2);
refresh();
}
FUNCTION int setPieceAscii(b,i,j,p)
pBoard b;
int i,j;
piece p;
{
boardSet(b,i,j,p);
plotPieceAscii(b,i,j);
}
FUNCTION int showCommentAscii(i) /* display from line i onward */
int i;
{
short line;
line=MIN(commentLines()-i,COMMENT_HIGH);
/* move(line+COMMENT_Y,COMMENT_X);
clrtoeol(); */
mvaddch(COMMENT_Y,COMMENT_X-1,i ? '-' : ' ');
mvaddch(COMMENT_Y+COMMENT_HIGH-1,COMMENT_X-1,
commentLines()-i>COMMENT_HIGH? '+' : ' ');
while (line--) {
move(line+COMMENT_Y,COMMENT_X);
printw(commentGet(line+i));
clrtoeol();
}
refresh();
}
FUNCTION int clearCommentAscii()
{
commentExists = false;
{
int line;
for (line = COMMENT_Y; line < COMMENT_Y+COMMENT_HIGH; line++)
move(line,COMMENT_X),clrtoeol();
}
mvaddch(COMMENT_Y,COMMENT_X-1,' ');
mvaddch(COMMENT_Y+COMMENT_HIGH-1,COMMENT_X-1, ' ');
}
FUNCTION int displayCommentAscii(s)
char *s;
{
commentExists = true;
commentBuf = s;
commentLine = 0;
formatComment(s, COMMENT_WIDE);
showCommentAscii(0);
refresh();
}
FUNCTION int clearBoardAscii(b)
pBoard b;
{
int i,j;
clear();
for (i = boardsize; i--;) {
/* left */
mvprintw(TOP+i,LEFT-3,"%2d|",19-i);
/* right */
mvprintw(TOP+i,LEFT+boardsize*2-1,"|%2d|",19-i);
/* top */
mvaddch(TOP-2,LEFT+i*2,xAxisChars[i]);
mvprintw(TOP-1,LEFT+i*2-1,"--");
/* bottom */
mvaddch(TOP+boardsize+1,LEFT+i*2,xAxisChars[i]);
mvprintw(TOP+boardsize,LEFT+i*2-1,"--");
for (j = boardsize; j--;) {
setPieceAscii(b,i,j,P_NOTHING);
}
}
mvaddch(TOP-1,LEFT-1,'+');
mvaddch(TOP-1,LEFT+boardsize*2-1,'+');
mvaddch(TOP+boardsize,LEFT-1,'+');
mvaddch(TOP+boardsize,LEFT+boardsize*2-1,'+');
printCmdSet();
}
FUNCTION commandIndex charToIndex(c)
char c;
{
int i;
for (i=ASC_CHAR_BLACK; i--;) {
if (c == envAscii[i])
break;
}
return i; /* -1 = not found */
}
FUNCTION int idleAscii(curnode)
nodep curnode;
{
char c;
command r;
commandIndex i;
long mask;
struct timeval t;
t.tv_sec = 0;
t.tv_usec = 0;
mask = 1;
r = C_NOTHING;
highlightLast();
if (select(32,&mask,0,0,&t)>0) {
c = getKey();
if (c>='A' && c<='Z') {
r=(command)((char)C_CHOSECHILD+(c-'A'));
} else {
switch(charToIndex(c)) {
case ASC_QUIT:
{
char buf[5];
getLine("Quit (y/n)?",buf,1);
if (buf[0]=='y')
r = C_QUIT;
}
break;
case ASC_DOWN:
r = C_DOWN;
break;
case ASC_UP:
r = C_UP;
break;
case ASC_WALKDOWN:
r = C_WALKDOWN;
break;
case ASC_WALKUP:
r = C_WALKUP;
break;
case ASC_END:
r = C_END;
break;
case ASC_BEGINNING:
r = C_BEGINNING;
break;
case ASC_SEARCHCOMMENT:
r = C_SEARCHCOMMENT;
break;
case ASC_SEARCHBACKCOMMENT:
r = C_SEARCHBACKCOMMENT;
break;
case ASC_DOWNFORK:
r = C_DOWNFORK;
break;
case ASC_UPFORK:
r = C_UPFORK;
break;
case ASC_GOTO:
{
char buf[7];
getLine("Move to node # ?",buf,5);
searchNodeNum = atoi(buf);
if (searchNodeNum)
r = C_GOTO;
}
break;
case ASC_COMMENTSCROLLDOWN:
{
if (commentLine< commentLines() - COMMENT_HIGH)
commentLine++;
showCommentAscii(commentLine);
}
break;
case ASC_COMMENTSCROLLUP:
if (commentLine) {
commentLine--;
showCommentAscii(commentLine);
}
break;
case ASC_TREESCROLLDOWN:
treeLine++;
drawTreeAscii0(curnode);
break;
case ASC_TREESCROLLUP:
treeLine--;
drawTreeAscii0(curnode);
break;
default:
helpAscii();
break;
}
}
}
return (int)r;
}
FUNCTION boolean setEnvAscii(env)
char *env;
{
char *c;
if (!strncmp(env,ASCII_ENV_STR, strlen(ASCII_ENV_STR))) {
env += strlen(ASCII_ENV_STR);
strncpy(envAscii,env,ASC_NUMENVS);
return true;
} else {
return false;
}
}
FUNCTION helpMessage(s)
char *s;
{
notifyMessageAscii(s);
getKey();
}
FUNCTION helpAscii()
{
char buf[3];
do {
getLine("Help for what key (<return> stops)?",buf,2);
if (buf[0]) switch (charToIndex(buf[0])) {
case ASC_QUIT:
helpMessage("Quit mgt -more-");
break;
case ASC_DOWN:
helpMessage("Move forward -more-");
break;
case ASC_UP:
helpMessage("Move backward -more-");
break;
case ASC_WALKDOWN:
helpMessage("Walk forwards & visit variations -more-");
break;
case ASC_WALKUP:
helpMessage("Walk backwards & visit variations -more-");
break;
case ASC_END:
helpMessage("Go to the end of the current variation -more-");
break;
case ASC_BEGINNING:
helpMessage("Go to the beginning of all variations -more-");
break;
case ASC_SEARCHCOMMENT:
helpMessage("Walk forward until a comment is found -more-");
break;
case ASC_SEARCHBACKCOMMENT:
helpMessage("Walk backward until a comment is found -more-");
break;
case ASC_DOWNFORK:
helpMessage("Go forward until a variation appears -more-");
break;
case ASC_UPFORK:
helpMessage("Go backward until a variation appears -more-");
break;
case ASC_GOTO:
helpMessage("Jump to a specific node number -more-");
helpMessage("The number is next to 'Node #' -more-");
break;
case ASC_COMMENTSCROLLDOWN:
helpMessage("Scroll the comment window down -more-");
break;
case ASC_COMMENTSCROLLUP:
helpMessage("Scroll the comment window up -more-");
break;
case ASC_TREESCROLLDOWN:
helpMessage("Scroll the tree window down -more-");
break;
case ASC_TREESCROLLUP:
helpMessage("Scroll the tree window up -more-");
break;
default:
helpMessage("That does not exist. -more-");
helpMessage("Uppercase letters visit variations. -more-");
helpMessage("To the right, 'Commands' lists the key commands. -more-");
break;
}
} while (buf[0]);
}
interface asciiInterface = {
"Curses",
"c",
(char *)0,
initAscii,
closeAscii,
refreshAscii,
setPieceAscii,
plotPieceAscii,
displayCommentAscii,
clearCommentAscii,
clearBoardAscii,
idleAscii,
depthAscii,
drawTreeAscii,
highlightAscii,
setEnvAscii,
notifyMessageAscii,
notifyClearAscii
};