MiningScript::
	killsfx
	checkflag ENGINE_POKEMON_MODE
	sif true
		end
	opentext ;Check mining pick
	checkitem IRON_PICKAXE
	sif false, then
		scall .onlyCheckNormalPicks
	selse
		copybytetovar wMiningPickaxeMode
		scriptjumptable .MiningModes
	sendif
	sif false, then
		copybytetovar wIronPickaxeStepCount
		multiplyvar -1
		addvar 250
		copyvartobyte wScriptBuffer
		checkitem IRON_PICKAXE
		sif true, then
			copybytetovar wMiningPickaxeMode
		selse
			writebyte 2 ;pretend we're always in mode 3 if we don't have the Iron Pickaxe
		sendif
		playsound SFX_READ_TEXT_2
		loadarray .MiningFailureTextsArray
		readarrayhalfword 0
		writebyte 0
		jumptext -1
	selse
		copyvartobyte wScriptBuffer
	sendif

	;We have mining pick
	playwaitsfx SFX_BEAT_UP
	scall ExtractItem
	sif false, then
		writetext NoItemExtracted
	selse
		; save the obtained item in case our bag is full
		copyvartobyte wSavedMiningItem
		; special check for fossil
		sif =, FOSSIL, then
			; check for fossil case
			checkitem FOSSIL_CASE
			sif false, then
				writebyte 2
			selse
				writehalfword wFossilCaseCount
				copyhalfwordvartovar
				sif >, FOSSIL_CASE_SIZE - 1, then
					writebyte 1
				selse
					addvar 1
					copyvartohalfwordvar
					addhalfwordvartovar
					random 4
					copyvartohalfwordvar
					addhalfwordtohalfwordvar 1
					loadhalfwordvar $ff
					writebyte 0
				sendif
			sendif
			writetext ReceivedFossilText
			loadarray .FossilTextArray
			readarrayhalfword 0
			sif false, then
				killsfx
				playwaitsfx SFX_ITEM
			selse
				buttonsound
			sendif
			writetext -1
			sif true, then
				writebyte 0
			selse
				writebyte 1
			sendif
		selse
			verbosegiveitem SPECIAL_ITEM, 1
		sendif
		
		sif true, then
			waitbutton
			loadvar wSavedMiningItem, 0
		selse
			closetextend
		sendif
	sendif
	copybytetovar wScriptBuffer
	sif =, 1, then
		takeitem MINING_PICK, 1
	selse
		loadvar wIronPickaxeStepCount, 0
	sendif

	writebyte 1
	givecraftingEXP CRAFT_MINING
	closetextend

.MiningModes:
	dw .onlyCheckIronPick
	dw .smartCheckBothPicks
	dw .onlyCheckNormalPicks

.onlyCheckIronPick:
; iron pick is already before call
	writebyte 250
	comparevartobyte wIronPickaxeStepCount
	sif true
		writebyte 2
	end

.smartCheckBothPicks:
	scall .onlyCheckIronPick
	sif true
		end

; fallthrough
.onlyCheckNormalPicks:
	checkitem MINING_PICK
	sif true
		writebyte 1
	end

.MiningFailureTextsArray:
	dw IronPickaxeNotChargedEnoughText
.MiningFailureTextsArrayEntrySizeEnd:
	dw IronPickaxeNotChargedEnoughAndNoMiningPickText
	dw NoMiningPickText

.FossilTextArray:
	dw PutFossilInCaseText
.FossilTextArrayEntrySizeEnd:
	dw NoRoomForFossilText
	dw NoFossilCaseText

ExtractItem:
	copybytetovar wSavedMiningItem
	sif true
		end
	copybytetovar wMiningLevel
	divideop 0, 2
	loadarray MiningPickItemTable, 1
	variablestablerandom VSR_MINING, 100
	readarray -1
	sif !=, SPECIAL_ITEM
		end
	copybytetovar MapGroup
	addvar -1
	loadarray MiningSpecialItems, 1
	readarray 0
	end

