	
	.def	playerSourceWidth		288

	.def	plFullEnergy			23
	.def	plFullLives			5
	
	.def	plShotCounterMax		10
	.def	plPlayerSpeedDefault		2
	.def	plPlayerHitEnergyDecrease	5
	.def	plExplosionAnimMax		9	;0..9
	
	.def	plAnimExplosionMaxDelay		15
	
	.def	plBulletLifeTimeDefault		50

	.def	plStateOk		0		;AAB (AA = 0 enemy not dead or explode)
	.def	plStateExplode		2
	.def	plStateDead		4
	
;player struct

	.def	plPositionX		0
	.def	plPositionY		1
	.def	plDirection		2
	.def	plIsMoving		3
	.def	plMoveCounter		4
	.def	plAnimDelayCounter	5
	.def	plShotDelayCounter	6
	.def	plEnergy		7
	.def	plLives			8
	.def	plState			9
	
;******************************
;*
;* isPlayerDead
;* 
;* r0 = player sturct
;*
;* return 
;*
;* r0 = 0 not dead
;*

isPlayerDead

	push	r1
	rqldi	r0,plState
	movei	r1,0	
	nop		
	ld	r0

	jmpi	r7,0
	rqpop			;delay slot
	cmpeqi	r0,plStateDead	;delay slot
	addtqi	r0,r1,0		;delay slot
	pop	r1		;delay slot (dangerous)	
	
;******************************
;*
;* getLiveCount
;* 
;* r0 = player sturct
;*
;* return 
;*
;* r0 = life count
;*

getLiveCount

	rqldi	r0,plLives
	jmpi	r7,0
	nop		;delay slot
	nop		;delay slot
	ld	r0	;delay slot
	nop		;delay slot
	
;******************************
;*
;* resetEnergy
;* 
;* r0 = player sturct
;*

resetEnergy

	push	r2
	movei	r2,plFullEnergy
	ssto	plEnergy
	stwo	r0,r2		;reload energy
	rqpop
	jmpi	r7,0
	nop		;delay slot
	pop	r2	;delay slot
	nop		;delay slot
	nop		;delay slot	
	
;******************************
;*
;* resetLives
;* 
;* r0 = player sturct
;*

resetLives

	push	r2
	movei	r2,plFullLives
	ssto	plLives
	stwo	r0,r2		;reset lives
	rqpop
	jmpi	r7,0
	nop		;delay slot
	pop	r2	;delay slot
	nop		;delay slot
	nop		;delay slot	
		
	
;******************************
;*
;* increaseLife
;* 
;* r0 = player sturct
;*

increaseLife

	push	r1
	push	r2
	
	rqldi	r0,plLives
	movei	r2,plFullEnergy
	nop
	ld	r1
	
	ssto	plEnergy
	stwo	r0,r2		;reload energy
	
	subi	r1,1		;one live decrease
	
	ssto	plLives
	stwo	r0,r1
	
	movei	r2,plStateOk
	ssto	plState
	stwo	r0,r2		;set live state
	
	movei	r2,0
	ssto	plDirection
	stwo	r0,r2		;set direction north
	
	rqpop
	rqpop
	jmpi	r7,0
	pop	r2	;delay slot
	pop	r1	;delay slot
	nop		;delay slot
	nop		;delay slot	
	
;******************************
;*
;* playerHit
;* 
;*

playerHit

	;PLAY SOUND HERE ????

	;prolog

	push	r0
	push	r1
	push	r2
	push	r3
	push	r4
	
	;cheat mode
	
	dexti	>UcRSwichtWLedG
	movei	r0,UcRSwichtWLedG	;test switch 1
	nop
	rqldi	r0,0
	nop
	nop
	ld	r2
	nop
	extri	r2,2
	brts	playerHitDontCare
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
	;

	dexti	>playerData		;get player struct
	movei	r0,playerData
	nop
	rqldi	r0,plEnergy
	rqldi	r0,plState	;current state
	nop	;
	ld	r1	;energy
	ld	r2	;state
	
	movei	r3,0	
	
	cmpeqi	r2,plStateOk
	brtc	playerHitIsAlreadyNotOk	
	subi	r1,plPlayerHitEnergyDecrease	;delay slot
	movei	r4,plStateExplode		;delay slot
	cmples	r1,r3				;	 energy <=0 ?			delay slot
	xorts	r1,r1				;energy	= 		0 : energy	delay slot
	
	movets	r2,r4,r2			;state  =		explosion : old state (plStateOk)
	ssto	plState
	stwo	r0,r2		
	ssto	plEnergy	;store decreased energy
	stwo	r0,r1

