SpurgeGym1F_MapScriptHeader;trigger count
	db 2
	maptrigger .Trigger0
	maptrigger .Trigger1

 ;callback count
	db 0

.Trigger0
	priorityjump .Script
.Trigger1
	end

.Script:
	checkevent EVENT_ENTERED_SPURGE_GYM
	clearevent EVENT_ENTERED_SPURGE_GYM
	iffalse .done
	checkevent EVENT_SPURGE_GYM_B1F_ITEM_2
	iffalse .noDynamite
	checkevent EVENT_SPURGE_GYM_SMASHROCK
	iftrue .noDynamite
	takeitem DYNAMITE, 1
.noDynamite
	callasm UnhideParty
	applymovement 2, .GymLeaderMovementAfterPlayerQuits
	faceperson PLAYER, 2
	opentext
	writetext GymLeaderAfterPlayerQuitsText
	waitbutton
	closetext
	applymovement 2, .GymLeaderMovementReturnToOriginalSpot
.done
	dotrigger 1
	end

.GymLeaderMovementAfterPlayerQuits:
	step_down
	step_end

.GymLeaderMovementReturnToOriginalSpot:
	step_up
	step_end
	
SpurgeGymSignpost:
	jumptext SpurgeGymSignpostText

UnhideParty:
	ld hl, wPartyCount
	ld a, [hli]
	ld c, a
	ld b, 0
	add hl, bc
	ld a, [wBackupSecondPartySpecies]
	ld [hl], a
	ld a, $ff
	ld [wPartyEnd], a
	ld a, 6
	ld [wPartyCount], a
	ret

SpurgeGym1FNPC1:
	faceplayer
	opentext
	checkevent EVENT_SPURGE_GYM_DEFEATED
	sif true
		jumptext SpurgeGym1FNPC1_Text_OpportunityToFaceEliteFour

	checkcode VAR_PARTYCOUNT
	sif !=, 6
		jumptext SpurgeGym1FNPC1_Text_ThisGymRequires6Pokemon

	callasm .CheckPartyForEggs
	sif false
		jumptext SpurgeGym1FNPC1_Text_PartyHasEggInIt

	writetext SpurgeGym1FNPC1_Text_2fb03f
	waitbutton
	closetext
	playsound SFX_ENTER_DOOR
	changeblock 4, 2, $17
	reloadmappart
	pause 16
	blackoutmod SPURGE_GYM_1F
	special HealParty
	callasm SpurgeHidePokemon
	setevent EVENT_ENTERED_SPURGE_GYM
	clearevent EVENT_SPURGE_GYM_POKEMON_1
	clearevent EVENT_SPURGE_GYM_POKEMON_2
	clearevent EVENT_SPURGE_GYM_POKEMON_3
	clearevent EVENT_SPURGE_GYM_POKEMON_4
	clearevent EVENT_SPURGE_GYM_POKEMON_5
	clearevent EVENT_SPURGE_GYM_POKEMON_6
	clearevent EVENT_SPURGE_GYM_B1F_NPC_2
	clearevent EVENT_SPURGE_GYM_B1F_NPC_3
	clearevent EVENT_SPURGE_GYM_B1F_NPC_4
	clearevent EVENT_SPURGE_GYM_B1F_NPC_5
	clearevent EVENT_SPURGE_GYM_PUSHROCK
	clearevent EVENT_SPURGE_GYM_B1F_ITEM_2
	clearevent EVENT_SPURGE_GYM_SWITCH_ENABLED
	clearevent EVENT_SPURGE_GYM_SMASHROCK
	applymovement PLAYER, SpurgeGym1FNPC1_HidePlayer
	warp SPURGE_GYM_B1F, 20, 36
	warpsound
	applymovement PLAYER, SpurgeGym1FNPC1_Movement1
	domaptrigger SPURGE_GYM_1F, $0
	jumptext SpurgeGym1F_2fb1ba_Text_2fb0e0

.CheckPartyForEggs:
	ld hl, wPartySpecies
	xor a
	ld [hScriptVar], a
.loop
	ld a, [hli]
	cp EGG
	ret z
	inc a
	jr nz, .loop
	inc a
	ld [hScriptVar], a
	ret

SpurgeGym1FNPC1_HidePlayer:
	skyfall_top
	step_end

SpurgeGym1FNPC1_Movement1:
	skyfall
	step_end

SpurgeGym1FNPC2:
	jumpstd strengthboulder

SpurgeGym1FNPC3:
	jumpstd smashrock

