state checkhitscan
	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
	hitscan px py pz psector mycos mysin phoriz HITSECTOR HITWALL HITSPRITE HITX HITY HITZ 1
ends

state KEYSWITCH
ifpdistl 1280
ifp palive
ifp pfacing
ifcansee
ifcount 4
{
	ifhitspace
	{
		ifvare peractor 0
		{
			ifvarvarn VAR_SKULLKEY 1
			ifvarvare HITAGSAVED 1
			{
				quote 137
				soundonce 473
				setvar peractor 1
			}
			ifvarvarn VAR_EYEKEY 1
			ifvarvare HITAGSAVED 2
			{
				quote 137
				soundonce 473
				setvar peractor 1
			}
			ifvarvarn VAR_FIREKEY 1
			ifvarvare HITAGSAVED 3
			{
				quote 137
				soundonce 473
				setvar peractor 1
			}
			ifvarvarn VAR_DAGGERKEY 1
			ifvarvare HITAGSAVED 4
			{
				quote 137
				soundonce 473
				setvar peractor 1
			}
			ifvarvarn VAR_SPIDERKEY 1
			ifvarvare HITAGSAVED 5
			{
				quote 137
				soundonce 473
				action DELAY2
			}
			ifvarvarn VAR_MOONKEY 1
			ifvarvare HITAGSAVED 6
			{
				quote 137
				soundonce 473
				setvar peractor 1
			}
		}
		ifvare peractor 0
		{
			sound QLATCH2
			ifaction 0 { action ONE } else ifaction ONE { action 0 }
			operateactivators LOTAGSAVED THISACTOR
			operaterespawns LOTAGSAVED
			operatemasterswitches LOTAGSAVED
			setvar peractor 1
		}
	}
	else
	ifvare peractor 1
	setvar peractor 0
    resetcount
}
stopsound DUKE_SEARCH
stopsound DUKE_SEARCH2
ends



state spawnlight
	setactorvar[RETURN].fire_id THISACTOR
	setactorvar[RETURN].movelightse 1
	setactor[RETURN].lotag 49 
	setactor[RETURN].hitag light_radius 
	setactor[RETURN].shade 0
	setactor[RETURN].xvel red 
	setactor[RETURN].yvel green
	setactor[RETURN].zvel blue
	setactor[RETURN].picnum SECTOREFFECTOR
	changespritestat RETURN 3
ends



state teslasparkstate

	ezshootvar zdist TESLASPARK
	getactor[RETURN].ang ang
	randvar tmp 2048
	ifrnd 128 mulvar tmp -1
	addvarvar ang tmp
	setactor[RETURN].ang ang
	getactor[RETURN].x x
	getactor[RETURN].y y
	getactor[RETURN].z z
	randvar tmp 128
	randvar tmp2 128
	randvar tmp3 4092
	ifrnd 128 mulvar tmp -1
	ifrnd 128 mulvar tmp2 -1
	addvarvar x tmp
	addvarvar y tmp2
	addvarvar z tmp3
	setactor[RETURN].x x
	setactor[RETURN].y y
	setactor[RETURN].z z
	ifrnd 128 setprojectile[TESLASPARK].drop -256 else setprojectile[TESLASPARK].drop 32
	
ends

state altteslasparkstate

	randvar zdist 256
	ifrnd 128 mulvar zdist -1
	subvar zdist 256
	ezshootvar zdist TESLASPARK
	getactor[RETURN].ang ang
	randvar tmp 2048
	ifrnd 128 mulvar tmp -1
	addvarvar ang tmp
	setactor[RETURN].ang ang
	getactor[RETURN].x x
	getactor[RETURN].y y
	getactor[RETURN].z z
	randvar tmp 128
	randvar tmp2 128
	randvar tmp3 4092
	ifrnd 128 mulvar tmp -1
	ifrnd 128 mulvar tmp2 -1
	addvarvar x tmp
	addvarvar y tmp2
	addvarvar z tmp3
	setactor[RETURN].x x
	setactor[RETURN].y y
	setactor[RETURN].z z
	setprojectile[TESLASPARK].drop -64
	setprojectile[TESLASPARK].vel 192
	
