// uses base.df
// uses charblurb.df


(* TODO:

*)

// : .statup

// 50% chance if skill = skillcheck... Ex: Character with 2 Strength, cchar .strength@ 2 skillcheck? ... Skill one lower means 33%, higher means 60%, 66%, etc.
// Search for "statroll" below for a bunch of convenience words.

: skillcheck? ( amt checkamt -- true|false )

	0 max swap 0 max swap

	DEBUG if
		2dup swap "SKILL " type type " vs CHECK " type type " (" type
	then

	// Ratio
	over + /

	DEBUG if
		dup type " vs roll of " type
	then

	// Roll
	0 1 frnd

	DEBUG if
		dup type cr "... GOT " type
	then

	// Compare
	>

	DEBUG if
		dup type

		dup if
			" (rolled less or equal, SUCCESS)" else " (rolled over, FAIL)"
		then print
	then
;	

// Words that you really don't need to use, just used for below shortcut words.

: ?false; if r> drop 0 exit then ;

0 value charsel

: .reveal ( STAT char -- )
	2dup .knownstat@ if 2drop exit then
	2dup 1 -rot .knownstat! report-reveal
;

// : .stat+ ( amt STAT char -- )

: .stat++ ( STAT char -- )
	2dup .stat@ CHAR_STAT_MAX = if 2drop exit then
	1 -rot 3dup .stat+ report-statchg
;

: .stat-- ( STAT char -- )
	2dup .stat@ CHAR_STAT_MIN = if 2drop exit then
	-1 -rot 3dup .stat+ report-statchg
;

: .stat2+ ( STAT char -- )
	2dup .stat@ CHAR_STAT_MAX = if 2drop exit then
	2 -rot 3dup .stat+ report-statchg
;

: .stat2- ( STAT char -- )
	2dup .stat@ CHAR_STAT_MIN = if 2drop exit then
	-2 -rot 3dup .stat+ report-statchg
;

: .stat3+ ( STAT char -- )
	2dup .stat@ CHAR_STAT_MAX = if 2drop exit then
	3 -rot 3dup .stat+ report-statchg
;

: .stat4+ ( STAT char -- )
	2dup .stat@ CHAR_STAT_MAX = if 2drop exit then
	4 -rot 3dup .stat+ report-statchg
;

: .stat3- ( STAT char -- )
	2dup .stat@ CHAR_STAT_MIN = if 2drop exit then
	-3 -rot 3dup .stat+ report-statchg
;

: .stat4- ( STAT char -- )
	2dup .stat@ CHAR_STAT_MIN = if 2drop exit then
	-4 -rot 3dup .stat+ report-statchg
;

// Shortcuts for increasing, decreasing, or revealing stats. Example: choicechar .morale2- to take the character chosen by CYOA and reduce their morale by 2.

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

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

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

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

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

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

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

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

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

: .witsattREV .witsREV .attitudeREV ;
: .aholeREV .loyaltyREV .temperREV ;

// choicechar versions of the above, just remove the . at beginning of previous.


: .showitall STAT_COUNT for i 1- over .reveal next drop ;
: bigreveal ' .showitall foreach-team-char ; 
' bigreveal 0 hotkey!

: shootingREVall ' .shootingREV foreach-team-char ;

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

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

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

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


// ========================
// LOOT STUFF
// ========================

: ammosearch1 12 22 rnd LOOT_BULLET trunk.loot+ 50 %chance if 9 16 rnd LOOT_RIFLE_SHELL trunk.loot+ then 30 %chance if 8 12 rnd LOOT_SHOTGUN_SHELL trunk.loot+ then ;
: ammosearch2 20 35 rnd LOOT_BULLET trunk.loot+ 70 %chance if 13 25 rnd LOOT_RIFLE_SHELL trunk.loot+ then 50 %chance if 12 16 rnd LOOT_SHOTGUN_SHELL trunk.loot+ then ;

