

spritenoshade LIGHTSOURCE

gamevar numwalls 0 2
gamevar wallpoint 0 0
gamevar currwall 0 0

gamevar angvar1 0 0
gamevar angvar2 0 0
gamevar intensity 0 2
gamevar ceilintensity 0 2
gamevar flicker 0 2
// gamevar directional 0 2
gamevar initfloorshade 0 2
gamevar initceilingshade 0 2
gamevar area 0 2
gamevar ONCE 0 0
gamevar WALLTEMP 0 0
gamevar WALLX 0 0
gamevar WALLY 0 0
gamevar WALLZ1 0 0
gamevar WALLZ2 0 0
gamevar X 0 0
gamevar Y 0 0
gamevar SMALLX 0 0
gamevar BIGX 0 0
gamevar SMALLY 0 0
gamevar BIGY 0 0

gamearray wallshades 16384
gamearray wallpals 16384
define MAXLIGHTS 1024
gamearray lights MAXLIGHTS
gamearray temparray MAXLIGHTS
// gamevar taggedSC -1 1026 // array holding the IDs of hte sector controllers that have lotags
// gamevar hightaggedSC 0 0
gamevar lighting USE_FLARES 0
gamevar lightinmap 0 0 // set to 1 if sector controllers exist in the map
gamevar highlight -1 0
gamevar lightidle 0 0 // countdown timer; if it reaches zero, the lighting situation is considered stable with no need for further calculation
gamevar pintensity LIGHT_BRIGHTNESS 1
gamevar pcolor LIGHT_COLOR 1
gamevar pflicker LIGHT_FLICKER 1
gamevar flares 0 0 // number of active flares

// gamevar player_directional BEAM_WIDTH 1

// definegamefuncname 17 INCREASE_INTENSITY
// onevent EVENT_AIMUP
// setvar RETURN -1
// addvar pintensity 4
// ifvarg pintensity 2000 setvar pintensity 2000
// endevent

// definegamefuncname 18 DECREASE_INTENSITY
// onevent EVENT_AIMDOWN
// setvar RETURN -1
// subvar pintensity 4
// ifvarl pintensity 1 setvar pintensity 1
// endevent

define SECTOR_CONTROLLER 2942 //8120
eventloadactor SECTOR_CONTROLLER
getactor[THISACTOR].lotag mtype
ifvare mtype 1 { setvar lightinmap 1 setvar lighting 1 }
enda

state fill_wall_arrays
setvar temp 0
//whilevarn temp 16384
whilevarvarn temp NUMWALLS
{
	getwall[temp].shade shade
	//setactorvar[temp].wallshades shade
	setarray wallshades[temp] shade
	getwall[temp].pal tempb
	//setactorvar[temp].wallpals tempb
	setarray wallpals[temp] tempb
	addvar temp 1
	
}
ends


state FIND_CENTER_OF_SECTOR
// also calculates area of sector
setvar mx 0
setvar my 0
setvar mz 0

whilevarvarn currwall numwalls
{
	getwall[wallpoint].x WALLX
	ifvare currwall 0 { setvarvar SMALLX WALLX setvarvar BIGX WALLX }
	ifvarvarl WALLX SMALLX setvarvar SMALLX WALLX
	ifvarvarg WALLX BIGX setvarvar BIGX WALLX
	addvarvar mx WALLX
	getwall[wallpoint].y WALLY
	ifvare currwall 0 { setvarvar SMALLY WALLY setvarvar BIGY WALLY }
	ifvarvarl WALLY SMALLY setvarvar SMALLY WALLY
	ifvarvarg WALLY BIGY setvarvar BIGY WALLY
	addvarvar my WALLY
	
	addvar wallpoint 1
	addvar currwall 1
}

subvarvar BIGX SMALLX
subvarvar BIGY SMALLY
mulvarvar BIGX BIGY
setvarvar area BIGX
ifvarl area 0 mulvar area -1
ifvare numwalls 0 setvar numwalls 1 // failsafe
divvarvar mx numwalls
divvarvar my numwalls

getflorzofslope temp mx my WALLZ1 // wallz1 is the return var
setvarvar digz WALLZ1
subvar digz 8192 //12288
getceilzofslope temp mx my WALLZ2


