Prev: 24667 Up: Map Next: 25001
24851: 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 3, 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 24851 LD IY,25001 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 24855 LD HL,24667 Use the first pool record.
24858 LD B,46 Set a counter in B for 46 records.
ChooseItemSites_ClearLoop 24860 LD A,(HL) Fetch the first byte of the record.
24861 AND %01111111 Clear its used flag (bit 7).
24863 LD (HL),A Write it back.
24864 INC HL Move on to the next record.
24865 INC HL
24866 INC HL
24867 INC HL
24868 DJNZ ChooseItemSites_ClearLoop Decrease the record counter by one and loop back to ChooseItemSites_ClearLoop until all of the records have been cleared.
24870 LD A,255 Make the first byte of the record at ItemSitesPool2 255, so it can never be picked.
24872 LD (24803),A
24875 LD (24811),A Do the same for the record at 24811.
Work through the list of pool numbers, filling one site each time.
ChooseItemSites_Loop 24878 LD A,(IY+0) Fetch the pool number for this site.
24881 AND A Test whether it is zero.
24882 JR NZ,ChooseItemSites_SelectPool Jump to ChooseItemSites_SelectPool if it is not.
24884 LD A,(IY+1) Fetch the next pool number from the list.
24887 CP 3 Jump to ChooseItemSites_Skip if it is not equal to 3, leaving this site unset.
24889 JR NZ,ChooseItemSites_Skip
This site is a copy of the previous one, moved along a little.
24891 PUSH DE Copy the table pointer from DE to HL.
24892 POP HL
24893 DEC HL Step back to the previous site.
24894 DEC HL
24895 DEC HL
24896 DEC HL
24897 LD BC,4 Set the length of a site record in BC.
24900 LDIR Copy the previous site into this one.
24902 DEC DE Step back to the last byte of the site, the vertical position.
24903 LD A,(DE) Fetch it...
24904 SUB 35 ...subtract 35...
24906 LD (DE),A ...and write it back.
24907 DEC DE Step back to the horizontal position.
24908 LD A,(DE) Fetch it...
24909 ADD A,16 ...add 16...
24911 LD (DE),A ...and write it back.
24912 JR ChooseItemSites_SkipRest Jump to ChooseItemSites_SkipRest.
Select the pool for this pool number.
ChooseItemSites_SelectPool 24914 LD C,15 Set the pool size mask to 15 (sixteen records).
24916 LD HL,24731 Use the pool for pool number 1.
24919 CP 1 Jump to ChooseItemSites_PickRecord if the pool number is 1.
24921 JR Z,ChooseItemSites_PickRecord
24923 LD HL,24667 Use the pool for pool number 3.
24926 CP 3 Jump to ChooseItemSites_PickRecord if the pool number is 3.
24928 JR Z,ChooseItemSites_PickRecord
24930 LD C,7 Set the pool size mask to 7 (eight records).
24932 LD HL,24803 Use the pool for pool number 2.
24935 CP 2 Jump to ChooseItemSites_PickRecord if the pool number is 2.
24937 JR Z,ChooseItemSites_PickRecord
24939 LD C,3 Set the pool size mask to 3 (four records).
24941 LD HL,24835 Use the pool for pool number 4.
24944 CP 4 Jump to ChooseItemSites_PickRecord if the pool number is 4.
24946 JR Z,ChooseItemSites_PickRecord
24948 LD HL,24795 Use the pool for pool number 5.
24951 LD C,1 Set the pool size mask to 1 (two records).
Pick an unused record from the pool at random.
ChooseItemSites_PickRecord 24953 PUSH HL Stash the address of the pool on the stack.
24954 LD A,(39883) Fetch a random number.
24957 AND C Keep only the bits allowed by the pool size.
24958 ADD A,A Multiply it by four to get an offset into the pool.
24959 ADD A,A
24960 CALL AddAToHL Add the offset to HL.
24963 BIT 7,(HL) Jump to ChooseItemSites_CopyRecord if it has not been used.
24965 JR Z,ChooseItemSites_CopyRecord
24967 CALL RandomNumber Call RandomNumber to get a new random number.
24970 POP HL Restore the address of the pool.
24971 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 24973 LD BC,4 Set the length of a site record in BC.
24976 LDIR Copy the record into the site table.
24978 DEC HL Step back to the start of the pool record.
24979 DEC HL
24980 DEC HL
24981 DEC HL
24982 SET 7,(HL) Set the used flag of the pool record.
24984 POP HL Restore the address of the pool.
24985 JR ChooseItemSites_Next Jump to ChooseItemSites_Next.
ChooseItemSites_Skip 24987 INC DE Skip the first two bytes of this site, leaving it unset.
24988 INC DE
ChooseItemSites_SkipRest 24989 INC DE Skip the last two bytes of the site.
24990 INC DE
Move on to the next pool number, unless this was the last one.
ChooseItemSites_Next 24991 LD A,(IY+0) Fetch the pool number just dealt with.
24994 CP 4 Return if it was 4, as the last pool number in the list is always 4.
24996 RET Z
24997 INC IY Use the next pool number.
24999 JR ChooseItemSites_Loop Jump back to ChooseItemSites_Loop.
Prev: 24667 Up: Map Next: 25001