      SUBROUTINE MOVECOM
C*BEGIN COMMON 
      COMMON SNAP,SNAPSHT(247), 
     +   DATE,REMKL,REMCOM,REMBASE,REMRES,REMTIME,STARKL,BASEKL,
     +   KILLK,KILLC,GALAXY(8,8),CX(10),CY(10),BASEQX(5),BASEQY(5), 
     +   NEWSTUF(8,8),PLNETS(10,5),ISX,ISY,NSCREM,NROMKL,NROMREM,
     +   NSCKILL,NPLANKL,ISATB,BATX,BATY,THINGX,THINGY,
     +   QUAD(10,10),KX(20),KY(20),KPOWER(20),KDIST(20),KSTUF(20),  
     +   FUTURE(10),MESSAGE(5,10),
     +   INKLING,INBASE,INRESOR,INCOM,INTIME,INSTAR,INENRG,INSHLD,
     +   INTORPS,INLSR,INDATE,ENERGY,SHLD,SHLDUP,CONDIT,TORPS,SHIP, 
     +   QUADX,QUADY,SECTX,SECTY,WARPFAC,WFACSQ,LSUPRES,DAMAGE(20), 
     +   LENGTH,SKILL,PASSWD,DIST,DIREC,TIME,BASEX,BASEY,DOCKFAC, 
     +   KLHERE,COMHERE,CASUAL,NHELP,NKINKS,STARCH(8,8),
     +   DEVICE(2,14),IDIDIT,GAMEWON,ALIVE,JUSTIN,RESTING,ALLDONE,
     +   DAMFAC,SHLDCHG,NDEVICE,PLNETX,PLNETY,INORBIT,LANDED,IPLANET,
     +   IMINE,ICRYSTL,INPLAN,NENHERE,ISHERE,NEUTZ,IRHERE,ICRAFT,
     +   IENTESC,ISCRAFT,ISCATE,CRYPROB,ICITE,IPHWHO,
     +   CRACKS(12) 
      INTEGER SHLDUP,CONDIT,QUADX,QUADY,SECTX,SECTY,TORPS,
     +   REMKL,REMBASE,SKILL,REMCOM,GALAXY,STARCH,CX,CY, 
     +   SHIP,ALLDONE,BASEQX,BASEQY,BASEX,BASEY,GAMEWON, 
     +   ALIVE,STARKL,BASEKL,CASUAL,COMHERE,RESTING,SNAP,SHLDCHG, 
     +   THINGX,THINGY,BATX,BATY,PLNETX,PLNETY,PLNETS
      REAL KDIST,KPOWER,LSUPRES,INTIME,INRESOR,INDATE,INSHLD,   
     +   INENRG,INLSR 
	BYTE QUAD
	REAL*8 DEVICE,PASSWD
C*END COMMON 
	LOGICAL*1 IHS,IHR,IHC,IHK,IHE,IHF,IHBLANK,IHDOT,IHP,IHB
	1 ,IHSTAR,IHT,IHQUEST,IHNUM,ISHIP,IENM,IQUAD
      COMMON/HOLLER/IHEOL,IHREAL,IHALPHA,IHS,IHR,IHC,IHK,IHGREEN,IHRED, 
     +IHYELLO,IHDOCKD,IHE,IHF,IHBLANK,IHDOT,IHQUEST,IHP,IHSTAR,IHB
     +,IHT,IHNUM  
      INTEGER COMX,COMY,SUPX,SUPY 
	EQUIVALENCE (CRACKS(5),LOCCOM),(SHIP,ISHIP)
      DATA KRAWLX,KRAWLY/1,1/ 
      IF((NENHERE.EQ.0).OR.(JUSTIN.EQ.1)) RETURN
      IRUN=0
      NBADDYS=COMHERE+ISHERE
      IF(KSTUF(5).NE.0) NBADDYS=((COMHERE*2)+(ISHERE*2)+  
     $  (FLOAT(KLHERE)*1.23) + (FLOAT(IRHERE)*1.5)) / 2.0 
C-------LOOP FOR MOVING ENEMIES BEGINS HERE.    
	I=1
1	IX=KX(I)
	IY=KY(I)
	IENM=QUAD(IX,IY)
	COMX=IX
	COMY=IY
	LOCCOM=I
      IF((KSTUF(5).EQ.0).AND.((IENM.NE.IHS).OR.(IENM.NE.IHC))) GO TO 500
      IF(IENM.NE.IHS) GO TO 3 
C--------CHECK WITH SPY TO SEE IF S.C. SHOULD HI-TAIL IT. 
      IF((KPOWER(LOCCOM).GT.500.) .AND. ((CONDIT.NE.IHDOCKD)
     +  .OR. (DAMAGE(4).NE.0.)))  GO TO 3 
	IRUN=1
	MOTION=-10
      GO TO 8 
