project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/EngineProtocol/Team.java
branchhedgeroid
changeset 6047 10011f051f9c
child 6432 ffb18b44415d
equal deleted inserted replaced
6045:9a7cc0f29430 6047:10011f051f9c
       
     1 /*
       
     2  * Hedgewars for Android. An Android port of Hedgewars, a free turn based strategy game
       
     3  * Copyright (c) 2011 Richard Deurwaarder <xeli@xelification.com>
       
     4  *
       
     5  * This program is free software; you can redistribute it and/or modify
       
     6  * it under the terms of the GNU General Public License as published by
       
     7  * the Free Software Foundation; version 2 of the License
       
     8  *
       
     9  * This program is distributed in the hope that it will be useful,
       
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    12  * GNU General Public License for more details.
       
    13  *
       
    14  * You should have received a copy of the GNU General Public License
       
    15  * along with this program; if not, write to the Free Software
       
    16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
       
    17  */
       
    18 
       
    19 package org.hedgewars.hedgeroid.EngineProtocol;
       
    20 
       
    21 import java.io.BufferedReader;
       
    22 import java.io.FileNotFoundException;
       
    23 import java.io.FileReader;
       
    24 import java.io.IOException;
       
    25 import java.io.OutputStream;
       
    26 import java.util.ArrayList;
       
    27 
       
    28 import org.xmlpull.v1.XmlPullParser;
       
    29 import org.xmlpull.v1.XmlPullParserException;
       
    30 import org.xmlpull.v1.XmlPullParserFactory;
       
    31 import org.xmlpull.v1.XmlSerializer;
       
    32 
       
    33 import android.os.Parcel;
       
    34 import android.os.Parcelable;
       
    35 import android.util.Xml;
       
    36 
       
    37 public class Team implements Parcelable{
       
    38 
       
    39 	public static final String DIRECTORY_TEAMS = "teams";
       
    40 	private static final Integer[] TEAM_COLORS = {
       
    41 		0xd12b42, /* red    */ 
       
    42 		0x4980c1, /* blue   */ 
       
    43 		0x6ab530, /* green  */ 
       
    44 		0xbc64c4, /* purple */ 
       
    45 		0xe76d14, /* orange */ 
       
    46 		0x3fb6e6, /* cyan   */ 
       
    47 		0xe3e90c, /* yellow */ 
       
    48 		0x61d4ac, /* mint   */ 
       
    49 		0xf1c3e1, /* pink   */ 
       
    50 		/* add new colors here */
       
    51 	};
       
    52 
       
    53 //	private static final Integer[] TEAM_COLORS = {
       
    54 //		0xff0000, /* red    */ 
       
    55 //		0x00ff00, /* blue   */ 
       
    56 //		0x0000ff, /* green  */ 
       
    57 //	};
       
    58 
       
    59 	private static final int STATE_START = 0;
       
    60 	private static final int STATE_ROOT = 1;
       
    61 	private static final int STATE_HOG_ROOT = 2;
       
    62 
       
    63 	public String name, grave, flag, voice, fort, hash;
       
    64 
       
    65 	public static int maxNumberOfHogs = 0;
       
    66 	public static int maxNumberOfTeams = 0;
       
    67 
       
    68 	static{
       
    69 		maxNumberOfHogs = PascalExports.HWgetMaxNumberOfHogs();
       
    70 		maxNumberOfTeams = PascalExports.HWgetMaxNumberOfTeams();
       
    71 	}
       
    72 	public String[] hats = new String[maxNumberOfHogs];
       
    73 	public String[] hogNames = new String[maxNumberOfHogs];
       
    74 	public int[] levels = new int[maxNumberOfHogs];
       
    75 
       
    76 	public int hogCount = 4;
       
    77 	public int color = TEAM_COLORS[0];
       
    78 
       
    79 	public Team(){
       
    80 	}
       
    81 
       
    82 	public Team(Parcel in){
       
    83 		readFromParcel(in);
       
    84 	}
       
    85 
       
    86 	public boolean equals(Object o){
       
    87 		if(super.equals(o)) return true;
       
    88 		else if(o instanceof Team){
       
    89 			Team t = (Team)o;
       
    90 			boolean ret = name.equals(t.name);
       
    91 			ret &= grave.equals(t.grave);
       
    92 			ret &= flag.equals(t.flag);
       
    93 			ret &= voice.equals(t.voice);
       
    94 			ret &= fort.equals(t.fort);
       
    95 			ret &= hash.equals(t.hash);
       
    96 			return ret;
       
    97 		}else{
       
    98 			return false;
       
    99 		}
       
   100 	}
       
   101 
       
   102 	public void setRandomColor(int[] illegalcolors){
       
   103 		Integer[] colorsToPickFrom = TEAM_COLORS;
       
   104 		if(illegalcolors != null){
       
   105 			ArrayList<Integer> colors = new ArrayList<Integer>();
       
   106 			for(int color : TEAM_COLORS){
       
   107 				boolean validColor = true;
       
   108 				for(int illegal : illegalcolors){
       
   109 					if(color == illegal) validColor = false;
       
   110 				}
       
   111 				if(validColor) colors.add(color);
       
   112 			}
       
   113 			if(colors.size() != 0) colorsToPickFrom = colors.toArray(new Integer[1]);
       
   114 		}
       
   115 		int index = (int)Math.round(Math.random()*(colorsToPickFrom.length-1));
       
   116 		color = colorsToPickFrom[index];
       
   117 	}
       
   118 
       
   119 
       
   120 	public void sendToEngine(EngineProtocolNetwork epn, int hogCount, int health) throws IOException{
       
   121 		epn.sendToEngine(String.format("eaddteam %s %d %s", hash, color, name));
       
   122 		epn.sendToEngine(String.format("egrave %s", grave));
       
   123 		epn.sendToEngine(String.format("efort %s", fort));
       
   124 		epn.sendToEngine(String.format("evoicepack %s", voice));
       
   125 		epn.sendToEngine(String.format("eflag %s", flag));
       
   126 
       
   127 		for(int i = 0; i < hogCount; i++){
       
   128 			epn.sendToEngine(String.format("eaddhh %d %d %s", levels[i], health, hogNames[i]));
       
   129 			epn.sendToEngine(String.format("ehat %s", hats[i]));
       
   130 		}
       
   131 	}
       
   132 
       
   133 	/*
       
   134 	 * XML METHODS
       
   135 	 */
       
   136 
       
   137 	/**
       
   138 	 * Read the xml file path and convert it to a Team object
       
   139 	 * @param path absolute path to the xml file
       
   140 	 * @return
       
   141 	 */
       
   142 	public static Team getTeamFromXml(String path){
       
   143 		try {
       
   144 			XmlPullParserFactory xmlPullFactory = XmlPullParserFactory.newInstance();
       
   145 			XmlPullParser xmlPuller = xmlPullFactory.newPullParser();
       
   146 
       
   147 			BufferedReader br = new BufferedReader(new FileReader(path), 1024);
       
   148 			xmlPuller.setInput(br);
       
   149 			Team team = new Team();
       
   150 			int hogCounter = 0;
       
   151 
       
   152 			int eventType = xmlPuller.getEventType();
       
   153 			int state = STATE_START;
       
   154 			while(eventType != XmlPullParser.END_DOCUMENT){
       
   155 				switch(state){
       
   156 				case STATE_START:
       
   157 					if(eventType == XmlPullParser.START_TAG && xmlPuller.getName().equals("team")) state = STATE_ROOT;
       
   158 					else if(eventType != XmlPullParser.START_DOCUMENT) throwException(path, eventType);
       
   159 					break;
       
   160 				case STATE_ROOT:
       
   161 					if(eventType == XmlPullParser.START_TAG){
       
   162 						if(xmlPuller.getName().toLowerCase().equals("name")){
       
   163 							team.name = getXmlText(xmlPuller, "name");
       
   164 						}else if(xmlPuller.getName().toLowerCase().equals("flag")){
       
   165 							team.flag= getXmlText(xmlPuller, "flag");
       
   166 						}else if(xmlPuller.getName().toLowerCase().equals("voice")){
       
   167 							team.voice = getXmlText(xmlPuller, "voice");
       
   168 						}else if(xmlPuller.getName().toLowerCase().equals("grave")){
       
   169 							team.grave = getXmlText(xmlPuller, "grave");
       
   170 						}else if(xmlPuller.getName().toLowerCase().equals("fort")){
       
   171 							team.fort = getXmlText(xmlPuller, "fort");
       
   172 						}else if(xmlPuller.getName().toLowerCase().equals("hash")){
       
   173 							team.hash = getXmlText(xmlPuller, "hash");
       
   174 						}else if(xmlPuller.getName().toLowerCase().equals("hog")){
       
   175 							state = STATE_HOG_ROOT;
       
   176 						}else throwException(xmlPuller.getName(), eventType);
       
   177 					}else if(eventType == XmlPullParser.END_TAG) state = STATE_START;
       
   178 					else throwException(xmlPuller.getText(), eventType);
       
   179 					break;
       
   180 				case STATE_HOG_ROOT:
       
   181 					if(eventType == XmlPullParser.START_TAG){
       
   182 						if(xmlPuller.getName().toLowerCase().equals("name")){
       
   183 							team.hogNames[hogCounter] = getXmlText(xmlPuller, "name");
       
   184 						}else if(xmlPuller.getName().toLowerCase().equals("hat")){
       
   185 							team.hats[hogCounter] = getXmlText(xmlPuller, "hat");
       
   186 						}else if(xmlPuller.getName().toLowerCase().equals("level")){
       
   187 							team.levels[hogCounter] = Integer.parseInt(getXmlText(xmlPuller, "level"));
       
   188 						}else throwException(xmlPuller.getText(), eventType);
       
   189 					}else if(eventType == XmlPullParser.END_TAG){
       
   190 						hogCounter++;
       
   191 						state = STATE_ROOT;
       
   192 					}else throwException(xmlPuller.getText(), eventType);
       
   193 					break;
       
   194 				}
       
   195 				eventType = getEventType(xmlPuller);
       
   196 			}//end while(eventtype != END_DOCUMENT
       
   197 			return team;
       
   198 		} catch (NumberFormatException e){
       
   199 			e.printStackTrace();
       
   200 		} catch (XmlPullParserException e) {
       
   201 			e.printStackTrace();
       
   202 		} catch (FileNotFoundException e) {
       
   203 			e.printStackTrace();
       
   204 		} catch (IOException e) {
       
   205 			e.printStackTrace();
       
   206 		}
       
   207 		return null;
       
   208 	}
       
   209 
       
   210 	private static String getXmlText(XmlPullParser xmlPuller, String parentTag)throws XmlPullParserException, IOException{
       
   211 		if(getEventType(xmlPuller) == XmlPullParser.TEXT){
       
   212 			String txt = xmlPuller.getText();
       
   213 			if(getEventType(xmlPuller) == XmlPullParser.END_TAG && xmlPuller.getName().toLowerCase().equals(parentTag)){
       
   214 				return txt;
       
   215 			}
       
   216 		}
       
   217 		throw new XmlPullParserException("malformed xml file on string read from tag: " + parentTag);
       
   218 	}
       
   219 
       
   220 	/**
       
   221 	 * Skips whitespaces..
       
   222 	 */
       
   223 	private static int getEventType(XmlPullParser xmlPuller)throws XmlPullParserException, IOException{
       
   224 		int eventType = xmlPuller.next();
       
   225 		while(eventType == XmlPullParser.TEXT && xmlPuller.isWhitespace()){
       
   226 			eventType = xmlPuller.next();
       
   227 		}
       
   228 		return eventType;
       
   229 	}
       
   230 
       
   231 	private static void throwException(String file, int eventType){
       
   232 		throw new IllegalArgumentException(String.format("Xml file: %s malformed with error: %d.", file, eventType));
       
   233 	}
       
   234 
       
   235 	public void writeToXml(OutputStream os){
       
   236 		XmlSerializer serializer = Xml.newSerializer();
       
   237 		try{
       
   238 			serializer.setOutput(os, "UTF-8");	
       
   239 			serializer.startDocument("UTF-8", true);
       
   240 			serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
       
   241 
       
   242 			serializer.startTag(null, "team");
       
   243 			serializer.startTag(null, "name");
       
   244 			serializer.text(name);
       
   245 			serializer.endTag(null, "name");
       
   246 			serializer.startTag(null, "flag");
       
   247 			serializer.text(flag);
       
   248 			serializer.endTag(null, "flag");
       
   249 			serializer.startTag(null, "fort");
       
   250 			serializer.text(fort);
       
   251 			serializer.endTag(null, "fort");
       
   252 			serializer.startTag(null, "grave");
       
   253 			serializer.text(grave);
       
   254 			serializer.endTag(null, "grave");
       
   255 			serializer.startTag(null, "voice");
       
   256 			serializer.text(voice);
       
   257 			serializer.endTag(null, "voice");
       
   258 			serializer.startTag(null, "hash");
       
   259 			serializer.text(hash);
       
   260 			serializer.endTag(null, "hash");
       
   261 
       
   262 			for(int i = 0; i < maxNumberOfHogs; i++){
       
   263 				serializer.startTag(null, "hog");
       
   264 				serializer.startTag(null, "name");
       
   265 				serializer.text(hogNames[i]);
       
   266 				serializer.endTag(null, "name");
       
   267 				serializer.startTag(null, "hat");
       
   268 				serializer.text(hats[i]);
       
   269 				serializer.endTag(null, "hat");
       
   270 				serializer.startTag(null, "level");
       
   271 				serializer.text(String.valueOf(levels[i]));
       
   272 				serializer.endTag(null, "level");
       
   273 
       
   274 				serializer.endTag(null, "hog");
       
   275 			}
       
   276 			serializer.endTag(null, "team");
       
   277 			serializer.endDocument();
       
   278 			serializer.flush();
       
   279 
       
   280 		} catch (IOException e) {
       
   281 			e.printStackTrace();
       
   282 		}finally{
       
   283 			try {
       
   284 				os.close();
       
   285 			} catch (IOException e) {}
       
   286 		}
       
   287 	}
       
   288 	/*
       
   289 	 * END XML METHODS
       
   290 	 */
       
   291 
       
   292 
       
   293 
       
   294 	/*
       
   295 	 * PARCABLE METHODS
       
   296 	 */
       
   297 
       
   298 	public int describeContents() {
       
   299 		return 0;
       
   300 	}
       
   301 
       
   302 	public void writeToParcel(Parcel dest, int flags) {
       
   303 		dest.writeString(name);
       
   304 		dest.writeString(grave);
       
   305 		dest.writeString(flag);
       
   306 		dest.writeString(voice);
       
   307 		dest.writeString(fort);
       
   308 		dest.writeString(hash);
       
   309 		dest.writeStringArray(hats);
       
   310 		dest.writeStringArray(hogNames);
       
   311 		dest.writeIntArray(levels);
       
   312 		dest.writeInt(color);
       
   313 		dest.writeInt(hogCount);
       
   314 	}
       
   315 
       
   316 
       
   317 	public void readFromParcel(Parcel src){
       
   318 		name = src.readString();
       
   319 		grave = src.readString();
       
   320 		flag = src.readString();
       
   321 		voice = src.readString();
       
   322 		fort = src.readString();
       
   323 		hash = src.readString();
       
   324 		src.readStringArray(hats);
       
   325 		src.readStringArray(hogNames);
       
   326 		src.readIntArray(levels);
       
   327 		color = src.readInt();
       
   328 		hogCount = src.readInt();
       
   329 	}
       
   330 
       
   331 	public static final Parcelable.Creator<Team> CREATOR = new Parcelable.Creator<Team>() {
       
   332 		public Team createFromParcel(Parcel source) {
       
   333 			return new Team(source);
       
   334 		}
       
   335 		public Team[] newArray(int size) {
       
   336 			return new Team[size];
       
   337 		}
       
   338 
       
   339 	};
       
   340 
       
   341 	/*
       
   342 	 * END PARCABLE METHODS
       
   343 	 */
       
   344 
       
   345 }