# uniutil

if $ARGS[0] = 'eligible_for_university':
	RESULT = func('uniutil', 'student', 'status') = 0 and func('uniutil', 'diploma', 'obtained') = 0 and (age >= 17 or fakepassport = 1)
end

if $ARGS[0] = 'prepclass':
	if $ARGS[1] = 'cost':
		RESULT = 15000
	end

	if $ARGS[1] = 'closing_hour':
		RESULT = 21
	end

	if $ARGS[1] = 'is_enrolled':
		RESULT = prepclassstats['enrolled'] = 1
	end

	if $ARGS[1] = 'set_enrolled':
		prepclassstats['enrolled'] = ARGS[2]
	end

	if $ARGS[1] = 'count':
		RESULT = prepclassstats['count']
	end

	if $ARGS[1] = 'set_count':
		prepclassstats['count'] = ARGS[2]
	end

	if $ARGS[1] = 'update_count':
		gs 'uniutil', 'prepclass', 'set_count', func('uniutil', 'prepclass', 'count') + 1
	end

	if $ARGS[1] = 'enroll':
		money -= func('uniutil', 'prepclass', 'cost')
		gs 'uniutil', 'prepclass', 'set_enrolled', 1
		gs 'uniutil', 'prepclass', 'set_count', 0
	end

	if $ARGS[1] = 'unenroll':
		killvar 'prepclassstats'
	end

	if $ARGS[1] = 'offered_this_month':
		RESULT = month <= func('uniutil', 'entrance_exam', 'month')
	end

	if $ARGS[1] = 'offered_today':
		RESULT = func('uniutil', 'entrance_exam', 'offered_this_month') and week < 6
	end

	if $ARGS[1] = 'in_session':
		RESULT = func('uniutil', 'prepclass', 'offered_today') and hour < func('uniutil', 'prepclass', 'closing_hour')
	end
end

if $ARGS[0] = 'entrance_exam':
	if $ARGS[1] = 'month':
		RESULT = 8
	end

	if $ARGS[1] = 'offered_today':
		RESULT = month = func('uniutil', 'entrance_exam', 'month')
	end

	if $ARGS[1] = 'is_passed':
		RESULT = pcs_intel + func('uniutil', 'prepclass', 'count') >= 60
	end

	if $ARGS[1] = 'print_month':
		$RESULT = $monthName[func('uniutil', 'entrance_exam', 'month')]
	end
end

!! The legacy vars are the vars used by the university before the vars were largely consolidated into
!! arrays with string indices. This function exists to avoid breaking old save files with this change,
!! which was made in September 2020. It can be removed whenever it is felt enough time has passed.
if $ARGS[0] = 'transfer_legacy_vars':
	if abiturient > 0:
		gs 'uniutil', 'prepclass', 'set_enrolled', 1
		gs 'uniutil', 'prepclass', 'set_count', abiturient - 1
	end

	killvar 'abiturient'

	if kurs > 0:
		if (semestr = 1 and month < func('uniutil', 'entrance_exam', 'month')) or (semestr = 2 and month > func('uniutil', 'exam', 'month', 2)):
			!! under the old system, the semestr variable was advanced when the exam was passed, so if the
			!! variable is set to a value that does not match the current month, the player skipped the exam
			!! and is expelled
			gs 'uniutil', 'student', 'set_status', -2
			killvar 'unisemestrstats'
		elseif semestr = 2 and month = func('uniutil', 'exam', 'month', 1):
			!! semestr = 2 in first semester exam month in the old system indicates that its still the first
			!! semester (time-wise) but the exam has been passed, so we set the new variables accordingly.
			gs 'uniutil', 'semester', 'set', 1
			gs 'uniutil', 'attendance', 'reset'
			gs 'uniutil', 'assignment_progress', 'reset'
			gs 'uniutil', 'assignments_done', 'reset'
			gs 'uniutil', 'semester_result', 'set_passed'
			gs 'uniutil', 'passed_semesters', 'set', 0
		else
			gs 'uniutil', 'semester', 'set', semestr
			gs 'uniutil', 'attendance', 'set', lektor
			gs 'uniutil', 'assignment_progress', 'set', kursovik
			gs 'uniutil', 'assignments_done', 'set', kursovikD
			gs 'uniutil', 'semester_result', 'reset'
			gs 'uniutil', 'passed_semesters', 'set', iif(semestr = 2, 1, 0)
		end

		gs 'uniutil', 'semester', 'update_year'
		gs 'uniutil', 'scholarship', 'set', iif(stipuha = 1, func('uniutil', 'scholarship', 'honors_value'), 0)
	end

	killvar 'kurs'
	killvar 'semestr'
	killvar 'stipuha'
	killvar 'lektor'
	killvar 'kursovik'
	killvar 'kursovikD'
	killvar 'examen'
