//------------------------------CULTIST CODE

gamevar cult_count 0 2
gamevar cult_extra 0 2
gamevar cult_htextra 0 2
gamevar cult_tmp 0 2
gamevar cult_duck 0 2

defineprojectile CULTTOM PROJ_WORKSLIKE 4097
defineprojectile CULTTOM PROJ_SPAWNS -1
defineprojectile CULTTOM PROJ_DECAL -1
defineprojectile CULTTOM PROJ_XREPEAT 3
defineprojectile CULTTOM PROJ_YREPEAT 3

defineprojectile CULTSPARK PROJ_WORKSLIKE 4097
defineprojectile CULTSPARK PROJ_SPAWNS -1
defineprojectile CULTSPARK PROJ_DECAL -1
defineprojectile CULTSPARK PROJ_XREPEAT 3
defineprojectile CULTSPARK PROJ_YREPEAT 3

defineprojectile TNTBUNDLE2 PROJ_WORKSLIKE 6150 //6286
defineprojectile TNTBUNDLE2 PROJ_HITRADIUS 0
defineprojectile TNTBUNDLE2 PROJ_BSOUND -1
defineprojectile TNTBUNDLE2 PROJ_VEL 256
defineprojectile TNTBUNDLE2 PROJ_DROP -368
defineprojectile TNTBUNDLE2 PROJ_XREPEAT 24
defineprojectile TNTBUNDLE2 PROJ_YREPEAT 24
defineprojectile TNTBUNDLE2 PROJ_EXTRA 0
defineprojectile TNTBUNDLE2 PROJ_CSTAT 0
defineprojectile TNTBUNDLE2 PROJ_SPAWNS -1
actor TNTBUNDLE2 0 FRAME_FRAME1 enda

defineprojectile ENEMYTNTBUNDLE PROJ_WORKSLIKE 6146
defineprojectile ENEMYTNTBUNDLE PROJ_HITRADIUS 0
defineprojectile ENEMYTNTBUNDLE PROJ_VEL 512
defineprojectile ENEMYTNTBUNDLE PROJ_DROP -512
defineprojectile ENEMYTNTBUNDLE PROJ_XREPEAT 32
defineprojectile ENEMYTNTBUNDLE PROJ_YREPEAT 32
defineprojectile ENEMYTNTBUNDLE PROJ_EXTRA 0
defineprojectile ENEMYTNTBUNDLE PROJ_CSTAT 128
defineprojectile ENEMYTNTBUNDLE PROJ_SPAWNS ENEMYTNTFLOOR

spritenoshade HERETIC
spriteshadow HERETIC

eventloadactor HERETICSTOPED1
setactor[THISACTOR].extra HERETICSTRENGTH
enda

eventloadactor HERETICSTOPED2
setactor[THISACTOR].extra HERETICSTRENGTH
enda

actor HERETICSTOPED1
fall
addvar TOTALKILLS 1 
getactor[THISACTOR].xvel rx_can 
getactor[THISACTOR].yvel tx_can 
espawn HERETIC 
getactor[THISACTOR].ang ang
setactorvar[RETURN].enemy_ang ang
setactor[RETURN].extra HERETICSTRENGTH 
setactor[RETURN].yvel tx_can 
setactor[RETURN].xvel rx_can 
setactorvar[RETURN].se_xvel rx_can 
setactor[RETURN].xrepeat sprite[THISACTOR].xrepeat
setactor[RETURN].yrepeat sprite[THISACTOR].yrepeat 
setactorvar[RETURN].settotalkill 1 
setactor[RETURN].pal sprite[THISACTOR].pal
killit
enda

actor HERETICSTOPED2
fall
addvar TOTALKILLS 1 
getactor[THISACTOR].xvel rx_can 
getactor[THISACTOR].yvel tx_can 	
espawn HERETIC 
getactor[THISACTOR].ang ang
setactorvar[RETURN].enemy_ang ang
setactor[RETURN].extra HERETICSTRENGTH 
setactor[RETURN].yvel tx_can 
setactor[RETURN].xvel rx_can
setactorvar[RETURN].se_xvel rx_can 
setactor[RETURN].xrepeat sprite[THISACTOR].xrepeat 
setactor[RETURN].yrepeat sprite[THISACTOR].yrepeat 	
setactor[RETURN].pal sprite[THISACTOR].pal
setactorvar[RETURN].settotalkill 1 
killit
enda


