// uses hack/region-downtown-test.df
uses paul/region-layout.df

// "../deathforth/hack/gasbar.txt" $load-rooms
// "additional-rooms.txt" $add-rooms

map{ regiondef{

0 value bfx
0 value bfy
0 value gbx
0 value gby
0 value gbx2
0 value gby2

: rdrift args( amt ) lt .pos 2 for <- amt dup neg rnd + swap next
	lt .pos!
;

: lt-here xyp.xy tile>pos lt .pos! ;

: dotirestack tirestack lt-here 8 rdrift ;
: docarwreck carwreck lt-here drift ;

: testbox spawn car_wrecks_base_id 0x 7 + spriteid! bbox immovable lt-here ;

: wooded xyp.xy tile>pos 0 1 frnd * pine lt .pos! lt .sprw 0.5 * 4 + lt .wallbox.x! drift ;

: spawnhere spawn lt-here ;

: smallball bbox lt .rad.x@ lt .rad.y@ min 0 lt .rad.y! lt .rad.x! ;

: missionchar? #teamchar .party@ if 1 then; 0 ;

: .tent-enter args( tent thing )
	<- tent <- thing .enter
;

: camping-road-start
	"SAY THAT WAS A NICE SLEEP, MAYBE SOMEONE GOT HEALED OR IS NOT SICK ANYMORE OR HAD A WEIRD DREAM OR SOMETHING"
	$cyoa-ok-dialog
;

// NOTE: Could also queue this in the road stack as before... just testing new stuff
: end-camping to-road 90 global_delay! -90 fade_to 1 ' camping-road-start timer_add ;

: cyoa-tent args( thing )

	player .inside_id@ if then;

	<- thing player .tent-enter
	0 <- thing .cyoa!
	90 fade_to

	120 ' end-camping timer_add
;

: cyoa-chatter args( thing )
	"HEY I MAYBE SOMEONE CAN TREAT MY WOUNDS OR MY COUSIN HAS A HIDDEN BUNKER IN THE WOODS OR I HEARD SOME URBAN LEGEND"
	" TO INVESTIGATE AS A TRIP-LENGTHENING SIDE-QUEST OR WE CAN JUST CHAT ABOUT STUFF" .. $cyoa-ok-dialog
	// TODO: ... And then a side quest could be QUEUED into the road event stack
;

: tentlt
	special_objects48_base_id 0x 28 + lt .spriteid!
	rnd5050 lt .flip!
	bbox 0 lt .rad.y! 0.5 lt .rad.x@ * lt .rad.x!
	15 lt .hitpoints!
	100 lt .flammability!
	1 lt .no_hit!
	immovable
;

: tenthere spawnhere tentlt
	' cyoa-tent lt .cyoa!
	particles_base_id 0x 3d + lt .action_icon!
;

: ttenthere spawnhere tentlt 8 rdrift ;

: campfire-events args( me eventid )
	<- eventid THING_EVENT_TIMER <> if then;
	0.1 <- me

	5 10 rnd <- me .timer!

	1 ( hour 23 < hour 17 > and ) if
		special_objects48_base_id 0x 29 + <- me .spriteid!
		10 onein if .flames else
			rnd5050 if
				.licks
			else
				.embers
				rnd5050 if
					sfx{ noise 0.1 0.25 frnd fx .scale! 1 10 frnd fx .pitch!
						5000 fx .filt_high!
						2 10 rnd fx .a!
						5 15 rnd fx .d! 0.7 0.95 fx .q! }
					<- me .adjust-vol
				then
			then
		then

		1 <- me .no_lighting!

		<- me .clr.r@ 1 < if 1 else 0.9 then <- me .clr.r!
		0.9 <- me .clr.g!
		0.9 <- me .clr.b!

	else
		0 <- me .no_lighting!
		special_objects48_base_id 0x 2a + <- me .spriteid!
	then
;


: himher if "her" else "him" then ;

: cyoa-fire
	"HMMM WE COULD COOK AN EXTRA GOOD MEAL OR MARSHMALLOWS OR TELL SPOOKY STORIES OR HAVE AN NPC SHOW UP"
	$cyoa-ok-dialog
;

: gravename .thingseed@ dup 1 & seeded-name ;
: gravehimher .thingseed@ 1 & himher ;

: .followed .ai.followid@ #thing ;

// TODO: Adapt this to use everywhere for CYOA stuff
: .sprayit args( me ) ( GREEN swap $cat ) dup <- me statmod{ .yellmsglong } 60 5 * <- me .stun!
	<- me player face-each-other
	( $len <- me .chatter! )
;

: mourner-chat dup .followed args( me grave )
	 ($ "Alas, poor " .. <- grave gravename .. ". I knew " .. <- grave gravehimher .. ", " .. pchar .name@ .. "..." $)
	 <- me .sprayit
;

// TODO: Do actual sayings, draw from a deck of HINTS or MISSIONS or character-specific or just stupid shit
: random-npc-saying
	rnd5050 if
		"How about those zombies, what a pain am I right?"
	else
		"I heard some people are going for Canada, LOL " trollface $cat
	then
;

: camper-chat args( me ) random-npc-saying <- me .sprayit ( $cyoa-ok-dialog ) ; 

: new-trader-npc new-npc
	30 onein if
		lt .character@ dup statmod{ .doggify .superdog }
		WEAPON_CLAW lt .character@ .weapona! // This makes them stand up
	then
;

: new-camper new-trader-npc lt .character@ args( ch )
	lt-here
	0.025 <- ch .ai.wander_chance!
	50 <- ch .ai.safety_leash!
	' camper-chat lt .cyoa!
;

: campfirehere spawnhere lt args( t )
	special_objects48_base_id 0x 29 + lt .spriteid!
	rnd5050 lt .flip!
	1 lt .no_hit!
	0 lt .flammability!
	smallball
	immovable
	' campfire-events lt .events!
	1 lt .timer!
	1 lt .no_lighting!
	1 lt .uncentered!

	' cyoa-fire lt .cyoa!

	lt thing# 1 3 rnd for dup new-camper lt .ai.followid! next drop
;

: tradercartype
	0 9 rnd args( n )

	<- n 3 < if CAR_VAN then;
	<- n 6 < if CAR_STATIONWAGON then;
	<- n 9 < if CAR_FOODTRUCK then;

	rnd5050 if CAR_MUSCLE1 else CAR_MUSCLE2 then
;

: tradercarhere tradercartype spawncar lt-here 0 lt .cyoa! 8 rdrift ;

: gravehere 3 onein if then;
	spawnhere special_objects32_base_id 0x 8f + lt .spriteid!
	rnd5050 lt .flip!
	bbox immovable 
	8 rdrift
	3 onein if
		lt thing# new-camper ' mourner-chat lt .cyoa! lt .ai.followid!
	then
;

: cargohere
	rnd5050 if
		spawnhere special_objects48_base_id 0x 10 + 0 9 rnd + lt .spriteid! bbox immovable 8 rdrift
	then;

	3 for indcrate lt-here immovable drift next
;

: trader-cyoa args( me )
	<- me player face-each-other
	"INSERT TRADER CYOA STUFF HERE" $cyoa-ok-dialog
	"Trader" cyoa-title!
;

: .interacting? thing# player .nearest_interact_id@ = ;

: trader-event args( me eventid )
	THING_EVENT_TIMER <- eventid <> if then;

	15 <- me .repeating_timer

	<- me .interacting? if "" <- me .report then;

	1 <- me .loot@ looticon <- me .reportdur
;

: new-trader new-trader-npc lt .character@ args( ch )
	lt-here
	0.05 <- ch .ai.wander_chance!
	50 <- ch .ai.safety_leash!
	50 <- ch .ai.follow_leash!
	179 rnd5050 + lt .aim_angle!
	' trader-event lt .events!
	1 lt .timer!

	// TODO: Decide how to pick what CYOA to use for the trader and set it up

	LOOT_NONE LOOT_COUNT 2 - rnd lt .loot!
	' trader-cyoa lt .cyoa!
;


: traderhere cargohere lt args( loc )
	<- loc thing# new-trader lt .ai.followid!
;

0 value dx
0 value dy
0 value sy

: dirtstart xyp.x to dx xyp.y to dy ;
: dirtend dx dy xyp.x xyp.y map{ 4dup dirtbox grasshole } ;
: gravelendw dx 1- dy xyp.x 1+ xyp.y map{ 4dup gravelbox grasshole } ;

: grassstart xyp.y to sy ;
: grassend map{ -1 sy w xyp.y grasslot } ;

' plotdirt >defer ft

: pt
	xyp.xy ft // Plot the current floor by default

	xyp.i char v = if tradercarhere then;
	xyp.i char P = if pictable1 lt-here 8 rdrift then;
	xyp.i char x = if gravehere then;
	xyp.i char c = if campfirehere then;
	xyp.i char C = if tenthere then;
	xyp.i char * = if ttenthere then;
	xyp.i char T = if traderhere then;
//	xyp.i char t = if tires then;
	xyp.i char w = if docarwreck then;
	xyp.i char s = if dotirestack then;
	xyp.i char t = if wooded then;
	xyp.i char { = if dirtstart then;
	xyp.i char } = if dirtend then;
	xyp.i char [ = if dirtstart then;
	xyp.i char ] = if gravelendw then;
	xyp.i char ( = if grassstart then;
	xyp.i char ) = if grassend then;
;

"
.....................
.....................
.....................
ttttttttttttttttttttt
.....................
ttttttttttttttttttttt
.....{ttttttttt......
tttt....x.x.x....tttt
........x.x.x........
.....................
...............}.....
.....................
ttt...............ttt
.....................
....*...*...*...*....
.....................
........{............
...*......c......*...
.....................
............}........
.....................
.......P.....P.......
.....................
.....................
.....................
....T.....T.....T....
.....................
.....................
.....................
.s....s.......s....s.
[....................
.....v.........v.....
.....................
.....................
.....................
.....................
.....................
....................]"
constant maplayout

: plotmap
	0 -1 maplayout xyplot: pt
;

: campsite-light tint_default 1 light_mode!
	1 0.5 0 tint_near!
	0 0 1 tint_far!
;

: site-setup
	1 no_time!

	-1 1 frnd wind_force!

	' plotgrass to ft
	plotmap
	mission{ no-leaving }
	mission{ no-edge-spawns }
//	hour 17 < if 17 o'clock then
//	0 o'clock
//	1 gloom_lighting!
	-30 fade_to

	// NOTE: Don't let you just drive away! Or should we... (good way to abandon everyone)
//	mission.vehicle@ if setup-vehicle then
;

region: campsite
	w: maplayout layoutw
	h: maplayout layouth

	start-mode: 1

	post-script: site-setup

} }

//	: iconxy particle{ new pos! spriteid! 1 ticks! 0 layer! 0 global_colour! } ;
