BattleTowerEntrance_MapScriptHeader:
; triggers
	db 5
	maptrigger .null_trigger
	maptrigger .resume_trigger
	maptrigger .abort_trigger
	maptrigger .reward_trigger
	maptrigger .finished_trigger

; callbacks
	db 0

.abort_trigger
	priorityjump .abort_script
.null_trigger
	end

.resume_trigger
	priorityjump .resume_script
	end

.reward_trigger
	priorityjump .reward_script
	end

.finished_trigger
	priorityjump BattleTower_AbortedOrFinishedBattleTower
	end

.abort_script
	spriteface PLAYER, UP
	opentext
	writetext .abort_text
	callasm BattleTower_ResetWinStreak
	jump BattleTower_AbortedOrFinishedBattleTower

.abort_text
	db TX_COMPRESSED, $74, $59, $10, $15, $ac, $17

	db $80, $39, $42, $d4, $c8, $f3, $08, $1b, $88, $d0, $63
	db $79, $59, $e0, $15, $9c, $32, $64, $64, $e6, $02, $0b, $4b
	db $55, $dd, $45, $c6, $53, $bd, $e9, $93, $93

	db $f0, $01, $23, $67, $5a, $41, $c0, $e1, $2d, $23, $47
	db $de, $04, $16, $97, $04, $2c, $3c, $ac, $d0, $88, $9d
	db $42, $a5, $66, $64, $9d, $0a, $2f

	db $c0, $1b, $9c, $a8, $7d, $ec, $20, $6a, $88, $d0, $63
	db $7a, $d2, $73, $9d, $7c
	db $fd, $4d

.resume_script
	spriteface PLAYER, UP
	opentext
	writetext .resume_text
	callasm BattleTower_LoadChallengeData
	dotrigger 2
	writebyte 1
	callasm SetBattleTowerChallengeState
	callasm SaveGameData
	playwaitsfx SFX_SAVE
	jump BattleTowerReceptionistScript_GoToBattleRoom

.resume_text
	db TX_COMPRESSED, $6e, $7d, $64, $2f, $9c, $57, $ac, $a9, $71

	db $00, $24, $78, $05, $67, $2a, $38, $b3, $43, $4f, $9f
	db $de, $a8, $e3, $ed, $22, $e2, $34, $18, $e5, $a4
	db $d5, $73, $af, $9f
	db $a9, $a0

.reward_normal_text:
	db TX_COMPRESSED, $6d, $09, $39, $5e, $db, $5d, $db, $18, $48, $8e

	db $20, $14, $c2, $b8, $ac, $f6, $c6, $4e, $66, $f7
	db $af, $20, $c1, $8c, $90, $a5, $e8, $c8, $6b, $47
	db $d5, $02, $0b, $52, $a1, $92, $09, $70, $f1

	db $01, $4f, $1c, $59, $b2, $63, $06, $3c
	db $bc, $2c, $a0, $97, $0f
	db $1a, $68

.reward_tycoon_text:
	db TX_COMPRESSED, $6d, $09, $39, $5e, $db, $5d, $db, $18, $48, $8e

	db $20, $14, $c2, $b8, $ac, $f6, $c6, $4e, $66, $f7
	db $af, $20, $c1, $8c, $90, $a5, $e8, $c8, $6b, $45, $3c

	db $00, $2b, $26, $66, $01, $5c, $04, $16
	db $96, $f6, $bf, $a2, $6c, $64, $e6, $60, $3e, $53, $72, $a1, $da
	db $55, $05, $a5, $c2, $81, $32, $10, $92, $9e

	db $00, $0d, $ce, $24, $d6, $6a, $c1, $4d, $ab, $57
	db $97, $81, $05, $a9, $69, $34, $1c, $12, $e1

	db $e2, $02, $9e, $38, $b3, $64, $c6, $0c, $79
	db $79, $cd, $16, $20, $97, $0f
	db $1a, $68

