;;; Enemy_SpawnNewEnemy ;;;
;; Parameters:
;; r0: New enemy ID
;; r1: New enemy spriteset slot
;; r2: New enemy spriteset GFX slot
;; r3: New enemy slot
;; sp0: New enemy Y position
;; sp4: New enemy X position
;; sp8: New enemy orientation
;; Return r0: New enemy slot
0800FAE0 (T)  push    r4-r7,r14
0800FAE2 (T)  mov     r7,r10
0800FAE4 (T)  mov     r6,r9
0800FAE6 (T)  mov     r5,r8
0800FAE8 (T)  push    r5-r7
0800FAEA (T)  add     sp,-8h
0800FAEC (T)  ldr     r4,[sp,28h]
0800FAEE (T)  ldr     r5,[sp,2Ch]
0800FAF0 (T)  ldr     r6,[sp,30h]
0800FAF2 (T)  lsl     r0,r0,18h                 ;\
0800FAF4 (T)  lsr     r0,r0,18h                 ;} r10 = new enemy ID
0800FAF6 (T)  mov     r10,r0                    ;/
0800FAF8 (T)  lsl     r1,r1,18h                 ;\
0800FAFA (T)  lsr     r1,r1,18h                 ;} sp0 = new enemy spriteset slot
0800FAFC (T)  str     r1,[sp]                   ;/
0800FAFE (T)  lsl     r2,r2,18h                 ;\
0800FB00 (T)  lsr     r2,r2,18h                 ;} r9 = new enemy spriteset GFX slot
0800FB02 (T)  mov     r9,r2                     ;/
0800FB04 (T)  lsl     r3,r3,18h                 ;\
0800FB06 (T)  lsr     r3,r3,18h                 ;} sp4 = new enemy slot
0800FB08 (T)  str     r3,[sp,4h]                ;/
0800FB0A (T)  lsl     r4,r4,10h                 ;\
0800FB0C (T)  lsr     r7,r4,10h                 ;} r7 = new enemy Y position
0800FB0E (T)  lsl     r5,r5,10h                 ;\
0800FB10 (T)  lsr     r5,r5,10h                 ;} r5 = new enemy X position
0800FB12 (T)  lsl     r6,r6,10h                 ;\
0800FB14 (T)  lsr     r6,r6,10h                 ;} r6 = new enemy orientation
0800FB16 (T)  mov     r1,0FFh                   ; r1 = FFh
0800FB18 (T)  mov     r4,0h                     ; r4 = i = 0
0800FB1A (T)  mov     r0,1h                     ;\
0800FB1C (T)  mov     r8,r0                     ;} r8 = 1
0800FB1E (T)  mov     r0,7h                     ;
0800FB20 (T)  mov     r2,0h                     ; r2 = 0
0800FB22 (T)  orr     r6,r0                     ; New enemy orientation |= 7