end

if $ARGS[0] = 'check_semester_vs_date':
	if year > func('uniutil', 'semester', 'get_year') + 1:
		!! if year has advanced by more than one since a semester was finished, semesters were skipped.
		RESULT = -1
		exit
	end

	unilect_sm = func('uniutil', 'lecture', 'start_month', func('uniutil', 'semester', 'get'))
	uniexam_m = func('uniutil', 'exam', 'month', func('uniutil', 'semester', 'get'))
	uniexam_mns = func('uniutil', 'exam', 'month', func('uniutil','semester', 'next'))

	if year = func('uniutil', 'semester', 'get_year') + 1:
		if month > uniexam_m mod 12 and func('uniutil', 'semester_passed') and month <= uniexam_mns:
			!! the semester was passed and the next one is not over
			RESULT = 1
		elseif month <= uniexam_m:
			!! the semester is still in progress
			RESULT = 0
		else
			!! expelled
			RESULT = -1
		end
	else
		if unilect_sm > uniexam_m or month <= uniexam_m:
			!! The semester is still in progress
			RESULT = 0
		elseif func('uniutil', 'semester_result', 'is_passed') and (uniexam_m > uniexam_mns or month <= uniexam_mns):
			!! the semester was passed and the next one is not over
			RESULT = 1
		else
			!! expelled
			RESULT = -1
		end
	end

	killvar 'unilect_sm'
	killvar 'uniexam_m'
	killvar 'uniexam_mns'
end

if $ARGS[0] = 'student':
	if $ARGS[1] = 'status':
		if student > 0:
			uni_semvdateres = func('uniutil', 'check_semester_vs_date')

			if uni_semvdateres < 0:
				!! date indicates semester exam skipped
				gs 'uniutil', 'student', 'expel'
			elseif uni_semvdateres > 0:
				!! date indicates semester exam passed and next semester started
				gs 'uniutil', 'semester', 'update'
				gs 'uniutil', 'semester', 'update_year'
				gs 'uniutil', 'semester_result', 'reset'
				gs 'uniutil', 'passed_semesters', 'update'
			else
				!! date indicates semester is still in progress
				gs 'uniutil', 'semester', 'update_year'
			end

			killvar 'uni_semvdateres'
		end

		RESULT = student
	end

	if $ARGS[1] = 'set_status':
		student = ARGS[2]
	end

	if $ARGS[1] = 'enroll':
		gs 'uniutil', 'prepclass', 'unenroll'
		gs 'uniutil', 'student', 'set_status', 1
		gs 'uniutil', 'semester', 'set', 1
		gs 'uniutil', 'semester', 'update_year'
		gs 'uniutil', 'scholarship', 'reset'
		gs 'uniutil', 'attendance', 'reset'
		gs 'uniutil', 'assignment_progress', 'reset'
		gs 'uniutil', 'assignments_done', 'reset'
		gs 'uniutil', 'semester_result', 'reset'
	end

	if $ARGS[1] = 'unenroll':
		gs 'uniutil', 'prepclass', 'unenroll'
		if student > 0:
			killvar 'student'
			killvar 'unisemestrstats'
		end
	end

	if $ARGS[1] = 'expel':
		if func('uniutil', 'semester_result', 'is_failed'):
			gs 'uniutil', 'student', 'unenroll'
			gs 'uniutil', 'student', 'set_status', -1
		elseif func('uniutil', 'check_semester_vs_date') < 0:
			gs 'uniutil', 'student', 'unenroll'
			gs 'uniutil', 'student', 'set_status', -2
		else
			gs 'uniutil', 'student', 'unenroll'
			gs 'uniutil', 'student', 'set_status', -3
		end
	end

	if $ARGS[1] = 'graduate':
		gs 'uniutil', 'student', 'unenroll'
		gs 'uniutil', 'diploma', 'set_obtained'
	end

	if $ARGS[1] = 'expelled_for_failing':
		RESULT = func('uniutil', 'student', 'status') = -1
	end

	if $ARGS[1] = 'expelled_for_skipping':
		RESULT = func('uniutil', 'student', 'status') = -2
	end

	if $ARGS[1] = 'expelled_for_other_reason':
		RESULT = func('uniutil', 'student', 'status') = -3
	end

	if $ARGS[1] = 'enrolled':
		RESULT = func('uniutil', 'student', 'status') > 0
	end

	if $ARGS[1] = 'not_enrolled':
		RESULT = func('uniutil', 'student', 'enrolled') = 0
	end

	if $ARGS[1] = 'semesters_to_graduate':
		RESULT = 2
	end
