Orphanage_MapScriptHeader;trigger count
	db 0
 ;callback count
	db 0

OrphanageNPC1:
	faceplayer
	checkevent EVENT_GET_ORPHAN_CARD
	iffalse .giveOrphanCard
	faceplayer
	opentext
	checkcode VAR_PARTYCOUNT
	sif <, 2
		jumptext Orphanage_Text_CantDonateOnlyMon
	writetext OrphanageNPC1_Text_182eb0
	waitbutton
	special Special_SelectMonFromParty
	sif false
		jumptext Orphanage_Text_ChangedYourMind
	sif =, $ff
		jumptext Orphanage_Text_WeCantAcceptThisMon
	sif =, EGG
		jumptext Orphanage_Text_DontAcceptEggs
	callasm IsThisPokemonPlayerLarvitar
	sif true
		jumptext Orphanage_Text_WeCantAcceptThisMon
	callasm OrphanageCalculatePoints
	writetext Orphanage_Text_DonateAreYouSure
	yesorno
	iffalse .done
	callasm OrphanageTrackDonatedMon
	callasm OrphanageGiveOrphanPoints
	writetext Orphanage_Text_ThankYouForDonating
	waitbutton
	callasm DeletePartyPoke
	special HealParty
	playwaitsfx SFX_HEAL_POKEMON
.done
	jumptext Orphanage_Text_WeHopeToSeeYouAgain

.giveOrphanCard:
	opentext
	writetext Orphanage_1835cf_Text_183760
	waitbutton
	verbosegiveitem ORPHAN_CARD, 1
	waitbutton
	writetext Orphanage_1835cf_Text_183876
	setevent EVENT_GET_ORPHAN_CARD
	endtext

OrphanageTrackDonatedMon:
	ld hl, wOrphanageDonation1
	push hl
	ld de, wOrphanageDonation2
	ld c, wOrphanageDonationEnd - wOrphanageDonation2
.loop
	ld a, [hli]
	ld [de], a
	inc de
	dec c
	jr nz, .loop
	call UpdateTime
	ld a, [wCurPartySpecies]
	pop hl
	ld [hli], a
	ld de, wCurYear
	rept 2
		ld a, [de]
		inc de
		ld [hli], a
	endr
	ld a, [de]
	ld [hl], a
	ret

OrphanageGiveOrphanPoints:
	ld a, [wCurPartySpecies]
	call PlayCry
	ld hl, TempNumber
	ld a, [hli]
	ld [hMoneyTemp], a
	ld a, [hl]
	ld [hMoneyTemp + 1], a
	ld bc, hMoneyTemp
	push hl
	callba GiveOrphanPoints
	pop hl
	ld a, [hld]
	ld b, [hl]
	ld hl, wAccumulatedOrphanPoints + 3
	add [hl]
	ld [hld], a
	ld a, b
	adc [hl]
	ld [hld], a
	ret nc
	inc [hl]
	ret nz
	dec hl
	inc [hl]
	ret

IsThisPokemonPlayerLarvitar:
; Is it ours?
	call GetCurNick
	callba CheckIfMonIsYourOT
	jr c, .yup

; Is this even a Larvitar-line Pokemon?
	ld a, [wCurPartySpecies]
	cp LARVITAR
	jr c, .nope
	cp TYRANITAR + 1
	jr nc, .nope

; Did we get it in Acqua?
	ld a, MON_CAUGHTLOCATION
	call GetPartyParam
	and $7f
	cp ACQUA_MINES
	jr nz, .nope
.yup
	ld a, 1
	jr .done

.nope
	xor a
.done
	ld [hScriptVar], a
	ret

DeletePartyPoke:
	xor a
	ld [wPokemonWithdrawDepositParameter], a
	jpba RemoveMonFromPartyOrBox

OrphanageCalculatePoints:
	ld a, MON_DVS
	call GetPartyParamLocation
	; Get the attributes of the Party Pokemon
	ld bc, 0

	push hl
	call .GetIVScore
	call .GetIVScore  ;Max: 12

	ld de, MON_LEVEL - (MON_DVS + 2)
	add hl, de
	ld a, [hl]
	call .AddOneQuarterA ; Max: 37

	ld a, [wCurPartySpecies]
	ld [wCurSpecies], a
	call GetBaseData
	ld a, [BaseCatchRate]
	cpl
	call .AddOneEighthA ; Max: 68

	ld a, [BaseExp]
	call .AddOneEighthA ; Max: 99
	inc c ; Max: 100

	pop hl ; recover the DVs pointer
	callba CheckShininessHL
	ld h, b
	ld l, c
	jr nc, .NotShiny
	; Max if shiny: 400
	add hl, hl
	add hl, hl
