
state jib_sounds

ifvare monstatus 0 break
ifrnd SWEARFREQUENCY
{
  setplayer[THISACTOR].sound_pitch 0

  randvar temp 17
  switch temp
  case 0 globalsound JIBBED_ACTOR12 break
  case 1 globalsound JIBBED_ACTOR1 break
  case 2 globalsound JIBBED_ACTOR9 break
  case 3 globalsound JIBBED_ACTOR14 break
  case 4 globalsound SMACKED break
  case 5 globalsound JIBBED_ACTOR2 break
  case 6 globalsound MDEVSPEECH break
  case 7 globalsound JIBBED_ACTOR5 break
  case 8 globalsound JIBBED_ACTOR11 break
  case 9 globalsound JIBBED_ACTOR13 break
  case 10 globalsound JIBBED_ACTOR3 break
  case 11 globalsound JIBBED_ACTOR8 break
  case 12 globalsound JIBBED_ACTOR6 break
  case 13 globalsound JIBBED_ACTOR4 break
  case 14 globalsound JIBBED_ACTOR10 break
  case 15 globalsound JIBBED_ACTOR15 break
  case 16 globalsound JIBBED_ACTOR7 break
  case 17 globalsound YIPPEE1 break
  default break
  endswitch
}
ends

state dukepainsounds

ifphealthl YELLHURTSOUNDSTRENGTHMP
{
  ifrnd 64
    globalsound DUKE_LONGTERM_PAIN2
  else
    ifrnd 64
      globalsound DUKE_LONGTERM_PAIN3
  else
    ifrnd 64
      globalsound DUKE_LONGTERM_PAIN4
  else
    globalsound DUKE_DEAD
}
else
{
  ifrnd 64
    globalsound DUKE_LONGTERM_PAIN5
  else ifrnd 64
    globalsound DUKE_LONGTERM_PAIN6
  else ifrnd 64
    globalsound DUKE_LONGTERM_PAIN7
  else
    globalsound DUKE_LONGTERM_PAIN8
}
ends

state imonfire

	subvar burning 1
	ifvarg burning 2048 setvar burning 2048
	
	ifactor APLAYER
	{
		getplayer[THISACTOR].shield_amount temp
		ifvarg temp 0
		{
			subvar temp 1
			setplayer[THISACTOR].shield_amount temp
			subvar burning 2
			ifvarl burning 0 setvar burning 0
		}
	}
	
	ifvarg underwater 0 setvar burning 0
	ifinwater setvar burning 0
	ifonwater ifvarg burning 1 subvar burning 1
	ifvarl sprite[THISACTOR].htextra 1
	ifvarand timer 1
	{
		ifactor APLAYER ifrnd 128 state dukepainsounds
		
		setvarvar temp burning
		shiftvarr temp 7
		ifvarg temp 8 setvar temp 8
		ifactor APLAYER ifvarg temp 4 setvar temp 4
		ifvarl temp 1 setvar temp 1
		subvarvar burning temp
		ifvarl burning 0 setvar burning 0
		ifvarg temp 0
		{
			setactor[THISACTOR].htextra temp
			setactor[THISACTOR].htpicnum BURNING
			setactor[THISACTOR].htowner player[THISACTOR].i
			addvar temp 1
			ifvarvarl sprite[THISACTOR].extra temp
			{
				getactor[THISACTOR].htflags temp
				orvar temp 4
				setactor[THISACTOR].htflags temp
				getsector[THISACTOR].floorshade shade
				addvar shade 12 ifvarg shade 30 setvar shade 30
				setactor[THISACTOR].shade shade
			}
		}
		
	}

	getactor[THISACTOR].picnum tempc
	espawn STICKYFIRE
	setactorvar[RETURN].monstatus tempc
	setactorvar[RETURN].myspawner THISACTOR
	espawn STICKYFIRE
	setactorvar[RETURN].monstatus tempc
	setactorvar[RETURN].myspawner THISACTOR
	ifvarg burning 260
	{
		espawn STICKYFIRE
		setactorvar[RETURN].monstatus tempc
		setactorvar[RETURN].myspawner THISACTOR
	}
	ifvarn monstatus 0
		soundonce COOKINGDEEPFRIER
	
ends

state pigheadshoot
  randvar angvar 2048
  setactor[THISACTOR].ang angvar
  randvar zdist 4096
  mulvar zdist -1
  ezshoot zdist PIGHEAD
  setthisprojectile[RETURN].pal pal
 ends

state piguppershoot
  randvar angvar 2048
  setactor[THISACTOR].ang angvar
  randvar zdist 4096
  mulvar zdist -1
  ezshoot zdist PIGUPPER
  setthisprojectile[RETURN].pal pal
  randvar angvar 2048
  setactor[THISACTOR].ang angvar
  randvar zdist 4096
  mulvar zdist -1
  ezshoot zdist PIGUPPER2
  setthisprojectile[RETURN].pal pal
 ends

state piglowershoot
  randvar angvar 2048
  setactor[THISACTOR].ang angvar
  randvar zdist 4096
  mulvar zdist -1
  ezshoot zdist PIGLOWER
  setthisprojectile[RETURN].pal pal
  randvar angvar 2048
  setactor[THISACTOR].ang angvar
  randvar zdist 4096
  mulvar zdist -1
  ezshoot zdist PIGLOWER2
  setthisprojectile[RETURN].pal pal
 ends

state pig_body_jibs

 getactor[THISACTOR].pal pal
  
 getactor[THISACTOR].z z
 subvar z 9216
 setactor[THISACTOR].z z
 state pigheadshoot

 addvar z 2048
 setactor[THISACTOR].z z
 state piguppershoot

 addvar z 4096
 setactor[THISACTOR].z z
 state piglowershoot

ends

// state standard_jibs
//   guts JIBS2 1
//   guts JIBS3 2
//   guts JIBS4 3
//   guts JIBS5 2
//   guts JIBS6 3
//   guts JIBS1 1
//   ifrnd 12
//   {
//     //guts JIBS1 1
//     spawn BLOODPOOL
//   }         // a badly drawn spine

//   state jib_sounds
//   ifvarand morefx 4
//   {
// 	  ifactor PIGCOP state pig_body_jibs else
// 	  ifactor PIGCOPDIVE state pig_body_jibs
//   }
// ends

action ANULLACTION 0
action FEMANIMATE
action JIBSPROJFRAME1 -1 1 1 1 8
action JIBSPROJFRAME2 0 1 1 1 8
action JIBSPROJFRAME3 1 1 1 1 8
action JIBSPROJFRAME4 2 1 1 1 8

actor JIBSPROJ1 0 JIBSPROJFRAME2 enda
actor JIBSPROJ2 0 JIBSPROJFRAME2 enda
actor JIBSPROJ3 0 JIBSPROJFRAME2 enda
actor JIBSPROJ4 0 JIBSPROJFRAME2 enda
actor JIBSPROJ5 0 JIBSPROJFRAME2 enda
actor LIZMANHEADPROJ 0 JIBSPROJFRAME2 enda
actor LIZMANARMPROJ 0 JIBSPROJFRAME2 enda
actor LIZMANLEGPROJ 0 JIBSPROJFRAME2 enda
actor TROOPHEADPROJ 0 JIBSPROJFRAME2 enda
actor TROOPARMPROJ 0 JIBSPROJFRAME2 enda
actor TROOPLEGPROJ 0 JIBSPROJFRAME2 enda


// state randsquishsound

// randvar temp 3
// switch temp
// case 0 setvar tempc SQUISHED break
// case 1 setvar tempc NEWGIB1 break
// case 2 setvar tempc NEWGIB3 break
// case 3 setvar tempc NEWGIB4 break
// endswitch
// espawn SOUNDMAKER
// setactorvar[RETURN].peractor1 tempc

// ends

state aimjib

randvar xvel 64
addvar xvel 32
randvar zdist 512
addvar zdist 512
mulvar zdist -1

setvarvar tempc bleeding
ifvarl tempc 96 setvar tempc 96
ifvarg tempc 200
{
	subvar tempc 200
	shiftvarr tempc 1
	addvar tempc 200
}
ifvare weap SHOTSPARK1 shiftvarr tempc 1

randvar tempb 48
addvar tempb 16
mulvarvar tempb tempc
shiftvarr tempb 1
subvarvar zdist tempb

randvar tempb 96
addvar tempb 32
mulvarvar tempc tempb
shiftvarr tempc 6
addvarvar xvel tempc


ends

state setjib

getactor[THISACTOR].pal pal
setthisprojectile[RETURN].pal pal
setactor[RETURN].xvel xvel


randvar angvar 2047
setactor[RETURN].ang angvar
getactor[THISACTOR].xrepeat x
getactor[THISACTOR].yrepeat y
ifvare sprite[RETURN].picnum JIBSPROJ2 // eyeball
{
	shiftvarr x 1
	shiftvarr y 1
}
setthisprojectile[RETURN].xrepeat x
setthisprojectile[RETURN].yrepeat y
setactor[RETURN].xrepeat x
setactor[RETURN].yrepeat y
ifrnd 128
{
	setactor[RETURN].cstat 132
	setthisprojectile[RETURN].cstat 132
}

randvar temp 96
ifrnd 128 mulvar temp -1
setactorvar[RETURN].peractor1 temp

ifactor EGG setthisprojectile[RETURN].trail -1
ifaction FEMANIMATE setactorvar[RETURN].countvarb 12

ends

state standard_jibs

ifactor EGG spritepal 6
guts JIBS6 6


state aimjib
ezshoot zdist JIBSPROJ2
state setjib
state aimjib
ezshoot zdist JIBSPROJ2
state setjib
state aimjib
ezshoot zdist JIBSPROJ3
state setjib
state aimjib
ezshoot zdist JIBSPROJ3
state setjib
state aimjib
ezshoot zdist JIBSPROJ3
state setjib
state aimjib
ezshoot zdist JIBSPROJ3
state setjib
state aimjib
ezshoot zdist JIBSPROJ4
state setjib
state aimjib
ezshoot zdist JIBSPROJ4
state setjib
state aimjib
ezshoot zdist JIBSPROJ4
state setjib

ifactor EGG break

state aimjib
ezshoot zdist JIBSPROJ1
state setjib
state aimjib
ezshoot zdist JIBSPROJ5
state setjib
state aimjib
ezshoot zdist JIBSPROJ5
state setjib

ifspritepal 6 nullop else
spawn BLOODPOOL

state jib_sounds

ifvarn rendmode 0
ifvarand morefx 4
{
  ifactor PIGCOP state pig_body_jibs else
  ifactor PIGCOPDIVE state pig_body_jibs
}
   
   
ends

state standard_pjibs
  guts JIBS1 1
  guts JIBS3 2
  guts JIBS4 1
  guts JIBS5 1
  guts JIBS6 2
  guts DUKETORSO 1
  guts DUKELEG 2
  guts DUKEGUN 1
  ifrnd 16 money 1
ends

state random_wall_jibs
  ifrnd 96
    shoot BLOODSPLAT1
  ifrnd 96
    shoot BLOODSPLAT2
  ifrnd 96
    shoot BLOODSPLAT3
  ifrnd 96
    shoot BLOODSPLAT4
  ifrnd 96
    shoot BLOODSPLAT1
ends

state headhitstate

// Unrem the following line to invoke screen tilting during
// low player hitpoint damage.

// wackplayer

ends

state random_ooz
  ifrnd 128
    spawn OOZ2
  else
    spawn OOZ
ends

move DUKENOTMOVING
state handle_dead_dukes
  fall
  ifmove 0   // 1st time initializiation...
  {
    ifrnd 128
      cstat 4
    else
      cstat 0
    move DUKENOTMOVING
  }

  ifsquished
  {
    sound SQUISHED
    state random_ooz
    killit
  }
  else
    ifcount 1024
      ifpdistg 4096
        killit
  else
  {
    strength 0
    ifhitweapon
      ifwasweapon RADIUSEXPLOSION
    {
      state standard_jibs
      killit
    }
  }
ends

action PLYINGFRAMES 0 1 0 1 1
actor DUKELYINGDEAD 0 PLYINGFRAMES
  state handle_dead_dukes
enda

action PGROWING   0
action PSTAND     0   1   5   1  1
action PEXPLODE   106   5   1   1   10
action PEXPLODEAD   113   1   1
action PJPHOUVER  15   1   5   1
action PWALK    20   4   5   1   20 // 16
action PRUN     20   4   5   1   14 // 10
action PWALKBACK  45   4   5  -1   20 // 16
action PRUNBACK   45   4   5  -1   14 // 10
action PJUMPING   50   4   5   1   30
action PFALLING   60   1   5
action PDUCKING   86   1   5
action PCRAWLING  86   3   5   1   20
action PAKICKING  40   2   5   1   25
action PFLINTCHING  106  1   1   1   10
action PTHROWNBACK  106  5   1   1   18
action PFROZEN     20  1   5
action PLYINGDEAD   113  1   1

action PSWIMMINGGO   375   1   5   1   10
action PSWIMMING   375   4   5   1   13
action PSWIMMINGWAIT 395   1   5   1   13
action PTREDWATER  395   2   5   1   17

action PSKICKING 40 2 5 1 30

move PSTOPED
move PSHRINKING // used as a var only

state check_pstandard
  ifp pwalking
    action PWALK
  else
    ifp pkicking
      action PAKICKING
  else
    ifp pwalkingback
      action PWALKBACK
  else
    ifp prunning
      action PRUN
  else
    ifp prunningback
      action PRUNBACK
  else
    ifp pjumping
      action PJUMPING
  else
    ifp pducking
      action PDUCKING
ends

// state wallhitscan

// getactor[spriteid].picnum picnum
// ifvare picnum TRIPBOMB break

// getactor[spriteid].pal pal
// ifvarg pal 69 break
// getactorvar[spriteid].pulse pal
// ifvarn pal 0 break

// // the sprite ID of the scanning projectile has been put in spriteid before this state is called
// getactor[spriteid].x x
// getactor[spriteid].y y
// getactor[spriteid].z z
// getactor[spriteid].sectnum mysector
// ifvare mysector -1 updatesector x y mysector
// ifvarl mysector 0 break
// ifvarg mysector 4095 break
// getactor[spriteid].ang angvar
// addvar angvar 1024
// cos mycos angvar
// sin mysin angvar
// hitscan x y z mysector mycos mysin 0 hitsector hitwall hitsprite hitx hity hitz clipmask

// state pythagoras
// ifvarg xydist 1024 setvar hitwall -1
// ifvarn hitwall -1
// {
// 	setactorvar[spriteid].mywall hitwall
// 	getactor[spriteid].shade shade
// 	setactorvar[spriteid].initshade shade
// 	getwall[hitwall].shade shade
// 	setactorvar[spriteid].ammo shade
// }
// else 
// {
// 	setactorvar[spriteid].mywall 32768 // dummy value
// 	getactor[spriteid].sectnum hitsector
// 	getsector[hitsector].floorshade shade
// 	setactorvar[spriteid].ammo shade
// 	getactor[spriteid].shade shade
// 	setactorvar[spriteid].initshade shade
// } 
// 	
// ends

state checkhitscan


getplayer[THISACTOR].cursectnum temp
ifvarl temp 0 
	break // i don't know what this will fuck up but it has to be better than those log errors
ifvarg temp 4095 break

ifvarvarn temp mysector
{
	// just moved to new sector
	setvarvar prevsectstartx lastsectstartx
	setvarvar prevsectstarty lastsectstarty
	setvarvar prevsectstartz lastsectstartz
	setvarvar lastsectstartx cursectstartx
	setvarvar lastsectstarty cursectstarty
	setvarvar lastsectstartz cursectstartz
	getplayer[THISACTOR].posx cursectstartx
	getplayer[THISACTOR].posy cursectstarty
	getplayer[THISACTOR].posz cursectstartz
	addvar cursectstartz 6144
}

setvarvar mysector temp
getplayer[THISACTOR].ang angvar
getplayer[THISACTOR].horiz zdist
subvar zdist 100
mulvar zdist -2048
cos mycos angvar
sin mysin angvar

// horiz at 0 z angle is 100; pos for up, neg for down

hitscan player[THISACTOR].posx player[THISACTOR].posy player[THISACTOR].posz mysector mycos mysin zdist hitsector hitwall hitsprite hitx hity hitz clipmask

ifvare pipethrow YES
{
	ifvare player[THISACTOR].rapid_fire_hold YES // pipebomb ready to be thrown
	{
		ifvarl weapcount 192 addvar weapcount 8 // 4
		ifp pducking nullop else
		{
			setvar countvar 0
			setvar xvel  192
			addvarvar xvel weapcount
			divvar zdist 56
			getplayer[THISACTOR].posx x
			getplayer[THISACTOR].posy y
			getplayer[THISACTOR].posz z
			whilevarn countvar 130
			{
				setvarvar digx x
				addvarvar digx xvel
				subvar xvel 5
				ifvarl xvel 0 setvar xvel 0
				rotatepoint x y digx y angvar x y
				espawn LOBTRAIL
				updatesectorz x y z temp
				ifvare temp -1 setvar countvar 129 else
				changespritesect RETURN temp
				setactor[RETURN].x x
				setactor[RETURN].y y
				setactor[RETURN].z z
				setactorvar[RETURN].countvar countvar
				addvarvar z zdist
				addvar zdist 264
				addvar countvar 1
			}
		}
	}
	else ifvare KICKBACKPIC 0 setvar weapcount 0
}

ifvarn viewcam -1 ifp palive
ifvare perplayeronbike -1
{
	setvar countvar 0
	getplayer[THISACTOR].horiz zdist
	subvar zdist 100
	mulvar zdist -2048
	divvar zdist 60
	getplayer[THISACTOR].posx x
	getplayer[THISACTOR].posy y
	getplayer[THISACTOR].posz z
	setvarvar X x
	setvarvar Y y
	setvarvar digz z
	whilevarn countvar 40
	{
		setvarvar digx x
		addvar digx 256
		rotatepoint x y digx y angvar x y
		updatesectorz x y z temp
		ifvare temp -1 setvar countvar 39
		else 
		{
// 			cansee x y z temp X Y digz mysector tempb
// 			ifvare tempb 0 setvar countvar 39 else
// 			{
				espawn SCANTRAIL
				changespritesect RETURN temp
				setvarvar scansect temp
				setvarvar scanx x
				setvarvar scany y
				setvarvar scanz z
				setactor[RETURN].x x
				setactor[RETURN].y y
				setactor[RETURN].z z
//			}
		}
		
		addvarvar z zdist
		addvar countvar 1
	}
}

setvar nextsector -1
ifhitspace ifvarn hitwall -1
{
	getplayer[THISACTOR].posx xydist
	subvarvar xydist hitx
	mulvarvar xydist xydist
	getplayer[THISACTOR].posy y
	subvarvar y hity
	mulvarvar y y
	addvarvar xydist y
	sqrt xydist xydist
	ifvarl xydist 480
	getwall[hitwall].nextsector nextsector 	
}
setvarvar scannedsprite hitsprite

ends

state projectilehitscan
// the sprite ID of the scanning projectile has been put in spriteid before this state is called

getactor[spriteid].ang angvar
getactor[spriteid].zvel zvel
getactor[spriteid].xvel xvel
shiftvarl zvel 14
ifvare xvel 0 setvar xvel 1
divvarvar zvel xvel
cos mycos angvar
sin mysin angvar
hitscan sprite[spriteid].x sprite[spriteid].y sprite[spriteid].z sprite[spriteid].sectnum mycos mysin zvel hitsector hitwall hitsprite hitx hity hitz clipmask
ends

state runningcode

ifp pjetpack break
subvar runflag 1

setvarvar temp strafedir
mulvarvar temp laststrafedir
ifvarl temp 0 setvar runcount 1 // lose speed if switching strafe directions

ifvarn forbackdir 0
{
	ifvare strafedir 13
		ifvarg runcount 20 setvar runcount 20
	ifvare strafedir -13
		ifvarg runcount 20 setvar runcount 20
}

ifvarg strafedir 0 subvar strafedir 1
ifvarl strafedir 0 addvar strafedir 1
ifvarg forbackdir 0 subvar forbackdir 1
ifvarl forbackdir 0 addvar forbackdir 1

ifp prunning ifvarg runflag 0
{
	ifvare runcount SPEED2TIME setplayer[THISACTOR].runspeed RUNNINGSPEED
	else
	{
		ifp pducking nullop else ifp ponground addvar runcount 1
		ifvarg runcount SPEED2TIME setvar runcount SPEED2TIME
		else
		{
			setvar temp SPEED1
			setvarvar tempb runcount
			mulvar tempb RUNACCEL
			addvarvar temp tempb
			ifp pducking { mulvar temp 4 divvar temp 5 }
			ifvarg temp RUNNINGSPEED setvar temp RUNNINGSPEED
			setplayer[THISACTOR].runspeed temp
		}
	}
}
else
{
	setvar temp RUNNINGSPEED
	ifp pducking { mulvar temp 4 divvar temp 5 }
	
	ifvare runcount 0 setplayer[THISACTOR].runspeed temp
	else ifp pfalling nullop else
	ifp ponground
	{
		subvar runcount 2
		ifvarl runcount 0 setvar runcount 0
		setplayer[THISACTOR].runspeed temp
	}
}

setvarvar lastforbackdir forbackdir
setvarvar laststrafedir strafedir

ends

state raincode


setvar tempe 0
setvar countvar 0
setvarvar countvarb raining
ifvarl countvarb 0 mulvar countvarb -1
ifvare rendmode 4 ifvarl framerate 30 { shiftvarr countvarb 1 }
//   ifmultiplayer nullop else
//   {
//   	ifvarl framerate 2 setvar countvarb 1
//   	ifvarvarg countvarb framerate setvarvar countvarb framerate
//   }

ifvarg countvarb 64 setvar countvarb 64
getplayer[THISACTOR].posx x
getplayer[THISACTOR].posy y

