
state rollfordrop

// about 4% chance of drop
ifrnd 10
{
	randvar temp 100
	ifvarl temp 18 setvar picnum COLA else
	ifvarl temp 25 setvar picnum SIXPAK else
	ifvarl temp 30 setvar picnum FIRSTAID else
	ifvarl temp 35 setvar picnum ATOMICHEALTH else
	ifvarl temp 42 setvar picnum AMMO else
	ifvarl temp 46 setvar picnum FIRSTGUNSPRITE else
	ifvarl temp 50 setvar picnum SHOTGUNAMMO else
	ifvarl temp 52 { ifrnd 96 setvar picnum DBSPRITE else setvar picnum SHOTGUNSPRITE } else
	ifvarl temp 56 setvar picnum BATTERYAMMO else
	ifvarl temp 58 setvar picnum CHAINGUNSPRITE else
	ifvarl temp 62 setvar picnum RPGAMMO else
	ifvarl temp 64 setvar picnum RPGSPRITE else
	ifvarl temp 68 setvar picnum HEAVYHBOMB else
	ifvarl temp 70 setvar picnum HBOMBAMMO else
	ifvarl temp 73 setvar picnum CRYSTALAMMO else
	ifvarl temp 76 setvar picnum GROWAMMO else
	ifvarl temp 78 setvar picnum SHRINKERSPRITE else
	ifvarl temp 82 setvar picnum DEVISTATORAMMO else
	ifvarl temp 82 setvar picnum DEVISTATORSPRITE else
	ifvarl temp 83 setvar picnum TRIPBOMBSPRITE else
	ifvarl temp 86 setvar picnum FREEZEAMMO else
	ifvarl temp 88 setvar picnum FREEZESPRITE else
	ifvarl temp 90 setvar picnum HOLODUKE else
	ifvarl temp 92 setvar picnum HEATSENSOR else
	ifvarl temp 94 setvar picnum BOOTS else
	ifvarl temp 95 setvar picnum JETPACK else
	ifvarl temp 96 setvar picnum STEROIDS else
	setvar picnum SHIELD
	espawnvar picnum
	setactorvar[RETURN].peractor4 13
	randvar angvar 2047
	setactor[RETURN].ang angvar
}

ends

state randomdrop

ifactor TRIPBOMBSPRITE break

setvar peractor1 20 // peractor1 will be the value of the monster
ifactor PIGCOP addvar peractor1 25
ifactor PIGCOPDIVE addvar peractor1 25
ifactor LIZMAN addvar peractor1 35
ifactor OCTABRAIN addvar peractor1 20
ifactor COMMANDER addvar peractor1 40
ifactor DRONE addvar peractor1 10
ifactor NEWBEAST ifvarn mtype 1 addvar peractor1 30
ifactor EGG subvar peractor1 10
ifactor BOSS1 addvar peractor1 80
ifactor BOSS2 addvar peractor1 80
ifactor BOSS3 addvar peractor1 80
ifactor BOSS4 addvar peractor1 80
ifactor RECON addvar peractor1 20
ifactor RECONDP addvar peractor1 25
ifvarand monstflags 128 addvar peractor1 15
ifvarand monstflags 512 addvar peractor1 10
ifvarand monstflags 1024 addvar peractor1 10
ifvarand monstflags 2048 addvar peractor1 5

ifvarand monstflags 2 { mulvar peractor1 3 divvar peractor1 2 }
ifvarand monstflags 4 mulvar peractor1 2
ifvarand monstflags 8 { mulvar peractor1 3 divvar peractor1 2 }
ifvarand monstflags 64 mulvar peractor1 2 else
ifvarand monstflags 32 { mulvar peractor1 3 divvar peractor1 2 } else
ifvarand monstflags 16 { mulvar peractor1 3 divvar peractor1 2 }

shiftvarr peractor1 3
ifvarl peractor1 2 setvar peractor1 2
setvar botclip 0
whilevarvarn botclip peractor1
{
	state rollfordrop
	addvar botclip 1
}

ends

state activatorstate

setvar countvarb 0
whilevarn countvarb 16384
{
	ifvarn sprite[countvarb].statnum 1024
	{
		getactor[countvarb].picnum picnum
		switch picnum
		//case ACTIVATOR case ACTIVATORLOCKED 
		
		case NOISESPRITE
			getactorvar[countvarb].activator tempc
			ifvarvare tempc activator
			{
				setactorvar[countvarb].mtype 1
				setactorvar[countvarb].activator 0
				changespritestat countvarb 1
			}
		break
		case SPAWNER
			getactorvar[countvarb].lotag tempc
			ifvarvare tempc activator setactorvar[countvarb].mtype 1
		break
		
		case SHOOTER
			getactorvar[countvarb].lotag tempc
			ifvarvare tempc activator 
				setactorvar[countvarb].mtype 1
			else
			{
				addvar tempc 1
				ifvarvare tempc activator
				  setactorvar[countvarb].mtype 0
			}
		break
		
		case SECTOREFFECTOR
			ifvare actorvar[countvarb].lotag 90 // cutscene SE
			ifvarvare actorvar[countvarb].hitag activator
				setvarvar camerasprite countvarb
			else	
			ifvarvare actorvar[countvarb].activator activator 
			{
				ifvarn peractor1 0 setactorvar[countvarb].peractor1 2 else
				setactorvar[countvarb].peractor1 1
				
				// polymer light activation
				ifvarg sprite[countvarb].lotag 48 ifvarl sprite[countvarb].lotag 51
				{
					getactorvar[countvarb].hitag temp
					ifvare temp -1 
					{
						getactorvar[countvarb].intensity tempb
						setactorvar[countvarb].hitag tempb
						setactor[countvarb].hitag tempb
						
						ifvarn actorvar[countvarb].target2 -1
						{
							getactorvar[countvarb].target2 tempb
							getactor[tempb].picnum picnum
							setactor[tempb].picnum actorvar[countvarb].mtype
							setactorvar[countvarb].mtype picnum
						}
									
									
			
			
					}
					else
					{
						setactorvar[countvarb].hitag -1
						setactor[countvarb].hitag 0
				
						ifvarn actorvar[countvarb].target2 -1
						{
							getactorvar[countvarb].target2 tempb
							getactor[tempb].picnum picnum
							setactor[tempb].picnum actorvar[countvarb].mtype
							setactorvar[countvarb].mtype picnum
						}
									
					}
				}
			}
			
		break
		
		case DPTOUCHPLATE
			getactorvar[countvarb].droptile tempc
			ifvarn tempc 0 ifvarvare activator tempc
			setactorvar[countvarb].droptile 0
		
		break
		
		case QUOTER
			getactorvar[countvarb].lotag tempc
			ifvarvare tempc activator 
			{
				setactorvar[countvarb].peractor1 1
				changespritestat countvarb 1
				setactor[countvarb].httimetosleep 768
			}
		break
		
		case TEMPSPRITE
		ifvare actorvar[countvarb].peractor1 0
		{
			ifvarvare activator actorvar[countvarb].hitag
			setactorvar[countvarb].peractor1 1
		}
		else
		{
			ifvarvare activator actorvar[countvarb].lotag
			setactorvar[countvarb].peractor1 2
		}
		break
		
		case HELICOPTER
		case BLACKHAWK
		getactorvar[countvarb].activator tempc
		ifvarvare tempc activator 
		{
			changespritestat countvarb 1
			setactor[countvarb].httimetosleep 768
			setactorvar[countvarb].peractor1 1
		}
		break
		
		case FUSE
		getactorvar[countvarb].topladder tempc
		ifvarvare tempc activator setactorvar[countvarb].topladder 0
		break
		
		case WAYPOINT
		getactorvar[countvarb].target2 tempc
		ifvarvare tempc activator setactorvar[countvarb].target2 0
		break
		default break
		endswitch
	}
	addvar countvarb 1
}
operatemasterswitches activator
operaterespawns activator
operateactivators activator 0
	
ends

state specialdeath // for special events that happen when monsters die

ifmove 0 nullop else
ifvare moretalk YES
ifvare tauntcount 0
{
	ifvarvare sprite[THISACTOR].htowner player[THISACTOR].i
	ifrnd SWEARFREQUENCY ifrnd SWEARFREQUENCY
	ifvarn sprite[THISACTOR].picnum DRONE
	ifvarn sprite[THISACTOR].picnum ROTATEGUN
	ifvarn sprite[THISACTOR].picnum EGG
	ifvarn sprite[THISACTOR].htpicnum RADIUSEXPLOSION
	ifvarn sprite[THISACTOR].htpicnum RPG
	ifvarn sprite[THISACTOR].htpicnum FREEZEBLAST
	{
		ifvare CURRWEAP 2 
		ifrnd 64 
		setvar taunt SUCKMYSTIK else
		{
			randvar temp 19
			switch temp
			case 0 case 1 setvar taunt LEAVESCAR break
			case 2 case 3 setvar taunt ENDSEQVOL3SND2 break
			case 4 case 5 setvar taunt WAR_AMBIENCE7 break
			case 6 case 7 setvar taunt COMEON break
			case 8 case 9 case 10 setvar taunt JUSTDIE break
			case 11 case 12 setvar taunt WASTEDSND break
			case 13 case 14 setvar taunt YOUSUKSND break
			case 15 case 16 setvar taunt HURTS2BU break
			case 17 setvar taunt UGLYSND break
			case 18 setvar taunt LIFESBICH break
			case 19 setvar taunt NEWHOLE break
			default break
			endswitch
		}
		setvar tauntcount 10
	}
}

addvar kickasscount 1
setvar activemonsters -52

// ifvarg intensity 0 
// {
// 	// state exitlightarray
// 	setvar intensity 0
// }

setvar stun 0
setvar monstatus 2 // dead
ifvarn activator 0
{
	ifvarn group 0
	{
		setvarvar temp mongroup[group]
		ifvarg temp 0 subvar temp 1
		setarray mongroup[group] temp
		ifvare temp 0 state activatorstate
	}
	else
	state activatorstate
}
setvar activator 0

ifvarg droptile 10
{
	ifvare droptile 62 { espawn ACCESSCARD  setactor[RETURN].pal 21 } else
	ifvare droptile 63 { espawn ACCESSCARD  setactor[RETURN].pal 23 } else
	espawnvar droptile
	ifactor ROTATEGUN nullop else
	setactorvar[RETURN].peractor4 13
	setvar droptile 0
}

ifvarn randomize 0 	
	state randomdrop 


ends

state gradualturn
// assumes target is set

getactor[target].x mx
getactor[target].y my
getactor[THISACTOR].x x
getactor[THISACTOR].y y
subvarvar mx x
subvarvar my y
getangle angvar mx my
ifactor FIREBALLPROJ addvar angvar 1024
getactor[THISACTOR].ang tempb
getincangle tempd angvar tempb

ifvarl tempd 17 ifvarg tempd -17
{
	setactor[THISACTOR].ang angvar
	break
}
ifactor RECONDP
{
	ifvarl tempd 33 ifvarg tempd -33
	{
		setactor[THISACTOR].ang angvar
		break
	}
}

ifvarl tempd 0 
{
	addvar tempb 16
	ifactor RECONDP addvar tempb 16 
}
else
ifvarg tempd 0 
{
	subvar tempb 16
	ifactor RECONDP subvar tempb 16
}

setactor[THISACTOR].ang tempb

ends

state validatetrail

getactor[spriteid].x mx
getactor[spriteid].y my
getactor[THISACTOR].x x
getactor[THISACTOR].y y
subvarvar mx x
subvarvar my y
getangle angvar mx my


ldist tempc THISACTOR spriteid

getactor[THISACTOR].z z

getactor[spriteid].z zdist
subvarvar zdist z
subvar z 8192
getactor[THISACTOR].sectnum mysector
cos mycos angvar
sin mysin angvar
shiftvarl zdist 14
ifvare tempc 0 setvar tempc 1
divvarvar zdist tempc
setactor[spriteid].cstat 256
setactor[spriteid].xrepeat 128
setactor[spriteid].yrepeat 128
hitscan x y z mysector mycos mysin zdist hitsector hitwall hitsprite hitx hity hitz clipmask

ifvarvare hitsprite spriteid 
{
	setvar tempc 1 
	setactor[spriteid].cstat 32768
	setactor[spriteid].xrepeat 4
	setactor[spriteid].yrepeat 4
	break 
}

addvar z 4096
hitscan x y z mysector mycos mysin zdist hitsector hitwall hitsprite hitx hity hitz clipmask
ifvarvarn hitsprite spriteid
	setvar tempc 0
else setvar tempc 1

setactor[spriteid].cstat 32768
setactor[spriteid].xrepeat 4
setactor[spriteid].yrepeat 4

ends

state avoidwater

ifactornotstayput nullop else break

ifinwater break // too late

getactor[THISACTOR].picnum picnum
switch picnum

case DRONE
case LIZTROOP
case RECONDP
case OCTABRAIN
case COMMANDER
break

default
ifonwater
nullop else // ifvarg sprite[THISACTOR].xvel 0
{
	getactor[THISACTOR].ang angvar
	ifvarl sprite[THISACTOR].xvel -16 addvar angvar 1024 // moving backwards
	// neartag <x> <y> <z> <sect> <ang> <nearTagSector> <nearTagWall> <nearTagSprite> <nearTagHitDist> <nearTagRange> <tagSearch> 
	// <tagSearch> 1-lotag only, 2-hitag only, 3-lotag&hitag 
	neartag sprite[THISACTOR].x sprite[THISACTOR].y sprite[THISACTOR].z sprite[THISACTOR].sectnum angvar hitsector hitwall hitsprite temp 1024 1
	ifvarn hitsector -1
	ifvare sector[hitsector].lotag 1 // water found ahead
	{
		getactor[THISACTOR].xvel xvel
		ifvarg xvel 0 setvar xvel 0
		ifvarg xvel -64 subvar xvel 64
		setactor[THISACTOR].xvel xvel
	}
		
}
break
endswitch

ends

state avoidcliffs

ifcount 10 nullop else break

getactor[THISACTOR].x x
getactor[THISACTOR].y y
getactor[THISACTOR].ang angvar
ifvarl sprite[THISACTOR].xvel -16 addvar angvar 1024 // moving backwards
setvarvar mx x
addvar mx 768

rotatepoint x y mx y angvar mx my
updatesector mx my mysector
ifvarn mysector -1
ifvarvarn mysector sprite[THISACTOR].sectnum
{
	getflorzofslope mysector mx my temp
	getflorzofslope sprite[THISACTOR].sectnum x y tempb
	subvarvar temp tempb
	ifvarg temp 20480 // cliff
	{
		addvar angvar 768
		randvar temp 512
		addvarvar angvar temp
		ifvarl sprite[THISACTOR].xvel -16 // moving backwards
		{
			getactor[THISACTOR].xvel xvel
			mulvar xvel -1
			setactor[THISACTOR].xvel xvel
		}
		setactor[THISACTOR].ang angvar
		
	}
}

ends

state facetarget

ifmove 0 ifspritepal 1 { setvar target -1 break } // frozen

ifvarvare target THISACTOR { setvar target -1 break }

setvar digz 0
ifactor BOTALLY setvar digz 1
ifactor BOTALLYCROUCH setvar digz 1

ifvare sprite[target].statnum 1024
{
	ifvare digz 1 state jib_sounds
	setvar target -1 
	setvar targetlock 0
	break 
}


ifvarn actorvar[target].monstatus 1
{
	setvar target -1 
	setvar targetlock 0
	break 
}

ifactor RECONDP { state gradualturn break }
getactor[target].x mx
getactor[target].y my
getactor[THISACTOR].x x
getactor[THISACTOR].y y
subvarvar mx x
subvarvar my y
getangle angvar mx my
setactor[THISACTOR].ang angvar

ends

state calczdist

getactor[THISACTOR].z z
getactor[target].z mz
setvarvar zdist mz
subvarvar zdist z // zdist now holds how far up or down you would have to go from THISACTOR's position to get to target's position
getactor[target].picnum temp
ifvare temp ROTATEGUN addvar zdist 4096
ifvare temp GREENSLIME addvar zdist 4096
ifvare temp PIGCOPDIVE addvar zdist 4096
ifvare temp LIZTROOPDUCKING addvar zdist 2048
ifvare temp EGG addvar zdist 3072

ifvarl zdist -8192 subvar zdist 4096 // aim for the head when shooting up into windows

ends



action ATROOPSTAND     0  1  5  1  1
action ATROOPGROW    0  1  5  1  1
action ATROOPSTAYSTAND  -2  1  5  1  1
action ATROOPWALKING   0  4  5  1   12
action ATROOPWALKINGBACK 15   4  5   -1   12
action ATROOPRUNNING   0  4  5  1  8
action ATROOPSHOOT    35  1  5  1   30
action ATROOPSHOOTFAST 35 1 5 1 20
action ATROOPJETPACK  40  1  5  1  1
action ATROOPJETPACKILL 40  2  5  1   50
action ATROOPJETPACKILLFAST 40 2 5 1 25
action ATROOPFLINTCH  50  1  1  1  6
action ATROOPJETDEAD  50  1  1  1  6
action ATROOPDYING    50  5  1  1   16
action ATROOPDEAD     54
action ATROOPPLAYDEAD   54
action ATROOPSUFFERDEAD 58  2  1   -4   24
action ATROOPSUFFERING  59  2  1  1   21
action ATROOPDUCK     0  1  5  1  3
action ATROOPDUCKSHOOT  0  2  5  1   25
action ATROOPDUCKSHOOTFAST 0 2 5 1 17
action ATROOPABOUTHIDE  74  1  1  1   25
action ATROOPABOUTBLOW  74  1  1  1   25
action ATROOPHIDE     79  1  1  1   25
action ATROOPREAPPEAR   74  1  1  1   25
action ATROOPFROZEN    0  1  5

move TROOPWALKVELS 72
move TROOPWALKVELSBACK -72
move TROOPJETPACKVELS 64 -84
move TROOPJETPACKILLVELS 192 -38
move TROOPJETDEADVELS 256 -144
move TROOPJETDEADFALL 96 -14
move TROOPRUNVELS 108
move DONTGETUP
move SHRUNKVELS 32

ai AITROOPSEEKENEMY  ATROOPWALKING   TROOPWALKVELS seekplayer
ai AITROOPSEEKPLAYER ATROOPWALKING   TROOPWALKVELS seekplayer
ai AITROOPFLEEING  ATROOPWALKING   TROOPWALKVELS fleeenemy
ai AITROOPFLEEINGBACK ATROOPWALKINGBACK   TROOPWALKVELSBACK faceplayer
ai AITROOPDODGE    ATROOPRUNNING   TROOPRUNVELS  dodgebullet
ai AITROOPSHOOTING   ATROOPSHOOT   STOPPED  faceplayer
ai AITROOPDUCKING  ATROOPDUCK    STOPPED  faceplayer
ai AITROOPJETPACK  ATROOPJETPACK   TROOPJETPACKVELS seekplayer
ai AITROOPSHRUNK   ATROOPWALKING   SHRUNKVELS fleeenemy
ai AITROOPHIDE     ATROOPABOUTHIDE STOPPED faceplayer
ai AITROOPGROW     ATROOPGROW    DONTGETUP faceplayerslow
ai AITROOPMEDIC    ATROOPRUNNING TROOPRUNVELS  geth
ai AITROOPJETDEAD  ATROOPJETDEAD TROOPJETDEADVELS geth getv

action APIGWALK     0  4 5 1 20
action APIGRUN      0  4 5 1 11
action APIGSHOOT    30 2 5 1 58
action APIGCOCK     25 1 5 1 16
action APIGSTAND    30 1 5 1 1
action APIGDIVE     -5 2 5 1 40
action APIGDIVEWAIT -5 1 5 1 40
action APIGDIVESHOOT  0 2 5 1 58
action APIGDYING    55 5 1 1 15
action APIGHIT      55 1 1 1 10
action APIGDEAD     60 1 1 1 1
action APIGFROZEN    0 1 5
action APIGGROW      0


move PIGWALKVELS 72
move PIGRUNVELS 108

ai AIPIGSEEKENEMY APIGWALK PIGWALKVELS seekplayer
ai AIPIGSHOOTENEMY APIGSHOOT STOPPED faceplayer
ai AIPIGFLEEENEMY APIGWALK PIGWALKVELS fleeenemy
ai AIPIGSHOOT APIGSHOOT STOPPED faceplayer
ai AIPIGDODGE APIGRUN PIGRUNVELS dodgebullet
ai AIPIGCHARGE APIGRUN PIGRUNVELS seekplayer
ai AIPIGDIVING APIGDIVE STOPPED faceplayer
ai AIPIGDYING APIGDYING STOPPED geth //faceplayer
ai AIPIGSHRINK APIGWALK SHRUNKVELS fleeenemy
ai AIPIGGROW APIGGROW STOPPED faceplayerslow
ai AIPIGHIT APIGHIT STOPPED faceplayer

action ACOMMBREETH  0  3  5  1  40
action ACOMMFROZEN  0  1  5
action ACOMMSPIN   -5  1  5  1  12
action ACOMMGET   0  3  5  1  30
action ACOMMSHOOT  20  1  5   1 35
action ACOMMABOUTTOSHOOT 20 1 5 1 30
action ACOMMDYING  30  8  1  1  12
action ACOMMDEAD   38  1  1  1  1


move COMMGETUPVELS 128 -64
move COMMGETVELS 128 64
move COMMSLOW 64 24

ai AICOMMWAIT ACOMMBREETH STOPPED faceplayerslow
ai AICOMMGET ACOMMGET COMMGETVELS seekplayer
ai AICOMMSHOOT ACOMMSHOOT STOPPED faceplayerslow
ai AICOMMABOUTTOSHOOT ACOMMABOUTTOSHOOT STOPPED faceplayerslow
ai AICOMMSPIN ACOMMSPIN STOPPED spin
ai AICOMMDYING ACOMMDYING STOPPED geth // faceplayer
ai AICOMMSHRUNK ACOMMGET SHRUNKVELS furthestdir
ai AICOMMGROW ACOMMGET STOPPED furthestdir
ai AICOMMFRISBEE ACOMMSPIN STOPPED spin

action ALIZSTAND     0
action ALIZWALKING     0   4  5  1  15
action ALIZRUNNING     0   4  5  1  11
action ALIZTHINK     20  2  5  1  40
action ALIZSCREAM    30  1  5  1  2
action ALIZJUMP      45  3  5  1  20
action ALIZFALL      55  1  5
action ALIZSHOOTING    70  2  5  1  7
action ALIZDYING     60  6  1  1  15
action ALIZLYINGDEAD   65  1
action ALIZFROZEN     0  1  5
action ALIZFLINTCH 60 1 1 1 6

move LIZWALKVEL 72
move LIZRUNVEL 192
move LIZJUMPVEL 184
move LIZWALKVELBACK -72
move LIZRUNVELBACK  -192
move LIZJUMPVELBACK -184

ai AILIZGETENEMY ALIZWALKING LIZWALKVEL seekplayer
ai AILIZDODGE ALIZRUNNING LIZRUNVEL dodgebullet
ai AILIZCHARGEENEMY ALIZRUNNING LIZRUNVEL seekplayer
ai AILIZFLEENEMY ALIZWALKING LIZWALKVEL fleeenemy
ai AILIZSHOOTENEMY ALIZSHOOTING STOPPED faceplayer
ai AILIZJUMPENEMY ALIZJUMP LIZJUMPVEL jumptoplayer
ai AILIZTHINK ALIZTHINK STOPPED faceplayerslow
ai AILIZSHRUNK ALIZWALKING SHRUNKVELS fleeenemy
ai AILIZGROW ALIZSTAND  STOPPED faceplayerslow
ai AILIZSPIT ALIZSCREAM STOPPED faceplayerslow
ai AILIZDYING ALIZDYING STOPPED geth //faceplayer
ai AILIZHIT ALIZFLINTCH STOPPED geth

move BABYBEASTVEL 128
move NEWBEASTWALKVEL 182
move NEWBEASTRUNVEL 256
move NEWBEASTJUMPVEL 264

action ANEWBEASTSTAND     0
action ANEWBEASTWALKING   10  4  5  1  12
action ANEWBEASTRUNNING   10  4  5  1  8
action ANEWBEASTTHINK     0   2  5  1  40
action ANEWBEASTSCRATCHING  30  3  5  1  20
action ANEWBEASTDYING     72  8  1  1  15
action ANEWBEASTFLINTCH   71  1  1  1  1
action ANEWBEASTLYINGDEAD 79  1  1
action ANEWBEASTSCREAM    50  2  5  1  40
action ANEWBEASTJUMP      80  2  5  1  50
action ANEWBEASTFALL      90  1  5
action ANEWBEASTFROZEN    10  1  5

action ANEWBEASTHANG      0  1  5

action ANEWBEASTHANGDEAD      -1  1  5

ai AINEWBEASTGETENEMY ANEWBEASTWALKING NEWBEASTWALKVEL seekplayer
ai AINEWBEASTDODGE ANEWBEASTRUNNING NEWBEASTRUNVEL dodgebullet
ai AINEWBEASTCHARGEENEMY ANEWBEASTRUNNING NEWBEASTRUNVEL seekplayer
ai AINEWBEASTFLEENEMY ANEWBEASTWALKING NEWBEASTWALKVEL fleeenemy
ai AINEWBEASTSCRATCHENEMY ANEWBEASTSCRATCHING STOPPED faceplayerslow
ai AINEWBEASTJUMPENEMY ANEWBEASTJUMP NEWBEASTJUMPVEL jumptoplayer
ai AINEWBEASTTHINK ANEWBEASTTHINK STOPPED
// ai AINEWBEASTSHRUNK ANEWBEASTWALKING SHRUNKVELS fleeenemy
ai AINEWBEASTGROW ANEWBEASTSTAND  STOPPED faceplayerslow
ai AINEWBEASTDYING ANEWBEASTDYING STOPPED geth // faceplayer
ai AINEWBEASTSHOOT ANEWBEASTSCREAM STOPPED faceplayer
ai AINEWBEASTFLINTCH ANEWBEASTFLINTCH STOPPED faceplayerslow

action ATANKSPIN   0 1 7 1 4
action ATANKSHOOTING 7 2 7 1 10
action ATANKWAIT 0 1 7 1 1
action ATANKDESTRUCT 0 1 7 1 1
action ATANKDEAD 0 1 7 1 1
// move TANKVEL horiz vert
move TANKFORWARD 100

action ABOSS1STAND       0  1  5  1  10
action ABOSS1WALK        0  4  5  1  12
action ABOSS1FROZEN        30 1  5
action ABOSS1RUN         0  6  5  1  5
action ABOSS1SHOOT         30 2  5  1  4
action ABOSS1LOB         40 2  5  1  35
action ABOSS1DYING        50 5  1  1  35
action BOSS1FLINTCH       50 1  1  1  1
action ABOSS1DEAD         55

move PALBOSS1SHRUNKRUNVELS 32
move PALBOSS1RUNVELS 128
move BOSS1WALKVELS 208
move BOSS1RUNVELS 296

ai AIBOSS1WAIT ABOSS1STAND STOPPED geth
ai AIBOSS1SEEKENEMY ABOSS1WALK BOSS1WALKVELS seekplayer
ai AIBOSS1RUNENEMY ABOSS1RUN BOSS1RUNVELS faceplayer
ai AIBOSS1SHOOTENEMY ABOSS1SHOOT STOPPED faceplayer
ai AIBOSS1LOBBED ABOSS1LOB STOPPED faceplayer
ai AIBOSS1DYING ABOSS1DYING STOPPED geth // faceplayer
ai AIBOSS1PALSHRINK ABOSS1WALK PALBOSS1SHRUNKRUNVELS furthestdir

action ABOSS3WALK        0  4  5  1  30
action ABOSS3FROZEN        0  1  5
action ABOSS3RUN         0  4  5  1  15
action ABOSS3LOB        20 4  5  1  50
action ABOSS3LOBBING      30 2  5  1  15
action ABOSS3DYING        40 8  1  1  20
action BOSS3FLINTCH       40 1  1  1  1
action ABOSS3DEAD         48

move PALBOSS3SHRUNKRUNVELS 32
move PALBOSS3RUNVELS 208 //84
move BOSS3WALKVELS 208
move BOSS3RUNVELS 270

ai AIBOSS3SEEKENEMY ABOSS3WALK BOSS3WALKVELS seekplayer
ai AIBOSS3RUNENEMY ABOSS3RUN BOSS3RUNVELS faceplayerslow
ai AIBOSS3LOBENEMY ABOSS3LOB STOPPED faceplayer
ai AIBOSS3DYING ABOSS3DYING STOPPED geth // faceplayer
ai AIBOSS3PALSHRINK ABOSS3WALK SHRUNKVELS furthestdir

action ABOSS2WALK        0  4  5  1  30
action ABOSS2FROZEN        0  1  5
action ABOSS2RUN         0  4  5  1  15
action ABOSS2SHOOT         20 2  5  1  15
action ABOSS2LOB         30 2  5  1  105
action ABOSS2DYING        40 8  1  1  35
action BOSS2FLINTCH       40 1  1  1  1
action ABOSS2DEAD         48

move PALBOSS2SHRUNKRUNVELS 32
move PALBOSS2RUNVELS 256
move BOSS2WALKVELS 192
move BOSS2RUNVELS 256

ai AIBOSS2SEEKENEMY ABOSS2WALK BOSS2WALKVELS seekplayer
ai AIBOSS2RUNENEMY ABOSS2RUN BOSS2RUNVELS faceplayerslow
ai AIBOSS2SHOOTENEMY ABOSS2SHOOT STOPPED faceplayersmart
ai AIBOSS2LOBBED ABOSS2LOB STOPPED faceplayer
ai AIBOSS2DYING ABOSS2DYING STOPPED geth // faceplayer
ai AIBOSS2PALSHRINK ABOSS2WALK SHRUNKVELS furthestdir

action ABOSS4WALK          0 4  5  1  30
action ABOSS4DYING        40 9  1  1  20
action ABOSS4ABOUTTOSHOOT 20 1  5  1  40
action ABOSS4SHOOT        25 2  5  1  10
action ABOSS4LAYIT        50 3  5  1  120
action BOSS4FLINTCH       40 1  1  1  1
action ABOSS4DEAD         49

move BOSS4WALKVELS 128

ai AIBOSS4LAYEGGS ABOSS4WALK BOSS4WALKVELS randomangle geth
ai AIBOSS4SHOOT ABOSS4ABOUTTOSHOOT STOPPED faceplayer
ai AIBOSS4DYING ABOSS4DYING STOPPED geth // faceplayer

action AOCTAWALK    0   3   5   1   15
action AOCTASTAND     0   1   5   1   15
action AOCTASCRATCH   0   4   5   1   15
action AOCTAHIT    30   1   1   1   10
action AOCTASHOOT    20   1   5   1   10
action AOCTADYING    30   8   1   1   17
action AOCTADEAD     38   1   1   1  1
action AOCTAFROZEN    0   1   5

move OCTAWALKVELS 96 -30
move OCTAUPVELS 96 -70
move OCTASTOPPED 0 -30
move OCTAINWATER 96 24

ai AIOCTAGETENEMY AOCTAWALK OCTAWALKVELS seekplayer
ai AIOCTASHOOTENEMY AOCTASHOOT OCTASTOPPED faceplayer
ai AIOCTASCRATCHENEMY AOCTASCRATCH OCTASTOPPED faceplayerslow
ai AIOCTAHIT AOCTAHIT OCTASTOPPED faceplayer
ai AIOCTASHRUNK AOCTAWALK SHRUNKVELS faceplayer
ai AIOCTAGROW AOCTASTAND OCTASTOPPED faceplayerslow
ai AIOCTADYING AOCTADYING OCTASTOPPED geth //faceplayer

move TROOPRUNVELSBACK -108
move OCTAWALKVELSBACK -96 -30
move OCTAUPVELSBACK -96 -70
move PIGWALKVELSBACK -72
move PIGRUNVELSBACK -108
move PALBOSS1RUNVELSBACK -128
move BOSS1WALKVELSBACK -208
move BOSS1RUNVELSBACK -296
move PALBOSS2RUNVELSBACK -84
move BOSS2WALKVELSBACK -192
move BOSS2RUNVELSBACK -256
move PALBOSS3RUNVELSBACK -84
move BOSS3WALKVELSBACK -208
move BOSS3RUNVELSBACK -270
move COMMGETUPVELSBACK -128 -64
move COMMGETVELSBACK -128 64
move COMMSLOWBACK -64 24
move TANKFORWARDBACK -100
move BOSS4WALKVELSBACK -128
move NEWBEASTWALKVELBACK -182
move NEWBEASTRUNVELBACK -256
move NEWBEASTJUMPVELBACK -264

state transitionai

ifai AIBOSS2SEEKENEMY ifawayfromwall { ifpdistl 6144 ai AIBOSS2LOBBED else ai AIBOSS2SHOOTENEMY } else
ifai AIBOSS3SEEKENEMY ifawayfromwall ai AIBOSS3LOBENEMY else
ifai AITROOPDODGE ai AITROOPSHOOTING else
ifai AITROOPSEEKPLAYER { ifrnd 128 { cactor LIZTROOPDUCKING ai AITROOPDUCKING } else ai AITROOPSHOOTING } else
ifai AITROOPSEEKENEMY { ifrnd 128 { cactor LIZTROOPDUCKING ai AITROOPDUCKING } else ai AITROOPSHOOTING } else
ifai AIPIGCHARGE 
{
	ifrnd 128 { cactor PIGCOPDIVE ai AIPIGDIVING } 
	else ai AIPIGSHOOTENEMY
} else
ifai AIPIGSEEKENEMY 
{
	ifrnd 128 { cactor PIGCOPDIVE ai AIPIGDIVING } 
	else ai AIPIGSHOOTENEMY
} else
ifai AICOMMGET ifawayfromwall ai AICOMMSHOOT else
ifai AILIZGETENEMY ai AILIZSHOOTENEMY else
ifai AILIZCHARGEENEMY ai AILIZSHOOTENEMY

ends

state missedtarget

ifvarn hitsprite -1
{
	ifvare sprite[hitsprite].picnum GLASS
		ifvarn actorvar[hitsprite].lotag 0 setvar targetlock 26
	else
	ifvare sprite[hitsprite].statnum 1
	{
		ifvare playerally 0 // enemy
		{
			ifvarn actorvar[hitsprite].playerally 0
			{
				setvar targetlock 26
				setvarvar target hitsprite
			}
			else 
			ifvarn sprite[hitsprite].picnum BOTALLY
			ifvare actorvar[hitsprite].monstatus 1 // move out of the way
			{
				setvar targetlock 0
				setvar target -1
				setactorvar[hitsprite].peractor3 6
				// a countdown timer which is set when the other actor is in the way
				ifrnd 128
				ifactor LIZTROOP ifoutside 
				{
					ifai AITROOPJETPACK nullop else
					ai AITROOPJETPACK
				}
			}
		}
		else
		{
			ifvarn actorvar[hitsprite].playerally 0
			{
				setvar targetlock 0
				setvar target -1
				setactorvar[hitsprite].peractor3 6
				// a countdown timer which is set when the other actor is in the way
				ifrnd 128
				ifactor LIZTROOP ifoutside 
				{
					ifai AITROOPJETPACK nullop else
					ai AITROOPJETPACK
				}
			}
		}
		
	}

}
else ifvarn hitwall -1
{
	getwall[hitwall].picnum picnum
	ifvare picnum GLASS setvar targetlock 26
	
}
ends

state visibilitycheck // ALSO CHECKS FOR TRANSPARENT WALLS

ifvarand sprite[target].cstat 256 nullop else { setvar target -1 setvar targetlock 0 break }

ldist xydist THISACTOR target

state calczdist

getactor[THISACTOR].x x
getactor[THISACTOR].y y
getactor[THISACTOR].z z

subvar z 8192
updatesectorz x y z mysector
ifvare mysector -1
getactor[THISACTOR].sectnum mysector
ifvare mysector -1 break
getactor[THISACTOR].ang angvar
cos mycos angvar
sin mysin angvar
shiftvarl zdist 14
ifvare xydist 0 setvar xydist 1
divvarvar zdist xydist
hitscan x y z mysector mycos mysin zdist hitsector hitwall hitsprite hitx hity hitz clipmask

