defineProperty("vehF", globalPropertyf("sim/flightmodel/parts/tire_vrt_def_veh[0]"))
defineProperty("defF", globalPropertyf("anim/deflectionF"))
defineProperty("pause", globalPropertyf("sim/time/paused"))

defineProperty("ignP", globalPropertyf("sim/cockpit2/engine/actuators/igniter_on[4]"))

defineProperty("frame_time", globalPropertyf("frame_time")) 
defineProperty("ON", globalPropertyf("pushback/on")) 
defineProperty("groundspeed", globalPropertyf("sim/flightmodel/position/groundspeed")) 

defineProperty("overrideT", globalPropertyf("sim/operation/override/override_throttles")) 
defineProperty("useL1", globalPropertyf("sim/flightmodel/engine/ENGN_thro_use[0]")) 
defineProperty("useR1", globalPropertyf("sim/flightmodel/engine/ENGN_thro_use[1]")) 
defineProperty("useP", globalPropertyf("sim/flightmodel/engine/ENGN_thro_use[4]")) 
defineProperty("useL2", globalPropertyf("sim/flightmodel/engine/ENGN_thro_use[2]")) 
defineProperty("useR2", globalPropertyf("sim/flightmodel/engine/ENGN_thro_use[3]")) 

local use={useL1, useR1, useL2, useR2}

defineProperty("throttleL1", globalPropertyf("sim/cockpit2/engine/actuators/throttle_ratio[0]")) 
defineProperty("throttleR1", globalPropertyf("sim/cockpit2/engine/actuators/throttle_ratio[1]")) 
defineProperty("throttleL2", globalPropertyf("sim/cockpit2/engine/actuators/throttle_ratio[2]")) 
defineProperty("throttleR2", globalPropertyf("sim/cockpit2/engine/actuators/throttle_ratio[3]")) 
defineProperty("throttleP", globalPropertyf("sim/cockpit2/engine/actuators/throttle_ratio[4]")) 

local throttle={throttleL1, throttleR1, throttleL2, throttleR2}

defineProperty("fuelP", globalPropertyf("sim/cockpit/engine/fuel_pump_on[4]")) 
defineProperty("mixP", globalPropertyf("sim/flightmodel/engine/ENGN_mixt[4]")) 
defineProperty("vector", globalPropertyf("sim/cockpit2/controls/thrust_vector_ratio")) 

defineProperty("NL1", globalPropertyf("sim/flightmodel/engine/ENGN_N1_[0]"))
defineProperty("NR1", globalPropertyf("sim/flightmodel/engine/ENGN_N1_[1]"))
defineProperty("NL2", globalPropertyf("sim/flightmodel/engine/ENGN_N1_[2]"))
defineProperty("NR2", globalPropertyf("sim/flightmodel/engine/ENGN_N1_[3]"))
defineProperty("NP", globalPropertyf("sim/flightmodel/engine/ENGN_N1_[4]"))
defineProperty("NFF", globalPropertyf("sim/flightmodel/engine/ENGN_FF_[4]"))

local N={NL1, NR1, NL2, NR2}

defineProperty("lB", globalPropertyf("sim/cockpit2/controls/left_brake_ratio"))
defineProperty("rB", globalPropertyf("sim/cockpit2/controls/right_brake_ratio"))
defineProperty("pB", globalPropertyf("sim/cockpit2/controls/parking_brake_ratio"))

defineProperty("steering_angle", globalPropertyf("sim/flightmodel2/gear/tire_steer_actual_deg[0]"))

defineProperty("tire", globalPropertyf("prep/pushT_tire"))
defineProperty("tireS", globalPropertyf("prep/pushT_tireS"))
defineProperty("pushT_tire_rot", globalPropertyf("prep/pushT_tire_rot"))
defineProperty("pushT_angle", globalPropertyf("prep/pushT_angle"))
defineProperty("pushT_long", globalPropertyf("prep/pushT_long"))
defineProperty("pushT_lat", globalPropertyf("prep/pushT_lat"))
defineProperty("pushT_is", globalPropertyf("prep/pushT_is"))
defineProperty("pushT_pitch", globalPropertyf("prep/pushT_pitch"))

