REM /*******************************************
REM * Star Trek for KM-BASIC web ver 0.2       *
REM * Constructed by Katsumi                   *
REM * kmorimatsu@users.sourceforge.jp          *
REM * http://hp.vector.co.jp/authors/VA016157/ *
REM ********************************************
REM Strings
REM   s$ Current sector map string (64 characters)
REM     ".": None
REM     "E": Enterprise
REM     "K": Klingon starship
REM     "B": Starbase
REM     "*": Star
REM   m$ BGM
REM Dimensions:
REM   d() Damage value is between 0-100 (%)
REM     0% correspond to no damage
REM     d(0): Warp
REM     d(1): Short Range Sensor
REM     d(2): Long Range Sensor
REM     d(3): Phaser control
REM     d(4): Photon tubes
REM     d(5): Damage control
REM     d(6): Shield control
REM     d(7): Mr. Spock
REM  q(0-63) Galaxy data; y*8+x; x,y: 0-7
REM    =K*100+B*10+S, where K, B, and S are # of Klingon ships, Base, and Stars
REM  e() Enterprise data
REM    e(0), e(1): x and y positions in Galaxy
REM    e(2), e(3): x and y positions in current sector
REM    e(4): Energy
REM    e(5): # of photon tubes
REM    e(6): Shields
REM    e(7): 1 if docked to Starbase. 0 if not
REM  g() Global data
REM    g(0): Stardate in the beginning (*100)
REM    g(1): Due date for mission in stardate (*100)
REM    g(2): Current stardate (*100)
REM    g(3): Average of Klingon ship energy when appears (=200)
REM    g(4): Full energy of Enterprise when docked (=3000)
REM    g(5): # of photon tubes when docked (=10)
REM    g(6): Klingon remaining
REM    g(7): # of Klingon starhip in the beginning
REM    g(8): # of starbase in Galaxy
REM k(0-8) Klingon starship data in current sector
REM   k(0),k(3),k(6): Energy
REM   k(1),k(4),k(7): X position in current sector
REM   k(2),k(5),k(8): Y position in current sector
REM The other valiables are used temporarily and can be destroyed in sub routines.
REM */

REM /******************************************
REM * Begin the game                          *
REM * For senarios #1-10,                     *
REM * there must be at least a starbase.      *
REM ******************************************/
print
print "ص ݺ޳ ƭخø ޻"
print " ޱ 0"
print "Senario #(0-999)";
a=val(input$())
if a<1 or 999<a then a=drawcount()%998+1
for i=0 to a:b=rnd():next
color 6:print "ص #";
color 4:print a;
color 6:print " ʼϽ"
if 10<a then gosub INIT:goto INTRO
 REM Senarios #1-10 require at lease one starbase
 label REINIT
 gosub INIT
 if g(8)<1 then REINIT
label INTRO
REM goto SKPINT
color 5
music "Q:1/4=90"
music "L:1/24"
music "K:C"
music "G,6F9E3D2C2B,zB,_5zB,_6B,_12"
print
print "ުѽ  "
print ""
print " USS ײ "
print ""
gosub WAITM
music "G,6G9F3E2D2C2B,5zB,6B,9B,_3"
print " ڷ ";
color 7:print g(1)/100;:color 5
print " "
print "ݶ ݻ޲ ݺò"
print "ޭֳ ";
color 7:print g(7);:color 5
print "  ޷ʽٺ ޱ"
gosub WAITM
music "A,9B,3C3D3E2F2E2G12B_9A3"
print "ݶ  ";
color 7:print g(8);:color 5
print " "
print "ηɳ  ޭ޼"
gosub WAITM
music "G6F,10z2D6C12c12"
print "ĳ "
print "","        ڷ ";
color 7:print g(0)/100:color 5
print "߳ "
print
gosub WAITM
label SKPINT
color 7
gosub DOCKED:e(7)=0
a=e(1)*8+e(0)
q(a)=q(a)%1000
s$=""
gosub SRS
goto CMD

REM /********************
REM * General functions *
REM ********************/

REM Wait for specified periods (times of 1/60 sec)
label WAIT
drawcount 0
label WAIT0
if drawcount()<a then WAIT0
return

REM Wait until music ends
label WAITM
 if 1<music() then WAITM
 return

REM Wait until a key is pressed
label WAITK
 if 0<inkey() then WAITK
label WAITK2
 if inkey()=0 then WAITK2
 return

REM Current sector data
label VALSEC
 return q(e(1)*8+e(0))%1000

REM pseudo SIN*100 from A (100-899)
label SIN
 if A<500 then return 100-(A-300)*(A-300)/400
 return (A-700)*(A-700)/400-100

REM pseudo COS*100 from A (100-899)
label COS
 if A<300 then return 100-(A-100)*(A-100)/400
 if A<700 then return (A-500)*(A-500)/400-100
 return 100-(A-900)*(A-900)/400

REM SQRT from A (0-16384); destroy B and C
label SQRT
 B=0:C=128
 label SQRTLP
  if (B+C)*(B+C)<=A*4 then B=(B+C)/2 else C=(B+C)/2
  if C-B<2 then return B
  goto SQRTLP

REM Get piece (as ascii) in sector at x,y
label SECPC
 if x<0 or y<0 or 7<x or 7<y then return 0
 return asc(s$(y*8+x,1))