playerHitDontCare	
playerHitIsAlreadyNotOk	
	
	rqpop	
	rqpop
	rqpop
	pop	r4
	pop	r3
	pop	r2
	
	rqpop
	rqpop
	jmpi	r7,0
	pop	r1	;delay slot
	pop	r0	;delay slot
	nop		;delay slot
	nop		;delay slot

;******************************
;*
;* testPlayerCollision
;* 
;* r0 = player sturct
;* r1 = map struct
;*	

testPlayerCollision

	;prolog

	push	r1
	push	r2
	push	r3
	push	r4
	push	r5
	push	r6
	push	r7
	
	;---- test every collsion point
	
	;get current position (player/map total)
	
	rqldi	r0,plPositionX
	rqldi	r0,plPositionY
	rqldi	r1,mapWidth
	ld	r2		;pos x
	ld	r3		;pos y	
	ld	r4		;map width
	
	rqldi	r1,mapPositionX
	rqldi	r1,mapPositionY
	nop
	ld	r5
	ld	r6
	
	sub	r2,r2,r5
	sub	r3,r3,r6	;px, py
		
	dexti	>collisionPoints
	movei	r7,collisionPoints
	nop
		
	; ;r2,r3 -> px,py	
			
collisionPointLoop	
	
	rqldi	r7,0
	rqldi	r7,1
	rqldi	r1,mapWidth
	ld	r4	;cx
	ld	r5	;cy
	ld	r6	;map width
	
	;sentinel so end
	
	dexti	$ff
	cmpeqi	r4,$ff
	brts	collisionPointLoopEnd
	add	r4,r4,r2	;delay slot	x = cx + px
	add	r5,r5,r3	;delay slot	y = cy + py
	lsri	r4,4		;delay slot	x in map (/16)	
	lsri	r5,4		;delay slot	y in map (/16)	
		
	push	r2		;store px,py
	push	r3
	
	mul	r5,r5,r6	;		y * map width
	nop
	add	r3,r5,r4	;r3 = xypos =			+x
	
	rqldi	r1,mapAdrCtrlLow
	rqldi	r1,mapAdrCtrlHigh
	nop
	ld	r5
	ld	r6
	
	add	r5,r5,r3
	addtqi	r6,r6,0
	nop
	
	edrqldi	r6,r5,0		;request collion data
	eld	r4
	
	ssto	2
	stwo	r7,r4		;store to hit data (x,y,hit -> 0,1,2)
	
	
	rqpop
	rqpop
	br	collisionPointLoop
	pop	r3			;delay slot
	pop	r2                      ;delay slot
	addi	r7,collisionPointsWidth	;delay slot	next point
	nop				;delay slot

