Unit History;
INTERFACE
USES
{$L-}
{$U-}
		 {$LOAD MQOTP.dumpfile}
			 Memtypes, QuickDraw, OSIntf, ToolIntf, PackIntf,
	 {$LOAD BOPGlobals.dumpfile}
		 {$U Globals.p}	Globals;
	 {$LOAD}
{$U+}
{$E+}
{$L+}
{$ASM+}
{$D-}
{$R-}
{$OV-}
{$SETC DebugFlg:=TRUE}
PROCEDURE DoHistory;
{$IFC DebugFlg}
	{$D+}
	{$R+}
	{$OV+}
PROCEDURE Do2History;
{$ENDC}

IMPLEMENTATION
{***********************************************************************}
{$S HistorySeg}
{************************************************************************}
PROCEDURE GimmeByt(Offset,Count: LongInt);
VAR		 x:			Integer;
BEGIN
	x:=SetFPos(StartDummy,1,Offset);
	x:=FSRead(StartDummy,Count,@HistDum1);
END;
{************************************************************************}
PROCEDURE DoHistory;
VAR
	x,w,Turn,i,j,k,OrgX,OrgY,DotType,Scale1,Scale2,Vert,Val,Max,DType:		Integer;
	y,z,Count:					LongInt;
	ClickFlg,JunkFlag:		Boolean;
	TempStrng:					Str255;
	StrHandle:					StringHandle;
	MyRect:					Rect;
	odkgray,oltgray:			Pattern;
	Kludge:					Integer;