ifvarvare hitsprite target
{
	ifvarand monstflags 2048 
	{
		ifvare targetlock 0 
			state transitionai
		else
		ifrnd 64 state transitionai
	}
	setvar targetlock 26
	ifvare playerally 0 ifvare sprite[target].picnum APLAYER
	{
		getactor[target].x lastseenx
		getactor[target].y lastseeny
	}
}
else state missedtarget

ifvare playerally 0 break
ifvare target -1 break

addvar z 4096
hitscan x y z mysector mycos mysin zdist hitsector hitwall hitsprite hitx hity hitz clipmask
ifvarvarn hitsprite target
{
	state missedtarget
	ifvarn targetlock 0 break
	ifvarn targetlock 26
	setvar target -1
}
else 
{
	ifvarand monstflags 2048 
	{
		ifvare targetlock 0 
			state transitionai
		else
		ifrnd 64 state transitionai
	}
	setvar targetlock 26
	ifvare playerally 0 ifvare sprite[target].picnum APLAYER
	{
		getactor[target].x lastseenx
		getactor[target].y lastseeny
	}
}
	
ends

state checksight

ifvarn target -1
{
	ifvare actorvar[target].monstatus 2 setvar target -1 else
	{
		getactor[target].cstat temp
		ifvarand temp 256 nullop else setvar target -1
		ifvarand temp 32768 setvar target -1
	}
	ifvare target -1 break
}

ifvarvare target THISACTOR { setvar target -1 break }

ifvarand monstflags 2048
{
	getactor[THISACTOR].ang tempd
	state facetarget
	ifvarn target -1
	state visibilitycheck
	setactor[THISACTOR].ang tempd
	ifvare targetlock 0 setvar target -1
}
else
{
	getactor[THISACTOR].z z
	subvar z 8192
	setactor[THISACTOR].z z
	getactor[target].z mz
	subvar mz 8192
	setactor[target].z mz
	canseespr THISACTOR target tempc
	addvar z 8192
	setactor[THISACTOR].z z
	addvar mz 8192
	setactor[target].z mz
	ifvare tempc 0 setvar target -1
}

ends

state validatetarget

ifvare sprite[target].picnum ROTATEGUN
ifvarn actorvar[target].myspawner -1
getactorvar[target].myspawner target


getactor[THISACTOR].ang tempd
state facetarget
ifvarn target -1
state visibilitycheck
ifvare target -1 setactor[THISACTOR].ang tempd

ends

state generate_location

ifvarn target -1
{
	getactor[target].x mx
	getactor[target].y my
	getactor[target].z mz
} else
{
	getplayer[THISACTOR].posx mx
	getplayer[THISACTOR].posy my
	getactor[THISACTOR].z mz
}
setvarvar xydist mx
randvar temp 5120 // the random component of the distance from player
addvar temp 2048
addvarvar xydist temp
ifvarn target -1 getactor[target].ang angvar else
getplayer[THISACTOR].ang angvar
addvar angvar 512
randvar temp 1024 // the random component of the angle from player
addvarvar angvar temp

rotatepoint mx my xydist my angvar tempb my
// tempb and my are the new x and y coords

setvar tempd 0
updatesectorz tempb my mz mysector
ifvarn mysector -1
{
	// it's in a sector, so now we check for visibility
	espawn NOTHING
	setactor[RETURN].x tempb
	setactor[RETURN].y my
	setvarvar tempc mz
	subvar tempc 4096
	setactor[RETURN].z tempc
	getplayer[THISACTOR].i spriteid
	canseespr spriteid RETURN tempd // tempd is 1 if location is valid
}

ends

state teleport_behind

ifvare target -1 ifvarn playerally 0 break

// first, we look for a valid location to teleport to
// this will be (a) behind the player, (b) in a sector, and (c) a place where the player can be seen from
state generate_location
ifvare tempd 0 state generate_location
ifvare tempd 0 state generate_location
ifvare tempd 0 state generate_location
ifvare tempd 0 state generate_location
ifvare tempd 0 state generate_location // a total of 6 tries

ifvare tempd 1
{
	sound TELEPORTER
	espawn TRANSPORTERSTAR
	ifactor NEWBEAST setactor[RETURN].pal 6 else
	{
		getactor[THISACTOR].pal pal
		setactor[RETURN].pal pal
	}
	getactor[THISACTOR].z z
	subvar z 8192
	setactor[RETURN].z z
	setactor[RETURN].xrepeat 96
	setactor[RETURN].yrepeat 96
	
	setactor[THISACTOR].x tempb
	setactor[THISACTOR].y my
	setactor[THISACTOR].z mz
	changespritesect THISACTOR mysector
	
	sound TELEPORTER
	espawn TRANSPORTERSTAR
	ifactor NEWBEAST setactor[RETURN].pal 6 else
	{
		getactor[THISACTOR].pal pal
		setactor[RETURN].pal pal
	}
	getactor[THISACTOR].z z
	subvar z 8192
	setactor[RETURN].z z
	setactor[RETURN].xrepeat 96
	setactor[RETURN].yrepeat 96
	setvar teleported 130
}
	 
ends


state montargetsearch

ifvarn playerally 0
{
	findnearactorz SHOOTME 4096 16384 target
	ifvarn target -1
	{
		getactorvar[target].myspawner target
		state checksight
	} 
	ifvare target -1
	{
		findnearactorz SHOOTME 16384 65536 target
		ifvarn target -1 
		{
			getactorvar[target].myspawner target
			state checksight
		}
	}
} else
{
	ifvarg canseeplayer 0 ifvarg canshootplayer 0 ifpdistl 8192 ifrnd 160 break
	ifvarg numallies 0
	{
		findnearactorz BOTALLY 4096 16384 target
		ifvarn target -1 state checksight
		ifvare target -1
		findnearactorz BOTALLY 16384 65536 target
		ifvarn target -1 state checksight
		ifvare target -1 
		findnearactorz BOTALLY 32768 131072 target
		ifvarn target -1 state checksight
	}
	ifvare target -1
	ifvarg monallies 0
	{
		
		findnearactorz SHOOTMEALLY 4096 16384 target
		ifvarn target -1
		{
			getactorvar[target].myspawner target
			state checksight
		} 
		ifvare target -1
		{
			findnearactorz SHOOTMEALLY 16384 65536 target
			ifvarn target -1 
			{
				getactorvar[target].myspawner target
				state checksight
			}
		}
	}
}

ends

state troopmedcheck
	getactorvar[spriteid].monstatus tempb
	getactorvar[spriteid].playerally tempc
	ifvarn playerally 0 { ifvare tempc 0 setvar tempb 0 } else
	ifvarn tempc 0 setvar tempb 0
	
	ifvare tempb 1
	{
		setvar temp 0
		getactor[spriteid].picnum picnum
		switch picnum
		case BOTALLY
		case BOTALLYCROUCH
		setvar temp MAXPLAYERHEALTH
		break
		case LIZTROOP
		case LIZTROOPDUCKING
		setvar temp TROOPSTRENGTH
		ifvare sprite[spriteid].pal 21 shiftvarl temp 1
		break
		case PIGCOP
		case PIGCOPDIVE
		setvar temp PIGCOPSTRENGTH
		break
		case LIZMAN
		setvar temp LIZSTRENGTH
		break
		case OCTABRAIN
		setvar temp OCTASTRENGTH
		break
		case COMMANDER
		setvar temp COMMANDERSTRENGTH
		break
		case NEWBEAST
		setvar temp NEWBEASTSTRENGTH
		break
		case BOSS1
		setvar temp BOSS1PALSTRENGTH
		break
		default break
		endswitch
		ifvarn temp 0
		{
			subvar temp 10
			getactor[spriteid].extra tempb
			ifvarvarl tempb temp // ally is wounded
			{
				dist tempc THISACTOR spriteid
				ifvarl tempc 8192
				{
					getactor[THISACTOR].z z
					subvar z 8192
					setactor[THISACTOR].z z
					canseespr THISACTOR spriteid tempd
					addvar z 8192
					setactor[THISACTOR].z z
					ifvare tempd 1 setvarvar peractor7 spriteid
				}
			}
			else ifvarn peractor7 -1 setvar peractor7 -1
		}
	}
	else ifvarn peractor7 -1 setvar peractor7 -1
ends

state troopmedicsearch

ifvarn peractor7 -1 break

headspritestat spriteid 1
whilevarn spriteid -1
{
	state troopmedcheck
	
	nextspritestat spriteid spriteid
	ifvarn peractor7 -1 
	{ 
		setvar target -1 
		setvar spriteid -1 
		ai AITROOPMEDIC
	}
}
ifvare peractor7 -1 state montargetsearch

ends



state monsterai

ifvarg peractor6 0
{
	setvar botdodge 0
	cos xvel peractor5
	sin yvel peractor5
	setvarvar temp peractor6
	ifvarg temp 768 setvar temp 768
	mulvarvar xvel temp
	mulvarvar yvel temp
	shiftvarr xvel 13
	shiftvarr yvel 13
	setvar zvel 0
	movesprite THISACTOR xvel yvel zvel CLIPMASK0 RETURN
	ifvarn RETURN 0
	{
		addvar RETURN 16384
        ifvarl RETURN 16384
        ifvarg RETURN -1 // hit a sprite
        {
	        getactor[RETURN].picnum picnum
	        ifvare picnum GLASS
	        {
		        setactor[RETURN].htextra peractor6
		        setactor[RETURN].htpicnum SHOTSPARK1 //RPG
	        }
        }
    }
	shiftvarr peractor6 1
	ifvarl peractor6 4 setvar peractor6 0
}
ifvare beamhit 0 ifwasweapon POWBEAM setactor[THISACTOR].htpicnum SHOTSPARK1
ifvarg burning 0 state imonfire
ifvare monstatus 2 break

ifvarg stun 0 subvar stun 1
ifvarg canseeplayer 0 subvar canseeplayer 1
ifvarg canshootplayer 0 subvar canshootplayer 1

ifvarg canseeplayer 3 ifvarg canshootplayer 3 nullop else
{
	getactor[THISACTOR].z z
	ifactor ROTATEGUN subvar z 0 else subvar z 3072
	setactor[THISACTOR].z z
	ifcansee setvar canseeplayer 8
	ifcanshoottarget setvar canshootplayer 8
	ifactor ROTATEGUN addvar z 0 else
	addvar z 3072
	setactor[THISACTOR].z z
	
	ifvarn sprite[THISACTOR].picnum PIGCOPDIVE ifvarn sprite[THISACTOR].picnum ROTATEGUN
	{
		ifvarl canseeplayer 8
		{
			subvar z 8192
			setactor[THISACTOR].z z
			ifcansee setvar canseeplayer 8
			addvar z 8192
			setactor[THISACTOR].z z
		}
		ifvarl canshootplayer 8
		{
			subvar z 8192
			setactor[THISACTOR].z z
			ifcanshoottarget setvar canshootplayer 8
			addvar z 8192
			setactor[THISACTOR].z z
		}
	}
}



ifvarand monstflags 8192 
state avoidcliffs

ifvarand monstflags 4096 // move backwards flag
{
  setvar peractor1 26
  ifvarand monstflags 8192
  ifrnd 16 setvar peractor1 0
  ifvare bouncesound -1
  ifvarg canseeplayer 0
  {
	ifmove TROOPWALKVELS move TROOPWALKVELSBACK faceplayer else
	ifmove TROOPRUNVELS move TROOPRUNVELSBACK faceplayer else
	ifmove LIZWALKVEL move LIZWALKVELBACK faceplayer else
	ifmove LIZRUNVEL move LIZRUNVELBACK faceplayer else
	ifmove LIZJUMPVEL move LIZJUMPVELBACK jumptoplayer else
	ifmove OCTAWALKVELS move OCTAWALKVELSBACK faceplayer else
	ifmove OCTAUPVELS move OCTAUPVELSBACK faceplayer else
	ifmove PIGWALKVELS move PIGWALKVELSBACK faceplayer else
	ifmove PIGRUNVELS move PIGRUNVELSBACK faceplayer else
	ifmove PALBOSS1RUNVELS move PALBOSS1RUNVELSBACK faceplayer else
	ifmove BOSS1WALKVELS move BOSS1WALKVELSBACK faceplayer else
	ifmove BOSS1RUNVELS move BOSS1RUNVELSBACK faceplayer else
	ifmove PALBOSS2RUNVELS move PALBOSS2RUNVELSBACK faceplayer else
	ifmove BOSS2WALKVELS move BOSS2WALKVELSBACK faceplayer else
	ifmove BOSS2RUNVELS move BOSS2RUNVELSBACK faceplayer else
	ifmove PALBOSS3RUNVELS move PALBOSS3RUNVELSBACK faceplayer else
	ifmove BOSS3WALKVELS move BOSS3WALKVELSBACK faceplayer else
	ifmove BOSS3RUNVELS move BOSS3RUNVELSBACK faceplayer else
	ifmove BOSS4WALKVELS move BOSS4WALKVELSBACK faceplayer else
	ifmove COMMGETUPVELS { ifai AICOMMSPIN nullop else move COMMGETUPVELSBACK faceplayer } else
	ifmove COMMGETVELS { ifai AICOMMSPIN nullop else move COMMGETVELSBACK faceplayer } else
	ifmove COMMSLOW move COMMSLOWBACK faceplayer else
	ifmove TANKFORWARD move TANKFORWARDBACK faceplayer else
	ifmove NEWBEASTWALKVEL move NEWBEASTWALKVELBACK faceplayer else
	ifmove NEWBEASTRUNVEL move NEWBEASTRUNVELBACK faceplayer else
	ifmove NEWBEASTJUMPVEL move NEWBEASTJUMPVELBACK jumptoplayer
  }
	
}

setvar activemonsters 52 // just means that there are active monsters
ifai AITROOPHIDE break

ifmove 0 nullop else
ifmove SHRUNKVELS nullop else
ifvarand monstflags 2048 // smart tactics
// ifvare playerally 0
{
	state avoidwater
	
	ifactor LIZTROOP ifvare canseeplayer NO
	ifai AITROOPSEEKPLAYER
	ifnotmoving
	{
		ifceilingdistl 128 nullop else
		ai AITROOPJETPACK
	}
	ifvarn peractor3 0 
	{
		ifvarg peractor3 0
		{
			subvar peractor3 1
			ifvare peractor3 0 ifvarand monstflags 4096 subvar monstflags 4096
		}
		else // dodge pipebomb!
		{
			ifvarand monstflags 128 ifvare teleported 0 state teleport_behind
			setvar teleported 130
			setvar peractor3 0
			ifvarand monstflags 4096 subvar monstflags 4096
			getactor[THISACTOR].ang angvar // save angle
			ifai AIPIGDODGE nullop else
			{
				ifactor PIGCOP ai AIPIGDODGE else
				ifactor PIGCOPDIVE { cactor PIGCOP ai AIPIGDODGE }
			}
			ifai AITROOPDODGE nullop else
			{
				ifactor LIZTROOP ai AITROOPDODGE else
				ifactor LIZTROOPDUCKING { cactor LIZTROOP ai AITROOPDODGE }
			}
			ifactor LIZMAN { ifai AILIZDODGE nullop else ai AILIZDODGE } else
			ifactor COMMANDER { ifai AICOMMFRISBEE nullop else ifvare playerally 0 ai AICOMMFRISBEE } else
			ifactor NEWBEAST { ifai AINEWBEASTDODGE nullop else ai AINEWBEASTDODGE }
			
			ifvarl sprite[THISACTOR].xvel 128 setactor[THISACTOR].xvel 128
			setactor[THISACTOR].ang angvar
			
		}
	}
	ifvarn playerally 0
	{
		ifvarn target -1 ifvarl targetlock 13 state checksight
	}
	else
	{
		ifvare target -1 setvarvar target player[THISACTOR].i
		ifvarl targetlock 13
		state checksight
		ifvarvare target player[THISACTOR].i setvar target -1
	}
	ifvarg targetlock 0 subvar targetlock 1
	ifvare targetlock 0 { setvar target -1 setvar countvarb 26 }
	
	ifvarl ammo 200 ifrnd 16 addvar ammo 1
	ifaction ATROOPSHOOT action ATROOPSHOOTFAST
	ifaction ATROOPJETPACKILL action ATROOPJETPACKILLFAST
	ifaction ATROOPDUCKSHOOT action ATROOPDUCKSHOOTFAST

	ifvarg botdodge 0
	{
		ifvare weapcount 1 nullop else
		{
			ifmove 0 setvar botdodge 0
			ifmove STOPPED setvar botdodge 0
		}
		ifvarg botdodge 0
		{
			getplayer[THISACTOR].posx mx
			getplayer[THISACTOR].posy my
			getactor[THISACTOR].x x
			getactor[THISACTOR].y y
			subvarvar mx x
			subvarvar my y
			getangle angvar mx my
			setactor[THISACTOR].ang angvar
			cos xvel peractor5
			sin yvel peractor5
			shiftvarr xvel 7
			shiftvarr yvel 7
			setvar temp 0
			ifactor LIZTROOP { shiftvarr xvel 1 shiftvarr yvel 1 }
			ifactor LIZMAN { mulvar xvel 2 divvar xvel 3 mulvar yvel 2 divvar yvel 3 }
			movesprite THISACTOR xvel yvel 0 CLIPMASK0 RETURN
			subvar botdodge 1
			ifvare botdodge 0 
			{
				ifvare weapcount 1 ifactor COMMANDER ifvare playerally 0 ai AICOMMFRISBEE 
				setvar weapcount 0
			}
		}
	}

 	getplayer[THISACTOR].i spriteid

	ifvarg targetlock 0
	{
//		ifvarn initshade -1 setactor[THISACTOR].htactorstayput -1
// 		ifvarand monstflags 4096
// 		{
// 			ifnotmoving 
// 			{ 
// 				ifmove STOPPED nullop else
// 				{ 
// 					subvar monstflags 4096 
// 					getactor[THISACTOR].xvel xvel
// 					ifvarl xvel 0 mulvar xvel -1
// 					setactor[THISACTOR].xvel xvel
// 				}
// 			}
// 			else
// 			ifpdistg 6144 ifrnd 4 
// 			{
// 				subvar monstflags 4096 
// 				getactor[THISACTOR].xvel xvel
// 				ifvarl xvel 0 mulvar xvel -1
// 				setactor[THISACTOR].xvel xvel
// 			}
// 			else
// 			{
// 				setvar temp 0
// 				ifvarn smokecount 0 setvar temp 1
// 				ifvarn FAKERELOAD 0 setvar temp 1
// 				ifvare CURRWEAP 5 setvar temp 1
// 				ifvare CURRWEAP 10 setvar temp 1
// 				ifvare CURRWEAP 0 setvar temp 1
// 				ifvare CURRWEAP 1 ifvarn akimbo 1 setvar temp 1
// 				ifphealthl 40 setvar temp 1
// 				ifvarvarl sprite[spriteid].extra sprite[THISACTOR].extra setvar temp 1
// 				ifvarn superkicking 0 setvar temp 0
// 				ifvarn mantling 0 setvar temp 1
// 				ifvare temp 1 
// 				{
// 					subvar monstflags 4096
// 					getactor[THISACTOR].xvel xvel
// 					ifvarl xvel 0 mulvar xvel -1
// 					setactor[THISACTOR].xvel xvel
// 				}
// 				
// 			}
// 			
// 		}
		
		setvar temp 0
		ifvarg peractor3 0 setvar temp 1 else
		ifvare playerally 0
		ifp pfacing setvar temp 1
		ifvarand monstflags 4096 setvar temp 0
		ifvarn weapcount -1 ifvare botdodge 0 ifvare peractor6 0
		ifvare temp 1
		iffloordistl 8
		{
			setvar temp 0
			ifvarvare scannedsprite THISACTOR setvar temp 1 else
			ifrnd 3 setvar temp 1 else
			ifvarg peractor3 0 setvar temp 1
			ifvare temp 1
			{
				ifactor BOSS1 nullop else
				ifactor TANK nullop else
				ifactor BOSS4 nullop else
				ifactor OCTABRAIN nullop else
				ifactor PIGCOP nullop else
				ifangdiffl 512 nullop else
				{
					ifmove STOPPED nullop else
					ifmove 0 nullop else
					{
						getactor[THISACTOR].ang peractor5
						setvar temp 448
						randvar tempb 128
						addvarvar temp tempb
						ifrnd 128 mulvar temp -1
						addvarvar peractor5 temp
						setvar botdodge 26
					}
				}
			}
		}
	}
	else
	{
		ifactor PIGCOP ifvare ammo 200
		ifvarn bouncesound -1 ifvare playerally 0
		{
			// face player's last known position
			ifvarvarn lastseenx lastseeny
			{
				setvarvar mx lastseenx
				setvarvar my lastseeny
				getactor[THISACTOR].x x
				getactor[THISACTOR].y y
				subvarvar mx x
				subvarvar my y
				getangle angvar mx my
				setactor[THISACTOR].ang angvar
			}
			sound GRAVSHOOT 
			zshoot -2048 GASGRENADE 
			subvar ammo 50
		}
// 		ifactor NEWBEAST nullop else
// 		ifp pfacing ifactornotstayput
// 		ifvare playerally 0
// 		iffloordistl 8
// 		{
// 			setvar temp 0
// 			ifvarn smokecount 0 setvar temp 1 else
// 			ifvarn FAKERELOAD 0 setvar temp 1 else
// 			ifvare CURRWEAP 1 
// 			{
// 				ifvarn akimbo 1 setvar temp 1
// 				ifvarl player[THISACTOR].ammo_amount 1 24 setvar temp 1
// 			} else
// 			ifvare CURRWEAP 2 ifvarl player[THISACTOR].ammo_amount 2 7 setvar temp 1 else
// 			ifvare CURRWEAP 3 ifvarl player[THISACTOR].ammo_amount 3 30 setvar temp 1 else
// 			ifvare CURRWEAP 5 setvar temp 1 else
// 			ifvare CURRWEAP 7 ifvarl player[THISACTOR].ammo_amount 7 12 setvar temp 1 else
// 			ifvare CURRWEAP 8 setvar temp 1 else
// 			ifvare CURRWEAP 9 ifvarl player[THISACTOR].ammo_amount 9 8 setvar temp 1 else
// 			ifvare CURRWEAP 10 setvar temp 1 else
// 			ifvare CURRWEAP 0 setvar temp 1

// 			ifphealthl 40 setvar temp 1
// 			ifvarvarl sprite[spriteid].extra sprite[THISACTOR].extra setvar temp 1
// 			ifvarn mantling 0 setvar temp 1
// 			ifvare temp 0
// 			{
// 				getplayer[THISACTOR].i spriteid
// 				ldist xydist THISACTOR spriteid
// 				ifvarl xydist 5120
// 				{
// 					setvar bouncesound -1
// 					orvar monstflags 4096
// 					// getactor[THISACTOR].sectnum temp
// 					// ifvarg temp -1 ifvarl temp 4095 setactor[THISACTOR].htactorstayput temp
// 				}
// 			}
// 		}
		
		ifactornotstayput
		{
			// use player waypoints to follow him
			// player trail code

			ifvarn bouncesound -1
			{
				// ifvarand monstflags 4096 subvar monstflags 4096
				ifvare ptrailtimer 0 ifvarg bouncesound 0 subvar bouncesound 1
				ifvarg bouncesound 99 setvar bouncesound -1
				setvarvar spriteid ptrails[bouncesound]
				ifvare spriteid -1 setvar bouncesound -1 else
				ifvarn sprite[spriteid].picnum PLAYERTRAIL setvar bouncesound -1
				
				ifvarn bouncesound -1
				{
					ldist tempd THISACTOR spriteid
					ifvarl tempd 512 
					{
						addvar bouncesound 1
						setvarvar tempb bouncesound
						whilevarn tempb 100
						{
							setvarvar spriteid ptrails[tempb]
							ifvare spriteid -1 { setvar bouncesound -1 setvar tempb 100 } else
							ifvarn sprite[spriteid].picnum PLAYERTRAIL { setvar bouncesound -1 setvar tempb 100 }
							ifvarn bouncesound -1
							{
								ldist tempd THISACTOR spriteid
								ifvarl tempd 384 
								{
									addvar tempb 1 
									ifvare tempb 100 setvar bouncesound -1
									// newbeast and lizman should jump here if the waypoint tells them
								}
								else { setvarvar bouncesound tempb setvar tempb 100 }
							}
						}
						ifvarg bouncesound 99 setvar bouncesound -1 
					}
					else
					{
						ifnotmoving
						ifvarn weapcount -1 ifvare botdodge 0 ifvare peractor6 0
						{
							getactor[THISACTOR].ang peractor5
							setvar temp 512 ifrnd 128 mulvar temp -1
							addvarvar peractor5 temp
							setvar botdodge 26
						}
						getactor[spriteid].x mx
						getactor[spriteid].y my
						getactor[THISACTOR].x x
						getactor[THISACTOR].y y
						subvarvar mx x
						subvarvar my y
						getangle angvar mx my
						
						ifvare target -1
						setactor[THISACTOR].ang angvar
						ifvarand monstflags 4096 
						{
							subvar monstflags 4096 
							getactor[THISACTOR].xvel xvel
							ifvarl xvel 0 mulvar xvel -1
							setactor[THISACTOR].xvel xvel	
						}	
						
						ifmove STOPPED nullop else
						ifvarg canseeplayer 0 nullop else
						{
							ifactor PIGCOP setvar temp 192 // max speed
							else ifactor OCTABRAIN setvar temp 192
							else setvar temp 256
							getactor[THISACTOR].xvel xvel
							ifvarg xvel -1 ifvarvarl xvel temp
							setactor[THISACTOR].xvel temp
						}				
					}
					ifnotmoving ifrnd 32 ifvarn bouncesound -1
					{
						operate
						ifrnd 64 iffloordistl 8
						{
							ifactor LIZMAN ai AILIZJUMPENEMY
							ifactor NEWBEAST ai AINEWBEASTJUMPENEMY
						}
						state validatetrail
						ifvare tempc 0 setvar bouncesound -1
					}
				}
			}

			else ifvarand monstflags 4096 nullop else
			ifvare target -1
// 			// find the player trail
// 		 	{
// 				setvar temp 99
// 				whilevarn temp -1
// 				{
// 					setvarvar spriteid ptrails[temp]
// 					ifvarn spriteid -1 ifvarn sprite[spriteid].picnum PLAYERTRAIL setvar spriteid -1
// 					ifvarn spriteid -1
// 					{
// 						state validatetrail
// 						ifvare tempc 1
// 						{
// 							setvarvar bouncesound temp
// 							setvar temp 0
// 						}
// 					}
// 					subvar temp 1
// 				}
// 		 	}
		 	
			// find the player trail
		 	{
				setvarvar spriteid ptrails[lasttrail]
				ifvarn spriteid -1 ifvarn sprite[spriteid].picnum PLAYERTRAIL setvar spriteid -1
				ifvarn spriteid -1
				{
					state validatetrail
					ifvare tempc 1
					{
						setvarvar bouncesound lasttrail
						setvar lasttrail 100
					}
				}
				subvar lasttrail 1
				ifvare lasttrail -1 setvar lasttrail 99
		 	}
		 	
		 	// code to make monster back away from wall if player above
		 	getactor[THISACTOR].picnum picnum
		 	switch picnum
		 	case PIGCOP case LIZMAN case NEWBEAST
		 	case BOSS1 case BOSS2 case BOSS3
			getactor[THISACTOR].htmovflag hitwall
			andvar hitwall 49152
			ifvare hitwall 32768
			ifvare target -1
		    {
			    getplayer[THISACTOR].posz digz
			    addvar digz 32768
			    ifvarvarg sprite[THISACTOR].z digz
			    {
				    getplayer[THISACTOR].i spriteid
				    ldist xydist THISACTOR spriteid
				    ifvarl xydist 6144
				    {
					    setvar bouncesound -1
						orvar monstflags 4096
						setvar peractor3 208
					}
				}
			}
			break
			case RECONDP ifvarn myspawner -1 setvar bouncesound -1 break
			default break
			endswitch
		}
	}
	
}

	ifonwater
	{
		setvar temp 0
		ifactor LIZMAN setvar temp 1
		ifactor PIGCOP setvar temp 1
		ifactor NEWBEAST setvar temp 1
		ifactor LIZTROOP setvar temp 1
		ifvare temp 1
		{
			getactor[THISACTOR].htflags temp
			orvar temp 16
			setactor[THISACTOR].htflags temp			
//			cstator 128	
			ifvarand morefx 2
			{
				ifmove STOPPED spawn WATERCIRCLE else spawn NEWSPLASH
			}
			else ifmove STOPPED nullop else spawn WATERSPLASH2
 			
		}
	}
	else
	{
		getactor[THISACTOR].htflags temp
		ifvarand temp 16 subvar temp 16
		setactor[THISACTOR].htflags temp
	}

ifvarl sprite[THISACTOR].yrepeat 10 // shrunk
{
	ifvarn playerally 0
		findnearactorz SHOOTME 1280 8192 spriteid
	else
	{
		findnearactorz SHOOTMEALLY 1280 8192 spriteid
		ifvare spriteid -1 findnearactorz BOTALLY 1024 8192 spriteid
	}
	ifvarn spriteid -1
	{
		canseespr THISACTOR spriteid temp
		ifvare temp 1
		{
			state specialdeath
			sound SQUISHED
			state standard_jibs
			killit
		}
	}
}

ifvarg superkicking 0 ifvare kickimpact 0
{	
	ifp pfacing ifpdistl 1280 
	ifvare spinkick 0
	ifvare kicktarg -1 setvarvar kicktarg THISACTOR
	
	ifvare sprite[THISACTOR].htpicnum KNEE
	ifvarg sprite[THISACTOR].htextra 0
	{
		ifpdistl 1792
		ifvarg superkicking 7
		{
			ifvare spinkick 0 setvar kickimpact 1
			sound WHACK
			setactor[THISACTOR].htpicnum KNEE
			getactor[THISACTOR].htextra temp
			addvar temp 40
			setactor[THISACTOR].htextra temp
			
			getplayer[THISACTOR].i spriteid
			setactor[THISACTOR].htowner spriteid	
		}
			
		
	}
}

ifvarn playerally 0 
{
	setvar monallies 10
	ifvarg playerally 0 { subvar playerally 1 ifvare playerally 0 setvar target -1 }
}
addvar countvarb 1
addvar target2 1
ifvarg target2 25
{
	setvar target2 0
	ifvare player[THISACTOR].heat_on 0 ifspritepal 66 nullop else 
	ifvare playerally 0
	{
		espawn SHOOTME
		setactorvar[RETURN].myspawner THISACTOR
	}
	ifvarn playerally 0
	{
		espawn SHOOTMEALLY
		setactorvar[RETURN].myspawner THISACTOR
		setactorvar[RETURN].lotag 1
	}
}

getactor[THISACTOR].extra hp

ifvare monstatus 0
{
	ifvare smartmonsters YES 
	{
		orvar monstflags 2048
		ifactornotstayput setvar initshade 0
		else setvar initshade -1
		ifactor PIGCOP setvar ammo 180
		ifactor PIGCOPDIVE setvar ammo 180
		ifactor PIGCOPSTAYPUT setvar ammo 180
	}
	ifvarand monstflags 2048
	{
		ifactor ROTATEGUN subvar monstflags 2048 else
		ifactor RECONDP subvar monstflags 2048 else
		ifactor EGG subvar monstflags 2048
	}
	ifvarl hp 0 setvar hp 0
	ifvare lifebars YES
	ifvarn sprite[THISACTOR].picnum ROTATEGUN
	{
		espawn LIFEBAR
		setactorvar[RETURN].myspawner THISACTOR
	}
	randvar countvarb 25
	setvar monstatus 1
	ifvarand monstflags 16384 setvar playerally -1
	ifvarn playerally 0 { espawn SHOOTMEALLY setactorvar[RETURN].lotag 1 } else
	espawn SHOOTME
	setactorvar[RETURN].myspawner THISACTOR
	getactor[THISACTOR].x initx
	getactor[THISACTOR].y inity
	getactor[THISACTOR].z initz
	ifvarand monstflags 4
	{
		mulvar hp 3 setactor[THISACTOR].extra hp
	} 
	ifvarand monstflags 2
	{
		mulvar hp 2 setactor[THISACTOR].extra hp
	}
	
	randvar temp 100
	ifvarvarl temp killerpigs
	{
		ifactor PIGCOP { orvar monstflags 256 ifspritepal 0 spritepal 16 }
		ifactor PIGCOPDIVE { orvar monstflags 256 ifspritepal 0 spritepal 16 }
	}
	
	ifvarg hp 32767 setactor[THISACTOR].extra 32767
}

