**CAUTION: SPOOKY UNTESTED TUNNEL FLY CODE

**Spawn twenty tunnel flies in the gomato room.
inst
reps 20
	new: simp 2 13 1 "tunnelfly" 5 0 rand 500 1000
	attr 198
	bhvr 57
**Give it lots of energy and make it live longer just in case the gomatoes aren't ready.
	setv ov01 600
	setv ov00 -600
	rnge 300
	anim [0 1 255]
	tick rand 15 25
	aero 10
	accg 0
	clac 3
	mvsf rand 2247 3730 rand 1616 1695
repe


**ov00: Age.
**ov01: Energy.
**ov02: Whether it's a larva.
**ov03: The gomato it's attached to if it's a larva.
**ov04: Larva X position.
**ov05: Larva Y position.
**ov06: Whether it's dead.

**Tick script: Do flying bug stuff.
scrp 2 13 1 9
	doif ov06 ne 1
**If it's a larva and the gomato it's attached to is gone, mature.
		inst
		doif ov02 eq 1
			doif ov03 eq null
**If it's in the room system, try to mature.
				anim [0 1 255]
				attr 198
				doif tmvt ov04 ov05 eq 1
**If it's in water, literally just die.
					doif rtyp grap posx posy eq 8 or rtyp grap posx posy eq 9
						kill ownr
**Else mature.
					else
						mvsf ov04 ov05
						setv ov02 0
						rnge 300
						aero 10
						accg 0
						setv ov01 90
						clac 3
						bhvr 57
					endi
**Else be killed.
				else
					kill ownr
				endi
			else
**Save its location so it doesn't teleport upon maturing.
				setv ov04 posl
				setv ov05 post
			endi
		else
			slow
**Make noises.
			setv va00 rand 0 49
			doif va00 eq 0
				snde "beez"
			endi
			addv ov00 1
			subv ov01 1
			tick rand 15 25
**If energy is zero or age is 300, die.
			doif ov01 le 0 or ov00 ge 300
				inst
				setv ov06 1
				tick rand 1500 2000
				pose 2
				accg 2
				elas 0

				stop
			endi
**If it's not hungry, just wander.
			doif ov01 gt 30
				gsub wander
**Else look for gomato detritus.
			else
				seta va00 null
**See if it's touching it.
				ttar 2 10 1
				doif targ ne null
					seta va00 targ
					setv va01 1
					targ ownr
**If it's not touching it, look around and fly towards it.
				else
					targ ownr
					star 2 10 1
					doif targ ne null
						seta va00 targ
						targ ownr
**If it can't see anything, wander helplessly. :(
					else
						gsub wander
						stop
					endi
				endi
**If it's touching gomato detritus, eat it, and if there's a reasonable number of living tunnel flies nearby, poop a larva on it.
				inst
				doif va00 ne null
					doif va01 eq 1
						esee 2 13 1
							doif ov06 ne 1
								addv va01 1
							endi
						next
						doif va01 le 10
							targ va00
							new: simp 2 13 1 "tunnelfly" 5 0 1000
							setv ov02 1
							frat 2
							attr 48
							seta ov03 va00
							anim [3 4 255]
							seta va03 targ
							targ ov03
							setv va01 rand posl posr
							setv va02 rand post posb
							setv va00 plne
							targ va03
							subv va01 8
							subv va02 5
							frel ov03
							mvto va01 va02
							addv va00 1
							plne va00
							tick 1
							targ ownr
						endi
**Don't actually affect the detritus itself, though. Tunnel flies aren't *that* big.
						addv ov01 60
**Else approach it.
					else
						gsub approach
					endi
				endi
			endi
		endi
	else
**If it's dead, decompose.
		kill ownr
	endi
**Wander around.
	subr wander
		targ ownr
		mira rand 0 1
		doif mira eq 0
			doif rtyp grap posx posy eq 8 or rtyp grap posx posy eq 9
				velo rand 5 10 rand -20 -10
			else
				velo rand 5 10 rand -5 5
			endi
		else
			doif rtyp grap posx posy eq 8 or rtyp grap posx posy eq 9
				velo rand -10 -5 rand -20 -10
			else
				velo rand -10 -5 rand -5 5
			endi
		endi
	retn
**Calculate some stuff and fly in the general direction of va00.
	subr approach
		targ va00
		setv va02 posx
		setv va03 posy
		targ ownr
		doif va02 gt posx
			setv va04 rand 5 10
			mira 0
		else
			setv va04 rand -10 -5
			mira 1
		endi
		doif va03 gt posy
			setv va05 rand 0 5
		else
			setv va05 rand -5 0
		endi
		velo va04 va05
	retn
endm

**Push script.
scrp 2 13 1 1
	inst
**If it's a creature, stim it with played with bug.
	doif crea from eq 1
		stim writ from 85 1
	endi
	snde "bez2"
	velo rand -5 5 rand -5 5
endm

**Eat script.
scrp 2 13 1 12
	inst
**If it's a creature, stim it with eaten animal.
	doif crea from eq 1
		stim writ from 80 1
	endi
	snde "chwp"
	kill ownr
endm

scrp 2 13 1 3
**If it's a creature, stim it with hit critter.
	inst
	doif crea from eq 1
		stim writ from 87 1
	endi
	doif from eq pntr
		targ from
		anim [4 5 5 0]
		targ ownr
	endi
	snde "spnk"
	doif ov06 ne 1
		setv ov06 1
		tick rand 1500 2000
		pose 2
		accg 2
		elas 0
	endi
	velo rand -5 5 rand -10 -5
endm

rscr
enum 2 13 1
	kill targ
next
scrx 2 13 1 9
scrx 2 13 1 1
scrx 2 13 1 12
scrx 2 13 1 3