
/*
LIGHTEFFECTOR bits
Wall hitag = 1 игнорируется факелом, не будет освещаться
Cstat = 4 - Факел будет затемнять сектор
Hitag = 0/1 - Active = 0/1

tr_bits = 0 - Light Switch (SE12)
tr_bits = 1 - Random Light
tr_bits = 2 - Sine Light. Lotag = frequency;
tr_bits = 3 - Wave Light. GPSLotag = величина изменения уровня освещенности, GPSHitag = скорость изменения величины уровня освещенности, GPSZvel = скорость перемещения центра волны;
tr_bits = 4 - Candle light //SECTOR HITAG == 1000


tr_bits = 5 - Square light (amplitude - shade, freq - lotag, phase - blend, floorpal - xoffset, ceilpal - yoffset)
tr_bits = 6 - Saw light (amplitude - shade, freq - lotag, phase - blend, floorpal - xoffset, ceilpal - yoffset)
tr_bits = 7 - Ramp up light (amplitude - shade, freq - lotag, phase - blend, floorpal - xoffset, ceilpal - yoffset)
tr_bits = 8 - Ramp down light (amplitude - shade, freq - lotag, phase - blend, floorpal - xoffset, ceilpal - yoffset)
tr_bits = 9 - Strobe light (amplitude - shade, freq - lotag, phase - blend, floorpal - xoffset, ceilpal - yoffset)
tr_bits = 10 - Search light (amplitude - shade, freq - lotag, phase - blend, floorpal - xoffset, ceilpal - yoffset)
tr_bits = 11 - flicker3 (amplitude - shade, freq - lotag, phase - blend, floorpal - xoffset, ceilpal - yoffset)
tr_bits = 12 - Sine light (amplitude - shade, freq - lotag, phase - blend, floorpal - xoffset, ceilpal - yoffset)
tr_bits = 13 - flicker1 (amplitude - shade, freq - lotag, phase - blend, floorpal - xoffset, ceilpal - yoffset)
tr_bits = 14 - flicker2 (amplitude - shade, freq - lotag, phase - blend, floorpal - xoffset, ceilpal - yoffset)

yvel = le_syncid, sync frequency from other sprite which id has written

owner = le_bits
le_bits = 0 => le_bits = 7
le_bits = 1 - floor light
le_bits = 2 - ceiling light
le_bits = 4 - walls light
le_bits = 8 - do nothing when active = 0...if sector have two LE
*/



eventloadactor LIGHTEFFECTOR
getactor[THISACTOR].lotag LOTAGSAVED
setactor[THISACTOR].lotag 0
getactor[THISACTOR].hitag HITAGSAVED
setactor[THISACTOR].hitag 0
setvarvar active HITAGSAVED
getactor[THISACTOR].shade le_shade
getactor[THISACTOR].cstat le_cstat
getactor[THISACTOR].sectnum sectnum1
getsector[sectnum1].ceilingshade ceilingshadesaved
getsector[sectnum1].floorshade floorshadesaved
getsector[sectnum1].ceilingpal ceilingpalsaved
getsector[sectnum1].floorpal floorpalsaved
getactor[THISACTOR].sectnum sectnum1
getsector[sectnum1].wallnum wallnum
getsector[sectnum1].wallptr wallptr
setvar tmp 0 
ifvarl wallnum 1 break
whilevarvarn tmp wallnum
{ 
	ifvarand wall[wallptr].cstat 2 {
		getwall[wallptr].nextwall se_walltmp
		ifvarn se_walltmp -1 {
			getwall[se_walltmp].shade wallshade_tmp
			setarray WALL_SHADE[se_walltmp] wallshade_tmp
			getwall[se_walltmp].pal wallpal_tmp
			setarray WALL_PAL[se_walltmp] wallpal_tmp
		}
	}
							
	getwall[wallptr].shade wallshade_tmp
	setarray WALL_SHADE[wallptr] wallshade_tmp
	getwall[wallptr].pal wallpal_tmp
	setarray WALL_PAL[wallptr] wallpal_tmp
	addvar wallptr 1
	addvar tmp 1
}
getactor[THISACTOR].zvel tr_bits
ifvare tr_bits 3 nullop else 
ifvare tr_bits 4 { ifvarn HITAGSAVED 1 { setvarvar floorshade_tmp floorshadesaved setvarvar ceilingshade_tmp ceilingshadesaved } } else
{
	ifvare HITAGSAVED 1 { ifvarg le_shade 0 { setvarvar floorshade_tmp le_shade } else setvarvar floorshade_tmp 8 setvarvar ceilingshade_tmp floorshade_tmp 
		getactor[THISACTOR].sectnum sectnum1
		getsector[sectnum1].wallnum wallnum
		getsector[sectnum1].wallptr wallptr
		setvar tmp 0 
		ifvarl wallnum 1 break
		whilevarvarn tmp wallnum
		{ 
			ifvarg le_shade 0 setarray WALL_SHADETMP[wallptr] le_shade else
			setarray WALL_SHADETMP[wallptr] 8
			addvar wallptr 1
			addvar tmp 1
		}

	} else { 
		setvarvar floorshade_tmp floorshadesaved 
		setvarvar ceilingshade_tmp ceilingshadesaved 
		getactor[THISACTOR].sectnum sectnum1
		getsector[sectnum1].wallnum wallnum
		getsector[sectnum1].wallptr wallptr
		setvar tmp 0 
		ifvarl wallnum 1 break
		whilevarvarn tmp wallnum
		{ 
			getwall[wallptr].hitag hitag
			ifvarn hitag 1 setarray WALL_SHADETMP[wallptr] WALL_SHADE[wallptr]
			addvar wallptr 1
			addvar tmp 1
		}
	} 	
}
getactor[THISACTOR].yvel le_owner
getactor[THISACTOR].owner le_bits

ifvare tr_bits 2 {
	ifvare LOTAGSAVED 0 {
		setvar spriteid 0
		whilevarvarn spriteid NUMSPRITES {
			ifvarg le_owner 0 ifvarvarn spriteid THISACTOR
			ifvarvare le_owner sprite[spriteid].yvel { 
				getactor[spriteid].lotag lotag
				ifvare lotag 0 getactorvar[spriteid].LOTAGSAVED lotag
				ifvarg lotag 0 { setvarvar le_syncid spriteid }
			}
			addvar spriteid 1
		}
	}
}