getactor[THISACTOR].htextra bleeding
ifvarg bleeding 0
{
	getactor[THISACTOR].htpicnum picnum
	getactor[THISACTOR].htowner spriteid
	ifvarn spriteid -1
	{
		getactor[spriteid].picnum tempb
		ifvare tempb APLAYER
		{
			ifvarand monstflags 2048 ifvare playerally 0 ifvarl targetlock 20 addvar targetlock 20
			ifvare picnum SHOTSPARK1 ifvare CURRWEAP 9 ifvare WEAPON9_SHOOTS ICEBEAM 
			{ setactor[THISACTOR].htpicnum FREEZEBLAST setvar picnum FREEZEBLAST }
			ifvarg bleeding 80 ifvare CURRWEAP 2 ifvarand ALTSHOTGUN 4
			{
				setvar tempb 0
				ifactor DRONE setvar tempb 1
				ifactor TANK setvar tempb 1
				ifactor RECONDP setvar tempb 1
				ifactor ROTATEGUN setvar tempb 1
				ifvare tempb 1
				{
					debris SCRAP1 4
					debris SCRAP2 2
					debris SCRAP3 3
				} else
				guts JIBS6 10
			}
				
			ifvare altshrinker 1 ifvare picnum SHRINKSPARK
			{
				ifvarn playerally -1 ifvarl hp 600
				{
					setvarvar tempc hp
					shiftvarl tempc 3
					ifvarg tempc 1300 setvar tempc 1300
					addvar playerally 1560
					subvarvar playerally tempc
				}
				setvar picnum SHOTSPARK1
				setactor[THISACTOR].htpicnum SHOTSPARK1
				setvar bleeding 0
				setactor[THISACTOR].htextra 0
				setvar target -1
				setvar countvarb 26
			}
			ifvarn pdamage 100
			{
				mulvarvar bleeding pdamage
				divvarvar bleeding 100
				setactor[THISACTOR].htextra bleeding
			}
			ifvarand ppowers 4 // stun
			ifvarn picnum BURNING
			ifvarn picnum BULLETWOUND
			{
				setvarvar tempb bleeding
				shiftvarr tempb 2
				addvar tempb 3
				addvarvar stun tempb
				ifvarg stun 60 setvar stun 60
			}
		} else
		{
			ifvarand monstflags 256 ifvarvare tempb sprite[THISACTOR].picnum
			{
				setvar bleeding 0
				setactor[THISACTOR].htextra -1
				setactor[THISACTOR].htpicnum -1
			}
			else
			{
				ifvare target -1 ifvarvarn spriteid THISACTOR 
				ifvare playerally 0
				ifvarg infighting 0
				ifvarand monstflags 2048
				{
					randvar temp 100
					ifvarvarl temp infighting
					setvarvar target spriteid 	
				}
				
				ifvarn mdamage 100
				{
					mulvarvar bleeding mdamage
					divvarvar bleeding 100
					setactor[THISACTOR].htextra bleeding
				}
			}
			ifvarvare spriteid THISACTOR
			{
				// no self damage
				setvar bleeding 0
				setactor[THISACTOR].htextra -1
			}
		}
	}
	
	switch picnum
	
	case MORTER2
	setvar picnum RPG
	setactor[THISACTOR].htpicnum RPG
	getactor[THISACTOR].xvel xvel
	mulvar xvel -1
	setactor[THISACTOR].xvel xvel
	break
	
	case RPGNOAIM
	case RPG
	case RADIUSEXPLOSION
	// knock back direction code
	getactor[THISACTOR].xvel xvel
	mulvar xvel -1
	setactor[THISACTOR].xvel xvel
	ifvare picnum RPGNOAIM { setvar picnum RPG setactor[THISACTOR].htpicnum RPG }
	break
	case KNEE
	ifvarn ALTMELEE 0 soundonce WHACK
	ifvare steroidkicks YES
	{
		ifp ponsteroids
		{
			mulvar bleeding 4
			setactor[THISACTOR].htextra bleeding
		}
	}
	break
	case ICESHARD
	ifspritepal 1 nullop else
	sound HITFLESH
	break
	case FREEZENOAIM
	setvar picnum FREEZEBLAST
	setactor[THISACTOR].htpicnum FREEZEBLAST
	break
	
	case SAWBLADEPROJ
	setvar picnum RADIUSEXPLOSION
	setactor[THISACTOR].htpicnum RADIUSEXPLOSION
	break
	default break
	endswitch
	
	ifvarg bleeding 0
	{
		setvarvar tempb knockdist
		ifvarvarg bleeding hp ifvarn droptile 0 { mulvar bleeding 3 ifvarl bleeding 30 setvar bleeding 40 }
		ifvare CURRWEAP 11 ifvare gravgun YES ifwasweapon SHOTSPARK1 setvar bleeding 200
		ifactor SHARK setvar tempb 0
		ifvarvare sprite[THISACTOR].htowner player[THISACTOR].i ifvarn ONCAR 0 setvar tempb 0
		ifvarn tempb 0
		ifvarn picnum FREEZEBLAST
		ifvarn picnum ICESHARD
		{
			
			ifactor ROTATEGUN nullop else
			{
				getactor[THISACTOR].htang peractor5 // the angle monster will move in
				setvarvar peractor6 bleeding
				mulvarvar peractor6 tempb // peractor6 is the knockback force
				setvarvar temp hp
				shiftvarl temp 1
				subvarvar peractor6 temp
				setvar tempc 0
				ifactor BOSS1 setvar tempc 1
				ifactor BOSS2 setvar tempc 1
				ifactor BOSS3 setvar tempc 1
				ifactor BOSS4 setvar tempc 1
				ifactor TANK setvar tempc 1
				ifvare tempc 1
				{
					ifvare picnum SHOTSPARK1 shiftvarr peractor6 1
					shiftvarr peractor6 1
				}
				ifvarl peractor6 0 setvar peractor6 0
			}
		}
		else setvar botdodge 0
		
		// weapon resistance code
	
		ifactor NEWBEAST ifvare picnum SPIT 
		{
			setvar bleeding 0
			setactor[THISACTOR].htextra -1
		}
		
		ifvarand monstflags 512 // explosives resistance
		{
			switch picnum
			case RPG case RADIUSEXPLOSION case FIREBALLPROJ
				ifactor OCTABRAIN ifvare picnum RPG setactor[THISACTOR].htpicnum RADIUSEXPLOSION
				divvar bleeding 4
				ifvarl bleeding 1 setvar bleeding 1
				setactor[THISACTOR].htextra bleeding
			break
			default break
			endswitch
		}
		ifvarand monstflags 1024 // energy weapon immunity
		{
			switch picnum
			case SHRINKSPARK case GROWSPARK case FIRELASER case FIREBULLET
				setvar bleeding 0
				setactor[THISACTOR].htpicnum -1
				setactor[THISACTOR].htextra -1
			break
			default break
			endswitch
		}
	}

}

ifmove 0 break
ifmove SHRUNKVELS break
ifvare target -1 ifvarg countvarb 25 
{
	setvar countvarb 0
	ifactor LIZTROOP ifspritepal 0
	ifvare smartmonsters YES 
	ifvarg ammo 50 iffloordistl 16
	ifvarg sprite[THISACTOR].xvel 16
	state troopmedicsearch
	else state montargetsearch
}
ifvarn target -1 
{
	ifai AICOMMSPIN nullop else
	ifai AICOMMFRISBEE nullop else
	state facetarget
	ifvarn target -1
	ifrnd 8 ifvarg countvarb 52
	{
		ifvarg canseeplayer 0 ifvarg canshootplayer 0 setvar target -1 else
		ifvare targetlock 0 state checksight
		
	}
	ifvarn target -1
	{
		dist xydist THISACTOR target
		ifvare xydist 0 setvar xydist 1
	}
	ifvarvare target THISACTOR setvar target -1
}


ifvarg teleported 0 subvar teleported 1
ifmove 0 nullop else ifvarand monstflags 128 ifvare monstatus 1 // teleporting
{
	ifvare teleported 0
	{
		ifpdistl 10240
		//ifvarvare scannedsprite THISACTOR
		ifp pfacing
		ifvarg canseeplayer 0
		{
			ifrnd 4
				state teleport_behind
			else 
			{
				getactor[THISACTOR].ang angvar
				ifbulletnear
				{
					setactor[THISACTOR].ang angvar
					state teleport_behind
				}
			}
			
		}
	}
}


ends

move RECONDYINGVELS 192 16
move RECONPATROLVELS 256 0
move RECONRUNVELS 320 0
move RECONRUNUPVELS 320 0
move RECONBULLVELS 320 0
move RECONBACKWARDS -160 0
move RECONRISE 32 -128
move RECONSHRUNKVELS 48

action RECONFLY -6 1 7 1 1
action RECONTURN 1 1 7 1 1

ai AIRECONGETE RECONFLY RECONRUNVELS geth getv
ai AIRECONWAIT RECONFLY STOPPED geth
ai AIRECONGETUP RECONFLY RECONRUNUPVELS geth getv
ai AIRECONPULLBACK RECONFLY RECONBACKWARDS geth
ai AIRECONHIT RECONFLY RECONBACKWARDS geth
ai AIRECONSHRUNK RECONFLY RECONSHRUNKVELS geth
ai AIRECONDODGE RECONFLY RECONBULLVELS dodgebullet geth
ai AIRECONDODGEUP RECONFLY RECONRISE getv geth
ai AIRECONDYING RECONTURN RECONDYINGVELS geth getv

state recontilt

 // tilt code thanks to Hunter_Rus for initial version
  getactor[THISACTOR].ang angvar
  getincangle temp angvar peractor1
//    ifactor RECONDP 
//    {
//  	  ifaction RECONTURN
//  	  {
//  		ifai AIRECONDYING nullop else ifvarg temp -16 ifvarl temp 16
//  	    action RECONFLY
//  	  }
//  	  else
//  	    ifvarn temp 0 
//  	    {
// 	 	    ifvarg temp 0 cstat 257 else cstat 261
// 	 	    action RECONTURN
//  	    }
//   }
	  
  setvarvar peractor1 angvar
  shiftvarl temp 20
  divvar temp 18768
  ifvarg temp  256 setvar temp  256 else
  ifvarl temp -256 setvar temp -256 
  // added this code to make it smoother 
  getactor[THISACTOR].roll mtype
  ifvarvarg temp mtype 
  {
	  addvar mtype 24
	  ifvarvarg mtype temp setvarvar mtype temp
  }
  else ifvarvarl temp mtype
  {
	  subvar mtype 24
	  ifvarvarl mtype temp setvarvar mtype temp
  }
  setactor[THISACTOR].roll mtype
  
ends

state recondead
  addkills 1
  state specialdeath
  debris SCRAP1 16
  debris SCRAP2 8
  debris SCRAP3 14
  spawn EXPLOSION2
  spawn BURNING
  sound RPG_EXPLODE
  espawn PIGCOP
  hitradius 1560 5 10 15 20
  killit
  break
ends

state recondying

fall
strength 1
getactor[THISACTOR].htextra temp
ifvarl temp 50 
{
   setactor[THISACTOR].htextra -1
   setactor[THISACTOR].htpicnum -1
}
else state recondead

iffloordistl 16 state recondead
addvar countvar 1
ifvarg countvar 3
{
	addvar position 1
	ifvarg position 3 setvar position 0
    ifrnd 64 sound RPG_EXPLODE
    espawn EXPLOSION2
    ifvarn position 0 
    setactor[RETURN].htflags 256
    spawn SMALLSMOKE
    setvar countvar 0
}

  getactor[THISACTOR].ang angvar
  subvar angvar 48
  setactor[THISACTOR].ang angvar

ends

state checkreconhitstate
  getactor[THISACTOR].extra hp
  ifvarl hp 1 { strength 1 ai AIRECONDYING break }
  else ifsquished { ai AIRECONDYING break }
  else
  {
    ifwasweapon SHOTSPARK1 sound PISTOL_RICOCHET
    debris SCRAP1 1
    // ai AIRECONHIT
  }
ends

state reconshrunkstate
  ifcount 24
    killit
  else
    sizeto 1 1
ends

state checkreconnearplayer

  ifvarn target -1 ifvarl xydist 1024 ifvarl zdist 16384
  {
    ifcount 8 nullop
     else
      ifai AIRECONPULLBACK nullop
         else
             ai AIRECONPULLBACK
  }

ends

state recongetstate

// ramming code
ifvarn target -1 ifvarl xydist 1280 ifvarl zdist 16384 ifvare botclip 1
  {
    getactor[target].picnum temp
    ifvare temp APLAYER
	palfrom 32 16
	setactor[target].htowner THISACTOR
	setactor[target].htpicnum RADIUSEXPLOSION
	setactor[target].htextra 45
    soundonce SQUISHED
    setvar botclip 0
    setvar ammo 0
  }

  ifrnd 192
  {
    ifvarn target -1
    {
      ifmove RECONBULLVELS
      {
        ifcount 64
          ai AIRECONPULLBACK
        else
          ifnotmoving
            ifcount 16
              ai AIRECONPULLBACK
      }
      else
        ifcount 32
      {
        ifvarvarg z mz
          move RECONBULLVELS geth getv
        else
          move RECONBULLVELS geth
      }
    }

  }

ends

state recondodgestate
  ifai AIRECONDODGEUP
  {
     ifcount 8
       { ai AIRECONGETE  ifvarg ammo 51 setvar botclip 1 }
     else
       ifnotmoving 
         { ai AIRECONGETE ifvarg ammo 51 setvar botclip 1 }
  }
  else
  {
    ifcount 8
      { ai AIRECONGETE ifvarg ammo 51 setvar botclip 1 }
    else
      ifnotmoving
        { ai AIRECONGETE ifvarg ammo 51 setvar botclip 1 }
        
  }
ends

useractor enemy RECONDP 130

ifai AIRECONDYING { setvar target -1 state recondying break }
//ifai AIRECONDODGE setvar botdodge 2 else setvar botdodge 0 // AIRECONDODGE not currently used

ifvare ONCAR 2 ifvare target -1 ifvarg canseeplayer 0 getplayer[THISACTOR].i target

ifvare target -1 ifrnd 4 ifpdistl 16384 ifvare playerally 0
{
	ifvarg canseeplayer 0 
	{
	    getplayer[THISACTOR].i target
    }
}
ifvarn target -1
{
	setvar myspawner -1 // no locator set
	getactor[THISACTOR].z z
	getactor[target].z mz
	setvarvar zdist mz
	subvarvar zdist z
	ifvarl zdist 0 mulvar zdist -1
}
else
{
	ifvarg locators 0
	{
		ifmove RECONRUNVELS move RECONPATROLVELS geth
		getactorvar[lotag].locatorids myspawner
		canseespr THISACTOR myspawner temp
		ifvare temp 1
		{
			ldist xydist THISACTOR myspawner
			setvarvar temp lotag
			addvar temp 1
			ifvarvarg temp locators setvar temp 0
			ifvarl xydist 512 setvarvar lotag temp else
			{
				getactorvar[temp].locatorids spriteid
				ldist xydist2 THISACTOR spriteid
				ifvarvarl xydist2 xydist setvarvar lotag temp
			}
			ifvarvarn lotag temp
			{
				setvarvar target myspawner
				state gradualturn
				setvar target -1
				getactor[myspawner].z mz
				getactor[THISACTOR].z z
				subvarvar mz z
				shiftvarr mz 3
				movesprite THISACTOR 0 0 mz CLIPMASK0 RETURN
			}
		}
		else
		{
			setvar myspawner -1
			addvar lotag 1
			ifvarvarg lotag locators setvar lotag 0
			
		}
	}
	
}


state monsterai

state checkreconnearplayer
addvar ammo 1 // used as a counter

ifai AIRECONGETE nullop else
  ifvarn target -1
     {
      ifvarvarg z mz 
       {
        getactor[THISACTOR].z z
        subvar z 512
        updatesectorz x y z temp
        ifvarn temp -1
        setactor[THISACTOR].z z
        }
      else ifvarvarg mz z
        {
        getactor[THISACTOR].z z
        addvar z 512
        updatesectorz x y z temp
        ifvarn temp -1
        setactor[THISACTOR].z z
        }
     }

    soundonce RECO_ROAM

 
  ifaction 0
  {
     ai AIRECONGETE
     setvar ammo 0 
     setvar botclip 1
     sizeat 48 48
     cstat 257
     clipdist 100
     sound PIG_ROAM
  }

  else
    ifai AIRECONWAIT
  {
    ifcount 14
      ifvarn target -1
    {
      addvar weap 1
      ifvarg weap 52
       {
	    ifrnd 128 setvar target -1
        setvar weap 0
        setvar weapcount 0
        ifvarvarg z mz
          { ai AIRECONGETUP ifvarg ammo 51 setvar botclip 1 }
        else { ai AIRECONGETE ifvarg ammo 51 setvar botclip 1 }
        }
      else
       {
        ifvare weap 2
        {

	        ifvarand monstflags 32
	        {
		        sound BLASTERSND
			    eshoot FIREBALLPROJ 
				ifvarand monstflags 8 
				{
					setthisprojectile[RETURN].pal 13
					setthisprojectile[RETURN].extra 64
				}
			}
			else
			{
	            sound RPG_SHOOT
	            ifvare ONCAR 2 shoot RPG else
				shoot MORTER
			}
        }
        else ifvarg weap 19
        {
	         ldist xydist THISACTOR target
	         ifvare xydist 0 setvar xydist 1
	         state calczdist
	         subvar zdist 8192
             addvar weapcount 1
             ifvarg weapcount 2
             {
	             flash
	             setvar weapcount 0
	             ifvarand monstflags 16
				{
					setprojectile[STARPROJ].workslike 38914
					mulvar zdist 768
					divvarvar zdist xydist
					sound PISTOL_LOW
					zshoot zdist STARPROJ
					ifvarand monstflags 8 zshoot zdist STARPROJ
					setprojectile[STARPROJ].workslike 34818
				}
				else
				{
	                 // shoot bullets
					 sound PISTOL_LOW
	                 state calczdist
	                 shiftvarl zdist 8
	                 ifvare xydist 0 setvar xydist 1 
	                 divvarvar zdist xydist
	                 zshoot zdist SHOTSPARK1
	                 zshoot zdist SHOTSPARK1
	                 ifvarand monstflags 8
	                 {
		                zshoot zdist SHOTSPARK1
	                 	zshoot zdist SHOTSPARK1
	             	 }
                }
             }
         }
        }
    }
    else ifcount 52 ai AIRECONGETE
  }
  else
    ifai AIRECONGETE
      state recongetstate
  else
    ifai AIRECONGETUP
      state recongetstate
  else
    ifai AIRECONPULLBACK
  {
    ifcount 32
    ai AIRECONWAIT
  }
  else
    ifai AIRECONHIT
  {
    ifcount 8
      ai AIRECONWAIT
  }
  else
    ifai AIRECONSHRUNK
      state reconshrunkstate
  else
    ifai AIRECONDODGE
      state recondodgestate
  else
    ifai AIRECONDODGEUP
      state recondodgestate

  ifhitweapon
    state checkreconhitstate

  state recontilt
  
  ifceilingdistl 96 { getactor[THISACTOR].z temp addvar temp 512 setactor[THISACTOR].z temp }
  else
  {
	  getactor[THISACTOR].x x
	  getactor[THISACTOR].y y
	  getactor[THISACTOR].z z
	  getactor[THISACTOR].sectnum mysector
	  getflorzofslope mysector x y temp
	  subvar temp 8192
	  ifvarvarg z temp setactor[THISACTOR].z temp
  }

enda

state rf
  ifrnd 128
    cstat 4
  else
    cstat 0
ends

state shelleject
espawn SHELL
setactor[RETURN].xrepeat 8
setactor[RETURN].yrepeat 8
getactor[THISACTOR].ang angvar
addvar angvar 512
setactor[RETURN].ang angvar
randvar temp 64
addvar temp 64
setactor[RETURN].xvel temp
ends

state shelleject2
ifactor PIGCOPDIVE
{
	getactor[THISACTOR].z z
	addvar z 4096
	setactor[THISACTOR].z z
}
espawn SHOTGUNSHELL
setactor[RETURN].xrepeat 8
setactor[RETURN].yrepeat 8
getactor[THISACTOR].ang angvar
addvar angvar 512
setactor[RETURN].ang angvar
randvar temp 32
addvar temp 32
setactor[RETURN].xvel temp
ifactor PIGCOPDIVE
{
	getactor[THISACTOR].z z
	subvar z 4096
	setactor[THISACTOR].z z
}
ends

state drop_ammo

  ifspritepal 21 ifvare uselaser YES
  {
	  espawn LASERPISTOL
	  setactorvar[RETURN].peractor4 13
	  break
  }
  
ifvarn droptile 0 break

  ifrnd SPAWNAMMOODDS
  {
	ifvare useakimbo YES ifvare akimbo -1
	ifrnd 16 espawn FIRSTGUNSPRITE else
	{
	    espawn AMMO
	    setactorvar[RETURN].peractor4 13
    }
  }
ends

state drop_battery
ifvarn droptile 0 break
  ifrnd SPAWNAMMOODDS
  {
    espawn BATTERYAMMO
    setactorvar[RETURN].peractor4 13
  }
ends
state drop_sgshells
ifvarn droptile 0 break
  ifrnd SPAWNAMMOODDS
  {
    espawn SHOTGUNAMMO
    setactorvar[RETURN].peractor4 13
  }
ends
state drop_shotgun
ifvarn droptile 0 break
  ifrnd SPAWNAMMOODDS
  {
	ifvarand monstflags 256 espawn DBSPRITE else
    espawn SHOTGUNSPRITE
    setactorvar[RETURN].peractor4 13
  }
  else ifvarand monstflags 256 ifrnd SPAWNAMMOODDS
  {
	  espawn DBSPRITE
      setactorvar[RETURN].peractor4 13
      // DOUBLE CHANCE AT DBSHOTGUN
  }
ends
state drop_chaingun
ifvarn droptile 0 break
  ifrnd SPAWNAMMOODDS
  {
    ifrnd 32
      espawn CHAINGUNSPRITE
    else
      espawn BATTERYAMMO
    setactorvar[RETURN].peractor4 13
  }
ends

state getfloordist
  getactor[THISACTOR].x x
  getactor[THISACTOR].y y
  updatesector x y mysector
  ifvare mysector -1 getsector[THISACTOR].floorz z else
  getflorzofslope mysector x y z
  getactor[THISACTOR].z temp
  subvarvar z temp
  shiftvarr z 8
  subvar z 1
ends

state genericshrunkcode
  ifcount 32
  {
	  
    ifpdistl SQUISHABLEDISTANCE
    {
    	
    	ifactor BOTALLY break
    	ifvarn monstatus 2
    	state specialdeath
    	pstomp
    	break
	}
  }
  else
  {
    sizeto MINXSTRETCH MINYSTRETCH
    spawn FRAMEEFFECT1
  }
  setactor[THISACTOR].htextra -1
ends

state genericgrowcode
  ifcount 32
  {
    guts JIBS4 24
    guts JIBS6 28
    addkills 1 state specialdeath
    sound SQUISHED
    sound PIPEBOMB_EXPLODE
    hitradius 2048 60 70 80 90
    spawn BLOODPOOL
    spawn EXPLOSION2
    killit
  }
  else
  {
    ifactor COMMANDER
      sizeto 100 100
    else
      ifactor SHARK
        sizeto 84 84
    else
      sizeto MAXXSTRETCH MAXYSTRETCH
    break
  }
ends

state icesmoke

  spawn ICESMOKE
  spawn ICESMOKE
  spawn ICESMOKE
  spawn ICESMOKE
  spawn ICESMOKE
  spawn ICESMOKE
  spawn ICESMOKE
  spawn ICESMOKE
ends

action ASHARKCRUZING 0 8 5 1 24
action ASHARKFLEE  0 8 5 1 10
action ASHARKATACK   0 8 5 1 6
action ASHARKSHRUNK  0 8 5 1 24
action ASHARKGROW  0 8 5 1 24
action ASHARKFROZEN  0 1 5 1 24
action ASHARKDEAD    0 1 5 1 24

move SHARKBACK -84
move SHARKVELS 24
move SHARKFASTVELS 72
move SHARKFLEEVELS 40
move KILLERSHARKVELS 128
move KILLERSHARKVELSUP 128 -64
move KILLERSHARKVELSDOWN 128 32

state painsounds
randvar temp 6
switch temp
case 0 soundonce DUKE_LONGTERM_PAIN2 break
case 1 soundonce DUKE_LONGTERM_PAIN3 break
case 2 soundonce DUKE_LONGTERM_PAIN4 break
case 3 soundonce DUKE_LONGTERM_PAIN5 break
case 4 soundonce DUKE_LONGTERM_PAIN6 break
case 5 soundonce DUKE_LONGTERM_PAIN7 break
case 6 soundonce DUKE_LONGTERM_PAIN8 break
default break
endswitch
ends

state killershark

ifvare ONCAR 0
 ifpdistl 1560 
   ifp palive ifvarg canseeplayer 0
{
      setplayer[THISACTOR].sound_pitch 0
      state dukepainsounds
      palfrom 48 32
      addphealth SHARKBITESTRENGTH
      wackplayer
  	  resetcount
  	  ifrnd 32
  	  {
  	  		action ASHARKFLEE
	        move SHARKFLEEVELS fleeenemy
      }
}
      	  
ends

actor SHARK SHARKSTRENGTH ASHARKCRUZING SHARKVELS randomangle geth

ifvare countvar 0
{
	setvar countvar 1
	getactor[THISACTOR].extra hp
	ifvarand monstflags 4
	{
		mulvar hp 3 setactor[THISACTOR].extra hp
	} 
	ifvarand monstflags 2
	{
		mulvar hp 2 setactor[THISACTOR].extra hp
	}
	ifinwater setvar mtype 1
	ifonwater 
	{
		getactor[THISACTOR].sectnum temp
		ifvarg temp -1 ifvarl temp 4095
		setactor[THISACTOR].htactorstayput temp
	}
}

//state monsterai
ifcansee setvar canseeplayer YES else setvar canseeplayer NO
ifcanshoottarget setvar canshootplayer YES else setvar canshootplayer NO

state getfloordist
ifvarl z 0 movesprite THISACTOR 0 0 -512 CLIPMASK0 RETURN

ifaction ASHARKDEAD
{
	strength 0
	setvar monstatus 2
	ifhitweapon
    {
	  sound SQUISHED
      //guts JIBS6 5
      state standard_jibs
      killit
    }
    ifvarg underwater 0
    {
		setactor[THISACTOR].zvel 0
		getactor[watereffector].z mz
	  	getactor[THISACTOR].z z
	  	addvar mz 6144 //3072
	  	ifvarvarl z mz setactor[THISACTOR].z mz else
	  	ifvarvarg z mz
	  	{
		  	fall // necessary or the movesprite won't work; don't know why but it's true 	
	  		movesprite THISACTOR 0 0 -1024 CLIPMASK0 RETURN
	  		getactor[THISACTOR].z z
	  		ifvarvarl z mz setactor[THISACTOR].z mz
  	    }
	  	
  	} else
  	ifinwater nullop else iffloordistl 16 nullop else
  	{
	  	getactor[THISACTOR].z z
	  	addvar z 128
	  	setactor[THISACTOR].z z
	  	//fall
  	}
  	ifonwater fall
    break
}

ifvarn watereffector -1
{
	ifvarl underwater 2
	{
		getactor[watereffector].x mx
		getactor[watereffector].y my
		getactor[watereffector].z mz
		getactor[THISACTOR].x x
		getactor[THISACTOR].y y
		subvarvar mx x
		subvarvar my y
		getangle angvar mx my
		cos xvel angvar
		sin yvel angvar
		shiftvarr xvel 6
		shiftvarr yvel 6
		setvar zvel 2048
		movesprite THISACTOR xvel yvel zvel CLIPMASK0 RETURN	
	}
}

ifvarand monstflags 8
{
	setactor[THISACTOR].zvel 0
	//fall
	ifmove SHARKFASTVELS move KILLERSHARKVELS geth
	ifp phigher ifrnd 8
	{
		ifinwater
		{
			ifceilingdistl 8
			{
				ifmove KILLERSHARKVELSDOWN nullop else move KILLERSHARKVELSDOWN faceplayerslow getv
			} else
			ifvarg canseeplayer 0
			{
				ifmove KILLERSHARKVELSUP nullop else move KILLERSHARKVELSUP faceplayerslow getv
			}
		} else
		ifvarg underwater 1 ifvare headunder YES
		{
			ifmove KILLERSHARKVELSUP nullop else move KILLERSHARKVELSUP faceplayerslow getv
		}
	}
	else ifrnd 8
	{
		getactor[THISACTOR].z z
		getplayer[THISACTOR].posz mz
		ifvarvarg mz z
		{
			ifmove KILLERSHARKVELSDOWN nullop else move KILLERSHARKVELSDOWN faceplayerslow getv
		}
	}
	ifinwater nullop else ifvarg underwater 1 nullop else
	fall
}




  ifaction ASHARKSHRUNK
  {
    ifcount SHRUNKDONECOUNT
      action ASHARKCRUZING
    else ifcount SHRUNKCOUNT
      sizeto 60 60
    else
      state genericshrunkcode
    break
  }
  else
    ifaction ASHARKGROW
  {
    ifcount SHRUNKDONECOUNT
      action ASHARKCRUZING
    else
      ifcount SHRUNKCOUNT
        sizeto 24 24
    else
      state genericgrowcode
  }
  else
    ifaction ASHARKFROZEN
  {
    fall

    ifp pfacing
      ifpdistl FROZENQUICKKICKDIST
      ifvare superkicking 0
        pkick

    ifcount THAWTIME
    {
      action ASHARKFLEE
      getlastpal
      break
    }
    else
      ifcount FROZENDRIPTIME
        ifactioncount 26
          resetactioncount

    ifhitweapon
    {
      ifwasweapon FREEZEBLAST
        break

      lotsofglass 30
      state standard_jibs
      sound GLASS_BREAKING
      addkills 1 state specialdeath
      killit
    }
    break
  }
  else
    ifaction ASHARKFLEE
  {
    ifcount 16
      ifrnd 48
    {
      action ASHARKCRUZING
      move SHARKVELS randomangle geth
    }
  }
  else
    ifaction ASHARKCRUZING
  {
    ifvarg canseeplayer 0
      ifcount 48
        ifrnd 2
          ifvarg canshootplayer 0
    {
      action ASHARKATACK
      ifvarand monstflags 8 move KILLERSHARKVELS faceplayerslow getv
      else
      move SHARKFASTVELS faceplayerslow getv 
      break
    }
    ifcount 32
      ifnotmoving
    {
      ifrnd 128
        move SHARKVELS randomangle geth
      else
        move SHARKFASTVELS randomangle geth
    }
  }
  else
    ifaction ASHARKATACK
  {
    ifcount 4
    {
	  ifvarand monstflags 8 state killershark else
	  {
		  ifpdistl 1280 
	      {
	        ifp palive ifvarg canshootplayer 0
	        {
		      setplayer[THISACTOR].sound_pitch 0
	          sound DUKE_GRUNT
	          palfrom 32 32
	          addphealth SHARKBITESTRENGTH
	        }
	        action ASHARKFLEE
	        move SHARKFLEEVELS fleeenemy
	      }
      }
    }
    else
      ifnotmoving
    {
      ifcount 32
      {
        action ASHARKCRUZING
        move SHARKVELS randomangle geth
      }
    }
    else
      ifcount 48
        ifrnd 2
    {
      action ASHARKCRUZING
      move SHARKFASTVELS randomangle geth
    }
  }

  ifhitweapon
  {
    ifdead
    {
      ifwasweapon GROWSPARK
      {
        move 0
        cstat 0
        action ASHARKGROW
        sound ACTOR_GROWING
        break
      }
      else
        ifwasweapon FREEZEBLAST
      {
        spritepal 1
        strength 0
        action ASHARKFROZEN
        sound SOMETHINGFROZE
        state icesmoke
      }
      else
        ifwasweapon RPG
      {
	    sound SQUISHED
        //guts JIBS6 5
        state standard_jibs
        addkills 1 state specialdeath
        killit
      } 
      else
        ifwasweapon RADIUSEXPLOSION
      {
	    sound SQUISHED
        //guts JIBS6 5
        state standard_jibs
        addkills 1 state specialdeath
        killit
      } 
      else
      {
	    
        //sound SQUISHED
        guts JIBS6 5
        //state standard_jibs
        addkills 1 state specialdeath
        //killit
        setactor[THISACTOR].mdflags 2

        
        action ASHARKDEAD
        cstat 264
        move INIT
      }
    }
    else
    {
      ifwasweapon SHRINKSPARK
      {
        action ASHARKSHRUNK
        sound ACTOR_SHRINKING
        move 0
        break
      }
      else
        ifwasweapon GROWSPARK
          sound EXPANDERHIT
	  
      ifvarand monstflags 8
      {   
	      action ASHARKATACK
	      move SHARKFASTVELS faceplayerslow getv break
	  } else
      move SHARKVELS randomangle geth
    }
  }
enda

state turretshootbot

state calczdist
subvar zdist 4096
ldist xydist THISACTOR target
ifvare xydist 0 setvar xydist 1

ifvarand monstflags 64
{
	setprojectile[RAILSLUG_WEAK].workslike 36866 //4097
	//shiftvarl zdist 8
	mulvar zdist 844
	divvarvar zdist xydist
	sound SHOOTRAIL
	ifvarand monstflags 8 setprojectile[RAILSLUG_WEAK].extra 30
	zshoot zdist RAILSLUG_WEAK
	ifvarand monstflags 8 setprojectile[RAILSLUG_WEAK].extra 15
	setprojectile[RAILSLUG_WEAK].workslike 32770 //1
} else
ifvarand monstflags 32 ifvarg xydist TRIPBOMBBLASTRADIUS
{
	sound RPG_SHOOT
	mulvar zdist 644
	divvarvar zdist xydist
	ezshoot zdist RPG // NOAIM
	ifvarand monstflags 8 setactor[RETURN].extra 100
	setactor[RETURN].ang sprite[THISACTOR].ang
} else
ifvarand monstflags 16
{
	ifvarand morefx 16 ifvarn rendmode 0 spawn SHOTSPARK4
	shiftvarl zdist 8
	divvarvar zdist xydist
	sound PISTOL_FIRE
	zshoot zdist SHOTSPARK1
	zshoot zdist SHOTSPARK1
	ifvarand monstflags 8 
	{
		zshoot zdist SHOTSPARK1
		zshoot zdist SHOTSPARK1
	}
} else
{
	mulvar zdist 768
	divvarvar zdist xydist
	ifvare mtype 1 sound BLASTERSND else
	ifvare dpsounds YES sound PRED_ATTACK2 else
	sound PRED_ATTACK
	zshoot zdist FIRELASER
	ifvarand monstflags 8 zshoot zdist FIRELASER
}

ends

state turretshoot

ifvarn target -1 { state turretshootbot break }
ifvare target -1 ifvarn playerally 0 break

ifvarand monstflags 64
{
	sound SHOOTRAIL
	ifvarand monstflags 8 setprojectile[RAILSLUG_WEAK].extra 30
	shoot RAILSLUG_WEAK
	ifvarand monstflags 8 setprojectile[RAILSLUG_WEAK].extra 15
} else
ifvarand monstflags 32
{
	sound RPG_SHOOT
	shoot RPG
} else
ifvarand monstflags 16
{
	ifvarand morefx 16 ifvarn rendmode 0 spawn SHOTSPARK4
	sound PISTOL_FIRE
	shoot SHOTSPARK1
	shoot SHOTSPARK1
	ifvarand monstflags 8 
	{
		shoot SHOTSPARK1
		shoot SHOTSPARK1
	}
} else
{
	ifvare mtype 1 sound BLASTERSND else
	ifvare dpsounds YES sound PRED_ATTACK2 else
	sound PRED_ATTACK
	shoot FIRELASER
	ifvarand monstflags 8 shoot FIRELASER
}
ends


action ASATNSPIN     0 5 1 1 4
action ASATSHOOTING   -10 3 5 1 40
action ASATWAIT      0 1 5 1 1

state fastshooting

ifactioncount 12
      ifrnd 32
    {
      action ASATWAIT
      move 0
    }
    ifcount 16
      resetcount
    else
      ifcount 9
    {
      ifcount 10 nullop
      else
        state turretshoot
      
    }
    else
      ifcount 2
    {
      ifcount 3 nullop
      else
      {
	    setvar temp 0
        ifvarg canseeplayer 0
          //ifvarg canshootplayer 0
        setvar temp 1
        else ifvarn target -1 setvar temp 1
        ifvare temp 1
          state turretshoot
        
      }
    } 

ends

actor ROTATEGUN ROTTURRETSTRENGTH
// ASATSHOOTING STOPPED faceplayer
state monsterai

  ifvarn myspawner -1
  {
	  cstator 8
	  getactorvar[myspawner].playerally playerally
	  setvar tempb 0
	  getactor[myspawner].statnum temp
	  ifvare temp 1024 setvar tempb 1
	  getactor[myspawner].picnum temp
	  ifvarn temp DRONE setvar tempb 1
	  ifvare tempb 1
	  {
		  strength 0
		  setactor[THISACTOR].htextra 10
		  setactor[THISACTOR].htpicnum RADIUSEXPLOSION
	  }
	  ifvare tempb 1
	  {
		  strength 0
		  setactor[THISACTOR].htextra 10
		  setactor[THISACTOR].htpicnum RADIUSEXPLOSION
	  }
	  else
	  {
		  getactor[myspawner].x x
		  getactor[myspawner].y y
		  getactor[myspawner].z z
		  subvar z 512 //1024
		  setactor[THISACTOR].x x
		  setactor[THISACTOR].y y
		  setactor[THISACTOR].z z
		  getactor[myspawner].sectnum temp
		  ifvarvarn temp sprite[THISACTOR].sectnum ifvarn temp -1
		  changespritesect THISACTOR temp
		  ifvare target -1 getactorvar[myspawner].target target
	  }

  }
  ifvare mtype 1
  {
	  ifstrength 100 ifrnd 6
	  {
		  espawn NEWSMOKE
		  setactor[RETURN].xrepeat 128
		  setactor[RETURN].yrepeat 128
		  getactor[THISACTOR].cstat temp
		  ifvarand temp 8 nullop else
		  {
			  getactor[THISACTOR].z digz
			  subvar digz 10240
			  setactor[RETURN].z digz
		  }
	  }
  }
  
  ifaction 0
  {
    action ASATSHOOTING
    move STOPPED faceplayer
    ifvare mtype 1 addstrength 200
  }
  else
    ifaction ASATNSPIN
  {
	getactor[THISACTOR].ang angvar
	ifrnd 128 addvar angvar 64 else subvar angvar 64
	setactor[THISACTOR].ang angvar
	
    ifactioncount 32
    {
      action ASATWAIT
      move STOPPED faceplayer
      ifvare mtype 1 move STOPPED faceplayerslow
    }
  }
  else
    ifaction ASATSHOOTING
  {
	ifvarand monstflags 256 state fastshooting
	else
	{
	    ifactioncount 12
	      ifrnd 32
	    {
	      action ASATWAIT
	      move 0
	    }
	    ifcount 32
	      resetcount
	    else
	      ifcount 16
	    {
	      ifcount 17 nullop
	      else
	      {
	        state turretshoot
	      }
	    }
	    else
	      ifcount 4
	    {
	      ifcount 5 nullop
	      else
	      {
		    ifvarn target -1 state turretshoot
		    else
	        ifvarg canseeplayer 0
	          state turretshoot
	      }
	    }
    }
  }
  else
    ifaction ASATWAIT
  {
    ifactioncount 64
      ifrnd 32
        ifp palive
    {
	   ifvarn target -1 { action ASATSHOOTING move STOPPED geth }
	   else
	     ifvare playerally 0
	   {
		  setvar temp 0
		  ifvarand monstflags 2048 { ifvarg targetlock 0 setvar temp 1 } else
          ifvarg canseeplayer 0 setvar temp 1
          ifvare temp 1
		    {
		      action ASATSHOOTING
		      move STOPPED faceplayer
		      ifvare mtype 1 move STOPPED faceplayerslow
		    }
       }
    }
  }
  ifhitweapon
  {
    ifdead
    {
      addkills 1 state specialdeath
      sound LASERTRIP_EXPLODE
      debris SCRAP1 15
      spawn EXPLOSION2
      ifvare mtype 1 { ifrnd 128 spawn BURNING else spawn BURNING2 }
      ifvarn myspawner -1
      {
	      getactorvar[myspawner].monstflags temp
	      ifvarand temp 16 subvar temp 16
	      ifvarand temp 32 subvar temp 32
	      ifvarand temp 64 subvar temp 64
	      setactorvar[myspawner].monstflags temp
      }
      killit
    }
    else
    {
      action ASATNSPIN
      debris SCRAP1 4
    }
    move 0
  }