setvarvar countvar countvarb
whilevarn countvar 0
//whilevarvarn countvar countvarb
{
	subvar countvar 1
	//addvar countvar 1
	setvarvar xydist x
	randvarvar temp rainradius // the random component of the distance from player
	addvar temp 512
	addvarvar xydist temp
	randvar angvar 2047
	
	rotatepoint x y xydist y angvar tempb y
	// tempb and y are the new x and y coords
	updatesector tempb y tempd
	ifvarn tempd -1
	{
		ifvarand sector[tempd].ceilingstat 1
		ifvarn sector[tempd].hitag 101
		{
			getsector[tempd].ceilingpal temp
			ifvare temp 0
			{
				getsector[tempd].ceilingpicnum picnum
				ifvarn picnum MOONSKY1 ifvarn picnum BIGORBIT1 setvar temp 1
			}
			ifvarn temp 0
			{
				getceilzofslope tempd tempb y z // z is the return var
				ifvarl raining 0 // don't put snow too high up
				{
					getplayer[THISACTOR].posz temp
					setvarvar tempc temp
					subvarvar temp z // positive value is distance above player to ceiling
					ifvarvarg temp snowceiling 
					{
						subvarvar tempc snowceiling 
						setvarvar z tempc
					}
				}
				updatesectorz tempb y z tempd
				ifvarn tempd -1
				{
					espawn RAINSPRITE
// 					setactor[RETURN].x tempb
// 					setactor[RETURN].y y
// 					setactor[RETURN].z z
// 					setactor[RETURN].sectnum tempd
					setsprite RETURN tempb y z
					addvar tempe 1
				}
			}
		}
	}
}

ifvarg raining 16 
{
	ifoutside ifvare headunder 0
		soundonce RAIN
	 else
	{
		stopactorsound THISACTOR RAIN
	    ifvarg tempe 4 
	    soundonce RAINWINDOW //else stopsound RAINWINDOW
    }
}
else ifvarg raining 0
{
	ifoutside ifvare headunder 0
	soundonce RAINLOOP
	 else
	{
		stopactorsound THISACTOR RAINLOOP
	    ifvarg tempe 4
	    soundonce RAINWINDOW //else stopsound RAINWINDOW
    }
}

ends

state slipperycode

ifp pjetpack break 
iffloordistl 48 nullop else break

ifvarn sector[THISACTOR].lotag 50
{
	ifvare player[THISACTOR].spritebridge YES
	{
		getplayer[THISACTOR].sbs spriteid
		ifvarn sprite[spriteid].picnum SECTOREFFECTOR
		getactor[spriteid].lotag lotag
		
	}
	ifvarn lotag 50 { setvar lotag 0 break }
}

getplayer[THISACTOR].posxv xvel
setvar x 0
ifvarg xvel 0 ifvarvarl xvel oposxv setvar x -1 // slowing down x vel
ifvarl xvel 0 ifvarvarg xvel oposxv setvar x -1

getplayer[THISACTOR].posyv yvel
setvar y 0
ifvarg yvel 0 ifvarvarl yvel oposyv setvar y -1 // slowing down y vel
ifvarl yvel 0 ifvarvarg yvel oposyv setvar y -1

ifvarg dodge 0 ifvare jumpadxvel 0 { setvar x -1 setvar y -1 }
	
subvarvar oposxv xvel
ifvare x -1
{ 
	mulvar oposxv 15 divvar oposxv 16
} else
{
	mulvar oposxv 3 divvar oposxv 4
}
addvarvar xvel oposxv
setplayer[THISACTOR].posxv xvel

subvarvar oposyv yvel
ifvare y -1
{
	mulvar oposyv 15 divvar oposyv 16
} else
{
	mulvar oposyv 3 divvar oposyv 4
}
addvarvar yvel oposyv
setplayer[THISACTOR].posyv yvel

ifvare x -1 state STOPSTEPSOUNDS else
ifvare y -1 state STOPSTEPSOUNDS
ends

state laddercode

ifaction PSTAND nullop else action PSTAND
ifp ponground setvar bpress 0
setplayer[THISACTOR].falling_counter 0
ifvare fpress 0 ifvare bpress 0
{
	 setvar laddervel -256
	 getplayer[THISACTOR].posx lastladderx
	 getplayer[THISACTOR].posy lastladdery
}
 else
 {
	 ifvarn laddersound -1
	 soundoncevar laddersound
	 getplayer[THISACTOR].posz z
	 updatesectorz lastladderx lastladdery z mysector
	 ifvarn mysector -1
	 {
		 setplayer[THISACTOR].posx lastladderx
		 setplayer[THISACTOR].posy lastladdery
		 getplayer[THISACTOR].i spriteid
		 setactor[spriteid].x lastladderx
		 setactor[spriteid].y lastladdery
	 }
 }
ifp ponground { ifvarg laddervel 0 setvar laddervel 0 } else
{
	setplayer[THISACTOR].posxv 0
	setplayer[THISACTOR].posyv 0
}
subvar onladder 1
setplayer[THISACTOR].poszv laddervel

ends

define MANTDIST 480

// state checkmantlesprite

// ifvarn hitsprite -1
// {
// 	getactor[hitsprite].picnum picnum
// 	ifvare picnum 3699 // special mantling sprite
// 	ifvarl xydist MANTDIST
// 		setvar mantling 1
// }
// ends

// gamevar watch 0 0

state checkwall // checks to see if there is a close wall to grab onto

// mantling will be set to 1 if the answer is yes, 0 otherwise
setvar mantling 0

getactor[THISACTOR].z z
getactor[THISACTOR].x x
getactor[THISACTOR].y y
getactor[THISACTOR].sectnum mysector
getactor[THISACTOR].ang angvar
setvar zdist 0
cos mycos angvar
sin mysin angvar

hitscan x y z mysector mycos mysin zdist hitsector hitwall hitsprite hitx hity hitz clipmask
state pythagoras
setvarvar xydist2 xydist // saves the distance
subvar z 1536
hitscan x y z mysector mycos mysin zdist hitsector hitwall2 hitsprite hitx hity hitz clipmask
state pythagoras
ifvarn hitsprite -1
{
	getactor[hitsprite].picnum picnum
	
	ifvare picnum CRATE2 ifvarl xydist 844
	{
		setvar mantling 1 break 
	}
	
}

subvar z 4096

setvar hp 10
whilevarn hp 0
{
	setvar initcstat 1
	hitscan x y z mysector mycos mysin zdist hitsector hitwall hitsprite hitx hity hitz clipmask
	state pythagoras
	ifvarn hitsprite -1 ifvarl xydist MANTDIST getactor[hitsprite].cstat initcstat
	setvarvar xydist2 xydist
	subvar z 512 //256
	hitscan x y z mysector mycos mysin zdist hitsector hitwall2 hitsprite hitx hity hitz clipmask
	state pythagoras
	ifvarn hitsprite -1 ifvarl xydist MANTDIST
	{
		getactor[hitsprite].picnum picnum
		ifvarn picnum BGRATE1 ifvarn picnum FANSPRITEBROKE
		getactor[hitsprite].cstat initcstat
	}
	
	
// 	ifvare watch 1
// 	{
// 		addlogvar xydist
// 		addlogvar xydist2
// 		addlogvar hitsprite
// 		addlogvar hitwall
// 		addlogvar hitwall2
// 		addlogvar initcstat
// 	}
	ifvarg xydist2 MANTDIST nullop else
	ifvare hitsprite -1 ifvare hitwall -1 nullop else
	ifvarn hitwall -1 ifvarvare hitwall hitwall2 nullop else
	ifvarand initcstat 1
	{
		ifvarvarg xydist xydist2 
		{
			setvar mantling 1
			ifvare hitsprite -1
			setvar mantling 1 else
			{
				getactor[hitsprite].cstat tempb
				ifvarand tempb 32768 nullop else
				ifvarand tempb 1 setvar mantling 1
			}
		}
		else ifvarn hitsprite -1
		{
		    ifvare picnum BGRATE1
		    setvar mantling 1
		    ifvare picnum FANSPRITEBROKE
		    setvar mantling 1
	    }
	}
	
	
	subvar hp 1
	ifvare mantling 1 setvar hp 0 //10
	subvar z 512 // 256
}

ends

state mantlingcode

ifonwater nullop else
ifvare scaled NO
{
	ifp ponsteroids setvar tempb 901 else setvar tempb 721
	ifvarvare player[THISACTOR].jumping_counter tempb setplayer[THISACTOR].jumping_counter 0
}

ifp ponground ifvare jumped 1 setvar jumped 0
ifvarl mantling 0 addvar mantling 1
ifvare mantling 1 addvar mantwait 1 else setvar mantwait 0

ifvarn onladder 0 { setvar mantling 0 break }
ifvarg dodge 0 { setvar mantling 0 break }
ifvarn holding -1 { setvar mantling 0 break }
ifp pjetpack { setvar mantling 0 break }

ifvare mantling 0 ifvare jumpadxvel 0
{
	getplayer[THISACTOR].posx x
	getplayer[THISACTOR].posy y
	getactor[THISACTOR].z z
	getplayer[THISACTOR].cursectnum mysector
	ifvare mysector -1 getsector[THISACTOR].floorz mz else
	getflorzofslope mysector x y mz // mz is the return var
	subvarvar mz z // mz is now the distance to the ground
	setvar temp 0
	ifvarg mz 6144 setvar temp 1
	ifvarg underwater 0 
	{
		ifvarg mz 3072 setvar temp 1
		ifvarand bits 1 nullop else setvar temp 0
	}
	ifvare temp 1 
	{
		getplayer[THISACTOR].poszv zvel
		ifvare jumped 1 setvar tempb 0 else setvar tempb 1536
		ifvarg underwater 0 setvar zvel 2048
		//ifonwater setvar zvel 2048
		//ifinwater setvar zvel 2048
		ifvarvarg zvel tempb
		{
			state checkwall
			ifvare mantling 1 
			{
				getplayer[THISACTOR].posz mantz
				ifvarg underwater 0
				setplayer[THISACTOR].sound_pitch 0
				sound DUKE_GRUNT
				wackplayer
				getplayer[THISACTOR].posz z
				subvar z 512
				setplayer[THISACTOR].posz z
				shiftvarr zvel 7
				addvar zvel 3
				setvarvar mantwaittime zvel
				ifvarg mantwaittime 40 setvar mantwaittime 40
			}
			
		}
	}

}

ifvarg mantling 0
{
	
	ifvare mantling 1 // hanging there, possibly strafing
	{
		setplayer[THISACTOR].falling_counter 0
		getplayer[THISACTOR].posxv xvel
		divvar xvel 2
		setplayer[THISACTOR].posxv xvel
		getplayer[THISACTOR].posyv yvel
		divvar yvel 2
		setplayer[THISACTOR].posyv yvel
		setplayer[THISACTOR].posz mantz
		setplayer[THISACTOR].poszv -256 // 0
		state checkwall
		ifvare mantling 0
		{
			ifvarg underwater 0 ifvarand bits 1 setvar mantling 2 else
			ifvarg mantcheck 0 setvar mantling 1
		} else setvar mantcheck 6 // 6 ticks grace period
	}
	else
	{
		setplayer[THISACTOR].poszv -1536
		getplayer[THISACTOR].ang angvar
		cos xvel angvar
     	sin yvel angvar
        mulvar xvel 64 
        mulvar yvel 64
		setplayer[THISACTOR].posxv xvel
		setplayer[THISACTOR].posyv yvel
		getactor[THISACTOR].z z
		getactor[THISACTOR].x x
		getactor[THISACTOR].y y
		getactor[THISACTOR].sectnum mysector
		getactor[THISACTOR].ang angvar
		setvar zdist 0
		cos mycos angvar
		sin mysin angvar
		hitscan x y z mysector mycos mysin zdist hitsector hitwall hitsprite hitx hity hitz clipmask
		state pythagoras
		ifvarg xydist 384 ifvarg mantling 5 setvar mantling -10
		//addvar mantling 1
		else
		ifvarand bits 1 addvar mantling 1 else setvar mantling -10
		ifvarg mantling 23 setvar mantling -10
		setplayer[THISACTOR].jumping_toggle 1
	}
	
}

ends

state dodgewallcheck // checks to see if there is a close wall in the actor's path at angvar

getactor[THISACTOR].z z
subvar z 6144
getactor[THISACTOR].x x
getactor[THISACTOR].y y
getactor[THISACTOR].sectnum mysector
setvar zdist 0
cos mycos angvar
sin mysin angvar

hitscan x y z mysector mycos mysin zdist hitsector hitwall hitsprite hitx hity hitz clipmask

// temp will be set to 1 if the answer is yes, 0 otherwise
setvar temp 0
ifvarn hitsprite -1
{
	 getactor[hitsprite].cstat tempb
	 ifvarand tempb 16 nullop else
	 ifvarand tempb 1 nullop else 
		   break
	   
}
espawn NOTHING
setactor[RETURN].x hitx
setactor[RETURN].y hity
setactor[RETURN].z hitz
ldist xydist THISACTOR RETURN

// next code gets distance between (x,y) and (hitx,hity), puts result in xydist

ifvarg xydist 512 break

setvar temp 1

ends

state walljumpcode

setvarvar angvar dodgeang
addvar angvar 1024
state dodgewallcheck
ifvare temp 1 
{
	addvar walljump 1
	addvar runcount 3
	getplayer[THISACTOR].falling_counter digz
	ifvarg digz 8 
	{
		subvar digz 8
		setplayer[THISACTOR].falling_counter digz
		setvar dodgeagain 5
	}
	setvar dodge 7
	randvar tempb 3
	ifvare tempb 0 sound DUKE_GRUNT
	ifvare tempb 1 sound JUMP5 else
	ifvare tempb 2 sound JUMP2 else
	sound JUMP4
	
}

ends

state dodgecode

ifvarg dodgeagain 0 subvar dodgeagain 1

setvarvar tempb dodge
//setvar temp FREERUNNER
//ifvare temp YES 
ifvarn walljump 0 setvar tempb 0

ifvarn holding -1
ifvarg actorvar[holding].weight 40 break

ifp palive
   //ifvare dodge 0 
   ifvare tempb 0
     ifvare dodgeagain 0
   {
	   setvar temp 0
	   ifp ponground ifvare walljump 0 setvar temp 1
	   
	   ifvare ltap 2
	   {
		  getplayer[THISACTOR].ang dodgeang
		  subvar dodgeang 512
		  ifvare temp 0 state walljumpcode

// 		  ifvare temp 1
// 		  {  
// 		     setvar dodge 10
// 		     sound DUKE_GRUNT
// 	      }
	   }
	   ifvare rtap 2
	   {
		  getplayer[THISACTOR].ang dodgeang
		  addvar dodgeang 512
		  ifvare temp 0 state walljumpcode
		  
// 		  ifvare temp 1
// 		  {
// 		     setvar dodge 10
// 		     sound DUKE_GRUNT
// 	      }
	   }
	   ifvare ftap 2
	   {
		  getplayer[THISACTOR].ang dodgeang
		  ifvare temp 0 state walljumpcode
// 		  ifvare temp 1
// 		  {
// 		     setvar dodge 10
// 		     sound DUKE_GRUNT 
// 	      }
	   }
	   ifvare btap 2
	   {
		  getplayer[THISACTOR].ang dodgeang
		  addvar dodgeang 1024
		  ifvare temp 0 state walljumpcode

// 		  ifvare temp 1
// 		  {
// 		     setvar dodge 10
// 		     sound DUKE_GRUNT
// 	      }
	   }
   }
 
 
ifvarg dodge 0
{
	 cos xvel dodgeang
     sin yvel dodgeang
     ifvarn jumpadxvel 0
     {
	     mulvarvar xvel jumpadxvel
	     mulvarvar yvel jumpadxvel
     }
     else
     {
	     ifvarg superkicking 0
	     {
		     ifvare spinkick 1
		     {
			     shiftvarl xvel 7
			     shiftvarl yvel 7
		     } else
		     {
			      	
			 	ifvarn kicktarg -1
			 	{
				 	ifvarn sprite[kicktarg].statnum 1 setvar kicktarg -1
				 	else
				 	{
					 	getactor[kicktarg].x mx
						getactor[kicktarg].y my
						subvarvar mx player[THISACTOR].posx
						subvarvar my player[THISACTOR].posy
						getangle angvar mx my
						setplayer[THISACTOR].ang angvar
				 	}
			 	}
			     shiftvarl xvel 8
			     shiftvarl yvel 8
		     }
		     ifp pducking { divvar xvel 3 divvar yvel 3 }
	     }
	     else
	     {
		     mulvar xvel 352
		     mulvar yvel 352
	     }
	     //setvar temp FREERUNNER
	     //ifvare temp YES
	     ifvarg walljump 0
	     {
		     shiftvarr xvel 1
		     shiftvarr yvel 1
	     }
     }
	 ifvarg walljump 0
	 {
		 setvar zvel -1536
		 ifvarg walljump 1 
		 {
			 setvarvar tempb walljump
		     divvar tempb 2
		     ifvare tempb 0 setvar tempb 1
			 divvarvar zvel tempb
		 }
		 setplayer[THISACTOR].poszv zvel
	 }
	 
	 setplayer[THISACTOR].posxv xvel
	 setplayer[THISACTOR].posyv yvel
	 ifvarn jumpadxvel 0 
	 {
		 setplayer[THISACTOR].falling_counter 0
		 ifaction PJUMPING nullop else action PJUMPING
	 }
}

ends

state underwatercode

getplayer[THISACTOR].posxv xvel
mulvar xvel 5 divvar xvel 4
ifvarg xvel 1000000 setvar xvel 1000000
ifvarl xvel -1000000 setvar xvel -1000000
setplayer[THISACTOR].posxv xvel
getplayer[THISACTOR].posyv yvel
mulvar yvel 5 divvar yvel 4
ifvarg yvel 1000000 setvar yvel 1000000
ifvarl yvel -1000000 setvar yvel -1000000
setplayer[THISACTOR].posyv yvel

getactorvar[watereffector].initx temp // waterspeed
ifvarn temp 0 // moving water
ifvare mantling 0
ifvare onladder 0
{
	getactor[watereffector].ang angvar // water direction
	cos xvel angvar
    sin yvel angvar
	mulvarvar xvel temp
	mulvarvar yvel temp
	
    getplayer[THISACTOR].posxv mx
    setvarvar tempc xvel
    divvar tempc 2
    addvarvar mx tempc // xvel
	getplayer[THISACTOR].posyv my
	setvarvar tempc yvel
	divvar tempc 2
	addvarvar my tempc // yvel
	ifvarg xvel 0 
	{
		ifvarl xvel 1000000 setvar xvel 1000000
		ifvarvarg mx xvel setvarvar mx xvel
	}
	else 
	{
		ifvarg xvel -1000000 setvar xvel -1000000
		ifvarvarl mx xvel setvarvar mx xvel
	}
	ifvarg yvel 0
	{
		ifvarl yvel 1000000 setvar yvel 1000000
		ifvarvarg my yvel setvarvar my yvel
	}
	else 
	{
		ifvarg yvel -1000000 setvar yvel -1000000
		ifvarvarl my yvel setvarvar my yvel
	}
	
	setplayer[THISACTOR].posxv mx
	setplayer[THISACTOR].posyv my
}

ifvarg underwater 3 soundonce ITEM_SPLASH
else
ifvare underwater 1 
{
	// setgamepalette 0
	setuserdef[THISACTOR].const_visibility DEFAULTVISIBILITY
	setplayer[THISACTOR].footprintcount 6
	setplayer[THISACTOR].footprintpal 0
	setplayer[THISACTOR].footprintshade 0
	stopsound DUKE_UNDERWATER
	setplayer[THISACTOR].sound_pitch 0
	setplayer[THISACTOR].runspeed RUNNINGSPEED
	stopsound DUKE_UNDERWATER
	getactor[watereffector].sectnum tempb
	break
}


getactor[watereffector].z peractor2
setplayer[THISACTOR].falling_counter 0
setvar temp RUNNINGSPEED
mulvar temp 2 divvar temp 3
setplayer[THISACTOR].runspeed temp
setplayer[THISACTOR].footprintcount 0

getplayer[THISACTOR].posz z
addvar z 512
getactorvar[watereffector].monstflags temp
ifvarvarg z peractor2 setvar temp 1
ifvare temp 1
{
	// setgamepalette 1
	setvar headunder YES
	stopsound RAIN
	getactorvar[watereffector].initshade temp
	addvar temp 1
	shiftvarl temp 5
	addvar temp DEFAULTVISIBILITY
	setuserdef[THISACTOR].const_visibility temp
	
	getactor[watereffector].pal waterpal
	getactor[watereffector].sectnum tempb
	ifvare waterpal 32
	palfrom 52 0 3 6 // very dark water
	else
    ifvare waterpal 31
    palfrom 36 0 5 10 // dark water
    else
    ifvare waterpal 27 // red liquid
    palfrom 36 30
    else
    ifvare waterpal 28 // green liquid
    palfrom 36 0 30
    else
    ifvare waterpal 29 // blue liquid
    palfrom 36 0 0 30
    else
    palfrom 18 0 25 35 // normal water
	setplayer[THISACTOR].sound_pitch 0
	setplayer[THISACTOR].crack_time 777
	soundonce DUKE_UNDERWATER
	setplayer[THISACTOR].sound_pitch -1024
	getplayer[THISACTOR].scuba_amount temp
	ifvarg temp 0
	{
		subvar temp 1
		setplayer[THISACTOR].scuba_amount temp
		setplayer[THISACTOR].scuba_on 1
		setvar oxygen 390
	}
	else
	{
		ifvarg oxygen 0 subvar oxygen 1
		setplayer[THISACTOR].airleft oxygen
		ifvare oxygen 0
		{
			getplayer[THISACTOR].extra_extra8 temp
			addvar temp 32
			setplayer[THISACTOR].extra_extra8 temp
		}
	}
}
else 
{
	// setgamepalette 0
	setvar headunder 0
	stopsound DUKE_UNDERWATER
	setplayer[THISACTOR].sound_pitch 0
	setuserdef[THISACTOR].const_visibility DEFAULTVISIBILITY
	getactor[watereffector].sectnum tempb
	ifvarl oxygen 312 sound DUKE_GASP
	setvar oxygen 390
}


getplayer[THISACTOR].poszv zvel
ifvarg zvel 0
{
 	subvar zvel 480
 	ifvarl zvel -176 setvar zvel -176
 	setplayer[THISACTOR].poszv zvel 
}