.reward_script
	spriteface PLAYER, UP
	opentext
	callasm BattleTower_CheckDefeatedTycoon
	sif false, then
		writetext .reward_normal_text
		playwaitsfx SFX_2ND_PLACE
		writebyte 3
		callasm BattleTower_GiveBattlePoints
	selse
		callasm BattleTower_IncrementTycoonWinCounter
		writetext .reward_tycoon_text
		playwaitsfx SFX_1ST_PLACE
		writebyte 20
		callasm BattleTower_GiveBattlePoints
	sendif

; fallthrough

BattleTower_AbortedOrFinishedBattleTower:
	opentext
	callasm RestorePartyAfterBattleTower
	domaptrigger BATTLE_TOWER_BATTLE_ROOM, 0
	dotrigger 0
	writebyte 0
	callasm SetBattleTowerChallengeState
	callasm SaveGameData
	playwaitsfx SFX_SAVE
	jump BattleTowerReceptionistScript_Cancel

BattleTower_GiveBattlePoints:
	ld a, [hScriptVar]
	ld [hMoneyTemp + 1], a
	xor a
	ld [hMoneyTemp], a
	ld bc, hMoneyTemp
	jpba GiveBattlePoints

BattleTower_IncrementTycoonWinCounter:
	ld hl, wTowerTycoonsDefeated
	inc [hl]
	ret nz
	inc hl
	inc [hl]
	ret nz
	ld a, $ff
	ld [hld], a
	ld [hl], a
	ret

SetBattleTowerChallengeState:
	sbk BANK(sBattleTowerChallengeState)
	ld a, [hScriptVar]
	ld [sBattleTowerChallengeState], a
	jp CloseSRAM

BattleTowerReceptionistScript:
	faceplayer
	opentext
	writetext .welcome_text
	checkevent EVENT_BATTLE_TOWER_INTRO
	sif false, then
		setevent EVENT_BATTLE_TOWER_INTRO
.play_intro
		writetext .intro_text
	sendif
	writetext .ask_start_challenge_text
	menuanonjumptable .menu
	dw BattleTowerReceptionistScript_Cancel
	dw BattleTowerReceptionistScript_Continue
	dw .play_intro
IF DEF(DEBUG_MODE)
	dw .DEBUG
ELSE
	dw BattleTowerReceptionistScript_Cancel
ENDC

.DEBUG
	callasm BattleTower_InitChallenge
	callasm BattleTower_DebugTeam
	jump BattleTowerReceptionistScript_AskSave

.menu
	db $40 ; flags
	db 00, 00 ; start coords
	db 07, 14 ; end coords
	dw .options
	db 1 ; default option

.options
	db $a0 ; flags
	db 3
	db "Challenge@"
	db "Explanation@"
IF DEF(DEBUG_MODE)
	db "DEBUG @" ;trailing space so there are no shifts between debug and release
ELSE
	db "Cancel@"
ENDC

.welcome_text
	db TX_COMPRESSED, $6e, $7d, $64, $2f, $9c, $b0, $cb, $49
	db $bc, $25, $b7, $7d, $38, $50, $6a, $35, $f1
	db $07, $30

.intro_text
	db TX_COMPRESSED, $62, $6b, $38, $08, $2d, $46, $74, $90, $46, $ac
	db $0b, $d6, $36, $46, $23, $6c, $e0, $c9
	db $34, $02, $0d, $63, $34, $40, $e1, $19, $09, $10, $60
	db $5e, $65, $ab, $18, $31, $c5, $7b, $77, $d3, $47

	db $80, $37, $49, $92, $02, $0b, $50, $c9, $66, $bf
	db $bc, $08, $2d, $4a, $87, $de, $cc, $35, $93, $94
	db $f5, $51, $4b, $d1, $90, $d6, $88, $32, $4d, $8b, $0d, $5e

	db $00, $24, $7b, $73, $7b, $d8, $1c, $25, $a4, $df
	db $7a, $c3, $19, $09, $2c, $64, $68, $e0, $20, $b4
	db $b5, $55, $0c, $84, $19, $39, $88, $b5, $9e, $a9, $71

	db $01, $8e, $14, $1f, $e6, $93, $30, $10, $5a
	db $5b, $c8, $b5, $9e, $a9, $41, $a2, $04, $18, $d7

	db $e0, $0e, $b0, $80, $82, $d4, $c9, $e0, $7b
	db $6f, $19, $02, $d7, $29, $85, $2f, $46, $43, $5a
	db $3e, $00, $10, $5a, $8f, $e8, $29, $95, $ed, $52, $d2
	db $6f, $6d, $d9, $92, $c6, $12, $40, $e1, $02, $d2
	db $d5, $36, $a4, $67, $87
	db $10, $73

