Mana = 100.0
Think = 0
LastCast = 0
CurrentSpell = "death ray"
OverDrive = false

function onInit( )
end

function Holster()
	_GModText_Hide(Owner, 8988)
end

function onThink( )
	if _CurTime() >= Think+0.35 then
		if Mana < 100 then
			if OverDrive then
				Mana = Mana+33
			else
				Mana = Mana+3.9
			end
		end
		DrawMana()
		Think = _CurTime()
	end
	if _PlayerIsKeyDown(Owner, IN_ATTACK2) and _PlayerIsKeyDown(Owner, IN_ATTACK) then
		DrawSelectSpells2()
		if _PlayerIsKeyDown(Owner, IN_FORWARD)  and _PlayerIsKeyDown(Owner, IN_MOVERIGHT) then
			SelectSpell("shock")
		elseif _PlayerIsKeyDown(Owner, IN_FORWARD) and _PlayerIsKeyDown(Owner, IN_MOVELEFT) then
			SelectSpell("meteor")
		elseif _PlayerIsKeyDown(Owner, IN_FORWARD) then
			SelectSpell("inversion")
		elseif _PlayerIsKeyDown(Owner, IN_BACK) and _PlayerIsKeyDown(Owner, IN_MOVERIGHT) then
			SelectSpell("teleport")
		elseif _PlayerIsKeyDown(Owner, IN_BACK) and _PlayerIsKeyDown(Owner, IN_MOVELEFT) then
			SelectSpell("energy bolt")
		elseif _PlayerIsKeyDown(Owner, IN_BACK) then
			SelectSpell("counter spell")
		elseif _PlayerIsKeyDown(Owner, IN_MOVELEFT) then
			SelectSpell("repel")
		elseif _PlayerIsKeyDown(Owner, IN_MOVERIGHT) then
			SelectSpell("slow")
		end 
	elseif _PlayerIsKeyDown(Owner, IN_ATTACK2) and not _PlayerIsKeyDown(Owner, IN_ATTACK) then
		DrawSelectSpells()
		if _PlayerIsKeyDown(Owner, IN_FORWARD)  and _PlayerIsKeyDown(Owner, IN_MOVERIGHT) then
			SelectSpell("shield")
		elseif _PlayerIsKeyDown(Owner, IN_FORWARD) and _PlayerIsKeyDown(Owner, IN_MOVELEFT) then
			SelectSpell("lightning")
		elseif _PlayerIsKeyDown(Owner, IN_FORWARD) then
			SelectSpell("death ray")
		elseif _PlayerIsKeyDown(Owner, IN_BACK) and _PlayerIsKeyDown(Owner, IN_MOVERIGHT) then
			SelectSpell("air hike")
		elseif _PlayerIsKeyDown(Owner, IN_BACK) and _PlayerIsKeyDown(Owner, IN_MOVELEFT) then
			SelectSpell("invisibility")
		elseif _PlayerIsKeyDown(Owner, IN_BACK) then
			SelectSpell("heal")
		elseif _PlayerIsKeyDown(Owner, IN_MOVELEFT) then
			SelectSpell("blink")
		elseif _PlayerIsKeyDown(Owner, IN_MOVERIGHT) then
			SelectSpell("protrusion")
		end
	else
		_GModRect_Hide(Owner, 8988)
	end
end

function SelectSpell(str)
	CurrentSpell = str
	_PrintMessage(Owner, 4, "Current Spell: "..string.upper(str))
end

function onPrimaryAttack()
	if OverDrive then
		if _CurTime() < LastCast+0.1 or _PlayerIsKeyDown(Owner, IN_ATTACK2) then return end
	else
		if _CurTime() < LastCast+1 or _PlayerIsKeyDown(Owner, IN_ATTACK2) then return end
	end
	local ManaCost = 0
	if CurrentSpell == "death ray" then ManaCost = 60
	elseif CurrentSpell == "shield" then ManaCost = 80
	elseif CurrentSpell == "lightning" then ManaCost = 35
	elseif CurrentSpell == "blink" then ManaCost = 30
	elseif CurrentSpell == "protrusion" then ManaCost = 45
	elseif CurrentSpell == "air hike" then ManaCost = 45
	elseif CurrentSpell == "invisibility" then ManaCost = 70
	elseif CurrentSpell == "heal" then ManaCost = 45
	elseif CurrentSpell == "meteor" then ManaCost = 50
	elseif CurrentSpell == "shock" then ManaCost = 35
	elseif CurrentSpell == "inversion" then ManaCost = 25
	elseif CurrentSpell == "counter spell" then ManaCost = 30
	elseif CurrentSpell == "energy bolt" then ManaCost = 50
	elseif CurrentSpell == "slow" then ManaCost = 35
	elseif CurrentSpell == "repel" then ManaCost = 30
	elseif CurrentSpell == "teleport" then ManaCost = 40 end

	if Mana < ManaCost then
		_PrintMessage(Owner, 4, "Insufficient Mana")
		return
	end
	Mana = Mana - ManaCost
	_RunString("CastSpell("..Number()..", "..Owner..")")
	LastCast = _CurTime()