addvarvar WALLZ1 WALLZ2
divvar WALLZ1 2
ifvarvarl WALLZ1 digz setvarvar WALLZ1 digz // to prevent SC from being too high
setvarvar mz WALLZ1  // mx, my, mz contain final coords

ends

state spawn_controller
	getsector[temp].wallptr wallpoint // get the firstwall of the sector
	ifvare wallpoint -1 break
	getsector[temp].wallnum numwalls // get the number of walls in the sector
	setvar currwall 0
	
	state FIND_CENTER_OF_SECTOR
	espawn SECTOR_CONTROLLER
	setactor[RETURN].x mx
	setactor[RETURN].y my
	setactor[RETURN].z mz
	setactorvar[RETURN].mysector temp
	setactorvar[RETURN].area area
	setactorvar[RETURN].mtype 1
	changespritesect RETURN temp

ends

// state checkdirection
// // spriteid is the lightsource
// // do NOT mess with temp or tempb in this state
// // are you (the sector controller) within the light's FOV?
// getactor[THISACTOR].x x
// getactor[THISACTOR].y y
// getactor[spriteid].x mx
// getactor[spriteid].y my
// subvarvar x mx
// subvarvar y my
// getangle angvar1 x y // angle the light would need to turn to to face the sector
// getactor[spriteid].ang angvar2
// getincangle tempc angvar1 angvar2
// ifvarl tempc 0 mulvar tempc -1
// mulvar tempc 2
// ifvarvarg tempc directional setvar directional -1
// ends



state lightsector

ifvarvare mysector WALLTEMP setvar xydist 1 else
{
	ldist xydist THISACTOR spriteid
	subvar xydist 1536
	ifvarl xydist 1 setvar xydist 1
	shiftvarr xydist 7 //8
}
ifvarl xydist 1 setvar xydist 1
addvar xydist 4
getactorvar[spriteid].intensity tempc
divvarvar tempc xydist
ifvarl tempc 1 break

getactor[spriteid].z mz
subvar mz 4096
setactor[spriteid].z mz
canseespr THISACTOR spriteid tempd

setvarvar tempb tempc
ifvare tempd 0
{
	setactor[THISACTOR].z fallflag
	canseespr THISACTOR spriteid tempe
	setactor[THISACTOR].z initz
	ifvare tempe 0 shiftvarr tempb 2
}

addvarvar ceilintensity tempb

addvar mz 4096
setactor[spriteid].z mz

ifvare tempd 0 
//ifvarg xydist 4 
shiftvarr tempc 2

ifvarvarg tempc intensity
getactor[spriteid].pal peractor7

addvarvar intensity tempc


ends

state initialize_SE

headspritesect spriteid mysector
whilevarn spriteid -1
{
	getactor[spriteid].picnum picnum
	ifvare picnum SECTOREFFECTOR
	{
		getactor[spriteid].lotag temp
		switch temp
		// case 103
		// case 104
		case 3
			setvarvar lotag temp
			ifvare target -1 setvarvar target spriteid
			getactor[spriteid].hitag hitag
			getactor[spriteid].shade initshade
			getactor[spriteid].htg_t 3 initfloorshade
			getactor[spriteid].htg_t 3 initceilingshade
			getsector[THISACTOR].wallptr wallpoint
			setvar currwall 0
			whilevarvarn currwall numwalls
			{
				setarray wallshades[wallpoint] initfloorshade
				addvar wallpoint 1
				addvar currwall 1
			}	
		break
		case 108
			setvarvar lotag temp
			ifvare target -1 setvarvar target spriteid
			getactor[spriteid].hitag hitag
			getactor[spriteid].shade initshade
			// in this case, initshade is the value it will randomly go to
		break
		case 8
		case 9
		case 12
			ifvare target -1 setvarvar target spriteid else
			ifvare target2 -1 setvarvar target2 spriteid else
		break
		case 107 // replacement for cycler
			getactor[spriteid].shade initcstat // this is the peak shade value of the cycler
			setvarvar lotag temp
			getactor[spriteid].yvel hitag
			setvar command 1
		break
		endswitch
	}
		
		
	nextspritesect spriteid spriteid
}

ends

