// aiblood.con

gamevar ai_naffected -1 2
gamevar ai_switchaction 0 2
gamevar ain1 0 0
gamevar ain2 0 0
gamevar ain3 0 0
gamevar ain4 0 0
gamevar ain5 0 0
gamevar ain6 0 0

gamevar sa1 0 0
gamevar sa2 0 0
gamevar sa3 0 0
gamevar sa4 0 0
gamevar sa5 0 0
gamevar sa6 0 0

gamevar sprXVel 0 2
gamevar sprYVel 0 2
gamevar sprZVel 0 2
gamevar sprFlags 0 2

define kAttrMove 1
define kAttrGravity 2
define kAttrFalling 4

define kFrameTicks 4

state saveregisters
	setvarvar sa1 ain1
	setvarvar sa2 ain2
	setvarvar sa3 ain3
	setvarvar sa4 ain4
	setvarvar sa5 ain5
	setvarvar sa6 ain6
ends

state loadregisters
	setvarvar ain1 sa1
	setvarvar ain2 sa2
	setvarvar ain3 sa3
	setvarvar ain4 sa4
	setvarvar ain5 sa5
	setvarvar ain6 sa6
ends

include blcons/blgameutils.con

gamevar floordist 0 0
gamevar ceildist 0 0
gamevar movehit 0 0
gamevar nsector 0 0
gamevar ceilhit 0 0
gamevar floorhit 0 0
gamevar ceilz 0 0
gamevar floorz 0 0

