Prev: D0BC Up: Map Next: D292
D0BD: Draw Terrain
Used by the routine at ScrollWorld.
Draws the terrain of the four tiles that are in view, by running the script of each tile. The number of the tile is kept in TileNumber: bit 0 is set for a tile below the first and bit 1 for a tile to the right. The scripts of the tiles are found from the table at TileScriptPointer. Each tile is a list of segments, and each segment is set up with SetUpSegment and ReadSegmentShapes and then drawn step by step, each step of a segment being a piece of picture. TilesChanged is non-zero when the tiles have changed, and then the routine also makes a record of each segment in the area from GameEntry: a count byte for the tile followed by five bytes for each segment (the position, the size and the code from SegmentCodes), which the routines that move objects use to test for collisions with the terrain. The segments that are not near the position at PositionAcross and PositionDown are not drawn.
DrawTerrain D0BD LD A,($5C3C) The scroll-in flag.
D0C0 AND A Jump to DrawTerrain_Tables if A is zero.
D0C1 JR Z,DrawTerrain_Tables
D0C3 LD ($5C3D),A Update the tiles changed.
Choose the table of shifted pictures for the horizontal offset, and the offsets table that goes with it.
DrawTerrain_Tables D0C6 LD A,($CE42) The vertical offset.
D0C9 ADD A,$60 Add 60 to A.
D0CB LD ($CE42),A Update the vertical offset.
D0CE LD DE,$CCBF The picture offsets.
D0D1 LD ($CE4B),DE Update the picture offsets pointer.
D0D5 EXX Switch to the alternate registers.
D0D6 LD HL,$E37D The shape table shift zero.
D0D9 LD A,($CE41) The horizontal offset.
D0DC AND %00000111 Keep the bits of A that are set in 07.
D0DE JR Z,DrawTerrain_Tile Jump to DrawTerrain_Tile if the zero flag is set.
D0E0 EXX Switch to the alternate registers.
D0E1 INC DE Advance DE.
D0E2 LD ($CE4B),DE Update the picture offsets pointer.
D0E6 EXX Switch to the alternate registers.
D0E7 LD HL,$E2BD The shape table shift two.
D0EA CP $02 Jump to DrawTerrain_Tile if A is 02.
D0EC JR Z,DrawTerrain_Tile
D0EE LD HL,$E2FD The shape table shift four.
D0F1 CP $04 Jump to DrawTerrain_Tile if A is 04.
D0F3 JR Z,DrawTerrain_Tile
D0F5 LD HL,$E33D The shape table shift six.
Run the script of each of the four tiles in turn.
DrawTerrain_Tile D0F8 LD ($CCBD),HL Update the shape table pointer.
D0FB XOR A Clear A.
D0FC LD ($5C1D),A Update the tile number.
This entry point is used by the routine at NextSegment.
DrawTerrain_Record D0FF LD IX,$5B80 The decryption routine.
D103 LD A,($5C1D) The tile number.
D106 AND A Jump to DrawTerrain_Script if A is zero.
D107 JR Z,DrawTerrain_Script
D109 LD IX,$5BA5 Use 5BA5.
D10D CP $01 Jump to DrawTerrain_Script if A is 01.
D10F JR Z,DrawTerrain_Script
D111 LD IX,$5BCA Use 5BCA.
D115 CP $02 Jump to DrawTerrain_Script if A is 02.
D117 JR Z,DrawTerrain_Script
D119 LD IX,$5BEF Use 5BEF.
Read the address of the script of the tile and see if it starts with the prefix bytes that draw it upside down (99) and back to front (FF).
DrawTerrain_Script D11D LD HL,($DC87) Fetch the word at TileScriptPointer into HL.
D120 LD E,(HL) Read HL into E.
D121 INC HL Advance HL.
D122 LD D,(HL) Read HL into D.
D123 INC HL Advance HL.
D124 LD ($DC87),HL Update the tile script pointer.
D127 EX DE,HL Swap DE and HL.
D128 XOR A Clear A.
D129 LD ($5C30),A Update the back to front flag.
D12C LD ($5C26),A Update the upside down flag.
D12F LD (IX+$00),A Write A to offset 00 of IX.
D132 LD A,(HL) Read HL.
D133 AND A Jump to NextSegment_Tile if A is zero.
D134 JP Z,NextSegment_Tile
D137 CP $99 Jump to DrawTerrain_BackToFront if A is not 99.
D139 JR NZ,DrawTerrain_BackToFront
D13B LD ($5C26),A Update the upside down flag.
D13E INC HL Advance HL.
DrawTerrain_BackToFront D13F LD A,(HL) Read HL.
D140 CP $FF Jump to DrawTerrain_Count if A is not FF.
D142 JR NZ,DrawTerrain_Count
D144 LD ($5C30),A Update the back to front flag.
D147 INC HL Advance HL.
DrawTerrain_Count D148 LD A,(HL) Read HL.
D149 CP $95 Jump to DrawTerrain_Remember if A is less than 95.
D14B JR C,DrawTerrain_Remember
D14D INC HL Advance HL.
D14E LD A,(HL) Read HL.
DrawTerrain_Remember D14F LD ($5C20),A Update the segments still to draw.
D152 LD (IX+$00),A Write A to offset 00 of IX.
D155 INC IX Advance IX.
D157 INC HL Advance HL.
D158 LD ($BA09),IX The segment record.
This entry point is used by the routine at NextSegment.
Start a segment: clear the variables, read its position and type from the script and set up its slope and shapes.
DrawTerrain_Segment D15C LD IX,$DEDC The segment record.
D160 XOR A Clear A.
D161 LD ($5C2F),A Update the flagged steps.
D164 LD ($CE54),A Update the steps already drawn.
D167 LD ($5C32),A Update the last contact flags.
D16A LD ($5C27),A Update the shape flag.
D16D LD ($5C34),A Update the segment end shape.
D170 LD ($5C33),A Update the segment start shape.
D173 LD ($5C35),A Update the upside-down segment flag.
D176 LD ($CE5A),A Update the vertical step copy.
D179 LD ($D85D),A Update the edge flag.
D17C LD (IX+$01),A Write A to offset 01 of IX.
D17F LD A,(HL) Read HL.
D180 LD ($CE43),A Update the segment column.
D183 INC HL Advance HL.
D184 LD A,(HL) Read HL.
D185 LD ($CE44),A Update the segment row.
D188 LD ($CE3C),A The segment row.
D18B INC HL Advance HL.
D18C LD A,(HL) Read HL.
D18D RRCA Rotate A right.
D18E RRCA Rotate A right.
D18F RRCA Rotate A right.
D190 RRCA Rotate A right.
D191 AND %00001111 Keep the bits of A that are set in 0F.
D193 LD (IX+$03),A Write A to offset 03 of IX.
D196 EXX Switch to the alternate registers.
D197 CALL SetUpSegment Call SetUpSegment.
D19A EXX Switch to the alternate registers.
D19B LD A,(HL) Read HL.
D19C AND %00001111 Keep the bits of A that are set in 0F.
D19E JR NZ,DrawTerrain_Length Jump to DrawTerrain_Length if the zero flag is clear.
D1A0 LD A,$10 A=10.
DrawTerrain_Length D1A2 LD ($5C22),A Update the segment length.
D1A5 LD C,A C=A.
D1A6 INC HL Advance HL.
D1A7 CALL ReadSegmentShapes Call ReadSegmentShapes.
D1AA LD HL,$CE24 The column phase table.
D1AD LD B,(IX+$03) Read offset 03 of IX into B.
D1B0 EXX Switch to the alternate registers.
D1B1 LD IX,($BA09) Fetch the word at BA09 into IX.
If the tiles have changed, make the record of the segment.
DrawTerrain_Changed D1B5 LD A,($5C3D) The tiles changed.
D1B8 AND A Jump to DrawTerrain_Test if A is zero.
D1B9 JP Z,DrawTerrain_Test
D1BC LD A,($DEDF) The segment type.
D1BF LD E,A E=A.
D1C0 LD D,$00 Clear D.
D1C2 LD HL,$D0AC The segment codes.
D1C5 ADD HL,DE HL gains DE.
D1C6 LD A,(HL) Read HL.
D1C7 LD (IX+$04),A Write A to offset 04 of IX.
D1CA LD A,($5C22) The segment length.
D1CD LD (IX+$02),A Write A to offset 02 of IX.
D1D0 LD A,($CE43) The segment column.
D1D3 LD (IX+$00),A Write A to offset 00 of IX.
D1D6 LD A,($CE44) The segment row.
D1D9 LD (IX+$01),A Write A to offset 01 of IX.
D1DC LD A,($CE37) The vertical step.
D1DF AND A Jump to DrawTerrain_Sloped if A is not zero.
D1E0 JR NZ,DrawTerrain_Sloped
D1E2 LD B,(IX+$02) Read offset 02 of IX into B.
D1E5 XOR A Clear A.
DrawTerrain_FlatWidth D1E6 ADD A,$18 Add 18 to A.
D1E8 DJNZ DrawTerrain_FlatWidth Decrease B and jump to DrawTerrain_FlatWidth until none remain.
D1EA LD (IX+$02),A Write A to offset 02 of IX.
D1ED LD A,($DEDF) The segment type.
D1F0 CP $07 Compare A with 07.
D1F2 LD (IX+$03),$14 Store 14 at offset 03 of IX.
D1F6 JP Z,DrawTerrain_Test Jump to DrawTerrain_Test if the zero flag is set.
D1F9 LD A,(IX+$01) Read offset 01 of IX.
D1FC ADD A,$10 Add 10 to A.
D1FE LD (IX+$01),A Write A to offset 01 of IX.
D201 JP DrawTerrain_Test Jump to DrawTerrain_Test.
DrawTerrain_Sloped D204 LD A,($CE59) The horizontal step copy.
D207 AND A Jump to DrawTerrain_Extent if A is not zero.
D208 JR NZ,DrawTerrain_Extent
D20A LD B,(IX+$02) Read offset 02 of IX into B.
D20D XOR A Clear A.
DrawTerrain_SlopeWidth D20E ADD A,$10 Add 10 to A.
D210 DJNZ DrawTerrain_SlopeWidth Decrease B and jump to DrawTerrain_SlopeWidth until none remain.
D212 LD B,A B=A.
D213 LD (IX+$02),$18 Store 18 at offset 02 of IX.
D217 LD A,($DEDF) The segment type.
D21A LD (IX+$03),B Write B to offset 03 of IX.
D21D CP $0F Jump to DrawTerrain_Test if A is not 0F.
D21F JP NZ,DrawTerrain_Test
D222 LD A,(IX+$00) Read offset 00 of IX.
D225 SUB $10 Subtract 10 from A.
D227 LD (IX+$00),A Write A to offset 00 of IX.
D22A JP DrawTerrain_Test Jump to DrawTerrain_Test.
DrawTerrain_Extent D22D LD A,($E41B) The slope direction.
D230 CP $FF Jump to DrawTerrain_End if A is FF.
D232 JR Z,DrawTerrain_End
D234 LD A,($5C25) The horizontal step size.
D237 ADD A,(IX+$00) Add the byte at offset 00 of IX to A.
D23A LD (IX+$00),A Write A to offset 00 of IX.
DrawTerrain_End D23D LD B,(IX+$02) Read offset 02 of IX into B.
D240 LD D,(IX+$00) Read offset 00 of IX into D.
D243 LD E,(IX+$01) Read offset 01 of IX into E.
DrawTerrain_ExtentLoop D246 LD A,($CE35) The horizontal step.
D249 ADD A,D A gains D.
D24A LD D,A D=A.
D24B LD A,($CE37) The vertical step.
D24E NEG Negate A.
D250 ADD A,E A gains E.
D251 LD E,A E=A.
D252 DJNZ DrawTerrain_ExtentLoop Decrease B and jump to DrawTerrain_ExtentLoop until none remain.
D254 LD (IX+$03),E Write E to offset 03 of IX.
D257 LD (IX+$02),D Write D to offset 02 of IX.
Test whether the segment is near the position at PositionAcross and PositionDown, and skip it if it is not.
DrawTerrain_Test D25A LD A,($CE45) The position across.
D25D LD B,A B=A.
D25E LD A,($5C1D) The tile number.
D261 BIT 1,A Jump to DrawTerrain_TestVertical if bit 1 of A is clear.
D263 JR Z,DrawTerrain_TestVertical
D265 CALL TestRightColumn Call TestRightColumn.
D268 JR DrawTerrain_Vertical Jump to DrawTerrain_Vertical.
DrawTerrain_TestVertical D26A CALL TestLeftColumn Call TestLeftColumn.
DrawTerrain_Vertical D26D JP NZ,DrawTerrain_Skip Jump to DrawTerrain_Skip if the zero flag is clear.
D270 LD A,($CE46) The position down.
D273 LD B,A B=A.
D274 LD A,($5C1D) The tile number.
D277 BIT 0,A Jump to DrawTerrain_Top if bit 0 of A is clear.
D279 JR Z,DrawTerrain_Top
D27B CALL TestBottomRow Call TestBottomRow.
D27E JR DrawTerrain_Near Jump to DrawTerrain_Near.
DrawTerrain_Top D280 CALL TestTopRow Call TestTopRow.
DrawTerrain_Near D283 JP NZ,DrawTerrain_Skip Jump to DrawTerrain_Skip if the zero flag is clear.
D286 CALL NextRecord Call NextRecord.
D289 JP NextStep_Test Jump to NextStep_Test.
DrawTerrain_Skip D28C CALL NextRecord Call NextRecord.
D28F JP NextSegment Jump to NextSegment.
Prev: D0BC Up: Map Next: D292