IronPickaxeNotChargedEnoughText:
	db TX_COMPRESSED, $29, $27, $1d, $2c, $24, $9c, $23, $65, $4b, $69, $d3
	db $78, $68, $84, $0d, $c6, $53, $ab, $e4
	db $f2, $a8, $c8, $16, $b9, $4f, $9c, $c0

; fallthrough
WalkNumberOfStepsToRechargeText:
	db TX_COMPRESSED
	db $00, $df, $7a, $a5, $1c, $c0
	deciram wScriptBuffer, 1, 3
	db TX_COMPRESSED, $8f, $85

	db $66, $f2, $2c, $c4, $39, $80
	start_asm
	ld hl, .IronPickaxeNotChargedEnoughContinueText
	ld a, [wScriptBuffer]
	dec a
	ret z
	ld a, "s"
	ld [bc], a
	inc bc
	ret
.IronPickaxeNotChargedEnoughContinueText:
	db TX_COMPRESSED, $98, $05, $70, $77, $2c
	db $2a, $96, $6c, $a7, $57, $c9
	db $fa, $9a

IronPickaxeNotChargedEnoughAndNoMiningPickText:
	db TX_COMPRESSED, $29, $27, $1d, $2c, $24, $9c, $23, $65, $4b, $69, $d3
	db $78, $68, $84, $0d, $c6, $53, $ab, $e4
	db $f2, $00, $c8, $16, $b9, $4f, $e6, $93, $32, $d2, $6b
	db $37, $b5, $67, $08, $18, $2a, $32, $0c, $9c
	db $80, $e1, $1b, $2a, $48, $96, $18, $f8, $c8, $72, $a1, $da
	db $5e, $31, $da, $4d, $7e, $73, $00
	text_jump WalkNumberOfStepsToRechargeText

NoMiningPickText:
	db TX_COMPRESSED, $72, $85, $a8, $43, $3c, $cd, $82, $a3, $20, $c9
	db $cb, $dc, $23, $65, $4a, $58, $63, $e3, $21
	db $fa, $9a

NoItemExtracted::
	db TX_COMPRESSED, $73, $93, $55, $f4, $e5, $86, $1d, $3b, $6b, $d6
	db $6d, $ed, $20, $2b, $48, $c9, $cf
	db $a9, $e2

ReceivedFossilText:
	db TX_COMPRESSED, $a7, $8e, $2c, $d9, $31, $83, $1e
	db $5e, $d8, $a6, $14, $20, $7d
	db $e3, $4d

PutFossilInCaseText:
	db TX_COMPRESSED, $a7, $8e, $08, $6a, $e5, $a4
	db $de, $53, $0a, $10, $3e, $c1, $92, $5a
	db $4d, $52, $98, $50, $81, $f6, $36, $ea, $0f
	db $d4, $d0

NoRoomForFossilText:
	db TX_COMPRESSED, $12, $6a, $e5, $a4, $e2, $98, $50, $81
	db $f5, $e6, $dd, $41, $c1, $a2, $60, $d7, $7b, $4f
	db $05, $3c, $40

NoFossilCaseText:
	db TX_COMPRESSED, $12, $6a, $e5, $3c, $73, $21, $34, $12
	db $98, $f2, $74, $18, $e6, $c5, $30, $a1, $03
	db $ea, $a6, $dd, $41, $d3
	db $c1, $4f, $10

