#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#include <amix.h>			// For Structures
#include <clib/amix_protos.h>		// For Prototypes
#include <ami-custom.h>			// For My Structures

#include <clib/dos_protos.h>		
#include <exec/types.h>

#define Ver "$VER: Sysop Menu Version 1.0 Cray-1 of A((eSS"
#define sm sendmessage
#define pm prompt

#define MaxIters    30
#define XSIZE           80
#define YSIZE           22
#define BLACK           -1

float 	jimi=0.0,jimr=0.0;
float 	LEFT=-2.0,RIGHT=1.0,TOP=1.0,BOTTOM=-1.0;
short   x, y, counter;
long 	double zr=0.0, zi=0.0, cr, ci, rsquared, isquared;
char 	buffer[40];
char 	fish[200];

char FISH[2],single[2];
char colour[10];
int screenx=0,screeny=22;
void plot( void );

main(int argc,char *argv[])
{
       BOOL terminated=FALSE;
       int fred=0;
       //float bobp=0.50,bobn=-0.50;
       float argX, argY, realSide, imagSide;
       
       Register(argv[1][0]-'0');      		// ALWAYS include this LINE At the Start
						// It registers Your Door with X!


       sm("[33m Online Fractals By Cray-1:[0m",1);
       sm("                [34mExclusive For Future Entrance.[0m",1);
       sm("",1);
       Delay( 50 * 2);
       sm("",0);
       plot();

while(!terminated)
  {       
       hotkey("",FISH);
       fred = atoi(FISH);
       switch(fred)
             {
             
             case 8:
                  sprintf(single,"%c",11);
                  sm(single,0);
                  screeny--;           
                 break;
             
             case 9:
                  argX = LEFT + screenx * (RIGHT - LEFT) / XSIZE;
                  argY = TOP + screeny * (BOTTOM - TOP) / YSIZE;
                  realSide=RIGHT-LEFT;
                  imagSide=BOTTOM-TOP;
                  realSide/=4;
                  imagSide/=4;
                  TOP=argY+imagSide;
                  BOTTOM=argY-imagSide;
                  LEFT=argX-realSide;
                  RIGHT=argX+realSide;
                  sm("",0);
                  plot();
                break;


             case 2:          
                  sprintf(single,"%c",132);
                  sm(single,0);
                  screeny++;
                break;

             case 4:           
                  if(screenx<0)screenx=80;
                  sm("[D",0);
                  screenx--;
                break;

             case 6:  
                   sm("[C",0);
                   screenx++;
                break;        

             case 5:
                 sprintf(buffer,"%d,%d",screenx,screeny);
                 sm(buffer,1);
 		 sm("ANSI Mandelbrot.. By Cray-1",1);
                 ShutDown();
                 end();
                break;
              }
        
        }      

}

// The next two functions always have to be included!!! 

void end(void)
{
       exit(0);
}

void LastCommand(void)
{

      sm("",1);
}




void plot(void )
{  
cr = LEFT + x * (RIGHT - LEFT) / XSIZE;
                ci = TOP + y * (BOTTOM - TOP) / YSIZE;
    strcpy(colour,"[0m\0");
 for (y = 0; y < YSIZE; y++)
        {
        for (x = 0; x < XSIZE; x++)
                {
                zi=jimi;
                zr=jimr;  
                cr = LEFT + x * (RIGHT - LEFT) / XSIZE;
                ci = TOP + y * (BOTTOM - TOP) / YSIZE;
                rsquared = zr * zr;
                isquared = zi * zi;

                /* The next eleven lines actually do all of the work. */
for (counter = 0; rsquared + isquared <= 4.0 && counter < MaxIters; counter++)
        {
              zi = zr * zi;
              zi += zi;
              zi += ci;

              zr = rsquared - isquared;
              zr += cr;
              
              rsquared = zr * zr;
              isquared = zi * zi;
        }
        
        if(counter==14)strcpy(colour,"[31m±\0");
        if(counter==13)strcpy(colour,"[32m±\0");
        if(counter==12)strcpy(colour,"[33m±\0");
        if(counter==11)strcpy(colour,"[34m±\0");
        if(counter==10)strcpy(colour,"[35m±\0");
        if(counter==9)strcpy(colour,"[36m±\0");
        if(counter==8)strcpy(colour,"[37m±\0");
        if(counter==7)strcpy(colour,"[31mÛ\0");
        if(counter==6)strcpy(colour,"[32mÛ\0");
        if(counter==5)strcpy(colour,"[33mÛ\0");
        if(counter==4)strcpy(colour,"[34mÛ\0");
        if(counter==3)strcpy(colour,"[35mÛ\0");
        if(counter==2)strcpy(colour,"[36mÛ\0");
        if(counter==1)strcpy(colour,"[37mÛ\0");      
        if(counter>14)strcpy(colour,"[0m \0");
        sprintf(buffer,"%s",colour);
               if (rsquared + isquared <= 4.0)
                   {      
                        sm(" ",0);   /* In the set. */
                    }                
                else
                     {
                       sm(buffer,0);   /* Not in the set. */
                    }

            } 
       sm("",1);
       }
}

