EVAR_PACHISI_POSITION EQU EVAR_X
EVAR_PACHISI_DICE_ROLLS EQU EVAR_Y

PachisiGameBotan::
	setevent EVENT_PACHISI_GAME_IN_BOTAN
	jump PachisiMain

PachisiGameTorenia::
	clearevent EVENT_PACHISI_GAME_IN_BOTAN
	; fallthrough

PachisiMain:
	opentext
	special PlaceMoneyTopRight
	writetext PachisiTextStartText
	yesorno
	sif false, then
		closetext
		end
	sendif
	checkmoney $0, 2000
	sif =, 2
		jumptext PachisiTextNotEnoughMoney
	takemoney $0, 2000
	waitsfx
	playsound SFX_TRANSACTION
	closetext
	applymovement PLAYER, .player_step_aside_movement
	applymovement 2, .guard_move_down_movement
	applymovement PLAYER, .player_enter_board_movement
	applymovement 2, .guard_move_up_movement
	playmusic MUSIC_MOBILE_ADAPTER
	seteventvartovalue EVAR_PACHISI_DICE_ROLLS, 25
	checkevent EVENT_PACHISI_GAME_IN_BOTAN
	sif true
		seteventvartovalue EVAR_PACHISI_DICE_ROLLS, 30
	seteventvar EVAR_PACHISI_POSITION, 0
.main_loop
	opentext
	callasm FacePlayerToNextTile
	applymovement PLAYER, wPachisiPath
	readeventvar EVAR_PACHISI_DICE_ROLLS
	sif false, then
		writetext PachisiOutOfRollsText
		playwaitsfx SFX_QUIT_SLOTS
		closetext
		jump .game_over
	sendif
	sif =, 1, then
		writetext PachisiAskRollFinalText
	selse
		writetext PachisiAskRollText
	sendif
	yesorno
	iffalse .ask_end_game
	deceventvar EVAR_PACHISI_DICE_ROLLS
	random 6
	addvar 1
	writetext PachisiRolledDiceText
.move_player_forwards
	callasm CreatePachisiPath
.move_player_by_calculated_path
	closetext
	applymovement PLAYER, wPachisiPath
	callasm GetPachisiTile
	if_greater_than PACHISI_WARP_BOTAN_SECTION_4, .main_loop
	anonjumptable
	dw .main_loop
	dw .grass_mon_tile
	dw .water_mon_tile
	dw .cave_mon_tile
	dw .get_money_tile
	dw .party_heal_tile
	dw .get_item_tile
	dw .lose_money_tile
	dw .warp_tile_torenia_forwards
	dw .warp_tile_torenia_backwards
	dw .death_tile
	dw .extra_rolls_tile
	dw .random_tile
	dw .finish_tile
	dw .move_forwards_tile
	dw .move_backwards_tile
	dw .warp_tile_botan_item_section
	dw .warp_tile_botan_second_section
	dw .warp_tile_botan_random_section
	dw .warp_tile_botan_third_section
	dw .warp_tile_botan_fourth_section
	dw .main_loop

.grass_mon_tile
	writehalfword PachisiGrassPokemon
	jump .do_battle_tile

.water_mon_tile
	writehalfword PachisiWaterPokemon
	jump .do_battle_tile

.cave_mon_tile	
	writehalfword PachisiCavePokemon
.do_battle_tile
	callasm PachisiGetPokemon
	setlasttalked 255
	writecode VAR_BATTLETYPE, BATTLETYPE_TRAP
	startbattle
	dontrestartmapmusic
	reloadmap
	iftrue .game_over
	playmusic MUSIC_MOBILE_ADAPTER
	jump .main_loop

.get_money_tile
	opentext
	playwaitsfx SFX_TRANSACTION
	writetext PachisiMoneyText
	givemoney $0, 500
	waitbutton
	closetext
	jump .main_loop

.party_heal_tile
	special HealParty
	special Special_BattleTowerFade
	playwaitsfx SFX_HEAL_POKEMON
	special FadeInPalettes
	showtext HealTileText
	jump .main_loop

.get_item_tile
	callasm GetPachisiItem
	opentext
	verbosegiveitem ITEM_FROM_MEM
	waitbutton
	jump .main_loop

.lose_money_tile
	opentext
	playwaitsfx SFX_BEAT_UP
	writetext PachisiMuncher
	takemoney $0, 500
	waitbutton
	closetext
	jump .main_loop