collisionPointLoopEnd	
	
	;hit data per point are avaitable
	
	;make upper test
	
	dexti	>collisionPoints
	movei	r7,collisionPoints
	
	dexti	$ff
	movei	r1,$f0		;XXXX XXXX XXXS OURL 
	
	;oben
	
	rqldi	r7,14		;oben	
	rqldi	r7,17		;oben
	rqldi	r7,20		;oben	
	ld	r3
	ld	r4
	ld	r5
	
	or	r2,r3,r4	
	nop
	or	r2,r4,r5	;r2 != 0 if hit oben
	
	movei	r6,8		;OXXX
	cmpeqi	r2,0
	orts	r1,r6		;
	
	;unten
	
	rqldi	r7,23		;unten	
	rqldi	r7,26		;unten
	rqldi	r7,29		;unten	
	ld	r3
	ld	r4
	ld	r5
	
	or	r2,r3,r4	
	nop
	or	r2,r4,r5	;r2 != 0 if hit unten
	
	movei	r6,4		;XUXX
	cmpeqi	r2,0
	orts	r1,r6		;
	
	;links
	
	rqldi	r7,2		;links	
	rqldi	r7,5		;links	
	nop
	ld	r4
	ld	r5
		
	nop
	or	r2,r4,r5	;r2 != 0 if hit links
	
	movei	r6,1		;XXXL
	cmpeqi	r2,0
	orts	r1,r6
	
	;rechts
	
	rqldi	r7,8		;rechts	
	rqldi	r7,11		;rechts	
	nop
	ld	r4
	ld	r5
		
	nop
	or	r2,r4,r5	;r2 != 0 if hit links
	
	movei	r6,2		;XXRX
	cmpeqi	r2,0
	orts	r1,r6
	
	;
	;mask the OURL blocker
	
	dexti	>OURL
	movei	r6,OURL
	nop
	rqldi	r6,0
	dexti	>directionOURL
	movei	r5,directionOURL
	ld	r2
	nop
	st	r5,r2		;for direction no collsion masking
	and	r2,r2,r1
	nop
	st	r6,r2
	
	;epilog
	
	rqpop
	rqpop
	rqpop
	poparqp	r7
	poparqp	r6
	poparqp	r5
	poparqp	r4
	pop	r3
	pop	r2
	pop	r1
	
	jmpi	r7,0
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
		
	.def	collisionPointsWidth	3	
		
collisionPoints

	;x,y,hit
	
	.data	0,16,0			;links		0,1,2
	.data	0,31,0			;links		3,4,5
	
	.data	31,16,0			;rechts		6,7,8
	.data	31,31,0			;rechts		9,10,11

	.data	2,14,0			;oben		12,13,14
	.data	16,14,0			;oben		15,16,17
	.data	29,14,0			;oben		18,19,20
	
	.data	2,33,0			;unten		21,22,23
	.data	16,33,0			;unten		24,25,26
	.data	29,33,0			;unten		27,28,29
	
	.data	$ffff,$ffff,$ffff	;end
	

	
;******************************
;*
;* calcPlayer
;* 
;* r0 = player sturct
;* r1 = map struct
;*	

calcPlayer

	;prolog

	push	r1
	push	r2
	push	r3
	push	r4
	push	r5
	push	r6
	push	r7	
	
	;test state
	
	rqldi	r0,plState
	nop
	nop
	ld	r2
	
	nop
	cmpeqi	r2,plStateOk
	brtc	calcPlayerNoMoving
	nop		;delay slot
	nop		;delay slot	
	nop		;delay slot
	nop		;delay slot
	
	;---- collision test
	
	gpci	r7,2
	br	testPlayerCollision
	nop		;delay slot
	nop		;delay slot	
	nop		;delay slot
	nop		;delay slot	
	
	;---- move position 
	
	dexti	>OURL
	movei	r6,OURL
	
	rqldi	r0,plPositionX
	rqldi	r0,plPositionY
	rqldi	r6,0	
	ld	r2		;pos x
	ld	r3		;pos y
	ld	r6		;SOURL
	
	;
	
	subqi	r4,r2,plPlayerSpeedDefault
	extri	r6,0
	movets	r2,r4,r2
	
	nop
	addqi	r4,r2,plPlayerSpeedDefault
	extri	r6,1
	movets	r2,r4,r2
	
	subqi	r4,r3,plPlayerSpeedDefault
	extri	r6,3
	movets	r3,r4,r3
	
	nop
	addqi	r4,r3,plPlayerSpeedDefault
	extri	r6,2
	movets	r3,r4,r3
	
	ssto	plPositionX
	stwo	r0,r2
	
	ssto	plPositionY
	stwo	r0,r3
	
	;---- calculation
	
	;---- calc map & player position
	
	;x
	
	rqldi	r0,plPositionX
	rqldi	r1,mapPositionX	
	nop
	ld	r2		;pos x
	ld	r4		;map pos X
	
	;x max
	
	dexti	1
	movei	r5,$08		;x max
	nop
	
	cmples	r2,r5
	brts	noClampXMax
	sub	r6,r4,r2	;delay slot
	dexti	$F1		;delay slot
	movei	r7,$78		;delay slot	clamp = screenWidth - mapWidth - 16 (360+32 - (256*16) - 16)
	add	r6,r6,r5	;delay slot	newMapPosX = mapPosX - playerPosX + xMax;
	
	nop
	cmples	r6,r7
	movets	r4,r7,r4
	movets	r2,r2,r5
	movets	r4,r4,r6
	
	ssto	mapPositionX
	stwo	r1,r4
	ssto	plPositionX
	stwo	r0,r2
	
