home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume28 / ldb / part02 / misc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-03-15  |  6.8 KB  |  249 lines

  1. /*    misc.c        8/8/91
  2.  *
  3.  * Copyright 1991  Perry R. Ross
  4.  *
  5.  * Permission to use, copy, modify, and distribute this software and its
  6.  * documentation without fee is hereby granted, subject to the restrictions
  7.  * detailed in the README file, which is included here by reference.
  8.  * Any other use requires written permission from the author.  This software
  9.  * is distributed "as is" without any warranty, including any implied
  10.  * warranties of merchantability or fitness for a particular purpose.
  11.  * The author shall not be liable for any damages resulting from the
  12.  * use of this software.  By using this software, the user agrees
  13.  * to these terms.
  14.  */
  15.  
  16. #include "ldb.h"
  17.  
  18.  
  19. /*----------------------------------------------------------------------
  20.  *    rolldice -- roll two dice
  21.  *
  22.  * This function calls Rolldie twice and fills in the game structure
  23.  * with the resulting values.  If the two calls to Rolldie return the
  24.  * same number, the mvs field is filled in so that the user has 4 rolls
  25.  * to use, otherwise the rolls are stored in the first two elements
  26.  * of the mvs field and the last two are marked unused.
  27.  *----------------------------------------------------------------------
  28.  */
  29.  
  30. rolldice(g)
  31. struct game *g;
  32. {
  33.  
  34. clearmvs(g->mvs);            /* clear old stuff */
  35. g->mvs[0].roll = Rolldie();             /* roll the dice */
  36. g->mvs[1].roll = Rolldie();
  37. if (g->mvs[0].roll == g->mvs[1].roll) {    /* hot damn, we got doubles */
  38.     g->mvs[2].roll = g->mvs[0].roll;    /* copy roll into two */
  39.     g->mvs[3].roll = g->mvs[0].roll;    /* more moves */
  40.     }
  41. legalmoves(g);            /* calculate the # of moves & hi roll */
  42. }
  43.  
  44.  
  45.  
  46. /*----------------------------------------------------------------------
  47.  *    sendpkt -- send a packet to the opponent
  48.  *
  49.  * This function fills in the fields of a packet and passes that
  50.  * packet to the transport using TSendPacket.  It also stores the
  51.  * opcode sent in the lastop field of the game, so the packet
  52.  * can be regenerated if necessary.
  53.  *----------------------------------------------------------------------
  54.  */
  55.  
  56. sendpkt(g,op)
  57. struct game *g;
  58. char op;
  59. {
  60. static char colors[4], adbl[10];
  61. int i;
  62.  
  63. if (FeIsActive)
  64.     FeMessage("Sending...");
  65. P.version = LDB_VER;            /* these fields go in all packets */
  66. P.gameid = g->gameid;
  67. P.opcode = op;
  68. P.seq = g->seq;
  69. P.comment = g->mycmt;
  70. P.comment2 = g->mycmt2;
  71. if ( (g->flags & F_SENTNAME) == 0) {    /* if I haven't already sent my name */
  72.     P.name = rc.myname;        /* send it */
  73.     g->flags |= F_SENTNAME;        /* set flag */
  74.     }
  75. else
  76.     P.name = NULL;
  77. P.addr = NULL;                /* these fields only used by START */
  78. P.colors = NULL;
  79. P.dir = NULL;
  80. P.autodbl = NULL;            /* used by START and TIE */
  81. clearmvs(P.mvs);
  82. switch (op) {                /* now do operation-specific stuff */
  83. case START:
  84.     P.addr = rc.myaddr;        /* send opponent my email address */
  85.     P.mvs[0].roll = g->mvs[0].roll;    /* send initial die roll */
  86.     sprintf(colors,"%c%c",g->mycolor,g->opcolor);
  87.     P.colors = colors;
  88.     P.dir = (g->mydir > 0) ? "down" : "up";
  89.     sprintf(adbl,"%d",rc.autodouble);
  90.     P.autodbl = adbl;
  91.     break;
  92. case USTART:
  93.     P.mvs[0].roll = g->mvs[0].roll;    /* send both initial dice */
  94.     P.mvs[1].roll = g->mvs[1].roll;
  95.     break;
  96. case RESTART:                /* retry initial roll */
  97.     P.mvs[0].roll = g->mvs[0].roll;    /* send new roll */
  98.     break;
  99. case TIE:
  100.     if (g->adcnt > 0) {        /* send current autodouble count */
  101.         sprintf(adbl,"%d",g->adcnt);
  102.         P.autodbl = adbl;
  103.         }
  104.     break;
  105. case MOVE:
  106.     for (i = 0; i < 4; i++)
  107.         P.mvs[i] = g->mvs[i];
  108.     break;
  109.     }
  110. g->lastop = op;                /* save last op for resend */
  111. TSendPacket(&P,g->opaddr);        /* send the packet */
  112. if (FeIsActive)                /* clear "Sending..." from mesg line */
  113.     FeMessage(NULL);
  114. }
  115.  
  116.  
  117.  
  118. /*----------------------------------------------------------------------
  119.  *    str2mv -- decode move string to struct mv
  120.  *
  121.  * This function takes a string representation of a move, decodes it,
  122.  * and places the information into a mv structure.  This format is:
  123.  *
  124.  *    roll/move
  125.  *
  126.  * where roll is the die value used in the move, and is a single
  127.  * digit in [1..6], and move is one of the following:
  128.  *
  129.  *    a 1 or 2 digit number in [1..24]
  130.  *        This designates the point the move originates from.
  131.  *        This number is stored in the "pt" field of the move
  132.  *        structure without modification.
  133.  *    the string "BAR"
  134.  *        This means the piece is coming off the bar.
  135.  *        Zero is stored in the "pt" field of the move structure,
  136.  *        regardless of whether the player's bar point is 0 or 25.
  137.  *        Apply() and FeDrawMove understand this and convert 0 to
  138.  *        the appropriate bar point before using it.
  139.  *    the string "UNUSED"
  140.  *        This means the roll is unused.  -1 is stored in the
  141.  *        "pt" field of the mv structure.
  142.  *----------------------------------------------------------------------
  143.  */
  144.  
  145. str2mv(s,m)
  146. char *s;
  147. struct mv *m;
  148. {
  149. char *p, *strchr();
  150.  
  151. if ( (p = strchr(s,'/')) == NULL) {
  152.     fprintf(stderr,"ERROR: malformed move: %s\n",s);
  153.     return;
  154.     }
  155. if ( ( (m->roll = atoi(s)) < 0) || (m->roll > 6) ) {
  156.     fprintf(stderr,"ERROR: invalid roll: %d\n",m->roll);
  157.     return;
  158.     }
  159. p++;
  160. if ( (m->roll == 0) || (*p == 'U') || (*p == 'u') )
  161.     m->pt = -1;        /* this roll is unused */
  162. else if ( (*p == 'B') || (*p == 'b') )
  163.     m->pt = 0;        /* move from bar */
  164. else if ( ( (m->pt = atoi(p)) < 0) || (m->pt > 25) ) {
  165.     fprintf(stderr,"ERROR: invalid point: %d\n",m->pt);
  166.     return;
  167.     }
  168. }
  169.  
  170.  
  171. /*----------------------------------------------------------------------
  172.  *    mv2str -- encode move string from struct mv
  173.  *
  174.  * This function forms a string representation of a move based on
  175.  * the information in a mv structure.  This format is:
  176.  *
  177.  *    roll/move
  178.  *
  179.  * where roll is the die value stored in mv->roll, and move is:
  180.  *
  181.  *    mv->pt if mv->pt is in [1..24]
  182.  *    the string "BAR", if mv->pt is 0 or 25
  183.  *    the string "UNUSED", if mv->pt is < 0
  184.  *----------------------------------------------------------------------
  185.  */
  186.  
  187. mv2str(m,s)
  188. struct mv *m;
  189. char *s;
  190. {
  191.  
  192. if (m->roll <= 0) {        /* non-existant roll */
  193.     strcpy(s,"0/0");    /* should be skipped by nvwrite */
  194.     return;            /* so we should never get here */
  195.     }
  196. if (m->pt < 0)
  197.     sprintf(s,"%d/UNUSED",m->roll);
  198. else if ( (m->pt == DOWNBAR) || (m->pt == UPBAR) )
  199.     sprintf(s,"%d/BAR",m->roll);
  200. else
  201.     sprintf(s,"%d/%d",m->roll,m->pt);
  202. }
  203.  
  204.  
  205. /*----------------------------------------------------------------------
  206.  *    clearmvs -- mark all entries in a mv array empty
  207.  *
  208.  * This function marks all elements of a mv array as being unused.
  209.  *----------------------------------------------------------------------
  210.  */
  211.  
  212.  
  213. clearmvs(m)
  214. struct mv *m;
  215. {
  216. int i;
  217.  
  218. for (i = 0; i < 4; i++) {
  219.     m[i].roll = -1;
  220.     m[i].pt = -1;
  221.     }
  222. }
  223.  
  224.  
  225.  
  226. /*----------------------------------------------------------------------
  227.  *    colorname -- convert color character to color name
  228.  *
  229.  * This function expands a color character to the full color name.
  230.  * Legal colors are: r (Red), w (White), and b (Black).
  231.  *----------------------------------------------------------------------
  232.  */
  233.  
  234. char *colorname(c)
  235. char c;
  236. {
  237.  
  238. switch (c) {
  239. case 'r':
  240.     return("Red");
  241. case 'w':
  242.     return("White");
  243. case 'b':
  244.     return("Black");
  245. default:
  246.     return("*BAD COLOR*");
  247.     }
  248. }
  249.