ends

state botcommandcode

ifhitspace
  ifvarvarn hitsprite botselected
{
	setactorvar[botselected].initx hitx
	setactorvar[botselected].inity hity
	setactorvar[botselected].initz hitz
	setactorvar[botselected].command 1
	globalsound EXITMENUSOUND
	setvar botselected -1
	userquote 1003
}
else
{
	espawn BOTMARKER
	setactor[RETURN].x hitx
	setactor[RETURN].y hity
	ifvarn hitsector -1
	{
		getflorzofslope hitsector hitx hity temp
		setactor[RETURN].z temp
	}
}

ends

state positionholding

ifvare drinksoda 0
{
	getactor[holding].pitch temp
	ifvarg temp 0 
	{ 
		subvar temp 32 
		ifvarl temp 0 setvar temp 0 
		setactor[holding].pitch temp 
		setactor[holding].mdzoff 0
	}
}
getplayer[THISACTOR].horiz zdist
subvar zdist 100
mulvar zdist -32
addvarvar mz zdist
rotatepoint x y digx y angvar mx my
updatesectorz mx my mz temp
ifvarn temp -1
{
	changespritesect holding temp
	setactor[holding].x mx
	setactor[holding].y my
	ifvarn pickup 0
	{
		getactor[holding].z tempb
		ifvarvarg mz tempb setvarvar mz tempb
	}
	setactor[holding].z mz
}

ends

state holdingcode

ifvarn holding -1
{
	ifvarvare holding gravitysprite soundonce GRAVPULL
	ifvare sprite[holding].statnum 1024 { setvar holding -1 setvar pickup -12 }
	ifvarn holding -1 ifvarvarn sprite[holding].picnum holdingpic
	{
		setvar holding -1 
		setvar pickup -12 
	}
	
	ifvarn holding -1
	{
		ifp ponsteroids nullop else
		ifvare gravitysprite -1
		{
			// limit player speed
			ifvarg actorvar[holding].weight 40
			{
				getplayer[THISACTOR].posxv xvel
				ifvarg xvel 350000 setplayer[THISACTOR].posxv 350000
				ifvarl xvel -350000 setplayer[THISACTOR].posxv -350000
				getplayer[THISACTOR].posyv xvel
				ifvarg xvel 350000 setplayer[THISACTOR].posyv 350000
				ifvarl xvel -350000 setplayer[THISACTOR].posyv -350000
			}
		}
		getplayer[THISACTOR].posz mz
		ifvare holdingpic SAWBLADEGROUND addvar mz 1536 else
		addvar mz 3072
		setvarvar temp pickup
		mulvar temp 640
		addvarvar mz temp
		getplayer[THISACTOR].posx x
		getplayer[THISACTOR].posy y
		setvarvar digx x
		ifvarn drinksoda 0 addvar digx 480 else
		{
			addvar digx 400
			getactor[holding].mdflags temp
			orvar temp 16
			setactor[holding].mdflags temp // held sprites are adjusted in ANIMATESPRITES to prevent jittering
		}
		ifvarg drinksoda 20
		{
			setvarvar temp drinksoda
			subvar temp 20
			shiftvarl temp 4
			ifvarg temp 240 
			{
				setvar temp 240
				ifvarl drinksoda 70 soundonce DUKE_TAKEPILLS
			}
			subvarvar digx temp
			setvarvar temp drinksoda
			subvar temp 20
			shiftvarl temp 7
			ifvarg temp 4096 setvar temp 4096
			subvarvar mz temp
		}
		getplayer[THISACTOR].ang angvar
		state positionholding
		ifvare temp -1 ifvare pickup 0 // object not in sector
		{
			getactorvar[holding].bouncesound temp
			ifvarn temp -1 soundoncevar temp
			subvar digx 96
			state positionholding
			ifvare temp -1 ifvarn holdingpic FUSE ifvarvarn holding gravitysprite
			{
				getactorvar[holding].initcstat temp
				setactor[holding].cstat temp
				setactor[holding].x x
				setactor[holding].y y
				//subvar mz 6144
				setactor[holding].z mz
				updatesectorz x y mz temp
				ifvarn temp -1
				changespritesect holding temp
				setvar holding -1
				setvar pickup -12
				setvar drinksoda 0
			}
		}
	
		ifvarg pickup 0 subvar pickup 1
	}
} 
else setvar droptile 0 // in the player, droptile is 1 only when pressing alt-fire should drop the gg's sprite
ifvarl pickup 0 addvar pickup 1

ends

state changeakimbo

ifvarl akimbo 1
{
	setvar akimbo 9 // countdown timer for moving guns into correct position
	ifvare depistols NO
	{
		setvar WEAPON1_CLIP 24
		setvar WEAPON1_RELOAD 48
		setvar WEAPON1_FIREDELAY 2
		setvar WEAPON1_TOTALTIME 8
		setvar WEAPON1_FLAGS 68
		setvar WEAPON1_SPAWNTIME 2
		setvar WEAPON1_SPAWN 2533
		setvar WEAPON1_INITIALSOUND 0
		setvar WEAPON1_SOUND2TIME 0
		setvar WEAPON1_SOUND2SOUND 0
		setvar WEAPON1_RELOADSOUND1 4
		setvar WEAPON1_RELOADSOUND2 5
	}
	else
	{
		setvar WEAPON1_CLIP 16
		setvar WEAPON1_RELOAD 52
		setvar WEAPON1_FIREDELAY 2
		setvar WEAPON1_TOTALTIME 12
		setvar WEAPON1_FLAGS 68
		setvar WEAPON1_SPAWNTIME 2
		setvar WEAPON1_SPAWN 2533
		setvar WEAPON1_INITIALSOUND 0
		setvar WEAPON1_SOUND2TIME 0
		setvar WEAPON1_SOUND2SOUND 0
		setvar WEAPON1_RELOADSOUND1 4
		setvar WEAPON1_RELOADSOUND2 5
	}
	ifvarn botclip 0 ifvare menutoggle 0
	{
		getplayer[THISACTOR].ammo_amount 1 tempb
		mulvar botclip 2
		ifvarvarg botclip tempb setvarvar botclip tempb
		// this code does nothing if the state is called from FIRSTGUNSPRITE, but that's ok
	}
}
else
{
	setvar akimbo -9
	ifvare depistols NO
	{
		setvar WEAPON1_CLIP 12
		setvar WEAPON1_RELOAD 27
		setvar WEAPON1_FIREDELAY 2
		setvar WEAPON1_TOTALTIME 6
		setvar WEAPON1_FLAGS 4
		setvar WEAPON1_SPAWNTIME 2
		setvar WEAPON1_SPAWN 2533
		setvar WEAPON1_INITIALSOUND 0
		setvar WEAPON1_SOUND2TIME 0
		setvar WEAPON1_SOUND2SOUND 0
		setvar WEAPON1_RELOADSOUND1 4
		setvar WEAPON1_RELOADSOUND2 5
	}
	else
	{
		setvar WEAPON1_CLIP 9 // actually 8, but we tell the game it's 9 to avoid a hardcoded reloading bug
		setvar WEAPON1_RELOAD 29
		setvar WEAPON1_FIREDELAY 2
		setvar WEAPON1_TOTALTIME 10
		setvar WEAPON1_FLAGS 4
		setvar WEAPON1_SPAWNTIME 2
		setvar WEAPON1_SPAWN 2533
		setvar WEAPON1_INITIALSOUND 0
		setvar WEAPON1_SOUND2TIME 0
		setvar WEAPON1_SOUND2SOUND 0
		setvar WEAPON1_RELOADSOUND1 4
		setvar WEAPON1_RELOADSOUND2 5
	}
	ifvarn botclip 0 ifvarg timer 1 ifvare menutoggle 0
	{
		setvarvar temp botclip
		divvar botclip 2
	}
		
		
}

ends

state changepistols // switches between regular pistols and desert eagle pistols

ifvare depistols YES
{
	setvar depistols NO
	setvar WEAPON1_SHOOTS SHOTSPARK1
	ifvare dpsounds NO
	setvar WEAPON1_FIRESOUND PISTOL_FIRE else
	setvar WEAPON1_FIRESOUND DPPISTOLFIRE
	getplayer[THISACTOR].ammo_amount 1 temp
	mulvar temp 3 divvar temp 2
	ifvarg temp MAXPISTOLAMMO setvar temp MAXPISTOLAMMO
	setplayer[THISACTOR].ammo_amount 1 temp
	ifvarl akimbo 1 // not using akimbo pistols
	{
		setvar WEAPON1_CLIP 12
		setvar WEAPON1_RELOAD 27
		setvar WEAPON1_FIREDELAY 2
		setvar WEAPON1_TOTALTIME 6
		setvar WEAPON1_FLAGS 4
		setvar WEAPON1_SPAWNTIME 2
		setvar WEAPON1_SPAWN 2533
		setvar WEAPON1_INITIALSOUND 0
		setvar WEAPON1_SOUND2TIME 0
		setvar WEAPON1_SOUND2SOUND 0
		setvar WEAPON1_RELOADSOUND1 4
		setvar WEAPON1_RELOADSOUND2 5
	}
	else
	{
		setvar WEAPON1_CLIP 24
		setvar WEAPON1_RELOAD 48
		setvar WEAPON1_FIREDELAY 2
		setvar WEAPON1_TOTALTIME 8
		setvar WEAPON1_FLAGS 68
		setvar WEAPON1_SPAWNTIME 2
		setvar WEAPON1_SPAWN 2533
		setvar WEAPON1_INITIALSOUND 0
		setvar WEAPON1_SOUND2TIME 0
		setvar WEAPON1_SOUND2SOUND 0
		setvar WEAPON1_RELOADSOUND1 4
		setvar WEAPON1_RELOADSOUND2 5
	}
}
else
{
	setvar depistols YES
	setvar WEAPON1_SHOOTS DEBULLET
	setvar WEAPON1_FIRESOUND DEPISTOL_FIRE
	getplayer[THISACTOR].ammo_amount 1 temp
	mulvar temp 2 divvar temp 3
	setplayer[THISACTOR].ammo_amount 1 temp
	ifvarl akimbo 1 // not using akimbo pistols
	{
		ifvarg botclip 8 setvar botclip 8
		getplayer[THISACTOR].ammo_amount 1 temp
		mulvar temp 2 divvar temp 3
		setvar WEAPON1_CLIP 9  // actually 8, but we tell the game it's 9 to avoid a hardcoded reloading bug
		setvar WEAPON1_RELOAD 29
		setvar WEAPON1_FIREDELAY 2
		setvar WEAPON1_TOTALTIME 10
		setvar WEAPON1_FLAGS 4
		setvar WEAPON1_SPAWNTIME 2
		setvar WEAPON1_SPAWN 2533
		setvar WEAPON1_INITIALSOUND 0
		setvar WEAPON1_SOUND2TIME 0
		setvar WEAPON1_SOUND2SOUND 0
		setvar WEAPON1_RELOADSOUND1 4
		setvar WEAPON1_RELOADSOUND2 5
	}
	else
	{
		ifvarg botclip 16 setvar botclip 16
		setvar WEAPON1_CLIP 16
		setvar WEAPON1_RELOAD 52
		setvar WEAPON1_FIREDELAY 2
		setvar WEAPON1_TOTALTIME 12
		setvar WEAPON1_FLAGS 68
		setvar WEAPON1_SPAWNTIME 2
		setvar WEAPON1_SPAWN 2533
		setvar WEAPON1_INITIALSOUND 0
		setvar WEAPON1_SOUND2TIME 0
		setvar WEAPON1_SOUND2SOUND 0
		setvar WEAPON1_RELOADSOUND1 4
		setvar WEAPON1_RELOADSOUND2 5
	}
}
ends

state changeshotgun

// THIS STATE SHOULD NOT BE CALLED UNLESS YOU HAVE TWO SHOTGUNS

ifvarand ALTSHOTGUN 4
{
	subvar ALTSHOTGUN 4
	ifvarand ALTSHOTGUN 1
	{
		setvar WEAPON2_FIREDELAY 2
		setvar WEAPON2_TOTALTIME 26
		setvar WEAPON2_SHOTSPERBURST 8
		setvar WEAPON2_SPAWN SHOTGUNSHELL
		setvar WEAPON2_FIRESOUND MOSSFIRE
		setvar WEAPON2_SOUND2TIME 13
		setvar WEAPON2_SOUND2SOUND -1
	} else
	{
		setvar WEAPON2_FIREDELAY 4
		setvar WEAPON2_TOTALTIME 30
		setvar WEAPON2_SHOTSPERBURST 7
		setvar WEAPON2_SPAWN SHOTGUNSHELL
		setvar WEAPON2_FIRESOUND SHOTGUN_FIRE
		setvar WEAPON2_SOUND2TIME 15
		setvar WEAPON2_SOUND2SOUND SHOTGUN_COCK
	}
} else
{
	sound DBCLOSE
	orvar ALTSHOTGUN 4
	setvar WEAPON2_FIREDELAY 4
	setvar WEAPON2_SPAWN NOTHING
	setvar WEAPON2_FIRESOUND DBFIRE
	setvar WEAPON2_TOTALTIME 62 //82
	setvar WEAPON2_SHOTSPERBURST 20
	setvar WEAPON2_SOUND2SOUND -1
}


ends

state changeminigun

getplayer[THISACTOR].ammo_amount 3 temp
setplayer[THISACTOR].ammo_amount 3 MINIGUNAMMO
setvarvar MINIGUNAMMO temp

ifvarand ALTCHAINGUN 2
{
	setvar temp MAXCHAINGUNAMMO
	shiftvarl temp 1
	setplayer[THISACTOR].max_ammo_amount 3 temp
	subvar ALTCHAINGUN 2
	ifvare usemp5 NO
	{
		setvar WEAPON3_SHOTSPERBURST 0
		setvar WEAPON3_FIREDELAY 3
		ifvare dpsounds YES
		setvar WEAPON3_FIRESOUND DPCHAINGUNFIRE else
		setvar WEAPON3_FIRESOUND CHAINGUN_FIRE
		setvar WEAPON3_SHOOTS CHAINGUN
		setvar WEAPON3_TOTALTIME 12
		setvar WEAPON3_FLAGS 73812
		setvar WEAPON3_CLIP 0
		setvar WEAPON3_SPAWNTIME 0
		setvar WEAPON3_RELOADSOUND1 4
		setvar WEAPON3_RELOADSOUND2 5
	}
	else
	{
		setvar WEAPON3_SHOTSPERBURST 0
		setvar WEAPON3_FIREDELAY 3
		setvar WEAPON3_SHOOTS MP5BULLET
		setvar WEAPON3_TOTALTIME 4
		setvar WEAPON3_FIRESOUND MP5FIRE
		setvar WEAPON3_FLAGS 8196
		setvar WEAPON3_CLIP 30
		setvar WEAPON3_SPAWNTIME 2
		setvar WEAPON3_RELOADSOUND1 -1
		setvar WEAPON3_RELOADSOUND2 -1
	}
}
else
{
	setplayer[THISACTOR].max_ammo_amount 3 800
	orvar ALTCHAINGUN 2
	setvar WEAPON3_SHOTSPERBURST 2
	setvar WEAPON3_SHOOTS SHOTSPARK1
	setvar WEAPON3_FIRESOUND BOS1_ATTACK1 // MINIGUN_FIRE
	setvar WEAPON3_FLAGS 84
	setvar WEAPON3_FIREDELAY 2
	setvar WEAPON3_TOTALTIME 12
	// setvar WEAPON3_FIRESOUND DPCHAINGUNFIRE
	setvar WEAPON3_FLAGS 73812
	setvar WEAPON3_CLIP 0
	setvar WEAPON3_SPAWNTIME 0
	setvar WEAPON3_RELOADSOUND1 4
	setvar WEAPON3_RELOADSOUND2 5
}


ends

state resetweaponstate

ifvare useakimbo YES
{
	setvar akimbo 1
	state changeakimbo
	setvar akimbo -1
}

ifvare dukemon YES
{
	setvar dukeballstrength 50
	setvar dukeballselected 0
	setvar temp 0
	whilevarn temp MAXTRIPBOMBAMMO
	{
		setarray dukeballpics[temp] 0
		setarray dukeballflags[temp] 0
		setarray dukeballpals[temp] 0
		addvar temp 1
	}

}

setvar SHOTGUNCLIP 0
setvar MP5INCLIP 0
setvar botclip 0
setvar ALTSHOTGUN 0
setvar LASERAMMO 0

ifvare usemossberg YES
{
	
	setvar WEAPON2_FIREDELAY 2
	setvar WEAPON2_TOTALTIME 26
	setvar WEAPON2_SHOTSPERBURST 8
	setvar WEAPON2_FIRESOUND MOSSFIRE
	setvar WEAPON2_SOUND2TIME 13
	setvar WEAPON2_SOUND2SOUND -1
	
}
else
{
	// normal shotgun
	setvar WEAPON2_FIREDELAY 4
	setvar WEAPON2_TOTALTIME 30
	setvar WEAPON2_SPAWN SHOTGUNSHELL
	setvar WEAPON2_SHOTSPERBURST 7
	setvar WEAPON2_FIRESOUND SHOTGUN_FIRE
	setvar WEAPON2_SOUND2TIME 15
	setvar WEAPON2_SOUND2SOUND SHOTGUN_COCK		
}

ends

state clearinventory

setplayer[THISACTOR].gotweapon 1 0
setplayer[THISACTOR].gotweapon 2 0
setplayer[THISACTOR].gotweapon 3 0
setplayer[THISACTOR].gotweapon 4 0
setplayer[THISACTOR].gotweapon 5 0
setplayer[THISACTOR].gotweapon 6 0
setplayer[THISACTOR].gotweapon 7 0
setplayer[THISACTOR].gotweapon 8 0
setplayer[THISACTOR].gotweapon 9 0
setplayer[THISACTOR].gotweapon 11 0
setplayer[THISACTOR].ammo_amount 1 0
setplayer[THISACTOR].ammo_amount 2 0
setplayer[THISACTOR].ammo_amount 3 0
setplayer[THISACTOR].ammo_amount 4 0
setplayer[THISACTOR].ammo_amount 5 0
setplayer[THISACTOR].ammo_amount 6 0
setplayer[THISACTOR].ammo_amount 7 0
setplayer[THISACTOR].ammo_amount 8 0
setplayer[THISACTOR].ammo_amount 9 0
setplayer[THISACTOR].ammo_amount 11 0
setplayer[THISACTOR].curr_weapon 0
setactor[THISACTOR].extra MAXPLAYERHEALTH
setplayer[THISACTOR].heat_amount 0
setplayer[THISACTOR].firstaid_amount 0
setplayer[THISACTOR].steroids_amount 0
setplayer[THISACTOR].holoduke_amount 0
setplayer[THISACTOR].jetpack_amount 0
setplayer[THISACTOR].boot_amount 0
setplayer[THISACTOR].scuba_amount 0
setvar fpresstime 0
setvar bpresstime 0
setvar lpresstime 0
setvar rpresstime 0

ends

state sleepingcode

setplayer[THISACTOR].movement_lock 31
setvar ftap 0
setvar btap 0
setvar ltap 0
setvar rtap 0
setactor[THISACTOR].htextra -1
ifvare teleported 105
{
	getactor[THISACTOR].extra temp
	ifvarl temp MAXPLAYERHEALTH setactor[THISACTOR].extra MAXPLAYERHEALTH
}
addvar sleeping 1
ifvarn sleeping 0 
ifvare teleported 107 
{
	setvarvar temp sleeping
	addvar sleeping 1
	addvarvar sleeping fallflag
	ifvarl temp 0 ifvarg sleeping 0 setvar sleeping 0 // must stop at 0
}
ifvare sleeping 0 
{
	addvar sleeping 1 // do not let it be zero until finished
	// teleport to next SE if one exists
	setvar spriteid 0
	whilevarn spriteid 16384
	{
		ifvare sprite[spriteid].picnum SECTOREFFECTOR ifvarn sprite[spriteid].statnum 1024
		{
			getactorvar[spriteid].lotag temp
			ifvare temp 105
			// ifvare actorvar[spriteid].lotag 105
			{
				// ifvarvare hitag sprite[spriteid].hitag
				getactor[spriteid].hitag tempb
				ifvarvare hitag tempb
				{
					getactor[spriteid].x x
					getactor[spriteid].y y
					getactor[spriteid].z z
					setplayer[THISACTOR].posx x
					setplayer[THISACTOR].posy y
					setactor[THISACTOR].z z
					subvar z 8192
					setplayer[THISACTOR].posz z
					getactor[spriteid].sectnum mysector
					changespritesect THISACTOR mysector
					setplayer[THISACTOR].cursectnum mysector
					setactorvar[spriteid].lotag 106
					getactor[spriteid].ang peractor4
					getactorvar[spriteid].monstflags temp
					ifvarand temp 1
					{
						state resetweaponstate
						state clearinventory
					}
					
					setvar spriteid 16383
				}
			}
			// ifvare actorvar[spriteid].lotag 107
			else ifvare temp 107
			{
				ifvarvare hitag sprite[spriteid].hitag
				{
					getactor[spriteid].sectnum mysector
					//ifvarvarn player[THISACTOR].cursectnum mysector
					ifvarvarn targetlock mysector
					{
						getactor[spriteid].x x
						getactor[spriteid].y y
						getactor[spriteid].z z
						setplayer[THISACTOR].posx x
						setplayer[THISACTOR].posy y
						setactor[THISACTOR].z z
						subvar z 8192
						setplayer[THISACTOR].posz z
						changespritesect THISACTOR mysector
						setplayer[THISACTOR].cursectnum mysector
						setactorvar[spriteid].mtype -1 // tells it to wait until player leaves sector before porting again
						getactor[spriteid].ang peractor4
						getactorvar[spriteid].monstflags temp
						ifvarand temp 1
						{
							state resetweaponstate
							state clearinventory
						}
						setvar spriteid 16383
					}
				}
			}
		}
		addvar spriteid 1
	}
	setvar targetlock 0
	
}