eventloadactor HERETICDUCKED1
fall
addvar TOTALKILLS 1 
getactor[THISACTOR].xvel rx_can 
getactor[THISACTOR].yvel tx_can
espawn HERETIC 
setactor[RETURN].pal sprite[THISACTOR].pal
setactorvar[RETURN].cult_duck 1
getactor[THISACTOR].ang ang
setactorvar[RETURN].enemy_ang ang
setactor[RETURN].extra HERETICSTRENGTH 
setactor[RETURN].yvel tx_can 
setactor[RETURN].xvel rx_can
setactorvar[RETURN].se_xvel rx_can 
setactor[RETURN].xrepeat sprite[THISACTOR].xrepeat
setactor[RETURN].yrepeat sprite[THISACTOR].yrepeat 
setactorvar[RETURN].settotalkill 1
killit
enda

action AHERETICWALK1  0 4 -5 1 15
action AHERETICWALK2  20 4 -5 -1 15
action AHERETICSTAYPUT  0 1 -5 1 1
action AHERETICSHOOTGUN -40 2 -5 1 36
action AHERETICTOMMYSHOOT -40 2 -5 1 7
action AHERETICSHOOTGUNDUCK 535 2 -5 1 28
action AHERETICTOMMYSHOOTDUCK 535 2 -5 1 7
action AHERETICGRENADE -30 5 -5 1 15
action AHERETICSWIM 381 4 -5 1 20
action AHERETICWATERSTAYPUT 381 1 -5 1 1
action AHERETICTESLA  813 1 -5 1 1
action AHERETICDYING 70 7 1 1 15
action AHERETICDEAD 77 1 1 1 10
action AHERETICDEADDD 77 1 1 1 1
action AHERETICDEADD 76 2 1 1 30
action AHERETICGUTS 88 6 1 1 10
action AHERETICDEAD2 94 1 1 1 10
action AHERETICDUCK 515 3 -5 1 22
action AHERETICDUCKSTAYPUT 515 1 -5 1 1
action AHERETICHURT 65 1 5 1 1
action AHERETICDEAD4 -277 2 1 1 30
action AHERETICDEAD5 -273 4 1 1 30
action AHERETICTRANSFORM 1159 4 1 1 30
action AHERETICTRANSFORM2 1162 1 1 1 1

move HERETICWALK 0
move HERETICSTOP 

ai AIHERETICWALK AHERETICWALK1 HERETICWALK 
ai AIHERETICSWIM AHERETICSWIM HERETICWALK 
ai AIHERETICSTEP AHERETICWALK1 HERETICSTOP 
ai AIHERETICDUCKSTEP AHERETICDUCK HERETICSTOP 
ai AIHERETICRANDOM AHERETICWALK1 HERETICWALK randomangle //faceplayer
ai AIHERETICRANDOMSWIM AHERETICSWIM HERETICWALK seekplayer
ai AIHERETICHURT AHERETICHURT HERETICSTOP  
ai AIHERETICSTAYPUT AHERETICSTAYPUT HERETICSTOP
ai AIHERETICDUCKSTAYPUT AHERETICDUCKSTAYPUT HERETICSTOP
ai AIHERETICWATERSTAYPUT AHERETICWATERSTAYPUT HERETICSTOP

ai AIHERETICSHOOTGUN AHERETICSHOOTGUN HERETICSTOP  
ai AIHERETICTOMMYSHOOT AHERETICTOMMYSHOOT HERETICSTOP   
ai AIHERETICGRENADE AHERETICGRENADE HERETICSTOP  
ai AIHERETICSHOOTGUNDUCK AHERETICSHOOTGUNDUCK HERETICSTOP
ai AIHERETICTOMMYSHOOTDUCK AHERETICTOMMYSHOOTDUCK HERETICSTOP 
ai AIHERETICDYING AHERETICDYING HERETICSTOP 
ai AIHERETICDEAD AHERETICDEAD HERETICSTOP 
ai AIHERETICDEADD AHERETICDEADD HERETICSTOP 
ai AIHERETICDEADDD AHERETICDEADDD HERETICSTOP 
ai AIHERETICTESLA AHERETICTESLA HERETICSTOP 
ai AIHERETICGUTS AHERETICGUTS HERETICSTOP faceplayer
ai AIHERETICDEAD2 AHERETICDEAD2 HERETICSTOP 
ai AIHERETICDEAD4 AHERETICDEAD4 HERETICSTOP 
ai AIHERETICDEAD5 AHERETICDEAD5 HERETICSTOP 

