Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | Download | RSS feed

  1. unit LRLLevels;
  2.  
  3. {$mode objfpc}
  4.  
  5.  
  6. interface
  7.  
  8.  
  9. uses
  10.   SysUtils,
  11.   LRLRoutines, LRLSprites;
  12.  
  13.  
  14. type
  15.   TLRLPlayerPosition = packed record
  16.     x, y:         Byte;
  17.     xoffs, yoffs: ShortInt;
  18.   end;
  19.  
  20.   TLRLPlayer = packed record
  21.     Command:       Byte;
  22.     { pictures:
  23.       1 - running left <-
  24.       2 - running right ->
  25.       3 - climbing up ^
  26.       4 - climbing down v
  27.       5 - falling
  28.       6 - ~~~~~ left <-
  29.       7 - ~~~~~ right ->
  30.       8 - firing left <-
  31.       9 - firing right ->
  32.     }
  33.     NewCommandWas: Boolean;
  34.     NewCommand:    Byte;
  35.     Position:      TLRLPlayerPosition;
  36.     Sprite:        Byte;
  37.     SpriteData:    Byte;
  38.     Controller:    Byte;
  39.     {
  40.       controllers:
  41.       0 - not playing
  42.       1 - human/keyboard
  43.       2 - computer
  44.     }
  45.     Prizes:        Byte;
  46.     {
  47.       max 1 if computer player
  48.       a) computer player leaves prize if falling into hole
  49.       b) takes prize if he has no prizes
  50.     }
  51.     Colour:        Byte;
  52.   end;
  53.  
  54.   TLRLBrick = packed record
  55.     Image:     Byte;
  56.     Count:     Byte;
  57.     Flags:     Byte;
  58.     { flags:
  59.       bit 0 - needed to animate this brick 5 sprites then pause
  60.         and then finnally 5 sprites
  61.       bit 1 - set if fatal brick
  62.       bit 2 - set if allowable to jump
  63.       bit 3 - allowable to walk thru
  64.       bit 4 - hidden
  65.       bit 5 - background
  66.       bit 6 - wait now
  67.       bit 7 - not draw it
  68.     }
  69.     IdleCount: Byte;
  70.   end;
  71.  
  72.   TLRLLevel = packed record
  73.     Field:  array[1..30, 1..16] of TLRLBrick;
  74.     Player: array[1..20] of TLRLPlayer;
  75.   end;
  76.  
  77.  
  78. const
  79.   BrickFlags: array[1..20] of Byte = (
  80.     48, 4 + 8 + 32 + 128,
  81.     49, 8 + 32,
  82.     50, 4 + 8 + 32,
  83.     51, 4 + 8 + 32,
  84.     52, 2,
  85.     53, 4,
  86.     54, 4 + 8,
  87.     55, 2,
  88.     56, 2,
  89.     65, 4 + 8 + 16 + 32);
  90.  
  91.  
  92. const
  93.   KeyboardControls: array[1..21] of Word = (
  94.     $00B0, 1, 1,
  95.     $00B3, 1, 2,
  96.     $00B2, 1, 3,
  97.     $00B1, 1, 4,
  98.     $00B5, 1, 6,
  99.     $00B7, 1, 7,
  100.     $0037, 1, 5);
  101.   ControlNumber = 7;
  102.  
  103.  
  104. var
  105.   ShowLives:        Boolean;
  106.   ShowScore:        Boolean;
  107.   ShowLevel:        Boolean;
  108.   LRLLevel:         TLRLLevel;
  109.   LRLLives:         Integer;
  110.   LRLScore:         Longint;
  111.   LRLCLevel:        Word;
  112.   ComputerTurn:     Word;
  113.   ComputerReaction: Word;
  114.   TimeToRefresh:    Boolean;
  115.   OldTimer:         Pointer;
  116.   TotalPrizes:      Integer;
  117.   GameStarted:      Boolean;
  118.   EndOfGame:        Boolean;
  119.   GameResult:       Word;
  120.   Paused:           Boolean;
  121.  
  122.  
  123. procedure LRLLoadLevel(Number: Byte);
  124. procedure LRLUpdatePlayers;
  125. procedure LRLDrawOrnamental(x1, y1, x2, y2, ornament: Byte);
  126. function LRLPlayLevel(Number: Byte): Word;
  127. function LRLLevelCount: Word;
  128. procedure LRLDeleteLevel(Count: Word);
  129. procedure LRLInsertLevel(After: Word);
  130. procedure LRLSaveLevel(Count: Word);
  131.  
  132.  
  133. implementation
  134.  
  135.  
  136. const
  137.   LevelFileName = 'LRL.LEV';
  138.   LevelFileHeader: String = 'Lode Runner Live Levels'#26;
  139.  
  140.   ERR_OPENFILE = '¥¢®§¬®¦­® ®âªàëâì ä ©« ã஢­¥©';
  141.   ERR_BADFILE  = '¥¢¥à­ë© ¨«¨ ¯®¢à¥¦¤¥­­ë© ä ©« ã஢­¥©';
  142.  
  143.  
  144. function LRLLevelCount: Word;
  145. var
  146.   LevelFile: File;
  147.   c, k: Word;
  148. begin
  149.   c := 0;
  150.   AssignFile(LevelFile, LevelFileName);
  151.   Reset(LevelFile, 1);
  152.   Seek(LevelFile, 24);
  153.   BlockRead(LevelFile, c, 1, k);
  154.   LRLLevelCount := c;
  155.   Close(LevelFile);
  156. end;
  157.  
  158.  
  159. procedure LRLSaveLevel(Count: Word);
  160. var
  161.   LevelFile: File;
  162.   i, j: Integer;
  163.   k: Word;
  164.   b: Pointer;
  165. begin
  166.   GetMem(b, 480);
  167.   if (Count = 0) or (Count > LRLLevelCount) then
  168.     Exit;
  169.   AssignFile(LevelFile, LevelFileName);
  170.   Reset(LevelFile, 1);
  171.   Seek(LevelFile, Longint(25 + 520 * (Longint(Count) - 1)));
  172.   for i := 1 to 10 do
  173.   begin
  174.     DataBytePut(b^, (i - 1) * 4, LRLLevel.Player[i].Position.x);
  175.     DataBytePut(b^, (i - 1) * 4 + 1, LRLLevel.Player[i].Position.y);
  176.     DataBytePut(b^, (i - 1) * 4 + 2, LRLLevel.Player[i].Colour);
  177.     DataBytePut(b^, (i - 1) * 4 + 3, LRLLevel.Player[i].Controller);
  178.   end;
  179.   BlockWrite(LevelFile, b^, 40, k);
  180.   for i := 1 to 16 do
  181.     for j := 1 to 30 do
  182.       DataBytePut(b^, (i - 1) * 30 + j - 1, LRLLevel.Field[j, i].Image + 47);
  183.   BlockWrite(LevelFile, b^, 480, k);
  184.   Close(LevelFile);
  185.   FreeMem(b, 480);
  186. end;
  187.  
  188.  
  189. procedure LRLDeleteLevel(Count: Word);
  190. var
  191.   Buffer: Pointer;
  192.   LevelFile: File;
  193.   i, j: Integer;
  194.   l: Longint;
  195.   k: Word;
  196. begin
  197.   GetMem(Buffer, 1000);
  198.   j := LRLLevelCount;
  199.   if (j < Count) or (j < 2) or (Count = 0) then
  200.     Exit;
  201.   AssignFile(LevelFile, LevelFileName);
  202.   Reset(LevelFile, 1);
  203.   for l := Count + 1 to j do
  204.   begin
  205.     Seek(LevelFile, Longint(25 + 520 * (Longint(l) - 1)));
  206.     BlockRead(LevelFile, Buffer^, 520, k);
  207.     Seek(LevelFile, Longint(25 + 520 * (Longint(l - 1) - 1)));
  208.     BlockWrite(LevelFile, Buffer^, 520, k);
  209.   end;
  210.   Seek(LevelFile, 24);
  211.   Dec(j);
  212.   BlockWrite(LevelFile, j, 1, k);
  213.   Seek(LevelFile, FileSize(LevelFile) - 520);
  214.   Truncate(LevelFile);
  215.   Close(LevelFile);
  216.   FreeMem(Buffer, 1000);
  217. end;
  218.  
  219.  
  220. procedure LRLInsertLevel(After: Word);
  221. var
  222.   Buffer: Pointer;
  223.   LevelFile: File;
  224.   i, j: Integer;
  225.   l: Longint;
  226.   k: Word;
  227. begin
  228.   GetMem(Buffer, 1000);
  229.   j := LRLLevelCount;
  230.   if (After > j) or (After = 0) then
  231.     Exit;
  232.   AssignFile(LevelFile, LevelFileName);
  233.   Reset(LevelFile, 1);
  234.   for l := j downto After + 1 do
  235.   begin
  236.     Seek(LevelFile, Longint(25 + 520 * (Longint(l) - 1)));
  237.     BlockRead(LevelFile, Buffer^, 520, k);
  238.     Seek(LevelFile, Longint(25 + 520 * (Longint(l + 1) - 1)));
  239.     BlockWrite(LevelFile, Buffer^, 520, k);
  240.   end;
  241.   Seek(LevelFile, 24);
  242.   Inc(j);
  243.   BlockWrite(LevelFile, j, 1, k);
  244.   Seek(LevelFile, Longint(25 + 520 * (Longint(After + 1) - 1)));
  245.   DataFill(Buffer^, 40, 0, 0);
  246.   DataFill(Buffer^, 480, 48, 40);
  247.   BlockWrite(LevelFile, Buffer^, 520, k);
  248.   Close(LevelFile);
  249.   FreeMem(Buffer, 1000);
  250. end;
  251.  
  252.  
  253. procedure LRLLoadLevel(Number: Byte);
  254. var
  255.   LevelFile: File;
  256.   InBuffer: Pointer;
  257.   i, j, k, l, x, y: Word;
  258.   a, b, c: Byte;
  259. begin
  260.   TotalPrizes := 0;
  261.   GetMem(InBuffer, $FFF0);
  262.  
  263.   AssignFile(LevelFile, LevelFileName);
  264.   Reset(LevelFile, 1);
  265.   if IOResult <> 0 then
  266.     raise Exception.Create(ERR_OPENFILE);
  267.  
  268.   BlockRead(LevelFile, InBuffer^, 24, k);
  269.   BlockRead(LevelFile, c, 1, k);
  270.   if (c = 0) or (IOResult <> 0) or (not DataIdentical(InBuffer^, LevelFileHeader[1], 24, 0, 0)) then
  271.     raise Exception.Create(ERR_BADFILE);
  272.  
  273.   if (Number = 0) or (Number > c) then Number := 1;
  274.   Seek(LevelFile, Longint(25 + 520 * (Longint(Number) - 1)));
  275.   BlockRead(LevelFile, InBuffer^, 40, k);
  276.  
  277.   for i := 1 to 10 do
  278.   with LRLLevel.Player[i] do
  279.   begin
  280.     Command := 10;
  281.     NewCommandWas := False;
  282.     NewCommand := 10;
  283.     Position.x := DataByteGet(InBuffer^, (i - 1) * 4 + 0);
  284.     Position.y := DataByteGet(InBuffer^, (i - 1) * 4 + 1);
  285.     Position.xoffs := 0;
  286.     Position.yoffs := 0;
  287.     Sprite  := 1;
  288.     SpriteData := 1;
  289.     Controller := DataByteGet(InBuffer^, (i - 1) * 4 + 3);
  290.     Prizes  := 0;
  291.     Colour  := DataByteGet(InBuffer^, (i - 1) * 4 + 2);
  292.   end;
  293.  
  294.   BlockRead(LevelFile, InBuffer^, 480, k);
  295.   for i := 1 to 16 do for j := 1 to 30 do
  296.   with LRLLevel.Field[j, i] do
  297.   begin
  298.     a := DataByteGet(InBuffer^, (i - 1) * 30 + (j - 1));
  299.     for b := 1 to 10 do
  300.     if BrickFlags[b * 2 - 1] = a then
  301.       Flags := BrickFlags[b * 2];
  302.     Count := 1;
  303.     if a < 64 then
  304.       a := a - 47 else
  305.       a := a - 63;
  306.     Image := a;
  307.     IdleCount := 0;
  308.     if Image = 4 then Inc(TotalPrizes);
  309.   end;
  310.  
  311.   BlockRead(LevelFile, InBuffer^, 480, k);
  312.   Close(LevelFile);
  313.   LRLCLevel := Number;
  314.   FreeMem(InBuffer, $FFF0);
  315. end;
  316.  
  317.  
  318. procedure LRLDrawOrnamental(x1, y1, x2, y2, ornament: Byte);
  319. var
  320.   i: Integer;
  321. begin
  322.   ImagePut(LRLScreen^, LRLDecoration[ornament].Image[6].Data^, x1 * 10, y1 * 10, 0, 0, 319, 199);
  323.   ImagePut(LRLScreen^, LRLDecoration[ornament].Image[7].Data^, x2 * 10, y1 * 10, 0, 0, 319, 199);
  324.   ImagePut(LRLScreen^, LRLDecoration[ornament].Image[5].Data^, x1 * 10, y2 * 10, 0, 0, 319, 199);
  325.   ImagePut(LRLScreen^, LRLDecoration[ornament].Image[8].Data^, x2 * 10, y2 * 10, 0, 0, 319, 199);
  326.   for i := x1 + 1 to x2 - 1 do
  327.   begin
  328.     ImagePut(LRLScreen^, LRLDecoration[ornament].Image[3].Data^, i * 10, y1 * 10, 0, 0, 319, 199);
  329.     ImagePut(LRLScreen^, LRLDecoration[ornament].Image[4].Data^, i * 10, y2 * 10, 0, 0, 319, 199);
  330.   end;
  331.   for i := y1 + 1 to y2 - 1 do
  332.   begin
  333.     ImagePut(LRLScreen^, LRLDecoration[ornament].Image[2].Data^, x1 * 10, i * 10, 0, 0, 319, 199);
  334.     ImagePut(LRLScreen^, LRLDecoration[ornament].Image[1].Data^, x2 * 10, i * 10, 0, 0, 319, 199);
  335.   end;
  336. end;
  337.  
  338.  
  339. procedure LRLRedrawLevel;
  340. var
  341.   i, j: Integer;
  342.   s: string;
  343. begin
  344.   ImageClear(LRLScreen^);
  345.   for i := 1 to 16 do for j := 1 to 30 do
  346.   with LRLLevel.Field[j, i] do
  347.   if ((Flags and 128) = 0) and ((Flags and 32) <> 0) and ((Flags and 16) = 0) then
  348.     ImagePut(LRLScreen^, LRLEnvironment[Image].Image[Count].Data^,j * 10, i * 10, 0, 0, 319, 199);
  349.  
  350.   for i := 1 to 10 do
  351.   with LRLLevel.Player[i] do
  352.   if Controller <> 0 then
  353.     ImagePutTransparent(LRLScreen^, LRLFigure[Colour, SpriteData].Image[Sprite].Data^,Position.x * 10 + Position.xoffs, Position.y * 10 + Position.yoffs, 0, 0, 319, 199);
  354.  
  355.   for i := 1 to 16 do for j := 1 to 30 do
  356.   with LRLLevel.Field[j, i] do
  357.   if ((Flags and 128) = 0) and ((Flags and 32) = 0) and ((Flags and 16) = 0) then
  358.     ImagePutTransparent(LRLScreen^, LRLEnvironment[Image].Image[LRLLevel.Field[j, i].Count].Data^, j * 10, i * 10, 0, 0, 319, 199);
  359.  
  360.   if not Paused then
  361.   begin
  362.     if ShowScore then
  363.     begin
  364.       STR(LRLScore, s);
  365.       ImageStringGet(s, LRLFont^, LRLFontBuffer^, 222);
  366.       ImagePut(LRLScreen^, LRLFontBuffer^, 56, 185, 0, 0, 319, 199);
  367.       ImageStringGet('Score: ', LRLFont^, LRLFontBuffer^, 254);
  368.       ImagePut(LRLScreen^, LRLFontBuffer^, 10, 185, 0, 0, 319, 199);
  369.     end;
  370.     if ShowLives then
  371.     begin
  372.       STR(LRLLives, s);
  373.       ImageStringGet(s, LRLFont^, LRLFontBuffer^, 222);
  374.       ImagePut(LRLScreen^, LRLFontBuffer^, 177, 185, 0, 0, 319, 199);
  375.       ImageStringGet('Lives: ', LRLFont^, LRLFontBuffer^, 254);
  376.       ImagePut(LRLScreen^, LRLFontBuffer^, 135, 185, 0, 0, 319, 199);
  377.     end;
  378.     if ShowLevel then
  379.     begin
  380.       Str(LRLCLevel, s);
  381.       ImageStringGet(s, LRLFont^, LRLFontBuffer^, 222);
  382.       ImagePut(LRLScreen^, LRLFontBuffer^, 292, 185, 0, 0, 319, 199);
  383.       ImageStringGet('Level: ', LRLFont^, LRLFontBuffer^, 254);
  384.       ImagePut(LRLScreen^, LRLFontBuffer^, 250, 185, 0, 0, 319, 199);
  385.     end;
  386.   end
  387.   else
  388.   begin
  389.     ImageStringGet('Game now paused', LRLFont^, LRLFontBuffer^, 254);
  390.     ImagePut(LRLScreen^, LRLFontBuffer^, 160 - ImageSizex(LRLFontBuffer^) div
  391.       2, 185, 0, 0, 319, 199);
  392.   end;
  393.   LRLDrawOrnamental(0, 0, 31, 17, 1);
  394. end;
  395.  
  396.  
  397. procedure LRLStartSequence;
  398. var
  399.   tmpScreen1: Pointer;
  400.   tmpScreen2: Pointer;
  401.   i: Integer;
  402. begin
  403.   GetMem(tmpScreen1, 64000);
  404.   GetMem(tmpScreen2, 49000);
  405.   ImageFill(tmpScreen2^, 300, 160, 0);
  406.   LRLRedrawLevel;
  407.   i := 0;
  408.   while i < 100 do
  409.   begin
  410.     DataMove(LRLScreen^, tmpScreen1^, 64000, 0, 0);
  411.     ImagePut(tmpScreen1^, tmpScreen2^, 10, 10, 0, i, 319, 199 - i);
  412.     ScreenApply(tmpScreen1^);
  413.     Sleep(20);
  414.     i := i + 4;
  415.   end;
  416.   ScreenApply(LRLScreen^);
  417.   FreeMem(tmpScreen1, 64000);
  418.   FreeMem(tmpScreen2, 49000);
  419. end;
  420.  
  421.  
  422. procedure LRLEndSequence;
  423. var
  424.   tmpScreen1: Pointer;
  425.   tmpScreen2: Pointer;
  426.   i: Integer;
  427. begin
  428.   GetMem(tmpScreen1, 64000);
  429.   GetMem(tmpScreen2, 49000);
  430.   ImageFill(tmpScreen2^, 300, 160, 0);
  431.   LRLRedrawLevel;
  432.   i := 100;
  433.   while i > 0 do
  434.   begin
  435.     DataMove(LRLScreen^, tmpScreen1^, 64000, 0, 0);
  436.     ImagePut(tmpScreen1^, tmpScreen2^, 10, 10, 0, i, 319, 199 - i);
  437.     ScreenApply(tmpScreen1^);
  438.     Sleep(20);
  439.     i := i - 4;
  440.   end;
  441.   ImagePut(LRLScreen^, tmpScreen2^, 10, 10, 0, 0, 319, 199);
  442.   ScreenApply(LRLScreen^);
  443.   FreeMem(tmpScreen1, 64000);
  444.   FreeMem(tmpScreen2, 49000);
  445. end;
  446.  
  447.  
  448. {
  449. game result:
  450. 1 - zamurovali
  451. 2 - poimali
  452. 10 - vse zdelano
  453. 50 - no more levels
  454. 60 - no human players
  455. 100 - esc was pressed
  456. }
  457. procedure LRLUpdatePlayers;
  458. var
  459.   i, j, k: Integer;
  460.   spd: Word;
  461. begin
  462.   for i := 1 to 10 do
  463.   begin
  464.     with LRLLevel.Player[i] do
  465.     begin
  466.       if Controller <> 0 then
  467.       begin
  468.         if (LRLLevel.Field[Position.x, Position.y].Flags and 2 <> 0) then
  469.         begin
  470.           if i = 1 then
  471.           begin
  472.             EndOfGame  := True;
  473.             GameResult := 1;
  474.             Exit;
  475.           end;
  476.           if Prizes <> 0 then
  477.           begin
  478.             Prizes := 0;
  479.             LRLLevel.Field[Position.x, Position.y - 1].Image := 4;
  480.             LRLLevel.Field[Position.x, Position.y - 1].Flags := BrickFlags[8];
  481.           end;
  482.           repeat
  483.             Position.y := Random(2) + 1;
  484.             Position.x := Random(30) + 1;
  485.           until (LRLLevel.Field[Position.x, Position.y].Image = 1) or
  486.             (LRLLevel.Field[Position.x, Position.y].Image = 2) or
  487.             (LRLLevel.Field[Position.x, Position.y].Image = 3) or
  488.             (LRLLevel.Field[Position.x, Position.y].Image = 4);
  489.           Command := 10;
  490.           Continue;
  491.         end;
  492.  
  493.         if LRLLevel.Field[Position.x, Position.y].Image = 4 then
  494.         if Controller = 2 then
  495.         if Prizes = 0 then
  496.         begin
  497.           Inc(Prizes);
  498.           LRLLevel.Field[Position.x, Position.y].Image := 1;
  499.           LRLLevel.Field[Position.x, Position.y].Flags := BrickFlags[2];
  500.         end else else
  501.         begin
  502.           Dec(TotalPrizes);
  503.           LRLScore := LRLScore + 100 * Longint(LRLCLevel);
  504.           LRLLevel.Field[Position.x, Position.y].Image := 1;
  505.           LRLLevel.Field[Position.x, Position.y].Flags := BrickFlags[2];
  506.         end;
  507.  
  508.         if (i = 1) then
  509.         begin
  510.           if (TotalPrizes = 0) and (Position.y = 1) and
  511.             (LRLLevel.Field[Position.x, Position.y].Image = 2) then
  512.           begin
  513.             EndOfGame  := True;
  514.             GameResult := 10;
  515.             Exit;
  516.           end;
  517.           for k := 2 to 10 do
  518.             if (LRLLevel.Player[k].Controller <> 0) then
  519.               if (LRLLevel.Player[k].Position.x = Position.x) and
  520.                 (LRLLevel.Player[k].Position.y = Position.y) then
  521.               begin
  522.                 EndOfGame  := True;
  523.                 GameResult := 2;
  524.                 Exit;
  525.               end;
  526.         end;
  527.         if (LRLLevel.Field[Position.x, Position.y].Flags and 1 <> 0) then
  528.         begin
  529.           if (Controller = 2) then
  530.           begin
  531.             if Prizes <> 0 then
  532.             begin
  533.               Prizes := 0;
  534.               LRLLevel.Field[Position.x, Position.y - 1].Image := 4;
  535.               LRLLevel.Field[Position.x, Position.y - 1].Flags := BrickFlags[8];
  536.             end;
  537.           end;
  538.         end;
  539.         if Controller = 2 then
  540.           spd := 2
  541.         else
  542.           spd := 3;
  543.  
  544.         if (LRLLevel.Field[Position.x, Position.y + 1].Flags and 4 <> 0) and
  545.            (LRLLevel.Field[Position.x, Position.y].Image <> 3) and
  546.            ((LRLLevel.Field[Position.x, Position.y].Image <> 2) or
  547.             (LRLLevel.Field[Position.x, Position.y].Flags and 16 <> 0)) and
  548.            (Position.y < 16) then
  549.         begin
  550.           k := 2;
  551.           while k <= 10 do
  552.           if (k <> i) and (LRLLevel.Player[k].Controller <> 0) and
  553.              (LRLLevel.Player[k].Position.x = Position.x) and
  554.              (LRLLevel.Player[k].Position.y = Position.y + 1) and
  555.              (Position.y < 16) then
  556.           begin
  557.             k := 100;
  558.             Break;
  559.           end else
  560.             Inc(k);
  561.  
  562.           if k <> 100 then
  563.           begin
  564.             NewCommand := 5;
  565.             NewCommandWas := True;
  566.           end;
  567.         end;
  568.  
  569.         if NewCommandWas then
  570.         begin
  571.           if (NewCommand <> Command) and (Command <> 5) then
  572.           begin
  573.             Command := NewCommand;
  574.             Sprite  := 1;
  575.           end;
  576.           NewCommandWas := False;
  577.         end;
  578.  
  579.         if (Command = 1) then
  580.         begin
  581.           if (LRLLevel.Field[Position.x, Position.y].Image = 3) then
  582.           begin
  583.             if Position.xoffs < 1 then
  584.             begin
  585.               if ((LRLLevel.Field[Position.x - 1, Position.y].Flags and 8 = 0) and
  586.                 (LRLLevel.Field[Position.x - 1, Position.y].Image <> 3)) or
  587.                 (LRLLevel.Field[Position.x, Position.y].Image <> 3) or (Position.x = 1) then
  588.               begin
  589.                 Command := 10;
  590.                 Position.xoffs := 0;
  591.               end;
  592.             end;
  593.             if (Command <> 10) and (SpriteData <> 6) then
  594.             begin
  595.               SpriteData := 6;
  596.               Sprite := 1;
  597.             end;
  598.           end else
  599.           begin
  600.             if Position.xoffs < 1 then
  601.             begin
  602.               if (LRLLevel.Field[Position.x - 1, Position.y].Flags and 8 = 0) or (Position.x = 1) then
  603.               begin
  604.                 Command := 10;
  605.                 Position.xoffs := 0;
  606.               end;
  607.             end;
  608.             if (Command <> 10) and (SpriteData <> 1) then SpriteData := 1;
  609.           end;
  610.  
  611.           if Command <> 10 then
  612.           begin
  613.             k := 1;
  614.             while (k > 0) do
  615.             begin
  616.               Inc(k);
  617.               if k = 11 then
  618.               begin
  619.                 if (SpriteData = 6) then
  620.                 begin
  621.                   if (Sprite = 2) then Dec(Position.xoffs, 5) else
  622.                   if (Sprite = 3) then Dec(Position.xoffs, 1);
  623.                 end else
  624.                   Dec(Position.xoffs, spd);
  625.                 Break;
  626.               end;
  627.               if (k <> i) and (i <> 1) and
  628.                  (LRLLevel.Player[k].Controller <> 0) and
  629.                  (LRLLevel.Player[k].Position.x = Position.x - 1) and
  630.                  (LRLLevel.Player[k].Position.y = Position.y) then
  631.                 begin
  632.                   Command := 10;
  633.                   Break;
  634.                 end;
  635.             end;
  636.           end;
  637.         end;
  638.  
  639.         if (Command = 2) then
  640.         begin
  641.           if (LRLLevel.Field[Position.x, Position.y].Image = 3) then
  642.           begin
  643.             if Position.xoffs > -1 then
  644.             begin
  645.               if ((LRLLevel.Field[Position.x + 1, Position.y].Flags and 8 = 0) and
  646.                 (LRLLevel.Field[Position.x + 1, Position.y].Image <> 3)) or
  647.                 (LRLLevel.Field[Position.x, Position.y].Image <> 3) or (Position.x = 30) then
  648.               begin
  649.                 Command := 10;
  650.                 Position.xoffs := 0;
  651.               end;
  652.             end;
  653.             if (Command <> 10) and (SpriteData <> 7) then
  654.             begin
  655.               SpriteData := 7;
  656.               Sprite := 1;
  657.             end;
  658.           end
  659.           else
  660.           begin
  661.             if Position.xoffs > -1 then
  662.             begin
  663.               if (LRLLevel.Field[Position.x + 1, Position.y].Flags and 8 = 0) or (Position.x = 30) then
  664.               begin
  665.                 Command := 10;
  666.                 Position.xoffs := 0;
  667.               end;
  668.             end;
  669.             if (Command <> 10) and (SpriteData <> 2) then
  670.               SpriteData := 2;
  671.           end;
  672.           if Command <> 10 then
  673.           begin
  674.             k := 1;
  675.             while (k > 0) do
  676.             begin
  677.               Inc(k);
  678.               if k = 11 then
  679.               begin
  680.                 if (SpriteData = 7) then
  681.                 begin
  682.                   if (Sprite = 2) then
  683.                     Inc(Position.xoffs, 5);
  684.                   if (Sprite = 3) then
  685.                     Inc(Position.xoffs, 1);
  686.                 end
  687.                 else
  688.                   Inc(Position.xoffs, spd);
  689.                 Break;
  690.               end;
  691.               if (k <> i) and (i <> 1) and (LRLLevel.Player[k].Controller <> 0) then
  692.                 if (LRLLevel.Player[k].Position.x = Position.x + 1) and
  693.                   (LRLLevel.Player[k].Position.y = Position.y) then
  694.                 begin
  695.                   Command := 10;
  696.                   Break;
  697.                 end;
  698.             end;
  699.           end;
  700.         end;
  701.  
  702.         if (Command = 3) then
  703.         begin
  704.           if Position.yoffs < 1 then
  705.           begin
  706.             if ((LRLLevel.Field[Position.x, Position.y].Image <> 2) or
  707.               (LRLLevel.Field[Position.x, Position.y].Flags and 16 <> 0)) or
  708.               ((LRLLevel.Field[Position.x, Position.y - 1].Flags and 4 = 0) and
  709.               ((LRLLevel.Field[Position.x, Position.y - 1].Image <> 2) or
  710.               (LRLLevel.Field[Position.x, Position.y - 1].Flags and 16 <> 0))) or
  711.               (Position.y < 2) then
  712.             begin
  713.               Command := 10;
  714.               Position.yoffs := 0;
  715.             end;
  716.           end;
  717.           if (Command <> 10) and (SpriteData <> 3) then
  718.             SpriteData := 3;
  719.           if Command <> 10 then
  720.           begin
  721.             k := 1;
  722.             while (k > 0) do
  723.             begin
  724.               Inc(k);
  725.               if k = 11 then
  726.               begin
  727.                 Dec(Position.yoffs, spd);
  728.                 Break;
  729.               end;
  730.               if (k <> i) and (i <> 1) and (LRLLevel.Player[k].Controller <> 0) then
  731.                 if (LRLLevel.Player[k].Position.y = Position.y - 1) and
  732.                   (LRLLevel.Player[k].Position.x = Position.x) then
  733.                 begin
  734.                   Command := 10;
  735.                   Break;
  736.                 end;
  737.             end;
  738.           end;
  739.         end;
  740.  
  741.         if (Command = 4) then
  742.         begin
  743.           if (LRLLevel.Field[Position.x, Position.y].Image = 3) and
  744.              ((LRLLevel.Field[Position.x, Position.y + 1].Image <> 2) or
  745.               (LRLLevel.Field[Position.x, Position.y + 1].Flags and 16 <> 0)) and
  746.              (Position.y < 16) then
  747.           begin
  748.             Command := 5;
  749.             Sprite  := 1;
  750.             if (LRLLevel.Field[Position.x, Position.y + 1].Flags and 4 <> 0) then
  751.               Inc(Position.yoffs);
  752.           end
  753.           else
  754.           begin
  755.             if Position.yoffs > -1 then
  756.             begin
  757.               if (((LRLLevel.Field[Position.x, Position.y + 1].Image <> 2) or
  758.                 (LRLLevel.Field[Position.x, Position.y + 1].Flags and 16 <> 0)) and
  759.                 (LRLLevel.Field[Position.x, Position.y + 1].Flags and 4 = 0)) or
  760.                 (Position.y = 16) then
  761.               begin
  762.                 Command := 10;
  763.                 Position.yoffs := 0;
  764.               end;
  765.             end;
  766.             if (Command <> 10) and (SpriteData <> 4) then
  767.               SpriteData := 4;
  768.             if Command <> 10 then
  769.             begin
  770.               k := 1;
  771.               while (k > 0) do
  772.               begin
  773.                 Inc(k);
  774.                 if k = 11 then
  775.                 begin
  776.                   Inc(Position.yoffs, spd);
  777.                   Break;
  778.                 end;
  779.                 if (k <> i) and (i <> 1) and (LRLLevel.Player[k].Controller <> 0) then
  780.                   if (LRLLevel.Player[k].Position.y = Position.y + 1) and
  781.                     (LRLLevel.Player[k].Position.x = Position.x) then
  782.                   begin
  783.                     Command := 10;
  784.                     Break;
  785.                   end;
  786.               end;
  787.             end;
  788.           end;
  789.         end;
  790.  
  791.         if (Command = 5) then
  792.         begin
  793.           if Position.yoffs < 1 then
  794.           begin
  795.             if (LRLLevel.Field[Position.x, Position.y + 1].Flags and 4 = 0) or
  796.               (Position.y = 16) or (LRLLevel.Field[Position.x, Position.y].Image = 3) or
  797.               ((LRLLevel.Field[Position.x, Position.y].Flags and 1 <> 0) and (i <> 1)) then
  798.             begin
  799.               Command := 10;
  800.               if (LRLLevel.Field[Position.x, Position.y].Image = 3) then
  801.                 SpriteData := 5;
  802.               Position.yoffs := 0;
  803.               Position.xoffs := 0;
  804.             end;
  805.             for k := 2 to 10 do
  806.               if (k <> i) and (LRLLevel.Player[k].Controller <> 0) then
  807.                 if (LRLLevel.Player[k].Position.x = Position.x) and
  808.                   (LRLLevel.Player[k].Position.y = Position.y + 1) and
  809.                   (LRLLevel.Field[Position.x, Position.y + 1].Flags and 1 <> 0) and
  810.                   (Position.y < 16) then
  811.                 begin
  812.                   Command := 10;
  813.                   Position.yoffs := 0;
  814.                   Break;
  815.                 end;
  816.           end;
  817.           if (Command <> 10) and (SpriteData <> 5) then
  818.           begin
  819.             SpriteData := 5;
  820.             Sprite := 1;
  821.           end;
  822.           if Command <> 10 then
  823.           begin
  824.             Inc(Position.yoffs, 2);
  825.           end;
  826.         end;
  827.  
  828.         if (Command = 6) then
  829.         begin
  830.           if (Position.y < 16) and (Position.x > 1) and
  831.             (LRLLevel.Field[Position.x - 1, Position.y + 1].Image = 9) and
  832.             (LRLLevel.Field[Position.x - 1, Position.y + 1].Flags and 1 = 0) and
  833.             (((LRLLevel.Field[Position.x - 1, Position.y].Image = 1) or
  834.             (LRLLevel.Field[Position.x - 1, Position.y].Flags and 1 <> 0)) or
  835.             (LRLLevel.Field[Position.x - 1, Position.y].Flags and 16 <> 0)) then
  836.           begin
  837.             NewCommandWas := True;
  838.             for k := 2 to 10 do
  839.               if (k <> i) and (LRLLevel.Player[k].Controller <> 0) then
  840.                 if (LRLLevel.Player[k].Position.x = Position.x - 1) and
  841.                   (LRLLevel.Player[k].Position.y = Position.y) then
  842.                 begin
  843.                   NewCommandWas := False;
  844.                   Break;
  845.                 end;
  846.             if NewCommandWas then
  847.             begin
  848.               LRLLevel.Field[Position.x - 1, Position.y + 1].Flags :=
  849.                 LRLLevel.Field[Position.x - 1, Position.y + 1].Flags or 1;
  850.               Position.xoffs := 0;
  851.               SpriteData := 8;
  852.               NewCommandWas := False;
  853.             end;
  854.           end;
  855.           Command := 10;
  856.         end;
  857.  
  858.         if (Command = 7) then
  859.         begin
  860.           if (Position.y < 16) and (Position.x < 30) and
  861.             (LRLLevel.Field[Position.x + 1, Position.y + 1].Image = 9) and
  862.             (LRLLevel.Field[Position.x + 1, Position.y + 1].Flags and 1 = 0) and
  863.             (((LRLLevel.Field[Position.x + 1, Position.y].Image = 1) or
  864.             (LRLLevel.Field[Position.x + 1, Position.y].Flags and 1 <> 0)) or
  865.             (LRLLevel.Field[Position.x + 1, Position.y].Flags and 16 <> 0)) then
  866.           begin
  867.             NewCommandWas := True;
  868.             for k := 2 to 10 do
  869.               if (k <> i) and (LRLLevel.Player[k].Controller <> 0) then
  870.                 if (LRLLevel.Player[k].Position.x = Position.x + 1) and
  871.                   (LRLLevel.Player[k].Position.y = Position.y) then
  872.                 begin
  873.                   NewCommandWas := False;
  874.                   Break;
  875.                 end;
  876.             if NewCommandWas then
  877.             begin
  878.               LRLLevel.Field[Position.x + 1, Position.y + 1].Flags :=
  879.                 LRLLevel.Field[Position.x + 1, Position.y + 1].Flags or 1;
  880.               Position.xoffs := 0;
  881.               SpriteData := 9;
  882.               NewCommandWas := False;
  883.             end;
  884.           end;
  885.           Command := 10;
  886.         end;
  887.  
  888.         if (Command = 1) or (Command = 2) then
  889.           if Position.yoffs < 0 then Inc(Position.yoffs) else
  890.           if Position.yoffs > 0 then  Dec(Position.yoffs);
  891.  
  892.         if (Command = 3) or (Command = 4) or (Command = 5) then
  893.           if Position.xoffs < 0 then Inc(Position.xoffs) else
  894.           if Position.xoffs > 0 then Dec(Position.xoffs);
  895.  
  896.         if Command < 6 then
  897.         begin
  898.           Inc(Sprite);
  899.           if Sprite > LRLFigure[Colour, SpriteData].ImageCount then Sprite := 1;
  900.           if Position.xoffs < -4 then
  901.           begin
  902.             Dec(Position.x);
  903.             Position.xoffs := 10 + Position.xoffs;
  904.           end;
  905.           if Position.xoffs > 5 then
  906.           begin
  907.             Inc(Position.x);
  908.             Position.xoffs := Position.xoffs - 10;
  909.           end;
  910.           if Position.yoffs < -4 then
  911.           begin
  912.             Dec(Position.y);
  913.             Position.yoffs := 10 + Position.yoffs;
  914.           end;
  915.           if Position.yoffs > 5 then
  916.           begin
  917.             Inc(Position.y);
  918.             Position.yoffs := Position.yoffs - 10;
  919.           end;
  920.         end;
  921.       end;
  922.     end;
  923.   end;
  924. end;
  925.  
  926.  
  927. procedure LRLUpdateBricks;
  928. var
  929.   i, j, k: Integer;
  930. begin
  931.   for i := 1 to 16 do
  932.     for j := 1 to 30 do
  933.     begin
  934.       if LRLLevel.Field[j, i].Flags and 1 <> 0 then
  935.       begin
  936.         if LRLLevel.Field[j, i].Count = 1 then
  937.         begin
  938.           LRLLevel.Field[j, i].Flags := LRLLevel.Field[j, i].Flags and $FF - 2;
  939.           LRLLevel.Field[j, i].Flags := LRLLevel.Field[j, i].Flags or 4 + 8;
  940.         end;
  941.         if LRLLevel.Field[j, i].IdleCount = 0 then
  942.         begin
  943.           Inc(LRLLevel.Field[j, i].Count);
  944.           if LRLLevel.Field[j, i].Count < 6 then
  945.           begin
  946.             for k := 2 to 10 do
  947.               if (LRLLevel.Player[k].Controller <> 0) then
  948.                 if (LRLLevel.Player[k].Position.x = j) and
  949.                   (LRLLevel.Player[k].Position.y = i - 1) then
  950.                 begin
  951.                   LRLLevel.Field[j, i].Count := 13 - LRLLevel.Field[j, i].Count;
  952.                   LRLLevel.Field[j, i].Flags := LRLLevel.Field[j, i].Flags or 2;
  953.                   LRLLevel.Field[j, i].Flags := LRLLevel.Field[j, i].Flags and $FE - 4 - 8;
  954.                   LRLLevel.Field[j, i].Count := 1;
  955.                   Break;
  956.                 end;
  957.           end;
  958.           if LRLLevel.Field[j, i].Count = 6 then
  959.           begin
  960.             LRLLevel.Field[j, i].IdleCount := 100;
  961.           end;
  962.         end
  963.         else
  964.           Dec(LRLLevel.Field[j, i].IdleCount);
  965.         if LRLLevel.Field[j, i].Count = 12 then
  966.         begin
  967.           LRLLevel.Field[j, i].Flags := LRLLevel.Field[j, i].Flags or 2;
  968.           LRLLevel.Field[j, i].Flags := LRLLevel.Field[j, i].Flags and $FE - 4 - 8;
  969.           LRLLevel.Field[j, i].Count := 1;
  970.         end;
  971.       end;
  972.     end;
  973. end;
  974.  
  975.  
  976. procedure LRLComputerPlayer;
  977. var
  978.   k, l, m, f1, f2, i, j: Integer;
  979. begin
  980.   if ComputerTurn >= ComputerReaction then
  981.   begin
  982.     ComputerTurn := 0;
  983.     for k := 1 to 10 do
  984.     begin
  985.       with LRLLevel.Player[k] do
  986.       begin
  987.         if Controller = 2 then
  988.         begin
  989.           NewCommandWas := True;
  990.           NewCommand := 10;
  991.           if (Position.y > LRLLevel.Player[1].Position.y) then
  992.           begin
  993.             if ((LRLLevel.Field[Position.x, Position.y].Image = 2) and
  994.               (LRLLevel.Field[Position.x, Position.y].Flags and 16 = 0) and
  995.               ((LRLLevel.Field[Position.x, Position.y - 1].Image = 2) or
  996.               (LRLLevel.Field[Position.x, Position.y - 1].Flags and 4 <> 0)) and
  997.               (Position.y > 1)) then
  998.             begin
  999.               NewCommand := 3;
  1000.             end
  1001.             else
  1002.             begin
  1003.               m := 1;
  1004.               l := Position.x;
  1005.               i := 1;
  1006.               while i <> 0 do
  1007.               begin
  1008.                 l := l + i;
  1009.                 if ((LRLLevel.Field[l, Position.y].Image = 2) and
  1010.                   (LRLLevel.Field[l, Position.y].Flags and 16 = 0)) and
  1011.                   ((LRLLevel.Field[l, Position.y - 1].Image = 2) and
  1012.                   (LRLLevel.Field[l, Position.y - 1].Flags and 16 = 0)) and (Position.y <> 1) then
  1013.                 begin
  1014.                   if m = 0 then
  1015.                   begin
  1016.                     f2 := Position.x - l;
  1017.                     Break;
  1018.                   end;
  1019.                   m  := 0;
  1020.                   i  := not i + 1;
  1021.                   f1 := l - Position.x;
  1022.                   l  := Position.x;
  1023.                 end
  1024.                 else
  1025.                 if (LRLLevel.Field[l, Position.y].Flags and 8 = 0) or (l > 30) or (l < 1) then
  1026.                 begin
  1027.                   if m = 0 then
  1028.                   begin
  1029.                     f2 := 100;
  1030.                     Break;
  1031.                   end;
  1032.                   m  := 0;
  1033.                   i  := not i + 1;
  1034.                   l  := Position.x;
  1035.                   f1 := 100;
  1036.                 end;
  1037.               end;
  1038.               if (f1 = 100) and (f2 = 100) then
  1039.                 NewCommand := 10
  1040.               else
  1041.               begin
  1042.                 if f1 > f2 then
  1043.                   NewCommand := 1
  1044.                 else
  1045.                   NewCommand := 2;
  1046.               end;
  1047.             end;
  1048.           end
  1049.           else
  1050.           if (Position.y < LRLLevel.Player[1].Position.y) then
  1051.           begin
  1052.             if (((LRLLevel.Field[Position.x, Position.y + 1].Image = 2) and
  1053.               (LRLLevel.Field[Position.x, Position.y + 1].Flags and 16 = 0)) or
  1054.               (LRLLevel.Field[Position.x, Position.y + 1].Flags and 4 <> 0)) and
  1055.               (Position.y < 16) and (LRLLevel.Field[Position.x, Position.y + 1].Flags and 1 = 0) then
  1056.             begin
  1057.               NewCommand := 4;
  1058.             end
  1059.             else
  1060.             begin
  1061.               m := 1;
  1062.               l := Position.x;
  1063.               i := 1;
  1064.               while i <> 0 do
  1065.               begin
  1066.                 l := l + i;
  1067.                 if ((LRLLevel.Field[l, Position.y + 1].Image = 2) and
  1068.                   (LRLLevel.Field[l, Position.y + 1].Flags and 16 = 0)) or
  1069.                   ((LRLLevel.Field[l, Position.y + 1].Flags and 4 <> 0) and
  1070.                   (LRLLevel.Field[l, Position.y + 1].Flags and 1 = 0)) then
  1071.                 begin
  1072.                   if m = 0 then
  1073.                   begin
  1074.                     f2 := Position.x - l;
  1075.                     Break;
  1076.                   end;
  1077.                   m  := 0;
  1078.                   i  := not i + 1;
  1079.                   f1 := l - Position.x;
  1080.                   l  := Position.x;
  1081.                 end
  1082.                 else
  1083.                 if (LRLLevel.Field[l, Position.y].Flags and 8 = 0) or (l > 30) or (l < 1) then
  1084.                 begin
  1085.                   if m = 0 then
  1086.                   begin
  1087.                     f2 := 100;
  1088.                     Break;
  1089.                   end;
  1090.                   m  := 0;
  1091.                   i  := not i + 1;
  1092.                   l  := Position.x;
  1093.                   f1 := 100;
  1094.                 end;
  1095.               end;
  1096.               if (f1 = 100) and (f2 = 100) then
  1097.                 NewCommand := 10
  1098.               else
  1099.               begin
  1100.                 if f1 > f2 then
  1101.                   NewCommand := 1
  1102.                 else
  1103.                   NewCommand := 2;
  1104.               end;
  1105.             end;
  1106.           end
  1107.           else
  1108.           begin
  1109.             if (Position.x > LRLLevel.Player[1].Position.x) then
  1110.               NewCommand := 1;
  1111.             if (Position.x < LRLLevel.Player[1].Position.x) then
  1112.               NewCommand := 2;
  1113.           end;
  1114.         end;
  1115.       end;
  1116.     end;
  1117.   end
  1118.   else
  1119.     Inc(ComputerTurn);
  1120. end;
  1121.  
  1122.  
  1123. function LRLPlayLevel(Number: Byte): Word;
  1124. var
  1125.   Keypress: Word;
  1126.   i: Word;
  1127.   L, C: Longword;
  1128. begin
  1129.   Randomize;
  1130.   ComputerReaction := 1;
  1131.   LRLLoadLevel(Number);
  1132.  
  1133.   if LRLCLevel <> Number then
  1134.   begin
  1135.     GameResult := 50;
  1136.     Exit;
  1137.   end;
  1138.   if LRLLevel.Player[1].Controller <> 1 then
  1139.   begin
  1140.     GameResult := 60;
  1141.     Exit;
  1142.   end;
  1143.  
  1144.   TimeToRefresh := True;
  1145.   GameStarted := False;
  1146.   GameResult := 0;
  1147.   Paused := False;
  1148.   EndOfGame := False;
  1149.  
  1150.   LRLStartSequence;
  1151.  
  1152.   Keypress := 0;
  1153.   L := 0;
  1154.  
  1155.   repeat
  1156.     C := LastDosTick();
  1157.     if L <> C then
  1158.     begin
  1159.       L := C;
  1160.       if GameStarted and not Paused then
  1161.       begin
  1162.         LRLComputerPlayer;
  1163.         LRLUpdatePlayers;
  1164.         LRLUpdateBricks;
  1165.       end;
  1166.       LRLRedrawLevel;
  1167.       ScreenApply(LRLScreen^);
  1168.     end else
  1169.       Sleep(20);
  1170.  
  1171.     if Keypressed then
  1172.     begin
  1173.       Keypress := ReadKey;
  1174.       GameStarted := True;
  1175.       Paused := False;
  1176.  
  1177.       for i := 0 to ControlNumber - 1 do
  1178.       if KeyboardControls[i * 3 + 1] = Keypress then
  1179.       begin
  1180.         LRLLevel.Player[KeyboardControls[i * 3 + 2]].NewCommand := KeyboardControls[i * 3 + 3];
  1181.         LRLLevel.Player[KeyboardControls[i * 3 + 2]].NewCommandWas := True;
  1182.       end;
  1183.  
  1184.       if (Keypress = $50) or (Keypress = $70) then
  1185.         Paused := True;
  1186.     end;
  1187.   until (Keypress = $1B) or EndOfGame;
  1188.  
  1189.   if EndOfGame then
  1190.     LRLEndSequence else
  1191.     GameResult := 100;
  1192. end;
  1193.  
  1194. end.
  1195.