/* TRIGGER bits
0 - триггер реагирующий на переменную active
1 - триггер реагирующий на игрока, находящиеся в том же секторе что и триггер
2 - триггер реагирующий на убивание монстров
3 - при попадании в актор, условие попадание в помеченный актор
4 - триггер реагирующий на спрайты, находящиеся в том же секторе что и триггер
5 - триггер реагирующий на удаление спрайта с уровня (killit)
6 - триггер реагирующий на векторное оружие
7 - триггер, срабатывающий в мультиплеере при загрузке карты
8 - триггер, реагирующий на нажатие спрайта игроком
*/

state find_rx_tx_cannels
getactor[THISACTOR].yvel tx_can 
ifvarn tx_can 0
{
	setvar sectorid 0
	setvar rx_id_numbers 0
	whilevarvarn sectorid NUMSECTORS 
	{
		headspritesect spriteid sectorid
		whilevarn spriteid -1 
		{
			getactor[spriteid].xvel rx_can
			ifvarvare rx_can tx_can { addvar rx_id_numbers 1 } // количество акторов, которые надо активировать
			nextspritesect spriteid spriteid
		}
		addvar sectorid 1
	}
}
ends

eventloadactor TRIGGER
   getactor[THISACTOR].hitag HITAGSAVED
   getactor[THISACTOR].lotag LOTAGSAVED
   setactor[THISACTOR].hitag 0
   setactor[THISACTOR].lotag 0
   cstat 32768
   
	state find_rx_tx_cannels //set up rx_id_numbers variable
    
	getactor[THISACTOR].zvel tr_bits
    getactor[THISACTOR].xvel switch_rx 
	getactor[THISACTOR].owner trigger_var
	getactor[THISACTOR].pal tr_pal

	ifvarn HITAGSAVED 0 
		setvar tr_start2 1
	
	ifvarn tr_pal 1
	ifvarn switch_rx 0
	{
		ifvarn tr_bits 2 {
			setvar spriteid 0
			setvar TRIGGERNUMSAVED 0
			whilevarvarn spriteid NUMSPRITES
			{
				getactor[spriteid].picnum picnum
				ifvare picnum SECTOREFFECTOR
				{
					getactor[spriteid].lotag lotag
					getactor[spriteid].hitag hitag
					getactor[spriteid].xvel rx_can
					getactor[THISACTOR].xvel tx_can
					ifvarvare tx_can rx_can 
					{
						ifvare lotag 60 ifvare tr_bits 1 { setvar active 1 setvarvar trigger_skill hitag }
					}
				}
				
				getactor[spriteid].yvel tx_can
				ifvarvare tx_can switch_rx {
					addvar TRIGGERNUMSAVED 1   // число акторов, которые должен активировать триггер
				 }
				addvar spriteid 1
			}
			ifvare HITAGSAVED 0 { setvarvar HITAGSAVED TRIGGERNUMSAVED }
		}
	} else ifvare tr_pal 1
	{ setvarvar tr_lotag LOTAGSAVED }

enda

state trigger_switchon
ifvare trigger_var -1 { setvar peractor2 0 setvar active 0 }
operateactivators LOTAGSAVED THISACTOR
operaterespawns LOTAGSAVED
operatemasterswitches LOTAGSAVED
state rx_cansetactive
ifvarn trigger_var -1 setvar HITAGSAVED 0
ends

state tr_definetrigger
ifvarvarl peractor2 trigger_id_numbers { setvar sectorid 0 addvarvar peractor2 1 }
setvar tr_currtrig_id -1
whilevarvarn sectorid NUMSECTORS 
{
	headspritesect spriteid sectorid
	whilevarn spriteid -1 
	{
		getactor[spriteid].picnum picnum
		getactor[spriteid].pal pal
		ifvare picnum TRIGGER
		ifvare pal 1
		{
			getactorvar[THISACTOR].se_xvel rx_can
			getactorvar[spriteid].se_xvel tx_can
			ifvarvare rx_can tx_can
			{
			getactor[spriteid].zvel tmp
			ifvarvare tmp peractor2 setvarvar tr_currtrig_id spriteid
			}
		}
		nextspritesect spriteid spriteid
	}
	addvar sectorid 1
}
ends

