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:
-
document.dnd.fieldResult.value = "";
-
var total = 0;
-
var temp = 0;
-
-
// Generate a new roll for each die specified
-
for (var x = 0; x < document.dnd.fieldNumber.value; x++) {
-
// Generate the result of the die roll
-
temp = Math.ceil(document.dnd.fieldDie.value * Math.random());
-
-
// Add the result to the field
-
if (document.dnd.fieldResult.value == "")
-
document.dnd.fieldResult.value = temp;
-
else
-
document.dnd.fieldResult.value =
-
document.dnd.fieldResult.value + "+" + temp;
-
-
// Add to the total
-
total = total + temp;
-
}
-
// Put the total before the rolls
-
document.dnd.fieldResult.value = total + " = "
-
+ 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.
-
var iReturn = 0;
-
if (x < 3) x = 3;
-
if ((x <= 6) && (y <= 1)) iReturn = 300 * y;
-
else if (y < 1) iReturn = 0;
-
-
// This formula looks nice, but 3.5 doesn't follow a smooth
-
// formula like 3.0 did.
-
else iReturn = 6.25 * x * ( Math.pow(2,mEven(7- (x-y) ) /2) ) *
-
( 11-(x-y) – mEven(7-(x-y)) );
-
-
// Below catches places where the formula fails for 3.5.
-
if ((y == 4) || (y == 6) || (y == 8) || (y == 10) || (y == 12) ||
-
(y == 14) ||(y == 16) ||(y == 18) ||(y == 20))
-
{
-
if (x <= 3) iReturn = 1350 * Math.pow(2,(y-4)/2);
-
else if (x == 5 && y >= 6) iReturn = 2250*Math.pow(2,(y-6)/2);
-
else if (x == 7 && y >= 8) iReturn = 3150*Math.pow(2,(y-8)/2);
-
else if (x == 9 && y >= 10) iReturn = 4050*Math.pow(2,(y-10)/2);
-
else if (x == 11 && y >= 12) iReturn = 4950*Math.pow(2,(y-12)/2);
-
else if (x == 13 && y >= 14) iReturn = 5850*Math.pow(2,(y-14)/2);
-
else if (x == 15 && y >= 16) iReturn = 6750*Math.pow(2,(y-16)/2);
-
else if (x == 17 && y >= 18) iReturn = 7650*Math.pow(2,(y-18)/2);
-
else if (x == 19 && y >= 20) iReturn = 8550*Math.pow(2,(y-20)/2);
-
}
-
if ((y == 7) || (y == 9) || (y == 11) || (y == 13) || (y == 15) ||
-
(y == 17) ||(y == 19))
-
{
-
if (x == 6) iReturn = 2700 * Math.pow(2,(y-7)/2);
-
if (x == 8 && y >= 9) iReturn = 3600 * Math.pow(2,(y-9)/2);
-
if (x == 10 && y >= 11) iReturn = 4500 * Math.pow(2,(y-11)/2);
-
if (x == 12 && y >= 13) iReturn = 5400 * Math.pow(2,(y-13)/2);
-
if (x == 14 && y >= 15) iReturn = 6300 * Math.pow(2,(y-15)/2);
-
if (x == 16 && y >= 17) iReturn = 7200 * Math.pow(2,(y-17)/2);
-
if (x == 18 && y >= 19) iReturn = 8100 * Math.pow(2,(y-19)/2);
-
}
-
-
if (y > 20) iReturn = 2 * mExperience(x, y-2);
-
// recursion should end this in short order.
-
// This method is clean, and ensures any errors in the above
-
// formulas for 3.5 are accounted for.
-
-
// Finally we correct for out of bounds entries, doing this last
-
// to cut space on the above formulas.
-
if (x – y > 7) iReturn = 0;
-
else if (y – x > 7) iReturn = 0;
-
-
return iReturn;
Screenshots
History
- 4 April 2007 - v1.0 – Released both gadgets to the Windows Live Gallery.
Download
- D&D: Random Number Generator (v1.0) – 122kb
- md5: 3f1664ab2c2229e54a5671dacb865fd5
- sha1: 539afe9f19e443f924515ce52cac6fb18808d225
- D&D: Experience Calculator (v1.0) – 133kb
- md5: 5367633b085b396dac361ce51fc93052
- sha1: c5fd36b100d4a3c3761d5db2e632eedd81333ed2






Thank you for your help!