//DudeInfo 251
define BEASTHEALTH 500
define BEASTMASS 300
define BEASTEYEHEIGHT 27 //41
define BEASTHEARDIST 8533 //12800
define BEASTSEEDIST 34133 //51200
define BEASTPERIPHERY 341
define BEASTSPEED 89621 // 116508   k=1.3
define BEASTANGSPEED 384

action ABEASTIDLE 0 1 -5 1 1
action ABEASTRECOIL -5 1 -5 1 1
action ABEASTHACK 5 5 -5 1 15
action ABEASTWALK 50 6 -5 1 15
action ABEASTDYING 80 6 1 1 15
action ABEASTDEAD 85 1 1 1 1
action ABEASTSTOMP 734 5 -5 1 15

define STOMPS 7

state beast_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 BEASTSPEED
	setvar ai_angspeed BEASTANGSPEED
	setvar ai_periphery BEASTPERIPHERY
	setvar ai_eyeheight BEASTEYEHEIGHT
	setvar ai_heardist BEASTHEARDIST
	setvar ai_seedist BEASTSEEDIST
	setvar ai_mass BEASTMASS
	
	setactor[THISACTOR].extra BEASTHEALTH
	
	orvar sprFlags kAttrMove
	orvar sprFlags kAttrGravity
	orvar sprFlags kAttrFalling
	
	setactor[THISACTOR].xrepeat 30
	setactor[THISACTOR].yrepeat 30
	
	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

state abeastswitchaction

	ifvare ai_aistate IDLE ifvarn ai_animation_old IDLE { 
		action ABEASTIDLE
		setvar ai_animation_old IDLE
	}
	ifvare ai_aistate SEARCH ifvarn ai_animation_old GOTO {
		action ABEASTWALK
		setvar ai_animation_old GOTO
	}
	ifvare ai_aistate CHASE ifvarn ai_animation_old GOTO {	
		action ABEASTWALK
		setvar ai_animation_old GOTO
	}
	ifvare ai_aistate GOTO ifvarn ai_animation_old GOTO {
		action ABEASTWALK
		setvar ai_animation_old GOTO
	}
	ifvare ai_aistate HACK ifvarn ai_animation_old HACK {	
		action ABEASTHACK
		setvar ai_animation_old HACK
	}
	
	ifvare ai_aistate RECOIL ifvarn ai_animation_old RECOIL {
		action ABEASTRECOIL
		setvar ai_animation_old RECOIL
	}
	
	ifvare ai_aistate STOMPS ifvarn ai_animation_old STOMPS {
		action ABEASTSTOMP
		setvar ai_animation_old STOMPS
	}
			
ends

eventloadactor BEAST
	state beast_init
	addvar TOTALKILLS 1
enda

state aiBeast_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_switchaction 1
		}
	}
	state aiThinkTarget
ends

state aiBeast_thinkSearch
	setvarvar ain1 ai_goalang
	state aiChooseDirection
	state aiThinkTarget
	state zombie_findInnocent
ends


state aiBeast_thinkChase
	ifvare ai_target -1 {
		setvar ai_aistate SEARCH
		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_switchaction 1
		}
		else ifvarvare ai_target PLAYERID ifvare actorvar[PLAYERID].invisible 1 {
			setvar ai_aistate SEARCH
			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
						
						ifrnd 128 {
							ifvarg targetdist 1706 ifvarl targetdist 3413 ifvarl ai_losang 85 ifvarg sprite[ai_target].extra 10 {
								setvar ai_aistate STOMPS
								setvar peractor2 0
								setvar ai_switchaction 1
								setvar ai_statetimer 80
								setvar ai_next CHASE
							}
						}	

						ifvarl targetdist 614 {
							ifvarl ai_losang 28 {
								setvar ai_aistate HACK
								setvar ai_switchaction 1
								setvar ai_statetimer 90
								setvar ai_next CHASE
							}
						} 
						
					} else {
						//Axe zombie lost sight of target
						setvar ai_aistate GOTO
						setvar ai_statetimer 1000
						setvar ai_next IDLE
						setvar ai_switchaction 1
					}
				} else {
					//Axe zombie lost sight of target
					setvar ai_aistate GOTO
					setvar ai_statetimer 1000
					setvar ai_next IDLE
					setvar ai_switchaction 1
				}
			} else {
				//Axe zombie lost sight of target
				setvar ai_aistate GOTO
				setvar ai_statetimer 1000
				setvar ai_next IDLE
				setvar ai_switchaction 1
			}		
		}
	}
	
ends

state aibeast_hackcallback
	ifrnd 85 soundonce SLASH1 else ifrnd 85 soundonce SLASH2 else soundonce SLASH3
	ifvarn actorvar[ai_target].reflective 1 {
		getactor[THISACTOR].z z
		addvar z 8192
		setactor[THISACTOR].z z
		shoot ZOMBIEAXE 
		subvar z 8192
		setactor[THISACTOR].z z
	}
	else {
		state bloodspraystate 
		getactor[THISACTOR].extra extra
		getprojectile[ZOMBIEAXE].extra tmp
		subvarvar extra tmp
		setactor[THISACTOR].extra extra

		setactor[THISACTOR].htextra tmp
	}					
