// 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

: blocked tile@ TILE_BLOCK over .id! drop ;

: trashblock tile@ tbwreck ;
: trashs plotswreck ;
: pwreck tile@ tbwreck ;
: junkblock args( x y ) <- x <- y 1- <- x 1+ <- y ' pwreck boxcall ;

// : plotdirty 2dup plotdirt 0x a + floortop ;
: plotdirty 2dup plotdirt

	3 onein not if 2drop then;

	2dup 0x 1a floortop
	tile@ LAYER_FLOORTOP tile_layer.rndshifts
;

(* : removegrass tile@ dup LAYER_FLOOR .lspriteid@ street_tiles_base_id 0x 61 + .s cr <> if drop then;
	dup LAYER_FLOORTOP 0 .lspriteid!
//	dup LAYER_WALLSIDE 0 .lspriteid!
; *)

: grasstile? dup street_tiles_base_id 6 + > swap street_tiles_base_id 12 + < and ;

: removegrassedges
	LAYER_FLOORTOP 0 .lspriteid!
;

: removegrass tile@ dup LAYER_FLOOR .lspriteid@ grasstile? not if drop then; removegrassedges ;

: fenceup args( x y s )

	0x f5 <- s + -> s

	3 for
		<- x <- y <- s i 1- 0x 10 * + walltop 
		<- x <- y tile@ LAYER_WALLTOP 0 0 .tile_layer.off!
		<- x <- y tile@ LAYER_WALLTOP 0 .tile_layer.flipx!
		<- x <- y blocked
		<- y 1- -> y
	next
;

: hereup w 0 1 frnd * xyp.y 0 1 frnd * tile>pos ;

: mishmash hereup args( x y )
	rnd5050 if
		shoppingcart <- x <- y lt .pos!
		0 lt .nocollide! drift
		0 1 rnd 180 * lt .angle!
		lt .rusty
	then;

	1 4 rnd for cinderblock <- x <- y lt .pos! drift next

	// TODO: Other throwabale junk
;

: tires hereup args( x y )
	rnd5050 if bigtire <- x <- y lt .pos! drift then;

	1 7 rnd for
		tire <- x <- y lt .pos! drift
	next
;

: weapons hereup args( x y )
	rnd5050 if  <- x <- y rebar .pos! then;
	rnd5050 if  <- x <- y metal .pos! then;
	rnd5050 if  <- x <- y wrench lt .pos! then; // Wrench doesn't push a THING?
	 <- x <- y crowbar .pos!
;

: shiftin // Push wrecks away from sides
	lt .pos.x@ 50 < if lt .pos.x@ 50 + lt .pos.x! then;
	lt .pos.x@ pixelw 50 - > if lt .pos.x@ 50 - lt .pos.x! then;
;

: docarwreck hereup carwreck lt .pos! drift 0 1 rnd 180 * lt .angle! shiftin TILEFLAG_NO_COLLIDE lt .tileflags!
	lt .angle@ if lt .sprh 4 / 16 - lt .anim.off.y! then
	// TODO: ADD LOOT, or zombie spawn from car upon search
;

: dotirestack xyp.xy 1- tile>pos -4 4 rnd + swap -4 4 rnd + swap tirestack lt .pos! ;

: junkdawg new-npc lt .character@ .doggify hereup lt .pos!
	// TODO: Junkdawg CYOA?
;


: bldg tanchor! map{ "junkyardoffice" $picklocid addloc prefab{ 1 9 1 shop } } ;


' plotdirty >defer ft

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

	xyp.i char { = if xyp.xy to bfy to bfx then;
	xyp.i char } = if bfx 1- bfy 1- xyp.x 1+ xyp.y 1+ pavedlot then;
	xyp.i char D = if junkdawg then;
	xyp.i char w = if docarwreck then;
	xyp.i char s = if 1 2 rnd for mishmash next then;
	xyp.i char W = if 1 2 rnd for weapons next then;
	xyp.i char < = if xyp.xy to gby to gbx then;
	xyp.i char > = if gbx 1- gby xyp.xy swap 1+ swap grasslot then;
	xyp.i char ( = if xyp.xy to gby2 to gbx2 then;
	xyp.i char ) = if gbx2 1- gby2 xyp.xy swap 1+ swap grasslot then;
	xyp.i char T = if xyp.xy trashblock then;
	xyp.i char J = if rnd5050 if xyp.xy junkblock then then;
	xyp.i char t = if 3 for tires next then;
	xyp.i char S = if dotirestack then;
	xyp.i char @ = if xyp.xy bldg then;
	xyp.i char z = if carwreck xyp.xy tile>pos lt .pos! drift then;
	xyp.i char G = if dumpster xyp.xy tile>pos lt .pos! then;

	xyp.i char 5 = if xyp.xy 0 fenceup then;
	xyp.i char 6 = if xyp.xy 1 xyp.x 1 & + fenceup then;
	xyp.i char 7 = if xyp.xy 3 fenceup then;

;

"
TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT
TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT
TTTTJ.J.J.J.J.J.J.J.J.J.J.J.J.J.J.J.J.J.J.JJTTTT
TTTJ...............J.J.J.J.J................JTTT
TTJ..........................................JTT
TT..................J.J.J.J...................TT
TTJ.J.J.J.J...........J.J...........J.J.J.J.J.TT
TT............................................TT
TTJ.J.J.................................J.J.J.TT
TT..............J.J.........J.J...............TT
T.J.........................................J.JT
T...............J.J.........J.J................T
T........J.J..........J.J........J.J..........JT
T..............................................T
T........J.J..........J.J........J.J..........JT
T..............................................T
T.J.........................................JDJT
T........................tttssswwwwwwwwwwwwwwwwT
T.J.J.J.J.J.......J.J.J.J.J.J.J.J.J.J.J.J.J.J.JT
T.........................................WWWW.T
T.J.J.J.J.J.......J.J.J.J.J.J.J.J.J.J.J.J.J.J.JT
T..............................................T
6666666666667.....566666666666666666666666666666
....................TTTTTTTTTT...............z..
<..................(S..........SSSS..z..........
..........SS.......S............SS..............
...........S.......S....................z..z....
...........S.......S...............z............
...........S.......S............GG.....z....z...
...........S.......S............................
...........S.......S.@.........)................
...........>....................................
...........S....................................
................................................
................................................
...............................................W"
constant maplayout

: plotmap
	0 -1 maplayout xyplot: pt
;


: site-setup
	' plotdirty to ft plotmap
	map-extents ' removegrass boxcall
	street-junk
;

region: junkyard
	w: maplayout layoutw
	h: maplayout layouth 1-

	loc-choice: junkyardoffice
		loc-weight: 1
		loc-count: 1

	post-script: site-setup

} }