state activevar_trigger //tr_bits = 0
setvarvar trigger_id_numbers 0
ifvarg active 0 {
	setvar sectorid 0
	whilevarvarn sectorid NUMSECTORS 
	{
		headspritesect spriteid sectorid
		whilevarn spriteid -1 
		{
			setvarvar trigger_id_numbers active
			nextspritesect spriteid spriteid
		}
		addvar sectorid 1
	}
}
ends


state enemykill_trigger //tr_bits = 2
	getactor[THISACTOR].yvel tx_can
	ifvarg tx_can 0 {
		setvar sectorid 0
		whilevarvarn sectorid NUMSECTORS 
		{
			headspritesect spriteid sectorid
			whilevarn spriteid -1 
			{
				ifvare sprite[spriteid].picnum TRIGGER {
					ifvarvare actorvar[spriteid].tr_bits 2
						ifvarvare tx_can actorvar[spriteid].switch_rx {
						getactorvar[spriteid].trigger_id_numbers tmp
						addvar tmp 1
						setactorvar[spriteid].trigger_id_numbers tmp
					}
				}
				nextspritesect spriteid spriteid
			}
			addvar sectorid 1
		}
	}
ends


state bit3reset
	setvar sectorid 0
	whilevarvarn sectorid NUMSECTORS 
	{
		headspritesect spriteid sectorid
		whilevarn spriteid -1 
		{
			getactor[spriteid].yvel tx_can
			ifvarvare tx_can switch_rx 
			{ 
				setactorvar[spriteid].trshoot_var 0 
			} 
			nextspritesect spriteid spriteid
		}
		addvar sectorid 1
	}
	ifvare trigger_var -1 setvar trigger_id_numbers 0
ends

state loadsprite_trigger
	getactor[THISACTOR].yvel tx_can
	ifvarg tx_can 0 
		ifvarg sprite[THISACTOR].picnum 32 {
		setvar spriteid 0
		whilevarvarn spriteid NUMSPRITES
		{
			ifvare sprite[spriteid].picnum TRIGGER {
			
				ifvarvare tx_can sprite[spriteid].xvel {
					ifvare sprite[spriteid].zvel 6 {
						//setactor[THISACTOR].statnum 1
						changespritestat THISACTOR 1
						ifvarl sprite[THISACTOR].extra 1
							setactor[THISACTOR].extra 1 
						setvar triggerlink 6
						cstator 256
					}
				}
				ifvarvare tx_can sprite[spriteid].xvel {
					ifvare sprite[spriteid].zvel 3 {
					
						//setactor[THISACTOR].statnum 1
						changespritestat THISACTOR 1
						ifvarl sprite[THISACTOR].extra 1
							setactor[THISACTOR].extra 1 
						setvar triggerlink 3
						cstator 256
					}
				}
			}
			addvar spriteid 1
		}
	}
ends

