;****************************************
;*** compute current sequence from IY slot ***
;****************************************
.THEM_ComputeSequence
	push DE
	LD E,(IY+6)
 	LD D,(IY+7)
 	LD A,(IY+8)
 	ADD A
 	ADD 3
 	LD H,0
	LD L,A
	ADD HL,DE
	pop DE
	ret

;show slot infos
;A = slot
;HL = addr to draw
IF debug
.THEM_Debug
;	push HL
;	push AF
	
;	call GETSEQ
	
;	ld BC,THEM_DebugStr+2
;	pop AF
;	push BC
;	push HL
;	call FONT_GetHexFromByte
;	pop HL
;	pop BC
	
;	ld E,9
	
;.THEM_DebugLoop
;	inc BC
;	inc BC
;	inc BC
;	ld A,(HL)
;	inc L
	
;	push HL
;	push BC
;	push DE
;	call FONT_GetHexFromByte
;	pop DE
;	pop BC
;	pop HL
;	dec E
;	jr nz,THEM_DebugLoop
	
;	pop DE
;	ld HL,THEM_DebugStr
;	ld BC,color_table_options_normal
;	call FONT_DrawStringEX
	
;	ret
	
	
	


.THEM_DebugStr
;db "E XX 00 01 02 03 04 05 06 07 08"
;str " "
ENDIF


;*************************
;*** KILL ALL ENEMIES ***
;*************************
;.THEM_KillAll
;	ld B,28
;	ld HL,SEQBUFF
;	ld C,8
	
;.THEM_KillAll_loop
;	ld A,(HL)
;	and 63
;	jr z,THEM_KillAll_dead
;	ld (HL),0	
;	ld A,C
;	add L
;	ld L,A
;	set 7,(HL)
;	inc L
;	djnz THEM_KillAll_loop	
;	ret
;.THEM_KillAll_dead
;	ld (HL),0
;	ld A,C
;	add L
;	ld L,A
;	ld (HL),0
;	inc L
;	djnz THEM_KillAll_loop
;	ret


;damage an enemy
; A =enemy ID
; B= damage to do
;returns resulting damage in A
; B ,  HL and AF altered
.THEM_MakeDamage
	push DE
	;get enemy first
	AND 127
 	DEC A
 	LD L,A
	ADD A
 	ADD A
 	ADD A
 	ADD L
 	LD H,SEQBUFF_high
	LD L,A
	;get life points
	ld A,(HL)
	ld E,A
	;removed next part of code as life = 63 is in THEM code
	and 63
	;jr z,THEM_MakeDamageNoDamage
	;cp 63	;if life = 63 enemy is immortal
	;jr z,THEM_MakeDamageImmortal
	;if damage to do is higher than life ?
	sub B
	jr c,THEM_MakeDamage_OpCorrect
	ld D,A	;save result
	ld A,E
	and %110000000 ;get high bits of IY+0
	or D
	ld (HL),A ;enemy absorb all damage
	ld B,0
.THEM_MakeDamage_Finish
	ld A,8
	add L
	ld L,A
	set 7,(HL)
	ld A,B
	pop DE
	ret
;correct if enemy life is under 0
.THEM_MakeDamage_OpCorrect
	neg
	ld B,A	;remaining damage
	;xor A
	ld A,E
	and %11000000
	ld (HL),A
	jr THEM_MakeDamage_Finish
.THEM_MakeDamageNoDamage
	xor A
	pop DE
	ret

;
;	BYTE 0 = CURRENT HIT POINTS
;	BYTE 1 = LSB CURRENT PATTERN
;	BYTE 2 = MSB CURRENT PATTERN
;	BYTE 3 = CURRENT X POS
;	BYTE 4 = CURRENT Y POS
;	BYTE 5 = SPECIAL EVENTS
;	BYTE 6 = LSB SEQUENCE START
;	BYTE 7 = MSB SEQUENCE START
;	BYTE 8 = PATTERN POINTER
;
THEM_Count	db 0
.THEM	
	xor A
	ld (THEM_Count),A

	LD A,(SYNCOFF)
	AND A
 	RET NZ
	LD IY,SEQBUFF
 	LD B,1
.THEM0	
	BIT 7,(IY+8)
 	JR NZ,THEM1
 	LD A,(IY+0)
 	AND 63
	JR NZ,THEM1
.THEM00	
	LD DE,9
 	ADD IY,DE
 	INC B
 	LD A,B
	CP 29
 	JR NZ,THEM0
 	RET
 ;
.THEM1	
	ld A,(THEM_Count)
	inc A
	ld (THEM_Count),A
;check if it is mortal , if no , make master null for weapons
	ld A,(IY+0)
	and 63
	cp 63
	jr nz,THEM_MasterMortal
	xor A
	jr THEM1_MasterDone

.THEM_MasterMortal

	ld A,B	;set master as register B

.THEM1_MasterDone
	ld (MASTER),A
	
	ld A,B	;save for shot
 	OR 128
	LD (SPRITE),A
	
 	PUSH BC
 	LD E,(IY+1)
 	LD D,(IY+2)
 	LD C,(IY+3)
 	LD B,(IY+4)
 	LD A,(DE)
 	EX AF,AF'
 	INC DE
	LD A,(DE)
 	RRCA
	RRCA
	CALL ALIENF99 
 ;