noClampXMax	

	movei	r5,96		;x min
	nop
		
	cmplos	r2,r5		;playerPosX < xMin
	brtc	noClampXMin
	sub	r6,r4,r2	;delay slot
	movei	r7,16		;delay slot	clamp = 16
	add	r6,r6,r5	;delay slot	newMapPosX = mapPosX - playerPosX + xMin;
	nop			;delay slot
	
	nop
	cmplos	r6,r7		;newMapPosX < clamp
	movets	r4,r6,r4	;  mapPosX = newMapPosX else mapPosX;
	movets	r2,r5,r2	;  playerPosX = xMin else playerPosX
	movets	r4,r4,r7	;  mapPosX = mapPosX else clamp;
	
	ssto	mapPositionX
	stwo	r1,r4
	ssto	plPositionX
	stwo	r0,r2
	
noClampXMin

	;y
	
	rqldi	r0,plPositionY
	rqldi	r1,mapPositionY	
	nop
	ld	r2		;pos y
	ld	r4		;map pos y
	
	;y max
	
	movei	r5,144		;y max
	nop
	
	cmples	r2,r5		;playerPosY <= yMax	
	brts	noClampYMax
	sub	r6,r4,r2	;delay slot
	dexti	$f0		;delay slot
	movei	r7,$e1		;delay slot	clamp = screenHeight - mapHeight (225 - (256*16))
	add	r6,r6,r5	;delay slot	newMapPosY = mapPosY - playerPosY + yMax
	
	nop
	cmples	r6,r7
	movets	r4,r7,r4
	movets	r2,r2,r5
	movets	r4,r4,r6
	
	ssto	mapPositionY
	stwo	r1,r4
	ssto	plPositionY
	stwo	r0,r2
	
noClampYMax	

	movei	r5,64		;y min
	nop
		
	cmplos	r2,r5		;playerPosY < yMin
	brtc	noClampYMin
	sub	r6,r4,r2	;delay slot
	movei	r7,0		;delay slot	clamp = 0
	add	r6,r6,r5	;delay slot	newMapPosY = mapPosY - playerPosY + yMin;
	nop			;delay slot
	
	nop
	cmplos	r6,r7		;newMapPosY < clamp
	movets	r4,r6,r4	;  mapPosY = newMapPosY else mapPosY
	movets	r2,r5,r2	;  playerPosY = yMin else playerPosY
	movets	r4,r4,r7	;  mapPosY = mapPosY else clamp;
	
	ssto	mapPositionY
	stwo	r1,r4
	ssto	plPositionY
	stwo	r0,r2
	
noClampYMin
calcPlayerNoMoving

	;epilog
	
	rqpop
	rqpop
	rqpop
	poparqp	r7
	poparqp	r6
	poparqp	r5
	poparqp	r4
	pop	r3
	pop	r2
	pop	r1
	
	jmpi	r7,0
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot	
	


;******************************
;*
;* drawPlayer
;* 
;* r0 = player sturct
;* r1 = map struct
;*	
	
drawPlayer

	;prolog

	push	r1
	push	r2
	push	r3
	push	r4
	push	r5
	push	r6
	push	r7	
	
	;store destination position
	
	dexti	>playerBobData
	movei	r6,playerBobData	
	rqldi	r0,plPositionX
	rqldi	r0,plPositionY
	nop
	ld	r1		;pos x
	ld	r2		;pos y
	
	ssto	csDestX		;store x y
	stwo	r6,r1
	ssto	csDestY
	stwo	r6,r2
	
	;get/test state
	
	rqldi	r0,plState	;current state
	nop
	nop
	ld	r2
	nop
		
	cmpeqi	r2,plStateOk
	brts	drawPlayerNormal
	nop		;delay slot
	nop		;delay slot	
	nop		;delay slot
	nop		;delay slot
	
	cmpeqi	r2,plStateExplode
	brts	drawPlayerExplosion
	nop		;delay slot
	nop		;delay slot	
	nop		;delay slot
	nop		;delay slot	
	
	cmpeqi	r2,plStateDead
	brts	drawPlayerDead
	nop		;delay slot
	nop		;delay slot	
	nop		;delay slot
	nop		;delay slot
	
	.execm	stop		;wtf

	;---- explode
	
