      PROGRAM MAIN
      DIMENSION NSET(10000)
      INCLUDE '/usr/ecn/slam/PARAM.INC'
      COMMON/SCOM1/ATRIB(MATRB), DD(MEQT), DDL(MEQT), DTNOW, II, MFA,
     1MSTOP,NCLNR,NCRDR,NPRNT,NNRUN,NNSET,NTAPE,SS(MEQT),
     2SSL(MEQT), TNEXT, TNOW, XX(MMXXV)
      COMMON QSET(10000)
      EQUIVALENCE (NSET(1),QSET(1))
      EQUIVALENCE (XX(1),CTCAP),(XX(2),XDIR),(XX(3),SPEED),(XX(4),XRUN),
     1 (XX(5),CLOAD),(XX(6),CUR),(XX(7),DROAD),(XX(8),SWATH),
     2 (XX(9),DONE),(XX(10),TLOAD)
      EQUIVALENCE (SS(1),CGRAIN),(SS(2),TGRAIN),(SS(3),YPOS),
     1 (SS(4),XPOS),(SS(5),BUSH)
      EQUIVALENCE (ATRIB(1), TCAP),(ATRIB(2),TBUSH), (ATRIB(3),TUNLD),
     1 (ATRIB(4),HLS),(ATRIB(5),HUS),(ATRIB(6),FLS),(ATRIB(7),FUS),
     2 (ATRIB(8),TTYPE),(ATRIB(9),TSTAR)
      NNSET=10000
      NCRDR=5
      NPRNT=6
      NTAPE=7
      OPEN(UNIT=NCRDR,FILE='fort.5')
      OPEN(UNIT=NPRNT,FILE='fort.6')
      CALL SLAM
      STOP
      END
c
c
      SUBROUTINE STATE
      INCLUDE '/usr/ecn/slam/PARAM.INC'
      COMMON/SCOM1/ATRIB(MATRB), DD(MEQT), DDL(MEQT), DTNOW, II, MFA, 
     1MSTOP,NCLNR,NCRDR,NPRNT,NNRUN,NNSET,NTAPE,SS(MEQT),      
     2SSL(MEQT), TNEXT, TNOW, XX(MMXXV) 
      EQUIVALENCE (XX(1),CTCAP),(XX(2),XDIR),(XX(3),SPEED),(XX(4),XRUN), 
     1 (XX(5),CLOAD),(XX(6),CUR),(XX(7),DROAD),(XX(8),SWATH),       
     2 (XX(9),DONE),(XX(10),TLOAD) 
      EQUIVALENCE (SS(1),CGRAIN),(SS(2),TGRAIN),(SS(3),YPOS), 
     1 (SS(4),XPOS),(SS(5),BUSH)       
      EQUIVALENCE (ATRIB(1), TCAP),(ATRIB(2),TBUSH), (ATRIB(3),TUNLD),
     1 (ATRIB(4),HLS),(ATRIB(5),HUS),(ATRIB(6),FLS),(ATRIB(7),FUS),       
     2 (ATRIB(8),TTYPE),(ATRIB(9),TSTAR)
c
c state eqn for grain in combine tank
c
      CGRAIN=CGRAIN+(SPEED*SWATH*YIELD(XPOS,YPOS)*(5280./43560.)
     1  *XRUN-CUR*CLOAD*TLOAD)*DTNOW
c
c state eqn for grain in truck tank
      TGRAIN=TGRAIN+CUR*CLOAD*TLOAD*DTNOW
c
c state eqn for the x-position of the combine
c
      XPOS=XPOS+SPEED*XDIR*XRUN*5280.*DTNOW
      RETURN
      END
c
c
      SUBROUTINE EVENT(I)
      INCLUDE '/usr/ecn/slam/PARAM.INC'
      COMMON/SCOM1/ATRIB(MATRB), DD(MEQT), DDL(MEQT), DTNOW, II, MFA, 
     1MSTOP,NCLNR,NCRDR,NPRNT,NNRUN,NNSET,NTAPE,SS(MEQT),      
     2SSL(MEQT), TNEXT, TNOW, XX(MMXXV) 
      EQUIVALENCE (XX(1),CTCAP),(XX(2),XDIR),(XX(3),SPEED),(XX(4),XRUN), 
     1 (XX(5),CLOAD),(XX(6),CUR),(XX(7),DROAD),(XX(8),SWATH),       
     2 (XX(9),DONE),(XX(10),TLOAD) 
      EQUIVALENCE (SS(1),CGRAIN),(SS(2),TGRAIN),(SS(3),YPOS), 
     1 (SS(4),XPOS),(SS(5),BUSH)       
      EQUIVALENCE (ATRIB(1), TCAP),(ATRIB(2),TBUSH), (ATRIB(3),TUNLD),
     1 (ATRIB(4),HLS),(ATRIB(5),HUS),(ATRIB(6),FLS),(ATRIB(7),FUS),       
     2 (ATRIB(8),TTYPE),(ATRIB(9),TSTAR)