defineProperty("gearZ", globalPropertyf("sim/aircraft/parts/acf_gear_znodef[0]"))
defineProperty("gearleg", globalPropertyf("sim/aircraft/parts/acf_gear_leglen[0]"))

defineProperty("light1", globalPropertyf("prep/pushT_light1"))
defineProperty("light2", globalPropertyf("prep/pushT_light2"))
defineProperty("night", globalPropertyf("sim/graphics/scenery/percent_lights_on")) -- how dark it is


defineProperty("ZTS", globalPropertyf("sim/time/zulu_time_sec"))
defineProperty("taxiC", globalPropertyf("sim/cockpit/electrical/taxi_light_on"))
defineProperty("landingC", globalPropertyf("sim/cockpit/electrical/landing_lights_on"))
defineProperty("beacon", globalPropertyf("sim/graphics/animation/airport_beacon_rotation"))
defineProperty("batt", globalPropertyf("sim/cockpit2/electrical/battery_on[0]"))
defineProperty("hyd", globalPropertyf("sim/cockpit2/switches/electric_hydraulic_pump_on"))
defineProperty("precip", globalPropertyf("sim/weather/rain_percent"))


defineProperty("wiper_angle", globalPropertyf("sim/flightmodel2/misc/wiper_angle_deg[0]"))
defineProperty("wiper", globalPropertyf("anim/wiper"))

defineProperty("dsp1", globalPropertyf("DSP1"))
defineProperty("dsp2", globalPropertyf("DSP2"))
defineProperty("dsp3", globalPropertyf("DSP3"))
defineProperty("dsp4", globalPropertyf("DSP4"))



local eP=findCommand("sim/starters/engage_start_run_5")
local sP=findCommand("sim/starters/shut_down_5")
local timer=0
local hydmem=0

set(vector, 0.5)
local SP=0

