//////////////// STATES FOR MOVING SPRITES LOGIC //////////

state length_calc
	getwall[se_ptr].x se_x1 // first line
	getwall[se_ptr].y se_y1
	getwall[se_ptr].nextwall se_tmp
	getwall[se_tmp].x mx
	getwall[se_tmp].y my
	subvarvar mx se_x1
	subvarvar my se_y1
	getangle mang mx my
	addvar mang 512
	setvarvar x2 se_x1
	addvarvar x2 10
	rotatepoint se_x1 se_y1 x2 se_y1 mang se_x2 se_y2
	getwall[rot_wallptr].x se_x3 //second line
	getwall[rot_wallptr].y se_y3
	getwall[rot_wallptr].nextwall se_tmp
	getwall[se_tmp].x se_x4
	getwall[se_tmp].y se_y4

	setvarvar se_A1 se_y1
	subvarvar se_A1 se_y2 //A1
	setvarvar se_B1 se_x2
	subvarvar se_B1 se_x1 //B1
	setvarvar se_C1 se_x1 //C1
	subvarvar se_C1 se_x2
	mulvarvar se_C1 se_y1
	setvarvar se_tmp se_y2
	subvarvar se_tmp se_y1
	mulvarvar se_tmp se_x1
	addvarvar se_C1 se_tmp //C1
						
	setvarvar se_A2 se_y3
	subvarvar se_A2 se_y4 //A2
	setvarvar se_B2 se_x4
	subvarvar se_B2 se_x3 //B2
	setvarvar se_C2 se_x3 //C2
	subvarvar se_C2 se_x4
	mulvarvar se_C2 se_y3
	setvarvar se_tmp se_y4
	subvarvar se_tmp se_y3
	mulvarvar se_tmp se_x3
	addvarvar se_C2 se_tmp //C2
						
	setvarvar se_tmp1 se_C1
	setvarvar se_tmp2 se_C2
	setvarvar se_tmp3 se_A1
	setvarvar se_tmp4 se_A2
	mulvarvar se_tmp1 se_B2
	mulvarvar se_tmp2 se_B1
	subvarvar se_tmp1 se_tmp2
	mulvarvar se_tmp1 -1
	mulvarvar se_tmp3 se_B2
	mulvarvar se_tmp4 se_B1
	subvarvar se_tmp3 se_tmp4
	divvarvar se_tmp1 se_tmp3
	setvarvar x se_tmp1 //coord X

	setvarvar se_tmp1 se_A1
	setvarvar se_tmp2 se_A2
	setvarvar se_tmp3 se_A1
	setvarvar se_tmp4 se_A2
	mulvarvar se_tmp1 se_C2
	mulvarvar se_tmp2 se_C1
	subvarvar se_tmp1 se_tmp2
	mulvarvar se_tmp1 -1
	mulvarvar se_tmp3 se_B2
	mulvarvar se_tmp4 se_B1
	subvarvar se_tmp3 se_tmp4
	divvarvar se_tmp1 se_tmp3
	setvarvar y se_tmp1 //coord Y
						
	subvarvar se_x1 x
	subvarvar se_y1 y
	mulvarvar se_x1 se_x1
	mulvarvar se_y1 se_y1
	addvarvar se_x1 se_y1
	sqrt se_x1 se_len
ends

state init_calcvel
// --Ускорение

	ifvarn sprite[THISACTOR].picnum MOVER 
		getactor[THISACTOR].extra acc_extravar
		
	ifvare acc_extravar -1 setvar acc_extravar 0
	
	ifvarvarg acc_speed1 acc_dist {
		setvarvar acc_speed1 acc_dist
	}
	
	ifvarg acc_dist 0
	{
		ifvarn acc_extravar 3
		{
			setvarvar se_tmp1 acc_dist //полное расстояние которое необходимо пройти
			setvarvar acc_var acc_speed1
			mulvarvar acc_var acc_var
			mulvarvar acc_var buffer_multiple //определение ускорения до 0.01
			mulvar se_tmp1 2
			divvarvar se_tmp1 3  //деление всего пройденного пути на 3
			ifvarg se_tmp1 0 { divvarvar acc_var se_tmp1 } //a = V^2/2*(S/3)
			setvarvar acceleration acc_var //определение ускорения на полном пути
			setvarvar acc_var acc_speed1 //t = V/a
			mulvarvar acc_var buffer_multiple //определение ускорения до 0.01
			ifvarg acceleration 0 divvarvar acc_var acceleration
			setvarvar acc_atime acc_var //время затрачиваемое на прохождение 1/3 пути с ускорением
			addvarvar se_endtick1 acc_atime //прибавка к общему времени
			setvarvar acc_dx1 se_endtick1 //проверка пройденого пути
			mulvarvar acc_dx1 acc_dx1
			mulvarvar acc_dx1 acceleration
			divvar acc_dx1 2
			divvarvar acc_dx1 buffer_multiple
		}

		// --Ускорение							
		ifvare acc_extravar 0 { 
			addvarvar se_endtick1 acc_atime //прибавка к общему времени
			shiftvarl acc_dx1 1 //путь, затрачиваемый на ускорение и замеделение
		}
		ifvarand acc_extravar 1 setvar acc_atime 0
		setvarvar acc_Sx1 acc_dist
		subvarvar acc_Sx1 acc_dx1 //путь, с постоянной скоростью
		setvarvar acc_vtime acc_Sx1
		ifvarg acc_speed1 0 divvarvar acc_vtime acc_speed1 //время с постоянной скоростью
		ifvare acc_extravar 3 ifvare acc_vtime 0 { setvar acc_vtime 1 setvarvar acc_speed1 acc_Sx1 }
		setvarvar dx_time acc_vtime
		addvarvar se_endtick1 acc_vtime //общее время движения
		addvarvar acc_vtime acc_atime //время с ускорением и движением с постоянной скоростью
		
		setvar buff_dx 0
		getactor[THISACTOR].ang ang
		whilevarvarn se_count se_endtick1
		{
			addvar se_count 1
			
			/*
			//ifvarvarg se_count acc_atime { setvarvar tmp acc_vtime addvar tmp 1 ifvarvarl se_count tmp setvar moving_vel 1 } else
			ifvarvarg se_count acc_atime ifvarvarl se_count acc_vtime setvar moving_vel 1 else
			ifvarvarg se_count acc_vtime { ifvarand acc_extravar 2 nullop else setvar moving_vel 2 }
			*/
			
			ifvare acc_extravar 3 {
				ifvarvarg se_count acc_atime { setvarvar tmp acc_vtime addvar tmp 1 ifvarvarl se_count tmp setvar moving_vel 1 }
			} else { ifvarvarg se_count acc_atime ifvarvarl se_count acc_vtime setvar moving_vel 1 }
			ifvarvarg se_count acc_vtime { ifvarand acc_extravar 2 nullop else setvar moving_vel 2 }

			ifvare moving_vel 0 //ускорение
			{
				setvarvar acc_var acceleration
				mulvarvar acc_var se_count //ускорение * время = скорость 
				ifvarg acc_acount 0 divvarvar acc_var acc_acount
				setvarvar acc_defvel acc_var	
			} 	else 
			ifvare moving_vel 1 { setvarvar acc_defvel acc_speed1 mulvarvar acc_defvel buffer_multiple }
				else
			ifvare moving_vel 2 //замеделение
			{
				addvar acc_count 1
				setvarvar acc_var acc_speed1
				mulvarvar acc_var buffer_multiple
				setvarvar acc_var2 acceleration
				mulvarvar acc_var2 -1
				mulvarvar acc_var2 acc_count
				addvarvar acc_var acc_var2
				setvarvar acc_defvel acc_var 	
			}
			
			addvarvar buffer_speed acc_defvel
			setvarvar buff_dx buffer_speed
			divvarvar buff_dx buffer_multiple
			setvarvar acc_var acc_dist
			subvarvar buff_dx acc_var

			//Debug
			ifvarvarg se_count se_endtick1 { 
			//redefinequote TEMPQUOTE INIT_CALCVEL CYCLE OUT OF RANGE (SPEED1)
			addlogvar THISACTOR
			addlogvar se_count
			addlogvar se_endtick1
			getactor[THISACTOR].lotag lotag
			addlogvar lotag
			addlogvar acc_dist
			addlogvar acc_speed1
			//qstrcat 8 TEMPQUOTE 
			//quote 8
			setvarvar se_count se_endtick1
			break
			}
		}
		ifvarl buff_dx 0 { mulvar buff_dx -1 orvar dx_case 1 }
		ifvarvarg buff_dx dx_time orvar dx_case 2
	}	
	
	setvar se_count 0
	setvar moving_vel 0
	setvar acc_count 0
	setvar buffer_speed 0
	ifvarg acc_speed2 0
	{
		ifvarn acc_extravar 3
		{
			ifvare lotag 64 ifvare se_zvel 2 setvarvar se_tmp1 rot_distang else 
			ifvare sprite[THISACTOR].picnum MOVER setvarvar se_tmp1 rot_distang else
			setvarvar se_tmp1 acc_dist //полное расстояние которое необходимо пройти
			setvarvar acc_var acc_speed2
			mulvarvar acc_var acc_var
			mulvarvar acc_var buffer_multiple //определение ускорения до 0.01
			mulvar se_tmp1 2
			divvarvar se_tmp1 3  //деление всего пройденного пути на 3
			ifvarg se_tmp1 0 { divvarvar acc_var se_tmp1 } //a = V^2/2*(S/3)
			setvarvar acceleration2 acc_var //определение ускорения на полном пути
			setvarvar acc_var acc_speed2 //t = V/a
			mulvarvar acc_var buffer_multiple //определение ускорения до 0.01
			ifvarg acceleration2 0 divvarvar acc_var acceleration2
			setvarvar acc_atime2 acc_var //время затрачиваемое на прохождение 1/3 пути с ускорением
			addvarvar se_endtick2 acc_atime2 //прибавка к общему времени
			setvarvar acc_dx1 se_endtick2 //проверка пройденого пути
			mulvarvar acc_dx1 acc_dx1
			mulvarvar acc_dx1 acceleration2
			divvar acc_dx1 2
			divvarvar acc_dx1 buffer_multiple
		}

		// --Ускорение							
		ifvare acc_extravar 0 { 
			addvarvar se_endtick2 acc_atime2 //прибавка к общему времени
			shiftvarl acc_dx1 1 //путь, затрачиваемый на ускорение и замеделение
		}
		ifvarand acc_extravar 1 setvar acc_atime2 0
		ifvare lotag 64 ifvare se_zvel 2 setvarvar acc_Sx1 rot_distang else  
		ifvare sprite[THISACTOR].picnum MOVER setvarvar acc_Sx1 rot_distang else
		setvarvar acc_Sx1 acc_dist
		subvarvar acc_Sx1 acc_dx1 //путь, с постоянной скоростью
		setvarvar acc_vtime2 acc_Sx1
		ifvarg acc_speed2 0 divvarvar acc_vtime2 acc_speed2 //время с постоянной скоростью
		ifvare acc_extravar 3 ifvare acc_vtime2 0 { setvar acc_vtime2 1 setvarvar acc_speed2 acc_Sx1 }
		setvarvar dx_time acc_vtime2
		addvarvar se_endtick2 acc_vtime2 //общее время движения
		addvarvar acc_vtime2 acc_atime2 //время с ускорением и движением с постоянной скоростью

		setvar buff_dx2 0
		whilevarvarn se_count se_endtick2
		{
			addvar se_count 1
			
			/*
			//ifvarvarg se_count acc_atime2 { setvarvar tmp acc_vtime2 addvar tmp 1 ifvarvarl se_count tmp setvar moving_vel 1 } else
			ifvarvarg se_count acc_atime2 ifvarvarl se_count acc_vtime2 setvar moving_vel 1 else
			ifvarvarg se_count acc_vtime2 { ifvarand acc_extravar 2 nullop else setvar moving_vel 2 }
			*/
			
			ifvare acc_extravar 3 {
				ifvarvarg se_count acc_atime2 { setvarvar tmp acc_vtime2 addvar tmp 1 ifvarvarl se_count tmp setvar moving_vel 1 }
			} else { ifvarvarg se_count acc_atime2 ifvarvarl se_count acc_vtime2 setvar moving_vel 1 }
			ifvarvarg se_count acc_vtime2 { ifvarand acc_extravar 2 nullop else setvar moving_vel 2 }
			
			ifvare moving_vel 0 //ускорение
			{
				setvarvar acc_var acceleration2
				mulvarvar acc_var se_count //ускорение * время = скорость 
				ifvarg acc_acount 0 divvarvar acc_var acc_acount
				setvarvar acc_defvel acc_var	
			} 	else 
			ifvare moving_vel 1 { setvarvar acc_defvel acc_speed2 mulvarvar acc_defvel buffer_multiple }
				else
			ifvare moving_vel 2 //замеделение
			{
				addvar acc_count 1
				setvarvar acc_var acc_speed2
				mulvarvar acc_var buffer_multiple
				setvarvar acc_var2 acceleration2
				mulvarvar acc_var2 -1
				mulvarvar acc_var2 acc_count
				addvarvar acc_var acc_var2
				setvarvar acc_defvel acc_var 	
			}
			addvarvar buffer_speed acc_defvel
			setvarvar buff_dx2 buffer_speed
			divvarvar buff_dx2 buffer_multiple
			ifvare lotag 64 ifvare se_zvel 2 setvarvar acc_var rot_distang else 
			ifvare sprite[THISACTOR].picnum MOVER setvarvar acc_var rot_distang else
				setvarvar acc_var acc_dist
			subvarvar buff_dx2 acc_var
			
			//Debug
			
			ifvarvarg se_count se_endtick2 { 
			redefinequote TEMPQUOTE INIT_CALCVEL CYCLE OUT OF RANGE (SPEED2)
			addlogvar THISACTOR
			addlogvar se_count
			addlogvar se_endtick2
			getactor[THISACTOR].lotag lotag
			addlogvar lotag
			qstrcat 8 TEMPQUOTE 
			quote 8
			setvarvar se_count se_endtick2
			break
			}
		}
		
		ifvarl buff_dx2 0 { mulvar buff_dx2 -1 orvar dx_case2 1 }
		ifvarvarg buff_dx2 dx_time orvar dx_case2 2

		setvar se_count 0
		setvar moving_vel 0
		setvar acc_count 0
		setvar buffer_speed 0
	}
