ZorkReader refactored
This commit is contained in:
parent
4a7eca5302
commit
e6b96d69d1
11 changed files with 233 additions and 260 deletions
|
@ -6,6 +6,7 @@
|
||||||
package com.github.dtschust.zork;
|
package com.github.dtschust.zork;
|
||||||
|
|
||||||
import com.github.dtschust.zork.parser.ZorkGame;
|
import com.github.dtschust.zork.parser.ZorkGame;
|
||||||
|
import com.github.dtschust.zork.types.*;
|
||||||
import com.github.dtschust.zork.parser.ZorkReader;
|
import com.github.dtschust.zork.parser.ZorkReader;
|
||||||
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
@ -165,7 +166,6 @@ public class Zork {
|
||||||
private void doActionDelete(String object) {
|
private void doActionDelete(String object) {
|
||||||
String objectType;
|
String objectType;
|
||||||
objectType = game.ObjectLookup.get(object);
|
objectType = game.ObjectLookup.get(object);
|
||||||
game.Objects.remove(object);
|
|
||||||
if (objectType.equals("room")) {
|
if (objectType.equals("room")) {
|
||||||
for (String key : game.Rooms.keySet()) {
|
for (String key : game.Rooms.keySet()) {
|
||||||
ZorkRoom tempRoom = game.Rooms.get(key);
|
ZorkRoom tempRoom = game.Rooms.get(key);
|
||||||
|
@ -211,28 +211,10 @@ public class Zork {
|
||||||
}
|
}
|
||||||
|
|
||||||
private void doActionUpdate(String object, String newStatus) {
|
private void doActionUpdate(String object, String newStatus) {
|
||||||
String objectType;
|
ZorkMap<ZorkObject> collection = (ZorkMap<ZorkObject>) game.getListThroughLookup(object);
|
||||||
objectType = game.ObjectLookup.get(object);
|
ZorkObject tempObject = collection.get(object);
|
||||||
if (objectType.equals("room")) {
|
tempObject.status = newStatus;
|
||||||
ZorkRoom tempRoom = game.Rooms.get(object);
|
collection.put(tempObject);
|
||||||
tempRoom.status = newStatus;
|
|
||||||
game.Rooms.put(tempRoom);
|
|
||||||
} else if (objectType.equals("container")) {
|
|
||||||
ZorkContainer tempContainer = game.Containers.get(object);
|
|
||||||
tempContainer.status = newStatus;
|
|
||||||
game.Containers.put(tempContainer);
|
|
||||||
|
|
||||||
} else if (objectType.equals("creature")) {
|
|
||||||
ZorkCreature tempCreature = game.Creatures.get(object);
|
|
||||||
tempCreature.status = newStatus;
|
|
||||||
game.Creatures.put(tempCreature);
|
|
||||||
|
|
||||||
} else if (objectType.equals("item")) {
|
|
||||||
ZorkItem tempItem = game.Items.get(object);
|
|
||||||
tempItem.status = newStatus;
|
|
||||||
game.Items.put(tempItem);
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void doActionAttack(String tempString, String weapon) {
|
private void doActionAttack(String tempString, String weapon) {
|
||||||
|
|
|
@ -1,5 +1,8 @@
|
||||||
package com.github.dtschust.zork;
|
package com.github.dtschust.zork;
|
||||||
|
|
||||||
|
import com.github.dtschust.zork.types.ZorkContainer;
|
||||||
|
import com.github.dtschust.zork.types.ZorkRoom;
|
||||||
|
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
/* Has conditions*/
|
/* Has conditions*/
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
package com.github.dtschust.zork;
|
package com.github.dtschust.zork;
|
||||||
|
|
||||||
|
import com.github.dtschust.zork.types.ZorkObject;
|
||||||
|
|
||||||
/* Status conditions*/
|
/* Status conditions*/
|
||||||
public class ZorkConditionStatus extends ZorkCondition {
|
public class ZorkConditionStatus extends ZorkCondition {
|
||||||
private final String status;
|
private final String status;
|
||||||
|
@ -11,7 +13,7 @@ public class ZorkConditionStatus extends ZorkCondition {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean evaluate(Zork zork) {
|
public boolean evaluate(Zork zork) {
|
||||||
ZorkObject tested = zork.game.Objects.get(object);
|
ZorkObject tested = zork.game.getListThroughLookup(object).get(object);
|
||||||
return tested != null && tested.status.equals(status);
|
return tested != null && tested.status.equals(status);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
package com.github.dtschust.zork.parser;
|
package com.github.dtschust.zork.parser;
|
||||||
|
|
||||||
import com.github.dtschust.zork.*;
|
import com.github.dtschust.zork.types.*;
|
||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
|
@ -10,9 +10,39 @@ public class ZorkGame {
|
||||||
public ZorkMap<ZorkRoom> Rooms = new ZorkMap<>();
|
public ZorkMap<ZorkRoom> Rooms = new ZorkMap<>();
|
||||||
public ZorkMap<ZorkItem> Items = new ZorkMap<>();
|
public ZorkMap<ZorkItem> Items = new ZorkMap<>();
|
||||||
public ZorkMap<ZorkContainer> Containers = new ZorkMap<>();
|
public ZorkMap<ZorkContainer> Containers = new ZorkMap<>();
|
||||||
public ZorkMap<ZorkObject> Objects = new ZorkMap<>();
|
|
||||||
public ZorkMap<ZorkCreature> Creatures = new ZorkMap<>();
|
public ZorkMap<ZorkCreature> Creatures = new ZorkMap<>();
|
||||||
public Set<String> Inventory = new HashSet<>();
|
public Set<String> Inventory = new HashSet<>();
|
||||||
public HashMap<String, String> ObjectLookup = new HashMap<>();
|
public HashMap<String, String> ObjectLookup = new HashMap<>();
|
||||||
|
|
||||||
|
public void addObjectThroughLookup(String name, ZorkObject object){
|
||||||
|
switch (name) {
|
||||||
|
case "room":
|
||||||
|
Rooms.put((ZorkRoom) object);
|
||||||
|
break;
|
||||||
|
case "container":
|
||||||
|
Containers.put((ZorkContainer) object);
|
||||||
|
break;
|
||||||
|
case "creature":
|
||||||
|
Creatures.put((ZorkCreature) object);
|
||||||
|
break;
|
||||||
|
case "item":
|
||||||
|
Items.put((ZorkItem) object);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
ObjectLookup.put(object.name, name);
|
||||||
|
}
|
||||||
|
public ZorkMap<? extends ZorkObject> getListThroughLookup(String name){
|
||||||
|
switch (ObjectLookup.get(name)) {
|
||||||
|
case "room":
|
||||||
|
return Rooms;
|
||||||
|
case "container":
|
||||||
|
return Containers;
|
||||||
|
case "creature":
|
||||||
|
return Creatures;
|
||||||
|
case "item":
|
||||||
|
return Items;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,13 +1,16 @@
|
||||||
package com.github.dtschust.zork.parser;
|
package com.github.dtschust.zork.parser;
|
||||||
|
|
||||||
import com.github.dtschust.zork.*;
|
import com.github.dtschust.zork.*;
|
||||||
|
import com.github.dtschust.zork.types.*;
|
||||||
import org.w3c.dom.CharacterData;
|
import org.w3c.dom.CharacterData;
|
||||||
import org.w3c.dom.*;
|
import org.w3c.dom.*;
|
||||||
|
|
||||||
import javax.xml.parsers.DocumentBuilder;
|
import javax.xml.parsers.DocumentBuilder;
|
||||||
import javax.xml.parsers.DocumentBuilderFactory;
|
import javax.xml.parsers.DocumentBuilderFactory;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
import java.util.Iterator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.NoSuchElementException;
|
||||||
|
|
||||||
public class ZorkReader {
|
public class ZorkReader {
|
||||||
|
|
||||||
|
@ -28,7 +31,6 @@ public class ZorkReader {
|
||||||
}
|
}
|
||||||
|
|
||||||
public ZorkGame build() {
|
public ZorkGame build() {
|
||||||
int j, k, l;
|
|
||||||
|
|
||||||
ZorkGame data = new ZorkGame();
|
ZorkGame data = new ZorkGame();
|
||||||
|
|
||||||
|
@ -46,209 +48,29 @@ public class ZorkReader {
|
||||||
Element rootElement = doc.getDocumentElement();
|
Element rootElement = doc.getDocumentElement();
|
||||||
|
|
||||||
/* Every single first generation child is a room, container, creature, or item. So load them in*/
|
/* Every single first generation child is a room, container, creature, or item. So load them in*/
|
||||||
NodeList nodes = rootElement.getChildNodes();
|
for (Node node : iterNodes(rootElement.getChildNodes())) {
|
||||||
for (k = 0; k < nodes.getLength(); k++) {
|
|
||||||
Node node = nodes.item(k);
|
|
||||||
Element element;
|
Element element;
|
||||||
if (node instanceof Element) {
|
if (node instanceof Element) {
|
||||||
element = (Element) node;
|
element = (Element) node;
|
||||||
String tagType = element.getTagName();
|
String tagType = element.getTagName();
|
||||||
|
|
||||||
/* If it's a room ... */
|
switch (tagType) {
|
||||||
if (tagType.equals("room")) {
|
/* If it's a room ... */
|
||||||
ZorkRoom tempRoom = new ZorkRoom();
|
case "room":
|
||||||
|
addRoom(data, element);
|
||||||
/*Get all possible Room attributes*/
|
break;
|
||||||
|
/* If it's an item... */
|
||||||
NodeList name = element.getElementsByTagName("name");
|
case "item":
|
||||||
tempRoom.name = getString((Element) name.item(0));
|
addItem(data, element);
|
||||||
NodeList type = element.getElementsByTagName("type");
|
break;
|
||||||
if (type.getLength() > 0) {
|
/* If it's a container... */
|
||||||
tempRoom.type = getString((Element) type.item(0));
|
case "container":
|
||||||
} else {
|
addContainer(data, element);
|
||||||
tempRoom.type = "regular";
|
break;
|
||||||
}
|
/* And finally, if it's a creature...*/
|
||||||
NodeList status = element.getElementsByTagName("status");
|
case "creature":
|
||||||
if (status.getLength() > 0) {
|
addCreature(data, element);
|
||||||
tempRoom.status = getString((Element) type.item(0));
|
break;
|
||||||
} else {
|
|
||||||
tempRoom.status = "";
|
|
||||||
}
|
|
||||||
NodeList description = element.getElementsByTagName("description");
|
|
||||||
tempRoom.description = getString((Element) description.item(0));
|
|
||||||
|
|
||||||
NodeList items = element.getElementsByTagName("item");
|
|
||||||
for (j = 0; j < items.getLength(); j++) {
|
|
||||||
Element item = (Element) items.item(j);
|
|
||||||
String itemName = getString(item);
|
|
||||||
tempRoom.item.add(itemName);
|
|
||||||
}
|
|
||||||
|
|
||||||
NodeList creatures = element.getElementsByTagName("creature");
|
|
||||||
for (j = 0; j < creatures.getLength(); j++) {
|
|
||||||
Element creature = (Element) creatures.item(j);
|
|
||||||
String creatureName = getString(creature);
|
|
||||||
tempRoom.creature.add(creatureName);
|
|
||||||
}
|
|
||||||
|
|
||||||
readTriggersInObject(element, tempRoom);
|
|
||||||
|
|
||||||
NodeList containers = element.getElementsByTagName("container");
|
|
||||||
for (j = 0; j < containers.getLength(); j++) {
|
|
||||||
Element container = (Element) containers.item(j);
|
|
||||||
String containerName = getString(container);
|
|
||||||
tempRoom.container.add(containerName);
|
|
||||||
}
|
|
||||||
|
|
||||||
NodeList borders = element.getElementsByTagName("border");
|
|
||||||
for (j = 0; j < borders.getLength(); j++) {
|
|
||||||
Element border = (Element) borders.item(j);
|
|
||||||
String borderDirection = getString((Element) border.getElementsByTagName("direction").item(0));
|
|
||||||
String borderName = getString((Element) border.getElementsByTagName("name").item(0));
|
|
||||||
tempRoom.border.put(borderDirection, borderName);
|
|
||||||
}
|
|
||||||
/*Add this room to the rooms hashmap, put it in the generic objects hashmap, and store it's type in the objectlookup hashmap*/
|
|
||||||
data.Rooms.put(tempRoom);
|
|
||||||
data.Objects.put(tempRoom);
|
|
||||||
data.ObjectLookup.put(tempRoom.name, "room");
|
|
||||||
}
|
|
||||||
|
|
||||||
/* If it's an item... */
|
|
||||||
else if (tagType.equals("item")) {
|
|
||||||
ZorkItem tempItem = new ZorkItem();
|
|
||||||
|
|
||||||
/* Get all possible item attributes*/
|
|
||||||
NodeList name = element.getElementsByTagName("name");
|
|
||||||
if (name.getLength() > 0)
|
|
||||||
tempItem.name = getString((Element) name.item(0));
|
|
||||||
|
|
||||||
NodeList status = element.getElementsByTagName("status");
|
|
||||||
if (status.getLength() > 0) {
|
|
||||||
tempItem.status = getString((Element) status.item(0));
|
|
||||||
} else {
|
|
||||||
tempItem.status = "";
|
|
||||||
}
|
|
||||||
|
|
||||||
NodeList description = element.getElementsByTagName("description");
|
|
||||||
if (description.getLength() > 0)
|
|
||||||
tempItem.description = getString((Element) description.item(0));
|
|
||||||
|
|
||||||
NodeList writing = element.getElementsByTagName("writing");
|
|
||||||
if (writing.getLength() > 0)
|
|
||||||
tempItem.writing = getString((Element) writing.item(0));
|
|
||||||
|
|
||||||
NodeList turnon = element.getElementsByTagName("turnon");
|
|
||||||
if (turnon.getLength() > 0) {
|
|
||||||
NodeList prints = element.getElementsByTagName("print");
|
|
||||||
for (j = 0; j < prints.getLength(); j++) {
|
|
||||||
tempItem.turnOnPrint.add(getString((Element) prints.item(j)));
|
|
||||||
}
|
|
||||||
NodeList actions = element.getElementsByTagName("action");
|
|
||||||
for (j = 0; j < actions.getLength(); j++) {
|
|
||||||
tempItem.turnOnAction.add(getString((Element) actions.item(j)));
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
readTriggersInObject(element, tempItem);
|
|
||||||
|
|
||||||
/* Put each item in the items hashmap, the generic objects hashmap, and store its type in objectlookup*/
|
|
||||||
data.Items.put(tempItem);
|
|
||||||
data.Objects.put(tempItem);
|
|
||||||
data.ObjectLookup.put(tempItem.name, "item");
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
/* If it's a container... */
|
|
||||||
else if (tagType.equals("container")) {
|
|
||||||
ZorkContainer tempCont = new ZorkContainer();
|
|
||||||
|
|
||||||
/*Get all possible container attributes*/
|
|
||||||
|
|
||||||
NodeList name = element.getElementsByTagName("name");
|
|
||||||
if (name.getLength() > 0)
|
|
||||||
tempCont.name = getString((Element) name.item(0));
|
|
||||||
|
|
||||||
NodeList status = element.getElementsByTagName("status");
|
|
||||||
if (status.getLength() > 0)
|
|
||||||
tempCont.status = getString((Element) status.item(0));
|
|
||||||
|
|
||||||
/*Initially assume a closed container*/
|
|
||||||
tempCont.isOpen = false;
|
|
||||||
NodeList description = element.getElementsByTagName("description");
|
|
||||||
if (description.getLength() > 0)
|
|
||||||
tempCont.description = getString((Element) description.item(0));
|
|
||||||
|
|
||||||
NodeList accepts = element.getElementsByTagName("accept");
|
|
||||||
for (j = 0; j < accepts.getLength(); j++) {
|
|
||||||
/* If container has an accepts attribute, then it is always open*/
|
|
||||||
tempCont.isOpen = true;
|
|
||||||
tempCont.accept.add(getString((Element) accepts.item(j)));
|
|
||||||
}
|
|
||||||
|
|
||||||
NodeList citems = element.getElementsByTagName("item");
|
|
||||||
for (j = 0; j < citems.getLength(); j++) {
|
|
||||||
Element item = (Element) citems.item(j);
|
|
||||||
String itemName = getString(item);
|
|
||||||
tempCont.item.add(itemName);
|
|
||||||
}
|
|
||||||
|
|
||||||
readTriggersInObject(element, tempCont);
|
|
||||||
|
|
||||||
/* Put each container in the containers hashmap, the generic object hashmap, and the objectlookup hashmap*/
|
|
||||||
data.Containers.put(tempCont);
|
|
||||||
data.Objects.put(tempCont);
|
|
||||||
data.ObjectLookup.put(tempCont.name, "container");
|
|
||||||
}
|
|
||||||
|
|
||||||
/* And finally, if it's a creature...*/
|
|
||||||
else if (tagType.equals("creature")) {
|
|
||||||
ZorkCreature tempCreature = new ZorkCreature();
|
|
||||||
|
|
||||||
/* Get all possible creature attributes*/
|
|
||||||
|
|
||||||
NodeList name = element.getElementsByTagName("name");
|
|
||||||
if (name.getLength() > 0)
|
|
||||||
tempCreature.name = getString((Element) name.item(0));
|
|
||||||
|
|
||||||
NodeList status = element.getElementsByTagName("status");
|
|
||||||
if (status.getLength() > 0)
|
|
||||||
tempCreature.status = getString((Element) status.item(0));
|
|
||||||
|
|
||||||
NodeList description = element.getElementsByTagName("description");
|
|
||||||
if (description.getLength() > 0)
|
|
||||||
tempCreature.description = getString((Element) description.item(0));
|
|
||||||
|
|
||||||
NodeList vulns = element.getElementsByTagName("vulnerability");
|
|
||||||
for (j = 0; j < vulns.getLength(); j++) {
|
|
||||||
String vulnString = getString((Element) vulns.item(j));
|
|
||||||
tempCreature.vulnerability.add(vulnString);
|
|
||||||
}
|
|
||||||
|
|
||||||
NodeList attacks = element.getElementsByTagName("attack");
|
|
||||||
for (j = 0; j < attacks.getLength(); j++) {
|
|
||||||
Element attack = (Element) attacks.item(j);
|
|
||||||
|
|
||||||
readConditionsInTrigger(attack, tempCreature.conditions);
|
|
||||||
|
|
||||||
NodeList prints = attack.getElementsByTagName("print");
|
|
||||||
for (l = 0; l < prints.getLength(); l++) {
|
|
||||||
Element print = (Element) prints.item(l);
|
|
||||||
tempCreature.print.add(getString(print));
|
|
||||||
}
|
|
||||||
NodeList actions = attack.getElementsByTagName("action");
|
|
||||||
for (l = 0; l < actions.getLength(); l++) {
|
|
||||||
Element action = (Element) actions.item(l);
|
|
||||||
tempCreature.action.add(getString(action));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
readTriggersInObject(element, tempCreature);
|
|
||||||
|
|
||||||
/* Put each creature in the creatures hashmap, the generic object hashmap, and the objectlookup hashmap*/
|
|
||||||
data.Creatures.put(tempCreature);
|
|
||||||
data.Objects.put(tempCreature);
|
|
||||||
data.ObjectLookup.put(tempCreature.name, "creature");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -262,16 +84,137 @@ public class ZorkReader {
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static void addCreature(ZorkGame data, Element element) {
|
||||||
|
/* Get all possible creature attributes*/
|
||||||
|
ZorkCreature tempCreature = (ZorkCreature) readZorkObjectAttributes(new ZorkCreature(), element);
|
||||||
|
|
||||||
|
for (Element vuln : iterElements(element.getElementsByTagName("vulnerability"))) {
|
||||||
|
tempCreature.vulnerability.add(getString(vuln));
|
||||||
|
}
|
||||||
|
|
||||||
|
NodeList attacks = element.getElementsByTagName("attack");
|
||||||
|
for (Element attack : iterElements(attacks)) {
|
||||||
|
|
||||||
|
readConditionsInTrigger(attack, tempCreature.conditions);
|
||||||
|
|
||||||
|
for (Element print : iterElements(attack.getElementsByTagName("print"))) {
|
||||||
|
tempCreature.print.add(getString(print));
|
||||||
|
}
|
||||||
|
for (Element action : iterElements(attack.getElementsByTagName("action"))) {
|
||||||
|
tempCreature.action.add(getString(action));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
readTriggersInObject(element, tempCreature);
|
||||||
|
|
||||||
|
/* Put each creature in the creatures hashmap, the generic object hashmap, and the objectlookup hashmap*/
|
||||||
|
data.addObjectThroughLookup("creature", tempCreature);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void addContainer(ZorkGame data, Element element) {
|
||||||
|
/*Get all possible container attributes*/
|
||||||
|
ZorkContainer tempCont = (ZorkContainer) readZorkObjectAttributes(new ZorkContainer(), element);
|
||||||
|
|
||||||
|
/*Initially assume a closed container*/
|
||||||
|
tempCont.isOpen = false;
|
||||||
|
|
||||||
|
for (Element accept : iterElements(element.getElementsByTagName("accept"))) {
|
||||||
|
/* If container has an accepts attribute, then it is always open*/
|
||||||
|
tempCont.isOpen = true;
|
||||||
|
tempCont.accept.add(getString(accept));
|
||||||
|
}
|
||||||
|
|
||||||
|
for (Element item : iterElements(element.getElementsByTagName("item"))) {
|
||||||
|
String itemName = getString(item);
|
||||||
|
tempCont.item.add(itemName);
|
||||||
|
}
|
||||||
|
|
||||||
|
readTriggersInObject(element, tempCont);
|
||||||
|
|
||||||
|
/* Put each container in the containers hashmap, the generic object hashmap, and the objectlookup hashmap*/
|
||||||
|
data.addObjectThroughLookup("container", tempCont);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void addItem(ZorkGame data, Element element) {
|
||||||
|
/* Get all possible item attributes*/
|
||||||
|
ZorkItem tempItem = (ZorkItem) readZorkObjectAttributes(new ZorkItem(), element);
|
||||||
|
|
||||||
|
NodeList writing = element.getElementsByTagName("writing");
|
||||||
|
if (writing.getLength() > 0)
|
||||||
|
tempItem.writing = getString((Element) writing.item(0));
|
||||||
|
|
||||||
|
NodeList turnon = element.getElementsByTagName("turnon");
|
||||||
|
if (turnon.getLength() > 0) {
|
||||||
|
for (Element print : iterElements(element.getElementsByTagName("print"))) {
|
||||||
|
tempItem.turnOnPrint.add(getString(print));
|
||||||
|
}
|
||||||
|
for (Element action : iterElements(element.getElementsByTagName("action"))) {
|
||||||
|
tempItem.turnOnAction.add(getString(action));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
readTriggersInObject(element, tempItem);
|
||||||
|
|
||||||
|
/* Put each item in the items hashmap, the generic objects hashmap, and store its type in objectlookup*/
|
||||||
|
data.addObjectThroughLookup("item", tempItem);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void addRoom(ZorkGame data, Element element) {
|
||||||
|
|
||||||
|
/*Get all possible Room attributes*/
|
||||||
|
ZorkRoom tempRoom = (ZorkRoom) readZorkObjectAttributes(new ZorkRoom(), element);
|
||||||
|
|
||||||
|
NodeList type = element.getElementsByTagName("type");
|
||||||
|
if (type.getLength() > 0) {
|
||||||
|
tempRoom.type = getString((Element) type.item(0));
|
||||||
|
} else {
|
||||||
|
tempRoom.type = "regular";
|
||||||
|
}
|
||||||
|
|
||||||
|
for (Element item : iterElements(element.getElementsByTagName("item"))) {
|
||||||
|
String itemName = getString(item);
|
||||||
|
tempRoom.item.add(itemName);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (Element creature : iterElements(element.getElementsByTagName("creature"))) {
|
||||||
|
String creatureName = getString(creature);
|
||||||
|
tempRoom.creature.add(creatureName);
|
||||||
|
}
|
||||||
|
|
||||||
|
readTriggersInObject(element, tempRoom);
|
||||||
|
|
||||||
|
for (Element container : iterElements(element.getElementsByTagName("container"))) {
|
||||||
|
String containerName = getString(container);
|
||||||
|
tempRoom.container.add(containerName);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (Element border : iterElements(element.getElementsByTagName("border"))) {
|
||||||
|
String borderDirection = getString((Element) border.getElementsByTagName("direction").item(0));
|
||||||
|
String borderName = getString((Element) border.getElementsByTagName("name").item(0));
|
||||||
|
tempRoom.border.put(borderDirection, borderName);
|
||||||
|
}
|
||||||
|
/*Add this room to the rooms hashmap, put it in the generic objects hashmap, and store it's type in the objectlookup hashmap*/
|
||||||
|
data.addObjectThroughLookup("room", tempRoom);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static ZorkObject readZorkObjectAttributes(ZorkObject tempObject, Element element) {
|
||||||
|
NodeList name = element.getElementsByTagName("name");
|
||||||
|
tempObject.name = getString((Element) name.item(0));
|
||||||
|
|
||||||
|
NodeList status = element.getElementsByTagName("status");
|
||||||
|
tempObject.status = (status.getLength() > 0) ? getString((Element) status.item(0)) : "";
|
||||||
|
|
||||||
|
NodeList description = element.getElementsByTagName("description");
|
||||||
|
tempObject.description = (description.getLength() > 0) ? getString((Element) description.item(0)) : "";
|
||||||
|
|
||||||
|
return tempObject;
|
||||||
|
}
|
||||||
|
|
||||||
private static void readTriggersInObject(Element element, ZorkObject tempRoom) {
|
private static void readTriggersInObject(Element element, ZorkObject tempRoom) {
|
||||||
int j;
|
for (Element trigger : iterElements(element.getElementsByTagName("trigger"))) {
|
||||||
int l;
|
|
||||||
NodeList triggers = element.getElementsByTagName("trigger");
|
|
||||||
for (j = 0; j < triggers.getLength(); j++) {
|
|
||||||
ZorkTrigger tempTrigger = new ZorkTrigger();
|
ZorkTrigger tempTrigger = new ZorkTrigger();
|
||||||
Element trigger = (Element) triggers.item(j);
|
for (Element command : iterElements(trigger.getElementsByTagName("command"))) {
|
||||||
NodeList commands = trigger.getElementsByTagName("command");
|
|
||||||
for (l = 0; l < commands.getLength(); l++) {
|
|
||||||
Element command = (Element) commands.item(l);
|
|
||||||
ZorkCommand tempCommand = new ZorkCommand(getString(command));
|
ZorkCommand tempCommand = new ZorkCommand(getString(command));
|
||||||
tempTrigger.conditions.add(tempCommand);
|
tempTrigger.conditions.add(tempCommand);
|
||||||
tempTrigger.hasCommand = true;
|
tempTrigger.hasCommand = true;
|
||||||
|
@ -285,14 +228,10 @@ public class ZorkReader {
|
||||||
} else {
|
} else {
|
||||||
tempTrigger.type = "single";
|
tempTrigger.type = "single";
|
||||||
}
|
}
|
||||||
NodeList prints = trigger.getElementsByTagName("print");
|
for (Element print : iterElements(trigger.getElementsByTagName("print"))) {
|
||||||
for (l = 0; l < prints.getLength(); l++) {
|
|
||||||
Element print = (Element) prints.item(l);
|
|
||||||
tempTrigger.print.add(getString(print));
|
tempTrigger.print.add(getString(print));
|
||||||
}
|
}
|
||||||
NodeList actions = trigger.getElementsByTagName("action");
|
for (Element action : iterElements(trigger.getElementsByTagName("action"))) {
|
||||||
for (l = 0; l < actions.getLength(); l++) {
|
|
||||||
Element action = (Element) actions.item(l);
|
|
||||||
tempTrigger.action.add(getString(action));
|
tempTrigger.action.add(getString(action));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -301,10 +240,7 @@ public class ZorkReader {
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void readConditionsInTrigger(Element trigger, List<? super ZorkCondition> conditionsList) {
|
private static void readConditionsInTrigger(Element trigger, List<? super ZorkCondition> conditionsList) {
|
||||||
int l;
|
for (Element condition : iterElements(trigger.getElementsByTagName("condition"))) {
|
||||||
NodeList conditions = trigger.getElementsByTagName("condition");
|
|
||||||
for (l = 0; l < conditions.getLength(); l++) {
|
|
||||||
Element condition = (Element) conditions.item(l);
|
|
||||||
NodeList object = condition.getElementsByTagName("object");
|
NodeList object = condition.getElementsByTagName("object");
|
||||||
NodeList has = condition.getElementsByTagName("has");
|
NodeList has = condition.getElementsByTagName("has");
|
||||||
if (has.getLength() > 0) {
|
if (has.getLength() > 0) {
|
||||||
|
@ -326,4 +262,27 @@ public class ZorkReader {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static <T> Iterable<T> iterable(final NodeList nodeList) {
|
||||||
|
return () -> new Iterator<>() {
|
||||||
|
private int index = 0;
|
||||||
|
@Override
|
||||||
|
public boolean hasNext() {
|
||||||
|
return index < nodeList.getLength();
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public T next() {
|
||||||
|
if (!hasNext())
|
||||||
|
throw new NoSuchElementException();
|
||||||
|
return (T) nodeList.item(index++);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Iterable<Node> iterNodes(final NodeList nodeList) {
|
||||||
|
return iterable(nodeList);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Iterable<Element> iterElements(final NodeList nodeList) {
|
||||||
|
return iterable(nodeList);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,14 +1,12 @@
|
||||||
package com.github.dtschust.zork;
|
package com.github.dtschust.zork.types;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
/* Container*/
|
/* Container*/
|
||||||
public class ZorkContainer extends ZorkObject {
|
public class ZorkContainer extends ZorkObject {
|
||||||
public Set<String> item = new HashSet<String>();
|
public Set<String> item = new HashSet<String>();
|
||||||
public String description;
|
|
||||||
public ArrayList<String> accept = new ArrayList<>();
|
public ArrayList<String> accept = new ArrayList<>();
|
||||||
public boolean isOpen;
|
public boolean isOpen;
|
||||||
|
|
|
@ -1,13 +1,14 @@
|
||||||
package com.github.dtschust.zork;
|
package com.github.dtschust.zork.types;
|
||||||
|
|
||||||
|
import com.github.dtschust.zork.Zork;
|
||||||
|
import com.github.dtschust.zork.ZorkCondition;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
/* Creature*/
|
/* Creature*/
|
||||||
public class ZorkCreature extends ZorkObject {
|
public class ZorkCreature extends ZorkObject {
|
||||||
public String description;
|
|
||||||
public Set<String> vulnerability = new HashSet<>();
|
public Set<String> vulnerability = new HashSet<>();
|
||||||
public ArrayList<ZorkCondition> conditions = new ArrayList<>();
|
public ArrayList<ZorkCondition> conditions = new ArrayList<>();
|
||||||
public ArrayList<String> print = new ArrayList<>();
|
public ArrayList<String> print = new ArrayList<>();
|
|
@ -1,10 +1,9 @@
|
||||||
package com.github.dtschust.zork;
|
package com.github.dtschust.zork.types;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
|
||||||
/* Item*/
|
/* Item*/
|
||||||
public class ZorkItem extends ZorkObject {
|
public class ZorkItem extends ZorkObject {
|
||||||
public String description;
|
|
||||||
public String writing;
|
public String writing;
|
||||||
public ArrayList<String> turnOnPrint = new ArrayList<>();
|
public ArrayList<String> turnOnPrint = new ArrayList<>();
|
||||||
public ArrayList<String> turnOnAction = new ArrayList<>();
|
public ArrayList<String> turnOnAction = new ArrayList<>();
|
|
@ -1,6 +1,4 @@
|
||||||
package com.github.dtschust.zork.parser;
|
package com.github.dtschust.zork.types;
|
||||||
|
|
||||||
import com.github.dtschust.zork.ZorkObject;
|
|
||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
|
|
@ -1,12 +1,14 @@
|
||||||
package com.github.dtschust.zork;
|
package com.github.dtschust.zork.types;
|
||||||
|
|
||||||
|
import com.github.dtschust.zork.ZorkTrigger;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
|
||||||
/* Generic object, everything inherits from this*/
|
/* Generic object, everything inherits from this*/
|
||||||
public class ZorkObject {
|
public abstract class ZorkObject {
|
||||||
public String status;
|
|
||||||
|
|
||||||
public String name;
|
public String name;
|
||||||
|
public String status;
|
||||||
|
public String description;
|
||||||
public ArrayList<ZorkTrigger> trigger = new ArrayList<>();
|
public ArrayList<ZorkTrigger> trigger = new ArrayList<>();
|
||||||
|
|
||||||
public ZorkObject() {
|
public ZorkObject() {
|
|
@ -1,4 +1,4 @@
|
||||||
package com.github.dtschust.zork;
|
package com.github.dtschust.zork.types;
|
||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
|
@ -7,7 +7,6 @@ import java.util.Set;
|
||||||
/* Room*/
|
/* Room*/
|
||||||
public class ZorkRoom extends ZorkObject {
|
public class ZorkRoom extends ZorkObject {
|
||||||
public String type = "regular";
|
public String type = "regular";
|
||||||
public String description;
|
|
||||||
public HashMap<String, String> border = new HashMap<>();
|
public HashMap<String, String> border = new HashMap<>();
|
||||||
public Set<String> container = new HashSet<>();
|
public Set<String> container = new HashSet<>();
|
||||||
public Set<String> item = new HashSet<>();
|
public Set<String> item = new HashSet<>();
|
Reference in a new issue