Prev: C91A Up: Map Next: CA7C
C92C: Build Room Scenery
Used by the routine at BuildRoom.
Makes the scenery objects of a room, from the data of the room, which starts at Room_00 and has a record for each of the rooms. A record is the room number, then a length byte (which counts itself and the bytes after it), then a byte holding the ink colour of the room in bits 0-2 and the size of the room in bits 3-7, then the features of the room and then the groups of templates. The size is the index of one of the three 03-byte entries at RoomSizeTable, which hold the distance from the centre of the room to the walls on the x axis and the y axis, and the height of the floor.
The features are pairs of bytes: the number of a feature and a parameter, ended by FF (or by the end of the data of the room, as 2B of the rooms have no FF). The number is the index into the table of addresses at FeatureTable. A feature is a list of the eight bytes of one or more objects (the type, x, y, z, the sizes along each axis and the flags) that ends with a zero in place of the type of another object. These are the doors and their frames, and the objects that are fixed to the walls. The parameter is stored in the room field of each of the objects in the feature, which makes it the number of the room that a door leads to.
Each group of templates that follows is a byte holding the number of a template in bits 3-7 and, in bits 0-2, one less than the number of cells that follow, and then a byte for each of the cells. A cell holds the x position in bits 0-2 and the y position in bits 3-5 (in steps of 10 from 48), and the height in bits 6-7 (in steps of 0C above the floor). A template is a list of the five bytes of one or more objects (the type, the sizes along each axis and the flags) that ends with a zero in place of the type of another object, and an object is made from it for each of the cells. The template numbers are the indexes into the table of addresses that starts at TemplateTable, which a group with the number 1F moves on by 20 templates, though no room has one. The last group of rooms 0D and 6C is cut short by the end of their data.
The objects are made from the last of the object slots, which is at ObjectsTable_Last, and downwards. The first feature of a room is always a door, with its frame in the last slot and the door in the one before, so that the doors are found by FindDoor in the slots at the top. The player and the creatures have the slots at the start.
Input
IX Object record of the player's legs
Clear the objects and point at the room data.
BuildRoomScenery C92C CALL ClearObjects Call ClearObjects to clear all of the objects except for the player.
C92F LD HL,$6CE5 Start with the first of the tables of templates, which is TemplateTable.
C932 LD ($A73A),HL
C935 XOR A Set the offset of the cells to zero.
C936 LD ($A73C),A
C939 LD DE,$AE0F Take the last of the object slots.
C93C LD BC,$696D Take the end of the room data, which is FeatureTable.
C93F LD HL,$5E10 Start at the first room record, which is Room_00.
Find the record for this room.
BuildRoomScenery_FindRoomRecord C942 LD A,(HL) Fetch the number of the room, and move on to the length.
C943 INC HL
C944 CP (IX+$08) Jump to BuildRoomScenery_ReadRoomHeader if it is the room that the player is in.
C947 JR Z,BuildRoomScenery_ReadRoomHeader
C949 LD A,(HL) Fetch the length of the record.
C94A CALL AddAToHL Call AddAToHL to move on to the next record.
C94D AND A Compare HL with the end of the room data, which sets the carry flag if it is before it.
C94E SBC HL,BC
C950 ADD HL,BC
C951 JR BuildRoomScenery_FindRoomRecord Jump to BuildRoomScenery_FindRoomRecord if there are more records.
Read the colour and size of the room.
BuildRoomScenery_ReadRoomHeader C953 LD B,(HL) Fetch the length of the record into B, and move on to the colour and size of the room.
C954 INC HL
C955 LD A,(HL) Fetch the colour and size of the room.
C956 AND %00000111 Keep the ink colour.
C958 OR %01000000 Set the bright bit.
C95A LD ($A738),A Store it as the colour of the room.
C95D PUSH DE Save the address of the next free object slot.
C95E EX DE,HL Put the address of the room data in DE.
C95F LD A,(DE) Fetch the colour and size again, and move on to the first feature.
C960 INC DE
C961 RRCA Move the size of the room to bits 0-4.
C962 RRCA
C963 RRCA
C964 AND %00011111 Keep it.
C966 LD C,A Put the size in C.
C967 ADD A,A Multiply the size by three.
C968 ADD A,C
C969 LD HL,$5E07 Take the sizes of the rooms, which is RoomSizeTable.
C96C CALL AddAToHL Call AddAToHL to find the entry for the size.
C96F LD A,(HL) Fetch the distance to the walls along x, move on and store it.
C970 INC HL
C971 LD ($A71D),A
C974 LD A,(HL) Fetch the distance to the walls along y, move on and store it.
C975 INC HL
C976 LD ($A71E),A
C979 LD A,(HL) Fetch the height of the floor and store it.
C97A LD ($A71F),A
C97D DEC B Count the length and the colour and size as read.
C97E DEC B
C97F EX DE,HL Put the address of the room data back in HL.
C980 POP DE Get the address of the next free object slot back.
Make the objects of each feature.
BuildRoomScenery_MakeFeature C981 LD A,(HL) Fetch the number of the feature, and move on to its parameter.
C982 INC HL
C983 CP $FF Jump to BuildRoomScenery_MakeTemplates if it is the end of the features.
C985 JR Z,BuildRoomScenery_MakeTemplates
C987 PUSH BC Save the number of bytes left to read and the address of the parameter.
C988 PUSH HL
C989 LD L,A Put the number of the feature in HL.
C98A LD H,$00
C98C ADD HL,HL Double it.
C98D LD BC,$696D Take the table of addresses of the features, which is FeatureTable.
C990 ADD HL,BC Add the offset to find the entry for the feature.
C991 LD A,(HL) Fetch the low byte of the address of the feature, and move on.
C992 INC HL
C993 LD H,(HL) Fetch the high byte, and make the address of the feature in HL.
C994 LD L,A
BuildRoomScenery_MakeFeatureObject C995 LD BC,$0008 Set the number of bytes that make an object in a feature, which is eight.
C998 LDIR Copy the type, position, sizes and flags into the object slot.
C99A EX (SP),HL Fetch the parameter, leaving the address in the feature on the stack.
C99B LD A,(HL)
C99C EX (SP),HL
C99D LD (DE),A Store it in the room field of the object, and move on.
C99E INC DE
C99F LD B,$17 Set the number of bytes to clear, which is the rest of the object.
C9A1 CALL ClearBytes Call ClearBytes to clear them.
C9A4 PUSH HL Save the address in the feature.
C9A5 LD HL,$FFC0 Subtract 20 from DE, by adding FFC0...
C9A8 ADD HL,DE
C9A9 EX DE,HL ...and put it back in DE.
C9AA POP HL Get the address in the feature back.
C9AB LD A,(HL) Jump to BuildRoomScenery_MakeFeatureObject to make another object if the next type in the feature is not zero.
C9AC AND A
C9AD JR NZ,BuildRoomScenery_MakeFeatureObject
C9AF POP HL Get the address of the parameter and the number of bytes left to read.
C9B0 POP BC
C9B1 INC HL Move on to the next feature.
C9B2 DEC B Count the parameter as read.
C9B3 DJNZ BuildRoomScenery_MakeFeature Jump to BuildRoomScenery_MakeFeature for the next feature, counting the number as read, while any data is left.
C9B5 RET Return.
Make the objects from the templates.
BuildRoomScenery_MakeTemplates C9B6 DEC B Count the end of the features as read.
C9B7 PUSH IY Save IY.
C9B9 PUSH DE Move on to the next free object slot (using the stack).
C9BA POP IY
BuildRoomScenery_MakeTemplateGroup C9BC LD A,(HL) Fetch the byte that holds the template and the number of cells.
C9BD AND %00000111 Keep the number of cells less one.
C9BF INC A Add one.
C9C0 LD C,A Put the number of cells in C.
C9C1 LD A,(HL) Fetch the byte again, and move on to the first cell.
C9C2 INC HL
C9C3 DEC B Count it as read.
C9C4 LD D,(HL) Fetch the first cell into D, and move on.
C9C5 INC HL
C9C6 PUSH HL Save the address of the next byte.
C9C7 RRCA Move the template number to bits 1-5, which doubles it.
C9C8 RRCA
C9C9 AND %00111110 Keep it.
C9CB CP $3E Jump to BuildRoomScenery_SwitchTemplateTable if it is the template number that moves on the table.
C9CD JP Z,BuildRoomScenery_SwitchTemplateTable
C9D0 LD HL,($A73A) Fetch the address of the table of templates.
C9D3 CALL AddAToHL Call AddAToHL to find the entry for the template.
C9D6 LD A,(HL) Fetch the low byte of the address of the template, and move on.
C9D7 INC HL
C9D8 LD H,(HL) Fetch the high byte, and make the address of the template in HL.
C9D9 LD L,A
Copy a template object into a cell.
BuildRoomScenery_MakeTemplateCell C9DA PUSH HL Save the address of the template.
BuildRoomScenery_MakeTemplateObject C9DB LD A,(HL) Copy the type from the template to the object.
C9DC INC HL
C9DD LD (IY+$00),A
C9E0 LD A,(HL) Copy the half size along x.
C9E1 INC HL
C9E2 LD (IY+$04),A
C9E5 LD A,(HL) Copy the half size along y.
C9E6 INC HL
C9E7 LD (IY+$05),A
C9EA LD A,(HL) Copy the half size along z.
C9EB INC HL
C9EC LD (IY+$06),A
C9EF LD A,(HL) Copy the flags.
C9F0 INC HL
C9F1 LD (IY+$07),A
C9F4 LD A,(IX+$08) Copy the room that the player is in to the room field of the object.
C9F7 LD (IY+$08),A
C9FA LD A,($A73C) Fetch the cell offset.
C9FD RLCA Move bit 0 of the offset to bit 3.
C9FE RLCA
C9FF RLCA
CA00 AND %00001000 Keep it.
CA02 LD E,A Put it in E.
CA03 LD A,D Fetch the cell.
CA04 RLCA Move the x position of the cell to bits 4-6.
CA05 RLCA
CA06 RLCA
CA07 RLCA
CA08 AND %01110000 Keep it.
CA0A ADD A,E Add bit 3 of the offset.
CA0B ADD A,$48 Add the start of the cells.
CA0D LD (IY+$01),A Store it as the x position of the object.
CA10 LD A,($A73C) Fetch the cell offset.
CA13 RLCA Move bit 1 of the offset to bit 3.
CA14 RLCA
CA15 AND %00001000 Keep it.
CA17 LD E,A Put it in E.
CA18 LD A,D Fetch the cell.
CA19 RLCA Move the y position of the cell to bits 4-6.
CA1A AND %01110000 Keep it.
CA1C ADD A,E Add bit 3 of the offset.
CA1D ADD A,$48 Add the start of the cells.
CA1F LD (IY+$02),A Store it as the y position of the object.
CA22 LD A,D Fetch the cell.
CA23 RLCA Move the height of the cell to bits 0-1.
CA24 RLCA
CA25 AND %00000011 Keep it.
CA27 ADD A,A Multiply it by four.
CA28 ADD A,A
CA29 LD E,A Put it in E.
CA2A ADD A,A Multiply it by eight.
CA2B ADD A,E Add the four times to make it twelve times the height.
CA2C LD E,A Put it in E.
CA2D LD A,($A73C) Fetch the cell offset.
CA30 ADD A,E Add the height.
CA31 AND %11111100 Keep the multiple of four.
CA33 LD E,A Put it in E.
CA34 LD A,($A71F) Fetch the height of the floor.
CA37 ADD A,E Add the height of the cell.
CA38 LD (IY+$03),A Store it as the z position of the object.
CA3B PUSH BC Save the number of bytes left to read and the number of cells.
CA3C LD BC,$0009 Set the offset from the type to the byte after the room field, which is 09.
CA3F ADD IY,BC Add it to IY.
CA41 LD B,$17 Set the number of bytes to clear, which is the rest of the object.
BuildRoomScenery_ClearTemplateObject CA43 LD (IY+$00),$00 Clear a byte of the object.
CA47 INC IY Move on to the next byte.
CA49 DJNZ BuildRoomScenery_ClearTemplateObject Jump to BuildRoomScenery_ClearTemplateObject until the rest of the object is clear.
CA4B LD BC,$FFC0 Subtract 40 from IY, which moves it to the slot below the object.
CA4E ADD IY,BC
CA50 POP BC Get the number of bytes left to read and the number of cells back.
CA51 LD A,(HL) Jump to BuildRoomScenery_MakeTemplateObject to make another object if the next type in the template is not zero.
CA52 AND A
CA53 JR NZ,BuildRoomScenery_MakeTemplateObject
CA55 POP DE Get the address of the template and the address of the next byte of the room data.
CA56 POP HL
CA57 DEC B Count the cell as read, and jump to BuildRoomScenery_FinishRoomScenery if there is no more room data.
CA58 JR Z,BuildRoomScenery_FinishRoomScenery
CA5A DEC C Count the object as made, and jump to BuildRoomScenery_MakeTemplateGroup for the next group if there is no cell left.
CA5B JP Z,BuildRoomScenery_MakeTemplateGroup
CA5E LD A,(HL) Fetch the next cell, and move on.
CA5F INC HL
CA60 PUSH HL Save the address of the next byte.
CA61 EX DE,HL Put the address of the template in HL.
CA62 LD D,A Put the cell in D.
CA63 JP BuildRoomScenery_MakeTemplateCell Jump to BuildRoomScenery_MakeTemplateCell to make the object for it.
Finish, or switch the template table.
BuildRoomScenery_FinishRoomScenery CA66 PUSH IY Put the address of the next free object slot in DE (using the stack).
CA68 POP DE
CA69 POP IY Get the original IY back.
CA6B RET Return.
BuildRoomScenery_SwitchTemplateTable CA6C LD HL,($A73A) Fetch the address of the table of templates.
CA6F LD A,$40 Set the number of bytes to move on, which is 20 templates.
CA71 CALL AddAToHL Call AddAToHL to move on.
CA74 LD ($A73A),HL Store it as the table of templates.
This entry point is used by the routine at SetCellOffset.
BuildRoomScenery_NextTemplateGroup CA77 DEC B Count the unused cell as read.
CA78 POP HL Get the address of the next group back.
CA79 JP BuildRoomScenery_MakeTemplateGroup Jump to BuildRoomScenery_MakeTemplateGroup to make the objects for it.
View the equivalent code in;
Prev: C91A Up: Map Next: CA7C