--Lua refactor of Windy Valley Objects >:(
freeslot(
"sfx_sadlnd",
"MT_WVALLEYWG1",
"MT_WVALLEYWPROP",
"SPR_WVW1",
"S_WVPROP1SPIN",
"S_WVPROP1SPINFAST",
"S_WVGENERICSPIN",
"S_WVALLEYWG",
"MT_WVALLEYWG2",
"MT_WVALLEYWPROP2",
"SPR_WVW2",
"S_WVPROP2SPIN",
"S_WVPROP2SPINFAST",
"S_WVALLEYWG2"
)

--A_WVPropellerSpawn spawns objects on evenly divided slices.
--var1: upper:Z offset, lower:Object to spawn
--var2: Number of slices 

function A_WVPropellerSpawn(actor, var1, var2)
		for i = 0, var2 - 1
			local propeller = P_SpawnMobj(actor.x, actor.y, actor.z + FixedMul((var1>>16)*FRACUNIT,actor.scale), (var1 & 65535))
			propeller.scale = actor.scale
			propeller.angle = actor.angle + FixedAngle(FixedMul(FixedDiv(360*FRACUNIT, 2*var2*FRACUNIT),i*FRACUNIT))
		
		end
end
--[[mobjinfo[MT_PLAYER] = {
        doomednum = -1,
        spawnstate = S_PLAY_STND,
        spawnhealth = 1,
        seestate = S_PLAY_RUN1,
        seesound = sfx_None,
        reactiontime = 0,
        attacksound = sfx_thok,
        painstate = S_PLAY_PAIN,
        painchance = MT_THOK,
        painsound = sfx_None,
        meleestate = S_NULL,
        missilestate = S_PLAY_ATK1,
        deathstate = S_PLAY_DIE,
        xdeathstate = S_NULL,
        deathsound = sfx_None,
        speed = 1,
        radius = 16*FRACUNIT,
        height = 48*FRACUNIT,
        dispoffset = 0,
        mass = 1000,
        damage = MT_THOK,
        activesound = sfx_None,
        flags = MF_SOLID|MF_SHOOTABLE,
        raisestate = MT_THOK
}]]
--Rock Whirligig
mobjinfo[MT_WVALLEYWPROP] = {
        doomednum = -1,
        spawnstate = S_WVPROP1SPIN,
        spawnhealth = 1000,
        seestate = S_NULL,
        seesound = sfx_None,
        reactiontime = 0,
        attacksound = sfx_None,
        painstate = S_NULL,
        painchance = 0,
        painsound = sfx_None,
        meleestate = S_NULL,
        missilestate = S_NULL,
        deathstate = S_NULL,
        xdeathstate = S_NULL,
        deathsound = sfx_None,
        speed = 1,
        radius = 280*FRACUNIT,
        height = 48*FRACUNIT,
        dispoffset = 0,
        mass = 1000,
        damage = 0,
        activesound = sfx_None,
        flags = MF_PAPERCOLLISION|MF_NOGRAVITY|MF_RUNSPAWNFUNC,
        raisestate = S_NULL
}

states[S_WVPROP1SPIN] = {SPR_WVW1, B|FF_PAPERSPRITE, 
1,A_ChangeAngleRelative , 2, 2, S_WVPROP1SPIN}

states[S_WVPROP1SPINFAST] = {SPR_WVW1, B|FF_PAPERSPRITE, 
1,A_ChangeAngleRelative , 20, 20, S_WVPROP1SPINFAST}

mobjinfo[MT_WVALLEYWG1] = {
		--$Name Windy Valley Whirligig 1
		--$Sprite WVW1A0
		--$Category Windy Valley
        doomednum = 3346,
        spawnstate = S_WVALLEYWG,
        spawnhealth = 1000,
        seestate = S_NULL,
        seesound = sfx_None,
        reactiontime = 0,
        attacksound = sfx_None,
        painstate = S_NULL,
        painchance = 0,
        painsound = sfx_None,
        meleestate = S_NULL,
        missilestate = S_NULL,
        deathstate = S_NULL,
        xdeathstate = S_NULL,
        deathsound = sfx_None,
        speed = 1,
        radius = 52*FRACUNIT,
        height = 120*FRACUNIT,
        dispoffset = 0,
        mass = 1000,
        damage = 0,
        activesound = sfx_None,
        flags = MF_SOLID|MF_NOGRAVITY|MF_RUNSPAWNFUNC,
        raisestate = S_NULL
}

states[S_WVALLEYWG] = {SPR_WVW1, A, 
-1,A_WVPropellerSpawn ,(126<<16)+MT_WVALLEYWPROP,2, S_WVALLEYWG}

--Fence Whirligig
mobjinfo[MT_WVALLEYWPROP2] = {
        doomednum = -1,
        spawnstate = S_WVPROP2SPIN,
        spawnhealth = 1000,
        seestate = S_NULL,
        seesound = sfx_None,
        reactiontime = 0,
        attacksound = sfx_None,
        painstate = S_NULL,
        painchance = 0,
        painsound = sfx_None,
        meleestate = S_NULL,
        missilestate = S_NULL,
        deathstate = S_NULL,
        xdeathstate = S_NULL,
        deathsound = sfx_None,
        speed = 1,
        radius = 280*FRACUNIT,
        height = 48*FRACUNIT,
        dispoffset = 0,
        mass = 1000,
        damage = 0,
        activesound = sfx_None,
        flags = MF_PAPERCOLLISION|MF_NOGRAVITY|MF_RUNSPAWNFUNC,
        raisestate = S_NULL
}

states[S_WVPROP2SPIN] = {SPR_WVW2, B|FF_PAPERSPRITE, 
1,A_ChangeAngleRelative , 6, 6, S_WVPROP2SPIN}

states[S_WVPROP2SPINFAST] = {SPR_WVW2, B|FF_PAPERSPRITE, 
1,A_ChangeAngleRelative , 30, 30, S_WVPROP2SPINFAST}

mobjinfo[MT_WVALLEYWG2] = {
		--$Name Windy Valley Whirligig 2
		--$Sprite WVW2A0
		--$Category Windy Valley
        doomednum = 3365,
        spawnstate = S_WVALLEYWG2,
        spawnhealth = 1000,
        seestate = S_NULL,
        seesound = sfx_None,
        reactiontime = 0,
        attacksound = sfx_None,
        painstate = S_NULL,
        painchance = 0,
        painsound = sfx_None,
        meleestate = S_NULL,
        missilestate = S_NULL,
        deathstate = S_NULL,
        xdeathstate = S_NULL,
        deathsound = sfx_None,
        speed = 1,
        radius = 16*FRACUNIT,
        height = 96*FRACUNIT,
        dispoffset = 0,
        mass = 1000,
        damage = 0,
        activesound = sfx_None,
        flags = MF_NOCLIP|MF_NOGRAVITY|MF_RUNSPAWNFUNC,
        raisestate = S_NULL
}

states[S_WVALLEYWG2] = {SPR_WVW2, A, 
-1,A_WVPropellerSpawn ,(72<<16)+MT_WVALLEYWPROP2,2, S_WVALLEYWG2}

--Windy Valley Windmills
freeslot(
"MT_WINDY_VALLEY_WINDMILL",
"S_WINDY_VALLEY_WINDMILL",
"S_WV_WINDMILL_SUPPORT",
"S_WV_WINDMILL_BLADE",
"S_WV_WINDMILL_BLADE2",
"SPR_WVW3"
)

--A_WVWindmill windmill slice handler.
--Parameter determines scale in +25% increments, parameter 0 is default scale.
--Angle/360 determines blade speed (negative angles decrease it to a minimum of 0)
--Extra flag removes supports.
--Special/Ambush add 2/3 frame delay to the blades. Can be combined.
function A_WVWindmill(actor, Var1, Var2)
		local bladedelay = 0
		local dosupports = 1
		
		if (actor.spawnpoint.options & (1)) then dosupports = 0 end
		if (actor.spawnpoint.options & (MTF_OBJECTSPECIAL)) then bladedelay = $1 + 2 end
		if (actor.spawnpoint.options & (MTF_AMBUSH)) then bladedelay = $1 + 3 end
	
		if actor.spawnpoint.extrainfo ~= 0
			actor.scale = FixedMul($,actor.spawnpoint.extrainfo * (FRACUNIT/4))
		end
		local speedchange = actor.spawnpoint.angle/360
		if ((actor.spawnpoint.angle/360) < -3) then speedchange = -3 end  
		local helper = P_SpawnMobj(actor.x-FixedMul(FixedMul(224*FRACUNIT,actor.scale), cos(actor.angle+FixedAngle(180*FRACUNIT)))*dosupports, 
	actor.y-FixedMul(FixedMul(224*FRACUNIT,actor.scale), sin(actor.angle+FixedAngle(180*FRACUNIT)))*dosupports, actor.z, MT_GFZFLOWER3)
		helper.scale = actor.scale
		helper.angle = actor.angle
		for i = -48,48,2
			if (( i >= -48 and i <= -32) or ( i >= 32 and i <=48)) and (dosupports == 0) then
				continue
			end
			local bladebit = P_SpawnMobj(helper.x,helper.y,helper.z,MT_THOK)
			bladebit.scale = actor.scale
			bladebit.target = helper
			bladebit.angle = actor.angle
			A_CapeChase(bladebit,0,i)
			--Supports
			if (( i >= -48 and i <= -32) or ( i >= 32 and i <=48))
				bladebit.state = S_WV_WINDMILL_SUPPORT
			elseif ( i >= -30 and i <=-24) or ( i >= 24 and i <=30)
				--Edge of blade (yellow)
				bladebit.state = S_WV_WINDMILL_BLADE
				bladebit.threshold = 3 + speedchange 
				bladebit.tics = $1 + bladedelay*bladebit.threshold
				bladebit.extravalue2 = bladebit.frame
			else
				--Blade internals (Yellow and white)
				bladebit.state = S_WV_WINDMILL_BLADE2
				bladebit.threshold = 3 + speedchange
				bladebit.tics = $1 + bladedelay*bladebit.threshold
				bladebit.extravalue2 = bladebit.frame
			end
		end
		helper.state = S_NULL
end

--Animation Handler because changing var2 of an FF_ANIMATE state does nothing to
--animation.
--Var1: StartFrame
--Var2: End Frame.
function A_WVBladeHandler(actor,var1,var2)
	if (actor.extravalue1 == actor.threshold)
		actor.extravalue1 = 0
		actor.extravalue2 = $1 + 1
		if actor.extravalue2 > var2
			actor.extravalue2 = (var1)
		end
	end
	if actor.threshold ~= 0 
		actor.frame = FF_PAPERSPRITE|actor.extravalue2
		actor.extravalue1 = $1+1
	end
end

states[S_WV_WINDMILL_SUPPORT] = {SPR_WVW3, A|FF_PAPERSPRITE, 
-1,None ,0,0, S_WV_WINDMILL_SUPPORT}

states[S_WV_WINDMILL_BLADE] = {SPR_WVW3, B|FF_PAPERSPRITE, 
1,A_WVBladeHandler ,B,G, S_WV_WINDMILL_BLADE}

states[S_WV_WINDMILL_BLADE2] = {SPR_WVW3, H|FF_PAPERSPRITE, 
1,A_WVBladeHandler ,H,M, S_WV_WINDMILL_BLADE2}

mobjinfo[MT_WINDY_VALLEY_WINDMILL] = {
		--$Name Windy Valley Windmill
		--$Sprite WVW3ALAR
		--$Category Windy Valley
        doomednum = 3366,
        spawnstate = S_WINDY_VALLEY_WINDMILL,
        spawnhealth = 1000,
        seestate = S_NULL,
        seesound = sfx_mswing,
        reactiontime = 0,
        attacksound = sfx_None,
        painstate = S_NULL,
        painchance = 0,
        painsound = sfx_None,
        meleestate = S_NULL,
        missilestate = S_NULL,
        deathstate = S_NULL,
        xdeathstate = S_NULL,
        deathsound = sfx_None,
        speed = 1,
        radius = 32*FRACUNIT,
        height = 128*FRACUNIT,
        dispoffset = 0,
        mass = 1000,
        damage = 0,
        activesound = sfx_None,
        flags = MF_NOCLIP|MF_NOGRAVITY|MF_RUNSPAWNFUNC,
        raisestate = S_NULL
}

states[S_WINDY_VALLEY_WINDMILL] = {SPR_NULL, A, 
-1,A_WVWindmill ,0,0, S_WINDY_VALLEY_WINDMILL}


--Giant Fan
freeslot(
	"SPR_WVW5",
	"S_WV_BIGFAN",
	"S_WV_PLANE1",
	"S_WV_PLANE2",
	"S_WV_PLANE3",
	"MT_WV_BIGPROP",
	"S_WV_BIGPROP",
	"MT_WV_BIGFAN"
)



--The parameter of the fan determines it's size (going up from 1x)
function A_WVBigFan(mo,var1,var2)
		if mo.spawnpoint.extrainfo ~= 0
			mo.scale = FixedMul($,(mo.spawnpoint.extrainfo + 4)*(FRACUNIT/4))
		end
		for i =0,8
			--[+FixedMul(mo.scale,16*FRACUNIT)]]
			local panel = P_SpawnMobj(mo.x,mo.y,mo.z+FixedMul(mo.scale,48*FRACUNIT),MT_THOK)
			panel.destscale = mo.scale
			P_SetScale(panel,mo.scale)
			panel.height = FixedMul(26*FRACUNIT,mo.scale)
			--panel.radius = 128*FRACUNIT * mo.scale
			panel.flags = $1 & ~(MF_DONTENCOREMAP|MF_SCENERY)
			--panel.flags = $1|(MF_SOLID|MF_PAPERCOLLISION|MF_RUNSPAWNFUNC)
			panel.info.speed = 2*FRACUNIT
			panel.movedir = FixedAngle(FixedMul(FixedDiv(i<<16,8<<16),360<<16))
			panel.threshold = FixedMul(155*FRACUNIT,mo.scale)
			panel.target = mo
			panel.state = S_WV_PLANE1
		end
		mo.angle = $1 + ANG30
		A_WVPropellerSpawn(mo, MT_WV_BIGPROP, 2)