ifvare tr_bits 5 {
	ifvare LOTAGSAVED 0 {
		setvar spriteid 0
		whilevarvarn spriteid NUMSPRITES {
			ifvarg le_owner 0 ifvarvarn spriteid THISACTOR
			ifvarvare le_owner sprite[spriteid].yvel { 
				getactor[spriteid].lotag lotag
				ifvare lotag 0 getactorvar[spriteid].LOTAGSAVED lotag
				ifvarg lotag 0 { setvarvar le_syncid spriteid }
			}
			addvar spriteid 1
		}
	}
	addvarvar COUNTER sprite[THISACTOR].blend
	
	setvarvar le_floorpal sprite[THISACTOR].xoffset
	setvarvar le_ceilpal sprite[THISACTOR].yoffset
	setactor[THISACTOR].xoffset 0
	setactor[THISACTOR].yoffset 0
}
ifvare tr_bits 6 {
	addvarvar COUNTER sprite[THISACTOR].blend
	ifvarg COUNTER 128 setvar le_trig 1

	setvarvar le_floorpal sprite[THISACTOR].xoffset
	setvarvar le_ceilpal sprite[THISACTOR].yoffset
	setactor[THISACTOR].xoffset 0
	setactor[THISACTOR].yoffset 0
}
ifvare tr_bits 7 {
	addvarvar COUNTER sprite[THISACTOR].blend
	setvarvar sine_tmp sprite[THISACTOR].blend
	mulvar sine_tmp -1
	
	setvarvar le_floorpal sprite[THISACTOR].xoffset
	setvarvar le_ceilpal sprite[THISACTOR].yoffset
	setactor[THISACTOR].xoffset 0
	setactor[THISACTOR].yoffset 0
}
ifvare tr_bits 8 {
	addvarvar COUNTER sprite[THISACTOR].blend
	setvarvar sine_tmp sprite[THISACTOR].blend
	
	setvarvar le_floorpal sprite[THISACTOR].xoffset
	setvarvar le_ceilpal sprite[THISACTOR].yoffset
	setactor[THISACTOR].xoffset 0
	setactor[THISACTOR].yoffset 0
}
ifvare tr_bits 9 {
	addvarvar COUNTER sprite[THISACTOR].blend
	setvarvar sine_tmp sprite[THISACTOR].blend
	
	setvarvar le_floorpal sprite[THISACTOR].xoffset
	setvarvar le_ceilpal sprite[THISACTOR].yoffset
	setactor[THISACTOR].xoffset 0
	setactor[THISACTOR].yoffset 0
}
ifvare tr_bits 10 {
	addvarvar COUNTER sprite[THISACTOR].blend
	ifvarg COUNTER 128 setvar le_trig 1

	setvarvar le_floorpal sprite[THISACTOR].xoffset
	setvarvar le_ceilpal sprite[THISACTOR].yoffset
	setactor[THISACTOR].xoffset 0
	setactor[THISACTOR].yoffset 0
}
ifvare tr_bits 11 {
	setvarvar tmp sprite[THISACTOR].blend
	shiftvarl tmp 1
	addvarvar COUNTER tmp

	setvarvar le_floorpal sprite[THISACTOR].xoffset
	setvarvar le_ceilpal sprite[THISACTOR].yoffset
	setactor[THISACTOR].xoffset 0
	setactor[THISACTOR].yoffset 0
}
ifvare tr_bits 12 {
	shiftvarl LOTAGSAVED 2

	setvarvar tmp sprite[THISACTOR].blend
	shiftvarl tmp 3
	addvarvar COUNTER tmp
	
	setvarvar le_floorpal sprite[THISACTOR].xoffset
	setvarvar le_ceilpal sprite[THISACTOR].yoffset
	setactor[THISACTOR].xoffset 0
	setactor[THISACTOR].yoffset 0
}
ifvare tr_bits 13 {
	setvarvar tmp sprite[THISACTOR].blend
	shiftvarl tmp 1
	addvarvar COUNTER tmp

	setvarvar le_floorpal sprite[THISACTOR].xoffset
	setvarvar le_ceilpal sprite[THISACTOR].yoffset
	setactor[THISACTOR].xoffset 0
	setactor[THISACTOR].yoffset 0
}
ifvare tr_bits 14 {
	setvarvar tmp sprite[THISACTOR].blend
	shiftvarl tmp 1
	addvarvar COUNTER tmp

	setvarvar le_floorpal sprite[THISACTOR].xoffset
	setvarvar le_ceilpal sprite[THISACTOR].yoffset
	setactor[THISACTOR].xoffset 0
	setactor[THISACTOR].yoffset 0
}
ifvare tr_bits 4
{
	getactor[THISACTOR].sectnum sect2
	getactor[THISACTOR].yvel le_owner
	getsector[sect2].visibility le_visibsaved
}

setvar spriteid 0
whilevarvarn spriteid NUMSPRITES
{
	getactor[THISACTOR].zvel tr_bits
	ifvare tr_bits 0
	{
		getactor[spriteid].picnum picnum
		ifvare picnum GPSPEED
		{
			getactor[spriteid].xvel rx_can
			getactor[THISACTOR].xvel tx_can
			ifvarn tx_can 0
			ifvarvare tx_can rx_can 
			{ 
				getactor[spriteid].lotag lotag 
				setvarvar le_incrspeed lotag
				getactor[spriteid].hitag hitag
				setvarvar le_attenspeed hitag
				getactor[spriteid].zvel zvel 
				setvarvar le_count zvel
			}
		}
	}
	ifvare tr_bits 3
	{
		getactor[spriteid].picnum picnum
		ifvare picnum GPSPEED
		{
			getactor[spriteid].xvel rx_can
			getactor[THISACTOR].xvel tx_can
			ifvarn tx_can 0
			ifvarvare tx_can rx_can 
			{ 
				getactor[spriteid].lotag lotag 
				setvarvar le_incrspeed lotag
				setvarvar le_attenspeed lotag
				getactor[spriteid].hitag hitag
				setvarvar le_changecnt hitag
				getactor[spriteid].zvel zvel 
				setvarvar le_count zvel
			}
		}
	}
	addvar spriteid 1
}
cstat 32768
getactor[THISACTOR].pal le_wallpal
setactor[THISACTOR].pal 0
enda

state staticlight //tr_bits = 0

		ifvarg le_incrspeed 0
		{
			ifvarg le_count 0
			{
				addvar COUNTER 1
				ifvarvare COUNTER le_count
				{	
					ifvarand le_cstat 4
					{
						ifvarvarl floorshade_tmp le_shade addvarvar floorshade_tmp le_incrspeed
						ifvarvarl ceilingshade_tmp le_shade addvarvar ceilingshade_tmp le_incrspeed
					} else
					{
						ifvarvarg floorshade_tmp le_shade subvarvar floorshade_tmp le_incrspeed
						ifvarvarg ceilingshade_tmp le_shade subvarvar ceilingshade_tmp le_incrspeed
					}
					setvar COUNTER 0
				}
			} else
			{
				ifvarand le_cstat 4
				{
					ifvarvarl floorshade_tmp le_shade addvarvar floorshade_tmp le_incrspeed
					ifvarvarl ceilingshade_tmp le_shade addvarvar ceilingshade_tmp le_incrspeed
				} else
				{
					ifvarvarg floorshade_tmp le_shade subvarvar floorshade_tmp le_incrspeed
					ifvarvarg ceilingshade_tmp le_shade subvarvar ceilingshade_tmp le_incrspeed
				}
			}
		} else
		{
		ifvarand le_cstat 4
		{
			ifvarvarl floorshade_tmp le_shade	addvarvar floorshade_tmp 2
			ifvarvarl ceilingshade_tmp le_shade addvarvar ceilingshade_tmp 2
		} else
		{
			ifvarvarg floorshade_tmp le_shade	subvarvar floorshade_tmp 2
			ifvarvarg ceilingshade_tmp le_shade subvarvar ceilingshade_tmp 2
		}
		}
ends

state randomlight //tr_bits 1

		ifvare le_wallpal 1
		{
			espawn LIGHTEFFECTOR //опорный источник
			setvarvar le_syncid RETURN
			setactorvar[RETURN].floorshadesaved floorshadesaved
			setactorvar[RETURN].ceilingshadesaved ceilingshadesaved
			setactor[RETURN].pal 2
			setactorvar[RETURN].tr_bits 1
			setactor[RETURN].yvel sprite[THISACTOR].yvel
			setactorvar[RETURN].active 1
			setactorvar[RETURN].activatedby THISACTOR
					
			setvar sectorid 0
			whilevarvarn sectorid NUMSECTORS 
			{
				headspritesect spriteid sectorid
				whilevarn spriteid -1 
				{
					ifvarg le_owner 0
					ifvare sprite[spriteid].picnum LIGHTEFFECTOR
					ifvarvare le_owner sprite[spriteid].yvel 
					ifvare sprite[spriteid].pal 0 {					
						setactorvar[spriteid].le_syncid le_syncid 
					}	
					
					nextspritesect spriteid spriteid
				}
				addvar sectorid 1
			}
			setvar le_wallpal 0
		}

		ifvarg le_syncid 0
		{	
			ifvarg le_syncid 0 getactorvar[le_syncid].le_freq le_freq
			ifvare le_freq 1 { setvarvar floorshade_tmp floorshadesaved setvarvar ceilingshade_tmp ceilingshadesaved } else { setvarvar floorshade_tmp le_shade setvarvar ceilingshade_tmp le_shade }
			ifvarand le_cstat 4 nullop else
			{
				ifvarvarl floorshadesaved floorshade_tmp setvarvar floorshade_tmp floorshadesaved
				ifvarvarg floorshade_tmp floorshadesaved setvarvar floorshade_tmp floorshadesaved
				ifvarvarl ceilingshadesaved ceilingshade_tmp setvarvar ceilingshade_tmp ceilingshadesaved
				ifvarvarg ceilingshade_tmp ceilingshadesaved setvarvar ceilingshade_tmp ceilingshadesaved
			}
		}
		else
		{
			randvar COUNTER 16
			ifvarl COUNTER 14 setvar le_freq 1 else setvar le_freq 0
			ifvarand le_cstat 4
			{ 
			ifvare le_freq 1 setvarvar floorshade_tmp floorshadesaved else setvarvar floorshade_tmp le_shade
			ifvarvarl floorshade_tmp floorshadesaved setvarvar floorshade_tmp floorshadesaved
			setvarvar ceilingshade_tmp floorshade_tmp
			} else
			{
			ifvare le_freq 1 setvarvar floorshade_tmp floorshadesaved else setvarvar floorshade_tmp le_shade
			ifvarvarg floorshade_tmp floorshadesaved setvarvar floorshade_tmp floorshadesaved
			setvarvar ceilingshade_tmp floorshade_tmp
			}
		}
