package th; // Utility class import java.util.Vector; public class Utl { public static Object newInstance(Class cl) { try { return cl.newInstance(); } catch (Exception e) { Ifc.msg("Something bad happened: "+cl); e.printStackTrace(); return null; } } // rn (short for random) is a very versitile function, // with a lot of different overloaded behaviors. // Give a random number, n. // x >= 0: 0 <= n < x. // x < 0: -x <= n <= x public static int rn(int x) { if (x == 0) return 0; if (x >= 0) return g.rng.nextInt(x); else return g.rng.nextInt((-x)+1) * (g.rng.nextBoolean()? 1 : -1); } public static Class gclass(String name) { try { return Class.forName("th." + name); } catch (Exception e) { System.out.println("Uh oh, something's wrong: " +name); e.printStackTrace(); } return null; } public static int rn(int min, int max) { return g.rng.nextInt(max-min)+min; } // RPG style dice expression, e.g. 2d6 => d(2,6); // Obviously then d10+3 => d(1,10)+3 // note that all dice are considered to not have a zero // which is generally true. (counting the 0 of d10 as a 10 // is fairly conventional except in d% rolls.) public static int d(int n, int x) { int i = 0, sum = 0; while ( i++ < n ) sum += rn(x)+1; return sum; } public static int d(int x) { return rn(x)+1; } public static boolean contains(Object[] marray, Object thing) { for (Object current : marray) { if (current == thing) return true; } return false; } public static boolean rn() { return g.rng.nextBoolean(); } // Return a random node. If fix, don't leave current region. (here) public static Node rn_node (boolean fix, int here) { Node tmp = null; while (true) { tmp = (Node)Utl.rn(g.nodes); if (fix && here != tmp.region) continue; if (tmp.locked) continue; break; } return tmp; } // Here we have functions to choose one of a list. // same trick as in Ifc's choice. public static String rn(String[] choices) { return choices[rn(choices.length)]; } public static String rn(String choices) { return rn(choices.split("\\|")); } public static Itm rn(Itm[] choices) { return choices[rn(choices.length)]; } public static Kind rn(Kind[] choices) { return choices[rn(choices.length)]; } public static Spe rn(Spe[] choices) { return choices[rn(choices.length)]; } public static Class rn(Class[] choices) { return choices[rn(choices.length)]; } public static int rn(int[] choices) { return choices[rn(choices.length)]; } public static Object rn(Vector choices) { if (choices.size() == 0) return null; return choices.elementAt(rn(choices.size())); } // Describe spatial relationship of there to here. public static String nsew(Node there) { return nsew(g.here, there); } public static String nsew(Node here, Node there) { if (here.north == there) return "north"; else if (here.south == there) return "south"; else if (here.east == there) return "east"; else if (here.west == there) return "west"; else return "somewhere"; } public static Node rn(Node room) { return rn(room, 0); } public static Node rn(Node room, int i) { if (i > 64) return room; switch (rn(4)) { case 0: if (room.north == null) return rn(room, i+1); else return room.north; case 1: if (room.south == null) return rn(room, i+1); else return room.south; case 2: if (room.east == null) return rn(room, i+1); else return room.east; case 3: if (room.west == null) return rn(room, i+1); else return room.west; default: return null; } } // Split along pipes public static String[] sp(String choices) { return choices.split("\\|"); } public static boolean coaligned(Mon a, Mon b) { if (a.alignment < 0 && b.alignment < 0) { return true; } else if (b.alignment > 0 && a.alignment > 0) { return true; } else if (b.alignment == a.alignment) { return true; } else return false; } }