ends



/*
//Блок определения скоростей
Ввод:
acc_dist - путь
acc_speed1 - скорость 1
acc_speed2 - скорость 2
acc_extravar - опции для ускорения/замедления
Выход:
acc_atime
acc_vtime
acc_a1time
acc_atime2
acc_vtime2
acc_a1time2
se_endtick1
se_endtick2
*/

state se_calcvel1

	/*
	ifvarvarg se_count acc_atime ifvarvarl se_count acc_vtime setvar moving_vel 1 else
	//ifvarvarg se_count acc_atime { setvarvar tmp acc_vtime addvar tmp 1 ifvarvarl se_count tmp setvar moving_vel 1 } else
	ifvarvarg se_count acc_vtime { ifvarand acc_extravar 2 nullop else setvar moving_vel 2 }
	*/
	
	
	ifvare acc_extravar 3 {
		ifvarvarg se_count acc_atime { setvarvar tmp acc_vtime addvar tmp 1 ifvarvarl se_count tmp setvar moving_vel 1 }
	} else { ifvarvarg se_count acc_atime ifvarvarl se_count acc_vtime setvar moving_vel 1 }
	ifvarvarg se_count acc_vtime { ifvarand acc_extravar 2 nullop else setvar moving_vel 2 }
	
	ifvare se_count 1 setvar acc_tempvel3 0
	ifvare moving_vel 0 //ускорение
	{
		setvarvar acc_var acceleration
		mulvarvar acc_var se_count //ускорение * время = скорость 
		setvarvar acc_defvel acc_var
	} else 
	ifvare moving_vel 1 
	{ 
		setvarvar acc_defvel acc_speed1

		ifvarg acc_extravar 0 
		ifvarand acc_extravar 2 {
		ifvarvare se_count acc_vtime {
		ifvarand dx_case 1 addvarvar acc_defvel buff_dx else 
			subvarvar acc_defvel buff_dx } } else
		ifvarvarl speed_dx buff_dx {
			ifvare dx_case 0 subvarvar acc_defvel 1 else 
				ifvare dx_case 1 addvarvar acc_defvel 1  
			addvarvar speed_dx 1
		}	
		mulvarvar acc_defvel buffer_multiple
	} else
	ifvare moving_vel 2 //замеделение
	{
		setvar speed_dx 0 //переменная внесения поправки
		addvar acc_count 1
		setvarvar acc_var acc_speed1
		mulvarvar acc_var buffer_multiple
		setvarvar acc_var2 acceleration
		mulvarvar acc_var2 -1
		mulvarvar acc_var2 acc_count
		addvarvar acc_var acc_var2
		ifvarand dx_case 2 {
			setvarvar acc_var2 buff_dx
			mulvarvar acc_var2 buffer_multiple
			ifvare acc_count 1 { ifvarand dx_case 1 addvarvar acc_var acc_var2 else
				subvarvar acc_var acc_var2 }
		}
		setvarvar acc_defvel acc_var 
	}
	addvarvar buffer_speed acc_defvel
	setvarvar buffer_tmp buffer_speed
	divvarvar buffer_tmp buffer_multiple
	setvarvar acc_tempvel acc_defvel //скорость до деления
	divvarvar acc_defvel buffer_multiple
	setvarvar acc_tempvel2 acc_defvel
	mulvarvar acc_tempvel2 buffer_multiple //скорость после деления
	subvarvar acc_tempvel acc_tempvel2 //остаток от деления
	addvarvar acc_tempvel3 acc_tempvel
	ifvarvarg acc_tempvel3 buffer_multiple 
		{ addvarvar acc_defvel 1 subvarvar acc_tempvel3 buffer_multiple }
ends

state se_calcvel2
	/*
	ifvarvarg se_count acc_atime2 ifvarvarl se_count acc_vtime2 setvar moving_vel 1 else
	//ifvarvarg se_count acc_atime2 { setvarvar tmp acc_vtime2 addvar tmp 1 ifvarvarl se_count tmp setvar moving_vel 1 } else
	ifvarvarg se_count acc_vtime2 { ifvarand acc_extravar 2 nullop else setvar moving_vel 2 }
	*/
	
	ifvare acc_extravar 3 {
		ifvarvarg se_count acc_atime2 { setvarvar tmp acc_vtime2 addvar tmp 1 ifvarvarl se_count tmp setvar moving_vel 1 }
	} else { ifvarvarg se_count acc_atime2 ifvarvarl se_count acc_vtime2 setvar moving_vel 1 }
	ifvarvarg se_count acc_vtime2 { ifvarand acc_extravar 2 nullop else setvar moving_vel 2 }
					
	ifvare se_count 1 setvar acc_tempvel3 0
	ifvare moving_vel 0 //ускорение
	{
		setvarvar acc_var acceleration2
		mulvarvar acc_var se_count //ускорение * время = скорость 
		setvarvar acc_defvel acc_var
	} else 
	ifvare moving_vel 1 
	{ 
		setvarvar acc_defvel acc_speed2

		ifvarg acc_extravar 0 
		ifvarand acc_extravar 2 {
		ifvarvare se_count acc_vtime2 {
		ifvarand dx_case2 1 addvarvar acc_defvel buff_dx2 else 
			subvarvar acc_defvel buff_dx2 } } else
		ifvarvarl speed_dx buff_dx2 {
			ifvare dx_case2 0 subvarvar acc_defvel 1 else 
				ifvare dx_case2 1 addvarvar acc_defvel 1  
			addvarvar speed_dx 1
		}	
		mulvarvar acc_defvel buffer_multiple
	} else
	ifvare moving_vel 2 //замеделение
	{
		setvar speed_dx 0 //переменная внесения поправки
		addvar acc_count 1
		setvarvar acc_var acc_speed2
		mulvarvar acc_var buffer_multiple
		setvarvar acc_var2 acceleration2
		mulvarvar acc_var2 -1
		mulvarvar acc_var2 acc_count
		addvarvar acc_var acc_var2
		ifvarand dx_case2 2 {
			setvarvar acc_var2 buff_dx2
			mulvarvar acc_var2 buffer_multiple
			ifvare acc_count 1 { ifvarand dx_case2 1 addvarvar acc_var acc_var2 else
				subvarvar acc_var acc_var2 }
		}
		setvarvar acc_defvel acc_var 
	}
	addvarvar buffer_speed acc_defvel
	setvarvar buffer_tmp buffer_speed
	divvarvar buffer_tmp buffer_multiple
	setvarvar acc_tempvel acc_defvel //скорость до деления
	divvarvar acc_defvel buffer_multiple
	setvarvar acc_tempvel2 acc_defvel
	mulvarvar acc_tempvel2 buffer_multiple //скорость после деления
	subvarvar acc_tempvel acc_tempvel2 //остаток от деления
	addvarvar acc_tempvel3 acc_tempvel
	ifvarvarg acc_tempvel3 buffer_multiple 
		{ addvarvar acc_defvel 1 subvarvar acc_tempvel3 buffer_multiple }
ends