.death_tile
	playmusic MUSIC_NONE
	playwaitsfx SFX_GS_INTRO_CHARIZARD_FIREBALL
	showtext PachisiDeathText
	jump .game_over

.extra_rolls_tile
	random 4
	sif false
		writebyte 2
	playwaitsfx SFX_EGG_HATCH
	sif =, 1, then
		showtext PachisiGetOneDieRollText
	selse
		showtext PachisiGetDiceRollsText
	sendif
	addtoeventvar EVAR_PACHISI_DICE_ROLLS, 0
	jump .main_loop

.random_tile
	random 11
	anonjumptable
	dw .grass_mon_tile
	dw .cave_mon_tile
	dw .water_mon_tile
	dw .party_heal_tile
	dw .get_item_tile
	dw .lose_money_tile
	dw .get_money_tile
	dw .death_tile
	dw .extra_rolls_tile
	dw .move_forwards_tile
	dw .move_backwards_tile

.move_forwards_tile
	opentext
	random 6
	addvar 1
	writetext PachisiMoveForwardText
	jump .move_player_forwards

.move_backwards_tile
	opentext
	random 6
	addvar 1
	callasm BackwardsTile
	writetext PachisiMoveBackwardsText
	jump .move_player_by_calculated_path

.warp_tile_torenia_forwards
	scall .prepare_warp
	warp TORENIA_PACHISI, 3, 12
	writebyte 69
	jump .do_warp

.warp_tile_torenia_backwards
	scall .prepare_warp
	warp TORENIA_PACHISI, 18, 2
	writebyte 42
	jump .do_warp

.warp_tile_botan_second_section
	scall .prepare_warp
	warp BOTAN_PACHISI, 12, 8
	writebyte 28
	jump .do_warp

.warp_tile_botan_third_section
	scall .prepare_warp
	warp BOTAN_PACHISI, 17, 18
	writebyte 74
	jump .do_warp

.warp_tile_botan_fourth_section
	scall .prepare_warp
	warp BOTAN_PACHISI, 10, 30
	writebyte 110
	jump .do_warp

.warp_tile_botan_item_section
	scall .prepare_warp
	warp BOTAN_PACHISI, 38, 2
	writebyte 188
	jump .do_warp

.warp_tile_botan_random_section
	scall .prepare_warp
	warp BOTAN_PACHISI, 27, 30
	writebyte 147
.do_warp
	writeeventvar EVAR_PACHISI_POSITION
	playsound SFX_WARP_TO
	playmusic MUSIC_MOBILE_ADAPTER
	applymovement PLAYER, .after_warp_movement
	jump .main_loop

.prepare_warp
	playsound SFX_WARP_FROM
	applymovement PLAYER, .before_warp_movement
	special FadeOutPalettes
	end

.finish_tile
	playwaitsfx SFX_DEX_FANFARE_230_PLUS
	scriptstartasm
	ld hl, wPachisiWinCount
	inc [hl]
	jr nz, .done_increasing_win_count
	inc hl
	inc [hl]
	jr nz, .done_increasing_win_count
	dec [hl]
	dec hl
	dec [hl]
.done_increasing_win_count
	scriptstopasm
	checkevent EVENT_PACHISI_GAME_IN_BOTAN
	sif true, then
		warpfacing 1, BOTAN_PACHISI, 15, 39	
		writehalfword EVENT_BOTAN_SHINY_BALL
	selse
		warpfacing 1, TORENIA_PACHISI, 17, 27
		writehalfword EVENT_TORENIA_SHINY_BALL
	sendif
	opentext
	writetext PachisiCongratsText
	checkevent -1
	sif true, then
		verbosegiveitem BIG_NUGGET
	selse
		setevent -1
		verbosegiveitem SHINY_BALL
	sendif
	endtext

.ask_end_game
	writetext AskLeaveText
	yesorno
	iffalse .main_loop
.game_over
	special HealParty
	callasm ForceMapMusicRestart
	checkevent EVENT_PACHISI_GAME_IN_BOTAN
	sif false, then
		warpfacing UP, TORENIA_PACHISI, 17, 27
	selse
		warpfacing UP, BOTAN_PACHISI, 15, 39
	sendif
	jumptext LostGameText

.before_warp_movement
	teleport_from
	step_end

.after_warp_movement
	teleport_to
	step_end