;FIRST SPECIAL JUMP
;HL  - FREE
;A   - FREE
;BC  - SPRITE POSITION
 ;DE  - 2ND BYTE ALIEN SPRITE
 ;AF' - ALIEN SPRITE
;
.THEM5	
	BIT 7,(IY+0)
 	JR NZ,THEM5A
	;LD A,(SPRITE)
 	BIT 6,(IY+0)
	jr z,THEM5A
	;CALL NZ,ALIENPOW ;edit later
;new ! check if frame = current enemy to shoot
	ld A,(option_menu_game)
	cp 2
	jr z,THEM5Z		;MASTER mode always shoot when possible
	ld A,(SPRITE)
	and 31
	add A
	ld L,A
	ld A,(FRAME)
	and 63
	cp L
.THEM5Z	
	call z,ALIENPOW
.THEM5A	
	LD A,(IY+5)
 	AND 15
	LD HL,(SPEVENT1)
	CALL THEM13
 	CALL HITIT ;later !
	JP (HL)		;conditions - NZ = damaged - C = destroyed
 ;
.TV1	
	LD (IY+3),C
	LD (IY+4),B
.TV2	
	LD A,(DE)
.TVA	
	PUSH DE
	PUSH AF
 	EX AF,AF'
 	CALL TSPGADD
	POP AF
 	AND 3
	JR NZ,THEM6
 	CALL SPNS
 	JR THEM9
THEM6	
	CP 1
 	JR NZ,THEM7
 	CALL SPMS
 	JR THEM9
THEM7	
	CP 2
 	JR NZ,THEM8
 	CALL SPFS
 	JR THEM9
THEM8	
	CALL SPBS
THEM9	
	POP DE
 
 ;SECOND SPECIAL JUMP
 ;HL - FREE
 ;BC - FREE
 ;A  - FREE
 ;A' - FREE
 ;DE - 2ND BYTE ALIEN SPRITE
;
.TV5	
.THEM9A	
	LD A,(IY+5)
 	RRCA
	RRCA
	RRCA
	RRCA
	AND 15
	LD HL,(SPEVENT2)
	CALL THEM13
 	JP (HL)
 ;
TV3	
	LD A,(DE)
	CP 128
	JR NC,THEM10
	INC DE
	LD (IY+1),E
 	LD (IY+2),D
.TV4	
	JR THEM11
.THEM10	
	LD E,(IY+6)
 	LD D,(IY+7)
 	LD A,(IY+8)
	;and 127	;new here , remove high bit of pattern pointer
 	ADD A
 	ADD 3
 	LD H,0
	LD L,A
	ADD HL,DE
 	LD B,(HL)
 	INC HL
	LD A,(HL)
 	AND A
 	JR Z,THEM12
 	CP 255
	JR NZ,THEM11A
	;ld A,(IY+8)	;edited here , save high bit of the pattern pointer
	;and 128
 	LD (IY+8),0
 	JR THEM10
 ;
THEM11A	
	LD (IY+1),B
	LD (IY+2),A
 	INC (IY+8)
THEM11	
	POP BC	
 	JP THEM00
.THEM12	
	LD (IY+0),0
	LD (IY+8),0	;added
 	JR THEM11
 ;
.THEM13	
	ADD A
 	ADD L
 	LD L,A
	LD A,0
	ADC H
 	LD H,A
	LD A,(HL)
 	INC HL
	LD H,(HL)
 	LD L,A
	RET 
	
.HITIT	
	BIT 7,(IY+8)
 	RET Z
 	RES 7,(IY+8)
	LD A,(IY+0)
 	AND 63
	JR Z,HITIT1
  ;PUSH HL
  ;LD HL,4*256+FULL+1
 ;CALL BEEP
  ;POP HL
	LD A,1
	AND A
 	RET
.HITIT1	
	;ex AF,AF'
	;push AF
	PUSH DE
	PUSH HL
	PUSH BC
 	LD (CIRCHOLD1),BC
 	CALL ADDEXPLO
 	POP BC
	POP HL
	POP DE
	;pop AF
	;ex AF,AF'
	SCF
 	RET
	
;.CIRCTABLE1	
;	DEFB 0,62,0,63,1,64,2,65
;	DEFB 2,66,1,67,2,66,2,67
;	DEFB 2,66,2,67,2,66,2,67
;	DEFB 2,66,2,67,2,66,2,67
;	DEFB 2,66,2,67,99
;.CIRCTABLE2	
;	DEFB 0,62,254,63,254,64,253,65
;	DEFB 254,66,254,67,254,66,254,67
;	DEFB 254,66,254,67,254,66,254,67
;	DEFB 254,66,254,67,254,66,254,67
;	DEFB 254,66,99
	
;omgfg ! please move that in globals baby !	
	
.FXBYTE1	DEFB 0
.FXBYTE2	DEFB 0
.FXBYTE3	DEFB 0
.FXBYTE4	DEFB 0
.FXBYTE5	DEFB 0
.FXBYTE6	DEFB 0
.FXBYTE7	DEFB 0

