/*/////////////////////////////////////////////////////////////////////////////
Dear reader,
Sorry my code is not good
I'm sure it could be cleaner and clearer
but I'm a noob with a deadline
You are epic though
*//////////////////////////////////////////////////////////////////////////////
freeslot(
"MT_ICEPLATSPAWNER",
"MT_ICEPLATHITBOX",
"MT_ICEPLATFLOOR",
"MT_ICEPLATCEILING",
"MT_IPNORTHWALL",
"MT_IPEASTWALL",
"MT_IPSOUTHWALL",
"MT_IPWESTWALL",
"MT_LARGEICESPAWNER",
"MT_LARGEICECORNER",
"MT_LARGEICESIDE",
"MT_LARGEICECENTER",
"MT_LARGEICECEILING",
"MT_CBUBSPAWNER",
"MT_CURRENTBUBBLE",
"MT_CBUBREF",
"MT_CBUBUP",
"S_ICEPLATSPAWNER",
"S_ICEPLATHITBOX",
"S_ICEPLATFLOOR",
"S_ICEPLATCEILING",
"S_IPNORTHWALL",
"S_IPEASTWALL",
"S_IPSOUTHWALL",
"S_IPWESTWALL",
"S_ICEPLATWALL",
"S_LARGEICESPAWNER",
"S_LARGEICECEILING",
"S_LARGEICEPOSA1",
"S_LARGEICEPOSA2",
"S_LARGEICEPOSA3",
"S_LARGEICEPOSA4",
"S_LARGEICEPOSB1",
"S_LARGEICEPOSB2",
"S_LARGEICEPOSB3",
"S_LARGEICEPOSB4",
"S_LARGEICEPOSC1",
"S_LARGEICEPOSC2",
"S_LARGEICEPOSC3",
"S_LARGEICEPOSC4",
"S_LARGEICEPOSD1",
"S_LARGEICEPOSD2",
"S_LARGEICEPOSD3",
"S_LARGEICEPOSD4",
"S_LARGEICEREFERENCE",
"S_CBUBSPAWNER",
"S_CBUB",
"S_CBUBREF",
"SPR_ICEF",
"SPR_ICEW",
"SPR_BIGI"
)



/*
This function spawns the objects for the floor, ceiling, and wall visuals of the ice platform.
*/

local function spawnIcePlatVisuals(actor, spawnpoint)

	local newIceHBX = P_SpawnMobj(actor.x, actor.y, actor.z, MT_ICEPLATHITBOX)
	
	local newFloor = P_SpawnMobj(actor.x-4*FRACUNIT, actor.y, actor.z, MT_ICEPLATFLOOR)
	newFloor.flags2 = MF2_SPLAT
	newFloor.renderflags = RF_NOSPLATBILLBOARD
	newFloor.target = newIceHBX
	
	local newCeiling = P_SpawnMobj(actor.x-4*FRACUNIT, actor.y, actor.z+64*FRACUNIT, MT_ICEPLATCEILING)
	newCeiling.flags2 = MF2_SPLAT
	newCeiling.renderflags = RF_NOSPLATBILLBOARD
	newCeiling.target = newIceHBX
	
	local newWallNorth = P_SpawnMobj(actor.x+64*FRACUNIT, actor.y, actor.z+28*FRACUNIT, MT_IPNORTHWALL)
	newWallNorth.target = newIceHBX
	newWallNorth.angle = ANGLE_90
	
	local newWallEast = P_SpawnMobj(actor.x, actor.y+64*FRACUNIT, actor.z+28*FRACUNIT, MT_IPEASTWALL)
	newWallEast.target = newIceHBX
	newWallEast.angle = 0
	
	local newWallWest = P_SpawnMobj(actor.x, actor.y-64*FRACUNIT, actor.z+28*FRACUNIT, MT_IPWESTWALL)
	newWallWest.target = newIceHBX
	newWallWest.angle = ANGLE_180
	
	local newWallSouth = P_SpawnMobj(actor.x-64*FRACUNIT, actor.y, actor.z+28*FRACUNIT, MT_IPSOUTHWALL)
	newWallSouth.target = newIceHBX
	newWallSouth.angle = ANGLE_270
	
end


/*
This function spawns only the floor and ceiling.
*/

local function spawnIcePlatCollision(actor, spawnpoint)

	local newIceHBX = P_SpawnMobj(actor.x, actor.y, actor.z, MT_ICEPLATHITBOX)
	newIceHBX.flags = MF_SOLID|MF_NOGRAVITY
	
	/*
	local newFloor = P_SpawnMobj(actor.x-4*FRACUNIT, actor.y, actor.z, MT_ICEPLATFLOOR)
	newFloor.flags2 = MF2_SPLAT
	newFloor.renderflags = RF_NOSPLATBILLBOARD
	newFloor.target = newIceHBX
	
	local newCeiling = P_SpawnMobj(actor.x-4*FRACUNIT, actor.y, actor.z+64*FRACUNIT, MT_ICEPLATCEILING)
	newCeiling.flags2 = MF2_SPLAT
	newCeiling.renderflags = RF_NOSPLATBILLBOARD
	newCeiling.target = newIceHBX
	*/
	