ends

state sinelight //tr_bits 2
	ifvarn LOTAGSAVED 0 { 
		addvarvar COUNTER LOTAGSAVED 
		ifvarg COUNTER 32768 setvar COUNTER 0
		sin sine_tmp COUNTER
		addvar sine_tmp 16384 // ot 0 do 32768
		setvarvar le_freq sine_tmp
	} else {
		ifvarn le_syncid -1 {
			setvarvar sine_tmp actorvar[le_syncid].le_freq
		}
	}
			
	//FLOOR
	setvarvar le_tmp1 sine_tmp
	ifvarg le_shade 0 ifvarand le_cstat 4 {
		mulvarvar le_tmp1 le_shade 
	}
	else
		mulvarvar le_tmp1 floorshadesaved
	divvarvar le_tmp1 32768
	setvarvar floorshade_tmp le_tmp1
	ifvarg le_shade 0 {
		ifvarand le_cstat 4 { ifvarvarl floorshade_tmp floorshadesaved setvarvar floorshade_tmp floorshadesaved } else {
		ifvarvarl floorshade_tmp le_shade setvarvar floorshade_tmp le_shade }
	}
	
	//CEILING			
	setvarvar le_tmp1 sine_tmp
	ifvarg le_shade 0 ifvarand le_cstat 4
		mulvarvar le_tmp1 le_shade 
	else
		mulvarvar le_tmp1 ceilingshadesaved
	divvarvar le_tmp1 32768
	setvarvar ceilingshade_tmp le_tmp1
	
	ifvarg le_shade 0 {
		ifvarand le_cstat 4 { ifvarvarl ceilingshade_tmp ceilingshadesaved setvarvar ceilingshade_tmp floorshadesaved } else {
		ifvarvarl ceilingshade_tmp le_shade setvarvar ceilingshade_tmp le_shade }
	}
	
	ifvarand le_bits 1 { setsector[sectnum1].floorshade floorshade_tmp ifvarg le_wallpal 0 setsector[sectnum1].floorpal le_wallpal }
	ifvarand le_bits 2 { setsector[sectnum1].ceilingshade ceilingshade_tmp ifvarg le_wallpal 0 setsector[sectnum1].ceilingpal le_wallpal }
	
	//WALLS
	ifvarand le_bits 4 {
		getsector[sectnum1].wallnum wallnum
		getsector[sectnum1].wallptr wallptr
		setvar tmp 0
		ifvarl wallnum 1 break
		whilevarvarn tmp wallnum
		{
			setvarvar le_tmp1 sine_tmp
			ifvarg le_shade 0 ifvarand le_cstat 4
				mulvarvar le_tmp1 le_shade 
			else
				mulvarvar le_tmp1 WALL_SHADE[wallptr]
			divvarvar le_tmp1 32768
			setvarvar wallshade_tmp le_tmp1
			ifvarg le_shade 0 {
				ifvarand le_cstat 4 { ifvarvarl wallshade_tmp WALL_SHADE[wallptr] setvarvar wallshade_tmp floorshadesaved } else {
				ifvarvarl wallshade_tmp le_shade setvarvar wallshade_tmp le_shade }
			}
			
			ifvarand wall[wallptr].cstat 2 {
				getwall[wallptr].nextwall se_walltmp
				ifvarn se_walltmp -1 ifvarn wall[se_walltmp].hitag 1 
					setwall[se_walltmp].shade wallshade_tmp 
			}
			ifvarn wall[wallptr].hitag 1 
				setwall[wallptr].shade wallshade_tmp
				
			ifvarg le_wallpal 0 setwall[wallptr].pal le_wallpal
							
			addvar wallptr 1 
			addvar tmp 1	
		}
	}
ends

state radius_wavelight //tr_bits 3
addvarvar le_tmp 1
ifvarvarg le_tmp le_count
{
setvar le_switch 1
ifvarg le_owner 0
{
	setvar sectorid 0
	whilevarvarn sectorid NUMSECTORS 
	{
		headspritesect spriteid sectorid
		whilevarn spriteid -1 
		{
			getactorvar[spriteid].se_xvel rx_can
			ifvarvare le_owner rx_can 
			{ 
				setactorvar[spriteid].le_incrspeed le_incrspeed
				setactorvar[spriteid].le_attenspeed le_attenspeed
				setactorvar[spriteid].le_count le_count
				setactorvar[spriteid].le_changecnt le_changecnt
				setactorvar[spriteid].active 1 
				setactorvar[spriteid].activatedby THISACTOR
			}
			nextspritesect spriteid spriteid
		}
		addvar sectorid 1
	}
}
			
setvar active 0 
setvar le_tmp 0
}
ends

state candlelight //tr_bits 4
ifvarvare le_owner 0 //yvel
		{
			ifrnd 96 randvar sector_vis 12
			ifrnd 128 { randvar sector_vis2 24 subvarvar sector_vis2 6 }
		} else
		{
			ifrnd 96 randvarvar sector_vis le_owner
			ifrnd 128 { randvar sector_vis2 24 subvarvar sector_vis2 6 }
		}
		
		setvar sectnum 0
		whilevarvarn sectnum NUMSECTORS
		{
			getsector[sectnum].hitag HITAG
			ifvarvare HITAG LOTAGSAVED 
			{ 
				setsector[sectnum].visibility sector_vis 
				ifvare le_bits 1
				{
					ifvare SECT_SHADE[sectnum] 0 ifvarn EOL_VAR 1 { getsector[sectnum].floorshade tmp setarray SECT_SHADE[sectnum] tmp }
					setvarvar shade SECT_SHADE[sectnum]
					divvarvar sector_vis2 2
					addvarvar shade sector_vis2
					setsector[sectnum].floorshade shade
					setsector[sectnum].ceilingshade shade
					getsector[sectnum].wallnum wallnum
					getsector[sectnum].wallptr wallptr
					setvar tmp 0 
					ifvarl wallnum 1 break
					whilevarvarn tmp wallnum
					{ 
						getwall[wallptr].hitag hitag
						ifvarn hitag 1 setwall[wallptr].shade shade
						addvar wallptr 1
						addvar tmp 1
					}
				}
			}
			addvar sectnum 1
		}
ends

state squarelight //tr_bits 5
	ifvarn LOTAGSAVED 0 { 
		setvarvar le_freq LOTAGSAVED
		addvarvar COUNTER le_freq 
		ifvarvarg COUNTER 255 setvar COUNTER 0
		ifvare COUNTER 0 {
			ifvare le_trig 0 setvar le_trig 1
			else setvar le_trig 0
		}
	} else {
		ifvarn le_syncid -1 {
			setvarvar le_trig actorvar[le_syncid].le_trig
		}
	}
	
	setvarvar le_amplitude floorshadesaved		
	addvarvar le_amplitude le_shade

	//FLOOR
	ifvare le_trig 1
		setvarvar floorshade_tmp le_amplitude
	else setvarvar floorshade_tmp floorshadesaved
	
	//CEILING
	ifvare le_trig 1
		setvarvar ceilingshade_tmp le_amplitude
	else setvarvar ceilingshade_tmp ceilingshadesaved	
	
	ifvarand le_bits 1 { 
		setsector[sectnum1].floorshade floorshade_tmp 
		ifvarg le_floorpal 0 {
			ifvare le_trig 1 setsector[sectnum1].floorpal le_floorpal else setsector[sectnum1].floorpal floorpalsaved
		}
	}
			
	ifvarand le_bits 2 { 
		setsector[sectnum1].ceilingshade ceilingshade_tmp 
		ifvarg le_ceilpal 0 {
			ifvare le_trig 1 setsector[sectnum1].ceilingpal le_ceilpal else setsector[sectnum1].ceilingpal ceilingpalsaved
		}
	}
	
	//WALLS
	ifvarand le_bits 4 {
		getsector[sectnum1].wallnum wallnum
		getsector[sectnum1].wallptr wallptr
		setvar tmp 0
		ifvarl wallnum 1 break
		whilevarvarn tmp wallnum
		{
			ifvare le_trig 1
				setvarvar wallshade_tmp le_amplitude
			else setvarvar wallshade_tmp WALL_SHADE[wallptr]	
			
			ifvarand wall[wallptr].cstat 2 {
				getwall[wallptr].nextwall se_walltmp
				ifvarn se_walltmp -1 ifvarn wall[se_walltmp].hitag 1 
					setwall[se_walltmp].shade wallshade_tmp 
			}
			ifvarn wall[wallptr].hitag 1 
				setwall[wallptr].shade wallshade_tmp
				
			ifvarg le_floorpal 0 { 
				ifvare le_trig 1 setwall[wallptr].pal le_floorpal else setwall[wallptr].pal WALL_PAL[wallptr]
			}
							
			addvar wallptr 1 
			addvar tmp 1	
		}
	}
