uses atari, crt, fastgraph;

type String40 = string[40];

const CHB = $8000;

var 
    fscr: file;
    c: char;
    aStr, bStr, iStr, ah, nb: TString;
    x, y, i, m, a, k, q: byte;
    b, r: shortInt;
    p: smallInt;
    s, e, sp, w: real;
    

function Antic2Atascii(c: byte):byte;assembler;overload;
asm {
    lda c
    asl
    php
    cmp #2*$60
    bcs @+
    sbc #2*$40-1
    bcs @+
    adc #2*$60
@   plp
    ror
    sta result;
    };
end;

function getKey:byte;overload;
begin
    result := byte(readkey) and %01011111;
end;


function getKey(a, b:byte):byte;overload;
begin
    repeat 
        result := byte(readkey) and %01011111;
    until (result = a) or (result = b);
end;

function getKey(a, b, c, d: byte):byte;overload;
begin
    repeat 
        result := byte(readkey) and %01011111;
    until (result = a) or (result = b) or (result = c) or (result = d);
end;

procedure print(s: String40);
begin
    blockwrite(fscr, s[1], length(s));
end;

procedure position(x, y: byte);
begin
    gotoxy(x + 1, y + 1);
end;

function locate(x, y: byte):byte;
begin
    result := Antic2Atascii(peek(savmsc + (y * 40) + x));
end;

procedure TitleScreen;
var dl: word;
(*    
 2000 GRAPHICS 17:SETCOLOR 2,0,0:DL=PEEK(560)+256*PEEK(561)
 2010 POKE DL+10,7:POSITION 5,5:? #6;"STARY DOM":POSITION 3,9:? #6;"gra przygodowa"
 2015 POKE DL+25,2:POSITION 0,18:? #6;"PROGRAM:   POPRAWKI:"
 2020 POKE DL+25,2:POSITION 0,20:? #6;"W.Zientara - 1987     P.Galanciak - 2019"
*)
begin
    InitGraph(17);
    color2 := 0;
    dl := dpeek(560);
    
    poke(dl + 10,7);
    position(5, 5); print('STARY DOM');
    position(3, 9); print('gra przygodowa');
    
    poke(dl + 25, 2);
    position(0, 18); print('PROGRAM:   POPRAWKI:');
    position(0, 20); print('W.Zientara - 1987     P.Galanciak - 2019');
end;

procedure LoadCharset;
(*
28999 REM NOWE ZNAKI
 29000 CHB=(PEEK(106)+4)*256
 29011 FOR I=0 TO 1023:POKE CHB+I,PEEK(57344+I):NEXT I:RESTORE 29040
 29020 READ A:IF A=-1 THEN RETURN 
 29030 FOR I=0 TO 7:READ B:POKE CHB+8*A+I,B:NEXT I:GOTO 29020
 29040 DATA 66,0,24,60,102,102,126,102,12,96,12,60,102,96,96,102,60,0,84,0,0,60,102,126,96,60,6
 29050 DATA 75,0,126,96,124,96,96,126,12,70,12,24,126,12,24,48,126,0,71,12,24,0,126,12,48,126,0
 29060 DATA 76,0,96,120,112,224,96,126,0,77,12,24,0,124,102,102,102,0,78,24,102,118,126,126,110,102,0
 29070 DATA 79,12,60,102,102,102,102,60,0,80,12,24,0,60,102,102,60,0,72,0,0,60,6,62,102,62,12
 29080 DATA 73,12,60,96,60,6,6,60,0,86,12,24,0,60,96,96,60,0,74,12,24,62,96,60,6,124,0
 29090 DATA 89,0,24,0,126,12,48,126,0,85,24,0,126,12,24,48,126,0,123,0,56,24,28,56,24,60,0
 29100 DATA 6,24,60,24,60,90,24,36,102,-1
*)
var charfaces : array [0..170] of byte = (
66,0,24,60,102,102,126,102,12,96,12,60,102,96,96,102,60,0,84,0,0,60,102,126,96,60,6,
75,0,126,96,124,96,96,126,12,70,12,24,126,12,24,48,126,0,71,12,24,0,126,12,48,126,0,
76,0,96,120,112,224,96,126,0,77,12,24,0,124,102,102,102,0,78,24,102,118,126,126,110,102,0,
79,12,60,102,102,102,102,60,0,80,12,24,0,60,102,102,60,0,72,0,0,60,6,62,102,62,12,
73,12,60,96,60,6,6,60,0,86,12,24,0,60,96,96,60,0,74,12,24,62,96,60,6,124,0,
89,0,24,0,126,12,48,126,0,85,24,0,126,12,24,48,126,0,123,0,56,24,28,56,24,60,0,
6,24,60,24,60,90,24,36,102);
    charptr: byte;