state MoveDude

	state GetSpriteExtents
	
	setvarvar floordist zbot
	subvarvar floordist sprite[THISACTOR].z
	divvarvar floordist 5
	setvarvar ceildist sprite[THISACTOR].z
	subvarvar ceildist ztop
	divvarvar ceildist 5
	setvarvar cm_clipdist sprite[THISACTOR].clipdist
	shiftvarl cm_clipdist 2

	ifvare sprXVel 0 ifvare sprYVel 0 {
		setvarvar ain1 sprite[THISACTOR].x
		setvarvar ain2 sprite[THISACTOR].y
		setvarvar ain3 sprite[THISACTOR].z
		setvarvar ain4 sprite[THISACTOR].sectnum
		state FindSector
		
		ifvare RSECTOR -1 
			updatesector ain1 ain2 RSECTOR
		setvarvar nsector RSECTOR
	} else {
	
		setvarvar xvel sprXVel
		shiftvarr xvel 12
		shiftvarl xvel 14
		setvarvar yvel sprYVel
		shiftvarr yvel 12
		shiftvarl yvel 14

		getactor[THISACTOR].x x
		getactor[THISACTOR].y y
		getactor[THISACTOR].z z
		getactor[THISACTOR].sectnum nsector

		clipmove movehit x y z nsector xvel yvel cm_clipdist floordist ceildist CLIPMASK0 
		
		ifvare nsector -1 {
			setvarvar nsector sprite[THISACTOR].sectnum
			//actDamageSprite(nSprite, pSprite, 0, 16000); XXX
		} else {
			setactor[THISACTOR].x x
			setactor[THISACTOR].y y
			setactor[THISACTOR].z z
		}
		
		//if ( sector[nSector].type >= 612 && sector[nSector].type <= 617 ) pushmove XXX
		
		/*
		setvarvar tmp movehit
		andvar tmp 57344
		ifvare tmp 32768 {
			
			//reflectVector
		
			setvarvar wallid movehit
			andvar wallid 0x1FFF
			// get wall vector
			// wall[wall[j].point2].x-wall[j].x
			// wall[wall[j].point2].y-wall[j].y
			getwall[wallid].x phys_core_tmp2
			getwall[wallid].y phys_core_tmp3
			getwall[wallid].point2 wallid
			getwall[wallid].x phys_core_tmp4
			getwall[wallid].y phys_core_tmp5
			subvarvar phys_core_tmp4 phys_core_tmp2
			subvarvar phys_core_tmp5 phys_core_tmp3
			// ( phys_core_tmp4,phys_core_tmp5 ) - wall vector
			// get reflect angle
			// s->ang = ((k<<1) - s->ang)&2047;
			getangle phys_core_tmp2 phys_core_tmp4 phys_core_tmp5
			getangle phys_core_tmp3 sprXVel sprYVel
			shiftvarl phys_core_tmp2 1
			subvarvar phys_core_tmp2 phys_core_tmp3
			andvar phys_core_tmp2 2047
			//setactor[THISACTOR].ang phys_core_tmp2
			
			// calc vector length
			setvarvar in1 sprXVel
			setvarvar in2 sprYVel
			state vec2d_lenght
			
			// calc reflect vector
			// phys_out_1 vector lendth
			cos sprXVel phys_core_tmp2
			mulvarvar sprXVel out1 
			divvar sprXVel TRIG_MAX 
			
			sin sprYVel phys_core_tmp2
			mulvarvar sprYVel out1
			divvar sprYVel TRIG_MAX	
			
			addlogvar sprXVel
		}
		*/
	}
	
	ifvarn nsector -1 ifvarvarn nsector sprite[THISACTOR].sectnum {
		changespritesect THISACTOR nsector
		setvarvar nsector sprite[THISACTOR].sectnum
	}

	ifvarn sprZVel 0 {
		setvarvar tmp sprZVel
		shiftvarr tmp 8
		getactor[THISACTOR].z z
		addvarvar z tmp
		setactor[THISACTOR].z z
	}
	
	ifvarn nsector -1 getzrange sprite[THISACTOR].x sprite[THISACTOR].y sprite[THISACTOR].z nsector ceilz ceilhit floorz floorhit cm_clipdist CLIPMASK0
	
	setvarvar tmp floorhit
	andvar tmp 0xE000
	ifvare tmp 16384 {
		setvarvar sectnum floorhit
		andvar sectnum 0x1FFF
		ifvarand sector[sectnum].floorstat 1 
			setvar floorz 0x7FFFFFFF;
		ifvare sector[sectnum].lotag 1  {
			addvar floorz 5120 //underwater depth << 10	
		}
	}
	
	state GetSpriteExtents

	ifvarand sprFlags kAttrGravity {
		/*
		ifonwater { //depth - код рабочий, но tsprite[].picnum не обновляется, если монстр упал под воду и не рисуется
			getflorzofslope sprite[THISACTOR].sectnum sprite[THISACTOR].x sprite[THISACTOR].y sector_z1
			ifvarvarl sector_z1 zbot {
				setvarvar tmp zbot
				subvarvar tmp sector_z1
				mulvarvar tmp -80099
				setvarvar tmp2 zbot
				subvarvar tmp2 ztop
				divvarvar tmp tmp2
				addvarvar tmp 58254
	
				ifvarn tmp 0 {
					addvarvar sprZVel tmp
					shiftvarr tmp 7
					getactor[THISACTOR].z z
					addvarvar z tmp
					setactor[THISACTOR].z z
				}
			}
		} 
		*/
		ifonwater ifvarg sprite[THISACTOR].extra 0 { //depth
			getflorzofslope sprite[THISACTOR].sectnum sprite[THISACTOR].x sprite[THISACTOR].y sector_z1
			ifvarvarg z sector_z1 {
				ifrnd 128 {
					setvar tmp -58254
					addvarvar sprZVel tmp
					shiftvarr tmp 7
					getactor[THISACTOR].z z
					addvarvar z tmp
					setactor[THISACTOR].z z
				}
			} else
			ifvarvarl zbot floorz ifrnd 128 {
				addvarvar sprZVel 58254
				getactor[THISACTOR].z z
				addvarvar z 455
				setactor[THISACTOR].z z
			}	
		} 
		else
		ifvarvarl zbot floorz {
			addvarvar sprZVel 58254
			getactor[THISACTOR].z z
			addvarvar z 455
			setactor[THISACTOR].z z
		}
	}
	
	state GetSpriteExtents

	ifvarvarg floorz zbot {
		setvarvar movehit 0
		ifvarand sprFlags kAttrGravity
			orvar sprFlags kAttrFalling
	} else { 
		setvarvar movehit floorhit
		setvarvar tmp2 floorz
		subvarvar tmp2 zbot
		addvarvar tmp2 sprite[THISACTOR].z
		setactor[THISACTOR].z tmp2
		
		ifvarg sprZVel 0 {
			mulscale RETURN 65536 sprZVel 16
			subvarvar sprZVel RETURN
			mulvar sprZVel -1
			mulscale tmp RETURN RETURN 30

			ifvarg tmp 200 ifvarl sprite[THISACTOR].extra 1 {
				setactor[THISACTOR].htextra 1000
			}
			
			subvar tmp 1600
			ifvarg tmp 0 {
				setactor[THISACTOR].htextra tmp
			}
			setvarvar tmp sprZVel
			ifvarl tmp 0
				mulvar tmp -1
			ifvarg tmp 65536 {
				xorvar sprFlags kAttrFalling
			}	
		}
	}
	
	ifvarvarl ceilz ztop {
		setvarvar movehit 0 
	} else {
		setvarvar movehit ceilhit
		
		subvarvar ceilz ztop
		ifvarg ceilz 0 {
			addvarvar ceilz sprite[THISACTOR].z
			setactor[THISACTOR].z ceilz
		}
		
		ifvarl sprZVel 0 ifvarand sprFlags kAttrFalling {
			mulvar sprZVel -1
			shiftvarr sprZVel 3
		}
	}
	
	state GetSpriteExtents
	
	setvarvar tmp floorz
	subvarvar tmp zbot
	ifvarg tmp 0 {
		shiftvarr tmp 8
	} else setvar tmp 0

	// drag and friction
	ifvare sprXVel 0 ifvare sprYVel 0 nullop else {
		ifvarl tmp 256 {
			setvar tmp2 10752
			ifvarn tmp 0 {
				mulscale RETURN tmp2 tmp 8
				subvarvar tmp2 RETURN
			}

			mulscale RETURN tmp2 sprXVel 16
			subvarvar sprXVel RETURN
			
			mulscale RETURN tmp2 sprYVel 16
			subvarvar sprYVel RETURN

			setvarvar ain1 sprXVel
			setvarvar ain2 sprYVel
			ifvarl ain1 0 mulvar ain1 -1
			ifvarl ain2 0 mulvar ain2 -1
			ifvarvarg ain1 ain2 {
				mulvar ain2 3
				shiftvarr ain2 3
			} else {
				mulvar ain1 3
				shiftvarr ain1 3
			}
			
			addvarvar ain1 ain2

			ifvarl ain1 4096 {
				setvar sprXVel 0
				setvar sprYVel 0
			}
		}
	}
	
	getactor[THISACTOR].cstat tmp
	andvar tmp 1
	ifvarg sprite[THISACTOR].extra 0 ifvare tmp 1 {
		setvarvar cm_clipdist sprite[THISACTOR].clipdist
		shiftvarl cm_clipdist 4
		findnearspritevar APLAYER cm_clipdist spriteid
		ifvarn spriteid -1 ifvarvarg sprite[spriteid].z ztop {
			setvarvar in1 sprite[THISACTOR].x
			setvarvar in2 sprite[THISACTOR].y
			subvarvar in1 sprite[spriteid].x
			subvarvar in2 sprite[spriteid].y
			
			ldist len THISACTOR spriteid

			setvar penV -1
			setvarvar nx in1
			setvarvar ny in2
			mulvar nx 100
			mulvar ny 100
			ifvarvarn len 0 {
				divvarvar nx len
				divvarvar ny len
			} else {
				setvar nx 0
				setvar ny 0
			}
			setvarvar tmp width
			mulvar tmp 2
			setvarvar penV tmp
			subvarvar penV len
			ifvarvarg penV 0 {
				setvarvar ejx penV
				mulvarvar ejx nx
				divvar ejx 100
				setvarvar ejy penV
				mulvarvar ejy ny
				divvar ejy 100
				getplayer[THISACTOR].posx x2
				getplayer[THISACTOR].posy y2
				subvarvar x2 ejx
				subvarvar y2 ejy
				updatesectorz x2 y2 sprite[spriteid].z RETURN
				
				ifvarn RETURN -1 ifvare phitobj 0 {
					setplayer[THISACTOR].posx x2
					setplayer[THISACTOR].posy y2
				}
			}
		}
	}
	
	//ProcessTouchObjects( nSprite, nXSprite ); XXX
