
//DudeInfo 229
define TCHERNHEALTH 2500
define TCHERNMASS 120
define TCHERNEYEHEIGHT 0
define TCHERNHEARDIST 17066 //25600
define TCHERNSEEDIST 34133 //51200
define TCHERNPERIPHERY 512
define TCHERNSPEED 44000 //58254   k=1.3
define TCHERNANGSPEED 384

action ATCHERIDLE  0 1 -5 1 1
action ATCHERATTACK1 -1305 2 -5 1 7
action ATCHERATTACK2 -1305 4 -5 1 12
action ATCHERWALK 826 6 -5 1 10
action ATCHERHIT -2116 1 -5 1 1
action ATCHERDYING -2280 7 -1 1 12
action ATCHERDYING2 -2273 3 -1 1 12
action ATCHERDEAD -914 3 -1 1 10

define TBURNING 7

spritenoshade TCHERNMISSLE
defineprojectile TCHERNMISSLE PROJ_WORKSLIKE 2
defineprojectile TCHERNMISSLE PROJ_HITRADIUS 2048
defineprojectile TCHERNMISSLE PROJ_PAL 1
defineprojectile TCHERNMISSLE PROJ_SXREPEAT 40
defineprojectile TCHERNMISSLE PROJ_SYREPEAT 40
defineprojectile TCHERNMISSLE PROJ_VEL 512
defineprojectile TCHERNMISSLE PROJ_DROP 0
defineprojectile TCHERNMISSLE PROJ_BSOUND -1
defineprojectile TCHERNMISSLE PROJ_ISOUND EXPLODCM
defineprojectile TCHERNMISSLE PROJ_SOUND SNGAIR2
defineprojectile TCHERNMISSLE PROJ_CLIPDIST 32
defineprojectile TCHERNMISSLE PROJ_XREPEAT 32
defineprojectile TCHERNMISSLE PROJ_YREPEAT 32
defineprojectile TCHERNMISSLE PROJ_CSTAT 128
defineprojectile TCHERNMISSLE PROJ_SPAWNS EXPLOSION4
defineprojectile TCHERNMISSLE PROJ_ISOUND EXPLODCM

gamevar tcher_shoot 0 2
eventloadactor TCHERNOBOG  

	//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 TCHERNSPEED
	setvar ai_angspeed TCHERNANGSPEED
	setvar ai_periphery TCHERNPERIPHERY
	setvar ai_eyeheight TCHERNEYEHEIGHT
	setvar ai_heardist TCHERNHEARDIST
	setvar ai_seedist TCHERNSEEDIST
	setvar ai_mass TCHERNMASS
	
	setactor[THISACTOR].extra TCHERNHEALTH
	
	orvar sprFlags kAttrMove
	orvar sprFlags kAttrGravity
	orvar sprFlags kAttrFalling
	
	setactor[THISACTOR].xrepeat 42
	setactor[THISACTOR].yrepeat 42
	
	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 256
	setvar height 8192
	addvar TOTALKILLS 1

enda

state tcher_thinkSearch
	setvarvar ain1 ai_goalang
	state aiChooseDirection
	state aiThinkTarget
ends

state tcher_thinkChase
	ifvare ai_target -1 {
		setvar ai_aistate GOTO
		setvar ai_statetimer 1000
		setvar ai_next IDLE
		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 1000
			setvar ai_next IDLE
			setvar ai_switchaction 1
		}
		else ifvarvare ai_target PLAYERID ifvare actorvar[PLAYERID].invisible 1 {
			setvar ai_aistate SEARCH
			setvar ai_statetimer 1000
			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 ai_losang 85 {
							ifvarg targetdist 2218 ifvarl targetdist 5290 {
								setvar ai_aistate TBURNING
								setvar ai_switchaction 1
								setvar ai_statetimer 60
								setvar ai_next CHASE
							} else ifvarg targetdist 853 ifvarl targetdist 2218 {
								setvar ai_aistate HACK
								setvar ai_switchaction 1
								setvar ai_statetimer 60
								setvar ai_next CHASE
							}
						} 
					} else {
						setvar ai_aistate GOTO
						setvar ai_statetimer 1000
						setvar ai_next IDLE
						setvar ai_switchaction 1
					}
				} else {
					setvar ai_aistate GOTO
					setvar ai_statetimer 1000
					setvar ai_next IDLE
					setvar ai_switchaction 1
				}
			} else {
				setvar ai_aistate GOTO
				setvar ai_statetimer 1000
				setvar ai_next IDLE
				setvar ai_switchaction 1
			}		
		}
	}
	