drawPlayerExplosion

	;anim delay

	rqldi	r0,plAnimDelayCounter
	rqldi	r0,plMoveCounter
	nop
	ld	r5		;anim delay counter
	ld	r4		;move counter
	
	nop
	cmploi	r5,plAnimExplosionMaxDelay		
	brts	noPlayerAnimExplode
	addi	r5,1			;delay slot
	ssto	plAnimDelayCounter	;delay slot
	stwo	r0,r5			;delay slot
	nop				;delay slot
	
	movei	r5,0	
	ssto	plAnimDelayCounter
	stwo	r0,r5			;clear anim counter
	
	;animation frames
	
	nop
	addi	r4,1		;next anim
	movei	r7,0
	cmploi	r4,plExplosionAnimMax	;		move counter < max expl ?
	movets	r4,r4,r7		;move counter =				  move counter : 0
	
	brts	playerExplosionAnimInProgress
	ssto	plMoveCounter		;delay slot
	stwo	r0,r4			;delay slot
	nop				;delay slot
	nop				;delay slot
	
	;explosion anim is over so go to state dead and dont draw
	
	br	drawPlayerEnd
	movei	r1,plStateDead		;delay slot
	ssto	plState			;delay slot
	stwo	r0,r1			;delay slot
	nop				;delay slot
	
playerExplosionAnimInProgress
noPlayerAnimExplode

	;get source data (from effects)
		
	dexti	>bulletSourceWidth
	movei	r2,bulletSourceWidth
	dexti	>>>globalBullets
	movei	r4,>>globalBullets
	dexti	>globalBullets
	movei	r3,globalBullets
	
	ssto	csSourceWidth
	stwo	r6,r2
	ssto	csSourceAdrHigh	
	stwo	r6,r4
	ssto	csSourceAdrLow
	stwo	r6,r3	
	
	;get correct source position
	
	rqldi	r0,plMoveCounter
	nop
	nop
	ld	r5		;move counter
	
	dexti	>playerExplosionGFXPositions
	movei	r4,playerExplosionGFXPositions
	add	r5,r5,r5	;move counter * 2
	nop
	add	r4,r4,r5
	nop
	rqldi	r4,0	;x
	rqldi	r4,1	;y
	nop
	ld	r1
	ld	r2
	
	;
	
	br	drawPlayerNow
	ssto	csSourceX	;delay slot	store source x y
	stwo	r6,r1		;delay slot	
	ssto	csSourceY	;delay slot	
	stwo	r6,r2		;delay slot	
	
	;---- normal draw
	