ends

state sparkstate

	randvar zdist 2048
	ifrnd 128 mulvar zdist -1
	subvar zdist 1024
	ezshootvar zdist NEWSCRAP1
	getactor[RETURN].ang ang
	randvar tmp 2048
	ifrnd 128 mulvar tmp -1
	addvarvar ang tmp
	setactor[RETURN].ang ang
	
	getactor[RETURN].x x
	getactor[RETURN].y y
	getactor[RETURN].z z
	randvar tmp 128
	randvar tmp2 128
	randvar tmp3 4092
	ifrnd 128 mulvar tmp -1
	ifrnd 128 mulvar tmp2 -1
	addvarvar x tmp
	addvarvar y tmp2
	addvarvar z tmp3
	setactor[RETURN].x x
	setactor[RETURN].y y
	setactor[RETURN].z z
	
ends

state random_param

	getprojectile[JIBS7].vel tmp
	randvar tmp2 32
	addvarvar tmp tmp2
	setprojectile[JIBS7].vel tmp
	getprojectile[JIBS8].vel tmp
	randvar tmp2 32
	addvarvar tmp tmp2
	setprojectile[JIBS8].vel tmp
	getprojectile[JIBS9].vel tmp
	randvar tmp2 32
	addvarvar tmp tmp2
	setprojectile[JIBS9].vel tmp
	getprojectile[JIBS10].vel tmp
	randvar tmp2 32
	addvarvar tmp tmp2
	setprojectile[JIBS10].vel tmp
	getprojectile[JIBS11].vel tmp
	randvar tmp2 32
	addvarvar tmp tmp2
	setprojectile[JIBS11].vel tmp
	getprojectile[JIBS12].vel tmp
	randvar tmp2 32
	addvarvar tmp tmp2
	setprojectile[JIBS12].vel tmp
	getactor[RETURN].ang ang
	randvar tmp 2048
	ifrnd 128 mulvar tmp -1
	addvarvar ang tmp
	setactor[RETURN].ang ang
	
	getactor[THISACTOR].x x
	getactor[THISACTOR].y y
	getactor[THISACTOR].xrepeat xrepeat
	getactor[THISACTOR].yrepeat yrepeat
	setvarvar x2 x
	mulvar xrepeat 2
	addvarvar x2 xrepeat
	randvar ang 2048
	ifrnd 128 mulvar ang -1
	rotatepoint x y x2 y ang x3 y3
	setactor[RETURN].x x3
	setactor[RETURN].y y3
	getactor[RETURN].z z
	randvar tmp3 4092
	addvarvar z tmp3
	setactor[RETURN].z z
	
ends

state bloodspraystate

	randvar zdist 4096
	ifrnd 128 mulvar zdist -1
	subvar zdist 4096
	ezshootvar zdist BLOODSPRAY
	getactor[RETURN].ang ang
	randvar tmp 2048
	ifrnd 128 mulvar tmp -1
	addvarvar ang tmp
	setactor[RETURN].ang ang
	
	getactor[THISACTOR].x x
	getactor[THISACTOR].y y
	getactor[THISACTOR].xrepeat xrepeat
	getactor[THISACTOR].yrepeat yrepeat
	setvarvar x2 x
	mulvar xrepeat 2
	addvarvar x2 xrepeat
	randvar ang 2048
	ifrnd 128 mulvar ang -1
	rotatepoint x y x2 y ang x3 y3
	setactor[RETURN].x x3
	setactor[RETURN].y y3
	getactor[RETURN].z z
	randvar tmp3 4092
	addvarvar z tmp3
	setactor[RETURN].z z
	
ends