C--------DECIDE WHETHER TO ADVANCE, RETREAT, OR HOLD POSITION 
C        AND BY HOW MUCH
3	MOTION=0
	DIST1=KDIST(LOCCOM)
	MDIST=DIST1+0.5
      FORCES=KPOWER(LOCCOM)+100.0*NENHERE +400.*(NBADDYS-1) 
      IF(SHLDUP .EQ. 0) FORCES=FORCES+1000. 
      IF((DAMAGE(3) .EQ. 0) .OR. (DAMAGE(4) .EQ. 0)) GO TO 4
      FORCES=FORCES+1000. 
	GO TO 7
4	EFAC=1.
	TFAC=1.
      IF(DAMAGE(3) .EQ. 0) GO TO 5
	EFAC=0.
	FORCES=FORCES+300.
 5    IF(DAMAGE(4) .EQ. 0) GO TO 6
	TFAC=0.
	FORCES=FORCES+300.
 6    FORCES=FORCES-50.*TORPS*TFAC+0.2*(2500.-ENERGY)*EFAC
     C   +0.6*(1250.-SHLD)*SHLDUP 
 7    IF(FORCES .GT. 1000.0) MOTION=(1.-RANF(0)**2)*DIST1+1.0 
      IF(CONDIT .EQ. IHDOCKD ) MOTION=MOTION-SKILL*(2.-RANF(0)**2)
      IF(MOTION .EQ. 0) MOTION=((FORCES+200.0*RANF(0))/150.0)-5.0 
      IF(MOTION .EQ. 0) GO TO 500 
      IF(IABS(MOTION) .GT. SKILL) MOTION=ISIGN(SKILL,MOTION)
C--------CALCULATE PREFERRED NUMBER OF STEPS TO MOVE COMMANDER
 8    NSTEPS=IABS(MOTION) 
      IF((MOTION .GT. 0) .AND. (NSTEPS .GT. MDIST)) NSTEPS=MDIST
      NSTEPS=MIN0(10,NSTEPS)
      NSTEPS=MAX0(1,NSTEPS) 
C--------COMPUTE PREFERRED VALUES OF DELTA X AND DELTA Y
      MX=SECTX-COMX 
      MY=SECTY-COMY 
      IF(2*IABS(MX) .LT. IABS(MY)) MX=0 
      IF(2*IABS(MY) .LT. IABS(MX)) MY=0 
      IF(MX .NE. 0) MX=ISIGN(1,MX*MOTION) 
      IF(MY .NE. 0) MY=ISIGN(1,MY*MOTION) 
C--------MAIN LOOP TO ATTEMPT TO MOVE COMMANDER <NSTEPS> STEPS
      NEXTX=COMX
      NEXTY=COMY
      QUAD(COMX,COMY)=IHDOT 
      DO 60 LL=1,NSTEPS 
C--------TEST IF PREFERRED POSITION IS AVAILABLE
      LOOKX=NEXTX+MX
      LOOKY=NEXTY+MY
      ASSIGN 10 TO NOEXIT 
      IF(LOOKX.LE.0 .OR. LOOKX.GT.10) IF(MOTION)70,30,30
      IF(LOOKY.LE.0 .OR. LOOKY.GT.10) IF(MOTION)70,10,10
      IQUAD=QUAD(LOOKX,LOOKY) 
C--------DECIDE IF COMMANDER SHOULD RAM 
      IF(IQUAD .NE. ISHIP) GO TO 9010
C--------ONLY LET COMMANDERS RAM THE SHIP.      
      IF((IENM.NE.IHC).AND.(IENM.NE.IHS)) GO TO 9010
C--------WHAMO!   
      CALL RAM(1,IENM,COMX,COMY)
	GO TO 500
 9010 IF(IQUAD .EQ. IHDOT) GO TO 50
C--------TRY TO FUDGE ON Y COORDINATE 
 10   IF(MY.EQ.KRAWLY .OR. MX.EQ.0) GO TO 30
      LOOKY=NEXTY+KRAWLY
      ASSIGN 20 TO NOEXIT 
      IF(LOOKY.LE.0 .OR. LOOKY.GT.10) IF(MOTION)70,20,20
      IF(LOOKX .LE. 0 .OR. LOOKX .GT. 10) IF(MOTION)70,20,20
      IF(QUAD(LOOKX,LOOKY) .EQ. IHDOT) GO TO 50 
 20   KRAWLY=-KRAWLY
