/* * BAGSRooms.bsh * * Copyright 2005 M. Aaron Wadley * * This file is part of BAGS (Beany Adventure Game System). * BAGS (Beany Adventure Game System) is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * BAGS (Beany Adventure Game System) is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with BAGS (Beany Adventure Game System); if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ Rooms(theGame, theOutput) { roomList = new HashMap(); output = theOutput; game = theGame; getRoom(room) { return roomList.get(room); } loadRooms(customRooms) { log.info("Loading rooms"); methods = customRooms.namespace.getMethods(); for (m : methods) { Object o = new Object(); o = eval("customRooms." + m.getName() + "()"); if (o.roomId != null && o.roomId != void) { key = o.roomId; } else { key = o.roomTitle; } log.debug("Loading room: " + key); if (o.contents == null || o.contents == void) { o.contents = new String(""); } else { log.debug(o.roomTitle + " contains " + o.contents); } // Set up default properties o.visited=false; o.lit=true; o.roomId = key; // add the room to the list roomList.put(key, o); } } getDynamicTitle(room) { if (game.hasMethod(room, "getTitle")) { return room.getTitle(); } else { if (room.visited) { return room.roomTitle; } else { if (room.unvisitedTitle != null && room.unvisitedTitle != void) { return room.unvisitedTitle; } else { return room.roomTitle; } } } } getDynamicDescription(room) { if (game.hasMethod(room, "getDescription")) { return room.getDescription(); } else { return room.roomDescription; } // check for dropped objects contents = game.rooms.getRoomContents(game.currentLocation); Vector droppedItems = new Vector(); for (i : contents) { obj = game.gameObjects.getObject(i.toUpperCase()); if (obj.dropped != void && obj.dropped==true) { droppedItems.add(i); } } if (droppedItems.size() > 0) { StringBuffer sb = new StringBuffer(); sb.append("You can also see "); count = 1; for (d : droppedItems) { sb.append(" your " + d); if (count < droppedItems.size()) { sb.append(" and"); } } sb.append(" here."); game.output.insertString(sb.toString() + "\n"); game.output.insertLineBreak(); } } outputDescription(room) { // Show room title output.insertStyledString(getDynamicTitle(room), STYLE_BOLD ); output.insertLineBreak(); // Show room description output.insertString( getDynamicDescription(room) ); output.insertLineBreak(); output.insertLineBreak(); output.scrollToEnd(); } outputExits(roomTitle) { // Show available exits output.insertLineBreak(); output.insertStyledString("Available exits :", STYLE_ITALIC ); output.insertLineBreak(); room = roomList.get(roomTitle); for (an_exit : room.exits) { output.insertStyledString( " " + an_exit.toString(), STYLE_BOLD); output.insertStyledString ( " to ", STYLE_REGULAR); output.insertStyledString (roomList.get(an_exit.leadsTo).roomTitle, STYLE_ITALIC); output.insertLineBreak(); } output.scrollToEnd(); } onEntrance(room) { if (game.hasMethod(room, "onEntrance")) { room.onEntrance(); } room.visited = true; } getLinkedExit(from, exit) { r = roomList.get(exit.leadsTo); for (e : from.exits) { if (e.leadsTo.equalsIgnoreCase(from.roomTitle)) { return e; } } return null; } onExit(room) { if (game.hasMethod(room, "onExit")) { return room.onExit(); } return true; } parseContents(room) { log.trace("ENTER parseContents()"); String[] contents = room.contents.split(","); log.debug("Contents: " + room.contents); log.trace("EXIT parseContents()"); return contents; } getRoomContents(room) { return parseContents(room); } addItemToContents(item, contents) { StringBuffer sb = new StringBuffer(); String[] conts = contents.split(","); itemExists = false; for ( s : conts) { if (!s.equalsIgnoreCase(item)) { sb.append(s); sb.append(","); } else { itemExists = true; } } if (!itemExists) { sb.append(item); } return sb.toString(); } removeItemFromContents(item, contents) { StringBuffer sb = new StringBuffer(); String[] conts = contents.split(","); itemExists = false; for ( s : conts) { if (!s.equalsIgnoreCase(item)) { sb.append(s); sb.append(","); } } return sb.toString(); } addItemToRoom(item, room) { newContents = addItemToContents(item, room.contents); room.contents = newContents; } removeItemFromRoom(item, room) { newContents = removeItemFromContents(item, room.contents); room.contents = newContents; } getItemInRoom(item, room) { log.trace("ENTER getItemInRoom()"); log.debug("Trying to retrieve object " + item); contents = parseContents(room); for(s : contents) { if (s.equalsIgnoreCase(item)) { obj = game.gameObjects.getObject(s.toUpperCase()); if (obj != null) { log.debug("Found object " + obj.id + " in room " + getDynamicTitle(room)); log.trace("EXIT getItemInRoom()"); return obj; } } } log.warn("Did NOT find object " + item + " in room " + getDynamicTitle(room)); log.trace("EXIT getItemInRoom()"); return null; } return this; }