	const_def
	const ORE_ZINC
	const ORE_COPPER
	const ORE_LEAD
	const ORE_IRON
	const ORE_BRONZE
	const ORE_SILVER
	const ORE_RUTHENIUM
	const ORE_GOLD
	const ORE_COBALT
	const ORE_PRISM

Smelting:
	opentext
	checkitem ORE
	iffalse .noore
	checkitem COAL
	iftrue .coalandore

.smeltore
	playwaitsfx SFX_BURN
	takeitem ORE, 1
	callasm ChooseOre
	sif =, $ff, then
		writetext FailedSmeltText
		jump .giveSmeltingEXP
	sendif
	if_equal ORE_ZINC, .givezinc
	if_equal ORE_COPPER, .givecopper
	if_equal ORE_LEAD, .givelead
	if_equal ORE_IRON, .giveiron
	if_equal ORE_BRONZE, .givebronze
	if_equal ORE_SILVER, .givesilver
	if_equal ORE_RUTHENIUM, .giveruthenium
	if_equal ORE_GOLD, .givegold
	if_equal ORE_COBALT, .givecobalt
	if_equal ORE_PRISM, .giveprism

.smeltingFinish
	writetext GotOreText
.giveSmeltingEXP
	writebyte 1
	givecraftingEXP CRAFT_SMELTING
	closetext
	end

.coalandore
	writetext ChooseSmeltText

	loadmenudata WhichToSmelt
	verticalmenu
	closewindow
	if_equal $1, .smeltore
	if_equal $2, .smeltcoal

	closetext
	end

.noore
	checkitem COAL
	sif false
		jumptext NoOreText
.smeltcoal
	checkitem SOOT_SACK
	sif false
		jumptext NoSootSackText

	playwaitsfx SFX_BURN
	takeitem COAL, 1
	callasm ExtractAsh
	writetext CollectedAshText
	jump .giveSmeltingEXP

.givezinc
	givemoney 0, 10
	jump .smeltingFinish

.givecopper
	givemoney 0, 25
	jump .smeltingFinish

.givelead
	givemoney 0, 50
	jump .smeltingFinish

.giveiron
	givemoney 0, 200
	jump .smeltingFinish

.givebronze
	givemoney 0, 500
	jump .smeltingFinish

.givesilver
	givemoney 0, 1000
	jump .smeltingFinish

.giveruthenium
	givemoney 0, 3500
	jump .smeltingFinish

.givegold
	givemoney 0, 5000
	jump .smeltingFinish

.givecobalt
	givemoney 0, 7500
	jump .smeltingFinish

.giveprism
	givemoney 0, 10000
	jump .smeltingFinish

ExtractAsh:
	ld a, [SmeltingLevel]
	ld b, 10
	add a, b
	ld b, a
.random
	call Random
	cp b
	jr nc, .random

	ld b, a
	ld a, 10
	add a, b

	ld [TempNumber], a
	ld [hMoneyTemp + 1], a
	xor a

	ld [TempNumber + 1], a
	ld [hMoneyTemp], a
	ld bc, hMoneyTemp
	jpba GiveAsh

ChooseOre:
	ld hl, OrePercentChances
.randomloop
	call Random
	cp 100
	jr nc, .randomloop
	inc a ; 1 <= a <= 100
	push af
	push bc

	ld b, a ;Chosen Ore Index: 60
	ld a, [SmeltingLevel] ;Smelting Level: 82
	ld c, a ; c = 82
	ld a, 100 ;a = 100
	sbc a, c ; a = 100 - 82 = 18
	srl a ;a = 9
	ld c, a
	ld a, 100
	sbc a, c ;100 - 9 = 91 (So any index from 91 + should return carry)
	cp b
	jr c, .notHighEnoughLvl

	pop bc
	pop af
	ld b, a
.prob_bracket_loop
	ld a, [hli]
	cp b
	jr nc, .got_it
	inc hl
	jr .prob_bracket_loop
.got_it
	ld a, [hl]
	push af
	ld [hScriptVar], a
	ld hl, OreNames
	call GetNthString
	ld de, wStringBuffer1
	ld bc, 12
	rst CopyBytes

	ld hl, OrePrices
	pop af
	sla a
	ld c, a
	ld b, 0
	add hl, bc

	ld a, [hli]
	ld c, a
	ld a, [hl]
	ld b, a
	ld hl, TempNumber
	ld a, b
	ld [hli], a
	ld a, c
	ld [hl], a
	ret

.notHighEnoughLvl
	pop af
	pop bc
	ld a, $ff
	ld [hScriptVar], a
	ret

OrePrices:
	dw 10
	dw 25
	dw 50
	dw 200
	dw 500
	dw 1000
	dw 3500
	dw 5000
	dw 7500
	dw 10000