ifvarl sleeping 0
{
	ifvarand peractor5 2 
	{
		setplayer[THISACTOR].horiz 100
		setplayer[THISACTOR].ang peractor4
	}
}
ifvarg sleeping 0 
{
	ifvarand peractor5 2 setplayer[THISACTOR].horiz 100
	setplayer[THISACTOR].ang peractor4
}

switch peractor7
	case 0 case 4
	setplayer[THISACTOR].pals 0 0
	setplayer[THISACTOR].pals 1 0
	setplayer[THISACTOR].pals 2 0
	break
	
	case 1
	setplayer[THISACTOR].pals 0 0
	setplayer[THISACTOR].pals 1 0
	setplayer[THISACTOR].pals 2 42
	break
	
	case 2
	setplayer[THISACTOR].pals 0 42
	setplayer[THISACTOR].pals 1 0
	setplayer[THISACTOR].pals 2 0
	break
	
	case 3
	case 13
	setplayer[THISACTOR].pals 0 63
	setplayer[THISACTOR].pals 1 63
	setplayer[THISACTOR].pals 2 63
	break
	
	case 8
	setplayer[THISACTOR].pals 0 0
	setplayer[THISACTOR].pals 1 42
	setplayer[THISACTOR].pals 2 0
	break
	
	default
	setplayer[THISACTOR].pals 0 0
	setplayer[THISACTOR].pals 1 0
	setplayer[THISACTOR].pals 2 0
	break
endswitch
	
	
setvarvar temp sleeping
ifvarl temp 0
mulvar temp -1
setvar tempb 130
subvarvar tempb temp
ifvarl tempb 0 setvar tempb 0
mulvar tempb 2 divvar tempb 3
ifvarg tempb 63 setvar tempb 63
setplayer[THISACTOR].pals_time tempb

ifvarg sleeping 130
{
	setvar sleeping 0
	setplayer[THISACTOR].movement_lock 0
	setvar peractor7 0
}


ends

state normaldev
setvar devshotty 1
setvar WEAPON7_SHOTSPERBURST 2
setvar WEAPON7_SHOOTS 2605
setvar WEAPON7_FIRESOUND RPG_SHOOT
setvar WEAPON7_FIREDELAY 3
setvar WEAPON7_TOTALTIME 5
setvar WEAPON7_FLAGS 72
setvar WEAPON7_SPAWNTIME 0
setvar WEAPON7_SPAWN 0
setvar WEAPON7_INITIALSOUND CAT_FIRE
ends

state bfgdev
setvar WEAPON7_SHOOTS POWBEAM
setvar WEAPON7_TOTALTIME 16
setvar WEAPON7_SHOTSPERBURST 0
setvar WEAPON7_FLAGS 0
setvar WEAPON7_FIRESOUND IMP_SHOOT
ifvarand gunparts 4 nullop else
setplayer[THISACTOR].max_ammo_amount 7 20 //50
ends

state normalfreeze

setvar WEAPON9_SHOTSPERBURST 0
setvar WEAPON9_SHOOTS FREEZEBLAST
setvar WEAPON9_FIRESOUND CAT_FIRE
setvar WEAPON9_FIREDELAY 3
setvar WEAPON9_TOTALTIME 5
setvar WEAPON9_FLAGS 65536
setvar WEAPON9_SPAWNTIME 0
setvar WEAPON9_SPAWN 0
setvar WEAPON9_INITIALSOUND CAT_FIRE

ends

state toggledukemon
quote 1059
sound SWITCH_ON
ifvare dukemon YES
{
	setvar dukemon NO
	setvar WEAPON8_WORKSLIKE 8
	setvar WEAPON8_FLAGS 3072
	setvar WEAPON8_RELOAD 16
	setvar WEAPON8_FIREDELAY 3
	setvar WEAPON8_TOTALTIME 16
	setvar WEAPON8_HOLDDELAY 7
	setvar WEAPON8_SHOOTS HANDHOLDINGLASER
}
else
{
	setvar dukemon YES
	setvar WEAPON8_WORKSLIKE 5
	setvar WEAPON8_FLAGS 512
	setvar WEAPON8_RELOAD 30
	setvar WEAPON8_FIREDELAY 6
	setvar WEAPON8_TOTALTIME 19
	setvar WEAPON8_HOLDDELAY 12
	setvar WEAPON8_SHOOTS HEAVYHBOMB
	setplayer[THISACTOR].gotweapon 8 YES
	getplayer[THISACTOR].ammo_amount 8 temp
	ifvarl temp 3 setplayer[THISACTOR].ammo_amount 8 3
}
savegamevar dukemon
ends

state togglefreezer

sound SWITCH_ON
ifvare altfreeze NO
{
	setvar altfreeze YES
	setvar WEAPON9_SHOOTS ICEBEAM
	setvar WEAPON9_FIRESOUND ICEBEAMSND
	setvar WEAPON9_INITIALSOUND -1
	setvar WEAPON9_TOTALTIME 10
	setvar WEAPON9_SHOTSPERBURST 1
}
else
{
	setvar altfreeze NO
	setvar WEAPON9_SHOOTS FREEZEBLAST
	setvar WEAPON9_FIRESOUND CAT_FIRE
	setvar WEAPON9_INITIALSOUND CAT_FIRE
	setvar WEAPON9_TOTALTIME 5
	setvar WEAPON9_SHOTSPERBURST 0
}

savegamevar altfreeze

ends

state togglegravgun
sound SWITCH_ON
ifvare gravgun YES
{
	setvar gravgun NO
	setvar WEAPON11_SHOOTS GROWSPARK
	setvar WEAPON11_FIRESOUND EXPANDERSHOOT
	setvar WEAPON11_TOTALTIME 5
}
else
{
	setvar gravgun YES
	setvar WEAPON11_SHOOTS GRAVPROJ2
	setvar WEAPON11_FIRESOUND GRAVSHOOTSND
	setvar WEAPON11_TOTALTIME 20
}
savegamevar gravgun
ends

state togglerpg
sound SWITCH_ON
ifvare usebigrox YES
{
	setvar usebigrox NO
	setvar WEAPON4_CLIP 0
	setvar WEAPON4_FLAGS 0
	setvar WEAPON4_RELOAD 0
}
else
{
	setvar usebigrox YES
	setvar WEAPON4_CLIP 2
	setvar WEAPON4_FLAGS 1024
	setvar WEAPON4_RELOAD 65 // 78 
	setvar WEAPON4_RELOADSOUND1 RPGOPEN
	setvar WEAPON4_RELOADSOUND2 RPGCLOSE
}
savegamevar usebigrox
ends

state togglechaingun
sound SWITCH_ON
ifvare usemp5 YES
{
	setvar usemp5 NO
	ifvarand ALTCHAINGUN 2 nullop else
	{
		setvar WEAPON3_SHOOTS CHAINGUN
		setvar WEAPON3_TOTALTIME 12
		ifvare dpsounds YES
		setvar WEAPON3_FIRESOUND DPCHAINGUNFIRE else
		setvar WEAPON3_FIRESOUND CHAINGUN_FIRE
		setvar WEAPON3_FLAGS 73812
		setvar WEAPON3_CLIP 0
		setvar WEAPON3_SPAWNTIME 0
		setvar WEAPON3_RELOADSOUND1 4
		setvar WEAPON3_RELOADSOUND2 5
	}
}
else
{
	setvar usemp5 YES
	ifvarand ALTCHAINGUN 2 nullop else
	{
		setvar WEAPON3_SHOOTS MP5BULLET
		setvar WEAPON3_TOTALTIME 4
		setvar WEAPON3_FIRESOUND MP5FIRE
		setvar WEAPON3_FLAGS 8196
		setvar WEAPON3_CLIP 30
		setvar WEAPON3_SPAWNTIME 2
		setvar WEAPON3_RELOADSOUND1 -1
		setvar WEAPON3_RELOADSOUND2 -1
	}
}
savegamevar usemp5
ends

state toggleshotgun
sound SWITCH_ON
ifvare usemossberg YES
{
	setvar usemossberg NO
	ifvarand ALTSHOTGUN 16 setvar ALTSHOTGUN 16
	else setvar ALTSHOTGUN 0
	setvar WEAPON2_FIREDELAY 4
	setvar WEAPON2_TOTALTIME 30
	setvar WEAPON2_SPAWN SHOTGUNSHELL
	setvar WEAPON2_SHOTSPERBURST 7
	setvar WEAPON2_FIRESOUND SHOTGUN_FIRE
	setvar WEAPON2_SOUND2TIME 15
	setvar WEAPON2_SOUND2SOUND SHOTGUN_COCK
}
else
{
	setvar usemossberg YES
	ifvarand ALTSHOTGUN 16 { subvar ALTSHOTGUN 16 orvar ALTSHOTGUN 1 }
	setvar WEAPON2_FIREDELAY 2
	setvar WEAPON2_TOTALTIME 26
	setvar WEAPON2_SHOTSPERBURST 8
	setvar WEAPON2_FIRESOUND MOSSFIRE
	setvar WEAPON2_SOUND2TIME 13
	setvar WEAPON2_SOUND2SOUND -1
}
savegamevar usemossberg
ends

state toggleakimbo
sound SWITCH_ON
ifvare useakimbo YES 
{
	setvar akimbo 1
	state changeakimbo
	setvar useakimbo NO 
	setvar akimbo -1
}
else setvar useakimbo YES
savegamevar useakimbo
ends

state swapwall
ifvarvarn J K {
getwall[J].x temp
getwall[K].x tempb
setwall[J].x tempb
setwall[K].x temp
getwall[J].y temp
getwall[K].y tempb
setwall[J].y tempb
setwall[K].y temp
getwall[J].cstat temp
getwall[K].cstat tempb
setwall[J].cstat tempb
setwall[K].cstat temp
getwall[J].picnum temp
getwall[K].picnum tempb
setwall[J].picnum tempb
setwall[K].picnum temp
getwall[J].overpicnum temp
getwall[K].overpicnum tempb
setwall[J].overpicnum tempb
setwall[K].overpicnum temp
getwall[J].shade temp
getwall[K].shade tempb
setwall[J].shade tempb
setwall[K].shade temp
getwall[J].pal temp
getwall[K].pal tempb
setwall[J].pal tempb
setwall[K].pal temp
getwall[J].xrepeat temp
getwall[K].xrepeat tempb
setwall[J].xrepeat tempb
setwall[K].xrepeat temp
getwall[J].yrepeat temp
getwall[K].yrepeat tempb
setwall[J].yrepeat tempb
setwall[K].yrepeat temp
getwall[J].xpanning temp
getwall[K].xpanning tempb
setwall[J].xpanning tempb
setwall[K].xpanning temp
getwall[J].ypanning temp
getwall[K].ypanning tempb
setwall[J].ypanning tempb
setwall[K].ypanning temp
getwall[J].lotag temp
getwall[K].lotag tempb
setwall[J].lotag tempb
setwall[K].lotag temp
getwall[J].hitag temp
getwall[K].hitag tempb
setwall[J].hitag tempb
setwall[K].hitag temp
getwall[J].extra temp
getwall[K].extra tempb
setwall[J].extra tempb
setwall[K].extra temp
getwall[J].nextwall temp
getwall[K].nextwall tempb
setwall[J].nextwall tempb
setwall[K].nextwall temp
getwall[J].nextsector temp
getwall[K].nextsector tempb
setwall[J].nextsector tempb
setwall[K].nextsector temp
}
ends

state mirror

setvar I 0
whilevarn I 16384
{
    getactor[I].x A
    mulvar A -1
    setactor[I].x A
    getactor[I].picnum picnum
    getactor[I].lotag temp
    	
    getactor[I].ang A
    ifvarg A 1024
    {
        setvar B 1536
    } else {
        setvar B 512
    }
    subvarvar B A
    mulvar B 2
    addvarvar A B
    
    setactor[I].ang A
    
    getactor[I].lotag temp
    ifvare picnum SECTOREFFECTOR ifvare temp 0
	{
	    getactor[I].pal temp
	    ifvare temp 2 setactor[I].pal 0 else
	    ifvare temp 0 setactor[I].pal 2
    }
    ifvare picnum SECTOREFFECTOR ifvare temp 11
    {
        ifvarg A 0 ifvarl A 1025 subvar A 1 else
        ifvarg A 1024 ifvarl A 2047 addvar A 1
        addvar A 1024
        ifvarg A 2047 subvar A 2048
        setactor[I].ang A
    }
    addvar I 1
}

setvar I 0
whilevarvarn I NUMWALLS
{
    getwall[I].x A
    mulvar A -1
    setwall[I].x A
    setarray SWAPWALLS[I] -1
    getwall[I].picnum temp
    switch temp
    case 277 case 394 case 415 case 498 case 499
    case 500 case 502 case 518 case 519 case 520 case 521 case 522 case 523 case 524 case 525
    case 526 case 527 case 528 case 529 case 530 case 531 case 532 case 533 case 534 case 535
    case 568 case 593 case 594 case 598 case 599 case 600 case 612 case 613 case 614 case 639
    case 657 case 666 case 667 case 687 case 699 case 721 case 728 case 736 case 766 case 776 
    case 777 case 778 case 784 case 817 case 818 case 822 case 826 case 835 case 838 case 837
    case 841 case 839 case 848 case 849 case 856 case 858 case 871 case 872 case 937 case 938 
    case 945 case 946 case 949 case 961 case 963 case 964 case 965 case 966 case 967 case 968
    case 970 case 980 case 992 case 1043 case 1044 case 1046 case 1047 case 1064 case 1065 case 1066
    case 1072 case 1210 case 1211 case 1212 case 1214 case 1215 case 1217 case 1244 case 1245
    case 2486 case 2492 case 2493 case 2500 case 2504 case 2505 case 2506 case 3370 case 3371 case 3372
    case 3373 case 3374 case 3382 case 3383 case 3391 case 3402 case 3403 case 3421 case 3422 case 3423
    case 4096 case 4108 case 4125 case 4126 case 4238 case 4306 case 4307 case 4318 case 4323
    case 4329 case 4447 case 4450 case 4451 case 4477 case 4472 case 4475 case 4476
    case 4478 case 4479 case 4484 case 4485 case 4486 case 4487 case 4488 case 4492 case 4499
    case 4494 case 4504 case 4505 case 4506 case 4507 case 4508 case 4553 case 4564 case 4565
    case 4578 case 4579 case 4597 case 4598 case 4599 case 4889 case 4891 case 4892 case 4896 
    case 4897 case 4898 case 4899 case 4903 case 4904 case 4905 case 4906 case 4907 case 4908
    case 4924 case 4925 case 4926 case 4927 case 4928 case 4929 case 4930 case 4931 case 4932
    case 4933 case 4934 case 4937 case 4938 case 4939 case 4942 case 4945 case 4947 case 4948
    case 4950 case 4951 case 4952 case 4956
    break
    default
        getwall[I].cstat temp
        ifvarand temp 8 subvar temp 8 else orvar temp 8
        setwall[I].cstat temp
    break
    endswitch
    addvar I 1
}

setvar I 0
whilevarvarn I NUMWALLS
{
    setvarvar A I
    getwall[I].point2 B
    setvarvar K I
    whilevarvarn A B
    {
        addvar K 1
        getwall[K].point2 B
    }
    setvarvar A K
    setvarvar J I
    setvarvar L K
    subvarvar L J
    divvar L 2
    addvar J 1
    whilevarn L 0
    {
        state swapwall
        setarray SWAPWALLS[J] K
        setarray SWAPWALLS[K] J
        addvar J 1
        addvar K -1
        addvar L -1
    }

    setvarvar J I
    whilevarvarn I A
    {
        setvarvar K I
        addvar K 1
        getwall[J].x B
        getwall[J].y C
        getwall[K].x D
        getwall[K].y E
        setwall[J].x D
        setwall[J].y E
        setwall[K].x B
        setwall[K].y C

        addvar I 1
    }

    addvar I 1
}

setvar I 0
whilevarvarn I NUMWALLS
{
    getwall[I].nextwall A
    ifvarn A -1
        ifvarn SWAPWALLS[A] -1
            setwall[I].nextwall SWAPWALLS[A]

    addvar I 1
}

ends

state superkickcode

	ifvarn player[THISACTOR].somethingonplayer -1
	{
		orvar bits 4
		setinput[THISACTOR].bits bits
	}
	ifvare superkicking 1 { getplayer[THISACTOR].posz initz ifp pducking nullop else addvar initz 1536 }
	getplayer[THISACTOR].posz z
	ifvarvarl z initz
	setplayer[THISACTOR].posz initz
	
	ifvare spinkick 1 ifvare superkickdone 0
	{
		getplayer[THISACTOR].oang angvar
		addvar angvar 232
		setplayer[THISACTOR].ang angvar
	}
	
	ifvare superkickdone 1 ifvarl superkicking 4 nullop else
	{
		getplayer[THISACTOR].rotscrnang temp
		subvar temp 36
		setplayer[THISACTOR].rotscrnang temp
	}
	ifvare superkickdone 0 
	{
		addvar superkicking 1
		ifvarg superkicking 6 ifvare spinkick 1 { shoot KNEE shoot KNEE }
	    
	    else ifvarg superkicking 7 ifvarl superkicking 10 shoot KNEE
		ifvare superkicking 7 ifvare kickimpact 0 sound KICKOUT
	}
	else 
	{
		subvar superkicking 1
		ifvare superkicking 0 setvar kickimpact 0
	}
	ifvare superkicking 0 { setvar superkickdone 0 setvar spinkick 0 setvar kicktarg -1 }
	ifvarg superkicking 9 setvar superkickdone 1

ends

state optiontogglestate
setvarvar lastinput timer
switch menuitem
case 0
setvar menutoggle 2 // weapons sub-menu
setvar menuitem 9
sound INTO_MENU
break
case 1
setvar menutoggle 3 // abilities sub-menu
setvar menuitem 30
sound INTO_MENU
break
case 2
setvar menutoggle 4 // monsters sub-menu
setvar menuitem 50
sound INTO_MENU
break
case 3
setvar menutoggle 5 // effects sub-menu
setvar menuitem 70
sound INTO_MENU
break
case 4
setvar menutoggle 6 // weather sub-menu
setvar menuitem 90
sound INTO_MENU
break

case 5
setvar menutoggle 7 // DUKEBOTS
setvar menuitem 110
sound INTO_MENU
break

case 6 // restore DUKE PLUS defaults
sound SWITCH_ON
quote 1071
setvar gamespeed 120
setvar gamespeed_display 100
setvar effects DAMAGE_EFFECTS
setvar morefx 31
setvar viewmode 1
setvar dpsounds YES

ifvare startweapons -1
{
	setvar karate YES
	ifvare useakimbo NO state toggleakimbo
	ifvare usemossberg NO state toggleshotgun
	ifvare usemp5 NO state togglechaingun
	ifvare usebigrox NO state togglerpg
	setvar pipethrow YES
	setvar usemindblast YES
	ifvare gravgun NO state togglegravgun
	setvar usebfg YES
	setvar dukemon NO
	ifvare altfreeze NO state togglefreezer
	ifvare depistols YES state changepistols
}
else { quote 1060 soundonce LASERTRIP_ARMING }

setvar numbots 0
setvar doubled NO
setvar scaled NO
setvar mirrored NO
setvar knockdist KNOCKBACK
ifvare playermoves -1
{
	setvar cangrab YES
	setvar candodge YES
	setvar canmantle YES
	setvar slowrunaccel YES
	setvar moretalk YES
}

setvar lifebars YES
setvar infighting 10
setvar killerpigs 20
setvar maxmedkit FIRSTAID_MAX

ifvare startinventory -1
{
	setvar goodarmor YES
	setvar storehealth STOREHEALTH
	setvar steroidkicks STEROIDKICKS
	setvar holodukebot HOLODUKEBOT
	setvar goggleswitches GOGGLESWITCHES
	setvar newjetpack NO
}
else { quote 1060 soundonce LASERTRIP_ARMING }

setvar randomize 0
setvar rainradius 32768
setvar pdamage 100
setvar mdamage 100
setvar raining 0
setvar quotestyle 0
setvar smartmonsters YES
setvar uselaser YES
savegamevar effects
savegamevar morefx
savegamevar viewmode
savegamevar dpsounds
ifvare startweapons -1
{
	savegamevar karate
	savegamevar useakimbo
	savegamevar usemossberg
	savegamevar usemp5
	savegamevar usebigrox
	savegamevar pipethrow
	savegamevar usemindblast
	savegamevar gravgun
	savegamevar usebfg
	savegamevar dukemon
	savegamevar altfreeze
	savegamevar depistols
}

savegamevar numbots
savegamevar doubled
savegamevar scaled
savegamevar mirrored
savegamevar knockdist
ifvare playermoves -1
{
	savegamevar cangrab
	savegamevar candodge
	savegamevar canmantle
	savegamevar slowrunaccel
	savegamevar moretalk
}
savegamevar lifebars
savegamevar maxmedkit

ifvare startinventory -1
{
	savegamevar goodarmor
	savegamevar storehealth
	savegamevar steroidkicks
	savegamevar holodukebot
	savegamevar goggleswitches
	savegamevar newjetpack
}

savegamevar infighting
savegamevar killerpigs

savegamevar randomize
savegamevar rainradius
//savegamevar raining
savegamevar pdamage
savegamevar mdamage
savegamevar quotestyle
savegamevar smartmonsters
savegamevar uselaser

break

case 7 // restore classic defaults

sound SWITCH_ON
quote 1072
setvar gamespeed 120
setvar gamespeed_display 100
setvar effects NO
setvar morefx NO
setvar viewmode NO
setvar dpsounds NO
ifvare startweapons -1
{
	setvar karate NO
	ifvare useakimbo YES state toggleakimbo
	ifvare usemossberg YES state toggleshotgun
	ifvare usemp5 YES state togglechaingun
	ifvare usebigrox YES state togglerpg
	setvar pipethrow NO
	setvar usemindblast NO
	ifvare gravgun YES state togglegravgun
	ifvarn usebfg 0 { state normaldev setvar usebfg 0 }
	ifvare dukemon YES state toggledukemon
	ifvare altfreeze YES state togglefreezer
	ifvare depistols YES state changepistols
}
else { quote 1060 soundonce LASERTRIP_ARMING }
setvar numbots 0
setvar doubled NO
setvar scaled NO
setvar mirrored NO
setvar knockdist 0
ifvare playermoves -1
{
	setvar cangrab NO
	setvar candodge NO
	setvar canmantle NO
	setvar slowrunaccel NO
	setvar moretalk NO
}
ifvare startinventory -1
{
	setvar storehealth NO
	setvar steroidkicks NO
	setvar holodukebot NO
	setvar goggleswitches NO
	setvar newjetpack NO
	setvar goodarmor NO
}
else { quote 1060 soundonce LASERTRIP_ARMING }
setvar lifebars NO
setvar infighting NO
setvar killerpigs NO
	
