/* Various interface functions. * * */ package th; import java.util.Scanner; // Ifc -> Interface import javax.swing.*; import java.util.Vector; public class Ifc { public static final String RED = "31"; public static final String GREEN = "32"; public static final String YELLOW = "33"; public static final String BLUE = "34"; public static final String MAGENTA = "35"; public static final String CYAN = "36"; public static final String BRIGHT_WHITE = "1;37"; public static final String BRIGHT_RED = "1;31"; public static final String BRIGHT_GREEN = "1;32"; public static final String BRIGHT_YELLOW = "1;33"; public static final String BRIGHT_BLUE = "1;34"; public static final String BRIGHT_MAGENTA = "1;35"; public static final String BRIGHT_CYAN = "1;36"; public static final String MAJOR_WHITE = "7;37"; public static final String MAJOR_RED = "7;31"; public static final String MAJOR_GREEN = "7;32"; public static final String MAJOR_YELLOW = "7;33"; public static final String MAJOR_BLUE = "7;34"; public static final String MAJOR_MAGENTA = "7;35"; public static final String MAJOR_CYAN = "7;36"; public static final String INVERSE = "7"; public static final String DEFAULT_COLOR = "37;0"; public static final String INTRO = "Long have strange tales been told of the things that lurk in\n" +"the dark recesses of the steam tunnels, a maze of forgotten\n" +"catacombs underneith this very institution.\n" +"Some say they conceil fabulous riches. Others say that they offer\n" +"only instant death to those who dare enter them.\n" +"You, however, have not come for treasure, but for a more noble goal:\n" +"You seek the legendary |artifact|, said to have been stolen by the\n" +"evil technician Suzie. Even now, Suzie plots to overthrow\n" +"the government and, by use of the mighty artifact's power, rule Earth\n" +"from her lair in the steam tunnels.\n" +"Hailed by the faculty and your fellow students as the last hope for\n" +"humankind in the face of this impending disaster, you have been\n" +"escorted to an abandoned entrance to the tunnels and pushed in,\n" +"instructed not to return without the |artifact|!"+ "\n\nMay the random number generator be with you, adventurer!"; // Taken: nsewlatgdpqrciu?v#xzj // Avaible: bfhkmy public static final String HELP = "n,s,e,w: north, south, east, west; l: look around; a: attack;\n" +"t: talk; g: get item; d: drop item; p: put on item; q: quit;\n" +"r: ready a weapon; c: consume food or drink; i: view items;\n" +"u: use item; ?: help, v: view into adjacent rooms; #: list mode;\n" +"x: debug; z: name item; j: pet & ally commands; o: glide.\n"; public static Scanner input = new Scanner(System.in); // Various permutations of the yes-no prompt function, // to compensate for java's lack of keyword parameters // as in Lisp, Pytion, etc; or default values (as in C++). // public static boolean yn() { return yn("Answer y/n (n) ", false); } public static boolean yn(String prompt) { return yn(prompt, false); } public static boolean yn(String prompt, boolean def) { System.out.print(prompt+" "); String tmp = ""; while (tmp.length() == 0) tmp = g.in.nextLine(); char value; if (tmp.length() == 0) return def; else value = tmp.charAt(0); if (value == 'y') return true; else if (value == 'n') return false; else return def; //return the def[ault], naturally. } public static void pk(String prompt) { if (prompt == null) prompt = "Press return to continue."; System.out.print(prompt); g.in.nextLine(); } public static void pk () { pk(null); } // ask a multiple-choice question. // There are several versions for conveniance. public static int choice(String prompt, String[] choices) { int i, selection; while (true) { System.out.println(prompt); i = 0; while (i++ < choices.length) System.out.printf("%d: %s\n", i, choices[i-1]); if (!g.in.hasNextInt()) { g.in.next(".*"); continue; } selection = g.in.nextInt(); if (selection > 0 && selection <= choices.length) return selection - 1; else System.out.println("Invalid selection."); } } public static Mon choice_mon(String prompt, Vector mons) { int i, selection; Mon current; while (true) { System.out.println(prompt); i = 0; System.out.println("0: Cancel"); while (i++ < mons.size()) { current = (Mon)mons.elementAt(i-1); System.out.printf("%d: %s %s\n", i, (current.name == null? "A "+current.species.name : current.name + " the " + current.species.name) + " HP: " + Ifc.hpcolor (current)+ current.hp + Ifc.color(), (current == g.player? "" : Mon.string_status[current.ai]) ); } if (!g.in.hasNextInt()) { g.in.next(".*"); continue; } selection = g.in.nextInt(); if (selection == 0) return null; else if (selection > 0 && selection <= mons.size()) { return (Mon)mons.elementAt(selection - 1); } else System.out.println("Invalid selection."); } } public static void show_items(String prompt, Vector itms) { int i, selection; if (itms.size() == 0) { System.out.println(" There are no items. "); return; } Itm current; System.out.println(prompt); i = 0; while (i++ < itms.size()) { current = (Itm)itms.elementAt(i-1); System.out.printf("%d: %s %s %s %s\n", i, current.kind.name, current.name != null? "named "+current.name : "", current.wielded? "(wielded)" : "", current.worn? "(worn)" : ""); } pk(); } public static void show_mons(String prompt, Vector itms) { int i, selection; if (itms.size() == 0) { System.out.println(" There are no creatures. "); return; } Mon current; System.out.println(prompt); i = 0; while (i++ < itms.size()) { current = (Mon)itms.elementAt(i-1); System.out.printf("%d: %s %s %s %s\n", i, current.species.name, current.name != null? "named "+current.name : "", current.rabid? "(rabid)" : "", current.cyborg? "(borg)" : ""); } pk(); } public static Itm choice_itm(String prompt, Vector itms) { int i, selection; Itm current; while (true) { System.out.println(prompt); i = 0; System.out.println("0: Cancel"); while (i++ < itms.size()) { current = (Itm)itms.elementAt(i-1); System.out.printf("%d: %s %s %s %s\n", i, current.kind.name, current.name != null? "named "+current.name : "", current.wielded? "(wielded)" : "", current.worn? "(worn)" : ""); //System.out.printf("%d: %s\n", i, current.kind.name); } if (!g.in.hasNextInt()) { g.in.next(".*"); continue; } selection = g.in.nextInt(); if (selection == 0) return null; else if (selection > 0 && selection <= itms.size()) { return (Itm)itms.elementAt(selection - 1); } else System.out.println("Invalid selection."); } } // Make things a bit simpler // The choices are deliniated with pipes. public static int choice(String prompt, String choices) { return choice(prompt, choices.split("\\|")); } // Simplier yet public static int choice(String choices) { return choice("Please choose one: ", choices.split("\\|")); } // Permutation with default prompt, string array. // Note how not having anything other than simple overloading has // really made this a pain. public static int choice(String[] choices) { return choice("Please choose one", choices); } // Get various things public static Spe get_spe(String name) { for (Spe x : Species.one_of_everything) if (x.name.toLowerCase().compareTo(name.toLowerCase()) == 0) return x; return Species.amorphous_blob; } public static Kind get_kind(String name) { for (Kind x : Items.one_of_everything) if (x.name.toLowerCase().compareTo(name.toLowerCase()) == 0) return x; return Items.filth; } public static int gint(String prompt) { System.out.print(prompt+" "); return g.in.nextInt(); } public static String gstring(String prompt) { return gstring(prompt, "Enter Text"); } public static String gstring(String prompt, String def) { String tmp = null; if (Tunnels.gmode != Tunnels.GUI) { System.out.print(prompt+" "); while (tmp == null || tmp.length() < 2) { tmp = g.in.nextLine(); } } else { while (tmp == null || tmp.length() < 1) { tmp = JOptionPane.showInputDialog(Tunnels.iwindow, prompt, def); } } return tmp; } public static Node gnsew(String prompt, Node here) { String tmp = null; String dirs = (here.north != null? "n" : "") + (here.south != null? "s" : "") + (here.east != null? "e" : "") + (here.west != null? "w" : ""); while (true) { System.out.print(prompt+" ("+dirs+") "); tmp = g.in.nextLine(); if (tmp.length() == 0) continue; if (tmp.charAt(0) == 'n' && here.north != null) return here.north; else if (tmp.charAt(0) == 's' && here.south != null) return here.south; else if (tmp.charAt(0) == 'w' && here.west != null) return here.west; else if (tmp.charAt(0) == 'e' && here.east != null) return here.east; } } public static Node gnsew (Node here) { return gnsew ("Which direction?", here); } // Various verbalizing functions. public static void you(String text, Mon who) { if (g.phase == 0) return; if (who == null && g.turns <= 1) return; if (who != null && who.here != g.player.here) return; if (who == null || who == g.player) { text = text.replaceAll("(\\|s\\||\\|es\\|)", ""); text = text.replaceAll("\\|is\\|", "are"); } else { text = text.replaceAll("\\{.*\\}", ""); // Can be used to hide results of item use from player // if he isn't using the item. text = text.replaceAll("\\|s\\|", "s"); text = text.replaceAll("\\|es\\|", "es"); text = text.replaceAll("\\|is\\|", "is"); } if (who == null || who == g.player) System.out.printf("You %s\n", text); else if (who.here == g.player.here && who.name != null) System.out.printf("%s %s\n", who.name, text); else if (who.here == g.player.here) System.out.printf("The %s%s%s %s\n", (who.rabid? (who.species.smart? "insane " : "rabid ") : ""), (who.ai == who.PET? "allied " : ""), who.species.name, text); if (who == null || who.here == g.player.here) g.glidetrigger = true; } public static void you(String text) { you(text, null); } // Wrapper for gui or console output public static void msg(String what) { if (Tunnels.gmode == Tunnels.GUI) { // Print to message console in the future System.out.println(what); } else System.out.println(what); } public static String the_mon(Mon who) { if (who == null) return "you"; else if (who.name != null) return who.name; else return "the " + (who.ai == who.PET? "allied " : (who.rabid? (who.species.smart? "insane " : "rabid ") : "")) + who.species.name; } // Gendered pronouns // I use the male ones because // the neuter and female ones do not have three sperate words! // ("I saw her", "Her cat is cute" versus "I saw him", "His cat is cute".) public static String he(Mon who) { if (who == g.player) return "you"; else if (who.gender == 0) return "it"; else if (who.gender == 1) return "he"; else return "she"; } public static String his(Mon who) { if (who == g.player) return "your"; else if (who.gender == 0) return "its"; else if (who.gender == 1) return "his"; else return "her"; } public static String him(Mon who) { if (who == g.player) return "you"; else if (who.gender == 0) return "it"; else if (who.gender == 1) return "him"; else return "her"; } public static String sir(Mon who) { if (who.gender == 1) return "sir"; else if (who.gender == 2) return "ma'am"; else return "honored creature"; } public static void your(String text, Mon who) { if (who != null && who.here != g.player.here) return; if (who == null || who == g.player) { text = text.replaceAll("\\|s\\|", ""); text = text.replaceAll("\\|do\\|", "do"); text = text.replaceAll("\\|is\\|", "are"); } else { text = text.replaceAll("\\|s\\|", "s"); text = text.replaceAll("\\|do\\|", "does"); text = text.replaceAll("\\|is\\|", "is"); } if (who == null || who == g.player) { System.out.printf("Your %s\n", text); } else if (who.here == g.player.here && who.name != null) System.out.printf("%s'%s %s\n", who.name, (who.name.charAt(who.name.length()-1) == 's'? "" : "s"), text ); else if (who.here == g.player.here) System.out.printf("The %s'%s %s\n", who.species.name, (who.species.name.charAt(who.species.name.length()-1) == 's'? "" : "s"), text ); } public static void your(String text) { your(text, null); } public static String hpcolor (Mon mon) { if (mon.hp >= mon.mhp) return color(); else if (mon.hp >= mon.mhp*3.0/4.0) return color(GREEN); else if (mon.hp > mon.mhp/2) return color(YELLOW); else if (mon.hp > mon.mhp/4) return color (RED); else return color (MAJOR_RED); } public static String color () { return color (DEFAULT_COLOR); } public static String color (String cl) { if (Tunnels.gmode != Tunnels.DOS) return "\033["+cl+"m"; else return ""; } }