end

mobjinfo[MT_WV_BIGPROP] = {
        doomednum = -1,
        spawnstate = S_WV_BIGPROP,
        spawnhealth = 1000,
        seestate = S_NULL,
        seesound = sfx_mswing,
        reactiontime = 0,
        attacksound = sfx_None,
        painstate = S_NULL,
        painchance = 0,
        painsound = sfx_None,
        meleestate = S_NULL,
        missilestate = S_NULL,
        deathstate = S_NULL,
        xdeathstate = S_NULL,
        deathsound = sfx_None,
        speed = 1,
        radius = 795*FRACUNIT,
        height = 128*FRACUNIT,
        dispoffset = 0,
        mass = 1000,
        damage = 0,
        activesound = sfx_None,
        flags = MF_NOCLIP|MF_NOGRAVITY|MF_RUNSPAWNFUNC|MF_RUNSPAWNFUNC,
        raisestate = S_NULL
}

mobjinfo[MT_WV_BIGFAN] = {
		--$Name Windy Valley  Big Fan
		--$Sprite WVW5ALAR
		--$Category Windy Valley
        doomednum = 3367,
        spawnstate = S_WV_BIGFAN,
        spawnhealth = 1000,
        seestate = S_NULL,
        seesound = sfx_mswing,
        reactiontime = 0,
        attacksound = sfx_None,
        painstate = S_NULL,
        painchance = 0,
        painsound = sfx_None,
        meleestate = S_NULL,
        missilestate = S_NULL,
        deathstate = S_NULL,
        xdeathstate = S_NULL,
        deathsound = sfx_None,
        speed = 1,
        radius = 60*FRACUNIT,
        height = 26*FRACUNIT,
        dispoffset = 0,
        mass = 1000,
        damage = 0,
        activesound = sfx_None,
        flags = MF_NOCLIP|MF_NOGRAVITY|MF_RUNSPAWNFUNC,
        raisestate = S_NULL
}