state check_my_lightswitches
ifvare lotag 107 break
setvar command 0

getactor[target].htg_t 0 temp
ifvarn temp 0 
{ 
	getactor[target].shade initshade
	getactor[target].pal pal
	setvar command 1 
}

ifvare command 0 ifvarn target2 -1
{
	getactor[target2].htg_t 0 temp
	ifvarn temp 0 
	{ 
		getactor[target].shade initshade 
		getactor[target].pal pal
		setvar command 1
	}
}
ends

useractor notenemy SECTOR_CONTROLLER 0

ifvarn mtype 1 break
sleeptime -1

ifmove 0 ifvare monstatus 0
{
	setvar NOCODE 1
	setvar monstatus 1
	setvar command 0
	cstat 32768
	//cstat 0
	sizeat 20 20
	ifvare mysector -1 getactor[THISACTOR].sectnum mysector
	getsector[THISACTOR].wallnum numwalls
	getsector[mysector].floorshade initfloorshade
	getsector[mysector].ceilingshade initceilingshade
	getsector[mysector].floorpal peractor4
	getsector[mysector].ceilingpal peractor5
	getactor[THISACTOR].x initx
	getactor[THISACTOR].y inity
	getactor[THISACTOR].z initz // these must not be overwritten!!
	getceilzofslope mysector initx inity fallflag // fallflag is the saved ceiling z
	addvar fallflag 1024
	state initialize_SE
	randvar countvar LIGHT_LAG
	move INIT
}

ifvare lotag 3
{
	getactor[target].htg_t 0 temp
	ifvarn temp 0
	{
		setactor[target].htg_t 0 0
		setactor[target].lotag 104
		setvar lotag 104
	}
} else
ifvare lotag 108
{
	ifvare activator 1 // go back to normal shades
	{
		setvar activator 0
		setvarvar temp initfloorshade
		subvarvar temp intensity
		setsector[mysector].floorshade temp
		setvarvar temp initceilingshade
		subvarvar temp intensity
		setsector[mysector].ceilingshade temp
		getsector[THISACTOR].wallptr wallpoint
		setvar currwall 0
		whilevarvarn currwall numwalls
		{
			setvarvar temp wallshades[wallpoint]
			subvarvar temp intensity
			setwall[wallpoint].shade temp
			addvar wallpoint 1
			addvar currwall 1
		}	
	} else
	{
 		setvarvar tempb randomseed // saves randomseed
 		setvarvar randomseed hitag
 		addvarvar randomseed timer
 		mulvar randomseed 32902843
 		xorvar randomseed 0xAAAA

		ifrnd 16
		{
			setvar activator 1 // random lights flash
			getsector[mysector].floorshade temp
			ifvarvarg temp initshade setsector[mysector].floorshade initshade
			getsector[mysector].ceilingshade temp
			ifvarvarg temp initshade setsector[mysector].ceilingshade initshade
			getsector[THISACTOR].wallptr wallpoint
			setvar currwall 0
			whilevarvarn currwall numwalls
			{
				getwall[wallpoint].shade temp
				getwall[wallpoint].hitag tempc
				ifvare tempc 0
				ifvarvarg temp initshade setwall[wallpoint].shade initshade
				addvar wallpoint 1
				addvar currwall 1
			}
		}
		setvarvar randomseed tempb
		ifrnd 16 nullop
	}
} else
ifvare lotag 107
{
	// initshade is the peak shade
	// hitag is the length of the cycle
	// initfloorshade is the darkest shade
	// shade at a time = peak deviation from mid-shade value * sin ( (frequency * time) + offset value )
	setvarvar temp initfloorshade
	setvarvar tempb initcstat // these are the high and low values
	setvarvar tempc temp
	subvarvar tempc tempb // total difference in shade values
	divvar tempc 2 // largest deviation from mid-shade value
	setvarvar tempd timer
	divvar tempd 2
	mulvarvar tempd hitag // frequency * time
	sin initshade tempd
	mulvarvar initshade tempc
	shiftvarr initshade 14
	addvarvar tempb tempc
	addvarvar initshade tempb
	
	getsector[mysector].floorshade temp
	ifvarvarg temp initshade setsector[mysector].floorshade initshade else
	ifvarvarl temp initshade // floor is not dark enough
	{
		setvarvar tempb initshade
		subvarvar tempb temp // tempb is how many shades darker it should be
		subvarvar tempb intensity // intensity subtracts from the amount to make it darker
		addvarvar temp tempb
		setsector[mysector].floorshade temp
	}
		
	getsector[mysector].ceilingshade temp
	ifvarvarg temp initshade setsector[mysector].ceilingshade initshade else
	ifvarvarl temp initshade
	{
		setvarvar tempb initshade
		subvarvar tempb temp // tempb is how many shades darker it should be
		subvarvar tempb intensity // intensity subtracts from the amount to make it darker
		addvarvar temp tempb
		setsector[mysector].ceilingshade temp
	}
	
	getsector[THISACTOR].wallptr wallpoint
	// getsector[THISACTOR].wallnum numwalls
	setvar currwall 0
	
	whilevarvarn currwall numwalls
	{
		getwall[wallpoint].shade temp
		ifvarvarg temp initshade setwall[wallpoint].shade initshade else
		ifvarvarl temp initshade
		{
			setvarvar tempb initshade
			subvarvar tempb temp // tempb is how many shades darker it should be
			subvarvar tempb intensity // intensity subtracts from the amount to make it darker
			addvarvar temp tempb
			setwall[wallpoint].shade temp
		}
		addvar wallpoint 1
		addvar currwall 1
	}
	
}

