::root <- getroottable()
PrecacheSound("mvm/mvm_tank_smash.wav")

function root::explodeWindow() {
	local origin = Entities.FindByName(null, "finalPath_3").GetOrigin() + Vector(0, 0, 64)
	DispatchParticleEffect("explosionTrail_seeds_mvm", origin, Vector())
	DispatchParticleEffect("fluidSmokeExpl_ring_mvm", origin, Vector())
	DispatchParticleEffect("hightower_explosion", origin, Vector())
	EmitSoundEx({
		sound_name = "mvm/mvm_tank_smash.wav"
	})
	
	if(!colonelBlimpCallbacks.persistentParticle) {
		//this will be deleted whenever a map reset happens
		SpawnEntityFromTable("info_particle_system", {
			origin = origin
			effect_name = "hightower_smoke"
			start_active = 1
		})
		SpawnEntityFromTable("info_particle_system", {
			origin = origin
			effect_name = "hightower_smoke"
			start_active = 1
		})
		SpawnEntityFromTable("info_particle_system", {
			origin = origin - Vector(0, 64, 0)
			effect_name = "hightower_smoke"
			start_active = 1
		})
		SpawnEntityFromTable("info_particle_system", {
			origin = origin - Vector(0, 0, 128)
			effect_name = "hightower_smoke"
			start_active = 1
		})
		SpawnEntityFromTable("info_particle_system", {
			origin = origin - Vector(0, 0, 128)
			effect_name = "hightower_smoke"
			start_active = 1
		})
		
		colonelBlimpCallbacks.persistentParticle = true
	}
}

::colonelBlimpCallbacks <- {
	missionName = null
	finalPath = [
		Vector(-768, 64, 320),
		Vector(-768, -96, 320), //this is the node before the window
		Vector(-768, -256, 320), 
		Vector(-768, -960, 192), //marks the straightway into hatch
		Vector(-784, -1664, 128),
		Vector(-800, -2288, 72)
	]

	outsidePath = [
		Vector(-1984, 1920, 384)    // tank_path_1
		Vector(-1280, 2048, 320)    // tank_path_2
		Vector(-576, 2176, 64)    // tank_path_3
		Vector(-64, 2176, -128)    // tank_path_4
		Vector(512, 2048, -192)    // tank_path_5
		Vector(896, 1920, -192)    // tank_path_6
		Vector(1472, 1472, -128)    // tank_path_7
		Vector(1536, 832, -128)    // tank_path_8
		Vector(1472, 640, -384)    // tank_path_9
		Vector(1472, 128, -384)    // tank_path_10
		Vector(1472, 0, -384)    // tank_path_11
		Vector(1280, -256, -384)    // tank_path_12
		Vector(896, -512, -384)    // tank_path_13
		Vector(512, -832, -192)    // tank_path_14
		Vector(384, -960, -128)    // tank_path_15
		Vector(192, -960, -128)    // tank_path_16
		Vector(128, -832, -128)    // tank_path_17
		Vector(0, -448, -64)    // tank_path_18
		Vector(-640, -576, 64)    // tank_path_19
		Vector(-768, -960, 192)    // tank_path_20
		Vector(-784, -1664, 128)    // tank_path_21
		Vector(-800, -2288, 72)    // tank_path_22
	]

	midPath = [
		Vector(2560, -704, -256)    // tank_path_1
		Vector(1984, -704, -256)    // tank_path_2
		Vector(1728, -768, -320)    // tank_path_3
		Vector(1536, -768, -384)    // tank_path_4
		Vector(1408, -640, -384)    // tank_path_5
		Vector(1408, -448, -384)    // tank_path_6
		Vector(1216, -384, -384)    // tank_path_7
		Vector(896, -512, -384)    // tank_path_8 //aligns with outsidepath_13 here
		Vector(512, -832, -192)    // tank_path_9
		Vector(384, -960, -128)    // tank_path_10
		Vector(192, -960, -128)    // tank_path_11
		Vector(128, -832, -128)    // tank_path_12
		Vector(0, -448, -64)     // tank_path_13
		Vector(-640, -576, 64)     // tank_path_14
		Vector(-768, -960, 192)    // tank_path_15
		Vector(-784, -1664, 128)    // tank_path_16
		Vector(-800, -2288, 72)    // tank_path_17
	]
	
	paths = null
	persistentParticle = false
	
	function cleanup() {
		delete ::explodeWindow
		delete ::colonelBlimpCallbacks
	}

	function OnGameEvent_recalculate_holidays(_) {
		if(GetRoundState() == 3) {
			if(missionName != NetProps.GetPropString(Entities.FindByClassname(null, "tf_objective_resource"), "m_iszMvMPopfileName")) {
				cleanup()
			}
			else {
				//i guess tankext cleans itself up on fail?
				recreatePaths()
			}
		}
	}
	
	function recreatePaths() {
		IncludeScript("tankextensions_main", root)
		TankExt.CreatePaths(colonelBlimpCallbacks.paths)

		persistentParticle = false

		local finalPath2 = Entities.FindByName(null, "finalPath_2")
		EntityOutputs.AddOutput(finalPath2, "OnPass", "!self", "callscriptfunction", "explodeWindow", -1, -1)
		EntityOutputs.AddOutput(finalPath2, "OnPass", "!activator", "runscriptcode", "self.SetCollisionGroup(2)", -1, -1)
		EntityOutputs.AddOutput(Entities.FindByName(null, "finalPath_3"), "OnPass", "!activator", "runscriptcode", "self.SetCollisionGroup(0)", -1, -1)
	}
}

colonelBlimpCallbacks.paths = {
	"finalPath" : colonelBlimpCallbacks.finalPath
	"outsidePath" : colonelBlimpCallbacks.outsidePath
	"midPath" : colonelBlimpCallbacks.midPath
}
colonelBlimpCallbacks.missionName = NetProps.GetPropString(Entities.FindByClassname(null, "tf_objective_resource"), "m_iszMvMPopfileName")
__CollectGameEventCallbacks(colonelBlimpCallbacks)
colonelBlimpCallbacks.recreatePaths()