c
c have reached one end of field 
c
      IF((I.EQ.1).OR.(I.EQ.2)) THEN
c close down motion and disable combine unload
	XRUN=0
	CLOAD=0
c reverse combine travel direction
	XDIR=XDIR*(-1)
c set up time increment for turn
	TIMINC=RNORM(25./3600.,10./3600.,1)
c call to schedule event
	CALL SCHDL(3,TIMING,ATRIB)
c
c end of turn event
c
      ELSE IF(I.EQ.3) THEN
c increment y-position
	YPOS=YPOS+SWATH
c enable combine movement
	XRUN=1
c if have more than one half tank in combine, enable unload
	IF (CGRAIN.GE.100.) CLOAD=1
c
c all done event
c
      ELSE IF(I.EQ.4) THEN
	MSTOP=-1
c
c truck full event
c
      ELSE IF(I.EQ.5) THEN
c send full truck on its way
	CALL STOPA(1)
c if have more than one half tank in combine, enable unload
	IF(CGRAIN.GE.100.) THEN
	  CLOAD =1
        ELSE
	  CLOAD =0
        ENDIF
      ELSE
	PRINT*,'TROUBLE IN EVENT'
      ENDIF
      RETURN
      END
c
c
      FUNCTION USERF(I)
      INCLUDE '/usr/ecn/slam/PARAM.INC'
      COMMON/SCOM1/ATRIB(MATRB), DD(MEQT), DDL(MEQT), DTNOW, II, MFA, 
     1MSTOP,NCLNR,NCRDR,NPRNT,NNRUN,NNSET,NTAPE,SS(MEQT),      
     2SSL(MEQT), TNEXT, TNOW, XX(MMXXV) 
      EQUIVALENCE (XX(1),CTCAP),(XX(2),XDIR),(XX(3),SPEED),(XX(4),XRUN), 
     1 (XX(5),CLOAD),(XX(6),CUR),(XX(7),DROAD),(XX(8),SWATH),       
     2 (XX(9),DONE),(XX(10),TLOAD) 
      EQUIVALENCE (SS(1),CGRAIN),(SS(2),TGRAIN),(SS(3),YPOS), 
     1 (SS(4),XPOS),(SS(5),BUSH)       
      EQUIVALENCE (ATRIB(1), TCAP),(ATRIB(2),TBUSH), (ATRIB(3),TUNLD),
     1 (ATRIB(4),HLS),(ATRIB(5),HUS),(ATRIB(6),FLS),(ATRIB(7),FUS),       
     2 (ATRIB(8),TTYPE),(ATRIB(9),TSTAR)
c
c get distance of field travel in feet, speed in ft/hr, then divide
c   to get the travel time in hrs
c
      DIST=SQRT(XPOS**2+YPOS**2)
      IF(I.EQ.2) THEN
	TSPD=FUS*5280.
      ELSE IF (I.EQ.4) THEN
	TSPD=FLS*5280.
      ELSE
	PRINT*, 'TROUBLE IN USERF'
      ENDIF
      USERF=DIST/TSPD
      RETURN
      END
c
c
      FUNCTION YIELD(X,Y)
      DIMENSION XD(3),YD(3),ZD(9)
      DATA XD/0.0,2640.0,5280.0/,YD/0.0,1320.0,2640.0/
      DATA ZD/42.,46.,50.,43.,47.,51.,44.,48.,52./
c
      YIELD=DTABL(X,Y,XD,YD,ZD,3,3)
      RETURN
      END
c
      FUNCTION DTABL(X,Y,XV,YV,ZV,NX,NY)
      DIMENSION XV(1),YV(1),ZV(1)
      DO 101 IY=2,NY
	  IF(Y.LT.YV(IY)) GO TO 102
101   CONTINUE
      IY=NY
102   IY=IY-1
      DO 103 IX=2,NX
	  IF(X.LT.XV(IX)) GO TO 104
103   CONTINUE
      IX=NX
104   IX=IX-1
      DELX=(X-XV(IX))/(XV(IX+1)-XV(IX))
      IZ=IX+(IY-1)*NX
      ZP=ZV(IZ)+(ZV(IZ+1)-ZV(IZ))*DELX
      IZNX=IZ+NX
      ZPP=ZV(IZNX)+(ZV(IZNX+1)-ZV(IZNX))*DELX
      DTABL=ZP+(ZPP-ZP)*(Y-YV(IY))/(YV(IY+1)-YV(IY))
      RETURN
      END