ends

state sawlight //tr_bits 6
	ifvarn LOTAGSAVED 0 { 
		setvarvar le_freq LOTAGSAVED
		divvarvar le_freq 2
		addvarvar COUNTER le_freq
		ifvarg COUNTER 255 { setvar COUNTER 0 setvar sine_tmp 1 }
		ifvarl COUNTER 129 setvar le_trig 0 else setvar le_trig 1
		
		ifvare le_trig 0 {
			subvarvar sine_tmp le_freq
		} else addvarvar sine_tmp le_freq
		
		
		setvarvar le_tmp sine_tmp
		mulvarvar le_tmp le_shade
		divvarvar le_tmp 128
		
		setvarvar le_amplitude floorshadesaved
		subvarvar le_amplitude le_tmp

		
	} else {
		ifvarn le_syncid -1 {
			setvarvar le_trig actorvar[le_syncid].le_trig
		}
	}
	
	
	//FLOOR
	setvarvar floorshade_tmp le_amplitude
	
	//CEILING
	setvarvar ceilingshade_tmp le_amplitude
	
	ifvarand le_bits 1 { 
		setsector[sectnum1].floorshade floorshade_tmp 
		ifvarg le_floorpal 0 {
			setsector[sectnum1].floorpal le_floorpal
		}
	}
			
	ifvarand le_bits 2 { 
		setsector[sectnum1].ceilingshade ceilingshade_tmp 
		ifvarg le_ceilpal 0 {
			setsector[sectnum1].ceilingpal le_ceilpal
		}
	}
	
	//WALLS
	ifvarand le_bits 4 {
		getsector[sectnum1].wallnum wallnum
		getsector[sectnum1].wallptr wallptr
		setvar tmp 0
		ifvarl wallnum 1 break
		whilevarvarn tmp wallnum
		{
			setvarvar wallshade_tmp le_amplitude
			
			ifvarand wall[wallptr].cstat 2 {
				getwall[wallptr].nextwall se_walltmp
				ifvarn se_walltmp -1 ifvarn wall[se_walltmp].hitag 1 
					setwall[se_walltmp].shade wallshade_tmp 
			}
			ifvarn wall[wallptr].hitag 1 
				setwall[wallptr].shade wallshade_tmp
				
			ifvarg le_floorpal 0 { 
				setwall[wallptr].pal le_floorpal
			}
							
			addvar wallptr 1 
			addvar tmp 1	
		}
	}
ends

state rampuplight  //tr_bits 7
	ifvarn LOTAGSAVED 0 { 
		setvarvar le_freq LOTAGSAVED
		divvarvar le_freq 2
		addvarvar COUNTER le_freq
		ifvarg COUNTER 255 { setvar COUNTER 0 setvar sine_tmp 0 }

		subvarvar sine_tmp le_freq

		setvarvar le_tmp sine_tmp
		mulvarvar le_tmp le_shade
		divvarvar le_tmp 256
		
		setvarvar le_amplitude floorshadesaved
		subvarvar le_amplitude le_tmp
	} else {
		ifvarn le_syncid -1 {
			setvarvar le_trig actorvar[le_syncid].le_trig
		}
	}

	//FLOOR
	setvarvar floorshade_tmp le_amplitude
	
	//CEILING
	setvarvar ceilingshade_tmp le_amplitude
	
	ifvarand le_bits 1 { 
		setsector[sectnum1].floorshade floorshade_tmp 
		ifvarg le_floorpal 0 {
			setsector[sectnum1].floorpal le_floorpal
		}
	}
			
	ifvarand le_bits 2 { 
		setsector[sectnum1].ceilingshade ceilingshade_tmp 
		ifvarg le_ceilpal 0 {
			setsector[sectnum1].ceilingpal le_ceilpal
		}
	}
	
	//WALLS
	ifvarand le_bits 4 {
		getsector[sectnum1].wallnum wallnum
		getsector[sectnum1].wallptr wallptr
		setvar tmp 0
		ifvarl wallnum 1 break
		whilevarvarn tmp wallnum
		{
			setvarvar wallshade_tmp le_amplitude
			
			ifvarand wall[wallptr].cstat 2 {
				getwall[wallptr].nextwall se_walltmp
				ifvarn se_walltmp -1 ifvarn wall[se_walltmp].hitag 1 
					setwall[se_walltmp].shade wallshade_tmp 
			}
			ifvarn wall[wallptr].hitag 1 
				setwall[wallptr].shade wallshade_tmp
				
			ifvarg le_floorpal 0 { 
				setwall[wallptr].pal le_floorpal
			}
							
			addvar wallptr 1 
			addvar tmp 1	
		}
	}
ends

state rampdownlight //tr_bits 8
	ifvarn LOTAGSAVED 0 { 
		setvarvar le_freq LOTAGSAVED
		divvarvar le_freq 2
		addvarvar COUNTER le_freq

		ifvarg COUNTER 255 { setvar COUNTER 0 setvarvar sine_tmp 0 }

		addvarvar sine_tmp le_freq

		setvarvar le_tmp sine_tmp
		mulvarvar le_tmp le_shade
		divvarvar le_tmp 256
		
		
		setvarvar le_tmp2 floorshadesaved
		addvarvar le_tmp2 le_shade
		
		setvarvar le_amplitude le_tmp2
		subvarvar le_amplitude le_tmp	
	} else {
		ifvarn le_syncid -1 {
			setvarvar le_trig actorvar[le_syncid].le_trig
		}
	}

	//FLOOR
	setvarvar floorshade_tmp le_amplitude
	
	//CEILING
	setvarvar ceilingshade_tmp le_amplitude
	
	ifvarand le_bits 1 { 
		setsector[sectnum1].floorshade floorshade_tmp 
		ifvarg le_floorpal 0 {
			setsector[sectnum1].floorpal le_floorpal
		}
	}
			
	ifvarand le_bits 2 { 
		setsector[sectnum1].ceilingshade ceilingshade_tmp 
		ifvarg le_ceilpal 0 {
			setsector[sectnum1].ceilingpal le_ceilpal
		}
	}
	
	//WALLS
	ifvarand le_bits 4 {
		getsector[sectnum1].wallnum wallnum
		getsector[sectnum1].wallptr wallptr
		setvar tmp 0
		ifvarl wallnum 1 break
		whilevarvarn tmp wallnum
		{
			setvarvar wallshade_tmp le_amplitude
			
			ifvarand wall[wallptr].cstat 2 {
				getwall[wallptr].nextwall se_walltmp
				ifvarn se_walltmp -1 ifvarn wall[se_walltmp].hitag 1 
					setwall[se_walltmp].shade wallshade_tmp 
			}
			ifvarn wall[wallptr].hitag 1 
				setwall[wallptr].shade wallshade_tmp
				
			ifvarg le_floorpal 0 { 
				setwall[wallptr].pal le_floorpal
			}
							
			addvar wallptr 1 
			addvar tmp 1	
		}
	}
ends

