/*  File drawmap.h  */

#ifndef MAX
#define MAX(a,b) ((a)>=(b) ? (a) : (b))
#define ABS(a) ((a)>=0 ? (a) : (-(a)))
#endif

#define NOABORT 1<<1                   /* used in printmap() */
#define U_ABORT 1<<2
#define P_ABORT 1<<3

#ifndef PDERR_NOERR
#define PDERR_NOERR 0L
#endif

#define BITVAL(x,n)  ((x>>n)&1)
#define BITSETT(x,n)  (x | bitval[n])
#define BITCLEAR(x,n)  (x & (~bitval[n]))
#define BITSTORE(x,n,val)  ((val==0) ? (x=BITCLEAR(x,n)) : (x=BITSETT(x,n)))

#define SHADOW_DISP  4                 /* shadow displacement (pix) */

#define WC WINDOWCLOSE
#define WDP WINDOWDEPTH

#define IDCMPFLAGS (CLOSEWINDOW | MOUSEBUTTONS | MENUPICK)

#define WINFLAGS1 (SMART_REFRESH | ACTIVATE | BACKDROP)
#define WINFLAGS2 (BORDERLESS | REPORTMOUSE)

#define WWIDTH 640                     /* window width (pixels)       */
#define WHEIGHT 400                    /* window height (pixels)      */
#define VMODE (HIRES | LACE)           /* view mode                   */
#define DEPTH 4                        /* 16 colors => 4 bitplanes    */

#define CENTERX (WWIDTH/2)             /* place origin at center of   */
#define CENTERY (WHEIGHT/2)            /*   window                    */
#define HFACTOR (WWIDTH/360.)          /* horizontal scale (pix/deg)  */
#define VFACTOR (WHEIGHT/180.)         /* vertical scale (pix/deg)    */
#define M_VFACTOR (WHEIGHT/5.90)       /* vertical scale for Mercator */
                                       /*   map (+/- 84 degrees lat.) */
#define ASP_RAT ((640./400.)*(188./260.)) /* screen aspect ratio      */
                                       /*  (max width / max hgt)      */
#define VRADIUS 160                    /* vertical radius of ellipse  */
                                       /*   (pixels)                  */
#define HRADIUS (short)(ASP_RAT * VRADIUS+0.5) /* horiz. radius (pix) */
#define ROWOFFSET (WWIDTH/8)           /* length of each screen row   */
                                       /*   in bytes                  */

#define OK     0                       /* result flags */
#define NOT_OK 1
#define ABORT  2

#define BLUE       0L                  /* default colors */
#define WHITE      1L
#define LT_VIO     2L
#define DK_VIO     3L
#define LT_GRN     4L
#define DK_GRN     5L
#define LT_BL      6L
#define DK_BL      7L
#define LT_YEL     8L
#define DK_YEL     9L
#define LT_PUR    10L
#define DK_PUR    11L
#define LT_RED    12L
#define DK_RED    13L
#define BLACK     14L
#define ORANGE    15L

UWORD mapcolors[] = {                  /* default color table            */
   0x006b, 0x0fff, 0x0caf, 0x095f,     /* blue, white, lt vio, dk vio    */
   0x00f0, 0x00a0, 0x00bf, 0x000f,     /* lt grn, dk grn, lt bl, dk bl   */
   0x0ff0, 0x0cc0, 0x0f0f, 0x0b0b,     /* lt yel, dk yel, lt pur, dk pur */
   0x0f44, 0x0c00, 0x0000, 0x0f90      /* lt red, dk red, black, orange  */
};

                                       /* number of colors in color table */
#define NUM_COLORS (sizeof(mapcolors)/sizeof(UWORD))

UWORD configcolors[NUM_COLORS];        /* color configuration */

char configfile[]   = "map.config";
char limitsfile[]   = "map.limits.bin";
char flatmapfile[]  = "map.flat.pic";

#define RE 6378.165                    /* radius of Earth (kilometers) */
#define VIEW_HEIGHT 300.               /* default height for orbital   */
                                       /*   view (km)                  */
#define MIN_HEIGHT 10.                 /* minimum view height (km)     */

double view_height, eta, etap, facp;   /* declare global constants */

#define PI    3.141592653589793        /* define constants */
#define PI2   (PI/2.)
#define TWOPI (2.*PI)
#define RAD   (PI/180.)
#define EXPAND_SCALE_FACTOR 2.0        /* for box zoom out option */

char fontname[] = "topaz.font";        /* screen font name */

struct TextAttr mapfont = {            /* screen font */
   (STRPTR) &fontname, 8, 0, 0
};

