//welcome to the domain of the unused and unfinished

//sage capture table
if not sagecrawladex
	rawset(_G,"sagecrawladex", {})
end

local GENERIC_LAND = -1
local GENERIC_AIR = -2
local GENERIC_NOMOVE = -3

local function crwaltopia(player)
	local grounded = P_IsObjectOnGround(player.mo)
	local crawlathrust = (grounded) and FRACUNIT or FRACUNIT>>1
	player.powers[pw_justsprung] = 1
	player.normalspeed = 0
	player.thrustfactor = 0
	player.accelstart = 0
	player.acceleration = 0
	player.jumpfactor = FRACUNIT*7/10
	if (player.speed) and grounded
		player.sage.control.flags = $ & ~MF_NOTHINK & ~MF_NOCLIPHEIGHT
	else
		player.sage.control.flags = $|MF_NOTHINK
	end
	if (player.speed > FixedMul(40<<FRACBITS, player.mo.scale)) and grounded
		P_SpawnSkidDust(player, player.sage.control.radius, sfx_skid)
	end
	if (player.cmd.sidemove) or (player.cmd.forwardmove)
		player.drawangle = R_PointToAngle2(0, 0, player.rmomx, player.rmomy)
		player.mo.friction = FRACUNIT*92/93
		if (player.pflags & PF_SPINDOWN) and grounded
			player.mo.momx = $*13/14
			player.mo.momy = $*13/14
			if (player.sage.control.turbo)
				player.sage.control.turbo = $+1
				if (player.sage.control.turbo == 2)
					player.sage.control.recrawlation = player.speed
				end
				if (player.sage.control.turbo > TICRATE/2) and (player.sage.control.turbo < TICRATE*2)
					if not (leveltime%4)
						local crwalBIG = P_SpawnGhostMobj(player.sage.control)
						crwalBIG.destscale = $<<2
					end
				end
			end
		else
			if (player.sage.control.turbo)
				if (player.sage.control.turbo > TICRATE/2) and (player.sage.control.turbo < TICRATE*2)
					P_InstaThrust(player.mo, player.mo.angle, player.sage.control.recrawlation)
					S_StartSound(player.mo, sfx_thok)
				end
			end
			if (player.speed > FixedMul(40<<FRACBITS, player.mo.scale))
				player.sage.control.turbo = 1
			else
				player.sage.control.turbo = 0
			end
		end
		if (twodlevel or (player.mo.flags2 & MF2_TWOD))
			 if (player.cmd.sidemove)
				P_Thrust(player.mo, player.drawangle, FixedMul(crawlathrust, player.mo.scale))
			end
		else
			P_Thrust(player.mo, (player.cmd.angleturn<<16 + R_PointToAngle2(0, 0, player.cmd.forwardmove*FRACUNIT, -player.cmd.sidemove*FRACUNIT)), FixedMul(crawlathrust, player.mo.scale))
		end
	else
		if grounded
			if not (player.pflags & PF_SPINDOWN)
				player.sage.control.turbo = 0
			end
			player.mo.momx = $*13/14
			player.mo.momy = $*13/14
		end
	end
end

sagecrawladex[GENERIC_LAND] = function(player)
	player.normalspeed = 36
	player.thrustfactor = 5
	player.accelstart = 96
	player.acceleration = 40
	crwaltopia(player)
end

sagecrawladex[GENERIC_AIR] = function(player)
	player.normalspeed = 32
	player.thrustfactor = 3
	player.accelstart = 192
	player.acceleration = 50
	player.mo.flags = $|MF_NOGRAVITY
end

sagecrawladex[GENERIC_NOMOVE] = function(player)
	player.normalspeed = 0
	player.thrustfactor = 0
	player.accelstart = 0
	player.acceleration = 0
end

sagecrawladex[MT_BLUECRAWLA] = function(player)
	crwaltopia(player)
end

sagecrawladex[MT_REDCRAWLA] = function(player)
	crwaltopia(player)
	if (player.sage.control.turbo) and (player.sage.control.turbo < TICRATE*2)
		player.sage.control.color = SKINCOLOR_TANGERINE
		player.sage.control.colorized = (leveltime%2) and true or false
		player.powers[pw_strong] = STR_ATTACK|STR_WALL|STR_SPIKE
	else
		player.sage.control.colorized = false
		player.sage.control.color = SKINCOLOR_NONE
		player.powers[pw_strong] = STR_NONE
	end
end

/* lavla note: basic battlemod support framework, someone else feel free to finish this (i don't play battlemod)
local battler = false
addHook("AddonLoaded", function()
	if CBW_Battle and not battler
		local function sagespecial(mo, special)
			local plyr = mo.player
			if (plyr.sage.control)
				plyr.actiontext = "explode"
				plyr.actionrings = 0
			else
				plyr.actiontext = "badnik"
				plyr.actionrings = 15
			end
			if P_PlayerInPain(plyr) or plyr.powers[pw_nocontrol] or (plyr.playerstate != PST_LIVE)
			or not CBW_Battle.CanDoAction(plyr)
				if plyr.actionstate
					CBW_Battle.ApplyCooldown(plyr, 2*(TICRATE))
				end
				plyr.actionstate = 0
				plyr.actiontime = 0
				return
			end
			if (special == 1) and not (plyr.actionstate)
				if (plyr.sage.control)
					plyr.actionstate = 2
				else
					plyr.actionstate = 1
				end
				plyr.actiontime = 0
				CBW_Battle.PayRings(plyr)
			end
			if (plyr.actionstate)
				if (plyr.actionstate == 2)
					pof(plyr.sage.control, mo)
					P_SetObjectMomZ(mo, 15<<FRACBITS, false)
					if (mo.eflags & MFE_UNDERWATER)
						mo.momz = $/2
					end
					plyr.pflags = $|P_GetJumpFlags(plyr) & ~PF_THOKKED & ~PF_STARTJUMP
					mo.state = S_PLAY_JUMP
					plyr.sage.control = 0
					plyr.powers[pw_carry] = CR_NONE
					plyr.sage.pop = MAX_POP
					plyr.sage.stock = 1
					plyr.glidetime = 0
					plyr.actionstate = 0
					CBW_Battle.ApplyCooldown(plyr, 3*TICRATE)
					return
				end
				local get = (plyr.pflags & PF_JUMPED) and P_SpawnMobjFromMobj(mo, 0, 0, 0, MT_PYREFLY) or P_SpawnMobjFromMobj(mo, 0, 0, 0, MT_BLUECRAWLA)
				get.flags = $|MF_NOTHINK|MF_NOBLOCKMAP|MF_NOCLIP
				plyr.sage.control = get
				plyr.actionstate = 0
				CBW_Battle.ApplyCooldown(plyr, TICRATE)
			end
		end
		//vars
		battler = true
		CBW_Battle.SkinVars["sage"] = {
			flags = 0,
			weight = 70,
			shields = 2,
			special = sagespecial,
			guard_frame = 1}
		print("\x87\Marine's BattleMod support has been added!")
	end
end)
*/

print("\x85\Sage v1 \x80\- \x89\Spritework by lightdasher, Bendede, and IKEA", "\x82\Code by candelavla, Buggie, GoldenShine, and Metakimi", "\x83\Sounds from Sonic Frontiers and RPG Maker")
