uses cyoa.df

0 var slotinfo
0 value slotcount

: slot? slotinfo swap peek $len 1 > nip ;
: slot-info-name saveslot ".slot" $cat ;
: slot-data-name saveslot ".save" $cat ;

cyoa{

	: backicon icons16_id ICON_ARROWLEFT + cyoa-choice-iconid! ;
	: circleicon icons16_id ICON_SLOT_EMPTY + cyoa-choice-iconid! ;
	: ballicon icons16_id ICON_SLOT_USED + cyoa-choice-iconid! ;
	: thumbupicon icons16_id ICON_UP + cyoa-choice-iconid! ;
	: thumbdownicon icons16_id ICON_DOWN + cyoa-choice-iconid! ;
	: skullicon icons16_id ICON_SKULL + cyoa-choice-iconid! ;

	: make-it-snappy 1 cyoa-fade! 1 cyoa-clickspeed! ;

	: load-slotinfo
		3 array to slotinfo
		0 to slotcount
		3 for
			($ 3 i - .. ".slot" $) $sread if
				slotinfo 3 i - rot poke drop
				' slotcount ++
			then
		next
	;

	: start-the-actual-game-here
		"NEW SAVE" slot-info-name $swrite
		"($ skull .. skull .. skull .. skull $) print" slot-data-name $swrite
		gogo
		MAIN_EVENT_NEW main-post-event
	;

	: continue-the-saved-game-here
		gogo
		MAIN_EVENT_DO_LOAD main-post-event
	;

	cyoa-choice: new-gamestart
		choicenum saveslot!
		to-gamestart-setup
	;

	cyoa-choice: cont-saveslot
		choicenum saveslot!
		cyoa-start
		"- CONTINUE GAME -" cyoa-title!
		"THE HORROR CONTINUES..." cyoa-text!
		choice( "YIKES" )choice: continue-the-saved-game-here
	;

	defer select-new

	cyoa-choice: delete-slot make-it-snappy
		skull cyoa-title!
		"DELETED" cyoa-text!
		delete-the-game
		sfx{ bite } 10 0.75 rumble-gui
		choice( "OK" )choice: select-new
	;

// TODO: SLOT HEAD
	cyoa-choice: new-delete-game
		choicenum saveslot!
		cyoa-start make-it-snappy
		"- DELETE GAME -" cyoa-title!
		($ "DELETE old saved game '" .. slotinfo saveslot peek nip .. "'?" $) cyoa-text!
		choice( "NO" )choice: select-new thumbdownicon
		choice( "YES" )choice: delete-slot thumbupicon
	;

	: slotchoices-new 0 args( slot )
		load-slotinfo
		3 for
			3 i - -> slot
			choice( "empty" )choice: new-gamestart circleicon
			<- slot slot? if
				slotinfo <- slot peek cyoa-choice-text! drop
				icons16_id ICON_UP + cyoa-choice-iconid!
				ballicon
				' new-delete-game cyoa-choice-handler!
			then
			<- slot cyoa-choice-num!
		next
	;

	: slotchoices-cont 0 args( slot )
		load-slotinfo
		3 for
			3 i - -> slot
			<- slot slot? if
				choice( "..." )choice: cont-saveslot circleicon
				slotinfo <- slot peek cyoa-choice-text! drop
				<- slot cyoa-choice-num!
				ballicon
			then
		next
	;

	defer select-start // forward reference

	cyoa: select-new-game make-it-snappy
		"- NEW GAME -" cyoa-title!
		"Choose game slot:" cyoa-text!
		slotchoices-new
		choice( "BACK" )choice: select-start backicon
	;

	last to select-new

	cyoa: select-cont-game make-it-snappy
		"- CONTINUE GAME -" cyoa-title!
		"Choose game slot:" cyoa-text!
		slotchoices-cont
		choice( "BACK" )choice: select-start backicon
	;

	cyoa-choice: select-create-char
		to-new-char-maker
	;

	: do-char-maker
		select-start // Have to re-call this CYOA state for C-side char maker to return to
		to-new-char-maker
	;

	cyoa: select-cyoa make-it-snappy
		load-slotinfo

		"- SELECT -" cyoa-title!
		"Choose game option:" cyoa-text!
		slotcount if
			choice( "Continue Game" )choice: select-cont-game
		then
		choice( "New Game" )choice: select-new-game

		// TODO: unlock?
		choice( "Custom Characters" )choice: do-char-maker
		// choice( "Custom Characters" )choice: select-create-char

		choice( "BACK" )choice backicon
	;

	// TEMP: FOR QUICK GAME START
(*
	cyoa: quick-start-game
		"- NEW GAME -" cyoa-title!
		"THE HORROR BEGINS..." cyoa-text!
		choice( "OH, DANG" )choice: start-the-actual-game-here
	;
*)
	: select-boot
		gstats{ tutorial-done } 0 > if select-cyoa then;
//		0 saveslot! MAIN_EVENT_NEW main-post-event road{ tutorial }
//		0 saveslot! MAIN_EVENT_NEW main-post-event 
// "events/exterior/region/canada-crossing-region.df" $load-region to-mission
		0 saveslot! MAIN_EVENT_NEW main-post-event "events/tutorial.df" $load-region to-mission
//		0 saveslot! MAIN_EVENT_NEW main-post-event "events/inner/region/easyhotel.df" $load-region to-mission
	;

	last to select-start

	last to main-event-do-select
}