end

function Number(str)
	if CurrentSpell == "death ray" then return 1
	elseif CurrentSpell == "shield" then return 2
	elseif CurrentSpell == "lightning" then return 3
	elseif CurrentSpell == "blink" then return 4
	elseif CurrentSpell == "protrusion" then return 5
	elseif CurrentSpell == "air hike" then return 6
	elseif CurrentSpell == "invisibility" then return 7
	elseif CurrentSpell == "heal" then return 8
	elseif CurrentSpell == "shock" then return 9
	elseif CurrentSpell == "meteor" then return 10
	elseif CurrentSpell == "inversion" then return 11
	elseif CurrentSpell == "teleport" then return 12
	elseif CurrentSpell == "energy bolt" then return 13
	elseif CurrentSpell == "counter spell" then return 14
	elseif CurrentSpell == "repel" then return 15
	elseif CurrentSpell == "slow" then return 16 end
end

function onSecondaryAttack()
end

function DrawSelectSpells()
_GModRect_Start("mage/spellselect")
 _GModRect_SetPos(0.4, 0.75, 0.2, 0.21)
 _GModRect_SetColor(0, 0, 0, 255)
 _GModRect_SetTime(10, 0, 0)
_GModRect_Send(Owner, 8988)
end

function DrawSelectSpells2()
_GModRect_Start("mage/spellselect2")
 _GModRect_SetPos(0.4, 0.75, 0.2, 0.21)
 _GModRect_SetColor(0, 0, 0, 255)
 _GModRect_SetTime(10, 0, 0)
_GModRect_Send(Owner, 8988)
end

function DrawMana()
if Owner <= 0 then return end
_GModRect_Start("gmod/white")
 _GModRect_SetPos(0.41, 0.96, Mana*0.0018, 0.05)
 _GModRect_SetColor( 0, 0, 255, 255 )
 _GModRect_SetTime(9999, 0, 0)
_GModRect_Send(Owner, 1801)
end

function onReload( )
	return false
end

function getWeaponSwapHands()
	return false	
end

function getWeaponFOV() return 75 end
function getWeaponSlot() return 5 end
function getWeaponSlotPos() return 6 end
function getFiresUnderwater() return true end
function getReloadsSingly() return false end
function getDamage() return 10 end
function getPrimaryShotDelay()	return 0.1 end
function getPrimaryIsAutomatic() return false	end
function getBulletSpread() return vector3( 0.00, 0.00, 0.00 ) end
function getViewKick()	return vector3( 0.0, 0.0, 0.0) end
function getViewKickRandom() return vector3( 0.0, 0.0, 0.0 ) end
function getNumShotsPrimary() return 1 end
function getPrimaryAmmoType() return "none" end
function getDamageSecondary() return 0 end
function getSecondaryShotDelay() return 1 end
function getSecondaryIsAutomatic()	return false end
function getBulletSpreadSecondary() return vector3( 0.001, 0.001, 0.001 ) end
function getViewKickSecondary() return vector3( 0.5, 0.0, 0.0) end
function getViewKickRandomSecondary() return vector3( 0.0, 0.0, 0.0 ) end
function getNumShotsSecondary() return 1 end
function getSecondaryAmmoType() return "none" end
function getViewModel( ) return "models/weapons/v_stunbaton.mdl" end
function getWorldModel( ) return "models/weapons/w_stunbaton.mdl" end
function getClassName() return "mage_wand" end
function getMaxClipPrimary() return 1 end
function getMaxClipSecondary()	return -1 end
function getDefClipPrimary() return 1	end
function getDefClipSecondary()	return 0 end
function getAnimPrefix() return "melee" end
function getPrintName() return "Mage's Wand" end
function getPrimaryScriptOverride() return 3 end
function getSecondaryScriptOverride() return 3 end
function getDeathIcon( ) return "d_magegeneric" end
