Commits

Chris Beelby committed 7ba6268

Added more global functions

  • Participants
  • Parent commits c6c59d4

Comments (0)

Files changed (1)

 var g = NETHACK.globals;
 
-g.rn2 = function(x) { // 0 <= rn2(x) < x
+// from src/rnd.c
+
+// 0 <= rn2(x) < x
+g.rn2 = function(x) {
    return Math.floor(Math.random()*x);
 }
 
-g.rnd = function(x) { // 1 <= rnd(x) <= x
+// 1 <= rnd(x) <= x
+g.rnd = function(x) {
    return NETHACK.globals.rn2(x) + 1;
 }
 
+// 0 <= rnl(x) < x; sometimes subtracting luck
+// good luck approaches 0, bad luck approaches (x-1)
 g.rnl = function(x) {
    var g = NETHACK.globals;
    var i = g.rn2(x);
    return i;
 }
 
-g.d = function(n, x) { // n <= d(n, x) <= (n*x)
+// n <= d(n, x) <= (n*x)
+g.d = function(n, x) {
    var temp = n;
    while (n--) {
       temp += NETHACK.globals.rn2(x);
    return Math.floor(x);
 }
 
+// from src/role.c
+
+g.validrole = function(rolenum) {
+   return (rolenum >= 0 && rolenum < NETHACK.globals.roles.length);
+}
+
+g.randrole = function() {
+   return NETHACK.globals.rn2(NETHACK.globals.roles.length - 1);
+}
+
+g.str2role = function(str) {
+   var roles = NETHACK.globals.roles;
+
+   if (str == '*' || str == '@' || str == 'random') {
+      return -2;//ROLE_RANDOM
+   }
+
+   for (var i = 0; i < roles.length; i++) {
+      if (roles[i].name.m == str || roles[i].name.f == str ||
+         roles[i].name.filecode == str) {
+         return i;
+      }
+   }
+
+   return -1;//ROLE_NONE
+}
+
+g.validrace = function(rolenum, racenum) {
+   var g = NETHACK.globals;
+
+   //return (racenum >= 0 && racenum < g.races.length &&
+   //   (g.roles[rolenum].allow & g.races[racenum].allow & ));
+   return true;
+}
+