state actionsprite_trigger
	getactor[THISACTOR].yvel tx_can
	ifvarg tx_can 0 ifvarn triggerlink 0 {
	getactor[THISACTOR].picnum picnum 
		ifvarg picnum 32 {
			getactor[THISACTOR].picnum picnum 
			getactor[THISACTOR].extra extra 
			getactor[THISACTOR].htextra htextra 

			ifhitweapon { //projectile trigger для зомби код в самом зомби
				getactor[THISACTOR].htpicnum enemy_htpic 
			}

			ifvarg enemy_htpic 0 {
				ifvare triggerlink 6 {
					ifvarn enemy_htpic 2595 ifvarn enemy_htpic APLAYER { 
						ifvarl extra 1 setvarvar hitspritesaved THISACTOR }
				}
				ifvare triggerlink 3 {
					setvarvar hitspritesaved THISACTOR
				}					
			} else {
				ifvare triggerlink 6 {
				getactorvar[THISACTOR].burninactivenow burninactivenow
				ifvare burninactivenow 1 { setvarvar hitspritesaved THISACTOR } //если загорелся
				getactorvar[THISACTOR].flamedamage flamedamage
				ifvarg flamedamage 0 { ifvarvarg flamedamage extra ifvarl extra 2 setvarvar hitspritesaved THISACTOR } else { ifvarvare flamedamage extra ifvarl extra 2 setvarvar hitspritesaved THISACTOR }
				}
			}
			
			ifvarg hitspritesaved 0 {
				setvar sectorid 0
				whilevarvarn sectorid NUMSECTORS 
				{
					headspritesect spriteid sectorid
					whilevarn spriteid -1 
					{
						ifvare sprite[spriteid].picnum TRIGGER {
							ifvarvare actorvar[spriteid].tr_bits triggerlink
								ifvarvare tx_can actorvar[spriteid].switch_rx {
								checkactivatormotion actorvar[spriteid].LOTAGSAVED
								ifvare RETURN 0 ifvare trshoot_var 0 {
									getactorvar[spriteid].trigger_id_numbers tmp
									addvar tmp 1
									setactorvar[spriteid].trigger_id_numbers tmp
									setvar trshoot_var 1
								}
								ifvare actorvar[spriteid].trigger_var -1 
									setactor[THISACTOR].extra 100							
							}
						}
						nextspritesect spriteid spriteid
					}
					addvar sectorid 1
				}
				setvar enemy_htpic -1
				setvar hitspritesaved -1
			}
		}
	}

ends

state sprsector_trigger //tr_bits = 4
	getactor[THISACTOR].sectnum sectorid
	setvarvar trigger_id_numbers 0
	headspritesect spriteid sectorid
	whilevarn spriteid -1 
	{
		getactor[spriteid].yvel tx_can
		ifvarvare tx_can switch_rx 
		{
			getactor[THISACTOR].sectnum sectnum
			getactor[spriteid].sectnum sectorid
			ifvarvare sectnum sectorid addvar trigger_id_numbers 1
		}
		nextspritesect spriteid spriteid
	}
ends

state sprkillit_trigger //tr_bits = 5
	getactor[THISACTOR].yvel tx_can
	ifvarg tx_can 0 {
		setvar sectorid 0
		whilevarvarn sectorid NUMSECTORS 
		{
			headspritesect spriteid sectorid
			whilevarn spriteid -1 
			{
				ifvare sprite[spriteid].picnum TRIGGER {
					ifvarvare actorvar[spriteid].tr_bits 5
						ifvarvare tx_can actorvar[spriteid].switch_rx {
						getactorvar[spriteid].trigger_id_numbers tmp
						addvar tmp 1
						setactorvar[spriteid].trigger_id_numbers tmp
					}
				}
				nextspritesect spriteid spriteid
			}
			addvar sectorid 1
		}
	}
ends