REM Show course
label SHWCRS
 print "","γ ò ø޻"
 print " 4 3 2"
 print "  ";chr$(0xef);chr$(0x96);chr$(0xee)
 print " 5- -1"
 print "  ";chr$(0xee);chr$(0x96);chr$(0xef)
 print " 6 7 8"
 print "Course (1.00-8.99)?";
 return

REM Show equipments
label EQPMNT
 color 6
 if a=0 then print "Warp Engine",
 if a=1 then print "Short Range Sensor",
 if a=2 then print "Long Range Sensor",
 if a=3 then print "Phaser Control",
 if a=4 then print "Photon Tubes",
 if a=5 then print "Damage Control",
 if a=6 then print "Shield Control",
 if a=7 then print "Galaxy Map",
 color 7
 return

REM /*************
REM * Initialize *
REM *************/

label INIT
 clear
 dim e(9),q(63),d(7),k(8),g(8)
 m$=""
 REM Position of Enterprise
 for i=0 to 3:e(i)=rnd()/4096:next
 REM Stardate in the beginning (2100-3600)
 g(0)=((RND()/2048)+21)*10000
 REM Due date for mission in stardate (25-34 in addition)
 g(1)=g(0)+2500+(RND()/3277)*100
 REM Current stardate
 g(2)=g(0)
 REM Full energy of Enterprise when docked (=3000)
 g(4)=3000
 REM # of photon tubes when docked (=10)
 g(5)=10
 REM Average of Klingon ship energy when appears (=200)
 g(3)=200
 
 REM    g(6): Klingon remaining
 REM    g(7): # of Klingon starhip in the beginning
 REM    g(8): # of starbase in Galaxy 
 
 REM Initialize Galaxy
 REM In each sector,
 REM   Probability of 3 Klingon ships is 0.02
 REM   Probability of 2 Klingon ships is 0.03
 REM   Probability of 1 Klingon ships is 0.15
 REM   Probability of a starbase is 0.04
 REM   # of stars is between 1-8
 REM As a result, average number of total Klingon ships is 17.3.
 REM Average number of total starbase is 2.6.
 REM # of stars is between 1 and 7
 REM Less than 8 stars cannot completely cover all directions of Enterprise
 REM in center of Galaxy,less than 5 stars cannot in edge of Galaxy, and
 REM less than 3 stars cannot in corner of Galaxy.
 REM Destroy: i,j,a,b,c
 REM   a: # of Klingon starship in a sector
 REM   b: # of Starbase in a sector
 REM   c: # of Stars in a sector
 
 g(6)=0:g(7)=0:g(8)=0
 for j=0 to 7:for i=0 to 7
  a=0:b=0
  r=rnd()
  if r<6554 then a=1
  if r<1638 then a=2
  if r<655  then a=3
  if rnd()<1311 then b=1
  c=(rnd()/4682)+1
  if i<1 or 6<i or j<1 or 6<j then c=(rnd()/8192)+1
  if (i<1 or 6<i) and (j<1 or 6<j) then c=(rnd()/16384)+1
  q(j*8+i)=a*100+b*10+c+1000
  g(6)=g(6)+a
  g(7)=g(7)+a
  g(8)=g(8)+b
 next:next
 return

REM /*******************************************
REM * Initialize Enterprise                    *
REM * This is called in the beggining          *
REM * or when docked to starbase               *
REM * Damages are all zero.                    *
REM * Mr. Spock can use hospital for recovery. *
REM * Enegy will be g(4) (=3000)               *
REM * # of Photon tubes will be g(5) (=10)     *
REM * Shield will be cleared for docking       *
REM *******************************************/

label DOCKED
 for i=0 to 7:d(i)=0:next
 e(4)=g(4):e(5)=g(5):e(6)=0:e(7)=1
 return

REM /*************************************
REM * Check if docked                    *
REM * Parameters:                        *
REM *   s$: sector string                *
REM *   e(2),e(3): Enterprise sector x,y *
REM *************************************/
label CHKDOC
 a=gosub(VALSEC)%100
 e(7)=0
 if a<10 then return
 x=e(2):y=e(3):a=y*8+x
 i=0
 if 0<x and strncmp(s$(a-1),"B",1)=0 then i=1
 if x<7 and strncmp(s$(a+1),"B",1)=0 then i=1
 if 0<y and strncmp(s$(a-8),"B",1)=0 then i=1
 if y<7 and strncmp(s$(a+8),"B",1)=0 then i=1
 if i<1 then return
 music "Q:1/4=90"
 music "L:1/24"
 music "K:C"
 music "z6C6E11z1D6A11z1"
 gosub waitm
 music "G3F^3G3C3E11z1"
 a=6:gosub PEOPLE
 print " ޽"
 gosub waitm
 music "D6d11z1d6"
 print "","ޯݸ  ϼ"
 gosub waitm
 a=5:gosub PEOPLE
 music "d^11z1A^11z1G11z1"
 print "ޯݸ ϼ"
 gosub waitm
 music "G11z1F3G3A6"
 a=d(7)
 gosub DOCKED
 if 0<a then a=2:gosub PEOPLE:print " ָϼ"
 return

REM /******************
REM * Command routine *
REM ******************/

