Prev: 53478 Up: Map Next: 53672
53488: Spawn Primary Wave Enemies
Used by the routine at Stage_Next_Wave.
Reads the enemy count and type for the current wave from the definition pointed to by HL, then spawns that many primary wave entities at random valid screen positions by writing records into the entity list at Entity_List_Write_Pointer. Each entity is placed at a random column (1–30) and row (2–22), scaled to pixel coordinates, and drawn to the screen via 52353. If the count is zero the entity list is left empty and the routine returns immediately.
Input
HL Pointer to the wave enemy definition (count byte followed by type byte)
Spawn_Primary_Wave_Enemies 53488 LD B,(HL) Read the enemy count from the wave definition and store at Wave_Enemy_Count.
53489 LD A,B
53490 LD (50211),A
53493 INC HL Advance HL to the enemy type byte.
53494 LD A,(HL) Read the enemy type parameter from the wave definition.
53495 LD (50212),A Store the enemy type parameter at Wave_Enemy_Type.
53498 INC HL Advance the wave definition pointer.
53499 PUSH HL Stash the wave definition pointer on the stack.
53500 LD HL,(50441) Load the entity list write pointer from *Entity_List_Write_Pointer.
53503 LD A,B Skip spawning if the wave has no enemies.
53504 AND A
53505 JP Z,Spawn_Primary_Wave_Enemies_Done
For each enemy: write the entity handler, pick a random valid screen position, build the entity record, and draw the sprite.
Spawn_Primary_Wave_Enemies_Loop 53508 PUSH BC Stash the enemy count on the stack.
53509 LD DE,52430 Load the primary entity handler address Primary_Enemy_Entity_Handler into DE.
53512 LD (HL),E Write the primary entity handler address into the entity list.
53513 INC HL
53514 LD (HL),D
53515 INC HL Advance HL past the handler bytes.
Spawn_Primary_Wave_Enemies_Get_X 53516 CALL Generate_Random_Byte Generate a random column via Generate_Random_Byte.
53519 AND %00011111 Mask to columns 0–31.
53521 JR Z,Spawn_Primary_Wave_Enemies_Get_X Retry if the column is at the left or right wall.
53523 CP 31
53525 JR Z,Spawn_Primary_Wave_Enemies_Get_X
53527 LD B,A Copy the valid column to B.
Spawn_Primary_Wave_Enemies_Get_Y 53528 CALL Generate_Random_Byte Generate a random row via Generate_Random_Byte.
53531 AND %00011111 Mask to rows 0–31.
53533 CP 2 Retry if the row is too close to the top or bottom edge.
53535 JR C,Spawn_Primary_Wave_Enemies_Get_Y
53537 CP 23
53539 JR NC,Spawn_Primary_Wave_Enemies_Get_Y
53541 SLA A Scale the row and column to pixel coordinates (multiply by 8).
53543 SLA A
53545 SLA A
53547 SLA B
53549 SLA B
53551 SLA B
53553 LD (HL),A Write the Y pixel to the entity record.
53554 LD (50419),A Cache the Y pixel at Cached_Spawn_Pixels.
53557 INC HL Advance HL to the X slot in the entity record.
53558 LD (HL),B Write the X pixel to the entity record.
53559 LD A,B Copy the X pixel from B into A and cache it at 50420.
53560 LD (50420),A
53563 INC HL Advance HL to the colour slot.
53564 LD (HL),7 Write WHITE to the colour and type bytes.
53566 INC HL
53567 LD (HL),7
53569 DEC HL Step back three bytes to point at the start of the entity record.
53570 DEC HL
53571 DEC HL
53572 EXX Load the entity spawn handler address Entity_Spawn_Handler into alternate HL.
53573 LD HL,53703
53576 EXX
53577 CALL HitTestBounds8AtHL Test whether this position overlaps an existing entity via HitTestBounds8AtHL.
53580 DEC HL Retry with new coordinates if the position is occupied.
53581 DEC HL
53582 DEC HL
53583 JR NC,Spawn_Primary_Wave_Enemies_Get_X
53585 INC HL Advance past the entity record header fields.
53586 INC HL
53587 INC HL
53588 INC HL
53589 LD A,(50212) Copy the wave enemy type from Wave_Enemy_Type into the entity record.
53592 LD (HL),A
53593 INC HL
Build the on-screen pixel address and attribute address from the cached pixel coordinates at Cached_Spawn_Pixels.
53594 EX DE,HL Copy HL to DE (entity record pointer now in DE).
53595 LD HL,50419 Point HL at the cached pixel coordinates at Cached_Spawn_Pixels.
53598 LD A,(HL) Read the Y pixel (row) from the cache.
53599 INC HL Advance HL to the X pixel byte.
53600 LD C,(HL) Read the X pixel (column) from the cache.
53601 LD L,A Move the Y pixel into L for the row term.
53602 LD B,69 Load the screen address high-byte base 69 into B.
53604 SLA C Shift the X pixel left in C.
53606 RL B Roll the carry into the screen high byte in B.
53608 LD H,70 Load the second high-byte term 70 into H.
53610 SLA L Shift the Y pixel left in L.
53612 RL H Finish forming the screen address high byte in H.
53614 LD A,(HL) Read the low byte of the pixel data pointer from the table.
53615 INC HL Advance HL to the high byte.
53616 LD H,(HL) Read the high byte of the pixel data pointer.
53617 LD L,A Form the full pixel data pointer in HL (low in L).
53618 LD A,(BC) Read a byte from the built screen address in BC.
53619 OR L Combine the screen low byte with the pixel table low byte.
53620 LD L,A Store the combined screen low byte in L.
53621 EX DE,HL Exchange HL with DE (entity record pointer back in HL).
53622 LD (HL),E Write the screen address into the entity record.
53623 INC HL
53624 LD (HL),D
53625 INC HL Advance HL to the attribute slot.
53626 LD A,D Load the attribute address high byte from D.
53627 RRCA Rotate the attribute page bits toward the low end three times.
53628 RRCA
53629 RRCA
53630 AND %00000011 Mask to the attribute page bits.
53632 OR %01011000 Set the attribute area high byte.
53634 LD D,A Store the attribute address high byte in D.
53635 LD (HL),E Write the attribute address into the entity record.
53636 INC HL
53637 LD (HL),D
53638 DEC HL Step HL back four bytes to the entity origin for drawing.
53639 DEC HL
53640 DEC HL
53641 DEC HL
53642 CALL Draw_Spawned_Entity Call Draw_Spawned_Entity.
53645 DEC HL Step HL to the end-of-record marker position.
53646 LD (HL),255 Write the record terminator 255 after this entity.
53648 INC HL Advance HL for the next list write.
53649 POP BC Restore the enemy count from the stack.
53650 DEC B Decrement the remaining enemies to spawn for this wave.
53651 JP NZ,Spawn_Primary_Wave_Enemies_Loop Loop back to Spawn_Primary_Wave_Enemies_Loop if more enemies remain.
All enemies spawned: write the entity list terminator and return.
Spawn_Primary_Wave_Enemies_Done 53654 LD DE,53668 Load Entity_List_Reset into DE.
53657 LD (50441),HL Store the updated write pointer at Entity_List_Write_Pointer.
53660 LD (HL),E Store the entity list terminator routine address Entity_List_Reset at *HL (low byte then high byte).
53661 INC HL
53662 LD (HL),D
53663 INC HL Advance HL past the stored pointer bytes.
53664 LD (HL),255 Write the terminator byte (255) to the entity list.
53666 POP HL Restore the wave definition pointer from the stack.
53667 RET Return.
Inline stub: resets the entity list pointer and returns. Used as the terminal routine address written into the entity list by the spawning routines.
Entity_List_Reset 53668 LD HL,24576 Reset the entity list pointer to Entity_List.
53671 RET Return.
Prev: 53478 Up: Map Next: 53672