// CIVILIAN CODE FOR DUKEPLUS

state civhitscan

getactor[THISACTOR].ang angvar
cos mycos angvar
sin mysin angvar
getactor[THISACTOR].x x
getactor[THISACTOR].y y
getactor[THISACTOR].z z
subvar z 6144

hitscan x y z sprite[THISACTOR].sectnum mycos mysin 0 hitsector hitwall hitsprite hitx hity hitz clipmask

state pythagoras
// xydist is now the distance to hitscan destination


ends

state civilianai

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
	        }
        }
        else setvar peractor6 0
    }
	shiftvarr peractor6 1
	ifvarl peractor6 4 setvar peractor6 0
}
ifvare beamhit 0 ifwasweapon POWBEAM setactor[THISACTOR].htpicnum SHOTSPARK1
ifvare monstatus 2 break
state avoidcliffs
state avoidwater
setvar monallies 10 // otherwise monsters will not look for the civilians
addvar target2 1
ifvarg target2 25
{
	setvar target2 0
	espawn SHOOTMEALLY
	setactorvar[RETURN].myspawner THISACTOR
	setactorvar[RETURN].lotag 1
}

getactor[THISACTOR].extra hp
getactor[THISACTOR].htextra bleeding
ifvarg bleeding 0
{
	getactor[THISACTOR].htpicnum picnum
	getactor[THISACTOR].htowner spriteid
	ifvarn spriteid -1
	{
		getactor[spriteid].picnum initshade
		
		ifvare initshade APLAYER
		{
			setvarvar tempb bleeding
			shiftvarl tempb 1
			subvarvar playerally tempb
			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
				guts JIBS6 10

			ifvarn pdamage 100
			{
				mulvarvar bleeding pdamage
				divvarvar bleeding 100
				setactor[THISACTOR].htextra bleeding
			}
		} 
		else
		ifvarn mdamage 100
		{
			mulvarvar bleeding mdamage
			divvarvar bleeding 100
			setactor[THISACTOR].htextra bleeding
		}
			
	}
	
	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
		ifvare CURRWEAP 11 ifvare gravgun YES ifwasweapon SHOTSPARK1 setvar bleeding 200
		ifvarn tempb 0
		ifvarn picnum FREEZEBLAST
		ifvarn picnum ICESHARD
		{
			getactor[THISACTOR].htang peractor5 // the angle actor will move in
			setvarvar peractor6 bleeding
			mulvarvar peractor6 tempb // peractor6 is the knockback force
			setvarvar temp hp
			shiftvarl temp 1
			subvarvar peractor6 temp
			ifvarl peractor6 0 setvar peractor6 0	
		}
	
	}
}

ends

state newdirection
	getactor[THISACTOR].ang angvar
	addvar angvar 512
	randvar temp 1024
	addvarvar angvar temp
	setactor[THISACTOR].ang angvar
ends

define CIVILIANSTRENGTH 50 

move CIVWALKVEL 72
move CIVRUNVEL 144

action ACIVWALK 0 1 1 1 10
action ACIVRUN 1 1 1 1 10
action ACIVIDLE 2 1 1 1 10
action ACIVBROW 3 1 1 1 10
action ACIVTALK 4 1 1 1 10
action ACIVDIE1 -5 1 1 1 10
action ACIVDIE2 -4 1 1 1 10
action ACIVCOWER1 7 1 1 1 10
action ACIVCOWER2 0 1 1 1 10
// only used by CIV3DUCKING actor
action FLIPOFF 8 1 1 1 10
action ACIVFLIPOFF 8 1 1 1 10
action ACIVIDLE2 -38 1 1 1 10
action ACIVSIT 0 1 1 1 10

ai AICIVIDLE ACIVIDLE STOPPED geth
ai AICIVSIT ACIVSIT STOPPED geth
ai AICIVTALK ACIVTALK STOPPED geth
ai AICIVSTROLL ACIVWALK CIVWALKVEL geth
ai AICIVRUN ACIVRUN CIVRUNVEL geth
ai AICIVCOWER ACIVCOWER2 STOPPED geth
ai AICIVDIE ACIVDIE1 STOPPED geth
ai AICIVSHRUNK ACIVRUN SHRUNKVELS randomangle
ai AICIVSEEKPLAYER ACIVWALK CIVWALKVEL geth

