SavePatches:
	; if build < (2 byte value), apply patch indicated by 4 byte pointer; 0 ends the list
	dh 148
	dw SaveRTCPatch
	
	dh 150
	dw CreateExtraDataSaveArea
	
	dh 151
	dw FixSavefileChecksum
	
	dh 158
	dw FixCaterpieQuestFlags
	
	dh 161
	dw FixIronHeadTMFlag
	
	dh 164
	dw FixSwaggerTMFlags
	
	dh 164
	dw CreateRandomData

	dh 166
	dw SetTimeEnabledBit

	dh 170
	dw FixAronDrifloonFeebasSwabluExp

	dh 175
	dw SetQRReaderEvent

	dh 0

SaveRTCPatch:
	seek sRTCStatusFlags
	lockpos
	readbyte #temp
	or #temp, 0x80
	writebyte #temp
	unlockpos
	return

CreateExtraDataSaveArea:
	seek sExtraData
	fillbyte EXTRA_DATA_SIZE, #zero
	seek sBackupExtraData
	fillbyte EXTRA_DATA_SIZE, #zero
	return

FixCaterpieQuestFlags:
	set #1, EVENT_FLAGS_OFFSET
	call GetGameDataPointer
	seek #result
	set #1, EVENT_POKEONLY_FINISHED_CATERPIE_QUEST
	set #2, #result ;minor optimization, since we know #result > 2
	call FlagAction
	retz #result
	set #1, .flags
	set #2, #zero
	call EventFlagCheck
	retz #result
	print .caterpie_error_text
	menu #result, YesNoMenu
	retnz #result
	set #1, .flags
	set #2, #zero
	call EventFlagPatch
	jump FixSavefileChecksum
	
.caterpie_error_text
	string "It seems like the Caterpie quest in Laurel Forest is done, but the event flags are incorrectly set. Do you want to fix those flags?"
.flags
	dh EVENT_POKEONLY_CATERPIE_IN_PARTY, EVENT_POKEONLY_MOTHERBUTTERFREE_IN_PARTY, -1

FixIronHeadTMFlag:
	set #1, TM_FLAGS_OFFSET
	call GetGameDataPointer
	seek #result
	set #1, 57 ;TM58 - 1
	set #2, #result
	call FlagAction
	retnz #result
	set #1, EVENT_FLAGS_OFFSET
	call GetGameDataPointer
	seek #result
	set #1, EVENT_ROUTE_63_TM
	set #2, #result
	push #1
	call FlagAction
	pop #1
	retz #result
	print .restored_item_text
	set #2, #zero
	call FlagAction
	jump FixSavefileChecksum

.restored_item_text
	string "You seemed to have already picked up the item ball on Route 63, but the item was changed, so the ball was restored for you."

FixSwaggerTMFlags:
	set #1, EVENT_FLAGS_OFFSET
	call GetGameDataPointer
	seek #result
	set #1, EVENT_FIRELIGHT_ITEMROOM_TM
	set #2, #result
	push #1
	call FlagAction
	pop #1
	retz #result
	set #2, #zero
	call FlagAction
	print .restored_item_text
	jump FixSavefileChecksum

.restored_item_text
	string "You seemed to have already picked up the TM in a room in Firelight Caverns, but the TM was changed, so the item ball was restored for you."

CreateRandomData:
	seek sStableRNGData
	writehalfword #zero
	set #temp, 8
.loop
	call Random
	writebyte #result
	decrement #temp
	jumpnz #temp, .loop
	return

SetTimeEnabledBit:
	set #1, STATUS_FLAGS_2_OFFSET
	call GetGameDataPointer
	seek #result
	getfilebyte #temp
	or #temp, 8 ;set bit 3
	writebyte #temp
	jump FixSavefileChecksum

FixAronDrifloonFeebasSwabluExp:
	set #1, .curves
	call FixExperienceCurves
	jump FixSavefileChecksum

.curves
	db ARON, FAST, SLOW
	db LAIRON, FAST, SLOW
	db AGGRON, FAST, SLOW
	db DRIFLOON, MEDIUM_SLOW, FLUCTUATING
	db DRIFBLIM, MEDIUM_SLOW, FLUCTUATING
	db FEEBAS, MEDIUM_FAST, ERRATIC
	db MILOTIC, MEDIUM_FAST, ERRATIC
	db SWABLU, MEDIUM_FAST, ERRATIC
	db ALTARIA, MEDIUM_FAST, ERRATIC
	db 0

SetQRReaderEvent:
	push 1
	set #1, KEY_ITEMS_COUNT_OFFSET
	call GetGameDataPointer
	seek #result
	readbyte #temp
	jump .handle_key_item_loop
.key_item_loop
	readbyte #result
	ifeq #result, QR_READER, .done
	decrement #temp
.handle_key_item_loop
	jumpnz #temp, .key_item_loop
	set #1, PC_ITEMS_COUNT_OFFSET
	call GetGameDataPointer
	seek #result
	readbyte #temp
	jump .handle_pc_item_loop
.pc_item_loop
	readbyte #result
	ifeq #result, QR_READER, .done
	readbyte #result ;skip a byte
	decrement #temp
.handle_pc_item_loop
	jumpnz #temp, .pc_item_loop
	;not found
	stackwrite #zero, #zero
.done
	set #1, EVENT_FLAGS_OFFSET
	call GetGameDataPointer
	seek #result
	set #1, EVENT_HAS_QR_READER
	pop #2
	call FlagAction
	jump FixSavefileChecksum
