Javascript Tools for RPG
To write Random Generater for D&D3.5e, I made some Javascript
utilities,
and open them as GPL Free Software.
Copyright
These three scripts ( util.js, rpg_util.js, item_util.js
) are GPL free software. You can use them freely under the GPL.
These scripts
are distributed without any warranty.
For the details of GPL, visit GNU GENERAL PUBLIC LICENSE
Ver
2.
Other Javascripts in my website are *NOT GPL FREE SOFTWARE* because
they
are derivative works from D&D RPG. You can use them for
non-commercial
personal use only.
Usage
For example, if you want ramdom encounter generator as follows:
- 01-10 : None
- 11-100 : Encounter
- 1 : Green Dragon x1
- 2-3 : 1d4+1 Skeletons
- 4-5 : Choose 1 from
- Dire Bear
- Dire Wolf
- Dire Weasel
- 6 : Choose 3 from
- 1 : Skeleton
- 2 : Zombie
- 3 : Wight
- 4 : Ghoul
To display this, HTML file sample.html
is as follows:
<head>
<title>Sample</title>
<script language="JavaScript" src="./util.js"></script>
<script language="JavaScript" src="./rpg_util.js"></script>
<script language="JavaScript" src="./item_util.js"></script>
</head>
<body>
<script language="Javascript">
var rTable = new RT(D100, [
new RTE(10, "None"),
new RTE(100, new RT(D6, [
new RTE(1, "Green Dragon"),
new RTE(3, function() {return ((dice(4)+1)+" Skeletons");}),
new RTE(5, new CH(["Dire Bear", "Dire Wolf", "Dire Weasel"])),
new RTE(6, new AM(3, new CH(["Skeleton", "Zombie", "Wight", "Ghoul"])))
]))
]);
document.writeln("Result="+getValue(rTable)+"<BR>");
</script>
</body>
</html>
APIs
util.js : Utilitiy for
Javascript
Required: None.
- For debug print:
- var STDOUT; Destination for debug print. Default =
document
- var DEBUG; Flag for debug print
- function debugln(msg); Print msg
with <BR>-tag
- function debug(msg); Print msg
- Object type
- function isObject(o); true if o
is not String, Number, Function.
- function isArray(o); true if o
is Array
- function isFunction(o); true if o
is Function
- function isString(o); true if o
is String
- Get arguments from the search part (param1=value1¶m2=value2&...)
of the URL protocol://hostname:port/pathname?search#hash
- function getArgs(); returns Object args. param1
can be accessed by args.param1 or args["param1"].
Required: utils.js
- Dice roll
- function dice(nS); returns random number from
1 to nS
- function nDice(nD, nS); Sum: dice
number = nD, dice sides = nS
- var D100 : D100() is dice(100).
- var D2, D3, D4, D6, D8, D10, D12, D20 : as D100
- getValue : Evaluate objects recursively
- function getValue(o); Returns
- [getValue(o1), getValue(o2),...] if o
= [o1, o2,...]
- o() if o is function
- o.getValue() if o.getValue()
exists
- Otherwise, o itself
- Random Table Entry
- function RTE(aThre, aResult) : Constructor
- aThre : int threshold
- aResult : the value to return if chosen
- RTE.getValue(); Return getValue(aResult)
- RTE.isChosen(x); true if x <=
aThre
- RTE.toString() : String expression
- Random Table
- function RT(fDice, anArrayOfRTE) :
Constructor
- fDice : getValue(fDice) is
used to choose one of RTEs
- anArrayOfRTE : array of RTE
- RT.roll() : returns getValue(fDice)
- RT.getValue() : Choose one RT rt, and
returns getValue(rt)
- RT.toString() : String expression
- Choice
- function CH(anArray) : Constructor
- anArray : array of object
- CH.getValue() : Choose one item randomly from anArray
- CH.toString() : String expression
- Concat Array
- function CA(anAry, aSep) : Constructor
- anAry : Array [o1,o2,...]
- aSep : separator
- CA.getValue() : returns getValue(o1)+aSep+getValue(o2)+...
- CH.toString() : String expression
- Array Maker
- function AM(fNum, anObj) : Constructor
- fNum : number = getValue(fNum)
- anObj : Object to duplicate
- AM.getValue() : returns [getValue(anObj),
getValue(anObj),...]
- AM.toString() : String expression
- Misc
- function opAry(anAry,
func, sep) : operate func
for each item of anAry,
and concatinate with sep
item_util.js
:
Utility for item generation
Require: rpg_util.js (thus also util.js)
- Concrete Item
- function Item(aDesc, aPrice) : Constructor
- aDesc : description string, usually in
the form
of "Type: description"
- aPrice : price in gp
- Item.toString() : Strng expression
- Item.toDisp() : Formatted string "Type: description
(aPrice gp)"
- Item.toShort() : Formatted in short (without description)
- Item Template
- function IT(fDesc, fPrice) : Constructor
- fDesc : function to generate description
string
by getValue(fDesc); usually in the form of "Type:
description"
- fPrice : function to generate price in gp
- IT.toString() : String expression
- IT.getValue() : returns Item(getValue(fDesc),
getValue(fPrice))
- var NONE : = new Item("None", 0);
- [Adj] + Noun : For convennience
- function AN(anAry) : Constructor, anAry
= [adj1, adj2,..., adjN, noun]
- AN.getValue() : Choose one adj, and
concatinate noun with space between them
- AN.toString() : String Expression
- function toDispArray(anAry) : Apply toDisp() to
all
items, and concatinate them; (This function is temporary.)