      SUBROUTINE RATING(IR)
C     ...END OF GAME ROUTINE.

c	include 'tcommon.for'
	%include tcommon.for

      DIMENSION PWIN(3,4),PLOSE(3,4)
      DATA PWIN/1.,2.,3.,2.,4.,6.,3.,6.,9.,5.,10.,15./
      DATA PLOSE/3*1.,3*2.,3*3.,3*5./
      DATA ILOSE/0/
      GO TO (10,20,30,40,50,60,60),IR 
10    write(*,1)
1     FORMAT(' Collision with Star!  Ship Destroyed')
      GO TO 100
20    ENERGY=ENERGY+DEFL
      DEFL=0.
      IF(ENERGY.LE.0.)GO TO 4000
      write(*,5000)
5000	FORMAT(' Defectors automatically dropped to obtain energy')
      RETURN
4000	write(*,2)
2     FORMAT(' Out of energy')
      GO TO 100
30    write(*,3)
3     FORMAT(' Time''s Up!')
      GO TO 100
40    write(*,4)
4     FORMAT(' Congratulations!!')
      GO TO 100
50    write(*,5)
5     FORMAT(' Crew wiped out!')
      GO TO 100
60    write(*,6)
6     FORMAT(' Warp drive explosion!...everything in quadrant destroyed!
     1!')
      IF(PSP.GE.1.)GO TO 100
      IF(KLNGNS.EQ.0)GO TO 90
      DO 80 J=1,KLNGNS
      IF(XKL(J,1).EQ.0.)GO TO 80
      LEFTK=LEFTK-1
80    CONTINUE
90    IF(NROM.EQ.0)GO TO 100
      DO 70    J=1,NROM
      IF(XROM(J,1).EQ.0.)GO TO 70
      LEFTR=LEFTR-1
70    CONTINUE
100   R=LEFTK+LEFTR
      S=NKL+MROM
      R=(S-R)/S
      KR=R*1000.
      IF(KR.GE.1000)GO TO 999
      IF(KR.GE.900)GO TO 998
      IF(KR.GE.750)GO TO 997
      IF(KR.GE.500)GO TO 996
      IF(KR.GE.250)GO TO 995
      write(*,94)
94    FORMAT(' You really blew it. the incumbent will lose by'/      ' a3576
     1 landslide and don''t be surprised if they rename the Vulcan Bowl'3577
     1/      ' the Klingon Bowl!')
      GO TO 994
995   write(*,95)
95    FORMAT(' Starfleet command hangs you in effigy.'/      ' at least 
     1you won''t be called upon to testify at the stargate hearings!')
      GO TO 994
996   write(*,96)
96    FORMAT(' You seem to be getting the hang of it.'/      ' the presi3585
     1dent may be able to pull the election out by referring to'/      '3586
     1 your efforts as a "tactical redeployment in certain non-'/      '3587
     1 critical sectors of the galaxy."')
      GO TO 994
997   write(*,97)
97    FORMAT(' A most valiant effort. your name will long be remembered 3591
     1with fear'/      ' by the pitiful remnants of the once mighty Klin3592
     1gon-Romulan Empire'/      ' the president would have appointed you3593
     1 attorney general at least..')
      GO TO 994
998   write(*,98)
98    FORMAT(' congratulations!! you have overcome incredible odds'/    3597
     1  ' to come within a hair of victory. your children will be well'/3598
     1      ' provided for.')
      GO TO 994
999   IF(IR.EQ.7)GO TO 993
      write(*,99)
99    FORMAT(' Victory, total and complete, is yours!!!'/      ' your fa3603
     1me will spread throughout the entire  universe! unfortunately,'/  3604
     1    '  the president doesn''t like being knocked off the front pag3605
     1e'/      ' so he has appointed you as permanent ambassador to the 3606
     1outer stotinki'/      ' colonies.')
      GO TO 994
993   write(*,92)
92    FORMAT(' Your place in Valhalla is assured. you have eliminated th3610
     1e last'/      ' major threat to the federation in this era. you ma3611
     1y even inspire'/      ' a new television series!?!?')
994   write(*,93)
93    FORMAT(///)
      write(*,7)KR
7     FORMAT(' Rating = ',I5)
C     ...DETERMINE POINT GAIN OR LOSS FACTOR.
      IF(NRW.EQ.0)GO TO 661
      IWIN=1
      IF(KR.LT.750)IWIN=-1
      IF(KR.LT.900.AND.IRANK.GE.10)IWIN=-1
C     ...DETERMINE INDICES FOR PWIN OR PLOSE. S IS TOTAL ENEMY AT START.3622
      IL=4
      IF(S.LT.100.)IL=3
      IF(S.LT.50.)IL=2
      IF(S.LT.25)IL=1
      IF(IRANK.EQ.NRANKS)GO TO 3005
      IF(LEVEL.EQ.1.AND.IRANK.GE.7.AND.IWIN.GT.0)write(*,6601 )
6601  format(' if you want to gain rank you''ll have to play at a higher3629
     1 level')
      IF(LEVEL.EQ.1.AND.IRANK.GE.7.AND.IWIN.GT.0)IWIN=0
      IF(IL.EQ.1.AND.IRANK.GE.9.AND.IWIN.GT.0)write(*,6602 )
6602  format(' sir if you want to gain rank you''ll have to play against3633
     1 more enemies')
      IF(IL.EQ.1.AND.IRANK.GE.9.AND.IWIN.GT.0)IWIN=0
      IF(IRANK.GE.11.AND.IL.LT.3.AND.IWIN.GT.0)write(*,6602 )
      IF(IRANK.GE.11.AND.IL.LT.3.AND.IWIN.GT.0)IWIN=0
      IF(IRANK.EQ.NRANKS-1.AND.(S.LT.198.OR.LEVEL.LT.3.OR.KR.NE.1000.OR.3638
     1  ITFCTR.GT.10).AND.IWIN.GT.0)write(*,6603 )

6603  FORMAT(' Admiral sir: if you want to become a Fleet Admiral you''l3640
     1l have'/' to play a level 3 game with 198 enemies and a timing fac3641
     1tor of 10 or less.')
      IF(IRANK.EQ.NRANKS-1.AND.(S.LT.198.OR.LEVEL.LT.3.OR.KR.NE.1000.OR.3643
     1  ITFCTR.GT.10).AND.IWIN.GT.0)IWIN=0
3005  IF(IRANK.EQ.NRANKS.AND.IWIN.LT.0)GO TO 1999
      IF(IWIN)1000,1999,3000
C     ...LOSS. DEMOTION.
1000  XPTS=-IWIN*PLOSE(LEVEL,IL)
      XPTS=XPTS-XPTS*R
      XPTS=AMIN1(XPTS,POINTS)
      IF(XPTS.EQ.0.)GO TO 1999
      write(*,1001)XPTS
1001  FORMAT(' Well, you blew',f5.2,' points on that one, meathead.')
      IF(POINTS.GE.1.)POINTS=AMAX1(1.,POINTS-XPTS)
      IF(POINTS.GE.1.)GO TO 1002
      POINTS=AMAX1(0.,POINTS-XPTS)
1002  IF(POINTS.GE.RANKPT(IRANK))GO TO 1003
      IRANK=IRANK-1
      GO TO 1002
1003  write(*,1004)RANKS(1,IRANK),RANKS(2,IRANK),POINTS 
1004  FORMAT(' your present rank is ',2a8,'. # points = ',f6.2) 
      GO TO 2000
C     ...WIN. PROMOTION.
3000  XPTS=IWIN*PWIN(LEVEL,IL)
      XPTS=XPTS*R
      IF(IR.EQ.7)XPTS=XPTS/2.
      write(*,3001)XPTS
3001  format(' you gained ',f5.2,' points on that one.')
      POINTS=POINTS+XPTS
      IF(IRANK.EQ.NRANKS)GO TO 1003
      IF(POINTS.LT.RANKPT(IRANK+1))GO TO 1003
      IRANK=IRANK+1
      IF(IRANK.EQ.NRANKS)GO TO 1003
      POINTS=AMIN1(POINTS,RANKPT(IRANK+1)-1.)
      GO TO 1003
1999  write(*,1998)
1998  FORMAT(' No change in status this time.')
      GO TO 661
2000  READ(3,REC=MMKEY)MNAME,X,MPASS,X6,X7
      WRITE(3,REC=MMKEY)MNAME,POINTS,MPASS,X6,X7
661   WRITE(9,REC=1)MNAME,POINTS,MPASS,ILOSE
	close(3)
	close(9)
      STOP
      END
                                     5024
      IF(RANGE(SHX,X,SHY,Y).GT.STMRAD)GO TO 600           