begin
    charptr := 0;
    move(pointer(57344), pointer(CHB), 1024);
    repeat
        move(charfaces[charptr + 1],pointer(CHB + (charfaces[charptr] * 8)), 8);
        inc(charptr, 9);
    until charptr >= 170
end;

(*
2999 REM INICJACJA TABLIC
 3000 DIM P$(300),B$(80),S$(40),O$(72),H$(36),A$(10),I$(4),AH$(4),NB$(8)
 3010 DIM T1$(16),T2$(7),T3$(9),T4$(16)
 3020 P$=" ":P$(300)=" ":P$(2)=P$:B$=P$(221):S$=B$(41):O$=B$(9)
 3030 RESTORE 4000:FOR I=0 TO 29:READ A$:P$(I*10+1,I*10+LEN(A$))=A$:NEXT I
 3040 FOR I=0 TO 9:READ A$:B$(I*8+1,I*8+LEN(A$))=A$:NEXT I
 3050 FOR I=0 TO 4:READ A$:S$(I*8+1,I*8+LEN(A$))=A$:NEXT I
 3060 FOR I=0 TO 8:READ A$:O$(I*8+1,I*8+LEN(A$))=A$:NEXT I
 3070 H$="RYBAJAJOBAJTTRZYKLANKREWMYSZKLOCBICZ"
 3080 T1$="WIĘC WYRZUCIŁEŚ.":T2$="UŻYŁEŚ ":T3$="NIE MASZ ":T4$="ZAATAKOWAŁ":RETURN 
 3999 REM DANE
 4000 DATA KRONK,TROLL,DUCH,SKRZAT,NIETOPERZ,GIGANT,ZOOMBIE,CZAROWNIK,PIES,DIABEŁ,WĄŻ,SZCZUR,SMOK
 4010 DATA GOBLIN,PAJĄK,HYDRA,DEMON,CYKLOP,FENIKS,UPIÓR,JASZCZUR,GARGOYLA,MEDUZA,WARG,CZAROWNICA
 4020 DATA WAMPIR,WILKOŁAK,MINOTAUR,ORK,ELF,WIDELEC,SCYZORYK,FINKĘ,SZTYLET,SZPADĘ,LANCĘ,OSZCZEP
 4030 DATA SZABLĘ,TOPÓR,MIECZ,BRĄZ,SREBRO,ZŁOTO,PLATYNĘ,DIAMENTY,MŁOTEK,KAGANEK,KLUCZ,DESKĘ,PROWIANT
 4040 DATA NAPOJE,BANDAŻ,ŁUBKI,HASŁO* )    
