;Fix boutons commandes script
.unbackground $059786 $059788
.bank $05 .org $9786
.db $8d $d9 $00

;Fix suppression d'objets inventaire
;Insertion table de suppression
;00 -> Objet supprimable
;01 -> Objet non supprimable
;Fin d'insertion en $9257
.bank $12 .org $9190
.incbin "../asm/Items_trash.bin"

.unbackground $05cf22 $05cf30
.bank $05 .org $cf22
	.index 16
	.accu 16
		and #$00ff
		tax
		lda $929190,x
		plx
		and #$00ff
		rts

;Choix entre Sprites 8x16 ou 16x16
.unbackground $0586ee $0586f1
.bank $05 .org $86ee
		jsl sprites_tag

.unbackground $058fa2 $058fa5
.bank $05 .org $8fa2
		jml sprites_do

.unbackground $05877c $058781
.bank $05 .org $877c
		jml release
		nop
		nop

.unbackground $05874e $058750
.bank $05 .org $874e
.section "space_hack" forced
	adc		#$0008
.ends

.unbackground $05915f $059162
.bank $05 .org $915f
		jsl width

.unbackground $0597d5 $0597da
.bank $05 .org $97d5
		jsl items
		nop
		nop

;Patch NTSC
.unbackground $0797cc $0797cc
.bank $07 .org $97cc
.db $a9

;Objets shop
.unbackground $12e46e $12e495
.bank $12 .org $e46e
	.index 16
	.accu 16
		jsl objets_shop
		plx
		xba
		and #$00ff
		asl
		asl
		clc
		adc $09ab
		sta $09ab
		cmp #$00d0
		bcc $06
.db $a9 $18 $00
		sta $09ab
.db $02 $6c $92 $d9 $a2 $02 $6d $80 $c6
		nop
		nop

;Nom d'objets boutique  passage en Sprite16
.unbackground $388715 $388718
.bank $38 .org $8715
		jml shop_sprite16

;Déplacement crédits
.unbackground $07ec01 $07ec02
.bank $07 .org $ec01
	.index 16
	.accu 8
		lda #$92

.unbackground $07cf0e $07cf10
.bank $07 .org $cf0e
	.index 16
	.accu 16
		lda #$9400


.bank $38 .org $8840
.section "routines" forced

	sprites_tag:
	.index 16
	.accu 8
		sep #$20
		lda [$dd],y
		cmp #$ca
		bne +
		lda $ef
		cmp #$01
		beq ++
		lda #$01
		sta $ef
		bra +++
++		stz $ef
+++		iny
+		sty $ec					;stockage de l'emplacement du texte
		lda [$dd],y
		rtl

	sprites_do:
	.index 16
	.accu 16
		ldx $00ef
		cpx #$0001
		bne +++
		ldx #$0000
		txy
		lda #$0010
		jsl ram_copy
		ldx #$0040
		ldy #$0020
		lda #$0030
		jsl ram_copy
		ldy $ec
		iny
-		sep #$20
		lda [$dd],y				;chargement du 2nd caractère
		rep #$20
		and #$00ff
		cmp #$0080
		bcc ++
		cmp #$00ca				;code contrôle du sprite 16x16
		bne +
		stz $ef
		iny
		sty $ec
		bra -
+		lda #$0020				;tile "vide" : le but est de traiter le code contrôle 
		dey						;par le code du jeu (en 1er caractère)
++		sty $ec
		xba						;chargement de la font de la lettre
		lsr
		lsr
		clc
		adc.w #$8000
		sta $d3
		ldx #$0020
		ldy #$0000
		lda #$0010
		jsl ram_copy
		ldx #$0060
		ldy #$0020
		lda #$0030
		jsl ram_copy
		jml $858fd0
+++		ldx #$0000
		txy
		jml $858fa6

	ram_copy:
	.index 16
	.accu 16
		php
		sta $0e
		sep #$20
-		lda [$d3],y
		iny
		pha
		sta $00
		lda [$d3],y
		iny
		pha
		and $00
		sta $00
		eor $01,s
		sta $7fd801,x
		pla
		pla
		eor $00
		sta $7fd800,x
		inx
		inx
		cpy $0e
		bcc -
		plp
		rtl

	release:
	.index 16
	.accu 16
		ply
		ldy $ec
		bcc +
		jml $8586e2
+		jml $858782

	width:
	.index 16
	.accu 16
		pha
		lda $00ef
		cmp #$0001
		bne +
		pla
		clc
		adc #$0010
		rtl
+		pla
		clc
		adc $09c5
		rtl

	gfx_decomp:
	.index 16
	.accu 8
		cmp #$56
		beq +
		cmp #$57
		beq +
		cmp #$58
		beq +
		jmp $86853d
+		jmp $f887af


	items:
	.index 16
	.accu 16
		lda [$d7],y
		and #$00ff
		cmp #$15			;permet de garder les entrées suivantes
		bcs +				;du dictionaire fonctionnelles si besoin
		lda $09c7
		and #$00ff
		tay
		lda $9260,y			;Nous sommes déjà dans la bank $12, good news :-)
		and #$00ff
		rtl
+		lda #$00			;Charge une valeur fonctionnelle du dictionnaire pour éviter
		and #$00ff 			;les crashs si une phrase n'est pas repointée, et ce tant que 
		rtl					;les entrées supplémentaires du dictionnaires ne sont pas utilisées.

	objets_shop:
	.index 16
	.accu 16
		stz $09a5			;Raz compteur Vram (de souvenir)
		lda #$00			;Raz compteur sprite
		sta $7f0810
		sep #$20
		lda #$00			;Raz animation en cours
		sta $099f
		lda #01				;flag pour modif pointeur nom d'objet (passage en sprite 16)
		sta $ee
		rep #$20
		lda $460000,x
		tax
		phx
		phy
		ldy #$00
-		lda $460000,x
		and #$00ff
		inx
		iny
		cmp #$00ca
		bne +
		dey
		bra -
+		cmp #$00d4
		bne -
		dey
		tya
		asl
		asl
		sta $09ab
		lda #$0080
		sec
		sbc $09ab
		sta $09ab			;Centrage du nom d'objet
		lda $1002			;Position y du joueur 
		sec
		sbc $0822
		clc
		adc #$b0
		and #$00ff
		sta $09ad			;Hauteur du nom d'objet
		ply
		plx
		lda #$00c9			;Pas de décalage de tile
		rtl

	shop_sprite16:
	.index 16
	.accu 16
		pha
		lda $ee
		and #$00ff
		cmp #0001
		bne +
		stz $ee				; reset flag pour modif pointeur nom d'objet (passage en sprite 16)
		pla
		sec
		sbc #$7100
		phx
		asl
		tax
		lda $460000,x
		tay
		sep #$20
		lda #$46
		sta $df
		rep #$20
		plx
		jml $f88731
+		pla
		sec
		sbc #$7000
		jml $f88719

.ends