.ask_start_challenge_text
	db TX_COMPRESSED, $6e, $16, $bb, $98, $08, $2d, $4e, $8d, $48, $e5
	db $85, $e4, $5e, $ae, $e6, $c6, $53, $bd, $e9, $93, $93, $a5
	db $a6, $80

BattleTowerReceptionistScript_Continue:
	; Do you have at least 3 Pokemon?
	checkcode VAR_PARTYCOUNT
	sif <, 3, then
		writetext BattleTower_NotEnoughPokemonText
		jump BattleTowerReceptionistScript_WaitCancel
	sendif

	; Do you have at least 3 Pokemon that are legal?
	callasm BattleTower_InitChallenge
	writetext BattleTower_ChooseStrengthText

	; Choose either level 50 or open level
	loadmenudata BattleTower_LevelSetMenu
	verticalmenu
	closewindow
	iffalse BattleTowerReceptionistScript_Cancel
	if_equal 3, BattleTowerReceptionistScript_Cancel

	; Do you have enough Pokemon that can participate?
	callasm BattleTower_SetLevelGroup
	callasm CheckAtLeastThreeLegalPokemon
	sif <, 3, then
		writetext BattleTower_NotEnoughPokemonLevelText
		jump BattleTowerReceptionistScript_WaitCancel
	sendif

	; Party menu select
	writetext BattleTower_SelectThreePokemonText
	callasm BattleTower_LegalityCheck
	callasm ChooseThreePartyMonsForBattle
	iffalse BattleTowerReceptionistScript_Cancel
	writetext BattleTower_SaveBeforeText
	yesorno
	iffalse BattleTowerReceptionistScript_Cancel
	callasm BattleTower_SaveGame
	sif false, then
		dotrigger 0
		jump BattleTowerReceptionistScript_Cancel
	sendif
	callasm SetBattleTowerParty

BattleTowerReceptionistScript_AskSave:
	dotrigger 2
	callasm BattleTower_SaveChallengeData
	callasm SaveTheGame

BattleTowerReceptionistScript_GoToBattleRoom:
	domaptrigger BATTLE_TOWER_BATTLE_ROOM, 1
	domaptrigger BATTLE_TOWER_HALLWAY, 0
	domaptrigger BATTLE_TOWER_ELEVATOR, 0
	writetext .follow_me_text
	closetext
	follow 2, PLAYER
	applymovement 2, BattleTowerMovement_ReceptionistWalksUp
	stopfollow
	playsound SFX_ENTER_DOOR
	disappear 2
	applymovement PLAYER, BattleTowerMovement_PlayerStepsUp
	warpcheck
	end

.follow_me_text
	db TX_COMPRESSED, $e1, $e9, $ea, $0e, $60, $1e, $f4, $35, $47, $cf
	db $d4, $f1, $00

BattleTowerMovement_ReceptionistWalksUp:
	step_up
	step_up
	step_up
BattleTowerMovement_PlayerStepsUp:
	step_up
	step_end

BattleTower_LevelSetMenu:
	db $40 ; flags
	db 00, 00 ; start coords
	db 07, 14 ; end coords
	dw .options
	db 1 ; default option

.options
	db $a0 ; flags
	db 3
	db "Level 50@"
	db "Open Level@"
	db "Cancel@"

