// New Lock system ( TC_LOCKER ) 

/* TC_LOCKER tr_bits
1 - activated by key
2 - activated by one-wall without 16384
8 - locked while active = 0; 
	+ 4 - locked with user quote and sound
16 - elevator locker, activater if player is in a sector
32 - opening with active = 1, and closing with active = 0
64 - move checker_opening door locker
128 - move checker_closing door locker
*/

eventloadactor TC_LOCKER

	getactor[THISACTOR].hitag HITAGSET
	getactor[THISACTOR].lotag LOTAGSET
	setactor[THISACTOR].hitag 0
	setactor[THISACTOR].lotag 0
	
	setvar spriteid 0
	whilevarvarn spriteid NUMSPRITES
	{
		getactor[spriteid].picnum picnum
		ifvare picnum SECTOREFFECTOR
		{
			getactor[spriteid].sectnum sect2
			getactor[THISACTOR].sectnum sectnum
			getsector[sectnum].lotag tmp
			ifvare tmp 25 ifvarvare sect2 sectnum { getactor[spriteid].lotag lotag ifvare lotag 10 { setactor[spriteid].picnum 0 setactor[spriteid].cstat 32768 getactor[spriteid].hitag autoclose_delay } }
			
			getactor[spriteid].lotag lotag
			ifvare lotag 69
			{
				getactor[THISACTOR].yvel rx_can
				getactor[spriteid].xvel tx_can
			    ifvarvare rx_can tx_can setvar se_not31op 1
			}
		} 
		ifvare picnum TRIGGERQUOTE
		{
			getactor[spriteid].zvel tr_bits
			getactor[spriteid].sectnum sect2
			getactor[THISACTOR].sectnum sectnum
			ifvarvare sect2 sectnum ifvare tr_bits 2
			{ getactorvar[spriteid].tr_lotag tr_lotag }
		}
		addvar spriteid 1
	}

	ifvarg HITAGSET 0 {
	espawn ACTIVATOR 
	setactor[RETURN].lotag HITAGSET }
	
	getactor[THISACTOR].zvel tr_bits 
	getactor[THISACTOR].sectnum sectorid
	getsector[sectorid].wallptr wallptr
	getsector[sectorid].wallnum wallnum
		//state find_rx_tx_cannels
		
	setvar tmp 0
	ifvarl wallnum 1 break
	whilevarvarn tmp wallnum
	{ 
		getactor[THISACTOR].x x
		getactor[THISACTOR].y y
		getactor[THISACTOR].z z
		getactor[THISACTOR].ang ang
		getactor[THISACTOR].sectnum sectorid
		cos mycos ang
		sin mysin ang
		hitscan x y z sectorid mycos mysin 0 RETURN peractor RETURN RETURN RETURN RETURN 0xFFFF0030h //4294901808
		ifvarand tr_bits 2 { setwall[wallptr].hitag 16384 ifvarn peractor -1 setwall[peractor].hitag HITAGSET } else
		ifvarn tr_bits 32 setwall[wallptr].hitag HITAGSET	
		addvar wallptr 1
		addvar tmp 1
	}
    cstat 32768
	
enda

state operatelocker
operateactivators HITAGSET THISACTOR
ifvarg autoclose_delay 0 { setvar COUNTER 0 ifvare activate_autoclose 0 setvar activate_autoclose 1 else setvar activate_autoclose 0 }
//ifvarg rx_id_numbers 0 {  
	getactor[THISACTOR].yvel tx_can 

	ifvarn tx_can 0 {
		setvar sectorid 0
		whilevarvarn sectorid NUMSECTORS 
		{
			headspritesect spriteid sectorid
			whilevarn spriteid -1 
			{
				getactor[spriteid].picnum picnum
				
				getactor[spriteid].lotag lotag
				getactorvar[spriteid].rot_moving rot_moving
				
				getactorvar[spriteid].se_xvel rx_can
				ifvarvare rx_can tx_can {
					ifvarand tr_bits 64 {
						ifvare actorvar[spriteid].status 0 {
							ifvare actorvar[spriteid].active 1 setactorvar[spriteid].active 0 else setactorvar[spriteid].active 1
							setactorvar[spriteid].activatedby THISACTOR
							setactorvar[spriteid].status 1
						} else setvar active 0
					} else ifvarand tr_bits 128 {
						ifvare actorvar[spriteid].status 1 {
							ifvare actorvar[spriteid].active 1 setactorvar[spriteid].active 0 else setactorvar[spriteid].active 1
							setactorvar[spriteid].activatedby THISACTOR
							setactorvar[spriteid].status 0
						} else setvar active 0
					} else {
						ifvare actorvar[spriteid].active 1 setactorvar[spriteid].active 0 else setactorvar[spriteid].active 1
						ifvare actorvar[spriteid].status 1 setactorvar[spriteid].status 0 else setactorvar[spriteid].status 1
						setactorvar[spriteid].activatedby THISACTOR
					}
				}
				nextspritesect spriteid spriteid
			}
			addvar sectorid 1
		}
	}