setvar maxmedkit 100
	
setvar randomize NO
setvar rainradius 32768
setvar pdamage 100
setvar mdamage 100
setvar raining 0
setvar quotestyle 1
setvar smartmonsters NO
setvar uselaser NO
savegamevar effects
savegamevar morefx
savegamevar viewmode
savegamevar dpsounds
ifvare startweapons -1
{
	savegamevar karate
	savegamevar useakimbo
	savegamevar usemossberg
	savegamevar usemp5
	savegamevar usebigrox
	savegamevar pipethrow
	savegamevar usemindblast
	savegamevar gravgun
	savegamevar usebfg
	savegamevar dukemon
	savegamevar altfreeze
	savegamevar depistols
}
savegamevar numbots
savegamevar doubled
savegamevar scaled
savegamevar mirrored
savegamevar knockdist
ifvare playermoves -1
{
	savegamevar cangrab
	savegamevar candodge
	savegamevar canmantle
	savegamevar slowrunaccel
	savegamevar moretalk
}
ifvare startinventory -1
{
	savegamevar goodarmor
	savegamevar storehealth
	savegamevar steroidkicks
	savegamevar holodukebot
	savegamevar goggleswitches
	savegamevar newjetpack
}
savegamevar lifebars
savegamevar infighting
savegamevar killerpigs
savegamevar maxmedkit
savegamevar randomize
savegamevar rainradius
//savegamevar raining
savegamevar pdamage
savegamevar mdamage
savegamevar quotestyle
savegamevar smartmonsters
savegamevar uselaser

break

case 8
setvar menutoggle 0 // exit menu
setvar menuitem 0
setplayer[THISACTOR].movement_lock 0
sound EXITMENUSOUND
break
case 22
case 41
case 59
case 83
case 95
case 115
setvar menutoggle 1 // main menu
setvar menuitem 8
sound EXITMENUSOUND
break

// various game options

case 9 // toggle karate kicks
ifvare startweapons -1
{
	sound SWITCH_ON
	ifvare karate NO setvar karate YES else setvar karate NO
	savegamevar karate
}
else { quote 1060 soundonce LASERTRIP_ARMING }
break

case 10 // toggle akimbo
ifvare startweapons -1
state toggleakimbo
else { quote 1060 soundonce LASERTRIP_ARMING }
break

case 11 // toggle de pistols
state changepistols
savegamevar depistols
sound SWITCH_ON
break

case 12 // toggle shotgun type
ifvare startweapons -1
state toggleshotgun
else { quote 1060 soundonce LASERTRIP_ARMING }
break

case 13 // toggle chaingun type
ifvare startweapons -1
state togglechaingun
else { quote 1060 soundonce LASERTRIP_ARMING }
break

case 14 // toggle rpg type
ifvare startweapons -1
state togglerpg
else { quote 1060 soundonce LASERTRIP_ARMING }
break

case 15 // toggle better pipebomb throws
sound SWITCH_ON
ifvare pipethrow YES setvar pipethrow NO else setvar pipethrow YES
savegamevar pipethrow
break

case 16 // toggle mind blast
ifvare startweapons -1
{
	ifvare usemindblast YES { setvar usemindblast NO setvar altshrinker NO } else setvar usemindblast YES
	savegamevar usemindblast
	sound SWITCH_ON
}
else { quote 1060 soundonce LASERTRIP_ARMING }
break

case 17 // toggle gravity gun
ifvare startweapons -1
state togglegravgun
else { quote 1060 soundonce LASERTRIP_ARMING }
break

case 18 // toggle shotty devastator
ifvare startweapons -1
{
	sound SWITCH_ON
	ifvarn usebfg 0 
	{ 
		state normaldev
		setvar usebfg 0
	} 
	else 
	{
		state bfgdev
		setvar usebfg YES
	}
	savegamevar usebfg
}
else { quote 1060 soundonce LASERTRIP_ARMING }
break


case 19 // toggle dukemon
ifvare startweapons -1
state toggledukemon
else { quote 1060 soundonce LASERTRIP_ARMING }
break

case 20 // toggle Freezer Plus
ifvare startweapons -1
state togglefreezer
else { quote 1060 soundonce LASERTRIP_ARMING }
break

case 30 // toggle realistic running
ifvare playermoves -1
{
	sound SWITCH_ON
	ifvare slowrunaccel YES setvar slowrunaccel NO else setvar slowrunaccel YES
	savegamevar slowrunaccel
} else { quote 1060 soundonce LASERTRIP_ARMING }
break

case 31 // toggle mantling
ifvare playermoves -1
{
	sound SWITCH_ON
	ifvare canmantle YES setvar canmantle NO else setvar canmantle YES
	savegamevar canmantle
} else { quote 1060 soundonce LASERTRIP_ARMING }
break

case 32 // toggle dodging
ifvare playermoves -1
{
	sound SWITCH_ON
	ifvare candodge YES setvar candodge NO else setvar candodge YES
	savegamevar candodge
} else { quote 1060 soundonce LASERTRIP_ARMING }
break

case 33 // toggle grabbing
ifvare playermoves -1
{
	sound SWITCH_ON
	ifvare cangrab YES setvar cangrab NO else setvar cangrab YES
	savegamevar cangrab
} else { quote 1060 soundonce LASERTRIP_ARMING }
break

case 34 // toggle health storage
ifvare startinventory -1
{
	sound SWITCH_ON
	ifvare storehealth YES setvar storehealth NO else setvar storehealth YES
	savegamevar storehealth
}
else { quote 1060 soundonce LASERTRIP_ARMING }
break

// case 25 medkit max is controlled by strafing

case 36 // steroid enhanced kicks
ifvare startinventory -1
{
	sound SWITCH_ON
	ifvare steroidkicks YES setvar steroidkicks NO else setvar steroidkicks YES
	savegamevar steroidkicks
}
else { quote 1060 soundonce LASERTRIP_ARMING }
break

case 37 // holoduke bot
ifvare startinventory -1
{
	sound SWITCH_ON
	ifvare holodukebot YES setvar holodukebot NO else setvar holodukebot YES
	savegamevar holodukebot
}
else { quote 1060 soundonce LASERTRIP_ARMING }
break

case 38 // goggles reveal dipswitch patterns
ifvare startinventory -1
{
	sound SWITCH_ON
	ifvare goggleswitches YES setvar goggleswitches NO else setvar goggleswitches YES
	savegamevar goggleswitches
}
else { quote 1060 soundonce LASERTRIP_ARMING }
break

case 39 // better armor
ifvare startinventory -1
{
	sound SWITCH_ON
	ifvare goodarmor YES setvar goodarmor NO else setvar goodarmor YES
	savegamevar goodarmor
}
else { quote 1060 soundonce LASERTRIP_ARMING }
break

case 40 // new jetpack control
ifvare startinventory -1
{
	sound SWITCH_ON
	ifvare newjetpack YES setvar newjetpack NO else setvar newjetpack YES
	savegamevar newjetpack
}
else { quote 1060 soundonce LASERTRIP_ARMING }
break

case 50 // monster life bars
sound SWITCH_ON
quote 1059
ifvare lifebars YES setvar lifebars NO else setvar lifebars YES
savegamevar lifebars
break

// case 31 monster random attributes controlled by strafing

case 55 // toggle pal 21 troops dropping lasers
sound SWITCH_ON
ifvare uselaser YES setvar uselaser NO else setvar uselaser YES
savegamevar uselaser
break

case 56 // double monsters
sound SWITCH_ON
ifvare doubled YES setvar doubled NO else setvar doubled YES
quote 1059
savegamevar doubled
break

case 58
sound SWITCH_ON
ifvare smartmonsters YES setvar smartmonsters NO else setvar smartmonsters YES
savegamevar smartmonsters
break

case 70 // toggle damage effects
sound SWITCH_ON
ifvare effects YES setvar effects NO else setvar effects YES
savegamevar effects
break

case 71 // toggle map mirroring
sound SWITCH_ON
ifvare mirrored NO
setvar mirrored YES else setvar mirrored NO
quote 1059
savegamevar mirrored
break

case 72 // toggle double sized maps
sound SWITCH_ON
ifvare scaled NO
setvar scaled YES else setvar scaled NO
quote 1059
savegamevar scaled
break

case 73 // toggle Duke Plus sounds
sound SWITCH_ON
ifvare dpsounds NO
{
	setvar dpsounds YES 
	setvar WEAPON1_FIRESOUND DPPISTOLFIRE
	ifvare usemp5 NO
	setvar WEAPON3_FIRESOUND DPCHAINGUNFIRE
}
else 
{
	setvar dpsounds NO
	setvar WEAPON1_FIRESOUND PISTOL_FIRE
	ifvare usemp5 NO
	setvar WEAPON3_FIRESOUND CHAINGUN_FIRE
}
savegamevar dpsounds
break

case 75
// hi-res blood
sound SWITCH_ON
ifvarand morefx 1 xorvar morefx 1 else orvar morefx 1
savegamevar morefx
break

case 76
// water splashes
sound SWITCH_ON
ifvarand morefx 2 xorvar morefx 2 else orvar morefx 2
savegamevar morefx
break

case 77
// pigcop jibs
sound SWITCH_ON
ifvarand morefx 4 xorvar morefx 4 else orvar morefx 4
savegamevar morefx
break

case 78
// smoke
sound SWITCH_ON
ifvarand morefx 8 xorvar morefx 8 else orvar morefx 8
savegamevar morefx
break

case 79
// enemy muzzle flashes
sound SWITCH_ON
ifvarand morefx 16 subvar morefx 16 else orvar morefx 16
savegamevar morefx
break

case 80
// F7 mode
sound SWITCH_ON
ifvare viewmode 0 setvar viewmode 1 else setvar viewmode 0
savegamevar viewmode
break

case 81
// extra duke talk
ifvarn playermoves -1 ifvare moretalk NO
{
	quote 1060 
	soundonce LASERTRIP_ARMING	
}
else
{
	sound SWITCH_ON
	ifvare moretalk NO setvar moretalk YES else setvar moretalk NO
	savegamevar moretalk
}
break

case 82
// disable character models
sound SWITCH_ON
ifvarand morefx 32 xorvar morefx 32 else
orvar morefx 32
savegamevar morefx
quote 1059
break

case 90 // toggle weather type
ifvare raining 0 setvar raining -1
else
	mulvar raining -1
sound SWITCH_ON
//savegamevar raining
break

case 111 // bot respawns
sound SWITCH_ON
ifvarand usebots 1 xorvar usebots 1 else orvar usebots 1
savegamevar usebots
break

case 112 // bot use pickups
// sound SWITCH_ON
// ifvarand usebots 2 xorvar usebots 2 else orvar usebots 2
// savegamevar usebots
break

case 113 // show bot lifebars
sound SWITCH_ON
ifvarand usebots 4 xorvar usebots 4 else orvar usebots 4
savegamevar usebots
quote 1059
break

case 114 // bot medics
sound SWITCH_ON
ifvarand usebots 8 xorvar usebots 8 else orvar usebots 8
savegamevar usebots
break
default break
endswitch
ends

// state lightstarter


// state clear_light_array
// state fill_light_array
// ifvare lighting YES
// {
// 	state fill_wall_arrays
// 	ifvare lightinmap 0
// 	{
// 		setvarvar temp NUMSECTORS
// 		subvar temp 1
// 		whilevarn temp -1
// 		{
// 			state spawn_controller
// 			subvar temp 1
// 		}
// 	}
// 	setvar lightinmap 0

// 	// make wall aligned sprites get the ID of their wall, so they can check their wall later on
// 	setvar spriteid 0
// 	whilevarn spriteid 16384
// 	{
// 		ifvarn sprite[spriteid].statnum 1024
// 		{
// 			// ifvarand sprite[spriteid].cstat 16
// 			getactor[spriteid].cstat temp
// 			ifvarand temp 16
// 			{
// 				getactor[spriteid].pal pal
// 				ifvarl pal 70
// 				state wallhitscan
// 			} else
// 			// ifvarand sprite[spriteid].cstat 32
// 			ifvarand temp 32
// 			{
// 				ifvarl sprite[spriteid].pal 70
// 				{
// 					setactorvar[spriteid].mywall 32768 // dummy value
// 					getactor[spriteid].sectnum hitsector
// 					ifvarn hitsector -1 ifvarl hitsector 4096
// 					{
// 						getsector[hitsector].floorshade shade
// 						setactorvar[spriteid].ammo shade
// 						getactor[spriteid].shade shade
// 						setactorvar[spriteid].initshade shade
// 					}
// 				}
// 			}
// 		}
// 		addvar spriteid 1
// 	}
// }

// ends

state playertargcheck
getactor[THISACTOR].z z
subvar z 8192
setactor[THISACTOR].z z
canseespr THISACTOR playertarg temp
addvar z 8192
setactor[THISACTOR].z z
ifvare temp 0 setvar playertarg -1
ends

state pview2
getplayer[THISACTOR].i spriteid
getactor[spriteid].z digz
subvar digz 8192
setactor[spriteid].z digz
canseespr temp spriteid tempd
addvar digz 8192
setactor[spriteid].z digz
ends

state spawnviewcam
getplayer[THISACTOR].posx digx
getplayer[THISACTOR].posy digy
getplayer[THISACTOR].ang angvar
addvar angvar 1024
setvarvar mx digx
addvar mx 768
rotatepoint digx digy mx digy angvar digx digy
getplayer[THISACTOR].posz digz
updatesectorz digx digy digz temp
getplayer[THISACTOR].cursectnum mysector
ifvarvarn temp mysector
{
	getplayer[THISACTOR].posx digx
	getplayer[THISACTOR].posy digy
	updatesectorz digx digy digz temp
}

ifvarn temp -1
{
	espawn FOLLOWCAM
	setactor[RETURN].x digx
	setactor[RETURN].y digy
	setactor[RETURN].z digz
	setactor[RETURN].xrepeat 64
	setactor[RETURN].yrepeat 64
	setactor[RETURN].cstat 32768
}
ends

state kludges

ifvare moretalk YES
ifvarl activemonsters 0 // meaning that a monster was recently killed
{
	addvar activemonsters 1
	ifvare activemonsters 0 
	{
		ifvarg kickasscount 3
		{
			getplayer[THISACTOR].player_par temp
			subvarvar temp lastspeechtime
			ifvarg temp 1560
			{
				ifphealthl 50 
				{
					randvar temp 3
					ifvare temp 0
					globalsound PAYBKSND else
					ifvare temp 1
					globalsound HADIT else
					ifvare temp 2
					globalsound THATWASFUN else
					ifvare temp 3
					globalsound BRINGITON
				}
				else
				{
					randvar temp 4
					ifvare temp 0
					globalsound ICAMEISAW else
					ifvare temp 1
					globalsound THATWASFUN else
					ifvare temp 2
					globalsound ALLUGOT else
					ifvare temp 3
					globalsound BRINGITON else
					ifvare temp 4
					globalsound ILOOKGOOD
				}
				setvar kickasscount 0
				getplayer[THISACTOR].player_par lastspeechtime
			}
		}
		else subvar kickasscount 1
	}
	
}
ifvarg tauntcount 0
{
	subvar tauntcount 1
	ifvare tauntcount 0 globalsoundvar taunt
}

ifvare timer 1
{
	setvar spriteid 0
	whilevarn spriteid 16384
	{
		ifvarn sprite[spriteid].statnum 1024
		{
			getactor[spriteid].picnum picnum
			switch picnum
			case LIZTROOP
			case LIZTROOPDUCKING
			case LIZTROOPJETPACK
			case LIZTROOPSHOOT
			case LIZTROOPONTOILET
			case LIZTROOPJUSTSIT
			case LIZTROOPRUNNING
			case OCTABRAIN  
			case OCTABRAINSTAYPUT
			case DRONE  
			case COMMANDER  
			case COMMANDERSTAYPUT
			case TANK  
			case PIGCOP  
			case PIGCOPDIVE  
			case PIGCOPSTAYPUT
			case LIZMAN  
			case LIZMANSTAYPUT
			case LIZMANSPITTING
			case LIZMANJUMP
			case ROTATEGUN   
			case BOSS1 
			case BOSS1STAYPUT 
			case BOSS2  
			case BOSS3  
			case NEWBEAST
			case NEWBEASTSTAYPUT
			case NEWBEASTHANG
			case NEWBEASTJUMP
			case BOSS4
			case BOSS4STAYPUT
			case EGG
			case RECONDP
			case SHARK
			getactorvar[spriteid].monstflags temp
			ifvarand temp 1
			{
				changespritestat spriteid 1
				setactor[spriteid].httimetosleep 768
			}
			break
			default break
			endswitch
		}
		addvar spriteid 1
	}
		
	ifvare rendmode 4 
	{ 
		setvar pal 26
		setvar tempd 2 // 514
		setvar intensity 4096 
		state spawnpointlight 
	}
	setvarvar raining rainstart
	setvar portalview 0
	setvar portalcam -1
	setvar perplayeronbike -1
	setvar triphack -1
	setvar rainstart 0
	setvar temp 99
	whilevarn temp -1
	{
		setarray ptrails[temp] -1
		subvar temp 1
	}
}

ifvare HUBEXITS YES
{
	ifvarand player[THISACTOR].gm 8
	{
		setvar temp 1
		setvar tempb 1
		whilevarvarn tempb LEVEL
		{
			mulvar temp 2
			addvar tempb 1
		}
		orvarvar completed temp
		savegamevar completed
		setvar nomenu 0
		startlevel VOLUME 0
		setuserdef[THISACTOR].display_bonus_screen YES
		state resetweaponstate
		setvar ONCAR 0
		setvar playermoves -1
		setplayer[THISACTOR].gotweapon 1 1
		setplayer[THISACTOR].gotweapon 2 0
		setplayer[THISACTOR].gotweapon 3 0
		setplayer[THISACTOR].gotweapon 4 0
		setplayer[THISACTOR].gotweapon 5 0
		setplayer[THISACTOR].gotweapon 6 0
		setplayer[THISACTOR].gotweapon 7 0
		setplayer[THISACTOR].gotweapon 8 0
		setplayer[THISACTOR].gotweapon 9 0
		setplayer[THISACTOR].gotweapon 11 0
		setplayer[THISACTOR].ammo_amount 1 48
		setplayer[THISACTOR].ammo_amount 2 0
		setplayer[THISACTOR].ammo_amount 3 0
		setplayer[THISACTOR].ammo_amount 4 0
		setplayer[THISACTOR].ammo_amount 5 0
		setplayer[THISACTOR].ammo_amount 6 0
		setplayer[THISACTOR].ammo_amount 7 0
		setplayer[THISACTOR].ammo_amount 8 0
		setplayer[THISACTOR].ammo_amount 9 0
		setplayer[THISACTOR].ammo_amount 11 0
		setplayer[THISACTOR].curr_weapon 0
		setactor[THISACTOR].extra MAXPLAYERHEALTH
		setplayer[THISACTOR].heat_amount 0
		setplayer[THISACTOR].firstaid_amount 0
		setplayer[THISACTOR].steroids_amount 0
		setplayer[THISACTOR].holoduke_amount 0
		setplayer[THISACTOR].jetpack_amount 0
		setplayer[THISACTOR].boot_amount 0
		setplayer[THISACTOR].scuba_amount 0
	}
}

ifvare player[THISACTOR].heat_on YES
{
	ifvare gogmsgs NO
	{
		setvar gogmsgs YES
		setvar spriteid 0
		whilevarn spriteid 16384
		{
			ifvarg sprite[spriteid].picnum 2821 ifvarl sprite[spriteid].picnum 3023
			ifvarg sprite[spriteid].cstat -1
			{
				setactorvar[spriteid].position 65536
				setactor[spriteid].mdflags 16
			}
			addvar spriteid 1
		}
	}
}
else
{
	ifvare gogmsgs YES
	{
		setvar gogmsgs NO
		setvar spriteid 0
		whilevarn spriteid 16384
		{
			ifvarg sprite[spriteid].picnum 2821 ifvarl sprite[spriteid].picnum 3023
			ifvarg sprite[spriteid].cstat -1
			{
				setactorvar[spriteid].position 0
				setactor[spriteid].mdflags 0
			}
			addvar spriteid 1
		}
	}
}

// ifvare rendmode 4 setvar startlights NO
// ifvare startlights YES
// {
// 	state lightstarter
// 	setvar startlights 0
// }


ifvarn jumptap 2
{
	ifvarand bits 1
	{
		ifvare jumptap 1 setvar jumptap 2
	}
	else
	{
		ifvare jumptap 0 setvar jumptap 1
	}
}