state se64_loadoptions
	setvar spriteid 0
	setvar rot_varmax 0
	whilevarvarn spriteid NUMSPRITES
	{
		getactor[spriteid].picnum picnum
		ifvare picnum GPSPEED
		{
			getactor[THISACTOR].xvel rx_can
			getactor[spriteid].xvel tx_can
			ifvarvare rx_can tx_can
			{	
				getactor[spriteid].lotag lotag
				getactor[spriteid].hitag hitag	
				ifvare lotag 0 setvar sectordist 256 else 
					setvarvar sectordist lotag
				ifvare hitag 0 setvar sectorspeed 16 else  
					setvarvar sectorspeed hitag
			}
		} else
		ifvare picnum MUSICANDSFX
		{
			getactor[spriteid].xvel rx_can
			getactor[THISACTOR].xvel tx_can
			getactor[spriteid].zvel zvel
			ifvarvare tx_can rx_can
			{
				ifvare zvel 0 { 
					setvarvar doorsound_open sprite[spriteid].lotag 
					setvarvar doorsound_close sprite[spriteid].hitag
				}
				ifvare zvel 1 { 
					setvar se64_msf 1 
					setvarvar doorsound_open2 sprite[spriteid].lotag 
					setvarvar doorsound_close2 sprite[spriteid].hitag 
				}
				setactor[spriteid].owner sprite[THISACTOR].owner
			}
		} else
		ifvare picnum SECTOREFFECTOR
		{
			getactor[spriteid].xvel rx_can
			getactor[THISACTOR].xvel tx_can
			ifvarvare tx_can rx_can ifvare sprite[spriteid].lotag 10 
				setvarvar autoclose_delay sprite[spriteid].hitag
		} else
		ifvare picnum MOVER_POINT
		{
			getactor[spriteid].xvel rx_can
			getactor[THISACTOR].xvel tx_can
			ifvarvare tx_can rx_can {
			ifvare sprite[THISACTOR].zvel 3 {
				getactor[spriteid].zvel rot_turnway
				getactor[spriteid].lotag rot_angspeed
				getactor[spriteid].hitag acceleration
			} else {	
				getactor[spriteid].lotag rot_angspeed
				getactor[spriteid].hitag hitag
				/*
				ifvare hitag 1 { 
				getactor[THISACTOR].x x
				getactor[THISACTOR].y y
				getactor[spriteid].x x2
				getactor[spriteid].y y2
				setvarvar in1 x
				setvarvar in2 y
				subvarvar in1 x2
				subvarvar in2 y2
				state vec2d_lenght
				setvarvar rot_savedist out1 } //для zvel = 2
				*/
				ifvarvarl rot_varmax hitag 
					 setvarvar rot_varmax hitag
				getactor[THISACTOR].x rot_savex //для zvel = 2 и mover
				getactor[THISACTOR].y rot_savey
				getactor[THISACTOR].z rot_savez
				getactor[THISACTOR].ang rot_saveang
				}
			}
		}
		addvar spriteid 1
	}
ends

state se64_wallpointsaving
	setvar rot_tmp 0
	whilevarvarn rot_tmp NUMSECTORS
	{
		getactor[THISACTOR].hitag se_hitag
		getsector[rot_tmp].hitag rot_lotag
		ifvarvare rot_lotag se_hitag
		{
			getsector[rot_tmp].wallnum rot_wallnum
			getsector[rot_tmp].wallptr rot_wallptr
			setvar rot_tmp2 0
			whilevarvarn rot_tmp2 rot_wallnum
			{ 
				getwall[rot_wallptr].x wallx
				getwall[rot_wallptr].y wally
				getactor[THISACTOR].x x
				getactor[THISACTOR].y y
				subvarvar wallx x
				subvarvar wally y
				getangle rot_ang wallx wally
				mulvarvar wallx wallx
				mulvarvar wally wally
				addvarvar wallx wally
				sqrt wallx rot_dist 
				setarray ROT_PNDIST[rot_wallptr] rot_dist
				getactor[THISACTOR].ang ang
				subvarvar rot_ang ang
				setarray ROT_PNANG[rot_wallptr] rot_ang
				addvar rot_wallptr 1
				addvar rot_tmp2 1
			}
		}
		addvar rot_tmp 1
	}
ends

///// PREPARE MOVING VARIABLES //////

state se64_initslide //zvel = 0 
	setvarvar acc_speed1 sectorspeed
	setvar se_count 0
	setvar acc_count 0
	setvar buffer_speed 0

	setvar moving_vel 0
	ifvare moving_var 0 setvar moving_var 1 
		else setvar moving_var 0
ends

state se64_initrotator //zvel = 1
	addvarvar rot_nextway 1
	ifvarvarg rot_nextway rot_varmax setvar rot_nextway 0
	getactor[THISACTOR].ang rot_startang

	ifvare rot_nextway 0
	{
		setvarvar rot_gpang rot_initang
		ifvare rot_varmax 1 { ifvare rot_turnway 1 setvarvar rot_turnway 0 else setvarvar rot_turnway 1 }
	}

	setvar sectorid 0
	whilevarvarn sectorid NUMSECTORS 
	{
		headspritesect spriteid sectorid
		whilevarn spriteid -1 
		{
			getactor[spriteid].picnum picnum
			ifvare picnum MOVER_POINT
			{
				ifvarvare actorvar[spriteid].se_xvel se_xvel
				{
					getactor[spriteid].hitag hitag
					ifvarvare hitag rot_nextway
					{
						getactor[spriteid].ang rot_gpang
						getactor[spriteid].zvel rot_turnway
						getactor[spriteid].lotag rot_angspeed
					}
					getactor[THISACTOR].ang ang
					setvarvar rot_saveang rot_gpang //rot_saveang - для определения дистанции движения
					ifvare rot_turnway 0 { ifvarvarg rot_saveang ang subvarvar rot_saveang ang else { addvarvar rot_saveang 2048 subvarvar rot_saveang ang  } } else 
					ifvare rot_turnway 1 { ifvarvarg rot_saveang ang { addvarvar ang 2048 subvarvar rot_saveang ang } else subvarvar rot_saveang ang }			
					ifvarl rot_saveang 0 mulvar rot_saveang -1
					
					ifvarvare hitag rot_nextway
					{
						setvarvar acc_dist rot_saveang
						setvarvar acc_speed1 rot_angspeed
						setvar se_endtick1 0
						setvar buffer_multiple 1000
						state init_calcvel
					} else
					ifvarvarg rot_varmax 1
					ifvarvarg hitag rot_nextway
					{
						setvarvar acc_dist rot_saveang
						setvarvar acc_speed1 rot_angspeed
						setvar se_endtick1 0
						setvar buffer_multiple 1000
						state init_calcvel
					}
					
				}					
			}
			nextspritesect spriteid spriteid
		}
		addvar sectorid 1
	}
ends

state se64_initzvel2 //zvel = 2
	//ifvare moving_var 1 subvar mover_position 1 
		//else addvar mover_position 1
	addvar mover_position 1
	ifvarvarg mover_position rot_varmax setvar mover_position 0

	setvar sectorid 0
	setvar mov_pointid -1
	whilevarvarn sectorid NUMSECTORS 
	{
		headspritesect spriteid sectorid
		whilevarn spriteid -1 
		{
			getactor[spriteid].picnum picnum
			ifvare picnum MOVER_POINT
			{
				getactorvar[spriteid].se_xvel rx_can
				ifvarvare rx_can se_xvel
				ifvarvare sprite[spriteid].hitag mover_position
				{
					getactor[spriteid].x mov_coordx
					getactor[spriteid].y mov_coordy
					getactor[spriteid].z mov_coordz
					getactor[spriteid].ang mov_gpang
					getactor[spriteid].lotag acc_speed1
					getactor[spriteid].zvel rot_turnway
					setvarvar mov_pointid spriteid
				}
									
			}
			nextspritesect spriteid spriteid
		}
		addvar sectorid 1
	}
	
	getactor[THISACTOR].x x
	getactor[THISACTOR].y y
	getactor[THISACTOR].z z
	getactor[THISACTOR].ang ang
	ifvarg mover_position 0 {
	getactor[mov_pointid].x x2
	getactor[mov_pointid].y y2
	getactor[mov_pointid].z z2
	getactor[mov_pointid].ang rot_distang
	/*
	getincangle rot_distang ang mov_gpang
	ifvarl rot_distang 0 { ifvare rot_turnway 0 addvar rot_distang 2048 else mulvar rot_distang -1 } else { 
		ifvare rot_turnway 1 { setvarvar tmp 2048 subvarvar tmp rot_distang setvarvar rot_distang tmp } } 
	*/
	} 	
	else
	{
		
		setvarvar x2 rot_savex //координаты до нулевой точки
		setvarvar y2 rot_savey
		setvarvar z2 rot_savez
		setvarvar rot_distang rot_saveang
		ifvare rot_varmax 1 { ifvare rot_turnway 0 setvar rot_turnway 1 else setvar rot_turnway 0 }
		/*
		getincangle rot_distang rot_saveang ang
		ifvarl rot_distang 0 mulvar rot_distang -1 else { 
		ifvare rot_turnway 1 { setvarvar tmp 2048 subvarvar tmp rot_distang setvarvar rot_distang tmp } } 
		*/
	}
	
	ifvare rot_turnway 0 { ifvarvarg rot_distang ang subvarvar rot_distang ang else { addvarvar rot_distang 2048 subvarvar rot_distang ang  } } else 
	ifvare rot_turnway 1 { ifvarvarg rot_distang ang { addvarvar ang 2048 subvarvar rot_distang ang } else subvarvar rot_distang ang }			
	ifvarl rot_distang 0 mulvar rot_distang -1
	ifvare rot_distang 2048 setvar rot_distang 0

	setvarvar in1 x
	setvarvar in2 y
	setvarvar in3 z //-26368
	subvarvar in1 x2
	subvarvar in2 y2
	subvarvar in3 z2 //-9984
	shiftvarr in3 4
	setvarvar out1 in1
	mulvarvar out1 out1
	setvarvar math_tmp1 in2
	mulvarvar math_tmp1 math_tmp1
	addvarvar out1 math_tmp1
	setvarvar math_tmp1 in3
	mulvarvar math_tmp1 math_tmp1
	addvarvar out1 math_tmp1
	ifvarl out1 0 { //слижком большое расстояние
		ifvarn mov_pointid -1 {
			dist out1 THISACTOR mov_pointid
		} else setvar out1 0
	} else
		sqrt out1 out1
	setvarvar acc_dist out1
	
	setvar se_endtick1 0
	setvar se_endtick2 0
	setvar buffer_multiple 1000

	ifvarg acc_dist 64 {
	setvarvar acc_speed2 rot_distang
	mulvarvar acc_speed2 acc_speed1
	divvarvar acc_speed2 acc_dist 
	} else
		setvarvar acc_speed2 acc_speed1
		
	state init_calcvel
	setvarvar startx x
	setvarvar starty y
	setvarvar startz z

	subvarvar x2 x
	subvarvar y2 y
	getangle rot_startang x2 y2
	getactor[THISACTOR].ang rot_startang2
	addvar rot_nextway 1