label CMD
 gosub BGM
 if inkey() then CMD
 REM DEBUG
 gosub WAITM
 print
 gosub SPOCK:print
 a=5:gosub PEOPLE:print "޳Ͻ?"
 print "1:NAV 2:SRS 3:LRS 4:PHA 5:TOR"
 print "6:SHE 7:DAM 8:MAP 9:RST 0:HLP"
label CMDLP1
 gosub BGM
 i=inkey()
 if i=0 then CMDLP1
 print
 if i=0x31 then gosub NAV:goto CMD
 if i=0x32 then gosub SRS:goto CMD
 if i=0x33 then gosub LRS:goto CMD
 if i=0x34 then gosub PHA:goto CMD
 if i=0x35 then gosub TOR:goto CMD
 if i=0x36 then gosub SHE:goto CMD
 if i=0x37 then gosub DAM:goto CMD
 if i=0x38 then gosub MAP:goto CMD
 if i=0x39 then gosub RST:goto CMD
 a=1:gosub PEOPLE
 print "Ҳڲؽ · ĵ"
 print "1 NAV: ܰ ײ"
 print "2 SRS: ݷ ݻ"
 print "3 LRS:  ݻ"
 print "4 PHA: ̪ް γ"
 print "5 TOR:  ޮײ"
 print "6 SHE: ޳ޮ "
 print "7 DAM: Ұ ޮ"
 print "8 MAP: ݶ޹ "
 print "9 RST:  Խ"
 goto CMD

REM /****************************************
REM * Mr. Spock suggests next command       *
REM * Destroy:                              *
REM *  a: # of Klingon ships in this sector *
REM *  b: # of Starbase in this sector      *
REM *  c: # of Stars in this sector         *
REM ****************************************/

label SPOCK
 if 0<d(7) then return
 a=2:gosub PEOPLE
 c=q(e(1)*8+e(0))%1000
 a=(c/100):c=c%100
 b=(c/10):c=c%10
 if 0<a then SPKKLI
  REM There is no Klingon ship in this sector
  if 0<d(3) and (0<d(4) or e(5)=0) then print "ݻ޲ ޷ ¶Ͼ",""," ؼϼ":return
  if e(4)<1000 then print "ɺ ٷް ޽";""," ηϼ":return
  if 0<d(6) then print " ޮ ޷Ų";"","ئ ϼ":return
  if 0<d(1) then print "ݷؾݻ ޽","","ئ ϼ":return
  print "÷ݦ ޼","","޷ʼϼ":return
 label SPKKLI
  REM There are Klingon ships in this sector
  if e(6)<200 then print "޶ ܸ ޽":return
  if 0<d(3) and (0<d(4) or e(5)=0) then print "޷ ޷Ͼ","","ƹŹ Ͻ":return
  if 0<d(6) then print " ޮ ޷Ų";"","ƹŹ ޽":return
  if 0<d(1) and a=1 and d(4)=0 and 0<e(5) and c<2 then SPKTOR
  if 0<d(1) then print "ݷؾݻ ޽","","ƹŹ ޽":return
  if 1<a and d(3)=0 then print "̸ ÷ ճ","",:goto SPKPHA
  if 3<c and d(3)=0 and d(4)=0 and 0<e(5) then print "μ ޶ Ͻ","",:goto SPKPHA
  if a=1 and d(4)=0 and 0<e(5) then SPKTOR
  if d(3)=0 then SPKPHA
  if a=3 OR e(5)<2 then print "÷ ޶ Ͻ","","ƹγ ֲ?":return
  print "ޮײ޹ ϸ","","޷ ֲ޽":return
 label SPKPHA
  REM * Phaser energy is divided by the distance between Enterprise and Klingon ship,       *
  REM * and multiplied by 2-3 (*=(2~3)).                                                    *
  print "̪ްγ ¶ϼ"
  j=0
  for i=0 to 2
   b=e(2)-k(i*3+1):c=e(3)-k(i*3+2)
   a=b*b+c*c:a=gosub(SQRT)
   j=j+a*k(i*3)/2
  next
  print "","خ ";j;" ޽"
  return
 label SPKTOR
  print "ޮײ ¶ϼ"
  if d(1)=0 then return
  for i=0 to 2
   b=k(i*3+1)+1:c=k(i*3+2)+1
   if 0<k(i*3) then print "","÷ (";b;",";c;")ƲϽ"
  next
  return

REM /********************
REM * The other members *
REM * Parameter: a      *
REM ********************/
label PEOPLE
 if a=0 then print "÷Ų Ҳڲ޽":return
 color 5
 if a=1 then print " :";
 if a=2 then print "Mr. ߯:";
 if a=3 then print "ϯ :";
 if a=4 then print " :";
 if a=5 then print "ٰ : ";
 if a=6 then print "̰ :";
 if a=7 then print " :";
 if a=8 then print "߭: ";
 color 7
 return

REM /**********************************************
REM * Short Range Sensor                          *
REM * Parameter: s$ (Sector data string)          *
REM * Parameter: d(1) (Damage to SRS)             *
REM * Destroy: a,c,i,j,t$                         *
REM * Destroy: x, y (0-7: position of Enterprise) *
REM * Destroy: b (0-8: available range to show)   *
REM **********************************************/