ends


state tcher_thinkGoto
	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 1000
			setvar ai_next IDLE
			setvar ai_switchaction 1
		}
	}
	state aiThinkTarget
ends

state tcherswitchaction

	ifvare ai_aistate IDLE ifvarn ai_animation_old IDLE { 
		action ATCHERIDLE
		setvar ai_animation_old IDLE
	}
	ifvare ai_aistate SEARCH ifvarn ai_animation_old GOTO {
		action ATCHERWALK
		setvar ai_animation_old GOTO
	}
	ifvare ai_aistate CHASE ifvarn ai_animation_old GOTO {	
		action ATCHERWALK
		setvar ai_animation_old GOTO
	}
	ifvare ai_aistate GOTO ifvarn ai_animation_old GOTO {
		action ATCHERWALK
		setvar ai_animation_old GOTO
	}
	ifvare ai_aistate HACK ifvarn ai_animation_old HACK {	
		action ATCHERATTACK2
		setvar ai_animation_old HACK
	}
	ifvare ai_aistate TBURNING ifvarn ai_animation_old TBURNING {	
		action ATCHERATTACK1
		setvar ai_animation_old TBURNING
	}
	ifvare ai_aistate RECOIL ifvarn ai_animation_old RECOIL {
		action ATCHERHIT
		setvar ai_animation_old RECOIL
	}
		
ends

state tcher_callback
	
	eshoot TCHERNMISSLE
	
	cos mycos sprite[THISACTOR].ang
	mulvar mycos 100
	divvarvar mycos 16384
	getactor[THISACTOR].ang tmp
	addvarvar tmp 512
	cos tmp tmp
	mulvarvar tmp 350
	divvarvar tmp 16384
	addvarvar mycos tmp
	addvarvar mycos sprite[THISACTOR].x
	
	cos mysin sprite[THISACTOR].ang
	mulvar mysin 100
	divvarvar mysin 16384
	getactor[THISACTOR].ang tmp
	addvarvar tmp 512
	sin tmp tmp
	mulvarvar tmp 350
	divvarvar tmp 16384
	addvarvar mysin tmp
	addvarvar mysin sprite[THISACTOR].y
	
	setactor[RETURN].x mycos
	setactor[RETURN].y mysin 
	getactor[THISACTOR].z z
	subvar z 3100
	setactor[RETURN].z z
	
	eshoot TCHERNMISSLE
	
	cos mycos sprite[THISACTOR].ang
	mulvar mycos 100
	divvarvar mycos 16384
	getactor[THISACTOR].ang tmp
	addvarvar tmp 512
	cos tmp tmp
	mulvarvar tmp -350
	divvarvar tmp 16384
	addvarvar mycos tmp
	addvarvar mycos sprite[THISACTOR].x
	
	cos mysin sprite[THISACTOR].ang
	mulvar mysin 100
	divvarvar mysin 16384
	getactor[THISACTOR].ang tmp
	addvarvar tmp 512
	sin tmp tmp
	mulvarvar tmp -350
	divvarvar tmp 16384
	addvarvar mysin tmp
	addvarvar mysin sprite[THISACTOR].y
	
	setactor[RETURN].x mycos
	setactor[RETURN].y mysin 
	getactor[THISACTOR].z z
	subvar z 3100
	setactor[RETURN].z z
	 
ends