end

if $ARGS[0] = 'diploma':
	if $ARGS[1] = 'get':
		RESULT = diplom
	end

	if $ARGS[1] = 'set':
		diplom = ARGS[2]
	end

	if $ARGS[1] = 'set_obtained':
		gs 'uniutil', 'diploma', 'set', 1
		if func('uniutil', 'fake_diploma', 'aware_needed'):
			gs 'uniutil', 'fake_diploma', 'reset'
		end
	end

	if $ARGS[1] = 'obtained':
		RESULT = func('uniutil', 'diploma', 'get') > 0
	end
end

if $ARGS[0] = 'fake_diploma':
	if $ARGS[1] = 'get':
		RESULT = fakediplom
	end

	if $ARGS[1] = 'set':
		fakediplom = ARGS[2]
	end

	if $ARGS[1] = 'set_aware_needed':
		gs 'uniutil', 'fake_diploma', 'set', 2
	end

	if $ARGS[1] = 'set_obtained':
		gs 'uniutil', 'fake_diploma', 'set', 1
	end

	if $ARGS[1] = 'reset':
		gs 'uniutil', 'fake_diploma', 'set', 0
	end

	if $ARGS[1] = 'aware_needed':
		RESULT = func('uniutil', 'fake_diploma', 'get') = 2
	end

	if $ARGS[1] = 'obtained':
		RESULT = func('uniutil', 'fake_diploma', 'get') = 1
	end
end

if $ARGS[0] = 'appear_to_have_graduated':
	RESULT = func('uniutil', 'diploma', 'obtained') or func('uniutil', 'fake_diploma', 'obtained')
end

if $ARGS[0] = 'attendance':
	if $ARGS[1] = 'get':
		RESULT = unisemestrstats['lectures_attendad']
	end

	if $ARGS[1] = 'set':
		unisemestrstats['lectures_attendad'] = ARGS[2]
	end

	if $ARGS[1] = 'update':
		gs 'uniutil', 'attendance', 'set', func('uniutil', 'attendance', 'get') + 1
	end

	if $ARGS[1] = 'reset':
		gs 'uniutil', 'attendance', 'set', 0
	end

	if $ARGS[1] = 'is_great':
		RESULT = func('uniutil', 'attendance', 'get') > 55
	end

	if $ARGS[1] = 'is_ok':
		RESULT = func('uniutil', 'attendance', 'get') > 40
	end

	if $ARGS[1] = 'is_bad':
		RESULT = func('uniutil', 'attendance', 'is_ok') = 0
	end
end

if $ARGS[0] = 'assignment_progress':
	if $ARGS[1] = 'get':
		RESULT = unisemestrstats['assignment_progress']
	end

	if $ARGS[1] = 'set':
		unisemestrstats['assignment_progress'] = iif(ARGS[2] <= 100, ARGS[2], 100)
	end

	if $ARGS[1] = 'update':
		gs 'uniutil', 'assignment_progress', 'set', func('uniutil', 'assignment_progress', 'get') + rand(pcs_intel / 20, pcs_intel / 10)
	end

	if $ARGS[1] = 'reset':
		gs 'uniutil', 'assignment_progress', 'set', 0
	end

	if $ARGS[1] = 'is_finished':
		RESULT = func('uniutil', 'assignment_progress', 'get') >= 100
	end

	if $ARGS[1] = 'is_not_finished':
		RESULT = func('uniutil', 'assignment_progress', 'is_finished') = 0
	end
end