ends

state se64_initengine //zvel = 3
	setvar moving_vel 0
	setvar acc_count 0
	setvar se_count 0
	ifvare rot_turnway 0 setvar rot_turnway 1 else setvar rot_turnway 0
	
	ifvare doorsound_open2 0 
		setvarvar doorsound_open2 doorsound_open
	ifvare doorsound_close2 0 
		setvarvar doorsound_close2 doorsound_close
ends

///// END PREPARE MOVING VARIABLES //////

state se64_slide
	ifvarvarl se_count se_endtick1
	{
		ifvare moving_var 1 { setvarvar vector_x startx setvarvar vector_y starty } else 
		{ setvarvar vector_x endx setvarvar vector_y endy }
					
		setvarvar x vector_x
		setvarvar y vector_y
		getactor[THISACTOR].ang ang 
		cos vector_x ang
		sin vector_y ang
		state se_calcvel1
		setvarvar vector2_x vector_x
		setvarvar vector2_y vector_y
		mulvarvar vector2_x acc_defvel
		mulvarvar vector2_y acc_defvel
		mulvarvar vector_x buffer_tmp
		mulvarvar vector_y buffer_tmp
		divvarvar vector_x TRIG_MAX
		divvarvar vector_y TRIG_MAX
		divvarvar vector2_x TRIG_MAX
		divvarvar vector2_y TRIG_MAX
					
		ifvare moving_var 1 {
			subvarvar x vector_x
			subvarvar y vector_y
		} else {
			addvarvar x vector_x
			addvarvar y vector_y 
		}		
		setactor[THISACTOR].x x
		setactor[THISACTOR].y y
	}
	else 
	{
		ifvarg autoclose_delay 0 setvar activate_autoclose 1
		setvar active 0
		ifvarg doorsound_close 0 { stopsoundvar doorsound_open 
		soundvar doorsound_close }
		setvar rot_moving 0
	}
ends

state se64_rotator
	ifvarvarl se_count se_endtick1
	{
		getactor[THISACTOR].ang ang
		ifvare rot_turnway 0 
		{ 
		setvarvar acc_speed1 rot_angspeed
		state se_calcvel1
		setvarvar ang rot_startang
		addvarvar ang buffer_tmp
		ifvarg ang 2047 subvar ang 2048
		setactor[THISACTOR].ang ang
		} else 
		ifvare rot_turnway 1 
		{ 
		setvarvar acc_speed1 rot_angspeed
		state se_calcvel1
		setvarvar ang rot_startang
		subvarvar ang buffer_tmp
		ifvarl ang 0 addvar ang 2048
		setactor[THISACTOR].ang ang
		}
	} else 
	{ 
		ifvare rot_turnway 1 { 
		ifvarg doorsound_close 0 stopsoundvar doorsound_open soundvar doorsound_close } else 
		{ ifvarg doorsound_close2 0 stopsoundvar doorsound_open2 ifvare se64_msf 1 soundvar doorsound_close2 else 
		{ stopsoundvar doorsound_open soundvar doorsound_close } }
		setvar rot_moving 0
		setvar se_count 0
		setvar acc_count 0
		setvar buffer_speed 0
		setvar moving_vel 0
		ifvare rot_varmax 1 ifvarg autoclose_delay 0 { setvar activate_autoclose 1 setvar COUNTER 0 }
	}
ends

state se64_zvel2
	ifvarvarl se_count se_endtick1
	{	
		setvarvar vector_x startx 
		setvarvar vector_y starty
		setvarvar x vector_x
		setvarvar y vector_y
		cos vector_x rot_startang
		sin vector_y rot_startang
		state se_calcvel1
		setvarvar vector2_x vector_x
		setvarvar vector2_y vector_y
		mulvarvar vector2_x acc_defvel
		mulvarvar vector2_y acc_defvel
		mulvarvar vector_x buffer_tmp
		mulvarvar vector_y buffer_tmp
		divvarvar vector_x TRIG_MAX
		divvarvar vector_y TRIG_MAX
		divvarvar vector2_x TRIG_MAX
		divvarvar vector2_y TRIG_MAX		
		addvarvar x vector_x
		addvarvar y vector_y
		setactor[THISACTOR].x x
		setactor[THISACTOR].y y
	} else orvar se_zvel2stop 1
	ifvarvarl se_count se_endtick2
	{
			getactor[THISACTOR].ang ang
			ifvarvarg se_count acc_atime2 ifvarvarl se_count acc_vtime2 setvar moving_vel2 1 else
			ifvarvarg se_count acc_vtime2 { ifvarand acc_extravar 2 nullop else setvar moving_vel2 2 }

			ifvare moving_vel2 0 //ускорение
			{
				setvarvar acc_var acceleration2
				mulvarvar acc_var se_count //ускорение * время = скорость 
				setvarvar acc_defvel2 acc_var
			} else 
			ifvare moving_vel2 1 
			{ 
				setvarvar acc_defvel2 acc_speed2

				ifvarg acc_extravar 0 
				ifvarand acc_extravar 2 {
				ifvarvare se_count acc_vtime2 {
				ifvarand dx_case2 1 addvarvar acc_defvel2 buff_dx2 else 
					subvarvar acc_defvel2 buff_dx2 } } else
				ifvarvarl speed_dx2 buff_dx2 {
					ifvare dx_case2 0 subvarvar acc_defvel2 1 else 
						ifvare dx_case2 1 addvarvar acc_defvel2 1  
					addvarvar speed_dx2 1
				}	
				mulvarvar acc_defvel2 buffer_multiple
			} else
			ifvare moving_vel2 2 //замеделение
			{
				setvar speed_dx2 0 //переменная внесения поправки
				addvar acc_count2 1
				setvarvar acc_var acc_speed2
				mulvarvar acc_var buffer_multiple
				setvarvar acc_var2 acceleration2
				mulvarvar acc_var2 -1
				mulvarvar acc_var2 acc_count2
				addvarvar acc_var acc_var2
				ifvarand dx_case2 2 {
					setvarvar acc_var2 buff_dx2
					mulvarvar acc_var2 buffer_multiple
					ifvare acc_count2 1 { ifvarand dx_case2 1 addvarvar acc_var acc_var2 else
						subvarvar acc_var acc_var2 }
				}
				setvarvar acc_defvel2 acc_var 
			}
			ifvare rot_turnway 1 
				mulvarvar acc_defvel2 -1
			addvarvar buffer_speed2  acc_defvel2
			setvarvar buffer_tmp2 buffer_speed2 
			divvarvar buffer_tmp2 buffer_multiple
			setvarvar ang rot_startang2
			addvarvar ang buffer_tmp2
			ifvarg ang 2047 subvar ang 2048
			setactor[THISACTOR].ang ang
	} else orvar se_zvel2stop 2
	ifvare se_zvel2stop 3
	{
		ifvarg mover_position 0 setactor[THISACTOR].ang mov_gpang else 
			setactor[THISACTOR].ang rot_saveang
		ifvarg autoclose_delay 0 setvar activate_autoclose 1
		setvar active 0
		ifvarg doorsound_close 0 stopsoundvar doorsound_open 
		soundvar doorsound_close 
		setvar rot_moving 0
		
		setvar se_zvel2stop 0
		setvar se_count 0
		setvar acc_count 0
		setvar buffer_speed 0
		setvar moving_vel 0
		setvar acc_count2 0
		setvar buffer_speed2 0
		setvar moving_vel2 0
		ifvarvare rot_nextway rot_varmax { ifvare moving_var 0 setvar moving_var 1 else setvar moving_var 0 setvar rot_nextway 0 }
	}
ends

state se64_engine

	ifvare active 1 { setvar active 0 setvar moving_vel 2 }			
	
	ifvare moving_vel 0 {
		setvarvar acc_var acceleration
		mulvarvar acc_var se_count //ускорение * время = скорость 
		setvarvar acc_defvel acc_var
		ifvarvarg acc_defvel rot_angspeed 
			setvar moving_vel 1
		setvarvar rot_tmp3 acc_defvel
	}	
	ifvare moving_vel 1 {
		setvarvar acc_defvel rot_angspeed
		setvarvar rot_tmp3 acc_defvel
	}
	ifvare moving_vel 2 {
		addvar acc_count 1
		setvarvar acc_var rot_tmp3
		setvarvar acc_var2 acceleration
		mulvarvar acc_var2 -1
		mulvarvar acc_var2 acc_count
		addvarvar acc_var acc_var2
		setvarvar acc_defvel acc_var 	
		ifvarvarl acc_defvel 1 {
			ifvare rot_turnway 0 {
				ifvarg doorsound_close2 0 soundvar doorsound_close2
				stopsoundvar doorsound_open2
			} else {
				ifvarg doorsound_close 0 soundvar doorsound_close
				stopsoundvar doorsound_open
			}
			setvar rot_moving 0
		}
	}
	getactor[THISACTOR].ang ang
	ifvare rot_turnway 0 { 
		addvarvar ang acc_defvel 
		ifvarg ang 2047 subvar ang 2048 
	} else 
	ifvare rot_turnway 1 { 
		subvarvar ang acc_defvel 
		ifvarl ang 0 addvar ang 2048 
	}
	setactor[THISACTOR].ang ang
		
ends



//////////////// LOAD SECTOREFFECTOR //////////////////////