end




/*
Creates visuals for ice platform when the object is spawned by its thing
*/
addHook("MapThingSpawn", spawnIcePlatVisuals, MT_ICEPLATSPAWNER)




local function A_ICeilingPos(actor)
	P_TeleportMove(actor, actor.target.x-4*FRACUNIT, actor.target.y, actor.target.z+64*FRACUNIT)
end

local function A_IFloorPos(actor)
	P_TeleportMove(actor, actor.target.x-4*FRACUNIT, actor.target.y, actor.target.z)
end

local function A_INorthPos(actor)
	P_TeleportMove(actor, actor.target.x+64*FRACUNIT, actor.target.y, actor.target.z+28*FRACUNIT)
end

local function A_IEastPos(actor)
	P_TeleportMove(actor, actor.target.x, actor.target.y+64*FRACUNIT, actor.target.z+28*FRACUNIT)
end

local function A_ISouthPos(actor)
	P_TeleportMove(actor, actor.target.x-64*FRACUNIT, actor.target.y, actor.target.z+28*FRACUNIT)
end

local function A_IWestPos(actor)
	P_TeleportMove(actor, actor.target.x, actor.target.y-64*FRACUNIT, actor.target.z+28*FRACUNIT)
end



/*//////////////////////////////////////////////////////////////////////////////
Small ice platform
//////////////////////////////////////////////////////////////////////////////*/
mobjinfo[MT_ICEPLATSPAWNER] = {
doomednum = 2320,
spawnstate = S_ICEPLATSPAWNER,
radius = 64*FRACUNIT,
height = 64*FRACUNIT,
flags = MF_NOGRAVITY|MF_NOCLIP
}



states[S_ICEPLATSPAWNER] = {
sprite = SPR_NULL,
nextstate = S_ICEPLATSPAWNER
}



mobjinfo[MT_ICEPLATHITBOX] = {
doomednum = 2317,
spawnstate = S_ICEPLATHITBOX,
radius = 64*FRACUNIT,
height = 64*FRACUNIT,
flags = MF_SOLID|MF_SLIDEME
}



states[S_ICEPLATHITBOX] = {
sprite = SPR_NULL,
nextstate = S_ICEPLATHITBOX
}



mobjinfo[MT_ICEPLATFLOOR] = {
doomednum = 2318,
spawnstate = S_ICEPLATFLOOR,
radius = 64*FRACUNIT,
height = 64*FRACUNIT,
flags = MF_NOGRAVITY|MF_NOBLOCKMAP
}
mobjinfo[MT_ICEPLATCEILING] = {
doomednum = 2319,
spawnstate = S_ICEPLATCEILING,
radius = 64*FRACUNIT,
height = 64*FRACUNIT,
flags = MF_NOGRAVITY|MF_NOBLOCKMAP
}



states[S_ICEPLATCEILING] = {
sprite = SPR_ICEF,
frame = A,
tics = 1,
action = A_ICeilingPos,
nextstate = S_ICEPLATCEILING
}
states[S_ICEPLATFLOOR] = {
sprite = SPR_ICEF,
frame = A,
tics = 1,
action = A_IFloorPos,
nextstate = S_ICEPLATFLOOR
}




mobjinfo[MT_IPNORTHWALL] = {
doomednum = 2319,
spawnstate = S_IPNORTHWALL,
radius = 64*FRACUNIT,
height = 32*FRACUNIT,
flags = MF_NOGRAVITY|MF_NOBLOCKMAP
}
mobjinfo[MT_IPEASTWALL] = {
doomednum = 2322,
spawnstate = S_IPEASTWALL,
radius = 64*FRACUNIT,
height = 32*FRACUNIT,
flags = MF_NOGRAVITY|MF_NOBLOCKMAP
}
mobjinfo[MT_IPSOUTHWALL] = {
doomednum = 2323,
spawnstate = S_IPSOUTHWALL,
radius = 64*FRACUNIT,
height = 32*FRACUNIT,
flags = MF_NOGRAVITY|MF_NOBLOCKMAP
}
mobjinfo[MT_IPWESTWALL] = {
doomednum = 2324,
spawnstate = S_IPWESTWALL,
radius = 64*FRACUNIT,
height = 32*FRACUNIT,
flags = MF_NOGRAVITY|MF_NOBLOCKMAP
}



