home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 199.lha / DmfSrc_v2.5 / roller.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-12-27  |  3.3 KB  |  120 lines

  1. /* Dungeons and Dragons dice roller                */
  2. /* Phase I of the Dungeon Master's                 */
  3. /* Familiar project.                               */
  4. /* James Gary                                      */
  5. /* 2-13-88                                         */
  6. /* 3-4-88   Modified for inclusion in              */
  7. /*          combat system                          */
  8. /* 3-8-88   Modified to use common                 */
  9. /*          message port                           */
  10. /* 7-9-88   Modified to use Inovatolls             */
  11. /*          FlashyOpenWindow and                   */
  12. /*          FlashyCloseWindow calls                */
  13. /* 7-12-88  Converted to PowerWindows format       */
  14. /*          Added user number gadget and repeat    */
  15. /*          Added total display and clear gadget   */
  16.  
  17. /* Note: this program was adapted from John Toebes port of a Unix
  18.    calculator program. Although the purpose is vastely different,
  19.    the appearance of the window is similar, so I kept most of his
  20.    declarations (gadget borders etc.). Thanks John. Note: used
  21.    powerwindows grab function, so original declarations now gone
  22.    7-12-88 */
  23.  
  24. #include <math.h>
  25. #include <dos.h>
  26. #include <intuition/intuition.h>
  27. #include <exec/io.h>
  28. #include <proto/graphics.h>
  29. #include <proto/intuition.h>
  30. #include <proto/exec.h>
  31. #include "roller.h"
  32.  
  33. #define GRAPHICS_REV 29
  34. #define INTUITION_REV 29
  35. #define MAXLONG 2147483646.0
  36.  
  37.  
  38.  
  39. /* Send our messages to this port attached to the main combat window */
  40. extern struct MsgPort *MyPort;
  41. int Total = 0;
  42.  
  43. struct Window *MyWindow = NULL; /* the roller window */
  44.  
  45. /* User selected 'roll' gadget from main combat window */
  46. void setup()
  47. {
  48.     if (MyWindow == NULL) { /* window is not open */
  49.         MyWindow = (struct Window *)
  50.                            FlashyOpenWindow(&NewWindowStructure1);
  51.             MyWindow->UserPort = MyPort; /* attach to main port*/
  52.                    /* We don't want an IDCMP port, so set flags after opening*/
  53.             ModifyIDCMP(MyWindow,GADGETUP|GADGETDOWN|CLOSEWINDOW);
  54.                 PrintIText(MyWindow->RPort,&IntuiTextList1,0,0);
  55.         }
  56.     else  /* window is already open, just bring to front */
  57.         WindowToFront(MyWindow);
  58. }
  59.  
  60. /* Generate random number 1--spots */
  61. random(spots)
  62. int spots;
  63. {
  64.     float x;
  65.     x = spots * (lrand48()/MAXLONG);
  66.     return(( int )x + 1);
  67. }
  68.  
  69. /* Display result of dice roll */
  70. void display(size)
  71. int size;
  72. {
  73.     char buff[20];
  74.     int linelen;
  75.         int num;
  76.     
  77.         num = random(size);
  78.     linelen = sprintf(buff,"%2d",num);
  79.     Move(MyWindow->RPort,12,103);
  80.     Text(MyWindow->RPort,"   ",3);
  81.     Move(MyWindow->RPort,12,103);
  82.     Text(MyWindow->RPort,buff,linelen);
  83.         Total += num;
  84.         DisplayTotal();
  85. }
  86.         
  87. /* Display running total*/
  88. DisplayTotal()
  89. {
  90.         char buff[20];
  91.         int linelen;
  92.         
  93.         linelen = sprintf(buff,"%2d",Total);
  94.         Move(MyWindow->RPort,160,103);
  95.         Text(MyWindow->RPort,"   ",3);
  96.         Move(MyWindow->RPort,160,103);
  97.         Text(MyWindow->RPort,buff,linelen);
  98.         return(0);
  99. }
  100.  
  101.  
  102. /* Quitting roller or main, so close it up!*/
  103. void cleanup()
  104. {
  105.     if (MyWindow != NULL) { /* Close only if open*/
  106.         MyWindow->UserPort = NULL;
  107.         FlashyCloseWindow(MyWindow);
  108.         MyWindow = NULL;
  109.     }
  110. }
  111.  
  112. /* User wants to roll, so lets set-up and seed r.n. generator*/
  113. Roll(gad)
  114. struct Gadget *gad;
  115. {
  116.     setup(); /* Open window if needed */
  117.     srand48(time(NULL)); /* Seed random number generator*/
  118.     return(0);
  119. }
  120.