PROGRAM HEROQUEST(INPUT,OUTPUT);


USES GRAPH,CRT;


TYPE
 GRAPHICS=ARRAY[-10..125] OF POINTER;
 DUNGEON=RECORD
          TILES: ARRAY[1..100,1..100] OF SHORTINT;
          ROOM:ARRAY[1..100,1..100] OF SHORTINT;
          TRAP:ARRAY[1..100,1..100]OF SHORTINT;
         END;


VAR
 GD,GM:INTEGER;
 X,Y:INTEGER;
 TILE:GRAPHICS;
 MAP:DUNGEON;
 CSX:INTEGER;
 CSY:INTEGER;
 XPOS,YPOS:INTEGER;
 XOLD,YOLD:INTEGER;
 COMMAND:CHAR;
 SX,SY:INTEGER;
 CT:INTEGER;
 PAGE:INTEGER;
 PAGE2:INTEGER;
 FIN:TEXT;
 FOUT:TEXT;
 MAPNAME:STRING;
 XMAX,YMAX:INTEGER;
 CROOM:INTEGER;
 SROOM:STRING[5];
 TOR:BOOLEAN;
 CTRAP:INTEGER;


PROCEDURE LOADMAP;
 VAR
  X,Y:INTEGER;
  new:char;
  CH:INTEGER;
   BEGIN
    CLRSCR;
    write('MAKE NEW MAP Y/N');
    new:=readkey;
    writeln;
    if new='N' then
    begin
     writeLn('ENTER MAP NAME:');
     readln(mapname);

     Assign(FIN,mapname);
     Reset(FIN);

     READ(FIN,XMAX);
     READLN(FIN,YMAX);
     FOR y:=1 TO YMAX DO
      BEGIN
       FOR x:=1 TO XMAX DO
        BEGIN
         read(FIN,CH);
         MAP.TILES[X,Y]:=CH;
        END;


       READLN(FIN);
      END;
     READLN(FIN);

     FOR y:=1 TO YMAX DO
      BEGIN
       FOR x:=1 TO XMAX DO
        BEGIN
         read(FIN,CH);
         MAP.ROOM[X,Y]:=CH;
        END;
       READLN(FIN);
      END;

      READLN(FIN);

      FOR y:=1 TO YMAX DO
      BEGIN
       FOR x:=1 TO XMAX DO
        BEGIN
         read(FIN,CH);
         MAP.TRAP[X,Y]:=CH;
        END;
       READLN(FIN);
      END;


    END
   else
    BEGIN
     write('ENTER MAP NAME:');
     readln(mapname);

     Assign(FIN,mapname);
     WRITE('ENTER SIZE OF MAP X:');
     READLN(XMAX);
     WRITE('ENTER SIZE OF MAP Y:');
     READLN(YMAX);

     FOR y:=1 TO YMAX DO
       FOR x:=1 TO XMAX DO
        BEGIN
         MAP.TILES[X,Y]:=0;
         MAP.ROOM[X,Y]:=0;
         MAP.TRAP[X,Y]:=0;
        END;

       END;
    END;

PROCEDURE SAVEMAP;
 BEGIN
  closegraph;
  Assign(FOUT,MAPNAME);
  ReWRITE(FOUT);
  WRITE(FOUT,XMAX,' ');
  WRITELN(FOUT,YMAX);
  for X:=1 to YMAX do
   begin
    writeln(FOUT);
    for Y:=1 to XMAX do
    write(FOUT,map.TILES[Y,X]:5);
   end;
  WRITELN(FOUT);
  for X:=1 to YMAX do
   begin
    writeln(FOUT);
    for Y:=1 to XMAX do
    write(FOUT,map.ROOM[Y,X]:5);
   end;
  WRITELN(FOUT);
  for X:=1 to YMAX do
   begin
    writeln(FOUT);
    for Y:=1 to XMAX do
    write(FOUT,map.TRAP[Y,X]:5);
   end;


  CLOSE(FOUT);
  READLN;
 END;





PROCEDURE LOADPIC(FILENAME:STRING;VAR POINT:POINTER);
 VAR
  J:INTEGER;
  K:INTEGER;
  TC:CHAR;
  WIDTH:INTEGER;
  HEIGHT:INTEGER;
  NUM:INTEGER;
  SIZE:INTEGER;
  FIN:TEXT;


   BEGIN

    Assign(FIN,FILENAME);
    Reset(FIN);
    Read(FIN,Tc);
    Width := Ord(Tc);
    Read(FIN,Tc);
    Height := Ord(Tc);
    For J := 1 to Height Do
      Begin
        For K := 1 to Width  Do
          Begin
            Read(FIN,Tc);
            Num := Ord(Tc);
            if num<>0 then
            PutPixel(K,J,Num);
          End;
      End;
    CLOSE(FIN);
    Size := ImageSize(1,1,Width,HEIGHt);

     GetMem(POINT,Size);
     GetImage(1,1,Width,Height,POINT^);
     CLEARDEVICE;
  End;

