
//---------------------------INNOCENT CODE 

//DudeInfo 245
define INNOCHEALTH 45
define INNOCMASS 120
define INNOCEYEHEIGHT 30 //46
define INNOCHEARDIST 1706 //2560
define INNOCSEEDIST 0
define INNOCPERIPHERY 384
define INNOCSPEED 44000 //58254   k=1.3
define INNOCANGSPEED 384
define INNOCALERT 32768

action ACIVILIANSTAND 44 2 -5 1 40
action ACIVILIANWALK1  0 4 -5 1 12
action ACIVILIANWALK2  20 4 -5 -1 12
action ACIVILIANDYING 30 8 1 1 12
action ACIVILIANDEAD 38 1 1 1 12
action ACIVILIANHIT 39 1 -5 1 1

state civil_init
	//aiInit
	
	getactor[THISACTOR].mdflags  tmp
	orvar  tmp 16
	setactor[THISACTOR].mdflags  tmp
	
	setvar ai_animation_old -1
	setvar ai_aistate IDLE
	setvar ai_switchaction 1

	setvarvar ai_target -1
	setvarvar ai_targetx 0
	setvarvar ai_targety 0
	setvarvar ai_targetz 0
	setvar ai_statetimer 0
	
	setvar ai_speed INNOCSPEED
	setvar ai_angspeed INNOCANGSPEED
	setvar ai_periphery INNOCPERIPHERY
	setvar ai_eyeheight INNOCEYEHEIGHT
	setvar ai_heardist INNOCHEARDIST
	setvar ai_seedist INNOCSEEDIST
	setvar ai_alertchance INNOCALERT
	setvar ai_mass INNOCMASS
	
	setactor[THISACTOR].extra INNOCHEALTH
	
	orvar sprFlags kAttrMove
	orvar sprFlags kAttrGravity
	orvar sprFlags kAttrFalling
	
	setactor[THISACTOR].xrepeat 28
	setactor[THISACTOR].yrepeat 28
	
	getactor[THISACTOR].cstat acstat
	ifvarand acstat 256 nullop else
	orvar acstat 256
	ifvarand acstat 128 nullop else
	orvar acstat 128
	ifvarand acstat 1 nullop else
	orvar acstat 1
	setactor[THISACTOR].cstat acstat
	
	setvar width 128
	setvar height 8192
ends

eventloadactor CIVILIAN

	state civil_init

enda


eventloadactor CIVILIANSTOPED

	//aiInit
	
	getactor[THISACTOR].mdflags  tmp
	orvar  tmp 16
	setactor[THISACTOR].mdflags  tmp
	
	setvar ai_animation_old -1
	setvar ai_aistate IDLE
	setvar ai_switchaction 1

	setvarvar ai_target -1
	setvarvar ai_targetx 0
	setvarvar ai_targety 0
	setvarvar ai_targetz 0
	setvar ai_statetimer 0
	
	setvar ai_speed INNOCSPEED
	setvar ai_angspeed INNOCANGSPEED
	setvar ai_periphery INNOCPERIPHERY
	setvar ai_eyeheight INNOCEYEHEIGHT
	setvar ai_heardist INNOCHEARDIST
	setvar ai_seedist INNOCSEEDIST
	setvar ai_alertchance INNOCALERT
	setvar ai_mass INNOCMASS
	
	setactor[THISACTOR].extra INNOCHEALTH
	
	orvar sprFlags kAttrMove
	orvar sprFlags kAttrGravity
	orvar sprFlags kAttrFalling
	
	setactor[THISACTOR].xrepeat 28
	setactor[THISACTOR].yrepeat 28
	
	getactor[THISACTOR].cstat acstat
	ifvarand acstat 256 nullop else
	orvar acstat 256
	ifvarand acstat 128 nullop else
	orvar acstat 128
	ifvarand acstat 1 nullop else
	orvar acstat 1
	setactor[THISACTOR].cstat acstat
	
	setvar width 128
	setvar height 8192
	
	cactor CIVILIAN

enda

