-- beadsDefs.mesa

DIRECTORY InlineDefs:FROM"InlineDefs";
BeadsDefs:DEFINITIONS IMPORTS InlineDefs =BEGIN

documents:BOOLEAN=TRUE;

PtrToCardArray:TYPE=POINTER TO ARRAY[0..2] OF CARDINAL;

Byte:TYPE=INTEGER[0..256);

wholeWire:INTEGER=10000;
CR:CHARACTER=’
;
Color:TYPE={none,r,b,g,y,o};
Direction:TYPE={h,v};
BeadType:TYPE={none,all,rg,rb,bg,tt,dd,ttV,ddV,endG,endR,endB,stub,
jctnG,jctnR,jctnB,bf,wireG,wireR,wireB,wireO,endO};

BeadStuff:TYPE=ARRAY [0..2] OF Bead;
BeadPtr:TYPE=LONG POINTER TO Bead;
Bead:TYPE=RECORD[
x,y:INTEGER,
h,w:INTEGER,
beadU,beadD,beadL,beadR,beadT:CARDINAL,
t:BeadType,
external:CARDINAL,
circuit:INTEGER,
wire:BOOLEAN,
dumB1:CARDINAL,
noodle:CARDINAL,
nextBelow:CARDINAL];
WorkStuff:TYPE=ARRAY [0..2] OF Work;
WorkPtr:TYPE=LONG POINTER TO Work;
Work:TYPE=RECORD[
sort:CARDINAL,
newY:INTEGER,
stiff,processed:BOOLEAN,
chain:CARDINAL,
backSort:CARDINAL,
processNo:CARDINAL,
oldY:INTEGER,
seen:CARDINAL];
topBead:CARDINAL;
noBead:CARDINAL;
bead:BeadPtr;
work:WorkPtr;
Desc:TYPE=RECORD[z:CARDINAL,p:BeadPtr];
Coord:TYPE=RECORD[x,y:INTEGER];

desP:POINTER TO desStuff;
desStuff:TYPE=ARRAY BeadType OF DesRec;
DesRec:TYPE=RECORD[
level,short,h:INTEGER,
print:CHARACTER,
toCode:INTEGER,
toWire,toWireR,w,wsR,wsU,stickOutToRight,lessLevel,bitPerLevel:INTEGER←NULL,
bitPerLevelV:INTEGER←NULL,
rotate,rwc,uwc:BeadType←NULL,
color:Color←NULL,
trueIfTrans,blueOnly,rgOnly,rg:BOOLEAN←NULL,ug:BOOLEAN←NULL];
DesArray:TYPE=ARRAY [0..7] OF ARRAY [0..7] OF INTEGER;
Des4:DesArray;
Des5:DesArray;
Des6:DesArray;
Des7:DesArray;

maxY:INTEGER;
maxX:INTEGER;
freeBeadList:CARDINAL;

getWhatYouSee,bendingWires,debugBeads,smallDisplay,fallBack:BOOLEAN;
trackBead:CARDINAL;

topNoodle:CARDINAL=2000; --=1000;

BentWireData:TYPE=RECORD[this,next,old,oldOld:CARDINAL,
thisX,thisY,nextX,nextY,oldX,oldY,firstX,lastX,
dx,dy,nextdx,nextdy,olddx,olddy:INTEGER,done:BOOLEAN,me,lf,rt:Desc];
BentWire:TYPE=POINTER TO BentWireData;

--from BeadsNoodles, all called by fall, some by others
AllocateNoodles:PROCEDURE;
FreeNoodles:PROCEDURE;
StartBentWire:PROCEDURE[CARDINAL,BentWire];--put
AdvanceBentWire:PROCEDURE[BentWire];--put
GetFreeNoodle:PROCEDURE RETURNS[CARDINAL];
ReleaseNoodle:PROCEDURE[CARDINAL,CARDINAL,Desc];
IncrementNoodle:PROCEDURE[this:CARDINAL,x,y:INTEGER];
MergeLeft:PROCEDURE[j,old:CARDINAL];
MergeRight:PROCEDURE[j,old:CARDINAL];
MergeL:PROCEDURE[BentWire];
MergeLPlus:PROCEDURE[BentWire];
MergeR:PROCEDURE[BentWire];
MoveJog:PROCEDURE[this:CARDINAL,x,y:INTEGER];
ReleaseLink:PROCEDURE[j,old:CARDINAL];
InitNoodle:PROCEDURE;
SetNoodle:PROCEDURE[this:CARDINAL,x,y:INTEGER];--put
InsertNewNoodle:PROCEDURE[CARDINAL,BeadPtr] RETURNS[CARDINAL];--put
MakeNewNoodle:PROCEDURE[this:CARDINAL,bpj:BeadPtr,x,y:INTEGER]
RETURNS[new:CARDINAL];
GetNoodle:PROCEDURE[this:CARDINAL]RETURNS[INTEGER,INTEGER,CARDINAL];--put
FollowShortNoodle:PROCEDURE[i:Desc,call:PROCEDURE[Coord]];
FollowNoodle:PROCEDURE[i:Desc,bw:BentWire,call:PROCEDURE];

--from BeadsPut, called by ctl
InitPut:PROCEDURE;
PositionBeads:PROCEDURE;
AdjustTheBottom:PROCEDURE;
PrintCounts:PROCEDURE;
howMuch:Byte;