BEGIN
  SaveMap;
	SetCursor(GetCursor(4)^^);
	MyWind:=GetNewWindow(140,NIL,POINTER(-1));
	SetPort(MyWind);
	TempStrng:=ConCat('History of ',CntryNam[HitCntry]);
	SetWTitle(MyWind,TempStrng);
	TextFont(1); TextSize(9);
	StuffHex(@odkgray,'EEBBEEBBEEBBEEBB');
	StuffHex(@oltgray,'1144114411441144');
	StrHandle:=GetString(1200);
	FOR i:=0 to 2 DO
		BEGIN {draw and label axes}
			FOR j:=0 TO 2 DO IF (j=2) OR (Level>i) THEN
				BEGIN
					MoveTo(40+160*i,96*j+15);
					LineTo(40+160*i,95+96*j);
					LineTo(176+160*i,95+96*j);
					FOR Turn:=0 TO 8 DO
						BEGIN
							MoveTo(40+160*i+17*Turn,95+96*j);
							LineTo(40+160*i+17*Turn,97+96*j);
							MoveTo(42+160*i+17*Turn,104+96*j);
							IF Turn<8 THEN MyWrite(Turn+89);
						END;
					IF j>0 THEN
						BEGIN
							FOR Turn:=1 TO 5 DO
								BEGIN
									MoveTo(39+160*i,95+96*j-14*Turn);
									LineTo(41+160*i,95+96*j-14*Turn);
								END;
						END;
					w:=24*i+8*j+1;
					FOR k:=w TO w+7 DO
						BEGIN
							IF (StrHandle^^[k]='A') OR (StrHandle^^[k]='W')
								THEN Kludge:=-2 ELSE Kludge:=0;
							MoveTo(30+160*i+Kludge,96*j+8*(k-w+2)+15);
							DrawChar(StrHandle^^[k]);
						END;
				END;
		END;
	ReleaseResource(pointer(StrHandle));
	MoveTo(6,10);
	DrawString('[WHO:	dark: USSR		 light: USA]		 [WHAT:	gray: anti-govt		solid: pro-govt]	 [parallel lines: major event]');
	x:=FSOpen('SavedGame',0,StartDummy);
	z:=LongInt(@HistDum2)-LongInt(@HistDum1);
	y:=LongInt(@StartDummy)-LongInt(@EndDummy)
		+LongInt(9*ord4(SizeOf(TwoDArr)))
		+LongInt(ord4(SizeOf(TwoFArr)));
	CASE Level OF
		1: j:=12;
		2: j:=17;
		3,4: j:=23;
	END;
	FOR i:=1 TO j DO
		BEGIN
			Scale1:=14; Scale2:=1;
			CASE i OF
				1: BEGIN OrgX:=40; OrgY:=95; DotType:=1; Scale1:=1; Val:=SqrtStrg[HitCntry]; END;
				2: BEGIN OrgX:=40; OrgY:=191; DotType:=4; Val:=MiltAid^^[1,HitCntry]; END;
				3: BEGIN OrgX:=40; OrgY:=191; DotType:=2; Val:=MiltAid^^[2,HitCntry]; END;
				4: BEGIN OrgX:=40; OrgY:=287; DotType:=4; Val:=IntvGovt^^[1,HitCntry]; END;
				5: BEGIN OrgX:=40; OrgY:=287; DotType:=2; Val:=IntvGovt^^[2,HitCntry]; END;
				6: BEGIN OrgX:=40; OrgY:=191; DotType:=5; Val:=InsgAid^^[1,HitCntry]; END;
				7: BEGIN OrgX:=40; OrgY:=191; DotType:=3; Val:=InsgAid^^[2,HitCntry]; END;
				8: BEGIN OrgX:=40; OrgY:=287; DotType:=5; Val:=IntvRebl^^[1,HitCntry]; END;
				9: BEGIN OrgX:=40; OrgY:=287; DotType:=3; Val:=IntvRebl^^[2,HitCntry]; END;
				10: BEGIN OrgX:=200; OrgY:=287; DotType:=4; Scale1:=5; Scale2:=16; Val:=DipAff^^[1,HitCntry]; END;
				11: BEGIN OrgX:=200; OrgY:=287; DotType:=2; Scale1:=5; Scale2:=16; Val:=DipAff^^[2,HitCntry]; END;
				12: BEGIN OrgX:=360; OrgY:=287; DotType:=1; Scale1:=1; Scale2:=3; Val:=MiltPress[HitCntry]; END;
				13: BEGIN OrgX:=200; OrgY:=95; DotType:=1; Scale1:=4; Val:=GPopular[HitCntry]; END;
				14: BEGIN OrgX:=200; OrgY:=191; DotType:=4; Val:=EconAid[1,HitCntry]; END;
				15: BEGIN OrgX:=200; OrgY:=191; DotType:=2; Val:=EconAid[2,HitCntry]; END;
				16: BEGIN OrgX:=200; OrgY:=191; DotType:=5; Val:=Destab[1,HitCntry]; END;
				17: BEGIN OrgX:=200; OrgY:=191; DotType:=3; Val:=Destab[2,HitCntry]; END;
				18: BEGIN OrgX:=360; OrgY:=95; DotType:=4; Scale1:=5; Scale2:=8; Val:=FinlProb[1,HitCntry]; END;
				19: BEGIN OrgX:=360; OrgY:=95; DotType:=2; Scale1:=5; Scale2:=8; Val:=FinlProb[2,HitCntry]; END;
				20: BEGIN OrgX:=360; OrgY:=191; DotType:=4; Val:=Treaty[1,HitCntry]; END;
				21: BEGIN OrgX:=360; OrgY:=191; DotType:=2; Val:=Treaty[2,HitCntry]; END;
				22: BEGIN OrgX:=360; OrgY:=191; DotType:=5; Val:=Pressure[1,HitCntry]; END;
				23: BEGIN OrgX:=360; OrgY:=191; DotType:=3; Val:=Pressure[2,HitCntry]; END;
			END;
			MoveTo(OrgX,OrgY);
			FOR Turn:=1 TO Year-1988 DO
				BEGIN
					DType:=DotType;
					IF Turn=Year-1988
						THEN HistDum1:=Val
						ELSE 
							BEGIN 
								Count:=2; 
								GimmeByt(y+Turn*z+2*NoCntry*(i-1)+2*HitCntry,Count); 
							END;
					IF (i=10) OR (i=11) THEN HistDum1:=HistDum1+128; {wotta kluge!}
					Vert:=(HistDum1*Scale1) div Scale2;
					IF Vert>80 THEN Vert:=80;
					IF Vert<0 THEN Vert:=0;
					x:=OrgX+17*Turn; 
					w:=OrgY-Vert;
					IF (DotType=1) OR (Scale2=8) OR (Scale2=16)
						THEN	{top graph -- lines}
							BEGIN
								IF i=1 THEN
									BEGIN
										Count:=1; 
										HistDum1:=0;
										IF (Turn=(Year-1988))
											THEN BEGIN IF LeftPowr[HitCntry] THEN HistDum1:=256; END
											ELSE GimmeByt(y+Turn*z+49*NoCntry+HitCntry+1,Count);
										IF HistDum1=0 THEN DType:=4 ELSE DType:=2;
										Count:=1; 
										HistDum1:=0;
										IF (Turn=(Year-1988))
											THEN BEGIN IF RebWinFlag[HitCntry] THEN HistDum1:=256; END
											ELSE GimmeByt(y+Turn*z+46*NoCntry+HitCntry+1,Count);
										IF HistDum1<>0 THEN
											BEGIN
												x:=x-17;
												LineTo(x,OrgY-80);
												SetRect(MyRect,x-2,OrgY-82,x+3,OrgY-77);
												CASE DType OF
													2: BEGIN FillRect(MyRect,white); FrameRect(MyRect); END;
													4: FillRect(MyRect,black);
												END;
												MoveTo(x,OrgY-80);
												x:=x+17;
											END;
									END;
								IF i=13 THEN
									BEGIN
										Count:=1; 
										HistDum1:=0;
										IF (Turn=(Year-1988))
											THEN BEGIN IF CoupFlag[HitCntry] THEN HistDum1:=256; END
											ELSE GimmeByt(y+Turn*z+47*NoCntry+HitCntry+1,Count);
										IF HistDum1<>0 THEN
											BEGIN
												x:=x-17;
												LineTo(x,OrgY);
												ClearRect(x-2,OrgY-2,x+3,OrgY+3);
												MoveTo(x,OrgY-2); LineTo(x,OrgY+2);
												MoveTo(x-2,OrgY); LineTo(x+2,OrgY);
												MoveTo(x,OrgY);
												x:=x+17;
											END;
									END;

								x:=x-17; {kludge for shift}
								LineTo(x,w);
								ClearRect(x-2,w-2,x+2,w+2);
								CASE DType OF
									1: BEGIN MoveTo(x,w-2); LineTo(x,w+2); MoveTo(x-2,w); LineTo(x+2,w); END;
									2: BEGIN SetRect(MyRect,x-2,w-2,x+3,w+3);
													 FillRect(MyRect,black);
										 END;
									4: BEGIN SetRect(MyRect,x-2,w-2,x+3,w+3);
													 FillRect(MyRect,white); FrameRect(MyRect);
										 END;
								END;
								MoveTo(x+2,w);
							END
						ELSE	{lower graph -- bars}
							BEGIN
								IF Turn<9 THEN
									BEGIN
										SetRect(MyRect,x-4*(DotType-1),w,x-4*(DotType-2)+1,OrgY+1);
										CASE DotType OF
											2: FillRect(MyRect,black);
											3: IF odd(x)
													 THEN FillRect(MyRect,dkgray) ELSE FillRect(MyRect,odkgray);
											4: FillRect(MyRect,white);
											5: IF odd(x)
													 THEN FillRect(MyRect,oltgray) ELSE FillRect(MyRect,ltgray);
										END;
										FrameRect(MyRect);
									END;
							END;
				END; {of TURN-loop}
		END;	{of i-loop}
	FOR i:=1 TO Level DO IF i<4 THEN
		BEGIN
			FOR Turn:=0 TO Year-1988 DO
				BEGIN
					Count:=1; 
					HistDum1:=0;
					IF (Turn=(Year-1988))
						THEN
							BEGIN
								CASE i OF
									1: IF RebWinFlag[HitCntry] THEN HistDum1:=255;
									2: IF CoupFlag[HitCntry] THEN HistDum1:=255;
									3: IF FinlFlag[HitCntry] THEN HistDum1:=255;
								END;
							END
						ELSE GimmeByt(y+Turn*z+NoCntry*(i+45)+HitCntry+1,Count);
					IF HistDum1<>0 THEN
						BEGIN
							x:=21+160*(i-1)+17*Turn;
							MoveTo(x,95); LineTo(x,15);
							x:=x+4;
							MoveTo(x,95); LineTo(x,15);
						END;
				END;
		END;
	x:=FSClose(StartDummy);
	ClickFlg:=FALSE; 
	InitCursor;
	REPEAT
		JunkFlag:=GetNextEvent(everyEvent,myEvent);
		IF myEvent.what=Mousedown THEN
			BEGIN
				IF FindWindow(myEvent.where,MyWind)=inGoAway THEN
					BEGIN
						IF TrackGoAway(MyWind,myEvent.where) THEN
							BEGIN
								SetPort(MainWind);
								KillControls(MyWind);
								DisposeWindow(MyWind);
								RedrawMap;
								ClickFlg:=TRUE;
							END;
					END;
			END;
	UNTIL ClickFlg;