WhichToSmelt:
	db $40 ; flags
	db 04, 00 ; start coords
	db 11, 9 ; end coords
	dw .MenuDataSmelt
	db 1 ; default option

.MenuDataSmelt:
	db $80 ; flags
	db 3 ; items
	db "Ore@"
	db "Coal@"
	db "Cancel@"

ChooseSmeltText::
	db TX_COMPRESSED, $6e, $91, $b2, $99, $50, $5a, $ee, $60, $20
	db $b4, $f7, $46, $a4, $72, $c3, $10, $f9, $f6, $e9
	db $69, $a0

FailedSmeltText::
	db TX_COMPRESSED, $72, $85, $a9, $86, $8f, $a7, $99
	db $b6, $f2, $1f, $3e, $d8, $c9, $cc, $b4, $8d, $10, $2b, $3f
	db $53, $c4

GotOreText::
	db TX_COMPRESSED, $72, $85, $a8, $87, $cf, $b6, $79
	db $9a, $f0, $39, $11, $da, $cf

	db $88, $06, $10, $6e, $32, $11, $91, $12, $a0, $ae
	db $d2, $f0, $9a, $d7, $1c, $c0
	deciram TempNumber, 2, 0
	db TX_COMPRESSED
	db $e3, $4f, $10

NoOreText::
	db TX_COMPRESSED, $72, $85, $a8, $43, $3c, $c7, $6b, $38, $15
	db $bc, $da, $1d, $ec, $b0, $c4, $3e, $7d, $bf
	db $53, $40

CollectedAshText::
	db TX_COMPRESSED, $72, $85, $a9, $51, $ac, $e6, $ab, $e9, $cb
	db $0b, $cc, $87, $bd, $36, $6e, $39, $80
	deciram hMoneyTemp, 2, 0
	db TX_COMPRESSED, $9a, $84, $f1
	db $a7, $88

NoSootSackText:
	db TX_COMPRESSED, $72, $85, $a8, $ce, $92, $99, $12, $1e
	db $e5, $e0, $92, $c3, $35, $09, $95, $0e, $d2, $16
	db $ad, $56, $c5, $01, $0d, $c5, $1a, $ca, $97
	db $a9, $a0

OreNames:
	db "Zinc@" ;10
	db "Copper@" ;25
	db "Lead@" ;50
	db "Iron@" ;200
	db "Bronze@" ;500
	db "Silver@" ;1000
	db "Ruthenium@" ;3500
	db "Gold@" ;5000
	db "Cobalt@" ;7500
	db "Prism@" ;10000

OrePercentChances: ; 2a1cb Reveal a new ore every X
	;ALL LEVELS
	db 11,  ORE_ZINC ;11%
	db 22,  ORE_COPPER ;11%
	db 32,  ORE_LEAD ;10%
	db 42,  ORE_IRON ;10%
	db 52,  ORE_BRONZE ;10%
	;LEVEL 4
	db 55,  ORE_SILVER ;3%
	db 56,  ORE_LEAD ;1%
	;LEVEL 12
	db 58,  ORE_RUTHENIUM  ;2%
	db 59,  ORE_COPPER  ;1%
	db 60,  ORE_BRONZE  ;1%
	;LEVEL 20
	db 62,  ORE_GOLD  ;2%
	db 63,  ORE_LEAD  ;1%
	db 65,  ORE_ZINC  ;2%
	;LEVEL 30
	db 67,  ORE_COBALT  ;2%
	db 69,  ORE_COPPER  ;2%
	db 71,  ORE_LEAD  ;2%
	db 73,  ORE_IRON  ;2%
	db 74,  ORE_RUTHENIUM  ;1%
	db 75,  ORE_GOLD  ;1%
	;LEVEL 50
	db 76,  ORE_PRISM  ;1%
	db 77,  ORE_ZINC  ;1%
	db 78,  ORE_COPPER  ;1%
	db 79,  ORE_LEAD  ;1%
	db 80,  ORE_IRON  ;1%
	db 81,  ORE_BRONZE  ;1%
	db 85,  ORE_SILVER  ;4%
	db 87,  ORE_RUTHENIUM  ;2%
	db 88,  ORE_GOLD  ;1%
	db 89,  ORE_COBALT  ;1%
	db 91,  ORE_ZINC  ;2%
	db 93,  ORE_RUTHENIUM  ;2%
	db 94,  ORE_SILVER  ;1%
	db 96,  ORE_GOLD  ;2%
	db 98,  ORE_SILVER  ;2%
	db 99,  ORE_RUTHENIUM  ;1%
	db 100, ORE_COBALT, ;1%

;Zinc: 16%
;Copper: 15%
;Lead: 14%
;Iron: 13%
;Bronze: 12%
;Silver: 11%
;Ruthenium: 8%
;Gold: 6%
;Cobalt: 4%
;Prism: 1%

