{//-------------------------------------------------------------------------}
{/*                                                                         }
{Copyright (C) 1987, 2009 - Apogee Software, Ltd.                           }
{                                                                           }
{This file is part of Kroz. Kroz is free software; you can redistribute it  }
{and/or modify it under the terms of the GNU General Public License         }
{as published by the Free Software Foundation; either version 2             }
{of the License, or (at your option) any later version.                     }
{                                                                           }
{This program is distributed in the hope that it will be useful,            }
{but WITHOUT ANY WARRANTY; without even the implied warranty of             }
{MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.                       }
{                                                                           }
{See the GNU General Public License for more details.                       }
{                                                                           }
{You should have received a copy of the GNU General Public License          }
{along with this program; if not, write to the Free Software                }
{Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.}
{                                                                           }
{Original Source: 1987-1990 Scott Miller                                    }
{Prepared for public release: 03/19/09 - Joe Siegler, Apogee Software, Ltd. }
{*/                                                                         }
{//-------------------------------------------------------------------------}
{***************************************************************************}
{*                          THE KINGDOM OF KROZ                            *}
{*                       Programmed by Scott Miller                        *}
{*                           Shareware Version                             *}
{*                       This version has 25 levels.                       *}
{*                          began Sept. 15, 1986                           *}
{***************************************************************************}
{$C-,R-,V-,K-}
                       {***************************}
                       {* Last modified:  7/21/89 *}
                       {***************************}

const

   {0}  Null      = 0;    { empty    }  {************************}
   {1}  Slow      = 142;  { l. red   }  {*                      *}
   {2}  Medium    = 153;  { l. green }  {*    Cavern Objects    *}
   {3}  Fast      = 234;  { l. blue  }  {*                      *}
   {4}  Block     = 178;  { brown    }  {************************}
   {5}  Whip      = 244;  { white    }
   {6}  Stairs    = 240;  { grey     }
   {7}  Chest     = 15;   { yellow   }
   {8}  SlowTime  = 232;  { l. cyan  }
   {9}  Gem       = 4;    { random   }
   {10} Invisible = 9;    { d. green }
   {11} Teleport  = 24;   { magenta  }
   {12} Key       = 140;  { l. red   }
   {13} Door      = 236;  { d. cyan  }
   {14} Wall      = 219;  { brown    }
   {15} SpeedTime = 233;  { l. cyan  }
   {16} Trap      = 249;  { grey     }
   {17} River     = 176;  { l. blue  }
   {18} Power     = 2;    { l. blue  }
   {19} Forest    = 176;  { l. green }
   {20} Tree      = 177;  { l. green }
   {21} Bomb      = 157;  { white    }
   {22} Lava      = 178;  { l. red   }
   {23} Pit       = 176;  { grey     }
   {24} Staff     = 231;  { white    }
   {25} Tunnel    = 239;  { white    }
   {26} Freeze    = 159;  { l. cyan  }
   {27} Artifact  = 42;   { random   }
   {28} Quake     = 0;    { blank    }
   {29} IBlock    = 0;    { blank    }
   {30} IWall     = 0;    { blank    }
   {31} IDoor     = 0;    { blank    }
   {32} Stop      = 0;    { blank    }
   {33} Trap2     = 0;    { black    }
   {40} Player    = 1;    { yellow   }

        Objects   = 33;

        XTop      = 65;
        YTop      = 24;
        XBot      = 2;
        YBot      = 2;
        XSize     = 64;
        YSize     = 23;

        Bottom    = 30;

        TMax      = 7;

type
        Field     = string[99];
        Str80     = string[80];
        NameStr   = string[15];
        StrXSize  = string[XSize];
        Regs      = record AX,BX,CX,DX,BP,SI,DI,DS,ES,Flags: integer; end;
        HSType    = record
                     Name        : NameStr;
                     HighScore,
                     HighLevel   : integer;
                    end;
        SaveType  = record
                     S_Level,
                     S_Score,
                     S_Gems,
                     S_Whips,
                     S_Teleports,
                     S_Keys,
                     S_WhipPower,
                     S_Difficulty,
                     S_PX, S_PY  : integer;
                     S_FoundSet  : set of 1..Objects;
                     S_MixUp     : boolean;
                    end;

var
        PF        : array [XBot..XTop,YBot..YTop] of byte;    { Play Field }
        DF        : array [1..Bottom]  of Field;
        Parsed    : array [1..Objects] of integer;
        FP        : array [1..YSize] of StrXSize;{ stores new floor plan }
        SX, SY,                                  { store monster's X, Y  }
        MX, MY,                                  { 1=alive, 0=dead }
        FX, FY    : array [1..1000] of byte;
        T         : array [1..TMax] of integer; { 1=Slow, 2=Medium, 3=Fast }
        Score,                                  { 4=SlowTime, 5=invisible  }
        Gems,                                   { 6=SpeedTime, 7=Freeze    }
        Whips,
        Teleports,
        Keys,
        Difficulty,
        Level,
        GemColor,
        ArtColor,
        WhipPower,
        PX, PY,
        PXOld, PYOld,
        BC, BB,
        SkipTime,
        SNum,
        MNum,
        FNum,
        STime,
        MTime,
        FTime,
        XDir,
        YDir,
        loop,
        width,
        C1,
        xl,xr,
        yl,yr,
        x,y,i,a,b : integer;
        xb, yb    : byte;
        ch        : char;
        StrVal    : Field;
        Result    : Regs;
        MixUp,                   { true if levels appear randomly }
        Done,
        FastPC,
        Restart   : boolean;
        FoundSet  : set of Null..Objects;
        I_Score,
        I_Gems,
        I_Whips,
        I_Teleports,
        I_Keys,
        I_WhipPower,
        I_Difficulty,
        I_PX, I_PY  : integer;
        I_FoundSet  : set of 1..Objects;
        SaveStuff   : SaveType;
        SaveFile    : file of SaveType;
        HSList      : array [1..15] of HSType;
        HSFile      : file of HSType;
        FileName    : file;

label   NewGame;

procedure Restore_Border;forward;

procedure Col(Num:byte);
 begin if C1=9 then textcolor(Num) else textcolor(7) end;

procedure ColRan(Num:byte);
 begin textcolor(Num) end;

procedure Bak(Num:byte);
 begin if C1=9 then textbackground(Num) else textbackground(0) end;

procedure Bor(Num:byte);
 begin
  if C1=9 then
    begin
      with Result do begin AX:=$B00;BX:=Num; end;
      intr($10,Result);
    end;
 end;

procedure CK; { ClearKeys }
 begin
  while keypressed do read(kbd,ch);
 end;

procedure Print(XPos,YPos:byte; Message:Str80);
 begin
  gotoxy(XPos,YPos);
  write(Message);
 end;

procedure PrintNum(YPos:byte; Num:integer);
 begin
  gotoxy(70,YPos);
  write('       ');
  str(Num,StrVal);
  if (YPos=2) and (Score>0) then StrVal:=StrVal+'0';
  if (YPos=11) and (WhipPower>3) then StrVal:='+'+StrVal;
  gotoxy(73-length(StrVal) div 2,YPos);
  write(StrVal);
 end;

procedure Flash(XPos,YPos:byte;Message:Str80);
 var Counter : integer;
 begin
  Counter := 0;
  while keypressed do read(kbd,ch);
  repeat
   Counter := Counter + 1;
   if Counter > 15 then Counter := 1;
   colran(Counter);
   print(XPos,YPos,Message);
  until keypressed;
  Restore_Border;
 end;

procedure Cur(Num:byte);
 begin
  with Result do
   begin
    AX:=$100;
    case Num of
     1:CX:=$707; { Underline   }
     2:CX:=$8;   { Solid Block }
     3:CX:=$2000;{ Invisible   }
    end;
   end;
   intr($10,Result);
 end; { Cur }

procedure FootStep;
  var x:integer;
 begin
  for x:=1 to 23 do sound(random(550)+350);
  nosound;delay(120);
  for x:=1 to 30 do sound(random(50)+150);
  nosound;
 end;

procedure GrabSound;
  var x:integer;
 begin
  for x:=1 to 65 do sound(random(1000)+1000);nosound
 end;

procedure BlockSound;
  var x:integer;
 begin
  for x:= 150 downto 35 do begin sound(x);delay(1);end;
  nosound;
 end;

procedure NoneSound;
  var x:integer;
 begin
  for x:=1 to 5 do
   begin
    sound(400);delay(10);nosound;delay(10);
    sound(700);delay(10);nosound;delay(10);
   end;
 end;

procedure Static;
  var x,y:integer;
 begin for x:= 1 to 15 do bor(random(16));bor(1);
  for x:=1 to 25 do
   case random(2) of
    0:for y:=1 to random(60)+10 do sound(random(4000)+3000);
    1:begin nosound;delay(random(30)); end;
   end; nosound;
 end; { Static }

procedure Play(Start,Stop,Speed:integer);
  var x:integer;
 begin
  if Start<=Stop then
   for x:=Start to Stop do begin sound(x);delay(Speed)end
  else
   for x:=Start downto Stop do begin sound(x);delay(Speed)end;
  nosound;
 end; { Play }

procedure Update_Info;
 begin
  bak(7);textcolor(4);
  printnum(2,Score);
  printnum(5,Level);
  if Gems> 9 then printnum(8,Gems)
  else begin textcolor(20);printnum(8,Gems);textcolor(4);end;
  printnum(11,Whips);
  printnum(14,Teleports);
  printnum(17,Keys);
  textbackground(0);
 end; { Update_Info }

procedure AddScore(What:integer);
 begin
  case What of
   {Monsters}  1..3:Score:=Score+What;
   {Block}     4,14:if Score > 2 then Score:=Score-2;
   {Whip}      5:Score:=Score+1;
   {Stairs}    6:Score:=Score+Level*5;
   {Chest}     7:Score:=Score+50;
   {Gem}       9:Score:=Score+(Level div 2)+1;
   {Invisible} 10:Score:=Score+25;
   {Teleport}  11:Score:=Score+2;
   {SpeedTime} 15:Score:=Score+5;
   {Trap}      16:if Score>5 then Score:=Score-5;
   {Lava}      22:if Score>100 then Score:=Score+100;
   {Border}    20:if Score > (Level*1) then Score:=Score-(Level div 2);
   {Artifact}  27:Score:=Score+100;
  end;
  Update_Info;
 end; { AddScore }

procedure Screen;
 begin
  textbackground(0);
  clrscr;
  cur(3);
  BB:=0;BC:=0;
  Flash(1,10,'Color or Monochrome (C/M)?');
  read(kbd,ch);
  if upcase(ch)='M' then
   begin
    textmode(BW80);
    C1:=15;
   end
  else
   C1:=9;

  clrscr;
  gotoxy(9,17);
  textcolor(7);
  write('If you have an older PC (like an XT model) choose "S" for Slow.');
  gotoxy(10,19);
  write('If you have a PC AT, 80386 chip, etc., choose "F" for Fast.');
  gotoxy(32,21);
  write('(Default = Slow)');
  Flash(29,14,'Slow or Fast PC (S/F)?');
  read(kbd,ch);
  if upcase(ch) = 'F' then FastPC := true else FastPC := false;
  clrscr;
 end;

procedure Border;
 begin
  gotoxy(1,24);
  BC:=random(8)+8;
  BB:=random(7)+1;
  col(BC);bak(BB);
  for i:=1 to 66 do
   begin gotoxy(i,25);write(chr(Block));gotoxy(i,1);write(chr(Block));end;
  for i:=2 to 24 do
   begin
    gotoxy(1,i);write(chr(Block));gotoxy(66,i);write(chr(Block));
   end;
  bak(0);
  end; { Border }

procedure Restore_Border;
 begin    { Restores the bottom line of the border }
  gotoxy(4,25);
  col(BC);bak(BB);
  for x:=1 to 28 do write(chr(Block),chr(Block));
  bak(0);
 end; { Restore_Border }

procedure Init_Screen;
 begin
  Restart  := false;
  Score    := 0;
  Level    := 1;   {1}
  case Difficulty of
    9:Gems:=300;
    8:Gems:=20;{20}
    5:Gems:=10;
    2:Gems:=5
  end;
  Whips    := 1;   {1}
  Teleports:= 0;   {0}
  Keys     := 0;   {0}
  WhipPower:= 2;   {2}
  FoundSet := [];
  if MixUp then
   begin
    Level    :=4;
    Gems     :=20;
    Whips    :=10;
    Teleports:=5;
    Keys     :=0;
    FoundSet :=[0..Objects];
   end;
  PX       := random(XSize)+XBot;
  PY       := random(YSize)+YBot;
  if FastPC then STime := 10 else STime := 3;
  if FastPC then MTime := 8  else MTime := 2;
  if FastPC then FTime := 6  else FTime := 1;
  SkipTime := 0;
  for x:=1 to TMax do T[x]:=-1; {***** reset timers *****}
  T[1]     := 5;
  T[2]     := 6;
  T[3]     := 7;
  if C1=9 then
   begin
    window(67,1,80,25);
    bak(1);
    clrscr;
    window(1,1,80,25);
   end;
  col(14);
  print(71,1,'Score');
  print(71,4,'Level');
  print(71,7,'Gems');
  print(71,10,'Whips');
  print(69,13,'Teleports');
  print(71,16,'Keys');
  col(11);bak(4);
  print(70,19,'OPTIONS');
  bak(1);
  gotoxy(70,20);col(15);write('W');col(7);write('hip');
  gotoxy(70,21);col(15);write('T');col(7);write('eleport');
  gotoxy(70,22);col(15);write('P');col(7);write('ause');
  gotoxy(70,23);col(15);write('Q');col(7);write('uit');
  gotoxy(70,24);col(15);write('S');col(7);write('ave');
  gotoxy(70,25);col(15);write('R');col(7);write('estore');
 end; { Init_Screen }

(****************************************************************************)
{$ITitle3.Inc      Includes all defined levels                            *}
(****************************************************************************)

procedure High_Score(PlayAgain:boolean);
  var x,
      Place : integer;
      Stop  : boolean;
 begin
  CK;
  window(2,2,XSize+1,YSize+1);
  textbackground(0);clrscr;
  window(1,1,80,25);
  cur(3);
  assign(HSFile,'KINGDOM.HS');
  {$I-}
  reset(HSFile);
  {$I+}
  if IOResult <> 0 then
   begin
    close(HSFile);
    rewrite(HSFile);
    for x:=1 to 15 do
     with HSList[x] do
      begin
       case x of
        1:begin Name:='Scott Miller';HighScore:=4420;HighLevel:=11;end;
        2:begin Name:='Amy Maher';HighScore:=731;HighLevel:=2;end
        else begin Name:='-----';HighScore:=0;HighLevel:=0;end
       end;
       write(HSFile,HSList[x]);
      end;
    close(HSFile);
   end;
  col(9);
  gotoxy(27,3);
  write('KINGDOM OF KROZ');
  col(11);
  gotoxy(16,5);write('NAME');
  gotoxy(33,5);write('HIGH SCORE');
  gotoxy(49,5);write('LEVEL');
  reset(HSFile);
  for x:=1 to 15 do
   read(HSFile,HSList[x]);
  Place:=1;
  Stop:=false;
  repeat
   if Score>HSList[Place].HighScore then Stop:=true;
   Place:=Place+1;
   if not Stop and (Place>15) then Place:=100;
  until Stop or (Place>15);
  Place:=Place-1;
  if Place<16 then
   for x:=15 downto Place do
    HSList[x]:=HSList[x-1];
  with HSList[Place] do
   begin
    Name:='';
    HighScore:=Score;
    HighLevel:=Level;
   end;

  for x:=1 to 15 do
   begin
    if odd(x) then col(14) else col(10);
    with HSList[x] do
     begin
      gotoxy(13,x+6);write(x:2);
      gotoxy(16,x+6);write(Name);
      gotoxy(36,x+6);write(HighScore);if HighScore>0 then write('0');
      gotoxy(50,x+6);write(HighLevel);
     end;
   end;
  close(HSFile);
  CK;
  if Place<16 then
   begin
    bak(4);
    gotoxy(16,Place+6);
    write('               ');
    col(4);bak(7);
    gotoxy(15,23);
    write('Enter your name then press <enter>.');
    col(15);
    bak(4);
    buflen:=15;
    gotoxy(16,Place+6);
    cur(2);
    readln(HSList[Place].Name);
    cur(3);
    rewrite(HSFile);
    for x:=1 to 15 do write(HSFile,HSList[x]);
    close(HSFile);
   end;
  bak(0);
  gotoxy(15,23);
  write('                                   ');
  for x:=1 to 999 do
   begin
    SX[x]:=0;SY[x]:=0;
    MX[x]:=0;MY[x]:=0;
    FX[x]:=0;FY[x]:=0;
   end;
  if PlayAgain then
   Flash(14,25,'Do you want to play another game (Y/N)?')
  else
   begin
    ch:='N';
    Flash(21,25,'Press any key to continue.');
   end;
   if PlayAgain then read(kbd,ch);
   if upcase(ch) <> 'N' then Restart:=true
   else
    begin
     bak(0);col(15);bor(0);cur(1);
     clrscr;
     if not PlayAgain then
      begin
       gotoxy(1,2);
       writeln('Congratulations for completing KINGDOM OF KROZ!');
      end
     else
      begin
       gotoxy(15,2);
       writeln('KINGDOM OF KROZ');
      end;
     CK;
     col(7);
     bor(0);
     bak(0);
     clrscr;
     gotoxy(33,1);write('KINGDOM OF KROZ');
     gotoxy(26,2);writeln('An Apogee Software Production');
     gotoxy(1,wherey+1);
     writeln('Other shareware games available from Scott Miller:');
     writeln;
     write(  '* SUPERNOVA - An epic text adventure, with character graphics and sound effects.');
     writeln('     Understands full-sentence input and over 1000 words.  ($10)');
     writeln;
     writeln('* BEYOND THE TITANIC - A fantastic adventure of exploration and survival.');
     writeln('     Sound effects and 16 color screens.  ($8)');
     writeln;
     writeln('* TREK TRIVIA - VOLUMES 1 through 10 -  Each volume has 100 Star Trek trivia');
     writeln('     questions, and is played like a game.  16-colors and sound effects, too.');
     writeln('     $4 per volume (plus $2 per order for postage and disks), order all 10');
     writeln('     volumes and pay only $30, a $12 savings!');
     writeln;
     writeln('* CAVERNS OF KROZ (40 levels) and DUNGEONS OF KROZ (30 levels).');
     writeln('     Two sequels to KINGDOM OF KROZ with new screens.  ($15 for both)');
     writeln;
     writeln('Have a nice day.');
     writeln;
     writeln('Scott Miller');
     cur(1);
     CK;
     HALT;
    end;
 end; { High_Score }

procedure Won;
 begin
  Border;
  CK;
  col(16);bak(BB);
  print(10,1,'YOUR QUEST FOR THE AMULET OF KROZ WAS SUCCESSFUL!');
  bak(0);
  High_Score(false);
 end; { Dead }

procedure Dead;
 begin
  if Gems>9 then textcolor(4)
  else
   begin Gems:=0;textcolor(20);end;
  bak(7);
  gotoxy(71,8);
  write('     ');
  str(Gems,StrVal);
  gotoxy(73-length(StrVal) div 2,8);
  write(StrVal);
  textbackground(0);
  for x:=150 downto 5 do
   begin
    gotoxy(PX,PY);
    textcolor(x);bak(random(8));
    write(chr(Player));
    sound(x*x);
   end; nosound;
  CK;
  col(16);bak(BB);
  print(26,1,'YOU HAVE DIED!!');
  bak(0);
  repeat
   colran(random(16));
   gotoxy(PX,PY);
   write('*');
   print(21,25,'Press any key to continue.');
  until keypressed;
  Border;
  High_Score(true);
 end; { Dead }

procedure New_Gem_Color;
 begin
  repeat
   GemColor:=random(15)+1;
   if (C1=15) and (GemColor in[1,9]) then GemColor:=11;
  until GemColor <> 8;
  repeat
   ArtColor:=random(15)+1;
   if (C1=15) and (ArtColor in[1,9]) then ArtColor:=11;
  until (ArtColor<>8) and (ArtColor<>GemColor);
 end;

(****************************************************************************)
{$ILevels3.Inc       Includes all defined levels                          *}
(****************************************************************************)

procedure Parse_Field;
  var Slot,
      Counter,
      test  : integer;
      Fetch : string[4];
 begin
  Slot:=1;
  Counter:=1;
  repeat
   Fetch:=copy(DF[Level],Slot,3);
   if Fetch[1] = ' ' then begin Fetch:=Fetch[2]+Fetch[3];end;
   if Fetch[1] = ' ' then Fetch:=Fetch[2];
   val(Fetch,Parsed[Counter],test);
   Slot:=Slot+3;
   Counter:=Counter+1;
  until Counter > Objects;
 end; { Parse_Field }

procedure Create_Playfield;
  var x,y,
      Obj,
      Loop,
      XSpot,
      YSpot : integer;
 begin
  SNum:=Null; MNum:=Null; FNum:=Null;
  for x:=1 to 999 do
   begin                        {* reset monster's X, Y *}
    SX[x]:=0;SY[x]:=0;
    MX[x]:=0;MY[x]:=0;
    FX[x]:=0;FY[x]:=0;
   end;
  New_Gem_Color;
  for x:=XBot to XTop do for y:=YBot to YTop do PF[x,y]:=Null;
  PF[PX,PY]:=40;
  Parse_Field;
  for Obj:=1 to Objects do
    if Parsed[Obj] > Null then
     for Loop:=1 to Parsed[Obj] do
      begin
       Done:=false;
        repeat
         XSpot:=random(XSize)+XBot;
         YSpot:=random(YSize)+YBot;
         if PF[XSpot,YSpot] = Null then
          begin
           PF[XSpot,YSpot]:=Obj;Done:=true;
           case obj of
            1:begin SNum:=SNum+1;SX[SNum]:=XSpot;SY[SNum]:=YSpot;end;
            2:begin MNum:=MNum+1;MX[MNum]:=XSpot;MY[MNum]:=YSpot;end;
            3:begin FNum:=FNum+1;FX[FNum]:=XSpot;FY[FNum]:=YSpot;end;
           end;
          end;
        until Done;
      end;
 end; { Create_Playfield }

procedure Display_Playfield;
  var XLoop,
      YLoop : integer;
 begin
  for XLoop:=XBot to XTop do
   for YLoop:=YBot to YTop do
    if PF[XLoop,YLoop] > Null then
     begin
      gotoxy(XLoop,YLoop);
      case PF[XLoop,YLoop] of
       {Slow}      1:begin textcolor(12);write(chr(Slow))end;
       {Medium}    2:begin textcolor(10);write(chr(Medium))end;
       {Fast}      3:begin textcolor(C1);write(chr(Fast))end;
       {Block}     4:begin textcolor(6);write(chr(Block))end;
       {Whip}      5:begin textcolor(15);write(chr(Whip))end;
       {Stairs}    6:begin textbackground(7);textcolor(16);write(chr(Stairs));bak(0)end;
       {Chest}     7:begin textcolor(14);bak(4);write(chr(Chest));bak(0)end;
       {SlowTime}  8:begin textcolor(11);write(chr(SlowTime))end;
       {Gem}       9:begin textcolor(GemColor);write(chr(Gem))end;
       {Invisible} 10:begin textcolor(2);write(chr(Invisible))end;
       {Teleport}  11:begin textcolor(13);write(chr(Teleport))end;
       {Key}       12:begin textcolor(12);write(chr(Key))end;
       {Door}      13:begin textbackground(5);textcolor(3);write(chr(Door));bak(0)end;
       {Wall}      14:begin textcolor(6);write(chr(Wall))end;
       {SpeedTime} 15:begin textcolor(11);write(chr(SpeedTime))end;
       {Trap}      16:begin textcolor(7);write(chr(Trap))end;
       {River}     17:begin textcolor(25);bak(1);write(chr(River));bak(0)end;
       {Power}     18:begin textcolor(C1);bak(4);write(chr(Power));bak(0)end;
       {Forest}    19:begin textcolor(6);bak(2);write(chr(Forest));bak(0)end;
       {Tree}      20:begin textcolor(6);bak(2);write(chr(Tree));bak(0)end;
       {Bomb}      21:begin textcolor(15);write(chr(Bomb))end;
       {Lava}      22:begin textcolor(12);bak(4);write(chr(Lava));bak(0)end;
       {Pit}       23:begin textcolor(7);write(chr(Pit))end;
       {Staff}     24:begin col(15);bak(1);write(chr(Staff));bak(0)end;
       {Tunnel}    25:begin col(15);write(chr(Tunnel))end;
       {Freeze}    26:begin col(11);write(chr(Freeze))end;
       {Artifact}  27:begin col(ArtColor);write('*')end;
       {Quake}     28:;
       {IBlock}    29:;
       {IWall}     30:;
       {IDoor}     31:;
       {Stop}      32:;
       {Trap2}     33:;
       {Player}    40:begin bak(7);col(16);write(chr(Stairs));bak(0);end
       {LETTERS} else begin col(GemColor);bak(6);if GemColor=6 then col(0);
                       write(upcase(chr(PF[XLoop,YLoop])));bak(0);
                      end;
      end;
     end;
 end; { Display_Playfield }

function GetKey:byte;
      procedure BadKeySound;
       begin
        sound(540);delay(40);
        for x:=1 to 4 do
         begin sound(100);delay(15);nosound;delay(15)end;
        nosound;
       end;
  var key:char;
 begin
  if keypressed then
   begin
    read(kbd,key);
    if key=#27 then
     if keypressed then { key must be an extended character }
      begin
       read(kbd,key);
       if key in [#72,#80,#77,#75,#71,#79,#73,#81] then
        GetKey:=ord(key)+100
       else begin BadKeySound;GetKey:=Null;end;
      end
     else GetKey:=81
    else { key must be an alpha character }
     case ord(key) of
      119,87:GetKey:=87;       { Whip     }
      116,84:GetKey:=84;       { Teleport }
      112,80:GetKey:=80;       { Pause    }
      113,81:GetKey:=81;       { Quit     }
      115,83:GetKey:=83;       { Save     }
      114,82:GetKey:=82;       { Restore  }
      117,85:GetKey:=171;      { U-NW     }
      105,73:GetKey:=172;      { I-North  }
      111,79:GetKey:=173;      { O-NE     }
      106,74:GetKey:=175;      { J-West   }
      107,75:GetKey:=177;      { K-East   }
      110,78:GetKey:=179;      { N-SW     }
      109,77:GetKey:=180;      { M-South  }
      44    :GetKey:=181       { ,-SE     }
      else begin BadKeySound;GetKey:=Null;end;
     end;
   end
  else GetKey:=Null;

 end; { GetKey }

procedure Hit(X, Y:integer;ch:char);
  var i,
      Thing : integer;
 begin
  Thing:=PF[x,y];
  for i:=1 to 30 do begin col(random(16));gotoxy(x,y);write(ch);end;
  gotoxy(x,y);
  case Thing of
   1..3: begin
          PF[x,y]:=Null;write(' ');Score:=Score+Thing;
          sound(400);delay(20);sound(90);
         end;
      4,19,20:
         begin
          case Thing of 4:Thing:=Block;19:Thing:=Forest;20:Thing:=Tree; end;
          if random(7)<WhipPower then
           begin write(' ');PF[x,y]:=Null;
            for i:=2600 downto 20 do sound(random(i));sound(90);
           end
          else
           begin
            sound(130);delay(25);sound(90);
            textcolor(6);
            if Thing in [Forest,Tree] then bak(2);
            write(chr(Thing));
            if Thing in [Forest,Tree] then bak(0);
           end;
         end;
      6: begin textcolor(16);textbackground(7);write(chr(Stairs));bak(0);end;
   10,15,16,18:
         begin
          PF[x,y]:=Null;write(' ');
          sound(400);delay(20);sound(70);
         end;
       5:begin textcolor(15);write(chr(Whip))end;
       7:begin textcolor(14);bak(4);write(chr(Chest));bak(0)end;
       8:begin textcolor(11);write(chr(SlowTime))end;
       9:begin textcolor(GemColor);write(chr(Gem))end;
      11:begin textcolor(13);write(chr(Teleport))end;
      12:begin textcolor(12);write(chr(Key))end;
      13:begin textcolor(3);textbackground(5);write(chr(Door));bak(0)end;
      14:begin textcolor(6);write(chr(Wall))end;
      17:begin textcolor(25);bak(1);write(chr(River));bak(0)end;
      21:begin textcolor(15);write(chr(Bomb))end;
      22:begin textcolor(12);bak(4);write(chr(Lava));bak(0)end;
      23:begin textcolor(7);write(chr(Pit))end;
      24:begin col(15);bak(1);write(chr(Staff));bak(0)end;
      25:begin col(15);write(chr(Tunnel))end;
      26:begin col(11);write(chr(Freeze))end;
      27:begin col(ArtColor);write('*')end;
      28..31,33:begin col(7);write(' ')end;
      32:begin PF[x,y]:=Null;write(' ')end
   else begin
         PF[x,y]:=Null;write(' ');
        end;
  end;
 end; { Hit }

procedure Move(XWay,YWay:integer);
   procedure Go;
    begin
     PF[PX,PY]:=Null;
     gotoxy(PX,PY);
     write(' ');
     PX:=PX+XWay;PY:=PY+YWay;
     PF[PX,PY]:=40;
     if T[5]<1 then
      begin
       gotoxy(PX,PY);textcolor(14);bak(4);
       write(chr(Player));textbackground(0);
      end
     else begin gotoxy(PX,PY);write(' ');end;
     FootStep;
    if keypressed then
     begin
      read(kbd,ch);
      if ch=#27 then read(kbd,ch);
     end;
    end; { Go }
 begin
  if (PX+XWay<XBot) or (PX+XWay>XTop) or
     (PY+YWay<YBot) or (PY+YWay>YTop) then
       begin
        Static;
        AddScore(20);
        CK;
        if not(0 in FoundSet) then
         begin
          FoundSet:=FoundSet+[0];
          Flash(16,25,'An electrified Wall blocks your way.');
         end;
        exit;
       end;
  case PF[PX+XWay,PY+YWay] of
   {Null}      0:Go;
   {Monsters}  1..3:
                 begin
                  Gems:=Gems-PF[PX+XWay,PY+YWay];
                  if Gems<0 then DEAD;
                  AddScore(PF[PX+XWay,PY+YWay]);
                  sound(200+200*PF[PX+XWay,PY+YWay]);delay(25);nosound;
                  Go;
                  if keypressed then
                   begin
                    read(kbd,ch);
                    if ch=#27 then read(kbd,ch);
                   end;
                 end;
   {Block}     4:begin
                  BlockSound;AddScore(4);
                  CK;
                  if not(4 in FoundSet) then
                   begin
                    FoundSet:=FoundSet+[4];
                    Flash(18,25,'A crumbled Wall blocks your way.');
                  end;
                 end;
   {Whip}      5:begin
                  Go;
                  GrabSound;
                  Whips:=Whips+1;
                  AddScore(5);
                  if not(5 in FoundSet) then
                   begin
                    FoundSet:=FoundSet+[5];
                    Flash(26,25,'You found a Whip.');
                   end;
                 end;
   {Stairs}    6:begin
                  Go;
                  CK;
                  if Level=25 then
                   begin
                    FootStep;
                    delay(300);
                    FootStep;
                    delay(300);
                    FootStep;
                    col(14);
                    for x:=1 to 175 do
                     begin
                      sound(random(3000)+x);
                      gotoxy(PX,PY);
                      bak(random(8));
                      col(14);
                      write(chr(Player));
                      colran(random(16));
                      bak(0);
                      print(15,25,'Oh no, something strange is happening!');
                     end;
                    for i:=1200 downto 20 do sound(random(i));
                    col(14);bak(4);
                    for x:=1 to 650 do
                     begin
                      sound(x*25);
                      gotoxy(PX,PY);
                      write(chr(220+random(4)));
                     end;
                    nosound;
                    gotoxy(PX,PY);
                    col(16);
                    bak(2);
                    write(chr(Stairs));
                    Restore_Border;
                    Flash(6,25,
                     'You are magically transported from the Kingdom of Kroz!!');
                    WON;
                   end;
                  if MixUp then Level:=random(28)+2
                  else Level:=Level+1;
                  AddScore(6);
                  if not(6 in FoundSet) then
                   begin
                    FoundSet:=FoundSet+[6];
                    Flash(14,25,'Stairs take you to the next lower level.');
                    CK;
                   end;
                  FootStep;
                  case level of
                   1:Level1;
                   3:Level3;
                   5:Level5;
                   7:Level7;
                   9:Level9;
                   11:Level11;
                   13:Level13;
                   15:Level15;
                   17:Level17;
                   19:Level19;
                   21:Level21;
                   23:Level23;
                   25:Level25
                   else Create_PlayField;
                  end;
                  FootStep;
                  if C1=9 then
                   begin
                    bak(GemColor);
                    window(2,2,65,24);
                    clrscr;
                    bak(0);
                    window(2,2,65,24);
                    clrscr;
                    window(1,1,80,25);cur(3);
                   end
                  else
                   begin
                    window(2,2,XSize+1,YSize+1);
                    gotoxy(1,22);
                    for i:=1 to 24 do writeln;
                   end;
                  window(1,1,80,25);cur(3);
                  Border;
                  T[1]:=5;T[2]:=6;T[3]:=7;
                  T[4]:=0;  { restore SlowTime   }
                  T[5]:=0;  { restore visibility }
                  T[6]:=0;  { restore SpeedTime  }
                  FootStep;
                  Display_PlayField;
                  FootStep;
                  for x:=1 to 600 do
                   begin
                    gotoxy(PX,PY);colran(random(16));bak(random(8));
                    write(chr(Player));sound(x div 2);
                   end;
                  gotoxy(PX,PY);col(14);bak(4);write(chr(Player));bak(0);
                  nosound;
                  I_Score     := Score;   { SAVE/RESTORE VARIABLES }
                  I_Gems      := Gems;
                  I_Whips     := Whips;
                  I_Teleports := Teleports;
                  I_Keys      := Keys;
                  I_WhipPower := WhipPower;
                  I_Difficulty:= Difficulty;
                  I_PX        := PX;
                  I_PY        := PY;
                  I_FoundSet  := FoundSet;
                  if Level=30 then
                   Flash(11,25,'You notice something unusual about this cavern!');
                 end;
   {Chest}     7:begin
                  Go;
                  for xb:=3 to 42 do for yb:=3 to 42 do
                   begin sound(xb*yb);delay(1);end; nosound;
                  x:=random(2)+2;          {Whips}
                  i:=random(Difficulty)+2; {Gems}
                  Whips:=Whips+x;
                  Gems:=Gems+i;
                  AddScore(7);
                  bak(0);
                  CK;
                  repeat
                   colran(random(16));
                   gotoxy(10,25);
                   write('You found ',i,' gems and ',x,' whips inside the chest!');
                  until keypressed;
                  Restore_Border;
                 end;
   {SlowTime}  8:begin
                  Go;
                  AddScore(5);
                  for x:=7 downto 1 do
                   begin sound(x*50+300);delay(x*10+40);end;nosound;
                  if FastPC then T[4] := 180 else T[4]:=60;
                  T[6]:=0;
                  if not(8 in FoundSet) then
                   begin
                    FoundSet:=FoundSet+[8];
                    Flash(18,25,'You activated a Slow Time spell.');
                   end;
                 end;
   {Gem}       9:begin
                  Go;
                  GrabSound;
                  Gems:=Gems+1;
                  AddScore(9);
                  if not(9 in FoundSet) then
                   begin
                    FoundSet:=FoundSet+[9];
                    Flash(15,25,'Gems give you both points and strength.');
                   end;
                 end;
   {Invisible}10:begin
                  Go;
                  AddScore(10);
                  for x:=1 to 4 do
                   begin sound(600);delay(50);nosound;delay(50);end;nosound;
                  gotoxy(PX,PY);write(' ');
                  if FastPC then T[5] := 120 else T[5]:=35;
                  if not(10 in FoundSet) then
                   begin
                    FoundSet:=FoundSet+[10];
                    Flash(13,25,'You found an Invisibility ring--big points!');
                   end;
                 end;
   {Teleport} 11:begin
                  Go;
                  GrabSound;
                  Teleports:=Teleports+1;
                  AddScore(11);
                  if not(11 in FoundSet) then
                   begin
                    FoundSet:=FoundSet+[11];
                    Flash(20,25,'You found a Teleport scroll.');
                   end;
                 end;
   {Key}      12:begin
                  Go;
                  Keys:=Keys+1;
                  GrabSound;
                  Update_Info;
                  if not(12 in FoundSet) then
                   begin
                    FoundSet:=FoundSet+[12];
                    Flash(22,25,'Use Keys to unlock doors.');
                   end;
                 end;
   {Door}     13:if Keys<1 then
                  begin
                   for x:=1 to 15 do
                    begin sound(random(99)+30);delay(15);nosound;delay(15);end;
                   CK;
                   if not(13 in FoundSet) then
                    begin
                     FoundSet:=FoundSet+[13];
                     Flash(18,25,'To pass the Door you need a Key.');
                    end;
                  end
                 else
                  begin
                   Keys:=Keys-1;
                   AddScore(11);
                   for x:=10 to 90 do
                    begin sound(x);delay(15);end;
                   CK;
                   Go;
                   if(Level=30)and(PX=53)and(PY=13)then
                    Flash(17,25,'You have found the magical Staff!');
                  end;
   {Wall/River}14,17:begin
                  if PF[PX+XWay,PY+YWay]=14 then BlockSound
                  else
                   begin
                    for x:=1 to 500 do sound(random(x*2+200)+x);nosound;
                   end;
                  AddScore(14);
                  CK;
                  if not(PF[PX+XWay,PY+YWay] in FoundSet) then
                   begin
                    FoundSet:=FoundSet+[PF[PX+XWay,PY+YWay]];
                    case PF[PX+XWay,PY+YWay] of
                     14:Flash(20,25,'A solid Wall blocks your way.');
                     17:Flash(18,25,'You cannot travel through Water.');
                    end;
                   end;
                 end;
   {SpeedTime}15:begin
                  Go;
                  AddScore(15);
                  for x:=1 to 7 do
                   begin sound(x*50+300);delay(x*10+40);end;nosound;
                  if FastPC then T[6] := 140 else T[6]:=50;
                  T[4]:=0;
                  if not(15 in FoundSet) then
                   begin
                    FoundSet:=FoundSet+[15];
                    Flash(18,25,'You activated a Speed Time spell.');
                   end;
                 end;
   {Trap}     16:begin
                  Go;
                  AddScore(16);
                  if not(16 in FoundSet) then
                   begin
                    FoundSet:=FoundSet+[16];
                    Flash(19,25,'You activated a Teleport trap!');
                   end;
                  for x:=1 to 100 do
                   begin
                    gotoxy(PX,PY);colran(random(16));bak(random(8));
                    write(chr(Player));
                   end;
                  gotoxy(PX,PY);bak(0);colran(0);write(' ');
                  for yb:= 30 downto 1 do
                   for xb:= 350 downto 20 do sound(yb*xb);
                  nosound;
                  PF[PX,PY]:=Null;PX:=Null;
                  repeat
                   x:=random(XSize)+XBot;
                   y:=random(YSize)+YBot;
                   if PF[x,y] = Null then
                    begin
                     PX:=x; PY:=y; PF[PX,PY]:=40;
                    end;
                  until PX <> Null;
                  for xb:=1 to 500 do
                   begin
                    gotoxy(PX,PY);colran(random(16));bak(random(8));
                    write(chr(Player));
                   end;
                  if T[5]<1 then
                   begin
                    gotoxy(PX,PY);col(14);bak(4);write(chr(Player));bak(0);
                   end
                  else begin gotoxy(PX,PY);bak(0);write(' ');end;
                  CK;
                 end;
   {Power}    18:begin
                  Go;
                  WhipPower:=WhipPower+1;
                  AddScore(15);
                  for x:=3 to 35 do
                   for y:=45 to 52 do
                    begin
                     sound(x*y);delay(7);nosound;delay(15);
                     colran(random(8));
                     gotoxy(PX,PY);
                     write(chr(Player));
                    end;
                  bak(4);col(14);
                  gotoxy(PX,PY);
                  write(chr(Player));
                  bak(0);
                  Flash(24,25,'Increased whip power!');
                 end;
   {Forest/Tree}19,20:
                 begin
                  BlockSound;AddScore(4);
                  CK;
                  if not(PF[PX+XWay,PY+YWay] in FoundSet) then
                   begin
                    FoundSet:=FoundSet+[PF[PX+XWay,PY+YWay]];
                    case PF[PX+XWay,PY+YWay] of
                     19:Flash(14,25,'You cannot travel through forest terrain.');
                     20:Flash(24,25,'A tree blocks your way.');
                    end;
                   end;
                 end;
   {Bomb}     21:begin
                  Go;
                  if not(21 in FoundSet) then
                   begin
                    FoundSet:=FoundSet+[21];
                    Flash(20,25,'You activated a Magic Bomb!');
                   end;
                  xr:=0;xl:=0;yr:=0;yl:=0;
                  for i:=70 to 600 do begin sound(i*2);delay(3);end;bor(4);
                  for i:=5000 downto 20 do sound(random(i));
                   for width:=1 to 4 do
                    begin
                     sound(30);
                     if PX-width>1 then xl:=width;
                     if PX+width<66 then xr:=width;
                     if PY-width>1 then yl:=width;
                     if PY+width<25 then yr:=width;
                     for x:=PX-xl to PX+xr do
                      for y:=PY-yl to PY+yr do
                       if PF[x,y] in [Null..4,13,16,19,28..32] then
                        begin
                         gotoxy(x,y);
                         textcolor(12);
                         write(chr(219));
                        end;
                    end;
                    for width:=1 to 4 do
                     begin
                      if PX-width>1 then xl:=width;
                      if PX+width<66 then xr:=width;
                      if PY-width>1 then yl:=width;
                      if PY+width<25 then yr:=width;
                      for x:=PX-xl to PX+xr do
                       for y:=PY-yl to PY+yr do
                        if PF[x,y] in [Null..4,13,16,19,28..32] then
                         begin
                          gotoxy(x,y);
                          textcolor(0);
                          write(' ');
                          if PF[x,y] in [1..3] then Score:=Score+PF[x,y];
                          PF[x,y]:=Null;
                         end;
                     end;
                  nosound;
                  bor(1);
                  Update_Info;
                  CK;
                 end;
   {Lava}     22:begin
                  Go;
                   Gems:=Gems-10;
                  for x:=400 downto 20 do
                   for y:= 7 downto 2 do sound(random(y*x+100)+y*x);nosound;
                  if Gems<0 then
                   begin
                    Gems:=0;
                    AddScore(22);
                    Dead;
                   end
                  else
                   AddScore(22);
                  CK;
                  if not(22 in FoundSet) then
                   begin
                    FoundSet:=FoundSet+[22];
                    Flash(22,25,'Lava causes heavy damage!');
                   end;
                 end;
   {Pit}      23:begin
                  Go;
                  for x:=300 downto 30 do
                   begin
                    textcolor(x mod 16);
                    print(22,25,'Oh no, a Bottomless Pit!');
                    sound(x*8);
                    gotoxy(PX,PY);textcolor(14);
                    if x>250 then write(chr(Player)) else
                    if x>200 then write(chr(9)) else
                    if x>150 then write(chr(111)) else
                    if x>100 then write(chr(248)) else
                    if x>50 then write(chr(249)) else
                    if x>0 then write(chr(250));
                   end;
                  for i:=1000 downto 20 do sound(random(i));nosound;
                  Dead;
                 end;
   {Staff}    24:if Level=25 then {* Player won the game! *}
                  begin
                   for x:=1 to 24 do
                    for y:=5 downto 1 do
                     begin
                      sound(x*45+y*10);delay(y*3);nosound;delay(40);
                      gotoxy(34,6);colran(random(16));
                      write(chr(Staff));
                     end;
                   gotoxy(34,6);
                   col(16);bak(4);
                   write(chr(Stairs));bak(0);
                   PF[PX+XWay,PY+YWay]:=6;
                   Score:=Score+2500;
                   Update_Info;
                   Flash(21,25,'The Amulet is finally yours!');
                  end;
   {Tunnel}   25:begin
                  PXOld:=PX;PYOld:=PY;
                  Go;
                  delay(350);FootStep;delay(500);FootStep;
                  PF[PX,PY]:=25;
                  gotoxy(PX,PY);
                  col(15);
                  write(chr(Tunnel));
                  if not(25 in FoundSet) then
                   begin
                    FoundSet:=FoundSet+[25];
                    Flash(17,25,'You have entered a secret Tunnel!');
                   end;
                  repeat
                   sound(random(3000)+100);
                   x:=random(XSize)+XBot;
                   y:=random(YSize)+YBot;
                  until (PF[x,y]=25)and((PXOld+XWay<>x)or(PYOld+YWay<>y));
                  Done:=false;
                  for i:=1 to 100 do
                   begin
                    sound(random(3000)+100);
                    a:=random(3)-1;
                    b:=random(3)-1;
                    if(PF[x+a,y+b]=Null) and (Done=false)then
                     begin
                      if not((x+a<XBot)or(x+a>XTop)or
                        (y+b<YBot)or(y+b>YTop)) then
                       begin
                        Done:=true;
                        x:=x+a;
                        y:=y+b;
                       end;
                     end;
                   end;
                  nosound;
                  if Done=false then
                   begin
                    x:=PXOld;
                    y:=PYOld;
                   end;
                  PX:=x;PY:=y;
                  PF[PX,PY]:=40;
                  for x:=1 to 400 do
                   begin
                    sound(random(1000));
                    gotoxy(PX,PY);colran(random(16));bak(random(8));
                    write(chr(Player));
                   end;nosound;
                  if T[5]<1 then
                   begin
                    gotoxy(PX,PY);textcolor(14);bak(4);
                    write(chr(Player));textbackground(0);
                   end
                  else begin gotoxy(PX,PY);bak(0);col(0);write(' ');end;
                  CK;
                 end;
   {Freeze}   26:begin
                  Go;
                  AddScore(11);
                  GrabSound;
                  for x:=1 to 5000 do sound(random(1000)+x+200);nosound;
                  T[7]:=25;
                  if not(26 in FoundSet) then
                   begin
                    FoundSet:=FoundSet+[26];
                    Flash(15,25,'You have activated a Freeze Time spell!');
                   end;
                 end;
   {Artifact} 27:begin
                  Go;
                  AddScore(27);
                  GrabSound;
                  if not(27 in FoundSet) then
                   begin
                    FoundSet:=FoundSet+[27];
                    Flash(11,25,'You have found an ancient Artifact...huge points!');
                   end;
                 end;
   {Quake}    28:begin
                  Go;
                  if not(28 in FoundSet) then
                   begin
                    FoundSet:=FoundSet+[28];
                    Flash(21,25,'Oh no, an Earthquake trap!');
                   end;
                  for i:=1 to 2500 do sound(random(i));nosound;
                  for i:=1 to 50 do
                   begin
                    Done:=false;
                    repeat
                     x:=random(XSize)+XBot;
                     y:=random(YSize)+YBot;
                     if PF[x,y] in [0..3,5,7..11,14..16,26,32] then
                      begin
                       Done:=true;
                       PF[x,y]:=4;
                       gotoxy(x,y);
                       col(6);
                       write(chr(Block));
                      end;
                    until (random(100)=0) or Done;
                    for xb:=1 to 400 do sound(random(200));nosound;
                   end;
                  for i:=2500 downto 20 do sound(random(i));nosound;
                 end;
   {IBlock}   29:begin
                  gotoxy(PX+XWay,PY+YWay);
                  col(6);
                  write(chr(Block));
                  PF[PX+XWay,PY+YWay]:=4;
                  BlockSound;
                  CK;
                  if not(29 in FoundSet) then
                   begin
                    FoundSet:=FoundSet+[29];
                    Flash(13,25,'An invisible crumbled Wall blocks your way.');
                   end;
                 end;
   {IWall}    30:begin
                  gotoxy(PX+XWay,PY+YWay);
                  col(6);
                  write(chr(Wall));
                  PF[PX+XWay,PY+YWay]:=14;
                  BlockSound;
                  CK;
                  if not(30 in FoundSet) then
                   begin
                    FoundSet:=FoundSet+[30];
                    Flash(17,25,'An invisible Wall blocks your way.');
                   end;
                 end;
   {IDoor}    31:begin
                  gotoxy(PX+XWay,PY+YWay);
                  col(3);bak(5);
                  write(chr(Door));
                  bak(0);
                  PF[PX+XWay,PY+YWay]:=13;
                  BlockSound;
                  CK;
                  if not(31 in FoundSet) then
                   begin
                    FoundSet:=FoundSet+[31];
                    Flash(17,25,'An invisible Door blocks your way.');
                   end;
                 end;
   {Stop}     32:Go;
   {Trap2}    33:begin
                  Go;
                  for x := XBot to XTop do
                   for y := YBot to YTop do
                    if PF[x,y] = 33 then PF[x,y] := Null;
                 end;
  end;{case}
 end; { Move }

procedure Player_Move;
 begin
  case GetKey of
    Null:;

       { Pause }
    80:begin
        textbackground(0);
        play(500,500,100);play(200,100,2);
        repeat
         colran(random(16));
         print(19,25,'Press any key to resume game.');
        until keypressed;
        Restore_Border;
       end;

       { Quit }
    81:begin
        play(600,600,100);play(450,450,100);play(300,300,100);play(99,99,99);
        repeat
         colran(random(16));
         print(24,25,'Are you sure (Y/N)?');
        until keypressed;
        read(kbd,ch);
        if upcase(ch)='Y' then
         begin
          bak(0);col(15);bor(0);cur(1);
          clrscr;
          gotoxy(33,1);writeln('KINGDOM OF KROZ');
          gotoxy(26,2);writeln('An Apogee Software Production');
          CK;
          gotoxy(1,wherey+1);
          writeln('Other shareware games available from Scott Miller:');
          writeln;
          write(  '* SUPERNOVA - An epic text adventure, with character graphics and sound effects.');
          writeln('     Understands full-sentence input and over 1000 words.  ($10)');
          writeln;
          writeln('* BEYOND THE TITANIC - A fantastic adventure of exploration and survival.');
          writeln('     Sound effects and 16 color screens.  ($8)');
          writeln;
          writeln('* TREK TRIVIA - VOLUMES 1 through 10 -  Each volume has 100 Star Trek trivia');
          writeln('     questions, and is played like a game.  16-colors and sound effects, too.');
          writeln('     $4 per volume (plus $2 per order for postage and disks), order all 10');
          writeln('     volumes and pay only $30, a $12 savings!');
          writeln;
          writeln('* CAVERNS OF KROZ (40 levels) and DUNGEONS OF KROZ (30 levels).');
          writeln('     Two sequels to KINGDOM OF KROZ with new screens.  ($15 for both)');
          writeln;
          writeln('Have a nice day.');
          writeln;
          writeln('Scott Miller');
          cur(1);
          CK;
          HALT;
         end
        else Restore_Border;
       end;

       { Restore }
    82:begin
        Flash(15,25,'Are you sure you want to RESTORE (Y/N)?');
        Restore_Border;
        read(kbd,ch);
        if upcase(ch)='N' then exit;
        bak(0);col(15);
        CK;
        print(26,25,'  Restoring...  ');
        assign(SaveFile,'KINGDOM.DAT');
        {$I-}
        reset(SaveFile);
        {$I+}
        if IOResult<>0 then
         begin
          close(SaveFile);
          Restore_Border;
          repeat
           colran(random(16));
           print(14,25,'A SAVE FILE does not exist on this disk.');
          until keypressed;
          Restore_Border;
         end
        else
         begin
          reset(SaveFile);
          read(SaveFile,SaveStuff);
          close(SaveFile);
          with SaveStuff do
           begin
            Level     := S_Level;
            Score     := S_Score;
            Gems      := S_Gems;
            Whips     := S_Whips;
            Teleports := S_Teleports;
            Keys      := S_Keys;
            WhipPower := S_WhipPower;
            Difficulty:= S_Difficulty;
            PX        := S_PX;
            PY        := S_PY;
            FoundSet  := S_FoundSet;
            MixUp     := S_MixUp;
           end;
          I_Score     := Score;   { SAVE/RESTORE VARIABLES }
          I_Gems      := Gems;
          I_Whips     := Whips;
          I_Teleports := Teleports;
          I_Keys      := Keys;
          I_WhipPower := WhipPower;
          I_Difficulty:= Difficulty;
          I_PX        := PX;
          I_PY        := PY;
          I_FoundSet  := FoundSet;
          Update_Info;
          delay(1000);
          case Level of
           1:Level1;
           3:Level3;
           5:Level5;
           7:Level7;
           9:Level9;
           11:Level11;
           13:Level13;
           15:Level15;
           17:Level17;
           19:Level19;
           21:Level21;
           23:Level23;
           25:Level25
           else Create_PlayField;
          end;
          window(2,2,XSize+1,YSize+1);
          textbackground(0);clrscr;cur(3);
          window(1,1,80,25);
          Border;
          T[1]:=5;T[2]:=6;T[3]:=7;
          T[4]:=0;  { restore SlowTime   }
          T[5]:=0;  { restore visibility }
          T[6]:=0;  { restore SpeedTime  }
          Display_PlayField;
          for x:=1 to 600 do
           begin
            gotoxy(PX,PY);colran(random(16));bak(random(8));
            write(chr(Player));sound(x div 2);
           end;
          nosound;
          gotoxy(PX,PY);col(14);bak(4);write(chr(Player));bak(0);
         end;
       end;

       { Save }
    83:begin
        Flash(16,25,'Are you sure you want to SAVE (Y/N)?');
        Restore_Border;
        read(kbd,ch);
        if upcase(ch)='N' then exit;
        bak(0);col(15);
        with SaveStuff do
         begin
          S_Level     := Level;
          S_Score     := I_Score;
          S_Gems      := I_Gems;
          S_Whips     := I_Whips;
          S_Teleports := I_Teleports;
          S_Keys      := I_Keys;
          S_WhipPower := I_WhipPower;
          S_Difficulty:= I_Difficulty;
          S_PX        := I_PX;
          S_PY        := I_PY;
          S_FoundSet  := I_FoundSet;
          S_MixUp     := MixUp;
         end;
        print(28,25,'  Saving...  ');
        assign(SaveFile,'KINGDOM.DAT');
        rewrite(SaveFile);
        write(SaveFile,SaveStuff);
        close(SaveFile);
        delay(1000);
        Restore_Border;
       end;

       { Teleport }
    84:begin
        if Teleports < 1 then begin NoneSound;exit;end;
        Teleports:=Teleports-1;
        Update_Info;
        for x:=1 to 100 do
        begin
         gotoxy(PX,PY);colran(random(16));bak(random(8));
         write(chr(Player));
        end;
        gotoxy(PX,PY);bak(0);colran(0);write(' ');
        xb:=0;
        repeat
         xb:=xb+2;yb:=0;
         repeat yb:=yb+1;sound(xb*yb)until yb>200;
        until xb>70;
        nosound;
        PF[PX,PY]:=Null;PX:=Null;
        repeat
         x:=random(XSize)+XBot;
         y:=random(YSize)+YBot;
         if PF[x,y] = Null then
          begin
           PX:=x; PY:=y; PF[PX,PY]:=40;
          end;
        until PX <> Null;
        CK;
        for xb:=1 to 500 do
        begin
         gotoxy(PX,PY);colran(random(16));bak(random(8));
         write(chr(Player));
        end;
        if T[5]<1 then
         begin
          gotoxy(PX,PY);col(14);bak(4);write(chr(Player));bak(0);
         end
        else begin gotoxy(PX,PY);bak(0);col(0);write(' ');end;
       end;

       { Whip }
    87:begin
        if Whips < 1 then begin NoneSound;exit;end;
        Whips:=Whips-1;
        sound(70);
        if (PY>YBot) and (PX>XBot) then Hit(PX-1,PY-1,'\');
        if (PX>XBot) then Hit(PX-1,PY,chr(196));
        if (PY<YTop) and (PX>XBot) then Hit(PX-1,PY+1,'/');
        if (PY<YTop) then Hit(PX,PY+1,chr(179));
        if (PY<YTop) and (PX<XTop) then Hit(PX+1,PY+1,'\');
        if (PX<XTop) then Hit(PX+1,PY,chr(196));
        if (PY>YBot) and (PX<XTop) then Hit(PX+1,PY-1,'/');
        if (PY>YBot) then Hit(PX,PY-1,chr(179));
        nosound;
        Update_Info;
       end;

       { North }
   172:move(0,-1);

       { South }
   180:move(0,1);

       { East }
   177:move(1,0);

       { West }
   175:move(-1,0);

       { Northwest }
   171:move(-1,-1);

       { Northeast }
   173:move(1,-1);

       { Southwest }
   179:move(-1,1);

       { Southeast }
   181:move(1,1);

  end;
 end; { Player_Move }


(****************************************************************************)
{$IMonster3.Inc     Includes the monster movement routines                *}
(****************************************************************************)


BEGIN
Screen;
NewGame:
Title;
Border;
Init_Screen;
Update_Info;
Define_Levels;
Level1;
Display_PlayField;
I_Score     := Score;   { SAVE/RESTORE VARIABLES }
I_Gems      := Gems;
I_Whips     := Whips;
I_Teleports := Teleports;
I_Keys      := Keys;
I_WhipPower := WhipPower;
I_Difficulty:= Difficulty;
I_PX        := PX;
I_PY        := PY;
I_FoundSet  := FoundSet;
for x:=1 to 800 do
 begin
  gotoxy(PX,PY);colran(random(16));bak(random(8));
  write(chr(Player));sound(x div 2);
 end;
gotoxy(PX,PY);col(14);bak(4);write(chr(Player));nosound;bak(0);
CK;

repeat
 Player_Move;
 if keypressed then SkipTime:=801 else SkipTime:=SkipTime+1;
 if SkipTime>800 then
  begin
   for x:=1 to TMax do T[x]:=T[x]-1;
   if FastPC then SkipTime := -150 else SkipTime:=0;
   if T[7]<1 then
    for x:=1 to 3 do
     case x of
      1:if T[x]<1 then Move_Slow;
      2:if T[x]<1 then Move_Medium;
      3:if T[x]<1 then Move_Fast;
     end;
   textcolor(4);bak(7);
   gotoxy(70,2);
   str(Score,StrVal);
   if Score>0 then StrVal:=StrVal+'0';
   gotoxy(73-length(StrVal) div 2,2);
   write(StrVal);
   if T[5]<1 then
    begin
     bak(4);textcolor(14);
     gotoxy(PX,PY);
     write(chr(Player));
    end;
   textbackground(0);
  end;
until Restart;
goto NewGame;

END. {*** KINGDOM OF KROZ ***}
