Skip to navigation

Gameplay: ExpendEnemyEnergy

Name: ExpendEnemyEnergy [Show more] Type: Subroutine Category: Gameplay Summary: Drain one unit of energy from an enemy and expend it onto the landscape by spawning a tree, if possible
Context: See this subroutine in context in the source code References: This subroutine is called as follows: * ApplyEnemyTactics calls ExpendEnemyEnergy * ApplyTactics (Part 3 of 8) calls ExpendEnemyEnergy

If we can't spawn a tree because we are about to pan the screen and the tree would spawn in a position that would be visible on-screen, then the routine does not drain energy or spawn a tree, and instead it gives up and aborts applying tactics for this gameplay loop.
Arguments: enemyObject The object number of the enemy to drain
Returns: C flag Status flag: * Clear if the energy is drained and a tree is spawned successfully * Set if: * The enemy had no energy to drain * We could not spawn a tree to take on the drained energy X The object number of the spawned tree, if one was spawned
.ExpendEnemyEnergy LDX enemyObject \ Set X to the object number of the enemy we are \ draining (so this is now object #X) SEC \ If the enemy has zero energy, jump to dren1 to return LDA enemyEnergy,X \ from the subroutine with the C flag set BEQ dren1 \ If we get here then the enemy has got some energy \ left, so we spawn a tree and decrease the enemy's \ energy level by one (as a tree is worth one energy \ unit) 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 altitude of the lowest enemy on the landscape JSR PlaceObjectBelow \ Attempt to place the tree on a tile that is below the \ maximum altitude specified in A (though we may end up \ placing the tree higher than this) BCS dren1 \ If the call to PlaceObjectBelow sets the C flag then \ the tree has not been successfully placed, so jump to \ dren1 to return from the subroutine with the C flag \ set TXA \ Set A to the object number of the tree we added to the \ landscape so we can pass it to CheckObjVisibility JSR CheckObjVisibility \ Check whether the tree in object A is visible and \ could therefore be seen on-screen by the player BCC dren2 \ If the C flag is clear then we are about to repeat the \ same screen pan (as the player is still holding down \ the same pan key) and the tree we just added would be \ visible in the landscape view if we drew it again \ \ This means that when we pan the screen, the new part \ of the screen that pans into view might show part of \ the tree while the rest of the screen won't, and that \ won't look good \ \ So jump to dren2 to delete the tree and stop applying \ tactics to the enemy, thus aborting the whole process \ If we get here then we are either not repeating the \ same screen pan, or we are but the object is not \ visible on-screen, so in either case the tree won't \ only partially appear on-screen so we can go ahead \ with the energy drain LDX enemyObject \ Decrement enemyEnergy for the enemy object we are DEC enemyEnergy,X \ draining LDX currentObject \ Set X to currentObject to return from the subroutine, \ which the call to CheckObjVisibility set to the object \ number of the newly spawned tree CLC \ Clear the C flag to indicate success .dren1 RTS \ Return from the subroutine .dren2 JSR DeleteObject \ Delete the tree in object #X and remove it from the \ landscape JMP FinishEnemyTactics \ Jump to FinishEnemyTactics to stop applying tactics to \ the current enemy and return to the ProcessGameplay \ routine to continue with the gameplay loop