state jibs_state

	randvar zdist 2048
	ifrnd 128 mulvar zdist -1
	subvar zdist 1024
	ezshootvar zdist JIBS7
	state random_param
	randvar zdist 2048
	ifrnd 128 mulvar zdist -1
	subvar zdist 1024
	ezshootvar zdist JIBS8
	state random_param
	randvar zdist 2048
	ifrnd 128 mulvar zdist -1
	subvar zdist 1024
	ezshootvar zdist JIBS9
	state random_param
	randvar zdist 2048
	ifrnd 128 mulvar zdist -1
	subvar zdist 1024
	ezshootvar zdist JIBS9
	state random_param
	randvar zdist 2048
	ifrnd 128 mulvar zdist -1
	subvar zdist 1024
	ezshootvar zdist JIBS9
	state random_param
	randvar zdist 2048
	ifrnd 128 mulvar zdist -1
	subvar zdist 1024
	ezshootvar zdist JIBS9
	state random_param
	randvar zdist 2048
	ifrnd 128 mulvar zdist -1
	subvar zdist 1024
	ezshootvar zdist JIBS9
	state random_param
	randvar zdist 2048
	ifrnd 128 mulvar zdist -1
	subvar zdist 1024
	ezshootvar zdist JIBS9
	state random_param
	randvar zdist 2048
	ifrnd 128 mulvar zdist -1
	subvar zdist 1024
	ezshootvar zdist JIBS10
	state random_param
	randvar zdist 2048
	ifrnd 128 mulvar zdist -1
	subvar zdist 1024
	ezshootvar zdist JIBS10
	state random_param
	randvar zdist 2048
	ifrnd 128 mulvar zdist -1
	subvar zdist 1024
	ezshootvar zdist JIBS11
	state random_param
	randvar zdist 2048
	ifrnd 128 mulvar zdist -1
	subvar zdist 1024
	ezshootvar zdist JIBS11
	state random_param
	randvar zdist 2048
	ifrnd 128 mulvar zdist -1
	subvar zdist 1024
	ezshootvar zdist JIBS12
	state random_param
	ifrnd 6
	{
	randvar zdist 2048
	ifrnd 128 mulvar zdist -1
	subvar zdist 1024
	ezshootvar zdist JIBS9
	state random_param
	randvar zdist 2048
	ifrnd 128 mulvar zdist -1
	subvar zdist 1024
	ezshootvar zdist JIBS9
	state random_param
    spawn BLOODPOOL2
	}
	ifrnd 64 { sound FLSHDOR1 }
	ifrnd 64 { sound FLSHDOR2 }
	ifrnd 127 { sound FLSHSTP1 sound FLSHSTP2 }
	state bloodspraystate
	state bloodspraystate

ends

state bloodsprayshit
	state bloodspraystate
	state bloodspraystate
	state bloodspraystate
ends

state randomleafstate

	randvar zdist 1024
	ifrnd 128 mulvar zdist -1
	subvar zdist 4096
	ifrnd 128 ezshootvar zdist GREENLEAF else ezshootvar zdist YELLOWLEAF
	state random_param

ends

state leafs_state
	state randomleafstate
	state randomleafstate
	state randomleafstate
	state randomleafstate
	state randomleafstate
	state randomleafstate
	state randomleafstate
ends

state newfirestate
ifinwater { setactorvar[THISACTOR].burningguyactive 0 setactorvar[THISACTOR].flamedamage 0 }
else
{
	eshoot NEWFIRE
	getactor[THISACTOR].x x
	getactor[THISACTOR].y y
	getactor[THISACTOR].xrepeat xrepeat
	getactor[THISACTOR].yrepeat yrepeat
	setvarvar x2 x
	mulvar xrepeat 2
	addvarvar x2 xrepeat
	randvar ang 2048
	ifrnd 128 mulvar ang -1
	rotatepoint x y x2 y ang x3 y3
	setactor[RETURN].x x3
	setactor[RETURN].y y3
	
	getactor[RETURN].z z
	divvar yrepeat 2
	mulvar yrepeat 256
	addvarvar z yrepeat
	randvar tmp 2048
	ifrnd 128 mulvar tmp -1
	addvarvar z tmp
	setactor[RETURN].z z

	getactor[THISACTOR].extra extra
	ifvarg extra 0
	{
		getactorvar[THISACTOR].fireshield FIRESHIELD_AMOUNT
		ifvare FIRESHIELD_AMOUNT 0 { subvar extra 1 } else
		{ ifcount 2 { subvar extra 1 subvar FIRESHIELD_AMOUNT 1 setactorvar[THISACTOR].fireshield FIRESHIELD_AMOUNT resetcount } }
		ifvarl extra 1 { setvar extra 1 setactorvar[THISACTOR].burningguyactive 1 setactorvar[THISACTOR].flamedamage 0 }
		setactor[THISACTOR].extra extra
	}
}
ends



