Prev: 35239 Up: Map Next: 35845
35283: Spawn Enemy
Used by the routine at UpdateEnemySpawning.
Starts an enemy in the slot found by FindFreeSlot. The kind of enemy comes from the table at EnemySpawnTable, indexed by a counter that steps through a sequence of values.
Input
IX Address of the free slot
SpawnEnemy 35283 LD B,6 Use a counter limit of six...
35285 LD A,(62961) ...or eight if the flags at 62961 and 62962 are both set.
35288 AND A
35289 JR Z,SpawnEnemy_Counter
35291 LD A,(62962)
35294 AND A
35295 JR Z,SpawnEnemy_Counter
35297 LD B,8
SpawnEnemy_Counter 35299 LD A,(39218) Fetch the counter from EnemySpawnCounter.
35302 INC A Increase it and store it.
35303 LD (39218),A
35306 CP B Compare it with the limit.
35307 JR C,SpawnEnemy_Kind Jump to SpawnEnemy_Kind if it is lower.
35309 XOR A Otherwise restart the counter at zero.
35310 LD (39218),A
SpawnEnemy_Kind 35313 CP 4 Jump to SpawnEnemy_Store if the counter is not four.
35315 JR NZ,SpawnEnemy_Store
35317 LD B,A Keep the counter in B.
35318 LD A,R Fetch a pseudo-random value from the refresh register.
35320 AND %00110010 Keep some of its bits.
35322 LD A,B Restore the counter.
35323 JR Z,SpawnEnemy_Store Jump to SpawnEnemy_Store if the random bits were zero.
35325 DEC A Otherwise use three instead of four.
SpawnEnemy_Store 35326 LD (35282),A Store the kind of enemy in 35282.
35329 CP 1 Jump to SpawnEnemy_CheckZone if it is one.
35331 JR Z,SpawnEnemy_CheckZone
35333 CP 5 Jump to SpawnEnemy_CheckBonus if it is not five.
35335 JR NZ,SpawnEnemy_CheckBonus
SpawnEnemy_CheckZone 35337 LD A,(52824) Fetch the map zone from 52824.
35340 CP 3 Jump to SpawnEnemy_Reset if it is lower than 3.
35342 JR C,SpawnEnemy_Reset
35344 CP 10 Jump to SpawnEnemy_CheckBonus if it is lower than 10.
35346 JR C,SpawnEnemy_CheckBonus
SpawnEnemy_Reset 35348 XOR A Use kind zero instead.
35349 LD (35282),A
SpawnEnemy_CheckBonus 35352 LD A,(52824) Fetch the map zone from 52824.
35355 CP 11 Jump to SpawnEnemy_Limit if it is 11 or more.
35357 JR NC,SpawnEnemy_Limit
35359 LD A,(34805) Fetch the bonus timer from 34805.
35362 AND A Test it.
35363 JP NZ,SpawnEnemy_Bonus Jump to SpawnEnemy_Bonus if it is running.
SpawnEnemy_Limit 35366 LD B,3 B=3.
This entry point is used by the routine at SpawnSpecialEnemy.
SpawnEnemy_ChooseSide 35368 LD C,0 Clear C.
35370 LD A,(51454) Fetch the first byte of 51454.
35373 AND A Test it.
35374 JR Z,SpawnEnemy_ChooseLower Jump to SpawnEnemy_ChooseLower if it is zero.
35376 INC A Increment it.
35377 JR Z,SpawnEnemy_ChooseLower Jump to SpawnEnemy_ChooseLower if it was 255.
35379 LD A,(52411) Fetch the scroll speed from HorizontalSpeed.
35382 BIT 7,A Jump to SpawnEnemy_ChooseRight if bit 7 is set.
35384 JR NZ,SpawnEnemy_ChooseRight
35386 LD A,B Keep only bit 0 of B.
35387 AND 1
35389 LD B,A
35390 JR SpawnEnemy_ChooseLower Jump to SpawnEnemy_ChooseLower.
SpawnEnemy_ChooseRight 35392 LD C,2 C=2.
SpawnEnemy_ChooseLower 35394 LD A,(51456) Fetch the first byte of 51456.
35397 AND A Test it.
35398 JR Z,SpawnEnemy_Pick Jump to SpawnEnemy_Pick if it is zero.
35400 INC A Increment it.
35401 JR Z,SpawnEnemy_Pick Jump to SpawnEnemy_Pick if it was 255.
35403 INC C Add one to C.
35404 LD A,(52410) Fetch the scroll speed from VerticalSpeed.
35407 BIT 7,A Jump to SpawnEnemy_Pick if bit 7 is set.
35409 JR NZ,SpawnEnemy_Pick
35411 DEC C Subtract one from C.
35412 LD A,B Keep only bit 1 of B.
35413 AND 2
35415 LD B,A
SpawnEnemy_Pick 35416 LD A,4 Set the number of attempts in 35844 to four.
35418 LD (35844),A
35421 LD A,(39883) Fetch the first byte of the random seed.
35424 AND B Mask it with B and combine it with C.
35425 OR C
SpawnEnemy_Try 35426 LD (23580),A Store the result in ScratchValue.
35429 LD HL,56465 Use the table at VisibleTiles.
35432 CALL AddAToHL Add a random value to the address.
35435 LD E,(HL) Fetch the entry.
35436 LD HL,34140 Use the table at SpawnPatternTable and add the entry.
35439 ADD HL,DE
35440 LD A,(HL) Fetch the pattern.
35441 LD (23581),A Store it in TileNumber.
35444 LD A,R Fetch a pseudo-random value from the refresh register.
35446 AND %00000111 Keep three bits.
35448 LD (23582),A Store them in ScratchPair.
35451 ADD A,A Multiply them by eight...
35452 ADD A,A
35453 ADD A,A
35454 ADD A,71 ...and add 71 to make an attribute value.
35456 LD (35467),A Store it in the data at 35467.
35459 XOR A Clear the bit counter in 23583.
35460 LD (23583),A
SpawnEnemy_Bit 35463 LD A,(23581) Fetch the pattern from TileNumber.
35466 RLC B Rotate the mask in B left.
35468 JR NZ,SpawnEnemy_Position Jump to SpawnEnemy_Position if the bit was set.
35470 LD A,(23581) Fetch the pattern again.
35473 RRCA Rotate it right.
35474 LD (23581),A Store it.
35477 LD A,(23583) Fetch the bit counter from 23583.
35480 INC A Increment it and store it.
35481 LD (23583),A
35484 CP 8 Jump back to SpawnEnemy_Bit if all eight bits have not been tried.
35486 JR NZ,SpawnEnemy_Bit
35488 JP SpawnEnemy_Retry Give up and jump to SpawnEnemy_Retry.
SpawnEnemy_Position 35491 LD A,(23582) Fetch the random three bits from ScratchPair.
35494 LD B,A Add the bit counter from 23583.
35495 LD A,(23583)
35498 ADD A,B
35499 AND %00000111 Keep the bottom three bits.
35501 ADD A,A Double it to index a pair of bytes.
35502 LD HL,34309 Use the table at SpawnPositionTable.
35505 CALL AddAToHL Add the offset to the address.
35508 LD A,(HL) Set the horizontal position of the enemy from the table.
35509 LD (IX+2),A
35512 INC HL Set the vertical position of the enemy from the table.
35513 LD A,(HL)
35514 LD (IX+5),A
35517 LD (IX+13),255 Make the enemy's attribute 255.
35521 LD A,(39884) Fetch the second byte of the random seed.
35524 AND %01001000 Keep two bits.
35526 JR NZ,SpawnEnemy_FetchKind Jump to SpawnEnemy_FetchKind if either is set.
35528 LD (IX+13),119 Otherwise make the attribute 119.
SpawnEnemy_FetchKind 35532 LD A,(35282) Fetch the kind of enemy from 35282.
SpawnEnemy_SetKind 35535 ADD A,A Multiply it by four.
35536 ADD A,A
35537 LD HL,39178 Use the table at EnemySpawnTable.
35540 CALL AddAToHL Add the offset to the address.
35543 CALL FreeObjectSlot Clear the slot.
35546 LD (IX+21),1 Set the lifetime of the enemy to one.
35550 LD A,(HL) Set the type of the enemy from the table.
35551 LD (IX+0),A
35554 INC HL
35555 LD A,(HL)
35556 LD (IX+16),A Set the flags of the enemy from the table.
35559 INC HL
35560 LD A,(HL)
35561 LD (IX+19),A Set the first state byte of the enemy from the table.
35564 INC HL
35565 LD A,(HL)
35566 LD (IX+20),A Set the second state byte of the enemy from the table.
35569 LD A,(IX+0) Fetch the type of the enemy.
35572 CP 29 Jump to SpawnEnemy_Type1D if it is 29.
35574 JP Z,SpawnEnemy_Type1D
35577 LD HL,(52823) Fetch the map position from MapPosition.
35580 LD A,(23580) Fetch the value from ScratchValue.
35583 AND A Test it.
35584 JR Z,SpawnEnemy_Place Jump to SpawnEnemy_Place if it is zero.
35586 BIT 0,A Jump to SpawnEnemy_Column if bit 0 is clear.
35588 JR Z,SpawnEnemy_Column
35590 INC H Move the enemy one zone along.
SpawnEnemy_Column 35591 BIT 1,A Jump to SpawnEnemy_Place if bit 1 is clear.
35593 JR Z,SpawnEnemy_Place
35595 LD A,(56823) Fetch the column from NextColumn.
35598 LD L,A Use it as the column of the enemy.
SpawnEnemy_Place 35599 LD (IX+1),L Set the map column of the enemy.
35602 LD (IX+4),H Set the map zone of the enemy.
35605 LD A,(IX+0) Fetch the type of the enemy.
35608 CP 19 Jump to SpawnEnemy_Attribute if it is not 19.
35610 JR NZ,SpawnEnemy_Attribute
35612 LD (IX+19),0 Clear the first state byte of the enemy.
35616 LD A,R Fetch a pseudo-random value from the refresh register.
35618 AND %00011100 Keep some of its bits.
35620 JR NZ,SpawnEnemy_Zone Jump to SpawnEnemy_Zone if any are set.
35622 INC (IX+19) Otherwise change the state of the enemy.
SpawnEnemy_Zone 35625 LD A,H Fetch the map zone of the enemy.
35626 CP 5 Jump to SpawnEnemy_Finish if it is 5 or higher.
35628 JR NC,SpawnEnemy_Finish
35630 LD (IX+16),35 Set the flags of the enemy to 35.
35634 LD (IX+4),4 Set the map zone of the enemy to 4.
35638 LD (IX+5),31 Set the vertical position of the enemy to 31.
35642 LD (IX+13),255 Make the attribute of the enemy 255.
35646 LD A,(56823) Fetch the column from NextColumn.
35649 CP L Compare it with the column of the enemy.
35650 JR Z,SpawnEnemy_SameColumn Jump to SpawnEnemy_SameColumn if it is the same.
35652 LD A,(52805) Fetch the horizontal position of the player from PositionAcross.
35655 SUB 48 Subtract 48 from it.
35657 JR C,SpawnEnemy_Remove Remove the enemy if that goes below zero.
35659 ADD A,8 Add 8 to it.
35661 LD (IX+2),A Set the horizontal position of the enemy.
35664 RET Return.
SpawnEnemy_SameColumn 35665 LD A,(52805) Fetch the horizontal position of the player from PositionAcross.
35668 LD (IX+2),A Set the horizontal position of the enemy.
35671 RET Return.
SpawnEnemy_Attribute 35672 LD A,R Fetch a pseudo-random value from the refresh register.
35674 AND %00000111 Keep three bits.
35676 LD B,A Copy them into B.
35677 LD A,H Fetch the map zone of the enemy.
35678 CP 12 Jump to SpawnEnemy_Colour if it is 12 or higher.
35680 JR NC,SpawnEnemy_Colour
35682 CP 4 Jump to SpawnEnemy_Colour if it is lower than 4.
35684 JR C,SpawnEnemy_Colour
35686 LD A,B Keep only two bits of the random value.
35687 AND 3
35689 LD B,A
SpawnEnemy_Colour 35690 LD A,B Fetch the random value.
35691 LD HL,39219 Use the table at EnemyAttributes.
35694 CALL AddAToHL Add the value to the address.
35697 LD A,(HL) Fetch the attribute from the table.
35698 LD (IX+13),A Set the attribute of the enemy.
SpawnEnemy_Finish 35701 CALL FindObjectScreenPosition Update the enemy.
35704 BIT 7,(IX+16) Jump to SpawnEnemy_Accelerate if bit 7 of the flags of the enemy is clear.
35708 JR Z,SpawnEnemy_Accelerate
35710 LD (IX+0),255 Otherwise remove the enemy.
SpawnEnemy_Retry 35714 LD A,(35844) Fetch the number of attempts left from 35844.
35717 DEC A Decrease it and store it.
35718 LD (35844),A
35721 RET Z Return if there are none left.
35722 LD A,(23580) Fetch the value from ScratchValue.
35725 INC A Move on to the next one...
35726 AND %00000011 ...keeping two bits.
35728 JP SpawnEnemy_Try Try again.
SpawnEnemy_Accelerate 35731 JP AccelerateTowardsPlayer_Horizontal Set the acceleration of the enemy.
SpawnEnemy_Remove 35734 LD (IX+0),255 Free the slot.
35738 RET Return.
SpawnEnemy_Bonus 35739 LD IY,51429 Use the player's object at ShipObject.
35743 CALL CopyPosition Copy the position of the player to the enemy.
35746 LD A,(39883) Fetch the first byte of the random seed.
35749 LD B,A Copy it into B.
35750 AND %00110000 Keep two bits.
35752 ADD A,24 Add 24.
35754 BIT 7,B Jump to SpawnEnemy_BonusSpeed if bit 7 of the random value is clear.
35756 JR Z,SpawnEnemy_BonusSpeed
35758 SUB 24 Subtract 24...
35760 NEG ...and negate the result.
SpawnEnemy_BonusSpeed 35762 LD (IX+7),A Set the horizontal speed of the enemy.
35765 LD A,(IY+30) Fetch the second vertical value of the player.
35768 SUB 16 Subtract 16...
35770 NEG ...and negate the result.
35772 LD (IX+9),A Set the vertical speed of the enemy.
35775 CALL MoveObject Draw the enemy.
35778 LD A,(52823) Fetch the map column from MapPosition.
35781 CP (IX+1) Compare it with the map column of the enemy.
35784 LD A,9 Select the kind of enemy.
35786 JP Z,SpawnEnemy_SetKind Jump to SpawnEnemy_SetKind if they are the same.
35789 DEC A Otherwise use the one before.
35790 JP SpawnEnemy_SetKind Jump to SpawnEnemy_SetKind.
SpawnEnemy_Type1D 35793 LD A,(39883) Fetch the first byte of the random seed.
35796 AND %00001000 Keep bit 3.
35798 SRL A Shift it right twice.
35800 SRL A
35802 ADD A,2 Add two.
35804 LD (IX+9),A Set the vertical speed of the enemy.
35807 LD A,(39884) Fetch the second byte of the random seed.
35810 AND %00001000 Keep bit 3.
35812 SRL A Shift it right twice.
35814 SRL A
35816 ADD A,2 Add two.
35818 LD (IX+7),A Set the horizontal speed of the enemy.
35821 NEG Negate it and copy it into B.
35823 LD B,A
35824 LD A,(52823) Fetch the map column from MapPosition.
35827 CP (IX+1) Compare it with the map column of the enemy.
35830 LD (IX+13),254 Make the attribute of the enemy 254.
35834 RET Z Return if they are the same.
35835 LD (IX+7),B Otherwise reverse the horizontal speed.
35838 LD (IX+13),238 Make the attribute of the enemy 238.
35842 RET Return.
35843 DEFB 0
35844 DEFB 0
Prev: 35239 Up: Map Next: 35845