C--------TRY TO FUDGE X COORDINATE
 30   IF(MX.EQ.KRAWLX .OR. MY.EQ.0) GO TO 60
      LOOKX=NEXTX+KRAWLX
      ASSIGN 40 TO NOEXIT 
      IF(LOOKX.LE.0 .OR. LOOKX.GT.10) IF(MOTION)70,40,40
      IF(LOOKY .LE. 0 .OR. LOOKY .GT. 10) IF(MOTION) 70,40,40 
      IF(QUAD(LOOKX,LOOKY) .EQ. IHDOT) GO TO 50 
 40   KRAWLX=-KRAWLX
      GO TO 60
 50   NEXTX=LOOKX 
      NEXTY=LOOKY 
 60   CONTINUE
C--------PUT COMMANDER IN NEW PLACE WITHIN SAME QUADRANT
      QUAD(NEXTX,NEXTY)=IENM
      IF(NEXTX.EQ.COMX .AND. NEXTY.EQ.COMY) GO TO 500 
      KX(LOCCOM)=NEXTX
      KY(LOCCOM)=NEXTY
      KDIST(LOCCOM)=SQRT(FLOAT((SECTX-NEXTX)**2 + (SECTY-NEXTY)**2)) 
	MOTION=-1
	IF(KDIST(LOCCOM) .LT. DIST1) MOTION=1
	CALL CRAM3AS
	CALL CRAMEN(IENM)
      IF(MOTION .GT. 0) CALL CRAM(12H ADVANCES TO)
      IF(MOTION .LT. 0) CALL CRAM(12H RETREATS TO)
      CALL CRAMLOC(2,NEXTX,NEXTY) 
      CALL CREND
      GO TO 500 
C--------TRY TO MOVE INTO ADJACENT QUADRANT, AVOIDING NEGATIVE ENERGY 
C        BARRIER, SUPERNOVAE, AND QUADRANTS WITH MORE THAN 8 KLINGONS.
 70   IQX=QUADX+(LOOKX+9)/10-1
      IQY=QUADY+(LOOKY+9)/10-1
      IF(IQX.LT.1 .OR. IQX.GT.8) GO TO NOEXIT 
      IF(IQY.LT.1 .OR. IQY.GT.8) GO TO NOEXIT 
      IF(GALAXY(IQX,IQY) .GT. 899) GO TO NOEXIT 
C--------ALSO AVOID INTRUDING ON ANOTHER COMMANDERS TERRITORY (UNLESS S.C.) 
      IF(IRUN.NE.0) GO TO 86
      IF(IENM .EQ. IHS) GO TO 85
      DO 80 L=1,REMCOM
      IF(CX(L).EQ.IQX .AND. CY(L).EQ.IQY) GO TO NOEXIT
 80   CONTINUE
C------DON'T LET ROMULANS LEAVE.      
      IF(IENM.EQ.IHR) GO TO NOEXIT    
C--------ALSO, REFUSE TO LEAVE IF CURRENTLY ATTACKING STARBASE (UNLESS S.C.)
      IF(BATX.EQ.QUADX .AND. BATY.EQ.QUADY) GO TO NOEXIT
C--------FINALLY, DON'T LEAVE WITH OVER 1000 UNITS OF ENERGY.       
 85   IF(KPOWER(LOCCOM) .GT. 1000.) GO TO NOEXIT
C--------PRINT ESCAPE MESSAGE AND MOVE COMMANDER TO ADJACENT QUADRANT 
86	CALL CRAM3AS
	CALL CRAMEN(IENM)
      CALL CRAM(11H ESCAPES TO) 
      CALL CRAMLOC(1,IQX,IQY) 
      CALL CRAMDMP(23H (AND REGAINS STRENGTH))
C--------HANDLE LOCAL MATTERS RELATING TO COMMANDERS ESCAPE 
      CALL LEAVE
	I=I-1		!NUMBER OF KLINGONS HAS BEEN REDUCED (IN QUAD)
C--------HANDLE GLOBAL MATTERS RELATING TO COMMANDERS ESCAPE
      GALAXY(QUADX,QUADY)=GALAXY(QUADX,QUADY)-100 
      GALAXY(IQX,IQY)=GALAXY(IQX,IQY)+100 
      IF(IENM .EQ. IHC) GO TO 87
      IF(IENM .NE. IHS) GO TO 500     
	ISHERE=0
	ISCATE=0
	IENTESC=0
	ISATB=0
      FUTURE(6)=0.2777+DATE 
      FUTURE(7)=1E38 
	ISX=IQX
	ISY=IQY
	GO TO 500
 87   DO 90 L=1,REMCOM
      IF(CX(L).EQ.QUADX .AND. CY(L).EQ.QUADY) GO TO 100 
 90   CONTINUE
 100  CX(L)=IQX 
      CY(L)=IQY 
      COMHERE=0 
500	I=I+1
	IF(I.LE.NENHERE) GOTO 1
	CALL SORTKL
	RETURN
      END 