state civ3loadactorstate
sizeat 40 40
getactor[THISACTOR].lotag activator
setactor[THISACTOR].lotag 0
getactor[THISACTOR].extra monstflags
ifvare monstflags -1 setvar monstflags 0

// bitfield
// 1 extra tough
// 2 brave, will not get scared easily
// 4 will not walk or run
// 256 will follow the player (overrides 4)

ends

eventloadactor CIV3
state civ3loadactorstate
enda

eventloadactor CIV3SIT
state civ3loadactorstate
enda

state civshrunkstate

  ifcount SHRUNKDONECOUNT
  {
	ifactor CIV3 nullop else
	cactor CIV3
    ai AICIVIDLE
  }
  else
    ifcount SHRUNKCOUNT
      sizeto 40 40
    
  action ACIVRUN
      
  ifcount 32  
	nullop
  else
  {
    sizeto MINXSTRETCH MINYSTRETCH
    spawn FRAMEEFFECT1
  }
  setactor[THISACTOR].htextra -1
ends

state startrunning
	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
	addvar angvar 768
	randvar temp 512
	addvarvar angvar temp
	setactor[THISACTOR].ang angvar
	ifactor CIV3 nullop else
	cactor CIV3
	ai AICIVRUN
	setvar countvar 0
ends

state civdiestate 
	strength 0
	cstat 256
	ifhitweapon 
	{
		ifcount 30 addvar countvar 1
		setvar temp 0
		ifwasweapon RPG setvar temp YES
		ifwasweapon RADIUSEXPLOSION setvar temp YES
		ifwasweapon RPGNOAIM setvar temp YES
		ifvarg countvar 7 setvar temp YES
		ifvare temp YES
		{
			setvar monstatus 0
			state standard_jibs
			killit
		}
	}
	ifcount 31 nullop else
	ifcount 30 spawn BLOODPOOL

ends

state talkcheck

	ifhitspace ifpdistl 2048 ifp pfacing ifcansee
	{
		ifactor CIV3 nullop else
		cactor CIV3
		ai AICIVTALK
		move STOPPED faceplayer
		
		ifvarg playerally 0
		{
			addvar mtype 1
			ifvarg mtype 2 setvar mtype 0
			ifvare mtype 0 sound FC_COOLSHADES else
			ifvare mtype 1 sound FC_YOURCUTE else
			sound FC_DOFORYOU
		}
		else
		{
			randvar temp 2
			switch temp
			case 0 sound FC_GOAWAY break
			case 1 sound FC_STOPTHAT break
			case 2 sound FC_YOUMORON break
			endswitch	
			action FLIPOFF
		}
	}

ends

state seekcheck

ifcount 30

ifcanseetarget
{
	ifpdistg 1024
	{
		ifpdistl 5120
		ifp pfacing
		{
			ifvare targetlock NO
			{
				ifrnd 96 globalsound DUKE_CRACK else
				globalsound DUKE_CRACK2
			}
			setvar targetlock YES
		}
		
		ifvare targetlock YES
		{
			ifactor CIV3 nullop else
			cactor CIV3
			ai AICIVSEEKPLAYER
		}
	}
}

ends

state cowercheck

	ifbulletnear
	{
		ifvarl sprite[THISACTOR].htextra 1
		{
			ifrnd 128 soundonce FC_HOLYSHIT else
			soundonce FC_HELPME
		}
		ifvarand monstflags 4 nullop else // does not run
		{
			findnearactorz SHOOTME 4096 65536 target
			ifvarn target -1 ifrnd 128
			{
				state startrunning
				break
			}
		}
		cactor CIV3DUCKING
		ai AICIVCOWER
		break
	}
	else
	ifvarand monstflags 2 nullop else
	ifvarg KICKBACKPIC 2
	ifvare player[THISACTOR].reloading 0
	{
		ifpdistl 6144
		{
			cactor CIV3DUCKING
			ai AICIVCOWER
			ifvare activemonsters 0
			{
				randvar temp 3
				switch temp
				case 0 soundonce FC_HESARMED break
				case 1 soundonce FC_HOLYSHIT break
				case 2 soundonce FC_YOUMORON break
				case 3 soundonce FC_PAIN break
				endswitch
				subvar playerally 1
			}
		}
	}
	
	state talkcheck

ends


state civsitstate

	state cowercheck
	ifvarand monstflags 256
	{
		state seekcheck
		ifai AICIVSEEKPLAYER break
	}


ends

