home *** CD-ROM | disk | FTP | other *** search
- /* misc.c 8/8/91
- *
- * Copyright 1991 Perry R. Ross
- *
- * Permission to use, copy, modify, and distribute this software and its
- * documentation without fee is hereby granted, subject to the restrictions
- * detailed in the README file, which is included here by reference.
- * Any other use requires written permission from the author. This software
- * is distributed "as is" without any warranty, including any implied
- * warranties of merchantability or fitness for a particular purpose.
- * The author shall not be liable for any damages resulting from the
- * use of this software. By using this software, the user agrees
- * to these terms.
- */
-
- #include "ldb.h"
-
-
- /*----------------------------------------------------------------------
- * rolldice -- roll two dice
- *
- * This function calls Rolldie twice and fills in the game structure
- * with the resulting values. If the two calls to Rolldie return the
- * same number, the mvs field is filled in so that the user has 4 rolls
- * to use, otherwise the rolls are stored in the first two elements
- * of the mvs field and the last two are marked unused.
- *----------------------------------------------------------------------
- */
-
- rolldice(g)
- struct game *g;
- {
-
- clearmvs(g->mvs); /* clear old stuff */
- g->mvs[0].roll = Rolldie(); /* roll the dice */
- g->mvs[1].roll = Rolldie();
- if (g->mvs[0].roll == g->mvs[1].roll) { /* hot damn, we got doubles */
- g->mvs[2].roll = g->mvs[0].roll; /* copy roll into two */
- g->mvs[3].roll = g->mvs[0].roll; /* more moves */
- }
- legalmoves(g); /* calculate the # of moves & hi roll */
- }
-
-
-
- /*----------------------------------------------------------------------
- * sendpkt -- send a packet to the opponent
- *
- * This function fills in the fields of a packet and passes that
- * packet to the transport using TSendPacket. It also stores the
- * opcode sent in the lastop field of the game, so the packet
- * can be regenerated if necessary.
- *----------------------------------------------------------------------
- */
-
- sendpkt(g,op)
- struct game *g;
- char op;
- {
- static char colors[4], adbl[10];
- int i;
-
- if (FeIsActive)
- FeMessage("Sending...");
- P.version = LDB_VER; /* these fields go in all packets */
- P.gameid = g->gameid;
- P.opcode = op;
- P.seq = g->seq;
- P.comment = g->mycmt;
- P.comment2 = g->mycmt2;
- if ( (g->flags & F_SENTNAME) == 0) { /* if I haven't already sent my name */
- P.name = rc.myname; /* send it */
- g->flags |= F_SENTNAME; /* set flag */
- }
- else
- P.name = NULL;
- P.addr = NULL; /* these fields only used by START */
- P.colors = NULL;
- P.dir = NULL;
- P.autodbl = NULL; /* used by START and TIE */
- clearmvs(P.mvs);
- switch (op) { /* now do operation-specific stuff */
- case START:
- P.addr = rc.myaddr; /* send opponent my email address */
- P.mvs[0].roll = g->mvs[0].roll; /* send initial die roll */
- sprintf(colors,"%c%c",g->mycolor,g->opcolor);
- P.colors = colors;
- P.dir = (g->mydir > 0) ? "down" : "up";
- sprintf(adbl,"%d",rc.autodouble);
- P.autodbl = adbl;
- break;
- case USTART:
- P.mvs[0].roll = g->mvs[0].roll; /* send both initial dice */
- P.mvs[1].roll = g->mvs[1].roll;
- break;
- case RESTART: /* retry initial roll */
- P.mvs[0].roll = g->mvs[0].roll; /* send new roll */
- break;
- case TIE:
- if (g->adcnt > 0) { /* send current autodouble count */
- sprintf(adbl,"%d",g->adcnt);
- P.autodbl = adbl;
- }
- break;
- case MOVE:
- for (i = 0; i < 4; i++)
- P.mvs[i] = g->mvs[i];
- break;
- }
- g->lastop = op; /* save last op for resend */
- TSendPacket(&P,g->opaddr); /* send the packet */
- if (FeIsActive) /* clear "Sending..." from mesg line */
- FeMessage(NULL);
- }
-
-
-
- /*----------------------------------------------------------------------
- * str2mv -- decode move string to struct mv
- *
- * This function takes a string representation of a move, decodes it,
- * and places the information into a mv structure. This format is:
- *
- * roll/move
- *
- * where roll is the die value used in the move, and is a single
- * digit in [1..6], and move is one of the following:
- *
- * a 1 or 2 digit number in [1..24]
- * This designates the point the move originates from.
- * This number is stored in the "pt" field of the move
- * structure without modification.
- * the string "BAR"
- * This means the piece is coming off the bar.
- * Zero is stored in the "pt" field of the move structure,
- * regardless of whether the player's bar point is 0 or 25.
- * Apply() and FeDrawMove understand this and convert 0 to
- * the appropriate bar point before using it.
- * the string "UNUSED"
- * This means the roll is unused. -1 is stored in the
- * "pt" field of the mv structure.
- *----------------------------------------------------------------------
- */
-
- str2mv(s,m)
- char *s;
- struct mv *m;
- {
- char *p, *strchr();
-
- if ( (p = strchr(s,'/')) == NULL) {
- fprintf(stderr,"ERROR: malformed move: %s\n",s);
- return;
- }
- if ( ( (m->roll = atoi(s)) < 0) || (m->roll > 6) ) {
- fprintf(stderr,"ERROR: invalid roll: %d\n",m->roll);
- return;
- }
- p++;
- if ( (m->roll == 0) || (*p == 'U') || (*p == 'u') )
- m->pt = -1; /* this roll is unused */
- else if ( (*p == 'B') || (*p == 'b') )
- m->pt = 0; /* move from bar */
- else if ( ( (m->pt = atoi(p)) < 0) || (m->pt > 25) ) {
- fprintf(stderr,"ERROR: invalid point: %d\n",m->pt);
- return;
- }
- }
-
-
- /*----------------------------------------------------------------------
- * mv2str -- encode move string from struct mv
- *
- * This function forms a string representation of a move based on
- * the information in a mv structure. This format is:
- *
- * roll/move
- *
- * where roll is the die value stored in mv->roll, and move is:
- *
- * mv->pt if mv->pt is in [1..24]
- * the string "BAR", if mv->pt is 0 or 25
- * the string "UNUSED", if mv->pt is < 0
- *----------------------------------------------------------------------
- */
-
- mv2str(m,s)
- struct mv *m;
- char *s;
- {
-
- if (m->roll <= 0) { /* non-existant roll */
- strcpy(s,"0/0"); /* should be skipped by nvwrite */
- return; /* so we should never get here */
- }
- if (m->pt < 0)
- sprintf(s,"%d/UNUSED",m->roll);
- else if ( (m->pt == DOWNBAR) || (m->pt == UPBAR) )
- sprintf(s,"%d/BAR",m->roll);
- else
- sprintf(s,"%d/%d",m->roll,m->pt);
- }
-
-
- /*----------------------------------------------------------------------
- * clearmvs -- mark all entries in a mv array empty
- *
- * This function marks all elements of a mv array as being unused.
- *----------------------------------------------------------------------
- */
-
-
- clearmvs(m)
- struct mv *m;
- {
- int i;
-
- for (i = 0; i < 4; i++) {
- m[i].roll = -1;
- m[i].pt = -1;
- }
- }
-
-
-
- /*----------------------------------------------------------------------
- * colorname -- convert color character to color name
- *
- * This function expands a color character to the full color name.
- * Legal colors are: r (Red), w (White), and b (Black).
- *----------------------------------------------------------------------
- */
-
- char *colorname(c)
- char c;
- {
-
- switch (c) {
- case 'r':
- return("Red");
- case 'w':
- return("White");
- case 'b':
- return("Black");
- default:
- return("*BAD COLOR*");
- }
- }
-