SpurgeHidePokemon:
	ld de, wMisc
	ld bc, wPartyMon1
	ld h, 6
.loop
	push hl
	ld hl, MON_LEVEL
	add hl, bc
	ld a, [hl]
	ld [de], a
	inc de
	cp MAX_LEVEL
	jr nc, .atMaxLevel
	ld [TempMonLevel], a
	push de
	push bc
	call CalcExpToNextLevelPercent
	pop bc
	pop de
	ld a, [hLongQuotient + 3]
	cpl
	jr .writePercent
.atMaxLevel
	call Random
.writePercent
	ld [de], a
	inc de
	ld a, PARTYMON_STRUCT_LENGTH
	add c
	ld c, a
	jr nc, .noCarry
	inc b
.noCarry
	pop hl
	ld a, 6
	sub h
	ld [de], a
	inc de
	dec h
	jr nz, .loop

	call SortPartyFromWeakestToStrongest
	call ReorganizeParty
	ld hl, wPartySpecies
	ld a, [hld]
	ld [wBackupSecondPartySpecies], a
	xor a
	ld [hli], a
	ld [hl], $ff
	ret

CalcExpToNextLevelPercent:
	ld hl, MON_EXP
	add hl, bc
	ld de, TempMonExp
	ld a, [hli]
	ld [de], a
	inc de
	ld a, [hli]
	ld [de], a
	inc de
	ld a, [hl]
	ld [de], a
	callba CalcExpToNextLevel
	ld a, [TempMonLevel]
	ld d, a
	callba CalcExpAtLevel
	ld hl, wTotalExpToNextLevel + 2

	ld a, [hQuotient + 2]
	ld b, a
	ld a, [hld]
	sub b
	ld e, a

	ld a, [hQuotient + 1]
	ld b, a
	ld a, [hld]
	sbc b
	ld d, a

	ld a, [hQuotient]
	ld b, a
	ld a, [hl]
	sbc b
	ld c, a
	jr z, .notFluctuatingL99
; scale both exp values by 2
; scale wTotalExpToNextLevel
	srl c
	rr d
	rr e
; scale wTotalExpToNextLevel
	ld hl, wExpToNextLevel
	srl [hl]
	inc hl
	rr [hl]
	inc hl
	rr [hl]
.notFluctuatingL99
	ld hl, wExpToNextLevel + 1
	ld a, [hli]
	ld [hMultiplicand + 1], a
	ld a, [hl]
	ld [hMultiplicand + 2], a
	xor a
	ld [hMultiplicand], a
	dec a
	ld [hMultiplier], a
	predef Multiply
	ld a, d
	ld [hDivisor], a
	ld a, e
	ld [hDivisor + 1], a
	predef_jump DivideLong

SortPartyFromWeakestToStrongest:
	ld hl, wMisc
	ld c, 6
.loop1
	push hl
	push bc
	call .SortIteration
	pop bc
	pop hl
	jr c, .loop1
	ld hl, wWeakestToStrongestMonList + 2
	ld de, wMisc
	ld c, 6
.loop2
	ld a, [hli]
	ld [de], a
	inc hl
	inc hl
	inc de
	dec c
	jr nz, .loop2
	ret

.SortIteration:
; make sure the list of c items at hl is currently in descending order
; items: 16-bit big-endian value, 8-bit index
; swap the items and return carry if it's not in descending order
	ld de, 0
	ld b, -1
.loop
	ld a, [hli]
	cp d
	jr c, .swap
	jr nz, .replace_de
	ld a, [hl]
	cp e
	jr c, .swap
.replace_de
	dec hl
	ld a, [hli]
	ld d, a
	ld a, [hli]
	ld e, a
	ld a, [hli]
	ld b, a
	dec c
	jr nz, .loop
	and a
	ret

.swap
	inc hl
	ld a, [hl]
	ld [hl], b
	ld b, a
	dec hl
	ld a, [hl]
	ld [hl], e
	ld e, a
	dec hl
	ld a, [hl]
	ld [hl], d
	ld d, a
	dec hl
	ld [hl], b
	dec hl
	ld [hl], e
	dec hl
	ld [hl], d
	scf
	ret

ReorganizeParty:
; copy species
; copy mon data
; copy ot name
; copy nickname
	xor a
.loop
	ld [hMonToStore], a
	ld hl, wWeakestToStrongestMonList
	ld c, a
	ld b, 0
	add hl, bc
	ld a, [hl]
	ld [hMonToCopy], a

	ld de, wPartySpecies
	add e
	ld e, a
	jr nc, .noCarry
	inc d
