//Diamond's code, plus spawn code from vehicles...
freeslot("MT_DIAMOND")
freeslot("sfx_diamnd")

//Note that in the mapload function, the angle is multiplied by FRACUNIT... on occasions where it doesnt work, anyway.

local function diamondspawn(aim, angle, x2, y2, z2, addtimer)
	local mo = P_SpawnMobj(x2*FRACUNIT, y2*FRACUNIT, z2*FRACUNIT, MT_DIAMOND)
	if (mo and mo.valid)
		mo.angle = FixedAngle(angle)
		mo.aiming = aim
		mo.scale = mo.scale*2
		mo.scaleinitialise = true
		if not mo.wisptimerset then mo.wisptimerset = addtimer end
		mo.flags = $1|MF_NOCLIPTHING
	end
end

addHook("MobjThinker", function(mo)
	if not mo.ambcheck then mo.ambcheck = false end
	if not mo.flipcheck then mo.flipcheck = false end
	if not mo.scaleinitialise then
		mo.scale = mo.scale*2 
		mo.scaleinitialise = true
	end
	if not mo.wisptimerset then mo.wisptimerset = 0 end
	if (mo.flags2 & MF2_AMBUSH)
	and not (mo.ambcheck)
		mo.aiming = 90
		mo.ambcheck = true
		mo.flags2 = $1 & ~(MF2_AMBUSH)
	end
	if (mo.flags2 & MF2_OBJECTFLIP)
	and not (mo.flipcheck)
		mo.aiming = -(mo.aiming)
		mo.flags2 = $1 & ~(MF2_OBJECTFLIP)
		mo.flipcheck = true
	end
end, MT_DIAMOND)

local function DiamondHandler(special, toucher)
if (toucher.player.laserzoom == true)
		toucher.player.dz = special.aiming
		toucher.angle = special.angle
		if (special.wisptimerset)
			toucher.player.wisptimer = special.wisptimerset
		end
		S_StartSound(toucher, sfx_diamnd)
		if (P_CheckSight(toucher, special))
			toucher.flags = $1|MF_NOCLIPTHING
			P_TeleportMove(toucher, special.x, special.y, special.z + special.height/2)
			toucher.flags = $1 & ~(MF_NOCLIPTHING)
		end
	end
	return true
end

addHook("TouchSpecial", DiamondHandler, MT_DIAMOND)

local function box(x, y, z, obj)
	P_SpawnMobj(x*FRACUNIT, y*FRACUNIT, z*FRACUNIT, obj)
end