struct NewScreen mapscreen = {
   0, 0,
   WWIDTH, WHEIGHT, DEPTH,
   1, 0,
   VMODE,
   CUSTOMSCREEN,
   &mapfont,
   (UBYTE *) "Drawmap v. 4.0, by Bryan Brown",
   NULL, NULL
};

struct NewWindow mapWindow = {
   0, 0,
   WWIDTH, WHEIGHT,
   0, 1,
   IDCMPFLAGS,
   WINFLAGS1 | WINFLAGS2,
   NULL, NULL, NULL, NULL, NULL,
   10, 10, WWIDTH, WHEIGHT,
   CUSTOMSCREEN
};

UWORD arrow_data[] = {                 /* mouse pointer = arrow  */
   0x0000, 0x0000,

   0x8000, 0x0000,
   0xc000, 0x0000,
   0xe000, 0x0000,

   0xf000, 0x0000,

   0xe000, 0x0000,
   0x2000, 0x0000,
   0x0000, 0x0000,

   0x0000, 0x0000
};

#define ARROW_X_OFFSET -1
#define ARROW_Y_OFFSET  0
#define ARROW_SIZE      sizeof(arrow_data)

UWORD cross_data[] = {                 /* mouse pointer = cross */
   0x0000, 0x0000,

   0x0400, 0x0000,
   0x0400, 0x0000,
   0x0400, 0x0000,
   0x0400, 0x0000,
   0x0000, 0x0000,

   0xf1e0, 0x0000,

   0x0000, 0x0000,
   0x0400, 0x0000,
   0x0400, 0x0000,
   0x0400, 0x0000,
   0x0400, 0x0000,

   0x0000, 0x0000
};

#define CROSS_X_OFFSET -6
#define CROSS_Y_OFFSET -5
#define CROSS_SIZE     (sizeof(cross_data))

UWORD waiter_data[] = {                /* mouse pointer = 'wait' */
   0x0000, 0x0000,

   0x892e, 0x0000,
   0xaaa4, 0x0000,
   0xaba4, 0x0000,
   0x52a4, 0x0000,

   0x0000, 0x0000
};

#define WAITER_X_OFFSET -1
#define WAITER_Y_OFFSET  0
#define WAITER_SIZE      (sizeof(waiter_data))

UWORD transparent_data[] = {           /* mouse pointer = transparent */
   0x0000, 0x0000,

   0x0000, 0x0000,
   0x0000, 0x0000,
   0x0000, 0x0000,

   0x0000, 0x0000
};

#define TRANSPARENT_X_OFFSET 0
#define TRANSPARENT_Y_OFFSET 0
#define TRANSPARENT_SIZE     (sizeof(transparent_data))

#define MAX_DETAIL_LEVEL 5             /* maximum value of detail level */

int detail_level = MEDIUM;             /* drawing detail level */

struct Pt  {                           /* format for each point */
   short code;
   short lat;
   short lam;
};

struct Arc  {                          /* format for limits in */
   short lat_min, lat_max;             /*   each segment       */
   short lam_min, lam_max;
   int first, last;
};

typedef struct mapinfo {               /* format for general map info   */
   char *mapname;                      /* map file name                 */
   int numpts;                         /* number of points in map       */
   int nsegs;                          /* number of segments in map     */
   int first_seg;                      /* location in big segment array */
   int plot;                           /* flag for plotting map         */
   long color;                         /* color to use for plotting     */
   struct Pt *pt;                      /* address of points buffer      */
   struct Arc *seg;                    /* address of segment buffer     */
}  MapInfo;

MapInfo map[] = {                     /* map information */
   { "coast.pnt",   74967, 208,    0, TRUE, ORANGE, NULL, NULL },
   { "country.pnt", 22359, 301,  208, TRUE, LT_RED, NULL, NULL },
   { "state.pnt",    2259, 111,  509, TRUE, LT_RED, NULL, NULL },
   { "island.pnt",  35171, 344,  620, TRUE, ORANGE, NULL, NULL },
   { "lake.pnt",    15118, 103,  964, TRUE, DK_BL,  NULL, NULL },
   { "river.pnt",   28194, 196, 1067, TRUE, LT_BL,  NULL, NULL }
};

#define NUM_MAPS (sizeof(map)/sizeof(MapInfo))

#define NSEGS (208+301+111+344+103+196) /* number of segments */

short box_borderpts[] = {               /* border points for outline box */
   0, 0,
   1, 0,
   1, 1,
   0, 1,
   0, 0
};

struct Border box_border = {           /* border definition for box */
   0, 0,
   WHITE, BLACK, COMPLEMENT,
   sizeof (box_borderpts) / (2*sizeof (short)),
   &box_borderpts[0],
   NULL
};
