-- Loading Road Control
info = {
	line = "Maverick2797_diamonds",
	rc = "autorun FREIGHT",
	outside = "DURT Logs to Diamonds Conversion Train",
	inside = "Cargo: Logs and Leaves\nOrigin: Auto Logging Plantation\nDestination: Icebreaker Factory",
}
accelerator_loop_diverge_sig = POS(-3602,9,-2282)
loading_exit_sig = POS(-3539,9,-2279)
control_interface = POS(-3530,8,-2267)

depart = function()
	if not atc_arrow then atc_send("R") end
	if can_set_route(loading_exit_sig,"Train Accelerator Loop") then
		set_route(loading_exit_sig,"Train Accelerator Loop")
		atc_send("SM")
		set_line(info.line)
		set_rc(info.rc)
		atc_set_text_inside(info.inside)
		atc_set_text_outside(info.outside)
		return
	else
		interrupt(2,"depart")
		return
	end
end

if event.type=="ext_int" then
	if not atc_id then
		interrupt_pos(control_interface,"No Train at Signal")
		return
	elseif atc_speed ~= 0 then
		interrupt_pos(control_interface,"Train Still Moving")
		return
	end
	
	if event.message == "split_around" then
    atc_send("I+S0B0WRS3 E S3;")
		set_rc("split_around")
		return 
	end
	if event.message == "depart" then
		depart()
		return
	end
	if event.message=="shuffle" then
		if can_set_route(loading_exit_sig,"Loop Around") then
			set_route(loading_exit_sig,"Loop Around")
			set_route(accelerator_loop_diverge_sig,"Loading Road")
			atc_send("S8D30S5")
			return
		else
			interrupt_pos(control_interface,"Unable to set routes")
			return
		end
		return
	end
	return
end

if event.type=="int" then
	if event.message=="depart" then depart() return end
	return
end