state cividlestate

	state cowercheck
	ifai AICIVCOWER break
	ifai AICIVTALK break
	
	ifvarand monstflags 256
	{
		state seekcheck
		ifai AICIVSEEKPLAYER break
	}
	
	ifvarand monstflags 4 break
	
	// walk check
	ifcount 30 ifrnd 4
	{
		state civhitscan
		ifvarl xydist 1024
			state newdirection
	 	ifactor CIV3 nullop else
		cactor CIV3
		ai AICIVSTROLL
	}
ends
 
state civstrollstate

	state cowercheck
	ifai AICIVCOWER break
	ifai AICIVTALK break
	ifnotmoving 
	{ 
		state newdirection
		ai AICIVIDLE 
		break 
	}
	ifvarand monstflags 256
	{
		state seekcheck
		ifai AICIVSEEKPLAYER break
	}
	state civhitscan
	ifvarl xydist 1024 { ai AICIVIDLE break }
	

ends

state civseekstate

	state cowercheck
	ifai AICIVCOWER break
	ifai AICIVTALK break
	
	ifcount 30
	ifnotmoving
	ifvare bouncesound -1
	{
		state newdirection
		ai AICIVSEEKPLAYER
		
	}
	ifpdistl 1024 { ai AICIVIDLE break }
	
	ifpdistg 4606 ifaction ACIVWALK
	{
		action ACIVRUN
		ifvare bouncesound -1
		move CIVRUNVEL faceplayerslow
		else
		move CIVRUNVEL geth
	}
	ifpdistl 2048 ifaction ACIVRUN
	{
		action ACIVWALK
		ifvare bouncesound -1
		move CIVWALKVEL faceplayerslow
		else
		move CIVWALKVEL geth
	}
	
	// player trail code

	ifvarn bouncesound -1
	{
		setvarvar spriteid ptrails[bouncesound]
		ifvare spriteid -1 { setvar bouncesound -1  }
		else
		{
			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
					{
						ldist tempd THISACTOR spriteid
						ifvarl tempd 384 
						{ 
							addvar tempb 1 
							ifvare tempb 100 setvar bouncesound -1
						}
						else { setvarvar bouncesound tempb setvar tempb 100 }
					}
				}
				ifvare bouncesound 100 setvar bouncesound -1 
			}
			else
			{
				getactor[spriteid].x mx
				getactor[spriteid].y my
				subvarvar mx sprite[THISACTOR].x
				subvarvar my sprite[THISACTOR].y
				getangle angvar mx my
				setactor[THISACTOR].ang angvar
			}
		}
	}
	else
 	{
		setvar temp 99
		whilevarn temp -1
		{
			setvarvar spriteid ptrails[temp]
			ifvarn spriteid -1
			{
				canseespr THISACTOR spriteid tempc
				ifvare tempc 1
				{
					ldist tempd THISACTOR spriteid
					ifvarg tempd 1024
					{
						getactor[spriteid].x mx
						getactor[spriteid].y my
						subvarvar mx sprite[THISACTOR].x
						subvarvar my sprite[THISACTOR].y
						getangle angvar mx my
						setactor[THISACTOR].ang angvar
						setvarvar bouncesound temp
						setvar temp 0
					}
				}
			}
			subvar temp 1
		}
 	}

ends

state civrunstate

	ifcount 10
	{
		setvar temp 0
		ifnotmoving setvar temp YES
		state civhitscan
		ifvarl xydist 768 setvar temp YES
		ifvare temp YES
		{
			ifvarg countvar 3 { cactor CIV3DUCKING ai AICIVCOWER setvar countvar 0 break }
			state newdirection
			resetcount
			addvar countvar 1
		}
	}
	
	ifcount 120 ai AICIVSTROLL

ends

state civcowerstate

	ifvarand monstflags 2 nullop else
	ifvarn KICKBACKPIC 0
	ifvare activemonsters 0
	{
		ifpdistl 6144 resetcount
	}
	ifcount 90 ifrnd 4
	{
		ifvarand monstflags 4 nullop else
		{
			findnearactorz SHOOTME 4096 16384 target
			ifvarn target -1
			{
				state startrunning
				break
			}
		}
		ifactor CIV3 nullop else
		cactor CIV3
		ai AICIVIDLE
	}
ends

state civtalkstate
	ifcount 70 
	{
		ai AICIVIDLE 
		ifrnd 128 { action ACIVIDLE2 }
	}
ends

state femcivcode

