/*

DO NOT EDIT THIS FILE!

This is the basic time controller. It will show you how the game will display the time in your map.
This can be useful in case you forget to make your interior areas with the interior area type!

If you want to set the time in the editor test, just use the set-time verb.

NOTE: THE NEW LIGHTING SYSTEM IS NOT FUNCTIONAL IN THIS VERSION OF THE MAPPING ENVIRONMENT.
DO NOT WASTE YOUR TIME PAINTING AREAS FOR LIGHTING.
THE STREET_LAMP TIME OBJS WILL AUTOMATICALLY CREATE A LIGHTING CIRCLE AROUND THEM IN SL2.
THEREFORE YOU DON'T HAVE TO WORRY ABOUT PAINTING TIME AREAS AT ALL.
THE NEW SYSTEM MAY BE IMPLEMENTED IN THE MAPPING ENVIRONMENT IN THE FUTURE.

*/

var
	const
		MAX_HOUR=23
		MAX_DAY=30
		MAX_MONTH=12
		HOUR_DUR=4500 // 7.5 minutes.

	time/time
time
	var
		month=1
		day=1
		year=300
		hour=1
		keeping=0
	proc
		fix()
			if(!hour) hour=1
			if(!month) month=1
			if(!year) year=300
			if(!day) day=1
		keep_time()
			set background = 1
			if(keeping==1) return
			keeping=1
			while(src)
				on_new_hour()
				sleep(HOUR_DUR)
				hour++
				if(hour>MAX_HOUR) // New Day
					hour=1
					day++
				if(day>MAX_DAY)
					month++
					day=1
				if(month>MAX_MONTH)
					month=1
					year++

mob
	var
		see_all=0
	verb
		set_time(whats as num)
			time.hour=whats
			on_new_hour()
		set_z(whats as num)
			usr.z=whats
			gen_aftermove(usr)
		toggle_dense()
			density=!density
			usr << "You are now [density==1 ? "dense" : "not dense"]."
		toggle_see_everything()
			if(usr.see_all==0)
				usr.sight|=(SEE_MOBS|SEE_TURFS|SEE_OBJS)
			else
				usr.sight=0
			usr.see_all=!usr.see_all
		polymorph()
			if(usr.icon=='prinny.dmi')
				usr.icon='base male normal 2.dmi'
			else
				usr.icon='prinny.dmi'

var
	init_timeover=0
	list
		hour_states=list("damnitsdark"=list(0,1,2,23,24),"darker"=list(20,21,22,3,4,5),"night"=list(18,19),"afternoon"=list(6,7,8,9,16,17))
		hour_colors=list("#000033"=list(0,1,2,23,24,20,21,22,3,4,5),"#000066"=list(18,19),"#CC3333"=list(6,7,8,9,16,17))


proc
	on_new_hour()
		TimeOverlays()
		if(time.hour==6)
			world << "<b><font color=yellow>Day Breaks...</b>"
		if(time.hour==18)
			world << "<b><font color=yellow>Night Sets In...</b>"

	is_night()
		.=0
		if(time.hour>=18) .=1
		if(time.hour<6) .=1
	TimeOverlays()
		var/list/time_alphas=list("0"=220,"1"=190,"2"=180,"3"=165,"4"=135,"5"=95,"6"=45,"7"=65,"8"=55,"9"=25,\
"16"=65,"17"=45,"18"=75,"19"=150,"20"=185,"21"=195,"22"=205,"23"=210)
		var/timez=time.hour

		var/new_color
		var/new_alpha=0
		if(("[timez]" in time_alphas))
			new_alpha=time_alphas["[timez]"]

		for(var/X in hour_colors)
			if((timez in hour_colors[X]))
				new_color=X
		for(var/area/O in world)
			if(O.should_light(timez))
				O.icon='shading.dmi'
				var/lstate=O.light_state(timez)
				if(!lstate)
//					O.icon_state=state
					O.icon_state="white"
					O.blend_mode=4

					var/delays=1200
					if(init_timeover==0)
						delays=0
					animate(O,alpha=new_alpha+O.alpha_mod(),color=new_color,time=delays)
				else
					O.icon_state=lstate
			else
				O.icon=initial(O.icon)
				O.icon_state=initial(O.icon_state)
		init_timeover=1
		for(var/obj/time_obj/O in world)
			O.update(timez)

// Time objects change their appearance based on the current time.hour

obj
	time_obj
		proc
			update(times=1)
		street_lamp
			layer=14
			icon_state="8,3"
			icon='tilec.dmi'
			update(times=1)
				if(time.hour in list(0,1,2,23,24,20,21,22,3,4,18,19))
					icon_state="9,3"
				else
					icon_state="8,3"
		street_lamp_chat
			layer=14
			icon_state="lamp off"
			icon='chaturanga supplement.dmi'
			update(times=1)
				if(time.hour in list(0,1,2,23,24,20,21,22,3,4,18,19))
					icon_state="lamp on"
				else
					icon_state="lamp off"