enda

state troop_body_jibs

//     guts HEADJIB1 1

//     guts LEGJIB1 2

//     guts ARMJIB1 1
//   ifrnd 48
//     spawn BLOODPOOL

    state aimjib
	ezshoot zdist TROOPHEADPROJ
	state setjib
	state aimjib
	ezshoot zdist TROOPARMPROJ
	state setjib
	state aimjib
	ezshoot zdist TROOPLEGPROJ
	state setjib
	state aimjib
	ezshoot zdist TROOPLEGPROJ
	state setjib
    
ends

state liz_body_jibs

//     guts LIZMANHEAD1 1

//     guts LIZMANLEG1 2

//     guts LIZMANARM1 1
//   ifrnd 48
//     spawn BLOODPOOL
    
    state aimjib
	ezshoot zdist LIZMANHEADPROJ
	state setjib
	state aimjib
	ezshoot zdist LIZMANARMPROJ
	state setjib
	state aimjib
	ezshoot zdist LIZMANLEGPROJ
	state setjib
	state aimjib
	ezshoot zdist LIZMANLEGPROJ
	state setjib
ends

action BLOODFRAMES 0 4 1 1 15
actor BLOOD 0 BLOODFRAMES
  sizeto 72 72 sizeto 72 72 sizeto 72 72
  ifpdistg 3144 killit
  ifactioncount 4 killit
enda

action EGGOPEN1 1 1 1 1 4
action EGGOPEN2 2 1 1 1 4
action EGGOPEN3 2 1 1 1 4
action EGGWAIT  0
action EGGFROZEN 1
action EGGGROW 1
action EGGSHRUNK 1

actor EGG TOUGH
  state monsterai
  setvar countvarb 0 // to prevent it from targetting enemies and facing them
  fall
  ifaction 0
  {
    ifcount 64
    {
      ifrnd 128
      {
        action EGGWAIT
        move 0
      }
      else
      {
        sound SLIM_HATCH
        action EGGOPEN1
      }
    }
  }
  else
    ifaction EGGOPEN1
      ifactioncount 4
        action EGGOPEN2
  else
    ifaction EGGOPEN2
      ifactioncount 4
    {
	  ifvarn playerally 0 orvar monstflags 16
	  ifvarand monstflags 16
	  {
		espawn NEWBEASTHANG
	  	setactorvar[RETURN].mtype 1
      	setactorvar[RETURN].playerally playerally
      	ifvarand morefx 32 setactor[RETURN].mdflags 1
      	getactor[THISACTOR].pal pal
      	setactor[RETURN].pal pal
  	  } else
  	  {
      	espawn GREENSLIME
      	ifvarand morefx 32 setactor[RETURN].mdflags 1
  	  }
      action EGGOPEN3
    }
  else
    ifaction EGGGROW
      state genericgrowcode
  else
    ifaction EGGSHRUNK
      state genericshrunkcode
  else
    ifaction EGGFROZEN
  {
    ifcount THAWTIME
    {
      action 0
      getlastpal
    }
    else
      ifcount FROZENDRIPTIME
    {
      ifactioncount 26
      {
        spawn WATERDRIP
        resetactioncount
      }
    }

    ifhitweapon
    {
      ifwasweapon FREEZEBLAST
      {
        strength 0
        break
      }
      lotsofglass 30
	  state standard_jibs
      sound GLASS_BREAKING
      ifrnd 84
        spawn BLOODPOOL

      addkills 1 state specialdeath
      killit
    }

    // ifp pducking
    ifp pfacing
      ifpdistl FROZENQUICKKICKDIST
      ifvare superkicking 0
        pkick

    break
  }

  ifhitweapon
  {
    ifdead
    {
      ifwasweapon FREEZEBLAST
      {
        sound SOMETHINGFROZE
        spritepal 1
        move 0
        action EGGFROZEN
        strength 0
        state icesmoke
        break
      }
      else
        ifwasweapon GROWSPARK
      {
        cstat 0
        move 0
        sound ACTOR_GROWING
        action EGGGROW
        break
      }

      addkills 1 state specialdeath

      sound SQUISHED
	  debris SCRAP3 10
      state standard_jibs
      killit
    }
    else
      ifwasweapon SHRINKSPARK
      {
        move 0
        sound ACTOR_SHRINKING
        action EGGSHRUNK
        break
      }

      ifwasweapon GROWSPARK
        sound EXPANDERHIT
  }
  else
    ifaction EGGWAIT
  {
    ifcount 512
      ifrnd 2
    {
      ifaction EGGSHRUNK
        break
      sound SLIM_HATCH
      action EGGOPEN1
    }
  }

enda

//
//
//    TROOP/LIZTROOP CODE
//
//



state troophidestate
  ifaction ATROOPREAPPEAR
  {
    ifactioncount 2
    {
      sound TELEPORTER
      ifspritepal 21 ifvare uselaser YES ifrnd 128
      {
	      cactor LIZTROOPDUCKING
	      ai AITROOPDUCKING
      } else
      ai AITROOPSHOOTING
      cstat 257
    }
    else
    {
      sizeto 41 40
      sizeto 41 40
      sizeto 41 40
      sizeto 41 40
      spawn FRAMEEFFECT1
    }
  }
  else
    ifaction ATROOPWALKING
  {
	ifcount 300 // failsafe
    {
	   spawn TRANSPORTERSTAR
       action ATROOPREAPPEAR
       move STOPPED
       break
    }
    ifpdistl 2448
      ifpdistg 1024
    {
	  
      ifceilingdistl 48
        break
      ifp pfacing
        break

      ifgapzl 64 nullop
      else
        ifawayfromwall
      {
        spawn TRANSPORTERSTAR
        action ATROOPREAPPEAR
        move STOPPED
        break
      }

    }
  }
  else
    ifaction ATROOPHIDE
  {
    ifactioncount 2
    {
      spawn TRANSPORTERSTAR
      sound TELEPORTER
      action ATROOPWALKING
      move TROOPWALKVELS faceplayer
      cstat 32768
    }
    else
    {
      sizeto 4 40
      sizeto 4 40
      sizeto 4 40
      sizeto 4 40
      spawn FRAMEEFFECT1
    }
  }
  else
    ifaction ATROOPABOUTHIDE
      ifactioncount 2
    {
      action ATROOPHIDE
      cstat 0
    }
ends

state troopgunnashootbot
  
    ifvarl xydist 1024
    {
	    ifrnd 128 ifactornotstayput
	    ai AITROOPFLEEINGBACK else
        ai AITROOPSHOOTING
    }
    else
      ifactornotstayput
    {
      ifactioncount 12
        ifrnd 16
      {
        ifspritepal 21
          ifrnd 4
            ifvarg xydist 4096
              ai AITROOPHIDE
        else
        {
          ifvarl xydist 1280 // 1100
            ai AITROOPFLEEINGBACK
          else
          {
            ifvarl xydist 4096
            {
	            ifvarand monstflags 256 { move TROOPRUNVELS seekplayer action ATROOPRUNNING break }
	            ifspritepal 21 ifvare uselaser YES ifrnd 128
	              {
		              cactor LIZTROOPDUCKING
		              ai AITROOPDUCKING
		              break
	              }
                  ai AITROOPSHOOTING
            }
            else
            {
	          ifvarg peractor1 0 { ai AITROOPSHOOTING break }
              move TROOPRUNVELS seekplayer
              action ATROOPRUNNING
            }
          }
        }
      }
    }
    else
      ifcount 26
        ifrnd 32
        {
	      ifvarand monstflags 256 nullop else
          ai AITROOPSHOOTING
        }
  
ends

state troopgunnashoot
  ifp palive
  {
    ifpdistl 1024
      ai AITROOPSHOOTING
    else
      ifactornotstayput
    {
      ifactioncount 12
        ifrnd 16
          ifvarg canshootplayer 0
      {
        ifspritepal 21
          ifrnd 4
            ifpdistg 4096
              ai AITROOPHIDE
        else
        {
          ifpdistl 1100
            ai AITROOPFLEEING
          else
          {
            ifpdistl 4096
              ifvarg canseeplayer 0
                {
	              ifvarand monstflags 256 { move TROOPRUNVELS seekplayer action ATROOPRUNNING break }
	              ifvarand monstflags 2048 ifvare targetlock 0 nullop else
	              {
		              ifspritepal 21 ifvare uselaser YES ifrnd 128
		              {
			              cactor LIZTROOPDUCKING
			              ai AITROOPDUCKING
			              break
		              }
	                  ai AITROOPSHOOTING
                  }
                }
            else
            {
	          ifvarand monstflags 2048 ifvare targetlock 0 nullop else
	          {
		          ifvarg peractor1 0 { ai AITROOPSHOOTING break }
		          ifspritepal 21 ifvare smartmonsters YES ifrnd 8
		          {
			          cactor LIZTROOPDUCKING
			          ai AITROOPDUCKING
			          break
		          }
	          }
              move TROOPRUNVELS seekplayer
              action ATROOPRUNNING
            }
          }
        }
      }
    }
    else
    ifvarand monstflags 2048 ifvare targetlock 0 nullop else
      ifcount 26
        ifrnd 32
        {
	      ifvarand monstflags 256 nullop else
          ai AITROOPSHOOTING
          ifvare smartmonsters YES ifspritepal 21 ifrnd 128
          {
	          cactor LIZTROOPDUCKING
	          ai AITROOPDUCKING
          }
        }
  }
ends

state troopseekstate
  ifpdistl 1596 ifp pfacing ifvarn playerally 0 { ai AITROOPFLEEINGBACK break }
  ifvarn target -1 state troopgunnashootbot else
  state troopgunnashoot
  ifinwater
  {
    ai AITROOPJETPACK
    break
  }
  ifvarg underwater 0
  {
	ai AITROOPJETPACK
	break
  }
  ifvarg canseeplayer 0
  {
    ifmove TROOPRUNVELS
      ifpdistl 1596
      {
	    ifvarn playerally 0 { ifp pfacing ai AITROOPFLEEINGBACK } else
	    ifvarand monstflags 2048 ifvare targetlock 0 nullop else
	    {
		    ifvarand monstflags 256 nullop else
		    {
		    	cactor LIZTROOPDUCKING
	        	ai AITROOPDUCKING
        	}
        }
      }
    ifp phigher
    {
      ifceilingdistl 128 nullop
      else
        ifactornotstayput
          ai AITROOPJETPACK
      break
    }
    else
      ifrnd 2
    {
      ifspritepal 21
        ifpdistg 1596
      {
        ai AITROOPHIDE
        break
      }
      ifai AITROOPDODGE nullop else
      ifbulletnear
      {
        ifrnd 128
          ai AITROOPDODGE
        else
        {
	      cactor LIZTROOPDUCKING
          ai AITROOPDUCKING
        }
        break
      }
    }
  }
  ifnotmoving
  {
    ifrnd 32
      operate
    else
      ifcount 32
        ifp palive
        {
	      setvar temp 0
          ifvarg canseeplayer 0
            ifvarg canshootplayer 0
          setvar temp 1
          ifvarn target -1 setvar temp 1
          else
          ifvarand monstflags 2048 ifvare targetlock 0 setvar temp 0
          ifvare temp 1
              ai AITROOPSHOOTING
        }
  }
  ifrnd 1
  {
    ifrnd 128
      soundonce PRED_ROAM
    else
      soundonce PRED_ROAM2
  }
ends

state troopshootbot

ldist xydist THISACTOR target
ifvare xydist 0 setvar xydist 1
state calczdist

ifvarand monstflags 64
{
	setprojectile[RAILSLUG_WEAK].workslike 36866
	//shiftvarl zdist 8
	mulvar zdist 844
	divvarvar zdist xydist
	sound SHOOTRAIL
	ifvarand monstflags 8 setprojectile[RAILSLUG_WEAK].extra 30
	zshoot zdist RAILSLUG_WEAK
	ifvarand monstflags 8 setprojectile[RAILSLUG_WEAK].extra 15
	setprojectile[RAILSLUG_WEAK].workslike 32770
}
else
ifvarand monstflags 32
{
	ifrnd 64
	{
	    sound BLASTERSND
	    eshoot FIREBALLPROJ 
		ifvarand monstflags 8 
		{
			setthisprojectile[RETURN].pal 13
			setthisprojectile[RETURN].extra 64
		}
	}
	else
	{
		mulvar zdist 768
		divvarvar zdist xydist
		ifvare dpsounds YES sound PRED_ATTACK2 else
	    sound PRED_ATTACK
	    zshoot zdist FIRELASER
	    ifvarand monstflags 8 zshoot zdist FIRELASER
    }
} else
ifvarand monstflags 16
{
	setprojectile[STARPROJ].workslike 38914
	mulvar zdist 768
	divvarvar zdist xydist
	sound PISTOL_LOW
	zshoot zdist STARPROJ
	ifvarand monstflags 8 zshoot zdist STARPROJ
	setprojectile[STARPROJ].workslike 34818
}
else
ifactor LIZTROOPDUCKING ifspritepal 21 ifvare uselaser YES
{
	setprojectile[BIGFIRELASER].workslike 36866
	mulvar zdist 768
	divvarvar zdist xydist
	cactor LIZTROOP
	ai AITROOPSEEKPLAYER
	zshoot zdist BIGFIRELASER
	ifvarand monstflags 8 zshoot zdist BIGFIRELASER
	sound BLASTERSND
	setprojectile[BIGFIRELASER].workslike 32770
	break
}
else
{
	mulvar zdist 768
	divvarvar zdist xydist
	ifvare dpsounds YES sound PRED_ATTACK2 else
    sound PRED_ATTACK
    zshoot zdist FIRELASER
    ifvarand monstflags 8 zshoot zdist FIRELASER
}

ends

state troopshooting

ifvarn target -1 { state troopshootbot break }

ifactor LIZTROOPDUCKING ifspritepal 21 ifvare uselaser YES
{
	cactor LIZTROOP
	ai AITROOPSEEKPLAYER
	shoot BIGFIRELASER
	sound BLASTERSND
	break
}

ifvarand monstflags 64
{
	sound SHOOTRAIL
	ifvarand monstflags 8 setprojectile[RAILSLUG_WEAK].extra 30
	shoot RAILSLUG_WEAK
	ifvarand monstflags 8 setprojectile[RAILSLUG_WEAK].extra 15
}
else
ifvarand monstflags 32
{
	ifrnd 64
	{
		sound BLASTERSND
		eshoot FIREBALLPROJ 
		ifvarand monstflags 8 
		{
			setthisprojectile[RETURN].pal 13
			setthisprojectile[RETURN].extra 64
		}
	}
	else
	{
		ifvare dpsounds YES sound PRED_ATTACK2 else
	    sound PRED_ATTACK
	    shoot FIRELASER
	    ifvarand monstflags 8 shoot FIRELASER
    }
	
} else
ifvarand monstflags 16
{
	ifvare xydist 0 setvar xydist 1
	divvarvar zdist xydist
	sound PISTOL_LOW
	shoot STARPROJ
	ifvarand monstflags 8 shoot STARPROJ
}
else
{
	ifvare dpsounds YES sound PRED_ATTACK2 else
    sound PRED_ATTACK
    shoot FIRELASER
    ifvarand monstflags 8 shoot FIRELASER
}
ends

state troopduckstate
  ifaction ATROOPDUCK
  {
    ifactioncount 8
    {
      ifp palive
      {
        ifrnd 128
          action ATROOPDUCKSHOOT
      }
      else
        ifmove DONTGETUP
          break
        else
        {
	      cactor LIZTROOP
          ai AITROOPSEEKPLAYER
        }
    }
  }
  else
{
  setvar temp 0
  ifaction ATROOPDUCKSHOOT setvar temp 1
  ifaction ATROOPDUCKSHOOTFAST setvar temp 1
  
  ifvare temp 1
  {
	ifspritepal 21 ifvare uselaser YES
	{
		ifcount 28 nullop else
		{
			action ATROOPDUCKSHOOT
			soundonce CHARGESND
		}
	}
    ifcount 64
    {
      ifmove DONTGETUP
        resetcount
      else
      {
	    cactor LIZTROOP
        ifpdistl 1100
          ai AITROOPFLEEING
        else
        ifvarn target -1 ifvarl xydist 1100
          ai AITROOPFLEEING
        else
          ai AITROOPSEEKPLAYER
      }
    }
    else
      ifactioncount 2
      {
	    ifvarn target -1 { state troopshootbot resetactioncount } else
	    ifvarand monstflags 2048 ifvare targetlock 0 nullop else
        ifvarg canshootplayer 0 ifvare playerally 0
        {
	      state troopshooting
          resetactioncount
        }
        else 
        {
	        cactor LIZTROOP
	        ifvarn playerally 0 ai AITROOPFLEEINGBACK else
	        ai AITROOPSEEKPLAYER
        }
      }
  }
}
ends

state suicidebomber

setvar temp 0
ifvarn target -1
{
  dist xydist2 THISACTOR target
  ifvarl xydist2 1280 setvar temp 1
}
ifvare playerally 0
ifvarg canshootplayer 0 ifpdistl 1280 setvar temp 1

ifvare temp 0 break
action ATROOPABOUTBLOW
sound LASERTRIP_ONWALL
sound LASERTRIP_ARMING

ends

state suicideblow
	debris SCRAP1 8
	debris SCRAP2 4
	debris SCRAP3 7
	state standard_jibs
	spawn EXPLOSION2
	sound RPG_EXPLODE
	ifvarand monstflags 8
	hitradius 3072 72 96 120 144 else
	hitradius 3072 36 48 60 72
	addkills 1
	state specialdeath
	killit 
ends

state troopshootstate

ifvarn playerally 0 ifvare target -1 { ai AITROOPSEEKPLAYER break }

ifaction ATROOPABOUTBLOW
{
	ifactioncount 2
	{
		state suicideblow
	}
	break
}
else ifvarand monstflags 256 state suicidebomber

  ifactioncount 2
  {
	setvar temp 0
	ifvarg canshootplayer 0 setvar temp 1
	ifvarn target -1 setvar temp 1
	else
	ifvarand monstflags 2048 ifvare targetlock 0 setvar temp 0
	ifvare temp 1
    {
      state troopshooting
      resetactioncount
      ifrnd 128
        ai AITROOPSEEKPLAYER
      ifcount 24
      {
        ifrnd 96
          ifpdistg 2048
            ai AITROOPSEEKPLAYER
          else
          {
            ifpdistg 1596
              ai AITROOPFLEEING
            else
              ai AITROOPFLEEINGBACK
          }
      }
    }
    else
      ai AITROOPSEEKPLAYER
  }
ends

state troopfleestate
  ifactioncount 7
  {
    ifpdistg 3084
    {
      ai AITROOPSEEKPLAYER
      break
    }
    else
      ifrnd 32
        ifp palive
    {
	    ifvarand monstflags 2048
	    {
		    ifvarg targetlock 0
		    {
			    ifrnd 128
		        {
			      cactor LIZTROOPDUCKING
		          ai AITROOPDUCKING
		        }
		        else
		          ai AITROOPSHOOTING
		          break
	         }
        }
        else
          ifvarg canseeplayer 0
            ifvarg canshootplayer 0
	      {
	        ifrnd 128
	        {
		      cactor LIZTROOPDUCKING
	          ai AITROOPDUCKING
	        }
	        else
	          ai AITROOPSHOOTING
	          break
	        
	      }
     }
  }
  ifnotmoving
  {
    ifrnd 32
      operate
    else
      ifcount 32
        ifp palive   
      {
	    setvar temp 0
	    ifvarg canseeplayer 0
          ifvarg canshootplayer 0
            setvar temp 1
        ifvarn target -1 setvar temp 1
        else
        ifvarand monstflags 2048 ifvare targetlock 0 setvar temp 0
        ifvare temp 1
          {
	        ifrnd 128
	          ai AITROOPSHOOTING
	        else
	        {
		      cactor LIZTROOPDUCKING
	          ai AITROOPDUCKING
	        }
          }
      }
  }
ends

state troopdying
  iffloordistl 32
  {
    ifactioncount 5
    {
      cstat 0
      iffloordistl 8
      sound THUD
      ifrnd 64
        spawn BLOODPOOL
      state rf
      strength 0
      //move STOPPED geth
      //action ATROOPDEAD
      action ANULLACTION
      move 0
      cactor LIZTROOPCORPSE
    }
    break
  }
  else
  {
    state rf
    move 0
    action ATROOPDYING
  }
ends

state checktroophit
  ifaction ATROOPSUFFERING
  {
    stopsound LIZARD_BEG
    sound PRED_DYING
    cstat 0
    strength 0
    action ATROOPSUFFERDEAD
    ifvarand monstflags 256 state suicideblow
    break
  }
  ifdead
  {
	cactor LIZTROOP
    ifwasweapon FREEZEBLAST
    {
      sound SOMETHINGFROZE
      spritepal 1
      move 0
      action ATROOPFROZEN
      strength 0
      setvar target -1
      setvar peractor7 -1
      setvar targetlock 0
      state icesmoke
      break
    }

    state drop_ammo
    state random_wall_jibs

    ifwasweapon GROWSPARK
    {
      cstat 0
      sound ACTOR_GROWING
      ai AITROOPGROW
      setvar target -1
      setvar peractor7 -1
      setvar targetlock 0
      break
    }

    addkills 1 state specialdeath

    ifwasweapon RPG
    {
      sound SQUISHED
      state troop_body_jibs
      state standard_jibs
      killit
    }
    else
      ifwasweapon RADIUSEXPLOSION
    {
      sound SQUISHED
      state troop_body_jibs
      state standard_jibs
      killit
    }
    else
    {
      sound PRED_DYING
      
      ifgapzl 192 nullop else
      ifrnd 48
      {
	      espawn EXPLOSION2
	      setactor[RETURN].cstat 130
	      sound ROBOBOOST
	      state icesmoke
	      state sparkstate
	      setvar target -1
	      ai AITROOPJETDEAD
	      break
      }
      
      ifrnd 32
        iffloordistl 32
      {
        sound LIZARD_BEG
        spawn BLOODPOOL
        strength 0
        move 0
        action ATROOPSUFFERING
        setvar target -1
        setvar peractor7 -1
        setvar targetlock 0
        break
      }
      action ATROOPDYING
      break
    }
  }
  else
  {
    state random_wall_jibs
    sound PRED_PAIN

    ifwasweapon SHRINKSPARK
    {
	  cactor LIZTROOP
      sound ACTOR_SHRINKING
      ai AITROOPSHRUNK
      setvar target -1
      setvar peractor7 -1
      setvar targetlock 0
    }
    else
      ifwasweapon GROWSPARK
        sound EXPANDERHIT
    else
      iffloordistl 32
        ifrnd 96
        ifvarn sprite[THISACTOR].htpicnum BULLETWOUND
        {
	      ifai AITROOPDUCKING ifvare smartmonsters YES ifspritepal 21 nullop else
	      {
		      cactor LIZTROOP
	          action ATROOPFLINTCH
	          setvar target -1
      		  setvar peractor7 -1
      		  setvar targetlock 0
          }
        }
        else ifvarg stun 0
        {
	        cactor LIZTROOP
	        action ATROOPFLINTCH
	        move STOPPED
	        setvar target -1
      		setvar myspawner -1
      		setvar targetlock 0
    	}
  }
ends

state troopjetpackstate

cactor LIZTROOP

setvar temp 0
ifaction ATROOPJETPACKILL setvar temp 1
ifaction ATROOPJETPACKILLFAST setvar temp 1
  ifvare temp 1
  {
    //ifvarg canseeplayer 0
      ifactioncount 2
    {
	  setvar temp 0
	  ifvare playerally 0
	  ifvarg canseeplayer 0 setvar temp 1
	  ifvarn target -1 setvar temp 1
	  ifvare temp 1
	  {
		  state troopshooting
	      resetactioncount
      }
    }

    ifp phigher
      ai AITROOPJETPACK
    else
      ifinwater
        ai AITROOPJETPACK
    else
      ifvarg underwater 0
        ai AITROOPJETPACK
    else
      ifcount 26
        iffloordistl 32
          ai AITROOPSEEKPLAYER
  }
  else
    ifcount 48
      //ifvarg canseeplayer 0
  {
	  setvar temp 0
	  ifvarg canseeplayer 0 setvar temp 1
	  ifvarn target -1 setvar temp 1
	  ifvare temp 1
	  {
	    action ATROOPJETPACKILL
	    move TROOPJETPACKILLVELS seekplayer
      }
  }
  ifvare target -1
  {
	  getactor[THISACTOR].z z
	  getplayer[THISACTOR].posz mz
	  subvarvar mz z
	  ifvarg mz 16384
	  {
		shiftvarr mz 5
	  	movesprite THISACTOR 0 0 mz CLIPMASK0 RETURN
  	  }
  }
ends

state checksquished
  ifsquished
  {
    addkills 1 state specialdeath
    sound SQUISHED
    state standard_jibs
    state random_ooz
    killit
  }
ends

state troopsufferingstate
  ifactioncount 2
  {
    ifrnd 16
      spawn WATERDRIP
    ifactioncount 14
    {
      stopsound LIZARD_BEG
      cstat 0
      strength 0
      action ATROOPSUFFERDEAD
      break
    }
  }
ends

state troopshrunkstate
  ifcount SHRUNKDONECOUNT
    ai AITROOPSEEKENEMY
  else
    ifcount SHRUNKCOUNT
      sizeto 48 40
  else
    state genericshrunkcode
ends

state medichidestate


ifvarn peractor7 -1
{
	getactor[peractor7].statnum temp
	ifvarn temp 1 setvar peractor7 -1
}
ifvarn peractor7 -1
{
	setvarvar spriteid peractor7
	state troopmedcheck
}
ifvare peractor7 -1 { ifaction ATROOPRUNNING count 150 }
else
ifvarvarn peractor7 THISACTOR
{
	getactor[peractor7].x mx
	getactor[peractor7].y my
	getactor[THISACTOR].x x
	getactor[THISACTOR].y y
	subvarvar mx x
	subvarvar my y
	getangle angvar mx my
	setactor[THISACTOR].ang angvar
}

  ifaction ATROOPREAPPEAR
  {
	setactor[THISACTOR].htextra -1
    ifactioncount 2
    {
      sound TELEPORTER
      cactor LIZTROOPDUCKING
      cstat 257
      setvar mtype 0
      action ATROOPDUCK
	  move STOPPED
	  ifvarn peractor7 -1 ifvarg tempc 1280 { ai AITROOPDUCKING setvar peractor7 -1 }
    }
    else
    {
      sizeto 41 40
      sizeto 41 40
      sizeto 41 40
      sizeto 41 40
      spawn FRAMEEFFECT1
    }
  }
  else
    ifaction ATROOPRUNNING
  {
	  setactor[THISACTOR].htextra -1
      setvar temp 0
      ifcount 130 setvar temp 1
      ifvarn peractor7 -1
      ifvarl tempc 1024 setvar temp 1
      ifvare temp 1
      {
        spawn TRANSPORTERSTAR
        action ATROOPREAPPEAR
        move STOPPED
        break
      }
    
  }
  else
    ifaction ATROOPHIDE
  {
    ifactioncount 2
    {
      spawn TRANSPORTERSTAR
      sound TELEPORTER
      action ATROOPRUNNING
      move TROOPRUNVELS geth
      cstat 32768
    }
    else
    {
      sizeto 4 40
      sizeto 4 40
      sizeto 4 40
      sizeto 4 40
      spawn FRAMEEFFECT1
    }
  }
  else
    ifaction ATROOPABOUTHIDE
      ifactioncount 2
    {
      action ATROOPHIDE
      cstat 0
    }
ends

state troopmedicstate

ifvare mtype 1
{
	state medichidestate
	break
}

ifvare peractor7 -1 { cactor LIZTROOPDUCKING ai AITROOPDUCKING break }
ifvarl ammo 2 { setvar peractor7 -1 cactor LIZTROOP ai AITROOPSEEKENEMY break }
getactor[peractor7].statnum temp
ifvarn temp 1 { setvar peractor7 -1 cactor LIZTROOPDUCKING ai AITROOPDUCKING break }
setvarvar spriteid peractor7
state troopmedcheck
ifvare peractor7 -1 { cactor LIZTROOP ai AITROOPSEEKENEMY break }

// target is now validated
ifvarvarn peractor7 THISACTOR
{
	getactor[peractor7].x mx
	getactor[peractor7].y my
	getactor[THISACTOR].x x
	getactor[THISACTOR].y y
	subvarvar mx x
	subvarvar my y
	getangle angvar mx my
	setactor[THISACTOR].ang angvar
	setvar temp 0
	ifbulletnear setvar temp 1
	ifvarvare scannedsprite THISACTOR setvar temp 1
	ifvare temp 1
	ifvarg tempc 2048
	{
		setvar mtype 1
		// cloak!
		action ATROOPABOUTHIDE
		move STOPPED
		break
	}
}

// now facing the target
// distance to target is still in tempc from state troopmedcheck
ifvarl tempc 1280
{
	// target's strength is still in tempb
	ifvarl tempc 1024
	{
		cactor LIZTROOPDUCKING
		action ATROOPDUCK
		move STOPPED
	}
	addvar tempb 1
	setactor[peractor7].extra tempb
	subvar ammo 2
	espawn HEALTHICON
	subvar z 12288
	setactor[RETURN].z z
	setactorvar[RETURN].myspawner THISACTOR
	soundonce TROOPHEAL
} else ifcount 6
ifmove STOPPED
{
	cactor LIZTROOP
 	ai AITROOPMEDIC
}

ends

state jetdeadstate

getactor[THISACTOR].ang angvar
addvar angvar 48
randvar temp 48
addvarvar angvar temp
setactor[THISACTOR].ang angvar



ifhitweapon
{
    ifwasweapon RPG
    {
      sound SQUISHED
      state troop_body_jibs
      state standard_jibs
      killit
    }
    else
      ifwasweapon RADIUSEXPLOSION
    {
      sound SQUISHED
      state troop_body_jibs
      state standard_jibs
      killit
    }
}
else ifcount 30 ifrnd 1
{
	globalsound NEW_EXPLODE
	spawn EXPLOSION2
	state troop_body_jibs
    state standard_jibs
    killit
}
	

soundonce DUKE_JETPACK_IDLE
espawn SMALLSMOKE
setactorvar[RETURN].peractor1 1
setactor[RETURN].cstat 32768

ifmove TROOPJETDEADVELS
{
	ifrnd 64
	{
		ifrnd 128 cstat 260 else cstat 256
	}
	ifrnd 128
	state sparkstate
	ifceilingdistl 64
	{
		move TROOPJETDEADFALL
		cstator 8
	}
	else ifcount 120
	{
		move TROOPJETDEADFALL
		cstator 8
	}
} else ifmove TROOPJETDEADFALL
{
	ifrnd 64
	{
		ifrnd 128 cstat 268 else cstat 264
	}
	iffloordistl 2
	{
		sound SQUISHED
		guts JIBS6 4
		action ANULLACTION
        move 0
        cstat 256
        cactor LIZTROOPCORPSE
    } else
    ifcount 150
    {
	    action ANULLACTION
        move 0
        cstat 256
        cactor LIZTROOPCORPSE
    }
}
		

ends


state troopcode fall
state monsterai
  ifinwater
    ifrnd 1
      spawn WATERBUBBLE
  ifvarg underwater 0
    ifrnd 1
      spawn WATERBUBBLE
      
  ifai AITROOPJETDEAD { state jetdeadstate break }

  ifaction ATROOPSTAND
  {
    ifrnd 192
      ai AITROOPSHOOTING
    else
      ai AITROOPSEEKPLAYER
  }
  else
    ifaction ATROOPFROZEN
  {
    ifcount THAWTIME
    {
      ai AITROOPSEEKENEMY
      getlastpal
    }
    else
      ifcount FROZENDRIPTIME
    {
      ifactioncount 26
      {
        spawn WATERDRIP
        resetactioncount
      }
    }
    ifhitweapon
    {
      ifwasweapon FREEZEBLAST
      {
        strength 0
        break
      }
      addkills 1 
      state specialdeath

      ifrnd 84
        spawn BLOODPOOL
      lotsofglass 30
      state standard_jibs
      state troop_body_jibs
      sound GLASS_BREAKING
      killit
    }
    ifp pfacing
      ifpdistl FROZENQUICKKICKDIST
      ifvare superkicking 0
        pkick
    break
  }
  else ifaction ATROOPPLAYDEAD
  {
    ifhitweapon
    {
      ifwasweapon RADIUSEXPLOSION
      {
        sound SQUISHED
        state troop_body_jibs
        state standard_jibs
        killit
      }
      break
    }
    else
      state checksquished

    ifcount PLAYDEADTIME
    {
      addkills -1
      setvar monstatus 0
      soundonce PRED_ROAM
      cstat 257
      strength 1
      //ai AITROOPSHOOTING
      cactor LIZTROOPDUCKING
      ai AITROOPDUCKING
      action ATROOPDUCKSHOOT
    }
    else
      ifp pfacing
        resetcount

    break
  }
  else
    ifaction ATROOPDEAD
  {
    strength 0
    ifrespawn
      ifcount RESPAWNACTORTIME
    {
	  addkills -1
      spawn TRANSPORTERSTAR
      cstat 257
      strength TROOPSTRENGTH
      setvar monstatus 1
      ai AITROOPSEEKENEMY
    }
    ifhitweapon
    {
      ifwasweapon RADIUSEXPLOSION
      {
        sound SQUISHED
        state troop_body_jibs
        state standard_jibs
        killit
      }
      break
    }
    else
      state checksquished
    break
  }
  else
    ifaction ATROOPSUFFERDEAD
  {
    ifactioncount 2
    {
      ifrnd 64
      {
        resetcount
        action ATROOPPLAYDEAD
      }
      else
      {
        soundonce PRED_DYING
        action ANULLACTION
        move 0
        cactor LIZTROOPCORPSE
        //action ATROOPDEAD
      }
    }
  }
  else
    ifaction ATROOPDYING
  {
    state troopdying
    break
  }
  else
    ifaction ATROOPSUFFERING
  {
    state troopsufferingstate
    ifhitweapon
      state checktroophit
    break
  }
  else
    ifaction ATROOPFLINTCH
  {
	ifvare stun 0
    ifactioncount 4
      ai AITROOPSEEKENEMY
  }
  else
  {
    ifai AITROOPSEEKPLAYER
      state troopseekstate
    else
      ifai AITROOPJETPACK
    {
      state troopjetpackstate
      ifinwater nullop
      else
      ifvarg underwater 0 nullop
      else
        soundonce DUKE_JETPACK_IDLE
    }
    else
      ifai AITROOPSEEKENEMY
        state troopseekstate
    else
      ifai AITROOPSHOOTING
        state troopshootstate
    else
      ifai AITROOPFLEEING
        state troopfleestate
    else
      ifai AITROOPFLEEINGBACK
        state troopfleestate
    else
      ifai AITROOPDODGE
        state troopseekstate
    else
      ifai AITROOPDUCKING
        state troopduckstate
    else
      ifai AITROOPSHRUNK
        state troopshrunkstate
    else
      ifai AITROOPGROW
        state genericgrowcode
    else
      ifai AITROOPMEDIC
      {
        state troopmedicstate
        ifvare mtype 1 break
      }
    else
      ifai AITROOPHIDE
    {
      state troophidestate
      break
    }
  }

  ifhitweapon
    state checktroophit
  else
    state checksquished
