freeslot("MT_CUTSCENEOBJECT", "S_CUTSCENEOBJECT", "MT_CUTSCENEWAYPOINT", "S_CUTSCENEWAYPOINT")

mobjinfo[MT_CUTSCENEOBJECT] = {
	//$Name Cutscene Object
	//$Sprite PPPLA0
	//$Arg0 First Frame
	//$Arg1 Timer
	//$Arg2 Last Frame
	//$Arg3 Use custom state?
	//$Arg4 Waypoint Order
	//$Arg5 Use color?
	//$Arg6 Object Flags
	//$Arg7 Inactive at spawn?
	//$Arg8 Effects
	//$Arg9 Dialogue Set
	//$Arg3Type	11
	//$Arg3Enum	noyes
	//$Arg7Type	11
	//$Arg7Enum	{0="No"; 1="Only Animate"; 2="Yes";}
	//$Arg5Type	11
	//$Arg5Enum	noyes
	//$Arg8Type	12
	//$Arg8Enum	{1="Gravity"; 2="Spinny;}
	//$StringArg0 Sprite/State
	//$StringArg1 Color/Sound
	doomednum = 3964,
	spawnstate = S_CUTSCENEOBJECT,
	spawnhealth = 9999999,
	flags = MF_NOGRAVITY|MF_NOCLIP|MF_NOCLIPHEIGHT,
	radius = 16*FRACUNIT,
	height = 16*FRACUNIT,
	activesound = sfx_None
}
--arg 5, 6, 7, 9, stringarg1 need proper implementation
mobjinfo[MT_CUTSCENEWAYPOINT] = {
	//$Name Cutscene Waypoints
	//$Sprite CTWPA0
	//$Arg0 Waypoint Order
	//$Arg0Default 1
	//$Arg1 Sequence
	//$Arg1Default 0
	//$Arg2 Waypoint Speed
	//$Arg3 First Frame
	//$Arg4 Timer
	//$Arg5 Last Frame
	//$Arg6 Use custom state?
	//$Arg7 Finish object here?
	//$Arg8 Effects
	//$Arg9 Waypoint Timer
	//$StringArg0 Sprite/State
	//$StringArg1 Sound
	//$Arg3Default -1
	//$Arg4Default -1
	//$Arg5Default -1
	//$Arg6Default 0
	//$Arg6Type	11
	//$Arg6Enum	noyes
	//$Arg7Type	11
	//$Arg7Enum	noyes
	doomednum = 3965,
	spawnstate = S_CUTSCENEWAYPOINT,
	flags = MF_NOGRAVITY,
	spawnhealth = 9999999,
	radius = 16*FRACUNIT,
	height = 16*FRACUNIT,
	activesound = sfx_None
}
--arg 8, 9, stringarg 1 need proper implementation

states[S_CUTSCENEWAYPOINT] = {nil, nil, -1, None, 0, 0, S_CUTSCENEWAYPOINT}

states[S_CUTSCENEOBJECT] = {nil, nil, -1, None, 0, 0, S_CUTSCENEOBJECT}

addHook("MapThingSpawn", function(mobj, thing)
	mobj.inactive = thing.args[7]
	if thing.args[3] == 1 then 
		mobj.state = _G[thing.stringargs[0]]
		if thing.stringargs[1] ~= nil then mobj.color = _G[thing.stringargs[1]] end
	else
		mobj.state = S_CUTSCENEOBJECT
		mobj.sprite = _G[thing.stringargs[0]]
		mobj.frame = thing.args[0]
		mobj.ogframe = thing.args[0]
		mobj.time = 0
		mobj.currentwaypoint = -1
		mobj.animtime = thing.args[1]
		mobj.animframes = thing.args[2]
		mobj.zoomtimer = 1
		mobj.savedangle = thing.angle*ANG1
		if thing.stringargs[1] ~= nil then mobj.color = _G[thing.stringargs[1]] end
	end
	if thing.args[9] then
		mobj.dialogueset = thing.args[9]
		mobj.radius = 128*FRACUNIT
		mobj.height = 100*FRACUNIT
		mobj.flags = $|MF_SPECIAL
	end
end, MT_CUTSCENEOBJECT)

