/*

DO NOT EDIT THIS FILE!

*/

area
	var
		region

	region
		cells
			region=R_CELLSVICH
		dormeho
			region=R_DORMEHO
		tannis
			region=R_TANNIS
		oniga
			region=R_ONIGA
		wilderness

		law_end
			region=R_LAW_END

		shrine
			region=R_SHRINE

		chaturanga
			region=R_CHATURANGA

		lispool
			region=R_LISPOOL

		darkwood
			region=R_DARKWOOD

		alstalsia
			region=R_ALSTALSIA

mob
	var
		tmp
			last_region
	verb
		set_weather()
			var/reg=input("Select the region that will be affected.","Set Weather")as null|anything in regions
			if(reg)
				var/wea=input("Select the weather type that will be started.","Set Weather")as null|anything in list(WE_NONE,WE_RAIN,WE_SNOW,WE_SANDSTORM)
				if(wea)
					do_region_weather(reg,wea)


/*

REGION MARKERS

These mark areas which are considered in a specific region. In the same region, weather effects and such will be played.

IMPORTANT: Place this in the top right your map. Some functions may depend on this placement.

For Interior Areas: Make sure weather is set to 0. You might want to also set weather_vol to 25 to give an indoors effect.
For Exterior Areas: You don't need to do anything special.


*/

obj
	world_obj
		region_marker
			icon='weather.dmi'
			icon_state="region"
			var
				region
				weather=1
				weather_vol=40
			cells
				region=R_CELLSVICH
			dormeho
				region=R_DORMEHO
			tannis
				region=R_TANNIS
			oniga
				region=R_ONIGA
			law_end
				region=R_LAW_END
			shrine
				region=R_SHRINE
			chaturanga
				region=R_CHATURANGA
			lispool
				region=R_LISPOOL
			darkwood
				region=R_DARKWOOD
			alstalsia
				region=R_ALSTALSIA


var
	const
		R_CELLSVICH="Cellsvich"
		R_DORMEHO="Dormeho"
		R_TANNIS="Tannis"
		R_ONIGA="Oniga"
		R_LAW_END="Law's End"
		R_SHRINE="Nameless Shrine"
		R_CHATURANGA="Chaturanga"
		R_LISPOOL="Lispool"
		R_DARKWOOD="Darkwood Village"
		R_ALSTALSIA="Alstalsia"

		RD_CELL=/region/cellsvich
		RD_DORM=/region/dormeho
		RD_TANN=/region/tannis
		RD_ONIG=/region/oniga
		RD_LAWEND=/region/law_end
		RD_SHRINE=/region/shrine
		RD_CHATURANGA=/region/chaturanga
		RD_LISPOOL=/region/lispool
		RD_DARKWOOD=/region/darkwood
		RD_ALSTALSIA=/region/alstalsia

		WEATHER_CHANNEL=64

proc
	get_weather_sound(wtype)
//		world << wtype
		if(istype(wtype,WE_RAIN))
//			world << "tis' raining"
			.=sound('bg_rain.ogg',1,0,WEATHER_CHANNEL,40)
		else if(istype(wtype,WE_SANDSTORM))
			.=sound('sandstorm.ogg',1,0,WEATHER_CHANNEL,40)
		else
			.=sound(null,0,0,WEATHER_CHANNEL)
	reg_weatherse(mob/combat/user)
//		world << "Setting eyes"
		if(!user) return
		if(ismob(user))
			if(!user.client) return
		var/list/whats=list()
		if(ismob(user))
			whats+=user

		for(var/mob/M in whats)
			var/sets=0
			if(M.set_se==0)
				var/sound/sounds=sound(null,0,0,WEATHER_CHANNEL)
				M << sounds
			else
				for(var/obj/world_obj/region_marker/Z in world)
					if(Z.z==M.z)
						var/sound/sounds=get_weather_sound(get_region_weather(Z.region))
						sounds.volume=Z.weather_vol
						M << sounds
						sets=1
						break
				if(sets==0)
					var/sound/sounds=sound(null,0,0,WEATHER_CHANNEL)
					M << sounds

	reg_weatherscreen(mob/combat/user)
		if(!user) return
		if(weather_mode=="screens")

			var/obj/world_obj/region_marker/R

			var/list/whats=list()
			if(ismob(user))
				whats+=user

			for(var/obj/world_obj/region_marker/Z in world)
				if(Z.z==user.z)
					R=Z
					break



			var/weather/W
			if(R)
				W=get_region_weather(R.region)
			else
				W=new/weather/none
			for(var/mob/C in whats)