ends

state checktrooppalette
  ifai 0
  {
	 // sizeat 52 42 fat liztroop size
     ifspritepal 21
       addstrength TROOPSTRENGTH // Double the hitpoint vals     
     else ifvare smartmonsters YES
     {
	     ifvarn randomize 0 ifspritepal 3 spritepal 22
	     ifrnd 64
	     ifspritepal 22
	     {
		     spritepal 0
		     setvar peractor7 -1
		     addstrength 10
	     }
     } 
  }
ends


actor LIZTROOPJETPACK TROOPSTRENGTH
  state checktrooppalette
  ai AITROOPJETPACK
  cactor LIZTROOP
enda

actor LIZTROOPDUCKING TROOPSTRENGTH
  ifai 0 
  {  state checktrooppalette
     ai AITROOPDUCKING
     ifgapzl 48
       move DONTGETUP
  }
  //cactor LIZTROOP
  state troopcode
  
enda

actor LIZTROOPSHOOT TROOPSTRENGTH ATROOPSTAND
  state checktrooppalette
  ai AITROOPSHOOTING
  cactor LIZTROOP
enda
actor LIZTROOPSTAYPUT TROOPSTRENGTH ATROOPSTAYSTAND
  state checktrooppalette
  ai AITROOPSEEKPLAYER
  cactor LIZTROOP
enda
actor LIZTROOPRUNNING TROOPSTRENGTH ATROOPSTAND
  state checktrooppalette
  ai AITROOPSEEKPLAYER
  cactor LIZTROOP
enda
actor LIZTROOPONTOILET TROOPSTRENGTH
  ifcount 24
  {
    sound FLUSH_TOILET
    operate
    ai AITROOPSEEKPLAYER
    cactor LIZTROOP
  }
  else
    ifcount 2
      nullop
  else
    state checktrooppalette
    ifhitweapon
    state checktroophit
    ifactor LIZTROOP spawn FECES
enda

actor LIZTROOPJUSTSIT TROOPSTRENGTH
  ifcount 30
  {
    operate
    ai AITROOPSEEKPLAYER
    cactor LIZTROOP
  }
  else
    ifcount 2
      nullop
  else
    state checktrooppalette
    ifhitweapon
    state checktroophit
    ifactor LIZTROOP spawn FECES

enda


actor LIZTROOP TROOPSTRENGTH ATROOPSTAND
  state checktrooppalette
  state troopcode
enda


state shootorspit
ifvarand monstflags 2048 ifvare ammo 200
ifvare playerally 0
{
	ifai AILIZSPIT break
	ai AILIZSPIT
	break
}
//ifai AILIZSHOOTENEMY break
ai AILIZSHOOTENEMY
ends

state lizseekbotstate

getactor[THISACTOR].z z
getactor[target].z mz

 ifactornotstayput
  {
        ifvarl xydist 2048
          ifcount 16
    {
      ai AILIZSHOOTENEMY
      break
    }
    ifai AILIZCHARGEENEMY
    {
	  ifcount 13
	    ifvarg peractor1 0 
	  { 
		  ai AILIZSHOOTENEMY 
		  break 
	  }
      ifcount 72
      {
        ai AILIZSHOOTENEMY
        break
      }
       ifvarvarg z mz
        ifvarg xydist 2048
          ifrnd 6
      {
        ai AILIZJUMPENEMY
        break
      }
    }
    else
      ifvarg xydist 4096
    {
      ifrnd 92
      {
        ifcount 48
            ai AILIZSHOOTENEMY
      }
      else
        ifcount 24
      {
        ai AILIZCHARGEENEMY
        break
      }
    }

    iffloordistl 16
    {
      ifcount 48
        ifnotmoving
      {
	      ifmove LIZWALKVELBACK { ai AILIZSHOOTENEMY break } else
		  ifmove LIZRUNVELBACK { ai AILIZSHOOTENEMY break } else
        ai AILIZJUMPENEMY
        break
      }
    }
    else
    {
      ifvarg xydist 1280
        ai AILIZJUMPENEMY
      break
    }

    ifrnd 4
      ifnotmoving
        operate
    else
      ifrnd 1
        ifbulletnear
    {
      ifgapzl 128
        ai AILIZDODGE
      else
        ifactornotstayput
      {
        ifrnd 32 ifvare peractor1 0
          ai AILIZJUMPENEMY
        else
          ai AILIZDODGE
      }
    }
  }
  else
  {
    ifactioncount 16
    {
        ifrnd 32
              ai AILIZSHOOTENEMY
    }
    ifcount 16
      ifrnd 32
        move LIZWALKVEL randomangle geth
  }

ends

state lizseekstate

ifvarn target -1 { state lizseekbotstate break }

  ifactornotstayput
  {
      ifp palive
        ifpdistl 2048
          ifcount 16
    {
	    ifvarand monstflags 2048 ifvare playerally 0
		{
			ifvarg targetlock 0 
			{
				state shootorspit 
				break 
			}
		}
		else
         ifvarg canseeplayer 0
            ifvarg canshootplayer 0
		    {
			  ifvarn playerally 0 ai AILIZFLEENEMY else
		      state shootorspit
		      break
		    }
	}
    ifai AILIZCHARGEENEMY
    {
	  ifcount 13
	    ifvarg peractor1 0 ifvare playerally 0
	  {
		 ifvarand monstflags 2048
		 {
			 ifvarg targetlock 0
			 {
				 state shootorspit
				 break
			 }
		 } else
	     ifvarg canseeplayer 0 ifvarg canshootplayer 0
		  {
			  state shootorspit
			  break 
		  }
	  }
      ifcount 72
      {
	    ifvarand monstflags 2048
	    {
		    ifvarg targetlock 0
		    {
			    ifvarn playerally 0 ai AILIZGETENEMY else
		        state shootorspit
		        break
	        }
	    }
	    else
        ifvarg canshootplayer 0
	      {
		    ifvarn playerally 0 ai AILIZGETENEMY else
	        state shootorspit
	        break
	      }
      }
      ifp phigher
        ifpdistg 2048
          ifrnd 6
      {
        ai AILIZJUMPENEMY
        break
      }
    }
  
    else
      ifpdistg 4096
    {
      ifrnd 92
      {
	    ifcount 48
	      ifvare playerally 0
	      ifvarand monstflags 2048 
	      {
		      ifvarg targetlock 0 state shootorspit else
          	  ifvarg canshootplayer 0
                state shootorspit
          }
        
      }
      else
        ifcount 24
        {
          ai AILIZCHARGEENEMY
          break
        }
    }
	

    iffloordistl 16
    {
      ifcount 48
        ifnotmoving
          ifvarg canseeplayer 0
      {
	      ifmove LIZWALKVELBACK { ifvarn playerally 0 ai AILIZTHINK else ai AILIZSHOOTENEMY break } else
		  ifmove LIZRUNVELBACK { ifvarn playerally 0 ai AILIZTHINK else ai AILIZSHOOTENEMY break } else
        ai AILIZJUMPENEMY
        break
      }
    }
    else
    {
      ifpdistg 1280
        ai AILIZJUMPENEMY
      break
    }

    ifrnd 4
      ifnotmoving
        operate
    else
      ifrnd 1
        ifbulletnear
    {
      ifgapzl 128
        ai AILIZDODGE
      else
        ifactornotstayput
      {
        ifrnd 32 ifvare peractor1 0
          ai AILIZJUMPENEMY
        else
          ai AILIZDODGE
      }
    }
  }
  else
  {
    ifactioncount 16
    {
      ifp palive
        ifrnd 32
          ifvare playerally 0
          {
	        ifvarand monstflags 2048 ifvarg targetlock 0 state shootorspit
	        else
            ifvarg canseeplayer 0
              ifvarg canshootplayer 0    
                state shootorspit
          }
    }
    ifcount 16
      ifrnd 32
        move LIZWALKVEL randomangle geth
  }

ends

state lizshrunkstate
  ifcount SHRUNKDONECOUNT
    ai AILIZGETENEMY
  else
    ifcount SHRUNKCOUNT
      sizeto 48 40
  else
    state genericshrunkcode
ends

state lizfleestate
  ifcount 16
  {
    ifrnd 48
      ifp palive
      {
        ifvarg canseeplayer 0
          ai AILIZSPIT
        ifvarn target -1
          ai AILIZSPIT
      }
  }
  else
  {
    iffloordistl 16 nullop
    else
      ai AILIZGETENEMY
    break
  }
ends

state lizthinkstate
  ifrnd 8
    soundonce CAPT_ROAM
  ifactioncount 3
  {
	ifvarand monstflags 2048 ifvare ammo 200
	ifvare playerally 0
	{
		ai AILIZSPIT
		break
	}
	ifvarn target -1
	{
		ifrnd 32
		  ai AILIZSPIT
		else
		  ifrnd 96
		    ai AILIZGETENEMY
	}
	else
	{
    ifrnd 32
      ifp palive
        ifvarg canseeplayer 0
         ifvare playerally 0
          ai AILIZSPIT
        else
          ifrnd 96
            ai AILIZGETENEMY
    }
  }
  else
    ifactioncount 2
      ifrnd 1
        spawn FECES
  ifrnd 1
    ifbulletnear
  {
    ifgapzl 96
      ai AILIZDODGE
    else
    {
      ifrnd 128
        ai AILIZJUMPENEMY
      else
        ai AILIZDODGE
    }
  }
ends

state lizshootbot

state calczdist
ldist xydist THISACTOR target
ifvare xydist 0 setvar xydist 1

ifvarand monstflags 64
{
	setprojectile[RAILSLUG_WEAK].workslike 36866
	mulvar zdist 844
	divvarvar zdist xydist
	sound SHOOTRAIL
	ifvarand monstflags 8 setprojectile[RAILSLUG_WEAK].extra 30
	zshoot zdist RAILSLUG_WEAK
	ifvarand monstflags 8 setprojectile[RAILSLUG_WEAK].extra 15
	setprojectile[RAILSLUG_WEAK].workslike 32770
} else
ifvarand monstflags 32
{
	ifvarand morefx 16
	ifvarn rendmode 0 spawn SHOTSPARK3
	mulvar zdist 844
	divvarvar zdist xydist
	setprojectile[FIREBULLET].workslike 36870
	sound RPG_SHOOT
	zshoot zdist FIREBULLET
	ifvarand monstflags 8 zshoot zdist FIREBULLET
	setprojectile[FIREBULLET].workslike 32774
} else
ifvarand monstflags 16
{
	ifvarand morefx 16
	ifvarn rendmode 0 spawn SHOTSPARK3
	mulvar zdist 768
	divvarvar zdist xydist
	setprojectile[STARPROJ].workslike 38914
	sound PISTOL_LOW
	zshoot zdist STARPROJ
	ifvarand monstflags 8 zshoot zdist STARPROJ
	setprojectile[STARPROJ].workslike 34818
} else
{
	ifvarand morefx 16
	ifvarn rendmode 0 spawn SHOTSPARK3
	shiftvarl zdist 8
	divvarvar zdist xydist
	ifvare dpsounds YES sound DPCHAINGUNFIRE else
	sound CAPT_ATTACK
	zshoot zdist SHOTSPARK1
	ifvarand monstflags 8 zshoot zdist SHOTSPARK1
}
state shelleject
resetactioncount


ends

state lizshooting

ifvarn target -1 { state lizshootbot break }

ifvarand monstflags 64
{
	sound SHOOTRAIL
	ifvarand monstflags 8 setprojectile[RAILSLUG_WEAK].extra 30
	shoot RAILSLUG_WEAK
	ifvarand monstflags 8 setprojectile[RAILSLUG_WEAK].extra 15
	ifvarand monstflags 8 shoot RAILSLUG_WEAK
} else
ifvarand monstflags 32
{
	sound RPG_SHOOT
	shoot FIREBULLET
	ifvarand monstflags 8 shoot FIREBULLET
} else
ifvarand monstflags 16
{
	ifvarn rendmode 0 ifvarand morefx 16 spawn SHOTSPARK3
	sound PISTOL_LOW
	shoot STARPROJ
	ifvarand monstflags 8 shoot STARPROJ
} else
{
	ifvarn rendmode 0 ifvarand morefx 16 spawn SHOTSPARK3
	ifvare dpsounds YES sound DPCHAINGUNFIRE else
	sound CAPT_ATTACK
	shoot SHOTSPARK1
	ifvarand monstflags 8 shoot SHOTSPARK1
}
state shelleject
resetactioncount

ends

state lizshootbotstate

  ifcount 20
    ifrnd 8
  {
      ifvarl xydist 2048
    {
      ifrnd 128
        ai AILIZFLEENEMY
      break
    }
    ifrnd 80
      ai AILIZTHINK
    else
      ai AILIZGETENEMY
  }
  ifactioncount 2
  {
	canseespr THISACTOR target temp
    ifvare temp 1
    {
      //ifrnd 224
      //{
	    state lizshooting
      //}
      //else
      //  ai AILIZTHINK
    }
    else
      ai AILIZGETENEMY
  }

ends

state lizshootstate

ifvarn target -1 { state lizshootbotstate break } else
ifvarn playerally 0 { ai AILIZGETENEMY break }

  ifcount 20
    ifrnd 8
  {
    ifvarg canseeplayer 0
      ifpdistl 2048
    {
      ifrnd 128
        ai AILIZFLEENEMY
      break
    }
    ifrnd 80
      ai AILIZTHINK
    else
      ai AILIZGETENEMY
  }
  
  ifactioncount 2
  {
	  ifvarand monstflags 2048 
	  {
		  ifvarg targetlock 0 state lizshooting
		  else ai AILIZTHINK
	  }
		else
		    state lizshooting
  }
ends

state checklizhit
  ifvarn sprite[THISACTOR].htpicnum BULLETWOUND
  spawn BLOOD
  ifai AILIZSHRUNK
  {
    addkills 1 state specialdeath
    sound SQUISHED
    state standard_jibs
    killit
  }
  ifdead
  {
    ifwasweapon FREEZEBLAST
    {
      sound SOMETHINGFROZE
      spritepal 1
      move 0
      action ALIZFROZEN
      strength 0
      state icesmoke
      break
    }

    state drop_chaingun

    ifwasweapon GROWSPARK
    {
      cstat 0
      sound ACTOR_GROWING
      ai AILIZGROW
      break
    }

    addkills 1 state specialdeath

    ifwasweapon RPG
    {
      sound SQUISHED
      state liz_body_jibs
      state standard_jibs
      killit
    }
    else
      ifwasweapon RADIUSEXPLOSION
    {
      sound SQUISHED
      state liz_body_jibs
      state standard_jibs
      killit
    }
    else
    {
      state rf
      ai AILIZDYING
      ifrnd 64
        spawn BLOODPOOL
    }
    sound CAPT_DYING
  }
  else
  {
    sound CAPT_PAIN
    ifwasweapon SHRINKSPARK
    {
      sound ACTOR_SHRINKING
      ai AILIZSHRUNK
      break
    }
    ifwasweapon GROWSPARK
      sound EXPANDERHIT
    
    ifvarn sprite[THISACTOR].htpicnum BULLETWOUND
    ifvarg stun 0 ai AILIZHIT
    else
    ifwasweapon KNEE ifvarn superkicking 0
    ai AILIZHIT

    state random_wall_jibs
    ifai AILIZHIT break
    
    ifvarn target -1 ai AILIZSHOOTENEMY else
    ifp palive
    ifvare playerally 0
    {
	  ifvarand monstflags 2048
	  {
		  ifvarg targetlock 0 state shootorspit
	  }
	  else
      ifvarg canseeplayer 0
        ifvarg canshootplayer 0
          
      state shootorspit
    }
      

    
  }
ends

state lizjumpstate
  ifaction ALIZFALL
  {
    iffloordistl 16
      ai AILIZGETENEMY
  }
  else
    ifactioncount 3
      action ALIZFALL
ends

state lizdyingstate
  ifaction ALIZLYINGDEAD
  {
    strength 0
    ifhitweapon
      ifwasweapon RADIUSEXPLOSION
    {
      sound SQUISHED
      state standard_jibs
      killit
    }
    ifcount RESPAWNACTORTIME
      ifrespawn
    {
	  addkills -1
      spawn TRANSPORTERSTAR
      cstat 257
      setvar monstatus 1
      strength LIZSTRENGTH
      ai AILIZGETENEMY
    }
  }
  else
    ifai AILIZDYING
      ifactioncount 6
  {
    iffloordistl 8
      sound THUD
    //move STOPPED
    //action ALIZLYINGDEAD
    action ANULLACTION
    move 0
    cactor LIZMANCORPSE
  }
ends

state lizdodgestate
  ifcount 13
    ai AILIZGETENEMY
ends

actor LIZMANSTAYPUT LIZSTRENGTH ai AILIZGETENEMY cactor LIZMAN enda
actor LIZMANSPITTING LIZSTRENGTH ai AILIZSPIT cactor LIZMAN enda
actor LIZMANJUMP LIZSTRENGTH ai AILIZJUMPENEMY cactor LIZMAN enda

state lizcode

  state monsterai
  state checksquished
  ifai 0
    ai AILIZGETENEMY
  else
    ifaction ALIZLYINGDEAD
  {
    fall
    state lizdyingstate
    break
  }
  else
    ifaction ALIZFROZEN
  {
    ifcount THAWTIME
    {
      ai AILIZGETENEMY
      getlastpal
    }
    else
      ifcount FROZENDRIPTIME
    {
      ifactioncount 26
      {
        spawn WATERDRIP
        resetactioncount
      }
    }

    ifhitweapon
    {
      ifwasweapon FREEZEBLAST
      {
        strength 0
        break
      }
      addkills 1 state specialdeath
      state drop_chaingun

      ifrnd 84
        spawn BLOODPOOL
      lotsofglass 30
      state standard_jibs
      state liz_body_jibs
      sound GLASS_BREAKING
      killit
    }
    ifp pfacing
      ifpdistl FROZENQUICKKICKDIST
      ifvare superkicking 0
        pkick
    break
  }
  else
    ifai AILIZJUMPENEMY
      state lizjumpstate
  else
  {
    fall
    ifai AILIZGETENEMY
      state lizseekstate
    else
      ifai AILIZCHARGEENEMY
        state lizseekstate
    else
      ifai AILIZDODGE
        state lizdodgestate
    else
      ifai AILIZSHOOTENEMY
        state lizshootstate
    else
      ifai AILIZFLEENEMY
        state lizfleestate
    else
      ifai AILIZTHINK
        state lizthinkstate
    else
      ifai AILIZSHRUNK
        state lizshrunkstate
    else
      ifai AILIZGROW
        state genericgrowcode
    else
      ifai AILIZDYING
        state lizdyingstate
    else
      ifai AILIZHIT
    {
	  ifcount 6 
	  ifvare stun 0
	  {
		  ifrnd 128 ifvarg canshootplayer 0
		  ai AILIZSHOOTENEMY else
		  ai AILIZTHINK
		  
	  }
    }
    else
      ifai AILIZSPIT
    {
      ifcount 26
        ai AILIZGETENEMY
      else
        ifcount 18
          
      {
	    ifvarand monstflags 2048 ifvare playerally 0 ifvare ammo 200
	    {
		    findplayer xydist
		    divvar xydist 3
		    ifvarl xydist 1 setvar xydist 1
		    mulvar xydist -1
		    zshoot xydist LIZARDVOMIT
		    sound LIZARD_SPIT
		    subvar ammo 50
		    break
	    }
	    ifrnd 96
	    {
		    ifvarn playerally 0 ifvare target -1 nullop else
		    {
		        shoot SPIT
		        ifvarand monstflags 8 shoot SPIT
		        sound LIZARD_SPIT
	        }
        }
      }
    }
  }

  ifai AILIZSHRUNK
    break
  ifhitweapon
    state checklizhit
ends

actor LIZMAN LIZSTRENGTH
  fall
  state lizcode
enda


action DRONEFRAMES   0   1   7   1   1
action DRONESCREAM   0   1   7   1   1
move DRONERUNVELS 128 64
move DRONERUNUPVELS 128 -64
move DRONEBULLVELS 252 -64
move DRONEBACKWARDS -64 -64
move DRONERISE 32 -32
move DRONESTOPPED -16

ai AIDRONEGETE DRONESCREAM DRONERUNVELS faceplayerslow getv
ai AIDRONEWAIT DRONEFRAMES DRONESTOPPED faceplayerslow
ai AIDRONEGETUP DRONESCREAM DRONERUNUPVELS faceplayer getv
ai AIDRONEPULLBACK DRONEFRAMES DRONEBACKWARDS faceplayerslow
ai AIDRONEHIT DRONESCREAM DRONEBACKWARDS faceplayer
ai AIDRONESHRUNK DRONEFRAMES SHRUNKVELS fleeenemy
ai AIDRONEDODGE DRONEFRAMES DRONEBULLVELS dodgebullet geth
ai AIDRONEDODGEUP DRONEFRAMES DRONERISE getv geth

state dronedead
  addkills 1 state specialdeath
  debris SCRAP1 8
  debris SCRAP2 4
  debris SCRAP3 7
  spawn EXPLOSION2
  sound RPG_EXPLODE
  ifvarand monstflags 8 hitradius 2048 30 40 50 60 else
  hitradius 2048 15 20 25 30
  killit
ends

state checkdronehitstate

  ifdead state dronedead
  else ifsquished state dronedead
  else
  {
    sound DRON_PAIN
    ifbulletnear
    {
      ifceilingdistl 64
        ifrnd 48
          ai AIDRONEDODGE
      ai AIDRONEDODGEUP
    }
    else
      ai AIDRONEGETE
  }
ends

state droneshrunkstate
  ifcount 24
    killit
  else
    sizeto 1 1
ends

state checkdronenearplayer

setvar temp 0

ifvarn target -1 { ifvarl xydist 1596 setvar temp 1 } else
ifvare playerally 0
ifp palive ifpdistl 1596 
{
	ifvarand monstflags 2048 ifvarl targetlock 12 nullop else
	setvar temp 1
}

  ifvare temp 1
  {
    ifcount 8
    {
	  ifvarand monstflags 16 { ai AIDRONEPULLBACK break }
	  ifvarand monstflags 32 { ai AIDRONEPULLBACK break }
	  ifvarand monstflags 64 { ai AIDRONEPULLBACK break }
      addkills 1 state specialdeath
      sound DRON_ATTACK2
      debris SCRAP1 8
      debris SCRAP2 4
      debris SCRAP3 7
      spawn EXPLOSION2
      sound RPG_EXPLODE
      ifvarand monstflags 8 hitradius 2048 30 40 50 60 else
      hitradius 2048 15 20 25 30
      killit
    }
    else
      ifcount 3 nullop
      else
        sound LASERTRIP_ARMING
  }

ends

state dronegetbotstate

getactor[THISACTOR].z z
getactor[target].z mz


      ifbulletnear
      {
        ai AIDRONEDODGE
        break
      }
      ifmove DRONEBULLVELS
      {
        ifcount 64
          ai AIDRONEPULLBACK
        else
          ifnotmoving
            ifcount 16
              ai AIDRONEPULLBACK
      }
      else
        ifcount 32
      {
        ifvarvarg z mz
          move DRONEBULLVELS geth getv
        else
          move DRONEBULLVELS geth
      }
   
ends

state dronegetstate

ifvarn target -1 { state dronegetbotstate break }

  ifrnd 192
  {
    ifvarg canseeplayer 0
    {
      ifbulletnear
      {
        ai AIDRONEDODGE
        break
      }
      ifmove DRONEBULLVELS
      {
        ifcount 64
          ai AIDRONEPULLBACK
        else
          ifnotmoving
            ifcount 16
              ai AIDRONEPULLBACK
      }
      else
        ifcount 32
      {
        ifp phigher
          move DRONEBULLVELS geth getv
        else
          move DRONEBULLVELS geth
      }
    }
    else
      ifrnd 1
        operate
  }
ends

state dronedodgestate
  ifai AIDRONEDODGEUP
  {
     ifcount 8
       ai AIDRONEGETE
     else
       ifnotmoving
         ai AIDRONEGETE
  }
  else
  {
    ifcount 8
      ai AIDRONEGETE
    else
      ifnotmoving
        ai AIDRONEGETE
  }
ends

actor DRONE DRONESTRENGTH
state monsterai
  state checkdronenearplayer

  ifrnd 2
    fall
  else
    soundonce DRON_JETSND

  ifaction 0
  {
    ai AIDRONEGETE
    getactor[THISACTOR].pal pal
    ifvarand monstflags 64
    {
	    espawn ROTATEGUN
		setactorvar[RETURN].myspawner THISACTOR
		setactorvar[RETURN].monstflags 32
		setactor[RETURN].pal pal
	}
    ifvarand monstflags 32
    {
	    espawn ROTATEGUN
		setactorvar[RETURN].myspawner THISACTOR
		setactorvar[RETURN].monstflags 16
		setactor[RETURN].pal pal
	} else
    ifvarand monstflags 16
    {
	    espawn ROTATEGUN
		setactorvar[RETURN].myspawner THISACTOR
		setactor[RETURN].pal pal
	}
  }

  else
    ifai AIDRONEWAIT
  {
	ifvarn target -1
	{
		getactor[THISACTOR].z z
		getactor[target].z mz
		ifactioncount 4
	      ifrnd 16
	    {
	      sound DRON_ATTACK1
	      ifvarvarg z mz
	        ai AIDRONEGETUP
	      else ai AIDRONEGETE
	    }
	}
	else
	{
	    ifactioncount 4
	      ifrnd 16
	        ifvarg canseeplayer 0
	    {
	      sound DRON_ATTACK1
	      ifp phigher
	        ai AIDRONEGETUP
	      else ai AIDRONEGETE
	    }
    }
  }
  else
    ifai AIDRONEGETE
      state dronegetstate
  else
    ifai AIDRONEGETUP
      state dronegetstate
  else
    ifai AIDRONEPULLBACK
  {
    ifcount 32
    ai AIDRONEWAIT
  }
  else
    ifai AIDRONEHIT
  {
    ifcount 8
      ai AIDRONEWAIT
  }
  else
    ifai AIDRONESHRUNK
      state droneshrunkstate
  else
    ifai AIDRONEDODGE
      state dronedodgestate
  else
    ifai AIDRONEDODGEUP
      state dronedodgestate

  ifhitweapon
    state checkdronehitstate

  ifrnd 1
    soundonce DRON_ROAM

enda




state octagetbotstate

    ifactioncount 32
    {
      ifrnd 48
      {
        sound OCTA_ATTACK1
        ai AIOCTASHOOTENEMY
        break
      }
    }
    else
      ifvarl xydist 1280
        ai AIOCTASCRATCHENEMY
  
ends

state octagetenemystate

ifvarn target -1 { state octagetbotstate break }
  ifvarg canseeplayer 0
  {
    ifactioncount 32
    {
      ifrnd 48
      {
	      ifvarand monstflags 2048
	      {
		      ifvarg targetlock 0
		      {
			      sound OCTA_ATTACK1
		          ai AIOCTASHOOTENEMY
		          break
	          }
	      }
	      else
	      ifvarg canshootplayer 0
	      {
	        sound OCTA_ATTACK1
	        ai AIOCTASHOOTENEMY
	        break
	      }
      }
    }
    else
      ifpdistl 1280
        ai AIOCTASCRATCHENEMY
  }

ends

state octascratchbotstate

  ifvarg xydist 1280
    ai AIOCTAGETENEMY
  else
    ifcount 32
  {
    resetcount
    sound OCTA_ATTACK2
    setvar temp OCTASCRATCHINGPLAYER
    mulvar temp -1
    ifvarand monstflags 8 mulvar temp 2
    setactor[target].htextra temp
    setactor[target].htpicnum KNEE
    setactor[target].htowner THISACTOR
  }
ends

state octascratchenemystate

ifvarn target -1 { state octascratchbotstate break }
ifvare target -1 ifvarn playerally 0 ai AIOCTAGETENEMY

  ifpdistg 1280
    ai AIOCTAGETENEMY
  else
    ifcount 32
  {
    resetcount
    sound OCTA_ATTACK2
    palfrom 8 32
    addphealth OCTASCRATCHINGPLAYER
    ifvarand monstflags 8 addphealth OCTASCRATCHINGPLAYER
  }
ends

state octashootbot

state calczdist
ldist xydist THISACTOR target
ifvare xydist 0 setvar xydist 1

	ifvarand monstflags 64
	{
		eshoot FIREBALLPROJ
		ifvarand monstflags 8 
		{
			setthisprojectile[RETURN].pal 13
			setthisprojectile[RETURN].extra 64
		}
	} else
	ifvarand monstflags 32
	{
		mulvar zdist 768
		divvarvar zdist xydist
		sound SHRINKER_FIRE
		zshoot zdist SHRINKER
	} else
	  ifvarand monstflags 16 
	  {
		  mulvar zdist 768
		  divvarvar zdist xydist
		  setprojectile[ELECTROPROJ].workslike 36866
		  zshoot zdist ELECTROPROJ
		  ifvarand monstflags 8 zshoot zdist ELECTROPROJ
		  setprojectile[ELECTROPROJ].workslike 32770
	  } else
	  {
		  // will always shoot at player ?
	      shoot COOLEXPLOSION1
	      ifvarand monstflags 8 shoot COOLEXPLOSION1
      }
ends

state octashooting

ifvarn target -1 { state octashootbot break }

	ifvarand monstflags 64
	{
		eshoot FIREBALLPROJ
		ifvarand monstflags 8 
		{
			setthisprojectile[RETURN].pal 13
			setthisprojectile[RETURN].extra 64
		}
	} else
	ifvarand monstflags 32
	{
		sound SHRINKER_FIRE
		shoot SHRINKER
	} else
	  ifvarand monstflags 16 
	  {
		  shoot ELECTROPROJ
		  ifvarand monstflags 8 shoot ELECTROPROJ
	  } else
	  {
	      shoot COOLEXPLOSION1
	      ifvarand monstflags 8 shoot COOLEXPLOSION1
      }
ends

state pullplayer

ifvarand monstflags 256 spawn INKBLAST

soundonce PULLPLAYER
ifpdistl 1280
{
	ifvarand monstflags 256 palfrom 45 0 0 0 else
	palfrom 40 63 63 63
	ifcount 26
	{
		resetcount
		sound OCTA_ATTACK2
		ifrnd 128 state painsounds
	    addphealth OCTASCRATCHINGPLAYER
	    ifvarand monstflags 8 addphealth OCTASCRATCHINGPLAYER
    }
}
else
{
	ifvarand monstflags 256
	{
		ifpdistl 2048 palfrom 30 0 0 0
		else ifpdistl 3072 palfrom 20 0 0 0
		else palfrom 15 0 0 0
	}
	else
	{
	 	ifpdistl 2048 palfrom 30 63 63 63
		else ifpdistl 3072 palfrom 20 63 63 63
		else palfrom 15 63 63 63
	}
}

ifpdistl 1024 break

getactor[THISACTOR].x mx
getactor[THISACTOR].y my
getplayer[THISACTOR].posx x
getplayer[THISACTOR].posy y
getplayer[THISACTOR].i spriteid
subvarvar mx x
subvarvar my y
getangle angvar mx my
cos xvel angvar
sin yvel angvar
ifinwater
{
	shiftvarr xvel 7
	shiftvarr yvel 7
} else
ifvarg underwater 0
{
	shiftvarr xvel 7
	shiftvarr yvel 7
}
else
{
	shiftvarr xvel 6
	shiftvarr yvel 6
}
ifp phigher setvar zvel 0 else setvar zvel -2048
movesprite spriteid xvel yvel zvel CLIPMASK0 RETURN
ifvare RETURN 0
{
	getactor[spriteid].x x
	getactor[spriteid].y y
	getactor[spriteid].z z
	subvar z 8192
	updatesectorz x y z temp
	ifvarn temp -1
	{
		setplayer[THISACTOR].posx x
		setplayer[THISACTOR].posy y
		setplayer[THISACTOR].posz z
	}
}

subvar ammo 1
ifmove OCTASTOPPED move STOPPED faceplayer


ends

state octashootenemystate
ifvare target -1 
{
	ifvarn playerally 0 { ai AIOCTAGETENEMY break }
	else
	ifvarand monstflags 2048
	ifpdistl 8192 ifvarg canseeplayer 0
	ifvare cannonid -1
	{
		ifmove STOPPED ifvarg ammo 0
		{
			state pullplayer
			break
		}
		else ifvarg ammo 78 ifrnd 12
		ifp pfacing
		{
			state pullplayer
			break
		}
		else ifmove STOPPED
		{
			move OCTASTOPPED
			stopsound PULLPLAYER
		}
	}
}

  ifcount 25
  {
    ifcount 27
      ai AIOCTAGETENEMY
  }
  else
    ifcount 24
    {
	  state octashooting
    }
  else
    ifactioncount 6
      resetactioncount
ends

state checkoctahitstate
  ifwasweapon SHRINKSPARK
  {
    sound ACTOR_SHRINKING
    ai AIOCTASHRUNK
  }
  else
  {
    ifdead
    {
      ifwasweapon FREEZEBLAST
      {
        sound SOMETHINGFROZE
        spritepal 1
        move 0
        action AOCTAFROZEN
        strength 0
        state icesmoke
        break
      }

      addkills 1 state specialdeath
      ifwasweapon RPG
      {
        sound SQUISHED
        state standard_jibs
        killit
      }
      else
        ifwasweapon RADIUSEXPLOSION
      {
        sound SQUISHED
        state standard_jibs
        killit
      }
      else
        ifwasweapon GROWSPARK
      {
        cstat 0
        sound ACTOR_GROWING
        ai AIOCTAGROW
        break
      }
      else
      {
        state rf
        ai AIOCTADYING
      }
      sound OCTA_DYING
    }
    else
    {
      ifwasweapon RPG
      {
        sound OCTA_DYING
        addkills 1 state specialdeath
        state standard_jibs
        killit
      }
      else
        ifwasweapon GROWSPARK
          sound EXPANDERHIT

      sound OCTA_PAIN
      ifvarn sprite[THISACTOR].htpicnum BULLETWOUND
      spawn BLOOD
      ifrnd 64
        ai AIOCTAHIT
      else ifvarg stun 0
        ai AIOCTAHIT
    }
  }
  state random_wall_jibs
ends

state octashrunkstate
  ifcount SHRUNKDONECOUNT
    ai AIOCTAGETENEMY
  else
    ifcount SHRUNKCOUNT
      sizeto 40 40
  else
    state genericshrunkcode
ends


state octadyingstate
  ifactioncount 8
  {
    ifrnd 64
      spawn BLOODPOOL
    //move OCTASTOPPED
    action ANULLACTION
    //action AOCTADEAD
    move 0
    cactor OCTABRAINCORPSE
    break
  }
  else
    ifactioncount 5 nullop
  else
    ifactioncount 4
      iffloordistl 8
        sound THUD
ends

actor OCTABRAINSTAYPUT OCTASTRENGTH
  ai AIOCTAGETENEMY
  cactor OCTABRAIN
enda

