.GetEnemyCount LDA landscapeNumberHi \ Set T = (landscapeNumberHi / 4) + 2 LSR A \ LSR A \ Because the landscape number is in BCD and in the form LSR A \ 0000 to 9999, this extracts the top digit and adds 2 LSR A \ CLC \ So T is in the range 2 to 11, with higher values of T ADC #2 \ for higher landscape numbers STA T .enem1 JSR GetNextSeedNumber \ Set A to the next number from the landscape's sequence \ of seed numbers, which we now use to calculate the \ enemy count for this landscape (so the same number is \ calculated for the same landscape number each time) LDY #7 \ Set Y = 7 to use as the count of clear bits in A when \ A is zero ASL A \ Set the C flag from bit 7 of this landscape's seed \ number and clear bit 0 of A, leaving bits 6 to 0 of \ the original A in bits 7 to 1 PHP \ Store the status flags on the stack, so we can use the \ C flag below to decide whether to negate the result \ We now count the number of continuous clear bits at \ the top of A, ignoring bit 0, so we count zeroes from \ bit 7 down until we hit a 1, and put the result into Y BEQ enem3 \ If A = 0 then jump to enem3 with Y = 7, as we have a \ continuous run of seven clear bits in bits 7 to 1 LDY #&FF \ Otherwise set Y = -1 so the following loop counts the \ number of zeroes correctly .enem2 INY \ Increment the zero counter in Y ASL A \ Shift A to the left, moving the top bit into the C \ flag BCC enem2 \ Loop back to keep shifting and counting zeroes until \ we shift a 1 out of bit 7, at which point Y contains \ the length of the run of zeroes in bits 6 to 0 of the \ landscape's original seed number .enem3 TYA \ At this point Y contains a number in the range 0 to 7, \ so copy this into A PLP \ If the C flag we stored on the stack above was set, BCC enem4 \ invert A, so this flips the result into the range -1 EOR #%11111111 \ to -8 if bit 7 of the landscape's original seed number \ was set .enem4 \ At this point A is in the range -8 to 7 CLC \ Set A = A + T ADC T \ \ T is in the range 2 to 11, so A is now in the range \ -6 to 18 CMP #8 \ If A < 0 or A >= 8 then loop back to enem1 to try BCS enem1 \ again \ If we get here then A is now in the range 0 to 7, with \ higher values for higher landscape numbers ADC #1 \ Set A = A + 1 \ \ This addition works as we know the C flag is clear \ because we just passed through a BCS \ So A is now a number in the range 1 to 8, with higher \ values for higher landscape numbers, which we can use \ as our enemy count (after capping it to the value of \ maxNumberOfEnemies after we return from the \ subroutine) RTS \ Return from the subroutineName: GetEnemyCount [Show more] Type: Subroutine Category: Landscape Summary: Calculate the number of enemies for the current landscapeContext: See this subroutine in context in the source code References: This subroutine is called as follows: * SpawnEnemies calls GetEnemyCount
Returns: A The enemy count for the landscape (in the range 1 to 8, with higher values for higher landscape numbers)
[X]
Subroutine GetNextSeedNumber (category: Maths (Arithmetic))
Set A to a seed number
[X]
Label enem1 is local to this routine
[X]
Label enem2 is local to this routine
[X]
Label enem3 is local to this routine
[X]
Label enem4 is local to this routine
[X]
Variable landscapeNumberHi in workspace Main variable workspace
The high byte of the four-digit binary coded decimal landscape number (0000 to 9999)