--from BeadsFall
TrueFall:PROCEDURE;
FallTwo:PROCEDURE[Desc,INTEGER];
--FallOne:PROCEDURE[Desc];
--WireFall:PROCEDURE[Desc] RETURNS[BOOLEAN];
--BeadFall:PROCEDURE[Desc,INTEGER] RETURNS[BOOLEAN];
TurnWiresToBeads:PROCEDURE;--ctl
TurnWire:PROCEDURE[Desc];
FixWires:PROCEDURE;--put ctl
FixWire:PROCEDURE[Desc];
Track:PROCEDURE[i:CARDINAL,s:STRING,y:INTEGER];--merge
GetFreeBead:PROCEDURE RETURNS[Desc];--create

--fromBeadsMerge
ScavageBeads:PROCEDURE;--ctl
ReleaseBead:PROCEDURE[i:Desc];
ScourBeads:PROCEDURE;--ctl

--from BeadsClean
Clean:PROCEDURE;--ctl

--
procedures for looking at things
PrintBeads:PROCEDURE;
PrintGeometry:PROCEDURE[STRING];
PrintLong:PROCEDURE[i:LONG CARDINAL];--ctl put
ShowStats:PROCEDURE[s:STRING,a,b:CARDINAL];
PrintDes:PROCEDURE[x,y,z:POINTER TO DesArray];
StartDisplay:PROCEDURE;
Display:PROCEDURE;--ctl
ManipulateDisplay:PROCEDURE RETURNS[BOOLEAN];--put


Local:PROCEDURE;--ctl
CleanUp:PROCEDURE;--ctl

--Create
FromInputToBeads:PROCEDURE[STRING,BOOLEAN];
InsertBead:PROCEDURE[Bead]RETURNS[Desc];
MakeCircuits:PROCEDURE;
CheckBeads:PROCEDURE;--ctl

--utility routines
Rotate:PROCEDURE;
Reflect:PROCEDURE;--ctl
RotateBack:PROCEDURE;
DeltaZ:PROCEDURE[ii,jj:CARDINAL] RETURNS[Coord];
InitBeadWork:PROCEDURE[Desc];--ctl
WriteOneNumber:PROCEDURE[STRING,INTEGER];--ctl
FindBoundingBox:PROCEDURE;--ctl
EnumerateBeads:PROCEDURE[PROCEDURE[Desc]];
--EnumBeads:PROCEDURE[PROCEDURE[Desc]];
EnumerateSortedBottomUp:PROCEDURE[PROCEDURE[Desc]];

--Test
FullTest:PROCEDURE;--ctl

--////Start Below Defs/////
ProcessPair:TYPE=PROCEDURE[CARDINAL,CARDINAL] RETURNS[ok:BOOLEAN];

noBelow,worstBelow:CARDINAL;
timeV5,timeV6:LONG CARDINAL;

AllocateBelow:PROCEDURE;
FreeBelow:PROCEDURE;
ResetBelow:PROCEDURE;--put fal
MakeBelow:PROCEDURE;--ctl
ShowBelow:PROCEDURE;--ctl
TryAllBelow:PROCEDURE[CARDINAL,ProcessPair] RETURNS[BOOLEAN];--put clr fal

--////End Below Defs/////


Variant:TYPE={tied,left,right,up,down,bead,wire};

Find
:PROCEDURE[b:BeadType,i:CARDINAL] RETURNS[CARDINAL];
NumRelatives:PROCEDURE[i:CARDINAL] RETURNS[CARDINAL];
AndOfRelatives:PROCEDURE[CARDINAL,CARDINAL] RETURNS[BOOLEAN];
Attach:PROCEDURE[Desc,Variant,Desc];
AttachAll:PROCEDURE[i,j:CARDINAL];
Move:PROCEDURE[Desc,Coord] RETURNS[r:BOOLEAN];
Initialize:PROCEDURE;
Related:PROCEDURE[i,j:CARDINAL] RETURNS[BOOLEAN];
Overlap:PROCEDURE[i,j:CARDINAL] RETURNS[BOOLEAN];
MinSepInY:PROCEDURE[i,j:CARDINAL] RETURNS[INTEGER];
MinSepInX:PROCEDURE[i,j:CARDINAL] RETURNS[INTEGER];
Put:PROCEDURE[i:Desc,dx,dy,h,w:INTEGER];
Tied:PROCEDURE[i,j:CARDINAL] RETURNS[BOOLEAN];
InsertJogs:PROCEDURE[i:Desc,horizontal,vertical:Variant];
RemovePaths:PROCEDURE[i,j:CARDINAL,v:Variant];
FindPath:PROCEDURE[i,j:CARDINAL,v:Variant] RETURNS[BOOLEAN];
RoomFor:PROCEDURE[i:CARDINAL] RETURNS[r:BOOLEAN];
Restore:PROCEDURE;
Save:PROCEDURE[a:Desc,abortable,shiftable:BOOLEAN];
Abort:PROCEDURE;
Commit:PROCEDURE;
Remove:PROCEDURE[i:CARDINAL];
ClearShift:PUBLIC PROCEDURE;
Shift:PROCEDURE[dx,dy:INTEGER] RETURNS[BOOLEAN];
Process:PROCEDURE;

--User defs

InitBeadsProgram:PROCEDURE;--ctl
SetUpForRun:PROCEDURE RETURNS[cl,dw,dl,dc,op,sc,dr,bw,dis,lab:BOOLEAN];--ctl
Finalize:PROCEDURE;--ctl
time:LONG CARDINAL;



END..