state se_loadactor
	ifvare lotag 64 // slider door / rotator door SECTOREFFECTOR
	{
		state se64_loadoptions // while with sprites scan
		state se64_wallpointsaving // while with sectors scan
		
		ifvare sprite[THISACTOR].zvel 0 //слайд сектор
		{
			//задание начальных и конечных координат
				
				getactor[THISACTOR].x startx
				getactor[THISACTOR].y starty
				getactor[THISACTOR].ang ang
				addvar ang 1024
					
				setvarvar x2 startx
				addvarvar x2 sectordist
				rotatepoint startx starty x2 starty ang endx endy
				setvarvar acc_dist sectordist
				setvarvar acc_speed1 sectorspeed
				
				setvarvar buffer_multiple 1000
				state init_calcvel
		} else
		ifvare sprite[THISACTOR].zvel 1 //поворотный сектор
		{	
			getactor[THISACTOR].ang ang
			setvarvar rot_initang ang
		} else 
		ifvare sprite[THISACTOR].zvel 2 //слайд сектор с движением до mover_point
		{
			getactor[THISACTOR].ang ang
			setvarvar rot_initang ang
		}
	}
	ifvare lotag 68 { //Floating wave
		getactor[THISACTOR].sectnum se_sect1
		ifvarand sector[se_sect1].floorstat 1024 { //TROR определение смежного TROR сектора
			setvarvar tmp sector[se_sect1].floorz
			addvar tmp 512 //способ заглянуть под пол верхнего сектора
			updatesectorz sprite[THISACTOR].x sprite[THISACTOR].y tmp se_trorsect
			ifvarvarn se_trorsect se_sect1 ifvarn se_trorsect -1 setvar se_hastror 1
		}
	}
	ifvare lotag 69 //floorslope_mover
	{	
	
		getactor[THISACTOR].sectnum se_sect1
		getsector[se_sect1].floorz se_startz
		getactor[THISACTOR].z se_endz
			
		ifvarand sector[se_sect1].floorstat 1024 { //TROR определение смежного TROR сектора
			setvarvar tmp sector[se_sect1].floorz
			addvar tmp 1 //способ заглянуть под пол верхнего сектора
			updatesectorz sprite[THISACTOR].x sprite[THISACTOR].y tmp se_trorsect
			ifvarn se_trorsect -1 setvar se_hastror 1
		}

		getactor[THISACTOR].zvel se_zvel
		setvar spriteid 0
		whilevarvarn spriteid NUMSPRITES
		{
			getactor[spriteid].picnum picnum
			ifvare picnum MOVER_POINT
			{
				getactor[spriteid].xvel rx_can
				getactor[THISACTOR].xvel tx_can
			
				ifvarvare tx_can rx_can 
				{
					getactor[spriteid].lotag lotag
					getactor[spriteid].hitag hitag
				
					ifvarvarl se_startz se_endz 
					{ 
						setvarvar se_z se_startz 
						setvarvar tmp se_endz
						subvarvar tmp se_z 
						setvarvar se_z tmp 
					} else 
					{ 
						setvarvar se_z se_endz 
						setvarvar tmp se_startz
						subvarvar tmp se_z 
						setvarvar se_z tmp 
					}
					
					setvarvar acc_dist se_z
					
					ifvarl lotag 1 setvar lotag 32767
					ifvarl hitag 1 setvar hitag 32767
					setvarvar acc_speed1 lotag
					setvarvar acc_speed2 hitag
					setvarvar buffer_multiple 10
					state init_calcvel
			
					ifvare sprite[THISACTOR].ang 512 {
						setsector[se_sect1].floorz se_endz
						ifvare se_hastror 1 setsector[se_trorsect].ceilingz se_endz
						setvar moving_var 1
					}
				}
			} else
			ifvare picnum MUSICANDSFX
			{
				getactor[spriteid].xvel rx_can
				getactor[THISACTOR].xvel tx_can
				getactor[spriteid].zvel zvel
				ifvarvare tx_can rx_can
				{
					ifvare zvel 0 { setvarvar doorsound_open sprite[spriteid].lotag setvarvar doorsound_close sprite[spriteid].hitag }
					ifvare zvel 1 { setvarvar doorsound_open2 sprite[spriteid].lotag setvarvar doorsound_close2 sprite[spriteid].hitag }
				}
			} else
			ifvare picnum SECTOREFFECTOR
			{
				getactor[spriteid].xvel rx_can
				getactor[THISACTOR].xvel tx_can
				ifvarvare tx_can rx_can { ifvare sprite[spriteid].lotag 10 { setvarvar autoclose_delay sprite[spriteid].hitag } }				
			} 
			addvar spriteid 1
		}
		
		setvar spriteid 0
		whilevarvarn spriteid NUMSPRITES
		{
			getactor[spriteid].picnum picnum
			ifvarn picnum SECTOREFFECTOR
			ifvarn picnum APLAYER {
				ifvarg se_owner 0 
				ifvarvare se_owner actorvar[spriteid].mover_rid
				{
					ifvarn picnum BL_BUTTON1
					ifvarn picnum BL_BUTTON1ON
					ifvarn picnum BL_BUTTON2
					ifvarn picnum BL_BUTTON3
					ifvarn picnum BL_BUTTON4
					ifvarn picnum BL_BUTTON5
					ifvarn picnum BL_BUTTON6
					ifvarn picnum LIGHT_SWITCH
					ifvarn picnum WHEEL
					ifvarn picnum EXITBUTTON
					ifvarn picnum BL_MULTISWITCH
					ifvarn picnum MULTIBUTTON
					ifvare sprite[spriteid].statnum 0 
						changespritestat spriteid 1

					ifvare sprite[THISACTOR].ang 512 { 
						getactor[spriteid].z z
						subvarvar z acc_dist
						setactor[spriteid].z z
					}
				}
			}
			addvar spriteid 1
		}
	}
	ifvare lotag 70 //ceilingslope_mover
	{
		getactor[THISACTOR].sectnum se_sect1
		getsector[se_sect1].ceilingz se_startz
		getactor[THISACTOR].z se_endz
		
		getactor[THISACTOR].zvel se_zvel
			
		setvar spriteid 0
		whilevarvarn spriteid NUMSPRITES
		{
			getactor[spriteid].picnum picnum
			ifvare picnum MOVER_POINT
			{
				getactor[spriteid].xvel rx_can
				getactor[THISACTOR].xvel tx_can
				ifvarvare tx_can rx_can 
				{
					getactor[spriteid].lotag lotag
					getactor[spriteid].hitag hitag
				
					ifvarvarl se_startz se_endz 
					{ 
						setvarvar se_z se_startz 
						setvarvar tmp se_endz
						subvarvar tmp se_z 
						setvarvar se_z tmp 
					} else 
					{ 
						setvarvar se_z se_endz 
						setvarvar tmp se_startz
						subvarvar tmp se_z 
						setvarvar se_z tmp 
					}
					
					
					setvarvar acc_dist se_z
					
					ifvarl lotag 1 setvar lotag 32767
					ifvarl hitag 1 setvar hitag 32767
					setvarvar acc_speed1 lotag
					setvarvar acc_speed2 hitag
					setvarvar buffer_multiple 10
					state init_calcvel
					
					getactor[THISACTOR].ang se_ang
					ifvare se_ang 512
					{
						setsector[se_sect1].ceilingz se_endz 
						setvar moving_var 1
					}
				}
			} else
			ifvare picnum MUSICANDSFX
			{
				getactor[spriteid].xvel rx_can
				getactor[THISACTOR].xvel tx_can
				getactor[spriteid].zvel zvel
				ifvarvare tx_can rx_can
				{
					ifvare zvel 0 { setvarvar doorsound_open sprite[spriteid].lotag setvarvar doorsound_close sprite[spriteid].hitag }
					ifvare zvel 1 { setvarvar doorsound_open2 sprite[spriteid].lotag setvarvar doorsound_close2 sprite[spriteid].hitag }
				}
			} else
			ifvare picnum SECTOREFFECTOR
			{
				getactor[spriteid].xvel rx_can
				getactor[THISACTOR].xvel tx_can
				ifvarvare tx_can rx_can { ifvare sprite[spriteid].lotag 10 { setvarvar autoclose_delay sprite[spriteid].hitag } }		
			}
			addvar spriteid 1
		}
		
		setvar spriteid 0
		whilevarvarn spriteid NUMSPRITES
		{
			getactor[spriteid].picnum picnum
			ifvarn picnum SECTOREFFECTOR
			ifvarn picnum APLAYER {
				ifvarg se_owner 0 
				ifvarvare se_owner actorvar[spriteid].mover_rid {
					ifvarn picnum BL_BUTTON1
					ifvarn picnum BL_BUTTON1ON
					ifvarn picnum BL_BUTTON2
					ifvarn picnum BL_BUTTON3
					ifvarn picnum BL_BUTTON4
					ifvarn picnum BL_BUTTON5
					ifvarn picnum BL_BUTTON6
					ifvarn picnum LIGHT_SWITCH
					ifvarn picnum WHEEL
					ifvarn picnum EXITBUTTON
					ifvarn picnum BL_MULTISWITCH
					ifvarn picnum MULTIBUTTON {
						ifvare sprite[spriteid].statnum 0 
							changespritestat spriteid 1
					}

					ifvare sprite[THISACTOR].ang 512 { 
						getactor[spriteid].z z
						subvarvar z acc_dist
						setactor[spriteid].z z
					}
				}
			}
			addvar spriteid 1
		}	
	}
	ifvare lotag 71 {
		
		ifvare sprite[THISACTOR].owner 1 {
			getactor[THISACTOR].sectnum sectnum
			getsector[sectnum].floorslope acc_dist 
			ifvarl acc_dist 0 { mulvar acc_dist -1 } else 
				setvar moving_var 1
			
			setsector[sectnum].floorslope 0
		} else { 
			getactor[THISACTOR].zvel acc_dist
			getactor[THISACTOR].ang se_ang
			ifvare se_ang 512
				setvar moving_var 1	
		}
		
		getactor[THISACTOR].hitag acc_speed1
		setvarvar buffer_multiple 1000
		state init_calcvel		
	}
ends


//////////////// INITIALIZATION COORD FOR SPRITES ///////////////////

state se64_initactor
ifvare se_spriteid 0
{
	setvar spriteid 0
	whilevarvarn spriteid 16384
	{
		getactor[spriteid].picnum picnum
		ifvarn picnum SECTOREFFECTOR
		ifvarn picnum MOVER_POINT
		{
			ifvarvarn spriteid THISACTOR 
			ifvarg se_owner 0 ifvarvare se_owner actorvar[spriteid].mover_rid
			{	
				getactor[spriteid].statnum astatnum
				ifvare astatnum 0 changespritestat spriteid 1
				getactor[spriteid].x x2
				getactor[spriteid].y y2
				getactor[spriteid].z z2
				subvarvar x2 sprite[THISACTOR].x
				subvarvar y2 sprite[THISACTOR].y
				subvarvar z2 sprite[THISACTOR].z
				setactorvar[spriteid].spr_zdist z2
				getangle rot_ang x2 y2
				mulvarvar x2 x2
				mulvarvar y2 y2
				addvarvar x2 y2
				sqrt x2 rot_dist 
				setactorvar[spriteid].spr_pndist rot_dist
				getactor[THISACTOR].ang ang
				subvarvar rot_ang ang
				setactorvar[spriteid].spr_pnang rot_ang
				getactor[spriteid].ang tmp
				subvarvar tmp ang
				setactorvar[spriteid].rot_actorang tmp
			}
		}
		addvar spriteid 1
	}
	setvar se_spriteid 1
}
ends