PROCEDURE LOADGRAPHICS;
 VAR
  LOAD:INTEGER;
  GL:TEXT;
  FILENAME:STRING[15];



  BEGIN
   ASSIGN(GL,'GRAPHIC.GAM');
   RESET(GL);
   LOAD:=-9;
   REPEAT
     READLN(GL,FILENAME);
     LOADPIC(FILENAME,TILE[LOAD]);
     LOAD:=LOAD+1;
   UNTIL EOF(GL);
   CLOSE(GL);


END;



PROCEDURE PUTTILES;
VAR
 SCOPECHANGE:BOOLEAN;
 XTILE,YTILE:INTEGER;

 BEGIN
   SCOPECHANGE:=FALSE;
   IF (CSX<>SX) OR (CSY<>SY) THEN
      BEGIN
        IF PAGE=0 THEN
          PAGE:=1
        ELSE
          PAGE:=0;

        SETACTIVEPAGE(PAGE);
        SCOPECHANGE:=TRUE;
        CSX:=SX;
        CSY:=SY;
         FOR X:=1 TO 14 DO
          FOR Y:=1 TO 12 DO
           BEGIN
             XTILE:=X+((SX-1)*14);
             YTILE:=Y+((SY-1)*12);
             IF (XTILE<=XMAX) AND (YTILE<=YMAX) THEN
              BEGIN
               PUTIMAGE(30*(X-1),25*(Y-1),TILE[MAP.TILES[XTILE,YTILE]]^,NORMALPUT);
               IF TOR=FALSE THEN
                 BEGIN
                  STR(MAP.ROOM[XTILE,YTILE],SROOM);
                  OUTTEXTXY(30*(X-1)+10,25*(Y-1)+10,SROOM);
                 END
               ELSE
                 BEGIN
                  STR(MAP.TRAP[XTILE,YTILE],SROOM);
                  OUTTEXTXY(30*(X-1)+10,25*(Y-1)+10,SROOM);
                 END;


              END

             ELSE
              PUTIMAGE(30*(X-1),25*(Y-1),TILE[0]^,NORMALPUT);
          END;

        SETVISUALPAGE(PAGE);

      END;

     IF SCOPECHANGE=FALSE THEN
       BEGIN
        X:=((XOLD-1)-((CSX-1)*14))*30;
        Y:=((YOLD-1)-((CSY-1)*12))*25;
        PUTIMAGE(X,Y,TILE[35]^,XORPUT);
       END;

      X:=((XPOS-1)-((CSX-1)*14))*30;
      Y:=((YPOS-1)-((CSY-1)*12))*25;
      PUTIMAGE(X,Y,TILE[35]^,XORPUT);


END;