state rotatestate
getactor[THISACTOR].ang rotate
addvar rotate 32
setactor[THISACTOR].ang rotate
ends

state armorline
  mulvar num 19
  divvar num 200
  setvar tmp 19
  subvarvar tmp num
  whilevarn tmp 0 { subvar tmp 1 rotatesprite x y 65536 0 9701 0 0 26 0 0 xdim ydim addvar x -1 }
ends

state drawinvnum
   setvarvar tmp num
   modvar tmp 10
   addvar tmp 7370
   rotatesprite x y 65536 0  tmp 0 col 26 0 0 xdim ydim
   addvar x -5
    ifvarg num 9
   {
      setvarvar tmp num
      divvar tmp 10
      modvar tmp 10
      addvar tmp 7370
      rotatesprite x y 65536 0  tmp 0 col 26 0 0 xdim ydim
      addvar x -5
    }
    ifvarg num 99
    {
      setvarvar tmp num
      divvar tmp 100
      addvar tmp 7370
      rotatesprite x y 65536 0  tmp 0 col 26 0 0 xdim ydim
      
    }
ends

state drawweapnum
   setvarvar tmp num
   modvar tmp 10
   addvar tmp 7350
   rotatesprite x y 65536 0  tmp numshade col 26 0 0 xdim ydim
   addvar x -4
    ifvarg num 9
    {
      setvarvar tmp num
      divvar tmp 10
      modvar tmp 10
      addvar tmp 7350
      rotatesprite x y 65536 0  tmp numshade col 26 0 0 xdim ydim
      addvar x -4
    }
    ifvarg num 99
    {
      setvarvar tmp num
      divvar tmp 100
      addvar tmp 7350
      rotatesprite x y 65536 0  tmp numshade col 26 0 0 xdim ydim
      
    }
setvar col 0
setvar numshade 0
ends

state drawarmnum
   ifvare num 0 break
   setvarvar tmp num
   modvar tmp 10
   addvar tmp 7350
   rotatesprite x y 65536 0  tmp 0 col 26 0 0 xdim ydim
   addvar x -4
    ifvarg num 9
    {
      setvarvar tmp num
      divvar tmp 10
      modvar tmp 10
      addvar tmp 7350
      rotatesprite x y 65536 0  tmp 0 col 26 0 0 xdim ydim
      addvar x -4
    }
    ifvarg num 99
    {
      setvarvar tmp num
      divvar tmp 100
      addvar tmp 7350
      rotatesprite x y 65536 0  tmp 0 col 26 0 0 xdim ydim
      
    }
ends

state drawnumR
   setvarvar tmp num
   modvar tmp 10
   addvar tmp 7310
   rotatesprite x y 65536 0  tmp 0 col 26 0 0 xdim ydim
   addvar x -9
    ifvarg num 9
    {
      setvarvar tmp num
      divvar tmp 10
      modvar tmp 10
      addvar tmp 7310
      rotatesprite x y 65536 0  tmp 0 col 26 0 0 xdim ydim
      addvar x -9
    }
    ifvarg num 99
    {
      setvarvar tmp num
      divvar tmp 100
      addvar tmp 7310
      rotatesprite x y 65536 0  tmp 0 col 26 0 0 xdim ydim
      
    }
ends

state drawnumB
   setvarvar tmp num
   modvar tmp 10
   addvar tmp 7360
   rotatesprite x y 65536 0  tmp 0 col 26 0 0 xdim ydim
   addvar x -9
    ifvarg num 9
    {
      setvarvar tmp num
      divvar tmp 10
      modvar tmp 10
      addvar tmp 7360
      rotatesprite x y 65536 0  tmp 0 col 26 0 0 xdim ydim
      addvar x -9
    }
    ifvarg num 99
    {
      setvarvar tmp num
      divvar tmp 100
      addvar tmp 7360
      rotatesprite x y 65536 0  tmp 0 col 26 0 0 xdim ydim
      
    }