getplayer[THISACTOR].cursectnum temp
ifvarvare temp mysector 
{
	ifhitspace
	{
		addlogvar initfloorshade
		getsector[mysector].floorshade temp
		addlogvar temp
	}
}
 else
{
	ifpdistg 32768 break
	ifvare lightidle 0 break
}

ifvarg countvar LIGHT_LAG
{
	setvar countvar 0
	ifvarg highlight -1
	{
		setvar intensity 0
		setvar ceilintensity 0
		
		setvar SMALLX 0
		setvarvar BIGX highlight
		addvar BIGX 1
		whilevarvarn SMALLX BIGX
		{
			setvarvar spriteid lights[SMALLX]
			addvar SMALLX 1
			ifvarn spriteid -1 // a light source found
			{
				getactor[spriteid].sectnum WALLTEMP
				state lightsector
			}
			
	// 		ifvarn spriteid -1 // a light source found
	// 		{
	// 			getactor[spriteid].sectnum tempc
	// 			ifvarvare mysector tempc state lightsector
	// 			else
	// 			{
	// 				getactorvar[spriteid].directional directional
	// 				ifvare directional 0 state lightsector // omnidirectional light
	// 				else 
	// 				{
	// 					state checkdirection
	// 					ifvarn directional -1 state lightsector
	// 				}
	// 			}
	// 			
	// 		}
		}
		
		ifvarg ceilintensity 0
		{
			setvar peractor6 LIGHT_LAG
			addvar peractor6 2
			ifoutside nullop else
			{
				ifvare command 0
				setvarvar temp initceilingshade
				else setvarvar temp initshade
				
				
				subvarvar temp ceilintensity // intensity
				ifvarl temp -125 setvar temp -125
				setsector[mysector].ceilingshade temp
				
				ifvarn peractor7 0 setsector[mysector].ceilingpal peractor7 else
				ifvare command 0
				setsector[mysector].ceilingpal peractor5 else
				setsector[mysector].ceilingpal pal
			}
		} else setvar peractor6 1
		ifvarg intensity 0
		{
			ifvarn target -1 state check_my_lightswitches	
			ifvare command 0
			setvarvar temp initfloorshade
			else setvarvar temp initshade
			
			subvarvar temp intensity
			ifvarl temp -125 setvar temp -125
			setsector[mysector].floorshade temp
			
			ifvarn peractor7 0 setsector[mysector].floorpal peractor7 else
			ifvare command 0
			setsector[mysector].floorpal peractor4 else
			setsector[mysector].floorpal pal
			getsector[mysector].wallptr wallpoint
			setvar currwall 0
			whilevarvarn currwall numwalls
			{
				ifvare command 0
				setvarvar temp wallshades[wallpoint]
				else setvarvar temp initshade
				
				subvarvar temp intensity
				ifvarl temp -125 setvar temp -125
				setwall[wallpoint].shade temp
				ifvarn peractor7 0 setwall[wallpoint].pal peractor7
				else
				{
					ifvare command 0
					{
						setvarvar temp wallpals[wallpoint]
						setwall[wallpoint].pal temp
					}
					else setwall[wallpoint].pal pal
				}
				addvar wallpoint 1
				addvar currwall 1
			}
			
		} else setvar peractor6 1
	}
}

