//		  ÚÄÄÄÄÄÄÄ¿
//	ÄÄÄÄÄÄÄÄÄ>³ AVNER ³
//	ÄÄÄÄÄÄÄÄÄ>³  BEN  ³ÄÄÄÄÄÄ> Software Engineering Method
//		  ÀÄÄÄÄÄÄÄÙ
//	10 Dov-Hoz st. Tel-Aviv 63416 Israel tel. 972-3-221535

// The Screen NAVigator, ver 1.20 August 1990
// Copyright (c) 1990 by Avner Ben
// Snav is not, and never was, free software.
// for conditions for use refer to file "copyrigh.txt"

// The Screen Navigator is an object-oriented device-independent
// character-graphics driver package, written in the C++ language,
// distributed in the form of C++ source code.
// For further information refer to the documentation files.

// The user may not omit this text, from the beginning of the file, to
// the line of asterisks below.

/***************************************************************************/

// package nucleus part 1 - headers.
// This file defines the basic terms of "character geometry", to be used
// in the more specific other parts.
// for data-structure logic, refer to file "article".

// History:
// 25.3.89 avner ben coded.
// 9.7.90  avner ben - last update.
// refer to documentation for further details.
// 5.8.90  - renamed MAXDIR MAX_DIR
// 6.8.90  - removed some constructors to implementation


// Site history (of this copy):
// __.__.__ ____________ : __________________.

#include <stdio.h>
#ifndef SNAV0_H
#define SNAV0_H

#ifndef TRUE
typedef int	boolean;
#define TRUE	1
#define FALSE	0
#endif

#define ENV_MODIFY	// governs the behaviour of some full-screen methods.

enum connective {
	// the order reflects the natural order of reading a
	// decision tree in english, horizontally oriented.
	// note: the direction a character connects at is often
	// opposite the one it "points" at!
	NODIR,		// not a semigraphic
	RTDIR,		// char connects right (bit 1 on)
	UPDIR,		// char connects up (bit 2 on)
	LTDIR=4,	// char connects left (bit 3 on)
	DNDIR=8 	// char connects down (bit 4 on)
};

#define DEF_DIR RTDIR
#define MAX_DIR DNDIR

class direction
{ // which way are we going?

	friend class intersection;
	friend class axis;

	private :
		connective where;

	public :
		direction(connective initdir=DEF_DIR)
		 { where=initdir; }
		direction(int initdir)
		 { where=(connective)initdir; }
		direction(char c); // use code-letter
		connective whereto(void)
		 { return(where); }
		connective operator()(void)
		 { return(where); }    // numeric value

		// internal states:
		// note: most functions implemented inline for efficiency.
		// these calls may account for most run-traffic in application.
		// synonims copied for this very reason. maintain carefully!
		connective reset(void)
		 { return (where=DEF_DIR); }
		void set(connective newval)
		 { where=newval; }
		connective succ(void)
		 // next direction in sequence, or NODIR
		 // RTDIR.succ()==>UPDIR.
		 { return((where=(connective)((where<<1) & 0x000f))); }
		direction &operator++(void)
		 { where=(connective)((where<<1) & 0x000f); return *this; }
		connective pred(void)
		 // previous direction in sequence, or NODIR
		 // DNDIR.pred()==>LTDIR
		 { return((where=(connective)(where>>1))); }
		direction &operator--(void)
		 { where=(connective)(where>>1); return *this; }
		connective opposite(void);
		 // RTDIR.opposite() returns LTDIR
		direction &operator!(void);
		 // non-destructive opposite() - (temporary result)

		// miscelanous
		boolean operator==(direction &other)
		 { return(where==other.where); }
		boolean operator!=(direction &other)
		 { return(where!=other.where); }
		connective clockwise(void);
		 // circular movement clockwise.
		char *name(void);
		 // RTDIR.name() returns "Right"
		char code(void);
		 // RTDIR.code() returns 'R'
		int serial(void);
		 // serial number of direction 1,2,3,4.
		 // DNDIR.serial() returns 4
};


#ifndef SNAV0_C
extern direction		// intended to be read only!
	nodir,
	dir1st,
	rt,
	up,
	lt,
	dn;
#endif

enum intersection_type	{
	J_NUL,		// no directions. not semigraphic
	J_ARROW,	// 1 direction. terminal char
	J_LINE, 	// 2 directions, complementary.
	J_CORNER,	// 2 directions, at right angle
	J_FORK, 	// 3 directions
	J_CROSS 	// all 4 directions
};