ends

state drawnumW
   setvarvar tmp num
   modvar tmp 10
   addvar tmp 2930
   rotatesprite x y 65536 0  tmp 0 col 0 0 0 xdim ydim
   addvar x -9
    ifvarg num 9
    {
      setvarvar tmp num
      divvar tmp 10
      modvar tmp 10
      addvar tmp 2930
      rotatesprite x y 65536 0  tmp 0 col 0 0 0 xdim ydim
      addvar x -9
    }
    ifvarg num 99
    {
      setvarvar tmp num
      divvar tmp 100
      addvar tmp 2930
      rotatesprite x y 65536 0  tmp 0 col 0 0 0 xdim ydim
    }
ends

state checkb_armor
ifvare reflmode 0
ifvare godmode 0
ifvare DEATHMASK_ON 0
ifvarg paextra 0
ifvarg HEALTHAMOUNT 0
{
	getactorvar[PLAYERID].bodyshield BODYSHIELD_AMOUNT
	ifvarg BODYSHIELD_AMOUNT 0
	{
		divvar paextra 2
		ifvarvarg BODYSHIELD_AMOUNT paextra
		{
			subvarvar BODYSHIELD_AMOUNT paextra
			addvarvar HEALTHAMOUNT paextra
			setactor[PLAYERID].extra HEALTHAMOUNT
			setactorvar[PLAYERID].bodyshield BODYSHIELD_AMOUNT
		}
		else
		{
			subvarvar BODYSHIELD_AMOUNT paextra	
			addvarvar HEALTHAMOUNT paextra
			addvarvar HEALTHAMOUNT BODYSHIELD_AMOUNT
			setactorvar[PLAYERID].bodyshield 0
		}
	}
}
ends

state checkf_armor
ifvare reflmode 0
ifvare godmode 0
ifvare DEATHMASK_ON 0
ifvarg paextra 0
ifvarg HEALTHAMOUNT 0
{
	getactorvar[PLAYERID].fireshield FIRESHIELD_AMOUNT
	ifvarg FIRESHIELD_AMOUNT 0
	{
		divvar paextra 2
		ifvarvarg FIRESHIELD_AMOUNT paextra
		{
			 subvarvar FIRESHIELD_AMOUNT paextra
			 addvarvar HEALTHAMOUNT paextra
			 setactor[PLAYERID].extra HEALTHAMOUNT
			 setactorvar[PLAYERID].fireshield FIRESHIELD_AMOUNT
		}
		else
		{
			subvarvar FIRESHIELD_AMOUNT paextra	
			addvarvar HEALTHAMOUNT paextra
			addvarvar HEALTHAMOUNT FIRESHIELD_AMOUNT
			setactorvar[PLAYERID].fireshield 0
		}
	}
}
ends

state checks_armor
ifvare reflmode 0
ifvare godmode 0
ifvare DEATHMASK_ON 0
ifvarg paextra 0
ifvarg HEALTHAMOUNT 0
{
	getactorvar[PLAYERID].spiritshield SPIRITSHIELD_AMOUNT
	ifvarg SPIRITSHIELD_AMOUNT  0
	{
		divvar paextra 2
		ifvarvarg SPIRITSHIELD_AMOUNT paextra
		{
			 subvarvar SPIRITSHIELD_AMOUNT paextra
			 addvarvar HEALTHAMOUNT paextra
			 setactor[PLAYERID].extra HEALTHAMOUNT
			 setactorvar[PLAYERID].spiritshield SPIRITSHIELD_AMOUNT
		} 
		else
		{
			subvarvar SPIRITSHIELD_AMOUNT paextra	
			addvarvar HEALTHAMOUNT paextra
			addvarvar HEALTHAMOUNT SPIRITSHIELD_AMOUNT
			setactorvar[PLAYERID].spiritshield 0
		}
	}
}
ends

state hudcoordfix
addvarvar xweapon weapon_xoffset
subvarvar xweapon looking_angSR1
addvarvar xweapon HUDsx
addvar xweapon 30
addvarvar yweapon looking_arc
subvarvar yweapon gun_pos
addvarvar yweapon HUDsy
ifvare hudtype 0 { subvarvar yweapon 22 }
ends