state spawncoord
getactor[THISACTOR].x x
getactor[THISACTOR].y y
setvarvar x2 x
addvarvar x2 256
randvar ang 2048
rotatepoint x y x2 y ang x3 y3
setactor[RETURN].x x3
setactor[RETURN].y y3
ends


state transform
	getactor[THISACTOR].cstat tmp
	ifvarand tmp 256 xorvar tmp 256
	setactor[THISACTOR].cstat tmp
	setvar enemy_type 666
	setvar change_ai USERAI
	setvar burningguyactive 0
	action AHERETICTRANSFORM
ends

state randomspawn
	ifvare se_xvel 0 ifvare sprite[THISACTOR].xvel 0 {
		randvar tmp 4
		ifvare tmp 0 { espawn HEART state spawncoord }
		ifvare tmp 1 { ifspritepal 0 espawn FEWSHELLS else espawn FEWBULLETS state spawncoord }
		ifvare tmp 2 { ifspritepal 0 espawn SAWED-OFF else ifspritepal 3 espawn TOMMY state spawncoord }
	}
ends


state hereticshootgun
cstator 2048
setvar enemyshoot 1 
ifvarn sprite[THISACTOR].pal 12 { 
	getactor[THISACTOR].ang ang
	setvarvar saveang ang
	randvar tomrange 32
	ifrnd 128 mulvar tomrange -1
	addvarvar ang tomrange
	setactor[THISACTOR].ang ang
} else {
	getactor[THISACTOR].ang saveang
}

shoot CULTSPARK 
shoot CULTSPARK  
shoot CULTSPARK  
shoot CULTSPARK  
shoot CULTSPARK  
shoot CULTSPARK 
shoot CULTSPARK  
shoot CULTSPARK  
getuserdef[THISACTOR].player_skill pskill
ifvarn pskill 3 shoot CULTSPARK    
ifrnd 128 sound CULTATT1 else 
sound CULTATT2
setactor[THISACTOR].ang saveang
setactorvar[THISACTOR].enemy_ang saveang
ends

state hereticteslashoot 
cstator 2048
setvar enemyshoot 1 
eshoot PLASMAPROJ  
ifvare cult_duck 1 {
	getactor[RETURN].z z
	addvar z 4096
	setactor[RETURN].z z
}
sound TESSNGFR
sound TESSNGHT
ends

state heretictommyshoot 
cstator 2048
setvar enemyshoot 1 
getactor[THISACTOR].ang ang
setvarvar saveang ang
randvar tomrange 32
ifrnd 128 mulvar tomrange -1
addvarvar ang tomrange
setactor[THISACTOR].ang ang
shoot CULTTOM  
sound TOM
setactor[THISACTOR].ang saveang
setactorvar[THISACTOR].enemy_ang saveang
ends


state cultist_step
ifvarn target_id -1 {
	getactor[target_id].x x2
	getactor[target_id].y y2
	getactor[THISACTOR].x x
	getactor[THISACTOR].y y
	subvarvar x2 x
	subvarvar y2 y
	getangle emang x2 y2
	setvarvar enemy_ang emang
	setactor[THISACTOR].ang emang
	
	addvarvar emang cult_tmp //направление движения
	cos xvel emang
	sin yvel emang
	mulvarvar xvel 40 //Скорость движения боком
	mulvarvar yvel 40
	divvar xvel TRIG_MAX
	divvar yvel TRIG_MAX
	movesprite THISACTOR xvel yvel 0 CLIPMASK0 RETURN
} else {
	setvar change_ai WALK
	setvarvar change_ai enemy_type
}
ends	


