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