state se69-70_initactor
ifvare se_spriteid 0
{
	setvar spriteid 0
	whilevarvarn spriteid 16384
	{
		getactor[spriteid].picnum picnum
		ifvarn picnum SECTOREFFECTOR
		ifvarn picnum APLAYER
		{
			ifvarg se_owner 0 
			ifvarvare se_owner actorvar[spriteid].mover_rid
			{
				getactor[spriteid].statnum astatnum
				getactor[THISACTOR].ang se_ang
				ifvare astatnum 0 changespritestat spriteid 1
				getactor[THISACTOR].sectnum se_sect1
				getactor[spriteid].z se_z1 
				ifvare lotag 69 getsector[se_sect1].floorz se_z2 
					else getsector[se_sect1].ceilingz se_z2
				subvarvar se_z1 se_z2
				
				/*
				ifvare lotag 69 ifvare se_ang 512 
				{ 
					subvarvar se_z1 acc_dist
					getactor[spriteid].z z
					subvarvar z acc_dist
					setactor[spriteid].z z
				} else
				ifvare lotag 70 ifvare se_ang 512 
				{ 
					addvarvar se_z1 acc_dist
					getactor[spriteid].z z
					addvarvar z acc_dist
					setactor[spriteid].z z
				}
				*/
				setactorvar[spriteid].savez se_z1
			}
		}
		addvar spriteid 1
	}
	setvar se_spriteid 1
}
ends

//////////////// EVENTGAME SECTOREFFECTOR /////////////////