useractor notenemy HERETIC HERETICSTRENGTH sizeat 28 28
	getactor[THISACTOR].x enemy_x
	getactor[THISACTOR].y enemy_y
	getactor[THISACTOR].z enemy_z
	updatesectorz enemy_x enemy_y enemy_z RETURN
	ifvarn RETURN -1 { ifvarn sector[RETURN].lotag 2 fall }
		else fall //если случайно занесо под ступеньку, чтобы он не оставался под землей

	setvar maxSpeed 60
	setvar weight 1
	setvar width 128
	ifvare cult_duck 0  
		setvar height 8192
	else setvar height 4192
	setvar enemy_nohitscan_z 0
	setvar cantseetarget 0
	setvar fly_enemy 0
	
	ifvare enemyshoot 1 setvar enemyshoot 0
	getactor[THISACTOR].pal pal

	ifai NO { ifvarn settotalkill 1 { addvar TOTALKILLS 1 } ifvare sprite[THISACTOR].pal 12 strength HERETIC2STRENGTH else strength HERETICSTRENGTH cstat 257 setvarvar enemy_type STAYPUT setvarvar change_ai enemy_type }

	ifinwater {
		ifcanseetarget ifvarn target_id -1 {
			getactor[THISACTOR].z enemy_z
			getactor[target_id].z target_z
			setvarvar tmp target_z
			addvarvar tmp 512
			ifvarvarl tmp enemy_z {
				subvarvar enemy_z 400
			} else {
			
				setvarvar tmp target_z
				subvarvar tmp 512
				ifvarvarg tmp enemy_z 
					addvarvar enemy_z 400
			}
			setactor[THISACTOR].z enemy_z
		}
		
		getactor[THISACTOR].sectnum sectnum
		getactor[THISACTOR].z z
		ifvarvarg z sector[sectnum].floorz 
			setactor[THISACTOR].z sector[sectnum].floorz
		
		subvar z 8192
		ifvarvarl z sector[sectnum].ceilingz {
			setvarvar z sector[sectnum].ceilingz
			addvar z 8192
			setactor[THISACTOR].z z
		}
		
		
	}
	
	ifvare enemy_type STAYPUT  { 
		ifvare change_ai STAYPUT {
		
			ifinwater 
			ai AIHERETICWATERSTAYPUT 
			else {
				ifvare cult_duck 0 
					ai AIHERETICSTAYPUT
				else 
					ai AIHERETICDUCKSTAYPUT
			}
			setvar change_ai -1 
		}
		
		ifvare change_ai -1 {
			ifvare active 1 ifvare ai_state 0 {
				setvarvar target_id PLAYERID 
				randvar enemyroam 4
				ifvare enemyroam 0 soundonce CULTROM1 else
				ifvare enemyroam 1 soundonce CULTROM2 else
				ifvare enemyroam 2 soundonce CULTROM3 else
				ifvare enemyroam 3 soundonce CULTROM4 else
				ifvare enemyroam 4 soundonce CULTROM5
				setvar enemy_movtype EVASION
				setvar change_ai WALK
				setvar ai_state 1
			}
	
			ifp palive ifcansee {
				state facingstate 
				ifvare set 1 {
					setvarvar target_id PLAYERID 
					randvar enemyroam 4
					ifvare enemyroam 0 soundonce CULTROM1 else
					ifvare enemyroam 1 soundonce CULTROM2 else
					ifvare enemyroam 2 soundonce CULTROM3 else
					ifvare enemyroam 3 soundonce CULTROM4 else
					ifvare enemyroam 4 soundonce CULTROM5
					setvar enemy_movtype EVASION
					setvar change_ai WALK
				}
			}
		}
	}
	
	ifvarg sprite[THISACTOR].htextra 0 {
		getactor[THISACTOR].htextra tmp
		getuserdef[THISACTOR].player_skill pskill
		ifvare pskill 2 {
			mulvarvar tmp 2
			divvarvar tmp 3
		}
		ifvare pskill 3 {
			divvarvar tmp 2
		}
		ifvare pskill 4 {
			divvarvar tmp 3
		}
		ifvare pskill 5 {
			divvarvar tmp 4
		}
		ifvare tmp 0 setvar tmp 1
		setactor[THISACTOR].htextra tmp
		
		ifvare sprite[THISACTOR].picnum HERETIC 
		ifvare sprite[THISACTOR].pal 11 {
			ifvare sprite[THISACTOR].htpicnum ALTTESLA {
				setvar change_ai -1
				setvar teslahitenable 0
				setactor[THISACTOR].htextra -1
			}
			ifvare sprite[THISACTOR].htpicnum PLASMAPROJ {
				setvar change_ai -1
				setvar teslahitenable 0
				setactor[THISACTOR].htextra -1
				
			}
		}
	}

	state mainAI

	ifvarn target_id -1
	ifvare actorvar[target_id].reflective 1 
	ifvarn gethurt -1
	{
		getactor[THISACTOR].extra extra		
		state bloodspraystate 
		subvarvar extra gethurt
		setactor[THISACTOR].extra extra
		ifvarg extra 0 
			setvar enemy_type GETDAMAGE
		else 
			setvar enemy_type DYING

		setvarvar change_ai enemy_type
		setvar gethurt -1
	}
	
	ifvare sector[THISACTOR].lotag 1 {	
		setvar cult_duck 0
	}

	ifvare enemy_type 666 {
		ifaction AHERETICTRANSFORM { 
			ifactioncount 4 { 
				action AHERETICTRANSFORM2 
			}
		}
		ifaction AHERETICTRANSFORM2 {
			espawn BEAST
			setactor[RETURN].yvel sprite[THISACTOR].yvel
			setactor[RETURN].xvel sprite[THISACTOR].xvel
			getactor[RETURN].z z
			subvarvar z 8192
			setactor[RETURN].z z
			setactorvar[RETURN].beast_transform 1
			killit
		}
	}

	ifvare enemy_type WALK {

		ifvare change_ai WALK {
			ifinwater ai AIHERETICSWIM else {
				ifinwater ai AIHERETICSWIM else {
				ifvare cult_duck 1 ai AIHERETICDUCKSTEP else ai AIHERETICWALK } }
			
			setvar change_ai -1 
		}
		
		ifaction AHERETICWALK1 ifactioncount 3 action AHERETICWALK2
		ifaction AHERETICWALK2 ifactioncount 4 action AHERETICWALK1
		
		ifvarn target_id -1 ifvarg sprite[THISACTOR].extra 0
		ifcanseetarget ifvarg sprite[target_id].extra 0 { 
			ifvare enemyroam 0 soundonce CULTROM1 else
			ifvare enemyroam 1 soundonce CULTROM2 else
			ifvare enemyroam 2 soundonce CULTROM3 else
			ifvare enemyroam 3 soundonce CULTROM4 else
			ifvare enemyroam 4 soundonce CULTROM5
		}
		
		ifvarvarn enemy_hittid target_id {
			ifvarn enemy_hittid -1 {
				ifvare actorvar[enemy_hittid].obj_statnum 15 ifvarand actorvar[enemy_hittid].obj_zvel 1 ifvarand sprite[enemy_hittid].cstat 16 {
					ifvarg anglefield -128 ifvarl anglefield 128 ifcount 2 {
						ifvarn pal 13 {
							setvar enemy_type ATTACK
							setvar change_ai USERAI
						}
						resetcount
					}
				} else {
					ifvare enemy_strafe 1 { addvar COUNTER 1 ifvarg COUNTER 52 { ifinwater ai AIHERETICSWIM else { ifvare cult_duck 1 ai AIHERETICDUCKSTEP else ai AIHERETICWALK }  setvar COUNTER 0 setvar change_ai -1 setvar enemy_strafe 0 } }
					ifvarvarg targetdist evasion_len ifvarn enemy_strafe 1 { ifinwater ai AIHERETICSWIM else { ifvare cult_duck 1 ai AIHERETICDUCKSTEP else ai AIHERETICWALK }  ifrnd 128 setvar cult_tmp 512 else setvar cult_tmp -512 setvar enemy_strafe 1 setvar change_ai USERAI }
				}
			} else ifvare change_ai USERAI { ifinwater ai AIHERETICSWIM else { ifvare cult_duck 1 ai AIHERETICDUCKSTEP else ai AIHERETICWALK }  setvar enemy_strafe 0 setvar COUNTER 0 setvar change_ai -1 }
		} else {
			ifvare enemy_strafe 1 {
				addvar COUNTER 1 
				ifvarg COUNTER 26 { 
					ifinwater ai AIHERETICSWIM else { ifvare cult_duck 1 ai AIHERETICDUCKSTEP else ai AIHERETICWALK } 
					setvar enemy_strafe 0
					setvar COUNTER 0 
					setvar change_ai -1
				} 
			}

			ifvarn enemy_movtype EVASION  
			ifvarn enemy_movtype BLOCK {
			  ifvarg sprite[target_id].extra 0
			  ifvarn actorvar[target_id].invisible 1
			  ifvarg anglefield -128 ifvarl anglefield 128
				ifvarl targetdist 8192 ifcount 4 { //3192
					setvar enemy_strafe 0
					setvar enemy_type ATTACK
					setvar change_ai USERAI
					resetcount
				}
				
				ifvarvarg targetdist 8192 ifcount 60 { ifvare cult_duck 1 { setvar cult_duck 0 setvar change_ai WALK } }
			}
		}
		
		ifvare change_ai USERAI { 
			ifvare enemy_strafe 1 
				state cultist_step
		}	
	}
	
	

	ifvare enemy_type ATTACK {
	
		ifvare change_ai USERAI { 
			ifvare pal 13 {
				ifrnd 64 ifvarg targetdist 1024
					ai AIHERETICGRENADE 
				else {
					setvar enemy_type WALK
					setvar change_ai -1
				}
			} else {
				ifinwater { 
					ifvare pal 0
						ai AIHERETICSHOOTGUNDUCK else
					ifvare pal 12
						ai AIHERETICSHOOTGUNDUCK else
					ifvare pal 3 ai AIHERETICTOMMYSHOOTDUCK
					else ifvare pal 11 ai AIHERETICTOMMYSHOOTDUCK
				} else {
				ifvare cult_duck 1 {
					ifvare pal 0
					ai AIHERETICSHOOTGUNDUCK else
					ifvare pal 12
					ai AIHERETICSHOOTGUNDUCK else
					ifvare pal 3 ai AIHERETICTOMMYSHOOTDUCK
					else ifvare pal 11 ai AIHERETICTOMMYSHOOTDUCK
				}
				else
					ifvare pal 0
					ai AIHERETICSHOOTGUN else
					ifvare pal 12
					ai AIHERETICSHOOTGUN else
					ifvare pal 3 ai AIHERETICTOMMYSHOOT
					else ifvare pal 11 ai AIHERETICTOMMYSHOOT
				}
				ifvare pal 0 state hereticshootgun else
				ifvare pal 12 state hereticshootgun else
				ifvare pal 3 state heretictommyshoot else 
				ifvare pal 11 state hereticteslashoot
			}
			setvar change_ai ATTACK
		}
		
		ifvare change_ai ATTACK {

			ifai AIHERETICGRENADE {
				setvarvar DIST targetdist
				ifactioncount 5 
				{
					getuserdef[THISACTOR].player_skill pskill 
					ifvare pal 13 ifvarl pskill 3 setvar pskill 3
					ifvarg pskill 2
					{
						setvarvar se_z enemy_z
						subvarvar se_z target_z
						addvar se_z 4096

						divvarvar DIST 540
						shiftvarl DIST 23
						divvarvar DIST 120 ///velocity
						
						shiftvarr se_z 7
						subvarvar se_z 14500 //nSlope
											
						sound TNTTOSS
						ifvare pskill 3 
							setvarvar picnum ENEMYTNTBUNDLE
						else 
							setvarvar picnum TNTBUNDLE2

						soundonce CULTTOSS 
						setvarvar tmp2 DIST
						ifvarl targetdist 2500
							shiftvarr tmp2 9
						else
							shiftvarr tmp2 10
							
						setprojectile[picnum].vel tmp2
						mulscale tmp DIST se_z 14
						
						ifvarl targetdist 2048
							shiftvarr tmp 5
						else
							shiftvarr tmp 7
						ezshootvar tmp picnum 
						
						getactor[THISACTOR].z z
						subvarvar z 5333
						setactor[RETURN].z z
						
					
					
						setvar cult_duck 0
				
						
						
						/*
						sound TNTTOSS
						divvar DIST 5
						ifvare pskill 3 
							setvarvar picnum ENEMYTNTBUNDLE
						else 
							setvarvar picnum TNTBUNDLE2
							
						setprojectile[picnum].vel DIST
						getprojectile[picnum].drop tmp
						divvar DIST 2
						subvarvar tmp DIST
						setprojectile[picnum].drop tmp
						mulvar DIST -7
						soundonce CULTTOSS 
						ezshootvar DIST picnum 
						setprojectile[picnum].drop -512
						*/
					}
					setvar change_ai -1
					resetactioncount 
				}
			} else {
				ifvare pal 13 {
					setvar enemy_type WALK
					setvar change_ai -1
				} else {
					ifinwater { 
						ifvare pal 0 { ifai AIHERETICSHOOTGUNDUCK nullop else ai AIHERETICSHOOTGUNDUCK } else 
						ifvare pal 12 { ifai AIHERETICSHOOTGUNDUCK nullop else ai AIHERETICSHOOTGUNDUCK } else 
						ifvare pal 3 { ifai AIHERETICTOMMYSHOOTDUCK nullop else ai AIHERETICTOMMYSHOOTDUCK } else
						ifvare pal 11 { ifai AIHERETICTOMMYSHOOTDUCK nullop else ai AIHERETICTOMMYSHOOTDUCK }
					} else {
						ifvare cult_duck 1 { 
							ifvare pal 0 { ifai AIHERETICSHOOTGUNDUCK nullop else ai AIHERETICSHOOTGUNDUCK } else 
							ifvare pal 12 { ifai AIHERETICSHOOTGUNDUCK nullop else ai AIHERETICSHOOTGUNDUCK } else 
							ifvare pal 3 { ifai AIHERETICTOMMYSHOOTDUCK nullop else ai AIHERETICTOMMYSHOOTDUCK } else
							ifvare pal 11 { ifai AIHERETICTOMMYSHOOTDUCK nullop else ai AIHERETICTOMMYSHOOTDUCK } 
						} else
						{ 
							ifvare pal 0 { ifai AIHERETICSHOOTGUN nullop else ai AIHERETICSHOOTGUN } else 
							ifvare pal 12 { ifai AIHERETICSHOOTGUN nullop else ai AIHERETICSHOOTGUN } else 
							ifvare pal 3 { ifai AIHERETICTOMMYSHOOT nullop else ai AIHERETICTOMMYSHOOT } else
							ifvare pal 11 { ifai AIHERETICTOMMYSHOOT nullop else ai AIHERETICTOMMYSHOOT } 
						} 
					}
				
					ifactioncount 2 { 
						ifvare pal 0 state hereticshootgun else
						ifvare pal 12 state hereticshootgun else
						ifvare pal 3 state heretictommyshoot
						else ifvare pal 11 state hereticteslashoot
						resetactioncount 
					}
			
					ifvare enemyshoot 1 {
						setactor[THISACTOR].ang enemy_hita
						ifvarg targetdist 4096
						ifvarl targetdist 12288 {
							randvar tmp 4
							ifinwater nullop else //чтобы под водой не кидал гранаты
								ifvare tmp 1 ifvarn cult_duck 1 ifvarn pal 3 ifvarn pal 12 ifvarn pal 11 { getuserdef[THISACTOR].player_skill pskill ifvarg pskill 2 ai AIHERETICGRENADE } 
						} 
			
						ifvarg targetdist 8192 
							setvar change_ai -1
						
						ifvarvarn enemy_hittid target_id {
							addvar shoot_var 1
							ifvarn enemy_strafe 1 ifvarg shoot_var 1 {
								ifinwater ai AIHERETICSWIM else { ifvare cult_duck 1 ai AIHERETICDUCKSTEP else ai AIHERETICWALK } 
									
								ifrnd 128 setvar cult_tmp 512 
									else setvar cult_tmp -512
								setvar enemy_type WALK
								setvar enemy_strafe 1
								setvar shoot_var 0
								setvar change_ai USERAI
							}
						} else setvar shoot_var 0
						
						getactor[target_id].extra extra
						ifvarl extra 1 { 
							setvar cult_duck 0
						}
					}
				}
				
			}
		}
	}
	
	ifvare enemy_type GETDAMAGE {
		ifvare change_ai GETDAMAGE {

			stopsound CULTROM1
			stopsound CULTROM2
			stopsound CULTROM3
			stopsound CULTROM4
			stopsound CULTROM5
			ifrnd 128 soundonce CULTPAN1 
				else soundonce CULTPAN2
				
			ifvarn teslahitenable 1 {
				ai AIHERETICHURT
			} else { ifvarn pal 12
					ai AIHERETICTESLA
						else ai AIHERETICHURT
				}
			setvar change_ai USERAI
		}
		
		ifvare change_ai USERAI {
			ifai AIHERETICHURT 
			ifcount 5 {
				ifvarn target_id -1 
				ifvarn actorvar[target_id].invisible 1 {
					ifrnd 128 setvar cult_tmp 512 
						else setvar cult_tmp -512
					setvar enemy_strafe 1	
					setvar enemy_movtype EVASION 
					setvar change_ai USERAI 
				} else setvar change_ai -1
				
				ifvarn pal 12 ifrnd 64 setvar cult_duck 1 else setvar cult_duck 0
				ifinwater ai AIHERETICSWIM else {
				ifvare cult_duck 1 ai AIHERETICDUCKSTEP else ai AIHERETICWALK } 
				
				setvar enemy_type WALK
			}
			ifai AIHERETICTESLA { setvar enemy_tesla 1 ifcount 30 { ifinwater ai AIHERETICSWIM else {
				ifvare cult_duck 1 ai AIHERETICDUCKSTEP else ai AIHERETICWALK } setvar enemy_tesla 0 setvar teslahitenable 0 setvar change_ai -1 }  }			
		}		
	}
	
	ifvare enemy_type GETHEAVYDAMAGE {
		ifvare change_ai GETHEAVYDAMAGE {
			ifvarn pal 12 {
				state enemyjibs_state 
				ai AIHERETICGUTS
				ifrnd 64 { soundonce CULTDIE1 } 
				else ifrnd 64 { soundonce CULTDIE2 } else { soundonce CULTDIE3 }
				setvar burningguyactive 0
				setvar COUNTER 0
				setactor[THISACTOR].extra -100
				setvar change_ai USERAI
			} else state transform
		}
		
		ifvare change_ai USERAI {
			ifai AIHERETICGUTS { ifactioncount 6 { cstat 0 ai AIHERETICDEAD2 setvar enemy_type DEAD setvar change_ai DEAD } }
		}
	}
	
	ifvare enemy_type DYING {
		ifvare change_ai DYING {
			ifvarn pal 12 {
			cstat 0
			state bloodsprayshit 
			ifrnd 64 { soundonce CULTDIE1 } 
			else ifrnd 64 { soundonce CULTDIE2 } else { soundonce CULTDIE3 }
			stopsound CULTROM1
			stopsound CULTROM2
			stopsound CULTROM3
			stopsound CULTROM4
			stopsound CULTROM5
			ai AIHERETICDYING 
			state randomspawn
			setvar change_ai USERAI
			} else state transform
		}
		
		ifvare change_ai USERAI {
			ifai AIHERETICDYING ifactioncount 7 { ai AIHERETICDEAD setvar change_ai -1 }
		}
	}
	
	ifvare enemy_type BURNING {
		ifvarn sprite[THISACTOR].pal 12 {
			espawn BURNINGGUY 
			setactor[THISACTOR].extra 0 //чтобы привязанный ключ смог переопределить спрайт горящего чувака
			setactor[RETURN].xrepeat 22
			setactor[RETURN].yrepeat 22
			setactorvar[RETURN].bguy_id 2
			setactorvar[RETURN].se_xvel se_xvel
			state enemykill_trigger
			addkills 1 
			killit 
		} else {
			state transform
		}
	}
	
	ifvare enemy_type VOODOO {
		ifvare change_ai VOODOO {
			ifvarn sprite[THISACTOR].pal 12 {
			cstat 0
			state bloodsprayshit 
			ifrnd 64 { soundonce CULTDIE1 } 
			else ifrnd 64 { soundonce CULTDIE2 } else { soundonce CULTDIE3 }
			ai AIHERETICDYING 
			state randomspawn
			setvar change_ai USERAI
			} else state transform
		}
		
		ifvare change_ai USERAI {
			ifai AIHERETICDYING ifactioncount 7 { ai AIHERETICDEAD setvar change_ai -1 }
		}
	}
	
	ifvare enemy_type DEAD {
		ifai AIHERETICDEAD ifcount 56 ai AIHERETICDEADD
		ifai AIHERETICDEADD ifcount 30 ai AIHERETICDEADDD
		ifai AIHERETICDEADDD ifcount 156 ai AIHERETICDEAD4
		ifai AIHERETICDEAD4 ifactioncount 2 { ai AIHERETICDEAD5 setactor[THISACTOR].yvel 0  setvar se_yvel 0 }
		ifai AIHERETICDEAD5 ifactioncount 4 killit
	}
	
	ifvarl sprite[THISACTOR].extra 1 
		fall
	
enda