: homesearch1 4 7 rnd LOOT_FOOD trunk.loot+ 50 %chance if 2 5 rnd LOOT_MEDICAL trunk.loot+ then 30 %chance if 20 40 rnd LOOT_GAS trunk.loot+ then ;
: homesearch2 7 10 rnd LOOT_FOOD trunk.loot+ 70 %chance if 3 7 rnd LOOT_MEDICAL trunk.loot+ then 50 %chance if 30 60 rnd LOOT_GAS trunk.loot+ then ;

: trunk-food LOOT_FOOD trunk.loot@ ;
: trunk-food+ LOOT_FOOD trunk.loot+ ;
: trunk-shotgun-shell LOOT_SHOTGUN_SHELL trunk.loot@ ;
: trunk-shotgun-shell+ LOOT_SHOTGUN_SHELL trunk.loot+ ;
: trunk-gas LOOT_GAS trunk.loot@ ;
: trunk-gas+ LOOT_GAS trunk.loot+ ;
: trunk-medical LOOT_MEDICAL trunk.loot@ ;
: trunk-medical+ LOOT_MEDICAL trunk.loot+ ;
: trunk-bullet LOOT_BULLET trunk.loot@ ;
: trunk-bullet+ LOOT_BULLET trunk.loot+ ;
: trunk-rifle-shell LOOT_RIFLE_SHELL trunk.loot@ ;
: trunk-rifle-shell+ LOOT_RIFLE_SHELL trunk.loot+ ;


// ===========================
// COMMON LOGIC
// ===========================

// General common logic things

: morale++all ' .morale++ foreach-team-char ;
: morale--all ' .morale-- foreach-team-char ;
: morale++all-except ' .morale++ foreach-team-char-except ;
: morale--all-except ' .morale-- foreach-team-char-except ;
: morale2-all ' .morale2- foreach-team-char ;
: morale3-all ' .morale3- foreach-team-char ;
: morale2+all ' .morale2+ foreach-team-char ;
: morale3+all ' .morale3+ foreach-team-char ; // DKT3
: morale4+all ' .morale4+ foreach-team-char ;

// Recruitment shortcuts

// Logic for getting hurt or healing

: .nonfatal .health@ 1 > ;
: .alive? .health@ 0 > ;