end;
*)
const
    Ptab: array [0..29] of TString = (
     'KRONK','TROLL','DUCH','SKRZAT','NIETOPERZ','GIGANT','ZOOMBIE','CZAROWNIK','PIES','DIABE'#12,'W'#2#21,'SZCZUR','SMOK',
     'GOBLIN','PAJ'#2'K','HYDRA','DEMON','CYKLOP','FENIKS','UPI'#15'R','JASZCZUR','GARGOYLA','MEDUZA','WARG','CZAROWNICA',
     'WAMPIR','WILKO'#12'AK','MINOTAUR','ORK','ELF');
    Btab: array [0..9] of TString = (
        'WIDELEC','SCYZORYK','FINK'#11,'SZTYLET','SZPAD'#11,'LANC'#11,'OSZCZEP','SZABL'#11,'TOP'#15'R','MIECZ' );
    Stab: array [0..4] of TString = (
        'BR'#2'Z','SREBRO','Z'#12'OTO','PLATYN'#11,'DIAMENTY' );
    Otab: array [0..8] of TString = (        
        'M'#12'OTEK','KAGANEK','KLUCZ','DESK'#11,'PROWIANT','NAPOJE','BANDA'#21,#12'UBKI','HAS'#12'O' );
    itemSymbols: array [0..7] of char = ( 'M', 'G', 'K', 'D', 'P', 'N', 'B', 'L');
        
    Htab: array [0..8] of TString = (
        'RYBA','JAJO','BAJT','TRZY','KLAN','KREW','MYSZ','KLOC','BICZ');
    Ttab: array [0..3] of TString = ('WI'#11'C WYRZUCI'#12'E'#9'.','U'#21'Y'#12'E'#9' ','NIE MASZ ','ZAATAKOWA'#12);

procedure PromptAny;
// 5999 REM PRZERWA WYDRUKU
// 6000 POSITION 8,23:? "NACIŚNIJ DOWOLNY KLAWISZ";:GET #5,K:? :? CHR$(125):RETURN 
begin
   position(8, 23);
   write('NACI'#9'NIJ DOWOLNY KLAWISZ');
   ReadKey;
   write(#125);
end;


procedure ShowManual;
(*   
2040 GRAPHICS 0:SETCOLOR 2,0,0:? :? :? "   CZY CHCESZ INSTRUKCJE ? (T/N) ";:GET #5,K:POKE 752,1
 2050 POKE 756,CHB/256:IF K=84 THEN GOSUB 5000    
4999 REM INSTRUKCJA
 5000 ? :? :? " Znalazłeś się nagle w starym domu,   z którego musisz się wydostać."
 5010 ? "Jest tu tylko jedno wyjście i jest    ono strzeżone. Strażnik wypuści cię,"
 5020 ? "jeśli podasz hasło i zapłacisz 100 $.":? :? " Musisz zbierać pieniądze (w formie"
 5030 ? "skarbów), które są własnością potwo-  rów zamieszkujących dom. Możesz z"
 5040 ? "nimi walczyć, jeśli masz mniej niż    5 ran i siłę większą od zera."
 5050 ? :? " Możesz zwiększyć swoją energię, gdy  znajdziesz lub kupisz prowiant lub"
 5060 ? "napoje. Rany możesz leczyć łubkami    lub bandażami.":GOSUB 6000
 5070 ? :? " Jeżeli nie możesz walczyć, to musisz zapłacić potworowi za wypuszczenie."
 5080 ? :? " Hasło znajdziesz w domu. Musisz je   dobrze zapamiętać.":? 
 5090 ? :? " Dom jest bardzo stary. W niektórych  pokojach jest ciemno, w innych nie"
 5100 ? "ma podłogi. Możesz także napotkać":? "zamknięte drzwi lub cienkie ścianki."
 5110 ? "Dlatego zbieraj napotkane przedmioty, ale pamiętaj, że możesz jednocześnie"
 5120 ? "nieść tylko cztery przedmioty i jed-  ną broń. Wybieraj zawsze najlepszą"
 5130 ? "broń; zależy od tego twoja siła oraz  wielkość zdobywanych skarbów.":GOSUB 6000
 5140 ? :? "           SKRÓTY:":? :? "W - wyjście":? "C - ciemno":? "P - brak podłogi"
 5150 ? "* - zamknięte drzwi":? "+ - cienka ścianka":? "M - młotek":? "G - kaganek":? "K - klucz"
 5160 ? "D - deska":? "P - prowiant":? "N - napoje":? "B - bandaże":? "L - łubki":? :? "& - a to jesteś TY"
 5170 GOSUB 6000:RETURN 
*)

begin
    InitGraph(0);
    color2 := 0;
    poke(756, Hi(CHB));
    Writeln;
    Write('   CZY CHCESZ INSTRUKCJE ? (T/N) ');
    k := getKey;
    if k <> 84 then exit;
    CursorOff;
    Writeln;
    Writeln;
    Writeln(' Znalaz'#123'e'#10' si'#20' nagle w starym domu,   z kt'#16'rego musisz si'#20' wydosta'#22'.');
    Writeln('Jest tu tylko jedno wyj'#10'cie i jest    ono strze'#21'one. Stra'#21'nik wypu'#10'ci ci'#20',');
    Writeln('je'#10'li podasz has'#123'o i zap'#123'acisz 100 $.');
    Writeln;
    Writeln(' Musisz zbiera',#22,' pieni',#8,'dze (w formie');
    Writeln('skarb'#16'w), kt'#16're s'#8' w'#123'asno'#10'ci'#8' potwo-  r'#16'w zamieszkuj'#8'cych dom. Mo'#21'esz z');
    Writeln('nimi walczy'#22', je'#10'li masz mniej ni'#21'    5 ran i si'#123''#20' wi'#20'ksz'#8' od zera.');
    Writeln;
    Writeln(' Mo'#21'esz zwi'#20'kszy'#22' swoj'#8' energi'#20', gdy  znajdziesz lub kupisz prowiant lub');
    Writeln('napoje. Rany mo'#21'esz leczy'#22' '#123'ubkami    lub banda'#21'ami.');
    PromptAny;
    
    Writeln;
    Writeln(' Je'#21'eli nie mo'#21'esz walczy'#22', to musisz zap'#123'aci'#22' potworowi za wypuszczenie.');
    Writeln;
    Writeln(' Has'#123'o znajdziesz w domu. Musisz je   dobrze zapami'#20'ta'#22'.');
    Writeln;
    Writeln;
    Writeln(' Dom jest bardzo stary. W niekt'#16'rych  pokojach jest ciemno, w innych nie');
    Writeln('ma pod'#123'ogi. Mo',#21,'esz tak'#21'e napotka'#22'');
    Writeln('zamkni'#20'te drzwi lub cienkie '#10'cianki.');
    Writeln('Dlatego zbieraj napotkane przedmioty, ale pami'#20'taj, '#21'e mo'#21'esz jednocze'#10'nie');
    Writeln('nie'#10#22' tylko cztery przedmioty i jed-  n'#8' bro'#13'. Wybieraj zawsze najlepsz'#8'');
    Writeln('bro'#13'; zale'#21'y od tego twoja si'#123'a oraz  wielko'#10#22' zdobywanych skarb'#16'w.');
    PromptAny;
    
    Writeln;
    Writeln('           SKR'#15'TY:');
    Writeln;
    Writeln('W'*,' - wyj'#10'cie');
    Writeln('C'*,' - ciemno');
    Writeln('P'*,' - brak pod'#123'ogi');
    Writeln('* - zamkni'#20'te drzwi');
    Writeln('+ - cienka '#10'cianka');
    Writeln('M - m'#123'otek');
    Writeln('G - kaganek');
    Writeln('K - klucz');
    Writeln('D - deska');
    Writeln('P - prowiant');
    Writeln('N - napoje');
    Writeln('B - banda'#21'e');
    Writeln('L - '#123'ubki');
    Writeln('& - a to jeste'#10' TY');
    PromptAny;
end;

procedure VarInit;
(*
 2499 REM INICJACJA ZMIENNYCH
 2500 X=6:Y=2:I$="----":I=INT(RND(0)*9):AH$=H$(I*4+1,I*4+4):B=1:P=0:R=0:E=3:M=32:NB$="WIDELEC":RETURN 
 *)
begin
    x := 6;
    y := 2;
    iStr := '----';
    i := Random(9);
    ah := Htab[i];
    b := 1;
    p := 0;
    r := 0;
    e := 3 ;
    m := 32;
    nb := 'WIDELEC';
end;


procedure PaintBoard;
(*
 2060 GOSUB 2500:SETCOLOR 2,1,10:SETCOLOR 1,2,2:? CHR$(125):? 
 2070 POSITION 5,1:? "":FOR I=1 TO 8
 2080 POSITION 5,I*2:? "| | | | | | | | | | | | | | | |":POSITION 5,I*2+1:? ""
 2090 NEXT I:POSITION 5,17:? ""
 2100 POSITION 7,2:? " ":POSITION 6,3:? " ":POSITION X,Y:? "&"
 2110 FOR F=2 TO 16:FOR G=6 TO 34:LOCATE G,F,Q:IF Q<>18 AND Q<>124 THEN 2130
 2120 V=INT(RND(0)*5):V=32+(V<2)*(10+V):POSITION G,F:? CHR$(V)
 2130 NEXT G:NEXT F:POSITION 34,16:? "
 ":GOTO 100
*)
var i, f, g, q, v:byte;
begin
    VarInit;
    color2 := $1a;
    color1 := 2;
    writeln(#125);
    CursorOff;
    writeln;
    position(5, 1); write('');
    for i:=1 to 8 do begin
        position(5, i * 2); write('| | | | | | | | | | | | | | | |');
        position(5, i * 2 + 1); write('');
    end;
    position(5, 17); write('');
    position(7, 2); write(' ');
    position(6, 3); write(' ');
    position(x, y); write('&');
    for f := 2 to 16 do for g := 6 to 34 do begin
        q := locate(g, f);
        if (q = 18) or (q = 124) then begin
            v := random(5);
            i := 0;
            if v < 2 then i := 1;
            v := 32 + (i* (10 + v));
            position(g, f); write(char(v));
        end;
    end;
    position(34, 16); write('W'*);
end;

function formatFloat(num: real):TString;
var m: cardinal;
    ms: TString;
begin
    Str(Trunc(num), result);
    m := Trunc(Frac(num) * 1000.0);
    if m > 0 then begin
        Str(m, ms);
        while (length(ms) < 3) do ms := concat('0', ms);
        result := concat(result, '.');
        result := concat(result, ms);
    end;
    if num<>0 then 
        while (result[byte(result[0])]='0') do dec(result[0]);
end;

procedure ShowStats;
(*
 30 IF E<0 THEN E=0
 40 POSITION 2,18:? "=";E;" "," =";R;" ":Z=E-R:IF Z<0 THEN Z=0.1
 50 POSITION 2,19:? "=";P;"$   ","  =";I$
 60 POSITION 2,20:? "ώ ";NB$;"     "
 70 S=Z*(1+B*0.25):POSITION 2,21:? "Ɍ =";S;"          ":RETURN 
 *)
var z: real;
begin
    if e < 0 then e := 0;
    position(2, 18); write('ENERGIA'*,' =',formatFloat(e),'    ');
    position(22, 18); write('RANY'*,' =',r,'    ');
    z := e - r;
    if z < 0 then z := 0.1;
    position(2, 19); write('SKARBY'*,' =',p,'$   ');
    position(22, 19); write('OBIEKTY'*,' =',iStr);
    position(2, 20); write('JAK'#2' BRO'#14' MASZ'*,' ',nb,'     ');
    s := z * (1 + b * 0.25);
    position(2, 21); write('SI'#12'A'*,' =',formatFloat(s),'          ');
end;

procedure ClearLine;
(*
80 POSITION 2,22:? CHR$(156);CHR$(156):RETURN
*)
begin
    position(2, 22); write(#156,#156);
end;

function hasItem(c: char):boolean;
var i: byte;
begin
    result := false;
    for i := 1 to 4 do 
        if iStr[i] = c then exit(true);
end;

procedure delItem(c: char);
var i: byte;
begin
    for i := 1 to 4 do 
        if iStr[i] = c then begin
            iStr[i] := '-';
            exit;
        end;
end;

procedure addItem(c: char);
var i: byte;
begin
    for i := 1 to 4 do 
        if iStr[i] = '-' then begin
            iStr[i] := c;
            exit;
        end;
end;


procedure MakeMove;
(*
100 GOSUB 30:POSITION 10,22:? "CZEKANIE CZY RUCH ?":A=0
 110 GOSUB 20:ON K<>82 AND K<>67 GOTO 110:GOSUB 80:IF K=82 THEN 150
 120 IF P<5 THEN E=E+0.5:GOTO 140
 130 P=P-5:E=E+2
 140 A=1:GOSUB 30:GOTO 420
 150 POSITION 7,22:? " EWO,  RAWO,  RA,   ?"
 160 GOSUB 20:IF K<>76 AND K<>80 AND K<>71 AND K<>68 THEN 160
 170 GOSUB 80:ZX=(K=80)-(K=76):ZY=(K=68)-(K=71)
 175 LOCATE X+ZX,Y+ZY,Z:POSITION X+ZX,Y+ZY:? CHR$(Z):IF Z<>18 AND Z<>124 THEN 200
 180 POSITION 2,22:? "MUR, UDERZYE	 SI W NOS.":? "TDY NIE PRZEJDZIESZ.";
 190 GOSUB 20:GOSUB 80:GOTO 100
 200 IF Z=32 THEN POSITION 2,22:? "OTWARTE DRZWI, PRZECHODZISZ.":GOSUB 20:GOSUB 80:GOTO 280
 210 ON Z<>42 GOTO 250:POSITION 2,22:? "ZAMKNITE DRZWI, ";:FOR I=1 TO 4:IF I$(I,I)="K" THEN POP :GOTO 230
 220 NEXT I:? T3$;"KLUCZA.":GOTO 190
 230 ? T2$;"KLUCZ."
 240 GOSUB 20:E=E-0.5:GOSUB 80:POSITION X+ZX,Y+ZY:? " ":GOTO 280
 250 POSITION 2,22:? "CIENKA 	CIANKA, ";:FOR I=1 TO 4:IF I$(I,I)="M" THEN POP :GOTO 270
 260 NEXT I:? T3$;"MOTKA.":GOTO 190
 270 ? T2$;"MOTEK.":GOTO 240
  
*) 
var z, l: byte;
    g, zx, zy: shortInt;
    isIn, moved, skipMonster: boolean;
    itemLost: char;
begin 
    ShowStats;
    position(10, 22); write('C'*,'ZEKANIE CZY ','R'*,'UCH ?');
    a := 0;
    k := getKey(82, 67);
    ClearLine;
    isIn := false;
    moved := false;
    skipMonster := false;
    
    if k = 67 then begin      // czekanie
        if p < 5 then begin
            e := e + 0.5;
        end else begin
            p := p - 5;
            e := e + 2;
        end;
        a := 1;
        ShowStats;
        moved := true;
    //--------------------------->> do losowych
    
    end else begin 
        position(7, 22);     // ruch
        write('L'*,'EWO, ','P'*,'RAWO, ','G'*,#15'RA, ','D'*,#15#12' ?');
        k := getKey(76, 80, 71, 68);
        ClearLine;
        zx := 0;
        zy := 0;
        case k of
            76: zx := -1;
            80: zx := 1;
            68: zy := 1;
            71: zy := -1;
        end;
        
        z := locate(x + zx, y + zy);
        
        if (z <> 18) and (z <> 124) then begin // check doors
            if z = 32 then begin 
                position(2, 22);
                write('OTWARTE DRZWI, PRZECHODZISZ.');
                isIn := true;
            end else begin
                if z = 42 then begin
                    position(2, 22);
                    write('ZAMKNI'#11'TE DRZWI, ');
                    if hasItem('K') then begin
                        write(tTab[1], 'KLUCZ.');
                        isIn := true;
                    end else begin
                        write(tTab[2], 'KLUCZA.');
                    end;
                end else begin
                    position(2, 22);
                    write('CIENKA '#9'CIANKA, ');
                    if hasItem('M') then begin
                        write(tTab[1], 'M'#12'OTEK.');
                        isIn := true;
                    end else begin
                        write(tTab[2], 'M'#12'OTKA.');
                    end;
                end;
                if isIn then begin
                    e := e - 0.5;
                    position(x + zx, y + zy);
                    write(' ');
                end;
            end;
            readkey;
            clearLine;
            if isIn then begin  // check room
                l := locate(x + 2 * zx, y + 2 * zy);
                if l=32 then begin
                    q := random(2);
                    if (a = 0) and (x > 8) and (y > 4) and (random(10) > 4) then begin
                        if q = 0 then l := 195
                        else l := 208;
                    end else begin
                        a := random(6);
                        if a = 0 then begin
                            if q = 0 then l := 195
                            else l := 208;
                        end;
                    end;
                end;
                
                if l=195 then begin
                    position(x + 2 * zx, y + 2 * zy);
                    write('C'*);
                    position(2,22);
                    writeln('W POKOJU JEST CIEMNO,');
                    if hasItem('G') then begin
                        write(tTab[1], 'KAGANEK.');
                        isIn := true;
                    end else begin
                        write(tTab[2], 'KAGANKA.');
                        isIn := false;
                    end;
                    readkey;
                    clearLine;                    
                end;
                if l=208 then begin
                    position(x + 2 * zx, y + 2 * zy);
                    write('P'*);
                    position(2,22);
                    writeln('W POKOJU NIE MA POD'#12'OGI,');
                    if hasItem('D') then begin
                        write(tTab[1], 'DESK'#11'.');
                        isIn := true;
                    end else begin
                        write(tTab[2], 'DESKI.');
                        isIn := false;
                    end;
                    readkey;
                    clearLine;
                end;
                if l=214 then begin
                    //  wyjscie
                
                end;

                if isIn then begin
                    e := e - 0.5;
                end;


            end;
            
            if isIn then begin
                position(x, y);
                write(chr(m));
                x := x + 2 * zx;
                y := y + 2 * zy;
                position(x, y);
                write('&');
                m := l;
                e := e - 0.5;
                moved := true;
            end;
            
    
        end else begin  // mur
            position(2, 22); writeln('MUR, UDERZY'#12'E'#9' SI'#11' W NOS.');
            write('T'#11'DY NIE PRZEJDZIESZ.');
            Readkey;
            clearLine;
        end;
    end;
    
    if moved then begin  // random events
        a := random(15) + 1;
        itemLost := '-';
        case a of
            1: if hasItem('M') then begin 
                    position(2, 22);
                    write('M'#12'OTEK SI'#11' Z'#12'AMA'#12', ');
                    itemLost:='M';
               end;
            2: if hasItem('G') then begin 
                    position(2, 22);
                    write('W KAGANKU SKO'#14'CZY'#12'A SI'#11' OLIWA, ');
                    itemLost:='G';
               end;
            3: if hasItem('K') then begin 
                    position(2, 22);
                    writeln('KLUCZ JEST OD FORTEPIANU, ');
                    itemLost:='K';
               end;
            4: if hasItem('D') then begin 
                    position(2, 22);
                    write('W DESCE S'#2' KORNIKI, ');
                    itemLost:='D';
               end;
            5: if hasItem('P') then begin 
                    position(2, 22);
                    write('PROWIANT ZEPSU'#12' SI'#11', ');
                    itemLost:='P';
               end;
            6: if hasItem('N') then begin 
                    position(2, 22);
                    write('NAPÓJ JEST ST'#11'CH'#12'Y, ');
                    itemLost:='N';
               end;
            7: if hasItem('B') then begin 
                    position(2, 22);
                    write('BANDAŻ PODAR'#12' SI'#11', ');
                    itemLost:='B';
               end;
            8: if hasItem('L') then begin 
                    position(2, 22);
                    write(#12'UBKI Z'#12'AMA'#12'Y SI'#11', ');
                    itemLost:='L';
               end;
            9: if random(10)>5 then begin
                    position(2, 22);
                    writeln('ZNALAZ'#12'E'#9' HAS'#12'O, BRZMI ONO ',ah,'.');
                    write('ZAPAMI'#11'TAJ JE.');
                    pause(200);
                    ClearLine;
                    skipMonster := true;
               end;
            10,11: 
                begin
                    position(2, 22);
                    write('Z'#12'AMA'#12'E'#9' ', nb, ', ');
                    b := b - 4;
                    if b < 1 then b := 1;
                    nb := BTab[b-1];
                    writeln('ZNALAZ'#12'E'#9' ', nb);
                    Readkey;
                    ClearLine;
                    ShowStats;
                end;
            12: begin
                    position(2, 22);
                    write('ZOSTA'#12'E'#9' PRZENIESIONY NA START.');
                    position(x, y);
                    write(chr(m));
                    m := 32;
                    x := 6;
                    y := 2;
                    position(x, y);
                    write('&');
                    Readkey;
                    ClearLine;
                    ShowStats;
                end;
        end;

        if itemLost<>'-' then begin
            write(tTab[0]);
            delItem(itemLost);
            Readkey;
            ClearLine;
            ShowStats;
        end;
    
    end;
    
    if not skipMonster then begin
        a := random(4);
        if a>0 then begin  // encounter
            q := random(30) + 1;
            sp := round((random * s * 2.65) + 1);
            w := sp;
            aStr := Ptab[q - 1];
            bStr := ' CI'#11' ';
            if (q = 16) or (q = 23) or (q = 25) then bStr := 'A CI'#11' ';
            position(2, 22);
            writeln(Ttab[3], bStr, aStr);
            write('SI'#12'A ', formatFloat(sp));

            moved := false;
            repeat
                position(12,23);
                write('W'*,'ALKA CZY ','O'*,'KUP ?');
                k := getKey(87, 79);
                if k = 87 then begin
                    if (s = 0) or (r > 4) then begin // too weak
                        ClearLine;
                        position(2,22);
                        write('JESTE'#9' ZA S'#12'ABY, MUSISZ P'#12'ACI'#96);
                        p := round(p - round(w * random));  // pay ransom
                        if p < 0 then p := 0;
                        moved := true;
                        readKey;
                        ShowStats;
                    end else begin   // fight
                        if (s < sp * 1.2) and (random(10) > 4) then r := r + 1;
                        sp := round(sp - ((random(2) + 1) * s * 0.57));
                        ShowStats;
                        if (sp <= 0) or (random(10) > 5) then begin
                            ClearLine;
                            a := random(5) + 1;
                            position(2, 22);
                            write(aStr,' ZOSTA'#12);
                            if (q = 16) or (q = 23) or (q = 25) then writeln('A POKONANA, ')
                            else writeln(' POKONANY, ');
                            write('ZDOBY'#12'E'#9' ', Stab[a - 1]);
                            p := p + round(a * (1.25 * (1 + w / 15) + random));
                            moved := true;
                            readKey;
                            ShowStats;
                        end else begin
                            ClearLine;
                            position(2, 22);
                            write(Astr, ' MA SI'#12#11' ', formatFloat(sp));
                        end;
                    end;
                end else begin
                    p := round(p - round(w * random));  // pay ransom
                    if p < 0 then p := 0;
                    moved := true;
                    ShowStats;
                end;
            until moved;

        end;
        
        ClearLine;
        a := random(11) + 1; // loot
        g := a - 8;
        if g < 0 then g := 0;
        case g of
            0: begin 
                position(2, 22);
                writeln('ZNALAZ'#12'E'#9' ', Otab[a-1]);
                write('B'*,'IERZESZ CZY ','Z'*,'OSTAWIASZ ?');
                k := getKey(66,90);
                if k = 90 then begin
                    ClearLine;
                    a := random(10) + 1;
                    position(2, 22);
                    writeln('ZNALAZ'#12'E'#9' ', Btab[a-1]);
                    write('B'*,'IERZESZ CZY ','Z'*,'OSTAWIASZ ?');
                    k := getKey(66,90);
                    if k = 66 then begin
                        nb := Btab[a-1];
                        b := a;
                        ShowStats;
                    end;
                end else begin
                    if hasItem('-') then begin
                        addItem(itemSymbols[a-1]);
                        ShowStats;
                    end else begin
                        moved := false;
                        repeat
                            position(2, 23);
                            write('CO CHCESZ ZOSTAWI'#96' ?       ');
                            repeat 
                                k := getKey;
                            until (k > 65) and (k < 90);
                            if hasItem(char(k)) then begin
                                delItem(char(k));
                                addItem(itemSymbols[a-1]);
                                ShowStats;
                                moved:=true;
                                ClearLine;
                            end else begin
                                position(2, 23);
                                write('NIE MASZ '#39,char(k),#39'      ');
                                readkey;
                                ClearLine;
                            end;
                        until moved;
                    end;
                end;
            end;

            1: begin 
                a := random(10) + 1;
                position(2, 22);
                writeln('ZNALAZ'#12'E'#9' ', Btab[a-1]);
                write('B'*,'IERZESZ CZY ','Z'*,'OSTAWIASZ ?');
                k := getKey(66,90);
                if k = 66 then begin
                    nb := Btab[a-1];
                    b := a;
                end;
            end;
            
            2: begin
                position(2, 22);
                writeln('ZNALAZ'#12'E'#9' HAS'#12'O, BRZMI ONO ',ah,'.');
                write('ZAPAMI'#11'TAJ JE.');
                pause(200);
            end;
 
            
        end;
        ClearLine;
    
    end;
            // use items
    if true then begin
        moved := false;
        repeat 
            position(2, 22);
            write('CHCESZ U'#21'YC JAKIEGO'#9' OBIEKTU (T/N) ?');
            k := getKey(84,78);
            if k=84 then begin
                position(2, 23);
                write('KT'#15'REGO ?      ');
                repeat 
                    k := getKey;
                until (k > 65) and (k < 90);
                if not hasItem(char(k)) then begin
                    position(2, 23);
                    write('NIE MASZ '#39,chr(k),#39);
                    readkey;
                    clearLine
                end else
                    if (k = 78) or (k = 80) or (k = 66) or (k = 76) then begin
                        delItem(char(k));    
                        case k of
                            78: e := e + 1;
                            80: e := e + 3;
                            66: r := r - 1;
                            76: r := r - 3;
                        end;
                        if r < 0 then r := 0;
                        ClearLine;
                        ShowStats;
                        if random(10)<6 then moved := true;
                    end else begin
                        position(2, 23);
                        write('TERAZ MO'#21'ESZ U'#21'Y'#96' TYLKO ','N'*,' ','P'*,' ','B'*,' ','L'*);
                        ReadKey;
                        ClearLine;
                    end;
            end else moved := true;
        until moved;
        ClearLine;
    end;
    
    
end;


begin
    randomize;
    assign(fscr, 'S:'); 
    rewrite(fscr, 1);

    LoadCharset;
    
    TitleScreen;
    ReadKey;
    ShowManual;
    
    PaintBoard;

    repeat     
        MakeMove;
    until false;

    Readkey;
    close(fscr);

end.