state playersect_trigger
	getplayer[THISACTOR].i PLAYERID
	getuserdef[THISACTOR].m_player_skill skillit //установка работы триггера при разных уровнях сложности
	ifmultiplayer addvar skillit 1
	ifvare skillit 3 setvar skillit 4 else ifvare skillit 4 setvar skillit 8 else ifvare skillit 5 setvar skillit 16
	getactorvar[THISACTOR].trigger_skill trigger_skill
	
	ifvarg trigger_skill 0 //если есть условие работы через сложность уровня (SE60)
	{
		ifvarvarand trigger_skill skillit
		{ 
		    getactor[PLAYERID].sectnum sectorid
			getactor[THISACTOR].sectnum sectnum
			ifvarvare sectnum sectorid { 
				setvarvar trigger_sector sectnum 
				ifvare sprite[THISACTOR].pal 0 { ifvare peractor 0 { state rx_cansetactive operateactivators LOTAGSAVED THISACTOR operaterespawns LOTAGSAVED operatemasterswitches LOTAGSAVED setvar peractor 1 } } 
			}
			else {
				ifvare sprite[THISACTOR].pal 5 {
					ifvarn trigger_sector -1 ifvarvarn trigger_sector sectorid {
						ifvare peractor 0 { state rx_cansetactive operateactivators LOTAGSAVED THISACTOR operaterespawns LOTAGSAVED operatemasterswitches LOTAGSAVED setvar peractor 1 }
						ifvarn trigger_var 1 setvar peractor 0
						setvar trigger_sector -1
					}
				} else
				ifvarn trigger_var 1
				{
					ifvare peractor 1
					{
						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 tx_can rx_can { setactorvar[spriteid].active 0  }
										nextspritesect spriteid spriteid
									}
									addvar sectorid 1
								}
							}
						}
						setvar peractor 0
					}
				}
			}
		} 
	} 
	else 
	{
	    getactor[PLAYERID].sectnum sectorid
		getactor[THISACTOR].sectnum sectnum
		ifvarvare sectnum sectorid { 
			setvarvar trigger_sector sectnum 
			ifvare sprite[THISACTOR].pal 0 { ifvare peractor 0 { state rx_cansetactive operateactivators LOTAGSAVED THISACTOR operaterespawns LOTAGSAVED operatemasterswitches LOTAGSAVED setvar peractor 1 } }
		} else {
			ifvare sprite[THISACTOR].pal 5 {
				ifvarn trigger_sector -1 ifvarvarn trigger_sector sectorid {
					ifvare peractor 0 { state rx_cansetactive operateactivators LOTAGSAVED THISACTOR operaterespawns LOTAGSAVED operatemasterswitches LOTAGSAVED setvar peractor 1 }
					ifvarn trigger_var 1 setvar peractor 0
					setvar trigger_sector -1
				}
			} else
			ifvarn trigger_var 1
			{
				ifvare peractor 1
				{
					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 tx_can rx_can { setactorvar[spriteid].active 0  }
									nextspritesect spriteid spriteid
								}
								addvar sectorid 1
							}
						}
					}
					setvar peractor 0
				}
			}
		}
	}
ends

state scdtrigger_active
setvar sectorid 0
whilevarvarn sectorid NUMSECTORS 
{
	headspritesect spriteid sectorid
	whilevarn spriteid -1 
	{
		ifvarn tr_currtrig_id -1 { getactor[tr_currtrig_id].yvel tx_can }
		getactorvar[spriteid].se_xvel rx_can
		ifvarn tx_can 0 ifvarvare tx_can rx_can 
		{ 
			ifvarn tr_currtrig_id -1 getactorvar[tr_currtrig_id].tr_lotag tr_lotag
			operateactivators tr_lotag THISACTOR
			operaterespawns tr_lotag
			operatemasterswitches tr_lotag
			getactorvar[spriteid].active tmp 
			addvar tmp 1 
			setactorvar[spriteid].active tmp 
			setactorvar[spriteid].activatedby THISACTOR
			ifvare actorvar[spriteid].status 0 {
				setactorvar[spriteid].status 1
			} else setactorvar[spriteid].status 0
			setvar tr_currtrig_id -1 
		} else
		{
			ifvarn tr_currtrig_id -1 
			{
			getactorvar[tr_currtrig_id].tr_lotag tr_lotag
			operateactivators tr_lotag THISACTOR
			operaterespawns tr_lotag
			operatemasterswitches tr_lotag
			}
		}
		nextspritesect spriteid spriteid
	}
	addvar sectorid 1
}

ends

state multiplayer_trigger //tr_bits = 7
ifmultiplayer
{
	operateactivators LOTAGSAVED THISACTOR
	operaterespawns LOTAGSAVED
	operatemasterswitches LOTAGSAVED
	killit
}
ends