ifvarg peractor6 0
{
	subvar peractor6 1
	ifvare peractor6 0
	{
		ifvarn target -1 state check_my_lightswitches
		setvar peractor7 0
		ifvare highlight -1
		{
			setvar intensity 0
			setvar ceilintensity 0
		}
		// restore sector normal floor and ceiling shades
		ifvare ceilintensity 0
		{
			ifvare command 0
			{
				setsector[THISACTOR].ceilingshade initceilingshade
				setsector[mysector].ceilingpal peractor5
			} else
			{
				setsector[THISACTOR].ceilingshade initshade
				setsector[mysector].ceilingpal pal
			}
		}
		
		// next restore normal wall shades
		
		ifvare intensity 0
		{
			getsector[THISACTOR].wallptr wallpoint
			setvar currwall 0
			whilevarvarn currwall numwalls
			{
				ifvare command 0
				setvarvar temp wallshades[wallpoint]
				else setvarvar temp initshade
				setwall[wallpoint].shade temp
				ifvare command 0
				{
					setvarvar temp wallpals[wallpoint]
					setwall[wallpoint].pal temp
				}
				else setwall[wallpoint].pal pal
				addvar wallpoint 1
				addvar currwall 1
			}
			ifvare command 0
			{
				setsector[THISACTOR].floorshade initfloorshade
				setsector[mysector].floorpal peractor4
				
			} else
			{
				setsector[THISACTOR].floorshade initshade
				setsector[mysector].floorpal pal
			}
		}
	}
}
addvar countvar 1
enda

state fill_light_array

// this state is for filling the light array with lights that are present at map load time

setvar spriteid 0
whilevarn spriteid 16384
{
	getactor[spriteid].statnum tempb
	getactorvar[spriteid].intensity tempc
	ifvarn tempb 1024
	ifvarg tempc 0
	{
		setvar lighting YES
		setvar tempd 0
		whilevarn tempd MAXLIGHTS
		{
			setvarvar temp lights[tempd]
			ifvare temp -1
			{
				setarray lights[tempd] spriteid
				setactorvar[spriteid].position tempd // permanently saves position in array for deletion later
				ifvarvarg tempd highlight setvarvar highlight tempd
				setvar tempd 1023
			}
			addvar tempd 1
		}
	}
	addvar spriteid 1
}

ends

state enter_light_array
setvar lightidle 40
setvar temp 0
//ifvare highlight -1 setvar highlight 0
whilevarn temp 1024
{
	setvarvar tempb lights[temp]
	ifvare tempb -1
	{
		setarray lights[temp] THISACTOR
		setvarvar position temp // permanently saves position in array for deletion later
		addvar highlight 1
		setvar temp 1023
	}
	addvar temp 1
}
ends

state exitlightarray

setvar lightidle 40

setvar position 0
setvar temp 0
setarray temparray[temp] -1
whilevarn position MAXLIGHTS
{
	setvarvar spriteid lights[position]
	ifvarvare spriteid THISACTOR setvar spriteid -1
	ifvarn spriteid -1
	{
		setarray temparray[temp] spriteid
		addvar temp 1
	}
	addvar position 1
}
setvarvar highlight temp
subvar highlight 1
whilevarn temp MAXLIGHTS
{
	setarray temparray[temp] -1
	addvar temp 1
}
setvar temp 0
whilevarn temp MAXLIGHTS
{
	setvarvar spriteid temparray[temp]
	setarray lights[temp] spriteid
	addvar temp 1
}

setvar intensity 0

ends

state clear_light_array
setvar temp 0
whilevarn temp MAXLIGHTS
{
	setarray lights[temp] -1
	addvar temp 1
}
setvar highlight -1
ends