//				C << "Region weather [R ? "for [R.region]" :""] is [W] ([W ? "[W.type]" : "null"])"
				if(!C.client) continue
				var/obj/weather_obj/WO=locate() in C.client.screen
				if(R)
					C.last_region=R.region
				if(!R||R.weather==0||istype(W,/weather/none)||isnull(W))
//					C << "Herp"
					C.client.screen-=WO
				else if(W)
					if(!WO)
						WO=new
						C.client.screen+=WO
					WO.icon=W.icon
					WO.icon_state=W.istate


region
	var
		list
			weathers=list(new WE_RAIN=5) // Weather type = Probability
		name="Cellsvich"


	proc
		weather(hours=1)
			var/weather/z=get_region_weather(name)
			if(z)
				z.dur--
				if(z.dur<=0)

					z.end(name)

					reset_region_weather_list(name)
					del(z)

					reset_weather_ses(name)
					reset_weather_screens(name)
			if(!z)
				for(var/X in weathers)
					if(prob(weathers[X]))

						do_region_weather(name,X:type)
						break
	cellsvich
		weathers=list(new WE_RAIN=2)

	dormeho
		name="Dormeho"

	tannis
		name="Tannis"

	oniga
		name="Oniga"
		weathers=list(new WE_RAIN=2)

	law_end
		name="Law's End"
		weathers=list(new WE_SANDSTORM=10)

	shrine
		name="Nameless Shrine"
		weathers=list(new WE_RAIN=2)

	chaturanga
		name="Chaturanga"
		weathers=list(new WE_RAIN=2)

	lispool
		name="Lispool"
		weathers=list(new WE_SNOW=50)

	darkwood
		name="Darkwood Village"
		weathers=list(new WE_RAIN=2)

	alstalsia
		name="Alstalsia"
		weathers=list(new WE_RAIN=2)


var
	list
		regions=list(new RD_CELL,new RD_DORM,new RD_TANN,new RD_ONIG,new RD_LAWEND,new RD_SHRINE,new RD_CHATURANGA,new RD_LISPOOL)
		regions_turfs=list()

proc
	is_in_region(mob/combat/user,regionzz)
		.=0
		var/obj/world_obj/region_marker/R
		for(var/obj/world_obj/region_marker/Z in world)
			if(Z.z==user.z)
//				user << "[regionzz] and [Z.region]"
				R=Z
				break
		if(R)

			if("[R.region]"=="[regionzz]") return 1
	reset_weather_ses(regionz)
		set background = 1
		for(var/mob/Y in world)
//			for(var/obj/world_obj/region_marker/Z in world)
//				if(Z.z==Y.z)
			if(regionz)
				if(is_in_region(Y,regionz))
//						if(Y.last_region!=regionz)
//							continue
					reg_weatherse(Y)
	reset_weather_screens(regionz)
		set background = 1
		for(var/mob/Y in world)
//			for(var/obj/world_obj/region_marker/Z in world)
//				if(Z.z==Y.z)
			if(Y.client)
				if(regionz)
//						if(Y.last_region!=regionz)
//							continue
					if(is_in_region(Y,regionz))
//						Y << "Updating you since you're in [regionz]."
						reg_weatherscreen(Y)
					else