state hitspace_trigger //tr_bits = 8
getactorvar[THISACTOR].active active
ifvare active 0 setvar trigger_id_numbers 0
ifhitspace {
	getplayer[THISACTOR].posx px
	getplayer[THISACTOR].posy py
	getplayer[THISACTOR].posz pz
	getplayer[THISACTOR].cursectnum psector
	getplayer[THISACTOR].ang pang
	getplayer[THISACTOR].horiz phoriz
	getplayer[THISACTOR].i PLAYERID
	subvar phoriz 100
	mulvar phoriz -2048
	cos mycos pang
	sin mysin pang
	ifvarn psector -1 hitscan px py pz psector mycos mysin phoriz RETURN RETURN spriteid RETURN RETURN RETURN 4294901808 
	ifvarn spriteid -1 
	{
		getactor[spriteid].yvel tx_can
		ifvarvare tx_can switch_rx
		{ 
			ldist tmp PLAYERID spriteid
			ifvarl tmp 1280
			{
				ifp palive ifvare buttonhit 0 { addvar trigger_id_numbers 1 setactorvar[THISACTOR].active trigger_id_numbers setvar buttonhit 1 }
			}
		}
	}
} else setvar buttonhit 0
ends

state bit2start
	ifvarl trigger_start 3 {
		setvar TRIGGERNUMSAVED 0
		setvar sectorid 0
		whilevarvarn sectorid NUMSECTORS 
		{
			headspritesect spriteid sectorid
			whilevarn spriteid -1 
			{
				getactor[spriteid].yvel tx_can
				ifvarvare tx_can switch_rx 
				{ 
					getactor[spriteid].extra extra 
					ifvarg extra 0 { addvar TRIGGERNUMSAVED 1 }
				} 
				nextspritesect spriteid spriteid
			}
			addvar sectorid 1
		}
		ifvare tr_start2 0 { setvarvar HITAGSAVED TRIGGERNUMSAVED }
		addvar trigger_start 1
	} 
ends
							
actor TRIGGER

ifvare tr_pal 0
{
	ifvarn switch_rx 0 {	
	ifvare tr_bits 0 { state activevar_trigger } // триггер реагирующий на переменную active
	//перенесен в enemy.con (DEAD)
	ifvare tr_bits 2 { state bit2start } //триггер реагирующий на убивание монстров
	
		//переделан в event_game
		//ifvare tr_bits 3 { state shootsprite_trigger } // триггер реагирующий на выстрел в акторы
	ifvare tr_bits 4 { state sprsector_trigger } // триггер реагирующий на спрайты, находящиеся в том же секторе что и триггер
	
		//перенесен в EVENT_KILLIT
		//ifvare tr_bits 5 { state sprkillit_trigger } //триггер реагирующий на удаление спрайта с уровня (event_killit)
	
		//переделан в event_game
		//ifvare tr_bits 6 { state vectorkill_trigger } //триггер реагирующий на векторное оружие //активируется, если врага убить прожектайлом
	ifvare tr_bits 8 state hitspace_trigger

	ifvarn tr_bits 1 ifvarn tr_bits 7
	ifvarn HITAGSAVED 0 
	{
		ifvarg trigger_id_numbers 0 
		{ 
			state tr_definetrigger
			ifvarn tr_currtrig_id -1 state scdtrigger_active
		} //опция сумматора триггера
		
		setvarvar tmp trigger_id_numbers
		addvar tmp 1
		ifvarvarg tmp HITAGSAVED {
			state trigger_switchon
			ifvare tr_bits 3 state bit3reset
		}
	}
}
ifvare tr_bits 1 { ifvarn switch_rx 0 { ifvare active 1 state playersect_trigger ifvarg active 1 setactorvar[THISACTOR].active 0 } else state playersect_trigger } //триггер реагирующий на игрока, находящиеся в том же секторе что и триггер
ifvare tr_bits 7 { state multiplayer_trigger } //триггер, срабатывающий в мультиплеере при загрузке карты
} else ifvare tr_pal 5 {
	ifvare tr_bits 1 { ifvarn switch_rx 0 { ifvare active 1 state playersect_trigger ifvarg active 1 setactorvar[THISACTOR].active 0 } else state playersect_trigger } //триггер реагирующий на игрока, находящиеся в том же секторе что и триггер
}
enda





