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