: .health-- dup .health@ 1- swap .health! ;
: .health++ dup .health@ 1+ swap .health! gstats{ ' total-heal ++ } ;
: .fullheal dup .vitality@ swap .health! gstats{ ' total-heal ++ } ;


: hit'n'die? dup .health-- .health@ 0= ;

: damage dup .vitality@ swap .health@ - ; 
: hurtcheck checkchar damage pickchar damage > ;
: most-hurt ' hurtcheck pick-team-char ;

: sum-damage damage + ;
: team-damage 0 ' sum-damage foreach-team-char ;

: groupheal
	for 
		most-hurt damage 0 > if
				most-hurt .health++ 
		then
	next
;

(*
// Example using locals for fun
: .effstat locals( stat chara )
	set chara set stat
	get stat get chara .stat@
	get stat get chara .bonus@ +
;
*)

: team-attacked
	dup .health@ 0= if exit then
	dup hit'n'die? if
		($ ICON_SKULL textclr$ .. over .name@ .. " is KILLED!" .. 0 textclr$ $)

			blurbevent blurbskullicon

		sfx{ creepy 30 fx .delay! }

		TEAM_STATUS_DIED over .s .decruit // Needs a REASON when decruiting
	else
		dup .health@ 0 > if
			($ ICON_DOWN textclr$ .. over .name@ .. " is HURT!" ..
				0 textclr$ $) blurbevent ICON_DOWN blurbicon
		then
	then
	blurbchar blurb-last-to-cyoa
;

: .kill	args( chara ) 0 <- chara .health! 
		($ ICON_SKULL textclr$ .. <- chara .name@ .. " is KILLED!" ..
			0 textclr$ $)

			blurbevent blurbskullicon

		sfx{ creepy 30 fx .delay! }

		TEAM_STATUS_DIED <- chara .decruit
		blurb-last-to-cyoa
;

: .ouch-- team-attacked ;
: .ouch2- 2 for dup team-attacked next ;
: ouch--all ' .ouch-- foreach-team-char ;
: ouch2-all ' .ouch2- foreach-team-char ;

: .sting-- dup .nonfatal if .ouch-- then ;
: .sting2- 2 for dup .sting-- next ;
: sting--all ' .sting-- foreach-team-char ;

: .ouch01rnd-- 50 %chance if .ouch-- then ;
: .ouch12rnd-- 50 %chance if .ouch-- else .ouch2- then ;
: .sting12rnd-- 50 %chance if .sting-- else .sting2- then ;

: ouch01rnd--all ' .ouch01rnd-- foreach-team-char ;
: ouch12rnd--all ' .ouch12rnd-- foreach-team-char ;
: sting12rnd--all ' .sting12rnd-- foreach-team-char ;

// Losing weapons and supplies

: givebats // mostly for testing
	8 0 #teamchar .weapona! 
	8 1 #teamchar .weapona! 	
	8 2 #teamchar .weapona! 
	8 3 #teamchar .weapona! 
	8 0 #teamchar .weaponb! 
	8 1 #teamchar .weaponb! 	
	8 2 #teamchar .weaponb! 
	8 3 #teamchar .weaponb! 
;

: takeweapona 0 over .weapona! ;
: takeweaponb 0 over .weaponb! ; 
: takeweaponc 0 over .weaponc! ; 
: takeweapons takeweapona takeweaponb takeweaponc ;
: 0aweaponsall ' takeweapona foreach-team-char ;
: 0bweaponsall ' takeweaponb foreach-team-char ;
: 0weaponsall ' takeweapons foreach-team-char ;

: supplyloss1
	trunk-food 10 > 
		if -5 -10 rnd trunk-food+
	else
		trunk-food 3 > if -1 -4 rnd trunk-food+ then
	then
	trunk-medical 6 >
		if -3 -7 rnd trunk-medical+
	else
		trunk-medical 2 > if -1 -3 rnd trunk-medical+ then
	then	
;

: supplyloss2
	supplyloss1
	trunk-bullet 20 > 
		if -10 -21 rnd trunk-bullet+
	else
		trunk-bullet 10 > if -3 -10 rnd trunk-bullet+ then
	then
	trunk-rifle-shell 20 > 
		if -10 -21 rnd trunk-rifle-shell+
	else
		trunk-rifle-shell 10 > if -3 -10 rnd trunk-rifle-shell+ then
	then
	trunk-shotgun-shell 12 > 
		if -5 -13 rnd trunk-shotgun-shell+
	else
		trunk-shotgun-shell 6 > if -2 -7 rnd trunk-shotgun-shell+ then
	then
;

: nosupplyloss2? 
	trunk-bullet 11 < trunk-rifle-shell 11 < trunk-shotgun-shell 7 <
	trunk-food 4 < trunk-medical 3 < and and and and
;

// Eating checks used for camping / resting

: fispet? .pet@ 0 > ;
: fnotpet? .pet@ 0 = ;

: numeats team-stack ' fnotpet? stack-filter ;
: numtinyeats team-stack ' fispet? stack-filter ;

: petteam numeats 2 * numtinyeats + ;
: 2team team-count 2 * ;
: fullmeal trunk-food 2team 1 - > ;
: halfmeal trunk-food team-count 1 - > ;
: team-eat 2team -1 * trunk-food+ ;
: eat-all trunk-food -1 * trunk-food+ ;

: chowday
	fullmeal if
		team-eat
	then;
	halfmeal if
		team-count -1 * trunk-food+
	then;
	trunk-food 0 > if
		eat-all
	then
;

: .cmorale-recover args( chara )
	<- chara .cmorale- if
		<- chara .cattitude+ if
			<- chara .morale2+ <- chara .attitudeREV
		else
			<- chara .morale++
		then	
	then
;


: chars-process

;