MiningPickItemTable: ;Every 2 levels shifts the table up, totaling in 150 items
	rept 29
		db NO_ITEM
	endr
	rept 7
		db GOLD_DUST
	endr
	rept 10
		db HEART_SCALE
	endr
	rept 13
		db COAL
		db ORE
	endr
	db HARD_STONE
	db REVIVE
	db SPECIAL_ITEM
	db SPECIAL_ITEM
	rept 5
		db EVERSTONE
	endr
	db HARD_STONE
	db SPECIAL_ITEM
	db KINGS_ROCK
	db SPECIAL_ITEM
	db REVIVE
	db SPECIAL_ITEM
	rept 2 ; 89
		db COAL
		db ORE
	endr
	rept 4
		db HEART_SCALE
	endr
	db FOSSIL
	db LEAF_STONE
	db FIRE_STONE
	db WATER_STONE
	db THUNDERSTONE
	db SHINY_STONE
	db SUN_STONE
	db DAWN_STONE
	db DUSK_STONE
	rept 8
		db COAL
		db ORE
	endr
	db HARD_STONE
	db CHARCOAL
	db KINGS_ROCK
	db KINGS_ROCK
	db REVIVE
	db REVIVE
	rept 9
		db SPECIAL_ITEM
	endr
	db LIGHT_CLAY
	db REVIVE
	db REVIVE
	db MAX_REVIVE
	db NUGGET
	db NUGGET
	db LEAF_STONE
	db FIRE_STONE
	db WATER_STONE
	db THUNDERSTONE
	db SHINY_STONE
	db SUN_STONE
	db DAWN_STONE
	db DUSK_STONE
	db BIG_NUGGET
	db FOSSIL
	db FOSSIL

MiningSpecialItems: ;Ordered by bank
	db NO_ITEM ;1
	db ICY_ROCK ;2
	db COAL ;3
	db COAL ;4
	db LEAF_STONE ;5
	db WATER_STONE ;6
	db NO_ITEM ;7
	db ORE ;8
	db HARD_STONE ;9
	db ICY_ROCK ;10
	db NO_ITEM ;11
	db ENERGYPOWDER ;12
	db ICY_ROCK ;13
	db ICY_ROCK ;14
	db ICY_ROCK ;15
	db ICY_ROCK ;16
	db DAMP_ROCK ;17
	db NO_ITEM ;18
	db COAL ;19
	db NO_ITEM ;20
	db GOLD_DUST ;21
	db SOFT_SAND ;22
	db SMOOTH_ROCK ;23
	db SOFT_SAND ;24
	db BRICK_PIECE ;25
	db SMOOTH_ROCK ;26
	db DAMP_ROCK ;27
	db ICY_ROCK ;28
	db HEAT_ROCK ;29
	db NO_ITEM ;30
	db WATER_STONE ;31
	db GOLD_DUST ;32
	db ENERGYPOWDER;33
	db SUN_STONE;34
	db FIRE_STONE;35
	db FIRE_STONE;36
	db LIGHT_CLAY;37
	db ICY_ROCK;38
	db COAL;39
	db COAL;40
	db ORE;41
	;Rijon
	db DAMP_ROCK ;42
	db HARD_STONE ;43
	db HARD_STONE ;44
	db EVERSTONE ;45
	db NO_ITEM ;46
	db ENERGYPOWDER ;47
	db ENERGYPOWDER ;48
	db DUSK_STONE ;49
	db NO_ITEM ;50
	db HEAT_ROCK ;51
	db GOLD_DUST ;52
	db NO_ITEM ;53
	db NO_ITEM ;54
	db NO_ITEM ;55
	db NO_ITEM ;56
	db NO_ITEM ;57
	db DAMP_ROCK ;58
	db HARD_STONE ;59
	db HARD_STONE ;60
	db COAL ;61
	db HEAT_ROCK ;62
	db NO_ITEM ;63
	db ORE ;64
	db ENERGYPOWDER ;65
	db NO_ITEM ;66
	db NO_ITEM ;67
	db NO_ITEM ;68
	db COAL ;69
	db HARD_STONE ;70
	db GOLD_DUST ;71
	db DAMP_ROCK ;72
	db GOLD_DUST ;73
	db HARD_STONE ;74
	db ICY_ROCK ;75
	db NO_ITEM ;76
	db LEAF_STONE ;77
	db NO_ITEM ;78
	db MOON_STONE ;79
	db NO_ITEM ;80
	db KINGS_ROCK ;81
	db NO_ITEM ;82
	db NO_ITEM ;83
	db THUNDERSTONE ;84
	db NO_ITEM ;85
	db NO_ITEM ;86
	db NO_ITEM ;87
	db FIRE_STONE ;88
	db DAWN_STONE ;89
	db NO_ITEM ;90
	db NO_ITEM ;91

