Peaeater

Life in hyperbole. HYPERBOLE, I said!


Casting Magic Missile with javascript

Gordon Coleman made me do this. I haven't actually tested it yet.
 
/* EVENTS */
function onload()
{
  var wiz = new wizard();
  wiz.name = "Alakazool";
  wiz.level = 3;
  wiz.alignment = "chaotic evil";
  wiz.spells = ["magic missile", "light cantrip"];
  wiz.spellSlots = 2;
 
  var unsuspectingBystander = new target();
  unsuspectingBystander.name = "Durff the Goat Herder";
  unsuspectingBystander.hitPoints = 6;
 
  if ((wiz.alignment.indexOf("evil") != -1) && (wiz.spells.indexOf("magic missile") != -1) && (wiz.spellSlots > 0))
  {
    while (target.lifeStatus != 'dead' && wiz.spellSlots > 0)
    {
      wiz.castSpell("magic missile", wiz.level, unsuspectingBystander);
    }
    if (target.lifeStatus == 'dead' || target.lifeStatus == 'unconscious')
    {
      wiz.laughManiacally();
    }
    else 
    {
      wiz.rest();  
    }
  }
}
 
 
/* OBJECTS */
function target()
{
  this.name = "";
  this.hitPoints = null;
  this.lifeStatus = function() {
    if (this.hitPoints < 1 && this.hitPoints > -10) {return 'unconscious';} 
    else if (this.hitPoints < -9) {return 'dead';} 
    else {return '';}
  }
}
 
function missile(intDamage)
{
  this.target = null;
  this.damage = intDamage;
}
 
function wizard()
{
  this.name = "";
  this.level = 0;
  this.alignment = "";
  this.spells = [];
  this.spellSlots = 0;
  this.maxSpellSlots = level;
  this.castSpell = CastSpell();
  this.rest = function() {setTimeout(spellSlots = maxSpellSlots, 8*1000*60*60);}
  this.laughManiacally = function() {alert("mwa ha ha ha ha ha haaaaa!!");}
}
 
/* PUBLIC METHODS */
function CastSpell(strSpell, intSpellLevel, oTarget)
{
  switch (strSpell)
  {
    case "magic missile":
      CastMagicMissile(intSpellLevel, oTarget);
      break;
    case "light cantrip":
      // do nothing useful
      break;
  }
}
 
function CastMagicMissile(intLevel, oTarget)
{
  var count = getNumberMissilesFromCharLevel(intLevel);
  var missiles = generateMissiles(count);
  if (oTarget != null)
  {
    fireMissiles(oTarget, missiles);
  }
}
 
function RollDie(intMax)
{
  // generate random integer between 1 and intMax
  var output = Math.floor(Math.random()*intMax+1);
  return output;
}
 
/* PRIVATE METHODS */
function fireMissiles(oTarget, missiles)
{
  var count = missiles.length;
  for (var i = 0; i < count; i++)
  {
    oTarget.hitPoints -= missiles[i].damage;  
  }
}
 
function generateMissiles(intCount)
{
  var output = [];
  for (var i = 0; i < intCount; i++)
  {
    var intDamage = RollDie(4);
    output[output.length] = new missile(intDamage);
  }
  return output;
}
 
function getNumberMissilesFromCharLevel(intLevel)
{
  var output = 0;
  if (intLevel >= 1 && intLevel < 3)
  {
    output = 1;  
  }
  else if (intLevel >= 3 && intLevel < 5)
  {
    output = 2;  
  }
  else if (intLevel >= 5 && intLevel < 7)
  {
    output = 3;  
  }
  else if (intLevel >= 7 && intLevel < 9)
  {
    output = 4;  
  }
  else if (intLevel >= 9)
  {
    output = 5;  
  }
  return output;
}

2 Responses to “Casting Magic Missile with javascript”

  1. # RockyDil

    Good grief, that was geeky.

    But GRAND.

    "{alert("mwa ha ha ha ha ha haaaaa!!");}" = best part.  

  2. # Chelle

    Why? Why, why, why, why, why?
    (and why me?)  

Post a Comment

Links to this post

Create a Link