.noCarry
	ld hl, wPartySpeciesMisc
	add hl, bc
	ld a, [de]
	ld [hl], a

	ld hl, wPartyMon1Misc
	ld de, wPartyMon1
	ld bc, PARTYMON_STRUCT_LENGTH
	call .CopyPartymonDataToMisc

	ld hl, wPartyMonOTMisc
	ld de, wPartyMonOT
	ld bc, NAME_LENGTH
	push bc
	call .CopyPartymonDataToMisc
	pop bc

	ld hl, wPartyMonNicknamesMisc
	ld de, wPartyMonNicknames
	call .CopyPartymonDataToMisc

.handleLoop
	ld a, [hMonToStore]
	inc a
	cp 6
	jr nz, .loop
	ld [wPartyCountMisc], a
	ld a, $ff
	ld [wPartyEndMisc], a
	ld hl, wPokemonDataMisc
	ld de, wPokemonData
	ld bc, wPokemonDataMiscEnd - wPokemonDataMisc
	rst CopyBytes
	ret

.CopyPartymonDataToMisc:
	ld a, [hMonToStore]
	rst AddNTimes
	push hl
	ld h, d
	ld l, e
	ld a, [hMonToCopy]
	rst AddNTimes
	pop de
	rst CopyBytes
	ret
	
SpurgeGymSignpostText:
	db TX_COMPRESSED, $50, $21, $a5, $f2, $71, $84, $09
	
	db $f0, $18, $e7, $b9, $35, $b8, $c1, $25, $b4, $c9
	db $d3, $40

SpurgeGym1FNPC1_Text_ThisGymRequires6Pokemon:
	db TX_COMPRESSED, $50, $15, $ac, $17, $e5, $a4, $68, $9c, $82
	db $7d, $e5, $98, $b3, $68, $6b, $34, $4c, $02, $b8, $08, $2d

	db $00, $b0, $c4, $e8, $31, $cd, $98, $35, $de
	db $bc, $23, $57, $60, $a0, $70, $88, $1d, $3b

	db $e0, $0d, $a1, $7c, $e2, $bd, $65, $4a, $54, $3b
	db $4b, $db, $30, $6b, $bd, $96, $7a, $fc, $d2
	db $65, $55, $a4, $c9, $2a, $37, $0f, $65, $ef, $54

	db $bc, $02, $be, $a0, $3f, $99, $09, $29, $91, $5a, $c6, $4e
	db $5e, $d2, $02, $87, $cf, $28, $83, $25, $86, $02, $0b, $4b
	db $54, $23, $57, $60, $bf, $04, $74, $f5, $07

	db $e0, $0e, $af, $14, $c0, $2b, $80, $82, $d2
	db $de, $56, $64, $3c, $07, $7f, $9a, $40, $55, $68, $2f
	db $53, $40

SpurgeGym1FNPC1_Text_PartyHasEggInIt:
	db TX_COMPRESSED, $50, $15, $ac, $17, $e1, $f3, $ca, $26, $ac, $c9, $4c
	db $7b, $7b, $d0, $d4, $79, $83, $24, $b4, $8d
	db $0a, $98, $40, $9f, $8c, $a7, $7b, $d3, $27, $27

	db $e0, $0d, $a1, $7c, $e2, $bd, $65, $4a, $54, $3b, $4c, $20
	db $5e, $3e, $79, $44, $19, $20, $20, $b4, $b8, $23, $57, $60

	db $80, $0f, $08, $08, $2d, $4a, $b4, $97, $2c
	db $2f, $32, $9d, $ef, $4c, $9c, $9c, $b4, $9c, $61, $02, $7f
	db $d4, $d0

SpurgeGym1FNPC1_Text_2fb03f:
	db TX_COMPRESSED, $6e, $7d, $64, $2f, $9c, $29, $7a, $32, $1a, $f8

	db $80, $ad, $67, $01, $05, $a8, $b3, $dc, $82, $96
	db $17, $b0, $1f, $29, $b9, $80, $57, $2d
	db $26, $a8, $1f, $bd, $a7, $03, $04, $b7, $3c, $9d

	db $28, $0a, $78

	db $00, $37, $3e, $f7, $f1, $d1, $64, $20, $6f
	db $d4, $d0