label SRS
 if len(s$)<64 then gosub SRSINI
 b=0
 if d(1)<8 then b=1
 if d(1)<4 then b=2
 if d(1)<1 then b=8
 print " 12345678";:j=8:gosub SRSINF
 for j=0 to 7
  print j+1;
  for i=0 to 7
   a$=s$(j*8+i,1)
   if b<abs(i-e(2)) or b<abs(j-e(3)) then a$=chr$(0xf0):color 2
   if strncmp(a$,".",1)=0 then a$=chr$(0xa5)
   if strncmp(a$,"K",1)=0 then color 3
   if strncmp(a$,"*",1)=0 then color 6
   if strncmp(s$(j*8+i),"B",1)=0 then a$="B":color 5
   print a$;
   color 7
  next
  gosub SRSINF
 next
 return

REM Information view
label SRSINF
 if j=8 then t$="Stardate":c$=dec$(g(2)/100)
 if j=0 then t$="Mission due":c$=dec$(g(1)/100)
 if j=1 then t$="Condition"
 if j=2 then t$="Quadrant":c$=dec$(e(0)+1)+","+dec$(e(1)+1)
 if j=3 then t$="Sector":c$=dec$(e(2)+1)+","+dec$(e(3)+1)
 if j=4 then t$="Energy":c$=dec$(e(4))
 if j=5 then t$="Photon tubes":c$=dec$(e(5))
 if j=6 then t$="Shields":c$=dec$(e(6))
 if j=7 then t$="Klingon ships":c$=dec$(g(6))
 t$=t$+"        "
 color 1
 print " ";t$(0,14);
 color 7
 if j=1 and 99<gosub(VALSEC) then color 2:c$="RED"
 if j=1 and gosub(VALSEC)<100 then color 4:c$="GREEN"
 if j=1 and gosub(VALSEC)<100 and e(4)<g(4)/10 then color 6:c$="YELLOW"
 if 5<len(c$) then print c$; else print c$
 color 7
 return

REM /*****************************************************
REM * Initialize sector data                             *
REM * Parameters:                                        *
REM *   e(0),e(1): Enterprise X and Y in Galaxy          *
REM *   e(2),e(3): Enterprise X and Y in Sector          *
REM * Return:                                            *
REM *   s$: Sector data string                           *
REM * Destroy:                                           *
REM *   a, b, c: # of Klingon ships, starbase, and stars *
REM *   i,j                                              *
REM *****************************************************/
label SRSINI
 c=gosub(VALSEC)
 a=c/100
 b=((c%100)/10)
 c=c%10
 s$="."
 for i=1 to 6:s$=s$+s$:next
 i=e(3)*8+e(2):s$=s$(0,i)+"E"+s$(i+1)
 i=c:t$="*":gosub SRSISB
 i=b:t$="B":gosub SRSISB
 i=a:t$="K":gosub SRSISB
 for i=0 to 2:k(i*3)=0:next
 if a<1 then return
 REM Give power to Klingon ships
 for i=0 to a-1
  k(i*3)=(RND()+16384)*g(3)/32768
 next
 return

label SRSISB
 if i=0 then return
 j=RND()/512
 if strncmp(s$(j,1),".",1) then SRSISB
 s$=s$(0,j)+t$+s$(j+1)
 i=i-1
 if i<3 then k(i*3+1)=j%8:k(i*3+2)=(j/8)
 goto SRSISB

REM /*********************************************
REM * Long Range Sensor                          *
REM * Parameters:                                *
REM *   e(0), e(1) (Quadrant x,y: 0-7)           *
REM *   q(0-63): Quadrants data                  *
REM * destroy: a,i,j,t$,x,y                      *
REM *********************************************/

label LRS
 if 0<d(2) then a=5:gosub PEOPLE:print " ޽":return
 x=e(0):y=e(1)
 for i=x-1 to x+1
  if 0<=i and i<=7 then print "   ";i+1; else print "    ";
 next
 print ""
 gosub LRSTT
 for j=y-1 to y+1
  if 0<=j AND j<=7 then print j+1; else print " ";
  for i=x-1 to x+1
   print chr$(0x96);
   if i<0 or j<0 or 7<i or 7<j then print "***";: goto LRSL1
    a=q(j*8+i) % 1000
    q(j*8+i)=a
    color 3:print (a/100);: a=a % 100
    color 5:print (a/10); : a=a % 10
    color 6:print a;
    color 7
   label LRSL1
  next
  print chr$(0x96)
  if j<y+1 then gosub LRSTM
 next
 gosub LRSTB
 return

label LRSTT
 t$=chr$(0x95)+chr$(0x95)+chr$(0x95)
 print " ";chr$(0x98);t$;chr$(0x91);t$;chr$(0x91);t$;chr$(0x99)
 return

label LRSTM
 t$=chr$(0x95)+chr$(0x95)+chr$(0x95)
 print " ";chr$(0x93);t$;chr$(0x8f);t$;chr$(0x8f);t$;chr$(0x92)
 return

label LRSTB
 t$=chr$(0x95)+chr$(0x95)+chr$(0x95)
 print " ";chr$(0x9a);t$;chr$(0x90);t$;chr$(0x90);t$;chr$(0x9b)
 return