ends

actor BEAST BEASTHEALTH

	ifvare beast_transform 1 {
		state beast_init
		setvar beast_transform 0
	}
	
	ifspawnedby BL_RESPAWN ifvare peractor 0 {
		state beast_init
		setvar peractor 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 }
	
	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 {
			ifvarn CURRWEAP 3 {
				state WeaponHitProcess //aim
				state ApplyWeaponForce //add vel
			}
		}
	}
	
	ifvare ai_switchaction 1 {
		state abeastswitchaction
		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 aiBeast_thinkSearch	
			}
		}
		ifvare ai_aistate CHASE {	
			state aiMoveForward
			ifvare ai_thinktick 0 state aiBeast_thinkChase
		}
		ifvare ai_aistate GOTO {
			state aiMoveForward
			ifvare ai_thinktick 0 state aiBeast_thinkGoto
		}
		
		ifvare ai_aistate HACK {
			addvar COUNTER 1

			ifvare COUNTER 15 
				state aibeast_hackcallback
			ifvare COUNTER 25 {
				state aibeast_hackcallback
				setvar COUNTER 0
			}
		}
		
		ifvare ai_aistate STOMPS {
			ifaction ABEASTSTOMP ifactioncount 4 {
				ifvare peractor2 0 {
					ifrnd 85 sound STOM5 else ifrnd 85 sound STOMP3 else sound STOMP4
					setvar sectorid 0
					whilevarvarn sectorid NUMSECTORS 
					{
						headspritesect spriteid sectorid
						whilevarn spriteid -1 
						{
							switch sprite[spriteid].picnum
							case ZOMBIE
							case HERETIC
							case FISH
							case SPIDER
							case PHANTASM
							case HELLDOG
							case GILLBEAST
							case GARGOYLE
							case BUTCHER
							case FATRAT
							case CERBERUS
							case POD
							case TCHERNOBOG
							case BOSSSPIDER
							case APLAYER
							
								setvar le_maxradius 266
								shiftvarl le_maxradius 4
								canseespr THISACTOR spriteid fl_see 
								ifvare fl_see 1 {
									ldist len THISACTOR spriteid
									ifvarvarl len le_maxradius {
										setvar k_damage 7
										setvarvar tmp le_maxradius
										subvarvar tmp len
										mulvar tmp 55
										divvarvar tmp le_maxradius
										addvarvar k_damage tmp
										
										ifvarvare spriteid PLAYERID { //isPlayer
											setactor[spriteid].htextra k_damage
											setactor[spriteid].htowner THISACTOR
											setvar QUAKEACTIVE 3
											setvarvar QUAKETIME k_damage
											divvar QUAKETIME 2
											setvarvar QUAKEAMPL k_damage
											divvarvar QUAKEAMPL 3
										} else {
											getactor[spriteid].extra tmp
											mulvar k_damage 4
											subvarvar tmp k_damage
											setactor[spriteid].extra tmp
										}
								
										
									}
								}
							break
							endswitch
							nextspritesect spriteid spriteid
						}
						addvar sectorid 1
					}
					setvar peractor2 1
				}
				resetcount
			}
		}
		
		ifvare ai_aistate RECOIL {
			ifaction ABEASTRECOIL {
				ifactioncount 10 {
					setvar ai_aistate CHASE
					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 ai_switchaction 1
			setvar ai_next -1
		}	
		
		// process dudes for voodoo recoil
		ifvare voodootouchenemy 1 
		{
			state bloodspraystate
			ifrnd 64 state bloodspraystate
				
			setvar voodootouchenemy 0
			resetcount
		}
		
		// process dudes for recoil
		
		ifvarg sprite[THISACTOR].htextra 0 { 
		
			getactor[THISACTOR].htextra tmp
			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
			
			getactor[THISACTOR].htowner spriteid
			ifvarg spriteid 0 { 
				getactor[THISACTOR].picnum picnum
				getactor[spriteid].picnum tmp
				ifvarvarn tmp picnum { 
				switch tmp
				case ZOMBIE
				case HERETIC
				case FISH
				case SPIDER
				case PHANTASM
				case HELLDOG
				case GILLBEAST
				case GARGOYLE
				case BUTCHER
				case FATRAT
				case BOSSSPIDER
				case APLAYER
					setvarvar ai_target spriteid 
				break 
				endswitch
				}
			}
			
			ifvarg sprite[THISACTOR].htextra 100 {
				setvar ai_aistate RECOIL
				setvar ai_animation_old -1
				setvar ai_switchaction 1

				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
			
			ifrnd 85 soundonce BEASTD1 else ifrnd 85 soundonce BEASTD2 else soundonce BEASTD3
			
			action ABEASTDYING

			setvar ai_dead 1
		}
		
		ifaction ABEASTDYING
			ifactioncount 6 action ABEASTDEAD
	}
enda



