Skip to navigation

Landscape: SpawnTrees

Name: SpawnTrees [Show more] Type: Subroutine Category: Landscape Summary: Add trees to the landscape, ideally placing them below all the enemies in the landscape
Context: See this subroutine in context in the source code References: This subroutine is called as follows: * SpawnPlayer calls SpawnTrees
.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 number of trees determines the total amount of \ energy in the landscape, this makes the levels get \ even more difficult when there are higher enemy counts \ \ 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 \ Spawn a tree (an object of type 2), returning the JSR SpawnObject \ object 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 PlaceObjectBelow \ Attempt to place the player 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 PlaceObjectBelow sets the C flag then \ the object has not been successfully placed, 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 landscape