addHook("MapLoad", function(mapnum)
	if G_BuildMapName(mapnum) == "MAP01" then
		-- Greenflower 1
		box(1132, 5354, 103, MT_BURSTBOX)
		box(7624, 7146, 765, MT_BURSTBOX)
	elseif G_BuildMapName(mapnum) == "MAP02" then
		-- Greenflower 2
		box(-978, 5863, 0, MT_ROCKETBOX)
		box(-978, 5863, 1000, MT_ROCKETBOX)
		box(1136, 4072, 1536, MT_ROCKETBOX)
		box(-1362, -1626, 258, MT_DRILLBOX)
		box(2695, -1906, 2048, MT_BOOSTBOX)
		box(-166, -3565, 726, MT_ROCKETBOX)
	elseif G_BuildMapName(mapnum) == "MAP03" then
		-- Greenflower 3
		box(825, 637, 42, MT_ASTEROIDBOX)
		box(-825, 637, 42, MT_ASTEROIDBOX)
		box(825, -1022, 42, MT_ASTEROIDBOX)
		box(-825, -1022, 42, MT_ASTEROIDBOX)
	elseif G_BuildMapName(mapnum) == "MAP04" then
		-- Techno Hill 1
		box(1139, -2237, 2148, MT_BURSTBOX)
		box(478, -5280, 2368, MT_BOOSTBOX)
		box(310, -4211, 3072, MT_BURSTBOX)
		box(-6343, -6424, 1948, MT_BURSTBOX)
		box(-6343, -6603, 1948, MT_LASERBOX)
		diamondspawn(90, 180*FRACUNIT, -6918, -6603, 1948, 14*TICRATE)
		diamondspawn(0, 180*FRACUNIT, -6918, -6603, 3660, 100)
		box(-4458, -12692, 1984, MT_BURSTBOX)
		box(-1927, -12917, 1602, MT_BOOSTBOX)
		box(-2095, -12785, 1602, MT_BOOSTBOX)
		box(-4125, -11302, 2302, MT_ASTEROIDBOX)
		box(1564, -10039, 1920, MT_DRILLBOX)
		box(4485, -6194, 2188, MT_ROCKETBOX)
		box(4485, -6194, 3188, MT_ROCKETBOX)
		box(6311, -7382, 2108, MT_ROCKETBOX)
		diamondspawn(0, 315*FRACUNIT, 11750, -8317, 2069, 14*TICRATE)
		diamondspawn(0, 270*FRACUNIT, 12799, -9427, 2069, 14*TICRATE)
		diamondspawn(0, 0*FRACUNIT, 12799, -10566, 2069, 35*TICRATE)
		diamondspawn(0, 90*FRACUNIT, 20165, -10566, 2069, 100)
		box(10078, -8341, 1972, MT_LASERBOX)
		box(14908, -6830, 2508, MT_ROCKETBOX)
	elseif G_BuildMapName(mapnum) == "MAP05" then
		-- Techno Hill 2
		box(-17500, -3955, 448, MT_BURSTBOX)
		box(-16325, -4314, 1024, MT_BOOSTBOX)
		box(-8768, -8161, 1280, MT_DRILLBOX)
		box(210, -800, 1208, MT_DRILLBOX)
		box(210, -1120, 1208, MT_BURSTBOX)
		box(722, -8772, 16, MT_DRILLBOX)
		box(-2469, -12125, 1280, MT_LASERBOX)
		box(4081, -11693, 3840, MT_BOOSTBOX)
	elseif G_BuildMapName(mapnum) == "MAP06" then
		-- Techno Hill 3
		box(2138, -559, 152, MT_BURSTBOX)
	elseif G_BuildMapName(mapnum) == "MAP07" then
		-- Deep Sea 1
		box(-6975, -13645, 1316, MT_DRILLBOX)
		box(-5675, -11436, 1076, MT_ROCKETBOX)
		box(7876, -5862, 1000, MT_ROCKETBOX)
		box(9566, 1799, 2520, MT_DRILLBOX)
		box(5151, 4514, 1056, MT_DRILLBOX)
		box(-7034, -4985, 2750, MT_BOOSTBOX)
		box(-7140, -6458, 2656, MT_DRILLBOX)
		box(-11936, -8900, 968, MT_ROCKETBOX)
		box(-12136, -8900, 2748, MT_ASTEROIDBOX)
		box(-13646, -8447, 2622, MT_DRILLBOX)
		box(-13374, -5238, 3578, MT_YELLOWSPRING) //yes, i know its not a box, it's an additional spring to access the rocket box that it hides.
		box(-13286, -5248, 3900, MT_ROCKETBOX)
		box(-7968, -846, 1040, MT_DRILLBOX)
		box(-5531, 3122, 1055, MT_ASTEROIDBOX)
		box(2311, 4702, 544, MT_DRILLBOX)
		box(12140, 8046, -320, MT_ROCKETBOX)
		box(12140, 8046, 1000, MT_ROCKETBOX)
	elseif G_BuildMapName(mapnum) == "MAP08" then
		-- Deep Sea 2
		box(-1538, -6432, 856, MT_DRILLBOX)
		box(9709, 1071, 1312, MT_LASERBOX)
		box(13553, 4334, 1152, MT_LASERBOX)
		box(5890, 21383, 1024, MT_LASERBOX)
		box(1573, 17191, 2472, MT_DRILLBOX)
		box(2678, 15744, 700, MT_DRILLBOX)
		box(-10593, 18349, 684, MT_BOOSTBOX)
	elseif G_BuildMapName(mapnum) == "MAP09" then
		-- Deep Sea 3
		box(0, 0, 576, MT_LASERBOX)
	elseif G_BuildMapName(mapnum) == "MAP10" then
		-- Castle Eggman 1
		box(-5110, -11148, -1024, MT_ROCKETBOX)
		box(3759, -11390, -928, MT_BURSTBOX)
		box(-3264, -5742, -384, MT_DRILLBOX)
		box(4074, -5711, 0, MT_ROCKETBOX)
		box(-3736, -3825, 512, MT_BURSTBOX)
		box(-6242, -4070, -384, MT_BURSTBOX)
		box(-8518, -3326, -384, MT_BURSTBOX)
		box(-8443, -2673, 320, MT_BURSTBOX)
		box(-7578, 7448, 1705, MT_BOOSTBOX)
		box(-7295, 4864, 152, MT_BURSTBOX)
		box(-4806, 9655, 288, MT_LASERBOX)
		diamondspawn(90, 0*FRACUNIT, -4662, 9655, 320, 14*TICRATE)
		diamondspawn(0, 0*FRACUNIT, -4662, 9655, 672, 28*TICRATE)
		diamondspawn(0, 315*FRACUNIT, 660, 9655, 672, 28*TICRATE)
		diamondspawn(-90, 315*FRACUNIT, 1235, 9099, 672, 28)
		box(13371, 12248, 1536, MT_BURSTBOX)
		box(20300, 13329, 3072, MT_BOOSTBOX)
	elseif G_BuildMapName(mapnum) == "MAP11" then
		-- Castle Eggman 2
		box(-23209, -19175, 0, MT_ASTEROIDBOX)
		box(-19649, -19488, -160, MT_BURSTBOX)
		box(-18765, -20883, 1160, MT_BURSTBOX)
		box(-17667, -22176, 1523, MT_BURSTBOX)
		box(-18241, -23054, 2096, MT_BURSTBOX)
		box(-19226, -22618, 2416, MT_BURSTBOX)
		box(-19781, -22268, 2748, MT_BURSTBOX)
		box(-19781, -22204, 2748, MT_ROCKETBOX)
		box(-26138, -20299, 856, MT_BOOSTBOX)
		box(-26206, -17641, 2008, MT_BOOSTBOX)
		box(-23969, -16181, 1768, MT_ASTEROIDBOX)
		box(-21157, -17865, 2624, MT_BURSTBOX)
		box(-21411, -12171, 3280, MT_ASTEROIDBOX)
		box(-19239, -9845, 3840, MT_BOOSTBOX)
		box(-18395, -12973, 2624, MT_LASERBOX)
		box(-7295, -22455, 464, MT_DRILLBOX)
		box(-9509, -20863, 960, MT_DRILLBOX)
		box(-6053, -21713, 1248, MT_DRILLBOX)
		box(-5349, -17726, 1504, MT_DRILLBOX)
		box(-8974, -15104, 2624, MT_BOOSTBOX)
		box(-8027, -13919, 3008, MT_LASERBOX)
		box(-7404, -12449, 2944, MT_BURSTBOX)
		box(-2899, -16129, 2816, MT_ASTEROIDBOX)
		P_SpawnMobj(-2408*FRACUNIT, -14051*FRACUNIT, 3360*FRACUNIT, MT_REDCRAWLA)
		box(-2504, -14051, 3360, MT_LASERBOX)
		box(318, -11844, 4960, MT_BURSTBOX)
		box(1698, -9225, 3968, MT_BOOSTBOX)
		box(2228, -11012, 3968, MT_ASTEROIDBOX)
		box(4517, -7985, 4224, MT_BOOSTBOX)
		box(1927, -12354, 3424, MT_BOOSTBOX)
		box(9221, -9927, 3808, MT_BURSTBOX)
		box(3173, -6952, 3872, MT_BURSTBOX)
		box(3253, -6952, 3872, MT_ASTEROIDBOX)
		box(3093, -6952, 3872, MT_BOOSTBOX)
		box(4860, -3818, 3872, MT_BURSTBOX)
		box(4988, -3818, 3872, MT_ASTEROIDBOX)
		box(5100, -3818, 3872, MT_BOOSTBOX)
		-- (2.1 only) P_SpawnMobj(5463, -8546, 3872, MT_PRUP) -- MT_PRUP no longer exists in 2.2
		box(11965, -8095, 4656, MT_LASERBOX)
		box(10505, -10561, 3808, MT_BOOSTBOX)
		box(10720, -12385, 3553, MT_BURSTBOX)
		box(11297, -12738, 4064, MT_BOOSTBOX)
		box(16213, -13316, 4096, MT_BURSTBOX)
		box(13250, -9907, 4800, MT_BURSTBOX)
		box(15034, -10925, 6560, MT_BURSTBOX)
	elseif G_BuildMapName(mapnum) == "MAP12" then
		-- Castle Eggman 3
		box(417, -417, 960, MT_BURSTBOX)
		box(393, 393, 960, MT_BURSTBOX)
		box(-417, 393, 960, MT_BURSTBOX)
		box(-393, -417, 960, MT_BURSTBOX)
	elseif G_BuildMapName(mapnum) == "MAP13" then
		-- Arid Canyon 1
		box(2219, -10629, 1520, MT_BURSTBOX)
		box(379, -1528, 3264, MT_BOOSTBOX)
		box(-6934, 1327, 4232, MT_BURSTBOX)
		box(-11841, -9138, 4896, MT_BURSTBOX)
		box(-9422, 14840, 5776, MT_ASTEROIDBOX)
		box(6415, 11167, 3696, MT_BURSTBOX)
		box(1483, 11495, 3544, MT_BURSTBOX)
		box(7386, 9855, 3744, MT_BURSTBOX)
	elseif G_BuildMapName(mapnum) == "MAP16" then
		-- Red Volcano 1
		box(190, 744, 2504, MT_BURSTBOX)
		box(0, 1915, 2304, MT_BURSTBOX)
		box(5000, -5592, 1648, MT_BURSTBOX)
		box(-12524, 6540, 1285, MT_BURSTBOX)
	elseif G_BuildMapName(mapnum) == "MAP20" then
		-- Tutorial Level
		diamondspawn(-90, 0, 4608, 9792, 1510, 14)
	elseif G_BuildMapName(mapnum) == "MAP22" then
		-- Egg Rock 1
		box(-5378, -12364, 1792, MT_BURSTBOX)
		local rgrckt = P_SpawnMobj(-6678*FRACUNIT, -11284*FRACUNIT, 4016*FRACUNIT, MT_ROCKETBOX)
		//if (rgrckt and rgrckt.valid)
		rgrckt.flags2 = $1|MF2_OBJECTFLIP
		//end
		box(-8286, -11220, 1536, MT_ASTEROIDBOX)
		box(-7236, -2927, 192, MT_BURSTBOX)
	elseif G_BuildMapName(mapnum) == "MAP23" then
		-- Egg Rock 2
		box(8365, 10792, 1372, MT_LASERBOX)
		box(-3760, 23293, 3072, MT_ASTEROIDBOX)
		box(-11874, 2724, 874, MT_LASERBOX)
		box(-11745, 2724, 874, MT_ASTEROIDBOX)
		box(-12003, 2724, 874, MT_BURSTBOX)
		box(-11935, 2724, 874, MT_ROCKETBOX)
		box(-11811, 2724, 874, MT_BOOSTBOX)
	elseif G_BuildMapName(mapnum) == "MAP24" then
		-- Egg Rock 3
		box(38, -3529, 264, MT_BOOSTBOX)
		box(-4809, -2928, 0, MT_BOOSTBOX)
		box(-8970, -482, 192, MT_BOOSTBOX)
		local reverseboost = P_SpawnMobj(-6700*FRACUNIT, -7047*FRACUNIT, 720*FRACUNIT, MT_BOOSTBOX)
		reverseboost.flags2 = $1|MF2_OBJECTFLIP
		local reverseboost2 = P_SpawnMobj(-5592*FRACUNIT, -13787*FRACUNIT, 1464*FRACUNIT, MT_BOOSTBOX)
		reverseboost2.flags2 = $1|MF2_OBJECTFLIP
		box(-14726, -10275, 192, MT_BOOSTBOX)
		P_SpawnMobj(-12704*FRACUNIT, 3087*FRACUNIT, -1920*FRACUNIT, MT_ASTEROIDBOX)
	elseif G_BuildMapName(mapnum) == "MAP25" then
		-- Egg Rock Core
		box(0, -500, 0, MT_LASERBOX)
		box(0, 500, 0, MT_ASTEROIDBOX)
		box(-500, 0, 0, MT_BURSTBOX)
		box(500, 0, 0, MT_BOOSTBOX)
	end
end)