//}
ends

			

actor TC_LOCKER 0 DELAY1

ifvarg active 1 setvar active 1
 
ifhitspace
{
	ifvare active 1
	ifvare locker_unlock 0
	{
		getactor[THISACTOR].sectnum sectorid
		getsector[sectorid].wallptr wallptr
		getsector[sectorid].wallnum wallnum	
		setvar tmp 0
		ifvarl wallnum 1 break
		whilevarvarn tmp wallnum
		{
			getwall[wallptr].hitag wallhitag
			ifvarvarn wallhitag HITAGSET 
			{ 
				ifvarand tr_bits 1 
				{  
					ifvare LOTAGSET 1 ifvare VAR_SKULLKEY 1 { setwall[wallptr].hitag HITAGSET setvar locker_unlock 1 } else
					ifvare LOTAGSET 2 ifvare VAR_EYEKEY 1 { setwall[wallptr].hitag HITAGSET setvar locker_unlock 1 } else
					ifvare LOTAGSET 3 ifvare VAR_FIREKEY 1 { setwall[wallptr].hitag HITAGSET setvar locker_unlock 1 } else
					ifvare LOTAGSET 4 ifvare VAR_DAGGERKEY 1 { setwall[wallptr].hitag HITAGSET setvar locker_unlock 1 } else
					ifvare LOTAGSET 5 ifvare VAR_SPIDERKEY 1 { setwall[wallptr].hitag HITAGSET setvar locker_unlock 1 } else
					ifvare LOTAGSET 6 ifvare VAR_MOONKEY 1 { setwall[wallptr].hitag HITAGSET setvar locker_unlock 1 } else
					{ setactorvar[THISACTOR].active 0 }
				} else { setwall[wallptr].hitag HITAGSET setvar locker_unlock 1 }
			}
			addvar wallptr 1
			addvar tmp 1
		}
	}
			
	getplayer[THISACTOR].i PLAYERID
	getplayer[THISACTOR].posx px
	getplayer[THISACTOR].posy py
	getplayer[THISACTOR].posz pz
	getplayer[THISACTOR].cursectnum psector
	getplayer[THISACTOR].ang pang
	getplayer[THISACTOR].horiz phoriz
	subvar phoriz 100
	mulvar phoriz -2048
	cos mycos pang
	sin mysin pang
	ifvarn psector -1 hitscan px py pz psector mycos mysin phoriz RETURN HITWALL spriteid lock_x lock_y RETURN CLIPMASK0 //1

		ifvarn HITWALL -1
		{
			/*
			getwall[HITWALL].hitag HITAG
			ifvare HITAG 0 {
				getwall[HITWALL].nextwall wallid
				ifvarn wallid -1 { getwall[wallid].hitag HITAG } else setvar HITAG 0
			}
			*/
			getwall[HITWALL].nextwall wallid
			ifvarn wallid -1 { getwall[wallid].hitag HITAG } else setvar HITAG 0
			
			ifvarg HITAG 0
			{
				getplayer[THISACTOR].posx px
				getplayer[THISACTOR].posy py
				getplayer[THISACTOR].posy pz
				subvarvar lock_x px
				subvarvar lock_y py
				mulvarvar lock_x lock_x
				mulvarvar lock_y lock_y
				addvarvar lock_x lock_y
				sqrt lock_x WALLDIST
			} else setvar WALLDIST -1
		} else { setvar WALLDIST -1 setvar HITAG 0 }

		ifvarl WALLDIST 1278 ifvarn WALLDIST -1
		{ 	
			/*
			ifvarg wall[HITWALL].hitag 0 {
				getwall[HITWALL].nextwall wallid
				ifvarn wallid -1 getwall[wallid].nextsector lk_sectnum
				
				ifvarn lk_sectnum -1 headspritesect spriteid lk_sectnum
				whilevarn spriteid -1 
				{
					getactor[spriteid].picnum picnum
					ifvare picnum TC_LOCKER	setactorvar[spriteid].set 1
					nextspritesect spriteid spriteid
				}	
			} else {
				getwall[HITWALL].nextsector lk_sectnum
				ifvarn lk_sectnum -1 headspritesect spriteid lk_sectnum
				whilevarn spriteid -1 
				{
					getactor[spriteid].picnum picnum
					ifvare picnum TC_LOCKER	setactorvar[spriteid].set 1
					nextspritesect spriteid spriteid
				}
			}
			*/	
			setvarvar closest_locker -1
			getwall[HITWALL].nextsector lk_sectnum
			ifvarn lk_sectnum -1 headspritesect spriteid lk_sectnum
			whilevarn spriteid -1 
			{
				getactor[spriteid].picnum picnum
				ifvare picnum TC_LOCKER	{ 
					setactorvar[spriteid].set 1
					setvarvar closest_locker spriteid
				}
				nextspritesect spriteid spriteid
			}	

			ifvare set 1
			ifvare press_space 0
			{	
				ifaction DELAY1
				{
					ifvarand tr_bits 1 //activated by key
					{
						ifvarvarn VAR_SKULLKEY 1
						ifvarvare HITAG HITAGSET
						ifvarvare LOTAGSET 1
						{
							quote 137
							soundonce 473
							action DELAY2
						}
						ifvarvarn VAR_EYEKEY 1
						ifvarvare HITAG HITAGSET
						ifvarvare LOTAGSET 2
						{
							quote 137
							soundonce 473
							action DELAY2
						}
						ifvarvarn VAR_FIREKEY 1
						ifvarvare HITAG HITAGSET
						ifvarvare LOTAGSET 3
						{
							quote 137
							soundonce 473
							action DELAY2
						}
						ifvarvarn VAR_DAGGERKEY 1
						ifvarvare HITAG HITAGSET
						ifvarvare LOTAGSET 4
						{
							quote 137
							soundonce 473
							action DELAY2
						}
						ifvarvarn VAR_SPIDERKEY 1
						ifvarvare HITAG HITAGSET
						ifvarvare LOTAGSET 5
						{
							quote 137
							soundonce 473
							action DELAY2
						}
						ifvarvarn VAR_MOONKEY 1
						ifvarvare HITAG HITAGSET
						ifvarvare LOTAGSET 6
						{
							quote 137
							soundonce 473
							action DELAY2
						}
					}
					ifvarand tr_bits 8 //locked while active = 0
					{
						getactorvar[THISACTOR].active active
						ifvarn active 1
						ifvarvare HITAG HITAGSET
						{
							ifvarand tr_bits 4 { ifvarg tr_lotag 0 { qstrcpy 8 tr_lotag quote 8 } } 
							else
							{
								quote 168
								soundonce 538
							}
							action DELAY2
						}
					}
					ifvarand tr_bits 32 //opening with active = 1, and closing with active = 0
					{
						getactorvar[THISACTOR].active active
						ifvarn active 1
						ifvarvare HITAG HITAGSET
						{
							ifvarand tr_bits 4 { ifvarg tr_lotag 0 { qstrcpy 8 tr_lotag quote 8 } } 
							else ifvarand tr_bits 8
							{
								quote 168
								soundonce 538
							}
							action DELAY2
						}
					}
					ifvarn active 1
					ifvare HITAG 16384
					{	
						ifvarand tr_bits 4 { ifvarg tr_lotag 0 { qstrcpy 8 tr_lotag quote 8 } } 
						else
						{
							quote 168
							soundonce 538
						}
						action DELAY2
					}
				}
				ifaction DELAY1
				{
					ifvarand tr_bits 32 nullop else
					ifvarvare HITAG HITAGSET
					{
						state se64_check
						ifvarand tr_bits 16 {
						  checkactivatormotion HITAGSET
						  ifvare RETURN 0 ifvare SERETURN 0 state operatelocker
						} else ifvare SERETURN 0 state operatelocker
						action DELAY2
					}
				}
				setvar press_space 1
			}			
		}
}
else 
{
	setvar press_space 0
	setvar set 0
	ifaction DELAY2
		action DELAY1
}