ends

state AirDrag

		mulscale RETURN 128, -sprXVel, 16
		addvarvar sprXVel RETURN
		
		mulscale RETURN 128, -sprYVel, 16
		addvarvar sprYVel RETURN

		mulscale RETURN 128, sprZVel, 16
		subvarvar sprZVel RETURN

ends

gamevar aimdx 0 1
gamevar aimdy 0 1
gamevar aimdz 0 1

state WeaponHitProcess

	getplayer[THISACTOR].ang pang
	cos aimdx pang
	sin aimdy pang
	
	getplayer[THISACTOR].horiz aimdz
	subvar aimdz 100
	mulvar aimdz -128
	
ends

// enemy ai code

define IDLE 0
define CHASE 1
define SEARCH 2
define GOTO 3
define HACK 4
define RECOIL 5
define PONDER 6

gamevar ai_goalang 0 2
gamevar ai_target 0 2
gamevar ai_targetx 0 2
gamevar ai_targety 0 2
gamevar ai_targetz 0 2
gamevar ai_angspeed 0 2
gamevar ai_speed 0 2
gamevar ai_state 0 2
gamevar ai_aistate 0 2
gamevar ai_aistate_old 0 2
gamevar ai_animation_old 0 2
gamevar ai_alertchance 0 2
gamevar ai_seedist 0 2
gamevar ai_heardist 0 2
gamevar ai_eyeheight 0 2
gamevar ai_periphery 0 2
gamevar ai_statetimer 0 2
gamevar ai_mass 0 2
gamevar ai_next -1 2
gamevar ai_tmp1 0 0
gamevar ai_recoil 0 2
gamevar ai_dead 0 2
gamevar ai_thinktick 0 2
gamevar ai_losang 0 2
gamevar ai_nangle 0 2
gamevar ai_canmove 0 2
gamevar ai_debdist 0 2