actor OCTABRAIN OCTASTRENGTH
  fall
  // orvar monstflags 256
  ifvarand monstflags 256
  {
	  orvar monstflags 2048
	  getactor[THISACTOR].cstat temp
	  
	  ifai AIOCTASHOOTENEMY
	  {
		  ifvarand temp 32768 subvar temp 32768
		  ifvarand temp 2 xorvar temp 2
		  orvar temp 256
	  }
	  else
	  {
		  ifstrength 1 ifvarand temp 32768 subvar temp 32768
		  ifvarand temp 256 xorvar temp 256
		  orvar temp 2
		  
		  addvar countvar 1
		  ifvarg countvar 32
		  ifrnd 8
		  {
			setvar countvar 0
			ifvarand temp 32768 subvar temp 32768
		  	ifrnd 128 orvar temp 512 else xorvar temp 512
		  	ifstrength 1 nullop else
		  	ifrnd 85 orvar temp 32768
	  	  }
		  
	  }
	  setactor[THISACTOR].cstat temp
	  
  }
  state monsterai
  state checksquished
  ifai 0
    ai AIOCTAGETENEMY
  else
    ifaction AOCTADEAD
  {
    strength 0
    ifcount RESPAWNACTORTIME
      ifrespawn
    {
	  addkills -1
      setvar monstatus 0
      spawn TRANSPORTERSTAR
      cstat 257
      setvar monstatus 1
      strength OCTASTRENGTH
      ai AIOCTAGETENEMY
    }
    ifhitweapon
      ifwasweapon RADIUSEXPLOSION
    {
      state standard_jibs
      killit
    }
    break
  }
  else ifaction AOCTAFROZEN
  {
    ifcount THAWTIME
    {
      ai AIOCTAGETENEMY
      getlastpal
    }
    else
      ifcount FROZENDRIPTIME
      {
        ifactioncount 26
        {
          spawn WATERDRIP
          resetactioncount
        }
      }

    ifhitweapon
    {
      addkills 1 state specialdeath
      ifwasweapon FREEZEBLAST
      {
        strength 0
        break
      }

      lotsofglass 30
      state standard_jibs
      ifrnd 84
        spawn BLOODPOOL
      sound GLASS_BREAKING
      killit
    }
    ifp pfacing
      ifpdistl FROZENQUICKKICKDIST
      ifvare superkicking 0
        pkick
    break
  }
  else
  {
    ifrnd 1
      soundonce OCTA_ROAM

    ifai AIOCTAGETENEMY
      state octagetenemystate
    else
      ifai AIOCTAHIT
      {
	    ifvare stun 0
        ifcount 8
          ai AIOCTASHOOTENEMY
      }
    else
      ifai AIOCTADYING
      {
        state octadyingstate
        break
      }
    else
      ifai AIOCTASCRATCHENEMY
        state octascratchenemystate
    else
      ifai AIOCTASHOOTENEMY
        state octashootenemystate
    else
      ifai AIOCTASHRUNK
      {
        state octashrunkstate
        break
      }
    else
      ifai AIOCTAGROW
        state genericgrowcode

    ifmove OCTAUPVELS nullop
    else
    {
	  setvar temp 0
	  ifvarn target -1
	  {
		  getactor[THISACTOR].z z
		  getactor[target].z mz
		  ifvarvarg z mz setvar temp 1
	  }
	  else
      ifp phigher setvar temp 1
      
      ifvare temp 1
        move OCTAUPVELS seekplayer
    }
    ifmove OCTAUPVELS nullop else
      ifmove OCTAINWATER nullop
      else
      {
        ifinwater
          move OCTAINWATER seekplayer else
        ifvarg underwater 0
          move OCTAINWATER seekplayer 
      }

    ifhitweapon
      state checkoctahitstate
  }
enda

state pigseekenemybot

  ifai AIPIGCHARGE
  {
	ifvarg peractor1 0 ifrnd 16
	{
		//ifvarg canseeplayer 0 ifvarg canshootplayer 0
		//{
			ai AIPIGSHOOTENEMY
			break
		//}
	}
    ifvarl xydist 3084
    {
      ifnotmoving
        ai AIPIGSEEKENEMY
      else
      {
	    cactor PIGCOPDIVE
        ai AIPIGDIVING
      }
    }
    getactor[THISACTOR].ang angvar
    ifbulletnear
    {
	    setactor[THISACTOR].ang angvar
	    cactor PIGCOPDIVE
        ai AIPIGDIVING
    }
    break
  }
  else iffloordistl 32
  {
    ifvarg xydist 4096
    {
	  ifvarg peractor1 0 
	     ifrnd 64
	     {
	       ai AIPIGSHOOTENEMY 
         }
	       else
	  ifai AIPIGDODGE nullop else
      ifactornotstayput
      ai AIPIGCHARGE
    }
    ifrnd 8
    {
      ifbulletnear
        ai AIPIGDODGE
    }
  }

  ifrnd 128
  {
    ifai AIPIGDODGE
    {
      ifcount 32
        ai AIPIGCHARGE
          break
    }
    iffloordistl 32
    {
      ifvarl xydist 1024
      {
        ai AIPIGSHOOTENEMY
        break
      }
      ifcount 48
      {
        ifrnd 8
        {
          ifrnd 192
            ai AIPIGSHOOTENEMY
          else
          {
	        cactor PIGCOPDIVE
            ai AIPIGDIVING
          }
          break
        }
      }
    }
  }
ends

state pigseekenemystate

  ifvarn target -1 { state pigseekenemybot break }

  ifai AIPIGCHARGE
  {
	ifvarn playerally 0 nullop else
	ifvarg peractor1 0 ifrnd 16
	{
		ifvarg canseeplayer 0
		// ifvarg canshootplayer 0
		{
			ifvarand monstflags 2048 ifvare targetlock 0 nullop else
			{
				ai AIPIGSHOOTENEMY
				break
			}
		}
	}
    ifvarg canseeplayer 0
      ifpdistl 3084
    {
      ifnotmoving
        ai AIPIGSEEKENEMY
      else
      ifvarand monstflags 2048 ifvare targetlock 0 nullop else
      {
	    cactor PIGCOPDIVE
        ai AIPIGDIVING
      }
    }
    getactor[THISACTOR].ang angvar
    ifbulletnear
    {
	    setactor[THISACTOR].ang angvar
	    cactor PIGCOPDIVE
        ai AIPIGDIVING
    }
    break
  }
  else iffloordistl 32
  {
    ifpdistg 4096
    {
	  ifvarg peractor1 0 
	   ifvarg canseeplayer 0 
	    // ifvarg canshootplayer 0 
	    
	     ifrnd 64
	     {
			   ifvarn playerally 0 ai AIPIGCHARGE else
			   ifvarand monstflags 2048 ifvare targetlock 0 nullop else
		       ai AIPIGSHOOTENEMY  
         }
        
	  else
	  ifai AIPIGDODGE nullop else
      ifactornotstayput
      ai AIPIGCHARGE
    }
    ifrnd 8
    {
      ifbulletnear
        ai AIPIGDODGE
    }
  }

  ifrnd 128
    ifvarg canseeplayer 0
  {
    ifai AIPIGDODGE
    {
      ifcount 32
        ai AIPIGCHARGE
          break
    }
    iffloordistl 32
    {
      ifpdistl 1024
        ifp palive
          // ifvarg canshootplayer 0
      {
	      ifvarand monstflags 2048 ifvare targetlock 0 nullop else
	      {
		    ifvarn playerally 0 ai AIPIGFLEEENEMY else
	        ai AIPIGSHOOTENEMY
	        break
	      }
      }
      ifcount 48
      {
        ifrnd 8
          ifp palive
            // ifvarg canshootplayer 0
        {
	      ifvarn playerally 0 break
	      ifvarand monstflags 2048 ifvare targetlock 0 nullop else
	      {
	          ifrnd 192
	            ai AIPIGSHOOTENEMY
	          else
	          {
		        cactor PIGCOPDIVE
	            ai AIPIGDIVING
	          }
	          break
          }
        }
      }
    }
  }
ends

state pigshootbot

ldist xydist THISACTOR target
ifvare xydist 0 setvar xydist 1
state calczdist

ifvarand monstflags 64
{
	setprojectile[RAILSLUG_WEAK].workslike 36866 //4097
	//shiftvarl zdist 8
	mulvar zdist 844
	divvarvar zdist xydist
	ifvarand monstflags 256 sound DBFIRE else
	sound SHOOTRAIL
	ifvarand monstflags 8 setprojectile[RAILSLUG_WEAK].extra 30
	zshoot zdist RAILSLUG_WEAK
	zshoot zdist RAILSLUG_WEAK
	zshoot zdist RAILSLUG_WEAK
	ifvarand monstflags 8 setprojectile[RAILSLUG_WEAK].extra 15
	ifpdistg 4096 ifrnd 128 shoot MORTER
	setprojectile[RAILSLUG_WEAK].workslike 32770 //1
}
else
ifvarand monstflags 32
{
	setprojectile[FIREBULLET].workslike 36870
	mulvar zdist 844
	divvarvar zdist xydist
	ifvarand monstflags 256 sound DBFIRE else
	sound PIG_ATTACK
	sound RPG_SHOOT
	zshoot zdist FIREBULLET
	zshoot zdist FIREBULLET
	zshoot zdist FIREBULLET
	zshoot zdist FIREBULLET
	ifvarand monstflags 8
	{
		zshoot zdist FIREBULLET
		zshoot zdist FIREBULLET
		zshoot zdist FIREBULLET
		zshoot zdist FIREBULLET
	}
	setprojectile[FIREBULLET].workslike 32774
}
else			
ifvarand monstflags 16
{
	setprojectile[STARPROJ].workslike 38914
	mulvar zdist 768
	divvarvar zdist xydist
	ifvarand monstflags 256 sound DBFIRE else
	sound PIG_ATTACK
	zshoot zdist STARPROJ
	zshoot zdist STARPROJ
	zshoot zdist STARPROJ
	zshoot zdist STARPROJ
	ifvarand monstflags 8
	{
		zshoot zdist STARPROJ
		zshoot zdist STARPROJ
		zshoot zdist STARPROJ
		zshoot zdist STARPROJ
	}
	setprojectile[STARPROJ].workslike 34818
}
else
{
//  DOESN'T LOOK GOOD; DISABLE TILL FURTHER NOTICE
// 	ifvarand morefx 16
// 	{
// 		ifactor PIGCOPDIVE 
// 		{
// 			spawn SHOTSPARK4 
// 			spawn SHOTSPARK4 
// 			spawn SHOTSPARK4 
// 		}
// 		else
// 		{
// 			spawn SHOTSPARK3
// 			spawn SHOTSPARK3
// 			spawn SHOTSPARK3
// 		}
// 	}
	shiftvarl zdist 8
	divvarvar zdist xydist
	ifvarand monstflags 256 sound DBFIRE else
	sound PIG_ATTACK
	zshoot zdist SHOTGUN
	zshoot zdist SHOTGUN
	zshoot zdist SHOTGUN
	zshoot zdist SHOTGUN
	zshoot zdist SHOTGUN
	
	ifvarand monstflags 8 
	{
		zshoot zdist SHOTGUN
		zshoot zdist SHOTGUN
		zshoot zdist SHOTGUN
		zshoot zdist SHOTGUN
		zshoot zdist SHOTGUN
	}
}

ends

state pigshooting

ifvarn target -1 { state pigshootbot break }

getactor[THISACTOR].picnum picnum

ifvarand monstflags 64
{
	ifvarand monstflags 256 sound DBFIRE else
	sound SHOOTRAIL
	ifvarand monstflags 8 setprojectile[RAILSLUG_WEAK].extra 30
	shoot RAILSLUG_WEAK
	shoot RAILSLUG_WEAK
	shoot RAILSLUG_WEAK
	ifvarand monstflags 8 setprojectile[RAILSLUG_WEAK].extra 15
	ifpdistg 4096 ifrnd 128 shoot MORTER
	
}
else
ifvarand monstflags 32
{
	ifvarand monstflags 256 sound DBFIRE else
	sound PIG_ATTACK
	sound RPG_SHOOT
	shoot FIREBULLET
	shoot FIREBULLET
	shoot FIREBULLET
	shoot FIREBULLET
	ifvarand monstflags 8
	{
		shoot FIREBULLET
		shoot FIREBULLET
		shoot FIREBULLET
		shoot FIREBULLET
	}
}
else		

ifvarand monstflags 16
{
	ifvarand monstflags 256 sound DBFIRE else
	sound PIG_ATTACK
	shoot STARPROJ
	shoot STARPROJ
	shoot STARPROJ
	shoot STARPROJ
	ifvarand monstflags 8
	{
		shoot STARPROJ
		shoot STARPROJ
		shoot STARPROJ
		shoot STARPROJ
	}
}
else
{
//	DOESN'T LOOK GOOD; DISABLE TILL FURTHER NOTICE
// 	ifvarand morefx 16
// 	{
// 		ifactor PIGCOPDIVE 
// 		{
// 			spawn SHOTSPARK4 
// 			spawn SHOTSPARK4 
// 			spawn SHOTSPARK4 
// 		}
// 		else
// 		{
// 			spawn SHOTSPARK3
// 			spawn SHOTSPARK3
// 			spawn SHOTSPARK3
// 		}
// 	}
	ifvarand monstflags 256 sound DBFIRE else
	sound PIG_ATTACK
	shoot SHOTGUN
	shoot SHOTGUN
	shoot SHOTGUN
	shoot SHOTGUN
	shoot SHOTGUN
	
	ifvarand monstflags 8 
	{
		shoot SHOTGUN
		shoot SHOTGUN
		shoot SHOTGUN
		shoot SHOTGUN
		shoot SHOTGUN
	}
}

ends

state pigdbshooting

ifvare target -1 ifvarn playerally 0 { ai AIPIGSEEKENEMY break }
	
    ifcount 12 nullop
    else
      ifcount 11
    {
	  ifvarn target -1 {  state pigshooting state pigshooting state pigshooting } else
      // ifvarg canshootplayer 0
      ifvarg canseeplayer 0 // ifcanseetarget
      {
	    // state pigshooting
	    state pigshooting
	    state pigshooting
      }
      else
        ai AIPIGSEEKENEMY
    }
    ifcount 19 nullop
    else
      ifcount 18
      {
        action APIGCOCK
        sound DBOPEN
      }
    ifcount 35 nullop
    else
      ifcount 34
      {
        state shelleject2
        state shelleject2
        getactor[THISACTOR].ang angvar
	    ifbulletnear
	    {
		    setactor[THISACTOR].ang angvar
		    cactor PIGCOPDIVE
	        ai AIPIGDIVING
	    }
      }
    ifcount 43 nullop else ifcount 42 sound DBLOAD
    ifcount 55 nullop else ifcount 54 sound DBCLOSE
    
 
    ifcount 62
    {
      ifrnd 64
      {
        resetcount
        action APIGSHOOT
      }
      else
      {
        ifpdistl 768
          ai AIPIGFLEEENEMY
        else
          ai AIPIGSEEKENEMY
      }
    }

ends

state pigshootenemystate

ifvarand monstflags 256 { state pigdbshooting break }

ifcount 47 nullop else
ifvare target -1 ifvarn playerally 0 { ai AIPIGSEEKENEMY break }

//  ifvarg canseeplayer 0
//  {
	
    ifcount 12 nullop
    else
      ifcount 11
    {
	  ifvarn target -1 state pigshooting else
      // ifvarg canshootplayer 0
      ifvarg canseeplayer 0 // ifcanseetarget
	    state pigshooting
	    
      else
        ai AIPIGSEEKENEMY
    }
    ifcount 25 nullop
    else
      ifcount 24
      {
        action APIGCOCK
        sound SHOTGUN_COCK
        state shelleject2
        getactor[THISACTOR].ang angvar
	    ifbulletnear
	    {
		    setactor[THISACTOR].ang angvar
		    cactor PIGCOPDIVE
	        ai AIPIGDIVING
	    }
      }
    ifcount 48 nullop
    else
      ifcount 47
    {
	  ifvarn target -1 state pigshooting else
      //ifvarg canshootplayer 0
      ifvarg canseeplayer 0 // ifcanseetarget
        state pigshooting
      else
        ai AIPIGSEEKENEMY
    }
    ifcount 60 nullop
    else
      ifcount 59
      {
        action APIGCOCK
        sound SHOTGUN_COCK
        state shelleject2
        getactor[THISACTOR].ang angvar
	    ifbulletnear
	    {
		    setactor[THISACTOR].ang angvar
		    cactor PIGCOPDIVE
	        ai AIPIGDIVING
	    }
      }
    ifcount 72
    {
      ifrnd 64
        resetcount
      
      else
      {
        ifpdistl 768
          ai AIPIGFLEEENEMY
        else
          ai AIPIGSEEKENEMY
      }
    }
    ifaction APIGCOCK
      ifactioncount 2
        action APIGSHOOT
//  }
//  else
//    ai AIPIGSEEKENEMY
ends

state pigfleeenemystate
  ifactioncount 8
    ai AIPIGSEEKENEMY
  else
    ifnotmoving
      ai AIPIGSEEKENEMY
ends

state pigdivedb

ifaction APIGDIVESHOOT
  {
	ifvare target -1 ifvarn playerally 0 { cactor PIGCOP ai AIPIGSEEKENEMY break }
    //ifvarg canseeplayer 0
    //{
      ifcount 12 nullop
      else
        ifcount 11
      {
	    ifvarn target -1 { state pigshooting state pigshooting state pigshooting } else
        //ifvarg canshootplayer 0
        ifvarg canseeplayer 0 // ifcanseetarget
        {
          state pigshooting
          state pigshooting
          state pigshooting
        }
        else
        {
	      cactor PIGCOP
          ai AIPIGSEEKENEMY
        }
      }
      ifcount 19 nullop
      else
        ifcount 18 sound DBOPEN
      ifcount 35 action APIGDIVESHOOT
      else
        ifcount 34
        {
          state shelleject2
          state shelleject2
        }
      ifcount 43 nullop else ifcount 42 sound DBLOAD
      ifcount 55 nullop else ifcount 54 sound DBCLOSE
      
      
      ifcount 62
      {
	      ifgapzl 32
	         ai AIPIGDIVING
	       else
	       {
		      cactor PIGCOP
	          ifpdistl 4096
	            ai AIPIGFLEEENEMY
	          else
	            ai AIPIGSEEKENEMY
	       }
       }
    //}
    ifvarg canseeplayer 0 nullop else ifcount 10 nullop else ifgapzl 32 nullop else
    {
	    cactor PIGCOP
	    ai AIPIGSEEKENEMY
    }
    
  }
  else
    ifactioncount 2
    ifp palive
    {
      resetcount
      ifvare target -1 ifvarn playerally 0 { cactor PIGCOP ai AIPIGSEEKENEMY } else
      action APIGDIVESHOOT
    }

ends

state pigdivestate

ifvarand monstflags 256 { state pigdivedb break }

  ifaction APIGDIVESHOOT
  {
	ifvare target -1 ifvarn playerally 0 { cactor PIGCOP ai AIPIGSEEKENEMY break }
    ifvarg canseeplayer 0
    {
      ifcount 12 nullop
      else
        ifcount 11
      {
	    ifvarn target -1 state pigshooting else
        //ifvarg canshootplayer 0
        ifvarg canseeplayer 0 // ifcanseetarget
          state pigshooting
 
        else
        {
	      cactor PIGCOP
          ai AIPIGSEEKENEMY
        }
      }
      ifcount 25 nullop
      else
        ifcount 24
        {
          sound SHOTGUN_COCK
          state shelleject2
        }
      ifcount 48 nullop
      else
        ifcount 47
      {
	    ifvarn target -1 state pigshooting else
        //ifvarg canshootplayer 0
        ifvarg canseeplayer 0 // ifcanseetarget
          state pigshooting
        else
        {
	      cactor PIGCOP
          ai AIPIGSEEKENEMY
        }
      }
      ifcount 60 nullop
      else
        ifcount 59
      {
        sound SHOTGUN_COCK
        state shelleject2
        ifgapzl 32
          ai AIPIGDIVING
        else
        {
	      cactor PIGCOP
          ifpdistl 4096
            ai AIPIGFLEEENEMY
          else
            ai AIPIGSEEKENEMY
        }
      }
    }
    else
      ifgapzl 32
        ai AIPIGDIVING
      else
      {
	    cactor PIGCOP
        ai AIPIGSEEKENEMY
      }
  }
  else
    ifactioncount 2
    ifp palive
    {
      resetcount
      ifvare target -1 ifvarn playerally 0 { cactor PIGCOP ai AIPIGSEEKENEMY } else
      action APIGDIVESHOOT
    }
ends

state checkpighitstate
  ifvarn sprite[THISACTOR].htpicnum BULLETWOUND
  spawn BLOOD
  ifdead
  {
	cactor PIGCOP
    state random_wall_jibs
    
    ifwasweapon GROWSPARK
    {
      sound ACTOR_GROWING
      ai AIPIGGROW
      break
    }

    ifwasweapon FREEZEBLAST
    {
      sound SOMETHINGFROZE
      spritepal 1
      move 0
      action APIGFROZEN
      strength 0
      state icesmoke
      break
    }
    ifrnd 16
      spawn SHIELD
    else
      state drop_shotgun
    addkills 1 state specialdeath
    ifwasweapon RADIUSEXPLOSION
    {
      sound SQUISHED
      state standard_jibs
      killit
    }
    else
      ifwasweapon RPG
    {
      sound SQUISHED
      state standard_jibs
      killit
    }
    else
      ai AIPIGDYING
        sound PIG_DYING
  }
  else
  {
    sound PIG_PAIN
    state random_wall_jibs

    ifwasweapon SHRINKSPARK
    {
	  cactor PIGCOP
      sound ACTOR_SHRINKING
      ai AIPIGSHRINK
    }
    else
      ifwasweapon GROWSPARK
        sound EXPANDERHIT
    else
      ifrnd 32
      ifvarn sprite[THISACTOR].htpicnum BULLETWOUND
      {
	    cactor PIGCOP
        ai AIPIGHIT
      }
      else ifwasweapon KNEE ifvarn superkicking 0
       {
	     cactor PIGCOP
	     ai AIPIGHIT
       }
      else ifvarg stun 0
      {
	      cactor PIGCOP
	      ai AIPIGHIT
      }
    else
      ifrnd 64
      {
	    ifvarand monstflags 2048 ifvare targetlock 0 nullop else
	    {
		    cactor PIGCOP
	        ai AIPIGSHOOTENEMY
        }
      }
    else
      ifrnd 64
      {
	    ifvarand monstflags 2048 ifvare targetlock 0 nullop else
	    {
		    cactor PIGCOPDIVE
	        ai AIPIGDIVING
	        action APIGDIVESHOOT
        }
      }
  }
ends

state pigshrinkstate
  ifcount SHRUNKDONECOUNT
    ai AIPIGSEEKENEMY
  else
    ifcount SHRUNKCOUNT
      sizeto 48 40
  else
    state genericshrunkcode
ends

state pigdyingstate
  ifactioncount 5
  {
    ifrnd 64
      spawn BLOODPOOL
    state rf
    iffloordistl 8
      sound THUD
    //action APIGDEAD
    action ANULLACTION
    //move STOPPED
    move 0
    cactor PIGCOPCORPSE
    break
  }
ends


state pigcopcode
state monsterai
  fall
  state checksquished
  ifaction APIGSTAND
    ai AIPIGSEEKENEMY
  else
    ifaction APIGDEAD
  {
    ifrespawn
      ifcount RESPAWNACTORTIME
    {
	  addkills -1
      spawn TRANSPORTERSTAR
      cstat 257
      setvar monstatus 1
      strength PIGCOPSTRENGTH
      ai AIPIGSEEKENEMY
    }
    else
    {
      strength 0
      ifhitweapon
        ifwasweapon RADIUSEXPLOSION
      {
        sound SQUISHED
        state standard_jibs
        killit
      }
      break
    }
  }
  else
    ifaction APIGFROZEN
  {
    ifcount THAWTIME
    {
      ai AIPIGSEEKENEMY
      getlastpal
    }
    else
      ifcount FROZENDRIPTIME
        ifrnd 8
          spawn WATERDRIP
    ifhitweapon
    {
      ifwasweapon FREEZEBLAST
      {
        strength 0
        break
      }
      addkills 1 state specialdeath
      ifrnd 16
      spawn SHIELD
      else
      state drop_shotgun
      lotsofglass 30
      state standard_jibs
      ifrnd 84 spawn BLOODPOOL
      sound GLASS_BREAKING
      killit
    }
    ifp pfacing
      ifpdistl FROZENQUICKKICKDIST
      ifvare superkicking 0
        pkick
    break
  }
  else
    ifai AIPIGDYING
    {
      state pigdyingstate
      break
    }
  else
    ifai AIPIGHIT
    {
	  ifvare stun 0
      ifactioncount 3
      ai AIPIGSEEKENEMY
    }
  else
    ifai AIPIGSHRINK
    {
      state pigshrinkstate
      break
    }
  else
  {
    ifai AIPIGSEEKENEMY
      state pigseekenemystate
    else
      ifai AIPIGDODGE
        state pigseekenemystate
    else
      ifai AIPIGSHOOTENEMY
        state pigshootenemystate
    else
      ifai AIPIGGROW
        state genericgrowcode
    else
      ifai AIPIGFLEEENEMY
        state pigfleeenemystate
    else
      ifai AIPIGDIVING
        state pigdivestate
    else
      ifai AIPIGCHARGE
        state pigseekenemystate
    ifrnd 1
    {
      ifrnd 32
        soundonce PIG_ROAM
      else
        ifrnd 64
          soundonce PIG_ROAM2
      else
        soundonce PIG_ROAM3
    }
  }
  ifhitweapon
    state checkpighitstate
ends

actor PIGCOPDIVE PIGCOPSTRENGTH
ifai 0 ai AIPIGDIVING
state pigcopcode
enda
actor PIGCOPSTAYPUT PIGCOPSTRENGTH
  ai AIPIGSEEKENEMY
  cactor PIGCOP
enda
actor PIGCOP PIGCOPSTRENGTH APIGSTAND
state pigcopcode
enda


state boss1wait // only used by allied BOSS1

setactor[THISACTOR].mdflags 2
ifvarn target -1 { setactor[THISACTOR].mdflags 0 ai AIBOSS1SHOOTENEMY break }

ifpdistg 1560
ifactioncount 2 
{
	setactor[THISACTOR].mdflags 0
	ai AIBOSS1SEEKENEMY
}

ends

state boss1palshrunkstate
  ifcount SHRUNKDONECOUNT
  {
    ai AIBOSS1SEEKENEMY
    cstat 257
  }
  else
    ifcount SHRUNKCOUNT
      sizeto 40 40
  else
    state genericshrunkcode
ends

state checkboss1seekstate
  ai AIBOSS1SEEKENEMY
  ifspritepal 0 nullop
    else   // a fake way of doing a ifspritepal NOT.
      move PALBOSS1RUNVELS seekplayer
ends

state boss1runbotstate

  ifvarl xydist 2048
  {
      ai AIBOSS1SHOOTENEMY
    break
  }
  else
    ifactioncount 6
    {
        resetactioncount
        sound BOS1_WALK
    }
  
  ifcount 130 ifvarg xydist 6144
    ai AIBOSS1SEEKENEMY
ends

state boss1runenemystate

ifvarn target -1 { state boss1runbotstate break }

  ifpdistl 2048
  {
	ifvarn playerally 0 ai AIBOSS1WAIT else
    ifp palive
      ai AIBOSS1SHOOTENEMY
    break
  }
  else
    ifvarg canseeplayer 0
  {
    ifactioncount 6
    {
	  ifvarand monstflags 2048 ifvare targetlock 0 ai AIBOSS1SEEKENEMY else
      ifvarg canshootplayer 0
      {
        resetactioncount
        sound BOS1_WALK
      }
      else
        ai AIBOSS1SEEKENEMY
    }
  }
  else
    ai AIBOSS1SEEKENEMY
ends

state boss1seekbotstate

  ifrnd 2
    soundonce BOS1_ROAM
  else
    ifactioncount 6
  {
    resetactioncount
    sound BOS1_WALK
  }

  ifvarl xydist 2548
  {
    ai AIBOSS1SHOOTENEMY
    break
  }

    ifcount 32
  {
    ifrnd 32
    {
          ai AIBOSS1SHOOTENEMY
    }
    else
      ifvarg xydist 2548
        ifrnd 192
    {
      ifrnd 64
      {
        ai AIBOSS1RUNENEMY
        ifspritepal 0 nullop
        else
          move PALBOSS1RUNVELS seekplayer
      }
      else
        ai AIBOSS1LOBBED
    }
  }

ends

state boss1seekenemystate

ifvarn target -1 { state boss1seekbotstate break }

  ifrnd 2
    soundonce BOS1_ROAM
  else
    ifactioncount 6
  {
    resetactioncount
    sound BOS1_WALK
  }

  ifpdistl 2548
    ifp palive
  {
	ifvarn playerally 0 ai AIBOSS1WAIT else
    ai AIBOSS1SHOOTENEMY
    break
  }

  ifvarg canseeplayer 0
    ifcount 32
  {
    ifrnd 32
    {
	  ifvarn playerally 0 nullop else
      ifp palive
      {
	    ifvarand monstflags 2048 ifvare targetlock 0 nullop else
        ifvarg canshootplayer 0
          ai AIBOSS1SHOOTENEMY
      }
    }
    else
      ifpdistg 2548
        ifrnd 192
          
    {
	  setvar temp 0
	  ifvarand monstflags 2048 { ifvarg targetlock 0 setvar temp 1 }
	  else
	  ifvarg canshootplayer 0 setvar temp 1
	  ifvare temp 1
	  {
	      ifrnd 64
	      {
	        ai AIBOSS1RUNENEMY
	        ifspritepal 0 nullop
	        else
	          move PALBOSS1RUNVELS seekplayer
	      }
	      else ifvare playerally 0
	        ai AIBOSS1LOBBED
      }
    }
  }

ends

state boss1dyingstate
  ifaction ABOSS1DEAD
  {
    ifspritepal 0
      break
    ifrespawn
      ifcount RESPAWNACTORTIME
    {
	  addkills -1
      spawn TRANSPORTERSTAR
      cstat 257
      setvar monstatus 1
      strength PIGCOPSTRENGTH
      state checkboss1seekstate
    }
    else
    {
      strength 0
      ifhitweapon
        ifwasweapon RADIUSEXPLOSION
      {
        sound SQUISHED
        state standard_jibs
        killit
      }
      break
    }
  }
  ifactioncount 5
  {
    iffloordistl 8
      sound THUD
    //action ABOSS1DEAD
    cstat 0
    ifspritepal 0
    endofgame 52
    action ANULLACTION
    move 0
    ifvare smartmonsters YES
    {
	    espawn MINIGUNSPRITE
		setactorvar[RETURN].peractor4 13
		cactor BOSS1CORPSENOGUN
	}
	else
    cactor BOSS1CORPSE
  }
ends

state boss1lobbedstate
ifvare target -1 ifvarn playerally 0 { state checkboss1seekstate break }
  ifvarg canseeplayer 0
  {
    ifactioncount 2
    {
      resetactioncount
      sound BOS1_ATTACK2
      shoot MORTER
      ifvarand monstflags 8 
	  shoot MORTER

    }
    else
      ifcount 64
        ifrnd 16
          state checkboss1seekstate
  }
  else
    state checkboss1seekstate
ends

state boss1shootbot

state calczdist

ldist xydist THISACTOR target
ifvare xydist 0 setvar xydist 1

ifvarand monstflags 64
{
	//shiftvarl zdist 8
	mulvar zdist 844
	divvarvar zdist xydist
	sound SHOOTRAIL
	setprojectile[RAILSLUG_WEAK].workslike 36866 //4097
	ifvarand monstflags 8 setprojectile[RAILSLUG_WEAK].extra 30
	zshoot zdist RAILSLUG_WEAK
	ifvarand monstflags 8 setprojectile[RAILSLUG_WEAK].extra 15
	setprojectile[RAILSLUG_WEAK].workslike 32770 //1
} else
ifvarand monstflags 32
{
	mulvar zdist 844
	divvarvar zdist xydist
	sound RPG_SHOOT
	setprojectile[FIREBULLET].workslike 36870
	zshoot zdist FIREBULLET
	zshoot zdist FIREBULLET
	ifvarand monstflags 8
	{
		zshoot zdist FIREBULLET
		zshoot zdist FIREBULLET
	}
	setprojectile[FIREBULLET].workslike 32774
}
else
ifvarand monstflags 16
{
	mulvar zdist 768
	divvarvar zdist xydist
	sound PISTOL_LOW
	setprojectile[STARPROJ].workslike 38914
	zshoot zdist STARPROJ
	zshoot zdist STARPROJ
	ifvarand monstflags 8
	{
		zshoot zdist STARPROJ
		zshoot zdist STARPROJ
	}
	setprojectile[STARPROJ].workslike 34818
}
else
{
	shiftvarl zdist 8
	divvarvar zdist xydist
	sound BOS1_ATTACK1
	cactor PIGCOP // to prevent hardcoded auto-aiming at player
	zshoot zdist SHOTSPARK1
	zshoot zdist SHOTSPARK1
	zshoot zdist SHOTSPARK1
	zshoot zdist SHOTSPARK1
	ifspritepal 0
	{
	zshoot zdist SHOTSPARK1
	zshoot zdist SHOTSPARK1
	}
	
	ifvarand monstflags 8 
	{
	    zshoot zdist SHOTSPARK1
	    zshoot zdist SHOTSPARK1
	    zshoot zdist SHOTSPARK1
	    zshoot zdist SHOTSPARK1
	    ifspritepal 0
	    {
	    zshoot zdist SHOTSPARK1
	    zshoot zdist SHOTSPARK1
    	}
	}
	cactor BOSS1
}
state shelleject
state shelleject
resetactioncount

ends

state boss1shooting

ifvarn target -1 { state boss1shootbot break }
ifvarn playerally 0 { state checkboss1seekstate break }


ifvarand monstflags 64
{
	sound SHOOTRAIL
	ifvarand monstflags 8 setprojectile[RAILSLUG_WEAK].extra 30
	shoot RAILSLUG_WEAK
	ifvarand monstflags 8 setprojectile[RAILSLUG_WEAK].extra 15
} else
ifvarand monstflags 32
{
	sound RPG_SHOOT
	shoot FIREBULLET
	shoot FIREBULLET
	ifvarand monstflags 8
	{
		shoot FIREBULLET
		shoot FIREBULLET
	}
} else
ifvarand monstflags 16
{
	sound PISTOL_LOW
	shoot STARPROJ
	shoot STARPROJ
	ifvarand monstflags 8
	{
		shoot STARPROJ
		shoot STARPROJ
	}
}
else
{
	sound BOS1_ATTACK1
	shoot SHOTSPARK1
	shoot SHOTSPARK1
	shoot SHOTSPARK1
	shoot SHOTSPARK1
	ifspritepal 0
	{
	shoot SHOTSPARK1
	shoot SHOTSPARK1
	}
	
	ifvarand monstflags 8 
	{
	    shoot SHOTSPARK1
	    shoot SHOTSPARK1
	    shoot SHOTSPARK1
	    shoot SHOTSPARK1
	    ifspritepal 0
	    {
	    shoot SHOTSPARK1
	    shoot SHOTSPARK1
    	}
	}
}
state shelleject
state shelleject
resetactioncount

ends

state boss1shootenemy
  ifcount 72
    state checkboss1seekstate
  else
    ifaction ABOSS1SHOOT
      ifactioncount 2
  {
	state boss1shooting
  }
ends