state strobelight //tr_bits 9
	ifvarn LOTAGSAVED 0 { 
		setvarvar le_freq LOTAGSAVED
		addvarvar COUNTER le_freq

		ifvarg COUNTER 511 { setvar COUNTER 0 setvarvar sine_tmp 0 }

		addvarvar sine_tmp le_freq

		setvarvar le_tmp sine_tmp
		mulvarvar le_tmp le_shade
		divvarvar le_tmp 128

		setvarvar le_tmp2 floorshadesaved
		addvarvar le_tmp2 le_shade
		
		setvarvar le_amplitude le_tmp2
		subvarvar le_amplitude le_tmp	
		
		ifvarvarg le_amplitude floorshadesaved
		setvarvar le_amplitude floorshadesaved

	} else {
		ifvarn le_syncid -1 {
			setvarvar le_trig actorvar[le_syncid].le_trig
		}
	}

	//FLOOR
	setvarvar floorshade_tmp le_amplitude
	
	//CEILING
	setvarvar ceilingshade_tmp le_amplitude
	
	ifvarand le_bits 1 { 
		setsector[sectnum1].floorshade floorshade_tmp 
		ifvarg le_floorpal 0 {
			setsector[sectnum1].floorpal le_floorpal
		}
	}
			
	ifvarand le_bits 2 { 
		setsector[sectnum1].ceilingshade ceilingshade_tmp 
		ifvarg le_ceilpal 0 {
			setsector[sectnum1].ceilingpal le_ceilpal
		}
	}
	
	//WALLS
	ifvarand le_bits 4 {
		getsector[sectnum1].wallnum wallnum
		getsector[sectnum1].wallptr wallptr
		setvar tmp 0
		ifvarl wallnum 1 break
		whilevarvarn tmp wallnum
		{
			setvarvar wallshade_tmp le_amplitude
			
			ifvarand wall[wallptr].cstat 2 {
				getwall[wallptr].nextwall se_walltmp
				ifvarn se_walltmp -1 ifvarn wall[se_walltmp].hitag 1 
					setwall[se_walltmp].shade wallshade_tmp 
			}
			ifvarn wall[wallptr].hitag 1 
				setwall[wallptr].shade wallshade_tmp
				
			ifvarg le_floorpal 0 { 
				setwall[wallptr].pal le_floorpal
			}
							
			addvar wallptr 1 
			addvar tmp 1	
		}
	}
ends

state searchlight //tr_bits 10
	ifvarn LOTAGSAVED 0 { 
		setvarvar le_freq LOTAGSAVED
		addvarvar COUNTER le_freq
		ifvarg COUNTER 512 { setvar COUNTER 0 setvar sine_tmp 1 }
		ifvarl COUNTER 65 setvar le_trig 0 else setvar le_trig 1

		mulvar le_freq 2
		ifvare le_trig 0 {
			subvarvar sine_tmp le_freq
		} else addvarvar sine_tmp le_freq
		
		
		setvarvar le_tmp sine_tmp
		mulvarvar le_tmp le_shade
		divvarvar le_tmp 128
		
		setvarvar le_amplitude floorshadesaved
		subvarvar le_amplitude le_tmp
		
		ifvarvarg le_amplitude floorshadesaved
		setvarvar le_amplitude floorshadesaved
		
	} else {
		ifvarn le_syncid -1 {
			setvarvar le_trig actorvar[le_syncid].le_trig
		}
	}
	
	
	//FLOOR
	setvarvar floorshade_tmp le_amplitude
	
	//CEILING
	setvarvar ceilingshade_tmp le_amplitude
	
	ifvarand le_bits 1 { 
		setsector[sectnum1].floorshade floorshade_tmp 
		ifvarg le_floorpal 0 {
			setsector[sectnum1].floorpal le_floorpal
		}
	}
			
	ifvarand le_bits 2 { 
		setsector[sectnum1].ceilingshade ceilingshade_tmp 
		ifvarg le_ceilpal 0 {
			setsector[sectnum1].ceilingpal le_ceilpal
		}
	}
	
	//WALLS
	ifvarand le_bits 4 {
		getsector[sectnum1].wallnum wallnum
		getsector[sectnum1].wallptr wallptr
		setvar tmp 0
		ifvarl wallnum 1 break
		whilevarvarn tmp wallnum
		{
			setvarvar wallshade_tmp le_amplitude
			
			ifvarand wall[wallptr].cstat 2 {
				getwall[wallptr].nextwall se_walltmp
				ifvarn se_walltmp -1 ifvarn wall[se_walltmp].hitag 1 
					setwall[se_walltmp].shade wallshade_tmp 
			}
			ifvarn wall[wallptr].hitag 1 
				setwall[wallptr].shade wallshade_tmp
				
			ifvarg le_floorpal 0 { 
				setwall[wallptr].pal le_floorpal
			}
							
			addvar wallptr 1 
			addvar tmp 1	
		}
	}
ends
gamevar le_arraylen 0 2
gamevar le_divscale 0 2

state flic3light //tr_bits 11
	ifvarn LOTAGSAVED 0 { 
		setvar le_arraylen 63
		setvarvar le_freq LOTAGSAVED
		addvarvar COUNTER le_freq
		ifvarg COUNTER 512 { setvar COUNTER 0 }
		setvarvar le_divscale 512
		divvarvar le_divscale le_arraylen
		setvarvar sine_tmp COUNTER
		divvarvar sine_tmp le_divscale
		ifvarg sine_tmp 63 setvar sine_tmp 63
		setvarvar le_tmp2 FLICKER3[sine_tmp]

		setvarvar le_tmp le_shade
		mulvarvar le_tmp le_tmp2
		divvarvar le_tmp 100

		setvarvar le_amplitude floorshadesaved
		addvarvar le_amplitude le_tmp
	} else {
		ifvarn le_syncid -1 {
			setvarvar le_trig actorvar[le_syncid].le_trig
		}
	}
	
	
	//FLOOR
	setvarvar floorshade_tmp le_amplitude
	
	//CEILING
	setvarvar ceilingshade_tmp le_amplitude
	
	ifvarand le_bits 1 { 
		setsector[sectnum1].floorshade floorshade_tmp 
		ifvarg le_floorpal 0 {
			setsector[sectnum1].floorpal le_floorpal
		}
	}
			
	ifvarand le_bits 2 { 
		setsector[sectnum1].ceilingshade ceilingshade_tmp 
		ifvarg le_ceilpal 0 {
			setsector[sectnum1].ceilingpal le_ceilpal
		}
	}
	
	//WALLS
	ifvarand le_bits 4 {
		getsector[sectnum1].wallnum wallnum
		getsector[sectnum1].wallptr wallptr
		setvar tmp 0
		ifvarl wallnum 1 break
		whilevarvarn tmp wallnum
		{
			setvarvar wallshade_tmp le_amplitude
			
			ifvarand wall[wallptr].cstat 2 {
				getwall[wallptr].nextwall se_walltmp
				ifvarn se_walltmp -1 ifvarn wall[se_walltmp].hitag 1 
					setwall[se_walltmp].shade wallshade_tmp 
			}
			ifvarn wall[wallptr].hitag 1 
				setwall[wallptr].shade wallshade_tmp
				
			ifvarg le_floorpal 0 { 
				setwall[wallptr].pal le_floorpal
			}
							
			addvar wallptr 1 
			addvar tmp 1	
		}
	}
ends