state ApplyWeaponForce
	getactor [THISACTOR].htextra htextra
	setvarvar tmp htextra //shotgun
	shiftvarl tmp 22
	divvarvar tmp ai_mass //force

	mulscale RETURN tmp aimdx 16
	addvarvar sprXVel RETURN	
	mulscale RETURN tmp aimdy 16
	addvarvar sprYVel RETURN
	mulscale RETURN tmp aimdz 16
	addvarvar sprZVel RETURN
ends

//ain1 - ang
state aiChooseDirection

	setvarvar mang 1024
	addvarvar mang ain1
	subvarvar mang sprite[THISACTOR].ang
	andvar mang 2047
	subvarvar mang 1024	//dang

	cos mycos sprite[THISACTOR].ang
	sin mysin sprite[THISACTOR].ang

	// find vel relative to current angle
	mulscale tmp2 sprXVel mycos 14
	mulscale tmp3 sprYVel mysin 14
	addvarvar tmp2 tmp3 //vel

	mulvar tmp2 15
	shiftvarr tmp2 13
	setvarvar tmp3 tmp2
	shiftvarr tmp3 30
	subvarvar tmp2 tmp3 //avoidDist
	
	mulvar tmp2 2
	//divvar tmp2 2
	setvarvar ai_debdist tmp2

	setvarvar ain1 THISACTOR
	setvarvar ain2 ai_target
	setvarvar ain4 tmp2
	
	setvarvar ain3 mang
	addvarvar ain3 sprite[THISACTOR].ang
	state saveregisters
	// clear movement toward target?
	state CanMove	
	state loadregisters	
	setvarvar ai_canmove RETURN //debug
	
	ifvare RETURN 0 {
		setvarvar ain3 mang
		divvar ain3 2
		addvarvar ain3 sprite[THISACTOR].ang
		state saveregisters
		// clear movement partially toward target?
		state CanMove	
		state loadregisters	

		ifvare RETURN 0 {
			setvarvar ain3 mang
			divvar ain3 2
			setvarvar tmp2 sprite[THISACTOR].ang
			subvarvar tmp2 ain3
			setvarvar ain3 tmp2

			state saveregisters
			// clear movement partially toward target?
			state CanMove			
			state loadregisters	
			ifvare RETURN 0 {
				setvar tmp3 341 //turnTo
				ifvarl mang 0 mulvar tmp3 -1
		
				setvarvar ain3 tmp3
				addvarvar ain3 sprite[THISACTOR].ang
				state saveregisters
				// try turning in target direction
				state CanMove	
				state loadregisters

				ifvare RETURN 0 {
					setvarvar ain3 sprite[THISACTOR].ang
					state saveregisters
					// clear movement straight?
					state CanMove	
					state loadregisters

					ifvare RETURN 0 {
						setvarvar ain3 sprite[THISACTOR].ang
						setvar tmp3 341 //turnTo
						ifvarl mang 0 mulvar tmp3 -1
						subvarvar ain3 tmp3
						state saveregisters
						// try turning away
						state CanMove	
						state loadregisters

						ifvare RETURN 0 {
							setvarvar ain3 sprite[THISACTOR].ang
							addvar ain3 341
						}
					}
				}
			}
		}
	}
	setvarvar ai_goalang ain3
	andvar ai_goalang 2047

	//dodge direction