//						Y << "You weren't in the region of [regionz] apparently."
	get_region(regionz,weather=1)
		set background = 1
		if(!(regionz in regions_turfs))
			regions_turfs+=regionz
			regions_turfs[regionz]=list()
			var/list/turfs=list()
			for(var/area/Z in world)
				if(Z.region==regionz)
					for(var/turf/T in Z.contents)
						if(weather_mode=="screens"&&T.z!=7) continue
						turfs+=T
			if(weather_mode!="screens")
				for(var/obj/world_obj/region_marker/Z in world)
					if(Z.region==regionz)
						if(weather==1&&Z.weather==0) continue
		//				for(var/turf/Y in block(locate(1,1,Z.z),locate(world.maxx,world.maxy,Z.z)))
						for(var/turf/Y in block(locate(1,1,Z.z),locate(Z.x,Z.y,Z.z)))
							turfs+=Y
		//				for(var/turf/Y in world)
		//					if(Y.z==Z.z)
	//						turfs+=Y
			regions_turfs[regionz]=turfs
		return regions_turfs[regionz]

	get_region_bigmap(regionz)
		set background = 1
		var/list/turfs=list()
		for(var/obj/world_obj/region_marker/Z in world)
			if(Z.region==regionz)
				for(var/turf/Y in block(locate(1,1,Z.z),locate(Z.x,Z.y,Z.z)))
					turfs+=Y
		return turfs

	get_region_weather(regionz)
		for(var/region/Z in regions)
			if("[Z.name]"=="[regionz]")
				.=regions[Z]

	do_region_weather(regionss,weather)
		var/weather/Z=get_region_weather(regionss)
		if(Z)
			Z.end(regionss)
		del(Z)
		Z=new weather
		for(var/region/ZZ in regions)
			if("[ZZ.name]"=="[regionss]")
//				world << "Found [ZZ.name]. Setting."
				regions[ZZ]=Z
		Z.start(regionss)
		reset_weather_ses(regionss)
		reset_weather_screens(regionss)

	reset_region_weather_list(regionz)
		for(var/region/Z in regions)
			if("[Z.name]"=="[regionz]")
				regions[Z]=null
var
	weather_mode="screens"
	const
		WE_NONE=/weather/none
		WE_RAIN=/weather/rain
		WE_SNOW=/weather/snow
		WE_SANDSTORM=/weather/sandstorm

obj
	weather_obj
		layer=HIGH_TURF_LAYER+1
		screen_loc="SOUTH,WEST to NORTH,EAST"
		mouse_opacity=0


weather
	var
		icon // The icon overlay for this type of weather.
		istate // The icon state.
		obj/olay

		dur=3 // The duration in hours. -1 for permanent.
		list
			temps=list()

	New()
		setup()
		..()

	proc
		setup()
			olay=new
			olay.icon=icon(icon,istate)
			olay.layer=HIGH_TURF_LAYER+1 // One below the layer for weather effects.
		apply(turf/T)
			if(icon)
				if(weather_mode=="objs"||weather_mode=="screens")
					var/obj/weather_obj/Z=new(T)
					Z.icon=icon
					Z.icon_state=istate
					temps+=Z
				else if(weather_mode=="overlays")
					T.overlays+=olay

		unapply(turf/T)
			if(weather_mode=="overlays")
				T.overlays-=olay

		start(regions)
			if(istype(src,/weather/none)) return
			if(weather_mode=="objs"||weather_mode=="screens")
				var/list/whats=get_region(regions)
				for(var/turf/Z in whats)
					apply(Z)
		end(regions)
//			var/list/whats=get_region(regions)
			if(weather_mode=="objs"||weather_mode=="screens")
				for(var/obj/Z in temps)
					temps-=Z
					Z.loc=null
				//Z.loc=null
//				del(Z)
			else if(weather_mode=="overlays")
				var/list/whats=get_region(regions)
				for(var/turf/Z in whats)
					unapply(Z)
	none
	rain
		icon='weather.dmi'
		istate="rain"
	sandstorm
		icon='sandstorm.dmi'
	snow
		icon='weather.dmi'
		istate="snow"