state flic1light //tr_bits 13
	ifvarn LOTAGSAVED 0 { 
		setvar le_arraylen 63
		setvarvar le_freq LOTAGSAVED
		addvarvar COUNTER le_freq
		ifvarg COUNTER 512 { setvar COUNTER 0 }
		setvarvar le_divscale 512
		divvarvar le_divscale le_arraylen
		setvarvar sine_tmp COUNTER
		divvarvar sine_tmp le_divscale
		ifvarg sine_tmp 63 setvar sine_tmp 63
		setvarvar le_tmp2 FLICKER1[sine_tmp]

		setvarvar le_tmp le_shade
		mulvarvar le_tmp le_tmp2
		divvarvar le_tmp 100

		setvarvar le_amplitude floorshadesaved
		addvarvar le_amplitude le_tmp
	} else {
		ifvarn le_syncid -1 {
			setvarvar le_trig actorvar[le_syncid].le_trig
		}
	}
	
	
	//FLOOR
	setvarvar floorshade_tmp le_amplitude
	
	//CEILING
	setvarvar ceilingshade_tmp le_amplitude
	
	ifvarand le_bits 1 { 
		setsector[sectnum1].floorshade floorshade_tmp 
		ifvarg le_floorpal 0 {
			setsector[sectnum1].floorpal le_floorpal
		}
	}
			
	ifvarand le_bits 2 { 
		setsector[sectnum1].ceilingshade ceilingshade_tmp 
		ifvarg le_ceilpal 0 {
			setsector[sectnum1].ceilingpal le_ceilpal
		}
	}
	
	//WALLS
	ifvarand le_bits 4 {
		getsector[sectnum1].wallnum wallnum
		getsector[sectnum1].wallptr wallptr
		setvar tmp 0
		ifvarl wallnum 1 break
		whilevarvarn tmp wallnum
		{
			setvarvar wallshade_tmp le_amplitude
			
			ifvarand wall[wallptr].cstat 2 {
				getwall[wallptr].nextwall se_walltmp
				ifvarn se_walltmp -1 ifvarn wall[se_walltmp].hitag 1 
					setwall[se_walltmp].shade wallshade_tmp 
			}
			ifvarn wall[wallptr].hitag 1 
				setwall[wallptr].shade wallshade_tmp
				
			ifvarg le_floorpal 0 { 
				setwall[wallptr].pal le_floorpal
			}
							
			addvar wallptr 1 
			addvar tmp 1	
		}
	}
ends

state flic2light //tr_bits 14
	ifvarn LOTAGSAVED 0 { 
		setvar le_arraylen 63
		setvarvar le_freq LOTAGSAVED
		addvarvar COUNTER le_freq
		ifvarg COUNTER 512 { setvar COUNTER 0 }
		setvarvar le_divscale 512
		divvarvar le_divscale le_arraylen
		setvarvar sine_tmp COUNTER
		divvarvar sine_tmp le_divscale
		ifvarg sine_tmp 63 setvar sine_tmp 63
		setvarvar le_tmp2 FLICKER2[sine_tmp]

		setvarvar le_tmp le_shade
		mulvarvar le_tmp le_tmp2
		divvarvar le_tmp 100

		setvarvar le_amplitude floorshadesaved
		addvarvar le_amplitude le_tmp
	} else {
		ifvarn le_syncid -1 {
			setvarvar le_trig actorvar[le_syncid].le_trig
		}
	}
	
	
	//FLOOR
	setvarvar floorshade_tmp le_amplitude
	
	//CEILING
	setvarvar ceilingshade_tmp le_amplitude
	
	ifvarand le_bits 1 { 
		setsector[sectnum1].floorshade floorshade_tmp 
		ifvarg le_floorpal 0 {
			setsector[sectnum1].floorpal le_floorpal
		}
	}
			
	ifvarand le_bits 2 { 
		setsector[sectnum1].ceilingshade ceilingshade_tmp 
		ifvarg le_ceilpal 0 {
			setsector[sectnum1].ceilingpal le_ceilpal
		}
	}
	
	//WALLS
	ifvarand le_bits 4 {
		getsector[sectnum1].wallnum wallnum
		getsector[sectnum1].wallptr wallptr
		setvar tmp 0
		ifvarl wallnum 1 break
		whilevarvarn tmp wallnum
		{
			setvarvar wallshade_tmp le_amplitude
			
			ifvarand wall[wallptr].cstat 2 {
				getwall[wallptr].nextwall se_walltmp
				ifvarn se_walltmp -1 ifvarn wall[se_walltmp].hitag 1 
					setwall[se_walltmp].shade wallshade_tmp 
			}
			ifvarn wall[wallptr].hitag 1 
				setwall[wallptr].shade wallshade_tmp
				
			ifvarg le_floorpal 0 { 
				setwall[wallptr].pal le_floorpal
			}
							
			addvar wallptr 1 
			addvar tmp 1	
		}
	}
ends

state bloodsinelight //tr_bits 12

	ifvarn LOTAGSAVED 0 { 
		setvarvar le_freq LOTAGSAVED
		addvarvar COUNTER le_freq
		ifvarg COUNTER 2047 setvar COUNTER 0
		sin sine_tmp COUNTER
		addvar sine_tmp 16384 // ot 0 do 32768

		mulvarvar sine_tmp le_shade
		shiftvarr sine_tmp 15
	}

	//FLOOR
	setvarvar floorshade_tmp floorshadesaved
	addvarvar floorshade_tmp sine_tmp
	
	//CEILING
	setvarvar ceilingshade_tmp ceilingshadesaved
	addvarvar ceilingshade_tmp sine_tmp
	
	ifvarand le_bits 1 { 
		setsector[sectnum1].floorshade floorshade_tmp 
		ifvarg le_floorpal 0 {
			setsector[sectnum1].floorpal le_floorpal
		}
	}
			
	ifvarand le_bits 2 { 
		setsector[sectnum1].ceilingshade ceilingshade_tmp 
		ifvarg le_ceilpal 0 {
			setsector[sectnum1].ceilingpal le_ceilpal
		}
	}
	
	//WALLS
	ifvarand le_bits 4 {
		getsector[sectnum1].wallnum wallnum
		getsector[sectnum1].wallptr wallptr
		setvar tmp 0
		ifvarl wallnum 1 break
		whilevarvarn tmp wallnum
		{
			setvarvar wallshade_tmp WALL_SHADE[wallptr]
			addvarvar wallshade_tmp sine_tmp
			
			ifvarand wall[wallptr].cstat 2 {
				getwall[wallptr].nextwall se_walltmp
				ifvarn se_walltmp -1 ifvarn wall[se_walltmp].hitag 1 
					setwall[se_walltmp].shade wallshade_tmp 
			}
			ifvarn wall[wallptr].hitag 1 
				setwall[wallptr].shade wallshade_tmp
				
			ifvarg le_floorpal 0 { 
				setwall[wallptr].pal le_floorpal
			}
							
			addvar wallptr 1 
			addvar tmp 1	
		}
	}
ends

state func_light_to_sector
	ifvarand le_bits 1 { 
		ifvarn tr_bits 4 { setsector[sectnum1].floorshade floorshade_tmp ifvarg le_wallpal 0 setsector[sectnum1].floorpal le_wallpal } 
	}
	ifvarand le_bits 2 { 
		ifvarn tr_bits 4 { setsector[sectnum1].ceilingshade ceilingshade_tmp ifvarg le_wallpal 0 setsector[sectnum1].ceilingpal le_wallpal } 
	}
	ifvarand le_bits 4
	{
		ifvarn tr_bits 4
		ifvarn tr_bits 3
		{
			ifvarg le_count 0
			{
				addvar wallshade_count 1
				ifvarvare wallshade_count le_count 
				{
					getsector[sectnum1].wallnum wallnum
					getsector[sectnum1].wallptr wallptr
					setvar tmp 0
					ifvarl wallnum 1 break
					whilevarvarn tmp wallnum
					{ 
						ifvarg le_incrspeed 0
						{
							setvarvar wallshade_tmp WALL_SHADETMP[wallptr]
							ifvarvarg wallshade_tmp le_shade subvarvar wallshade_tmp le_incrspeed
							setarray WALL_SHADETMP[wallptr] wallshade_tmp
		
							ifvarand wall[wallptr].cstat 2 {
								getwall[wallptr].nextwall se_walltmp
								ifvarn se_walltmp -1 ifvarn wall[se_walltmp].hitag 1 
									setwall[se_walltmp].shade wallshade_tmp 
							}
							ifvarn wall[wallptr].hitag 1 
								setwall[wallptr].shade wallshade_tmp
						} else { 
							ifvarand wall[wallptr].cstat 2 {
								getwall[wallptr].nextwall se_walltmp
								ifvarn se_walltmp -1 ifvarn wall[se_walltmp].hitag 1 
									setwall[se_walltmp].shade floorshade_tmp 
							} 
							ifvarn wall[wallptr].hitag 1 
								setwall[wallptr].shade floorshade_tmp
						}
						
						ifvarg le_wallpal 0 setwall[wallptr].pal le_wallpal
						
						addvar wallptr 1 
						addvar tmp 1	
					}
					setvar wallshade_count 0
				}
			} else
			{
				getsector[sectnum1].wallnum wallnum
				getsector[sectnum1].wallptr wallptr
				setvar tmp 0
				ifvarl wallnum 1 break
				whilevarvarn tmp wallnum
				{ 

					ifvarg le_incrspeed 0
					{
						setvarvar wallshade_tmp WALL_SHADETMP[wallptr]
						ifvarvarg wallshade_tmp le_shade subvarvar wallshade_tmp le_incrspeed
						setarray WALL_SHADETMP[wallptr] wallshade_tmp
						ifvarand wall[wallptr].cstat 2 {
							getwall[wallptr].nextwall se_walltmp
							ifvarn se_walltmp -1 ifvarn wall[se_walltmp].hitag 1 
								setwall[se_walltmp].shade wallshade_tmp 
						} 
						ifvarn wall[wallptr].hitag 1 
							setwall[wallptr].shade wallshade_tmp
					} else 
					{
						ifvare tr_bits 1
						{
							setvarvar wallshade_tmp WALL_SHADETMP[wallptr]
							ifvare le_freq 1 setvarvar wallshade_tmp WALL_SHADE[wallptr] else setvarvar wallshade_tmp le_shade
							ifvarand le_cstat 4 {
							ifvarvarl wallshade_tmp WALL_SHADE[wallptr] setvarvar wallshade_tmp WALL_SHADE[wallptr] } else {
							ifvarvarg wallshade_tmp WALL_SHADE[wallptr] setvarvar wallshade_tmp WALL_SHADE[wallptr] }
							ifvarand wall[wallptr].cstat 2 {
								getwall[wallptr].nextwall se_walltmp
								ifvarn se_walltmp -1 ifvarn wall[se_walltmp].hitag 1 
									setwall[se_walltmp].shade wallshade_tmp 
							} 
							
							ifvarn wall[wallptr].hitag 1 
								setwall[wallptr].shade wallshade_tmp
						} else { 
							ifvarand wall[wallptr].cstat 2 {
								getwall[wallptr].nextwall se_walltmp
								ifvarn se_walltmp -1 ifvarn wall[se_walltmp].hitag 1 
									setwall[se_walltmp].shade floorshade_tmp 
							} 
							ifvarn wall[wallptr].hitag 1 
								setwall[wallptr].shade floorshade_tmp 
							
						}
					}
					
					ifvarg le_wallpal 0 setwall[wallptr].pal le_wallpal
						
					
					addvar wallptr 1 
					addvar tmp 1	
					
				}
			}
		}
	}