ends

state aiMoveTurn

	setvarvar tmp ai_angspeed
	mulvarvar tmp kFrameTicks
	shiftvarr tmp 4 //maxturn

	setvarvar mang 1024
	addvarvar mang ai_goalang
	subvarvar mang sprite[THISACTOR].ang
	andvar mang 2047
	subvarvar mang 1024	//dang
	
	ifvarvarg mang tmp
		setvarvar mang tmp	
	setvarvar tmp2 tmp
	mulvar tmp2 -1	//-maxturn
	ifvarvarl mang tmp2
		setvarvar mang tmp2	
		
	getactor[THISACTOR].ang ang
	addvarvar ang mang
	andvar ang 2047
	setactor[THISACTOR].ang ang

ends

state aiMoveForward

	setvarvar tmp ai_angspeed
	mulvarvar tmp kFrameTicks
	shiftvarr tmp 4 //maxturn

	setvarvar mang 1024
	addvarvar mang ai_goalang
	subvarvar mang sprite[THISACTOR].ang
	andvar mang 2047
	subvarvar mang 1024	//dang
	setvarvar tmp3 mang

	ifvarvarg mang tmp
		setvarvar mang tmp	
	setvarvar tmp2 tmp
	mulvar tmp2 -1	//-maxturn
	ifvarvarl mang tmp2
		setvarvar mang tmp2	
		
	getactor[THISACTOR].ang ang
	addvarvar ang mang
	andvar ang 2047
	setactor[THISACTOR].ang ang

	ifvarl tmp3 0
		mulvar tmp3 -1

	ifvarl tmp3 342 {
		cos mycos sprite[THISACTOR].ang
		sin mysin sprite[THISACTOR].ang
		
		mulscale RETURN mycos ai_speed 14
		addvarvar sprXVel RETURN
		mulscale RETURN mysin ai_speed 14
		addvarvar sprYVel RETURN
	}
	
ends

state aiActivateDude
	
	ifvare ai_state 0 {
		// this doesn't take into account sprites triggered w/o a target location....
		setvarvar tmp ai_targetx
		subvarvar tmp sprite[THISACTOR].x
		setvarvar tmp2 ai_targety
		subvarvar tmp2 sprite[THISACTOR].y
		getangle ain1 tmp tmp2
		
		state aiChooseDirection
		setvar ai_state 1
	}
	
	ifvare ai_target -1 {
		setvarvar ai_aistate_old ai_aistate
		setvar ai_aistate SEARCH
		setvar ai_switchaction 1
	} else {	
		setvarvar ai_aistate_old ai_aistate
		setvar ai_aistate CHASE
		setvar ai_switchaction 1
		ifvare sprite[THISACTOR].picnum BEAST {
			ifrnd 96 soundonce BEAST1 else ifrnd 96 soundonce BEAST2 else soundonce BEAST3
		}
		ifvare sprite[THISACTOR].picnum ZOMBIE {
			ifrnd 96 soundonce AZOMMOAN else ifrnd 96 soundonce AZOMROAM else soundonce AZOMSPOT
		}
		ifvare sprite[THISACTOR].picnum CIVILIAN {
			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
		}
		ifvare sprite[THISACTOR].picnum TCHERNOBOG {
			ifrnd 36 sound TBBOW1 else 
			ifrnd 36 sound TBBOW2 else 
			ifrnd 36 sound TBKNEEL1 else 
			ifrnd 36 sound TBKNEEL2 else 
			ifrnd 36 sound TBKNEEL3 else 
			ifrnd 36 sound TBKNEEL4 else 
			sound TBWAIT1
		}
	}
	
ends

