inherit"obj/treasure";

#define SPELLS "players/auronthas/spells/spells"

string spells;

reset(ar)
{
  if(ar) return;
  name="book";
  alias_name="tome";
  short_desc="spell book";
  long_desc="This is an ancient book of spells for use by the\n"+
            "magicians of Collegium Magicus.\n"+
            "Type 'read index' to see what's in the book.\n";
  local_weight=5;
  info="This is a spell book.\n";
}

get() { return; }

init()
{
  add_action("read_me","read");
  add_action("study","study");
}

read_me(str)
{
  int page,t;
  if(str=="index") {
    write("The book's contents are:\n");
    for(t=0;t<sizeof(spells);t++) {
      write("Page "+extract((t+1)+"  ",0,2)+": "+capitalize(spells[t])+"\n");
    }
    write("Type 'read <page>' to get info, 'study spell name' to learn.\n");
    say(this_player()->query_name()+" reads the index of the book.\n");
    return 1;
  }
  notify_fail("Type 'read index' or 'read <page>', where <page> is a number.\n");
  if(!sscanf(str,"%d",page)) return;
  notify_fail("Not that many pages!\n");
  if(page > sizeof(spells)) return;
  notify_fail("Please be reasonable..\n");
  if(page < 1) return;
  page--;
  write(page+" "+spells[page]+"\n");
  SPELLS->spell_help(SPELLS->regognize_spell_name(spells[page]));
  say(this_player()->query_name()+" reads about the spell "+spells[page]+".\n");
  return 1;
}

study(str)
{
  int t,c,level,spell,xp1,xp2,xp3;
  string school;
  object skill;
  notify_fail("You are unable to learn spells!\n");
  skill=present("spell_skill",this_player());
  if(!skill) return;
  notify_fail("Please give a spell to study.\n");
  if(!str) return;
  str=lower_case(str);
  notify_fail("No spell of that name in the book!\n");
  for(t=0;t<sizeof(spells);t++) {
    if(str==spells[t]) {
      spell=SPELLS->recognize_spell_name(str);
      level=SPELLS->query_spell_level(spell);
      school=SPELLS->query_school(spell);
      if(test_bit(skill->query_known(),spell)) {
	write("You already know that spell!\n");
	return 1;
      }
      if(school == skill->query_forbidden()) {
	write("You can't learn "+school+" spells!\n");
	return 1;
      }
      if(school == skill->query_specialization())
	level-=2;
      else
	if(skill->query_specialization() != "none")
	  level++;
      if(level < 1)
	level=1;
      if(this_player()->query_level() < level) {
	write("You are not high enough level to learn that spell.\n"+
	      "Level "+level+" is required.\n");
	return 1;
      }
      if(this_player()->query_int() < level) {
	write("You are not intelligent enough to understand the concept of the spell.\n"+
	      "Intelligence required is "+level+".\n");
	return 1;
      }
      xp1=(level*10)*(level*10);
      xp2=this_player()->query_exp();
      if(this_player()->query_level() == 1)
	xp3=0;
      else {
	c=this_player()->query_level();
	if(c > 19) c=19;
	xp3="room/adv_guild"->get_next_exp(c);
      }
      if((xp2-xp1) < xp3) {
	write("You have not enough experience!\n");
	write("This spell costs "+xp1+" experience points to learn, and you only\n"+
	      "have "+(xp2-xp3)+" to spend.\n");
	return 1;
      }
      this_player()->add_exp(-xp1);
      c=skill->learn_spell(spell);
      if(c == 0) {
	write("You learn "+str+".\nCongratulations!\n");
	return 1;
      } else {
	write("Unable to learn spell!\nPlease send a bug report!|n");
	return 1;
      }
    }
  }
}