// alt jetpack option
ifvare newjetpack YES
{
	ifp ponground setvar jumptap 0
	ifvare player[THISACTOR].jumping_counter 0
	ifp palive
	ifvarand bits 1
	ifvare mantling NO
	ifvare ONCAR 0
	ifvarg player[THISACTOR].jetpack_amount 0
	ifvare player[THISACTOR].jetpack_on 0
	{
		setvar tempb 0
		ifvare player[THISACTOR].jumping_toggle 1 ifvare jumptap 2 setvar tempb 1
		ifvarg player[THISACTOR].falling_counter 0 setvar tempb 1
		ifvare tempb 1
		{
			ifp ponground setplayer[THISACTOR].jumping_toggle 0
			setplayer[THISACTOR].jumping_counter 0
			getplayer[THISACTOR].poszv zvel
			subvar zvel 384
			ifvarl zvel -10240 setvar zvel -10240
			setplayer[THISACTOR].poszv zvel
			getplayer[THISACTOR].falling_counter temp
			ifvarg temp 2 subvar temp 2
			setplayer[THISACTOR].falling_counter temp
			getplayer[THISACTOR].jetpack_amount temp
			subvar temp 1
			setplayer[THISACTOR].jetpack_amount temp
			action PJPHOUVER
			espawn SMALLSMOKE
			setactorvar[RETURN].peractor1 1
			addvar thrust 3
			ifvarg thrust 10 soundonce DUKE_JETPACK_IDLE
			ifvare thrust 3 soundonce DUKE_JETPACK_ON
			
		}
		else ifvarg thrust 0 
		{
			ifp ponground setvar thrust 0
			else
			subvar thrust 2 
			ifvarl thrust 0 setvar thrust 0 
			ifvare thrust 0 soundonce DUKE_JETPACK_OFF 
		}
		
	}
	else ifvarg thrust 0 
	{
		ifp ponground setvar thrust 0
		else
		subvar thrust 2
		ifvarl thrust 0 setvar thrust 0 
		ifvare thrust 0 soundonce DUKE_JETPACK_OFF 
	}
}

iffloordistl 16 ifvare slowrunaccel YES
{
	getplayer[THISACTOR].poszv zvel
	ifvarg zvel 0 { addvar zvel 256 setplayer[THISACTOR].poszv zvel }
}

ifvare scaled YES
{
	getplayer[THISACTOR].falling_counter temp
	ifvarg temp 0 ifrnd 128
	{
		subvar temp 1 
		setplayer[THISACTOR].falling_counter temp
	}
	ifvarand bits 1
	{
		getplayer[THISACTOR].jumping_counter temp
		ifvare canmantle YES { ifvare temp 901 setplayer[THISACTOR].jumping_counter 0 } else
		ifvare temp 1081 setplayer[THISACTOR].jumping_counter 0
	}
	getplayer[THISACTOR].airleft temp
	ifvarl temp 390 ifrnd 128 addvar temp 1
	setplayer[THISACTOR].airleft temp
	getplayer[THISACTOR].scuba_on temp
	ifvare temp 1
	{
		getplayer[THISACTOR].scuba_amount temp
		ifvarl temp 6400 ifrnd 128 addvar temp 1
		setplayer[THISACTOR].scuba_amount temp
	}
	
}

ifvarl totalclock 60 setuserdef[THISACTOR].const_visibility DEFAULTVISIBILITY

ifvarand morefx 2
{
	setvar temp 0
	ifonwater 
	{
      setvar RETURN -1
	  ifrnd 32 spawn WATERCIRCLE else
	  ifaction PRUN spawn WATERCIRCLE else
	  ifaction PRUNBACK spawn WATERCIRCLE else
	  ifaction PWALK spawn WATERCIRCLE else
	  ifaction PWALKBACK spawn WATERCIRCLE else
	  ifaction PAKICKING spawn WATERCIRCLE else
	  ifaction PCRAWLING spawn WATERCIRCLE
	}
}

ifvarn chainsparksprite -1
{
	ifvarn sprite[chainsparksprite].statnum 1024
	{
		getplayer[THISACTOR].posx x
		getplayer[THISACTOR].posy y
		getplayer[THISACTOR].posz z
		getplayer[THISACTOR].cursectnum mysector
		
		setvarvar mx sprite[chainsparksprite].x
		addvar mx 384
		rotatepoint sprite[chainsparksprite].x sprite[chainsparksprite].y mx sprite[chainsparksprite].y sprite[chainsparksprite].ang digx digy
		updatesector digx digy temp
		ifvarn temp -1
		{
			setplayer[THISACTOR].posx digx
			setplayer[THISACTOR].posy digy
			getactor[chainsparksprite].z digz
			subvar digz 2048
			setplayer[THISACTOR].posz digz
			
			addvar digz 8192
			getplayer[THISACTOR].i I
			setsprite I digx digy digz
			changespritesect I temp
			setplayer[THISACTOR].cursectnum temp
			
			ifvare CURRWEAP 1
			addvar botclip 1 // this extra bullet does not come from the clip
			ifvare CURRWEAP 3 ifvare usemp5 YES ifvarn MP5INCLIP 0 addvar MP5INCLIP 1
			ifvare CURRWEAP 9 shoot ICEBEAM else
			shoot SHOTSPARK1	
			
			
			setplayer[THISACTOR].posx x
			setplayer[THISACTOR].posy y
			setplayer[THISACTOR].posz z
			setplayer[THISACTOR].cursectnum mysector
			addvar z 8192
			setsprite I x y z
			changespritesect I mysector
		}
	}
	setvar chainsparksprite -1
}

ifvarn ONCAR 0
{
	cstator 32768
	getactor[THISACTOR].htextra temp
	ifvarg temp 0
	{
		shiftvarr temp 1
		ifvare temp 0 setvar temp 1
		setactor[THISACTOR].htextra temp
	}
	getplayer[THISACTOR].over_shoulder_on temp
	ifvare temp 0 setvar lastcameradist 0 else
	{
		ifvarand bits 4 setvar lastcameradist 32768 else
		ifvare camerasect -1 setvar lastcameradist 32768 else
		ifvarl lastcameradist 114688 addvar lastcameradist 3072 // 65536
		ifvarg lastcameradist 114688 setvar lastcameradist 114688 // 65536
	}
}
else
{
	setvar lastcameradist 0
	ifvare player[THISACTOR].over_shoulder_on 1 ifvarn viewmode 0
	{
		ifvarn playertarg -1
		{
			getactor[playertarg].statnum temp
			getactor[playertarg].picnum picnum
			ifvarn temp 1 setvar playertarg -1
			ifvarn picnum SHOOTME setvar playertarg -1
			ifvarn playertarg -1
			{
				getactor[playertarg].x mx
				getactor[playertarg].y my
				
				getactor[THISACTOR].x x
				getactor[THISACTOR].y y
				subvarvar mx x
				subvarvar my y
				getangle angvar mx my
				setactor[THISACTOR].ang angvar
				setplayer[THISACTOR].ang angvar
				// next change horiz
			    // ad hoc adjustments depending on target
			    
			    getactorvar[playertarg].myspawner spriteid
			    getactor[spriteid].z mz
			    ifvare sprite[spriteid].picnum ROTATEGUN addvar mz 5120
			    ifvare sprite[spriteid].picnum GREENSLIME addvar mz 5120
			    ifvare sprite[spriteid].picnum PIGCOPDIVE addvar mz 3072
			    getactor[spriteid].yrepeat temp
			    mulvar temp 192
			    subvarvar mz temp
			    getplayer[THISACTOR].posz z
			    subvarvar mz z
			    ldist xydist THISACTOR spriteid
			    ifvarl xydist 384 setvar spriteid -1 else
			    {
				    divvar xydist -8
				    divvarvar mz xydist
				    addvar mz 100
				    setplayer[THISACTOR].horiz mz
			    }
			    
			    
			}			
		}
		ifvare playertarg -1
		ifp pducking
		{
			findnearactorz SHOOTME 2048 16384 playertarg
			ifvarn playertarg -1 state playertargcheck
			ifvare playertarg -1 
			{
				findnearactorz SHOOTME 4096 32768 playertarg
				ifvarn playertarg -1 state playertargcheck
			}
			ifvare playertarg -1 
			{
				findnearactorz SHOOTME 7168 49152 playertarg
				ifvarn playertarg -1 state playertargcheck
			}
			ifvare playertarg -1 
			{
				findnearactorz SHOOTME 10240 65536 playertarg
				ifvarn playertarg -1 state playertargcheck
			}
		}
		
	}
	ifvarn viewmode 0 ifvare player[THISACTOR].newowner -1
	{
		ifvarn viewcam -1
		{
			setplayer[THISACTOR].runspeed RUNNINGSPEED
			getactor[THISACTOR].mdflags temp
			orvar temp 16
			setactor[THISACTOR].mdflags temp
			ifgapzl 64 nullop else
			ifvare nextcam -1 state spawnviewcam
		}
		else
		{
			ifvare player[THISACTOR].over_shoulder_on 1
			ifvarn nextcam -1
			{
				setvarvar temp nextcam
				state pview2
				ifvare tempd 1
				{
					setvarvar viewcam nextcam
					setvar nextcam -1
				}
			}
			ifgapzl 64 nullop else
			state spawnviewcam
			
			getactor[THISACTOR].mdflags temp
			xorvar temp 16
			setactor[THISACTOR].mdflags temp
		}
	}
}

ifvare menutoggle 0
{
	setvar temp 0
	ifvarand usebots 1 { ifvarvarl numallies numbots setvar temp 1 } else
	{
		ifvarg numbots 0
		setvar temp 1
	}
	ifvare temp 1
	{
		ifvarg botspawntime 0 subvar botspawntime 1
		ifvarl botspawntime 1
		{
			updatesectorz prevsectstartx prevsectstarty prevsectstartz temp
			ifvarn temp -1
			{
				setvar botspawntime 52
				espawn BOTALLY
				ifvarand usebots 2 nullop else
				{
					setvar tempb 0
					ifvarand usebots 8 ifrnd 64 orvar tempb 8
					ifvarand usebots 1 nullop else
					{ orvar tempb 2 ifvarg numbots 0 subvar numbots 1 }
					ifrnd 128 orvar tempb 16
					ifrnd 128 orvar tempb 32
					ifrnd 64 orvar tempb 64
					ifrnd 128 orvar tempb 128
					setactorvar[RETURN].monstflags tempb
				}
				setactor[RETURN].x prevsectstartx
				setactor[RETURN].y prevsectstarty
				setactor[RETURN].z prevsectstartz
				espawn TRANSPORTERSTAR
				setactor[RETURN].x prevsectstartx
				setactor[RETURN].y prevsectstarty
				setactor[RETURN].z prevsectstartz
				sound TELEPORTER
			}
		}
	}
}

ifvarg quikpipe 0
{
	addvar quikpipe 1
	ifvare quikpipe 6
	{
		getplayer[THISACTOR].ammo_amount 5 temp
		ifvarg temp 0
		{
			subvar temp 1
			setplayer[THISACTOR].ammo_amount 5 temp
			espawn HEAVYHBOMB
			setactor[RETURN].xvel 480
			setactor[RETURN].htg_t 7 40
			setactor[RETURN].htg_t 0 1
			setactor[RETURN].htg_t 6 1
			getplayer[THISACTOR].ang angvar
			setactor[RETURN].ang angvar
			getplayer[THISACTOR].posz mz
			addvar mz 2048
			setactor[RETURN].z mz
			getplayer[THISACTOR].horiz temp
			subvar temp 100
			mulvar temp -32
			subvar temp 512
			setactor[RETURN].zvel temp
			getactor[THISACTOR].x x
			getactor[THISACTOR].y y
			setvarvar mx x
			addvar mx 256
			rotatepoint x y mx y angvar mx my
			setactor[RETURN].x mx
			setactor[RETURN].y my
		}
		
	}
	ifvare quikpipe 19 setvar quikpipe 0
}
ifvarg botwait 0 subvar botwait 1
ifvarg beamhit 0 subvar beamhit 1
ifvarg wow 0 subvar wow 1
ifvarg fta 0 subvar fta 1
ifvarg showsurvivors 0 subvar showsurvivors 1
ifvarg smokecount 0 subvar smokecount 1
ifvarg mantcheck 0 subvar mantcheck 1
ifvarg booster 0 subvar booster 1
ifvarg burning 0 state imonfire
ifvarg LASERFIRE 0 addvar LASERFIRE 1
{
	ifvare LASERFIRE 4 setvar weap -1
	ifvarg LASERFIRE 7 setvar LASERFIRE 0
}

ifvarg portalview 0
{
	subvar portalview 1
	ifvare portalview 0 setvar portalcam -1
}


ifvarn cannonid -1
{
	getactor[cannonid].sectnum temp
	changespritesect THISACTOR temp
	setplayer[THISACTOR].cursectnum temp
	getactor[cannonid].x digx
	getactor[cannonid].y digy
	getactor[cannonid].ang angvar
	addvar angvar 1024
	
	setvarvar mx digx
	addvar mx 320 // 640
	rotatepoint digx digy mx digy angvar digx digy
	updatesector digx digy temp
	ifvarn temp -1
	{
		setplayer[THISACTOR].posx digx
		setactor[THISACTOR].x digx
		setplayer[THISACTOR].posy digy
		setactor[THISACTOR].y digy
	}
	else
	{
		getactor[cannonid].x digx
		getactor[cannonid].y digy
		setplayer[THISACTOR].posx digx
		setactor[THISACTOR].x digx
		setplayer[THISACTOR].posy digy
		setactor[THISACTOR].y digy
	}

	setplayer[THISACTOR].movement_lock 31
	
}
else
ifvare menutoggle 0 setplayer[THISACTOR].movement_lock 0

ends

state placetripbomb2

getactor[THISACTOR].ang angvar
addvar angvar 1024
setvarvar mx player[THISACTOR].posx
addvar mx 256
rotatepoint player[THISACTOR].posx player[THISACTOR].posy mx player[THISACTOR].posy angvar x y

espawn TRIPBOMB2
setsprite RETURN x y tripz

setactorvar[RETURN].myspawner triphack
setactorvar[RETURN].peractor5 sprite[triphack].picnum
getactor[THISACTOR].ang angvar
			
getincangle angvar1 angvar sprite[triphack].ang
setactorvar[RETURN].peractor1 angvar1

setactorvar[RETURN].mtype 256

getactor[THISACTOR].x mx
getactor[THISACTOR].y my
getactor[triphack].x x
getactor[triphack].y y
subvarvar mx x
subvarvar my y
getangle tempc mx my // angle to make monster face tripbomb2
getincangle ammo tempc sprite[triphack].ang
setactorvar[RETURN].ammo ammo

getactor[THISACTOR].z z
setactor[RETURN].z z

getactor[triphack].z z
subvarvar z tripz
ifvarl z 0 setvar z 0
setactorvar[RETURN].initz z

ifvarn sprite[triphack].picnum TANK
ifvarn sprite[triphack].picnum DRONE
ifvarn sprite[triphack].picnum ROTATEGUN
sound ALIEN_SWITCH1 else sound LASERTRIP_ONWALL

ends

move TRAILPROBE 160

state enter_ptrail

setvar temp 0
setvar spriteid -1
whilevarn temp 100
{
	setvarvar tempb ptrails[temp]
	ifvare tempb -1 // empty spot
	{
		setarray ptrails[temp] THISACTOR
		ifvarn spriteid -1 // should be the previous trail sprite
		{
			ldist xydist THISACTOR spriteid
			ifvarl xydist 512 move TRAILPROBE randomangle
		}
		setvar temp 99
	}
	else setvarvar spriteid tempb
	addvar temp 1
}

ends

state exit_ptrail

setarray ptrails[0] -1
setvar temp 1
setvar tempb 0
whilevarn temp 100
{
	setvarvar tempc ptrails[temp]
	setarray ptrails[tempb] tempc
	addvar temp 1
	addvar tempb 1
}
setarray ptrails[99] -1

ifvarg numallies 0
{
	setvar tempb 0
	setvarvar tempc highally
	addvar tempc 1
	whilevarvarn tempb tempc
	{
		setvarvar spriteid botallies[tempb]
		ifvarn spriteid -1
		{
			getactorvar[spriteid].bouncesound temp
			ifvarn temp -1 subvar temp 1
			setactorvar[spriteid].bouncesound temp
		}
		addvar tempb 1
	}
}

ends


useractor notenemy PLAYERTRAIL 0
sleeptime -1
ifmove 0
{
	spriteflags 16
	setvar playerally -1
	cstat 32768
	move INIT
	sizeat 4 4
	state enter_ptrail
	getactor[THISACTOR].x initx
	getactor[THISACTOR].y inity
	getactor[THISACTOR].z initz
}
else ifmove INIT
{
	setactor[THISACTOR].x initx
	setactor[THISACTOR].y inity
	setactor[THISACTOR].z initz
}
else ifmove TRAILPROBE
{
	ifnotmoving { move TRAILPROBE randomangle addvar countvar 1 }
	ifcount 52 move INIT
	ifvarg countvar 3 move INIT
}
ifcount 520 { state exit_ptrail killit }

enda

state playertrailcode

addvar ptrailtimer 1
ifvarg ptrailtimer 6 
{
	espawn PLAYERTRAIL
	getplayer[THISACTOR].posz z
	addvar z 4096
	setactor[RETURN].z z
	setvar ptrailtimer 0 
}


ends

state menucode

setplayer[THISACTOR].crack_time 777
setplayer[THISACTOR].weapon_pos -9
setplayer[THISACTOR].movement_lock 31
getplayer[THISACTOR].oang angvar
setplayer[THISACTOR].ang angvar
getplayer[THISACTOR].ohoriz temp
getplayer[THISACTOR].horiz tempb
setplayer[THISACTOR].horiz temp
subvarvar tempb temp
getuserdef[THISACTOR].mouseflip tempc
ifvare tempc 0 mulvar tempb -1
ifvarg tempb 1 
{
	setvarvar temp timer
	subvarvar temp lastchange
	ifvarg temp 2
	{
		orvar extbits 1
		subvar lastinput 1
	}
} else
ifvarl tempb -1 
{ 
	setvarvar temp timer
	subvarvar temp lastchange
	ifvarg temp 2
	{
		orvar extbits 2
		subvar lastinput 1
	}
}

ifvarand extbits 1 addvar lastinput 1
ifvarand extbits 2 addvar lastinput 1
ifvarand extbits 4 addvar lastinput 1
ifvarand extbits 8 addvar lastinput 1
ifvarand bits 4 addvar lastinput 1
ifvarvarg lastinput timer setvarvar lastinput timer

ifvarvarn timer lastinput
{
	ifvarand extbits 1 // move up
	{
		sound KICK_HIT
		setvarvar lastinput timer
		subvar menuitem 1
		setvarvar lastchange timer
		ifvare menutoggle 1
			ifvarl menuitem 0 setvar menuitem 8 // main menu
		
		ifvare menutoggle 2
			ifvarl menuitem 9 setvar menuitem 22 // weapons
		
		ifvare menutoggle 3
			ifvarl menuitem 30 setvar menuitem 41 // abilities
		
		ifvare menutoggle 4
			ifvarl menuitem 50 setvar menuitem 59 // monsters
		
		ifvare menutoggle 5
			ifvarl menuitem 70 setvar menuitem 83 // effects
			
		ifvare menutoggle 6
			ifvarl menuitem 90 setvar menuitem 95 // weather
			
		ifvare menutoggle 7
		    ifvarl menuitem 110 setvar menuitem 115 // bots
		
	} else
	ifvarand extbits 2 // move down
	{
		sound KICK_HIT
		setvarvar lastinput timer
		addvar menuitem 1
		setvarvar lastchange timer
		ifvare menutoggle 1
			ifvarg menuitem 8 setvar menuitem 0
		
		ifvare menutoggle 2
			ifvarg menuitem 22 setvar menuitem 9
		
		ifvare menutoggle 3
			ifvarg menuitem 41 setvar menuitem 30
		
		ifvare menutoggle 4
			ifvarg menuitem 59 setvar menuitem 50
		
		ifvare menutoggle 5
			ifvarg menuitem 83 setvar menuitem 70
			
		ifvare menutoggle 6
			ifvarg menuitem 95 setvar menuitem 90 // weather
			
		ifvare menutoggle 7
			ifvarg menuitem 115 setvar menuitem 110 // bots
		
	}
	
	ifvarand extbits 4 // strafe left, subtract
	{
		sound FOODLAND
		setvarvar lastinput timer
		
		switch menuitem
		
// 		case 5 // game speed
// 		ifvarg gamespeed 30 
// 		{
// 			subvar gamespeed 6
// 			subvar gamespeed_display 5
// 		}
// 		break
		
		case 21 // knockback distance
		ifvarg knockdist 0 subvar knockdist 1
		savegamevar knockdist
		break
		
		case 35 // maxmedkit
		subvar maxmedkit 5
		ifvarl maxmedkit 0 setvar maxmedkit 0
		savegamevar maxmedkit
		break
		
		case 51 // monster random attributes
		ifvarg randomize 0 { quote 1059 subvar randomize 1 }
		savegamevar randomize
		break
		
		case 52 // reduce monster damage
		ifvarg mdamage 10 subvar mdamage 10
		savegamevar mdamage
		break
		
		case 53 // reduce player damage
		ifvarg pdamage 10 subvar pdamage 10
		savegamevar pdamage
		break
		
		case 54 // monster in-fighting
		ifvarg infighting 0 subvar infighting 10
		ifvarl infighting 0 setvar infighting 0
		savegamevar infighting
		break
		
		case 57 // reduce pig db shotgun %
		ifvarg killerpigs 0 subvar killerpigs 10
		savegamevar killerpigs
		break
		
		case 74
		ifvare quotestyle 0 setvar quotestyle 2 else
		ifvare quotestyle 2 setvar quotestyle 1 else
		setvar quotestyle 0
		savegamevar quotestyle
		break
		
		case 91 // weather color
		subvar rainpal 1
		ifvarl rainpal 0 setvar rainpal 25
		savegamevar rainpal
		break
		
		case 92 // weather transparency
		ifvarand raincstat 512 subvar raincstat 512 else
		ifvarand raincstat 2 subvar raincstat 2 else
		{ orvar raincstat 2 orvar raincstat 512 }
		savegamevar raincstat
		break
		
		case 93 // weather severity
		ifvarl raining 0 addvar raining 1 else
		ifvare raining 0 nullop else
		subvar raining 1
		//savegamevar raining
		break
		
		case 94 // rainradius
		subvar rainradius 1024
		ifvarl rainradius 1024 setvar rainradius 1024
		savegamevar rainradius
		break
		
		case 110 // number of dukebots decrease
		subvar numbots 1
		ifvarl numbots 0 setvar numbots 0
		savegamevar numbots
		break
		default break		
		endswitch
		state optiontogglestate
	}
	
	ifvarand extbits 8 // strafe right, add
	{
		sound FOODLAND
		setvarvar lastinput timer
		
		switch menuitem
		
// 		case 5 // game speed
// 		ifvarl gamespeed 240 
// 		{
// 			addvar gamespeed 6
// 			addvar gamespeed_display 5
// 		}
// 		break
		
		case 21 // knockback distance
		ifvarl knockdist 100 addvar knockdist 1
		savegamevar knockdist
		break
		
		case 35 // maxmedkit
		addvar maxmedkit 5
		ifvarg maxmedkit 255 setvar maxmedkit 255
		savegamevar maxmedkit
		break
		
		case 51 // monster random attributes
		ifvarl randomize 20 { quote 1059 addvar randomize 1 }
		savegamevar randomize
		break
		
		case 52 // increase monster damage
		ifvarl mdamage 1000 addvar mdamage 10
		savegamevar mdamage
		break
		
		case 53 // increase player damage
		ifvarl pdamage 1000 addvar pdamage 10
		savegamevar pdamage
		break
		
		case 54 // monster in-fighting
		ifvarl infighting 100 addvar infighting 10
		ifvarg infighting 100 setvar infighting 100
		savegamevar infighting
		break
		
		case 57 // increase pig db shotgun %
		ifvarl killerpigs 100 addvar killerpigs 10
		savegamevar killerpigs
		break
		
		case 74
		ifvare quotestyle 0 setvar quotestyle 1 else
		ifvare quotestyle 1 setvar quotestyle 2 else
		setvar quotestyle 0
		savegamevar quotestyle
		break
				
		case 91 // weather color
		addvar rainpal 1
		ifvarg rainpal 25 setvar rainpal 0
		savegamevar rainpal
		break
		
		case 92 // weather transparency
		ifvarand raincstat 512 setvar raincstat 0 else
		ifvarand raincstat 2 orvar raincstat 512 else
		orvar raincstat 2
		savegamevar raincstat
		break
		
		case 93 // weather severity
		ifvarl raining 0 subvar raining 1 else
		addvar raining 1
		ifvarg raining 64 setvar raining 64
		//savegamevar raining
		break
		
		case 94 // rainradius
		addvar rainradius 1024
		ifvarg rainradius 131072 setvar rainradius 131072
		savegamevar rainradius
		break
		
		case 110 // number of bots increase
		addvar numbots 1
		ifvarg numbots 7 setvar numbots 7
		savegamevar numbots
		break
		default break
		endswitch
		state optiontogglestate
	}
			
	ifvarand bits 4 
	{
		state optiontogglestate
		
	}
}
setactor[THISACTOR].htextra -1
getplayer[THISACTOR].player_par timer
setvar mantling 0

