.SpawnTrees LDA #48 \ Set U = 48 - 3 * numberOfEnemies SEC \ SBC numberOfEnemies \ We use this to cap the number of trees we add to the SBC numberOfEnemies \ landscape (though it only affects higher levels) SBC numberOfEnemies STA U JSR GetNextSeed0To22 \ Set A to the next number from the landscape's sequence \ of seed numbers, converted to the range 0 to 22 CLC \ Set A to this number, converted to the range 10 to 32 ADC #10 CMP U \ If A >= U then set A = U BCC tree1 \ LDA U \ So A = min(U, A) .tree1 STA treeCounter \ By this point A contains a value in the range 10 to 32 \ that's no greater than 48 - 3 * numberOfEnemies \ \ So when numberOfEnemies is six or more, this reduces \ the value of A as follows: \ \ * When numberOfEnemies = 6, range is 10 to 30 \ * When numberOfEnemies = 7, range is 10 to 27 \ * When numberOfEnemies = 8, range is 10 to 24 \ \ As the total number of enemies and trees determines \ the total amount of energy in the landscape, and each \ sentry is worth 3 energy points, this calculation \ ensures that the total amount of energy in the \ landscape is never greater than 49 (as the Sentinel is \ worth 4 energy points, while each sentry is worth 3) \ \ We now try to add this number of trees to the \ landscape, so store the result in treeCounter to use \ as a counter in the following loop .tree2 LDA #2 \ Fetch a new object number that we can use for the tree JSR GetObjectNumber \ (an object of type 2), returning the number of the new \ object in X and currentObject LDA minEnemyAltitude \ Set A to the altitude of the lowest enemy on the \ landscape, so we try to spawn all the trees at a lower \ altitude to the enemies JSR SpawnObjectBelow \ Attempt to spawn the tree object on a tile that is \ below the maximum altitude specified in A (though we \ may end up placing the object higher than this) BCS tree3 \ If the call to SpawnObjectBelow sets the C flag then \ the object has not been successfully spawned, so jump \ to tree3 to stop adding trees to the landscape DEC treeCounter \ Decrement the tree counter BNE tree2 \ Loop back until we have spawned the number of trees \ in treeCounter .tree3 \ We have now placed all the objects on the landscape, \ so now we fall through into CheckSecretCode to check \ that the player entered the correct secret code for \ this landscapeName: SpawnTrees [Show more] Type: Subroutine Category: Landscape Summary: Add trees to the landscape, ideally placing them below all the enemies in the landscape Deep dive: Adding enemies and trees to the landscape Object managementContext: See this subroutine in context in the source code References: This subroutine is called as follows: * SpawnPlayer calls SpawnTrees
[X]
Subroutine GetNextSeed0To22 (category: Maths (Arithmetic))
Set A to the next number from the landscape's sequence of seed numbers, converted to the range 0 to 22
[X]
Subroutine GetObjectNumber (category: 3D objects)
Fetch an object number that we can use for a new object of the specified type, and add the type to the objectTypes table
[X]
Subroutine SpawnObjectBelow (category: 3D objects)
Attempt to spawn an object on a tile that is below the maximum altitude specified in A
[X]
Variable minEnemyAltitude in workspace Main variable workspace
The altitude of the lowest enemy on the landscape
[X]
Variable numberOfEnemies in workspace Main variable workspace
The number of enemies in the current landscape, including the Sentinel (in the range 1 to 8)
[X]
Label tree1 is local to this routine
[X]
Label tree2 is local to this routine
[X]
Label tree3 is local to this routine
[X]
Variable treeCounter in workspace Zero page
A counter for the number of trees that are added to the landscape in the SpawnTrees routine