.NotShiny
	ld a, h
	ld [TempNumber], a
	ld a, l
	ld [TempNumber + 1], a
	ret

.GetIVScore:
	ld a, [hl]
	and $f
	call .AddOneQuarterA
	ld a, [hli]
	swap a
	and $f
	jr .AddOneQuarterA

.AddOneEighthA:
	srl a
.AddOneQuarterA:
	srl a
	srl a
	add c
	ld c, a
	ret

OrphanageNPC2:
	faceplayer
	opentext
	writetext OrphanageNPC2_Text_1839a4
	waitbutton
.loop
	writetext OrphanageNPC2_Text_1839eb
	loadmenudata OrphanageMenu
	verticalmenu
	closewindow
	addvar -1
	sif >, 3
		jumptext OrphanageNPC2_Text_ComeAgainToAdopt
	loadarray .OrphanageAdoptedPokemonArray
	readarrayhalfword 4
	checkevent -1
	sif true
		jumptext Orphanage_Text_AlreadyReceivedMon
	readarrayhalfword 2
	checkorphanpoints -1
	sif =, 2
		jumptext Orphanage_Text_NeedMorePoints
	checkcode VAR_PARTYCOUNT
	sif =, 6, then
		checkcode VAR_BOXSPACE
		sif false
			jumptext Orphanage_Text_PartyAndBoxFull
	sendif
	writetext Orphanage_Text_AdoptAreYouSure
	yesorno
	sif false
		jumptext OrphanageNPC2_Text_ComeAgainToAdopt
	writetext Orphanage_Text_TakeCareOfIt
	waitbutton
	readarray 0
	special Special_GameCornerPrizeMonCheckDex
	cmdwitharrayargs
	db givepoke_command, %0011, 0, 1, NO_ITEM, 0
	endcmdwitharrayargs
	readarrayhalfword 4
	setevent -1
	readarrayhalfword 2
	takeorphanpoints -1
	jump .loop

adopt_mon: MACRO
; input: pokemon, level, orphan point cost
; output:
; 0. pokemon
; 1. level
; 2. orphan point cost
; 4. event flag
	db \1, \2
	dw \3, EVENT_ADOPTED_\1
ENDM

.OrphanageAdoptedPokemonArray:
	adopt_mon CHIKORITA, 10, 100
.OrphanageAdoptedPokemonArrayEntrySizeEnd:
	adopt_mon EEVEE, 15, 250
	adopt_mon TOGEPI, 15, 500
	adopt_mon RIOLU, 15, 1000

OrphanageMenu:
	db $40 ; flags
	db 00, 00 ; start coords
	db 11, 19 ; end coords
	dw OrphanageMenuOptions
	db 1 ; default option

OrphanageMenuOptions:
	db $80 ; flags
	db 5
	db "Chikorita    100@"
	db "Eevee        250@"
	db "Togepi       500@"
	db "Riolu       1000@"
	db "Cancel@"

OrphanageNPC1_Text_182eb0:
	db TX_COMPRESSED, $6e, $7d, $64, $2f, $9c, $b0, $cb, $49
	db $bc, $0e, $57, $c2, $48, $15, $88, $4e, $93, $79, $3f

	db $00, $6e, $91, $b2, $98, $0e, $57, $c2
	db $47, $aa, $0b, $5d, $cc, $04, $16, $a7, $46, $a4, $72
	db $c2, $ab, $21, $26, $d9, $d2
	db $d3, $40

OrphanageNPC2_Text_1839a4:
	db TX_COMPRESSED, $62, $7d, $e8, $7f, $01, $05, $a8, $ce
	db $91, $e3, $a7, $59, $4e, $93, $93, $80, $82, $d2
	db $d5, $3b, $58, $84, $e9, $27, $08, $46, $4b, $44, $c0, $2b
	db $54, $0e, $57, $c2, $48, $d0, $89, $80
	db $56, $ab, $72, $11, $16, $30, $93
	db $d4, $d0

OrphanageNPC2_Text_1839eb:
	db TX_COMPRESSED, $6e, $91, $b2, $98, $0e, $57, $c2, $49, $90
	db $bc, $08, $2d, $4a, $b4, $97
	db $4b, $4d

