anchor.diff:  	w_class = 1.0
   	var/uses = 5
   	var/temp = null
  -	var/max_uses = 5
   	var/op = 1
  -	var/activepage
  -	var/list/active_challenges = list()
   	var/mob/living/carbon/human/owner
  +	var/list/datum/spellbook_entry/entries = list()
  +	var/list/categories = list()
  +
  +/obj/item/weapon/spellbook/New()
  +	..()
  +	var/entry_types = typesof(/datum/spellbook_entry) - /datum/spellbook_entry - /datum/spellbook_entry/item - /datum/spellbook_entry/summon
  +	for(var/T in entry_types)
  +		var/datum/spellbook_entry/E = new T
  +		if(E.IsAvailible())
  +			entries |= E
  +			categories |= E.category
  +		else
  +			del(E)
  +
   
   /obj/item/weapon/spellbook/attackby(obj/item/O as obj, mob/user as mob, params)
   	if(istype(O, /obj/item/weapon/antag_spawner/contract))
  
positive.code: 		user << "<span class='warning'>The [name] does not recognize you as it's owner and refuses to open!</span>"
  		return
  	user.set_machine(src)
  	var/dat
  	if(temp)
  		dat = "[temp]<BR><BR><A href='byond://?src=\ref[src];temp=1'>Clear</A>"
  	else if (!activepage || activepage == "return")
  		dat = "<B>The Book of Magic:</B><BR>"
  		dat += "Uses remaining: [uses]<BR>"
  		dat += "<HR>"
  		dat += "<A href='byond://?src=\ref[src];spell_choice=spells'>Learn and Improve Magical Abilities</A><BR>"
  		dat += "<A href='byond://?src=\ref[src];spell_choice=artifacts'>Summon Magical Tools and Weapons</A><BR>"
  		if(ticker.mode.name != "ragin' mages") // we totally need summon guns x100
  			dat += "<A href='byond://?src=\ref[src];spell_choice=challenge'>Raise The Stakes For More Power</A><BR>"
  		dat += "<HR>"
  		dat += "<A href='byond://?src=\ref[src];spell_choice=rememorize'>Re-memorize Spells</A><BR>"
  
  	else if (activepage == "spells")
  		dat += "<B>Spells:</B><BR>"
  		dat += "Spells that can be reused endlessly. Unless stated otherwise all spells require full wizard garb as a focus.<BR>"
  		dat += "The number after the spell name is the cooldown time. You can reduce this number by spending more points on the spell.<BR>"
  
  		dat += "<HR>"
  
  		dat += "<A href='byond://?src=\ref[src];spell_choice=magicmissile'>Magic Missile</A> (15)<BR>"
  		dat += "<I>This spell fires several, slow moving, magic projectiles at nearby targets. If they hit a target, it is paralyzed and takes minor damage.</I><BR>"
  
  		dat += "<A href='byond://?src=\ref[src];spell_choice=fireball'>Fireball</A> (10)<BR>"
  		dat += "<I>This spell fires a fireball in the direction you're facing and does not require wizard garb. Be careful not to fire it at people that are standing next to you.</I><BR>"
  
  		dat += "<A href='byond://?src=\ref[src];spell_choice=disintegrate'>Disintegrate</A> (60)<BR>"
  		dat += "<I>This spell instantly kills somebody adjacent to you with the vilest of magick. It has a long cooldown.</I><BR>"
  
  		dat += "<A href='byond://?src=\ref[src];spell_choice=disabletech'>Disable Technology</A> (60)<BR>"
  		dat += "<I>This spell disables all weapons, cameras and most other technology in range.</I><BR>"
  
  		dat += "<A href='byond://?src=\ref[src];spell_choice=repulse'>Repulse</A> (40)<BR>"
  		dat += "<I>This spell throws nearby objects away from you and knocks creatures down.</I><BR>"
  
  		dat += "<A href='byond://?src=\ref[src];spell_choice=smoke'>Smoke</A> (10)<BR>"
  		dat += "<I>This spell spawns a cloud of choking smoke at your location and does not require wizard garb.</I><BR>"
  
  		dat += "<A href='byond://?src=\ref[src];spell_choice=blind'>Blind</A> (30)<BR>"
  		dat += "<I>This spell temporarly blinds a single person and does not require wizard garb.</I><BR>"
  
  		dat += "<A href='byond://?src=\ref[src];spell_choice=mindswap'>Mind Transfer</A> (60)<BR>"
  		dat += "<I>This spell allows the user to switch bodies with a target. Careful to not lose your memory in the process.</I><BR>"
  
  		dat += "<A href='byond://?src=\ref[src];spell_choice=forcewall'>Forcewall</A> (10)<BR>"
  		dat += "<I>This spell creates an unbreakable wall that lasts for 30 seconds and does not need wizard garb.</I><BR>"
  
  		dat += "<A href='byond://?src=\ref[src];spell_choice=blink'>Blink</A> (2)<BR>"
  		dat += "<I>This spell randomly teleports you a short distance. Useful for evasion or getting into areas if you have patience.</I><BR>"
  
  		dat += "<A href='byond://?src=\ref[src];spell_choice=teleport'>Teleport</A> (60)<BR>"
  		dat += "<I>This spell teleports you to a type of area of your selection. Very useful if you are in danger, but has a decent cooldown, and is unpredictable.</I><BR>"
  
  		dat += "<A href='byond://?src=\ref[src];spell_choice=mutate'>Mutate</A> (60)<BR>"
  		dat += "<I>This spell causes you to turn into a hulk and gain telekinesis for a short while.</I><BR>"
  
  		dat += "<A href='byond://?src=\ref[src];spell_choice=etherealjaunt'>Ethereal Jaunt</A> (30)<BR>"
  		dat += "<I>This spell creates your ethereal form, temporarily making you invisible and able to pass through walls.</I><BR>"
  
  		dat += "<A href='byond://?src=\ref[src];spell_choice=knock'>Knock</A> (10)<BR>"
  		dat += "<I>This spell opens nearby doors and does not require wizard garb.</I><BR>"
  
  		dat += "<A href='byond://?src=\ref[src];spell_choice=barnyard'>Curse of the Barnyard</A> (15)<BR>"
  		dat += "<I> This Spell dooms any unlucky soul to the life of a barnyard animal. Well not exactly but you still get to laugh at them when they MOO!. It does not require a wizard garb.</I><BR>"
  
  		dat += "<A href='byond://?src=\ref[src];spell_choice=fleshtostone'>Flesh to Stone</A> (60)<BR>"
  		dat += "<I>This spell will curse a person to immediately turn into an unmoving statue. The effect will eventually wear off if the statue is not destroyed.</I><BR>"
  
  		dat += "<A href='byond://?src=\ref[src];spell_choice=lightningbolt'>Lightning Bolt</A> (30)<BR>"
  		dat += "<I>Charge up and throw lightning bolt at the nearby enemy. Classic. The longer you charge the more powerful the spell, beware of overcharge however!</I><BR>"
  
  		dat += "<A href='byond://?src=\ref[src];spell_choice=summonitem'>Instant Summons</A> (10)<BR>"
  		dat += "<I>This spell can be used to bind a valuable item to you, bringing it to your hand at will. Using this spell while holding the bound item will allow you to unbind it. It does not require wizard garb.</I><BR>"
  		if(ticker.mode.name != "ragin' mages" && !config.no_summon_events) // we totally need summon greentext x100
  			dat += "<A href='byond://?src=\ref[src];spell_choice=summonevents'>Summon Events</A> (One time use, persistent global spell)<BR>"
  			dat += "<I>Give Murphy's law a little push and replace all events with special wizard ones that will confound and confuse everyone. Multiple castings increase the rate of these events.</I><BR>"
  
  		dat += "<HR>"
  
  		dat += "<A href='byond://?src=\ref[src];spell_choice=return'><B>Return</B></A><BR>"
  
  	else if (activepage == "challenge")
  
  		dat += "<B>Challenges:</B><BR>"
  		dat += "The Wizard Federation typically has hard limits on the potency and number of spells brought to the station based on risk.<BR>"
  		dat += "Arming the station against you will increases the risk, but will grant you one more charge for your spellbook.<BR>"
  
  		dat += "<HR>"
  
  		if(!("summon guns" in active_challenges) && !config.no_summon_guns)
  			dat += "<A href='byond://?src=\ref[src];spell_choice=summonguns'>Summon Guns</A> (Single use only, global spell)<BR>"
  			dat += "<I>Nothing could possibly go wrong with arming a crew of lunatics just itching for an excuse to kill you. Just be careful not to stand still too long!</I><BR>"
  
  		if(!("summon magic" in active_challenges) && !config.no_summon_magic)
  			dat += "<A href='byond://?src=\ref[src];spell_choice=summonmagic'>Summon Magic</A> (Single use only, global spell)<BR>"
  			dat += "<I>Share the wonders of magic with the crew and show them why they aren't to be trusted with it at the same time.</I><BR>"
  
  		dat += "<HR>"
  
  		dat += "<A href='byond://?src=\ref[src];spell_choice=return'><B>Return</B></A><BR>"
  
  	else if (activepage == "artifacts")
  
  		dat += "<B>Artifacts:</B><BR>"
  		dat += "Powerful items imbued with eldritch magics. Summoning one will count towards your maximum number of uses.<BR>"
  		dat += "These items are not bound to you and can be stolen. Additionaly they cannot typically be returned once purchased.<BR>"
  		dat += "<HR>"
  
  		dat += "<A href='byond://?src=\ref[src];spell_choice=staffchange'>Staff of Change</A><BR>"
  		dat += "<I>An artefact that spits bolts of coruscating energy which cause the target's very form to reshape itself.</I><BR>"
  		dat += "<HR>"
  
  		dat += "<A href='byond://?src=\ref[src];spell_choice=staffanimation'>Staff of Animation</A><BR>"
  		dat += "<I>An arcane staff capable of shooting bolts of eldritch energy which cause inanimate objects to come to life. This magic doesn't affect machines.</I><BR>"
  		dat += "<HR>"
  
  		dat += "<A href='byond://?src=\ref[src];spell_choice=staffdoor'>Staff of Door Creation</A><BR>"
  		dat += "<I>A particular staff that can mold solid metal into ornate wooden doors. Useful for getting around in the absence of other transportation. Does not work on glass.</I><BR>"
  		dat += "<HR>"
  
  		dat += "<A href='byond://?src=\ref[src];spell_choice=staffchaos'>Staff of Chaos</A><BR>"
  		dat += "<I>A caprious tool that can fire all sorts of magic without any rhyme or reason. Using it on people you care about is not recommended.</I><BR>"
  		dat += "<HR>"
  
  		dat += "<A href='byond://?src=\ref[src];spell_choice=soulstone'>Six Soul Stone Shards and the spell Artificer</A><BR>"
  		dat += "<I>Soul Stone Shards are ancient tools capable of capturing and harnessing the spirits of the dead and dying. The spell Artificer allows you to create arcane machines for the captured souls to pilot.</I><BR>"
  		dat += "<HR>"
  
  		dat += "<A href='byond://?src=\ref[src];spell_choice=necrostone'>A Necromantic Stone</A><BR>"
  		dat += "<I>A Necromantic stone is able to resurrect three dead individuals as skeletal thralls for you to command.</I>"
  		dat += "<HR>"
  
  		dat += "<A href='byond://?src=\ref[src];spell_choice=wands'>Wand Assortment</A><BR>"
  		dat += "<I>A collection of wands that allow for a wide variety of utility. Wands do not recharge, so be conservative in use. Comes in a handy belt.</I><BR>"
  		dat += "<HR>"
  
  		dat += "<A href='byond://?src=\ref[src];spell_choice=armor'>Mastercrafted Armor Set</A><BR>"
  		dat += "<I>An artefact suit of armor that allows you to cast spells while providing more protection against attacks and the void of space.</I><BR>"
  		dat += "<HR>"
  
  		dat += "<A href='byond://?src=\ref[src];spell_choice=contract'>Contract of Apprenticeship</A><BR>"
  		dat += "<I>A magical contract binding an apprentice wizard to your service, using it will summon them to your side.</I><BR>"
  		dat += "<HR>"
  
  		dat += "<A href='byond://?src=\ref[src];spell_choice=scrying'>Scrying Orb</A><BR>"
  		dat += "<I>An incandescent orb of crackling energy, using it will allow you to ghost while alive, allowing you to spy upon the station with ease. In addition, buying it will permanently grant you x-ray vision.</I><BR>"
  
  		dat += "<HR>"
  
  		dat += "<A href='byond://?src=\ref[src];spell_choice=return'><B>Return</B></A><BR>"
  	user << browse(dat, "window=radio")
  	onclose(user, "radio")
  	return
  
  /obj/item/weapon/spellbook/Topic(href, href_list)
  
negative.code: 		temp = "If you got caught sneaking a peak from your teacher's spellbook, you'd likely be expelled from the Wizard Academy. Better not."
  		return
  
  	var/datum/spellbook_entry/E = null
  	if(loc == H || (in_range(src, H) && istype(loc, /turf)))
  		H.set_machine(src)
  		if(href_list["buy"])
  			E = entries[text2num(href_list["buy"])]
  			if(E && E.CanBuy(H,src))
  				if(E.Buy(H,src))
  					uses -= E.cost
  		else if(href_list["refund"])
  			E = entries[text2num(href_list["refund"])]
  			if(E && E.refundable)
  				var/result = E.Refund(H,src)
  				if(result > 0)
  					uses += result
  	attack_self(H)
  	return
  
  //Single Use Spellbooks//
  