state checkboss1hitstate
  ifrnd 2
    spawn BLOODPOOL
  ifdead
  {
    ifspritepal 0
      globalsound DUKE_TALKTOBOSSFALL
    else
    {
      ifrnd 64
      globalsound DUKE_TALKTOBOSSFALL
      else ifrnd 64
        globalsound BIGBLEED
      ifwasweapon FREEZEBLAST
      {
        sound SOMETHINGFROZE
        spritepal 1
        move 0
        action ABOSS1FROZEN
        strength 0
        state icesmoke
        break
      }
    }

    sound BOS1_DYING

    addkills 1 state specialdeath
    ai AIBOSS1DYING
  }
  else
  {
    ifrnd 32
    ifvarn sprite[THISACTOR].htpicnum BULLETWOUND
    {
      action BOSS1FLINTCH
      move STOPPED
    }
    else
    ifvarn sprite[THISACTOR].pal 0 ifvarg stun 0
    {
	   action BOSS1FLINTCH
       move STOPPED
    } 

    ifspritepal 0 nullop
    else
      ifwasweapon SHRINKSPARK
    {
      sound ACTOR_SHRINKING
      ai AIBOSS1PALSHRINK
      break
    }

    soundonce BOS1_PAIN

    ifvarn sprite[THISACTOR].htpicnum BULLETWOUND
    {
	    debris SCRAP1 1
	    guts JIBS6 1
    }
  }
ends

state boss1code
state monsterai
  ifaction ABOSS1FROZEN
  {
    ifcount THAWTIME
    {
      ai AIBOSS1SEEKENEMY
      spritepal 21
    }
    else
      ifcount FROZENDRIPTIME
    {
      ifactioncount 26
      {
        spawn WATERDRIP
        resetactioncount
      }
    }

    ifhitweapon
    {
      ifwasweapon FREEZEBLAST
      {
        strength 0
        break
      }
      addkills 1 state specialdeath
      lotsofglass 30
	  state standard_jibs
      ifrnd 84
        spawn BLOODPOOL
      sound GLASS_BREAKING
      killit
    }
    ifp pfacing
      ifpdistl FROZENQUICKKICKDIST
      ifvare superkicking 0
        pkick
    break
  }
  ifai 0
  {
    ifspritepal 0
      ai AIBOSS1RUNENEMY
    else
    {
	  cstator 257
      strength BOSS1PALSTRENGTH
      ai AIBOSS1SHOOTENEMY
      getactor[THISACTOR].extra hp
	  // this was already done in monsterai, but has to be done again for pal bosses	
	  ifvarand monstflags 2 mulvar hp 2
	  ifvarand monstflags 4 mulvar hp 4
	  ifvarg hp 32767 setvar hp 32767
	  setactor[THISACTOR].extra hp	
		
    }
  }
  else
    ifaction BOSS1FLINTCH
  {
	ifspritepal 0 setvar stun 0
	ifvare stun 0
    ifactioncount 3
    ai AIBOSS1SHOOTENEMY
  }
  else
    ifai AIBOSS1SEEKENEMY
      state boss1seekenemystate
  else
    ifai AIBOSS1RUNENEMY
      state boss1runenemystate
  else
    ifai AIBOSS1SHOOTENEMY
    state boss1shootenemy
  else
    ifai AIBOSS1LOBBED
      state boss1lobbedstate
  else
    ifai AIBOSS1PALSHRINK
      state boss1palshrunkstate
  else
    ifai AIBOSS1WAIT
      state boss1wait

  ifai AIBOSS1DYING
    state boss1dyingstate
  else
  {
    ifhitweapon
      state checkboss1hitstate
    else
      ifp palive
      {
		  ifspritepal 0        
		  {
			  ifpdistl 1280 ifvare playerally 0
			  {
			      addphealth -1000
			      palfrom 63 63
		      }
		      ifvarn target -1 ifvarl xydist 1280
		      {
			      setactor[target].htextra 100
			      setactor[target].htpicnum KNEE
			      setactor[target].htowner THISACTOR
		      }
		  }
		  else
		  {
			  ifvarn target -1 ifvarl xydist 768
			  {
				  setactor[target].htextra 2
			      setactor[target].htpicnum KNEE
			      setactor[target].htowner THISACTOR
			  }
		  }
	  }
  }
ends

actor BOSS1STAYPUT BOSS1STRENGTH cactor BOSS1 enda
actor BOSS1 BOSS1STRENGTH fall state boss1code enda

state boss2palshrunkstate
  ifcount SHRUNKDONECOUNT
  {
    cstat 257
    ai AIBOSS2SEEKENEMY
  }
  else
    ifcount SHRUNKCOUNT
      sizeto 40 40
  else
    state genericshrunkcode
ends

state checkboss2seekstate
  ai AIBOSS2SEEKENEMY
  // ifspritepal 0 nullop
  //else   // a fake way of doing a ifspritepal NOT.
  //  move PALBOSS2RUNVELS seekplayer
ends

state boss2runbotstate

    ifactioncount 3
    {
        resetactioncount
        sound BOS1_WALK
    }
    ifcount 48
      ifrnd 2
    {
        sound BOS2_ATTACK
        ai AIBOSS2SHOOTENEMY
      break
    }
  
ends

state boss2runenemystate

ifvarn target -1 { state boss2runbotstate break }

  ifvarg canseeplayer 0
  {
    ifactioncount 3
    {
	  ifvarand monstflags 2048
	  {
		ifvarg targetlock 0
		{
			resetactioncount
	        sound BOS1_WALK
        }
        else ai AIBOSS2SEEKENEMY
      } 
      else
      ifvarg canshootplayer 0
      {
        resetactioncount
        sound BOS1_WALK
      }
      else
        ai AIBOSS2SEEKENEMY
    }
    ifvarn playerally 0 nullop else
    ifcount 48
      ifrnd 2
    {
      ifp palive
      {
        sound BOS2_ATTACK
        ai AIBOSS2SHOOTENEMY
      }
      break
    }
  }
  else
    ai AIBOSS2SEEKENEMY
ends

state boss2seekbotstate

  ifrnd 2
    soundonce BOS2_ROAM
  else
    ifactioncount 3
  {
    resetactioncount
    sound BOS1_WALK
  }

 
    ifcount 32
        ifrnd 48
  {
    ifrnd 64
      ifvarg xydist 4096
    {
      ai AIBOSS2RUNENEMY
      // ifspritepal 0 nullop
      // else
      //  move PALBOSS2RUNVELS seekplayer
      break
    }

    ifvarl xydist 10240
    {
      ifrnd 128
      {
        sound BOS2_ATTACK
        ai AIBOSS2LOBBED
      }
    }
    else
    {
      sound BOS2_ATTACK
      ai AIBOSS2SHOOTENEMY
    }
  }
ends

state boss2seekenemystate

ifvarn target -1 { state boss2seekbotstate break }

  ifrnd 2
    soundonce BOS2_ROAM
  else
    ifactioncount 3
  {
    resetactioncount
    sound BOS1_WALK
  }

  setvar temp 0
  ifvarand monstflags 2048 { ifvarg targetlock 0 setvar temp 1 } else
  ifvarg canseeplayer 0 ifvarg canshootplayer 0 setvar temp 1
  ifvare temp 1
  ifcount 32
      ifp palive
        ifrnd 48
  {
    ifrnd 64
      ifpdistg 4096
    {
      ai AIBOSS2RUNENEMY
      // ifspritepal 0 nullop
      // else
      //  move PALBOSS2RUNVELS seekplayer
      break
    }
	ifvarn playerally 0 break
    ifpdistl 10240
    {
      ifrnd 128
      {
        sound BOS2_ATTACK
        ai AIBOSS2LOBBED
      }
    }
    else
    {
      sound BOS2_ATTACK
      ai AIBOSS2SHOOTENEMY
    }
  }
ends

state boss2dyingstate
  ifaction ABOSS2DEAD
  {
    ifspritepal 0
      break
    ifrespawn
      ifcount RESPAWNACTORTIME
    {
	  addkills -1
      spawn TRANSPORTERSTAR
      cstat 257
      setvar monstatus 1
      strength PIGCOPSTRENGTH
      state checkboss2seekstate
    }
    else
    {
      strength 0
      ifhitweapon
        ifwasweapon RADIUSEXPLOSION
      {
        sound SQUISHED
        state standard_jibs
        state standard_jibs
        killit
      }
      break
    }
  }
  ifactioncount 8
  {
    iffloordistl 8
      sound THUD
    //action ABOSS2DEAD
    action ANULLACTION
    cstat 0
    ifspritepal 0
      endofgame 52
    move 0
    cactor BOSS2CORPSE
  }
ends

state boss2lobbingbot
state calczdist
ifspritepal 0 addvar zdist 10240 // else addvar zdist 6144
ldist xydist THISACTOR target
ifvare xydist 0 setvar xydist 1
cactor LIZTROOP // avoids hardcoded auto-aim at player

	ifvarand monstflags 16
	{
      sound BLASTERSND
	  mulvar zdist 768
	  divvarvar zdist xydist
	  zshoot zdist FIRELASER
	  ifvarand monstflags 8 zshoot zdist FIRELASER
    } else
	ifrnd 128
    {
	  mulvar zdist 768
	  divvarvar zdist xydist
      zshoot zdist COOLEXPLOSION1
      ifvarand monstflags 8 zshoot zdist COOLEXPLOSION1
    }
cactor BOSS2
ends

state boss2lobbing

ifvarn target -1 { state boss2lobbingbot break }

	ifvarand monstflags 16
	{
	  sound BLASTERSND
	  shoot FIRELASER
	  ifvarand monstflags 8 shoot FIRELASER
    } else
	ifrnd 128
    {
      shoot COOLEXPLOSION1
      ifvarand monstflags 8 shoot COOLEXPLOSION1
    }

ends

state boss2shootbot
state calczdist
ifspritepal 0 addvar zdist 12288 else addvar zdist 8192
ldist xydist THISACTOR target
ifvare xydist 0 setvar xydist 1
cactor LIZTROOP // avoids hardcoded auto-aim at player

ifvarand monstflags 16
{
	sound BLASTERSND
	mulvar zdist 768
	divvarvar zdist xydist
	zshoot zdist FIRELASER
	zshoot zdist FIRELASER
	zshoot zdist FIRELASER
	ifvarand monstflags 8
	{
		zshoot zdist FIRELASER
		zshoot zdist FIRELASER
		zshoot zdist FIRELASER
	}
// 	sound BLASTERSND
// 	eshoot FIREBALLPROJ 
// 	ifvarand monstflags 8 
// 	{
// 		setthisprojectile[RETURN].pal 13
// 		setthisprojectile[RETURN].extra 64
// 	}
} 
else
{
	mulvar zdist 644
	divvarvar zdist xydist
	ezshoot zdist RPG
	setactor[RETURN].ang sprite[THISACTOR].ang
	ifspritepal 0 nullop else
	{
		getactor[RETURN].extra temp
		divvar temp 2
		setactor[RETURN].extra temp
	}
	ifvarand monstflags 8 shoot MORTER
}
	cactor BOSS2
    resetactioncount

ends

state boss2shooting
ifvarn target -1 { state boss2shootbot break }

ifvarand monstflags 16
{
	sound BLASTERSND
	shoot FIRELASER
	shoot FIRELASER
	shoot FIRELASER
	ifvarand monstflags 8
	{
		shoot FIRELASER
		shoot FIRELASER
		shoot FIRELASER
	}
// 	eshoot FIREBALLPROJ 
// 	ifvarand monstflags 8 
// 	{
// 		setthisprojectile[RETURN].pal 13
// 		setthisprojectile[RETURN].extra 64
// 	}
} 
else
{
	shoot RPG
	ifvarand monstflags 8 shoot MORTER
}
	
    resetactioncount

ends

state boss2lobbedstate

ifvare target -1 ifvarn playerally 0 { state checkboss2seekstate break }

  ifvarg canseeplayer 0
  {
    ifactioncount 2
      resetactioncount
    else
      ifactioncount 1
    {
      state boss2lobbing
    }
    else
      ifcount 64
        ifrnd 16
          state checkboss2seekstate
  }
  else
    state checkboss2seekstate
ends

state boss2shootenemy

ifvare target -1 ifvarn playerally 0 { state checkboss2seekstate break }

  ifcount 72
    state checkboss2seekstate
  else
    ifaction ABOSS2SHOOT
      ifactioncount 2
  {
	state boss2shooting
  }
ends

state checkboss2hitstate
  ifrnd 2
    spawn BLOODPOOL
  ifdead
  {
    ifspritepal 0
      globalsound DUKE_TALKTOBOSSFALL
    else
    {
      ifrnd 64
        globalsound DUKE_TALKTOBOSSFALL
      else ifrnd 64
        globalsound BIGBLEED
      ifwasweapon FREEZEBLAST
      {
        sound SOMETHINGFROZE
        spritepal 1
        move 0
        action ABOSS2FROZEN
        strength 0
        state icesmoke
        break
      }
    }

    sound BOS2_DYING

    addkills 1 state specialdeath

    ai AIBOSS2DYING
  }
  else
  {
    ifrnd 144
    {
      ifrnd 32
      ifvarn sprite[THISACTOR].htpicnum BULLETWOUND
      {
        action BOSS2FLINTCH
        move STOPPED
      }
      else ifvarn sprite[THISACTOR].pal 0 ifvarg stun 0
      {
	    action BOSS2FLINTCH
        move STOPPED
      }
      else
      {
        sound BOS2_ATTACK
        ai AIBOSS2SHOOTENEMY
      }
    }

    ifspritepal 0 nullop
    else
      ifwasweapon SHRINKSPARK
    {
      sound ACTOR_SHRINKING
      ai AIBOSS2PALSHRINK
      break
    }

    soundonce BOS2_PAIN
	ifvarn sprite[THISACTOR].htpicnum BULLETWOUND
	{
	    debris SCRAP1 1
	    guts JIBS6 1
    }
  }
ends

state boss2code
state monsterai
  ifaction ABOSS2FROZEN
  {
    ifcount THAWTIME
    {
      ai AIBOSS2SEEKENEMY
        spritepal 21
    }
    else
      ifcount FROZENDRIPTIME
    {
      ifactioncount 26
      {
        spawn WATERDRIP
        resetactioncount
      }
    }

    ifhitweapon
    {
      ifwasweapon FREEZEBLAST
      {
        strength 0
        break
      }
      addkills 1 state specialdeath
	  state standard_jibs
      lotsofglass 60
      sound GLASS_BREAKING
      ifrnd 84 spawn BLOODPOOL
      killit
    }
    ifp pfacing
      ifpdistl FROZENQUICKKICKDIST
      ifvare superkicking 0
        pkick
    break
  }
  ifai 0
  {
    ifspritepal 0
      ai AIBOSS2RUNENEMY
    else
    {
	  cstator 257
	  clipdist 32
	  sizeat 40 40
      strength BOSS1PALSTRENGTH
      sound BOS2_ATTACK ai AIBOSS2SHOOTENEMY
      getactor[THISACTOR].extra hp
	  // this was already done in monsterai, but has to be done again for pal bosses
	  ifvarand monstflags 2 mulvar hp 2
	  ifvarand monstflags 4 mulvar hp 4
	  ifvarg hp 32767 setvar hp 32767
	  setactor[THISACTOR].extra hp	
    }
  }
  else
    ifaction BOSS2FLINTCH
  {
    ifspritepal 0 setvar stun 0
	ifvare stun 0
    ifactioncount 3
      ai AIBOSS2SEEKENEMY
  }
  else
    ifai AIBOSS2SEEKENEMY
      state boss2seekenemystate
  else
    ifai AIBOSS2RUNENEMY
      state boss2runenemystate
  else
    ifai AIBOSS2SHOOTENEMY
      state boss2shootenemy
  else
    ifai AIBOSS2LOBBED
      state boss2lobbedstate
  else
    ifai AIBOSS2PALSHRINK
      state boss2palshrunkstate

  ifai AIBOSS2DYING
    state boss2dyingstate
  else
  {
    ifhitweapon state checkboss2hitstate
    else
    {
	  ifvarn target -1
	  {
		 ifspritepal 0
		 {
			 ifvarl xydist 1280
			 {
				 setactor[target].htextra 100
				 setactor[target].htpicnum KNEE
				 setactor[target].htowner THISACTOR
			 }
		 }
		 else ifvarl xydist 844
		 {
			 ifai AIBOSS2PALSHRINK nullop else
			 {
				 setactor[target].htextra 2
				 setactor[target].htpicnum KNEE
				 setactor[target].htowner THISACTOR
			 }
		 }
	  }
      ifp palive ifvarg canseeplayer 0
      {
	     ifspritepal 0 
	     {
		    ifpdistl 1280 ifvare playerally 0
		    {
		      addphealth -1000
		      palfrom 63 63
		    }
	    }
	    else ifpdistl 844
	    {
		    ifai AIBOSS2PALSHRINK nullop else
		    ifvare playerally 0
		    {
			    addphealth -1
			    ifvarand monstflags 8 addphealth -1
			    palfrom 30 63
		    }
	    }
      }
    }
  }


ends

actor BOSS2 BOSS2STRENGTH fall state boss2code enda


state boss3palshrunkstate
  ifcount SHRUNKDONECOUNT
    ai AIBOSS3SEEKENEMY
  else
    ifcount SHRUNKCOUNT
      sizeto 40 40
  else
    state genericshrunkcode
ends

state checkboss3seekstate
  ai AIBOSS3SEEKENEMY
  ifspritepal 0 nullop
  else   // a fake way of doing a ifspritepal NOT.
    move PALBOSS3RUNVELS seekplayer
ends

state boss3runenemystate

ifvarand monstflags 2048
{
	ifvarg targetlock 0
	{
		resetactioncount
        sound BOS1_WALK
    }
    else
        ai AIBOSS3SEEKENEMY
    break
}

  ifvarg canseeplayer 0
  {
    ifactioncount 3
    {
      ifvarg canshootplayer 0
      {
        resetactioncount
        sound BOS1_WALK
      }
      else
        ai AIBOSS3SEEKENEMY
    }
  }
  else
    ai AIBOSS3SEEKENEMY
ends

state boss3seekbotstate

  ifrnd 2
    soundonce BOS3_ROAM
  else
    ifactioncount 3
    {
      resetactioncount
      sound BOS1_WALK
    }

 
    ifcount 32
      ifrnd 48
  {
    ifrnd 64
      ifvarg xydist 4096
      {
        ai AIBOSS3RUNENEMY
        ifspritepal 0
          break
        move PALBOSS3RUNVELS seekplayer
        break
      }

      ai AIBOSS3LOBENEMY
  }
ends

state boss3seekenemystate

ifvarn target -1 { state boss3seekbotstate break }

  ifrnd 2
    soundonce BOS3_ROAM
  else
    ifactioncount 3
    {
      resetactioncount
      sound BOS1_WALK
    }

ifcount 32
  ifrnd 48
{
  setvar temp 0
  ifvarand monstflags 2048
  {
	  ifvarg targetlock 0 setvar temp 1
  }
  else
  ifvarg canseeplayer 0
        ifvarg canshootplayer 0
  setvar temp 1
  ifvare temp 1
  {
    ifrnd 64
      ifpdistg 4096
      {
        ai AIBOSS3RUNENEMY
        ifspritepal 0
          break
        move PALBOSS3RUNVELS seekplayer
        break
      }

    ifp palive
    {
	  ifvare playerally 0
	  {
        ai AIBOSS3LOBENEMY
        break
        
      }
      ai AIBOSS3RUNENEMY
        ifspritepal 0
          break
        move PALBOSS3RUNVELS seekplayer
      
    }
  }
}
ends

state boss3dyingstate
  ifaction ABOSS3DEAD
  {
    ifspritepal 0
      break
    ifrespawn
      ifcount RESPAWNACTORTIME
    {
	  addkills -1
      spawn TRANSPORTERSTAR
      cstat 257
      setvar monstatus 1
      strength PIGCOPSTRENGTH
      state checkboss3seekstate
    }
    else
    {
      strength 0
      ifhitweapon
        ifwasweapon RADIUSEXPLOSION
      {
        sound SQUISHED
        state standard_jibs
        state standard_jibs
        killit
      }
      break
    }
  }
  ifactioncount 8
  {
    iffloordistl 8
      sound THUD
    //action ABOSS3DEAD
    action ANULLACTION
    cstat 0
    ifspritepal 0
    endofgame 52
    move 0
    cactor BOSS3CORPSE
  }
ends

state boss3lobbingbot

state calczdist
ifspritepal 0 addvar zdist 12288 else addvar zdist 8192
ldist xydist THISACTOR target
ifvare xydist 0 setvar xydist 1

ifvarand monstflags 32
{
	mulvar zdist 768
	divvarvar zdist xydist
	setprojectile[ELECTROPROJ].workslike 36866
	zshoot zdist ELECTROPROJ
	ifvarand monstflags 8 zshoot zdist ELECTROPROJ
	setprojectile[ELECTROPROJ].workslike 32770
} else
ifvarand monstflags 16
{
	eshoot FIREBALLPROJ 
	ifvarand monstflags 8 
	{
		setthisprojectile[RETURN].pal 13
		setthisprojectile[RETURN].extra 64
	}
} 
else
{
	mulvar zdist 644
	divvarvar zdist xydist
	ezshoot zdist RPG
	setactor[RETURN].ang sprite[THISACTOR].ang
	ifspritepal 0 nullop else ifvarand monstflags 8 nullop else
	{
		getactor[RETURN].extra temp
		divvar temp 2
		setactor[RETURN].extra temp
	}
	
}

ends

state boss3lobbing

ifvarn target -1 { state boss3lobbingbot break }

ifvarand monstflags 32
{
	shoot ELECTROPROJ
	ifvarand monstflags 8 shoot ELECTROPROJ
} else
ifvarand monstflags 16
{
	eshoot FIREBALLPROJ 
	ifvarand monstflags 8 
	{
		setthisprojectile[RETURN].pal 13
		setthisprojectile[RETURN].extra 64
	}
} 
else
{
	shoot RPG
	ifvarand monstflags 8 shoot RPG
}

ends


state boss3lobbedstate
  setvar temp 0
  ifvarand monstflags 2048 { ifvare playerally 0 ifvarg targetlock 0 setvar temp 1 } else
  ifvarg canseeplayer 0 ifvare playerally 0 setvar temp 1
  ifvarn target -1 setvar temp 1
  ifvare temp 1
  {
    ifaction ABOSS3LOBBING
      ifactioncount 2
    {
      state boss3lobbing
      resetactioncount
      ifrnd 8
        ai AIBOSS3SEEKENEMY
    }

    ifactioncount 3
    {
      action ABOSS3LOBBING
      resetcount
    }
  }
  else
    state checkboss3seekstate
ends

state checkboss3hitstate
  ifrnd 2
    spawn BLOODPOOL
  ifdead
  {
    ifspritepal 0
      globalsound DUKE_TALKTOBOSSFALL
    else
    {
      ifrnd 64
        globalsound DUKE_TALKTOBOSSFALL
      else
      ifrnd 64
        globalsound BIGBLEED
      ifwasweapon FREEZEBLAST
      {
        sound SOMETHINGFROZE
        spritepal 1
        move 0
        action ABOSS3FROZEN
        strength 0
        state icesmoke
        break
      }
    }

    addkills 1 state specialdeath
    ai AIBOSS3DYING

    sound BOS3_DYING
    sound JIBBED_ACTOR9

  }
  else
  {
    ifrnd 32
    ifvarn sprite[THISACTOR].htpicnum BULLETWOUND
    {
      action BOSS3FLINTCH
      move STOPPED
    }
    else ifvarn sprite[THISACTOR].pal 0 ifvarg stun 0
    {
      action BOSS3FLINTCH
      move STOPPED
    }

    ifspritepal 0 nullop
    else
      ifwasweapon SHRINKSPARK
    {
      sound ACTOR_SHRINKING
      ai AIBOSS3PALSHRINK
      break
    }

    soundonce BOS3_PAIN
	ifvarn sprite[THISACTOR].htpicnum BULLETWOUND
	{
	    debris SCRAP1 1
	    guts JIBS6 1
    }
  }
ends

state boss3code
state monsterai
  ifaction ABOSS3FROZEN
  {
	ifcount THAWTIME
    {
      ai AIBOSS3SEEKENEMY
        getlastpal
    }
    else
      ifcount FROZENDRIPTIME
    {
      ifactioncount 26
      {
        spawn WATERDRIP
        resetactioncount
      }
    }
    ifhitweapon
    {
      ifwasweapon FREEZEBLAST
      {
        strength 0
        break
      }

      addkills 1 state specialdeath
      lotsofglass 30
	  state standard_jibs
      ifrnd 84
        spawn BLOODPOOL
      sound GLASS_BREAKING
      killit
    }
    ifp pfacing
      ifpdistl FROZENQUICKKICKDIST
      ifvare superkicking 0
        pkick
    break
  }
  ifai 0
  {
    ifspritepal 0
      ai AIBOSS3RUNENEMY
    else
    {
	  cstator 257
	  sizeat 40 40
	  clipdist 32 //16
      strength BOSS1PALSTRENGTH
      ai AIBOSS3LOBENEMY
      getactor[THISACTOR].extra hp
	  // this was already done in monsterai, but has to be done again for pal bosses	
	  ifvarand monstflags 2 mulvar hp 2
	  ifvarand monstflags 4 mulvar hp 4
	  ifvarg hp 32767 setvar hp 32767
	  setactor[THISACTOR].extra hp	
    }
  }
  else
    ifaction BOSS3FLINTCH
    {
	  ifspritepal 0 setvar stun 0
	  ifvare stun 0
      ifactioncount 3
      ai AIBOSS3SEEKENEMY
    }
  else
    ifai AIBOSS3SEEKENEMY
      state boss3seekenemystate
  else
    ifai AIBOSS3RUNENEMY
      state boss3runenemystate
  else
    ifai AIBOSS3LOBENEMY
      state boss3lobbedstate
  else
    ifai AIBOSS3PALSHRINK
      state boss3palshrunkstate

  ifai AIBOSS3DYING
    state boss3dyingstate
  else
  {
    ifhitweapon
      state checkboss3hitstate
    else
    {
	  ifvarn target -1
	  {
		 ifspritepal 0
		 {
			 ifvarl xydist 1280
			 {
				 setactor[target].htextra 100
				 setactor[target].htpicnum KNEE
				 setactor[target].htowner THISACTOR
			 }
		 }
		 else ifvarl xydist 844
		 {
			 ifai AIBOSS2PALSHRINK nullop else
			 {
				 setactor[target].htextra 2
				 setactor[target].htpicnum KNEE
				 setactor[target].htowner THISACTOR
			 }
		 }
	  }
      ifp palive ifvarg canseeplayer 0 ifvare playerally 0
      {
	     ifspritepal 0 
	     {
		    ifpdistl 1280
		    {
		      addphealth -1000
		      palfrom 63 63
		    }
	    }
	    else ifpdistl 844
	    {
		    ifai AIBOSS2PALSHRINK nullop else
		    {
			    addphealth -1
			    ifvarand monstflags 8 addphealth -1
			    palfrom 30 63
		    }
	    }
      }
    }
  }
ends

actor BOSS3 BOSS3STRENGTH fall state boss3code enda


state checkcommhitstate

  ifhitweapon
  {
	ifvarn sprite[THISACTOR].htpicnum BULLETWOUND
    guts JIBS6 2

    ifdead
    {
      ifwasweapon FREEZEBLAST
      {
        sound SOMETHINGFROZE
        spritepal 1
        move 0
        action ACOMMFROZEN
        strength 0
        state icesmoke
        break
      }
      else
        ifwasweapon GROWSPARK
      {
        sound ACTOR_GROWING
        ai AICOMMGROW
        break
      }

      addkills 1 state specialdeath

      ifwasweapon RADIUSEXPLOSION
      {
        spawn BLOODPOOL
        sound SQUISHED
        state standard_jibs
        killit
      }
      else
        ifwasweapon RPG
      {
        sound SQUISHED
        spawn BLOODPOOL
        state standard_jibs
        killit
      }

      sound COMM_DYING
      ai AICOMMDYING
    }
    else
    {
      soundonce COMM_PAIN
      ifwasweapon SHRINKSPARK
      {
        sound ACTOR_SHRINKING
        ai AICOMMSHRUNK
      }
      else
        ifwasweapon GROWSPARK
          sound EXPANDERHIT
      else
        ifrnd 24
          ai AICOMMABOUTTOSHOOT
    }
  }
ends

state commshootbot

state calczdist
ldist xydist THISACTOR target
ifvare xydist 0 setvar xydist 1

ifvarand monstflags 32
{
	mulvar zdist 768
	divvarvar zdist xydist
	setprojectile[ELECTROPROJ].workslike 36866
	ezshoot zdist ELECTROPROJ
	setthisprojectile[RETURN].extra 50
	ifvarand monstflags 8
	{
		ezshoot zdist ELECTROPROJ
		setthisprojectile[RETURN].extra 50
	}
	setprojectile[ELECTROPROJ].workslike 32770
}
ifvarand monstflags 16
{
	eshoot FIREBALLPROJ 
	ifvarand monstflags 8 
	{
		setthisprojectile[RETURN].pal 13
		setthisprojectile[RETURN].extra 64
	}
} 
else
{
	mulvar zdist 644
	divvarvar zdist xydist
	ezshoot zdist RPG // NOAIM
	ifvarand monstflags 8 setactor[RETURN].extra 100
	setactor[RETURN].ang sprite[THISACTOR].ang

}


resetactioncount

ends

state commshooting

ifvarn target -1 { state commshootbot break }

ifvarand monstflags 32
{
	eshoot ELECTROPROJ
	setthisprojectile[RETURN].extra 50
	ifvarand monstflags 8
	{
		eshoot ELECTROPROJ
		setthisprojectile[RETURN].extra 50
	}
} else
ifvarand monstflags 16
{
	eshoot FIREBALLPROJ 
	ifvarand monstflags 8 
	{
		setthisprojectile[RETURN].pal 13
		setthisprojectile[RETURN].extra 64
	}
} 
else
{
	shoot RPG
	ifvarand monstflags 8 shoot RPG
}


resetactioncount

ends

actor COMMANDERSTAYPUT COMMANDERSTRENGTH
  cactor COMMANDER
  ai AICOMMABOUTTOSHOOT
enda

state commspinbotstate

getactor[target].x mx
getactor[target].y my
getactor[target].z mz
getactor[THISACTOR].x x
getactor[THISACTOR].y y
getactor[THISACTOR].z z
subvarvar mx x
subvarvar my y
getangle angvar mx my

cos xvel angvar
sin yvel angvar
shiftvarr xvel 7
shiftvarr yvel 7
subvarvar z mz 
ifvarg z 2048
setvar zvel -512 else
ifvarl z -2048 setvar zvel 512
movesprite THISACTOR xvel yvel zvel CLIPMASK0 RETURN

    soundonce COMM_SPIN
    ifcount 16
    {
      ifvarl xydist 1280
      {
	    setvar temp CAPTSPINNINGPLAYER
        ifvarand monstflags 8 mulvar temp -2 else mulvar temp -1
        setactor[target].htextra temp
        setactor[target].htpicnum RPG
        setactor[target].htowner THISACTOR
        //sound DUKE_GRUNT
        resetcount
      }
      else
        ifvarg xydist 2300
          ai AICOMMWAIT
    }
    ifactioncount 52
      ai AICOMMWAIT
    ifnotmoving
      ifrnd 32
        operate

ends

state commspinstate

getplayer[THISACTOR].posx mx
getplayer[THISACTOR].posy my
getactor[THISACTOR].x x
getactor[THISACTOR].y y
subvarvar mx x
subvarvar my y
getangle angvar mx my

cos xvel angvar
sin yvel angvar
shiftvarr xvel 7
shiftvarr yvel 7
setvar zvel 0
movesprite THISACTOR xvel yvel zvel CLIPMASK0 RETURN 

    soundonce COMM_SPIN
    ifcount 16
    {
      ifpdistl 1280 ifvarg canshootplayer 0
      {
        addphealth CAPTSPINNINGPLAYER
        ifvarand monstflags 8 addphealth CAPTSPINNINGPLAYER
        sound DUKE_GRUNT
        palfrom 32 16 
        resetcount
      }
      else
        ifpdistg 2300
          ai AICOMMWAIT
    }
    ifactioncount 52
      ai AICOMMWAIT
    ifnotmoving
      ifrnd 32
        operate
        
ends

state commfrisbeestate

setvar weapcount -1
ifcount 6
{
	ifcount 7 nullop else
	{
		getplayer[THISACTOR].posx mx
		getplayer[THISACTOR].posy my
		getactor[THISACTOR].x x
		getactor[THISACTOR].y y
		subvarvar mx x
		subvarvar my y
		getangle peractor5 mx my
	}
	
	cos xvel peractor5
	sin yvel peractor5
	shiftvarr xvel 5
	shiftvarr yvel 5
	setvar zvel 0
	movesprite THISACTOR xvel yvel zvel CLIPMASK0 RETURN
	ifvarn RETURN 0
	{
		addvar RETURN 16384
        ifvarl RETURN 16384
        ifvarg RETURN -1 // hit a sprite
        {
	        getactor[RETURN].picnum picnum
	        getactorvar[RETURN].monstatus temp
	        ifvare picnum APLAYER sound SQUISHED else
	        ifvare temp 1 sound SQUISHED
	        setactor[RETURN].htpicnum RPG
	        setactor[RETURN].htextra 100
	        setactor[RETURN].htowner THISACTOR
	        setactor[RETURN].htang peractor5
	        sound COMM_ATTACK
	        
        }
        ai AICOMMWAIT
        setvar weapcount 0
    }
}

    soundonce COMM_SPIN
    
      ifpdistl 1280
      ifvarg canseeplayer 0
      {
        addphealth -1
        ifvarand monstflags 8 addphealth -1
        sound DUKE_GRUNT
        palfrom 32 16 
      }
      
    
    ifactioncount 52
    {
      ai AICOMMWAIT
      setvar weapcount 0
    }
ends

actor COMMANDER COMMANDERSTRENGTH