ends

move PGROWINGPOP        // Used only as a variable

actor APLAYER MAXPLAYERHEALTH PSTAND 0 0

setvar playerally -1

ifvare steroidkicks YES
{
	setvarvar temp gamespeed
	mulvar temp 2
	divvar temp 3
	ifp ponsteroids 
		inittimer temp 
	else 
		inittimer gamespeed
}
else inittimer gamespeed


state checkhitscan
ifvarn botselected -1 state botcommandcode
state kludges


ifvarg numallies 0 state playertrailcode
else
ifvare smartmonsters YES
state playertrailcode


getplayer[THISACTOR].curr_weapon CURRWEAP
getplayer[THISACTOR].kickback_pic KICKBACKPIC
getplayer[THISACTOR].ammo_amount 3 MP5AMMO

ifvarn menutoggle 0 { state menucode break }

ifvarn sleeping 0 state sleepingcode


ifvarg superkicking 0 state superkickcode
ifvarl kickenergy KENERGY addvar kickenergy 1
ifvarn FAKERELOAD 0 addvar FAKERELOAD 1
ifvarn ALTMELEE 0 addvar ALTMELEE 1
ifvarg ALTMELEE 6 ifvarl ALTMELEE 11 shoot KNEE
ifvarg ALTMELEE 20 setvar ALTMELEE 0

findnearsprite RPG 1536 spriteid
  ifvarn spriteid -1
  {
		getactor[spriteid].owner temp
		ifvarvarn temp THISACTOR
		{
		  state projectilehitscan
		  ifvarvarn hitsprite THISACTOR
			soundonce WHOOSH
		}
  }

ifvare CURRWEAP 3
ifvarand ALTCHAINGUN 2 nullop else
{		
	setvar miniguncount 0 
	setvar grenadecounter 0 
}
			
ifvare miniguncount 0 
{
	setvar gunframe 6208 
	stopactorsound THISACTOR MINISPINSLOW
	stopactorsound THISACTOR MINISPINFAST
}

ifvare CURRWEAP 8
{
	ifvare KICKBACKPIC 4
	ifvarn triphack -1
	{
		// spawn it
		setvar temp 1
		ifvarn sprite[triphack].statnum 1 ifvarn sprite[triphack].statnum 2 setvar temp 0 else
		ifvarn actorvar[triphack].monstatus 1 setvar temp 0
		else
		{
			dist xydist THISACTOR triphack
			ldist xydist2 THISACTOR triphack
			ifvarg xydist 1792 setvar temp 0
			ifvarg xydist2 1024 setvar temp 0
		}
		ifvare temp 0
		{
			getplayer[THISACTOR].ammo_amount 8 tempb
			addvar tempb 1
			setplayer[THISACTOR].ammo_amount 8 tempb
		}
		else
		state placetripbomb2
		setvar triphack -1
	}
	else
	ifvarand bits 4
	ifvare KICKBACKPIC 0
	ifvare player[THISACTOR].weapon_pos 0
	ifvarg player[THISACTOR].ammo_amount 8 0
	ifvarn scannedsprite -1
	ifvare actorvar[scannedsprite].monstatus 1
	ifvarl sprite[scannedsprite].statnum 3
	ifvarg sprite[scannedsprite].statnum 0
	{
		dist xydist THISACTOR scannedsprite
		ldist xydist2 THISACTOR scannedsprite
		ifvarl xydist2 768 ifvarl xydist 1280
		{
			setplayer[THISACTOR].kickback_pic 1
			setvarvar triphack scannedsprite
			setvarvar tripz hitz // this should be the player's current z hitscan coord
		}
	}
}
else setvar triphack -1

ifp palive
{
	ifvare gravgun YES
	{
		ifvare CURRWEAP 11
		{
			ifvarand gravonce 1 nullop else
			ifvarl player[THISACTOR].fta 6
			{
				setplayer[THISACTOR].ftq 1075
				setplayer[THISACTOR].fta 6
			}
			
			ifvarn gravitysprite -1 
			{
				ifvarand gravonce 2 nullop else ifvarvare holding gravitysprite
				ifvarl player[THISACTOR].fta 6
				{
						setplayer[THISACTOR].ftq 1082
						setplayer[THISACTOR].fta 6
				}
				
				orvar gravonce 1
				setplayer[THISACTOR].kickback_pic 18
				ifvare holdingpic SAWBLADEGROUND ifvarand bits 4
				{
					setplayer[THISACTOR].kickback_pic 1
					setvar WEAPON11_SHOOTS SAWBLADEPROJ
					setactorvar[holding].mtype 1
					setactor[holding].cstat 32768
					setvar holding -1
				}
			}
			ifvarand bits 262144 { soundonce GRAVPULL setplayer[THISACTOR].kickback_pic 18 }
			else ifvare holding -1 { setvar gravitysprite -1 stopsound GRAVPULL }
		}
		else { setvar gravitysprite -1 stopsound GRAVPULL }
	}
// 	ifvare CURRWEAP 7 ifvare devshotty 2 ifvare KICKBACKPIC 3
// 	{
// 		getplayer[THISACTOR].ammo_amount 7 temp
// 		ifvarg temp 1 subvar temp 1
// 		setplayer[THISACTOR].ammo_amount 7 temp
// 	}
	ifvare WEAPON7_SHOOTS POWBALL
	{
		ifvare CURRWEAP 7 ifvare KICKBACKPIC 0 
		{
			setvar WEAPON7_SHOOTS POWBEAM
			setvar WEAPON7_TOTALTIME 16
			setvar WEAPON7_FIRESOUND IMP_SHOOT
		}
	}
	ifvare CURRWEAP 7 ifvare usebfg YES
	{
		getplayer[THISACTOR].weapon_pos temp
		ifvare temp -8 sound IMP_SWITCH
	}
	ifvare CURRWEAP 5 setvarvar PIPEBOMB_CONTROL pipecontrol
	ifvare CURRWEAP 4 ifvare usebigrox YES
	{
		ifvare KICKBACKPIC 20 setplayer[THISACTOR].weapon_pos -1
		ifvarg KICKBACKPIC 29
		ifvarl KICKBACKPIC 51 // 65 
		 setplayer[THISACTOR].weapon_pos -9
		ifvare KICKBACKPIC 45 sound RPGLOAD
		ifvare KICKBACKPIC 35 sound RPGLOAD
		
	}
	ifvare CURRWEAP 3 
	{
		ifvarand ALTCHAINGUN 4 // switching chainguns
		{
			getplayer[THISACTOR].weapon_pos temp
			ifvare temp -9 { state changeminigun subvar ALTCHAINGUN 4 }
		}
		ifvarand ALTCHAINGUN 2
		{
			ifvarl player[THISACTOR].ammo_amount 3 1 ifvarand bits 4
			{
				ifvarvarl lastinput timer soundonce WEAPEMPTY
				setvarvar lastinput timer addvar lastinput 1
			}
			ifp palive
			ifvare CURRWEAP 3
			{
// 				ifvare player[THISACTOR].ammo_amount 3 0 ifvarg grenades 0
// 				{
// 					setplayer[THISACTOR].ammo_amount 3 1
// 					ifvarn KICKBACKPIC 0
// 					{
// 						setvar KICKBACKPIC 0
// 						setplayer[THISACTOR].kickback_pic 0
// 					}
// 				}
// 				
				ifvarn grenadecounter 0
				{
					ifvare grenadecounter 3
					{
						shoot MORTER2
						sound RPG_SHOOT
					}
					addvar grenadecounter 1
					ifvarg grenadecounter 25 setvar grenadecounter 0
				}
				ifvarn miniguncount 0 
				{
					ifvare gunframe 6208 setvar gunframe 6209 else
					ifvare gunframe 6209 setvar gunframe 6210 else
					ifvare gunframe 6210 setvar gunframe 6211 else
					ifvare gunframe 6211 setvar gunframe 6209 else
					setvar gunframe 6209
					
					ifvarl miniguncount MAXSPIN
					{
						stopactorsound THISACTOR MINISPINFAST
						soundonce MINISPINSLOW
					} else
					{
						stopactorsound THISACTOR MINISPINSLOW
						soundonce MINISPINFAST
					}
				}
				
			    ifvarand bits 4
			       addvar miniguncount 1
			    else
			    {
				   ifvarg miniguncount MINIFIRECOUNT setvar miniguncount MINIFIRECOUNT
				   ifvarg miniguncount 0 subvar miniguncount 1
			    }
				   
				ifvarn KICKBACKPIC 0
				{
					ifvarl miniguncount MINIFIRECOUNT 
						setplayer[THISACTOR].kickback_pic 0
					else ifvare KICKBACKPIC 11
					{
						setvar KICKBACKPIC 2
						getplayer[THISACTOR].ammo_amount 3 temp
						ifvare temp 0 setvar KICKBACKPIC 0
						setplayer[THISACTOR].kickback_pic KICKBACKPIC
					}
				}
				ifvarg miniguncount MINISPEEDCOUNT 
				{
					setvar WEAPON3_FLAGS 76
					ifvarg miniguncount MAXSPIN setvar miniguncount MAXSPIN
				} else
				setvar WEAPON3_FLAGS 84 
			}
			
		}
		else
		{
			getplayer[THISACTOR].ammo_amount 3 tempb
			ifvare tempb 0 ifvarand bits 4 
			{
				ifvarvarl lastinput timer soundonce WEAPEMPTY
				setvarvar lastinput timer addvar lastinput 1
			}
			ifvare usemp5 YES
			{
				ifvarg FAKERELOAD 65 setvar FAKERELOAD 0
				// ifvare KICKBACKPIC 20 setvar wacked -1
				
				ifvarn FAKERELOAD 0
				{
					ifvare FAKERELOAD 16 sound MP5CLIPOUT
					ifvare FAKERELOAD 48
					{
						setvar tempc 30
						subvarvar tempc MP5INCLIP // tempc is now how much room is in the clip
						setvarvar tempb MP5AMMO
						subvarvar tempb MP5INCLIP // tempb is the amount you have in excess of the amount in clip
						ifvarvarl tempb tempc // if you have less ammo than what the clip can hold
							addvarvar MP5INCLIP tempb
						else
							setvar MP5INCLIP 30
						sound MP5CLIPIN
					}
				}
				else
				{
					ifvare MP5INCLIP 0
					{
						ifvare KICKBACKPIC 0
						ifvarg MP5AMMO 0
						{
							setvar FAKERELOAD 1
						}
					}
					else
					ifvarl MP5INCLIP 15 ifvare reloadonce 0 ifvare KICKBACKPIC 0
					ifvarg MP5AMMO 0
					ifvarl player[THISACTOR].fta 6
					{
						setplayer[THISACTOR].ftq 1074
						setplayer[THISACTOR].fta 6
					}
					
				}
				
			}
		}
	}
	else
	ifvare CURRWEAP 2
	{
		ifvare usemossberg YES ifvarl player[THISACTOR].ammo_amount 2 2
		{
			getplayer[THISACTOR].weaponswitch temp
			andvar temp 0xFFFD // clear bit2
			setplayer[THISACTOR].weaponswitch temp // to prevent auto switching away
		}

		ifvarand ALTSHOTGUN 8 // switching shotguns
		{
			getplayer[THISACTOR].weapon_pos temp
			ifvare temp -9 { state changeshotgun subvar ALTSHOTGUN 8 }
		}
		ifvarand ALTSHOTGUN 4 // double barreled shotgun
		{
			ifvare KICKBACKPIC 5 // firing time
			{
				getplayer[THISACTOR].ammo_amount 2 temp
				ifvarg temp 0 subvar temp 1
				setplayer[THISACTOR].ammo_amount 2 temp
			}
			ifvare KICKBACKPIC 18 sound DBOPEN else
			ifvare KICKBACKPIC 34
			{
				//spawn NEWSMOKE
				espawn SHOTGUNSHELL
				getactor[RETURN].z z
				addvar z 2048
				setactor[RETURN].z z
				getplayer[THISACTOR].ang angvar
				addvar angvar 1024
				setactor[RETURN].ang angvar
				espawn SHOTGUNSHELL
				setactor[RETURN].z z
				setactor[RETURN].ang angvar
			} else
			ifvare KICKBACKPIC 42 sound DBLOAD else
			ifvare KICKBACKPIC 54 
				sound DBCLOSE
			
			ifvarg KICKBACKPIC 15
			{
				ifvare KICKBACKPIC 16
				{
					getplayer[THISACTOR].ammo_amount 2 temp
					ifvarg temp 0
					setvar dbpicnum 8826 else // 6350 for 8bit
					{
						setplayer[THISACTOR].kickback_pic 0
						setvar KICKBACKPIC 0
					}
				}
				
				else ifvarl KICKBACKPIC 61
					setvar dbpicnum 8828 // 6352 for 8bit
				else
				setvar dbpicnum 8826 // 6350 for 8bit
			}

		} else ifvare usemossberg YES 
		{
			ifvare FAKERELOAD 0
			{
				getplayer[THISACTOR].ammo_amount 2 tempb
				ifvarvarg SHOTGUNCLIP tempb setvarvar SHOTGUNCLIP tempb
				ifvare KICKBACKPIC 13 sound MOSSPUMP
				ifvare KICKBACKPIC 0 ifvare SHOTGUNCLIP 0
				{
					ifvarg tempb 0
						setvar FAKERELOAD 1
				} else
				ifvarl SHOTGUNCLIP 4 ifvare reloadonce 0 ifvare KICKBACKPIC 0
				{
					ifvarg tempb 0
					{
						getplayer[THISACTOR].fta temp
						ifvarl temp 6
						{
							setplayer[THISACTOR].ftq 1074
							setplayer[THISACTOR].fta 6
						}
					}
				}
			}
			else
			{
				ifvare reloadpicnum 8844 ifvare FAKERELOAD 25 sound MOSSINSERTSHELL // 27
				ifvare reloadpicnum 8847 ifvare FAKERELOAD 30 // 34
					sound MOSSPUMP
			}
		}
		
	}
	else 
	{
		setvar ALTMELEE 0
		ifvare usemossberg NO setvar FAKERELOAD 0
	}
	
	ifvare FAKERELOAD 0
		setvar reloadpicnum -1
	ifvarn player[THISACTOR].weapon_pos 0 setvar FAKERELOAD 0
	
	ifvare CURRWEAP 2 ifvarn FAKERELOAD 0
	{
		ifvarand bits 4 ifvarn SHOTGUNCLIP 0 setvar STOPRELOAD 1
		ifvarl FAKERELOAD 20
		  setvar reloadpicnum 8843
		else
		{
			ifvare reloadpicnum 8843 { setvarvar shellstarted timer setvar reloadpicnum 8844 }
			else
			{
				ifvarl reloadpicnum 8846 // in the shell loading loop
				{
					ifvare reloadpicnum 8844 // the shell loading animation
					{
						setvarvar temp timer
						subvarvar temp shellstarted
						ifvarg temp 12 // 16 another shell has been loaded
						{
							// stop when full, or when all the shells you have are loaded, whichever comes first...
							addvar SHOTGUNCLIP 1
							getplayer[THISACTOR].ammo_amount 2 temp
							setvar tempb 0
							ifvarl temp 1 setvar tempb 1
							ifvarvare SHOTGUNCLIP temp setvar tempb 1
							ifvarg SHOTGUNCLIP 7 setvar tempb 1
							ifvare STOPRELOAD 1 setvar tempb 1 // also stop if the player wants to fire
							ifvare tempb 1
							{
								setvar reloadpicnum 8846
								setvar FAKERELOAD 21
							}
							else
							{
								setvar reloadpicnum 8845 // this will reset the loading loop
								setvar FAKERELOAD 21
							}
					    }
				    } else
				    // time to start another shell
				    {
					    setvarvar shellstarted timer 
					    setvar reloadpicnum 8844
				    }
			    }
			    else 
			    {
				    ifvarl FAKERELOAD 28 setvar reloadpicnum 8846
				    else setvar reloadpicnum 8847
				}
		    }
		}
		ifvarg FAKERELOAD 37 setvar FAKERELOAD 0
	}
	else setvar STOPRELOAD 0
	
	ifvare CURRWEAP 1
	{
		ifvarn PISTOLRELOAD 0
		{
			getplayer[THISACTOR].ammo_amount 1 tempb
			subvarvar tempb botclip // tempb is the amount of ammo you have in excess of what is in clip
			setvarvar PISTOLRELOAD KICKBACKPIC
			ifvare akimbo -1 setvar temp 26 
			else ifvare akimbo 0 setvar temp 26 
			else setvar temp 47
			ifvarvare PISTOLRELOAD temp
			{
				ifvare depistols YES
				{
					ifvare akimbo -1 setvar tempc 8 else
					ifvare akimbo 0 setvar tempc 8 
					else setvar tempc 16 // CLIP SIZE
				}
				else
				{
					ifvare akimbo -1 setvar tempc 12 else
					ifvare akimbo 0 setvar tempc 12 
					else setvar tempc 24 // CLIP SIZE
				}
				subvarvar tempc botclip // tempc is now how much room is in the clip
				ifvarvarl tempb tempc // if you have less ammo than what the clip can hold
					addvarvar botclip tempb
				else
				{
					ifvare depistols YES
					{
						ifvare akimbo -1 setvar botclip 8 else
						ifvare akimbo 0 setvar botclip 8 
						else setvar botclip 16 // CLIP SIZE
					}
					else
					{
						ifvare akimbo -1 setvar botclip 12 
						else ifvare akimbo 0 setvar botclip 12 
						else setvar botclip 24
					}
				}
				
			}
		}
		else
		{
			getplayer[THISACTOR].ammo_amount 1 tempb
			ifvare tempb 0 ifvarand bits 4 
			{
				ifvarvarl lastinput timer soundonce WEAPEMPTY
				setvarvar lastinput timer addvar lastinput 1
			}
			ifvarn player[THISACTOR].last_weapon 0
			ifvare botclip 0 ifvarg akimbo -2 ifvarl akimbo 2 ifvarg tempb 0
			ifvare player[THISACTOR].weapon_pos 0
			{
				ifvare depistols YES ifvarl akimbo 1 nullop // handled elsewhere
				else
				{
					ifvare akimbo -1 setvar WEAPON1_RELOAD 27 else
					ifvare akimbo 0 setvar WEAPON1_RELOAD 27 
					else setvar WEAPON1_RELOAD 48
					ifvare depistols YES addvar WEAPON1_RELOAD 2
					setplayer[THISACTOR].reloading 1
					ifvare depistols YES setplayer[THISACTOR].kickback_pic 13
					else
					setplayer[THISACTOR].kickback_pic 9
					sound EJECT_CLIP
					setvar PISTOLRELOAD 1
				}
			}
			else
			setvar WEAPON1_RELOAD 0
			setvar temp 0
			ifvarl akimbo 1 { ifvarl botclip 4 setvar temp 1 }
			
			else ifvarl botclip 8 setvar temp 1
			ifvare temp 1 ifvarg tempb 1 ifvare reloadonce 0 ifvare KICKBACKPIC 0
			{
				getplayer[THISACTOR].fta temp
				ifvarl temp 6
				{
					setplayer[THISACTOR].ftq 1074
					setplayer[THISACTOR].fta 6
				}
				
			}
			
		}
			
	}
				
}

ifvarg monallies 0 subvar monallies 1
ifvarg numallies 0
{
	ifvare sector[THISACTOR].hitag 666 // kill the bots
	{
		setvar tempb 0
		setvarvar tempc highally
		addvar tempc 1
		whilevarvarn tempb tempc
		{
			setvarvar spriteid botallies[tempb]
			setactorvar[spriteid].command 666
			addvar tempb 1
		}
	}
}
// ifvarg lightidle 0 subvar lightidle 1
ifvarg backview 0 subvar backview 1
// ifhitspace setvar lightidle 40
ifvarg underwater 0 state underwatercode else { setvar oxygen 390 setvar headunder 0 }