state se_eventgame
	/* zvel = 0 - слайд сектор
	zvel = 1 - поворотный сектор
	zvel = 2 - слайд сектор с движением до mover_point
	zvel = 3 - бесконечное вращение сектора */

	ifvare lotag 64  // slider/rotator SE
	{
		state se64_initactor //задание прикрепленного спрайта
		
		ifvare active 1 ifvare rot_moving 0
		{
			ifvare se_zvel 0 state se64_initslide else 
			ifvare se_zvel 1 state se64_initrotator else
			ifvare se_zvel 2 state se64_initzvel2 else
			ifvare se_zvel 3 state se64_initengine

			addvar rot_moving 1
			setvar COUNTER 0 // счетчик автозакрытия
			setvar active 0
		} else ifvarn se_zvel 3 setvar active 0 // активация во время движения
		
		ifvare rot_moving 1
		{	
			addvar se_count 1
			ifvare se_zvel 1 { 
				ifvare rot_turnway 1 { ifvare se64_sound 0 { soundoncevar doorsound_open setvar se64_sound 1 } } else { 
					ifvarg doorsound_open2 0 { ifvare se64_sound 0 { soundoncevar doorsound_open2 setvar se64_sound 1 } } else 
					ifvare se64_msf 0 { ifvare se64_sound 0 { soundoncevar doorsound_open setvar se64_sound 1 } }
				} 
			} else
			ifvare se_zvel 3 { ifvare rot_turnway 1 soundoncevar doorsound_open else { ifvarg doorsound_open2 0 soundoncevar doorsound_open2 else soundoncevar doorsound_open } } else
			soundoncevar doorsound_open
			
			ifvare se_zvel 0 state se64_slide else
			ifvare se_zvel 1 state se64_rotator else 
			ifvare se_zvel 2 state se64_zvel2 else
			ifvare se_zvel 3 state se64_engine
				
			//перемещение секторов
			
			
			setvar rot_tmp 0
			whilevarvarn rot_tmp NUMSECTORS
			{
			getsector[rot_tmp].hitag hitag
			ifvarvare hitag se_hitag
			{
				getsector[rot_tmp].wallnum rot_wallnum
				getsector[rot_tmp].wallptr rot_wallptr
				setvar rot_tmp2 0
				whilevarvarn rot_tmp2 rot_wallnum
				{ 	
					sectsetinterpolation rot_tmp
					getactor[THISACTOR].ang ang
					addvarvar ang ROT_PNANG[rot_wallptr]
									
					cos wall_rotx ang
					mulvarvar wall_rotx ROT_PNDIST[rot_wallptr]
					divvar wall_rotx 16384
						
					sin wall_roty ang
					mulvarvar wall_roty ROT_PNDIST[rot_wallptr]
					divvar wall_roty 16384
					getactor[THISACTOR].x x
					addvarvar wall_rotx x
					getactor[THISACTOR].y y
					addvarvar wall_roty y				
					dragpoint rot_wallptr wall_rotx wall_roty
					addvar rot_wallptr 1
					addvar rot_tmp2 1
				}
			}
			addvar rot_tmp 1
			}


			//перемещение спрайтов
			//ifvare se_pal 0
			{
				ifvare rot_turnway 1 
					mulvarvar acc_defvel -1
				cos mycos acc_defvel
				sin mysin acc_defvel
				setvar sectorid 0
				whilevarvarn sectorid NUMSECTORS 
				{
					headspritesect spriteid sectorid
					whilevarn spriteid -1 
					{
						getactor[spriteid].picnum picnum
						getactor[spriteid].sectnum se_sector
						getactor[spriteid].z z
						ifvarg se_sector -1 ifvarl se_sector 4096 { getflorzofslope se_sector 0 0 mz } else getactor[spriteid].z mz
						subvarvar mz z

						ifvarg se_sector -1 ifvarl se_sector 4096 getsector[se_sector].hitag hitag
						ifvarn picnum SECTOREFFECTOR
						ifvarn picnum MOVER_POINT
						ifvarvarn spriteid THISACTOR 
						{
							ifvarg se_owner 0 ifvarvare se_owner actorvar[spriteid].mover_rid
							{								
								getactorvar[spriteid].spr_pndist spr_pndist
								getactorvar[spriteid].spr_pnang spr_pnang	
									
								getactor[THISACTOR].ang ang
								addvarvar ang spr_pnang
																
								cos wall_rotx ang
								mulvarvar wall_rotx spr_pndist
								divvar wall_rotx 16384
								sin wall_roty ang
								mulvarvar wall_roty spr_pndist
								divvar wall_roty 16384
													
								getactor[THISACTOR].x x
								addvarvar wall_rotx x
								getactor[THISACTOR].y y
								addvarvar wall_roty y				
								setactor[spriteid].x wall_rotx
								setactor[spriteid].y wall_roty
								ifvare se_zvel 1 {
								getactor[THISACTOR].ang ang
								getactorvar[spriteid].rot_actorang tmp
								addvarvar ang tmp
								setactor[spriteid].ang ang }	
							} else ifvare se_pal 0
							ifvarvare hitag se_hitag
							//ifvarl actorvar[spriteid].mover_rid 1
							{
								//ifvarl mz 2048
								{	
									getactor[spriteid].statnum astatnum
									ifvare astatnum 0 changespritestat spriteid 1
									ifvare se_zvel 0
									{
										getactor[spriteid].x x
										getactor[spriteid].y y
										ifvare picnum APLAYER {
											getplayer[THISACTOR].posx x
											getplayer[THISACTOR].posy y }
										ifvare se_zvel 0 
										setvarvar acc_var vector2_x
										setvarvar acc_var2 vector2_y
										ifvare moving_var 1 {
										mulvar acc_var -1
										mulvar acc_var2 -1
										} 
										addvarvar x acc_var
										addvarvar y acc_var2
										setactor[spriteid].x x
										setactor[spriteid].y y
										ifvare picnum APLAYER {
										setplayer[THISACTOR].posx x
										setplayer[THISACTOR].posy y }
									} else
									ifvare se_zvel 2
									{
									getactor[spriteid].x x
									getactor[spriteid].y y
									ifvare picnum APLAYER {
										getplayer[THISACTOR].posx x
										getplayer[THISACTOR].posy y }
									addvarvar x vector2_x
									addvarvar y vector2_y
									setactor[spriteid].x x
									setactor[spriteid].y y
									ifvare picnum APLAYER {
									setplayer[THISACTOR].posx x
									setplayer[THISACTOR].posy y }
									} else
									{
									getactor[THISACTOR].x x
									getactor[THISACTOR].y y
									getactor[spriteid].x x2
									getactor[spriteid].y y2
									subvarvar x2 x
									setvarvar dx x2
									mulvarvar dx mycos
									divvarvar dx TRIG_MAX
									subvarvar y2 y
									setvarvar dy y2
									mulvarvar dy mysin
									divvarvar dy TRIG_MAX
									addvarvar x dx
									subvarvar x dy
									setactor[spriteid].x x
									ifvare picnum APLAYER 
										setplayer[THISACTOR].posx x
									setvarvar dy y2
									mulvarvar dy mycos
									divvarvar dy TRIG_MAX
									setvarvar dx x2
									mulvarvar dx mysin
									divvarvar dx TRIG_MAX
									addvarvar y dy
									addvarvar y dx
									setactor[spriteid].y y
									ifvare picnum APLAYER 
										setplayer[THISACTOR].posy y
									ifvare picnum APLAYER 
										getplayer[THISACTOR].ang se_ang else
											getactor[spriteid].ang se_ang
									addvarvar se_ang acc_defvel
									ifvare picnum APLAYER 
										setplayer[THISACTOR].ang se_ang else
											setactor[spriteid].ang se_ang
									}
								}
							} 
						}
						nextspritesect spriteid spriteid
					}
					addvar sectorid 1
				}	
			}	
		} else setvar se64_sound 0
		
		ifvare activate_autoclose 1
		{
			ifvare se_zvel 0
			ifvare moving_var 1
			{
				addvar COUNTER 1
				ifvarvarg COUNTER autoclose_delay { 
					cos mycos sprite[THISACTOR].ang
					sin mysin sprite[THISACTOR].ang
					hitscan sprite[THISACTOR].x sprite[THISACTOR].y player[THISACTOR].posz player[THISACTOR].cursectnum mycos mysin 0 RETURN RETURN HITSPRITE RETURN RETURN RETURN CLIPMASK1
					
					ifvarn HITSPRITE -1 ifvare sprite[HITSPRITE].picnum APLAYER
						setvar se_close 1
					else setvar se_close 0
						
					ifvare se_close 0 {
						ifvare status 0 
							setvar status 1 
						else setvar status 0 
						setvar active 1 
						setvar COUNTER 0 
						setvar activate_autoclose 0 
					} else setvar COUNTER 0
				}
			} else ifvare se_zvel 1
			ifvare rot_nextway 1
			{
				addvar COUNTER 1
				ifvarvarg COUNTER autoclose_delay { ifvare status 0 setvar status 1 else setvar status 0 setvar active 1 setvar COUNTER 0 setvar activate_autoclose 0 }
			}
		}
	}
	ifvare lotag 69 //floorslope_mover
	{	
		ifvare doorsound_open2 0 setvarvar doorsound_open2 doorsound_open
		ifvare doorsound_close2 0 setvarvar doorsound_close2 doorsound_close
		
		ifvare active 1 ifvare rot_moving 0
		{	
			state se69-70_initactor
			setvar se_count 0
			setvar acc_count 0
			setvar buffer_speed 0

			setvar moving_vel 0
			ifvare moving_var 0 setvar moving_var 1 else setvar moving_var 0
			setvar rot_moving 1

			setvar active 0
		}
		
		ifvare rot_moving 1
		{
			getactor[THISACTOR].sectnum se_sect1
			sectsetinterpolation se_sect1
			ifvare moving_var 1
			{
				ifvarvarl se_count se_endtick1
				{
					addvarvar se_count 1
					ifvarg doorsound_open 0 
						soundoncevar doorsound_open
					state se_calcvel1
					setvarvar se_z se_startz
					subvarvar se_z buffer_tmp
					setsector[se_sect1].floorz se_z
					ifvare se_hastror 1 setsector[se_trorsect].ceilingz se_z
				} else 
				{ 
					ifvare moving_var 1 ifvarg autoclose_delay 0 { setvar activate_autoclose 1 setvar COUNTER 0 }
					ifvarg doorsound_close 0 { stopsoundvar doorsound_open soundvar doorsound_close }
					setvar rot_moving 0
				}
			} 
			else
			{
				ifvarvarl se_count se_endtick2
				{
					addvarvar se_count 1
					ifvarg doorsound_open2 0 
						soundoncevar doorsound_open2
					
					state se_calcvel2
					setvarvar se_z se_endz
					addvarvar se_z buffer_tmp
					setsector[se_sect1].floorz se_z
					ifvare se_hastror 1 setsector[se_trorsect].ceilingz se_z
				} else 
				{ 
					ifvare moving_var 0 ifvarg autoclose_delay 0 { setvar activate_autoclose 1 setvar COUNTER 0 }
					ifvarg doorsound_close2 0 { stopsoundvar doorsound_open2 soundvar doorsound_close2 }
					setvar rot_moving 0
				}
			
			}
		
			setvar sectorid 0
			whilevarvarn sectorid NUMSECTORS 
			{
				headspritesect spriteid sectorid
				whilevarn spriteid -1 
				{
					ifvarg se_owner 0
					ifvarvare actorvar[spriteid].mover_rid se_owner
					{
						getactor[THISACTOR].sectnum tmp
						getsector[tmp].floorz se_z2
						addvarvar se_z2 actorvar[spriteid].savez
						setactor[spriteid].z se_z2	
					}
					nextspritesect spriteid spriteid
				}
				addvar sectorid 1
			}
			
			getactor[THISACTOR].sectnum sectorid //перемещение врагов внутри лифта
			headspritesect spriteid sectorid
			whilevarn spriteid -1 {
				switch sprite[spriteid].picnum
				//case ZOMBIE
				case EZOMBIEFALL
				case HERETIC
				case FISH
				case SPIDER
				case PHANTASM
				//case CIVILIAN
				case HELLDOG
				case GILLBEAST
				case GARGOYLE
				case BUTCHER
				case FATRAT
				case EVILHAND
				case BOSSSPIDER
				case BAT
				//case APLAYER
				case CERBERUS
				//case TCHERNOBOG
				case POD
				case TENTACLE
				//case BEAST
				case BLOODPOOL2
					getflorzofslope sectorid x y z 
					setactor[spriteid].z z
				break
				endswitch
				nextspritesect spriteid spriteid
			}
		}
		ifvare activate_autoclose 1
		{
			getactor[THISACTOR].sectnum tmp
			ifvarvare player[THISACTOR].cursectnum tmp {
				ifvarvare se_endz sector[tmp].ceilingz
					setvar COUNTER 0
			}

			getactor[THISACTOR].ang ang
			ifvare ang 1536 ifvare moving_var 1
			{
				addvar COUNTER 1
				ifvarvarg COUNTER autoclose_delay { ifvare status 0 setvar status 1 else setvar status 0 setvar active 1 setvar COUNTER 0 setvar activate_autoclose 0 } 
			} else ifvare ang 512 ifvare moving_var 0
			{
				addvar COUNTER 1
				ifvarvarg COUNTER autoclose_delay { ifvare status 0 setvar status 1 else setvar status 0 setvar active 1 setvar COUNTER 0 setvar activate_autoclose 0 }
			}
		}

			ifvarand se_zvel 1
			{
				
				getactor[THISACTOR].sectnum rot_tmp
				getsector[rot_tmp].wallnum rot_wallnum
				getsector[rot_tmp].wallptr rot_wallptr
				setvarvar se_ptr rot_wallptr
				setvar rot_tmp2 0
				whilevarvarn rot_tmp2 rot_wallnum
				{ 
					getwall[rot_wallptr].nextsector se_sector
					ifvarn se_sector -1 getsector[se_sector].hitag hitag else setvar hitag 0

					ifvarvare hitag se_hitag 
					{
						setvarvar se_twall rot_wallptr
						state length_calc //variable se_len
					}
					addvar rot_wallptr 1
					addvar rot_tmp2 1
				}
					
				ifvarg se_len 0 {
				getwall[se_twall].nextsector sectnum
				getwall[wallptr].x x
				getwall[wallptr].y y
				ifvarn sectnum -1 getflorzofslope sectnum x y z 
				setvarvar se_tmp se_len
				mulvarvar se_tmp 10
				divvar se_tmp 256
				getactor[THISACTOR].sectnum sect2
				getsector[sect2].floorz z2
				subvarvar z z2
				mulvarvar z 10
				divvarvar z se_tmp
				setsector[sect2].floorslope z
				}
			}
	}
	ifvare lotag 70 //ceilingslope_mover
	{
		ifvare doorsound_open2 0 setvarvar doorsound_open2 doorsound_open
		ifvare doorsound_close2 0 setvarvar doorsound_close2 doorsound_close
		
		ifvare active 1 ifvare rot_moving 0
		{	
			state se69-70_initactor
			setvar se_count 0
			setvar acc_count 0
			setvar buffer_speed 0

			setvar moving_vel 0
			ifvare moving_var 0 setvar moving_var 1 else setvar moving_var 0
			setvar rot_moving 1

			setvar active 0
		}
		ifvare rot_moving 1
		{
			getactor[THISACTOR].sectnum se_sect1
			sectsetinterpolation se_sect1

			ifvare moving_var 1
			{
				ifvarvarl se_count se_endtick1
				{
					addvarvar se_count 1
					ifvarg doorsound_open 0 
						soundoncevar doorsound_open
					state se_calcvel1
					setvarvar se_z se_startz
					addvarvar se_z buffer_tmp
					setsector[se_sect1].ceilingz se_z
				} else 
				{ 
					ifvare moving_var 1 ifvarg autoclose_delay 0 { setvar activate_autoclose 1 setvar COUNTER 0 }
					ifvarg doorsound_close 0 { stopsoundvar doorsound_open soundvar doorsound_close }
					//ifvarvarn se_z se_endz setsector[se_sect1].ceilingz se_endz
					setvar rot_moving 0
				}
			} 
			else
			{
				ifvarvarl se_count se_endtick2
				{
					addvarvar se_count 1
					ifvarg doorsound_open2 0 
						soundoncevar doorsound_open2
					
					state se_calcvel2
					setvarvar se_z se_endz
					subvarvar se_z buffer_tmp
					setsector[se_sect1].ceilingz se_z
				} else 
				{ 
					ifvare moving_var 0 ifvarg autoclose_delay 0 { setvar activate_autoclose 1 setvar COUNTER 0 }
					ifvarg doorsound_close2 0 { stopsoundvar doorsound_open2 soundvar doorsound_close2 }
					//ifvarvarn se_startz se_z setsector[se_sect1].ceilingz se_startz
					setvar rot_moving 0
				}
			
			}
			setvar sectorid 0
			whilevarvarn sectorid NUMSECTORS 
			{
				headspritesect spriteid sectorid
				whilevarn spriteid -1 
				{
					ifvarg se_owner 0
					ifvarvare actorvar[spriteid].mover_rid se_owner
					{
						getactor[THISACTOR].sectnum tmp
						getsector[tmp].ceilingz se_z2
						addvarvar se_z2 actorvar[spriteid].savez
						setactor[spriteid].z se_z2	
					}
					nextspritesect spriteid spriteid
				}
				addvar sectorid 1
			}
		}
		ifvare activate_autoclose 1
		{
			ifvarvare player[THISACTOR].cursectnum sprite[THISACTOR].sectnum {
				setvar COUNTER 0
			}
			
			getactor[THISACTOR].ang ang
			ifvare ang 1536 ifvare moving_var 1
			{
				addvar COUNTER 1
				ifvarvarg COUNTER autoclose_delay { ifvare status 0 setvar status 1 else setvar status 0 setvar active 1 setvar COUNTER 0 setvar activate_autoclose 0 } 
			} else ifvare ang 512 ifvare moving_var 0
			{
				addvar COUNTER 1
				ifvarvarg COUNTER autoclose_delay { ifvare status 0 setvar status 1 else setvar status 0 setvar active 1 setvar COUNTER 0 setvar activate_autoclose 0 }
			}
		}

		ifvarand se_zvel 1
		{
			getactor[THISACTOR].sectnum rot_tmp
			getsector[rot_tmp].wallnum rot_wallnum
			getsector[rot_tmp].wallptr rot_wallptr
			setvarvar se_ptr rot_wallptr
			setvar rot_tmp2 0
			whilevarvarn rot_tmp2 rot_wallnum
			{ 
				getwall[rot_wallptr].nextsector se_sector
				ifvarn se_sector -1 getsector[se_sector].hitag hitag else setvar hitag 0

				ifvarvare hitag se_hitag 
				{
					setvarvar se_twall rot_wallptr
					state length_calc //variable se_len
				}
				addvar rot_wallptr 1
				addvar rot_tmp2 1
			}
			getwall[se_twall].nextsector sectnum
			getwall[wallptr].x x
			getwall[wallptr].y y
			ifvarn sectnum -1 getceilzofslope sectnum x y z 
			setvarvar se_tmp se_len
			mulvarvar se_tmp 10
			divvar se_tmp 256
			getactor[THISACTOR].sectnum sect2
			getsector[sect2].ceilingz z2
			subvarvar z z2
			mulvarvar z 10
			divvarvar z se_tmp
			setsector[sect2].ceilingslope z
		}
	}
	ifvare lotag 71 {
		ifvare doorsound_open2 0 setvarvar doorsound_open2 doorsound_open
		ifvare doorsound_close2 0 setvarvar doorsound_close2 doorsound_close

		ifvare active 1 ifvare rot_moving 0
		{	
			setvar se_count 0
			setvar acc_count 0
			setvar buffer_speed 0

			setvar moving_vel 0
			ifvare moving_var 0 setvar moving_var 1 else setvar moving_var 0
			setvar rot_moving 1

			setvar active 0
		}
		ifvare rot_moving 1
		{
			ifvarvarl se_count se_endtick1
			{
				addvarvar se_count 1
				ifvarg doorsound_open 0 
					soundoncevar doorsound_open
				state se_calcvel1
				
				getactor[THISACTOR].sectnum sectnum
				getsector[sectnum].floorslope z
				ifvare moving_var 1 subvarvar z acc_defvel else addvarvar z acc_defvel
				setsector[sectnum].floorslope z
			} else 
			{ 
				ifvare moving_var 1 ifvarg autoclose_delay 0 { setvar activate_autoclose 1 setvar COUNTER 0 }
				ifvarg doorsound_close 0 { stopsoundvar doorsound_open soundvar doorsound_close }
				setvar rot_moving 0
			}
		}
	}