REM /**********************************
REM * Galaxy Map                      *
REM * Parameters:                     *
REM *   e(0),e(1): Quadrant x,y (0-7) *
REM *   q(0-63): Quadrants data       *
REM * Destroy: x,y,a,t$               *
REM **********************************/
label MAP
 if d(1)=0 then q(e(1)*8+e(0))=GOSUB(VALSEC)
 for x=1 to 8:print "  ";x;:next:print
 gosub MAPTT
 for y=0 to 7
  print y+1;chr$(0x96);
  for x=0 to 7
   if x=e(0) and y=e(1) then print "E"; else print " ";
   if q(y*8+x)<1000 then color 3:print (q(y*8+x)/100); else print "*";
   color 7:print chr$(0x96);
  next
  print
  print " ";chr$(0x96);
  for x=0 to 7
   if q(y*8+x)<1000 then color 5:print ((q(y*8+x)%100)/10); else print "*";
   color 7
   if q(y*8+x)<1000 then color 6:print q(y*8+x)%10; else print "*";
   color 7:print chr$(0x96);
  next
  print
  if y<7 then gosub MAPTM
 next
 gosub MAPTB
 a=1:gosub PEOPLE
 print "  ";g(8);" ";
 label MAPLP1:gosub BGM:if inkey() then MAPLP1
 label MAPLP2:gosub BGM:if inkey()=0 then MAPLP2
 print
 return

label MAPTT
t$=chr$(0x95)+chr$(0x95)
print " ";chr$(0x98);t$;chr$(0x91);t$;chr$(0x91);t$;chr$(0x91);t$;chr$(0x91);
print t$;chr$(0x91);t$;chr$(0x91);t$;chr$(0x91);t$;chr$(0x99)
return

label MAPTM
t$=chr$(0x95)+chr$(0x95)
print " ";chr$(0x93);t$;chr$(0x8f);t$;chr$(0x8f);t$;chr$(0x8f);t$;chr$(0x8f);
print t$;chr$(0x8f);t$;chr$(0x8f);t$;chr$(0x8f);t$;chr$(0x92)
return

label MAPTB
t$=chr$(0x95)+chr$(0x95)
print " ";chr$(0x9a);t$;chr$(0x90);t$;chr$(0x90);t$;chr$(0x90);t$;chr$(0x90);
print t$;chr$(0x90);t$;chr$(0x90);t$;chr$(0x90);t$;chr$(0x9b)
return

REM /***********************************
REM * Navigation (Warp drive)          *
REM * Parameters:                      *
REM *   s$: Sectors data               *
REM *   e(2),e(3): x,y sector position *
REM *   e(0),e(1): x,y quadrant pos.   *
REM *   e(7): docking state            *
REM * Destroy: a,b,c,i,j,t$,x,y        *
REM ***********************************/
label NAV
 a=5:gosub PEOPLE:print "ܰ Ͻ"
 gosub SHWCRS
 t$=input$()+".00"
 i=val(t$)*100+val(t$(2,1))*10+val(t$(3,1))
 if i<100 or 899<i then gosub PEOPLE:a=0:gosub PEOPLE:return
 a=4:gosub PEOPLE
 b=90:if 0<d(0) then print "ݼ  ޽":b=3
 print "ܰ  ޳Ͻ"
 print "Warp Factor (1-";b;")?";
 t$=input$()
 j=val(t$)
 if j<1 or b<j then gosub PEOPLE:a=0:gosub PEOPLE:return
 if (e(4)-e(6)<j+10) then gosub PEOPLE:print "ٷް Ͼ":return
 REM Construct navigation vector
 a=i:x=gosub(COS):y=-gosub(SIN)
 REM Refine navigation vector
 a=x*x+y*y:a=gosub(SQRT)
 x=100*x/a:y=100*y/a
 REM Check if not going out galaxy
 a=e(0)*8+e(2)+(j*x/100)
 b=e(1)*8+e(3)+(j*y/100)
 if a<0 or b<0 or 63<a or 63<b then a=5:gosub PEOPLE:print "ݶ  üϲϽ!";:return
 REM Move in current sector
 REM  c will valid move length
 REM  a=1 if go out from current sector
 REM  a=0 if not
 e(7)=0
 a=1:gosub PEOPLE:print "ܰ ײ!"
 music "Q:1/4=90"
 music "L:1/180"
 music "K:C"
 music "C1C^1D1D^1E1F1F^1G1G^1A1A^1B1"
 gosub waitm
 music "c1c^1d1d^1e1f1f^1g1g^1a1a^1b1"
 gosub waitm
 music "B,1A,^1A,1G,^1G,1F,^1F,1E,1D,^1D,1C,^1C,1"
 gosub waitm
 c=j
 for i=1 to j
  a=e(2)+(i*x/100)
  b=e(3)+(i*y/100)
  if a<0 or b<0 or 7<a or 7<b then i=j:a=1:goto SRSSK1
   t$=s$(b*8+a,1)
   a=0
   if strncmp(t$,"E",1)=0 or strncmp(t$,".",1)=0 then SRSSK1
    a=5:gosub PEOPLE:print "߳ ޲":print "","ݼ òϽ"
    a=0
    c=i-1
    i=j
  label SRSSK1
 next
 x=e(0)*8+e(2)+(c*x/100)
 y=e(1)*8+e(3)+(c*y/100)
 s$=s$(0,e(3)*8+e(2))+"."+s$(e(3)*8+e(2)+1)
 e(0)=(x/8):e(2)=x%8
 e(1)=(y/8):e(3)=y%8
 s$=s$(0,e(3)*8+e(2))+"E"+s$(e(3)*8+e(2)+1)
 REM Warp used some energy
 e(4)=e(4)-c-10
 REM Spent a day or some
 if a=0 then g(2)=g(2)+12*c else c=8:g(2)=g(2)+100
 REM Damage event. (Note that DAMNAV destroies a)
 b=a
 gosub DAMNAV
 REM Judge energy and dates
 gosub JUDGE
 REM if remaining in current sector, KLGN, SRS and return
 if b=0 then gosub CHKDOC:gosub KLGN:gosub SRS:return
 REM Enter to different sector
 a=5:gosub PEOPLE:print "Quadrant";e(0)+1;",";e(1)+1;" ʲϼ"
 s$=""
 gosub SRS
 return