SpurgeGym1F_2fb1ba_Text_2fb0e0:
	db TX_COMPRESSED, $62, $6b, $3c, $52, $d2, $71, $0a, $43, $21, $01

	db $78, $03, $a6, $03, $97, $aa, $46, $49, $bd, $ec, $0e
	db $0f, $02, $0b, $4b, $80, $e5, $7c, $24

	db $f0, $07, $64, $64, $e0, $20, $b5, $30, $19, $33, $37, $bd
	db $78, $38, $4b, $49, $bf, $f8, $c8, $5f, $39, $a4

	db $c8, $0c, $06, $4c, $c7, $cf, $d0, $a4, $99, $23, $a7, $0f
	db $5e, $41, $9c, $1e, $10, $10, $5a, $2e

	db $30, $16, $b6, $ba, $0a, $54, $15, $da, $41, $40
	db $e0, $f7, $27, $76, $32, $73, $1f, $05, $15, $ee, $79, $3f
	db $53, $40

GymLeaderAfterPlayerQuitsText:
	db TX_COMPRESSED, $07, $86, $e3, $42, $26, $01, $5c, $b4
	db $9b, $cc, $a7, $7b, $d3, $27, $27

	db $4a, $01, $89, $ac, $e6, $ac, $e0, $20, $b4
	db $b7, $81, $ca, $f8, $49, $15, $eb, $2a, $5f
	db $55, $69, $32, $7a, $09, $34, $fb, $e6, $bf

	db $00, $6d, $0b, $e7, $15, $eb, $2a, $50, $24, $64
	db $de, $04, $16, $a7, $27, $71, $16, $b0, $93, $93, $5f
	db $a9, $a0

SpurgeGym1FNPC1_Text_OpportunityToFaceEliteFour:
	db TX_COMPRESSED, $07, $86, $a9, $69, $db, $80, $82, $d4, $4e, $83
	db $1b, $d6, $93, $80, $fd, $ed, $38, $18, $25, $b9, $e4

	db $fe, $00, $10, $5a, $95, $0f, $bd, $8a, $ce, $6a, $be
	db $9b, $d6, $18, $c8, $5f, $10, $76, $70, $64, $96, $93
	db $54, $e4, $1d, $38, $12, $4c, $73, $de, $5a, $3e

	db $20, $0a, $0c, $e4, $ee, $5a, $4d, $67
	db $f7, $b9, $0c, $b0, $ca, $d9, $d2, $0f, $67, $00, $59, $52
	db $42, $ad, $26, $62, $4f, $73, $0f, $51, $7e

	db $00, $e5, $0b, $4e, $1e, $c5, $9e, $b2, $99, $69
	db $37, $81, $fb, $da, $70, $30, $48, $2b, $c9, $af

	db $e0, $2a, $49, $ac, $e0, $20, $b4, $e1, $ec, $56
	db $6f, $6a, $be, $9c, $b0, $c3, $25, $9a, $e5, $a4
	db $d5, $28, $32, $1b, $3b, $1b, $74, $18, $d6, $44

	db $78, $02, $9a, $c2, $fc, $b4, $ed, $c1, $01, $19
	db $2d, $e0, $93, $f0, $10, $5a, $70, $f6, $27, $41, $8e, $58

	db $40, $2c, $fa, $0a, $04, $90, $10, $5a, $5c, $0d, $44
	db $79, $0a, $43, $ef, $65, $86, $13, $41, $87, $ce, $d9

	db $80, $b4, $ac, $2d, $72, $98, $3b, $83
	db $23, $c1, $5e, $4d, $72, $c3, $16, $7a, $ca
	db $55, $5a, $4e, $39, $07, $4e, $04, $93, $1c, $f7, $96, $8f

	db $c0, $14, $5e, $82, $88, $b5, $84, $9c, $f1
	db $a6, $80

SpurgeGym1F_MapEventHeader:: db 0, 0

.Warps: db 2
	warp_def 13, 4, 6, SPURGE_CITY
	warp_def 13, 5, 6, SPURGE_CITY

.CoordEvents: db 0

.BGEvents: db 2
	signpost 11, 2, SIGNPOST_READ, SpurgeGymSignpost
	signpost 11, 7, SIGNPOST_READ, SpurgeGymSignpost

.ObjectEvents: db 1
	person_event SPRITE_CLAIR, 2, 4, SPRITEMOVEDATA_STANDING_DOWN, 0, 0, -1, -1, PAL_OW_BLUE, PERSONTYPE_SCRIPT, 0, SpurgeGym1FNPC1, -1

