/* Dungeons and Dragons dice roller                */
/* Phase I of the Dungeon Master's                 */
/* Familiar project.                               */
/* James Gary                                      */
/* 2-13-88                                         */
/* 3-4-88   Modified for inclusion in              */
/*          combat system                          */
/* 3-8-88   Modified to use common                 */
/*          message port                           */
/* 7-9-88   Modified to use Inovatolls             */
/*          FlashyOpenWindow and                   */
/*          FlashyCloseWindow calls                */
/* 7-12-88  Converted to PowerWindows format       */
/*          Added user number gadget and repeat    */
/*          Added total display and clear gadget   */

/* Note: this program was adapted from John Toebes port of a Unix
   calculator program. Although the purpose is vastely different,
   the appearance of the window is similar, so I kept most of his
   declarations (gadget borders etc.). Thanks John. Note: used
   powerwindows grab function, so original declarations now gone
   7-12-88 */

#include <math.h>
#include <dos.h>
#include <intuition/intuition.h>
#include <exec/io.h>
#include <proto/graphics.h>
#include <proto/intuition.h>
#include <proto/exec.h>
#include "roller.h"

#define GRAPHICS_REV 29
#define INTUITION_REV 29
#define MAXLONG 2147483646.0



/* Send our messages to this port attached to the main combat window */
extern struct MsgPort *MyPort;
int Total = 0;

struct Window *MyWindow = NULL; /* the roller window */

/* User selected 'roll' gadget from main combat window */
void setup()
{
	if (MyWindow == NULL) { /* window is not open */
		MyWindow = (struct Window *)
                           FlashyOpenWindow(&NewWindowStructure1);
	        MyWindow->UserPort = MyPort; /* attach to main port*/
                   /* We don't want an IDCMP port, so set flags after opening*/
	        ModifyIDCMP(MyWindow,GADGETUP|GADGETDOWN|CLOSEWINDOW);
                PrintIText(MyWindow->RPort,&IntuiTextList1,0,0);
        }
	else  /* window is already open, just bring to front */
		WindowToFront(MyWindow);
}

/* Generate random number 1--spots */
random(spots)
int spots;
{
	float x;
	x = spots * (lrand48()/MAXLONG);
	return(( int )x + 1);
}

/* Display result of dice roll */
void display(size)
int size;
{
	char buff[20];
	int linelen;
        int num;
	
        num = random(size);
	linelen = sprintf(buff,"%2d",num);
	Move(MyWindow->RPort,12,103);
	Text(MyWindow->RPort,"   ",3);
	Move(MyWindow->RPort,12,103);
	Text(MyWindow->RPort,buff,linelen);
        Total += num;
        DisplayTotal();
}
		
/* Display running total*/
DisplayTotal()
{
        char buff[20];
        int linelen;
        
        linelen = sprintf(buff,"%2d",Total);
        Move(MyWindow->RPort,160,103);
        Text(MyWindow->RPort,"   ",3);
        Move(MyWindow->RPort,160,103);
        Text(MyWindow->RPort,buff,linelen);
        return(0);
}


/* Quitting roller or main, so close it up!*/
void cleanup()
{
	if (MyWindow != NULL) { /* Close only if open*/
		MyWindow->UserPort = NULL;
		FlashyCloseWindow(MyWindow);
		MyWindow = NULL;
	}
}

/* User wants to roll, so lets set-up and seed r.n. generator*/
Roll(gad)
struct Gadget *gad;
{
	setup(); /* Open window if needed */
	srand48(time(NULL)); /* Seed random number generator*/
	return(0);
}