0800FB24 (T)  lsl     r0,r4,3h                  ;\
0800FB26 (T)  sub     r0,r0,r4                  ;|
0800FB28 (T)  lsl     r0,r0,3h                  ;|
0800FB2A (T)  ldr     r3,=EnemySlot0            ;} r12 = enemy slot i
0800FB2C (T)  add     r0,r0,r3                  ;|
0800FB2E (T)  mov     r12,r0                    ;/
0800FB30 (T)  ldrh    r0,[r0]                   ;\
0800FB32 (T)  mov     r3,1h                     ;|
0800FB34 (T)  and     r3,r0                     ;} If enemy i orientation has 01h: next! (find first empty enemy slot)
0800FB36 (T)  cmp     r3,0h                     ;|
0800FB38 (T)  bne     800FBACh                  ;/
0800FB3A (T)  mov     r0,r12                    ;\
0800FB3C (T)  strh    r6,[r0]                   ;} Enemy i orientation = new enemy orientation
0800FB3E (T)  mov     r1,r12                    ;\
0800FB40 (T)  add     r1,34h                    ;|
0800FB42 (T)  mov     r0,80h                    ;} Enemy i collision properties = 80h (special AI)
0800FB44 (T)  strb    r0,[r1]                   ;/
0800FB46 (T)  mov     r0,r9                     ;\
0800FB48 (T)  mov     r1,r12                    ;} Enemy i spriteset GFX slot = new spriteset GFX slot
0800FB4A (T)  strb    r0,[r1,1Fh]               ;/
0800FB4C (T)  mov     r0,r10                    ;\
0800FB4E (T)  strb    r0,[r1,1Dh]               ;} Enemy i ID = new enemy ID
0800FB50 (T)  strh    r7,[r1,2h]                ; Enemy i Y position = new enemy Y position
0800FB52 (T)  strh    r5,[r1,4h]                ; Enemy i X position = new enemy X position
0800FB54 (T)  mov     r1,r13                    ;\
0800FB56 (T)  ldrb    r0,[r1]                   ;|
0800FB58 (T)  mov     r1,r12                    ;} Enemy i spriteset slot = new spriteset slot
0800FB5A (T)  strb    r0,[r1,1Eh]               ;/
0800FB5C (T)  add     r1,21h                    ;\
0800FB5E (T)  mov     r0,2h                     ;} $Enemy[i]21 = 2
0800FB60 (T)  strb    r0,[r1]                   ;/
0800FB62 (T)  add     r1,1h                     ;\
0800FB64 (T)  mov     r0,4h                     ;} $Enemy[i]22 = 4
0800FB66 (T)  strb    r0,[r1]                   ;/
0800FB68 (T)  mov     r0,r12                    ;\
0800FB6A (T)  add     r0,24h                    ;} Enemy i pose = 0
0800FB6C (T)  strb    r2,[r0]                   ;/
0800FB6E (T)  mov     r1,r12                    ;\
0800FB70 (T)  strh    r3,[r1,14h]               ;} Enemy i health = 1
0800FB72 (T)  add     r0,8h                     ;\
0800FB74 (T)  strb    r2,[r0]                   ;} Enemy i invincibility timer = 0
0800FB76 (T)  add     r0,1h                     ;\
0800FB78 (T)  strb    r2,[r0]                   ;} $Enemy[i]2D = 0
0800FB7A (T)  sub     r0,0Dh                    ;\
0800FB7C (T)  strb    r2,[r0]                   ;} Enemy i palette = 0
0800FB7E (T)  add     r0,15h                    ;\
0800FB80 (T)  strb    r2,[r0]                   ;} $Enemy[i]35 = 0
0800FB82 (T)  sub     r0,0Fh                    ;\
0800FB84 (T)  mov     r3,r8                     ;} $Enemy[i]26 = 1
0800FB86 (T)  strb    r3,[r0]                   ;/
0800FB88 (T)  sub     r0,3h                     ;\
0800FB8A (T)  mov     r1,r13                    ;|
0800FB8C (T)  ldrb    r1,[r1,4h]                ;} Enemy i slot = new enemy slot
0800FB8E (T)  strb    r1,[r0]                   ;/
0800FB90 (T)  mov     r1,r12                    ;\
0800FB92 (T)  add     r1,2Ah                    ;|
0800FB94 (T)  mov     r0,0FFh                   ;} $Enemy[i]2A = FFh
0800FB96 (T)  strb    r0,[r1]                   ;/
0800FB98 (T)  mov     r0,r12                    ;\
0800FB9A (T)  add     r0,32h                    ;} Enemy i freeze timer = 0
0800FB9C (T)  strb    r2,[r0]                   ;/
0800FB9E (T)  add     r0,4h                     ;\
0800FBA0 (T)  strb    r3,[r0]                   ;} $Enemy[i]36 = 1
0800FBA2 (T)  mov     r1,r4                     ;\
0800FBA4 (T)  b       800FBB6h                  ;} Return i
0800FBA6      dw      00000h
0800FBA8      dd      003000140h

0800FBAC (T)  add     r0,r4,1                   ; ++i
0800FBAE (T)  lsl     r0,r0,18h                 ;\
0800FBB0 (T)  lsr     r4,r0,18h                 ;|
0800FBB2 (T)  cmp     r4,17h                    ;} If i <= 17h: loop
0800FBB4 (T)  bls     800FB24h                  ;/
0800FBB6 (T)  mov     r0,r1
0800FBB8 (T)  add     sp,8h
0800FBBA (T)  pop     r3-r5
0800FBBC (T)  mov     r8,r3
0800FBBE (T)  mov     r9,r4
0800FBC0 (T)  mov     r10,r5
0800FBC2 (T)  pop     r4-r7
0800FBC4 (T)  pop     r1
0800FBC6 (T)  bx      r1