state acivilswitchaction

	ifvare ai_aistate IDLE ifvarn ai_animation_old IDLE { 
		action ACIVILIANSTAND
		setvar ai_animation_old IDLE
	}
	ifvare ai_aistate SEARCH ifvarn ai_animation_old GOTO {
		action ACIVILIANWALK1
		setvar ai_animation_old GOTO
	}
	ifvare ai_aistate CHASE ifvarn ai_animation_old GOTO {	
		action ACIVILIANWALK1
		setvar ai_animation_old GOTO
	}
	ifvare ai_aistate GOTO ifvarn ai_animation_old GOTO {
		action ACIVILIANWALK1
		setvar ai_animation_old GOTO
	}
	ifvare ai_aistate RECOIL ifvarn ai_animation_old RECOIL {
		action ACIVILIANHIT
		setvar ai_animation_old RECOIL
	}
			
ends

state innocSearch
	setvarvar ain1 ai_goalang
	state aiChooseDirection
	state aiThinkTarget
ends


state innocGoto
	setvarvar se_x ai_targetx
	subvarvar se_x sprite[THISACTOR].x
	setvarvar se_y ai_targety
	subvarvar se_y sprite[THISACTOR].y
	
	getangle ain1 se_x se_y
	setvarvar ai_tmp1 ain1
	state aiChooseDirection

	mulvarvar se_x se_x
	mulvarvar se_y se_y
	addvarvar se_x se_y
	sqrt se_x targetdist

	ifvarl targetdist 512 {
		setvarvar ang sprite[THISACTOR].ang
		subvarvar ang ai_tmp1
		ifvarl ang 0
			mulvar ang -1
			
		ifvarvarl ang ai_periphery {
			setvar ai_aistate SEARCH
			setvar ai_statetimer 1800
			setvar ai_next IDLE
			setvar ai_switchaction 1
		}
	}
	state aiThinkTarget
ends

state innocChase
	ifvare ai_target -1 {
		setvar ai_aistate GOTO
		setvar ai_switchaction 1
	}
	else {
		setvarvar se_x ai_targetx
		subvarvar se_x sprite[THISACTOR].x
		setvarvar se_y ai_targety
		subvarvar se_y sprite[THISACTOR].y

		// check target
		getangle ain1 se_x se_y
		setvarvar ai_tmp1 ain1
		
		state aiChooseDirection

		// target is dead
		ifvarl sprite[ai_target].extra 1 {
			setvar ai_aistate SEARCH
			setvar ai_statetimer 1800
			setvar ai_next IDLE
			setvar ai_switchaction 1
		}
		else ifvarvare ai_target PLAYERID {
			setvar ai_aistate SEARCH
			setvar ai_statetimer 1800
			setvar ai_next IDLE
			setvar ai_switchaction 1
		}
		else {
			mulvarvar se_x se_x
			mulvarvar se_y se_y
			addvarvar se_x se_y
			sqrt se_x targetdist
			ifvarvarl targetdist ai_seedist {
			
				setvarvar ai_nangle ai_tmp1

				setvarvar z ai_eyeheight
				setvarvar tmp sprite[THISACTOR].yrepeat
				shiftvarl tmp 2
				mulvarvar z tmp
				
				setvarvar tmp sprite[THISACTOR].z
				subvarvar tmp z
				
				// is there a line of sight to the target?
				ifvarg sprite[ai_target].sectnum -1 
					cansee sprite[ai_target].x sprite[ai_target].y sprite[ai_target].z sprite[ai_target].sectnum sprite[THISACTOR].x sprite[THISACTOR].y tmp sprite[THISACTOR].sectnum see
				else setvar see 0
				
				ifvare see 1 {
					setvarvar ai_losang 1024
					addvarvar ai_losang ai_nangle
					subvarvar ai_losang sprite[THISACTOR].ang
					andvar ai_losang 2047
					subvarvar ai_losang 1024
					
					ifvarl ai_losang 0
						mulvar ai_losang -1
				
					// is the target visible?

					ifvarvarl ai_losang ai_periphery {
						setvarvar ai_target PLAYERID
						setvarvar ai_targetx sprite[ai_target].x
						setvarvar ai_targety sprite[ai_target].y
						setvarvar ai_targetz sprite[ai_target].z

						ifvarl targetdist 1638 {
							ifvarl ai_losang 85 {
								setvar ai_aistate IDLE
								setvar ai_switchaction 1
							}
						} 
					} else {
						ifrnd 42 sound ICRYING else 
						ifrnd 42 sound ICRYING2 else 
						ifrnd 42 sound IDONHME else 
						ifrnd 42 sound IGOAWAY2 else 
						ifrnd 42 sound IGOAWAY3 else 
						ifrnd 42 sound INOPLACE
						setvar ai_aistate GOTO
						setvar ai_switchaction 1
						setvarvar ai_target -1
					}
				} else {
					ifrnd 42 sound ICRYING else 
					ifrnd 42 sound ICRYING2 else 
					ifrnd 42 sound IDONHME else 
					ifrnd 42 sound IGOAWAY2 else 
					ifrnd 42 sound IGOAWAY3 else 
					ifrnd 42 sound INOPLACE
					setvar ai_aistate GOTO
					setvar ai_switchaction 1
					setvarvar ai_target -1
				}
			} else {
				ifrnd 42 sound ICRYING else 
				ifrnd 42 sound ICRYING2 else 
				ifrnd 42 sound IDONHME else 
				ifrnd 42 sound IGOAWAY2 else 
				ifrnd 42 sound IGOAWAY3 else 
				ifrnd 42 sound INOPLACE
				setvar ai_aistate GOTO
				setvar ai_switchaction 1
				setvarvar ai_target -1
			}		
		}
	}