state resetwallshades
setvar temp 0
whilevarvarn temp NUMWALLS
{
	setvarvar tempb wallshades[temp]
	setwall[temp].shade tempb
	addvar temp 1
}
ends

define LIGHTGLOW 8045

state lightglowcode

ifmove 0
{
	move INIT
	spriteflags 4
	setactor[THISACTOR].shade -127
	cstat 130
	ifvarn hitag 0 setvarvar initx hitag else
	getactor[THISACTOR].cstat peractor1
	sizeat 40 40
}

ifvarvare holding myspawner
	cstator 512
else cstat 130

getactorvar[myspawner].intensity intensity
ifvarg intensity 255 setvar intensity 255
shiftvarr intensity 1
setactor[THISACTOR].xrepeat intensity
setactor[THISACTOR].yrepeat intensity

getactor[myspawner].x x
getactor[myspawner].y y
getactor[myspawner].z z
updatesectorz x y z mysector
changespritesect THISACTOR mysector
setactor[THISACTOR].x x
setactor[THISACTOR].y y
setactor[THISACTOR].z z
setvarvar temp lights[position]
ifvare temp -1 killit
ifvare monstatus 2 killit
ends

useractor notenemy LIGHTGLOW 0
state lightglowcode
enda

move FULLBRIGHT
action LIGHTSOURCEFRAME1 -3237 1 1 1
useractor notenemy LIGHTSOURCE 0 // LIGHTSOURCEFRAME1

// needs pickup code
state pickupcode

ifvarvare holding THISACTOR
{
	setvar lightidle 40
	cstat 256
	// setvarvar directional player_directional
	getplayer[THISACTOR].ang angvar
	setactor[THISACTOR].ang angvar
}
else
{
	fall
	cstat 257
}

ifmove 0
{
	move INIT
	sizeat 32 32
	setvarvar flicker pflicker
	setactor[THISACTOR].pal pcolor
	ifspawnedby LIGHTSOURCEPROJ 
	{ 
		ifvarvarl intensity pintensity setvar countvar 1000
		move FULLBRIGHT
	} else
	ifvarn flicker 0
	{
		setvarvar intensity pintensity
		move FULLBRIGHT
	}
	ifvare peractor7 0
	setvarvar peractor7 intensity // peractor7 stores the original intensity
	state enter_light_array
	espawn LIGHTGLOW 
	setactor[RETURN].pal pcolor
	setactorvar[RETURN].myspawner THISACTOR
	setvarvar myspawner RETURN
	setactorvar[RETURN].position position
}

ifmove INIT
{
	ifvarl lightidle 10 addvar lightidle 2
	ifvarvarl intensity pintensity addvar intensity 2
	else
	move FULLBRIGHT
}

ifvarg flicker 0
{
	setvar lightidle 40
	setvarvar temp peractor7
	mulvarvar temp flicker
	divvar temp 100 // temp is now the random component of the intensity
	randvarvar intensity temp
	// now add the base component
	setvarvar tempb peractor7
	subvarvar tempb temp // base is what is left after subtracting max flickering amount
	addvarvar intensity tempb
}


ifhitweapon
{
	setvar lightidle 40
	ifvarvare holding THISACTOR setvar holding -1
	lotsofglass 10
	sound GLASS_BREAKING
	setvar intensity 2
	// state exitlightarray
	setactorvar[myspawner].monstatus 2
	setvar mtype 0
	whilevarn mtype 32
	{
		addvar mtype 1
		randvar angvar 2047
		setactor[THISACTOR].ang angvar
		randvar zdist 3072
		ifrnd 128 mulvar zdist -1
		zshoot zdist SPARKPROJ
	}
	killit
}

addvar countvar 1
ifvarg countvar FLARE_FADETIME
{
	ifvarg intensity 4 
	{ 
		ifrnd 16
		{
			ifvarg flicker 0 subvar peractor7 1 else
			subvar intensity 1 ifvare lightidle 0 addvar lightidle 8 
		} 	
	}
	
	ifvarg countvar FLARE_LIFETIME
	{
		ifvarvare holding THISACTOR setvar holding -1
		setvar intensity 2
		setactorvar[myspawner].monstatus 2
		killit
	}
}

enda