ifvarn target -1
{
	getactor[THISACTOR].z z
	getactor[target].z mz
}

  state checksquished
  state monsterai
  ifaction ACOMMFROZEN
  {
    fall

    ifcount THAWTIME
    {
      getlastpal
      ai AICOMMWAIT
    }
    else
      ifcount FROZENDRIPTIME
    {
      ifactioncount 26
      {
        spawn WATERDRIP
        resetactioncount
      }
    }

    ifhitweapon
    {
      ifwasweapon FREEZEBLAST
      {
        strength 0
        break
      }
      addkills 1 state specialdeath

      ifrnd 84
        spawn BLOODPOOL
      lotsofglass 30
      state standard_jibs
      sound GLASS_BREAKING
      killit
    }
    ifp pfacing
      ifpdistl FROZENQUICKKICKDIST
      ifvare superkicking 0
        pkick
    break
  }
  ifai 0
    ai AICOMMSHOOT
  else
    ifai AICOMMWAIT
  {
    ifcount 20
    {
	  ifvarn target -1
	  {
		  ifrnd 96
	            ai AICOMMGET
	          else
	            ai AICOMMABOUTTOSHOOT
	  }
	  else
	  {
		  ifvarand monstflags 2048
		  {
			  ifvarg targetlock 0
			  {
				ifrnd 96
	              ai AICOMMGET
	            else ifvare playerally 0
	              ai AICOMMABOUTTOSHOOT
	          }
	          else
	             ai AICOMMGET
	      }
	      else
	      
	      ifvarg canseeplayer 0
	      {
	        ifvarg canshootplayer 0
	        {
	          ifrnd 96
	            ai AICOMMGET
	          else ifvare playerally 0
	            ai AICOMMABOUTTOSHOOT
	        }
	      }
	      else
	        ai AICOMMGET
      }
    }
  }
  else
    ifai AICOMMABOUTTOSHOOT
  {
	ifvare target -1 ifvarn playerally 0 { ai AICOMMGET break }
    ifactioncount 2
    {
	  ifvarn target -1 ai AICOMMSHOOT else
      ifvarg canseeplayer 0 ifvare playerally 0
        ai AICOMMSHOOT
      else
      {
        ai AICOMMGET
        break
      }
    }
    ifrnd 32 soundonce COMM_ATTACK
  }
  else
    ifai AICOMMSHOOT
  {
	ifvare target -1 ifvarn playerally 0 { ai AICOMMGET break }
	setvar temp 0
	ifvarn target -1 setvar temp 1
	ifvarand monstflags 2048
	ifvare playerally 0
	{
		ifangdiffl 512 
		{ 
			ai AICOMMFRISBEE 
			break 
		} 
		else ifvarg targetlock 0 setvar temp 1 
	} else
	ifvarg canshootplayer 0 setvar temp 1
    ifvare temp 1
    {
      ifcount 24
        ifrnd 16
          ai AICOMMWAIT
      ifactioncount 2
      {
	    state commshooting
      }
    }
    else
      ai AICOMMGET
  }
  else
    ifai AICOMMSHRUNK
  {
    ifcount SHRUNKDONECOUNT
      ai AICOMMGET
    else
      ifcount SHRUNKCOUNT
        sizeto 48 40
    else
      state genericshrunkcode
  }
  else
    ifai AICOMMGROW
      state genericgrowcode
  else
    ifai AICOMMGET
  {
	ifvarn playerally 0 ifvare target -1 ifpdistl 1280 { ai AICOMMWAIT break }
    ifnotmoving
      ifrnd 4
        operate
    ifvare playerally 0
    ifpdistl 1280
      ifp palive
    {
      sound COMM_SPIN
      ai AICOMMSPIN
      break
    }
    ifvarn target -1
      ifvarl xydist 1280
    {
	  sound COMM_SPIN
	  ai AICOMMSPIN
	  break
    }

    ifvarg canseeplayer 0
    {
	  setvar temp 0
	  ifp phigher setvar temp 1
	  ifvarn target -1
	  ifvarvarg z mz setvar temp 1
	  
      ifvare temp 1
        move COMMGETUPVELS getv geth faceplayer
      else
        move COMMGETVELS getv geth faceplayer
    }
    ifactioncount 8
      ifrnd 2
        ai AICOMMABOUTTOSHOOT
  }
  else
    ifai AICOMMSPIN
  {
	ifvarn target -1 state commspinbotstate else
	state commspinstate
  }
  else ifai AICOMMFRISBEE state commfrisbeestate

  ifai AICOMMDYING
  {
    fall
    strength 0

    ifhitweapon
      ifwasweapon RADIUSEXPLOSION
    {
      sound SQUISHED
      spawn BLOODPOOL
      state standard_jibs
      killit
    }

    ifaction ACOMMDYING
      ifactioncount 8
    {
      iffloordistl 8
        sound THUD
      cstat 0
      action ANULLACTION
      //action ACOMMDEAD
      move 0
      cactor COMMANDERCORPSE
    }
  }
  else
  {
    ifrnd 2
      soundonce COMM_ROAM
    state checkcommhitstate
  }
enda

// RIOT TANK with Pigcop inside

define TANKSTRENGTH       500      // 2 3 4 5 7 8
//           off-set, numofframes, numofrot, skipframes, delay

state tankshootbot

state calczdist
dist xydist THISACTOR target
ifvare xydist 0 setvar xydist 1
ldist xydist2 THISACTOR target
ifvare xydist2 0 setvar xydist2 1

ifactioncount 22
{
  ifvarg xydist 8192
  {
    sound BOS1_ATTACK2
    mulvar zdist 644
	divvarvar zdist xydist
	ezshoot zdist RPG // NOAIM
	ifvarand monstflags 8 setactor[RETURN].extra 100
	setactor[RETURN].ang sprite[THISACTOR].ang
  }
  resetcount
  move 0 action ATANKWAIT
}
else
  ifactioncount 2
{
    ifvarl xydist 16384
    {
      ifrnd 128
      {
        ifvarand monstflags 16
        {
	        ifvarand morefx 16
			ifvarn rendmode 0 espawn SHOTSPARK5
	        mulvar zdist 768
	        divvarvar zdist xydist2
	        sound PISTOL_LOW
	        setprojectile[STARPROJ].workslike 38914
	        zshoot zdist STARPROJ
	        ifvarand monstflags 8 zshoot zdist STARPROJ
	        setprojectile[STARPROJ].workslike 34818
        } else
        {
	        ifvarand morefx 16
			ifvarn rendmode 0 espawn SHOTSPARK5
	        shiftvarl zdist 8
	        divvarvar zdist xydist2
            sound PISTOL_FIRE
            zshoot zdist SHOTSPARK1
            ifvarand monstflags 8 zshoot zdist SHOTSPARK1
        }
      }
    }
    else
      ifrnd 128
    {
        ifvarand monstflags 16
        {
	        mulvar zdist 844
	        divvarvar zdist xydist2
	        setprojectile[FIREBULLET].workslike 36870
	        sound RPG_SHOOT
	        zshoot zdist FIREBULLET
	        ifvarand monstflags 8 zshoot zdist FIREBULLET
	        setprojectile[FIREBULLET].workslike 32774
        }
        else
        {
	      mulvar zdist 768
	      divvarvar zdist xydist2
	      ifvare dpsounds YES sound PRED_ATTACK2 else
          sound PRED_ATTACK
          zshoot zdist FIRELASER
          ifvarand monstflags 8 zshoot zdist FIRELASER
        }
    }
  
  else
  {
    move TANKFORWARD seekplayer
    action ATANKSPIN
  }
}

ifrnd 16
{
  stopsound TANK_ROAM
  move STOPPED faceplayerslow
}
	    
ends


eventloadactor TANK
sizeat 60 60
enda

useractor enemy TANK TANKSTRENGTH fall
  state monsterai
  ifaction 0
  {
    sizeat 60 60
    action ATANKWAIT
    cstat 257
    clipdist 100
  }
  else
    ifaction ATANKSPIN
  {
    soundonce TANK_ROAM

    ifactioncount 20
    {
	  ifvare target -1 ifvarn playerally 0 nullop else
      ifrnd 16
        ifvarg canseeplayer 0
          ifvarg canshootplayer 0
      {
        move STOPPED geth
        action ATANKSHOOTING
        stopsound TANK_ROAM
      }
    }

    ifrnd 16
      move TANKFORWARD seekplayer
  }
  else
    ifaction ATANKSHOOTING
  {
	ifvarn target -1 state tankshootbot else
	{
		ifvarn playerally 0 { move 0 action ATANKWAIT break }
	    ifactioncount 22
	    {
	      ifpdistg 8192
	      {
	        sound BOS1_ATTACK2
	        shoot MORTER
	        ifvarand monstflags 8 shoot MORTER
	      }
	      resetcount
	      move 0 action ATANKWAIT
	    }
	    else
	      ifactioncount 2
	    {
	      ifvarg canseeplayer 0
	      {
	        ifpdistl 16384
	        {
	          ifrnd 128
	          {
		        ifvarand monstflags 16
		        {
			        ifvarand morefx 16
			        ifvarn rendmode 0 espawn SHOTSPARK5
			        sound PISTOL_LOW
			        shoot STARPROJ
			        ifvarand monstflags 8 shoot STARPROJ
		        } else
		        {
			        ifvarand morefx 16
			        ifvarn rendmode 0 espawn SHOTSPARK5
			        addvar position 1 ifvarg position 3 setvar position 0
			        setactorvar[RETURN].position position
			        ifvare dpsounds YES sound DPCHAINGUNFIRE else
		            sound PISTOL_FIRE
		            shoot SHOTSPARK1
		            ifvarand monstflags 8 shoot SHOTSPARK1
	            }
	          }
	        }
	        else
	          ifrnd 128
	        {
		        ifvarand monstflags 16
		        {
			        sound RPG_SHOOT
			        shoot FIREBULLET
			        ifvarand monstflags 8 shoot FIREBULLET
		        }
		        else
		        {
			      ifvare dpsounds YES sound PRED_ATTACK2 else
		          sound PRED_ATTACK
		          shoot FIRELASER
		          ifvarand monstflags 8 shoot FIRELASER
	            }
	        }
	      }
	      else
	      {
	        move TANKFORWARD seekplayer
	        action ATANKSPIN
	      }
	    }
	
	    ifrnd 16
	    {
	      stopsound TANK_ROAM
	      move STOPPED faceplayerslow
	    }
    }
  }
  else
    ifaction ATANKWAIT
  {
    ifactioncount 32
    {
      move TANKFORWARD seekplayer
      action ATANKSPIN
    }
  }
  else
    ifaction ATANKDESTRUCT
  {
    ifactioncount 64
      action ATANKDEAD
    else
      ifactioncount 56
       sound LASERTRIP_ARMING
     else
       ifactioncount 48
         sound LASERTRIP_ARMING
     else
       ifactioncount 32
         sound LASERTRIP_ARMING
     else
       ifactioncount 16
         sound LASERTRIP_ARMING
     break
  }
  else
    ifaction ATANKDEAD
  {
    addkills 1 state specialdeath
    hitradius 6144 TOUGH TOUGH TOUGH TOUGH
    sound LASERTRIP_EXPLODE
    debris SCRAP1 15
    spawn EXPLOSION2
    ifrnd 128 spawn PIGCOP
    killit
  }

  ifhitweapon
  {
    ifdead
      action ATANKDEAD
    else
    {
      debris SCRAP1 1
      ifaction ATANKSHOOTING break
	  ifvare target -1 ifvarn playerally 0 nullop else
      ifrnd 192
      {
        move STOPPED geth
        action ATANKSHOOTING
        stopsound TANK_ROAM
      }
    }
  }

  ifpdistl 1280
    ifhitspace
      ifp pfacing
        ifangdiffl 512
          action ATANKDESTRUCT
enda



state boss4shoottarget
ifvarand monstflags 16 
{ 
    shiftvarl zdist 8
	divvarvar zdist xydist
	zshoot zdist GROWSPARK
    zshoot zdist GROWSPARK 
} 
else
{
    mulvar zdist 768
    divvarvar zdist xydist
    setprojectile[ELECTROPROJ].workslike 36866
    zshoot zdist ELECTROPROJ
    setprojectile[ELECTROPROJ].workslike 32770
}
ends

state boss4shootbot

state calczdist
ldist xydist THISACTOR target
ifvare xydist 0 setvar xydist 1

  ifaction ABOSS4ABOUTTOSHOOT
    ifactioncount 3
      action ABOSS4SHOOT

  ifaction ABOSS4SHOOT
  {
    ifcount 48 ifvare playerally 0
    {
      ifrnd 4
        ai AIBOSS4LAYEGGS
    }
    ifcount 26
    {
	    ifvare pal 0
	    {
		    ifrnd 32
		    {
			  setactor[target].htpicnum SHOTSPARK1
		      setactor[target].htowner THISACTOR
		      sound SHORT_CIRCUIT
		      ifrnd 128
		      {
		        ifvarand monstflags 8 setactor[target].htextra 4 else
		        setactor[target].htextra 2
		      }
		      else
		      {
		        ifvarand monstflags 8 setactor[target].htextra 2 else
		        setactor[target].htextra 1
		      }
		    }
	    } else
	    {
		    ifcount 27 nullop else
			    state boss4shoottarget
			
		    ifcount 34 nullop else
		    ifcount 33 
			    state boss4shoottarget
			
		    ifcount 41 nullop else
		    ifcount 40 
			    state boss4shoottarget
			
		    ifvarand monstflags 8
		    {
			    ifcount 31 nullop else ifcount 30 state boss4shoottarget
			    ifcount 38 nullop else ifcount 37 state boss4shoottarget
			    ifcount 45 nullop else ifcount 44 state boss4shoottarget
		    }
	    }
    }
  }

ends

state boss4shootstate

ifvarn target -1  { state boss4shootbot break }
  ifaction ABOSS4ABOUTTOSHOOT
    ifactioncount 3
      action ABOSS4SHOOT

  ifaction ABOSS4SHOOT
  {
    ifcount 48 ifvare playerally 0
    {
      ifrnd 4
        ai AIBOSS4LAYEGGS
    }
    ifcount 26
    {
	    ifvare pal 0
	    {
		    ifrnd 32
		    {
		      ifrnd 128
		      {
		        sound SHORT_CIRCUIT
		        addphealth -2
		        ifvarand monstflags 8 addphealth -2
		      }
		      else
		      {
		        sound DUKE_GRUNT
		        addphealth -1
		        ifvarand monstflags 8 addphealth -1
		      }
		      palfrom 32 32 0 0
		    }
	    } else
	    {
		    ifcount 27 nullop else
		    { ifvarand monstflags 16 { shoot GROWSPARK shoot GROWSPARK } else shoot ELECTROPROJ }
		    ifcount 34 nullop else
		    ifcount 33 { ifvarand monstflags 16 { shoot GROWSPARK shoot GROWSPARK } else shoot ELECTROPROJ }
		    ifcount 41 nullop else
		    ifcount 40 { ifvarand monstflags 16 { shoot GROWSPARK shoot GROWSPARK } else shoot ELECTROPROJ }
		    ifvarand monstflags 8
		    {
			    ifcount 31 nullop else ifcount 30 { ifvarand monstflags 16 { shoot GROWSPARK shoot GROWSPARK } else shoot ELECTROPROJ }
			    ifcount 38 nullop else ifcount 37 { ifvarand monstflags 16 { shoot GROWSPARK shoot GROWSPARK } else shoot ELECTROPROJ }
			    ifcount 45 nullop else ifcount 44 { ifvarand monstflags 16 { shoot GROWSPARK shoot GROWSPARK } else shoot ELECTROPROJ }
		    }
	    }
    }
  }

ends

state boss4layeggs
  ifrnd 2
    sound BOS4_ROAM

  ifaction ABOSS4LAYIT
  {
    ifactioncount 3
      ifcount 32
    {
      ai AIBOSS4LAYEGGS
      ifrnd 32
        move BOSS4WALKVELS furthestdir geth
      espawn NEWBEASTHANG
      ifvarn pal 0 setactorvar[RETURN].mtype 1
      ifvarand morefx 32 setactor[RETURN].mdflags 1
      setactorvar[RETURN].playerally playerally
    }
  }
  else
    ifcount 64
      ifrnd 4
  {
    move 0

    ifrnd 88
    {
      action ABOSS4LAYIT
      sound BOS4_LAY
    }
    else
      ifp palive
        ifvarg canseeplayer 0
    {
	  ifvare target -1 ifvarn playerally 0 nullop else
	  {
      	ai AIBOSS4SHOOT
      	sound BOS4_ATTACK
      }
    }
  }

ends

state boss4dyingstate
  ifaction ABOSS4DEAD
    break
  else
    ifactioncount 9
  {
    iffloordistl 8
      sound THUD
	ifspritepal 0
    endofgame 52 else
    ifvare sprite[THISACTOR].httempang 0
    endofgame 52
    //action ABOSS4DEAD
    action ANULLACTION
    cstat 0
    move 0
    cactor BOSS4CORPSE
  }
ends

state checkboss4hitstate
  ifrnd 2
    spawn BLOODPOOL

  ifdead
  {
	ifspritepal 0
    globalsound DUKE_TALKTOBOSSFALL
    else ifrnd 64 globalsound BIGBLEED

    addkills 1 state specialdeath
    ai AIBOSS4DYING

    sound BOS4_DYING
    sound BOSS4_DEADSPEECH
  }
  else
  {
    soundonce BOS4_PAIN
    ifvarn sprite[THISACTOR].htpicnum BULLETWOUND
    {
	    debris SCRAP1 1
	    guts JIBS6 1
    }

    ifaction ABOSS4LAYIT
      break

    ifrnd 16
    ifvarn sprite[THISACTOR].htpicnum BULLETWOUND
    {
      action BOSS4FLINTCH
      move STOPPED
    }
    else ifvarn pal 0 ifvarg stun 0
    {
	  action BOSS4FLINTCH
      move STOPPED
    }
  }
ends

state boss4code

state monsterai
  ifai 0
  {
    ai AIBOSS4LAYEGGS
    ifvarn pal 0
    {
	    cstator 257
	    sizeat 40 40
	    clipdist 32
	    strength BOSS1PALSTRENGTH
	    addstrength NEWBEASTSTRENGTH
	    getactor[THISACTOR].extra hp
	    // this was already done in monsterai, but has to be done again for pal bosses	
		ifvarand monstflags 2 mulvar hp 2
	    ifvarand monstflags 4 mulvar hp 4
	    ifvarg hp 32767 setvar hp 32767
	    setactor[THISACTOR].extra hp
    }
  }
  else
    ifaction BOSS4FLINTCH
    {
	  ifvare pal 0 setvar stun 0
	  ifvare stun 0
      ifactioncount 3
      {
	      ifvare playerally 0
	        ai AIBOSS4LAYEGGS
	      else ifvarn target -1
	        ai AIBOSS4SHOOT
      }
    }
  else
    ifai AIBOSS4LAYEGGS
      state boss4layeggs
  else
    ifai AIBOSS4SHOOT
      state boss4shootstate

  ifai AIBOSS4DYING
    state boss4dyingstate
  else
  {
    ifhitweapon
      state checkboss4hitstate
    else
      ifp palive ifvarg canseeplayer 0 ifvare playerally 0
      {
	      ifvare pal 0
	      {
		    ifpdistl 1280
		    {
		        addphealth -1000
		        palfrom 63 63
	        }
	      }
	      else ifpdistl 844
	      {
		    addphealth -1
		    ifvarand monstflags 8 addphealth -1
		    palfrom 30 63
	      }
      }
  }
ends

actor BOSS4STAYPUT BOSS4STRENGTH

  fall
  ifaction 0 getactor[THISACTOR].pal pal
  cactor BOSS4
  spritepal 6
  state boss4code
  getlastpal

enda

actor BOSS4 BOSS4STRENGTH

  fall
  ifaction 0 getactor[THISACTOR].pal pal
  cactor BOSS4
  spritepal 6
  state boss4code
  getlastpal

enda

useractor enemy NEWBEASTHANGDEAD TOUGH

  ifaction 0
  {
    action ANEWBEASTHANGDEAD
    sizeat 40 40
    cstator 257
  }
  else
    ifhitweapon
  {
    ifdead
    {
      state standard_jibs
      spawn BLOODPOOL
      sound SQUISHED
      killit
    }
    else
    {
      guts JIBS6 1
      sound SQUISHED
    }
  }
enda

eventloadactor MINIBEAST
getactor[THISACTOR].lotag lotag
setactor[THISACTOR].lotag 0
ifvare lotag 1
{
    setvar mtype 1
	//cactor NEWBEAST
	sizeat 20 20 
	strength TROOPSTRENGTH
	cstator 257
}
enda

useractor enemy MINIBEAST TROOPSTRENGTH

cactor NEWBEAST

enda


state babynewbeastmoves

ifmove BABYBEASTVEL break
ifmove STOPPED break
ifmove 0 break
ifai AINEWBEASTGETENEMY move BABYBEASTVEL seekplayer else
ifai AINEWBEASTDODGE move BABYBEASTVEL dodgebullet else
ifai AINEWBEASTCHARGEENEMY move BABYBEASTVEL seekplayer else
ifai AINEWBEASTFLEENEMY move BABYBEASTVEL fleeenemy else
ifai AINEWBEASTJUMPENEMY move BABYBEASTVEL jumptoplayer else

ifcount 2 nullop else count 78

ends


state newbeastseekbot

  ifactornotstayput
  {
    ifvarl xydist 1596
    ifvarg sprite[target].yrepeat 10
    {
      ai AINEWBEASTSCRATCHENEMY
      break
    }

    ifai AINEWBEASTCHARGEENEMY
    {
      ifvarl xydist 1596
      ifvarg sprite[target].yrepeat 10
      {
        ai AINEWBEASTSCRATCHENEMY
        break
      }
	  ifvarand monstflags 256 nullop else
      ifrnd 1
        ifvarg xydist 2048 // 4096
      {
        ai AINEWBEASTSHOOT
        break
      }
    }
    else
      ifvarg xydist 4096
    {
      ai AINEWBEASTCHARGEENEMY
      break
    }

    iffloordistl 16
    {
      ifcount 32
        ifrnd 16
      {
        ifceilingdistl 96 break
        ai AINEWBEASTJUMPENEMY
      }
      break
    }

    ifrnd 4
    {
      ifnotmoving
        operate
    }
    else
      ifrnd 16
        ifbulletnear
    {
      ifgapzl 128
        ai AINEWBEASTDODGE
      else
        ifactornotstayput
      {
        ifrnd 128
        {
          ifceilingdistl 96 break
          ai AINEWBEASTJUMPENEMY
        }
        else
          ai AINEWBEASTDODGE
      }
    }
  }
  else
  {
    ifactioncount 16
    {
      ifvarl xydist 1596
      {
        ai AINEWBEASTSCRATCHENEMY
        break
      }
    }
    ifcount 16
      ifrnd 32
        move NEWBEASTWALKVEL randomangle geth
  }

ends

state newbeastseekstate

  ifvarn target -1 { state newbeastseekbot break }

  ifactornotstayput
  {
	ifvarand monstflags 2048 ifvare targetlock 0 nullop else
    ifp palive
     ifpdistl 1596
      ifvare playerally 0
      ifvarg canseeplayer 0      
    {
      ai AINEWBEASTSCRATCHENEMY
      break
    }

    ifai AINEWBEASTCHARGEENEMY
    {
	  ifvarand monstflags 2048 ifvare targetlock 0 nullop else
      ifp palive
        ifpdistl 1596
          ifvarg canseeplayer 0
            ifvare playerally 0
      {
        ai AINEWBEASTSCRATCHENEMY
        break
      }
	  ifvarand monstflags 256 nullop else
	  ifvarand monstflags 2048 ifvare targetlock 0 nullop else
      ifrnd 1
        ifpdistg 4096
          ifp palive
           ifvare playerally 0
             ifvarg canseeplayer 0        
      {
        ai AINEWBEASTSHOOT
        break
      }
    }
    else
      ifpdistg 4096
    {
      ai AINEWBEASTCHARGEENEMY
      break
    }

    iffloordistl 16
    {
      ifcount 32
        ifrnd 16
      {
        ifceilingdistl 96 break
        ai AINEWBEASTJUMPENEMY
      }
      break
    }

    ifrnd 4
    {
      ifnotmoving
        operate
    }
    else
      ifrnd 16
        ifbulletnear
    {
      ifgapzl 128
        ai AINEWBEASTDODGE
      else
        ifactornotstayput
      {
        ifrnd 128
        {
          ifceilingdistl 96 break
          ai AINEWBEASTJUMPENEMY
        }
        else
          ai AINEWBEASTDODGE
      }
    }
  }
  else
  {
    ifactioncount 16
    {
	  ifvarand monstflags 2048 ifvare targetlock 0 nullop else
      ifp palive
        ifpdistl 1596
          ifvarg canseeplayer 0
            ifvare playerally 0
      {
        ai AINEWBEASTSCRATCHENEMY
        break
      }
    }
    ifcount 16
      ifrnd 32
        move NEWBEASTWALKVEL randomangle geth
  }

ends

/*
state newbeastshrunkstate
  ifcount SHRUNKDONECOUNT
    ai AINEWBEASTGETENEMY
  else
    ifcount SHRUNKCOUNT
      sizeto 48 40
  else
    state genericshrunkcode
ends
*/

state newbeastfleestate
  ifvarand monstflags 256 nullop else
  ifcount 8
  {
    ifrnd 64
    {
	  ifvarn target -1
	  {
		  ifvarg xydist 3500 ai AINEWBEASTSHOOT
	  }
	  else
	  {
	      ifpdistg 3500
	        ifp palive
	          ifvarg canseeplayer 0
	            ifvare playerally 0
	              ai AINEWBEASTSHOOT
      }
    }
  }
  else
  {
    iffloordistl 16
    {
      ifnotmoving
        ai AINEWBEASTGETENEMY
    }
    else
      ai AINEWBEASTGETENEMY
    break
  }
ends

state newbeastthinkstate

  ifrnd 8
    soundonce NEWBEAST_ROAM

  ifactioncount 3
  {
    ifrnd 128
    {
	  ifvarand monstflags 256 nullop else
	  ifvarn target -1
	  {
		  ifvarg xydist 3500 ai AINEWBEASTSHOOT
	  }
	  else
	  {
	      ifpdistg 3500
	        ifp palive
	          ifvarg canseeplayer 0
	            ifvare playerally 0
	              ai AINEWBEASTSHOOT
      }
    }
    else
      ai AINEWBEASTGETENEMY
  }

  ifrnd 16
    ifbulletnear
  {
    ifgapzl 96
      ai AINEWBEASTDODGE
    else
    {
      ifrnd 128
      {
        ifceilingdistl 144 break
        ai AINEWBEASTJUMPENEMY
      }
      else
        ai AINEWBEASTDODGE
    }
  }
ends

state newbeastscratchbot

  ifcount 20
    ifrnd 8
  {
      ifvarl xydist 2048
    {
      ifrnd 128
        ai AINEWBEASTFLEENEMY
      break
    }
    ifrnd 80
      ai AINEWBEASTTHINK
    else
      ai AINEWBEASTGETENEMY
  }

  ifactioncount 3
  {
    ifvarg xydist 1596
      ai AINEWBEASTTHINK
    else
    {
	  ifvare mtype 0 setvar temp NEWBEASTSCRATCHAMOUNT
	  else setvar temp OCTASCRATCHINGPLAYER
	  ifvarand monstflags 8 mulvar temp -2 else mulvar temp -1
	  setactor[target].htextra temp
	  setactor[target].htpicnum KNEE
	  setactor[target].htowner THISACTOR
      resetactioncount
      resetcount
    }
  }
  else
    ifcount 15
      nullop
  else
    ifcount 14
  {
    ifvarl xydist 1596
      soundonce NEWBEAST_ATTACK
    else
      soundonce NEWBEAST_ATTACKMISS
  }
ends

state newbeastscratchstate

ifvarg target -1 { state newbeastscratchbot break }

  ifcount 20
    ifrnd 8
  {
    ifvarg canseeplayer 0
      ifpdistl 2048
    {
      ifrnd 128
        ai AINEWBEASTFLEENEMY
      break
    }
    ifrnd 80
      ai AINEWBEASTTHINK
    else
      ai AINEWBEASTGETENEMY
  }

  ifactioncount 3
  {
    ifpdistg 1596
      ai AINEWBEASTTHINK
    else ifp palive ifvarg canseeplayer 0
    {
      palfrom 16 16
      ifvare mtype 0
      addphealth NEWBEASTSCRATCHAMOUNT
      else addphealth OCTASCRATCHINGPLAYER
      ifvarand monstflags 8 addphealth NEWBEASTSCRATCHAMOUNT
      sound DUKE_GRUNT
      resetactioncount
      resetcount
    }
  }
  else
    ifcount 15
      nullop
  else
    ifcount 14
  {
    ifpdistl 1596
      soundonce NEWBEAST_ATTACK
    else
      soundonce NEWBEAST_ATTACKMISS
  }
ends

state checknewbeasthit
  ifvarn sprite[THISACTOR].htpicnum BULLETWOUND
  spawn BLOOD
/*
  ifai AINEWBEASTSHRUNK
  {
    addkills 1 state specialdeath
    sound SQUISHED
    state standard_jibs
    killit
  }
*/
  ifdead
  {
    ifwasweapon FREEZEBLAST
    {
      sound SOMETHINGFROZE
      spritepal 1
      move 0
      action ANEWBEASTFROZEN
      strength 0
      state icesmoke
      break
    }

    ifwasweapon GROWSPARK
    {
      cstat 0
      sound ACTOR_GROWING
      ai AINEWBEASTGROW
      break
    }

    addkills 1 state specialdeath

    ifwasweapon RPG
    {
      sound SQUISHED
      state liz_body_jibs
      state standard_jibs
      killit
    }
    else
      ifwasweapon RADIUSEXPLOSION
    {
      sound SQUISHED
      state liz_body_jibs
      state standard_jibs
      killit
    }
    else
    {
      state rf
      ai AINEWBEASTDYING
      ifrnd 64
        spawn BLOODPOOL
    }
    sound NEWBEAST_DYING
  }
  else
  {
    sound NEWBEAST_PAIN
    ifwasweapon GROWSPARK
      sound EXPANDERHIT
/*
//  The alien is immune to the shrinker
    ifwasweapon SHRINKSPARK
    {
      sound ACTOR_SHRINKING
      ai AINEWBEASTSHRUNK
      break
    }
*/
    state random_wall_jibs
    ifrnd 32
    ifvarn sprite[THISACTOR].htpicnum BULLETWOUND
      ai AINEWBEASTFLINTCH
    else ifvarg stun 0
      ai AINEWBEASTFLINTCH
    else
    ifwasweapon KNEE ifvarn superkicking 0
      ai AINEWBEASTFLINTCH
    else
    ifvarand monstflags 256 { ifrnd 32 ai AINEWBEASTGETENEMY }
    else
      ifrnd 32
        ifpdistg 3500
          ifp palive
            ifvarg canseeplayer 0
              ai AINEWBEASTSHOOT
  }
ends

state newbeastjumpstate
  ifaction ANEWBEASTFALL
  {
    iffloordistl 16
      ai AINEWBEASTGETENEMY
  }
  else
    ifcount 32
      action ANEWBEASTFALL
ends

state newbeastdyingstate
  ifaction ANEWBEASTLYINGDEAD
  {
    strength 0
    ifhitweapon
      ifwasweapon RADIUSEXPLOSION
    {
      sound SQUISHED
      state standard_jibs
      killit
    }
    ifcount RESPAWNACTORTIME
      ifrespawn
    {
	  addkills -1
      spawn TRANSPORTERSTAR
      cstat 257
      setvar monstatus 1
      strength NEWBEASTSTRENGTH
      ai AINEWBEASTGETENEMY
    }
  }
  else
    ifai AINEWBEASTDYING
      ifactioncount 7
  {
    iffloordistl 8
      sound THUD
    move STOPPED
    //action ANEWBEASTLYINGDEAD
    action ANULLACTION
    move 0
    cactor NEWBEASTCORPSE
  }
ends

state newbeastdodgestate
  ifcount 13
    ai AINEWBEASTGETENEMY
ends

state newbeastshootbot

state calczdist
ldist xydist THISACTOR target
ifvare xydist 0 setvar xydist 1

ifvarand monstflags 64
{
	shiftvarl zdist 8
	divvarvar zdist xydist
	sound EXPANDERSHOOT
	zshoot zdist GROWSPARK zshoot zdist GROWSPARK zshoot zdist GROWSPARK
	ifvarand monstflags 8
	{
		zshoot zdist GROWSPARK zshoot zdist GROWSPARK zshoot zdist GROWSPARK
	}
} else
ifvarand monstflags 32 
{ 
	mulvar zdist 768
	divvarvar zdist xydist
	setprojectile[ELECTROPROJ].workslike 36866
	shoot ELECTROPROJ 
	ifvarand monstflags 8 shoot ELECTROPROJ 
	setprojectile[ELECTROPROJ].workslike 32770
} 
else
ifvarand monstflags 16 
{ 
	eshoot FIREBALLPROJ 
	ifvarand monstflags 8 
	{
		setthisprojectile[RETURN].pal 13
		setthisprojectile[RETURN].extra 64
	}
} else
{
	mulvar zdist 768
	divvarvar zdist xydist
	zshoot zdist SHRINKER
}

ends

state newbeastshooting

ifvarn target -1 { state newbeastshootbot break }

ifvarand monstflags 64
{
	sound EXPANDERSHOOT
	shoot GROWSPARK shoot GROWSPARK shoot GROWSPARK
	ifvarand monstflags 8
	{
		shoot GROWSPARK shoot GROWSPARK shoot GROWSPARK
	}
} else
ifvarand monstflags 32 { shoot ELECTROPROJ ifvarand monstflags 8 shoot ELECTROPROJ } else
ifvarand monstflags 16 
{ 
	eshoot FIREBALLPROJ 
	ifvarand monstflags 8 
	{
		setthisprojectile[RETURN].pal 13
		setthisprojectile[RETURN].extra 64
	}
} else
shoot SHRINKER

ends

useractor enemystayput NEWBEASTSTAYPUT NEWBEASTSTRENGTH ai AINEWBEASTGETENEMY cstator 257 cactor NEWBEAST enda
useractor enemy NEWBEASTJUMP NEWBEASTSTRENGTH ai AINEWBEASTJUMPENEMY cstator 257 cactor NEWBEAST enda

state newbeastcode

  state monsterai
  state checksquished
  ifai 0
  {
    cstator 257
    ai AINEWBEASTGETENEMY
  }
  else
    ifaction ANEWBEASTLYINGDEAD
  {
    fall
    state newbeastdyingstate
    break
  }
  else
    ifaction ANEWBEASTFROZEN
  {
    ifcount THAWTIME
    {
      ai AINEWBEASTGETENEMY
      getlastpal
      // spritepal 0
    }
    else
      ifcount FROZENDRIPTIME
    {
      ifactioncount 26
      {
        spawn WATERDRIP
        resetactioncount
      }
    }

    ifhitweapon
    {
      ifwasweapon FREEZEBLAST
      {
        strength 0
        break
      }
      addkills 1 state specialdeath

      ifrnd 84
        spawn BLOODPOOL
      lotsofglass 30
      state standard_jibs
      sound GLASS_BREAKING
      killit
    }
    ifp pfacing
      ifpdistl FROZENQUICKKICKDIST
      ifvare superkicking 0
        pkick
    break
  }
  else
    ifai AINEWBEASTJUMPENEMY
      state newbeastjumpstate
  else
  {
    fall
    ifai AINEWBEASTGETENEMY
      state newbeastseekstate
    
    else
      ifai AINEWBEASTCHARGEENEMY
       state newbeastseekstate
    else
      ifai AINEWBEASTFLINTCH
    {
	  ifvare stun 0
      ifcount 8
        ai AINEWBEASTGETENEMY
    }

    else
      ifai AINEWBEASTDODGE
        state newbeastdodgestate
    else
      ifai AINEWBEASTSCRATCHENEMY
        state newbeastscratchstate
    else
      ifai AINEWBEASTFLEENEMY
        state newbeastfleestate
    else
      ifai AINEWBEASTTHINK
        state newbeastthinkstate
/*
    else
      ifai AINEWBEASTSHRUNK
        state newbeastshrunkstate
*/
    else
      ifai AINEWBEASTGROW
        state genericgrowcode
    else
      ifai AINEWBEASTDYING
        state newbeastdyingstate
    else
      ifai AINEWBEASTSHOOT
    {
      ifp pshrunk
        ai AINEWBEASTGETENEMY
      else
        ifcount 26
          ai AINEWBEASTGETENEMY
      else
        ifcount 25
        {
	      ifvare mtype 1 shoot SPIT else
	      state newbeastshooting
        }
      else
      {
        ifcount 5
          nullop
        else
          ifcount 4
            sound NEWBEAST_SPIT
      }
    }
  }

  ifhitweapon
    state checknewbeasthit
    
ends


useractor enemy NEWBEAST NEWBEASTSTRENGTH
  fall
  ifaction 0
  {
    cstator 257
    ifvare mtype 1 sizeat 20 20 else
    sizeat 40 40
    ai AINEWBEASTDODGE
  }
  ifaction ANEWBEASTFROZEN
    state newbeastcode
  else
  {
    spritepal 6
    state newbeastcode
    ifaction ANEWBEASTFROZEN
      break
    getlastpal
  }
  ifvare mtype 1 state babynewbeastmoves 
enda

useractor enemy NEWBEASTHANG NEWBEASTSTRENGTH

  ifaction 0
  {
    action ANEWBEASTHANG
    cstator 257
	ifvare mtype 1 { sizeat 20 20 strength TROOPSTRENGTH } else
    sizeat 40 40
  }
  else
    ifhitweapon
  {
    cactor NEWBEAST
    action ANEWBEASTSTAND
    sound NEWBEAST_PAIN
    spritepal 6
    state checknewbeasthit
    getlastpal
  }
  else
    ifspawnedby BOSS4
      ifcount 200
        ifrnd 1
  {
    cactor NEWBEAST
    action ANEWBEASTSTAND
    sound NEWBEAST_PAIN
  }
  else ifspawnedby EGG
    ifcount 78
      ifrnd 2
  {
    cactor NEWBEAST
    action ANEWBEASTSTAND
    sound NEWBEAST_PAIN
  }
enda