[P2play-commit] SF.net SVN: p2play: [14] trunk/P2Play/src/org/p2play/scripting/pnuts
Status: Pre-Alpha
Brought to you by:
tisoft
|
From: <ti...@us...> - 2006-12-27 18:05:33
|
Revision: 14
http://p2play.svn.sourceforge.net/p2play/?rev=14&view=rev
Author: tisoft
Date: 2006-12-27 10:05:30 -0800 (Wed, 27 Dec 2006)
Log Message:
-----------
transfered from university cvs
added license header
changed package name to org.p2play.*
Added Paths:
-----------
trunk/P2Play/src/org/p2play/scripting/pnuts/api/object/
trunk/P2Play/src/org/p2play/scripting/pnuts/api/object/ObjectAPI.java
trunk/P2Play/src/org/p2play/scripting/pnuts/object/
trunk/P2Play/src/org/p2play/scripting/pnuts/object/GameActionClass.java
trunk/P2Play/src/org/p2play/scripting/pnuts/object/GameActionObject.java
trunk/P2Play/src/org/p2play/scripting/pnuts/object/GameClass.java
trunk/P2Play/src/org/p2play/scripting/pnuts/object/GameDescriptor.java
trunk/P2Play/src/org/p2play/scripting/pnuts/object/GameField.java
trunk/P2Play/src/org/p2play/scripting/pnuts/object/GameList.java
trunk/P2Play/src/org/p2play/scripting/pnuts/object/GameMap.java
trunk/P2Play/src/org/p2play/scripting/pnuts/object/GameObject.java
trunk/P2Play/src/org/p2play/scripting/pnuts/object/GameStateClass.java
trunk/P2Play/src/org/p2play/scripting/pnuts/object/GameStateObject.java
trunk/P2Play/src/org/p2play/scripting/pnuts/object/RegionClass.java
trunk/P2Play/src/org/p2play/scripting/pnuts/object/RegionObject.java
Added: trunk/P2Play/src/org/p2play/scripting/pnuts/api/object/ObjectAPI.java
===================================================================
--- trunk/P2Play/src/org/p2play/scripting/pnuts/api/object/ObjectAPI.java (rev 0)
+++ trunk/P2Play/src/org/p2play/scripting/pnuts/api/object/ObjectAPI.java 2006-12-27 18:05:30 UTC (rev 14)
@@ -0,0 +1,180 @@
+/*
+ * Copyright 2006 P2Play.org
+ * All rights reserved.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+package org.p2play.scripting.pnuts.api.object;
+
+import java.util.Iterator;
+
+import org.p2play.scripting.pnuts.api.ScriptAPI;
+import org.p2play.scripting.pnuts.manager.PackageHandler;
+import org.p2play.scripting.pnuts.object.GameClass;
+import org.p2play.scripting.pnuts.object.GameDescriptor;
+import org.p2play.scripting.pnuts.object.GameField;
+import org.p2play.scripting.pnuts.object.GameList;
+import org.p2play.scripting.pnuts.object.GameMap;
+
+import pnuts.lang.Context;
+import pnuts.lang.Package;
+import pnuts.lang.PnutsFunction;
+
+/**
+ *
+ * @author tisoft_media
+ *
+ */
+public class ObjectAPI implements ScriptAPI{
+ private GameDescriptor gameDescriptor;
+
+ public ObjectAPI(GameDescriptor gameDescriptor) {
+ this.gameDescriptor=gameDescriptor;
+ }
+
+ public void registerAPI(PackageHandler packageHandler) {
+ Package chatPackage = packageHandler.getRootPackage();//TODO: Root package for now
+
+ // Register predifined first (Map and List)
+ PackageHandler.addFunction(chatPackage, "Map", new PnutsFunction("Map") {
+
+ @Override
+ public boolean defined(int narg) {
+ return true;// todo check max arhs
+ }
+
+ @Override
+ protected Object exec(Object[] args, Context context) {
+ return createGameMap(context, args);
+
+ }
+
+ });
+
+ PackageHandler.addFunction(chatPackage, "Field", new PnutsFunction(
+ "Field") {
+
+ @Override
+ public boolean defined(int narg) {
+ return true;// todo check max arhs
+ }
+
+ @Override
+ protected Object exec(Object[] args, Context context) {
+ return createGameField(context, args);
+
+ }
+
+ });
+
+ PackageHandler.addFunction(chatPackage, "List",
+ new PnutsFunction("List") {
+
+ @Override
+ public boolean defined(int narg) {
+ return true;// todo check max arhs
+ }
+
+ @Override
+ protected Object exec(Object[] args, Context context) {
+ return createGameList(context, args);
+
+ }
+
+ });
+
+ Iterator<String> names = gameDescriptor.getGameActionMap().keySet()
+ .iterator();
+ while (names.hasNext()) {
+ String name = names.next().intern();
+
+ final GameClass clazz = gameDescriptor.getGameActionMap().get(name);
+
+ PackageHandler.addFunction(chatPackage, name,
+ new PnutsFunction(name) {
+
+ @Override
+ public boolean defined(int narg) {
+ return true;// todo check max arhs
+ }
+
+ @Override
+ protected Object exec(Object[] args, Context context) {
+ return createInstance(context, clazz, args);
+
+ }
+
+ });
+
+ }
+
+ names = gameDescriptor.getGameClassMap().keySet().iterator();
+ while (names.hasNext()) {
+ String name = names.next().intern();
+
+ final GameClass clazz = gameDescriptor.getGameClassMap().get(name);
+
+ PackageHandler.addFunction(chatPackage, name,
+ new PnutsFunction(name) {
+
+ @Override
+ public boolean defined(int narg) {
+ return true;// todo check max arhs
+ }
+
+ @Override
+ protected Object exec(Object[] args, Context context) {
+ return createInstance(context, clazz, args);
+
+ }
+
+ });
+
+ }
+
+ //finally register the reguion
+ PackageHandler.addFunction(chatPackage, gameDescriptor.getRegionClass().getType(), new PnutsFunction(gameDescriptor.getRegionClass().getType()) {
+
+ @Override
+ public boolean defined(int narg) {
+ return true;// todo check max args
+ }
+
+ @Override
+ protected Object exec(Object[] args, Context context) {
+ return createInstance(context, gameDescriptor.getRegionClass(), args);
+
+ }
+
+ });
+ }
+
+ private Object createInstance(Context context, GameClass prototype,
+ Object[] args) {
+ return prototype.createInstance(args);
+ }
+
+ private Object createGameMap(Context context, Object[] args) {
+ return new GameMap();
+ }
+
+ private Object createGameField(Context context, Object[] args) {
+ return new GameField();
+ }
+
+ private Object createGameList(Context context, Object[] args) {
+ return new GameList();
+ }
+}
\ No newline at end of file
Added: trunk/P2Play/src/org/p2play/scripting/pnuts/object/GameActionClass.java
===================================================================
--- trunk/P2Play/src/org/p2play/scripting/pnuts/object/GameActionClass.java (rev 0)
+++ trunk/P2Play/src/org/p2play/scripting/pnuts/object/GameActionClass.java 2006-12-27 18:05:30 UTC (rev 14)
@@ -0,0 +1,53 @@
+/*
+ * Copyright 2006 P2Play.org
+ * All rights reserved.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+package org.p2play.scripting.pnuts.object;
+
+import org.jdom.Element;
+import org.p2play.net.region.Region;
+
+public class GameActionClass extends GameClass {
+
+ private Region region;
+
+ public GameActionClass(String game, String name, Element element) {
+ super(game, name, element);
+ }
+
+ public void setRegion(Region region) {
+ this.region = region;
+ }
+
+ @Override
+ public GameObject createInstance(Object... objects) {
+ GameObject gameObject= new GameActionObject(this,region);
+
+ if(objects!=null){
+ for (int i = 0; i < objects.length; i++) {
+ try{
+ gameObject.set(getVariable(i), objects[i]);
+ }
+ catch(Exception e){
+ throw new IllegalArgumentException("Too many parameters in Constructor for "+getType());
+ }
+ }
+ }
+
+ return gameObject;
+ }
+}
Added: trunk/P2Play/src/org/p2play/scripting/pnuts/object/GameActionObject.java
===================================================================
--- trunk/P2Play/src/org/p2play/scripting/pnuts/object/GameActionObject.java (rev 0)
+++ trunk/P2Play/src/org/p2play/scripting/pnuts/object/GameActionObject.java 2006-12-27 18:05:30 UTC (rev 14)
@@ -0,0 +1,79 @@
+/*
+ * Copyright 2006 P2Play.org
+ * All rights reserved.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+package org.p2play.scripting.pnuts.object;
+
+import java.io.IOException;
+
+import org.p2play.net.region.GameAction;
+import org.p2play.net.region.Region;
+
+import pnuts.lang.Context;
+import rice.p2p.commonapi.rawserialization.InputBuffer;
+import rice.p2p.commonapi.rawserialization.OutputBuffer;
+
+public class GameActionObject extends GameObject implements GameAction {
+
+ private Region region;
+
+ public GameActionObject(GameClass gameClass, Region region) {
+ super(gameClass);
+ this.region = region;
+ }
+
+ public GameActionObject(InputBuffer buffer) throws IOException {
+ super(buffer);
+ region = (Region) deserializeObject(buffer);
+ }
+
+ public long getGameTime() {
+ return 0;
+ }
+
+ public Region getRegion() {
+ return region;
+ }
+
+ @Override
+ public Object get(String name, Context context) {
+ if ("Type".equals(name)) {
+ return getType().intern();
+ } else {
+ return super.get(name, context);
+ }
+ }
+
+ @Override
+ public GameClass getGameClass() {
+ if (super.getGameClass() == null)
+ setGameClass(GameDescriptor.getGameDescriptor(getGame())
+ .getGameActionMap().get(getType()));
+ return super.getGameClass();
+ }
+
+ public boolean sendToAll() {
+ // TODO Auto-generated method stub
+ return false;
+ }
+
+ @Override
+ public void doSerialize(OutputBuffer buffer) throws IOException {
+ super.doSerialize(buffer);
+ serializeObject(buffer, region);
+ }
+}
Added: trunk/P2Play/src/org/p2play/scripting/pnuts/object/GameClass.java
===================================================================
--- trunk/P2Play/src/org/p2play/scripting/pnuts/object/GameClass.java (rev 0)
+++ trunk/P2Play/src/org/p2play/scripting/pnuts/object/GameClass.java 2006-12-27 18:05:30 UTC (rev 14)
@@ -0,0 +1,173 @@
+/*
+ * Copyright 2006 P2Play.org
+ * All rights reserved.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+package org.p2play.scripting.pnuts.object;
+
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+
+import org.jdom.Element;
+
+public class GameClass {
+ private Map<String, Object> variableMap; // Name,Type
+ private List<String> variableList;
+
+ private String type;
+
+ private Element element;
+
+ private String game;
+
+ public GameClass(String game, String name, Element element) {
+ super();
+ this.game=game;
+ this.type = name;
+ this.element = element;
+ variableMap = new HashMap<String, Object>();
+ variableList=new LinkedList<String>();
+ }
+
+ @SuppressWarnings("unchecked")
+ void parse(HashMap<String, GameClass> classMap) {
+ Iterator<Element> varElementIterator = element.getChildren("var")
+ .iterator();
+
+ while (varElementIterator.hasNext()) {
+ Element element = varElementIterator.next();
+
+ String name = element.getAttributeValue("name").trim();
+ String type = element.getAttributeValue("type").trim();
+
+ if (classMap.containsKey(type)) {
+ // its a userdefined class
+ addVariable(name, classMap.get(type));
+ } else if (type.equals("List")) {
+ addVariable(name, List.class);
+ } else if (type.equals("Field")) {
+ addVariable(name, GameField.class);
+ }else if (type.equals("Map")) {
+ addVariable(name, Map.class);
+ } else if (type.equals("String")) {
+ addVariable(name, String.class);
+ } else if (convertPrimitiveFromString(type) != null) {
+ addVariable(name, convertPrimitiveFromString(type));
+ } else if (type.equals("any")) {
+ addVariable(name, Object.class);
+ }
+ }
+
+ System.out.println(getType() + ": " + variableMap);
+ }
+
+ void addVariable(String name, Object type){
+ variableMap.put(name, type);
+ variableList.add(name);
+ }
+
+ String getVariable(int index){
+ return variableList.get(index);
+ }
+
+ Iterator<String> iteratorOfVariables(){
+ return variableList.iterator();
+ }
+
+ public String getType() {
+ return type;
+ }
+
+ public String getGame() {
+ return game;
+ }
+
+ public void checkVariable(String name, Object value) {
+ //System.out.println("Checking: "+name+" ("+variableMap.get(name)+")="+value);
+ if(variableMap.get(name)==null)
+ throw new RuntimeException(new IllegalAccessException("Variable "+name+" is not defined for type "+getType()));
+ //TODO: typecheck
+ }
+
+ public GameObject createInstance(Object... objects){
+ GameObject gameObject= new GameObject(this);
+
+ fillObject(gameObject, objects);
+
+ return gameObject;
+ }
+
+ protected void fillObject(GameObject gameObject, Object... objects) {
+ if(objects!=null){
+ for (int i = 0; i < objects.length; i++) {
+ try{
+ gameObject.set(getVariable(i), objects[i]);
+ }
+ catch(Exception e){
+ throw new IllegalArgumentException("Too many parameters in Constructor for "+getType());
+ }
+ }
+ }
+ }
+
+ public Class convertPrimitiveFromString(String s) {
+ if (s.equalsIgnoreCase("boolean"))
+ return Boolean.class;
+ if (s.equalsIgnoreCase("char"))
+ return Character.class;
+ if (s.equalsIgnoreCase("byte"))
+ return Byte.class;
+ if (s.equalsIgnoreCase("short"))
+ return Short.class;
+ if (s.equalsIgnoreCase("int"))
+ return Integer.class;
+ if (s.equalsIgnoreCase("long"))
+ return Long.class;
+ if (s.equalsIgnoreCase("float"))
+ return Float.class;
+ if (s.equalsIgnoreCase("double"))
+ return Double.class;
+ return null;
+ }
+
+ public static Class convertPrimitive(Class c) {
+ if (c == boolean.class)
+ return Boolean.class;
+ if (c == char.class)
+ return Character.class;
+ if (c == byte.class)
+ return Byte.class;
+ if (c == short.class)
+ return Short.class;
+ if (c == int.class)
+ return Integer.class;
+ if (c == long.class)
+ return Long.class;
+ if (c == float.class)
+ return Float.class;
+ if (c == double.class)
+ return Double.class;
+ return c;
+ }
+
+ public static boolean isPrimitive(Object object){
+ return object.getClass().isPrimitive();
+ }
+
+}
Added: trunk/P2Play/src/org/p2play/scripting/pnuts/object/GameDescriptor.java
===================================================================
--- trunk/P2Play/src/org/p2play/scripting/pnuts/object/GameDescriptor.java (rev 0)
+++ trunk/P2Play/src/org/p2play/scripting/pnuts/object/GameDescriptor.java 2006-12-27 18:05:30 UTC (rev 14)
@@ -0,0 +1,253 @@
+/*
+ * Copyright 2006 P2Play.org
+ * All rights reserved.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+package org.p2play.scripting.pnuts.object;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.LinkedList;
+import java.util.List;
+
+import org.jdom.Document;
+import org.jdom.Element;
+import org.jdom.JDOMException;
+import org.jdom.input.SAXBuilder;
+import org.p2play.net.region.GameState;
+import org.p2play.net.region.Region;
+import org.p2play.resource.ResourceClassLoader;
+import org.p2play.scripting.pnuts.PnutsHandler;
+import org.p2play.scripting.pnuts.api.object.ObjectAPI;
+import org.p2play.scripting.pnuts.manager.PackageHandler;
+
+
+public class GameDescriptor {
+ private static HashMap<String, GameDescriptor> gameDescriptorMap = new HashMap<String, GameDescriptor>();
+
+ private String clientScript;
+
+ private String serverScript;
+
+ private GameStateClass gameStateClass;
+
+ private HashMap<String, GameClass> gameClassMap;
+
+ private HashMap<String, GameActionClass> gameActionMap;
+
+ private String neighbourFunction;
+
+ private String emptyFunction;
+
+ private RegionClass regionClass;
+
+ private String game;
+
+ private PnutsHandler pnutsHandler;
+
+ private GameDescriptor() {
+ gameClassMap = new HashMap<String, GameClass>();
+ gameActionMap = new HashMap<String, GameActionClass>();
+ }
+
+ public String getClientScript() {
+ return clientScript;
+ }
+
+ public HashMap<String, GameClass> getGameClassMap() {
+ return gameClassMap;
+ }
+
+ public HashMap<String, GameActionClass> getGameActionMap() {
+ return gameActionMap;
+ }
+
+ public GameStateClass getGameStateClass() {
+ return gameStateClass;
+ }
+
+ public RegionClass getRegionClass() {
+ return regionClass;
+ }
+
+ public String getServerScript() {
+ return serverScript;
+ }
+
+ public String getNeighbourFunction() {
+ return neighbourFunction;
+ }
+
+ public String getEmptyFunction() {
+ return emptyFunction;
+ }
+
+ @SuppressWarnings("unchecked")
+ private static GameDescriptor parse(InputStream inputStream) {
+ GameDescriptor gameDescriptor = new GameDescriptor();
+
+ SAXBuilder builder = new SAXBuilder();
+ Document doc = null;
+ try {
+ doc = builder.build(inputStream);
+ } catch (JDOMException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ } catch (IOException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ Element root = doc.getRootElement();
+
+ gameDescriptor.game = root.getAttributeValue("name");
+
+ // load neighbour function
+ Element neighbourElement = root.getChild("neighbour");
+ if (neighbourElement != null)
+ gameDescriptor.neighbourFunction = neighbourElement.getValue()
+ .trim();
+
+ // load empty function
+ Element emptyElement = root.getChild("empty");
+ if (emptyElement != null)
+ gameDescriptor.emptyFunction = emptyElement.getValue().trim();
+
+ // load script names
+ Iterator<Element> scriptElementIterator = root.getChildren("script")
+ .iterator();
+ while (scriptElementIterator.hasNext()) {
+ Element element = scriptElementIterator.next();
+
+ String type = element.getAttributeValue("type");
+
+ if (type.equals("client"))
+ gameDescriptor.clientScript = element.getValue().trim();
+ else if (type.equals("server"))
+ gameDescriptor.serverScript = element.getValue().trim();
+ }
+
+ // create classes
+ Iterator<Element> classElementIterator = root.getChild("classes")
+ .getChildren("class").iterator();
+ while (classElementIterator.hasNext()) {
+ Element element = classElementIterator.next();
+
+ String type = element.getAttributeValue("type").trim();
+
+ gameDescriptor.gameClassMap.put(type, new GameClass(
+ gameDescriptor.game, type, element));
+ }
+
+ // then fill in the variables
+ Iterator<GameClass> gameClassIterator = gameDescriptor.gameClassMap
+ .values().iterator();
+ while (gameClassIterator.hasNext()) {
+ GameClass gameClass = gameClassIterator.next();
+
+ gameClass.parse(gameDescriptor.gameClassMap);
+ }
+
+ // create Actions
+ Iterator<Element> actionElementIterator = root.getChild("actions")
+ .getChildren("action").iterator();
+ while (actionElementIterator.hasNext()) {
+ Element element = actionElementIterator.next();
+
+ String type = element.getAttributeValue("type").trim();
+
+ gameDescriptor.gameActionMap.put(type, new GameActionClass(
+ gameDescriptor.game, type, element));
+
+ gameDescriptor.gameActionMap.get(type).parse(
+ gameDescriptor.gameClassMap);
+ }
+
+ // create game state class
+ gameDescriptor.gameStateClass = new GameStateClass(gameDescriptor.game,
+ "GameState", root.getChild("state"));
+ gameDescriptor.gameStateClass.parse(gameDescriptor.gameClassMap);
+
+ // create region class
+ gameDescriptor.regionClass = new RegionClass(gameDescriptor.game, root
+ .getChild("region").getAttributeValue("type"), root
+ .getChild("region"));
+ gameDescriptor.regionClass.parse(gameDescriptor.gameClassMap);
+
+ return gameDescriptor;
+ }
+
+ public static GameDescriptor getGameDescriptor(String game) {
+ synchronized (gameDescriptorMap) {
+ GameDescriptor gameDescriptor = gameDescriptorMap.get(game);
+
+ if (gameDescriptor == null) {
+ gameDescriptor = parse(ResourceClassLoader
+ .getResourceClassLoader().getResourceAsStream(
+ game + ".xml"));
+ gameDescriptorMap.put(game, gameDescriptor);
+ }
+
+ return gameDescriptor;
+ }
+ }
+
+ public List<Region> getNeighbourRegionIdentifiers(Region region) {
+ LinkedList<Region> neigbours = new LinkedList<Region>();
+ PnutsHandler pnutsHandler=getNutCracker();
+ if (getNeighbourFunction() != null) {
+ Object[] tmp = (Object[]) pnutsHandler.callFunction(
+ getNeighbourFunction(), region);
+
+ for (int i = 0; i < tmp.length; i++) {
+ neigbours.add((Region) tmp[i]);
+ }
+
+ }
+
+ if (!neigbours.contains(region))
+ neigbours.add(region);
+
+ return neigbours;
+
+ }
+
+ private PnutsHandler getNutCracker() {
+ if (pnutsHandler == null) {
+ PackageHandler packageHandler = new PackageHandler();
+
+ new ObjectAPI(this).registerAPI(packageHandler);
+
+ pnutsHandler = new PnutsHandler(packageHandler);
+ try {
+ pnutsHandler.execute(pnutsHandler.getPnuts(ResourceClassLoader
+ .getResourceClassLoader().getResourceAsStream(
+ getServerScript())));
+ } catch (Exception e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ }
+
+ return pnutsHandler;
+ }
+
+ public boolean isIdle(GameState gameState){
+ return (Boolean)pnutsHandler.callFunction(
+ getEmptyFunction(), gameState);
+ }
+}
Added: trunk/P2Play/src/org/p2play/scripting/pnuts/object/GameField.java
===================================================================
--- trunk/P2Play/src/org/p2play/scripting/pnuts/object/GameField.java (rev 0)
+++ trunk/P2Play/src/org/p2play/scripting/pnuts/object/GameField.java 2006-12-27 18:05:30 UTC (rev 14)
@@ -0,0 +1,44 @@
+/*
+ * Copyright 2006 P2Play.org
+ * All rights reserved.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+package org.p2play.scripting.pnuts.object;
+
+import java.io.IOException;
+
+import rice.p2p.commonapi.rawserialization.InputBuffer;
+
+public class GameField extends GameMap{
+
+ public GameField() {
+ super();
+ }
+
+ public GameField(InputBuffer buffer) throws IOException {
+ super(buffer);
+ }
+
+ @Override
+ public Object get(Object key) {
+ GameMap map=(GameMap) super.get(key);
+ if(map==null){
+ map=new GameMap();
+ put(key, map);
+ }
+ return map;
+ }
+}
Added: trunk/P2Play/src/org/p2play/scripting/pnuts/object/GameList.java
===================================================================
--- trunk/P2Play/src/org/p2play/scripting/pnuts/object/GameList.java (rev 0)
+++ trunk/P2Play/src/org/p2play/scripting/pnuts/object/GameList.java 2006-12-27 18:05:30 UTC (rev 14)
@@ -0,0 +1,160 @@
+/*
+ * Copyright 2006 P2Play.org
+ * All rights reserved.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+package org.p2play.scripting.pnuts.object;
+
+import java.io.IOException;
+import java.io.Serializable;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.List;
+import java.util.ListIterator;
+
+import rice.p2p.commonapi.rawserialization.InputBuffer;
+import rice.p2p.commonapi.rawserialization.OutputBuffer;
+
+public class GameList implements List<Object>, Serializable {
+ private List<Object> list;
+
+ public GameList() {
+ this.list = new ArrayList<Object>();
+ }
+
+ public GameList(InputBuffer buffer) throws IOException {
+ this();
+
+ int size = buffer.readInt();
+ for (int i = 0; i < size; i++) {
+ list.add(GameObject.deserializeObject(buffer));
+ }
+ }
+
+ public void add(int index, Object element) {
+ list.add(index, element);
+ }
+
+ public boolean add(Object o) {
+ return list.add(o);
+ }
+
+ public boolean addAll(Collection<? extends Object> c) {
+ return list.addAll(c);
+ }
+
+ public boolean addAll(int index, Collection<? extends Object> c) {
+ return list.addAll(index, c);
+ }
+
+ public void clear() {
+ list.clear();
+ }
+
+ public boolean contains(Object o) {
+ return list.contains(o);
+ }
+
+ public boolean containsAll(Collection<? extends Object> c) {
+ return list.containsAll(c);
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ return list.equals(o);
+ }
+
+ public Object get(int index) {
+ return list.get(index);
+ }
+
+ @Override
+ public int hashCode() {
+ return list.hashCode();
+ }
+
+ public int indexOf(Object o) {
+ return list.indexOf(o);
+ }
+
+ public boolean isEmpty() {
+ return list.isEmpty();
+ }
+
+ public Iterator<Object> iterator() {
+ return list.iterator();
+ }
+
+ public int lastIndexOf(Object o) {
+ return list.lastIndexOf(o);
+ }
+
+ public ListIterator<Object> listIterator() {
+ return list.listIterator();
+ }
+
+ public ListIterator<Object> listIterator(int index) {
+ return list.listIterator(index);
+ }
+
+ public Object remove(int index) {
+ return list.remove(index);
+ }
+
+ public boolean remove(Object o) {
+ return list.remove(o);
+ }
+
+ public boolean removeAll(Collection c) {
+ return list.removeAll(c);
+ }
+
+ public boolean retainAll(Collection c) {
+ return list.retainAll(c);
+ }
+
+ public Object set(int index, Object element) {
+ return list.set(index, element);
+ }
+
+ public int size() {
+ return list.size();
+ }
+
+ public List<Object> subList(int fromIndex, int toIndex) {
+ return list.subList(fromIndex, toIndex);
+ }
+
+ public Object[] toArray() {
+ return list.toArray();
+ }
+
+ public <T> T[] toArray(T[] a) {
+ return list.toArray(a);
+ }
+
+ public void doSerialize(OutputBuffer buffer) throws IOException {
+ Object[] objects = toArray();
+
+ buffer.writeInt(objects.length);
+
+ for (int i = 0; i < objects.length; i++) {
+ GameObject.serializeObject(buffer, objects[i]);
+ }
+ }
+
+}
Added: trunk/P2Play/src/org/p2play/scripting/pnuts/object/GameMap.java
===================================================================
--- trunk/P2Play/src/org/p2play/scripting/pnuts/object/GameMap.java (rev 0)
+++ trunk/P2Play/src/org/p2play/scripting/pnuts/object/GameMap.java 2006-12-27 18:05:30 UTC (rev 14)
@@ -0,0 +1,125 @@
+/*
+ * Copyright 2006 P2Play.org
+ * All rights reserved.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+package org.p2play.scripting.pnuts.object;
+
+import java.io.IOException;
+import java.io.Serializable;
+import java.util.Collection;
+import java.util.Map;
+import java.util.Set;
+
+import javolution.util.FastMap;
+import rice.p2p.commonapi.rawserialization.InputBuffer;
+import rice.p2p.commonapi.rawserialization.OutputBuffer;
+
+public class GameMap implements Map<Object, Object>, Serializable {
+ private Map<Object, Object> map;
+
+ public GameMap() {
+ this.map = new FastMap<Object, Object>();
+ }
+
+ public GameMap(InputBuffer buffer) throws IOException {
+ this();
+
+ int size = buffer.readInt();
+
+ for (int i = 0; i < size; i++) {
+ put(GameObject.deserializeObject(buffer), GameObject
+ .deserializeObject(buffer));
+ }
+ }
+
+ public void clear() {
+ map.clear();
+ }
+
+ public boolean containsKey(Object key) {
+ return map.containsKey(key);
+ }
+
+ public boolean containsValue(Object value) {
+ return map.containsValue(value);
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ return map.equals(o);
+ }
+
+ public Object get(Object key) {
+ return map.get(key);
+ }
+
+ @Override
+ public int hashCode() {
+ return map.hashCode();
+ }
+
+ public boolean isEmpty() {
+ return map.isEmpty();
+ }
+
+ public Set<Object> keySet() {
+ return map.keySet();
+ }
+
+ public Object put(Object key, Object value) {
+ return map.put(key, value);
+ }
+
+ public Object remove(Object key) {
+ return map.remove(key);
+ }
+
+ public int size() {
+ return map.size();
+ }
+
+ public Collection<Object> values() {
+ return map.values();
+ }
+
+ @Override
+ public String toString() {
+ // TODO Auto-generated method stub
+ return "GameMap: " + map.toString();
+ }
+
+ public void doSerialize(OutputBuffer buffer) throws IOException {
+ Object keys[] = map.keySet().toArray();
+
+ buffer.writeInt(keys.length);
+
+ for (int i = 0; i < keys.length; i++) {
+ GameObject.serializeObject(buffer, keys[i]);
+ GameObject.serializeObject(buffer, map.get(keys[i]));
+ }
+ }
+
+ public void putAll(Map<? extends Object, ? extends Object> t) {
+ map.putAll(t);
+
+ }
+
+ public Set<java.util.Map.Entry<Object, Object>> entrySet() {
+ return map.entrySet();
+ }
+
+}
Added: trunk/P2Play/src/org/p2play/scripting/pnuts/object/GameObject.java
===================================================================
--- trunk/P2Play/src/org/p2play/scripting/pnuts/object/GameObject.java (rev 0)
+++ trunk/P2Play/src/org/p2play/scripting/pnuts/object/GameObject.java 2006-12-27 18:05:30 UTC (rev 14)
@@ -0,0 +1,352 @@
+/*
+ * Copyright 2006 P2Play.org
+ * All rights reserved.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+package org.p2play.scripting.pnuts.object;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import java.io.OutputStream;
+import java.io.Serializable;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map;
+
+import org.p2play.util.Hashable;
+import org.p2play.util.NetRandom;
+
+import pnuts.lang.Context;
+import pnuts.lang.Property;
+import rice.p2p.commonapi.rawserialization.InputBuffer;
+import rice.p2p.commonapi.rawserialization.OutputBuffer;
+
+public class GameObject implements Serializable, Property, Hashable {
+ public static final byte TYPE_NULL = 0;
+
+ public static final byte TYPE_BOOLEAN = 1;
+
+ public static final byte TYPE_CHARACTER = 2;
+
+ public static final byte TYPE_BYTE = 3;
+
+ public static final byte TYPE_SHORT = 4;
+
+ public static final byte TYPE_INTEGER = 5;
+
+ public static final byte TYPE_LONG = 6;
+
+ public static final byte TYPE_FLOAT = 7;
+
+ public static final byte TYPE_STRING = 8;
+
+ public static final byte TYPE_GAMELIST = 9;
+
+ public static final byte TYPE_GAMEMAP = 10;
+
+ public static final byte TYPE_GAMEFIELD = 11;
+
+ public static final byte TYPE_GAMESTATEOBJECT = 12;
+
+ public static final byte TYPE_GAMEACTIONOBJECT = 13;
+
+ public static final byte TYPE_REGIONOBJECT = 14;
+
+ public static final byte TYPE_GAMEOBJECT = 15;
+
+ public static final byte TYPE_NETRANDOM = 16;
+
+ public static final byte TYPE_OBJECT = 17;
+
+ private transient GameClass gameClass;
+
+ private Map<String, Object> content;
+
+ private String type;
+
+ private String game;
+
+ GameObject(GameClass gameClass) {
+ super();
+ this.type = gameClass.getType();
+ this.game = gameClass.getGame();
+ this.gameClass = gameClass;
+ content = new HashMap<String, Object>();
+ }
+
+ public GameObject(InputBuffer buffer) throws IOException {
+ super();
+ // read game and type
+ game = buffer.readUTF();
+ type = buffer.readUTF();
+
+ content = new HashMap<String, Object>();
+
+ // now retrieve the content
+ Iterator<String> variables = getGameClass().iteratorOfVariables();
+ while (variables.hasNext()) {
+ String variable = variables.next();
+ Object object = deserializeObject(buffer);
+ if (object != null)
+ content.put(variable, object);
+
+ }
+ }
+
+ public Object get(String name, Context context) {
+ return content.get(name);
+ }
+
+ public void set(String name, Object value, Context context) {
+ getGameClass().checkVariable(name, value);
+ content.put(name, value);
+ }
+
+ protected void setGameClass(GameClass gameClass) {
+ this.gameClass = gameClass;
+ }
+
+ public GameClass getGameClass() {
+ if (gameClass == null)
+ gameClass = GameDescriptor.getGameDescriptor(game)
+ .getGameClassMap().get(type);
+ return gameClass;
+ }
+
+ public Object get(String name) {
+ return get(name, null);
+ }
+
+ public void set(String name, Object value) {
+ set(name, value, null);
+ }
+
+ public String getGame() {
+ return game;
+ }
+
+ public String getType() {
+ return type;
+ }
+
+ public int hash() {
+ return 0;
+ }
+
+ @Override
+ public String toString() {
+ return "Game: " + game + " Type: " + type + " Content: " + content;
+ }
+
+ public void doSerialize(OutputBuffer buffer) throws IOException {
+ // first write the game and type
+ buffer.writeUTF(game);
+ buffer.writeUTF(type);
+
+ // now store the content
+ Iterator<String> variables = getGameClass().iteratorOfVariables();
+ while (variables.hasNext()) {
+ String variable = variables.next();
+ Object object = content.get(variable);
+ serializeObject(buffer, object);
+ }
+ }
+
+ public static byte getType(Object object) {
+ if (object == null)
+ return TYPE_NULL;
+ else if (object instanceof Boolean)
+ return TYPE_BOOLEAN;
+ else if (object instanceof Character)
+ return TYPE_CHARACTER;
+ else if (object instanceof Byte)
+ return TYPE_BYTE;
+ else if (object instanceof Short)
+ return TYPE_SHORT;
+ else if (object instanceof Integer)
+ return TYPE_INTEGER;
+ else if (object instanceof Long)
+ return TYPE_LONG;
+ else if (object instanceof Float)
+ return TYPE_FLOAT;
+ else if (object instanceof String)
+ return TYPE_STRING;
+ else if (object instanceof GameList)
+ return TYPE_GAMELIST;
+ else if (object instanceof GameField)// this has to be before
+ // GameMap, because it is a
+ // subtype of it
+ return TYPE_GAMEFIELD;
+ else if (object instanceof GameMap)
+ return TYPE_GAMEMAP;
+ else if (object instanceof RegionObject)// this has to be before
+ // GameObject, because it is a
+ // subtype of it
+ return TYPE_REGIONOBJECT;
+ else if (object instanceof GameStateObject)// this has to be before
+ // GameObject, because it is
+ // a subtype of it
+ return TYPE_GAMESTATEOBJECT;
+ else if (object instanceof GameActionObject)// this has to be before
+ // GameObject, because it is
+ // a subtype of it
+ return TYPE_GAMEACTIONOBJECT;
+ else if (object instanceof GameObject)
+ return TYPE_GAMEOBJECT;
+ else if (object instanceof NetRandom)
+ return TYPE_NETRANDOM;
+ else
+ return TYPE_OBJECT;
+ }
+
+ public static void serializeObject(OutputBuffer buffer, Object object)
+ throws IOException {
+ byte type = getType(object);
+ buffer.writeByte(type);
+
+ switch (type) {
+ case TYPE_NULL:
+ // do nothing
+ break;
+ case TYPE_BOOLEAN:
+ buffer.writeBoolean((Boolean) object);
+ break;
+ case TYPE_CHARACTER:
+ buffer.writeChar((Character) object);
+ break;
+ case TYPE_BYTE:
+ buffer.writeByte((Byte) object);
+ break;
+ case TYPE_SHORT:
+ buffer.writeShort((Short) object);
+ break;
+ case TYPE_INTEGER:
+ buffer.writeInt((Integer) object);
+ break;
+ case TYPE_LONG:
+ buffer.writeLong((Long) object);
+ break;
+ case TYPE_FLOAT:
+ buffer.writeFloat((Float) object);
+ break;
+ case TYPE_STRING:
+ buffer.writeUTF((String) object);
+ break;
+ case TYPE_GAMELIST:
+ ((GameList) object).doSerialize(buffer);
+ break;
+ case TYPE_GAMEMAP:
+ ((GameMap) object).doSerialize(buffer);
+ break;
+ case TYPE_GAMEFIELD:
+ ((GameField) object).doSerialize(buffer);
+ break;
+ case TYPE_GAMESTATEOBJECT:
+ ((GameStateObject) object).doSerialize(buffer);
+ break;
+ case TYPE_GAMEACTIONOBJECT:
+ ((GameActionObject) object).doSerialize(buffer);
+ break;
+ case TYPE_REGIONOBJECT:
+ ((RegionObject) object).doSerialize(buffer);
+ break;
+ case TYPE_GAMEOBJECT:
+ ((GameObject) object).doSerialize(buffer);
+ break;
+ case TYPE_NETRANDOM:
+ ((NetRandom) object).doSerialize(buffer);
+ break;
+ case TYPE_OBJECT:
+ // java serialisation...
+ ByteArrayOutputStream bos = new ByteArrayOutputStream();
+ OutputStream gos = bos;// new GZIPOutputStream(bos);
+ ObjectOutputStream oos = new ObjectOutputStream(gos);
+
+ oos.writeObject(object);
+
+ gos.close();
+
+ byte[] b = bos.toByteArray();
+ buffer.writeInt(b.length);
+ buffer.write(b, 0, b.length);
+ break;
+ default:
+ throw new IllegalArgumentException(
+ "Object serialisation not supported: " + type + " "
+ + object.getClass() + " " + object);
+ }
+ }
+
+ public static Object deserializeObject(InputBuffer buffer)
+ throws IOException {
+ byte type = buffer.readByte();
+
+ switch (type) {
+ case TYPE_NULL:
+ return null;
+ case TYPE_BOOLEAN:
+ return buffer.readBoolean();
+ case TYPE_CHARACTER:
+ return buffer.readChar();
+ case TYPE_BYTE:
+ return buffer.readByte();
+ case TYPE_SHORT:
+ return buffer.readShort();
+ case TYPE_INTEGER:
+ return buffer.readInt();
+ case TYPE_LONG:
+ return buffer.readLong();
+ case TYPE_FLOAT:
+ return buffer.readFloat();
+ case TYPE_STRING:
+ return buffer.readUTF();
+ case TYPE_GAMELIST:
+ return new GameList(buffer);
+ case TYPE_GAMEMAP:
+ return new GameMap(buffer);
+ case TYPE_GAMEFIELD:
+ return new GameField(buffer);
+ case TYPE_GAMESTATEOBJECT:
+ return new GameStateObject(buffer);
+ case TYPE_GAMEACTIONOBJECT:
+ return new GameActionObject(buffer);
+ case TYPE_REGIONOBJECT:
+ return new RegionObject(buffer);
+ case TYPE_GAMEOBJECT:
+ return new GameObject(buffer);
+ case TYPE_NETRANDOM:
+ return new NetRandom(buffer);
+ case TYPE_OBJECT:
+ byte[] b = new byte[buffer.readInt()];
+ buffer.read(b);
+ ObjectInputStream ois = new ObjectInputStream(
+ /* new GZIPInputStream */(new ByteArrayInputStream(b)));
+ try {
+ return ois.readObject();
+ } catch (ClassNotFoundException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ return null;
+ }
+ default:
+ throw new IllegalArgumentException(
+ "Object deserilation not supported: " + type);
+ }
+ }
+}
Added: trunk/P2Play/src/org/p2play/scripting/pnuts/object/GameStateClass.java
===================================================================
--- trunk/P2Play/src/org/p2play/scripting/pnuts/object/GameStateClass.java (rev 0)
+++ trunk/P2Play/src/org/p2play/scripting/pnuts/object/GameStateClass.java 2006-12-27 18:05:30 UTC (rev 14)
@@ -0,0 +1,53 @@
+/*
+ * Copyright 2006 P2Play.org
+ * All rights reserved.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+package org.p2play.scripting.pnuts.object;
+
+import java.util.HashMap;
+import java.util.Random;
+
+import org.jdom.Element;
+import org.p2play.net.region.Region;
+
+public class GameStateClass extends GameClass{
+ public static final String GAME_TIME="GAMETIME";
+ public static final String RANDOM="RANDOM";
+ public static final String REGION = "REGION";
+
+ public GameStateClass(String game, String name, Element element) {
+ super(game, name, element);
+ }
+
+ @Override
+ void parse(HashMap<String, GameClass> classMap) {
+ super.parse(classMap);
+
+ addVariable(GAME_TIME, Long.class);
+ addVariable(RANDOM, Random.class);
+ addVariable(REGION, String.class);
+ }
+
+ @Override
+ public GameObject createInstance(Object... objects) {
+ throw new IllegalArgumentException();
+ }
+
+ public GameStateObject createInstance(Region region, long gameTime) {
+ return new GameStateObject(this,region,gameTime);
+ }
+}
Added: trunk/P2Play/src/org/p2play/scripting/pnuts/object/GameStateObject.java
===================================================================
--- trunk/P2Play/src/org/p2play/scripting/pnuts/object/GameStateObject.java (rev 0)
+++ trunk/P2Play/src/org/p2play/scripting/pnuts/object/GameStateObject.java 2006-12-27 18:05:30 UTC (rev 14)
@@ -0,0 +1,86 @@
+/*
+ * Copyright 2006 P2Play.org
+ * All rights reserved.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+package org.p2play.scripting.pnuts.object;
+
+import java.io.IOException;
+import java.util.List;
+import java.util.Random;
+
+import org.p2play.net.region.GameState;
+import org.p2play.net.region.Region;
+import org.p2play.util.NetRandom;
+
+import rice.p2p.commonapi.rawserialization.InputBuffer;
+
+public class GameStateObject extends GameObject implements GameState {
+ private transient GameClass gameClass = null;
+
+ public GameStateObject(GameClass gameClass, Region region, long gameTime) {
+ super(gameClass);
+ set(GameStateClass.GAME_TIME, gameTime);
+ set(GameStateClass.RANDOM, new NetRandom(gameTime));
+ set(GameStateClass.REGION, region);
+ }
+
+ public GameStateObject(InputBuffer buffer) throws IOException {
+ super(buffer);
+ // TODO Auto-generated constructor stub
+ }
+
+ public long getGameTime() {
+ return (Long) get(GameStateClass.GAME_TIME);
+ }
+
+ public Random getRandom() {
+ return (Random) get(GameStateClass.RANDOM);
+ }
+
+ public Region getRegion() {
+ return (Region) get(GameStateClass.REGION);
+ }
+
+ public void tick() {
+ set(GameStateClass.GAME_TIME, (Long) get(GameStateClass.GAME_TIME) + 1);
+ }
+
+ public int randomInt() {
+ return getRandom().nextInt();
+ }
+
+ public int randomInt(int n) {
+ return getRandom().nextInt(n);
+ }
+
+ @Override
+ public GameClass getGameClass() {
+ if (gameClass == null)
+ gameClass = GameDescriptor.getGameDescriptor(getGame())
+ .getGameStateClass();
+ return gameClass;
+ }
+
+ public boolean isIdle() {
+ return GameDescriptor.getGameDescriptor(getGame()).isIdle(this);
+ }
+
+ public List<Region> getNeighbourRegions() {
+ return GameDescriptor.getGameDescriptor(getGame())
+ .getNeighbourRegionIdentifiers(this.getRegion());
+ }
+}
Added: trunk/P2Play/src/org/p2play/scripting/pnuts/object/RegionClass.java
===================================================================
--- trunk/P2Play/src/org/p2play/scripting/pnuts/object/RegionClass.java (rev 0)
+++ trunk/P2Play/src/org/p2play/scripting/pnuts/object/RegionClass.java 2006-12-27 18:05:30 UTC (rev 14)
@@ -0,0 +1,36 @@
+/*
+ * Copyright 2006 P2Play.org
+ * All rights reserved.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+package org.p2play.scripting.pnuts.object;
+
+import org.jdom.Element;
+
+public class RegionClass extends GameClass {
+ public RegionClass(String game, String name, Element element) {
+ super(game, name, element);
+ }
+
+ @Override
+ public GameObject createInstance(Object... objects) {
+ GameObject gameObject=new RegionObject(this);
+
+ fillObject(gameObject, objects);
+
+ return gameObject;
+ }
+}
Added: trunk/P2Play/src/org/p2play/scripting/pnuts/object/RegionObject.java
===================================================================
--- trunk/P2Play/src/org/p2play/scripting/pnuts/object/RegionObject.java (rev 0)
+++ trunk/P2Play/src/org/p2play/scripting/pnuts/object/RegionObject.java 2006-12-27 18:05:30 UTC (rev 14)
@@ -0,0 +1,93 @@
+/*
+ * Copyright 2006 P2Play.org
+ * All rights reserved.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+package org.p2play.scripting.pnuts.object;
+
+import java.io.IOException;
+import java.util.Iterator;
+
+import org.p2play.net.region.Region;
+
+import pnuts.lang.Context;
+import rice.p2p.commonapi.rawserialization.InputBuffer;
+
+public class RegionObject extends GameObject implements Region {
+
+ private transient String regionIdentifier;
+
+ public RegionObject(GameClass gameClass) {
+ super(gameClass);
+ }
+
+ public RegionObject(InputBuffer buffer) throws IOException {
+ super(buffer);
+ }
+
+ @Override
+ public GameClass getGameClass() {
+ if (super.getGameClass() == null)
+ setGameClass(GameDescriptor.getGameDescriptor(getGame())
+ .getRegionClass());
+ return super.getGameClass();
+ }
+
+ @Override
+ public void set(String name, Object value) {
+ super.set(name, value);
+ regionIdentifier = null;
+ }
+
+ @Override
+ public void set(String name, Object value, Context context) {
+ super.set(name, value, context);
+ regionIdentifier = null;
+ ;
+ }
+
+ public String getRegionIdentifier() {
+ if (regionIdentifier == null) {
+ regionIdentifier = getGame();
+
+ Iterator<String> iterator = getGameClass().iteratorOfVariables();
+ while (iterator.hasNext()) {
+ String name = iterator.next();
+ regionIdentifier += "." + get(name).toString();
+ }
+ }
+ return regionIdentifier;
+ }
+
+ @Override
+ public String toString() {
+ return getRegionIdentifier();
+ }
+
+ @Override
+ public int hashCode() {
+ return getRegionIdentifier().hashCode();
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (!(obj instanceof RegionObject))
+ return false;
+ else
+ return getRegionIdentifier().equals(
+ ((RegionObject) obj).getRegionIdentifier());
+ }
+}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|