END;
{************************************************************************}
{$IFC DebugFlg}
PROCEDURE Do2History;
VAR
	x,Turn,i,d:					Integer;
	y,z,Count:					LongInt;
	ClickFlg,JunkFlag:		Boolean;
	TempStrng:					Str255;
BEGIN
  SaveMap;
	MyWind:=GetNewWindow(140,NIL,POINTER(-1));
	SetPort(MyWind);
	TempStrng:=ConCat('History of ',CntryNam[HitCntry]);
	SetWTitle(MyWind,TempStrng);
	x:=FSOpen('SavedGame',0,StartDummy);
	z:=LongInt(@HistDum2)-LongInt(@HistDum1);
	y:=LongInt(@StartDummy)-LongInt(@EndDummy)
		+LongInt(10*ord4(SizeOf(TwoDArr)))
		+LongInt(ord4(SizeOf(TwoFArr)));
	FOR Turn:=0 TO Year-1989 DO
		BEGIN
			FOR i:=1 TO 26 DO
				BEGIN
					Count:=2; 
					d:=2*NoCntry*(i-1); 
					HistDum1:=0;
					IF i>23 THEN BEGIN Count:=1; d:=NoCntry*(i+22)+1; END;
					GimmeByt(y+Turn*z+d+Count*HitCntry,Count);
					MoveTo(50*Turn,11*i); MyWrite(HistDum1);
				END;
		END;
	x:=FSClose(StartDummy);
	ClickFlg:=FALSE;
	REPEAT
		JunkFlag:=GetNextEvent(everyEvent,myEvent);
		IF myEvent.what=Mousedown THEN
			BEGIN
				IF FindWindow(myEvent.where,MyWind)=inGoAway THEN
					BEGIN
						IF TrackGoAway(MyWind,myEvent.where) THEN
							BEGIN
								SetPort(MainWind);
								KillControls(MyWind);
								DisposeWindow(MyWind);
								RedrawMap;
								ClickFlg:=TRUE;
							END;
					END;
			END;
	UNTIL ClickFlg;
END;
{$ENDC}
{************************************************************************}
END.