REM /*****************************
REM * Shileds                    *
REM * Parameters:                *
REM *   e(4): Total energy       *
REM *   e(6): Strength of shield *
REM *****************************/
label SHE
 a=7:gosub PEOPLE
 if 0<d(6) then print "  ޽":return
 print "޷ަ ݺϽ"
 print "Shieds (0-";e(4);")?";
 i=val(input$())
 gosub PEOPLE
 if i<0 or e(4)<i then a=0:gosub PEOPLE:return
 e(6)=i
 print "ަ ";i;" ϼ"
 return

REM /**************************************************************************************
REM * Phaser                                                                              *
REM * If shield control is damaged, phaser energy is decreased (*=0.00-0.99)              *
REM * Phaser energy equally hits each Klingon ships. For example, if 900 units are        *
REM * used for 3 Klingon ships, 300 units will hit each ship.                             *
REM * Phaser energy is divided by the distance between Enterprise and Klingon ship,       *
REM * and multiplied by 2-3 (*=(2~3)).                                                    *
REM * Phaser energy to reach Klingon ship must be more than 15% of Klingon ship's energy. *
REM * If less than that, it does not take effect.                                         *
REM * Parameters:                                                                         *
REM *   e(4),e(6): Enterprise energy and shileds values                                   *
REM *   e(2),e(3): Enterprise sector x,y position                                         *
REM *   k(i*3): Klingon energy                                                            *
REM *   k(i*3+1),k(i*3+2): Klingon sector x,y position                                    *
REM *   d(6): Shields damage                                                              *
REM * Destroy: a,b,c,i,j,x,y                                                              *
REM **************************************************************************************/
label PHA
 a=7:gosub PEOPLE
 b=e(4)-e(6)
 c=gosub(VALSEC)/100
 if 0<d(3) then print "̪ް  ޽":return
 if 0<d(1) then print "ݷؾݻ ¶Ų";:print "","÷ ״Ͼ":return
 REM TODO: SRS damage effect will be here
 if c<1 then print "÷ Ͼ":return
 print "̪ްγ ޭ޶":print "","޷ϼ"
 print "Phaser (0-";b;")?";
 i=val(input$())
 if i<1 or b<i then gosub PEOPLE:a=0:gosub PEOPLE:return
 if 0<d(6) then i=i*rnd()/32768:a=7:gosub PEOPLE:print "޶ ۰ ޷";:print "","ܰ ";i;" ϼ!"
 e(4)=e(4)-i
 i=i/c
 music "Q:1/4=90"
 music "L:1/256"
 music "K:C"
 for j=1 to c
  gosub waitm
  music "b'1a'^1a'1g'^1g'1f'^1f'1e'1d'^1d'1c'^1c'1"
  gosub waitm
  music "b1a^1a1g^1g1f^1f1e1d^1d1c^1c1"
 next
 for j=0 to 2
  if 0<k(j*3) then gosub PHASUB
 next
 gosub KLGN
 gosub JUDGE
 return

label PHASUB
 gosub waitm
 music "c^1c1c^1c1c^1c1c^1c1c^1c1c^1c1z20"
 a=7:gosub PEOPLE
 x=k(j*3+1):y=k(j*3+2)
 b=e(2)-x:c=e(3)-y
 a=b*b+c*c:a=gosub(SQRT)
 b=i/a*(65536+rnd())/32768
 if b<15*k(j*3)/100 then b=0
 print "÷(";x+1;",";y+1;") ޷:";b
 k(j*3)=k(j*3)-b
 if 0<k(j*3) then return
 print "","޷ ϼ"
 k(j*3)=0
 a=y*8+x
 s$=s$(0,a)+"."+s$(a+1)
 g(6)=g(6)-1
 a=e(1)*8+e(0)
 q(a)=q(a)-100
 return