useractor notenemy TCHERNOBOG TCHERNHEALTH

	ifvare VISION 1 ifvarg sprite[THISACTOR].extra 0 { setactor[THISACTOR].shade -127 }
	
	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 300
		
		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 tcherswitchaction
		setvar ai_switchaction 0
	}

	state AirDrag
	state MoveDude
	
	//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 {
			state aiThinkTarget
		}
			
		ifvare ai_aistate SEARCH {
			state aiMoveForward
			ifvare ai_thinktick 0 { 
				state tcher_thinkSearch	
			}
		}
		ifvare ai_aistate CHASE {	
			state aiMoveForward
			ifvare ai_thinktick 0 state tcher_thinkChase
		}
		ifvare ai_aistate GOTO {
			state aiMoveForward
			ifvare ai_thinktick 0 state tcher_thinkGoto
		}
		
		ifpdistl 1536 {
			addphealth -300
		}
		
		ifvare ai_aistate HACK { //missle
			ifvare tcher_shoot 0 { 
				ifactioncount 2 { 
					state tcher_callback
					setvar tcher_shoot 1 
				} 
			}
		}
		
		ifvare ai_aistate TBURNING { //burn
			ifactioncount 1 {
				ifvarl actorvar[ai_target].flamedamage 1200 {
					getactorvar[ai_target].flamedamage tmp
					addvar tmp 40
					setactorvar[ai_target].flamedamage tmp
				}
				ifrnd 64 {
					setvar ai_aistate HACK
					setvar ai_switchaction 1
					setvar ai_statetimer 60
					setvar ai_next CHASE
				}
				resetactioncount
			}
		}

		ifvare ai_aistate RECOIL {
			ifaction ATCHERHIT {
				ifactioncount 1 {
					setvar ai_aistate SEARCH
					setvar ai_statetimer 1000
					setvar ai_next IDLE
					setvar ai_switchaction 1
					getactor[THISACTOR].cstat tmp
					orvar tmp 257
					setactor[THISACTOR].cstat tmp
				}
			}
		}
		
		ifvare ai_statetimer 0 ifvarn ai_next -1 {
			setvarvar ai_aistate ai_next
			setvar tcher_shoot 0
			setvar ai_switchaction 1
			setvar ai_next -1
		}
		
		// process dudes for voodoo recoil
		ifvare voodootouchenemy 1 
		{
			ifrnd 42 sound TBPAIN1 else 
			ifrnd 42 sound TBPAIN2 else 
			ifrnd 42 sound TBPAIN3 else 
			ifrnd 42 sound TBPAIN4 else 
			ifrnd 42 sound TBPAIN5 else 
			sound TBPAIN6
			
			setvar voodootouchenemy 0
		}

		ifvarg sprite[THISACTOR].htextra 0 { 
			ifvarvare sprite[THISACTOR].htowner THISACTOR {
				setactor[THISACTOR].htextra -1
			}
			
			ifvarvare sprite[THISACTOR].htpicnum STAFFALTPROJ {
				getactor[THISACTOR].htextra tmp
				divvar tmp 3
				setactor[THISACTOR].htextra tmp
			}

			ifvarvarg sprite[THISACTOR].htextra sprite[THISACTOR].extra {
				action ATCHERHIT
				setvar ai_switchaction 0
			}

			getactor[THISACTOR].htextra tmp
			ifvarg tmp 10 {
				getuserdef[THISACTOR].player_skill pskill
				ifvare pskill 2 {
					mulvarvar tmp 4
					divvarvar tmp 5
				}
				ifvare pskill 3 {
					mulvarvar tmp 3
					divvarvar tmp 4
				}
				ifvare pskill 4 {
					mulvarvar tmp 2
					divvarvar tmp 3
				}
				ifvare pskill 5 {
					divvarvar tmp 2
				}
				setactor[THISACTOR].htextra tmp
			}
			
			ifvarg sprite[THISACTOR].htextra 15 { //cumulDamage[nXSprite] >= pDudeInfo.hinderDamage << 4
				action ATCHERHIT
				ifrnd 42 sound TBPAIN1 else 
				ifrnd 42 sound TBPAIN2 else 
				ifrnd 42 sound TBPAIN3 else 
				ifrnd 42 sound TBPAIN4 else 
				ifrnd 42 sound TBPAIN5 else 
				sound TBPAIN6
				setvar ai_aistate RECOIL

				state shootblood 
				state shootblood 
				state shootblood 
				
				state bloodspraystate
				ifrnd 64 state bloodspraystate
			
				setvar ai_next -1
				
				resetcount
			}
			
			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 {
			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
			addkills 1	
			state rx_cansetactive
			setvar enemy_tesla 0
			state enemykill_trigger
			
			soundonce TBDIES
			action ATCHERDYING

			setvar ai_dead 1
		}
				
		ifaction ATCHERDYING ifactioncount 7 action ATCHERDYING2
		ifaction ATCHERDYING2 ifactioncount 30 action  ATCHERDEAD
		ifaction ATCHERDEAD ifactioncount 3 killit
	}
enda