/*
 * gEvent.js
 *
 * Copyright (c) 2008 Tyler de Witt (tyler-dewitt.com)
 * Unauthorized reproduction or use of this code is not permitted.
 */

function gEvent() {
  this.zIndex=15;
  this.walkable=false;
  this.object_type="gEvent";
  this.title="event";
  //for keeping track of what stuff is part of this event
  this.objs=new Array();
  this.vars=new Object();

  /* override to save objs */
  this.save=function() {
    var o=this.save_default("gEvent");
    var ids=new Array();
    for(i in this.objs) { 
      ids.push(this.objs[i].id);
    }
    o.push(ids);
    return o;
  }

  /* override to load objs*/
  this.load=function(o) {
    this.load_default(o,"gEvent");
    this.objs_ids=o.shift();
    if (this.objs_ids==null) this.objs_ids=new Array();
  }

  //reassociate the objs
  this.reassociate=function() {
    this.objs=new Array();
    for (i in this.objs_ids) {
      var o=g.oi(this.objs_ids[i]);
      if (o != null) { this.objs.push(o); }
    }
    this.objs_ids=new Array();
  }

  this.destroy=function() {
    //something to make this disappear
    g.unregister_object(this);
  }

  //stub
  this.act=function() { this.destroy(); };
}

gEvent.prototype=new gTile;
gEvent.prototype.savedVars = ["vars"];

gEvent.prototype.apply_effect=function(who,sub_type,vars) {
  var e=g.create_object("gEvent",sub_type);
  if (typeof vars=="object")
    for (var i in vars) { e.vars[i]=vars[i]; }

  e.objs.push(who);
  who.events.push(e);
  e.go();
}

gEvent_protos = {
  poison: { vars: { wearoff: 49 },
	    go: function() 
	    {
	      g.actor_list.push(this);
	      var who=this.objs[0];
	      if (who.state.poisoned) { 
		who.state.poisoned++; msg("You feel sicker."); } else { who.state.poisoned=1; msg("You feel sick.") 
	      }
	    }, act: function() 
	    {
	      var who=this.objs[0];

	      if (this.vars.wearoff <= 0) {
		if (who.state.poisoned) who.state.poisoned--;
		msg("You feel a little better");
		this.destroy();
		return;
	      }
	      var w="1d1";
	      var r=8;
	      if (who.state.poison_resistant) { 
		w="1d2+1";
		r=11;
	      };
	      this.vars.wearoff -= roll(w);
	      if (roll("1d12") >= r ) who.hp -= 1;
	    } 
  },

  invisible: { vars: { wearoff: 10},
	       go: function() 
	       {
		 g.actor_list.push(this);
		 var who=this.objs[0];	
		 make_invisible(who);
		 who.state.invisible=true;
		 msg("Suddenly you can't see your self");
	       }, act: function() 
	       {
		 var who=this.objs[0];
		 this.vars.wearoff--;
		 if (this.vars.wearoff <= 0) {
		   delete who.state.invisible;
		   make_visible(who);
		   msg("You are no longer invisible");
		   this.destroy();
		 }
	       } 
  },

  speed: { vars: { wearoff: 30, amount: 2},
	       go: function() 
	       {
		 var who=this.objs[0];	
		 if (who.state.fast) {
		   msg("Nothing happens.");
		   this.destroy();
		   return;
		 }

		 g.actor_list.push(this);
		 who.state.fast=1;
		 who.speed /= this.vars.amount;
		 msg("You feel yourself moving faster.");
	       }, act: function() 
	       {
		 var who=this.objs[0];
		 this.vars.wearoff--;
		 if (this.vars.wearoff <= 0) {
		   delete who.state.fast;
		   who.speed *= this.vars.amount;
		   msg("You feel yourself slowing down again.");
		   this.destroy();
		 }
	       } 
  },

  levitate: { vars: { wearoff: 50},
	       go: function() 
	       {
		 g.actor_list.push(this);
		 var who=this.objs[0];	
		 who.state.levitate=true;
		 msg("You become weightless and float in midair!");
	       }, act: function() 
	       {
		 var who=this.objs[0];
		 this.vars.wearoff--;
		 if (this.vars.wearoff <= 0) {
		   who.state.levitate=false;
		   msg("You float gently back to the ground.");
		   this.destroy();
		 }
	       } 
  }


  
};


function make_invisible(o) {
  o.displayTile=false;
  g.gm.dirtyList.push(o);
  o.onScreen=false;
  var d=$("div#o" + o.id);
  if (d.length != 0) d.remove();
}

function make_visible(o) {
  o.displayTile=true;
  g.gm.dirtyList.push(o);
}