ends

state radious_wavelight_off //tr_bits 3
ifvare le_switch 1
{
	ifvarvarg floorshade_tmp 0 { ifvarg le_changecnt 0 { addvarvar le_changetmp 1 ifvarvarg le_changetmp le_changecnt { subvarvar floorshade_tmp le_incrspeed setvar le_changetmp 0 } } else subvarvar floorshade_tmp le_incrspeed } else setvar le_switch 0
	setvarvar ceilingshade_tmp floorshade_tmp
} else
{
	ifvarg le_attenspeed 0
	{
		ifvarvarl floorshade_tmp floorshadesaved { ifvarg le_changecnt 0 { addvarvar le_changetmp 1 ifvarvarg le_changetmp le_changecnt { addvarvar floorshade_tmp le_attenspeed setvar le_changetmp 0 } } else addvarvar floorshade_tmp le_attenspeed } else setvarvar floorshade_tmp floorshadesaved
		//ifvarvarl ceilingshade_tmp ceilingshadesaved { ifvarg le_changecnt 0 { addvarvar le_changetmp 1 ifvarvarg le_changetmp le_changecnt { addvarvar ceilingshade_tmp le_attenspeed setvar le_changetmp 0 } } else addvarvar ceilingshade_tmp le_attenspeed } else setvarvar  ceilingshade_tmp ceilingshadesaved
		setvarvar ceilingshade_tmp floorshade_tmp
	} else //затенение сразу после загрузки карты
	{
		ifvarvarl floorshade_tmp floorshadesaved addvarvar floorshade_tmp 2 else setvarvar floorshade_tmp floorshadesaved
		ifvarvarl ceilingshade_tmp ceilingshadesaved addvarvar ceilingshade_tmp 2 else setvarvar ceilingshade_tmp ceilingshadesaved
	}
}
ends

state candle_light_off
whilevarvarn sectnum NUMSECTORS
		{
			getsector[sectnum].hitag HITAG
			ifvarvare HITAG LOTAGSAVED 
			{ 
				setsector[sectnum].visibility le_visibsaved 
				ifvare le_bits 1
				{
					setsector[sectnum].floorshade floorshadesaved
					setsector[sectnum].ceilingshade floorshadesaved
					getsector[sectnum].wallnum wallnum
					getsector[sectnum].wallptr wallptr
					setvar tmp 0 
					ifvarl wallnum 1 break
					whilevarvarn tmp wallnum
					{ 
						getwall[wallptr].hitag hitag
						ifvarn hitag 1 setwall[wallptr].shade floorshadesaved
						addvar wallptr 1
						addvar tmp 1
					}
				}
			}
			addvar sectnum 1
		}
ends

state radiuslight_off

		setsector[le_savesector].floorshade floorshadesaved
		setsector[le_savesector].ceilingshade ceilingshadesaved
		getsector[le_savesector].wallnum wallnum
		getsector[le_savesector].wallptr wallptr
		setvar tmp 0
		ifvarl wallnum 1 break
		whilevarvarn tmp wallnum
		{ 
			getwall[wallptr].hitag hitag
			ifvarn hitag 1 setwall[wallptr].shade floorshadesaved
			addvar wallptr 1
			addvar tmp 1
		}
		killit
ends

state randomlight_off
	setvar le_attenspeed 2
	ifvarvarl floorshade_tmp floorshadesaved addvarvar floorshade_tmp le_attenspeed else setvarvar floorshade_tmp floorshadesaved
	setvarvar ceilingshade_tmp ceilingshadesaved
ends

state sinelight_off

	setvar COUNTER 0
	setvar sine_tmp 0
	setvarvar floorshade_tmp floorshadesaved
	setvarvar ceilingshade_tmp ceilingshadesaved
	
ends

state bsinelight_off

	setvarvar floorshade_tmp floorshadesaved
	setvarvar ceilingshade_tmp ceilingshadesaved
	
ends

state func_light_floorandceiling_off
ifvarn tr_bits 3
{
	ifvarg le_attenspeed 0
	{
		ifvarg le_count 0
		{
			addvar COUNTER 1
			ifvarvare COUNTER le_count
			{
				ifvarvarl floorshade_tmp floorshadesaved addvarvar floorshade_tmp le_attenspeed else setvarvar floorshade_tmp floorshadesaved
				ifvarvarl ceilingshade_tmp ceilingshadesaved addvarvar ceilingshade_tmp le_attenspeed else setvarvar ceilingshade_tmp ceilingshadesaved
				setvar COUNTER 0
			}
		} else
		{
			ifvarvarl floorshade_tmp floorshadesaved addvarvar floorshade_tmp le_attenspeed else setvarvar floorshade_tmp floorshadesaved
			ifvarvarl ceilingshade_tmp ceilingshadesaved addvarvar ceilingshade_tmp le_attenspeed else setvarvar ceilingshade_tmp ceilingshadesaved
		}
	} else
	{
		setvarvar floorshade_tmp floorshadesaved
		setvarvar ceilingshade_tmp ceilingshadesaved
	}
}
ends