ifvarand tr_bits 16 //локер для лифта, срабатывает при нахождении в секторе. + Условие открывания двери первой
{
	getplayer[THISACTOR].i PLAYERID
	getactor[PLAYERID].sectnum sect1
	getactor[THISACTOR].sectnum sectorid
	ifvarvare sect1 sectorid
	{
		ifhitspace
		{
			ifvare press_space 0
			{	
				ifvare HITAG 0
				{
					ifvarand tr_bits 1 //activated by key
					{
						ifvarvarn VAR_SKULLKEY 1
						ifvarvare HITAG HITAGSET
						ifvarvare LOTAGSET 1
						{
							quote 137
							soundonce 473
							action DELAY2
						}
						ifvarvarn VAR_EYEKEY 1
						ifvarvare HITAG HITAGSET
						ifvarvare LOTAGSET 2
						{
							quote 137
							soundonce 473
							action DELAY2
						}
						ifvarvarn VAR_FIREKEY 1
						ifvarvare HITAG HITAGSET
						ifvarvare LOTAGSET 3
						{
							quote 137
							soundonce 473
							action DELAY2
						}
						ifvarvarn VAR_DAGGERKEY 1
						ifvarvare HITAG HITAGSET
						ifvarvare LOTAGSET 4
						{
							quote 137
							soundonce 473
							action DELAY2
						}
						ifvarvarn VAR_SPIDERKEY 1
						ifvarvare HITAG HITAGSET
						ifvarvare LOTAGSET 5
						{
							quote 137
							soundonce 473
							action DELAY2
						}
						ifvarvarn VAR_MOONKEY 1
						ifvarvare HITAG HITAGSET
						ifvarvare LOTAGSET 6
						{
							quote 137
							soundonce 473
							action DELAY2
						}
					}
					
					ifaction DELAY1
					{
						checkactivatormotion HITAGSET
						state se64_check //SERETURN = 0/1
						ifvare RETURN 0 ifvare SERETURN 0 
						state operatelocker
						action DELAY2
					}
					setvar press_space 1
				} else
				ifvarg WALLDIST 1277
				{
					ifaction DELAY1
					{
						checkactivatormotion HITAGSET
						state se64_check //SERETURN = 0/1
						ifvare RETURN 0 ifvare SERETURN 0 
						state operatelocker
						action DELAY2
					}
					setvar press_space 1
				}
			}
		}
		else 
		{
			setvar press_space 0
			ifaction DELAY2
			action DELAY1
		}
	}
} else
ifvarand tr_bits 32 //opening with active = 1, and closing with active = 0
{
	getactorvar[THISACTOR].active active
	ifvare active 1
	ifvare status 0
	{
		state operatelocker
		setvar status 1
	} else
	ifvare active 0 ifvare status 1
	{
		state operatelocker
		setvar status 0
	}
} else
ifvarand tr_bits 64 //move checker_opening door locker
{
	ifvare active 1 {
		state operatelocker
	}
		
	/*
	state se64_check
	ifvare SERETURN 1 { ifvare peractor 0 { ifvare status 0 setvar status 1 else setvar status 0 setvar peractor 1 } } else setvar peractor 0
	checkactivatormotion HITAGSET
	ifvare RETURN 1 { ifvare peractor2 0 { ifvare status 0 setvar status 1 else setvar status 0 setvar peractor2 1 } } else setvar peractor2 0

	ifvare active 1
	ifvare status 0
	{
		state operatelocker
	} else setvar active 0
	*/
} else
ifvarand tr_bits 128 //move checker_closing door locker
{
	/*
	state se64_check
	ifvare SERETURN 1 { ifvare peractor 0 { ifvare status 0 setvar status 1 else setvar status 0 setvar peractor 1 } } else setvar peractor 0
	checkactivatormotion HITAGSET
	ifvare RETURN 1 { ifvare peractor2 0 { ifvare status 0 setvar status 1 else setvar status 0 setvar peractor2 1 } } else setvar peractor2 0
	getactorvar[THISACTOR].active active
	ifvare active 1
	{
		ifvare status 1
		{
			state operatelocker
		} else setactorvar[THISACTOR].active 0
	}
	*/
	ifvare active 1 {
		state se64_check
		checkactivatormotion HITAGSET
		ifvare SERETURN 0 ifvare RETURN 0
			state operatelocker
	 }
}