REM /*****************************************
REM * Photon Torpedo                         *
REM * Parameters:                            *
REM *   e(2),e(3): Enterprise sector pos x,y *
REM *   e(5): # of photon torpedos remaining *
REM *   d(4): damage                         *
REM *   s$: sector data string               *
REM *   k(i*3): Klingon (if not active, 0)   *
REM *   k(i*3+1),k(i*3+2): Klingon pos x,y   *
REM * Destroy: i,j,a,b,c,x,y,z,t$            *
REM *****************************************/
label TOR
 a=7: gosub PEOPLE
 if e(5)<1 then print "ޮײ ӳ Ͼ":return
 if 0<d(4) then print "ޮײ ޽";:return
 print "ޮײ ޭ޼Ͻ"
 gosub SHWCRS
 t$=input$()+".00"
 i=val(t$)*100+val(t$(2,1))*10+val(t$(3,1))
 if i<100 or 899<i then gosub PEOPLE:a=0:gosub PEOPLE:return
 music "Q:1/4=90"
 music "L:1/256"
 music "K:C"
 gosub waitm
 music "b'1a'1a'^1g'1g'^1f'1f'^1d'^1e'1c'^1d'1b1"
 gosub waitm
 gosub PEOPLE:print "ޮײ ²Ͻ":print "",
 music "c'1a1a^1g1g^1f1f^1d^1e1c^1d1B1"
 gosub waitm
 music "c1A1A^1G1G^1F1F^1D^1E1C^1D1"
 gosub waitm
 e(5)=e(5)-1
 z=-1
 for j=0 to 10
   a=i
   x=e(2)+(j*gosub(COS)/100)
   y=e(3)-(j*gosub(SIN)/100)
   if y*8+x=z then TORL1
   if x<0 or y<0 or 7<x or 7<y then print "Ҳ Ͼ!":j=10:goto TORL1
     print "(";(x+1);",";(y+1);")":print "",
     a=30:gosub WAIT
     z=y*8+x
     if strncmp(s$(z),"*",1)=0 then gosub TORSTR:j=10:goto TORL1
     if strncmp(s$(z),"K",1)=0 then gosub TORKLG:j=10:goto TORL1
     if strncmp(s$(z),"B",1)=0 then gosub TORSBS:j=10:goto TORL1
     REM BGM will be here
   label TORL1
 next
 gosub KLGN
 gosub JUDGE
 return
REM Hit star
label TORSTR
 print "ٷް μ":print "","ϼ!";
 return
REM Hit Klingon
label TORKLG
 music "c^1c1c^1c1c^1c1c^1c1c^1c1c^1c1"
 gosub waitm
 print "Ҳ!"
 s$=s$(0,z)+"."+s$(z+1)
 a=e(1)*8+e(0)
 q(a)=q(a)-100
 g(6)=g(6)-1
 for i=0 to 2
  if k(i*3+1)=x and k(i*3+2)=y then k(i*3)=0
 next
 return
REM Hit starbase
label TORSBS
 music "c^1c1c^1c1c^1c1c^1c1c^1c1c^1c1"
 gosub waitm
 print " ʶ":print "","ϲϼ!";
 s$=s$(0,z)+"."+s$(z+1)
 a=e(1)*8+e(0)
 q(a)=q(a)-10
 g(8)=g(8)-1
 return

REM /************************************************************
REM * Klingon warships hit Enterprise                           *
REM * Hit from Klingon ship is calculated as follows:           *
REM * (Enery of Klingon; 100-300)/(distance; 1-9)*(random: 2-3) *
REM * Parameters:                                               *
REM *   k(i*3): Energy of Klingon                               *
REM *   k(i*3+1),k(i*3+2): Sector x,y position of Klingon       *
REM *   e(4),e(6): Enterprise energy and shild values           *
REM *   e(7): Docking state (1: docked; 0: not docked)          *
REM * Destroy: a,b,c,i,x,y                                      *
REM ************************************************************/
label KLGN
 for i=0 to 2
  if 0<k(i*3) then gosub KLGSUB
 next
 REM Judge shields
 gosub JUDGE
 return
label KLGSUB
 a=7:gosub PEOPLE
 x=k(i*3+1):y=k(i*3+2)
 b=e(2)-x:c=e(3)-y
 a=b*b+c*c:a=gosub(SQRT)
 b=k(i*3)/a*(65536+rnd())/32768
 if e(7) then b=0
 print "÷(";x+1;",";y+1;") ޷:";b
 e(4)=e(4)-b
 e(6)=e(6)-b
 if 0<d(6) then e(6)=4*e(6)/5
 REM    If hit-score is more than 19, damage to an equipment (randomely chosen) occurs with 60% probability.
 REM    If (shield energy) is 50 times more than (hit score), this doesn't happen.
 REM    Damage value to eqipment is caluculated as: 8*(hit score)/(shield energy)+(random:1-5).
 music "Q:1/4=90"
 music "L:1/256"
 music "K:C"
 music "c^1c1c^1c1c^1c1c^1c1c^1c1c^1c1"
 gosub waitm
 if b<20 then return
 if (e(6)/b)>50 then return
 if rnd()>199661 then return
 c=(rnd()/4096)
 if c<7 then a=c:gosub EQPMNT:color 2:print "Ұ"
 if c=7 then a=3:gosub PEOPLE:color 2:print "Mr. ߯ ̼!"
 music "c6z6c12z12"
 gosub waitm
 color 7
 d(c)=d(c)+(8*b/e(6))+(rnd()/8192)+1
 return

REM /*****************************
REM * Damage report              *
REM * Parameters:                *
REM *   d(0): Warp Engine        *
REM *   d(1): Short Range Sensor *
REM *   d(2): Long Range Sensor  *
REM *   d(3): Phaser control     *
REM *   d(4): Photon tubes       *
REM *   d(5): Damage control     *
REM *   d(6): Shield control     *
REM *   d(7): Mr. Spock          *
REM *****************************/
label DAM
 REM when Mr. Spock is injured, Dr. McCoy answers
 if d(7)<1 then a=2:t$="޽" else a=3:t$=""
 gosub PEOPLE
 if 0<d(5) then print "Ұ޺۰װ",""," ";t$:goto DAMSPK
  print "ڿ  Ұ","","·ĵ ";t$
  for i=0 to 6:a=i:gosub EQPMNT:print d(i):next