BattleTower_NotEnoughPokemonText:
	db TX_COMPRESSED, $74, $59, $10, $15, $ac, $17

	db $80, $39, $42, $d4, $21, $9e, $66, $dc, $e9, $ea
	db $2d, $e1, $65, $01, $ca, $f8, $49, $06
	db $40, $01, $5e, $4d, $72, $c3, $04, $6a, $c0
	db $bd, $63, $64, $62, $36, $cf

	db $c0, $05, $dc, $f2, $89, $a4, $cc, $c7, $3e, $4c, $99, $d5, $82
	db $78, $1c, $af, $84, $93, $56, $70, $81
	db $b5, $42, $0d, $6f, $8e, $ec, $f3
	db $ea, $68

BattleTower_ChooseStrengthText:
	db TX_COMPRESSED, $6e, $91, $b2, $98, $b0, $85, $f9, $90, $c0, $41
	db $69, $ea, $86, $84, $cb, $0c, $65, $3b, $de, $99, $39, $3a
	db $5a, $68

BattleTower_NotEnoughPokemonLevelText:
	db TX_COMPRESSED, $74, $59, $10, $15, $ac, $17

	db $80, $39, $42, $d4, $c8, $61, $03, $71, $3a, $0c
	db $6f, $19, $02, $d7, $29, $80, $e5, $7c, $24
	db $01, $52, $46, $ca, $66, $ac, $e6, $dc, $0a
	db $de, $56, $7d, $0d, $52, $d2, $71, $07, $d3, $66, $cf
	db $2a, $ba, $60, $c7, $df

	db $00, $72, $85, $a8, $43, $3c, $cd, $b9, $d3, $d4
	db $5b, $c2, $ca, $03, $95, $f0, $92, $0c
	db $80, $02, $bc, $9a, $e5, $86, $08, $d5, $81
	db $7a, $c6, $c8, $c4, $6d, $9f

	db $80, $0b, $b9, $e5, $13, $49, $99, $8e, $7c, $99, $33, $ab
	db $04, $f0, $39, $5f, $09, $26, $ac, $e1, $03
	db $6a, $84, $1a, $df, $1d, $d9, $e7
	db $d4, $d0

BattleTower_SelectThreePokemonText:
	db TX_COMPRESSED, $e1, $e9, $ea, $0e, $20, $fa, $6c, $dc, $16
	db $4f, $03, $95, $f0, $93
	db $d4, $f1, $00

BattleTower_SaveBeforeText:
	db TX_COMPRESSED, $12, $3c, $02, $b3, $80, $82, $d4, $56, $7c, $8c, $9f
	db $7a, $a3, $8f, $b4, $8b, $88, $d0, $63, $96, $93
	db $55, $ce, $be, $7e, $8e, $91, $2d, $3b, $71, $d8, $98, $a5
	db $a6, $80

BattleTowerReceptionistScript_WaitCancel:
	pause 30
BattleTowerReceptionistScript_Cancel:
	jumptext .text

.text
	db TX_COMPRESSED, $6e, $71, $21, $10, $72, $c3, $10, $67, $01, $05
	db $a7, $b7, $9d, $19, $38
	db $d3, $c4

BattleTower_LegalityCheck:
	ld hl, wPartyCount
	ld a, [hli]
	ld c, a
	ld b, 0
.loop
	ld a, [hli]
	callba BattleTower_IsCurSpeciesLegal
	ccf
	rr b
	dec c
	jr nz, .loop
	ld a, [wPartyCount]
	ld c, a
	ld a, 8
	sub c
.bitShiftLoop
	srl b
	dec a
	jr nz, .bitShiftLoop
	ld a, b
	ld [wBattleTowerLegalPokemonFlags], a
	ret

BattleTowerEntrance_Lass:
	db TX_COMPRESSED, $af, $ab, $1f, $29, $be, $30, $54
	db $13, $d5, $89, $b6, $9f, $d5, $8f, $bd, $9a, $4c, $c7
	db $40, $35, $67, $39, $09, $09, $b1, $61, $ef
	db $5e, $08, $31, $ae, $5a
	db $4d, $53, $21, $7c, $41, $d8, $ec, $61, $27

	db $10, $0d, $c7, $69, $83, $b4, $46, $5a, $b3
	db $7b, $a1, $0a, $92, $3f, $2a, $37, $0f, $62, $b3
	db $54, $d1, $11, $61, $10, $23, $55, $f4, $f8

	db $80, $a7, $82, $53, $c1, $29, $e0, $8c, $5a, $4d
	db $2b, $c5, $24, $68, $83, $41, $29, $93
	db $55, $36, $84, $96, $68, $b8, $c7, $7b, $da
	db $5a, $68