state aiThinkTarget

	setvar RETURN 0
	ifrnd 128
		setvar RETURN 1
		
	ifvare RETURN 0 {
		// skip this player if the he owns the dude or is invisible
		ifvarvarn THISACTOR PLAYERID ifvarg sprite[PLAYERID].extra 0 ifvarn actorvar[PLAYERID].invisible 1 {
			getplayer[THISACTOR].posx x
			getplayer[THISACTOR].posy y
			getplayer[THISACTOR].posz z
			getplayer[THISACTOR].cursectnum sectnum
			
			
			setvarvar se_x sprite[PLAYERID].x
			subvarvar se_x sprite[THISACTOR].x
			setvarvar se_y sprite[PLAYERID].y
			subvarvar se_y sprite[THISACTOR].y
			
			mulvarvar se_x se_x
			mulvarvar se_y se_y
			addvarvar se_x se_y
			sqrt se_x DIST
		
			ifvarvarl DIST ai_seedist {
				setvarvar tmp ai_eyeheight
				setvarvar tmp2 sprite[THISACTOR].yrepeat
				shiftvarl tmp2 2
				mulvarvar tmp tmp2
				
				// is there a line of sight to the player?
				setvarvar tmp2 sprite[THISACTOR].z
				subvarvar tmp2 tmp
				ifvarn sectnum -1 cansee x y z sectnum sprite[THISACTOR].x sprite[THISACTOR].y tmp2 sprite[THISACTOR].sectnum see
				
				ifvare see 1 {
					setvarvar x2 x
					subvarvar x2 sprite[THISACTOR].x
					setvarvar y2 y
					subvarvar y2 sprite[THISACTOR].y
			
					getangle mang x2 y2
					
					setvarvar ang 1024
					addvarvar ang mang
					subvarvar ang sprite[THISACTOR].ang
					andvar ang 2047
					subvarvar ang 1024
					
					ifvarl ang 0
						mulvar ang -1
					
					ifvarvarl ang 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
						state aiActivateDude
						setvar RETURN 1
					}
				}
			} 
			
			ifvare RETURN 0 {
				ifvarvarl DIST ai_heardist {
					setvarvar tmp ai_eyeheight
					setvarvar tmp2 sprite[THISACTOR].yrepeat
					shiftvarl tmp2 2
					mulvarvar tmp tmp2
					
					// is there a line of sight to the player?
					setvarvar tmp2 sprite[THISACTOR].z
					subvarvar tmp2 tmp
					ifvarn sectnum -1 cansee x y z sectnum sprite[THISACTOR].x sprite[THISACTOR].y tmp2 sprite[THISACTOR].sectnum see

					ifvare see 1 {
						setvarvar ai_target -1
						setvarvar ai_targetx x
						setvarvar ai_targety y
						setvarvar ai_targetz z
		
						state aiActivateDude
					}
				}
			}
		}
	}
			
ends

/*
	ain1 x
	ain2 y
	ain3 z
	ain4 damage
*/

state ConcussSprite

	getactor[THISACTOR].x x
	subvarvar x ain1
	setvarvar ain1 x
	getactor[THISACTOR].y y
	subvarvar y ain2
	setvarvar ain2 y
	getactor[THISACTOR].z z
	subvarvar z ain3
	setvarvar ain3 z
	shiftvarr ain3 4

	setvarvar DIST ain1
	mulvarvar DIST DIST

	setvarvar tmp2 ain2
	mulvarvar tmp2 tmp2
	addvarvar DIST tmp2

	setvarvar tmp2 ain3
	mulvarvar tmp2 tmp2
	addvarvar DIST tmp2
	addvarvar DIST 262144
	
	getactor[THISACTOR].picnum picnum
	setvarvar ain5 tiledata[picnum].xsize
	mulvarvar ain5 sprite[THISACTOR].xrepeat
	mulvarvar ain5 tiledata[picnum].ysize
	mulvarvar ain5 sprite[THISACTOR].yrepeat
	shiftvarr ain5 1 //area

	setvarvar ain6 262144
	mulvarvar ain6 ain4
	divvarvar ain6 DIST //force

	ifvarand sprFlags kAttrMove ifvarn ai_mass 0 {
	
		setvarvar tmp ain6
		divvarvar tmp ai_mass
		mulvarvar tmp ain5 //impulse

		mulscale ain1 tmp ain1 16
		mulscale ain2 tmp ain2 16
		mulscale ain3 tmp ain3 16
		
		addvarvar sprXVel ain1
		addvarvar sprYVel ain2
		addvarvar sprZVel ain3
	}

ends