ends


/*
zvel = 0 - линейный или угловой мувер
zvel = 1 - бесконечный мувер
*/

eventloadactor MOVER_POINT
	cstat 32768
enda

state mover_init
	state se64_initzvel2
	getactor[THISACTOR].x x
	getactor[THISACTOR].y y
	getactor[THISACTOR].z z
	ifvarg mover_position 0 {
	setvarvar vector_x mov_coordx
	setvarvar vector_y mov_coordy
	setvarvar vector_z mov_coordz } 
	else { 
		setvarvar vector_x rot_savex
		setvarvar vector_y rot_savey
		setvarvar vector_z rot_savez }
	subvarvar vector_x x
	subvarvar vector_y y
	subvarvar vector_z z
	shiftvarr vector_z 4
	setvarvar in1 vector_x
	setvarvar in2 vector_y
	setvarvar in3 vector_z
	state vec3d_length
	shiftvarl vector_z 4
	mulvar vector_x TRIG_MAX
	mulvar vector_y TRIG_MAX
	mulvar vector_z TRIG_MAX
	ifvarg out1 0 {
	divvarvar vector_x out1
	divvarvar vector_y out1
	divvarvar vector_z out1 }
ends

state mover_action
	ifvarvarl se_count se_endtick1
	{	
		setvarvar x startx
		setvarvar y starty
		setvarvar z startz
		setvarvar vector2_x vector_x
		setvarvar vector2_y vector_y
		setvarvar vector2_z vector_z
		state se_calcvel1
		mulvarvar vector2_x buffer_tmp
		mulvarvar vector2_y buffer_tmp
		mulvarvar vector2_z buffer_tmp
		divvar vector2_x TRIG_MAX
		divvar vector2_y TRIG_MAX
		divvar vector2_z TRIG_MAX
		addvarvar x vector2_x
		addvarvar y vector2_y
		addvarvar z vector2_z
		setactor[THISACTOR].x x
		setactor[THISACTOR].y y
		setactor[THISACTOR].z z
	} 
	else orvar se_zvel2stop 1
	ifvarvarl se_count se_endtick2
	{
			getactor[THISACTOR].ang ang
			ifvarvarg se_count acc_atime2 ifvarvarl se_count acc_vtime2 setvar moving_vel2 1 else
			ifvarvarg se_count acc_vtime2 { ifvarand acc_extravar 2 nullop else setvar moving_vel2 2 }

			ifvare moving_vel2 0 //ускорение
			{
				setvarvar acc_var acceleration2
				mulvarvar acc_var se_count //ускорение * время = скорость 
				setvarvar acc_defvel2 acc_var
			} else 
			ifvare moving_vel2 1 
			{ 
				setvarvar acc_defvel2 acc_speed2

				ifvarg acc_extravar 0 
				ifvarand acc_extravar 2 {
				ifvarvare se_count acc_vtime2 {
				ifvarand dx_case2 1 addvarvar acc_defvel2 buff_dx2 else 
					subvarvar acc_defvel2 buff_dx2 } } else
				ifvarvarl speed_dx2 buff_dx2 {
					ifvare dx_case2 0 subvarvar acc_defvel2 1 else 
						ifvare dx_case2 1 addvarvar acc_defvel2 1  
					addvarvar speed_dx2 1
				}	
				mulvarvar acc_defvel2 buffer_multiple
			} else
			ifvare moving_vel2 2 //замеделение
			{
				setvar speed_dx2 0 //переменная внесения поправки
				addvar acc_count2 1
				setvarvar acc_var acc_speed2
				mulvarvar acc_var buffer_multiple
				setvarvar acc_var2 acceleration2
				mulvarvar acc_var2 -1
				mulvarvar acc_var2 acc_count2
				addvarvar acc_var acc_var2
				ifvarand dx_case2 2 {
					setvarvar acc_var2 buff_dx2
					mulvarvar acc_var2 buffer_multiple
					ifvare acc_count2 1 { ifvarand dx_case2 1 addvarvar acc_var acc_var2 else
						subvarvar acc_var acc_var2 }
				}
				setvarvar acc_defvel2 acc_var 
			}
			ifvare rot_turnway 1 
				mulvarvar acc_defvel2 -1
			addvarvar buffer_speed2  acc_defvel2
			setvarvar buffer_tmp2 buffer_speed2 
			divvarvar buffer_tmp2 buffer_multiple
			setvarvar ang rot_startang2
			addvarvar ang buffer_tmp2
			ifvarg ang 2047 subvar ang 2048
			setactor[THISACTOR].ang ang
	} else orvar se_zvel2stop 2
	ifvare se_zvel2stop 3
	{
		ifvare se_zvel 1 {
			ifvare active 0 { stopsoundvar doorsound_open 
		ifvarg doorsound_close 0 soundoncevar doorsound_close setvar active 1 } else setvar active 0
		} else ifvare se_zvel 2 { 
			ifvare active 0 { ifvarvarl mover_position rot_varmax setvar active 1 } else setvar active 0
		} else setvar active 0

		ifvare active 0 {
			ifvarvare acc_speed1 acc_dist {
				ifvare mover_position 1 {
					setactor[THISACTOR].x mov_coordx
					setactor[THISACTOR].y mov_coordy
					setactor[THISACTOR].z mov_coordz
				} else {
					setactor[THISACTOR].x rot_savex
					setactor[THISACTOR].y rot_savey
					setactor[THISACTOR].z rot_savez
				}
			}
			ifvarg mover_position 0 setactor[THISACTOR].ang mov_gpang else 
				setactor[THISACTOR].ang rot_saveang
			ifvarg autoclose_delay 0 setvar activate_autoclose 1
			stopsoundvar doorsound_open 
			stopsoundvar doorsound_open2 
			ifvare se_zvel 0 { ifvare mover_position 1 ifvarg doorsound_close 0 soundoncevar doorsound_close 
				else ifvare mover_position 0 ifvarg doorsound_close2 0 soundoncevar doorsound_close2 } 
					else ifvarg doorsound_close 0 soundoncevar doorsound_close 
		}
		setvar rot_moving 0
		setvar se_zvel2stop 0
		setvar se_count 0
		setvar acc_count 0
		setvar buffer_speed 0
		setvar moving_vel 0
		setvar acc_count2 0
		setvar buffer_speed2 0
		setvar moving_vel2 0
		ifvarvare rot_nextway rot_varmax { setvar rot_nextway 0 }
	}
ends

eventloadactor MOVER
	cstat 32768
	getactor[THISACTOR].lotag LOTAGSET
	setactor[THISACTOR].lotag 0
	getactor[THISACTOR].zvel se_zvel
	getactor[THISACTOR].owner se_owner
	getactor[THISACTOR].extra acc_extravar
	setvarvar period sprite[THISACTOR].clipdist
	state se64_loadoptions
	ifvare doorsound_open2 0 
		setvarvar doorsound_open2 doorsound_open
	ifvare doorsound_close2 0 
		setvarvar doorsound_close2 doorsound_close
enda

actor MOVER

	state se64_initactor //задание прикрепленного спрайта
	ifvare active 1 ifvare rot_moving 0
	{
		state mover_init

		addvar rot_moving 1
		setvar COUNTER 0 // счетчик автозакрытия
		setvar active 0
	} else { ifvare se_zvel 0 setvar active 0 } // активация во время движения
	
	ifvare rot_moving 1
	{	
		ifvare se_zvel 0 { ifvare mover_position 1 ifvarg doorsound_open 0 soundoncevar doorsound_open else ifvare mover_position 0 ifvarg doorsound_open2 0 soundoncevar doorsound_open2 } 
		else ifvarg doorsound_open 0 { ifvare period 32 soundoncevar doorsound_open else { addvar soundtime 1 ifvarvarg soundtime period { soundvar doorsound_open setvar soundtime 0 } } }
		
		addvar se_count 1
		state mover_action //state se64_zvel2
		
		//перемещение спрайтов
			
		setvar sectorid 0
		whilevarvarn sectorid NUMSECTORS 
		{
			headspritesect spriteid sectorid
			whilevarn spriteid -1 
			{
				getactor[spriteid].picnum picnum
				ifvarn picnum SECTOREFFECTOR
				ifvarn picnum MOVER_POINT
				ifvarvarn spriteid THISACTOR 
				ifvarg se_owner 0 ifvarvare se_owner actorvar[spriteid].mover_rid
				{	
					getactorvar[spriteid].spr_pndist spr_pndist
					getactorvar[spriteid].spr_pnang spr_pnang	
					getactorvar[spriteid].spr_zdist spr_zdist
					addvarvar spr_zdist sprite[THISACTOR].z
							
					getactor[THISACTOR].ang ang
					addvarvar ang spr_pnang
														
					cos wall_rotx ang
					mulvarvar wall_rotx spr_pndist
					divvar wall_rotx 16384
					sin wall_roty ang
					mulvarvar wall_roty spr_pndist
					divvar wall_roty 16384
											
					getactor[THISACTOR].x x
					addvarvar wall_rotx x
					getactor[THISACTOR].y y
					addvarvar wall_roty y		
					updatesectorz wall_rotx wall_roty spr_zdist tmp					
					setactor[spriteid].x wall_rotx
					setactor[spriteid].y wall_roty
					setactor[spriteid].z spr_zdist
					setactor[spriteid].sectnum tmp
					
								
					getactor[THISACTOR].ang ang
					getactorvar[spriteid].rot_actorang tmp
					addvarvar ang tmp
					setactor[spriteid].ang ang			
				}
				nextspritesect spriteid spriteid
			}
			addvar sectorid 1
		}
	
	}
	
	ifvare activate_autoclose 1
	{
		ifvare se_zvel 0
		ifvare moving_var 1
		{
			addvar COUNTER 1
			ifvarvarg COUNTER autoclose_delay { ifvare status 0 setvar status 1 else setvar status 0 setvar active 1 setvar COUNTER 0 setvar activate_autoclose 0 }
		} 
		/*
		else ifvare se_zvel 1
		ifvare rot_nextway 1
		{
			addvar COUNTER 1
			ifvarvarg COUNTER autoclose_delay { setvar active 1 setvar COUNTER 0 setvar activate_autoclose 0 }
		}
		*/
	}
enda