drawPlayerNormal
	
	
	;
	
	dexti	>directionOURL
	movei	r6,directionOURL
		
	;make direction OURL and run animation
	
	
	rqldi	r0,plDirection
	rqldi	r0,plAnimDelayCounter
	rqldi	r6,0
	ld	r3		;old direction
	ld	r5		;anim delay counter
	ld	r2		;sourl
	
	;test anim delay counter
	
	nop
	cmpeqi	r5,3		
	brtc	noPlayerAnim
	addi	r5,1			;delay slot
	ssto	plAnimDelayCounter	;delay slot
	stwo	r0,r5			;delay slot
	nop				;delay slot
	
	;
	
	movei	r5,0			
	ssto	plAnimDelayCounter	;clear anim counter	
	stwo	r0,r5	
	
	rqldi	r0,plMoveCounter
	nop
	movei	r7,$f
	ld	r5		;move counter
	
	and	r2,r2,r7	;only OURL
	movei	r4,2
	cmpeqi	r2,0		;no OURL
	movets	r5,r4,r5
	
	movei	r4,3
	addi	r5,1		;next anim
	nop
	and	r5,r5,r4	;0..3
	
	ssto	plMoveCounter
	stwo	r0,r5
	
	;
	
	movei	r4,0		;N
	cmpeqi	r2,%1000
	movets	r3,r4,r3
	
	movei	r4,1		;NO
	cmpeqi	r2,%1010
	movets	r3,r4,r3
	
	movei	r4,2		;O
	cmpeqi	r2,%0010
	movets	r3,r4,r3
	
	movei	r4,3		;SO
	cmpeqi	r2,%0110
	movets	r3,r4,r3
	
	movei	r4,4		;S
	cmpeqi	r2,%0100
	movets	r3,r4,r3
	
	movei	r4,5		;SW
	cmpeqi	r2,%0101
	movets	r3,r4,r3
	
	movei	r4,6		;W
	cmpeqi	r2,%0001
	movets	r3,r4,r3
	
	movei	r4,7		;NW
	cmpeqi	r2,%1001
	movets	r3,r4,r3
	
	ssto	plDirection
	stwo	r0,r3
	
	;
	
noPlayerAnim	
	
	
	dexti	>playerBobData
	movei	r6,playerBobData	
	
	;
	
	rqldi	r0,plDirection
	rqldi	r0,plMoveCounter
	nop
	ld	r3	;direction
	ld	r2	;move counter
	
	
	muli	r3,8		;direction * 8
	
	dexti	>playerMoveGFXPosition
	movei	r1,playerMoveGFXPosition
	muli	r2,2		;move counter * 2
	add	r1,r1,r3	;get direction
	nop
	add	r1,r1,r2	;+move counter
	nop
	rqldi	r1,0		;request source x
	rqldi	r1,1		;request source y
	nop
	ld	r1
	ld	r2
	
	;
	
	ssto	csSourceX		;store source x y
	stwo	r6,r1
	ssto	csSourceY
	stwo	r6,r2
	
	;set source gfx
	
	dexti	>enemySourceWidth
	movei	r2,enemySourceWidth
	dexti	>>>globalPlayer
	movei	r4,>>globalPlayer
	dexti	>globalPlayer
	movei	r3,globalPlayer
	
	ssto	csSourceWidth
	stwo	r6,r2
	ssto	csSourceAdrHigh	
	stwo	r6,r4
	ssto	csSourceAdrLow
	stwo	r6,r3	
	
drawPlayerNow
	
	;
	
	dexti	>drawFrame
	movei	r7,drawFrame
	nop
	rqldi	r7,0
	rqldi	r7,1
	nop
	ld	r1	;high
	ld	r2	;low
	nop
	
	ssto	csDestAdrLow
	stwo	r6,r2
	ssto	csDestAdrHigh
	stwo	r6,r1
	
	dexti	>drawBob
	movei	r4,drawBob
	gpci	r7,2		;
	jmpi	r4,0		;
	or	r0,r6,r6	;delay slot
	nop	;delay slot
	nop			;delay slot
	nop			;delay slot	

drawPlayerDead
drawPlayerEnd

	;epilog
	
	rqpop
	rqpop
	rqpop
	poparqp	r7
	poparqp	r6
	poparqp	r5
	poparqp	r4
	pop	r3
	pop	r2
	pop	r1
	
	jmpi	r7,0
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot

;******************************
;*
;* testPlayerShot
;* 
;* r0 = player struct
;* r1 = map struct
;*	
	