//autoclose for slidedoor (sector lotag 25)
ifvarand tr_bits 3 nullop else
{
	ifvare activate_autoclose 1
	{
		addvar COUNTER 1
		ifvarvarg COUNTER autoclose_delay 
		{  
			operateactivators HITAGSET THISACTOR
			//ifvarg rx_id_numbers 0 
			//{  
				getactor[THISACTOR].yvel tx_can 
				ifvarn tx_can 0
				{
					setvar sectorid 0
					whilevarvarn sectorid NUMSECTORS 
					{
					headspritesect spriteid sectorid
					whilevarn spriteid -1 
					{
						getactorvar[spriteid].se_xvel rx_can
						ifvarvare rx_can tx_can { getactorvar[spriteid].active tmp ifvare tmp 1 { setactorvar[spriteid].active 0 } else setactorvar[spriteid].active 1 setactorvar[spriteid].activatedby THISACTOR }
						nextspritesect spriteid spriteid
					}
					addvar sectorid 1
					}
				}
			//}
			setvar COUNTER 0 setvar activate_autoclose 0 
		}
	}
}

// открытие двери врагом
ifvare tr_bits 0
ifvare enemy_opendr 1 {
	state se64_check
	ifvarand tr_bits 16 {
		checkactivatormotion HITAGSET
		ifvare RETURN 0 ifvare SERETURN 0 state operatelocker
	} else ifvare SERETURN 0 state operatelocker
	setvar enemy_opendr 0
}
enda