local ZP=-7.5
function update()

	local FT=get(frame_time)
	local speed=get(groundspeed)
	local push_loc=get(pushT_long)
	local Dark=get(night)
	
	--set(wiper, get(wiper_angle)/170)
	local prec=0.3*get(precip)+0.8*(1-get(precip))
	
	if get(precip)>0.01 then set(wiper,0.8*math.abs(prec-math.mod(get(ZTS),2*prec))/prec)
	else set(wiper, 0)
	end
	
	set(defF, 0.508+3.846*get(vehF)+0.9*(get(gearZ)+10.9732)/2.6268)
	set(pushT_pitch, 1.5+0.5*(get(gearZ)+10.9732)/2.6268)
	
	if get(pushT_long)<ZP+0.01 and get(ON)==1 then
		set(gearZ, (-13+get(gearZ)*20)/21)
		set(gearleg, (1.17 + get(gearleg)*20)/21)
	else 
		set(gearZ, -10.9732)
		set(gearleg, 1.15824)
	end
	
	if get(pause)==0 then
		if get(ON)==1 and get(pushT_long)<ZP+0.01 then
			commandBegin(eP)
			set(overrideT ,1)
			for i=1,4 do set(use[i], 0)	end set(lB, 0)	set(rB, 0)	set(pB, 0) 
					
			if speed>2.5 then 
				set(useP, 0)
				set(pB, (speed-2.5)/3)
			elseif speed >1.5 then
				set(useP, math.max(get(throttleP)*(2.5-speed),0))
			else
				set(useP, get(throttleP))
			end
			
			set(tire, get(tire)+(get(vector)*2-1)*FT*speed/0.5)
			set(tireS, get(tireS)-45*(get(vector)*2-1)*FT*speed/1)

			set(pushT_tire_rot, (get(pushT_tire_rot)*30+ get(steering_angle)/60)/31)
			set(pushT_angle, (50*get(pushT_angle)+2*(speed)*get(pushT_tire_rot)/10)/51)
			set(landingC, 0)
			set(hyd, 1)
		elseif get(ON)==1 and get(pushT_long)>ZP+0.01 then
			commandBegin(eP)
			set(overrideT ,1)
			for i=1,4 do set(use[i], 0)	end set(lB, 0)	set(rB, 0)	set(pB, 0) 
			set(pushT_long, get(pushT_long)-FT)		
			set(tire, get(tire)+FT*2.5)
			set(tireS, get(tireS)-45*FT*2*1.5)
			set(landingC, 0)
		else
			commandEnd(eP)
			set(overrideT ,0)
			set(mixP, 0)
			set(useP, 0)
			set(NP, 0)
			set(vector, 0.5)
		end
		
		if timer>0 then
				set(overrideT ,1)
				timer=timer-FT
				set(pushT_long, push_loc+1.5*FT*math.cos(get(pushT_angle)*45*3.141592/180))
				set(pushT_lat, get(pushT_lat)+1.5*FT*math.sin(get(pushT_angle)*45*3.141592/180))
				set(pushT_angle, get(pushT_angle)+5/45*FT)
				set(tire, get(tire)-2.5*FT)
				set(tireS, get(tireS)+45*FT*2*1.5)
				set(pushT_tire_rot, 0.4)
		else timer=0
		end
	
	end
	
	if get(ON)==1 or timer>0 then set(pushT_is, 1) 
	else set(pushT_is, 0)
	end
	
	if speed>3.5 then set(ON, 0) end
	set(NFF,0)
	
	------ Lights
	local temp=math.abs(0.8-math.mod(get(ZTS),1.6))/0.8
	set(light2, 0.5*get(ON)*(Dark+(1-Dark)*0.1)*temp)
	set(light1, get(taxiC))
	
	
	set(dsp1, get(ON))
	set(dsp2, get(vector))
	set(dsp3, get(batt))
	
end

ENGAGE = findCommand("pushback/engage")
function ENGAGE_handler(phase)  -- for all commands phase equals: 0 on press; 1 while holding; 2 on release
	if 0 == phase and timer==0 and get(batt)==1 then
			if get(ON)==1 and get(vector)==1 then set(ON, 0) set(vector, 0.5) set(pB,0.5) timer=20 set(hyd, hydmem) commandBegin(sP)	
			elseif get(ON)==1 and get(vector)==0 then
				set(vector, 1)
			else
				hydmem=get(hyd)
				set(hyd, 1)
				set(vector, 1)
				set(pushT_long,SP)
				set(pushT_angle,0)
				set(pushT_lat,0)
				set(ON, 1)
				set(ignP, 1)
				commandBegin(eP)
				commandEnd(sP)
				set(NP, 100)			
			end
			
	end
return 0
end
registerCommandHandler(ENGAGE, 0, ENGAGE_handler)	

REVERSE = findCommand("pushback/reverse")
function REVERSE_handler(phase)  -- for all commands phase equals: 0 on press; 1 while holding; 2 on release
	if 0 == phase and timer==0 and get(batt)==1 then
			if get(ON)==1 and get(vector)==0 then set(ON, 0) set(vector, 0.5) set(pB,0.5) timer=20 set(hyd, hydmem) commandBegin(sP)	
			elseif get(ON)==1 and get(vector)==1 then
				set(vector, 0)
			else
				hydmem=get(hyd)
				set(hyd, 1)
				set(vector, 0)
				set(pushT_long,SP)
				set(pushT_angle,0)
				set(pushT_lat,0)
				set(ON, 1)
				set(ignP, 1)
				commandBegin(eP)
				set(NP, 100)
				commandEnd(sP)
			end
			
	end
return 0
end
registerCommandHandler(REVERSE, 0, REVERSE_handler)	