states[S_WV_BIGFAN] = {SPR_NULL, A, 
-1,A_WVBigFan ,0,0, S_WV_BIGFAN}

states[S_WV_BIGPROP] = {SPR_WVW5, B|FF_PAPERSPRITE, 
1,A_ChangeAngleRelative , 2, 2, S_WV_BIGPROP}

states[S_WV_PLANE1] = {SPR_WVW5, A|FF_PAPERSPRITE, 
0,A_UnidusBall , 0, 0, S_WV_PLANE2}

states[S_WV_PLANE2] = {SPR_WVW5, A|FF_PAPERSPRITE, 
0,A_FaceTarget , 0, 0, S_WV_PLANE3}

states[S_WV_PLANE3] = {SPR_WVW5, A|FF_PAPERSPRITE, 
1,A_ChangeAngleRelative , 90, 90, S_WV_PLANE1}

--Windy Valley Fan
freeslot(
"MT_WV_FANBLADES",
"S_WV_FANBLADES",
"SPR_WVW4"
)

mobjinfo[MT_WV_FANBLADES] = {
		--$Name Windy Valley Fan
		--$Sprite WVW4ALAR
		--$Category Windy Valley
        doomednum = 3368,
        spawnstate = S_WV_FANBLADES,
        spawnhealth = 1000,
        seestate = S_NULL,
        seesound = sfx_mswing,
        reactiontime = 0,
        attacksound = sfx_None,
        painstate = S_NULL,
        painchance = 0,
        painsound = sfx_None,
        meleestate = S_NULL,
        missilestate = S_NULL,
        deathstate = S_NULL,
        xdeathstate = S_NULL,
        deathsound = sfx_None,
        speed = 1,
        radius = 60*FRACUNIT,
        height = 26*FRACUNIT,
        dispoffset = 0,
        mass = 1000,
        damage = 0,
        activesound = sfx_None,
        flags = MF_SOLID|MF_PAPERCOLLISION|MF_NOGRAVITY|MF_RUNSPAWNFUNC,
        raisestate = S_NULL
}

states[S_WV_FANBLADES] = {SPR_WVW4, A|FF_PAPERSPRITE|FF_ANIMATE, 
-1,None,3,5, S_WV_FANBLADES}