if $ARGS[0] = 'assignments_done':
	if $ARGS[1] = 'get':
		RESULT = unisemestrstats['assignments_done']
	end

	if $ARGS[1] = 'set':
		unisemestrstats['assignments_done'] = ARGS[2]
	end

	if $ARGS[1] = 'update':
		gs 'uniutil', 'assignment_progress', 'reset'
		gs 'uniutil', 'assignments_done', 'set', func('uniutil', 'assignments_done', 'get') + 1
	end

	if $ARGS[1] = 'reset':
		gs 'uniutil', 'assignments_done', 'set', 0
	end

	if $ARGS[1] = 'great_num':
		RESULT = 11
	end

	if $ARGS[1] = 'min_num':
		RESULT = 1
	end

	if $ARGS[1] = 'did_great_num':
		RESULT = func('uniutil', 'assignments_done', 'get') >= func('uniutil', 'assignments_done', 'great_num')
	end

	if $ARGS[1] = 'did_min_num':
		RESULT = func('uniutil', 'assignments_done', 'get') >= func('uniutil', 'assignments_done', 'min_num')
	end
end

if $ARGS[0] = 'scholarship':
	if $ARGS[1] = 'get':
		RESULT = unisemestrstats['scholarship']
	end

	if $ARGS[1] = 'set':
		unisemestrstats['scholarship'] = ARGS[2]
	end

	if $ARGS[1] = 'reset':
		gs 'uniutil', 'scholarship', 'set', 0
	end

	if $ARGS[1] = 'is_active':
		RESULT = unisemestrstats['scholarship'] > 0 and func('uniutil', 'semester', 'is_active')
	end

	if $ARGS[1] = 'honors_value':
		RESULT = 1200
	end
end

if $ARGS[0] = 'lecture':
	if $ARGS[1] = 'start_hour':
		RESULT = 8
	end

	if $ARGS[1] = 'length':
		RESULT = 6
	end

	if $ARGS[1] = 'start_month':
		if ARGS[2] = 1:
			RESULT = 9
		elseif ARGS[2] = 2:
			RESULT = 2
		else
			!! Set to impossible month if it is not semester 1 or 2
			RESULT = 13
		end
	end

	if $ARGS[1] = 'end_month':
		if ARGS[2] = 1:
			RESULT = 11
		elseif ARGS[2] = 2:
			RESULT = 4
		else
			!! Set to impossible month if it is not semester 1 or 2
			RESULT = 0
		end
	end

	if $ARGS[1] = 'offered_this_month':
		unilect_sm = func('uniutil', 'lecture', 'start_month', func('uniutil', 'semester', 'get'))
		unilect_em = func('uniutil', 'lecture', 'end_month', func('uniutil', 'semester', 'get'))
		if unilect_sm <= unilect_em:
			!! if semester is in one calendar year
			RESULT = month >= unilect_sm and month <= unilect_em
		else
			!! if semester continues through New Years
			RESULT = month >= unilect_sm or month <= unilect_em
		end
		killvar 'unilect_sm'
		killvar 'unilect_em'
	end

	if $ARGS[1] = 'offered_today':
		RESULT = func('uniutil', 'lecture', 'offered_this_month') and week < 6
	end

	if $ARGS[1] = 'in_session':
		RESULT = func('uniutil', 'lecture', 'offered_today') and hour < func('uniutil', 'lecture', 'start_hour')
	end

	if $ARGS[1] = 'print_start_month':
		$RESULT = $monthName[func('uniutil', 'lecture', 'start_month', ARGS[2])]
	end

	if $ARGS[1] = 'print_months':
		unilect_idx = func('uniutil', 'lecture', 'start_month', ARGS[2])
		unilect_end = func('uniutil', 'lecture', 'end_month', ARGS[2])

		:lectureprintmonthsloop
		$RESULT += $monthname[unilect_idx]
		if unilect_idx ! unilect_end:
			$RESULT += ', '
			unilect_idx = (unilect_idx mod 12) + 1
			jump 'lectureprintmonthsloop'
		end

		killvar 'unilect_idx'
		killvar 'unilect_end'
	end
end

