Orphanage_MapScriptHeader;trigger count
	db 0
 ;callback count
	db 0

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

OrphanageDonationLady:
	faceplayer
	opentext
	checkevent EVENT_GET_ORPHAN_CARD
	sif false, then
		writetext .first_welcome_text
		verbosegiveitem ORPHAN_CARD, 1
		waitbutton
		writetext .received_orphan_card_text
		setevent EVENT_GET_ORPHAN_CARD
		endtext
	sendif
	checkcode VAR_PARTYCOUNT
	sif <, 2
		jumptext .only_mon_text
	writetext .which_mon_text
	special Special_SelectMonFromParty
	sif false
		jumptext .cancelled_text
	sif =, $ff
		jumptext .invalid_mon_text
	sif =, EGG
		jumptext .egg_text
	callasm IsThisPokemonPlayerLarvitar
	sif true
		jumptext .invalid_mon_text
	callasm OrphanageCalculatePoints
	writetext .confirmation_text
	yesorno
	sif true, then
		callasm .process_donation
		writetext .thank_you_text
		callasm DeletePartyPoke
		special HealParty
		playwaitsfx SFX_HEAL_POKEMON
	sendif
	jumptext .parting_text

.process_donation
	ld hl, wOrphanageDonation1
	push hl
	ld de, wOrphanageDonation2
	ld c, wOrphanageDonationEnd - wOrphanageDonation2
.shift_loop
	ld a, [hli]
	ld [de], a
	inc de
	dec c
	jr nz, .shift_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
	ld a, [wCurPartySpecies]
	call PlayCry
	ld hl, wTempNumber
	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

.only_mon_text
	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

.which_mon_text
	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, $c4

.cancelled_text
	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

.invalid_mon_text
	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

.egg_text
	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

.confirmation_text
	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

.thank_you_text
	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 wTempNumber, 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 $80, $d4, $4d, $8c, $85, $f1, $1d, $1b, $e6, $4b, $d5, $82
	db $79, $06, $b0, $61, $b2, $7e
	db $a7, $88

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

.first_welcome_text
	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, $e2

.received_orphan_card_text
	db TX_COMPRESSED, $17, $03, $1a, $c1, $4b, $1b, $e7, $01, $05
	db $a7, $b2, $12, $6d, $9c, $d8, $0e, $57, $c2, $4f
	db $80, $75, $2a, $1f, $7b, $04, $35, $70, $40, $46, $4b
	db $43, $c1, $24, $04, $16, $97, $19, $d5, $e7

	db $c0, $0a, $49, $cd, $7c, $2d, $12, $e0, $70
	db $78, $40, $46, $4b, $44, $d5, $9c, $57, $a8, $3c
	db $80, $09, $20, $36, $ac, $61, $69, $13, $0d, $66, $c2, $b4
	db $3c, $1c, $25, $a4, $e0, $39, $5f, $09

	db $3c, $01, $d9, $19, $38, $08, $2d, $4e, $4e
	db $de, $32, $05, $ae, $53, $04, $04, $64, $b4, $7e, $02, $0b
	db $40, $19, $d2, $43, $a7, $59, $4e, $93, $93
	db $78, $40, $46, $4b, $44, $c0, $2b
	db $9a, $00, $1c, $af, $84, $92, $d3, $b7, $8a, $34
	db $21, $ec, $02, $b9, $b9, $08, $8b, $18, $49

	db $e0, $0e, $b0, $80, $82, $d4, $d5, $9c, $b5, $b5, $d0
	db $4f, $0c, $96, $6b, $34, $59, $e6, $0c
	db $80, $32, $12, $6d, $8c, $9c, $cb, $0c, $69, $1f
	db $7a, $f7, $aa, $52, $c3, $1f, $39, $bc, $e8, $c9
	db $ea, $68

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

; Is this even a Larvitar-line Pokemon?
	ld a, [wCurPartySpecies]
	sub LARVITAR
	cp TYRANITAR - LARVITAR + 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 c, 1

	push hl
	call .add_DV_score
	call .add_DV_score ; Max: 13

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

	ld a, [wCurPartySpecies]
	ld [wCurSpecies], a
	call GetBaseData
	ld a, [wBaseCatchRate]
	cpl
	call .add_one_eighth ; Max: 69

	ld a, [wBaseExp]
	call .add_one_eighth ; Max: 100

	pop hl ; recover the DVs pointer
	callba CheckShininessHL
	ld a, c
	jr nc, .not_shiny
	; Max if shiny: 400
	add a, a
	add a, a
.not_shiny
	ld hl, wTempNumber + 1
	ld [hld], a
	ld [hl], 0
	rl [hl]
	ret

.add_DV_score
	ld a, [hl]
	rrca
	rrca
	and 3
	ld b, a
	ld a, [hli]
	rlca
	rlca
	and 3
	add a, c
	add a, b
	ld c, a
	ret

.add_one_eighth
	swap a
	rlca
.add_lower_five_bits
	and $1f
	add a, c
	ld c, a
	ret

OrphanageAdoptionLady:
	faceplayer
	opentext
	writetext .introduction_text
.loop
	writetext .which_mon_text
	loadmenudata .menu
	verticalmenu
	closewindow
	addvar -1
	sif >, 3
		jumptext .come_again_text
	loadarray .OrphanageAdoptedPokemonArray
	readarrayhalfword 4
	checkevent -1
	sif true
		jumptext .already_received_text
	readarrayhalfword 2
	callasm CheckOrphanPointsFromScript
	dw -1
	sif =, 2
		jumptext .not_enough_points_text
	checkcode VAR_PARTYCOUNT
	sif =, 6, then
		checkcode VAR_BOXSPACE
		sif false
			jumptext .no_room_text
	sendif
	writetext .confirmation_text
	yesorno
	sif false
		jumptext .come_again_text
	writetext .received_mon_text
	readarray 0
	special Special_GameCornerPrizeMonCheckDex
	cmdwitharrayargs
	db givepoke_command, %0011, 0, 1, NO_ITEM, 0
	endcmdwitharrayargs
	readarrayhalfword 4
	setevent -1
	readarrayhalfword 2
	callasm TakeOrphanPointsFromScript
	dw -1
	jump .loop

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

.menu
	db $40 ; flags
	db 00, 00 ; start coords
	db 11, 19 ; end coords
	dw .menu_options
	db 1 ; default option

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

.introduction_text
	db TX_COMPRESSED, $62, $7d, $e8, $7f, $01, $05, $a8, $ce
	db $91, $e3, $a7, $59, $4e, $93, $93, $80, $82, $d2
	db $c0, $3b, $58, $84, $e9, $27, $08, $46, $4b, $44, $c0, $2b
	db $78, $1c, $af, $84, $91, $a1, $13, $00
	db $ad, $56, $e4, $22, $2c, $61, $27
	db $a9, $e2

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

.come_again_text
	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

.already_received_text
	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

.not_enough_points_text
	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

.no_room_text
	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

.confirmation_text
	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

.received_mon_text
	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, $9e, $20

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, OrphanageDonationLady, -1
	person_event SPRITE_COOLTRAINER_F, 19, 7, SPRITEMOVEDATA_SPINRANDOM_SLOW, 0, 0, -1, -1, PAL_OW_RED, PERSONTYPE_SCRIPT, 0, OrphanageAdoptionLady, -1