Orphanage_1835cf_Text_183760:
	db TX_COMPRESSED, $6e, $7d, $64, $2f, $9c, $b0, $cb, $49
	db $bc, $0e, $57, $c2, $48, $15, $88, $4e, $93, $79, $3f

	db $00, $15, $d2, $02, $82, $0c, $22, $3a, $71, $9d, $b6
	db $52, $f0, $39, $5f, $09, $26, $93, $33, $a7, $a0
	db $c6, $02, $d2, $70, $40, $42, $b9, $69, $19, $39
	db $43, $c3, $24, $b4, $98, $d7, $38, $5b
	db $50, $ab, $00, $ac, $c1, $8d, $7e

	db $00, $dc, $e6, $ac, $e2, $4d, $67, $2c
	db $2f, $08, $59, $83, $19, $2e, $5a, $76, $ff, $34, $99
	db $01, $c8, $c1, $8e, $5a, $4e, $34, $4a, $b4, $96, $79
	db $78, $1c, $af, $84, $92, $c3, $04, $18, $44, $74
	db $c0, $54, $90, $cd, $66, $da, $de, $f4, $14, $67, $56
	db $6f, $6a, $b0, $b5, $72, $d2, $6f

	db $f8, $02, $81, $91, $93, $96, $91, $a2, $0d
	db $0f, $02, $0b, $4b, $98, $0d, $68, $b9, $63, $7c
	db $c0, $24, $d6, $7f, $8e, $a5, $43, $ef, $67, $23, $06
	db $37, $81, $05, $a8, $08, $2d, $2e, $03, $1a, $c1, $40, $d4
	db $45, $4e, $d6, $21, $3a, $48, $db, $ab, $cf
	db $a9, $a0

Orphanage_1835cf_Text_183876:
	db TX_COMPRESSED, $17, $03, $1a, $c1, $4b, $1b, $e7, $01, $05
	db $a7, $b2, $12, $6d, $9c, $d8, $0e, $57, $c2, $4f
	db $aa, $75, $2a, $1f, $7b, $04, $35, $70, $40, $46, $4b
	db $42, $a0, $92, $02, $0b, $4b, $8c, $ea, $f3

	db $e0, $05, $24, $e6, $be, $16, $89, $70, $38
	db $3c, $20, $23, $25, $a2, $6a, $ce, $2b, $d4, $1e
	db $55, $04, $90, $1b, $56, $30, $b4, $89, $86, $b3, $61, $5a
	db $15, $07, $09, $69, $38, $0e, $57, $c2, $4f

	db $00, $76, $46, $4e, $02, $0b, $53, $93
	db $b7, $8c, $81, $6b, $94, $c1, $01, $19, $2d, $1f, $80, $82
	db $d2, $a6, $74, $90, $e9, $d6, $53, $a4, $e4
	db $d5, $08, $08, $c9, $68, $98, $05, $73
	db $55, $03, $95, $f0, $92, $5a, $76, $f1, $46, $84
	db $2a, $c0, $2b, $9b, $90, $88, $b1, $84, $9e

	db $00, $eb, $08, $08, $2d, $4d, $59, $cb, $5b, $5d
	db $04, $f0, $c9, $66, $b3, $45, $9e, $60
	db $c8, $ab, $21, $26, $d8, $c9, $cc, $b0, $c6, $91
	db $f5, $57, $bd, $52, $96, $18, $f9, $cd, $e7, $46, $4f
	db $53, $40

Orphanage_Text_CantDonateOnlyMon:
	db TX_COMPRESSED, $72, $85, $a8, $ce, $92, $99, $32, $12, $6d
	db $9b, $c0, $82, $d2, $e0, $49, $d0, $50, $1c, $af, $84, $9e
	db $a6, $80

Orphanage_Text_DontAcceptEggs:
	db TX_COMPRESSED, $74, $59, $10, $15, $ac, $17, $e2, $b6, $ae, $54
	db $6f, $64, $24, $a6, $4d, $65, $93, $11, $70, $f9, $e5, $1e
	db $a6, $80

Orphanage_Text_ChangedYourMind:
	db TX_COMPRESSED, $6d, $4e, $93, $93, $cc, $04, $16, $97, $1f, $19, $33

	db $4a, $01, $d6, $10, $10, $5a, $89, $d0, $63
	db $9a, $f3, $29, $d2, $72, $70, $38, $44, $9e, $ae

	db $fe, $00, $47, $4f, $50, $71, $90, $91, $03, $c9
	db $ad, $ec, $84, $9b, $63, $27, $3e
	db $a6, $80