BattleTowerEntrance_CooltrainerM:
	db TX_COMPRESSED, $29, $26, $0a, $60, $32, $6f, $7a, $0a, $08, $6a
	db $de, $d9, $c2, $db, $12, $6b, $3f

	db $00, $12, $3c, $02, $b3, $fc, $04, $16, $a2, $77, $32
	db $c2, $f7, $21, $9b, $de, $cb, $49, $ca, $b4
	db $12, $a5, $7a, $ca, $94, $b0, $ce, $14, $eb, $27, $d1
	db $e8, $0a, $71, $a4, $5c, $b0, $c6, $53, $a4, $e4
	db $de, $b3, $d7, $d1, $35, $61, $68, $99
	db $e3, $4d

BattleTowerEntrance_CooltrainerF:
	db TX_COMPRESSED, $29, $26, $b3, $c5, $36, $09, $6d, $df
	db $4d, $e2, $83, $51, $ae, $0c, $91, $df, $46, $0c, $32
	db $1a, $a6, $d1, $d8, $2f, $cb, $08

	db $7c, $00, $a4, $98, $29, $90, $92, $99, $37, $bd, $0d
	db $4f, $03, $95, $f0, $92, $22, $d6, $12, $72, $6b
	db $55, $69, $d2, $4e, $98, $31, $f6, $53, $f1, $63

	db $c0, $1b, $a7, $6e, $6c, $a7, $02, $a4, $7c, $63, $10
	db $d4, $f6, $ac, $e0, $20, $b5, $10, $d0, $81, $01, $41
	db $e4, $05, $86, $54, $32, $41, $e1, $01, $05, $a8, $ce, $92
	db $98, $f3, $48, $38, $08, $2d, $2e, $22, $d6, $12, $72, $68
	db $b5, $40, $e5, $7c, $24, $a5
	db $a6, $80

BattleTowerEntrance_MapEventHeader:: db 0, 0

.Warps
	db 3
	warp_def 7, 3, 3, ROUTE_71B
	warp_def 7, 4, 3, ROUTE_71B
	warp_def 0, 3, 1, BATTLE_TOWER_ELEVATOR

.CoordEvents
	db 0

.BGEvents
	db 0

.ObjectEvents
	db 5
	person_event SPRITE_RECEPTIONIST, 4, 3, SPRITEMOVEDATA_STANDING_DOWN, 0, 0, -1, -1, PAL_OW_BLUE, PERSONTYPE_SCRIPT, 0, BattleTowerReceptionistScript, -1
	person_event SPRITE_LASS, 5, 0, SPRITEMOVEDATA_WALK_LEFT_RIGHT, 0, 1, -1, -1, PAL_OW_GREEN, PERSONTYPE_TEXTFP, 0, BattleTowerEntrance_Lass, -1
	person_event SPRITE_COOLTRAINER_M, 7, 1, SPRITEMOVEDATA_SPINRANDOM_SLOW, 0, 0, -1, -1, PAL_OW_YELLOW, PERSONTYPE_TEXTFP, 0, BattleTowerEntrance_CooltrainerM, -1
	person_event SPRITE_COOLTRAINER_F, 6, 6, SPRITEMOVEDATA_SPINRANDOM_SLOW, 0, 0, -1, -1, PAL_OW_PURPLE, PERSONTYPE_TEXTFP, 0, BattleTowerEntrance_CooltrainerF, -1
	person_event SPRITE_RECEPTIONIST, 3, 6, SPRITEMOVEDATA_STANDING_DOWN, 0, 0, -1, -1, PAL_OW_BLUE, PERSONTYPE_MART, 0, MART_BATTLETOWER, 0, -1

