define FIREGUN 7303
define GUNFIRED 7302

gamearray GUNS_ID 16384
gamevar activategun 0 2
gamevar activateguntime 0 2
gamevar gettarget 0 2

action GUNSLEEP -5 1 1 1 1
action GUNOPENING -4 3 1 1 10
action GUNOPED -1 1 1 1 1
action GUNFIRING 0 2 1 1 10
action GUNCLOSING -1 4 1 -1 10


onevent EVENT_LOADACTOR
ifactor CANNONBALL
{
	getactor[THISACTOR].lotag LOTAGSAVED
	setvar spriteid 0
	whilevarn spriteid 16384
	{
		getactor[spriteid].picnum picnum
		ifvare picnum FIREGUN
		{
			getactor[spriteid].lotag lotag
			ifvarvare lotag LOTAGSAVED { setarray GUNS_ID[spriteid] lotag }
		}
		addvar spriteid 1
	}
}
endevent

actor CANNONBALL
cstat 32768
	getactor[THISACTOR].lotag LOTAGSAVED 
	getactor[THISACTOR].sectnum sectnum
	getplayer[THISACTOR].i PLAYERID
	getactor[PLAYERID].sectnum psectnum
		
	setvar spriteid 0
	whilevarn spriteid 16384
	{
		ifvarvare GUNS_ID[spriteid] LOTAGSAVED { ifvarvare sectnum psectnum { setactorvar[spriteid].activategun 1 setactorvar[spriteid].gettarget 1 } else setactorvar[spriteid].gettarget 0 }
		addvar spriteid 1
	}
enda

actor FIREGUN 0 GUNSLEEP
addlogvar gettarget
ifvare activategun 1 
{
	ifaction GUNSLEEP action GUNOPENING
	ifaction GUNOPENING ifactioncount 3 action GUNOPED
}
else action GUNSLEEP

	ifaction GUNOPED
	{ addvar activateguntime 1 ifvare activateguntime 26 { action GUNFIRING setvar activateguntime 0 } }
	ifaction GUNFIRING
	{
		addvar activateguntime 1
		ifvare gettarget 1 setvar activateguntime 0
		ifvare activateguntime 256 ifvarn gettarget 1 { action GUNCLOSING setvar activateguntime 0 }
	}
	ifaction GUNFIRING 
	{ 
		ifrnd 128 eshoot FLAME else eshoot FLAME2
		getactor[RETURN].z z
		addvar z 5120
		setactor[RETURN].z z
		soundonce SPRAYFIR 
		getactor[THISACTOR].ang temp
		randvar tmp 48
		ifrnd 128  addvarvar temp tmp else subvarvar temp tmp
		setactor[RETURN].ang temp
		setvar zvel 0
		randvar tmp 1280
		addvarvar zvel tmp
		setactor[RETURN].zvel zvel
	}
	ifaction GUNCLOSING ifactioncount 4 { setactorvar[THISACTOR].activategun 0 action GUNSLEEP }
enda