ends

gamevar cvspawn 0 2

useractor notenemy CIVILIAN INNOCHEALTH
	
	ifspawnedby BL_RESPAWN ifvare cvspawn 0 {
		state civil_init
		setvar cvspawn 1
	}

	ifvare active 1 ifvarg sprite[THISACTOR].extra 0 {
		setvarvar ai_state 1
		setvarvar ai_aistate SEARCH
		setvar ai_statetimer 1800
		setvar ai_next IDLE
		setvar ai_switchaction 1
		setvar active 0
	}

	ifvare VISION 1 ifvarg sprite[THISACTOR].extra 0 { setactor[THISACTOR].shade -127 }
	
	ifaction ACIVILIANWALK1 ifactioncount 3 action ACIVILIANWALK2
	ifaction ACIVILIANWALK2 ifactioncount 4 action ACIVILIANWALK1
	
	ifvarg flamedamage 0 { addvar COUNTER 1 state newfirestate } 
	ifinwater setvar COUNTER 0
	ifvarvare COUNTER flamedamage { setvar flamedamage 0 setvar COUNTER 0 }
	
	// process explosions
	findnearspritevar EXPLOSION2 1536 ai_naffected
	ifvare ai_naffected -1 findnearspritevar EXPLOSION3 1536 ai_naffected
	ifvare ai_naffected -1 findnearspritevar EXPLOSION4 1536 ai_naffected
	ifvare ai_naffected -1 findnearspritevar EXPLOSION6 1536 ai_naffected
	ifvarg ai_naffected 0 {

		setvarvar ain1 sprite[ai_naffected].x
		setvarvar ain2 sprite[ai_naffected].y
		setvarvar ain3 sprite[ai_naffected].z
		setvarvar ain4 4000

		state ConcussSprite
		setactor[THISACTOR].htpicnum RADIUSEXPLOSION
		
	} else {
		// process vectorscan
		ifvarg sprite[THISACTOR].htextra 0 ifvare sprite[THISACTOR].htpicnum 2595 {
			state WeaponHitProcess //aim
			state ApplyWeaponForce //add vel
		}
	}
	
	ifvare ai_switchaction 1 {
		state acivilswitchaction
		setvar ai_switchaction 0
	}

	state AirDrag
	state MoveDude
	
	ifvare burningguyactive 1 { 
		espawn BURNINGGUY 
		getactor[THISACTOR].z z
		addvar z 10144
		setactor[RETURN].z z
		setactor[THISACTOR].extra 0 //чтобы привязанный ключ смог переопределить спрайт горящего чувака
		setactor[RETURN].xrepeat 26
		setactor[RETURN].yrepeat 26
		setactorvar[RETURN].bguy_id 4 
		state enemykill_trigger
		setactorvar[RETURN].se_xvel se_xvel
		state enemykill_trigger
		killit 
	}

	//aiProcessDudes
	// don't manipulate dead guys
	ifvarg sprite[THISACTOR].extra 0 ifvare ai_dead 0 {
	
		ifvarg ai_statetimer 0 {
			subvar ai_statetimer 4
		} else setvar ai_statetimer 0

		ifvarl ai_thinktick 3 {
			addvar ai_thinktick 1
		} else setvar ai_thinktick 0

		ifvare ai_aistate IDLE {
			setvar ai_statetimer 1800
			setvar ai_next IDLE
			state aiThinkTarget
		}
		
		ifvare ai_aistate SEARCH {
			state aiMoveForward
			ifvare ai_thinktick 0 { 
				state innocSearch	
			}
		}

		ifvare ai_aistate CHASE {	
			state aiMoveForward
			ifvare ai_thinktick 0 state innocChase
		}
		ifvare ai_aistate GOTO {
			setvar ai_next -1
			state aiMoveForward
			ifvare ai_thinktick 0 state innocGoto
		} else setvar ai_next IDLE
		

		ifvare ai_aistate RECOIL {
			ifaction ACIVILIANHIT {
				ifactioncount 2 {
					setvar ai_aistate GOTO
					setvar ai_switchaction 1
				}
			}
		}

		ifvare ai_statetimer 0 ifvarn ai_next -1 {
			setvarvar ai_aistate ai_next
			setvar ai_switchaction 1
			setvar ai_next -1
		}	
		
		// process dudes for recoil
		ifvarg sprite[THISACTOR].htextra 0 { //cumulDamage[nXSprite] >= pDudeInfo.hinderDamage << 4
			setvar ai_aistate RECOIL
			setvar ai_switchaction 1
			soundonce ISCREAM1
			ifvarvarg sprite[THISACTOR].htextra sprite[THISACTOR].extra {
				action ACIVILIANDYING
				setvar ai_switchaction 0
			}
			
			setvar ai_animation_old -1

			state shootblood 
			state shootblood 
			state shootblood 
			
			state bloodspraystate
			ifrnd 64 state bloodspraystate

			getactor[THISACTOR].extra tmp
			subvarvar tmp sprite[THISACTOR].htextra
			setactor[THISACTOR].extra tmp
			setvar ai_animation_old -1
			getactor[THISACTOR].htpicnum enemy_htpic 
			setactor[THISACTOR].htextra -1
		}
	} else { 
		ifvarg sprite[THISACTOR].htextra 0 {
			ifvare sprite[THISACTOR].htextra 1000 {
				setvar in1 10
				setvar in2 JIBS9
				setvarvar utl_tmp1 in1
				whilevarn utl_tmp1 0
				{
					espawnvar in2
					getactor[RETURN].z z
					subvar z 8192
					setactor[RETURN].z z
					subvar utl_tmp1 1
				}
				killit
			}
			setactor[THISACTOR].htextra -1
		}
		
		ifvare ai_dead 0 {
			ifvarg enemy_htpic 0 { //projectile trigger
				ifvare triggerlink 6 {
					ifvarn enemy_htpic 2595  
						setvarvar hitspritesaved THISACTOR
				}
				ifvare triggerlink 3 
					setvarvar hitspritesaved THISACTOR				
			} else {
				ifvare triggerlink 6 {
					ifvare burninactivenow 1 setvarvar hitspritesaved THISACTOR //если загорелся
					ifvarg flamedamage 0 setvarvar hitspritesaved THISACTOR
					ifvare voodoohitenemy 1 setvarvar hitspritesaved THISACTOR
				}
			}
			
			setactor[THISACTOR].cstat 128
			state rx_cansetactive
			setvar enemy_tesla 0
			state enemykill_trigger
			stopsound ISCREAM1
			ifrnd 42 sound ICRYING else 
			ifrnd 42 sound ICRYING2 else 
			ifrnd 42 sound IDONHME else 
			ifrnd 42 sound IGOAWAY2 else 
			ifrnd 42 sound IGOAWAY3 else 
			ifrnd 42 sound INOPLACE
				
			action ACIVILIANDYING
			setvar ai_dead 1
		}
		
		
		ifaction ACIVILIANDYING { 
			ifactioncount 8 { action ACIVILIANDEAD }
		}
	}
	