state func_light_walls_off
//ifvare le_lightoff 0
{
	ifvarn tr_bits 4 ifvarn tr_bits 3 { 
			ifvarg le_count 0
			{
				addvar wallshade_count 1
				ifvarvare wallshade_count le_count 
				{ 
					getsector[sectnum1].wallnum wallnum
					getsector[sectnum1].wallptr wallptr
					setvar tmp 0
					ifvarl wallnum 1 break
					whilevarvarn tmp wallnum
					{ 
						ifvarg le_attenspeed 0
						{
							setvarvar wallshade_tmp WALL_SHADETMP[wallptr]
							ifvarvarl wallshade_tmp WALL_SHADE[wallptr] addvarvar wallshade_tmp le_attenspeed else setvarvar wallshade_tmp WALL_SHADE[wallptr]
							setarray WALL_SHADETMP[wallptr] wallshade_tmp
							
							ifvarand wall[wallptr].cstat 2 {
								getwall[wallptr].nextwall se_walltmp
								ifvarn se_walltmp -1 ifvarn wall[se_walltmp].hitag 1 
									setwall[se_walltmp].shade wallshade_tmp 
							}
							ifvarn wall[wallptr].hitag 1 
								setwall[wallptr].shade wallshade_tmp
						} else	{ 
							ifvarand wall[wallptr].cstat 2 {
								getwall[wallptr].nextwall se_walltmp
								ifvarn se_walltmp -1 ifvarn wall[se_walltmp].hitag 1 
									setwall[se_walltmp].shade WALL_SHADE[se_walltmp]  
							}

							ifvarn wall[wallptr].hitag 1 
								setwall[wallptr].shade WALL_SHADE[wallptr] 
						}
						ifvarvare wallshade_tmp WALL_SHADE[wallptr] setvar le_lightoff 1
						ifvarg le_wallpal 0 setwall[wallptr].pal WALL_PAL[wallptr]
						
						addvar wallptr 1
						addvar tmp 1
							
					}
					setvar wallshade_count 0
				}
			} else
			{
				getsector[sectnum1].wallnum wallnum
				getsector[sectnum1].wallptr wallptr
				setvar tmp 0
				ifvarl wallnum 1 break
				whilevarvarn tmp wallnum
				{ 
					ifvarg le_attenspeed 0
					{
						setvarvar wallshade_tmp WALL_SHADETMP[wallptr]
						ifvarvarl wallshade_tmp WALL_SHADE[wallptr] addvarvar wallshade_tmp le_attenspeed else setvarvar wallshade_tmp WALL_SHADE[wallptr]
						setarray WALL_SHADETMP[wallptr] wallshade_tmp
						ifvarand wall[wallptr].cstat 2 {
								getwall[wallptr].nextwall se_walltmp
								ifvarn se_walltmp -1 ifvarn wall[se_walltmp].hitag 1 
									setwall[se_walltmp].shade WALL_SHADE[se_walltmp] //wallshade_tmp
						}

						ifvarn wall[wallptr].hitag 1
							setwall[wallptr].shade wallshade_tmp
					} else { 
						ifvarand wall[wallptr].cstat 2 {
								getwall[wallptr].nextwall se_walltmp
								ifvarn se_walltmp -1 ifvarn wall[se_walltmp].hitag 1 
									setwall[se_walltmp].shade WALL_SHADE[se_walltmp] 
						}
						ifvarn wall[wallptr].hitag 1 
							setwall[wallptr].shade WALL_SHADE[wallptr] 
					}
					ifvarvare wallshade_tmp WALL_SHADE[wallptr] setvar le_lightoff 1
					ifvarvare floorshade_tmp floorshadesaved { ifvarg le_wallpal 0 setwall[wallptr].pal WALL_PAL[wallptr] }
					
					ifvare tr_bits 5 
						ifvarg le_floorpal 0 setwall[wallptr].pal WALL_PAL[wallptr]
					ifvare tr_bits 6 
						ifvarg le_floorpal 0 setwall[wallptr].pal WALL_PAL[wallptr]
					ifvare tr_bits 7
						ifvarg le_floorpal 0 setwall[wallptr].pal WALL_PAL[wallptr]
					ifvare tr_bits 8
						ifvarg le_floorpal 0 setwall[wallptr].pal WALL_PAL[wallptr]
					ifvare tr_bits 9
						ifvarg le_floorpal 0 setwall[wallptr].pal WALL_PAL[wallptr]
					ifvare tr_bits 10
						ifvarg le_floorpal 0 setwall[wallptr].pal WALL_PAL[wallptr]
					ifvare tr_bits 11
						ifvarg le_floorpal 0 setwall[wallptr].pal WALL_PAL[wallptr]
					ifvare tr_bits 12
						ifvarg le_floorpal 0 setwall[wallptr].pal WALL_PAL[wallptr]
					ifvare tr_bits 13
						ifvarg le_floorpal 0 setwall[wallptr].pal WALL_PAL[wallptr]
					ifvare tr_bits 14
						ifvarg le_floorpal 0 setwall[wallptr].pal WALL_PAL[wallptr]
					
					addvar wallptr 1
					addvar tmp 1
							
				}
			}
		}
		else ifvare tr_bits 3
		{
			getsector[sectnum1].wallnum wallnum
			getsector[sectnum1].wallptr wallptr
			setvar tmp 0
			ifvarl wallnum 1 break
			whilevarvarn tmp wallnum
			{ 
				ifvarand wall[wallptr].cstat 2 {
					getwall[wallptr].nextwall se_walltmp
					ifvarn se_walltmp -1 ifvarn wall[se_walltmp].hitag 1 
						setwall[se_walltmp].shade floorshade_tmp 
				}
						
				ifvarn wall[wallptr].hitag 1 
					setwall[wallptr].shade floorshade_tmp
				ifvare le_switch 1 { ifvarg le_wallpal 0 setwall[wallptr].pal le_wallpal } else 
				ifvarvare floorshade_tmp floorshadesaved { setwall[wallptr].pal WALL_PAL[wallptr] setvar le_lightoff 1 }
				addvar wallptr 1 
				addvar tmp 1	
			}
		}
}
ends

actor LIGHTEFFECTOR	

ifvarg active 1 setvar active 0 

ifvare le_bits -1 setvar le_bits 7
ifvare active 1
{
	setvar le_lightoff 0
	ifvare tr_bits 0 state staticlight else
	ifvare tr_bits 1 state randomlight else
	ifvare tr_bits 2 state sinelight else
	ifvare tr_bits 3 state radius_wavelight else
	ifvare tr_bits 4 state candlelight else
	ifvare tr_bits 5 state squarelight else
	ifvare tr_bits 6 state sawlight else
	ifvare tr_bits 7 state rampuplight else
	ifvare tr_bits 8 state rampdownlight else
	ifvare tr_bits 9 state strobelight else
	ifvare tr_bits 10 state searchlight else
	ifvare tr_bits 11 state flic3light else
	ifvare tr_bits 12 state bloodsinelight else 
	ifvare tr_bits 13 state flic1light else 
	ifvare tr_bits 14 state flic2light 
	
	ifvarn tr_bits 2 ifvarn tr_bits 5 ifvarn tr_bits 6 ifvarn tr_bits 7 
	ifvarn tr_bits 8 ifvarn tr_bits 9 ifvarn tr_bits 10 ifvarn tr_bits 11 ifvarn tr_bits 12 ifvarn tr_bits 13 ifvarn tr_bits 14
		state func_light_to_sector	
} 
else //active = 0
{	
	ifvare tr_bits 3 state radious_wavelight_off
	ifvare tr_bits 4 state candle_light_off
	
		
	ifvarand le_bits 8 { ifvarg le_shade 0 { setvarvar floorshade_tmp le_shade } else setvarvar floorshade_tmp 4 } //two light in a sector
	else
	{
		ifvare tr_bits 1 state randomlight_off else
		ifvare tr_bits 2 state sinelight_off else
		ifvare tr_bits 5 state bsinelight_off else
		ifvare tr_bits 6 state bsinelight_off else 
		ifvare tr_bits 7 state bsinelight_off else 
		ifvare tr_bits 8 state bsinelight_off else 
		ifvare tr_bits 9 state bsinelight_off else 
		ifvare tr_bits 10 state bsinelight_off else 
		ifvare tr_bits 11 state bsinelight_off else 
		ifvare tr_bits 12 state bsinelight_off else 
		ifvare tr_bits 13 state bsinelight_off else 
		ifvare tr_bits 14 state bsinelight_off else 
			state func_light_floorandceiling_off
		
		ifvarn tr_bits 4 { 
			ifvarand le_bits 1 setsector[sectnum1].floorshade floorshade_tmp 
			ifvarand le_bits 2 setsector[sectnum1].ceilingshade ceilingshade_tmp
		}
		
		ifvarvare floorshade_tmp floorshadesaved
		{
			ifvare tr_bits 3 { 
				ifvare le_switch 0 { 
				setsector[sectnum1].ceilingpal ceilingpalsaved
				setsector[sectnum1].floorpal floorpalsaved } 
			} else {
				setsector[sectnum1].ceilingpal ceilingpalsaved
				setsector[sectnum1].floorpal floorpalsaved
			}
		}
		ifvarand le_bits 4 state func_light_walls_off
	}
}
enda