state enemyeffects
ifvare VISION 1
{ setactor[THISACTOR].shade -127 }

	getactorvar[THISACTOR].flamedamage peractor
	ifvarg peractor 0 { addvar COUNTER 1 state newfirestate } 
	ifinwater { setvar COUNTER 0 }
	ifvarvare COUNTER peractor { setactorvar[THISACTOR].flamedamage 0 setvar COUNTER 0 }
	
	getactorvar[THISACTOR].voodoohitenemy tmp
	ifvare tmp 1 { setactorvar[THISACTOR].voodoohitenemy 0 addkills 1 setactor[THISACTOR].extra 0 }
	
	
	// Thanks to KLIMaka for this code
	
	getactor[THISACTOR].htextra htextra
getactor[THISACTOR].htang htang
cos x htang
sin y htang
ifvarn htextra -1 { setvarvar k_damage htextra addvar peractor2 4 } // damage				     
iffloordistl 10	// deaccelerate on ground

subvarvar k_damage peractor2

ifvarl k_damage 0 { setvar k_damage 0 setactor[THISACTOR].htextra -1 setvar peractor2 0 }     // clamping multiplier

mulvarvar x k_damage                // roll force = damage - deacceleration
mulvarvar y k_damage
shiftvarr x 10
shiftvarr y 10
setvar z 0
movesprite THISACTOR x y z CLIPMASK0 RETURN
ends

state staff_fire
ifvarn spriteid -1
{
	canseespr THISACTOR spriteid see
	getactor[spriteid].extra extra
	ifvare see 1
	ifvarg extra 0
	{
		getactor[spriteid].x mx
		getactor[spriteid].y my
		getactor[spriteid].z mz
		getactor[THISACTOR].x x
		getactor[THISACTOR].y y
		getactor[THISACTOR].z z
		subvarvar mx x
		subvarvar my y
		getangle ang mx my
		setactor[THISACTOR].ang ang
		setvarvar zdist mz
		subvarvar zdist z

		getactor[THISACTOR].lotag tmp2
		ifvarg tmp2 0
		{
			ifcount 10
			ifvare see 1
			{ 
				ifvarl zdist -8192 subvar zdist 4096
				ldist xydist THISACTOR spriteid
				ifvare xydist 0 setvar xydist 1
				mulvar zdist 160
				divvarvar zdist xydist
				subvar tmp2 1
				ifvarl tmp2 0 { setvar tmp2 0 }
				setactor[THISACTOR].lotag tmp2
				ezshootvar zdist STAFFALTPROJ 
				resetcount 
			}
		} 
		else
		{
			ifcount 3
			ifvare see 1
			{ 
				ifvarl zdist -8192 subvar zdist 4096
				ldist xydist THISACTOR spriteid
				ifvare xydist 0 setvar xydist 1
				mulvar zdist 160
				divvarvar zdist xydist
				subvar tmp2 1
				ifvarl tmp2 0 { setvar tmp2 0 }
				setactor[THISACTOR].lotag tmp2
				ezshootvar zdist STAFFALTPROJ 
				resetcount 
			}
		}
	}
}
ends

state squishcode
guts JIBS1 2 guts JIBS2 2 guts JIBS3 2 guts JIBS6 10 spawn BLOODPOOL sound SQUISHED
ends

state drippercode
fall cstat 257
ifhitweapon { lotsofglass 20 sound GLASS_BREAKING debris SCRAP5 5 killit }
ends

state bottlecode
cstat 257
ifhitweapon { lotsofglass 10 sound GLASS_BREAKING killit }
ends

state vasecode
fall cstat 257
ifhitweapon { lotsofglass 20 debris SCRAP5 5 sound GLASS_BREAKING killit }
ends

state rackcode
fall cstat 257
ifhitweapon { debris SCRAP1 20 sound GLASS_HEAVYBREAK killit }
ends

state rackcode2
cstat 273
ifhitweapon { debris SCRAP1 20 sound GLASS_HEAVYBREAK killit }
ends