enda



































/*
actor CIVILIANSTOPED 
fall
getactor[THISACTOR].xvel rx_can 
getactor[THISACTOR].yvel tx_can 
espawn CIVILIAN 
getactor[THISACTOR].ang ang
setactorvar[RETURN].enemy_ang ang
setactor[RETURN].yvel tx_can 
setactor[RETURN].xvel rx_can 
setactorvar[RETURN].se_xvel rx_can 
setactor[RETURN].xrepeat 28 //sprite[THISACTOR].xrepeat
setactor[RETURN].yrepeat 28 //sprite[THISACTOR].yrepeat 
killit
enda

action ACIVILIANSTAND 44 2 -5 1 40
action ACIVILIANWALK1  0 4 -5 1 12
action ACIVILIANWALK2  20 4 -5 -1 12
action ACIVILIANDYING 30 8 1 1 12
action ACIVILIANDEAD 38 1 1 1 12


move CIVILIANWALK 0
move CIVILIANSTOP 0

ai AICIVILIANWALKING ACIVILIANWALK1 CIVILIANWALK
ai AICIVILIANSTOP ACIVILIANSTAND CIVILIANSTOP
ai AICIVILIANDYING ACIVILIANDYING CIVILIANSTOP
ai AICIVILIANDEAD ACIVILIANDEAD CIVILIANSTOP


useractor notenemy CIVILIAN CIVILHANDSTRANGTH sizeat 28 28
	fall
	setvar maxSpeed 180
	setvar weight 3
	setvar width 128
	setvar height 8192
	setvar enemy_nohitscan_z 1
	setvar cantseetarget 1
	setvar fly_enemy 0
	
	ifai NO { cstat 257 setvarvar enemy_type STAYPUT setvarvar change_ai enemy_type }
	
	ifvare enemy_type STAYPUT  { 
		ifvare change_ai STAYPUT {
			ai AICIVILIANSTOP 
			setvar change_ai -1 
		}
		
		ifvare change_ai -1 {
			ifp palive ifcansee {
				state facingstate 
				ifvare set 1 {
					setvarvar target_id PLAYERID 
					setvar enemy_movtype WALK
					setvar change_ai WALK
				}
			}
		}
	}
	
	state mainAI
	
	
	ifvare enemy_type WALK {
		ifvare change_ai WALK {
			ai AICIVILIANWALKING
			setvar change_ai -1 
		}
		randvar enemyroam 512
		ifvarl enemyroam 3 { ai AICIVILIANSTOP setvar change_ai USERAI }
		
		ifaction ACIVILIANWALK1 ifactioncount 3 action ACIVILIANWALK2
		ifaction ACIVILIANWALK2 ifactioncount 4 action ACIVILIANWALK1
		
		ifai AICIVILIANSTOP { 
			ifrnd 128 { ifcount 50 { ai AICIVILIANWALKING resetcount setvar change_ai -1 } } 
			ifvarl targetdist 1536 { ai AICIVILIANWALKING setvar change_ai -1 } 
		}
	} 
	
	ifvare enemy_type GETDAMAGE {
		ifvare change_ai GETDAMAGE {
			ifrnd 42 sound ICRYING else 
			ifrnd 42 sound ICRYING2 else 
			ifrnd 42 sound IDONHME else 
			ifrnd 42 sound IGOAWAY2 else 
			ifrnd 42 sound IGOAWAY3 else 
			ifrnd 42 sound INOPLACE
			
			soundonce ISCREAM1
			setvar change_ai -1
		}	
	}
	
	ifvare enemy_type GETHEAVYDAMAGE {
		setvar enemy_type DYING 
		setvar change_ai DYING
	}
	
	ifvare enemy_type BURNING {
		espawn BURNINGGUY 
		setactor[THISACTOR].extra 0 //чтобы привязанный ключ смог переопределить спрайт горящего чувака
		setactor[RETURN].xrepeat 22
		setactor[RETURN].yrepeat 22
		setactorvar[RETURN].bguy_id 4 
		setactorvar[RETURN].se_xvel se_xvel
		state enemykill_trigger
		addkills 1 
		killit 
	}
	
	ifvare enemy_type VOODOO {
		ifvare change_ai VOODOO {
			setvar enemy_type DYING 
			setvar change_ai DYING
		}
	}
	
	ifvare enemy_type DYING {
		ifvare change_ai DYING {
			setactor[THISACTOR].extra 0
			state bloodsprayshit 
			ifrnd 42 sound ICRYING else 
			ifrnd 42 sound ICRYING2 else 
			ifrnd 42 sound IDONHME else 
			ifrnd 42 sound IGOAWAY2 else 
			ifrnd 42 sound IGOAWAY3 else 
			sound INOPLACE
			
			soundonce ISCREAM2
			ai AICIVILIANDYING
			ifrnd 128 cstat 4 else cstat 0
			setvar change_ai USERAI
		}
		ifvare change_ai USERAI {
			ifai AICIVILIANDYING ifactioncount 8 { ai AICIVILIANDEAD setvar change_ai -1 }
		}
	}
enda
*/