Vista Sidebar Gadgets

[ Gadgets | Screenshots | History | Download | Links ]
{ Javascript / Personal Idea / April 2007 }

Microsoft Windows Vista was released in early 2007. Also at that time, I was spending at least 5 hours a week playing Dungeons & Dragons with my friends and family. So to make things easier for me, I decided to write up two quick Javascript sidebar gadgets to help with playing. If you don’t know or don’t play Dungeons & Dragons, then these gadgets are not very useful for you.

The nice thing about Vista sidebar gadgets is that you can write them as easily as building a tiny webpage. There are advanced features like XML and Windows scripting, but you don’t need to use them.

Gadgets

I made two sidebar gadgets: a dice roller and a game experience calculator.

D&D: Random Number Generator

This gadget is basically a simple dice roller.  The only thing unique about it is that you can specify how many rolls you wish to make with an X sided die. 

The code is simple javascript:

// Clear the result field
  1. document.dnd.fieldResult.value = "";
  2. var total = 0;
  3. var temp = 0;
  4.  
  5. // Generate a new roll for each die specified
  6. for (var x = 0; x < document.dnd.fieldNumber.value; x++) {
  7.   // Generate the result of the die roll
  8.   temp  = Math.ceil(document.dnd.fieldDie.value * Math.random());
  9.  
  10.   // Add the result to the field
  11.   if (document.dnd.fieldResult.value == "")
  12.     document.dnd.fieldResult.value = temp;
  13.   else
  14.     document.dnd.fieldResult.value =
  15.       document.dnd.fieldResult.value + "+" + temp;
  16.  
  17.   // Add to the total
  18.   total = total + temp;
  19. }
  20. // Put the total before the rolls
  21. document.dnd.fieldResult.value =  total + " = "
  22.   + document.dnd.fieldResult.value;

Looking back on it now, there is a severe glitch with the rounding. The Javascript function Math.Random() returns a number from 0 to 1. Now multiply that number by the sides on the die (for sake of argument, let us use a 4-sided die). That means the number can be anything between 0 and 4. Obviously, you cannot roll a 0, so I use the Javascript function Math.Ceil() to ensure than all numbers from 0.0 to 1.0 (not including 0.0) round to 1.

Therefore, the problem occurs if the Random function returns 0.0; in that case, the die roll will result in 0 (which is impossible).

D&D: Experience Calculator

This gadget calculates the number of experience points that players should receive after defeating monsters at a specific level. This is done following the 3.5 rules of Dungeons & Dragons. The math behind this calculation is a little complex. Here is the code that calculates experience for defeating a monster at level y by a player at level x.

// x = PClevel y = monsterlevel
  1. var iReturn = 0;
  2. if (x < 3) x = 3;
  3. if ((x <= 6) && (y <= 1)) iReturn = 300 * y;
  4. else if (y < 1) iReturn = 0;
  5.  
  6. // This formula looks nice, but 3.5 doesn't follow a smooth
  7. // formula like 3.0 did.
  8. else iReturn = 6.25 * x * ( Math.pow(2,mEven(7- (x-y) ) /2) ) *
  9.   ( 11-(x-y) – mEven(7-(x-y)) );
  10.  
  11. // Below catches places where the formula fails for 3.5.
  12. if ((y == 4) || (y == 6) || (y == 8) || (y == 10) || (y == 12) ||
  13.   (y == 14) ||(y == 16) ||(y == 18) ||(y == 20))
  14. {
  15.   if (x <= 3) iReturn = 1350 * Math.pow(2,(y-4)/2);
  16.   else if (x == 5 && y >= 6) iReturn = 2250*Math.pow(2,(y-6)/2);
  17.   else if (x == 7 && y >= 8) iReturn = 3150*Math.pow(2,(y-8)/2);
  18.   else if (x == 9 && y >= 10) iReturn = 4050*Math.pow(2,(y-10)/2);
  19.   else if (x == 11 && y >= 12) iReturn = 4950*Math.pow(2,(y-12)/2);
  20.   else if (x == 13 && y >= 14) iReturn = 5850*Math.pow(2,(y-14)/2);
  21.   else if (x == 15 && y >= 16) iReturn = 6750*Math.pow(2,(y-16)/2);
  22.   else if (x == 17 && y >= 18) iReturn = 7650*Math.pow(2,(y-18)/2);
  23.   else if (x == 19 && y >= 20) iReturn = 8550*Math.pow(2,(y-20)/2);
  24. }
  25. if ((y == 7) || (y == 9) || (y == 11) || (y == 13) || (y == 15) ||
  26.   (y == 17) ||(y == 19))
  27. {
  28.   if (x == 6) iReturn = 2700 * Math.pow(2,(y-7)/2);
  29.   if (x == 8 && y >= 9) iReturn = 3600 * Math.pow(2,(y-9)/2);
  30.   if (x == 10 && y >= 11) iReturn = 4500 * Math.pow(2,(y-11)/2);
  31.   if (x == 12 && y >= 13) iReturn = 5400 * Math.pow(2,(y-13)/2);
  32.   if (x == 14 && y >= 15) iReturn = 6300 * Math.pow(2,(y-15)/2);
  33.   if (x == 16 && y >= 17) iReturn = 7200 * Math.pow(2,(y-17)/2);
  34.   if (x == 18 && y >= 19) iReturn = 8100 * Math.pow(2,(y-19)/2);
  35. }
  36.  
  37. if (y > 20) iReturn = 2 * mExperience(x, y-2);
  38. // recursion should end this in short order.
  39. // This method is clean, and ensures any errors in the above
  40. // formulas for 3.5 are accounted for.
  41.  
  42. // Finally we correct for out of bounds entries, doing this last
  43. // to cut space on the above formulas.
  44. if (x – y > 7) iReturn = 0;
  45. else if (y – x > 7) iReturn = 0;
  46.  
  47. return iReturn;

Screenshots

Experience Calculator

Experience Screenshot

Random Number Screenshot

Random Number Screenshot

Logo used for both gadgets

Logo used for both gadgets

History

Download

Links/Resources

1 Comment

  1. Alexander  •  Jul 8, 2009 @3:19 pm

    Thank you for your help!

Leave a Reply

Allowed tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>



  • Donate

    If my work has helped you and you want to return the favor, you could purchase something for me from my Amazon Wish List or send me a donation via PayPal.

  • My Lifestream

  • License

    Unless otherwise noted, all source code and compiled files published on this website are released under the terms of the GNU Lesser General Public License.