OrphanageNPC2_Text_ComeAgainToAdopt:
	db TX_COMPRESSED, $6d, $0b, $e7, $37, $9d, $19, $20, $f0, $80, $82
	db $d3, $d5, $69, $2e, $58, $66, $e4, $22, $2e
	db $6a, $a0, $72, $be, $12
	db $7a, $9a

Orphanage_Text_WeHopeToSeeYouAgain:
	db TX_COMPRESSED, $6e, $71, $21, $10, $72, $c3, $10, $67, $01, $05
	db $a7, $b7, $9d, $19
	db $3d, $4d

Orphanage_Text_WeCantAcceptThisMon:
	db TX_COMPRESSED, $74, $59, $10, $15, $ac, $17, $e2, $b6, $ae, $54
	db $6f, $33, $a4, $20, $6e, $6b, $2c, $98, $8b, $96, $91
	db $a1, $50, $39, $5f, $09
	db $3d, $4d

Orphanage_Text_PartyAndBoxFull:
	db TX_COMPRESSED, $74, $59, $10, $15, $ac, $17, $e2, $b6, $ae, $02, $0b, $4b
	db $78, $46, $ae, $c1, $4d, $26, $62, $b0, $d3, $b3, $56
	db $6a, $95, $86, $d3, $30, $6b, $bd
	db $f5, $34

Orphanage_Text_DonateAreYouSure:
	db TX_COMPRESSED, $ad, $67, $01, $05, $a8, $86, $96, $70, $10, $5a
	db $7a, $ad, $25, $cb, $0c, $c8, $49, $b6
	db $6a, $81, $c8, $a5
	db $a6, $80

Orphanage_Text_AdoptAreYouSure:
	db TX_COMPRESSED, $ad, $67, $01, $05, $a8, $86, $96, $70, $10, $5a
	db $7a, $ad, $25, $cb, $0c, $dc, $84, $45
	db $aa, $b4, $8d, $10, $1c, $af, $84, $94
	db $b4, $d0

Orphanage_Text_TakeCareOfIt:
	db TX_COMPRESSED, $29, $3a, $45, $28, $08, $2d, $71

	db $01, $c3, $d3, $d4, $1c, $bd, $52, $39, $c8, $43
	db $97, $99, $d5, $9c, $0e, $10, $77
	db $c6, $9a

Orphanage_Text_ThankYouForDonating:
	db TX_COMPRESSED, $29, $3a, $45, $28, $08, $2d, $4c, $02
	db $b7, $81, $05, $a5, $cc, $84, $9b, $63, $09

	db $3c, $01, $d4, $4e, $83, $1c, $10, $d5, $c7, $30
	deciram TempNumber, 2, 0
	db TX_COMPRESSED
	db $78, $40, $46, $4b, $44, $09, $20, $20, $b4
	db $b5, $4c, $ea, $f3

	db $e0, $0d, $ce, $54, $3e, $f6, $6f, $50
	db $17, $92, $7b, $d8, $08, $2d, $2e, $08, $d5, $d8, $2f
	db $aa, $d4, $4d, $8c, $85, $f1, $1d, $1b, $e6, $4b, $d5, $82
	db $54, $83, $58, $30, $d9, $3f
	db $53, $40

Orphanage_Text_AlreadyReceivedMon:
	db TX_COMPRESSED, $72, $85, $a9, $bd, $59, $ee, $41
	db $3c, $b3, $64, $c6, $0c, $79, $96, $91
	db $a1, $50, $39, $5f, $09, $38
	db $d3, $40

Orphanage_Text_NeedMorePoints:
	db TX_COMPRESSED, $72, $85, $a8, $43, $3c, $c7, $c2, $b3
	db $78, $40, $46, $4b, $44, $c0, $2b, $96, $91
	db $a1, $50, $39, $5f, $09
	db $3d, $4d

Orphanage_MapEventHeader ;filler
	db 0, 0

;warps
	db 2
	warp_def $15, $e, 7, SPURGE_CITY
	warp_def $15, $f, 7, SPURGE_CITY

	;xy triggers
	db 0

	;signposts
	db 0

	;people-events
	db 2
	person_event SPRITE_BUENA, 11, 8, SPRITEMOVEDATA_SPINRANDOM_SLOW, 0, 0, -1, -1, PAL_OW_RED, PERSONTYPE_SCRIPT, 0, OrphanageNPC1, -1
	person_event SPRITE_COOLTRAINER_F, 19, 7, SPRITEMOVEDATA_SPINRANDOM_SLOW, 0, 0, -1, -1, PAL_OW_RED, PERSONTYPE_SCRIPT, 0, OrphanageNPC2, -1

