project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/Datastructures/Weapon.java
branchhedgeroid
changeset 7857 2bc61f8841a1
parent 7855 ddcdedd3330b
parent 7695 6237d2f002ba
child 7859 519d5bc91dd3
equal deleted inserted replaced
7855:ddcdedd3330b 7857:2bc61f8841a1
     1 /*
       
     2  * Hedgewars for Android. An Android port of Hedgewars, a free turn based strategy game
       
     3  * Copyright (c) 2011-2012 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.util.ArrayList;
       
    27 
       
    28 import org.hedgewars.hedgeroid.EngineProtocol.EngineProtocolNetwork;
       
    29 import org.hedgewars.hedgeroid.EngineProtocol.PascalExports;
       
    30 import org.xmlpull.v1.XmlPullParser;
       
    31 import org.xmlpull.v1.XmlPullParserException;
       
    32 import org.xmlpull.v1.XmlPullParserFactory;
       
    33 
       
    34 import android.content.Context;
       
    35 import android.os.Parcel;
       
    36 import android.os.Parcelable;
       
    37 
       
    38 public class Weapon implements Parcelable, Comparable<Weapon>{
       
    39 
       
    40 	public static final String DIRECTORY_WEAPON = "weapons";
       
    41 	
       
    42 	private String name;
       
    43 	private String QT;
       
    44 	private String prob;
       
    45 	private String delay;
       
    46 	private String crate;
       
    47 	private static int maxWeapons;
       
    48 	
       
    49 	static{
       
    50 		maxWeapons = PascalExports.HWgetNumberOfWeapons();
       
    51 	}
       
    52 	
       
    53 	public Weapon(String _name, String _QT, String _prob, String _delay, String _crate){
       
    54 		name = _name;
       
    55 		
       
    56 		//Incase there's a newer ammoStore which is bigger we append with zeros
       
    57 		StringBuffer sb = new StringBuffer();
       
    58 		while(_QT.length() + sb.length() < maxWeapons){
       
    59 			sb.append('0');
       
    60 		}
       
    61 		
       
    62 		QT = String.format("e%s %s%s", "ammloadt", _QT, sb);
       
    63 		prob = String.format("e%s %s%s", "ammprob", _prob, sb);
       
    64 		delay = String.format("e%s %s%s", "ammdelay", _delay, sb);
       
    65 		crate = String.format("e%s %s%s", "ammreinf", _crate, sb);
       
    66 	}
       
    67 	
       
    68 	public Weapon(Parcel in){
       
    69 		readFromParcel(in);
       
    70 	}
       
    71 	
       
    72 	public String toString(){
       
    73 		return name;
       
    74 	}
       
    75 	
       
    76 	public void sendToEngine(EngineProtocolNetwork epn, int teamsCount) throws IOException{
       
    77 		epn.sendToEngine(QT);//command prefix is already in string 
       
    78 		epn.sendToEngine(prob);
       
    79 		epn.sendToEngine(delay);
       
    80 		epn.sendToEngine(crate);
       
    81 		
       
    82 		for(int i = 0; i < teamsCount; i++){
       
    83 			epn.sendToEngine("eammstore");
       
    84 		}
       
    85 	}
       
    86 	
       
    87 	public static final int STATE_START = 0;
       
    88 	public static final int STATE_ROOT = 1;
       
    89 	public static final int STATE_NAME = 2;
       
    90 	public static final int STATE_QT = 3;
       
    91 	public static final int STATE_PROBABILITY = 4;
       
    92 	public static final int STATE_DELAY = 5;
       
    93 	public static final int STATE_CRATE = 6;
       
    94 	
       
    95 	public static ArrayList<Weapon> getWeapons(Context c) throws IllegalArgumentException{
       
    96 		String dir = c.getFilesDir().getAbsolutePath() + '/' + DIRECTORY_WEAPON + '/';
       
    97 		String[] files = new File(dir).list();
       
    98 		if(files == null) files = new String[]{};
       
    99 		
       
   100 		ArrayList<Weapon> weapons = new ArrayList<Weapon>();
       
   101 
       
   102 		try {
       
   103 			XmlPullParserFactory xmlPullFactory = XmlPullParserFactory.newInstance();
       
   104 			XmlPullParser xmlPuller = xmlPullFactory.newPullParser();
       
   105 			
       
   106 			for(String file : files){
       
   107 				BufferedReader br = new BufferedReader(new FileReader(dir + file), 1024);
       
   108 				xmlPuller.setInput(br);
       
   109 				String name = null;
       
   110 				String qt = null;
       
   111 				String prob = null;
       
   112 				String delay = null;
       
   113 				String crate = null;
       
   114 				
       
   115 				int eventType = xmlPuller.getEventType();
       
   116 				int state = STATE_START;
       
   117 				while(eventType != XmlPullParser.END_DOCUMENT){
       
   118 					switch(state){
       
   119 					case STATE_START:
       
   120 						if(eventType == XmlPullParser.START_TAG && xmlPuller.getName().equals("weapon")) state = STATE_ROOT;
       
   121 						else if(eventType != XmlPullParser.START_DOCUMENT) throwException(file, eventType);
       
   122 						break;
       
   123 					case STATE_ROOT:
       
   124 						if(eventType == XmlPullParser.START_TAG){
       
   125 							if(xmlPuller.getName().toLowerCase().equals("qt")) state = STATE_QT;
       
   126 							else if(xmlPuller.getName().toLowerCase().equals("name")) state = STATE_NAME;
       
   127 							else if(xmlPuller.getName().toLowerCase().equals("probability")) state = STATE_PROBABILITY;
       
   128 							else if(xmlPuller.getName().toLowerCase().equals("delay")) state = STATE_DELAY;
       
   129 							else if(xmlPuller.getName().toLowerCase().equals("crate")) state = STATE_CRATE;
       
   130 							else throwException(file, eventType);
       
   131 						}else if(eventType == XmlPullParser.END_TAG) state = STATE_START;
       
   132 						else throwException(xmlPuller.getText(), eventType);
       
   133 						break;
       
   134 					case STATE_NAME:
       
   135 						if(eventType == XmlPullParser.TEXT) name = xmlPuller.getText().trim();
       
   136 						else if(eventType == XmlPullParser.END_TAG) state = STATE_ROOT;
       
   137 						else throwException(file, eventType);
       
   138 						break;
       
   139 					case STATE_QT:
       
   140 						if(eventType == XmlPullParser.TEXT) qt = xmlPuller.getText().trim();
       
   141 						else if(eventType == XmlPullParser.END_TAG) state = STATE_ROOT;
       
   142 						else throwException(file, eventType);
       
   143 						break;
       
   144 					case STATE_PROBABILITY:
       
   145 						if(eventType == XmlPullParser.TEXT) prob = xmlPuller.getText().trim();
       
   146 						else if(eventType == XmlPullParser.END_TAG) state = STATE_ROOT;
       
   147 						else throwException(file, eventType);
       
   148 						break;
       
   149 					case STATE_DELAY:
       
   150 						if(eventType == XmlPullParser.TEXT) delay = xmlPuller.getText().trim();
       
   151 						else if(eventType == XmlPullParser.END_TAG) state = STATE_ROOT;
       
   152 						else throwException(file, eventType);
       
   153 						break;
       
   154 					case STATE_CRATE:
       
   155 						if(eventType == XmlPullParser.TEXT) crate = xmlPuller.getText().trim();
       
   156 						else if(eventType == XmlPullParser.END_TAG) state = STATE_ROOT;
       
   157 						else throwException(file, eventType);
       
   158 						break;
       
   159 					}
       
   160 					eventType = xmlPuller.next();
       
   161 					while(eventType == XmlPullParser.TEXT && xmlPuller.isWhitespace()){//Skip whitespaces
       
   162 						eventType = xmlPuller.next();
       
   163 					}
       
   164 				}//end while(eventtype != END_DOCUMENT
       
   165 				weapons.add(new Weapon(name, qt, prob, delay, crate));
       
   166 			}//end for(string file : files
       
   167 			return weapons;
       
   168 			
       
   169 		} catch (XmlPullParserException e) {
       
   170 			e.printStackTrace();
       
   171 		} catch (FileNotFoundException e) {
       
   172 			e.printStackTrace();
       
   173 		} catch (IOException e) {
       
   174 			e.printStackTrace();
       
   175 		}
       
   176 		return new ArrayList<Weapon>();//TODO handle correctly
       
   177 	}
       
   178 	
       
   179 	private static void throwException(String file, int eventType){
       
   180 		throw new IllegalArgumentException(String.format("Xml file: %s malformed with eventType: %d.", file, eventType));
       
   181 	}
       
   182 
       
   183 	public int describeContents() {
       
   184 		return 0;
       
   185 	}
       
   186 
       
   187 	public void writeToParcel(Parcel dest, int flags) {
       
   188 		dest.writeString(name);
       
   189 		dest.writeString(QT);
       
   190 		dest.writeString(prob);
       
   191 		dest.writeString(delay);
       
   192 		dest.writeString(crate);
       
   193 	}
       
   194 	
       
   195 	private void readFromParcel(Parcel src){
       
   196 		name = src.readString();
       
   197 		QT = src.readString();
       
   198 		prob = src.readString();
       
   199 		delay = src.readString();
       
   200 		crate = src.readString();
       
   201 	}
       
   202 	
       
   203 	public static final Parcelable.Creator<Weapon> CREATOR = new Parcelable.Creator<Weapon>() {
       
   204 		public Weapon createFromParcel(Parcel source) {
       
   205 			return new Weapon(source);
       
   206 		}
       
   207 		public Weapon[] newArray(int size) {
       
   208 			return new Weapon[size];
       
   209 		}
       
   210 		
       
   211 	};
       
   212 
       
   213 	public int compareTo(Weapon another) {
       
   214 		return name.compareTo(another.name);
       
   215 	}
       
   216 	
       
   217 	
       
   218 }