state holdingcode
ifvarg drinksoda 0
{
	addvar drinksoda 1
	ifvarg drinksoda 78 
	{
		addphealth 5
		setvar drinksoda 0
		setactor[holding].pitch 0
		setactor[holding].roll 0
		setactor[holding].picnum COLACANSTAND
		setvar holding -1
	}
}

// damage reduction code
getactor[THISACTOR].htextra temp
getuserdef[THISACTOR].god tempb
ifvare tempb YES setvar temp 0
ifvarg temp 0
{
	ifvarn cannonid -1 // half damage when using cannon
	{
		shiftvarr temp 1
		ifvarl temp 1 setvar temp 1
		setactor[THISACTOR].htextra temp
	}
	getactor[THISACTOR].htowner spriteid
	ifvarn spriteid -1 ifvarvarn spriteid THISACTOR
	{
		getactorvar[spriteid].playerally picnum
		{
			ifvarn picnum 0
			{
				setvar temp -1
				setactor[THISACTOR].htextra -1
			}
		}
	}
	ifvarg temp 0
	{
		ifvare goodarmor YES
		{
			getplayer[THISACTOR].shield_amount tempb
			ifvarg tempb 0
			ifvarn sprite[THISACTOR].htpicnum INKBLAST ifvarn sprite[THISACTOR].htpicnum SPIT
			{
				sound HITARMOR
				shiftvarr temp 1
				ifvarl temp 1 setvar temp 1
				else subvarvar tempb temp
				setactor[THISACTOR].htextra temp
				ifvarl tempb 0 setvar tempb 0
				setplayer[THISACTOR].shield_amount tempb
			}
		}
		ifvarn mdamage 100
		{
			getactor[THISACTOR].htpicnum picnum
			ifvarn picnum RADIUSEXPLOSION
			ifvarn picnum RPG
			{
				mulvarvar temp mdamage
				divvarvar temp 100
				setactor[THISACTOR].htextra temp
			}
		}
	}
	
}

setvar monstatus 1

ifvare useakimbo YES
{
	ifvarg weapswitch 0 subvar weapswitch 1
	ifvarg akimbo 1 subvar akimbo 1
	ifvarl akimbo -1
	{
		addvar akimbo 1
		ifvare akimbo -1 setvar akimbo 0
	}
	
	ifp palive
	ifvare CURRWEAP 1 
	{
		ifvare depistols YES
		{
			ifvarl akimbo 1
			ifvare botclip 0
			ifvarg player[THISACTOR].ammo_amount 1 0
			{
				ifvare KICKBACKPIC 9
				{
					setvar WEAPON1_RELOAD 29
					setplayer[THISACTOR].reloading 1
					setplayer[THISACTOR].kickback_pic 11
					sound EJECT_CLIP
					setvar PISTOLRELOAD 1
				} else
				ifvare KICKBACKPIC 0 ifvare player[THISACTOR].weapon_pos 0
				{
					setvar WEAPON1_RELOAD 29
					setplayer[THISACTOR].reloading 1
					setplayer[THISACTOR].kickback_pic 11
					sound EJECT_CLIP
					setvar PISTOLRELOAD 1
				}
			}
			
			ifvarg akimbo 0
			ifvare player[THISACTOR].kickback_pic 8
			{
				sound DEPISTOL_FIRE 
				shoot DEBULLET
				getplayer[THISACTOR].ammo_amount 1 temp
				ifvarg temp 0 subvar temp 1
				setplayer[THISACTOR].ammo_amount 1 temp
				espawn SHELL
				setactor[RETURN].pal 30
			}
			getplayer[THISACTOR].horiz tempb
			switch KICKBACKPIC
			case 3 subvar tempb 4 break
			case 8 ifvarg akimbo 0 subvar tempb 4 break
			case 4 case 5 addvar tempb 2 break
			case 9 case 10 ifvarg akimbo 0 addvar tempb 2 break
			endswitch
			setplayer[THISACTOR].horiz tempb
		}
		else
		ifvarg akimbo 0
		ifvare player[THISACTOR].kickback_pic 6
		{	
			ifvare dpsounds YES sound DPPISTOLFIRE else
			sound PISTOL_FIRE
			shoot SHOTSPARK1
			getplayer[THISACTOR].ammo_amount 1 temp
			ifvarg temp 0 subvar temp 1
			setplayer[THISACTOR].ammo_amount 1 temp
			espawn SHELL
			setactor[RETURN].pal 30
		}

	}
	
}

subvar chaingunheat 2 ifvarl chaingunheat 0 setvar chaingunheat 0
setvar temp OVERHEAT
ifvare temp YES ifvarg chaingunheat 1000
ifvare CURRWEAP 3
{
	setplayer[THISACTOR].kickback_pic 0
	soundonce STEAM_HISSING
}

ifvarg fpress 0 subvar fpress 1
ifvarg bpress 0 subvar bpress 1
ifvarl onladder 0 addvar onladder 1

ifvarg onladder 0 state laddercode

ifp ponground setvar nofalldamage 0
ifvarn nofalldamage 0 setplayer[THISACTOR].falling_counter 0

ifvarg bhold 7 { setvar btap 0 setvar bhold 0 }
ifvarg fhold 7 { setvar ftap 0 setvar fhold 0 }
ifvarg lhold 7 { setvar ltap 0 setvar lhold 0 }
ifvarg rhold 7 { setvar rtap 0 setvar rhold 0 }

ifvarvarn timer fpresstime setvar fhold 0
ifvarvarn timer bpresstime setvar bhold 0


getplayer[THISACTOR].player_par timer
ifvare timer 1
nullop else setvar colamachines 0

// ifvare rendmode 4
// {
// 	setvarvar temp framerate
// 	ifvarg temp 80 setvar temp 80
// 	shiftvarl temp 9
// 	setvar drawdist 1024
// 	addvarvar drawdist temp
// 	ifvarn camerasprite -1 setvar drawdist 131072
// 	
// 	setvar temp 1024
// 	whilevarn temp 0
// 	{
// 		ifvarn sprite[lastsprite].statnum 1024
// 		ifvarg sprite[lastsprite].picnum 100
// 		{
// 			dist xydist THISACTOR lastsprite
// 			setvarvar D drawdist
// 			ifvare actorvar[lastsprite].monstatus 1 shiftvarl D 2
// 			
// 			ifvarvarg xydist D
// 			ifvarvarn sprite[lastsprite].sectnum player[THISACTOR].cursectnum
// 			{
// 				ifvare actorvar[lastsprite].outofsight NO
// 				ifvarg sprite[lastsprite].cstat -1
// 				{
// 					getactor[lastsprite].cstat tempb
// 					orvar tempb 32768
// 					setactor[lastsprite].cstat tempb
// 					setactorvar[lastsprite].outofsight YES
// 				}
// 			}
// 			else ifvare actorvar[lastsprite].outofsight YES
// 			{
// 				getactor[lastsprite].cstat tempb
// 				xorvar tempb 32768
// 				setactor[lastsprite].cstat tempb
// 				setactorvar[lastsprite].outofsight NO
// 			}
// 		}
// 		addvar lastsprite 1
// 		ifvarg lastsprite 16383 setvar lastsprite 0
// 		subvar temp 1
// 	}
// }

setvarvar tempb timer
subvarvar tempb fpresstime
ifvarg tempb 4 setvar ftap 0

setvarvar tempb timer
subvarvar tempb bpresstime
ifvarg tempb 4 setvar btap 0

setvarvar tempb timer
subvarvar tempb lpresstime
ifvarg tempb 4 setvar ltap 0

setvarvar tempb timer
subvarvar tempb rpresstime
ifvarg tempb 4 setvar rtap 0

ifvare candodge YES ifvare ONCAR 0
{
	ifinwater nullop else
	ifvare underwater 0 state dodgecode
}
else
ifvarn dodge 0 state dodgecode
ifvarg dodge 0 
{
	subvar dodge 1 // safest to put it here
	ifvare dodge 0 
    {
// 	       ifvarn jumpadxvel 0 setvar dodgeagain 6 else
// 	       setvar dodgeagain 26
// 	       setvar walljump 0
// 	       setvar jumpadxvel 0
	       
	       setvar temp FREERUNNER
	       ifvarn jumpadxvel 0 setvar dodgeagain 6 else
	       setvar dodgeagain 26
	       ifp ponground setvar walljump 0 else
	       ifvare temp YES ifvarn walljump 0 setvar dodgeagain 0
	       setvar jumpadxvel 0
	}
}
ifp ponground setvar walljump 0

ifp pdead nullop
 else 
  state stepsoundstuff
  

ifvare slowrunaccel YES ifvare underwater 0 ifvare viewcam -1 state runningcode
ifvare canmantle YES state mantlingcode
else ifvarg underwater 0 state mantlingcode else
ifvarn mantling 0 state mantlingcode


state slipperycode
getplayer[THISACTOR].posxv oposxv
getplayer[THISACTOR].posyv oposyv


ifvarn raining 0 ifvare menutoggle 0 state raincode

  ifaction 0
    action PSTAND
  

  ifaction PFROZEN
  {
    cstat 257

    fall

    palfrom 16 0 0 24

    ifmove 0
    {
      ifhitweapon
      {
        ifwasweapon FREEZEBLAST
          break
        lotsofglass 60
        ifrnd 84
          spawn BLOODPOOL

        sound GLASS_BREAKING
        spawn ATOMICHEALTH

        getlastpal

        move 1
        break
      }
    }
    else
    {
      cstat 32768
      quote 13
      ifhitspace
      {
        action PSTAND
        resetplayer
      }
      break
    }

    ifactioncount THAWTIME
    {
      getlastpal
      strength 1
      move 0
      action PSTAND
    }
    else
      ifactioncount FROZENDRIPTIME
    {
      ifrnd 32
      spawn WATERDRIP
    }

    ifp pfacing   // assumes another other player  (multiplayer)
      ifpdistl FROZENQUICKKICKDIST
      ifvare superkicking 0
        pkick

    break
  }

  ifdead
  {
    ifaction PGROWING
    {
      ifmove PGROWINGPOP
      {
        quote 13
        ifhitspace
        {
          action 0
          resetplayer
        }
        break
      }
      else
      {
        ifcount 32
        {
          sound SQUISHED
          palfrom 48 64

          state standard_pjibs

          guts JIBS4 20
          guts JIBS6 20
          move PGROWINGPOP
          cstat 32768
          tossweapon

          hitradius 2048 60 70 80 90

        }
        else
          sizeto MAXXSTRETCH MAXYSTRETCH
      }
      break
    }

    ifsquished
      palfrom 32 63 63 63
    else
      fall

    ifactioncount 7
      move 0
    else
      ifactioncount 6
    {
      ifmultiplayer nullop
      else
      {
        ifrnd 32
          sound DUKE_KILLED5
        else
          ifrnd 32
            sound DUKE_KILLED3
        else
          ifrnd 32
            sound DUKE_KILLED1
        else
          ifrnd 32
            sound DUKE_KILLED2
      }
    }

    ifaction PLYINGDEAD
    {
      ifactioncount 3
        move PSTOPED
      quote 13
      ifhitspace
      {
        action PSTAND
        spawn DUKELYINGDEAD
        resetplayer
      }
      break
    }

    ifaction PTHROWNBACK
    {
      ifactioncount 5
      {
        spawn BLOODPOOL
        action PLYINGDEAD
      }
      else
        ifactioncount 1
          move 0
      break
    }

    ifaction PEXPLODEAD
    {
      quote 13
      ifhitspace
      {
        resetplayer
        action PSTAND
      }
      break
    }
    ifaction PEXPLODE
    {
      ifactioncount 5
      {
        action PEXPLODEAD
        spawn BLOODPOOL
      }
      break
    }

    ifp pshrunk
    {
      state standard_pjibs
      spawn BLOODPOOL

      sound SQUISHED
      sound DUKE_DEAD

      cstat 32768         // Hide the sprite

      action PLYINGDEAD
    }
    else
    {
	  setvar temp 0
	  ifinwater setvar temp 1
	  ifvarg underwater 0 setvar temp 1
	  ifvare temp 1
      {
        action PLYINGDEAD
        spawn WATERBUBBLE
        spawn WATERBUBBLE
      }
      else
      {
        action PEXPLODE
        state standard_pjibs
        cstat 32768
        sound SQUISHED
        sound DUKE_DEAD
      }
    }

    tossweapon

    break
  }

  ifsquished
  {
    strength -1
    sound SQUISHED
    state random_ooz
    break
  }

  ifp ponsteroids
  {
    ifp pstanding nullop
    else
      spawn FRAMEEFFECT1
  }

  ifmove PSHRINKING
  {
    ifcount 32
    {
      ifcount SHRUNKDONECOUNT
      {
        move 0
        cstat 257
      }
      else
        ifcount SHRUNKCOUNT
      {
        sizeto 42 36
        ifgapzl 24
        {
          strength 0
          sound SQUISHED
          palfrom 48 64
          break
        }
      }
      else
        ifp ponsteroids
          count SHRUNKCOUNT
    }
    else
    {
      ifp ponsteroids
        count SHRUNKCOUNT
      else
      {
        sizeto 8 9
        spawn FRAMEEFFECT1
      }
    }
  }
  else ifhitweapon
  {
    ifdead
    {
      ifmultiplayer
        sound DUKE_KILLED4

      ifwasweapon GROWSPARK
      {
        palfrom 48 48
        action PGROWING
        count 0
        move 0
        sound ACTOR_GROWING
        cstat 0
        break
      }
    }
    else
    {
	  setplayer[THISACTOR].sound_pitch 0
      ifmultiplayer
      {
        ifphealthl YELLHURTSOUNDSTRENGTHMP
        {
          ifrnd 64
            sound DUKE_LONGTERM_PAIN2
          else
            ifrnd 64
              sound DUKE_LONGTERM_PAIN3
          else
            ifrnd 64
              sound DUKE_LONGTERM_PAIN4
          else
            sound DUKE_DEAD
        }
        else
        {
          ifrnd 64
            sound DUKE_LONGTERM_PAIN5
          else ifrnd 64
            sound DUKE_LONGTERM_PAIN6
          else ifrnd 64
            sound DUKE_LONGTERM_PAIN7
          else
            sound DUKE_LONGTERM_PAIN8
        }
      }
      else
      {
        ifphealthl YELLHURTSOUNDSTRENGTH
        {
          ifrnd 74
            sound DUKE_LONGTERM_PAIN2
          else
            ifrnd 8
              sound DUKE_LONGTERM_PAIN3
          else
            sound DUKE_LONGTERM_PAIN4
        }
        ifrnd 128
          sound DUKE_LONGTERM_PAIN
      }
    }
    ifstrength TOUGH
    {
      state headhitstate
      sound DUKE_GRUNT
      ifp pstanding
        action PFLINTCHING
    }

    ifwasweapon RPG
    {
      ifrnd 32
        spawn BLOOD
      ifdead
        state standard_pjibs
      palfrom 48 52
      break
    }
    ifwasweapon RADIUSEXPLOSION
    {
      ifrnd 32
        spawn BLOOD
      ifdead
        state standard_pjibs
      palfrom 48 52
      break
    }
    ifwasweapon FIREEXT
    {
      ifrnd 32
        spawn BLOOD
      ifdead
        state standard_pjibs
      palfrom 48 52
      break
    }
    ifwasweapon GROWSPARK
    {
      palfrom 48 52
      sound EXPANDERHIT
      break
    }
    ifwasweapon SHRINKSPARK
    ifvare ONCAR 0
    {
      palfrom 48 0 48
      move PSHRINKING // used as a var only.
      sound ACTOR_SHRINKING
      cstat 0
      break
    }
    ifwasweapon SHOTSPARK1
      palfrom 24 48
    ifwasweapon STARPROJ
      palfrom 28 48
    ifwasweapon SHRAPNEL
      palfrom 20 48
    ifwasweapon FIREBALLPROJ
      palfrom 32 48
    ifwasweapon FIREBULLET
      palfrom 28 48
    ifwasweapon ELECTROPROJ
      palfrom 40 63 63 63
    ifwasweapon BIGFIRELASER
      palfrom 50 48 0 10
    ifwasweapon RAILSLUG_WEAK
      palfrom 32 48
    ifwasweapon INKBLAST
    {
      palfrom 40 0 0 0
      getplayer[THISACTOR].posxv xvel
      shiftvarr xvel 1
      getplayer[THISACTOR].posyv yvel
      shiftvarr yvel 1
      setplayer[THISACTOR].posxv xvel
      setplayer[THISACTOR].posyv yvel
    }
    ifwasweapon FREEZEBLAST
    {
      palfrom 48 0 0 48
      ifdead
      {
        sound SOMETHINGFROZE
        spritepal 1
        move 0
        action PFROZEN
        break
      }
    }
    ifwasweapon COOLEXPLOSION1
      palfrom 48 48 0 48
    ifwasweapon KNEE
      palfrom 16 32
    ifwasweapon FIRELASER
      palfrom 32 32

    ifdead
    {
      action PTHROWNBACK
      tossweapon
    }

    state random_wall_jibs

    break
  }
  
  ifaction PSKICKING // superkicking action
  {
	setvar temp 0
	ifvare superkicking 0 setvar temp 1
	ifactioncount 2 setvar temp 1
	ifvare temp 1
	{
		ifp pducking action PDUCKING else
		action PSTAND 
	}
	// break 
  }
  else
  ifvarn superkicking 0 ifvare superkickdone 0 ifp palive
  {
	ifp pducking nullop else ifaction PFROZEN nullop else
    action PSKICKING
    break
  }

  ifaction PFLINTCHING
  {
    ifactioncount 2
      action PSTAND
    break
  }

  setvar temp 0
  getplayer[THISACTOR].posz z
  ifinwater setvar temp 1
  ifvarg underwater 0 ifvarvarg z peractor2 setvar temp 1
  ifvare temp 1
  {
    ifaction PTREDWATER
    {
      ifp pwalking prunning
        action PSWIMMINGGO
    }
    else
      ifp pstanding pwalkingback prunningback
        action PTREDWATER
    else
    {
      ifaction PSWIMMING
      {
        ifrnd 4
          spawn WATERBUBBLE
        ifactioncount 4
          action PSWIMMINGWAIT
      }
      else
        ifaction PSWIMMINGWAIT
      {
        ifactioncount 2
          action PSWIMMINGGO
      }
      else
        ifaction PSWIMMINGGO
      {
        ifactioncount 2
          action PSWIMMING
      }
      else
        action PTREDWATER
    }

    ifrnd 4
      spawn WATERBUBBLE // For effect

    break
  }
  else ifp pjetpack
  {
    ifaction PJPHOUVER
    {
      ifactioncount 4
        resetactioncount
    }
    else
      action PJPHOUVER
    break
  }
  else
  {
    ifaction PTREDWATER
      action PSTAND
    ifaction PSWIMMING
      action PSTAND
    ifaction PSWIMMINGWAIT
      action PSTAND
    ifaction PSWIMMINGGO
      action PSTAND
    ifaction PJPHOUVER
      action PFALLING
  }

    ifaction PFALLING
  {
    state STOPSTEPSOUNDS
    ifp ponground
      { 
	      action PSTAND 
	      state landsounds
	  }
    else
    {
      ifp pfalling
        break
      else
        state check_pstandard
    }
  }

  ifaction PDUCKING
  {
    state STOPSTEPSOUNDS
    ifgapzl 48
    {
      ifp pwalking pwalkingback prunning prunningback
        action PCRAWLING
    }
    else ifp pducking
    {
      ifp pwalking pwalkingback prunning prunningback
        action PCRAWLING
    }
    else
    {
      ifp pstanding
        action PSTAND
      else
        state check_pstandard
    }
  }

  else ifaction PCRAWLING
  {
    state STOPSTEPSOUNDS
    ifgapzl 48
    {
      ifp pstanding
        action PCRAWLING
    }
    else
      ifp pducking
    {
      ifp pstanding
        action PDUCKING
    }
    else
    {
      ifp pstanding
        action PSTAND
      else
        state check_pstandard
    }
  }
  else
    ifgapzl 48
      action PDUCKING

  else
    ifaction PJUMPING
  {
    state STOPSTEPSOUNDS
    ifp ponground
      action PSTAND
    else
      ifactioncount 4
        ifp pfalling
          action PFALLING
  }


  ifp pfalling
    action PFALLING
  else
    ifaction PSTAND
      state check_pstandard
  else
    ifaction PAKICKING
  {
    ifactioncount 2
      action PSTAND
    break
  }
  else
    ifaction PWALK
  {
    ifp pfalling
      action PFALLING
    else
      ifp pstanding
        action PSTAND
    else
      ifp prunning
        action PRUN
    else
      ifp pwalkingback
        action PWALKBACK
    else
      ifp prunningback
        action PRUNBACK
    else
      ifp pjumping
        action PJUMPING
    else
      ifp pducking
        action PDUCKING
  }

  else
    ifaction PRUN
  {
    ifp pstanding
      action PSTAND
    else
      ifp pwalking
        action PWALK
    else
      ifp pwalkingback
        action PWALKBACK
    else
      ifp prunningback
        action PRUNBACK
    else
      ifp pjumping
        action PJUMPING
    else
      ifp pducking
        action PDUCKING
  }

  else
    ifaction PWALKBACK
  {
    ifp pstanding
      action PSTAND
    else
      ifp pwalking
        action PWALK
    else
      ifp prunning
        action PRUN
    else
      ifp prunningback
        action PRUNBACK
    else
      ifp pjumping
        action PJUMPING
    else
      ifp pducking
        action PDUCKING
  }

  else
    ifaction PRUNBACK
  {
    ifp pstanding
      action PSTAND
    else
      ifp pwalking
        action PWALK
    else
      ifp prunning
        action PRUN
    else
      ifp pwalkingback
        action PWALKBACK
    else
      ifp pjumping
        action PJUMPING
    else
      ifp pducking
        action PDUCKING
  }
  
enda