if $ARGS[0] = 'exam':
	if $ARGS[1] = 'month':
		if ARGS[2] = 1:
			RESULT = 12
		elseif ARGS[2] = 2:
			RESULT = 5
		else
			!! Set to impossible month if it is not semester 1 or 2
			RESULT = 0
		end
	end

	if $ARGS[1] = 'offered_this_month':
		RESULT = month = func('uniutil', 'exam', 'month', func('uniutil', 'semester', 'get'))
	end

	if $ARGS[1] = 'offered_today':
		RESULT = func('uniutil', 'exam', 'offered_this_month') and week < 6
	end

	if $ARGS[1] = 'print_month':
		$RESULT = $monthname[func('uniutil', 'exam', 'month', ARGS[2])]
	end
end

if $ARGS[0] = 'semester':
	if $ARGS[1] = 'get':
		RESULT = unisemestrstats['num']
	end

	if $ARGS[1] = 'set':
		unisemestrstats['num'] = ARGS[2]
	end

	if $ARGS[1] = 'next':
		RESULT = (func('uniutil', 'semester', 'get') mod func('uniutil', 'semester', 'total')) + 1
	end

	if $ARGS[1] = 'total':
		RESULT = 2
	end

	if $ARGS[1] = 'update':
		gs 'uniutil', 'semester', 'set', func('uniutil', 'semester', 'next')
	end

	if $ARGS[1] = 'get_year':
		RESULT = unisemestrstats['year']
	end

	if $ARGS[1] = 'update_year':
		unisemestrstats['year'] = year
	end

	if $ARGS[1] = 'is_active':
		RESULT = func('uniutil', 'lecture', 'offered_this_month') or func('uniutil', 'exam', 'offered_this_month')
	end

	if $ARGS[1] = 'print_months':
		$RESULT = 'Semester ' + ARGS[2] + ' lectures: ' + func('uniutil', 'lecture', 'print_months', ARGS[2]) + '.
'
		$RESULT += 'Semester ' + ARGS[2] + ' exams: ' + func('uniutil', 'exam', 'print_month', ARGS[2]) + '.
'
	end

	if $ARGS[1] = 'print_all_months':
		unisemester_idx = 1

		:semesterprintmonthsloop
		$RESULT += func('uniutil', 'semester', 'print_months', unisemester_idx)
		if unisemester_idx < func('uniutil', 'semester', 'total'):
			unisemester_idx += 1
			jump 'semesterprintmonthsloop'
		end

		killvar 'unisemester_idx'
	end
end

if $ARGS[0] = 'semester_result':
	if $ARGS[1] = 'get':
	RESULT = unisemestrstats['is_passed']
end

	if $ARGS[1] = 'set':
		unisemestrstats['is_passed'] = ARGS[2]
	end

	if $ARGS[1] = 'set_passed':
		gs 'uniutil', 'semester_result', 'set', 1
	end

	if $ARGS[1] = 'set_failed':
		gs 'uniutil', 'semester_result', 'set', -1
	end

	if $ARGS[1] = 'reset':
		gs 'uniutil', 'semester_result', 'set', 0
	end

	if $ARGS[1] = 'is_passed':
		RESULT = func('uniutil', 'semester_result', 'get') > 0
	end

	if $ARGS[1] = 'is_failed':
		RESULT = func('uniutil', 'semester_result', 'get') < 0
	end
end

if $ARGS[0] =  'exam_outcome':
	gs 'uniutil', 'assignment_progress', 'reset'
	gs 'uniutil', 'assignments_done', 'reset'
	gs 'uniutil', 'attendance', 'reset'

	if $ARGS[1] = 'passed_honors':
		gs 'uniutil', 'semester_result', 'set_passed'
		gs 'uniutil', 'scholarship', 'set', func('uniutil', 'scholarship', 'honors_value')
	elseif $ARGS[1] = 'passed':
		gs 'uniutil', 'semester_result', 'set_passed'
		gs 'uniutil', 'scholarship', 'reset'
	else
		gs 'uniutil', 'semester_result', 'set_failed'
		gs 'uniutil', 'scholarship', 'reset'
	end
end

if $ARGS[0] = 'passed_semesters':
	if $ARGS[1] = 'get':
		RESULT = unisemestrstats['prev_passed_count'] + iif(func('uniutil', 'semester_result', 'is_passed'), 1, 0)
	end

	if $ARGS[1] = 'set':
		unisemestrstats['prev_passed_count'] = ARGS[2]
	end

	if $ARGS[1] = 'update':
		gs 'uniutil', 'passed_semesters', 'set', func('uniutil', 'passed_semesters', 'get')
	end
end

--- uniutil ---------------------------------