states[S_IPNORTHWALL] = {
sprite = SPR_ICEW,
frame = FF_PAPERSPRITE | A,
tics = 1,
action = A_INorthPos,
nextstate = S_IPNORTHWALL
}
states[S_IPEASTWALL] = {
sprite = SPR_ICEW,
frame = FF_PAPERSPRITE | A,
tics = 1,
action = A_IEastPos,
nextstate = S_IPEASTWALL
}
states[S_IPSOUTHWALL] = {
sprite = SPR_ICEW,
frame = FF_PAPERSPRITE | A,
tics = 1,
action = A_ISouthPos,
nextstate = S_IPSOUTHWALL
}
states[S_IPWESTWALL] = {
sprite = SPR_ICEW,
frame = FF_PAPERSPRITE | A,
tics = 1,
action = A_IWestPos,
nextstate = S_IPWESTWALL
}






/*//////////////////////////////////////////////////////////////////////////////
Large ice platform
//////////////////////////////////////////////////////////////////////////////*/



mobjinfo[MT_LARGEICESPAWNER] = {
doomednum = 4000,
spawnstate = S_ICEPLATSPAWNER,
radius = 256*FRACUNIT,
height = 64*FRACUNIT,
flags = MF_NOGRAVITY|MF_NOCLIP
}

mobjinfo[MT_LARGEICECEILING] = {
doomednum = 3000,
spawnstate = S_LARGEICECEILING,
radius = 64*FRACUNIT,
height = 64*FRACUNIT,
flags = MF_NOGRAVITY|MF_NOBLOCKMAP
}



states[S_LARGEICESPAWNER] = {
sprite = SPR_BRAK,
frame = A,
tics = 1,
nextstate = S_LARGEICESPAWNER
}

states[S_LARGEICECEILING] = {
sprite = SPR_BIGI,
frame = A,
tics = 1,
action = A_ICeilingPos,
nextstate = S_LARGEICECEILING
}

/*//////////////////////////////////////////////////////////////////////////////
Start large ice position function definition
//////////////////////////////////////////////////////////////////////////////*/

local function A_BigIcePosA1(actor)
	P_TeleportMove(actor, actor.target.x-192*FRACUNIT, actor.target.y+192*FRACUNIT, actor.target.z+64*FRACUNIT)
end

states[S_LARGEICEPOSA1] = {
sprite = SPR_DETN,
frame = A,
tics = 1,
action = A_BigIcePosA1,
nextstate = S_LARGEICEPOSA1
}

states[S_LARGEICEREFERENCE] = {
sprite = SPR_PUMI,
nextstate = S_LARGEICEREFERENCE
}