addHook("MobjThinker", function(mobj)
	if mobj.spawnpoint.args[4] ~= 0 and mobj.waypoints == nil --keeping it here for now, even if it's ugly and should be in the mapthingspawn hook
		mobj.waypoints = {}
		for doilus in mobjs.iterate() do
			if doilus.type == MT_CUTSCENEWAYPOINT then
				if doilus.spawnpoint.args[0] == mobj.spawnpoint.args[4]
					table.insert(mobj.waypoints, doilus.spawnpoint.args[1], doilus)
					doilus.flags = $|MF_NOTHINK
				end
			end
		end
	end
	if mobj.toucher and mobj.toucher.valid
		mobj.toucher.promptcoord = nil
		if mobj.toucher.dialoguethinker == true
			mobj.angle = mobj.talkingangle
		elseif mobj.angle ~= mobj.savedangle then 
			mobj.angle = mobj.savedangle
		end
	end
	if mobj.inactive == 2 then return end
	if mobj.state == S_CUTSCENEOBJECT
		mobj.time = $ + 1
		if mobj.time == mobj.animtime
			if mobj.frame ~= mobj.animframes
				mobj.frame = $ + 1
				mobj.time = 0
			else
				mobj.frame = mobj.ogframe
				mobj.time = 0
			end
		end
	end
	if mobj.inactive == 1 then return end
	if mobj.waypoints ~= nil
		mobj.zoomtimer = $ - 1
		if mobj.zoomtimer <= 0
			if mobj.currentwaypoint ~= #(mobj.waypoints)
				mobj.currentwaypoint = $ + 1
			else
				mobj.currentwaypoint = 0
			end
			if mobj.finisher == true then
				P_RemoveMobj(mobj)
				return
			end
			local waypoint = mobj.waypoints[mobj.currentwaypoint]
			local speed = waypoint.spawnpoint.args[2]*FRACUNIT
			local dist = P_AproxDistance(P_AproxDistance(waypoint.x - mobj.x, waypoint.y - mobj.y), waypoint.z - mobj.z)
			if waypoint.spawnpoint.args[3] ~= -1 then 
				mobj.frame = waypoint.spawnpoint.args[3]
				mobj.ogframe = waypoint.spawnpoint.args[3]
			end
			if waypoint.spawnpoint.args[4] ~= -1 then 
				mobj.animtime = waypoint.spawnpoint.args[4]
				mobj.time = 0
			end
			if waypoint.spawnpoint.args[5] ~= -1 then mobj.animframes = waypoint.spawnpoint.args[5] end
			if waypoint.spawnpoint.stringargs[0] ~= nil
				if waypoint.spawnpoint.args[6] == 1
					mobj.state = _G[waypoint.spawnpoint.stringargs[0]]
				else
					mobj.sprite = _G[waypoint.spawnpoint.stringargs[0]]
				end
			end
			mobj.angle = waypoint.angle
			if speed ~= 0
				mobj.momx = FixedMul(FixedDiv(waypoint.x - mobj.x, dist), (speed))
				mobj.momy = FixedMul(FixedDiv(waypoint.y - mobj.y, dist), (speed))
				P_SetObjectMomZ(mobj, FixedMul(FixedDiv(waypoint.z - mobj.z, dist), (speed)), false)
				mobj.zoomtimer = dist/speed
			else
				mobj.momx = 0
				mobj.momy = 0
				P_SetObjectMomZ(mobj, 0, false)
				mobj.zoomtimer = 0
			end
			if waypoint.spawnpoint.stringargs[1] then 
				S_StartSound(mobj, _G[(waypoint.spawnpoint.stringargs[1])])
			end
			if waypoint.spawnpoint.args[9] ~= 0 then mobj.zoomtimer = waypoint.spawnpoint.args[9] end
			if waypoint.spawnpoint.args[7] == 1 then mobj.finisher = true end
		end
	end
end, MT_CUTSCENEOBJECT)

addHook("LinedefExecute", function(line, mo, sec)
	for thing in mapthings.tagged(line.tag) do
		thing.mobj.inactive = 0
	end
end, "ACTIVENU")

addHook("TouchSpecial", function(mobj, toucher)
	if toucher.player.bot then return end

	if not multiplayer then

		mobj.toucher = toucher

		toucher = toucher
		toucher.player.pflags = $|PF_JUMPSTASIS
		toucher.promptcoord = R_WorldToScreen2(toucher.player, camera, mobj)
	
		if (toucher.player.cmd.buttons & BT_JUMP and not (toucher.player.lastbuttons & BT_JUMP)) and not toucher.dialoguethinker and not toucher.shopthinker
			TriggerDialogue(toucher, sldc2dialogue[mobj.dialogueset])
			mobj.talkingangle = R_PointToAngle2(mobj.x, mobj.y, toucher.x, toucher.y)
		end
	end
	
	return true
end, MT_CUTSCENEOBJECT)

freeslot("SPR_CUTR", "SPR_CUTJ", "SPR_WRWK")