Prev: 605B Up: Map Next: 61A9
6113: Choose Item Sites
Used by the routine at StartNewGame.
Chooses the sites of the items on a planet. Each site is a four byte record (map column, map zone, horizontal position and vertical position) copied into the table in DE. The sites are picked at random from the five pools of candidate records at ItemSitesPool3, ItemSitesPool1, ItemSitesPool5, ItemSitesPool2 and ItemSitesPool4, using the list of pool numbers at ItemSitePoolNumbers. A pool number of zero leaves the site unset or, when the next pool number is 03, copies the previous site with the positions adjusted.
Bit 7 of the first byte of a pool record marks it as used so that the same record is never picked twice in one go.
Input
DE Address of the table to fill (ItemSites for player one, OtherPlayerState_ItemSites for player two)
Start at the beginning of the list of pool numbers.
ChooseItemSites 6113 LD IY,$61A9 Use the list of pool numbers at ItemSitePoolNumbers.
This entry point is used by the routine at CompletePlanet.
Mark all of the pool records as unused.
ChooseItemSites_ClearUsed 6117 LD HL,$605B Use the first pool record.
611A LD B,$2E Set a counter in B for 2E records.
ChooseItemSites_ClearLoop 611C LD A,(HL) Fetch the first byte of the record.
611D AND %01111111 Clear its used flag (bit 7).
611F LD (HL),A Write it back.
6120 INC HL Move on to the next record.
6121 INC HL
6122 INC HL
6123 INC HL
6124 DJNZ ChooseItemSites_ClearLoop Decrease the record counter by one and loop back to ChooseItemSites_ClearLoop until all of the records have been cleared.
6126 LD A,$FF Make the first byte of the record at ItemSitesPool2 FF, so it can never be picked.
6128 LD ($60E3),A
612B LD ($60EB),A Do the same for the record at 60EB.
Work through the list of pool numbers, filling one site each time.
ChooseItemSites_Loop 612E LD A,(IY+$00) Fetch the pool number for this site.
6131 AND A Test whether it is zero.
6132 JR NZ,ChooseItemSites_SelectPool Jump to ChooseItemSites_SelectPool if it is not.
6134 LD A,(IY+$01) Fetch the next pool number from the list.
6137 CP $03 Jump to ChooseItemSites_Skip if it is not equal to 03, leaving this site unset.
6139 JR NZ,ChooseItemSites_Skip
This site is a copy of the previous one, moved along a little.
613B PUSH DE Copy the table pointer from DE to HL.
613C POP HL
613D DEC HL Step back to the previous site.
613E DEC HL
613F DEC HL
6140 DEC HL
6141 LD BC,$0004 Set the length of a site record in BC.
6144 LDIR Copy the previous site into this one.
6146 DEC DE Step back to the last byte of the site, the vertical position.
6147 LD A,(DE) Fetch it...
6148 SUB $23 ...subtract 23...
614A LD (DE),A ...and write it back.
614B DEC DE Step back to the horizontal position.
614C LD A,(DE) Fetch it...
614D ADD A,$10 ...add 10...
614F LD (DE),A ...and write it back.
6150 JR ChooseItemSites_SkipRest Jump to ChooseItemSites_SkipRest.
Select the pool for this pool number.
ChooseItemSites_SelectPool 6152 LD C,$0F Set the pool size mask to 0F (sixteen records).
6154 LD HL,$609B Use the pool for pool number 01.
6157 CP $01 Jump to ChooseItemSites_PickRecord if the pool number is 01.
6159 JR Z,ChooseItemSites_PickRecord
615B LD HL,$605B Use the pool for pool number 03.
615E CP $03 Jump to ChooseItemSites_PickRecord if the pool number is 03.
6160 JR Z,ChooseItemSites_PickRecord
6162 LD C,$07 Set the pool size mask to 07 (eight records).
6164 LD HL,$60E3 Use the pool for pool number 02.
6167 CP $02 Jump to ChooseItemSites_PickRecord if the pool number is 02.
6169 JR Z,ChooseItemSites_PickRecord
616B LD C,$03 Set the pool size mask to 03 (four records).
616D LD HL,$6103 Use the pool for pool number 04.
6170 CP $04 Jump to ChooseItemSites_PickRecord if the pool number is 04.
6172 JR Z,ChooseItemSites_PickRecord
6174 LD HL,$60DB Use the pool for pool number 05.
6177 LD C,$01 Set the pool size mask to 01 (two records).
Pick an unused record from the pool at random.
ChooseItemSites_PickRecord 6179 PUSH HL Stash the address of the pool on the stack.
617A LD A,($9BCB) Fetch a random number.
617D AND C Keep only the bits allowed by the pool size.
617E ADD A,A Multiply it by four to get an offset into the pool.
617F ADD A,A
6180 CALL AddAToHL Add the offset to HL.
6183 BIT 7,(HL) Jump to ChooseItemSites_CopyRecord if it has not been used.
6185 JR Z,ChooseItemSites_CopyRecord
6187 CALL RandomNumber Call RandomNumber to get a new random number.
618A POP HL Restore the address of the pool.
618B JR ChooseItemSites_PickRecord Jump back to ChooseItemSites_PickRecord to try again.
Copy the unused record into the site table and mark it used.
ChooseItemSites_CopyRecord 618D LD BC,$0004 Set the length of a site record in BC.
6190 LDIR Copy the record into the site table.
6192 DEC HL Step back to the start of the pool record.
6193 DEC HL
6194 DEC HL
6195 DEC HL
6196 SET 7,(HL) Set the used flag of the pool record.
6198 POP HL Restore the address of the pool.
6199 JR ChooseItemSites_Next Jump to ChooseItemSites_Next.
ChooseItemSites_Skip 619B INC DE Skip the first two bytes of this site, leaving it unset.
619C INC DE
ChooseItemSites_SkipRest 619D INC DE Skip the last two bytes of the site.
619E INC DE
Move on to the next pool number, unless this was the last one.
ChooseItemSites_Next 619F LD A,(IY+$00) Fetch the pool number just dealt with.
61A2 CP $04 Return if it was 04, as the last pool number in the list is always 04.
61A4 RET Z
61A5 INC IY Use the next pool number.
61A7 JR ChooseItemSites_Loop Jump back to ChooseItemSites_Loop.
Prev: 605B Up: Map Next: 61A9