ifvarg peractor1 0
{
	subvar peractor1 1
	ifvarl peractor1 20 { ifvarand peractor1 1 cstat 514 else cstat 32768 } else
	ifvarl peractor1 40 cstator 512 else
	ifvarl peractor1 60 cstator 2
	ifvare peractor1 0 killit
}

ifai 0
{
	ifvarand monstflags 256 { addvar survivors 1 addvar startsurv 1 }
	randvar mtype 2 // which idle talk she starts with
	sizeat 40 40
	cstat 257
	setvar monstatus 1
	ifvarand monstflags 1 addstrength CIVILIANSTRENGTH
	ifactor CIV3SIT ai AICIVSIT else
	ai AICIVIDLE
	setvar playerally 10 // the higher the better
	
}
fall
ifai AICIVSHRUNK { state civshrunkstate break }
state civilianai


ifai AICIVDIE { state civdiestate break }
ifai AICIVIDLE state cividlestate else
ifai AICIVSTROLL state civstrollstate else
ifai AICIVRUN state civrunstate else
ifai AICIVCOWER state civcowerstate else
ifai AICIVTALK state civtalkstate else
ifai AICIVSIT state civsitstate else
ifai AICIVSEEKPLAYER state civseekstate

ifhitweapon
{
	ifdead
	{
		ifvarn activator 0 state activatorstate
		setvar monstatus 2
		ifvarand monstflags 256
		{
			addvar survkilled 1
			subvar survivors 1
		}
		setvar countvar 0
		// sound FEMCIVDEATH
		cactor CIV3DUCKING
		setvar temp 0
		ifwasweapon RPG setvar temp YES
		ifwasweapon RPGNOAIM setvar temp YES
		ifvare temp YES
		{
			sound SQUISHED
			state standard_jibs
			killit
		}
		ai AICIVDIE
		ifrnd 128 action ACIVDIE2
		sound FC_DEATH
		userquote 1193
	}
	else 
	{
		ifrnd 128 sound FC_PAIN else ifrnd 128 soundonce FC_SCREAM1 else soundonce FC_SCREAM2
		
		ifwasweapon SHRINKSPARK
        {
	      ifactor CIV3 nullop else
          cactor CIV3
          ai AICIVSHRUNK
          sound ACTOR_SHRINKING
          break
        }
		ifvarand monstflags 4 nullop else
		{
			findnearactorz SHOOTME 4096 65536 target
			ifvare target -1 findnearactorz SHOOTME 16384 65536 target
			ifvare target -1 ifvare initshade APLAYER getplayer[THISACTOR].i target
			ifvarn target -1 ifrnd 128
			{
				state startrunning
				break
			}
		}
		ifstrength 30
		{
			cactor CIV3DUCKING
			ai AICIVCOWER
			break
		}
	}
}

ends

useractor notenemy CIV3 CIVILIANSTRENGTH
state femcivcode
enda

useractor notenemy CIV3DUCKING CIVILIANSTRENGTH
state femcivcode
enda

useractor notenemy CIV3SIT CIVILIANSTRENGTH
state femcivcode
enda

eventloadactor CIVILIANRESCUE
getactor[THISACTOR].extra countvar
ifvare countvar -1 setvar countvar 1
getactor[THISACTOR].lotag activator
setactor[THISACTOR].lotag 0
ifvare activator 0 getactor[THISACTOR].yvel activator
cstat 32768
setvar survivors 0
setvar startsurv 0
setvar rescued 0
setvar survkilled 0
enda

actor CIVILIANRESCUE 0

ifvare showsurvivors 0
{
	ifcansee
	setvar showsurvivors 150
}
else setvar showsurvivors 150
ifvarg countvar 0
{
	headspritestat spriteid 1
	whilevarn spriteid -1
	{
		ifvare sprite[spriteid].picnum CIV3
		ifvarand actorvar[spriteid].monstflags 256
		ifvarvare sprite[THISACTOR].sectnum sprite[spriteid].sectnum
		{
			subvar countvar 1
			addvar rescued 1
			subvar survivors 1
			getactorvar[spriteid].monstflags temp
			xorvar temp 256
			orvar temp 4
			setactorvar[spriteid].monstflags temp
			setactorvar[spriteid].playerally 50
			setactorvar[spriteid].peractor1 150 // countdown to fade
			ifvare countvar 0
			{
				state activatorstate
			}
		}
		nextspritestat spriteid spriteid
	}
}
else killit

enda