label DAMSPK
 if d(7)<1 then return
 a=3:gosub PEOPLE:print "Mr. ߯ ","","ޮ ",d(7);t$
 return

REM /*****************************************************************************
REM * Damage to each equipment is repaired by adding warp factor                 *
REM * (if trans-quadrant warp, 8).                                               *
REM * In addition, every warp drive, following event happens:                    *
REM *   In 8% probability, more damage to an equipment occurs: (random: 8-48)    *
REM *   In 12% probability, early repair of an equipment happens: (ramdom: 8-32) *
REM * Parameter:                                                                 *
REM *   c: warp factor                                                           *
REM * Destroy: a,i                                                               *
REM ******************************************************************************/
label DAMNAV
 music "Q:1/4=90"
 music "L:1/256"
 music "K:C"
 for i=0 to 7
  if d(i)<1 then DAMNA2
   d(i)=d(i)-c
   if 0<d(i) then DAMNA2
    d(i)=0
    if i<7 then a=i:gosub EQPMNT:color 4:print " خ":color 7
    if i=7 then a=2:gosub PEOPLE:print " ָϼ"
    music "C6z6C12z12"
    gosub WAITM
  label DAMNA2
 next
 if 6553<rnd() then return
 c=(rnd()/4096)
 if 13107<rnd() then DAMNA3
  REM Damage to equipment
  if c<7 then a=c:gosub EQPMNT:color 2:print "!"
  if c=7 then a=3:gosub PEOPLE:color 2:print "Mr. ߯ ޮ!";
  color 7
  d(c)=d(c)+8+(rnd()/819)
  music "c6z6c12z12"
  gosub WAITM
  return
 label DAMNA3
  REM Repaired earlier
  if d(c)<1 then return
  d(c)=d(c)-8-(rnd()/1024)
  if 0<d(c) and c<7 then a=c:gosub EQPMNT:print "ض ϼ":return
  if 0<d(c) and c=7 then a=3:gosub PEOPLE:print "Mr. ߯ خ","","޲ ":return
  d(c)=0
  if c<7 then a=c:gosub EQPMNT:color 4:print " خ"
  if c=7 then a=2:gosub PEOPLE:color 4:print " ָϼ"
  color 7
  music "C6z6C12z12"
  gosub WAITM
  return

REM /*********************************************
REM * Win-loss judgement                         *
REM * Parameters:                                *
REM *   g(6): Total # of Klingon ships remaining *
REM *   e(4): Enterprise total energy            *
REM *   e(6): Enterprise shields strength        *
REM *   g(1): Due date for mission (stardate*100)*
REM *   g(2): Current stardate (stardate*100)    *
REM *********************************************/
label JUDGE
 if g(6)<1 then goto JUDGEW
 if e(6)<0 then print "ײ ʶ":goto JUDGEL
 if e(4)<11 then a=1:gosub PEOPLE:print "ٷް ·üϯ":goto JUDGEL
 if (g(1)/100)<(g(2)/100) then a=1:gosub PEOPLE:print "ӳ ޶ݶŲ...":goto JUDGEL
 return
label JUDGEW
 music "K:Gm"
 music "Q:1/4=150"
 music "L:1/8"
 music "z12z12z1"
 gosub waitm
 print "ݺݶ ¼"
 music "F4Bdc4A_gf6z"
 gosub waitm
 print "ݺò ܸ߳ ̸"
 music "A_efgef3e3d2zdBdc9z3"
 gosub waitm
 print "ĳ  "
 music "F4Bdc4A_gf6z"
 gosub waitm
 print " "
 music "A_efgef3e3d3zdBc6"
 gosub waitm
 end
label JUDGEL
 print "ܸ߳ ݺò ̸"
 print "  "
 end

label COMSTR:print "COMSTR":return
label COMPTD:print "COMPTD":return
label COMNAV:print "COMNAV":return


REM /***************
REM * Rest routine *
REM ****************/
label RST
 b=8+((g(1)-g(2))/100)*8
 if 99<gosub(VALSEC) then b=1:a=7:gosub PEOPLE:print "÷ Ͻ!"
 a=3:gosub PEOPLE:print "ڸײ Խӳ?"
 print "Periodes(1-";b;")?";
 i=val(input$())
 if i<1 or b<i then a=0:gosub PEOPLE:return
 c=i
 g(2)=g(2)+(i*100/8)
 gosub DAMNAV
 if 99<gosub(VALSEC) then gosub KLGN:return
 a=3:gosub PEOPLE:print "¶ ?"
 gosub JUDGE
 return

REM /**************
REM * BGM routine *
REM **************/
label BGM
 if 1<music() then return
 if gosub(VALSEC)<100 then m$="":return
 if len(m$)=0 then gosub BGMSEL
 music m$(0,2)
 m$=m$(2)
 return
label BGMSEL
 REM Klingon theme
 music "Q:1/4=120"
 music "L:1/24"
 music "K:C"
 m$="G3d6G3d6G3d6G3d3G3d3G3c3F3E3B6B6z6z6"
 m$=m$+"G3d6G3d6G3d6G3d3G3d3G3c3F3B3E6E6z6z6"
 return
