# sleep

if $ARGS[0] = 'simple':
!! to avoid sleeping the whole day I decide to calcululate the duration of the sleep in advance
!! the quality of the sleep depends on many factors, so the time of sleep can be modify later, especially the part when the time of sleep is shortened

	varsleep['time_to_full'] = (100 - pcs_sleep) * 6
!! fully exhausted Sveta can recover in 420 minutes = 7 hours of sleep
!! healthy Sveta can hardly sleep more then 10 hours = 600 minutes
!! so we can add about 3 hours to time to full as a limit
	varsleep['time_to_full'] += 150 + rand(0,60)

	varsleep['time_now'] = daystart*1440 + hour * 60 + minut
	varsleep['alarm_time_end'] = daystart*1440 + timerEnd * 60 + timerEndM
	if varsleep['time_now'] >= varsleep['alarm_time_end']: varsleep['alarm_time_end'] += 1440
	varsleep['alarm_time'] = daystart*1440 + timer * 60 + timerM
	if varsleep['time_now'] >= varsleep['alarm_time']: varsleep['alarm_time'] += 1440
	if budilnikOn = 1: 
		varsleep['minutes to wakeup'] = min(varsleep['time_to_full'], varsleep['alarm_time'] - varsleep['time_now'])
		varsleep['minutes to wakeup end'] = min(varsleep['time_to_full'], varsleep['alarm_time_end'] - varsleep['time_now'])
	else 
		varsleep['minutes to wakeup'] = varsleep['time_to_full']
		varsleep['minutes to wakeup end'] = varsleep['time_to_full']
	end

	InSleep = 1
	:loopson_sleep
	minut += 1
	stime += 1
	if recuperation = 0:pcs_health += 5
	
	if stime >= 60:
		stime = 0
		pcs_sleep += 15
		gs 'stat'
	end
	
	varsleep['minutes to wakeup'] -= 1
	varsleep['minutes to wakeup end'] -= 1

	if alarm_holiday = 1 and kanikuli > 0:
	!! use weekend time alarm
		varsleep['alarm_time'] = daystart*1440 + timerEnd * 60 + timerEndM
		if varsleep['minutes to wakeup end'] > 0:jump 'loopson_sleep'
	elseif week > 5:
	!! use weeken time alarm 
		varsleep['alarm_time'] = daystart*1440 + timerEnd * 60 + timerEndM
		if varsleep['minutes to wakeup end'] > 0:jump 'loopson_sleep'
	else 
		varsleep['alarm_time'] = daystart*1440 + timer * 60 + timerM
		if varsleep['minutes to wakeup'] > 0:jump 'loopson_sleep'
	end
	varsleep['time_now'] = daystart*1440 + hour * 60 + minut
	InSleep = 0
	gs'stat'
end

--- sleep ---------------------------------
