// Useful words for assigning CYOA roles
0 value victim
0 value bully
0 value sc-camper


: recruited 
	+cr ($ rname .. " joins the team!
	Onward to Canada! " $) +text
	recruitee .morale@ 2 < if
		2 recruitee .morale!
	then
	recruitee .recruit 
;

// Word replacements that alter pronouns and such to the situation

: xhis .female@ if "her" else "his" then ;
: xHis .female@ if "Her" else "His" then ;
: xhim .female@ if "her" else "him" then ;
: xhe .female@ if "she" else "he" then ;
: xHe .female@ if "She" else "He" then ;

: ghis solo if soloer xhis else "their" then ;
: gHis solo if soloer xHis else "Their" then ;
: ghim solo if soloer xhim else "them" then ;
: ghe solo if soloer xhe else "the group" then ;
: gHe solo if soloer xHe else "The group" then ;
: gthey solo if soloer xhe else "they" then ;
: gThey solo if soloer xHe else "They" then ;

// Below words for putting in an extra s depending on group

: g1s solo if "s" else "" then ;
: g2s team-count 2 = if "s" else "" then ;
: gs solo if "" else "s" then ;

: gare solo if "is" else "are" then ;
: gwere solo if "was" else "were" then ;
: gtheywere gthey .. " " .. gwere ;
: gtheyare gthey .. " " .. gare ;
: gTheyare gThey .. " " .. gare ;

// Below example: "butt" aVSs = you're a butt versus you're butts, depending 2 or 3 people.
: g2a team-count-alive 2 = if " a" else "" then ;
: g1a team-count-alive 1 = if " a" else "" then ;
: g3s team-count-alive 2 > if "s" else "" then ;
: aVSs ($ g2a .. swap .. g3s $) ;
: gaVSs ($ g1a .. swap .. gs $) ;

: cname-he solo if soloer xhe else cname then ;
: cname-He solo if soloer xHe else cname then ;
: thegroup-him solo if soloer xhim else "the group" then ;
: chis cchar xhis ;
: cHis cchar xHis ;
: chim cchar xhim ;
: che cchar xhe ;
: cHe cchar xHe ;

: phis pchar xhis ;
: pHis pchar xHis ;
: phim pchar xhim ;
: phe pchar xhe ;
: pHe pchar xHe ;


: rhis recruitee xhis ;
: rhim recruitee xhim ;
: rhe recruitee xhe ;
: rHe recruitee xHe ;

: vhis victim xhis ;
: vhim victim xhim ;
: vhe victim xhe ;
: vHe victim xHe ;

: bhim bully xhim ;
: bhe bully xhe ;
: bHe bully xHe ;


: Thegroup solo if soloer .colourname else "The group" then ;
: thegroup solo if soloer .colourname else "the group" then ;
: yourteam solo if soloer .colourname else "your team" then ;

: Theothers team-count 2 = if notcname else "The other members" then ;
: theothers team-count 2 = if notcname else "the other members" then ;
: Theothersare team-count 2 = if ($ notcname .. " is" $) else "The others are" then ;

: everyone team-count 2 = if notcname else "everyone" then ;
: Everyone team-count 2 = if notcname else "Everyone" then ;

: Theothersare team-count 2 = if "The other member is" else "The other members are" then ;
: theothersare team-count 2 = if "the other member is" else "the other members are" then ;

: attitudeREV STAT_ATTITUDE cchar .reveal ;
: temperREV STAT_TEMPERAMENT cchar .reveal ;
: loyaltyREV STAT_LOYALTY cchar .reveal ;
: witsREV STAT_WITS cchar .reveal ;
: witsattREV witsREV attitudeREV ;

: aholeREV loyaltyREV temperREV ;

: strengthREV STAT_STRENGTH cchar .reveal ;
: medicalREV STAT_MEDICAL cchar .reveal ;
: mechanicREV STAT_MECHANICAL cchar .reveal ;
: shootingREV STAT_SHOOTING cchar .reveal ;
: fitnessREV STAT_FITNESS cchar .reveal ;

: rageREV temperREV strengthREV ;

: morale-- STAT_MORALE cchar .stat-- ;
: morale2- STAT_MORALE cchar .stat2- ;
: morale3- STAT_MORALE cchar .stat3- ;
: morale4- STAT_MORALE cchar .stat4- ;

: attitude-- STAT_ATTITUDE cchar .stat-- ;
: temper-- STAT_TEMPERAMENT cchar .stat-- ;
: loyalty-- STAT_LOYALTY cchar .stat-- ;
: wits-- STAT_WITS cchar .stat-- ;

: strength-- STAT_STRENGTH cchar .stat-- ;
: medical-- STAT_MEDICAL cchar .stat-- ;
: mechanic-- STAT_MECHANICAL cchar .stat-- ;
: shooting-- STAT_SHOOTING cchar .stat-- ;
: fitness-- STAT_FITNESS cchar .stat-- ;

: morale++ STAT_MORALE cchar .stat++ ;
: morale2+ STAT_MORALE cchar .stat2+ ;
: morale3+ STAT_MORALE cchar .stat3+ ;
: morale4+ STAT_MORALE cchar .stat4+ ;

: vitality++ STAT_VITALITY cchar .stat++ ;
: dexterity++ STAT_DEXTERITY cchar .stat++ ;

: attitude++ STAT_ATTITUDE cchar .stat++ ;
: temper++ STAT_TEMPERAMENT cchar .stat++ ;
: loyalty++ STAT_LOYALTY cchar .stat++ ;
: wits++ STAT_WITS cchar .stat++ ;

: strength++ STAT_STRENGTH cchar .stat++ gstats{ ' total-str+ ++ } ;
: medical++ STAT_MEDICAL cchar .stat++ gstats{ ' total-med+ ++ } ;
: mechanic++ STAT_MECHANICAL cchar .stat++ gstats{ ' total-mech+ ++ } ;
: shooting++ STAT_SHOOTING cchar .stat++ gstats{ ' total-shoot+ ++ } ;
: fitness++ STAT_FITNESS cchar .stat++ gstats{ ' total-fit+ ++ } ;

: ricon recruitee character# cyoa-char-icon! ;
: dead? cchar .health@ 1 < ;

0 value post-mission-cyoa

: ouch-- cchar team-attacked ;
: ouch2- 2 for ouch-- next ;
: nonfatal cchar .health@ 1 > ;
: alive? cchar .alive? ;
: health++ cchar .health++ ;
: despairbite cchar team-attacked 50 %chance if cchar team-attacked then ;
: sting-- nonfatal if ouch-- then ;
: sting2- 2 for sting-- next ;

cyoa{

cyoa-choice: toiletwishA
	"Riches and Gold" cyoa-title!
	picon
		($ "Cans of food start raining from the ceiling.
		Finally. You're rich!
		Then the roof collapses on top of " .. pname .. "... " $) cyoa-text! goodfx clear-cyoa
	15 20 rnd for 1 LOOT_FOOD spawnloot scatter next
	' xyzombceil 10 40 90 rnd zwave-ex	
;

cyoa-choice: toiletwishB
		picon
		($ pname .. " stares in horror as " .. phis .. " skin turns to stone, trapping " .. phim .. " in place forever as a statue.
		No wait, it just kind of turned " .. phis .. " skin gray." $) text! goodfx clear-cyoa deleteme
		pchar .vitality++ pchar .fullheal pchar .stoneclr
;

cyoa-choice: toiletwishC
	"The Shocking Answer" cyoa-title!
	picon
	($ "YEAH PRETTY MUCH says the genie. Then it just kind of hovers around, staring at " .. pname .. " for awhile before it disappears." $) cyoa-text! badfx clear-cyoa deleteme
	nearest .tpos anchor! 20 zombs
;

cyoa: toiletwishgenie // Added an underscore so that "toiletwish" isn't redefined, as that would mess up the next part
	"The Wishing Toilet" cyoa-title! 
	($ "When " .. pname .. " opened the toilet, a magical genie appeared.
	" .. "THANKS FOR FREEING ME FROM THE TOILET," pbold .. " it says. " .. "NOW MAKE A WISH.
	" pbold .. pname .. " makes a wish:" $) text!
		choice( "Don't make a wish yet" )choice
		choice( "I vant.. to bee rich" )choice: toiletwishA
		choice( "Immortality" )choice: toiletwishB
		choice( "Are you one of those evil genies?" )choice: toiletwishC
	clear-cyoa
;

: toiletwish_
	new-npc lt .character@ .genieify goodfx lt mission{ .smokepoof }
	0.01 lt .character@ .ai.wander_chance!
	-30 lt .fade!
	90 lt .stun!
	180 lt .aim_angle!
	cyoa{ ' toiletwishgenie cyoa! }
;

// Assign to the previously-defined toiletwish deferred word
' toiletwish_ to toiletwish

: foodamt
	($ " You have " .. LOOT_FOOD looticon .. "" .. trunk-food .. " food left. " $) +text
;

: gasamt
	($ " You have " .. LOOT_GAS looticon .. "" .. trunk-gas .. " gas left. " $) +text
;

cyoa: just-testing beep "TESTING POST-MISSION ROAD-STACKED CYOA" cyoa-text! ;

// Special events called later on stack-road-action

: comicbookgain shooting++ strength++ fitness++ morale3+ ;

cyoa-choice: comicbook-who
	chumany? if
		($ cname .. " gets to read " .. "THE SPANDEX LEAGUE" fbold .. ".
		" .. cHe .. " absorbs the contents! Sadly, " .. che .. " ruins the comic book for others due to " .. chis .. " grimy " .. cfingspaws .. "." $) cyoa-text!
		comicbookgain goodfx
	else
		($ cname .. " completely ruins the comic.
		Seems like someone doesn't like graphic novels." $) cyoa-text! badfx
	then 
;

cyoa: comicbook-2
	"It's a Graphic Novel NOT a Comic Book" cyoa-title!
	picon
	dead? if
		($ "The comic book got torn up during the fighting." $) cyoa-text! badfx
	then;
	($ Thegroup .. " got out with a comic book! It's full of stories of burly people punching each other and shooting each other with eye beams." $) cyoa-text! goodfx
	solohuman if
		choosehuman cicon
		($ cname-He .. " reads it and absorbs the wisdom contained within! It's informative and entertaining!" $) +crtext cchar comicbookgain
	then;  
		"Who should get to read it?" +crtext
		' comicbook-who cyoa-team-choice
;

// More common events

cyoa-choice: bookmag-who
	chumany? if
		($ cname .. " gets to read " .. bookmagtitle .. ".
		" .. cHe .. " absorbs the contents! Sadly, " .. che .. " ruins the magazine for others due to " .. chis .. " grimy " .. cfingspaws .. "." $) cyoa-text!
		cchar bookmaggain goodfx
	else
		($ cname .. " completely ruins the magazine.
		Seems like someone doesn't appreciate fine literature." $) cyoa-text! badfx
	then 
;

cyoa: bookmag-get2
	"Catch up on Reading" cyoa-title!
	picon
	dead? if
		($ "The " .. bookormag .. " was lost in the carnage." $) cyoa-text! badfx
	then;
	($ Thegroup .. " now has the " .. bookormag .. ", " .. bookmagtitle .. "." $) cyoa-text! goodfx
	solohuman if
		choosehuman cicon
		($ cname-He .. " reads it and absorbs the knowledge!" $) +crtext cchar bookmaggain goodfx
	then;  
	bookmagtype 4 > if
		"Everyone gets a chance to read it and learn some new things." +crtext ' bookmaggain foreach-human-char 
	else
		"Who should get to read it?" +crtext
		' bookmag-who cyoa-team-choice
	then
;

cyoa: bookmag-get
	"Reading Material" cyoa-title!
	picon
	($ cname .. " finds a " .. bookormag .. " in good condition, among the ruined magazines and books. 
	The " .. bookormag .. "'s title is: " .. bookmagtitle .. ".
	" .. cHe .. " stuffs it into " .. chis .. " pack." $) cyoa-text!
	deleteme
	clear-cyoa goodfx mission{ ' bookmag-get2 to post-mission-cyoa }
;

cyoa-choice: recruit-replace
	ricon
	($ cname .. " gets booted out of the group to make room for " .. rname .. "." $) cyoa-text!
	TEAM_STATUS_BAD_LEFT cchar .decruit 0.5 600 tone
	recruited
;

: recruit-replace?
	' recruit-replace 3 #cyoa-team-choice
	choice( "Leave " .. rname .. " behind" )choice
;

cyoa: recruit-yes
	team-count 3 > if
		($ "The group is full.
		Who would you like to replace to make room for " .. rname .. "?" $) cyoa-text!
			recruit-replace?
	then;
	ricon
	($ Thegroup .. " accepts " .. rname .. " to the team!" $) cyoa-text! 2 300 tone
		recruited
;

: recruit-yesno
	choice( "Ask " .. rname .. " to join" )choice: recruit-yes
	choice( "Leave " .. rname .. " behind" )choice
;

: npc-died 
	"Lost to the Death Road" cyoa-title! 
	($ pname .. " gets there too late.
	There's nothing that you can do for " .. chim .. " now." $) cyoa-text!
	clear-cyoa badfx
; 

0 value rescskilltype

: rescskillup
	rescskilltype case
		// 0 of dup .strengthREV .strength++ endof - Got rid of the reveal
		0 of .strength++ endof
		1 of .fitness++ endof
		2 of .medical++ endof
		3 of .mechanic++ endof
		4 of .shooting++ endof
	endcase
;
: rescskillname
	rescskilltype case
		0 of "strength" endof
		1 of "fitness" endof
		2 of "medical" endof
		3 of "mechanic" endof
		4 of "shooting" endof
	endcase
;

: rescskill-got
	($ Thegroup .. " gets all sorts of secret " .. rescskillname .. " tips from " .. rname .. "!
	The training ends, and " .. rname .. " departs." $) cyoa-text!
	// formerly cchar
	' rescskillup foreach-human-char goodfx
;

 cyoa-choice: rescskill-who
	choosehuman cicon
	chumany? if
		rescskill-got
	else
		($ cname .. " doesn't understand " .. rname .. "'s attempts at training, at all." $) cyoa-text! badfx
	then 
;

cyoa: rescskill-choose
 	solohuman if
		choosehuman cicon
		rescskill-got
	else  
		($ "Who should get the " .. rescskillname .. " training?" $) cyoa-text!
		' rescskill-who cyoa-team-choice
	then
;
 
: rescskill-pick to rescskilltype 
	// formerly rescskill-choose 
	rescskill-who
;

: groupadd team-count 1 > if "Group " .. then ;

: rescskill1-get 0 rescskill-pick ;
: rescskill1 choice( groupadd "Strength Training" )choice: rescskill1-get 
	4 recruitee .strength!
;
: rescskill2-get 1 rescskill-pick ;
: rescskill2 choice( groupadd "Fitness Training" )choice: rescskill2-get 
	4 recruitee .fitness!
;
: rescskill3-get 4 rescskill-pick ;
: rescskill3 choice( groupadd "Shooting Training" )choice: rescskill3-get 
	4 recruitee .shooting!
;
: rescskill4-get 3 rescskill-pick ;
: rescskill4 choice( groupadd "Mechanic Training" )choice: rescskill4-get 
	4 recruitee .mechanical!
;
: rescskill5-get 2 rescskill-pick ;
: rescskill5 choice( groupadd "Medical Training" )choice: rescskill5-get 
	4 recruitee .medical!
;
 
// Old way of doing it
0 var rescue-skill-deck
	deck-new
	card: rescskill1
	card: rescskill2
	card: rescskill3
	card: rescskill4
	card: rescskill5
constant RESCUE-SKILL
RESCUE-SKILL shuffles to rescue-skill-deck

: rescreward1-get 75 trunk-bullet+ ; 
: rescreward1 choice( "Stash of 75 bullets" )choice: rescreward1-get ;
: rescreward2-get 60 trunk-rifle-shell+ ;
: rescreward2 choice( "Stash of 60 rifle ammo" )choice: rescreward2-get ;
: rescreward3-get 45 trunk-shotgun-shell+ ;
: rescreward3 choice( "45 shotgun shells" )choice: rescreward3-get ;
: rescreward4-get 15 trunk-food+ ;
: rescreward4 choice( "Stash of 15 food" )choice: rescreward4-get ;
: rescreward5-get 80 trunk-gas+ ;
: rescreward5 choice( "Tank of 80 gas" )choice: rescreward5-get ;
: rescreward6-get 10 trunk-medical+ ;
: rescreward6 choice( "Stash of 10 medical" )choice: rescreward6-get ;
: rescreward7-get morale2+all goodfx ;
: rescreward8-get 5 groupheal goodfx ; // trader is 7
: rescreward7 // Two for the price of one
	choice( "Morale Pep Talk" )choice: rescreward7-get
	choice( groupadd "First Aid" )choice: rescreward8-get 
;
 
0 var rescue-reward-deck
	deck-new
	card: rescreward1
	card: rescreward2
	card: rescreward3
	card: rescreward4
	card: rescreward5
	card: rescreward6
	card: rescreward7
	// Training added to resc reward deck
	card: rescskill1
	card: rescskill2
	card: rescskill3
	card: rescskill4
	card: rescskill5
constant RESCUE-REWARD
RESCUE-REWARD shuffles to rescue-reward-deck

: rescue-reward-draw rescue-reward-deck drawcard ;
: rescue-reward-shuffle
	rescue-reward-deck depths 0= if RESCUE-REWARD shuffles to rescue-reward-deck then
	rescue-reward-draw
;
: rescue-skill-shuffle
	rescue-skill-deck depths 0= if RESCUE-SKILL shuffles to rescue-skill-deck then
	rescue-skill-deck drawcard
;

: rescue-trainitem
	// rescue-skill-shuffle
	rescue-reward-shuffle
	rescue-reward-draw
;


: recruit-choice
	choice( "Recruit " .. rname .. " to team" )choice: recruit-yes
;

: rescue-reward
	recruit-choice
	rescue-trainitem
;

: judgerecruit
	most-paranoid if choosechar
		50 %chance if 
			($ cname .. " sizes up " .. rhis .. " skills..." $) +crtext
			recruitee dup .medicalREV dup .mechanicREV dup .shootingREV dup .strengthREV dup .fitnessREV
		else
			($ cname .. " judges " .. rhis .. " character..." $) +crtext
			recruitee dup .attitudeREV dup .temperREV dup .witsREV dup .loyaltyREV
		then goodfx witsattREV
	then
;

cyoa: rescuedme
	// rescue choosechar cicon
	// Maybe need to do a check here to see if the rescue team was left alive
	rescue to recruitee ricon
	0 rescue .party!
	recruitee .alive? if
		"Rescue Successful!" cyoa-title!
		($ rname .. " catches up with " .. thegroup .. " at a safe spot.
		" .. rHe .. " offers a reward in thanks for saving " .. rhim .. "!" $) cyoa-text!
		rescue-reward
		judgerecruit
	else
		"Yeah Well Nobody's Perfect" cyoa-title!
		($ Thegroup .. " feels pretty guilty about " .. rname .. " getting eaten.
		Such is life, I suppose." $) cyoa-text!
		morale--all
	then
;

cyoa: rescueddog
	// rescue choosechar cicon
	rescue to recruitee ricon
	0 rescue .party!
	recruitee .alive? if
		"Rescue Successful!" cyoa-title!
		($ rname .. " catches up with " .. thegroup .. " at a safe spot.
		" .. rHe .. " seems like " .. rhe .. " wants to stick around!" $) cyoa-text!
		recruit-choice
		choice( "Set " .. rname .. " free" )choice: morale2+all
		judgerecruit
	else
		"Dog Adoption Failed" cyoa-title!
		($ Thegroup .. " feels pretty guilty about " .. rname .. " getting eaten.
		A dog could have been really handy." $) cyoa-text!
		morale2-all
	then
;

: drunkchat-up
	rescue choosechar cicon
	($ "OK, I'm awake!" pbold .. "
	" .. cHe .. " says " .. che .. "'ll follow you for awhile. Also that you should meet " .. chim .. " outside of town, if you both make it." $) +crtext
		1 rescue .party!
		rescue .wakeup
		mission{ ' rescuedme to post-mission-cyoa }
		clear-cyoa
;

cyoa-choice: drunkchat-med
	rescue choosechar
	($ pname .. " makes a careful diagnosis, and then starts kicking " .. cname .. " on the rump." $) cyoa-text!
	drunkchat-up
;

cyoa-choice: drunkchat-food
	($ pname .. " starts smooshing some food on " .. cname .. "'s mouth as " .. che .. " sleeps." $) cyoa-text!
	-1 add-food
	drunkchat-up
;

cyoa-choice: drunkchat-pet
	($ pname .. " starts licking " .. cname .. "'s face as " .. che .. " sleeps." $) cyoa-text!
	drunkchat-up
;

cyoa: drunkchat
	// rescue to recruitee ricon
	rescue choosechar cicon
	dead? if npc-died then;
	"Passed Out" cyoa-title!
	($ pname .. " has seen this kind of thing before.
	This person clearly drank too much soda judging from the bottles lying around.
	Now " .. che .. "'s in need of food to even out " .. chis .. " blood sugar." $) cyoa-text!
	pchar notpet? if
		pchar .cmedical+ if
			pchar charchoice( " revives using medical skills" )choice: drunkchat-med
		then
		LOOT_FOOD pchar .lootamt@ 0 > if
			choice( "Feed " .. chim .. " 1 food" )choice: drunkchat-food
		else
			"There might be some food in a nearby building..." +crtext
		then
	else
		pchar charchoice( " revives " .. chim .. " via face licking" )choice: drunkchat-pet
	then
	choice( "Leave " .. chim .. " alone" )choice
;

cyoa-choice: rescueme-2
	($ cname .. " starts following " .. pname .. "!
	" .. cHe .. " could be useful, if you can get " .. chim .. " out of here alive. Or a handy distraction, if you can't." $) cyoa-text!
	rescueget mission{ ' rescuedme to post-mission-cyoa }
;

cyoa-choice: rescueme-d
	($ cname .. " starts following " .. pname .. "!
	" .. cHe .. " could be useful, if you can get " .. chim .. " out of here alive. Or a handy distraction, if you can't." $) cyoa-text!
	rescueget mission{ ' rescueddog to post-mission-cyoa }
;

cyoa: rescueme
	rescue choosechar cicon
	dead? if npc-died then;
	"Everyone needs help sometimes" cyoa-title!
	($ cname .. " says that " .. che .. " really wants to get out of this place.
	" .. cHe .. " asks if " .. che .. " can follow you around." $) cyoa-text!
		choice( "Let " .. chim .. " follow you"  )choice: rescueme-2
		choice( "Leave " .. chim .. " for now" )choice
;

cyoa: dogrescue
	rescue choosechar cicon
	dead? if npc-died then;
	"What's a dog doing here?" cyoa-title!
	($ "The dog seems unusually tame! Most dogs that are still alive have become feral at this point. This dog seems eager to see humans." $) cyoa-text!
		choice( "Let dog follow you"  )choice: rescueme-d
		choice( "Leave dog alone for now" )choice
;

cyoa: feralrescue
	rescue choosechar cicon
	dead? if npc-died then;
	"Dog Team" cyoa-title!
	($ "The dog is feral! It's no longer capable of trusting humans. " $) cyoa-text!
		pchar isdogpal? if
			($ "Of course, " .. pname .. " is different! The dog starts to remember some long forgotten memories of when dogs and humans worked together... " $) +crtext
			choice( "Have dog follow you" )choice: rescueme-d
		else
			"This dog's eyes glimmer with recognition for a brief moment..." +text
		then
		choice( "Leave dog alone" )choice
;

: safe1-get 12 add-shell shotgun onplayer ;
: safe1 choice( "Shotgun with 12 shells" )choice: safe1-get ;
: safe2-get 24 add-bullet dolt onplayer ;
: safe2 choice( "Magnum and 24 bullets" )choice: safe2-get ;
: safe3-get 75 add-bullet ;
: safe3 choice( "Box of 75 bullets" )choice: safe3-get ;
: safe4-get 80 add-gas ;
: safe4 choice( "Container of 80 gas" )choice: safe4-get ;
: safe5-get 45 add-shell ;
: safe5 choice( "Box of 45 shells" )choice: safe5-get ;
: safe6-get 60 add-rifle ;
: safe6 choice( "Box of 60 rifle ammo" )choice: safe6-get ;
: safe7-get 10 add-medical ;
: safe7 choice( "Pack of 10 medical" )choice: safe7-get ;
: safe8-get 15 add-food ;
: safe8 choice( "Stockpile of 15 food" )choice: safe8-get ;
: safe9-get uzi onplayer ;
: safe9 choice( "Submachine Gun" )choice: safe9-get ;
: safe10-get hunting onplayer ;
: safe10 choice( "Hunting Rifle" )choice: safe10-get ;
: safe11-get 20 add-rifle rifle onplayer ;
: safe11 choice( "Cowboy Rifle and 20 ammo" )choice: safe11-get ;
: safe12-geta knight onplayer ;
: safe12-getb ak47 onplayer ;
: safe12-getc autoshot onplayer ;
: safe12-getd slug onplayer ;
: safe12
	20 %chance if
		choice( "Knight Sword" )choice: safe12-geta
	else
		25 %chance if
			choice( "Rifled Slug Shotgun" )choice: safe12-getd
		else
			50 %chance if
				choice( "Auto Shotgun" )choice: safe12-getc
			else
				choice( "Assault Rifle" )choice: safe12-getb
			then
		then
	then
 ;

0 var safe-reward
	deck-new
	card: safe1
	card: safe2
	card: safe3
	card: safe4
	card: safe5
	card: safe6
	card: safe7
	card: safe8
	card: safe9
	card: safe10
	card: safe11
	card: safe12
constant SAFE-REWARD
SAFE-REWARD shuffles to safe-reward

: safe-reward-get
	1 nearest .open!
	safe-reward depths 0= if SAFE-REWARD shuffles to safe-reward then
	4 for safe-reward drawcard next	
	clear-cyoa resetsafe
;
: combo? safecombo 0 > ;

cyoa: safe-crack
	"Safe Cracked!" cyoa-title!
	($ "It takes a couple hours, but " .. pname .. " figures out the safe's combination!
	Along with some now useless money, it had:" $) cyoa-text!
	safe-reward-get
	o'clock@ 2 + o'clock 6 10 rnd nearest .tpos anchor! dup zombs 
;
cyoa: safe
	pchar humany? not if
	"Safe vs Pets" cyoa-title!
		picon
		($ pname .. " doesn't possess the brain necessary to be able to open a safe. Just another example of the human world's anti-" .. cdogcat .. " bias." $) cyoa-text!
	then;
	combo? if 
	"Safe Opened!" cyoa-title!
		($ pname .. " opens the safe with the combination " .. phe .. " found.
		Along with some now useless papers, it had:" $) cyoa-text!
		safe-reward-get
	else
	"Locked Safe" cyoa-title!
	"This safe is locked close, and too sturdy to destroy.
	With luck the combination will be around, somewhere." cyoa-text!
		pchar .cmechanic+ if
			choice( "Leave it be for now" )choice
			pchar charchoice( " cracks the safe!" )choice: safe-crack
		then
	then 
	
;

cyoa: comboskel
	"Combination Number" cyoa-title!
	($ "This skeleton is clutching a small piece of paper. 
	The paper has a series of numbers on it that looks like a padlock or safe combination!
	" .. pname .. " picks it up." $) cyoa-text! goodfx
	1 to safecombo clear-cyoa
;

cyoa: combokey
	"Big Metal Key" cyoa-title!
	($ "This big fancy key looks like it would unlock a safe!
	" .. pname .. " picks it up." $) cyoa-text! goodfx
	1 to safecombo deleteme
;

cyoa: combonote
	"Combination Number" cyoa-title!
	($ "This piece of paper has a series of numbers on it that looks like a padlock or safe combination!
	" .. pname .. " picks it up." $) cyoa-text! goodfx
	1 to safecombo deleteme
;

: lab1 
	cchar .hairtype@ 0 = if 
		($ cname .. " grows new hair! Nice!" $) +crtext morale3+ goodfx
		1 25 rnd cchar .hairtype!
	else
		($ cname .. " loses " .. chis .. " hair!" $) +crtext morale3- badfx
		0 cchar .hairtype!
	then
;
: lab2 
	cchar .cwits+ if
		($ cname .. "'s eyes and mind are a bit more blurry..." $) +crtext wits-- shooting-- badfx
		0.8 cchar .scale.head.x! 0.8 cchar .scale.head.y! 0.5 pchar .headoff.y!
	else
		($ cname .. " feels more perceptive!" $) +crtext wits++ shooting++ goodfx
		1.2 cchar .scale.head.x! 1.2 cchar .scale.head.y! 0.5 pchar .headoff.y!
	then	
;
: lab3
	cchar .dexterity@ 0 > if
		($ cname .. " feels like " .. che .. " is slowing down..." $) +crtext 0 cchar .dexterity! badfx
		0.7 pchar .scale.body.y!
	else
		($ cname .. " feels faster!" $) +crtext dexterity++ goodfx
		1.3 pchar .scale.body.y!
	then	
;
: lab4
	($ cname .. " feels like a new person!" $) +crtext 6 cchar .morale! goodfx
		0 3 rnd cchar .bodytype!
		0 25 rnd cchar .hairtype!
		0 15 rnd cchar .torsotype!
		0 15 rnd cchar .legstype!	
		0 11 rnd cchar .headtype!
		cchar .female@ 0 = if 
			1
		else
			0
		then cchar .female!
;
: lab5
	cpet? if
		($ cname .. " feels more human!" $) +crtext 1 cchar .loyalty! loyalty-- goodfx
		// 0 cchar .pet! cchar statmod{ .undog } 0 3 rnd cchar .bodytype!
		cchar .dogtosuperdog	
	else
		($ cname .. " feels more loyal!" $) +crtext 5 cchar .loyalty! loyalty++ goodfx
		cchar .doggify
	then loyaltyREV
;
: lab6
	50 %chance if
		($ cname .. " feels like the world got bigger!" $) +crtext badfx
		0.8 cchar .scale.head.x! 0.8 cchar .scale.head.y! 0.5 cchar .headoff.y!
		0.7 cchar .scale.body.y! 0.7 cchar .scale.body.x!
	else
		($ cname .. " feels large and in charge!" $) +crtext vitality++ goodfx
		1.2 cchar .scale.head.x! 1.2 cchar .scale.head.y! 0.5 cchar .headoff.y!
		1.2 cchar .scale.body.y! 1.2 cchar .scale.body.x!
	then	
;
: lab7
	cchar .vitality@ 3 > if
		($ cname .. " feels frail!" $) +crtext 2 cchar .vitality!  2 cchar .health! badfx
		0.6 cchar .scale.body.x!
	else
		($ cname .. " feels sturdy!" $) +crtext vitality++ goodfx
		1.5 cchar .scale.body.x!
	then	
;
: lab8
	cchar .specialbody@ 73 = cchar .specialhead@ 73 = and if
		($ cname .. " turns into a dogdogdogtaur! It's unclear what this actually means..." $) +crtext
	then;
	cpet? if
		($ cname .. " turns into a dogman: a dog with the body of a human!" $) +crtext goodfx
		0 cchar .pet! cchar statmod{ .undog } 0 3 rnd cchar .bodytype! 73 cchar .specialhead!
	then;
	cchar .specialbody@ 73 = cchar .specialhead@ 73 = or if
		($ cname .. " turns into a dogdogtaur! A dog with the arms of a human and the head of a dog and the legs of a dog!" $) +crtext goodfx 73 cchar .specialhead! cchar .dogtaur
	else
		($ cname .. " turns into a dogtaur: a dog with the torso and arms of a human!" $) +crtext goodfx
		cchar .dogtaur
	then	
;
: lab9
	cchar .specialtype@ 74 = if
		($ "A fly wanders into the machine! Doesn't seem to bother " .. cname .. "..." $) +crtext
	then;
	cpet? if
		($ cname .. " turns into a dogfly! How horrifying!" $) +crtext goodfx
		0 cchar .pet! cchar statmod{ .undog } 
		50 %chance if 73 cchar .specialhead! cchar .flybody else 73 cchar .specialbody! cchar .flyhead then
	then;
	($ "A fly wanders into the machine as it starts up! " .. cname .. " gets spliced up with fly DNA! Bonus!!!" $) +crtext goodfx cchar
	cchar .specialbody@ 74 = cchar .specialhead@ 74 = or if
		.fly
	else
		rnd5050 if .flyhead else .flybody then
	then	
;


0 var lab-effect
	deck-new
	card: lab1
	card: lab2
	card: lab3
	card: lab4
	card: lab5
	card: lab6
	card: lab7
	card: lab8
	card: lab9
constant LAB-EFFECT
LAB-EFFECT shuffles to lab-effect

cyoa-choice: lab-go
	"Experiment Results" cyoa-title!
	($ cname .. " gets blasted by mysterious science rays until the machine ceases to work. " .. cHe .. " feels energized!" $) text!
	strength++ fitness++ cchar .fullheal
	lab-effect depths 0= if LAB-EFFECT shuffles to lab-effect then
	lab-effect drawcard clear-cyoa
;

cyoa: lab
	"Mysterious Device" cyoa-title!
	($ "Looks like some sorta mad science thing. There's no telling what this could do if messed with." $) text!
		choice( "Leave it alone for now" )choice
		pchar charchoice( " enters the machine!" )choice: lab-go
;


cyoa-choice: altar-1
	($ cname .. " touches the altar and is instantly blasted by strange energies!" $) text!
	ouch2- bitefx 300 tint_trigger 0 light_mode!
	alive? if 
		($ cHe .. " manages to survive, and feels different than before." $) +crtext goodfx
		33 %chance if strength++ else 50 %chance if fitness++ else shooting++ then then
	else
		($ "For " .. chis .. " sacrifice, the altar bestows a gift!
		In the distance, a " .. "spooky goblin" fbold .. " laughs in a goblin-like fashion." $) +crtext
		0 6 rnd case
		0 of 30 add-shell shotgun onplayer endof
		1 of 50 add-bullet dolt onplayer endof
		2 of 10 add-food endof
		3 of 40 add-rifle rifle endof
		4 of 60 add-gas endof
		5 of 7 add-medical endof
		6 of 
			70 %chance if 
				33 %chance if aluminum else 50 %chance if machete2 else fire then then
			else 
				60 %chance if hunting else 70 %chance if uzi else autoshot then then
			then onplayer 
		endof
	endcase
	then
;

cyoa: altar
	"Sacrificial Altar" cyoa-title!
	picon
	($ cname .. " stands near a stone altar. It must be the source of the ominous feeling coming from this entire place.
	The altar seems " .. "extremely dangerous!" rbold .. " It's a bit weird that someone would have this in their house.
	It probably shouldn't be messed with!" $) text!
		choice( "Have " .. cname .. " touch altar" )choice: altar-1
		choice( "Leave altar alone for now" )choice
;

}

: randommag
	magazine 0 4 rnd cyoa{ to bookmagtype ' bookmag-get cyoa! } 50 health! scatter
;

: randombook
	book 5 9 rnd cyoa{ to bookmagtype ' bookmag-get cyoa! } 50 health!
;

: randombookmag
	50 %chance if
	book 5 9
	else
		magazine 0 4
	then rnd cyoa{ to bookmagtype ' bookmag-get cyoa! } 50 health!
;

: altar altarstand cyoa{ ' altar cyoa! } ;
: laboratory labmachine cyoa{ ' lab cyoa! } ;
: metalsafe safe cyoa{ ' safe cyoa! } ;
: combokey goldkey cyoa{ ' combokey cyoa! } delete-covering ;
: comboskel combokey skelscat ;
// skelscat cyoa{ ' comboskel cyoa! } 12 health! ;
: combonote wrotenote scatter cyoa{ ' combonote cyoa! } delete-covering ;

: new-drunk-npc new-npc lt .character@ dup .liedown 0 over .ai.wander_chance! 0 over .ai.attack_chance! drop
	// lying on random side
	rndsign 90 * lt .angle!
	160 190 frnd lt .aim_angle! 
	npc@rescue
	lt cyoa{ ' drunkchat cyoa! }	
;

: new-rescue-npc new-npc@ rescue-weapon over .weapona! 1 lt .unseen! 0 lt .leaveok! npc@rescue cyoa{ ' rescueme cyoa! } drop ;
: new-feraldog map{ selected } . cr new-npc@ .doggify 1 lt .unseen! 0 lt .leaveok! npc@rescue lt cyoa{ ' feralrescue cyoa! } drop ;
: new-rescue-dog map{ selected } . cr new-npc@ .doggify 1 lt .unseen! 0 lt .leaveok! npc@rescue lt cyoa{ ' dogrescue cyoa! } drop ;
: inside-npc new-npc 0 lt .leaveok! 1 lt .unseen! lt .character@ ;
: still-npc new-npc 0 lt .leaveok! 1 lt .unseen! 0 lt .character@ .ai.wander_chance! 180 lt .aim_angle! lt .character@ ;


// Effective stats used to be here, was moved to charfilt.df due to weird dependencies

// =================
// Quick stat checks
// =================

: statroll 2 skillcheck? ;

: moraleroll cchar .morale statroll ;
: attituderoll cchar .attitude statroll ;
: temperroll cchar .temperament statroll ;
: loyaltyroll cchar .loyalty statroll ;
: witsroll cchar .wits statroll ;

: charm-sum dup .attitude swap .wits + ;
: charmroll cchar charm-sum 2 / statroll ;

: strengthroll cchar .strength statroll ;
: medicalroll cchar .medical statroll ;
: mechanicroll cchar .mechanical statroll ;
: shootingroll cchar .shooting statroll ;
: fitnessroll cchar .fitness statroll ;

// checks for the choicechar to have over 4 or under 2 in a stat

: cmorale- cchar .morale low ;
: cattitude- cchar .attitude low ;
: ctemper- cchar .temperament low ;
: cloyalty- cchar .loyalty low ;
: cwits- cchar .wits low ;

: cstrength- cchar .strength low ;
: cmedical- cchar .medical low ;
: cmechanic- cchar .mechanical low ;
: cshooting- cchar .shooting low ;
: cfitness- cchar .fitness low ;

: cmorale+ cchar .morale high ;
: cattitude+ cchar .attitude high ;
: ctemper+ cchar .temperament high ;
: cloyalty+ cchar .loyalty high ;
: cwits+ cchar .wits high ;

: cstrength+ cchar .strength high ;
: cmedical+ cchar .medical high ;
: cmechanic+ cchar .mechanical high ;
: cshooting+ cchar .shooting high ;
: cfitness+ cchar .fitness high ;

: morale+roll cmorale+ moraleroll or ;
: attitude+roll cattitude+ attituderoll or ;
: temper+roll ctemper+ temperroll or ;
: loyalty+roll cloyalty+ loyaltyroll or ;
: wits+roll cwits+ witsroll or ;
: strength+roll cstrength+ strengthroll or ;
: medical+roll cmedical+ medicalroll or ;
: mechanic+roll cmechanic+ mechanicroll or ;
: shooting+roll cshooting+ shootingroll or ;
: fitness+roll cfitness+ fitnessroll or ;

: decruited
	+cr ($ cname .. " has left the team!" $) +text
;

: screcruit recruitee .specialtype! ;
: decruit-bad decruited TEAM_STATUS_BAD_LEFT cchar .decruit badfx ;
: decruit-good decruited TEAM_STATUS_GOOD_LEFT cchar .decruit ;

// pain words moved upwards

: 0weapona
	cchar .weapona@ 0 > if
		($ cHe .. " loses " .. chis .. " primary weapon!" $) +crtext
		0 cchar .weapona!
	then
;
: 0weaponb
	cchar .weaponb@ 0 > if
		($ cHe .. " loses " .. chis .. " secondary weapon!" $) +crtext
		0 cchar .weaponb!
	then
;
: 0weaponc
	cchar .weaponb@ 0 > if
		($ cHe .. " loses " .. chis .. " tertiary weapon!" $) +crtext
		0 cchar .weaponc!
	then
;
: 0weapons
	cchar .weaponc@ 0 > cchar .weaponb@ 0 > cchar .weapona@ 0 > or or if
		($ cHe .. " loses " .. chis .. " weapons!" $) +crtext
		0 cchar .weaponb! 0 cchar .weapona! 0 cchar .weaponc!
	then
;

: trunk.killer 
	trunk.count 0 > if
		($ trunk.count rbold .. " WEAPONS LOST!" rbold $) +crtext
		trunk.clear
	then
;

: chowtime
	fullmeal if
		($ Thegroup .. " eats a decent meal." $)
		cyoa-text-append
		team-eat
		goodfx
		' .cmorale-recover foreach-team-char
	then;
	halfmeal if
		($ Thegroup .. " is low on food, and eats half as much." $)
		cyoa-text-append
		team-count -1 * trunk-food+
		badfx
		' .morale-- foreach-team-char
	then;
	trunk-food 0 = if
		($ Thegroup .. " finds that there is no food left at all. Despair creeps in." $)
		cyoa-text-append
		team-count 1 > if
			' .morale3- foreach-team-char
		else
			soloer .morale2-
		then badfx
	then;
	($ Thegroup .. " didn't have nearly enough food to eat. Doubt festers." $)
		cyoa-text-append
	eat-all
	badfx
	' .morale2- foreach-team-char
;

// Sleeping

: tired statmod{ .tired } ;
: untired statmod{ .untired } ;

: nosleep ' tired foreach-team-char ;
: sleep	' untired foreach-team-char ;
: sleep-except cchar tired cchar ' untired foreach-team-char-except ;

: 50nosleep 50 %chance if tired then ;
: 5050nosleep ' 50nosleep foreach-team-char ;

: tiredcamp chowtime nosleep ;
: fullcamp chowtime sleep ;

// Words for trading
: charm-text choosechar 
	($ "The trader falls for " .. cname .. "'s charm and gives extra!" $) 
	+crtext +cr witsREV attitudeREV cicon
;

: charm-gas	most-charming if charm-text 40 trunk-gas+ then ;
: charmhalf-gas	most-charming if charm-text 20 trunk-gas+ then ;

: charm-bullet most-charming if charm-text 30 trunk-bullet+ then ;
: charmhalf-bullet most-charming if charm-text 15 trunk-bullet+ then ;

: charm-shotgun-shell most-charming if charm-text 20 trunk-shotgun-shell+ then ;
: charmhalf-shotgun-shell most-charming if charm-text 10 trunk-shotgun-shell+ then ;

: charm-rifle-shell	most-charming if charm-text 25 trunk-rifle-shell+ then ;
: charmhalf-rifle-shell	most-charming if charm-text 12 trunk-rifle-shell+ then ;

: charm-medical	most-charming if charm-text 5 trunk-medical+ then ;
: charmhalf-medical	most-charming if charm-text 2 trunk-medical+ then ;

: charm-allammo	most-charming if charm-text 20 trunk-bullet+ 10 trunk-shotgun-shell+ 15 trunk-rifle-shell+ then ;
: charmhalf-allammo	most-charming if charm-text 10 trunk-bullet+ 5 trunk-shotgun-shell+ 7 trunk-rifle-shell+ then ;

: food-all-to-gas trunk-food 10 * trunk-gas+ eat-all ;
: food-15-to-gas 150 trunk-gas+ -15 trunk-food+ charm-gas ;
: food-10-to-gas 100 trunk-gas+ -10 trunk-food+ charmhalf-gas ;
: food-5-to-gas 50 trunk-gas+ -5 trunk-food+ ;

: food-all-to-bullet trunk-food 8 * trunk-bullet+ eat-all ;
: food-15-to-bullet 120 trunk-bullet+ -15 trunk-food+ charm-bullet ;
: food-10-to-bullet 80 trunk-bullet+ -10 trunk-food+ charmhalf-bullet ;
: food-5-to-bullet 40 trunk-bullet+ -5 trunk-food+ ;

: food-all-to-shotgun-shell trunk-food 5 * trunk-shotgun-shell+ eat-all ;
: food-15-to-shotgun-shell 75 trunk-shotgun-shell+ -15 trunk-food+ charm-shotgun-shell ;
: food-10-to-shotgun-shell 50 trunk-shotgun-shell+ -10 trunk-food+ charmhalf-shotgun-shell ;
: food-5-to-shotgun-shell 25 trunk-shotgun-shell+ -5 trunk-food+ ;

: food-all-to-rifle-shell trunk-food 6 * trunk-rifle-shell+ eat-all ;
: food-15-to-rifle-shell 90 trunk-rifle-shell+ -15 trunk-food+ charm-rifle-shell ;
: food-10-to-rifle-shell 60 trunk-rifle-shell+ -10 trunk-food+ charmhalf-rifle-shell ;
: food-5-to-rifle-shell 30 trunk-rifle-shell+ -5 trunk-food+ ;

: food-all-to-medical trunk-food 1 * trunk-medical+ eat-all ;
: food-15-to-medical 15 trunk-medical+ -15 trunk-food+ charm-medical ;
: food-10-to-medical 10 trunk-medical+ -10 trunk-food+ charmhalf-medical ;
: food-5-to-medical 5 trunk-medical+ -5 trunk-food+ ;

: food-all-to-allammo 
	trunk-food 5 * trunk-bullet+ 
	trunk-food 4 * trunk-rifle-shell+ 
	trunk-food 3 * trunk-shotgun-shell+ 
	eat-all 
;
: food-15-to-allammo 75 trunk-bullet+ 45 trunk-shotgun-shell+ 60 trunk-rifle-shell+ -15 trunk-food+ charm-allammo ;
: food-10-to-allammo 50 trunk-bullet+ 30 trunk-shotgun-shell+ 40 trunk-rifle-shell+ -10 trunk-food+ charmhalf-allammo ;
: food-5-to-allammo 25 trunk-bullet+ 15 trunk-shotgun-shell+ 20 trunk-rifle-shell+ -5 trunk-food+ ;

: rob-guiltloyal
	dup .cloyalty+ if
		dup .loyaltyREV
		dup .morale2-
	then;
	dup .loyaltyREV
	dup .morale--
;
: rob-guilt?
	dup .cloyalty- if
		dup .loyaltyREV
		dup .morale++
	then;
	rob-guiltloyal
;
: rob-guilt2?
	dup .cloyalty- if dup .loyaltyREV then;
	rob-guiltloyal
;
: rob-guilt	' rob-guilt? foreach-team-char ;
: rob-guilt2 ' rob-guilt2? foreach-team-char ;

: rob-fail?
	70 %chance if .ouch-- else .morale2- then
;
: rob-fail ouch-- ' rob-fail? foreach-team-char ;
: rob-megafail?
	dup .ouch-- dup .morale3-
;
: rob-megafail ' rob-megafail? foreach-team-char ouch2- ;

// Words for dealing with bandits

: ezbandit? road{ easybandit-mode } 0 > ;
: ezbandit2/ ezbandit? if 2 / then ;

: anyammo? trunk-bullet 1 > trunk-rifle-shell 1 > trunk-shotgun-shell 1 > or or ;

: bandit-toll team-count ezbandit? if 3 * 3 else 4 * 4 then + ;
// 5 * 5 + ;
: bandit-toll2 bandit-toll 1.3 / >int ;

: bandit-toll? trunk-food bandit-toll 1 - > ;
: bandit-toll-pay bandit-toll -1 * trunk-food+ badfx ;
: bandit-toll-pay2 bandit-toll2 -1 * trunk-food+ badfx ;
: bandit-ammo
	trunk-bullet 0 > if trunk-bullet ezbandit2/ -1 * trunk-bullet+ then
	trunk-rifle-shell 0 > if trunk-rifle-shell ezbandit2/ -1 * trunk-rifle-shell+ then
	trunk-shotgun-shell 0 > if trunk-shotgun-shell ezbandit2/ -1 * trunk-shotgun-shell+ then
;
: bandit-med
	trunk-medical 0 > if trunk-medical ezbandit2/ -1 * trunk-medical+ then
;
: bandit-gas
	trunk-gas 0 > if trunk-gas -1 * trunk-gas+ then
;
: bandit-gastank
	trunk-gas 100 > if trunk-gas 100 - -1 * >int trunk-gas+ then
;
: bandit-ammomed bandit-ammo bandit-med ;
: bandit-food trunk-food 0 > if eat-all then ;
: bandit-ammofood bandit-food bandit-ammo ;
: bandit-most bandit-ammofood bandit-med ;
: bandit-mostgas bandit-most bandit-gastank ;
: bandit-all bandit-most bandit-gas ;

: bandit-ammo2
	trunk-bullet 1 > if trunk-bullet 2 / ezbandit2/ -1 * >int trunk-bullet+ then
	trunk-rifle-shell 1 > if trunk-rifle-shell 2 / ezbandit2/ -1 * >int trunk-rifle-shell+ then
	trunk-shotgun-shell 1 > if trunk-shotgun-shell 2 / ezbandit2/ -1 * >int trunk-shotgun-shell+ then
;
: bandit-med2
	trunk-medical 1 > if trunk-medical 2 / ezbandit2/ -1 * >int trunk-medical+ then
;
: bandit-ammomed2
	bandit-ammo2 bandit-med2
;
: bandit-food2
	trunk-food 1 > if trunk-food 2 / -1 * >int trunk-food+ then
;
: bandit-supplies2
	bandit-ammomed2 bandit-food2 bandit-gastank
;

: vname victim .colourname ;
: bname bully .colourname ;
: bully-st bully .specialtype@ ;
: camper-st sc-camper .specialtype@ ;