testPlayerShot	

	;prolog

	push	r0
	push	r1
	push	r2
	push	r3
	push	r4
	push	r5
	push	r6
	push	r7	
	
	;test state
	
	rqldi	r0,plState
	nop
	nop
	ld	r2
	
	nop
	cmpeqi	r2,plStateOk
	brtc	testPlayerShotNoShot
	nop		;delay slot
	nop		;delay slot	
	nop		;delay slot
	nop		;delay slot
	
	;
	
	dexti	>OURL
	movei	r6,OURL
	
	rqldi	r0,plShotDelayCounter
	rqldi	r6,0
	nop
	ld	r3		;shot counter
	ld	r4		;SOURL
	
	;shot counter test
	
	addi	r3,1			
	nop
	cmpeqi	r3,plShotCounterMax
	brtc	testPlayerShotNoShot	;no shot 
	nop				;delay slot
	xorts	r3,r3			;clear r2 if shot count max reached
	ssto	plShotDelayCounter	;delay slot
	stwo	r0,r3			;delay slot
	
	extri	r4,4
	brtc	testPlayerShotNoShot
	nop	;delay slot
	nop	;delay slot
	nop	;delay slot
	nop	;delay slot
	
	;calc shot
	
	rqldi	r0,plPositionX
	rqldi	r0,plPositionY
	rqldi	r0,plDirection
	ld	r3	;px
	ld	r4	;py
	ld	r2	;direction
	
	rqldi	r1,mapPositionX
	rqldi	r1,mapPositionY
	nop
	ld	r5		;map pos x
	ld	r6		;map pos y

	
	sub	r3,r3,r5	;x	
	sub	r4,r4,r6	;y
	
	;add/sub xy center (from direction)
	
	dexti	>playerShootCenterToDirection
	movei	r7,playerShootCenterToDirection
	nop
	add	r7,r7,r2
	nop
	add	r7,r7,r2
	nop
	rqldi	r7,0	;x center
	rqldi	r7,1	;y center
	nop
	ld	r5
	ld	r6
	
	add	r3,r3,r5
	add	r4,r4,r6
	
	
	;r1 = type,r2 = direction r3 = x, r4 = y
	
	gpci	r7,2
	br	addBullet
	movei	r5,plBulletLifeTimeDefault	;life time	delay slot
	movei	r6,buSpeedDefault		;speed		delay slot
	movei	r1,buPlayer			;type		delay slot
	nop					;delay slot
	
	;epilog
	
testPlayerShotNoShot	
	
	rqpop
	rqpop
	rqpop
	poparqp	r7
	poparqp	r6
	poparqp	r5
	poparqp	r4
	poparqp	r3
	pop	r2
	pop	r1
	pop	r0
	
	jmpi	r7,0
	nop		;delay slot
	nop		;delay slot	
	nop		;delay slot
	nop		;delay slot	
	
;*********************************************************************
;* data
;*********************************************************************		

directionOURL

	word	0

playerData

	.data	0,0				;x,y
	.data	0,0,0				;direction, moving, movecounter
	.data	0,0				;anim delay,shot delay
	.data	plFullEnergy,plFullLives	;energy (0..23), lives
	word	plStateOk			;state
		
		
playerMoveGFXPosition

	;	3	2	1	0				OURL
	.data	11,44,	44,44,	77,44,	110,44	;N	0	O	1000
	.data	141,44,	174,44,	207,44,	240,44	;NO	1	OR	1010
	.data	11,77,	44,77,	77,77,	110,77	;O	2	R	0010
	.data	141,77,	174,77,	207,77,	240,77	;SO	3	UR	0110
	.data	11,110,	44,110,	77,110,	110,110	;S	4	U	0100
	.data	145,110,178,110,211,110,244,110 ;SW	5	UL	0101
	.data	11,143,	44,143,	77,143,	110,143	;W	6	L	0001
	.data	145,143,178,143,211,143,244,143	;NW	7	OL	1001
	
	
playerExplosionGFXPositions

	.data	345,36		;0
	.data	378,36          ;1
	.data	411,36          ;2
	.data	444,36          ;3
	.data	477,36          ;4
	.data	510,36          ;5
	.data	543,36          ;6
	.data	576,36          ;7
	.data	609,36	        ;8	
	
playerShootCenterToDirection	
	
	.data	14,-14		;N	0		y-		ok
	.data	24,0		;NO	1	x+      y-		ok
	.data	22,13		;O	2	x+      		ok
	.data	18,18		;SO	3	x+      y+		ok
	.data	2,28		;S	4	        y+		ok
	.data	-7,12		;SW	5	x-      y+		ok
	.data	-10,3		;W	6	x-      		ok
	.data	-4,-8		;NW	7	x-      y-		ok
	