class intersection
 { // where can we go from here?

	protected :
		int value;

	public :
		intersection(int dir_set=0x000f)
		 { value=dir_set; }  // by default, the whole range
		intersection(connective onedir)
		 { value=(int)onedir; } 	 // singular set
		intersection(char *name);
		 // construct by name (as obtained from name()), case sensitive.
		int whereto(void)
		 { return(value); }
		int operator()(void)
		 { return value; } // numeric value

		// internal state control (set-theoretic operators):
		// note: bit arithmetic implemented for efficiency
		// (observed to improve overall prog performance).
		// one could do the same with division and multiplication by 2.
		int include(direction &dir) // add direction to set
		 { return((value|=dir.where)); }
		intersection &operator+=(direction &dir)
		 { value|=dir.where; return *this; }
		int exclude(direction &dir) // subtract direction from set
		 { return((value^=(value&dir.where))); }
		intersection &operator-=(direction &dir)
		 { value^=(value&dir.where); return *this; }
		int intersect(intersection &other)
		 // RTDIR+UPDIR+DNDIR.intersect(RTDIR+LTDIR) returns RTDIR
		 { return((value&=other.value)); }
		intersection &operator*=(intersection &other)
		 { value&=other.value; return(*this); }

		// test inclusion:
		boolean includes(direction &member);
		 // test if direction is included
		 // RTDIR+LTDIR+UPDIR+DNDIR.includes(RTDIR) returns TRUE */
		boolean operator>=(direction &member)
		 { return(includes(member)); }
		boolean includes(intersection &subset);
		 // test if all target directions are included
		 // RTDIR+UPDIR+DNDIR.includes(RTDIR+LTDIR) returns FALSE
		boolean operator>=(intersection &subset)
		 { return(includes(subset)); }
		boolean intersects(intersection &paralel);
		 // test if at least one of the target directions is included
		 // RTDIR+UPDIR+DNDIR.intersects(RTDIR+LTDIR) returns TRUE

		// miscelanous:
		boolean operator==(intersection &other)
		 { return(value==other.value); }
		boolean operator!=(intersection &other)
		 { return(value!=other.value); }
		boolean unary(void);
		 // test if direction set includes only one direction
		 // (RTDIR+LTDIR).unary() returns FALSE;
		direction &ask_first(void);
		 // find lowest order member - temporary result
		direction &get_first(void);
		 // get and remove lowest order member - temporary result
		char *names(void);
		 // (RTDIR+UPDIR).names() returns "Right,Up"
		intersection_type type(void);
		 // (RTDIR+UPDIR).type() returns J_CORNER
		char *name(void);
		 // (RTDIR+UPDIR+LTDIR).name() returns "Ufork"
};

enum	biconnective  {
	// intersection (value), binary, at 180 degrees.
	NODIM,
	HDIM,	// "horizontal", panel=row,    cartesian=y
	VDIM	// "vertical",   panel=column, cartesian=x
};

#define NUMDIMS 2

class axis : public intersection  {
// dimension on the plain

	friend class weight_d;

	protected :
		biconnective where;

	public :
		axis(biconnective newdim=NODIM)
		 { set_dirs(newdim); }
		axis(int newdim)
		 { set_dirs((biconnective)newdim); }
		axis(direction &dir);
		 // generalize direction
		 // axis(RTDIR) returns HDIM
		biconnective wherein(void)
		 { return(where); }
		biconnective operator()(void)
		 { return(where); }	     // numeric value

		// internal states
		void set_dirs(biconnective newdim);
		intersection &directions(void);
		 // reduce to direction set - temporary result
		boolean operator==(axis &other)
		 { return(where==other.where); }
		boolean operator!=(axis &other)
		 { return(where!=other.where); }
		biconnective opposite(void);
		 // HDIM.opposite() returns VDIM
		axis &operator!(void);
		 // non-destructive opposite - temp result
		char *name(void);
		 // decode dimension name
		 // HDIM.name() returns "Horizontal"
		char code(void);
		 // dimension "code" - first character returned by name()
		 // HDIM.code() returns "H"
};

#ifndef SNAV0_C
// predefined instances ("figurative constants")
extern axis		// intended to be read only!
	nodim,
	hdim,
	vdim;
extern intersection
	nodirs,
	hdirs,
	vdirs,
	alldirs;
#endif

class axis_wgt : public axis  {

	friend class weight_d;		   // technical, for internal use only

	protected :
		int width;

	public:
		axis_wgt() : axis(NODIM)
		 { width=0; }
		void set_axis(biconnective dirs)
		 { axis::set_dirs(dirs); }
} ;

class weight_d	{
// character weight distributed on the two axes of the plane

	private:
		int value;
		axis_wgt wgt[NUMDIMS+1]; // axis 0 is dummi!

	public:
		weight_d(int hwidth, int vwidth);
		weight_d(int weight_combi=0)
		 { set(weight_combi); } // default typewriter-graphics
		weight_d(weight_d &other);
		int operator()(void)
		 { return value; } // numeric value

		int y(void)
		 { return wgt[HDIM].width; }
		int x(void)
		 { return wgt[VDIM].width; }
		int width(axis &dim)
		 { return wgt[dim()].width; }
		int replace(axis &dim, int width);
		void set(int weight_combi);
		weight_d &mask(weight_d &other);
};

#ifndef SNAV0_C
// predefined instances
extern weight_d 	// intended to be read only!
	twgwgt, 		// 0:0 (TypeWriter Graphics)
	h1v1,
	h1v2,
	h2v1,
	h2v2,
	fullwgt;		 // 3:3 (full pixel block)
#endif

#endif