.player_step_aside_movement
	step_left
	turn_head_right
	step_end

.guard_move_down_movement
	step_down
	step_down
	turn_head_up
	step_end

.player_enter_board_movement
	step_right
.guard_move_up_movement
	step_up
	step_up
	step_end

FacePlayerToNextTile:
	call GetPachisiDirection
	call AddPachisiPositionOffset
	ld a, [hl]
	and $3
	ld hl, wPachisiPath
	ld [hli], a
	ld a, movement_step_end
	ld [hl], a
	ret

PachisiGetPokemon:
	ld a, (1 << 7) | 1
	ld [wBattleScriptFlags], a
	call Random
	and $f
	ld l, a
	ld a, [hScriptHalfwordVar]
	add a, l
	ld l, a
	ld a, [hScriptHalfwordVar + 1]
	ld h, a
	jr nc, .ok
	inc h
.ok
	ld a, [hl]
	ld [TempWildMonSpecies], a
	call Random
	and $7
	add a, 25
	ld [CurPartyLevel], a
	ret

GetPachisiItem:
	ld hl, PachisiItems
	call Random
	and $3f
	ld c, a
	ld b, 0
	add hl, bc
	ld a, [hl]
	ld [hScriptVar], a
	ret

GetPachisiTile:
	ld de, EVENT_PACHISI_GAME_IN_BOTAN
	ld b, CHECK_FLAG
	predef EventFlagAction
	ld hl, PachisiToreniaTiles
	jr z, .inTorenia
	ld hl, PachisiBotanTiles
.inTorenia
	call AddPachisiPositionOffset
	ld a, [hl]
	ld [hScriptVar], a
	ret

GetPachisiDirection:
	ld de, EVENT_PACHISI_GAME_IN_BOTAN
	ld b, CHECK_FLAG
	predef EventFlagAction
	ld hl, PachisiToreniaDirections
	ret z
	ld hl, PachisiBotanDirections
	ret

BackwardsTile:
	call GetPachisiDirection
	call AddPachisiPositionOffset
	dec hl
	ld a, [hScriptVar]
	ld c, a
	push bc
	ld de, wPachisiPath
.loop
	ld a, [hld]
	cp movement_step_end
	jr z, .endLoop
	xor 1
	ld [de], a
	inc de
	dec c
	jr nz, .loop
.endLoop
	ld a, movement_step_end
	ld [de], a
	pop bc
	xor a
	sub c
	jr UpdatePachisiPosition

CreatePachisiPath:
	call GetPachisiDirection
	call AddPachisiPositionOffset
	ld a, [hScriptVar]
	ld c, a
	ld b, 0
	ld de, wPachisiPath
	push bc
	rst CopyBytes
	ld a, movement_step_end
	ld [de], a
	pop bc
	ld a, c
UpdatePachisiPosition:
	wbk BANK(wEventVariables)
	ld hl, wEventVariables + EVAR_PACHISI_POSITION
	add a, [hl]
	ld [hl], a
	wbk 1
	ret

AddPachisiPositionOffset:
	wbk BANK(wEventVariables)
	ld a, [wEventVariables + EVAR_PACHISI_POSITION]
	wbk 1
	add a, l
	ld l, a
	ret nc
	inc h
	ret

PachisiMoveBackwardsText:
	start_asm
	ld hl, .backwards_string
	jr PachisiBackwardsForwardsTextRoutine

.backwards_string
	db "backwards@"
PachisiForwardsString:
	db "forwards@"

PachisiMoveForwardText:
	start_asm
	ld hl, PachisiForwardsString
PachisiBackwardsForwardsTextRoutine:
	push bc
	ld de, wStringBuffer3
	ld bc, 10
	rst CopyBytes
	ld a, [hScriptVar]
	pop bc
	ld hl, .multiple_tiles_text
	dec a
	ret nz
	ld hl, .one_tile_text
	ret

.multiple_tiles_text
	db TX_COMPRESSED, $15, $08, $31, $c7, $30
	deciram hScriptVar, 1, 3
	db TX_COMPRESSED, $96, $3e, $9a
	db $1e, $a7, $97
	db $1a, $78, $80

.one_tile_text
	db TX_COMPRESSED, $15, $08, $31, $c0, $90, $e5, $8f
	db $a6, $f5, $3c, $b8
	db $d3, $c4