PROCEDURE MOVE;
   BEGIN
     COMMAND:=READKEY;
     XOLD:=XPOS;
     YOLD:=YPOS;
     CASE COMMAND OF
      '8':IF YPOS>1 THEN
          BEGIN
           YPOS:=YPOS-1;
           IF (YPOS) < ((CSY-1)*12)+1 THEN
              SY:=SY-1;


          END;
      '2':IF YPOS<YMAX THEN
          BEGIN
           YPOS:=YPOS+1;
           IF (YPOS) > ((CSY)*12) THEN
              SY:=SY+1;
          END;
      '4':IF XPOS>1 THEN
          BEGIN
           XPOS:=XPOS-1;
           IF (XPOS) < ((CSX-1)*14)+1 THEN
             SX:=SX-1;
          END;
      '6':IF XPOS<XMAX THEN
          BEGIN
           XPOS:=XPOS+1;
           IF (XPOS) > ((CSX)*14) THEN
              SX:=SX+1;
          END;
       '=':BEGIN
           CT:=CT+1;
           IF CT>123 THEN
            CT:=-9;
            PUTIMAGE(500,50,TILE[CT]^,NORMALPUT);
            IF PAGE=0 THEN
              PAGE2:=1
            ELSE
              PAGE2:=0;
            SETACTIVEPAGE(PAGE2);
            PUTIMAGE(500,50,TILE[CT]^,NORMALPUT);
            SETACTIVEPAGE(PAGE);

          END;
       '-':BEGIN
           CT:=CT-1;
           IF CT<-9 THEN
            CT:=123;
            PUTIMAGE(500,50,TILE[CT]^,NORMALPUT);
            IF PAGE=0 THEN
              PAGE2:=1
            ELSE
              PAGE2:=0;
            SETACTIVEPAGE(PAGE2);
            PUTIMAGE(500,50,TILE[CT]^,NORMALPUT);
            SETACTIVEPAGE(PAGE);



          END;
        ' ':
            BEGIN
             MAP.TILES[XPOS,YPOS]:=CT;
             X:=((XPOS-1)-((CSX-1)*14))*30;
             Y:=((YPOS-1)-((CSY-1)*12))*25;
             
             PUTIMAGE(X,Y,TILE[CT]^,NORMALPUT);
             PUTIMAGE(X,Y,TILE[35]^,XORPUT);
             STR(MAP.ROOM[XPOS,YPOS],SROOM);
             OUTTEXTXY(X+10,Y+10,SROOM);
             

           END;
        ',':BEGIN
             IF TOR=FALSE THEN
              IF CROOM>0 THEN
               BEGIN
                CROOM:=CROOM-1;
                BAR(490,110,530,120);
                STR(CROOM,SROOM);
                MOVETO(490,100);
                OUTTEXT('CURRENT ROOM');
                OUTTEXTXY(515,110,SROOM);
               END;
             IF TOR=TRUE THEN
              IF CTRAP>0 THEN
               BEGIN
                CTRAP:=CTRAP-1;
                BAR(490,150,530,160);
                STR(CTRAP,SROOM);
                MOVETO(490,100);
                OUTTEXT('CURRENT TRAP');
                OUTTEXTXY(515,150,SROOM);
               END;


            END;
        '.':BEGIN
             IF TOR=FALSE THEN
              BEGIN
               CROOM:=CROOM+1;
               BAR(490,110,530,120);
               STR(CROOM,SROOM);
               MOVETO(490,100);
               OUTTEXT('CURRENT ROOM');
               OUTTEXTXY(515,110,SROOM);
              END;
             IF TOR=TRUE THEN
              BEGIN
               CTRAP:=CTRAP+1;
               BAR(490,150,530,160);
               STR(CTRAP,SROOM);
               MOVETO(490,100);
               OUTTEXT('CURRENT ROOM');
               OUTTEXTXY(515,150,SROOM);
              END;


            END;
        'R':IF TOR=FALSE THEN
            BEGIN
             MAP.ROOM[XPOS,YPOS]:=CROOM;
             X:=((XPOS-1)-((CSX-1)*14))*30;
             Y:=((YPOS-1)-((CSY-1)*12))*25;
             PUTIMAGE(X,Y,TILE[MAP.TILES[XPOS,YPOS]]^,NORMALPUT);
             STR(CROOM,SROOM);
             OUTTEXTXY(X+10,Y+10,SROOM);

             PUTIMAGE(X,Y,TILE[35]^,XORPUT);
           END;

        'T':IF TOR=TRUE THEN
            BEGIN
             MAP.TRAP[XPOS,YPOS]:=CTRAP;
             X:=((XPOS-1)-((CSX-1)*14))*30;
             Y:=((YPOS-1)-((CSY-1)*12))*25;
             PUTIMAGE(X,Y,TILE[MAP.TILES[XPOS,YPOS]]^,NORMALPUT);
             STR(CTRAP,SROOM);
             OUTTEXTXY(X+10,Y+10,SROOM);

             PUTIMAGE(X,Y,TILE[35]^,XORPUT);
            END;

         'C':BEGIN
              CSX:=1000;
              IF TOR= TRUE THEN
                 TOR:=FALSE
              ELSE
                 TOR:=TRUE;
             END;










    END;
  END;






BEGIN
 LOADMAP;
 GD:=EGA;GM:=1; INITGRAPH(GD,GM, ' ');
 SETACTIVEPAGE(0);
 SETVISUALPAGE(1);

 LOADGRAPHICS;
 SETVISUALPAGE(0);


 CSX:=2;
 CSY:=2;
 SX:=1;
 SY:=1;
 XPOS:=1;
 YPOS:=1;
 CT:=0;
 CROOM:=0;
 CTRAP:=0;
 TOR:=FALSE;
 SETFILLSTYLE(1,0);



 SETACTIVEPAGE(1);
 MOVETO(490,40);
 SETTEXTSTYLE(2,0,3);
 OUTTEXT('CURRENT TILE');
 PUTIMAGE(500,50,TILE[CT]^,NORMALPUT);
 STR(CROOM,SROOM);
 MOVETO(490,100);
 OUTTEXT('CURRENT ROOM');
 OUTTEXTXY(515,110,SROOM);
 STR(CTRAP,SROOM);
 MOVETO(490,140);
 OUTTEXT('CURRENT TRAP');
 OUTTEXTXY(515,150,SROOM);


 SETACTIVEPAGE(0);
 MOVETO(490,40);
 SETTEXTSTYLE(2,0,3);
 OUTTEXT('CURRENT TILE');
 PUTIMAGE(500,50,TILE[CT]^,NORMALPUT);
 STR(CROOM,SROOM);
 MOVETO(490,100);
 OUTTEXT('CURRENT ROOM');
 OUTTEXTXY(515,110,SROOM);
 STR(CTRAP,SROOM);
 MOVETO(490,140);
 OUTTEXT('CURRENT TRAP');
 OUTTEXTXY(515,150,SROOM);

 PAGE:=0;
 REPEAT
  PUTTILES;
  MOVE;


  
 UNTIL COMMAND='E';

SAVEMAP;




END.