addHook("MapThingSpawn", function(actor, spawnpoint)

	/*
	Every tic, all of the bigIce pieces are teleported to coordinates relative to bigIceReference.
	bigIceReference is below the bigIce pieces, and should be the only object touching the conveyor
	belt, like a mushroom where the stem is bigIceReference, and the top is the bigIce pieces.
	*/
	local bigIceReference = P_SpawnMobj(actor.x, actor.y, actor.z-64*FRACUNIT, MT_ICEPLATHITBOX)
	bigIceReference.state = S_LARGEICEREFERENCE
	
	

	local bigIceA1 = P_SpawnMobj(actor.x-192*FRACUNIT, actor.y+192*FRACUNIT, actor.z, MT_ICEPLATHITBOX)
	bigIceA1.target = bigIceReference
	bigIceA1.state = S_LARGEICEPOSA1
	
	/*
	local bigIceA2 = P_SpawnMobj(actor.x-192*FRACUNIT, actor.y+64*FRACUNIT, actor.z, MT_ICEPLATHITBOX)
	local bigIceA3 = P_SpawnMobj(actor.x-192*FRACUNIT, actor.y-64*FRACUNIT, actor.z, MT_ICEPLATHITBOX)
	local bigIceA4 = P_SpawnMobj(actor.x-192*FRACUNIT, actor.y-192*FRACUNIT, actor.z, MT_ICEPLATHITBOX)
	local bigIceB1 = P_SpawnMobj(actor.x-64*FRACUNIT, actor.y+192*FRACUNIT, actor.z, MT_ICEPLATHITBOX)
	local bigIceB2 = P_SpawnMobj(actor.x-64*FRACUNIT, actor.y+64*FRACUNIT, actor.z, MT_ICEPLATHITBOX)
	local bigIceB3 = P_SpawnMobj(actor.x-64*FRACUNIT, actor.y-64*FRACUNIT, actor.z, MT_ICEPLATHITBOX)
	local bigIceB4 = P_SpawnMobj(actor.x-64*FRACUNIT, actor.y-192*FRACUNIT, actor.z, MT_ICEPLATHITBOX)
	local bigIceC1 = P_SpawnMobj(actor.x+64*FRACUNIT, actor.y+192*FRACUNIT, actor.z, MT_ICEPLATHITBOX)
	local bigIceC2 = P_SpawnMobj(actor.x+64*FRACUNIT, actor.y+64*FRACUNIT, actor.z, MT_ICEPLATHITBOX)
	local bigIceC3 = P_SpawnMobj(actor.x+64*FRACUNIT, actor.y-64*FRACUNIT, actor.z, MT_ICEPLATHITBOX)
	local bigIceC4 = P_SpawnMobj(actor.x+64*FRACUNIT, actor.y-192*FRACUNIT, actor.z, MT_ICEPLATHITBOX)
	local bigIceD1 = P_SpawnMobj(actor.x+192*FRACUNIT, actor.y+192*FRACUNIT, actor.z, MT_ICEPLATHITBOX)
	local bigIceD2 = P_SpawnMobj(actor.x+192*FRACUNIT, actor.y+64*FRACUNIT, actor.z, MT_ICEPLATHITBOX)
	local bigIceD3 = P_SpawnMobj(actor.x+192*FRACUNIT, actor.y-64*FRACUNIT, actor.z, MT_ICEPLATHITBOX)
	local bigIceD4 = P_SpawnMobj(actor.x+192*FRACUNIT, actor.y-192*FRACUNIT, actor.z, MT_ICEPLATHITBOX)
	*/
	
	local largeCeilingVisual = P_SpawnMobj(actor.x, actor.y, actor.z+64*FRACUNIT, MT_LARGEICECEILING)
	largeCeilingVisual.flags2 = MF2_SPLAT
	largeCeilingVisual.renderflags = RF_NOSPLATBILLBOARD
	largeCeilingVisual.target = actor
	
	/*
	spawnIcePlatCollision(bigIceA1)
	spawnIcePlatCollision(bigIceA2)
	spawnIcePlatCollision(bigIceA3)
	spawnIcePlatCollision(bigIceA4)
	spawnIcePlatCollision(bigIceB1)
	spawnIcePlatCollision(bigIceB2)
	spawnIcePlatCollision(bigIceB3)
	spawnIcePlatCollision(bigIceB4)
	spawnIcePlatCollision(bigIceC1)
	spawnIcePlatCollision(bigIceC2)
	spawnIcePlatCollision(bigIceC3)
	spawnIcePlatCollision(bigIceC4)
	spawnIcePlatCollision(bigIceD1)
	spawnIcePlatCollision(bigIceD2)
	spawnIcePlatCollision(bigIceD3)
	spawnIcePlatCollision(bigIceD4)
	*/
	
end, MT_LARGEICESPAWNER)







mobjinfo[MT_CBUBSPAWNER] = {
doomednum = 4001,
spawnstate = S_CBUBSPAWNER,
radius = 32*FRACUNIT,
height = 32*FRACUNIT,
flags = MF_NOGRAVITY|MF_NOTHINK
}

mobjinfo[MT_CBUBREF] = {
doomednum = 4003,
spawnstate = S_CBUBREF,
radius = 32*FRACUNIT,
height = 32*FRACUNIT,
flags = MF_NOGRAVITY|MF_NOTHINK
}

mobjinfo[MT_CURRENTBUBBLE] = {
doomednum = 4002,
spawnstate = S_CBUB,
radius = 32*FRACUNIT,
height = 32*FRACUNIT,
flags = MF_NOGRAVITY
}



states[S_CBUB] = {
sprite = SPR_FBUB,
frame = A,
tics = 70,
action = A_Thrust,
var1 = 16,
var2 = 1,
nextstate = S_CBUBSPAWNER
}



states[S_CBUBREF] = {
sprite = SPR_FBUB,
frame = A,
tics = -1,
nextstate = S_CBUBREF
}


states[S_CBUBSPAWNER] = {
sprite = SPR_FBUB,
frame = A,
tics = 1,
action = A_ResetBubble,
nextstate = S_CBUB
}




addHook("MapThingSpawn", function(actor, spawnpoint)
local cbubble = P_SpawnMobj(actor.x, actor.y, actor.z, MT_CURRENTBUBBLE)
cbubble.angle = actor.angle
local cbubreference = P_SpawnMobj(actor.x, actor.y, actor.z, MT_CBUBREF)
cbubreference.angle = actor.angle
cbubble.target = cbubreference


end, MT_CBUBSPAWNER)


/*
Teleports bubble to its target (cbubref)
*/
local function A_ResetBubble(actor, spawnpoint)
	P_TeleportMove(actor, actor.target.x, actor.target.y, actor.target.z)
end