PachisiOutOfRollsText:
	db TX_COMPRESSED, $72, $85, $a8, $9d, $06, $38, $bd, $24, $0b
	db $56, $f0, $70, $8b, $0f, $7a, $8f

	db $00, $29, $27, $39, $d7, $ce, $0d, $10, $20, $c6, $bf
	db $53, $40

HealTileText:
	db TX_COMPRESSED, $72, $85, $a5, $c0, $72, $be, $12, $41, $1a, $bb
	db $04, $f5, $5a, $89, $83, $5d, $e8, $28, $93, $de, $9e, $78
	db $d3, $c4

LostGameText:
	db TX_COMPRESSED, $07, $8d, $93, $96, $b0, $5f, $8c, $85, $f3
	db $79, $5e, $b2, $a5, $10, $17, $ce, $06, $d2, $6b
	db $55, $63, $7c, $f8
	db $d3, $40

AskLeaveText:
	db TX_COMPRESSED, $ad, $67, $01, $05, $a8, $86, $96, $70, $10, $5a
	db $7a, $ad, $25, $cb, $0c, $e9, $e8, $31, $cb, $49
	db $aa, $e7, $5f, $3a
	db $5a, $68

PachisiRolledDiceText:
	db TX_COMPRESSED, $72, $85, $a8, $b0, $f7, $a7, $99, $b1, $cc, $00
	deciram hScriptVar, 1, 3
	db TX_COMPRESSED
	db $e3, $4f, $10

PachisiAskRollFinalText:
	db TX_COMPRESSED, $c7, $d4, $5c, $58, $7b, $de
	db $2f, $39, $03, $de, $cb, $49, $cc, $8d, $93, $a5
	db $a6, $80

PachisiAskRollText:
	deciram hScriptVar, 1, 2
	db TX_COMPRESSED, $8b, $0f, $7a, $89, $d3, $c2
	db $fc, $f3, $90, $3d, $ec, $b4, $9c, $c8, $d9, $3a
	db $5a, $68

PachisiTextNotEnoughMoney:
	db TX_COMPRESSED, $72, $85, $a9, $90, $92, $99, $13, $a0
	db $c6, $f1, $90, $2d, $72, $98, $f8, $48, $60, $a5
	db $85, $50, $8e, $e8, $2f
	db $53, $40

PachisiTextStartText:
	db TX_COMPRESSED, $6e, $7d, $64, $2f, $9c, $b0, $cb, $49
	db $bd, $c3, $ac, $a4, $68, $1c, $12, $0e, $af, $3c

	db $40, $3a, $b8, $c8, $51, $68, $82, $6b, $5c, $73, $45, $81, $60, $58
	db $7a, $c3, $04, $77, $41

	db $78, $03, $7d, $25, $cb, $0c, $e4, $60, $c7, $07
	db $6f, $6c, $e4, $34
	db $b4, $d0

PachisiMoneyText:
	db TX_COMPRESSED, $72, $85, $a9, $80, $5a, $26, $60, $9a, $d7, $05, $b1, $60, $58
	db $e3, $4d

PachisiMuncher:
	db TX_COMPRESSED, $29, $27, $05, $5a, $23, $29, $35, $c2, $0c, $40, $83
	db $cb, $c2, $6b, $5c, $16, $c5, $81, $62, $07, $0c, $20, $70, $80, $82, $d7
	db $1a, $68

PachisiDeathText:
	db TX_COMPRESSED, $73, $a6, $05, $3c

	db $40, $14, $93, $9c, $eb, $e7, $06, $88, $40
	db $d4, $f0, $41, $8d, $7e
	db $a7, $88

PachisiGetOneDieRollText:
	db TX_COMPRESSED, $61, $68, $c8, $79, $81, $21
	db $bd, $b9, $c8, $ec, $61, $26, $f6, $2c, $3d, $ef
	db $1a, $78, $80

PachisiGetDiceRollsText:
	db TX_COMPRESSED, $61, $68, $c8, $79, $8e, $60
	deciram hScriptVar, 1, 3
	db TX_COMPRESSED
	db $7b, $73, $91, $d8, $c2, $4d, $ec, $58, $7b, $d4, $71
	db $a7, $88

PachisiCongratsText:
	db TX_COMPRESSED, $6d, $09, $39, $5e, $db, $5d, $db, $18, $48, $8e
	db $2f, $39, $42, $d4, $4e, $83, $1c, $a8, $24
	db $e3, $4f, $10