/*
state vectorkill_trigger
	setvar sectorid 0
	setvarvar trigger_id_numbers 0
	whilevarvarn sectorid NUMSECTORS 
	{
		headspritesect spriteid sectorid
		whilevarn spriteid -1 
		{
			getactor[spriteid].yvel tx_can
			ifvarvare tx_can switch_rx 
			{
				getactor[spriteid].statnum statnum
				getactor[spriteid].picnum picnum
				ifvarg picnum 32 //not equal special sprites
				ifvarn statnum 1024 
				{ 
					setactor[spriteid].statnum 1 
					{ 
						getactor[spriteid].extra extra 
						getactor[spriteid].htpicnum htpicnum 
						getactor[spriteid].picnum picnum 
						getactorvar[spriteid].burninactivenow burninactivenow
						ifvare burninactivenow 1 { setvarvar hitspritesaved spriteid } //если загорелся
						getactorvar[spriteid].flamedamage flamedamage
						ifvarg flamedamage 0 { ifvarvarg flamedamage extra ifvarl extra 2 setvarvar hitspritesaved spriteid } else { ifvarvare flamedamage extra ifvarl extra 2 setvarvar hitspritesaved spriteid }
						ifvarg htpicnum 0 ifvarn htpicnum 2595 ifvarn htpicnum APLAYER { ifvarvarn htpicnum picnum { ifvarl extra 1 setvarvar hitspritesaved spriteid } else ifvarl extra 1 setvarvar hitspritesaved spriteid }
					} 
				}
				ifvarvare spriteid hitspritesaved 
				{ setactorvar[hitspritesaved].trshoot_var 1 setvar hitspritesaved -1 }
				getactorvar[spriteid].trshoot_var trshoot_var
				ifvarvarg trshoot_var 0 { addvar trigger_id_numbers 1 }	
			}
			nextspritesect spriteid spriteid
		}
		addvar sectorid 1
	}
ends
*/

/*
state shootsprite_trigger //tr_bits = 3
	setvar sectorid 0
	setvarvar trigger_id_numbers 0
	whilevarvarn sectorid NUMSECTORS 
	{
		headspritesect spriteid sectorid
		whilevarn spriteid -1 
		{
			getactor[spriteid].yvel tx_can
			ifvarvare tx_can switch_rx 
			{
				getactor[spriteid].statnum statnum
				ifvarn statnum 1024 
				{ 
					setactor[spriteid].statnum 1 { getactor[spriteid].htextra htextra ifvarg htextra 0 setvarvar hitspritesaved spriteid } 
				}
				ifvarvare spriteid hitspritesaved 
				{ setactorvar[hitspritesaved].trshoot_var 1 setvar hitspritesaved -1 }
				getactorvar[spriteid].trshoot_var trshoot_var
				checkactivatormotion LOTAGSAVED
				ifvare RETURN 0 {
				ifvarvarg trshoot_var 0 { addvar trigger_id_numbers 1 } } else setvar tr_reset 1
			}
			ifvare trigger_var -1 { ifvarvare HITAGSAVED trigger_id_numbers setvar tr_reset 1 }
			nextspritesect spriteid spriteid
		}
		addvar sectorid 1
	}
	ifvare tr_reset 1
	{
		setvar sectorid 0
		whilevarvarn sectorid NUMSECTORS 
		{
			headspritesect spriteid sectorid
			whilevarn spriteid -1 
			{
				getactor[spriteid].yvel tx_can
				ifvarvare tx_can switch_rx 
				{ setactor[spriteid].htextra -1 setactorvar[spriteid].trshoot_var 0 }
				nextspritesect spriteid spriteid
			}
			addvar sectorid 1
		}
		setvar tr_reset 0
	}
ends
*/


/*
state enemykill_trigger //tr_bits = 2
	setvar sectorid 0
	setvarvar trigger_id_numbers TRIGGERNUMSAVED
	whilevarvarn sectorid NUMSECTORS 
	{
		headspritesect spriteid sectorid
		whilevarn spriteid -1 
		{
			getactor[spriteid].yvel tx_can
			ifvarvare tx_can switch_rx 
			{
				getactor[spriteid].extra extra 
				ifvarg extra 0 subvar trigger_id_numbers 1
			}
			nextspritesect spriteid spriteid
		}
		addvar sectorid 1
	}
ends
*/